Coverage Report

Created: 2024-02-28 06:15

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