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