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