Debian Codegraph @

components/playlist/xspf/uriparser/UriCommon.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 /* What encodings are enabled? */
41 #include <uriparser/UriDefsConfig.h>
42 #if (!defined(URI_PASS_ANSI) && !defined(URI_PASS_UNICODE))
43 /* Include SELF twice */
44 # ifdef URI_ENABLE_ANSI
45 # define URI_PASS_ANSI 1
46 # include "UriCommon.c"
47 # undef URI_PASS_ANSI
48 # endif
49 # ifdef URI_ENABLE_UNICODE
50 # define URI_PASS_UNICODE 1
51 # include "UriCommon.c"
52 # undef URI_PASS_UNICODE
53 # endif
54 #else
55 # ifdef URI_PASS_ANSI
56 # include <uriparser/UriDefsAnsi.h>
57 # else
58 # include <uriparser/UriDefsUnicode.h>
59 # include <wchar.h>
60 # endif
61
62 # ifndef URI_DOXYGEN
63 # include <uriparser/Uri.h>
64 # include "UriCommon.h"
65 # include "UriSets.h"
66 # endif
67
68 # include <assert.h>
69
70 /*extern*/ const URI_CHAR * const URI_FUNC(SafeToPointTo) = _UT("X");
71 /*extern*/ const URI_CHAR * const URI_FUNC(ConstPwd) = _UT(".");
72 /*extern*/ const URI_CHAR * const URI_FUNC(ConstParent) = _UT("..");
73
74 void URI_FUNC(ResetUri)(URI_TYPE(Uri) * uri) {
75 if (uri == NULL) {
76 return;
77 }
78 memset(uri, 0, sizeof(URI_TYPE(Uri)));
79 }
80
81 int URI_FUNC(FreeUriPath)(URI_TYPE(Uri) * uri, UriMemoryManager * memory) {
82 assert(uri != NULL);
83 assert(memory != NULL);
84
85 if (uri->pathHead != NULL) {
86 URI_TYPE(PathSegment) * segWalk = uri->pathHead;
87 while (segWalk != NULL) {
88 URI_TYPE(PathSegment) * const next = segWalk->next;
89 if ((uri->owner == URI_TRUE)
90 && (segWalk->text.first != segWalk->text.afterLast)) {
91 memory->free(memory, (URI_CHAR *)segWalk->text.first);
92 }
93 segWalk->text.first = NULL;
94 segWalk->text.afterLast = NULL;
95 segWalk->next = NULL;
96 memory->free(memory, segWalk);
97 segWalk = next;
98 }
99 uri->pathHead = NULL;
100 uri->pathTail = NULL;
101 }
102
103 return URI_SUCCESS;
104 }
105
106 /* Compares two text ranges for equal text content */
107 int URI_FUNC(CompareRange)(const URI_TYPE(TextRange) * a, const URI_TYPE(TextRange) * b) {
108 int diff;
109
110 /* NOTE: Both NULL means equal! */
111 if ((a == NULL) || (b == NULL)) {
112 return ((a == NULL) ? 0 : 1) - ((b == NULL) ? 0 : 1);
113 }
114
115 /* NOTE: Both NULL means equal! */
116 if ((a->first == NULL) || (b->first == NULL)) {
117 return ((a->first == NULL) ? 0 : 1) - ((b->first == NULL) ? 0 : 1);
118 }
119
120 diff = ((int)(a->afterLast - a->first) - (int)(b->afterLast - b->first));
121 if (diff > 0) {
122 return 1;
123 } else if (diff < 0) {
124 return -1;
125 }
126
127 diff = URI_STRNCMP(a->first, b->first, (a->afterLast - a->first));
128
129 if (diff > 0) {
130 return 1;
131 } else if (diff < 0) {
132 return -1;
133 }
134
135 return diff;
136 }
137
138 UriBool URI_FUNC(CopyRange)(URI_TYPE(TextRange) * destRange,
139 const URI_TYPE(TextRange) * sourceRange,
140 UriMemoryManager * memory) {
141 const int lenInChars = (int)(sourceRange->afterLast - sourceRange->first);
142 const int lenInBytes = lenInChars * sizeof(URI_CHAR);
143 URI_CHAR * dup = memory->malloc(memory, lenInBytes);
144 if (dup == NULL) {
145 return URI_FALSE;
146 }
147 memcpy(dup, sourceRange->first, lenInBytes);
148 destRange->first = dup;
149 destRange->afterLast = dup + lenInChars;
150
151 return URI_TRUE;
152 }
153
154 UriBool URI_FUNC(CopyRangeAsNeeded)(URI_TYPE(TextRange) * destRange,
155 const URI_TYPE(TextRange) * sourceRange,
156 UriMemoryManager * memory) {
157 if (sourceRange->first == NULL) {
158 destRange->first = NULL;
159 destRange->afterLast = NULL;
160 } else if (sourceRange->first == sourceRange->afterLast) {
161 destRange->first = URI_FUNC(SafeToPointTo);
162 destRange->afterLast = URI_FUNC(SafeToPointTo);
163 } else {
164 return URI_FUNC(CopyRange)(destRange, sourceRange, memory);
165 }
166
167 return URI_TRUE;
168 }
169
170 UriBool URI_FUNC(RemoveDotSegmentsEx)(URI_TYPE(Uri) * uri, UriBool relative,
171 UriBool pathOwned, UriMemoryManager * memory) {
172 URI_TYPE(PathSegment) * walker;
173 if ((uri == NULL) || (uri->pathHead == NULL)) {
174 return URI_TRUE;
175 }
176
177 walker = uri->pathHead;
178 walker->reserved = NULL; /* Prev pointer */
179 do {
180 UriBool removeSegment = URI_FALSE;
181 int len = (int)(walker->text.afterLast - walker->text.first);
182 switch (len) {
183 case 1:
184 if ((walker->text.first)[0] == _UT('.')) {
185 /* "." segment -> remove if not essential */
186 URI_TYPE(PathSegment) * const prev = walker->reserved;
187 URI_TYPE(PathSegment) * const nextBackup = walker->next;
188
189 /*
190 * Is this dot segment essential,
191 * i.e. is there a chance of changing semantics by dropping this dot
192 * segment?
193 *
194 * For example, changing "./http://foo" into "http://foo" would change
195 * semantics and hence the dot segment is essential to that case and
196 * cannot be removed.
197 *
198 * Other examples that would change semantics are:
199 * - cutting "/.//" down to "//"
200 * - cutting "scheme:/.//" down to "scheme://".
201 */
202 removeSegment = URI_TRUE;
203 if ((walker == uri->pathHead) && (walker->next != NULL)) {
204 /* Detect case "/.//" (with or without scheme) */
205 if ((walker->next->text.first == walker->next->text.afterLast)
206 && (URI_FUNC(HasHost)(uri) == URI_FALSE)) {
207 removeSegment = URI_FALSE;
208 /* Detect case "./withcolon:" */
209 } else if (relative) {
210 const URI_CHAR * ch = walker->next->text.first;
211 for (; ch < walker->next->text.afterLast; ch++) {
212 if (*ch == _UT(':')) {
213 removeSegment = URI_FALSE;
214 break;
215 }
216 }
217 }
218 }
219
220 if (removeSegment) {
221 /* .. then let's go remove that segment. */
222 /* Last segment? */
223 if (walker->next != NULL) {
224 /* Not last segment, i.e. first or middle segment
225 * OLD: (prev|NULL) <- walker <- next
226 * NEW: (prev|NULL) <----------- next */
227 walker->next->reserved = prev;
228
229 if (prev == NULL) {
230 /* First but not last segment
231 * OLD: head -> walker -> next
232 * NEW: head -----------> next */
233 uri->pathHead = walker->next;
234 } else {
235 /* Middle segment
236 * OLD: prev -> walker -> next
237 * NEW: prev -----------> next */
238 prev->next = walker->next;
239 }
240
241 if (pathOwned && (walker->text.first != walker->text.afterLast)) {
242 memory->free(memory, (URI_CHAR *)walker->text.first);
243 }
244 memory->free(memory, walker);
245 } else {
246 /* Last segment */
247 if (pathOwned && (walker->text.first != walker->text.afterLast)) {
248 memory->free(memory, (URI_CHAR *)walker->text.first);
249 }
250
251 if (prev == NULL) {
252 /* Last and first */
253 if (URI_FUNC(HasHost)(uri)) {
254 /* Replace "." with empty segment to represent trailing
255 * slash */
256 walker->text.first = URI_FUNC(SafeToPointTo);
257 walker->text.afterLast = URI_FUNC(SafeToPointTo);
258 } else {
259 memory->free(memory, walker);
260
261 uri->pathHead = NULL;
262 uri->pathTail = NULL;
263 }
264 } else {
265 /* Last but not first, replace "." with empty segment to
266 * represent trailing slash */
267 walker->text.first = URI_FUNC(SafeToPointTo);
268 walker->text.afterLast = URI_FUNC(SafeToPointTo);
269 }
270 }
271
272 walker = nextBackup;
273 }
274 }
275 break;
276
277 case 2:
278 if (((walker->text.first)[0] == _UT('.'))
279 && ((walker->text.first)[1] == _UT('.'))) {
280 /* Path ".." -> remove this and the previous segment */
281 URI_TYPE(PathSegment) * const prev = walker->reserved;
282 URI_TYPE(PathSegment) * prevPrev;
283 URI_TYPE(PathSegment) * const nextBackup = walker->next;
284
285 removeSegment = URI_TRUE;
286 if (relative) {
287 if (prev == NULL) {
288 /* We cannot remove traversal beyond because the
289 * URI is relative and may be resolved later.
290 * So we can simplify "a/../b/d" to "b/d" but
291 * we cannot simplify "../b/d" (outside of reference resolution).
292 */
293 removeSegment = URI_FALSE;
294 } else if ((prev != NULL)
295 && ((prev->text.afterLast - prev->text.first) == 2)
296 && ((prev->text.first)[0] == _UT('.'))
297 && ((prev->text.first)[1] == _UT('.'))) {
298 /* We need to protect against mis-simplifying "a/../../b" to
299 * "a/b". */
300 removeSegment = URI_FALSE;
301 }
302 }
303
304 if (removeSegment) {
305 if (prev != NULL) {
306 /* Not first segment */
307 prevPrev = prev->reserved;
308 if (prevPrev != NULL) {
309 /* Not even prev is the first one
310 * OLD: prevPrev -> prev -> walker -> (next|NULL)
311 * NEW: prevPrev -------------------> (next|NULL) */
312 prevPrev->next = walker->next;
313 if (walker->next != NULL) {
314 /* Update parent relationship as well
315 * OLD: prevPrev <- prev <- walker <- next
316 * NEW: prevPrev <------------------- next */
317 walker->next->reserved = prevPrev;
318 } else {
319 /* Last segment -> insert "" segment to represent trailing
320 * slash, update tail */
321 URI_TYPE(PathSegment) * const segment = memory->calloc(
322 memory, 1, sizeof(URI_TYPE(PathSegment)));
323 if (segment == NULL) {
324 if (pathOwned
325 && (walker->text.first
326 != walker->text.afterLast)) {
327 memory->free(memory,
328 (URI_CHAR *)walker->text.first);
329 }
330 memory->free(memory, walker);
331
332 if (pathOwned
333 && (prev->text.first != prev->text.afterLast)) {
334 memory->free(memory,
335 (URI_CHAR *)prev->text.first);
336 }
337 memory->free(memory, prev);
338
339 return URI_FALSE; /* Raises malloc error */
340 }
341 segment->text.first = URI_FUNC(SafeToPointTo);
342 segment->text.afterLast = URI_FUNC(SafeToPointTo);
343 prevPrev->next = segment;
344 uri->pathTail = segment;
345 }
346
347 if (pathOwned
348 && (walker->text.first != walker->text.afterLast)) {
349 memory->free(memory, (URI_CHAR *)walker->text.first);
350 }
351 memory->free(memory, walker);
352
353 if (pathOwned && (prev->text.first != prev->text.afterLast)) {
354 memory->free(memory, (URI_CHAR *)prev->text.first);
355 }
356 memory->free(memory, prev);
357
358 walker = nextBackup;
359 } else {
360 /* Prev is the first segment */
361 if (walker->next != NULL) {
362 uri->pathHead = walker->next;
363 walker->next->reserved = NULL;
364
365 if (pathOwned
366 && (walker->text.first != walker->text.afterLast)) {
367 memory->free(memory, (URI_CHAR *)walker->text.first);
368 }
369 memory->free(memory, walker);
370 } else {
371 /* Reuse segment for "" path segment to represent trailing
372 * slash, update tail */
373 URI_TYPE(PathSegment) * const segment = walker;
374 if (pathOwned
375 && (segment->text.first != segment->text.afterLast)) {
376 memory->free(memory, (URI_CHAR *)segment->text.first);
377 }
378 segment->text.first = URI_FUNC(SafeToPointTo);
379 segment->text.afterLast = URI_FUNC(SafeToPointTo);
380 uri->pathHead = segment;
381 uri->pathTail = segment;
382 }
383
384 if (pathOwned && (prev->text.first != prev->text.afterLast)) {
385 memory->free(memory, (URI_CHAR *)prev->text.first);
386 }
387 memory->free(memory, prev);
388
389 walker = nextBackup;
390 }
391 } else {
392 URI_TYPE(PathSegment) * const anotherNextBackup = walker->next;
393 int freeWalker = URI_TRUE;
394
395 /* First segment */
396 if (walker->next != NULL) {
397 /* First segment of multiple -> update head
398 * OLD: head -> walker -> next
399 * NEW: head -----------> next */
400 uri->pathHead = walker->next;
401
402 /* Update parent link as well
403 * OLD: head <- walker <- next
404 * NEW: head <----------- next */
405 walker->next->reserved = NULL;
406 } else {
407 if (uri->absolutePath) {
408 /* First and only segment -> update head
409 * OLD: head -> walker -> NULL
410 * NEW: head -----------> NULL */
411 uri->pathHead = NULL;
412
413 /* Last segment -> update tail
414 * OLD: tail -> walker
415 * NEW: tail -> NULL */
416 uri->pathTail = NULL;
417 } else {
418 /* Reuse segment for "" path segment to represent trailing
419 * slash, then update head and tail */
420 if (pathOwned
421 && (walker->text.first != walker->text.afterLast)) {
422 memory->free(memory, (URI_CHAR *)walker->text.first);
423 }
424 walker->text.first = URI_FUNC(SafeToPointTo);
425 walker->text.afterLast = URI_FUNC(SafeToPointTo);
426 freeWalker = URI_FALSE;
427 }
428 }
429
430 if (freeWalker) {
431 if (pathOwned
432 && (walker->text.first != walker->text.afterLast)) {
433 memory->free(memory, (URI_CHAR *)walker->text.first);
434 }
435 memory->free(memory, walker);
436 }
437
438 walker = anotherNextBackup;
439 }
440 }
441 }
442 break;
443 } /* end of switch */
444
445 if (!removeSegment) {
446 /* .. then let's move to the next element, and start again. */
447 if (walker->next != NULL) {
448 walker->next->reserved = walker;
449 } else {
450 /* Last segment -> update tail */
451 uri->pathTail = walker;
452 }
453 walker = walker->next;
454 }
455 } while (walker != NULL);
456
457 return URI_TRUE;
458 }
459
460 /* Properly removes "." and ".." path segments */
461 UriBool URI_FUNC(RemoveDotSegmentsAbsolute)(URI_TYPE(Uri) * uri,
462 UriMemoryManager * memory) {
463 const UriBool ABSOLUTE = URI_FALSE;
464 if (uri == NULL) {
465 return URI_TRUE;
466 }
467 return URI_FUNC(RemoveDotSegmentsEx)(uri, ABSOLUTE, uri->owner, memory);
468 }
469
470 unsigned char URI_FUNC(HexdigToInt)(URI_CHAR hexdig) {
471 switch (hexdig) {
472 case URI_SET_DIGIT(_UT):
473 return (unsigned char)(9 + hexdig - _UT('9'));
474 case URI_SET_HEX_LETTER_LOWER(_UT):
475 return (unsigned char)(15 + hexdig - _UT('f'));
476 case URI_SET_HEX_LETTER_UPPER(_UT):
477 return (unsigned char)(15 + hexdig - _UT('F'));
478
479 default:
480 return 0;
481 }
482 }
483
484 URI_CHAR URI_FUNC(HexToLetterEx)(unsigned int value, UriBool uppercase) {
485 switch (value) {
486 case 0:
487 return _UT('0');
488 case 1:
489 return _UT('1');
490 case 2:
491 return _UT('2');
492 case 3:
493 return _UT('3');
494 case 4:
495 return _UT('4');
496 case 5:
497 return _UT('5');
498 case 6:
499 return _UT('6');
500 case 7:
501 return _UT('7');
502 case 8:
503 return _UT('8');
504 case 9:
505 return _UT('9');
506
507 case 10:
508 return (uppercase == URI_TRUE) ? _UT('A') : _UT('a');
509 case 11:
510 return (uppercase == URI_TRUE) ? _UT('B') : _UT('b');
511 case 12:
512 return (uppercase == URI_TRUE) ? _UT('C') : _UT('c');
513 case 13:
514 return (uppercase == URI_TRUE) ? _UT('D') : _UT('d');
515 case 14:
516 return (uppercase == URI_TRUE) ? _UT('E') : _UT('e');
517 default:
518 return (uppercase == URI_TRUE) ? _UT('F') : _UT('f');
519 }
520 }
521
522 /* Checks if a URI has the host component set. */
523 UriBool URI_FUNC(HasHost)(const URI_TYPE(Uri) * uri) {
524 /* NOTE: .hostData.ipFuture.first is not being checked, *
525 * because we do check .hostText.first and *
526 * .hostData.ipFuture.first has to be identical to *
527 * .hostText.first if set, and hence there is *
528 * no more information to be gained. */
529 return (uri != NULL)
530 && ((uri->hostText.first != NULL) || (uri->hostData.ip4 != NULL)
531 || (uri->hostData.ip6 != NULL));
532 }
533
534 /* Copies the path segment list from one URI to another. */
535 UriBool URI_FUNC(CopyPath)(URI_TYPE(Uri) * dest, const URI_TYPE(Uri) * source,
536 UriMemoryManager * memory) {
537 if (source->pathHead == NULL) {
538 /* No path component */
539 dest->pathHead = NULL;
540 dest->pathTail = NULL;
541 } else {
542 /* Copy list but not the text contained */
543 URI_TYPE(PathSegment) * sourceWalker = source->pathHead;
544 URI_TYPE(PathSegment) * destPrev = NULL;
545 do {
546 URI_TYPE(PathSegment) * cur =
547 memory->malloc(memory, sizeof(URI_TYPE(PathSegment)));
548 if (cur == NULL) {
549 /* Fix broken list */
550 if (destPrev != NULL) {
551 destPrev->next = NULL;
552 }
553 return URI_FALSE; /* Raises malloc error */
554 }
555
556 /* From this functions usage we know that *
557 * the dest URI cannot be uri->owner */
558 cur->text = sourceWalker->text;
559 if (destPrev == NULL) {
560 /* First segment ever */
561 dest->pathHead = cur;
562 } else {
563 destPrev->next = cur;
564 }
565 destPrev = cur;
566 sourceWalker = sourceWalker->next;
567 } while (sourceWalker != NULL);
568 dest->pathTail = destPrev;
569 dest->pathTail->next = NULL;
570 }
571
572 dest->absolutePath = source->absolutePath;
573 return URI_TRUE;
574 }
575
576 /* Copies the authority part of an URI over to another. */
577 UriBool URI_FUNC(CopyAuthority)(URI_TYPE(Uri) * dest, const URI_TYPE(Uri) * source,
578 UriMemoryManager * memory) {
579 /* From this functions usage we know that *
580 * the dest URI cannot be uri->owner */
581
582 /* Copy userInfo */
583 dest->userInfo = source->userInfo;
584
585 /* Copy hostText */
586 dest->hostText = source->hostText;
587
588 /* Copy hostData */
589 if (source->hostData.ip4 != NULL) {
590 dest->hostData.ip4 = memory->malloc(memory, sizeof(UriIp4));
591 if (dest->hostData.ip4 == NULL) {
592 return URI_FALSE; /* Raises malloc error */
593 }
594 *(dest->hostData.ip4) = *(source->hostData.ip4);
595 dest->hostData.ip6 = NULL;
596 dest->hostData.ipFuture.first = NULL;
597 dest->hostData.ipFuture.afterLast = NULL;
598 } else if (source->hostData.ip6 != NULL) {
599 dest->hostData.ip4 = NULL;
600 dest->hostData.ip6 = memory->malloc(memory, sizeof(UriIp6));
601 if (dest->hostData.ip6 == NULL) {
602 return URI_FALSE; /* Raises malloc error */
603 }
604 *(dest->hostData.ip6) = *(source->hostData.ip6);
605 dest->hostData.ipFuture.first = NULL;
606 dest->hostData.ipFuture.afterLast = NULL;
607 } else {
608 dest->hostData.ip4 = NULL;
609 dest->hostData.ip6 = NULL;
610 dest->hostData.ipFuture = source->hostData.ipFuture;
611 }
612
613 /* Copy portText */
614 dest->portText = source->portText;
615
616 return URI_TRUE;
617 }
618
619 UriBool URI_FUNC(FixAmbiguity)(URI_TYPE(Uri) * uri, UriMemoryManager * memory) {
620 URI_TYPE(PathSegment) * segment;
621
622 if (/* Case 1: absolute path, empty first segment */
623 (uri->absolutePath && (uri->pathHead != NULL)
624 && (uri->pathHead->text.afterLast == uri->pathHead->text.first))
625
626 /* Case 2: relative path, empty first and second segment */
627 || (!uri->absolutePath && (uri->pathHead != NULL) && (uri->pathHead->next != NULL)
628 && (uri->pathHead->text.afterLast == uri->pathHead->text.first)
629 && (uri->pathHead->next->text.afterLast
630 == uri->pathHead->next->text.first))) {
631 /* NOOP */
632 } else {
633 return URI_TRUE;
634 }
635
636 segment = memory->malloc(memory, 1 * sizeof(URI_TYPE(PathSegment)));
637 if (segment == NULL) {
638 return URI_FALSE; /* Raises malloc error */
639 }
640
641 /* Insert "." segment in front */
642 segment->next = uri->pathHead;
643 segment->text.first = URI_FUNC(ConstPwd);
644 segment->text.afterLast = URI_FUNC(ConstPwd) + 1;
645 uri->pathHead = segment;
646 return URI_TRUE;
647 }
648
649 static UriBool URI_FUNC(PrependNewDotSegment)(URI_TYPE(Uri) * uri,
650 UriMemoryManager * memory) {
651 assert(uri != NULL);
652 assert(memory != NULL);
653
654 URI_TYPE(PathSegment) * const segment =
655 memory->malloc(memory, 1 * sizeof(URI_TYPE(PathSegment)));
656
657 if (segment == NULL) {
658 return URI_FALSE; /* i.e. raise malloc error */
659 }
660
661 segment->next = uri->pathHead;
662
663 URI_TYPE(TextRange) dotRange;
664 dotRange.first = URI_FUNC(ConstPwd);
665 dotRange.afterLast = URI_FUNC(ConstPwd) + 1;
666
667 if (uri->owner == URI_TRUE) {
668 if (URI_FUNC(CopyRange)(&(segment->text), &dotRange, memory) == URI_FALSE) {
669 memory->free(memory, segment);
670 return URI_FALSE; /* i.e. raise malloc error */
671 }
672 } else {
673 segment->text = dotRange; /* copies all members */
674 }
675
676 uri->pathHead = segment;
677
678 return URI_TRUE;
679 }
680
681 /* When dropping a scheme from a URI without a host and with a colon (":")
682 * in the first path segment, a consecutive reparse would rightfully
683 * mis-classify the first path segment as a scheme due to the colon.
684 * To protect against this case, we prepend an artificial "." segment
685 * to the path in here; the function is called after the scheme has
686 * just been dropped.
687 *
688 * 0. We start with parsed URI "scheme:path1:/path2/path3".
689 * 1. We drop the scheme naively and yield "path1:/path2/path3".
690 * 2. We prepend "." and yield unambiguous "./path1:/path2/path3".
691 *
692 * From the view of the RFC 3986 grammar, this is replacing rule path-rootless
693 * by path-noscheme content.
694 *
695 * Returns URI_TRUE for (a) nothing to do or (b) successful changes.
696 * Returns URI_FALSE to signal out-of-memory.
697 */
698 UriBool URI_FUNC(FixPathNoScheme)(URI_TYPE(Uri) * uri, UriMemoryManager * memory) {
699 assert(uri != NULL);
700 assert(memory != NULL);
701
702 if ((uri->absolutePath == URI_TRUE) || (uri->pathHead == NULL)
703 || (uri->scheme.first != NULL) || URI_FUNC(HasHost)(uri)) {
704 return URI_TRUE; /* i.e. nothing to do */
705 }
706
707 /* Check for troublesome first path segment containing a colon */
708 UriBool colonFound = URI_FALSE;
709 const URI_CHAR * walker = uri->pathHead->text.first;
710
711 while (walker < uri->pathHead->text.afterLast) {
712 if (walker[0] == _UT(':')) {
713 colonFound = URI_TRUE;
714 break;
715 }
716 walker++;
717 }
718
719 assert((walker == uri->pathHead->text.afterLast) || (colonFound == URI_TRUE));
720
721 if (colonFound == URI_FALSE) {
722 return URI_TRUE; /* i.e. nothing to do */
723 }
724
725 /* Insert "." segment in front */
726 return URI_FUNC(PrependNewDotSegment)(uri, memory);
727 }
728
729 /* When dropping a host from a URI without a scheme, an absolute path
730 * and and empty first path segment, a consecutive reparse would rightfully
731 * mis-classify the first path segment as a host marker due to the "//".
732 * To protect against this case, we prepend an artificial "." segment
733 * to the path in here; the function is called after the host has
734 * just been dropped.
735 *
736 * 0. We start with parsed URI "//host//path1/path2".
737 * 1. We drop the host naively and yield "//path1/path2".
738 * 2. We insert "./" and yield unambiguous "/.//path1/path2".
739 *
740 * Returns URI_TRUE for (a) nothing to do or (b) successful changes.
741 * Returns URI_FALSE to signal out-of-memory.
742 */
743 UriBool URI_FUNC(EnsureThatPathIsNotMistakenForHost)(URI_TYPE(Uri) * uri,
744 UriMemoryManager * memory) {
745 assert(uri != NULL);
746 assert(memory != NULL);
747
748 if ((URI_FUNC(HasHost)(uri) == URI_TRUE) || (uri->absolutePath == URI_FALSE)
749 || (uri->pathHead == NULL)
750 || (uri->pathHead == uri->pathTail) /* i.e. no second slash */
751 || (uri->pathHead->text.first != uri->pathHead->text.afterLast)) {
752 return URI_TRUE; /* i.e. nothing to do */
753 }
754
755 /* Insert "." segment in front */
756 return URI_FUNC(PrependNewDotSegment)(uri, memory);
757 }
758
759 void URI_FUNC(FixEmptyTrailSegment)(URI_TYPE(Uri) * uri, UriMemoryManager * memory) {
760 /* Fix path if only one empty segment */
761 if (!uri->absolutePath && !URI_FUNC(HasHost)(uri) && (uri->pathHead != NULL)
762 && (uri->pathHead->next == NULL)
763 && (uri->pathHead->text.first == uri->pathHead->text.afterLast)) {
764 memory->free(memory, uri->pathHead);
765 uri->pathHead = NULL;
766 uri->pathTail = NULL;
767 }
768 }
769
770 #endif