Coverage Report

Created: 2024-12-12 06:19

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