idn.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 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 https://curl.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. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. /*
  25. * IDN conversions
  26. */
  27. #include "curl_setup.h"
  28. #include "urldata.h"
  29. #include "idn.h"
  30. #include "sendf.h"
  31. #include "curl_multibyte.h"
  32. #include "warnless.h"
  33. #ifdef USE_LIBIDN2
  34. #include <idn2.h>
  35. #if defined(WIN32) && defined(UNICODE)
  36. #define IDN2_LOOKUP(name, host, flags) \
  37. idn2_lookup_u8((const uint8_t *)name, (uint8_t **)host, flags)
  38. #else
  39. #define IDN2_LOOKUP(name, host, flags) \
  40. idn2_lookup_ul((const char *)name, (char **)host, flags)
  41. #endif
  42. #endif /* USE_LIBIDN2 */
  43. /* The last 3 #include files should be in this order */
  44. #include "curl_printf.h"
  45. #include "curl_memory.h"
  46. #include "memdebug.h"
  47. #ifdef USE_WIN32_IDN
  48. /* using Windows kernel32 and normaliz libraries. */
  49. #if !defined(_WIN32_WINNT) || _WIN32_WINNT < 0x600
  50. WINBASEAPI int WINAPI IdnToAscii(DWORD dwFlags,
  51. const WCHAR *lpUnicodeCharStr,
  52. int cchUnicodeChar,
  53. WCHAR *lpASCIICharStr,
  54. int cchASCIIChar);
  55. WINBASEAPI int WINAPI IdnToUnicode(DWORD dwFlags,
  56. const WCHAR *lpASCIICharStr,
  57. int cchASCIIChar,
  58. WCHAR *lpUnicodeCharStr,
  59. int cchUnicodeChar);
  60. #endif
  61. #define IDN_MAX_LENGTH 255
  62. static CURLcode win32_idn_to_ascii(const char *in, char **out)
  63. {
  64. wchar_t *in_w = curlx_convert_UTF8_to_wchar(in);
  65. *out = NULL;
  66. if(in_w) {
  67. wchar_t punycode[IDN_MAX_LENGTH];
  68. int chars = IdnToAscii(0, in_w, (int)(wcslen(in_w) + 1), punycode,
  69. IDN_MAX_LENGTH);
  70. curlx_unicodefree(in_w);
  71. if(chars) {
  72. char *mstr = curlx_convert_wchar_to_UTF8(punycode);
  73. if(mstr) {
  74. *out = strdup(mstr);
  75. curlx_unicodefree(mstr);
  76. if(!*out)
  77. return CURLE_OUT_OF_MEMORY;
  78. }
  79. else
  80. return CURLE_OUT_OF_MEMORY;
  81. }
  82. else
  83. return CURLE_URL_MALFORMAT;
  84. }
  85. return CURLE_OK;
  86. }
  87. static CURLcode win32_ascii_to_idn(const char *in, char **output)
  88. {
  89. char *out = NULL;
  90. wchar_t *in_w = curlx_convert_UTF8_to_wchar(in);
  91. if(in_w) {
  92. WCHAR idn[IDN_MAX_LENGTH]; /* stores a UTF-16 string */
  93. int chars = IdnToUnicode(0, in_w, (int)(wcslen(in_w) + 1), idn,
  94. IDN_MAX_LENGTH);
  95. if(chars) {
  96. /* 'chars' is "the number of characters retrieved" */
  97. char *mstr = curlx_convert_wchar_to_UTF8(idn);
  98. if(mstr) {
  99. out = strdup(mstr);
  100. curlx_unicodefree(mstr);
  101. if(!out)
  102. return CURLE_OUT_OF_MEMORY;
  103. }
  104. }
  105. else
  106. return CURLE_URL_MALFORMAT;
  107. }
  108. else
  109. return CURLE_URL_MALFORMAT;
  110. *output = out;
  111. return CURLE_OK;
  112. }
  113. #endif /* USE_WIN32_IDN */
  114. /*
  115. * Helpers for IDNA conversions.
  116. */
  117. bool Curl_is_ASCII_name(const char *hostname)
  118. {
  119. /* get an UNSIGNED local version of the pointer */
  120. const unsigned char *ch = (const unsigned char *)hostname;
  121. if(!hostname) /* bad input, consider it ASCII! */
  122. return TRUE;
  123. while(*ch) {
  124. if(*ch++ & 0x80)
  125. return FALSE;
  126. }
  127. return TRUE;
  128. }
  129. #ifdef USE_IDN
  130. /*
  131. * Curl_idn_decode() returns an allocated IDN decoded string if it was
  132. * possible. NULL on error.
  133. *
  134. * CURLE_URL_MALFORMAT - the host name could not be converted
  135. * CURLE_OUT_OF_MEMORY - memory problem
  136. *
  137. */
  138. static CURLcode idn_decode(const char *input, char **output)
  139. {
  140. char *decoded = NULL;
  141. CURLcode result = CURLE_OK;
  142. #ifdef USE_LIBIDN2
  143. if(idn2_check_version(IDN2_VERSION)) {
  144. int flags = IDN2_NFC_INPUT
  145. #if IDN2_VERSION_NUMBER >= 0x00140000
  146. /* IDN2_NFC_INPUT: Normalize input string using normalization form C.
  147. IDN2_NONTRANSITIONAL: Perform Unicode TR46 non-transitional
  148. processing. */
  149. | IDN2_NONTRANSITIONAL
  150. #endif
  151. ;
  152. int rc = IDN2_LOOKUP(input, &decoded, flags);
  153. if(rc != IDN2_OK)
  154. /* fallback to TR46 Transitional mode for better IDNA2003
  155. compatibility */
  156. rc = IDN2_LOOKUP(input, &decoded, IDN2_TRANSITIONAL);
  157. if(rc != IDN2_OK)
  158. result = CURLE_URL_MALFORMAT;
  159. }
  160. #elif defined(USE_WIN32_IDN)
  161. result = win32_idn_to_ascii(input, &decoded);
  162. #endif
  163. if(!result)
  164. *output = decoded;
  165. return result;
  166. }
  167. static CURLcode idn_encode(const char *puny, char **output)
  168. {
  169. char *enc = NULL;
  170. #ifdef USE_LIBIDN2
  171. int rc = idn2_to_unicode_8z8z(puny, &enc, 0);
  172. if(rc != IDNA_SUCCESS)
  173. return rc == IDNA_MALLOC_ERROR ? CURLE_OUT_OF_MEMORY : CURLE_URL_MALFORMAT;
  174. #elif defined(USE_WIN32_IDN)
  175. CURLcode result = win32_ascii_to_idn(puny, &enc);
  176. if(result)
  177. return result;
  178. #endif
  179. *output = enc;
  180. return CURLE_OK;
  181. }
  182. CURLcode Curl_idn_decode(const char *input, char **output)
  183. {
  184. char *d = NULL;
  185. CURLcode result = idn_decode(input, &d);
  186. #ifdef USE_LIBIDN2
  187. if(!result) {
  188. char *c = strdup(d);
  189. idn2_free(d);
  190. if(c)
  191. d = c;
  192. else
  193. result = CURLE_OUT_OF_MEMORY;
  194. }
  195. #endif
  196. if(!result)
  197. *output = d;
  198. return result;
  199. }
  200. CURLcode Curl_idn_encode(const char *puny, char **output)
  201. {
  202. char *d = NULL;
  203. CURLcode result = idn_encode(puny, &d);
  204. #ifdef USE_LIBIDN2
  205. if(!result) {
  206. char *c = strdup(d);
  207. idn2_free(d);
  208. if(c)
  209. d = c;
  210. else
  211. result = CURLE_OUT_OF_MEMORY;
  212. }
  213. #endif
  214. if(!result)
  215. *output = d;
  216. return result;
  217. }
  218. /*
  219. * Frees data allocated by idnconvert_hostname()
  220. */
  221. void Curl_free_idnconverted_hostname(struct hostname *host)
  222. {
  223. if(host->encalloc) {
  224. /* must be freed with idn2_free() if allocated by libidn */
  225. Curl_idn_free(host->encalloc);
  226. host->encalloc = NULL;
  227. }
  228. }
  229. #endif /* USE_IDN */
  230. /*
  231. * Perform any necessary IDN conversion of hostname
  232. */
  233. CURLcode Curl_idnconvert_hostname(struct hostname *host)
  234. {
  235. /* set the name we use to display the host name */
  236. host->dispname = host->name;
  237. #ifdef USE_IDN
  238. /* Check name for non-ASCII and convert hostname if we can */
  239. if(!Curl_is_ASCII_name(host->name)) {
  240. char *decoded;
  241. CURLcode result = idn_decode(host->name, &decoded);
  242. if(!result) {
  243. if(!*decoded) {
  244. /* zero length is a bad host name */
  245. Curl_idn_free(decoded);
  246. return CURLE_URL_MALFORMAT;
  247. }
  248. /* successful */
  249. host->encalloc = decoded;
  250. /* change the name pointer to point to the encoded hostname */
  251. host->name = host->encalloc;
  252. }
  253. else
  254. return result;
  255. }
  256. #endif
  257. return CURLE_OK;
  258. }