2 *******************************************************************************
4 * Copyright (C) 2003, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
8 * file name: nameprep.h
10 * tab size: 8 (not used)
13 * created on: 2003feb1
14 * created by: Ram Viswanadha
18 Disclaimer and license
20 Regarding this entire document or any portion of it (including
21 the pseudocode and C code), the author makes no guarantees and
22 is not responsible for any damage resulting from its use. The
23 author grants irrevocable permission to anyone to use, modify,
24 and distribute it in any way that does not diminish the rights
25 of anyone else to use, modify, and distribute it, provided that
26 redistributed derivative works do not contain misleading author or
27 version information. Derivative works need not be licensed under
30 punycode.c 0.4.0 (2001-Nov-17-Sat)
31 http://www.cs.berkeley.edu/~amc/idn/
33 http://www.nicemice.net/amc/
36 /**********************************************************/
37 /* Implementation (would normally go in its own .c file): */
41 #include "unicode/utypes.h"
47 /*** Bootstring parameters for Punycode ***/
49 enum { base
= 36, tmin
= 1, tmax
= 26, skew
= 38, damp
= 700,
50 initial_bias
= 72, initial_n
= 0x80, delimiter
= 0x2D };
52 /* basic(cp) tests whether cp is a basic code point: */
53 #define basic(cp) ((punycode_uint)(cp) < 0x80)
55 /* delim(cp) tests whether cp is a delimiter: */
56 #define delim(cp) ((cp) == delimiter)
59 /* decode_digit(cp) returns the numeric value of a basic code */
60 /* point (for use in representing integers) in the range 0 to */
61 /* base-1, or base if cp is does not represent a value. */
63 static punycode_uint
decode_digit(punycode_uint cp
)
65 return cp
- 48 < 10 ? cp
- 22 : cp
- 65 < 26 ? cp
- 65 :
66 cp
- 97 < 26 ? cp
- 97 : base
;
69 /* encode_digit(d,flag) returns the basic code point whose value */
70 /* (when used for representing integers) is d, which needs to be in */
71 /* the range 0 to base-1. The lowercase form is used unless flag is */
72 /* nonzero, in which case the uppercase form is used. The behavior */
73 /* is undefined if flag is nonzero and digit d has no uppercase form. */
75 static char encode_digit(punycode_uint d
, int flag
)
77 return (char) d
+ 22 + 75 * (d
< 26) - ((flag
!= 0) << 5);
78 /* 0..25 map to ASCII a..z or A..Z */
79 /* 26..35 map to ASCII 0..9 */
82 /* flagged(bcp) tests whether a basic code point is flagged */
83 /* (uppercase). The behavior is undefined if bcp is not a */
84 /* basic code point. */
86 #define flagged(bcp) ((punycode_uint)(bcp) - 65 < 26)
88 /* encode_basic(bcp,flag) forces a basic code point to lowercase */
89 /* if flag is zero, uppercase if flag is nonzero, and returns */
90 /* the resulting code point. The code point is unchanged if it */
91 /* is caseless. The behavior is undefined if bcp is not a basic */
94 static char encode_basic(punycode_uint bcp
, int flag
)
96 bcp
-= (bcp
- 97 < 26) << 5;
97 return (char) bcp
+ ((!flag
&& (bcp
- 65 < 26)) << 5);
100 /*** Platform-specific constants ***/
102 /* maxint is the maximum value of a punycode_uint variable: */
103 static const punycode_uint maxint
= (punycode_uint
) (-1);
104 /* Because maxint is unsigned, -1 becomes the maximum value. */
106 /*** Bias adaptation function ***/
108 static punycode_uint
adapt(
109 punycode_uint delta
, punycode_uint numpoints
, int firsttime
)
113 delta
= firsttime
? delta
/ damp
: delta
>> 1;
114 /* delta >> 1 is a faster way of doing delta / 2 */
115 delta
+= delta
/ numpoints
;
117 for (k
= 0; delta
> ((base
- tmin
) * tmax
) / 2; k
+= base
) {
118 delta
/= base
- tmin
;
121 return k
+ (base
- tmin
+ 1) * delta
/ (delta
+ skew
);
124 /*** Main encode function ***/
126 enum punycode_status
punycode_encode(
127 punycode_uint input_length
,
128 const punycode_uint input
[],
129 const unsigned char case_flags
[],
130 punycode_uint
*output_length
,
133 punycode_uint n
, delta
, h
, b
, out
, max_out
, bias
, j
, m
, q
, k
, t
;
135 /* Initialize the state: */
139 max_out
= *output_length
;
142 /* Handle the basic code points: */
144 for (j
= 0; j
< input_length
; ++j
) {
145 if (basic(input
[j
])) {
146 if (max_out
- out
< 2) return punycode_big_output
;
147 output
[out
++] = (char)
148 (case_flags
? encode_basic(input
[j
], case_flags
[j
]) : input
[j
]);
150 /* else if (input[j] < n) return punycode_bad_input; */
151 /* (not needed for Punycode with unsigned code points) */
156 /* h is the number of code points that have been handled, b is the */
157 /* number of basic code points, and out is the number of characters */
158 /* that have been output. */
160 if (b
> 0) output
[out
++] = delimiter
;
162 /* Main encoding loop: */
164 while (h
< input_length
) {
165 /* All non-basic code points < n have been */
166 /* handled already. Find the next larger one: */
168 for (m
= maxint
, j
= 0; j
< input_length
; ++j
) {
169 /* if (basic(input[j])) continue; */
170 /* (not needed for Punycode) */
171 if (input
[j
] >= n
&& input
[j
] < m
) m
= input
[j
];
174 /* Increase delta enough to advance the decoder's */
175 /* <n,i> state to <m,0>, but guard against overflow: */
177 if (m
- n
> (maxint
- delta
) / (h
+ 1)) return punycode_overflow
;
178 delta
+= (m
- n
) * (h
+ 1);
181 for (j
= 0; j
< input_length
; ++j
) {
182 /* Punycode does not need to check whether input[j] is basic: */
183 if (input
[j
] < n
/* || basic(input[j]) */ ) {
184 if (++delta
== 0) return punycode_overflow
;
188 /* Represent delta as a generalized variable-length integer: */
190 for (q
= delta
, k
= base
; ; k
+= base
) {
191 if (out
>= max_out
) return punycode_big_output
;
192 t
= k
<= bias
/* + tmin */ ? tmin
: /* +tmin not needed */
193 k
>= bias
+ tmax
? tmax
: k
- bias
;
195 output
[out
++] = encode_digit(t
+ (q
- t
) % (base
- t
), 0);
196 q
= (q
- t
) / (base
- t
);
199 output
[out
++] = encode_digit(q
, case_flags
&& case_flags
[j
]);
200 bias
= adapt(delta
, h
+ 1, h
== b
);
209 *output_length
= out
;
210 return punycode_success
;
213 /*** Main decode function ***/
215 enum punycode_status
punycode_decode(
216 punycode_uint input_length
,
218 punycode_uint
*output_length
,
219 punycode_uint output
[],
220 unsigned char case_flags
[] )
222 punycode_uint n
, out
, i
, max_out
, bias
,
223 b
, j
, in
, oldi
, w
, k
, digit
, t
;
225 /* Initialize the state: */
229 max_out
= *output_length
;
232 /* Handle the basic code points: Let b be the number of input code */
233 /* points before the last delimiter, or 0 if there is none, then */
234 /* copy the first b code points to the output. */
236 for (b
= j
= 0; j
< input_length
; ++j
) if (delim(input
[j
])) b
= j
;
237 if (b
> max_out
) return punycode_big_output
;
239 for (j
= 0; j
< b
; ++j
) {
240 if (case_flags
) case_flags
[out
] = flagged(input
[j
]);
241 if (!basic(input
[j
])) return punycode_bad_input
;
242 output
[out
++] = input
[j
];
245 /* Main decoding loop: Start just after the last delimiter if any */
246 /* basic code points were copied; start at the beginning otherwise. */
248 for (in
= b
> 0 ? b
+ 1 : 0; in
< input_length
; ++out
) {
250 /* in is the index of the next character to be consumed, and */
251 /* out is the number of code points in the output array. */
253 /* Decode a generalized variable-length integer into delta, */
254 /* which gets added to i. The overflow checking is easier */
255 /* if we increase i as we go, then subtract off its starting */
256 /* value at the end to obtain delta. */
258 for (oldi
= i
, w
= 1, k
= base
; ; k
+= base
) {
259 if (in
>= input_length
) return punycode_bad_input
;
260 digit
= decode_digit(input
[in
++]);
261 if (digit
>= base
) return punycode_bad_input
;
262 if (digit
> (maxint
- i
) / w
) return punycode_overflow
;
264 t
= k
<= bias
/* + tmin */ ? tmin
: /* +tmin not needed */
265 k
>= bias
+ tmax
? tmax
: k
- bias
;
266 if (digit
< t
) break;
267 if (w
> maxint
/ (base
- t
)) return punycode_overflow
;
271 bias
= adapt(i
- oldi
, out
+ 1, oldi
== 0);
273 /* i was supposed to wrap around from out+1 to 0, */
274 /* incrementing n each time, so we'll fix that now: */
276 if (i
/ (out
+ 1) > maxint
- n
) return punycode_overflow
;
280 /* Insert n at position i of the output: */
282 /* not needed for Punycode: */
283 /* if (decode_digit(n) <= base) return punycode_invalid_input; */
284 if (out
>= max_out
) return punycode_big_output
;
287 memmove(case_flags
+ i
+ 1, case_flags
+ i
, out
- i
);
288 /* Case of last character determines uppercase flag: */
289 case_flags
[i
] = flagged(input
[in
- 1]);
292 memmove(output
+ i
+ 1, output
+ i
, (out
- i
) * sizeof *output
);
296 *output_length
= out
;
297 return punycode_success
;
302 #endif /* #if !UCONFIG_NO_IDNA */