OSDN Git Service

Only check current clock() on every 32-th cycle, in order to limit the overhead.
authorLoRd_MuldeR <mulder2@gmx.de>
Sun, 28 Mar 2021 14:17:06 +0000 (16:17 +0200)
committerLoRd_MuldeR <mulder2@gmx.de>
Sun, 28 Mar 2021 14:17:06 +0000 (16:17 +0200)
frontend/src/main.c
libslunkcrypt/src/slunkcrypt.c

index 49a0e02..d6aa189 100644 (file)
@@ -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;
+                       }
                }
        }
 
index 2201323..0e468e3 100644 (file)
@@ -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();
        }