Coverage Report

Created: 2025-10-10 06:40

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