1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2010, 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 ***************************************************************************/
22 /***************************************************************************
23 * IDN Implementation using windows native APIs
24 * Pierre Joye <pierre@php.net>
25 ***************************************************************************/
26 #if defined(WIN32) && defined(USE_WIN32_IDN)
28 #ifdef HAVE_NORMALIZATION_H
30 #define __in_ecount(x)
31 #define __out_ecount(x)
32 #include <normalization.h>
36 #define IDN_MAX_LENGTH 255
38 static wchar_t *_curl_win32_UTF8_to_wchar(const char *str_utf8)
40 wchar_t *str_w = NULL;
43 int str_w_len = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS,
44 str_utf8, -1, NULL, 0);
46 str_w = (wchar_t *) malloc(str_w_len * sizeof(wchar_t));
48 if (MultiByteToWideChar(CP_UTF8, 0, str_utf8, -1, str_w,
60 static const char *_curl_win32_wchar_to_UTF8(const wchar_t *str_w)
62 char *str_utf8 = NULL;
65 size_t str_utf8_len = WideCharToMultiByte(CP_UTF8, 0, str_w, -1, NULL,
68 str_utf8 = (char *) malloc(str_utf8_len * sizeof(wchar_t));
70 if (WideCharToMultiByte(CP_UTF8, 0, str_w, -1, str_utf8, str_utf8_len,
72 (void) GetLastError();
73 free((void *)str_utf8);
79 (void) GetLastError();
86 int curl_win32_idn_to_ascii(const char *in, char **out)
88 wchar_t *in_w = _curl_win32_UTF8_to_wchar(in);
90 wchar_t punycode[IDN_MAX_LENGTH];
91 if (IdnToAscii(0, in_w, -1, punycode, IDN_MAX_LENGTH) == 0) {
92 wprintf(L"ERROR %d converting to Punycode\n", GetLastError());
98 *out = (char *)_curl_win32_wchar_to_UTF8(punycode);
106 int curl_win32_ascii_to_idn(const char *in, size_t in_len, char **out_utf8)
109 WCHAR unicode[IDN_MAX_LENGTH];
111 if (IdnToUnicode(0, (wchar_t *)in, -1, unicode, IDN_MAX_LENGTH) == 0) {
112 wprintf(L"ERROR %d converting to Punycode\n", GetLastError());
116 const char *out_utf8 = _curl_win32_wchar_to_UTF8(unicode);
125 typedef int dummy; /* because ISO C forbids an empty translation unit! */