Debian Codegraph @

components/playlist/xspf/uriparser/UriQuery.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 "UriQuery.c"
47 # undef URI_PASS_ANSI
48 # endif
49 # ifdef URI_ENABLE_UNICODE
50 # define URI_PASS_UNICODE 1
51 # include "UriQuery.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 # include <limits.h>
69 # include <stddef.h> /* size_t */
70
71 static int URI_FUNC(ComposeQueryEngine)(URI_CHAR * dest,
72 const URI_TYPE(QueryList) * queryList,
73 int maxChars, int * charsWritten,
74 int * charsRequired, UriBool spaceToPlus,
75 UriBool normalizeBreaks);
76
77 static UriBool URI_FUNC(AppendQueryItem)(
78 URI_TYPE(QueryList) * *prevNext, int * itemCount, const URI_CHAR * keyFirst,
79 const URI_CHAR * keyAfter, const URI_CHAR * valueFirst, const URI_CHAR * valueAfter,
80 UriBool plusToSpace, UriBreakConversion breakConversion, UriMemoryManager * memory);
81
82 int URI_FUNC(ComposeQueryCharsRequired)(const URI_TYPE(QueryList) * queryList,
83 int * charsRequired) {
84 const UriBool spaceToPlus = URI_TRUE;
85 const UriBool normalizeBreaks = URI_TRUE;
86
87 return URI_FUNC(ComposeQueryCharsRequiredEx)(queryList, charsRequired, spaceToPlus,
88 normalizeBreaks);
89 }
90
91 int URI_FUNC(ComposeQueryCharsRequiredEx)(const URI_TYPE(QueryList) * queryList,
92 int * charsRequired, UriBool spaceToPlus,
93 UriBool normalizeBreaks) {
94 if ((queryList == NULL) || (charsRequired == NULL)) {
95 return URI_ERROR_NULL;
96 }
97
98 return URI_FUNC(ComposeQueryEngine)(NULL, queryList, 0, NULL, charsRequired,
99 spaceToPlus, normalizeBreaks);
100 }
101
102 int URI_FUNC(ComposeQuery)(URI_CHAR * dest, const URI_TYPE(QueryList) * queryList,
103 int maxChars, int * charsWritten) {
104 const UriBool spaceToPlus = URI_TRUE;
105 const UriBool normalizeBreaks = URI_TRUE;
106
107 return URI_FUNC(ComposeQueryEx)(dest, queryList, maxChars, charsWritten, spaceToPlus,
108 normalizeBreaks);
109 }
110
111 int URI_FUNC(ComposeQueryEx)(URI_CHAR * dest, const URI_TYPE(QueryList) * queryList,
112 int maxChars, int * charsWritten, UriBool spaceToPlus,
113 UriBool normalizeBreaks) {
114 if ((dest == NULL) || (queryList == NULL)) {
115 return URI_ERROR_NULL;
116 }
117
118 if (maxChars < 1) {
119 return URI_ERROR_OUTPUT_TOO_LARGE;
120 }
121
122 return URI_FUNC(ComposeQueryEngine)(dest, queryList, maxChars, charsWritten, NULL,
123 spaceToPlus, normalizeBreaks);
124 }
125
126 int URI_FUNC(ComposeQueryMalloc)(URI_CHAR ** dest,
127 const URI_TYPE(QueryList) * queryList) {
128 const UriBool spaceToPlus = URI_TRUE;
129 const UriBool normalizeBreaks = URI_TRUE;
130
131 return URI_FUNC(ComposeQueryMallocEx)(dest, queryList, spaceToPlus, normalizeBreaks);
132 }
133
134 int URI_FUNC(ComposeQueryMallocEx)(URI_CHAR ** dest,
135 const URI_TYPE(QueryList) * queryList,
136 UriBool spaceToPlus, UriBool normalizeBreaks) {
137 return URI_FUNC(ComposeQueryMallocExMm)(dest, queryList, spaceToPlus, normalizeBreaks,
138 NULL);
139 }
140
141 int URI_FUNC(ComposeQueryMallocExMm)(URI_CHAR ** dest,
142 const URI_TYPE(QueryList) * queryList,
143 UriBool spaceToPlus, UriBool normalizeBreaks,
144 UriMemoryManager * memory) {
145 int charsRequired;
146 int res;
147 URI_CHAR * queryString;
148
149 if (dest == NULL) {
150 return URI_ERROR_NULL;
151 }
152
153 URI_CHECK_MEMORY_MANAGER(memory); /* may return */
154
155 /* Calculate space */
156 res = URI_FUNC(ComposeQueryCharsRequiredEx)(queryList, &charsRequired, spaceToPlus,
157 normalizeBreaks);
158 if (res != URI_SUCCESS) {
159 return res;
160 }
161 if (charsRequired == INT_MAX) {
162 return URI_ERROR_MALLOC;
163 }
164 charsRequired++;
165
166 /* Allocate space */
167 queryString = memory->calloc(memory, charsRequired, sizeof(URI_CHAR));
168 if (queryString == NULL) {
169 return URI_ERROR_MALLOC;
170 }
171
172 /* Put query in */
173 res = URI_FUNC(ComposeQueryEx)(queryString, queryList, charsRequired, NULL,
174 spaceToPlus, normalizeBreaks);
175 if (res != URI_SUCCESS) {
176 memory->free(memory, queryString);
177 return res;
178 }
179
180 *dest = queryString;
181 return URI_SUCCESS;
182 }
183
184 int URI_FUNC(ComposeQueryEngine)(URI_CHAR * dest, const URI_TYPE(QueryList) * queryList,
185 int maxChars, int * charsWritten, int * charsRequired,
186 UriBool spaceToPlus, UriBool normalizeBreaks) {
187 UriBool firstItem = URI_TRUE;
188 int ampersandLen = 0; /* increased to 1 from second item on */
189 URI_CHAR * write = dest;
190
191 /* Subtract terminator */
192 if (dest == NULL) {
193 *charsRequired = 0;
194 } else {
195 maxChars--;
196 }
197
198 while (queryList != NULL) {
199 const URI_CHAR * const key = queryList->key;
200 const URI_CHAR * const value = queryList->value;
201 const int worstCase = (normalizeBreaks == URI_TRUE ? 6 : 3);
202 const size_t keyLen = (key == NULL) ? 0 : URI_STRLEN(key);
203 int keyRequiredChars;
204 const size_t valueLen = (value == NULL) ? 0 : URI_STRLEN(value);
205 int valueRequiredChars;
206
207 if ((keyLen >= (size_t)INT_MAX / worstCase)
208 || (valueLen >= (size_t)INT_MAX / worstCase)) {
209 return URI_ERROR_OUTPUT_TOO_LARGE;
210 }
211 keyRequiredChars = worstCase * (int)keyLen;
212 valueRequiredChars = worstCase * (int)valueLen;
213
214 if (dest == NULL) {
215 (*charsRequired) += ampersandLen + keyRequiredChars
216 + ((value == NULL) ? 0 : 1 + valueRequiredChars);
217
218 if (firstItem == URI_TRUE) {
219 ampersandLen = 1;
220 firstItem = URI_FALSE;
221 }
222 } else {
223 if ((write - dest) + ampersandLen + keyRequiredChars > maxChars) {
224 return URI_ERROR_OUTPUT_TOO_LARGE;
225 }
226
227 /* Copy key */
228 if (firstItem == URI_TRUE) {
229 ampersandLen = 1;
230 firstItem = URI_FALSE;
231 } else {
232 write[0] = _UT('&');
233 write++;
234 }
235 write = URI_FUNC(EscapeEx)(key, key + keyLen, write, spaceToPlus,
236 normalizeBreaks);
237
238 if (value != NULL) {
239 if ((write - dest) + 1 + valueRequiredChars > maxChars) {
240 return URI_ERROR_OUTPUT_TOO_LARGE;
241 }
242
243 /* Copy value */
244 write[0] = _UT('=');
245 write++;
246 write = URI_FUNC(EscapeEx)(value, value + valueLen, write, spaceToPlus,
247 normalizeBreaks);
248 }
249 }
250
251 queryList = queryList->next;
252 }
253
254 if (dest != NULL) {
255 write[0] = _UT('\0');
256 if (charsWritten != NULL) {
257 *charsWritten = (int)(write - dest) + 1; /* .. for terminator */
258 }
259 }
260
261 return URI_SUCCESS;
262 }
263
264 UriBool URI_FUNC(AppendQueryItem)(URI_TYPE(QueryList) * *prevNext, int * itemCount,
265 const URI_CHAR * keyFirst, const URI_CHAR * keyAfter,
266 const URI_CHAR * valueFirst,
267 const URI_CHAR * valueAfter, UriBool plusToSpace,
268 UriBreakConversion breakConversion,
269 UriMemoryManager * memory) {
270 const int keyLen = (int)(keyAfter - keyFirst);
271 const int valueLen = (int)(valueAfter - valueFirst);
272 URI_CHAR * key;
273 URI_CHAR * value;
274
275 if ((prevNext == NULL) || (itemCount == NULL) || (keyFirst == NULL)
276 || (keyAfter == NULL) || (keyFirst > keyAfter) || (valueFirst > valueAfter)
277 || ((keyFirst == keyAfter) && (valueFirst == NULL) && (valueAfter == NULL))) {
278 return URI_TRUE;
279 }
280
281 /* Append new empty item */
282 *prevNext = memory->malloc(memory, 1 * sizeof(URI_TYPE(QueryList)));
283 if (*prevNext == NULL) {
284 return URI_FALSE; /* Raises malloc error */
285 }
286 (*prevNext)->next = NULL;
287
288 /* Fill key */
289 key = memory->malloc(memory, (keyLen + 1) * sizeof(URI_CHAR));
290 if (key == NULL) {
291 memory->free(memory, *prevNext);
292 *prevNext = NULL;
293 return URI_FALSE; /* Raises malloc error */
294 }
295
296 key[keyLen] = _UT('\0');
297 if (keyLen > 0) {
298 /* Copy 1:1 */
299 memcpy(key, keyFirst, keyLen * sizeof(URI_CHAR));
300
301 /* Unescape */
302 URI_FUNC(UnescapeInPlaceEx)(key, plusToSpace, breakConversion);
303 }
304 (*prevNext)->key = key;
305
306 /* Fill value */
307 if (valueFirst != NULL) {
308 value = memory->malloc(memory, (valueLen + 1) * sizeof(URI_CHAR));
309 if (value == NULL) {
310 memory->free(memory, key);
311 memory->free(memory, *prevNext);
312 *prevNext = NULL;
313 return URI_FALSE; /* Raises malloc error */
314 }
315
316 value[valueLen] = _UT('\0');
317 if (valueLen > 0) {
318 /* Copy 1:1 */
319 memcpy(value, valueFirst, valueLen * sizeof(URI_CHAR));
320
321 /* Unescape */
322 URI_FUNC(UnescapeInPlaceEx)(value, plusToSpace, breakConversion);
323 }
324 (*prevNext)->value = value;
325 } else {
326 value = NULL;
327 }
328 (*prevNext)->value = value;
329
330 (*itemCount)++;
331 return URI_TRUE;
332 }
333
334 void URI_FUNC(FreeQueryList)(URI_TYPE(QueryList) * queryList) {
335 URI_FUNC(FreeQueryListMm)(queryList, NULL);
336 }
337
338 int URI_FUNC(FreeQueryListMm)(URI_TYPE(QueryList) * queryList,
339 UriMemoryManager * memory) {
340 URI_CHECK_MEMORY_MANAGER(memory); /* may return */
341 while (queryList != NULL) {
342 URI_TYPE(QueryList) * nextBackup = queryList->next;
343 memory->free(memory, (URI_CHAR *)queryList->key); /* const cast */
344 memory->free(memory, (URI_CHAR *)queryList->value); /* const cast */
345 memory->free(memory, queryList);
346 queryList = nextBackup;
347 }
348 return URI_SUCCESS;
349 }
350
351 int URI_FUNC(DissectQueryMalloc)(URI_TYPE(QueryList) * *dest, int * itemCount,
352 const URI_CHAR * first, const URI_CHAR * afterLast) {
353 const UriBool plusToSpace = URI_TRUE;
354 const UriBreakConversion breakConversion = URI_BR_DONT_TOUCH;
355
356 return URI_FUNC(DissectQueryMallocEx)(dest, itemCount, first, afterLast, plusToSpace,
357 breakConversion);
358 }
359
360 int URI_FUNC(DissectQueryMallocEx)(URI_TYPE(QueryList) * *dest, int * itemCount,
361 const URI_CHAR * first, const URI_CHAR * afterLast,
362 UriBool plusToSpace,
363 UriBreakConversion breakConversion) {
364 return URI_FUNC(DissectQueryMallocExMm)(dest, itemCount, first, afterLast,
365 plusToSpace, breakConversion, NULL);
366 }
367
368 int URI_FUNC(DissectQueryMallocExMm)(URI_TYPE(QueryList) * *dest, int * itemCount,
369 const URI_CHAR * first, const URI_CHAR * afterLast,
370 UriBool plusToSpace,
371 UriBreakConversion breakConversion,
372 UriMemoryManager * memory) {
373 const URI_CHAR * walk = first;
374 const URI_CHAR * keyFirst = first;
375 const URI_CHAR * keyAfter = NULL;
376 const URI_CHAR * valueFirst = NULL;
377 const URI_CHAR * valueAfter = NULL;
378 URI_TYPE(QueryList) ** prevNext = dest;
379 int nullCounter;
380 int * itemsAppended = (itemCount == NULL) ? &nullCounter : itemCount;
381
382 if ((dest == NULL) || (first == NULL) || (afterLast == NULL)) {
383 return URI_ERROR_NULL;
384 }
385
386 if (first > afterLast) {
387 return URI_ERROR_RANGE_INVALID;
388 }
389
390 URI_CHECK_MEMORY_MANAGER(memory); /* may return */
391
392 *dest = NULL;
393 *itemsAppended = 0;
394
395 /* Parse query string */
396 for (; walk < afterLast; walk++) {
397 switch (*walk) {
398 case _UT('&'):
399 if (valueFirst != NULL) {
400 valueAfter = walk;
401 } else {
402 keyAfter = walk;
403 }
404
405 if (URI_FUNC(AppendQueryItem)(prevNext, itemsAppended, keyFirst, keyAfter,
406 valueFirst, valueAfter, plusToSpace,
407 breakConversion, memory)
408 == URI_FALSE) {
409 /* Free list we built */
410 *itemsAppended = 0;
411 URI_FUNC(FreeQueryListMm)(*dest, memory);
412 return URI_ERROR_MALLOC;
413 }
414
415 /* Make future items children of the current */
416 if ((prevNext != NULL) && (*prevNext != NULL)) {
417 prevNext = &((*prevNext)->next);
418 }
419
420 if (walk + 1 < afterLast) {
421 keyFirst = walk + 1;
422 } else {
423 keyFirst = NULL;
424 }
425 keyAfter = NULL;
426 valueFirst = NULL;
427 valueAfter = NULL;
428 break;
429
430 case _UT('='):
431 /* NOTE: WE treat the first '=' as a separator, */
432 /* all following go into the value part */
433 if (keyAfter == NULL) {
434 keyAfter = walk;
435 if (walk + 1 <= afterLast) {
436 valueFirst = walk + 1;
437 valueAfter = walk + 1;
438 }
439 }
440 break;
441
442 default:
443 break;
444 }
445 }
446
447 if (valueFirst != NULL) {
448 /* Must be key/value pair */
449 valueAfter = walk;
450 } else {
451 /* Must be key only */
452 keyAfter = walk;
453 }
454
455 if (URI_FUNC(AppendQueryItem)(prevNext, itemsAppended, keyFirst, keyAfter, valueFirst,
456 valueAfter, plusToSpace, breakConversion, memory)
457 == URI_FALSE) {
458 /* Free list we built */
459 *itemsAppended = 0;
460 URI_FUNC(FreeQueryListMm)(*dest, memory);
461 return URI_ERROR_MALLOC;
462 }
463
464 return URI_SUCCESS;
465 }
466
467 #endif