1 /* punycode.c Implementation of punycode used to ASCII encode IDN's.
2 * Copyright (C) 2002, 2003 Simon Josefsson
4 * This file is part of GNU Libidn.
6 * GNU Libidn is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * GNU Libidn is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with GNU Libidn; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * This file is derived from RFC 3492 written by Adam M. Costello.
25 * Disclaimer and license: Regarding this entire document or any
26 * portion of it (including the pseudocode and C code), the author
27 * makes no guarantees and is not responsible for any damage resulting
28 * from its use. The author grants irrevocable permission to anyone
29 * to use, modify, and distribute it in any way that does not diminish
30 * the rights of anyone else to use, modify, and distribute it,
31 * provided that redistributed derivative works do not contain
32 * misleading author or version information. Derivative works need
33 * not be licensed under similar terms.
35 * Copyright (C) The Internet Society (2003). All Rights Reserved.
37 * This document and translations of it may be copied and furnished to
38 * others, and derivative works that comment on or otherwise explain it
39 * or assist in its implementation may be prepared, copied, published
40 * and distributed, in whole or in part, without restriction of any
41 * kind, provided that the above copyright notice and this paragraph are
42 * included on all such copies and derivative works. However, this
43 * document itself may not be modified in any way, such as by removing
44 * the copyright notice or references to the Internet Society or other
45 * Internet organizations, except as needed for the purpose of
46 * developing Internet standards in which case the procedures for
47 * copyrights defined in the Internet Standards process must be
48 * followed, or as required to translate it into languages other than
51 * The limited permissions granted above are perpetual and will not be
52 * revoked by the Internet Society or its successors or assigns.
54 * This document and the information contained herein is provided on an
55 * "AS IS" basis and THE INTERNET SOCIETY AND THE INTERNET ENGINEERING
56 * TASK FORCE DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING
57 * BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE INFORMATION
58 * HEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED WARRANTIES OF
59 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
64 /*** Bootstring parameters for Punycode ***/
67 { base
= 36, tmin
= 1, tmax
= 26, skew
= 38, damp
= 700,
68 initial_bias
= 72, initial_n
= 0x80, delimiter
= 0x2D
71 /* basic(cp) tests whether cp is a basic code point: */
72 #define basic(cp) ((uint32_t)(cp) < 0x80)
74 /* delim(cp) tests whether cp is a delimiter: */
75 #define delim(cp) ((cp) == delimiter)
77 /* decode_digit(cp) returns the numeric value of a basic code */
78 /* point (for use in representing integers) in the range 0 to */
79 /* base-1, or base if cp is does not represent a value. */
82 decode_digit (uint32_t cp
)
84 return cp
- 48 < 10 ? cp
- 22 : cp
- 65 < 26 ? cp
- 65 :
85 cp
- 97 < 26 ? cp
- 97 : base
;
88 /* encode_digit(d,flag) returns the basic code point whose value */
89 /* (when used for representing integers) is d, which needs to be in */
90 /* the range 0 to base-1. The lowercase form is used unless flag is */
91 /* nonzero, in which case the uppercase form is used. The behavior */
92 /* is undefined if flag is nonzero and digit d has no uppercase form. */
95 encode_digit (uint32_t d
, int flag
)
97 return d
+ 22 + 75 * (d
< 26) - ((flag
!= 0) << 5);
98 /* 0..25 map to ASCII a..z or A..Z */
99 /* 26..35 map to ASCII 0..9 */
102 /* flagged(bcp) tests whether a basic code point is flagged */
103 /* (uppercase). The behavior is undefined if bcp is not a */
104 /* basic code point. */
106 #define flagged(bcp) ((uint32_t)(bcp) - 65 < 26)
108 /* encode_basic(bcp,flag) forces a basic code point to lowercase */
109 /* if flag is zero, uppercase if flag is nonzero, and returns */
110 /* the resulting code point. The code point is unchanged if it */
111 /* is caseless. The behavior is undefined if bcp is not a basic */
115 encode_basic (uint32_t bcp
, int flag
)
117 bcp
-= (bcp
- 97 < 26) << 5;
118 return bcp
+ ((!flag
&& (bcp
- 65 < 26)) << 5);
121 /*** Platform-specific constants ***/
123 /* maxint is the maximum value of a uint32_t variable: */
124 static const uint32_t maxint
= -1;
125 /* Because maxint is unsigned, -1 becomes the maximum value. */
127 /*** Bias adaptation function ***/
130 adapt (uint32_t delta
, uint32_t numpoints
, int firsttime
)
134 delta
= firsttime
? delta
/ damp
: delta
>> 1;
135 /* delta >> 1 is a faster way of doing delta / 2 */
136 delta
+= delta
/ numpoints
;
138 for (k
= 0; delta
> ((base
- tmin
) * tmax
) / 2; k
+= base
)
140 delta
/= base
- tmin
;
143 return k
+ (base
- tmin
+ 1) * delta
/ (delta
+ skew
);
146 /*** Main encode function ***/
150 * @input_length: The input_length is the number of code points in the input.
151 * @input: The input is represented as an array of Unicode code points
152 * (not code units; surrogate pairs are not allowed).
153 * @case_flags: The case_flags array holds input_length boolean
154 * values, where nonzero suggests that the corresponding
155 * Unicode character be forced to uppercase after being
156 * decoded (if possible), and zero suggests that it be
157 * forced to lowercase (if possible). ASCII code points
158 * are encoded literally, except that ASCII letters are
159 * forced to uppercase or lowercase according to the
160 * corresponding uppercase flags. If case_flags is a
161 * null pointer then ASCII letters are left as they are,
162 * and other code points are treated as if their
163 * uppercase flags were zero.
164 * @output_length: The output_length is an in/out argument: the caller
165 * passes in the maximum number of code points that it
166 * can receive, and on successful return it will
167 * contain the number of code points actually output.
168 * @output: The output will be represented as an array of ASCII code
169 * points. The output string is *not* null-terminated; it
170 * will contain zeros if and only if the input contains
171 * zeros. (Of course the caller can leave room for a
172 * terminator and add one if needed.)
174 * Converts Unicode to Punycode.
176 * Return value: The return value can be any of the punycode_status
177 * values defined above except punycode_bad_input; if
178 * not punycode_success, then output_size and output
179 * might contain garbage.
182 punycode_encode (size_t input_length
,
183 const uint32_t input
[],
184 const unsigned char case_flags
[],
185 size_t * output_length
, char output
[])
187 uint32_t n
, delta
, b
, out
, bias
, m
, q
, k
, t
;
188 size_t h
, max_out
, j
;
190 /* Initialize the state: */
194 max_out
= *output_length
;
197 /* Handle the basic code points: */
198 for (j
= 0; j
< input_length
; ++j
)
200 if (basic (input
[j
]))
202 if (max_out
- out
< 2)
203 return punycode_big_output
;
205 case_flags
? encode_basic (input
[j
], case_flags
[j
]) : input
[j
];
207 /* else if (input[j] < n) return punycode_bad_input; */
208 /* (not needed for Punycode with unsigned code points) */
213 /* h is the number of code points that have been handled, b is the */
214 /* number of basic code points, and out is the number of characters */
215 /* that have been output. */
218 output
[out
++] = delimiter
;
220 /* Main encoding loop: */
222 while (h
< input_length
)
224 /* All non-basic code points < n have been */
225 /* handled already. Find the next larger one: */
227 for (m
= maxint
, j
= 0; j
< input_length
; ++j
)
229 /* if (basic(input[j])) continue; */
230 /* (not needed for Punycode) */
231 if (input
[j
] >= n
&& input
[j
] < m
)
235 /* Increase delta enough to advance the decoder's */
236 /* <n,i> state to <m,0>, but guard against overflow: */
238 if (m
- n
> (maxint
- delta
) / (h
+ 1))
239 return punycode_overflow
;
240 delta
+= (m
- n
) * (h
+ 1);
243 for (j
= 0; j
< input_length
; ++j
)
245 /* Punycode does not need to check whether input[j] is basic: */
246 if (input
[j
] < n
/* || basic(input[j]) */ )
249 return punycode_overflow
;
254 /* Represent delta as a generalized variable-length integer: */
256 for (q
= delta
, k
= base
;; k
+= base
)
259 return punycode_big_output
;
260 t
= k
<= bias
/* + tmin */ ? tmin
: /* +tmin not needed */
261 k
>= bias
+ tmax
? tmax
: k
- bias
;
264 output
[out
++] = encode_digit (t
+ (q
- t
) % (base
- t
), 0);
265 q
= (q
- t
) / (base
- t
);
268 output
[out
++] = encode_digit (q
, case_flags
&& case_flags
[j
]);
269 bias
= adapt (delta
, h
+ 1, h
== b
);
278 *output_length
= out
;
279 return punycode_success
;
282 /*** Main decode function ***/
286 * @input_length: The input_length is the number of code points in the input.
287 * @input: The input is represented as an array of ASCII code points.
288 * @output_length: The output_length is an in/out argument: the caller
289 * passes in the maximum number of code points that it
290 * can receive, and on successful return it will
291 * contain the actual number of code points output.
292 * @output: The output will be represented as an array of Unicode code
294 * @case_flags: The case_flags array needs room for at least
295 * output_length values, or it can be a null pointer if
296 * the case information is not needed. A nonzero flag
297 * suggests that the corresponding Unicode character be
298 * forced to uppercase by the caller (if possible), while
299 * zero suggests that it be forced to lowercase (if
300 * possible). ASCII code points are output already in
301 * the proper case, but their flags will be set
302 * appropriately so that applying the flags would be
305 * Converts Punycode to Unicode.
307 * Return value: The return value can be any of the punycode_status
308 * values defined above; if not punycode_success, then
309 * output_length, output, and case_flags might contain
310 * garbage. On success, the decoder will never need to
311 * write an output_length greater than input_length,
312 * because of how the encoding is defined.
316 punycode_decode (size_t input_length
,
318 size_t * output_length
,
319 uint32_t output
[], unsigned char case_flags
[])
321 uint32_t n
, out
, i
, max_out
, bias
, b
, j
, in
, oldi
, w
, k
, digit
, t
;
323 /* Initialize the state: */
327 max_out
= *output_length
;
330 /* Handle the basic code points: Let b be the number of input code */
331 /* points before the last delimiter, or 0 if there is none, then */
332 /* copy the first b code points to the output. */
334 for (b
= j
= 0; j
< input_length
; ++j
)
335 if (delim (input
[j
]))
338 return punycode_big_output
;
340 for (j
= 0; j
< b
; ++j
)
343 case_flags
[out
] = flagged (input
[j
]);
344 if (!basic (input
[j
]))
345 return punycode_bad_input
;
346 output
[out
++] = input
[j
];
349 /* Main decoding loop: Start just after the last delimiter if any */
350 /* basic code points were copied; start at the beginning otherwise. */
352 for (in
= b
> 0 ? b
+ 1 : 0; in
< input_length
; ++out
)
355 /* in is the index of the next character to be consumed, and */
356 /* out is the number of code points in the output array. */
358 /* Decode a generalized variable-length integer into delta, */
359 /* which gets added to i. The overflow checking is easier */
360 /* if we increase i as we go, then subtract off its starting */
361 /* value at the end to obtain delta. */
363 for (oldi
= i
, w
= 1, k
= base
;; k
+= base
)
365 if (in
>= input_length
)
366 return punycode_bad_input
;
367 digit
= decode_digit (input
[in
++]);
369 return punycode_bad_input
;
370 if (digit
> (maxint
- i
) / w
)
371 return punycode_overflow
;
373 t
= k
<= bias
/* + tmin */ ? tmin
: /* +tmin not needed */
374 k
>= bias
+ tmax
? tmax
: k
- bias
;
377 if (w
> maxint
/ (base
- t
))
378 return punycode_overflow
;
382 bias
= adapt (i
- oldi
, out
+ 1, oldi
== 0);
384 /* i was supposed to wrap around from out+1 to 0, */
385 /* incrementing n each time, so we'll fix that now: */
387 if (i
/ (out
+ 1) > maxint
- n
)
388 return punycode_overflow
;
392 /* Insert n at position i of the output: */
394 /* not needed for Punycode: */
395 /* if (decode_digit(n) <= base) return punycode_invalid_input; */
397 return punycode_big_output
;
401 memmove (case_flags
+ i
+ 1, case_flags
+ i
, out
- i
);
403 /* Case of last character determines uppercase flag: */
404 case_flags
[i
] = flagged (input
[in
- 1]);
407 memmove (output
+ i
+ 1, output
+ i
, (out
- i
) * sizeof *output
);
411 *output_length
= out
;
412 return punycode_success
;