1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at http://curl.haxx.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
24 * IDN conversions using Windows kernel32 and normaliz libraries.
33 #ifdef WANT_IDN_PROTOTYPES
34 WINBASEAPI int WINAPI IdnToAscii(DWORD, LPCWSTR, int, LPWSTR, int);
35 WINBASEAPI int WINAPI IdnToUnicode(DWORD, LPCWSTR, int, LPWSTR, int);
38 #define IDN_MAX_LENGTH 255
40 static wchar_t *_curl_win32_UTF8_to_wchar(const char *str_utf8)
42 wchar_t *str_w = NULL;
45 int str_w_len = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS,
46 str_utf8, -1, NULL, 0);
48 str_w = malloc(str_w_len * sizeof(wchar_t));
50 if(MultiByteToWideChar(CP_UTF8, 0, str_utf8, -1, str_w,
62 static const char *_curl_win32_wchar_to_UTF8(const wchar_t *str_w)
64 char *str_utf8 = NULL;
67 size_t str_utf8_len = WideCharToMultiByte(CP_UTF8, 0, str_w, -1, NULL,
70 str_utf8 = malloc(str_utf8_len * sizeof(wchar_t));
72 if(WideCharToMultiByte(CP_UTF8, 0, str_w, -1, str_utf8, str_utf8_len,
74 (void) GetLastError();
75 free((void *)str_utf8);
81 (void) GetLastError();
88 int curl_win32_idn_to_ascii(const char *in, char **out)
90 wchar_t *in_w = _curl_win32_UTF8_to_wchar(in);
92 wchar_t punycode[IDN_MAX_LENGTH];
93 if(IdnToAscii(0, in_w, -1, punycode, IDN_MAX_LENGTH) == 0) {
94 wprintf(L"ERROR %d converting to Punycode\n", GetLastError());
100 *out = (char *)_curl_win32_wchar_to_UTF8(punycode);
108 int curl_win32_ascii_to_idn(const char *in, size_t in_len, char **out_utf8)
111 WCHAR unicode[IDN_MAX_LENGTH];
113 if(IdnToUnicode(0, (wchar_t *)in, -1, unicode, IDN_MAX_LENGTH) == 0) {
114 wprintf(L"ERROR %d converting to Punycode\n", GetLastError());
118 const char *out_utf8 = _curl_win32_wchar_to_UTF8(unicode);
127 #endif /* USE_WIN32_IDN */