From 04bbf2486da795d02e27833bf98a1bbf0188c42e Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Thu, 25 Apr 2019 18:18:31 +0200 Subject: Simplify code around #ifdef UNICODE The whole dlfcn.h API works with char* (ANSI) strings. For WINAPI UNICODE builds it is still possible to call WINAPI ANSI functions with -A suffix. E.g. LoadLibraryExA() instead of LoadLibraryEx() or FormatMessageA() instead of FormatMessage(). This simplify whole implementation when compiling with UNICODE support as there is no need to do conversion from wchar_t to char and vice-versa. --- dlfcn.c | 79 +++++++++-------------------------------------------------------- 1 file changed, 10 insertions(+), 69 deletions(-) diff --git a/dlfcn.c b/dlfcn.c index 8d3c793..cd1ca83 100644 --- a/dlfcn.c +++ b/dlfcn.c @@ -50,15 +50,6 @@ #define snprintf sprintf_s #endif -#ifdef UNICODE -#include -#define CHAR wchar_t -#define UNICODE_L(s) L##s -#else -#define CHAR char -#define UNICODE_L(s) s -#endif - /* Note: * MSDN says these functions are not thread-safe. We make no efforts to have * any kind of thread safety. @@ -142,11 +133,11 @@ static void local_rem( HMODULE hModule ) * MSDN says the buffer cannot be larger than 64K bytes, so we set it to * the limit. */ -static CHAR error_buffer[65535]; -static CHAR *current_error; +static char error_buffer[65535]; +static char *current_error; static char dlerror_buffer[65536]; -static int copy_string( CHAR *dest, int dest_size, const CHAR *src ) +static int copy_string( char *dest, int dest_size, const char *src ) { int i = 0; @@ -166,7 +157,7 @@ static int copy_string( CHAR *dest, int dest_size, const CHAR *src ) return i; } -static void save_err_str( const CHAR *str ) +static void save_err_str( const char *str ) { DWORD dwMessageId; DWORD pos; @@ -179,10 +170,10 @@ static void save_err_str( const CHAR *str ) /* Format error message to: * "": */ - pos = copy_string( error_buffer, sizeof(error_buffer), UNICODE_L("\"") ); + pos = copy_string( error_buffer, sizeof(error_buffer), "\"" ); pos += copy_string( error_buffer+pos, sizeof(error_buffer)-pos, str ); - pos += copy_string( error_buffer+pos, sizeof(error_buffer)-pos, UNICODE_L("\": ") ); - pos += FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, NULL, dwMessageId, + pos += copy_string( error_buffer+pos, sizeof(error_buffer)-pos, "\": " ); + pos += FormatMessageA( FORMAT_MESSAGE_FROM_SYSTEM, NULL, dwMessageId, MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ), error_buffer+pos, sizeof(error_buffer)-pos, NULL ); @@ -198,19 +189,9 @@ static void save_err_str( const CHAR *str ) static void save_err_ptr_str( const void *ptr ) { - CHAR ptr_buf[19]; /* 0x up to 64 bits. */ + char ptr_buf[19]; /* 0x up to 64 bits. */ -#ifdef UNICODE - -# if ((defined(_WIN32) || defined(WIN32)) && (defined(_MSC_VER)) ) - swprintf_s( ptr_buf, 19, UNICODE_L("0x%p"), ptr ); -# else - swprintf(ptr_buf, 19, UNICODE_L("0x%p"), ptr); -# endif - -#else snprintf( ptr_buf, 19, "0x%p", ptr ); -#endif save_err_str( ptr_buf ); } @@ -246,7 +227,7 @@ void *dlopen( const char *file, int mode ) { HANDLE hCurrentProc; DWORD dwProcModsBefore, dwProcModsAfter; - CHAR lpFileName[MAX_PATH]; + char lpFileName[MAX_PATH]; size_t i; /* MSDN says backslashes *must* be used instead of forward slashes. */ @@ -271,7 +252,7 @@ void *dlopen( const char *file, int mode ) * to UNIX's search paths (start with system folders instead of current * folder). */ - hModule = LoadLibraryEx(lpFileName, NULL, + hModule = LoadLibraryExA(lpFileName, NULL, LOAD_WITH_ALTERED_SEARCH_PATH ); if( EnumProcessModules( hCurrentProc, NULL, 0, &dwProcModsAfter ) == 0 ) @@ -331,11 +312,6 @@ void *dlsym( void *handle, const char *name ) HMODULE hModule; HANDLE hCurrentProc; -#ifdef UNICODE - wchar_t namew[MAX_PATH]; - wmemset(namew, 0, MAX_PATH); -#endif - current_error = NULL; symbol = NULL; hCaller = NULL; @@ -426,23 +402,7 @@ void *dlsym( void *handle, const char *name ) end: if( symbol == NULL ) { -#ifdef UNICODE - size_t converted_chars; - - size_t str_len = strlen(name) + 1; - -#if ((defined(_WIN32) || defined(WIN32)) && (defined(_MSC_VER)) ) - errno_t err = mbstowcs_s(&converted_chars, namew, str_len, name, str_len); - if (err != 0) - return NULL; -#else - mbstowcs(namew, name, str_len); -#endif - - save_err_str( namew ); -#else save_err_str( name ); -#endif } // warning C4054: 'type cast' : from function pointer 'FARPROC' to data pointer 'void *' @@ -462,26 +422,7 @@ char *dlerror( void ) return NULL; } -#ifdef UNICODE - errno_t err = 0; - size_t converted_chars = 0; - size_t str_len = wcslen(current_error) + 1; - memset(error_pointer, 0, 65535); - -# if ((defined(_WIN32) || defined(WIN32)) && (defined(_MSC_VER)) ) - err = wcstombs_s(&converted_chars, - error_pointer, str_len * sizeof(char), - current_error, str_len * sizeof(wchar_t)); - - if (err != 0) - return NULL; -# else - wcstombs(error_pointer, current_error, str_len); -# endif - -#else memcpy(error_pointer, current_error, strlen(current_error) + 1); -#endif /* POSIX says that invoking dlerror( ) a second time, immediately following * a prior invocation, shall result in NULL being returned. -- cgit v1.2.3-55-g6feb From d9d49f2dfc4eb4c22c8d9185348e67352e488f8b Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Thu, 25 Apr 2019 19:26:23 +0200 Subject: Remove ifdef hack for snprintf() Old version of MSVC does not support snprintf() function and sprintf_s() is not replacement for C99 snprintf(). As the only usage of snprintf() is to format void* pointer we can use sprintf() with enough long buffer. --- dlfcn.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/dlfcn.c b/dlfcn.c index cd1ca83..475b069 100644 --- a/dlfcn.c +++ b/dlfcn.c @@ -46,10 +46,6 @@ #endif #include "dlfcn.h" -#if ((defined(_WIN32) || defined(WIN32)) && (defined(_MSC_VER)) ) -#define snprintf sprintf_s -#endif - /* Note: * MSDN says these functions are not thread-safe. We make no efforts to have * any kind of thread safety. @@ -191,7 +187,11 @@ static void save_err_ptr_str( const void *ptr ) { char ptr_buf[19]; /* 0x up to 64 bits. */ - snprintf( ptr_buf, 19, "0x%p", ptr ); +#ifdef _MSC_VER +/* Supress warning C4996: 'sprintf': This function or variable may be unsafe */ +#pragma warning( suppress: 4996 ) +#endif + sprintf( ptr_buf, "0x%p", ptr ); save_err_str( ptr_buf ); } -- cgit v1.2.3-55-g6feb From 83add39260d44a35e9acd2fc40389e7a9a27111f Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Thu, 25 Apr 2019 19:36:57 +0200 Subject: Simplify implementation of save_err_str() Check return value of FormatMessageA() function and remove copy_string() function as it is not needed. --- dlfcn.c | 48 ++++++++++++++++++++++-------------------------- 1 file changed, 22 insertions(+), 26 deletions(-) diff --git a/dlfcn.c b/dlfcn.c index 475b069..8687c51 100644 --- a/dlfcn.c +++ b/dlfcn.c @@ -133,45 +133,41 @@ static char error_buffer[65535]; static char *current_error; static char dlerror_buffer[65536]; -static int copy_string( char *dest, int dest_size, const char *src ) -{ - int i = 0; - - /* gcc should optimize this out */ - if( !src || !dest ) - return 0; - - for( i = 0 ; i < dest_size-1 ; i++ ) - { - if( !src[i] ) - break; - else - dest[i] = src[i]; - } - dest[i] = '\0'; - - return i; -} - static void save_err_str( const char *str ) { DWORD dwMessageId; - DWORD pos; + DWORD ret; + size_t pos, len; dwMessageId = GetLastError( ); if( dwMessageId == 0 ) return; + len = strlen( str ); + if( len > sizeof( error_buffer ) - 5 ) + len = sizeof( error_buffer ) - 5; + /* Format error message to: * "": */ - pos = copy_string( error_buffer, sizeof(error_buffer), "\"" ); - pos += copy_string( error_buffer+pos, sizeof(error_buffer)-pos, str ); - pos += copy_string( error_buffer+pos, sizeof(error_buffer)-pos, "\": " ); - pos += FormatMessageA( FORMAT_MESSAGE_FROM_SYSTEM, NULL, dwMessageId, + pos = 0; + error_buffer[pos++] = '"'; + memcpy( error_buffer+pos, str, len ); + pos += len; + error_buffer[pos++] = '"'; + error_buffer[pos++] = ':'; + error_buffer[pos++] = ' '; + + ret = FormatMessageA( FORMAT_MESSAGE_FROM_SYSTEM, NULL, dwMessageId, MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ), - error_buffer+pos, sizeof(error_buffer)-pos, NULL ); + error_buffer+pos, (DWORD) (sizeof(error_buffer)-pos), NULL ); + pos += ret; + + /* When FormatMessageA() fails it returns zero and does not touch buffer + * so add trailing null byte */ + if( ret == 0 ) + error_buffer[pos] = '\0'; if( pos > 1 ) { -- cgit v1.2.3-55-g6feb From e0a5832163960d3eb95fc0514be1bd99e51caf0c Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Thu, 25 Apr 2019 20:01:34 +0200 Subject: Test also UNICODE builds on AppVeyor CI --- .appveyor.yml | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) diff --git a/.appveyor.yml b/.appveyor.yml index c269f59..fb6c5da 100644 --- a/.appveyor.yml +++ b/.appveyor.yml @@ -31,84 +31,186 @@ environment: cc_path: 'C:\MinGW\bin' BUILD_SHARED_LIBS: ON + - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015 + GENERATOR: "MinGW Makefiles" + cc_path: 'C:\MinGW\bin' + BUILD_SHARED_LIBS: ON + CFLAGS: '-DUNICODE -D_UNICODE' + - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015 GENERATOR: "MinGW Makefiles" cc_path: 'C:\MinGW\bin' BUILD_SHARED_LIBS: OFF + - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015 + GENERATOR: "MinGW Makefiles" + cc_path: 'C:\MinGW\bin' + BUILD_SHARED_LIBS: OFF + CFLAGS: '-DUNICODE -D_UNICODE' + - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015 GENERATOR: "MinGW Makefiles" cc_path: 'C:\mingw-w64\i686-5.3.0-posix-dwarf-rt_v4-rev0\mingw32\bin' BUILD_SHARED_LIBS: ON + - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015 + GENERATOR: "MinGW Makefiles" + cc_path: 'C:\mingw-w64\i686-5.3.0-posix-dwarf-rt_v4-rev0\mingw32\bin' + BUILD_SHARED_LIBS: ON + CFLAGS: '-DUNICODE -D_UNICODE' + + - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015 + GENERATOR: "MinGW Makefiles" + cc_path: 'C:\mingw-w64\i686-5.3.0-posix-dwarf-rt_v4-rev0\mingw32\bin' + BUILD_SHARED_LIBS: OFF + - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015 GENERATOR: "MinGW Makefiles" cc_path: 'C:\mingw-w64\i686-5.3.0-posix-dwarf-rt_v4-rev0\mingw32\bin' BUILD_SHARED_LIBS: OFF + CFLAGS: '-DUNICODE -D_UNICODE' + + - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015 + GENERATOR: "MinGW Makefiles" + cc_path: 'C:\mingw-w64\i686-6.3.0-posix-dwarf-rt_v5-rev1\mingw32\bin' + BUILD_SHARED_LIBS: ON - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015 GENERATOR: "MinGW Makefiles" cc_path: 'C:\mingw-w64\i686-6.3.0-posix-dwarf-rt_v5-rev1\mingw32\bin' BUILD_SHARED_LIBS: ON + CFLAGS: '-DUNICODE -D_UNICODE' - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015 GENERATOR: "MinGW Makefiles" cc_path: 'C:\mingw-w64\i686-6.3.0-posix-dwarf-rt_v5-rev1\mingw32\bin' BUILD_SHARED_LIBS: OFF + - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015 + GENERATOR: "MinGW Makefiles" + cc_path: 'C:\mingw-w64\i686-6.3.0-posix-dwarf-rt_v5-rev1\mingw32\bin' + BUILD_SHARED_LIBS: OFF + CFLAGS: '-DUNICODE -D_UNICODE' + - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015 GENERATOR: "MinGW Makefiles" cc_path: 'C:\mingw-w64\x86_64-6.3.0-posix-seh-rt_v5-rev1\mingw64\bin' BUILD_SHARED_LIBS: ON + - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015 + GENERATOR: "MinGW Makefiles" + cc_path: 'C:\mingw-w64\x86_64-6.3.0-posix-seh-rt_v5-rev1\mingw64\bin' + BUILD_SHARED_LIBS: ON + CFLAGS: '-DUNICODE -D_UNICODE' + + - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015 + GENERATOR: "MinGW Makefiles" + cc_path: 'C:\mingw-w64\x86_64-6.3.0-posix-seh-rt_v5-rev1\mingw64\bin' + BUILD_SHARED_LIBS: OFF + - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015 GENERATOR: "MinGW Makefiles" cc_path: 'C:\mingw-w64\x86_64-6.3.0-posix-seh-rt_v5-rev1\mingw64\bin' BUILD_SHARED_LIBS: OFF + CFLAGS: '-DUNICODE -D_UNICODE' + + - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015 + GENERATOR: "MinGW Makefiles" + cc_path: 'C:\mingw-w64\x86_64-7.3.0-posix-seh-rt_v5-rev0\mingw64\bin' + BUILD_SHARED_LIBS: ON - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015 GENERATOR: "MinGW Makefiles" cc_path: 'C:\mingw-w64\x86_64-7.3.0-posix-seh-rt_v5-rev0\mingw64\bin' BUILD_SHARED_LIBS: ON + CFLAGS: '-DUNICODE -D_UNICODE' - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015 GENERATOR: "MinGW Makefiles" cc_path: 'C:\mingw-w64\x86_64-7.3.0-posix-seh-rt_v5-rev0\mingw64\bin' BUILD_SHARED_LIBS: OFF + - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015 + GENERATOR: "MinGW Makefiles" + cc_path: 'C:\mingw-w64\x86_64-7.3.0-posix-seh-rt_v5-rev0\mingw64\bin' + BUILD_SHARED_LIBS: OFF + CFLAGS: '-DUNICODE -D_UNICODE' + - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017 GENERATOR: "MinGW Makefiles" cc_path: 'C:\mingw-w64\x86_64-7.3.0-posix-seh-rt_v5-rev0\mingw64\bin' BUILD_SHARED_LIBS: ON + - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017 + GENERATOR: "MinGW Makefiles" + cc_path: 'C:\mingw-w64\x86_64-7.3.0-posix-seh-rt_v5-rev0\mingw64\bin' + BUILD_SHARED_LIBS: ON + CFLAGS: '-DUNICODE -D_UNICODE' + + - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017 + GENERATOR: "MinGW Makefiles" + cc_path: 'C:\mingw-w64\x86_64-7.3.0-posix-seh-rt_v5-rev0\mingw64\bin' + BUILD_SHARED_LIBS: OFF + - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017 GENERATOR: "MinGW Makefiles" cc_path: 'C:\mingw-w64\x86_64-7.3.0-posix-seh-rt_v5-rev0\mingw64\bin' BUILD_SHARED_LIBS: OFF + CFLAGS: '-DUNICODE -D_UNICODE' + + - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017 + GENERATOR: "Visual Studio 15 2017" + BUILD_SHARED_LIBS: ON - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017 GENERATOR: "Visual Studio 15 2017" BUILD_SHARED_LIBS: ON + CFLAGS: '/DUNICODE /D_UNICODE' + + - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017 + GENERATOR: "Visual Studio 15 2017" + BUILD_SHARED_LIBS: OFF - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017 GENERATOR: "Visual Studio 15 2017" BUILD_SHARED_LIBS: OFF + CFLAGS: '/DUNICODE /D_UNICODE' - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015 GENERATOR: "Visual Studio 14 2015" BUILD_SHARED_LIBS: ON + - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015 + GENERATOR: "Visual Studio 14 2015" + BUILD_SHARED_LIBS: ON + CFLAGS: '/DUNICODE /D_UNICODE' + - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015 GENERATOR: "Visual Studio 14 2015" BUILD_SHARED_LIBS: OFF + - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015 + GENERATOR: "Visual Studio 14 2015" + BUILD_SHARED_LIBS: OFF + CFLAGS: '/DUNICODE /D_UNICODE' + + - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015 + GENERATOR: "Visual Studio 12 2013" + BUILD_SHARED_LIBS: ON + - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015 GENERATOR: "Visual Studio 12 2013" BUILD_SHARED_LIBS: ON + CFLAGS: '/DUNICODE /D_UNICODE' + + - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015 + GENERATOR: "Visual Studio 12 2013" + BUILD_SHARED_LIBS: OFF - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015 GENERATOR: "Visual Studio 12 2013" BUILD_SHARED_LIBS: OFF + CFLAGS: '/DUNICODE /D_UNICODE' platform: - Win32 -- cgit v1.2.3-55-g6feb