File: decode.c

package info (click to toggle)
libidn2 2.3.8-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,768 kB
  • sloc: ansic: 6,049; sh: 1,480; makefile: 499; xml: 50; perl: 15
file content (446 lines) | stat: -rw-r--r-- 11,260 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/* decode.c - implementation of IDNA2008 decoding functions
   Copyright (C) 2011-2025 Simon Josefsson

   Libidn2 is free software: you can redistribute it and/or modify it
   under the terms of either:

     * the GNU Lesser General Public License as published by the Free
       Software Foundation; either version 3 of the License, or (at
       your option) any later version.

   or

     * the GNU General Public License as published by the Free
       Software Foundation; either version 2 of the License, or (at
       your option) any later version.

   or both in parallel, as here.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received copies of the GNU General Public License and
   the GNU Lesser General Public License along with this program.  If
   not, see <http://www.gnu.org/licenses/>.
*/

#include <config.h>

#include "idn2.h"

#include <errno.h>		/* errno */
#include <stdlib.h>		/* malloc, free */

#include <unitypes.h>
#include <uniconv.h>		/* u8_strconv_from_locale */
#include <unistr.h>		/* u8_to_u32, u32_cpy, ... */

/**
 * idn2_to_unicode_8z4z:
 * @input: Input zero-terminated UTF-8 string.
 * @output: Newly allocated UTF-32/UCS-4 output string.
 * @flags: Currently unused.
 *
 * Converts a possibly ACE encoded domain name in UTF-8 format into a
 * UTF-32 string (punycode decoding). The output buffer will be zero-terminated
 * and must be deallocated by the caller.
 *
 * @output may be NULL to test lookup of @input without allocating memory.
 *
 * Returns:
 *   %IDN2_OK: The conversion was successful.
 *   %IDN2_TOO_BIG_DOMAIN: The domain is too long.
 *   %IDN2_TOO_BIG_LABEL: A label is would have been too long.
 *   %IDN2_ENCODING_ERROR: Character conversion failed.
 *   %IDN2_MALLOC: Memory allocation failed.
 *
 * Since: 2.0.0
 **/
int
idn2_to_unicode_8z4z (const char *input, uint32_t **output,
		      G_GNUC_UNUSED int flags)
{
  uint32_t *domain_u32;
  int rc;

  if (!input)
    {
      if (output)
	*output = NULL;
      return IDN2_OK;
    }

  /* split into labels and check */
  uint32_t out_u32[IDN2_DOMAIN_MAX_LENGTH + 1];
  size_t out_len = 0;
  const char *e, *s;

  for (e = s = input; *e; s = e)
    {
      uint32_t label_u32[IDN2_LABEL_MAX_LENGTH];
      size_t label_len = IDN2_LABEL_MAX_LENGTH;

      while (*e && *e != '.')
	e++;

      if (e - s >= 4 && (s[0] == 'x' || s[0] == 'X')
	  && (s[1] == 'n' || s[1] == 'N') && s[2] == '-' && s[3] == '-')
	{
	  s += 4;

	  rc = idn2_punycode_decode ((char *) s, e - s,
				     label_u32, &label_len);
	  if (rc)
	    return rc;

	  if (out_len + label_len + (*e == '.') > IDN2_DOMAIN_MAX_LENGTH)
	    return IDN2_TOO_BIG_DOMAIN;

	  u32_cpy (out_u32 + out_len, label_u32, label_len);
	}
      else
	{
	  /* convert UTF-8 input to UTF-32 */
	  if (!
	      (domain_u32 =
	       u8_to_u32 ((uint8_t *) s, e - s, NULL, &label_len)))
	    {
	      if (errno == ENOMEM)
		return IDN2_MALLOC;
	      return IDN2_ENCODING_ERROR;
	    }

	  if (label_len > IDN2_LABEL_MAX_LENGTH)
	    {
	      free (domain_u32);
	      return IDN2_TOO_BIG_LABEL;
	    }

	  if (out_len + label_len + (*e == '.') > IDN2_DOMAIN_MAX_LENGTH)
	    {
	      free (domain_u32);
	      return IDN2_TOO_BIG_DOMAIN;
	    }

	  u32_cpy (out_u32 + out_len, domain_u32, label_len);
	  free (domain_u32);
	}

      out_len += label_len;
      if (*e)
	{
	  out_u32[out_len++] = '.';
	  e++;
	}
    }

  if (output)
    {
      uint32_t *_out;

      out_u32[out_len] = 0;

      _out = u32_cpy_alloc (out_u32, out_len + 1);
      if (!_out)
	{
	  if (errno == ENOMEM)
	    return IDN2_MALLOC;
	  return IDN2_ENCODING_ERROR;
	}

      *output = _out;
    }

  return IDN2_OK;
}

/**
 * idn2_to_unicode_4z4z:
 * @input: Input zero-terminated UTF-32 string.
 * @output: Newly allocated UTF-32 output string.
 * @flags: Currently unused.
 *
 * Converts a possibly ACE encoded domain name in UTF-32 format into a
 * UTF-32 string (punycode decoding). The output buffer will be zero-terminated
 * and must be deallocated by the caller.
 *
 * @output may be NULL to test lookup of @input without allocating memory.
 *
 * Returns:
 *   %IDN2_OK: The conversion was successful.
 *   %IDN2_TOO_BIG_DOMAIN: The domain is too long.
 *   %IDN2_TOO_BIG_LABEL: A label is would have been too long.
 *   %IDN2_ENCODING_ERROR: Character conversion failed.
 *   %IDN2_MALLOC: Memory allocation failed.
 *
 * Since: 2.0.0
 **/
int
idn2_to_unicode_4z4z (const uint32_t *input, uint32_t **output, int flags)
{
  uint8_t *input_u8;
  uint32_t *output_u32;
  size_t length;
  int rc;

  if (!input)
    {
      if (output)
	*output = NULL;
      return IDN2_OK;
    }

  input_u8 = u32_to_u8 (input, u32_strlen (input) + 1, NULL, &length);
  if (!input_u8)
    {
      if (errno == ENOMEM)
	return IDN2_MALLOC;
      return IDN2_ENCODING_ERROR;
    }

  rc = idn2_to_unicode_8z4z ((char *) input_u8, &output_u32, flags);
  free (input_u8);

  if (rc == IDN2_OK)
    {
      if (output)
	*output = output_u32;
      else
	free (output_u32);
    }

  return rc;
}

/**
 * idn2_to_unicode_44i:
 * @in: Input array with UTF-32 code points.
 * @inlen: number of code points of input array
 * @out: output array with UTF-32 code points.
 * @outlen: on input, maximum size of output array with UTF-32 code points,
 *          on exit, actual size of output array with UTF-32 code points.
 * @flags: Currently unused.
 *
 * The ToUnicode operation takes a sequence of UTF-32 code points
 * that make up one domain label and returns a sequence of UTF-32
 * code points. If the input sequence is a label in ACE form, then the
 * result is an equivalent internationalized label that is not in ACE
 * form, otherwise the original sequence is returned unaltered.
 *
 * @output may be NULL to test lookup of @input without allocating memory.
 *
 * Returns:
 *   %IDN2_OK: The conversion was successful.
 *   %IDN2_TOO_BIG_DOMAIN: The domain is too long.
 *   %IDN2_TOO_BIG_LABEL: A label is would have been too long.
 *   %IDN2_ENCODING_ERROR: Character conversion failed.
 *   %IDN2_MALLOC: Memory allocation failed.
 *
 * Since: 2.0.0
 **/
int
idn2_to_unicode_44i (const uint32_t *in, size_t inlen, uint32_t *out,
		     size_t *outlen, int flags)
{
  uint32_t *input_u32;
  uint32_t *output_u32;
  size_t len;
  int rc;

  if (!in)
    {
      if (outlen)
	*outlen = 0;
      return IDN2_OK;
    }

  input_u32 = (uint32_t *) malloc ((inlen + 1) * sizeof (uint32_t));
  if (!input_u32)
    return IDN2_MALLOC;

  u32_cpy (input_u32, in, inlen);
  input_u32[inlen] = 0;

  rc = idn2_to_unicode_4z4z (input_u32, &output_u32, flags);
  free (input_u32);
  if (rc != IDN2_OK)
    return rc;

  len = u32_strlen (output_u32);
  if (out && outlen)
    u32_cpy (out, output_u32, len < *outlen ? len : *outlen);
  free (output_u32);

  if (outlen)
    *outlen = len;

  return IDN2_OK;
}

/**
 * idn2_to_unicode_8z8z:
 * @input: Input zero-terminated UTF-8 string.
 * @output: Newly allocated UTF-8 output string.
 * @flags: Currently unused.
 *
 * Converts a possibly ACE encoded domain name in UTF-8 format into a
 * UTF-8 string (punycode decoding). The output buffer will be zero-terminated
 * and must be deallocated by the caller.
 *
 * @output may be NULL to test lookup of @input without allocating memory.
 *
 * Returns:
 *   %IDN2_OK: The conversion was successful.
 *   %IDN2_TOO_BIG_DOMAIN: The domain is too long.
 *   %IDN2_TOO_BIG_LABEL: A label is would have been too long.
 *   %IDN2_ENCODING_ERROR: Character conversion failed.
 *   %IDN2_MALLOC: Memory allocation failed.
 *
 * Since: 2.0.0
 **/
int
idn2_to_unicode_8z8z (const char *input, char **output, int flags)
{
  uint32_t *output_u32;
  uint8_t *output_u8;
  size_t length;
  int rc;

  rc = idn2_to_unicode_8z4z (input, &output_u32, flags);
  if (rc != IDN2_OK || !input)
    return rc;

  output_u8 =
    u32_to_u8 (output_u32, u32_strlen (output_u32) + 1, NULL, &length);
  free (output_u32);

  if (!output_u8)
    {
      if (errno == ENOMEM)
	return IDN2_MALLOC;
      return IDN2_ENCODING_ERROR;
    }

  if (output)
    *output = (char *) output_u8;
  else
    free (output_u8);

  return IDN2_OK;
}

/**
 * idn2_to_unicode_8zlz:
 * @input: Input zero-terminated UTF-8 string.
 * @output: Newly allocated output string in current locale's character set.
 * @flags: Currently unused.
 *
 * Converts a possibly ACE encoded domain name in UTF-8 format into a
 * string encoded in the current locale's character set (punycode
 * decoding). The output buffer will be zero-terminated and must be
 * deallocated by the caller.
 *
 * @output may be NULL to test lookup of @input without allocating memory.
 *
 * Returns:
 *   %IDN2_OK: The conversion was successful.
 *   %IDN2_TOO_BIG_DOMAIN: The domain is too long.
 *   %IDN2_TOO_BIG_LABEL: A label is would have been too long.
 *   %IDN2_ENCODING_ERROR: Character conversion failed.
 *   %IDN2_MALLOC: Memory allocation failed.
 *
 * Since: 2.0.0
 **/
int
idn2_to_unicode_8zlz (const char *input, char **output, int flags)
{
  int rc;
  uint8_t *output_u8, *output_l8;
  const char *encoding;

  rc = idn2_to_unicode_8z8z (input, (char **) &output_u8, flags);
  if (rc != IDN2_OK || !input)
    return rc;

  encoding = locale_charset ();
  output_l8 =
    (uint8_t *) u8_strconv_to_encoding (output_u8, encoding, iconveh_error);

  if (!output_l8)
    {
      if (errno == ENOMEM)
	rc = IDN2_MALLOC;
      else
	rc = IDN2_ENCODING_ERROR;

      free (output_l8);
    }
  else
    {
      if (output)
	*output = (char *) output_l8;
      else
	free (output_l8);

      rc = IDN2_OK;
    }

  free (output_u8);

  return rc;
}

/**
 * idn2_to_unicode_lzlz:
 * @input: Input zero-terminated string encoded in the current locale's character set.
 * @output: Newly allocated output string in current locale's character set.
 * @flags: Currently unused.
 *
 * Converts a possibly ACE encoded domain name in the locale's character
 * set into a string encoded in the current locale's character set (punycode
 * decoding). The output buffer will be zero-terminated and must be
 * deallocated by the caller.
 *
 * @output may be NULL to test lookup of @input without allocating memory.
 *
 * Returns:
 *   %IDN2_OK: The conversion was successful.
 *   %IDN2_TOO_BIG_DOMAIN: The domain is too long.
 *   %IDN2_TOO_BIG_LABEL: A label is would have been too long.
 *   %IDN2_ENCODING_ERROR: Output character conversion failed.
 *   %IDN2_ICONV_FAIL: Input character conversion failed.
 *   %IDN2_MALLOC: Memory allocation failed.
 *
 * Since: 2.0.0
 **/
int
idn2_to_unicode_lzlz (const char *input, char **output, int flags)
{
  uint8_t *input_l8;
  const char *encoding;
  int rc;

  if (!input)
    {
      if (output)
	*output = NULL;
      return IDN2_OK;
    }

  encoding = locale_charset ();
  input_l8 = u8_strconv_from_encoding (input, encoding, iconveh_error);

  if (!input_l8)
    {
      if (errno == ENOMEM)
	return IDN2_MALLOC;
      return IDN2_ICONV_FAIL;
    }

  rc = idn2_to_unicode_8zlz ((char *) input_l8, output, flags);
  free (input_l8);

  return rc;
}