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

View File

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

View File

@ -8,42 +8,40 @@
#ifdef _WIN32
# define WIN32_LEAN_AND_MEAN 1
# 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
# include <unistd.h>
# include <fcntl.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>
#else
static const char *const DEV_RANDOM[] = { "/dev/urandom", "/dev/arandom", "/dev/random", NULL };
# endif
#endif
#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;
while ((state = InterlockedCompareExchange(&g_random_init, -1L, 0L)) != 0L)
while ((state = InterlockedCompareExchange(&s_random_init, -1L, 0L)) != 0L)
{
if (state > 0L)
{
return g_genrandom;
return s_genrandom;
}
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);
return g_genrandom;
InterlockedExchange(&s_random_init, 1L);
return s_genrandom;
}
}
InterlockedExchange(&g_random_init, 0L);
InterlockedExchange(&s_random_init, 0L);
return NULL;
}
#endif
@ -53,21 +51,21 @@ int slunkcrypt_random_bytes(uint8_t* const buffer, const size_t length)
#ifdef _WIN32
if ((length <= ((size_t)ULONG_MAX)))
{
const genrandom_t genrandom = init_genrandom();
const genrandom_t genrandom = win32_init_genrandom();
if (genrandom)
{
return genrandom(buffer, (ULONG)length) ? 0 : (-1);
}
}
return -1;
#else
#if (defined(__GLIBC__) && __GLIBC_PREREQ(2,25)) || (defined(__FreeBSD__) && (__FreeBSD__ >= 12))
#elif (defined(__GLIBC__) && (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 25)) || (defined(__FreeBSD__) && (__FreeBSD__ >= 12))
if (getrandom(buffer, length, 0U) >= length)
{
return 0;
}
return -1;
#else
static const char *const DEV_RANDOM[] = { "/dev/urandom", "/dev/arandom", "/dev/random", NULL };
int result = -1;
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;
#endif
#endif
}
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)
SecureZeroMemory(ptr, length);
#else
#if (defined(__GLIBC__) && __GLIBC_PREREQ(2,25)) || (defined(__FreeBSD__) && (__FreeBSD__ >= 11))
#elif (defined(__GLIBC__) && (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 25)) || (defined(__FreeBSD__) && (__FreeBSD__ >= 11))
explicit_bzero(ptr, length);
#else
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;
}
#endif
#endif
}
}