diff --git a/frontend/src/crypt.c b/frontend/src/crypt.c index 86df4e5..a376c46 100644 --- a/frontend/src/crypt.c +++ b/frontend/src/crypt.c @@ -4,9 +4,9 @@ /******************************************************************************/ #ifdef _WIN32 -# define _CRT_SECURE_NO_WARNINGS 1 +# define _CRT_SECURE_NO_WARNINGS 1 #else -# define _GNU_SOURCE 1 +# define _GNU_SOURCE 1 #endif /* Internal */ diff --git a/frontend/src/main.c b/frontend/src/main.c index 0be1247..2dfed86 100644 --- a/frontend/src/main.c +++ b/frontend/src/main.c @@ -4,9 +4,9 @@ /******************************************************************************/ #ifdef _WIN32 -# define _CRT_SECURE_NO_WARNINGS 1 +# define _CRT_SECURE_NO_WARNINGS 1 #else -# define _GNU_SOURCE 1 +# define _GNU_SOURCE 1 #endif /* Internal */ diff --git a/frontend/src/pwgen.c b/frontend/src/pwgen.c index 90970b0..7294abb 100644 --- a/frontend/src/pwgen.c +++ b/frontend/src/pwgen.c @@ -4,9 +4,9 @@ /******************************************************************************/ #ifdef _WIN32 -# define _CRT_SECURE_NO_WARNINGS 1 +# define _CRT_SECURE_NO_WARNINGS 1 #else -# define _GNU_SOURCE 1 +# define _GNU_SOURCE 1 #endif /* Internal */ @@ -126,7 +126,7 @@ int weak_passphrase(const char *str) { flags |= isupper(c) ? 0x1 : 0x2; } - else + else { flags |= isdigit(c) ? 0x4 : 0x8; } diff --git a/frontend/src/selftest.c b/frontend/src/selftest.c index fdb8d3c..c21e9fe 100644 --- a/frontend/src/selftest.c +++ b/frontend/src/selftest.c @@ -4,9 +4,9 @@ /******************************************************************************/ #ifdef _WIN32 -# define _CRT_SECURE_NO_WARNINGS 1 +# define _CRT_SECURE_NO_WARNINGS 1 #else -# define _GNU_SOURCE 1 +# define _GNU_SOURCE 1 #endif /* Internal */ diff --git a/frontend/src/utils.c b/frontend/src/utils.c index 4c13e4b..b9b83bc 100644 --- a/frontend/src/utils.c +++ b/frontend/src/utils.c @@ -4,10 +4,10 @@ /******************************************************************************/ #ifdef _WIN32 -# define WIN32_LEAN_AND_MEAN 1 -# define _CRT_SECURE_NO_WARNINGS 1 +# define WIN32_LEAN_AND_MEAN 1 +# define _CRT_SECURE_NO_WARNINGS 1 #else -# define _GNU_SOURCE 1 +# define _GNU_SOURCE 1 #endif /* Internal */ @@ -23,31 +23,31 @@ /* Platform support */ #ifdef _WIN32 -# include -# include -# include -# define STAT_T struct _stati64 -# define STAT(X,Y) _wstati64((X),(Y)) -# define FSTAT(X,Y) _fstati64((X),(Y)) -# define FILENO(X) _fileno((X)) -# define S_IFMT _S_IFMT -# define S_IFDIR _S_IFDIR -# define S_IFIFO _S_IFIFO -# ifndef _O_U8TEXT -# define _O_U8TEXT 0x40000 -# endif +# include +# include +# include +# define STAT_T struct _stati64 +# define STAT(X,Y) _wstati64((X),(Y)) +# define FSTAT(X,Y) _fstati64((X),(Y)) +# define FILENO(X) _fileno((X)) +# define S_IFMT _S_IFMT +# define S_IFDIR _S_IFDIR +# define S_IFIFO _S_IFIFO +# ifndef _O_U8TEXT +# define _O_U8TEXT 0x40000 +# endif #else -# include -# if defined(__USE_LARGEFILE64) && (__USE_LARGEFILE64) -# define STAT_T struct stat64 -# define STAT(X,Y) stat64((X),(Y)) -# define FSTAT(X,Y) fstat64((X),(Y)) -# else -# define STAT_T struct stat -# define STAT(X,Y) stat((X),(Y)) -# define FSTAT(X,Y) fstat((X),(Y)) -# endif -# define FILENO(X) fileno((X)) +# include +# if defined(__USE_LARGEFILE64) && (__USE_LARGEFILE64) +# define STAT_T struct stat64 +# define STAT(X,Y) stat64((X),(Y)) +# define FSTAT(X,Y) fstat64((X),(Y)) +# else +# define STAT_T struct stat +# define STAT(X,Y) stat((X),(Y)) +# define FSTAT(X,Y) fstat((X),(Y)) +# endif +# define FILENO(X) fileno((X)) #endif // ========================================================================== diff --git a/libslunkcrypt/include/slunkcrypt.h b/libslunkcrypt/include/slunkcrypt.h index 52883e1..ce9c7f8 100644 --- a/libslunkcrypt/include/slunkcrypt.h +++ b/libslunkcrypt/include/slunkcrypt.h @@ -37,7 +37,7 @@ #else #define SLUNKCRYPT_API #endif - + /* * C++ support */ diff --git a/libslunkcrypt/src/slunkcrypt.c b/libslunkcrypt/src/slunkcrypt.c index abd4462..1a07540 100644 --- a/libslunkcrypt/src/slunkcrypt.c +++ b/libslunkcrypt/src/slunkcrypt.c @@ -298,7 +298,7 @@ slunkcrypt_t slunkcrypt_alloc_ext(const uint64_t nonce, const uint8_t *const pas { crypt_state_t* state = NULL; - if ((!passwd) || (passwd_len < SLUNKCRYPT_PWDLEN_MIN) || (passwd_len > SLUNKCRYPT_PWDLEN_MAX) || + if ((!passwd) || (passwd_len < SLUNKCRYPT_PWDLEN_MIN) || (passwd_len > SLUNKCRYPT_PWDLEN_MAX) || (mode < SLUNKCRYPT_ENCRYPT) || (mode > SLUNKCRYPT_DECRYPT) || (!param) || (param->version == 0U) || (param->version > SLUNKCRYPT_PARAM_VERSION)) { return SLUNKCRYPT_NULL; diff --git a/libslunkcrypt/src/thread.c b/libslunkcrypt/src/thread.c index d4d1d70..cda59b9 100644 --- a/libslunkcrypt/src/thread.c +++ b/libslunkcrypt/src/thread.c @@ -3,6 +3,12 @@ /* 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 "thread.h" #include "compiler.h" @@ -16,10 +22,17 @@ # define PTW32_STATIC_LIB 1 #endif #include +#include -/* System info */ -#ifdef __unix__ -# include +/* Platform */ +#if defined(__linux__) || defined(PTW32_VERSION) || defined(__CYGWIN__) +# define HAVE_SCHED_GETAFFINITY 1 +#elif defined(_WIN32) +# define WIN32_LEAN_AND_MEAN 1 +# define HAVE_GETPROCESSAFFINITYMASK 1 +# include +#else +# include /* fall back to sysconf() function */ #endif /* States */ @@ -120,6 +133,35 @@ while(0) } \ while(0) +// ========================================================================== +// System info +// ========================================================================== + +static size_t detect_available_cpu_count(void) +{ + long num_processors = 0L; +#if defined(HAVE_SCHED_GETAFFINITY) + cpu_set_t cpu_mask; + CPU_ZERO(&cpu_mask); + if (sched_getaffinity(0, sizeof(cpu_set_t), &cpu_mask) == 0) + { + num_processors = CPU_COUNT(&cpu_mask); + } +#elif defined(HAVE_GETPROCESSAFFINITYMASK) + DWORD_PTR proc_mask, sys_mask; + if (GetProcessAffinityMask(GetCurrentProcess(), &proc_mask, &sys_mask)) + { + for (; proc_mask != 0U; proc_mask &= proc_mask - 1U) + { + ++num_processors; + } + } +#else + num_processors = sysconf(_SC_NPROCESSORS_ONLN); +#endif + return (num_processors > 0) ? ((size_t)num_processors) : 1U; +} + // ========================================================================== // Thread main // ========================================================================== @@ -128,7 +170,7 @@ static void *worker_thread_main(void *const arg) { thrdpl_thread_t *const data = (thrdpl_thread_t*) arg; thrdpl_shared_t *const shared = (thrdpl_shared_t*) data->shared; - + size_t previous = 0U; PTHRD_MUTEX_ENTER(&shared->mutex); @@ -157,28 +199,6 @@ static void *worker_thread_main(void *const arg) } } -// ========================================================================== -// System info -// ========================================================================== - -#if defined(__unix__) -# define NUM_PROCESSORS_FUNC get_nprocs -#elif defined(PTW32_VERSION) -# define NUM_PROCESSORS_FUNC pthread_num_processors_np -#endif - -static size_t detect_cpu_count(void) -{ -#ifdef NUM_PROCESSORS_FUNC - const int cpu_count = NUM_PROCESSORS_FUNC(); - if (cpu_count > 0) - { - return (size_t) cpu_count; - } -#endif - return 1U; -} - // ========================================================================== // Thread pool API // ========================================================================== @@ -188,7 +208,7 @@ thrdpl_t *slunkcrypt_thrdpl_create(const size_t count, const thrdpl_worker_t wor size_t i; thrdpl_t *thrdpl = NULL; - const size_t cpu_count = BOUND(1U, (count > 0U) ? count : detect_cpu_count(), MAX_THREADS); + const size_t cpu_count = BOUND(1U, (count > 0U) ? count : detect_available_cpu_count(), MAX_THREADS); if (cpu_count < 2U) { return NULL; @@ -206,7 +226,7 @@ thrdpl_t *slunkcrypt_thrdpl_create(const size_t count, const thrdpl_worker_t wor { goto failure; } - + if (pthread_cond_init(&thrdpl->shared.cond_0, NULL) != 0) { pthread_mutex_destroy(&thrdpl->shared.mutex); @@ -260,7 +280,7 @@ void slunkcrypt_thrdpl_exec(thrdpl_t *const thrdpl, uint8_t *const buffer, const thrdpl->shared.buffer = buffer; thrdpl->shared.length = length; thrdpl->shared.remain = thrdpl->shared.thread_count; - + ++thrdpl->shared.generation; PTHRD_COND_BRDCST(&thrdpl->shared.cond_0);