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

@ -26,7 +26,6 @@
// ==========================================================================
static const uint64_t MAGIC_NUMBER = 0x243F6A8885A308D3ull;
static const clock_t UPDATE_INTERVAL = (clock_t)(1.5708 * CLOCKS_PER_SEC);
#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;
clock_t clk_update = clock();
uint64_t bytes_read = 0U;
uint64_t bytes_read = 0U, clk_update = clock_read();
uint8_t buffer[BUFFER_SIZE];
const uint64_t update_interval = (uint64_t)(clock_freq() * 1.0625);
blake2s_t 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*/
}
if (!(++refresh_cycles & 0x1F))
if (!(++refresh_cycles & 0x7))
{
const clock_t clk_now = clock();
if ((clk_now < clk_update) || (clk_now - clk_update > UPDATE_INTERVAL))
const uint64_t clk_now = clock_read();
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);
@ -285,10 +285,10 @@ int decrypt(const char *const passphrase, const CHR *const input_path, const CHR
}
unsigned refresh_cycles = 0U;
clock_t clk_update = clock();
uint64_t bytes_read = sizeof(uint64_t);
uint64_t bytes_read = sizeof(uint64_t), clk_update = clock_read();
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));
blake2s_t blake2s_state;
@ -321,10 +321,10 @@ int decrypt(const char *const passphrase, const CHR *const input_path, const CHR
{
break; /*EOF*/
}
if (!(++refresh_cycles & 0x1F))
if (!(++refresh_cycles & 0x7))
{
const clock_t clk_now = clock();
if ((clk_now < clk_update) || (clk_now - clk_update > UPDATE_INTERVAL))
const uint64_t clk_now = clock_read();
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);

View File

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

View File

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

View File

@ -17,6 +17,7 @@
/* CRT */
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <signal.h>
/* Platform support */
@ -104,6 +105,50 @@ void setup_signal_handler(const int signo, signal_handler_t* const handler)
#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
// ==========================================================================

View File

@ -14,6 +14,9 @@ typedef void (signal_handler_t)(int);
void init_terminal(void);
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);
uint64_t load_ui64(const uint8_t* const src);
size_t fwrite_ui64(const uint64_t value, FILE *const stream);