Debian Codegraph @

components/playlist/xspf/uriparser/UriNormalize.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 UriNormalize.c
42 * Holds the RFC 3986 %URI normalization 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 "UriNormalize.c"
53 # undef URI_PASS_ANSI
54 # endif
55 # ifdef URI_ENABLE_UNICODE
56 # define URI_PASS_UNICODE 1
57 # include "UriNormalize.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 "UriNormalizeBase.h"
71 # include "UriCommon.h"
72 # include "UriMemory.h"
73 # endif
74
75 # include <assert.h>
76
77 static int URI_FUNC(NormalizeSyntaxEngine)(URI_TYPE(Uri) * uri, unsigned int inMask,
78 unsigned int * outMask,
79 UriMemoryManager * memory);
80
81 static UriBool URI_FUNC(MakeRangeOwner)(unsigned int * revertMask, unsigned int maskTest,
82 URI_TYPE(TextRange) * range,
83 UriMemoryManager * memory);
84 static UriBool URI_FUNC(MakeOwnerEngine)(URI_TYPE(Uri) * uri, unsigned int * revertMask,
85 UriMemoryManager * memory);
86
87 static void URI_FUNC(FixPercentEncodingInplace)(const URI_CHAR * first,
88 const URI_CHAR ** afterLast);
89 static UriBool URI_FUNC(FixPercentEncodingMalloc)(const URI_CHAR ** first,
90 const URI_CHAR ** afterLast,
91 UriMemoryManager * memory);
92 static void URI_FUNC(FixPercentEncodingEngine)(const URI_CHAR * inFirst,
93 const URI_CHAR * inAfterLast,
94 const URI_CHAR * outFirst,
95 const URI_CHAR ** outAfterLast);
96
97 static UriBool URI_FUNC(ContainsUppercaseLetters)(const URI_CHAR * first,
98 const URI_CHAR * afterLast);
99 static UriBool URI_FUNC(ContainsUglyPercentEncoding)(const URI_CHAR * first,
100 const URI_CHAR * afterLast);
101
102 static void URI_FUNC(LowercaseInplace)(const URI_CHAR * first,
103 const URI_CHAR * afterLast);
104 static void URI_FUNC(LowercaseInplaceExceptPercentEncoding)(const URI_CHAR * first,
105 const URI_CHAR * afterLast);
106 static UriBool URI_FUNC(LowercaseMalloc)(const URI_CHAR ** first,
107 const URI_CHAR ** afterLast,
108 UriMemoryManager * memory);
109
110 void URI_FUNC(PreventLeakage)(URI_TYPE(Uri) * uri, unsigned int revertMask,
111 UriMemoryManager * memory) {
112 if (revertMask & URI_NORMALIZE_SCHEME) {
113 /* NOTE: A scheme cannot be the empty string
114 * so no need to compare .first with .afterLast, here. */
115 memory->free(memory, (URI_CHAR *)uri->scheme.first);
116 uri->scheme.first = NULL;
117 uri->scheme.afterLast = NULL;
118 }
119
120 if (revertMask & URI_NORMALIZE_USER_INFO) {
121 if (uri->userInfo.first != uri->userInfo.afterLast) {
122 memory->free(memory, (URI_CHAR *)uri->userInfo.first);
123 }
124 uri->userInfo.first = NULL;
125 uri->userInfo.afterLast = NULL;
126 }
127
128 if (revertMask & URI_NORMALIZE_HOST) {
129 if (uri->hostData.ipFuture.first != NULL) {
130 /* IPvFuture */
131 /* NOTE: An IPvFuture address cannot be the empty string
132 * so no need to compare .first with .afterLast, here. */
133 memory->free(memory, (URI_CHAR *)uri->hostData.ipFuture.first);
134 uri->hostData.ipFuture.first = NULL;
135 uri->hostData.ipFuture.afterLast = NULL;
136 uri->hostText.first = NULL;
137 uri->hostText.afterLast = NULL;
138 } else if (uri->hostText.first != NULL) {
139 /* Regname */
140 if (uri->hostText.first != uri->hostText.afterLast) {
141 memory->free(memory, (URI_CHAR *)uri->hostText.first);
142 }
143 uri->hostText.first = NULL;
144 uri->hostText.afterLast = NULL;
145 }
146 }
147
148 /* NOTE: Port cannot happen! */
149
150 if (revertMask & URI_NORMALIZE_PATH) {
151 URI_TYPE(PathSegment) * walker = uri->pathHead;
152 while (walker != NULL) {
153 URI_TYPE(PathSegment) * const next = walker->next;
154 if (walker->text.afterLast > walker->text.first) {
155 memory->free(memory, (URI_CHAR *)walker->text.first);
156 }
157 memory->free(memory, walker);
158 walker = next;
159 }
160 uri->pathHead = NULL;
161 uri->pathTail = NULL;
162 }
163
164 if (revertMask & URI_NORMALIZE_QUERY) {
165 if (uri->query.first != uri->query.afterLast) {
166 memory->free(memory, (URI_CHAR *)uri->query.first);
167 }
168 uri->query.first = NULL;
169 uri->query.afterLast = NULL;
170 }
171
172 if (revertMask & URI_NORMALIZE_FRAGMENT) {
173 if (uri->fragment.first != uri->fragment.afterLast) {
174 memory->free(memory, (URI_CHAR *)uri->fragment.first);
175 }
176 uri->fragment.first = NULL;
177 uri->fragment.afterLast = NULL;
178 }
179 }
180
181 static URI_INLINE UriBool URI_FUNC(ContainsUppercaseLetters)(const URI_CHAR * first,
182 const URI_CHAR * afterLast) {
183 if ((first != NULL) && (afterLast != NULL) && (afterLast > first)) {
184 const URI_CHAR * i = first;
185 for (; i < afterLast; i++) {
186 /* 6.2.2.1 Case Normalization: uppercase letters in scheme or host */
187 if ((*i >= _UT('A')) && (*i <= _UT('Z'))) {
188 return URI_TRUE;
189 }
190 }
191 }
192 return URI_FALSE;
193 }
194
195 static URI_INLINE UriBool URI_FUNC(ContainsUglyPercentEncoding)(
196 const URI_CHAR * first, const URI_CHAR * afterLast) {
197 if ((first != NULL) && (afterLast != NULL) && (afterLast > first)) {
198 const URI_CHAR * i = first;
199 for (; i + 2 < afterLast; i++) {
200 if (i[0] == _UT('%')) {
201 /* 6.2.2.1 Case Normalization: *
202 * lowercase percent-encodings */
203 if (((i[1] >= _UT('a')) && (i[1] <= _UT('f')))
204 || ((i[2] >= _UT('a')) && (i[2] <= _UT('f')))) {
205 return URI_TRUE;
206 } else {
207 /* 6.2.2.2 Percent-Encoding Normalization: *
208 * percent-encoded unreserved characters */
209 const unsigned char left = URI_FUNC(HexdigToInt)(i[1]);
210 const unsigned char right = URI_FUNC(HexdigToInt)(i[2]);
211 const int code = 16 * left + right;
212 if (uriIsUnreserved(code)) {
213 return URI_TRUE;
214 }
215 }
216 }
217 }
218 }
219 return URI_FALSE;
220 }
221
222 static URI_INLINE void URI_FUNC(LowercaseInplace)(const URI_CHAR * first,
223 const URI_CHAR * afterLast) {
224 if ((first != NULL) && (afterLast != NULL) && (afterLast > first)) {
225 URI_CHAR * i = (URI_CHAR *)first;
226 const int lowerUpperDiff = (_UT('a') - _UT('A'));
227 for (; i < afterLast; i++) {
228 if ((*i >= _UT('A')) && (*i <= _UT('Z'))) {
229 *i = (URI_CHAR)(*i + lowerUpperDiff);
230 }
231 }
232 }
233 }
234
235 static URI_INLINE void
236 URI_FUNC(LowercaseInplaceExceptPercentEncoding)(const URI_CHAR * first,
237 const URI_CHAR * afterLast) {
238 if ((first != NULL) && (afterLast != NULL) && (afterLast > first)) {
239 URI_CHAR * i = (URI_CHAR *)first;
240 const int lowerUpperDiff = (_UT('a') - _UT('A'));
241 for (; i < afterLast; i++) {
242 if ((*i >= _UT('A')) && (*i <= _UT('Z'))) {
243 *i = (URI_CHAR)(*i + lowerUpperDiff);
244 } else if (*i == _UT('%')) {
245 if (i + 3 >= afterLast) {
246 return;
247 }
248 i += 2;
249 }
250 }
251 }
252 }
253
254 static URI_INLINE UriBool URI_FUNC(LowercaseMalloc)(const URI_CHAR ** first,
255 const URI_CHAR ** afterLast,
256 UriMemoryManager * memory) {
257 int lenInChars;
258 const int lowerUpperDiff = (_UT('a') - _UT('A'));
259 URI_CHAR * buffer;
260 int i = 0;
261
262 if ((first == NULL) || (afterLast == NULL) || (*first == NULL)
263 || (*afterLast == NULL)) {
264 return URI_FALSE;
265 }
266
267 lenInChars = (int)(*afterLast - *first);
268 if (lenInChars == 0) {
269 return URI_TRUE;
270 } else if (lenInChars < 0) {
271 return URI_FALSE;
272 }
273
274 buffer = memory->malloc(memory, lenInChars * sizeof(URI_CHAR));
275 if (buffer == NULL) {
276 return URI_FALSE;
277 }
278
279 for (; i < lenInChars; i++) {
280 if (((*first)[i] >= _UT('A')) && ((*first)[i] <= _UT('Z'))) {
281 buffer[i] = (URI_CHAR)((*first)[i] + lowerUpperDiff);
282 } else {
283 buffer[i] = (*first)[i];
284 }
285 }
286
287 *first = buffer;
288 *afterLast = buffer + lenInChars;
289 return URI_TRUE;
290 }
291
292 /* NOTE: Implementation must stay inplace-compatible */
293 static URI_INLINE void
294 URI_FUNC(FixPercentEncodingEngine)(const URI_CHAR * inFirst, const URI_CHAR * inAfterLast,
295 const URI_CHAR * outFirst,
296 const URI_CHAR ** outAfterLast) {
297 URI_CHAR * write = (URI_CHAR *)outFirst;
298 const int lenInChars = (int)(inAfterLast - inFirst);
299 int i = 0;
300
301 /* All but last two */
302 for (; i + 2 < lenInChars; i++) {
303 if (inFirst[i] != _UT('%')) {
304 write[0] = inFirst[i];
305 write++;
306 } else {
307 /* 6.2.2.2 Percent-Encoding Normalization: *
308 * percent-encoded unreserved characters */
309 const URI_CHAR one = inFirst[i + 1];
310 const URI_CHAR two = inFirst[i + 2];
311 const unsigned char left = URI_FUNC(HexdigToInt)(one);
312 const unsigned char right = URI_FUNC(HexdigToInt)(two);
313 const int code = 16 * left + right;
314 if (uriIsUnreserved(code)) {
315 write[0] = (URI_CHAR)(code);
316 write++;
317 } else {
318 /* 6.2.2.1 Case Normalization: *
319 * uppercase percent-encodings */
320 write[0] = _UT('%');
321 write[1] = URI_FUNC(HexToLetterEx)(left, URI_TRUE);
322 write[2] = URI_FUNC(HexToLetterEx)(right, URI_TRUE);
323 write += 3;
324 }
325
326 i += 2; /* For the two chars of the percent group we just ate */
327 }
328 }
329
330 /* Last two */
331 for (; i < lenInChars; i++) {
332 write[0] = inFirst[i];
333 write++;
334 }
335
336 *outAfterLast = write;
337 }
338
339 static URI_INLINE void URI_FUNC(FixPercentEncodingInplace)(const URI_CHAR * first,
340 const URI_CHAR ** afterLast) {
341 /* Death checks */
342 if ((first == NULL) || (afterLast == NULL) || (*afterLast == NULL)) {
343 return;
344 }
345
346 /* Fix inplace */
347 URI_FUNC(FixPercentEncodingEngine)(first, *afterLast, first, afterLast);
348 }
349
350 static URI_INLINE UriBool URI_FUNC(FixPercentEncodingMalloc)(const URI_CHAR ** first,
351 const URI_CHAR ** afterLast,
352 UriMemoryManager * memory) {
353 int lenInChars;
354 URI_CHAR * buffer;
355
356 /* Death checks */
357 if ((first == NULL) || (afterLast == NULL) || (*first == NULL)
358 || (*afterLast == NULL)) {
359 return URI_FALSE;
360 }
361
362 /* Old text length */
363 lenInChars = (int)(*afterLast - *first);
364 if (lenInChars == 0) {
365 return URI_TRUE;
366 } else if (lenInChars < 0) {
367 return URI_FALSE;
368 }
369
370 /* New buffer */
371 buffer = memory->malloc(memory, lenInChars * sizeof(URI_CHAR));
372 if (buffer == NULL) {
373 return URI_FALSE;
374 }
375
376 /* Fix on copy */
377 URI_FUNC(FixPercentEncodingEngine)(*first, *afterLast, buffer, afterLast);
378 *first = buffer;
379 return URI_TRUE;
380 }
381
382 static URI_INLINE UriBool URI_FUNC(MakeRangeOwner)(unsigned int * revertMask,
383 unsigned int maskTest,
384 URI_TYPE(TextRange) * range,
385 UriMemoryManager * memory) {
386 if (((*revertMask & maskTest) == 0) && (range->first != NULL)
387 && (range->afterLast != NULL) && (range->afterLast > range->first)) {
388 if (URI_FUNC(CopyRange)(range, range, memory) == URI_FALSE) {
389 return URI_FALSE;
390 }
391 *revertMask |= maskTest;
392 }
393 return URI_TRUE;
394 }
395
396 static URI_INLINE UriBool URI_FUNC(MakeOwnerEngine)(URI_TYPE(Uri) * uri,
397 unsigned int * revertMask,
398 UriMemoryManager * memory) {
399 URI_TYPE(PathSegment) * walker = uri->pathHead;
400 if (!URI_FUNC(MakeRangeOwner)(revertMask, URI_NORMALIZE_SCHEME, &(uri->scheme),
401 memory)
402 || !URI_FUNC(MakeRangeOwner)(revertMask, URI_NORMALIZE_USER_INFO,
403 &(uri->userInfo), memory)
404 || !URI_FUNC(MakeRangeOwner)(revertMask, URI_NORMALIZE_QUERY, &(uri->query),
405 memory)
406 || !URI_FUNC(MakeRangeOwner)(revertMask, URI_NORMALIZE_FRAGMENT, &(uri->fragment),
407 memory)) {
408 return URI_FALSE; /* Raises malloc error */
409 }
410
411 /* Host */
412 if ((*revertMask & URI_NORMALIZE_HOST) == 0) {
413 if (uri->hostData.ipFuture.first != NULL) {
414 /* IPvFuture */
415 if (!URI_FUNC(MakeRangeOwner)(revertMask, URI_NORMALIZE_HOST,
416 &(uri->hostData.ipFuture), memory)) {
417 return URI_FALSE; /* Raises malloc error */
418 }
419 uri->hostText.first = uri->hostData.ipFuture.first;
420 uri->hostText.afterLast = uri->hostData.ipFuture.afterLast;
421 } else if (uri->hostText.first != NULL) {
422 /* Regname */
423 if (!URI_FUNC(MakeRangeOwner)(revertMask, URI_NORMALIZE_HOST,
424 &(uri->hostText), memory)) {
425 return URI_FALSE; /* Raises malloc error */
426 }
427 }
428 }
429
430 /* Path */
431 if ((*revertMask & URI_NORMALIZE_PATH) == 0) {
432 while (walker != NULL) {
433 if (!URI_FUNC(MakeRangeOwner)(revertMask, 0, &(walker->text), memory)) {
434 /* Free allocations done so far and kill path */
435
436 /* Kill path to one before walker (if any) */
437 URI_TYPE(PathSegment) * ranger = uri->pathHead;
438 while (ranger != walker) {
439 URI_TYPE(PathSegment) * const next = ranger->next;
440 if ((ranger->text.first != NULL) && (ranger->text.afterLast != NULL)
441 && (ranger->text.afterLast > ranger->text.first)) {
442 memory->free(memory, (URI_CHAR *)ranger->text.first);
443 }
444 memory->free(memory, ranger);
445 ranger = next;
446 }
447
448 /* Kill path from walker */
449 while (walker != NULL) {
450 URI_TYPE(PathSegment) * const next = walker->next;
451 memory->free(memory, walker);
452 walker = next;
453 }
454
455 uri->pathHead = NULL;
456 uri->pathTail = NULL;
457 return URI_FALSE; /* Raises malloc error */
458 }
459 walker = walker->next;
460 }
461 *revertMask |= URI_NORMALIZE_PATH;
462 }
463
464 /* Port text, must come last so we don't have to undo that one if it fails. *
465 * Otherwise we would need and extra enum flag for it although the port *
466 * cannot go unnormalized... */
467 if (!URI_FUNC(MakeRangeOwner)(revertMask, 0, &(uri->portText), memory)) {
468 return URI_FALSE; /* Raises malloc error */
469 }
470
471 return URI_TRUE;
472 }
473
474 unsigned int URI_FUNC(NormalizeSyntaxMaskRequired)(const URI_TYPE(Uri) * uri) {
475 unsigned int outMask = URI_NORMALIZED; /* for NULL uri */
476 URI_FUNC(NormalizeSyntaxMaskRequiredEx)(uri, &outMask);
477 return outMask;
478 }
479
480 int URI_FUNC(NormalizeSyntaxMaskRequiredEx)(const URI_TYPE(Uri) * uri,
481 unsigned int * outMask) {
482 UriMemoryManager * const memory = NULL; /* no use of memory manager */
483
484 # if defined(__GNUC__) \
485 && ((__GNUC__ > 4) \
486 || ((__GNUC__ == 4) && defined(__GNUC_MINOR__) && (__GNUC_MINOR__ >= 2)))
487 /* Slower code that fixes a warning, not sure if this is a smart idea */
488 URI_TYPE(Uri) writeableClone;
489 # endif
490
491 if ((uri == NULL) || (outMask == NULL)) {
492 return URI_ERROR_NULL;
493 }
494
495 # if defined(__GNUC__) \
496 && ((__GNUC__ > 4) \
497 || ((__GNUC__ == 4) && defined(__GNUC_MINOR__) && (__GNUC_MINOR__ >= 2)))
498 /* Slower code that fixes a warning, not sure if this is a smart idea */
499 memcpy(&writeableClone, uri, 1 * sizeof(URI_TYPE(Uri)));
500 URI_FUNC(NormalizeSyntaxEngine)(&writeableClone, 0, outMask, memory);
501 # else
502 URI_FUNC(NormalizeSyntaxEngine)((URI_TYPE(Uri) *)uri, 0, outMask, memory);
503 # endif
504 return URI_SUCCESS;
505 }
506
507 int URI_FUNC(NormalizeSyntaxEx)(URI_TYPE(Uri) * uri, unsigned int mask) {
508 return URI_FUNC(NormalizeSyntaxExMm)(uri, mask, NULL);
509 }
510
511 int URI_FUNC(NormalizeSyntaxExMm)(URI_TYPE(Uri) * uri, unsigned int mask,
512 UriMemoryManager * memory) {
513 URI_CHECK_MEMORY_MANAGER(memory); /* may return */
514 return URI_FUNC(NormalizeSyntaxEngine)(uri, mask, NULL, memory);
515 }
516
517 int URI_FUNC(NormalizeSyntax)(URI_TYPE(Uri) * uri) {
518 return URI_FUNC(NormalizeSyntaxEx)(uri, (unsigned int)-1);
519 }
520
521 static const URI_CHAR * URI_FUNC(PastLeadingZeros)(const URI_CHAR * first,
522 const URI_CHAR * afterLast) {
523 assert(first != NULL);
524 assert(afterLast != NULL);
525 assert(first != afterLast);
526
527 /* Find the first non-zero character */
528 const URI_CHAR * remainderFirst = first;
529 while ((remainderFirst < afterLast) && (remainderFirst[0] == _UT('0'))) {
530 remainderFirst++;
531 }
532
533 /* Is the string /all/ zeros? */
534 if (remainderFirst == afterLast) {
535 /* Yes, and length is >=1 because we ruled out the empty string earlier;
536 * pull back onto rightmost zero */
537 assert(remainderFirst > first);
538 remainderFirst--;
539 assert(remainderFirst[0] == _UT('0'));
540 }
541
542 return remainderFirst;
543 }
544
545 static void URI_FUNC(DropLeadingZerosInplace)(URI_CHAR * first,
546 const URI_CHAR ** afterLast) {
547 assert(first != NULL);
548 assert(afterLast != NULL);
549 assert(*afterLast != NULL);
550
551 if (first == *afterLast) {
552 return;
553 }
554
555 const URI_CHAR * const remainderFirst = URI_FUNC(PastLeadingZeros)(first, *afterLast);
556
557 if (remainderFirst > first) {
558 const size_t remainderLen = *afterLast - remainderFirst;
559 memmove(first, remainderFirst, remainderLen * sizeof(URI_CHAR));
560 first[remainderLen] = _UT('\0');
561 *afterLast = first + remainderLen;
562 }
563 }
564
565 static void URI_FUNC(AdvancePastLeadingZeros)(const URI_CHAR ** first,
566 const URI_CHAR * afterLast) {
567 assert(first != NULL);
568 assert(*first != NULL);
569 assert(afterLast != NULL);
570
571 if (*first == afterLast) {
572 return;
573 }
574
575 const URI_CHAR * const remainderFirst = URI_FUNC(PastLeadingZeros)(*first, afterLast);
576
577 /* Cut off leading zeros */
578 *first = remainderFirst;
579 }
580
581 static URI_INLINE int URI_FUNC(NormalizeSyntaxEngine)(URI_TYPE(Uri) * uri,
582 unsigned int inMask,
583 unsigned int * outMask,
584 UriMemoryManager * memory) {
585 unsigned int revertMask = URI_NORMALIZED;
586
587 /* Not just doing inspection? -> memory manager required! */
588 if (outMask == NULL) {
589 assert(memory != NULL);
590 }
591
592 if (uri == NULL) {
593 if (outMask != NULL) {
594 *outMask = URI_NORMALIZED;
595 return URI_SUCCESS;
596 } else {
597 return URI_ERROR_NULL;
598 }
599 }
600
601 if (outMask != NULL) {
602 /* Reset mask */
603 *outMask = URI_NORMALIZED;
604 } else if (inMask == URI_NORMALIZED) {
605 /* Nothing to do */
606 return URI_SUCCESS;
607 }
608
609 /* Scheme, host */
610 if (outMask != NULL) {
611 const UriBool normalizeScheme =
612 URI_FUNC(ContainsUppercaseLetters)(uri->scheme.first, uri->scheme.afterLast);
613 const UriBool normalizeHostCase = URI_FUNC(ContainsUppercaseLetters)(
614 uri->hostText.first, uri->hostText.afterLast);
615 if (normalizeScheme) {
616 *outMask |= URI_NORMALIZE_SCHEME;
617 }
618
619 if (normalizeHostCase) {
620 *outMask |= URI_NORMALIZE_HOST;
621 } else {
622 const UriBool normalizeHostPrecent = URI_FUNC(ContainsUglyPercentEncoding)(
623 uri->hostText.first, uri->hostText.afterLast);
624 if (normalizeHostPrecent) {
625 *outMask |= URI_NORMALIZE_HOST;
626 }
627 }
628 } else {
629 /* Scheme */
630 if ((inMask & URI_NORMALIZE_SCHEME) && (uri->scheme.first != NULL)) {
631 if (uri->owner) {
632 URI_FUNC(LowercaseInplace)(uri->scheme.first, uri->scheme.afterLast);
633 } else {
634 if (!URI_FUNC(LowercaseMalloc)(&(uri->scheme.first),
635 &(uri->scheme.afterLast), memory)) {
636 URI_FUNC(PreventLeakage)(uri, revertMask, memory);
637 return URI_ERROR_MALLOC;
638 }
639 revertMask |= URI_NORMALIZE_SCHEME;
640 }
641 }
642
643 /* Host */
644 if (inMask & URI_NORMALIZE_HOST) {
645 if (uri->hostData.ipFuture.first != NULL) {
646 /* IPvFuture */
647 if (uri->owner) {
648 URI_FUNC(LowercaseInplace)(uri->hostData.ipFuture.first,
649 uri->hostData.ipFuture.afterLast);
650 } else {
651 if (!URI_FUNC(LowercaseMalloc)(&(uri->hostData.ipFuture.first),
652 &(uri->hostData.ipFuture.afterLast),
653 memory)) {
654 URI_FUNC(PreventLeakage)(uri, revertMask, memory);
655 return URI_ERROR_MALLOC;
656 }
657 revertMask |= URI_NORMALIZE_HOST;
658 }
659 uri->hostText.first = uri->hostData.ipFuture.first;
660 uri->hostText.afterLast = uri->hostData.ipFuture.afterLast;
661 } else if ((uri->hostText.first != NULL) && (uri->hostData.ip4 == NULL)) {
662 /* Regname or IPv6 */
663 if (uri->owner) {
664 URI_FUNC(FixPercentEncodingInplace)(uri->hostText.first,
665 &(uri->hostText.afterLast));
666 } else {
667 if (!URI_FUNC(FixPercentEncodingMalloc)(
668 &(uri->hostText.first), &(uri->hostText.afterLast), memory)) {
669 URI_FUNC(PreventLeakage)(uri, revertMask, memory);
670 return URI_ERROR_MALLOC;
671 }
672 revertMask |= URI_NORMALIZE_HOST;
673 }
674
675 URI_FUNC(LowercaseInplaceExceptPercentEncoding)(uri->hostText.first,
676 uri->hostText.afterLast);
677 }
678 }
679 }
680
681 /* Port */
682 if (outMask != NULL) {
683 /* Is there a port even? */
684 if (uri->portText.first != NULL) {
685 /* Determine whether the port is already normalized, i.e. either "", "0" or no
686 * leading zeros */
687 const size_t portLen = uri->portText.afterLast - uri->portText.first;
688 if ((portLen > 1) && (uri->portText.first[0] == _UT('0'))) {
689 *outMask |= URI_NORMALIZE_PORT;
690 }
691 }
692 } else {
693 /* Normalize the port, i.e. drop leading zeros (except for string "0") */
694 if ((inMask & URI_NORMALIZE_PORT) && (uri->portText.first != NULL)) {
695 if (uri->owner) {
696 URI_FUNC(DropLeadingZerosInplace)((URI_CHAR *)uri->portText.first,
697 &(uri->portText.afterLast));
698 } else {
699 URI_FUNC(AdvancePastLeadingZeros)(&(uri->portText.first),
700 uri->portText.afterLast);
701 }
702 }
703 }
704
705 /* User info */
706 if (outMask != NULL) {
707 const UriBool normalizeUserInfo = URI_FUNC(ContainsUglyPercentEncoding)(
708 uri->userInfo.first, uri->userInfo.afterLast);
709 if (normalizeUserInfo) {
710 *outMask |= URI_NORMALIZE_USER_INFO;
711 }
712 } else {
713 if ((inMask & URI_NORMALIZE_USER_INFO) && (uri->userInfo.first != NULL)) {
714 if (uri->owner) {
715 URI_FUNC(FixPercentEncodingInplace)(uri->userInfo.first,
716 &(uri->userInfo.afterLast));
717 } else {
718 if (!URI_FUNC(FixPercentEncodingMalloc)(
719 &(uri->userInfo.first), &(uri->userInfo.afterLast), memory)) {
720 URI_FUNC(PreventLeakage)(uri, revertMask, memory);
721 return URI_ERROR_MALLOC;
722 }
723 revertMask |= URI_NORMALIZE_USER_INFO;
724 }
725 }
726 }
727
728 /* Path */
729 if (outMask != NULL) {
730 const URI_TYPE(PathSegment) * walker = uri->pathHead;
731 while (walker != NULL) {
732 const URI_CHAR * const first = walker->text.first;
733 const URI_CHAR * const afterLast = walker->text.afterLast;
734 if ((first != NULL) && (afterLast != NULL) && (afterLast > first)
735 && ((((afterLast - first) == 1) && (first[0] == _UT('.')))
736 || (((afterLast - first) == 2) && (first[0] == _UT('.'))
737 && (first[1] == _UT('.')))
738 || URI_FUNC(ContainsUglyPercentEncoding)(first, afterLast))) {
739 *outMask |= URI_NORMALIZE_PATH;
740 break;
741 }
742 walker = walker->next;
743 }
744 } else if (inMask & URI_NORMALIZE_PATH) {
745 URI_TYPE(PathSegment) * walker;
746 const UriBool relative =
747 ((uri->scheme.first == NULL) && !uri->absolutePath) ? URI_TRUE : URI_FALSE;
748
749 /* Fix percent-encoding for each segment */
750 walker = uri->pathHead;
751 if (uri->owner) {
752 while (walker != NULL) {
753 URI_FUNC(FixPercentEncodingInplace)(walker->text.first,
754 &(walker->text.afterLast));
755 walker = walker->next;
756 }
757 } else {
758 while (walker != NULL) {
759 if (!URI_FUNC(FixPercentEncodingMalloc)(
760 &(walker->text.first), &(walker->text.afterLast), memory)) {
761 URI_FUNC(PreventLeakage)(uri, revertMask, memory);
762 return URI_ERROR_MALLOC;
763 }
764 walker = walker->next;
765 }
766 revertMask |= URI_NORMALIZE_PATH;
767 }
768
769 /* 6.2.2.3 Path Segment Normalization */
770 if (!URI_FUNC(RemoveDotSegmentsEx)(
771 uri, relative,
772 (uri->owner == URI_TRUE) || ((revertMask & URI_NORMALIZE_PATH) != 0),
773 memory)) {
774 URI_FUNC(PreventLeakage)(uri, revertMask, memory);
775 return URI_ERROR_MALLOC;
776 }
777 URI_FUNC(FixEmptyTrailSegment)(uri, memory);
778 }
779
780 /* Query, fragment */
781 if (outMask != NULL) {
782 const UriBool normalizeQuery =
783 URI_FUNC(ContainsUglyPercentEncoding)(uri->query.first, uri->query.afterLast);
784 const UriBool normalizeFragment = URI_FUNC(ContainsUglyPercentEncoding)(
785 uri->fragment.first, uri->fragment.afterLast);
786 if (normalizeQuery) {
787 *outMask |= URI_NORMALIZE_QUERY;
788 }
789
790 if (normalizeFragment) {
791 *outMask |= URI_NORMALIZE_FRAGMENT;
792 }
793 } else {
794 /* Query */
795 if ((inMask & URI_NORMALIZE_QUERY) && (uri->query.first != NULL)) {
796 if (uri->owner) {
797 URI_FUNC(FixPercentEncodingInplace)(uri->query.first,
798 &(uri->query.afterLast));
799 } else {
800 if (!URI_FUNC(FixPercentEncodingMalloc)(
801 &(uri->query.first), &(uri->query.afterLast), memory)) {
802 URI_FUNC(PreventLeakage)(uri, revertMask, memory);
803 return URI_ERROR_MALLOC;
804 }
805 revertMask |= URI_NORMALIZE_QUERY;
806 }
807 }
808
809 /* Fragment */
810 if ((inMask & URI_NORMALIZE_FRAGMENT) && (uri->fragment.first != NULL)) {
811 if (uri->owner) {
812 URI_FUNC(FixPercentEncodingInplace)(uri->fragment.first,
813 &(uri->fragment.afterLast));
814 } else {
815 if (!URI_FUNC(FixPercentEncodingMalloc)(
816 &(uri->fragment.first), &(uri->fragment.afterLast), memory)) {
817 URI_FUNC(PreventLeakage)(uri, revertMask, memory);
818 return URI_ERROR_MALLOC;
819 }
820 revertMask |= URI_NORMALIZE_FRAGMENT;
821 }
822 }
823 }
824
825 /* Dup all not duped yet */
826 if ((outMask == NULL) && !uri->owner) {
827 if (!URI_FUNC(MakeOwnerEngine)(uri, &revertMask, memory)) {
828 URI_FUNC(PreventLeakage)(uri, revertMask, memory);
829 return URI_ERROR_MALLOC;
830 }
831 uri->owner = URI_TRUE;
832 }
833
834 return URI_SUCCESS;
835 }
836
837 int URI_FUNC(MakeOwnerMm)(URI_TYPE(Uri) * uri, UriMemoryManager * memory) {
838 unsigned int revertMask = URI_NORMALIZED;
839
840 URI_CHECK_MEMORY_MANAGER(memory); /* may return */
841
842 if (uri == NULL) {
843 return URI_ERROR_NULL;
844 }
845
846 if (uri->owner == URI_TRUE) {
847 return URI_SUCCESS;
848 }
849
850 if (!URI_FUNC(MakeOwnerEngine)(uri, &revertMask, memory)) {
851 URI_FUNC(PreventLeakage)(uri, revertMask, memory);
852 return URI_ERROR_MALLOC;
853 }
854
855 uri->owner = URI_TRUE;
856
857 return URI_SUCCESS;
858 }
859
860 int URI_FUNC(MakeOwner)(URI_TYPE(Uri) * uri) {
861 return URI_FUNC(MakeOwnerMm)(uri, NULL);
862 }
863
864 #endif