Remove unnecessary typecast
[platform/upstream/curl.git] / lib / idn_win32.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2011, 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  * IDN Implementation using windows native APIs
24  * Pierre Joye <pierre@php.net>
25  ***************************************************************************/
26 #if defined(WIN32) && defined(USE_WIN32_IDN)
27 #include <windows.h>
28 #include <stdio.h>
29 #include <tchar.h>
30
31 #ifdef WANT_IDN_PROTOTYPES
32 WINBASEAPI int WINAPI IdnToAscii(DWORD, LPCWSTR, int, LPWSTR, int);
33 WINBASEAPI int WINAPI IdnToUnicode(DWORD, LPCWSTR, int, LPWSTR, int);
34 #endif
35
36 #define IDN_MAX_LENGTH 255
37
38 static wchar_t *_curl_win32_UTF8_to_wchar(const char *str_utf8)
39 {
40   wchar_t *str_w = NULL;
41
42   if(str_utf8) {
43     int str_w_len = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS,
44                                         str_utf8, -1, NULL, 0);
45     if(str_w_len) {
46       str_w = malloc(str_w_len * sizeof(wchar_t));
47       if(str_w) {
48         if(MultiByteToWideChar(CP_UTF8, 0, str_utf8, -1, str_w,
49                                 str_w_len) == 0) {
50           free(str_w);
51           str_w = NULL;
52         }
53       }
54     }
55   }
56
57   return str_w;
58 }
59
60 static const char *_curl_win32_wchar_to_UTF8(const wchar_t *str_w)
61 {
62   char *str_utf8 = NULL;
63
64   if(str_w) {
65     size_t str_utf8_len = WideCharToMultiByte(CP_UTF8, 0, str_w, -1, NULL,
66                                               0, NULL, NULL);
67     if(str_utf8_len) {
68       str_utf8 = malloc(str_utf8_len * sizeof(wchar_t));
69       if(str_utf8) {
70         if(WideCharToMultiByte(CP_UTF8, 0, str_w, -1, str_utf8, str_utf8_len,
71                                 NULL, FALSE) == 0) {
72           (void) GetLastError();
73           free((void *)str_utf8);
74           str_utf8 = NULL;
75         }
76       }
77     }
78     else {
79       (void) GetLastError();
80     }
81   }
82
83   return str_utf8;
84 }
85
86 int curl_win32_idn_to_ascii(const char *in, char **out)
87 {
88   wchar_t *in_w = _curl_win32_UTF8_to_wchar(in);
89   if(in_w) {
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());
93       free(in_w);
94       return 0;
95     }
96     free(in_w);
97
98     *out = (char *)_curl_win32_wchar_to_UTF8(punycode);
99     if(!(*out)) {
100       return 0;
101     }
102   }
103   return 1;
104 }
105
106 int curl_win32_ascii_to_idn(const char *in, size_t in_len, char **out_utf8)
107 {
108   if(in) {
109     WCHAR unicode[IDN_MAX_LENGTH];
110
111     if(IdnToUnicode(0, (wchar_t *)in, -1, unicode, IDN_MAX_LENGTH) == 0) {
112       wprintf(L"ERROR %d converting to Punycode\n", GetLastError());
113       return 0;
114     }
115     else {
116       const char *out_utf8 = _curl_win32_wchar_to_UTF8(unicode);
117       if(!out_utf8) {
118         return 0;
119       }
120     }
121   }
122   return 1;
123 }
124 #else
125 typedef int dummy; /* because ISO C forbids an empty translation unit! */
126 #endif
127  /* WIN32 */