Coverage Report

Created: 2025-11-11 06:39

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