Coverage Report

Created: 2022-11-21 06:06

/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
81.7k
#define punycode_uint uint32_t
68
15.5k
#define punycode_success IDN2_OK
69
0
#define punycode_overflow IDN2_PUNYCODE_OVERFLOW
70
341
#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
105k
#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
133k
{
100
133k
  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
133k
}
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
65.8k
{
120
65.8k
  punycode_uint k;
121
122
65.8k
  delta = firsttime ? delta / damp : delta >> 1;
123
  /* delta >> 1 is a faster way of doing delta / 2 */
124
65.8k
  delta += delta / numpoints;
125
126
78.6k
  for (k = 0; delta > ((base - tmin) * tmax) / 2; k += base)
127
12.8k
    {
128
12.8k
      delta /= base - tmin;
129
12.8k
    }
130
131
65.8k
  return k + (base - tmin + 1) * delta / (delta + skew);
132
65.8k
}
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
15.8k
{
141
15.8k
  punycode_uint input_length, n, delta, h, b, bias, j, m, q, k, t;
142
15.8k
  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
15.8k
  if (input_length_orig > maxint)
149
0
    return punycode_overflow;
150
15.8k
  input_length = (punycode_uint) input_length_orig;
151
152
  /* Initialize the state: */
153
154
15.8k
  n = initial_n;
155
15.8k
  delta = 0;
156
15.8k
  out = 0;
157
15.8k
  max_out = *output_length;
158
15.8k
  bias = initial_bias;
159
160
  /* Handle the basic code points: */
161
162
121k
  for (j = 0; j < input_length; ++j)
163
105k
    {
164
105k
      if (basic (input[j]))
165
15.5k
  {
166
15.5k
    if (max_out - out < 2)
167
48
      return punycode_big_output;
168
15.5k
    output[out++] = (char) input[j];
169
15.5k
  }
170
89.8k
      else if (input[j] > 0x10FFFF
171
89.8k
         || (input[j] >= 0xD800 && input[j] <= 0xDBFF))
172
0
  return punycode_bad_input;
173
105k
    }
174
175
15.8k
  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
15.8k
  if (b > 0)
183
3.41k
    output[out++] = delimiter;
184
185
  /* Main encoding loop: */
186
187
54.9k
  while (h < input_length)
188
39.4k
    {
189
      /* All non-basic code points < n have been     */
190
      /* handled already.  Find the next larger one: */
191
192
500k
      for (m = maxint, j = 0; j < input_length; ++j)
193
460k
  {
194
    /* if (basic(input[j])) continue; */
195
    /* (not needed for Punycode) */
196
460k
    if (input[j] >= n && input[j] < m)
197
67.4k
      m = input[j];
198
460k
  }
199
200
      /* Increase delta enough to advance the decoder's    */
201
      /* <n,i> state to <m,0>, but guard against overflow: */
202
203
39.4k
      if (m - n > (maxint - delta) / (h + 1))
204
0
  return punycode_overflow;
205
39.4k
      delta += (m - n) * (h + 1);
206
39.4k
      n = m;
207
208
479k
      for (j = 0; j < input_length; ++j)
209
439k
  {
210
    /* Punycode does not need to check whether input[j] is basic: */
211
439k
    if (input[j] < n /* || basic(input[j]) */ )
212
162k
      {
213
162k
        if (++delta == 0)
214
0
    return punycode_overflow;
215
162k
      }
216
217
439k
    if (input[j] == n)
218
66.1k
      {
219
        /* Represent delta as a generalized variable-length integer: */
220
221
67.3k
        for (q = delta, k = base;; k += base)
222
133k
    {
223
133k
      if (out >= max_out)
224
293
        return punycode_big_output;
225
133k
      t = k <= bias /* + tmin */ ? tmin :  /* +tmin not needed */
226
133k
        k >= bias + tmax ? tmax : k - bias;
227
133k
      if (q < t)
228
65.8k
        break;
229
67.3k
      output[out++] = encode_digit (t + (q - t) % (base - t), 0);
230
67.3k
      q = (q - t) / (base - t);
231
67.3k
    }
232
233
65.8k
        output[out++] = encode_digit (q, 0);
234
65.8k
        bias = adapt (delta, h + 1, h == b);
235
65.8k
        delta = 0;
236
65.8k
        ++h;
237
65.8k
      }
238
439k
  }
239
240
39.1k
      ++delta, ++n;
241
39.1k
    }
242
243
15.5k
  *output_length = out;
244
15.5k
  return punycode_success;
245
15.8k
}
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