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