teptris 0.3.29.0 → 0.3.31.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/ext/teptris_ext/engine/README.md +1 -1
- data/ext/teptris_ext/engine/src/include/teptris/version.h +1 -1
- data/ext/teptris_ext/engine/src/teptris/common/ryu/s2d.c +14 -0
- data/ext/teptris_ext/engine/src/teptris/schema/formats.c +1118 -119
- data/ext/teptris_ext/engine/src/teptris/schema/idna_tables.h +14070 -0
- data/ext/teptris_ext/engine/src/teptris/schema/regex.c +5 -0
- data/lib/teptris/version.rb +1 -1
- metadata +3 -2
|
@@ -9,6 +9,8 @@
|
|
|
9
9
|
#include <stdlib.h>
|
|
10
10
|
#include <string.h>
|
|
11
11
|
|
|
12
|
+
#include "idna_tables.h"
|
|
13
|
+
|
|
12
14
|
#include "schema_internal.h"
|
|
13
15
|
|
|
14
16
|
static bool is_digit(char c) { return c >= '0' && c <= '9'; }
|
|
@@ -47,11 +49,6 @@ static size_t cp_count(const char *s, size_t len) {
|
|
|
47
49
|
return count;
|
|
48
50
|
}
|
|
49
51
|
|
|
50
|
-
static bool is_ascii_space(char c) {
|
|
51
|
-
return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\v' ||
|
|
52
|
-
c == '\f';
|
|
53
|
-
}
|
|
54
|
-
|
|
55
52
|
/* ------------------------------------------------- date / time atoms -- */
|
|
56
53
|
|
|
57
54
|
static bool leap_year(int y) {
|
|
@@ -81,7 +78,11 @@ static bool check_date(const char *s, size_t len) {
|
|
|
81
78
|
}
|
|
82
79
|
|
|
83
80
|
/* HH:MM:SS with ranges; answers the consumed length (8) or 0 */
|
|
84
|
-
static size_t check_hms(const char *s, size_t len
|
|
81
|
+
static size_t check_hms(const char *s, size_t len, bool allow_leap,
|
|
82
|
+
int *hour_out, int *minute_out, int *sec_out) {
|
|
83
|
+
/* allow_leap: the date-time format's leap second (:60 at the UTC
|
|
84
|
+
* day end — the caller validates the moment); the time format
|
|
85
|
+
* and the local shapes cap at 59 */
|
|
85
86
|
if (len < 8 || s[2] != ':' || s[5] != ':') return 0;
|
|
86
87
|
if (!all_digits(s, 0, 2) || !all_digits(s, 3, 2) ||
|
|
87
88
|
!all_digits(s, 6, 2)) {
|
|
@@ -90,7 +91,11 @@ static size_t check_hms(const char *s, size_t len) {
|
|
|
90
91
|
int h = two_digits(s, 0);
|
|
91
92
|
int m = two_digits(s, 3);
|
|
92
93
|
int sec = two_digits(s, 6);
|
|
93
|
-
if (h > 23 || m > 59
|
|
94
|
+
if (h > 23 || m > 59) return 0;
|
|
95
|
+
if (sec > 59 && !(allow_leap && sec == 60)) return 0;
|
|
96
|
+
if (hour_out) *hour_out = h;
|
|
97
|
+
if (minute_out) *minute_out = m;
|
|
98
|
+
if (sec_out) *sec_out = sec;
|
|
94
99
|
return 8;
|
|
95
100
|
}
|
|
96
101
|
|
|
@@ -112,7 +117,8 @@ static bool check_datetime(const char *s, size_t len) {
|
|
|
112
117
|
if (len < 20) return false;
|
|
113
118
|
if (!check_date(s, 10)) return false;
|
|
114
119
|
if (s[10] != 'T' && s[10] != 't') return false;
|
|
115
|
-
|
|
120
|
+
int h = 0, minute = 0, sec = 0;
|
|
121
|
+
size_t used = check_hms(s + 11, len - 11, true, &h, &minute, &sec);
|
|
116
122
|
if (used == 0) return false;
|
|
117
123
|
size_t pos = 11 + used;
|
|
118
124
|
if (pos < len && s[pos] == '.') {
|
|
@@ -124,6 +130,22 @@ static bool check_datetime(const char *s, size_t len) {
|
|
|
124
130
|
}
|
|
125
131
|
if (frac == 0) return false;
|
|
126
132
|
}
|
|
133
|
+
if (pos >= len) return false;
|
|
134
|
+
/* a leap second exists only at the UTC day end: the local clock
|
|
135
|
+
* must read :59:60 and land at second 86400 after the offset */
|
|
136
|
+
if (sec == 60) {
|
|
137
|
+
if (s[pos] == 'Z' || s[pos] == 'z') {
|
|
138
|
+
if (h * 3600 + minute * 60 + 60 != 86400) return false;
|
|
139
|
+
} else {
|
|
140
|
+
if (pos + 6 > len) return false;
|
|
141
|
+
bool neg = s[pos] == '-';
|
|
142
|
+
int oh = two_digits(s + pos, 1);
|
|
143
|
+
int om = two_digits(s + pos, 4);
|
|
144
|
+
/* (both read s[from], s[from+1] — signature is (s, from)) */
|
|
145
|
+
int off = (neg ? -1 : 1) * (oh * 3600 + om * 60);
|
|
146
|
+
if (h * 3600 + minute * 60 + 60 - off != 86400) return false;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
127
149
|
return len > pos && check_offset(s + pos, len - pos) == len - pos;
|
|
128
150
|
}
|
|
129
151
|
|
|
@@ -132,41 +154,164 @@ static bool check_datetime(const char *s, size_t len) {
|
|
|
132
154
|
/* the crate's email check: a leading dot fails, ".." fails, a dot
|
|
133
155
|
* directly before "@" fails, an "@" ends the (unvalidated) domain;
|
|
134
156
|
* everything else about the domain is accepted */
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
157
|
+
/* RFC 5322 addr-spec (the practical ASCII subset the suite tests).
|
|
158
|
+
* Local: dot-atom (atext incl. ~) or a quoted string (\ escapes one
|
|
159
|
+
* VCHAR; internal dots/spaces/@ fine; "" empty fine). Domain: LDH
|
|
160
|
+
* dot-atom labels (no leading/trailing '-', single-label "io" fine)
|
|
161
|
+
* or an address literal [dotted-quad 0-255 | IPv6:]. One @, ASCII
|
|
162
|
+
* only, no CR/LF/space/controls anywhere, no display names. */
|
|
163
|
+
static bool atext(char c)
|
|
164
|
+
{
|
|
165
|
+
if (is_ascii_alnum(c)) return true;
|
|
166
|
+
return c == '!' || c == '#' || c == '$' || c == '%' || c == '&' ||
|
|
167
|
+
c == '\'' || c == '*' || c == '+' || c == '-' || c == '/' ||
|
|
168
|
+
c == '=' || c == '?' || c == '^' || c == '_' || c == '`' ||
|
|
169
|
+
c == '{' || c == '|' || c == '}' || c == '~';
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
static bool quoted_local(const char *s, size_t len, size_t *end, bool utf8)
|
|
173
|
+
{
|
|
174
|
+
if (s[0] != '"') return false;
|
|
175
|
+
size_t i = 1;
|
|
176
|
+
for (;;) {
|
|
177
|
+
if (i >= len) return false; /* unterminated */
|
|
178
|
+
unsigned char c = (unsigned char)s[i];
|
|
179
|
+
if (c == '"') {
|
|
180
|
+
*end = i + 1;
|
|
181
|
+
return true; /* "" alone is legal; the caller checks what
|
|
182
|
+
follows */
|
|
183
|
+
}
|
|
184
|
+
if (c == '\\') {
|
|
185
|
+
if (i + 1 >= len) return false;
|
|
186
|
+
unsigned char n = (unsigned char)s[i + 1];
|
|
187
|
+
if (n < 0x20 || n == 0x7f) return false;
|
|
188
|
+
if (!utf8 && n >= 0x80) return false;
|
|
189
|
+
i += 2;
|
|
190
|
+
continue;
|
|
140
191
|
}
|
|
141
|
-
if (
|
|
192
|
+
if (c < 0x20 || c == 0x7f) return false;
|
|
193
|
+
if (!utf8 && c >= 0x80) return false;
|
|
194
|
+
i++;
|
|
142
195
|
}
|
|
143
|
-
return false; /* no @ */
|
|
144
196
|
}
|
|
145
197
|
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
* crate's alphanumeric test is Unicode-wide; the practical subset
|
|
150
|
-
* here is ASCII (flagged as a divergence in SCHEMAS.md) */
|
|
151
|
-
static bool check_hostname(const char *s, size_t len) {
|
|
152
|
-
if (len == 0 || s[0] == '-' || s[len - 1] == '-') return false;
|
|
153
|
-
if (cp_count(s, len) > 255) return false;
|
|
198
|
+
static bool dot_atom(const char *s, size_t len, bool atext_only, bool utf8)
|
|
199
|
+
{
|
|
200
|
+
if (len == 0 || s[0] == '.' || s[len - 1] == '.') return false;
|
|
154
201
|
for (size_t i = 0; i < len; i++) {
|
|
155
|
-
char c = s[i];
|
|
156
|
-
if (
|
|
202
|
+
unsigned char c = (unsigned char)s[i];
|
|
203
|
+
if (c == '.') {
|
|
204
|
+
if (i + 1 < len && s[i + 1] == '.') return false;
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
bool ok = atext_only ? atext((char)c)
|
|
208
|
+
: (is_ascii_alnum(c) || c == '-');
|
|
209
|
+
if (!ok && !(utf8 && c >= 0x80)) return false;
|
|
157
210
|
}
|
|
211
|
+
return true;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
static bool check_ipv6(const char *s, size_t len);
|
|
215
|
+
|
|
216
|
+
static bool domain_literal(const char *s, size_t len)
|
|
217
|
+
{
|
|
218
|
+
/* s = "[...]" inclusive */
|
|
219
|
+
if (len < 3 || s[0] != '[' || s[len - 1] != ']') return false;
|
|
220
|
+
const char *in = s + 1;
|
|
221
|
+
size_t n = len - 2;
|
|
222
|
+
if (n >= 5 && (in[0] == 'I' || in[0] == 'i') &&
|
|
223
|
+
(in[1] == 'P' || in[1] == 'p') && in[2] == 'v' && in[3] == '6' &&
|
|
224
|
+
in[4] == ':') {
|
|
225
|
+
return check_ipv6(in + 5, n - 5);
|
|
226
|
+
}
|
|
227
|
+
/* dotted quad, each octet 0-255 */
|
|
228
|
+
int octets = 0, val = -1, digits = 0;
|
|
229
|
+
for (size_t i = 0; i <= n; i++) {
|
|
230
|
+
if (i == n || in[i] == '.') {
|
|
231
|
+
if (digits == 0 || digits > 3 || val < 0 || val > 255)
|
|
232
|
+
return false;
|
|
233
|
+
octets++;
|
|
234
|
+
val = -1;
|
|
235
|
+
digits = 0;
|
|
236
|
+
} else if (is_digit(in[i])) {
|
|
237
|
+
if (digits == 0) val = 0;
|
|
238
|
+
val = val * 10 + (in[i] - '0');
|
|
239
|
+
digits++;
|
|
240
|
+
} else {
|
|
241
|
+
return false;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
return octets == 4;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
static bool check_domain(const char *s, size_t len, bool utf8)
|
|
248
|
+
{
|
|
249
|
+
if (len == 0) return false;
|
|
250
|
+
if (s[0] == '[') return domain_literal(s, len);
|
|
251
|
+
/* LDH dot-atom labels: alnum/hyphen, no leading/trailing '-' per
|
|
252
|
+
* label; "io"/"123.com"/"c--n.com" valid; non-ASCII only for the
|
|
253
|
+
* idn flavor */
|
|
158
254
|
size_t label = 0;
|
|
159
255
|
for (size_t i = 0; i <= len; i++) {
|
|
160
|
-
|
|
161
|
-
|
|
256
|
+
unsigned char c = i < len ? (unsigned char)s[i] : '.';
|
|
257
|
+
if (i == len || c == '.') {
|
|
258
|
+
if (label == 0) return false;
|
|
259
|
+
if (s[i - label] == '-' || s[i - 1] == '-') return false;
|
|
162
260
|
label = 0;
|
|
163
|
-
|
|
261
|
+
if (i == len) break;
|
|
262
|
+
} else if (is_ascii_alnum(c) || c == '-' || (utf8 && c >= 0x80)) {
|
|
164
263
|
label++;
|
|
264
|
+
} else {
|
|
265
|
+
return false;
|
|
165
266
|
}
|
|
166
267
|
}
|
|
167
268
|
return true;
|
|
168
269
|
}
|
|
169
270
|
|
|
271
|
+
static bool check_email_generic(const char *s, size_t len, bool utf8)
|
|
272
|
+
{
|
|
273
|
+
if (len == 0) return false;
|
|
274
|
+
for (size_t i = 0; i < len; i++) {
|
|
275
|
+
/* space/tab ride inside quoted locals; the unquoted paths
|
|
276
|
+
* reject them via atext/label rules. CRLF and controls are
|
|
277
|
+
* banned outright (quoted or not); non-ASCII only for the idn
|
|
278
|
+
* flavor. */
|
|
279
|
+
unsigned char c = (unsigned char)s[i];
|
|
280
|
+
if (c == '\n' || c == '\r') return false;
|
|
281
|
+
if (c < 0x20 && c != ' ' && c != '\t') return false;
|
|
282
|
+
if (!utf8 && c >= 0x7f) return false;
|
|
283
|
+
}
|
|
284
|
+
size_t lend = 0, at = 0;
|
|
285
|
+
if (s[0] == '"') {
|
|
286
|
+
/* quoted local: scan the closing quote, then exactly one @ */
|
|
287
|
+
if (!quoted_local(s, len, &lend, utf8)) return false;
|
|
288
|
+
if (lend >= len || s[lend] != '@') return false;
|
|
289
|
+
at = lend;
|
|
290
|
+
} else {
|
|
291
|
+
int nat = 0;
|
|
292
|
+
for (size_t i = 0; i < len; i++) {
|
|
293
|
+
if (s[i] == '@') {
|
|
294
|
+
nat++;
|
|
295
|
+
at = i;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
if (nat != 1) return false;
|
|
299
|
+
lend = at;
|
|
300
|
+
if (!dot_atom(s, at, true, utf8)) return false;
|
|
301
|
+
}
|
|
302
|
+
return check_domain(s + at + 1, len - at - 1, utf8);
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
static bool check_email(const char *s, size_t len)
|
|
306
|
+
{
|
|
307
|
+
return check_email_generic(s, len, false);
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
static bool check_idn_email(const char *s, size_t len)
|
|
311
|
+
{
|
|
312
|
+
return check_email_generic(s, len, true);
|
|
313
|
+
}
|
|
314
|
+
|
|
170
315
|
/* dotted quad, each group 1-3 digits without a leading zero (unless
|
|
171
316
|
* the group is exactly "0"), at most 255 */
|
|
172
317
|
static bool check_ipv4(const char *s, size_t len) {
|
|
@@ -199,6 +344,13 @@ static bool group_ok(const char *t, size_t n) {
|
|
|
199
344
|
}
|
|
200
345
|
|
|
201
346
|
static bool check_ipv6(const char *s, size_t len) {
|
|
347
|
+
/* '::' compresses once: a colon run longer than 2 (':::') is
|
|
348
|
+
* invalid outright ('1:2:3:4:5:::8') */
|
|
349
|
+
size_t run = 0;
|
|
350
|
+
for (size_t i = 0; i < len; i++) {
|
|
351
|
+
run = s[i] == ':' ? run + 1 : 0;
|
|
352
|
+
if (run > 2) return false;
|
|
353
|
+
}
|
|
202
354
|
const char *tok[9];
|
|
203
355
|
size_t tl[9];
|
|
204
356
|
size_t n = 0, start = 0;
|
|
@@ -246,45 +398,200 @@ static bool check_ipv6(const char *s, size_t len) {
|
|
|
246
398
|
/* absolute URL, the practical shape of the url-crate parse: a scheme
|
|
247
399
|
* (letter-first, alnum/+/-/.), a colon, and no control characters or
|
|
248
400
|
* spaces anywhere */
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
401
|
+
/* ---- RFC 3986 / RFC 3987 (iri) component parse — the unified
|
|
402
|
+
* suite runs the assert-flavored uri family against a real grammar:
|
|
403
|
+
* scheme [//authority] path ? query # fragment, pct = %HH only,
|
|
404
|
+
* reg-name/port/host-literal rules, bare IPv6 hosts rejected,
|
|
405
|
+
* non-ASCII only for iri. ---- */
|
|
406
|
+
|
|
407
|
+
static size_t utf8_one(const char *s, size_t len, uint32_t *cp) {
|
|
408
|
+
unsigned char c = (unsigned char)s[0];
|
|
409
|
+
if (c < 0x80) { *cp = c; return 1; }
|
|
410
|
+
size_t n;
|
|
411
|
+
uint32_t v;
|
|
412
|
+
if ((c & 0xE0) == 0xC0) { n = 2; v = c & 0x1F; }
|
|
413
|
+
else if ((c & 0xF0) == 0xE0) { n = 3; v = c & 0x0F; }
|
|
414
|
+
else if ((c & 0xF8) == 0xF0) { n = 4; v = c & 0x07; }
|
|
415
|
+
else return 0;
|
|
416
|
+
if (len < n) return 0;
|
|
417
|
+
for (size_t k = 1; k < n; k++) {
|
|
418
|
+
unsigned char cc = (unsigned char)s[k];
|
|
419
|
+
if ((cc & 0xC0) != 0x80) return 0;
|
|
420
|
+
v = (v << 6) | (cc & 0x3F);
|
|
254
421
|
}
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
422
|
+
*cp = v;
|
|
423
|
+
return n;
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
static bool uri_subdelim(uint32_t cp) {
|
|
427
|
+
switch (cp) {
|
|
428
|
+
case '!': case '$': case '&': case '\'': case '(': case ')':
|
|
429
|
+
case '*': case '+': case ',': case ';': case '=': return true;
|
|
261
430
|
}
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
431
|
+
return false;
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
static bool uri_unreserved(uint32_t cp, bool unicode) {
|
|
435
|
+
if (cp < 0x80) return is_ascii_alnum((char)cp) || cp == '-' ||
|
|
436
|
+
cp == '.' || cp == '_' || cp == '~';
|
|
437
|
+
return unicode; /* iri: any non-ASCII code point is an iunreserved */
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
/* pchar / '/' / '?' across [from, to): pct must be %HH, disallowed
|
|
441
|
+
* bytes and non-iri non-ASCII fail */
|
|
442
|
+
static bool uri_span_ok(const char *s, size_t from, size_t to,
|
|
443
|
+
bool unicode) {
|
|
444
|
+
size_t i = from;
|
|
445
|
+
while (i < to) {
|
|
446
|
+
if (s[i] == '%') {
|
|
447
|
+
if (i + 2 >= to || !is_hex(s[i + 1]) || !is_hex(s[i + 2]))
|
|
448
|
+
return false;
|
|
449
|
+
i += 3;
|
|
450
|
+
continue;
|
|
451
|
+
}
|
|
452
|
+
if (s[i] == '/' || s[i] == '?' || s[i] == '#') { i++; continue; }
|
|
453
|
+
uint32_t cp;
|
|
454
|
+
size_t adv = utf8_one(s + i, to - i, &cp);
|
|
455
|
+
if (adv == 0) return false;
|
|
456
|
+
if (uri_unreserved(cp, unicode) || uri_subdelim(cp) ||
|
|
457
|
+
cp == ':' || cp == '@') {
|
|
458
|
+
i += adv;
|
|
459
|
+
continue;
|
|
460
|
+
}
|
|
461
|
+
return false;
|
|
266
462
|
}
|
|
267
463
|
return true;
|
|
268
464
|
}
|
|
269
465
|
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
if (is_ascii_space(c)) return false;
|
|
280
|
-
if (c == '#') {
|
|
281
|
-
if (hash) return false;
|
|
282
|
-
hash = true;
|
|
466
|
+
static bool uri_reg_name(const char *s, size_t from, size_t to,
|
|
467
|
+
bool unicode) {
|
|
468
|
+
size_t i = from;
|
|
469
|
+
while (i < to) {
|
|
470
|
+
if (s[i] == '%') {
|
|
471
|
+
if (i + 2 >= to || !is_hex(s[i + 1]) || !is_hex(s[i + 2]))
|
|
472
|
+
return false;
|
|
473
|
+
i += 3;
|
|
474
|
+
continue;
|
|
283
475
|
}
|
|
476
|
+
uint32_t cp;
|
|
477
|
+
size_t adv = utf8_one(s + i, to - i, &cp);
|
|
478
|
+
if (adv == 0) return false;
|
|
479
|
+
if (!(uri_unreserved(cp, unicode) || uri_subdelim(cp))) return false;
|
|
480
|
+
i += adv;
|
|
284
481
|
}
|
|
285
482
|
return true;
|
|
286
483
|
}
|
|
287
484
|
|
|
485
|
+
static bool check_uri_generic(const char *s, size_t len,
|
|
486
|
+
bool require_scheme, bool unicode) {
|
|
487
|
+
if (len == 0) return !require_scheme;
|
|
488
|
+
size_t i = 0;
|
|
489
|
+
bool has_scheme = false;
|
|
490
|
+
bool via_authority = false;
|
|
491
|
+
/* scheme = ALPHA *( ALPHA / DIGIT / '+' / '-' / '.' ) ':' */
|
|
492
|
+
if (((unsigned char)s[0] | 32) >= 'a' && ((unsigned char)s[0] | 32) <= 'z') {
|
|
493
|
+
size_t j = 1;
|
|
494
|
+
while (j < len) {
|
|
495
|
+
unsigned char c = (unsigned char)s[j];
|
|
496
|
+
if (c == ':') { has_scheme = true; i = j + 1; break; }
|
|
497
|
+
if (!(is_ascii_alnum(c) || c == '+' || c == '-' || c == '.'))
|
|
498
|
+
break;
|
|
499
|
+
j++;
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
if (!has_scheme) {
|
|
503
|
+
if (require_scheme) return false;
|
|
504
|
+
/* relative reference */
|
|
505
|
+
if (len >= 2 && s[0] == '/' && s[1] == '/') {
|
|
506
|
+
i = 2; /* authority below, shared path */
|
|
507
|
+
via_authority = true;
|
|
508
|
+
} else {
|
|
509
|
+
/* path-noscheme / path-absolute / empty */
|
|
510
|
+
size_t qend = len;
|
|
511
|
+
for (size_t k = 0; k < len; k++) {
|
|
512
|
+
if (s[k] == '?' || s[k] == '#') { qend = k; break; }
|
|
513
|
+
}
|
|
514
|
+
if (s[0] != '/') {
|
|
515
|
+
/* the first segment may not carry ':' unless it opens
|
|
516
|
+
* with a dot segment ('./this:that' is fine) */
|
|
517
|
+
size_t seg = 0;
|
|
518
|
+
while (seg < qend && s[seg] != '/') seg++;
|
|
519
|
+
if (seg > 0 && s[0] != '.') {
|
|
520
|
+
for (size_t k = 0; k < seg; k++) {
|
|
521
|
+
if (s[k] == ':') return false;
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
return uri_span_ok(s, 0, len, unicode);
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
/* authority: '//' after the scheme, or a leading '//' in a
|
|
529
|
+
* relative reference */
|
|
530
|
+
size_t rest_from = i;
|
|
531
|
+
if (via_authority || (i + 1 < len && s[i] == '/' && s[i + 1] == '/')) {
|
|
532
|
+
size_t auth = via_authority ? i : i + 2;
|
|
533
|
+
size_t aend = auth;
|
|
534
|
+
while (aend < len && s[aend] != '/' && s[aend] != '?' &&
|
|
535
|
+
s[aend] != '#') {
|
|
536
|
+
aend++;
|
|
537
|
+
}
|
|
538
|
+
size_t host_start = auth;
|
|
539
|
+
size_t nat = 0, at = (size_t)-1;
|
|
540
|
+
for (size_t k = auth; k < aend; k++) {
|
|
541
|
+
if (s[k] == '@') { nat++; at = k; }
|
|
542
|
+
}
|
|
543
|
+
if (nat > 1) return false; /* '@' cannot ride in userinfo */
|
|
544
|
+
if (nat == 1) {
|
|
545
|
+
if (!uri_span_ok(s, host_start, at, unicode)) return false;
|
|
546
|
+
host_start = at + 1;
|
|
547
|
+
}
|
|
548
|
+
if (host_start < aend && s[host_start] == '[') {
|
|
549
|
+
size_t close = host_start;
|
|
550
|
+
while (close < aend && s[close] != ']') close++;
|
|
551
|
+
if (close >= aend) return false;
|
|
552
|
+
if (!check_ipv6(s + host_start + 1, close - host_start - 1))
|
|
553
|
+
return false;
|
|
554
|
+
if (close + 1 < aend && s[close + 1] == ':') {
|
|
555
|
+
for (size_t k = close + 2; k < aend; k++) {
|
|
556
|
+
if (!is_digit((unsigned char)s[k])) return false;
|
|
557
|
+
}
|
|
558
|
+
} else if (close + 1 != aend) {
|
|
559
|
+
return false;
|
|
560
|
+
}
|
|
561
|
+
} else {
|
|
562
|
+
/* reg-name up to an optional ':' port (digits only) */
|
|
563
|
+
size_t cend = aend;
|
|
564
|
+
for (size_t k = host_start; k < aend; k++) {
|
|
565
|
+
if (s[k] == ':') { cend = k; break; }
|
|
566
|
+
if (s[k] == '[' || s[k] == ']') return false;
|
|
567
|
+
}
|
|
568
|
+
if (!uri_reg_name(s, host_start, cend, unicode)) return false;
|
|
569
|
+
if (cend < aend) {
|
|
570
|
+
if (cend + 1 == aend) return false; /* 'joe.bloggs@'-class:
|
|
571
|
+
an empty port is
|
|
572
|
+
not a port */
|
|
573
|
+
for (size_t k = cend + 1; k < aend; k++) {
|
|
574
|
+
if (!is_digit((unsigned char)s[k])) return false;
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
rest_from = aend;
|
|
579
|
+
}
|
|
580
|
+
return uri_span_ok(s, rest_from, len, unicode);
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
static bool check_uri(const char *s, size_t len) {
|
|
584
|
+
return check_uri_generic(s, len, true, false);
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
static bool check_uri_reference(const char *s, size_t len) {
|
|
588
|
+
return check_uri_generic(s, len, false, false);
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
static bool check_iri_like(const char *s, size_t len, bool require_scheme) {
|
|
592
|
+
return check_uri_generic(s, len, require_scheme, true);
|
|
593
|
+
}
|
|
594
|
+
|
|
288
595
|
/* JSON pointer (RFC 6901): empty, or '/'-led with '~' only in the
|
|
289
596
|
* escapes ~0/~1 */
|
|
290
597
|
static bool check_json_pointer(const char *s, size_t len) {
|
|
@@ -304,17 +611,21 @@ static bool check_json_pointer(const char *s, size_t len) {
|
|
|
304
611
|
/* relative-json-pointer: non-negative base-10 index without a
|
|
305
612
|
* leading zero, then "#" or a '/'-led pointer tail */
|
|
306
613
|
static bool check_relative_json_pointer(const char *s, size_t len) {
|
|
614
|
+
/* RFC 6901: the prefix is "0" or a non-zero-leading integer, and
|
|
615
|
+
* the bare integer IS a valid pointer (the # / json-pointer part
|
|
616
|
+
* is optional) */
|
|
307
617
|
size_t i = 0;
|
|
308
618
|
if (len == 0) return false;
|
|
309
619
|
if (s[0] == '0') {
|
|
310
620
|
i = 1;
|
|
621
|
+
if (i < len && is_digit(s[i])) return false; /* 01, 012... */
|
|
311
622
|
} else if (s[0] >= '1' && s[0] <= '9') {
|
|
312
623
|
i = 1;
|
|
313
624
|
while (i < len && is_digit(s[i])) i++;
|
|
314
625
|
} else {
|
|
315
626
|
return false;
|
|
316
627
|
}
|
|
317
|
-
if (i == len) return
|
|
628
|
+
if (i == len) return true; /* the bare integer alone */
|
|
318
629
|
if (s[i] == '#') return i + 1 == len;
|
|
319
630
|
if (s[i] != '/') return false;
|
|
320
631
|
return check_json_pointer(s + i, len - i);
|
|
@@ -323,70 +634,97 @@ static bool check_relative_json_pointer(const char *s, size_t len) {
|
|
|
323
634
|
/* uuid: the crate's FromStr accepts the simple (32 hex) and
|
|
324
635
|
* hyphenated (8-4-4-4-12) spellings, case-insensitive */
|
|
325
636
|
static bool check_uuid(const char *s, size_t len) {
|
|
326
|
-
|
|
327
|
-
|
|
637
|
+
/* RFC 4122 abnf: exactly 8-4-4-4-12; the 32-char undashed form
|
|
638
|
+
* is not a uuid-literal (the unified suite's stance) */
|
|
639
|
+
if (len != 36) return false;
|
|
640
|
+
if (s[8] != '-' || s[13] != '-' || s[18] != '-' || s[23] != '-')
|
|
641
|
+
return false;
|
|
328
642
|
for (size_t i = 0; i < len; i++) {
|
|
329
|
-
if (
|
|
330
|
-
if (s[i] != '-') return false;
|
|
331
|
-
continue;
|
|
332
|
-
}
|
|
643
|
+
if (i == 8 || i == 13 || i == 18 || i == 23) continue;
|
|
333
644
|
if (!is_hex(s[i])) return false;
|
|
334
|
-
hex_seen++;
|
|
335
645
|
}
|
|
336
|
-
return
|
|
646
|
+
return true;
|
|
337
647
|
}
|
|
338
648
|
|
|
339
649
|
/* ISO 8601 duration, the practical core the iso8601 crate accepts:
|
|
340
650
|
* an optional sign, P, then at least one nY/nM/nW/nD component and an
|
|
341
651
|
* optional T-section of nH/nM/nS; a fraction is allowed on any unit */
|
|
342
652
|
static bool check_duration(const char *s, size_t len) {
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
653
|
+
/* RFC 3339 duration, strict: P 1*DIGIT unit, canonical order
|
|
654
|
+
* Y -> M -> D -> T -> H -> M -> S. Within each section the FIRST
|
|
655
|
+
* component may be any of the section's units, every following
|
|
656
|
+
* one is exactly the next index (no skips, no duplicates:
|
|
657
|
+
* PT1H2S fails, PT1M2S passes); W is legal ONLY as the sole
|
|
658
|
+
* component; no sign, no fraction, ASCII digits. */
|
|
659
|
+
if (len < 2 || s[0] != 'P') return false;
|
|
660
|
+
size_t wpos = (size_t)-1;
|
|
661
|
+
for (size_t i = 1; i < len; i++) {
|
|
662
|
+
if (s[i] == 'W') wpos = i;
|
|
663
|
+
}
|
|
664
|
+
if (wpos != (size_t)-1) {
|
|
665
|
+
if (wpos != len - 1) return false;
|
|
666
|
+
for (size_t i = 1; i < len - 1; i++) {
|
|
667
|
+
if (!is_digit((unsigned char)s[i])) return false;
|
|
668
|
+
}
|
|
669
|
+
return len > 2;
|
|
670
|
+
}
|
|
671
|
+
int last = -1; /* canonical index of the previous unit */
|
|
672
|
+
bool first_date = true;
|
|
673
|
+
bool first_time = true;
|
|
349
674
|
bool in_time = false;
|
|
675
|
+
int components = 0;
|
|
676
|
+
size_t i = 1;
|
|
350
677
|
while (i < len) {
|
|
351
|
-
if (
|
|
678
|
+
if (s[i] == 'T') {
|
|
679
|
+
if (in_time) return false;
|
|
352
680
|
in_time = true;
|
|
681
|
+
first_time = true;
|
|
353
682
|
i++;
|
|
683
|
+
if (i >= len) return false; /* trailing T */
|
|
354
684
|
continue;
|
|
355
685
|
}
|
|
356
|
-
|
|
357
|
-
while (i < len && is_digit(s[i])) i++;
|
|
358
|
-
if (i == start) return false;
|
|
359
|
-
if (i < len && (s[i] == '.' || s[i] == ',')) {
|
|
360
|
-
i++;
|
|
361
|
-
size_t fs = i;
|
|
362
|
-
while (i < len && is_digit(s[i])) i++;
|
|
363
|
-
if (i == fs) return false;
|
|
364
|
-
}
|
|
686
|
+
if (!is_digit((unsigned char)s[i])) return false;
|
|
687
|
+
while (i < len && is_digit((unsigned char)s[i])) i++;
|
|
365
688
|
if (i >= len) return false;
|
|
366
|
-
|
|
367
|
-
i
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
689
|
+
int idx;
|
|
690
|
+
switch (s[i]) {
|
|
691
|
+
case 'Y': idx = 0; break;
|
|
692
|
+
case 'M': idx = in_time ? 4 : 1; break;
|
|
693
|
+
case 'D': idx = 2; break;
|
|
694
|
+
case 'H': idx = 3; break;
|
|
695
|
+
case 'S': idx = 5; break;
|
|
696
|
+
default: return false;
|
|
697
|
+
}
|
|
698
|
+
if (idx == 2 && in_time) return false; /* no date units in the
|
|
699
|
+
time section */
|
|
700
|
+
if ((idx == 3 || idx == 5) && !in_time) return false;
|
|
701
|
+
if (in_time) {
|
|
702
|
+
if (first_time) {
|
|
703
|
+
first_time = false;
|
|
704
|
+
} else if (idx != last + 1) {
|
|
705
|
+
return false;
|
|
706
|
+
}
|
|
374
707
|
} else {
|
|
375
|
-
|
|
708
|
+
if (first_date) {
|
|
709
|
+
first_date = false;
|
|
710
|
+
} else if (idx != last + 1) {
|
|
711
|
+
return false;
|
|
712
|
+
}
|
|
376
713
|
}
|
|
377
|
-
|
|
714
|
+
last = idx;
|
|
715
|
+
components++;
|
|
716
|
+
i++;
|
|
378
717
|
}
|
|
379
|
-
return
|
|
718
|
+
return components > 0;
|
|
380
719
|
}
|
|
381
720
|
|
|
382
721
|
/* ----------------------------------------------------- uri-template -- */
|
|
383
722
|
|
|
384
|
-
/*
|
|
385
|
-
*
|
|
386
|
-
*
|
|
387
|
-
*
|
|
388
|
-
*
|
|
389
|
-
* of the crate's lowercase-only spellings) */
|
|
723
|
+
/* RFC 6570: literals are the unreserved set, %XX pairs, unicode and
|
|
724
|
+
* ":/?#[]@!$&'()*+,;="; expressions are '{' [op] varspec *(, varspec)
|
|
725
|
+
* '}' with op from "+#./;?&", varname = alnum/underscore/pct with
|
|
726
|
+
* single internal dots, and a ':' prefix (1-4 digits, no leading
|
|
727
|
+
* zero) or '*' modifier — but never both */
|
|
390
728
|
static bool check_uri_template(const char *s, size_t len) {
|
|
391
729
|
size_t i = 0;
|
|
392
730
|
while (i < len) {
|
|
@@ -400,14 +738,13 @@ static bool check_uri_template(const char *s, size_t len) {
|
|
|
400
738
|
}
|
|
401
739
|
if (c == '{') {
|
|
402
740
|
i++;
|
|
403
|
-
if (i < len && s[i] !=
|
|
404
|
-
strchr("+./;?&=,!@|", s[i]) != NULL) {
|
|
741
|
+
if (i < len && strchr("+#./;?&", s[i]) != NULL) {
|
|
405
742
|
i++;
|
|
406
743
|
}
|
|
407
|
-
|
|
408
|
-
while (i < len && s[i] != '}') {
|
|
744
|
+
for (;;) {
|
|
409
745
|
/* one varspec: name (:N or *)? */
|
|
410
746
|
size_t name = 0;
|
|
747
|
+
bool dot = false;
|
|
411
748
|
while (i < len && s[i] != '}' && s[i] != ',' &&
|
|
412
749
|
s[i] != ':' && s[i] != '*') {
|
|
413
750
|
if (s[i] == '%') {
|
|
@@ -416,14 +753,19 @@ static bool check_uri_template(const char *s, size_t len) {
|
|
|
416
753
|
return false;
|
|
417
754
|
}
|
|
418
755
|
i += 3;
|
|
756
|
+
dot = false;
|
|
419
757
|
} else if (is_ascii_alnum(s[i]) || s[i] == '_') {
|
|
420
758
|
i++;
|
|
759
|
+
dot = false;
|
|
760
|
+
} else if (s[i] == '.' && name > 0 && !dot) {
|
|
761
|
+
i++;
|
|
762
|
+
dot = true;
|
|
421
763
|
} else {
|
|
422
764
|
return false;
|
|
423
765
|
}
|
|
424
|
-
name++;
|
|
766
|
+
if (dot == false) name++;
|
|
425
767
|
}
|
|
426
|
-
if (name == 0) return false;
|
|
768
|
+
if (name == 0 || dot) return false;
|
|
427
769
|
if (i < len && s[i] == ':') {
|
|
428
770
|
i++;
|
|
429
771
|
size_t ds = i;
|
|
@@ -434,17 +776,18 @@ static bool check_uri_template(const char *s, size_t len) {
|
|
|
434
776
|
} else if (i < len && s[i] == '*') {
|
|
435
777
|
i++;
|
|
436
778
|
}
|
|
437
|
-
varspec = true;
|
|
438
779
|
if (i < len && s[i] == ',') {
|
|
439
780
|
i++;
|
|
781
|
+
continue; /* the next varspec is mandatory */
|
|
440
782
|
}
|
|
783
|
+
break;
|
|
441
784
|
}
|
|
442
|
-
if (i >= len ||
|
|
785
|
+
if (i >= len || s[i] != '}') return false;
|
|
443
786
|
i++; /* the closing brace */
|
|
444
787
|
continue;
|
|
445
788
|
}
|
|
446
789
|
unsigned char b = (unsigned char)c;
|
|
447
|
-
if (b <= 0x20 || b == 0x7F || c == '"' || c == '
|
|
790
|
+
if (b <= 0x20 || b == 0x7F || c == '"' || c == '<' ||
|
|
448
791
|
c == '>' || c == '\\' || c == '^' || c == '`' || c == '|' ||
|
|
449
792
|
c == '}') {
|
|
450
793
|
return false;
|
|
@@ -604,6 +947,662 @@ static bool check_semver_req(const char *s, size_t len) {
|
|
|
604
947
|
return any;
|
|
605
948
|
}
|
|
606
949
|
|
|
950
|
+
/* ---------------------------------------------------------- idna -- */
|
|
951
|
+
|
|
952
|
+
/* utf-8: decode one code point (strict: overlongs, surrogates and
|
|
953
|
+
* out-of-range rejected); i advances past the sequence */
|
|
954
|
+
static bool idna_utf8_next(const char *s, size_t len, size_t *i,
|
|
955
|
+
unsigned *cp) {
|
|
956
|
+
unsigned char b = (unsigned char)s[*i];
|
|
957
|
+
size_t adv;
|
|
958
|
+
unsigned v;
|
|
959
|
+
if (b < 0x80) {
|
|
960
|
+
adv = 1;
|
|
961
|
+
v = b;
|
|
962
|
+
} else if ((b & 0xE0) == 0xC0) {
|
|
963
|
+
adv = 2;
|
|
964
|
+
v = b & 0x1F;
|
|
965
|
+
} else if ((b & 0xF0) == 0xE0) {
|
|
966
|
+
adv = 3;
|
|
967
|
+
v = b & 0x0F;
|
|
968
|
+
} else if ((b & 0xF8) == 0xF0) {
|
|
969
|
+
adv = 4;
|
|
970
|
+
v = b & 0x07;
|
|
971
|
+
} else {
|
|
972
|
+
return false;
|
|
973
|
+
}
|
|
974
|
+
if (*i + adv > len) return false;
|
|
975
|
+
for (size_t k = 1; k < adv; k++) {
|
|
976
|
+
unsigned char c = (unsigned char)s[*i + k];
|
|
977
|
+
if ((c & 0xC0) != 0x80) return false;
|
|
978
|
+
v = (v << 6) | (c & 0x3F);
|
|
979
|
+
}
|
|
980
|
+
static const unsigned lo[4] = {0, 0x80, 0x800, 0x10000};
|
|
981
|
+
if (v < lo[adv - 1] || v > 0x10FFFF ||
|
|
982
|
+
(v >= 0xD800 && v <= 0xDFFF)) {
|
|
983
|
+
return false;
|
|
984
|
+
}
|
|
985
|
+
*cp = v;
|
|
986
|
+
*i += adv;
|
|
987
|
+
return true;
|
|
988
|
+
}
|
|
989
|
+
|
|
990
|
+
static size_t idna_utf8_put(char *out, unsigned v) {
|
|
991
|
+
if (v < 0x80) {
|
|
992
|
+
out[0] = (char)v;
|
|
993
|
+
return 1;
|
|
994
|
+
}
|
|
995
|
+
if (v < 0x800) {
|
|
996
|
+
out[0] = (char)(0xC0 | (v >> 6));
|
|
997
|
+
out[1] = (char)(0x80 | (v & 0x3F));
|
|
998
|
+
return 2;
|
|
999
|
+
}
|
|
1000
|
+
if (v < 0x10000) {
|
|
1001
|
+
out[0] = (char)(0xE0 | (v >> 12));
|
|
1002
|
+
out[1] = (char)(0x80 | ((v >> 6) & 0x3F));
|
|
1003
|
+
out[2] = (char)(0x80 | (v & 0x3F));
|
|
1004
|
+
return 3;
|
|
1005
|
+
}
|
|
1006
|
+
out[0] = (char)(0xF0 | (v >> 18));
|
|
1007
|
+
out[1] = (char)(0x80 | ((v >> 12) & 0x3F));
|
|
1008
|
+
out[2] = (char)(0x80 | ((v >> 6) & 0x3F));
|
|
1009
|
+
out[3] = (char)(0x80 | (v & 0x3F));
|
|
1010
|
+
return 4;
|
|
1011
|
+
}
|
|
1012
|
+
|
|
1013
|
+
static size_t idna_utf8_len(unsigned v) {
|
|
1014
|
+
return v < 0x80 ? 1 : v < 0x800 ? 2 : v < 0x10000 ? 3 : 4;
|
|
1015
|
+
}
|
|
1016
|
+
|
|
1017
|
+
/* range-table lookups (the tables are sorted by `first`) */
|
|
1018
|
+
static const idna_map_row *idna_map_of(unsigned v) {
|
|
1019
|
+
size_t lo = 0, hi = idna_map_len;
|
|
1020
|
+
while (lo < hi) {
|
|
1021
|
+
size_t mid = lo + (hi - lo) / 2;
|
|
1022
|
+
if (idna_map[mid].last < v) {
|
|
1023
|
+
lo = mid + 1;
|
|
1024
|
+
} else {
|
|
1025
|
+
hi = mid;
|
|
1026
|
+
}
|
|
1027
|
+
}
|
|
1028
|
+
if (lo < idna_map_len && idna_map[lo].first <= v) return &idna_map[lo];
|
|
1029
|
+
return NULL;
|
|
1030
|
+
}
|
|
1031
|
+
|
|
1032
|
+
static unsigned char idna_range_of(const idna_range_row *t, size_t n,
|
|
1033
|
+
unsigned v) {
|
|
1034
|
+
size_t lo = 0, hi = n;
|
|
1035
|
+
while (lo < hi) {
|
|
1036
|
+
size_t mid = lo + (hi - lo) / 2;
|
|
1037
|
+
if (t[mid].last < v) {
|
|
1038
|
+
lo = mid + 1;
|
|
1039
|
+
} else {
|
|
1040
|
+
hi = mid;
|
|
1041
|
+
}
|
|
1042
|
+
}
|
|
1043
|
+
if (lo < n && t[lo].first <= v) return t[lo].v;
|
|
1044
|
+
return 0;
|
|
1045
|
+
}
|
|
1046
|
+
|
|
1047
|
+
#define idna_bidi_of(v) idna_range_of(idna_bidi, idna_bidi_len, (v))
|
|
1048
|
+
#define idna_join_of(v) idna_range_of(idna_join, idna_join_len, (v))
|
|
1049
|
+
#define idna_script_of(v) idna_range_of(idna_script, idna_script_len, (v))
|
|
1050
|
+
#define idna_ccc_of(v) ((int)idna_range_of(idna_ccc, idna_ccc_len, (v)))
|
|
1051
|
+
|
|
1052
|
+
static bool idna_is_virama(unsigned v) {
|
|
1053
|
+
for (size_t i = 0; i < idna_virama_len; i++) {
|
|
1054
|
+
if (idna_virama[i] == v) return true;
|
|
1055
|
+
}
|
|
1056
|
+
return false;
|
|
1057
|
+
}
|
|
1058
|
+
|
|
1059
|
+
/* gc=M (UTS-46 validity criterion 6: no leading combining mark) */
|
|
1060
|
+
static bool idna_is_mark(unsigned v) {
|
|
1061
|
+
size_t lo = 0, hi = idna_mark_len;
|
|
1062
|
+
while (lo < hi) {
|
|
1063
|
+
size_t mid = lo + (hi - lo) / 2;
|
|
1064
|
+
if (idna_mark[mid].last < v) {
|
|
1065
|
+
lo = mid + 1;
|
|
1066
|
+
} else {
|
|
1067
|
+
hi = mid;
|
|
1068
|
+
}
|
|
1069
|
+
}
|
|
1070
|
+
return lo < idna_mark_len && idna_mark[lo].first <= v;
|
|
1071
|
+
}
|
|
1072
|
+
|
|
1073
|
+
static const idna_decomp_row *idna_decomp_of(unsigned v) {
|
|
1074
|
+
size_t lo = 0, hi = idna_decomp_len;
|
|
1075
|
+
while (lo < hi) {
|
|
1076
|
+
size_t mid = lo + (hi - lo) / 2;
|
|
1077
|
+
if (idna_decomp[mid].cp < v) {
|
|
1078
|
+
lo = mid + 1;
|
|
1079
|
+
} else {
|
|
1080
|
+
hi = mid;
|
|
1081
|
+
}
|
|
1082
|
+
}
|
|
1083
|
+
if (lo < idna_decomp_len && idna_decomp[lo].cp == v)
|
|
1084
|
+
return &idna_decomp[lo];
|
|
1085
|
+
return NULL;
|
|
1086
|
+
}
|
|
1087
|
+
|
|
1088
|
+
static unsigned idna_comp_of(unsigned a, unsigned b) {
|
|
1089
|
+
for (size_t i = 0; i < idna_comp_len; i++) {
|
|
1090
|
+
if (idna_comp[i].a == a && idna_comp[i].b == b) return idna_comp[i].cp;
|
|
1091
|
+
}
|
|
1092
|
+
return 0;
|
|
1093
|
+
}
|
|
1094
|
+
|
|
1095
|
+
/* hangul constants (the algorithmic decompositions/compositions) */
|
|
1096
|
+
#define IDNA_SBASE 0xAC00
|
|
1097
|
+
#define IDNA_LBASE 0x1100
|
|
1098
|
+
#define IDNA_VBASE 0x1161
|
|
1099
|
+
#define IDNA_TBASE 0x11A7
|
|
1100
|
+
#define IDNA_LCOUNT 19
|
|
1101
|
+
#define IDNA_VCOUNT 21
|
|
1102
|
+
#define IDNA_TCOUNT 28
|
|
1103
|
+
#define IDNA_NCOUNT (IDNA_VCOUNT * IDNA_TCOUNT)
|
|
1104
|
+
#define IDNA_SCOUNT (IDNA_LCOUNT * IDNA_NCOUNT)
|
|
1105
|
+
|
|
1106
|
+
/* NFC (canonical decomposition, ordering, composition); in place,
|
|
1107
|
+
* answers the new length, or 0 when the scratch runs out */
|
|
1108
|
+
static size_t idna_nfc(unsigned *cp, size_t n, size_t cap) {
|
|
1109
|
+
unsigned tmp[2048];
|
|
1110
|
+
size_t m = 0;
|
|
1111
|
+
for (size_t i = 0; i < n; i++) {
|
|
1112
|
+
unsigned v = cp[i];
|
|
1113
|
+
if (v >= IDNA_SBASE && v < IDNA_SBASE + IDNA_SCOUNT) {
|
|
1114
|
+
unsigned sidx = v - IDNA_SBASE;
|
|
1115
|
+
tmp[m++] = IDNA_LBASE + sidx / IDNA_NCOUNT;
|
|
1116
|
+
tmp[m++] = IDNA_VBASE + (sidx % IDNA_NCOUNT) / IDNA_TCOUNT;
|
|
1117
|
+
unsigned t = sidx % IDNA_TCOUNT;
|
|
1118
|
+
if (t != 0) tmp[m++] = IDNA_TBASE + t;
|
|
1119
|
+
} else {
|
|
1120
|
+
const idna_decomp_row *d = idna_decomp_of(v);
|
|
1121
|
+
if (d != NULL) {
|
|
1122
|
+
for (unsigned k = 0; k < d->len; k++) {
|
|
1123
|
+
if (m >= cap || m >= sizeof tmp / sizeof tmp[0]) return 0;
|
|
1124
|
+
tmp[m++] = idna_seq_pool[d->off + k];
|
|
1125
|
+
}
|
|
1126
|
+
} else {
|
|
1127
|
+
if (m >= cap || m >= sizeof tmp / sizeof tmp[0]) return 0;
|
|
1128
|
+
tmp[m++] = v;
|
|
1129
|
+
}
|
|
1130
|
+
}
|
|
1131
|
+
}
|
|
1132
|
+
/* canonical order: stable bubble on combining class */
|
|
1133
|
+
for (;;) {
|
|
1134
|
+
bool moved = false;
|
|
1135
|
+
for (size_t i = 1; i < m; i++) {
|
|
1136
|
+
int a = idna_ccc_of(tmp[i - 1]);
|
|
1137
|
+
int b = idna_ccc_of(tmp[i]);
|
|
1138
|
+
if (a > b && b != 0) {
|
|
1139
|
+
unsigned t = tmp[i - 1];
|
|
1140
|
+
tmp[i - 1] = tmp[i];
|
|
1141
|
+
tmp[i] = t;
|
|
1142
|
+
moved = true;
|
|
1143
|
+
}
|
|
1144
|
+
}
|
|
1145
|
+
if (!moved) break;
|
|
1146
|
+
}
|
|
1147
|
+
/* compose: blocked iff a char between starter and current has a
|
|
1148
|
+
* combining class >= the current's; a successful merge removes
|
|
1149
|
+
* the blocker, so the last class stays at the starter's 0 */
|
|
1150
|
+
size_t out = 0;
|
|
1151
|
+
size_t starter = (size_t)-1;
|
|
1152
|
+
unsigned last_cc = 0;
|
|
1153
|
+
for (size_t i = 0; i < m; i++) {
|
|
1154
|
+
unsigned v = tmp[i];
|
|
1155
|
+
unsigned cc = (unsigned)idna_ccc_of(v);
|
|
1156
|
+
bool merged = false;
|
|
1157
|
+
if (starter != (size_t)-1 && (last_cc == 0 || last_cc < cc)) {
|
|
1158
|
+
unsigned comp = idna_comp_of(tmp[starter], v);
|
|
1159
|
+
if (comp == 0 &&
|
|
1160
|
+
tmp[starter] >= IDNA_LBASE &&
|
|
1161
|
+
tmp[starter] < IDNA_LBASE + IDNA_LCOUNT &&
|
|
1162
|
+
v >= IDNA_VBASE && v < IDNA_VBASE + IDNA_VCOUNT) {
|
|
1163
|
+
comp = IDNA_SBASE +
|
|
1164
|
+
(tmp[starter] - IDNA_LBASE) * IDNA_NCOUNT +
|
|
1165
|
+
(v - IDNA_VBASE) * IDNA_TCOUNT;
|
|
1166
|
+
} else if (comp == 0 &&
|
|
1167
|
+
tmp[starter] >= IDNA_SBASE &&
|
|
1168
|
+
tmp[starter] < IDNA_SBASE + IDNA_SCOUNT &&
|
|
1169
|
+
(tmp[starter] - IDNA_SBASE) % IDNA_TCOUNT == 0 &&
|
|
1170
|
+
v >= IDNA_TBASE && v < IDNA_TBASE + IDNA_TCOUNT) {
|
|
1171
|
+
comp = tmp[starter] + (v - IDNA_TBASE);
|
|
1172
|
+
}
|
|
1173
|
+
if (comp != 0) {
|
|
1174
|
+
tmp[starter] = comp;
|
|
1175
|
+
merged = true;
|
|
1176
|
+
}
|
|
1177
|
+
}
|
|
1178
|
+
if (merged) continue;
|
|
1179
|
+
if (cc == 0) {
|
|
1180
|
+
starter = out;
|
|
1181
|
+
last_cc = 0;
|
|
1182
|
+
} else {
|
|
1183
|
+
last_cc = cc;
|
|
1184
|
+
}
|
|
1185
|
+
tmp[out++] = v;
|
|
1186
|
+
}
|
|
1187
|
+
for (size_t i = 0; i < out; i++) cp[i] = tmp[i];
|
|
1188
|
+
return out;
|
|
1189
|
+
}
|
|
1190
|
+
|
|
1191
|
+
/* UTS #46 map (nontransitional): answers the code point count, or
|
|
1192
|
+
* SIZE_MAX on a disallowed character or malformed utf-8 */
|
|
1193
|
+
#define IDNA_CPS_CAP 1100
|
|
1194
|
+
static size_t idna_ut46_map(const char *s, size_t len, unsigned *out) {
|
|
1195
|
+
size_t i = 0, n = 0;
|
|
1196
|
+
while (i < len) {
|
|
1197
|
+
unsigned v;
|
|
1198
|
+
if (!idna_utf8_next(s, len, &i, &v)) return SIZE_MAX;
|
|
1199
|
+
const idna_map_row *r = idna_map_of(v);
|
|
1200
|
+
if (r == NULL || r->st == IDNA_DISALLOWED) return SIZE_MAX;
|
|
1201
|
+
if (r->st == IDNA_IGNORED) continue;
|
|
1202
|
+
if (r->st == IDNA_MAPPED) {
|
|
1203
|
+
size_t k = 0;
|
|
1204
|
+
while (k < r->len) {
|
|
1205
|
+
unsigned mv;
|
|
1206
|
+
if (!idna_utf8_next((const char *)(idna_map_pool + r->off),
|
|
1207
|
+
r->len, &k, &mv)) {
|
|
1208
|
+
return SIZE_MAX;
|
|
1209
|
+
}
|
|
1210
|
+
if (n >= IDNA_CPS_CAP) return SIZE_MAX;
|
|
1211
|
+
out[n++] = mv;
|
|
1212
|
+
}
|
|
1213
|
+
continue;
|
|
1214
|
+
}
|
|
1215
|
+
if (n >= IDNA_CPS_CAP) return SIZE_MAX;
|
|
1216
|
+
out[n++] = v;
|
|
1217
|
+
}
|
|
1218
|
+
return n;
|
|
1219
|
+
}
|
|
1220
|
+
|
|
1221
|
+
/* punycode (RFC 3492 appendix C, the ANSI C reference adapted to the
|
|
1222
|
+
* engine's size_t/bool; the case-flag array is dropped — the
|
|
1223
|
+
* canonical re-encode form is lowercase) */
|
|
1224
|
+
enum { PC_BASE = 36, PC_TMIN = 1, PC_TMAX = 26, PC_SKEW = 38,
|
|
1225
|
+
PC_DAMP = 700, PC_INITIAL_BIAS = 72, PC_INITIAL_N = 0x80 };
|
|
1226
|
+
|
|
1227
|
+
static unsigned pc_decode_digit(char c) {
|
|
1228
|
+
unsigned u = (unsigned char)c;
|
|
1229
|
+
return u - 48 < 10 ? u - 22 : u - 65 < 26 ? u - 65 :
|
|
1230
|
+
u - 97 < 26 ? u - 97 : PC_BASE;
|
|
1231
|
+
}
|
|
1232
|
+
|
|
1233
|
+
static char pc_encode_digit(unsigned d) {
|
|
1234
|
+
return (char)(d + 22 + 75 * (d < 26));
|
|
1235
|
+
}
|
|
1236
|
+
|
|
1237
|
+
static unsigned pc_adapt(unsigned delta, unsigned numpoints,
|
|
1238
|
+
bool firsttime) {
|
|
1239
|
+
unsigned k;
|
|
1240
|
+
delta = firsttime ? delta / PC_DAMP : delta >> 1;
|
|
1241
|
+
delta += delta / numpoints;
|
|
1242
|
+
for (k = 0; delta > ((PC_BASE - PC_TMIN) * PC_TMAX) / 2; k += PC_BASE) {
|
|
1243
|
+
delta /= PC_BASE - PC_TMIN;
|
|
1244
|
+
}
|
|
1245
|
+
return k + (PC_BASE - PC_TMIN + 1) * delta / (delta + PC_SKEW);
|
|
1246
|
+
}
|
|
1247
|
+
|
|
1248
|
+
static int pc_encode(const unsigned *input, size_t input_length,
|
|
1249
|
+
char *output, size_t *output_length) {
|
|
1250
|
+
size_t n, delta, h, b, out, max_out, bias, j, m, q, k, t;
|
|
1251
|
+
n = PC_INITIAL_N;
|
|
1252
|
+
delta = out = 0;
|
|
1253
|
+
max_out = *output_length;
|
|
1254
|
+
bias = PC_INITIAL_BIAS;
|
|
1255
|
+
for (j = 0; j < input_length; ++j) {
|
|
1256
|
+
if (input[j] < 0x80) {
|
|
1257
|
+
if (max_out - out < 2) return -2;
|
|
1258
|
+
output[out++] = (char)input[j];
|
|
1259
|
+
}
|
|
1260
|
+
}
|
|
1261
|
+
h = b = out;
|
|
1262
|
+
if (b > 0) output[out++] = '-';
|
|
1263
|
+
while (h < input_length) {
|
|
1264
|
+
for (m = SIZE_MAX, j = 0; j < input_length; ++j) {
|
|
1265
|
+
if (input[j] >= n && input[j] < m) m = input[j];
|
|
1266
|
+
}
|
|
1267
|
+
if (m - n > (SIZE_MAX - delta) / (h + 1)) return -3;
|
|
1268
|
+
delta += (m - n) * (h + 1);
|
|
1269
|
+
n = m;
|
|
1270
|
+
for (j = 0; j < input_length; ++j) {
|
|
1271
|
+
if (input[j] < n) {
|
|
1272
|
+
if (++delta == 0) return -3;
|
|
1273
|
+
}
|
|
1274
|
+
if (input[j] == n) {
|
|
1275
|
+
for (q = delta, k = PC_BASE;; k += PC_BASE) {
|
|
1276
|
+
if (out >= max_out) return -2;
|
|
1277
|
+
t = k <= bias ? PC_TMIN :
|
|
1278
|
+
k >= bias + PC_TMAX ? PC_TMAX : k - bias;
|
|
1279
|
+
if (q < t) break;
|
|
1280
|
+
output[out++] =
|
|
1281
|
+
pc_encode_digit(t + (q - t) % (PC_BASE - t));
|
|
1282
|
+
q = (q - t) / (PC_BASE - t);
|
|
1283
|
+
}
|
|
1284
|
+
output[out++] = pc_encode_digit(q);
|
|
1285
|
+
bias = pc_adapt((unsigned)delta, (unsigned)(h + 1), h == b);
|
|
1286
|
+
delta = 0;
|
|
1287
|
+
++h;
|
|
1288
|
+
}
|
|
1289
|
+
}
|
|
1290
|
+
++delta, ++n;
|
|
1291
|
+
}
|
|
1292
|
+
*output_length = out;
|
|
1293
|
+
return 0;
|
|
1294
|
+
}
|
|
1295
|
+
|
|
1296
|
+
static int pc_decode(const char *input, size_t input_length,
|
|
1297
|
+
unsigned *output, size_t *output_length) {
|
|
1298
|
+
unsigned n, bias, digit, t;
|
|
1299
|
+
size_t out = 0, i = 0, max_out = *output_length, b, j, in, oldi, w, k;
|
|
1300
|
+
n = PC_INITIAL_N;
|
|
1301
|
+
bias = PC_INITIAL_BIAS;
|
|
1302
|
+
for (b = j = 0; j < input_length; ++j) {
|
|
1303
|
+
if ((unsigned char)input[j] == '-') b = j;
|
|
1304
|
+
}
|
|
1305
|
+
if (b > max_out) return -2;
|
|
1306
|
+
for (j = 0; j < b; ++j) {
|
|
1307
|
+
if ((unsigned char)input[j] >= 0x80) return -1;
|
|
1308
|
+
output[out++] = (unsigned char)input[j];
|
|
1309
|
+
}
|
|
1310
|
+
for (in = b > 0 ? b + 1 : 0; in < input_length; ++out) {
|
|
1311
|
+
for (oldi = i, w = 1, k = PC_BASE;; k += PC_BASE) {
|
|
1312
|
+
if (in >= input_length) return -1;
|
|
1313
|
+
digit = pc_decode_digit(input[in++]);
|
|
1314
|
+
if (digit >= PC_BASE) return -1;
|
|
1315
|
+
if (digit > (SIZE_MAX - i) / w) return -3;
|
|
1316
|
+
i += digit * w;
|
|
1317
|
+
t = k <= bias ? PC_TMIN :
|
|
1318
|
+
k >= bias + PC_TMAX ? PC_TMAX : k - bias;
|
|
1319
|
+
if (digit < t) break;
|
|
1320
|
+
if (w > SIZE_MAX / (PC_BASE - t)) return -3;
|
|
1321
|
+
w *= (PC_BASE - t);
|
|
1322
|
+
}
|
|
1323
|
+
bias = pc_adapt((unsigned)(i - oldi), (unsigned)(out + 1),
|
|
1324
|
+
oldi == 0);
|
|
1325
|
+
if (i / (out + 1) > SIZE_MAX - n) return -3;
|
|
1326
|
+
n += (unsigned)(i / (out + 1));
|
|
1327
|
+
i %= (out + 1);
|
|
1328
|
+
if (out >= max_out) return -2;
|
|
1329
|
+
if (n > 0x10FFFF || (n >= 0xD800 && n <= 0xDFFF)) return -1;
|
|
1330
|
+
memmove(output + i + 1, output + i, (out - i) * sizeof *output);
|
|
1331
|
+
output[i++] = n;
|
|
1332
|
+
}
|
|
1333
|
+
*output_length = out;
|
|
1334
|
+
return 0;
|
|
1335
|
+
}
|
|
1336
|
+
|
|
1337
|
+
/* RFC 5892 appendix A: the nine context rules, evaluated on a label */
|
|
1338
|
+
static bool idna_context_ok(const unsigned *cp, size_t n) {
|
|
1339
|
+
bool an_arabic = false, an_ext = false, kana_ctx = false;
|
|
1340
|
+
for (size_t i = 0; i < n; i++) {
|
|
1341
|
+
if (cp[i] >= 0x0660 && cp[i] <= 0x0669) an_arabic = true;
|
|
1342
|
+
if (cp[i] >= 0x06F0 && cp[i] <= 0x06F9) an_ext = true;
|
|
1343
|
+
unsigned char sc = idna_script_of(cp[i]);
|
|
1344
|
+
if (sc == SC_HIRAGANA || sc == SC_KATAKANA || sc == SC_HAN) {
|
|
1345
|
+
kana_ctx = true;
|
|
1346
|
+
}
|
|
1347
|
+
}
|
|
1348
|
+
if (an_arabic && an_ext) return false; /* A.8 / A.9 */
|
|
1349
|
+
for (size_t i = 0; i < n; i++) {
|
|
1350
|
+
unsigned v = cp[i];
|
|
1351
|
+
unsigned before = i > 0 ? cp[i - 1] : 0;
|
|
1352
|
+
unsigned after = i + 1 < n ? cp[i + 1] : 0;
|
|
1353
|
+
switch (v) {
|
|
1354
|
+
case 0x200C: {
|
|
1355
|
+
/* A.1: virama, or the joining-type regexp */
|
|
1356
|
+
if (i > 0 && idna_is_virama(before)) break;
|
|
1357
|
+
size_t k = i;
|
|
1358
|
+
while (k > 0 && idna_join_of(cp[k - 1]) == J_T) k--;
|
|
1359
|
+
if (k == 0) return false;
|
|
1360
|
+
unsigned char jl = idna_join_of(cp[k - 1]);
|
|
1361
|
+
if (jl != J_L && jl != J_D) return false;
|
|
1362
|
+
k = i + 1;
|
|
1363
|
+
while (k < n && idna_join_of(cp[k]) == J_T) k++;
|
|
1364
|
+
if (k >= n) return false;
|
|
1365
|
+
unsigned char jr = idna_join_of(cp[k]);
|
|
1366
|
+
if (jr != J_R && jr != J_D) return false;
|
|
1367
|
+
break;
|
|
1368
|
+
}
|
|
1369
|
+
case 0x200D: /* A.2 */
|
|
1370
|
+
if (i == 0 || !idna_is_virama(before)) return false;
|
|
1371
|
+
break;
|
|
1372
|
+
case 0x00B7: /* A.3 */
|
|
1373
|
+
if (before != 'l' || after != 'l') return false;
|
|
1374
|
+
break;
|
|
1375
|
+
case 0x0375: /* A.4 */
|
|
1376
|
+
if (i + 1 >= n || idna_script_of(after) != SC_GREEK)
|
|
1377
|
+
return false;
|
|
1378
|
+
break;
|
|
1379
|
+
case 0x05F3: /* A.5 / A.6 */
|
|
1380
|
+
case 0x05F4:
|
|
1381
|
+
if (i == 0 || idna_script_of(before) != SC_HEBREW) return false;
|
|
1382
|
+
break;
|
|
1383
|
+
case 0x30FB: /* A.7 */
|
|
1384
|
+
if (!kana_ctx) return false;
|
|
1385
|
+
break;
|
|
1386
|
+
default:
|
|
1387
|
+
break;
|
|
1388
|
+
}
|
|
1389
|
+
}
|
|
1390
|
+
return true;
|
|
1391
|
+
}
|
|
1392
|
+
|
|
1393
|
+
/* RFC 5893: the six bidi rules; applies to every label of a bidi
|
|
1394
|
+
* domain (labels outside bidi domains ride free) */
|
|
1395
|
+
static bool idna_bidi_ok(const unsigned *cp, size_t n, bool in_bidi_domain) {
|
|
1396
|
+
unsigned char cls[256];
|
|
1397
|
+
bool rtl = false;
|
|
1398
|
+
if (n == 0 || n > sizeof cls) return false;
|
|
1399
|
+
for (size_t i = 0; i < n; i++) {
|
|
1400
|
+
cls[i] = idna_bidi_of(cp[i]);
|
|
1401
|
+
if (cls[i] == B_R || cls[i] == B_AL || cls[i] == B_AN) rtl = true;
|
|
1402
|
+
}
|
|
1403
|
+
if (!rtl) {
|
|
1404
|
+
if (!in_bidi_domain) return true;
|
|
1405
|
+
/* an LTR label: rule 1 (first char L), rule 5, rule 6 */
|
|
1406
|
+
if (cls[0] != B_L) return false;
|
|
1407
|
+
for (size_t i = 0; i < n; i++) {
|
|
1408
|
+
switch (cls[i]) {
|
|
1409
|
+
case B_L: case B_EN: case B_ES: case B_CS: case B_ET:
|
|
1410
|
+
case B_ON: case B_BN: case B_NSM:
|
|
1411
|
+
break;
|
|
1412
|
+
default:
|
|
1413
|
+
return false;
|
|
1414
|
+
}
|
|
1415
|
+
}
|
|
1416
|
+
size_t e = n;
|
|
1417
|
+
while (e > 0 && cls[e - 1] == B_NSM) e--;
|
|
1418
|
+
if (e == 0 || (cls[e - 1] != B_L && cls[e - 1] != B_EN))
|
|
1419
|
+
return false;
|
|
1420
|
+
return true;
|
|
1421
|
+
}
|
|
1422
|
+
/* an RTL label: rules 1-4 */
|
|
1423
|
+
if (cls[0] != B_R && cls[0] != B_AL) return false;
|
|
1424
|
+
bool has_en = false, has_an = false;
|
|
1425
|
+
for (size_t i = 0; i < n; i++) {
|
|
1426
|
+
switch (cls[i]) {
|
|
1427
|
+
case B_R: case B_AL: case B_AN: case B_EN: case B_ES:
|
|
1428
|
+
case B_CS: case B_ET: case B_ON: case B_BN: case B_NSM:
|
|
1429
|
+
break;
|
|
1430
|
+
default:
|
|
1431
|
+
return false;
|
|
1432
|
+
}
|
|
1433
|
+
if (cls[i] == B_EN) has_en = true;
|
|
1434
|
+
if (cls[i] == B_AN) has_an = true;
|
|
1435
|
+
}
|
|
1436
|
+
if (has_en && has_an) return false;
|
|
1437
|
+
size_t e = n;
|
|
1438
|
+
while (e > 0 && cls[e - 1] == B_NSM) e--;
|
|
1439
|
+
if (e == 0) return false;
|
|
1440
|
+
if (cls[e - 1] != B_R && cls[e - 1] != B_AL && cls[e - 1] != B_EN &&
|
|
1441
|
+
cls[e - 1] != B_AN) {
|
|
1442
|
+
return false;
|
|
1443
|
+
}
|
|
1444
|
+
return true;
|
|
1445
|
+
}
|
|
1446
|
+
|
|
1447
|
+
static bool idna_label_chars_ok(const unsigned *cp, size_t n) {
|
|
1448
|
+
for (size_t i = 0; i < n; i++) {
|
|
1449
|
+
const idna_map_row *r = idna_map_of(cp[i]);
|
|
1450
|
+
if (r == NULL || r->st != IDNA_VALID) return false;
|
|
1451
|
+
}
|
|
1452
|
+
return true;
|
|
1453
|
+
}
|
|
1454
|
+
|
|
1455
|
+
/* decode an A-label body (past xn--) and gate the U-label: chars,
|
|
1456
|
+
* context rules, the 3rd/4th '--' reservation, and the canonical
|
|
1457
|
+
* re-encode; answers the decoded code points for the bidi pass */
|
|
1458
|
+
static bool idna_alabel_decode(const char *body, size_t blen,
|
|
1459
|
+
unsigned *dec, size_t *dn) {
|
|
1460
|
+
size_t cap = 128;
|
|
1461
|
+
*dn = cap;
|
|
1462
|
+
if (pc_decode(body, blen, dec, dn) != 0) return false;
|
|
1463
|
+
if (*dn == 0) return false;
|
|
1464
|
+
bool nonascii = false;
|
|
1465
|
+
for (size_t i = 0; i < *dn; i++) {
|
|
1466
|
+
if (dec[i] >= 0x80) nonascii = true;
|
|
1467
|
+
}
|
|
1468
|
+
if (!nonascii) return false; /* an A-label decodes to a U-label */
|
|
1469
|
+
if (idna_is_mark(dec[0])) return false; /* criterion 6 */
|
|
1470
|
+
if (*dn >= 4 && dec[2] == '-' && dec[3] == '-') return false;
|
|
1471
|
+
char ub[256];
|
|
1472
|
+
size_t un = 0;
|
|
1473
|
+
for (size_t i = 0; i < *dn; i++) un += idna_utf8_put(ub + un, dec[i]);
|
|
1474
|
+
if (un > 63) return false;
|
|
1475
|
+
if (!idna_label_chars_ok(dec, *dn)) return false;
|
|
1476
|
+
if (!idna_context_ok(dec, *dn)) return false;
|
|
1477
|
+
char enc[128];
|
|
1478
|
+
size_t en = sizeof enc;
|
|
1479
|
+
if (pc_encode(dec, *dn, enc, &en) != 0) return false;
|
|
1480
|
+
if (en != blen) return false;
|
|
1481
|
+
for (size_t i = 0; i < blen; i++) {
|
|
1482
|
+
char c = body[i];
|
|
1483
|
+
if (c >= 'A' && c <= 'Z') c = (char)(c + 32);
|
|
1484
|
+
if (c != enc[i]) return false;
|
|
1485
|
+
}
|
|
1486
|
+
return true;
|
|
1487
|
+
}
|
|
1488
|
+
|
|
1489
|
+
/* a direct (non-ACE) label: chars, context, and the wire-form length;
|
|
1490
|
+
* hyphen edges and the '--' reservation ride RFC 5891 4.2.3.1 */
|
|
1491
|
+
static bool idna_ulabel_ok(const unsigned *cp, size_t n) {
|
|
1492
|
+
if (n == 0 || n > 63) return false;
|
|
1493
|
+
if (idna_is_mark(cp[0])) return false;
|
|
1494
|
+
if (cp[0] == '-' || cp[n - 1] == '-') return false;
|
|
1495
|
+
if (n >= 4 && cp[2] == '-' && cp[3] == '-') return false;
|
|
1496
|
+
if (!idna_label_chars_ok(cp, n)) return false;
|
|
1497
|
+
if (!idna_context_ok(cp, n)) return false;
|
|
1498
|
+
bool ascii = true;
|
|
1499
|
+
for (size_t i = 0; i < n; i++) {
|
|
1500
|
+
if (cp[i] >= 0x80) {
|
|
1501
|
+
ascii = false;
|
|
1502
|
+
break;
|
|
1503
|
+
}
|
|
1504
|
+
}
|
|
1505
|
+
if (ascii) return true; /* stays ASCII; the octet cap is the cps cap */
|
|
1506
|
+
char enc[128];
|
|
1507
|
+
size_t en = sizeof enc;
|
|
1508
|
+
if (pc_encode(cp, n, enc, &en) != 0) return false;
|
|
1509
|
+
if (en + 4 > 63) return false; /* xn-- plus the body must fit */
|
|
1510
|
+
return true;
|
|
1511
|
+
}
|
|
1512
|
+
|
|
1513
|
+
static bool check_hostname(const char *s, size_t len) {
|
|
1514
|
+
if (len == 0 || s[0] == '-' || s[len - 1] == '-') return false;
|
|
1515
|
+
if (cp_count(s, len) > 255) return false;
|
|
1516
|
+
for (size_t i = 0; i < len; i++) {
|
|
1517
|
+
char c = s[i];
|
|
1518
|
+
if (!(is_ascii_alnum(c) || c == '-' || c == '.')) return false;
|
|
1519
|
+
}
|
|
1520
|
+
size_t label = 0;
|
|
1521
|
+
for (size_t i = 0; i <= len; i++) {
|
|
1522
|
+
if (i == len || s[i] == '.') {
|
|
1523
|
+
const char *lp = s + i - label;
|
|
1524
|
+
if (label == 0) return false; /* empty label: '.', 'a..b' */
|
|
1525
|
+
if (cp_count(lp, label) > 63) return false;
|
|
1526
|
+
/* an ACE label (any-case prefix, RFC 5890 lookup) must
|
|
1527
|
+
* decode to a valid, canonical U-label */
|
|
1528
|
+
if (label >= 4 && (lp[0] | 32) == 'x' && (lp[1] | 32) == 'n' &&
|
|
1529
|
+
(lp[2] | 32) == '-' && (lp[3] | 32) == '-') {
|
|
1530
|
+
unsigned dec[128];
|
|
1531
|
+
size_t dn;
|
|
1532
|
+
if (!idna_alabel_decode(lp + 4, label - 4, dec, &dn))
|
|
1533
|
+
return false;
|
|
1534
|
+
}
|
|
1535
|
+
label = 0;
|
|
1536
|
+
} else {
|
|
1537
|
+
label++;
|
|
1538
|
+
}
|
|
1539
|
+
}
|
|
1540
|
+
return true;
|
|
1541
|
+
}
|
|
1542
|
+
|
|
1543
|
+
#define IDNA_DOM_CAP 1100
|
|
1544
|
+
static bool check_idn_hostname(const char *s, size_t len) {
|
|
1545
|
+
if (len == 0 || len > 253) return false;
|
|
1546
|
+
unsigned cps[IDNA_DOM_CAP];
|
|
1547
|
+
size_t n = idna_ut46_map(s, len, cps);
|
|
1548
|
+
if (n == SIZE_MAX) return false;
|
|
1549
|
+
size_t octets = 0;
|
|
1550
|
+
for (size_t i = 0; i < n; i++) octets += idna_utf8_len(cps[i]);
|
|
1551
|
+
if (octets > 253) return false;
|
|
1552
|
+
n = idna_nfc(cps, n, IDNA_DOM_CAP);
|
|
1553
|
+
if (n == 0) return false;
|
|
1554
|
+
/* bidi domains gate every label; the RTL-ness rides the U-label
|
|
1555
|
+
* form (a decoded ACE label counts) */
|
|
1556
|
+
bool bidi_domain = false;
|
|
1557
|
+
size_t ls = 0;
|
|
1558
|
+
for (size_t i = 0; i <= n; i++) {
|
|
1559
|
+
if (i != n && cps[i] != '.') continue;
|
|
1560
|
+
size_t ln = i - ls;
|
|
1561
|
+
const unsigned *lp = cps + ls;
|
|
1562
|
+
if (ln == 0) return false;
|
|
1563
|
+
if (ln >= 4 && lp[0] == 'x' && lp[1] == 'n' && lp[2] == '-' &&
|
|
1564
|
+
lp[3] == '-') {
|
|
1565
|
+
unsigned dec[128];
|
|
1566
|
+
size_t dn;
|
|
1567
|
+
char body[128];
|
|
1568
|
+
size_t bn = 0;
|
|
1569
|
+
for (size_t k = 4; k < ln; k++) {
|
|
1570
|
+
if (lp[k] >= 0x80 || bn >= sizeof body) return false;
|
|
1571
|
+
body[bn++] = (char)lp[k];
|
|
1572
|
+
}
|
|
1573
|
+
if (!idna_alabel_decode(body, bn, dec, &dn)) return false;
|
|
1574
|
+
if (idna_bidi_ok(dec, dn, true)) bidi_domain = true;
|
|
1575
|
+
} else if (idna_bidi_ok(lp, ln, true)) {
|
|
1576
|
+
bidi_domain = true;
|
|
1577
|
+
}
|
|
1578
|
+
ls = i + 1;
|
|
1579
|
+
}
|
|
1580
|
+
ls = 0;
|
|
1581
|
+
for (size_t i = 0; i <= n; i++) {
|
|
1582
|
+
if (i != n && cps[i] != '.') continue;
|
|
1583
|
+
size_t ln = i - ls;
|
|
1584
|
+
const unsigned *lp = cps + ls;
|
|
1585
|
+
if (ln >= 4 && lp[0] == 'x' && lp[1] == 'n' && lp[2] == '-' &&
|
|
1586
|
+
lp[3] == '-') {
|
|
1587
|
+
unsigned dec[128];
|
|
1588
|
+
size_t dn;
|
|
1589
|
+
char body[128];
|
|
1590
|
+
size_t bn = 0;
|
|
1591
|
+
for (size_t k = 4; k < ln; k++) {
|
|
1592
|
+
if (lp[k] >= 0x80 || bn >= sizeof body) return false;
|
|
1593
|
+
body[bn++] = (char)lp[k];
|
|
1594
|
+
}
|
|
1595
|
+
if (!idna_alabel_decode(body, bn, dec, &dn)) return false;
|
|
1596
|
+
if (!idna_bidi_ok(dec, dn, bidi_domain)) return false;
|
|
1597
|
+
} else {
|
|
1598
|
+
if (!idna_ulabel_ok(lp, ln)) return false;
|
|
1599
|
+
if (!idna_bidi_ok(lp, ln, bidi_domain)) return false;
|
|
1600
|
+
}
|
|
1601
|
+
ls = i + 1;
|
|
1602
|
+
}
|
|
1603
|
+
return true;
|
|
1604
|
+
}
|
|
1605
|
+
|
|
607
1606
|
/* ----------------------------------------------------------- entry -- */
|
|
608
1607
|
|
|
609
1608
|
bool sch_format_string_check(int format, const char *s, size_t len,
|
|
@@ -615,7 +1614,7 @@ bool sch_format_string_check(int format, const char *s, size_t len,
|
|
|
615
1614
|
/* the local shape: full-date T HH:MM:SS[.frac], no offset */
|
|
616
1615
|
if (len < 19 || !check_date(s, 10)) return false;
|
|
617
1616
|
if (s[10] != 'T' && s[10] != 't') return false;
|
|
618
|
-
size_t used = check_hms(s + 11, len - 11);
|
|
1617
|
+
size_t used = check_hms(s + 11, len - 11, false, NULL, NULL, NULL);
|
|
619
1618
|
if (used == 0) return false;
|
|
620
1619
|
size_t pos = 11 + used;
|
|
621
1620
|
if (pos < len && s[pos] == '.') {
|
|
@@ -630,7 +1629,7 @@ bool sch_format_string_check(int format, const char *s, size_t len,
|
|
|
630
1629
|
return pos == len;
|
|
631
1630
|
}
|
|
632
1631
|
case FMT_TIME: {
|
|
633
|
-
size_t used = check_hms(s, len);
|
|
1632
|
+
size_t used = check_hms(s, len, false, NULL, NULL, NULL);
|
|
634
1633
|
if (used == 0) return false;
|
|
635
1634
|
size_t pos = used;
|
|
636
1635
|
if (pos < len && s[pos] == '.') {
|
|
@@ -640,13 +1639,13 @@ bool sch_format_string_check(int format, const char *s, size_t len,
|
|
|
640
1639
|
pos++;
|
|
641
1640
|
frac++;
|
|
642
1641
|
}
|
|
643
|
-
if (frac
|
|
1642
|
+
if (frac == 0) return false; /* any precision >= 1 */
|
|
644
1643
|
}
|
|
645
1644
|
return len > pos && check_offset(s + pos, len - pos) == len - pos;
|
|
646
1645
|
}
|
|
647
1646
|
case FMT_TIME_LOCAL: {
|
|
648
1647
|
/* HH:MM:SS[.frac] with no offset (TOML's local time) */
|
|
649
|
-
size_t used = check_hms(s, len);
|
|
1648
|
+
size_t used = check_hms(s, len, false, NULL, NULL, NULL);
|
|
650
1649
|
if (used == 0) return false;
|
|
651
1650
|
size_t pos = used;
|
|
652
1651
|
if (pos < len && s[pos] == '.') {
|
|
@@ -660,16 +1659,16 @@ bool sch_format_string_check(int format, const char *s, size_t len,
|
|
|
660
1659
|
}
|
|
661
1660
|
return pos == len;
|
|
662
1661
|
}
|
|
663
|
-
case FMT_EMAIL:
|
|
664
|
-
case FMT_IDN_EMAIL: return
|
|
665
|
-
case FMT_HOSTNAME:
|
|
666
|
-
case FMT_IDN_HOSTNAME: return
|
|
1662
|
+
case FMT_EMAIL: return check_email(s, len);
|
|
1663
|
+
case FMT_IDN_EMAIL: return check_idn_email(s, len);
|
|
1664
|
+
case FMT_HOSTNAME: return check_hostname(s, len);
|
|
1665
|
+
case FMT_IDN_HOSTNAME: return check_idn_hostname(s, len);
|
|
667
1666
|
case FMT_IPV4: return check_ipv4(s, len);
|
|
668
1667
|
case FMT_IPV6: return check_ipv6(s, len);
|
|
669
|
-
case FMT_URI:
|
|
670
|
-
case FMT_IRI: return
|
|
671
|
-
case FMT_URI_REFERENCE:
|
|
672
|
-
case FMT_IRI_REFERENCE: return
|
|
1668
|
+
case FMT_URI: return check_uri(s, len);
|
|
1669
|
+
case FMT_IRI: return check_iri_like(s, len, true);
|
|
1670
|
+
case FMT_URI_REFERENCE: return check_uri_reference(s, len);
|
|
1671
|
+
case FMT_IRI_REFERENCE: return check_iri_like(s, len, false);
|
|
673
1672
|
case FMT_JSON_POINTER: return check_json_pointer(s, len);
|
|
674
1673
|
case FMT_RELATIVE_JSON_POINTER:
|
|
675
1674
|
return check_relative_json_pointer(s, len);
|