2009-12-07 09:23:41 -08:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif /* HAVE_CONFIG_H */
|
|
|
|
|
2008-09-13 14:19:22 -07:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2012-02-22 22:10:36 -08:00
|
|
|
#include <limits.h>
|
2008-09-14 23:56:14 -07:00
|
|
|
#include <wchar.h>
|
2008-09-13 14:19:22 -07:00
|
|
|
|
2019-05-15 04:27:37 -07:00
|
|
|
#include "evil_private.h"
|
2012-02-17 12:48:11 -08:00
|
|
|
|
2017-12-10 22:06:02 -08:00
|
|
|
DWORD _evil_tls_index;
|
2008-09-13 14:19:22 -07:00
|
|
|
|
2015-12-07 20:53:23 -08:00
|
|
|
/* 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 *
|
2008-09-13 14:19:22 -07:00
|
|
|
evil_char_to_wchar(const char *text)
|
|
|
|
{
|
|
|
|
wchar_t *wtext;
|
|
|
|
int wsize;
|
|
|
|
|
2011-11-02 00:22:11 -07:00
|
|
|
if (!text)
|
|
|
|
return NULL;
|
|
|
|
|
2009-01-11 22:03:51 -08:00
|
|
|
wsize = MultiByteToWideChar(CP_ACP, 0, text, (int)strlen(text) + 1, NULL, 0);
|
2008-09-13 14:19:22 -07:00
|
|
|
if ((wsize == 0) ||
|
|
|
|
(wsize > (int)(ULONG_MAX / sizeof(wchar_t))))
|
2011-11-02 00:22:11 -07:00
|
|
|
{
|
|
|
|
if (wsize == 0)
|
2020-05-07 06:27:07 -07:00
|
|
|
_evil_last_error_display(__func__);
|
2011-11-02 00:22:11 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
2008-09-13 14:19:22 -07:00
|
|
|
|
|
|
|
wtext = malloc(wsize * sizeof(wchar_t));
|
|
|
|
if (wtext)
|
2009-01-11 22:03:51 -08:00
|
|
|
if (!MultiByteToWideChar(CP_ACP, 0, text, (int)strlen(text) + 1, wtext, wsize))
|
2011-11-02 00:22:11 -07:00
|
|
|
{
|
2020-05-07 06:27:07 -07:00
|
|
|
_evil_last_error_display(__func__);
|
2011-11-02 00:22:11 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
2008-09-13 14:19:22 -07:00
|
|
|
|
|
|
|
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 *
|
2008-09-13 14:19:22 -07:00
|
|
|
evil_wchar_to_char(const wchar_t *text)
|
|
|
|
{
|
2009-01-11 22:03:51 -08:00
|
|
|
char *atext;
|
2008-09-13 14:19:22 -07:00
|
|
|
int asize;
|
|
|
|
|
2011-11-02 00:22:11 -07:00
|
|
|
if (!text)
|
|
|
|
return NULL;
|
|
|
|
|
2011-10-12 11:50:11 -07:00
|
|
|
asize = WideCharToMultiByte(CP_ACP, 0, text, -1, NULL, 0, NULL, NULL);
|
2008-09-13 14:19:22 -07:00
|
|
|
if (asize == 0)
|
2011-11-02 00:22:11 -07:00
|
|
|
{
|
2020-05-07 06:27:07 -07:00
|
|
|
_evil_last_error_display(__func__);
|
2011-11-02 00:22:11 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
2008-09-13 14:19:22 -07:00
|
|
|
|
2011-10-12 11:50:11 -07:00
|
|
|
atext = (char*)malloc(asize * sizeof(char));
|
|
|
|
if (!atext)
|
|
|
|
return NULL;
|
2008-09-13 14:19:22 -07:00
|
|
|
|
2011-10-12 11:50:11 -07:00
|
|
|
asize = WideCharToMultiByte(CP_ACP, 0, text, -1, atext, asize, NULL, NULL);
|
|
|
|
if (asize == 0)
|
2011-11-02 00:22:11 -07:00
|
|
|
{
|
2020-05-07 06:27:07 -07:00
|
|
|
_evil_last_error_display(__func__);
|
2011-11-02 00:22:11 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
2008-09-13 14:19:22 -07:00
|
|
|
|
|
|
|
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 *
|
2011-11-02 00:22:11 -07:00
|
|
|
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;
|
2011-11-02 00:22:11 -07:00
|
|
|
|
|
|
|
size8 = WideCharToMultiByte(CP_UTF8, flag, text16, -1, NULL, 0, NULL, NULL);
|
|
|
|
if (size8 == 0)
|
|
|
|
{
|
2020-05-07 06:27:07 -07:00
|
|
|
_evil_last_error_display(__func__);
|
2011-11-02 00:22:11 -07:00
|
|
|
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)
|
|
|
|
{
|
2020-05-07 06:27:07 -07:00
|
|
|
_evil_last_error_display(__func__);
|
2011-11-02 00:22:11 -07:00
|
|
|
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 *
|
2020-02-25 06:23:48 -08:00
|
|
|
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)
|
|
|
|
{
|
2020-05-07 06:27:07 -07:00
|
|
|
_evil_last_error_display(__func__);
|
2020-02-25 06:23:48 -08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
text16 = malloc(size16 * sizeof(wchar_t));
|
|
|
|
if (text16)
|
|
|
|
if (!MultiByteToWideChar(CP_UTF8, flag, text, -1, text16, size16))
|
|
|
|
{
|
2020-05-07 06:27:07 -07:00
|
|
|
_evil_last_error_display(__func__);
|
2020-02-25 06:23:48 -08:00
|
|
|
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 *
|
2008-09-13 14:19:22 -07:00
|
|
|
evil_format_message(long err)
|
|
|
|
{
|
2017-12-10 22:06:02 -08:00
|
|
|
char *buf;
|
2008-09-13 14:19:22 -07:00
|
|
|
LPTSTR msg;
|
|
|
|
char *str;
|
|
|
|
|
|
|
|
if (!FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
|
|
NULL,
|
|
|
|
err,
|
2010-04-01 13:13:40 -07:00
|
|
|
0, /* Default language */
|
2008-09-13 14:19:22 -07:00
|
|
|
(LPTSTR)&msg,
|
|
|
|
0,
|
|
|
|
NULL))
|
2008-10-28 01:59:12 -07:00
|
|
|
{
|
2017-12-10 22:06:02 -08:00
|
|
|
buf = (char *)TlsGetValue(_evil_tls_index);
|
|
|
|
snprintf(buf, 4096,
|
|
|
|
"FormatMessage failed with error %ld\n", GetLastError());
|
|
|
|
return (const char *)buf;
|
2008-10-28 01:59:12 -07:00
|
|
|
}
|
2008-09-13 14:19:22 -07:00
|
|
|
|
|
|
|
#ifdef UNICODE
|
|
|
|
str = evil_wchar_to_char(msg);
|
|
|
|
#else
|
2011-09-14 00:44:09 -07:00
|
|
|
str = msg;
|
2008-09-13 14:19:22 -07:00
|
|
|
#endif /* UNICODE */
|
|
|
|
|
2017-12-10 22:06:02 -08:00
|
|
|
buf = (char *)TlsGetValue(_evil_tls_index);
|
|
|
|
snprintf(buf, 4096, "(%5ld) %s", err, str);
|
2008-09-13 14:19:22 -07:00
|
|
|
|
2011-09-14 00:44:09 -07:00
|
|
|
#ifdef UNICODE
|
2008-09-13 14:19:22 -07:00
|
|
|
free(str);
|
2011-09-14 00:44:09 -07:00
|
|
|
#endif /* UNICODE */
|
2008-09-13 14:19:22 -07:00
|
|
|
|
2012-08-27 23:18:53 -07:00
|
|
|
LocalFree(msg);
|
|
|
|
|
2017-12-10 22:06:02 -08:00
|
|
|
return (const char *)buf;
|
2008-09-13 14:19:22 -07:00
|
|
|
}
|
|
|
|
|
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 *
|
2008-09-13 14:19:22 -07:00
|
|
|
evil_last_error_get(void)
|
|
|
|
{
|
|
|
|
DWORD err;
|
|
|
|
|
|
|
|
err = GetLastError();
|
|
|
|
return evil_format_message(err);
|
|
|
|
}
|
|
|
|
|
2015-12-07 20:53:23 -08:00
|
|
|
static void
|
2008-10-29 01:04:52 -07:00
|
|
|
_evil_last_error_display(const char *fct)
|
|
|
|
{
|
2017-12-10 22:06:02 -08:00
|
|
|
fprintf(stderr, "[Evil] [%s] ERROR: %s\n", fct, evil_last_error_get());
|
2008-10-29 01:04:52 -07:00
|
|
|
}
|