Coverage Report

Created: 2023-06-07 07:18

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