1 /* punycode.c Implementation of punycode used to ASCII encode IDN's.
2 * Copyright (C) 2002, 2003 Simon Josefsson
3 * Copyright (C) 2002 Adam M. Costello
5 * This file is part of GNU Libidn.
7 * GNU Libidn is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * GNU Libidn is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with GNU Libidn; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * This file is derived from from draft-ietf-idn-punycode-03.txt by
27 * Disclaimer and license: Regarding this entire document or any
28 * portion of it (including the pseudocode and C code), the author
29 * makes no guarantees and is not responsible for any damage resulting
30 * from its use. The author grants irrevocable permission to anyone
31 * to use, modify, and distribute it in any way that does not diminish
32 * the rights of anyone else to use, modify, and distribute it,
33 * provided that redistributed derivative works do not contain
34 * misleading author or version information. Derivative works need
35 * not be licensed under similar terms.
41 /*** Bootstring parameters for Punycode ***/
44 { base
= 36, tmin
= 1, tmax
= 26, skew
= 38, damp
= 700,
45 initial_bias
= 72, initial_n
= 0x80, delimiter
= 0x2D
48 /* basic(cp) tests whether cp is a basic code point: */
49 #define basic(cp) ((unsigned long)(cp) < 0x80)
51 /* delim(cp) tests whether cp is a delimiter: */
52 #define delim(cp) ((cp) == delimiter)
54 /* decode_digit(cp) returns the numeric value of a basic code */
55 /* point (for use in representing integers) in the range 0 to */
56 /* base-1, or base if cp is does not represent a value. */
59 decode_digit (unsigned long cp
)
61 return cp
- 48 < 10 ? cp
- 22 : cp
- 65 < 26 ? cp
- 65 :
62 cp
- 97 < 26 ? cp
- 97 : base
;
65 /* encode_digit(d,flag) returns the basic code point whose value */
66 /* (when used for representing integers) is d, which needs to be in */
67 /* the range 0 to base-1. The lowercase form is used unless flag is */
68 /* nonzero, in which case the uppercase form is used. The behavior */
69 /* is undefined if flag is nonzero and digit d has no uppercase form. */
72 encode_digit (unsigned long d
, int flag
)
74 return d
+ 22 + 75 * (d
< 26) - ((flag
!= 0) << 5);
75 /* 0..25 map to ASCII a..z or A..Z */
76 /* 26..35 map to ASCII 0..9 */
79 /* flagged(bcp) tests whether a basic code point is flagged */
80 /* (uppercase). The behavior is undefined if bcp is not a */
81 /* basic code point. */
83 #define flagged(bcp) ((unsigned long)(bcp) - 65 < 26)
85 /* encode_basic(bcp,flag) forces a basic code point to lowercase */
86 /* if flag is zero, uppercase if flag is nonzero, and returns */
87 /* the resulting code point. The code point is unchanged if it */
88 /* is caseless. The behavior is undefined if bcp is not a basic */
92 encode_basic (unsigned long bcp
, int flag
)
94 bcp
-= (bcp
- 97 < 26) << 5;
95 return bcp
+ ((!flag
&& (bcp
- 65 < 26)) << 5);
98 /*** Platform-specific constants ***/
100 /* maxint is the maximum value of a unsigned long variable: */
101 static const unsigned long maxint
= -1;
102 /* Because maxint is unsigned, -1 becomes the maximum value. */
104 /*** Bias adaptation function ***/
107 adapt (unsigned long delta
, unsigned long numpoints
, int firsttime
)
111 delta
= firsttime
? delta
/ damp
: delta
>> 1;
112 /* delta >> 1 is a faster way of doing delta / 2 */
113 delta
+= delta
/ numpoints
;
115 for (k
= 0; delta
> ((base
- tmin
) * tmax
) / 2; k
+= base
)
117 delta
/= base
- tmin
;
120 return k
+ (base
- tmin
+ 1) * delta
/ (delta
+ skew
);
123 /*** Main encode function ***/
127 * @input_length: The input_length is the number of code points in the input.
128 * @input: The input is represented as an array of Unicode code points
129 * (not code units; surrogate pairs are not allowed).
130 * @case_flags: The case_flags array holds input_length boolean
131 * values, where nonzero suggests that the corresponding
132 * Unicode character be forced to uppercase after being
133 * decoded (if possible), and zero suggests that it be
134 * forced to lowercase (if possible). ASCII code points
135 * are encoded literally, except that ASCII letters are
136 * forced to uppercase or lowercase according to the
137 * corresponding uppercase flags. If case_flags is a
138 * null pointer then ASCII letters are left as they are,
139 * and other code points are treated as if their
140 * uppercase flags were zero.
141 * @output_length: The output_length is an in/out argument: the caller
142 * passes in the maximum number of code points that it
143 * can receive, and on successful return it will
144 * contain the number of code points actually output.
145 * @output: The output will be represented as an array of ASCII code
146 * points. The output string is *not* null-terminated; it
147 * will contain zeros if and only if the input contains
148 * zeros. (Of course the caller can leave room for a
149 * terminator and add one if needed.)
151 * Converts Unicode to Punycode.
153 * Return value: The return value can be any of the punycode_status
154 * values defined above except punycode_bad_input; if
155 * not punycode_success, then output_size and output
156 * might contain garbage.
159 punycode_encode (size_t input_length
,
160 const unsigned long input
[],
161 const unsigned char case_flags
[],
162 size_t * output_length
, char output
[])
164 unsigned long n
, delta
, b
, out
, bias
, m
, q
, k
, t
;
165 size_t h
, j
, max_out
;
167 /* Initialize the state: */
171 max_out
= *output_length
;
174 /* Handle the basic code points: */
176 for (j
= 0; j
< input_length
; ++j
)
178 if (basic (input
[j
]))
180 if (max_out
- out
< 2)
181 return PUNYCODE_BIG_OUTPUT
;
183 case_flags
? encode_basic (input
[j
], case_flags
[j
]) : input
[j
];
185 /* else if (input[j] < n) return punycode_bad_input; */
186 /* (not needed for Punycode with unsigned code points) */
191 /* h is the number of code points that have been handled, b is the */
192 /* number of basic code points, and out is the number of characters */
193 /* that have been output. */
196 output
[out
++] = delimiter
;
198 /* Main encoding loop: */
200 while (h
< input_length
)
202 /* All non-basic code points < n have been */
203 /* handled already. Find the next larger one: */
205 for (m
= maxint
, j
= 0; j
< input_length
; ++j
)
207 /* if (basic(input[j])) continue; */
208 /* (not needed for Punycode) */
209 if (input
[j
] >= n
&& input
[j
] < m
)
213 /* Increase delta enough to advance the decoder's */
214 /* <n,i> state to <m,0>, but guard against overflow: */
216 if (m
- n
> (maxint
- delta
) / (h
+ 1))
217 return PUNYCODE_OVERFLOW
;
218 delta
+= (m
- n
) * (h
+ 1);
221 for (j
= 0; j
< input_length
; ++j
)
223 /* Punycode does not need to check whether input[j] is basic: */
224 if (input
[j
] < n
/* || basic(input[j]) */ )
227 return PUNYCODE_OVERFLOW
;
232 /* Represent delta as a generalized variable-length integer: */
234 for (q
= delta
, k
= base
;; k
+= base
)
237 return PUNYCODE_BIG_OUTPUT
;
238 t
= k
<= bias
/* + tmin */ ? tmin
: /* +tmin not needed */
239 k
>= bias
+ tmax
? tmax
: k
- bias
;
242 output
[out
++] = encode_digit (t
+ (q
- t
) % (base
- t
), 0);
243 q
= (q
- t
) / (base
- t
);
246 output
[out
++] = encode_digit (q
, case_flags
&& case_flags
[j
]);
247 bias
= adapt (delta
, h
+ 1, h
== b
);
256 *output_length
= out
;
257 return PUNYCODE_SUCCESS
;
260 /*** Main decode function ***/
264 * @input_length: The input_length is the number of code points in the input.
265 * @input: The input is represented as an array of ASCII code points.
266 * @output_length: The output_length is an in/out argument: the caller
267 * passes in the maximum number of code points that it
268 * can receive, and on successful return it will
269 * contain the actual number of code points output.
270 * @output: The output will be represented as an array of Unicode code
272 * @case_flags: The case_flags array needs room for at least
273 * output_length values, or it can be a null pointer if
274 * the case information is not needed. A nonzero flag
275 * suggests that the corresponding Unicode character be
276 * forced to uppercase by the caller (if possible), while
277 * zero suggests that it be forced to lowercase (if
278 * possible). ASCII code points are output already in
279 * the proper case, but their flags will be set
280 * appropriately so that applying the flags would be
283 * Converts Punycode to Unicode.
285 * Return value: The return value can be any of the punycode_status
286 * values defined above; if not punycode_success, then
287 * output_length, output, and case_flags might contain
288 * garbage. On success, the decoder will never need to
289 * write an output_length greater than input_length,
290 * because of how the encoding is defined.
294 punycode_decode (size_t input_length
,
296 size_t * output_length
,
297 unsigned long output
[], unsigned char case_flags
[])
299 unsigned long n
, i
, bias
, b
, in
, oldi
, w
, k
, digit
, t
;
300 size_t out
, max_out
, j
;
302 /* Initialize the state: */
306 max_out
= *output_length
;
309 /* Handle the basic code points: Let b be the number of input code */
310 /* points before the last delimiter, or 0 if there is none, then */
311 /* copy the first b code points to the output. */
313 for (b
= j
= 0; j
< input_length
; ++j
)
314 if (delim (input
[j
]))
317 return PUNYCODE_BIG_OUTPUT
;
319 for (j
= 0; j
< b
; ++j
)
322 case_flags
[out
] = flagged (input
[j
]);
323 if (!basic (input
[j
]))
324 return PUNYCODE_BAD_INPUT
;
325 output
[out
++] = input
[j
];
328 /* Main decoding loop: Start just after the last delimiter if any */
329 /* basic code points were copied; start at the beginning otherwise. */
331 for (in
= b
> 0 ? b
+ 1 : 0; in
< input_length
; ++out
)
334 /* in is the index of the next character to be consumed, and */
335 /* out is the number of code points in the output array. */
337 /* Decode a generalized variable-length integer into delta, */
338 /* which gets added to i. The overflow checking is easier */
339 /* if we increase i as we go, then subtract off its starting */
340 /* value at the end to obtain delta. */
342 for (oldi
= i
, w
= 1, k
= base
;; k
+= base
)
344 if (in
>= input_length
)
345 return PUNYCODE_BAD_INPUT
;
346 digit
= decode_digit (input
[in
++]);
348 return PUNYCODE_BAD_INPUT
;
349 if (digit
> (maxint
- i
) / w
)
350 return PUNYCODE_OVERFLOW
;
352 t
= k
<= bias
/* + tmin */ ? tmin
: /* +tmin not needed */
353 k
>= bias
+ tmax
? tmax
: k
- bias
;
356 if (w
> maxint
/ (base
- t
))
357 return PUNYCODE_OVERFLOW
;
361 bias
= adapt (i
- oldi
, out
+ 1, oldi
== 0);
363 /* i was supposed to wrap around from out+1 to 0, */
364 /* incrementing n each time, so we'll fix that now: */
366 if (i
/ (out
+ 1) > maxint
- n
)
367 return PUNYCODE_OVERFLOW
;
371 /* Insert n at position i of the output: */
373 /* not needed for Punycode: */
374 /* if (decode_digit(n) <= base) return punycode_invalid_input; */
376 return PUNYCODE_BIG_OUTPUT
;
380 memmove (case_flags
+ i
+ 1, case_flags
+ i
, out
- i
);
381 /* Case of last character determines uppercase flag: */
382 case_flags
[i
] = flagged (input
[in
- 1]);
385 memmove (output
+ i
+ 1, output
+ i
, (out
- i
) * sizeof *output
);
389 *output_length
= out
;
390 return PUNYCODE_SUCCESS
;