Line | Count | Source (jump to first uncovered line) |
1 | | /** |
2 | | * uri.c: set of generic URI related routines |
3 | | * |
4 | | * Reference: RFCs 3986, 2732 and 2373 |
5 | | * |
6 | | * See Copyright for the status of this software. |
7 | | * |
8 | | * Author: Daniel Veillard |
9 | | */ |
10 | | |
11 | | #define IN_LIBXML |
12 | | #include "libxml.h" |
13 | | |
14 | | #include <limits.h> |
15 | | #include <string.h> |
16 | | |
17 | | #include <libxml/xmlmemory.h> |
18 | | #include <libxml/uri.h> |
19 | | #include <libxml/xmlerror.h> |
20 | | |
21 | | #include "private/error.h" |
22 | | #include "private/memory.h" |
23 | | |
24 | | /** |
25 | | * The definition of the URI regexp in the above RFC has no size limit |
26 | | * In practice they are usually relatively short except for the |
27 | | * data URI scheme as defined in RFC 2397. Even for data URI the usual |
28 | | * maximum size before hitting random practical limits is around 64 KB |
29 | | * and 4KB is usually a maximum admitted limit for proper operations. |
30 | | * The value below is more a security limit than anything else and |
31 | | * really should never be hit by 'normal' operations |
32 | | * Set to 1 MByte in 2012, this is only enforced on output |
33 | | */ |
34 | 46.3k | #define MAX_URI_LENGTH 1024 * 1024 |
35 | | |
36 | 1.07M | #define PORT_EMPTY 0 |
37 | 9.52k | #define PORT_EMPTY_SERVER -1 |
38 | | |
39 | | static void xmlCleanURI(xmlURIPtr uri); |
40 | | |
41 | | /* |
42 | | * Old rule from 2396 used in legacy handling code |
43 | | * alpha = lowalpha | upalpha |
44 | | */ |
45 | 67.2M | #define IS_ALPHA(x) (IS_LOWALPHA(x) || IS_UPALPHA(x)) |
46 | | |
47 | | |
48 | | /* |
49 | | * lowalpha = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | |
50 | | * "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" | |
51 | | * "u" | "v" | "w" | "x" | "y" | "z" |
52 | | */ |
53 | 67.2M | #define IS_LOWALPHA(x) (((x) >= 'a') && ((x) <= 'z')) |
54 | | |
55 | | /* |
56 | | * upalpha = "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" | "J" | |
57 | | * "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" | "S" | "T" | |
58 | | * "U" | "V" | "W" | "X" | "Y" | "Z" |
59 | | */ |
60 | 30.6M | #define IS_UPALPHA(x) (((x) >= 'A') && ((x) <= 'Z')) |
61 | | |
62 | | #ifdef IS_DIGIT |
63 | | #undef IS_DIGIT |
64 | | #endif |
65 | | /* |
66 | | * digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" |
67 | | */ |
68 | 7.17M | #define IS_DIGIT(x) (((x) >= '0') && ((x) <= '9')) |
69 | | |
70 | | /* |
71 | | * alphanum = alpha | digit |
72 | | */ |
73 | 67.2M | #define IS_ALPHANUM(x) (IS_ALPHA(x) || IS_DIGIT(x)) |
74 | | |
75 | | /* |
76 | | * mark = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")" |
77 | | */ |
78 | | |
79 | 6.40M | #define IS_MARK(x) (((x) == '-') || ((x) == '_') || ((x) == '.') || \ |
80 | 6.40M | ((x) == '!') || ((x) == '~') || ((x) == '*') || ((x) == '\'') || \ |
81 | 6.40M | ((x) == '(') || ((x) == ')')) |
82 | | |
83 | | /* |
84 | | * unwise = "{" | "}" | "|" | "\" | "^" | "`" |
85 | | */ |
86 | | #define IS_UNWISE(p) \ |
87 | 80.1k | (((*(p) == '{')) || ((*(p) == '}')) || ((*(p) == '|')) || \ |
88 | 80.1k | ((*(p) == '\\')) || ((*(p) == '^')) || ((*(p) == '[')) || \ |
89 | 80.1k | ((*(p) == ']')) || ((*(p) == '`'))) |
90 | | |
91 | | /* |
92 | | * reserved = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" | "$" | "," | |
93 | | * "[" | "]" |
94 | | */ |
95 | 320k | #define IS_RESERVED(x) (((x) == ';') || ((x) == '/') || ((x) == '?') || \ |
96 | 320k | ((x) == ':') || ((x) == '@') || ((x) == '&') || ((x) == '=') || \ |
97 | 320k | ((x) == '+') || ((x) == '$') || ((x) == ',') || ((x) == '[') || \ |
98 | 320k | ((x) == ']')) |
99 | | |
100 | | /* |
101 | | * unreserved = alphanum | mark |
102 | | */ |
103 | 33.6M | #define IS_UNRESERVED(x) (IS_ALPHANUM(x) || IS_MARK(x)) |
104 | | |
105 | | /* |
106 | | * Skip to next pointer char, handle escaped sequences |
107 | | */ |
108 | 36.1M | #define NEXT(p) ((*p == '%')? p += 3 : p++) |
109 | | |
110 | | /* |
111 | | * Productions from the spec. |
112 | | * |
113 | | * authority = server | reg_name |
114 | | * reg_name = 1*( unreserved | escaped | "$" | "," | |
115 | | * ";" | ":" | "@" | "&" | "=" | "+" ) |
116 | | * |
117 | | * path = [ abs_path | opaque_part ] |
118 | | */ |
119 | 629k | #define STRNDUP(s, n) (char *) xmlStrndup((const xmlChar *)(s), (n)) |
120 | | |
121 | | /************************************************************************ |
122 | | * * |
123 | | * RFC 3986 parser * |
124 | | * * |
125 | | ************************************************************************/ |
126 | | |
127 | 13.6M | #define ISA_DIGIT(p) ((*(p) >= '0') && (*(p) <= '9')) |
128 | 43.9M | #define ISA_ALPHA(p) (((*(p) >= 'a') && (*(p) <= 'z')) || \ |
129 | 43.9M | ((*(p) >= 'A') && (*(p) <= 'Z'))) |
130 | | #define ISA_HEXDIG(p) \ |
131 | 1.02M | (ISA_DIGIT(p) || ((*(p) >= 'a') && (*(p) <= 'f')) || \ |
132 | 1.02M | ((*(p) >= 'A') && (*(p) <= 'F'))) |
133 | | |
134 | | /* |
135 | | * sub-delims = "!" / "$" / "&" / "'" / "(" / ")" |
136 | | * / "*" / "+" / "," / ";" / "=" |
137 | | */ |
138 | | #define ISA_SUB_DELIM(p) \ |
139 | 37.1M | (((*(p) == '!')) || ((*(p) == '$')) || ((*(p) == '&')) || \ |
140 | 4.64M | ((*(p) == '(')) || ((*(p) == ')')) || ((*(p) == '*')) || \ |
141 | 4.64M | ((*(p) == '+')) || ((*(p) == ',')) || ((*(p) == ';')) || \ |
142 | 4.64M | ((*(p) == '=')) || ((*(p) == '\''))) |
143 | | |
144 | | /* |
145 | | * gen-delims = ":" / "/" / "?" / "\#" / "[" / "]" / "@" |
146 | | */ |
147 | | #define ISA_GEN_DELIM(p) \ |
148 | | (((*(p) == ':')) || ((*(p) == '/')) || ((*(p) == '?')) || \ |
149 | | ((*(p) == '#')) || ((*(p) == '[')) || ((*(p) == ']')) || \ |
150 | | ((*(p) == '@'))) |
151 | | |
152 | | /* |
153 | | * reserved = gen-delims / sub-delims |
154 | | */ |
155 | | #define ISA_RESERVED(p) (ISA_GEN_DELIM(p) || (ISA_SUB_DELIM(p))) |
156 | | |
157 | | /* |
158 | | * unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" |
159 | | */ |
160 | | #define ISA_STRICTLY_UNRESERVED(p) \ |
161 | 37.4M | ((ISA_ALPHA(p)) || (ISA_DIGIT(p)) || ((*(p) == '-')) || \ |
162 | 37.4M | ((*(p) == '.')) || ((*(p) == '_')) || ((*(p) == '~'))) |
163 | | |
164 | | /* |
165 | | * pct-encoded = "%" HEXDIG HEXDIG |
166 | | */ |
167 | | #define ISA_PCT_ENCODED(p) \ |
168 | 42.5M | ((*(p) == '%') && (ISA_HEXDIG(p + 1)) && (ISA_HEXDIG(p + 2))) |
169 | | |
170 | | /* |
171 | | * pchar = unreserved / pct-encoded / sub-delims / ":" / "@" |
172 | | */ |
173 | | #define ISA_PCHAR(u, p) \ |
174 | 39.3M | (ISA_UNRESERVED(u, p) || ISA_PCT_ENCODED(p) || ISA_SUB_DELIM(p) || \ |
175 | 31.2M | ((*(p) == ':')) || ((*(p) == '@'))) |
176 | | |
177 | | /* |
178 | | * From https://www.w3.org/TR/leiri/ |
179 | | * |
180 | | * " " / "<" / ">" / '"' / "{" / "}" / "|" |
181 | | * / "\" / "^" / "`" / %x0-1F / %x7F-D7FF |
182 | | * / %xE000-FFFD / %x10000-10FFFF |
183 | | */ |
184 | | #define ISA_UCSCHAR(p) \ |
185 | 0 | ((*(p) <= 0x20) || (*(p) >= 0x7F) || (*(p) == '<') || (*(p) == '>') || \ |
186 | 0 | (*(p) == '"') || (*(p) == '{') || (*(p) == '}') || (*(p) == '|') || \ |
187 | 0 | (*(p) == '\\') || (*(p) == '^') || (*(p) == '`')) |
188 | | |
189 | 74.9M | #define ISA_UNRESERVED(u, p) (xmlIsUnreserved(u, p)) |
190 | | |
191 | 5.12M | #define XML_URI_ALLOW_UNWISE 1 |
192 | 1.03M | #define XML_URI_NO_UNESCAPE 2 |
193 | 5.03M | #define XML_URI_ALLOW_UCSCHAR 4 |
194 | | |
195 | | static int |
196 | 37.4M | xmlIsUnreserved(xmlURIPtr uri, const char *cur) { |
197 | 37.4M | if (uri == NULL) |
198 | 0 | return(0); |
199 | | |
200 | 37.4M | if (ISA_STRICTLY_UNRESERVED(cur)) |
201 | 32.3M | return(1); |
202 | | |
203 | 5.11M | if (uri->cleanup & XML_URI_ALLOW_UNWISE) { |
204 | 80.1k | if (IS_UNWISE(cur)) |
205 | 5.29k | return(1); |
206 | 5.03M | } else if (uri->cleanup & XML_URI_ALLOW_UCSCHAR) { |
207 | 0 | if (ISA_UCSCHAR(cur)) |
208 | 0 | return(1); |
209 | 0 | } |
210 | | |
211 | 5.11M | return(0); |
212 | 5.11M | } |
213 | | |
214 | | /** |
215 | | * Parse an URI scheme |
216 | | * |
217 | | * ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) |
218 | | * |
219 | | * @param uri pointer to an URI structure |
220 | | * @param str pointer to the string to analyze |
221 | | * @returns 0 or the error code |
222 | | */ |
223 | | static int |
224 | 884k | xmlParse3986Scheme(xmlURIPtr uri, const char **str) { |
225 | 884k | const char *cur; |
226 | | |
227 | 884k | cur = *str; |
228 | 884k | if (!ISA_ALPHA(cur)) |
229 | 317k | return(1); |
230 | 567k | cur++; |
231 | | |
232 | | #if defined(_WIN32) || defined(__CYGWIN__) |
233 | | /* |
234 | | * Don't treat Windows drive letters as scheme. |
235 | | */ |
236 | | if (*cur == ':') |
237 | | return(1); |
238 | | #endif |
239 | | |
240 | 2.79M | while (ISA_ALPHA(cur) || ISA_DIGIT(cur) || |
241 | 2.79M | (*cur == '+') || (*cur == '-') || (*cur == '.')) cur++; |
242 | 567k | if (uri != NULL) { |
243 | 567k | if (uri->scheme != NULL) xmlFree(uri->scheme); |
244 | 567k | uri->scheme = STRNDUP(*str, cur - *str); |
245 | 567k | if (uri->scheme == NULL) |
246 | 214 | return(-1); |
247 | 567k | } |
248 | 567k | *str = cur; |
249 | 567k | return(0); |
250 | 567k | } |
251 | | |
252 | | /** |
253 | | * Parse the query part of an URI |
254 | | * |
255 | | * fragment = *( pchar / "/" / "?" ) |
256 | | * NOTE: the strict syntax as defined by 3986 does not allow '[' and ']' |
257 | | * in the fragment identifier but this is used very broadly for |
258 | | * xpointer scheme selection, so we are allowing it here to not break |
259 | | * for example all the DocBook processing chains. |
260 | | * |
261 | | * @param uri pointer to an URI structure |
262 | | * @param str pointer to the string to analyze |
263 | | * @returns 0 or the error code |
264 | | */ |
265 | | static int |
266 | | xmlParse3986Fragment(xmlURIPtr uri, const char **str) |
267 | 97.4k | { |
268 | 97.4k | const char *cur; |
269 | | |
270 | 97.4k | cur = *str; |
271 | | |
272 | 4.92M | while ((ISA_PCHAR(uri, cur)) || (*cur == '/') || (*cur == '?') || |
273 | 4.92M | (*cur == '[') || (*cur == ']')) |
274 | 4.82M | NEXT(cur); |
275 | 97.4k | if (uri != NULL) { |
276 | 97.4k | if (uri->fragment != NULL) |
277 | 0 | xmlFree(uri->fragment); |
278 | 97.4k | if (uri->cleanup & XML_URI_NO_UNESCAPE) |
279 | 626 | uri->fragment = STRNDUP(*str, cur - *str); |
280 | 96.7k | else |
281 | 96.7k | uri->fragment = xmlURIUnescapeString(*str, cur - *str, NULL); |
282 | 97.4k | if (uri->fragment == NULL) |
283 | 89 | return (-1); |
284 | 97.4k | } |
285 | 97.3k | *str = cur; |
286 | 97.3k | return (0); |
287 | 97.4k | } |
288 | | |
289 | | /** |
290 | | * Parse the query part of an URI |
291 | | * |
292 | | * query = *uric |
293 | | * |
294 | | * @param uri pointer to an URI structure |
295 | | * @param str pointer to the string to analyze |
296 | | * @returns 0 or the error code |
297 | | */ |
298 | | static int |
299 | | xmlParse3986Query(xmlURIPtr uri, const char **str) |
300 | 58.8k | { |
301 | 58.8k | const char *cur; |
302 | | |
303 | 58.8k | cur = *str; |
304 | | |
305 | 17.4M | while ((ISA_PCHAR(uri, cur)) || (*cur == '/') || (*cur == '?')) |
306 | 17.4M | NEXT(cur); |
307 | 58.8k | if (uri != NULL) { |
308 | 58.8k | if (uri->query != NULL) |
309 | 0 | xmlFree(uri->query); |
310 | 58.8k | if (uri->cleanup & XML_URI_NO_UNESCAPE) |
311 | 498 | uri->query = STRNDUP(*str, cur - *str); |
312 | 58.3k | else |
313 | 58.3k | uri->query = xmlURIUnescapeString(*str, cur - *str, NULL); |
314 | 58.8k | if (uri->query == NULL) |
315 | 56 | return (-1); |
316 | | |
317 | | /* Save the raw bytes of the query as well. |
318 | | * See: http://mail.gnome.org/archives/xml/2007-April/thread.html#00114 |
319 | | */ |
320 | 58.8k | if (uri->query_raw != NULL) |
321 | 0 | xmlFree (uri->query_raw); |
322 | 58.8k | uri->query_raw = STRNDUP (*str, cur - *str); |
323 | 58.8k | if (uri->query_raw == NULL) |
324 | 82 | return (-1); |
325 | 58.8k | } |
326 | 58.7k | *str = cur; |
327 | 58.7k | return (0); |
328 | 58.8k | } |
329 | | |
330 | | /** |
331 | | * Parse a port part and fills in the appropriate fields |
332 | | * of the `uri` structure |
333 | | * |
334 | | * port = *DIGIT |
335 | | * |
336 | | * @param uri pointer to an URI structure |
337 | | * @param str the string to analyze |
338 | | * @returns 0 or the error code |
339 | | */ |
340 | | static int |
341 | | xmlParse3986Port(xmlURIPtr uri, const char **str) |
342 | 14.5k | { |
343 | 14.5k | const char *cur = *str; |
344 | 14.5k | int port = 0; |
345 | | |
346 | 14.5k | if (ISA_DIGIT(cur)) { |
347 | 56.0k | while (ISA_DIGIT(cur)) { |
348 | 48.5k | int digit = *cur - '0'; |
349 | | |
350 | 48.5k | if (port > INT_MAX / 10) |
351 | 1.19k | return(1); |
352 | 47.3k | port *= 10; |
353 | 47.3k | if (port > INT_MAX - digit) |
354 | 1.37k | return(1); |
355 | 45.9k | port += digit; |
356 | | |
357 | 45.9k | cur++; |
358 | 45.9k | } |
359 | 7.53k | if (uri != NULL) |
360 | 7.53k | uri->port = port; |
361 | 7.53k | *str = cur; |
362 | 7.53k | return(0); |
363 | 10.1k | } |
364 | 4.45k | return(1); |
365 | 14.5k | } |
366 | | |
367 | | /** |
368 | | * Parse an user information part and fills in the appropriate fields |
369 | | * of the `uri` structure |
370 | | * |
371 | | * userinfo = *( unreserved / pct-encoded / sub-delims / ":" ) |
372 | | * |
373 | | * @param uri pointer to an URI structure |
374 | | * @param str the string to analyze |
375 | | * @returns 0 or the error code |
376 | | */ |
377 | | static int |
378 | | xmlParse3986Userinfo(xmlURIPtr uri, const char **str) |
379 | 151k | { |
380 | 151k | const char *cur; |
381 | | |
382 | 151k | cur = *str; |
383 | 1.27M | while (ISA_UNRESERVED(uri, cur) || ISA_PCT_ENCODED(cur) || |
384 | 1.27M | ISA_SUB_DELIM(cur) || (*cur == ':')) |
385 | 1.12M | NEXT(cur); |
386 | 151k | if (*cur == '@') { |
387 | 37.8k | if (uri != NULL) { |
388 | 37.8k | if (uri->user != NULL) xmlFree(uri->user); |
389 | 37.8k | if (uri->cleanup & XML_URI_NO_UNESCAPE) |
390 | 511 | uri->user = STRNDUP(*str, cur - *str); |
391 | 37.3k | else |
392 | 37.3k | uri->user = xmlURIUnescapeString(*str, cur - *str, NULL); |
393 | 37.8k | if (uri->user == NULL) |
394 | 28 | return(-1); |
395 | 37.8k | } |
396 | 37.7k | *str = cur; |
397 | 37.7k | return(0); |
398 | 37.8k | } |
399 | 113k | return(1); |
400 | 151k | } |
401 | | |
402 | | /** |
403 | | * dec-octet = DIGIT ; 0-9 |
404 | | * / %x31-39 DIGIT ; 10-99 |
405 | | * / "1" 2DIGIT ; 100-199 |
406 | | * / "2" %x30-34 DIGIT ; 200-249 |
407 | | * / "25" %x30-35 ; 250-255 |
408 | | * |
409 | | * Skip a dec-octet. |
410 | | * |
411 | | * @param str the string to analyze |
412 | | * @returns 0 if found and skipped, 1 otherwise |
413 | | */ |
414 | | static int |
415 | 70.9k | xmlParse3986DecOctet(const char **str) { |
416 | 70.9k | const char *cur = *str; |
417 | | |
418 | 70.9k | if (!(ISA_DIGIT(cur))) |
419 | 5.03k | return(1); |
420 | 65.8k | if (!ISA_DIGIT(cur+1)) |
421 | 31.6k | cur++; |
422 | 34.2k | else if ((*cur != '0') && (ISA_DIGIT(cur + 1)) && (!ISA_DIGIT(cur+2))) |
423 | 16.6k | cur += 2; |
424 | 17.5k | else if ((*cur == '1') && (ISA_DIGIT(cur + 1)) && (ISA_DIGIT(cur + 2))) |
425 | 3.61k | cur += 3; |
426 | 13.9k | else if ((*cur == '2') && (*(cur + 1) >= '0') && |
427 | 13.9k | (*(cur + 1) <= '4') && (ISA_DIGIT(cur + 2))) |
428 | 2.79k | cur += 3; |
429 | 11.1k | else if ((*cur == '2') && (*(cur + 1) == '5') && |
430 | 11.1k | (*(cur + 2) >= '0') && (*(cur + 1) <= '5')) |
431 | 2.97k | cur += 3; |
432 | 8.17k | else |
433 | 8.17k | return(1); |
434 | 57.7k | *str = cur; |
435 | 57.7k | return(0); |
436 | 65.8k | } |
437 | | /** |
438 | | * Parse an host part and fills in the appropriate fields |
439 | | * of the `uri` structure |
440 | | * |
441 | | * host = IP-literal / IPv4address / reg-name |
442 | | * IP-literal = "[" ( IPv6address / IPvFuture ) "]" |
443 | | * IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet |
444 | | * reg-name = *( unreserved / pct-encoded / sub-delims ) |
445 | | * |
446 | | * @param uri pointer to an URI structure |
447 | | * @param str the string to analyze |
448 | | * @returns 0 or the error code |
449 | | */ |
450 | | static int |
451 | | xmlParse3986Host(xmlURIPtr uri, const char **str) |
452 | 151k | { |
453 | 151k | const char *cur = *str; |
454 | 151k | const char *host; |
455 | | |
456 | 151k | host = cur; |
457 | | /* |
458 | | * IPv6 and future addressing scheme are enclosed between brackets |
459 | | */ |
460 | 151k | if (*cur == '[') { |
461 | 4.80k | cur++; |
462 | 3.48M | while ((*cur != ']') && (*cur != 0)) |
463 | 3.47M | cur++; |
464 | 4.80k | if (*cur != ']') |
465 | 1.25k | return(1); |
466 | 3.54k | cur++; |
467 | 3.54k | goto found; |
468 | 4.80k | } |
469 | | /* |
470 | | * try to parse an IPv4 |
471 | | */ |
472 | 146k | if (ISA_DIGIT(cur)) { |
473 | 43.1k | if (xmlParse3986DecOctet(&cur) != 0) |
474 | 3.35k | goto not_ipv4; |
475 | 39.7k | if (*cur != '.') |
476 | 13.6k | goto not_ipv4; |
477 | 26.1k | cur++; |
478 | 26.1k | if (xmlParse3986DecOctet(&cur) != 0) |
479 | 8.22k | goto not_ipv4; |
480 | 17.9k | if (*cur != '.') |
481 | 16.2k | goto not_ipv4; |
482 | 1.62k | if (xmlParse3986DecOctet(&cur) != 0) |
483 | 1.62k | goto not_ipv4; |
484 | 0 | if (*cur != '.') |
485 | 0 | goto not_ipv4; |
486 | 0 | if (xmlParse3986DecOctet(&cur) != 0) |
487 | 0 | goto not_ipv4; |
488 | 0 | goto found; |
489 | 43.1k | not_ipv4: |
490 | 43.1k | cur = *str; |
491 | 43.1k | } |
492 | | /* |
493 | | * then this should be a hostname which can be empty |
494 | | */ |
495 | 4.92M | while (ISA_UNRESERVED(uri, cur) || |
496 | 4.92M | ISA_PCT_ENCODED(cur) || ISA_SUB_DELIM(cur)) |
497 | 4.77M | NEXT(cur); |
498 | 149k | found: |
499 | 149k | if (uri != NULL) { |
500 | 149k | if (uri->authority != NULL) xmlFree(uri->authority); |
501 | 149k | uri->authority = NULL; |
502 | 149k | if (uri->server != NULL) xmlFree(uri->server); |
503 | 149k | if (cur != host) { |
504 | 127k | if (uri->cleanup & XML_URI_NO_UNESCAPE) |
505 | 875 | uri->server = STRNDUP(host, cur - host); |
506 | 126k | else |
507 | 126k | uri->server = xmlURIUnescapeString(host, cur - host, NULL); |
508 | 127k | if (uri->server == NULL) |
509 | 78 | return(-1); |
510 | 127k | } else |
511 | 22.5k | uri->server = NULL; |
512 | 149k | } |
513 | 149k | *str = cur; |
514 | 149k | return(0); |
515 | 149k | } |
516 | | |
517 | | /** |
518 | | * Parse an authority part and fills in the appropriate fields |
519 | | * of the `uri` structure |
520 | | * |
521 | | * authority = [ userinfo "@" ] host [ ":" port ] |
522 | | * |
523 | | * @param uri pointer to an URI structure |
524 | | * @param str the string to analyze |
525 | | * @returns 0 or the error code |
526 | | */ |
527 | | static int |
528 | | xmlParse3986Authority(xmlURIPtr uri, const char **str) |
529 | 151k | { |
530 | 151k | const char *cur; |
531 | 151k | int ret; |
532 | | |
533 | 151k | cur = *str; |
534 | | /* |
535 | | * try to parse an userinfo and check for the trailing @ |
536 | | */ |
537 | 151k | ret = xmlParse3986Userinfo(uri, &cur); |
538 | 151k | if (ret < 0) |
539 | 28 | return(ret); |
540 | 151k | if ((ret != 0) || (*cur != '@')) |
541 | 113k | cur = *str; |
542 | 37.7k | else |
543 | 37.7k | cur++; |
544 | 151k | ret = xmlParse3986Host(uri, &cur); |
545 | 151k | if (ret != 0) return(ret); |
546 | 149k | if (*cur == ':') { |
547 | 14.5k | cur++; |
548 | 14.5k | ret = xmlParse3986Port(uri, &cur); |
549 | 14.5k | if (ret != 0) return(ret); |
550 | 14.5k | } |
551 | 142k | *str = cur; |
552 | 142k | return(0); |
553 | 149k | } |
554 | | |
555 | | /** |
556 | | * Parse a segment and fills in the appropriate fields |
557 | | * of the `uri` structure |
558 | | * |
559 | | * segment = *pchar |
560 | | * segment-nz = 1*pchar |
561 | | * segment-nz-nc = 1*( unreserved / pct-encoded / sub-delims / "@" ) |
562 | | * ; non-zero-length segment without any colon ":" |
563 | | * |
564 | | * @param uri the URI |
565 | | * @param str the string to analyze |
566 | | * @param forbid an optional forbidden character |
567 | | * @param empty allow an empty segment |
568 | | * @returns 0 or the error code |
569 | | */ |
570 | | static int |
571 | | xmlParse3986Segment(xmlURIPtr uri, const char **str, char forbid, int empty) |
572 | 1.24M | { |
573 | 1.24M | const char *cur; |
574 | | |
575 | 1.24M | cur = *str; |
576 | 1.24M | if (!ISA_PCHAR(uri, cur) || (*cur == forbid)) { |
577 | 220k | if (empty) |
578 | 195k | return(0); |
579 | 24.4k | return(1); |
580 | 220k | } |
581 | 1.02M | NEXT(cur); |
582 | | |
583 | | #if defined(_WIN32) || defined(__CYGWIN__) |
584 | | /* |
585 | | * Allow Windows drive letters. |
586 | | */ |
587 | | if ((forbid == ':') && (*cur == forbid)) |
588 | | NEXT(cur); |
589 | | #endif |
590 | | |
591 | 6.83M | while (ISA_PCHAR(uri, cur) && (*cur != forbid)) |
592 | 5.81M | NEXT(cur); |
593 | 1.02M | *str = cur; |
594 | 1.02M | return (0); |
595 | 1.24M | } |
596 | | |
597 | | /** |
598 | | * Parse an path absolute or empty and fills in the appropriate fields |
599 | | * of the `uri` structure |
600 | | * |
601 | | * path-abempty = *( "/" segment ) |
602 | | * |
603 | | * @param uri pointer to an URI structure |
604 | | * @param str the string to analyze |
605 | | * @returns 0 or the error code |
606 | | */ |
607 | | static int |
608 | | xmlParse3986PathAbEmpty(xmlURIPtr uri, const char **str) |
609 | 142k | { |
610 | 142k | const char *cur; |
611 | 142k | int ret; |
612 | | |
613 | 142k | cur = *str; |
614 | | |
615 | 282k | while (*cur == '/') { |
616 | 140k | cur++; |
617 | 140k | ret = xmlParse3986Segment(uri, &cur, 0, 1); |
618 | 140k | if (ret != 0) return(ret); |
619 | 140k | } |
620 | 142k | if (uri != NULL) { |
621 | 142k | if (uri->path != NULL) xmlFree(uri->path); |
622 | 142k | if (*str != cur) { |
623 | 50.2k | if (uri->cleanup & XML_URI_NO_UNESCAPE) |
624 | 138 | uri->path = STRNDUP(*str, cur - *str); |
625 | 50.1k | else |
626 | 50.1k | uri->path = xmlURIUnescapeString(*str, cur - *str, NULL); |
627 | 50.2k | if (uri->path == NULL) |
628 | 32 | return (-1); |
629 | 92.4k | } else { |
630 | 92.4k | uri->path = NULL; |
631 | 92.4k | } |
632 | 142k | } |
633 | 142k | *str = cur; |
634 | 142k | return (0); |
635 | 142k | } |
636 | | |
637 | | /** |
638 | | * Parse an path absolute and fills in the appropriate fields |
639 | | * of the `uri` structure |
640 | | * |
641 | | * path-absolute = "/" [ segment-nz *( "/" segment ) ] |
642 | | * |
643 | | * @param uri pointer to an URI structure |
644 | | * @param str the string to analyze |
645 | | * @returns 0 or the error code |
646 | | */ |
647 | | static int |
648 | | xmlParse3986PathAbsolute(xmlURIPtr uri, const char **str) |
649 | 22.6k | { |
650 | 22.6k | const char *cur; |
651 | 22.6k | int ret; |
652 | | |
653 | 22.6k | cur = *str; |
654 | | |
655 | 22.6k | if (*cur != '/') |
656 | 0 | return(1); |
657 | 22.6k | cur++; |
658 | 22.6k | ret = xmlParse3986Segment(uri, &cur, 0, 0); |
659 | 22.6k | if (ret == 0) { |
660 | 39.1k | while (*cur == '/') { |
661 | 26.4k | cur++; |
662 | 26.4k | ret = xmlParse3986Segment(uri, &cur, 0, 1); |
663 | 26.4k | if (ret != 0) return(ret); |
664 | 26.4k | } |
665 | 12.7k | } |
666 | 22.6k | if (uri != NULL) { |
667 | 22.6k | if (uri->path != NULL) xmlFree(uri->path); |
668 | 22.6k | if (cur != *str) { |
669 | 22.6k | if (uri->cleanup & XML_URI_NO_UNESCAPE) |
670 | 233 | uri->path = STRNDUP(*str, cur - *str); |
671 | 22.4k | else |
672 | 22.4k | uri->path = xmlURIUnescapeString(*str, cur - *str, NULL); |
673 | 22.6k | if (uri->path == NULL) |
674 | 42 | return (-1); |
675 | 22.6k | } else { |
676 | 0 | uri->path = NULL; |
677 | 0 | } |
678 | 22.6k | } |
679 | 22.6k | *str = cur; |
680 | 22.6k | return (0); |
681 | 22.6k | } |
682 | | |
683 | | /** |
684 | | * Parse an path without root and fills in the appropriate fields |
685 | | * of the `uri` structure |
686 | | * |
687 | | * path-rootless = segment-nz *( "/" segment ) |
688 | | * |
689 | | * @param uri pointer to an URI structure |
690 | | * @param str the string to analyze |
691 | | * @returns 0 or the error code |
692 | | */ |
693 | | static int |
694 | | xmlParse3986PathRootless(xmlURIPtr uri, const char **str) |
695 | 82.0k | { |
696 | 82.0k | const char *cur; |
697 | 82.0k | int ret; |
698 | | |
699 | 82.0k | cur = *str; |
700 | | |
701 | 82.0k | ret = xmlParse3986Segment(uri, &cur, 0, 0); |
702 | 82.0k | if (ret != 0) return(ret); |
703 | 117k | while (*cur == '/') { |
704 | 35.8k | cur++; |
705 | 35.8k | ret = xmlParse3986Segment(uri, &cur, 0, 1); |
706 | 35.8k | if (ret != 0) return(ret); |
707 | 35.8k | } |
708 | 82.0k | if (uri != NULL) { |
709 | 82.0k | if (uri->path != NULL) xmlFree(uri->path); |
710 | 82.0k | if (cur != *str) { |
711 | 82.0k | if (uri->cleanup & XML_URI_NO_UNESCAPE) |
712 | 83 | uri->path = STRNDUP(*str, cur - *str); |
713 | 81.9k | else |
714 | 81.9k | uri->path = xmlURIUnescapeString(*str, cur - *str, NULL); |
715 | 82.0k | if (uri->path == NULL) |
716 | 49 | return (-1); |
717 | 82.0k | } else { |
718 | 0 | uri->path = NULL; |
719 | 0 | } |
720 | 82.0k | } |
721 | 81.9k | *str = cur; |
722 | 81.9k | return (0); |
723 | 82.0k | } |
724 | | |
725 | | /** |
726 | | * Parse an path which is not a scheme and fills in the appropriate fields |
727 | | * of the `uri` structure |
728 | | * |
729 | | * path-noscheme = segment-nz-nc *( "/" segment ) |
730 | | * |
731 | | * @param uri pointer to an URI structure |
732 | | * @param str the string to analyze |
733 | | * @returns 0 or the error code |
734 | | */ |
735 | | static int |
736 | | xmlParse3986PathNoScheme(xmlURIPtr uri, const char **str) |
737 | 568k | { |
738 | 568k | const char *cur; |
739 | 568k | int ret; |
740 | | |
741 | 568k | cur = *str; |
742 | | |
743 | 568k | ret = xmlParse3986Segment(uri, &cur, ':', 0); |
744 | 568k | if (ret != 0) return(ret); |
745 | 920k | while (*cur == '/') { |
746 | 366k | cur++; |
747 | 366k | ret = xmlParse3986Segment(uri, &cur, 0, 1); |
748 | 366k | if (ret != 0) return(ret); |
749 | 366k | } |
750 | 553k | if (uri != NULL) { |
751 | 553k | if (uri->path != NULL) xmlFree(uri->path); |
752 | 553k | if (cur != *str) { |
753 | 553k | if (uri->cleanup & XML_URI_NO_UNESCAPE) |
754 | 984 | uri->path = STRNDUP(*str, cur - *str); |
755 | 552k | else |
756 | 552k | uri->path = xmlURIUnescapeString(*str, cur - *str, NULL); |
757 | 553k | if (uri->path == NULL) |
758 | 277 | return (-1); |
759 | 553k | } else { |
760 | 0 | uri->path = NULL; |
761 | 0 | } |
762 | 553k | } |
763 | 553k | *str = cur; |
764 | 553k | return (0); |
765 | 553k | } |
766 | | |
767 | | /** |
768 | | * Parse an hierarchical part and fills in the appropriate fields |
769 | | * of the `uri` structure |
770 | | * |
771 | | * hier-part = "//" authority path-abempty |
772 | | * / path-absolute |
773 | | * / path-rootless |
774 | | * / path-empty |
775 | | * |
776 | | * @param uri pointer to an URI structure |
777 | | * @param str the string to analyze |
778 | | * @returns 0 or the error code |
779 | | */ |
780 | | static int |
781 | | xmlParse3986HierPart(xmlURIPtr uri, const char **str) |
782 | 212k | { |
783 | 212k | const char *cur; |
784 | 212k | int ret; |
785 | | |
786 | 212k | cur = *str; |
787 | | |
788 | 212k | if ((*cur == '/') && (*(cur + 1) == '/')) { |
789 | 109k | cur += 2; |
790 | 109k | ret = xmlParse3986Authority(uri, &cur); |
791 | 109k | if (ret != 0) return(ret); |
792 | | /* |
793 | | * An empty server is marked with a special URI value. |
794 | | */ |
795 | 106k | if ((uri->server == NULL) && (uri->port == PORT_EMPTY)) |
796 | 9.52k | uri->port = PORT_EMPTY_SERVER; |
797 | 106k | ret = xmlParse3986PathAbEmpty(uri, &cur); |
798 | 106k | if (ret != 0) return(ret); |
799 | 105k | *str = cur; |
800 | 105k | return(0); |
801 | 106k | } else if (*cur == '/') { |
802 | 4.83k | ret = xmlParse3986PathAbsolute(uri, &cur); |
803 | 4.83k | if (ret != 0) return(ret); |
804 | 97.8k | } else if (ISA_PCHAR(uri, cur)) { |
805 | 82.0k | ret = xmlParse3986PathRootless(uri, &cur); |
806 | 82.0k | if (ret != 0) return(ret); |
807 | 82.0k | } else { |
808 | | /* path-empty is effectively empty */ |
809 | 15.8k | if (uri != NULL) { |
810 | 15.8k | if (uri->path != NULL) xmlFree(uri->path); |
811 | 15.8k | uri->path = NULL; |
812 | 15.8k | } |
813 | 15.8k | } |
814 | 102k | *str = cur; |
815 | 102k | return (0); |
816 | 212k | } |
817 | | |
818 | | /** |
819 | | * Parse an URI string and fills in the appropriate fields |
820 | | * of the `uri` structure |
821 | | * |
822 | | * relative-ref = relative-part [ "?" query ] [ "\#" fragment ] |
823 | | * relative-part = "//" authority path-abempty |
824 | | * / path-absolute |
825 | | * / path-noscheme |
826 | | * / path-empty |
827 | | * |
828 | | * @param uri pointer to an URI structure |
829 | | * @param str the string to analyze |
830 | | * @returns 0 or the error code |
831 | | */ |
832 | | static int |
833 | 740k | xmlParse3986RelativeRef(xmlURIPtr uri, const char *str) { |
834 | 740k | int ret; |
835 | | |
836 | 740k | if ((*str == '/') && (*(str + 1) == '/')) { |
837 | 41.6k | str += 2; |
838 | 41.6k | ret = xmlParse3986Authority(uri, &str); |
839 | 41.6k | if (ret != 0) return(ret); |
840 | 36.6k | ret = xmlParse3986PathAbEmpty(uri, &str); |
841 | 36.6k | if (ret != 0) return(ret); |
842 | 698k | } else if (*str == '/') { |
843 | 17.8k | ret = xmlParse3986PathAbsolute(uri, &str); |
844 | 17.8k | if (ret != 0) return(ret); |
845 | 680k | } else if (ISA_PCHAR(uri, str)) { |
846 | 568k | ret = xmlParse3986PathNoScheme(uri, &str); |
847 | 568k | if (ret != 0) return(ret); |
848 | 568k | } else { |
849 | | /* path-empty is effectively empty */ |
850 | 112k | if (uri != NULL) { |
851 | 112k | if (uri->path != NULL) xmlFree(uri->path); |
852 | 112k | uri->path = NULL; |
853 | 112k | } |
854 | 112k | } |
855 | | |
856 | 720k | if (*str == '?') { |
857 | 46.5k | str++; |
858 | 46.5k | ret = xmlParse3986Query(uri, &str); |
859 | 46.5k | if (ret != 0) return(ret); |
860 | 46.5k | } |
861 | 720k | if (*str == '#') { |
862 | 88.3k | str++; |
863 | 88.3k | ret = xmlParse3986Fragment(uri, &str); |
864 | 88.3k | if (ret != 0) return(ret); |
865 | 88.3k | } |
866 | 720k | if (*str != 0) { |
867 | 280k | xmlCleanURI(uri); |
868 | 280k | return(1); |
869 | 280k | } |
870 | 439k | return(0); |
871 | 720k | } |
872 | | |
873 | | |
874 | | /** |
875 | | * Parse an URI string and fills in the appropriate fields |
876 | | * of the `uri` structure |
877 | | * |
878 | | * scheme ":" hier-part [ "?" query ] [ "\#" fragment ] |
879 | | * |
880 | | * @param uri pointer to an URI structure |
881 | | * @param str the string to analyze |
882 | | * @returns 0 or the error code |
883 | | */ |
884 | | static int |
885 | 884k | xmlParse3986URI(xmlURIPtr uri, const char *str) { |
886 | 884k | int ret; |
887 | | |
888 | 884k | ret = xmlParse3986Scheme(uri, &str); |
889 | 884k | if (ret != 0) return(ret); |
890 | 567k | if (*str != ':') { |
891 | 354k | return(1); |
892 | 354k | } |
893 | 212k | str++; |
894 | 212k | ret = xmlParse3986HierPart(uri, &str); |
895 | 212k | if (ret != 0) return(ret); |
896 | 208k | if (*str == '?') { |
897 | 12.3k | str++; |
898 | 12.3k | ret = xmlParse3986Query(uri, &str); |
899 | 12.3k | if (ret != 0) return(ret); |
900 | 12.3k | } |
901 | 208k | if (*str == '#') { |
902 | 9.10k | str++; |
903 | 9.10k | ret = xmlParse3986Fragment(uri, &str); |
904 | 9.10k | if (ret != 0) return(ret); |
905 | 9.10k | } |
906 | 208k | if (*str != 0) { |
907 | 64.4k | xmlCleanURI(uri); |
908 | 64.4k | return(1); |
909 | 64.4k | } |
910 | 144k | return(0); |
911 | 208k | } |
912 | | |
913 | | /** |
914 | | * Parse an URI reference string and fills in the appropriate fields |
915 | | * of the `uri` structure |
916 | | * |
917 | | * URI-reference = URI / relative-ref |
918 | | * |
919 | | * @param uri pointer to an URI structure |
920 | | * @param str the string to analyze |
921 | | * @returns 0 or the error code |
922 | | */ |
923 | | static int |
924 | 884k | xmlParse3986URIReference(xmlURIPtr uri, const char *str) { |
925 | 884k | int ret; |
926 | | |
927 | 884k | if (str == NULL) |
928 | 0 | return(-1); |
929 | 884k | xmlCleanURI(uri); |
930 | | |
931 | | /* |
932 | | * Try first to parse absolute refs, then fallback to relative if |
933 | | * it fails. |
934 | | */ |
935 | 884k | ret = xmlParse3986URI(uri, str); |
936 | 884k | if (ret < 0) |
937 | 378 | return(ret); |
938 | 884k | if (ret != 0) { |
939 | 740k | xmlCleanURI(uri); |
940 | 740k | ret = xmlParse3986RelativeRef(uri, str); |
941 | 740k | if (ret != 0) { |
942 | 300k | xmlCleanURI(uri); |
943 | 300k | return(ret); |
944 | 300k | } |
945 | 740k | } |
946 | 583k | return(0); |
947 | 884k | } |
948 | | |
949 | | /** |
950 | | * Parse an URI based on RFC 3986 |
951 | | * |
952 | | * URI-reference = [ absoluteURI | relativeURI ] [ "\#" fragment ] |
953 | | * |
954 | | * @since 2.13.0 |
955 | | * |
956 | | * @param str the URI string to analyze |
957 | | * @param uriOut optional pointer to parsed URI |
958 | | * @returns 0 on success, an error code (typically 1) if the URI is invalid |
959 | | * or -1 if a memory allocation failed. |
960 | | */ |
961 | | int |
962 | 872k | xmlParseURISafe(const char *str, xmlURI **uriOut) { |
963 | 872k | xmlURIPtr uri; |
964 | 872k | int ret; |
965 | | |
966 | 872k | if (uriOut == NULL) |
967 | 0 | return(1); |
968 | 872k | *uriOut = NULL; |
969 | 872k | if (str == NULL) |
970 | 10 | return(1); |
971 | | |
972 | 872k | uri = xmlCreateURI(); |
973 | 872k | if (uri == NULL) |
974 | 750 | return(-1); |
975 | | |
976 | 871k | ret = xmlParse3986URIReference(uri, str); |
977 | 871k | if (ret) { |
978 | 296k | xmlFreeURI(uri); |
979 | 296k | return(ret); |
980 | 296k | } |
981 | | |
982 | 575k | *uriOut = uri; |
983 | 575k | return(0); |
984 | 871k | } |
985 | | |
986 | | /** |
987 | | * Parse an URI based on RFC 3986 |
988 | | * |
989 | | * URI-reference = [ absoluteURI | relativeURI ] [ "\#" fragment ] |
990 | | * |
991 | | * @param str the URI string to analyze |
992 | | * @returns a newly built xmlURI or NULL in case of error |
993 | | */ |
994 | | xmlURI * |
995 | 118k | xmlParseURI(const char *str) { |
996 | 118k | xmlURIPtr uri; |
997 | 118k | xmlParseURISafe(str, &uri); |
998 | 118k | return(uri); |
999 | 118k | } |
1000 | | |
1001 | | /** |
1002 | | * Parse an URI reference string based on RFC 3986 and fills in the |
1003 | | * appropriate fields of the `uri` structure |
1004 | | * |
1005 | | * URI-reference = URI / relative-ref |
1006 | | * |
1007 | | * @param uri pointer to an URI structure |
1008 | | * @param str the string to analyze |
1009 | | * @returns 0 or the error code |
1010 | | */ |
1011 | | int |
1012 | 13.3k | xmlParseURIReference(xmlURI *uri, const char *str) { |
1013 | 13.3k | return(xmlParse3986URIReference(uri, str)); |
1014 | 13.3k | } |
1015 | | |
1016 | | /** |
1017 | | * Parse an URI but allows to keep intact the original fragments. |
1018 | | * |
1019 | | * URI-reference = URI / relative-ref |
1020 | | * |
1021 | | * @param str the URI string to analyze |
1022 | | * @param raw if 1 unescaping of URI pieces are disabled |
1023 | | * @returns a newly built xmlURI or NULL in case of error |
1024 | | */ |
1025 | | xmlURI * |
1026 | 4.56k | xmlParseURIRaw(const char *str, int raw) { |
1027 | 4.56k | xmlURIPtr uri; |
1028 | 4.56k | int ret; |
1029 | | |
1030 | 4.56k | if (str == NULL) |
1031 | 5 | return(NULL); |
1032 | 4.56k | uri = xmlCreateURI(); |
1033 | 4.56k | if (uri != NULL) { |
1034 | 4.48k | if (raw) { |
1035 | 4.48k | uri->cleanup |= XML_URI_NO_UNESCAPE; |
1036 | 4.48k | } |
1037 | 4.48k | ret = xmlParseURIReference(uri, str); |
1038 | 4.48k | if (ret) { |
1039 | 1.60k | xmlFreeURI(uri); |
1040 | 1.60k | return(NULL); |
1041 | 1.60k | } |
1042 | 4.48k | } |
1043 | 2.95k | return(uri); |
1044 | 4.56k | } |
1045 | | |
1046 | | /************************************************************************ |
1047 | | * * |
1048 | | * Generic URI structure functions * |
1049 | | * * |
1050 | | ************************************************************************/ |
1051 | | |
1052 | | /** |
1053 | | * Simply creates an empty xmlURI |
1054 | | * |
1055 | | * @returns the new structure or NULL in case of error |
1056 | | */ |
1057 | | xmlURI * |
1058 | 938k | xmlCreateURI(void) { |
1059 | 938k | xmlURIPtr ret; |
1060 | | |
1061 | 938k | ret = (xmlURIPtr) xmlMalloc(sizeof(xmlURI)); |
1062 | 938k | if (ret == NULL) |
1063 | 958 | return(NULL); |
1064 | 937k | memset(ret, 0, sizeof(xmlURI)); |
1065 | 937k | ret->port = PORT_EMPTY; |
1066 | 937k | return(ret); |
1067 | 938k | } |
1068 | | |
1069 | | /** |
1070 | | * Function to handle properly a reallocation when saving an URI |
1071 | | * Also imposes some limit on the length of an URI string output |
1072 | | */ |
1073 | | static xmlChar * |
1074 | 46.3k | xmlSaveUriRealloc(xmlChar *ret, int *max) { |
1075 | 46.3k | xmlChar *temp; |
1076 | 46.3k | int newSize; |
1077 | | |
1078 | 46.3k | newSize = xmlGrowCapacity(*max, 1, 80, MAX_URI_LENGTH); |
1079 | 46.3k | if (newSize < 0) |
1080 | 8 | return(NULL); |
1081 | 46.3k | temp = xmlRealloc(ret, newSize + 1); |
1082 | 46.3k | if (temp == NULL) |
1083 | 144 | return(NULL); |
1084 | 46.1k | *max = newSize; |
1085 | 46.1k | return(temp); |
1086 | 46.3k | } |
1087 | | |
1088 | | /** |
1089 | | * Save the URI as an escaped string |
1090 | | * |
1091 | | * @param uri pointer to an xmlURI |
1092 | | * @returns a new string (to be deallocated by caller) |
1093 | | */ |
1094 | | xmlChar * |
1095 | 90.3k | xmlSaveUri(xmlURI *uri) { |
1096 | 90.3k | xmlChar *ret = NULL; |
1097 | 90.3k | xmlChar *temp; |
1098 | 90.3k | const char *p; |
1099 | 90.3k | int len; |
1100 | 90.3k | int max; |
1101 | | |
1102 | 90.3k | if (uri == NULL) return(NULL); |
1103 | | |
1104 | | |
1105 | 88.6k | max = 80; |
1106 | 88.6k | ret = xmlMalloc(max + 1); |
1107 | 88.6k | if (ret == NULL) |
1108 | 200 | return(NULL); |
1109 | 88.4k | len = 0; |
1110 | | |
1111 | 88.4k | if (uri->scheme != NULL) { |
1112 | 30.7k | p = uri->scheme; |
1113 | 557k | while (*p != 0) { |
1114 | 527k | if (len >= max) { |
1115 | 1.85k | temp = xmlSaveUriRealloc(ret, &max); |
1116 | 1.85k | if (temp == NULL) goto mem_error; |
1117 | 1.84k | ret = temp; |
1118 | 1.84k | } |
1119 | 527k | ret[len++] = *p++; |
1120 | 527k | } |
1121 | 30.7k | if (len >= max) { |
1122 | 836 | temp = xmlSaveUriRealloc(ret, &max); |
1123 | 836 | if (temp == NULL) goto mem_error; |
1124 | 831 | ret = temp; |
1125 | 831 | } |
1126 | 30.6k | ret[len++] = ':'; |
1127 | 30.6k | } |
1128 | 88.4k | if (uri->opaque != NULL) { |
1129 | 0 | p = uri->opaque; |
1130 | 0 | while (*p != 0) { |
1131 | 0 | if (len + 3 >= max) { |
1132 | 0 | temp = xmlSaveUriRealloc(ret, &max); |
1133 | 0 | if (temp == NULL) goto mem_error; |
1134 | 0 | ret = temp; |
1135 | 0 | } |
1136 | 0 | if (IS_RESERVED(*(p)) || IS_UNRESERVED(*(p))) |
1137 | 0 | ret[len++] = *p++; |
1138 | 0 | else { |
1139 | 0 | int val = *(unsigned char *)p++; |
1140 | 0 | int hi = val / 0x10, lo = val % 0x10; |
1141 | 0 | ret[len++] = '%'; |
1142 | 0 | ret[len++] = hi + (hi > 9? 'A'-10 : '0'); |
1143 | 0 | ret[len++] = lo + (lo > 9? 'A'-10 : '0'); |
1144 | 0 | } |
1145 | 0 | } |
1146 | 88.4k | } else { |
1147 | 88.4k | if ((uri->server != NULL) || (uri->port != PORT_EMPTY)) { |
1148 | 31.3k | if (len + 3 >= max) { |
1149 | 735 | temp = xmlSaveUriRealloc(ret, &max); |
1150 | 735 | if (temp == NULL) goto mem_error; |
1151 | 730 | ret = temp; |
1152 | 730 | } |
1153 | 31.2k | ret[len++] = '/'; |
1154 | 31.2k | ret[len++] = '/'; |
1155 | 31.2k | if (uri->user != NULL) { |
1156 | 13.4k | p = uri->user; |
1157 | 23.4M | while (*p != 0) { |
1158 | 23.4M | if (len + 3 >= max) { |
1159 | 8.31k | temp = xmlSaveUriRealloc(ret, &max); |
1160 | 8.31k | if (temp == NULL) goto mem_error; |
1161 | 8.29k | ret = temp; |
1162 | 8.29k | } |
1163 | 23.4M | if ((IS_UNRESERVED(*(p))) || |
1164 | 23.4M | ((*(p) == ';')) || ((*(p) == ':')) || |
1165 | 23.4M | ((*(p) == '&')) || ((*(p) == '=')) || |
1166 | 23.4M | ((*(p) == '+')) || ((*(p) == '$')) || |
1167 | 23.4M | ((*(p) == ','))) |
1168 | 21.2M | ret[len++] = *p++; |
1169 | 2.16M | else { |
1170 | 2.16M | int val = *(unsigned char *)p++; |
1171 | 2.16M | int hi = val / 0x10, lo = val % 0x10; |
1172 | 2.16M | ret[len++] = '%'; |
1173 | 2.16M | ret[len++] = hi + (hi > 9? 'A'-10 : '0'); |
1174 | 2.16M | ret[len++] = lo + (lo > 9? 'A'-10 : '0'); |
1175 | 2.16M | } |
1176 | 23.4M | } |
1177 | 13.4k | if (len + 3 >= max) { |
1178 | 529 | temp = xmlSaveUriRealloc(ret, &max); |
1179 | 529 | if (temp == NULL) goto mem_error; |
1180 | 520 | ret = temp; |
1181 | 520 | } |
1182 | 13.4k | ret[len++] = '@'; |
1183 | 13.4k | } |
1184 | 31.2k | if (uri->server != NULL) { |
1185 | 24.0k | p = uri->server; |
1186 | 6.42M | while (*p != 0) { |
1187 | 6.40M | if (len >= max) { |
1188 | 2.28k | temp = xmlSaveUriRealloc(ret, &max); |
1189 | 2.28k | if (temp == NULL) goto mem_error; |
1190 | 2.27k | ret = temp; |
1191 | 2.27k | } |
1192 | | /* TODO: escaping? */ |
1193 | 6.40M | ret[len++] = (xmlChar) *p++; |
1194 | 6.40M | } |
1195 | 24.0k | } |
1196 | 31.2k | if (uri->port > 0) { |
1197 | 3.24k | if (len + 10 >= max) { |
1198 | 438 | temp = xmlSaveUriRealloc(ret, &max); |
1199 | 438 | if (temp == NULL) goto mem_error; |
1200 | 431 | ret = temp; |
1201 | 431 | } |
1202 | 3.24k | len += snprintf((char *) &ret[len], max - len, ":%d", uri->port); |
1203 | 3.24k | } |
1204 | 57.1k | } else if (uri->authority != NULL) { |
1205 | 0 | if (len + 3 >= max) { |
1206 | 0 | temp = xmlSaveUriRealloc(ret, &max); |
1207 | 0 | if (temp == NULL) goto mem_error; |
1208 | 0 | ret = temp; |
1209 | 0 | } |
1210 | 0 | ret[len++] = '/'; |
1211 | 0 | ret[len++] = '/'; |
1212 | 0 | p = uri->authority; |
1213 | 0 | while (*p != 0) { |
1214 | 0 | if (len + 3 >= max) { |
1215 | 0 | temp = xmlSaveUriRealloc(ret, &max); |
1216 | 0 | if (temp == NULL) goto mem_error; |
1217 | 0 | ret = temp; |
1218 | 0 | } |
1219 | 0 | if ((IS_UNRESERVED(*(p))) || |
1220 | 0 | ((*(p) == '$')) || ((*(p) == ',')) || ((*(p) == ';')) || |
1221 | 0 | ((*(p) == ':')) || ((*(p) == '@')) || ((*(p) == '&')) || |
1222 | 0 | ((*(p) == '=')) || ((*(p) == '+'))) |
1223 | 0 | ret[len++] = *p++; |
1224 | 0 | else { |
1225 | 0 | int val = *(unsigned char *)p++; |
1226 | 0 | int hi = val / 0x10, lo = val % 0x10; |
1227 | 0 | ret[len++] = '%'; |
1228 | 0 | ret[len++] = hi + (hi > 9? 'A'-10 : '0'); |
1229 | 0 | ret[len++] = lo + (lo > 9? 'A'-10 : '0'); |
1230 | 0 | } |
1231 | 0 | } |
1232 | 57.1k | } else if (uri->scheme != NULL) { |
1233 | 5.20k | if (len + 3 >= max) { |
1234 | 681 | temp = xmlSaveUriRealloc(ret, &max); |
1235 | 681 | if (temp == NULL) goto mem_error; |
1236 | 677 | ret = temp; |
1237 | 677 | } |
1238 | 5.20k | } |
1239 | 88.3k | if (uri->path != NULL) { |
1240 | 67.2k | p = uri->path; |
1241 | | /* |
1242 | | * the colon in file:///d: should not be escaped or |
1243 | | * Windows accesses fail later. |
1244 | | */ |
1245 | 67.2k | if ((uri->scheme != NULL) && |
1246 | 67.2k | (p[0] == '/') && |
1247 | 67.2k | (((p[1] >= 'a') && (p[1] <= 'z')) || |
1248 | 15.9k | ((p[1] >= 'A') && (p[1] <= 'Z'))) && |
1249 | 67.2k | (p[2] == ':') && |
1250 | 67.2k | (xmlStrEqual(BAD_CAST uri->scheme, BAD_CAST "file"))) { |
1251 | 863 | if (len + 3 >= max) { |
1252 | 426 | temp = xmlSaveUriRealloc(ret, &max); |
1253 | 426 | if (temp == NULL) goto mem_error; |
1254 | 423 | ret = temp; |
1255 | 423 | } |
1256 | 860 | ret[len++] = *p++; |
1257 | 860 | ret[len++] = *p++; |
1258 | 860 | ret[len++] = *p++; |
1259 | 860 | } |
1260 | 3.31M | while (*p != 0) { |
1261 | 3.24M | if (len + 3 >= max) { |
1262 | 11.2k | temp = xmlSaveUriRealloc(ret, &max); |
1263 | 11.2k | if (temp == NULL) goto mem_error; |
1264 | 11.2k | ret = temp; |
1265 | 11.2k | } |
1266 | 3.24M | if ((IS_UNRESERVED(*(p))) || ((*(p) == '/')) || |
1267 | 3.24M | ((*(p) == ';')) || ((*(p) == '@')) || ((*(p) == '&')) || |
1268 | 3.24M | ((*(p) == '=')) || ((*(p) == '+')) || ((*(p) == '$')) || |
1269 | 3.24M | ((*(p) == ','))) |
1270 | 3.04M | ret[len++] = *p++; |
1271 | 202k | else { |
1272 | 202k | int val = *(unsigned char *)p++; |
1273 | 202k | int hi = val / 0x10, lo = val % 0x10; |
1274 | 202k | ret[len++] = '%'; |
1275 | 202k | ret[len++] = hi + (hi > 9? 'A'-10 : '0'); |
1276 | 202k | ret[len++] = lo + (lo > 9? 'A'-10 : '0'); |
1277 | 202k | } |
1278 | 3.24M | } |
1279 | 67.2k | } |
1280 | 88.3k | if (uri->query_raw != NULL) { |
1281 | 14.7k | if (len + 1 >= max) { |
1282 | 470 | temp = xmlSaveUriRealloc(ret, &max); |
1283 | 470 | if (temp == NULL) goto mem_error; |
1284 | 462 | ret = temp; |
1285 | 462 | } |
1286 | 14.7k | ret[len++] = '?'; |
1287 | 14.7k | p = uri->query_raw; |
1288 | 10.6M | while (*p != 0) { |
1289 | 10.6M | if (len + 1 >= max) { |
1290 | 6.84k | temp = xmlSaveUriRealloc(ret, &max); |
1291 | 6.84k | if (temp == NULL) goto mem_error; |
1292 | 6.82k | ret = temp; |
1293 | 6.82k | } |
1294 | 10.6M | ret[len++] = *p++; |
1295 | 10.6M | } |
1296 | 73.6k | } else if (uri->query != NULL) { |
1297 | 0 | if (len + 3 >= max) { |
1298 | 0 | temp = xmlSaveUriRealloc(ret, &max); |
1299 | 0 | if (temp == NULL) goto mem_error; |
1300 | 0 | ret = temp; |
1301 | 0 | } |
1302 | 0 | ret[len++] = '?'; |
1303 | 0 | p = uri->query; |
1304 | 0 | while (*p != 0) { |
1305 | 0 | if (len + 3 >= max) { |
1306 | 0 | temp = xmlSaveUriRealloc(ret, &max); |
1307 | 0 | if (temp == NULL) goto mem_error; |
1308 | 0 | ret = temp; |
1309 | 0 | } |
1310 | 0 | if ((IS_UNRESERVED(*(p))) || (IS_RESERVED(*(p)))) |
1311 | 0 | ret[len++] = *p++; |
1312 | 0 | else { |
1313 | 0 | int val = *(unsigned char *)p++; |
1314 | 0 | int hi = val / 0x10, lo = val % 0x10; |
1315 | 0 | ret[len++] = '%'; |
1316 | 0 | ret[len++] = hi + (hi > 9? 'A'-10 : '0'); |
1317 | 0 | ret[len++] = lo + (lo > 9? 'A'-10 : '0'); |
1318 | 0 | } |
1319 | 0 | } |
1320 | 0 | } |
1321 | 88.3k | } |
1322 | 88.3k | if (uri->fragment != NULL) { |
1323 | 25.5k | if (len + 3 >= max) { |
1324 | 908 | temp = xmlSaveUriRealloc(ret, &max); |
1325 | 908 | if (temp == NULL) goto mem_error; |
1326 | 899 | ret = temp; |
1327 | 899 | } |
1328 | 25.4k | ret[len++] = '#'; |
1329 | 25.4k | p = uri->fragment; |
1330 | 2.72M | while (*p != 0) { |
1331 | 2.70M | if (len + 3 >= max) { |
1332 | 10.1k | temp = xmlSaveUriRealloc(ret, &max); |
1333 | 10.1k | if (temp == NULL) goto mem_error; |
1334 | 10.1k | ret = temp; |
1335 | 10.1k | } |
1336 | 2.70M | if ((IS_UNRESERVED(*(p))) || (IS_RESERVED(*(p)))) |
1337 | 2.67M | ret[len++] = *p++; |
1338 | 29.5k | else { |
1339 | 29.5k | int val = *(unsigned char *)p++; |
1340 | 29.5k | int hi = val / 0x10, lo = val % 0x10; |
1341 | 29.5k | ret[len++] = '%'; |
1342 | 29.5k | ret[len++] = hi + (hi > 9? 'A'-10 : '0'); |
1343 | 29.5k | ret[len++] = lo + (lo > 9? 'A'-10 : '0'); |
1344 | 29.5k | } |
1345 | 2.70M | } |
1346 | 25.4k | } |
1347 | 88.3k | if (len >= max) { |
1348 | 620 | temp = xmlSaveUriRealloc(ret, &max); |
1349 | 620 | if (temp == NULL) goto mem_error; |
1350 | 612 | ret = temp; |
1351 | 612 | } |
1352 | 88.3k | ret[len] = 0; |
1353 | 88.3k | return(ret); |
1354 | | |
1355 | 152 | mem_error: |
1356 | 152 | xmlFree(ret); |
1357 | 152 | return(NULL); |
1358 | 88.3k | } |
1359 | | |
1360 | | /** |
1361 | | * Prints the URI in the stream `stream`. |
1362 | | * |
1363 | | * @param stream a FILE* for the output |
1364 | | * @param uri pointer to an xmlURI |
1365 | | */ |
1366 | | void |
1367 | 0 | xmlPrintURI(FILE *stream, xmlURI *uri) { |
1368 | 0 | xmlChar *out; |
1369 | |
|
1370 | 0 | out = xmlSaveUri(uri); |
1371 | 0 | if (out != NULL) { |
1372 | 0 | fprintf(stream, "%s", (char *) out); |
1373 | 0 | xmlFree(out); |
1374 | 0 | } |
1375 | 0 | } |
1376 | | |
1377 | | /** |
1378 | | * Make sure the xmlURI struct is free of content |
1379 | | * |
1380 | | * @param uri pointer to an xmlURI |
1381 | | */ |
1382 | | static void |
1383 | 2.27M | xmlCleanURI(xmlURIPtr uri) { |
1384 | 2.27M | if (uri == NULL) return; |
1385 | | |
1386 | 2.27M | if (uri->scheme != NULL) xmlFree(uri->scheme); |
1387 | 2.27M | uri->scheme = NULL; |
1388 | 2.27M | if (uri->server != NULL) xmlFree(uri->server); |
1389 | 2.27M | uri->server = NULL; |
1390 | 2.27M | if (uri->user != NULL) xmlFree(uri->user); |
1391 | 2.27M | uri->user = NULL; |
1392 | 2.27M | if (uri->path != NULL) xmlFree(uri->path); |
1393 | 2.27M | uri->path = NULL; |
1394 | 2.27M | if (uri->fragment != NULL) xmlFree(uri->fragment); |
1395 | 2.27M | uri->fragment = NULL; |
1396 | 2.27M | if (uri->opaque != NULL) xmlFree(uri->opaque); |
1397 | 2.27M | uri->opaque = NULL; |
1398 | 2.27M | if (uri->authority != NULL) xmlFree(uri->authority); |
1399 | 2.27M | uri->authority = NULL; |
1400 | 2.27M | if (uri->query != NULL) xmlFree(uri->query); |
1401 | 2.27M | uri->query = NULL; |
1402 | 2.27M | if (uri->query_raw != NULL) xmlFree(uri->query_raw); |
1403 | 2.27M | uri->query_raw = NULL; |
1404 | 2.27M | } |
1405 | | |
1406 | | /** |
1407 | | * Free up the xmlURI struct |
1408 | | * |
1409 | | * @param uri pointer to an xmlURI |
1410 | | */ |
1411 | | void |
1412 | 961k | xmlFreeURI(xmlURI *uri) { |
1413 | 961k | if (uri == NULL) return; |
1414 | | |
1415 | 937k | if (uri->scheme != NULL) xmlFree(uri->scheme); |
1416 | 937k | if (uri->server != NULL) xmlFree(uri->server); |
1417 | 937k | if (uri->user != NULL) xmlFree(uri->user); |
1418 | 937k | if (uri->path != NULL) xmlFree(uri->path); |
1419 | 937k | if (uri->fragment != NULL) xmlFree(uri->fragment); |
1420 | 937k | if (uri->opaque != NULL) xmlFree(uri->opaque); |
1421 | 937k | if (uri->authority != NULL) xmlFree(uri->authority); |
1422 | 937k | if (uri->query != NULL) xmlFree(uri->query); |
1423 | 937k | if (uri->query_raw != NULL) xmlFree(uri->query_raw); |
1424 | 937k | xmlFree(uri); |
1425 | 937k | } |
1426 | | |
1427 | | /************************************************************************ |
1428 | | * * |
1429 | | * Helper functions * |
1430 | | * * |
1431 | | ************************************************************************/ |
1432 | | |
1433 | | static int |
1434 | 3.81M | xmlIsPathSeparator(int c, int isFile) { |
1435 | 3.81M | (void) isFile; |
1436 | | |
1437 | 3.81M | if (c == '/') |
1438 | 416k | return(1); |
1439 | | |
1440 | | #if defined(_WIN32) || defined(__CYGWIN__) |
1441 | | if (isFile && (c == '\\')) |
1442 | | return(1); |
1443 | | #endif |
1444 | | |
1445 | 3.40M | return(0); |
1446 | 3.81M | } |
1447 | | |
1448 | | /** |
1449 | | * Normalize a filesystem path or URI. |
1450 | | * |
1451 | | * @param path pointer to the path string |
1452 | | * @param isFile true for filesystem paths, false for URIs |
1453 | | * @returns 0 or an error code |
1454 | | */ |
1455 | | static int |
1456 | 88.1k | xmlNormalizePath(char *path, int isFile) { |
1457 | 88.1k | char *cur, *out; |
1458 | 88.1k | int numSeg = 0; |
1459 | | |
1460 | 88.1k | if (path == NULL) |
1461 | 33.2k | return(-1); |
1462 | | |
1463 | 54.8k | cur = path; |
1464 | 54.8k | out = path; |
1465 | | |
1466 | 54.8k | if (*cur == 0) |
1467 | 868 | return(0); |
1468 | | |
1469 | 54.0k | if (xmlIsPathSeparator(*cur, isFile)) { |
1470 | 15.6k | cur++; |
1471 | 15.6k | *out++ = '/'; |
1472 | 15.6k | } |
1473 | | |
1474 | 380k | while (*cur != 0) { |
1475 | | /* |
1476 | | * At this point, out is either empty or ends with a separator. |
1477 | | * Collapse multiple separators first. |
1478 | | */ |
1479 | 416k | while (xmlIsPathSeparator(*cur, isFile)) { |
1480 | | #if defined(_WIN32) || defined(__CYGWIN__) |
1481 | | /* Allow two separators at start of path */ |
1482 | | if ((isFile) && (out == path + 1)) |
1483 | | *out++ = '/'; |
1484 | | #endif |
1485 | 83.9k | cur++; |
1486 | 83.9k | } |
1487 | | |
1488 | 332k | if (*cur == '.') { |
1489 | 42.9k | if (cur[1] == 0) { |
1490 | | /* Ignore "." at end of path */ |
1491 | 4.99k | break; |
1492 | 37.9k | } else if (xmlIsPathSeparator(cur[1], isFile)) { |
1493 | | /* Skip "./" */ |
1494 | 9.39k | cur += 2; |
1495 | 9.39k | continue; |
1496 | 28.5k | } else if ((cur[1] == '.') && |
1497 | 28.5k | ((cur[2] == 0) || xmlIsPathSeparator(cur[2], isFile))) { |
1498 | 15.0k | if (numSeg > 0) { |
1499 | | /* Handle ".." by removing last segment */ |
1500 | 33.0k | do { |
1501 | 33.0k | out--; |
1502 | 33.0k | } while ((out > path) && |
1503 | 33.0k | !xmlIsPathSeparator(out[-1], isFile)); |
1504 | 5.13k | numSeg--; |
1505 | | |
1506 | 5.13k | if (cur[2] == 0) |
1507 | 528 | break; |
1508 | 4.60k | cur += 3; |
1509 | 4.60k | continue; |
1510 | 9.87k | } else if (out[0] == '/') { |
1511 | | /* Ignore extraneous ".." in absolute paths */ |
1512 | 2.16k | if (cur[2] == 0) |
1513 | 461 | break; |
1514 | 1.70k | cur += 3; |
1515 | 1.70k | continue; |
1516 | 7.70k | } else { |
1517 | | /* Keep "../" at start of relative path */ |
1518 | 7.70k | numSeg--; |
1519 | 7.70k | } |
1520 | 15.0k | } |
1521 | 42.9k | } |
1522 | | |
1523 | | /* Copy segment */ |
1524 | 2.83M | while ((*cur != 0) && !xmlIsPathSeparator(*cur, isFile)) { |
1525 | 2.51M | *out++ = *cur++; |
1526 | 2.51M | } |
1527 | | |
1528 | | /* Copy separator */ |
1529 | 310k | if (*cur != 0) { |
1530 | 268k | cur++; |
1531 | 268k | *out++ = '/'; |
1532 | 268k | } |
1533 | | |
1534 | 310k | numSeg++; |
1535 | 310k | } |
1536 | | |
1537 | | /* Keep "." if output is empty and it's a file */ |
1538 | 54.0k | if ((isFile) && (out <= path)) |
1539 | 742 | *out++ = '.'; |
1540 | 54.0k | *out = 0; |
1541 | | |
1542 | 54.0k | return(0); |
1543 | 54.8k | } |
1544 | | |
1545 | | /** |
1546 | | * Applies the 5 normalization steps to a path string--that is, RFC 2396 |
1547 | | * Section 5.2, steps 6.c through 6.g. |
1548 | | * |
1549 | | * Normalization occurs directly on the string, no new allocation is done |
1550 | | * |
1551 | | * @param path pointer to the path string |
1552 | | * @returns 0 or an error code |
1553 | | */ |
1554 | | int |
1555 | 29.4k | xmlNormalizeURIPath(char *path) { |
1556 | 29.4k | return(xmlNormalizePath(path, 0)); |
1557 | 29.4k | } |
1558 | | |
1559 | 971k | static int is_hex(char c) { |
1560 | 971k | if (((c >= '0') && (c <= '9')) || |
1561 | 971k | ((c >= 'a') && (c <= 'f')) || |
1562 | 971k | ((c >= 'A') && (c <= 'F'))) |
1563 | 940k | return(1); |
1564 | 31.0k | return(0); |
1565 | 971k | } |
1566 | | |
1567 | | /** |
1568 | | * Unescaping routine, but does not check that the string is an URI. The |
1569 | | * output is a direct unsigned char translation of %XX values (no encoding) |
1570 | | * Note that the length of the result can only be smaller or same size as |
1571 | | * the input string. |
1572 | | * |
1573 | | * @param str the string to unescape |
1574 | | * @param len the length in bytes to unescape (or <= 0 to indicate full string) |
1575 | | * @param target optional destination buffer |
1576 | | * @returns a copy of the string, but unescaped, will return NULL only in case |
1577 | | * of error |
1578 | | */ |
1579 | | char * |
1580 | 1.09M | xmlURIUnescapeString(const char *str, int len, char *target) { |
1581 | 1.09M | char *ret, *out; |
1582 | 1.09M | const char *in; |
1583 | | |
1584 | 1.09M | if (str == NULL) |
1585 | 5 | return(NULL); |
1586 | 1.09M | if (len <= 0) len = strlen(str); |
1587 | 1.09M | if (len < 0) return(NULL); |
1588 | | |
1589 | 1.09M | if (target == NULL) { |
1590 | 1.09M | ret = xmlMalloc(len + 1); |
1591 | 1.09M | if (ret == NULL) |
1592 | 725 | return(NULL); |
1593 | 1.09M | } else |
1594 | 0 | ret = target; |
1595 | 1.09M | in = str; |
1596 | 1.09M | out = ret; |
1597 | 129M | while(len > 0) { |
1598 | 128M | if ((len > 2) && (*in == '%') && (is_hex(in[1])) && (is_hex(in[2]))) { |
1599 | 465k | int c = 0; |
1600 | 465k | in++; |
1601 | 465k | if ((*in >= '0') && (*in <= '9')) |
1602 | 310k | c = (*in - '0'); |
1603 | 155k | else if ((*in >= 'a') && (*in <= 'f')) |
1604 | 24.4k | c = (*in - 'a') + 10; |
1605 | 130k | else if ((*in >= 'A') && (*in <= 'F')) |
1606 | 130k | c = (*in - 'A') + 10; |
1607 | 465k | in++; |
1608 | 465k | if ((*in >= '0') && (*in <= '9')) |
1609 | 179k | c = c * 16 + (*in - '0'); |
1610 | 286k | else if ((*in >= 'a') && (*in <= 'f')) |
1611 | 29.0k | c = c * 16 + (*in - 'a') + 10; |
1612 | 257k | else if ((*in >= 'A') && (*in <= 'F')) |
1613 | 257k | c = c * 16 + (*in - 'A') + 10; |
1614 | 465k | in++; |
1615 | 465k | len -= 3; |
1616 | | /* Explicit sign change */ |
1617 | 465k | *out++ = (char) c; |
1618 | 127M | } else { |
1619 | 127M | *out++ = *in++; |
1620 | 127M | len--; |
1621 | 127M | } |
1622 | 128M | } |
1623 | 1.09M | *out = 0; |
1624 | 1.09M | return(ret); |
1625 | 1.09M | } |
1626 | | |
1627 | | /** |
1628 | | * This routine escapes a string to hex, ignoring unreserved characters |
1629 | | * a-z, A-Z, 0-9, "-._~", a few sub-delims "!*'()", the gen-delim "@" |
1630 | | * (why?) and the characters in the exception list. |
1631 | | * |
1632 | | * @param str string to escape |
1633 | | * @param list exception list string of chars not to escape |
1634 | | * @returns a new escaped string or NULL in case of error. |
1635 | | */ |
1636 | | xmlChar * |
1637 | 35.3k | xmlURIEscapeStr(const xmlChar *str, const xmlChar *list) { |
1638 | 35.3k | xmlChar *ret, ch; |
1639 | 35.3k | const xmlChar *in; |
1640 | 35.3k | int len, out; |
1641 | | |
1642 | 35.3k | if (str == NULL) |
1643 | 5 | return(NULL); |
1644 | 35.3k | if (str[0] == 0) |
1645 | 376 | return(xmlStrdup(str)); |
1646 | 34.9k | len = xmlStrlen(str); |
1647 | | |
1648 | 34.9k | len += 20; |
1649 | 34.9k | ret = xmlMalloc(len); |
1650 | 34.9k | if (ret == NULL) |
1651 | 115 | return(NULL); |
1652 | 34.8k | in = (const xmlChar *) str; |
1653 | 34.8k | out = 0; |
1654 | 4.29M | while(*in != 0) { |
1655 | 4.26M | if (len - out <= 3) { |
1656 | 8.30k | xmlChar *temp; |
1657 | 8.30k | int newSize; |
1658 | | |
1659 | 8.30k | newSize = xmlGrowCapacity(len, 1, 1, XML_MAX_ITEMS); |
1660 | 8.30k | if (newSize < 0) { |
1661 | 0 | xmlFree(ret); |
1662 | 0 | return(NULL); |
1663 | 0 | } |
1664 | 8.30k | temp = xmlRealloc(ret, newSize); |
1665 | 8.30k | if (temp == NULL) { |
1666 | 22 | xmlFree(ret); |
1667 | 22 | return(NULL); |
1668 | 22 | } |
1669 | 8.28k | ret = temp; |
1670 | 8.28k | len = newSize; |
1671 | 8.28k | } |
1672 | | |
1673 | 4.26M | ch = *in; |
1674 | | |
1675 | 4.26M | if ((ch != '@') && (!IS_UNRESERVED(ch)) && (!xmlStrchr(list, ch))) { |
1676 | 1.89M | unsigned char val; |
1677 | 1.89M | ret[out++] = '%'; |
1678 | 1.89M | val = ch >> 4; |
1679 | 1.89M | if (val <= 9) |
1680 | 737k | ret[out++] = '0' + val; |
1681 | 1.16M | else |
1682 | 1.16M | ret[out++] = 'A' + val - 0xA; |
1683 | 1.89M | val = ch & 0xF; |
1684 | 1.89M | if (val <= 9) |
1685 | 744k | ret[out++] = '0' + val; |
1686 | 1.15M | else |
1687 | 1.15M | ret[out++] = 'A' + val - 0xA; |
1688 | 1.89M | in++; |
1689 | 2.36M | } else { |
1690 | 2.36M | ret[out++] = *in++; |
1691 | 2.36M | } |
1692 | | |
1693 | 4.26M | } |
1694 | 34.8k | ret[out] = 0; |
1695 | 34.8k | return(ret); |
1696 | 34.8k | } |
1697 | | |
1698 | | /** |
1699 | | * Escaping routine, does not do validity checks ! |
1700 | | * It will try to escape the chars needing this, but this is heuristic |
1701 | | * based it's impossible to be sure. |
1702 | | * |
1703 | | * 25 May 2001 |
1704 | | * Uses #xmlParseURI and #xmlURIEscapeStr to try to escape correctly |
1705 | | * according to RFC2396. |
1706 | | * - Carl Douglas |
1707 | | * @param str the string of the URI to escape |
1708 | | * @returns an copy of the string, but escaped |
1709 | | */ |
1710 | | xmlChar * |
1711 | | xmlURIEscape(const xmlChar * str) |
1712 | 4.56k | { |
1713 | 4.56k | xmlChar *ret, *segment = NULL; |
1714 | 4.56k | xmlURIPtr uri; |
1715 | 4.56k | int ret2; |
1716 | | |
1717 | 4.56k | if (str == NULL) |
1718 | 5 | return (NULL); |
1719 | | |
1720 | 4.56k | uri = xmlCreateURI(); |
1721 | 4.56k | if (uri != NULL) { |
1722 | | /* |
1723 | | * Allow escaping errors in the unescaped form |
1724 | | */ |
1725 | 4.51k | uri->cleanup = XML_URI_ALLOW_UNWISE; |
1726 | 4.51k | ret2 = xmlParseURIReference(uri, (const char *)str); |
1727 | 4.51k | if (ret2) { |
1728 | 1.27k | xmlFreeURI(uri); |
1729 | 1.27k | return (NULL); |
1730 | 1.27k | } |
1731 | 4.51k | } |
1732 | | |
1733 | 3.28k | if (!uri) |
1734 | 44 | return NULL; |
1735 | | |
1736 | 3.23k | ret = NULL; |
1737 | | |
1738 | 3.84k | #define NULLCHK(p) if(!p) { \ |
1739 | 38 | xmlFreeURI(uri); \ |
1740 | 38 | xmlFree(ret); \ |
1741 | 38 | return NULL; } \ |
1742 | 3.23k | |
1743 | 3.23k | if (uri->scheme) { |
1744 | 766 | segment = xmlURIEscapeStr(BAD_CAST uri->scheme, BAD_CAST "+-."); |
1745 | 766 | NULLCHK(segment) |
1746 | 758 | ret = xmlStrcat(ret, segment); |
1747 | 758 | ret = xmlStrcat(ret, BAD_CAST ":"); |
1748 | 758 | xmlFree(segment); |
1749 | 758 | } |
1750 | | |
1751 | 3.23k | if (uri->authority) { |
1752 | 0 | segment = |
1753 | 0 | xmlURIEscapeStr(BAD_CAST uri->authority, BAD_CAST "/?;:@"); |
1754 | 0 | NULLCHK(segment) |
1755 | 0 | ret = xmlStrcat(ret, BAD_CAST "//"); |
1756 | 0 | ret = xmlStrcat(ret, segment); |
1757 | 0 | xmlFree(segment); |
1758 | 0 | } |
1759 | | |
1760 | 3.23k | if (uri->user) { |
1761 | 531 | segment = xmlURIEscapeStr(BAD_CAST uri->user, BAD_CAST ";:&=+$,"); |
1762 | 531 | NULLCHK(segment) |
1763 | 525 | ret = xmlStrcat(ret,BAD_CAST "//"); |
1764 | 525 | ret = xmlStrcat(ret, segment); |
1765 | 525 | ret = xmlStrcat(ret, BAD_CAST "@"); |
1766 | 525 | xmlFree(segment); |
1767 | 525 | } |
1768 | | |
1769 | 3.22k | if (uri->server) { |
1770 | 898 | segment = xmlURIEscapeStr(BAD_CAST uri->server, BAD_CAST "/?;:@"); |
1771 | 898 | NULLCHK(segment) |
1772 | 894 | if (uri->user == NULL) |
1773 | 463 | ret = xmlStrcat(ret, BAD_CAST "//"); |
1774 | 894 | ret = xmlStrcat(ret, segment); |
1775 | 894 | xmlFree(segment); |
1776 | 894 | } |
1777 | | |
1778 | 3.22k | if (uri->port > 0) { |
1779 | 204 | xmlChar port[11]; |
1780 | | |
1781 | 204 | snprintf((char *) port, 11, "%d", uri->port); |
1782 | 204 | ret = xmlStrcat(ret, BAD_CAST ":"); |
1783 | 204 | ret = xmlStrcat(ret, port); |
1784 | 204 | } |
1785 | | |
1786 | 3.22k | if (uri->path) { |
1787 | 1.12k | segment = |
1788 | 1.12k | xmlURIEscapeStr(BAD_CAST uri->path, BAD_CAST ":@&=+$,/?;"); |
1789 | 1.12k | NULLCHK(segment) |
1790 | 1.11k | ret = xmlStrcat(ret, segment); |
1791 | 1.11k | xmlFree(segment); |
1792 | 1.11k | } |
1793 | | |
1794 | 3.21k | if (uri->query_raw) { |
1795 | 384 | ret = xmlStrcat(ret, BAD_CAST "?"); |
1796 | 384 | ret = xmlStrcat(ret, BAD_CAST uri->query_raw); |
1797 | 384 | } |
1798 | 2.82k | else if (uri->query) { |
1799 | 0 | segment = |
1800 | 0 | xmlURIEscapeStr(BAD_CAST uri->query, BAD_CAST ";/?:@&=+,$"); |
1801 | 0 | NULLCHK(segment) |
1802 | 0 | ret = xmlStrcat(ret, BAD_CAST "?"); |
1803 | 0 | ret = xmlStrcat(ret, segment); |
1804 | 0 | xmlFree(segment); |
1805 | 0 | } |
1806 | | |
1807 | 3.21k | if (uri->opaque) { |
1808 | 0 | segment = xmlURIEscapeStr(BAD_CAST uri->opaque, BAD_CAST ""); |
1809 | 0 | NULLCHK(segment) |
1810 | 0 | ret = xmlStrcat(ret, segment); |
1811 | 0 | xmlFree(segment); |
1812 | 0 | } |
1813 | | |
1814 | 3.21k | if (uri->fragment) { |
1815 | 527 | segment = xmlURIEscapeStr(BAD_CAST uri->fragment, BAD_CAST "#"); |
1816 | 527 | NULLCHK(segment) |
1817 | 518 | ret = xmlStrcat(ret, BAD_CAST "#"); |
1818 | 518 | ret = xmlStrcat(ret, segment); |
1819 | 518 | xmlFree(segment); |
1820 | 518 | } |
1821 | | |
1822 | 3.20k | xmlFreeURI(uri); |
1823 | 3.20k | #undef NULLCHK |
1824 | | |
1825 | 3.20k | return (ret); |
1826 | 3.21k | } |
1827 | | |
1828 | | /************************************************************************ |
1829 | | * * |
1830 | | * Public functions * |
1831 | | * * |
1832 | | ************************************************************************/ |
1833 | | |
1834 | | static int |
1835 | 60.7k | xmlIsAbsolutePath(const xmlChar *path) { |
1836 | 60.7k | int c = path[0]; |
1837 | | |
1838 | 60.7k | if (xmlIsPathSeparator(c, 1)) |
1839 | 5.45k | return(1); |
1840 | | |
1841 | | #if defined(_WIN32) || defined(__CYGWIN__) |
1842 | | if ((((c >= 'A') && (c <= 'Z')) || |
1843 | | ((c >= 'a') && (c <= 'z'))) && |
1844 | | (path[1] == ':')) |
1845 | | return(1); |
1846 | | #endif |
1847 | | |
1848 | 55.3k | return(0); |
1849 | 60.7k | } |
1850 | | |
1851 | | /** |
1852 | | * Resolves a filesystem path from a base path. |
1853 | | * |
1854 | | * @param escRef the filesystem path |
1855 | | * @param base the base value |
1856 | | * @param out pointer to result URI |
1857 | | * @returns 0 on success, -1 if a memory allocation failed or an error |
1858 | | * code if URI or base are invalid. |
1859 | | */ |
1860 | | static int |
1861 | 69.4k | xmlResolvePath(const xmlChar *escRef, const xmlChar *base, xmlChar **out) { |
1862 | 69.4k | const xmlChar *fragment; |
1863 | 69.4k | xmlChar *tmp = NULL; |
1864 | 69.4k | xmlChar *ref = NULL; |
1865 | 69.4k | xmlChar *result = NULL; |
1866 | 69.4k | int ret = -1; |
1867 | 69.4k | int i; |
1868 | | |
1869 | 69.4k | if (out == NULL) |
1870 | 0 | return(1); |
1871 | 69.4k | *out = NULL; |
1872 | | |
1873 | 69.4k | if ((escRef == NULL) || (escRef[0] == 0)) { |
1874 | 8.54k | if ((base == NULL) || (base[0] == 0)) |
1875 | 1.99k | return(1); |
1876 | 6.55k | ref = xmlStrdup(base); |
1877 | 6.55k | if (ref == NULL) |
1878 | 13 | goto err_memory; |
1879 | 6.54k | *out = ref; |
1880 | 6.54k | return(0); |
1881 | 6.55k | } |
1882 | | |
1883 | | /* |
1884 | | * If a URI is resolved, we can assume it is a valid URI and not |
1885 | | * a filesystem path. This means we have to unescape the part |
1886 | | * before the fragment. |
1887 | | */ |
1888 | 60.8k | fragment = xmlStrchr(escRef, '#'); |
1889 | 60.8k | if (fragment != NULL) { |
1890 | 11.1k | tmp = xmlStrndup(escRef, fragment - escRef); |
1891 | 11.1k | if (tmp == NULL) |
1892 | 13 | goto err_memory; |
1893 | 11.1k | escRef = tmp; |
1894 | 11.1k | } |
1895 | | |
1896 | 60.8k | ref = (xmlChar *) xmlURIUnescapeString((char *) escRef, -1, NULL); |
1897 | 60.8k | if (ref == NULL) |
1898 | 70 | goto err_memory; |
1899 | | |
1900 | 60.7k | if ((base == NULL) || (base[0] == 0)) |
1901 | 9.47k | goto done; |
1902 | | |
1903 | 51.2k | if (xmlIsAbsolutePath(ref)) |
1904 | 4.48k | goto done; |
1905 | | |
1906 | | /* |
1907 | | * Remove last segment from base |
1908 | | */ |
1909 | 46.8k | i = xmlStrlen(base); |
1910 | 440k | while ((i > 0) && !xmlIsPathSeparator(base[i-1], 1)) |
1911 | 394k | i--; |
1912 | | |
1913 | | /* |
1914 | | * Concatenate base and ref |
1915 | | */ |
1916 | 46.8k | if (i > 0) { |
1917 | 15.5k | int refLen = xmlStrlen(ref); |
1918 | | |
1919 | 15.5k | result = xmlMalloc(i + refLen + 1); |
1920 | 15.5k | if (result == NULL) |
1921 | 25 | goto err_memory; |
1922 | | |
1923 | 15.5k | memcpy(result, base, i); |
1924 | 15.5k | memcpy(result + i, ref, refLen + 1); |
1925 | 15.5k | } |
1926 | | |
1927 | | /* |
1928 | | * Normalize |
1929 | | */ |
1930 | 46.7k | xmlNormalizePath((char *) result, 1); |
1931 | | |
1932 | 60.7k | done: |
1933 | 60.7k | if (result == NULL) { |
1934 | 45.2k | result = ref; |
1935 | 45.2k | ref = NULL; |
1936 | 45.2k | } |
1937 | | |
1938 | 60.7k | if (fragment != NULL) { |
1939 | 11.1k | result = xmlStrcat(result, fragment); |
1940 | 11.1k | if (result == NULL) |
1941 | 17 | goto err_memory; |
1942 | 11.1k | } |
1943 | | |
1944 | 60.7k | *out = result; |
1945 | 60.7k | ret = 0; |
1946 | | |
1947 | 60.8k | err_memory: |
1948 | 60.8k | xmlFree(tmp); |
1949 | 60.8k | xmlFree(ref); |
1950 | 60.8k | return(ret); |
1951 | 60.7k | } |
1952 | | |
1953 | | /** |
1954 | | * Computes he final URI of the reference done by checking that |
1955 | | * the given URI is valid, and building the final URI using the |
1956 | | * base URI. This is processed according to section 5.2 of the |
1957 | | * RFC 2396 |
1958 | | * |
1959 | | * 5.2. Resolving Relative References to Absolute Form |
1960 | | * |
1961 | | * @since 2.13.0 |
1962 | | * |
1963 | | * @param URI the URI instance found in the document |
1964 | | * @param base the base value |
1965 | | * @param valPtr pointer to result URI |
1966 | | * @returns 0 on success, -1 if a memory allocation failed or an error |
1967 | | * code if URI or base are invalid. |
1968 | | */ |
1969 | | int |
1970 | 223k | xmlBuildURISafe(const xmlChar *URI, const xmlChar *base, xmlChar **valPtr) { |
1971 | 223k | xmlChar *val = NULL; |
1972 | 223k | int ret, len, indx, cur, out; |
1973 | 223k | xmlURIPtr ref = NULL; |
1974 | 223k | xmlURIPtr bas = NULL; |
1975 | 223k | xmlURIPtr res = NULL; |
1976 | | |
1977 | 223k | if (valPtr == NULL) |
1978 | 0 | return(1); |
1979 | 223k | *valPtr = NULL; |
1980 | | |
1981 | 223k | if (URI == NULL) |
1982 | 5.53k | return(1); |
1983 | | |
1984 | 218k | if (base == NULL) { |
1985 | 33.7k | val = xmlStrdup(URI); |
1986 | 33.7k | if (val == NULL) |
1987 | 15 | return(-1); |
1988 | 33.7k | *valPtr = val; |
1989 | 33.7k | return(0); |
1990 | 33.7k | } |
1991 | | |
1992 | | /* |
1993 | | * 1) The URI reference is parsed into the potential four components and |
1994 | | * fragment identifier, as described in Section 4.3. |
1995 | | * |
1996 | | * NOTE that a completely empty URI is treated by modern browsers |
1997 | | * as a reference to "." rather than as a synonym for the current |
1998 | | * URI. Should we do that here? |
1999 | | */ |
2000 | 184k | if (URI[0] != 0) |
2001 | 167k | ret = xmlParseURISafe((const char *) URI, &ref); |
2002 | 16.4k | else |
2003 | 16.4k | ret = 0; |
2004 | 184k | if (ret != 0) |
2005 | 38.5k | goto done; |
2006 | 145k | if ((ref != NULL) && (ref->scheme != NULL)) { |
2007 | | /* |
2008 | | * The URI is absolute don't modify. |
2009 | | */ |
2010 | 12.2k | val = xmlStrdup(URI); |
2011 | 12.2k | if (val == NULL) |
2012 | 32 | ret = -1; |
2013 | 12.2k | goto done; |
2014 | 12.2k | } |
2015 | | |
2016 | | /* |
2017 | | * If base has no scheme or authority, it is assumed to be a |
2018 | | * filesystem path. |
2019 | | */ |
2020 | 133k | if (xmlStrstr(base, BAD_CAST "://") == NULL) { |
2021 | 69.4k | xmlFreeURI(ref); |
2022 | 69.4k | return(xmlResolvePath(URI, base, valPtr)); |
2023 | 69.4k | } |
2024 | | |
2025 | | #if defined(_WIN32) || defined(__CYGWIN__) |
2026 | | /* |
2027 | | * Resolve paths with a Windows drive letter as filesystem path |
2028 | | * even if base has a scheme. |
2029 | | */ |
2030 | | if ((ref != NULL) && (ref->path != NULL)) { |
2031 | | int c = ref->path[0]; |
2032 | | |
2033 | | if ((((c >= 'A') && (c <= 'Z')) || |
2034 | | ((c >= 'a') && (c <= 'z'))) && |
2035 | | (ref->path[1] == ':')) { |
2036 | | xmlFreeURI(ref); |
2037 | | return(xmlResolvePath(URI, base, valPtr)); |
2038 | | } |
2039 | | } |
2040 | | #endif |
2041 | | |
2042 | 64.0k | ret = xmlParseURISafe((const char *) base, &bas); |
2043 | 64.0k | if (ret < 0) |
2044 | 172 | goto done; |
2045 | 63.8k | if (ret != 0) { |
2046 | 13.8k | if (ref) { |
2047 | 12.9k | ret = 0; |
2048 | 12.9k | val = xmlSaveUri(ref); |
2049 | 12.9k | if (val == NULL) |
2050 | 72 | ret = -1; |
2051 | 12.9k | } |
2052 | 13.8k | goto done; |
2053 | 13.8k | } |
2054 | 50.0k | if (ref == NULL) { |
2055 | | /* |
2056 | | * the base fragment must be ignored |
2057 | | */ |
2058 | 6.94k | if (bas->fragment != NULL) { |
2059 | 736 | xmlFree(bas->fragment); |
2060 | 736 | bas->fragment = NULL; |
2061 | 736 | } |
2062 | 6.94k | val = xmlSaveUri(bas); |
2063 | 6.94k | if (val == NULL) |
2064 | 34 | ret = -1; |
2065 | 6.94k | goto done; |
2066 | 6.94k | } |
2067 | | |
2068 | | /* |
2069 | | * 2) If the path component is empty and the scheme, authority, and |
2070 | | * query components are undefined, then it is a reference to the |
2071 | | * current document and we are done. Otherwise, the reference URI's |
2072 | | * query and fragment components are defined as found (or not found) |
2073 | | * within the URI reference and not inherited from the base URI. |
2074 | | * |
2075 | | * NOTE that in modern browsers, the parsing differs from the above |
2076 | | * in the following aspect: the query component is allowed to be |
2077 | | * defined while still treating this as a reference to the current |
2078 | | * document. |
2079 | | */ |
2080 | 43.0k | ret = -1; |
2081 | 43.0k | res = xmlCreateURI(); |
2082 | 43.0k | if (res == NULL) |
2083 | 51 | goto done; |
2084 | 43.0k | if ((ref->scheme == NULL) && (ref->path == NULL) && |
2085 | 43.0k | ((ref->authority == NULL) && (ref->server == NULL) && |
2086 | 14.0k | (ref->port == PORT_EMPTY))) { |
2087 | 11.0k | if (bas->scheme != NULL) { |
2088 | 7.44k | res->scheme = xmlMemStrdup(bas->scheme); |
2089 | 7.44k | if (res->scheme == NULL) |
2090 | 27 | goto done; |
2091 | 7.44k | } |
2092 | 11.0k | if (bas->authority != NULL) { |
2093 | 0 | res->authority = xmlMemStrdup(bas->authority); |
2094 | 0 | if (res->authority == NULL) |
2095 | 0 | goto done; |
2096 | 11.0k | } else { |
2097 | 11.0k | if (bas->server != NULL) { |
2098 | 5.00k | res->server = xmlMemStrdup(bas->server); |
2099 | 5.00k | if (res->server == NULL) |
2100 | 21 | goto done; |
2101 | 5.00k | } |
2102 | 11.0k | if (bas->user != NULL) { |
2103 | 3.39k | res->user = xmlMemStrdup(bas->user); |
2104 | 3.39k | if (res->user == NULL) |
2105 | 11 | goto done; |
2106 | 3.39k | } |
2107 | 11.0k | res->port = bas->port; |
2108 | 11.0k | } |
2109 | 11.0k | if (bas->path != NULL) { |
2110 | 5.41k | res->path = xmlMemStrdup(bas->path); |
2111 | 5.41k | if (res->path == NULL) |
2112 | 9 | goto done; |
2113 | 5.41k | } |
2114 | 10.9k | if (ref->query_raw != NULL) { |
2115 | 1.96k | res->query_raw = xmlMemStrdup (ref->query_raw); |
2116 | 1.96k | if (res->query_raw == NULL) |
2117 | 12 | goto done; |
2118 | 9.03k | } else if (ref->query != NULL) { |
2119 | 0 | res->query = xmlMemStrdup(ref->query); |
2120 | 0 | if (res->query == NULL) |
2121 | 0 | goto done; |
2122 | 9.03k | } else if (bas->query_raw != NULL) { |
2123 | 3.85k | res->query_raw = xmlMemStrdup(bas->query_raw); |
2124 | 3.85k | if (res->query_raw == NULL) |
2125 | 8 | goto done; |
2126 | 5.17k | } else if (bas->query != NULL) { |
2127 | 0 | res->query = xmlMemStrdup(bas->query); |
2128 | 0 | if (res->query == NULL) |
2129 | 0 | goto done; |
2130 | 0 | } |
2131 | 10.9k | if (ref->fragment != NULL) { |
2132 | 9.38k | res->fragment = xmlMemStrdup(ref->fragment); |
2133 | 9.38k | if (res->fragment == NULL) |
2134 | 28 | goto done; |
2135 | 9.38k | } |
2136 | 10.9k | goto step_7; |
2137 | 10.9k | } |
2138 | | |
2139 | | /* |
2140 | | * 3) If the scheme component is defined, indicating that the reference |
2141 | | * starts with a scheme name, then the reference is interpreted as an |
2142 | | * absolute URI and we are done. Otherwise, the reference URI's |
2143 | | * scheme is inherited from the base URI's scheme component. |
2144 | | */ |
2145 | 31.9k | if (ref->scheme != NULL) { |
2146 | 0 | val = xmlSaveUri(ref); |
2147 | 0 | if (val != NULL) |
2148 | 0 | ret = 0; |
2149 | 0 | goto done; |
2150 | 0 | } |
2151 | 31.9k | if (bas->scheme != NULL) { |
2152 | 15.1k | res->scheme = xmlMemStrdup(bas->scheme); |
2153 | 15.1k | if (res->scheme == NULL) |
2154 | 33 | goto done; |
2155 | 15.1k | } |
2156 | | |
2157 | 31.9k | if (ref->query_raw != NULL) { |
2158 | 5.39k | res->query_raw = xmlMemStrdup(ref->query_raw); |
2159 | 5.39k | if (res->query_raw == NULL) |
2160 | 10 | goto done; |
2161 | 26.5k | } else if (ref->query != NULL) { |
2162 | 0 | res->query = xmlMemStrdup(ref->query); |
2163 | 0 | if (res->query == NULL) |
2164 | 0 | goto done; |
2165 | 0 | } |
2166 | 31.9k | if (ref->fragment != NULL) { |
2167 | 10.0k | res->fragment = xmlMemStrdup(ref->fragment); |
2168 | 10.0k | if (res->fragment == NULL) |
2169 | 10 | goto done; |
2170 | 10.0k | } |
2171 | | |
2172 | | /* |
2173 | | * 4) If the authority component is defined, then the reference is a |
2174 | | * network-path and we skip to step 7. Otherwise, the reference |
2175 | | * URI's authority is inherited from the base URI's authority |
2176 | | * component, which will also be undefined if the URI scheme does not |
2177 | | * use an authority component. |
2178 | | */ |
2179 | 31.9k | if ((ref->authority != NULL) || (ref->server != NULL) || |
2180 | 31.9k | (ref->port != PORT_EMPTY)) { |
2181 | 4.59k | if (ref->authority != NULL) { |
2182 | 0 | res->authority = xmlMemStrdup(ref->authority); |
2183 | 0 | if (res->authority == NULL) |
2184 | 0 | goto done; |
2185 | 4.59k | } else { |
2186 | 4.59k | if (ref->server != NULL) { |
2187 | 3.91k | res->server = xmlMemStrdup(ref->server); |
2188 | 3.91k | if (res->server == NULL) |
2189 | 8 | goto done; |
2190 | 3.91k | } |
2191 | 4.58k | if (ref->user != NULL) { |
2192 | 2.55k | res->user = xmlMemStrdup(ref->user); |
2193 | 2.55k | if (res->user == NULL) |
2194 | 7 | goto done; |
2195 | 2.55k | } |
2196 | 4.57k | res->port = ref->port; |
2197 | 4.57k | } |
2198 | 4.57k | if (ref->path != NULL) { |
2199 | 1.64k | res->path = xmlMemStrdup(ref->path); |
2200 | 1.64k | if (res->path == NULL) |
2201 | 9 | goto done; |
2202 | 1.64k | } |
2203 | 4.56k | goto step_7; |
2204 | 4.57k | } |
2205 | 27.3k | if (bas->authority != NULL) { |
2206 | 0 | res->authority = xmlMemStrdup(bas->authority); |
2207 | 0 | if (res->authority == NULL) |
2208 | 0 | goto done; |
2209 | 27.3k | } else if ((bas->server != NULL) || (bas->port != PORT_EMPTY)) { |
2210 | 11.3k | if (bas->server != NULL) { |
2211 | 8.84k | res->server = xmlMemStrdup(bas->server); |
2212 | 8.84k | if (res->server == NULL) |
2213 | 14 | goto done; |
2214 | 8.84k | } |
2215 | 11.2k | if (bas->user != NULL) { |
2216 | 3.43k | res->user = xmlMemStrdup(bas->user); |
2217 | 3.43k | if (res->user == NULL) |
2218 | 11 | goto done; |
2219 | 3.43k | } |
2220 | 11.2k | res->port = bas->port; |
2221 | 11.2k | } |
2222 | | |
2223 | | /* |
2224 | | * 5) If the path component begins with a slash character ("/"), then |
2225 | | * the reference is an absolute-path and we skip to step 7. |
2226 | | */ |
2227 | 27.2k | if ((ref->path != NULL) && (ref->path[0] == '/')) { |
2228 | 2.39k | res->path = xmlMemStrdup(ref->path); |
2229 | 2.39k | if (res->path == NULL) |
2230 | 14 | goto done; |
2231 | 2.38k | goto step_7; |
2232 | 2.39k | } |
2233 | | |
2234 | | |
2235 | | /* |
2236 | | * 6) If this step is reached, then we are resolving a relative-path |
2237 | | * reference. The relative path needs to be merged with the base |
2238 | | * URI's path. Although there are many ways to do this, we will |
2239 | | * describe a simple method using a separate string buffer. |
2240 | | * |
2241 | | * Allocate a buffer large enough for the result string. |
2242 | | */ |
2243 | 24.8k | len = 2; /* extra / and 0 */ |
2244 | 24.8k | if (ref->path != NULL) |
2245 | 24.8k | len += strlen(ref->path); |
2246 | 24.8k | if (bas->path != NULL) |
2247 | 17.5k | len += strlen(bas->path); |
2248 | 24.8k | res->path = xmlMalloc(len); |
2249 | 24.8k | if (res->path == NULL) |
2250 | 42 | goto done; |
2251 | 24.8k | res->path[0] = 0; |
2252 | | |
2253 | | /* |
2254 | | * a) All but the last segment of the base URI's path component is |
2255 | | * copied to the buffer. In other words, any characters after the |
2256 | | * last (right-most) slash character, if any, are excluded. |
2257 | | */ |
2258 | 24.8k | cur = 0; |
2259 | 24.8k | out = 0; |
2260 | 24.8k | if (bas->path != NULL) { |
2261 | 91.6k | while (bas->path[cur] != 0) { |
2262 | 505k | while ((bas->path[cur] != 0) && (bas->path[cur] != '/')) |
2263 | 417k | cur++; |
2264 | 87.9k | if (bas->path[cur] == 0) |
2265 | 13.8k | break; |
2266 | | |
2267 | 74.1k | cur++; |
2268 | 442k | while (out < cur) { |
2269 | 368k | res->path[out] = bas->path[out]; |
2270 | 368k | out++; |
2271 | 368k | } |
2272 | 74.1k | } |
2273 | 17.5k | } |
2274 | 24.8k | res->path[out] = 0; |
2275 | | |
2276 | | /* |
2277 | | * b) The reference's path component is appended to the buffer |
2278 | | * string. |
2279 | | */ |
2280 | 24.8k | if (ref->path != NULL && ref->path[0] != 0) { |
2281 | 24.2k | indx = 0; |
2282 | | /* |
2283 | | * Ensure the path includes a '/' |
2284 | | */ |
2285 | 24.2k | if ((out == 0) && ((bas->server != NULL) || bas->port != PORT_EMPTY)) |
2286 | 6.63k | res->path[out++] = '/'; |
2287 | 492k | while (ref->path[indx] != 0) { |
2288 | 467k | res->path[out++] = ref->path[indx++]; |
2289 | 467k | } |
2290 | 24.2k | } |
2291 | 24.8k | res->path[out] = 0; |
2292 | | |
2293 | | /* |
2294 | | * Steps c) to h) are really path normalization steps |
2295 | | */ |
2296 | 24.8k | xmlNormalizeURIPath(res->path); |
2297 | | |
2298 | 42.7k | step_7: |
2299 | | |
2300 | | /* |
2301 | | * 7) The resulting URI components, including any inherited from the |
2302 | | * base URI, are recombined to give the absolute form of the URI |
2303 | | * reference. |
2304 | | */ |
2305 | 42.7k | val = xmlSaveUri(res); |
2306 | 42.7k | if (val != NULL) |
2307 | 42.6k | ret = 0; |
2308 | | |
2309 | 114k | done: |
2310 | 114k | if (ref != NULL) |
2311 | 68.3k | xmlFreeURI(ref); |
2312 | 114k | if (bas != NULL) |
2313 | 50.0k | xmlFreeURI(bas); |
2314 | 114k | if (res != NULL) |
2315 | 43.0k | xmlFreeURI(res); |
2316 | 114k | *valPtr = val; |
2317 | 114k | return(ret); |
2318 | 42.7k | } |
2319 | | |
2320 | | /** |
2321 | | * Computes he final URI of the reference done by checking that |
2322 | | * the given URI is valid, and building the final URI using the |
2323 | | * base URI. This is processed according to section 5.2 of the |
2324 | | * RFC 2396 |
2325 | | * |
2326 | | * 5.2. Resolving Relative References to Absolute Form |
2327 | | * |
2328 | | * @param URI the URI instance found in the document |
2329 | | * @param base the base value |
2330 | | * @returns a new URI string (to be freed by the caller) or NULL in case |
2331 | | * of error. |
2332 | | */ |
2333 | | xmlChar * |
2334 | 27.8k | xmlBuildURI(const xmlChar *URI, const xmlChar *base) { |
2335 | 27.8k | xmlChar *out; |
2336 | | |
2337 | 27.8k | xmlBuildURISafe(URI, base, &out); |
2338 | 27.8k | return(out); |
2339 | 27.8k | } |
2340 | | |
2341 | | static int |
2342 | 13.8k | xmlParseUriOrPath(const char *str, xmlURIPtr *out, int *drive) { |
2343 | 13.8k | xmlURIPtr uri; |
2344 | 13.8k | char *buf = NULL; |
2345 | 13.8k | int ret; |
2346 | | |
2347 | 13.8k | *out = NULL; |
2348 | 13.8k | *drive = 0; |
2349 | | |
2350 | 13.8k | uri = xmlCreateURI(); |
2351 | 13.8k | if (uri == NULL) { |
2352 | 40 | ret = -1; |
2353 | 40 | goto done; |
2354 | 40 | } |
2355 | | |
2356 | 13.8k | if (xmlStrstr(BAD_CAST str, BAD_CAST "://") == NULL) { |
2357 | 9.50k | const char *path; |
2358 | 9.50k | size_t pathSize; |
2359 | 9.50k | int prependSlash = 0; |
2360 | | |
2361 | 9.50k | buf = xmlMemStrdup(str); |
2362 | 9.50k | if (buf == NULL) { |
2363 | 22 | ret = -1; |
2364 | 22 | goto done; |
2365 | 22 | } |
2366 | 9.48k | xmlNormalizePath(buf, /* isFile */ 1); |
2367 | | |
2368 | 9.48k | path = buf; |
2369 | | |
2370 | 9.48k | if (xmlIsAbsolutePath(BAD_CAST buf)) { |
2371 | | #if defined(_WIN32) || defined(__CYGWIN__) |
2372 | | const char *server = NULL; |
2373 | | int isFileScheme = 0; |
2374 | | #endif |
2375 | | |
2376 | | #if defined(_WIN32) || defined(__CYGWIN__) |
2377 | | if (strncmp(buf, "//?/UNC/", 8) == 0) { |
2378 | | server = buf + 8; |
2379 | | isFileScheme = 1; |
2380 | | } else if (strncmp(buf, "//?/", 4) == 0) { |
2381 | | path = buf + 3; |
2382 | | isFileScheme = 1; |
2383 | | } else if (strncmp(buf, "//", 2) == 0) { |
2384 | | server = buf + 2; |
2385 | | isFileScheme = 1; |
2386 | | } |
2387 | | |
2388 | | if (server != NULL) { |
2389 | | const char *end = strchr(server, '/'); |
2390 | | |
2391 | | if (end == NULL) { |
2392 | | uri->server = xmlMemStrdup(server); |
2393 | | path = "/"; |
2394 | | } else { |
2395 | | uri->server = (char *) xmlStrndup(BAD_CAST server, |
2396 | | end - server); |
2397 | | path = end; |
2398 | | } |
2399 | | if (uri->server == NULL) { |
2400 | | ret = -1; |
2401 | | goto done; |
2402 | | } |
2403 | | } |
2404 | | |
2405 | | if ((((path[0] >= 'A') && (path[0] <= 'Z')) || |
2406 | | ((path[0] >= 'a') && (path[0] <= 'z'))) && |
2407 | | (path[1] == ':')) { |
2408 | | prependSlash = 1; |
2409 | | isFileScheme = 1; |
2410 | | } |
2411 | | |
2412 | | if (isFileScheme) { |
2413 | | uri->scheme = xmlMemStrdup("file"); |
2414 | | if (uri->scheme == NULL) { |
2415 | | ret = -1; |
2416 | | goto done; |
2417 | | } |
2418 | | |
2419 | | if (uri->server == NULL) |
2420 | | uri->port = PORT_EMPTY_SERVER; |
2421 | | } |
2422 | | #endif |
2423 | 972 | } |
2424 | | |
2425 | 9.48k | pathSize = strlen(path); |
2426 | 9.48k | uri->path = xmlMalloc(pathSize + prependSlash + 1); |
2427 | 9.48k | if (uri->path == NULL) { |
2428 | 20 | ret = -1; |
2429 | 20 | goto done; |
2430 | 20 | } |
2431 | 9.46k | if (prependSlash) { |
2432 | 0 | uri->path[0] = '/'; |
2433 | 0 | memcpy(uri->path + 1, path, pathSize + 1); |
2434 | 9.46k | } else { |
2435 | 9.46k | memcpy(uri->path, path, pathSize + 1); |
2436 | 9.46k | } |
2437 | 9.46k | } else { |
2438 | 4.32k | ret = xmlParseURIReference(uri, str); |
2439 | 4.32k | if (ret != 0) |
2440 | 1.84k | goto done; |
2441 | | |
2442 | 2.47k | xmlNormalizePath(uri->path, /* isFile */ 0); |
2443 | 2.47k | } |
2444 | | |
2445 | | #if defined(_WIN32) || defined(__CYGWIN__) |
2446 | | if ((uri->path[0] == '/') && |
2447 | | (((uri->path[1] >= 'A') && (uri->path[1] <= 'Z')) || |
2448 | | ((uri->path[1] >= 'a') && (uri->path[1] <= 'z'))) && |
2449 | | (uri->path[2] == ':')) |
2450 | | *drive = uri->path[1]; |
2451 | | #endif |
2452 | | |
2453 | 11.9k | *out = uri; |
2454 | 11.9k | uri = NULL; |
2455 | 11.9k | ret = 0; |
2456 | | |
2457 | 13.8k | done: |
2458 | 13.8k | xmlFreeURI(uri); |
2459 | 13.8k | xmlFree(buf); |
2460 | | |
2461 | 13.8k | return(ret); |
2462 | 11.9k | } |
2463 | | |
2464 | | /** |
2465 | | * Expresses the URI of the reference in terms relative to the |
2466 | | * base. Some examples of this operation include: |
2467 | | * |
2468 | | * base = "http://site1.com/docs/book1.html" |
2469 | | * URI input URI returned |
2470 | | * http://site1.com/docs/pic1.gif pic1.gif |
2471 | | * http://site2.com/docs/pic1.gif http://site2.com/docs/pic1.gif |
2472 | | * |
2473 | | * base = "docs/book1.html" |
2474 | | * URI input URI returned |
2475 | | * docs/pic1.gif pic1.gif |
2476 | | * docs/img/pic1.gif img/pic1.gif |
2477 | | * img/pic1.gif ../img/pic1.gif |
2478 | | * http://site1.com/docs/pic1.gif http://site1.com/docs/pic1.gif |
2479 | | * |
2480 | | * @since 2.13.0 |
2481 | | * |
2482 | | * @param URI the URI reference under consideration |
2483 | | * @param base the base value |
2484 | | * @param valPtr pointer to result URI |
2485 | | * @returns 0 on success, -1 if a memory allocation failed or an error |
2486 | | * code if URI or base are invalid. |
2487 | | */ |
2488 | | int |
2489 | | xmlBuildRelativeURISafe(const xmlChar * URI, const xmlChar * base, |
2490 | | xmlChar **valPtr) |
2491 | 15.3k | { |
2492 | 15.3k | xmlChar *val = NULL; |
2493 | 15.3k | int ret = 0; |
2494 | 15.3k | int ix; |
2495 | 15.3k | int nbslash = 0; |
2496 | 15.3k | int len; |
2497 | 15.3k | xmlURIPtr ref = NULL; |
2498 | 15.3k | xmlURIPtr bas = NULL; |
2499 | 15.3k | const xmlChar *bptr, *uptr, *rptr; |
2500 | 15.3k | xmlChar *vptr; |
2501 | 15.3k | int remove_path = 0; |
2502 | 15.3k | int refDrive, baseDrive; |
2503 | | |
2504 | 15.3k | if (valPtr == NULL) |
2505 | 0 | return(1); |
2506 | 15.3k | *valPtr = NULL; |
2507 | 15.3k | if ((URI == NULL) || (*URI == 0)) |
2508 | 5.60k | return(1); |
2509 | | |
2510 | 9.70k | ret = xmlParseUriOrPath((char *) URI, &ref, &refDrive); |
2511 | 9.70k | if (ret < 0) |
2512 | 64 | goto done; |
2513 | 9.63k | if (ret != 0) { |
2514 | | /* Return URI if URI is invalid */ |
2515 | 1.06k | ret = 0; |
2516 | 1.06k | val = xmlStrdup(URI); |
2517 | 1.06k | if (val == NULL) |
2518 | 3 | ret = -1; |
2519 | 1.06k | goto done; |
2520 | 1.06k | } |
2521 | | |
2522 | | /* Return URI if base is empty */ |
2523 | 8.57k | if ((base == NULL) || (*base == 0)) |
2524 | 4.40k | goto done; |
2525 | | |
2526 | 4.16k | ret = xmlParseUriOrPath((char *) base, &bas, &baseDrive); |
2527 | 4.16k | if (ret < 0) |
2528 | 42 | goto done; |
2529 | 4.12k | if (ret != 0) { |
2530 | | /* Return URI if base is invalid */ |
2531 | 753 | ret = 0; |
2532 | 753 | goto done; |
2533 | 753 | } |
2534 | | |
2535 | | /* |
2536 | | * If the scheme / server on the URI differs from the base, |
2537 | | * just return the URI |
2538 | | */ |
2539 | 3.37k | if ((xmlStrcmp ((xmlChar *)bas->scheme, (xmlChar *)ref->scheme)) || |
2540 | 3.37k | (xmlStrcmp ((xmlChar *)bas->server, (xmlChar *)ref->server)) || |
2541 | 3.37k | (bas->port != ref->port) || |
2542 | 3.37k | (baseDrive != refDrive)) { |
2543 | 1.26k | goto done; |
2544 | 1.26k | } |
2545 | 2.11k | if (xmlStrEqual((xmlChar *)bas->path, (xmlChar *)ref->path)) { |
2546 | 154 | val = xmlStrdup(BAD_CAST ""); |
2547 | 154 | if (val == NULL) |
2548 | 2 | ret = -1; |
2549 | 154 | goto done; |
2550 | 154 | } |
2551 | 1.95k | if (bas->path == NULL) { |
2552 | 172 | val = xmlStrdup((xmlChar *)ref->path); |
2553 | 172 | if (val == NULL) { |
2554 | 5 | ret = -1; |
2555 | 5 | goto done; |
2556 | 5 | } |
2557 | 167 | goto escape; |
2558 | 172 | } |
2559 | 1.78k | if (ref->path == NULL) { |
2560 | 8 | ref->path = (char *) "/"; |
2561 | 8 | remove_path = 1; |
2562 | 8 | } |
2563 | | |
2564 | 1.78k | bptr = (xmlChar *) bas->path; |
2565 | 1.78k | rptr = (xmlChar *) ref->path; |
2566 | | |
2567 | | /* |
2568 | | * Return URI if URI and base aren't both absolute or relative. |
2569 | | */ |
2570 | 1.78k | if ((bptr[0] == '/') != (rptr[0] == '/')) |
2571 | 203 | goto done; |
2572 | | |
2573 | | /* |
2574 | | * At this point we can compare the two paths |
2575 | | */ |
2576 | 1.58k | { |
2577 | 1.58k | int pos = 0; |
2578 | | |
2579 | | /* |
2580 | | * Next we compare the two strings and find where they first differ |
2581 | | */ |
2582 | 24.8k | while ((bptr[pos] == rptr[pos]) && (bptr[pos] != 0)) |
2583 | 23.2k | pos++; |
2584 | | |
2585 | 1.58k | if (bptr[pos] == rptr[pos]) { |
2586 | 5 | val = xmlStrdup(BAD_CAST ""); |
2587 | 5 | if (val == NULL) |
2588 | 1 | ret = -1; |
2589 | 5 | goto done; /* (I can't imagine why anyone would do this) */ |
2590 | 5 | } |
2591 | | |
2592 | | /* |
2593 | | * In URI, "back up" to the last '/' encountered. This will be the |
2594 | | * beginning of the "unique" suffix of URI |
2595 | | */ |
2596 | 1.57k | ix = pos; |
2597 | 24.0k | for (; ix > 0; ix--) { |
2598 | 22.6k | if (rptr[ix - 1] == '/') |
2599 | 158 | break; |
2600 | 22.6k | } |
2601 | 1.57k | uptr = (xmlChar *)&rptr[ix]; |
2602 | | |
2603 | | /* |
2604 | | * In base, count the number of '/' from the differing point |
2605 | | */ |
2606 | 337k | for (; bptr[ix] != 0; ix++) { |
2607 | 335k | if (bptr[ix] == '/') |
2608 | 60.7k | nbslash++; |
2609 | 335k | } |
2610 | | |
2611 | | /* |
2612 | | * e.g: URI="foo/" base="foo/bar" -> "./" |
2613 | | */ |
2614 | 1.57k | if (nbslash == 0 && !uptr[0]) { |
2615 | 5 | val = xmlStrdup(BAD_CAST "./"); |
2616 | 5 | if (val == NULL) |
2617 | 1 | ret = -1; |
2618 | 5 | goto done; |
2619 | 5 | } |
2620 | | |
2621 | 1.57k | len = xmlStrlen (uptr) + 1; |
2622 | 1.57k | } |
2623 | | |
2624 | 1.57k | if (nbslash == 0) { |
2625 | 704 | if (uptr != NULL) { |
2626 | | /* exception characters from xmlSaveUri */ |
2627 | 704 | val = xmlURIEscapeStr(uptr, BAD_CAST "/;&=+$,"); |
2628 | 704 | if (val == NULL) |
2629 | 5 | ret = -1; |
2630 | 704 | } |
2631 | 704 | goto done; |
2632 | 704 | } |
2633 | | |
2634 | | /* |
2635 | | * Allocate just enough space for the returned string - |
2636 | | * length of the remainder of the URI, plus enough space |
2637 | | * for the "../" groups, plus one for the terminator |
2638 | | */ |
2639 | 868 | val = (xmlChar *) xmlMalloc (len + 3 * nbslash); |
2640 | 868 | if (val == NULL) { |
2641 | 6 | ret = -1; |
2642 | 6 | goto done; |
2643 | 6 | } |
2644 | 862 | vptr = val; |
2645 | | /* |
2646 | | * Put in as many "../" as needed |
2647 | | */ |
2648 | 61.5k | for (; nbslash>0; nbslash--) { |
2649 | 60.7k | *vptr++ = '.'; |
2650 | 60.7k | *vptr++ = '.'; |
2651 | 60.7k | *vptr++ = '/'; |
2652 | 60.7k | } |
2653 | | /* |
2654 | | * Finish up with the end of the URI |
2655 | | */ |
2656 | 862 | if (uptr != NULL) { |
2657 | 862 | if ((vptr > val) && (len > 0) && |
2658 | 862 | (uptr[0] == '/') && (vptr[-1] == '/')) { |
2659 | 0 | memcpy (vptr, uptr + 1, len - 1); |
2660 | 0 | vptr[len - 2] = 0; |
2661 | 862 | } else { |
2662 | 862 | memcpy (vptr, uptr, len); |
2663 | 862 | vptr[len - 1] = 0; |
2664 | 862 | } |
2665 | 862 | } else { |
2666 | 0 | vptr[len - 1] = 0; |
2667 | 0 | } |
2668 | | |
2669 | 1.02k | escape: |
2670 | | /* escape the freshly-built path */ |
2671 | 1.02k | vptr = val; |
2672 | | /* exception characters from xmlSaveUri */ |
2673 | 1.02k | val = xmlURIEscapeStr(vptr, BAD_CAST "/;&=+$,"); |
2674 | 1.02k | if (val == NULL) |
2675 | 5 | ret = -1; |
2676 | 1.02k | else |
2677 | 1.02k | ret = 0; |
2678 | 1.02k | xmlFree(vptr); |
2679 | | |
2680 | 9.70k | done: |
2681 | 9.70k | if ((ret == 0) && (val == NULL)) { |
2682 | 6.62k | val = xmlSaveUri(ref); |
2683 | 6.62k | if (val == NULL) |
2684 | 12 | ret = -1; |
2685 | 6.62k | } |
2686 | | |
2687 | | /* |
2688 | | * Free the working variables |
2689 | | */ |
2690 | 9.70k | if (remove_path != 0) |
2691 | 8 | ref->path = NULL; |
2692 | 9.70k | if (ref != NULL) |
2693 | 8.57k | xmlFreeURI (ref); |
2694 | 9.70k | if (bas != NULL) |
2695 | 3.37k | xmlFreeURI (bas); |
2696 | 9.70k | if (ret != 0) { |
2697 | 146 | xmlFree(val); |
2698 | 146 | val = NULL; |
2699 | 146 | } |
2700 | | |
2701 | 9.70k | *valPtr = val; |
2702 | 9.70k | return(ret); |
2703 | 1.02k | } |
2704 | | |
2705 | | /** |
2706 | | * See #xmlBuildRelativeURISafe. |
2707 | | * |
2708 | | * @param URI the URI reference under consideration |
2709 | | * @param base the base value |
2710 | | * @returns a new URI string (to be freed by the caller) or NULL in case |
2711 | | * error. |
2712 | | */ |
2713 | | xmlChar * |
2714 | | xmlBuildRelativeURI(const xmlChar * URI, const xmlChar * base) |
2715 | 4.56k | { |
2716 | 4.56k | xmlChar *val; |
2717 | | |
2718 | 4.56k | xmlBuildRelativeURISafe(URI, base, &val); |
2719 | 4.56k | return(val); |
2720 | 4.56k | } |
2721 | | |
2722 | | /** |
2723 | | * Prepares a path. |
2724 | | * |
2725 | | * If the path contains the substring "://", it is considered a |
2726 | | * Legacy Extended IRI. Characters which aren't allowed in URIs are |
2727 | | * escaped. |
2728 | | * |
2729 | | * Otherwise, the path is considered a filesystem path which is |
2730 | | * copied without modification. |
2731 | | * |
2732 | | * The caller is responsible for freeing the memory occupied |
2733 | | * by the returned string. If there is insufficient memory available, or the |
2734 | | * argument is NULL, the function returns NULL. |
2735 | | * |
2736 | | * @param path the resource locator in a filesystem notation |
2737 | | * @returns the escaped path. |
2738 | | */ |
2739 | | xmlChar * |
2740 | | xmlCanonicPath(const xmlChar *path) |
2741 | 299k | { |
2742 | 299k | xmlChar *ret; |
2743 | | |
2744 | 299k | if (path == NULL) |
2745 | 1.08k | return(NULL); |
2746 | | |
2747 | | /* Check if this is an "absolute uri" */ |
2748 | 298k | if (xmlStrstr(path, BAD_CAST "://") != NULL) { |
2749 | | /* |
2750 | | * Escape all characters except reserved, unreserved and the |
2751 | | * percent sign. |
2752 | | * |
2753 | | * xmlURIEscapeStr already keeps unreserved characters, so we |
2754 | | * pass gen-delims, sub-delims and "%" to ignore. |
2755 | | */ |
2756 | 25.1k | ret = xmlURIEscapeStr(path, BAD_CAST ":/?#[]@!$&()*+,;='%"); |
2757 | 273k | } else { |
2758 | 273k | ret = xmlStrdup((const xmlChar *) path); |
2759 | 273k | } |
2760 | | |
2761 | 298k | return(ret); |
2762 | 299k | } |
2763 | | |
2764 | | /** |
2765 | | * Constructs an URI expressing the existing path |
2766 | | * |
2767 | | * @param path the resource locator in a filesystem notation |
2768 | | * @returns a new URI, or a duplicate of the path parameter if the |
2769 | | * construction fails. The caller is responsible for freeing the memory |
2770 | | * occupied by the returned string. If there is insufficient memory available, |
2771 | | * or the argument is NULL, the function returns NULL. |
2772 | | */ |
2773 | | xmlChar * |
2774 | | xmlPathToURI(const xmlChar *path) |
2775 | 254k | { |
2776 | 254k | return(xmlCanonicPath(path)); |
2777 | 254k | } |