From 39d9aab43964bd0521e0dfd74b4d367eeda816ac Mon Sep 17 00:00:00 2001 From: LoRd_MuldeR Date: Wed, 21 Oct 2020 17:07:03 +0200 Subject: [PATCH] Some compatibility fixes for legacy MinGW and Cygwin. --- Makefile | 19 +++++++++---------- frontend/src/main.c | 11 +++++++++++ frontend/src/platform.h | 5 ++++- frontend/src/utils.c | 8 ++++++-- libslunkcrypt/src/internal.c | 21 +++++++-------------- 5 files changed, 37 insertions(+), 27 deletions(-) diff --git a/Makefile b/Makefile index d0aae99..20ee405 100644 --- a/Makefile +++ b/Makefile @@ -20,26 +20,25 @@ SUBDIR_LIB := libslunkcrypt CFLAGS = -I$(SUBDIR_LIB)/include -Wall -Wno-trigraphs -march=$(MARCH) -mtune=$(MTUNE) ifeq ($(DEBUG),1) - CFLAGS += -Og -g CONFIG := _g - undefine LDFLGS + CFLAGS += -Og -g + LDFLGS := else + CONFIG := CFLAGS += -O3 -DNDEBUG - undefine CONFIG LDFLGS += -static -s endif -UNAME := $(shell uname) +MACHINE := $(shell $(CC) -dumpmachine) -undefine SUFFIX -ifneq ($(filter MINGW% CYGWIN%,$(UNAME)),) +ifneq ($(filter %-mingw32 %-windows-gnu %-cygwin %-cygnus,$(MACHINE)),) SUFFIX := .exe else - undefine SUFFIX + SUFFIX := endif -ifneq ($(filter MINGW%,$(UNAME)),) - LDFLGS += -municode -mconsole +ifneq ($(filter %-w64-mingw32,$(MACHINE)),) + LDFLGS += -mconsole -municode endif # --------------------------------------------------------------------------- @@ -58,7 +57,7 @@ OBJECTS_APP := $(patsubst $(SUBDIR_APP)/src/%.c,$(SUBDIR_APP)/obj/%$(CONFIG).o,$ SOURCES_LIB := $(wildcard $(SUBDIR_LIB)/src/*.c) OBJECTS_LIB := $(patsubst $(SUBDIR_LIB)/src/%.c,$(SUBDIR_LIB)/obj/%$(CONFIG).o,$(SOURCES_LIB)) -ifneq ($(filter MINGW% CYGWIN%,$(UNAME)),) +ifneq ($(filter %-mingw32 %-windows-gnu %-cygwin %-cygnus,$(MACHINE)),) RCFILES_APP := $(wildcard $(SUBDIR_APP)/res/*.rc) OBJECTS_APP += $(patsubst $(SUBDIR_APP)/res/%.rc,$(SUBDIR_APP)/obj/%.rsrc.o,$(RCFILES_APP)) endif diff --git a/frontend/src/main.c b/frontend/src/main.c index 5d306c4..764ebee 100644 --- a/frontend/src/main.c +++ b/frontend/src/main.c @@ -693,3 +693,14 @@ exiting: return result; } + +#if defined(_WIN32) && defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR) +void __wgetmainargs(int*, wchar_t***, wchar_t***, int, int*); +int main() +{ + wchar_t** enpv, ** argv; + int argc, si = 0; + __wgetmainargs(&argc, &argv, &enpv, 1, &si); + return wmain(argc, argv); +} +#endif diff --git a/frontend/src/platform.h b/frontend/src/platform.h index 450e9ea..3a63fe3 100644 --- a/frontend/src/platform.h +++ b/frontend/src/platform.h @@ -11,6 +11,9 @@ #include #ifdef _WIN32 #include +#ifndef _SH_SECURE +#define _SH_SECURE 0x80 +#endif #endif #ifdef _WIN32 @@ -64,7 +67,7 @@ #define FPUTS(X,Y) fputws((X),(Y)) #define FPRINTF(X,Y,...) fwprintf((X),(Y),__VA_ARGS__) #define FOPEN(X,Y) _wfsopen((X),(Y),_SH_SECURE) -#ifdef __MINGW32__ +#ifdef __USE_MINGW_ANSI_STDIO #define PRISTR "ls" #define PRIstr "hs" #define PRIwcs "ls" diff --git a/frontend/src/utils.c b/frontend/src/utils.c index c5ffea2..9b6346f 100644 --- a/frontend/src/utils.c +++ b/frontend/src/utils.c @@ -11,6 +11,7 @@ #endif #include "utils.h" +#include #include #include #include @@ -25,6 +26,9 @@ #define S_IFMT _S_IFMT #define S_IFDIR _S_IFDIR #define S_IFIFO _S_IFIFO +#ifndef _O_U8TEXT +#define _O_U8TEXT 0x40000 +#endif extern char *const _acmdln; extern wchar_t *const _wcmdln; #else @@ -44,8 +48,8 @@ void init_terminal(void) SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX); _setmode(_fileno(stdin), _O_BINARY); _setmode(_fileno(stderr), _O_U8TEXT); - if (_acmdln) SecureZeroMemory(_acmdln, strlen(_acmdln) * sizeof(char)); - if (_wcmdln) SecureZeroMemory(_wcmdln, wcslen(_wcmdln) * sizeof(wchar_t)); + if (_acmdln) slunkcrypt_bzero(_acmdln, strlen(_acmdln) * sizeof(char)); + if (_wcmdln) slunkcrypt_bzero(_wcmdln, wcslen(_wcmdln) * sizeof(wchar_t)); #endif } diff --git a/libslunkcrypt/src/internal.c b/libslunkcrypt/src/internal.c index a587c48..c141395 100644 --- a/libslunkcrypt/src/internal.c +++ b/libslunkcrypt/src/internal.c @@ -4,6 +4,7 @@ /******************************************************************************/ #ifdef _WIN32 +#define _WIN32_WINNT 0x0600 #define _CRT_RAND_S 1 #define WIN32_LEAN_AND_MEAN 1 #endif @@ -15,17 +16,9 @@ #else #include #include -#if (defined(__GLIBC__) && defined(__GLIBC_MINOR__) && (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 25)) || (defined(__FreeBSD__) && (__FreeBSD__ >= 12)) -#define HAVE_GENRANDOM_SYSCALL 1 -#include -#else -#undef HAVE_GENRANDOM_SYSCALL -#endif -#if (defined(__GLIBC__) && defined(__GLIBC_MINOR__) && (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 25)) || (defined(__FreeBSD__) && (__FreeBSD__ >= 11)) -#define HAVE_EXPLICIT_BZERO 1 #include -#else -#undef HAVE_EXPLICIT_BZERO +#if (defined(__GLIBC__) && defined(__GLIBC_MINOR__) && (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 25)) || (defined(__FreeBSD__) && (__FreeBSD__ >= 12)) +#include #endif #endif @@ -50,7 +43,7 @@ int slunkcrypt_random_bytes(uint8_t* const buffer, const size_t length) } return 0; #else -#ifdef HAVE_GENRANDOM_SYSCALL +#if (defined(__GLIBC__) && defined(__GLIBC_MINOR__) && (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 25)) || (defined(__FreeBSD__) && (__FreeBSD__ >= 12)) if (getrandom(buffer, length, 0U) >= length) { return 0; @@ -80,13 +73,13 @@ void slunkcrypt_bzero(void* const ptr, const size_t length) { if ((ptr) && (length > 0U)) { -#ifdef _WIN32 +#if defined(_WIN32) && defined(SecureZeroMemory) SecureZeroMemory(ptr, length); #else -#ifdef HAVE_EXPLICIT_BZERO +#if (defined(__GLIBC__) && defined(__GLIBC_MINOR__) && (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 25)) || (defined(__FreeBSD__) && (__FreeBSD__ >= 11)) explicit_bzero(ptr, length); #else - volatile uint8_t* buffer = ptr; + volatile uint8_t *buffer = (volatile uint8_t*)ptr; for (size_t i = 0U; i < length; ++i) { buffer[i] = 0U;