Debian Codegraph @

components/playlist/xspf/uriparser/UriResolve.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 "UriResolve.c"
47 # undef URI_PASS_ANSI
48 # endif
49 # ifdef URI_ENABLE_UNICODE
50 # define URI_PASS_UNICODE 1
51 # include "UriResolve.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 /* Appends a relative URI to an absolute. The last path segment of
69 * the absolute URI is replaced. */
70 static URI_INLINE UriBool URI_FUNC(MergePath)(URI_TYPE(Uri) * absWork,
71 const URI_TYPE(Uri) * relAppend,
72 UriMemoryManager * memory) {
73 URI_TYPE(PathSegment) * sourceWalker;
74 URI_TYPE(PathSegment) * destPrev;
75 if (relAppend->pathHead == NULL) {
76 return URI_TRUE;
77 }
78
79 /* Replace last segment ("" if trailing slash) with first of append chain */
80 if (absWork->pathHead == NULL) {
81 URI_TYPE(PathSegment) * const dup =
82 memory->malloc(memory, sizeof(URI_TYPE(PathSegment)));
83 if (dup == NULL) {
84 return URI_FALSE; /* Raises malloc error */
85 }
86 dup->next = NULL;
87 absWork->pathHead = dup;
88 absWork->pathTail = dup;
89 }
90 absWork->pathTail->text.first = relAppend->pathHead->text.first;
91 absWork->pathTail->text.afterLast = relAppend->pathHead->text.afterLast;
92
93 /* Append all the others */
94 sourceWalker = relAppend->pathHead->next;
95 if (sourceWalker == NULL) {
96 return URI_TRUE;
97 }
98 destPrev = absWork->pathTail;
99
100 for (;;) {
101 URI_TYPE(PathSegment) * const dup =
102 memory->malloc(memory, sizeof(URI_TYPE(PathSegment)));
103 if (dup == NULL) {
104 destPrev->next = NULL;
105 absWork->pathTail = destPrev;
106 return URI_FALSE; /* Raises malloc error */
107 }
108 dup->text = sourceWalker->text;
109 destPrev->next = dup;
110
111 if (sourceWalker->next == NULL) {
112 absWork->pathTail = dup;
113 absWork->pathTail->next = NULL;
114 break;
115 }
116 destPrev = dup;
117 sourceWalker = sourceWalker->next;
118 }
119
120 return URI_TRUE;
121 }
122
123 static int URI_FUNC(ResolveAbsolutePathFlag)(URI_TYPE(Uri) * absWork,
124 UriMemoryManager * memory) {
125 if (absWork == NULL) {
126 return URI_ERROR_NULL;
127 }
128
129 if (URI_FUNC(HasHost)(absWork) && absWork->absolutePath) {
130 /* Empty segment needed, instead? */
131 if (absWork->pathHead == NULL) {
132 URI_TYPE(PathSegment) * const segment =
133 memory->malloc(memory, sizeof(URI_TYPE(PathSegment)));
134 if (segment == NULL) {
135 return URI_ERROR_MALLOC;
136 }
137 segment->text.first = URI_FUNC(SafeToPointTo);
138 segment->text.afterLast = URI_FUNC(SafeToPointTo);
139 segment->next = NULL;
140
141 absWork->pathHead = segment;
142 absWork->pathTail = segment;
143 }
144
145 absWork->absolutePath = URI_FALSE;
146 }
147
148 return URI_SUCCESS;
149 }
150
151 static int URI_FUNC(AddBaseUriImpl)(URI_TYPE(Uri) * absDest,
152 const URI_TYPE(Uri) * relSource,
153 const URI_TYPE(Uri) * absBase,
154 UriResolutionOptions options,
155 UriMemoryManager * memory) {
156 UriBool relSourceHasScheme;
157
158 if (absDest == NULL) {
159 return URI_ERROR_NULL;
160 }
161 URI_FUNC(ResetUri)(absDest);
162
163 if ((relSource == NULL) || (absBase == NULL)) {
164 return URI_ERROR_NULL;
165 }
166
167 /* absBase absolute? */
168 if (absBase->scheme.first == NULL) {
169 return URI_ERROR_ADDBASE_REL_BASE;
170 }
171
172 /* NOTE: The curly brackets here force deeper indent (and that's all) */
173 {
174 {
175 {
176 /* clang-format off */
177 /* [00/32] -- A non-strict parser may ignore a scheme in the reference */
178 /* [00/32] -- if it is identical to the base URI's scheme. */
179 /* [00/32] if ((not strict) and (R.scheme == Base.scheme)) then */
180 /* clang-format on */
181 relSourceHasScheme =
182 (relSource->scheme.first != NULL) ? URI_TRUE : URI_FALSE;
183 if ((options & URI_RESOLVE_IDENTICAL_SCHEME_COMPAT)
184 && (absBase->scheme.first != NULL)
185 && (relSource->scheme.first != NULL)
186 && (0
187 == URI_FUNC(CompareRange)(&(absBase->scheme),
188 &(relSource->scheme)))) {
189 /* clang-format off */
190 /* [00/32] undefine(R.scheme); */
191 /* clang-format on */
192 relSourceHasScheme = URI_FALSE;
193 /* clang-format off */
194 /* [00/32] endif; */
195 /* clang-format on */
196 }
197
198 /* clang-format off */
199 /* [01/32] if defined(R.scheme) then */
200 /* clang-format on */
201 if (relSourceHasScheme) {
202 /* clang-format off */
203 /* [02/32] T.scheme = R.scheme; */
204 /* clang-format on */
205 absDest->scheme = relSource->scheme;
206 /* clang-format off */
207 /* [03/32] T.authority = R.authority; */
208 /* clang-format on */
209 if (!URI_FUNC(CopyAuthority)(absDest, relSource, memory)) {
210 return URI_ERROR_MALLOC;
211 }
212 /* clang-format off */
213 /* [04/32] T.path = remove_dot_segments(R.path); */
214 /* clang-format on */
215 if (!URI_FUNC(CopyPath)(absDest, relSource, memory)) {
216 return URI_ERROR_MALLOC;
217 }
218 if (!URI_FUNC(RemoveDotSegmentsAbsolute)(absDest, memory)) {
219 return URI_ERROR_MALLOC;
220 }
221 /* clang-format off */
222 /* [05/32] T.query = R.query; */
223 /* clang-format on */
224 absDest->query = relSource->query;
225 /* clang-format off */
226 /* [06/32] else */
227 /* clang-format on */
228 } else {
229 /* clang-format off */
230 /* [07/32] if defined(R.authority) then */
231 /* clang-format on */
232 if (URI_FUNC(HasHost)(relSource)) {
233 /* clang-format off */
234 /* [08/32] T.authority = R.authority; */
235 /* clang-format on */
236 if (!URI_FUNC(CopyAuthority)(absDest, relSource, memory)) {
237 return URI_ERROR_MALLOC;
238 }
239 /* clang-format off */
240 /* [09/32] T.path = remove_dot_segments(R.path); */
241 /* clang-format on */
242 if (!URI_FUNC(CopyPath)(absDest, relSource, memory)) {
243 return URI_ERROR_MALLOC;
244 }
245 if (!URI_FUNC(RemoveDotSegmentsAbsolute)(absDest, memory)) {
246 return URI_ERROR_MALLOC;
247 }
248 /* clang-format off */
249 /* [10/32] T.query = R.query; */
250 /* clang-format on */
251 absDest->query = relSource->query;
252 /* clang-format off */
253 /* [11/32] else */
254 /* clang-format on */
255 } else {
256 /* clang-format off */
257 /* [28/32] T.authority = Base.authority; */
258 /* clang-format on */
259 if (!URI_FUNC(CopyAuthority)(absDest, absBase, memory)) {
260 return URI_ERROR_MALLOC;
261 }
262 /* clang-format off */
263 /* [12/32] if (R.path == "") then */
264 /* clang-format on */
265 if (relSource->pathHead == NULL && !relSource->absolutePath) {
266 /* clang-format off */
267 /* [13/32] T.path = Base.path; */
268 /* clang-format on */
269 if (!URI_FUNC(CopyPath)(absDest, absBase, memory)) {
270 return URI_ERROR_MALLOC;
271 }
272 /* clang-format off */
273 /* [14/32] if defined(R.query) then */
274 /* clang-format on */
275 if (relSource->query.first != NULL) {
276 /* clang-format off */
277 /* [15/32] T.query = R.query; */
278 /* clang-format on */
279 absDest->query = relSource->query;
280 /* clang-format off */
281 /* [16/32] else */
282 /* clang-format on */
283 } else {
284 /* clang-format off */
285 /* [17/32] T.query = Base.query; */
286 /* clang-format on */
287 absDest->query = absBase->query;
288 /* clang-format off */
289 /* [18/32] endif; */
290 /* clang-format on */
291 }
292 /* clang-format off */
293 /* [19/32] else */
294 /* clang-format on */
295 } else {
296 /* clang-format off */
297 /* [20/32] if (R.path starts-with "/") then */
298 /* clang-format on */
299 if (relSource->absolutePath) {
300 int res;
301 /* clang-format off */
302 /* [21/32] T.path = remove_dot_segments(R.path); */
303 /* clang-format on */
304 if (!URI_FUNC(CopyPath)(absDest, relSource, memory)) {
305 return URI_ERROR_MALLOC;
306 }
307 res = URI_FUNC(ResolveAbsolutePathFlag)(absDest, memory);
308 if (res != URI_SUCCESS) {
309 return res;
310 }
311 if (!URI_FUNC(RemoveDotSegmentsAbsolute)(absDest,
312 memory)) {
313 return URI_ERROR_MALLOC;
314 }
315 /* clang-format off */
316 /* [22/32] else */
317 /* clang-format on */
318 } else {
319 /* clang-format off */
320 /* [23/32] T.path = merge(Base.path, R.path); */
321 /* clang-format on */
322 if (!URI_FUNC(CopyPath)(absDest, absBase, memory)) {
323 return URI_ERROR_MALLOC;
324 }
325 if (!URI_FUNC(MergePath)(absDest, relSource, memory)) {
326 return URI_ERROR_MALLOC;
327 }
328 /* clang-format off */
329 /* [24/32] T.path = remove_dot_segments(T.path); */
330 /* clang-format on */
331 if (!URI_FUNC(RemoveDotSegmentsAbsolute)(absDest,
332 memory)) {
333 return URI_ERROR_MALLOC;
334 }
335
336 if (!URI_FUNC(FixAmbiguity)(absDest, memory)) {
337 return URI_ERROR_MALLOC;
338 }
339 /* clang-format off */
340 /* [25/32] endif; */
341 }
342 /* clang-format off */
343 /* [26/32] T.query = R.query; */
344 /* clang-format on */
345 absDest->query = relSource->query;
346 /* clang-format off */
347 /* [27/32] endif; */
348 /* clang-format on */
349 }
350 URI_FUNC(FixEmptyTrailSegment)(absDest, memory);
351 /* clang-format off */
352 /* [29/32] endif; */
353 /* clang-format on */
354 }
355 /* clang-format off */
356 /* [30/32] T.scheme = Base.scheme; */
357 /* clang-format on */
358 absDest->scheme = absBase->scheme;
359 /* clang-format off */
360 /* [31/32] endif; */
361 /* clang-format on */
362 }
363 /* clang-format off */
364 /* [32/32] T.fragment = R.fragment; */
365 /* clang-format on */
366 absDest->fragment = relSource->fragment;
367 }
368 }
369 }
370 return URI_SUCCESS;
371 }
372
373 int URI_FUNC(AddBaseUri)(URI_TYPE(Uri) * absDest, const URI_TYPE(Uri) * relSource,
374 const URI_TYPE(Uri) * absBase) {
375 const UriResolutionOptions options = URI_RESOLVE_STRICTLY;
376 return URI_FUNC(AddBaseUriEx)(absDest, relSource, absBase, options);
377 }
378
379 int URI_FUNC(AddBaseUriEx)(URI_TYPE(Uri) * absDest, const URI_TYPE(Uri) * relSource,
380 const URI_TYPE(Uri) * absBase, UriResolutionOptions options) {
381 return URI_FUNC(AddBaseUriExMm)(absDest, relSource, absBase, options, NULL);
382 }
383
384 int URI_FUNC(AddBaseUriExMm)(URI_TYPE(Uri) * absDest, const URI_TYPE(Uri) * relSource,
385 const URI_TYPE(Uri) * absBase, UriResolutionOptions options,
386 UriMemoryManager * memory) {
387 int res;
388
389 URI_CHECK_MEMORY_MANAGER(memory); /* may return */
390
391 res = URI_FUNC(AddBaseUriImpl)(absDest, relSource, absBase, options, memory);
392 if ((res != URI_SUCCESS) && (absDest != NULL)) {
393 URI_FUNC(FreeUriMembersMm)(absDest, memory);
394 }
395 return res;
396 }
397
398 #endif