efl/src/lib/evil/evil_util.c

191 lines
3.8 KiB
C
Raw Normal View History

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif /* HAVE_CONFIG_H */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <wchar.h>
#include "evil_private.h"
* AUTHORS: * src/lib/Evil.h: * src/lib/Makefile.am: * src/lib/evil_inet.c: * src/lib/evil_mman.c: * src/lib/evil_stdio.c: * src/lib/evil_stdio.h: * src/lib/evil_stdlib.c: * src/lib/evil_util.c: * src/lib/sys/mman.h: * src/lib/evil_printa.c (added): * src/lib/evil_pformatw.c (added): * src/lib/evil_pformat.h (added): * src/lib/evil_printw.c (added): * src/lib/evil_print.h (added): * src/lib/evil_macro.h (added): * src/lib/evil_pformata.c (added): Add POSIX printf family. Code taken from the MinGW-w64 project and modified to be integrated into Evil. * src/bin/Makefile.am: * src/bin/evil_suite.c: * src/bin/evil_test_util.h (added): * src/bin/evil_test_print.c (added): * src/bin/evil_test_print.h (added): * src/bin/evil_test_util.c (added): Add util and printf unit tests * src/lib/evil_errno.c: * src/lib/errno.h (deleted): * src/lib/mingw32ce (added): * src/lib/mingw32ce/errno.h (added): Move errno.h for Windows CE in its own directory to suppress conflicts with standard errno.h when compiling for Windows XP. * src/lib/dlfcn/dlfcn.c: * src/lib/evil_link_ce.c: * src/lib/evil_main.c: * src/lib/evil_unistd.c: Define WIN32_LEAN_AND_MEAN only if it's not defined. * src/lib/evil_fcntl.c: Remove debug. * src/bin/evil_test_dlfcn.c: * src/bin/evil_test_environment.c: * src/bin/evil_test_gettimeofday.c: * src/bin/evil_test_link.c: * src/bin/evil_test_mkstemp.c: * src/bin/evil_test_pipe.c: * src/bin/evil_test_realpath.c: Remove warnings. * src/lib/evil_link_xp.cpp: Formatting. SVN revision: 68084
2012-02-17 12:48:11 -08:00
DWORD _evil_tls_index;
/* static void _evil_error_display(const char *fct, LONG res); */
static void _evil_last_error_display(const char *fct);
evil: Rename EAPI macro to EVIL_API in Evil library Summary: Patch from a series of patches to rename EAPI symbols to specific library DSOs. EAPI was designed to be able to pass ```__attribute__ ((visibility ("default")))``` for symbols with GCC, which would mean that even if -fvisibility=hidden was used when compiling the library, the needed symbols would get exported. MSVC __almost__ works like GCC (or mingw) in which you can declare everything as export and it will just work (slower, but it will work). But there's a caveat: global variables will not work the same way for MSVC, but works for mingw and GCC. For global variables (as opposed to functions), MSVC requires correct DSO visibility for MSVC: instead of declaring a symbol as export for everything, you need to declare it as import when importing from another DSO and export when defining it locally. With current EAPI definitions, we get the following example working in mingw and MSVC (observe it doesn't define any global variables as exported symbols). ``` Example 1: dll1: ``` EAPI void foo(void); EAPI void bar() { foo(); } ``` dll2: ``` EAPI void foo() { printf ("foo\n"); } ``` This works fine with API defined as __declspec(dllexport) in both cases and for gcc defining as ```__atttribute__((visibility("default")))``` However, the following: Example 2: dll1: ``` EAPI extern int foo; EAPI void foobar(void); EAPI void bar() { foo = 5; foobar(); } ``` dll2: ``` EAPI int foo = 0; EAPI void foobar() { printf ("foo %d\n", foo); } ``` This will work on mingw but will not work for MSVC. And that's why EAPI is the only solution that worked for MSVC. Co-authored-by: João Paulo Taylor Ienczak Zanette <jpaulotiz@gmail.com> Co-authored-by: Ricardo Campos <ricardo.campos@expertise.dev> Co-authored-by: Lucas Cavalcante de Sousa <lucks.sousa@gmail.com> Reviewers: raster, vtorri, jptiz, lucas, woohyun Reviewed By: vtorri, jptiz Subscribers: ProhtMeyhet, cedric, #reviewers, #committers Tags: #efl Differential Revision: https://phab.enlightenment.org/D12182
2020-11-12 08:47:38 -08:00
EVIL_API wchar_t *
evil_char_to_wchar(const char *text)
{
wchar_t *wtext;
int wsize;
if (!text)
return NULL;
wsize = MultiByteToWideChar(CP_ACP, 0, text, (int)strlen(text) + 1, NULL, 0);
if ((wsize == 0) ||
(wsize > (int)(ULONG_MAX / sizeof(wchar_t))))
{
if (wsize == 0)
_evil_last_error_display(__func__);
return NULL;
}
wtext = malloc(wsize * sizeof(wchar_t));
if (wtext)
if (!MultiByteToWideChar(CP_ACP, 0, text, (int)strlen(text) + 1, wtext, wsize))
{
_evil_last_error_display(__func__);
return NULL;
}
return wtext;
}
evil: Rename EAPI macro to EVIL_API in Evil library Summary: Patch from a series of patches to rename EAPI symbols to specific library DSOs. EAPI was designed to be able to pass ```__attribute__ ((visibility ("default")))``` for symbols with GCC, which would mean that even if -fvisibility=hidden was used when compiling the library, the needed symbols would get exported. MSVC __almost__ works like GCC (or mingw) in which you can declare everything as export and it will just work (slower, but it will work). But there's a caveat: global variables will not work the same way for MSVC, but works for mingw and GCC. For global variables (as opposed to functions), MSVC requires correct DSO visibility for MSVC: instead of declaring a symbol as export for everything, you need to declare it as import when importing from another DSO and export when defining it locally. With current EAPI definitions, we get the following example working in mingw and MSVC (observe it doesn't define any global variables as exported symbols). ``` Example 1: dll1: ``` EAPI void foo(void); EAPI void bar() { foo(); } ``` dll2: ``` EAPI void foo() { printf ("foo\n"); } ``` This works fine with API defined as __declspec(dllexport) in both cases and for gcc defining as ```__atttribute__((visibility("default")))``` However, the following: Example 2: dll1: ``` EAPI extern int foo; EAPI void foobar(void); EAPI void bar() { foo = 5; foobar(); } ``` dll2: ``` EAPI int foo = 0; EAPI void foobar() { printf ("foo %d\n", foo); } ``` This will work on mingw but will not work for MSVC. And that's why EAPI is the only solution that worked for MSVC. Co-authored-by: João Paulo Taylor Ienczak Zanette <jpaulotiz@gmail.com> Co-authored-by: Ricardo Campos <ricardo.campos@expertise.dev> Co-authored-by: Lucas Cavalcante de Sousa <lucks.sousa@gmail.com> Reviewers: raster, vtorri, jptiz, lucas, woohyun Reviewed By: vtorri, jptiz Subscribers: ProhtMeyhet, cedric, #reviewers, #committers Tags: #efl Differential Revision: https://phab.enlightenment.org/D12182
2020-11-12 08:47:38 -08:00
EVIL_API char *
evil_wchar_to_char(const wchar_t *text)
{
char *atext;
int asize;
if (!text)
return NULL;
asize = WideCharToMultiByte(CP_ACP, 0, text, -1, NULL, 0, NULL, NULL);
if (asize == 0)
{
_evil_last_error_display(__func__);
return NULL;
}
atext = (char*)malloc(asize * sizeof(char));
if (!atext)
return NULL;
asize = WideCharToMultiByte(CP_ACP, 0, text, -1, atext, asize, NULL, NULL);
if (asize == 0)
{
_evil_last_error_display(__func__);
return NULL;
}
return atext;
}
evil: Rename EAPI macro to EVIL_API in Evil library Summary: Patch from a series of patches to rename EAPI symbols to specific library DSOs. EAPI was designed to be able to pass ```__attribute__ ((visibility ("default")))``` for symbols with GCC, which would mean that even if -fvisibility=hidden was used when compiling the library, the needed symbols would get exported. MSVC __almost__ works like GCC (or mingw) in which you can declare everything as export and it will just work (slower, but it will work). But there's a caveat: global variables will not work the same way for MSVC, but works for mingw and GCC. For global variables (as opposed to functions), MSVC requires correct DSO visibility for MSVC: instead of declaring a symbol as export for everything, you need to declare it as import when importing from another DSO and export when defining it locally. With current EAPI definitions, we get the following example working in mingw and MSVC (observe it doesn't define any global variables as exported symbols). ``` Example 1: dll1: ``` EAPI void foo(void); EAPI void bar() { foo(); } ``` dll2: ``` EAPI void foo() { printf ("foo\n"); } ``` This works fine with API defined as __declspec(dllexport) in both cases and for gcc defining as ```__atttribute__((visibility("default")))``` However, the following: Example 2: dll1: ``` EAPI extern int foo; EAPI void foobar(void); EAPI void bar() { foo = 5; foobar(); } ``` dll2: ``` EAPI int foo = 0; EAPI void foobar() { printf ("foo %d\n", foo); } ``` This will work on mingw but will not work for MSVC. And that's why EAPI is the only solution that worked for MSVC. Co-authored-by: João Paulo Taylor Ienczak Zanette <jpaulotiz@gmail.com> Co-authored-by: Ricardo Campos <ricardo.campos@expertise.dev> Co-authored-by: Lucas Cavalcante de Sousa <lucks.sousa@gmail.com> Reviewers: raster, vtorri, jptiz, lucas, woohyun Reviewed By: vtorri, jptiz Subscribers: ProhtMeyhet, cedric, #reviewers, #committers Tags: #efl Differential Revision: https://phab.enlightenment.org/D12182
2020-11-12 08:47:38 -08:00
EVIL_API char *
evil_utf16_to_utf8(const wchar_t *text16)
{
char *text8;
DWORD flag = 0;
int size8;
if (!text16)
return NULL;
2013-06-20 04:08:36 -07:00
flag = WC_ERR_INVALID_CHARS;
size8 = WideCharToMultiByte(CP_UTF8, flag, text16, -1, NULL, 0, NULL, NULL);
if (size8 == 0)
{
_evil_last_error_display(__func__);
return NULL;
}
text8 = (char*)malloc(size8 * sizeof(char));
if (!text8)
return NULL;
size8 = WideCharToMultiByte(CP_UTF8, flag, text16, -1, text8, size8, NULL, NULL);
if (size8 == 0)
{
_evil_last_error_display(__func__);
return NULL;
}
return text8;
}
evil: Rename EAPI macro to EVIL_API in Evil library Summary: Patch from a series of patches to rename EAPI symbols to specific library DSOs. EAPI was designed to be able to pass ```__attribute__ ((visibility ("default")))``` for symbols with GCC, which would mean that even if -fvisibility=hidden was used when compiling the library, the needed symbols would get exported. MSVC __almost__ works like GCC (or mingw) in which you can declare everything as export and it will just work (slower, but it will work). But there's a caveat: global variables will not work the same way for MSVC, but works for mingw and GCC. For global variables (as opposed to functions), MSVC requires correct DSO visibility for MSVC: instead of declaring a symbol as export for everything, you need to declare it as import when importing from another DSO and export when defining it locally. With current EAPI definitions, we get the following example working in mingw and MSVC (observe it doesn't define any global variables as exported symbols). ``` Example 1: dll1: ``` EAPI void foo(void); EAPI void bar() { foo(); } ``` dll2: ``` EAPI void foo() { printf ("foo\n"); } ``` This works fine with API defined as __declspec(dllexport) in both cases and for gcc defining as ```__atttribute__((visibility("default")))``` However, the following: Example 2: dll1: ``` EAPI extern int foo; EAPI void foobar(void); EAPI void bar() { foo = 5; foobar(); } ``` dll2: ``` EAPI int foo = 0; EAPI void foobar() { printf ("foo %d\n", foo); } ``` This will work on mingw but will not work for MSVC. And that's why EAPI is the only solution that worked for MSVC. Co-authored-by: João Paulo Taylor Ienczak Zanette <jpaulotiz@gmail.com> Co-authored-by: Ricardo Campos <ricardo.campos@expertise.dev> Co-authored-by: Lucas Cavalcante de Sousa <lucks.sousa@gmail.com> Reviewers: raster, vtorri, jptiz, lucas, woohyun Reviewed By: vtorri, jptiz Subscribers: ProhtMeyhet, cedric, #reviewers, #committers Tags: #efl Differential Revision: https://phab.enlightenment.org/D12182
2020-11-12 08:47:38 -08:00
EVIL_API wchar_t *
evil_utf8_to_utf16(const char *text)
{
wchar_t *text16;
DWORD flag = MB_ERR_INVALID_CHARS;
int size16;
if (!text)
return NULL;
size16 = MultiByteToWideChar(CP_UTF8, flag, text, -1, NULL, 0);
if (size16 == 0)
{
_evil_last_error_display(__func__);
return NULL;
}
text16 = malloc(size16 * sizeof(wchar_t));
if (text16)
if (!MultiByteToWideChar(CP_UTF8, flag, text, -1, text16, size16))
{
_evil_last_error_display(__func__);
return NULL;
}
return text16;
}
evil: Rename EAPI macro to EVIL_API in Evil library Summary: Patch from a series of patches to rename EAPI symbols to specific library DSOs. EAPI was designed to be able to pass ```__attribute__ ((visibility ("default")))``` for symbols with GCC, which would mean that even if -fvisibility=hidden was used when compiling the library, the needed symbols would get exported. MSVC __almost__ works like GCC (or mingw) in which you can declare everything as export and it will just work (slower, but it will work). But there's a caveat: global variables will not work the same way for MSVC, but works for mingw and GCC. For global variables (as opposed to functions), MSVC requires correct DSO visibility for MSVC: instead of declaring a symbol as export for everything, you need to declare it as import when importing from another DSO and export when defining it locally. With current EAPI definitions, we get the following example working in mingw and MSVC (observe it doesn't define any global variables as exported symbols). ``` Example 1: dll1: ``` EAPI void foo(void); EAPI void bar() { foo(); } ``` dll2: ``` EAPI void foo() { printf ("foo\n"); } ``` This works fine with API defined as __declspec(dllexport) in both cases and for gcc defining as ```__atttribute__((visibility("default")))``` However, the following: Example 2: dll1: ``` EAPI extern int foo; EAPI void foobar(void); EAPI void bar() { foo = 5; foobar(); } ``` dll2: ``` EAPI int foo = 0; EAPI void foobar() { printf ("foo %d\n", foo); } ``` This will work on mingw but will not work for MSVC. And that's why EAPI is the only solution that worked for MSVC. Co-authored-by: João Paulo Taylor Ienczak Zanette <jpaulotiz@gmail.com> Co-authored-by: Ricardo Campos <ricardo.campos@expertise.dev> Co-authored-by: Lucas Cavalcante de Sousa <lucks.sousa@gmail.com> Reviewers: raster, vtorri, jptiz, lucas, woohyun Reviewed By: vtorri, jptiz Subscribers: ProhtMeyhet, cedric, #reviewers, #committers Tags: #efl Differential Revision: https://phab.enlightenment.org/D12182
2020-11-12 08:47:38 -08:00
EVIL_API const char *
evil_format_message(long err)
{
char *buf;
LPTSTR msg;
char *str;
if (!FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
err,
0, /* Default language */
(LPTSTR)&msg,
0,
NULL))
{
buf = (char *)TlsGetValue(_evil_tls_index);
snprintf(buf, 4096,
"FormatMessage failed with error %ld\n", GetLastError());
return (const char *)buf;
}
#ifdef UNICODE
str = evil_wchar_to_char(msg);
#else
str = msg;
#endif /* UNICODE */
buf = (char *)TlsGetValue(_evil_tls_index);
snprintf(buf, 4096, "(%5ld) %s", err, str);
#ifdef UNICODE
free(str);
#endif /* UNICODE */
LocalFree(msg);
return (const char *)buf;
}
evil: Rename EAPI macro to EVIL_API in Evil library Summary: Patch from a series of patches to rename EAPI symbols to specific library DSOs. EAPI was designed to be able to pass ```__attribute__ ((visibility ("default")))``` for symbols with GCC, which would mean that even if -fvisibility=hidden was used when compiling the library, the needed symbols would get exported. MSVC __almost__ works like GCC (or mingw) in which you can declare everything as export and it will just work (slower, but it will work). But there's a caveat: global variables will not work the same way for MSVC, but works for mingw and GCC. For global variables (as opposed to functions), MSVC requires correct DSO visibility for MSVC: instead of declaring a symbol as export for everything, you need to declare it as import when importing from another DSO and export when defining it locally. With current EAPI definitions, we get the following example working in mingw and MSVC (observe it doesn't define any global variables as exported symbols). ``` Example 1: dll1: ``` EAPI void foo(void); EAPI void bar() { foo(); } ``` dll2: ``` EAPI void foo() { printf ("foo\n"); } ``` This works fine with API defined as __declspec(dllexport) in both cases and for gcc defining as ```__atttribute__((visibility("default")))``` However, the following: Example 2: dll1: ``` EAPI extern int foo; EAPI void foobar(void); EAPI void bar() { foo = 5; foobar(); } ``` dll2: ``` EAPI int foo = 0; EAPI void foobar() { printf ("foo %d\n", foo); } ``` This will work on mingw but will not work for MSVC. And that's why EAPI is the only solution that worked for MSVC. Co-authored-by: João Paulo Taylor Ienczak Zanette <jpaulotiz@gmail.com> Co-authored-by: Ricardo Campos <ricardo.campos@expertise.dev> Co-authored-by: Lucas Cavalcante de Sousa <lucks.sousa@gmail.com> Reviewers: raster, vtorri, jptiz, lucas, woohyun Reviewed By: vtorri, jptiz Subscribers: ProhtMeyhet, cedric, #reviewers, #committers Tags: #efl Differential Revision: https://phab.enlightenment.org/D12182
2020-11-12 08:47:38 -08:00
EVIL_API const char *
evil_last_error_get(void)
{
DWORD err;
err = GetLastError();
return evil_format_message(err);
}
static void
_evil_last_error_display(const char *fct)
{
fprintf(stderr, "[Evil] [%s] ERROR: %s\n", fct, evil_last_error_get());
}