From 44fef04cbc5f61b3016e568d481fa96dcb145c8f Mon Sep 17 00:00:00 2001 From: LoRd_MuldeR Date: Wed, 14 Oct 2020 21:55:39 +0200 Subject: [PATCH] Some code clean-up. --- Makefile | 4 +- frontend/src/main.c | 139 +++++++++++++++++++++++-------------------- frontend/src/utils.c | 3 +- 3 files changed, 80 insertions(+), 66 deletions(-) diff --git a/Makefile b/Makefile index 244b58a..7d28f90 100644 --- a/Makefile +++ b/Makefile @@ -16,12 +16,12 @@ ifeq ($(DEBUG),1) CFLAGS += -Og -g undefine LDFLGS else - CFLAGS += -O3 -static -DNDEBUG + CFLAGS += -O3 -DNDEBUG LDFLGS += -static -s endif ifeq ($(OS),Windows_NT) - CFLAGS += -municode -mconsole + LDFLGS += -municode -mconsole endif # --------------------------------------------------------------------------- diff --git a/frontend/src/main.c b/frontend/src/main.c index 2ff035a..542b465 100644 --- a/frontend/src/main.c +++ b/frontend/src/main.c @@ -14,6 +14,7 @@ #include #include +#define BUFF_SIZE 4096U static volatile int g_interrupted = 0; static char* read_passphrase(const CHR* const file_name) @@ -24,16 +25,16 @@ static char* read_passphrase(const CHR* const file_name) { return NULL; } - FILE *const fin = FOPEN(file_name, T("rb")); - if (!fin) + FILE *const file = FOPEN(file_name, T("rb")); + if (!file) { return NULL; } do { - if (!fgets(buffer, (int)buff_size, fin)) + if (!fgets(buffer, (int)buff_size, file)) { - fclose(fin); + fclose(file); free(buffer); return NULL; } @@ -44,7 +45,7 @@ static char* read_passphrase(const CHR* const file_name) } } while (!buffer[0U]); - fclose(fin); + fclose(file); return buffer; } @@ -67,46 +68,46 @@ static int weak_passphrase(const char *str) return !strong; } -static int open_files(FILE** const fin, FILE** const fout, const CHR* const input, const CHR* const output) +static int open_files(FILE **const file_in, FILE **const file_out, const CHR* const input_path, const CHR* const output_path) { - *fin = FOPEN(input, T("rb")); - if (!(*fin)) + *file_in = FOPEN(input_path, T("rb")); + if (!(*file_in)) { FPUTS(T("Error: Failed to open input file for reading!\n\n"), stderr); return 1; } - *fout = FOPEN(output, T("wb")); - if (!(*fout)) + *file_out = FOPEN(output_path, T("wb")); + if (!(*file_out)) { FPUTS(T("Error: Failed to open output file for writing!\n\n"), stderr); - fclose(*fin); + fclose(*file_out); return 1; } return 0; } -static int encrypt(const char* const passphrase, const CHR* const input, const CHR* const output) +static int encrypt(const char* const passphrase, const CHR* const input_path, const CHR* const output_path) { mcrypt_t ctx = MCRYPT_NULL; - FILE *fin = NULL, *fout = NULL; + FILE * file_in = NULL, * file_out = NULL; int result = 1; - if (open_files(&fin, &fout, input, output) != 0) + if (open_files(&file_in, &file_out, input_path, output_path) != 0) { goto clean_up;; } - const uint64_t file_size = get_file_size(fin); + const uint64_t file_size = get_file_size(file_in); if (file_size == UINT64_MAX) { FPUTS(T("I/O error: Failed to determine size of input file!\n\n"), stderr); goto clean_up; } - else if (file_size < 1LL) + else if (file_size < 1U) { - FPUTS(T("Error: Input file is empty!\n\n"), stderr); + FPUTS(T("Error: Input file is empty or an unsupported type!\n\n"), stderr); goto clean_up; } @@ -117,7 +118,7 @@ static int encrypt(const char* const passphrase, const CHR* const input, const C goto clean_up; } - if (fwrite(&seed, sizeof(uint64_t), 1U, fout) < 1U) + if (fwrite(&seed, sizeof(uint64_t), 1U, file_out) < 1U) { FPUTS(T("I/O error: Failed to write seed value!\n\n"), stderr); goto clean_up; @@ -134,16 +135,13 @@ static int encrypt(const char* const passphrase, const CHR* const input, const C clock_t clk_now, clk_update = clock(); uint64_t crc_actual = CRC_INITIALIZER, bytes_read = sizeof(uint64_t); - uint8_t buffer[1024U]; + uint8_t buffer[BUFF_SIZE]; - while ((!feof(fin)) && (bytes_read < file_size)) + while (bytes_read < file_size) { - const size_t count = fread(buffer, sizeof(uint8_t), 1024U, fin); - if (ferror(fin)) - { - FPUTS(T("\n\nI/O error: Failed to read input data!\n\n"), stderr); - goto clean_up; - } + const uint64_t bytes_remaining = file_size - bytes_read; + const size_t request_len = (bytes_remaining < BUFF_SIZE) ? ((size_t)bytes_remaining) : BUFF_SIZE; + const size_t count = fread(buffer, sizeof(uint8_t), request_len, file_in); if (count > 0U) { crc_actual = crc64_update(crc_actual, buffer, count); @@ -153,12 +151,16 @@ static int encrypt(const char* const passphrase, const CHR* const input, const C FPUTS(T("\n\nMCrypt error: Failed to encrypt data!\n\n"), stderr); goto clean_up; } - if (fwrite(buffer, sizeof(uint8_t), count, fout) < count) + if (fwrite(buffer, sizeof(uint8_t), count, file_out) < count) { FPUTS(T("\n\nI/O error: Failed to write encrypted data!\n\n"), stderr); goto clean_up; } } + if (count < request_len) + { + break; /*EOF*/ + } if ((clk_now = clock()) - clk_update > CLOCKS_PER_SEC) { FPRINTF(stderr, T("\b\b\b\b\b\b%5.1f%%"), (bytes_read / ((double)file_size)) * 100.0); @@ -172,7 +174,11 @@ static int encrypt(const char* const passphrase, const CHR* const input, const C } } - crc_actual = crc64_finish(crc_actual); + if (ferror(file_in)) + { + FPUTS(T("\n\nI/O error: Failed to read input data!\n\n"), stderr); + goto clean_up; + } if (bytes_read < file_size) { @@ -180,9 +186,10 @@ static int encrypt(const char* const passphrase, const CHR* const input, const C goto clean_up; } + crc_actual = crc64_finish(crc_actual); FPRINTF(stderr, T("\b\b\b\b\b\b%5.1f%%\n\n"), 100.0); - if (fwrite(&crc_actual, sizeof(uint64_t), 1U, fout) < 1U) + if (fwrite(&crc_actual, sizeof(uint64_t), 1U, file_out) < 1U) { FPUTS(T("I/O error: Failed to write CRC checksum!\n\n"), stderr); goto clean_up; @@ -200,44 +207,44 @@ clean_up: mcrypt_free(ctx); } - if (fout) + if (file_out) { - fclose(fout); + fclose(file_out); } - if (fin) + if (file_in) { - fclose(fin); + fclose(file_in); } return result; } -static int decrypt(const char* const passphrase, const CHR* const input, const CHR* const output) +static int decrypt(const char* const passphrase, const CHR* const input_path, const CHR* const output_path) { mcrypt_t ctx = MCRYPT_NULL; - FILE *fin = NULL, *fout = NULL; + FILE *file_in = NULL, *file_out = NULL; int result = 1; - if (open_files(&fin, &fout, input, output) != 0) + if (open_files(&file_in, &file_out, input_path, output_path) != 0) { goto clean_up; } - const uint64_t file_size = get_file_size(fin); + const uint64_t file_size = get_file_size(file_in); if (file_size == UINT64_MAX) { FPUTS(T("I/O error: Failed to determine size of input file!\n\n"), stderr); goto clean_up; } - else if (file_size < 16LL) + else if (file_size < 16U) { FPUTS(T("Error: Input file is too small! Truncated?\n\n"), stderr); goto clean_up; } uint64_t seed; - if (fread(&seed, sizeof(uint64_t), 1U, fin) < 1U) + if (fread(&seed, sizeof(uint64_t), 1U, file_in) < 1U) { FPUTS(T("I/O error: Failed to read seed value!\n\n"), stderr); goto clean_up; @@ -254,19 +261,14 @@ static int decrypt(const char* const passphrase, const CHR* const input, const C clock_t clk_now, clk_update = clock(); uint64_t crc_actual = CRC_INITIALIZER, bytes_read = sizeof(uint64_t); - uint8_t buffer[1024U]; + uint8_t buffer[BUFF_SIZE]; const uint64_t read_limit = file_size - sizeof(uint64_t); - while ((!feof(fin)) && (bytes_read < read_limit)) + while (bytes_read < read_limit) { const uint64_t bytes_remaining = read_limit - bytes_read; - const size_t read_len = (bytes_remaining < 1024U) ? ((size_t)bytes_remaining) : 1024U; - const size_t count = fread(buffer, sizeof(uint8_t), read_len, fin); - if (ferror(fin)) - { - FPUTS(T("\n\nI/O error: Failed to read encrypted data!\n\n"), stderr); - goto clean_up; - } + const size_t request_len = (bytes_remaining < BUFF_SIZE) ? ((size_t)bytes_remaining) : BUFF_SIZE; + const size_t count = fread(buffer, sizeof(uint8_t), request_len, file_in); if (count > 0U) { bytes_read += count; @@ -276,12 +278,16 @@ static int decrypt(const char* const passphrase, const CHR* const input, const C goto clean_up; } crc_actual = crc64_update(crc_actual, buffer, count); - if (fwrite(buffer, sizeof(uint8_t), count, fout) < count) + if (fwrite(buffer, sizeof(uint8_t), count, file_out) < count) { FPUTS(T("failed!\n\nI/O error: Failed to write decrypted data!\n\n"), stderr); goto clean_up; } } + if (count < request_len) + { + break; /*EOF*/ + } if ((clk_now = clock()) - clk_update > CLOCKS_PER_SEC) { FPRINTF(stderr, T("\b\b\b\b\b\b%5.1f%%"), (bytes_read / ((double)read_limit)) * 100.0); @@ -295,7 +301,11 @@ static int decrypt(const char* const passphrase, const CHR* const input, const C } } - crc_actual = crc64_finish(crc_actual); + if (ferror(file_in)) + { + FPUTS(T("\n\nI/O error: Failed to read input data!\n\n"), stderr); + goto clean_up; + } if (bytes_read < read_limit) { @@ -303,10 +313,11 @@ static int decrypt(const char* const passphrase, const CHR* const input, const C goto clean_up; } + crc_actual = crc64_finish(crc_actual); FPRINTF(stderr, T("\b\b\b\b\b\b%5.1f%%\n\n"), 100.0); uint64_t crc_expected; - if (fread(&crc_expected, sizeof(uint64_t), 1U, fin) < 1U) + if (fread(&crc_expected, sizeof(uint64_t), 1U, file_in) < 1U) { FPUTS(T("I/O error: Failed to read CRC checksum!\n\n"), stderr); goto clean_up; @@ -331,14 +342,14 @@ clean_up: mcrypt_free(ctx); } - if (fout) + if (file_out) { - fclose(fout); + fclose(file_out); } - if (fin) + if (file_in) { - fclose(fin); + fclose(file_in); } return result; @@ -439,7 +450,7 @@ int MAIN(int argc, CHR* argv[]) FPRINTF(stderr, T("MCrypt Utility (%") T(PRIstr) T("-%") T(PRIstr) T("), by LoRd_MuldeR \n"), OS_TYPE, CPU_ARCH); FPRINTF(stderr, T("Using libMCrypt v%") T(PRIstr) T(" [%") T(PRIstr) T("]\n\n"), LIBMCRYPT_VERSION, LIBMCRYPT_BUILDNO); - if ((argc > 1) && (!STRICMP(argv[1U], T("--self-test")))) + if ((argc > 1) && ((!STRICMP(argv[1U], T("-t"))) || (!STRICMP(argv[1U], T("--self-test"))))) { return self_test(); /*only self-test!*/ } @@ -497,11 +508,11 @@ int MAIN(int argc, CHR* argv[]) const clock_t clk_start = clock(); int result = -1; - if (!STRICMP(command, T("--encrypt"))) + if ((!STRICMP(command, T("-e"))) || (!STRICMP(command, T("--encrypt")))) { result = encrypt(passphrase_buffer, input_file, output_file); } - else if (!STRICMP(command, T("--decrypt"))) + else if ((!STRICMP(command, T("-d"))) || (!STRICMP(command, T("--decrypt")))) { result = decrypt(passphrase_buffer, input_file, output_file); } @@ -511,11 +522,13 @@ int MAIN(int argc, CHR* argv[]) goto exiting; } - FPUTS(T("--------\n\n"), stderr); - fflush(stderr); - - const clock_t clk_end = clock(); - FPRINTF(stderr, T("Operation completed after %.1f seconds.\n\n"), (clk_end - clk_start) / ((double)CLOCKS_PER_SEC)); + if (!g_interrupted) + { + FPUTS(T("--------\n\n"), stderr); + fflush(stderr); + const clock_t clk_end = clock(); + FPRINTF(stderr, T("Operation completed after %.1f seconds.\n\n"), (clk_end - clk_start) / ((double)CLOCKS_PER_SEC)); + } exiting: diff --git a/frontend/src/utils.c b/frontend/src/utils.c index dc40519..8643c2e 100644 --- a/frontend/src/utils.c +++ b/frontend/src/utils.c @@ -84,7 +84,8 @@ uint64_t get_file_size(FILE* const file) uint16_t file_type = stat.st_mode & S_IFMT; if ((file_type != S_IFDIR) && (file_type != S_IFIFO)) { - return (stat.st_size >= 0LL) ? ((uint64_t)stat.st_size) : 0U; + const int64_t ssize = stat.st_size; + return (ssize >= 0) ? ((uint64_t)ssize) : 0U; } return 0U; }