Small improvement to debug logging code.

This commit is contained in:
LoRd_MuldeR 2022-10-14 22:48:47 +02:00
parent b0cd820fdf
commit b654c89dbf
Signed by: mulder
GPG Key ID: 2B5913365F57E03F
7 changed files with 58 additions and 58 deletions

View File

@ -40,6 +40,7 @@
# endif # endif
#else #else
# include <unistd.h> # include <unistd.h>
# include <syslog.h>
# define STAT_T struct stat # define STAT_T struct stat
# define STAT(X,Y) stat((X),(Y)) # define STAT(X,Y) stat((X),(Y))
# define FSTAT(X,Y) fstat((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"); 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 #endif
void init_terminal(void) void init_terminal(void)
{ {
#ifdef _WIN32 #ifdef _WIN32
SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX); SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);
set_translation_mode(stderr, 1); _setmode(_fileno(stderr), _O_U8TEXT);
set_translation_mode(stdin, 0); _setmode(_fileno(stdin), _O_BINARY);
clear_cmdline_args(_acmdln, _wcmdln); clear_cmdline_args(_acmdln, _wcmdln);
#else
openlog("slunkcrypt", LOG_PID | LOG_CONS, LOG_USER);
#endif #endif
} }

View File

@ -6,21 +6,28 @@
#ifndef INC_SLUNKCRYPT_COMPILER_H #ifndef INC_SLUNKCRYPT_COMPILER_H
#define 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 */ /* Intel(R) oneAPI DPC++/C++ Compiler */
#if defined(__INTEL_LLVM_COMPILER) && (!defined(__GNUC__)) #if defined(__INTEL_LLVM_COMPILER) && (!defined(__GNUC__))
# define __GNUC__ 4 # define __GNUC__ 4
#endif #endif
/* Compiler compatibility */ /* Compiler compatibility */
#if defined(_MSC_VER) && (!defined(__GNUC__)) #if defined(_MSC_VER) && (!defined(__GNUC__))
# define INLINE __inline # define INLINE __inline
# define UNUSED __pragma(warning(suppress: 4189)) # define UNUSED __pragma(warning(suppress: 4189))
#elif defined(__GNUC__) #elif defined(__GNUC__)
# define INLINE __inline__ # define INLINE __inline__
# define UNUSED __attribute__((unused)) # define UNUSED __attribute__((unused))
#else #else
# define INLINE inline # define INLINE inline
# define UNUSED # define UNUSED
#endif #endif
#endif #endif

View File

@ -10,31 +10,37 @@
#include <stdio.h> #include <stdio.h>
#include <stdarg.h> #include <stdarg.h>
/* syslog */ /* logging sub-system */
#if defined(_WIN32) #ifdef _WIN32
# define WIN32_LEAN_AND_MEAN 1 # define WIN32_LEAN_AND_MEAN 1
# include <Windows.h> # include <Windows.h>
#elif defined(__unix__) || defined(__HAIKU__) #else
# include <syslog.h> # include <syslog.h>
#endif #endif
void slunkcrypt_debug_write(const char* const text) void slunkcrypt_debug_write(const char* const message)
{ {
#if defined(_WIN32) #ifdef _WIN32
OutputDebugStringA(text); OutputDebugStringA(message);
#elif defined(__unix__) || defined(__HAIKU__) #else
syslog(LOG_DEBUG, "%s", text); syslog(LOG_ALERT, "%s", message);
#endif #endif
} }
void slunkcrypt_debug_print(const char* const text, ...) void slunkcrypt_debug_print(const char* const message, ...)
{ {
#ifdef _WIN32
char buffer[100U]; char buffer[100U];
va_list ap; #endif
va_start(ap, text); va_list args;
if (vsnprintf(buffer, 100U, text, ap) > 0) 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);
} }

View File

@ -6,7 +6,14 @@
#ifndef INC_SLUNKCRYPT_DEBUG_H #ifndef INC_SLUNKCRYPT_DEBUG_H
#define INC_SLUNKCRYPT_DEBUG_H #define INC_SLUNKCRYPT_DEBUG_H
void slunkcrypt_debug_write(const char *const text); void slunkcrypt_debug_write(const char *const message);
void slunkcrypt_debug_print(const char *const text, ...); 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 #endif

View File

@ -3,16 +3,9 @@
/* This work has been released under the CC0 1.0 Universal license! */ /* 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 */ /* Internal */
#include "slunkcrypt.h"
#include "compiler.h" #include "compiler.h"
#include "slunkcrypt.h"
/* CRT */ /* CRT */
#include <string.h> #include <string.h>

View File

@ -3,16 +3,10 @@
/* This work has been released under the CC0 1.0 Universal license! */ /* 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 */ /* Internal */
#include "compiler.h"
#include "slunkcrypt.h" #include "slunkcrypt.h"
#include "debug.h" #include "debug.h"
#include "compiler.h"
#include "keygen.h" #include "keygen.h"
#include "initialize.h" #include "initialize.h"
#include "thread.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 */ /* dump the final configuration */
if (debug) 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("cntr = %08X", data->thread_data[0].counter);
slunkcrypt_debug_print("drbg = %08X %08X %08X %08X %08X %08X", 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);
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);
for (r = 0U; r < 256U; ++r) for (r = 0U; r < 256U; ++r)
{ {
char text[775U]; char message[775U] = { '\0' };
size_t off = sprintf(text, "[%02X] =", (unsigned)r); char *ptr = message;
DBG_PRINTF(ptr, "[%02X] =", (unsigned)r);
for (i = 0U; i < 256U; ++i) 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(); CHECK_ABORTED();
slunkcrypt_debug_write(text); slunkcrypt_debug_write(message);
} }
} }

View File

@ -5,15 +5,9 @@
#ifndef SLUNKBUILD_NOTHREADS #ifndef SLUNKBUILD_NOTHREADS
#ifdef _WIN32
# define _CRT_SECURE_NO_WARNINGS 1
#else
# define _GNU_SOURCE 1
#endif
/* Internal */ /* Internal */
#include "thread.h"
#include "compiler.h" #include "compiler.h"
#include "thread.h"
/* CRT */ /* CRT */
#include <stdlib.h> #include <stdlib.h>