dclib-punycode.inc 9.6 KB

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