punycode.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /* punycode.c --- Implementation of punycode used to ASCII encode IDN's.
  2. Copyright (C) 2002-2024 Simon Josefsson
  3. This file is part of GNU Libidn.
  4. GNU Libidn is free software: you can redistribute it and/or
  5. modify it under the terms of either:
  6. * the GNU Lesser General Public License as published by the Free
  7. Software Foundation; either version 3 of the License, or (at
  8. your option) any later version.
  9. or
  10. * the GNU General Public License as published by the Free
  11. Software Foundation; either version 2 of the License, or (at
  12. your option) any later version.
  13. or both in parallel, as here.
  14. GNU Libidn is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. General Public License for more details.
  18. You should have received copies of the GNU General Public License and
  19. the GNU Lesser General Public License along with this program. If
  20. not, see <https://www.gnu.org/licenses/>. */
  21. /*
  22. * This file is derived from RFC 3492bis written by Adam M. Costello,
  23. * downloaded from http://www.nicemice.net/idn/punycode-spec.gz on
  24. * 2015-03-02 with SHA1 a966a8017f6be579d74a50a226accc7607c40133, a
  25. * copy of which is stored in the GNU Libidn version controlled
  26. * repository under doc/specification/punycode-spec.gz.
  27. *
  28. * The changes compared to Adam's file include: re-indentation, adding
  29. * the license boilerplate and this comment, #include of config.h and
  30. * punycode.h, adding GTK-DOC comments, changing the return code of
  31. * punycode_encode and punycode_decode from enum to int, renaming the
  32. * input_length_orig function input variable to input_length (and
  33. * renaming the internal input_length variable to input_len) in
  34. * punycode_encode.
  35. *
  36. * Adam's file contains the following:
  37. *
  38. * punycode-sample.c 2.0.0 (2004-Mar-21-Sun)
  39. * http://www.nicemice.net/idn/
  40. * Adam M. Costello
  41. * http://www.nicemice.net/amc/
  42. *
  43. * This is ANSI C code (C89) implementing Punycode 1.0.x.
  44. *
  45. * Disclaimer and license: Regarding this entire document or any
  46. * portion of it (including the pseudocode and C code), the author
  47. * makes no guarantees and is not responsible for any damage resulting
  48. * from its use. The author grants irrevocable permission to anyone
  49. * to use, modify, and distribute it in any way that does not diminish
  50. * the rights of anyone else to use, modify, and distribute it,
  51. * provided that redistributed derivative works do not contain
  52. * misleading author or version information. Derivative works need
  53. * not be licensed under similar terms.
  54. */
  55. #include <config.h>
  56. /**********************************************************/
  57. /* Implementation (would normally go in its own .c file): */
  58. #include <string.h>
  59. #include "punycode.h"
  60. /*** Bootstring parameters for Punycode ***/
  61. enum
  62. { base = 36, tmin = 1, tmax = 26, skew = 38, damp = 700,
  63. initial_bias = 72, initial_n = 0x80, delimiter = 0x2D
  64. };
  65. /* basic(cp) tests whether cp is a basic code point: */
  66. #define basic(cp) ((punycode_uint)(cp) < 0x80)
  67. /* delim(cp) tests whether cp is a delimiter: */
  68. #define delim(cp) ((cp) == delimiter)
  69. /* decode_digit(cp) returns the numeric value of a basic code */
  70. /* point (for use in representing integers) in the range 0 to */
  71. /* base-1, or base if cp does not represent a value. */
  72. static punycode_uint
  73. decode_digit (punycode_uint cp)
  74. {
  75. return cp - 48 < 10 ? cp - 22 : cp - 65 < 26 ? cp - 65 :
  76. cp - 97 < 26 ? cp - 97 : base;
  77. }
  78. /* encode_digit(d,flag) returns the basic code point whose value */
  79. /* (when used for representing integers) is d, which needs to be in */
  80. /* the range 0 to base-1. The lowercase form is used unless flag is */
  81. /* nonzero, in which case the uppercase form is used. The behavior */
  82. /* is undefined if flag is nonzero and digit d has no uppercase form. */
  83. static char
  84. encode_digit (punycode_uint d, int flag)
  85. {
  86. return d + 22 + 75 * (d < 26) - ((flag != 0) << 5);
  87. /* 0..25 map to ASCII a..z or A..Z */
  88. /* 26..35 map to ASCII 0..9 */
  89. }
  90. /* flagged(bcp) tests whether a basic code point is flagged */
  91. /* (uppercase). The behavior is undefined if bcp is not a */
  92. /* basic code point. */
  93. #define flagged(bcp) ((punycode_uint)(bcp) - 65 < 26)
  94. /* encode_basic(bcp,flag) forces a basic code point to lowercase */
  95. /* if flag is zero, uppercase if flag is nonzero, and returns */
  96. /* the resulting code point. The code point is unchanged if it */
  97. /* is caseless. The behavior is undefined if bcp is not a basic */
  98. /* code point. */
  99. static char
  100. encode_basic (punycode_uint bcp, int flag)
  101. {
  102. bcp -= (bcp - 97 < 26) << 5;
  103. return bcp + ((!flag && (bcp - 65 < 26)) << 5);
  104. }
  105. /*** Platform-specific constants ***/
  106. /* maxint is the maximum value of a punycode_uint variable: */
  107. static const punycode_uint maxint = -1;
  108. /* Because maxint is unsigned, -1 becomes the maximum value. */
  109. /*** Bias adaptation function ***/
  110. static punycode_uint
  111. adapt (punycode_uint delta, punycode_uint numpoints, int firsttime)
  112. {
  113. punycode_uint k;
  114. delta = firsttime ? delta / damp : delta >> 1;
  115. /* delta >> 1 is a faster way of doing delta / 2 */
  116. delta += delta / numpoints;
  117. for (k = 0; delta > ((base - tmin) * tmax) / 2; k += base)
  118. {
  119. delta /= base - tmin;
  120. }
  121. return k + (base - tmin + 1) * delta / (delta + skew);
  122. }
  123. /*** Main encode function ***/
  124. /**
  125. * punycode_encode:
  126. * @input_length: The number of code points in the @input array and
  127. * the number of flags in the @case_flags array.
  128. * @input: An array of code points. They are presumed to be Unicode
  129. * code points, but that is not strictly REQUIRED. The array
  130. * contains code points, not code units. UTF-16 uses code units
  131. * D800 through DFFF to refer to code points 10000..10FFFF. The
  132. * code points D800..DFFF do not occur in any valid Unicode string.
  133. * The code points that can occur in Unicode strings (0..D7FF and
  134. * E000..10FFFF) are also called Unicode scalar values.
  135. * @case_flags: A %NULL pointer or an array of boolean values parallel
  136. * to the @input array. Nonzero (true, flagged) suggests that the
  137. * corresponding Unicode character be forced to uppercase after
  138. * being decoded (if possible), and zero (false, unflagged) suggests
  139. * that it be forced to lowercase (if possible). ASCII code points
  140. * (0..7F) are encoded literally, except that ASCII letters are
  141. * forced to uppercase or lowercase according to the corresponding
  142. * case flags. If @case_flags is a %NULL pointer then ASCII letters
  143. * are left as they are, and other code points are treated as
  144. * unflagged.
  145. * @output_length: The caller passes in the maximum number of ASCII
  146. * code points that it can receive. On successful return it will
  147. * contain the number of ASCII code points actually output.
  148. * @output: An array of ASCII code points. It is *not*
  149. * null-terminated; it will contain zeros if and only if the @input
  150. * contains zeros. (Of course the caller can leave room for a
  151. * terminator and add one if needed.)
  152. *
  153. * Converts a sequence of code points (presumed to be Unicode code
  154. * points) to Punycode.
  155. *
  156. * Return value: The return value can be any of the #Punycode_status
  157. * values defined above except %PUNYCODE_BAD_INPUT. If not
  158. * %PUNYCODE_SUCCESS, then @output_size and @output might contain
  159. * garbage.
  160. **/
  161. int
  162. punycode_encode (size_t input_length,
  163. const punycode_uint input[],
  164. const unsigned char case_flags[],
  165. size_t *output_length, char output[])
  166. {
  167. punycode_uint input_len, n, delta, h, b, bias, j, m, q, k, t;
  168. size_t out, max_out;
  169. /* The Punycode spec assumes that the input length is the same type */
  170. /* of integer as a code point, so we need to convert the size_t to */
  171. /* a punycode_uint, which could overflow. */
  172. if (input_length > maxint)
  173. return punycode_overflow;
  174. input_len = (punycode_uint) input_length;
  175. /* Initialize the state: */
  176. n = initial_n;
  177. delta = 0;
  178. out = 0;
  179. max_out = *output_length;
  180. bias = initial_bias;
  181. /* Handle the basic code points: */
  182. for (j = 0; j < input_len; ++j)
  183. {
  184. if (basic (input[j]))
  185. {
  186. if (max_out - out < 2)
  187. return punycode_big_output;
  188. output[out++] = case_flags ?
  189. encode_basic (input[j], case_flags[j]) : (char) input[j];
  190. }
  191. else if (input[j] > 0x10FFFF
  192. || (input[j] >= 0xD800 && input[j] <= 0xDBFF))
  193. return punycode_bad_input;
  194. /* else if (input[j] < n) return punycode_bad_input; */
  195. /* (not needed for Punycode with unsigned code points) */
  196. }
  197. h = b = (punycode_uint) out;
  198. /* cannot overflow because out <= input_len <= maxint */
  199. /* h is the number of code points that have been handled, b is the */
  200. /* number of basic code points, and out is the number of ASCII code */
  201. /* points that have been output. */
  202. if (b > 0)
  203. output[out++] = delimiter;
  204. /* Main encoding loop: */
  205. while (h < input_len)
  206. {
  207. /* All non-basic code points < n have been */
  208. /* handled already. Find the next larger one: */
  209. for (m = maxint, j = 0; j < input_len; ++j)
  210. {
  211. /* if (basic(input[j])) continue; */
  212. /* (not needed for Punycode) */
  213. if (input[j] >= n && input[j] < m)
  214. m = input[j];
  215. }
  216. /* Increase delta enough to advance the decoder's */
  217. /* <n,i> state to <m,0>, but guard against overflow: */
  218. if (m - n > (maxint - delta) / (h + 1))
  219. return punycode_overflow;
  220. delta += (m - n) * (h + 1);
  221. n = m;
  222. for (j = 0; j < input_len; ++j)
  223. {
  224. /* Punycode does not need to check whether input[j] is basic: */
  225. if (input[j] < n /* || basic(input[j]) */ )
  226. {
  227. if (++delta == 0)
  228. return punycode_overflow;
  229. }
  230. if (input[j] == n)
  231. {
  232. /* Represent delta as a generalized variable-length integer: */
  233. for (q = delta, k = base;; k += base)
  234. {
  235. if (out >= max_out)
  236. return punycode_big_output;
  237. t = k <= bias /* + tmin */ ? tmin : /* +tmin not needed */
  238. k >= bias + tmax ? tmax : k - bias;
  239. if (q < t)
  240. break;
  241. output[out++] = encode_digit (t + (q - t) % (base - t), 0);
  242. q = (q - t) / (base - t);
  243. }
  244. output[out++] = encode_digit (q, case_flags && case_flags[j]);
  245. bias = adapt (delta, h + 1, h == b);
  246. delta = 0;
  247. ++h;
  248. }
  249. }
  250. ++delta, ++n;
  251. }
  252. *output_length = out;
  253. return punycode_success;
  254. }
  255. /*** Main decode function ***/
  256. /**
  257. * punycode_decode:
  258. * @input_length: The number of ASCII code points in the @input array.
  259. * @input: An array of ASCII code points (0..7F).
  260. * @output_length: The caller passes in the maximum number of code
  261. * points that it can receive into the @output array (which is also
  262. * the maximum number of flags that it can receive into the
  263. * @case_flags array, if @case_flags is not a %NULL pointer). On
  264. * successful return it will contain the number of code points
  265. * actually output (which is also the number of flags actually
  266. * output, if case_flags is not a null pointer). The decoder will
  267. * never need to output more code points than the number of ASCII
  268. * code points in the input, because of the way the encoding is
  269. * defined. The number of code points output cannot exceed the
  270. * maximum possible value of a punycode_uint, even if the supplied
  271. * @output_length is greater than that.
  272. * @output: An array of code points like the input argument of
  273. * punycode_encode() (see above).
  274. * @case_flags: A %NULL pointer (if the flags are not needed by the
  275. * caller) or an array of boolean values parallel to the @output
  276. * array. Nonzero (true, flagged) suggests that the corresponding
  277. * Unicode character be forced to uppercase by the caller (if
  278. * possible), and zero (false, unflagged) suggests that it be forced
  279. * to lowercase (if possible). ASCII code points (0..7F) are output
  280. * already in the proper case, but their flags will be set
  281. * appropriately so that applying the flags would be harmless.
  282. *
  283. * Converts Punycode to a sequence of code points (presumed to be
  284. * Unicode code points).
  285. *
  286. * Return value: The return value can be any of the #Punycode_status
  287. * values defined above. If not %PUNYCODE_SUCCESS, then
  288. * @output_length, @output, and @case_flags might contain garbage.
  289. *
  290. **/
  291. int
  292. punycode_decode (size_t input_length,
  293. const char input[],
  294. size_t *output_length,
  295. punycode_uint output[], unsigned char case_flags[])
  296. {
  297. punycode_uint n, out, i, max_out, bias, oldi, w, k, digit, t;
  298. size_t b, j, in;
  299. /* Initialize the state: */
  300. n = initial_n;
  301. out = i = 0;
  302. max_out = *output_length > maxint ? maxint
  303. : (punycode_uint) * output_length;
  304. bias = initial_bias;
  305. /* Handle the basic code points: Let b be the number of input code */
  306. /* points before the last delimiter, or 0 if there is none, then */
  307. /* copy the first b code points to the output. */
  308. for (b = j = 0; j < input_length; ++j)
  309. if (delim (input[j]))
  310. b = j;
  311. if (b > max_out)
  312. return punycode_big_output;
  313. for (j = 0; j < b; ++j)
  314. {
  315. if (case_flags)
  316. case_flags[out] = flagged (input[j]);
  317. if (!basic (input[j]))
  318. return punycode_bad_input;
  319. output[out++] = input[j];
  320. }
  321. for (j = b + (b > 0); j < input_length; ++j)
  322. if (!basic (input[j]))
  323. return punycode_bad_input;
  324. /* Main decoding loop: Start just after the last delimiter if any */
  325. /* basic code points were copied; start at the beginning otherwise. */
  326. for (in = b > 0 ? b + 1 : 0; in < input_length; ++out)
  327. {
  328. /* in is the index of the next ASCII code point to be consumed, */
  329. /* and out is the number of code points in the output array. */
  330. /* Decode a generalized variable-length integer into delta, */
  331. /* which gets added to i. The overflow checking is easier */
  332. /* if we increase i as we go, then subtract off its starting */
  333. /* value at the end to obtain delta. */
  334. for (oldi = i, w = 1, k = base;; k += base)
  335. {
  336. if (in >= input_length)
  337. return punycode_bad_input;
  338. digit = decode_digit (input[in++]);
  339. if (digit >= base)
  340. return punycode_bad_input;
  341. if (digit > (maxint - i) / w)
  342. return punycode_overflow;
  343. i += digit * w;
  344. t = k <= bias /* + tmin */ ? tmin : /* +tmin not needed */
  345. k >= bias + tmax ? tmax : k - bias;
  346. if (digit < t)
  347. break;
  348. if (w > maxint / (base - t))
  349. return punycode_overflow;
  350. w *= (base - t);
  351. }
  352. bias = adapt (i - oldi, out + 1, oldi == 0);
  353. /* i was supposed to wrap around from out+1 to 0, */
  354. /* incrementing n each time, so we'll fix that now: */
  355. if (i / (out + 1) > maxint - n)
  356. return punycode_overflow;
  357. n += i / (out + 1);
  358. if (n > 0x10FFFF || (n >= 0xD800 && n <= 0xDBFF))
  359. return punycode_bad_input;
  360. i %= (out + 1);
  361. /* Insert n at position i of the output: */
  362. /* not needed for Punycode: */
  363. /* if (basic(n)) return punycode_bad_input; */
  364. if (out >= max_out)
  365. return punycode_big_output;
  366. if (case_flags)
  367. {
  368. memmove (case_flags + i + 1, case_flags + i, out - i);
  369. /* Case of last ASCII code point determines case flag: */
  370. case_flags[i] = flagged (input[in - 1]);
  371. }
  372. memmove (output + i + 1, output + i, (out - i) * sizeof *output);
  373. output[i++] = n;
  374. }
  375. *output_length = (size_t) out;
  376. /* cannot overflow because out <= old value of *output_length */
  377. return punycode_success;
  378. }
  379. /**
  380. * punycode_uint
  381. *
  382. * Unicode code point data type, this is always a 32 bit unsigned
  383. * integer.
  384. */
  385. /**
  386. * Punycode_status
  387. * @PUNYCODE_SUCCESS: Successful operation. This value is guaranteed
  388. * to always be zero, the remaining ones are only guaranteed to hold
  389. * non-zero values, for logical comparison purposes.
  390. * @PUNYCODE_BAD_INPUT: Input is invalid.
  391. * @PUNYCODE_BIG_OUTPUT: Output would exceed the space provided.
  392. * @PUNYCODE_OVERFLOW: Input needs wider integers to process.
  393. *
  394. * Enumerated return codes of punycode_encode() and punycode_decode().
  395. * The value 0 is guaranteed to always correspond to success.
  396. */