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