SSPI related code: Unicode support for WinCE
[platform/upstream/curl.git] / lib / idn_win32.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2012, Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
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.
13  *
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.
17  *
18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19  * KIND, either express or implied.
20  *
21  ***************************************************************************/
22
23  /*
24   * IDN conversions using Windows kernel32 and normaliz libraries.
25   */
26
27 #include "setup.h"
28
29 #ifdef USE_WIN32_IDN
30
31 #include "curl_multibyte.h"
32
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);
36 #endif
37
38 #define IDN_MAX_LENGTH 255
39
40 int curl_win32_idn_to_ascii(const char *in, char **out)
41 {
42   wchar_t *in_w = Curl_convert_UTF8_to_wchar(in);
43   if(in_w) {
44     wchar_t punycode[IDN_MAX_LENGTH];
45     if(IdnToAscii(0, in_w, -1, punycode, IDN_MAX_LENGTH) == 0) {
46       wprintf(L"ERROR %d converting to Punycode\n", GetLastError());
47       free(in_w);
48       return 0;
49     }
50     free(in_w);
51
52     *out = (char *)Curl_convert_wchar_to_UTF8(punycode);
53     if(!(*out)) {
54       return 0;
55     }
56   }
57   return 1;
58 }
59
60 int curl_win32_ascii_to_idn(const char *in, size_t in_len, char **out_utf8)
61 {
62   if(in) {
63     WCHAR unicode[IDN_MAX_LENGTH];
64
65     if(IdnToUnicode(0, (wchar_t *)in, -1, unicode, IDN_MAX_LENGTH) == 0) {
66       wprintf(L"ERROR %d converting to Punycode\n", GetLastError());
67       return 0;
68     }
69     else {
70       const char *out_utf8 = Curl_convert_wchar_to_UTF8(unicode);
71       if(!out_utf8) {
72         return 0;
73       }
74     }
75   }
76   return 1;
77 }
78
79 #endif /* USE_WIN32_IDN */