From 698f4fa3c984013f509f0db6fc19c0aa04906961 Mon Sep 17 00:00:00 2001 From: LoRd_MuldeR Date: Tue, 13 Oct 2020 19:50:29 +0200 Subject: [PATCH] Code clean-up. --- frontend/src/main.c | 15 ++++++++++----- frontend/src/platform.h | 9 ++++++++- frontend/src/utils.c | 14 +------------- frontend/src/utils.h | 5 +---- 4 files changed, 20 insertions(+), 23 deletions(-) diff --git a/frontend/src/main.c b/frontend/src/main.c index 11e3008..00d97f3 100644 --- a/frontend/src/main.c +++ b/frontend/src/main.c @@ -101,7 +101,7 @@ static int encrypt(const char* const passphrase, const CHR* const input, const C goto clean_up; } - FPRINTF(stderr, T("Encrypting, please be patient... %5.1f%%"), 0.0); + FPRINTF(stderr, T("Encrypting file contents, please be patient... %5.1f%%"), 0.0); clock_t clk_now, clk_update = clock(); uint64_t crc_actual = CRC_INITIALIZER, bytes_read = sizeof(uint64_t); @@ -216,7 +216,7 @@ static int decrypt(const char* const passphrase, const CHR* const input, const C goto clean_up; } - FPRINTF(stderr, T("Decrypting, please be patient... %5.1f%%"), 0.0); + FPRINTF(stderr, T("Decrypting file contents, please be patient... %5.1f%%"), 0.0); clock_t clk_now, clk_update = clock(); uint64_t crc_actual = CRC_INITIALIZER, bytes_read = sizeof(uint64_t); @@ -309,7 +309,7 @@ int MAIN(int argc, CHR* argv[]) { init_terminal(); - FPRINTF(stderr, T("MCrypt Utility (%") T(PRIstr) T("-%") T(PRIstr) T("), by LoRd_MuldeR \n"), OS_TYPE, CPU_ARCH); + 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); const int help_requested = (argc > 1) && ((!STRICMP(argv[1U], T("/?"))) || (!STRICMP(argv[1U], T("--help"))) || (!STRICMP(argv[1U], T("--version")))); @@ -326,7 +326,7 @@ int MAIN(int argc, CHR* argv[]) FPUTS(T("Usage:\n"), stderr); FPRINTF(stderr, T(" %") T(PRISTR) T(" --encrypt \n"), argv[0U]); FPRINTF(stderr, T(" %") T(PRISTR) T(" --decrypt \n\n"), argv[0U]); - return 1; + return help_requested ? 0 : 1; } const CHR *const command = argv[1U], *const passphrase = argv[2U], *const input_file = argv[3U], *const output_file = argv[4U]; @@ -385,6 +385,11 @@ int MAIN(int argc, CHR* argv[]) exiting: - free_utf8(passphrase_utf8); + if (passphrase_utf8) + { + mcrypt_bzero(passphrase_utf8, strlen(passphrase_utf8)); + free(passphrase_utf8); + } + mcrypt_bzero((CHR*)passphrase, STRLEN(passphrase) * sizeof(CHR)); } diff --git a/frontend/src/platform.h b/frontend/src/platform.h index 7485523..8014945 100644 --- a/frontend/src/platform.h +++ b/frontend/src/platform.h @@ -6,6 +6,13 @@ #ifndef INC_PLATFORM_H #define INC_PLATFORM_H +#include +#include +#include +#ifdef _WIN32 +#include +#endif + #ifdef _WIN32 #define OS_TYPE "Win" #else @@ -50,7 +57,7 @@ #define STRICMP(X,Y) _wcsicmp((X),(Y)) #define FPUTS(X,Y) fputws((X),(Y)) #define FPRINTF(X,Y,...) fwprintf((X),(Y),__VA_ARGS__) -#define FOPEN(X,Y) _wfopen((X),(Y)) +#define FOPEN(X,Y) _wfsopen((X),(Y),_SH_SECURE) #define FILENO(X) _fileno((X)) #define FSTAT64(X,Y) _fstati64((X),(Y)) #define STAT64_T struct _stati64 diff --git a/frontend/src/utils.c b/frontend/src/utils.c index 6dc6a68..3602102 100644 --- a/frontend/src/utils.c +++ b/frontend/src/utils.c @@ -12,7 +12,6 @@ #include "utils.h" #include -#include #include #include @@ -52,18 +51,7 @@ char* CHR_to_utf8(const CHR*const input) free(buffer); return NULL; #else - return input; /*nothing to do*/ -#endif -} - -void free_utf8(char *const str_utf8) -{ -#ifdef _WIN32 - if (str_utf8) - { - mcrypt_bzero(str_utf8, strlen(str_utf8)); - free(str_utf8); - } + return strdup(input); #endif } diff --git a/frontend/src/utils.h b/frontend/src/utils.h index ed83052..1d3b764 100644 --- a/frontend/src/utils.h +++ b/frontend/src/utils.h @@ -6,13 +6,10 @@ #ifndef INC_UTILS_H #define INC_UTILS_H -#include -#include -#include #include "platform.h" +#include char* CHR_to_utf8(const CHR *const input); -void free_utf8(char* const str_utf8); uint64_t get_file_size(FILE* const file); void init_terminal(void);