Commit 84295c9afc0b42a61cf687d54c3bd5e286fd5176

Silvio Traversaro 2019-05-06T21:02:00

Merge pull request #50 from pali/master Simplify UNICODE build

diff --git a/.appveyor.yml b/.appveyor.yml
index c269f59..fb6c5da 100644
--- a/.appveyor.yml
+++ b/.appveyor.yml
@@ -34,22 +34,52 @@ environment:
     - 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"
@@ -58,24 +88,54 @@ environment:
 
     - 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'
@@ -84,15 +144,37 @@ environment:
     - 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"
@@ -100,15 +182,35 @@ environment:
 
     - 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
diff --git a/dlfcn.c b/dlfcn.c
index 8d3c793..8687c51 100644
--- a/dlfcn.c
+++ b/dlfcn.c
@@ -46,19 +46,6 @@
 #endif
 #include "dlfcn.h"
 
-#if ((defined(_WIN32) || defined(WIN32)) && (defined(_MSC_VER)) )
-#define snprintf sprintf_s
-#endif
-
-#ifdef UNICODE
-#include <wchar.h>
-#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,49 +129,45 @@ 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 )
-{
-    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 )
+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:
      * "<argument to function that failed>": <Windows localized error message>
       */
-    pos  = copy_string( error_buffer,     sizeof(error_buffer),     UNICODE_L("\"") );
-    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 = 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 )
     {
@@ -198,19 +181,13 @@ static void save_err_str( const CHAR *str )
 
 static void save_err_ptr_str( const void *ptr )
 {
-    CHAR ptr_buf[19]; /* 0x<pointer> 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
+    char ptr_buf[19]; /* 0x<pointer> up to 64 bits. */
 
-#else
-    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 );
 }
@@ -246,7 +223,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 +248,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 +308,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 +398,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 +418,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.