Replaced clock() function by clock_gettime() on Linux and QueryPerformanceFrequency() on Windows.

This commit is contained in:
LoRd_MuldeR 2022-02-11 21:52:22 +01:00
parent 18e398a1e7
commit 65f08d43a5
Signed by: mulder
GPG Key ID: 2B5913365F57E03F
5 changed files with 63 additions and 16 deletions

View File

@ -25,8 +25,7 @@
// Constants // Constants
// ========================================================================== // ==========================================================================
static const uint64_t MAGIC_NUMBER = 0x243F6A8885A308D3ull; static const uint64_t MAGIC_NUMBER = 0x243F6A8885A308D3ull;
static const clock_t UPDATE_INTERVAL = (clock_t)(1.5708 * CLOCKS_PER_SEC);
#define BUFFER_SIZE 4096U #define BUFFER_SIZE 4096U
@ -103,10 +102,11 @@ int encrypt(const char *const passphrase, const CHR *const input_path, const CHR
} }
unsigned refresh_cycles = 0U; unsigned refresh_cycles = 0U;
clock_t clk_update = clock(); uint64_t bytes_read = 0U, clk_update = clock_read();
uint64_t bytes_read = 0U;
uint8_t buffer[BUFFER_SIZE]; uint8_t buffer[BUFFER_SIZE];
const uint64_t update_interval = (uint64_t)(clock_freq() * 1.0625);
blake2s_t blake2s_state; blake2s_t blake2s_state;
blake2s_init(&blake2s_state); blake2s_init(&blake2s_state);
@ -137,10 +137,10 @@ int encrypt(const char *const passphrase, const CHR *const input_path, const CHR
{ {
break; /*EOF*/ break; /*EOF*/
} }
if (!(++refresh_cycles & 0x1F)) if (!(++refresh_cycles & 0x7))
{ {
const clock_t clk_now = clock(); const uint64_t clk_now = clock_read();
if ((clk_now < clk_update) || (clk_now - clk_update > UPDATE_INTERVAL)) 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); FPRINTF(stderr, T("\b\b\b\b\b\b\b%5.1f%% "), (bytes_read / ((double)file_size)) * 100.0);
fflush(stderr); fflush(stderr);
@ -285,10 +285,10 @@ int decrypt(const char *const passphrase, const CHR *const input_path, const CHR
} }
unsigned refresh_cycles = 0U; unsigned refresh_cycles = 0U;
clock_t clk_update = clock(); uint64_t bytes_read = sizeof(uint64_t), clk_update = clock_read();
uint64_t bytes_read = sizeof(uint64_t);
uint8_t buffer[BUFFER_SIZE]; uint8_t buffer[BUFFER_SIZE];
const uint64_t update_interval = (uint64_t)(clock_freq() * 1.0625);
const uint64_t read_limit = round_down(file_size, sizeof(uint64_t)) - (2U * sizeof(uint64_t)); const uint64_t read_limit = round_down(file_size, sizeof(uint64_t)) - (2U * sizeof(uint64_t));
blake2s_t blake2s_state; blake2s_t blake2s_state;
@ -321,10 +321,10 @@ int decrypt(const char *const passphrase, const CHR *const input_path, const CHR
{ {
break; /*EOF*/ break; /*EOF*/
} }
if (!(++refresh_cycles & 0x1F)) if (!(++refresh_cycles & 0x7))
{ {
const clock_t clk_now = clock(); const uint64_t clk_now = clock_read();
if ((clk_now < clk_update) || (clk_now - clk_update > UPDATE_INTERVAL)) 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); FPRINTF(stderr, T("\b\b\b\b\b\b\b%5.1f%% "), (bytes_read / ((double)read_limit)) * 100.0);
fflush(stderr); fflush(stderr);

View File

@ -269,7 +269,7 @@ int MAIN(const int argc, CHR *const argv[])
/* Encrypt or decrypt */ /* Encrypt or decrypt */
/* ----------------------------------------------------- */ /* ----------------------------------------------------- */
const clock_t clk_start = clock(); const uint64_t clk_start = clock_read();
const int keep_incomplete = keep_incomplete_files(); const int keep_incomplete = keep_incomplete_files();
switch (slunk_mode) switch (slunk_mode)
@ -286,8 +286,7 @@ int MAIN(const int argc, CHR *const argv[])
if (!g_slunkcrypt_abort_flag) if (!g_slunkcrypt_abort_flag)
{ {
const clock_t clk_end = clock(); FPRINTF(stderr, T("--------\n\nOperation completed after %.1f seconds.\n\n"), (clock_read() - clk_start) / ((double)clock_freq()));
FPRINTF(stderr, T("--------\n\nOperation completed after %.1f seconds.\n\n"), (clk_end - clk_start) / ((double)CLOCKS_PER_SEC));
} }
/* ----------------------------------------------------- */ /* ----------------------------------------------------- */

View File

@ -21,7 +21,7 @@
// Constants // Constants
// ========================================================================== // ==========================================================================
static const size_t MIX_ROUNDS = 97U; static const size_t MIX_ROUNDS = 131U;
static const char PASSWD_SYMBOLS[] = static const char PASSWD_SYMBOLS[] =
{ {

View File

@ -17,6 +17,7 @@
/* CRT */ /* CRT */
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
#include <time.h>
#include <signal.h> #include <signal.h>
/* Platform support */ /* Platform support */
@ -104,6 +105,50 @@ void setup_signal_handler(const int signo, signal_handler_t* const handler)
#endif #endif
} }
// ==========================================================================
// Time functions
// ==========================================================================
#ifndef _WIN32
#define TSPEC_TO_MSEC(X) ((((uint64_t)(X).tv_sec) * 1000U) + (((uint64_t)(X).tv_nsec) / 1000000U))
#endif
uint64_t clock_freq(void)
{
#ifdef _WIN32
LARGE_INTEGER freq;
if (QueryPerformanceFrequency(&freq))
{
return freq.QuadPart;
}
#endif
return 1000U;
}
uint64_t clock_read(void)
{
#ifdef _WIN32
LARGE_INTEGER count;
if (QueryPerformanceCounter(&count))
{
return count.QuadPart;
}
#else
struct timespec clock_value;
#ifdef CLOCK_MONOTONIC
if (clock_gettime(CLOCK_MONOTONIC, &clock_value) == 0)
{
return TSPEC_TO_MSEC(clock_value);
}
#endif
if (clock_gettime(CLOCK_REALTIME, &clock_value) == 0)
{
return TSPEC_TO_MSEC(clock_value);
}
#endif
return 0U;
}
// ========================================================================== // ==========================================================================
// Character set conversion // Character set conversion
// ========================================================================== // ==========================================================================

View File

@ -14,6 +14,9 @@ typedef void (signal_handler_t)(int);
void init_terminal(void); void init_terminal(void);
void setup_signal_handler(const int signo, signal_handler_t* const handler); void setup_signal_handler(const int signo, signal_handler_t* const handler);
uint64_t clock_freq(void);
uint64_t clock_read(void);
void store_ui64(uint8_t* const dst, const uint64_t value); void store_ui64(uint8_t* const dst, const uint64_t value);
uint64_t load_ui64(const uint8_t* const src); uint64_t load_ui64(const uint8_t* const src);
size_t fwrite_ui64(const uint64_t value, FILE *const stream); size_t fwrite_ui64(const uint64_t value, FILE *const stream);