2
0

idn.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. bool Curl_win32_idn_to_ascii(const char *in, char **out)
  63. {
  64. bool success = FALSE;
  65. wchar_t *in_w = curlx_convert_UTF8_to_wchar(in);
  66. if(in_w) {
  67. wchar_t punycode[IDN_MAX_LENGTH];
  68. int chars = IdnToAscii(0, in_w, -1, punycode, IDN_MAX_LENGTH);
  69. curlx_unicodefree(in_w);
  70. if(chars) {
  71. char *mstr = curlx_convert_wchar_to_UTF8(punycode);
  72. if(mstr) {
  73. *out = strdup(mstr);
  74. curlx_unicodefree(mstr);
  75. if(*out)
  76. success = TRUE;
  77. }
  78. }
  79. }
  80. return success;
  81. }
  82. #endif /* USE_WIN32_IDN */
  83. /*
  84. * Helpers for IDNA conversions.
  85. */
  86. bool Curl_is_ASCII_name(const char *hostname)
  87. {
  88. /* get an UNSIGNED local version of the pointer */
  89. const unsigned char *ch = (const unsigned char *)hostname;
  90. if(!hostname) /* bad input, consider it ASCII! */
  91. return TRUE;
  92. while(*ch) {
  93. if(*ch++ & 0x80)
  94. return FALSE;
  95. }
  96. return TRUE;
  97. }
  98. #ifdef USE_IDN
  99. /*
  100. * Curl_idn_decode() returns an allocated IDN decoded string if it was
  101. * possible. NULL on error.
  102. */
  103. static char *idn_decode(const char *input)
  104. {
  105. char *decoded = NULL;
  106. #ifdef USE_LIBIDN2
  107. if(idn2_check_version(IDN2_VERSION)) {
  108. int flags = IDN2_NFC_INPUT
  109. #if IDN2_VERSION_NUMBER >= 0x00140000
  110. /* IDN2_NFC_INPUT: Normalize input string using normalization form C.
  111. IDN2_NONTRANSITIONAL: Perform Unicode TR46 non-transitional
  112. processing. */
  113. | IDN2_NONTRANSITIONAL
  114. #endif
  115. ;
  116. int rc = IDN2_LOOKUP(input, &decoded, flags);
  117. if(rc != IDN2_OK)
  118. /* fallback to TR46 Transitional mode for better IDNA2003
  119. compatibility */
  120. rc = IDN2_LOOKUP(input, &decoded, IDN2_TRANSITIONAL);
  121. if(rc != IDN2_OK)
  122. decoded = NULL;
  123. }
  124. #elif defined(USE_WIN32_IDN)
  125. if(!Curl_win32_idn_to_ascii(input, &decoded))
  126. decoded = NULL;
  127. #endif
  128. return decoded;
  129. }
  130. char *Curl_idn_decode(const char *input)
  131. {
  132. char *d = idn_decode(input);
  133. #ifdef USE_LIBIDN2
  134. if(d) {
  135. char *c = strdup(d);
  136. idn2_free(d);
  137. d = c;
  138. }
  139. #endif
  140. return d;
  141. }
  142. /*
  143. * Frees data allocated by idnconvert_hostname()
  144. */
  145. void Curl_free_idnconverted_hostname(struct hostname *host)
  146. {
  147. if(host->encalloc) {
  148. /* must be freed with idn2_free() if allocated by libidn */
  149. Curl_idn_free(host->encalloc);
  150. host->encalloc = NULL;
  151. }
  152. }
  153. #endif /* USE_IDN */
  154. /*
  155. * Perform any necessary IDN conversion of hostname
  156. */
  157. CURLcode Curl_idnconvert_hostname(struct hostname *host)
  158. {
  159. /* set the name we use to display the host name */
  160. host->dispname = host->name;
  161. #ifdef USE_IDN
  162. /* Check name for non-ASCII and convert hostname if we can */
  163. if(!Curl_is_ASCII_name(host->name)) {
  164. char *decoded = idn_decode(host->name);
  165. if(decoded) {
  166. if(!*decoded) {
  167. /* zero length is a bad host name */
  168. Curl_idn_free(decoded);
  169. return CURLE_URL_MALFORMAT;
  170. }
  171. /* successful */
  172. host->encalloc = decoded;
  173. /* change the name pointer to point to the encoded hostname */
  174. host->name = host->encalloc;
  175. }
  176. else
  177. return CURLE_URL_MALFORMAT;
  178. }
  179. #endif
  180. return CURLE_OK;
  181. }