diff options
author | Pali Rohár <pali.rohar@gmail.com> | 2019-04-25 18:18:31 +0200 |
---|---|---|
committer | Pali Rohár <pali.rohar@gmail.com> | 2019-04-25 18:18:31 +0200 |
commit | 04bbf2486da795d02e27833bf98a1bbf0188c42e (patch) | |
tree | 98c192706e67a904683a8a4278967e6f47c363ee | |
parent | 278c782a3205dbe49de4233b3bd1e68687a9998b (diff) | |
download | dlfcn-win32-04bbf2486da795d02e27833bf98a1bbf0188c42e.tar.gz dlfcn-win32-04bbf2486da795d02e27833bf98a1bbf0188c42e.tar.bz2 dlfcn-win32-04bbf2486da795d02e27833bf98a1bbf0188c42e.zip |
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.
-rw-r--r-- | dlfcn.c | 79 |
1 files changed, 10 insertions, 69 deletions
@@ -50,15 +50,6 @@ | |||
50 | #define snprintf sprintf_s | 50 | #define snprintf sprintf_s |
51 | #endif | 51 | #endif |
52 | 52 | ||
53 | #ifdef UNICODE | ||
54 | #include <wchar.h> | ||
55 | #define CHAR wchar_t | ||
56 | #define UNICODE_L(s) L##s | ||
57 | #else | ||
58 | #define CHAR char | ||
59 | #define UNICODE_L(s) s | ||
60 | #endif | ||
61 | |||
62 | /* Note: | 53 | /* Note: |
63 | * MSDN says these functions are not thread-safe. We make no efforts to have | 54 | * MSDN says these functions are not thread-safe. We make no efforts to have |
64 | * any kind of thread safety. | 55 | * any kind of thread safety. |
@@ -142,11 +133,11 @@ static void local_rem( HMODULE hModule ) | |||
142 | * MSDN says the buffer cannot be larger than 64K bytes, so we set it to | 133 | * MSDN says the buffer cannot be larger than 64K bytes, so we set it to |
143 | * the limit. | 134 | * the limit. |
144 | */ | 135 | */ |
145 | static CHAR error_buffer[65535]; | 136 | static char error_buffer[65535]; |
146 | static CHAR *current_error; | 137 | static char *current_error; |
147 | static char dlerror_buffer[65536]; | 138 | static char dlerror_buffer[65536]; |
148 | 139 | ||
149 | static int copy_string( CHAR *dest, int dest_size, const CHAR *src ) | 140 | static int copy_string( char *dest, int dest_size, const char *src ) |
150 | { | 141 | { |
151 | int i = 0; | 142 | int i = 0; |
152 | 143 | ||
@@ -166,7 +157,7 @@ static int copy_string( CHAR *dest, int dest_size, const CHAR *src ) | |||
166 | return i; | 157 | return i; |
167 | } | 158 | } |
168 | 159 | ||
169 | static void save_err_str( const CHAR *str ) | 160 | static void save_err_str( const char *str ) |
170 | { | 161 | { |
171 | DWORD dwMessageId; | 162 | DWORD dwMessageId; |
172 | DWORD pos; | 163 | DWORD pos; |
@@ -179,10 +170,10 @@ static void save_err_str( const CHAR *str ) | |||
179 | /* Format error message to: | 170 | /* Format error message to: |
180 | * "<argument to function that failed>": <Windows localized error message> | 171 | * "<argument to function that failed>": <Windows localized error message> |
181 | */ | 172 | */ |
182 | pos = copy_string( error_buffer, sizeof(error_buffer), UNICODE_L("\"") ); | 173 | pos = copy_string( error_buffer, sizeof(error_buffer), "\"" ); |
183 | pos += copy_string( error_buffer+pos, sizeof(error_buffer)-pos, str ); | 174 | pos += copy_string( error_buffer+pos, sizeof(error_buffer)-pos, str ); |
184 | pos += copy_string( error_buffer+pos, sizeof(error_buffer)-pos, UNICODE_L("\": ") ); | 175 | pos += copy_string( error_buffer+pos, sizeof(error_buffer)-pos, "\": " ); |
185 | pos += FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, NULL, dwMessageId, | 176 | pos += FormatMessageA( FORMAT_MESSAGE_FROM_SYSTEM, NULL, dwMessageId, |
186 | MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ), | 177 | MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ), |
187 | error_buffer+pos, sizeof(error_buffer)-pos, NULL ); | 178 | error_buffer+pos, sizeof(error_buffer)-pos, NULL ); |
188 | 179 | ||
@@ -198,19 +189,9 @@ static void save_err_str( const CHAR *str ) | |||
198 | 189 | ||
199 | static void save_err_ptr_str( const void *ptr ) | 190 | static void save_err_ptr_str( const void *ptr ) |
200 | { | 191 | { |
201 | CHAR ptr_buf[19]; /* 0x<pointer> up to 64 bits. */ | 192 | char ptr_buf[19]; /* 0x<pointer> up to 64 bits. */ |
202 | 193 | ||
203 | #ifdef UNICODE | ||
204 | |||
205 | # if ((defined(_WIN32) || defined(WIN32)) && (defined(_MSC_VER)) ) | ||
206 | swprintf_s( ptr_buf, 19, UNICODE_L("0x%p"), ptr ); | ||
207 | # else | ||
208 | swprintf(ptr_buf, 19, UNICODE_L("0x%p"), ptr); | ||
209 | # endif | ||
210 | |||
211 | #else | ||
212 | snprintf( ptr_buf, 19, "0x%p", ptr ); | 194 | snprintf( ptr_buf, 19, "0x%p", ptr ); |
213 | #endif | ||
214 | 195 | ||
215 | save_err_str( ptr_buf ); | 196 | save_err_str( ptr_buf ); |
216 | } | 197 | } |
@@ -246,7 +227,7 @@ void *dlopen( const char *file, int mode ) | |||
246 | { | 227 | { |
247 | HANDLE hCurrentProc; | 228 | HANDLE hCurrentProc; |
248 | DWORD dwProcModsBefore, dwProcModsAfter; | 229 | DWORD dwProcModsBefore, dwProcModsAfter; |
249 | CHAR lpFileName[MAX_PATH]; | 230 | char lpFileName[MAX_PATH]; |
250 | size_t i; | 231 | size_t i; |
251 | 232 | ||
252 | /* MSDN says backslashes *must* be used instead of forward slashes. */ | 233 | /* MSDN says backslashes *must* be used instead of forward slashes. */ |
@@ -271,7 +252,7 @@ void *dlopen( const char *file, int mode ) | |||
271 | * to UNIX's search paths (start with system folders instead of current | 252 | * to UNIX's search paths (start with system folders instead of current |
272 | * folder). | 253 | * folder). |
273 | */ | 254 | */ |
274 | hModule = LoadLibraryEx(lpFileName, NULL, | 255 | hModule = LoadLibraryExA(lpFileName, NULL, |
275 | LOAD_WITH_ALTERED_SEARCH_PATH ); | 256 | LOAD_WITH_ALTERED_SEARCH_PATH ); |
276 | 257 | ||
277 | if( EnumProcessModules( hCurrentProc, NULL, 0, &dwProcModsAfter ) == 0 ) | 258 | if( EnumProcessModules( hCurrentProc, NULL, 0, &dwProcModsAfter ) == 0 ) |
@@ -331,11 +312,6 @@ void *dlsym( void *handle, const char *name ) | |||
331 | HMODULE hModule; | 312 | HMODULE hModule; |
332 | HANDLE hCurrentProc; | 313 | HANDLE hCurrentProc; |
333 | 314 | ||
334 | #ifdef UNICODE | ||
335 | wchar_t namew[MAX_PATH]; | ||
336 | wmemset(namew, 0, MAX_PATH); | ||
337 | #endif | ||
338 | |||
339 | current_error = NULL; | 315 | current_error = NULL; |
340 | symbol = NULL; | 316 | symbol = NULL; |
341 | hCaller = NULL; | 317 | hCaller = NULL; |
@@ -426,23 +402,7 @@ void *dlsym( void *handle, const char *name ) | |||
426 | end: | 402 | end: |
427 | if( symbol == NULL ) | 403 | if( symbol == NULL ) |
428 | { | 404 | { |
429 | #ifdef UNICODE | ||
430 | size_t converted_chars; | ||
431 | |||
432 | size_t str_len = strlen(name) + 1; | ||
433 | |||
434 | #if ((defined(_WIN32) || defined(WIN32)) && (defined(_MSC_VER)) ) | ||
435 | errno_t err = mbstowcs_s(&converted_chars, namew, str_len, name, str_len); | ||
436 | if (err != 0) | ||
437 | return NULL; | ||
438 | #else | ||
439 | mbstowcs(namew, name, str_len); | ||
440 | #endif | ||
441 | |||
442 | save_err_str( namew ); | ||
443 | #else | ||
444 | save_err_str( name ); | 405 | save_err_str( name ); |
445 | #endif | ||
446 | } | 406 | } |
447 | 407 | ||
448 | // warning C4054: 'type cast' : from function pointer 'FARPROC' to data pointer 'void *' | 408 | // warning C4054: 'type cast' : from function pointer 'FARPROC' to data pointer 'void *' |
@@ -462,26 +422,7 @@ char *dlerror( void ) | |||
462 | return NULL; | 422 | return NULL; |
463 | } | 423 | } |
464 | 424 | ||
465 | #ifdef UNICODE | ||
466 | errno_t err = 0; | ||
467 | size_t converted_chars = 0; | ||
468 | size_t str_len = wcslen(current_error) + 1; | ||
469 | memset(error_pointer, 0, 65535); | ||
470 | |||
471 | # if ((defined(_WIN32) || defined(WIN32)) && (defined(_MSC_VER)) ) | ||
472 | err = wcstombs_s(&converted_chars, | ||
473 | error_pointer, str_len * sizeof(char), | ||
474 | current_error, str_len * sizeof(wchar_t)); | ||
475 | |||
476 | if (err != 0) | ||
477 | return NULL; | ||
478 | # else | ||
479 | wcstombs(error_pointer, current_error, str_len); | ||
480 | # endif | ||
481 | |||
482 | #else | ||
483 | memcpy(error_pointer, current_error, strlen(current_error) + 1); | 425 | memcpy(error_pointer, current_error, strlen(current_error) + 1); |
484 | #endif | ||
485 | 426 | ||
486 | /* POSIX says that invoking dlerror( ) a second time, immediately following | 427 | /* POSIX says that invoking dlerror( ) a second time, immediately following |
487 | * a prior invocation, shall result in NULL being returned. | 428 | * a prior invocation, shall result in NULL being returned. |