2
0

idn.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. /* for macOS and iOS targets */
  48. #if defined(USE_APPLE_IDN)
  49. #include <unicode/uidna.h>
  50. static CURLcode mac_idn_to_ascii(const char *in, char **out)
  51. {
  52. UErrorCode err = U_ZERO_ERROR;
  53. UIDNA* idna = uidna_openUTS46(UIDNA_CHECK_BIDI, &err);
  54. if(U_FAILURE(err)) {
  55. return CURLE_OUT_OF_MEMORY;
  56. }
  57. else {
  58. UIDNAInfo info = UIDNA_INFO_INITIALIZER;
  59. char buffer[256] = {0};
  60. (void)uidna_nameToASCII_UTF8(idna, in, -1, buffer,
  61. sizeof(buffer), &info, &err);
  62. uidna_close(idna);
  63. if(U_FAILURE(err)) {
  64. return CURLE_URL_MALFORMAT;
  65. }
  66. else {
  67. *out = strdup(buffer);
  68. if(*out)
  69. return CURLE_OK;
  70. else
  71. return CURLE_OUT_OF_MEMORY;
  72. }
  73. }
  74. }
  75. static CURLcode mac_ascii_to_idn(const char *in, char **out)
  76. {
  77. UErrorCode err = U_ZERO_ERROR;
  78. UIDNA* idna = uidna_openUTS46(UIDNA_CHECK_BIDI, &err);
  79. if(U_FAILURE(err)) {
  80. return CURLE_OUT_OF_MEMORY;
  81. }
  82. else {
  83. UIDNAInfo info = UIDNA_INFO_INITIALIZER;
  84. char buffer[256] = {0};
  85. (void)uidna_nameToUnicodeUTF8(idna, in, -1, buffer,
  86. sizeof(buffer), &info, &err);
  87. uidna_close(idna);
  88. if(U_FAILURE(err)) {
  89. return CURLE_URL_MALFORMAT;
  90. }
  91. else {
  92. *out = strdup(buffer);
  93. if(*out)
  94. return CURLE_OK;
  95. else
  96. return CURLE_OUT_OF_MEMORY;
  97. }
  98. }
  99. }
  100. #endif
  101. #ifdef USE_WIN32_IDN
  102. /* using Windows kernel32 and normaliz libraries. */
  103. #if !defined(_WIN32_WINNT) || _WIN32_WINNT < 0x600
  104. WINBASEAPI int WINAPI IdnToAscii(DWORD dwFlags,
  105. const WCHAR *lpUnicodeCharStr,
  106. int cchUnicodeChar,
  107. WCHAR *lpASCIICharStr,
  108. int cchASCIIChar);
  109. WINBASEAPI int WINAPI IdnToUnicode(DWORD dwFlags,
  110. const WCHAR *lpASCIICharStr,
  111. int cchASCIIChar,
  112. WCHAR *lpUnicodeCharStr,
  113. int cchUnicodeChar);
  114. #endif
  115. #define IDN_MAX_LENGTH 255
  116. static CURLcode win32_idn_to_ascii(const char *in, char **out)
  117. {
  118. wchar_t *in_w = curlx_convert_UTF8_to_wchar(in);
  119. *out = NULL;
  120. if(in_w) {
  121. wchar_t punycode[IDN_MAX_LENGTH];
  122. int chars = IdnToAscii(0, in_w, (int)(wcslen(in_w) + 1), punycode,
  123. IDN_MAX_LENGTH);
  124. curlx_unicodefree(in_w);
  125. if(chars) {
  126. char *mstr = curlx_convert_wchar_to_UTF8(punycode);
  127. if(mstr) {
  128. *out = strdup(mstr);
  129. curlx_unicodefree(mstr);
  130. if(!*out)
  131. return CURLE_OUT_OF_MEMORY;
  132. }
  133. else
  134. return CURLE_OUT_OF_MEMORY;
  135. }
  136. else
  137. return CURLE_URL_MALFORMAT;
  138. }
  139. else
  140. return CURLE_URL_MALFORMAT;
  141. return CURLE_OK;
  142. }
  143. static CURLcode win32_ascii_to_idn(const char *in, char **output)
  144. {
  145. char *out = NULL;
  146. wchar_t *in_w = curlx_convert_UTF8_to_wchar(in);
  147. if(in_w) {
  148. WCHAR idn[IDN_MAX_LENGTH]; /* stores a UTF-16 string */
  149. int chars = IdnToUnicode(0, in_w, (int)(wcslen(in_w) + 1), idn,
  150. IDN_MAX_LENGTH);
  151. if(chars) {
  152. /* 'chars' is "the number of characters retrieved" */
  153. char *mstr = curlx_convert_wchar_to_UTF8(idn);
  154. if(mstr) {
  155. out = strdup(mstr);
  156. curlx_unicodefree(mstr);
  157. if(!out)
  158. return CURLE_OUT_OF_MEMORY;
  159. }
  160. }
  161. else
  162. return CURLE_URL_MALFORMAT;
  163. }
  164. else
  165. return CURLE_URL_MALFORMAT;
  166. *output = out;
  167. return CURLE_OK;
  168. }
  169. #endif /* USE_WIN32_IDN */
  170. /*
  171. * Helpers for IDNA conversions.
  172. */
  173. bool Curl_is_ASCII_name(const char *hostname)
  174. {
  175. /* get an UNSIGNED local version of the pointer */
  176. const unsigned char *ch = (const unsigned char *)hostname;
  177. if(!hostname) /* bad input, consider it ASCII! */
  178. return TRUE;
  179. while(*ch) {
  180. if(*ch++ & 0x80)
  181. return FALSE;
  182. }
  183. return TRUE;
  184. }
  185. #ifdef USE_IDN
  186. /*
  187. * Curl_idn_decode() returns an allocated IDN decoded string if it was
  188. * possible. NULL on error.
  189. *
  190. * CURLE_URL_MALFORMAT - the host name could not be converted
  191. * CURLE_OUT_OF_MEMORY - memory problem
  192. *
  193. */
  194. static CURLcode idn_decode(const char *input, char **output)
  195. {
  196. char *decoded = NULL;
  197. CURLcode result = CURLE_OK;
  198. #ifdef USE_LIBIDN2
  199. if(idn2_check_version(IDN2_VERSION)) {
  200. int flags = IDN2_NFC_INPUT
  201. #if IDN2_VERSION_NUMBER >= 0x00140000
  202. /* IDN2_NFC_INPUT: Normalize input string using normalization form C.
  203. IDN2_NONTRANSITIONAL: Perform Unicode TR46 non-transitional
  204. processing. */
  205. | IDN2_NONTRANSITIONAL
  206. #endif
  207. ;
  208. int rc = IDN2_LOOKUP(input, &decoded, flags);
  209. if(rc != IDN2_OK)
  210. /* fallback to TR46 Transitional mode for better IDNA2003
  211. compatibility */
  212. rc = IDN2_LOOKUP(input, &decoded, IDN2_TRANSITIONAL);
  213. if(rc != IDN2_OK)
  214. result = CURLE_URL_MALFORMAT;
  215. }
  216. else
  217. /* a too old libidn2 version */
  218. result = CURLE_NOT_BUILT_IN;
  219. #elif defined(USE_WIN32_IDN)
  220. result = win32_idn_to_ascii(input, &decoded);
  221. #elif defined(USE_APPLE_IDN)
  222. result = mac_idn_to_ascii(input, &decoded);
  223. #endif
  224. if(!result)
  225. *output = decoded;
  226. return result;
  227. }
  228. static CURLcode idn_encode(const char *puny, char **output)
  229. {
  230. char *enc = NULL;
  231. #ifdef USE_LIBIDN2
  232. int rc = idn2_to_unicode_8z8z(puny, &enc, 0);
  233. if(rc != IDNA_SUCCESS)
  234. return rc == IDNA_MALLOC_ERROR ? CURLE_OUT_OF_MEMORY : CURLE_URL_MALFORMAT;
  235. #elif defined(USE_WIN32_IDN)
  236. CURLcode result = win32_ascii_to_idn(puny, &enc);
  237. if(result)
  238. return result;
  239. #elif defined(USE_APPLE_IDN)
  240. CURLcode result = mac_ascii_to_idn(puny, &enc);
  241. if(result)
  242. return result;
  243. #endif
  244. *output = enc;
  245. return CURLE_OK;
  246. }
  247. CURLcode Curl_idn_decode(const char *input, char **output)
  248. {
  249. char *d = NULL;
  250. CURLcode result = idn_decode(input, &d);
  251. #ifdef USE_LIBIDN2
  252. if(!result) {
  253. char *c = strdup(d);
  254. idn2_free(d);
  255. if(c)
  256. d = c;
  257. else
  258. result = CURLE_OUT_OF_MEMORY;
  259. }
  260. #endif
  261. if(!result)
  262. *output = d;
  263. return result;
  264. }
  265. CURLcode Curl_idn_encode(const char *puny, char **output)
  266. {
  267. char *d = NULL;
  268. CURLcode result = idn_encode(puny, &d);
  269. #ifdef USE_LIBIDN2
  270. if(!result) {
  271. char *c = strdup(d);
  272. idn2_free(d);
  273. if(c)
  274. d = c;
  275. else
  276. result = CURLE_OUT_OF_MEMORY;
  277. }
  278. #endif
  279. if(!result)
  280. *output = d;
  281. return result;
  282. }
  283. /*
  284. * Frees data allocated by idnconvert_hostname()
  285. */
  286. void Curl_free_idnconverted_hostname(struct hostname *host)
  287. {
  288. Curl_safefree(host->encalloc);
  289. }
  290. #endif /* USE_IDN */
  291. /*
  292. * Perform any necessary IDN conversion of hostname
  293. */
  294. CURLcode Curl_idnconvert_hostname(struct hostname *host)
  295. {
  296. /* set the name we use to display the host name */
  297. host->dispname = host->name;
  298. #ifdef USE_IDN
  299. /* Check name for non-ASCII and convert hostname if we can */
  300. if(!Curl_is_ASCII_name(host->name)) {
  301. char *decoded;
  302. CURLcode result = Curl_idn_decode(host->name, &decoded);
  303. if(result)
  304. return result;
  305. /* successful */
  306. host->name = host->encalloc = decoded;
  307. }
  308. #endif
  309. return CURLE_OK;
  310. }