summaryrefslogtreecommitdiff
path: root/src/UriCommon.c
blob: 00256f201f7e2fd609a108a03e8f2fe57c30b0b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
/*
 * uriparser - RFC 3986 URI parsing library
 *
 * Copyright (C) 2007, Weijia Song <songweijia@gmail.com>
 * Copyright (C) 2007, Sebastian Pipping <sebastian@pipping.org>
 * All rights reserved.
 *
 * Redistribution and use in source  and binary forms, with or without
 * modification, are permitted provided  that the following conditions
 * are met:
 *
 *     1. Redistributions  of  source  code   must  retain  the  above
 *        copyright notice, this list  of conditions and the following
 *        disclaimer.
 *
 *     2. Redistributions  in binary  form  must  reproduce the  above
 *        copyright notice, this list  of conditions and the following
 *        disclaimer  in  the  documentation  and/or  other  materials
 *        provided with the distribution.
 *
 *     3. Neither the  name of the  copyright holder nor the  names of
 *        its contributors may be used  to endorse or promote products
 *        derived from  this software  without specific  prior written
 *        permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND  ANY EXPRESS OR IMPLIED WARRANTIES,  INCLUDING, BUT NOT
 * LIMITED TO,  THE IMPLIED WARRANTIES OF  MERCHANTABILITY AND FITNESS
 * FOR  A  PARTICULAR  PURPOSE  ARE  DISCLAIMED.  IN  NO  EVENT  SHALL
 * THE  COPYRIGHT HOLDER  OR CONTRIBUTORS  BE LIABLE  FOR ANY  DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO,  PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA,  OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT  LIABILITY,  OR  TORT (INCLUDING  NEGLIGENCE  OR  OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/* What encodings are enabled? */
#include <uriparser/UriDefsConfig.h>
#if (!defined(URI_PASS_ANSI) && !defined(URI_PASS_UNICODE))
/* Include SELF twice */
#  ifdef URI_ENABLE_ANSI
#    define URI_PASS_ANSI 1
#    include "UriCommon.c"
#    undef URI_PASS_ANSI
#  endif
#  ifdef URI_ENABLE_UNICODE
#    define URI_PASS_UNICODE 1
#    include "UriCommon.c"
#    undef URI_PASS_UNICODE
#  endif
#else
#  ifdef URI_PASS_ANSI
#    include <uriparser/UriDefsAnsi.h>
#  else
#    include <uriparser/UriDefsUnicode.h>
#    include <wchar.h>
#  endif

#  ifndef URI_DOXYGEN
#    include <uriparser/Uri.h>
#    include "UriCommon.h"
#    include "UriSets.h"
#  endif

#  include <assert.h>
#  include <stddef.h>

/*extern*/ const URI_CHAR * const URI_FUNC(SafeToPointTo) = _UT("X");
/*extern*/ const URI_CHAR * const URI_FUNC(ConstPwd) = _UT(".");
/*extern*/ const URI_CHAR * const URI_FUNC(ConstParent) = _UT("..");

void URI_FUNC(ResetUri)(URI_TYPE(Uri) * uri) {
    if (uri == NULL) {
        return;
    }
    memset(uri, 0, sizeof(URI_TYPE(Uri)));
}

int URI_FUNC(FreeUriPath)(URI_TYPE(Uri) * uri, UriMemoryManager * memory) {
    assert(uri != NULL);
    assert(memory != NULL);

    if (uri->pathHead != NULL) {
        URI_TYPE(PathSegment) * segWalk = uri->pathHead;
        while (segWalk != NULL) {
            URI_TYPE(PathSegment) * const next = segWalk->next;
            if ((uri->owner == URI_TRUE)
                && (segWalk->text.first != segWalk->text.afterLast)) {
                memory->free(memory, (URI_CHAR *)segWalk->text.first);
            }
            segWalk->text.first = NULL;
            segWalk->text.afterLast = NULL;
            segWalk->next = NULL;
            memory->free(memory, segWalk);
            segWalk = next;
        }
        uri->pathHead = NULL;
        uri->pathTail = NULL;
    }

    return URI_SUCCESS;
}

/* Compares two text ranges for equal text content */
int URI_FUNC(CompareRange)(const URI_TYPE(TextRange) * a, const URI_TYPE(TextRange) * b) {
    int diff;
    ptrdiff_t lenA;
    ptrdiff_t lenB;

    /* NOTE: Both NULL means equal! */
    if ((a == NULL) || (b == NULL)) {
        return ((a == NULL) ? 0 : 1) - ((b == NULL) ? 0 : 1);
    }

    /* NOTE: Both NULL means equal! */
    if ((a->first == NULL) || (b->first == NULL)) {
        return ((a->first == NULL) ? 0 : 1) - ((b->first == NULL) ? 0 : 1);
    }

    lenA = a->afterLast - a->first;
    lenB = b->afterLast - b->first;

    if (lenA > lenB) {
        return 1;
    } else if (lenA < lenB) {
        return -1;
    }

    diff = URI_STRNCMP(a->first, b->first, (size_t)lenA);

    if (diff > 0) {
        return 1;
    } else if (diff < 0) {
        return -1;
    }

    return diff;
}

UriBool URI_FUNC(CopyRange)(URI_TYPE(TextRange) * destRange,
                            const URI_TYPE(TextRange) * sourceRange,
                            UriMemoryManager * memory) {
    const int lenInChars = (int)(sourceRange->afterLast - sourceRange->first);
    const int lenInBytes = lenInChars * sizeof(URI_CHAR);
    URI_CHAR * dup = memory->malloc(memory, lenInBytes);
    if (dup == NULL) {
        return URI_FALSE;
    }
    memcpy(dup, sourceRange->first, lenInBytes);
    destRange->first = dup;
    destRange->afterLast = dup + lenInChars;

    return URI_TRUE;
}

UriBool URI_FUNC(CopyRangeAsNeeded)(URI_TYPE(TextRange) * destRange,
                                    const URI_TYPE(TextRange) * sourceRange,
                                    UriMemoryManager * memory) {
    if (sourceRange->first == NULL) {
        destRange->first = NULL;
        destRange->afterLast = NULL;
    } else if (sourceRange->first == sourceRange->afterLast) {
        destRange->first = URI_FUNC(SafeToPointTo);
        destRange->afterLast = URI_FUNC(SafeToPointTo);
    } else {
        return URI_FUNC(CopyRange)(destRange, sourceRange, memory);
    }

    return URI_TRUE;
}

UriBool URI_FUNC(RemoveDotSegmentsEx)(URI_TYPE(Uri) * uri, UriBool relative,
                                      UriBool pathOwned, UriMemoryManager * memory) {
    URI_TYPE(PathSegment) * walker;
    if ((uri == NULL) || (uri->pathHead == NULL)) {
        return URI_TRUE;
    }

    walker = uri->pathHead;
    walker->reserved = NULL; /* Prev pointer */
    do {
        UriBool removeSegment = URI_FALSE;
        int len = (int)(walker->text.afterLast - walker->text.first);
        switch (len) {
        case 1:
            if ((walker->text.first)[0] == _UT('.')) {
                /* "." segment -> remove if not essential */
                URI_TYPE(PathSegment) * const prev = walker->reserved;
                URI_TYPE(PathSegment) * const nextBackup = walker->next;

                /*
                 * Is this dot segment essential,
                 * i.e. is there a chance of changing semantics by dropping this dot
                 * segment?
                 *
                 * For example, changing "./http://foo" into "http://foo" would change
                 * semantics and hence the dot segment is essential to that case and
                 * cannot be removed.
                 *
                 * Other examples that would change semantics are:
                 * - cutting "/.//" down to "//"
                 * - cutting "scheme:/.//" down to "scheme://".
                 */
                removeSegment = URI_TRUE;
                if ((walker == uri->pathHead) && (walker->next != NULL)) {
                    /* Detect case "/.//" (with or without scheme) */
                    if ((walker->next->text.first == walker->next->text.afterLast)
                        && (URI_FUNC(HasHost)(uri) == URI_FALSE)) {
                        removeSegment = URI_FALSE;
                        /* Detect case "./withcolon:" */
                    } else if (relative) {
                        const URI_CHAR * ch = walker->next->text.first;
                        for (; ch < walker->next->text.afterLast; ch++) {
                            if (*ch == _UT(':')) {
                                removeSegment = URI_FALSE;
                                break;
                            }
                        }
                    }
                }

                if (removeSegment) {
                    /* .. then let's go remove that segment. */
                    /* Last segment? */
                    if (walker->next != NULL) {
                        /* Not last segment, i.e. first or middle segment
                         * OLD: (prev|NULL) <- walker <- next
                         * NEW: (prev|NULL) <----------- next */
                        walker->next->reserved = prev;

                        if (prev == NULL) {
                            /* First but not last segment
                             * OLD: head -> walker -> next
                             * NEW: head -----------> next */
                            uri->pathHead = walker->next;
                        } else {
                            /* Middle segment
                             * OLD: prev -> walker -> next
                             * NEW: prev -----------> next */
                            prev->next = walker->next;
                        }

                        if (pathOwned && (walker->text.first != walker->text.afterLast)) {
                            memory->free(memory, (URI_CHAR *)walker->text.first);
                        }
                        memory->free(memory, walker);
                    } else {
                        /* Last segment */
                        if (pathOwned && (walker->text.first != walker->text.afterLast)) {
                            memory->free(memory, (URI_CHAR *)walker->text.first);
                        }

                        if (prev == NULL) {
                            /* Last and first */
                            if (URI_FUNC(HasHost)(uri)) {
                                /* Replace "." with empty segment to represent trailing
                                 * slash */
                                walker->text.first = URI_FUNC(SafeToPointTo);
                                walker->text.afterLast = URI_FUNC(SafeToPointTo);
                            } else {
                                memory->free(memory, walker);

                                uri->pathHead = NULL;
                                uri->pathTail = NULL;
                            }
                        } else {
                            /* Last but not first, replace "." with empty segment to
                             * represent trailing slash */
                            walker->text.first = URI_FUNC(SafeToPointTo);
                            walker->text.afterLast = URI_FUNC(SafeToPointTo);
                        }
                    }

                    walker = nextBackup;
                }
            }
            break;

        case 2:
            if (((walker->text.first)[0] == _UT('.'))
                && ((walker->text.first)[1] == _UT('.'))) {
                /* Path ".." -> remove this and the previous segment */
                URI_TYPE(PathSegment) * const prev = walker->reserved;
                URI_TYPE(PathSegment) * prevPrev;
                URI_TYPE(PathSegment) * const nextBackup = walker->next;

                removeSegment = URI_TRUE;
                if (relative) {
                    if (prev == NULL) {
                        /* We cannot remove traversal beyond because the
                         * URI is relative and may be resolved later.
                         * So we can simplify "a/../b/d" to "b/d" but
                         * we cannot simplify "../b/d" (outside of reference resolution).
                         */
                        removeSegment = URI_FALSE;
                    } else if ((prev != NULL)
                               && ((prev->text.afterLast - prev->text.first) == 2)
                               && ((prev->text.first)[0] == _UT('.'))
                               && ((prev->text.first)[1] == _UT('.'))) {
                        /* We need to protect against mis-simplifying "a/../../b" to
                         * "a/b". */
                        removeSegment = URI_FALSE;
                    }
                }

                if (removeSegment) {
                    if (prev != NULL) {
                        /* Not first segment */
                        prevPrev = prev->reserved;
                        if (prevPrev != NULL) {
                            /* Not even prev is the first one
                             * OLD: prevPrev -> prev -> walker -> (next|NULL)
                             * NEW: prevPrev -------------------> (next|NULL) */
                            prevPrev->next = walker->next;
                            if (walker->next != NULL) {
                                /* Update parent relationship as well
                                 * OLD: prevPrev <- prev <- walker <- next
                                 * NEW: prevPrev <------------------- next */
                                walker->next->reserved = prevPrev;
                            } else {
                                /* Last segment -> insert "" segment to represent trailing
                                 * slash, update tail */
                                URI_TYPE(PathSegment) * const segment = memory->calloc(
                                    memory, 1, sizeof(URI_TYPE(PathSegment)));
                                if (segment == NULL) {
                                    if (pathOwned
                                        && (walker->text.first
                                            != walker->text.afterLast)) {
                                        memory->free(memory,
                                                     (URI_CHAR *)walker->text.first);
                                    }
                                    memory->free(memory, walker);

                                    if (pathOwned
                                        && (prev->text.first != prev->text.afterLast)) {
                                        memory->free(memory,
                                                     (URI_CHAR *)prev->text.first);
                                    }
                                    memory->free(memory, prev);

                                    return URI_FALSE; /* Raises malloc error */
                                }
                                segment->text.first = URI_FUNC(SafeToPointTo);
                                segment->text.afterLast = URI_FUNC(SafeToPointTo);
                                prevPrev->next = segment;
                                uri->pathTail = segment;
                            }

                            if (pathOwned
                                && (walker->text.first != walker->text.afterLast)) {
                                memory->free(memory, (URI_CHAR *)walker->text.first);
                            }
                            memory->free(memory, walker);

                            if (pathOwned && (prev->text.first != prev->text.afterLast)) {
                                memory->free(memory, (URI_CHAR *)prev->text.first);
                            }
                            memory->free(memory, prev);

                            walker = nextBackup;
                        } else {
                            /* Prev is the first segment */
                            if (walker->next != NULL) {
                                uri->pathHead = walker->next;
                                walker->next->reserved = NULL;

                                if (pathOwned
                                    && (walker->text.first != walker->text.afterLast)) {
                                    memory->free(memory, (URI_CHAR *)walker->text.first);
                                }
                                memory->free(memory, walker);
                            } else {
                                /* Reuse segment for "" path segment to represent trailing
                                 * slash, update tail */
                                URI_TYPE(PathSegment) * const segment = walker;
                                if (pathOwned
                                    && (segment->text.first != segment->text.afterLast)) {
                                    memory->free(memory, (URI_CHAR *)segment->text.first);
                                }
                                segment->text.first = URI_FUNC(SafeToPointTo);
                                segment->text.afterLast = URI_FUNC(SafeToPointTo);
                                uri->pathHead = segment;
                                uri->pathTail = segment;
                            }

                            if (pathOwned && (prev->text.first != prev->text.afterLast)) {
                                memory->free(memory, (URI_CHAR *)prev->text.first);
                            }
                            memory->free(memory, prev);

                            walker = nextBackup;
                        }
                    } else {
                        URI_TYPE(PathSegment) * const anotherNextBackup = walker->next;
                        int freeWalker = URI_TRUE;

                        /* First segment */
                        if (walker->next != NULL) {
                            /* First segment of multiple -> update head
                             * OLD: head -> walker -> next
                             * NEW: head -----------> next */
                            uri->pathHead = walker->next;

                            /* Update parent link as well
                             * OLD: head <- walker <- next
                             * NEW: head <----------- next */
                            walker->next->reserved = NULL;
                        } else {
                            if (uri->absolutePath) {
                                /* First and only segment -> update head
                                 * OLD: head -> walker -> NULL
                                 * NEW: head -----------> NULL */
                                uri->pathHead = NULL;

                                /* Last segment -> update tail
                                 * OLD: tail -> walker
                                 * NEW: tail -> NULL */
                                uri->pathTail = NULL;
                            } else {
                                /* Reuse segment for "" path segment to represent trailing
                                 * slash, then update head and tail */
                                if (pathOwned
                                    && (walker->text.first != walker->text.afterLast)) {
                                    memory->free(memory, (URI_CHAR *)walker->text.first);
                                }
                                walker->text.first = URI_FUNC(SafeToPointTo);
                                walker->text.afterLast = URI_FUNC(SafeToPointTo);
                                freeWalker = URI_FALSE;
                            }
                        }

                        if (freeWalker) {
                            if (pathOwned
                                && (walker->text.first != walker->text.afterLast)) {
                                memory->free(memory, (URI_CHAR *)walker->text.first);
                            }
                            memory->free(memory, walker);
                        }

                        walker = anotherNextBackup;
                    }
                }
            }
            break;
        } /* end of switch */

        if (!removeSegment) {
            /* .. then let's move to the next element, and start again. */
            if (walker->next != NULL) {
                walker->next->reserved = walker;
            } else {
                /* Last segment -> update tail */
                uri->pathTail = walker;
            }
            walker = walker->next;
        }
    } while (walker != NULL);

    return URI_TRUE;
}

/* Properly removes "." and ".." path segments */
UriBool URI_FUNC(RemoveDotSegmentsAbsolute)(URI_TYPE(Uri) * uri,
                                            UriMemoryManager * memory) {
    const UriBool ABSOLUTE = URI_FALSE;
    if (uri == NULL) {
        return URI_TRUE;
    }
    return URI_FUNC(RemoveDotSegmentsEx)(uri, ABSOLUTE, uri->owner, memory);
}

unsigned char URI_FUNC(HexdigToInt)(URI_CHAR hexdig) {
    switch (hexdig) {
    case URI_SET_DIGIT(_UT):
        return (unsigned char)(9 + hexdig - _UT('9'));
    case URI_SET_HEX_LETTER_LOWER(_UT):
        return (unsigned char)(15 + hexdig - _UT('f'));
    case URI_SET_HEX_LETTER_UPPER(_UT):
        return (unsigned char)(15 + hexdig - _UT('F'));

    default:
        return 0;
    }
}

URI_CHAR URI_FUNC(HexToLetterEx)(unsigned int value, UriBool uppercase) {
    switch (value) {
    case 0:
        return _UT('0');
    case 1:
        return _UT('1');
    case 2:
        return _UT('2');
    case 3:
        return _UT('3');
    case 4:
        return _UT('4');
    case 5:
        return _UT('5');
    case 6:
        return _UT('6');
    case 7:
        return _UT('7');
    case 8:
        return _UT('8');
    case 9:
        return _UT('9');

    case 10:
        return (uppercase == URI_TRUE) ? _UT('A') : _UT('a');
    case 11:
        return (uppercase == URI_TRUE) ? _UT('B') : _UT('b');
    case 12:
        return (uppercase == URI_TRUE) ? _UT('C') : _UT('c');
    case 13:
        return (uppercase == URI_TRUE) ? _UT('D') : _UT('d');
    case 14:
        return (uppercase == URI_TRUE) ? _UT('E') : _UT('e');
    default:
        return (uppercase == URI_TRUE) ? _UT('F') : _UT('f');
    }
}

/* Checks if a URI has the host component set. */
UriBool URI_FUNC(HasHost)(const URI_TYPE(Uri) * uri) {
    /* NOTE: .hostData.ipFuture.first is not being checked,   *
     *       because we do check .hostText.first and          *
     *       .hostData.ipFuture.first has to be identical to  *
     *       .hostText.first if set, and hence there is       *
     *       no more information to be gained.                */
    return (uri != NULL)
           && ((uri->hostText.first != NULL) || (uri->hostData.ip4 != NULL)
               || (uri->hostData.ip6 != NULL));
}

/* Copies the path segment list from one URI to another. */
UriBool URI_FUNC(CopyPath)(URI_TYPE(Uri) * dest, const URI_TYPE(Uri) * source,
                           UriMemoryManager * memory) {
    if (source->pathHead == NULL) {
        /* No path component */
        dest->pathHead = NULL;
        dest->pathTail = NULL;
    } else {
        /* Copy list but not the text contained */
        URI_TYPE(PathSegment) * sourceWalker = source->pathHead;
        URI_TYPE(PathSegment) * destPrev = NULL;
        do {
            URI_TYPE(PathSegment) * cur =
                memory->malloc(memory, sizeof(URI_TYPE(PathSegment)));
            if (cur == NULL) {
                /* Fix broken list */
                if (destPrev != NULL) {
                    destPrev->next = NULL;
                }
                return URI_FALSE; /* Raises malloc error */
            }

            /* From this functions usage we know that *
             * the dest URI cannot be uri->owner      */
            cur->text = sourceWalker->text;
            if (destPrev == NULL) {
                /* First segment ever */
                dest->pathHead = cur;
            } else {
                destPrev->next = cur;
            }
            destPrev = cur;
            sourceWalker = sourceWalker->next;
        } while (sourceWalker != NULL);
        dest->pathTail = destPrev;
        dest->pathTail->next = NULL;
    }

    dest->absolutePath = source->absolutePath;
    return URI_TRUE;
}

/* Copies the authority part of an URI over to another. */
UriBool URI_FUNC(CopyAuthority)(URI_TYPE(Uri) * dest, const URI_TYPE(Uri) * source,
                                UriMemoryManager * memory) {
    /* From this functions usage we know that *
     * the dest URI cannot be uri->owner      */

    /* Copy userInfo */
    dest->userInfo = source->userInfo;

    /* Copy hostText */
    dest->hostText = source->hostText;

    /* Copy hostData */
    if (source->hostData.ip4 != NULL) {
        dest->hostData.ip4 = memory->malloc(memory, sizeof(UriIp4));
        if (dest->hostData.ip4 == NULL) {
            return URI_FALSE; /* Raises malloc error */
        }
        *(dest->hostData.ip4) = *(source->hostData.ip4);
        dest->hostData.ip6 = NULL;
        dest->hostData.ipFuture.first = NULL;
        dest->hostData.ipFuture.afterLast = NULL;
    } else if (source->hostData.ip6 != NULL) {
        dest->hostData.ip4 = NULL;
        dest->hostData.ip6 = memory->malloc(memory, sizeof(UriIp6));
        if (dest->hostData.ip6 == NULL) {
            return URI_FALSE; /* Raises malloc error */
        }
        *(dest->hostData.ip6) = *(source->hostData.ip6);
        dest->hostData.ipFuture.first = NULL;
        dest->hostData.ipFuture.afterLast = NULL;
    } else {
        dest->hostData.ip4 = NULL;
        dest->hostData.ip6 = NULL;
        dest->hostData.ipFuture = source->hostData.ipFuture;
    }

    /* Copy portText */
    dest->portText = source->portText;

    return URI_TRUE;
}

UriBool URI_FUNC(FixAmbiguity)(URI_TYPE(Uri) * uri, UriMemoryManager * memory) {
    URI_TYPE(PathSegment) * segment;

    if (/* Case 1: absolute path, empty first segment */
        (uri->absolutePath && (uri->pathHead != NULL)
         && (uri->pathHead->text.afterLast == uri->pathHead->text.first))

        /* Case 2: relative path, empty first and second segment */
        || (!uri->absolutePath && (uri->pathHead != NULL) && (uri->pathHead->next != NULL)
            && (uri->pathHead->text.afterLast == uri->pathHead->text.first)
            && (uri->pathHead->next->text.afterLast
                == uri->pathHead->next->text.first))) {
        /* NOOP */
    } else {
        return URI_TRUE;
    }

    segment = memory->malloc(memory, 1 * sizeof(URI_TYPE(PathSegment)));
    if (segment == NULL) {
        return URI_FALSE; /* Raises malloc error */
    }

    /* Insert "." segment in front */
    segment->next = uri->pathHead;
    segment->text.first = URI_FUNC(ConstPwd);
    segment->text.afterLast = URI_FUNC(ConstPwd) + 1;
    uri->pathHead = segment;
    return URI_TRUE;
}

static UriBool URI_FUNC(PrependNewDotSegment)(URI_TYPE(Uri) * uri,
                                              UriMemoryManager * memory) {
    assert(uri != NULL);
    assert(memory != NULL);

    URI_TYPE(PathSegment) * const segment =
        memory->malloc(memory, 1 * sizeof(URI_TYPE(PathSegment)));

    if (segment == NULL) {
        return URI_FALSE; /* i.e. raise malloc error */
    }

    segment->next = uri->pathHead;

    URI_TYPE(TextRange) dotRange;
    dotRange.first = URI_FUNC(ConstPwd);
    dotRange.afterLast = URI_FUNC(ConstPwd) + 1;

    if (uri->owner == URI_TRUE) {
        if (URI_FUNC(CopyRange)(&(segment->text), &dotRange, memory) == URI_FALSE) {
            memory->free(memory, segment);
            return URI_FALSE; /* i.e. raise malloc error */
        }
    } else {
        segment->text = dotRange; /* copies all members */
    }

    uri->pathHead = segment;

    return URI_TRUE;
}

/* When dropping a scheme from a URI without a host and with a colon (":")
 * in the first path segment, a consecutive reparse would rightfully
 * mis-classify the first path segment as a scheme due to the colon.
 * To protect against this case, we prepend an artificial "." segment
 * to the path in here; the function is called after the scheme has
 * just been dropped.
 *
 * 0. We start with parsed URI "scheme:path1:/path2/path3".
 * 1. We drop the scheme naively and yield "path1:/path2/path3".
 * 2. We prepend "." and yield unambiguous "./path1:/path2/path3".
 *
 * From the view of the RFC 3986 grammar, this is replacing rule path-rootless
 * by path-noscheme content.
 *
 * Returns URI_TRUE for (a) nothing to do or (b) successful changes.
 * Returns URI_FALSE to signal out-of-memory.
 */
UriBool URI_FUNC(FixPathNoScheme)(URI_TYPE(Uri) * uri, UriMemoryManager * memory) {
    assert(uri != NULL);
    assert(memory != NULL);

    if ((uri->absolutePath == URI_TRUE) || (uri->pathHead == NULL)
        || (uri->scheme.first != NULL) || URI_FUNC(HasHost)(uri)) {
        return URI_TRUE; /* i.e. nothing to do */
    }

    /* Check for troublesome first path segment containing a colon */
    UriBool colonFound = URI_FALSE;
    const URI_CHAR * walker = uri->pathHead->text.first;

    while (walker < uri->pathHead->text.afterLast) {
        if (walker[0] == _UT(':')) {
            colonFound = URI_TRUE;
            break;
        }
        walker++;
    }

    assert((walker == uri->pathHead->text.afterLast) || (colonFound == URI_TRUE));

    if (colonFound == URI_FALSE) {
        return URI_TRUE; /* i.e. nothing to do */
    }

    /* Insert "." segment in front */
    return URI_FUNC(PrependNewDotSegment)(uri, memory);
}

/* When dropping a host from a URI without a scheme, an absolute path
 * and empty first path segment, a consecutive reparse would rightfully
 * mis-classify the first path segment as a host marker due to the "//".
 * To protect against this case, we prepend an artificial "." segment
 * to the path in here; the function is called after the host has
 * just been dropped.
 *
 * 0. We start with parsed URI "//host//path1/path2".
 * 1. We drop the host naively and yield "//path1/path2".
 * 2. We insert "./" and yield unambiguous "/.//path1/path2".
 *
 * Returns URI_TRUE for (a) nothing to do or (b) successful changes.
 * Returns URI_FALSE to signal out-of-memory.
 */
UriBool URI_FUNC(EnsureThatPathIsNotMistakenForHost)(URI_TYPE(Uri) * uri,
                                                     UriMemoryManager * memory) {
    assert(uri != NULL);
    assert(memory != NULL);

    if ((URI_FUNC(HasHost)(uri) == URI_TRUE) || (uri->absolutePath == URI_FALSE)
        || (uri->pathHead == NULL)
        || (uri->pathHead == uri->pathTail) /* i.e. no second slash */
        || (uri->pathHead->text.first != uri->pathHead->text.afterLast)) {
        return URI_TRUE; /* i.e. nothing to do */
    }

    /* Insert "." segment in front */
    return URI_FUNC(PrependNewDotSegment)(uri, memory);
}

void URI_FUNC(FixEmptyTrailSegment)(URI_TYPE(Uri) * uri, UriMemoryManager * memory) {
    /* Fix path if only one empty segment */
    if (!uri->absolutePath && !URI_FUNC(HasHost)(uri) && (uri->pathHead != NULL)
        && (uri->pathHead->next == NULL)
        && (uri->pathHead->text.first == uri->pathHead->text.afterLast)) {
        memory->free(memory, uri->pathHead);
        uri->pathHead = NULL;
        uri->pathTail = NULL;
    }
}

#endif