82 #define basic(cp) ((punycode_uint)(cp) < 0x80)
85 #define delim(cp) ((cp) == delimiter)
94 return (
unsigned) (cp - 48 < 10 ? cp - 22 : cp - 65 < 26 ? cp - 65 :
95 cp - 97 < 26 ? cp - 97 :
base);
107 return d + 22 + 75 * (d < 26) - ((flag != 0) << 5);
116 #define flagged(bcp) ((punycode_uint)(bcp) - 65 < 26)
127 bcp -= (bcp - 97 < 26) << 5;
128 return bcp + ((!flag && (bcp - 65 < 26)) << 5);
144 delta = firsttime ? delta /
damp : delta >> 1;
146 delta += delta / numpoints;
198 const unsigned char case_flags[],
199 size_t *output_length,
char output[])
201 punycode_uint input_len, n, delta, h, b, bias, j, m, q, k, t;
208 if (input_length > maxint)
217 max_out = *output_length;
222 for (j = 0; j < input_len; ++j)
224 if (
basic (input[j]))
226 if (max_out - out < 2)
228 output[out++] = case_flags ?
229 encode_basic (input[j], case_flags[j]) : (char) input[j];
231 else if (input[j] > 0x10FFFF
232 || (input[j] >= 0xD800 && input[j] <= 0xDBFF))
250 while (h < input_len)
255 for (m = maxint, j = 0; j < input_len; ++j)
259 if (input[j] >= n && input[j] < m)
266 if (m - n > (maxint - delta) / (h + 1))
268 delta += (m - n) * (h + 1);
271 for (j = 0; j < input_len; ++j)
284 for (q = delta, k =
base;; k +=
base)
288 t = k <= bias ?
tmin :
292 output[out++] = encode_digit (t + (q - t) % (
base - t), 0);
293 q = (q - t) / (
base - t);
296 output[out++] = encode_digit (q, case_flags && case_flags[j]);
297 bias = adapt (delta, h + 1, h == b);
306 *output_length = out;
350 size_t *output_length,
353 punycode_uint n, out, i, max_out, bias, oldi, w, k, digit, t;
360 max_out = *output_length > maxint ? maxint
368 for (b = j = 0; j < input_length; ++j)
369 if (
delim (input[j]))
374 for (j = 0; j < b; ++j)
377 case_flags[out] =
flagged (input[j]);
378 if (!
basic (input[j]))
380 output[out++] = input[j];
382 for (j = b + (b > 0); j < input_length; ++j)
383 if (!
basic (input[j]))
389 for (in = b > 0 ? b + 1 : 0; in < input_length; ++out)
400 for (oldi = i, w = 1, k =
base;; k +=
base)
402 if (in >= input_length)
404 digit = decode_digit (input[in++]);
407 if (digit > (maxint - i) / w)
410 t = k <= bias ?
tmin :
414 if (w > maxint / (
base - t))
419 bias = adapt (i - oldi, out + 1, oldi == 0);
424 if (i / (out + 1) > maxint - n)
427 if (n > 0x10FFFF || (n >= 0xD800 && n <= 0xDBFF))
440 memmove (case_flags + i + 1, case_flags + i, out - i);
442 case_flags[i] =
flagged (input[in - 1]);
445 memmove (output + i + 1, output + i, (out - i) *
sizeof *output);
449 *output_length = (size_t) out;
int punycode_decode(size_t input_length, const char input[], size_t *output_length, punycode_uint output[], unsigned char case_flags[])
int punycode_encode(size_t input_length, const punycode_uint input[], const unsigned char case_flags[], size_t *output_length, char output[])