Coverage Report

Created: 2026-04-28 06:03

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