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 | 5.56k | #define MAX_URI_LENGTH 1024 * 1024 |
36 | | |
37 | 943k | #define PORT_EMPTY 0 |
38 | 6.87k | #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 | 203M | #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 | 203M | #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 | 99.6M | #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 | 99.4M | #define IS_DIGIT(x) (((x) >= '0') && ((x) <= '9')) |
70 | | |
71 | | /* |
72 | | * alphanum = alpha | digit |
73 | | */ |
74 | 203M | #define IS_ALPHANUM(x) (IS_ALPHA(x) || IS_DIGIT(x)) |
75 | | |
76 | | /* |
77 | | * mark = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")" |
78 | | */ |
79 | | |
80 | 99.2M | #define IS_MARK(x) (((x) == '-') || ((x) == '_') || ((x) == '.') || \ |
81 | 99.2M | ((x) == '!') || ((x) == '~') || ((x) == '*') || ((x) == '\'') || \ |
82 | 99.2M | ((x) == '(') || ((x) == ')')) |
83 | | |
84 | | /* |
85 | | * unwise = "{" | "}" | "|" | "\" | "^" | "`" |
86 | | */ |
87 | | #define IS_UNWISE(p) \ |
88 | 118k | (((*(p) == '{')) || ((*(p) == '}')) || ((*(p) == '|')) || \ |
89 | 118k | ((*(p) == '\\')) || ((*(p) == '^')) || ((*(p) == '[')) || \ |
90 | 118k | ((*(p) == ']')) || ((*(p) == '`'))) |
91 | | |
92 | | /* |
93 | | * reserved = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" | "$" | "," | |
94 | | * "[" | "]" |
95 | | */ |
96 | 88.2k | #define IS_RESERVED(x) (((x) == ';') || ((x) == '/') || ((x) == '?') || \ |
97 | 88.2k | ((x) == ':') || ((x) == '@') || ((x) == '&') || ((x) == '=') || \ |
98 | 88.2k | ((x) == '+') || ((x) == '$') || ((x) == ',') || ((x) == '[') || \ |
99 | 88.2k | ((x) == ']')) |
100 | | |
101 | | /* |
102 | | * unreserved = alphanum | mark |
103 | | */ |
104 | 101M | #define IS_UNRESERVED(x) (IS_ALPHANUM(x) || IS_MARK(x)) |
105 | | |
106 | | /* |
107 | | * Skip to next pointer char, handle escaped sequences |
108 | | */ |
109 | 12.9M | #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 | 704k | #define STRNDUP(s, n) (char *) xmlStrndup((const xmlChar *)(s), (n)) |
121 | | |
122 | | /************************************************************************ |
123 | | * * |
124 | | * RFC 3986 parser * |
125 | | * * |
126 | | ************************************************************************/ |
127 | | |
128 | 10.9M | #define ISA_DIGIT(p) ((*(p) >= '0') && (*(p) <= '9')) |
129 | 21.2M | #define ISA_ALPHA(p) (((*(p) >= 'a') && (*(p) <= 'z')) || \ |
130 | 21.2M | ((*(p) >= 'A') && (*(p) <= 'Z'))) |
131 | | #define ISA_HEXDIG(p) \ |
132 | 587k | (ISA_DIGIT(p) || ((*(p) >= 'a') && (*(p) <= 'f')) || \ |
133 | 587k | ((*(p) >= 'A') && (*(p) <= 'F'))) |
134 | | |
135 | | /* |
136 | | * sub-delims = "!" / "$" / "&" / "'" / "(" / ")" |
137 | | * / "*" / "+" / "," / ";" / "=" |
138 | | */ |
139 | | #define ISA_SUB_DELIM(p) \ |
140 | 17.8M | (((*(p) == '!')) || ((*(p) == '$')) || ((*(p) == '&')) || \ |
141 | 3.75M | ((*(p) == '(')) || ((*(p) == ')')) || ((*(p) == '*')) || \ |
142 | 3.75M | ((*(p) == '+')) || ((*(p) == ',')) || ((*(p) == ';')) || \ |
143 | 3.75M | ((*(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 | 15.6M | ((ISA_ALPHA(p)) || (ISA_DIGIT(p)) || ((*(p) == '-')) || \ |
163 | 15.6M | ((*(p) == '.')) || ((*(p) == '_')) || ((*(p) == '~'))) |
164 | | |
165 | | /* |
166 | | * pct-encoded = "%" HEXDIG HEXDIG |
167 | | */ |
168 | | #define ISA_PCT_ENCODED(p) \ |
169 | 19.6M | ((*(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 | 17.6M | (ISA_UNRESERVED(u, p) || ISA_PCT_ENCODED(p) || ISA_SUB_DELIM(p) || \ |
176 | 12.4M | ((*(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 | 31.2M | #define ISA_UNRESERVED(u, p) (xmlIsUnreserved(u, p)) |
191 | | |
192 | 3.98M | #define XML_URI_ALLOW_UNWISE 1 |
193 | 1.02M | #define XML_URI_NO_UNESCAPE 2 |
194 | 3.86M | #define XML_URI_ALLOW_UCSCHAR 4 |
195 | | |
196 | | static int |
197 | 15.6M | xmlIsUnreserved(xmlURIPtr uri, const char *cur) { |
198 | 15.6M | if (uri == NULL) |
199 | 0 | return(0); |
200 | | |
201 | 15.6M | if (ISA_STRICTLY_UNRESERVED(cur)) |
202 | 11.6M | return(1); |
203 | | |
204 | 3.98M | if (uri->cleanup & XML_URI_ALLOW_UNWISE) { |
205 | 118k | if (IS_UNWISE(cur)) |
206 | 4.73k | return(1); |
207 | 3.86M | } else if (uri->cleanup & XML_URI_ALLOW_UCSCHAR) { |
208 | 0 | if (ISA_UCSCHAR(cur)) |
209 | 0 | return(1); |
210 | 0 | } |
211 | | |
212 | 3.98M | return(0); |
213 | 3.98M | } |
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 | 893k | xmlParse3986Scheme(xmlURIPtr uri, const char **str) { |
228 | 893k | const char *cur; |
229 | | |
230 | 893k | cur = *str; |
231 | 893k | if (!ISA_ALPHA(cur)) |
232 | 232k | return(1); |
233 | 660k | cur++; |
234 | 2.34M | while (ISA_ALPHA(cur) || ISA_DIGIT(cur) || |
235 | 2.34M | (*cur == '+') || (*cur == '-') || (*cur == '.')) cur++; |
236 | 660k | if (uri != NULL) { |
237 | 660k | if (uri->scheme != NULL) xmlFree(uri->scheme); |
238 | 660k | uri->scheme = STRNDUP(*str, cur - *str); |
239 | 660k | if (uri->scheme == NULL) |
240 | 237 | return(-1); |
241 | 660k | } |
242 | 660k | *str = cur; |
243 | 660k | return(0); |
244 | 660k | } |
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 | 35.1k | { |
264 | 35.1k | const char *cur; |
265 | | |
266 | 35.1k | cur = *str; |
267 | | |
268 | 1.27M | while ((ISA_PCHAR(uri, cur)) || (*cur == '/') || (*cur == '?') || |
269 | 1.27M | (*cur == '[') || (*cur == ']')) |
270 | 1.24M | NEXT(cur); |
271 | 35.1k | if (uri != NULL) { |
272 | 35.1k | if (uri->fragment != NULL) |
273 | 0 | xmlFree(uri->fragment); |
274 | 35.1k | if (uri->cleanup & XML_URI_NO_UNESCAPE) |
275 | 574 | uri->fragment = STRNDUP(*str, cur - *str); |
276 | 34.5k | else |
277 | 34.5k | uri->fragment = xmlURIUnescapeString(*str, cur - *str, NULL); |
278 | 35.1k | if (uri->fragment == NULL) |
279 | 103 | return (-1); |
280 | 35.1k | } |
281 | 35.0k | *str = cur; |
282 | 35.0k | return (0); |
283 | 35.1k | } |
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 | 40.1k | { |
299 | 40.1k | const char *cur; |
300 | | |
301 | 40.1k | cur = *str; |
302 | | |
303 | 3.62M | while ((ISA_PCHAR(uri, cur)) || (*cur == '/') || (*cur == '?')) |
304 | 3.58M | NEXT(cur); |
305 | 40.1k | if (uri != NULL) { |
306 | 40.1k | if (uri->query != NULL) |
307 | 0 | xmlFree(uri->query); |
308 | 40.1k | if (uri->cleanup & XML_URI_NO_UNESCAPE) |
309 | 424 | uri->query = STRNDUP(*str, cur - *str); |
310 | 39.7k | else |
311 | 39.7k | uri->query = xmlURIUnescapeString(*str, cur - *str, NULL); |
312 | 40.1k | if (uri->query == NULL) |
313 | 79 | 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 | 40.0k | if (uri->query_raw != NULL) |
319 | 0 | xmlFree (uri->query_raw); |
320 | 40.0k | uri->query_raw = STRNDUP (*str, cur - *str); |
321 | 40.0k | if (uri->query_raw == NULL) |
322 | 72 | return (-1); |
323 | 40.0k | } |
324 | 39.9k | *str = cur; |
325 | 39.9k | return (0); |
326 | 40.1k | } |
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 | 18.7k | { |
343 | 18.7k | const char *cur = *str; |
344 | 18.7k | int port = 0; |
345 | | |
346 | 18.7k | if (ISA_DIGIT(cur)) { |
347 | 76.9k | while (ISA_DIGIT(cur)) { |
348 | 71.6k | int digit = *cur - '0'; |
349 | | |
350 | 71.6k | if (port > INT_MAX / 10) |
351 | 3.67k | return(1); |
352 | 68.0k | port *= 10; |
353 | 68.0k | if (port > INT_MAX - digit) |
354 | 1.30k | return(1); |
355 | 66.7k | port += digit; |
356 | | |
357 | 66.7k | cur++; |
358 | 66.7k | } |
359 | 5.21k | if (uri != NULL) |
360 | 5.21k | uri->port = port; |
361 | 5.21k | *str = cur; |
362 | 5.21k | return(0); |
363 | 10.1k | } |
364 | 8.58k | return(1); |
365 | 18.7k | } |
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 | 159k | { |
382 | 159k | const char *cur; |
383 | | |
384 | 159k | cur = *str; |
385 | 1.66M | while (ISA_UNRESERVED(uri, cur) || ISA_PCT_ENCODED(cur) || |
386 | 1.66M | ISA_SUB_DELIM(cur) || (*cur == ':')) |
387 | 1.50M | NEXT(cur); |
388 | 159k | if (*cur == '@') { |
389 | 26.6k | if (uri != NULL) { |
390 | 26.6k | if (uri->user != NULL) xmlFree(uri->user); |
391 | 26.6k | if (uri->cleanup & XML_URI_NO_UNESCAPE) |
392 | 456 | uri->user = STRNDUP(*str, cur - *str); |
393 | 26.1k | else |
394 | 26.1k | uri->user = xmlURIUnescapeString(*str, cur - *str, NULL); |
395 | 26.6k | if (uri->user == NULL) |
396 | 32 | return(-1); |
397 | 26.6k | } |
398 | 26.5k | *str = cur; |
399 | 26.5k | return(0); |
400 | 26.6k | } |
401 | 132k | return(1); |
402 | 159k | } |
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 | 39.5k | xmlParse3986DecOctet(const char **str) { |
420 | 39.5k | const char *cur = *str; |
421 | | |
422 | 39.5k | if (!(ISA_DIGIT(cur))) |
423 | 4.90k | return(1); |
424 | 34.6k | if (!ISA_DIGIT(cur+1)) |
425 | 16.0k | cur++; |
426 | 18.5k | else if ((*cur != '0') && (ISA_DIGIT(cur + 1)) && (!ISA_DIGIT(cur+2))) |
427 | 4.57k | cur += 2; |
428 | 13.9k | else if ((*cur == '1') && (ISA_DIGIT(cur + 1)) && (ISA_DIGIT(cur + 2))) |
429 | 2.54k | cur += 3; |
430 | 11.4k | else if ((*cur == '2') && (*(cur + 1) >= '0') && |
431 | 11.4k | (*(cur + 1) <= '4') && (ISA_DIGIT(cur + 2))) |
432 | 2.34k | cur += 3; |
433 | 9.10k | else if ((*cur == '2') && (*(cur + 1) == '5') && |
434 | 9.10k | (*(cur + 2) >= '0') && (*(cur + 1) <= '5')) |
435 | 1.78k | cur += 3; |
436 | 7.32k | else |
437 | 7.32k | return(1); |
438 | 27.2k | *str = cur; |
439 | 27.2k | return(0); |
440 | 34.6k | } |
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 | 159k | { |
459 | 159k | const char *cur = *str; |
460 | 159k | const char *host; |
461 | | |
462 | 159k | host = cur; |
463 | | /* |
464 | | * IPv6 and future addressing scheme are enclosed between brackets |
465 | | */ |
466 | 159k | if (*cur == '[') { |
467 | 3.67k | cur++; |
468 | 1.15M | while ((*cur != ']') && (*cur != 0)) |
469 | 1.14M | cur++; |
470 | 3.67k | if (*cur != ']') |
471 | 1.61k | return(1); |
472 | 2.06k | cur++; |
473 | 2.06k | goto found; |
474 | 3.67k | } |
475 | | /* |
476 | | * try to parse an IPv4 |
477 | | */ |
478 | 155k | if (ISA_DIGIT(cur)) { |
479 | 25.7k | if (xmlParse3986DecOctet(&cur) != 0) |
480 | 5.56k | goto not_ipv4; |
481 | 20.1k | if (*cur != '.') |
482 | 7.84k | goto not_ipv4; |
483 | 12.3k | cur++; |
484 | 12.3k | if (xmlParse3986DecOctet(&cur) != 0) |
485 | 5.21k | goto not_ipv4; |
486 | 7.11k | if (*cur != '.') |
487 | 5.66k | goto not_ipv4; |
488 | 1.45k | if (xmlParse3986DecOctet(&cur) != 0) |
489 | 1.45k | 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 | 25.7k | not_ipv4: |
496 | 25.7k | cur = *str; |
497 | 25.7k | } |
498 | | /* |
499 | | * then this should be a hostname which can be empty |
500 | | */ |
501 | 1.49M | while (ISA_UNRESERVED(uri, cur) || |
502 | 1.49M | ISA_PCT_ENCODED(cur) || ISA_SUB_DELIM(cur)) |
503 | 1.33M | NEXT(cur); |
504 | 157k | found: |
505 | 157k | if (uri != NULL) { |
506 | 157k | if (uri->authority != NULL) xmlFree(uri->authority); |
507 | 157k | uri->authority = NULL; |
508 | 157k | if (uri->server != NULL) xmlFree(uri->server); |
509 | 157k | if (cur != host) { |
510 | 134k | if (uri->cleanup & XML_URI_NO_UNESCAPE) |
511 | 703 | uri->server = STRNDUP(host, cur - host); |
512 | 134k | else |
513 | 134k | uri->server = xmlURIUnescapeString(host, cur - host, NULL); |
514 | 134k | if (uri->server == NULL) |
515 | 68 | return(-1); |
516 | 134k | } else |
517 | 22.8k | uri->server = NULL; |
518 | 157k | } |
519 | 157k | *str = cur; |
520 | 157k | return(0); |
521 | 157k | } |
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 | 159k | { |
538 | 159k | const char *cur; |
539 | 159k | int ret; |
540 | | |
541 | 159k | cur = *str; |
542 | | /* |
543 | | * try to parse an userinfo and check for the trailing @ |
544 | | */ |
545 | 159k | ret = xmlParse3986Userinfo(uri, &cur); |
546 | 159k | if (ret < 0) |
547 | 32 | return(ret); |
548 | 159k | if ((ret != 0) || (*cur != '@')) |
549 | 132k | cur = *str; |
550 | 26.5k | else |
551 | 26.5k | cur++; |
552 | 159k | ret = xmlParse3986Host(uri, &cur); |
553 | 159k | if (ret != 0) return(ret); |
554 | 157k | if (*cur == ':') { |
555 | 18.7k | cur++; |
556 | 18.7k | ret = xmlParse3986Port(uri, &cur); |
557 | 18.7k | if (ret != 0) return(ret); |
558 | 18.7k | } |
559 | 144k | *str = cur; |
560 | 144k | return(0); |
561 | 157k | } |
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 | 1.63M | { |
582 | 1.63M | const char *cur; |
583 | | |
584 | 1.63M | cur = *str; |
585 | 1.63M | if (!ISA_PCHAR(uri, cur)) { |
586 | 633k | if (empty) |
587 | 620k | return(0); |
588 | 12.8k | return(1); |
589 | 633k | } |
590 | 5.16M | while (ISA_PCHAR(uri, cur) && (*cur != forbid)) |
591 | 4.16M | NEXT(cur); |
592 | 999k | *str = cur; |
593 | 999k | return (0); |
594 | 1.63M | } |
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 | 144k | { |
611 | 144k | const char *cur; |
612 | 144k | int ret; |
613 | | |
614 | 144k | cur = *str; |
615 | | |
616 | 674k | while (*cur == '/') { |
617 | 530k | cur++; |
618 | 530k | ret = xmlParse3986Segment(uri, &cur, 0, 1); |
619 | 530k | if (ret != 0) return(ret); |
620 | 530k | } |
621 | 144k | if (uri != NULL) { |
622 | 144k | if (uri->path != NULL) xmlFree(uri->path); |
623 | 144k | if (*str != cur) { |
624 | 83.8k | if (uri->cleanup & XML_URI_NO_UNESCAPE) |
625 | 126 | uri->path = STRNDUP(*str, cur - *str); |
626 | 83.7k | else |
627 | 83.7k | uri->path = xmlURIUnescapeString(*str, cur - *str, NULL); |
628 | 83.8k | if (uri->path == NULL) |
629 | 49 | return (-1); |
630 | 83.8k | } else { |
631 | 60.2k | uri->path = NULL; |
632 | 60.2k | } |
633 | 144k | } |
634 | 144k | *str = cur; |
635 | 144k | return (0); |
636 | 144k | } |
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 | 23.3k | { |
653 | 23.3k | const char *cur; |
654 | 23.3k | int ret; |
655 | | |
656 | 23.3k | cur = *str; |
657 | | |
658 | 23.3k | if (*cur != '/') |
659 | 0 | return(1); |
660 | 23.3k | cur++; |
661 | 23.3k | ret = xmlParse3986Segment(uri, &cur, 0, 0); |
662 | 23.3k | if (ret == 0) { |
663 | 47.3k | while (*cur == '/') { |
664 | 36.7k | cur++; |
665 | 36.7k | ret = xmlParse3986Segment(uri, &cur, 0, 1); |
666 | 36.7k | if (ret != 0) return(ret); |
667 | 36.7k | } |
668 | 10.5k | } |
669 | 23.3k | if (uri != NULL) { |
670 | 23.3k | if (uri->path != NULL) xmlFree(uri->path); |
671 | 23.3k | if (cur != *str) { |
672 | 23.3k | if (uri->cleanup & XML_URI_NO_UNESCAPE) |
673 | 267 | uri->path = STRNDUP(*str, cur - *str); |
674 | 23.0k | else |
675 | 23.0k | uri->path = xmlURIUnescapeString(*str, cur - *str, NULL); |
676 | 23.3k | if (uri->path == NULL) |
677 | 53 | return (-1); |
678 | 23.3k | } else { |
679 | 0 | uri->path = NULL; |
680 | 0 | } |
681 | 23.3k | } |
682 | 23.2k | *str = cur; |
683 | 23.2k | return (0); |
684 | 23.3k | } |
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 | 140k | { |
701 | 140k | const char *cur; |
702 | 140k | int ret; |
703 | | |
704 | 140k | cur = *str; |
705 | | |
706 | 140k | ret = xmlParse3986Segment(uri, &cur, 0, 0); |
707 | 140k | if (ret != 0) return(ret); |
708 | 214k | while (*cur == '/') { |
709 | 73.8k | cur++; |
710 | 73.8k | ret = xmlParse3986Segment(uri, &cur, 0, 1); |
711 | 73.8k | if (ret != 0) return(ret); |
712 | 73.8k | } |
713 | 140k | if (uri != NULL) { |
714 | 140k | if (uri->path != NULL) xmlFree(uri->path); |
715 | 140k | if (cur != *str) { |
716 | 140k | if (uri->cleanup & XML_URI_NO_UNESCAPE) |
717 | 71 | uri->path = STRNDUP(*str, cur - *str); |
718 | 140k | else |
719 | 140k | uri->path = xmlURIUnescapeString(*str, cur - *str, NULL); |
720 | 140k | if (uri->path == NULL) |
721 | 55 | return (-1); |
722 | 140k | } else { |
723 | 0 | uri->path = NULL; |
724 | 0 | } |
725 | 140k | } |
726 | 140k | *str = cur; |
727 | 140k | return (0); |
728 | 140k | } |
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 | 537k | { |
745 | 537k | const char *cur; |
746 | 537k | int ret; |
747 | | |
748 | 537k | cur = *str; |
749 | | |
750 | 537k | ret = xmlParse3986Segment(uri, &cur, ':', 0); |
751 | 537k | if (ret != 0) return(ret); |
752 | 827k | while (*cur == '/') { |
753 | 289k | cur++; |
754 | 289k | ret = xmlParse3986Segment(uri, &cur, 0, 1); |
755 | 289k | if (ret != 0) return(ret); |
756 | 289k | } |
757 | 537k | if (uri != NULL) { |
758 | 537k | if (uri->path != NULL) xmlFree(uri->path); |
759 | 537k | if (cur != *str) { |
760 | 533k | if (uri->cleanup & XML_URI_NO_UNESCAPE) |
761 | 991 | uri->path = STRNDUP(*str, cur - *str); |
762 | 532k | else |
763 | 532k | uri->path = xmlURIUnescapeString(*str, cur - *str, NULL); |
764 | 533k | if (uri->path == NULL) |
765 | 288 | return (-1); |
766 | 533k | } else { |
767 | 3.65k | uri->path = NULL; |
768 | 3.65k | } |
769 | 537k | } |
770 | 537k | *str = cur; |
771 | 537k | return (0); |
772 | 537k | } |
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 | 278k | { |
792 | 278k | const char *cur; |
793 | 278k | int ret; |
794 | | |
795 | 278k | cur = *str; |
796 | | |
797 | 278k | if ((*cur == '/') && (*(cur + 1) == '/')) { |
798 | 113k | cur += 2; |
799 | 113k | ret = xmlParse3986Authority(uri, &cur); |
800 | 113k | if (ret != 0) return(ret); |
801 | | /* |
802 | | * An empty server is marked with a special URI value. |
803 | | */ |
804 | 106k | if ((uri->server == NULL) && (uri->port == PORT_EMPTY)) |
805 | 6.87k | uri->port = PORT_EMPTY_SERVER; |
806 | 106k | ret = xmlParse3986PathAbEmpty(uri, &cur); |
807 | 106k | if (ret != 0) return(ret); |
808 | 106k | *str = cur; |
809 | 106k | return(0); |
810 | 164k | } else if (*cur == '/') { |
811 | 7.33k | ret = xmlParse3986PathAbsolute(uri, &cur); |
812 | 7.33k | if (ret != 0) return(ret); |
813 | 157k | } else if (ISA_PCHAR(uri, cur)) { |
814 | 140k | ret = xmlParse3986PathRootless(uri, &cur); |
815 | 140k | if (ret != 0) return(ret); |
816 | 140k | } else { |
817 | | /* path-empty is effectively empty */ |
818 | 16.3k | if (uri != NULL) { |
819 | 16.3k | if (uri->path != NULL) xmlFree(uri->path); |
820 | 16.3k | uri->path = NULL; |
821 | 16.3k | } |
822 | 16.3k | } |
823 | 164k | *str = cur; |
824 | 164k | return (0); |
825 | 278k | } |
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 | 679k | xmlParse3986RelativeRef(xmlURIPtr uri, const char *str) { |
845 | 679k | int ret; |
846 | | |
847 | 679k | if ((*str == '/') && (*(str + 1) == '/')) { |
848 | 45.7k | str += 2; |
849 | 45.7k | ret = xmlParse3986Authority(uri, &str); |
850 | 45.7k | if (ret != 0) return(ret); |
851 | 37.1k | ret = xmlParse3986PathAbEmpty(uri, &str); |
852 | 37.1k | if (ret != 0) return(ret); |
853 | 633k | } else if (*str == '/') { |
854 | 16.0k | ret = xmlParse3986PathAbsolute(uri, &str); |
855 | 16.0k | if (ret != 0) return(ret); |
856 | 617k | } else if (ISA_PCHAR(uri, str)) { |
857 | 537k | ret = xmlParse3986PathNoScheme(uri, &str); |
858 | 537k | if (ret != 0) return(ret); |
859 | 537k | } else { |
860 | | /* path-empty is effectively empty */ |
861 | 80.5k | if (uri != NULL) { |
862 | 80.5k | if (uri->path != NULL) xmlFree(uri->path); |
863 | 80.5k | uri->path = NULL; |
864 | 80.5k | } |
865 | 80.5k | } |
866 | | |
867 | 670k | if (*str == '?') { |
868 | 34.6k | str++; |
869 | 34.6k | ret = xmlParse3986Query(uri, &str); |
870 | 34.6k | if (ret != 0) return(ret); |
871 | 34.6k | } |
872 | 670k | if (*str == '#') { |
873 | 28.4k | str++; |
874 | 28.4k | ret = xmlParse3986Fragment(uri, &str); |
875 | 28.4k | if (ret != 0) return(ret); |
876 | 28.4k | } |
877 | 670k | if (*str != 0) { |
878 | 306k | xmlCleanURI(uri); |
879 | 306k | return(1); |
880 | 306k | } |
881 | 364k | return(0); |
882 | 670k | } |
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 | 893k | xmlParse3986URI(xmlURIPtr uri, const char *str) { |
899 | 893k | int ret; |
900 | | |
901 | 893k | ret = xmlParse3986Scheme(uri, &str); |
902 | 893k | if (ret != 0) return(ret); |
903 | 660k | if (*str != ':') { |
904 | 382k | return(1); |
905 | 382k | } |
906 | 278k | str++; |
907 | 278k | ret = xmlParse3986HierPart(uri, &str); |
908 | 278k | if (ret != 0) return(ret); |
909 | 271k | if (*str == '?') { |
910 | 5.50k | str++; |
911 | 5.50k | ret = xmlParse3986Query(uri, &str); |
912 | 5.50k | if (ret != 0) return(ret); |
913 | 5.50k | } |
914 | 271k | if (*str == '#') { |
915 | 6.64k | str++; |
916 | 6.64k | ret = xmlParse3986Fragment(uri, &str); |
917 | 6.64k | if (ret != 0) return(ret); |
918 | 6.64k | } |
919 | 271k | if (*str != 0) { |
920 | 58.1k | xmlCleanURI(uri); |
921 | 58.1k | return(1); |
922 | 58.1k | } |
923 | 213k | return(0); |
924 | 271k | } |
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 | 893k | xmlParse3986URIReference(xmlURIPtr uri, const char *str) { |
940 | 893k | int ret; |
941 | | |
942 | 893k | if (str == NULL) |
943 | 0 | return(-1); |
944 | 893k | xmlCleanURI(uri); |
945 | | |
946 | | /* |
947 | | * Try first to parse absolute refs, then fallback to relative if |
948 | | * it fails. |
949 | | */ |
950 | 893k | ret = xmlParse3986URI(uri, str); |
951 | 893k | if (ret < 0) |
952 | 429 | return(ret); |
953 | 892k | if (ret != 0) { |
954 | 679k | xmlCleanURI(uri); |
955 | 679k | ret = xmlParse3986RelativeRef(uri, str); |
956 | 679k | if (ret != 0) { |
957 | 315k | xmlCleanURI(uri); |
958 | 315k | return(ret); |
959 | 315k | } |
960 | 679k | } |
961 | 577k | return(0); |
962 | 892k | } |
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 | | * Returns 0 on success, an error code (typically 1) if the URI is invalid |
974 | | * or -1 if a memory allocation failed. |
975 | | */ |
976 | | int |
977 | 879k | xmlParseURISafe(const char *str, xmlURIPtr *uriOut) { |
978 | 879k | xmlURIPtr uri; |
979 | 879k | int ret; |
980 | | |
981 | 879k | if (uriOut != NULL) |
982 | 879k | *uriOut = NULL; |
983 | 879k | if (str == NULL) |
984 | 8 | return(1); |
985 | | |
986 | 879k | uri = xmlCreateURI(); |
987 | 879k | if (uri == NULL) |
988 | 565 | return(-1); |
989 | | |
990 | 878k | ret = xmlParse3986URIReference(uri, str); |
991 | 878k | if (ret) { |
992 | 312k | xmlFreeURI(uri); |
993 | 312k | return(ret); |
994 | 312k | } |
995 | | |
996 | 566k | if (uriOut != NULL) |
997 | 566k | *uriOut = uri; |
998 | 566k | return(0); |
999 | 878k | } |
1000 | | |
1001 | | /** |
1002 | | * xmlParseURI: |
1003 | | * @str: the URI string to analyze |
1004 | | * |
1005 | | * Parse an URI based on RFC 3986 |
1006 | | * |
1007 | | * URI-reference = [ absoluteURI | relativeURI ] [ "#" fragment ] |
1008 | | * |
1009 | | * Returns a newly built xmlURIPtr or NULL in case of error |
1010 | | */ |
1011 | | xmlURIPtr |
1012 | 84.3k | xmlParseURI(const char *str) { |
1013 | 84.3k | xmlURIPtr uri; |
1014 | 84.3k | xmlParseURISafe(str, &uri); |
1015 | 84.3k | return(uri); |
1016 | 84.3k | } |
1017 | | |
1018 | | /** |
1019 | | * xmlParseURIReference: |
1020 | | * @uri: pointer to an URI structure |
1021 | | * @str: the string to analyze |
1022 | | * |
1023 | | * Parse an URI reference string based on RFC 3986 and fills in the |
1024 | | * appropriate fields of the @uri structure |
1025 | | * |
1026 | | * URI-reference = URI / relative-ref |
1027 | | * |
1028 | | * Returns 0 or the error code |
1029 | | */ |
1030 | | int |
1031 | 14.4k | xmlParseURIReference(xmlURIPtr uri, const char *str) { |
1032 | 14.4k | return(xmlParse3986URIReference(uri, str)); |
1033 | 14.4k | } |
1034 | | |
1035 | | /** |
1036 | | * xmlParseURIRaw: |
1037 | | * @str: the URI string to analyze |
1038 | | * @raw: if 1 unescaping of URI pieces are disabled |
1039 | | * |
1040 | | * Parse an URI but allows to keep intact the original fragments. |
1041 | | * |
1042 | | * URI-reference = URI / relative-ref |
1043 | | * |
1044 | | * Returns a newly built xmlURIPtr or NULL in case of error |
1045 | | */ |
1046 | | xmlURIPtr |
1047 | 4.07k | xmlParseURIRaw(const char *str, int raw) { |
1048 | 4.07k | xmlURIPtr uri; |
1049 | 4.07k | int ret; |
1050 | | |
1051 | 4.07k | if (str == NULL) |
1052 | 4 | return(NULL); |
1053 | 4.06k | uri = xmlCreateURI(); |
1054 | 4.06k | if (uri != NULL) { |
1055 | 3.98k | if (raw) { |
1056 | 3.98k | uri->cleanup |= XML_URI_NO_UNESCAPE; |
1057 | 3.98k | } |
1058 | 3.98k | ret = xmlParseURIReference(uri, str); |
1059 | 3.98k | if (ret) { |
1060 | 1.41k | xmlFreeURI(uri); |
1061 | 1.41k | return(NULL); |
1062 | 1.41k | } |
1063 | 3.98k | } |
1064 | 2.65k | return(uri); |
1065 | 4.06k | } |
1066 | | |
1067 | | /************************************************************************ |
1068 | | * * |
1069 | | * Generic URI structure functions * |
1070 | | * * |
1071 | | ************************************************************************/ |
1072 | | |
1073 | | /** |
1074 | | * xmlCreateURI: |
1075 | | * |
1076 | | * Simply creates an empty xmlURI |
1077 | | * |
1078 | | * Returns the new structure or NULL in case of error |
1079 | | */ |
1080 | | xmlURIPtr |
1081 | 903k | xmlCreateURI(void) { |
1082 | 903k | xmlURIPtr ret; |
1083 | | |
1084 | 903k | ret = (xmlURIPtr) xmlMalloc(sizeof(xmlURI)); |
1085 | 903k | if (ret == NULL) |
1086 | 773 | return(NULL); |
1087 | 902k | memset(ret, 0, sizeof(xmlURI)); |
1088 | 902k | ret->port = PORT_EMPTY; |
1089 | 902k | return(ret); |
1090 | 903k | } |
1091 | | |
1092 | | /** |
1093 | | * xmlSaveUriRealloc: |
1094 | | * |
1095 | | * Function to handle properly a reallocation when saving an URI |
1096 | | * Also imposes some limit on the length of an URI string output |
1097 | | */ |
1098 | | static xmlChar * |
1099 | 5.56k | xmlSaveUriRealloc(xmlChar *ret, int *max) { |
1100 | 5.56k | xmlChar *temp; |
1101 | 5.56k | int tmp; |
1102 | | |
1103 | 5.56k | if (*max > MAX_URI_LENGTH) |
1104 | 0 | return(NULL); |
1105 | 5.56k | tmp = *max * 2; |
1106 | 5.56k | temp = (xmlChar *) xmlRealloc(ret, (tmp + 1)); |
1107 | 5.56k | if (temp == NULL) |
1108 | 102 | return(NULL); |
1109 | 5.46k | *max = tmp; |
1110 | 5.46k | return(temp); |
1111 | 5.56k | } |
1112 | | |
1113 | | /** |
1114 | | * xmlSaveUri: |
1115 | | * @uri: pointer to an xmlURI |
1116 | | * |
1117 | | * Save the URI as an escaped string |
1118 | | * |
1119 | | * Returns a new string (to be deallocated by caller) |
1120 | | */ |
1121 | | xmlChar * |
1122 | 31.7k | xmlSaveUri(xmlURIPtr uri) { |
1123 | 31.7k | xmlChar *ret = NULL; |
1124 | 31.7k | xmlChar *temp; |
1125 | 31.7k | const char *p; |
1126 | 31.7k | int len; |
1127 | 31.7k | int max; |
1128 | | |
1129 | 31.7k | if (uri == NULL) return(NULL); |
1130 | | |
1131 | | |
1132 | 30.2k | max = 80; |
1133 | 30.2k | ret = (xmlChar *) xmlMallocAtomic(max + 1); |
1134 | 30.2k | if (ret == NULL) |
1135 | 180 | return(NULL); |
1136 | 30.0k | len = 0; |
1137 | | |
1138 | 30.0k | if (uri->scheme != NULL) { |
1139 | 6.13k | p = uri->scheme; |
1140 | 169k | while (*p != 0) { |
1141 | 163k | if (len >= max) { |
1142 | 339 | temp = xmlSaveUriRealloc(ret, &max); |
1143 | 339 | if (temp == NULL) goto mem_error; |
1144 | 336 | ret = temp; |
1145 | 336 | } |
1146 | 163k | ret[len++] = *p++; |
1147 | 163k | } |
1148 | 6.13k | if (len >= max) { |
1149 | 19 | temp = xmlSaveUriRealloc(ret, &max); |
1150 | 19 | if (temp == NULL) goto mem_error; |
1151 | 17 | ret = temp; |
1152 | 17 | } |
1153 | 6.13k | ret[len++] = ':'; |
1154 | 6.13k | } |
1155 | 30.0k | if (uri->opaque != NULL) { |
1156 | 0 | p = uri->opaque; |
1157 | 0 | while (*p != 0) { |
1158 | 0 | if (len + 3 >= max) { |
1159 | 0 | temp = xmlSaveUriRealloc(ret, &max); |
1160 | 0 | if (temp == NULL) goto mem_error; |
1161 | 0 | ret = temp; |
1162 | 0 | } |
1163 | 0 | if (IS_RESERVED(*(p)) || IS_UNRESERVED(*(p))) |
1164 | 0 | ret[len++] = *p++; |
1165 | 0 | else { |
1166 | 0 | int val = *(unsigned char *)p++; |
1167 | 0 | int hi = val / 0x10, lo = val % 0x10; |
1168 | 0 | ret[len++] = '%'; |
1169 | 0 | ret[len++] = hi + (hi > 9? 'A'-10 : '0'); |
1170 | 0 | ret[len++] = lo + (lo > 9? 'A'-10 : '0'); |
1171 | 0 | } |
1172 | 0 | } |
1173 | 30.0k | } else { |
1174 | 30.0k | if ((uri->server != NULL) || (uri->port != PORT_EMPTY)) { |
1175 | 6.81k | if (len + 3 >= max) { |
1176 | 18 | temp = xmlSaveUriRealloc(ret, &max); |
1177 | 18 | if (temp == NULL) goto mem_error; |
1178 | 17 | ret = temp; |
1179 | 17 | } |
1180 | 6.81k | ret[len++] = '/'; |
1181 | 6.81k | ret[len++] = '/'; |
1182 | 6.81k | if (uri->user != NULL) { |
1183 | 2.71k | p = uri->user; |
1184 | 646k | while (*p != 0) { |
1185 | 643k | if (len + 3 >= max) { |
1186 | 1.76k | temp = xmlSaveUriRealloc(ret, &max); |
1187 | 1.76k | if (temp == NULL) goto mem_error; |
1188 | 1.74k | ret = temp; |
1189 | 1.74k | } |
1190 | 643k | if ((IS_UNRESERVED(*(p))) || |
1191 | 643k | ((*(p) == ';')) || ((*(p) == ':')) || |
1192 | 643k | ((*(p) == '&')) || ((*(p) == '=')) || |
1193 | 643k | ((*(p) == '+')) || ((*(p) == '$')) || |
1194 | 643k | ((*(p) == ','))) |
1195 | 252k | ret[len++] = *p++; |
1196 | 391k | else { |
1197 | 391k | int val = *(unsigned char *)p++; |
1198 | 391k | int hi = val / 0x10, lo = val % 0x10; |
1199 | 391k | ret[len++] = '%'; |
1200 | 391k | ret[len++] = hi + (hi > 9? 'A'-10 : '0'); |
1201 | 391k | ret[len++] = lo + (lo > 9? 'A'-10 : '0'); |
1202 | 391k | } |
1203 | 643k | } |
1204 | 2.69k | if (len + 3 >= max) { |
1205 | 51 | temp = xmlSaveUriRealloc(ret, &max); |
1206 | 51 | if (temp == NULL) goto mem_error; |
1207 | 46 | ret = temp; |
1208 | 46 | } |
1209 | 2.69k | ret[len++] = '@'; |
1210 | 2.69k | } |
1211 | 6.79k | if (uri->server != NULL) { |
1212 | 4.89k | p = uri->server; |
1213 | 518k | while (*p != 0) { |
1214 | 513k | if (len >= max) { |
1215 | 829 | temp = xmlSaveUriRealloc(ret, &max); |
1216 | 829 | if (temp == NULL) goto mem_error; |
1217 | 817 | ret = temp; |
1218 | 817 | } |
1219 | | /* TODO: escaping? */ |
1220 | 513k | ret[len++] = (xmlChar) *p++; |
1221 | 513k | } |
1222 | 4.89k | } |
1223 | 6.78k | if (uri->port > 0) { |
1224 | 602 | if (len + 10 >= max) { |
1225 | 35 | temp = xmlSaveUriRealloc(ret, &max); |
1226 | 35 | if (temp == NULL) goto mem_error; |
1227 | 33 | ret = temp; |
1228 | 33 | } |
1229 | 600 | len += snprintf((char *) &ret[len], max - len, ":%d", uri->port); |
1230 | 600 | } |
1231 | 23.2k | } else if (uri->authority != NULL) { |
1232 | 0 | if (len + 3 >= max) { |
1233 | 0 | temp = xmlSaveUriRealloc(ret, &max); |
1234 | 0 | if (temp == NULL) goto mem_error; |
1235 | 0 | ret = temp; |
1236 | 0 | } |
1237 | 0 | ret[len++] = '/'; |
1238 | 0 | ret[len++] = '/'; |
1239 | 0 | p = uri->authority; |
1240 | 0 | while (*p != 0) { |
1241 | 0 | if (len + 3 >= max) { |
1242 | 0 | temp = xmlSaveUriRealloc(ret, &max); |
1243 | 0 | if (temp == NULL) goto mem_error; |
1244 | 0 | ret = temp; |
1245 | 0 | } |
1246 | 0 | if ((IS_UNRESERVED(*(p))) || |
1247 | 0 | ((*(p) == '$')) || ((*(p) == ',')) || ((*(p) == ';')) || |
1248 | 0 | ((*(p) == ':')) || ((*(p) == '@')) || ((*(p) == '&')) || |
1249 | 0 | ((*(p) == '=')) || ((*(p) == '+'))) |
1250 | 0 | ret[len++] = *p++; |
1251 | 0 | else { |
1252 | 0 | int val = *(unsigned char *)p++; |
1253 | 0 | int hi = val / 0x10, lo = val % 0x10; |
1254 | 0 | ret[len++] = '%'; |
1255 | 0 | ret[len++] = hi + (hi > 9? 'A'-10 : '0'); |
1256 | 0 | ret[len++] = lo + (lo > 9? 'A'-10 : '0'); |
1257 | 0 | } |
1258 | 0 | } |
1259 | 23.2k | } else if (uri->scheme != NULL) { |
1260 | 1.23k | if (len + 3 >= max) { |
1261 | 16 | temp = xmlSaveUriRealloc(ret, &max); |
1262 | 16 | if (temp == NULL) goto mem_error; |
1263 | 15 | ret = temp; |
1264 | 15 | } |
1265 | 1.23k | } |
1266 | 30.0k | if (uri->path != NULL) { |
1267 | 11.6k | p = uri->path; |
1268 | | /* |
1269 | | * the colon in file:///d: should not be escaped or |
1270 | | * Windows accesses fail later. |
1271 | | */ |
1272 | 11.6k | if ((uri->scheme != NULL) && |
1273 | 11.6k | (p[0] == '/') && |
1274 | 11.6k | (((p[1] >= 'a') && (p[1] <= 'z')) || |
1275 | 2.43k | ((p[1] >= 'A') && (p[1] <= 'Z'))) && |
1276 | 11.6k | (p[2] == ':') && |
1277 | 11.6k | (xmlStrEqual(BAD_CAST uri->scheme, BAD_CAST "file"))) { |
1278 | 68 | if (len + 3 >= max) { |
1279 | 13 | temp = xmlSaveUriRealloc(ret, &max); |
1280 | 13 | if (temp == NULL) goto mem_error; |
1281 | 11 | ret = temp; |
1282 | 11 | } |
1283 | 66 | ret[len++] = *p++; |
1284 | 66 | ret[len++] = *p++; |
1285 | 66 | ret[len++] = *p++; |
1286 | 66 | } |
1287 | 733k | while (*p != 0) { |
1288 | 722k | if (len + 3 >= max) { |
1289 | 1.24k | temp = xmlSaveUriRealloc(ret, &max); |
1290 | 1.24k | if (temp == NULL) goto mem_error; |
1291 | 1.22k | ret = temp; |
1292 | 1.22k | } |
1293 | 722k | if ((IS_UNRESERVED(*(p))) || ((*(p) == '/')) || |
1294 | 722k | ((*(p) == ';')) || ((*(p) == '@')) || ((*(p) == '&')) || |
1295 | 722k | ((*(p) == '=')) || ((*(p) == '+')) || ((*(p) == '$')) || |
1296 | 722k | ((*(p) == ','))) |
1297 | 695k | ret[len++] = *p++; |
1298 | 26.4k | else { |
1299 | 26.4k | int val = *(unsigned char *)p++; |
1300 | 26.4k | int hi = val / 0x10, lo = val % 0x10; |
1301 | 26.4k | ret[len++] = '%'; |
1302 | 26.4k | ret[len++] = hi + (hi > 9? 'A'-10 : '0'); |
1303 | 26.4k | ret[len++] = lo + (lo > 9? 'A'-10 : '0'); |
1304 | 26.4k | } |
1305 | 722k | } |
1306 | 11.6k | } |
1307 | 30.0k | if (uri->query_raw != NULL) { |
1308 | 2.92k | if (len + 1 >= max) { |
1309 | 34 | temp = xmlSaveUriRealloc(ret, &max); |
1310 | 34 | if (temp == NULL) goto mem_error; |
1311 | 29 | ret = temp; |
1312 | 29 | } |
1313 | 2.92k | ret[len++] = '?'; |
1314 | 2.92k | p = uri->query_raw; |
1315 | 2.17M | while (*p != 0) { |
1316 | 2.17M | if (len + 1 >= max) { |
1317 | 555 | temp = xmlSaveUriRealloc(ret, &max); |
1318 | 555 | if (temp == NULL) goto mem_error; |
1319 | 544 | ret = temp; |
1320 | 544 | } |
1321 | 2.17M | ret[len++] = *p++; |
1322 | 2.17M | } |
1323 | 27.0k | } else if (uri->query != NULL) { |
1324 | 0 | if (len + 3 >= max) { |
1325 | 0 | temp = xmlSaveUriRealloc(ret, &max); |
1326 | 0 | if (temp == NULL) goto mem_error; |
1327 | 0 | ret = temp; |
1328 | 0 | } |
1329 | 0 | ret[len++] = '?'; |
1330 | 0 | p = uri->query; |
1331 | 0 | while (*p != 0) { |
1332 | 0 | if (len + 3 >= max) { |
1333 | 0 | temp = xmlSaveUriRealloc(ret, &max); |
1334 | 0 | if (temp == NULL) goto mem_error; |
1335 | 0 | ret = temp; |
1336 | 0 | } |
1337 | 0 | if ((IS_UNRESERVED(*(p))) || (IS_RESERVED(*(p)))) |
1338 | 0 | ret[len++] = *p++; |
1339 | 0 | else { |
1340 | 0 | int val = *(unsigned char *)p++; |
1341 | 0 | int hi = val / 0x10, lo = val % 0x10; |
1342 | 0 | ret[len++] = '%'; |
1343 | 0 | ret[len++] = hi + (hi > 9? 'A'-10 : '0'); |
1344 | 0 | ret[len++] = lo + (lo > 9? 'A'-10 : '0'); |
1345 | 0 | } |
1346 | 0 | } |
1347 | 0 | } |
1348 | 30.0k | } |
1349 | 29.9k | if (uri->fragment != NULL) { |
1350 | 2.64k | if (len + 3 >= max) { |
1351 | 40 | temp = xmlSaveUriRealloc(ret, &max); |
1352 | 40 | if (temp == NULL) goto mem_error; |
1353 | 37 | ret = temp; |
1354 | 37 | } |
1355 | 2.63k | ret[len++] = '#'; |
1356 | 2.63k | p = uri->fragment; |
1357 | 624k | while (*p != 0) { |
1358 | 621k | if (len + 3 >= max) { |
1359 | 579 | temp = xmlSaveUriRealloc(ret, &max); |
1360 | 579 | if (temp == NULL) goto mem_error; |
1361 | 563 | ret = temp; |
1362 | 563 | } |
1363 | 621k | if ((IS_UNRESERVED(*(p))) || (IS_RESERVED(*(p)))) |
1364 | 620k | ret[len++] = *p++; |
1365 | 1.90k | else { |
1366 | 1.90k | int val = *(unsigned char *)p++; |
1367 | 1.90k | int hi = val / 0x10, lo = val % 0x10; |
1368 | 1.90k | ret[len++] = '%'; |
1369 | 1.90k | ret[len++] = hi + (hi > 9? 'A'-10 : '0'); |
1370 | 1.90k | ret[len++] = lo + (lo > 9? 'A'-10 : '0'); |
1371 | 1.90k | } |
1372 | 621k | } |
1373 | 2.63k | } |
1374 | 29.9k | if (len >= max) { |
1375 | 38 | temp = xmlSaveUriRealloc(ret, &max); |
1376 | 38 | if (temp == NULL) goto mem_error; |
1377 | 33 | ret = temp; |
1378 | 33 | } |
1379 | 29.9k | ret[len] = 0; |
1380 | 29.9k | return(ret); |
1381 | | |
1382 | 102 | mem_error: |
1383 | 102 | xmlFree(ret); |
1384 | 102 | return(NULL); |
1385 | 29.9k | } |
1386 | | |
1387 | | /** |
1388 | | * xmlPrintURI: |
1389 | | * @stream: a FILE* for the output |
1390 | | * @uri: pointer to an xmlURI |
1391 | | * |
1392 | | * Prints the URI in the stream @stream. |
1393 | | */ |
1394 | | void |
1395 | 0 | xmlPrintURI(FILE *stream, xmlURIPtr uri) { |
1396 | 0 | xmlChar *out; |
1397 | |
|
1398 | 0 | out = xmlSaveUri(uri); |
1399 | 0 | if (out != NULL) { |
1400 | 0 | fprintf(stream, "%s", (char *) out); |
1401 | 0 | xmlFree(out); |
1402 | 0 | } |
1403 | 0 | } |
1404 | | |
1405 | | /** |
1406 | | * xmlCleanURI: |
1407 | | * @uri: pointer to an xmlURI |
1408 | | * |
1409 | | * Make sure the xmlURI struct is free of content |
1410 | | */ |
1411 | | static void |
1412 | 2.25M | xmlCleanURI(xmlURIPtr uri) { |
1413 | 2.25M | if (uri == NULL) return; |
1414 | | |
1415 | 2.25M | if (uri->scheme != NULL) xmlFree(uri->scheme); |
1416 | 2.25M | uri->scheme = NULL; |
1417 | 2.25M | if (uri->server != NULL) xmlFree(uri->server); |
1418 | 2.25M | uri->server = NULL; |
1419 | 2.25M | if (uri->user != NULL) xmlFree(uri->user); |
1420 | 2.25M | uri->user = NULL; |
1421 | 2.25M | if (uri->path != NULL) xmlFree(uri->path); |
1422 | 2.25M | uri->path = NULL; |
1423 | 2.25M | if (uri->fragment != NULL) xmlFree(uri->fragment); |
1424 | 2.25M | uri->fragment = NULL; |
1425 | 2.25M | if (uri->opaque != NULL) xmlFree(uri->opaque); |
1426 | 2.25M | uri->opaque = NULL; |
1427 | 2.25M | if (uri->authority != NULL) xmlFree(uri->authority); |
1428 | 2.25M | uri->authority = NULL; |
1429 | 2.25M | if (uri->query != NULL) xmlFree(uri->query); |
1430 | 2.25M | uri->query = NULL; |
1431 | 2.25M | if (uri->query_raw != NULL) xmlFree(uri->query_raw); |
1432 | 2.25M | uri->query_raw = NULL; |
1433 | 2.25M | } |
1434 | | |
1435 | | /** |
1436 | | * xmlFreeURI: |
1437 | | * @uri: pointer to an xmlURI |
1438 | | * |
1439 | | * Free up the xmlURI struct |
1440 | | */ |
1441 | | void |
1442 | 921k | xmlFreeURI(xmlURIPtr uri) { |
1443 | 921k | if (uri == NULL) return; |
1444 | | |
1445 | 902k | if (uri->scheme != NULL) xmlFree(uri->scheme); |
1446 | 902k | if (uri->server != NULL) xmlFree(uri->server); |
1447 | 902k | if (uri->user != NULL) xmlFree(uri->user); |
1448 | 902k | if (uri->path != NULL) xmlFree(uri->path); |
1449 | 902k | if (uri->fragment != NULL) xmlFree(uri->fragment); |
1450 | 902k | if (uri->opaque != NULL) xmlFree(uri->opaque); |
1451 | 902k | if (uri->authority != NULL) xmlFree(uri->authority); |
1452 | 902k | if (uri->query != NULL) xmlFree(uri->query); |
1453 | 902k | if (uri->query_raw != NULL) xmlFree(uri->query_raw); |
1454 | 902k | xmlFree(uri); |
1455 | 902k | } |
1456 | | |
1457 | | /************************************************************************ |
1458 | | * * |
1459 | | * Helper functions * |
1460 | | * * |
1461 | | ************************************************************************/ |
1462 | | |
1463 | | static int |
1464 | 1.54M | xmlIsPathSeparator(int c, int isFile) { |
1465 | 1.54M | (void) isFile; |
1466 | | |
1467 | 1.54M | if (c == '/') |
1468 | 325k | return(1); |
1469 | | |
1470 | | #ifdef _WIN32 |
1471 | | if (isFile && (c == '\\')) |
1472 | | return(1); |
1473 | | #endif |
1474 | | |
1475 | 1.22M | return(0); |
1476 | 1.54M | } |
1477 | | |
1478 | | /** |
1479 | | * xmlNormalizePath: |
1480 | | * @path: pointer to the path string |
1481 | | * @isFile: true for filesystem paths, false for URIs |
1482 | | * |
1483 | | * Normalize a filesystem path or URI. |
1484 | | * |
1485 | | * Returns 0 or an error code |
1486 | | */ |
1487 | | static int |
1488 | 37.6k | xmlNormalizePath(char *path, int isFile) { |
1489 | 37.6k | char *cur, *out; |
1490 | 37.6k | int numSeg = 0; |
1491 | | |
1492 | 37.6k | if (path == NULL) |
1493 | 21.3k | return(-1); |
1494 | | |
1495 | 16.2k | cur = path; |
1496 | 16.2k | out = path; |
1497 | | |
1498 | 16.2k | if (*cur == 0) |
1499 | 98 | return(0); |
1500 | | |
1501 | 16.1k | if (xmlIsPathSeparator(*cur, isFile)) { |
1502 | 4.11k | cur++; |
1503 | 4.11k | *out++ = '/'; |
1504 | 4.11k | } |
1505 | | |
1506 | 135k | while (*cur != 0) { |
1507 | | /* |
1508 | | * At this point, out is either empty or ends with a separator. |
1509 | | * Collapse multiple separators first. |
1510 | | */ |
1511 | 313k | while (xmlIsPathSeparator(*cur, isFile)) { |
1512 | | #ifdef _WIN32 |
1513 | | /* Allow two separators at start of path */ |
1514 | | if ((isFile) && (out == path + 1)) |
1515 | | *out++ = '/'; |
1516 | | #endif |
1517 | 192k | cur++; |
1518 | 192k | } |
1519 | | |
1520 | 121k | if (*cur == '.') { |
1521 | 17.8k | if (cur[1] == 0) { |
1522 | | /* Ignore "." at end of path */ |
1523 | 966 | break; |
1524 | 16.8k | } else if (xmlIsPathSeparator(cur[1], isFile)) { |
1525 | | /* Skip "./" */ |
1526 | 2.43k | cur += 2; |
1527 | 2.43k | continue; |
1528 | 14.4k | } else if ((cur[1] == '.') && |
1529 | 14.4k | ((cur[2] == 0) || xmlIsPathSeparator(cur[2], isFile))) { |
1530 | 11.6k | if (numSeg > 0) { |
1531 | | /* Handle ".." by removing last segment */ |
1532 | 17.1k | do { |
1533 | 17.1k | out--; |
1534 | 17.1k | } while ((out > path) && |
1535 | 17.1k | !xmlIsPathSeparator(out[-1], isFile)); |
1536 | 4.11k | numSeg--; |
1537 | | |
1538 | 4.11k | if (cur[2] == 0) |
1539 | 361 | break; |
1540 | 3.75k | cur += 3; |
1541 | 3.75k | continue; |
1542 | 7.52k | } else if (out[0] == '/') { |
1543 | | /* Ignore extraneous ".." in absolute paths */ |
1544 | 3.36k | if (cur[2] == 0) |
1545 | 711 | break; |
1546 | 2.65k | cur += 3; |
1547 | 2.65k | continue; |
1548 | 4.15k | } else { |
1549 | | /* Keep "../" at start of relative path */ |
1550 | 4.15k | numSeg--; |
1551 | 4.15k | } |
1552 | 11.6k | } |
1553 | 17.8k | } |
1554 | | |
1555 | | /* Copy segment */ |
1556 | 876k | while ((*cur != 0) && !xmlIsPathSeparator(*cur, isFile)) { |
1557 | 765k | *out++ = *cur++; |
1558 | 765k | } |
1559 | | |
1560 | | /* Copy separator */ |
1561 | 110k | if (*cur != 0) { |
1562 | 99.0k | cur++; |
1563 | 99.0k | *out++ = '/'; |
1564 | 99.0k | } |
1565 | | |
1566 | 110k | numSeg++; |
1567 | 110k | } |
1568 | | |
1569 | | /* Keep "." if output is empty and it's a file */ |
1570 | 16.1k | if ((isFile) && (out <= path)) |
1571 | 105 | *out++ = '.'; |
1572 | 16.1k | *out = 0; |
1573 | | |
1574 | 16.1k | return(0); |
1575 | 16.2k | } |
1576 | | |
1577 | | /** |
1578 | | * xmlNormalizeURIPath: |
1579 | | * @path: pointer to the path string |
1580 | | * |
1581 | | * Applies the 5 normalization steps to a path string--that is, RFC 2396 |
1582 | | * Section 5.2, steps 6.c through 6.g. |
1583 | | * |
1584 | | * Normalization occurs directly on the string, no new allocation is done |
1585 | | * |
1586 | | * Returns 0 or an error code |
1587 | | */ |
1588 | | int |
1589 | 7.21k | xmlNormalizeURIPath(char *path) { |
1590 | 7.21k | return(xmlNormalizePath(path, 0)); |
1591 | 7.21k | } |
1592 | | |
1593 | 638k | static int is_hex(char c) { |
1594 | 638k | if (((c >= '0') && (c <= '9')) || |
1595 | 638k | ((c >= 'a') && (c <= 'f')) || |
1596 | 638k | ((c >= 'A') && (c <= 'F'))) |
1597 | 600k | return(1); |
1598 | 37.8k | return(0); |
1599 | 638k | } |
1600 | | |
1601 | | /** |
1602 | | * xmlURIUnescapeString: |
1603 | | * @str: the string to unescape |
1604 | | * @len: the length in bytes to unescape (or <= 0 to indicate full string) |
1605 | | * @target: optional destination buffer |
1606 | | * |
1607 | | * Unescaping routine, but does not check that the string is an URI. The |
1608 | | * output is a direct unsigned char translation of %XX values (no encoding) |
1609 | | * Note that the length of the result can only be smaller or same size as |
1610 | | * the input string. |
1611 | | * |
1612 | | * Returns a copy of the string, but unescaped, will return NULL only in case |
1613 | | * of error |
1614 | | */ |
1615 | | char * |
1616 | 1.06M | xmlURIUnescapeString(const char *str, int len, char *target) { |
1617 | 1.06M | char *ret, *out; |
1618 | 1.06M | const char *in; |
1619 | | |
1620 | 1.06M | if (str == NULL) |
1621 | 4 | return(NULL); |
1622 | 1.06M | if (len <= 0) len = strlen(str); |
1623 | 1.06M | if (len < 0) return(NULL); |
1624 | | |
1625 | 1.06M | if (target == NULL) { |
1626 | 1.06M | ret = (char *) xmlMallocAtomic(len + 1); |
1627 | 1.06M | if (ret == NULL) |
1628 | 768 | return(NULL); |
1629 | 1.06M | } else |
1630 | 0 | ret = target; |
1631 | 1.06M | in = str; |
1632 | 1.06M | out = ret; |
1633 | 43.1M | while(len > 0) { |
1634 | 42.1M | if ((len > 2) && (*in == '%') && (is_hex(in[1])) && (is_hex(in[2]))) { |
1635 | 288k | int c = 0; |
1636 | 288k | in++; |
1637 | 288k | if ((*in >= '0') && (*in <= '9')) |
1638 | 221k | c = (*in - '0'); |
1639 | 66.6k | else if ((*in >= 'a') && (*in <= 'f')) |
1640 | 36.2k | c = (*in - 'a') + 10; |
1641 | 30.4k | else if ((*in >= 'A') && (*in <= 'F')) |
1642 | 30.4k | c = (*in - 'A') + 10; |
1643 | 288k | in++; |
1644 | 288k | if ((*in >= '0') && (*in <= '9')) |
1645 | 27.0k | c = c * 16 + (*in - '0'); |
1646 | 261k | else if ((*in >= 'a') && (*in <= 'f')) |
1647 | 30.6k | c = c * 16 + (*in - 'a') + 10; |
1648 | 230k | else if ((*in >= 'A') && (*in <= 'F')) |
1649 | 230k | c = c * 16 + (*in - 'A') + 10; |
1650 | 288k | in++; |
1651 | 288k | len -= 3; |
1652 | | /* Explicit sign change */ |
1653 | 288k | *out++ = (char) c; |
1654 | 41.8M | } else { |
1655 | 41.8M | *out++ = *in++; |
1656 | 41.8M | len--; |
1657 | 41.8M | } |
1658 | 42.1M | } |
1659 | 1.06M | *out = 0; |
1660 | 1.06M | return(ret); |
1661 | 1.06M | } |
1662 | | |
1663 | | /** |
1664 | | * xmlURIEscapeStr: |
1665 | | * @str: string to escape |
1666 | | * @list: exception list string of chars not to escape |
1667 | | * |
1668 | | * This routine escapes a string to hex, ignoring unreserved characters |
1669 | | * a-z, A-Z, 0-9, "-._~", a few sub-delims "!*'()", the gen-delim "@" |
1670 | | * (why?) and the characters in the exception list. |
1671 | | * |
1672 | | * Returns a new escaped string or NULL in case of error. |
1673 | | */ |
1674 | | xmlChar * |
1675 | 60.0k | xmlURIEscapeStr(const xmlChar *str, const xmlChar *list) { |
1676 | 60.0k | xmlChar *ret, ch; |
1677 | 60.0k | xmlChar *temp; |
1678 | 60.0k | const xmlChar *in; |
1679 | 60.0k | int len, out; |
1680 | | |
1681 | 60.0k | if (str == NULL) |
1682 | 4 | return(NULL); |
1683 | 60.0k | if (str[0] == 0) |
1684 | 25.0k | return(xmlStrdup(str)); |
1685 | 35.0k | len = xmlStrlen(str); |
1686 | | |
1687 | 35.0k | len += 20; |
1688 | 35.0k | ret = (xmlChar *) xmlMallocAtomic(len); |
1689 | 35.0k | if (ret == NULL) |
1690 | 138 | return(NULL); |
1691 | 34.8k | in = (const xmlChar *) str; |
1692 | 34.8k | out = 0; |
1693 | 99.7M | while(*in != 0) { |
1694 | 99.7M | if (len - out <= 3) { |
1695 | 7.49k | if (len > INT_MAX / 2) |
1696 | 0 | return(NULL); |
1697 | 7.49k | temp = xmlRealloc(ret, len * 2); |
1698 | 7.49k | if (temp == NULL) { |
1699 | 27 | xmlFree(ret); |
1700 | 27 | return(NULL); |
1701 | 27 | } |
1702 | 7.46k | ret = temp; |
1703 | 7.46k | len *= 2; |
1704 | 7.46k | } |
1705 | | |
1706 | 99.7M | ch = *in; |
1707 | | |
1708 | 99.7M | if ((ch != '@') && (!IS_UNRESERVED(ch)) && (!xmlStrchr(list, ch))) { |
1709 | 96.4M | unsigned char val; |
1710 | 96.4M | ret[out++] = '%'; |
1711 | 96.4M | val = ch >> 4; |
1712 | 96.4M | if (val <= 9) |
1713 | 16.5M | ret[out++] = '0' + val; |
1714 | 79.8M | else |
1715 | 79.8M | ret[out++] = 'A' + val - 0xA; |
1716 | 96.4M | val = ch & 0xF; |
1717 | 96.4M | if (val <= 9) |
1718 | 55.6M | ret[out++] = '0' + val; |
1719 | 40.7M | else |
1720 | 40.7M | ret[out++] = 'A' + val - 0xA; |
1721 | 96.4M | in++; |
1722 | 96.4M | } else { |
1723 | 3.25M | ret[out++] = *in++; |
1724 | 3.25M | } |
1725 | | |
1726 | 99.7M | } |
1727 | 34.8k | ret[out] = 0; |
1728 | 34.8k | return(ret); |
1729 | 34.8k | } |
1730 | | |
1731 | | /** |
1732 | | * xmlURIEscape: |
1733 | | * @str: the string of the URI to escape |
1734 | | * |
1735 | | * Escaping routine, does not do validity checks ! |
1736 | | * It will try to escape the chars needing this, but this is heuristic |
1737 | | * based it's impossible to be sure. |
1738 | | * |
1739 | | * Returns an copy of the string, but escaped |
1740 | | * |
1741 | | * 25 May 2001 |
1742 | | * Uses xmlParseURI and xmlURIEscapeStr to try to escape correctly |
1743 | | * according to RFC2396. |
1744 | | * - Carl Douglas |
1745 | | */ |
1746 | | xmlChar * |
1747 | | xmlURIEscape(const xmlChar * str) |
1748 | 4.07k | { |
1749 | 4.07k | xmlChar *ret, *segment = NULL; |
1750 | 4.07k | xmlURIPtr uri; |
1751 | 4.07k | int ret2; |
1752 | | |
1753 | 4.07k | if (str == NULL) |
1754 | 4 | return (NULL); |
1755 | | |
1756 | 4.06k | uri = xmlCreateURI(); |
1757 | 4.06k | if (uri != NULL) { |
1758 | | /* |
1759 | | * Allow escaping errors in the unescaped form |
1760 | | */ |
1761 | 3.98k | uri->cleanup = XML_URI_ALLOW_UNWISE; |
1762 | 3.98k | ret2 = xmlParseURIReference(uri, (const char *)str); |
1763 | 3.98k | if (ret2) { |
1764 | 1.04k | xmlFreeURI(uri); |
1765 | 1.04k | return (NULL); |
1766 | 1.04k | } |
1767 | 3.98k | } |
1768 | | |
1769 | 3.02k | if (!uri) |
1770 | 81 | return NULL; |
1771 | | |
1772 | 2.94k | ret = NULL; |
1773 | | |
1774 | 3.61k | #define NULLCHK(p) if(!p) { \ |
1775 | 31 | xmlFreeURI(uri); \ |
1776 | 31 | xmlFree(ret); \ |
1777 | 31 | return NULL; } \ |
1778 | 2.94k | |
1779 | 2.94k | if (uri->scheme) { |
1780 | 733 | segment = xmlURIEscapeStr(BAD_CAST uri->scheme, BAD_CAST "+-."); |
1781 | 733 | NULLCHK(segment) |
1782 | 730 | ret = xmlStrcat(ret, segment); |
1783 | 730 | ret = xmlStrcat(ret, BAD_CAST ":"); |
1784 | 730 | xmlFree(segment); |
1785 | 730 | } |
1786 | | |
1787 | 2.93k | if (uri->authority) { |
1788 | 0 | segment = |
1789 | 0 | xmlURIEscapeStr(BAD_CAST uri->authority, BAD_CAST "/?;:@"); |
1790 | 0 | NULLCHK(segment) |
1791 | 0 | ret = xmlStrcat(ret, BAD_CAST "//"); |
1792 | 0 | ret = xmlStrcat(ret, segment); |
1793 | 0 | xmlFree(segment); |
1794 | 0 | } |
1795 | | |
1796 | 2.93k | if (uri->user) { |
1797 | 501 | segment = xmlURIEscapeStr(BAD_CAST uri->user, BAD_CAST ";:&=+$,"); |
1798 | 501 | NULLCHK(segment) |
1799 | 497 | ret = xmlStrcat(ret,BAD_CAST "//"); |
1800 | 497 | ret = xmlStrcat(ret, segment); |
1801 | 497 | ret = xmlStrcat(ret, BAD_CAST "@"); |
1802 | 497 | xmlFree(segment); |
1803 | 497 | } |
1804 | | |
1805 | 2.93k | if (uri->server) { |
1806 | 710 | segment = xmlURIEscapeStr(BAD_CAST uri->server, BAD_CAST "/?;:@"); |
1807 | 710 | NULLCHK(segment) |
1808 | 704 | if (uri->user == NULL) |
1809 | 316 | ret = xmlStrcat(ret, BAD_CAST "//"); |
1810 | 704 | ret = xmlStrcat(ret, segment); |
1811 | 704 | xmlFree(segment); |
1812 | 704 | } |
1813 | | |
1814 | 2.92k | if (uri->port > 0) { |
1815 | 182 | xmlChar port[11]; |
1816 | | |
1817 | 182 | snprintf((char *) port, 11, "%d", uri->port); |
1818 | 182 | ret = xmlStrcat(ret, BAD_CAST ":"); |
1819 | 182 | ret = xmlStrcat(ret, port); |
1820 | 182 | } |
1821 | | |
1822 | 2.92k | if (uri->path) { |
1823 | 1.18k | segment = |
1824 | 1.18k | xmlURIEscapeStr(BAD_CAST uri->path, BAD_CAST ":@&=+$,/?;"); |
1825 | 1.18k | NULLCHK(segment) |
1826 | 1.16k | ret = xmlStrcat(ret, segment); |
1827 | 1.16k | xmlFree(segment); |
1828 | 1.16k | } |
1829 | | |
1830 | 2.91k | if (uri->query_raw) { |
1831 | 339 | ret = xmlStrcat(ret, BAD_CAST "?"); |
1832 | 339 | ret = xmlStrcat(ret, BAD_CAST uri->query_raw); |
1833 | 339 | } |
1834 | 2.57k | else if (uri->query) { |
1835 | 0 | segment = |
1836 | 0 | xmlURIEscapeStr(BAD_CAST uri->query, BAD_CAST ";/?:@&=+,$"); |
1837 | 0 | NULLCHK(segment) |
1838 | 0 | ret = xmlStrcat(ret, BAD_CAST "?"); |
1839 | 0 | ret = xmlStrcat(ret, segment); |
1840 | 0 | xmlFree(segment); |
1841 | 0 | } |
1842 | | |
1843 | 2.91k | if (uri->opaque) { |
1844 | 0 | segment = xmlURIEscapeStr(BAD_CAST uri->opaque, BAD_CAST ""); |
1845 | 0 | NULLCHK(segment) |
1846 | 0 | ret = xmlStrcat(ret, segment); |
1847 | 0 | xmlFree(segment); |
1848 | 0 | } |
1849 | | |
1850 | 2.91k | if (uri->fragment) { |
1851 | 488 | segment = xmlURIEscapeStr(BAD_CAST uri->fragment, BAD_CAST "#"); |
1852 | 488 | NULLCHK(segment) |
1853 | 485 | ret = xmlStrcat(ret, BAD_CAST "#"); |
1854 | 485 | ret = xmlStrcat(ret, segment); |
1855 | 485 | xmlFree(segment); |
1856 | 485 | } |
1857 | | |
1858 | 2.91k | xmlFreeURI(uri); |
1859 | 2.91k | #undef NULLCHK |
1860 | | |
1861 | 2.91k | return (ret); |
1862 | 2.91k | } |
1863 | | |
1864 | | /************************************************************************ |
1865 | | * * |
1866 | | * Public functions * |
1867 | | * * |
1868 | | ************************************************************************/ |
1869 | | |
1870 | | static int |
1871 | 34.5k | xmlIsAbsolutePath(const xmlChar *path) { |
1872 | 34.5k | int c = path[0]; |
1873 | | |
1874 | 34.5k | if (xmlIsPathSeparator(c, 1)) |
1875 | 4.07k | return(1); |
1876 | | |
1877 | | #ifdef _WIN32 |
1878 | | if ((((c >= 'A') && (c <= 'Z')) || |
1879 | | ((c >= 'a') && (c <= 'z'))) && |
1880 | | (path[1] == ':')) |
1881 | | return(1); |
1882 | | #endif |
1883 | | |
1884 | 30.4k | return(0); |
1885 | 34.5k | } |
1886 | | |
1887 | | /** |
1888 | | * xmlResolvePath: |
1889 | | * @ref: the filesystem path |
1890 | | * @base: the base value |
1891 | | * @out: pointer to result URI |
1892 | | * |
1893 | | * Resolves a filesystem path from a base path. |
1894 | | * |
1895 | | * Returns 0 on success, -1 if a memory allocation failed or an error |
1896 | | * code if URI or base are invalid. |
1897 | | */ |
1898 | | static int |
1899 | 49.9k | xmlResolvePath(const xmlChar *escRef, const xmlChar *base, xmlChar **out) { |
1900 | 49.9k | const xmlChar *fragment; |
1901 | 49.9k | xmlChar *tmp = NULL; |
1902 | 49.9k | xmlChar *ref = NULL; |
1903 | 49.9k | xmlChar *result = NULL; |
1904 | 49.9k | int ret = -1; |
1905 | 49.9k | int i; |
1906 | | |
1907 | 49.9k | if (out == NULL) |
1908 | 0 | return(1); |
1909 | 49.9k | *out = NULL; |
1910 | | |
1911 | 49.9k | if ((escRef == NULL) || (escRef[0] == 0)) { |
1912 | 3.74k | if ((base == NULL) || (base[0] == 0)) |
1913 | 240 | return(1); |
1914 | 3.50k | ref = xmlStrdup(base); |
1915 | 3.50k | if (ref == NULL) |
1916 | 13 | goto err_memory; |
1917 | 3.49k | *out = ref; |
1918 | 3.49k | return(0); |
1919 | 3.50k | } |
1920 | | |
1921 | | /* |
1922 | | * If a URI is resolved, we can assume it is a valid URI and not |
1923 | | * a filesystem path. This means we have to unescape the part |
1924 | | * before the fragment. |
1925 | | */ |
1926 | 46.2k | fragment = xmlStrchr(escRef, '#'); |
1927 | 46.2k | if (fragment != NULL) { |
1928 | 1.33k | tmp = xmlStrndup(escRef, fragment - escRef); |
1929 | 1.33k | if (tmp == NULL) |
1930 | 18 | goto err_memory; |
1931 | 1.32k | escRef = tmp; |
1932 | 1.32k | } |
1933 | | |
1934 | 46.2k | ref = (xmlChar *) xmlURIUnescapeString((char *) escRef, -1, NULL); |
1935 | 46.2k | if (ref == NULL) |
1936 | 65 | goto err_memory; |
1937 | | |
1938 | 46.1k | if ((base == NULL) || (base[0] == 0)) |
1939 | 11.6k | goto done; |
1940 | | |
1941 | 34.5k | if (xmlIsAbsolutePath(ref)) |
1942 | 4.07k | goto done; |
1943 | | |
1944 | | /* |
1945 | | * Remove last segment from base |
1946 | | */ |
1947 | 30.4k | i = xmlStrlen(base); |
1948 | 296k | while ((i > 0) && !xmlIsPathSeparator(base[i-1], 1)) |
1949 | 266k | i--; |
1950 | | |
1951 | | /* |
1952 | | * Concatenate base and ref |
1953 | | */ |
1954 | 30.4k | if (i > 0) { |
1955 | 9.12k | int refLen = xmlStrlen(ref); |
1956 | | |
1957 | 9.12k | result = xmlMalloc(i + refLen + 1); |
1958 | 9.12k | if (result == NULL) |
1959 | 22 | goto err_memory; |
1960 | | |
1961 | 9.09k | memcpy(result, base, i); |
1962 | 9.09k | memcpy(result + i, ref, refLen + 1); |
1963 | 9.09k | } |
1964 | | |
1965 | | /* |
1966 | | * Normalize |
1967 | | */ |
1968 | 30.4k | xmlNormalizePath((char *) result, 1); |
1969 | | |
1970 | 46.1k | done: |
1971 | 46.1k | if (result == NULL) { |
1972 | 37.0k | result = ref; |
1973 | 37.0k | ref = NULL; |
1974 | 37.0k | } |
1975 | | |
1976 | 46.1k | if (fragment != NULL) { |
1977 | 1.29k | result = xmlStrcat(result, fragment); |
1978 | 1.29k | if (result == NULL) |
1979 | 22 | goto err_memory; |
1980 | 1.29k | } |
1981 | | |
1982 | 46.1k | *out = result; |
1983 | 46.1k | ret = 0; |
1984 | | |
1985 | 46.2k | err_memory: |
1986 | 46.2k | xmlFree(tmp); |
1987 | 46.2k | xmlFree(ref); |
1988 | 46.2k | return(ret); |
1989 | 46.1k | } |
1990 | | |
1991 | | /** |
1992 | | * xmlBulidURISafe: |
1993 | | * @URI: the URI instance found in the document |
1994 | | * @base: the base value |
1995 | | * @valPtr: pointer to result URI |
1996 | | * |
1997 | | * Computes he final URI of the reference done by checking that |
1998 | | * the given URI is valid, and building the final URI using the |
1999 | | * base URI. This is processed according to section 5.2 of the |
2000 | | * RFC 2396 |
2001 | | * |
2002 | | * 5.2. Resolving Relative References to Absolute Form |
2003 | | * |
2004 | | * Returns 0 on success, -1 if a memory allocation failed or an error |
2005 | | * code if URI or base are invalid. |
2006 | | */ |
2007 | | int |
2008 | 75.2k | xmlBuildURISafe(const xmlChar *URI, const xmlChar *base, xmlChar **valPtr) { |
2009 | 75.2k | xmlChar *val = NULL; |
2010 | 75.2k | int ret, len, indx, cur, out; |
2011 | 75.2k | xmlURIPtr ref = NULL; |
2012 | 75.2k | xmlURIPtr bas = NULL; |
2013 | 75.2k | xmlURIPtr res = NULL; |
2014 | | |
2015 | | /* |
2016 | | * 1) The URI reference is parsed into the potential four components and |
2017 | | * fragment identifier, as described in Section 4.3. |
2018 | | * |
2019 | | * NOTE that a completely empty URI is treated by modern browsers |
2020 | | * as a reference to "." rather than as a synonym for the current |
2021 | | * URI. Should we do that here? |
2022 | | */ |
2023 | 75.2k | if (URI == NULL) |
2024 | 5.16k | ret = 1; |
2025 | 70.1k | else if (URI[0] != 0) |
2026 | 65.7k | ret = xmlParseURISafe((const char *) URI, &ref); |
2027 | 4.34k | else |
2028 | 4.34k | ret = 0; |
2029 | 75.2k | if (ret != 0) |
2030 | 12.8k | goto done; |
2031 | 62.4k | if ((ref != NULL) && (ref->scheme != NULL)) { |
2032 | | /* |
2033 | | * The URI is absolute don't modify. |
2034 | | */ |
2035 | 1.20k | val = xmlStrdup(URI); |
2036 | 1.20k | if (val == NULL) |
2037 | 10 | ret = -1; |
2038 | 1.20k | goto done; |
2039 | 1.20k | } |
2040 | | |
2041 | | /* |
2042 | | * If base has no scheme or authority, it is assumed to be a |
2043 | | * filesystem path. |
2044 | | */ |
2045 | 61.2k | if (xmlStrstr(base, BAD_CAST "://") == NULL) { |
2046 | 49.9k | xmlFreeURI(ref); |
2047 | 49.9k | return(xmlResolvePath(URI, base, valPtr)); |
2048 | 49.9k | } |
2049 | | |
2050 | 11.2k | ret = xmlParseURISafe((const char *) base, &bas); |
2051 | 11.2k | if (ret < 0) |
2052 | 73 | goto done; |
2053 | 11.2k | if (ret != 0) { |
2054 | 5.06k | if (ref) { |
2055 | 4.87k | ret = 0; |
2056 | 4.87k | val = xmlSaveUri(ref); |
2057 | 4.87k | if (val == NULL) |
2058 | 107 | ret = -1; |
2059 | 4.87k | } |
2060 | 5.06k | goto done; |
2061 | 5.06k | } |
2062 | 6.13k | if (ref == NULL) { |
2063 | | /* |
2064 | | * the base fragment must be ignored |
2065 | | */ |
2066 | 391 | if (bas->fragment != NULL) { |
2067 | 63 | xmlFree(bas->fragment); |
2068 | 63 | bas->fragment = NULL; |
2069 | 63 | } |
2070 | 391 | val = xmlSaveUri(bas); |
2071 | 391 | if (val == NULL) |
2072 | 23 | ret = -1; |
2073 | 391 | goto done; |
2074 | 391 | } |
2075 | | |
2076 | | /* |
2077 | | * 2) If the path component is empty and the scheme, authority, and |
2078 | | * query components are undefined, then it is a reference to the |
2079 | | * current document and we are done. Otherwise, the reference URI's |
2080 | | * query and fragment components are defined as found (or not found) |
2081 | | * within the URI reference and not inherited from the base URI. |
2082 | | * |
2083 | | * NOTE that in modern browsers, the parsing differs from the above |
2084 | | * in the following aspect: the query component is allowed to be |
2085 | | * defined while still treating this as a reference to the current |
2086 | | * document. |
2087 | | */ |
2088 | 5.74k | ret = -1; |
2089 | 5.74k | res = xmlCreateURI(); |
2090 | 5.74k | if (res == NULL) |
2091 | 18 | goto done; |
2092 | 5.72k | if ((ref->scheme == NULL) && (ref->path == NULL) && |
2093 | 5.72k | ((ref->authority == NULL) && (ref->server == NULL) && |
2094 | 2.23k | (ref->port == PORT_EMPTY))) { |
2095 | 1.87k | if (bas->scheme != NULL) { |
2096 | 1.54k | res->scheme = xmlMemStrdup(bas->scheme); |
2097 | 1.54k | if (res->scheme == NULL) |
2098 | 10 | goto done; |
2099 | 1.54k | } |
2100 | 1.86k | if (bas->authority != NULL) { |
2101 | 0 | res->authority = xmlMemStrdup(bas->authority); |
2102 | 0 | if (res->authority == NULL) |
2103 | 0 | goto done; |
2104 | 1.86k | } else { |
2105 | 1.86k | if (bas->server != NULL) { |
2106 | 1.11k | res->server = xmlMemStrdup(bas->server); |
2107 | 1.11k | if (res->server == NULL) |
2108 | 11 | goto done; |
2109 | 1.11k | } |
2110 | 1.84k | if (bas->user != NULL) { |
2111 | 239 | res->user = xmlMemStrdup(bas->user); |
2112 | 239 | if (res->user == NULL) |
2113 | 6 | goto done; |
2114 | 239 | } |
2115 | 1.84k | res->port = bas->port; |
2116 | 1.84k | } |
2117 | 1.84k | if (bas->path != NULL) { |
2118 | 340 | res->path = xmlMemStrdup(bas->path); |
2119 | 340 | if (res->path == NULL) |
2120 | 5 | goto done; |
2121 | 340 | } |
2122 | 1.83k | if (ref->query_raw != NULL) { |
2123 | 256 | res->query_raw = xmlMemStrdup (ref->query_raw); |
2124 | 256 | if (res->query_raw == NULL) |
2125 | 7 | goto done; |
2126 | 1.58k | } else if (ref->query != NULL) { |
2127 | 0 | res->query = xmlMemStrdup(ref->query); |
2128 | 0 | if (res->query == NULL) |
2129 | 0 | goto done; |
2130 | 1.58k | } else if (bas->query_raw != NULL) { |
2131 | 293 | res->query_raw = xmlMemStrdup(bas->query_raw); |
2132 | 293 | if (res->query_raw == NULL) |
2133 | 7 | goto done; |
2134 | 1.28k | } else if (bas->query != NULL) { |
2135 | 0 | res->query = xmlMemStrdup(bas->query); |
2136 | 0 | if (res->query == NULL) |
2137 | 0 | goto done; |
2138 | 0 | } |
2139 | 1.82k | if (ref->fragment != NULL) { |
2140 | 626 | res->fragment = xmlMemStrdup(ref->fragment); |
2141 | 626 | if (res->fragment == NULL) |
2142 | 14 | goto done; |
2143 | 626 | } |
2144 | 1.81k | goto step_7; |
2145 | 1.82k | } |
2146 | | |
2147 | | /* |
2148 | | * 3) If the scheme component is defined, indicating that the reference |
2149 | | * starts with a scheme name, then the reference is interpreted as an |
2150 | | * absolute URI and we are done. Otherwise, the reference URI's |
2151 | | * scheme is inherited from the base URI's scheme component. |
2152 | | */ |
2153 | 3.85k | if (ref->scheme != NULL) { |
2154 | 0 | val = xmlSaveUri(ref); |
2155 | 0 | if (val != NULL) |
2156 | 0 | ret = 0; |
2157 | 0 | goto done; |
2158 | 0 | } |
2159 | 3.85k | if (bas->scheme != NULL) { |
2160 | 3.03k | res->scheme = xmlMemStrdup(bas->scheme); |
2161 | 3.03k | if (res->scheme == NULL) |
2162 | 19 | goto done; |
2163 | 3.03k | } |
2164 | | |
2165 | 3.84k | if (ref->query_raw != NULL) { |
2166 | 880 | res->query_raw = xmlMemStrdup(ref->query_raw); |
2167 | 880 | if (res->query_raw == NULL) |
2168 | 7 | goto done; |
2169 | 2.96k | } else if (ref->query != NULL) { |
2170 | 0 | res->query = xmlMemStrdup(ref->query); |
2171 | 0 | if (res->query == NULL) |
2172 | 0 | goto done; |
2173 | 0 | } |
2174 | 3.83k | if (ref->fragment != NULL) { |
2175 | 313 | res->fragment = xmlMemStrdup(ref->fragment); |
2176 | 313 | if (res->fragment == NULL) |
2177 | 7 | goto done; |
2178 | 313 | } |
2179 | | |
2180 | | /* |
2181 | | * 4) If the authority component is defined, then the reference is a |
2182 | | * network-path and we skip to step 7. Otherwise, the reference |
2183 | | * URI's authority is inherited from the base URI's authority |
2184 | | * component, which will also be undefined if the URI scheme does not |
2185 | | * use an authority component. |
2186 | | */ |
2187 | 3.82k | if ((ref->authority != NULL) || (ref->server != NULL) || |
2188 | 3.82k | (ref->port != PORT_EMPTY)) { |
2189 | 469 | if (ref->authority != NULL) { |
2190 | 0 | res->authority = xmlMemStrdup(ref->authority); |
2191 | 0 | if (res->authority == NULL) |
2192 | 0 | goto done; |
2193 | 469 | } else { |
2194 | 469 | if (ref->server != NULL) { |
2195 | 387 | res->server = xmlMemStrdup(ref->server); |
2196 | 387 | if (res->server == NULL) |
2197 | 5 | goto done; |
2198 | 387 | } |
2199 | 464 | if (ref->user != NULL) { |
2200 | 284 | res->user = xmlMemStrdup(ref->user); |
2201 | 284 | if (res->user == NULL) |
2202 | 6 | goto done; |
2203 | 284 | } |
2204 | 458 | res->port = ref->port; |
2205 | 458 | } |
2206 | 458 | if (ref->path != NULL) { |
2207 | 96 | res->path = xmlMemStrdup(ref->path); |
2208 | 96 | if (res->path == NULL) |
2209 | 6 | goto done; |
2210 | 96 | } |
2211 | 452 | goto step_7; |
2212 | 458 | } |
2213 | 3.35k | if (bas->authority != NULL) { |
2214 | 0 | res->authority = xmlMemStrdup(bas->authority); |
2215 | 0 | if (res->authority == NULL) |
2216 | 0 | goto done; |
2217 | 3.35k | } else if ((bas->server != NULL) || (bas->port != PORT_EMPTY)) { |
2218 | 2.06k | if (bas->server != NULL) { |
2219 | 1.39k | res->server = xmlMemStrdup(bas->server); |
2220 | 1.39k | if (res->server == NULL) |
2221 | 13 | goto done; |
2222 | 1.39k | } |
2223 | 2.05k | if (bas->user != NULL) { |
2224 | 701 | res->user = xmlMemStrdup(bas->user); |
2225 | 701 | if (res->user == NULL) |
2226 | 7 | goto done; |
2227 | 701 | } |
2228 | 2.04k | res->port = bas->port; |
2229 | 2.04k | } |
2230 | | |
2231 | | /* |
2232 | | * 5) If the path component begins with a slash character ("/"), then |
2233 | | * the reference is an absolute-path and we skip to step 7. |
2234 | | */ |
2235 | 3.33k | if ((ref->path != NULL) && (ref->path[0] == '/')) { |
2236 | 166 | res->path = xmlMemStrdup(ref->path); |
2237 | 166 | if (res->path == NULL) |
2238 | 7 | goto done; |
2239 | 159 | goto step_7; |
2240 | 166 | } |
2241 | | |
2242 | | |
2243 | | /* |
2244 | | * 6) If this step is reached, then we are resolving a relative-path |
2245 | | * reference. The relative path needs to be merged with the base |
2246 | | * URI's path. Although there are many ways to do this, we will |
2247 | | * describe a simple method using a separate string buffer. |
2248 | | * |
2249 | | * Allocate a buffer large enough for the result string. |
2250 | | */ |
2251 | 3.17k | len = 2; /* extra / and 0 */ |
2252 | 3.17k | if (ref->path != NULL) |
2253 | 3.17k | len += strlen(ref->path); |
2254 | 3.17k | if (bas->path != NULL) |
2255 | 1.64k | len += strlen(bas->path); |
2256 | 3.17k | res->path = (char *) xmlMallocAtomic(len); |
2257 | 3.17k | if (res->path == NULL) |
2258 | 31 | goto done; |
2259 | 3.14k | res->path[0] = 0; |
2260 | | |
2261 | | /* |
2262 | | * a) All but the last segment of the base URI's path component is |
2263 | | * copied to the buffer. In other words, any characters after the |
2264 | | * last (right-most) slash character, if any, are excluded. |
2265 | | */ |
2266 | 3.14k | cur = 0; |
2267 | 3.14k | out = 0; |
2268 | 3.14k | if (bas->path != NULL) { |
2269 | 42.1k | while (bas->path[cur] != 0) { |
2270 | 88.6k | while ((bas->path[cur] != 0) && (bas->path[cur] != '/')) |
2271 | 46.8k | cur++; |
2272 | 41.8k | if (bas->path[cur] == 0) |
2273 | 1.24k | break; |
2274 | | |
2275 | 40.5k | cur++; |
2276 | 119k | while (out < cur) { |
2277 | 78.5k | res->path[out] = bas->path[out]; |
2278 | 78.5k | out++; |
2279 | 78.5k | } |
2280 | 40.5k | } |
2281 | 1.62k | } |
2282 | 3.14k | res->path[out] = 0; |
2283 | | |
2284 | | /* |
2285 | | * b) The reference's path component is appended to the buffer |
2286 | | * string. |
2287 | | */ |
2288 | 3.14k | if (ref->path != NULL && ref->path[0] != 0) { |
2289 | 3.12k | indx = 0; |
2290 | | /* |
2291 | | * Ensure the path includes a '/' |
2292 | | */ |
2293 | 3.12k | if ((out == 0) && ((bas->server != NULL) || bas->port != PORT_EMPTY)) |
2294 | 1.38k | res->path[out++] = '/'; |
2295 | 147k | while (ref->path[indx] != 0) { |
2296 | 144k | res->path[out++] = ref->path[indx++]; |
2297 | 144k | } |
2298 | 3.12k | } |
2299 | 3.14k | res->path[out] = 0; |
2300 | | |
2301 | | /* |
2302 | | * Steps c) to h) are really path normalization steps |
2303 | | */ |
2304 | 3.14k | xmlNormalizeURIPath(res->path); |
2305 | | |
2306 | 5.56k | step_7: |
2307 | | |
2308 | | /* |
2309 | | * 7) The resulting URI components, including any inherited from the |
2310 | | * base URI, are recombined to give the absolute form of the URI |
2311 | | * reference. |
2312 | | */ |
2313 | 5.56k | val = xmlSaveUri(res); |
2314 | 5.56k | if (val != NULL) |
2315 | 5.49k | ret = 0; |
2316 | | |
2317 | 25.2k | done: |
2318 | 25.2k | if (ref != NULL) |
2319 | 11.8k | xmlFreeURI(ref); |
2320 | 25.2k | if (bas != NULL) |
2321 | 6.13k | xmlFreeURI(bas); |
2322 | 25.2k | if (res != NULL) |
2323 | 5.72k | xmlFreeURI(res); |
2324 | 25.2k | *valPtr = val; |
2325 | 25.2k | return(ret); |
2326 | 5.56k | } |
2327 | | |
2328 | | /** |
2329 | | * xmlBuildURI: |
2330 | | * @URI: the URI instance found in the document |
2331 | | * @base: the base value |
2332 | | * |
2333 | | * Computes he final URI of the reference done by checking that |
2334 | | * the given URI is valid, and building the final URI using the |
2335 | | * base URI. This is processed according to section 5.2 of the |
2336 | | * RFC 2396 |
2337 | | * |
2338 | | * 5.2. Resolving Relative References to Absolute Form |
2339 | | * |
2340 | | * Returns a new URI string (to be freed by the caller) or NULL in case |
2341 | | * of error. |
2342 | | */ |
2343 | | xmlChar * |
2344 | 10.1k | xmlBuildURI(const xmlChar *URI, const xmlChar *base) { |
2345 | 10.1k | xmlChar *out; |
2346 | | |
2347 | 10.1k | xmlBuildURISafe(URI, base, &out); |
2348 | 10.1k | return(out); |
2349 | 10.1k | } |
2350 | | |
2351 | | /** |
2352 | | * xmlBuildRelativeURISafe: |
2353 | | * @URI: the URI reference under consideration |
2354 | | * @base: the base value |
2355 | | * @valPtr: pointer to result URI |
2356 | | * |
2357 | | * Expresses the URI of the reference in terms relative to the |
2358 | | * base. Some examples of this operation include: |
2359 | | * base = "http://site1.com/docs/book1.html" |
2360 | | * URI input URI returned |
2361 | | * docs/pic1.gif pic1.gif |
2362 | | * docs/img/pic1.gif img/pic1.gif |
2363 | | * img/pic1.gif ../img/pic1.gif |
2364 | | * http://site1.com/docs/pic1.gif pic1.gif |
2365 | | * http://site2.com/docs/pic1.gif http://site2.com/docs/pic1.gif |
2366 | | * |
2367 | | * base = "docs/book1.html" |
2368 | | * URI input URI returned |
2369 | | * docs/pic1.gif pic1.gif |
2370 | | * docs/img/pic1.gif img/pic1.gif |
2371 | | * img/pic1.gif ../img/pic1.gif |
2372 | | * http://site1.com/docs/pic1.gif http://site1.com/docs/pic1.gif |
2373 | | * |
2374 | | * |
2375 | | * Note: if the URI reference is really weird or complicated, it may be |
2376 | | * worthwhile to first convert it into a "nice" one by calling |
2377 | | * xmlBuildURI (using 'base') before calling this routine, |
2378 | | * since this routine (for reasonable efficiency) assumes URI has |
2379 | | * already been through some validation. |
2380 | | * |
2381 | | * Returns 0 on success, -1 if a memory allocation failed or an error |
2382 | | * code if URI or base are invalid. |
2383 | | */ |
2384 | | int |
2385 | | xmlBuildRelativeURISafe(const xmlChar * URI, const xmlChar * base, |
2386 | | xmlChar **valPtr) |
2387 | 11.0k | { |
2388 | 11.0k | xmlChar *val = NULL; |
2389 | 11.0k | int ret = 0; |
2390 | 11.0k | int ix; |
2391 | 11.0k | int nbslash = 0; |
2392 | 11.0k | int len; |
2393 | 11.0k | xmlURIPtr ref = NULL; |
2394 | 11.0k | xmlURIPtr bas = NULL; |
2395 | 11.0k | xmlChar *bptr, *uptr, *vptr; |
2396 | 11.0k | int remove_path = 0; |
2397 | | |
2398 | 11.0k | if (valPtr == NULL) |
2399 | 0 | return(1); |
2400 | 11.0k | *valPtr = NULL; |
2401 | 11.0k | if ((URI == NULL) || (*URI == 0)) |
2402 | 5.24k | return(1); |
2403 | | |
2404 | | /* |
2405 | | * First parse URI into a standard form |
2406 | | */ |
2407 | 5.81k | ref = xmlCreateURI (); |
2408 | 5.81k | if (ref == NULL) { |
2409 | 9 | ret = -1; |
2410 | 9 | goto done; |
2411 | 9 | } |
2412 | | /* If URI not already in "relative" form */ |
2413 | 5.80k | if (URI[0] != '.') { |
2414 | 2.78k | ret = xmlParseURIReference (ref, (const char *) URI); |
2415 | 2.78k | if (ret != 0) |
2416 | 434 | goto done; /* Error in URI, return NULL */ |
2417 | 3.01k | } else { |
2418 | 3.01k | ref->path = (char *)xmlStrdup(URI); |
2419 | 3.01k | if (ref->path == NULL) { |
2420 | 8 | ret = -1; |
2421 | 8 | goto done; |
2422 | 8 | } |
2423 | 3.01k | } |
2424 | | |
2425 | | /* |
2426 | | * Next parse base into the same standard form |
2427 | | */ |
2428 | 5.36k | if ((base == NULL) || (*base == 0)) { |
2429 | 1.22k | val = xmlStrdup (URI); |
2430 | 1.22k | if (val == NULL) |
2431 | 3 | ret = -1; |
2432 | 1.22k | goto done; |
2433 | 1.22k | } |
2434 | 4.13k | bas = xmlCreateURI (); |
2435 | 4.13k | if (bas == NULL) { |
2436 | 14 | ret = -1; |
2437 | 14 | goto done; |
2438 | 14 | } |
2439 | 4.11k | if (base[0] != '.') { |
2440 | 3.69k | ret = xmlParseURIReference (bas, (const char *) base); |
2441 | 3.69k | if (ret != 0) |
2442 | 716 | goto done; /* Error in base, return NULL */ |
2443 | 3.69k | } else { |
2444 | 420 | bas->path = (char *)xmlStrdup(base); |
2445 | 420 | if (bas->path == NULL) { |
2446 | 2 | ret = -1; |
2447 | 2 | goto done; |
2448 | 2 | } |
2449 | 420 | } |
2450 | | |
2451 | | /* |
2452 | | * If the scheme / server on the URI differs from the base, |
2453 | | * just return the URI |
2454 | | */ |
2455 | 3.40k | if ((ref->scheme != NULL) && |
2456 | 3.40k | ((bas->scheme == NULL) || |
2457 | 445 | (xmlStrcmp ((xmlChar *)bas->scheme, (xmlChar *)ref->scheme)) || |
2458 | 445 | (xmlStrcmp ((xmlChar *)bas->server, (xmlChar *)ref->server)) || |
2459 | 445 | (bas->port != ref->port))) { |
2460 | 426 | val = xmlStrdup (URI); |
2461 | 426 | if (val == NULL) |
2462 | 4 | ret = -1; |
2463 | 426 | goto done; |
2464 | 426 | } |
2465 | 2.97k | if (xmlStrEqual((xmlChar *)bas->path, (xmlChar *)ref->path)) { |
2466 | 275 | val = xmlStrdup(BAD_CAST ""); |
2467 | 275 | if (val == NULL) |
2468 | 2 | ret = -1; |
2469 | 275 | goto done; |
2470 | 275 | } |
2471 | 2.70k | if (bas->path == NULL) { |
2472 | 532 | val = xmlStrdup((xmlChar *)ref->path); |
2473 | 532 | if (val == NULL) |
2474 | 2 | ret = -1; |
2475 | 532 | goto done; |
2476 | 532 | } |
2477 | 2.16k | if (ref->path == NULL) { |
2478 | 171 | ref->path = (char *) "/"; |
2479 | 171 | remove_path = 1; |
2480 | 171 | } |
2481 | | |
2482 | | /* |
2483 | | * At this point (at last!) we can compare the two paths |
2484 | | * |
2485 | | * First we take care of the special case where either of the |
2486 | | * two path components may be missing (bug 316224) |
2487 | | */ |
2488 | 2.16k | bptr = (xmlChar *)bas->path; |
2489 | 2.16k | { |
2490 | 2.16k | xmlChar *rptr = (xmlChar *) ref->path; |
2491 | 2.16k | int pos = 0; |
2492 | | |
2493 | | /* |
2494 | | * Next we compare the two strings and find where they first differ |
2495 | | */ |
2496 | 2.16k | if ((*rptr == '.') && (rptr[1] == '/')) |
2497 | 71 | rptr += 2; |
2498 | 2.16k | if ((*bptr == '.') && (bptr[1] == '/')) |
2499 | 126 | bptr += 2; |
2500 | 2.04k | else if ((*bptr == '/') && (*rptr != '/')) |
2501 | 180 | bptr++; |
2502 | 22.4k | while ((bptr[pos] == rptr[pos]) && (bptr[pos] != 0)) |
2503 | 20.3k | pos++; |
2504 | | |
2505 | 2.16k | if (bptr[pos] == rptr[pos]) { |
2506 | 109 | val = xmlStrdup(BAD_CAST ""); |
2507 | 109 | if (val == NULL) |
2508 | 2 | ret = -1; |
2509 | 109 | goto done; /* (I can't imagine why anyone would do this) */ |
2510 | 109 | } |
2511 | | |
2512 | | /* |
2513 | | * In URI, "back up" to the last '/' encountered. This will be the |
2514 | | * beginning of the "unique" suffix of URI |
2515 | | */ |
2516 | 2.05k | ix = pos; |
2517 | 19.6k | for (; ix > 0; ix--) { |
2518 | 17.8k | if (rptr[ix - 1] == '/') |
2519 | 265 | break; |
2520 | 17.8k | } |
2521 | 2.05k | uptr = (xmlChar *)&rptr[ix]; |
2522 | | |
2523 | | /* |
2524 | | * In base, count the number of '/' from the differing point |
2525 | | */ |
2526 | 189k | for (; bptr[ix] != 0; ix++) { |
2527 | 187k | if (bptr[ix] == '/') |
2528 | 123k | nbslash++; |
2529 | 187k | } |
2530 | | |
2531 | | /* |
2532 | | * e.g: URI="foo/" base="foo/bar" -> "./" |
2533 | | */ |
2534 | 2.05k | if (nbslash == 0 && !uptr[0]) { |
2535 | 27 | val = xmlStrdup(BAD_CAST "./"); |
2536 | 27 | if (val == NULL) |
2537 | 1 | ret = -1; |
2538 | 27 | goto done; |
2539 | 27 | } |
2540 | | |
2541 | 2.03k | len = xmlStrlen (uptr) + 1; |
2542 | 2.03k | } |
2543 | | |
2544 | 2.03k | if (nbslash == 0) { |
2545 | 1.06k | if (uptr != NULL) { |
2546 | | /* exception characters from xmlSaveUri */ |
2547 | 1.06k | val = xmlURIEscapeStr(uptr, BAD_CAST "/;&=+$,"); |
2548 | 1.06k | if (val == NULL) |
2549 | 3 | ret = -1; |
2550 | 1.06k | } |
2551 | 1.06k | goto done; |
2552 | 1.06k | } |
2553 | | |
2554 | | /* |
2555 | | * Allocate just enough space for the returned string - |
2556 | | * length of the remainder of the URI, plus enough space |
2557 | | * for the "../" groups, plus one for the terminator |
2558 | | */ |
2559 | 971 | val = (xmlChar *) xmlMalloc (len + 3 * nbslash); |
2560 | 971 | if (val == NULL) { |
2561 | 8 | ret = -1; |
2562 | 8 | goto done; |
2563 | 8 | } |
2564 | 963 | vptr = val; |
2565 | | /* |
2566 | | * Put in as many "../" as needed |
2567 | | */ |
2568 | 124k | for (; nbslash>0; nbslash--) { |
2569 | 123k | *vptr++ = '.'; |
2570 | 123k | *vptr++ = '.'; |
2571 | 123k | *vptr++ = '/'; |
2572 | 123k | } |
2573 | | /* |
2574 | | * Finish up with the end of the URI |
2575 | | */ |
2576 | 963 | if (uptr != NULL) { |
2577 | 963 | if ((vptr > val) && (len > 0) && |
2578 | 963 | (uptr[0] == '/') && (vptr[-1] == '/')) { |
2579 | 71 | memcpy (vptr, uptr + 1, len - 1); |
2580 | 71 | vptr[len - 2] = 0; |
2581 | 892 | } else { |
2582 | 892 | memcpy (vptr, uptr, len); |
2583 | 892 | vptr[len - 1] = 0; |
2584 | 892 | } |
2585 | 963 | } else { |
2586 | 0 | vptr[len - 1] = 0; |
2587 | 0 | } |
2588 | | |
2589 | | /* escape the freshly-built path */ |
2590 | 963 | vptr = val; |
2591 | | /* exception characters from xmlSaveUri */ |
2592 | 963 | val = xmlURIEscapeStr(vptr, BAD_CAST "/;&=+$,"); |
2593 | 963 | if (val == NULL) |
2594 | 2 | ret = -1; |
2595 | 961 | else |
2596 | 961 | ret = 0; |
2597 | 963 | xmlFree(vptr); |
2598 | | |
2599 | 5.81k | done: |
2600 | | /* |
2601 | | * Free the working variables |
2602 | | */ |
2603 | 5.81k | if (remove_path != 0) |
2604 | 171 | ref->path = NULL; |
2605 | 5.81k | if (ref != NULL) |
2606 | 5.80k | xmlFreeURI (ref); |
2607 | 5.81k | if (bas != NULL) |
2608 | 4.11k | xmlFreeURI (bas); |
2609 | 5.81k | if (ret != 0) { |
2610 | 1.21k | xmlFree(val); |
2611 | 1.21k | val = NULL; |
2612 | 1.21k | } |
2613 | | |
2614 | 5.81k | *valPtr = val; |
2615 | 5.81k | return(ret); |
2616 | 963 | } |
2617 | | |
2618 | | /* |
2619 | | * xmlBuildRelativeURI: |
2620 | | * @URI: the URI reference under consideration |
2621 | | * @base: the base value |
2622 | | * |
2623 | | * See xmlBuildRelativeURISafe. |
2624 | | * |
2625 | | * Returns a new URI string (to be freed by the caller) or NULL in case |
2626 | | * error. |
2627 | | */ |
2628 | | xmlChar * |
2629 | | xmlBuildRelativeURI(const xmlChar * URI, const xmlChar * base) |
2630 | 4.07k | { |
2631 | 4.07k | xmlChar *val; |
2632 | | |
2633 | 4.07k | xmlBuildRelativeURISafe(URI, base, &val); |
2634 | 4.07k | return(val); |
2635 | 4.07k | } |
2636 | | |
2637 | | /** |
2638 | | * xmlCanonicPath: |
2639 | | * @path: the resource locator in a filesystem notation |
2640 | | * |
2641 | | * Prepares a path. |
2642 | | * |
2643 | | * If the path contains the substring "://", it is considered a |
2644 | | * Legacy Extended IRI. Characters which aren't allowed in URIs are |
2645 | | * escaped. |
2646 | | * |
2647 | | * Otherwise, the path is considered a filesystem path which is |
2648 | | * copied without modification. |
2649 | | * |
2650 | | * The caller is responsible for freeing the memory occupied |
2651 | | * by the returned string. If there is insufficient memory available, or the |
2652 | | * argument is NULL, the function returns NULL. |
2653 | | */ |
2654 | | xmlChar * |
2655 | | xmlCanonicPath(const xmlChar *path) |
2656 | 1.96M | { |
2657 | 1.96M | xmlChar *ret; |
2658 | | |
2659 | 1.96M | if (path == NULL) |
2660 | 11 | return(NULL); |
2661 | | |
2662 | | /* Check if this is an "absolute uri" */ |
2663 | 1.96M | if (xmlStrstr(path, BAD_CAST "://") != NULL) { |
2664 | | /* |
2665 | | * Escape all characters except reserved, unreserved and the |
2666 | | * percent sign. |
2667 | | * |
2668 | | * xmlURIEscapeStr already keeps unreserved characters, so we |
2669 | | * pass gen-delims, sub-delims and "%" to ignore. |
2670 | | */ |
2671 | 18.4k | ret = xmlURIEscapeStr(path, BAD_CAST ":/?#[]@!$&()*+,;='%"); |
2672 | 1.95M | } else { |
2673 | 1.95M | ret = xmlStrdup((const xmlChar *) path); |
2674 | 1.95M | } |
2675 | | |
2676 | 1.96M | return(ret); |
2677 | 1.96M | } |
2678 | | |
2679 | | /** |
2680 | | * xmlPathToURI: |
2681 | | * @path: the resource locator in a filesystem notation |
2682 | | * |
2683 | | * Constructs an URI expressing the existing path |
2684 | | * |
2685 | | * Returns a new URI, or a duplicate of the path parameter if the |
2686 | | * construction fails. The caller is responsible for freeing the memory |
2687 | | * occupied by the returned string. If there is insufficient memory available, |
2688 | | * or the argument is NULL, the function returns NULL. |
2689 | | */ |
2690 | | xmlChar * |
2691 | | xmlPathToURI(const xmlChar *path) |
2692 | 153k | { |
2693 | 153k | return(xmlCanonicPath(path)); |
2694 | 153k | } |