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

@ -2,7 +2,9 @@
# Options # Options
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
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

@ -10,92 +10,84 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#ifdef _WIN32 #ifdef _WIN32
#include <share.h> # include <share.h>
#ifndef _SH_SECURE # ifndef _SH_SECURE
#define _SH_SECURE 0x80 # define _SH_SECURE 0x80
#endif # endif
#endif #endif
#ifdef _WIN32 #if defined(_WIN32)
#define OS_TYPE "Win" # define OS_TYPE "Windows"
#elif defined(__CYGWIN__)
# define OS_TYPE "Cygwin"
#elif defined(__linux__)
# define OS_TYPE "Linux"
#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 #else
#ifdef __CYGWIN__ # error Unknown operating system!
#define OS_TYPE "Cygwin"
#else
#ifdef __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"
#else
#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"
#elif defined(__i386__) || defined(_M_IX86)
# define CPU_ARCH "x86"
#elif defined(__aarch64__) || defined(_M_ARM64)
# define CPU_ARCH "arm64"
#elif defined(__arm__) || defined(_M_ARM)
# define CPU_ARCH "arm"
#else #else
#if defined(__i386__) || defined(_M_IX86) # error Unknown CPU architecture!
#define CPU_ARCH "x86"
#else
#if defined(__aarch64__) || defined(_M_ARM64)
#define CPU_ARCH "arm64"
#else
#if defined(__arm__) || defined(_M_ARM)
#define CPU_ARCH "arm"
#else
#error Unknown CPU architecture!
#endif
#endif
#endif
#endif #endif
#ifdef _WIN32 #ifdef _WIN32
#define MAIN wmain # define MAIN wmain
#define CHR wchar_t # define CHR wchar_t
#define _T(X) L##X # define _T(X) L##X
#define GETENV(X) _wgetenv((X)) # define GETENV(X) _wgetenv((X))
#define STRLEN(X) wcslen((X)) # define STRLEN(X) wcslen((X))
#define STRICMP(X,Y) _wcsicmp((X),(Y)) # define STRICMP(X,Y) _wcsicmp((X),(Y))
#define STRRCHR(X,Y) wcsrchr((X),(Y)) # define STRRCHR(X,Y) wcsrchr((X),(Y))
#define FPUTS(X,Y) fputws((X),(Y)) # define FPUTS(X,Y) fputws((X),(Y))
#define FPRINTF(X,Y,...) fwprintf((X),(Y),__VA_ARGS__) # define FPRINTF(X,Y,...) fwprintf((X),(Y),__VA_ARGS__)
#define FOPEN(X,Y) _wfsopen((X),(Y),_SH_SECURE) # define FOPEN(X,Y) _wfsopen((X),(Y),_SH_SECURE)
#ifdef __USE_MINGW_ANSI_STDIO # ifdef __USE_MINGW_ANSI_STDIO
#define PRISTR "ls" # define PRISTR "ls"
#define PRIstr "hs" # define PRIstr "hs"
#define PRIwcs "ls" # define PRIwcs "ls"
# else
# define PRISTR "s"
# define PRIstr "S"
# define PRIwcs "s"
# endif
#else #else
#define PRISTR "s" # define MAIN main
#define PRIstr "S" # define CHR char
#define PRIwcs "s" # define _T(X) X
#endif # define GETENV(X) getenv((X))
#else # define STRLEN(X) strlen((X))
#define MAIN main # define STRICMP(X,Y) strcasecmp((X),(Y))
#define CHR char # define STRRCHR(X,Y) strrchr((X),(Y))
#define _T(X) X # define FPUTS(X,Y) fputs((X),(Y))
#define GETENV(X) getenv((X)) # define FPRINTF(X,Y,...) fprintf((X),(Y),__VA_ARGS__)
#define STRLEN(X) strlen((X)) # define FOPEN(X,Y) fopen((X),(Y))
#define STRICMP(X,Y) strcasecmp((X),(Y)) # define PRISTR "s"
#define STRRCHR(X,Y) strrchr((X),(Y)) # define PRIstr "s"
#define FPUTS(X,Y) fputs((X),(Y)) # define PRIwcs "ls"
#define FPRINTF(X,Y,...) fprintf((X),(Y),__VA_ARGS__)
#define FOPEN(X,Y) fopen((X),(Y))
#define PRISTR "s"
#define PRIstr "s"
#define PRIwcs "ls"
#endif #endif
#define T(X) _T(X) #define T(X) _T(X)
#ifdef _MSC_VER #ifdef _MSC_VER
#define strdup(X) _strdup((X)) # define strdup(X) _strdup((X))
#endif #endif
#endif #endif

View File

@ -6,44 +6,42 @@
#include <slunkcrypt.h> #include <slunkcrypt.h>
#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 # endif
static const char *const DEV_RANDOM[] = { "/dev/urandom", "/dev/arandom", "/dev/random", NULL };
#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
} }
} }