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 /* 24 * IDN conversions using Windows kernel32 and normaliz libraries. 25 */ 26 27#include "setup.h" 28 29#ifdef USE_WIN32_IDN 30 31#include <tchar.h> 32 33#ifdef WANT_IDN_PROTOTYPES 34WINBASEAPI int WINAPI IdnToAscii(DWORD, LPCWSTR, int, LPWSTR, int); 35WINBASEAPI int WINAPI IdnToUnicode(DWORD, LPCWSTR, int, LPWSTR, int); 36#endif 37 38#define IDN_MAX_LENGTH 255 39 40static wchar_t *_curl_win32_UTF8_to_wchar(const char *str_utf8) 41{ 42 wchar_t *str_w = NULL; 43 44 if(str_utf8) { 45 int str_w_len = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, 46 str_utf8, -1, NULL, 0); 47 if(str_w_len) { 48 str_w = malloc(str_w_len * sizeof(wchar_t)); 49 if(str_w) { 50 if(MultiByteToWideChar(CP_UTF8, 0, str_utf8, -1, str_w, 51 str_w_len) == 0) { 52 free(str_w); 53 str_w = NULL; 54 } 55 } 56 } 57 } 58 59 return str_w; 60} 61 62static const char *_curl_win32_wchar_to_UTF8(const wchar_t *str_w) 63{ 64 char *str_utf8 = NULL; 65 66 if(str_w) { 67 size_t str_utf8_len = WideCharToMultiByte(CP_UTF8, 0, str_w, -1, NULL, 68 0, NULL, NULL); 69 if(str_utf8_len) { 70 str_utf8 = malloc(str_utf8_len * sizeof(wchar_t)); 71 if(str_utf8) { 72 if(WideCharToMultiByte(CP_UTF8, 0, str_w, -1, str_utf8, str_utf8_len, 73 NULL, FALSE) == 0) { 74 (void) GetLastError(); 75 free((void *)str_utf8); 76 str_utf8 = NULL; 77 } 78 } 79 } 80 else { 81 (void) GetLastError(); 82 } 83 } 84 85 return str_utf8; 86} 87 88int curl_win32_idn_to_ascii(const char *in, char **out) 89{ 90 wchar_t *in_w = _curl_win32_UTF8_to_wchar(in); 91 if(in_w) { 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()); 95 free(in_w); 96 return 0; 97 } 98 free(in_w); 99 100 *out = (char *)_curl_win32_wchar_to_UTF8(punycode); 101 if(!(*out)) { 102 return 0; 103 } 104 } 105 return 1; 106} 107 108int curl_win32_ascii_to_idn(const char *in, size_t in_len, char **out_utf8) 109{ 110 if(in) { 111 WCHAR unicode[IDN_MAX_LENGTH]; 112 113 if(IdnToUnicode(0, (wchar_t *)in, -1, unicode, IDN_MAX_LENGTH) == 0) { 114 wprintf(L"ERROR %d converting to Punycode\n", GetLastError()); 115 return 0; 116 } 117 else { 118 const char *out_utf8 = _curl_win32_wchar_to_UTF8(unicode); 119 if(!out_utf8) { 120 return 0; 121 } 122 } 123 } 124 return 1; 125} 126 127#endif /* USE_WIN32_IDN */ 128