Coverage Report

Created: 2026-12-01 06:28

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