Coverage Report

Created: 2023-09-25 06:05

/src/libxml2-2.11.5/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
    int cacheNr;
1269
0
#ifdef LIBXML_XPTR_ENABLED
1270
0
    int saveFlags;
1271
0
#endif
1272
1273
#ifdef DEBUG_XINCLUDE
1274
    xmlGenericError(xmlGenericErrorContext, "Loading doc %s\n", url);
1275
#endif
1276
    /*
1277
     * Check the URL and remove any fragment identifier
1278
     */
1279
0
    uri = xmlParseURI((const char *)url);
1280
0
    if (uri == NULL) {
1281
0
  xmlXIncludeErr(ctxt, ref->elem, XML_XINCLUDE_HREF_URI,
1282
0
           "invalid value URI %s\n", url);
1283
0
        goto error;
1284
0
    }
1285
0
    if (uri->fragment != NULL) {
1286
0
  fragment = (xmlChar *) uri->fragment;
1287
0
  uri->fragment = NULL;
1288
0
    }
1289
0
    if (ref->fragment != NULL) {
1290
0
  if (fragment != NULL) xmlFree(fragment);
1291
0
  fragment = xmlStrdup(ref->fragment);
1292
0
    }
1293
0
    URL = xmlSaveUri(uri);
1294
0
    xmlFreeURI(uri);
1295
0
    if (URL == NULL) {
1296
0
        xmlXIncludeErr(ctxt, ref->elem, XML_XINCLUDE_HREF_URI,
1297
0
                       "invalid value URI %s\n", url);
1298
0
        goto error;
1299
0
    }
1300
1301
    /*
1302
     * Handling of references to the local document are done
1303
     * directly through ctxt->doc.
1304
     */
1305
0
    if ((URL[0] == 0) || (URL[0] == '#') ||
1306
0
  ((ctxt->doc != NULL) && (xmlStrEqual(URL, ctxt->doc->URL)))) {
1307
0
  doc = ctxt->doc;
1308
0
        goto loaded;
1309
0
    }
1310
1311
    /*
1312
     * Prevent reloading the document twice.
1313
     */
1314
0
    for (i = 0; i < ctxt->urlNr; i++) {
1315
0
  if (xmlStrEqual(URL, ctxt->urlTab[i].url)) {
1316
#ifdef DEBUG_XINCLUDE
1317
      printf("Already loaded %s\n", URL);
1318
#endif
1319
0
            if (ctxt->urlTab[i].expanding) {
1320
0
                xmlXIncludeErr(ctxt, ref->elem, XML_XINCLUDE_RECURSION,
1321
0
                               "inclusion loop detected\n", NULL);
1322
0
                goto error;
1323
0
            }
1324
0
      doc = ctxt->urlTab[i].doc;
1325
0
            if (doc == NULL)
1326
0
                goto error;
1327
0
      goto loaded;
1328
0
  }
1329
0
    }
1330
1331
    /*
1332
     * Load it.
1333
     */
1334
#ifdef DEBUG_XINCLUDE
1335
    printf("loading %s\n", URL);
1336
#endif
1337
0
#ifdef LIBXML_XPTR_ENABLED
1338
    /*
1339
     * If this is an XPointer evaluation, we want to assure that
1340
     * all entities have been resolved prior to processing the
1341
     * referenced document
1342
     */
1343
0
    saveFlags = ctxt->parseFlags;
1344
0
    if (fragment != NULL) { /* if this is an XPointer eval */
1345
0
  ctxt->parseFlags |= XML_PARSE_NOENT;
1346
0
    }
1347
0
#endif
1348
1349
0
    doc = xmlXIncludeParseFile(ctxt, (const char *)URL);
1350
0
#ifdef LIBXML_XPTR_ENABLED
1351
0
    ctxt->parseFlags = saveFlags;
1352
0
#endif
1353
1354
    /* Also cache NULL docs */
1355
0
    if (ctxt->urlNr >= ctxt->urlMax) {
1356
0
        xmlXIncludeDoc *tmp;
1357
0
        size_t newSize = ctxt->urlMax ? ctxt->urlMax * 2 : 8;
1358
1359
0
        tmp = xmlRealloc(ctxt->urlTab, sizeof(xmlXIncludeDoc) * newSize);
1360
0
        if (tmp == NULL) {
1361
0
            xmlXIncludeErrMemory(ctxt, ref->elem,
1362
0
                                 "growing XInclude URL table");
1363
0
            xmlFreeDoc(doc);
1364
0
            goto error;
1365
0
        }
1366
0
        ctxt->urlMax = newSize;
1367
0
        ctxt->urlTab = tmp;
1368
0
    }
1369
0
    cacheNr = ctxt->urlNr++;
1370
0
    cache = &ctxt->urlTab[cacheNr];
1371
0
    cache->doc = doc;
1372
0
    cache->url = xmlStrdup(URL);
1373
0
    cache->expanding = 0;
1374
1375
0
    if (doc == NULL)
1376
0
        goto error;
1377
    /*
1378
     * It's possible that the requested URL has been mapped to a
1379
     * completely different location (e.g. through a catalog entry).
1380
     * To check for this, we compare the URL with that of the doc
1381
     * and change it if they disagree (bug 146988).
1382
     */
1383
0
   if (!xmlStrEqual(URL, doc->URL)) {
1384
0
       xmlFree(URL);
1385
0
       URL = xmlStrdup(doc->URL);
1386
0
   }
1387
1388
    /*
1389
     * Make sure we have all entities fixed up
1390
     */
1391
0
    xmlXIncludeMergeEntities(ctxt, ctxt->doc, doc);
1392
1393
    /*
1394
     * We don't need the DTD anymore, free up space
1395
    if (doc->intSubset != NULL) {
1396
  xmlUnlinkNode((xmlNodePtr) doc->intSubset);
1397
  xmlFreeNode((xmlNodePtr) doc->intSubset);
1398
  doc->intSubset = NULL;
1399
    }
1400
    if (doc->extSubset != NULL) {
1401
  xmlUnlinkNode((xmlNodePtr) doc->extSubset);
1402
  xmlFreeNode((xmlNodePtr) doc->extSubset);
1403
  doc->extSubset = NULL;
1404
    }
1405
     */
1406
0
    cache->expanding = 1;
1407
0
    xmlXIncludeRecurseDoc(ctxt, doc, URL);
1408
    /* urlTab might be reallocated. */
1409
0
    cache = &ctxt->urlTab[cacheNr];
1410
0
    cache->expanding = 0;
1411
1412
0
loaded:
1413
0
    if (fragment == NULL) {
1414
  /*
1415
   * Add the top children list as the replacement copy.
1416
   */
1417
0
        ref->inc = xmlDocCopyNode(xmlDocGetRootElement(doc), ctxt->doc, 1);
1418
0
    }
1419
0
#ifdef LIBXML_XPTR_ENABLED
1420
0
    else {
1421
  /*
1422
   * Computes the XPointer expression and make a copy used
1423
   * as the replacement copy.
1424
   */
1425
0
  xmlXPathObjectPtr xptr;
1426
0
  xmlXPathContextPtr xptrctxt;
1427
0
  xmlNodeSetPtr set;
1428
1429
0
        if (ctxt->isStream && doc == ctxt->doc) {
1430
0
      xmlXIncludeErr(ctxt, ref->elem, XML_XINCLUDE_XPTR_FAILED,
1431
0
         "XPointer expressions not allowed in streaming"
1432
0
                           " mode\n", NULL);
1433
0
            goto error;
1434
0
        }
1435
1436
0
  xptrctxt = xmlXPtrNewContext(doc, NULL, NULL);
1437
0
  if (xptrctxt == NULL) {
1438
0
      xmlXIncludeErr(ctxt, ref->elem, XML_XINCLUDE_XPTR_FAILED,
1439
0
         "could not create XPointer context\n", NULL);
1440
0
            goto error;
1441
0
  }
1442
0
  xptr = xmlXPtrEval(fragment, xptrctxt);
1443
0
  if (xptr == NULL) {
1444
0
      xmlXIncludeErr(ctxt, ref->elem, XML_XINCLUDE_XPTR_FAILED,
1445
0
         "XPointer evaluation failed: #%s\n",
1446
0
         fragment);
1447
0
      xmlXPathFreeContext(xptrctxt);
1448
0
            goto error;
1449
0
  }
1450
0
  switch (xptr->type) {
1451
0
      case XPATH_UNDEFINED:
1452
0
      case XPATH_BOOLEAN:
1453
0
      case XPATH_NUMBER:
1454
0
      case XPATH_STRING:
1455
#ifdef LIBXML_XPTR_LOCS_ENABLED
1456
      case XPATH_POINT:
1457
#endif
1458
0
      case XPATH_USERS:
1459
0
      case XPATH_XSLT_TREE:
1460
0
    xmlXIncludeErr(ctxt, ref->elem, XML_XINCLUDE_XPTR_RESULT,
1461
0
             "XPointer is not a range: #%s\n",
1462
0
             fragment);
1463
0
                xmlXPathFreeObject(xptr);
1464
0
    xmlXPathFreeContext(xptrctxt);
1465
0
                goto error;
1466
0
      case XPATH_NODESET:
1467
0
          if ((xptr->nodesetval == NULL) ||
1468
0
        (xptr->nodesetval->nodeNr <= 0)) {
1469
0
                    xmlXPathFreeObject(xptr);
1470
0
        xmlXPathFreeContext(xptrctxt);
1471
0
                    goto error;
1472
0
    }
1473
1474
#ifdef LIBXML_XPTR_LOCS_ENABLED
1475
      case XPATH_RANGE:
1476
      case XPATH_LOCATIONSET:
1477
    break;
1478
#endif
1479
0
  }
1480
0
  set = xptr->nodesetval;
1481
0
  if (set != NULL) {
1482
0
      for (i = 0;i < set->nodeNr;i++) {
1483
0
    if (set->nodeTab[i] == NULL)
1484
0
        continue;
1485
0
    switch (set->nodeTab[i]->type) {
1486
0
        case XML_ELEMENT_NODE:
1487
0
        case XML_TEXT_NODE:
1488
0
        case XML_CDATA_SECTION_NODE:
1489
0
        case XML_ENTITY_REF_NODE:
1490
0
        case XML_ENTITY_NODE:
1491
0
        case XML_PI_NODE:
1492
0
        case XML_COMMENT_NODE:
1493
0
        case XML_DOCUMENT_NODE:
1494
0
        case XML_HTML_DOCUMENT_NODE:
1495
0
      continue;
1496
1497
0
        case XML_ATTRIBUTE_NODE:
1498
0
      xmlXIncludeErr(ctxt, ref->elem,
1499
0
                     XML_XINCLUDE_XPTR_RESULT,
1500
0
               "XPointer selects an attribute: #%s\n",
1501
0
               fragment);
1502
0
      set->nodeTab[i] = NULL;
1503
0
      continue;
1504
0
        case XML_NAMESPACE_DECL:
1505
0
      xmlXIncludeErr(ctxt, ref->elem,
1506
0
                     XML_XINCLUDE_XPTR_RESULT,
1507
0
               "XPointer selects a namespace: #%s\n",
1508
0
               fragment);
1509
0
      set->nodeTab[i] = NULL;
1510
0
      continue;
1511
0
        case XML_DOCUMENT_TYPE_NODE:
1512
0
        case XML_DOCUMENT_FRAG_NODE:
1513
0
        case XML_NOTATION_NODE:
1514
0
        case XML_DTD_NODE:
1515
0
        case XML_ELEMENT_DECL:
1516
0
        case XML_ATTRIBUTE_DECL:
1517
0
        case XML_ENTITY_DECL:
1518
0
        case XML_XINCLUDE_START:
1519
0
        case XML_XINCLUDE_END:
1520
0
      xmlXIncludeErr(ctxt, ref->elem,
1521
0
                     XML_XINCLUDE_XPTR_RESULT,
1522
0
           "XPointer selects unexpected nodes: #%s\n",
1523
0
               fragment);
1524
0
      set->nodeTab[i] = NULL;
1525
0
      set->nodeTab[i] = NULL;
1526
0
      continue; /* for */
1527
0
    }
1528
0
      }
1529
0
  }
1530
0
        ref->inc = xmlXIncludeCopyXPointer(ctxt, xptr);
1531
0
        xmlXPathFreeObject(xptr);
1532
0
  xmlXPathFreeContext(xptrctxt);
1533
0
    }
1534
0
#endif
1535
1536
    /*
1537
     * Do the xml:base fixup if needed
1538
     */
1539
0
    if ((doc != NULL) && (URL != NULL) &&
1540
0
        (!(ctxt->parseFlags & XML_PARSE_NOBASEFIX)) &&
1541
0
  (!(doc->parseFlags & XML_PARSE_NOBASEFIX))) {
1542
0
  xmlNodePtr node;
1543
0
  xmlChar *base;
1544
0
  xmlChar *curBase;
1545
1546
  /*
1547
   * The base is only adjusted if "necessary", i.e. if the xinclude node
1548
   * has a base specified, or the URL is relative
1549
   */
1550
0
  base = xmlGetNsProp(ref->elem, BAD_CAST "base", XML_XML_NAMESPACE);
1551
0
  if (base == NULL) {
1552
      /*
1553
       * No xml:base on the xinclude node, so we check whether the
1554
       * URI base is different than (relative to) the context base
1555
       */
1556
0
      curBase = xmlBuildRelativeURI(URL, ctxt->base);
1557
0
      if (curBase == NULL) { /* Error return */
1558
0
          xmlXIncludeErr(ctxt, ref->elem, XML_XINCLUDE_HREF_URI,
1559
0
           "trying to build relative URI from %s\n", URL);
1560
0
      } else {
1561
    /* If the URI doesn't contain a slash, it's not relative */
1562
0
          if (!xmlStrchr(curBase, '/'))
1563
0
        xmlFree(curBase);
1564
0
    else
1565
0
        base = curBase;
1566
0
      }
1567
0
  }
1568
0
  if (base != NULL) { /* Adjustment may be needed */
1569
0
      node = ref->inc;
1570
0
      while (node != NULL) {
1571
    /* Only work on element nodes */
1572
0
    if (node->type == XML_ELEMENT_NODE) {
1573
0
        curBase = xmlNodeGetBase(node->doc, node);
1574
        /* If no current base, set it */
1575
0
        if (curBase == NULL) {
1576
0
      xmlNodeSetBase(node, base);
1577
0
        } else {
1578
      /*
1579
       * If the current base is the same as the
1580
       * URL of the document, then reset it to be
1581
       * the specified xml:base or the relative URI
1582
       */
1583
0
      if (xmlStrEqual(curBase, node->doc->URL)) {
1584
0
          xmlNodeSetBase(node, base);
1585
0
      } else {
1586
          /*
1587
           * If the element already has an xml:base
1588
           * set, then relativise it if necessary
1589
           */
1590
0
          xmlChar *xmlBase;
1591
0
          xmlBase = xmlGetNsProp(node,
1592
0
              BAD_CAST "base",
1593
0
              XML_XML_NAMESPACE);
1594
0
          if (xmlBase != NULL) {
1595
0
        xmlChar *relBase;
1596
0
        relBase = xmlBuildURI(xmlBase, base);
1597
0
        if (relBase == NULL) { /* error */
1598
0
            xmlXIncludeErr(ctxt,
1599
0
            ref->elem,
1600
0
            XML_XINCLUDE_HREF_URI,
1601
0
          "trying to rebuild base from %s\n",
1602
0
            xmlBase);
1603
0
        } else {
1604
0
            xmlNodeSetBase(node, relBase);
1605
0
            xmlFree(relBase);
1606
0
        }
1607
0
        xmlFree(xmlBase);
1608
0
          }
1609
0
      }
1610
0
      xmlFree(curBase);
1611
0
        }
1612
0
    }
1613
0
          node = node->next;
1614
0
      }
1615
0
      xmlFree(base);
1616
0
  }
1617
0
    }
1618
0
    ret = 0;
1619
1620
0
error:
1621
0
    xmlFree(URL);
1622
0
    xmlFree(fragment);
1623
0
    return(ret);
1624
0
}
1625
1626
/**
1627
 * xmlXIncludeLoadTxt:
1628
 * @ctxt:  the XInclude context
1629
 * @url:  the associated URL
1630
 * @ref:  an XMLXincludeRefPtr
1631
 *
1632
 * Load the content, and store the result in the XInclude context
1633
 *
1634
 * Returns 0 in case of success, -1 in case of failure
1635
 */
1636
static int
1637
xmlXIncludeLoadTxt(xmlXIncludeCtxtPtr ctxt, const xmlChar *url,
1638
0
                   xmlXIncludeRefPtr ref) {
1639
0
    xmlParserInputBufferPtr buf;
1640
0
    xmlNodePtr node = NULL;
1641
0
    xmlURIPtr uri = NULL;
1642
0
    xmlChar *URL = NULL;
1643
0
    int i;
1644
0
    int ret = -1;
1645
0
    xmlChar *encoding = NULL;
1646
0
    xmlCharEncoding enc = (xmlCharEncoding) 0;
1647
0
    xmlParserCtxtPtr pctxt = NULL;
1648
0
    xmlParserInputPtr inputStream = NULL;
1649
0
    int len;
1650
0
    const xmlChar *content;
1651
1652
1653
    /* Don't read from stdin. */
1654
0
    if (xmlStrcmp(url, BAD_CAST "-") == 0)
1655
0
        url = BAD_CAST "./-";
1656
1657
    /*
1658
     * Check the URL and remove any fragment identifier
1659
     */
1660
0
    uri = xmlParseURI((const char *)url);
1661
0
    if (uri == NULL) {
1662
0
  xmlXIncludeErr(ctxt, ref->elem, XML_XINCLUDE_HREF_URI,
1663
0
                 "invalid value URI %s\n", url);
1664
0
  goto error;
1665
0
    }
1666
0
    if (uri->fragment != NULL) {
1667
0
  xmlXIncludeErr(ctxt, ref->elem, XML_XINCLUDE_TEXT_FRAGMENT,
1668
0
                 "fragment identifier forbidden for text: %s\n",
1669
0
           (const xmlChar *) uri->fragment);
1670
0
  goto error;
1671
0
    }
1672
0
    URL = xmlSaveUri(uri);
1673
0
    if (URL == NULL) {
1674
0
  xmlXIncludeErr(ctxt, ref->elem, XML_XINCLUDE_HREF_URI,
1675
0
                 "invalid value URI %s\n", url);
1676
0
  goto error;
1677
0
    }
1678
1679
    /*
1680
     * Handling of references to the local document are done
1681
     * directly through ctxt->doc.
1682
     */
1683
0
    if (URL[0] == 0) {
1684
0
  xmlXIncludeErr(ctxt, ref->elem, XML_XINCLUDE_TEXT_DOCUMENT,
1685
0
           "text serialization of document not available\n", NULL);
1686
0
  goto error;
1687
0
    }
1688
1689
    /*
1690
     * Prevent reloading the document twice.
1691
     */
1692
0
    for (i = 0; i < ctxt->txtNr; i++) {
1693
0
  if (xmlStrEqual(URL, ctxt->txtTab[i].url)) {
1694
0
            node = xmlNewDocText(ctxt->doc, ctxt->txtTab[i].text);
1695
0
      goto loaded;
1696
0
  }
1697
0
    }
1698
1699
    /*
1700
     * Try to get the encoding if available
1701
     */
1702
0
    if (ref->elem != NULL) {
1703
0
  encoding = xmlGetProp(ref->elem, XINCLUDE_PARSE_ENCODING);
1704
0
    }
1705
0
    if (encoding != NULL) {
1706
  /*
1707
   * TODO: we should not have to remap to the xmlCharEncoding
1708
   *       predefined set, a better interface than
1709
   *       xmlParserInputBufferCreateFilename should allow any
1710
   *       encoding supported by iconv
1711
   */
1712
0
        enc = xmlParseCharEncoding((const char *) encoding);
1713
0
  if (enc == XML_CHAR_ENCODING_ERROR) {
1714
0
      xmlXIncludeErr(ctxt, ref->elem, XML_XINCLUDE_UNKNOWN_ENCODING,
1715
0
         "encoding %s not supported\n", encoding);
1716
0
      goto error;
1717
0
  }
1718
0
    }
1719
1720
    /*
1721
     * Load it.
1722
     */
1723
0
    pctxt = xmlNewParserCtxt();
1724
0
    inputStream = xmlLoadExternalEntity((const char*)URL, NULL, pctxt);
1725
0
    if(inputStream == NULL)
1726
0
  goto error;
1727
0
    buf = inputStream->buf;
1728
0
    if (buf == NULL)
1729
0
  goto error;
1730
0
    if (buf->encoder)
1731
0
  xmlCharEncCloseFunc(buf->encoder);
1732
0
    buf->encoder = xmlGetCharEncodingHandler(enc);
1733
0
    node = xmlNewDocText(ctxt->doc, NULL);
1734
0
    if (node == NULL) {
1735
0
        xmlXIncludeErrMemory(ctxt, ref->elem, NULL);
1736
0
  goto error;
1737
0
    }
1738
1739
    /*
1740
     * Scan all chars from the resource and add the to the node
1741
     */
1742
0
    while (xmlParserInputBufferRead(buf, 4096) > 0)
1743
0
        ;
1744
1745
0
    content = xmlBufContent(buf->buffer);
1746
0
    len = xmlBufLength(buf->buffer);
1747
0
    for (i = 0; i < len;) {
1748
0
        int cur;
1749
0
        int l;
1750
1751
0
        cur = xmlStringCurrentChar(NULL, &content[i], &l);
1752
0
        if (!IS_CHAR(cur)) {
1753
0
            xmlXIncludeErr(ctxt, ref->elem, XML_XINCLUDE_INVALID_CHAR,
1754
0
                           "%s contains invalid char\n", URL);
1755
0
            goto error;
1756
0
        }
1757
1758
0
        i += l;
1759
0
    }
1760
1761
0
    xmlNodeAddContentLen(node, content, len);
1762
1763
0
    if (ctxt->txtNr >= ctxt->txtMax) {
1764
0
        xmlXIncludeTxt *tmp;
1765
0
        size_t newSize = ctxt->txtMax ? ctxt->txtMax * 2 : 8;
1766
1767
0
        tmp = xmlRealloc(ctxt->txtTab, sizeof(xmlXIncludeTxt) * newSize);
1768
0
        if (tmp == NULL) {
1769
0
            xmlXIncludeErrMemory(ctxt, ref->elem,
1770
0
                                 "growing XInclude text table");
1771
0
      goto error;
1772
0
        }
1773
0
        ctxt->txtMax = newSize;
1774
0
        ctxt->txtTab = tmp;
1775
0
    }
1776
0
    ctxt->txtTab[ctxt->txtNr].text = xmlStrdup(node->content);
1777
0
    ctxt->txtTab[ctxt->txtNr].url = xmlStrdup(URL);
1778
0
    ctxt->txtNr++;
1779
1780
0
loaded:
1781
    /*
1782
     * Add the element as the replacement copy.
1783
     */
1784
0
    ref->inc = node;
1785
0
    node = NULL;
1786
0
    ret = 0;
1787
1788
0
error:
1789
0
    xmlFreeNode(node);
1790
0
    xmlFreeInputStream(inputStream);
1791
0
    xmlFreeParserCtxt(pctxt);
1792
0
    xmlFree(encoding);
1793
0
    xmlFreeURI(uri);
1794
0
    xmlFree(URL);
1795
0
    return(ret);
1796
0
}
1797
1798
/**
1799
 * xmlXIncludeLoadFallback:
1800
 * @ctxt:  the XInclude context
1801
 * @fallback:  the fallback node
1802
 * @ref:  an XMLXincludeRefPtr
1803
 *
1804
 * Load the content of the fallback node, and store the result
1805
 * in the XInclude context
1806
 *
1807
 * Returns 0 in case of success, -1 in case of failure
1808
 */
1809
static int
1810
xmlXIncludeLoadFallback(xmlXIncludeCtxtPtr ctxt, xmlNodePtr fallback,
1811
0
                        xmlXIncludeRefPtr ref) {
1812
0
    int ret = 0;
1813
0
    int oldNbErrors;
1814
1815
0
    if ((fallback == NULL) || (fallback->type == XML_NAMESPACE_DECL) ||
1816
0
        (ctxt == NULL))
1817
0
  return(-1);
1818
0
    if (fallback->children != NULL) {
1819
  /*
1820
   * It's possible that the fallback also has 'includes'
1821
   * (Bug 129969), so we re-process the fallback just in case
1822
   */
1823
0
        oldNbErrors = ctxt->nbErrors;
1824
0
  ref->inc = xmlXIncludeCopyNode(ctxt, fallback, 1);
1825
0
  if (ctxt->nbErrors > oldNbErrors)
1826
0
      ret = -1;
1827
0
        else if (ref->inc == NULL)
1828
0
            ref->emptyFb = 1;
1829
0
    } else {
1830
0
        ref->inc = NULL;
1831
0
  ref->emptyFb = 1; /* flag empty callback */
1832
0
    }
1833
0
    ref->fallback = 1;
1834
0
    return(ret);
1835
0
}
1836
1837
/************************************************************************
1838
 *                  *
1839
 *      XInclude Processing       *
1840
 *                  *
1841
 ************************************************************************/
1842
1843
/**
1844
 * xmlXIncludeExpandNode:
1845
 * @ctxt: an XInclude context
1846
 * @node: an XInclude node
1847
 *
1848
 * If the XInclude node wasn't processed yet, create a new RefPtr,
1849
 * add it to ctxt->incTab and load the included items.
1850
 *
1851
 * Returns the new or existing xmlXIncludeRefPtr, or NULL in case of error.
1852
 */
1853
static xmlXIncludeRefPtr
1854
0
xmlXIncludeExpandNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) {
1855
0
    xmlXIncludeRefPtr ref;
1856
0
    int i;
1857
1858
0
    if (ctxt->fatalErr)
1859
0
        return(NULL);
1860
0
    if (ctxt->depth >= XINCLUDE_MAX_DEPTH) {
1861
0
        xmlXIncludeErr(ctxt, node, XML_XINCLUDE_RECURSION,
1862
0
                       "maximum recursion depth exceeded\n", NULL);
1863
0
        ctxt->fatalErr = 1;
1864
0
        return(NULL);
1865
0
    }
1866
1867
0
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
1868
    /*
1869
     * The XInclude engine offers no protection against exponential
1870
     * expansion attacks similar to "billion laughs". Avoid timeouts by
1871
     * limiting the total number of replacements when fuzzing.
1872
     */
1873
0
    if (ctxt->incTotal >= 20)
1874
0
        return(NULL);
1875
0
    ctxt->incTotal++;
1876
0
#endif
1877
1878
0
    for (i = 0; i < ctxt->incNr; i++) {
1879
0
        if (ctxt->incTab[i]->elem == node) {
1880
0
            if (ctxt->incTab[i]->expanding) {
1881
0
                xmlXIncludeErr(ctxt, node, XML_XINCLUDE_RECURSION,
1882
0
                               "inclusion loop detected\n", NULL);
1883
0
                return(NULL);
1884
0
            }
1885
0
            return(ctxt->incTab[i]);
1886
0
        }
1887
0
    }
1888
1889
0
    ref = xmlXIncludeAddNode(ctxt, node);
1890
0
    if (ref == NULL)
1891
0
        return(NULL);
1892
0
    ref->expanding = 1;
1893
0
    ctxt->depth++;
1894
0
    xmlXIncludeLoadNode(ctxt, ref);
1895
0
    ctxt->depth--;
1896
0
    ref->expanding = 0;
1897
1898
0
    return(ref);
1899
0
}
1900
1901
/**
1902
 * xmlXIncludeLoadNode:
1903
 * @ctxt: an XInclude context
1904
 * @ref: an xmlXIncludeRefPtr
1905
 *
1906
 * Find and load the infoset replacement for the given node.
1907
 *
1908
 * Returns 0 if substitution succeeded, -1 if some processing failed
1909
 */
1910
static int
1911
0
xmlXIncludeLoadNode(xmlXIncludeCtxtPtr ctxt, xmlXIncludeRefPtr ref) {
1912
0
    xmlNodePtr cur;
1913
0
    xmlChar *href;
1914
0
    xmlChar *parse;
1915
0
    xmlChar *base;
1916
0
    xmlChar *oldBase;
1917
0
    xmlChar *URI;
1918
0
    int xml = 1; /* default Issue 64 */
1919
0
    int ret;
1920
1921
0
    if ((ctxt == NULL) || (ref == NULL))
1922
0
  return(-1);
1923
0
    cur = ref->elem;
1924
0
    if (cur == NULL)
1925
0
  return(-1);
1926
1927
    /*
1928
     * read the attributes
1929
     */
1930
0
    href = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_HREF);
1931
0
    if (href == NULL) {
1932
0
  href = xmlStrdup(BAD_CAST ""); /* @@@@ href is now optional */
1933
0
  if (href == NULL)
1934
0
      return(-1);
1935
0
    }
1936
0
    parse = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_PARSE);
1937
0
    if (parse != NULL) {
1938
0
  if (xmlStrEqual(parse, XINCLUDE_PARSE_XML))
1939
0
      xml = 1;
1940
0
  else if (xmlStrEqual(parse, XINCLUDE_PARSE_TEXT))
1941
0
      xml = 0;
1942
0
  else {
1943
0
      xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_PARSE_VALUE,
1944
0
         "invalid value %s for 'parse'\n", parse);
1945
0
      if (href != NULL)
1946
0
    xmlFree(href);
1947
0
      if (parse != NULL)
1948
0
    xmlFree(parse);
1949
0
      return(-1);
1950
0
  }
1951
0
    }
1952
1953
    /*
1954
     * compute the URI
1955
     */
1956
0
    base = xmlNodeGetBase(ctxt->doc, cur);
1957
0
    if (base == NULL) {
1958
0
  URI = xmlBuildURI(href, ctxt->doc->URL);
1959
0
    } else {
1960
0
  URI = xmlBuildURI(href, base);
1961
0
    }
1962
0
    if (URI == NULL) {
1963
0
  xmlChar *escbase;
1964
0
  xmlChar *eschref;
1965
  /*
1966
   * Some escaping may be needed
1967
   */
1968
0
  escbase = xmlURIEscape(base);
1969
0
  eschref = xmlURIEscape(href);
1970
0
  URI = xmlBuildURI(eschref, escbase);
1971
0
  if (escbase != NULL)
1972
0
      xmlFree(escbase);
1973
0
  if (eschref != NULL)
1974
0
      xmlFree(eschref);
1975
0
    }
1976
0
    if (URI == NULL) {
1977
0
  xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_HREF_URI,
1978
0
                       "failed build URL\n", NULL);
1979
0
  if (parse != NULL)
1980
0
      xmlFree(parse);
1981
0
  if (href != NULL)
1982
0
      xmlFree(href);
1983
0
  if (base != NULL)
1984
0
      xmlFree(base);
1985
0
  return(-1);
1986
0
    }
1987
#ifdef DEBUG_XINCLUDE
1988
    xmlGenericError(xmlGenericErrorContext, "parse: %s\n",
1989
      xml ? "xml": "text");
1990
    xmlGenericError(xmlGenericErrorContext, "URI: %s\n", URI);
1991
#endif
1992
1993
    /*
1994
     * Save the base for this include (saving the current one)
1995
     */
1996
0
    oldBase = ctxt->base;
1997
0
    ctxt->base = base;
1998
1999
0
    if (xml) {
2000
0
  ret = xmlXIncludeLoadDoc(ctxt, URI, ref);
2001
  /* xmlXIncludeGetFragment(ctxt, cur, URI); */
2002
0
    } else {
2003
0
  ret = xmlXIncludeLoadTxt(ctxt, URI, ref);
2004
0
    }
2005
2006
    /*
2007
     * Restore the original base before checking for fallback
2008
     */
2009
0
    ctxt->base = oldBase;
2010
2011
0
    if (ret < 0) {
2012
0
  xmlNodePtr children;
2013
2014
  /*
2015
   * Time to try a fallback if available
2016
   */
2017
#ifdef DEBUG_XINCLUDE
2018
  xmlGenericError(xmlGenericErrorContext, "error looking for fallback\n");
2019
#endif
2020
0
  children = cur->children;
2021
0
  while (children != NULL) {
2022
0
      if ((children->type == XML_ELEMENT_NODE) &&
2023
0
    (children->ns != NULL) &&
2024
0
    (xmlStrEqual(children->name, XINCLUDE_FALLBACK)) &&
2025
0
    ((xmlStrEqual(children->ns->href, XINCLUDE_NS)) ||
2026
0
     (xmlStrEqual(children->ns->href, XINCLUDE_OLD_NS)))) {
2027
0
    ret = xmlXIncludeLoadFallback(ctxt, children, ref);
2028
0
    break;
2029
0
      }
2030
0
      children = children->next;
2031
0
  }
2032
0
    }
2033
0
    if (ret < 0) {
2034
0
  xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_NO_FALLBACK,
2035
0
           "could not load %s, and no fallback was found\n",
2036
0
           URI);
2037
0
    }
2038
2039
    /*
2040
     * Cleanup
2041
     */
2042
0
    if (URI != NULL)
2043
0
  xmlFree(URI);
2044
0
    if (parse != NULL)
2045
0
  xmlFree(parse);
2046
0
    if (href != NULL)
2047
0
  xmlFree(href);
2048
0
    if (base != NULL)
2049
0
  xmlFree(base);
2050
0
    return(0);
2051
0
}
2052
2053
/**
2054
 * xmlXIncludeIncludeNode:
2055
 * @ctxt: an XInclude context
2056
 * @ref: an xmlXIncludeRefPtr
2057
 *
2058
 * Implement the infoset replacement for the given node
2059
 *
2060
 * Returns 0 if substitution succeeded, -1 if some processing failed
2061
 */
2062
static int
2063
0
xmlXIncludeIncludeNode(xmlXIncludeCtxtPtr ctxt, xmlXIncludeRefPtr ref) {
2064
0
    xmlNodePtr cur, end, list, tmp;
2065
2066
0
    if ((ctxt == NULL) || (ref == NULL))
2067
0
  return(-1);
2068
0
    cur = ref->elem;
2069
0
    if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL))
2070
0
  return(-1);
2071
2072
0
    list = ref->inc;
2073
0
    ref->inc = NULL;
2074
0
    ref->emptyFb = 0;
2075
2076
    /*
2077
     * Check against the risk of generating a multi-rooted document
2078
     */
2079
0
    if ((cur->parent != NULL) &&
2080
0
  (cur->parent->type != XML_ELEMENT_NODE)) {
2081
0
  int nb_elem = 0;
2082
2083
0
  tmp = list;
2084
0
  while (tmp != NULL) {
2085
0
      if (tmp->type == XML_ELEMENT_NODE)
2086
0
    nb_elem++;
2087
0
      tmp = tmp->next;
2088
0
  }
2089
0
  if (nb_elem > 1) {
2090
0
      xmlXIncludeErr(ctxt, ref->elem, XML_XINCLUDE_MULTIPLE_ROOT,
2091
0
           "XInclude error: would result in multiple root nodes\n",
2092
0
         NULL);
2093
0
            xmlFreeNodeList(list);
2094
0
      return(-1);
2095
0
  }
2096
0
    }
2097
2098
0
    if (ctxt->parseFlags & XML_PARSE_NOXINCNODE) {
2099
  /*
2100
   * Add the list of nodes
2101
   */
2102
0
  while (list != NULL) {
2103
0
      end = list;
2104
0
      list = list->next;
2105
2106
0
      xmlAddPrevSibling(cur, end);
2107
0
  }
2108
        /*
2109
         * FIXME: xmlUnlinkNode doesn't coalesce text nodes.
2110
         */
2111
0
  xmlUnlinkNode(cur);
2112
0
  xmlFreeNode(cur);
2113
0
    } else {
2114
0
        xmlNodePtr child, next;
2115
2116
  /*
2117
   * Change the current node as an XInclude start one, and add an
2118
   * XInclude end one
2119
   */
2120
0
        if (ref->fallback)
2121
0
            xmlUnsetProp(cur, BAD_CAST "href");
2122
0
  cur->type = XML_XINCLUDE_START;
2123
        /* Remove fallback children */
2124
0
        for (child = cur->children; child != NULL; child = next) {
2125
0
            next = child->next;
2126
0
            xmlUnlinkNode(child);
2127
0
            xmlFreeNode(child);
2128
0
        }
2129
0
  end = xmlNewDocNode(cur->doc, cur->ns, cur->name, NULL);
2130
0
  if (end == NULL) {
2131
0
      xmlXIncludeErr(ctxt, ref->elem, XML_XINCLUDE_BUILD_FAILED,
2132
0
         "failed to build node\n", NULL);
2133
0
            xmlFreeNodeList(list);
2134
0
      return(-1);
2135
0
  }
2136
0
  end->type = XML_XINCLUDE_END;
2137
0
  xmlAddNextSibling(cur, end);
2138
2139
  /*
2140
   * Add the list of nodes
2141
   */
2142
0
  while (list != NULL) {
2143
0
      cur = list;
2144
0
      list = list->next;
2145
2146
0
      xmlAddPrevSibling(end, cur);
2147
0
  }
2148
0
    }
2149
2150
2151
0
    return(0);
2152
0
}
2153
2154
/**
2155
 * xmlXIncludeTestNode:
2156
 * @ctxt: the XInclude processing context
2157
 * @node: an XInclude node
2158
 *
2159
 * test if the node is an XInclude node
2160
 *
2161
 * Returns 1 true, 0 otherwise
2162
 */
2163
static int
2164
0
xmlXIncludeTestNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) {
2165
0
    if (node == NULL)
2166
0
  return(0);
2167
0
    if (node->type != XML_ELEMENT_NODE)
2168
0
  return(0);
2169
0
    if (node->ns == NULL)
2170
0
  return(0);
2171
0
    if ((xmlStrEqual(node->ns->href, XINCLUDE_NS)) ||
2172
0
        (xmlStrEqual(node->ns->href, XINCLUDE_OLD_NS))) {
2173
0
  if (xmlStrEqual(node->ns->href, XINCLUDE_OLD_NS)) {
2174
0
      if (ctxt->legacy == 0) {
2175
#if 0 /* wait for the XML Core Working Group to get something stable ! */
2176
    xmlXIncludeWarn(ctxt, node, XML_XINCLUDE_DEPRECATED_NS,
2177
                 "Deprecated XInclude namespace found, use %s",
2178
                    XINCLUDE_NS);
2179
#endif
2180
0
          ctxt->legacy = 1;
2181
0
      }
2182
0
  }
2183
0
  if (xmlStrEqual(node->name, XINCLUDE_NODE)) {
2184
0
      xmlNodePtr child = node->children;
2185
0
      int nb_fallback = 0;
2186
2187
0
      while (child != NULL) {
2188
0
    if ((child->type == XML_ELEMENT_NODE) &&
2189
0
        (child->ns != NULL) &&
2190
0
        ((xmlStrEqual(child->ns->href, XINCLUDE_NS)) ||
2191
0
         (xmlStrEqual(child->ns->href, XINCLUDE_OLD_NS)))) {
2192
0
        if (xmlStrEqual(child->name, XINCLUDE_NODE)) {
2193
0
      xmlXIncludeErr(ctxt, node,
2194
0
                     XML_XINCLUDE_INCLUDE_IN_INCLUDE,
2195
0
               "%s has an 'include' child\n",
2196
0
               XINCLUDE_NODE);
2197
0
      return(0);
2198
0
        }
2199
0
        if (xmlStrEqual(child->name, XINCLUDE_FALLBACK)) {
2200
0
      nb_fallback++;
2201
0
        }
2202
0
    }
2203
0
    child = child->next;
2204
0
      }
2205
0
      if (nb_fallback > 1) {
2206
0
    xmlXIncludeErr(ctxt, node, XML_XINCLUDE_FALLBACKS_IN_INCLUDE,
2207
0
             "%s has multiple fallback children\n",
2208
0
                   XINCLUDE_NODE);
2209
0
    return(0);
2210
0
      }
2211
0
      return(1);
2212
0
  }
2213
0
  if (xmlStrEqual(node->name, XINCLUDE_FALLBACK)) {
2214
0
      if ((node->parent == NULL) ||
2215
0
    (node->parent->type != XML_ELEMENT_NODE) ||
2216
0
    (node->parent->ns == NULL) ||
2217
0
    ((!xmlStrEqual(node->parent->ns->href, XINCLUDE_NS)) &&
2218
0
     (!xmlStrEqual(node->parent->ns->href, XINCLUDE_OLD_NS))) ||
2219
0
    (!xmlStrEqual(node->parent->name, XINCLUDE_NODE))) {
2220
0
    xmlXIncludeErr(ctxt, node,
2221
0
                   XML_XINCLUDE_FALLBACK_NOT_IN_INCLUDE,
2222
0
             "%s is not the child of an 'include'\n",
2223
0
             XINCLUDE_FALLBACK);
2224
0
      }
2225
0
  }
2226
0
    }
2227
0
    return(0);
2228
0
}
2229
2230
/**
2231
 * xmlXIncludeDoProcess:
2232
 * @ctxt: the XInclude processing context
2233
 * @tree: the top of the tree to process
2234
 *
2235
 * Implement the XInclude substitution on the XML document @doc
2236
 *
2237
 * Returns 0 if no substitution were done, -1 if some processing failed
2238
 *    or the number of substitutions done.
2239
 */
2240
static int
2241
0
xmlXIncludeDoProcess(xmlXIncludeCtxtPtr ctxt, xmlNodePtr tree) {
2242
0
    xmlXIncludeRefPtr ref;
2243
0
    xmlNodePtr cur;
2244
0
    int ret = 0;
2245
0
    int i, start;
2246
2247
0
    if ((tree == NULL) || (tree->type == XML_NAMESPACE_DECL))
2248
0
  return(-1);
2249
0
    if (ctxt == NULL)
2250
0
  return(-1);
2251
2252
    /*
2253
     * First phase: lookup the elements in the document
2254
     */
2255
0
    start = ctxt->incNr;
2256
0
    cur = tree;
2257
0
    do {
2258
  /* TODO: need to work on entities -> stack */
2259
0
        if (xmlXIncludeTestNode(ctxt, cur) == 1) {
2260
0
            ref = xmlXIncludeExpandNode(ctxt, cur);
2261
            /*
2262
             * Mark direct includes.
2263
             */
2264
0
            if (ref != NULL)
2265
0
                ref->replace = 1;
2266
0
        } else if ((cur->children != NULL) &&
2267
0
                   ((cur->type == XML_DOCUMENT_NODE) ||
2268
0
                    (cur->type == XML_ELEMENT_NODE))) {
2269
0
            cur = cur->children;
2270
0
            continue;
2271
0
        }
2272
0
        do {
2273
0
            if (cur == tree)
2274
0
                break;
2275
0
            if (cur->next != NULL) {
2276
0
                cur = cur->next;
2277
0
                break;
2278
0
            }
2279
0
            cur = cur->parent;
2280
0
        } while (cur != NULL);
2281
0
    } while ((cur != NULL) && (cur != tree));
2282
2283
    /*
2284
     * Second phase: extend the original document infoset.
2285
     */
2286
0
    for (i = start; i < ctxt->incNr; i++) {
2287
0
  if (ctxt->incTab[i]->replace != 0) {
2288
0
            if ((ctxt->incTab[i]->inc != NULL) ||
2289
0
                (ctxt->incTab[i]->emptyFb != 0)) { /* (empty fallback) */
2290
0
                xmlXIncludeIncludeNode(ctxt, ctxt->incTab[i]);
2291
0
            }
2292
0
            ctxt->incTab[i]->replace = 0;
2293
0
        } else {
2294
            /*
2295
             * Ignore includes which were added indirectly, for example
2296
             * inside xi:fallback elements.
2297
             */
2298
0
            if (ctxt->incTab[i]->inc != NULL) {
2299
0
                xmlFreeNodeList(ctxt->incTab[i]->inc);
2300
0
                ctxt->incTab[i]->inc = NULL;
2301
0
            }
2302
0
        }
2303
0
  ret++;
2304
0
    }
2305
2306
0
    if (ctxt->isStream) {
2307
        /*
2308
         * incTab references nodes which will eventually be deleted in
2309
         * streaming mode. The table is only required for XPointer
2310
         * expressions which aren't allowed in streaming mode.
2311
         */
2312
0
        for (i = 0;i < ctxt->incNr;i++) {
2313
0
            xmlXIncludeFreeRef(ctxt->incTab[i]);
2314
0
        }
2315
0
        ctxt->incNr = 0;
2316
0
    }
2317
2318
0
    return(ret);
2319
0
}
2320
2321
/**
2322
 * xmlXIncludeSetFlags:
2323
 * @ctxt:  an XInclude processing context
2324
 * @flags: a set of xmlParserOption used for parsing XML includes
2325
 *
2326
 * Set the flags used for further processing of XML resources.
2327
 *
2328
 * Returns 0 in case of success and -1 in case of error.
2329
 */
2330
int
2331
0
xmlXIncludeSetFlags(xmlXIncludeCtxtPtr ctxt, int flags) {
2332
0
    if (ctxt == NULL)
2333
0
        return(-1);
2334
0
    ctxt->parseFlags = flags;
2335
0
    return(0);
2336
0
}
2337
2338
/**
2339
 * xmlXIncludeSetStreamingMode:
2340
 * @ctxt:  an XInclude processing context
2341
 * @mode:  whether streaming mode should be enabled
2342
 *
2343
 * In streaming mode, XPointer expressions aren't allowed.
2344
 *
2345
 * Returns 0 in case of success and -1 in case of error.
2346
 */
2347
int
2348
0
xmlXIncludeSetStreamingMode(xmlXIncludeCtxtPtr ctxt, int mode) {
2349
0
    if (ctxt == NULL)
2350
0
        return(-1);
2351
0
    ctxt->isStream = !!mode;
2352
0
    return(0);
2353
0
}
2354
2355
/**
2356
 * xmlXIncludeProcessTreeFlagsData:
2357
 * @tree: an XML node
2358
 * @flags: a set of xmlParserOption used for parsing XML includes
2359
 * @data: application data that will be passed to the parser context
2360
 *        in the _private field of the parser context(s)
2361
 *
2362
 * Implement the XInclude substitution on the XML node @tree
2363
 *
2364
 * Returns 0 if no substitution were done, -1 if some processing failed
2365
 *    or the number of substitutions done.
2366
 */
2367
2368
int
2369
0
xmlXIncludeProcessTreeFlagsData(xmlNodePtr tree, int flags, void *data) {
2370
0
    xmlXIncludeCtxtPtr ctxt;
2371
0
    int ret = 0;
2372
2373
0
    if ((tree == NULL) || (tree->type == XML_NAMESPACE_DECL) ||
2374
0
        (tree->doc == NULL))
2375
0
        return(-1);
2376
2377
0
    ctxt = xmlXIncludeNewContext(tree->doc);
2378
0
    if (ctxt == NULL)
2379
0
        return(-1);
2380
0
    ctxt->_private = data;
2381
0
    ctxt->base = xmlStrdup((xmlChar *)tree->doc->URL);
2382
0
    xmlXIncludeSetFlags(ctxt, flags);
2383
0
    ret = xmlXIncludeDoProcess(ctxt, tree);
2384
0
    if ((ret >= 0) && (ctxt->nbErrors > 0))
2385
0
        ret = -1;
2386
2387
0
    xmlXIncludeFreeContext(ctxt);
2388
0
    return(ret);
2389
0
}
2390
2391
/**
2392
 * xmlXIncludeProcessFlagsData:
2393
 * @doc: an XML document
2394
 * @flags: a set of xmlParserOption used for parsing XML includes
2395
 * @data: application data that will be passed to the parser context
2396
 *        in the _private field of the parser context(s)
2397
 *
2398
 * Implement the XInclude substitution on the XML document @doc
2399
 *
2400
 * Returns 0 if no substitution were done, -1 if some processing failed
2401
 *    or the number of substitutions done.
2402
 */
2403
int
2404
0
xmlXIncludeProcessFlagsData(xmlDocPtr doc, int flags, void *data) {
2405
0
    xmlNodePtr tree;
2406
2407
0
    if (doc == NULL)
2408
0
  return(-1);
2409
0
    tree = xmlDocGetRootElement(doc);
2410
0
    if (tree == NULL)
2411
0
  return(-1);
2412
0
    return(xmlXIncludeProcessTreeFlagsData(tree, flags, data));
2413
0
}
2414
2415
/**
2416
 * xmlXIncludeProcessFlags:
2417
 * @doc: an XML document
2418
 * @flags: a set of xmlParserOption used for parsing XML includes
2419
 *
2420
 * Implement the XInclude substitution on the XML document @doc
2421
 *
2422
 * Returns 0 if no substitution were done, -1 if some processing failed
2423
 *    or the number of substitutions done.
2424
 */
2425
int
2426
0
xmlXIncludeProcessFlags(xmlDocPtr doc, int flags) {
2427
0
    return xmlXIncludeProcessFlagsData(doc, flags, NULL);
2428
0
}
2429
2430
/**
2431
 * xmlXIncludeProcess:
2432
 * @doc: an XML document
2433
 *
2434
 * Implement the XInclude substitution on the XML document @doc
2435
 *
2436
 * Returns 0 if no substitution were done, -1 if some processing failed
2437
 *    or the number of substitutions done.
2438
 */
2439
int
2440
0
xmlXIncludeProcess(xmlDocPtr doc) {
2441
0
    return(xmlXIncludeProcessFlags(doc, 0));
2442
0
}
2443
2444
/**
2445
 * xmlXIncludeProcessTreeFlags:
2446
 * @tree: a node in an XML document
2447
 * @flags: a set of xmlParserOption used for parsing XML includes
2448
 *
2449
 * Implement the XInclude substitution for the given subtree
2450
 *
2451
 * Returns 0 if no substitution were done, -1 if some processing failed
2452
 *    or the number of substitutions done.
2453
 */
2454
int
2455
0
xmlXIncludeProcessTreeFlags(xmlNodePtr tree, int flags) {
2456
0
    xmlXIncludeCtxtPtr ctxt;
2457
0
    int ret = 0;
2458
2459
0
    if ((tree == NULL) || (tree->type == XML_NAMESPACE_DECL) ||
2460
0
        (tree->doc == NULL))
2461
0
  return(-1);
2462
0
    ctxt = xmlXIncludeNewContext(tree->doc);
2463
0
    if (ctxt == NULL)
2464
0
  return(-1);
2465
0
    ctxt->base = xmlNodeGetBase(tree->doc, tree);
2466
0
    xmlXIncludeSetFlags(ctxt, flags);
2467
0
    ret = xmlXIncludeDoProcess(ctxt, tree);
2468
0
    if ((ret >= 0) && (ctxt->nbErrors > 0))
2469
0
  ret = -1;
2470
2471
0
    xmlXIncludeFreeContext(ctxt);
2472
0
    return(ret);
2473
0
}
2474
2475
/**
2476
 * xmlXIncludeProcessTree:
2477
 * @tree: a node in an XML document
2478
 *
2479
 * Implement the XInclude substitution for the given subtree
2480
 *
2481
 * Returns 0 if no substitution were done, -1 if some processing failed
2482
 *    or the number of substitutions done.
2483
 */
2484
int
2485
0
xmlXIncludeProcessTree(xmlNodePtr tree) {
2486
0
    return(xmlXIncludeProcessTreeFlags(tree, 0));
2487
0
}
2488
2489
/**
2490
 * xmlXIncludeProcessNode:
2491
 * @ctxt: an existing XInclude context
2492
 * @node: a node in an XML document
2493
 *
2494
 * Implement the XInclude substitution for the given subtree reusing
2495
 * the information and data coming from the given context.
2496
 *
2497
 * Returns 0 if no substitution were done, -1 if some processing failed
2498
 *    or the number of substitutions done.
2499
 */
2500
int
2501
0
xmlXIncludeProcessNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) {
2502
0
    int ret = 0;
2503
2504
0
    if ((node == NULL) || (node->type == XML_NAMESPACE_DECL) ||
2505
0
        (node->doc == NULL) || (ctxt == NULL))
2506
0
  return(-1);
2507
0
    ret = xmlXIncludeDoProcess(ctxt, node);
2508
0
    if ((ret >= 0) && (ctxt->nbErrors > 0))
2509
0
  ret = -1;
2510
0
    return(ret);
2511
0
}
2512
2513
#else /* !LIBXML_XINCLUDE_ENABLED */
2514
#endif