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