punycode.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. punycode.c from RFC 3492
  3. http://www.nicemice.net/idn/
  4. Adam M. Costello
  5. http://www.nicemice.net/amc/
  6. This is ANSI C code (C89) implementing Punycode (RFC 3492).
  7. C. Disclaimer and license
  8. Regarding this entire document or any portion of it (including
  9. the pseudocode and C code), the author makes no guarantees and
  10. is not responsible for any damage resulting from its use. The
  11. author grants irrevocable permission to anyone to use, modify,
  12. and distribute it in any way that does not diminish the rights
  13. of anyone else to use, modify, and distribute it, provided that
  14. redistributed derivative works do not contain misleading author or
  15. version information. Derivative works need not be licensed under
  16. similar terms.
  17. */
  18. #include "punycode.h"
  19. /**********************************************************/
  20. /* Implementation (would normally go in its own .c file): */
  21. #include <string.h>
  22. /*** Bootstring parameters for Punycode ***/
  23. enum { base = 36, tmin = 1, tmax = 26, skew = 38, damp = 700,
  24. initial_bias = 72, initial_n = 0x80, delimiter = 0x2D };
  25. /* basic(cp) tests whether cp is a basic code point: */
  26. #define basic(cp) ((punycode_uint)(cp) < 0x80)
  27. /* delim(cp) tests whether cp is a delimiter: */
  28. #define delim(cp) ((cp) == delimiter)
  29. /* decode_digit(cp) returns the numeric value of a basic code */
  30. /* point (for use in representing integers) in the range 0 to */
  31. /* base-1, or base if cp is does not represent a value. */
  32. static punycode_uint decode_digit(punycode_uint cp)
  33. {
  34. return cp - 48 < 10 ? cp - 22 : cp - 65 < 26 ? cp - 65 :
  35. cp - 97 < 26 ? cp - 97 : base;
  36. }
  37. /* encode_digit(d,flag) returns the basic code point whose value */
  38. /* (when used for representing integers) is d, which needs to be in */
  39. /* the range 0 to base-1. The lowercase form is used unless flag is */
  40. /* nonzero, in which case the uppercase form is used. The behavior */
  41. /* is undefined if flag is nonzero and digit d has no uppercase form. */
  42. static char encode_digit(punycode_uint d, int flag)
  43. {
  44. return d + 22 + 75 * (d < 26) - ((flag != 0) << 5);
  45. /* 0..25 map to ASCII a..z or A..Z */
  46. /* 26..35 map to ASCII 0..9 */
  47. }
  48. /* flagged(bcp) tests whether a basic code point is flagged */
  49. /* (uppercase). The behavior is undefined if bcp is not a */
  50. /* basic code point. */
  51. #define flagged(bcp) ((punycode_uint)(bcp) - 65 < 26)
  52. /* encode_basic(bcp,flag) forces a basic code point to lowercase */
  53. /* if flag is zero, uppercase if flag is nonzero, and returns */
  54. /* the resulting code point. The code point is unchanged if it */
  55. /* is caseless. The behavior is undefined if bcp is not a basic */
  56. /* code point. */
  57. static char encode_basic(punycode_uint bcp, int flag)
  58. {
  59. bcp -= (bcp - 97 < 26) << 5;
  60. return bcp + ((!flag && (bcp - 65 < 26)) << 5);
  61. }
  62. /*** Platform-specific constants ***/
  63. /* maxint is the maximum value of a punycode_uint variable: */
  64. static const punycode_uint maxint = (punycode_uint) -1;
  65. /* Because maxint is unsigned, -1 becomes the maximum value. */
  66. /*** Bias adaptation function ***/
  67. static punycode_uint adapt(
  68. punycode_uint delta, punycode_uint numpoints, int firsttime )
  69. {
  70. punycode_uint k;
  71. delta = firsttime ? delta / damp : delta >> 1;
  72. /* delta >> 1 is a faster way of doing delta / 2 */
  73. delta += delta / numpoints;
  74. for (k = 0; delta > ((base - tmin) * tmax) / 2; k += base) {
  75. delta /= base - tmin;
  76. }
  77. return k + (base - tmin + 1) * delta / (delta + skew);
  78. }
  79. /*** Main encode function ***/
  80. enum punycode_status punycode_encode(
  81. punycode_uint input_length,
  82. const punycode_uint input[],
  83. const unsigned char case_flags[],
  84. punycode_uint *output_length,
  85. char output[] )
  86. {
  87. punycode_uint n, delta, h, b, out, max_out, bias, j, m, q, k, t;
  88. /* Initialize the state: */
  89. n = initial_n;
  90. delta = out = 0;
  91. max_out = *output_length;
  92. bias = initial_bias;
  93. /* Handle the basic code points: */
  94. for (j = 0; j < input_length; ++j) {
  95. if (basic(input[j])) {
  96. if (max_out - out < 2) return punycode_big_output;
  97. output[out++] =
  98. case_flags ? encode_basic(input[j], case_flags[j]) : (char)input[j];
  99. }
  100. /* else if (input[j] < n) return punycode_bad_input; */
  101. /* (not needed for Punycode with unsigned code points) */
  102. }
  103. h = b = out;
  104. /* h is the number of code points that have been handled, b is the */
  105. /* number of basic code points, and out is the number of characters */
  106. /* that have been output. */
  107. if (b > 0) output[out++] = delimiter;
  108. /* Main encoding loop: */
  109. while (h < input_length) {
  110. /* All non-basic code points < n have been */
  111. /* handled already. Find the next larger one: */
  112. for (m = maxint, j = 0; j < input_length; ++j) {
  113. /* if (basic(input[j])) continue; */
  114. /* (not needed for Punycode) */
  115. if (input[j] >= n && input[j] < m) m = input[j];
  116. }
  117. /* Increase delta enough to advance the decoder's */
  118. /* <n,i> state to <m,0>, but guard against overflow: */
  119. if (m - n > (maxint - delta) / (h + 1)) return punycode_overflow;
  120. delta += (m - n) * (h + 1);
  121. n = m;
  122. for (j = 0; j < input_length; ++j) {
  123. /* Punycode does not need to check whether input[j] is basic: */
  124. if (input[j] < n /* || basic(input[j]) */ ) {
  125. if (++delta == 0) return punycode_overflow;
  126. }
  127. if (input[j] == n) {
  128. /* Represent delta as a generalized variable-length integer: */
  129. for (q = delta, k = base; ; k += base) {
  130. if (out >= max_out) return punycode_big_output;
  131. t = k <= bias /* + tmin */ ? tmin : /* +tmin not needed */
  132. k >= bias + tmax ? tmax : k - bias;
  133. if (q < t) break;
  134. output[out++] = encode_digit(t + (q - t) % (base - t), 0);
  135. q = (q - t) / (base - t);
  136. }
  137. output[out++] = encode_digit(q, case_flags && case_flags[j]);
  138. bias = adapt(delta, h + 1, h == b);
  139. delta = 0;
  140. ++h;
  141. }
  142. }
  143. ++delta, ++n;
  144. }
  145. *output_length = out;
  146. return punycode_success;
  147. }
  148. /*** Main decode function ***/
  149. enum punycode_status punycode_decode(
  150. punycode_uint input_length,
  151. const char input[],
  152. punycode_uint *output_length,
  153. punycode_uint output[],
  154. unsigned char case_flags[] )
  155. {
  156. punycode_uint n, out, i, max_out, bias,
  157. b, j, in, oldi, w, k, digit, t;
  158. if (!input_length) {
  159. return punycode_bad_input;
  160. }
  161. /* Initialize the state: */
  162. n = initial_n;
  163. out = i = 0;
  164. max_out = *output_length;
  165. bias = initial_bias;
  166. /* Handle the basic code points: Let b be the number of input code */
  167. /* points before the last delimiter, or 0 if there is none, then */
  168. /* copy the first b code points to the output. */
  169. for (b = 0, j = input_length - 1 ; j > 0; --j) {
  170. if (delim(input[j])) {
  171. b = j;
  172. break;
  173. }
  174. }
  175. if (b > max_out) return punycode_big_output;
  176. for (j = 0; j < b; ++j) {
  177. if (case_flags) case_flags[out] = flagged(input[j]);
  178. if (!basic(input[j])) return punycode_bad_input;
  179. output[out++] = input[j];
  180. }
  181. /* Main decoding loop: Start just after the last delimiter if any */
  182. /* basic code points were copied; start at the beginning otherwise. */
  183. for (in = b > 0 ? b + 1 : 0; in < input_length; ++out) {
  184. /* in is the index of the next character to be consumed, and */
  185. /* out is the number of code points in the output array. */
  186. /* Decode a generalized variable-length integer into delta, */
  187. /* which gets added to i. The overflow checking is easier */
  188. /* if we increase i as we go, then subtract off its starting */
  189. /* value at the end to obtain delta. */
  190. for (oldi = i, w = 1, k = base; ; k += base) {
  191. if (in >= input_length) return punycode_bad_input;
  192. digit = decode_digit(input[in++]);
  193. if (digit >= base) return punycode_bad_input;
  194. if (digit > (maxint - i) / w) return punycode_overflow;
  195. i += digit * w;
  196. t = k <= bias /* + tmin */ ? tmin : /* +tmin not needed */
  197. k >= bias + tmax ? tmax : k - bias;
  198. if (digit < t) break;
  199. if (w > maxint / (base - t)) return punycode_overflow;
  200. w *= (base - t);
  201. }
  202. bias = adapt(i - oldi, out + 1, oldi == 0);
  203. /* i was supposed to wrap around from out+1 to 0, */
  204. /* incrementing n each time, so we'll fix that now: */
  205. if (i / (out + 1) > maxint - n) return punycode_overflow;
  206. n += i / (out + 1);
  207. i %= (out + 1);
  208. /* Insert n at position i of the output: */
  209. /* not needed for Punycode: */
  210. /* if (decode_digit(n) <= base) return punycode_invalid_input; */
  211. if (out >= max_out) return punycode_big_output;
  212. if (case_flags) {
  213. memmove(case_flags + i + 1, case_flags + i, out - i);
  214. /* Case of last character determines uppercase flag: */
  215. case_flags[i] = flagged(input[in - 1]);
  216. }
  217. memmove(output + i + 1, output + i, (out - i) * sizeof *output);
  218. output[i++] = n;
  219. }
  220. *output_length = out;
  221. return punycode_success;
  222. }