/src/libidn2/lib/puny_encode.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* punycode.c - punycode encoding/decoding |
2 | | Copyright (C) 2011-2022 Simon Josefsson |
3 | | |
4 | | Libidn2 is free software: you can redistribute it and/or modify it |
5 | | under the terms of either: |
6 | | |
7 | | * the GNU Lesser General Public License as published by the Free |
8 | | Software Foundation; either version 3 of the License, or (at |
9 | | your option) any later version. |
10 | | |
11 | | or |
12 | | |
13 | | * the GNU General Public License as published by the Free |
14 | | Software Foundation; either version 2 of the License, or (at |
15 | | your option) any later version. |
16 | | |
17 | | or both in parallel, as here. |
18 | | |
19 | | This program is distributed in the hope that it will be useful, |
20 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
21 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22 | | GNU General Public License for more details. |
23 | | |
24 | | You should have received copies of the GNU General Public License and |
25 | | the GNU Lesser General Public License along with this program. If |
26 | | not, see <http://www.gnu.org/licenses/>. |
27 | | */ |
28 | | |
29 | | /* |
30 | | Code copied from http://www.nicemice.net/idn/punycode-spec.gz on |
31 | | 2011-01-04 with SHA-1 a966a8017f6be579d74a50a226accc7607c40133 |
32 | | labeled punycode-spec 1.0.3 (2006-Mar-23-Thu). It is modified for |
33 | | Libidn2 by Simon Josefsson. License on the original code: |
34 | | |
35 | | punycode-spec 1.0.3 (2006-Mar-23-Thu) |
36 | | http://www.nicemice.net/idn/ |
37 | | Adam M. Costello |
38 | | http://www.nicemice.net/amc/ |
39 | | |
40 | | B. Disclaimer and license |
41 | | |
42 | | Regarding this entire document or any portion of it (including |
43 | | the pseudocode and C code), the author makes no guarantees and |
44 | | is not responsible for any damage resulting from its use. The |
45 | | author grants irrevocable permission to anyone to use, modify, |
46 | | and distribute it in any way that does not diminish the rights |
47 | | of anyone else to use, modify, and distribute it, provided that |
48 | | redistributed derivative works do not contain misleading author or |
49 | | version information. Derivative works need not be licensed under |
50 | | similar terms. |
51 | | |
52 | | C. Punycode sample implementation |
53 | | |
54 | | punycode-sample.c 2.0.0 (2004-Mar-21-Sun) |
55 | | http://www.nicemice.net/idn/ |
56 | | Adam M. Costello |
57 | | http://www.nicemice.net/amc/ |
58 | | |
59 | | This is ANSI C code (C89) implementing Punycode 1.0.x. |
60 | | */ |
61 | | |
62 | | #include <config.h> |
63 | | |
64 | | #include "idn2.h" /* IDN2_OK, ... */ |
65 | | |
66 | | /* Re-definitions to avoid modifying code below too much. */ |
67 | 0 | #define punycode_uint uint32_t |
68 | 0 | #define punycode_success IDN2_OK |
69 | 0 | #define punycode_overflow IDN2_PUNYCODE_OVERFLOW |
70 | 0 | #define punycode_big_output IDN2_PUNYCODE_BIG_OUTPUT |
71 | 0 | #define punycode_bad_input IDN2_PUNYCODE_BAD_INPUT |
72 | | #define punycode_encode _idn2_punycode_encode_internal |
73 | | |
74 | | /**********************************************************/ |
75 | | /* Implementation (would normally go in its own .c file): */ |
76 | | |
77 | | #include <string.h> |
78 | | |
79 | | #include "punycode.h" |
80 | | |
81 | | /*** Bootstring parameters for Punycode ***/ |
82 | | |
83 | | enum |
84 | | { base = 36, tmin = 1, tmax = 26, skew = 38, damp = 700, |
85 | | initial_bias = 72, initial_n = 0x80, delimiter = 0x2D |
86 | | }; |
87 | | |
88 | | /* basic(cp) tests whether cp is a basic code point: */ |
89 | 0 | #define basic(cp) ((punycode_uint)(cp) < 0x80) |
90 | | |
91 | | /* encode_digit(d,flag) returns the basic code point whose value */ |
92 | | /* (when used for representing integers) is d, which needs to be in */ |
93 | | /* the range 0 to base-1. The lowercase form is used unless flag is */ |
94 | | /* nonzero, in which case the uppercase form is used. The behavior */ |
95 | | /* is undefined if flag is nonzero and digit d has no uppercase form. */ |
96 | | |
97 | | static char |
98 | | encode_digit (punycode_uint d, int flag) |
99 | 0 | { |
100 | 0 | return d + 22 + 75 * (d < 26) - ((flag != 0) << 5); |
101 | | /* 0..25 map to ASCII a..z or A..Z */ |
102 | | /* 26..35 map to ASCII 0..9 */ |
103 | 0 | } |
104 | | |
105 | | /*** Platform-specific constants ***/ |
106 | | |
107 | | /* maxint is the maximum value of a punycode_uint variable: */ |
108 | | static const punycode_uint maxint = -1; |
109 | | /* Because maxint is unsigned, -1 becomes the maximum value. */ |
110 | | |
111 | | /*** Bias adaptation function ***/ |
112 | | |
113 | | static punycode_uint |
114 | | adapt (punycode_uint delta, punycode_uint numpoints, int firsttime) |
115 | | _GL_ATTRIBUTE_CONST; |
116 | | |
117 | | static punycode_uint adapt (punycode_uint delta, punycode_uint numpoints, |
118 | | int firsttime) |
119 | 0 | { |
120 | 0 | punycode_uint k; |
121 | |
|
122 | 0 | delta = firsttime ? delta / damp : delta >> 1; |
123 | | /* delta >> 1 is a faster way of doing delta / 2 */ |
124 | 0 | delta += delta / numpoints; |
125 | |
|
126 | 0 | for (k = 0; delta > ((base - tmin) * tmax) / 2; k += base) |
127 | 0 | { |
128 | 0 | delta /= base - tmin; |
129 | 0 | } |
130 | |
|
131 | 0 | return k + (base - tmin + 1) * delta / (delta + skew); |
132 | 0 | } |
133 | | |
134 | | /*** Main encode function ***/ |
135 | | |
136 | | int |
137 | | punycode_encode (size_t input_length_orig, |
138 | | const punycode_uint input[], |
139 | | size_t *output_length, char output[]) |
140 | 0 | { |
141 | 0 | punycode_uint input_length, n, delta, h, b, bias, j, m, q, k, t; |
142 | 0 | size_t out, max_out; |
143 | | |
144 | | /* The Punycode spec assumes that the input length is the same type */ |
145 | | /* of integer as a code point, so we need to convert the size_t to */ |
146 | | /* a punycode_uint, which could overflow. */ |
147 | |
|
148 | 0 | if (input_length_orig > maxint) |
149 | 0 | return punycode_overflow; |
150 | 0 | input_length = (punycode_uint) input_length_orig; |
151 | | |
152 | | /* Initialize the state: */ |
153 | |
|
154 | 0 | n = initial_n; |
155 | 0 | delta = 0; |
156 | 0 | out = 0; |
157 | 0 | max_out = *output_length; |
158 | 0 | bias = initial_bias; |
159 | | |
160 | | /* Handle the basic code points: */ |
161 | |
|
162 | 0 | for (j = 0; j < input_length; ++j) |
163 | 0 | { |
164 | 0 | if (basic (input[j])) |
165 | 0 | { |
166 | 0 | if (max_out - out < 2) |
167 | 0 | return punycode_big_output; |
168 | 0 | output[out++] = (char) input[j]; |
169 | 0 | } |
170 | 0 | else if (input[j] > 0x10FFFF |
171 | 0 | || (input[j] >= 0xD800 && input[j] <= 0xDBFF)) |
172 | 0 | return punycode_bad_input; |
173 | 0 | } |
174 | | |
175 | 0 | h = b = (punycode_uint) out; |
176 | | /* cannot overflow because out <= input_length <= maxint */ |
177 | | |
178 | | /* h is the number of code points that have been handled, b is the */ |
179 | | /* number of basic code points, and out is the number of ASCII code */ |
180 | | /* points that have been output. */ |
181 | |
|
182 | 0 | if (b > 0) |
183 | 0 | output[out++] = delimiter; |
184 | | |
185 | | /* Main encoding loop: */ |
186 | |
|
187 | 0 | while (h < input_length) |
188 | 0 | { |
189 | | /* All non-basic code points < n have been */ |
190 | | /* handled already. Find the next larger one: */ |
191 | |
|
192 | 0 | for (m = maxint, j = 0; j < input_length; ++j) |
193 | 0 | { |
194 | | /* if (basic(input[j])) continue; */ |
195 | | /* (not needed for Punycode) */ |
196 | 0 | if (input[j] >= n && input[j] < m) |
197 | 0 | m = input[j]; |
198 | 0 | } |
199 | | |
200 | | /* Increase delta enough to advance the decoder's */ |
201 | | /* <n,i> state to <m,0>, but guard against overflow: */ |
202 | |
|
203 | 0 | if (m - n > (maxint - delta) / (h + 1)) |
204 | 0 | return punycode_overflow; |
205 | 0 | delta += (m - n) * (h + 1); |
206 | 0 | n = m; |
207 | |
|
208 | 0 | for (j = 0; j < input_length; ++j) |
209 | 0 | { |
210 | | /* Punycode does not need to check whether input[j] is basic: */ |
211 | 0 | if (input[j] < n /* || basic(input[j]) */ ) |
212 | 0 | { |
213 | 0 | if (++delta == 0) |
214 | 0 | return punycode_overflow; |
215 | 0 | } |
216 | | |
217 | 0 | if (input[j] == n) |
218 | 0 | { |
219 | | /* Represent delta as a generalized variable-length integer: */ |
220 | |
|
221 | 0 | for (q = delta, k = base;; k += base) |
222 | 0 | { |
223 | 0 | if (out >= max_out) |
224 | 0 | return punycode_big_output; |
225 | 0 | t = k <= bias /* + tmin */ ? tmin : /* +tmin not needed */ |
226 | 0 | k >= bias + tmax ? tmax : k - bias; |
227 | 0 | if (q < t) |
228 | 0 | break; |
229 | 0 | output[out++] = encode_digit (t + (q - t) % (base - t), 0); |
230 | 0 | q = (q - t) / (base - t); |
231 | 0 | } |
232 | | |
233 | 0 | output[out++] = encode_digit (q, 0); |
234 | 0 | bias = adapt (delta, h + 1, h == b); |
235 | 0 | delta = 0; |
236 | 0 | ++h; |
237 | 0 | } |
238 | 0 | } |
239 | | |
240 | 0 | ++delta, ++n; |
241 | 0 | } |
242 | | |
243 | 0 | *output_length = out; |
244 | 0 | return punycode_success; |
245 | 0 | } |
246 | | |
247 | | /* Create a compatibility symbol if supported. Hidden references make |
248 | | the target symbol hidden, hence the alias. */ |
249 | | #ifdef HAVE_SYMVER_ALIAS_SUPPORT |
250 | | __typeof__ (_idn2_punycode_encode_internal) _idn2_punycode_encode |
251 | | __attribute__((visibility ("default"), |
252 | | alias ("_idn2_punycode_encode_internal"))); |
253 | | __asm__ (".symver _idn2_punycode_encode, _idn2_punycode_encode@IDN2_0.0.0"); |
254 | | #endif |