Coverage Report

Created: 2023-06-07 06:14

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