Ignore excess bytes, if length of input file is *not* an integer multiple of 8.

This commit is contained in:
LoRd_MuldeR 2020-12-01 03:03:49 +01:00
parent c530556e94
commit fb4f429493
Signed by: mulder
GPG Key ID: 2B5913365F57E03F
3 changed files with 16 additions and 1 deletions

View File

@ -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);

View File

@ -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;
}

View File

@ -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)