From b654c89dbf3fc14fa8333b5499f738ce7f57bf0e Mon Sep 17 00:00:00 2001 From: LoRd_MuldeR Date: Fri, 14 Oct 2022 22:48:47 +0200 Subject: [PATCH] Small improvement to debug logging code. --- frontend/src/utils.c | 11 +++++------ libslunkcrypt/src/compiler.h | 21 ++++++++++++++------- libslunkcrypt/src/debug.c | 34 ++++++++++++++++++++-------------- libslunkcrypt/src/debug.h | 11 +++++++++-- libslunkcrypt/src/junk.c | 9 +-------- libslunkcrypt/src/slunkcrypt.c | 22 ++++++++-------------- libslunkcrypt/src/thread.c | 8 +------- 7 files changed, 58 insertions(+), 58 deletions(-) diff --git a/frontend/src/utils.c b/frontend/src/utils.c index 9f08794..b9fa881 100644 --- a/frontend/src/utils.c +++ b/frontend/src/utils.c @@ -40,6 +40,7 @@ # endif #else # include +# include # define STAT_T struct stat # define STAT(X,Y) stat((X),(Y)) # define FSTAT(X,Y) fstat((X),(Y)) @@ -73,19 +74,17 @@ static void clear_cmdline_args(char *const acmdln, wchar_t *const wcmdln) if (len > 5U) wcscpy(wcmdln, L"slunk"); } } -static void set_translation_mode(FILE* const stream, const int utf8_mode) -{ - _setmode(_fileno(stream), utf8_mode ? _O_U8TEXT : _O_BINARY); -} #endif void init_terminal(void) { #ifdef _WIN32 SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX); - set_translation_mode(stderr, 1); - set_translation_mode(stdin, 0); + _setmode(_fileno(stderr), _O_U8TEXT); + _setmode(_fileno(stdin), _O_BINARY); clear_cmdline_args(_acmdln, _wcmdln); +#else + openlog("slunkcrypt", LOG_PID | LOG_CONS, LOG_USER); #endif } diff --git a/libslunkcrypt/src/compiler.h b/libslunkcrypt/src/compiler.h index 6f4c80d..00a04a4 100644 --- a/libslunkcrypt/src/compiler.h +++ b/libslunkcrypt/src/compiler.h @@ -6,21 +6,28 @@ #ifndef INC_SLUNKCRYPT_COMPILER_H #define INC_SLUNKCRYPT_COMPILER_H +#ifdef _WIN32 +# define WIN32_LEAN_AND_MEAN 1 +# define _CRT_SECURE_NO_WARNINGS 1 +#else +# define _GNU_SOURCE 1 +#endif + /* Intel(R) oneAPI DPC++/C++ Compiler */ #if defined(__INTEL_LLVM_COMPILER) && (!defined(__GNUC__)) -# define __GNUC__ 4 +# define __GNUC__ 4 #endif /* Compiler compatibility */ #if defined(_MSC_VER) && (!defined(__GNUC__)) -# define INLINE __inline -# define UNUSED __pragma(warning(suppress: 4189)) +# define INLINE __inline +# define UNUSED __pragma(warning(suppress: 4189)) #elif defined(__GNUC__) -# define INLINE __inline__ -# define UNUSED __attribute__((unused)) +# define INLINE __inline__ +# define UNUSED __attribute__((unused)) #else -# define INLINE inline -# define UNUSED +# define INLINE inline +# define UNUSED #endif #endif diff --git a/libslunkcrypt/src/debug.c b/libslunkcrypt/src/debug.c index d0df4dc..b9c8c4b 100644 --- a/libslunkcrypt/src/debug.c +++ b/libslunkcrypt/src/debug.c @@ -10,31 +10,37 @@ #include #include -/* syslog */ -#if defined(_WIN32) +/* logging sub-system */ +#ifdef _WIN32 # define WIN32_LEAN_AND_MEAN 1 # include -#elif defined(__unix__) || defined(__HAIKU__) +#else # include #endif -void slunkcrypt_debug_write(const char* const text) +void slunkcrypt_debug_write(const char* const message) { -#if defined(_WIN32) - OutputDebugStringA(text); -#elif defined(__unix__) || defined(__HAIKU__) - syslog(LOG_DEBUG, "%s", text); +#ifdef _WIN32 + OutputDebugStringA(message); +#else + syslog(LOG_ALERT, "%s", message); #endif } -void slunkcrypt_debug_print(const char* const text, ...) +void slunkcrypt_debug_print(const char* const message, ...) { +#ifdef _WIN32 char buffer[100U]; - va_list ap; - va_start(ap, text); - if (vsnprintf(buffer, 100U, text, ap) > 0) +#endif + va_list args; + va_start(args, message); +#ifdef _WIN32 + if (vsnprintf(buffer, 100U, message, args) > 0) { - slunkcrypt_debug_write(buffer); + OutputDebugStringA(buffer); } - va_end(ap); +#else + vsyslog(LOG_ALERT, message, args); +#endif + va_end(args); } diff --git a/libslunkcrypt/src/debug.h b/libslunkcrypt/src/debug.h index 8e082a9..0fc68c6 100644 --- a/libslunkcrypt/src/debug.h +++ b/libslunkcrypt/src/debug.h @@ -6,7 +6,14 @@ #ifndef INC_SLUNKCRYPT_DEBUG_H #define INC_SLUNKCRYPT_DEBUG_H -void slunkcrypt_debug_write(const char *const text); -void slunkcrypt_debug_print(const char *const text, ...); +void slunkcrypt_debug_write(const char *const message); +void slunkcrypt_debug_print(const char *const message, ...); + +#define DBG_PRINTF(BUFF, FORMAT, ...) do \ +{ \ + const int _strlen = sprintf((BUFF), (FORMAT), __VA_ARGS__); \ + if (_strlen > 0) { BUFF += _strlen; } \ +} \ +while (0) #endif diff --git a/libslunkcrypt/src/junk.c b/libslunkcrypt/src/junk.c index a83213e..6ee40d3 100644 --- a/libslunkcrypt/src/junk.c +++ b/libslunkcrypt/src/junk.c @@ -3,16 +3,9 @@ /* This work has been released under the CC0 1.0 Universal license! */ /******************************************************************************/ -#ifdef _WIN32 -# define WIN32_LEAN_AND_MEAN 1 -# define _CRT_SECURE_NO_WARNINGS 1 -#else -# define _GNU_SOURCE 1 -#endif - /* Internal */ -#include "slunkcrypt.h" #include "compiler.h" +#include "slunkcrypt.h" /* CRT */ #include diff --git a/libslunkcrypt/src/slunkcrypt.c b/libslunkcrypt/src/slunkcrypt.c index 72fccad..29f2d0a 100644 --- a/libslunkcrypt/src/slunkcrypt.c +++ b/libslunkcrypt/src/slunkcrypt.c @@ -3,16 +3,10 @@ /* This work has been released under the CC0 1.0 Universal license! */ /******************************************************************************/ -#ifdef _WIN32 -# define _CRT_SECURE_NO_WARNINGS 1 -#else -# define _GNU_SOURCE 1 -#endif - /* Internal */ +#include "compiler.h" #include "slunkcrypt.h" #include "debug.h" -#include "compiler.h" #include "keygen.h" #include "initialize.h" #include "thread.h" @@ -219,20 +213,20 @@ static int initialize_state(crypt_data_t *const data, const size_t thread_count, /* dump the final configuration */ if (debug) { + const rand_state_t *const rand_state = &data->thread_data[0].random; slunkcrypt_debug_print("cntr = %08X", data->thread_data[0].counter); - slunkcrypt_debug_print("drbg = %08X %08X %08X %08X %08X %08X", - data->thread_data[0].random.d, data->thread_data[0].random.v, data->thread_data[0].random.w, - data->thread_data[0].random.x, data->thread_data[0].random.y, data->thread_data[0].random.z); + slunkcrypt_debug_print("drbg = %08X %08X %08X %08X %08X %08X", rand_state->d, rand_state->v, rand_state->w, rand_state->x, rand_state->y, rand_state->z); for (r = 0U; r < 256U; ++r) { - char text[775U]; - size_t off = sprintf(text, "[%02X] =", (unsigned)r); + char message[775U] = { '\0' }; + char *ptr = message; + DBG_PRINTF(ptr, "[%02X] =", (unsigned)r); for (i = 0U; i < 256U; ++i) { - off += sprintf(text + off, " %02X", data->wheel[r][i]); + DBG_PRINTF(ptr, " %02X", data->wheel[r][i]); } CHECK_ABORTED(); - slunkcrypt_debug_write(text); + slunkcrypt_debug_write(message); } } diff --git a/libslunkcrypt/src/thread.c b/libslunkcrypt/src/thread.c index 7a73329..de91ffa 100644 --- a/libslunkcrypt/src/thread.c +++ b/libslunkcrypt/src/thread.c @@ -5,15 +5,9 @@ #ifndef SLUNKBUILD_NOTHREADS -#ifdef _WIN32 -# define _CRT_SECURE_NO_WARNINGS 1 -#else -# define _GNU_SOURCE 1 -#endif - /* Internal */ -#include "thread.h" #include "compiler.h" +#include "thread.h" /* CRT */ #include