Coverage Report

Created: 2023-03-26 06:13

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