From 51a09ebdccdfe7634e07e4e83dbc9e3354e7e903 Mon Sep 17 00:00:00 2001 From: lifeng68 Date: Mon, 20 Jul 2020 20:05:14 +0800 Subject: [PATCH] clean code: remove unused sha256 functions Signed-off-by: lifeng68 --- src/utils/sha256/sha256.c | 205 -------------------------------------- src/utils/sha256/sha256.h | 16 --- 2 files changed, 221 deletions(-) diff --git a/src/utils/sha256/sha256.c b/src/utils/sha256/sha256.c index f3ddfd6..31dcc9b 100644 --- a/src/utils/sha256/sha256.c +++ b/src/utils/sha256/sha256.c @@ -59,210 +59,6 @@ static bool stream_check_error(void *stream, bool isgzip) return false; } -int stream_read(void *stream, int fd, bool isgzip) -{ - size_t n = 0; - char *buffer = NULL; - - buffer = util_common_calloc_s(BLKSIZE + 72); - if (buffer == NULL) { - ERROR("Malloc BLKSIZE memory error"); - return -1; - } - - /* Read block. Take care for partial reads. */ - while (1) { - if (isgzip) { - n = (size_t)gzread((gzFile)stream, buffer, BLKSIZE); - } else { - n = fread(buffer, 1, BLKSIZE, (FILE *)stream); - } - - if (n == BLKSIZE) { - if (write(fd, buffer, BLKSIZE) == -1) { - ERROR("Write pipe failed: %s", strerror(errno)); - goto free_out; - } - n = 0; - continue; - } - - if (n == 0) { - if (stream_check_error(stream, isgzip)) { - goto free_out; - } - goto end_send; - } - - if (stream_check_eof(stream, isgzip)) { - goto end_send; - } - } - -end_send: - /* Process any remaining bytes. */ - if (n > 0) { - if (write(fd, buffer, n) == -1) { - ERROR("Write pipe_for_write failed: %s", strerror(errno)); - goto free_out; - } - } - free(buffer); - return 0; - -free_out: - free(buffer); - return -1; -} - -static int sha256sum_calculate_parent_handle(pid_t pid, int pipe_for_read[], int pipe_for_write[], bool isfile, - bool isgzip, void *stream, char *buffer_out, size_t len) -{ - ssize_t size_pipe_read; - - close(pipe_for_read[1]); - pipe_for_read[1] = -1; - close(pipe_for_write[0]); - pipe_for_write[0] = -1; - - if (isfile) { - int ret = stream_read((gzFile)stream, pipe_for_write[1], isgzip); - if (ret != 0) { - close(pipe_for_read[0]); - pipe_for_read[0] = -1; - close(pipe_for_write[1]); - pipe_for_write[1] = -1; - ERROR("Read buffer error"); - return -1; - } - } else if (write(pipe_for_write[1], (char *)stream, len) == -1) { - close(pipe_for_read[0]); - pipe_for_read[0] = -1; - close(pipe_for_write[1]); - pipe_for_write[1] = -1; - ERROR("Write pipe_for_write failed: %s", strerror(errno)); - return -1; - } - - close(pipe_for_write[1]); - pipe_for_write[1] = -1; - if (wait_for_pid(pid)) { - char buffer_errmsg[BUFSIZ] = { 0 }; - size_t size_read = (size_t)read(pipe_for_read[0], buffer_errmsg, BUFSIZ); - if (size_read != 0) { - ERROR("Sha256sum run error: %s", buffer_errmsg); - } - close(pipe_for_read[0]); - pipe_for_read[0] = -1; - return -1; - } - - size_pipe_read = read(pipe_for_read[0], buffer_out, SHA256_SIZE); - close(pipe_for_read[0]); - pipe_for_read[0] = -1; - if (size_pipe_read <= 0) { - ERROR("Read sha256 buffer failed"); - return -1; - } - buffer_out[SHA256_SIZE] = '\0'; - return 0; -} - -int sha256sum_calculate(void *stream, char *buffer_out, size_t len, bool isfile, bool isgzip) -{ - int ret = 0; - pid_t pid; - int pipe_for_read[2] = { -1, -1 }; - int pipe_for_write[2] = { -1, -1 }; - - if (!stream || !buffer_out) { - ERROR("Param Error"); - ret = -1; - goto out; - } - if (pipe2(pipe_for_read, O_CLOEXEC) != 0) { - ERROR("Failed to create pipe"); - ret = -1; - goto out; - } - - if (pipe2(pipe_for_write, O_CLOEXEC) != 0) { - ERROR("Failed to create pipe"); - ret = -1; - goto out; - } - - pid = fork(); - if (pid == (pid_t) -1) { - ERROR("Failed to fork()"); - ret = -1; - close(pipe_for_read[0]); - close(pipe_for_read[1]); - close(pipe_for_write[0]); - close(pipe_for_write[1]); - goto out; - } - - if (pid == (pid_t)0) { - // child process, dup2 pipe_for_read[1] to stdout, - // pipe_for_write[1] to stdin - close(pipe_for_read[0]); - close(pipe_for_write[1]); - if (dup2(pipe_for_read[1], 1) < 0) { - fprintf(stdout, "Dup fd error: %s", strerror(errno)); - exit(EXIT_FAILURE); - } - if (dup2(pipe_for_write[0], 0)) { - fprintf(stdout, "Dup fd error: %s", strerror(errno)); - exit(EXIT_FAILURE); - } - - if (util_check_inherited(true, -1)) { - fprintf(stdout, "Failed to close fds."); - exit(EXIT_FAILURE); - } - execlp("sha256sum", "sha256sum", NULL); - - fprintf(stdout, "Failed to exec sha256sum program"); - exit(EXIT_FAILURE); - } - - ret = sha256sum_calculate_parent_handle(pid, pipe_for_read, pipe_for_write, isfile, isgzip, stream, buffer_out, - len); - -out: - return ret; -} - -char *sha256_digest(void *stream, bool isgzip) -{ - int ret = 0; - int cnt; - char buffer_out[SHA256_SIZE + 1]; - char *digest = NULL; - - if (stream == NULL) { - return NULL; - } - - ret = sha256sum_calculate(stream, buffer_out, 0, true, isgzip); - if (ret != 0) { - return NULL; - } - - digest = (char *)util_common_calloc_s(SHA256_SIZE + 1); - if (digest == NULL) { - return NULL; - } - digest[SHA256_SIZE] = 0; - - /* translate from binary to hex string */ - for (cnt = 0; cnt < SHA256_SIZE; ++cnt) { - digest[cnt] = buffer_out[cnt]; - } - return digest; -} - char *sha256_digest_str(const char *val) { SHA256_CTX ctx; @@ -521,4 +317,3 @@ char *without_sha256_prefix(char *digest) return digest + strlen(SHA256_PREFIX); } - diff --git a/src/utils/sha256/sha256.h b/src/utils/sha256/sha256.h index 9347ace..8a40a4f 100644 --- a/src/utils/sha256/sha256.h +++ b/src/utils/sha256/sha256.h @@ -24,22 +24,6 @@ extern "C" { #endif -enum { SHA224_SIZE = 224 / 8 }; -enum { SHA224_ALIGN = 4 }; -enum { SHA256_SIZE = 256 / 4 }; -enum { SHA256_ALIGN = 4 }; - -/* read file stream buffer */ -extern int fstream_read(FILE *stream, int fd); - -/* read gzfile stream buffer. */ -extern int gzstream_read(gzFile gzstream, int fd); - -extern int sha256sum_calculate(void *stream, char *buffer_out, size_t len, bool isfile, bool isgzip); -/* Compute SHA256 (SHA224) message digest for bytes read from STREAM. - The result is a 64 characters string without prefix "sha256:" */ -char *sha256_digest(void *stream, bool isgzip); - char *sha256_digest_file(const char *filename, bool isgzip); char *sha256_digest_str(const char *val); -- GitLab