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 RFC 3492 written by Adam M. Costello.
26 * Disclaimer and license: Regarding this entire document or any
27 * portion of it (including the pseudocode and C code), the author
28 * makes no guarantees and is not responsible for any damage resulting
29 * from its use. The author grants irrevocable permission to anyone
30 * to use, modify, and distribute it in any way that does not diminish
31 * the rights of anyone else to use, modify, and distribute it,
32 * provided that redistributed derivative works do not contain
33 * misleading author or version information. Derivative works need
34 * not be licensed under similar terms.
40 /*** Bootstring parameters for Punycode ***/
43 { base
= 36, tmin
= 1, tmax
= 26, skew
= 38, damp
= 700,
44 initial_bias
= 72, initial_n
= 0x80, delimiter
= 0x2D
47 /* basic(cp) tests whether cp is a basic code point: */
48 #define basic(cp) ((unsigned long)(cp) < 0x80)
50 /* delim(cp) tests whether cp is a delimiter: */
51 #define delim(cp) ((cp) == delimiter)
53 /* decode_digit(cp) returns the numeric value of a basic code */
54 /* point (for use in representing integers) in the range 0 to */
55 /* base-1, or base if cp is does not represent a value. */
58 decode_digit (unsigned long cp
)
60 return cp
- 48 < 10 ? cp
- 22 : cp
- 65 < 26 ? cp
- 65 :
61 cp
- 97 < 26 ? cp
- 97 : base
;
64 /* encode_digit(d,flag) returns the basic code point whose value */
65 /* (when used for representing integers) is d, which needs to be in */
66 /* the range 0 to base-1. The lowercase form is used unless flag is */
67 /* nonzero, in which case the uppercase form is used. The behavior */
68 /* is undefined if flag is nonzero and digit d has no uppercase form. */
71 encode_digit (unsigned long d
, int flag
)
73 return d
+ 22 + 75 * (d
< 26) - ((flag
!= 0) << 5);
74 /* 0..25 map to ASCII a..z or A..Z */
75 /* 26..35 map to ASCII 0..9 */
78 /* flagged(bcp) tests whether a basic code point is flagged */
79 /* (uppercase). The behavior is undefined if bcp is not a */
80 /* basic code point. */
82 #define flagged(bcp) ((unsigned long)(bcp) - 65 < 26)
84 /* encode_basic(bcp,flag) forces a basic code point to lowercase */
85 /* if flag is zero, uppercase if flag is nonzero, and returns */
86 /* the resulting code point. The code point is unchanged if it */
87 /* is caseless. The behavior is undefined if bcp is not a basic */
91 encode_basic (unsigned long bcp
, int flag
)
93 bcp
-= (bcp
- 97 < 26) << 5;
94 return bcp
+ ((!flag
&& (bcp
- 65 < 26)) << 5);
97 /*** Platform-specific constants ***/
99 /* maxint is the maximum value of a unsigned long variable: */
100 static const unsigned long maxint
= -1;
101 /* Because maxint is unsigned, -1 becomes the maximum value. */
103 /*** Bias adaptation function ***/
106 adapt (unsigned long delta
, unsigned long numpoints
, int firsttime
)
110 delta
= firsttime
? delta
/ damp
: delta
>> 1;
111 /* delta >> 1 is a faster way of doing delta / 2 */
112 delta
+= delta
/ numpoints
;
114 for (k
= 0; delta
> ((base
- tmin
) * tmax
) / 2; k
+= base
)
116 delta
/= base
- tmin
;
119 return k
+ (base
- tmin
+ 1) * delta
/ (delta
+ skew
);
122 /*** Main encode function ***/
126 * @input_length: The input_length is the number of code points in the input.
127 * @input: The input is represented as an array of Unicode code points
128 * (not code units; surrogate pairs are not allowed).
129 * @case_flags: The case_flags array holds input_length boolean
130 * values, where nonzero suggests that the corresponding
131 * Unicode character be forced to uppercase after being
132 * decoded (if possible), and zero suggests that it be
133 * forced to lowercase (if possible). ASCII code points
134 * are encoded literally, except that ASCII letters are
135 * forced to uppercase or lowercase according to the
136 * corresponding uppercase flags. If case_flags is a
137 * null pointer then ASCII letters are left as they are,
138 * and other code points are treated as if their
139 * uppercase flags were zero.
140 * @output_length: The output_length is an in/out argument: the caller
141 * passes in the maximum number of code points that it
142 * can receive, and on successful return it will
143 * contain the number of code points actually output.
144 * @output: The output will be represented as an array of ASCII code
145 * points. The output string is *not* null-terminated; it
146 * will contain zeros if and only if the input contains
147 * zeros. (Of course the caller can leave room for a
148 * terminator and add one if needed.)
150 * Converts Unicode to Punycode.
152 * Return value: The return value can be any of the punycode_status
153 * values defined above except punycode_bad_input; if
154 * not punycode_success, then output_size and output
155 * might contain garbage.
158 punycode_encode (size_t input_length
,
159 const unsigned long input
[],
160 const unsigned char case_flags
[],
161 size_t * output_length
, char output
[])
163 unsigned long n
, delta
, b
, out
, bias
, m
, q
, k
, t
;
164 size_t h
, j
, max_out
;
166 /* Initialize the state: */
170 max_out
= *output_length
;
173 /* Handle the basic code points: */
175 for (j
= 0; j
< input_length
; ++j
)
177 if (basic (input
[j
]))
179 if (max_out
- out
< 2)
180 return PUNYCODE_BIG_OUTPUT
;
182 case_flags
? encode_basic (input
[j
], case_flags
[j
]) : input
[j
];
184 /* else if (input[j] < n) return punycode_bad_input; */
185 /* (not needed for Punycode with unsigned code points) */
190 /* h is the number of code points that have been handled, b is the */
191 /* number of basic code points, and out is the number of characters */
192 /* that have been output. */
195 output
[out
++] = delimiter
;
197 /* Main encoding loop: */
199 while (h
< input_length
)
201 /* All non-basic code points < n have been */
202 /* handled already. Find the next larger one: */
204 for (m
= maxint
, j
= 0; j
< input_length
; ++j
)
206 /* if (basic(input[j])) continue; */
207 /* (not needed for Punycode) */
208 if (input
[j
] >= n
&& input
[j
] < m
)
212 /* Increase delta enough to advance the decoder's */
213 /* <n,i> state to <m,0>, but guard against overflow: */
215 if (m
- n
> (maxint
- delta
) / (h
+ 1))
216 return PUNYCODE_OVERFLOW
;
217 delta
+= (m
- n
) * (h
+ 1);
220 for (j
= 0; j
< input_length
; ++j
)
222 /* Punycode does not need to check whether input[j] is basic: */
223 if (input
[j
] < n
/* || basic(input[j]) */ )
226 return PUNYCODE_OVERFLOW
;
231 /* Represent delta as a generalized variable-length integer: */
233 for (q
= delta
, k
= base
;; k
+= base
)
236 return PUNYCODE_BIG_OUTPUT
;
237 t
= k
<= bias
/* + tmin */ ? tmin
: /* +tmin not needed */
238 k
>= bias
+ tmax
? tmax
: k
- bias
;
241 output
[out
++] = encode_digit (t
+ (q
- t
) % (base
- t
), 0);
242 q
= (q
- t
) / (base
- t
);
245 output
[out
++] = encode_digit (q
, case_flags
&& case_flags
[j
]);
246 bias
= adapt (delta
, h
+ 1, h
== b
);
255 *output_length
= out
;
256 return PUNYCODE_SUCCESS
;
259 /*** Main decode function ***/
263 * @input_length: The input_length is the number of code points in the input.
264 * @input: The input is represented as an array of ASCII code points.
265 * @output_length: The output_length is an in/out argument: the caller
266 * passes in the maximum number of code points that it
267 * can receive, and on successful return it will
268 * contain the actual number of code points output.
269 * @output: The output will be represented as an array of Unicode code
271 * @case_flags: The case_flags array needs room for at least
272 * output_length values, or it can be a null pointer if
273 * the case information is not needed. A nonzero flag
274 * suggests that the corresponding Unicode character be
275 * forced to uppercase by the caller (if possible), while
276 * zero suggests that it be forced to lowercase (if
277 * possible). ASCII code points are output already in
278 * the proper case, but their flags will be set
279 * appropriately so that applying the flags would be
282 * Converts Punycode to Unicode.
284 * Return value: The return value can be any of the punycode_status
285 * values defined above; if not punycode_success, then
286 * output_length, output, and case_flags might contain
287 * garbage. On success, the decoder will never need to
288 * write an output_length greater than input_length,
289 * because of how the encoding is defined.
293 punycode_decode (size_t input_length
,
295 size_t * output_length
,
296 unsigned long output
[], unsigned char case_flags
[])
298 unsigned long n
, i
, bias
, b
, in
, oldi
, w
, k
, digit
, t
;
299 size_t out
, max_out
, j
;
301 /* Initialize the state: */
305 max_out
= *output_length
;
308 /* Handle the basic code points: Let b be the number of input code */
309 /* points before the last delimiter, or 0 if there is none, then */
310 /* copy the first b code points to the output. */
312 for (b
= j
= 0; j
< input_length
; ++j
)
313 if (delim (input
[j
]))
316 return PUNYCODE_BIG_OUTPUT
;
318 for (j
= 0; j
< b
; ++j
)
321 case_flags
[out
] = flagged (input
[j
]);
322 if (!basic (input
[j
]))
323 return PUNYCODE_BAD_INPUT
;
324 output
[out
++] = input
[j
];
327 /* Main decoding loop: Start just after the last delimiter if any */
328 /* basic code points were copied; start at the beginning otherwise. */
330 for (in
= b
> 0 ? b
+ 1 : 0; in
< input_length
; ++out
)
333 /* in is the index of the next character to be consumed, and */
334 /* out is the number of code points in the output array. */
336 /* Decode a generalized variable-length integer into delta, */
337 /* which gets added to i. The overflow checking is easier */
338 /* if we increase i as we go, then subtract off its starting */
339 /* value at the end to obtain delta. */
341 for (oldi
= i
, w
= 1, k
= base
;; k
+= base
)
343 if (in
>= input_length
)
344 return PUNYCODE_BAD_INPUT
;
345 digit
= decode_digit (input
[in
++]);
347 return PUNYCODE_BAD_INPUT
;
348 if (digit
> (maxint
- i
) / w
)
349 return PUNYCODE_OVERFLOW
;
351 t
= k
<= bias
/* + tmin */ ? tmin
: /* +tmin not needed */
352 k
>= bias
+ tmax
? tmax
: k
- bias
;
355 if (w
> maxint
/ (base
- t
))
356 return PUNYCODE_OVERFLOW
;
360 bias
= adapt (i
- oldi
, out
+ 1, oldi
== 0);
362 /* i was supposed to wrap around from out+1 to 0, */
363 /* incrementing n each time, so we'll fix that now: */
365 if (i
/ (out
+ 1) > maxint
- n
)
366 return PUNYCODE_OVERFLOW
;
370 /* Insert n at position i of the output: */
372 /* not needed for Punycode: */
373 /* if (decode_digit(n) <= base) return punycode_invalid_input; */
375 return PUNYCODE_BIG_OUTPUT
;
379 memmove (case_flags
+ i
+ 1, case_flags
+ i
, out
- i
);
380 /* Case of last character determines uppercase flag: */
381 case_flags
[i
] = flagged (input
[in
- 1]);
384 memmove (output
+ i
+ 1, output
+ i
, (out
- i
) * sizeof *output
);
388 *output_length
= out
;
389 return PUNYCODE_SUCCESS
;