From 163dee5e9d41da3b7257adcc5e631a2f3f2a037c Mon Sep 17 00:00:00 2001 From: LoRd_MuldeR Date: Sun, 28 Mar 2021 16:17:06 +0200 Subject: [PATCH] Only check current clock() on every 32-th cycle, in order to limit the overhead. --- frontend/src/main.c | 31 +++++++++++++++++++++---------- libslunkcrypt/src/slunkcrypt.c | 3 +-- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/frontend/src/main.c b/frontend/src/main.c index 49a0e02..d6aa189 100644 --- a/frontend/src/main.c +++ b/frontend/src/main.c @@ -317,7 +317,8 @@ static int encrypt(const char* const passphrase, const CHR* const input_path, co goto clean_up; } - clock_t clk_now, clk_update = clock(); + unsigned refresh_cycles = 0U; + clock_t clk_update = clock(); uint64_t bytes_read = 0U; uint8_t buffer[BUFFER_SIZE]; @@ -351,11 +352,15 @@ static int encrypt(const char* const passphrase, const CHR* const input_path, co { break; /*EOF*/ } - if ((clk_now = clock()) - clk_update > UPDATE_INTERVAL) + if (!(++refresh_cycles & 0x1F)) { - FPRINTF(stderr, T("\b\b\b\b\b\b\b%5.1f%% "), (bytes_read / ((double)file_size)) * 100.0); - fflush(stderr); - clk_update = clk_now; + const clock_t clk_now = clock(); + if ((clk_now < clk_update) || (clk_now - clk_update > UPDATE_INTERVAL)) + { + FPRINTF(stderr, T("\b\b\b\b\b\b\b%5.1f%% "), (bytes_read / ((double)file_size)) * 100.0); + fflush(stderr); + clk_update = clk_now; + } } } @@ -488,9 +493,11 @@ static int decrypt(const char* const passphrase, const CHR* const input_path, co goto clean_up; } - clock_t clk_now, clk_update = clock(); + unsigned refresh_cycles = 0U; + clock_t clk_update = clock(); uint64_t bytes_read = sizeof(uint64_t); uint8_t buffer[BUFFER_SIZE]; + const uint64_t read_limit = round_down(file_size, sizeof(uint64_t)) - (2U * sizeof(uint64_t)); blake2s_t blake2s_state; @@ -523,11 +530,15 @@ static int decrypt(const char* const passphrase, const CHR* const input_path, co { break; /*EOF*/ } - if ((clk_now = clock()) - clk_update > UPDATE_INTERVAL) + if (!(++refresh_cycles & 0x1F)) { - FPRINTF(stderr, T("\b\b\b\b\b\b\b%5.1f%% "), (bytes_read / ((double)read_limit)) * 100.0); - fflush(stderr); - clk_update = clk_now; + const clock_t clk_now = clock(); + if ((clk_now < clk_update) || (clk_now - clk_update > UPDATE_INTERVAL)) + { + FPRINTF(stderr, T("\b\b\b\b\b\b\b%5.1f%% "), (bytes_read / ((double)read_limit)) * 100.0); + fflush(stderr); + clk_update = clk_now; + } } } diff --git a/libslunkcrypt/src/slunkcrypt.c b/libslunkcrypt/src/slunkcrypt.c index 2201323..0e468e3 100644 --- a/libslunkcrypt/src/slunkcrypt.c +++ b/libslunkcrypt/src/slunkcrypt.c @@ -242,8 +242,7 @@ static int initialize_state(crypt_state_t* const crypt_state, const uint64_t non } for (i = 0U; i < 256U; ++i) { - const size_t j = crypt_state->wheel_fwd[r][i]; - crypt_state->wheel_bwd[255U - r][j] = (uint8_t)i; + crypt_state->wheel_bwd[255U - r][crypt_state->wheel_fwd[r][i]] = (uint8_t)i; } CHECK_ABORTED(); }