Solaris compile fix.

This commit is contained in:
LoRd_MuldeR 2020-10-21 21:58:46 +02:00
parent 24574712d1
commit 183b40f97d
Signed by: mulder
GPG Key ID: 2B5913365F57E03F
3 changed files with 94 additions and 101 deletions

View File

@ -3,6 +3,8 @@
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
DEBUG ?= 0 DEBUG ?= 0
STATIC ?= 0
MARCH ?= native MARCH ?= native
MTUNE ?= native MTUNE ?= native
@ -26,7 +28,7 @@ ifeq ($(DEBUG),1)
else else
CONFIG := CONFIG :=
CFLAGS += -O3 -DNDEBUG CFLAGS += -O3 -DNDEBUG
LDFLGS += -static -s LDFLGS := -s
endif endif
MACHINE := $(shell $(CC) -dumpmachine) MACHINE := $(shell $(CC) -dumpmachine)
@ -37,6 +39,10 @@ else
SUFFIX := SUFFIX :=
endif endif
ifeq ($(STATIC),1)
LDFLGS += -static
endif
ifneq ($(filter %-w64-mingw32,$(MACHINE)),) ifneq ($(filter %-w64-mingw32,$(MACHINE)),)
LDFLGS += -mconsole -municode LDFLGS += -mconsole -municode
endif endif

View File

@ -16,45 +16,37 @@
# endif # endif
#endif #endif
#ifdef _WIN32 #if defined(_WIN32)
#define OS_TYPE "Win" # define OS_TYPE "Windows"
#else #elif defined(__CYGWIN__)
#ifdef __CYGWIN__
# define OS_TYPE "Cygwin" # define OS_TYPE "Cygwin"
#else #elif defined(__linux__)
#ifdef __linux__
# define OS_TYPE "Linux" # define OS_TYPE "Linux"
#else #elif defined(__FreeBSD__)
#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) # define OS_TYPE "FreeBSD"
#define OS_TYPE "BSD" #elif defined(__NetBSD__)
#else # define OS_TYPE "NetBSD"
#if defined(__APPLE__) && defined(__MACH__) #elif defined(__OpenBSD__)
#define OS_TYPE "macOS" # define OS_TYPE "OpenBSD"
#elif defined(__sun)
# define OS_TYPE "Solaris"
#elif defined(__unix__)
# define OS_TYPE "Unix"
#else #else
# error Unknown operating system! # error Unknown operating system!
#endif #endif
#endif
#endif
#endif
#endif
#if defined(__x86_64__) || defined(_M_X64) #if defined(__x86_64__) || defined(_M_X64)
# define CPU_ARCH "x64" # define CPU_ARCH "x64"
#else #elif defined(__i386__) || defined(_M_IX86)
#if defined(__i386__) || defined(_M_IX86)
# define CPU_ARCH "x86" # define CPU_ARCH "x86"
#else #elif defined(__aarch64__) || defined(_M_ARM64)
#if defined(__aarch64__) || defined(_M_ARM64)
# define CPU_ARCH "arm64" # define CPU_ARCH "arm64"
#else #elif defined(__arm__) || defined(_M_ARM)
#if defined(__arm__) || defined(_M_ARM)
# define CPU_ARCH "arm" # define CPU_ARCH "arm"
#else #else
# error Unknown CPU architecture! # error Unknown CPU architecture!
#endif #endif
#endif
#endif
#endif
#ifdef _WIN32 #ifdef _WIN32
# define MAIN wmain # define MAIN wmain

View File

@ -8,42 +8,40 @@
#ifdef _WIN32 #ifdef _WIN32
# define WIN32_LEAN_AND_MEAN 1 # define WIN32_LEAN_AND_MEAN 1
# include <Windows.h> # include <Windows.h>
typedef BOOLEAN(WINAPI *genrandom_t)(void*, ULONG);
static volatile LONG g_random_init = 0L;
static HMODULE g_advapi32 = NULL;
static genrandom_t g_genrandom = NULL;
#else #else
# include <unistd.h> # include <unistd.h>
# include <fcntl.h> # include <fcntl.h>
# include <string.h> # include <string.h>
#if (defined(__GLIBC__) && __GLIBC_PREREQ(2,25)) || (defined(__FreeBSD__) && (__FreeBSD__ >= 12)) # if (defined(__GLIBC__) && (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 25)) || (defined(__FreeBSD__) && (__FreeBSD__ >= 12))
# include <sys/random.h> # include <sys/random.h>
#else
static const char *const DEV_RANDOM[] = { "/dev/urandom", "/dev/arandom", "/dev/random", NULL };
# endif # endif
#endif #endif
#ifdef _WIN32 #ifdef _WIN32
static genrandom_t init_genrandom() typedef BOOLEAN(WINAPI *genrandom_t)(void*, ULONG);
static genrandom_t win32_init_genrandom()
{ {
static volatile LONG s_random_init = 0L;
static HMODULE s_advapi32 = NULL;
static genrandom_t s_genrandom = NULL;
LONG state; LONG state;
while ((state = InterlockedCompareExchange(&g_random_init, -1L, 0L)) != 0L) while ((state = InterlockedCompareExchange(&s_random_init, -1L, 0L)) != 0L)
{ {
if (state > 0L) if (state > 0L)
{ {
return g_genrandom; return s_genrandom;
} }
Sleep(0U); Sleep(0U);
} }
if (g_advapi32 || (g_advapi32 = LoadLibrary(L"advapi32.dll"))) if (s_advapi32 || (s_advapi32 = LoadLibraryW(L"advapi32.dll")))
{ {
if ((g_genrandom = (genrandom_t)GetProcAddress(g_advapi32, "SystemFunction036"))) if ((s_genrandom = (genrandom_t)GetProcAddress(s_advapi32, "SystemFunction036")))
{ {
InterlockedExchange(&g_random_init, 1L); InterlockedExchange(&s_random_init, 1L);
return g_genrandom; return s_genrandom;
} }
} }
InterlockedExchange(&g_random_init, 0L); InterlockedExchange(&s_random_init, 0L);
return NULL; return NULL;
} }
#endif #endif
@ -53,21 +51,21 @@ int slunkcrypt_random_bytes(uint8_t* const buffer, const size_t length)
#ifdef _WIN32 #ifdef _WIN32
if ((length <= ((size_t)ULONG_MAX))) if ((length <= ((size_t)ULONG_MAX)))
{ {
const genrandom_t genrandom = init_genrandom(); const genrandom_t genrandom = win32_init_genrandom();
if (genrandom) if (genrandom)
{ {
return genrandom(buffer, (ULONG)length) ? 0 : (-1); return genrandom(buffer, (ULONG)length) ? 0 : (-1);
} }
} }
return -1; return -1;
#else #elif (defined(__GLIBC__) && (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 25)) || (defined(__FreeBSD__) && (__FreeBSD__ >= 12))
#if (defined(__GLIBC__) && __GLIBC_PREREQ(2,25)) || (defined(__FreeBSD__) && (__FreeBSD__ >= 12))
if (getrandom(buffer, length, 0U) >= length) if (getrandom(buffer, length, 0U) >= length)
{ {
return 0; return 0;
} }
return -1; return -1;
#else #else
static const char *const DEV_RANDOM[] = { "/dev/urandom", "/dev/arandom", "/dev/random", NULL };
int result = -1; int result = -1;
for (size_t i = 0U; DEV_RANDOM[i] && (result != 0); ++i) for (size_t i = 0U; DEV_RANDOM[i] && (result != 0); ++i)
{ {
@ -83,7 +81,6 @@ int slunkcrypt_random_bytes(uint8_t* const buffer, const size_t length)
} }
return result; return result;
#endif #endif
#endif
} }
void slunkcrypt_bzero(void* const ptr, const size_t length) void slunkcrypt_bzero(void* const ptr, const size_t length)
@ -92,8 +89,7 @@ void slunkcrypt_bzero(void* const ptr, const size_t length)
{ {
#if defined(_WIN32) && defined(SecureZeroMemory) #if defined(_WIN32) && defined(SecureZeroMemory)
SecureZeroMemory(ptr, length); SecureZeroMemory(ptr, length);
#else #elif (defined(__GLIBC__) && (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 25)) || (defined(__FreeBSD__) && (__FreeBSD__ >= 11))
#if (defined(__GLIBC__) && __GLIBC_PREREQ(2,25)) || (defined(__FreeBSD__) && (__FreeBSD__ >= 11))
explicit_bzero(ptr, length); explicit_bzero(ptr, length);
#else #else
volatile uint8_t *buffer = (volatile uint8_t*)ptr; volatile uint8_t *buffer = (volatile uint8_t*)ptr;
@ -101,7 +97,6 @@ void slunkcrypt_bzero(void* const ptr, const size_t length)
{ {
buffer[i] = 0U; buffer[i] = 0U;
} }
#endif
#endif #endif
} }
} }