Debian Codegraph @

components/playlist/xspf/uriparser/UriRecompose.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 "UriRecompose.c"
47 # undef URI_PASS_ANSI
48 # endif
49 # ifdef URI_ENABLE_UNICODE
50 # define URI_PASS_UNICODE 1
51 # include "UriRecompose.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 # endif
66
67 static int URI_FUNC(ToStringEngine)(URI_CHAR * dest, const URI_TYPE(Uri) * uri,
68 int maxChars, int * charsWritten,
69 int * charsRequired);
70
71 int URI_FUNC(ToStringCharsRequired)(const URI_TYPE(Uri) * uri, int * charsRequired) {
72 const int MAX_CHARS = ((unsigned int)-1) >> 1;
73 return URI_FUNC(ToStringEngine)(NULL, uri, MAX_CHARS, NULL, charsRequired);
74 }
75
76 int URI_FUNC(ToString)(URI_CHAR * dest, const URI_TYPE(Uri) * uri, int maxChars,
77 int * charsWritten) {
78 return URI_FUNC(ToStringEngine)(dest, uri, maxChars, charsWritten, NULL);
79 }
80
81 static URI_INLINE int URI_FUNC(ToStringEngine)(URI_CHAR * dest, const URI_TYPE(Uri) * uri,
82 int maxChars, int * charsWritten,
83 int * charsRequired) {
84 int written = 0;
85 if ((uri == NULL) || ((dest == NULL) && (charsRequired == NULL))) {
86 if (charsWritten != NULL) {
87 *charsWritten = 0;
88 }
89 return URI_ERROR_NULL;
90 }
91
92 if (maxChars < 1) {
93 if (charsWritten != NULL) {
94 *charsWritten = 0;
95 }
96 return URI_ERROR_TOSTRING_TOO_LONG;
97 }
98 maxChars--; /* So we don't have to subtract 1 for '\0' all the time */
99
100 /* NOTE: The curly brackets here force deeper indent (and that's all) */
101 {
102 {
103 {
104 /* clang-format off */
105 /* [01/19] result = "" */
106 /* clang-format on */
107 if (dest != NULL) {
108 dest[0] = _UT('\0');
109 } else {
110 (*charsRequired) = 0;
111 }
112 /* clang-format off */
113 /* [02/19] if defined(scheme) then */
114 /* clang-format on */
115 if (uri->scheme.first != NULL) {
116 /* clang-format off */
117 /* [03/19] append scheme to result; */
118 /* clang-format on */
119 const int charsToWrite =
120 (int)(uri->scheme.afterLast - uri->scheme.first);
121 if (dest != NULL) {
122 if (written + charsToWrite <= maxChars) {
123 memcpy(dest + written, uri->scheme.first,
124 charsToWrite * sizeof(URI_CHAR));
125 written += charsToWrite;
126 } else {
127 dest[0] = _UT('\0');
128 if (charsWritten != NULL) {
129 *charsWritten = 0;
130 }
131 return URI_ERROR_TOSTRING_TOO_LONG;
132 }
133 } else {
134 (*charsRequired) += charsToWrite;
135 }
136 /* clang-format off */
137 /* [04/19] append ":" to result; */
138 /* clang-format on */
139 if (dest != NULL) {
140 if (written + 1 <= maxChars) {
141 memcpy(dest + written, _UT(":"), 1 * sizeof(URI_CHAR));
142 written += 1;
143 } else {
144 dest[0] = _UT('\0');
145 if (charsWritten != NULL) {
146 *charsWritten = 0;
147 }
148 return URI_ERROR_TOSTRING_TOO_LONG;
149 }
150 } else {
151 (*charsRequired) += 1;
152 }
153 /* clang-format off */
154 /* [05/19] endif; */
155 /* clang-format on */
156 }
157 /* clang-format off */
158 /* [06/19] if defined(authority) then */
159 /* clang-format on */
160 if (URI_FUNC(HasHost)(uri)) {
161 /* clang-format off */
162 /* [07/19] append "//" to result; */
163 /* clang-format on */
164 if (dest != NULL) {
165 if (written + 2 <= maxChars) {
166 memcpy(dest + written, _UT("//"), 2 * sizeof(URI_CHAR));
167 written += 2;
168 } else {
169 dest[0] = _UT('\0');
170 if (charsWritten != NULL) {
171 *charsWritten = 0;
172 }
173 return URI_ERROR_TOSTRING_TOO_LONG;
174 }
175 } else {
176 (*charsRequired) += 2;
177 }
178 /* clang-format off */
179 /* [08/19] append authority to result; */
180 /* clang-format on */
181 /* UserInfo */
182 if (uri->userInfo.first != NULL) {
183 const int charsToWrite =
184 (int)(uri->userInfo.afterLast - uri->userInfo.first);
185 if (dest != NULL) {
186 if (written + charsToWrite <= maxChars) {
187 memcpy(dest + written, uri->userInfo.first,
188 charsToWrite * sizeof(URI_CHAR));
189 written += charsToWrite;
190 } else {
191 dest[0] = _UT('\0');
192 if (charsWritten != NULL) {
193 *charsWritten = 0;
194 }
195 return URI_ERROR_TOSTRING_TOO_LONG;
196 }
197
198 if (written + 1 <= maxChars) {
199 memcpy(dest + written, _UT("@"), 1 * sizeof(URI_CHAR));
200 written += 1;
201 } else {
202 dest[0] = _UT('\0');
203 if (charsWritten != NULL) {
204 *charsWritten = 0;
205 }
206 return URI_ERROR_TOSTRING_TOO_LONG;
207 }
208 } else {
209 (*charsRequired) += charsToWrite + 1;
210 }
211 }
212
213 /* Host */
214 if (uri->hostData.ip4 != NULL) {
215 /* IPv4 */
216 int i = 0;
217 for (; i < 4; i++) {
218 const unsigned char value = uri->hostData.ip4->data[i];
219 const int charsToWrite =
220 (value > 99) ? 3 : ((value > 9) ? 2 : 1);
221 if (dest != NULL) {
222 if (written + charsToWrite <= maxChars) {
223 URI_CHAR text[4];
224 if (value > 99) {
225 text[0] = _UT('0') + (value / 100);
226 text[1] = _UT('0') + ((value % 100) / 10);
227 text[2] = _UT('0') + (value % 10);
228 } else if (value > 9) {
229 text[0] = _UT('0') + (value / 10);
230 text[1] = _UT('0') + (value % 10);
231 } else {
232 text[0] = _UT('0') + value;
233 }
234 text[charsToWrite] = _UT('\0');
235 memcpy(dest + written, text,
236 charsToWrite * sizeof(URI_CHAR));
237 written += charsToWrite;
238 } else {
239 dest[0] = _UT('\0');
240 if (charsWritten != NULL) {
241 *charsWritten = 0;
242 }
243 return URI_ERROR_TOSTRING_TOO_LONG;
244 }
245 if (i < 3) {
246 if (written + 1 <= maxChars) {
247 memcpy(dest + written, _UT("."),
248 1 * sizeof(URI_CHAR));
249 written += 1;
250 } else {
251 dest[0] = _UT('\0');
252 if (charsWritten != NULL) {
253 *charsWritten = 0;
254 }
255 return URI_ERROR_TOSTRING_TOO_LONG;
256 }
257 }
258 } else {
259 (*charsRequired) += charsToWrite + ((i == 3) ? 0 : 1);
260 }
261 }
262 } else if (uri->hostData.ip6 != NULL) {
263 /* IPv6 */
264 int i = 0;
265 if (dest != NULL) {
266 if (written + 1 <= maxChars) {
267 memcpy(dest + written, _UT("["), 1 * sizeof(URI_CHAR));
268 written += 1;
269 } else {
270 dest[0] = _UT('\0');
271 if (charsWritten != NULL) {
272 *charsWritten = 0;
273 }
274 return URI_ERROR_TOSTRING_TOO_LONG;
275 }
276 } else {
277 (*charsRequired) += 1;
278 }
279
280 for (; i < 16; i++) {
281 const unsigned char value = uri->hostData.ip6->data[i];
282 if (dest != NULL) {
283 if (written + 2 <= maxChars) {
284 URI_CHAR text[3];
285 text[0] =
286 URI_FUNC(HexToLetterEx)(value / 16, URI_FALSE);
287 text[1] =
288 URI_FUNC(HexToLetterEx)(value % 16, URI_FALSE);
289 text[2] = _UT('\0');
290 memcpy(dest + written, text, 2 * sizeof(URI_CHAR));
291 written += 2;
292 } else {
293 dest[0] = _UT('\0');
294 if (charsWritten != NULL) {
295 *charsWritten = 0;
296 }
297 return URI_ERROR_TOSTRING_TOO_LONG;
298 }
299 } else {
300 (*charsRequired) += 2;
301 }
302 if (((i & 1) == 1) && (i < 15)) {
303 if (dest != NULL) {
304 if (written + 1 <= maxChars) {
305 memcpy(dest + written, _UT(":"),
306 1 * sizeof(URI_CHAR));
307 written += 1;
308 } else {
309 dest[0] = _UT('\0');
310 if (charsWritten != NULL) {
311 *charsWritten = 0;
312 }
313 return URI_ERROR_TOSTRING_TOO_LONG;
314 }
315 } else {
316 (*charsRequired) += 1;
317 }
318 }
319 }
320
321 if (dest != NULL) {
322 if (written + 1 <= maxChars) {
323 memcpy(dest + written, _UT("]"), 1 * sizeof(URI_CHAR));
324 written += 1;
325 } else {
326 dest[0] = _UT('\0');
327 if (charsWritten != NULL) {
328 *charsWritten = 0;
329 }
330 return URI_ERROR_TOSTRING_TOO_LONG;
331 }
332 } else {
333 (*charsRequired) += 1;
334 }
335 } else if (uri->hostData.ipFuture.first != NULL) {
336 /* IPvFuture */
337 const int charsToWrite = (int)(uri->hostData.ipFuture.afterLast
338 - uri->hostData.ipFuture.first);
339 if (dest != NULL) {
340 if (written + 1 <= maxChars) {
341 memcpy(dest + written, _UT("["), 1 * sizeof(URI_CHAR));
342 written += 1;
343 } else {
344 dest[0] = _UT('\0');
345 if (charsWritten != NULL) {
346 *charsWritten = 0;
347 }
348 return URI_ERROR_TOSTRING_TOO_LONG;
349 }
350
351 if (written + charsToWrite <= maxChars) {
352 memcpy(dest + written, uri->hostData.ipFuture.first,
353 charsToWrite * sizeof(URI_CHAR));
354 written += charsToWrite;
355 } else {
356 dest[0] = _UT('\0');
357 if (charsWritten != NULL) {
358 *charsWritten = 0;
359 }
360 return URI_ERROR_TOSTRING_TOO_LONG;
361 }
362
363 if (written + 1 <= maxChars) {
364 memcpy(dest + written, _UT("]"), 1 * sizeof(URI_CHAR));
365 written += 1;
366 } else {
367 dest[0] = _UT('\0');
368 if (charsWritten != NULL) {
369 *charsWritten = 0;
370 }
371 return URI_ERROR_TOSTRING_TOO_LONG;
372 }
373 } else {
374 (*charsRequired) += 1 + charsToWrite + 1;
375 }
376 } else if (uri->hostText.first != NULL) {
377 /* Regname */
378 const int charsToWrite =
379 (int)(uri->hostText.afterLast - uri->hostText.first);
380 if (dest != NULL) {
381 if (written + charsToWrite <= maxChars) {
382 memcpy(dest + written, uri->hostText.first,
383 charsToWrite * sizeof(URI_CHAR));
384 written += charsToWrite;
385 } else {
386 dest[0] = _UT('\0');
387 if (charsWritten != NULL) {
388 *charsWritten = 0;
389 }
390 return URI_ERROR_TOSTRING_TOO_LONG;
391 }
392 } else {
393 (*charsRequired) += charsToWrite;
394 }
395 }
396
397 /* Port */
398 if (uri->portText.first != NULL) {
399 const int charsToWrite =
400 (int)(uri->portText.afterLast - uri->portText.first);
401 if (dest != NULL) {
402 /* Leading ':' */
403 if (written + 1 <= maxChars) {
404 memcpy(dest + written, _UT(":"), 1 * sizeof(URI_CHAR));
405 written += 1;
406 } else {
407 dest[0] = _UT('\0');
408 if (charsWritten != NULL) {
409 *charsWritten = 0;
410 }
411 return URI_ERROR_TOSTRING_TOO_LONG;
412 }
413
414 /* Port number */
415 if (written + charsToWrite <= maxChars) {
416 memcpy(dest + written, uri->portText.first,
417 charsToWrite * sizeof(URI_CHAR));
418 written += charsToWrite;
419 } else {
420 dest[0] = _UT('\0');
421 if (charsWritten != NULL) {
422 *charsWritten = 0;
423 }
424 return URI_ERROR_TOSTRING_TOO_LONG;
425 }
426 } else {
427 (*charsRequired) += 1 + charsToWrite;
428 }
429 }
430 /* clang-format off */
431 /* [09/19] endif; */
432 /* clang-format on */
433 }
434 /* clang-format off */
435 /* [10/19] append path to result; */
436 /* clang-format on */
437 /* Slash needed here? */
438 if (uri->absolutePath
439 || ((uri->pathHead != NULL) && URI_FUNC(HasHost)(uri))) {
440 if (dest != NULL) {
441 if (written + 1 <= maxChars) {
442 memcpy(dest + written, _UT("/"), 1 * sizeof(URI_CHAR));
443 written += 1;
444 } else {
445 dest[0] = _UT('\0');
446 if (charsWritten != NULL) {
447 *charsWritten = 0;
448 }
449 return URI_ERROR_TOSTRING_TOO_LONG;
450 }
451 } else {
452 (*charsRequired) += 1;
453 }
454 }
455
456 if (uri->pathHead != NULL) {
457 URI_TYPE(PathSegment) * walker = uri->pathHead;
458 do {
459 const int charsToWrite =
460 (int)(walker->text.afterLast - walker->text.first);
461 if (dest != NULL) {
462 if (written + charsToWrite <= maxChars) {
463 memcpy(dest + written, walker->text.first,
464 charsToWrite * sizeof(URI_CHAR));
465 written += charsToWrite;
466 } else {
467 dest[0] = _UT('\0');
468 if (charsWritten != NULL) {
469 *charsWritten = 0;
470 }
471 return URI_ERROR_TOSTRING_TOO_LONG;
472 }
473 } else {
474 (*charsRequired) += charsToWrite;
475 }
476
477 /* Not last segment -> append slash */
478 if (walker->next != NULL) {
479 if (dest != NULL) {
480 if (written + 1 <= maxChars) {
481 memcpy(dest + written, _UT("/"),
482 1 * sizeof(URI_CHAR));
483 written += 1;
484 } else {
485 dest[0] = _UT('\0');
486 if (charsWritten != NULL) {
487 *charsWritten = 0;
488 }
489 return URI_ERROR_TOSTRING_TOO_LONG;
490 }
491 } else {
492 (*charsRequired) += 1;
493 }
494 }
495
496 walker = walker->next;
497 } while (walker != NULL);
498 }
499 /* clang-format off */
500 /* [11/19] if defined(query) then */
501 /* clang-format on */
502 if (uri->query.first != NULL) {
503 /* clang-format off */
504 /* [12/19] append "?" to result; */
505 /* clang-format on */
506 if (dest != NULL) {
507 if (written + 1 <= maxChars) {
508 memcpy(dest + written, _UT("?"), 1 * sizeof(URI_CHAR));
509 written += 1;
510 } else {
511 dest[0] = _UT('\0');
512 if (charsWritten != NULL) {
513 *charsWritten = 0;
514 }
515 return URI_ERROR_TOSTRING_TOO_LONG;
516 }
517 } else {
518 (*charsRequired) += 1;
519 }
520 /* clang-format off */
521 /* [13/19] append query to result; */
522 /* clang-format on */
523 const int charsToWrite =
524 (int)(uri->query.afterLast - uri->query.first);
525 if (dest != NULL) {
526 if (written + charsToWrite <= maxChars) {
527 memcpy(dest + written, uri->query.first,
528 charsToWrite * sizeof(URI_CHAR));
529 written += charsToWrite;
530 } else {
531 dest[0] = _UT('\0');
532 if (charsWritten != NULL) {
533 *charsWritten = 0;
534 }
535 return URI_ERROR_TOSTRING_TOO_LONG;
536 }
537 } else {
538 (*charsRequired) += charsToWrite;
539 }
540 /* clang-format off */
541 /* [14/19] endif; */
542 /* clang-format on */
543 }
544 /* clang-format off */
545 /* [15/19] if defined(fragment) then */
546 /* clang-format on */
547 if (uri->fragment.first != NULL) {
548 /* clang-format off */
549 /* [16/19] append "#" to result; */
550 /* clang-format on */
551 if (dest != NULL) {
552 if (written + 1 <= maxChars) {
553 memcpy(dest + written, _UT("#"), 1 * sizeof(URI_CHAR));
554 written += 1;
555 } else {
556 dest[0] = _UT('\0');
557 if (charsWritten != NULL) {
558 *charsWritten = 0;
559 }
560 return URI_ERROR_TOSTRING_TOO_LONG;
561 }
562 } else {
563 (*charsRequired) += 1;
564 }
565 /* clang-format off */
566 /* [17/19] append fragment to result; */
567 /* clang-format on */
568 const int charsToWrite =
569 (int)(uri->fragment.afterLast - uri->fragment.first);
570 if (dest != NULL) {
571 if (written + charsToWrite <= maxChars) {
572 memcpy(dest + written, uri->fragment.first,
573 charsToWrite * sizeof(URI_CHAR));
574 written += charsToWrite;
575 } else {
576 dest[0] = _UT('\0');
577 if (charsWritten != NULL) {
578 *charsWritten = 0;
579 }
580 return URI_ERROR_TOSTRING_TOO_LONG;
581 }
582 } else {
583 (*charsRequired) += charsToWrite;
584 }
585 /* clang-format off */
586 /* [18/19] endif; */
587 /* clang-format on */
588 }
589 /* clang-format off */
590 /* [19/19] return result; */
591 /* clang-format on */
592 if (dest != NULL) {
593 dest[written++] = _UT('\0');
594 if (charsWritten != NULL) {
595 *charsWritten = written;
596 }
597 }
598 return URI_SUCCESS;
599 }
600 }
601 }
602 }
603
604 #endif