| 1 |
|
| 2 |
#define _WIN32_WINNT 0x0600 |
| 3 |
#define WINVER 0x0600 |
| 4 |
#define IDN_MAX_LENGTH 255 |
| 5 |
|
| 6 |
#ifdef _WIN32 |
| 7 |
#include <Windows.h> |
| 8 |
|
| 9 |
wchar_t * jeroen_convert_UTF8_to_wchar(const char *str_utf8){ |
| 10 |
wchar_t *str_w = NULL; |
| 11 |
|
| 12 |
if(str_utf8) { |
| 13 |
int str_w_len = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, |
| 14 |
str_utf8, -1, NULL, 0); |
| 15 |
if(str_w_len > 0) { |
| 16 |
str_w = malloc(str_w_len * sizeof(wchar_t)); |
| 17 |
if(str_w) { |
| 18 |
if(MultiByteToWideChar(CP_UTF8, 0, str_utf8, -1, str_w, |
| 19 |
str_w_len) == 0) { |
| 20 |
free(str_w); |
| 21 |
return NULL; |
| 22 |
} |
| 23 |
} |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
return str_w; |
| 28 |
} |
| 29 |
|
| 30 |
char *jeroen_convert_wchar_to_UTF8(const wchar_t *str_w){ |
| 31 |
char *str_utf8 = NULL; |
| 32 |
|
| 33 |
if(str_w) { |
| 34 |
int str_utf8_len = WideCharToMultiByte(CP_UTF8, 0, str_w, -1, NULL, |
| 35 |
0, NULL, NULL); |
| 36 |
if(str_utf8_len > 0) { |
| 37 |
str_utf8 = malloc(str_utf8_len * sizeof(wchar_t)); |
| 38 |
if(str_utf8) { |
| 39 |
if(WideCharToMultiByte(CP_UTF8, 0, str_w, -1, str_utf8, str_utf8_len, |
| 40 |
NULL, FALSE) == 0) { |
| 41 |
free(str_utf8); |
| 42 |
return NULL; |
| 43 |
} |
| 44 |
} |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
return str_utf8; |
| 49 |
} |
| 50 |
|
| 51 |
int jeroen_win32_idn_to_ascii(const char *in, char **out){ |
| 52 |
int success = FALSE; |
| 53 |
wchar_t *in_w = jeroen_convert_UTF8_to_wchar(in); |
| 54 |
if(in_w) { |
| 55 |
wchar_t punycode[IDN_MAX_LENGTH]; |
| 56 |
int chars = IdnToAscii(0, in_w, -1, punycode, IDN_MAX_LENGTH); |
| 57 |
free(in_w); |
| 58 |
if(chars) { |
| 59 |
*out = jeroen_convert_wchar_to_UTF8(punycode); |
| 60 |
if(*out) |
| 61 |
success = TRUE; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
return success; |
| 66 |
} |
| 67 |
|
| 68 |
#else |
| 69 |
void placeholder_to_avoid_stupid_warning(void){} |
| 70 |
#endif |