Debian Codegraph @

components/playlist/xspf/uriparser/UriEscape.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 "UriEscape.c"
47 # undef URI_PASS_ANSI
48 # endif
49 # ifdef URI_ENABLE_UNICODE
50 # define URI_PASS_UNICODE 1
51 # include "UriEscape.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 "UriSets.h"
66 # endif
67
68 URI_CHAR * URI_FUNC(Escape)(const URI_CHAR * in, URI_CHAR * out, UriBool spaceToPlus,
69 UriBool normalizeBreaks) {
70 return URI_FUNC(EscapeEx)(in, NULL, out, spaceToPlus, normalizeBreaks);
71 }
72
73 URI_CHAR * URI_FUNC(EscapeEx)(const URI_CHAR * inFirst, const URI_CHAR * inAfterLast,
74 URI_CHAR * out, UriBool spaceToPlus,
75 UriBool normalizeBreaks) {
76 const URI_CHAR * read = inFirst;
77 URI_CHAR * write = out;
78 UriBool prevWasCr = URI_FALSE;
79 if ((out == NULL) || (inFirst == out)) {
80 return NULL;
81 } else if (inFirst == NULL) {
82 if (out != NULL) {
83 out[0] = _UT('\0');
84 }
85 return out;
86 }
87
88 for (;;) {
89 if ((inAfterLast != NULL) && (read >= inAfterLast)) {
90 write[0] = _UT('\0');
91 return write;
92 }
93
94 switch (read[0]) {
95 case _UT('\0'):
96 write[0] = _UT('\0');
97 return write;
98
99 case _UT(' '):
100 if (spaceToPlus) {
101 write[0] = _UT('+');
102 write++;
103 } else {
104 write[0] = _UT('%');
105 write[1] = _UT('2');
106 write[2] = _UT('0');
107 write += 3;
108 }
109 prevWasCr = URI_FALSE;
110 break;
111
112 case URI_SET_UNRESERVED(_UT):
113 /* Copy unmodified */
114 write[0] = read[0];
115 write++;
116
117 prevWasCr = URI_FALSE;
118 break;
119
120 case _UT('\x0a'):
121 if (normalizeBreaks) {
122 if (!prevWasCr) {
123 write[0] = _UT('%');
124 write[1] = _UT('0');
125 write[2] = _UT('D');
126 write[3] = _UT('%');
127 write[4] = _UT('0');
128 write[5] = _UT('A');
129 write += 6;
130 }
131 } else {
132 write[0] = _UT('%');
133 write[1] = _UT('0');
134 write[2] = _UT('A');
135 write += 3;
136 }
137 prevWasCr = URI_FALSE;
138 break;
139
140 case _UT('\x0d'):
141 if (normalizeBreaks) {
142 write[0] = _UT('%');
143 write[1] = _UT('0');
144 write[2] = _UT('D');
145 write[3] = _UT('%');
146 write[4] = _UT('0');
147 write[5] = _UT('A');
148 write += 6;
149 } else {
150 write[0] = _UT('%');
151 write[1] = _UT('0');
152 write[2] = _UT('D');
153 write += 3;
154 }
155 prevWasCr = URI_TRUE;
156 break;
157
158 default:
159 /* Percent encode */
160 {
161 const unsigned char code = (unsigned char)read[0];
162 /* Uppercase recommended in (last sentence of) section 2.1 *
163 * of RFC 3986: *
164 * https://datatracker.ietf.org/doc/html/rfc3986#section-2.1 */
165 write[0] = _UT('%');
166 write[1] = URI_FUNC(HexToLetterEx)(code >> 4, URI_TRUE);
167 write[2] = URI_FUNC(HexToLetterEx)(code & 0x0f, URI_TRUE);
168 write += 3;
169 }
170 prevWasCr = URI_FALSE;
171 break;
172 }
173
174 read++;
175 }
176 }
177
178 const URI_CHAR * URI_FUNC(UnescapeInPlace)(URI_CHAR * inout) {
179 return URI_FUNC(UnescapeInPlaceEx)(inout, URI_FALSE, URI_BR_DONT_TOUCH);
180 }
181
182 const URI_CHAR * URI_FUNC(UnescapeInPlaceEx)(URI_CHAR * inout, UriBool plusToSpace,
183 UriBreakConversion breakConversion) {
184 URI_CHAR * read = inout;
185 URI_CHAR * write = inout;
186 UriBool prevWasCr = URI_FALSE;
187
188 if (inout == NULL) {
189 return NULL;
190 }
191
192 for (;;) {
193 switch (read[0]) {
194 case _UT('\0'):
195 if (read > write) {
196 write[0] = _UT('\0');
197 }
198 return write;
199
200 case _UT('%'):
201 switch (read[1]) {
202 case URI_SET_HEXDIG(_UT):
203 switch (read[2]) {
204 case URI_SET_HEXDIG(_UT): {
205 /* Percent group found */
206 const unsigned char left = URI_FUNC(HexdigToInt)(read[1]);
207 const unsigned char right = URI_FUNC(HexdigToInt)(read[2]);
208 const int code = 16 * left + right;
209 switch (code) {
210 case 10:
211 switch (breakConversion) {
212 case URI_BR_TO_LF:
213 if (!prevWasCr) {
214 write[0] = (URI_CHAR)10;
215 write++;
216 }
217 break;
218
219 case URI_BR_TO_CRLF:
220 if (!prevWasCr) {
221 write[0] = (URI_CHAR)13;
222 write[1] = (URI_CHAR)10;
223 write += 2;
224 }
225 break;
226
227 case URI_BR_TO_CR:
228 if (!prevWasCr) {
229 write[0] = (URI_CHAR)13;
230 write++;
231 }
232 break;
233
234 case URI_BR_DONT_TOUCH:
235 default:
236 write[0] = (URI_CHAR)10;
237 write++;
238 }
239 prevWasCr = URI_FALSE;
240 break;
241
242 case 13:
243 switch (breakConversion) {
244 case URI_BR_TO_LF:
245 write[0] = (URI_CHAR)10;
246 write++;
247 break;
248
249 case URI_BR_TO_CRLF:
250 write[0] = (URI_CHAR)13;
251 write[1] = (URI_CHAR)10;
252 write += 2;
253 break;
254
255 case URI_BR_TO_CR:
256 write[0] = (URI_CHAR)13;
257 write++;
258 break;
259
260 case URI_BR_DONT_TOUCH:
261 default:
262 write[0] = (URI_CHAR)13;
263 write++;
264 }
265 prevWasCr = URI_TRUE;
266 break;
267
268 default:
269 write[0] = (URI_CHAR)(code);
270 write++;
271
272 prevWasCr = URI_FALSE;
273 }
274 read += 3;
275 } break;
276
277 default:
278 /* Copy two chars unmodified and */
279 /* look at this char again */
280 if (read > write) {
281 write[0] = read[0];
282 write[1] = read[1];
283 }
284 read += 2;
285 write += 2;
286
287 prevWasCr = URI_FALSE;
288 }
289 break;
290
291 default:
292 /* Copy one char unmodified and */
293 /* look at this char again */
294 if (read > write) {
295 write[0] = read[0];
296 }
297 read++;
298 write++;
299
300 prevWasCr = URI_FALSE;
301 }
302 break;
303
304 case _UT('+'):
305 if (plusToSpace) {
306 /* Convert '+' to ' ' */
307 write[0] = _UT(' ');
308 } else {
309 /* Copy one char unmodified */
310 if (read > write) {
311 write[0] = read[0];
312 }
313 }
314 read++;
315 write++;
316
317 prevWasCr = URI_FALSE;
318 break;
319
320 default:
321 /* Copy one char unmodified */
322 if (read > write) {
323 write[0] = read[0];
324 }
325 read++;
326 write++;
327
328 prevWasCr = URI_FALSE;
329 }
330 }
331 }
332
333 #endif