Coverage Report

Created: 2026-11-11 06:09

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
122k
#define MAX_URI_LENGTH 1024 * 1024
35
36
2.41M
#define PORT_EMPTY           0
37
20.7k
#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
283M
#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
283M
#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
116M
#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
56.7M
#define IS_DIGIT(x) (((x) >= '0') && ((x) <= '9'))
69
70
/*
71
 * alphanum = alpha | digit
72
 */
73
283M
#define IS_ALPHANUM(x) (IS_ALPHA(x) || IS_DIGIT(x))
74
75
/*
76
 * mark = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")"
77
 */
78
79
51.5M
#define IS_MARK(x) (((x) == '-') || ((x) == '_') || ((x) == '.') ||     \
80
51.5M
    ((x) == '!') || ((x) == '~') || ((x) == '*') || ((x) == '\'') ||    \
81
51.5M
    ((x) == '(') || ((x) == ')'))
82
83
/*
84
 * unwise = "{" | "}" | "|" | "\" | "^" | "`"
85
 */
86
#define IS_UNWISE(p)                                                    \
87
106k
      (((*(p) == '{')) || ((*(p) == '}')) || ((*(p) == '|')) ||         \
88
106k
       ((*(p) == '\\')) || ((*(p) == '^')) || ((*(p) == '[')) ||        \
89
106k
       ((*(p) == ']')) || ((*(p) == '`')))
90
91
/*
92
 * reserved = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" | "$" | "," |
93
 *            "[" | "]"
94
 */
95
3.16M
#define IS_RESERVED(x) (((x) == ';') || ((x) == '/') || ((x) == '?') || \
96
3.16M
        ((x) == ':') || ((x) == '@') || ((x) == '&') || ((x) == '=') || \
97
3.16M
        ((x) == '+') || ((x) == '$') || ((x) == ',') || ((x) == '[') || \
98
3.16M
        ((x) == ']'))
99
100
/*
101
 * unreserved = alphanum | mark
102
 */
103
141M
#define IS_UNRESERVED(x) (IS_ALPHANUM(x) || IS_MARK(x))
104
105
/*
106
 * Skip to next pointer char, handle escaped sequences
107
 */
108
125M
#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
809k
#define STRNDUP(s, n) (char *) xmlStrndup((const xmlChar *)(s), (n))
120
121
/************************************************************************
122
 *                  *
123
 *                         RFC 3986 parser        *
124
 *                  *
125
 ************************************************************************/
126
127
89.3M
#define ISA_DIGIT(p) ((*(p) >= '0') && (*(p) <= '9'))
128
138M
#define ISA_ALPHA(p) (((*(p) >= 'a') && (*(p) <= 'z')) ||   \
129
138M
                      ((*(p) >= 'A') && (*(p) <= 'Z')))
130
#define ISA_HEXDIG(p)             \
131
19.0M
       (ISA_DIGIT(p) || ((*(p) >= 'a') && (*(p) <= 'f')) ||   \
132
19.0M
        ((*(p) >= 'A') && (*(p) <= 'F')))
133
134
/*
135
 *    sub-delims    = "!" / "$" / "&" / "'" / "(" / ")"
136
 *                     / "*" / "+" / "," / ";" / "="
137
 */
138
#define ISA_SUB_DELIM(p)            \
139
144M
      (((*(p) == '!')) || ((*(p) == '$')) || ((*(p) == '&')) ||   \
140
27.4M
       ((*(p) == '(')) || ((*(p) == ')')) || ((*(p) == '*')) ||   \
141
27.4M
       ((*(p) == '+')) || ((*(p) == ',')) || ((*(p) == ';')) ||   \
142
27.4M
       ((*(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
129M
      ((ISA_ALPHA(p)) || (ISA_DIGIT(p)) || ((*(p) == '-')) ||   \
162
129M
       ((*(p) == '.')) || ((*(p) == '_')) || ((*(p) == '~')))
163
164
/*
165
 *    pct-encoded   = "%" HEXDIG HEXDIG
166
 */
167
#define ISA_PCT_ENCODED(p)            \
168
165M
     ((*(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
173M
     (ISA_UNRESERVED(u, p) || ISA_PCT_ENCODED(p) || ISA_SUB_DELIM(p) ||  \
175
113M
      ((*(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
258M
#define ISA_UNRESERVED(u, p) (xmlIsUnreserved(u, p))
190
191
36.7M
#define XML_URI_ALLOW_UNWISE    1
192
2.17M
#define XML_URI_NO_UNESCAPE     2
193
36.6M
#define XML_URI_ALLOW_UCSCHAR   4
194
195
static int
196
129M
xmlIsUnreserved(xmlURIPtr uri, const char *cur) {
197
129M
    if (uri == NULL)
198
0
        return(0);
199
200
129M
    if (ISA_STRICTLY_UNRESERVED(cur))
201
92.2M
        return(1);
202
203
36.7M
    if (uri->cleanup & XML_URI_ALLOW_UNWISE) {
204
106k
        if (IS_UNWISE(cur))
205
3.77k
            return(1);
206
36.6M
    } else if (uri->cleanup & XML_URI_ALLOW_UCSCHAR) {
207
0
        if (ISA_UCSCHAR(cur))
208
0
            return(1);
209
0
    }
210
211
36.7M
    return(0);
212
36.7M
}
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
1.98M
xmlParse3986Scheme(xmlURIPtr uri, const char **str) {
225
1.98M
    const char *cur;
226
227
1.98M
    cur = *str;
228
1.98M
    if (!ISA_ALPHA(cur))
229
1.25M
  return(1);
230
728k
    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
3.61M
    while (ISA_ALPHA(cur) || ISA_DIGIT(cur) ||
241
2.88M
           (*cur == '+') || (*cur == '-') || (*cur == '.')) cur++;
242
728k
    if (uri != NULL) {
243
728k
  if (uri->scheme != NULL) xmlFree(uri->scheme);
244
728k
  uri->scheme = STRNDUP(*str, cur - *str);
245
728k
        if (uri->scheme == NULL)
246
189
            return(-1);
247
728k
    }
248
728k
    *str = cur;
249
728k
    return(0);
250
728k
}
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
944k
{
268
944k
    const char *cur;
269
270
944k
    cur = *str;
271
272
15.0M
    while ((ISA_PCHAR(uri, cur)) || (*cur == '/') || (*cur == '?') ||
273
1.10M
           (*cur == '[') || (*cur == ']'))
274
14.1M
        NEXT(cur);
275
944k
    if (uri != NULL) {
276
944k
        if (uri->fragment != NULL)
277
0
            xmlFree(uri->fragment);
278
944k
  if (uri->cleanup & XML_URI_NO_UNESCAPE)
279
630
      uri->fragment = STRNDUP(*str, cur - *str);
280
944k
  else
281
944k
      uri->fragment = xmlURIUnescapeString(*str, cur - *str, NULL);
282
944k
        if (uri->fragment == NULL)
283
121
            return (-1);
284
944k
    }
285
944k
    *str = cur;
286
944k
    return (0);
287
944k
}
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
76.9k
{
301
76.9k
    const char *cur;
302
303
76.9k
    cur = *str;
304
305
35.4M
    while ((ISA_PCHAR(uri, cur)) || (*cur == '/') || (*cur == '?'))
306
35.3M
        NEXT(cur);
307
76.9k
    if (uri != NULL) {
308
76.9k
        if (uri->query != NULL)
309
0
            xmlFree(uri->query);
310
76.9k
  if (uri->cleanup & XML_URI_NO_UNESCAPE)
311
498
      uri->query = STRNDUP(*str, cur - *str);
312
76.4k
  else
313
76.4k
      uri->query = xmlURIUnescapeString(*str, cur - *str, NULL);
314
76.9k
        if (uri->query == NULL)
315
78
            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
76.8k
  if (uri->query_raw != NULL)
321
0
      xmlFree (uri->query_raw);
322
76.8k
  uri->query_raw = STRNDUP (*str, cur - *str);
323
76.8k
        if (uri->query_raw == NULL)
324
100
            return (-1);
325
76.8k
    }
326
76.7k
    *str = cur;
327
76.7k
    return (0);
328
76.9k
}
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
25.6k
{
343
25.6k
    const char *cur = *str;
344
25.6k
    int port = 0;
345
346
25.6k
    if (ISA_DIGIT(cur)) {
347
123k
  while (ISA_DIGIT(cur)) {
348
113k
            int digit = *cur - '0';
349
350
113k
            if (port > INT_MAX / 10)
351
6.31k
                return(1);
352
106k
            port *= 10;
353
106k
            if (port > INT_MAX - digit)
354
1.47k
                return(1);
355
105k
      port += digit;
356
357
105k
      cur++;
358
105k
  }
359
10.4k
  if (uri != NULL)
360
10.4k
      uri->port = port;
361
10.4k
  *str = cur;
362
10.4k
  return(0);
363
18.2k
    }
364
7.39k
    return(1);
365
25.6k
}
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
241k
{
380
241k
    const char *cur;
381
382
241k
    cur = *str;
383
4.15M
    while (ISA_UNRESERVED(uri, cur) || ISA_PCT_ENCODED(cur) ||
384
596k
           ISA_SUB_DELIM(cur) || (*cur == ':'))
385
3.91M
  NEXT(cur);
386
241k
    if (*cur == '@') {
387
44.6k
  if (uri != NULL) {
388
44.6k
      if (uri->user != NULL) xmlFree(uri->user);
389
44.6k
      if (uri->cleanup & XML_URI_NO_UNESCAPE)
390
521
    uri->user = STRNDUP(*str, cur - *str);
391
44.1k
      else
392
44.1k
    uri->user = xmlURIUnescapeString(*str, cur - *str, NULL);
393
44.6k
            if (uri->user == NULL)
394
46
                return(-1);
395
44.6k
  }
396
44.6k
  *str = cur;
397
44.6k
  return(0);
398
44.6k
    }
399
196k
    return(1);
400
241k
}
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
85.6k
xmlParse3986DecOctet(const char **str) {
416
85.6k
    const char *cur = *str;
417
418
85.6k
    if (!(ISA_DIGIT(cur)))
419
9.68k
        return(1);
420
75.9k
    if (!ISA_DIGIT(cur+1))
421
42.8k
  cur++;
422
33.1k
    else if ((*cur != '0') && (ISA_DIGIT(cur + 1)) && (!ISA_DIGIT(cur+2)))
423
9.48k
  cur += 2;
424
23.6k
    else if ((*cur == '1') && (ISA_DIGIT(cur + 1)) && (ISA_DIGIT(cur + 2)))
425
5.87k
  cur += 3;
426
17.7k
    else if ((*cur == '2') && (*(cur + 1) >= '0') &&
427
10.1k
       (*(cur + 1) <= '4') && (ISA_DIGIT(cur + 2)))
428
3.44k
  cur += 3;
429
14.3k
    else if ((*cur == '2') && (*(cur + 1) == '5') &&
430
3.82k
       (*(cur + 2) >= '0') && (*(cur + 1) <= '5'))
431
3.82k
  cur += 3;
432
10.5k
    else
433
10.5k
        return(1);
434
65.4k
    *str = cur;
435
65.4k
    return(0);
436
75.9k
}
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
241k
{
453
241k
    const char *cur = *str;
454
241k
    const char *host;
455
456
241k
    host = cur;
457
    /*
458
     * IPv6 and future addressing scheme are enclosed between brackets
459
     */
460
241k
    if (*cur == '[') {
461
10.7k
        cur++;
462
169M
  while ((*cur != ']') && (*cur != 0))
463
169M
      cur++;
464
10.7k
  if (*cur != ']')
465
5.65k
      return(1);
466
5.11k
  cur++;
467
5.11k
  goto found;
468
10.7k
    }
469
    /*
470
     * try to parse an IPv4
471
     */
472
230k
    if (ISA_DIGIT(cur)) {
473
52.1k
        if (xmlParse3986DecOctet(&cur) != 0)
474
5.84k
      goto not_ipv4;
475
46.2k
  if (*cur != '.')
476
17.0k
      goto not_ipv4;
477
29.2k
  cur++;
478
29.2k
        if (xmlParse3986DecOctet(&cur) != 0)
479
10.0k
      goto not_ipv4;
480
19.2k
  if (*cur != '.')
481
14.8k
      goto not_ipv4;
482
4.33k
        if (xmlParse3986DecOctet(&cur) != 0)
483
4.33k
      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
52.1k
not_ipv4:
490
52.1k
        cur = *str;
491
52.1k
    }
492
    /*
493
     * then this should be a hostname which can be empty
494
     */
495
11.7M
    while (ISA_UNRESERVED(uri, cur) ||
496
396k
           ISA_PCT_ENCODED(cur) || ISA_SUB_DELIM(cur))
497
11.5M
        NEXT(cur);
498
235k
found:
499
235k
    if (uri != NULL) {
500
235k
  if (uri->authority != NULL) xmlFree(uri->authority);
501
235k
  uri->authority = NULL;
502
235k
  if (uri->server != NULL) xmlFree(uri->server);
503
235k
  if (cur != host) {
504
191k
      if (uri->cleanup & XML_URI_NO_UNESCAPE)
505
759
    uri->server = STRNDUP(host, cur - host);
506
190k
      else
507
190k
    uri->server = xmlURIUnescapeString(host, cur - host, NULL);
508
191k
            if (uri->server == NULL)
509
116
                return(-1);
510
191k
  } else
511
43.8k
      uri->server = NULL;
512
235k
    }
513
235k
    *str = cur;
514
235k
    return(0);
515
235k
}
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
241k
{
530
241k
    const char *cur;
531
241k
    int ret;
532
533
241k
    cur = *str;
534
    /*
535
     * try to parse an userinfo and check for the trailing @
536
     */
537
241k
    ret = xmlParse3986Userinfo(uri, &cur);
538
241k
    if (ret < 0)
539
46
        return(ret);
540
241k
    if ((ret != 0) || (*cur != '@'))
541
196k
        cur = *str;
542
44.6k
    else
543
44.6k
        cur++;
544
241k
    ret = xmlParse3986Host(uri, &cur);
545
241k
    if (ret != 0) return(ret);
546
235k
    if (*cur == ':') {
547
25.6k
        cur++;
548
25.6k
        ret = xmlParse3986Port(uri, &cur);
549
25.6k
  if (ret != 0) return(ret);
550
25.6k
    }
551
220k
    *str = cur;
552
220k
    return(0);
553
235k
}
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
4.43M
{
573
4.43M
    const char *cur;
574
575
4.43M
    cur = *str;
576
4.43M
    if (!ISA_PCHAR(uri, cur) || (*cur == forbid)) {
577
303k
        if (empty)
578
259k
      return(0);
579
44.5k
  return(1);
580
303k
    }
581
4.12M
    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
56.2M
    while (ISA_PCHAR(uri, cur) && (*cur != forbid))
592
52.1M
        NEXT(cur);
593
4.12M
    *str = cur;
594
4.12M
    return (0);
595
4.43M
}
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
220k
{
610
220k
    const char *cur;
611
220k
    int ret;
612
613
220k
    cur = *str;
614
615
472k
    while (*cur == '/') {
616
252k
        cur++;
617
252k
  ret = xmlParse3986Segment(uri, &cur, 0, 1);
618
252k
  if (ret != 0) return(ret);
619
252k
    }
620
220k
    if (uri != NULL) {
621
220k
  if (uri->path != NULL) xmlFree(uri->path);
622
220k
        if (*str != cur) {
623
92.5k
            if (uri->cleanup & XML_URI_NO_UNESCAPE)
624
142
                uri->path = STRNDUP(*str, cur - *str);
625
92.3k
            else
626
92.3k
                uri->path = xmlURIUnescapeString(*str, cur - *str, NULL);
627
92.5k
            if (uri->path == NULL)
628
51
                return (-1);
629
127k
        } else {
630
127k
            uri->path = NULL;
631
127k
        }
632
220k
    }
633
220k
    *str = cur;
634
220k
    return (0);
635
220k
}
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
37.2k
{
650
37.2k
    const char *cur;
651
37.2k
    int ret;
652
653
37.2k
    cur = *str;
654
655
37.2k
    if (*cur != '/')
656
0
        return(1);
657
37.2k
    cur++;
658
37.2k
    ret = xmlParse3986Segment(uri, &cur, 0, 0);
659
37.2k
    if (ret == 0) {
660
59.8k
  while (*cur == '/') {
661
41.5k
      cur++;
662
41.5k
      ret = xmlParse3986Segment(uri, &cur, 0, 1);
663
41.5k
      if (ret != 0) return(ret);
664
41.5k
  }
665
18.3k
    }
666
37.2k
    if (uri != NULL) {
667
37.2k
  if (uri->path != NULL) xmlFree(uri->path);
668
37.2k
        if (cur != *str) {
669
37.2k
            if (uri->cleanup & XML_URI_NO_UNESCAPE)
670
266
                uri->path = STRNDUP(*str, cur - *str);
671
37.0k
            else
672
37.0k
                uri->path = xmlURIUnescapeString(*str, cur - *str, NULL);
673
37.2k
            if (uri->path == NULL)
674
60
                return (-1);
675
37.2k
        } else {
676
0
            uri->path = NULL;
677
0
        }
678
37.2k
    }
679
37.2k
    *str = cur;
680
37.2k
    return (0);
681
37.2k
}
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
94.9k
{
696
94.9k
    const char *cur;
697
94.9k
    int ret;
698
699
94.9k
    cur = *str;
700
701
94.9k
    ret = xmlParse3986Segment(uri, &cur, 0, 0);
702
94.9k
    if (ret != 0) return(ret);
703
149k
    while (*cur == '/') {
704
54.5k
        cur++;
705
54.5k
  ret = xmlParse3986Segment(uri, &cur, 0, 1);
706
54.5k
  if (ret != 0) return(ret);
707
54.5k
    }
708
94.9k
    if (uri != NULL) {
709
94.9k
  if (uri->path != NULL) xmlFree(uri->path);
710
94.9k
        if (cur != *str) {
711
94.9k
            if (uri->cleanup & XML_URI_NO_UNESCAPE)
712
82
                uri->path = STRNDUP(*str, cur - *str);
713
94.8k
            else
714
94.8k
                uri->path = xmlURIUnescapeString(*str, cur - *str, NULL);
715
94.9k
            if (uri->path == NULL)
716
53
                return (-1);
717
94.9k
        } else {
718
0
            uri->path = NULL;
719
0
        }
720
94.9k
    }
721
94.9k
    *str = cur;
722
94.9k
    return (0);
723
94.9k
}
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
713k
{
738
713k
    const char *cur;
739
713k
    int ret;
740
741
713k
    cur = *str;
742
743
713k
    ret = xmlParse3986Segment(uri, &cur, ':', 0);
744
713k
    if (ret != 0) return(ret);
745
3.92M
    while (*cur == '/') {
746
3.23M
        cur++;
747
3.23M
  ret = xmlParse3986Segment(uri, &cur, 0, 1);
748
3.23M
  if (ret != 0) return(ret);
749
3.23M
    }
750
687k
    if (uri != NULL) {
751
687k
  if (uri->path != NULL) xmlFree(uri->path);
752
687k
        if (cur != *str) {
753
687k
            if (uri->cleanup & XML_URI_NO_UNESCAPE)
754
963
                uri->path = STRNDUP(*str, cur - *str);
755
687k
            else
756
687k
                uri->path = xmlURIUnescapeString(*str, cur - *str, NULL);
757
687k
            if (uri->path == NULL)
758
285
                return (-1);
759
687k
        } else {
760
0
            uri->path = NULL;
761
0
        }
762
687k
    }
763
687k
    *str = cur;
764
687k
    return (0);
765
687k
}
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
382k
{
783
382k
    const char *cur;
784
382k
    int ret;
785
786
382k
    cur = *str;
787
788
382k
    if ((*cur == '/') && (*(cur + 1) == '/')) {
789
172k
        cur += 2;
790
172k
  ret = xmlParse3986Authority(uri, &cur);
791
172k
  if (ret != 0) return(ret);
792
        /*
793
         * An empty server is marked with a special URI value.
794
         */
795
161k
  if ((uri->server == NULL) && (uri->port == PORT_EMPTY))
796
20.7k
      uri->port = PORT_EMPTY_SERVER;
797
161k
  ret = xmlParse3986PathAbEmpty(uri, &cur);
798
161k
  if (ret != 0) return(ret);
799
161k
  *str = cur;
800
161k
  return(0);
801
210k
    } else if (*cur == '/') {
802
11.4k
        ret = xmlParse3986PathAbsolute(uri, &cur);
803
11.4k
  if (ret != 0) return(ret);
804
198k
    } else if (ISA_PCHAR(uri, cur)) {
805
94.9k
        ret = xmlParse3986PathRootless(uri, &cur);
806
94.9k
  if (ret != 0) return(ret);
807
103k
    } else {
808
  /* path-empty is effectively empty */
809
103k
  if (uri != NULL) {
810
103k
      if (uri->path != NULL) xmlFree(uri->path);
811
103k
      uri->path = NULL;
812
103k
  }
813
103k
    }
814
210k
    *str = cur;
815
210k
    return (0);
816
382k
}
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
1.77M
xmlParse3986RelativeRef(xmlURIPtr uri, const char *str) {
834
1.77M
    int ret;
835
836
1.77M
    if ((*str == '/') && (*(str + 1) == '/')) {
837
69.0k
        str += 2;
838
69.0k
  ret = xmlParse3986Authority(uri, &str);
839
69.0k
  if (ret != 0) return(ret);
840
58.3k
  ret = xmlParse3986PathAbEmpty(uri, &str);
841
58.3k
  if (ret != 0) return(ret);
842
1.70M
    } else if (*str == '/') {
843
25.7k
  ret = xmlParse3986PathAbsolute(uri, &str);
844
25.7k
  if (ret != 0) return(ret);
845
1.68M
    } else if (ISA_PCHAR(uri, str)) {
846
713k
        ret = xmlParse3986PathNoScheme(uri, &str);
847
713k
  if (ret != 0) return(ret);
848
969k
    } else {
849
  /* path-empty is effectively empty */
850
969k
  if (uri != NULL) {
851
969k
      if (uri->path != NULL) xmlFree(uri->path);
852
969k
      uri->path = NULL;
853
969k
  }
854
969k
    }
855
856
1.74M
    if (*str == '?') {
857
58.1k
  str++;
858
58.1k
  ret = xmlParse3986Query(uri, &str);
859
58.1k
  if (ret != 0) return(ret);
860
58.1k
    }
861
1.74M
    if (*str == '#') {
862
923k
  str++;
863
923k
  ret = xmlParse3986Fragment(uri, &str);
864
923k
  if (ret != 0) return(ret);
865
923k
    }
866
1.74M
    if (*str != 0) {
867
364k
  xmlCleanURI(uri);
868
364k
  return(1);
869
364k
    }
870
1.37M
    return(0);
871
1.74M
}
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
1.98M
xmlParse3986URI(xmlURIPtr uri, const char *str) {
886
1.98M
    int ret;
887
888
1.98M
    ret = xmlParse3986Scheme(uri, &str);
889
1.98M
    if (ret != 0) return(ret);
890
728k
    if (*str != ':') {
891
346k
  return(1);
892
346k
    }
893
382k
    str++;
894
382k
    ret = xmlParse3986HierPart(uri, &str);
895
382k
    if (ret != 0) return(ret);
896
372k
    if (*str == '?') {
897
18.7k
  str++;
898
18.7k
  ret = xmlParse3986Query(uri, &str);
899
18.7k
  if (ret != 0) return(ret);
900
18.7k
    }
901
372k
    if (*str == '#') {
902
21.2k
  str++;
903
21.2k
  ret = xmlParse3986Fragment(uri, &str);
904
21.2k
  if (ret != 0) return(ret);
905
21.2k
    }
906
371k
    if (*str != 0) {
907
163k
  xmlCleanURI(uri);
908
163k
  return(1);
909
163k
    }
910
208k
    return(0);
911
371k
}
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
1.98M
xmlParse3986URIReference(xmlURIPtr uri, const char *str) {
925
1.98M
    int ret;
926
927
1.98M
    if (str == NULL)
928
0
  return(-1);
929
1.98M
    xmlCleanURI(uri);
930
931
    /*
932
     * Try first to parse absolute refs, then fallback to relative if
933
     * it fails.
934
     */
935
1.98M
    ret = xmlParse3986URI(uri, str);
936
1.98M
    if (ret < 0)
937
413
        return(ret);
938
1.98M
    if (ret != 0) {
939
1.77M
  xmlCleanURI(uri);
940
1.77M
        ret = xmlParse3986RelativeRef(uri, str);
941
1.77M
  if (ret != 0) {
942
401k
      xmlCleanURI(uri);
943
401k
      return(ret);
944
401k
  }
945
1.77M
    }
946
1.58M
    return(0);
947
1.98M
}
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
1.96M
xmlParseURISafe(const char *str, xmlURI **uriOut) {
963
1.96M
    xmlURIPtr uri;
964
1.96M
    int ret;
965
966
1.96M
    if (uriOut == NULL)
967
0
        return(1);
968
1.96M
    *uriOut = NULL;
969
1.96M
    if (str == NULL)
970
10
  return(1);
971
972
1.96M
    uri = xmlCreateURI();
973
1.96M
    if (uri == NULL)
974
856
        return(-1);
975
976
1.96M
    ret = xmlParse3986URIReference(uri, str);
977
1.96M
    if (ret) {
978
393k
        xmlFreeURI(uri);
979
393k
        return(ret);
980
393k
    }
981
982
1.56M
    *uriOut = uri;
983
1.56M
    return(0);
984
1.96M
}
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
101k
xmlParseURI(const char *str) {
996
101k
    xmlURIPtr uri;
997
101k
    xmlParseURISafe(str, &uri);
998
101k
    return(uri);
999
101k
}
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
25.7k
xmlParseURIReference(xmlURI *uri, const char *str) {
1013
25.7k
    return(xmlParse3986URIReference(uri, str));
1014
25.7k
}
1015
1016
/**
1017
 * Parse an URI but allows to keep intact the original fragments.
1018
 *
1019
 * URI-reference = URI / relative-ref
1020
 *
1021
 * @param str  the URI string to analyze
1022
 * @param raw  if 1 unescaping of URI pieces are disabled
1023
 * @returns a newly built xmlURI or NULL in case of error
1024
 */
1025
xmlURI *
1026
4.44k
xmlParseURIRaw(const char *str, int raw) {
1027
4.44k
    xmlURIPtr uri;
1028
4.44k
    int ret;
1029
1030
4.44k
    if (str == NULL)
1031
5
  return(NULL);
1032
4.43k
    uri = xmlCreateURI();
1033
4.43k
    if (uri != NULL) {
1034
4.36k
        if (raw) {
1035
4.36k
      uri->cleanup |= XML_URI_NO_UNESCAPE;
1036
4.36k
  }
1037
4.36k
  ret = xmlParseURIReference(uri, str);
1038
4.36k
        if (ret) {
1039
1.56k
      xmlFreeURI(uri);
1040
1.56k
      return(NULL);
1041
1.56k
  }
1042
4.36k
    }
1043
2.87k
    return(uri);
1044
4.43k
}
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
2.13M
xmlCreateURI(void) {
1059
2.13M
    xmlURIPtr ret;
1060
1061
2.13M
    ret = (xmlURIPtr) xmlMalloc(sizeof(xmlURI));
1062
2.13M
    if (ret == NULL)
1063
1.09k
  return(NULL);
1064
2.13M
    memset(ret, 0, sizeof(xmlURI));
1065
2.13M
    ret->port = PORT_EMPTY;
1066
2.13M
    return(ret);
1067
2.13M
}
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
122k
xmlSaveUriRealloc(xmlChar *ret, int *max) {
1075
122k
    xmlChar *temp;
1076
122k
    int newSize;
1077
1078
122k
    newSize = xmlGrowCapacity(*max, 1, 80, MAX_URI_LENGTH);
1079
122k
    if (newSize < 0)
1080
23
        return(NULL);
1081
122k
    temp = xmlRealloc(ret, newSize + 1);
1082
122k
    if (temp == NULL)
1083
245
        return(NULL);
1084
122k
    *max = newSize;
1085
122k
    return(temp);
1086
122k
}
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
209k
xmlSaveUri(xmlURI *uri) {
1096
209k
    xmlChar *ret = NULL;
1097
209k
    xmlChar *temp;
1098
209k
    const char *p;
1099
209k
    int len;
1100
209k
    int max;
1101
1102
209k
    if (uri == NULL) return(NULL);
1103
1104
1105
207k
    max = 80;
1106
207k
    ret = xmlMalloc(max + 1);
1107
207k
    if (ret == NULL)
1108
469
  return(NULL);
1109
207k
    len = 0;
1110
1111
207k
    if (uri->scheme != NULL) {
1112
46.1k
  p = uri->scheme;
1113
835k
  while (*p != 0) {
1114
789k
      if (len >= max) {
1115
1.81k
                temp = xmlSaveUriRealloc(ret, &max);
1116
1.81k
                if (temp == NULL) goto mem_error;
1117
1.80k
    ret = temp;
1118
1.80k
      }
1119
789k
      ret[len++] = *p++;
1120
789k
  }
1121
46.1k
  if (len >= max) {
1122
656
            temp = xmlSaveUriRealloc(ret, &max);
1123
656
            if (temp == NULL) goto mem_error;
1124
649
            ret = temp;
1125
649
  }
1126
46.1k
  ret[len++] = ':';
1127
46.1k
    }
1128
207k
    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
207k
    } else {
1147
207k
  if ((uri->server != NULL) || (uri->port != PORT_EMPTY)) {
1148
47.1k
      if (len + 3 >= max) {
1149
904
                temp = xmlSaveUriRealloc(ret, &max);
1150
904
                if (temp == NULL) goto mem_error;
1151
898
                ret = temp;
1152
898
      }
1153
47.1k
      ret[len++] = '/';
1154
47.1k
      ret[len++] = '/';
1155
47.1k
      if (uri->user != NULL) {
1156
19.6k
    p = uri->user;
1157
57.6M
    while (*p != 0) {
1158
57.6M
        if (len + 3 >= max) {
1159
27.2k
                        temp = xmlSaveUriRealloc(ret, &max);
1160
27.2k
                        if (temp == NULL) goto mem_error;
1161
27.1k
                        ret = temp;
1162
27.1k
        }
1163
57.6M
        if ((IS_UNRESERVED(*(p))) ||
1164
17.7M
      ((*(p) == ';')) || ((*(p) == ':')) ||
1165
17.6M
      ((*(p) == '&')) || ((*(p) == '=')) ||
1166
17.4M
      ((*(p) == '+')) || ((*(p) == '$')) ||
1167
17.4M
      ((*(p) == ',')))
1168
40.3M
      ret[len++] = *p++;
1169
17.3M
        else {
1170
17.3M
      int val = *(unsigned char *)p++;
1171
17.3M
      int hi = val / 0x10, lo = val % 0x10;
1172
17.3M
      ret[len++] = '%';
1173
17.3M
      ret[len++] = hi + (hi > 9? 'A'-10 : '0');
1174
17.3M
      ret[len++] = lo + (lo > 9? 'A'-10 : '0');
1175
17.3M
        }
1176
57.6M
    }
1177
19.5k
    if (len + 3 >= max) {
1178
1.19k
                    temp = xmlSaveUriRealloc(ret, &max);
1179
1.19k
                    if (temp == NULL) goto mem_error;
1180
1.17k
                    ret = temp;
1181
1.17k
    }
1182
19.5k
    ret[len++] = '@';
1183
19.5k
      }
1184
47.0k
      if (uri->server != NULL) {
1185
34.2k
    p = uri->server;
1186
85.9M
    while (*p != 0) {
1187
85.8M
        if (len >= max) {
1188
7.66k
      temp = xmlSaveUriRealloc(ret, &max);
1189
7.66k
      if (temp == NULL) goto mem_error;
1190
7.63k
      ret = temp;
1191
7.63k
        }
1192
                    /* TODO: escaping? */
1193
85.8M
        ret[len++] = (xmlChar) *p++;
1194
85.8M
    }
1195
34.2k
      }
1196
47.0k
            if (uri->port > 0) {
1197
4.23k
                if (len + 10 >= max) {
1198
547
                    temp = xmlSaveUriRealloc(ret, &max);
1199
547
                    if (temp == NULL) goto mem_error;
1200
533
                    ret = temp;
1201
533
                }
1202
4.21k
                len += snprintf((char *) &ret[len], max - len, ":%d", uri->port);
1203
4.21k
            }
1204
160k
  } 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
160k
  } else if (uri->scheme != NULL) {
1233
7.32k
      if (len + 3 >= max) {
1234
878
                temp = xmlSaveUriRealloc(ret, &max);
1235
878
                if (temp == NULL) goto mem_error;
1236
873
                ret = temp;
1237
873
      }
1238
7.32k
  }
1239
207k
  if (uri->path != NULL) {
1240
165k
      p = uri->path;
1241
      /*
1242
       * the colon in file:///d: should not be escaped or
1243
       * Windows accesses fail later.
1244
       */
1245
165k
      if ((uri->scheme != NULL) &&
1246
29.3k
    (p[0] == '/') &&
1247
23.4k
    (((p[1] >= 'a') && (p[1] <= 'z')) ||
1248
17.6k
     ((p[1] >= 'A') && (p[1] <= 'Z'))) &&
1249
8.24k
    (p[2] == ':') &&
1250
1.88k
          (xmlStrEqual(BAD_CAST uri->scheme, BAD_CAST "file"))) {
1251
1.13k
    if (len + 3 >= max) {
1252
452
                    temp = xmlSaveUriRealloc(ret, &max);
1253
452
                    if (temp == NULL) goto mem_error;
1254
449
                    ret = temp;
1255
449
    }
1256
1.13k
    ret[len++] = *p++;
1257
1.13k
    ret[len++] = *p++;
1258
1.13k
    ret[len++] = *p++;
1259
1.13k
      }
1260
55.3M
      while (*p != 0) {
1261
55.2M
    if (len + 3 >= max) {
1262
57.3k
                    temp = xmlSaveUriRealloc(ret, &max);
1263
57.3k
                    if (temp == NULL) goto mem_error;
1264
57.3k
                    ret = temp;
1265
57.3k
    }
1266
55.2M
    if ((IS_UNRESERVED(*(p))) || ((*(p) == '/')) ||
1267
8.14M
                    ((*(p) == ';')) || ((*(p) == '@')) || ((*(p) == '&')) ||
1268
7.72M
              ((*(p) == '=')) || ((*(p) == '+')) || ((*(p) == '$')) ||
1269
7.41M
              ((*(p) == ',')))
1270
47.8M
        ret[len++] = *p++;
1271
7.33M
    else {
1272
7.33M
        int val = *(unsigned char *)p++;
1273
7.33M
        int hi = val / 0x10, lo = val % 0x10;
1274
7.33M
        ret[len++] = '%';
1275
7.33M
        ret[len++] = hi + (hi > 9? 'A'-10 : '0');
1276
7.33M
        ret[len++] = lo + (lo > 9? 'A'-10 : '0');
1277
7.33M
    }
1278
55.2M
      }
1279
165k
  }
1280
207k
  if (uri->query_raw != NULL) {
1281
20.8k
      if (len + 1 >= max) {
1282
628
                temp = xmlSaveUriRealloc(ret, &max);
1283
628
                if (temp == NULL) goto mem_error;
1284
614
                ret = temp;
1285
614
      }
1286
20.8k
      ret[len++] = '?';
1287
20.8k
      p = uri->query_raw;
1288
18.0M
      while (*p != 0) {
1289
18.0M
    if (len + 1 >= max) {
1290
14.0k
                    temp = xmlSaveUriRealloc(ret, &max);
1291
14.0k
                    if (temp == NULL) goto mem_error;
1292
14.0k
                    ret = temp;
1293
14.0k
    }
1294
18.0M
    ret[len++] = *p++;
1295
18.0M
      }
1296
186k
  } 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
207k
    }
1322
207k
    if (uri->fragment != NULL) {
1323
27.7k
  if (len + 3 >= max) {
1324
2.47k
            temp = xmlSaveUriRealloc(ret, &max);
1325
2.47k
            if (temp == NULL) goto mem_error;
1326
2.45k
            ret = temp;
1327
2.45k
  }
1328
27.7k
  ret[len++] = '#';
1329
27.7k
  p = uri->fragment;
1330
3.50M
  while (*p != 0) {
1331
3.47M
      if (len + 3 >= max) {
1332
5.23k
                temp = xmlSaveUriRealloc(ret, &max);
1333
5.23k
                if (temp == NULL) goto mem_error;
1334
5.21k
                ret = temp;
1335
5.21k
      }
1336
3.47M
      if ((IS_UNRESERVED(*(p))) || (IS_RESERVED(*(p))))
1337
3.43M
    ret[len++] = *p++;
1338
44.2k
      else {
1339
44.2k
    int val = *(unsigned char *)p++;
1340
44.2k
    int hi = val / 0x10, lo = val % 0x10;
1341
44.2k
    ret[len++] = '%';
1342
44.2k
    ret[len++] = hi + (hi > 9? 'A'-10 : '0');
1343
44.2k
    ret[len++] = lo + (lo > 9? 'A'-10 : '0');
1344
44.2k
      }
1345
3.47M
  }
1346
27.7k
    }
1347
207k
    if (len >= max) {
1348
1.61k
        temp = xmlSaveUriRealloc(ret, &max);
1349
1.61k
        if (temp == NULL) goto mem_error;
1350
1.60k
        ret = temp;
1351
1.60k
    }
1352
207k
    ret[len] = 0;
1353
207k
    return(ret);
1354
1355
268
mem_error:
1356
268
    xmlFree(ret);
1357
268
    return(NULL);
1358
207k
}
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
4.69M
xmlCleanURI(xmlURIPtr uri) {
1384
4.69M
    if (uri == NULL) return;
1385
1386
4.69M
    if (uri->scheme != NULL) xmlFree(uri->scheme);
1387
4.69M
    uri->scheme = NULL;
1388
4.69M
    if (uri->server != NULL) xmlFree(uri->server);
1389
4.69M
    uri->server = NULL;
1390
4.69M
    if (uri->user != NULL) xmlFree(uri->user);
1391
4.69M
    uri->user = NULL;
1392
4.69M
    if (uri->path != NULL) xmlFree(uri->path);
1393
4.69M
    uri->path = NULL;
1394
4.69M
    if (uri->fragment != NULL) xmlFree(uri->fragment);
1395
4.69M
    uri->fragment = NULL;
1396
4.69M
    if (uri->opaque != NULL) xmlFree(uri->opaque);
1397
4.69M
    uri->opaque = NULL;
1398
4.69M
    if (uri->authority != NULL) xmlFree(uri->authority);
1399
4.69M
    uri->authority = NULL;
1400
4.69M
    if (uri->query != NULL) xmlFree(uri->query);
1401
4.69M
    uri->query = NULL;
1402
4.69M
    if (uri->query_raw != NULL) xmlFree(uri->query_raw);
1403
4.69M
    uri->query_raw = NULL;
1404
4.69M
}
1405
1406
/**
1407
 * Free up the xmlURI struct
1408
 *
1409
 * @param uri  pointer to an xmlURI
1410
 */
1411
void
1412
2.25M
xmlFreeURI(xmlURI *uri) {
1413
2.25M
    if (uri == NULL) return;
1414
1415
2.13M
    if (uri->scheme != NULL) xmlFree(uri->scheme);
1416
2.13M
    if (uri->server != NULL) xmlFree(uri->server);
1417
2.13M
    if (uri->user != NULL) xmlFree(uri->user);
1418
2.13M
    if (uri->path != NULL) xmlFree(uri->path);
1419
2.13M
    if (uri->fragment != NULL) xmlFree(uri->fragment);
1420
2.13M
    if (uri->opaque != NULL) xmlFree(uri->opaque);
1421
2.13M
    if (uri->authority != NULL) xmlFree(uri->authority);
1422
2.13M
    if (uri->query != NULL) xmlFree(uri->query);
1423
2.13M
    if (uri->query_raw != NULL) xmlFree(uri->query_raw);
1424
2.13M
    xmlFree(uri);
1425
2.13M
}
1426
1427
/************************************************************************
1428
 *                  *
1429
 *      Helper functions        *
1430
 *                  *
1431
 ************************************************************************/
1432
1433
static int
1434
56.5M
xmlIsPathSeparator(int c, int isFile) {
1435
56.5M
    (void) isFile;
1436
1437
56.5M
    if (c == '/')
1438
3.68M
        return(1);
1439
1440
#if defined(LIBXML_WINPATH_ENABLED)
1441
    if (isFile && (c == '\\'))
1442
        return(1);
1443
#endif
1444
1445
52.8M
    return(0);
1446
56.5M
}
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
203k
xmlNormalizePath(char *path, int isFile) {
1457
203k
    char *cur, *out;
1458
203k
    int numSeg = 0;
1459
1460
203k
    if (path == NULL)
1461
46.5k
  return(-1);
1462
1463
156k
    cur = path;
1464
156k
    out = path;
1465
1466
156k
    if (*cur == 0)
1467
1.43k
        return(0);
1468
1469
155k
    if (xmlIsPathSeparator(*cur, isFile)) {
1470
24.6k
        cur++;
1471
24.6k
        *out++ = '/';
1472
24.6k
    }
1473
1474
3.71M
    while (*cur != 0) {
1475
        /*
1476
         * At this point, out is either empty or ends with a separator.
1477
         * Collapse multiple separators first.
1478
         */
1479
3.72M
        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
157k
            cur++;
1486
157k
        }
1487
1488
3.56M
        if (*cur == '.') {
1489
151k
            if (cur[1] == 0) {
1490
                /* Ignore "." at end of path */
1491
5.10k
                break;
1492
146k
            } else if (xmlIsPathSeparator(cur[1], isFile)) {
1493
                /* Skip "./" */
1494
83.0k
                cur += 2;
1495
83.0k
                continue;
1496
83.0k
            } else if ((cur[1] == '.') &&
1497
58.9k
                       ((cur[2] == 0) || xmlIsPathSeparator(cur[2], isFile))) {
1498
54.6k
                if (numSeg > 0) {
1499
                    /* Handle ".." by removing last segment */
1500
53.1k
                    do {
1501
53.1k
                        out--;
1502
53.1k
                    } while ((out > path) &&
1503
51.0k
                             !xmlIsPathSeparator(out[-1], isFile));
1504
11.2k
                    numSeg--;
1505
1506
11.2k
                    if (cur[2] == 0)
1507
683
                        break;
1508
10.5k
                    cur += 3;
1509
10.5k
                    continue;
1510
43.3k
                } else if (path[0] == '/') {
1511
                    /* Ignore extraneous ".." in absolute paths */
1512
1.77k
                    if (cur[2] == 0)
1513
615
                        break;
1514
1.15k
                    cur += 3;
1515
1.15k
                    continue;
1516
41.6k
                } else {
1517
                    /* Keep "../" at start of relative path */
1518
41.6k
                    numSeg--;
1519
41.6k
                }
1520
54.6k
            }
1521
151k
        }
1522
1523
        /* Copy segment */
1524
50.0M
        while ((*cur != 0) && !xmlIsPathSeparator(*cur, isFile)) {
1525
46.5M
            *out++ = *cur++;
1526
46.5M
        }
1527
1528
        /* Copy separator */
1529
3.46M
        if (*cur != 0) {
1530
3.32M
            cur++;
1531
3.32M
            *out++ = '/';
1532
3.32M
        }
1533
1534
3.46M
        numSeg++;
1535
3.46M
    }
1536
1537
    /* Keep "." if output is empty and it's a file */
1538
155k
    if ((isFile) && (out <= path))
1539
2.03k
        *out++ = '.';
1540
155k
    *out = 0;
1541
1542
155k
    return(0);
1543
156k
}
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
35.1k
xmlNormalizeURIPath(char *path) {
1556
35.1k
    return(xmlNormalizePath(path, 0));
1557
35.1k
}
1558
1559
24.0M
static int is_hex(char c) {
1560
24.0M
    if (((c >= '0') && (c <= '9')) ||
1561
8.40M
        ((c >= 'a') && (c <= 'f')) ||
1562
8.31M
        ((c >= 'A') && (c <= 'F')))
1563
24.0M
  return(1);
1564
31.6k
    return(0);
1565
24.0M
}
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
3.09M
xmlURIUnescapeString(const char *str, int len, char *target) {
1581
3.09M
    char *ret, *out;
1582
3.09M
    const char *in;
1583
1584
3.09M
    if (str == NULL)
1585
5
  return(NULL);
1586
3.09M
    if (len <= 0) len = strlen(str);
1587
3.09M
    if (len < 0) return(NULL);
1588
1589
3.09M
    if (target == NULL) {
1590
3.09M
  ret = xmlMalloc(len + 1);
1591
3.09M
  if (ret == NULL)
1592
915
      return(NULL);
1593
3.09M
    } else
1594
0
  ret = target;
1595
3.08M
    in = str;
1596
3.08M
    out = ret;
1597
509M
    while(len > 0) {
1598
506M
  if ((len > 2) && (*in == '%') && (is_hex(in[1])) && (is_hex(in[2]))) {
1599
12.0M
            int c = 0;
1600
12.0M
      in++;
1601
12.0M
      if ((*in >= '0') && (*in <= '9'))
1602
8.99M
          c = (*in - '0');
1603
3.02M
      else if ((*in >= 'a') && (*in <= 'f'))
1604
38.0k
          c = (*in - 'a') + 10;
1605
2.98M
      else if ((*in >= 'A') && (*in <= 'F'))
1606
2.98M
          c = (*in - 'A') + 10;
1607
12.0M
      in++;
1608
12.0M
      if ((*in >= '0') && (*in <= '9'))
1609
6.68M
          c = c * 16 + (*in - '0');
1610
5.33M
      else if ((*in >= 'a') && (*in <= 'f'))
1611
45.1k
          c = c * 16 + (*in - 'a') + 10;
1612
5.29M
      else if ((*in >= 'A') && (*in <= 'F'))
1613
5.29M
          c = c * 16 + (*in - 'A') + 10;
1614
12.0M
      in++;
1615
12.0M
      len -= 3;
1616
            /* Explicit sign change */
1617
12.0M
      *out++ = (char) c;
1618
494M
  } else {
1619
494M
      *out++ = *in++;
1620
494M
      len--;
1621
494M
  }
1622
506M
    }
1623
3.08M
    *out = 0;
1624
3.08M
    return(ret);
1625
3.09M
}
1626
1627
/**
1628
 * This routine escapes a string to hex, ignoring unreserved characters
1629
 * a-z, A-Z, 0-9, "-._~", a few sub-delims "!*'()", the gen-delim "@"
1630
 * (why?) and the characters in the exception list.
1631
 *
1632
 * @param str  string to escape
1633
 * @param list  exception list string of chars not to escape
1634
 * @returns a new escaped string or NULL in case of error.
1635
 */
1636
xmlChar *
1637
51.8k
xmlURIEscapeStr(const xmlChar *str, const xmlChar *list) {
1638
51.8k
    xmlChar *ret, ch;
1639
51.8k
    const xmlChar *in;
1640
51.8k
    int len, out;
1641
1642
51.8k
    if (str == NULL)
1643
5
  return(NULL);
1644
51.8k
    if (str[0] == 0)
1645
578
  return(xmlStrdup(str));
1646
51.2k
    len = xmlStrlen(str);
1647
51.2k
    if (len == 0)
1648
0
        return(NULL);
1649
1650
51.2k
    len += 20;
1651
51.2k
    ret = xmlMalloc(len);
1652
51.2k
    if (ret == NULL)
1653
107
  return(NULL);
1654
51.1k
    in = (const xmlChar *) str;
1655
51.1k
    out = 0;
1656
25.4M
    while(*in != 0) {
1657
25.3M
  if (len - out <= 3) {
1658
9.75k
            xmlChar *temp;
1659
9.75k
            int newSize;
1660
1661
9.75k
            newSize = xmlGrowCapacity(len, 1, 1, XML_MAX_ITEMS);
1662
9.75k
            if (newSize < 0) {
1663
0
    xmlFree(ret);
1664
0
                return(NULL);
1665
0
            }
1666
9.75k
            temp = xmlRealloc(ret, newSize);
1667
9.75k
      if (temp == NULL) {
1668
34
    xmlFree(ret);
1669
34
    return(NULL);
1670
34
      }
1671
9.71k
      ret = temp;
1672
9.71k
            len = newSize;
1673
9.71k
  }
1674
1675
25.3M
  ch = *in;
1676
1677
25.3M
  if ((ch != '@') && (!IS_UNRESERVED(ch)) && (!xmlStrchr(list, ch))) {
1678
9.58M
      unsigned char val;
1679
9.58M
      ret[out++] = '%';
1680
9.58M
      val = ch >> 4;
1681
9.58M
      if (val <= 9)
1682
1.96M
    ret[out++] = '0' + val;
1683
7.61M
      else
1684
7.61M
    ret[out++] = 'A' + val - 0xA;
1685
9.58M
      val = ch & 0xF;
1686
9.58M
      if (val <= 9)
1687
1.92M
    ret[out++] = '0' + val;
1688
7.65M
      else
1689
7.65M
    ret[out++] = 'A' + val - 0xA;
1690
9.58M
      in++;
1691
15.7M
  } else {
1692
15.7M
      ret[out++] = *in++;
1693
15.7M
  }
1694
1695
25.3M
    }
1696
51.0k
    ret[out] = 0;
1697
51.0k
    return(ret);
1698
51.1k
}
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
4.44k
{
1715
4.44k
    xmlChar *ret, *segment = NULL;
1716
4.44k
    xmlURIPtr uri;
1717
4.44k
    int ret2;
1718
1719
4.44k
    if (str == NULL)
1720
5
        return (NULL);
1721
1722
4.43k
    uri = xmlCreateURI();
1723
4.43k
    if (uri != NULL) {
1724
  /*
1725
   * Allow escaping errors in the unescaped form
1726
   */
1727
4.39k
        uri->cleanup = XML_URI_ALLOW_UNWISE;
1728
4.39k
        ret2 = xmlParseURIReference(uri, (const char *)str);
1729
4.39k
        if (ret2) {
1730
1.25k
            xmlFreeURI(uri);
1731
1.25k
            return (NULL);
1732
1.25k
        }
1733
4.39k
    }
1734
1735
3.18k
    if (!uri)
1736
45
        return NULL;
1737
1738
3.13k
    ret = NULL;
1739
1740
3.66k
#define NULLCHK(p) if(!p) { \
1741
39
         xmlFreeURI(uri); \
1742
39
         xmlFree(ret); \
1743
39
         return NULL; } \
1744
3.13k
1745
3.13k
    if (uri->scheme) {
1746
720
        segment = xmlURIEscapeStr(BAD_CAST uri->scheme, BAD_CAST "+-.");
1747
720
        NULLCHK(segment)
1748
717
        ret = xmlStrcat(ret, segment);
1749
717
        ret = xmlStrcat(ret, BAD_CAST ":");
1750
717
        xmlFree(segment);
1751
717
    }
1752
1753
3.13k
    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
3.13k
    if (uri->user) {
1763
525
        segment = xmlURIEscapeStr(BAD_CAST uri->user, BAD_CAST ";:&=+$,");
1764
525
        NULLCHK(segment)
1765
517
        ret = xmlStrcat(ret,BAD_CAST "//");
1766
517
        ret = xmlStrcat(ret, segment);
1767
517
        ret = xmlStrcat(ret, BAD_CAST "@");
1768
517
        xmlFree(segment);
1769
517
    }
1770
1771
3.12k
    if (uri->server) {
1772
782
        segment = xmlURIEscapeStr(BAD_CAST uri->server, BAD_CAST "/?;:@");
1773
782
        NULLCHK(segment)
1774
772
        if (uri->user == NULL)
1775
354
            ret = xmlStrcat(ret, BAD_CAST "//");
1776
772
        ret = xmlStrcat(ret, segment);
1777
772
        xmlFree(segment);
1778
772
    }
1779
1780
3.11k
    if (uri->port > 0) {
1781
221
        xmlChar port[11];
1782
1783
221
        snprintf((char *) port, 11, "%d", uri->port);
1784
221
        ret = xmlStrcat(ret, BAD_CAST ":");
1785
221
        ret = xmlStrcat(ret, port);
1786
221
    }
1787
1788
3.11k
    if (uri->path) {
1789
1.12k
        segment =
1790
1.12k
            xmlURIEscapeStr(BAD_CAST uri->path, BAD_CAST ":@&=+$,/?;");
1791
1.12k
        NULLCHK(segment)
1792
1.10k
        ret = xmlStrcat(ret, segment);
1793
1.10k
        xmlFree(segment);
1794
1.10k
    }
1795
1796
3.09k
    if (uri->query_raw) {
1797
369
        ret = xmlStrcat(ret, BAD_CAST "?");
1798
369
        ret = xmlStrcat(ret, BAD_CAST uri->query_raw);
1799
369
    }
1800
2.72k
    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
3.09k
    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
3.09k
    if (uri->fragment) {
1817
512
        segment = xmlURIEscapeStr(BAD_CAST uri->fragment, BAD_CAST "#");
1818
512
        NULLCHK(segment)
1819
510
        ret = xmlStrcat(ret, BAD_CAST "#");
1820
510
        ret = xmlStrcat(ret, segment);
1821
510
        xmlFree(segment);
1822
510
    }
1823
1824
3.09k
    xmlFreeURI(uri);
1825
3.09k
#undef NULLCHK
1826
1827
3.09k
    return (ret);
1828
3.09k
}
1829
1830
/************************************************************************
1831
 *                  *
1832
 *      Public functions        *
1833
 *                  *
1834
 ************************************************************************/
1835
1836
static int
1837
162k
xmlIsAbsolutePath(const xmlChar *path) {
1838
162k
    int c = path[0];
1839
1840
162k
    if (xmlIsPathSeparator(c, 1))
1841
8.47k
        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
154k
    return(0);
1851
162k
}
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
935k
xmlResolvePath(const xmlChar *escRef, const xmlChar *base, xmlChar **out) {
1864
935k
    const xmlChar *fragment;
1865
935k
    xmlChar *tmp = NULL;
1866
935k
    xmlChar *ref = NULL;
1867
935k
    xmlChar *result = NULL;
1868
935k
    int ret = -1;
1869
935k
    int i;
1870
1871
935k
    if (out == NULL)
1872
0
        return(1);
1873
935k
    *out = NULL;
1874
1875
935k
    if ((escRef == NULL) || (escRef[0] == 0)) {
1876
16.6k
        if ((base == NULL) || (base[0] == 0))
1877
3.31k
            return(1);
1878
13.3k
        ref = xmlStrdup(base);
1879
13.3k
        if (ref == NULL)
1880
23
            goto err_memory;
1881
13.3k
        *out = ref;
1882
13.3k
        return(0);
1883
13.3k
    }
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
918k
    fragment = xmlStrchr(escRef, '#');
1891
918k
    if (fragment != NULL) {
1892
838k
        tmp = xmlStrndup(escRef, fragment - escRef);
1893
838k
        if (tmp == NULL)
1894
28
            goto err_memory;
1895
838k
        escRef = tmp;
1896
838k
    }
1897
1898
918k
    ref = (xmlChar *) xmlURIUnescapeString((char *) escRef, -1, NULL);
1899
918k
    if (ref == NULL)
1900
92
        goto err_memory;
1901
1902
918k
    if ((base == NULL) || (base[0] == 0))
1903
847k
        goto done;
1904
1905
70.9k
    if (xmlIsAbsolutePath(ref))
1906
6.13k
        goto done;
1907
1908
    /*
1909
     * Remove last segment from base
1910
     */
1911
64.8k
    i = xmlStrlen(base);
1912
2.41M
    while ((i > 0) && !xmlIsPathSeparator(base[i-1], 1))
1913
2.34M
        i--;
1914
1915
    /*
1916
     * Concatenate base and ref
1917
     */
1918
64.8k
    if (i > 0) {
1919
24.0k
        int refLen = xmlStrlen(ref);
1920
1921
24.0k
        result = xmlMalloc(i + refLen + 1);
1922
24.0k
        if (result == NULL)
1923
72
            goto err_memory;
1924
1925
23.9k
        memcpy(result, base, i);
1926
23.9k
        memcpy(result + i, ref, refLen + 1);
1927
23.9k
    }
1928
1929
    /*
1930
     * Normalize
1931
     */
1932
64.7k
    xmlNormalizePath((char *) result, 1);
1933
1934
918k
done:
1935
918k
    if (result == NULL) {
1936
894k
        result = ref;
1937
894k
        ref = NULL;
1938
894k
    }
1939
1940
918k
    if (fragment != NULL) {
1941
838k
        result = xmlStrcat(result, fragment);
1942
838k
        if (result == NULL)
1943
43
            goto err_memory;
1944
838k
    }
1945
1946
918k
    *out = result;
1947
918k
    ret = 0;
1948
1949
918k
err_memory:
1950
918k
    xmlFree(tmp);
1951
918k
    xmlFree(ref);
1952
918k
    return(ret);
1953
918k
}
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
1.11M
xmlBuildURISafe(const xmlChar *URI, const xmlChar *base, xmlChar **valPtr) {
1973
1.11M
    xmlChar *val = NULL;
1974
1.11M
    int ret, len, indx, cur, out;
1975
1.11M
    xmlURIPtr ref = NULL;
1976
1.11M
    xmlURIPtr bas = NULL;
1977
1.11M
    xmlURIPtr res = NULL;
1978
1979
1.11M
    if (valPtr == NULL)
1980
0
        return(1);
1981
1.11M
    *valPtr = NULL;
1982
1983
1.11M
    if (URI == NULL)
1984
5.39k
        return(1);
1985
1986
1.10M
    if (base == NULL) {
1987
35.1k
        val = xmlStrdup(URI);
1988
35.1k
        if (val == NULL)
1989
10
            return(-1);
1990
35.1k
        *valPtr = val;
1991
35.1k
        return(0);
1992
35.1k
    }
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
1.07M
    if (URI[0] != 0)
2003
1.04M
        ret = xmlParseURISafe((const char *) URI, &ref);
2004
29.6k
    else
2005
29.6k
        ret = 0;
2006
1.07M
    if (ret != 0)
2007
32.5k
  goto done;
2008
1.04M
    if ((ref != NULL) && (ref->scheme != NULL)) {
2009
  /*
2010
   * The URI is absolute don't modify.
2011
   */
2012
18.0k
  val = xmlStrdup(URI);
2013
18.0k
        if (val == NULL)
2014
41
            ret = -1;
2015
18.0k
  goto done;
2016
18.0k
    }
2017
2018
    /*
2019
     * If base has no scheme or authority, it is assumed to be a
2020
     * filesystem path.
2021
     */
2022
1.02M
    if (xmlStrstr(base, BAD_CAST "://") == NULL) {
2023
935k
        xmlFreeURI(ref);
2024
935k
        return(xmlResolvePath(URI, base, valPtr));
2025
935k
    }
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
86.6k
    ret = xmlParseURISafe((const char *) base, &bas);
2045
86.6k
    if (ret < 0)
2046
262
        goto done;
2047
86.3k
    if (ret != 0) {
2048
21.5k
  if (ref) {
2049
19.0k
            ret = 0;
2050
19.0k
      val = xmlSaveUri(ref);
2051
19.0k
            if (val == NULL)
2052
219
                ret = -1;
2053
19.0k
        }
2054
21.5k
  goto done;
2055
21.5k
    }
2056
64.7k
    if (ref == NULL) {
2057
  /*
2058
   * the base fragment must be ignored
2059
   */
2060
10.2k
  if (bas->fragment != NULL) {
2061
2.48k
      xmlFree(bas->fragment);
2062
2.48k
      bas->fragment = NULL;
2063
2.48k
  }
2064
10.2k
  val = xmlSaveUri(bas);
2065
10.2k
        if (val == NULL)
2066
114
            ret = -1;
2067
10.2k
  goto done;
2068
10.2k
    }
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
54.5k
    ret = -1;
2083
54.5k
    res = xmlCreateURI();
2084
54.5k
    if (res == NULL)
2085
64
  goto done;
2086
54.4k
    if ((ref->scheme == NULL) && (ref->path == NULL) &&
2087
18.7k
  ((ref->authority == NULL) && (ref->server == NULL) &&
2088
14.6k
         (ref->port == PORT_EMPTY))) {
2089
13.7k
  if (bas->scheme != NULL) {
2090
11.1k
      res->scheme = xmlMemStrdup(bas->scheme);
2091
11.1k
            if (res->scheme == NULL)
2092
24
                goto done;
2093
11.1k
        }
2094
13.7k
  if (bas->authority != NULL) {
2095
0
      res->authority = xmlMemStrdup(bas->authority);
2096
0
            if (res->authority == NULL)
2097
0
                goto done;
2098
13.7k
        } else {
2099
13.7k
      if (bas->server != NULL) {
2100
6.55k
    res->server = xmlMemStrdup(bas->server);
2101
6.55k
                if (res->server == NULL)
2102
18
                    goto done;
2103
6.55k
            }
2104
13.7k
      if (bas->user != NULL) {
2105
5.15k
    res->user = xmlMemStrdup(bas->user);
2106
5.15k
                if (res->user == NULL)
2107
18
                    goto done;
2108
5.15k
            }
2109
13.6k
      res->port = bas->port;
2110
13.6k
  }
2111
13.6k
  if (bas->path != NULL) {
2112
6.99k
      res->path = xmlMemStrdup(bas->path);
2113
6.99k
            if (res->path == NULL)
2114
22
                goto done;
2115
6.99k
        }
2116
13.6k
  if (ref->query_raw != NULL) {
2117
2.70k
      res->query_raw = xmlMemStrdup (ref->query_raw);
2118
2.70k
            if (res->query_raw == NULL)
2119
21
                goto done;
2120
10.9k
        } else if (ref->query != NULL) {
2121
0
      res->query = xmlMemStrdup(ref->query);
2122
0
            if (res->query == NULL)
2123
0
                goto done;
2124
10.9k
        } else if (bas->query_raw != NULL) {
2125
3.84k
      res->query_raw = xmlMemStrdup(bas->query_raw);
2126
3.84k
            if (res->query_raw == NULL)
2127
18
                goto done;
2128
7.12k
        } 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
13.6k
  if (ref->fragment != NULL) {
2134
10.9k
      res->fragment = xmlMemStrdup(ref->fragment);
2135
10.9k
            if (res->fragment == NULL)
2136
34
                goto done;
2137
10.9k
        }
2138
13.6k
  goto step_7;
2139
13.6k
    }
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
40.6k
    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
40.6k
    if (bas->scheme != NULL) {
2154
21.7k
  res->scheme = xmlMemStrdup(bas->scheme);
2155
21.7k
        if (res->scheme == NULL)
2156
49
            goto done;
2157
21.7k
    }
2158
2159
40.6k
    if (ref->query_raw != NULL) {
2160
8.29k
  res->query_raw = xmlMemStrdup(ref->query_raw);
2161
8.29k
        if (res->query_raw == NULL)
2162
16
            goto done;
2163
32.3k
    } 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
40.6k
    if (ref->fragment != NULL) {
2169
8.21k
  res->fragment = xmlMemStrdup(ref->fragment);
2170
8.21k
        if (res->fragment == NULL)
2171
16
            goto done;
2172
8.21k
    }
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
40.6k
    if ((ref->authority != NULL) || (ref->server != NULL) ||
2182
34.9k
         (ref->port != PORT_EMPTY)) {
2183
6.67k
  if (ref->authority != NULL) {
2184
0
      res->authority = xmlMemStrdup(ref->authority);
2185
0
            if (res->authority == NULL)
2186
0
                goto done;
2187
6.67k
        } else {
2188
6.67k
            if (ref->server != NULL) {
2189
5.68k
                res->server = xmlMemStrdup(ref->server);
2190
5.68k
                if (res->server == NULL)
2191
19
                    goto done;
2192
5.68k
            }
2193
6.66k
      if (ref->user != NULL) {
2194
3.24k
    res->user = xmlMemStrdup(ref->user);
2195
3.24k
                if (res->user == NULL)
2196
17
                    goto done;
2197
3.24k
            }
2198
6.64k
            res->port = ref->port;
2199
6.64k
  }
2200
6.64k
  if (ref->path != NULL) {
2201
1.66k
      res->path = xmlMemStrdup(ref->path);
2202
1.66k
            if (res->path == NULL)
2203
15
                goto done;
2204
1.66k
        }
2205
6.62k
  goto step_7;
2206
6.64k
    }
2207
33.9k
    if (bas->authority != NULL) {
2208
0
  res->authority = xmlMemStrdup(bas->authority);
2209
0
        if (res->authority == NULL)
2210
0
            goto done;
2211
33.9k
    } else if ((bas->server != NULL) || (bas->port != PORT_EMPTY)) {
2212
16.5k
  if (bas->server != NULL) {
2213
11.4k
      res->server = xmlMemStrdup(bas->server);
2214
11.4k
            if (res->server == NULL)
2215
30
                goto done;
2216
11.4k
        }
2217
16.5k
  if (bas->user != NULL) {
2218
5.31k
      res->user = xmlMemStrdup(bas->user);
2219
5.31k
            if (res->user == NULL)
2220
19
                goto done;
2221
5.31k
        }
2222
16.5k
  res->port = bas->port;
2223
16.5k
    }
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
33.8k
    if ((ref->path != NULL) && (ref->path[0] == '/')) {
2230
3.09k
  res->path = xmlMemStrdup(ref->path);
2231
3.09k
        if (res->path == NULL)
2232
22
            goto done;
2233
3.07k
  goto step_7;
2234
3.09k
    }
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
30.7k
    len = 2; /* extra / and 0 */
2246
30.7k
    if (ref->path != NULL)
2247
30.7k
  len += strlen(ref->path);
2248
30.7k
    if (bas->path != NULL)
2249
17.8k
  len += strlen(bas->path);
2250
30.7k
    res->path = xmlMalloc(len);
2251
30.7k
    if (res->path == NULL)
2252
71
  goto done;
2253
30.7k
    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
30.7k
    cur = 0;
2261
30.7k
    out = 0;
2262
30.7k
    if (bas->path != NULL) {
2263
205k
  while (bas->path[cur] != 0) {
2264
1.90M
      while ((bas->path[cur] != 0) && (bas->path[cur] != '/'))
2265
1.70M
    cur++;
2266
200k
      if (bas->path[cur] == 0)
2267
13.4k
    break;
2268
2269
187k
      cur++;
2270
1.95M
      while (out < cur) {
2271
1.77M
    res->path[out] = bas->path[out];
2272
1.77M
    out++;
2273
1.77M
      }
2274
187k
  }
2275
17.8k
    }
2276
30.7k
    res->path[out] = 0;
2277
2278
    /*
2279
     * b) The reference's path component is appended to the buffer
2280
     *    string.
2281
     */
2282
30.7k
    if (ref->path != NULL && ref->path[0] != 0) {
2283
29.5k
  indx = 0;
2284
  /*
2285
   * Ensure the path includes a '/'
2286
   */
2287
29.5k
  if ((out == 0) && ((bas->server != NULL) || bas->port != PORT_EMPTY))
2288
11.5k
      res->path[out++] = '/';
2289
42.8M
  while (ref->path[indx] != 0) {
2290
42.8M
      res->path[out++] = ref->path[indx++];
2291
42.8M
  }
2292
29.5k
    }
2293
30.7k
    res->path[out] = 0;
2294
2295
    /*
2296
     * Steps c) to h) are really path normalization steps
2297
     */
2298
30.7k
    xmlNormalizeURIPath(res->path);
2299
2300
54.0k
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
54.0k
    val = xmlSaveUri(res);
2308
54.0k
    if (val != NULL)
2309
53.7k
        ret = 0;
2310
2311
137k
done:
2312
137k
    if (ref != NULL)
2313
91.8k
  xmlFreeURI(ref);
2314
137k
    if (bas != NULL)
2315
64.7k
  xmlFreeURI(bas);
2316
137k
    if (res != NULL)
2317
54.4k
  xmlFreeURI(res);
2318
137k
    *valPtr = val;
2319
137k
    return(ret);
2320
54.0k
}
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
32.3k
xmlBuildURI(const xmlChar *URI, const xmlChar *base) {
2337
32.3k
    xmlChar *out;
2338
2339
32.3k
    xmlBuildURISafe(URI, base, &out);
2340
32.3k
    return(out);
2341
32.3k
}
2342
2343
static int
2344
108k
xmlParseUriOrPath(const char *str, xmlURIPtr *out, int *drive) {
2345
108k
    xmlURIPtr uri;
2346
108k
    char *buf = NULL;
2347
108k
    int ret;
2348
2349
108k
    *out = NULL;
2350
108k
    *drive = 0;
2351
2352
108k
    uri = xmlCreateURI();
2353
108k
    if (uri == NULL) {
2354
58
        ret = -1;
2355
58
  goto done;
2356
58
    }
2357
2358
108k
    if (xmlStrstr(BAD_CAST str, BAD_CAST "://") == NULL) {
2359
91.5k
        const char *path;
2360
91.5k
        size_t pathSize;
2361
91.5k
        int prependSlash = 0;
2362
2363
91.5k
        buf = xmlMemStrdup(str);
2364
91.5k
        if (buf == NULL) {
2365
31
            ret = -1;
2366
31
            goto done;
2367
31
        }
2368
91.5k
        xmlNormalizePath(buf, /* isFile */ 1);
2369
2370
91.5k
        path = buf;
2371
2372
91.5k
        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
2.33k
        }
2426
2427
91.5k
        pathSize = strlen(path);
2428
91.5k
        uri->path = xmlMalloc(pathSize + prependSlash + 1);
2429
91.5k
        if (uri->path == NULL) {
2430
29
            ret = -1;
2431
29
            goto done;
2432
29
        }
2433
91.5k
        if (prependSlash) {
2434
0
            uri->path[0] = '/';
2435
0
            memcpy(uri->path + 1, path, pathSize + 1);
2436
91.5k
        } else {
2437
91.5k
            memcpy(uri->path, path, pathSize + 1);
2438
91.5k
        }
2439
91.5k
    } else {
2440
16.9k
  ret = xmlParseURIReference(uri, str);
2441
16.9k
  if (ret != 0)
2442
5.23k
      goto done;
2443
2444
11.7k
        xmlNormalizePath(uri->path, /* isFile */ 0);
2445
11.7k
    }
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
103k
    *out = uri;
2456
103k
    uri = NULL;
2457
103k
    ret = 0;
2458
2459
108k
done:
2460
108k
    xmlFreeURI(uri);
2461
108k
    xmlFree(buf);
2462
2463
108k
    return(ret);
2464
103k
}
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
101k
{
2494
101k
    xmlChar *val = NULL;
2495
101k
    int ret = 0;
2496
101k
    size_t ix = 0;
2497
101k
    size_t nbslash = 0;
2498
101k
    size_t len = 0;
2499
101k
    xmlURIPtr ref = NULL;
2500
101k
    xmlURIPtr bas = NULL;
2501
101k
    const xmlChar *bptr, *uptr, *rptr;
2502
101k
    xmlChar *vptr;
2503
101k
    int remove_path = 0;
2504
101k
    int refDrive, baseDrive;
2505
2506
101k
    if (valPtr == NULL)
2507
0
        return(1);
2508
101k
    *valPtr = NULL;
2509
101k
    if ((URI == NULL) || (*URI == 0))
2510
6.65k
  return(1);
2511
2512
94.7k
    ret = xmlParseUriOrPath((char *) URI, &ref, &refDrive);
2513
94.7k
    if (ret < 0)
2514
99
        goto done;
2515
94.6k
    if (ret != 0) {
2516
        /* Return URI if URI is invalid */
2517
3.39k
        ret = 0;
2518
3.39k
        val = xmlStrdup(URI);
2519
3.39k
        if (val == NULL)
2520
3
            ret = -1;
2521
3.39k
        goto done;
2522
3.39k
    }
2523
2524
    /* Return URI if base is empty */
2525
91.2k
    if ((base == NULL) || (*base == 0))
2526
77.3k
        goto done;
2527
2528
13.8k
    ret = xmlParseUriOrPath((char *) base, &bas, &baseDrive);
2529
13.8k
    if (ret < 0)
2530
46
        goto done;
2531
13.8k
    if (ret != 0) {
2532
        /* Return URI if base is invalid */
2533
1.81k
        ret = 0;
2534
1.81k
        goto done;
2535
1.81k
    }
2536
2537
    /*
2538
     * If the scheme / server on the URI differs from the base,
2539
     * just return the URI
2540
     */
2541
12.0k
    if ((xmlStrcmp ((xmlChar *)bas->scheme, (xmlChar *)ref->scheme)) ||
2542
9.65k
  (xmlStrcmp ((xmlChar *)bas->server, (xmlChar *)ref->server)) ||
2543
9.12k
        (bas->port != ref->port) ||
2544
8.85k
        (baseDrive != refDrive)) {
2545
3.17k
  goto done;
2546
3.17k
    }
2547
8.85k
    if (xmlStrEqual((xmlChar *)bas->path, (xmlChar *)ref->path)) {
2548
218
  val = xmlStrdup(BAD_CAST "");
2549
218
        if (val == NULL)
2550
4
            ret = -1;
2551
218
  goto done;
2552
218
    }
2553
8.63k
    if (bas->path == NULL) {
2554
615
  val = xmlStrdup((xmlChar *)ref->path);
2555
615
        if (val == NULL) {
2556
4
            ret = -1;
2557
4
            goto done;
2558
4
        }
2559
611
  goto escape;
2560
615
    }
2561
8.01k
    if (ref->path == NULL) {
2562
1.17k
        ref->path = (char *) "/";
2563
1.17k
  remove_path = 1;
2564
1.17k
    }
2565
2566
8.01k
    bptr = (xmlChar *) bas->path;
2567
8.01k
    rptr = (xmlChar *) ref->path;
2568
2569
    /*
2570
     * Return URI if URI and base aren't both absolute or relative.
2571
     */
2572
8.01k
    if ((bptr[0] == '/') != (rptr[0] == '/'))
2573
2.02k
        goto done;
2574
2575
    /*
2576
     * At this point we can compare the two paths
2577
     */
2578
5.99k
    {
2579
5.99k
        size_t pos = 0;
2580
2581
        /*
2582
         * Next we compare the two strings and find where they first differ
2583
         */
2584
42.7k
  while ((bptr[pos] == rptr[pos]) && (bptr[pos] != 0))
2585
36.7k
      pos++;
2586
2587
5.99k
  if (bptr[pos] == rptr[pos]) {
2588
88
      val = xmlStrdup(BAD_CAST "");
2589
88
            if (val == NULL)
2590
2
                ret = -1;
2591
88
      goto done;    /* (I can't imagine why anyone would do this) */
2592
88
  }
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
5.90k
  ix = pos;
2599
27.7k
  for (; ix > 0; ix--) {
2600
24.6k
      if (rptr[ix - 1] == '/')
2601
2.83k
    break;
2602
24.6k
  }
2603
5.90k
  uptr = (xmlChar *)&rptr[ix];
2604
2605
  /*
2606
   * In base, count the number of '/' from the differing point
2607
   */
2608
371k
  for (; bptr[ix] != 0; ix++) {
2609
366k
      if (bptr[ix] == '/')
2610
107k
    nbslash++;
2611
366k
  }
2612
2613
  /*
2614
   * e.g: URI="foo/" base="foo/bar" -> "./"
2615
   */
2616
5.90k
  if (nbslash == 0 && !uptr[0]) {
2617
1.16k
      val = xmlStrdup(BAD_CAST "./");
2618
1.16k
            if (val == NULL)
2619
2
                ret = -1;
2620
1.16k
      goto done;
2621
1.16k
  }
2622
2623
4.73k
  len = (size_t) xmlStrlen (uptr) + 1;
2624
4.73k
    }
2625
2626
4.73k
    if (nbslash == 0) {
2627
1.82k
  if (uptr != NULL) {
2628
      /* exception characters from xmlSaveUri */
2629
1.82k
      val = xmlURIEscapeStr(uptr, BAD_CAST "/;&=+$,");
2630
1.82k
            if (val == NULL)
2631
3
                ret = -1;
2632
1.82k
        }
2633
1.82k
  goto done;
2634
1.82k
    }
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
2.91k
    if (len + 3 * nbslash > SIZE_MAX) {
2642
0
        ret = -1;
2643
0
        goto done;
2644
0
    }
2645
2.91k
    val = (xmlChar *) xmlMalloc (len + 3 * nbslash);
2646
2.91k
    if (val == NULL) {
2647
7
        ret = -1;
2648
7
  goto done;
2649
7
    }
2650
2.90k
    vptr = val;
2651
    /*
2652
     * Put in as many "../" as needed
2653
     */
2654
110k
    for (; nbslash>0; nbslash--) {
2655
107k
  *vptr++ = '.';
2656
107k
  *vptr++ = '.';
2657
107k
  *vptr++ = '/';
2658
107k
    }
2659
    /*
2660
     * Finish up with the end of the URI
2661
     */
2662
2.90k
    if (uptr != NULL) {
2663
2.90k
        if ((vptr > val) && (len > 0) &&
2664
2.90k
      (uptr[0] == '/') && (vptr[-1] == '/')) {
2665
0
      memcpy (vptr, uptr + 1, len - 1);
2666
0
      vptr[len - 2] = 0;
2667
2.90k
  } else {
2668
2.90k
      memcpy (vptr, uptr, len);
2669
2.90k
      vptr[len - 1] = 0;
2670
2.90k
  }
2671
2.90k
    } else {
2672
0
  vptr[len - 1] = 0;
2673
0
    }
2674
2675
3.51k
escape:
2676
    /* escape the freshly-built path */
2677
3.51k
    vptr = val;
2678
    /* exception characters from xmlSaveUri */
2679
3.51k
    val = xmlURIEscapeStr(vptr, BAD_CAST "/;&=+$,");
2680
3.51k
    if (val == NULL)
2681
7
        ret = -1;
2682
3.51k
    else
2683
3.51k
        ret = 0;
2684
3.51k
    xmlFree(vptr);
2685
2686
94.7k
done:
2687
94.7k
    if ((ret == 0) && (val == NULL)) {
2688
84.3k
        val = xmlSaveUri(ref);
2689
84.3k
        if (val == NULL)
2690
30
            ret = -1;
2691
84.3k
    }
2692
2693
    /*
2694
     * Free the working variables
2695
     */
2696
94.7k
    if (remove_path != 0)
2697
1.17k
        ref->path = NULL;
2698
94.7k
    if (ref != NULL)
2699
91.2k
  xmlFreeURI (ref);
2700
94.7k
    if (bas != NULL)
2701
12.0k
  xmlFreeURI (bas);
2702
94.7k
    if (ret != 0) {
2703
207
        xmlFree(val);
2704
207
        val = NULL;
2705
207
    }
2706
2707
94.7k
    *valPtr = val;
2708
94.7k
    return(ret);
2709
3.51k
}
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
4.44k
{
2722
4.44k
    xmlChar *val;
2723
2724
4.44k
    xmlBuildRelativeURISafe(URI, base, &val);
2725
4.44k
    return(val);
2726
4.44k
}
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
413k
{
2748
413k
    xmlChar *ret;
2749
2750
413k
    if (path == NULL)
2751
1.17k
  return(NULL);
2752
2753
    /* Check if this is an "absolute uri" */
2754
412k
    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
38.3k
        ret = xmlURIEscapeStr(path, BAD_CAST ":/?#[]@!$&()*+,;='%");
2763
373k
    } else {
2764
373k
        ret = xmlStrdup((const xmlChar *) path);
2765
373k
    }
2766
2767
412k
    return(ret);
2768
413k
}
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
369k
{
2782
369k
    return(xmlCanonicPath(path));
2783
369k
}