From c77f25ae31ce1720038e404e498f3a99af4e9d9a Mon Sep 17 00:00:00 2001 From: leleliu008 Date: Sun, 28 Apr 2024 20:05:32 +0800 Subject: [PATCH] optimized Signed-off-by: leleliu008 --- algorithm/base16/src/main/base16-decode.c | 32 +++-- algorithm/base16/src/main/base16-encode.c | 32 +++-- algorithm/bcc/libbcc.pc | 11 -- .../chinese-calendar/libchinese-calendar.pc | 11 -- algorithm/url/liburl.pc | 11 -- library/libgit2/CMakeLists.txt | 4 +- library/libgit2/git-submodule-update.c | 76 ++++++++++ library/mbedTLS/base64/base64.pc | 11 -- library/mbedTLS/base64/src/include/base64.h | 4 +- library/mbedTLS/base64/src/lib/base64.c | 30 +--- .../mbedTLS/base64/src/main/base64-decode.c | 46 ++++-- .../mbedTLS/base64/src/main/base64-encode.c | 46 ++++-- .../openssl/SHA-256/src/include/sha256sum.h | 7 +- library/openssl/SHA-256/src/lib/sha256sum.c | 131 ++++++++++++++---- library/openssl/SHA-256/src/main/sha256sum.c | 51 ++++--- standard/posix/unistd.h/chdir.c | 22 +-- standard/posix/unistd.h/getcwd.c | 21 +-- utils/sysinfo/README.md | 2 +- 18 files changed, 363 insertions(+), 185 deletions(-) delete mode 100644 algorithm/bcc/libbcc.pc delete mode 100644 algorithm/chinese-calendar/libchinese-calendar.pc delete mode 100644 algorithm/url/liburl.pc create mode 100644 library/libgit2/git-submodule-update.c delete mode 100644 library/mbedTLS/base64/base64.pc diff --git a/algorithm/base16/src/main/base16-decode.c b/algorithm/base16/src/main/base16-decode.c index 83eca3a..6f6651d 100644 --- a/algorithm/base16/src/main/base16-decode.c +++ b/algorithm/base16/src/main/base16-decode.c @@ -1,7 +1,9 @@ #include #include #include + #include + #include #define COLOR_GREEN "\033[0;32m" @@ -49,17 +51,17 @@ static void showHelp() { int main(int argc, char *argv[]) { if (argc < 2) { - char inputBuff[1024]; + char readBuf[1024]; for (;;) { - ssize_t inputSize = read(STDIN_FILENO, inputBuff, 1024); + ssize_t readSize = read(STDIN_FILENO, readBuf, 1024); - if (inputSize < 0) { + if (readSize == -1) { perror(NULL); return 1; } - if (inputSize == 0) { + if (readSize == 0) { if (isatty(STDOUT_FILENO)) { printf("\n"); } @@ -67,19 +69,26 @@ int main(int argc, char *argv[]) { return 0; } - size_t outputSize = inputSize >> 1; + size_t outputSize = readSize >> 1; unsigned char outputBuff[outputSize]; - int ret = base16_decode(outputBuff, inputBuff, inputSize); + int ret = base16_decode(outputBuff, readBuf, readSize); if (ret != 0) { return ret; } - if (write(STDOUT_FILENO, outputBuff, outputSize) < 0) { + ssize_t writtenSize = write(STDOUT_FILENO, outputBuff, outputSize); + + if (writtenSize == -1) { perror(NULL); return 1; } + + if ((size_t)writtenSize != outputSize) { + fprintf(stderr, "not fully written to stdout.\n"); + return 1; + } } } @@ -108,11 +117,18 @@ int main(int argc, char *argv[]) { return ret; } - if (write(STDOUT_FILENO, outputBuff, outputSize) < 0) { + ssize_t writtenSize = write(STDOUT_FILENO, outputBuff, outputSize); + + if (writtenSize == -1) { perror(NULL); return 1; } + if ((size_t)writtenSize != outputSize) { + fprintf(stderr, "not fully written to stdout.\n"); + return 1; + } + if (isatty(STDOUT_FILENO)) { printf("\n"); } diff --git a/algorithm/base16/src/main/base16-encode.c b/algorithm/base16/src/main/base16-encode.c index 67787e2..290970d 100644 --- a/algorithm/base16/src/main/base16-encode.c +++ b/algorithm/base16/src/main/base16-encode.c @@ -1,7 +1,9 @@ #include #include #include + #include + #include #define COLOR_GREEN "\033[0;32m" @@ -49,17 +51,17 @@ static void showHelp() { int main(int argc, char *argv[]) { if (argc < 2) { - unsigned char inputBuff[1024]; + unsigned char readBuf[1024]; for (;;) { - ssize_t inputSize = read(STDIN_FILENO, inputBuff, 1024); + ssize_t readSize = read(STDIN_FILENO, readBuf, 1024); - if (inputSize < 0) { + if (readSize == -1) { perror(NULL); return 1; } - if (inputSize == 0) { + if (readSize == 0) { if (isatty(STDOUT_FILENO)) { printf("\n"); } @@ -67,19 +69,26 @@ int main(int argc, char *argv[]) { return 0; } - size_t outputSize = inputSize << 1; + size_t outputSize = readSize << 1; char outputBuff[outputSize]; - int ret = base16_encode(outputBuff, inputBuff, inputSize, true); + int ret = base16_encode(outputBuff, readBuf, readSize, true); if (ret != 0) { return ret; } - if (write(STDOUT_FILENO, outputBuff, outputSize) < 0) { + ssize_t writtenSize = write(STDOUT_FILENO, outputBuff, outputSize); + + if (writtenSize == -1) { perror(NULL); return 1; } + + if ((size_t)writtenSize != outputSize) { + fprintf(stderr, "not fully written to stdout.\n"); + return 1; + } } } @@ -108,11 +117,18 @@ int main(int argc, char *argv[]) { return ret; } - if (write(STDOUT_FILENO, outputBuff, outputSize) < 0) { + ssize_t writtenSize = write(STDOUT_FILENO, outputBuff, outputSize); + + if (writtenSize == -1) { perror(NULL); return 1; } + if ((size_t)writtenSize != outputSize) { + fprintf(stderr, "not fully written to stdout.\n"); + return 1; + } + if (isatty(STDOUT_FILENO)) { printf("\n"); } diff --git a/algorithm/bcc/libbcc.pc b/algorithm/bcc/libbcc.pc deleted file mode 100644 index 3773bde..0000000 --- a/algorithm/bcc/libbcc.pc +++ /dev/null @@ -1,11 +0,0 @@ -prefix=/home/leleliu008/github.com/C/output -exec_prefix=${prefix} -libdir=${prefix}/lib -includedir=${prefix}/include - -Name: libbcc -URL: https://github.com/leleliu008/C/algorithm/bcc -Description: bcc check algorthm -Version: 1.0.0 -Libs: -L${libdir} -lbcc -Cflags: -I${includedir} diff --git a/algorithm/chinese-calendar/libchinese-calendar.pc b/algorithm/chinese-calendar/libchinese-calendar.pc deleted file mode 100644 index f5dc074..0000000 --- a/algorithm/chinese-calendar/libchinese-calendar.pc +++ /dev/null @@ -1,11 +0,0 @@ -prefix=/home/leleliu008/github.com/C/output -exec_prefix=${prefix} -libdir=${prefix}/lib -includedir=${prefix}/include - -Name: libchinese-calendar -URL: https://github.com/leleliu008/C/algorithm/chinese-calendar -Description: chinese festival/jieqi algorthm -Version: 1.0.0 -Libs: -L${libdir} -lchinese-calendar -Cflags: -I${includedir} diff --git a/algorithm/url/liburl.pc b/algorithm/url/liburl.pc deleted file mode 100644 index ba2d590..0000000 --- a/algorithm/url/liburl.pc +++ /dev/null @@ -1,11 +0,0 @@ -prefix=/home/leleliu008/github.com/C/output -exec_prefix=${prefix} -libdir=${prefix}/lib -includedir=${prefix}/include - -Name: liburl -URL: https://github.com/leleliu008/C/algorithm/url -Description: url encode/decode algorthm -Version: 1.0.0 -Libs: -L${libdir} -lurl -Cflags: -I${includedir} diff --git a/library/libgit2/CMakeLists.txt b/library/libgit2/CMakeLists.txt index ad8b7cd..ccd717c 100644 --- a/library/libgit2/CMakeLists.txt +++ b/library/libgit2/CMakeLists.txt @@ -28,6 +28,7 @@ add_executable(git-remote-list git-remote-list.c) add_executable(git-branch-list git-branch-list.c) add_executable(git-config-list git-config-list.c) add_executable(git-tag-name-list git-tag-name-list.c) +add_executable(git-submodule-update git-submodule-update.c) target_link_libraries(git-open LIBGIT2::LIBGIT2) target_link_libraries(git-init LIBGIT2::LIBGIT2) @@ -42,9 +43,10 @@ target_link_libraries(git-remote-list LIBGIT2::LIBGIT2) target_link_libraries(git-branch-list LIBGIT2::LIBGIT2) target_link_libraries(git-config-list LIBGIT2::LIBGIT2) target_link_libraries(git-tag-name-list LIBGIT2::LIBGIT2) +target_link_libraries(git-submodule-update LIBGIT2::LIBGIT2) ######################################################################################## include(GNUInstallDirs) -install(TARGETS git-open git-init git-pull git-sync git-fetch git-clone git-rev-parse git-clone-simple git-status-list git-remote-list git-branch-list git-config-list git-tag-name-list RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) +install(TARGETS git-open git-init git-pull git-sync git-fetch git-clone git-rev-parse git-clone-simple git-status-list git-remote-list git-branch-list git-config-list git-tag-name-list git-submodule-update RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) diff --git a/library/libgit2/git-submodule-update.c b/library/libgit2/git-submodule-update.c new file mode 100644 index 0000000..0a2b466 --- /dev/null +++ b/library/libgit2/git-submodule-update.c @@ -0,0 +1,76 @@ +#include +#include +#include + +#include + +#include + +static int git_submodule_foreach_callback(git_submodule * submodule, const char * name, void * payload) { + unsigned int status = 0; + + int ret = git_submodule_status(&status, (git_repository*)payload, name, GIT_SUBMODULE_IGNORE_UNTRACKED); + + if (ret != GIT_OK) { + const git_error * error = git_error_last(); + fprintf(stderr, "%s\n", error->message); + return ret; + } + + printf("status of (%s):%u\n", name, status); + + return git_submodule_update(submodule, true, NULL); +} + +int do_git_submodule_update_init(const char * gitRepoPath) { + git_libgit2_init(); + + git_repository * gitRepo = NULL; + + // https://libgit2.org/libgit2/#HEAD/group/repository/git_repository_init + int ret = git_repository_init(&gitRepo, gitRepoPath, false); + + if (ret != GIT_OK) { + const git_error * error = git_error_last(); + fprintf(stderr, "%s\n", error->message); + } + + ret = git_submodule_foreach(gitRepo, git_submodule_foreach_callback, gitRepo); + + if (ret != GIT_OK) { + const git_error * error = git_error_last(); + fprintf(stderr, "%s\n", error->message); + } + + git_repository_free(gitRepo); + + git_libgit2_shutdown(); + + return ret; +} + +int main(int argc, char* argv[]) { + if (argc == 1) { + printf("%s [ -C ]\n", argv[0]); + return 0; + } + + const char * workingDirPath = "."; + + for (int i = 1; i < argc; i++) { + if (strcmp(argv[i], "-C") == 0) { + i++; + workingDirPath = argv[i]; + + if (workingDirPath == NULL) { + fprintf(stderr, "-C option should have non-empty value."); + return 1; + } + } else { + fprintf(stderr, "unrecognized option: %s", argv[i]); + return 2; + } + } + + return do_git_submodule_update_init(workingDirPath); +} diff --git a/library/mbedTLS/base64/base64.pc b/library/mbedTLS/base64/base64.pc deleted file mode 100644 index 5bb008a..0000000 --- a/library/mbedTLS/base64/base64.pc +++ /dev/null @@ -1,11 +0,0 @@ -prefix=/home/leleliu008/C/library/mbedTLS/base64/output -exec_prefix=${prefix} -libdir=${prefix}/lib -includedir=${prefix}/include - -Name: base16 -URL: https://github.com/leleliu008/C -Description: base64 algorthm -Version: 1.0.0 -Libs: -L${libdir} -lbase64 -Cflags: -I${includedir} diff --git a/library/mbedTLS/base64/src/include/base64.h b/library/mbedTLS/base64/src/include/base64.h index 6ee106e..a5e1a57 100644 --- a/library/mbedTLS/base64/src/include/base64.h +++ b/library/mbedTLS/base64/src/include/base64.h @@ -15,7 +15,7 @@ * inputBuffer 要编码的字节的起始指针 * inputBufferSizeInBytes 要编码的字节的长度 */ - int base64_encode(char* outputBuffer, size_t outputBufferSizeInBytes, unsigned char* inputBuffer, size_t inputBufferSizeInBytes); + int base64_encode(char * outputBuffer, size_t outputBufferSizeInBytes, unsigned char * inputBuffer, size_t inputBufferSizeInBytes); /* base64解码 * outputBuffer base64解码后的字符串存放缓存区 @@ -23,7 +23,7 @@ * writenToOutputBufferSizeInBytes 真实的向缓存区写入了多少字节 * input base64编码的字符串 */ - int base64_decode(unsigned char* outputBuffer, size_t outputBufferSizeInBytes, size_t* writenToOutputBufferSizeInBytes, char* input); + int base64_decode(unsigned char * outputBuffer, size_t outputBufferSizeInBytes, size_t * writenToOutputBufferSizeInBytes, char * inputBuffer, size_t inputBufferSizeInBytes); #ifdef __cplusplus } diff --git a/library/mbedTLS/base64/src/lib/base64.c b/library/mbedTLS/base64/src/lib/base64.c index 7d32c04..d01de40 100644 --- a/library/mbedTLS/base64/src/lib/base64.c +++ b/library/mbedTLS/base64/src/lib/base64.c @@ -1,37 +1,21 @@ // http://blog.fpliu.com/it/software/mbedTLS/include/base64.h -#include -#include -#include #include + #include "base64_mbedtls.h" size_t get_base64_encode_output_length_in_bytes(size_t inputBufferSizeInBytes) { //看看需要分配多少个字节 + //编码后,长度变为原来的4/3倍 size_t n = inputBufferSizeInBytes / 3U + (((inputBufferSizeInBytes % 3U) == 0) ? 0U : 1U); return (n << 2); } -int base64_encode(char* outputBuffer, size_t outputBufferSizeInBytes, unsigned char* inputBuffer, size_t inputBufferSizeInBytes) { - size_t writtenToOutputBufferCountInBytes = 0U; - if (0 == mbedtls_base64_encode((unsigned char*)outputBuffer, outputBufferSizeInBytes, &writtenToOutputBufferCountInBytes, inputBuffer, inputBufferSizeInBytes)) { - return 0; - } else { - return MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL; - } +int base64_encode(char * outputBuffer, size_t outputBufferSizeInBytes, unsigned char * inputBuffer, size_t inputBufferSizeInBytes) { + size_t writtenSizeInBytes; + return mbedtls_base64_encode((unsigned char*)outputBuffer, outputBufferSizeInBytes, &writtenSizeInBytes, inputBuffer, inputBufferSizeInBytes); } -int base64_decode(unsigned char* outputBuffer, size_t outputBufferSizeInBytes, size_t* writtenToOutputBufferCountInBytes, char* input) { - size_t inputBufferSizeInBytes = strlen(input); - - //base64编码的字符串的长度必须是4的整数倍 - if ((inputBufferSizeInBytes & 3U) != 0U) { - return MBEDTLS_ERR_BASE64_INVALID_CHARACTER; - } - - if (0 == mbedtls_base64_decode(outputBuffer, outputBufferSizeInBytes, writtenToOutputBufferCountInBytes, (unsigned char *)input, inputBufferSizeInBytes)) { - return 0; - } else { - return MBEDTLS_ERR_BASE64_INVALID_CHARACTER; - } +int base64_decode(unsigned char * outputBuffer, size_t outputBufferSizeInBytes, size_t * writtenSizeInBytes, char * inputBuffer, size_t inputBufferSizeInBytes) { + return mbedtls_base64_decode(outputBuffer, outputBufferSizeInBytes, writtenSizeInBytes, (unsigned char *)inputBuffer, inputBufferSizeInBytes); } diff --git a/library/mbedTLS/base64/src/main/base64-decode.c b/library/mbedTLS/base64/src/main/base64-decode.c index b1c0170..0663969 100644 --- a/library/mbedTLS/base64/src/main/base64-decode.c +++ b/library/mbedTLS/base64/src/main/base64-decode.c @@ -1,7 +1,9 @@ #include #include #include + #include + #include #define COLOR_GREEN "\033[0;32m" @@ -49,17 +51,17 @@ static void showHelp() { int main(int argc, char *argv[]) { if (argc < 2) { - char inputBuff[1024]; + char readBuf[1024]; for (;;) { - ssize_t inputSize = read(STDIN_FILENO, inputBuff, 1024); + ssize_t readSize = read(STDIN_FILENO, readBuf, 1024); - if (inputSize < 0) { + if (readSize == -1) { perror(NULL); return 1; } - if (inputSize == 0) { + if (readSize == 0) { if (isatty(STDOUT_FILENO)) { printf("\n"); } @@ -67,20 +69,27 @@ int main(int argc, char *argv[]) { return 0; } - size_t outputBuffSize = (inputSize * 3U) >> 2; + size_t outputBuffSize = (readSize * 3U) >> 2; unsigned char outputBuff[outputBuffSize]; - size_t outputSize; + size_t outputRealSize; - int ret = base64_decode(outputBuff, outputBuffSize, &outputSize, inputBuff); + int ret = base64_decode(outputBuff, outputBuffSize, &outputRealSize, readBuf, readSize); if (ret != 0) { return ret; } - if (write(STDOUT_FILENO, outputBuff, outputSize) < 0) { + ssize_t writtenSize = write(STDOUT_FILENO, outputBuff, outputRealSize); + + if (writtenSize == -1) { perror(NULL); return 1; } + + if ((size_t)writtenSize != outputRealSize) { + fprintf(stderr, "not fully written to stdout.\n"); + return 1; + } } } @@ -93,28 +102,35 @@ int main(int argc, char *argv[]) { } else if (strcmp(argv[1], "--version") == 0) { printf("%s\n", "1.0.0"); } else { - size_t inputSize = strlen(argv[1]); - - if (inputSize == 0U) { - fprintf(stderr, "base64-encode , should be a non-empty string."); + if (argv[1][0] == '\0') { + fprintf(stderr, "base64-decode , should be a non-empty string."); return 1; } + size_t inputSize = strlen(argv[1]); + size_t outputBuffSize = (inputSize * 3U) >> 2; unsigned char outputBuff[outputBuffSize]; - size_t outputSize; + size_t outputRealSize; - int ret = base64_decode(outputBuff, outputBuffSize, &outputSize, argv[1]); + int ret = base64_decode(outputBuff, outputBuffSize, &outputRealSize, argv[1], inputSize); if (ret != 0) { return ret; } - if (write(STDOUT_FILENO, outputBuff, outputSize) < 0) { + ssize_t writtenSize = write(STDOUT_FILENO, outputBuff, outputRealSize); + + if (writtenSize == -1) { perror(NULL); return 1; } + if ((size_t)writtenSize != outputRealSize) { + fprintf(stderr, "not fully written to stdout.\n"); + return 1; + } + if (isatty(STDOUT_FILENO)) { printf("\n"); } diff --git a/library/mbedTLS/base64/src/main/base64-encode.c b/library/mbedTLS/base64/src/main/base64-encode.c index 5bf691a..d07b6b1 100644 --- a/library/mbedTLS/base64/src/main/base64-encode.c +++ b/library/mbedTLS/base64/src/main/base64-encode.c @@ -2,7 +2,9 @@ #include #include #include + #include + #include #define COLOR_GREEN "\033[0;32m" @@ -49,17 +51,17 @@ static void showHelp() { } static int base64_encode_stream(bool wrap) { - unsigned char inputBuff[1023]; + unsigned char readBuf[1023]; for (;;) { - ssize_t inputSize = read(STDIN_FILENO, inputBuff, 1023); + ssize_t readSize = read(STDIN_FILENO, readBuf, 1023); - if (inputSize < 0) { + if (readSize < 0) { perror(NULL); return 1; } - if (inputSize == 0) { + if (readSize == 0) { if (wrap) { printf("\n"); } @@ -67,44 +69,58 @@ static int base64_encode_stream(bool wrap) { return 0; } - size_t outputSize = get_base64_encode_output_length_in_bytes(inputSize) + 1U; - char outputBuff[outputSize]; + size_t outputSize = get_base64_encode_output_length_in_bytes(readSize); + char outputBuff[outputSize + 1U]; - int ret = base64_encode(outputBuff, outputSize, inputBuff, inputSize); + int ret = base64_encode(outputBuff, outputSize + 1U, readBuf, readSize); if (ret != 0) { return ret; } - if (write(STDOUT_FILENO, outputBuff, outputSize - 1U) < 0) { + ssize_t writtenSize = write(STDOUT_FILENO, outputBuff, outputSize); + + if (writtenSize == -1) { perror(NULL); return 1; } + + if ((size_t)writtenSize != outputSize) { + fprintf(stderr, "not fully written to stdout.\n"); + return 1; + } } } static int base64_encode_string(bool wrap, const char * inputString) { - size_t inputSize = strlen(inputString); - - if (inputSize == 0U) { + if (inputString[0] == '\0') { fprintf(stderr, "base64-encode , should be a non-empty string."); return 1; } - size_t outputSize = get_base64_encode_output_length_in_bytes(inputSize) + 1U; - char outputBuff[outputSize]; + size_t inputSize = strlen(inputString); + + size_t outputSize = get_base64_encode_output_length_in_bytes(inputSize); + char outputBuff[outputSize + 1U]; - int ret = base64_encode(outputBuff, outputSize, (unsigned char *)inputString, inputSize); + int ret = base64_encode(outputBuff, outputSize + 1U, (unsigned char *)inputString, inputSize); if (ret != 0) { return ret; } - if (write(STDOUT_FILENO, outputBuff, outputSize - 1U) < 0) { + ssize_t writtenSize = write(STDOUT_FILENO, outputBuff, outputSize); + + if (writtenSize == -1) { perror(NULL); return 1; } + if ((size_t)writtenSize != outputSize) { + fprintf(stderr, "not fully written to stdout.\n"); + return 1; + } + if (wrap) { printf("\n"); } diff --git a/library/openssl/SHA-256/src/include/sha256sum.h b/library/openssl/SHA-256/src/include/sha256sum.h index 13549a7..034c036 100644 --- a/library/openssl/SHA-256/src/include/sha256sum.h +++ b/library/openssl/SHA-256/src/include/sha256sum.h @@ -7,9 +7,10 @@ #ifdef __cplusplus extern "C" { #endif - char* sha256sum_of_bytes (unsigned char * inputBuffer, size_t inputBufferSizeInBytes); - char* sha256sum_of_string(const char * str); - char* sha256sum_of_file (FILE * file); + int sha256sum_of_bytes (char outputBuffer[65], unsigned char * inputBuffer, size_t inputBufferSizeInBytes); + int sha256sum_of_string(char outputBuffer[65], const char * str); + int sha256sum_of_file (char outputBuffer[65], const char * filepath); + int sha256sum_of_stream(char outputBuffer[65], FILE * file); #ifdef __cplusplus } #endif diff --git a/library/openssl/SHA-256/src/lib/sha256sum.c b/library/openssl/SHA-256/src/lib/sha256sum.c index 3d8c78c..6f72aa8 100644 --- a/library/openssl/SHA-256/src/lib/sha256sum.c +++ b/library/openssl/SHA-256/src/lib/sha256sum.c @@ -1,59 +1,140 @@ +#include #include #include + #include -#include -char* sha256sum_of_bytes(unsigned char * inputBuffer, size_t inputBufferSizeInBytes) { +#include "sha256sum.h" + +static inline void tohex(char buf[65], const unsigned char * sha256Bytes) { + const char * const table = "0123456789abcdef"; + + for (size_t i = 0U; i < SHA256_DIGEST_LENGTH; i++) { + size_t j = i << 1; + buf[j] = table[sha256Bytes[i] >> 4]; + buf[j + 1U] = table[sha256Bytes[i] & 0x0F]; + } +} + +int sha256sum_of_bytes(char outputBuffer[65], unsigned char * inputBuffer, size_t inputBufferSizeInBytes) { + if (outputBuffer == NULL) { + errno = EINVAL; + return -1; + } + + if (inputBuffer == NULL) { + errno = EINVAL; + return -1; + } + + if (inputBufferSizeInBytes == 0U) { + errno = EINVAL; + return -1; + } + unsigned char sha256Bytes[SHA256_DIGEST_LENGTH] = {0}; SHA256_CTX ctx; SHA256_Init(&ctx); SHA256_Update(&ctx, inputBuffer, inputBufferSizeInBytes); SHA256_Final(sha256Bytes, &ctx); - - char * sha256sum = (char*)calloc(SHA256_DIGEST_LENGTH * 2 + 1, sizeof(char)); - for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) { - sprintf(&sha256sum[i * 2], "%02x", (unsigned char)sha256Bytes[i]); - } - return sha256sum; + tohex(outputBuffer, sha256Bytes); + + return 0; } -char* sha256sum_of_string(const char * str) { +int sha256sum_of_string(char outputBuffer[65], const char * str) { + if (str == NULL) { + errno = EINVAL; + return -1; + } + + if (str[0] == '\0') { + errno = EINVAL; + return -1; + } + unsigned char sha256Bytes[SHA256_DIGEST_LENGTH] = {0}; SHA256_CTX ctx; SHA256_Init(&ctx); SHA256_Update(&ctx, str, strlen(str)); SHA256_Final(sha256Bytes, &ctx); - - char * sha256sum = (char*)calloc(SHA256_DIGEST_LENGTH * 2 + 1, sizeof(char)); - for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) { - sprintf(&sha256sum[i * 2], "%02x", (unsigned char)sha256Bytes[i]); - } - return sha256sum; + tohex(outputBuffer, sha256Bytes); + + return 0; } -char* sha256sum_of_file(FILE * file) { +int sha256sum_of_stream(char outputBuffer[65], FILE * file) { + if (outputBuffer == NULL) { + errno = EINVAL; + return -1; + } + + if (file == NULL) { + errno = EINVAL; + return -1; + } + unsigned char sha256Bytes[SHA256_DIGEST_LENGTH] = {0}; SHA256_CTX ctx; SHA256_Init(&ctx); - unsigned char buffer[1024] = {0}; - size_t size = 0; + unsigned char buffer[1024]; + + for (;;) { + size_t size = fread(buffer, 1, 1024, file); + + if (ferror(file)) { + errno = EIO; + return -1; + } - while ((size = fread(buffer, 1, 1024, file)) != 0) { - SHA256_Update(&ctx, buffer, size); + if (size > 0U) { + SHA256_Update(&ctx, buffer, size); + } + + if (feof(file)) { + break; + } } SHA256_Final(sha256Bytes, &ctx); - - char * sha256sum = (char*)calloc(SHA256_DIGEST_LENGTH * 2 + 1, sizeof(char)); - for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) { - sprintf(&sha256sum[i * 2], "%02x", (unsigned char)sha256Bytes[i]); + + tohex(outputBuffer, sha256Bytes); + + return 0; +} + +int sha256sum_of_file(char outputBuffer[65], const char * filepath) { + if (outputBuffer == NULL) { + errno = EINVAL; + return -1; + } + + if (filepath == NULL) { + errno = EINVAL; + return -1; + } + + if (filepath[0] == '\0') { + errno = EINVAL; + return -1; } - return sha256sum; + FILE * file = fopen(filepath, "rb"); + + if (file == NULL) { + errno = EINVAL; + return -1; + } + + int ret = sha256sum_of_stream(outputBuffer, file); + + fclose(file); + + return ret; } diff --git a/library/openssl/SHA-256/src/main/sha256sum.c b/library/openssl/SHA-256/src/main/sha256sum.c index fc97615..4a4424c 100644 --- a/library/openssl/SHA-256/src/main/sha256sum.c +++ b/library/openssl/SHA-256/src/main/sha256sum.c @@ -1,10 +1,10 @@ #include -#include #include + #include static void show_help(const char * programName) { - printf("USAGE: %s [--string STR] [--file FILE]\n", programName); + printf("USAGE: %s [--string ] [--file ]\n", programName); } int main(int argc, char* argv[]) { @@ -18,37 +18,48 @@ int main(int argc, char* argv[]) { } else if (strcmp(argv[1], "--help") == 0) { show_help(argv[0]); } else if (strcmp(argv[1], "--string") == 0) { - char * sha256sum = sha256sum_of_string(argv[2]); - if (sha256sum == NULL) { - perror("sha256sum_of_string"); - return 1; - } else { + if (argv[2] == NULL) { + fprintf(stderr, "USAGE: %s --string , is unspecifed.\n", argv[0]); + return 0; + } + + if (argv[2][0] == '\0') { + fprintf(stderr, "USAGE: %s --string , should be a non-empty string.\n", argv[0]); + return 0; + } + + char sha256sum[65] = {0}; + + int ret = sha256sum_of_string(sha256sum, argv[2]); + + if (ret == 0) { printf("%s\n", sha256sum); return 0; + } else { + perror(NULL); + return 1; } } else if (strcmp(argv[1], "--file") == 0) { if (argv[2] == NULL) { - printf("filepath argument is not given.\n"); - return 1; + fprintf(stderr, "USAGE: %s --file , is unspecifed.\n", argv[0]); + return 0; } - FILE * file = fopen(argv[2], "rb"); - - if (file == NULL) { - perror(argv[2]); - return 1; + if (argv[2][0] == '\0') { + fprintf(stderr, "USAGE: %s --file , should be a non-empty string.\n", argv[0]); + return 0; } - char * sha256sum = sha256sum_of_file(file); + char sha256sum[65] = {0}; - fclose(file); + int ret = sha256sum_of_file(sha256sum, argv[2]); - if (sha256sum == NULL) { - perror("sha256sum_of_file"); - return 1; - } else { + if (ret == 0) { printf("%s %s\n", sha256sum, argv[2]); return 0; + } else { + perror(argv[2]); + return 1; } } else { printf("unrecognized argument: %s\n", argv[1]); diff --git a/standard/posix/unistd.h/chdir.c b/standard/posix/unistd.h/chdir.c index 285625a..7aff31a 100644 --- a/standard/posix/unistd.h/chdir.c +++ b/standard/posix/unistd.h/chdir.c @@ -1,6 +1,8 @@ #include #include + #include +#include int main(int argc, char *argv[]) { if (argc < 2) { @@ -8,24 +10,24 @@ int main(int argc, char *argv[]) { return 1; } - const char * cwd = NULL; + char buff[PATH_MAX]; + const char * cwd = getcwd(buff, PATH_MAX); - for (int length = 100; cwd == NULL; length += 100) { - char buff[length]; - memset(buff, 0, length); - cwd = getcwd(buff, length); + if (cwd == NULL) { + perror(NULL); + return 1; } printf("current working dir is %s\n", cwd); printf("change working dir to %s\n", argv[1]); if (chdir(argv[1]) == 0) { - const char * cwd = NULL; + char buff[PATH_MAX]; + const char * cwd = getcwd(buff, PATH_MAX); - for (int length = 100; cwd == NULL; length += 100) { - char buff[length]; - memset(buff, 0, length); - cwd = getcwd(buff, length); + if (cwd == NULL) { + perror(NULL); + return 1; } printf("current working dir is %s\n", cwd); diff --git a/standard/posix/unistd.h/getcwd.c b/standard/posix/unistd.h/getcwd.c index c1ad046..375ca9e 100644 --- a/standard/posix/unistd.h/getcwd.c +++ b/standard/posix/unistd.h/getcwd.c @@ -1,17 +1,18 @@ #include -#include + #include +#include int main(int argc, char *argv[]) { - const char * cwd = NULL; - - for (int length = 100; cwd == NULL; length += 100) { - char buff[length]; - memset(buff, 0, length); - cwd = getcwd(buff, length); - } + char buff[PATH_MAX]; - printf("current working dir is %s\n", cwd); + const char * cwd = getcwd(buff, PATH_MAX); - return 0; + if (cwd == NULL) { + perror(NULL); + return 1; + } else { + printf("current working dir is %s\n", cwd); + return 0; + } } diff --git a/utils/sysinfo/README.md b/utils/sysinfo/README.md index 2b10c23..71c9f26 100644 --- a/utils/sysinfo/README.md +++ b/utils/sysinfo/README.md @@ -1,5 +1,5 @@ # sysinfo -a C library and command-line tool to get your system's information. +A C library and command-line tool to get your currently running system's information. ## sysinfo command usage |command|values|