File: idna.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 (296 lines) | stat: -rw-r--r-- 6,622 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
/* idna.c - implementation of high-level IDNA processing function
   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 <stdlib.h>		/* free */
#include <errno.h>		/* errno */

#include "idn2.h"
#include "bidi.h"
#include "tables.h"
#include "context.h"
#include "tr46map.h"

#include <unitypes.h>
#include <unictype.h>		/* uc_is_general_category, UC_CATEGORY_M */
#include <uninorm.h>		/* u32_normalize */
#include <unistr.h>		/* u8_to_u32 */

#include "idna.h"

/*
 * NFC Quick Check from
 * http://unicode.org/reports/tr15/#Detecting_Normalization_Forms
 *
 * They say, this is much faster than 'brute force' normalization.
 * Strings are very likely already in NFC form.
 */
G_GNUC_IDN2_ATTRIBUTE_PURE static int
_isNFC (uint32_t *label, size_t len)
{
  int lastCanonicalClass = 0;
  int result = 1;
  size_t it;

  for (it = 0; it < len; it++)
    {
      uint32_t ch = label[it];

      // supplementary code point
      if (ch >= 0x10000)
	it++;

      int canonicalClass = uc_combining_class (ch);
      if (lastCanonicalClass > canonicalClass && canonicalClass != 0)
	return 0;

      NFCQCMap *map = get_nfcqc_map (ch);
      if (map)
	{
	  if (map->check)
	    return 0;
	  result = -1;
	}

      lastCanonicalClass = canonicalClass;
    }

  return result;
}

int
_idn2_u8_to_u32_nfc (const uint8_t *src, size_t srclen,
		     uint32_t **out, size_t *outlen, int nfc)
{
  uint32_t *p;
  size_t plen;

  p = u8_to_u32 (src, srclen, NULL, &plen);
  if (p == NULL)
    {
      if (errno == ENOMEM)
	return IDN2_MALLOC;
      return IDN2_ENCODING_ERROR;
    }

  if (nfc && !_isNFC (p, plen))
    {
      size_t tmplen;
      uint32_t *tmp = u32_normalize (UNINORM_NFC, p, plen, NULL, &tmplen);
      free (p);
      if (tmp == NULL)
	{
	  if (errno == ENOMEM)
	    return IDN2_MALLOC;
	  return IDN2_NFC;
	}

      p = tmp;
      plen = tmplen;
    }

  *out = p;
  *outlen = plen;
  return IDN2_OK;
}

bool
_idn2_ascii_p (const uint8_t *src, size_t srclen)
{
  size_t i;

  for (i = 0; i < srclen; i++)
    if (src[i] >= 0x80)
      return false;

  return true;
}

int
_idn2_label_test (int what, const uint32_t *label, size_t llen)
{
  if (what & TEST_NFC)
    {
      size_t plen;
      uint32_t *p = u32_normalize (UNINORM_NFC, label, llen,
				   NULL, &plen);
      int ok;
      if (p == NULL)
	{
	  if (errno == ENOMEM)
	    return IDN2_MALLOC;
	  return IDN2_NFC;
	}
      ok = llen == plen && memcmp (label, p, plen * sizeof (*label)) == 0;
      free (p);
      if (!ok)
	return IDN2_NOT_NFC;
    }

  if (what & TEST_2HYPHEN)
    {
      if (llen >= 4 && label[2] == '-' && label[3] == '-')
	return IDN2_2HYPHEN;
    }

  if (what & TEST_HYPHEN_STARTEND)
    {
      if (llen > 0 && (label[0] == '-' || label[llen - 1] == '-'))
	return IDN2_HYPHEN_STARTEND;
    }

  if (what & TEST_LEADING_COMBINING)
    {
      if (llen > 0 && uc_is_general_category (label[0], UC_CATEGORY_M))
	return IDN2_LEADING_COMBINING;
    }

  if (what & TEST_DISALLOWED)
    {
      size_t i;
      for (i = 0; i < llen; i++)
	if (_idn2_disallowed_p (label[i]))
	  {
	    if ((what & (TEST_TRANSITIONAL | TEST_NONTRANSITIONAL)) &&
		(what & TEST_ALLOW_STD3_DISALLOWED))
	      {
		IDNAMap map;
		get_idna_map (label[i], &map);
		if (map_is (&map, TR46_FLG_DISALLOWED_STD3_VALID) ||
		    map_is (&map, TR46_FLG_DISALLOWED_STD3_MAPPED))
		  continue;

	      }

	    return IDN2_DISALLOWED;
	  }
    }

  if (what & TEST_CONTEXTJ)
    {
      size_t i;
      for (i = 0; i < llen; i++)
	if (_idn2_contextj_p (label[i]))
	  return IDN2_CONTEXTJ;
    }

  if (what & TEST_CONTEXTJ_RULE)
    {
      size_t i;
      int rc;

      for (i = 0; i < llen; i++)
	{
	  rc = _idn2_contextj_rule (label, llen, i);
	  if (rc != IDN2_OK)
	    return rc;
	}
    }

  if (what & TEST_CONTEXTO)
    {
      size_t i;
      for (i = 0; i < llen; i++)
	if (_idn2_contexto_p (label[i]))
	  return IDN2_CONTEXTO;
    }

  if (what & TEST_CONTEXTO_WITH_RULE)
    {
      size_t i;
      for (i = 0; i < llen; i++)
	if (_idn2_contexto_p (label[i])
	    && !_idn2_contexto_with_rule (label[i]))
	  return IDN2_CONTEXTO_NO_RULE;
    }

  if (what & TEST_CONTEXTO_RULE)
    {
      size_t i;
      int rc;

      for (i = 0; i < llen; i++)
	{
	  rc = _idn2_contexto_rule (label, llen, i);
	  if (rc != IDN2_OK)
	    return rc;
	}
    }

  if (what & TEST_UNASSIGNED)
    {
      size_t i;
      for (i = 0; i < llen; i++)
	if (_idn2_unassigned_p (label[i]))
	  return IDN2_UNASSIGNED;
    }

  if (what & TEST_BIDI)
    {
      int rc = _idn2_bidi (label, llen);
      if (rc != IDN2_OK)
	return rc;
    }

  if (what & (TEST_TRANSITIONAL | TEST_NONTRANSITIONAL))
    {
      size_t i;
      int transitional = what & TEST_TRANSITIONAL;

      /* TR46: 4. The label must not contain a U+002E ( . ) FULL STOP */
      for (i = 0; i < llen; i++)
	if (label[i] == 0x002E)
	  return IDN2_DOT_IN_LABEL;

      /* TR46: 6. Each code point in the label must only have certain status
       * values according to Section 5, IDNA Mapping Table:
       *    a. For Transitional Processing, each value must be valid.
       *    b. For Nontransitional Processing, each value must be either valid or deviation. */
      for (i = 0; i < llen; i++)
	{
	  IDNAMap map;

	  get_idna_map (label[i], &map);

	  if (map_is (&map, TR46_FLG_VALID) ||
	      (!transitional && map_is (&map, TR46_FLG_DEVIATION)))
	    continue;

	  if (what & TEST_ALLOW_STD3_DISALLOWED &&
	      (map_is (&map, TR46_FLG_DISALLOWED_STD3_VALID) ||
	       map_is (&map, TR46_FLG_DISALLOWED_STD3_MAPPED)))
	    continue;

	  return transitional ? IDN2_INVALID_TRANSITIONAL :
	    IDN2_INVALID_NONTRANSITIONAL;
	}
    }

  return IDN2_OK;
}