Coverage Report

Created: 2026-01-10 06:17

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