idn.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. else
  161. /* a too old libidn2 version */
  162. result = CURLE_NOT_BUILT_IN;
  163. #elif defined(USE_WIN32_IDN)
  164. result = win32_idn_to_ascii(input, &decoded);
  165. #endif
  166. if(!result)
  167. *output = decoded;
  168. return result;
  169. }
  170. static CURLcode idn_encode(const char *puny, char **output)
  171. {
  172. char *enc = NULL;
  173. #ifdef USE_LIBIDN2
  174. int rc = idn2_to_unicode_8z8z(puny, &enc, 0);
  175. if(rc != IDNA_SUCCESS)
  176. return rc == IDNA_MALLOC_ERROR ? CURLE_OUT_OF_MEMORY : CURLE_URL_MALFORMAT;
  177. #elif defined(USE_WIN32_IDN)
  178. CURLcode result = win32_ascii_to_idn(puny, &enc);
  179. if(result)
  180. return result;
  181. #endif
  182. *output = enc;
  183. return CURLE_OK;
  184. }
  185. CURLcode Curl_idn_decode(const char *input, char **output)
  186. {
  187. char *d = NULL;
  188. CURLcode result = idn_decode(input, &d);
  189. #ifdef USE_LIBIDN2
  190. if(!result) {
  191. char *c = strdup(d);
  192. idn2_free(d);
  193. if(c)
  194. d = c;
  195. else
  196. result = CURLE_OUT_OF_MEMORY;
  197. }
  198. #endif
  199. if(!result)
  200. *output = d;
  201. return result;
  202. }
  203. CURLcode Curl_idn_encode(const char *puny, char **output)
  204. {
  205. char *d = NULL;
  206. CURLcode result = idn_encode(puny, &d);
  207. #ifdef USE_LIBIDN2
  208. if(!result) {
  209. char *c = strdup(d);
  210. idn2_free(d);
  211. if(c)
  212. d = c;
  213. else
  214. result = CURLE_OUT_OF_MEMORY;
  215. }
  216. #endif
  217. if(!result)
  218. *output = d;
  219. return result;
  220. }
  221. /*
  222. * Frees data allocated by idnconvert_hostname()
  223. */
  224. void Curl_free_idnconverted_hostname(struct hostname *host)
  225. {
  226. if(host->encalloc) {
  227. /* must be freed with idn2_free() if allocated by libidn */
  228. Curl_idn_free(host->encalloc);
  229. host->encalloc = NULL;
  230. }
  231. }
  232. #endif /* USE_IDN */
  233. /*
  234. * Perform any necessary IDN conversion of hostname
  235. */
  236. CURLcode Curl_idnconvert_hostname(struct hostname *host)
  237. {
  238. /* set the name we use to display the host name */
  239. host->dispname = host->name;
  240. #ifdef USE_IDN
  241. /* Check name for non-ASCII and convert hostname if we can */
  242. if(!Curl_is_ASCII_name(host->name)) {
  243. char *decoded;
  244. CURLcode result = idn_decode(host->name, &decoded);
  245. if(!result) {
  246. if(!*decoded) {
  247. /* zero length is a bad host name */
  248. Curl_idn_free(decoded);
  249. return CURLE_URL_MALFORMAT;
  250. }
  251. /* successful */
  252. host->encalloc = decoded;
  253. /* change the name pointer to point to the encoded hostname */
  254. host->name = host->encalloc;
  255. }
  256. else
  257. return result;
  258. }
  259. #endif
  260. return CURLE_OK;
  261. }