Coverage Report

Created: 2024-09-06 07:53

/src/libxml2/xinclude.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * xinclude.c : Code to implement XInclude processing
3
 *
4
 * World Wide Web Consortium W3C Last Call Working Draft 10 November 2003
5
 * http://www.w3.org/TR/2003/WD-xinclude-20031110
6
 *
7
 * See Copyright for the status of this software.
8
 *
9
 * daniel@veillard.com
10
 */
11
12
#define IN_LIBXML
13
#include "libxml.h"
14
15
#include <string.h>
16
#include <libxml/xmlmemory.h>
17
#include <libxml/tree.h>
18
#include <libxml/parser.h>
19
#include <libxml/uri.h>
20
#include <libxml/xpath.h>
21
#include <libxml/xpointer.h>
22
#include <libxml/parserInternals.h>
23
#include <libxml/xmlerror.h>
24
#include <libxml/encoding.h>
25
26
#ifdef LIBXML_XINCLUDE_ENABLED
27
#include <libxml/xinclude.h>
28
29
#include "private/buf.h"
30
#include "private/error.h"
31
#include "private/parser.h"
32
#include "private/tree.h"
33
#include "private/xinclude.h"
34
35
0
#define XINCLUDE_MAX_DEPTH 40
36
37
/************************************************************************
38
 *                  *
39
 *      XInclude context handling     *
40
 *                  *
41
 ************************************************************************/
42
43
/*
44
 * An XInclude context
45
 */
46
typedef xmlChar *xmlURL;
47
48
typedef struct _xmlXIncludeRef xmlXIncludeRef;
49
typedef xmlXIncludeRef *xmlXIncludeRefPtr;
50
struct _xmlXIncludeRef {
51
    xmlChar              *URI; /* the fully resolved resource URL */
52
    xmlChar         *fragment; /* the fragment in the URI */
53
    xmlChar             *base; /* base URI of xi:include element */
54
    xmlNodePtr           elem; /* the xi:include element */
55
    xmlNodePtr            inc; /* the included copy */
56
    int                   xml; /* xml or txt */
57
    int              fallback; /* fallback was loaded */
58
    int       expanding; /* flag to detect inclusion loops */
59
    int         replace; /* should the node be replaced? */
60
};
61
62
typedef struct _xmlXIncludeDoc xmlXIncludeDoc;
63
typedef xmlXIncludeDoc *xmlXIncludeDocPtr;
64
struct _xmlXIncludeDoc {
65
    xmlDocPtr             doc; /* the parsed document */
66
    xmlChar              *url; /* the URL */
67
    int             expanding; /* flag to detect inclusion loops */
68
};
69
70
typedef struct _xmlXIncludeTxt xmlXIncludeTxt;
71
typedef xmlXIncludeTxt *xmlXIncludeTxtPtr;
72
struct _xmlXIncludeTxt {
73
    xmlChar   *text; /* text string */
74
    xmlChar              *url; /* the URL */
75
};
76
77
struct _xmlXIncludeCtxt {
78
    xmlDocPtr             doc; /* the source document */
79
    int                 incNr; /* number of includes */
80
    int                incMax; /* size of includes tab */
81
    xmlXIncludeRefPtr *incTab; /* array of included references */
82
83
    int                 txtNr; /* number of unparsed documents */
84
    int                txtMax; /* size of unparsed documents tab */
85
    xmlXIncludeTxt    *txtTab; /* array of unparsed documents */
86
87
    int                 urlNr; /* number of documents stacked */
88
    int                urlMax; /* size of document stack */
89
    xmlXIncludeDoc    *urlTab; /* document stack */
90
91
    int              nbErrors; /* the number of errors detected */
92
    int              fatalErr; /* abort processing */
93
    int                 errNo; /* error code */
94
    int                legacy; /* using XINCLUDE_OLD_NS */
95
    int            parseFlags; /* the flags used for parsing XML documents */
96
97
    void            *_private; /* application data */
98
99
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
100
    unsigned long    incTotal; /* total number of processed inclusions */
101
#endif
102
    int     depth; /* recursion depth */
103
    int        isStream; /* streaming mode */
104
105
    xmlXPathContextPtr xpctxt;
106
107
    xmlStructuredErrorFunc errorHandler;
108
    void *errorCtxt;
109
110
    xmlResourceLoader resourceLoader;
111
    void *resourceCtxt;
112
};
113
114
static xmlXIncludeRefPtr
115
xmlXIncludeExpandNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node);
116
117
static int
118
xmlXIncludeLoadNode(xmlXIncludeCtxtPtr ctxt, xmlXIncludeRefPtr ref);
119
120
static int
121
xmlXIncludeDoProcess(xmlXIncludeCtxtPtr ctxt, xmlNodePtr tree);
122
123
124
/************************************************************************
125
 *                  *
126
 *      XInclude error handler        *
127
 *                  *
128
 ************************************************************************/
129
130
/**
131
 * xmlXIncludeErrMemory:
132
 * @extra:  extra information
133
 *
134
 * Handle an out of memory condition
135
 */
136
static void
137
xmlXIncludeErrMemory(xmlXIncludeCtxtPtr ctxt)
138
0
{
139
0
    ctxt->errNo = XML_ERR_NO_MEMORY;
140
0
    ctxt->fatalErr = 1;
141
0
    ctxt->nbErrors++;
142
143
0
    xmlRaiseMemoryError(ctxt->errorHandler, NULL, ctxt->errorCtxt,
144
0
                        XML_FROM_XINCLUDE, NULL);
145
0
}
146
147
/**
148
 * xmlXIncludeErr:
149
 * @ctxt: the XInclude context
150
 * @node: the context node
151
 * @msg:  the error message
152
 * @extra:  extra information
153
 *
154
 * Handle an XInclude error
155
 */
156
static void LIBXML_ATTR_FORMAT(4,0)
157
xmlXIncludeErr(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node, int error,
158
               const char *msg, const xmlChar *extra)
159
0
{
160
0
    xmlStructuredErrorFunc schannel = NULL;
161
0
    xmlGenericErrorFunc channel = NULL;
162
0
    void *data = NULL;
163
0
    int res;
164
165
0
    if (ctxt->fatalErr != 0)
166
0
        return;
167
0
    ctxt->nbErrors++;
168
169
0
    schannel = ctxt->errorHandler;
170
0
    data = ctxt->errorCtxt;
171
172
0
    if (schannel == NULL) {
173
0
        channel = xmlGenericError;
174
0
        data = xmlGenericErrorContext;
175
0
    }
176
177
0
    res = xmlRaiseError(schannel, channel, data, ctxt, node,
178
0
                        XML_FROM_XINCLUDE, error, XML_ERR_ERROR,
179
0
                        NULL, 0, (const char *) extra, NULL, NULL, 0, 0,
180
0
                        msg, (const char *) extra);
181
0
    if (res < 0) {
182
0
        ctxt->errNo = XML_ERR_NO_MEMORY;
183
0
        ctxt->fatalErr = 1;
184
0
    } else {
185
0
        ctxt->errNo = error;
186
0
    }
187
0
}
188
189
/**
190
 * xmlXIncludeGetProp:
191
 * @ctxt:  the XInclude context
192
 * @cur:  the node
193
 * @name:  the attribute name
194
 *
195
 * Get an XInclude attribute
196
 *
197
 * Returns the value (to be freed) or NULL if not found
198
 */
199
static xmlChar *
200
xmlXIncludeGetProp(xmlXIncludeCtxtPtr ctxt, xmlNodePtr cur,
201
0
                   const xmlChar *name) {
202
0
    xmlChar *ret;
203
204
0
    if (xmlNodeGetAttrValue(cur, name, XINCLUDE_NS, &ret) < 0)
205
0
        xmlXIncludeErrMemory(ctxt);
206
0
    if (ret != NULL)
207
0
        return(ret);
208
209
0
    if (ctxt->legacy != 0) {
210
0
        if (xmlNodeGetAttrValue(cur, name, XINCLUDE_OLD_NS, &ret) < 0)
211
0
            xmlXIncludeErrMemory(ctxt);
212
0
        if (ret != NULL)
213
0
            return(ret);
214
0
    }
215
216
0
    if (xmlNodeGetAttrValue(cur, name, NULL, &ret) < 0)
217
0
        xmlXIncludeErrMemory(ctxt);
218
0
    return(ret);
219
0
}
220
/**
221
 * xmlXIncludeFreeRef:
222
 * @ref: the XInclude reference
223
 *
224
 * Free an XInclude reference
225
 */
226
static void
227
0
xmlXIncludeFreeRef(xmlXIncludeRefPtr ref) {
228
0
    if (ref == NULL)
229
0
  return;
230
0
    if (ref->URI != NULL)
231
0
  xmlFree(ref->URI);
232
0
    if (ref->fragment != NULL)
233
0
  xmlFree(ref->fragment);
234
0
    if (ref->base != NULL)
235
0
  xmlFree(ref->base);
236
0
    xmlFree(ref);
237
0
}
238
239
/**
240
 * xmlXIncludeNewContext:
241
 * @doc:  an XML Document
242
 *
243
 * Creates a new XInclude context
244
 *
245
 * Returns the new set
246
 */
247
xmlXIncludeCtxtPtr
248
0
xmlXIncludeNewContext(xmlDocPtr doc) {
249
0
    xmlXIncludeCtxtPtr ret;
250
251
0
    if (doc == NULL)
252
0
  return(NULL);
253
0
    ret = (xmlXIncludeCtxtPtr) xmlMalloc(sizeof(xmlXIncludeCtxt));
254
0
    if (ret == NULL)
255
0
  return(NULL);
256
0
    memset(ret, 0, sizeof(xmlXIncludeCtxt));
257
0
    ret->doc = doc;
258
0
    ret->incNr = 0;
259
0
    ret->incMax = 0;
260
0
    ret->incTab = NULL;
261
0
    ret->nbErrors = 0;
262
0
    return(ret);
263
0
}
264
265
/**
266
 * xmlXIncludeFreeContext:
267
 * @ctxt: the XInclude context
268
 *
269
 * Free an XInclude context
270
 */
271
void
272
0
xmlXIncludeFreeContext(xmlXIncludeCtxtPtr ctxt) {
273
0
    int i;
274
275
0
    if (ctxt == NULL)
276
0
  return;
277
0
    if (ctxt->urlTab != NULL) {
278
0
  for (i = 0; i < ctxt->urlNr; i++) {
279
0
      xmlFreeDoc(ctxt->urlTab[i].doc);
280
0
      xmlFree(ctxt->urlTab[i].url);
281
0
  }
282
0
  xmlFree(ctxt->urlTab);
283
0
    }
284
0
    for (i = 0;i < ctxt->incNr;i++) {
285
0
  if (ctxt->incTab[i] != NULL)
286
0
      xmlXIncludeFreeRef(ctxt->incTab[i]);
287
0
    }
288
0
    if (ctxt->incTab != NULL)
289
0
  xmlFree(ctxt->incTab);
290
0
    if (ctxt->txtTab != NULL) {
291
0
  for (i = 0;i < ctxt->txtNr;i++) {
292
0
      xmlFree(ctxt->txtTab[i].text);
293
0
      xmlFree(ctxt->txtTab[i].url);
294
0
  }
295
0
  xmlFree(ctxt->txtTab);
296
0
    }
297
0
    if (ctxt->xpctxt != NULL)
298
0
  xmlXPathFreeContext(ctxt->xpctxt);
299
0
    xmlFree(ctxt);
300
0
}
301
302
/**
303
 * xmlXIncludeParseFile:
304
 * @ctxt:  the XInclude context
305
 * @URL:  the URL or file path
306
 *
307
 * parse a document for XInclude
308
 */
309
static xmlDocPtr
310
0
xmlXIncludeParseFile(xmlXIncludeCtxtPtr ctxt, const char *URL) {
311
0
    xmlDocPtr ret = NULL;
312
0
    xmlParserCtxtPtr pctxt;
313
0
    xmlParserInputPtr inputStream;
314
315
0
    xmlInitParser();
316
317
0
    pctxt = xmlNewParserCtxt();
318
0
    if (pctxt == NULL) {
319
0
  xmlXIncludeErrMemory(ctxt);
320
0
  return(NULL);
321
0
    }
322
0
    if (ctxt->errorHandler != NULL)
323
0
        xmlCtxtSetErrorHandler(pctxt, ctxt->errorHandler, ctxt->errorCtxt);
324
0
    if (ctxt->resourceLoader != NULL)
325
0
        xmlCtxtSetResourceLoader(pctxt, ctxt->resourceLoader,
326
0
                                 ctxt->resourceCtxt);
327
328
    /*
329
     * pass in the application data to the parser context.
330
     */
331
0
    pctxt->_private = ctxt->_private;
332
333
    /*
334
     * try to ensure that new documents included are actually
335
     * built with the same dictionary as the including document.
336
     */
337
0
    if ((ctxt->doc != NULL) && (ctxt->doc->dict != NULL)) {
338
0
       if (pctxt->dict != NULL)
339
0
            xmlDictFree(pctxt->dict);
340
0
  pctxt->dict = ctxt->doc->dict;
341
0
  xmlDictReference(pctxt->dict);
342
0
    }
343
344
    /*
345
     * We set DTDLOAD to make sure that ID attributes declared in
346
     * external DTDs are detected.
347
     */
348
0
    xmlCtxtUseOptions(pctxt, ctxt->parseFlags | XML_PARSE_DTDLOAD);
349
350
0
    inputStream = xmlLoadResource(pctxt, URL, NULL, XML_RESOURCE_XINCLUDE);
351
0
    if (inputStream == NULL)
352
0
        goto error;
353
354
0
    if (inputPush(pctxt, inputStream) < 0) {
355
0
        xmlFreeInputStream(inputStream);
356
0
        goto error;
357
0
    }
358
359
0
    xmlParseDocument(pctxt);
360
361
0
    if (pctxt->wellFormed) {
362
0
        ret = pctxt->myDoc;
363
0
    }
364
0
    else {
365
0
        ret = NULL;
366
0
  if (pctxt->myDoc != NULL)
367
0
      xmlFreeDoc(pctxt->myDoc);
368
0
        pctxt->myDoc = NULL;
369
0
    }
370
371
0
error:
372
0
    if (pctxt->errNo == XML_ERR_NO_MEMORY)
373
0
        xmlXIncludeErrMemory(ctxt);
374
0
    xmlFreeParserCtxt(pctxt);
375
376
0
    return(ret);
377
0
}
378
379
/**
380
 * xmlXIncludeAddNode:
381
 * @ctxt:  the XInclude context
382
 * @cur:  the new node
383
 *
384
 * Add a new node to process to an XInclude context
385
 */
386
static xmlXIncludeRefPtr
387
0
xmlXIncludeAddNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr cur) {
388
0
    xmlXIncludeRefPtr ref = NULL;
389
0
    xmlXIncludeRefPtr ret = NULL;
390
0
    xmlURIPtr uri = NULL;
391
0
    xmlChar *href = NULL;
392
0
    xmlChar *parse = NULL;
393
0
    xmlChar *fragment = NULL;
394
0
    xmlChar *base = NULL;
395
0
    xmlChar *tmp;
396
0
    int xml = 1;
397
0
    int local = 0;
398
0
    int res;
399
400
0
    if (ctxt == NULL)
401
0
  return(NULL);
402
0
    if (cur == NULL)
403
0
  return(NULL);
404
405
    /*
406
     * read the attributes
407
     */
408
409
0
    fragment = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_PARSE_XPOINTER);
410
411
0
    href = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_HREF);
412
0
    if (href == NULL) {
413
0
        if (fragment == NULL) {
414
0
      xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_NO_HREF,
415
0
                     "href or xpointer must be present\n", parse);
416
0
      goto error;
417
0
        }
418
419
0
  href = xmlStrdup(BAD_CAST ""); /* @@@@ href is now optional */
420
0
  if (href == NULL) {
421
0
            xmlXIncludeErrMemory(ctxt);
422
0
      goto error;
423
0
        }
424
0
    } else if (xmlStrlen(href) > XML_MAX_URI_LENGTH) {
425
0
        xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_HREF_URI, "URI too long\n",
426
0
                       NULL);
427
0
        goto error;
428
0
    }
429
430
0
    parse = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_PARSE);
431
0
    if (parse != NULL) {
432
0
  if (xmlStrEqual(parse, XINCLUDE_PARSE_XML))
433
0
      xml = 1;
434
0
  else if (xmlStrEqual(parse, XINCLUDE_PARSE_TEXT))
435
0
      xml = 0;
436
0
  else {
437
0
      xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_PARSE_VALUE,
438
0
                     "invalid value %s for 'parse'\n", parse);
439
0
      goto error;
440
0
  }
441
0
    }
442
443
    /*
444
     * Check the URL and remove any fragment identifier
445
     */
446
0
    res = xmlParseURISafe((const char *)href, &uri);
447
0
    if (uri == NULL) {
448
0
        if (res < 0)
449
0
            xmlXIncludeErrMemory(ctxt);
450
0
        else
451
0
            xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_HREF_URI,
452
0
                           "invalid value href %s\n", href);
453
0
        goto error;
454
0
    }
455
456
0
    if (uri->fragment != NULL) {
457
0
        if (ctxt->legacy != 0) {
458
0
      if (fragment == NULL) {
459
0
    fragment = (xmlChar *) uri->fragment;
460
0
      } else {
461
0
    xmlFree(uri->fragment);
462
0
      }
463
0
  } else {
464
0
      xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_FRAGMENT_ID,
465
0
       "Invalid fragment identifier in URI %s use the xpointer attribute\n",
466
0
                           href);
467
0
      goto error;
468
0
  }
469
0
  uri->fragment = NULL;
470
0
    }
471
0
    tmp = xmlSaveUri(uri);
472
0
    if (tmp == NULL) {
473
0
  xmlXIncludeErrMemory(ctxt);
474
0
  goto error;
475
0
    }
476
0
    xmlFree(href);
477
0
    href = tmp;
478
479
    /*
480
     * Resolve URI
481
     */
482
483
0
    if (xmlNodeGetBaseSafe(ctxt->doc, cur, &base) < 0) {
484
0
        xmlXIncludeErrMemory(ctxt);
485
0
        goto error;
486
0
    }
487
488
0
    if (href[0] != 0) {
489
0
        if (xmlBuildURISafe(href, base, &tmp) < 0) {
490
0
            xmlXIncludeErrMemory(ctxt);
491
0
            goto error;
492
0
        }
493
0
        if (tmp == NULL) {
494
0
            xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_HREF_URI,
495
0
                           "failed build URL\n", NULL);
496
0
            goto error;
497
0
        }
498
0
        xmlFree(href);
499
0
        href = tmp;
500
501
0
        if (xmlStrEqual(href, ctxt->doc->URL))
502
0
            local = 1;
503
0
    } else {
504
0
        local = 1;
505
0
    }
506
507
    /*
508
     * If local and xml then we need a fragment
509
     */
510
0
    if ((local == 1) && (xml == 1) &&
511
0
        ((fragment == NULL) || (fragment[0] == 0))) {
512
0
  xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_RECURSION,
513
0
                 "detected a local recursion with no xpointer in %s\n",
514
0
           href);
515
0
  goto error;
516
0
    }
517
518
0
    ref = (xmlXIncludeRefPtr) xmlMalloc(sizeof(xmlXIncludeRef));
519
0
    if (ref == NULL) {
520
0
        xmlXIncludeErrMemory(ctxt);
521
0
        goto error;
522
0
    }
523
0
    memset(ref, 0, sizeof(xmlXIncludeRef));
524
525
0
    ref->elem = cur;
526
0
    ref->xml = xml;
527
0
    ref->URI = href;
528
0
    href = NULL;
529
0
    ref->fragment = fragment;
530
0
    fragment = NULL;
531
532
    /*
533
     * xml:base fixup
534
     */
535
0
    if (((ctxt->parseFlags & XML_PARSE_NOBASEFIX) == 0) &&
536
0
        (cur->doc != NULL) &&
537
0
        ((cur->doc->parseFlags & XML_PARSE_NOBASEFIX) == 0)) {
538
0
        if (base != NULL) {
539
0
            ref->base = base;
540
0
            base = NULL;
541
0
        } else {
542
0
            ref->base = xmlStrdup(BAD_CAST "");
543
0
            if (ref->base == NULL) {
544
0
          xmlXIncludeErrMemory(ctxt);
545
0
                goto error;
546
0
            }
547
0
        }
548
0
    }
549
550
0
    if (ctxt->incNr >= ctxt->incMax) {
551
0
        xmlXIncludeRefPtr *table;
552
0
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
553
0
        size_t newSize = ctxt->incMax ? ctxt->incMax * 2 : 1;
554
#else
555
        size_t newSize = ctxt->incMax ? ctxt->incMax * 2 : 4;
556
#endif
557
558
0
        table = (xmlXIncludeRefPtr *) xmlRealloc(ctxt->incTab,
559
0
               newSize * sizeof(ctxt->incTab[0]));
560
0
        if (table == NULL) {
561
0
      xmlXIncludeErrMemory(ctxt);
562
0
      goto error;
563
0
  }
564
0
        ctxt->incTab = table;
565
0
        ctxt->incMax = newSize;
566
0
    }
567
0
    ctxt->incTab[ctxt->incNr++] = ref;
568
569
0
    ret = ref;
570
0
    ref = NULL;
571
572
0
error:
573
0
    xmlXIncludeFreeRef(ref);
574
0
    xmlFreeURI(uri);
575
0
    xmlFree(href);
576
0
    xmlFree(parse);
577
0
    xmlFree(fragment);
578
0
    xmlFree(base);
579
0
    return(ret);
580
0
}
581
582
/**
583
 * xmlXIncludeRecurseDoc:
584
 * @ctxt:  the XInclude context
585
 * @doc:  the new document
586
 * @url:  the associated URL
587
 *
588
 * The XInclude recursive nature is handled at this point.
589
 */
590
static void
591
0
xmlXIncludeRecurseDoc(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc) {
592
0
    xmlDocPtr oldDoc;
593
0
    xmlXIncludeRefPtr *oldIncTab;
594
0
    int oldIncMax, oldIncNr, oldIsStream;
595
0
    int i;
596
597
0
    oldDoc = ctxt->doc;
598
0
    oldIncMax = ctxt->incMax;
599
0
    oldIncNr = ctxt->incNr;
600
0
    oldIncTab = ctxt->incTab;
601
0
    oldIsStream = ctxt->isStream;
602
0
    ctxt->doc = doc;
603
0
    ctxt->incMax = 0;
604
0
    ctxt->incNr = 0;
605
0
    ctxt->incTab = NULL;
606
0
    ctxt->isStream = 0;
607
608
0
    xmlXIncludeDoProcess(ctxt, xmlDocGetRootElement(doc));
609
610
0
    if (ctxt->incTab != NULL) {
611
0
        for (i = 0; i < ctxt->incNr; i++)
612
0
            xmlXIncludeFreeRef(ctxt->incTab[i]);
613
0
        xmlFree(ctxt->incTab);
614
0
    }
615
616
0
    ctxt->doc = oldDoc;
617
0
    ctxt->incMax = oldIncMax;
618
0
    ctxt->incNr = oldIncNr;
619
0
    ctxt->incTab = oldIncTab;
620
0
    ctxt->isStream = oldIsStream;
621
0
}
622
623
/************************************************************************
624
 *                  *
625
 *      Node copy with specific semantic    *
626
 *                  *
627
 ************************************************************************/
628
629
static void
630
xmlXIncludeBaseFixup(xmlXIncludeCtxtPtr ctxt, xmlNodePtr cur, xmlNodePtr copy,
631
0
                     const xmlChar *targetBase) {
632
0
    xmlChar *base = NULL;
633
0
    xmlChar *relBase = NULL;
634
0
    xmlNs ns;
635
0
    int res;
636
637
0
    if (cur->type != XML_ELEMENT_NODE)
638
0
        return;
639
640
0
    if (xmlNodeGetBaseSafe(cur->doc, cur, &base) < 0)
641
0
        xmlXIncludeErrMemory(ctxt);
642
643
0
    if ((base != NULL) && !xmlStrEqual(base, targetBase)) {
644
0
        if ((xmlStrlen(base) > XML_MAX_URI_LENGTH) ||
645
0
            (xmlStrlen(targetBase) > XML_MAX_URI_LENGTH)) {
646
0
            relBase = xmlStrdup(base);
647
0
            if (relBase == NULL) {
648
0
                xmlXIncludeErrMemory(ctxt);
649
0
                goto done;
650
0
            }
651
0
        } else if (xmlBuildRelativeURISafe(base, targetBase, &relBase) < 0) {
652
0
            xmlXIncludeErrMemory(ctxt);
653
0
            goto done;
654
0
        }
655
0
        if (relBase == NULL) {
656
0
            xmlXIncludeErr(ctxt, cur,
657
0
                    XML_XINCLUDE_HREF_URI,
658
0
                    "Building relative URI failed: %s\n",
659
0
                    base);
660
0
            goto done;
661
0
        }
662
663
        /*
664
         * If the new base doesn't contain a slash, it can be omitted.
665
         */
666
0
        if (xmlStrchr(relBase, '/') != NULL) {
667
0
            res = xmlNodeSetBase(copy, relBase);
668
0
            if (res < 0)
669
0
                xmlXIncludeErrMemory(ctxt);
670
0
            goto done;
671
0
        }
672
0
    }
673
674
    /*
675
     * Delete existing xml:base if bases are equal
676
     */
677
0
    memset(&ns, 0, sizeof(ns));
678
0
    ns.href = XML_XML_NAMESPACE;
679
0
    xmlUnsetNsProp(copy, &ns, BAD_CAST "base");
680
681
0
done:
682
0
    xmlFree(base);
683
0
    xmlFree(relBase);
684
0
}
685
686
/**
687
 * xmlXIncludeCopyNode:
688
 * @ctxt:  the XInclude context
689
 * @elem:  the element
690
 * @copyChildren:  copy children instead of node if true
691
 *
692
 * Make a copy of the node while expanding nested XIncludes.
693
 *
694
 * Returns a node list, not a single node.
695
 */
696
static xmlNodePtr
697
xmlXIncludeCopyNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr elem,
698
0
                    int copyChildren, const xmlChar *targetBase) {
699
0
    xmlNodePtr result = NULL;
700
0
    xmlNodePtr insertParent = NULL;
701
0
    xmlNodePtr insertLast = NULL;
702
0
    xmlNodePtr cur;
703
0
    xmlNodePtr item;
704
0
    int depth = 0;
705
706
0
    if (copyChildren) {
707
0
        cur = elem->children;
708
0
        if (cur == NULL)
709
0
            return(NULL);
710
0
    } else {
711
0
        cur = elem;
712
0
    }
713
714
0
    while (1) {
715
0
        xmlNodePtr copy = NULL;
716
0
        int recurse = 0;
717
718
0
        if ((cur->type == XML_DOCUMENT_NODE) ||
719
0
            (cur->type == XML_DTD_NODE)) {
720
0
            ;
721
0
        } else if ((cur->type == XML_ELEMENT_NODE) &&
722
0
                   (cur->ns != NULL) &&
723
0
                   (xmlStrEqual(cur->name, XINCLUDE_NODE)) &&
724
0
                   ((xmlStrEqual(cur->ns->href, XINCLUDE_NS)) ||
725
0
                    (xmlStrEqual(cur->ns->href, XINCLUDE_OLD_NS)))) {
726
0
            xmlXIncludeRefPtr ref = xmlXIncludeExpandNode(ctxt, cur);
727
728
0
            if (ref == NULL)
729
0
                goto error;
730
            /*
731
             * TODO: Insert XML_XINCLUDE_START and XML_XINCLUDE_END nodes
732
             */
733
0
            for (item = ref->inc; item != NULL; item = item->next) {
734
0
                copy = xmlStaticCopyNode(item, ctxt->doc, insertParent, 1);
735
0
                if (copy == NULL) {
736
0
                    xmlXIncludeErrMemory(ctxt);
737
0
                    goto error;
738
0
                }
739
740
0
                if (result == NULL)
741
0
                    result = copy;
742
0
                if (insertLast != NULL) {
743
0
                    insertLast->next = copy;
744
0
                    copy->prev = insertLast;
745
0
                } else if (insertParent != NULL) {
746
0
                    insertParent->children = copy;
747
0
                }
748
0
                insertLast = copy;
749
750
0
                if ((depth == 0) && (targetBase != NULL))
751
0
                    xmlXIncludeBaseFixup(ctxt, item, copy, targetBase);
752
0
            }
753
0
        } else {
754
0
            copy = xmlStaticCopyNode(cur, ctxt->doc, insertParent, 2);
755
0
            if (copy == NULL) {
756
0
                xmlXIncludeErrMemory(ctxt);
757
0
                goto error;
758
0
            }
759
760
0
            if (result == NULL)
761
0
                result = copy;
762
0
            if (insertLast != NULL) {
763
0
                insertLast->next = copy;
764
0
                copy->prev = insertLast;
765
0
            } else if (insertParent != NULL) {
766
0
                insertParent->children = copy;
767
0
            }
768
0
            insertLast = copy;
769
770
0
            if ((depth == 0) && (targetBase != NULL))
771
0
                xmlXIncludeBaseFixup(ctxt, cur, copy, targetBase);
772
773
0
            recurse = (cur->type != XML_ENTITY_REF_NODE) &&
774
0
                      (cur->children != NULL);
775
0
        }
776
777
0
        if (recurse) {
778
0
            cur = cur->children;
779
0
            insertParent = insertLast;
780
0
            insertLast = NULL;
781
0
            depth += 1;
782
0
            continue;
783
0
        }
784
785
0
        if (cur == elem)
786
0
            return(result);
787
788
0
        while (cur->next == NULL) {
789
0
            if (insertParent != NULL)
790
0
                insertParent->last = insertLast;
791
0
            cur = cur->parent;
792
0
            if (cur == elem)
793
0
                return(result);
794
0
            insertLast = insertParent;
795
0
            insertParent = insertParent->parent;
796
0
            depth -= 1;
797
0
        }
798
799
0
        cur = cur->next;
800
0
    }
801
802
0
error:
803
0
    xmlFreeNodeList(result);
804
0
    return(NULL);
805
0
}
806
807
#ifdef LIBXML_XPTR_ENABLED
808
/**
809
 * xmlXIncludeCopyXPointer:
810
 * @ctxt:  the XInclude context
811
 * @obj:  the XPointer result from the evaluation.
812
 *
813
 * Build a node list tree copy of the XPointer result.
814
 * This will drop Attributes and Namespace declarations.
815
 *
816
 * Returns an xmlNodePtr list or NULL.
817
 *         the caller has to free the node tree.
818
 */
819
static xmlNodePtr
820
xmlXIncludeCopyXPointer(xmlXIncludeCtxtPtr ctxt, xmlXPathObjectPtr obj,
821
0
                        const xmlChar *targetBase) {
822
0
    xmlNodePtr list = NULL, last = NULL, copy;
823
0
    int i;
824
825
0
    if ((ctxt == NULL) || (obj == NULL))
826
0
  return(NULL);
827
0
    switch (obj->type) {
828
0
        case XPATH_NODESET: {
829
0
      xmlNodeSetPtr set = obj->nodesetval;
830
0
      if (set == NULL)
831
0
    break;
832
0
      for (i = 0;i < set->nodeNr;i++) {
833
0
                xmlNodePtr node;
834
835
0
    if (set->nodeTab[i] == NULL)
836
0
        continue;
837
0
    switch (set->nodeTab[i]->type) {
838
0
        case XML_DOCUMENT_NODE:
839
0
        case XML_HTML_DOCUMENT_NODE:
840
0
                        node = xmlDocGetRootElement(
841
0
                                (xmlDocPtr) set->nodeTab[i]);
842
0
                        if (node == NULL) {
843
0
                            xmlXIncludeErr(ctxt, set->nodeTab[i],
844
0
                                           XML_ERR_INTERNAL_ERROR,
845
0
                                          "document without root\n", NULL);
846
0
                            continue;
847
0
                        }
848
0
                        break;
849
0
                    case XML_TEXT_NODE:
850
0
        case XML_CDATA_SECTION_NODE:
851
0
        case XML_ELEMENT_NODE:
852
0
        case XML_PI_NODE:
853
0
        case XML_COMMENT_NODE:
854
0
                        node = set->nodeTab[i];
855
0
      break;
856
0
                    default:
857
0
                        xmlXIncludeErr(ctxt, set->nodeTab[i],
858
0
                                       XML_XINCLUDE_XPTR_RESULT,
859
0
                                       "invalid node type in XPtr result\n",
860
0
                                       NULL);
861
0
      continue; /* for */
862
0
    }
863
                /*
864
                 * OPTIMIZE TODO: External documents should already be
865
                 * expanded, so xmlDocCopyNode should work as well.
866
                 * xmlXIncludeCopyNode is only required for the initial
867
                 * document.
868
                 */
869
0
    copy = xmlXIncludeCopyNode(ctxt, node, 0, targetBase);
870
0
                if (copy == NULL) {
871
0
                    xmlFreeNodeList(list);
872
0
                    return(NULL);
873
0
                }
874
0
    if (last == NULL) {
875
0
                    list = copy;
876
0
                } else {
877
0
                    while (last->next != NULL)
878
0
                        last = last->next;
879
0
                    copy->prev = last;
880
0
                    last->next = copy;
881
0
    }
882
0
                last = copy;
883
0
      }
884
0
      break;
885
0
  }
886
0
  default:
887
0
      break;
888
0
    }
889
0
    return(list);
890
0
}
891
#endif
892
893
/************************************************************************
894
 *                  *
895
 *      XInclude I/O handling       *
896
 *                  *
897
 ************************************************************************/
898
899
typedef struct _xmlXIncludeMergeData xmlXIncludeMergeData;
900
typedef xmlXIncludeMergeData *xmlXIncludeMergeDataPtr;
901
struct _xmlXIncludeMergeData {
902
    xmlDocPtr doc;
903
    xmlXIncludeCtxtPtr ctxt;
904
};
905
906
/**
907
 * xmlXIncludeMergeOneEntity:
908
 * @ent: the entity
909
 * @doc:  the including doc
910
 * @name: the entity name
911
 *
912
 * Implements the merge of one entity
913
 */
914
static void
915
xmlXIncludeMergeEntity(void *payload, void *vdata,
916
0
                 const xmlChar *name ATTRIBUTE_UNUSED) {
917
0
    xmlEntityPtr ent = (xmlEntityPtr) payload;
918
0
    xmlXIncludeMergeDataPtr data = (xmlXIncludeMergeDataPtr) vdata;
919
0
    xmlEntityPtr ret, prev;
920
0
    xmlDocPtr doc;
921
0
    xmlXIncludeCtxtPtr ctxt;
922
923
0
    if ((ent == NULL) || (data == NULL))
924
0
  return;
925
0
    ctxt = data->ctxt;
926
0
    doc = data->doc;
927
0
    if ((ctxt == NULL) || (doc == NULL))
928
0
  return;
929
0
    switch (ent->etype) {
930
0
        case XML_INTERNAL_PARAMETER_ENTITY:
931
0
        case XML_EXTERNAL_PARAMETER_ENTITY:
932
0
        case XML_INTERNAL_PREDEFINED_ENTITY:
933
0
      return;
934
0
        case XML_INTERNAL_GENERAL_ENTITY:
935
0
        case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
936
0
        case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
937
0
      break;
938
0
    }
939
0
    prev = xmlGetDocEntity(doc, ent->name);
940
0
    if (prev == NULL) {
941
0
        ret = xmlAddDocEntity(doc, ent->name, ent->etype, ent->ExternalID,
942
0
                              ent->SystemID, ent->content);
943
0
        if (ret == NULL) {
944
0
            xmlXIncludeErrMemory(ctxt);
945
0
            return;
946
0
        }
947
0
  if (ent->URI != NULL) {
948
0
      ret->URI = xmlStrdup(ent->URI);
949
0
            if (ret->URI == 0)
950
0
                xmlXIncludeErrMemory(ctxt);
951
0
        }
952
0
    } else {
953
0
        if (ent->etype != prev->etype)
954
0
            goto error;
955
956
0
        if ((ent->SystemID != NULL) && (prev->SystemID != NULL)) {
957
0
            if (!xmlStrEqual(ent->SystemID, prev->SystemID))
958
0
                goto error;
959
0
        } else if ((ent->ExternalID != NULL) &&
960
0
                   (prev->ExternalID != NULL)) {
961
0
            if (!xmlStrEqual(ent->ExternalID, prev->ExternalID))
962
0
                goto error;
963
0
        } else if ((ent->content != NULL) && (prev->content != NULL)) {
964
0
            if (!xmlStrEqual(ent->content, prev->content))
965
0
                goto error;
966
0
        } else {
967
0
            goto error;
968
0
        }
969
0
    }
970
0
    return;
971
0
error:
972
0
    switch (ent->etype) {
973
0
        case XML_INTERNAL_PARAMETER_ENTITY:
974
0
        case XML_EXTERNAL_PARAMETER_ENTITY:
975
0
        case XML_INTERNAL_PREDEFINED_ENTITY:
976
0
        case XML_INTERNAL_GENERAL_ENTITY:
977
0
        case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
978
0
      return;
979
0
        case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
980
0
      break;
981
0
    }
982
0
    xmlXIncludeErr(ctxt, (xmlNodePtr) ent, XML_XINCLUDE_ENTITY_DEF_MISMATCH,
983
0
                   "mismatch in redefinition of entity %s\n",
984
0
       ent->name);
985
0
}
986
987
/**
988
 * xmlXIncludeMergeEntities:
989
 * @ctxt: an XInclude context
990
 * @doc:  the including doc
991
 * @from:  the included doc
992
 *
993
 * Implements the entity merge
994
 *
995
 * Returns 0 if merge succeeded, -1 if some processing failed
996
 */
997
static int
998
xmlXIncludeMergeEntities(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc,
999
0
                   xmlDocPtr from) {
1000
0
    xmlNodePtr cur;
1001
0
    xmlDtdPtr target, source;
1002
1003
0
    if (ctxt == NULL)
1004
0
  return(-1);
1005
1006
0
    if ((from == NULL) || (from->intSubset == NULL))
1007
0
  return(0);
1008
1009
0
    target = doc->intSubset;
1010
0
    if (target == NULL) {
1011
0
  cur = xmlDocGetRootElement(doc);
1012
0
  if (cur == NULL)
1013
0
      return(-1);
1014
0
        target = xmlCreateIntSubset(doc, cur->name, NULL, NULL);
1015
0
  if (target == NULL) {
1016
0
            xmlXIncludeErrMemory(ctxt);
1017
0
      return(-1);
1018
0
        }
1019
0
    }
1020
1021
0
    source = from->intSubset;
1022
0
    if ((source != NULL) && (source->entities != NULL)) {
1023
0
  xmlXIncludeMergeData data;
1024
1025
0
  data.ctxt = ctxt;
1026
0
  data.doc = doc;
1027
1028
0
  xmlHashScan((xmlHashTablePtr) source->entities,
1029
0
        xmlXIncludeMergeEntity, &data);
1030
0
    }
1031
0
    source = from->extSubset;
1032
0
    if ((source != NULL) && (source->entities != NULL)) {
1033
0
  xmlXIncludeMergeData data;
1034
1035
0
  data.ctxt = ctxt;
1036
0
  data.doc = doc;
1037
1038
  /*
1039
   * don't duplicate existing stuff when external subsets are the same
1040
   */
1041
0
  if ((!xmlStrEqual(target->ExternalID, source->ExternalID)) &&
1042
0
      (!xmlStrEqual(target->SystemID, source->SystemID))) {
1043
0
      xmlHashScan((xmlHashTablePtr) source->entities,
1044
0
      xmlXIncludeMergeEntity, &data);
1045
0
  }
1046
0
    }
1047
0
    return(0);
1048
0
}
1049
1050
/**
1051
 * xmlXIncludeLoadDoc:
1052
 * @ctxt:  the XInclude context
1053
 * @url:  the associated URL
1054
 * @ref:  an XMLXincludeRefPtr
1055
 *
1056
 * Load the document, and store the result in the XInclude context
1057
 *
1058
 * Returns 0 in case of success, -1 in case of failure
1059
 */
1060
static int
1061
0
xmlXIncludeLoadDoc(xmlXIncludeCtxtPtr ctxt, xmlXIncludeRefPtr ref) {
1062
0
    xmlXIncludeDocPtr cache;
1063
0
    xmlDocPtr doc;
1064
0
    const xmlChar *url = ref->URI;
1065
0
    const xmlChar *fragment = ref->fragment;
1066
0
    int i = 0;
1067
0
    int ret = -1;
1068
0
    int cacheNr;
1069
0
#ifdef LIBXML_XPTR_ENABLED
1070
0
    int saveFlags;
1071
0
#endif
1072
1073
    /*
1074
     * Handling of references to the local document are done
1075
     * directly through ctxt->doc.
1076
     */
1077
0
    if ((url[0] == 0) || (url[0] == '#') ||
1078
0
  ((ctxt->doc != NULL) && (xmlStrEqual(url, ctxt->doc->URL)))) {
1079
0
  doc = ctxt->doc;
1080
0
        goto loaded;
1081
0
    }
1082
1083
    /*
1084
     * Prevent reloading the document twice.
1085
     */
1086
0
    for (i = 0; i < ctxt->urlNr; i++) {
1087
0
  if (xmlStrEqual(url, ctxt->urlTab[i].url)) {
1088
0
            if (ctxt->urlTab[i].expanding) {
1089
0
                xmlXIncludeErr(ctxt, ref->elem, XML_XINCLUDE_RECURSION,
1090
0
                               "inclusion loop detected\n", NULL);
1091
0
                goto error;
1092
0
            }
1093
0
      doc = ctxt->urlTab[i].doc;
1094
0
            if (doc == NULL)
1095
0
                goto error;
1096
0
      goto loaded;
1097
0
  }
1098
0
    }
1099
1100
    /*
1101
     * Load it.
1102
     */
1103
0
#ifdef LIBXML_XPTR_ENABLED
1104
    /*
1105
     * If this is an XPointer evaluation, we want to assure that
1106
     * all entities have been resolved prior to processing the
1107
     * referenced document
1108
     */
1109
0
    saveFlags = ctxt->parseFlags;
1110
0
    if (fragment != NULL) { /* if this is an XPointer eval */
1111
0
  ctxt->parseFlags |= XML_PARSE_NOENT;
1112
0
    }
1113
0
#endif
1114
1115
0
    doc = xmlXIncludeParseFile(ctxt, (const char *)url);
1116
0
#ifdef LIBXML_XPTR_ENABLED
1117
0
    ctxt->parseFlags = saveFlags;
1118
0
#endif
1119
1120
    /* Also cache NULL docs */
1121
0
    if (ctxt->urlNr >= ctxt->urlMax) {
1122
0
        xmlXIncludeDoc *tmp;
1123
0
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
1124
0
        size_t newSize = ctxt->urlMax ? ctxt->urlMax * 2 : 1;
1125
#else
1126
        size_t newSize = ctxt->urlMax ? ctxt->urlMax * 2 : 8;
1127
#endif
1128
1129
0
        tmp = xmlRealloc(ctxt->urlTab, sizeof(xmlXIncludeDoc) * newSize);
1130
0
        if (tmp == NULL) {
1131
0
            xmlXIncludeErrMemory(ctxt);
1132
0
            xmlFreeDoc(doc);
1133
0
            goto error;
1134
0
        }
1135
0
        ctxt->urlMax = newSize;
1136
0
        ctxt->urlTab = tmp;
1137
0
    }
1138
0
    cache = &ctxt->urlTab[ctxt->urlNr];
1139
0
    cache->doc = doc;
1140
0
    cache->url = xmlStrdup(url);
1141
0
    if (cache->url == NULL) {
1142
0
        xmlXIncludeErrMemory(ctxt);
1143
0
        xmlFreeDoc(doc);
1144
0
        goto error;
1145
0
    }
1146
0
    cache->expanding = 0;
1147
0
    cacheNr = ctxt->urlNr++;
1148
1149
0
    if (doc == NULL)
1150
0
        goto error;
1151
    /*
1152
     * It's possible that the requested URL has been mapped to a
1153
     * completely different location (e.g. through a catalog entry).
1154
     * To check for this, we compare the URL with that of the doc
1155
     * and change it if they disagree (bug 146988).
1156
     */
1157
0
    if ((doc->URL != NULL) && (!xmlStrEqual(url, doc->URL)))
1158
0
        url = doc->URL;
1159
1160
    /*
1161
     * Make sure we have all entities fixed up
1162
     */
1163
0
    xmlXIncludeMergeEntities(ctxt, ctxt->doc, doc);
1164
1165
    /*
1166
     * We don't need the DTD anymore, free up space
1167
    if (doc->intSubset != NULL) {
1168
  xmlUnlinkNode((xmlNodePtr) doc->intSubset);
1169
  xmlFreeNode((xmlNodePtr) doc->intSubset);
1170
  doc->intSubset = NULL;
1171
    }
1172
    if (doc->extSubset != NULL) {
1173
  xmlUnlinkNode((xmlNodePtr) doc->extSubset);
1174
  xmlFreeNode((xmlNodePtr) doc->extSubset);
1175
  doc->extSubset = NULL;
1176
    }
1177
     */
1178
0
    cache->expanding = 1;
1179
0
    xmlXIncludeRecurseDoc(ctxt, doc);
1180
    /* urlTab might be reallocated. */
1181
0
    cache = &ctxt->urlTab[cacheNr];
1182
0
    cache->expanding = 0;
1183
1184
0
loaded:
1185
0
    if (fragment == NULL) {
1186
0
        xmlNodePtr root;
1187
1188
0
        root = xmlDocGetRootElement(doc);
1189
0
        if (root == NULL) {
1190
0
            xmlXIncludeErr(ctxt, ref->elem, XML_ERR_INTERNAL_ERROR,
1191
0
                           "document without root\n", NULL);
1192
0
            goto error;
1193
0
        }
1194
1195
0
        ref->inc = xmlDocCopyNode(root, ctxt->doc, 1);
1196
0
        if (ref->inc == NULL) {
1197
0
            xmlXIncludeErrMemory(ctxt);
1198
0
            goto error;
1199
0
        }
1200
1201
0
        if (ref->base != NULL)
1202
0
            xmlXIncludeBaseFixup(ctxt, root, ref->inc, ref->base);
1203
0
    }
1204
0
#ifdef LIBXML_XPTR_ENABLED
1205
0
    else {
1206
  /*
1207
   * Computes the XPointer expression and make a copy used
1208
   * as the replacement copy.
1209
   */
1210
0
  xmlXPathObjectPtr xptr;
1211
0
  xmlNodeSetPtr set;
1212
1213
0
        if (ctxt->isStream && doc == ctxt->doc) {
1214
0
      xmlXIncludeErr(ctxt, ref->elem, XML_XINCLUDE_XPTR_FAILED,
1215
0
         "XPointer expressions not allowed in streaming"
1216
0
                           " mode\n", NULL);
1217
0
            goto error;
1218
0
        }
1219
1220
0
        if (ctxt->xpctxt == NULL) {
1221
0
            ctxt->xpctxt = xmlXPathNewContext(doc);
1222
0
            if (ctxt->xpctxt == NULL) {
1223
0
                xmlXIncludeErrMemory(ctxt);
1224
0
                goto error;
1225
0
            }
1226
0
            if (ctxt->errorHandler != NULL)
1227
0
                xmlXPathSetErrorHandler(ctxt->xpctxt, ctxt->errorHandler,
1228
0
                                        ctxt->errorCtxt);
1229
0
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
1230
0
            ctxt->xpctxt->opLimit = 100000;
1231
0
#endif
1232
0
        } else {
1233
0
            ctxt->xpctxt->doc = doc;
1234
0
        }
1235
0
  xptr = xmlXPtrEval(fragment, ctxt->xpctxt);
1236
0
  if (ctxt->xpctxt->lastError.code != XML_ERR_OK) {
1237
0
            if (ctxt->xpctxt->lastError.code == XML_ERR_NO_MEMORY)
1238
0
                xmlXIncludeErrMemory(ctxt);
1239
0
            else
1240
0
                xmlXIncludeErr(ctxt, ref->elem, XML_XINCLUDE_XPTR_FAILED,
1241
0
                               "XPointer evaluation failed: #%s\n",
1242
0
                               fragment);
1243
0
            goto error;
1244
0
  }
1245
0
        if (xptr == NULL)
1246
0
            goto done;
1247
0
  switch (xptr->type) {
1248
0
      case XPATH_UNDEFINED:
1249
0
      case XPATH_BOOLEAN:
1250
0
      case XPATH_NUMBER:
1251
0
      case XPATH_STRING:
1252
0
      case XPATH_USERS:
1253
0
      case XPATH_XSLT_TREE:
1254
0
    xmlXIncludeErr(ctxt, ref->elem, XML_XINCLUDE_XPTR_RESULT,
1255
0
             "XPointer is not a range: #%s\n",
1256
0
             fragment);
1257
0
                xmlXPathFreeObject(xptr);
1258
0
                goto error;
1259
0
      case XPATH_NODESET:
1260
0
                break;
1261
1262
0
  }
1263
0
  set = xptr->nodesetval;
1264
0
  if (set != NULL) {
1265
0
      for (i = 0;i < set->nodeNr;i++) {
1266
0
    if (set->nodeTab[i] == NULL)
1267
0
        continue;
1268
0
    switch (set->nodeTab[i]->type) {
1269
0
        case XML_ELEMENT_NODE:
1270
0
        case XML_TEXT_NODE:
1271
0
        case XML_CDATA_SECTION_NODE:
1272
0
        case XML_ENTITY_REF_NODE:
1273
0
        case XML_ENTITY_NODE:
1274
0
        case XML_PI_NODE:
1275
0
        case XML_COMMENT_NODE:
1276
0
        case XML_DOCUMENT_NODE:
1277
0
        case XML_HTML_DOCUMENT_NODE:
1278
0
      continue;
1279
1280
0
        case XML_ATTRIBUTE_NODE:
1281
0
      xmlXIncludeErr(ctxt, ref->elem,
1282
0
                     XML_XINCLUDE_XPTR_RESULT,
1283
0
               "XPointer selects an attribute: #%s\n",
1284
0
               fragment);
1285
0
      set->nodeTab[i] = NULL;
1286
0
      continue;
1287
0
        case XML_NAMESPACE_DECL:
1288
0
      xmlXIncludeErr(ctxt, ref->elem,
1289
0
                     XML_XINCLUDE_XPTR_RESULT,
1290
0
               "XPointer selects a namespace: #%s\n",
1291
0
               fragment);
1292
0
      set->nodeTab[i] = NULL;
1293
0
      continue;
1294
0
        case XML_DOCUMENT_TYPE_NODE:
1295
0
        case XML_DOCUMENT_FRAG_NODE:
1296
0
        case XML_NOTATION_NODE:
1297
0
        case XML_DTD_NODE:
1298
0
        case XML_ELEMENT_DECL:
1299
0
        case XML_ATTRIBUTE_DECL:
1300
0
        case XML_ENTITY_DECL:
1301
0
        case XML_XINCLUDE_START:
1302
0
        case XML_XINCLUDE_END:
1303
0
      xmlXIncludeErr(ctxt, ref->elem,
1304
0
                     XML_XINCLUDE_XPTR_RESULT,
1305
0
           "XPointer selects unexpected nodes: #%s\n",
1306
0
               fragment);
1307
0
      set->nodeTab[i] = NULL;
1308
0
      set->nodeTab[i] = NULL;
1309
0
      continue; /* for */
1310
0
    }
1311
0
      }
1312
0
  }
1313
0
        ref->inc = xmlXIncludeCopyXPointer(ctxt, xptr, ref->base);
1314
0
        xmlXPathFreeObject(xptr);
1315
0
    }
1316
0
#endif
1317
1318
0
done:
1319
0
    ret = 0;
1320
1321
0
error:
1322
0
    return(ret);
1323
0
}
1324
1325
/**
1326
 * xmlXIncludeLoadTxt:
1327
 * @ctxt:  the XInclude context
1328
 * @ref:  an XMLXincludeRefPtr
1329
 *
1330
 * Load the content, and store the result in the XInclude context
1331
 *
1332
 * Returns 0 in case of success, -1 in case of failure
1333
 */
1334
static int
1335
0
xmlXIncludeLoadTxt(xmlXIncludeCtxtPtr ctxt, xmlXIncludeRefPtr ref) {
1336
0
    xmlParserInputBufferPtr buf;
1337
0
    xmlNodePtr node = NULL;
1338
0
    const xmlChar *url = ref->URI;
1339
0
    int i;
1340
0
    int ret = -1;
1341
0
    xmlChar *encoding = NULL;
1342
0
    xmlCharEncodingHandlerPtr handler = NULL;
1343
0
    xmlParserCtxtPtr pctxt = NULL;
1344
0
    xmlParserInputPtr inputStream = NULL;
1345
0
    int len;
1346
0
    int res;
1347
0
    const xmlChar *content;
1348
1349
    /*
1350
     * Handling of references to the local document are done
1351
     * directly through ctxt->doc.
1352
     */
1353
0
    if (url[0] == 0) {
1354
0
  xmlXIncludeErr(ctxt, ref->elem, XML_XINCLUDE_TEXT_DOCUMENT,
1355
0
           "text serialization of document not available\n", NULL);
1356
0
  goto error;
1357
0
    }
1358
1359
    /*
1360
     * Prevent reloading the document twice.
1361
     */
1362
0
    for (i = 0; i < ctxt->txtNr; i++) {
1363
0
  if (xmlStrEqual(url, ctxt->txtTab[i].url)) {
1364
0
            node = xmlNewDocText(ctxt->doc, ctxt->txtTab[i].text);
1365
0
            if (node == NULL)
1366
0
                xmlXIncludeErrMemory(ctxt);
1367
0
      goto loaded;
1368
0
  }
1369
0
    }
1370
1371
    /*
1372
     * Try to get the encoding if available
1373
     */
1374
0
    if (ref->elem != NULL) {
1375
0
  encoding = xmlXIncludeGetProp(ctxt, ref->elem, XINCLUDE_PARSE_ENCODING);
1376
0
    }
1377
0
    if (encoding != NULL) {
1378
0
        res = xmlOpenCharEncodingHandler((const char *) encoding,
1379
0
                                         /* output */ 0, &handler);
1380
1381
0
        if (res != 0) {
1382
0
            if (res == XML_ERR_NO_MEMORY) {
1383
0
                xmlXIncludeErrMemory(ctxt);
1384
0
            } else if (res == XML_ERR_UNSUPPORTED_ENCODING) {
1385
0
                xmlXIncludeErr(ctxt, ref->elem, XML_XINCLUDE_UNKNOWN_ENCODING,
1386
0
                               "encoding %s not supported\n", encoding);
1387
0
                goto error;
1388
0
            } else {
1389
0
                xmlXIncludeErr(ctxt, ref->elem, res,
1390
0
                               "unexpected error from iconv or ICU\n", NULL);
1391
0
                goto error;
1392
0
            }
1393
0
        }
1394
0
    }
1395
1396
    /*
1397
     * Load it.
1398
     */
1399
0
    pctxt = xmlNewParserCtxt();
1400
0
    if (pctxt == NULL) {
1401
0
        xmlXIncludeErrMemory(ctxt);
1402
0
        goto error;
1403
0
    }
1404
0
    if (ctxt->errorHandler != NULL)
1405
0
        xmlCtxtSetErrorHandler(pctxt, ctxt->errorHandler, ctxt->errorCtxt);
1406
0
    if (ctxt->resourceLoader != NULL)
1407
0
        xmlCtxtSetResourceLoader(pctxt, ctxt->resourceLoader,
1408
0
                                 ctxt->resourceCtxt);
1409
1410
0
    inputStream = xmlLoadResource(pctxt, (const char*) url, NULL,
1411
0
                                  XML_RESOURCE_XINCLUDE_TEXT);
1412
0
    if (inputStream == NULL) {
1413
        /*
1414
         * ENOENT only produces a warning which isn't reflected in errNo.
1415
         */
1416
0
        if (pctxt->errNo == XML_ERR_NO_MEMORY)
1417
0
            xmlXIncludeErrMemory(ctxt);
1418
0
        else if ((pctxt->errNo != XML_ERR_OK) &&
1419
0
                 (pctxt->errNo != XML_IO_ENOENT) &&
1420
0
                 (pctxt->errNo != XML_IO_UNKNOWN))
1421
0
            xmlXIncludeErr(ctxt, NULL, pctxt->errNo, "load error", NULL);
1422
0
  goto error;
1423
0
    }
1424
0
    buf = inputStream->buf;
1425
0
    if (buf == NULL)
1426
0
  goto error;
1427
0
    if (buf->encoder)
1428
0
  xmlCharEncCloseFunc(buf->encoder);
1429
0
    buf->encoder = handler;
1430
0
    handler = NULL;
1431
1432
0
    node = xmlNewDocText(ctxt->doc, NULL);
1433
0
    if (node == NULL) {
1434
0
        xmlXIncludeErrMemory(ctxt);
1435
0
  goto error;
1436
0
    }
1437
1438
    /*
1439
     * Scan all chars from the resource and add the to the node
1440
     */
1441
0
    do {
1442
0
        res = xmlParserInputBufferRead(buf, 4096);
1443
0
    } while (res > 0);
1444
0
    if (res < 0) {
1445
0
        if (buf->error == XML_ERR_NO_MEMORY)
1446
0
            xmlXIncludeErrMemory(ctxt);
1447
0
        else
1448
0
            xmlXIncludeErr(ctxt, NULL, buf->error, "read error", NULL);
1449
0
        goto error;
1450
0
    }
1451
1452
0
    content = xmlBufContent(buf->buffer);
1453
0
    len = xmlBufUse(buf->buffer);
1454
0
    for (i = 0; i < len;) {
1455
0
        int cur;
1456
0
        int l;
1457
1458
0
        l = len - i;
1459
0
        cur = xmlGetUTF8Char(&content[i], &l);
1460
0
        if ((cur < 0) || (!IS_CHAR(cur))) {
1461
0
            xmlXIncludeErr(ctxt, ref->elem, XML_XINCLUDE_INVALID_CHAR,
1462
0
                           "%s contains invalid char\n", url);
1463
0
            goto error;
1464
0
        }
1465
1466
0
        i += l;
1467
0
    }
1468
1469
0
    if (xmlNodeAddContentLen(node, content, len) < 0)
1470
0
        xmlXIncludeErrMemory(ctxt);
1471
1472
0
    if (ctxt->txtNr >= ctxt->txtMax) {
1473
0
        xmlXIncludeTxt *tmp;
1474
0
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
1475
0
        size_t newSize = ctxt->txtMax ? ctxt->txtMax * 2 : 1;
1476
#else
1477
        size_t newSize = ctxt->txtMax ? ctxt->txtMax * 2 : 8;
1478
#endif
1479
1480
0
        tmp = xmlRealloc(ctxt->txtTab, sizeof(xmlXIncludeTxt) * newSize);
1481
0
        if (tmp == NULL) {
1482
0
            xmlXIncludeErrMemory(ctxt);
1483
0
      goto error;
1484
0
        }
1485
0
        ctxt->txtMax = newSize;
1486
0
        ctxt->txtTab = tmp;
1487
0
    }
1488
0
    ctxt->txtTab[ctxt->txtNr].text = xmlStrdup(node->content);
1489
0
    if ((node->content != NULL) &&
1490
0
        (ctxt->txtTab[ctxt->txtNr].text == NULL)) {
1491
0
        xmlXIncludeErrMemory(ctxt);
1492
0
        goto error;
1493
0
    }
1494
0
    ctxt->txtTab[ctxt->txtNr].url = xmlStrdup(url);
1495
0
    if (ctxt->txtTab[ctxt->txtNr].url == NULL) {
1496
0
        xmlXIncludeErrMemory(ctxt);
1497
0
        xmlFree(ctxt->txtTab[ctxt->txtNr].text);
1498
0
        goto error;
1499
0
    }
1500
0
    ctxt->txtNr++;
1501
1502
0
loaded:
1503
    /*
1504
     * Add the element as the replacement copy.
1505
     */
1506
0
    ref->inc = node;
1507
0
    node = NULL;
1508
0
    ret = 0;
1509
1510
0
error:
1511
0
    xmlFreeNode(node);
1512
0
    xmlFreeInputStream(inputStream);
1513
0
    xmlFreeParserCtxt(pctxt);
1514
0
    xmlCharEncCloseFunc(handler);
1515
0
    xmlFree(encoding);
1516
0
    return(ret);
1517
0
}
1518
1519
/**
1520
 * xmlXIncludeLoadFallback:
1521
 * @ctxt:  the XInclude context
1522
 * @fallback:  the fallback node
1523
 * @ref:  an XMLXincludeRefPtr
1524
 *
1525
 * Load the content of the fallback node, and store the result
1526
 * in the XInclude context
1527
 *
1528
 * Returns 0 in case of success, -1 in case of failure
1529
 */
1530
static int
1531
xmlXIncludeLoadFallback(xmlXIncludeCtxtPtr ctxt, xmlNodePtr fallback,
1532
0
                        xmlXIncludeRefPtr ref) {
1533
0
    int ret = 0;
1534
0
    int oldNbErrors;
1535
1536
0
    if ((fallback == NULL) || (fallback->type == XML_NAMESPACE_DECL) ||
1537
0
        (ctxt == NULL))
1538
0
  return(-1);
1539
0
    if (fallback->children != NULL) {
1540
  /*
1541
   * It's possible that the fallback also has 'includes'
1542
   * (Bug 129969), so we re-process the fallback just in case
1543
   */
1544
0
        oldNbErrors = ctxt->nbErrors;
1545
0
  ref->inc = xmlXIncludeCopyNode(ctxt, fallback, 1, ref->base);
1546
0
  if (ctxt->nbErrors > oldNbErrors)
1547
0
      ret = -1;
1548
0
    } else {
1549
0
        ref->inc = NULL;
1550
0
    }
1551
0
    ref->fallback = 1;
1552
0
    return(ret);
1553
0
}
1554
1555
/************************************************************************
1556
 *                  *
1557
 *      XInclude Processing       *
1558
 *                  *
1559
 ************************************************************************/
1560
1561
/**
1562
 * xmlXIncludeExpandNode:
1563
 * @ctxt: an XInclude context
1564
 * @node: an XInclude node
1565
 *
1566
 * If the XInclude node wasn't processed yet, create a new RefPtr,
1567
 * add it to ctxt->incTab and load the included items.
1568
 *
1569
 * Returns the new or existing xmlXIncludeRefPtr, or NULL in case of error.
1570
 */
1571
static xmlXIncludeRefPtr
1572
0
xmlXIncludeExpandNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) {
1573
0
    xmlXIncludeRefPtr ref;
1574
0
    int i;
1575
1576
0
    if (ctxt->fatalErr)
1577
0
        return(NULL);
1578
0
    if (ctxt->depth >= XINCLUDE_MAX_DEPTH) {
1579
0
        xmlXIncludeErr(ctxt, node, XML_XINCLUDE_RECURSION,
1580
0
                       "maximum recursion depth exceeded\n", NULL);
1581
0
        ctxt->fatalErr = 1;
1582
0
        return(NULL);
1583
0
    }
1584
1585
0
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
1586
    /*
1587
     * The XInclude engine offers no protection against exponential
1588
     * expansion attacks similar to "billion laughs". Avoid timeouts by
1589
     * limiting the total number of replacements when fuzzing.
1590
     *
1591
     * Unfortuately, a single XInclude can already result in quadratic
1592
     * behavior:
1593
     *
1594
     *     <doc xmlns:xi="http://www.w3.org/2001/XInclude">
1595
     *       <xi:include xpointer="xpointer(//e)"/>
1596
     *       <e>
1597
     *         <e>
1598
     *           <e>
1599
     *             <!-- more nested elements -->
1600
     *           </e>
1601
     *         </e>
1602
     *       </e>
1603
     *     </doc>
1604
     */
1605
0
    if (ctxt->incTotal >= 20)
1606
0
        return(NULL);
1607
0
    ctxt->incTotal++;
1608
0
#endif
1609
1610
0
    for (i = 0; i < ctxt->incNr; i++) {
1611
0
        if (ctxt->incTab[i]->elem == node) {
1612
0
            if (ctxt->incTab[i]->expanding) {
1613
0
                xmlXIncludeErr(ctxt, node, XML_XINCLUDE_RECURSION,
1614
0
                               "inclusion loop detected\n", NULL);
1615
0
                return(NULL);
1616
0
            }
1617
0
            return(ctxt->incTab[i]);
1618
0
        }
1619
0
    }
1620
1621
0
    ref = xmlXIncludeAddNode(ctxt, node);
1622
0
    if (ref == NULL)
1623
0
        return(NULL);
1624
0
    ref->expanding = 1;
1625
0
    ctxt->depth++;
1626
0
    xmlXIncludeLoadNode(ctxt, ref);
1627
0
    ctxt->depth--;
1628
0
    ref->expanding = 0;
1629
1630
0
    return(ref);
1631
0
}
1632
1633
/**
1634
 * xmlXIncludeLoadNode:
1635
 * @ctxt: an XInclude context
1636
 * @ref: an xmlXIncludeRefPtr
1637
 *
1638
 * Find and load the infoset replacement for the given node.
1639
 *
1640
 * Returns 0 if substitution succeeded, -1 if some processing failed
1641
 */
1642
static int
1643
0
xmlXIncludeLoadNode(xmlXIncludeCtxtPtr ctxt, xmlXIncludeRefPtr ref) {
1644
0
    xmlNodePtr cur;
1645
0
    int ret;
1646
1647
0
    if ((ctxt == NULL) || (ref == NULL))
1648
0
  return(-1);
1649
0
    cur = ref->elem;
1650
0
    if (cur == NULL)
1651
0
  return(-1);
1652
1653
0
    if (ref->xml) {
1654
0
  ret = xmlXIncludeLoadDoc(ctxt, ref);
1655
  /* xmlXIncludeGetFragment(ctxt, cur, URI); */
1656
0
    } else {
1657
0
  ret = xmlXIncludeLoadTxt(ctxt, ref);
1658
0
    }
1659
1660
0
    if (ret < 0) {
1661
0
  xmlNodePtr children;
1662
1663
  /*
1664
   * Time to try a fallback if available
1665
   */
1666
0
  children = cur->children;
1667
0
  while (children != NULL) {
1668
0
      if ((children->type == XML_ELEMENT_NODE) &&
1669
0
    (children->ns != NULL) &&
1670
0
    (xmlStrEqual(children->name, XINCLUDE_FALLBACK)) &&
1671
0
    ((xmlStrEqual(children->ns->href, XINCLUDE_NS)) ||
1672
0
     (xmlStrEqual(children->ns->href, XINCLUDE_OLD_NS)))) {
1673
0
    ret = xmlXIncludeLoadFallback(ctxt, children, ref);
1674
0
    break;
1675
0
      }
1676
0
      children = children->next;
1677
0
  }
1678
0
    }
1679
0
    if (ret < 0) {
1680
0
  xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_NO_FALLBACK,
1681
0
           "could not load %s, and no fallback was found\n",
1682
0
           ref->URI);
1683
0
    }
1684
1685
0
    return(0);
1686
0
}
1687
1688
/**
1689
 * xmlXIncludeIncludeNode:
1690
 * @ctxt: an XInclude context
1691
 * @ref: an xmlXIncludeRefPtr
1692
 *
1693
 * Implement the infoset replacement for the given node
1694
 *
1695
 * Returns 0 if substitution succeeded, -1 if some processing failed
1696
 */
1697
static int
1698
0
xmlXIncludeIncludeNode(xmlXIncludeCtxtPtr ctxt, xmlXIncludeRefPtr ref) {
1699
0
    xmlNodePtr cur, end, list, tmp;
1700
1701
0
    if ((ctxt == NULL) || (ref == NULL))
1702
0
  return(-1);
1703
0
    cur = ref->elem;
1704
0
    if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL))
1705
0
  return(-1);
1706
1707
0
    list = ref->inc;
1708
0
    ref->inc = NULL;
1709
1710
    /*
1711
     * Check against the risk of generating a multi-rooted document
1712
     */
1713
0
    if ((cur->parent != NULL) &&
1714
0
  (cur->parent->type != XML_ELEMENT_NODE)) {
1715
0
  int nb_elem = 0;
1716
1717
0
  tmp = list;
1718
0
  while (tmp != NULL) {
1719
0
      if (tmp->type == XML_ELEMENT_NODE)
1720
0
    nb_elem++;
1721
0
      tmp = tmp->next;
1722
0
  }
1723
0
        if (nb_elem != 1) {
1724
0
            if (nb_elem > 1)
1725
0
                xmlXIncludeErr(ctxt, ref->elem, XML_XINCLUDE_MULTIPLE_ROOT,
1726
0
                               "XInclude error: would result in multiple root "
1727
0
                               "nodes\n", NULL);
1728
0
            else
1729
0
                xmlXIncludeErr(ctxt, ref->elem, XML_XINCLUDE_MULTIPLE_ROOT,
1730
0
                               "XInclude error: would result in no root "
1731
0
                               "node\n", NULL);
1732
0
            xmlFreeNodeList(list);
1733
0
      return(-1);
1734
0
  }
1735
0
    }
1736
1737
0
    if (ctxt->parseFlags & XML_PARSE_NOXINCNODE) {
1738
  /*
1739
   * Add the list of nodes
1740
         *
1741
         * TODO: Coalesce text nodes unless we are streaming mode.
1742
   */
1743
0
  while (list != NULL) {
1744
0
      end = list;
1745
0
      list = list->next;
1746
1747
0
      if (xmlAddPrevSibling(cur, end) == NULL) {
1748
0
                xmlUnlinkNode(end);
1749
0
                xmlFreeNode(end);
1750
0
                goto err_memory;
1751
0
            }
1752
0
  }
1753
0
  xmlUnlinkNode(cur);
1754
0
  xmlFreeNode(cur);
1755
0
    } else {
1756
0
        xmlNodePtr child, next;
1757
1758
  /*
1759
   * Change the current node as an XInclude start one, and add an
1760
   * XInclude end one
1761
   */
1762
0
        if (ref->fallback)
1763
0
            xmlUnsetProp(cur, BAD_CAST "href");
1764
0
  cur->type = XML_XINCLUDE_START;
1765
        /* Remove fallback children */
1766
0
        for (child = cur->children; child != NULL; child = next) {
1767
0
            next = child->next;
1768
0
            xmlUnlinkNode(child);
1769
0
            xmlFreeNode(child);
1770
0
        }
1771
0
  end = xmlNewDocNode(cur->doc, cur->ns, cur->name, NULL);
1772
0
  if (end == NULL)
1773
0
            goto err_memory;
1774
0
  end->type = XML_XINCLUDE_END;
1775
0
  if (xmlAddNextSibling(cur, end) == NULL) {
1776
0
            xmlFreeNode(end);
1777
0
            goto err_memory;
1778
0
        }
1779
1780
  /*
1781
   * Add the list of nodes
1782
   */
1783
0
  while (list != NULL) {
1784
0
      cur = list;
1785
0
      list = list->next;
1786
1787
0
      if (xmlAddPrevSibling(end, cur) == NULL) {
1788
0
                xmlUnlinkNode(cur);
1789
0
                xmlFreeNode(cur);
1790
0
                goto err_memory;
1791
0
            }
1792
0
  }
1793
0
    }
1794
1795
1796
0
    return(0);
1797
1798
0
err_memory:
1799
0
    xmlXIncludeErrMemory(ctxt);
1800
0
    xmlFreeNodeList(list);
1801
0
    return(-1);
1802
0
}
1803
1804
/**
1805
 * xmlXIncludeTestNode:
1806
 * @ctxt: the XInclude processing context
1807
 * @node: an XInclude node
1808
 *
1809
 * test if the node is an XInclude node
1810
 *
1811
 * Returns 1 true, 0 otherwise
1812
 */
1813
static int
1814
0
xmlXIncludeTestNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) {
1815
0
    if (node == NULL)
1816
0
  return(0);
1817
0
    if (node->type != XML_ELEMENT_NODE)
1818
0
  return(0);
1819
0
    if (node->ns == NULL)
1820
0
  return(0);
1821
0
    if ((xmlStrEqual(node->ns->href, XINCLUDE_NS)) ||
1822
0
        (xmlStrEqual(node->ns->href, XINCLUDE_OLD_NS))) {
1823
0
  if (xmlStrEqual(node->ns->href, XINCLUDE_OLD_NS)) {
1824
0
      if (ctxt->legacy == 0) {
1825
0
          ctxt->legacy = 1;
1826
0
      }
1827
0
  }
1828
0
  if (xmlStrEqual(node->name, XINCLUDE_NODE)) {
1829
0
      xmlNodePtr child = node->children;
1830
0
      int nb_fallback = 0;
1831
1832
0
      while (child != NULL) {
1833
0
    if ((child->type == XML_ELEMENT_NODE) &&
1834
0
        (child->ns != NULL) &&
1835
0
        ((xmlStrEqual(child->ns->href, XINCLUDE_NS)) ||
1836
0
         (xmlStrEqual(child->ns->href, XINCLUDE_OLD_NS)))) {
1837
0
        if (xmlStrEqual(child->name, XINCLUDE_NODE)) {
1838
0
      xmlXIncludeErr(ctxt, node,
1839
0
                     XML_XINCLUDE_INCLUDE_IN_INCLUDE,
1840
0
               "%s has an 'include' child\n",
1841
0
               XINCLUDE_NODE);
1842
0
      return(0);
1843
0
        }
1844
0
        if (xmlStrEqual(child->name, XINCLUDE_FALLBACK)) {
1845
0
      nb_fallback++;
1846
0
        }
1847
0
    }
1848
0
    child = child->next;
1849
0
      }
1850
0
      if (nb_fallback > 1) {
1851
0
    xmlXIncludeErr(ctxt, node, XML_XINCLUDE_FALLBACKS_IN_INCLUDE,
1852
0
             "%s has multiple fallback children\n",
1853
0
                   XINCLUDE_NODE);
1854
0
    return(0);
1855
0
      }
1856
0
      return(1);
1857
0
  }
1858
0
  if (xmlStrEqual(node->name, XINCLUDE_FALLBACK)) {
1859
0
      if ((node->parent == NULL) ||
1860
0
    (node->parent->type != XML_ELEMENT_NODE) ||
1861
0
    (node->parent->ns == NULL) ||
1862
0
    ((!xmlStrEqual(node->parent->ns->href, XINCLUDE_NS)) &&
1863
0
     (!xmlStrEqual(node->parent->ns->href, XINCLUDE_OLD_NS))) ||
1864
0
    (!xmlStrEqual(node->parent->name, XINCLUDE_NODE))) {
1865
0
    xmlXIncludeErr(ctxt, node,
1866
0
                   XML_XINCLUDE_FALLBACK_NOT_IN_INCLUDE,
1867
0
             "%s is not the child of an 'include'\n",
1868
0
             XINCLUDE_FALLBACK);
1869
0
      }
1870
0
  }
1871
0
    }
1872
0
    return(0);
1873
0
}
1874
1875
/**
1876
 * xmlXIncludeDoProcess:
1877
 * @ctxt: the XInclude processing context
1878
 * @tree: the top of the tree to process
1879
 *
1880
 * Implement the XInclude substitution on the XML document @doc
1881
 *
1882
 * Returns 0 if no substitution were done, -1 if some processing failed
1883
 *    or the number of substitutions done.
1884
 */
1885
static int
1886
0
xmlXIncludeDoProcess(xmlXIncludeCtxtPtr ctxt, xmlNodePtr tree) {
1887
0
    xmlXIncludeRefPtr ref;
1888
0
    xmlNodePtr cur;
1889
0
    int ret = 0;
1890
0
    int i, start;
1891
1892
    /*
1893
     * First phase: lookup the elements in the document
1894
     */
1895
0
    start = ctxt->incNr;
1896
0
    cur = tree;
1897
0
    do {
1898
  /* TODO: need to work on entities -> stack */
1899
0
        if (xmlXIncludeTestNode(ctxt, cur) == 1) {
1900
0
            ref = xmlXIncludeExpandNode(ctxt, cur);
1901
            /*
1902
             * Mark direct includes.
1903
             */
1904
0
            if (ref != NULL)
1905
0
                ref->replace = 1;
1906
0
        } else if ((cur->children != NULL) &&
1907
0
                   ((cur->type == XML_DOCUMENT_NODE) ||
1908
0
                    (cur->type == XML_ELEMENT_NODE))) {
1909
0
            cur = cur->children;
1910
0
            continue;
1911
0
        }
1912
0
        do {
1913
0
            if (cur == tree)
1914
0
                break;
1915
0
            if (cur->next != NULL) {
1916
0
                cur = cur->next;
1917
0
                break;
1918
0
            }
1919
0
            cur = cur->parent;
1920
0
        } while (cur != NULL);
1921
0
    } while ((cur != NULL) && (cur != tree));
1922
1923
    /*
1924
     * Second phase: extend the original document infoset.
1925
     */
1926
0
    for (i = start; i < ctxt->incNr; i++) {
1927
0
  if (ctxt->incTab[i]->replace != 0) {
1928
0
            xmlXIncludeIncludeNode(ctxt, ctxt->incTab[i]);
1929
0
            ctxt->incTab[i]->replace = 0;
1930
0
        } else {
1931
            /*
1932
             * Ignore includes which were added indirectly, for example
1933
             * inside xi:fallback elements.
1934
             */
1935
0
            if (ctxt->incTab[i]->inc != NULL) {
1936
0
                xmlFreeNodeList(ctxt->incTab[i]->inc);
1937
0
                ctxt->incTab[i]->inc = NULL;
1938
0
            }
1939
0
        }
1940
0
  ret++;
1941
0
    }
1942
1943
0
    if (ctxt->isStream) {
1944
        /*
1945
         * incTab references nodes which will eventually be deleted in
1946
         * streaming mode. The table is only required for XPointer
1947
         * expressions which aren't allowed in streaming mode.
1948
         */
1949
0
        for (i = 0;i < ctxt->incNr;i++) {
1950
0
            xmlXIncludeFreeRef(ctxt->incTab[i]);
1951
0
        }
1952
0
        ctxt->incNr = 0;
1953
0
    }
1954
1955
0
    return(ret);
1956
0
}
1957
1958
/**
1959
 * xmlXIncludeDoProcessRoot:
1960
 * @ctxt: the XInclude processing context
1961
 * @tree: the top of the tree to process
1962
 *
1963
 * Implement the XInclude substitution on the XML document @doc
1964
 *
1965
 * Returns 0 if no substitution were done, -1 if some processing failed
1966
 *    or the number of substitutions done.
1967
 */
1968
static int
1969
0
xmlXIncludeDoProcessRoot(xmlXIncludeCtxtPtr ctxt, xmlNodePtr tree) {
1970
0
    if ((tree == NULL) || (tree->type == XML_NAMESPACE_DECL))
1971
0
  return(-1);
1972
0
    if (ctxt == NULL)
1973
0
  return(-1);
1974
1975
0
    return(xmlXIncludeDoProcess(ctxt, tree));
1976
0
}
1977
1978
/**
1979
 * xmlXIncludeGetLastError:
1980
 * @ctxt:  an XInclude processing context
1981
 *
1982
 * Available since 2.13.0.
1983
 *
1984
 * Returns the last error code.
1985
 */
1986
int
1987
0
xmlXIncludeGetLastError(xmlXIncludeCtxtPtr ctxt) {
1988
0
    if (ctxt == NULL)
1989
0
        return(XML_ERR_ARGUMENT);
1990
0
    return(ctxt->errNo);
1991
0
}
1992
1993
/**
1994
 * xmlXIncludeSetErrorHandler:
1995
 * @ctxt:  an XInclude processing context
1996
 * @handler:  error handler
1997
 * @data:  user data which will be passed to the handler
1998
 *
1999
 * Register a callback function that will be called on errors and
2000
 * warnings. If handler is NULL, the error handler will be deactivated.
2001
 *
2002
 * Available since 2.13.0.
2003
 */
2004
void
2005
xmlXIncludeSetErrorHandler(xmlXIncludeCtxtPtr ctxt,
2006
0
                           xmlStructuredErrorFunc handler, void *data) {
2007
0
    if (ctxt == NULL)
2008
0
        return;
2009
0
    ctxt->errorHandler = handler;
2010
0
    ctxt->errorCtxt = data;
2011
0
}
2012
2013
/**
2014
 * xmlXIncludeSetResourceLoader:
2015
 * @ctxt:  an XInclude processing context
2016
 * @loader:  resource loader
2017
 * @data:  user data which will be passed to the loader
2018
 *
2019
 * Register a callback function that will be called to load included
2020
 * documents.
2021
 *
2022
 * Available since 2.14.0.
2023
 */
2024
void
2025
xmlXIncludeSetResourceLoader(xmlXIncludeCtxtPtr ctxt,
2026
0
                             xmlResourceLoader loader, void *data) {
2027
0
    if (ctxt == NULL)
2028
0
        return;
2029
0
    ctxt->resourceLoader = loader;
2030
0
    ctxt->resourceCtxt = data;
2031
0
}
2032
2033
/**
2034
 * xmlXIncludeSetFlags:
2035
 * @ctxt:  an XInclude processing context
2036
 * @flags: a set of xmlParserOption used for parsing XML includes
2037
 *
2038
 * Set the flags used for further processing of XML resources.
2039
 *
2040
 * Returns 0 in case of success and -1 in case of error.
2041
 */
2042
int
2043
0
xmlXIncludeSetFlags(xmlXIncludeCtxtPtr ctxt, int flags) {
2044
0
    if (ctxt == NULL)
2045
0
        return(-1);
2046
0
    ctxt->parseFlags = flags;
2047
0
    return(0);
2048
0
}
2049
2050
/**
2051
 * xmlXIncludeSetStreamingMode:
2052
 * @ctxt:  an XInclude processing context
2053
 * @mode:  whether streaming mode should be enabled
2054
 *
2055
 * In streaming mode, XPointer expressions aren't allowed.
2056
 *
2057
 * Returns 0 in case of success and -1 in case of error.
2058
 */
2059
int
2060
0
xmlXIncludeSetStreamingMode(xmlXIncludeCtxtPtr ctxt, int mode) {
2061
0
    if (ctxt == NULL)
2062
0
        return(-1);
2063
0
    ctxt->isStream = !!mode;
2064
0
    return(0);
2065
0
}
2066
2067
/**
2068
 * xmlXIncludeProcessTreeFlagsData:
2069
 * @tree: an XML node
2070
 * @flags: a set of xmlParserOption used for parsing XML includes
2071
 * @data: application data that will be passed to the parser context
2072
 *        in the _private field of the parser context(s)
2073
 *
2074
 * Implement the XInclude substitution on the XML node @tree
2075
 *
2076
 * Returns 0 if no substitution were done, -1 if some processing failed
2077
 *    or the number of substitutions done.
2078
 */
2079
2080
int
2081
0
xmlXIncludeProcessTreeFlagsData(xmlNodePtr tree, int flags, void *data) {
2082
0
    xmlXIncludeCtxtPtr ctxt;
2083
0
    int ret = 0;
2084
2085
0
    if ((tree == NULL) || (tree->type == XML_NAMESPACE_DECL) ||
2086
0
        (tree->doc == NULL))
2087
0
        return(-1);
2088
2089
0
    ctxt = xmlXIncludeNewContext(tree->doc);
2090
0
    if (ctxt == NULL)
2091
0
        return(-1);
2092
0
    ctxt->_private = data;
2093
0
    xmlXIncludeSetFlags(ctxt, flags);
2094
0
    ret = xmlXIncludeDoProcessRoot(ctxt, tree);
2095
0
    if ((ret >= 0) && (ctxt->nbErrors > 0))
2096
0
        ret = -1;
2097
2098
0
    xmlXIncludeFreeContext(ctxt);
2099
0
    return(ret);
2100
0
}
2101
2102
/**
2103
 * xmlXIncludeProcessFlagsData:
2104
 * @doc: an XML document
2105
 * @flags: a set of xmlParserOption used for parsing XML includes
2106
 * @data: application data that will be passed to the parser context
2107
 *        in the _private field of the parser context(s)
2108
 *
2109
 * Implement the XInclude substitution on the XML document @doc
2110
 *
2111
 * Returns 0 if no substitution were done, -1 if some processing failed
2112
 *    or the number of substitutions done.
2113
 */
2114
int
2115
0
xmlXIncludeProcessFlagsData(xmlDocPtr doc, int flags, void *data) {
2116
0
    xmlNodePtr tree;
2117
2118
0
    if (doc == NULL)
2119
0
  return(-1);
2120
0
    tree = xmlDocGetRootElement(doc);
2121
0
    if (tree == NULL)
2122
0
  return(-1);
2123
0
    return(xmlXIncludeProcessTreeFlagsData(tree, flags, data));
2124
0
}
2125
2126
/**
2127
 * xmlXIncludeProcessFlags:
2128
 * @doc: an XML document
2129
 * @flags: a set of xmlParserOption used for parsing XML includes
2130
 *
2131
 * Implement the XInclude substitution on the XML document @doc
2132
 *
2133
 * Returns 0 if no substitution were done, -1 if some processing failed
2134
 *    or the number of substitutions done.
2135
 */
2136
int
2137
0
xmlXIncludeProcessFlags(xmlDocPtr doc, int flags) {
2138
0
    return xmlXIncludeProcessFlagsData(doc, flags, NULL);
2139
0
}
2140
2141
/**
2142
 * xmlXIncludeProcess:
2143
 * @doc: an XML document
2144
 *
2145
 * Implement the XInclude substitution on the XML document @doc
2146
 *
2147
 * Returns 0 if no substitution were done, -1 if some processing failed
2148
 *    or the number of substitutions done.
2149
 */
2150
int
2151
0
xmlXIncludeProcess(xmlDocPtr doc) {
2152
0
    return(xmlXIncludeProcessFlags(doc, 0));
2153
0
}
2154
2155
/**
2156
 * xmlXIncludeProcessTreeFlags:
2157
 * @tree: a node in an XML document
2158
 * @flags: a set of xmlParserOption used for parsing XML includes
2159
 *
2160
 * Implement the XInclude substitution for the given subtree
2161
 *
2162
 * Returns 0 if no substitution were done, -1 if some processing failed
2163
 *    or the number of substitutions done.
2164
 */
2165
int
2166
0
xmlXIncludeProcessTreeFlags(xmlNodePtr tree, int flags) {
2167
0
    xmlXIncludeCtxtPtr ctxt;
2168
0
    int ret = 0;
2169
2170
0
    if ((tree == NULL) || (tree->type == XML_NAMESPACE_DECL) ||
2171
0
        (tree->doc == NULL))
2172
0
  return(-1);
2173
0
    ctxt = xmlXIncludeNewContext(tree->doc);
2174
0
    if (ctxt == NULL)
2175
0
  return(-1);
2176
0
    xmlXIncludeSetFlags(ctxt, flags);
2177
0
    ret = xmlXIncludeDoProcessRoot(ctxt, tree);
2178
0
    if ((ret >= 0) && (ctxt->nbErrors > 0))
2179
0
  ret = -1;
2180
2181
0
    xmlXIncludeFreeContext(ctxt);
2182
0
    return(ret);
2183
0
}
2184
2185
/**
2186
 * xmlXIncludeProcessTree:
2187
 * @tree: a node in an XML document
2188
 *
2189
 * Implement the XInclude substitution for the given subtree
2190
 *
2191
 * Returns 0 if no substitution were done, -1 if some processing failed
2192
 *    or the number of substitutions done.
2193
 */
2194
int
2195
0
xmlXIncludeProcessTree(xmlNodePtr tree) {
2196
0
    return(xmlXIncludeProcessTreeFlags(tree, 0));
2197
0
}
2198
2199
/**
2200
 * xmlXIncludeProcessNode:
2201
 * @ctxt: an existing XInclude context
2202
 * @node: a node in an XML document
2203
 *
2204
 * Implement the XInclude substitution for the given subtree reusing
2205
 * the information and data coming from the given context.
2206
 *
2207
 * Returns 0 if no substitution were done, -1 if some processing failed
2208
 *    or the number of substitutions done.
2209
 */
2210
int
2211
0
xmlXIncludeProcessNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) {
2212
0
    int ret = 0;
2213
2214
0
    if ((node == NULL) || (node->type == XML_NAMESPACE_DECL) ||
2215
0
        (node->doc == NULL) || (ctxt == NULL))
2216
0
  return(-1);
2217
0
    ret = xmlXIncludeDoProcessRoot(ctxt, node);
2218
0
    if ((ret >= 0) && (ctxt->nbErrors > 0))
2219
0
  ret = -1;
2220
0
    return(ret);
2221
0
}
2222
2223
#else /* !LIBXML_XINCLUDE_ENABLED */
2224
#endif