annotate 3rdparty/vmime/contrib/punycode/punycode.c @ 0:a4671277546c tip

created the repository for the thymian project
author ferencd
date Tue, 17 Aug 2021 11:19:54 +0200
parents
children
rev   line source
ferencd@0 1 /*
ferencd@0 2 punycode.c from RFC 3492
ferencd@0 3 http://www.nicemice.net/idn/
ferencd@0 4 Adam M. Costello
ferencd@0 5 http://www.nicemice.net/amc/
ferencd@0 6
ferencd@0 7 This is ANSI C code (C89) implementing Punycode (RFC 3492).
ferencd@0 8
ferencd@0 9 */
ferencd@0 10
ferencd@0 11 #include <string.h>
ferencd@0 12
ferencd@0 13 /*** Bootstring parameters for Punycode ***/
ferencd@0 14
ferencd@0 15 enum { base = 36, tmin = 1, tmax = 26, skew = 38, damp = 700,
ferencd@0 16 initial_bias = 72, initial_n = 0x80, delimiter = 0x2D };
ferencd@0 17
ferencd@0 18 /* basic(cp) tests whether cp is a basic code point: */
ferencd@0 19 #define basic(cp) ((punycode_uint)(cp) < 0x80)
ferencd@0 20
ferencd@0 21 /* delim(cp) tests whether cp is a delimiter: */
ferencd@0 22 #define delim(cp) ((cp) == delimiter)
ferencd@0 23
ferencd@0 24 /* decode_digit(cp) returns the numeric value of a basic code */
ferencd@0 25 /* point (for use in representing integers) in the range 0 to */
ferencd@0 26 /* base-1, or base if cp is does not represent a value. */
ferencd@0 27
ferencd@0 28 static punycode_uint decode_digit(punycode_uint cp)
ferencd@0 29 {
ferencd@0 30 return cp - 48 < 10 ? cp - 22 : cp - 65 < 26 ? cp - 65 :
ferencd@0 31 cp - 97 < 26 ? cp - 97 : (punycode_uint) base;
ferencd@0 32 }
ferencd@0 33
ferencd@0 34 /* encode_digit(d,flag) returns the basic code point whose value */
ferencd@0 35 /* (when used for representing integers) is d, which needs to be in */
ferencd@0 36 /* the range 0 to base-1. The lowercase form is used unless flag is */
ferencd@0 37 /* nonzero, in which case the uppercase form is used. The behavior */
ferencd@0 38 /* is undefined if flag is nonzero and digit d has no uppercase form. */
ferencd@0 39
ferencd@0 40 static char encode_digit(punycode_uint d, int flag)
ferencd@0 41 {
ferencd@0 42 return d + 22 + 75 * (d < 26) - ((flag != 0) << 5);
ferencd@0 43 /* 0..25 map to ASCII a..z or A..Z */
ferencd@0 44 /* 26..35 map to ASCII 0..9 */
ferencd@0 45 }
ferencd@0 46
ferencd@0 47 /* flagged(bcp) tests whether a basic code point is flagged */
ferencd@0 48 /* (uppercase). The behavior is undefined if bcp is not a */
ferencd@0 49 /* basic code point. */
ferencd@0 50
ferencd@0 51 #define flagged(bcp) ((punycode_uint)(bcp) - 65 < 26)
ferencd@0 52
ferencd@0 53 /* encode_basic(bcp,flag) forces a basic code point to lowercase */
ferencd@0 54 /* if flag is zero, uppercase if flag is nonzero, and returns */
ferencd@0 55 /* the resulting code point. The code point is unchanged if it */
ferencd@0 56 /* is caseless. The behavior is undefined if bcp is not a basic */
ferencd@0 57 /* code point. */
ferencd@0 58
ferencd@0 59 static char encode_basic(punycode_uint bcp, int flag)
ferencd@0 60 {
ferencd@0 61 bcp -= (bcp - 97 < 26) << 5;
ferencd@0 62 return bcp + ((!flag && (bcp - 65 < 26)) << 5);
ferencd@0 63 }
ferencd@0 64
ferencd@0 65 /*** Platform-specific constants ***/
ferencd@0 66
ferencd@0 67 /* maxint is the maximum value of a punycode_uint variable: */
ferencd@0 68 static const punycode_uint maxint = -1U;
ferencd@0 69 /* Because maxint is unsigned, -1 becomes the maximum value. */
ferencd@0 70
ferencd@0 71 /*** Bias adaptation function ***/
ferencd@0 72
ferencd@0 73 static punycode_uint adapt(
ferencd@0 74 punycode_uint delta, punycode_uint numpoints, int firsttime )
ferencd@0 75 {
ferencd@0 76 punycode_uint k;
ferencd@0 77
ferencd@0 78 delta = firsttime ? delta / damp : delta >> 1;
ferencd@0 79 /* delta >> 1 is a faster way of doing delta / 2 */
ferencd@0 80 delta += delta / numpoints;
ferencd@0 81
ferencd@0 82 for (k = 0; delta > ((base - tmin) * tmax) / 2; k += base) {
ferencd@0 83 delta /= base - tmin;
ferencd@0 84 }
ferencd@0 85
ferencd@0 86 return k + (base - tmin + 1) * delta / (delta + skew);
ferencd@0 87 }
ferencd@0 88
ferencd@0 89 /*** Main encode function ***/
ferencd@0 90
ferencd@0 91 enum punycode_status punycode_encode(
ferencd@0 92 punycode_uint input_length,
ferencd@0 93 const punycode_uint input[],
ferencd@0 94 const unsigned char case_flags[],
ferencd@0 95 punycode_uint *output_length,
ferencd@0 96 char output[] )
ferencd@0 97 {
ferencd@0 98 punycode_uint n, delta, h, b, out, max_out, bias, j, m, q, k, t;
ferencd@0 99
ferencd@0 100 /* Initialize the state: */
ferencd@0 101
ferencd@0 102 n = initial_n;
ferencd@0 103 delta = out = 0;
ferencd@0 104 max_out = *output_length;
ferencd@0 105 bias = initial_bias;
ferencd@0 106
ferencd@0 107 /* Handle the basic code points: */
ferencd@0 108
ferencd@0 109 for (j = 0; j < input_length; ++j) {
ferencd@0 110 if (basic(input[j])) {
ferencd@0 111 if (max_out - out < 2) return punycode_big_output;
ferencd@0 112 output[out++] =
ferencd@0 113 case_flags ? encode_basic(input[j], case_flags[j]) : input[j];
ferencd@0 114 }
ferencd@0 115 /* else if (input[j] < n) return punycode_bad_input; */
ferencd@0 116 /* (not needed for Punycode with unsigned code points) */
ferencd@0 117 }
ferencd@0 118
ferencd@0 119 h = b = out;
ferencd@0 120
ferencd@0 121 /* h is the number of code points that have been handled, b is the */
ferencd@0 122 /* number of basic code points, and out is the number of characters */
ferencd@0 123 /* that have been output. */
ferencd@0 124
ferencd@0 125 if (b > 0) output[out++] = delimiter;
ferencd@0 126
ferencd@0 127 /* Main encoding loop: */
ferencd@0 128
ferencd@0 129 while (h < input_length) {
ferencd@0 130 /* All non-basic code points < n have been */
ferencd@0 131 /* handled already. Find the next larger one: */
ferencd@0 132
ferencd@0 133 for (m = maxint, j = 0; j < input_length; ++j) {
ferencd@0 134 /* if (basic(input[j])) continue; */
ferencd@0 135 /* (not needed for Punycode) */
ferencd@0 136 if (input[j] >= n && input[j] < m) m = input[j];
ferencd@0 137 }
ferencd@0 138
ferencd@0 139 /* Increase delta enough to advance the decoder's */
ferencd@0 140 /* <n,i> state to <m,0>, but guard against overflow: */
ferencd@0 141
ferencd@0 142 if (m - n > (maxint - delta) / (h + 1)) return punycode_overflow;
ferencd@0 143 delta += (m - n) * (h + 1);
ferencd@0 144 n = m;
ferencd@0 145
ferencd@0 146 for (j = 0; j < input_length; ++j) {
ferencd@0 147 /* Punycode does not need to check whether input[j] is basic: */
ferencd@0 148 if (input[j] < n /* || basic(input[j]) */ ) {
ferencd@0 149 if (++delta == 0) return punycode_overflow;
ferencd@0 150 }
ferencd@0 151
ferencd@0 152 if (input[j] == n) {
ferencd@0 153 /* Represent delta as a generalized variable-length integer: */
ferencd@0 154
ferencd@0 155 for (q = delta, k = base; ; k += base) {
ferencd@0 156 if (out >= max_out) return punycode_big_output;
ferencd@0 157 t = k <= bias /* + tmin */ ? (punycode_uint) tmin : /* +tmin not needed */
ferencd@0 158 k >= (punycode_uint) bias + (punycode_uint) tmax ? (punycode_uint) tmax : k - (punycode_uint) bias;
ferencd@0 159 if (q < t) break;
ferencd@0 160 output[out++] = encode_digit(t + (q - t) % (base - t), 0);
ferencd@0 161 q = (q - t) / (base - t);
ferencd@0 162 }
ferencd@0 163
ferencd@0 164 output[out++] = encode_digit(q, case_flags && case_flags[j]);
ferencd@0 165 bias = adapt(delta, h + 1, h == b);
ferencd@0 166 delta = 0;
ferencd@0 167 ++h;
ferencd@0 168 }
ferencd@0 169 }
ferencd@0 170
ferencd@0 171 ++delta, ++n;
ferencd@0 172 }
ferencd@0 173
ferencd@0 174 *output_length = out;
ferencd@0 175 return punycode_success;
ferencd@0 176 }
ferencd@0 177
ferencd@0 178 /*** Main decode function ***/
ferencd@0 179
ferencd@0 180 enum punycode_status punycode_decode(
ferencd@0 181 punycode_uint input_length,
ferencd@0 182 const char input[],
ferencd@0 183 punycode_uint *output_length,
ferencd@0 184 punycode_uint output[],
ferencd@0 185 unsigned char case_flags[] )
ferencd@0 186 {
ferencd@0 187 punycode_uint n, out, i, max_out, bias,
ferencd@0 188 b, j, in, oldi, w, k, digit, t;
ferencd@0 189
ferencd@0 190 /* Initialize the state: */
ferencd@0 191
ferencd@0 192 n = initial_n;
ferencd@0 193 out = i = 0;
ferencd@0 194 max_out = *output_length;
ferencd@0 195 bias = initial_bias;
ferencd@0 196
ferencd@0 197 /* Handle the basic code points: Let b be the number of input code */
ferencd@0 198 /* points before the last delimiter, or 0 if there is none, then */
ferencd@0 199 /* copy the first b code points to the output. */
ferencd@0 200
ferencd@0 201 for (b = j = 0; j < input_length; ++j) if (delim(input[j])) b = j;
ferencd@0 202 if (b > max_out) return punycode_big_output;
ferencd@0 203
ferencd@0 204 for (j = 0; j < b; ++j) {
ferencd@0 205 if (case_flags) case_flags[out] = flagged(input[j]);
ferencd@0 206 if (!basic(input[j])) return punycode_bad_input;
ferencd@0 207 output[out++] = input[j];
ferencd@0 208 }
ferencd@0 209
ferencd@0 210 /* Main decoding loop: Start just after the last delimiter if any */
ferencd@0 211 /* basic code points were copied; start at the beginning otherwise. */
ferencd@0 212
ferencd@0 213 for (in = b > 0 ? b + 1 : 0; in < input_length; ++out) {
ferencd@0 214
ferencd@0 215 /* in is the index of the next character to be consumed, and */
ferencd@0 216 /* out is the number of code points in the output array. */
ferencd@0 217
ferencd@0 218 /* Decode a generalized variable-length integer into delta, */
ferencd@0 219 /* which gets added to i. The overflow checking is easier */
ferencd@0 220 /* if we increase i as we go, then subtract off its starting */
ferencd@0 221 /* value at the end to obtain delta. */
ferencd@0 222
ferencd@0 223 for (oldi = i, w = 1, k = base; ; k += base) {
ferencd@0 224 if (in >= input_length) return punycode_bad_input;
ferencd@0 225 digit = decode_digit(input[in++]);
ferencd@0 226 if (digit >= base) return punycode_bad_input;
ferencd@0 227 if (digit > (maxint - i) / w) return punycode_overflow;
ferencd@0 228 i += digit * w;
ferencd@0 229 t = k <= (punycode_uint) bias /* + tmin */ ? (punycode_uint) tmin : /* +tmin not needed */
ferencd@0 230 k >= (punycode_uint) bias + (punycode_uint) tmax ? (punycode_uint) tmax : k - (punycode_uint) bias;
ferencd@0 231 if (digit < t) break;
ferencd@0 232 if (w > maxint / (base - t)) return punycode_overflow;
ferencd@0 233 w *= (base - t);
ferencd@0 234 }
ferencd@0 235
ferencd@0 236 bias = adapt(i - oldi, out + 1, oldi == 0);
ferencd@0 237
ferencd@0 238 /* i was supposed to wrap around from out+1 to 0, */
ferencd@0 239 /* incrementing n each time, so we'll fix that now: */
ferencd@0 240
ferencd@0 241 if (i / (out + 1) > maxint - n) return punycode_overflow;
ferencd@0 242 n += i / (out + 1);
ferencd@0 243 i %= (out + 1);
ferencd@0 244
ferencd@0 245 /* Insert n at position i of the output: */
ferencd@0 246
ferencd@0 247 /* not needed for Punycode: */
ferencd@0 248 /* if (decode_digit(n) <= base) return punycode_invalid_input; */
ferencd@0 249 if (out >= max_out) return punycode_big_output;
ferencd@0 250
ferencd@0 251 if (case_flags) {
ferencd@0 252 memmove(case_flags + i + 1, case_flags + i, out - i);
ferencd@0 253 /* Case of last character determines uppercase flag: */
ferencd@0 254 case_flags[i] = flagged(input[in - 1]);
ferencd@0 255 }
ferencd@0 256
ferencd@0 257 memmove(output + i + 1, output + i, (out - i) * sizeof *output);
ferencd@0 258 output[i++] = n;
ferencd@0 259 }
ferencd@0 260
ferencd@0 261 *output_length = out;
ferencd@0 262 return punycode_success;
ferencd@0 263 }