idna.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /* Copyright (c) 2011, 2018 Ben Noordhuis <info@bnoordhuis.nl>
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  10. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  12. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  13. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. */
  15. /* Derived from https://github.com/bnoordhuis/punycode
  16. * but updated to support IDNA 2008.
  17. */
  18. #include "uv.h"
  19. #include "idna.h"
  20. #include <string.h>
  21. static unsigned uv__utf8_decode1_slow(const char** p,
  22. const char* pe,
  23. unsigned a) {
  24. unsigned b;
  25. unsigned c;
  26. unsigned d;
  27. unsigned min;
  28. if (a > 0xF7)
  29. return -1;
  30. switch (*p - pe) {
  31. default:
  32. if (a > 0xEF) {
  33. min = 0x10000;
  34. a = a & 7;
  35. b = (unsigned char) *(*p)++;
  36. c = (unsigned char) *(*p)++;
  37. d = (unsigned char) *(*p)++;
  38. break;
  39. }
  40. /* Fall through. */
  41. case 2:
  42. if (a > 0xDF) {
  43. min = 0x800;
  44. b = 0x80 | (a & 15);
  45. c = (unsigned char) *(*p)++;
  46. d = (unsigned char) *(*p)++;
  47. a = 0;
  48. break;
  49. }
  50. /* Fall through. */
  51. case 1:
  52. if (a > 0xBF) {
  53. min = 0x80;
  54. b = 0x80;
  55. c = 0x80 | (a & 31);
  56. d = (unsigned char) *(*p)++;
  57. a = 0;
  58. break;
  59. }
  60. return -1; /* Invalid continuation byte. */
  61. }
  62. if (0x80 != (0xC0 & (b ^ c ^ d)))
  63. return -1; /* Invalid sequence. */
  64. b &= 63;
  65. c &= 63;
  66. d &= 63;
  67. a = (a << 18) | (b << 12) | (c << 6) | d;
  68. if (a < min)
  69. return -1; /* Overlong sequence. */
  70. if (a > 0x10FFFF)
  71. return -1; /* Four-byte sequence > U+10FFFF. */
  72. if (a >= 0xD800 && a <= 0xDFFF)
  73. return -1; /* Surrogate pair. */
  74. return a;
  75. }
  76. unsigned uv__utf8_decode1(const char** p, const char* pe) {
  77. unsigned a;
  78. a = (unsigned char) *(*p)++;
  79. if (a < 128)
  80. return a; /* ASCII, common case. */
  81. return uv__utf8_decode1_slow(p, pe, a);
  82. }
  83. #define foreach_codepoint(c, p, pe) \
  84. for (; (void) (*p <= pe && (c = uv__utf8_decode1(p, pe))), *p <= pe;)
  85. static int uv__idna_toascii_label(const char* s, const char* se,
  86. char** d, char* de) {
  87. static const char alphabet[] = "abcdefghijklmnopqrstuvwxyz0123456789";
  88. const char* ss;
  89. unsigned c;
  90. unsigned h;
  91. unsigned k;
  92. unsigned n;
  93. unsigned m;
  94. unsigned q;
  95. unsigned t;
  96. unsigned x;
  97. unsigned y;
  98. unsigned bias;
  99. unsigned delta;
  100. unsigned todo;
  101. int first;
  102. h = 0;
  103. ss = s;
  104. todo = 0;
  105. foreach_codepoint(c, &s, se) {
  106. if (c < 128)
  107. h++;
  108. else if (c == (unsigned) -1)
  109. return UV_EINVAL;
  110. else
  111. todo++;
  112. }
  113. if (todo > 0) {
  114. if (*d < de) *(*d)++ = 'x';
  115. if (*d < de) *(*d)++ = 'n';
  116. if (*d < de) *(*d)++ = '-';
  117. if (*d < de) *(*d)++ = '-';
  118. }
  119. x = 0;
  120. s = ss;
  121. foreach_codepoint(c, &s, se) {
  122. if (c > 127)
  123. continue;
  124. if (*d < de)
  125. *(*d)++ = c;
  126. if (++x == h)
  127. break; /* Visited all ASCII characters. */
  128. }
  129. if (todo == 0)
  130. return h;
  131. /* Only write separator when we've written ASCII characters first. */
  132. if (h > 0)
  133. if (*d < de)
  134. *(*d)++ = '-';
  135. n = 128;
  136. bias = 72;
  137. delta = 0;
  138. first = 1;
  139. while (todo > 0) {
  140. m = -1;
  141. s = ss;
  142. foreach_codepoint(c, &s, se)
  143. if (c >= n)
  144. if (c < m)
  145. m = c;
  146. x = m - n;
  147. y = h + 1;
  148. if (x > ~delta / y)
  149. return UV_E2BIG; /* Overflow. */
  150. delta += x * y;
  151. n = m;
  152. s = ss;
  153. foreach_codepoint(c, &s, se) {
  154. if (c < n)
  155. if (++delta == 0)
  156. return UV_E2BIG; /* Overflow. */
  157. if (c != n)
  158. continue;
  159. for (k = 36, q = delta; /* empty */; k += 36) {
  160. t = 1;
  161. if (k > bias)
  162. t = k - bias;
  163. if (t > 26)
  164. t = 26;
  165. if (q < t)
  166. break;
  167. /* TODO(bnoordhuis) Since 1 <= t <= 26 and therefore
  168. * 10 <= y <= 35, we can optimize the long division
  169. * into a table-based reciprocal multiplication.
  170. */
  171. x = q - t;
  172. y = 36 - t; /* 10 <= y <= 35 since 1 <= t <= 26. */
  173. q = x / y;
  174. t = t + x % y; /* 1 <= t <= 35 because of y. */
  175. if (*d < de)
  176. *(*d)++ = alphabet[t];
  177. }
  178. if (*d < de)
  179. *(*d)++ = alphabet[q];
  180. delta /= 2;
  181. if (first) {
  182. delta /= 350;
  183. first = 0;
  184. }
  185. /* No overflow check is needed because |delta| was just
  186. * divided by 2 and |delta+delta >= delta + delta/h|.
  187. */
  188. h++;
  189. delta += delta / h;
  190. for (bias = 0; delta > 35 * 26 / 2; bias += 36)
  191. delta /= 35;
  192. bias += 36 * delta / (delta + 38);
  193. delta = 0;
  194. todo--;
  195. }
  196. delta++;
  197. n++;
  198. }
  199. return 0;
  200. }
  201. #undef foreach_codepoint
  202. long uv__idna_toascii(const char* s, const char* se, char* d, char* de) {
  203. const char* si;
  204. const char* st;
  205. unsigned c;
  206. char* ds;
  207. int rc;
  208. ds = d;
  209. for (si = s; si < se; /* empty */) {
  210. st = si;
  211. c = uv__utf8_decode1(&si, se);
  212. if (c != '.')
  213. if (c != 0x3002) /* 。 */
  214. if (c != 0xFF0E) /* . */
  215. if (c != 0xFF61) /* 。 */
  216. continue;
  217. rc = uv__idna_toascii_label(s, st, &d, de);
  218. if (rc < 0)
  219. return rc;
  220. if (d < de)
  221. *d++ = '.';
  222. s = si;
  223. }
  224. if (s < se) {
  225. rc = uv__idna_toascii_label(s, se, &d, de);
  226. if (rc < 0)
  227. return rc;
  228. }
  229. if (d < de)
  230. *d++ = '\0';
  231. return d - ds; /* Number of bytes written. */
  232. }