From fb4f429493d6e942652951c94ace2c3d57ccc726 Mon Sep 17 00:00:00 2001 From: LoRd_MuldeR Date: Tue, 1 Dec 2020 03:03:49 +0100 Subject: [PATCH] Ignore excess bytes, if length of input file is *not* an integer multiple of 8. --- frontend/src/main.c | 6 +++++- frontend/src/utils.c | 10 ++++++++++ frontend/src/utils.h | 1 + 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/frontend/src/main.c b/frontend/src/main.c index 420768d..339352f 100644 --- a/frontend/src/main.c +++ b/frontend/src/main.c @@ -358,6 +358,10 @@ static int decrypt(const char* const passphrase, const CHR* const input_path, co FPUTS(T("Error: Input file is too small! Truncated?\n\n"), stderr); goto clean_up; } + else if ((file_size % sizeof(uint64_t)) != 0) + { + FPRINTF(stderr, T("Warning: File size is *not* an integer multiple of %u, ignoring excess bytes!\n\n"), sizeof(uint64_t)); + } FPUTS(T("Decrypting file contents, please be patient... "), stderr); fflush(stderr); @@ -379,7 +383,7 @@ static int decrypt(const char* const passphrase, const CHR* const input_path, co clock_t clk_now, clk_update = clock(); uint64_t crc_actual = CRC_INITIALIZER, bytes_read = sizeof(uint64_t); uint8_t buffer[BUFFER_SIZE]; - const uint64_t read_limit = file_size - (2U * sizeof(uint64_t)); + const uint64_t read_limit = round_down(file_size, sizeof(uint64_t)) - (2U * sizeof(uint64_t)); FPRINTF(stderr, T("%5.1f%% "), 0.0); fflush(stderr); diff --git a/frontend/src/utils.c b/frontend/src/utils.c index 016db2d..c2494c1 100644 --- a/frontend/src/utils.c +++ b/frontend/src/utils.c @@ -177,3 +177,13 @@ const CHR* get_file_name(const CHR* path) } return path; } + +// ========================================================================== +// Math functions +// ========================================================================== + +uint64_t round_down(const uint64_t value, const uint64_t base) +{ + const uint64_t modulus = value % base; + return modulus ? (value - modulus) : value; +} diff --git a/frontend/src/utils.h b/frontend/src/utils.h index 01e60a1..b4556a3 100644 --- a/frontend/src/utils.h +++ b/frontend/src/utils.h @@ -17,6 +17,7 @@ uint64_t swap_bytes_u64(const uint64_t value); char* CHR_to_utf8(const CHR *const input); uint64_t get_file_size(FILE* const file); const CHR *get_file_name(const CHR *path); +uint64_t round_down(const uint64_t value, const uint64_t base); #define GET_NIBBLE(X) ((X) & 0x0F) #define SET_NIBBLE(X, Y) do { X = ((X) & 0xF0) | ((Y) & 0x0F); } while(0)