Debian Codegraph @

components/playlist/xspf/uriparser/UriShorten.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 "UriShorten.c"
47 # undef URI_PASS_ANSI
48 # endif
49 # ifdef URI_ENABLE_UNICODE
50 # define URI_PASS_UNICODE 1
51 # include "UriShorten.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 "UriMemory.h"
66 # endif
67
68 static URI_INLINE UriBool URI_FUNC(AppendSegment)(URI_TYPE(Uri) * uri,
69 const URI_CHAR * first,
70 const URI_CHAR * afterLast,
71 UriMemoryManager * memory) {
72 /* Create segment */
73 URI_TYPE(PathSegment) * segment =
74 memory->malloc(memory, 1 * sizeof(URI_TYPE(PathSegment)));
75 if (segment == NULL) {
76 return URI_FALSE; /* Raises malloc error */
77 }
78 segment->next = NULL;
79 segment->text.first = first;
80 segment->text.afterLast = afterLast;
81
82 /* Put into chain */
83 if (uri->pathTail == NULL) {
84 uri->pathHead = segment;
85 } else {
86 uri->pathTail->next = segment;
87 }
88 uri->pathTail = segment;
89
90 return URI_TRUE;
91 }
92
93 static URI_INLINE UriBool URI_FUNC(EqualsAuthority)(const URI_TYPE(Uri) * first,
94 const URI_TYPE(Uri) * second) {
95 /* IPv4 */
96 if (first->hostData.ip4 != NULL) {
97 return ((second->hostData.ip4 != NULL)
98 && !memcmp(first->hostData.ip4->data, second->hostData.ip4->data, 4))
99 ? URI_TRUE
100 : URI_FALSE;
101 }
102
103 /* IPv6 */
104 if (first->hostData.ip6 != NULL) {
105 return ((second->hostData.ip6 != NULL)
106 && !memcmp(first->hostData.ip6->data, second->hostData.ip6->data, 16))
107 ? URI_TRUE
108 : URI_FALSE;
109 }
110
111 /* IPvFuture */
112 if (first->hostData.ipFuture.first != NULL) {
113 return ((second->hostData.ipFuture.first != NULL)
114 && !URI_FUNC(CompareRange)(&first->hostData.ipFuture,
115 &second->hostData.ipFuture))
116 ? URI_TRUE
117 : URI_FALSE;
118 }
119
120 return !URI_FUNC(CompareRange)(&first->hostText, &second->hostText) ? URI_TRUE
121 : URI_FALSE;
122 }
123
124 static int URI_FUNC(RemoveBaseUriImpl)(URI_TYPE(Uri) * dest,
125 const URI_TYPE(Uri) * absSource,
126 const URI_TYPE(Uri) * absBase,
127 UriBool domainRootMode,
128 UriMemoryManager * memory) {
129 if (dest == NULL) {
130 return URI_ERROR_NULL;
131 }
132 URI_FUNC(ResetUri)(dest);
133
134 if ((absSource == NULL) || (absBase == NULL)) {
135 return URI_ERROR_NULL;
136 }
137
138 /* absBase absolute? */
139 if (absBase->scheme.first == NULL) {
140 return URI_ERROR_REMOVEBASE_REL_BASE;
141 }
142
143 /* absSource absolute? */
144 if (absSource->scheme.first == NULL) {
145 return URI_ERROR_REMOVEBASE_REL_SOURCE;
146 }
147
148 /* NOTE: The curly brackets here force deeper indent (and that's all) */
149 {
150 {
151 {
152 /* clang-format off */
153 /* [01/50] if (A.scheme != Base.scheme) then */
154 /* clang-format on */
155 if (URI_FUNC(CompareRange)(&absSource->scheme, &absBase->scheme)) {
156 /* clang-format off */
157 /* [02/50] T.scheme = A.scheme; */
158 /* clang-format on */
159 dest->scheme = absSource->scheme;
160 /* clang-format off */
161 /* [03/50] T.authority = A.authority; */
162 /* clang-format on */
163 if (!URI_FUNC(CopyAuthority)(dest, absSource, memory)) {
164 return URI_ERROR_MALLOC;
165 }
166 /* clang-format off */
167 /* [04/50] T.path = A.path; */
168 /* clang-format on */
169 if (!URI_FUNC(CopyPath)(dest, absSource, memory)) {
170 return URI_ERROR_MALLOC;
171 }
172 /* clang-format off */
173 /* [05/50] else */
174 /* clang-format on */
175 } else {
176 /* clang-format off */
177 /* [06/50] undef(T.scheme); */
178 /* clang-format on */
179 /* NOOP */
180 /* clang-format off */
181 /* [07/50] if (A.authority != Base.authority) then */
182 /* clang-format on */
183 if (!URI_FUNC(EqualsAuthority)(absSource, absBase)) {
184 /* clang-format off */
185 /* [08/50] T.authority = A.authority; */
186 /* clang-format on */
187 if (!URI_FUNC(CopyAuthority)(dest, absSource, memory)) {
188 return URI_ERROR_MALLOC;
189 }
190 /* clang-format off */
191 /* [09/50] T.path = A.path; */
192 /* clang-format on */
193 if (!URI_FUNC(CopyPath)(dest, absSource, memory)) {
194 return URI_ERROR_MALLOC;
195 }
196 /* clang-format off */
197 /* [10/50] else */
198 /* clang-format on */
199 } else {
200 /* clang-format off */
201 /* [11/50] if domainRootMode then */
202 /* clang-format on */
203 if (domainRootMode == URI_TRUE) {
204 /* clang-format off */
205 /* [12/50] undef(T.authority); */
206 /* clang-format on */
207 /* NOOP */
208 /* clang-format off */
209 /* [13/50] if (first(A.path) == "") then */
210 /* clang-format on */
211 /* GROUPED */
212 /* clang-format off */
213 /* [14/50] T.path = "/." + A.path; */
214 /* clang-format on */
215 /* GROUPED */
216 /* clang-format off */
217 /* [15/50] else */
218 /* clang-format on */
219 /* GROUPED */
220 /* clang-format off */
221 /* [16/50] T.path = A.path; */
222 /* clang-format on */
223 /* GROUPED */
224 /* clang-format off */
225 /* [17/50] endif; */
226 /* clang-format on */
227 if (!URI_FUNC(CopyPath)(dest, absSource, memory)) {
228 return URI_ERROR_MALLOC;
229 }
230 dest->absolutePath = URI_TRUE;
231
232 if (!URI_FUNC(FixAmbiguity)(dest, memory)) {
233 return URI_ERROR_MALLOC;
234 }
235 /* clang-format off */
236 /* [18/50] else */
237 /* clang-format on */
238 } else {
239 const URI_TYPE(PathSegment) * sourceSeg = absSource->pathHead;
240 const URI_TYPE(PathSegment) * baseSeg = absBase->pathHead;
241 /* clang-format off */
242 /* [19/50] bool pathNaked = true; */
243 /* clang-format on */
244 UriBool pathNaked = URI_TRUE;
245 /* clang-format off */
246 /* [20/50] undef(last(Base.path)); */
247 /* clang-format on */
248 /* NOOP */
249 /* clang-format off */
250 /* [21/50] T.path = ""; */
251 /* clang-format on */
252 dest->absolutePath = URI_FALSE;
253 /* clang-format off */
254 /* [22/50] while (first(A.path) == first(Base.path)) do */
255 /* clang-format on */
256 while (
257 (sourceSeg != NULL) && (baseSeg != NULL)
258 && !URI_FUNC(CompareRange)(&sourceSeg->text,
259 &baseSeg->text)
260 && !((sourceSeg->text.first == sourceSeg->text.afterLast)
261 && ((sourceSeg->next == NULL)
262 != (baseSeg->next == NULL)))) {
263 /* clang-format off */
264 /* [23/50] A.path++; */
265 /* clang-format on */
266 sourceSeg = sourceSeg->next;
267 /* clang-format off */
268 /* [24/50] Base.path++; */
269 /* clang-format on */
270 baseSeg = baseSeg->next;
271 /* clang-format off */
272 /* [25/50] endwhile; */
273 /* clang-format on */
274 }
275 /* clang-format off */
276 /* [26/50] while defined(first(Base.path)) do */
277 /* clang-format on */
278 while ((baseSeg != NULL) && (baseSeg->next != NULL)) {
279 /* clang-format off */
280 /* [27/50] Base.path++; */
281 /* clang-format on */
282 baseSeg = baseSeg->next;
283 /* clang-format off */
284 /* [28/50] T.path += "../"; */
285 /* clang-format on */
286 if (!URI_FUNC(AppendSegment)(dest, URI_FUNC(ConstParent),
287 URI_FUNC(ConstParent) + 2,
288 memory)) {
289 return URI_ERROR_MALLOC;
290 }
291 /* clang-format off */
292 /* [29/50] pathNaked = false; */
293 /* clang-format on */
294 pathNaked = URI_FALSE;
295 /* clang-format off */
296 /* [30/50] endwhile; */
297 /* clang-format on */
298 }
299 /* clang-format off */
300 /* [31/50] while defined(first(A.path)) do */
301 /* clang-format on */
302 while (sourceSeg != NULL) {
303 /* clang-format off */
304 /* [32/50] if pathNaked then */
305 /* clang-format on */
306 if (pathNaked == URI_TRUE) {
307 /* clang-format off */
308 /* [33/50] if (first(A.path) contains ":") then */
309 /* clang-format on */
310 UriBool containsColon = URI_FALSE;
311 const URI_CHAR * ch = sourceSeg->text.first;
312 for (; ch < sourceSeg->text.afterLast; ch++) {
313 if (*ch == _UT(':')) {
314 containsColon = URI_TRUE;
315 break;
316 }
317 }
318
319 if (containsColon) {
320 /* clang-format off */
321 /* [34/50] T.path += "./"; */
322 /* clang-format on */
323 if (!URI_FUNC(AppendSegment)(
324 dest, URI_FUNC(ConstPwd),
325 URI_FUNC(ConstPwd) + 1, memory)) {
326 return URI_ERROR_MALLOC;
327 }
328 /* clang-format off */
329 /* [35/50] elseif (first(A.path) == "") then */
330 /* clang-format on */
331 } else if (sourceSeg->text.first
332 == sourceSeg->text.afterLast) {
333 /* clang-format off */
334 /* [36/50] T.path += "/."; */
335 /* clang-format on */
336 if (!URI_FUNC(AppendSegment)(
337 dest, URI_FUNC(ConstPwd),
338 URI_FUNC(ConstPwd) + 1, memory)) {
339 return URI_ERROR_MALLOC;
340 }
341 /* clang-format off */
342 /* [37/50] endif; */
343 /* clang-format on */
344 }
345 /* clang-format off */
346 /* [38/50] endif; */
347 /* clang-format on */
348 }
349 /* clang-format off */
350 /* [39/50] T.path += first(A.path); */
351 /* clang-format on */
352 if (!URI_FUNC(AppendSegment)(dest, sourceSeg->text.first,
353 sourceSeg->text.afterLast,
354 memory)) {
355 return URI_ERROR_MALLOC;
356 }
357 /* clang-format off */
358 /* [40/50] pathNaked = false; */
359 /* clang-format on */
360 pathNaked = URI_FALSE;
361 /* clang-format off */
362 /* [41/50] A.path++; */
363 /* clang-format on */
364 sourceSeg = sourceSeg->next;
365 /* clang-format off */
366 /* [42/50] if defined(first(A.path)) then */
367 /* clang-format on */
368 /* NOOP */
369 /* clang-format off */
370 /* [43/50] T.path += + "/"; */
371 /* clang-format on */
372 /* NOOP */
373 /* clang-format off */
374 /* [44/50] endif; */
375 /* clang-format on */
376 /* NOOP */
377 /* clang-format off */
378 /* [45/50] endwhile; */
379 /* clang-format on */
380 }
381 /* clang-format off */
382 /* [46/50] endif; */
383 /* clang-format on */
384 }
385 /* clang-format off */
386 /* [47/50] endif; */
387 /* clang-format on */
388 }
389 /* clang-format off */
390 /* [48/50] endif; */
391 /* clang-format on */
392 }
393 /* clang-format off */
394 /* [49/50] T.query = A.query; */
395 /* clang-format on */
396 dest->query = absSource->query;
397 /* clang-format off */
398 /* [50/50] T.fragment = A.fragment; */
399 /* clang-format on */
400 dest->fragment = absSource->fragment;
401 }
402 }
403 }
404 return URI_SUCCESS;
405 }
406
407 int URI_FUNC(RemoveBaseUri)(URI_TYPE(Uri) * dest, const URI_TYPE(Uri) * absSource,
408 const URI_TYPE(Uri) * absBase, UriBool domainRootMode) {
409 return URI_FUNC(RemoveBaseUriMm)(dest, absSource, absBase, domainRootMode, NULL);
410 }
411
412 int URI_FUNC(RemoveBaseUriMm)(URI_TYPE(Uri) * dest, const URI_TYPE(Uri) * absSource,
413 const URI_TYPE(Uri) * absBase, UriBool domainRootMode,
414 UriMemoryManager * memory) {
415 int res;
416
417 URI_CHECK_MEMORY_MANAGER(memory); /* may return */
418
419 res = URI_FUNC(RemoveBaseUriImpl)(dest, absSource, absBase, domainRootMode, memory);
420 if ((res != URI_SUCCESS) && (dest != NULL)) {
421 URI_FUNC(FreeUriMembersMm)(dest, memory);
422 }
423 return res;
424 }
425
426 #endif