Debian Codegraph @

components/playlist/xspf/uriparser/UriParse.c

raw symbols SCIP
1 /*
2 * uriparser - RFC 3986 URI parsing library
3 *
4 * Copyright (C) 2007, Weijia Song <[email protected]>
5 * Copyright (C) 2007, Sebastian Pipping <[email protected]>
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer in the documentation and/or other materials
19 * provided with the distribution.
20 *
21 * 3. Neither the name of the copyright holder nor the names of
22 * its contributors may be used to endorse or promote products
23 * derived from this software without specific prior written
24 * permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
29 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
30 * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
31 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
32 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
33 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
35 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
37 * OF THE POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /**
41 * @file UriParse.c
42 * Holds the RFC 3986 %URI parsing implementation.
43 * NOTE: This source file includes itself twice.
44 */
45
46 /* What encodings are enabled? */
47 #include <uriparser/UriDefsConfig.h>
48 #if (!defined(URI_PASS_ANSI) && !defined(URI_PASS_UNICODE))
49 /* Include SELF twice */
50 # ifdef URI_ENABLE_ANSI
51 # define URI_PASS_ANSI 1
52 # include "UriParse.c"
53 # undef URI_PASS_ANSI
54 # endif
55 # ifdef URI_ENABLE_UNICODE
56 # define URI_PASS_UNICODE 1
57 # include "UriParse.c"
58 # undef URI_PASS_UNICODE
59 # endif
60 #else
61 # ifdef URI_PASS_ANSI
62 # include <uriparser/UriDefsAnsi.h>
63 # else
64 # include <uriparser/UriDefsUnicode.h>
65 # include <wchar.h>
66 # endif
67
68 # ifndef URI_DOXYGEN
69 # include <uriparser/Uri.h>
70 # include <uriparser/UriIp4.h>
71 # include "UriCommon.h"
72 # include "UriMemory.h"
73 # include "UriParseBase.h"
74 # include "UriSets.h"
75 # endif
76
77 static const URI_CHAR * URI_FUNC(ParseAuthority)(URI_TYPE(ParserState) * state,
78 const URI_CHAR * first,
79 const URI_CHAR * afterLast,
80 UriMemoryManager * memory);
81 static const URI_CHAR * URI_FUNC(ParseAuthorityTwo)(URI_TYPE(ParserState) * state,
82 const URI_CHAR * first,
83 const URI_CHAR * afterLast);
84 static const URI_CHAR * URI_FUNC(ParseHexZero)(const URI_CHAR * first,
85 const URI_CHAR * afterLast);
86 static const URI_CHAR * URI_FUNC(ParseHierPart)(URI_TYPE(ParserState) * state,
87 const URI_CHAR * first,
88 const URI_CHAR * afterLast,
89 UriMemoryManager * memory);
90 static const URI_CHAR * URI_FUNC(ParseIpFutLoop)(URI_TYPE(ParserState) * state,
91 const URI_CHAR * first,
92 const URI_CHAR * afterLast,
93 UriMemoryManager * memory);
94 static const URI_CHAR * URI_FUNC(ParseIpLit2)(URI_TYPE(ParserState) * state,
95 const URI_CHAR * first,
96 const URI_CHAR * afterLast,
97 UriMemoryManager * memory);
98 static const URI_CHAR * URI_FUNC(ParseIPv6address2)(URI_TYPE(ParserState) * state,
99 const URI_CHAR * first,
100 const URI_CHAR * afterLast,
101 UriMemoryManager * memory);
102 static const URI_CHAR * URI_FUNC(ParseMustBeSegmentNzNc)(URI_TYPE(ParserState) * state,
103 const URI_CHAR * first,
104 const URI_CHAR * afterLast,
105 UriMemoryManager * memory);
106 static const URI_CHAR * URI_FUNC(ParseOwnHost)(URI_TYPE(ParserState) * state,
107 const URI_CHAR * first,
108 const URI_CHAR * afterLast,
109 UriMemoryManager * memory);
110 static const URI_CHAR * URI_FUNC(ParseOwnHost2)(URI_TYPE(ParserState) * state,
111 const URI_CHAR * first,
112 const URI_CHAR * afterLast,
113 UriMemoryManager * memory);
114 static const URI_CHAR * URI_FUNC(ParseOwnHostUserInfoNz)(URI_TYPE(ParserState) * state,
115 const URI_CHAR * first,
116 const URI_CHAR * afterLast,
117 UriMemoryManager * memory);
118 static const URI_CHAR * URI_FUNC(ParseOwnPortUserInfo)(URI_TYPE(ParserState) * state,
119 const URI_CHAR * first,
120 const URI_CHAR * afterLast,
121 UriMemoryManager * memory);
122 static const URI_CHAR * URI_FUNC(ParseOwnUserInfo)(URI_TYPE(ParserState) * state,
123 const URI_CHAR * first,
124 const URI_CHAR * afterLast,
125 UriMemoryManager * memory);
126 static const URI_CHAR * URI_FUNC(ParsePartHelperTwo)(URI_TYPE(ParserState) * state,
127 const URI_CHAR * first,
128 const URI_CHAR * afterLast,
129 UriMemoryManager * memory);
130 static const URI_CHAR * URI_FUNC(ParsePathAbsEmpty)(URI_TYPE(ParserState) * state,
131 const URI_CHAR * first,
132 const URI_CHAR * afterLast,
133 UriMemoryManager * memory);
134 static const URI_CHAR * URI_FUNC(ParsePathAbsNoLeadSlash)(URI_TYPE(ParserState) * state,
135 const URI_CHAR * first,
136 const URI_CHAR * afterLast,
137 UriMemoryManager * memory);
138 static const URI_CHAR * URI_FUNC(ParsePathRootless)(URI_TYPE(ParserState) * state,
139 const URI_CHAR * first,
140 const URI_CHAR * afterLast,
141 UriMemoryManager * memory);
142 static const URI_CHAR * URI_FUNC(ParsePchar)(URI_TYPE(ParserState) * state,
143 const URI_CHAR * first,
144 const URI_CHAR * afterLast,
145 UriMemoryManager * memory);
146 static const URI_CHAR * URI_FUNC(ParsePctEncoded)(URI_TYPE(ParserState) * state,
147 const URI_CHAR * first,
148 const URI_CHAR * afterLast,
149 UriMemoryManager * memory);
150 static const URI_CHAR * URI_FUNC(ParsePctSubUnres)(URI_TYPE(ParserState) * state,
151 const URI_CHAR * first,
152 const URI_CHAR * afterLast,
153 UriMemoryManager * memory);
154 static const URI_CHAR * URI_FUNC(ParsePort)(const URI_CHAR * first,
155 const URI_CHAR * afterLast);
156 static const URI_CHAR * URI_FUNC(ParseQueryFrag)(URI_TYPE(ParserState) * state,
157 const URI_CHAR * first,
158 const URI_CHAR * afterLast,
159 UriMemoryManager * memory);
160 static const URI_CHAR * URI_FUNC(ParseSegment)(URI_TYPE(ParserState) * state,
161 const URI_CHAR * first,
162 const URI_CHAR * afterLast,
163 UriMemoryManager * memory);
164 static const URI_CHAR * URI_FUNC(ParseSegmentNz)(URI_TYPE(ParserState) * state,
165 const URI_CHAR * first,
166 const URI_CHAR * afterLast,
167 UriMemoryManager * memory);
168 static const URI_CHAR * URI_FUNC(ParseSegmentNzNcOrScheme2)(URI_TYPE(ParserState) * state,
169 const URI_CHAR * first,
170 const URI_CHAR * afterLast,
171 UriMemoryManager * memory);
172 static const URI_CHAR * URI_FUNC(ParseUriReference)(URI_TYPE(ParserState) * state,
173 const URI_CHAR * first,
174 const URI_CHAR * afterLast,
175 UriMemoryManager * memory);
176 static const URI_CHAR * URI_FUNC(ParseUriTail)(URI_TYPE(ParserState) * state,
177 const URI_CHAR * first,
178 const URI_CHAR * afterLast,
179 UriMemoryManager * memory);
180 static const URI_CHAR * URI_FUNC(ParseUriTailTwo)(URI_TYPE(ParserState) * state,
181 const URI_CHAR * first,
182 const URI_CHAR * afterLast,
183 UriMemoryManager * memory);
184 static const URI_CHAR * URI_FUNC(ParseZeroMoreSlashSegs)(URI_TYPE(ParserState) * state,
185 const URI_CHAR * first,
186 const URI_CHAR * afterLast,
187 UriMemoryManager * memory);
188
189 static UriBool URI_FUNC(OnExitOwnHost2)(URI_TYPE(ParserState) * state,
190 const URI_CHAR * first,
191 UriMemoryManager * memory);
192 static UriBool URI_FUNC(OnExitOwnHostUserInfo)(URI_TYPE(ParserState) * state,
193 const URI_CHAR * first,
194 UriMemoryManager * memory);
195 static UriBool URI_FUNC(OnExitOwnPortUserInfo)(URI_TYPE(ParserState) * state,
196 const URI_CHAR * first,
197 UriMemoryManager * memory);
198 static UriBool URI_FUNC(OnExitSegmentNzNcOrScheme2)(URI_TYPE(ParserState) * state,
199 const URI_CHAR * first,
200 UriMemoryManager * memory);
201 static void URI_FUNC(OnExitPartHelperTwo)(URI_TYPE(ParserState) * state);
202
203 static void URI_FUNC(ResetParserStateExceptUri)(URI_TYPE(ParserState) * state);
204
205 static UriBool URI_FUNC(PushPathSegment)(URI_TYPE(ParserState) * state,
206 const URI_CHAR * first,
207 const URI_CHAR * afterLast,
208 UriMemoryManager * memory);
209
210 static void URI_FUNC(StopSyntax)(URI_TYPE(ParserState) * state, const URI_CHAR * errorPos,
211 UriMemoryManager * memory);
212 static void URI_FUNC(StopMalloc)(URI_TYPE(ParserState) * state,
213 UriMemoryManager * memory);
214
215 static int URI_FUNC(ParseUriExMm)(URI_TYPE(ParserState) * state, const URI_CHAR * first,
216 const URI_CHAR * afterLast, UriMemoryManager * memory);
217
218 static URI_INLINE void URI_FUNC(StopSyntax)(URI_TYPE(ParserState) * state,
219 const URI_CHAR * errorPos,
220 UriMemoryManager * memory) {
221 URI_FUNC(FreeUriMembersMm)(state->uri, memory);
222 state->errorPos = errorPos;
223 state->errorCode = URI_ERROR_SYNTAX;
224 }
225
226 static URI_INLINE void URI_FUNC(StopMalloc)(URI_TYPE(ParserState) * state,
227 UriMemoryManager * memory) {
228 URI_FUNC(FreeUriMembersMm)(state->uri, memory);
229 state->errorPos = NULL;
230 state->errorCode = URI_ERROR_MALLOC;
231 }
232
233 /*
234 * [authority]-><[>[ipLit2][authorityTwo]
235 * [authority]->[ownHostUserInfoNz]
236 * [authority]-><NULL>
237 */
238 static URI_INLINE const URI_CHAR * URI_FUNC(ParseAuthority)(URI_TYPE(ParserState) * state,
239 const URI_CHAR * first,
240 const URI_CHAR * afterLast,
241 UriMemoryManager * memory) {
242 if (first >= afterLast) {
243 /* "" regname host */
244 state->uri->hostText.first = URI_FUNC(SafeToPointTo);
245 state->uri->hostText.afterLast = URI_FUNC(SafeToPointTo);
246 return afterLast;
247 }
248
249 switch (*first) {
250 case _UT('['): {
251 const URI_CHAR * const afterIpLit2 =
252 URI_FUNC(ParseIpLit2)(state, first + 1, afterLast, memory);
253 if (afterIpLit2 == NULL) {
254 return NULL;
255 }
256 state->uri->hostText.first = first + 1; /* HOST BEGIN */
257 return URI_FUNC(ParseAuthorityTwo)(state, afterIpLit2, afterLast);
258 }
259
260 case URI_SET_PCHAR(_UT):
261 state->uri->userInfo.first = first; /* USERINFO BEGIN */
262 return URI_FUNC(ParseOwnHostUserInfoNz)(state, first, afterLast, memory);
263
264 default:
265 /* "" regname host */
266 state->uri->hostText.first = URI_FUNC(SafeToPointTo);
267 state->uri->hostText.afterLast = URI_FUNC(SafeToPointTo);
268 return first;
269 }
270 }
271
272 /*
273 * [authorityTwo]-><:>[port]
274 * [authorityTwo]-><NULL>
275 */
276 static URI_INLINE const URI_CHAR *
277 URI_FUNC(ParseAuthorityTwo)(URI_TYPE(ParserState) * state, const URI_CHAR * first,
278 const URI_CHAR * afterLast) {
279 if (first >= afterLast) {
280 return afterLast;
281 }
282
283 switch (*first) {
284 case _UT(':'): {
285 const URI_CHAR * const afterPort = URI_FUNC(ParsePort)(first + 1, afterLast);
286 if (afterPort == NULL) {
287 return NULL;
288 }
289 state->uri->portText.first = first + 1; /* PORT BEGIN */
290 state->uri->portText.afterLast = afterPort; /* PORT END */
291 return afterPort;
292 }
293
294 default:
295 return first;
296 }
297 }
298
299 /*
300 * [hexZero]->[HEXDIG][hexZero]
301 * [hexZero]-><NULL>
302 */
303 static const URI_CHAR * URI_FUNC(ParseHexZero)(const URI_CHAR * first,
304 const URI_CHAR * afterLast) {
305 tail_call:
306 if (first >= afterLast) {
307 return afterLast;
308 }
309
310 switch (*first) {
311 case URI_SET_HEXDIG(_UT):
312 first += 1;
313 goto tail_call;
314
315 default:
316 return first;
317 }
318 }
319
320 /*
321 * [hierPart]->[pathRootless]
322 * [hierPart]-></>[partHelperTwo]
323 * [hierPart]-><NULL>
324 */
325 static URI_INLINE const URI_CHAR * URI_FUNC(ParseHierPart)(URI_TYPE(ParserState) * state,
326 const URI_CHAR * first,
327 const URI_CHAR * afterLast,
328 UriMemoryManager * memory) {
329 if (first >= afterLast) {
330 return afterLast;
331 }
332
333 switch (*first) {
334 case URI_SET_PCHAR(_UT):
335 return URI_FUNC(ParsePathRootless)(state, first, afterLast, memory);
336
337 case _UT('/'):
338 return URI_FUNC(ParsePartHelperTwo)(state, first + 1, afterLast, memory);
339
340 default:
341 return first;
342 }
343 }
344
345 /*
346 * [ipFutLoop]->[subDelims][ipFutStopGo]
347 * [ipFutLoop]->[unreserved][ipFutStopGo]
348 * [ipFutLoop]-><:>[ipFutStopGo]
349 *
350 * [ipFutStopGo]->[ipFutLoop]
351 * [ipFutStopGo]-><NULL>
352 */
353 static const URI_CHAR * URI_FUNC(ParseIpFutLoop)(URI_TYPE(ParserState) * state,
354 const URI_CHAR * first,
355 const URI_CHAR * afterLast,
356 UriMemoryManager * memory) {
357 const URI_CHAR * const originalFirst = first;
358
359 while (first < afterLast) {
360 switch (*first) {
361 case _UT(':'):
362 case URI_SET_SUB_DELIMS(_UT):
363 case URI_SET_UNRESERVED(_UT):
364 first += 1;
365 break;
366
367 default:
368 goto done_looping;
369 break;
370 }
371 }
372
373 done_looping:
374 if (first == originalFirst) {
375 URI_FUNC(StopSyntax)(state, first, memory);
376 return NULL;
377 }
378
379 return first;
380 }
381
382 /*
383 * [ipFuture]-><v>[HEXDIG][hexZero]<.>[ipFutLoop]
384 */
385 static const URI_CHAR * URI_FUNC(ParseIpFuture)(URI_TYPE(ParserState) * state,
386 const URI_CHAR * first,
387 const URI_CHAR * afterLast,
388 UriMemoryManager * memory) {
389 if (first >= afterLast) {
390 URI_FUNC(StopSyntax)(state, afterLast, memory);
391 return NULL;
392 }
393
394 /*
395 First character has already been
396 checked before entering this rule.
397
398 switch (*first) {
399 case _UT('v'):
400 case _UT('V'):
401 */
402 if (afterLast - first < 2) {
403 URI_FUNC(StopSyntax)(state, afterLast, memory);
404 return NULL;
405 }
406
407 switch (first[1]) {
408 case URI_SET_HEXDIG(_UT): {
409 const URI_CHAR * afterIpFutLoop;
410 const URI_CHAR * const afterHexZero =
411 URI_FUNC(ParseHexZero)(first + 2, afterLast);
412 if (afterHexZero == NULL) {
413 return NULL;
414 }
415 if (afterHexZero >= afterLast) {
416 URI_FUNC(StopSyntax)(state, afterLast, memory);
417 return NULL;
418 }
419 if (*afterHexZero != _UT('.')) {
420 URI_FUNC(StopSyntax)(state, afterHexZero, memory);
421 return NULL;
422 }
423 state->uri->hostText.first = first; /* HOST BEGIN */
424 state->uri->hostData.ipFuture.first = first; /* IPFUTURE BEGIN */
425 afterIpFutLoop =
426 URI_FUNC(ParseIpFutLoop)(state, afterHexZero + 1, afterLast, memory);
427 if (afterIpFutLoop == NULL) {
428 return NULL;
429 }
430 state->uri->hostText.afterLast = afterIpFutLoop; /* HOST END */
431 state->uri->hostData.ipFuture.afterLast = afterIpFutLoop; /* IPFUTURE END */
432 return afterIpFutLoop;
433 }
434
435 default:
436 URI_FUNC(StopSyntax)(state, first + 1, memory);
437 return NULL;
438 }
439
440 /*
441 default:
442 URI_FUNC(StopSyntax)(state, first, memory);
443 return NULL;
444 }
445 */
446 }
447
448 /*
449 * [ipLit2]->[ipFuture]<]>
450 * [ipLit2]->[IPv6address2]
451 */
452 static URI_INLINE const URI_CHAR * URI_FUNC(ParseIpLit2)(URI_TYPE(ParserState) * state,
453 const URI_CHAR * first,
454 const URI_CHAR * afterLast,
455 UriMemoryManager * memory) {
456 if (first >= afterLast) {
457 URI_FUNC(StopSyntax)(state, afterLast, memory);
458 return NULL;
459 }
460
461 switch (*first) {
462 /* The leading "v" of IPvFuture is case-insensitive. */
463 case _UT('v'):
464 case _UT('V'): {
465 const URI_CHAR * const afterIpFuture =
466 URI_FUNC(ParseIpFuture)(state, first, afterLast, memory);
467 if (afterIpFuture == NULL) {
468 return NULL;
469 }
470 if (afterIpFuture >= afterLast) {
471 URI_FUNC(StopSyntax)(state, afterLast, memory);
472 return NULL;
473 }
474 if (*afterIpFuture != _UT(']')) {
475 URI_FUNC(StopSyntax)(state, afterIpFuture, memory);
476 return NULL;
477 }
478 return afterIpFuture + 1;
479 }
480
481 case _UT(':'):
482 case _UT(']'):
483 case URI_SET_HEXDIG(_UT):
484 state->uri->hostData.ip6 = memory->malloc(
485 memory, 1 * sizeof(UriIp6)); /* Freed when stopping on parse error */
486 if (state->uri->hostData.ip6 == NULL) {
487 URI_FUNC(StopMalloc)(state, memory);
488 return NULL;
489 }
490 return URI_FUNC(ParseIPv6address2)(state, first, afterLast, memory);
491
492 default:
493 URI_FUNC(StopSyntax)(state, first, memory);
494 return NULL;
495 }
496 }
497
498 /*
499 * [IPv6address2]->..<]>
500 */
501 static const URI_CHAR * URI_FUNC(ParseIPv6address2)(URI_TYPE(ParserState) * state,
502 const URI_CHAR * first,
503 const URI_CHAR * afterLast,
504 UriMemoryManager * memory) {
505 int zipperEver = 0;
506 int quadsDone = 0;
507 int digitCount = 0;
508 unsigned char digitHistory[4];
509 int ip4OctetsDone = 0;
510
511 unsigned char quadsAfterZipper[14];
512 int quadsAfterZipperCount = 0;
513
514 for (;;) {
515 if (first >= afterLast) {
516 URI_FUNC(StopSyntax)(state, afterLast, memory);
517 return NULL;
518 }
519
520 /* Inside IPv4 part? */
521 if (ip4OctetsDone > 0) {
522 /* Eat rest of IPv4 address */
523 for (;;) {
524 switch (*first) {
525 case URI_SET_DIGIT(_UT):
526 if (digitCount == 4) {
527 URI_FUNC(StopSyntax)(state, first, memory);
528 return NULL;
529 }
530 digitHistory[digitCount++] = (unsigned char)(9 + *first - _UT('9'));
531 break;
532
533 case _UT('.'):
534 if ((ip4OctetsDone == 4) /* NOTE! */
535 || (digitCount == 0) || (digitCount == 4)) {
536 /* Invalid digit or octet count */
537 URI_FUNC(StopSyntax)(state, first, memory);
538 return NULL;
539 } else if ((digitCount > 1) && (digitHistory[0] == 0)) {
540 /* Leading zero */
541 URI_FUNC(StopSyntax)(state, first - digitCount, memory);
542 return NULL;
543 } else if ((digitCount == 3)
544 && (100 * digitHistory[0] + 10 * digitHistory[1]
545 + digitHistory[2]
546 > 255)) {
547 /* Octet value too large */
548 if (digitHistory[0] > 2) {
549 URI_FUNC(StopSyntax)(state, first - 3, memory);
550 } else if (digitHistory[1] > 5) {
551 URI_FUNC(StopSyntax)(state, first - 2, memory);
552 } else {
553 URI_FUNC(StopSyntax)(state, first - 1, memory);
554 }
555 return NULL;
556 }
557
558 /* Copy IPv4 octet */
559 state->uri->hostData.ip6->data[16 - 4 + ip4OctetsDone] =
560 uriGetOctetValue(digitHistory, digitCount);
561 digitCount = 0;
562 ip4OctetsDone++;
563 break;
564
565 case _UT(']'):
566 if ((ip4OctetsDone != 3) /* NOTE! */
567 || (digitCount == 0) || (digitCount == 4)) {
568 /* Invalid digit or octet count */
569 URI_FUNC(StopSyntax)(state, first, memory);
570 return NULL;
571 } else if ((digitCount > 1) && (digitHistory[0] == 0)) {
572 /* Leading zero */
573 URI_FUNC(StopSyntax)(state, first - digitCount, memory);
574 return NULL;
575 } else if ((digitCount == 3)
576 && (100 * digitHistory[0] + 10 * digitHistory[1]
577 + digitHistory[2]
578 > 255)) {
579 /* Octet value too large */
580 if (digitHistory[0] > 2) {
581 URI_FUNC(StopSyntax)(state, first - 3, memory);
582 } else if (digitHistory[1] > 5) {
583 URI_FUNC(StopSyntax)(state, first - 2, memory);
584 } else {
585 URI_FUNC(StopSyntax)(state, first - 1, memory);
586 }
587 return NULL;
588 }
589
590 state->uri->hostText.afterLast = first; /* HOST END */
591
592 /* Copy missing quads right before IPv4 */
593 memcpy(state->uri->hostData.ip6->data + 16 - 4
594 - 2 * quadsAfterZipperCount,
595 quadsAfterZipper, 2 * quadsAfterZipperCount);
596
597 /* Copy last IPv4 octet */
598 state->uri->hostData.ip6->data[16 - 4 + 3] =
599 uriGetOctetValue(digitHistory, digitCount);
600
601 return first + 1;
602
603 default:
604 URI_FUNC(StopSyntax)(state, first, memory);
605 return NULL;
606 }
607 first++;
608
609 if (first >= afterLast) {
610 URI_FUNC(StopSyntax)(state, afterLast, memory);
611 return NULL;
612 }
613 }
614 } else {
615 /* Eat while no dot in sight */
616 int letterAmong = 0;
617 int walking = 1;
618 do {
619 switch (*first) {
620 case URI_SET_HEX_LETTER_LOWER(_UT):
621 letterAmong = 1;
622 if (digitCount == 4) {
623 URI_FUNC(StopSyntax)(state, first, memory);
624 return NULL;
625 }
626 digitHistory[digitCount] = (unsigned char)(15 + *first - _UT('f'));
627 digitCount++;
628 break;
629
630 case URI_SET_HEX_LETTER_UPPER(_UT):
631 letterAmong = 1;
632 if (digitCount == 4) {
633 URI_FUNC(StopSyntax)(state, first, memory);
634 return NULL;
635 }
636 digitHistory[digitCount] = (unsigned char)(15 + *first - _UT('F'));
637 digitCount++;
638 break;
639
640 case URI_SET_DIGIT(_UT):
641 if (digitCount == 4) {
642 URI_FUNC(StopSyntax)(state, first, memory);
643 return NULL;
644 }
645 digitHistory[digitCount] = (unsigned char)(9 + *first - _UT('9'));
646 digitCount++;
647 break;
648
649 case _UT(':'): {
650 int setZipper = 0;
651
652 if (digitCount > 0) {
653 if (zipperEver) {
654 uriWriteQuadToDoubleByte(digitHistory, digitCount,
655 quadsAfterZipper
656 + 2 * quadsAfterZipperCount);
657 quadsAfterZipperCount++;
658 } else {
659 uriWriteQuadToDoubleByte(digitHistory, digitCount,
660 state->uri->hostData.ip6->data
661 + 2 * quadsDone);
662 }
663 quadsDone++;
664 digitCount = 0;
665 }
666 letterAmong = 0;
667
668 /* Too many quads? */
669 if (quadsDone >= 8 - zipperEver) {
670 URI_FUNC(StopSyntax)(state, first, memory);
671 return NULL;
672 }
673
674 /* "::"? */
675 if (afterLast - first < 2) {
676 URI_FUNC(StopSyntax)(state, afterLast, memory);
677 return NULL;
678 }
679 if (first[1] == _UT(':')) {
680 const int resetOffset = 2 * (quadsDone + (digitCount > 0));
681
682 first++;
683 if (zipperEver) {
684 URI_FUNC(StopSyntax)(state, first, memory);
685 return NULL; /* "::.+::" */
686 }
687
688 /* Zero everything after zipper */
689 memset(state->uri->hostData.ip6->data + resetOffset, 0,
690 16 - resetOffset);
691 setZipper = 1;
692
693 /* ":::+"? */
694 if (afterLast - first < 2) {
695 URI_FUNC(StopSyntax)(state, afterLast, memory);
696 return NULL; /* No ']' yet */
697 }
698 if (first[1] == _UT(':')) {
699 URI_FUNC(StopSyntax)(state, first + 1, memory);
700 return NULL; /* ":::+ "*/
701 }
702 } else if (quadsDone == 0 || first[1] == _UT(']')) {
703 /* Single leading or trailing ":" */
704 URI_FUNC(StopSyntax)(state, first, memory);
705 return NULL;
706 }
707
708 if (setZipper) {
709 zipperEver = 1;
710 }
711 } break;
712
713 case _UT('.'):
714 if ((quadsDone + zipperEver > 6) /* NOTE */
715 || (!zipperEver && (quadsDone < 6)) || letterAmong
716 || (digitCount == 0) || (digitCount == 4)) {
717 /* Invalid octet before */
718 URI_FUNC(StopSyntax)(state, first, memory);
719 return NULL;
720 } else if ((digitCount > 1) && (digitHistory[0] == 0)) {
721 /* Leading zero */
722 URI_FUNC(StopSyntax)(state, first - digitCount, memory);
723 return NULL;
724 } else if ((digitCount == 3)
725 && (100 * digitHistory[0] + 10 * digitHistory[1]
726 + digitHistory[2]
727 > 255)) {
728 /* Octet value too large */
729 if (digitHistory[0] > 2) {
730 URI_FUNC(StopSyntax)(state, first - 3, memory);
731 } else if (digitHistory[1] > 5) {
732 URI_FUNC(StopSyntax)(state, first - 2, memory);
733 } else {
734 URI_FUNC(StopSyntax)(state, first - 1, memory);
735 }
736 return NULL;
737 }
738
739 /* Copy first IPv4 octet */
740 state->uri->hostData.ip6->data[16 - 4] =
741 uriGetOctetValue(digitHistory, digitCount);
742 digitCount = 0;
743
744 /* Switch over to IPv4 loop */
745 ip4OctetsDone = 1;
746 walking = 0;
747 break;
748
749 case _UT(']'):
750 /* Too little quads? */
751 if (!zipperEver && !((quadsDone == 7) && (digitCount > 0))) {
752 URI_FUNC(StopSyntax)(state, first, memory);
753 return NULL;
754 }
755
756 if (digitCount > 0) {
757 if (zipperEver) {
758 /* Too many quads? */
759 if (quadsDone >= 7) {
760 URI_FUNC(StopSyntax)(state, first, memory);
761 return NULL;
762 }
763 uriWriteQuadToDoubleByte(digitHistory, digitCount,
764 quadsAfterZipper
765 + 2 * quadsAfterZipperCount);
766 quadsAfterZipperCount++;
767 } else {
768 uriWriteQuadToDoubleByte(digitHistory, digitCount,
769 state->uri->hostData.ip6->data
770 + 2 * quadsDone);
771 }
772 /*
773 quadsDone++;
774 digitCount = 0;
775 */
776 }
777
778 /* Copy missing quads to the end */
779 memcpy(state->uri->hostData.ip6->data + 16
780 - 2 * quadsAfterZipperCount,
781 quadsAfterZipper, 2 * quadsAfterZipperCount);
782
783 state->uri->hostText.afterLast = first; /* HOST END */
784 return first + 1; /* Fine */
785
786 default:
787 URI_FUNC(StopSyntax)(state, first, memory);
788 return NULL;
789 }
790 first++;
791
792 if (first >= afterLast) {
793 URI_FUNC(StopSyntax)(state, afterLast, memory);
794 return NULL; /* No ']' yet */
795 }
796 } while (walking);
797 }
798 }
799 }
800
801 /*
802 * [mustBeSegmentNzNc]->[pctEncoded][mustBeSegmentNzNc]
803 * [mustBeSegmentNzNc]->[subDelims][mustBeSegmentNzNc]
804 * [mustBeSegmentNzNc]->[unreserved][mustBeSegmentNzNc]
805 * [mustBeSegmentNzNc]->[uriTail] // can take <NULL>
806 * [mustBeSegmentNzNc]-></>[segment][zeroMoreSlashSegs][uriTail]
807 * [mustBeSegmentNzNc]-><@>[mustBeSegmentNzNc]
808 */
809 static const URI_CHAR * URI_FUNC(ParseMustBeSegmentNzNc)(URI_TYPE(ParserState) * state,
810 const URI_CHAR * first,
811 const URI_CHAR * afterLast,
812 UriMemoryManager * memory) {
813 tail_call:
814 if (first >= afterLast) {
815 if (!URI_FUNC(PushPathSegment)(state, state->uri->scheme.first, first,
816 memory)) { /* SEGMENT BOTH */
817 URI_FUNC(StopMalloc)(state, memory);
818 return NULL;
819 }
820 state->uri->scheme.first = NULL; /* Not a scheme, reset */
821 return afterLast;
822 }
823
824 switch (*first) {
825 case _UT('%'): {
826 const URI_CHAR * const afterPctEncoded =
827 URI_FUNC(ParsePctEncoded)(state, first, afterLast, memory);
828 if (afterPctEncoded == NULL) {
829 return NULL;
830 }
831 first = afterPctEncoded;
832 goto tail_call;
833 }
834
835 case _UT('@'):
836 case URI_SET_SUB_DELIMS(_UT):
837 case URI_SET_UNRESERVED(_UT):
838 first += 1;
839 goto tail_call;
840
841 case _UT('/'): {
842 const URI_CHAR * afterZeroMoreSlashSegs;
843 const URI_CHAR * afterSegment;
844 if (!URI_FUNC(PushPathSegment)(state, state->uri->scheme.first, first,
845 memory)) { /* SEGMENT BOTH */
846 URI_FUNC(StopMalloc)(state, memory);
847 return NULL;
848 }
849 state->uri->scheme.first = NULL; /* Not a scheme, reset */
850 afterSegment = URI_FUNC(ParseSegment)(state, first + 1, afterLast, memory);
851 if (afterSegment == NULL) {
852 return NULL;
853 }
854 if (!URI_FUNC(PushPathSegment)(state, first + 1, afterSegment,
855 memory)) { /* SEGMENT BOTH */
856 URI_FUNC(StopMalloc)(state, memory);
857 return NULL;
858 }
859 afterZeroMoreSlashSegs =
860 URI_FUNC(ParseZeroMoreSlashSegs)(state, afterSegment, afterLast, memory);
861 if (afterZeroMoreSlashSegs == NULL) {
862 return NULL;
863 }
864 return URI_FUNC(ParseUriTail)(state, afterZeroMoreSlashSegs, afterLast, memory);
865 }
866
867 default:
868 if (!URI_FUNC(PushPathSegment)(state, state->uri->scheme.first, first,
869 memory)) { /* SEGMENT BOTH */
870 URI_FUNC(StopMalloc)(state, memory);
871 return NULL;
872 }
873 state->uri->scheme.first = NULL; /* Not a scheme, reset */
874 return URI_FUNC(ParseUriTail)(state, first, afterLast, memory);
875 }
876 }
877
878 /*
879 * [ownHost]-><[>[ipLit2][authorityTwo]
880 * [ownHost]->[ownHost2] // can take <NULL>
881 */
882 static URI_INLINE const URI_CHAR * URI_FUNC(ParseOwnHost)(URI_TYPE(ParserState) * state,
883 const URI_CHAR * first,
884 const URI_CHAR * afterLast,
885 UriMemoryManager * memory) {
886 if (first >= afterLast) {
887 state->uri->hostText.afterLast = afterLast; /* HOST END */
888 return afterLast;
889 }
890
891 switch (*first) {
892 case _UT('['): {
893 const URI_CHAR * const afterIpLit2 =
894 URI_FUNC(ParseIpLit2)(state, first + 1, afterLast, memory);
895 if (afterIpLit2 == NULL) {
896 return NULL;
897 }
898 state->uri->hostText.first = first + 1; /* HOST BEGIN */
899 return URI_FUNC(ParseAuthorityTwo)(state, afterIpLit2, afterLast);
900 }
901
902 default:
903 return URI_FUNC(ParseOwnHost2)(state, first, afterLast, memory);
904 }
905 }
906
907 static URI_INLINE UriBool URI_FUNC(OnExitOwnHost2)(URI_TYPE(ParserState) * state,
908 const URI_CHAR * first,
909 UriMemoryManager * memory) {
910 state->uri->hostText.afterLast = first; /* HOST END */
911
912 /* Valid IPv4 or just a regname? */
913 state->uri->hostData.ip4 = memory->malloc(
914 memory, 1 * sizeof(UriIp4)); /* Freed when stopping on parse error */
915 if (state->uri->hostData.ip4 == NULL) {
916 return URI_FALSE; /* Raises malloc error */
917 }
918 if (URI_FUNC(ParseIpFourAddress)(state->uri->hostData.ip4->data,
919 state->uri->hostText.first,
920 state->uri->hostText.afterLast)) {
921 /* Not IPv4 */
922 memory->free(memory, state->uri->hostData.ip4);
923 state->uri->hostData.ip4 = NULL;
924 }
925 return URI_TRUE; /* Success */
926 }
927
928 /*
929 * [ownHost2]->[authorityTwo] // can take <NULL>
930 * [ownHost2]->[pctSubUnres][ownHost2]
931 */
932 static const URI_CHAR * URI_FUNC(ParseOwnHost2)(URI_TYPE(ParserState) * state,
933 const URI_CHAR * first,
934 const URI_CHAR * afterLast,
935 UriMemoryManager * memory) {
936 tail_call:
937 if (first >= afterLast) {
938 if (!URI_FUNC(OnExitOwnHost2)(state, first, memory)) {
939 URI_FUNC(StopMalloc)(state, memory);
940 return NULL;
941 }
942 return afterLast;
943 }
944
945 switch (*first) {
946 case _UT('%'):
947 case URI_SET_SUB_DELIMS(_UT):
948 case URI_SET_UNRESERVED(_UT): {
949 const URI_CHAR * const afterPctSubUnres =
950 URI_FUNC(ParsePctSubUnres)(state, first, afterLast, memory);
951 if (afterPctSubUnres == NULL) {
952 return NULL;
953 }
954 first = afterPctSubUnres;
955 goto tail_call;
956 }
957
958 default:
959 if (!URI_FUNC(OnExitOwnHost2)(state, first, memory)) {
960 URI_FUNC(StopMalloc)(state, memory);
961 return NULL;
962 }
963 return URI_FUNC(ParseAuthorityTwo)(state, first, afterLast);
964 }
965 }
966
967 static URI_INLINE UriBool URI_FUNC(OnExitOwnHostUserInfo)(URI_TYPE(ParserState) * state,
968 const URI_CHAR * first,
969 UriMemoryManager * memory) {
970 state->uri->hostText.first =
971 state->uri->userInfo.first; /* Host instead of userInfo, update */
972 state->uri->userInfo.first = NULL; /* Not a userInfo, reset */
973 state->uri->hostText.afterLast = first; /* HOST END */
974
975 /* Valid IPv4 or just a regname? */
976 state->uri->hostData.ip4 = memory->malloc(
977 memory, 1 * sizeof(UriIp4)); /* Freed when stopping on parse error */
978 if (state->uri->hostData.ip4 == NULL) {
979 return URI_FALSE; /* Raises malloc error */
980 }
981 if (URI_FUNC(ParseIpFourAddress)(state->uri->hostData.ip4->data,
982 state->uri->hostText.first,
983 state->uri->hostText.afterLast)) {
984 /* Not IPv4 */
985 memory->free(memory, state->uri->hostData.ip4);
986 state->uri->hostData.ip4 = NULL;
987 }
988 return URI_TRUE; /* Success */
989 }
990
991 /*
992 * [ownHostUserInfoNz]->[pctSubUnres][ownHostUserInfo]
993 * [ownHostUserInfoNz]-><:>[ownPortUserInfo]
994 * [ownHostUserInfoNz]-><@>[ownHost]
995 *
996 * [ownHostUserInfo]->[ownHostUserInfoNz]
997 * [ownHostUserInfo]-><NULL>
998 */
999 static const URI_CHAR * URI_FUNC(ParseOwnHostUserInfoNz)(URI_TYPE(ParserState) * state,
1000 const URI_CHAR * first,
1001 const URI_CHAR * afterLast,
1002 UriMemoryManager * memory) {
1003 const URI_CHAR * const originalFirst = first;
1004
1005 while (first < afterLast) {
1006 switch (*first) {
1007 case _UT('%'):
1008 case URI_SET_SUB_DELIMS(_UT):
1009 case URI_SET_UNRESERVED(_UT): {
1010 const URI_CHAR * const afterPctSubUnres =
1011 URI_FUNC(ParsePctSubUnres)(state, first, afterLast, memory);
1012 if (afterPctSubUnres == NULL) {
1013 return NULL;
1014 }
1015 first = afterPctSubUnres;
1016 break;
1017 }
1018
1019 default:
1020 goto done_looping;
1021 break;
1022 }
1023 }
1024
1025 done_looping:
1026 if (first < afterLast) {
1027 switch (*first) {
1028 case _UT(':'):
1029 state->uri->hostText.afterLast = first; /* HOST END */
1030 state->uri->portText.first = first + 1; /* PORT BEGIN */
1031 return URI_FUNC(ParseOwnPortUserInfo)(state, first + 1, afterLast, memory);
1032
1033 case _UT('@'):
1034 state->uri->userInfo.afterLast = first; /* USERINFO END */
1035 state->uri->hostText.first = first + 1; /* HOST BEGIN */
1036 return URI_FUNC(ParseOwnHost)(state, first + 1, afterLast, memory);
1037
1038 default:
1039 break;
1040 }
1041 }
1042
1043 if (first == originalFirst) {
1044 URI_FUNC(StopSyntax)(state, afterLast, memory);
1045 return NULL;
1046 }
1047
1048 if (!URI_FUNC(OnExitOwnHostUserInfo)(state, first, memory)) {
1049 URI_FUNC(StopMalloc)(state, memory);
1050 return NULL;
1051 }
1052
1053 return first;
1054 }
1055
1056 static URI_INLINE UriBool URI_FUNC(OnExitOwnPortUserInfo)(URI_TYPE(ParserState) * state,
1057 const URI_CHAR * first,
1058 UriMemoryManager * memory) {
1059 state->uri->hostText.first =
1060 state->uri->userInfo.first; /* Host instead of userInfo, update */
1061 state->uri->userInfo.first = NULL; /* Not a userInfo, reset */
1062 state->uri->portText.afterLast = first; /* PORT END */
1063
1064 /* Valid IPv4 or just a regname? */
1065 state->uri->hostData.ip4 = memory->malloc(
1066 memory, 1 * sizeof(UriIp4)); /* Freed when stopping on parse error */
1067 if (state->uri->hostData.ip4 == NULL) {
1068 return URI_FALSE; /* Raises malloc error */
1069 }
1070 if (URI_FUNC(ParseIpFourAddress)(state->uri->hostData.ip4->data,
1071 state->uri->hostText.first,
1072 state->uri->hostText.afterLast)) {
1073 /* Not IPv4 */
1074 memory->free(memory, state->uri->hostData.ip4);
1075 state->uri->hostData.ip4 = NULL;
1076 }
1077 return URI_TRUE; /* Success */
1078 }
1079
1080 /*
1081 * [ownPortUserInfo]->[ALPHA][ownUserInfo]
1082 * [ownPortUserInfo]->[DIGIT][ownPortUserInfo]
1083 * [ownPortUserInfo]-><.>[ownUserInfo]
1084 * [ownPortUserInfo]-><_>[ownUserInfo]
1085 * [ownPortUserInfo]-><~>[ownUserInfo]
1086 * [ownPortUserInfo]-><->[ownUserInfo]
1087 * [ownPortUserInfo]->[subDelims][ownUserInfo]
1088 * [ownPortUserInfo]->[pctEncoded][ownUserInfo]
1089 * [ownPortUserInfo]-><:>[ownUserInfo]
1090 * [ownPortUserInfo]-><@>[ownHost]
1091 * [ownPortUserInfo]-><NULL>
1092 */
1093 static const URI_CHAR * URI_FUNC(ParseOwnPortUserInfo)(URI_TYPE(ParserState) * state,
1094 const URI_CHAR * first,
1095 const URI_CHAR * afterLast,
1096 UriMemoryManager * memory) {
1097 tail_call:
1098 if (first >= afterLast) {
1099 if (!URI_FUNC(OnExitOwnPortUserInfo)(state, first, memory)) {
1100 URI_FUNC(StopMalloc)(state, memory);
1101 return NULL;
1102 }
1103 return afterLast;
1104 }
1105
1106 switch (*first) {
1107 case URI_SET_SUB_DELIMS(_UT):
1108 /* begin unreserved (except alpha and digit) */
1109 case _UT('-'):
1110 case _UT('.'):
1111 case _UT('_'):
1112 case _UT('~'):
1113 /* end unreserved (except alpha and digit) */
1114 case _UT(':'):
1115 case URI_SET_ALPHA(_UT):
1116 state->uri->hostText.afterLast = NULL; /* Not a host, reset */
1117 state->uri->portText.first = NULL; /* Not a port, reset */
1118 return URI_FUNC(ParseOwnUserInfo)(state, first + 1, afterLast, memory);
1119
1120 case URI_SET_DIGIT(_UT):
1121 first += 1;
1122 goto tail_call;
1123
1124 case _UT('%'):
1125 state->uri->portText.first = NULL; /* Not a port, reset */
1126 const URI_CHAR * const afterPct =
1127 URI_FUNC(ParsePctEncoded)(state, first, afterLast, memory);
1128 if (afterPct == NULL) {
1129 return NULL;
1130 }
1131 return URI_FUNC(ParseOwnUserInfo)(state, afterPct, afterLast, memory);
1132
1133 case _UT('@'):
1134 state->uri->hostText.afterLast = NULL; /* Not a host, reset */
1135 state->uri->portText.first = NULL; /* Not a port, reset */
1136 state->uri->userInfo.afterLast = first; /* USERINFO END */
1137 state->uri->hostText.first = first + 1; /* HOST BEGIN */
1138 return URI_FUNC(ParseOwnHost)(state, first + 1, afterLast, memory);
1139
1140 default:
1141 if (!URI_FUNC(OnExitOwnPortUserInfo)(state, first, memory)) {
1142 URI_FUNC(StopMalloc)(state, memory);
1143 return NULL;
1144 }
1145 return first;
1146 }
1147 }
1148
1149 /*
1150 * [ownUserInfo]->[pctSubUnres][ownUserInfo]
1151 * [ownUserInfo]-><:>[ownUserInfo]
1152 * [ownUserInfo]-><@>[ownHost]
1153 */
1154 static const URI_CHAR * URI_FUNC(ParseOwnUserInfo)(URI_TYPE(ParserState) * state,
1155 const URI_CHAR * first,
1156 const URI_CHAR * afterLast,
1157 UriMemoryManager * memory) {
1158 tail_call:
1159 if (first >= afterLast) {
1160 URI_FUNC(StopSyntax)(state, afterLast, memory);
1161 return NULL;
1162 }
1163
1164 switch (*first) {
1165 case _UT('%'):
1166 case URI_SET_SUB_DELIMS(_UT):
1167 case URI_SET_UNRESERVED(_UT): {
1168 const URI_CHAR * const afterPctSubUnres =
1169 URI_FUNC(ParsePctSubUnres)(state, first, afterLast, memory);
1170 if (afterPctSubUnres == NULL) {
1171 return NULL;
1172 }
1173 first = afterPctSubUnres;
1174 goto tail_call;
1175 }
1176
1177 case _UT(':'):
1178 first += 1;
1179 goto tail_call;
1180
1181 case _UT('@'):
1182 /* SURE */
1183 state->uri->userInfo.afterLast = first; /* USERINFO END */
1184 state->uri->hostText.first = first + 1; /* HOST BEGIN */
1185 return URI_FUNC(ParseOwnHost)(state, first + 1, afterLast, memory);
1186
1187 default:
1188 URI_FUNC(StopSyntax)(state, first, memory);
1189 return NULL;
1190 }
1191 }
1192
1193 static URI_INLINE void URI_FUNC(OnExitPartHelperTwo)(URI_TYPE(ParserState) * state) {
1194 state->uri->absolutePath = URI_TRUE;
1195 }
1196
1197 /*
1198 * [partHelperTwo]->[pathAbsNoLeadSlash] // can take <NULL>
1199 * [partHelperTwo]-></>[authority][pathAbsEmpty]
1200 */
1201 static URI_INLINE const URI_CHAR *
1202 URI_FUNC(ParsePartHelperTwo)(URI_TYPE(ParserState) * state, const URI_CHAR * first,
1203 const URI_CHAR * afterLast, UriMemoryManager * memory) {
1204 if (first >= afterLast) {
1205 URI_FUNC(OnExitPartHelperTwo)(state);
1206 return afterLast;
1207 }
1208
1209 switch (*first) {
1210 case _UT('/'): {
1211 const URI_CHAR * const afterAuthority =
1212 URI_FUNC(ParseAuthority)(state, first + 1, afterLast, memory);
1213 const URI_CHAR * afterPathAbsEmpty;
1214 if (afterAuthority == NULL) {
1215 return NULL;
1216 }
1217 afterPathAbsEmpty =
1218 URI_FUNC(ParsePathAbsEmpty)(state, afterAuthority, afterLast, memory);
1219
1220 URI_FUNC(FixEmptyTrailSegment)(state->uri, memory);
1221
1222 return afterPathAbsEmpty;
1223 }
1224
1225 default:
1226 URI_FUNC(OnExitPartHelperTwo)(state);
1227 return URI_FUNC(ParsePathAbsNoLeadSlash)(state, first, afterLast, memory);
1228 }
1229 }
1230
1231 /*
1232 * [pathAbsEmpty]-></>[segment][pathAbsEmpty]
1233 * [pathAbsEmpty]-><NULL>
1234 */
1235 static const URI_CHAR * URI_FUNC(ParsePathAbsEmpty)(URI_TYPE(ParserState) * state,
1236 const URI_CHAR * first,
1237 const URI_CHAR * afterLast,
1238 UriMemoryManager * memory) {
1239 tail_call:
1240 if (first >= afterLast) {
1241 return afterLast;
1242 }
1243
1244 switch (*first) {
1245 case _UT('/'): {
1246 const URI_CHAR * const afterSegment =
1247 URI_FUNC(ParseSegment)(state, first + 1, afterLast, memory);
1248 if (afterSegment == NULL) {
1249 return NULL;
1250 }
1251 if (!URI_FUNC(PushPathSegment)(state, first + 1, afterSegment,
1252 memory)) { /* SEGMENT BOTH */
1253 URI_FUNC(StopMalloc)(state, memory);
1254 return NULL;
1255 }
1256 first = afterSegment;
1257 goto tail_call;
1258 }
1259
1260 default:
1261 return first;
1262 }
1263 }
1264
1265 /*
1266 * [pathAbsNoLeadSlash]->[segmentNz][zeroMoreSlashSegs]
1267 * [pathAbsNoLeadSlash]-><NULL>
1268 */
1269 static URI_INLINE const URI_CHAR *
1270 URI_FUNC(ParsePathAbsNoLeadSlash)(URI_TYPE(ParserState) * state, const URI_CHAR * first,
1271 const URI_CHAR * afterLast, UriMemoryManager * memory) {
1272 if (first >= afterLast) {
1273 return afterLast;
1274 }
1275
1276 switch (*first) {
1277 case URI_SET_PCHAR(_UT): {
1278 const URI_CHAR * const afterSegmentNz =
1279 URI_FUNC(ParseSegmentNz)(state, first, afterLast, memory);
1280 if (afterSegmentNz == NULL) {
1281 return NULL;
1282 }
1283 if (!URI_FUNC(PushPathSegment)(state, first, afterSegmentNz,
1284 memory)) { /* SEGMENT BOTH */
1285 URI_FUNC(StopMalloc)(state, memory);
1286 return NULL;
1287 }
1288 return URI_FUNC(ParseZeroMoreSlashSegs)(state, afterSegmentNz, afterLast, memory);
1289 }
1290
1291 default:
1292 return first;
1293 }
1294 }
1295
1296 /*
1297 * [pathRootless]->[segmentNz][zeroMoreSlashSegs]
1298 */
1299 static URI_INLINE const URI_CHAR *
1300 URI_FUNC(ParsePathRootless)(URI_TYPE(ParserState) * state, const URI_CHAR * first,
1301 const URI_CHAR * afterLast, UriMemoryManager * memory) {
1302 const URI_CHAR * const afterSegmentNz =
1303 URI_FUNC(ParseSegmentNz)(state, first, afterLast, memory);
1304 if (afterSegmentNz == NULL) {
1305 return NULL;
1306 } else {
1307 if (!URI_FUNC(PushPathSegment)(state, first, afterSegmentNz,
1308 memory)) { /* SEGMENT BOTH */
1309 URI_FUNC(StopMalloc)(state, memory);
1310 return NULL;
1311 }
1312 }
1313 return URI_FUNC(ParseZeroMoreSlashSegs)(state, afterSegmentNz, afterLast, memory);
1314 }
1315
1316 /*
1317 * [pchar]->[pctEncoded]
1318 * [pchar]->[subDelims]
1319 * [pchar]->[unreserved]
1320 * [pchar]-><:>
1321 * [pchar]-><@>
1322 */
1323 static const URI_CHAR * URI_FUNC(ParsePchar)(URI_TYPE(ParserState) * state,
1324 const URI_CHAR * first,
1325 const URI_CHAR * afterLast,
1326 UriMemoryManager * memory) {
1327 if (first >= afterLast) {
1328 URI_FUNC(StopSyntax)(state, afterLast, memory);
1329 return NULL;
1330 }
1331
1332 switch (*first) {
1333 case _UT('%'):
1334 return URI_FUNC(ParsePctEncoded)(state, first, afterLast, memory);
1335
1336 case URI_SET_PCHAR_WITHOUT_PERCENT(_UT):
1337 return first + 1;
1338
1339 default:
1340 URI_FUNC(StopSyntax)(state, first, memory);
1341 return NULL;
1342 }
1343 }
1344
1345 /*
1346 * [pctEncoded]-><%>[HEXDIG][HEXDIG]
1347 */
1348 static const URI_CHAR * URI_FUNC(ParsePctEncoded)(URI_TYPE(ParserState) * state,
1349 const URI_CHAR * first,
1350 const URI_CHAR * afterLast,
1351 UriMemoryManager * memory) {
1352 if (first >= afterLast) {
1353 URI_FUNC(StopSyntax)(state, afterLast, memory);
1354 return NULL;
1355 }
1356
1357 /*
1358 First character has already been
1359 checked before entering this rule.
1360
1361 switch (*first) {
1362 case _UT('%'):
1363 */
1364 if (afterLast - first < 2) {
1365 URI_FUNC(StopSyntax)(state, afterLast, memory);
1366 return NULL;
1367 }
1368
1369 switch (first[1]) {
1370 case URI_SET_HEXDIG(_UT):
1371 if (afterLast - first < 3) {
1372 URI_FUNC(StopSyntax)(state, afterLast, memory);
1373 return NULL;
1374 }
1375
1376 switch (first[2]) {
1377 case URI_SET_HEXDIG(_UT):
1378 return first + 3;
1379
1380 default:
1381 URI_FUNC(StopSyntax)(state, first + 2, memory);
1382 return NULL;
1383 }
1384
1385 default:
1386 URI_FUNC(StopSyntax)(state, first + 1, memory);
1387 return NULL;
1388 }
1389
1390 /*
1391 default:
1392 URI_FUNC(StopSyntax)(state, first, memory);
1393 return NULL;
1394 }
1395 */
1396 }
1397
1398 /*
1399 * [pctSubUnres]->[pctEncoded]
1400 * [pctSubUnres]->[subDelims]
1401 * [pctSubUnres]->[unreserved]
1402 */
1403 static const URI_CHAR * URI_FUNC(ParsePctSubUnres)(URI_TYPE(ParserState) * state,
1404 const URI_CHAR * first,
1405 const URI_CHAR * afterLast,
1406 UriMemoryManager * memory) {
1407 if (first >= afterLast) {
1408 URI_FUNC(StopSyntax)(state, afterLast, memory);
1409 return NULL;
1410 }
1411
1412 switch (*first) {
1413 case _UT('%'):
1414 return URI_FUNC(ParsePctEncoded)(state, first, afterLast, memory);
1415
1416 case URI_SET_SUB_DELIMS(_UT):
1417 case URI_SET_UNRESERVED(_UT):
1418 return first + 1;
1419
1420 default:
1421 URI_FUNC(StopSyntax)(state, first, memory);
1422 return NULL;
1423 }
1424 }
1425
1426 /*
1427 * [port]->[DIGIT][port]
1428 * [port]-><NULL>
1429 */
1430 static const URI_CHAR * URI_FUNC(ParsePort)(const URI_CHAR * first,
1431 const URI_CHAR * afterLast) {
1432 tail_call:
1433 if (first >= afterLast) {
1434 return afterLast;
1435 }
1436
1437 switch (*first) {
1438 case URI_SET_DIGIT(_UT):
1439 first += 1;
1440 goto tail_call;
1441
1442 default:
1443 return first;
1444 }
1445 }
1446
1447 /*
1448 * [queryFrag]->[pchar][queryFrag]
1449 * [queryFrag]-></>[queryFrag]
1450 * [queryFrag]-><?>[queryFrag]
1451 * [queryFrag]-><NULL>
1452 */
1453 static const URI_CHAR * URI_FUNC(ParseQueryFrag)(URI_TYPE(ParserState) * state,
1454 const URI_CHAR * first,
1455 const URI_CHAR * afterLast,
1456 UriMemoryManager * memory) {
1457 tail_call:
1458 if (first >= afterLast) {
1459 return afterLast;
1460 }
1461
1462 switch (*first) {
1463 case URI_SET_PCHAR(_UT): {
1464 const URI_CHAR * const afterPchar =
1465 URI_FUNC(ParsePchar)(state, first, afterLast, memory);
1466 if (afterPchar == NULL) {
1467 return NULL;
1468 }
1469 first = afterPchar;
1470 goto tail_call;
1471 }
1472
1473 case _UT('/'):
1474 case _UT('?'):
1475 first += 1;
1476 goto tail_call;
1477
1478 default:
1479 return first;
1480 }
1481 }
1482
1483 /*
1484 * [segment]->[pchar][segment]
1485 * [segment]-><NULL>
1486 */
1487 static const URI_CHAR * URI_FUNC(ParseSegment)(URI_TYPE(ParserState) * state,
1488 const URI_CHAR * first,
1489 const URI_CHAR * afterLast,
1490 UriMemoryManager * memory) {
1491 tail_call:
1492 if (first >= afterLast) {
1493 return afterLast;
1494 }
1495
1496 switch (*first) {
1497 case URI_SET_PCHAR(_UT): {
1498 const URI_CHAR * const afterPchar =
1499 URI_FUNC(ParsePchar)(state, first, afterLast, memory);
1500 if (afterPchar == NULL) {
1501 return NULL;
1502 }
1503 first = afterPchar;
1504 goto tail_call;
1505 }
1506
1507 default:
1508 return first;
1509 }
1510 }
1511
1512 /*
1513 * [segmentNz]->[pchar][segment]
1514 */
1515 static URI_INLINE const URI_CHAR * URI_FUNC(ParseSegmentNz)(URI_TYPE(ParserState) * state,
1516 const URI_CHAR * first,
1517 const URI_CHAR * afterLast,
1518 UriMemoryManager * memory) {
1519 const URI_CHAR * const afterPchar =
1520 URI_FUNC(ParsePchar)(state, first, afterLast, memory);
1521 if (afterPchar == NULL) {
1522 return NULL;
1523 }
1524 return URI_FUNC(ParseSegment)(state, afterPchar, afterLast, memory);
1525 }
1526
1527 static URI_INLINE UriBool URI_FUNC(OnExitSegmentNzNcOrScheme2)(
1528 URI_TYPE(ParserState) * state, const URI_CHAR * first, UriMemoryManager * memory) {
1529 if (!URI_FUNC(PushPathSegment)(state, state->uri->scheme.first, first,
1530 memory)) { /* SEGMENT BOTH */
1531 return URI_FALSE; /* Raises malloc error*/
1532 }
1533 state->uri->scheme.first = NULL; /* Not a scheme, reset */
1534 return URI_TRUE; /* Success */
1535 }
1536
1537 /*
1538 * [segmentNzNcOrScheme2]->[ALPHA][segmentNzNcOrScheme2]
1539 * [segmentNzNcOrScheme2]->[DIGIT][segmentNzNcOrScheme2]
1540 * [segmentNzNcOrScheme2]->[pctEncoded][mustBeSegmentNzNc]
1541 * [segmentNzNcOrScheme2]->[uriTail] // can take <NULL>
1542 * [segmentNzNcOrScheme2]-><!>[mustBeSegmentNzNc]
1543 * [segmentNzNcOrScheme2]-><$>[mustBeSegmentNzNc]
1544 * [segmentNzNcOrScheme2]-><&>[mustBeSegmentNzNc]
1545 * [segmentNzNcOrScheme2]-><(>[mustBeSegmentNzNc]
1546 * [segmentNzNcOrScheme2]-><)>[mustBeSegmentNzNc]
1547 * [segmentNzNcOrScheme2]-><*>[mustBeSegmentNzNc]
1548 * [segmentNzNcOrScheme2]-><,>[mustBeSegmentNzNc]
1549 * [segmentNzNcOrScheme2]-><.>[segmentNzNcOrScheme2]
1550 * [segmentNzNcOrScheme2]-></>[segment][zeroMoreSlashSegs][uriTail]
1551 * [segmentNzNcOrScheme2]-><:>[hierPart][uriTail]
1552 * [segmentNzNcOrScheme2]-><;>[mustBeSegmentNzNc]
1553 * [segmentNzNcOrScheme2]-><@>[mustBeSegmentNzNc]
1554 * [segmentNzNcOrScheme2]-><_>[mustBeSegmentNzNc]
1555 * [segmentNzNcOrScheme2]-><~>[mustBeSegmentNzNc]
1556 * [segmentNzNcOrScheme2]-><+>[segmentNzNcOrScheme2]
1557 * [segmentNzNcOrScheme2]-><=>[mustBeSegmentNzNc]
1558 * [segmentNzNcOrScheme2]-><'>[mustBeSegmentNzNc]
1559 * [segmentNzNcOrScheme2]-><->[segmentNzNcOrScheme2]
1560 */
1561 static const URI_CHAR * URI_FUNC(ParseSegmentNzNcOrScheme2)(URI_TYPE(ParserState) * state,
1562 const URI_CHAR * first,
1563 const URI_CHAR * afterLast,
1564 UriMemoryManager * memory) {
1565 tail_call:
1566 if (first >= afterLast) {
1567 if (!URI_FUNC(OnExitSegmentNzNcOrScheme2)(state, first, memory)) {
1568 URI_FUNC(StopMalloc)(state, memory);
1569 return NULL;
1570 }
1571 return afterLast;
1572 }
1573
1574 switch (*first) {
1575 case _UT('.'):
1576 case _UT('+'):
1577 case _UT('-'):
1578 case URI_SET_ALPHA(_UT):
1579 case URI_SET_DIGIT(_UT):
1580 first += 1;
1581 goto tail_call;
1582
1583 case _UT('%'): {
1584 const URI_CHAR * const afterPctEncoded =
1585 URI_FUNC(ParsePctEncoded)(state, first, afterLast, memory);
1586 if (afterPctEncoded == NULL) {
1587 return NULL;
1588 }
1589 return URI_FUNC(ParseMustBeSegmentNzNc)(state, afterPctEncoded, afterLast,
1590 memory);
1591 }
1592
1593 case _UT('!'):
1594 case _UT('$'):
1595 case _UT('&'):
1596 case _UT('('):
1597 case _UT(')'):
1598 case _UT('*'):
1599 case _UT(','):
1600 case _UT(';'):
1601 case _UT('@'):
1602 case _UT('_'):
1603 case _UT('~'):
1604 case _UT('='):
1605 case _UT('\''):
1606 return URI_FUNC(ParseMustBeSegmentNzNc)(state, first + 1, afterLast, memory);
1607
1608 case _UT('/'): {
1609 const URI_CHAR * afterZeroMoreSlashSegs;
1610 const URI_CHAR * const afterSegment =
1611 URI_FUNC(ParseSegment)(state, first + 1, afterLast, memory);
1612 if (afterSegment == NULL) {
1613 return NULL;
1614 }
1615 if (!URI_FUNC(PushPathSegment)(state, state->uri->scheme.first, first,
1616 memory)) { /* SEGMENT BOTH */
1617 URI_FUNC(StopMalloc)(state, memory);
1618 return NULL;
1619 }
1620 state->uri->scheme.first = NULL; /* Not a scheme, reset */
1621 if (!URI_FUNC(PushPathSegment)(state, first + 1, afterSegment,
1622 memory)) { /* SEGMENT BOTH */
1623 URI_FUNC(StopMalloc)(state, memory);
1624 return NULL;
1625 }
1626 afterZeroMoreSlashSegs =
1627 URI_FUNC(ParseZeroMoreSlashSegs)(state, afterSegment, afterLast, memory);
1628 if (afterZeroMoreSlashSegs == NULL) {
1629 return NULL;
1630 }
1631 return URI_FUNC(ParseUriTail)(state, afterZeroMoreSlashSegs, afterLast, memory);
1632 }
1633
1634 case _UT(':'): {
1635 const URI_CHAR * const afterHierPart =
1636 URI_FUNC(ParseHierPart)(state, first + 1, afterLast, memory);
1637 state->uri->scheme.afterLast = first; /* SCHEME END */
1638 if (afterHierPart == NULL) {
1639 return NULL;
1640 }
1641 return URI_FUNC(ParseUriTail)(state, afterHierPart, afterLast, memory);
1642 }
1643
1644 default:
1645 if (!URI_FUNC(OnExitSegmentNzNcOrScheme2)(state, first, memory)) {
1646 URI_FUNC(StopMalloc)(state, memory);
1647 return NULL;
1648 }
1649 return URI_FUNC(ParseUriTail)(state, first, afterLast, memory);
1650 }
1651 }
1652
1653 /*
1654 * [uriReference]->[ALPHA][segmentNzNcOrScheme2]
1655 * [uriReference]->[DIGIT][mustBeSegmentNzNc]
1656 * [uriReference]->[pctEncoded][mustBeSegmentNzNc]
1657 * [uriReference]->[subDelims][mustBeSegmentNzNc]
1658 * [uriReference]->[uriTail] // can take <NULL>
1659 * [uriReference]-><.>[mustBeSegmentNzNc]
1660 * [uriReference]-></>[partHelperTwo][uriTail]
1661 * [uriReference]-><@>[mustBeSegmentNzNc]
1662 * [uriReference]-><_>[mustBeSegmentNzNc]
1663 * [uriReference]-><~>[mustBeSegmentNzNc]
1664 * [uriReference]-><->[mustBeSegmentNzNc]
1665 */
1666 static const URI_CHAR * URI_FUNC(ParseUriReference)(URI_TYPE(ParserState) * state,
1667 const URI_CHAR * first,
1668 const URI_CHAR * afterLast,
1669 UriMemoryManager * memory) {
1670 if (first >= afterLast) {
1671 return afterLast;
1672 }
1673
1674 switch (*first) {
1675 case URI_SET_ALPHA(_UT):
1676 state->uri->scheme.first = first; /* SCHEME BEGIN */
1677 return URI_FUNC(ParseSegmentNzNcOrScheme2)(state, first + 1, afterLast, memory);
1678
1679 case URI_SET_DIGIT(_UT):
1680 case URI_SET_SUB_DELIMS(_UT):
1681 case _UT('.'):
1682 case _UT('_'):
1683 case _UT('~'):
1684 case _UT('-'):
1685 case _UT('@'):
1686 state->uri->scheme.first = first; /* SEGMENT BEGIN, ABUSE SCHEME POINTER */
1687 return URI_FUNC(ParseMustBeSegmentNzNc)(state, first + 1, afterLast, memory);
1688
1689 case _UT('%'): {
1690 const URI_CHAR * const afterPctEncoded =
1691 URI_FUNC(ParsePctEncoded)(state, first, afterLast, memory);
1692 if (afterPctEncoded == NULL) {
1693 return NULL;
1694 }
1695 state->uri->scheme.first = first; /* SEGMENT BEGIN, ABUSE SCHEME POINTER */
1696 return URI_FUNC(ParseMustBeSegmentNzNc)(state, afterPctEncoded, afterLast,
1697 memory);
1698 }
1699
1700 case _UT('/'): {
1701 const URI_CHAR * const afterPartHelperTwo =
1702 URI_FUNC(ParsePartHelperTwo)(state, first + 1, afterLast, memory);
1703 if (afterPartHelperTwo == NULL) {
1704 return NULL;
1705 }
1706 return URI_FUNC(ParseUriTail)(state, afterPartHelperTwo, afterLast, memory);
1707 }
1708
1709 default:
1710 return URI_FUNC(ParseUriTail)(state, first, afterLast, memory);
1711 }
1712 }
1713
1714 /*
1715 * [uriTail]-><#>[queryFrag]
1716 * [uriTail]-><?>[queryFrag][uriTailTwo]
1717 * [uriTail]-><NULL>
1718 */
1719 static URI_INLINE const URI_CHAR * URI_FUNC(ParseUriTail)(URI_TYPE(ParserState) * state,
1720 const URI_CHAR * first,
1721 const URI_CHAR * afterLast,
1722 UriMemoryManager * memory) {
1723 if (first >= afterLast) {
1724 return afterLast;
1725 }
1726
1727 switch (*first) {
1728 case _UT('#'): {
1729 const URI_CHAR * const afterQueryFrag =
1730 URI_FUNC(ParseQueryFrag)(state, first + 1, afterLast, memory);
1731 if (afterQueryFrag == NULL) {
1732 return NULL;
1733 }
1734 state->uri->fragment.first = first + 1; /* FRAGMENT BEGIN */
1735 state->uri->fragment.afterLast = afterQueryFrag; /* FRAGMENT END */
1736 return afterQueryFrag;
1737 }
1738
1739 case _UT('?'): {
1740 const URI_CHAR * const afterQueryFrag =
1741 URI_FUNC(ParseQueryFrag)(state, first + 1, afterLast, memory);
1742 if (afterQueryFrag == NULL) {
1743 return NULL;
1744 }
1745 state->uri->query.first = first + 1; /* QUERY BEGIN */
1746 state->uri->query.afterLast = afterQueryFrag; /* QUERY END */
1747 return URI_FUNC(ParseUriTailTwo)(state, afterQueryFrag, afterLast, memory);
1748 }
1749
1750 default:
1751 return first;
1752 }
1753 }
1754
1755 /*
1756 * [uriTailTwo]-><#>[queryFrag]
1757 * [uriTailTwo]-><NULL>
1758 */
1759 static URI_INLINE const URI_CHAR *
1760 URI_FUNC(ParseUriTailTwo)(URI_TYPE(ParserState) * state, const URI_CHAR * first,
1761 const URI_CHAR * afterLast, UriMemoryManager * memory) {
1762 if (first >= afterLast) {
1763 return afterLast;
1764 }
1765
1766 switch (*first) {
1767 case _UT('#'): {
1768 const URI_CHAR * const afterQueryFrag =
1769 URI_FUNC(ParseQueryFrag)(state, first + 1, afterLast, memory);
1770 if (afterQueryFrag == NULL) {
1771 return NULL;
1772 }
1773 state->uri->fragment.first = first + 1; /* FRAGMENT BEGIN */
1774 state->uri->fragment.afterLast = afterQueryFrag; /* FRAGMENT END */
1775 return afterQueryFrag;
1776 }
1777
1778 default:
1779 return first;
1780 }
1781 }
1782
1783 /*
1784 * [zeroMoreSlashSegs]-></>[segment][zeroMoreSlashSegs]
1785 * [zeroMoreSlashSegs]-><NULL>
1786 */
1787 static const URI_CHAR * URI_FUNC(ParseZeroMoreSlashSegs)(URI_TYPE(ParserState) * state,
1788 const URI_CHAR * first,
1789 const URI_CHAR * afterLast,
1790 UriMemoryManager * memory) {
1791 tail_call:
1792 if (first >= afterLast) {
1793 return afterLast;
1794 }
1795
1796 switch (*first) {
1797 case _UT('/'): {
1798 const URI_CHAR * const afterSegment =
1799 URI_FUNC(ParseSegment)(state, first + 1, afterLast, memory);
1800 if (afterSegment == NULL) {
1801 return NULL;
1802 }
1803 if (!URI_FUNC(PushPathSegment)(state, first + 1, afterSegment,
1804 memory)) { /* SEGMENT BOTH */
1805 URI_FUNC(StopMalloc)(state, memory);
1806 return NULL;
1807 }
1808 first = afterSegment;
1809 goto tail_call;
1810 }
1811
1812 default:
1813 return first;
1814 }
1815 }
1816
1817 static URI_INLINE void URI_FUNC(ResetParserStateExceptUri)(URI_TYPE(ParserState)
1818 * state) {
1819 URI_TYPE(Uri) * const uriBackup = state->uri;
1820 memset(state, 0, sizeof(URI_TYPE(ParserState)));
1821 state->uri = uriBackup;
1822 }
1823
1824 static URI_INLINE UriBool URI_FUNC(PushPathSegment)(URI_TYPE(ParserState) * state,
1825 const URI_CHAR * first,
1826 const URI_CHAR * afterLast,
1827 UriMemoryManager * memory) {
1828 URI_TYPE(PathSegment) * segment =
1829 memory->calloc(memory, 1, sizeof(URI_TYPE(PathSegment)));
1830 if (segment == NULL) {
1831 return URI_FALSE; /* Raises malloc error */
1832 }
1833 if (first == afterLast) {
1834 segment->text.first = URI_FUNC(SafeToPointTo);
1835 segment->text.afterLast = URI_FUNC(SafeToPointTo);
1836 } else {
1837 segment->text.first = first;
1838 segment->text.afterLast = afterLast;
1839 }
1840
1841 /* First segment ever? */
1842 if (state->uri->pathHead == NULL) {
1843 /* First segment ever, set head and tail */
1844 state->uri->pathHead = segment;
1845 state->uri->pathTail = segment;
1846 } else {
1847 /* Append, update tail */
1848 state->uri->pathTail->next = segment;
1849 state->uri->pathTail = segment;
1850 }
1851
1852 return URI_TRUE; /* Success */
1853 }
1854
1855 int URI_FUNC(ParseUriEx)(URI_TYPE(ParserState) * state, const URI_CHAR * first,
1856 const URI_CHAR * afterLast) {
1857 return URI_FUNC(ParseUriExMm)(state, first, afterLast, NULL);
1858 }
1859
1860 static int URI_FUNC(ParseUriExMm)(URI_TYPE(ParserState) * state, const URI_CHAR * first,
1861 const URI_CHAR * afterLast, UriMemoryManager * memory) {
1862 const URI_CHAR * afterUriReference;
1863 URI_TYPE(Uri) * uri;
1864
1865 /* Check params */
1866 if ((state == NULL) || (first == NULL) || (afterLast == NULL)) {
1867 return URI_ERROR_NULL;
1868 }
1869 URI_CHECK_MEMORY_MANAGER(memory); /* may return */
1870
1871 uri = state->uri;
1872
1873 /* Init parser */
1874 URI_FUNC(ResetParserStateExceptUri)(state);
1875 URI_FUNC(ResetUri)(uri);
1876
1877 /* Parse */
1878 afterUriReference = URI_FUNC(ParseUriReference)(state, first, afterLast, memory);
1879 if (afterUriReference == NULL) {
1880 /* Waterproof errorPos <= afterLast */
1881 if (state->errorPos && (state->errorPos > afterLast)) {
1882 state->errorPos = afterLast;
1883 }
1884 return state->errorCode;
1885 }
1886 if (afterUriReference != afterLast) {
1887 if (afterUriReference < afterLast) {
1888 URI_FUNC(StopSyntax)(state, afterUriReference, memory);
1889 } else {
1890 URI_FUNC(StopSyntax)(state, afterLast, memory);
1891 }
1892 return state->errorCode;
1893 }
1894 return URI_SUCCESS;
1895 }
1896
1897 int URI_FUNC(ParseUri)(URI_TYPE(ParserState) * state, const URI_CHAR * text) {
1898 if ((state == NULL) || (text == NULL)) {
1899 return URI_ERROR_NULL;
1900 }
1901 return URI_FUNC(ParseUriEx)(state, text, text + URI_STRLEN(text));
1902 }
1903
1904 int URI_FUNC(ParseSingleUri)(URI_TYPE(Uri) * uri, const URI_CHAR * text,
1905 const URI_CHAR ** errorPos) {
1906 return URI_FUNC(ParseSingleUriEx)(uri, text, NULL, errorPos);
1907 }
1908
1909 int URI_FUNC(ParseSingleUriEx)(URI_TYPE(Uri) * uri, const URI_CHAR * first,
1910 const URI_CHAR * afterLast, const URI_CHAR ** errorPos) {
1911 if ((afterLast == NULL) && (first != NULL)) {
1912 afterLast = first + URI_STRLEN(first);
1913 }
1914 return URI_FUNC(ParseSingleUriExMm)(uri, first, afterLast, errorPos, NULL);
1915 }
1916
1917 int URI_FUNC(ParseSingleUriExMm)(URI_TYPE(Uri) * uri, const URI_CHAR * first,
1918 const URI_CHAR * afterLast, const URI_CHAR ** errorPos,
1919 UriMemoryManager * memory) {
1920 URI_TYPE(ParserState) state;
1921 int res;
1922
1923 /* Check params */
1924 if ((uri == NULL) || (first == NULL) || (afterLast == NULL)) {
1925 return URI_ERROR_NULL;
1926 }
1927 URI_CHECK_MEMORY_MANAGER(memory); /* may return */
1928
1929 state.uri = uri;
1930
1931 res = URI_FUNC(ParseUriExMm)(&state, first, afterLast, memory);
1932
1933 if (res != URI_SUCCESS) {
1934 if (errorPos != NULL) {
1935 *errorPos = state.errorPos;
1936 }
1937 URI_FUNC(FreeUriMembersMm)(uri, memory);
1938 }
1939
1940 return res;
1941 }
1942
1943 void URI_FUNC(FreeUriMembers)(URI_TYPE(Uri) * uri) {
1944 URI_FUNC(FreeUriMembersMm)(uri, NULL);
1945 }
1946
1947 int URI_FUNC(FreeUriMembersMm)(URI_TYPE(Uri) * uri, UriMemoryManager * memory) {
1948 if (uri == NULL) {
1949 return URI_ERROR_NULL;
1950 }
1951
1952 URI_CHECK_MEMORY_MANAGER(memory); /* may return */
1953
1954 if (uri->owner) {
1955 /* Scheme */
1956 if (uri->scheme.first != NULL) {
1957 if (uri->scheme.first != uri->scheme.afterLast) {
1958 memory->free(memory, (URI_CHAR *)uri->scheme.first);
1959 }
1960 uri->scheme.first = NULL;
1961 uri->scheme.afterLast = NULL;
1962 }
1963
1964 /* User info */
1965 if (uri->userInfo.first != NULL) {
1966 if (uri->userInfo.first != uri->userInfo.afterLast) {
1967 memory->free(memory, (URI_CHAR *)uri->userInfo.first);
1968 }
1969 uri->userInfo.first = NULL;
1970 uri->userInfo.afterLast = NULL;
1971 }
1972
1973 /* Host data - IPvFuture (may affect host text) */
1974 if (uri->hostData.ipFuture.first != NULL) {
1975 /* NOTE: .hostData.ipFuture holds the very same range pointers
1976 * as .hostText; we must not free memory twice. */
1977 uri->hostText.first = NULL;
1978 uri->hostText.afterLast = NULL;
1979
1980 if (uri->hostData.ipFuture.first != uri->hostData.ipFuture.afterLast) {
1981 memory->free(memory, (URI_CHAR *)uri->hostData.ipFuture.first);
1982 }
1983 uri->hostData.ipFuture.first = NULL;
1984 uri->hostData.ipFuture.afterLast = NULL;
1985 }
1986
1987 /* Host text (after IPvFuture, see above) */
1988 if (uri->hostText.first != NULL) {
1989 if (uri->hostText.first != uri->hostText.afterLast) {
1990 memory->free(memory, (URI_CHAR *)uri->hostText.first);
1991 }
1992 uri->hostText.first = NULL;
1993 uri->hostText.afterLast = NULL;
1994 }
1995 }
1996
1997 /* Host data - IPv4 */
1998 if (uri->hostData.ip4 != NULL) {
1999 memory->free(memory, uri->hostData.ip4);
2000 uri->hostData.ip4 = NULL;
2001 }
2002
2003 /* Host data - IPv6 */
2004 if (uri->hostData.ip6 != NULL) {
2005 memory->free(memory, uri->hostData.ip6);
2006 uri->hostData.ip6 = NULL;
2007 }
2008
2009 /* Port text */
2010 if (uri->owner && (uri->portText.first != NULL)) {
2011 if (uri->portText.first != uri->portText.afterLast) {
2012 memory->free(memory, (URI_CHAR *)uri->portText.first);
2013 }
2014 uri->portText.first = NULL;
2015 uri->portText.afterLast = NULL;
2016 }
2017
2018 /* Path */
2019 URI_FUNC(FreeUriPath)(uri, memory);
2020
2021 if (uri->owner) {
2022 /* Query */
2023 if (uri->query.first != NULL) {
2024 if (uri->query.first != uri->query.afterLast) {
2025 memory->free(memory, (URI_CHAR *)uri->query.first);
2026 }
2027 uri->query.first = NULL;
2028 uri->query.afterLast = NULL;
2029 }
2030
2031 /* Fragment */
2032 if (uri->fragment.first != NULL) {
2033 if (uri->fragment.first != uri->fragment.afterLast) {
2034 memory->free(memory, (URI_CHAR *)uri->fragment.first);
2035 }
2036 uri->fragment.first = NULL;
2037 uri->fragment.afterLast = NULL;
2038 }
2039 }
2040
2041 return URI_SUCCESS;
2042 }
2043
2044 UriBool URI_FUNC(_TESTING_ONLY_ParseIpSix)(const URI_CHAR * text) {
2045 UriMemoryManager * const memory = &defaultMemoryManager;
2046 URI_TYPE(Uri) uri;
2047 URI_TYPE(ParserState) parser;
2048 const URI_CHAR * const afterIpSix = text + URI_STRLEN(text);
2049 const URI_CHAR * res;
2050
2051 URI_FUNC(ResetUri)(&uri);
2052 parser.uri = &uri;
2053 URI_FUNC(ResetParserStateExceptUri)(&parser);
2054 parser.uri->hostData.ip6 = memory->malloc(memory, 1 * sizeof(UriIp6));
2055 res = URI_FUNC(ParseIPv6address2)(&parser, text, afterIpSix, memory);
2056 URI_FUNC(FreeUriMembersMm)(&uri, memory);
2057 return res == afterIpSix ? URI_TRUE : URI_FALSE;
2058 }
2059
2060 UriBool URI_FUNC(_TESTING_ONLY_ParseIpFour)(const URI_CHAR * text) {
2061 unsigned char octets[4];
2062 int res = URI_FUNC(ParseIpFourAddress)(octets, text, text + URI_STRLEN(text));
2063 return (res == URI_SUCCESS) ? URI_TRUE : URI_FALSE;
2064 }
2065
2066 # undef URI_SET_DIGIT
2067 # undef URI_SET_HEX_LETTER_UPPER
2068 # undef URI_SET_HEX_LETTER_LOWER
2069 # undef URI_SET_HEXDIG
2070 # undef URI_SET_ALPHA
2071
2072 #endif