ferencd@0: /* ferencd@0: punycode.c from RFC 3492 ferencd@0: http://www.nicemice.net/idn/ ferencd@0: Adam M. Costello ferencd@0: http://www.nicemice.net/amc/ ferencd@0: ferencd@0: This is ANSI C code (C89) implementing Punycode (RFC 3492). ferencd@0: ferencd@0: */ ferencd@0: ferencd@0: #include ferencd@0: ferencd@0: /*** Bootstring parameters for Punycode ***/ ferencd@0: ferencd@0: enum { base = 36, tmin = 1, tmax = 26, skew = 38, damp = 700, ferencd@0: initial_bias = 72, initial_n = 0x80, delimiter = 0x2D }; ferencd@0: ferencd@0: /* basic(cp) tests whether cp is a basic code point: */ ferencd@0: #define basic(cp) ((punycode_uint)(cp) < 0x80) ferencd@0: ferencd@0: /* delim(cp) tests whether cp is a delimiter: */ ferencd@0: #define delim(cp) ((cp) == delimiter) ferencd@0: ferencd@0: /* decode_digit(cp) returns the numeric value of a basic code */ ferencd@0: /* point (for use in representing integers) in the range 0 to */ ferencd@0: /* base-1, or base if cp is does not represent a value. */ ferencd@0: ferencd@0: static punycode_uint decode_digit(punycode_uint cp) ferencd@0: { ferencd@0: return cp - 48 < 10 ? cp - 22 : cp - 65 < 26 ? cp - 65 : ferencd@0: cp - 97 < 26 ? cp - 97 : (punycode_uint) base; ferencd@0: } ferencd@0: ferencd@0: /* encode_digit(d,flag) returns the basic code point whose value */ ferencd@0: /* (when used for representing integers) is d, which needs to be in */ ferencd@0: /* the range 0 to base-1. The lowercase form is used unless flag is */ ferencd@0: /* nonzero, in which case the uppercase form is used. The behavior */ ferencd@0: /* is undefined if flag is nonzero and digit d has no uppercase form. */ ferencd@0: ferencd@0: static char encode_digit(punycode_uint d, int flag) ferencd@0: { ferencd@0: return d + 22 + 75 * (d < 26) - ((flag != 0) << 5); ferencd@0: /* 0..25 map to ASCII a..z or A..Z */ ferencd@0: /* 26..35 map to ASCII 0..9 */ ferencd@0: } ferencd@0: ferencd@0: /* flagged(bcp) tests whether a basic code point is flagged */ ferencd@0: /* (uppercase). The behavior is undefined if bcp is not a */ ferencd@0: /* basic code point. */ ferencd@0: ferencd@0: #define flagged(bcp) ((punycode_uint)(bcp) - 65 < 26) ferencd@0: ferencd@0: /* encode_basic(bcp,flag) forces a basic code point to lowercase */ ferencd@0: /* if flag is zero, uppercase if flag is nonzero, and returns */ ferencd@0: /* the resulting code point. The code point is unchanged if it */ ferencd@0: /* is caseless. The behavior is undefined if bcp is not a basic */ ferencd@0: /* code point. */ ferencd@0: ferencd@0: static char encode_basic(punycode_uint bcp, int flag) ferencd@0: { ferencd@0: bcp -= (bcp - 97 < 26) << 5; ferencd@0: return bcp + ((!flag && (bcp - 65 < 26)) << 5); ferencd@0: } ferencd@0: ferencd@0: /*** Platform-specific constants ***/ ferencd@0: ferencd@0: /* maxint is the maximum value of a punycode_uint variable: */ ferencd@0: static const punycode_uint maxint = -1U; ferencd@0: /* Because maxint is unsigned, -1 becomes the maximum value. */ ferencd@0: ferencd@0: /*** Bias adaptation function ***/ ferencd@0: ferencd@0: static punycode_uint adapt( ferencd@0: punycode_uint delta, punycode_uint numpoints, int firsttime ) ferencd@0: { ferencd@0: punycode_uint k; ferencd@0: ferencd@0: delta = firsttime ? delta / damp : delta >> 1; ferencd@0: /* delta >> 1 is a faster way of doing delta / 2 */ ferencd@0: delta += delta / numpoints; ferencd@0: ferencd@0: for (k = 0; delta > ((base - tmin) * tmax) / 2; k += base) { ferencd@0: delta /= base - tmin; ferencd@0: } ferencd@0: ferencd@0: return k + (base - tmin + 1) * delta / (delta + skew); ferencd@0: } ferencd@0: ferencd@0: /*** Main encode function ***/ ferencd@0: ferencd@0: enum punycode_status punycode_encode( ferencd@0: punycode_uint input_length, ferencd@0: const punycode_uint input[], ferencd@0: const unsigned char case_flags[], ferencd@0: punycode_uint *output_length, ferencd@0: char output[] ) ferencd@0: { ferencd@0: punycode_uint n, delta, h, b, out, max_out, bias, j, m, q, k, t; ferencd@0: ferencd@0: /* Initialize the state: */ ferencd@0: ferencd@0: n = initial_n; ferencd@0: delta = out = 0; ferencd@0: max_out = *output_length; ferencd@0: bias = initial_bias; ferencd@0: ferencd@0: /* Handle the basic code points: */ ferencd@0: ferencd@0: for (j = 0; j < input_length; ++j) { ferencd@0: if (basic(input[j])) { ferencd@0: if (max_out - out < 2) return punycode_big_output; ferencd@0: output[out++] = ferencd@0: case_flags ? encode_basic(input[j], case_flags[j]) : input[j]; ferencd@0: } ferencd@0: /* else if (input[j] < n) return punycode_bad_input; */ ferencd@0: /* (not needed for Punycode with unsigned code points) */ ferencd@0: } ferencd@0: ferencd@0: h = b = out; ferencd@0: ferencd@0: /* h is the number of code points that have been handled, b is the */ ferencd@0: /* number of basic code points, and out is the number of characters */ ferencd@0: /* that have been output. */ ferencd@0: ferencd@0: if (b > 0) output[out++] = delimiter; ferencd@0: ferencd@0: /* Main encoding loop: */ ferencd@0: ferencd@0: while (h < input_length) { ferencd@0: /* All non-basic code points < n have been */ ferencd@0: /* handled already. Find the next larger one: */ ferencd@0: ferencd@0: for (m = maxint, j = 0; j < input_length; ++j) { ferencd@0: /* if (basic(input[j])) continue; */ ferencd@0: /* (not needed for Punycode) */ ferencd@0: if (input[j] >= n && input[j] < m) m = input[j]; ferencd@0: } ferencd@0: ferencd@0: /* Increase delta enough to advance the decoder's */ ferencd@0: /* state to , but guard against overflow: */ ferencd@0: ferencd@0: if (m - n > (maxint - delta) / (h + 1)) return punycode_overflow; ferencd@0: delta += (m - n) * (h + 1); ferencd@0: n = m; ferencd@0: ferencd@0: for (j = 0; j < input_length; ++j) { ferencd@0: /* Punycode does not need to check whether input[j] is basic: */ ferencd@0: if (input[j] < n /* || basic(input[j]) */ ) { ferencd@0: if (++delta == 0) return punycode_overflow; ferencd@0: } ferencd@0: ferencd@0: if (input[j] == n) { ferencd@0: /* Represent delta as a generalized variable-length integer: */ ferencd@0: ferencd@0: for (q = delta, k = base; ; k += base) { ferencd@0: if (out >= max_out) return punycode_big_output; ferencd@0: t = k <= bias /* + tmin */ ? (punycode_uint) tmin : /* +tmin not needed */ ferencd@0: k >= (punycode_uint) bias + (punycode_uint) tmax ? (punycode_uint) tmax : k - (punycode_uint) bias; ferencd@0: if (q < t) break; ferencd@0: output[out++] = encode_digit(t + (q - t) % (base - t), 0); ferencd@0: q = (q - t) / (base - t); ferencd@0: } ferencd@0: ferencd@0: output[out++] = encode_digit(q, case_flags && case_flags[j]); ferencd@0: bias = adapt(delta, h + 1, h == b); ferencd@0: delta = 0; ferencd@0: ++h; ferencd@0: } ferencd@0: } ferencd@0: ferencd@0: ++delta, ++n; ferencd@0: } ferencd@0: ferencd@0: *output_length = out; ferencd@0: return punycode_success; ferencd@0: } ferencd@0: ferencd@0: /*** Main decode function ***/ ferencd@0: ferencd@0: enum punycode_status punycode_decode( ferencd@0: punycode_uint input_length, ferencd@0: const char input[], ferencd@0: punycode_uint *output_length, ferencd@0: punycode_uint output[], ferencd@0: unsigned char case_flags[] ) ferencd@0: { ferencd@0: punycode_uint n, out, i, max_out, bias, ferencd@0: b, j, in, oldi, w, k, digit, t; ferencd@0: ferencd@0: /* Initialize the state: */ ferencd@0: ferencd@0: n = initial_n; ferencd@0: out = i = 0; ferencd@0: max_out = *output_length; ferencd@0: bias = initial_bias; ferencd@0: ferencd@0: /* Handle the basic code points: Let b be the number of input code */ ferencd@0: /* points before the last delimiter, or 0 if there is none, then */ ferencd@0: /* copy the first b code points to the output. */ ferencd@0: ferencd@0: for (b = j = 0; j < input_length; ++j) if (delim(input[j])) b = j; ferencd@0: if (b > max_out) return punycode_big_output; ferencd@0: ferencd@0: for (j = 0; j < b; ++j) { ferencd@0: if (case_flags) case_flags[out] = flagged(input[j]); ferencd@0: if (!basic(input[j])) return punycode_bad_input; ferencd@0: output[out++] = input[j]; ferencd@0: } ferencd@0: ferencd@0: /* Main decoding loop: Start just after the last delimiter if any */ ferencd@0: /* basic code points were copied; start at the beginning otherwise. */ ferencd@0: ferencd@0: for (in = b > 0 ? b + 1 : 0; in < input_length; ++out) { ferencd@0: ferencd@0: /* in is the index of the next character to be consumed, and */ ferencd@0: /* out is the number of code points in the output array. */ ferencd@0: ferencd@0: /* Decode a generalized variable-length integer into delta, */ ferencd@0: /* which gets added to i. The overflow checking is easier */ ferencd@0: /* if we increase i as we go, then subtract off its starting */ ferencd@0: /* value at the end to obtain delta. */ ferencd@0: ferencd@0: for (oldi = i, w = 1, k = base; ; k += base) { ferencd@0: if (in >= input_length) return punycode_bad_input; ferencd@0: digit = decode_digit(input[in++]); ferencd@0: if (digit >= base) return punycode_bad_input; ferencd@0: if (digit > (maxint - i) / w) return punycode_overflow; ferencd@0: i += digit * w; ferencd@0: t = k <= (punycode_uint) bias /* + tmin */ ? (punycode_uint) tmin : /* +tmin not needed */ ferencd@0: k >= (punycode_uint) bias + (punycode_uint) tmax ? (punycode_uint) tmax : k - (punycode_uint) bias; ferencd@0: if (digit < t) break; ferencd@0: if (w > maxint / (base - t)) return punycode_overflow; ferencd@0: w *= (base - t); ferencd@0: } ferencd@0: ferencd@0: bias = adapt(i - oldi, out + 1, oldi == 0); ferencd@0: ferencd@0: /* i was supposed to wrap around from out+1 to 0, */ ferencd@0: /* incrementing n each time, so we'll fix that now: */ ferencd@0: ferencd@0: if (i / (out + 1) > maxint - n) return punycode_overflow; ferencd@0: n += i / (out + 1); ferencd@0: i %= (out + 1); ferencd@0: ferencd@0: /* Insert n at position i of the output: */ ferencd@0: ferencd@0: /* not needed for Punycode: */ ferencd@0: /* if (decode_digit(n) <= base) return punycode_invalid_input; */ ferencd@0: if (out >= max_out) return punycode_big_output; ferencd@0: ferencd@0: if (case_flags) { ferencd@0: memmove(case_flags + i + 1, case_flags + i, out - i); ferencd@0: /* Case of last character determines uppercase flag: */ ferencd@0: case_flags[i] = flagged(input[in - 1]); ferencd@0: } ferencd@0: ferencd@0: memmove(output + i + 1, output + i, (out - i) * sizeof *output); ferencd@0: output[i++] = n; ferencd@0: } ferencd@0: ferencd@0: *output_length = out; ferencd@0: return punycode_success; ferencd@0: }