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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
|
/* tld.c --- Declarations for TLD restriction checking.
Copyright (C) 2004-2024 Simon Josefsson.
Copyright (C) 2003-2024 Free Software Foundation, Inc.
Author: Thomas Jacob, Internet24.de
This file is part of GNU Libidn.
GNU Libidn 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.
GNU Libidn 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 <https://www.gnu.org/licenses/>. */
#include <config.h>
/* Get stringprep_utf8_to_ucs4, stringprep_locale_to_utf8. */
#include <stringprep.h>
/* Get strcmp(). */
#include <string.h>
/* Get specifications. */
#include <tld.h>
/* Array of built-in domain restriction structures. See tlds.c. */
extern const Tld_table *_tld_tables[];
/**
* tld_get_table:
* @tld: TLD name (e.g. "com") as zero terminated ASCII byte string.
* @tables: Zero terminated array of #Tld_table info-structures for
* TLDs.
*
* Get the TLD table for a named TLD by searching through the given
* TLD table array.
*
* Return value: Return structure corresponding to TLD @tld by going
* thru @tables, or return %NULL if no such structure is found.
*/
const Tld_table *
tld_get_table (const char *tld, const Tld_table **tables)
{
const Tld_table **tldtable = NULL;
if (!tld || !tables)
return NULL;
for (tldtable = tables; *tldtable; tldtable++)
if (!strcmp ((*tldtable)->name, tld))
return *tldtable;
return NULL;
}
/**
* tld_default_table:
* @tld: TLD name (e.g. "com") as zero terminated ASCII byte string.
* @overrides: Additional zero terminated array of #Tld_table
* info-structures for TLDs, or %NULL to only use library default
* tables.
*
* Get the TLD table for a named TLD, using the internal defaults,
* possibly overridden by the (optional) supplied tables.
*
* Return value: Return structure corresponding to TLD @tld_str, first
* looking through @overrides then thru built-in list, or %NULL if
* no such structure found.
*/
const Tld_table *
tld_default_table (const char *tld, const Tld_table **overrides)
{
const Tld_table *tldtable = NULL;
if (!tld)
return NULL;
if (overrides)
tldtable = tld_get_table (tld, overrides);
if (!tldtable)
tldtable = tld_get_table (tld, _tld_tables);
return tldtable;
}
#define DOTP(c) ((c) == 0x002E || (c) == 0x3002 || \
(c) == 0xFF0E || (c) == 0xFF61)
/**
* tld_get_4:
* @in: Array of unicode code points to process. Does not need to be
* zero terminated.
* @inlen: Number of unicode code points.
* @out: Zero terminated ascii result string pointer.
*
* Isolate the top-level domain of @in and return it as an ASCII
* string in @out.
*
* Return value: Return %TLD_SUCCESS on success, or the corresponding
* #Tld_rc error code otherwise.
*/
int
tld_get_4 (const uint32_t *in, size_t inlen, char **out)
{
const uint32_t *ipos;
size_t olen;
*out = NULL;
if (!in || inlen == 0)
return TLD_NODATA;
ipos = &in[inlen - 1];
olen = 0;
/* Scan backwards for non(latin)letters. */
while (ipos >= in && ((*ipos >= 0x41 && *ipos <= 0x5A) ||
(*ipos >= 0x61 && *ipos <= 0x7A)))
ipos--, olen++;
if (olen > 0 && ipos >= in && DOTP (*ipos))
{
/* Found something that appears a TLD. */
char *out_s = malloc (sizeof (char) * (olen + 1));
char *opos = out_s;
if (!opos)
return TLD_MALLOC_ERROR;
ipos++;
/* Transcribe to lowercase ascii string. */
for (; ipos < &in[inlen]; ipos++, opos++)
*opos = *ipos > 0x5A ? *ipos : *ipos + 0x20;
*opos = 0;
*out = out_s;
return TLD_SUCCESS;
}
return TLD_NO_TLD;
}
/**
* tld_get_4z:
* @in: Zero terminated array of unicode code points to process.
* @out: Zero terminated ascii result string pointer.
*
* Isolate the top-level domain of @in and return it as an ASCII
* string in @out.
*
* Return value: Return %TLD_SUCCESS on success, or the corresponding
* #Tld_rc error code otherwise.
*/
int
tld_get_4z (const uint32_t *in, char **out)
{
const uint32_t *ipos = in;
if (!in)
return TLD_NODATA;
while (*ipos)
ipos++;
return tld_get_4 (in, ipos - in, out);
}
/**
* tld_get_z:
* @in: Zero terminated character array to process.
* @out: Zero terminated ascii result string pointer.
*
* Isolate the top-level domain of @in and return it as an ASCII
* string in @out. The input string @in may be UTF-8, ISO-8859-1 or
* any ASCII compatible character encoding.
*
* Return value: Return %TLD_SUCCESS on success, or the corresponding
* #Tld_rc error code otherwise.
*/
int
tld_get_z (const char *in, char **out)
{
uint32_t *iucs;
size_t i, ilen;
int rc;
ilen = strlen (in);
iucs = calloc (ilen, sizeof (*iucs));
if (!iucs)
return TLD_MALLOC_ERROR;
for (i = 0; i < ilen; i++)
iucs[i] = in[i];
rc = tld_get_4 (iucs, ilen, out);
free (iucs);
return rc;
}
/*
* tld_checkchar - verify that character is permitted
* @ch: 32 bit unicode character to check.
* @tld: A #Tld_table data structure to check @ch against.
*
* Verify if @ch is either in [a-z0-9-.] or mentioned as a valid
* character in @tld.
*
* Return value: Return the #Tld_rc value %TLD_SUCCESS if @ch is a
* valid character for the TLD @tld or if @tld is %NULL,
* %TLD_INVALID if @ch is invalid as defined by @tld.
*/
static int
_tld_checkchar (uint32_t ch, const Tld_table *tld)
{
const Tld_table_element *s, *e, *m;
if (!tld)
return TLD_SUCCESS;
/* Check for [-a-z0-9.]. */
if ((ch >= 0x61 && ch <= 0x7A) ||
(ch >= 0x30 && ch <= 0x39) || ch == 0x2D || DOTP (ch))
return TLD_SUCCESS;
s = tld->valid;
e = s + tld->nvalid;
while (s < e)
{
m = s + ((e - s) >> 1);
if (ch < m->start)
e = m;
else if (ch > m->end)
s = m + 1;
else
return TLD_SUCCESS;
}
return TLD_INVALID;
}
/**
* tld_check_4t:
* @in: Array of unicode code points to process. Does not need to be
* zero terminated.
* @inlen: Number of unicode code points.
* @errpos: Position of offending character is returned here.
* @tld: A #Tld_table data structure representing the restrictions for
* which the input should be tested.
*
* Test each of the code points in @in for whether or not
* they are allowed by the data structure in @tld, return
* the position of the first character for which this is not
* the case in @errpos.
*
* Return value: Returns the #Tld_rc value %TLD_SUCCESS if all code
* points are valid or when @tld is null, %TLD_INVALID if a
* character is not allowed, or additional error codes on general
* failure conditions.
*/
int
tld_check_4t (const uint32_t *in, size_t inlen, size_t *errpos,
const Tld_table *tld)
{
const uint32_t *ipos;
int rc;
if (!tld) /* No data for TLD so everything is valid. */
return TLD_SUCCESS;
ipos = in;
while (ipos < &in[inlen])
{
rc = _tld_checkchar (*ipos, tld);
if (rc != TLD_SUCCESS)
{
if (errpos)
*errpos = ipos - in;
return rc;
}
ipos++;
}
return TLD_SUCCESS;
}
/**
* tld_check_4tz:
* @in: Zero terminated array of unicode code points to process.
* @errpos: Position of offending character is returned here.
* @tld: A #Tld_table data structure representing the restrictions for
* which the input should be tested.
*
* Test each of the code points in @in for whether or not
* they are allowed by the data structure in @tld, return
* the position of the first character for which this is not
* the case in @errpos.
*
* Return value: Returns the #Tld_rc value %TLD_SUCCESS if all code
* points are valid or when @tld is null, %TLD_INVALID if a
* character is not allowed, or additional error codes on general
* failure conditions.
*/
int
tld_check_4tz (const uint32_t *in, size_t *errpos, const Tld_table *tld)
{
const uint32_t *ipos = in;
if (!ipos)
return TLD_NODATA;
while (*ipos)
ipos++;
return tld_check_4t (in, ipos - in, errpos, tld);
}
/**
* tld_check_4:
* @in: Array of unicode code points to process. Does not need to be
* zero terminated.
* @inlen: Number of unicode code points.
* @errpos: Position of offending character is returned here.
* @overrides: A #Tld_table array of additional domain restriction
* structures that complement and supersede the built-in information.
*
* Test each of the code points in @in for whether or not they are
* allowed by the information in @overrides or by the built-in TLD
* restriction data. When data for the same TLD is available both
* internally and in @overrides, the information in @overrides takes
* precedence. If several entries for a specific TLD are found, the
* first one is used. If @overrides is %NULL, only the built-in
* information is used. The position of the first offending character
* is returned in @errpos.
*
* Return value: Returns the #Tld_rc value %TLD_SUCCESS if all code
* points are valid or when @tld is null, %TLD_INVALID if a
* character is not allowed, or additional error codes on general
* failure conditions.
*/
int
tld_check_4 (const uint32_t *in, size_t inlen, size_t *errpos,
const Tld_table **overrides)
{
const Tld_table *tld;
char *domain;
int rc;
if (errpos)
*errpos = 0;
/* Get TLD name. */
rc = tld_get_4 (in, inlen, &domain);
if (rc != TLD_SUCCESS)
{
if (rc == TLD_NO_TLD) /* No TLD, say OK */
return TLD_SUCCESS;
else
return rc;
}
/* Retrieve appropriate data structure. */
tld = tld_default_table (domain, overrides);
free (domain);
return tld_check_4t (in, inlen, errpos, tld);
}
/**
* tld_check_4z:
* @in: Zero-terminated array of unicode code points to process.
* @errpos: Position of offending character is returned here.
* @overrides: A #Tld_table array of additional domain restriction
* structures that complement and supersede the built-in information.
*
* Test each of the code points in @in for whether or not they are
* allowed by the information in @overrides or by the built-in TLD
* restriction data. When data for the same TLD is available both
* internally and in @overrides, the information in @overrides takes
* precedence. If several entries for a specific TLD are found, the
* first one is used. If @overrides is %NULL, only the built-in
* information is used. The position of the first offending character
* is returned in @errpos.
*
* Return value: Returns the #Tld_rc value %TLD_SUCCESS if all code
* points are valid or when @tld is null, %TLD_INVALID if a
* character is not allowed, or additional error codes on general
* failure conditions.
*/
int
tld_check_4z (const uint32_t *in, size_t *errpos, const Tld_table **overrides)
{
const uint32_t *ipos = in;
if (!ipos)
return TLD_NODATA;
while (*ipos)
ipos++;
return tld_check_4 (in, ipos - in, errpos, overrides);
}
/**
* tld_check_8z:
* @in: Zero-terminated UTF8 string to process.
* @errpos: Position of offending character is returned here.
* @overrides: A #Tld_table array of additional domain restriction
* structures that complement and supersede the built-in information.
*
* Test each of the characters in @in for whether or not they are
* allowed by the information in @overrides or by the built-in TLD
* restriction data. When data for the same TLD is available both
* internally and in @overrides, the information in @overrides takes
* precedence. If several entries for a specific TLD are found, the
* first one is used. If @overrides is %NULL, only the built-in
* information is used. The position of the first offending character
* is returned in @errpos. Note that the error position refers to the
* decoded character offset rather than the byte position in the
* string.
*
* Return value: Returns the #Tld_rc value %TLD_SUCCESS if all
* characters are valid or when @tld is null, %TLD_INVALID if a
* character is not allowed, or additional error codes on general
* failure conditions.
*/
int
tld_check_8z (const char *in, size_t *errpos, const Tld_table **overrides)
{
uint32_t *iucs;
size_t ilen;
int rc;
if (!in)
return TLD_NODATA;
iucs = stringprep_utf8_to_ucs4 (in, -1, &ilen);
if (!iucs)
return TLD_MALLOC_ERROR;
rc = tld_check_4 (iucs, ilen, errpos, overrides);
free (iucs);
return rc;
}
/**
* tld_check_lz:
* @in: Zero-terminated string in the current locales encoding to process.
* @errpos: Position of offending character is returned here.
* @overrides: A #Tld_table array of additional domain restriction
* structures that complement and supersede the built-in information.
*
* Test each of the characters in @in for whether or not they are
* allowed by the information in @overrides or by the built-in TLD
* restriction data. When data for the same TLD is available both
* internally and in @overrides, the information in @overrides takes
* precedence. If several entries for a specific TLD are found, the
* first one is used. If @overrides is %NULL, only the built-in
* information is used. The position of the first offending character
* is returned in @errpos. Note that the error position refers to the
* decoded character offset rather than the byte position in the
* string.
*
* Return value: Returns the #Tld_rc value %TLD_SUCCESS if all
* characters are valid or when @tld is null, %TLD_INVALID if a
* character is not allowed, or additional error codes on general
* failure conditions.
*/
int
tld_check_lz (const char *in, size_t *errpos, const Tld_table **overrides)
{
char *utf8;
int rc;
if (!in)
return TLD_NODATA;
utf8 = stringprep_locale_to_utf8 (in);
if (!utf8)
return TLD_ICONV_ERROR;
rc = tld_check_8z (utf8, errpos, overrides);
free (utf8);
return rc;
}
/**
* Tld_rc:
* @TLD_SUCCESS: Successful operation. This value is guaranteed to
* always be zero, the remaining ones are only guaranteed to hold
* non-zero values, for logical comparison purposes.
* @TLD_INVALID: Invalid character found.
* @TLD_NODATA: No input data was provided.
* @TLD_MALLOC_ERROR: Error during memory allocation.
* @TLD_ICONV_ERROR: Character encoding conversion error.
* @TLD_NO_TLD: No top-level domain found in domain string.
* @TLD_NOTLD: Same as @TLD_NO_TLD, for compatibility
* with typo in earlier versions.
*
* Enumerated return codes of the TLD checking functions.
* The value 0 is guaranteed to always correspond to success.
*/
|