Coverage Report

Created: 2025-07-07 10:01

/work/workdir/UnpackedTarball/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
#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
0
#define MAX_URI_LENGTH 1024 * 1024
37
38
1.64M
#define PORT_EMPTY           0
39
3.38k
#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
0
#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
0
#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
0
#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
0
#define IS_DIGIT(x) (((x) >= '0') && ((x) <= '9'))
71
72
/*
73
 * alphanum = alpha | digit
74
 */
75
0
#define IS_ALPHANUM(x) (IS_ALPHA(x) || IS_DIGIT(x))
76
77
/*
78
 * mark = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")"
79
 */
80
81
0
#define IS_MARK(x) (((x) == '-') || ((x) == '_') || ((x) == '.') ||     \
82
0
    ((x) == '!') || ((x) == '~') || ((x) == '*') || ((x) == '\'') ||    \
83
0
    ((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
0
#define IS_RESERVED(x) (((x) == ';') || ((x) == '/') || ((x) == '?') || \
98
0
        ((x) == ':') || ((x) == '@') || ((x) == '&') || ((x) == '=') || \
99
0
        ((x) == '+') || ((x) == '$') || ((x) == ',') || ((x) == '[') || \
100
0
        ((x) == ']'))
101
102
/*
103
 * unreserved = alphanum | mark
104
 */
105
0
#define IS_UNRESERVED(x) (IS_ALPHANUM(x) || IS_MARK(x))
106
107
/*
108
 * Skip to next pointer char, handle escaped sequences
109
 */
110
75.8M
#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
1.55M
#define STRNDUP(s, n) (char *) xmlStrndup((const xmlChar *)(s), (n))
122
123
/************************************************************************
124
 *                  *
125
 *                         RFC 3986 parser        *
126
 *                  *
127
 ************************************************************************/
128
129
27.1M
#define ISA_DIGIT(p) ((*(p) >= '0') && (*(p) <= '9'))
130
90.4M
#define ISA_ALPHA(p) (((*(p) >= 'a') && (*(p) <= 'z')) ||   \
131
90.4M
                      ((*(p) >= 'A') && (*(p) <= 'Z')))
132
#define ISA_HEXDIG(p)             \
133
337k
       (ISA_DIGIT(p) || ((*(p) >= 'a') && (*(p) <= 'f')) ||   \
134
337k
        ((*(p) >= 'A') && (*(p) <= 'F')))
135
136
/*
137
 *    sub-delims    = "!" / "$" / "&" / "'" / "(" / ")"
138
 *                     / "*" / "+" / "," / ";" / "="
139
 */
140
#define ISA_SUB_DELIM(p)            \
141
72.7M
      (((*(p) == '!')) || ((*(p) == '$')) || ((*(p) == '&')) ||   \
142
9.20M
       ((*(p) == '(')) || ((*(p) == ')')) || ((*(p) == '*')) ||   \
143
9.20M
       ((*(p) == '+')) || ((*(p) == ',')) || ((*(p) == ';')) ||   \
144
9.20M
       ((*(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
77.7M
      ((ISA_ALPHA(p)) || (ISA_DIGIT(p)) || ((*(p) == '-')) ||   \
164
77.7M
       ((*(p) == '.')) || ((*(p) == '_')) || ((*(p) == '~')))
165
166
/*
167
 *    pct-encoded   = "%" HEXDIG HEXDIG
168
 */
169
#define ISA_PCT_ENCODED(p)            \
170
87.1M
     ((*(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
95.8M
     (ISA_UNRESERVED(u, p) || ISA_PCT_ENCODED(p) || ISA_SUB_DELIM(p) ||  \
177
49.0M
      ((*(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
155M
#define ISA_UNRESERVED(u, p) (xmlIsUnreserved(u, p))
192
193
9.31M
#define XML_URI_ALLOW_UNWISE    1
194
2.55M
#define XML_URI_NO_UNESCAPE     2
195
9.31M
#define XML_URI_ALLOW_UCSCHAR   4
196
197
static int
198
77.7M
xmlIsUnreserved(xmlURIPtr uri, const char *cur) {
199
77.7M
    if (uri == NULL)
200
0
        return(0);
201
202
77.7M
    if (ISA_STRICTLY_UNRESERVED(cur))
203
68.4M
        return(1);
204
205
9.31M
    if (uri->cleanup & XML_URI_ALLOW_UNWISE) {
206
0
        if (IS_UNWISE(cur))
207
0
            return(1);
208
9.31M
    } else if (uri->cleanup & XML_URI_ALLOW_UCSCHAR) {
209
0
        if (ISA_UCSCHAR(cur))
210
0
            return(1);
211
0
    }
212
213
9.31M
    return(0);
214
9.31M
}
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
1.64M
xmlParse3986Scheme(xmlURIPtr uri, const char **str) {
229
1.64M
    const char *cur;
230
231
1.64M
    cur = *str;
232
1.64M
    if (!ISA_ALPHA(cur))
233
120k
  return(1);
234
1.52M
    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
5.52M
    while (ISA_ALPHA(cur) || ISA_DIGIT(cur) ||
245
5.52M
           (*cur == '+') || (*cur == '-') || (*cur == '.')) cur++;
246
1.52M
    if (uri != NULL) {
247
1.52M
  if (uri->scheme != NULL) xmlFree(uri->scheme);
248
1.52M
  uri->scheme = STRNDUP(*str, cur - *str);
249
1.52M
        if (uri->scheme == NULL)
250
0
            return(-1);
251
1.52M
    }
252
1.52M
    *str = cur;
253
1.52M
    return(0);
254
1.52M
}
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
41.1k
{
274
41.1k
    const char *cur;
275
276
41.1k
    cur = *str;
277
278
545k
    while ((ISA_PCHAR(uri, cur)) || (*cur == '/') || (*cur == '?') ||
279
545k
           (*cur == '[') || (*cur == ']'))
280
504k
        NEXT(cur);
281
41.1k
    if (uri != NULL) {
282
41.1k
        if (uri->fragment != NULL)
283
0
            xmlFree(uri->fragment);
284
41.1k
  if (uri->cleanup & XML_URI_NO_UNESCAPE)
285
0
      uri->fragment = STRNDUP(*str, cur - *str);
286
41.1k
  else
287
41.1k
      uri->fragment = xmlURIUnescapeString(*str, cur - *str, NULL);
288
41.1k
        if (uri->fragment == NULL)
289
0
            return (-1);
290
41.1k
    }
291
41.1k
    *str = cur;
292
41.1k
    return (0);
293
41.1k
}
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
30.2k
{
309
30.2k
    const char *cur;
310
311
30.2k
    cur = *str;
312
313
718k
    while ((ISA_PCHAR(uri, cur)) || (*cur == '/') || (*cur == '?'))
314
688k
        NEXT(cur);
315
30.2k
    if (uri != NULL) {
316
30.2k
        if (uri->query != NULL)
317
0
            xmlFree(uri->query);
318
30.2k
  if (uri->cleanup & XML_URI_NO_UNESCAPE)
319
0
      uri->query = STRNDUP(*str, cur - *str);
320
30.2k
  else
321
30.2k
      uri->query = xmlURIUnescapeString(*str, cur - *str, NULL);
322
30.2k
        if (uri->query == NULL)
323
0
            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
30.2k
  if (uri->query_raw != NULL)
329
0
      xmlFree (uri->query_raw);
330
30.2k
  uri->query_raw = STRNDUP (*str, cur - *str);
331
30.2k
        if (uri->query_raw == NULL)
332
0
            return (-1);
333
30.2k
    }
334
30.2k
    *str = cur;
335
30.2k
    return (0);
336
30.2k
}
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
16.9k
{
353
16.9k
    const char *cur = *str;
354
16.9k
    int port = 0;
355
356
16.9k
    if (ISA_DIGIT(cur)) {
357
37.4k
  while (ISA_DIGIT(cur)) {
358
31.9k
            int digit = *cur - '0';
359
360
31.9k
            if (port > INT_MAX / 10)
361
589
                return(1);
362
31.3k
            port *= 10;
363
31.3k
            if (port > INT_MAX - digit)
364
657
                return(1);
365
30.6k
      port += digit;
366
367
30.6k
      cur++;
368
30.6k
  }
369
5.53k
  if (uri != NULL)
370
5.53k
      uri->port = port;
371
5.53k
  *str = cur;
372
5.53k
  return(0);
373
6.78k
    }
374
10.1k
    return(1);
375
16.9k
}
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
802k
{
392
802k
    const char *cur;
393
394
802k
    cur = *str;
395
14.4M
    while (ISA_UNRESERVED(uri, cur) || ISA_PCT_ENCODED(cur) ||
396
14.4M
           ISA_SUB_DELIM(cur) || (*cur == ':'))
397
13.6M
  NEXT(cur);
398
802k
    if (*cur == '@') {
399
3.45k
  if (uri != NULL) {
400
3.45k
      if (uri->user != NULL) xmlFree(uri->user);
401
3.45k
      if (uri->cleanup & XML_URI_NO_UNESCAPE)
402
0
    uri->user = STRNDUP(*str, cur - *str);
403
3.45k
      else
404
3.45k
    uri->user = xmlURIUnescapeString(*str, cur - *str, NULL);
405
3.45k
            if (uri->user == NULL)
406
0
                return(-1);
407
3.45k
  }
408
3.45k
  *str = cur;
409
3.45k
  return(0);
410
3.45k
    }
411
798k
    return(1);
412
802k
}
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
18.7k
xmlParse3986DecOctet(const char **str) {
430
18.7k
    const char *cur = *str;
431
432
18.7k
    if (!(ISA_DIGIT(cur)))
433
429
        return(1);
434
18.3k
    if (!ISA_DIGIT(cur+1))
435
11.2k
  cur++;
436
7.04k
    else if ((*cur != '0') && (ISA_DIGIT(cur + 1)) && (!ISA_DIGIT(cur+2)))
437
1.57k
  cur += 2;
438
5.46k
    else if ((*cur == '1') && (ISA_DIGIT(cur + 1)) && (ISA_DIGIT(cur + 2)))
439
1.39k
  cur += 3;
440
4.07k
    else if ((*cur == '2') && (*(cur + 1) >= '0') &&
441
4.07k
       (*(cur + 1) <= '4') && (ISA_DIGIT(cur + 2)))
442
1.23k
  cur += 3;
443
2.83k
    else if ((*cur == '2') && (*(cur + 1) == '5') &&
444
2.83k
       (*(cur + 2) >= '0') && (*(cur + 1) <= '5'))
445
247
  cur += 3;
446
2.58k
    else
447
2.58k
        return(1);
448
15.7k
    *str = cur;
449
15.7k
    return(0);
450
18.3k
}
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
802k
{
469
802k
    const char *cur = *str;
470
802k
    const char *host;
471
472
802k
    host = cur;
473
    /*
474
     * IPv6 and future addressing scheme are enclosed between brackets
475
     */
476
802k
    if (*cur == '[') {
477
2.37k
        cur++;
478
338k
  while ((*cur != ']') && (*cur != 0))
479
336k
      cur++;
480
2.37k
  if (*cur != ']')
481
308
      return(1);
482
2.06k
  cur++;
483
2.06k
  goto found;
484
2.37k
    }
485
    /*
486
     * try to parse an IPv4
487
     */
488
799k
    if (ISA_DIGIT(cur)) {
489
13.6k
        if (xmlParse3986DecOctet(&cur) != 0)
490
1.23k
      goto not_ipv4;
491
12.3k
  if (*cur != '.')
492
7.23k
      goto not_ipv4;
493
5.13k
  cur++;
494
5.13k
        if (xmlParse3986DecOctet(&cur) != 0)
495
1.76k
      goto not_ipv4;
496
3.37k
  if (*cur != '.')
497
3.34k
      goto not_ipv4;
498
21
        if (xmlParse3986DecOctet(&cur) != 0)
499
21
      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
13.6k
not_ipv4:
506
13.6k
        cur = *str;
507
13.6k
    }
508
    /*
509
     * then this should be a hostname which can be empty
510
     */
511
14.2M
    while (ISA_UNRESERVED(uri, cur) ||
512
14.2M
           ISA_PCT_ENCODED(cur) || ISA_SUB_DELIM(cur))
513
13.4M
        NEXT(cur);
514
801k
found:
515
801k
    if (uri != NULL) {
516
801k
  if (uri->authority != NULL) xmlFree(uri->authority);
517
801k
  uri->authority = NULL;
518
801k
  if (uri->server != NULL) xmlFree(uri->server);
519
801k
  if (cur != host) {
520
791k
      if (uri->cleanup & XML_URI_NO_UNESCAPE)
521
0
    uri->server = STRNDUP(host, cur - host);
522
791k
      else
523
791k
    uri->server = xmlURIUnescapeString(host, cur - host, NULL);
524
791k
            if (uri->server == NULL)
525
0
                return(-1);
526
791k
  } else
527
10.6k
      uri->server = NULL;
528
801k
    }
529
801k
    *str = cur;
530
801k
    return(0);
531
801k
}
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
802k
{
548
802k
    const char *cur;
549
802k
    int ret;
550
551
802k
    cur = *str;
552
    /*
553
     * try to parse an userinfo and check for the trailing @
554
     */
555
802k
    ret = xmlParse3986Userinfo(uri, &cur);
556
802k
    if (ret < 0)
557
0
        return(ret);
558
802k
    if ((ret != 0) || (*cur != '@'))
559
798k
        cur = *str;
560
3.45k
    else
561
3.45k
        cur++;
562
802k
    ret = xmlParse3986Host(uri, &cur);
563
802k
    if (ret != 0) return(ret);
564
801k
    if (*cur == ':') {
565
16.9k
        cur++;
566
16.9k
        ret = xmlParse3986Port(uri, &cur);
567
16.9k
  if (ret != 0) return(ret);
568
16.9k
    }
569
790k
    *str = cur;
570
790k
    return(0);
571
801k
}
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
3.10M
{
592
3.10M
    const char *cur;
593
594
3.10M
    cur = *str;
595
3.10M
    if (!ISA_PCHAR(uri, cur) || (*cur == forbid)) {
596
121k
        if (empty)
597
117k
      return(0);
598
4.50k
  return(1);
599
121k
    }
600
2.98M
    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
43.7M
    while (ISA_PCHAR(uri, cur) && (*cur != forbid))
611
40.7M
        NEXT(cur);
612
2.98M
    *str = cur;
613
2.98M
    return (0);
614
3.10M
}
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
790k
{
631
790k
    const char *cur;
632
790k
    int ret;
633
634
790k
    cur = *str;
635
636
2.84M
    while (*cur == '/') {
637
2.05M
        cur++;
638
2.05M
  ret = xmlParse3986Segment(uri, &cur, 0, 1);
639
2.05M
  if (ret != 0) return(ret);
640
2.05M
    }
641
790k
    if (uri != NULL) {
642
790k
  if (uri->path != NULL) xmlFree(uri->path);
643
790k
        if (*str != cur) {
644
749k
            if (uri->cleanup & XML_URI_NO_UNESCAPE)
645
0
                uri->path = STRNDUP(*str, cur - *str);
646
749k
            else
647
749k
                uri->path = xmlURIUnescapeString(*str, cur - *str, NULL);
648
749k
            if (uri->path == NULL)
649
0
                return (-1);
650
749k
        } else {
651
40.6k
            uri->path = NULL;
652
40.6k
        }
653
790k
    }
654
790k
    *str = cur;
655
790k
    return (0);
656
790k
}
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
7.18k
{
673
7.18k
    const char *cur;
674
7.18k
    int ret;
675
676
7.18k
    cur = *str;
677
678
7.18k
    if (*cur != '/')
679
0
        return(1);
680
7.18k
    cur++;
681
7.18k
    ret = xmlParse3986Segment(uri, &cur, 0, 0);
682
7.18k
    if (ret == 0) {
683
9.78k
  while (*cur == '/') {
684
5.37k
      cur++;
685
5.37k
      ret = xmlParse3986Segment(uri, &cur, 0, 1);
686
5.37k
      if (ret != 0) return(ret);
687
5.37k
  }
688
4.41k
    }
689
7.18k
    if (uri != NULL) {
690
7.18k
  if (uri->path != NULL) xmlFree(uri->path);
691
7.18k
        if (cur != *str) {
692
7.18k
            if (uri->cleanup & XML_URI_NO_UNESCAPE)
693
0
                uri->path = STRNDUP(*str, cur - *str);
694
7.18k
            else
695
7.18k
                uri->path = xmlURIUnescapeString(*str, cur - *str, NULL);
696
7.18k
            if (uri->path == NULL)
697
0
                return (-1);
698
7.18k
        } else {
699
0
            uri->path = NULL;
700
0
        }
701
7.18k
    }
702
7.18k
    *str = cur;
703
7.18k
    return (0);
704
7.18k
}
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
674k
{
721
674k
    const char *cur;
722
674k
    int ret;
723
724
674k
    cur = *str;
725
726
674k
    ret = xmlParse3986Segment(uri, &cur, 0, 0);
727
674k
    if (ret != 0) return(ret);
728
715k
    while (*cur == '/') {
729
41.4k
        cur++;
730
41.4k
  ret = xmlParse3986Segment(uri, &cur, 0, 1);
731
41.4k
  if (ret != 0) return(ret);
732
41.4k
    }
733
674k
    if (uri != NULL) {
734
674k
  if (uri->path != NULL) xmlFree(uri->path);
735
674k
        if (cur != *str) {
736
674k
            if (uri->cleanup & XML_URI_NO_UNESCAPE)
737
0
                uri->path = STRNDUP(*str, cur - *str);
738
674k
            else
739
674k
                uri->path = xmlURIUnescapeString(*str, cur - *str, NULL);
740
674k
            if (uri->path == NULL)
741
0
                return (-1);
742
674k
        } else {
743
0
            uri->path = NULL;
744
0
        }
745
674k
    }
746
674k
    *str = cur;
747
674k
    return (0);
748
674k
}
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
254k
{
765
254k
    const char *cur;
766
254k
    int ret;
767
768
254k
    cur = *str;
769
770
254k
    ret = xmlParse3986Segment(uri, &cur, ':', 0);
771
254k
    if (ret != 0) return(ret);
772
322k
    while (*cur == '/') {
773
69.2k
        cur++;
774
69.2k
  ret = xmlParse3986Segment(uri, &cur, 0, 1);
775
69.2k
  if (ret != 0) return(ret);
776
69.2k
    }
777
253k
    if (uri != NULL) {
778
253k
  if (uri->path != NULL) xmlFree(uri->path);
779
253k
        if (cur != *str) {
780
253k
            if (uri->cleanup & XML_URI_NO_UNESCAPE)
781
0
                uri->path = STRNDUP(*str, cur - *str);
782
253k
            else
783
253k
                uri->path = xmlURIUnescapeString(*str, cur - *str, NULL);
784
253k
            if (uri->path == NULL)
785
0
                return (-1);
786
253k
        } else {
787
0
            uri->path = NULL;
788
0
        }
789
253k
    }
790
253k
    *str = cur;
791
253k
    return (0);
792
253k
}
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
1.48M
{
812
1.48M
    const char *cur;
813
1.48M
    int ret;
814
815
1.48M
    cur = *str;
816
817
1.48M
    if ((*cur == '/') && (*(cur + 1) == '/')) {
818
786k
        cur += 2;
819
786k
  ret = xmlParse3986Authority(uri, &cur);
820
786k
  if (ret != 0) return(ret);
821
        /*
822
         * An empty server is marked with a special URI value.
823
         */
824
775k
  if ((uri->server == NULL) && (uri->port == PORT_EMPTY))
825
3.38k
      uri->port = PORT_EMPTY_SERVER;
826
775k
  ret = xmlParse3986PathAbEmpty(uri, &cur);
827
775k
  if (ret != 0) return(ret);
828
775k
  *str = cur;
829
775k
  return(0);
830
775k
    } else if (*cur == '/') {
831
4.26k
        ret = xmlParse3986PathAbsolute(uri, &cur);
832
4.26k
  if (ret != 0) return(ret);
833
695k
    } else if (ISA_PCHAR(uri, cur)) {
834
674k
        ret = xmlParse3986PathRootless(uri, &cur);
835
674k
  if (ret != 0) return(ret);
836
674k
    } else {
837
  /* path-empty is effectively empty */
838
21.3k
  if (uri != NULL) {
839
21.3k
      if (uri->path != NULL) xmlFree(uri->path);
840
21.3k
      uri->path = NULL;
841
21.3k
  }
842
21.3k
    }
843
700k
    *str = cur;
844
700k
    return (0);
845
1.48M
}
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
283k
xmlParse3986RelativeRef(xmlURIPtr uri, const char *str) {
865
283k
    int ret;
866
867
283k
    if ((*str == '/') && (*(str + 1) == '/')) {
868
15.8k
        str += 2;
869
15.8k
  ret = xmlParse3986Authority(uri, &str);
870
15.8k
  if (ret != 0) return(ret);
871
14.7k
  ret = xmlParse3986PathAbEmpty(uri, &str);
872
14.7k
  if (ret != 0) return(ret);
873
267k
    } else if (*str == '/') {
874
2.91k
  ret = xmlParse3986PathAbsolute(uri, &str);
875
2.91k
  if (ret != 0) return(ret);
876
264k
    } else if (ISA_PCHAR(uri, str)) {
877
254k
        ret = xmlParse3986PathNoScheme(uri, &str);
878
254k
  if (ret != 0) return(ret);
879
254k
    } else {
880
  /* path-empty is effectively empty */
881
10.0k
  if (uri != NULL) {
882
10.0k
      if (uri->path != NULL) xmlFree(uri->path);
883
10.0k
      uri->path = NULL;
884
10.0k
  }
885
10.0k
    }
886
887
280k
    if (*str == '?') {
888
14.9k
  str++;
889
14.9k
  ret = xmlParse3986Query(uri, &str);
890
14.9k
  if (ret != 0) return(ret);
891
14.9k
    }
892
280k
    if (*str == '#') {
893
11.5k
  str++;
894
11.5k
  ret = xmlParse3986Fragment(uri, &str);
895
11.5k
  if (ret != 0) return(ret);
896
11.5k
    }
897
280k
    if (*str != 0) {
898
198k
  xmlCleanURI(uri);
899
198k
  return(1);
900
198k
    }
901
82.1k
    return(0);
902
280k
}
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
1.64M
xmlParse3986URI(xmlURIPtr uri, const char *str) {
919
1.64M
    int ret;
920
921
1.64M
    ret = xmlParse3986Scheme(uri, &str);
922
1.64M
    if (ret != 0) return(ret);
923
1.52M
    if (*str != ':') {
924
34.5k
  return(1);
925
34.5k
    }
926
1.48M
    str++;
927
1.48M
    ret = xmlParse3986HierPart(uri, &str);
928
1.48M
    if (ret != 0) return(ret);
929
1.47M
    if (*str == '?') {
930
15.2k
  str++;
931
15.2k
  ret = xmlParse3986Query(uri, &str);
932
15.2k
  if (ret != 0) return(ret);
933
15.2k
    }
934
1.47M
    if (*str == '#') {
935
29.6k
  str++;
936
29.6k
  ret = xmlParse3986Fragment(uri, &str);
937
29.6k
  if (ret != 0) return(ret);
938
29.6k
    }
939
1.47M
    if (*str != 0) {
940
118k
  xmlCleanURI(uri);
941
118k
  return(1);
942
118k
    }
943
1.35M
    return(0);
944
1.47M
}
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
1.64M
xmlParse3986URIReference(xmlURIPtr uri, const char *str) {
960
1.64M
    int ret;
961
962
1.64M
    if (str == NULL)
963
0
  return(-1);
964
1.64M
    xmlCleanURI(uri);
965
966
    /*
967
     * Try first to parse absolute refs, then fallback to relative if
968
     * it fails.
969
     */
970
1.64M
    ret = xmlParse3986URI(uri, str);
971
1.64M
    if (ret < 0)
972
0
        return(ret);
973
1.64M
    if (ret != 0) {
974
283k
  xmlCleanURI(uri);
975
283k
        ret = xmlParse3986RelativeRef(uri, str);
976
283k
  if (ret != 0) {
977
201k
      xmlCleanURI(uri);
978
201k
      return(ret);
979
201k
  }
980
283k
    }
981
1.43M
    return(0);
982
1.64M
}
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
1.64M
xmlParseURISafe(const char *str, xmlURIPtr *uriOut) {
1000
1.64M
    xmlURIPtr uri;
1001
1.64M
    int ret;
1002
1003
1.64M
    if (uriOut == NULL)
1004
0
        return(1);
1005
1.64M
    *uriOut = NULL;
1006
1.64M
    if (str == NULL)
1007
0
  return(1);
1008
1009
1.64M
    uri = xmlCreateURI();
1010
1.64M
    if (uri == NULL)
1011
0
        return(-1);
1012
1013
1.64M
    ret = xmlParse3986URIReference(uri, str);
1014
1.64M
    if (ret) {
1015
201k
        xmlFreeURI(uri);
1016
201k
        return(ret);
1017
201k
    }
1018
1019
1.43M
    *uriOut = uri;
1020
1.43M
    return(0);
1021
1.64M
}
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
0
xmlParseURI(const char *str) {
1035
0
    xmlURIPtr uri;
1036
0
    xmlParseURISafe(str, &uri);
1037
0
    return(uri);
1038
0
}
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
1.64M
xmlCreateURI(void) {
1104
1.64M
    xmlURIPtr ret;
1105
1106
1.64M
    ret = (xmlURIPtr) xmlMalloc(sizeof(xmlURI));
1107
1.64M
    if (ret == NULL)
1108
0
  return(NULL);
1109
1.64M
    memset(ret, 0, sizeof(xmlURI));
1110
1.64M
    ret->port = PORT_EMPTY;
1111
1.64M
    return(ret);
1112
1.64M
}
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
0
xmlSaveUriRealloc(xmlChar *ret, int *max) {
1122
0
    xmlChar *temp;
1123
0
    int newSize;
1124
1125
0
    newSize = xmlGrowCapacity(*max, 1, 80, MAX_URI_LENGTH);
1126
0
    if (newSize < 0)
1127
0
        return(NULL);
1128
0
    temp = xmlRealloc(ret, newSize + 1);
1129
0
    if (temp == NULL)
1130
0
        return(NULL);
1131
0
    *max = newSize;
1132
0
    return(temp);
1133
0
}
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
0
xmlSaveUri(xmlURIPtr uri) {
1145
0
    xmlChar *ret = NULL;
1146
0
    xmlChar *temp;
1147
0
    const char *p;
1148
0
    int len;
1149
0
    int max;
1150
1151
0
    if (uri == NULL) return(NULL);
1152
1153
1154
0
    max = 80;
1155
0
    ret = xmlMalloc(max + 1);
1156
0
    if (ret == NULL)
1157
0
  return(NULL);
1158
0
    len = 0;
1159
1160
0
    if (uri->scheme != NULL) {
1161
0
  p = uri->scheme;
1162
0
  while (*p != 0) {
1163
0
      if (len >= max) {
1164
0
                temp = xmlSaveUriRealloc(ret, &max);
1165
0
                if (temp == NULL) goto mem_error;
1166
0
    ret = temp;
1167
0
      }
1168
0
      ret[len++] = *p++;
1169
0
  }
1170
0
  if (len >= max) {
1171
0
            temp = xmlSaveUriRealloc(ret, &max);
1172
0
            if (temp == NULL) goto mem_error;
1173
0
            ret = temp;
1174
0
  }
1175
0
  ret[len++] = ':';
1176
0
    }
1177
0
    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
0
    } else {
1196
0
  if ((uri->server != NULL) || (uri->port != PORT_EMPTY)) {
1197
0
      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
0
      ret[len++] = '/';
1203
0
      ret[len++] = '/';
1204
0
      if (uri->user != NULL) {
1205
0
    p = uri->user;
1206
0
    while (*p != 0) {
1207
0
        if (len + 3 >= max) {
1208
0
                        temp = xmlSaveUriRealloc(ret, &max);
1209
0
                        if (temp == NULL) goto mem_error;
1210
0
                        ret = temp;
1211
0
        }
1212
0
        if ((IS_UNRESERVED(*(p))) ||
1213
0
      ((*(p) == ';')) || ((*(p) == ':')) ||
1214
0
      ((*(p) == '&')) || ((*(p) == '=')) ||
1215
0
      ((*(p) == '+')) || ((*(p) == '$')) ||
1216
0
      ((*(p) == ',')))
1217
0
      ret[len++] = *p++;
1218
0
        else {
1219
0
      int val = *(unsigned char *)p++;
1220
0
      int hi = val / 0x10, lo = val % 0x10;
1221
0
      ret[len++] = '%';
1222
0
      ret[len++] = hi + (hi > 9? 'A'-10 : '0');
1223
0
      ret[len++] = lo + (lo > 9? 'A'-10 : '0');
1224
0
        }
1225
0
    }
1226
0
    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
0
    ret[len++] = '@';
1232
0
      }
1233
0
      if (uri->server != NULL) {
1234
0
    p = uri->server;
1235
0
    while (*p != 0) {
1236
0
        if (len >= max) {
1237
0
      temp = xmlSaveUriRealloc(ret, &max);
1238
0
      if (temp == NULL) goto mem_error;
1239
0
      ret = temp;
1240
0
        }
1241
                    /* TODO: escaping? */
1242
0
        ret[len++] = (xmlChar) *p++;
1243
0
    }
1244
0
      }
1245
0
            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
0
  } 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
0
  } 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
0
  if (uri->path != NULL) {
1289
0
      p = uri->path;
1290
      /*
1291
       * the colon in file:///d: should not be escaped or
1292
       * Windows accesses fail later.
1293
       */
1294
0
      if ((uri->scheme != NULL) &&
1295
0
    (p[0] == '/') &&
1296
0
    (((p[1] >= 'a') && (p[1] <= 'z')) ||
1297
0
     ((p[1] >= 'A') && (p[1] <= 'Z'))) &&
1298
0
    (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
0
      while (*p != 0) {
1310
0
    if (len + 3 >= max) {
1311
0
                    temp = xmlSaveUriRealloc(ret, &max);
1312
0
                    if (temp == NULL) goto mem_error;
1313
0
                    ret = temp;
1314
0
    }
1315
0
    if ((IS_UNRESERVED(*(p))) || ((*(p) == '/')) ||
1316
0
                    ((*(p) == ';')) || ((*(p) == '@')) || ((*(p) == '&')) ||
1317
0
              ((*(p) == '=')) || ((*(p) == '+')) || ((*(p) == '$')) ||
1318
0
              ((*(p) == ',')))
1319
0
        ret[len++] = *p++;
1320
0
    else {
1321
0
        int val = *(unsigned char *)p++;
1322
0
        int hi = val / 0x10, lo = val % 0x10;
1323
0
        ret[len++] = '%';
1324
0
        ret[len++] = hi + (hi > 9? 'A'-10 : '0');
1325
0
        ret[len++] = lo + (lo > 9? 'A'-10 : '0');
1326
0
    }
1327
0
      }
1328
0
  }
1329
0
  if (uri->query_raw != NULL) {
1330
0
      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
0
      ret[len++] = '?';
1336
0
      p = uri->query_raw;
1337
0
      while (*p != 0) {
1338
0
    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
0
    ret[len++] = *p++;
1344
0
      }
1345
0
  } 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
0
    }
1371
0
    if (uri->fragment != NULL) {
1372
0
  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
0
  ret[len++] = '#';
1378
0
  p = uri->fragment;
1379
0
  while (*p != 0) {
1380
0
      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
0
      if ((IS_UNRESERVED(*(p))) || (IS_RESERVED(*(p))))
1386
0
    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
0
  }
1395
0
    }
1396
0
    if (len >= max) {
1397
0
        temp = xmlSaveUriRealloc(ret, &max);
1398
0
        if (temp == NULL) goto mem_error;
1399
0
        ret = temp;
1400
0
    }
1401
0
    ret[len] = 0;
1402
0
    return(ret);
1403
1404
0
mem_error:
1405
0
    xmlFree(ret);
1406
0
    return(NULL);
1407
0
}
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
2.44M
xmlCleanURI(xmlURIPtr uri) {
1435
2.44M
    if (uri == NULL) return;
1436
1437
2.44M
    if (uri->scheme != NULL) xmlFree(uri->scheme);
1438
2.44M
    uri->scheme = NULL;
1439
2.44M
    if (uri->server != NULL) xmlFree(uri->server);
1440
2.44M
    uri->server = NULL;
1441
2.44M
    if (uri->user != NULL) xmlFree(uri->user);
1442
2.44M
    uri->user = NULL;
1443
2.44M
    if (uri->path != NULL) xmlFree(uri->path);
1444
2.44M
    uri->path = NULL;
1445
2.44M
    if (uri->fragment != NULL) xmlFree(uri->fragment);
1446
2.44M
    uri->fragment = NULL;
1447
2.44M
    if (uri->opaque != NULL) xmlFree(uri->opaque);
1448
2.44M
    uri->opaque = NULL;
1449
2.44M
    if (uri->authority != NULL) xmlFree(uri->authority);
1450
2.44M
    uri->authority = NULL;
1451
2.44M
    if (uri->query != NULL) xmlFree(uri->query);
1452
2.44M
    uri->query = NULL;
1453
2.44M
    if (uri->query_raw != NULL) xmlFree(uri->query_raw);
1454
2.44M
    uri->query_raw = NULL;
1455
2.44M
}
1456
1457
/**
1458
 * xmlFreeURI:
1459
 * @uri:  pointer to an xmlURI
1460
 *
1461
 * Free up the xmlURI struct
1462
 */
1463
void
1464
1.64M
xmlFreeURI(xmlURIPtr uri) {
1465
1.64M
    if (uri == NULL) return;
1466
1467
1.64M
    if (uri->scheme != NULL) xmlFree(uri->scheme);
1468
1.64M
    if (uri->server != NULL) xmlFree(uri->server);
1469
1.64M
    if (uri->user != NULL) xmlFree(uri->user);
1470
1.64M
    if (uri->path != NULL) xmlFree(uri->path);
1471
1.64M
    if (uri->fragment != NULL) xmlFree(uri->fragment);
1472
1.64M
    if (uri->opaque != NULL) xmlFree(uri->opaque);
1473
1.64M
    if (uri->authority != NULL) xmlFree(uri->authority);
1474
1.64M
    if (uri->query != NULL) xmlFree(uri->query);
1475
1.64M
    if (uri->query_raw != NULL) xmlFree(uri->query_raw);
1476
1.64M
    xmlFree(uri);
1477
1.64M
}
1478
1479
/************************************************************************
1480
 *                  *
1481
 *      Helper functions        *
1482
 *                  *
1483
 ************************************************************************/
1484
1485
static int
1486
0
xmlIsPathSeparator(int c, int isFile) {
1487
0
    (void) isFile;
1488
1489
0
    if (c == '/')
1490
0
        return(1);
1491
1492
#if defined(_WIN32) || defined(__CYGWIN__)
1493
    if (isFile && (c == '\\'))
1494
        return(1);
1495
#endif
1496
1497
0
    return(0);
1498
0
}
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
0
xmlNormalizePath(char *path, int isFile) {
1511
0
    char *cur, *out;
1512
0
    int numSeg = 0;
1513
1514
0
    if (path == NULL)
1515
0
  return(-1);
1516
1517
0
    cur = path;
1518
0
    out = path;
1519
1520
0
    if (*cur == 0)
1521
0
        return(0);
1522
1523
0
    if (xmlIsPathSeparator(*cur, isFile)) {
1524
0
        cur++;
1525
0
        *out++ = '/';
1526
0
    }
1527
1528
0
    while (*cur != 0) {
1529
        /*
1530
         * At this point, out is either empty or ends with a separator.
1531
         * Collapse multiple separators first.
1532
         */
1533
0
        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
0
            cur++;
1540
0
        }
1541
1542
0
        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
0
        while ((*cur != 0) && !xmlIsPathSeparator(*cur, isFile)) {
1579
0
            *out++ = *cur++;
1580
0
        }
1581
1582
        /* Copy separator */
1583
0
        if (*cur != 0) {
1584
0
            cur++;
1585
0
            *out++ = '/';
1586
0
        }
1587
1588
0
        numSeg++;
1589
0
    }
1590
1591
    /* Keep "." if output is empty and it's a file */
1592
0
    if ((isFile) && (out <= path))
1593
0
        *out++ = '.';
1594
0
    *out = 0;
1595
1596
0
    return(0);
1597
0
}
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
0
xmlNormalizeURIPath(char *path) {
1612
0
    return(xmlNormalizePath(path, 0));
1613
0
}
1614
1615
237k
static int is_hex(char c) {
1616
237k
    if (((c >= '0') && (c <= '9')) ||
1617
237k
        ((c >= 'a') && (c <= 'f')) ||
1618
237k
        ((c >= 'A') && (c <= 'F')))
1619
182k
  return(1);
1620
55.2k
    return(0);
1621
237k
}
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
2.55M
xmlURIUnescapeString(const char *str, int len, char *target) {
1639
2.55M
    char *ret, *out;
1640
2.55M
    const char *in;
1641
1642
2.55M
    if (str == NULL)
1643
0
  return(NULL);
1644
2.55M
    if (len <= 0) len = strlen(str);
1645
2.55M
    if (len < 0) return(NULL);
1646
1647
2.55M
    if (target == NULL) {
1648
2.55M
  ret = xmlMalloc(len + 1);
1649
2.55M
  if (ret == NULL)
1650
0
      return(NULL);
1651
2.55M
    } else
1652
0
  ret = target;
1653
2.55M
    in = str;
1654
2.55M
    out = ret;
1655
65.2M
    while(len > 0) {
1656
62.7M
  if ((len > 2) && (*in == '%') && (is_hex(in[1])) && (is_hex(in[2]))) {
1657
86.5k
            int c = 0;
1658
86.5k
      in++;
1659
86.5k
      if ((*in >= '0') && (*in <= '9'))
1660
48.9k
          c = (*in - '0');
1661
37.5k
      else if ((*in >= 'a') && (*in <= 'f'))
1662
15.5k
          c = (*in - 'a') + 10;
1663
22.0k
      else if ((*in >= 'A') && (*in <= 'F'))
1664
22.0k
          c = (*in - 'A') + 10;
1665
86.5k
      in++;
1666
86.5k
      if ((*in >= '0') && (*in <= '9'))
1667
32.7k
          c = c * 16 + (*in - '0');
1668
53.8k
      else if ((*in >= 'a') && (*in <= 'f'))
1669
28.5k
          c = c * 16 + (*in - 'a') + 10;
1670
25.3k
      else if ((*in >= 'A') && (*in <= 'F'))
1671
25.3k
          c = c * 16 + (*in - 'A') + 10;
1672
86.5k
      in++;
1673
86.5k
      len -= 3;
1674
            /* Explicit sign change */
1675
86.5k
      *out++ = (char) c;
1676
62.6M
  } else {
1677
62.6M
      *out++ = *in++;
1678
62.6M
      len--;
1679
62.6M
  }
1680
62.7M
    }
1681
2.55M
    *out = 0;
1682
2.55M
    return(ret);
1683
2.55M
}
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
0
xmlURIEscapeStr(const xmlChar *str, const xmlChar *list) {
1698
0
    xmlChar *ret, ch;
1699
0
    const xmlChar *in;
1700
0
    int len, out;
1701
1702
0
    if (str == NULL)
1703
0
  return(NULL);
1704
0
    if (str[0] == 0)
1705
0
  return(xmlStrdup(str));
1706
0
    len = xmlStrlen(str);
1707
1708
0
    len += 20;
1709
0
    ret = xmlMalloc(len);
1710
0
    if (ret == NULL)
1711
0
  return(NULL);
1712
0
    in = (const xmlChar *) str;
1713
0
    out = 0;
1714
0
    while(*in != 0) {
1715
0
  if (len - out <= 3) {
1716
0
            xmlChar *temp;
1717
0
            int newSize;
1718
1719
0
            newSize = xmlGrowCapacity(len, 1, 1, XML_MAX_ITEMS);
1720
0
            if (newSize < 0) {
1721
0
    xmlFree(ret);
1722
0
                return(NULL);
1723
0
            }
1724
0
            temp = xmlRealloc(ret, newSize);
1725
0
      if (temp == NULL) {
1726
0
    xmlFree(ret);
1727
0
    return(NULL);
1728
0
      }
1729
0
      ret = temp;
1730
0
            len = newSize;
1731
0
  }
1732
1733
0
  ch = *in;
1734
1735
0
  if ((ch != '@') && (!IS_UNRESERVED(ch)) && (!xmlStrchr(list, ch))) {
1736
0
      unsigned char val;
1737
0
      ret[out++] = '%';
1738
0
      val = ch >> 4;
1739
0
      if (val <= 9)
1740
0
    ret[out++] = '0' + val;
1741
0
      else
1742
0
    ret[out++] = 'A' + val - 0xA;
1743
0
      val = ch & 0xF;
1744
0
      if (val <= 9)
1745
0
    ret[out++] = '0' + val;
1746
0
      else
1747
0
    ret[out++] = 'A' + val - 0xA;
1748
0
      in++;
1749
0
  } else {
1750
0
      ret[out++] = *in++;
1751
0
  }
1752
1753
0
    }
1754
0
    ret[out] = 0;
1755
0
    return(ret);
1756
0
}
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
0
xmlIsAbsolutePath(const xmlChar *path) {
1899
0
    int c = path[0];
1900
1901
0
    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
0
    return(0);
1912
0
}
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
0
xmlResolvePath(const xmlChar *escRef, const xmlChar *base, xmlChar **out) {
1927
0
    const xmlChar *fragment;
1928
0
    xmlChar *tmp = NULL;
1929
0
    xmlChar *ref = NULL;
1930
0
    xmlChar *result = NULL;
1931
0
    int ret = -1;
1932
0
    int i;
1933
1934
0
    if (out == NULL)
1935
0
        return(1);
1936
0
    *out = NULL;
1937
1938
0
    if ((escRef == NULL) || (escRef[0] == 0)) {
1939
0
        if ((base == NULL) || (base[0] == 0))
1940
0
            return(1);
1941
0
        ref = xmlStrdup(base);
1942
0
        if (ref == NULL)
1943
0
            goto err_memory;
1944
0
        *out = ref;
1945
0
        return(0);
1946
0
    }
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
0
    fragment = xmlStrchr(escRef, '#');
1954
0
    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
0
    ref = (xmlChar *) xmlURIUnescapeString((char *) escRef, -1, NULL);
1962
0
    if (ref == NULL)
1963
0
        goto err_memory;
1964
1965
0
    if ((base == NULL) || (base[0] == 0))
1966
0
        goto done;
1967
1968
0
    if (xmlIsAbsolutePath(ref))
1969
0
        goto done;
1970
1971
    /*
1972
     * Remove last segment from base
1973
     */
1974
0
    i = xmlStrlen(base);
1975
0
    while ((i > 0) && !xmlIsPathSeparator(base[i-1], 1))
1976
0
        i--;
1977
1978
    /*
1979
     * Concatenate base and ref
1980
     */
1981
0
    if (i > 0) {
1982
0
        int refLen = xmlStrlen(ref);
1983
1984
0
        result = xmlMalloc(i + refLen + 1);
1985
0
        if (result == NULL)
1986
0
            goto err_memory;
1987
1988
0
        memcpy(result, base, i);
1989
0
        memcpy(result + i, ref, refLen + 1);
1990
0
    }
1991
1992
    /*
1993
     * Normalize
1994
     */
1995
0
    xmlNormalizePath((char *) result, 1);
1996
1997
0
done:
1998
0
    if (result == NULL) {
1999
0
        result = ref;
2000
0
        ref = NULL;
2001
0
    }
2002
2003
0
    if (fragment != NULL) {
2004
0
        result = xmlStrcat(result, fragment);
2005
0
        if (result == NULL)
2006
0
            goto err_memory;
2007
0
    }
2008
2009
0
    *out = result;
2010
0
    ret = 0;
2011
2012
0
err_memory:
2013
0
    xmlFree(tmp);
2014
0
    xmlFree(ref);
2015
0
    return(ret);
2016
0
}
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
670
xmlBuildURISafe(const xmlChar *URI, const xmlChar *base, xmlChar **valPtr) {
2038
670
    xmlChar *val = NULL;
2039
670
    int ret, len, indx, cur, out;
2040
670
    xmlURIPtr ref = NULL;
2041
670
    xmlURIPtr bas = NULL;
2042
670
    xmlURIPtr res = NULL;
2043
2044
670
    if (valPtr == NULL)
2045
0
        return(1);
2046
670
    *valPtr = NULL;
2047
2048
670
    if (URI == NULL)
2049
0
        return(1);
2050
2051
670
    if (base == NULL) {
2052
670
        val = xmlStrdup(URI);
2053
670
        if (val == NULL)
2054
0
            return(-1);
2055
670
        *valPtr = val;
2056
670
        return(0);
2057
670
    }
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
0
    if (URI[0] != 0)
2068
0
        ret = xmlParseURISafe((const char *) URI, &ref);
2069
0
    else
2070
0
        ret = 0;
2071
0
    if (ret != 0)
2072
0
  goto done;
2073
0
    if ((ref != NULL) && (ref->scheme != NULL)) {
2074
  /*
2075
   * The URI is absolute don't modify.
2076
   */
2077
0
  val = xmlStrdup(URI);
2078
0
        if (val == NULL)
2079
0
            ret = -1;
2080
0
  goto done;
2081
0
    }
2082
2083
    /*
2084
     * If base has no scheme or authority, it is assumed to be a
2085
     * filesystem path.
2086
     */
2087
0
    if (xmlStrstr(base, BAD_CAST "://") == NULL) {
2088
0
        xmlFreeURI(ref);
2089
0
        return(xmlResolvePath(URI, base, valPtr));
2090
0
    }
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
0
    ret = xmlParseURISafe((const char *) base, &bas);
2110
0
    if (ret < 0)
2111
0
        goto done;
2112
0
    if (ret != 0) {
2113
0
  if (ref) {
2114
0
            ret = 0;
2115
0
      val = xmlSaveUri(ref);
2116
0
            if (val == NULL)
2117
0
                ret = -1;
2118
0
        }
2119
0
  goto done;
2120
0
    }
2121
0
    if (ref == NULL) {
2122
  /*
2123
   * the base fragment must be ignored
2124
   */
2125
0
  if (bas->fragment != NULL) {
2126
0
      xmlFree(bas->fragment);
2127
0
      bas->fragment = NULL;
2128
0
  }
2129
0
  val = xmlSaveUri(bas);
2130
0
        if (val == NULL)
2131
0
            ret = -1;
2132
0
  goto done;
2133
0
    }
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
0
    ret = -1;
2148
0
    res = xmlCreateURI();
2149
0
    if (res == NULL)
2150
0
  goto done;
2151
0
    if ((ref->scheme == NULL) && (ref->path == NULL) &&
2152
0
  ((ref->authority == NULL) && (ref->server == NULL) &&
2153
0
         (ref->port == PORT_EMPTY))) {
2154
0
  if (bas->scheme != NULL) {
2155
0
      res->scheme = xmlMemStrdup(bas->scheme);
2156
0
            if (res->scheme == NULL)
2157
0
                goto done;
2158
0
        }
2159
0
  if (bas->authority != NULL) {
2160
0
      res->authority = xmlMemStrdup(bas->authority);
2161
0
            if (res->authority == NULL)
2162
0
                goto done;
2163
0
        } else {
2164
0
      if (bas->server != NULL) {
2165
0
    res->server = xmlMemStrdup(bas->server);
2166
0
                if (res->server == NULL)
2167
0
                    goto done;
2168
0
            }
2169
0
      if (bas->user != NULL) {
2170
0
    res->user = xmlMemStrdup(bas->user);
2171
0
                if (res->user == NULL)
2172
0
                    goto done;
2173
0
            }
2174
0
      res->port = bas->port;
2175
0
  }
2176
0
  if (bas->path != NULL) {
2177
0
      res->path = xmlMemStrdup(bas->path);
2178
0
            if (res->path == NULL)
2179
0
                goto done;
2180
0
        }
2181
0
  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
0
        } else if (ref->query != NULL) {
2186
0
      res->query = xmlMemStrdup(ref->query);
2187
0
            if (res->query == NULL)
2188
0
                goto done;
2189
0
        } 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
0
        } 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
0
  if (ref->fragment != NULL) {
2199
0
      res->fragment = xmlMemStrdup(ref->fragment);
2200
0
            if (res->fragment == NULL)
2201
0
                goto done;
2202
0
        }
2203
0
  goto step_7;
2204
0
    }
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
0
    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
0
    if (bas->scheme != NULL) {
2219
0
  res->scheme = xmlMemStrdup(bas->scheme);
2220
0
        if (res->scheme == NULL)
2221
0
            goto done;
2222
0
    }
2223
2224
0
    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
0
    } 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
0
    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
0
    if ((ref->authority != NULL) || (ref->server != NULL) ||
2247
0
         (ref->port != PORT_EMPTY)) {
2248
0
  if (ref->authority != NULL) {
2249
0
      res->authority = xmlMemStrdup(ref->authority);
2250
0
            if (res->authority == NULL)
2251
0
                goto done;
2252
0
        } else {
2253
0
            if (ref->server != NULL) {
2254
0
                res->server = xmlMemStrdup(ref->server);
2255
0
                if (res->server == NULL)
2256
0
                    goto done;
2257
0
            }
2258
0
      if (ref->user != NULL) {
2259
0
    res->user = xmlMemStrdup(ref->user);
2260
0
                if (res->user == NULL)
2261
0
                    goto done;
2262
0
            }
2263
0
            res->port = ref->port;
2264
0
  }
2265
0
  if (ref->path != NULL) {
2266
0
      res->path = xmlMemStrdup(ref->path);
2267
0
            if (res->path == NULL)
2268
0
                goto done;
2269
0
        }
2270
0
  goto step_7;
2271
0
    }
2272
0
    if (bas->authority != NULL) {
2273
0
  res->authority = xmlMemStrdup(bas->authority);
2274
0
        if (res->authority == NULL)
2275
0
            goto done;
2276
0
    } else if ((bas->server != NULL) || (bas->port != PORT_EMPTY)) {
2277
0
  if (bas->server != NULL) {
2278
0
      res->server = xmlMemStrdup(bas->server);
2279
0
            if (res->server == NULL)
2280
0
                goto done;
2281
0
        }
2282
0
  if (bas->user != NULL) {
2283
0
      res->user = xmlMemStrdup(bas->user);
2284
0
            if (res->user == NULL)
2285
0
                goto done;
2286
0
        }
2287
0
  res->port = bas->port;
2288
0
    }
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
0
    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
0
    len = 2; /* extra / and 0 */
2311
0
    if (ref->path != NULL)
2312
0
  len += strlen(ref->path);
2313
0
    if (bas->path != NULL)
2314
0
  len += strlen(bas->path);
2315
0
    res->path = xmlMalloc(len);
2316
0
    if (res->path == NULL)
2317
0
  goto done;
2318
0
    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
0
    cur = 0;
2326
0
    out = 0;
2327
0
    if (bas->path != NULL) {
2328
0
  while (bas->path[cur] != 0) {
2329
0
      while ((bas->path[cur] != 0) && (bas->path[cur] != '/'))
2330
0
    cur++;
2331
0
      if (bas->path[cur] == 0)
2332
0
    break;
2333
2334
0
      cur++;
2335
0
      while (out < cur) {
2336
0
    res->path[out] = bas->path[out];
2337
0
    out++;
2338
0
      }
2339
0
  }
2340
0
    }
2341
0
    res->path[out] = 0;
2342
2343
    /*
2344
     * b) The reference's path component is appended to the buffer
2345
     *    string.
2346
     */
2347
0
    if (ref->path != NULL && ref->path[0] != 0) {
2348
0
  indx = 0;
2349
  /*
2350
   * Ensure the path includes a '/'
2351
   */
2352
0
  if ((out == 0) && ((bas->server != NULL) || bas->port != PORT_EMPTY))
2353
0
      res->path[out++] = '/';
2354
0
  while (ref->path[indx] != 0) {
2355
0
      res->path[out++] = ref->path[indx++];
2356
0
  }
2357
0
    }
2358
0
    res->path[out] = 0;
2359
2360
    /*
2361
     * Steps c) to h) are really path normalization steps
2362
     */
2363
0
    xmlNormalizeURIPath(res->path);
2364
2365
0
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
0
    val = xmlSaveUri(res);
2373
0
    if (val != NULL)
2374
0
        ret = 0;
2375
2376
0
done:
2377
0
    if (ref != NULL)
2378
0
  xmlFreeURI(ref);
2379
0
    if (bas != NULL)
2380
0
  xmlFreeURI(bas);
2381
0
    if (res != NULL)
2382
0
  xmlFreeURI(res);
2383
0
    *valPtr = val;
2384
0
    return(ret);
2385
0
}
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
0
xmlBuildURI(const xmlChar *URI, const xmlChar *base) {
2404
0
    xmlChar *out;
2405
2406
0
    xmlBuildURISafe(URI, base, &out);
2407
0
    return(out);
2408
0
}
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
}