blob: 4f5b68461370779dc0030d8cb921c288de20343c [file] [log] [blame]
Owen Taylor3473f882001-02-23 17:55:21 +00001/*
2 * xinclude.c : Code to implement XInclude processing
3 *
Daniel Veillarde74d2e12003-12-09 11:35:37 +00004 * World Wide Web Consortium W3C Last Call Working Draft 10 November 2003
5 * http://www.w3.org/TR/2003/WD-xinclude-20031110
Owen Taylor3473f882001-02-23 17:55:21 +00006 *
7 * See Copyright for the status of this software.
8 *
Daniel Veillardc5d64342001-06-24 12:13:24 +00009 * daniel@veillard.com
Owen Taylor3473f882001-02-23 17:55:21 +000010 */
11
Daniel Veillard34ce8be2002-03-18 19:37:11 +000012#define IN_LIBXML
Bjorn Reese70a9da52001-04-21 16:57:29 +000013#include "libxml.h"
Owen Taylor3473f882001-02-23 17:55:21 +000014
Owen Taylor3473f882001-02-23 17:55:21 +000015#include <string.h>
16#include <libxml/xmlmemory.h>
17#include <libxml/tree.h>
18#include <libxml/parser.h>
19#include <libxml/uri.h>
Nicolas Le Cam41586ca2013-06-17 13:01:33 +020020#include <libxml/xpath.h>
Owen Taylor3473f882001-02-23 17:55:21 +000021#include <libxml/xpointer.h>
22#include <libxml/parserInternals.h>
Owen Taylor3473f882001-02-23 17:55:21 +000023#include <libxml/xmlerror.h>
Daniel Veillardd076a202002-11-20 13:28:31 +000024#include <libxml/encoding.h>
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000025#include <libxml/globals.h>
Owen Taylor3473f882001-02-23 17:55:21 +000026
27#ifdef LIBXML_XINCLUDE_ENABLED
28#include <libxml/xinclude.h>
29
Daniel Veillard345ee8b2012-07-16 14:40:37 +080030#include "buf.h"
Owen Taylor3473f882001-02-23 17:55:21 +000031
Daniel Veillardf4b4f982003-02-13 11:02:08 +000032#define XINCLUDE_MAX_DEPTH 40
33
Daniel Veillard98485322003-08-14 15:44:40 +000034/* #define DEBUG_XINCLUDE */
Daniel Veillard017b1082001-06-21 11:20:21 +000035#ifdef DEBUG_XINCLUDE
36#ifdef LIBXML_DEBUG_ENABLED
37#include <libxml/debugXML.h>
38#endif
39#endif
Owen Taylor3473f882001-02-23 17:55:21 +000040
41/************************************************************************
42 * *
William M. Brack72ee48d2003-12-30 08:30:19 +000043 * XInclude context handling *
Owen Taylor3473f882001-02-23 17:55:21 +000044 * *
45 ************************************************************************/
46
47/*
48 * An XInclude context
49 */
Daniel Veillardedac3c92001-02-26 01:36:19 +000050typedef xmlChar *xmlURL;
Daniel Veillardbbc72c32002-09-05 10:52:10 +000051
52typedef struct _xmlXIncludeRef xmlXIncludeRef;
53typedef xmlXIncludeRef *xmlXIncludeRefPtr;
54struct _xmlXIncludeRef {
William M. Brack72ee48d2003-12-30 08:30:19 +000055 xmlChar *URI; /* the fully resolved resource URL */
Daniel Veillardbbc72c32002-09-05 10:52:10 +000056 xmlChar *fragment; /* the fragment in the URI */
57 xmlDocPtr doc; /* the parsed document */
58 xmlNodePtr ref; /* the node making the reference in the source */
59 xmlNodePtr inc; /* the included copy */
60 int xml; /* xml or txt */
61 int count; /* how many refs use that specific doc */
Haibo Huangf0a546b2020-09-01 20:28:19 -070062 int fallback; /* fallback was loaded */
William M. Brack95af5942004-02-08 04:12:49 +000063 int emptyFb; /* flag to show fallback empty */
Daniel Veillardbbc72c32002-09-05 10:52:10 +000064};
65
Owen Taylor3473f882001-02-23 17:55:21 +000066struct _xmlXIncludeCtxt {
67 xmlDocPtr doc; /* the source document */
Daniel Veillardbbc72c32002-09-05 10:52:10 +000068 int incBase; /* the first include for this document */
Owen Taylor3473f882001-02-23 17:55:21 +000069 int incNr; /* number of includes */
70 int incMax; /* size of includes tab */
Daniel Veillardbbc72c32002-09-05 10:52:10 +000071 xmlXIncludeRefPtr *incTab; /* array of included references */
72
Owen Taylor3473f882001-02-23 17:55:21 +000073 int txtNr; /* number of unparsed documents */
74 int txtMax; /* size of unparsed documents tab */
Haibo Huangd23e46c2020-10-28 22:26:09 -070075 xmlChar * *txtTab; /* array of unparsed text strings */
William M. Brack72ee48d2003-12-30 08:30:19 +000076 xmlURL *txturlTab; /* array of unparsed text URLs */
Daniel Veillardd581b7e2003-02-11 18:03:05 +000077
Daniel Veillardf4b4f982003-02-13 11:02:08 +000078 xmlChar * url; /* the current URL processed */
William M. Brack72ee48d2003-12-30 08:30:19 +000079 int urlNr; /* number of URLs stacked */
80 int urlMax; /* size of URL stack */
81 xmlChar * *urlTab; /* URL stack */
Daniel Veillardf4b4f982003-02-13 11:02:08 +000082
Daniel Veillardd581b7e2003-02-11 18:03:05 +000083 int nbErrors; /* the number of errors detected */
Daniel Veillardb5fa0202003-12-08 17:41:29 +000084 int legacy; /* using XINCLUDE_OLD_NS */
Daniel Veillarde74d2e12003-12-09 11:35:37 +000085 int parseFlags; /* the flags used for parsing XML documents */
William M. Brackf7789b12004-06-07 08:57:27 +000086 xmlChar * base; /* the current xml:base */
Daniel Veillard681e9042006-09-29 09:16:00 +000087
88 void *_private; /* application data */
Haibo Huangf0a546b2020-09-01 20:28:19 -070089
90 unsigned long incTotal; /* total number of processed inclusions */
Owen Taylor3473f882001-02-23 17:55:21 +000091};
92
Daniel Veillardd16df9f2001-05-23 13:44:21 +000093static int
Haibo Huangf0a546b2020-09-01 20:28:19 -070094xmlXIncludeDoProcess(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr tree,
95 int skipRoot);
Owen Taylor3473f882001-02-23 17:55:21 +000096
Daniel Veillard98485322003-08-14 15:44:40 +000097
Daniel Veillardcd6ff282003-10-08 22:38:13 +000098/************************************************************************
99 * *
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800100 * XInclude error handler *
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000101 * *
102 ************************************************************************/
103
Daniel Veillard98485322003-08-14 15:44:40 +0000104/**
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000105 * xmlXIncludeErrMemory:
William M. Brack72ee48d2003-12-30 08:30:19 +0000106 * @extra: extra information
Daniel Veillard98485322003-08-14 15:44:40 +0000107 *
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000108 * Handle an out of memory condition
Daniel Veillard98485322003-08-14 15:44:40 +0000109 */
110static void
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000111xmlXIncludeErrMemory(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node,
112 const char *extra)
Daniel Veillard98485322003-08-14 15:44:40 +0000113{
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000114 if (ctxt != NULL)
115 ctxt->nbErrors++;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000116 __xmlRaiseError(NULL, NULL, NULL, ctxt, node, XML_FROM_XINCLUDE,
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000117 XML_ERR_NO_MEMORY, XML_ERR_ERROR, NULL, 0,
118 extra, NULL, NULL, 0, 0,
119 "Memory allocation failed : %s\n", extra);
120}
Daniel Veillard98485322003-08-14 15:44:40 +0000121
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000122/**
123 * xmlXIncludeErr:
124 * @ctxt: the XInclude context
125 * @node: the context node
126 * @msg: the error message
William M. Brack72ee48d2003-12-30 08:30:19 +0000127 * @extra: extra information
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000128 *
Daniel Veillardb5fa0202003-12-08 17:41:29 +0000129 * Handle an XInclude error
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000130 */
David Kilzer4472c3a2016-05-13 15:13:17 +0800131static void LIBXML_ATTR_FORMAT(4,0)
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000132xmlXIncludeErr(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node, int error,
133 const char *msg, const xmlChar *extra)
134{
135 if (ctxt != NULL)
136 ctxt->nbErrors++;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000137 __xmlRaiseError(NULL, NULL, NULL, ctxt, node, XML_FROM_XINCLUDE,
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000138 error, XML_ERR_ERROR, NULL, 0,
139 (const char *) extra, NULL, NULL, 0, 0,
140 msg, (const char *) extra);
Daniel Veillard98485322003-08-14 15:44:40 +0000141}
142
Daniel Veillardf54cd532004-02-25 11:52:31 +0000143#if 0
Owen Taylor3473f882001-02-23 17:55:21 +0000144/**
Daniel Veillardb5fa0202003-12-08 17:41:29 +0000145 * xmlXIncludeWarn:
146 * @ctxt: the XInclude context
147 * @node: the context node
148 * @msg: the error message
William M. Brack72ee48d2003-12-30 08:30:19 +0000149 * @extra: extra information
Daniel Veillardb5fa0202003-12-08 17:41:29 +0000150 *
151 * Emit an XInclude warning.
152 */
David Kilzer4472c3a2016-05-13 15:13:17 +0800153static void LIBXML_ATTR_FORMAT(4,0)
Daniel Veillardb5fa0202003-12-08 17:41:29 +0000154xmlXIncludeWarn(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node, int error,
155 const char *msg, const xmlChar *extra)
156{
157 __xmlRaiseError(NULL, NULL, NULL, ctxt, node, XML_FROM_XINCLUDE,
158 error, XML_ERR_WARNING, NULL, 0,
159 (const char *) extra, NULL, NULL, 0, 0,
160 msg, (const char *) extra);
161}
Daniel Veillardf54cd532004-02-25 11:52:31 +0000162#endif
Daniel Veillardb5fa0202003-12-08 17:41:29 +0000163
164/**
165 * xmlXIncludeGetProp:
166 * @ctxt: the XInclude context
167 * @cur: the node
168 * @name: the attribute name
169 *
170 * Get an XInclude attribute
171 *
172 * Returns the value (to be freed) or NULL if not found
173 */
174static xmlChar *
175xmlXIncludeGetProp(xmlXIncludeCtxtPtr ctxt, xmlNodePtr cur,
176 const xmlChar *name) {
177 xmlChar *ret;
178
179 ret = xmlGetNsProp(cur, XINCLUDE_NS, name);
180 if (ret != NULL)
181 return(ret);
182 if (ctxt->legacy != 0) {
183 ret = xmlGetNsProp(cur, XINCLUDE_OLD_NS, name);
184 if (ret != NULL)
185 return(ret);
186 }
187 ret = xmlGetProp(cur, name);
188 return(ret);
189}
190/**
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000191 * xmlXIncludeFreeRef:
192 * @ref: the XInclude reference
193 *
194 * Free an XInclude reference
195 */
196static void
197xmlXIncludeFreeRef(xmlXIncludeRefPtr ref) {
198 if (ref == NULL)
199 return;
200#ifdef DEBUG_XINCLUDE
201 xmlGenericError(xmlGenericErrorContext, "Freeing ref\n");
202#endif
203 if (ref->doc != NULL) {
204#ifdef DEBUG_XINCLUDE
205 xmlGenericError(xmlGenericErrorContext, "Freeing doc %s\n", ref->URI);
206#endif
207 xmlFreeDoc(ref->doc);
208 }
209 if (ref->URI != NULL)
210 xmlFree(ref->URI);
211 if (ref->fragment != NULL)
212 xmlFree(ref->fragment);
213 xmlFree(ref);
214}
215
216/**
217 * xmlXIncludeNewRef:
218 * @ctxt: the XInclude context
219 * @URI: the resource URI
220 *
221 * Creates a new reference within an XInclude context
222 *
223 * Returns the new set
224 */
225static xmlXIncludeRefPtr
226xmlXIncludeNewRef(xmlXIncludeCtxtPtr ctxt, const xmlChar *URI,
227 xmlNodePtr ref) {
228 xmlXIncludeRefPtr ret;
229
230#ifdef DEBUG_XINCLUDE
231 xmlGenericError(xmlGenericErrorContext, "New ref %s\n", URI);
232#endif
233 ret = (xmlXIncludeRefPtr) xmlMalloc(sizeof(xmlXIncludeRef));
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000234 if (ret == NULL) {
235 xmlXIncludeErrMemory(ctxt, ref, "growing XInclude context");
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000236 return(NULL);
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000237 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000238 memset(ret, 0, sizeof(xmlXIncludeRef));
239 if (URI == NULL)
240 ret->URI = NULL;
241 else
242 ret->URI = xmlStrdup(URI);
243 ret->fragment = NULL;
244 ret->ref = ref;
Daniel Veillard24505b02005-07-28 23:49:35 +0000245 ret->doc = NULL;
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000246 ret->count = 0;
247 ret->xml = 0;
248 ret->inc = NULL;
249 if (ctxt->incMax == 0) {
250 ctxt->incMax = 4;
251 ctxt->incTab = (xmlXIncludeRefPtr *) xmlMalloc(ctxt->incMax *
252 sizeof(ctxt->incTab[0]));
253 if (ctxt->incTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000254 xmlXIncludeErrMemory(ctxt, ref, "growing XInclude context");
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000255 xmlXIncludeFreeRef(ret);
256 return(NULL);
257 }
258 }
259 if (ctxt->incNr >= ctxt->incMax) {
260 ctxt->incMax *= 2;
261 ctxt->incTab = (xmlXIncludeRefPtr *) xmlRealloc(ctxt->incTab,
262 ctxt->incMax * sizeof(ctxt->incTab[0]));
263 if (ctxt->incTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000264 xmlXIncludeErrMemory(ctxt, ref, "growing XInclude context");
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000265 xmlXIncludeFreeRef(ret);
266 return(NULL);
267 }
268 }
269 ctxt->incTab[ctxt->incNr++] = ret;
270 return(ret);
271}
272
273/**
Owen Taylor3473f882001-02-23 17:55:21 +0000274 * xmlXIncludeNewContext:
275 * @doc: an XML Document
276 *
277 * Creates a new XInclude context
278 *
279 * Returns the new set
280 */
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000281xmlXIncludeCtxtPtr
Owen Taylor3473f882001-02-23 17:55:21 +0000282xmlXIncludeNewContext(xmlDocPtr doc) {
283 xmlXIncludeCtxtPtr ret;
284
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000285#ifdef DEBUG_XINCLUDE
286 xmlGenericError(xmlGenericErrorContext, "New context\n");
287#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000288 if (doc == NULL)
289 return(NULL);
290 ret = (xmlXIncludeCtxtPtr) xmlMalloc(sizeof(xmlXIncludeCtxt));
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000291 if (ret == NULL) {
292 xmlXIncludeErrMemory(NULL, (xmlNodePtr) doc,
293 "creating XInclude context");
Owen Taylor3473f882001-02-23 17:55:21 +0000294 return(NULL);
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000295 }
Owen Taylor3473f882001-02-23 17:55:21 +0000296 memset(ret, 0, sizeof(xmlXIncludeCtxt));
297 ret->doc = doc;
298 ret->incNr = 0;
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000299 ret->incBase = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000300 ret->incMax = 0;
301 ret->incTab = NULL;
Daniel Veillardd581b7e2003-02-11 18:03:05 +0000302 ret->nbErrors = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000303 return(ret);
304}
305
306/**
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000307 * xmlXIncludeURLPush:
308 * @ctxt: the parser context
309 * @value: the url
310 *
311 * Pushes a new url on top of the url stack
312 *
313 * Returns -1 in case of error, the index in the stack otherwise
314 */
315static int
316xmlXIncludeURLPush(xmlXIncludeCtxtPtr ctxt,
317 const xmlChar *value)
318{
319 if (ctxt->urlNr > XINCLUDE_MAX_DEPTH) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000320 xmlXIncludeErr(ctxt, NULL, XML_XINCLUDE_RECURSION,
321 "detected a recursion in %s\n", value);
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000322 return(-1);
323 }
324 if (ctxt->urlTab == NULL) {
325 ctxt->urlMax = 4;
326 ctxt->urlNr = 0;
327 ctxt->urlTab = (xmlChar * *) xmlMalloc(
328 ctxt->urlMax * sizeof(ctxt->urlTab[0]));
329 if (ctxt->urlTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000330 xmlXIncludeErrMemory(ctxt, NULL, "adding URL");
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000331 return (-1);
332 }
333 }
334 if (ctxt->urlNr >= ctxt->urlMax) {
335 ctxt->urlMax *= 2;
336 ctxt->urlTab =
337 (xmlChar * *) xmlRealloc(ctxt->urlTab,
338 ctxt->urlMax *
339 sizeof(ctxt->urlTab[0]));
340 if (ctxt->urlTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000341 xmlXIncludeErrMemory(ctxt, NULL, "adding URL");
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000342 return (-1);
343 }
344 }
345 ctxt->url = ctxt->urlTab[ctxt->urlNr] = xmlStrdup(value);
346 return (ctxt->urlNr++);
347}
348
349/**
350 * xmlXIncludeURLPop:
351 * @ctxt: the parser context
352 *
William M. Brack72ee48d2003-12-30 08:30:19 +0000353 * Pops the top URL from the URL stack
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000354 */
355static void
356xmlXIncludeURLPop(xmlXIncludeCtxtPtr ctxt)
357{
358 xmlChar * ret;
359
360 if (ctxt->urlNr <= 0)
361 return;
362 ctxt->urlNr--;
363 if (ctxt->urlNr > 0)
364 ctxt->url = ctxt->urlTab[ctxt->urlNr - 1];
365 else
366 ctxt->url = NULL;
367 ret = ctxt->urlTab[ctxt->urlNr];
Daniel Veillard24505b02005-07-28 23:49:35 +0000368 ctxt->urlTab[ctxt->urlNr] = NULL;
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000369 if (ret != NULL)
370 xmlFree(ret);
371}
372
373/**
Owen Taylor3473f882001-02-23 17:55:21 +0000374 * xmlXIncludeFreeContext:
375 * @ctxt: the XInclude context
376 *
377 * Free an XInclude context
378 */
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000379void
Owen Taylor3473f882001-02-23 17:55:21 +0000380xmlXIncludeFreeContext(xmlXIncludeCtxtPtr ctxt) {
381 int i;
382
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000383#ifdef DEBUG_XINCLUDE
384 xmlGenericError(xmlGenericErrorContext, "Freeing context\n");
385#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000386 if (ctxt == NULL)
387 return;
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000388 while (ctxt->urlNr > 0)
389 xmlXIncludeURLPop(ctxt);
390 if (ctxt->urlTab != NULL)
391 xmlFree(ctxt->urlTab);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000392 for (i = 0;i < ctxt->incNr;i++) {
393 if (ctxt->incTab[i] != NULL)
394 xmlXIncludeFreeRef(ctxt->incTab[i]);
Owen Taylor3473f882001-02-23 17:55:21 +0000395 }
Haibo Huangd23e46c2020-10-28 22:26:09 -0700396 if (ctxt->incTab != NULL)
397 xmlFree(ctxt->incTab);
398 if (ctxt->txtTab != NULL) {
399 for (i = 0;i < ctxt->txtNr;i++) {
400 if (ctxt->txtTab[i] != NULL)
401 xmlFree(ctxt->txtTab[i]);
402 }
403 xmlFree(ctxt->txtTab);
404 }
Daniel Veillard11ce4002006-03-10 00:36:23 +0000405 if (ctxt->txturlTab != NULL) {
406 for (i = 0;i < ctxt->txtNr;i++) {
407 if (ctxt->txturlTab[i] != NULL)
408 xmlFree(ctxt->txturlTab[i]);
409 }
Owen Taylor3473f882001-02-23 17:55:21 +0000410 xmlFree(ctxt->txturlTab);
Haibo Huangd23e46c2020-10-28 22:26:09 -0700411 }
Daniel Veillardce244ad2004-11-05 10:03:46 +0000412 if (ctxt->base != NULL) {
413 xmlFree(ctxt->base);
414 }
Owen Taylor3473f882001-02-23 17:55:21 +0000415 xmlFree(ctxt);
416}
417
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000418/**
Daniel Veillard98485322003-08-14 15:44:40 +0000419 * xmlXIncludeParseFile:
420 * @ctxt: the XInclude context
421 * @URL: the URL or file path
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800422 *
William M. Brack72ee48d2003-12-30 08:30:19 +0000423 * parse a document for XInclude
Daniel Veillard98485322003-08-14 15:44:40 +0000424 */
425static xmlDocPtr
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000426xmlXIncludeParseFile(xmlXIncludeCtxtPtr ctxt, const char *URL) {
Daniel Veillard98485322003-08-14 15:44:40 +0000427 xmlDocPtr ret;
428 xmlParserCtxtPtr pctxt;
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000429 xmlParserInputPtr inputStream;
Daniel Veillard98485322003-08-14 15:44:40 +0000430
431 xmlInitParser();
432
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000433 pctxt = xmlNewParserCtxt();
Daniel Veillard98485322003-08-14 15:44:40 +0000434 if (pctxt == NULL) {
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000435 xmlXIncludeErrMemory(ctxt, NULL, "cannot allocate parser context");
Daniel Veillard98485322003-08-14 15:44:40 +0000436 return(NULL);
437 }
Daniel Veillard681e9042006-09-29 09:16:00 +0000438
439 /*
440 * pass in the application data to the parser context.
441 */
442 pctxt->_private = ctxt->_private;
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800443
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000444 /*
William M. Brack72ee48d2003-12-30 08:30:19 +0000445 * try to ensure that new documents included are actually
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000446 * built with the same dictionary as the including document.
447 */
Daniel Veillardcb6f5252009-08-25 19:24:15 +0200448 if ((ctxt->doc != NULL) && (ctxt->doc->dict != NULL)) {
449 if (pctxt->dict != NULL)
450 xmlDictFree(pctxt->dict);
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000451 pctxt->dict = ctxt->doc->dict;
452 xmlDictReference(pctxt->dict);
453 }
454
455 xmlCtxtUseOptions(pctxt, ctxt->parseFlags | XML_PARSE_DTDLOAD);
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800456
Haibo Huangcfd91dc2020-07-30 23:01:33 -0700457 /* Don't read from stdin. */
458 if ((URL != NULL) && (strcmp(URL, "-") == 0))
459 URL = "./-";
460
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000461 inputStream = xmlLoadExternalEntity(URL, NULL, pctxt);
462 if (inputStream == NULL) {
463 xmlFreeParserCtxt(pctxt);
464 return(NULL);
465 }
466
467 inputPush(pctxt, inputStream);
Daniel Veillard98485322003-08-14 15:44:40 +0000468
Daniel Veillard37d2d162008-03-14 10:54:00 +0000469 if (pctxt->directory == NULL)
470 pctxt->directory = xmlParserGetDirectory(URL);
Daniel Veillard98485322003-08-14 15:44:40 +0000471
William M. Bracka22da292005-02-12 01:08:22 +0000472 pctxt->loadsubset |= XML_DETECT_IDS;
Daniel Veillard98485322003-08-14 15:44:40 +0000473
474 xmlParseDocument(pctxt);
475
William M. Brack1ff42132003-12-31 14:05:15 +0000476 if (pctxt->wellFormed) {
Daniel Veillard98485322003-08-14 15:44:40 +0000477 ret = pctxt->myDoc;
William M. Brack1ff42132003-12-31 14:05:15 +0000478 }
Daniel Veillard98485322003-08-14 15:44:40 +0000479 else {
480 ret = NULL;
Daniel Veillard500a1de2004-03-22 15:22:58 +0000481 if (pctxt->myDoc != NULL)
Daniel Veillard4773df22004-01-23 13:15:13 +0000482 xmlFreeDoc(pctxt->myDoc);
Daniel Veillard98485322003-08-14 15:44:40 +0000483 pctxt->myDoc = NULL;
484 }
485 xmlFreeParserCtxt(pctxt);
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800486
Daniel Veillard98485322003-08-14 15:44:40 +0000487 return(ret);
488}
489
490/**
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000491 * xmlXIncludeAddNode:
492 * @ctxt: the XInclude context
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000493 * @cur: the new node
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800494 *
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000495 * Add a new node to process to an XInclude context
496 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000497static int
498xmlXIncludeAddNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr cur) {
499 xmlXIncludeRefPtr ref;
500 xmlURIPtr uri;
501 xmlChar *URL;
502 xmlChar *fragment = NULL;
503 xmlChar *href;
504 xmlChar *parse;
505 xmlChar *base;
506 xmlChar *URI;
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000507 int xml = 1, i; /* default Issue 64 */
508 int local = 0;
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000509
510
511 if (ctxt == NULL)
512 return(-1);
513 if (cur == NULL)
514 return(-1);
515
516#ifdef DEBUG_XINCLUDE
517 xmlGenericError(xmlGenericErrorContext, "Add node\n");
518#endif
519 /*
520 * read the attributes
521 */
Daniel Veillardb5fa0202003-12-08 17:41:29 +0000522 href = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_HREF);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000523 if (href == NULL) {
Daniel Veillardb5fa0202003-12-08 17:41:29 +0000524 href = xmlStrdup(BAD_CAST ""); /* @@@@ href is now optional */
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800525 if (href == NULL)
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000526 return(-1);
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000527 }
Daniel Veillardb242b082008-02-08 09:56:31 +0000528 if ((href[0] == '#') || (href[0] == 0))
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000529 local = 1;
Daniel Veillardb5fa0202003-12-08 17:41:29 +0000530 parse = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_PARSE);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000531 if (parse != NULL) {
532 if (xmlStrEqual(parse, XINCLUDE_PARSE_XML))
533 xml = 1;
534 else if (xmlStrEqual(parse, XINCLUDE_PARSE_TEXT))
535 xml = 0;
536 else {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000537 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_PARSE_VALUE,
538 "invalid value %s for 'parse'\n", parse);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000539 if (href != NULL)
540 xmlFree(href);
541 if (parse != NULL)
542 xmlFree(parse);
543 return(-1);
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000544 }
545 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000546
547 /*
548 * compute the URI
549 */
550 base = xmlNodeGetBase(ctxt->doc, cur);
551 if (base == NULL) {
552 URI = xmlBuildURI(href, ctxt->doc->URL);
553 } else {
554 URI = xmlBuildURI(href, base);
555 }
556 if (URI == NULL) {
557 xmlChar *escbase;
558 xmlChar *eschref;
559 /*
560 * Some escaping may be needed
561 */
562 escbase = xmlURIEscape(base);
563 eschref = xmlURIEscape(href);
564 URI = xmlBuildURI(eschref, escbase);
565 if (escbase != NULL)
566 xmlFree(escbase);
567 if (eschref != NULL)
568 xmlFree(eschref);
569 }
570 if (parse != NULL)
571 xmlFree(parse);
572 if (href != NULL)
573 xmlFree(href);
574 if (base != NULL)
575 xmlFree(base);
576 if (URI == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000577 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_HREF_URI,
578 "failed build URL\n", NULL);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000579 return(-1);
580 }
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000581 fragment = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_PARSE_XPOINTER);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000582
583 /*
584 * Check the URL and remove any fragment identifier
585 */
586 uri = xmlParseURI((const char *)URI);
587 if (uri == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000588 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_HREF_URI,
589 "invalid value URI %s\n", URI);
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000590 if (fragment != NULL)
591 xmlFree(fragment);
592 xmlFree(URI);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000593 return(-1);
594 }
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000595
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000596 if (uri->fragment != NULL) {
Daniel Veillarde74d2e12003-12-09 11:35:37 +0000597 if (ctxt->legacy != 0) {
598 if (fragment == NULL) {
599 fragment = (xmlChar *) uri->fragment;
600 } else {
601 xmlFree(uri->fragment);
602 }
603 } else {
604 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_FRAGMENT_ID,
605 "Invalid fragment identifier in URI %s use the xpointer attribute\n",
606 URI);
607 if (fragment != NULL)
608 xmlFree(fragment);
609 xmlFreeURI(uri);
610 xmlFree(URI);
611 return(-1);
612 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000613 uri->fragment = NULL;
614 }
615 URL = xmlSaveUri(uri);
616 xmlFreeURI(uri);
617 xmlFree(URI);
618 if (URL == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000619 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_HREF_URI,
620 "invalid value URI %s\n", URI);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000621 if (fragment != NULL)
622 xmlFree(fragment);
623 return(-1);
624 }
625
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000626 /*
Daniel Veillardb242b082008-02-08 09:56:31 +0000627 * If local and xml then we need a fragment
628 */
629 if ((local == 1) && (xml == 1) &&
630 ((fragment == NULL) || (fragment[0] == 0))) {
631 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_RECURSION,
632 "detected a local recursion with no xpointer in %s\n",
633 URL);
Haibo Huangf0a546b2020-09-01 20:28:19 -0700634 xmlFree(URL);
635 xmlFree(fragment);
Daniel Veillardb242b082008-02-08 09:56:31 +0000636 return(-1);
637 }
638
639 /*
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000640 * Check the URL against the stack for recursions
641 */
Daniel Veillardbf630c02006-06-06 08:21:41 +0000642 if ((!local) && (xml == 1)) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000643 for (i = 0;i < ctxt->urlNr;i++) {
644 if (xmlStrEqual(URL, ctxt->urlTab[i])) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000645 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_RECURSION,
646 "detected a recursion in %s\n", URL);
Haibo Huangf0a546b2020-09-01 20:28:19 -0700647 xmlFree(URL);
648 xmlFree(fragment);
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000649 return(-1);
650 }
651 }
652 }
653
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000654 ref = xmlXIncludeNewRef(ctxt, URL, cur);
Haibo Huangf0a546b2020-09-01 20:28:19 -0700655 xmlFree(URL);
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000656 if (ref == NULL) {
657 return(-1);
658 }
659 ref->fragment = fragment;
660 ref->doc = NULL;
661 ref->xml = xml;
662 ref->count = 1;
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000663 return(0);
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000664}
665
666/**
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000667 * xmlXIncludeRecurseDoc:
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000668 * @ctxt: the XInclude context
669 * @doc: the new document
670 * @url: the associated URL
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800671 *
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000672 * The XInclude recursive nature is handled at this point.
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000673 */
674static void
Daniel Veillard118aed72002-09-24 14:13:13 +0000675xmlXIncludeRecurseDoc(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc,
Daniel Veillarddda8f1b2002-09-26 09:47:36 +0000676 const xmlURL url ATTRIBUTE_UNUSED) {
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000677 xmlXIncludeCtxtPtr newctxt;
678 int i;
679
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000680 /*
Haibo Huangcfd91dc2020-07-30 23:01:33 -0700681 * Avoid recursion in already substituted resources
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000682 for (i = 0;i < ctxt->urlNr;i++) {
683 if (xmlStrEqual(doc->URL, ctxt->urlTab[i]))
684 return;
685 }
686 */
687
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000688#ifdef DEBUG_XINCLUDE
689 xmlGenericError(xmlGenericErrorContext, "Recursing in doc %s\n", doc->URL);
690#endif
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000691 /*
692 * Handle recursion here.
693 */
694
695 newctxt = xmlXIncludeNewContext(doc);
696 if (newctxt != NULL) {
697 /*
Daniel Veillarda6585822006-12-04 09:21:28 +0000698 * Copy the private user data
699 */
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800700 newctxt->_private = ctxt->_private;
Daniel Veillarda6585822006-12-04 09:21:28 +0000701 /*
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000702 * Copy the existing document set
703 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000704 newctxt->incMax = ctxt->incMax;
705 newctxt->incNr = ctxt->incNr;
706 newctxt->incTab = (xmlXIncludeRefPtr *) xmlMalloc(newctxt->incMax *
707 sizeof(newctxt->incTab[0]));
708 if (newctxt->incTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000709 xmlXIncludeErrMemory(ctxt, (xmlNodePtr) doc, "processing doc");
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000710 xmlFree(newctxt);
711 return;
712 }
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000713 /*
714 * copy the urlTab
715 */
716 newctxt->urlMax = ctxt->urlMax;
717 newctxt->urlNr = ctxt->urlNr;
718 newctxt->urlTab = ctxt->urlTab;
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000719
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000720 /*
William M. Brackf7789b12004-06-07 08:57:27 +0000721 * Inherit the existing base
722 */
Daniel Veillardce244ad2004-11-05 10:03:46 +0000723 newctxt->base = xmlStrdup(ctxt->base);
William M. Brackf7789b12004-06-07 08:57:27 +0000724
725 /*
William M. Brack72ee48d2003-12-30 08:30:19 +0000726 * Inherit the documents already in use by other includes
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000727 */
728 newctxt->incBase = ctxt->incNr;
729 for (i = 0;i < ctxt->incNr;i++) {
730 newctxt->incTab[i] = ctxt->incTab[i];
731 newctxt->incTab[i]->count++; /* prevent the recursion from
732 freeing it */
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000733 }
William M. Bracka11e4832004-03-07 11:03:43 +0000734 /*
735 * The new context should also inherit the Parse Flags
736 * (bug 132597)
737 */
738 newctxt->parseFlags = ctxt->parseFlags;
Haibo Huangf0a546b2020-09-01 20:28:19 -0700739 newctxt->incTotal = ctxt->incTotal;
740 xmlXIncludeDoProcess(newctxt, doc, xmlDocGetRootElement(doc), 0);
741 ctxt->incTotal = newctxt->incTotal;
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000742 for (i = 0;i < ctxt->incNr;i++) {
743 newctxt->incTab[i]->count--;
744 newctxt->incTab[i] = NULL;
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000745 }
Daniel Veillardd9b72832003-03-27 14:24:00 +0000746
747 /* urlTab may have been reallocated */
748 ctxt->urlTab = newctxt->urlTab;
749 ctxt->urlMax = newctxt->urlMax;
750
Daniel Veillardf4b4f982003-02-13 11:02:08 +0000751 newctxt->urlMax = 0;
752 newctxt->urlNr = 0;
753 newctxt->urlTab = NULL;
754
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000755 xmlXIncludeFreeContext(newctxt);
756 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000757#ifdef DEBUG_XINCLUDE
758 xmlGenericError(xmlGenericErrorContext, "Done recursing in doc %s\n", url);
759#endif
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000760}
761
762/**
763 * xmlXIncludeAddTxt:
764 * @ctxt: the XInclude context
765 * @txt: the new text node
766 * @url: the associated URL
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800767 *
Haibo Huangcfd91dc2020-07-30 23:01:33 -0700768 * Add a new text node to the list
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000769 */
770static void
Haibo Huangd23e46c2020-10-28 22:26:09 -0700771xmlXIncludeAddTxt(xmlXIncludeCtxtPtr ctxt, const xmlChar *txt,
772 const xmlURL url) {
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000773#ifdef DEBUG_XINCLUDE
774 xmlGenericError(xmlGenericErrorContext, "Adding text %s\n", url);
775#endif
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000776 if (ctxt->txtMax == 0) {
777 ctxt->txtMax = 4;
Haibo Huangd23e46c2020-10-28 22:26:09 -0700778 ctxt->txtTab = (xmlChar **) xmlMalloc(ctxt->txtMax *
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000779 sizeof(ctxt->txtTab[0]));
780 if (ctxt->txtTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000781 xmlXIncludeErrMemory(ctxt, NULL, "processing text");
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000782 return;
783 }
784 ctxt->txturlTab = (xmlURL *) xmlMalloc(ctxt->txtMax *
785 sizeof(ctxt->txturlTab[0]));
786 if (ctxt->txturlTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000787 xmlXIncludeErrMemory(ctxt, NULL, "processing text");
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000788 return;
789 }
790 }
791 if (ctxt->txtNr >= ctxt->txtMax) {
792 ctxt->txtMax *= 2;
Haibo Huangd23e46c2020-10-28 22:26:09 -0700793 ctxt->txtTab = (xmlChar **) xmlRealloc(ctxt->txtTab,
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000794 ctxt->txtMax * sizeof(ctxt->txtTab[0]));
795 if (ctxt->txtTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000796 xmlXIncludeErrMemory(ctxt, NULL, "processing text");
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000797 return;
798 }
799 ctxt->txturlTab = (xmlURL *) xmlRealloc(ctxt->txturlTab,
Daniel Veillardbbc72c32002-09-05 10:52:10 +0000800 ctxt->txtMax * sizeof(ctxt->txturlTab[0]));
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000801 if (ctxt->txturlTab == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000802 xmlXIncludeErrMemory(ctxt, NULL, "processing text");
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000803 return;
804 }
805 }
Haibo Huangd23e46c2020-10-28 22:26:09 -0700806 ctxt->txtTab[ctxt->txtNr] = xmlStrdup(txt);
Daniel Veillardd16df9f2001-05-23 13:44:21 +0000807 ctxt->txturlTab[ctxt->txtNr] = xmlStrdup(url);
808 ctxt->txtNr++;
809}
810
Owen Taylor3473f882001-02-23 17:55:21 +0000811/************************************************************************
812 * *
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000813 * Node copy with specific semantic *
814 * *
815 ************************************************************************/
816
Daniel Veillardcb6f5252009-08-25 19:24:15 +0200817static xmlNodePtr
818xmlXIncludeCopyNodeList(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
819 xmlDocPtr source, xmlNodePtr elem);
820
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000821/**
822 * xmlXIncludeCopyNode:
823 * @ctxt: the XInclude context
824 * @target: the document target
825 * @source: the document source
826 * @elem: the element
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800827 *
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000828 * Make a copy of the node while preserving the XInclude semantic
829 * of the Infoset copy
830 */
831static xmlNodePtr
832xmlXIncludeCopyNode(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
833 xmlDocPtr source, xmlNodePtr elem) {
834 xmlNodePtr result = NULL;
835
836 if ((ctxt == NULL) || (target == NULL) || (source == NULL) ||
837 (elem == NULL))
838 return(NULL);
839 if (elem->type == XML_DTD_NODE)
840 return(NULL);
Daniel Veillardcb6f5252009-08-25 19:24:15 +0200841 if (elem->type == XML_DOCUMENT_NODE)
842 result = xmlXIncludeCopyNodeList(ctxt, target, source, elem->children);
843 else
844 result = xmlDocCopyNode(elem, target, 1);
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000845 return(result);
846}
847
848/**
849 * xmlXIncludeCopyNodeList:
850 * @ctxt: the XInclude context
851 * @target: the document target
852 * @source: the document source
853 * @elem: the element list
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800854 *
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000855 * Make a copy of the node list while preserving the XInclude semantic
856 * of the Infoset copy
857 */
858static xmlNodePtr
859xmlXIncludeCopyNodeList(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
860 xmlDocPtr source, xmlNodePtr elem) {
861 xmlNodePtr cur, res, result = NULL, last = NULL;
862
863 if ((ctxt == NULL) || (target == NULL) || (source == NULL) ||
864 (elem == NULL))
865 return(NULL);
866 cur = elem;
867 while (cur != NULL) {
868 res = xmlXIncludeCopyNode(ctxt, target, source, cur);
869 if (res != NULL) {
870 if (result == NULL) {
871 result = last = res;
872 } else {
873 last->next = res;
874 res->prev = last;
875 last = res;
876 }
877 }
878 cur = cur->next;
879 }
880 return(result);
881}
882
883/**
William M. Brack72ee48d2003-12-30 08:30:19 +0000884 * xmlXIncludeGetNthChild:
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000885 * @cur: the node
886 * @no: the child number
887 *
William M. Brack72ee48d2003-12-30 08:30:19 +0000888 * Returns the @n'th element child of @cur or NULL
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000889 */
890static xmlNodePtr
891xmlXIncludeGetNthChild(xmlNodePtr cur, int no) {
892 int i;
Daniel Veillard3e62adb2012-08-09 14:24:02 +0800893 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL))
894 return(NULL);
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000895 cur = cur->children;
896 for (i = 0;i <= no;cur = cur->next) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800897 if (cur == NULL)
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000898 return(cur);
899 if ((cur->type == XML_ELEMENT_NODE) ||
900 (cur->type == XML_DOCUMENT_NODE) ||
901 (cur->type == XML_HTML_DOCUMENT_NODE)) {
902 i++;
903 if (i == no)
904 break;
905 }
906 }
907 return(cur);
908}
909
William M. Brackf7eb7942003-12-31 07:59:17 +0000910xmlNodePtr xmlXPtrAdvanceNode(xmlNodePtr cur, int *level); /* in xpointer.c */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000911/**
912 * xmlXIncludeCopyRange:
913 * @ctxt: the XInclude context
914 * @target: the document target
915 * @source: the document source
916 * @obj: the XPointer result from the evaluation.
917 *
918 * Build a node list tree copy of the XPointer result.
919 *
920 * Returns an xmlNodePtr list or NULL.
William M. Brack72ee48d2003-12-30 08:30:19 +0000921 * The caller has to free the node tree.
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000922 */
923static xmlNodePtr
924xmlXIncludeCopyRange(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
925 xmlDocPtr source, xmlXPathObjectPtr range) {
926 /* pointers to generated nodes */
William M. Brackf7eb7942003-12-31 07:59:17 +0000927 xmlNodePtr list = NULL, last = NULL, listParent = NULL;
928 xmlNodePtr tmp, tmp2;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000929 /* pointers to traversal nodes */
930 xmlNodePtr start, cur, end;
931 int index1, index2;
William M. Brack6bdacd72004-02-07 08:53:23 +0000932 int level = 0, lastLevel = 0, endLevel = 0, endFlag = 0;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000933
934 if ((ctxt == NULL) || (target == NULL) || (source == NULL) ||
935 (range == NULL))
936 return(NULL);
937 if (range->type != XPATH_RANGE)
938 return(NULL);
939 start = (xmlNodePtr) range->user;
940
Daniel Veillard3e62adb2012-08-09 14:24:02 +0800941 if ((start == NULL) || (start->type == XML_NAMESPACE_DECL))
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000942 return(NULL);
943 end = range->user2;
944 if (end == NULL)
945 return(xmlDocCopyNode(start, target, 1));
Daniel Veillard3e62adb2012-08-09 14:24:02 +0800946 if (end->type == XML_NAMESPACE_DECL)
947 return(NULL);
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000948
949 cur = start;
950 index1 = range->index;
951 index2 = range->index2;
William M. Brackf7eb7942003-12-31 07:59:17 +0000952 /*
953 * level is depth of the current node under consideration
954 * list is the pointer to the root of the output tree
955 * listParent is a pointer to the parent of output tree (within
956 the included file) in case we need to add another level
957 * last is a pointer to the last node added to the output tree
958 * lastLevel is the depth of last (relative to the root)
959 */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000960 while (cur != NULL) {
William M. Brackf7eb7942003-12-31 07:59:17 +0000961 /*
962 * Check if our output tree needs a parent
963 */
964 if (level < 0) {
965 while (level < 0) {
William M. Brack57e9e912004-03-09 16:19:02 +0000966 /* copy must include namespaces and properties */
967 tmp2 = xmlDocCopyNode(listParent, target, 2);
William M. Brackf7eb7942003-12-31 07:59:17 +0000968 xmlAddChild(tmp2, list);
969 list = tmp2;
970 listParent = listParent->parent;
971 level++;
972 }
973 last = list;
974 lastLevel = 0;
975 }
976 /*
977 * Check whether we need to change our insertion point
978 */
979 while (level < lastLevel) {
980 last = last->parent;
981 lastLevel --;
982 }
William M. Brack72ee48d2003-12-30 08:30:19 +0000983 if (cur == end) { /* Are we at the end of the range? */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000984 if (cur->type == XML_TEXT_NODE) {
985 const xmlChar *content = cur->content;
986 int len;
987
988 if (content == NULL) {
989 tmp = xmlNewTextLen(NULL, 0);
990 } else {
991 len = index2;
992 if ((cur == start) && (index1 > 1)) {
993 content += (index1 - 1);
994 len -= (index1 - 1);
Daniel Veillardc5f05ad2002-02-10 11:57:22 +0000995 } else {
996 len = index2;
997 }
998 tmp = xmlNewTextLen(content, len);
999 }
1000 /* single sub text node selection */
1001 if (list == NULL)
1002 return(tmp);
1003 /* prune and return full set */
William M. Brackf7eb7942003-12-31 07:59:17 +00001004 if (level == lastLevel)
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001005 xmlAddNextSibling(last, tmp);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001006 else
William M. Brackf7eb7942003-12-31 07:59:17 +00001007 xmlAddChild(last, tmp);
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001008 return(list);
William M. Brack72ee48d2003-12-30 08:30:19 +00001009 } else { /* ending node not a text node */
William M. Brack6bdacd72004-02-07 08:53:23 +00001010 endLevel = level; /* remember the level of the end node */
1011 endFlag = 1;
William M. Brack57e9e912004-03-09 16:19:02 +00001012 /* last node - need to take care of properties + namespaces */
1013 tmp = xmlDocCopyNode(cur, target, 2);
William M. Brackf7eb7942003-12-31 07:59:17 +00001014 if (list == NULL) {
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001015 list = tmp;
William M. Brackf7eb7942003-12-31 07:59:17 +00001016 listParent = cur->parent;
Elliott Hughesecdab2a2022-02-23 14:33:50 -08001017 last = tmp;
William M. Brackf7eb7942003-12-31 07:59:17 +00001018 } else {
1019 if (level == lastLevel)
Elliott Hughesecdab2a2022-02-23 14:33:50 -08001020 last = xmlAddNextSibling(last, tmp);
William M. Brackf7eb7942003-12-31 07:59:17 +00001021 else {
Elliott Hughesecdab2a2022-02-23 14:33:50 -08001022 last = xmlAddChild(last, tmp);
William M. Brackf7eb7942003-12-31 07:59:17 +00001023 lastLevel = level;
1024 }
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001025 }
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001026
1027 if (index2 > 1) {
1028 end = xmlXIncludeGetNthChild(cur, index2 - 1);
1029 index2 = 0;
1030 }
1031 if ((cur == start) && (index1 > 1)) {
1032 cur = xmlXIncludeGetNthChild(cur, index1 - 1);
1033 index1 = 0;
William M. Brack6bdacd72004-02-07 08:53:23 +00001034 } else {
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001035 cur = cur->children;
1036 }
William M. Brack6bdacd72004-02-07 08:53:23 +00001037 level++; /* increment level to show change */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001038 /*
1039 * Now gather the remaining nodes from cur to end
1040 */
William M. Brack6bdacd72004-02-07 08:53:23 +00001041 continue; /* while */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001042 }
William M. Brackf7eb7942003-12-31 07:59:17 +00001043 } else if (cur == start) { /* Not at the end, are we at start? */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001044 if ((cur->type == XML_TEXT_NODE) ||
1045 (cur->type == XML_CDATA_SECTION_NODE)) {
1046 const xmlChar *content = cur->content;
1047
1048 if (content == NULL) {
1049 tmp = xmlNewTextLen(NULL, 0);
1050 } else {
1051 if (index1 > 1) {
1052 content += (index1 - 1);
William M. Brack72ee48d2003-12-30 08:30:19 +00001053 index1 = 0;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001054 }
1055 tmp = xmlNewText(content);
1056 }
1057 last = list = tmp;
William M. Brackf7eb7942003-12-31 07:59:17 +00001058 listParent = cur->parent;
William M. Brack72ee48d2003-12-30 08:30:19 +00001059 } else { /* Not text node */
William M. Brack57e9e912004-03-09 16:19:02 +00001060 /*
1061 * start of the range - need to take care of
1062 * properties and namespaces
1063 */
1064 tmp = xmlDocCopyNode(cur, target, 2);
William M. Brackf7eb7942003-12-31 07:59:17 +00001065 list = last = tmp;
1066 listParent = cur->parent;
William M. Brack72ee48d2003-12-30 08:30:19 +00001067 if (index1 > 1) { /* Do we need to position? */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001068 cur = xmlXIncludeGetNthChild(cur, index1 - 1);
William M. Brackf7eb7942003-12-31 07:59:17 +00001069 level = lastLevel = 1;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001070 index1 = 0;
1071 /*
1072 * Now gather the remaining nodes from cur to end
1073 */
1074 continue; /* while */
1075 }
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001076 }
1077 } else {
1078 tmp = NULL;
1079 switch (cur->type) {
1080 case XML_DTD_NODE:
1081 case XML_ELEMENT_DECL:
1082 case XML_ATTRIBUTE_DECL:
1083 case XML_ENTITY_NODE:
Haibo Huangcfd91dc2020-07-30 23:01:33 -07001084 /* Do not copy DTD information */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001085 break;
1086 case XML_ENTITY_DECL:
1087 /* handle crossing entities -> stack needed */
1088 break;
1089 case XML_XINCLUDE_START:
1090 case XML_XINCLUDE_END:
1091 /* don't consider it part of the tree content */
1092 break;
1093 case XML_ATTRIBUTE_NODE:
1094 /* Humm, should not happen ! */
1095 break;
1096 default:
William M. Brack57e9e912004-03-09 16:19:02 +00001097 /*
1098 * Middle of the range - need to take care of
1099 * properties and namespaces
1100 */
1101 tmp = xmlDocCopyNode(cur, target, 2);
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001102 break;
1103 }
1104 if (tmp != NULL) {
William M. Brackf7eb7942003-12-31 07:59:17 +00001105 if (level == lastLevel)
Elliott Hughesecdab2a2022-02-23 14:33:50 -08001106 last = xmlAddNextSibling(last, tmp);
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001107 else {
Elliott Hughesecdab2a2022-02-23 14:33:50 -08001108 last = xmlAddChild(last, tmp);
William M. Brackf7eb7942003-12-31 07:59:17 +00001109 lastLevel = level;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001110 }
1111 }
1112 }
1113 /*
1114 * Skip to next node in document order
1115 */
William M. Brackf7eb7942003-12-31 07:59:17 +00001116 cur = xmlXPtrAdvanceNode(cur, &level);
William M. Brack6bdacd72004-02-07 08:53:23 +00001117 if (endFlag && (level >= endLevel))
1118 break;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001119 }
1120 return(list);
1121}
1122
1123/**
1124 * xmlXIncludeBuildNodeList:
1125 * @ctxt: the XInclude context
1126 * @target: the document target
1127 * @source: the document source
1128 * @obj: the XPointer result from the evaluation.
1129 *
1130 * Build a node list tree copy of the XPointer result.
1131 * This will drop Attributes and Namespace declarations.
1132 *
1133 * Returns an xmlNodePtr list or NULL.
1134 * the caller has to free the node tree.
1135 */
1136static xmlNodePtr
1137xmlXIncludeCopyXPointer(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
1138 xmlDocPtr source, xmlXPathObjectPtr obj) {
1139 xmlNodePtr list = NULL, last = NULL;
1140 int i;
1141
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001142 if (source == NULL)
1143 source = ctxt->doc;
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001144 if ((ctxt == NULL) || (target == NULL) || (source == NULL) ||
1145 (obj == NULL))
1146 return(NULL);
1147 switch (obj->type) {
1148 case XPATH_NODESET: {
1149 xmlNodeSetPtr set = obj->nodesetval;
1150 if (set == NULL)
1151 return(NULL);
1152 for (i = 0;i < set->nodeNr;i++) {
1153 if (set->nodeTab[i] == NULL)
1154 continue;
1155 switch (set->nodeTab[i]->type) {
1156 case XML_TEXT_NODE:
1157 case XML_CDATA_SECTION_NODE:
1158 case XML_ELEMENT_NODE:
1159 case XML_ENTITY_REF_NODE:
1160 case XML_ENTITY_NODE:
1161 case XML_PI_NODE:
1162 case XML_COMMENT_NODE:
1163 case XML_DOCUMENT_NODE:
1164 case XML_HTML_DOCUMENT_NODE:
1165#ifdef LIBXML_DOCB_ENABLED
1166 case XML_DOCB_DOCUMENT_NODE:
1167#endif
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001168 case XML_XINCLUDE_END:
1169 break;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001170 case XML_XINCLUDE_START: {
1171 xmlNodePtr tmp, cur = set->nodeTab[i];
1172
1173 cur = cur->next;
1174 while (cur != NULL) {
1175 switch(cur->type) {
1176 case XML_TEXT_NODE:
1177 case XML_CDATA_SECTION_NODE:
1178 case XML_ELEMENT_NODE:
1179 case XML_ENTITY_REF_NODE:
1180 case XML_ENTITY_NODE:
1181 case XML_PI_NODE:
1182 case XML_COMMENT_NODE:
1183 tmp = xmlXIncludeCopyNode(ctxt, target,
1184 source, cur);
1185 if (last == NULL) {
1186 list = last = tmp;
1187 } else {
Elliott Hughesecdab2a2022-02-23 14:33:50 -08001188 last = xmlAddNextSibling(last, tmp);
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001189 }
1190 cur = cur->next;
1191 continue;
1192 default:
1193 break;
1194 }
1195 break;
1196 }
1197 continue;
1198 }
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001199 case XML_ATTRIBUTE_NODE:
1200 case XML_NAMESPACE_DECL:
1201 case XML_DOCUMENT_TYPE_NODE:
1202 case XML_DOCUMENT_FRAG_NODE:
1203 case XML_NOTATION_NODE:
1204 case XML_DTD_NODE:
1205 case XML_ELEMENT_DECL:
1206 case XML_ATTRIBUTE_DECL:
1207 case XML_ENTITY_DECL:
1208 continue; /* for */
1209 }
1210 if (last == NULL)
1211 list = last = xmlXIncludeCopyNode(ctxt, target, source,
1212 set->nodeTab[i]);
1213 else {
1214 xmlAddNextSibling(last,
1215 xmlXIncludeCopyNode(ctxt, target, source,
1216 set->nodeTab[i]));
1217 if (last->next != NULL)
1218 last = last->next;
1219 }
1220 }
1221 break;
1222 }
Nicolas Le Cam41586ca2013-06-17 13:01:33 +02001223#ifdef LIBXML_XPTR_ENABLED
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001224 case XPATH_LOCATIONSET: {
1225 xmlLocationSetPtr set = (xmlLocationSetPtr) obj->user;
1226 if (set == NULL)
1227 return(NULL);
1228 for (i = 0;i < set->locNr;i++) {
1229 if (last == NULL)
1230 list = last = xmlXIncludeCopyXPointer(ctxt, target, source,
1231 set->locTab[i]);
1232 else
1233 xmlAddNextSibling(last,
1234 xmlXIncludeCopyXPointer(ctxt, target, source,
1235 set->locTab[i]));
1236 if (last != NULL) {
1237 while (last->next != NULL)
1238 last = last->next;
1239 }
1240 }
1241 break;
1242 }
1243 case XPATH_RANGE:
1244 return(xmlXIncludeCopyRange(ctxt, target, source, obj));
Daniel Veillard10acc2f2003-09-01 20:59:40 +00001245#endif
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001246 case XPATH_POINT:
1247 /* points are ignored in XInclude */
1248 break;
1249 default:
1250 break;
1251 }
1252 return(list);
1253}
1254/************************************************************************
1255 * *
Owen Taylor3473f882001-02-23 17:55:21 +00001256 * XInclude I/O handling *
1257 * *
1258 ************************************************************************/
1259
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001260typedef struct _xmlXIncludeMergeData xmlXIncludeMergeData;
1261typedef xmlXIncludeMergeData *xmlXIncludeMergeDataPtr;
1262struct _xmlXIncludeMergeData {
1263 xmlDocPtr doc;
1264 xmlXIncludeCtxtPtr ctxt;
1265};
1266
Owen Taylor3473f882001-02-23 17:55:21 +00001267/**
Daniel Veillard4287c572003-02-04 22:48:53 +00001268 * xmlXIncludeMergeOneEntity:
1269 * @ent: the entity
1270 * @doc: the including doc
1271 * @nr: the entity name
1272 *
Haibo Huangcfd91dc2020-07-30 23:01:33 -07001273 * Implements the merge of one entity
Daniel Veillard4287c572003-02-04 22:48:53 +00001274 */
1275static void
Nick Wellnhofere03f0a12017-11-09 16:42:47 +01001276xmlXIncludeMergeEntity(void *payload, void *vdata,
1277 const xmlChar *name ATTRIBUTE_UNUSED) {
1278 xmlEntityPtr ent = (xmlEntityPtr) payload;
1279 xmlXIncludeMergeDataPtr data = (xmlXIncludeMergeDataPtr) vdata;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001280 xmlEntityPtr ret, prev;
1281 xmlDocPtr doc;
1282 xmlXIncludeCtxtPtr ctxt;
Daniel Veillard4287c572003-02-04 22:48:53 +00001283
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001284 if ((ent == NULL) || (data == NULL))
Daniel Veillard4287c572003-02-04 22:48:53 +00001285 return;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001286 ctxt = data->ctxt;
1287 doc = data->doc;
1288 if ((ctxt == NULL) || (doc == NULL))
1289 return;
1290 switch (ent->etype) {
1291 case XML_INTERNAL_PARAMETER_ENTITY:
1292 case XML_EXTERNAL_PARAMETER_ENTITY:
1293 case XML_INTERNAL_PREDEFINED_ENTITY:
1294 return;
1295 case XML_INTERNAL_GENERAL_ENTITY:
1296 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
1297 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
1298 break;
1299 }
Daniel Veillard4287c572003-02-04 22:48:53 +00001300 ret = xmlAddDocEntity(doc, ent->name, ent->etype, ent->ExternalID,
1301 ent->SystemID, ent->content);
1302 if (ret != NULL) {
1303 if (ent->URI != NULL)
1304 ret->URI = xmlStrdup(ent->URI);
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001305 } else {
1306 prev = xmlGetDocEntity(doc, ent->name);
1307 if (prev != NULL) {
1308 if (ent->etype != prev->etype)
1309 goto error;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001310
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001311 if ((ent->SystemID != NULL) && (prev->SystemID != NULL)) {
1312 if (!xmlStrEqual(ent->SystemID, prev->SystemID))
1313 goto error;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001314 } else if ((ent->ExternalID != NULL) &&
1315 (prev->ExternalID != NULL)) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001316 if (!xmlStrEqual(ent->ExternalID, prev->ExternalID))
1317 goto error;
Daniel Veillard2406abd2003-02-24 18:16:47 +00001318 } else if ((ent->content != NULL) && (prev->content != NULL)) {
1319 if (!xmlStrEqual(ent->content, prev->content))
1320 goto error;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001321 } else {
1322 goto error;
1323 }
1324
1325 }
Daniel Veillard4287c572003-02-04 22:48:53 +00001326 }
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001327 return;
1328error:
Daniel Veillarda507fbf2003-03-31 16:09:37 +00001329 switch (ent->etype) {
1330 case XML_INTERNAL_PARAMETER_ENTITY:
1331 case XML_EXTERNAL_PARAMETER_ENTITY:
1332 case XML_INTERNAL_PREDEFINED_ENTITY:
1333 case XML_INTERNAL_GENERAL_ENTITY:
1334 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
1335 return;
1336 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
1337 break;
1338 }
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001339 xmlXIncludeErr(ctxt, (xmlNodePtr) ent, XML_XINCLUDE_ENTITY_DEF_MISMATCH,
1340 "mismatch in redefinition of entity %s\n",
1341 ent->name);
Daniel Veillard4287c572003-02-04 22:48:53 +00001342}
1343
1344/**
1345 * xmlXIncludeMergeEntities:
1346 * @ctxt: an XInclude context
1347 * @doc: the including doc
1348 * @from: the included doc
1349 *
Haibo Huangcfd91dc2020-07-30 23:01:33 -07001350 * Implements the entity merge
Daniel Veillard4287c572003-02-04 22:48:53 +00001351 *
1352 * Returns 0 if merge succeeded, -1 if some processing failed
1353 */
1354static int
1355xmlXIncludeMergeEntities(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc,
1356 xmlDocPtr from) {
1357 xmlNodePtr cur;
1358 xmlDtdPtr target, source;
1359
1360 if (ctxt == NULL)
1361 return(-1);
1362
1363 if ((from == NULL) || (from->intSubset == NULL))
1364 return(0);
1365
1366 target = doc->intSubset;
1367 if (target == NULL) {
1368 cur = xmlDocGetRootElement(doc);
1369 if (cur == NULL)
1370 return(-1);
1371 target = xmlCreateIntSubset(doc, cur->name, NULL, NULL);
1372 if (target == NULL)
1373 return(-1);
1374 }
1375
1376 source = from->intSubset;
1377 if ((source != NULL) && (source->entities != NULL)) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001378 xmlXIncludeMergeData data;
1379
1380 data.ctxt = ctxt;
1381 data.doc = doc;
1382
Daniel Veillard4287c572003-02-04 22:48:53 +00001383 xmlHashScan((xmlHashTablePtr) source->entities,
Nick Wellnhofere03f0a12017-11-09 16:42:47 +01001384 xmlXIncludeMergeEntity, &data);
Daniel Veillard4287c572003-02-04 22:48:53 +00001385 }
1386 source = from->extSubset;
1387 if ((source != NULL) && (source->entities != NULL)) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001388 xmlXIncludeMergeData data;
1389
1390 data.ctxt = ctxt;
1391 data.doc = doc;
1392
Daniel Veillard4287c572003-02-04 22:48:53 +00001393 /*
1394 * don't duplicate existing stuff when external subsets are the same
1395 */
1396 if ((!xmlStrEqual(target->ExternalID, source->ExternalID)) &&
1397 (!xmlStrEqual(target->SystemID, source->SystemID))) {
1398 xmlHashScan((xmlHashTablePtr) source->entities,
Nick Wellnhofere03f0a12017-11-09 16:42:47 +01001399 xmlXIncludeMergeEntity, &data);
Daniel Veillard4287c572003-02-04 22:48:53 +00001400 }
1401 }
1402 return(0);
1403}
1404
1405/**
Owen Taylor3473f882001-02-23 17:55:21 +00001406 * xmlXIncludeLoadDoc:
1407 * @ctxt: the XInclude context
1408 * @url: the associated URL
1409 * @nr: the xinclude node number
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001410 *
Owen Taylor3473f882001-02-23 17:55:21 +00001411 * Load the document, and store the result in the XInclude context
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001412 *
1413 * Returns 0 in case of success, -1 in case of failure
Owen Taylor3473f882001-02-23 17:55:21 +00001414 */
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001415static int
Owen Taylor3473f882001-02-23 17:55:21 +00001416xmlXIncludeLoadDoc(xmlXIncludeCtxtPtr ctxt, const xmlChar *url, int nr) {
1417 xmlDocPtr doc;
1418 xmlURIPtr uri;
1419 xmlChar *URL;
1420 xmlChar *fragment = NULL;
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001421 int i = 0;
William M. Brack4d59e222004-03-08 14:42:31 +00001422#ifdef LIBXML_XPTR_ENABLED
1423 int saveFlags;
1424#endif
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001425
1426#ifdef DEBUG_XINCLUDE
1427 xmlGenericError(xmlGenericErrorContext, "Loading doc %s:%d\n", url, nr);
1428#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001429 /*
1430 * Check the URL and remove any fragment identifier
1431 */
1432 uri = xmlParseURI((const char *)url);
1433 if (uri == NULL) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001434 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001435 XML_XINCLUDE_HREF_URI,
1436 "invalid value URI %s\n", url);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001437 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001438 }
1439 if (uri->fragment != NULL) {
1440 fragment = (xmlChar *) uri->fragment;
1441 uri->fragment = NULL;
1442 }
Daniel Veillardb98d0822003-12-24 11:06:25 +00001443 if ((ctxt->incTab != NULL) && (ctxt->incTab[nr] != NULL) &&
1444 (ctxt->incTab[nr]->fragment != NULL)) {
1445 if (fragment != NULL) xmlFree(fragment);
1446 fragment = xmlStrdup(ctxt->incTab[nr]->fragment);
1447 }
Owen Taylor3473f882001-02-23 17:55:21 +00001448 URL = xmlSaveUri(uri);
1449 xmlFreeURI(uri);
1450 if (URL == NULL) {
Daniel Veillard11ce4002006-03-10 00:36:23 +00001451 if (ctxt->incTab != NULL)
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001452 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
Daniel Veillard11ce4002006-03-10 00:36:23 +00001453 XML_XINCLUDE_HREF_URI,
1454 "invalid value URI %s\n", url);
1455 else
1456 xmlXIncludeErr(ctxt, NULL,
1457 XML_XINCLUDE_HREF_URI,
1458 "invalid value URI %s\n", url);
Owen Taylor3473f882001-02-23 17:55:21 +00001459 if (fragment != NULL)
1460 xmlFree(fragment);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001461 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001462 }
1463
1464 /*
1465 * Handling of references to the local document are done
1466 * directly through ctxt->doc.
1467 */
Daniel Veillardf4b4f982003-02-13 11:02:08 +00001468 if ((URL[0] == 0) || (URL[0] == '#') ||
1469 ((ctxt->doc != NULL) && (xmlStrEqual(URL, ctxt->doc->URL)))) {
Haibo Huangf0a546b2020-09-01 20:28:19 -07001470 doc = ctxt->doc;
Owen Taylor3473f882001-02-23 17:55:21 +00001471 goto loaded;
1472 }
1473
1474 /*
1475 * Prevent reloading twice the document.
1476 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001477 for (i = 0; i < ctxt->incNr; i++) {
1478 if ((xmlStrEqual(URL, ctxt->incTab[i]->URI)) &&
1479 (ctxt->incTab[i]->doc != NULL)) {
1480 doc = ctxt->incTab[i]->doc;
1481#ifdef DEBUG_XINCLUDE
1482 printf("Already loaded %s\n", URL);
1483#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001484 goto loaded;
1485 }
1486 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001487
Owen Taylor3473f882001-02-23 17:55:21 +00001488 /*
1489 * Load it.
1490 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001491#ifdef DEBUG_XINCLUDE
1492 printf("loading %s\n", URL);
1493#endif
William M. Brack4d59e222004-03-08 14:42:31 +00001494#ifdef LIBXML_XPTR_ENABLED
1495 /*
1496 * If this is an XPointer evaluation, we want to assure that
1497 * all entities have been resolved prior to processing the
1498 * referenced document
1499 */
1500 saveFlags = ctxt->parseFlags;
1501 if (fragment != NULL) { /* if this is an XPointer eval */
1502 ctxt->parseFlags |= XML_PARSE_NOENT;
1503 }
1504#endif
1505
Daniel Veillard98485322003-08-14 15:44:40 +00001506 doc = xmlXIncludeParseFile(ctxt, (const char *)URL);
William M. Brack4d59e222004-03-08 14:42:31 +00001507#ifdef LIBXML_XPTR_ENABLED
1508 ctxt->parseFlags = saveFlags;
1509#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001510 if (doc == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001511 xmlFree(URL);
1512 if (fragment != NULL)
1513 xmlFree(fragment);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001514 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001515 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001516 ctxt->incTab[nr]->doc = doc;
William M. Brackb85c9202004-07-26 00:20:13 +00001517 /*
1518 * It's possible that the requested URL has been mapped to a
1519 * completely different location (e.g. through a catalog entry).
1520 * To check for this, we compare the URL with that of the doc
1521 * and change it if they disagree (bug 146988).
1522 */
1523 if (!xmlStrEqual(URL, doc->URL)) {
1524 xmlFree(URL);
1525 URL = xmlStrdup(doc->URL);
1526 }
Daniel Veillard98485322003-08-14 15:44:40 +00001527 for (i = nr + 1; i < ctxt->incNr; i++) {
1528 if (xmlStrEqual(URL, ctxt->incTab[i]->URI)) {
1529 ctxt->incTab[nr]->count++;
1530#ifdef DEBUG_XINCLUDE
1531 printf("Increasing %s count since reused\n", URL);
1532#endif
1533 break;
1534 }
1535 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001536
1537 /*
Daniel Veillard4287c572003-02-04 22:48:53 +00001538 * Make sure we have all entities fixed up
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001539 */
Daniel Veillard4287c572003-02-04 22:48:53 +00001540 xmlXIncludeMergeEntities(ctxt, ctxt->doc, doc);
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001541
1542 /*
1543 * We don't need the DTD anymore, free up space
1544 if (doc->intSubset != NULL) {
1545 xmlUnlinkNode((xmlNodePtr) doc->intSubset);
1546 xmlFreeNode((xmlNodePtr) doc->intSubset);
1547 doc->intSubset = NULL;
1548 }
1549 if (doc->extSubset != NULL) {
1550 xmlUnlinkNode((xmlNodePtr) doc->extSubset);
1551 xmlFreeNode((xmlNodePtr) doc->extSubset);
1552 doc->extSubset = NULL;
1553 }
1554 */
1555 xmlXIncludeRecurseDoc(ctxt, doc, URL);
Owen Taylor3473f882001-02-23 17:55:21 +00001556
1557loaded:
1558 if (fragment == NULL) {
1559 /*
1560 * Add the top children list as the replacement copy.
Owen Taylor3473f882001-02-23 17:55:21 +00001561 */
Haibo Huangf0a546b2020-09-01 20:28:19 -07001562 ctxt->incTab[nr]->inc = xmlXIncludeCopyNodeList(ctxt, ctxt->doc,
1563 doc, doc->children);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001564 }
Daniel Veillard10acc2f2003-09-01 20:59:40 +00001565#ifdef LIBXML_XPTR_ENABLED
1566 else {
Owen Taylor3473f882001-02-23 17:55:21 +00001567 /*
1568 * Computes the XPointer expression and make a copy used
1569 * as the replacement copy.
1570 */
1571 xmlXPathObjectPtr xptr;
1572 xmlXPathContextPtr xptrctxt;
Daniel Veillard39196eb2001-06-19 18:09:42 +00001573 xmlNodeSetPtr set;
Owen Taylor3473f882001-02-23 17:55:21 +00001574
Haibo Huangf0a546b2020-09-01 20:28:19 -07001575 xptrctxt = xmlXPtrNewContext(doc, NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001576 if (xptrctxt == NULL) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001577 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001578 XML_XINCLUDE_XPTR_FAILED,
Daniel Veillarda152c4d2003-11-19 16:24:26 +00001579 "could not create XPointer context\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001580 xmlFree(URL);
1581 xmlFree(fragment);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001582 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001583 }
1584 xptr = xmlXPtrEval(fragment, xptrctxt);
1585 if (xptr == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001586 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1587 XML_XINCLUDE_XPTR_FAILED,
1588 "XPointer evaluation failed: #%s\n",
1589 fragment);
Owen Taylor3473f882001-02-23 17:55:21 +00001590 xmlXPathFreeContext(xptrctxt);
1591 xmlFree(URL);
1592 xmlFree(fragment);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001593 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001594 }
Daniel Veillard39196eb2001-06-19 18:09:42 +00001595 switch (xptr->type) {
1596 case XPATH_UNDEFINED:
1597 case XPATH_BOOLEAN:
1598 case XPATH_NUMBER:
1599 case XPATH_STRING:
1600 case XPATH_POINT:
1601 case XPATH_USERS:
1602 case XPATH_XSLT_TREE:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001603 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001604 XML_XINCLUDE_XPTR_RESULT,
1605 "XPointer is not a range: #%s\n",
1606 fragment);
Haibo Huangcfd91dc2020-07-30 23:01:33 -07001607 xmlXPathFreeObject(xptr);
Daniel Veillard39196eb2001-06-19 18:09:42 +00001608 xmlXPathFreeContext(xptrctxt);
1609 xmlFree(URL);
1610 xmlFree(fragment);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001611 return(-1);
Daniel Veillard39196eb2001-06-19 18:09:42 +00001612 case XPATH_NODESET:
Daniel Veillard798ae542003-11-03 17:13:52 +00001613 if ((xptr->nodesetval == NULL) ||
1614 (xptr->nodesetval->nodeNr <= 0)) {
Haibo Huangcfd91dc2020-07-30 23:01:33 -07001615 xmlXPathFreeObject(xptr);
Daniel Veillard798ae542003-11-03 17:13:52 +00001616 xmlXPathFreeContext(xptrctxt);
1617 xmlFree(URL);
1618 xmlFree(fragment);
1619 return(-1);
1620 }
William M. Brackabf598b2004-06-08 02:01:28 +00001621
Daniel Veillard39196eb2001-06-19 18:09:42 +00001622 case XPATH_RANGE:
1623 case XPATH_LOCATIONSET:
1624 break;
1625 }
1626 set = xptr->nodesetval;
1627 if (set != NULL) {
1628 for (i = 0;i < set->nodeNr;i++) {
1629 if (set->nodeTab[i] == NULL)
1630 continue;
1631 switch (set->nodeTab[i]->type) {
William M. Brackb0a94e82007-07-18 18:04:55 +00001632 case XML_ELEMENT_NODE:
Daniel Veillard39196eb2001-06-19 18:09:42 +00001633 case XML_TEXT_NODE:
1634 case XML_CDATA_SECTION_NODE:
Daniel Veillard39196eb2001-06-19 18:09:42 +00001635 case XML_ENTITY_REF_NODE:
1636 case XML_ENTITY_NODE:
1637 case XML_PI_NODE:
1638 case XML_COMMENT_NODE:
1639 case XML_DOCUMENT_NODE:
1640 case XML_HTML_DOCUMENT_NODE:
1641#ifdef LIBXML_DOCB_ENABLED
1642 case XML_DOCB_DOCUMENT_NODE:
1643#endif
1644 continue;
William M. Brackabf598b2004-06-08 02:01:28 +00001645
Daniel Veillard39196eb2001-06-19 18:09:42 +00001646 case XML_ATTRIBUTE_NODE:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001647 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001648 XML_XINCLUDE_XPTR_RESULT,
1649 "XPointer selects an attribute: #%s\n",
1650 fragment);
Daniel Veillard39196eb2001-06-19 18:09:42 +00001651 set->nodeTab[i] = NULL;
1652 continue;
1653 case XML_NAMESPACE_DECL:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001654 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001655 XML_XINCLUDE_XPTR_RESULT,
1656 "XPointer selects a namespace: #%s\n",
1657 fragment);
Daniel Veillard39196eb2001-06-19 18:09:42 +00001658 set->nodeTab[i] = NULL;
1659 continue;
1660 case XML_DOCUMENT_TYPE_NODE:
1661 case XML_DOCUMENT_FRAG_NODE:
1662 case XML_NOTATION_NODE:
1663 case XML_DTD_NODE:
1664 case XML_ELEMENT_DECL:
1665 case XML_ATTRIBUTE_DECL:
1666 case XML_ENTITY_DECL:
1667 case XML_XINCLUDE_START:
1668 case XML_XINCLUDE_END:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001669 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001670 XML_XINCLUDE_XPTR_RESULT,
1671 "XPointer selects unexpected nodes: #%s\n",
1672 fragment);
Daniel Veillard39196eb2001-06-19 18:09:42 +00001673 set->nodeTab[i] = NULL;
1674 set->nodeTab[i] = NULL;
1675 continue; /* for */
1676 }
1677 }
1678 }
Haibo Huangf0a546b2020-09-01 20:28:19 -07001679 ctxt->incTab[nr]->inc =
1680 xmlXIncludeCopyXPointer(ctxt, ctxt->doc, doc, xptr);
1681 xmlXPathFreeObject(xptr);
Owen Taylor3473f882001-02-23 17:55:21 +00001682 xmlXPathFreeContext(xptrctxt);
1683 xmlFree(fragment);
1684 }
Daniel Veillard10acc2f2003-09-01 20:59:40 +00001685#endif
Daniel Veillardc4bad4a2002-08-14 14:45:25 +00001686
1687 /*
1688 * Do the xml:base fixup if needed
1689 */
Alexey Neyman0b865372013-05-06 10:20:18 +08001690 if ((doc != NULL) && (URL != NULL) &&
Daniel Veillard54bd29b2008-08-26 07:26:55 +00001691 (!(ctxt->parseFlags & XML_PARSE_NOBASEFIX)) &&
1692 (!(doc->parseFlags & XML_PARSE_NOBASEFIX))) {
Daniel Veillardc4bad4a2002-08-14 14:45:25 +00001693 xmlNodePtr node;
William M. Brack0b13a092005-06-01 03:37:59 +00001694 xmlChar *base;
William M. Brackabf598b2004-06-08 02:01:28 +00001695 xmlChar *curBase;
Daniel Veillardc4bad4a2002-08-14 14:45:25 +00001696
William M. Brackf7789b12004-06-07 08:57:27 +00001697 /*
William M. Brack0b13a092005-06-01 03:37:59 +00001698 * The base is only adjusted if "necessary", i.e. if the xinclude node
1699 * has a base specified, or the URL is relative
William M. Brackf7789b12004-06-07 08:57:27 +00001700 */
William M. Brack0b13a092005-06-01 03:37:59 +00001701 base = xmlGetNsProp(ctxt->incTab[nr]->ref, BAD_CAST "base",
1702 XML_XML_NAMESPACE);
1703 if (base == NULL) {
1704 /*
1705 * No xml:base on the xinclude node, so we check whether the
1706 * URI base is different than (relative to) the context base
1707 */
1708 curBase = xmlBuildRelativeURI(URL, ctxt->base);
1709 if (curBase == NULL) { /* Error return */
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001710 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
William M. Brackf7789b12004-06-07 08:57:27 +00001711 XML_XINCLUDE_HREF_URI,
1712 "trying to build relative URI from %s\n", URL);
William M. Brack0b13a092005-06-01 03:37:59 +00001713 } else {
1714 /* If the URI doesn't contain a slash, it's not relative */
1715 if (!xmlStrchr(curBase, (xmlChar) '/'))
1716 xmlFree(curBase);
1717 else
1718 base = curBase;
William M. Brackf7789b12004-06-07 08:57:27 +00001719 }
William M. Brack0b13a092005-06-01 03:37:59 +00001720 }
1721 if (base != NULL) { /* Adjustment may be needed */
1722 node = ctxt->incTab[nr]->inc;
1723 while (node != NULL) {
1724 /* Only work on element nodes */
1725 if (node->type == XML_ELEMENT_NODE) {
1726 curBase = xmlNodeGetBase(node->doc, node);
1727 /* If no current base, set it */
1728 if (curBase == NULL) {
1729 xmlNodeSetBase(node, base);
1730 } else {
1731 /*
1732 * If the current base is the same as the
1733 * URL of the document, then reset it to be
1734 * the specified xml:base or the relative URI
1735 */
1736 if (xmlStrEqual(curBase, node->doc->URL)) {
1737 xmlNodeSetBase(node, base);
1738 } else {
1739 /*
1740 * If the element already has an xml:base
1741 * set, then relativise it if necessary
1742 */
1743 xmlChar *xmlBase;
1744 xmlBase = xmlGetNsProp(node,
1745 BAD_CAST "base",
1746 XML_XML_NAMESPACE);
1747 if (xmlBase != NULL) {
1748 xmlChar *relBase;
1749 relBase = xmlBuildURI(xmlBase, base);
1750 if (relBase == NULL) { /* error */
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001751 xmlXIncludeErr(ctxt,
William M. Brack0b13a092005-06-01 03:37:59 +00001752 ctxt->incTab[nr]->ref,
1753 XML_XINCLUDE_HREF_URI,
1754 "trying to rebuild base from %s\n",
1755 xmlBase);
1756 } else {
1757 xmlNodeSetBase(node, relBase);
1758 xmlFree(relBase);
1759 }
1760 xmlFree(xmlBase);
1761 }
1762 }
1763 xmlFree(curBase);
1764 }
1765 }
1766 node = node->next;
1767 }
1768 xmlFree(base);
Daniel Veillardc4bad4a2002-08-14 14:45:25 +00001769 }
1770 }
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001771 if ((nr < ctxt->incNr) && (ctxt->incTab[nr]->doc != NULL) &&
1772 (ctxt->incTab[nr]->count <= 1)) {
1773#ifdef DEBUG_XINCLUDE
1774 printf("freeing %s\n", ctxt->incTab[nr]->doc->URL);
1775#endif
1776 xmlFreeDoc(ctxt->incTab[nr]->doc);
1777 ctxt->incTab[nr]->doc = NULL;
1778 }
Owen Taylor3473f882001-02-23 17:55:21 +00001779 xmlFree(URL);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001780 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00001781}
1782
1783/**
1784 * xmlXIncludeLoadTxt:
1785 * @ctxt: the XInclude context
1786 * @url: the associated URL
1787 * @nr: the xinclude node number
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001788 *
Owen Taylor3473f882001-02-23 17:55:21 +00001789 * Load the content, and store the result in the XInclude context
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001790 *
1791 * Returns 0 in case of success, -1 in case of failure
Owen Taylor3473f882001-02-23 17:55:21 +00001792 */
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001793static int
Owen Taylor3473f882001-02-23 17:55:21 +00001794xmlXIncludeLoadTxt(xmlXIncludeCtxtPtr ctxt, const xmlChar *url, int nr) {
1795 xmlParserInputBufferPtr buf;
1796 xmlNodePtr node;
1797 xmlURIPtr uri;
1798 xmlChar *URL;
1799 int i;
Daniel Veillardd076a202002-11-20 13:28:31 +00001800 xmlChar *encoding = NULL;
William M. Brack78637da2003-07-31 14:47:38 +00001801 xmlCharEncoding enc = (xmlCharEncoding) 0;
Shaun McCance4cf73252012-05-10 20:59:33 +08001802 xmlParserCtxtPtr pctxt;
1803 xmlParserInputPtr inputStream;
Vitaly Ostanindce1c8b2012-08-17 20:42:52 +08001804 int xinclude_multibyte_fallback_used = 0;
Daniel Veillardd076a202002-11-20 13:28:31 +00001805
Haibo Huangcfd91dc2020-07-30 23:01:33 -07001806 /* Don't read from stdin. */
1807 if (xmlStrcmp(url, BAD_CAST "-") == 0)
1808 url = BAD_CAST "./-";
1809
Owen Taylor3473f882001-02-23 17:55:21 +00001810 /*
1811 * Check the URL and remove any fragment identifier
1812 */
1813 uri = xmlParseURI((const char *)url);
1814 if (uri == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001815 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_HREF_URI,
1816 "invalid value URI %s\n", url);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001817 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001818 }
1819 if (uri->fragment != NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001820 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_TEXT_FRAGMENT,
1821 "fragment identifier forbidden for text: %s\n",
1822 (const xmlChar *) uri->fragment);
Owen Taylor3473f882001-02-23 17:55:21 +00001823 xmlFreeURI(uri);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001824 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001825 }
1826 URL = xmlSaveUri(uri);
1827 xmlFreeURI(uri);
1828 if (URL == NULL) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001829 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_HREF_URI,
1830 "invalid value URI %s\n", url);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001831 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001832 }
1833
1834 /*
1835 * Handling of references to the local document are done
1836 * directly through ctxt->doc.
1837 */
1838 if (URL[0] == 0) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001839 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001840 XML_XINCLUDE_TEXT_DOCUMENT,
1841 "text serialization of document not available\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001842 xmlFree(URL);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001843 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001844 }
1845
1846 /*
1847 * Prevent reloading twice the document.
1848 */
1849 for (i = 0; i < ctxt->txtNr; i++) {
1850 if (xmlStrEqual(URL, ctxt->txturlTab[i])) {
Haibo Huangd23e46c2020-10-28 22:26:09 -07001851 node = xmlNewText(ctxt->txtTab[i]);
Owen Taylor3473f882001-02-23 17:55:21 +00001852 goto loaded;
1853 }
1854 }
1855 /*
Daniel Veillardd076a202002-11-20 13:28:31 +00001856 * Try to get the encoding if available
Owen Taylor3473f882001-02-23 17:55:21 +00001857 */
Daniel Veillardd076a202002-11-20 13:28:31 +00001858 if ((ctxt->incTab[nr] != NULL) && (ctxt->incTab[nr]->ref != NULL)) {
1859 encoding = xmlGetProp(ctxt->incTab[nr]->ref, XINCLUDE_PARSE_ENCODING);
1860 }
1861 if (encoding != NULL) {
1862 /*
1863 * TODO: we should not have to remap to the xmlCharEncoding
1864 * predefined set, a better interface than
1865 * xmlParserInputBufferCreateFilename should allow any
1866 * encoding supported by iconv
1867 */
1868 enc = xmlParseCharEncoding((const char *) encoding);
1869 if (enc == XML_CHAR_ENCODING_ERROR) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00001870 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1871 XML_XINCLUDE_UNKNOWN_ENCODING,
1872 "encoding %s not supported\n", encoding);
Daniel Veillardd076a202002-11-20 13:28:31 +00001873 xmlFree(encoding);
1874 xmlFree(URL);
1875 return(-1);
1876 }
1877 xmlFree(encoding);
1878 }
1879
1880 /*
1881 * Load it.
1882 */
Shaun McCance4cf73252012-05-10 20:59:33 +08001883 pctxt = xmlNewParserCtxt();
1884 inputStream = xmlLoadExternalEntity((const char*)URL, NULL, pctxt);
1885 if(inputStream == NULL) {
1886 xmlFreeParserCtxt(pctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001887 xmlFree(URL);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001888 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001889 }
Shaun McCance4cf73252012-05-10 20:59:33 +08001890 buf = inputStream->buf;
1891 if (buf == NULL) {
1892 xmlFreeInputStream (inputStream);
1893 xmlFreeParserCtxt(pctxt);
1894 xmlFree(URL);
1895 return(-1);
1896 }
1897 if (buf->encoder)
1898 xmlCharEncCloseFunc(buf->encoder);
1899 buf->encoder = xmlGetCharEncodingHandler(enc);
Owen Taylor3473f882001-02-23 17:55:21 +00001900 node = xmlNewText(NULL);
1901
1902 /*
1903 * Scan all chars from the resource and add the to the node
1904 */
Vitaly Ostanindce1c8b2012-08-17 20:42:52 +08001905xinclude_multibyte_fallback:
Owen Taylor3473f882001-02-23 17:55:21 +00001906 while (xmlParserInputBufferRead(buf, 128) > 0) {
1907 int len;
1908 const xmlChar *content;
1909
Daniel Veillard345ee8b2012-07-16 14:40:37 +08001910 content = xmlBufContent(buf->buffer);
1911 len = xmlBufLength(buf->buffer);
Daniel Veillardd076a202002-11-20 13:28:31 +00001912 for (i = 0;i < len;) {
1913 int cur;
1914 int l;
1915
1916 cur = xmlStringCurrentChar(NULL, &content[i], &l);
1917 if (!IS_CHAR(cur)) {
Haibo Huangcfd91dc2020-07-30 23:01:33 -07001918 /* Handle split multibyte char at buffer boundary */
Vitaly Ostanindce1c8b2012-08-17 20:42:52 +08001919 if (((len - i) < 4) && (!xinclude_multibyte_fallback_used)) {
1920 xinclude_multibyte_fallback_used = 1;
1921 xmlBufShrink(buf->buffer, i);
1922 goto xinclude_multibyte_fallback;
1923 } else {
1924 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1925 XML_XINCLUDE_INVALID_CHAR,
1926 "%s contains invalid char\n", URL);
Haibo Huangcfd91dc2020-07-30 23:01:33 -07001927 xmlFreeParserCtxt(pctxt);
Vitaly Ostanindce1c8b2012-08-17 20:42:52 +08001928 xmlFreeParserInputBuffer(buf);
1929 xmlFree(URL);
1930 return(-1);
1931 }
Owen Taylor3473f882001-02-23 17:55:21 +00001932 } else {
Vitaly Ostanindce1c8b2012-08-17 20:42:52 +08001933 xinclude_multibyte_fallback_used = 0;
Daniel Veillardd076a202002-11-20 13:28:31 +00001934 xmlNodeAddContentLen(node, &content[i], l);
Owen Taylor3473f882001-02-23 17:55:21 +00001935 }
Daniel Veillardd076a202002-11-20 13:28:31 +00001936 i += l;
Owen Taylor3473f882001-02-23 17:55:21 +00001937 }
Daniel Veillard345ee8b2012-07-16 14:40:37 +08001938 xmlBufShrink(buf->buffer, len);
Owen Taylor3473f882001-02-23 17:55:21 +00001939 }
Shaun McCance4cf73252012-05-10 20:59:33 +08001940 xmlFreeParserCtxt(pctxt);
Haibo Huangd23e46c2020-10-28 22:26:09 -07001941 xmlXIncludeAddTxt(ctxt, node->content, URL);
Shaun McCance4cf73252012-05-10 20:59:33 +08001942 xmlFreeInputStream(inputStream);
Owen Taylor3473f882001-02-23 17:55:21 +00001943
1944loaded:
1945 /*
1946 * Add the element as the replacement copy.
1947 */
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001948 ctxt->incTab[nr]->inc = node;
Owen Taylor3473f882001-02-23 17:55:21 +00001949 xmlFree(URL);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001950 return(0);
1951}
1952
1953/**
1954 * xmlXIncludeLoadFallback:
1955 * @ctxt: the XInclude context
1956 * @fallback: the fallback node
1957 * @nr: the xinclude node number
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001958 *
Daniel Veillardbbc72c32002-09-05 10:52:10 +00001959 * Load the content of the fallback node, and store the result
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001960 * in the XInclude context
1961 *
1962 * Returns 0 in case of success, -1 in case of failure
1963 */
1964static int
1965xmlXIncludeLoadFallback(xmlXIncludeCtxtPtr ctxt, xmlNodePtr fallback, int nr) {
William M. Brackaae10522004-01-02 14:59:41 +00001966 xmlXIncludeCtxtPtr newctxt;
1967 int ret = 0;
Haibo Huangcfd91dc2020-07-30 23:01:33 -07001968 int oldNbErrors = ctxt->nbErrors;
Daniel Veillard3e62adb2012-08-09 14:24:02 +08001969
1970 if ((fallback == NULL) || (fallback->type == XML_NAMESPACE_DECL) ||
1971 (ctxt == NULL))
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001972 return(-1);
William M. Brackef245fd2004-02-06 09:33:59 +00001973 if (fallback->children != NULL) {
1974 /*
1975 * It's possible that the fallback also has 'includes'
1976 * (Bug 129969), so we re-process the fallback just in case
1977 */
1978 newctxt = xmlXIncludeNewContext(ctxt->doc);
1979 if (newctxt == NULL)
1980 return (-1);
Daniel Veillarda6585822006-12-04 09:21:28 +00001981 newctxt->_private = ctxt->_private;
Daniel Veillardce244ad2004-11-05 10:03:46 +00001982 newctxt->base = xmlStrdup(ctxt->base); /* Inherit the base from the existing context */
William M. Brackef245fd2004-02-06 09:33:59 +00001983 xmlXIncludeSetFlags(newctxt, ctxt->parseFlags);
Haibo Huangf0a546b2020-09-01 20:28:19 -07001984 newctxt->incTotal = ctxt->incTotal;
1985 if (xmlXIncludeDoProcess(newctxt, ctxt->doc, fallback, 1) < 0)
1986 ret = -1;
1987 ctxt->incTotal = newctxt->incTotal;
Haibo Huangcfd91dc2020-07-30 23:01:33 -07001988 if (ctxt->nbErrors > oldNbErrors)
William M. Brackef245fd2004-02-06 09:33:59 +00001989 ret = -1;
1990 xmlXIncludeFreeContext(newctxt);
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00001991
Daniel Veillard03a53c32004-10-26 16:06:51 +00001992 ctxt->incTab[nr]->inc = xmlDocCopyNodeList(ctxt->doc,
1993 fallback->children);
Haibo Huangf0a546b2020-09-01 20:28:19 -07001994 if (ctxt->incTab[nr]->inc == NULL)
1995 ctxt->incTab[nr]->emptyFb = 1;
William M. Brackef245fd2004-02-06 09:33:59 +00001996 } else {
1997 ctxt->incTab[nr]->inc = NULL;
William M. Brack95af5942004-02-08 04:12:49 +00001998 ctxt->incTab[nr]->emptyFb = 1; /* flag empty callback */
William M. Brackef245fd2004-02-06 09:33:59 +00001999 }
Haibo Huangf0a546b2020-09-01 20:28:19 -07002000 ctxt->incTab[nr]->fallback = 1;
William M. Brackaae10522004-01-02 14:59:41 +00002001 return(ret);
Owen Taylor3473f882001-02-23 17:55:21 +00002002}
2003
2004/************************************************************************
2005 * *
2006 * XInclude Processing *
2007 * *
2008 ************************************************************************/
2009
2010/**
2011 * xmlXIncludePreProcessNode:
2012 * @ctxt: an XInclude context
2013 * @node: an XInclude node
2014 *
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002015 * Implement the XInclude preprocessing, currently just adding the element
2016 * for further processing.
Owen Taylor3473f882001-02-23 17:55:21 +00002017 *
2018 * Returns the result list or NULL in case of error
2019 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002020static xmlNodePtr
Owen Taylor3473f882001-02-23 17:55:21 +00002021xmlXIncludePreProcessNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) {
2022 xmlXIncludeAddNode(ctxt, node);
Daniel Veillard24505b02005-07-28 23:49:35 +00002023 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002024}
2025
Daniel Veillardbbc72c32002-09-05 10:52:10 +00002026/**
Owen Taylor3473f882001-02-23 17:55:21 +00002027 * xmlXIncludeLoadNode:
2028 * @ctxt: an XInclude context
2029 * @nr: the node number
2030 *
2031 * Find and load the infoset replacement for the given node.
2032 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002033 * Returns 0 if substitution succeeded, -1 if some processing failed
Owen Taylor3473f882001-02-23 17:55:21 +00002034 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002035static int
Owen Taylor3473f882001-02-23 17:55:21 +00002036xmlXIncludeLoadNode(xmlXIncludeCtxtPtr ctxt, int nr) {
2037 xmlNodePtr cur;
2038 xmlChar *href;
2039 xmlChar *parse;
2040 xmlChar *base;
William M. Brackf7789b12004-06-07 08:57:27 +00002041 xmlChar *oldBase;
Owen Taylor3473f882001-02-23 17:55:21 +00002042 xmlChar *URI;
2043 int xml = 1; /* default Issue 64 */
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00002044 int ret;
Owen Taylor3473f882001-02-23 17:55:21 +00002045
2046 if (ctxt == NULL)
2047 return(-1);
2048 if ((nr < 0) || (nr >= ctxt->incNr))
2049 return(-1);
Daniel Veillardbbc72c32002-09-05 10:52:10 +00002050 cur = ctxt->incTab[nr]->ref;
Owen Taylor3473f882001-02-23 17:55:21 +00002051 if (cur == NULL)
2052 return(-1);
2053
Owen Taylor3473f882001-02-23 17:55:21 +00002054 /*
2055 * read the attributes
2056 */
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002057 href = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_HREF);
Owen Taylor3473f882001-02-23 17:55:21 +00002058 if (href == NULL) {
Daniel Veillard03c2f0a2004-01-25 19:54:59 +00002059 href = xmlStrdup(BAD_CAST ""); /* @@@@ href is now optional */
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002060 if (href == NULL)
Daniel Veillard03c2f0a2004-01-25 19:54:59 +00002061 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002062 }
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002063 parse = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_PARSE);
Owen Taylor3473f882001-02-23 17:55:21 +00002064 if (parse != NULL) {
2065 if (xmlStrEqual(parse, XINCLUDE_PARSE_XML))
2066 xml = 1;
2067 else if (xmlStrEqual(parse, XINCLUDE_PARSE_TEXT))
2068 xml = 0;
2069 else {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002070 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
2071 XML_XINCLUDE_PARSE_VALUE,
2072 "invalid value %s for 'parse'\n", parse);
Owen Taylor3473f882001-02-23 17:55:21 +00002073 if (href != NULL)
2074 xmlFree(href);
2075 if (parse != NULL)
2076 xmlFree(parse);
2077 return(-1);
2078 }
2079 }
2080
2081 /*
2082 * compute the URI
2083 */
2084 base = xmlNodeGetBase(ctxt->doc, cur);
2085 if (base == NULL) {
2086 URI = xmlBuildURI(href, ctxt->doc->URL);
2087 } else {
2088 URI = xmlBuildURI(href, base);
2089 }
2090 if (URI == NULL) {
2091 xmlChar *escbase;
2092 xmlChar *eschref;
2093 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002094 * Some escaping may be needed
Owen Taylor3473f882001-02-23 17:55:21 +00002095 */
2096 escbase = xmlURIEscape(base);
2097 eschref = xmlURIEscape(href);
2098 URI = xmlBuildURI(eschref, escbase);
2099 if (escbase != NULL)
2100 xmlFree(escbase);
2101 if (eschref != NULL)
2102 xmlFree(eschref);
2103 }
2104 if (URI == NULL) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002105 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002106 XML_XINCLUDE_HREF_URI, "failed build URL\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002107 if (parse != NULL)
2108 xmlFree(parse);
2109 if (href != NULL)
2110 xmlFree(href);
2111 if (base != NULL)
2112 xmlFree(base);
2113 return(-1);
2114 }
2115#ifdef DEBUG_XINCLUDE
2116 xmlGenericError(xmlGenericErrorContext, "parse: %s\n",
2117 xml ? "xml": "text");
2118 xmlGenericError(xmlGenericErrorContext, "URI: %s\n", URI);
2119#endif
2120
2121 /*
William M. Brackf7789b12004-06-07 08:57:27 +00002122 * Save the base for this include (saving the current one)
Owen Taylor3473f882001-02-23 17:55:21 +00002123 */
William M. Brackf7789b12004-06-07 08:57:27 +00002124 oldBase = ctxt->base;
2125 ctxt->base = base;
2126
Owen Taylor3473f882001-02-23 17:55:21 +00002127 if (xml) {
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00002128 ret = xmlXIncludeLoadDoc(ctxt, URI, nr);
Owen Taylor3473f882001-02-23 17:55:21 +00002129 /* xmlXIncludeGetFragment(ctxt, cur, URI); */
2130 } else {
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00002131 ret = xmlXIncludeLoadTxt(ctxt, URI, nr);
2132 }
William M. Brackf7789b12004-06-07 08:57:27 +00002133
2134 /*
2135 * Restore the original base before checking for fallback
2136 */
2137 ctxt->base = oldBase;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002138
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00002139 if (ret < 0) {
2140 xmlNodePtr children;
2141
2142 /*
Haibo Huangcfd91dc2020-07-30 23:01:33 -07002143 * Time to try a fallback if available
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00002144 */
2145#ifdef DEBUG_XINCLUDE
2146 xmlGenericError(xmlGenericErrorContext, "error looking for fallback\n");
2147#endif
2148 children = cur->children;
2149 while (children != NULL) {
2150 if ((children->type == XML_ELEMENT_NODE) &&
2151 (children->ns != NULL) &&
2152 (xmlStrEqual(children->name, XINCLUDE_FALLBACK)) &&
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002153 ((xmlStrEqual(children->ns->href, XINCLUDE_NS)) ||
2154 (xmlStrEqual(children->ns->href, XINCLUDE_OLD_NS)))) {
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00002155 ret = xmlXIncludeLoadFallback(ctxt, children, nr);
Haibo Huangf0a546b2020-09-01 20:28:19 -07002156 break;
Daniel Veillarde3b7d9a2002-08-14 14:11:30 +00002157 }
2158 children = children->next;
2159 }
2160 }
2161 if (ret < 0) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002162 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002163 XML_XINCLUDE_NO_FALLBACK,
2164 "could not load %s, and no fallback was found\n",
2165 URI);
Owen Taylor3473f882001-02-23 17:55:21 +00002166 }
2167
2168 /*
2169 * Cleanup
2170 */
2171 if (URI != NULL)
2172 xmlFree(URI);
2173 if (parse != NULL)
2174 xmlFree(parse);
2175 if (href != NULL)
2176 xmlFree(href);
2177 if (base != NULL)
2178 xmlFree(base);
2179 return(0);
2180}
2181
2182/**
2183 * xmlXIncludeIncludeNode:
2184 * @ctxt: an XInclude context
2185 * @nr: the node number
2186 *
Haibo Huangcfd91dc2020-07-30 23:01:33 -07002187 * Implement the infoset replacement for the given node
Owen Taylor3473f882001-02-23 17:55:21 +00002188 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002189 * Returns 0 if substitution succeeded, -1 if some processing failed
Owen Taylor3473f882001-02-23 17:55:21 +00002190 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002191static int
Owen Taylor3473f882001-02-23 17:55:21 +00002192xmlXIncludeIncludeNode(xmlXIncludeCtxtPtr ctxt, int nr) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002193 xmlNodePtr cur, end, list, tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00002194
2195 if (ctxt == NULL)
2196 return(-1);
2197 if ((nr < 0) || (nr >= ctxt->incNr))
2198 return(-1);
Daniel Veillardbbc72c32002-09-05 10:52:10 +00002199 cur = ctxt->incTab[nr]->ref;
Daniel Veillard3e62adb2012-08-09 14:24:02 +08002200 if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL))
Owen Taylor3473f882001-02-23 17:55:21 +00002201 return(-1);
2202
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002203 list = ctxt->incTab[nr]->inc;
2204 ctxt->incTab[nr]->inc = NULL;
Haibo Huangf0a546b2020-09-01 20:28:19 -07002205 ctxt->incTab[nr]->emptyFb = 0;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002206
2207 /*
2208 * Check against the risk of generating a multi-rooted document
2209 */
2210 if ((cur->parent != NULL) &&
2211 (cur->parent->type != XML_ELEMENT_NODE)) {
2212 int nb_elem = 0;
2213
2214 tmp = list;
2215 while (tmp != NULL) {
2216 if (tmp->type == XML_ELEMENT_NODE)
2217 nb_elem++;
2218 tmp = tmp->next;
2219 }
2220 if (nb_elem > 1) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002221 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002222 XML_XINCLUDE_MULTIPLE_ROOT,
2223 "XInclude error: would result in multiple root nodes\n",
2224 NULL);
Haibo Huangf0a546b2020-09-01 20:28:19 -07002225 xmlFreeNodeList(list);
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002226 return(-1);
2227 }
2228 }
2229
Daniel Veillardc14c3892004-08-16 12:34:50 +00002230 if (ctxt->parseFlags & XML_PARSE_NOXINCNODE) {
2231 /*
2232 * Add the list of nodes
2233 */
2234 while (list != NULL) {
2235 end = list;
2236 list = list->next;
Owen Taylor3473f882001-02-23 17:55:21 +00002237
Daniel Veillardc14c3892004-08-16 12:34:50 +00002238 xmlAddPrevSibling(cur, end);
2239 }
2240 xmlUnlinkNode(cur);
2241 xmlFreeNode(cur);
2242 } else {
Haibo Huangf0a546b2020-09-01 20:28:19 -07002243 xmlNodePtr child, next;
2244
Daniel Veillardc14c3892004-08-16 12:34:50 +00002245 /*
2246 * Change the current node as an XInclude start one, and add an
2247 * XInclude end one
2248 */
Haibo Huangf0a546b2020-09-01 20:28:19 -07002249 if (ctxt->incTab[nr]->fallback)
2250 xmlUnsetProp(cur, BAD_CAST "href");
Daniel Veillardc14c3892004-08-16 12:34:50 +00002251 cur->type = XML_XINCLUDE_START;
Haibo Huangf0a546b2020-09-01 20:28:19 -07002252 /* Remove fallback children */
2253 for (child = cur->children; child != NULL; child = next) {
2254 next = child->next;
2255 xmlUnlinkNode(child);
2256 xmlFreeNode(child);
2257 }
Daniel Veillard03a53c32004-10-26 16:06:51 +00002258 end = xmlNewDocNode(cur->doc, cur->ns, cur->name, NULL);
Daniel Veillardc14c3892004-08-16 12:34:50 +00002259 if (end == NULL) {
2260 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
2261 XML_XINCLUDE_BUILD_FAILED,
2262 "failed to build node\n", NULL);
Haibo Huangf0a546b2020-09-01 20:28:19 -07002263 xmlFreeNodeList(list);
Daniel Veillardc14c3892004-08-16 12:34:50 +00002264 return(-1);
2265 }
2266 end->type = XML_XINCLUDE_END;
2267 xmlAddNextSibling(cur, end);
Owen Taylor3473f882001-02-23 17:55:21 +00002268
Daniel Veillardc14c3892004-08-16 12:34:50 +00002269 /*
2270 * Add the list of nodes
2271 */
2272 while (list != NULL) {
2273 cur = list;
2274 list = list->next;
2275
2276 xmlAddPrevSibling(end, cur);
2277 }
Owen Taylor3473f882001-02-23 17:55:21 +00002278 }
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00002279
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002280
Owen Taylor3473f882001-02-23 17:55:21 +00002281 return(0);
2282}
2283
2284/**
2285 * xmlXIncludeTestNode:
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002286 * @ctxt: the XInclude processing context
Owen Taylor3473f882001-02-23 17:55:21 +00002287 * @node: an XInclude node
2288 *
2289 * test if the node is an XInclude node
2290 *
2291 * Returns 1 true, 0 otherwise
2292 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002293static int
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002294xmlXIncludeTestNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) {
Owen Taylor3473f882001-02-23 17:55:21 +00002295 if (node == NULL)
2296 return(0);
Daniel Veillardffe4f5e2003-07-06 17:35:43 +00002297 if (node->type != XML_ELEMENT_NODE)
2298 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002299 if (node->ns == NULL)
2300 return(0);
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002301 if ((xmlStrEqual(node->ns->href, XINCLUDE_NS)) ||
2302 (xmlStrEqual(node->ns->href, XINCLUDE_OLD_NS))) {
2303 if (xmlStrEqual(node->ns->href, XINCLUDE_OLD_NS)) {
2304 if (ctxt->legacy == 0) {
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +00002305#if 0 /* wait for the XML Core Working Group to get something stable ! */
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002306 xmlXIncludeWarn(ctxt, node, XML_XINCLUDE_DEPRECATED_NS,
2307 "Deprecated XInclude namespace found, use %s",
2308 XINCLUDE_NS);
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +00002309#endif
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002310 ctxt->legacy = 1;
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002311 }
2312 }
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002313 if (xmlStrEqual(node->name, XINCLUDE_NODE)) {
2314 xmlNodePtr child = node->children;
2315 int nb_fallback = 0;
2316
2317 while (child != NULL) {
2318 if ((child->type == XML_ELEMENT_NODE) &&
2319 (child->ns != NULL) &&
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002320 ((xmlStrEqual(child->ns->href, XINCLUDE_NS)) ||
2321 (xmlStrEqual(child->ns->href, XINCLUDE_OLD_NS)))) {
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002322 if (xmlStrEqual(child->name, XINCLUDE_NODE)) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002323 xmlXIncludeErr(ctxt, node,
2324 XML_XINCLUDE_INCLUDE_IN_INCLUDE,
2325 "%s has an 'include' child\n",
2326 XINCLUDE_NODE);
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002327 return(0);
2328 }
2329 if (xmlStrEqual(child->name, XINCLUDE_FALLBACK)) {
2330 nb_fallback++;
2331 }
2332 }
2333 child = child->next;
2334 }
2335 if (nb_fallback > 1) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002336 xmlXIncludeErr(ctxt, node, XML_XINCLUDE_FALLBACKS_IN_INCLUDE,
2337 "%s has multiple fallback children\n",
2338 XINCLUDE_NODE);
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002339 return(0);
2340 }
2341 return(1);
2342 }
2343 if (xmlStrEqual(node->name, XINCLUDE_FALLBACK)) {
2344 if ((node->parent == NULL) ||
2345 (node->parent->type != XML_ELEMENT_NODE) ||
2346 (node->parent->ns == NULL) ||
Daniel Veillardb5fa0202003-12-08 17:41:29 +00002347 ((!xmlStrEqual(node->parent->ns->href, XINCLUDE_NS)) &&
2348 (!xmlStrEqual(node->parent->ns->href, XINCLUDE_OLD_NS))) ||
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002349 (!xmlStrEqual(node->parent->name, XINCLUDE_NODE))) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +00002350 xmlXIncludeErr(ctxt, node,
2351 XML_XINCLUDE_FALLBACK_NOT_IN_INCLUDE,
2352 "%s is not the child of an 'include'\n",
2353 XINCLUDE_FALLBACK);
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002354 }
2355 }
2356 }
Owen Taylor3473f882001-02-23 17:55:21 +00002357 return(0);
2358}
2359
2360/**
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002361 * xmlXIncludeDoProcess:
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002362 * @ctxt: the XInclude processing context
Owen Taylor3473f882001-02-23 17:55:21 +00002363 * @doc: an XML document
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002364 * @tree: the top of the tree to process
Haibo Huangf0a546b2020-09-01 20:28:19 -07002365 * @skipRoot: don't process the root node of the tree
Owen Taylor3473f882001-02-23 17:55:21 +00002366 *
2367 * Implement the XInclude substitution on the XML document @doc
2368 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002369 * Returns 0 if no substitution were done, -1 if some processing failed
Owen Taylor3473f882001-02-23 17:55:21 +00002370 * or the number of substitutions done.
2371 */
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002372static int
Haibo Huangf0a546b2020-09-01 20:28:19 -07002373xmlXIncludeDoProcess(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr tree,
2374 int skipRoot) {
Owen Taylor3473f882001-02-23 17:55:21 +00002375 xmlNodePtr cur;
2376 int ret = 0;
Daniel Veillarde0fd93f2005-08-10 13:39:10 +00002377 int i, start;
Owen Taylor3473f882001-02-23 17:55:21 +00002378
Daniel Veillard3e62adb2012-08-09 14:24:02 +08002379 if ((doc == NULL) || (tree == NULL) || (tree->type == XML_NAMESPACE_DECL))
Owen Taylor3473f882001-02-23 17:55:21 +00002380 return(-1);
Haibo Huangf0a546b2020-09-01 20:28:19 -07002381 if ((skipRoot) && (tree->children == NULL))
2382 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002383 if (ctxt == NULL)
2384 return(-1);
2385
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002386 if (doc->URL != NULL) {
2387 ret = xmlXIncludeURLPush(ctxt, doc->URL);
2388 if (ret < 0)
2389 return(-1);
2390 }
Daniel Veillard11ce4002006-03-10 00:36:23 +00002391 start = ctxt->incNr;
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002392
Owen Taylor3473f882001-02-23 17:55:21 +00002393 /*
Haibo Huangf0a546b2020-09-01 20:28:19 -07002394 * TODO: The phases must run separately for recursive inclusions.
2395 *
2396 * - Phase 1 should start with top-level XInclude nodes, load documents,
2397 * execute XPointer expressions, then process only the result nodes
2398 * (not whole document, see bug #324081) and only for phase 1
2399 * recursively. We will need a backreference from xmlNodes to
2400 * xmlIncludeRefs to detect references that were already visited.
2401 * This can also be used for proper cycle detection, see bug #344240.
2402 *
2403 * - Phase 2 should visit all top-level XInclude nodes and expand
2404 * possible subreferences in the replacement recursively.
2405 *
2406 * - Phase 3 should finally replace the top-level XInclude nodes.
2407 * It could also be run together with phase 2.
2408 */
2409
2410 /*
Owen Taylor3473f882001-02-23 17:55:21 +00002411 * First phase: lookup the elements in the document
2412 */
Haibo Huangf0a546b2020-09-01 20:28:19 -07002413 if (skipRoot)
2414 cur = tree->children;
2415 else
2416 cur = tree;
2417 do {
Owen Taylor3473f882001-02-23 17:55:21 +00002418 /* TODO: need to work on entities -> stack */
Haibo Huangf0a546b2020-09-01 20:28:19 -07002419 if (xmlXIncludeTestNode(ctxt, cur) == 1) {
2420#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
2421 /*
2422 * Avoid superlinear expansion by limiting the total number
2423 * of replacements.
2424 */
2425 if (ctxt->incTotal >= 20)
2426 return(-1);
2427#endif
2428 ctxt->incTotal++;
2429 xmlXIncludePreProcessNode(ctxt, cur);
2430 } else if ((cur->children != NULL) &&
Elliott Hughes5cefca72021-05-06 13:23:15 -07002431 ((cur->type == XML_DOCUMENT_NODE) ||
2432 (cur->type == XML_ELEMENT_NODE))) {
Haibo Huangf0a546b2020-09-01 20:28:19 -07002433 cur = cur->children;
2434 continue;
2435 }
2436 do {
2437 if (cur == tree)
2438 break;
2439 if (cur->next != NULL) {
2440 cur = cur->next;
2441 break;
2442 }
2443 cur = cur->parent;
2444 } while (cur != NULL);
2445 } while ((cur != NULL) && (cur != tree));
Owen Taylor3473f882001-02-23 17:55:21 +00002446
2447 /*
2448 * Second Phase : collect the infosets fragments
2449 */
Daniel Veillarde0fd93f2005-08-10 13:39:10 +00002450 for (i = start;i < ctxt->incNr; i++) {
Owen Taylor3473f882001-02-23 17:55:21 +00002451 xmlXIncludeLoadNode(ctxt, i);
Daniel Veillard97fd5672003-02-07 13:01:54 +00002452 ret++;
Owen Taylor3473f882001-02-23 17:55:21 +00002453 }
2454
2455 /*
2456 * Third phase: extend the original document infoset.
William M. Brack6b1a28d2004-02-06 11:24:44 +00002457 *
2458 * Originally we bypassed the inclusion if there were any errors
2459 * encountered on any of the XIncludes. A bug was raised (bug
2460 * 132588) requesting that we output the XIncludes without error,
2461 * so the check for inc!=NULL || xptr!=NULL was put in. This may
2462 * give some other problems in the future, but for now it seems to
2463 * work ok.
2464 *
Owen Taylor3473f882001-02-23 17:55:21 +00002465 */
William M. Brack6b1a28d2004-02-06 11:24:44 +00002466 for (i = ctxt->incBase;i < ctxt->incNr; i++) {
William M. Brack95af5942004-02-08 04:12:49 +00002467 if ((ctxt->incTab[i]->inc != NULL) ||
Haibo Huangf0a546b2020-09-01 20:28:19 -07002468 (ctxt->incTab[i]->emptyFb != 0)) /* (empty fallback) */
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002469 xmlXIncludeIncludeNode(ctxt, i);
Owen Taylor3473f882001-02-23 17:55:21 +00002470 }
2471
Daniel Veillardf4b4f982003-02-13 11:02:08 +00002472 if (doc->URL != NULL)
2473 xmlXIncludeURLPop(ctxt);
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002474 return(ret);
2475}
2476
2477/**
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002478 * xmlXIncludeSetFlags:
2479 * @ctxt: an XInclude processing context
2480 * @flags: a set of xmlParserOption used for parsing XML includes
2481 *
2482 * Set the flags used for further processing of XML resources.
2483 *
2484 * Returns 0 in case of success and -1 in case of error.
2485 */
2486int
2487xmlXIncludeSetFlags(xmlXIncludeCtxtPtr ctxt, int flags) {
2488 if (ctxt == NULL)
2489 return(-1);
2490 ctxt->parseFlags = flags;
2491 return(0);
2492}
Stefan Behnelb9590e92009-08-24 19:45:54 +02002493
2494/**
2495 * xmlXIncludeProcessTreeFlagsData:
2496 * @tree: an XML node
2497 * @flags: a set of xmlParserOption used for parsing XML includes
2498 * @data: application data that will be passed to the parser context
2499 * in the _private field of the parser context(s)
2500 *
2501 * Implement the XInclude substitution on the XML node @tree
2502 *
2503 * Returns 0 if no substitution were done, -1 if some processing failed
2504 * or the number of substitutions done.
2505 */
2506
2507int
2508xmlXIncludeProcessTreeFlagsData(xmlNodePtr tree, int flags, void *data) {
2509 xmlXIncludeCtxtPtr ctxt;
2510 int ret = 0;
2511
Daniel Veillard3e62adb2012-08-09 14:24:02 +08002512 if ((tree == NULL) || (tree->type == XML_NAMESPACE_DECL) ||
2513 (tree->doc == NULL))
Stefan Behnelb9590e92009-08-24 19:45:54 +02002514 return(-1);
2515
2516 ctxt = xmlXIncludeNewContext(tree->doc);
2517 if (ctxt == NULL)
2518 return(-1);
2519 ctxt->_private = data;
2520 ctxt->base = xmlStrdup((xmlChar *)tree->doc->URL);
2521 xmlXIncludeSetFlags(ctxt, flags);
Haibo Huangf0a546b2020-09-01 20:28:19 -07002522 ret = xmlXIncludeDoProcess(ctxt, tree->doc, tree, 0);
Stefan Behnelb9590e92009-08-24 19:45:54 +02002523 if ((ret >= 0) && (ctxt->nbErrors > 0))
2524 ret = -1;
2525
2526 xmlXIncludeFreeContext(ctxt);
2527 return(ret);
2528}
2529
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002530/**
Daniel Veillard681e9042006-09-29 09:16:00 +00002531 * xmlXIncludeProcessFlagsData:
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002532 * @doc: an XML document
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002533 * @flags: a set of xmlParserOption used for parsing XML includes
Daniel Veillard681e9042006-09-29 09:16:00 +00002534 * @data: application data that will be passed to the parser context
2535 * in the _private field of the parser context(s)
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002536 *
2537 * Implement the XInclude substitution on the XML document @doc
2538 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002539 * Returns 0 if no substitution were done, -1 if some processing failed
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002540 * or the number of substitutions done.
2541 */
2542int
Daniel Veillard681e9042006-09-29 09:16:00 +00002543xmlXIncludeProcessFlagsData(xmlDocPtr doc, int flags, void *data) {
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002544 xmlNodePtr tree;
Daniel Veillardd16df9f2001-05-23 13:44:21 +00002545
2546 if (doc == NULL)
2547 return(-1);
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002548 tree = xmlDocGetRootElement(doc);
2549 if (tree == NULL)
2550 return(-1);
Stefan Behnelb9590e92009-08-24 19:45:54 +02002551 return(xmlXIncludeProcessTreeFlagsData(tree, flags, data));
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002552}
2553
2554/**
Daniel Veillard681e9042006-09-29 09:16:00 +00002555 * xmlXIncludeProcessFlags:
2556 * @doc: an XML document
2557 * @flags: a set of xmlParserOption used for parsing XML includes
2558 *
2559 * Implement the XInclude substitution on the XML document @doc
2560 *
2561 * Returns 0 if no substitution were done, -1 if some processing failed
2562 * or the number of substitutions done.
2563 */
2564int
2565xmlXIncludeProcessFlags(xmlDocPtr doc, int flags) {
2566 return xmlXIncludeProcessFlagsData(doc, flags, NULL);
2567}
2568
2569/**
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002570 * xmlXIncludeProcess:
2571 * @doc: an XML document
2572 *
2573 * Implement the XInclude substitution on the XML document @doc
2574 *
2575 * Returns 0 if no substitution were done, -1 if some processing failed
2576 * or the number of substitutions done.
2577 */
2578int
2579xmlXIncludeProcess(xmlDocPtr doc) {
2580 return(xmlXIncludeProcessFlags(doc, 0));
2581}
2582
2583/**
2584 * xmlXIncludeProcessTreeFlags:
2585 * @tree: a node in an XML document
2586 * @flags: a set of xmlParserOption used for parsing XML includes
2587 *
2588 * Implement the XInclude substitution for the given subtree
2589 *
2590 * Returns 0 if no substitution were done, -1 if some processing failed
2591 * or the number of substitutions done.
2592 */
2593int
2594xmlXIncludeProcessTreeFlags(xmlNodePtr tree, int flags) {
2595 xmlXIncludeCtxtPtr ctxt;
2596 int ret = 0;
2597
Daniel Veillard3e62adb2012-08-09 14:24:02 +08002598 if ((tree == NULL) || (tree->type == XML_NAMESPACE_DECL) ||
2599 (tree->doc == NULL))
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002600 return(-1);
2601 ctxt = xmlXIncludeNewContext(tree->doc);
2602 if (ctxt == NULL)
2603 return(-1);
William M. Brackf7789b12004-06-07 08:57:27 +00002604 ctxt->base = xmlNodeGetBase(tree->doc, tree);
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002605 xmlXIncludeSetFlags(ctxt, flags);
Haibo Huangf0a546b2020-09-01 20:28:19 -07002606 ret = xmlXIncludeDoProcess(ctxt, tree->doc, tree, 0);
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002607 if ((ret >= 0) && (ctxt->nbErrors > 0))
2608 ret = -1;
2609
2610 xmlXIncludeFreeContext(ctxt);
2611 return(ret);
2612}
2613
2614/**
Daniel Veillard8edf1c52003-07-22 20:52:14 +00002615 * xmlXIncludeProcessTree:
2616 * @tree: a node in an XML document
2617 *
2618 * Implement the XInclude substitution for the given subtree
2619 *
2620 * Returns 0 if no substitution were done, -1 if some processing failed
2621 * or the number of substitutions done.
2622 */
2623int
2624xmlXIncludeProcessTree(xmlNodePtr tree) {
Daniel Veillarde74d2e12003-12-09 11:35:37 +00002625 return(xmlXIncludeProcessTreeFlags(tree, 0));
Owen Taylor3473f882001-02-23 17:55:21 +00002626}
2627
Daniel Veillard7899c5c2003-11-03 12:31:38 +00002628/**
2629 * xmlXIncludeProcessNode:
2630 * @ctxt: an existing XInclude context
2631 * @node: a node in an XML document
2632 *
2633 * Implement the XInclude substitution for the given subtree reusing
Haibo Huangcfd91dc2020-07-30 23:01:33 -07002634 * the information and data coming from the given context.
Daniel Veillard7899c5c2003-11-03 12:31:38 +00002635 *
2636 * Returns 0 if no substitution were done, -1 if some processing failed
2637 * or the number of substitutions done.
2638 */
2639int
2640xmlXIncludeProcessNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) {
2641 int ret = 0;
2642
Daniel Veillard3e62adb2012-08-09 14:24:02 +08002643 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL) ||
2644 (node->doc == NULL) || (ctxt == NULL))
Daniel Veillard7899c5c2003-11-03 12:31:38 +00002645 return(-1);
Haibo Huangf0a546b2020-09-01 20:28:19 -07002646 ret = xmlXIncludeDoProcess(ctxt, node->doc, node, 0);
Daniel Veillard7899c5c2003-11-03 12:31:38 +00002647 if ((ret >= 0) && (ctxt->nbErrors > 0))
2648 ret = -1;
2649 return(ret);
2650}
2651
Owen Taylor3473f882001-02-23 17:55:21 +00002652#else /* !LIBXML_XINCLUDE_ENABLED */
2653#endif