2 * xinclude.c : Code to implement XInclude processing
4 * World Wide Web Consortium W3C Last Call Working Draft 10 November 2003
5 * http://www.w3.org/TR/2003/WD-xinclude-20031110
7 * See Copyright for the status of this software.
16 #include <libxml/xmlmemory.h>
17 #include <libxml/tree.h>
18 #include <libxml/parser.h>
19 #include <libxml/uri.h>
20 #include <libxml/xpath.h>
21 #include <libxml/xpointer.h>
22 #include <libxml/parserInternals.h>
23 #include <libxml/xmlerror.h>
24 #include <libxml/encoding.h>
25 #include <libxml/globals.h>
27 #ifdef LIBXML_XINCLUDE_ENABLED
28 #include <libxml/xinclude.h>
30 #include "private/buf.h"
31 #include "private/error.h"
32 #include "private/tree.h"
33 #include "private/xinclude.h"
35 #define XINCLUDE_MAX_DEPTH 40
37 /* #define DEBUG_XINCLUDE */
39 #ifdef LIBXML_DEBUG_ENABLED
40 #include <libxml/debugXML.h>
44 /************************************************************************
46 * XInclude context handling *
48 ************************************************************************/
53 typedef xmlChar
*xmlURL
;
55 typedef struct _xmlXIncludeRef xmlXIncludeRef
;
56 typedef xmlXIncludeRef
*xmlXIncludeRefPtr
;
57 struct _xmlXIncludeRef
{
58 xmlChar
*URI
; /* the fully resolved resource URL */
59 xmlChar
*fragment
; /* the fragment in the URI */
60 xmlNodePtr elem
; /* the xi:include element */
61 xmlNodePtr inc
; /* the included copy */
62 int xml
; /* xml or txt */
63 int fallback
; /* fallback was loaded */
64 int emptyFb
; /* flag to show fallback empty */
65 int expanding
; /* flag to detect inclusion loops */
66 int replace
; /* should the node be replaced? */
69 typedef struct _xmlXIncludeDoc xmlXIncludeDoc
;
70 typedef xmlXIncludeDoc
*xmlXIncludeDocPtr
;
71 struct _xmlXIncludeDoc
{
72 xmlDocPtr doc
; /* the parsed document */
73 xmlChar
*url
; /* the URL */
74 int expanding
; /* flag to detect inclusion loops */
77 typedef struct _xmlXIncludeTxt xmlXIncludeTxt
;
78 typedef xmlXIncludeTxt
*xmlXIncludeTxtPtr
;
79 struct _xmlXIncludeTxt
{
80 xmlChar
*text
; /* text string */
81 xmlChar
*url
; /* the URL */
84 struct _xmlXIncludeCtxt
{
85 xmlDocPtr doc
; /* the source document */
86 int incNr
; /* number of includes */
87 int incMax
; /* size of includes tab */
88 xmlXIncludeRefPtr
*incTab
; /* array of included references */
90 int txtNr
; /* number of unparsed documents */
91 int txtMax
; /* size of unparsed documents tab */
92 xmlXIncludeTxt
*txtTab
; /* array of unparsed documents */
94 int urlNr
; /* number of documents stacked */
95 int urlMax
; /* size of document stack */
96 xmlXIncludeDoc
*urlTab
; /* document stack */
98 int nbErrors
; /* the number of errors detected */
99 int fatalErr
; /* abort processing */
100 int legacy
; /* using XINCLUDE_OLD_NS */
101 int parseFlags
; /* the flags used for parsing XML documents */
102 xmlChar
* base
; /* the current xml:base */
104 void *_private
; /* application data */
106 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
107 unsigned long incTotal
; /* total number of processed inclusions */
109 int depth
; /* recursion depth */
110 int isStream
; /* streaming mode */
113 static xmlXIncludeRefPtr
114 xmlXIncludeExpandNode(xmlXIncludeCtxtPtr ctxt
, xmlNodePtr node
);
117 xmlXIncludeLoadNode(xmlXIncludeCtxtPtr ctxt
, xmlXIncludeRefPtr ref
);
120 xmlXIncludeDoProcess(xmlXIncludeCtxtPtr ctxt
, xmlNodePtr tree
);
123 /************************************************************************
125 * XInclude error handler *
127 ************************************************************************/
130 * xmlXIncludeErrMemory:
131 * @extra: extra information
133 * Handle an out of memory condition
136 xmlXIncludeErrMemory(xmlXIncludeCtxtPtr ctxt
, xmlNodePtr node
,
141 __xmlRaiseError(NULL
, NULL
, NULL
, ctxt
, node
, XML_FROM_XINCLUDE
,
142 XML_ERR_NO_MEMORY
, XML_ERR_ERROR
, NULL
, 0,
143 extra
, NULL
, NULL
, 0, 0,
144 "Memory allocation failed : %s\n", extra
);
149 * @ctxt: the XInclude context
150 * @node: the context node
151 * @msg: the error message
152 * @extra: extra information
154 * Handle an XInclude error
156 static void LIBXML_ATTR_FORMAT(4,0)
157 xmlXIncludeErr(xmlXIncludeCtxtPtr ctxt
, xmlNodePtr node
, int error
,
158 const char *msg
, const xmlChar
*extra
)
162 __xmlRaiseError(NULL
, NULL
, NULL
, ctxt
, node
, XML_FROM_XINCLUDE
,
163 error
, XML_ERR_ERROR
, NULL
, 0,
164 (const char *) extra
, NULL
, NULL
, 0, 0,
165 msg
, (const char *) extra
);
171 * @ctxt: the XInclude context
172 * @node: the context node
173 * @msg: the error message
174 * @extra: extra information
176 * Emit an XInclude warning.
178 static void LIBXML_ATTR_FORMAT(4,0)
179 xmlXIncludeWarn(xmlXIncludeCtxtPtr ctxt
, xmlNodePtr node
, int error
,
180 const char *msg
, const xmlChar
*extra
)
182 __xmlRaiseError(NULL
, NULL
, NULL
, ctxt
, node
, XML_FROM_XINCLUDE
,
183 error
, XML_ERR_WARNING
, NULL
, 0,
184 (const char *) extra
, NULL
, NULL
, 0, 0,
185 msg
, (const char *) extra
);
190 * xmlXIncludeGetProp:
191 * @ctxt: the XInclude context
193 * @name: the attribute name
195 * Get an XInclude attribute
197 * Returns the value (to be freed) or NULL if not found
200 xmlXIncludeGetProp(xmlXIncludeCtxtPtr ctxt
, xmlNodePtr cur
,
201 const xmlChar
*name
) {
204 ret
= xmlGetNsProp(cur
, XINCLUDE_NS
, name
);
207 if (ctxt
->legacy
!= 0) {
208 ret
= xmlGetNsProp(cur
, XINCLUDE_OLD_NS
, name
);
212 ret
= xmlGetProp(cur
, name
);
216 * xmlXIncludeFreeRef:
217 * @ref: the XInclude reference
219 * Free an XInclude reference
222 xmlXIncludeFreeRef(xmlXIncludeRefPtr ref
) {
225 #ifdef DEBUG_XINCLUDE
226 xmlGenericError(xmlGenericErrorContext
, "Freeing ref\n");
228 if (ref
->URI
!= NULL
)
230 if (ref
->fragment
!= NULL
)
231 xmlFree(ref
->fragment
);
237 * @ctxt: the XInclude context
238 * @URI: the resource URI
239 * @elem: the xi:include element
241 * Creates a new reference within an XInclude context
243 * Returns the new set
245 static xmlXIncludeRefPtr
246 xmlXIncludeNewRef(xmlXIncludeCtxtPtr ctxt
, const xmlChar
*URI
,
248 xmlXIncludeRefPtr ret
;
250 #ifdef DEBUG_XINCLUDE
251 xmlGenericError(xmlGenericErrorContext
, "New ref %s\n", URI
);
253 ret
= (xmlXIncludeRefPtr
) xmlMalloc(sizeof(xmlXIncludeRef
));
255 xmlXIncludeErrMemory(ctxt
, elem
, "growing XInclude context");
258 memset(ret
, 0, sizeof(xmlXIncludeRef
));
262 ret
->URI
= xmlStrdup(URI
);
263 ret
->fragment
= NULL
;
267 if (ctxt
->incMax
== 0) {
269 ctxt
->incTab
= (xmlXIncludeRefPtr
*) xmlMalloc(ctxt
->incMax
*
270 sizeof(ctxt
->incTab
[0]));
271 if (ctxt
->incTab
== NULL
) {
272 xmlXIncludeErrMemory(ctxt
, elem
, "growing XInclude context");
273 xmlXIncludeFreeRef(ret
);
277 if (ctxt
->incNr
>= ctxt
->incMax
) {
278 xmlXIncludeRefPtr
*tmp
;
279 size_t newSize
= ctxt
->incMax
* 2;
281 tmp
= (xmlXIncludeRefPtr
*) xmlRealloc(ctxt
->incTab
,
282 newSize
* sizeof(ctxt
->incTab
[0]));
284 xmlXIncludeErrMemory(ctxt
, elem
, "growing XInclude context");
285 xmlXIncludeFreeRef(ret
);
291 ctxt
->incTab
[ctxt
->incNr
++] = ret
;
296 * xmlXIncludeNewContext:
297 * @doc: an XML Document
299 * Creates a new XInclude context
301 * Returns the new set
304 xmlXIncludeNewContext(xmlDocPtr doc
) {
305 xmlXIncludeCtxtPtr ret
;
307 #ifdef DEBUG_XINCLUDE
308 xmlGenericError(xmlGenericErrorContext
, "New context\n");
312 ret
= (xmlXIncludeCtxtPtr
) xmlMalloc(sizeof(xmlXIncludeCtxt
));
314 xmlXIncludeErrMemory(NULL
, (xmlNodePtr
) doc
,
315 "creating XInclude context");
318 memset(ret
, 0, sizeof(xmlXIncludeCtxt
));
328 * xmlXIncludeFreeContext:
329 * @ctxt: the XInclude context
331 * Free an XInclude context
334 xmlXIncludeFreeContext(xmlXIncludeCtxtPtr ctxt
) {
337 #ifdef DEBUG_XINCLUDE
338 xmlGenericError(xmlGenericErrorContext
, "Freeing context\n");
342 if (ctxt
->urlTab
!= NULL
) {
343 for (i
= 0; i
< ctxt
->urlNr
; i
++) {
344 xmlFreeDoc(ctxt
->urlTab
[i
].doc
);
345 xmlFree(ctxt
->urlTab
[i
].url
);
347 xmlFree(ctxt
->urlTab
);
349 for (i
= 0;i
< ctxt
->incNr
;i
++) {
350 if (ctxt
->incTab
[i
] != NULL
)
351 xmlXIncludeFreeRef(ctxt
->incTab
[i
]);
353 if (ctxt
->incTab
!= NULL
)
354 xmlFree(ctxt
->incTab
);
355 if (ctxt
->txtTab
!= NULL
) {
356 for (i
= 0;i
< ctxt
->txtNr
;i
++) {
357 xmlFree(ctxt
->txtTab
[i
].text
);
358 xmlFree(ctxt
->txtTab
[i
].url
);
360 xmlFree(ctxt
->txtTab
);
362 if (ctxt
->base
!= NULL
) {
369 * xmlXIncludeParseFile:
370 * @ctxt: the XInclude context
371 * @URL: the URL or file path
373 * parse a document for XInclude
376 xmlXIncludeParseFile(xmlXIncludeCtxtPtr ctxt
, const char *URL
) {
378 xmlParserCtxtPtr pctxt
;
379 xmlParserInputPtr inputStream
;
383 pctxt
= xmlNewParserCtxt();
385 xmlXIncludeErrMemory(ctxt
, NULL
, "cannot allocate parser context");
390 * pass in the application data to the parser context.
392 pctxt
->_private
= ctxt
->_private
;
395 * try to ensure that new documents included are actually
396 * built with the same dictionary as the including document.
398 if ((ctxt
->doc
!= NULL
) && (ctxt
->doc
->dict
!= NULL
)) {
399 if (pctxt
->dict
!= NULL
)
400 xmlDictFree(pctxt
->dict
);
401 pctxt
->dict
= ctxt
->doc
->dict
;
402 xmlDictReference(pctxt
->dict
);
405 xmlCtxtUseOptions(pctxt
, ctxt
->parseFlags
| XML_PARSE_DTDLOAD
);
407 /* Don't read from stdin. */
408 if ((URL
!= NULL
) && (strcmp(URL
, "-") == 0))
411 inputStream
= xmlLoadExternalEntity(URL
, NULL
, pctxt
);
412 if (inputStream
== NULL
) {
413 xmlFreeParserCtxt(pctxt
);
417 inputPush(pctxt
, inputStream
);
419 if (pctxt
->directory
== NULL
)
420 pctxt
->directory
= xmlParserGetDirectory(URL
);
422 pctxt
->loadsubset
|= XML_DETECT_IDS
;
424 xmlParseDocument(pctxt
);
426 if (pctxt
->wellFormed
) {
431 if (pctxt
->myDoc
!= NULL
)
432 xmlFreeDoc(pctxt
->myDoc
);
435 xmlFreeParserCtxt(pctxt
);
441 * xmlXIncludeAddNode:
442 * @ctxt: the XInclude context
445 * Add a new node to process to an XInclude context
447 static xmlXIncludeRefPtr
448 xmlXIncludeAddNode(xmlXIncludeCtxtPtr ctxt
, xmlNodePtr cur
) {
449 xmlXIncludeRefPtr ref
;
452 xmlChar
*fragment
= NULL
;
466 #ifdef DEBUG_XINCLUDE
467 xmlGenericError(xmlGenericErrorContext
, "Add node\n");
470 * read the attributes
472 href
= xmlXIncludeGetProp(ctxt
, cur
, XINCLUDE_HREF
);
474 href
= xmlStrdup(BAD_CAST
""); /* @@@@ href is now optional */
478 parse
= xmlXIncludeGetProp(ctxt
, cur
, XINCLUDE_PARSE
);
480 if (xmlStrEqual(parse
, XINCLUDE_PARSE_XML
))
482 else if (xmlStrEqual(parse
, XINCLUDE_PARSE_TEXT
))
485 xmlXIncludeErr(ctxt
, cur
, XML_XINCLUDE_PARSE_VALUE
,
486 "invalid value %s for 'parse'\n", parse
);
498 base
= xmlNodeGetBase(ctxt
->doc
, cur
);
500 URI
= xmlBuildURI(href
, ctxt
->doc
->URL
);
502 URI
= xmlBuildURI(href
, base
);
508 * Some escaping may be needed
510 escbase
= xmlURIEscape(base
);
511 eschref
= xmlURIEscape(href
);
512 URI
= xmlBuildURI(eschref
, escbase
);
525 xmlXIncludeErr(ctxt
, cur
, XML_XINCLUDE_HREF_URI
,
526 "failed build URL\n", NULL
);
529 fragment
= xmlXIncludeGetProp(ctxt
, cur
, XINCLUDE_PARSE_XPOINTER
);
532 * Check the URL and remove any fragment identifier
534 uri
= xmlParseURI((const char *)URI
);
536 xmlXIncludeErr(ctxt
, cur
, XML_XINCLUDE_HREF_URI
,
537 "invalid value URI %s\n", URI
);
538 if (fragment
!= NULL
)
544 if (uri
->fragment
!= NULL
) {
545 if (ctxt
->legacy
!= 0) {
546 if (fragment
== NULL
) {
547 fragment
= (xmlChar
*) uri
->fragment
;
549 xmlFree(uri
->fragment
);
552 xmlXIncludeErr(ctxt
, cur
, XML_XINCLUDE_FRAGMENT_ID
,
553 "Invalid fragment identifier in URI %s use the xpointer attribute\n",
555 if (fragment
!= NULL
)
561 uri
->fragment
= NULL
;
563 URL
= xmlSaveUri(uri
);
566 xmlXIncludeErr(ctxt
, cur
, XML_XINCLUDE_HREF_URI
,
567 "invalid value URI %s\n", URI
);
568 if (fragment
!= NULL
)
575 if (xmlStrEqual(URL
, ctxt
->doc
->URL
))
579 * If local and xml then we need a fragment
581 if ((local
== 1) && (xml
== 1) &&
582 ((fragment
== NULL
) || (fragment
[0] == 0))) {
583 xmlXIncludeErr(ctxt
, cur
, XML_XINCLUDE_RECURSION
,
584 "detected a local recursion with no xpointer in %s\n",
591 ref
= xmlXIncludeNewRef(ctxt
, URL
, cur
);
597 ref
->fragment
= fragment
;
603 * xmlXIncludeRecurseDoc:
604 * @ctxt: the XInclude context
605 * @doc: the new document
606 * @url: the associated URL
608 * The XInclude recursive nature is handled at this point.
611 xmlXIncludeRecurseDoc(xmlXIncludeCtxtPtr ctxt
, xmlDocPtr doc
,
612 const xmlURL url ATTRIBUTE_UNUSED
) {
614 xmlXIncludeRefPtr
*oldIncTab
;
615 int oldIncMax
, oldIncNr
, oldIsStream
;
619 oldIncMax
= ctxt
->incMax
;
620 oldIncNr
= ctxt
->incNr
;
621 oldIncTab
= ctxt
->incTab
;
622 oldIsStream
= ctxt
->isStream
;
629 xmlXIncludeDoProcess(ctxt
, xmlDocGetRootElement(doc
));
631 if (ctxt
->incTab
!= NULL
) {
632 for (i
= 0; i
< ctxt
->incNr
; i
++)
633 xmlXIncludeFreeRef(ctxt
->incTab
[i
]);
634 xmlFree(ctxt
->incTab
);
638 ctxt
->incMax
= oldIncMax
;
639 ctxt
->incNr
= oldIncNr
;
640 ctxt
->incTab
= oldIncTab
;
641 ctxt
->isStream
= oldIsStream
;
644 /************************************************************************
646 * Node copy with specific semantic *
648 ************************************************************************/
651 * xmlXIncludeCopyNode:
652 * @ctxt: the XInclude context
654 * @copyChildren: copy children instead of node if true
656 * Make a copy of the node while expanding nested XIncludes.
658 * Returns a node list, not a single node.
661 xmlXIncludeCopyNode(xmlXIncludeCtxtPtr ctxt
, xmlNodePtr elem
,
663 xmlNodePtr result
= NULL
;
664 xmlNodePtr insertParent
= NULL
;
665 xmlNodePtr insertLast
= NULL
;
669 cur
= elem
->children
;
677 xmlNodePtr copy
= NULL
;
680 if ((cur
->type
== XML_DOCUMENT_NODE
) ||
681 (cur
->type
== XML_DTD_NODE
)) {
683 } else if ((cur
->type
== XML_ELEMENT_NODE
) &&
685 (xmlStrEqual(cur
->name
, XINCLUDE_NODE
)) &&
686 ((xmlStrEqual(cur
->ns
->href
, XINCLUDE_NS
)) ||
687 (xmlStrEqual(cur
->ns
->href
, XINCLUDE_OLD_NS
)))) {
688 xmlXIncludeRefPtr ref
= xmlXIncludeExpandNode(ctxt
, cur
);
693 * TODO: Insert XML_XINCLUDE_START and XML_XINCLUDE_END nodes
695 if (ref
->inc
!= NULL
) {
696 copy
= xmlStaticCopyNodeList(ref
->inc
, ctxt
->doc
,
702 copy
= xmlStaticCopyNode(cur
, ctxt
->doc
, insertParent
, 2);
706 recurse
= (cur
->type
!= XML_ENTITY_REF_NODE
) &&
707 (cur
->children
!= NULL
);
713 if (insertLast
!= NULL
) {
714 insertLast
->next
= copy
;
715 copy
->prev
= insertLast
;
716 } else if (insertParent
!= NULL
) {
717 insertParent
->children
= copy
;
720 while (insertLast
->next
!= NULL
) {
721 insertLast
= insertLast
->next
;
727 insertParent
= insertLast
;
735 while (cur
->next
== NULL
) {
739 insertParent
->last
= insertLast
;
740 insertLast
= insertParent
;
741 insertParent
= insertParent
->parent
;
748 xmlFreeNodeList(result
);
752 #ifdef LIBXML_XPTR_LOCS_ENABLED
754 * xmlXIncludeGetNthChild:
756 * @no: the child number
758 * Returns the @n'th element child of @cur or NULL
761 xmlXIncludeGetNthChild(xmlNodePtr cur
, int no
) {
763 if ((cur
== NULL
) || (cur
->type
== XML_NAMESPACE_DECL
))
766 for (i
= 0;i
<= no
;cur
= cur
->next
) {
769 if ((cur
->type
== XML_ELEMENT_NODE
) ||
770 (cur
->type
== XML_DOCUMENT_NODE
) ||
771 (cur
->type
== XML_HTML_DOCUMENT_NODE
)) {
780 xmlNodePtr
xmlXPtrAdvanceNode(xmlNodePtr cur
, int *level
); /* in xpointer.c */
782 * xmlXIncludeCopyRange:
783 * @ctxt: the XInclude context
784 * @obj: the XPointer result from the evaluation.
786 * Build a node list tree copy of the XPointer result.
788 * Returns an xmlNodePtr list or NULL.
789 * The caller has to free the node tree.
792 xmlXIncludeCopyRange(xmlXIncludeCtxtPtr ctxt
, xmlXPathObjectPtr range
) {
793 /* pointers to generated nodes */
794 xmlNodePtr list
= NULL
, last
= NULL
, listParent
= NULL
;
795 xmlNodePtr tmp
, tmp2
;
796 /* pointers to traversal nodes */
797 xmlNodePtr start
, cur
, end
;
799 int level
= 0, lastLevel
= 0, endLevel
= 0, endFlag
= 0;
801 if ((ctxt
== NULL
) || (range
== NULL
))
803 if (range
->type
!= XPATH_RANGE
)
805 start
= (xmlNodePtr
) range
->user
;
807 if ((start
== NULL
) || (start
->type
== XML_NAMESPACE_DECL
))
811 return(xmlDocCopyNode(start
, ctxt
->doc
, 1));
812 if (end
->type
== XML_NAMESPACE_DECL
)
816 index1
= range
->index
;
817 index2
= range
->index2
;
819 * level is depth of the current node under consideration
820 * list is the pointer to the root of the output tree
821 * listParent is a pointer to the parent of output tree (within
822 the included file) in case we need to add another level
823 * last is a pointer to the last node added to the output tree
824 * lastLevel is the depth of last (relative to the root)
826 while (cur
!= NULL
) {
828 * Check if our output tree needs a parent
832 /* copy must include namespaces and properties */
833 tmp2
= xmlDocCopyNode(listParent
, ctxt
->doc
, 2);
834 xmlAddChild(tmp2
, list
);
836 listParent
= listParent
->parent
;
843 * Check whether we need to change our insertion point
845 while (level
< lastLevel
) {
849 if (cur
== end
) { /* Are we at the end of the range? */
850 if (cur
->type
== XML_TEXT_NODE
) {
851 const xmlChar
*content
= cur
->content
;
854 if (content
== NULL
) {
855 tmp
= xmlNewDocTextLen(ctxt
->doc
, NULL
, 0);
858 if ((cur
== start
) && (index1
> 1)) {
859 content
+= (index1
- 1);
864 tmp
= xmlNewDocTextLen(ctxt
->doc
, content
, len
);
866 /* single sub text node selection */
869 /* prune and return full set */
870 if (level
== lastLevel
)
871 xmlAddNextSibling(last
, tmp
);
873 xmlAddChild(last
, tmp
);
875 } else { /* ending node not a text node */
876 endLevel
= level
; /* remember the level of the end node */
878 /* last node - need to take care of properties + namespaces */
879 tmp
= xmlDocCopyNode(cur
, ctxt
->doc
, 2);
882 listParent
= cur
->parent
;
885 if (level
== lastLevel
)
886 last
= xmlAddNextSibling(last
, tmp
);
888 last
= xmlAddChild(last
, tmp
);
894 end
= xmlXIncludeGetNthChild(cur
, index2
- 1);
897 if ((cur
== start
) && (index1
> 1)) {
898 cur
= xmlXIncludeGetNthChild(cur
, index1
- 1);
903 level
++; /* increment level to show change */
905 * Now gather the remaining nodes from cur to end
907 continue; /* while */
909 } else if (cur
== start
) { /* Not at the end, are we at start? */
910 if ((cur
->type
== XML_TEXT_NODE
) ||
911 (cur
->type
== XML_CDATA_SECTION_NODE
)) {
912 const xmlChar
*content
= cur
->content
;
914 if (content
== NULL
) {
915 tmp
= xmlNewDocTextLen(ctxt
->doc
, NULL
, 0);
918 content
+= (index1
- 1);
921 tmp
= xmlNewDocText(ctxt
->doc
, content
);
924 listParent
= cur
->parent
;
925 } else { /* Not text node */
927 * start of the range - need to take care of
928 * properties and namespaces
930 tmp
= xmlDocCopyNode(cur
, ctxt
->doc
, 2);
932 listParent
= cur
->parent
;
933 if (index1
> 1) { /* Do we need to position? */
934 cur
= xmlXIncludeGetNthChild(cur
, index1
- 1);
935 level
= lastLevel
= 1;
938 * Now gather the remaining nodes from cur to end
940 continue; /* while */
947 case XML_ELEMENT_DECL
:
948 case XML_ATTRIBUTE_DECL
:
949 case XML_ENTITY_NODE
:
950 /* Do not copy DTD information */
952 case XML_ENTITY_DECL
:
953 /* handle crossing entities -> stack needed */
955 case XML_XINCLUDE_START
:
956 case XML_XINCLUDE_END
:
957 /* don't consider it part of the tree content */
959 case XML_ATTRIBUTE_NODE
:
960 /* Humm, should not happen ! */
964 * Middle of the range - need to take care of
965 * properties and namespaces
967 tmp
= xmlDocCopyNode(cur
, ctxt
->doc
, 2);
971 if (level
== lastLevel
)
972 last
= xmlAddNextSibling(last
, tmp
);
974 last
= xmlAddChild(last
, tmp
);
980 * Skip to next node in document order
982 cur
= xmlXPtrAdvanceNode(cur
, &level
);
983 if (endFlag
&& (level
>= endLevel
))
988 #endif /* LIBXML_XPTR_LOCS_ENABLED */
991 * xmlXIncludeCopyXPointer:
992 * @ctxt: the XInclude context
993 * @obj: the XPointer result from the evaluation.
995 * Build a node list tree copy of the XPointer result.
996 * This will drop Attributes and Namespace declarations.
998 * Returns an xmlNodePtr list or NULL.
999 * the caller has to free the node tree.
1002 xmlXIncludeCopyXPointer(xmlXIncludeCtxtPtr ctxt
, xmlXPathObjectPtr obj
) {
1003 xmlNodePtr list
= NULL
, last
= NULL
, copy
;
1006 if ((ctxt
== NULL
) || (obj
== NULL
))
1008 switch (obj
->type
) {
1009 case XPATH_NODESET
: {
1010 xmlNodeSetPtr set
= obj
->nodesetval
;
1013 for (i
= 0;i
< set
->nodeNr
;i
++) {
1016 if (set
->nodeTab
[i
] == NULL
)
1018 switch (set
->nodeTab
[i
]->type
) {
1019 case XML_DOCUMENT_NODE
:
1020 case XML_HTML_DOCUMENT_NODE
:
1021 node
= xmlDocGetRootElement(
1022 (xmlDocPtr
) set
->nodeTab
[i
]);
1024 xmlXIncludeErr(ctxt
, set
->nodeTab
[i
],
1025 XML_ERR_INTERNAL_ERROR
,
1026 "document without root\n", NULL
);
1031 case XML_CDATA_SECTION_NODE
:
1032 case XML_ELEMENT_NODE
:
1034 case XML_COMMENT_NODE
:
1035 node
= set
->nodeTab
[i
];
1038 xmlXIncludeErr(ctxt
, set
->nodeTab
[i
],
1039 XML_XINCLUDE_XPTR_RESULT
,
1040 "invalid node type in XPtr result\n",
1045 * OPTIMIZE TODO: External documents should already be
1046 * expanded, so xmlDocCopyNode should work as well.
1047 * xmlXIncludeCopyNode is only required for the initial
1050 copy
= xmlXIncludeCopyNode(ctxt
, node
, 0);
1052 xmlFreeNodeList(list
);
1058 while (last
->next
!= NULL
)
1067 #ifdef LIBXML_XPTR_LOCS_ENABLED
1068 case XPATH_LOCATIONSET
: {
1069 xmlLocationSetPtr set
= (xmlLocationSetPtr
) obj
->user
;
1072 for (i
= 0;i
< set
->locNr
;i
++) {
1074 list
= last
= xmlXIncludeCopyXPointer(ctxt
,
1077 xmlAddNextSibling(last
,
1078 xmlXIncludeCopyXPointer(ctxt
, set
->locTab
[i
]));
1080 while (last
->next
!= NULL
)
1087 return(xmlXIncludeCopyRange(ctxt
, obj
));
1089 /* points are ignored in XInclude */
1097 /************************************************************************
1099 * XInclude I/O handling *
1101 ************************************************************************/
1103 typedef struct _xmlXIncludeMergeData xmlXIncludeMergeData
;
1104 typedef xmlXIncludeMergeData
*xmlXIncludeMergeDataPtr
;
1105 struct _xmlXIncludeMergeData
{
1107 xmlXIncludeCtxtPtr ctxt
;
1111 * xmlXIncludeMergeOneEntity:
1113 * @doc: the including doc
1114 * @name: the entity name
1116 * Implements the merge of one entity
1119 xmlXIncludeMergeEntity(void *payload
, void *vdata
,
1120 const xmlChar
*name ATTRIBUTE_UNUSED
) {
1121 xmlEntityPtr ent
= (xmlEntityPtr
) payload
;
1122 xmlXIncludeMergeDataPtr data
= (xmlXIncludeMergeDataPtr
) vdata
;
1123 xmlEntityPtr ret
, prev
;
1125 xmlXIncludeCtxtPtr ctxt
;
1127 if ((ent
== NULL
) || (data
== NULL
))
1131 if ((ctxt
== NULL
) || (doc
== NULL
))
1133 switch (ent
->etype
) {
1134 case XML_INTERNAL_PARAMETER_ENTITY
:
1135 case XML_EXTERNAL_PARAMETER_ENTITY
:
1136 case XML_INTERNAL_PREDEFINED_ENTITY
:
1138 case XML_INTERNAL_GENERAL_ENTITY
:
1139 case XML_EXTERNAL_GENERAL_PARSED_ENTITY
:
1140 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY
:
1143 ret
= xmlAddDocEntity(doc
, ent
->name
, ent
->etype
, ent
->ExternalID
,
1144 ent
->SystemID
, ent
->content
);
1146 if (ent
->URI
!= NULL
)
1147 ret
->URI
= xmlStrdup(ent
->URI
);
1149 prev
= xmlGetDocEntity(doc
, ent
->name
);
1151 if (ent
->etype
!= prev
->etype
)
1154 if ((ent
->SystemID
!= NULL
) && (prev
->SystemID
!= NULL
)) {
1155 if (!xmlStrEqual(ent
->SystemID
, prev
->SystemID
))
1157 } else if ((ent
->ExternalID
!= NULL
) &&
1158 (prev
->ExternalID
!= NULL
)) {
1159 if (!xmlStrEqual(ent
->ExternalID
, prev
->ExternalID
))
1161 } else if ((ent
->content
!= NULL
) && (prev
->content
!= NULL
)) {
1162 if (!xmlStrEqual(ent
->content
, prev
->content
))
1172 switch (ent
->etype
) {
1173 case XML_INTERNAL_PARAMETER_ENTITY
:
1174 case XML_EXTERNAL_PARAMETER_ENTITY
:
1175 case XML_INTERNAL_PREDEFINED_ENTITY
:
1176 case XML_INTERNAL_GENERAL_ENTITY
:
1177 case XML_EXTERNAL_GENERAL_PARSED_ENTITY
:
1179 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY
:
1182 xmlXIncludeErr(ctxt
, (xmlNodePtr
) ent
, XML_XINCLUDE_ENTITY_DEF_MISMATCH
,
1183 "mismatch in redefinition of entity %s\n",
1188 * xmlXIncludeMergeEntities:
1189 * @ctxt: an XInclude context
1190 * @doc: the including doc
1191 * @from: the included doc
1193 * Implements the entity merge
1195 * Returns 0 if merge succeeded, -1 if some processing failed
1198 xmlXIncludeMergeEntities(xmlXIncludeCtxtPtr ctxt
, xmlDocPtr doc
,
1201 xmlDtdPtr target
, source
;
1206 if ((from
== NULL
) || (from
->intSubset
== NULL
))
1209 target
= doc
->intSubset
;
1210 if (target
== NULL
) {
1211 cur
= xmlDocGetRootElement(doc
);
1214 target
= xmlCreateIntSubset(doc
, cur
->name
, NULL
, NULL
);
1219 source
= from
->intSubset
;
1220 if ((source
!= NULL
) && (source
->entities
!= NULL
)) {
1221 xmlXIncludeMergeData data
;
1226 xmlHashScan((xmlHashTablePtr
) source
->entities
,
1227 xmlXIncludeMergeEntity
, &data
);
1229 source
= from
->extSubset
;
1230 if ((source
!= NULL
) && (source
->entities
!= NULL
)) {
1231 xmlXIncludeMergeData data
;
1237 * don't duplicate existing stuff when external subsets are the same
1239 if ((!xmlStrEqual(target
->ExternalID
, source
->ExternalID
)) &&
1240 (!xmlStrEqual(target
->SystemID
, source
->SystemID
))) {
1241 xmlHashScan((xmlHashTablePtr
) source
->entities
,
1242 xmlXIncludeMergeEntity
, &data
);
1249 * xmlXIncludeLoadDoc:
1250 * @ctxt: the XInclude context
1251 * @url: the associated URL
1252 * @ref: an XMLXincludeRefPtr
1254 * Load the document, and store the result in the XInclude context
1256 * Returns 0 in case of success, -1 in case of failure
1259 xmlXIncludeLoadDoc(xmlXIncludeCtxtPtr ctxt
, const xmlChar
*url
,
1260 xmlXIncludeRefPtr ref
) {
1261 xmlXIncludeDocPtr cache
;
1264 xmlChar
*URL
= NULL
;
1265 xmlChar
*fragment
= NULL
;
1269 #ifdef LIBXML_XPTR_ENABLED
1273 #ifdef DEBUG_XINCLUDE
1274 xmlGenericError(xmlGenericErrorContext
, "Loading doc %s\n", url
);
1277 * Check the URL and remove any fragment identifier
1279 uri
= xmlParseURI((const char *)url
);
1281 xmlXIncludeErr(ctxt
, ref
->elem
, XML_XINCLUDE_HREF_URI
,
1282 "invalid value URI %s\n", url
);
1285 if (uri
->fragment
!= NULL
) {
1286 fragment
= (xmlChar
*) uri
->fragment
;
1287 uri
->fragment
= NULL
;
1289 if (ref
->fragment
!= NULL
) {
1290 if (fragment
!= NULL
) xmlFree(fragment
);
1291 fragment
= xmlStrdup(ref
->fragment
);
1293 URL
= xmlSaveUri(uri
);
1296 xmlXIncludeErr(ctxt
, ref
->elem
, XML_XINCLUDE_HREF_URI
,
1297 "invalid value URI %s\n", url
);
1302 * Handling of references to the local document are done
1303 * directly through ctxt->doc.
1305 if ((URL
[0] == 0) || (URL
[0] == '#') ||
1306 ((ctxt
->doc
!= NULL
) && (xmlStrEqual(URL
, ctxt
->doc
->URL
)))) {
1312 * Prevent reloading the document twice.
1314 for (i
= 0; i
< ctxt
->urlNr
; i
++) {
1315 if (xmlStrEqual(URL
, ctxt
->urlTab
[i
].url
)) {
1316 #ifdef DEBUG_XINCLUDE
1317 printf("Already loaded %s\n", URL
);
1319 if (ctxt
->urlTab
[i
].expanding
) {
1320 xmlXIncludeErr(ctxt
, ref
->elem
, XML_XINCLUDE_RECURSION
,
1321 "inclusion loop detected\n", NULL
);
1324 doc
= ctxt
->urlTab
[i
].doc
;
1334 #ifdef DEBUG_XINCLUDE
1335 printf("loading %s\n", URL
);
1337 #ifdef LIBXML_XPTR_ENABLED
1339 * If this is an XPointer evaluation, we want to assure that
1340 * all entities have been resolved prior to processing the
1341 * referenced document
1343 saveFlags
= ctxt
->parseFlags
;
1344 if (fragment
!= NULL
) { /* if this is an XPointer eval */
1345 ctxt
->parseFlags
|= XML_PARSE_NOENT
;
1349 doc
= xmlXIncludeParseFile(ctxt
, (const char *)URL
);
1350 #ifdef LIBXML_XPTR_ENABLED
1351 ctxt
->parseFlags
= saveFlags
;
1354 /* Also cache NULL docs */
1355 if (ctxt
->urlNr
>= ctxt
->urlMax
) {
1356 xmlXIncludeDoc
*tmp
;
1357 size_t newSize
= ctxt
->urlMax
? ctxt
->urlMax
* 2 : 8;
1359 tmp
= xmlRealloc(ctxt
->urlTab
, sizeof(xmlXIncludeDoc
) * newSize
);
1361 xmlXIncludeErrMemory(ctxt
, ref
->elem
,
1362 "growing XInclude URL table");
1366 ctxt
->urlMax
= newSize
;
1369 cacheNr
= ctxt
->urlNr
++;
1370 cache
= &ctxt
->urlTab
[cacheNr
];
1372 cache
->url
= xmlStrdup(URL
);
1373 cache
->expanding
= 0;
1378 * It's possible that the requested URL has been mapped to a
1379 * completely different location (e.g. through a catalog entry).
1380 * To check for this, we compare the URL with that of the doc
1381 * and change it if they disagree (bug 146988).
1383 if (!xmlStrEqual(URL
, doc
->URL
)) {
1385 URL
= xmlStrdup(doc
->URL
);
1389 * Make sure we have all entities fixed up
1391 xmlXIncludeMergeEntities(ctxt
, ctxt
->doc
, doc
);
1394 * We don't need the DTD anymore, free up space
1395 if (doc->intSubset != NULL) {
1396 xmlUnlinkNode((xmlNodePtr) doc->intSubset);
1397 xmlFreeNode((xmlNodePtr) doc->intSubset);
1398 doc->intSubset = NULL;
1400 if (doc->extSubset != NULL) {
1401 xmlUnlinkNode((xmlNodePtr) doc->extSubset);
1402 xmlFreeNode((xmlNodePtr) doc->extSubset);
1403 doc->extSubset = NULL;
1406 cache
->expanding
= 1;
1407 xmlXIncludeRecurseDoc(ctxt
, doc
, URL
);
1408 /* urlTab might be reallocated. */
1409 cache
= &ctxt
->urlTab
[cacheNr
];
1410 cache
->expanding
= 0;
1413 if (fragment
== NULL
) {
1415 * Add the top children list as the replacement copy.
1417 ref
->inc
= xmlDocCopyNode(xmlDocGetRootElement(doc
), ctxt
->doc
, 1);
1419 #ifdef LIBXML_XPTR_ENABLED
1422 * Computes the XPointer expression and make a copy used
1423 * as the replacement copy.
1425 xmlXPathObjectPtr xptr
;
1426 xmlXPathContextPtr xptrctxt
;
1429 if (ctxt
->isStream
&& doc
== ctxt
->doc
) {
1430 xmlXIncludeErr(ctxt
, ref
->elem
, XML_XINCLUDE_XPTR_FAILED
,
1431 "XPointer expressions not allowed in streaming"
1436 xptrctxt
= xmlXPtrNewContext(doc
, NULL
, NULL
);
1437 if (xptrctxt
== NULL
) {
1438 xmlXIncludeErr(ctxt
, ref
->elem
, XML_XINCLUDE_XPTR_FAILED
,
1439 "could not create XPointer context\n", NULL
);
1442 xptr
= xmlXPtrEval(fragment
, xptrctxt
);
1444 xmlXIncludeErr(ctxt
, ref
->elem
, XML_XINCLUDE_XPTR_FAILED
,
1445 "XPointer evaluation failed: #%s\n",
1447 xmlXPathFreeContext(xptrctxt
);
1450 switch (xptr
->type
) {
1451 case XPATH_UNDEFINED
:
1455 #ifdef LIBXML_XPTR_LOCS_ENABLED
1459 case XPATH_XSLT_TREE
:
1460 xmlXIncludeErr(ctxt
, ref
->elem
, XML_XINCLUDE_XPTR_RESULT
,
1461 "XPointer is not a range: #%s\n",
1463 xmlXPathFreeObject(xptr
);
1464 xmlXPathFreeContext(xptrctxt
);
1467 if ((xptr
->nodesetval
== NULL
) ||
1468 (xptr
->nodesetval
->nodeNr
<= 0)) {
1469 xmlXPathFreeObject(xptr
);
1470 xmlXPathFreeContext(xptrctxt
);
1474 #ifdef LIBXML_XPTR_LOCS_ENABLED
1476 case XPATH_LOCATIONSET
:
1480 set
= xptr
->nodesetval
;
1482 for (i
= 0;i
< set
->nodeNr
;i
++) {
1483 if (set
->nodeTab
[i
] == NULL
)
1485 switch (set
->nodeTab
[i
]->type
) {
1486 case XML_ELEMENT_NODE
:
1488 case XML_CDATA_SECTION_NODE
:
1489 case XML_ENTITY_REF_NODE
:
1490 case XML_ENTITY_NODE
:
1492 case XML_COMMENT_NODE
:
1493 case XML_DOCUMENT_NODE
:
1494 case XML_HTML_DOCUMENT_NODE
:
1497 case XML_ATTRIBUTE_NODE
:
1498 xmlXIncludeErr(ctxt
, ref
->elem
,
1499 XML_XINCLUDE_XPTR_RESULT
,
1500 "XPointer selects an attribute: #%s\n",
1502 set
->nodeTab
[i
] = NULL
;
1504 case XML_NAMESPACE_DECL
:
1505 xmlXIncludeErr(ctxt
, ref
->elem
,
1506 XML_XINCLUDE_XPTR_RESULT
,
1507 "XPointer selects a namespace: #%s\n",
1509 set
->nodeTab
[i
] = NULL
;
1511 case XML_DOCUMENT_TYPE_NODE
:
1512 case XML_DOCUMENT_FRAG_NODE
:
1513 case XML_NOTATION_NODE
:
1515 case XML_ELEMENT_DECL
:
1516 case XML_ATTRIBUTE_DECL
:
1517 case XML_ENTITY_DECL
:
1518 case XML_XINCLUDE_START
:
1519 case XML_XINCLUDE_END
:
1520 xmlXIncludeErr(ctxt
, ref
->elem
,
1521 XML_XINCLUDE_XPTR_RESULT
,
1522 "XPointer selects unexpected nodes: #%s\n",
1524 set
->nodeTab
[i
] = NULL
;
1525 set
->nodeTab
[i
] = NULL
;
1530 ref
->inc
= xmlXIncludeCopyXPointer(ctxt
, xptr
);
1531 xmlXPathFreeObject(xptr
);
1532 xmlXPathFreeContext(xptrctxt
);
1537 * Do the xml:base fixup if needed
1539 if ((doc
!= NULL
) && (URL
!= NULL
) &&
1540 (!(ctxt
->parseFlags
& XML_PARSE_NOBASEFIX
)) &&
1541 (!(doc
->parseFlags
& XML_PARSE_NOBASEFIX
))) {
1547 * The base is only adjusted if "necessary", i.e. if the xinclude node
1548 * has a base specified, or the URL is relative
1550 base
= xmlGetNsProp(ref
->elem
, BAD_CAST
"base", XML_XML_NAMESPACE
);
1553 * No xml:base on the xinclude node, so we check whether the
1554 * URI base is different than (relative to) the context base
1556 curBase
= xmlBuildRelativeURI(URL
, ctxt
->base
);
1557 if (curBase
== NULL
) { /* Error return */
1558 xmlXIncludeErr(ctxt
, ref
->elem
, XML_XINCLUDE_HREF_URI
,
1559 "trying to build relative URI from %s\n", URL
);
1561 /* If the URI doesn't contain a slash, it's not relative */
1562 if (!xmlStrchr(curBase
, '/'))
1568 if (base
!= NULL
) { /* Adjustment may be needed */
1570 while (node
!= NULL
) {
1571 /* Only work on element nodes */
1572 if (node
->type
== XML_ELEMENT_NODE
) {
1573 curBase
= xmlNodeGetBase(node
->doc
, node
);
1574 /* If no current base, set it */
1575 if (curBase
== NULL
) {
1576 xmlNodeSetBase(node
, base
);
1579 * If the current base is the same as the
1580 * URL of the document, then reset it to be
1581 * the specified xml:base or the relative URI
1583 if (xmlStrEqual(curBase
, node
->doc
->URL
)) {
1584 xmlNodeSetBase(node
, base
);
1587 * If the element already has an xml:base
1588 * set, then relativise it if necessary
1591 xmlBase
= xmlGetNsProp(node
,
1594 if (xmlBase
!= NULL
) {
1596 relBase
= xmlBuildURI(xmlBase
, base
);
1597 if (relBase
== NULL
) { /* error */
1598 xmlXIncludeErr(ctxt
,
1600 XML_XINCLUDE_HREF_URI
,
1601 "trying to rebuild base from %s\n",
1604 xmlNodeSetBase(node
, relBase
);
1627 * xmlXIncludeLoadTxt:
1628 * @ctxt: the XInclude context
1629 * @url: the associated URL
1630 * @ref: an XMLXincludeRefPtr
1632 * Load the content, and store the result in the XInclude context
1634 * Returns 0 in case of success, -1 in case of failure
1637 xmlXIncludeLoadTxt(xmlXIncludeCtxtPtr ctxt
, const xmlChar
*url
,
1638 xmlXIncludeRefPtr ref
) {
1639 xmlParserInputBufferPtr buf
;
1640 xmlNodePtr node
= NULL
;
1641 xmlURIPtr uri
= NULL
;
1642 xmlChar
*URL
= NULL
;
1645 xmlChar
*encoding
= NULL
;
1646 xmlCharEncoding enc
= (xmlCharEncoding
) 0;
1647 xmlParserCtxtPtr pctxt
= NULL
;
1648 xmlParserInputPtr inputStream
= NULL
;
1650 const xmlChar
*content
;
1653 /* Don't read from stdin. */
1654 if (xmlStrcmp(url
, BAD_CAST
"-") == 0)
1655 url
= BAD_CAST
"./-";
1658 * Check the URL and remove any fragment identifier
1660 uri
= xmlParseURI((const char *)url
);
1662 xmlXIncludeErr(ctxt
, ref
->elem
, XML_XINCLUDE_HREF_URI
,
1663 "invalid value URI %s\n", url
);
1666 if (uri
->fragment
!= NULL
) {
1667 xmlXIncludeErr(ctxt
, ref
->elem
, XML_XINCLUDE_TEXT_FRAGMENT
,
1668 "fragment identifier forbidden for text: %s\n",
1669 (const xmlChar
*) uri
->fragment
);
1672 URL
= xmlSaveUri(uri
);
1674 xmlXIncludeErr(ctxt
, ref
->elem
, XML_XINCLUDE_HREF_URI
,
1675 "invalid value URI %s\n", url
);
1680 * Handling of references to the local document are done
1681 * directly through ctxt->doc.
1684 xmlXIncludeErr(ctxt
, ref
->elem
, XML_XINCLUDE_TEXT_DOCUMENT
,
1685 "text serialization of document not available\n", NULL
);
1690 * Prevent reloading the document twice.
1692 for (i
= 0; i
< ctxt
->txtNr
; i
++) {
1693 if (xmlStrEqual(URL
, ctxt
->txtTab
[i
].url
)) {
1694 node
= xmlNewDocText(ctxt
->doc
, ctxt
->txtTab
[i
].text
);
1700 * Try to get the encoding if available
1702 if (ref
->elem
!= NULL
) {
1703 encoding
= xmlGetProp(ref
->elem
, XINCLUDE_PARSE_ENCODING
);
1705 if (encoding
!= NULL
) {
1707 * TODO: we should not have to remap to the xmlCharEncoding
1708 * predefined set, a better interface than
1709 * xmlParserInputBufferCreateFilename should allow any
1710 * encoding supported by iconv
1712 enc
= xmlParseCharEncoding((const char *) encoding
);
1713 if (enc
== XML_CHAR_ENCODING_ERROR
) {
1714 xmlXIncludeErr(ctxt
, ref
->elem
, XML_XINCLUDE_UNKNOWN_ENCODING
,
1715 "encoding %s not supported\n", encoding
);
1723 pctxt
= xmlNewParserCtxt();
1724 inputStream
= xmlLoadExternalEntity((const char*)URL
, NULL
, pctxt
);
1725 if(inputStream
== NULL
)
1727 buf
= inputStream
->buf
;
1731 xmlCharEncCloseFunc(buf
->encoder
);
1732 buf
->encoder
= xmlGetCharEncodingHandler(enc
);
1733 node
= xmlNewDocText(ctxt
->doc
, NULL
);
1735 xmlXIncludeErrMemory(ctxt
, ref
->elem
, NULL
);
1740 * Scan all chars from the resource and add the to the node
1742 while (xmlParserInputBufferRead(buf
, 4096) > 0)
1745 content
= xmlBufContent(buf
->buffer
);
1746 len
= xmlBufLength(buf
->buffer
);
1747 for (i
= 0; i
< len
;) {
1751 cur
= xmlStringCurrentChar(NULL
, &content
[i
], &l
);
1752 if (!IS_CHAR(cur
)) {
1753 xmlXIncludeErr(ctxt
, ref
->elem
, XML_XINCLUDE_INVALID_CHAR
,
1754 "%s contains invalid char\n", URL
);
1761 xmlNodeAddContentLen(node
, content
, len
);
1763 if (ctxt
->txtNr
>= ctxt
->txtMax
) {
1764 xmlXIncludeTxt
*tmp
;
1765 size_t newSize
= ctxt
->txtMax
? ctxt
->txtMax
* 2 : 8;
1767 tmp
= xmlRealloc(ctxt
->txtTab
, sizeof(xmlXIncludeTxt
) * newSize
);
1769 xmlXIncludeErrMemory(ctxt
, ref
->elem
,
1770 "growing XInclude text table");
1773 ctxt
->txtMax
= newSize
;
1776 ctxt
->txtTab
[ctxt
->txtNr
].text
= xmlStrdup(node
->content
);
1777 ctxt
->txtTab
[ctxt
->txtNr
].url
= xmlStrdup(URL
);
1782 * Add the element as the replacement copy.
1790 xmlFreeInputStream(inputStream
);
1791 xmlFreeParserCtxt(pctxt
);
1799 * xmlXIncludeLoadFallback:
1800 * @ctxt: the XInclude context
1801 * @fallback: the fallback node
1802 * @ref: an XMLXincludeRefPtr
1804 * Load the content of the fallback node, and store the result
1805 * in the XInclude context
1807 * Returns 0 in case of success, -1 in case of failure
1810 xmlXIncludeLoadFallback(xmlXIncludeCtxtPtr ctxt
, xmlNodePtr fallback
,
1811 xmlXIncludeRefPtr ref
) {
1815 if ((fallback
== NULL
) || (fallback
->type
== XML_NAMESPACE_DECL
) ||
1818 if (fallback
->children
!= NULL
) {
1820 * It's possible that the fallback also has 'includes'
1821 * (Bug 129969), so we re-process the fallback just in case
1823 oldNbErrors
= ctxt
->nbErrors
;
1824 ref
->inc
= xmlXIncludeCopyNode(ctxt
, fallback
, 1);
1825 if (ctxt
->nbErrors
> oldNbErrors
)
1827 else if (ref
->inc
== NULL
)
1831 ref
->emptyFb
= 1; /* flag empty callback */
1837 /************************************************************************
1839 * XInclude Processing *
1841 ************************************************************************/
1844 * xmlXIncludeExpandNode:
1845 * @ctxt: an XInclude context
1846 * @node: an XInclude node
1848 * If the XInclude node wasn't processed yet, create a new RefPtr,
1849 * add it to ctxt->incTab and load the included items.
1851 * Returns the new or existing xmlXIncludeRefPtr, or NULL in case of error.
1853 static xmlXIncludeRefPtr
1854 xmlXIncludeExpandNode(xmlXIncludeCtxtPtr ctxt
, xmlNodePtr node
) {
1855 xmlXIncludeRefPtr ref
;
1860 if (ctxt
->depth
>= XINCLUDE_MAX_DEPTH
) {
1861 xmlXIncludeErr(ctxt
, node
, XML_XINCLUDE_RECURSION
,
1862 "maximum recursion depth exceeded\n", NULL
);
1867 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
1869 * The XInclude engine offers no protection against exponential
1870 * expansion attacks similar to "billion laughs". Avoid timeouts by
1871 * limiting the total number of replacements when fuzzing.
1873 if (ctxt
->incTotal
>= 20)
1878 for (i
= 0; i
< ctxt
->incNr
; i
++) {
1879 if (ctxt
->incTab
[i
]->elem
== node
) {
1880 if (ctxt
->incTab
[i
]->expanding
) {
1881 xmlXIncludeErr(ctxt
, node
, XML_XINCLUDE_RECURSION
,
1882 "inclusion loop detected\n", NULL
);
1885 return(ctxt
->incTab
[i
]);
1889 ref
= xmlXIncludeAddNode(ctxt
, node
);
1894 xmlXIncludeLoadNode(ctxt
, ref
);
1902 * xmlXIncludeLoadNode:
1903 * @ctxt: an XInclude context
1904 * @ref: an xmlXIncludeRefPtr
1906 * Find and load the infoset replacement for the given node.
1908 * Returns 0 if substitution succeeded, -1 if some processing failed
1911 xmlXIncludeLoadNode(xmlXIncludeCtxtPtr ctxt
, xmlXIncludeRefPtr ref
) {
1918 int xml
= 1; /* default Issue 64 */
1921 if ((ctxt
== NULL
) || (ref
== NULL
))
1928 * read the attributes
1930 href
= xmlXIncludeGetProp(ctxt
, cur
, XINCLUDE_HREF
);
1932 href
= xmlStrdup(BAD_CAST
""); /* @@@@ href is now optional */
1936 parse
= xmlXIncludeGetProp(ctxt
, cur
, XINCLUDE_PARSE
);
1937 if (parse
!= NULL
) {
1938 if (xmlStrEqual(parse
, XINCLUDE_PARSE_XML
))
1940 else if (xmlStrEqual(parse
, XINCLUDE_PARSE_TEXT
))
1943 xmlXIncludeErr(ctxt
, cur
, XML_XINCLUDE_PARSE_VALUE
,
1944 "invalid value %s for 'parse'\n", parse
);
1956 base
= xmlNodeGetBase(ctxt
->doc
, cur
);
1958 URI
= xmlBuildURI(href
, ctxt
->doc
->URL
);
1960 URI
= xmlBuildURI(href
, base
);
1966 * Some escaping may be needed
1968 escbase
= xmlURIEscape(base
);
1969 eschref
= xmlURIEscape(href
);
1970 URI
= xmlBuildURI(eschref
, escbase
);
1971 if (escbase
!= NULL
)
1973 if (eschref
!= NULL
)
1977 xmlXIncludeErr(ctxt
, cur
, XML_XINCLUDE_HREF_URI
,
1978 "failed build URL\n", NULL
);
1987 #ifdef DEBUG_XINCLUDE
1988 xmlGenericError(xmlGenericErrorContext
, "parse: %s\n",
1989 xml
? "xml": "text");
1990 xmlGenericError(xmlGenericErrorContext
, "URI: %s\n", URI
);
1994 * Save the base for this include (saving the current one)
1996 oldBase
= ctxt
->base
;
2000 ret
= xmlXIncludeLoadDoc(ctxt
, URI
, ref
);
2001 /* xmlXIncludeGetFragment(ctxt, cur, URI); */
2003 ret
= xmlXIncludeLoadTxt(ctxt
, URI
, ref
);
2007 * Restore the original base before checking for fallback
2009 ctxt
->base
= oldBase
;
2012 xmlNodePtr children
;
2015 * Time to try a fallback if available
2017 #ifdef DEBUG_XINCLUDE
2018 xmlGenericError(xmlGenericErrorContext
, "error looking for fallback\n");
2020 children
= cur
->children
;
2021 while (children
!= NULL
) {
2022 if ((children
->type
== XML_ELEMENT_NODE
) &&
2023 (children
->ns
!= NULL
) &&
2024 (xmlStrEqual(children
->name
, XINCLUDE_FALLBACK
)) &&
2025 ((xmlStrEqual(children
->ns
->href
, XINCLUDE_NS
)) ||
2026 (xmlStrEqual(children
->ns
->href
, XINCLUDE_OLD_NS
)))) {
2027 ret
= xmlXIncludeLoadFallback(ctxt
, children
, ref
);
2030 children
= children
->next
;
2034 xmlXIncludeErr(ctxt
, cur
, XML_XINCLUDE_NO_FALLBACK
,
2035 "could not load %s, and no fallback was found\n",
2054 * xmlXIncludeIncludeNode:
2055 * @ctxt: an XInclude context
2056 * @ref: an xmlXIncludeRefPtr
2058 * Implement the infoset replacement for the given node
2060 * Returns 0 if substitution succeeded, -1 if some processing failed
2063 xmlXIncludeIncludeNode(xmlXIncludeCtxtPtr ctxt
, xmlXIncludeRefPtr ref
) {
2064 xmlNodePtr cur
, end
, list
, tmp
;
2066 if ((ctxt
== NULL
) || (ref
== NULL
))
2069 if ((cur
== NULL
) || (cur
->type
== XML_NAMESPACE_DECL
))
2077 * Check against the risk of generating a multi-rooted document
2079 if ((cur
->parent
!= NULL
) &&
2080 (cur
->parent
->type
!= XML_ELEMENT_NODE
)) {
2084 while (tmp
!= NULL
) {
2085 if (tmp
->type
== XML_ELEMENT_NODE
)
2090 xmlXIncludeErr(ctxt
, ref
->elem
, XML_XINCLUDE_MULTIPLE_ROOT
,
2091 "XInclude error: would result in multiple root nodes\n",
2093 xmlFreeNodeList(list
);
2098 if (ctxt
->parseFlags
& XML_PARSE_NOXINCNODE
) {
2100 * Add the list of nodes
2102 while (list
!= NULL
) {
2106 xmlAddPrevSibling(cur
, end
);
2109 * FIXME: xmlUnlinkNode doesn't coalesce text nodes.
2114 xmlNodePtr child
, next
;
2117 * Change the current node as an XInclude start one, and add an
2121 xmlUnsetProp(cur
, BAD_CAST
"href");
2122 cur
->type
= XML_XINCLUDE_START
;
2123 /* Remove fallback children */
2124 for (child
= cur
->children
; child
!= NULL
; child
= next
) {
2126 xmlUnlinkNode(child
);
2129 end
= xmlNewDocNode(cur
->doc
, cur
->ns
, cur
->name
, NULL
);
2131 xmlXIncludeErr(ctxt
, ref
->elem
, XML_XINCLUDE_BUILD_FAILED
,
2132 "failed to build node\n", NULL
);
2133 xmlFreeNodeList(list
);
2136 end
->type
= XML_XINCLUDE_END
;
2137 xmlAddNextSibling(cur
, end
);
2140 * Add the list of nodes
2142 while (list
!= NULL
) {
2146 xmlAddPrevSibling(end
, cur
);
2155 * xmlXIncludeTestNode:
2156 * @ctxt: the XInclude processing context
2157 * @node: an XInclude node
2159 * test if the node is an XInclude node
2161 * Returns 1 true, 0 otherwise
2164 xmlXIncludeTestNode(xmlXIncludeCtxtPtr ctxt
, xmlNodePtr node
) {
2167 if (node
->type
!= XML_ELEMENT_NODE
)
2169 if (node
->ns
== NULL
)
2171 if ((xmlStrEqual(node
->ns
->href
, XINCLUDE_NS
)) ||
2172 (xmlStrEqual(node
->ns
->href
, XINCLUDE_OLD_NS
))) {
2173 if (xmlStrEqual(node
->ns
->href
, XINCLUDE_OLD_NS
)) {
2174 if (ctxt
->legacy
== 0) {
2175 #if 0 /* wait for the XML Core Working Group to get something stable ! */
2176 xmlXIncludeWarn(ctxt
, node
, XML_XINCLUDE_DEPRECATED_NS
,
2177 "Deprecated XInclude namespace found, use %s",
2183 if (xmlStrEqual(node
->name
, XINCLUDE_NODE
)) {
2184 xmlNodePtr child
= node
->children
;
2185 int nb_fallback
= 0;
2187 while (child
!= NULL
) {
2188 if ((child
->type
== XML_ELEMENT_NODE
) &&
2189 (child
->ns
!= NULL
) &&
2190 ((xmlStrEqual(child
->ns
->href
, XINCLUDE_NS
)) ||
2191 (xmlStrEqual(child
->ns
->href
, XINCLUDE_OLD_NS
)))) {
2192 if (xmlStrEqual(child
->name
, XINCLUDE_NODE
)) {
2193 xmlXIncludeErr(ctxt
, node
,
2194 XML_XINCLUDE_INCLUDE_IN_INCLUDE
,
2195 "%s has an 'include' child\n",
2199 if (xmlStrEqual(child
->name
, XINCLUDE_FALLBACK
)) {
2203 child
= child
->next
;
2205 if (nb_fallback
> 1) {
2206 xmlXIncludeErr(ctxt
, node
, XML_XINCLUDE_FALLBACKS_IN_INCLUDE
,
2207 "%s has multiple fallback children\n",
2213 if (xmlStrEqual(node
->name
, XINCLUDE_FALLBACK
)) {
2214 if ((node
->parent
== NULL
) ||
2215 (node
->parent
->type
!= XML_ELEMENT_NODE
) ||
2216 (node
->parent
->ns
== NULL
) ||
2217 ((!xmlStrEqual(node
->parent
->ns
->href
, XINCLUDE_NS
)) &&
2218 (!xmlStrEqual(node
->parent
->ns
->href
, XINCLUDE_OLD_NS
))) ||
2219 (!xmlStrEqual(node
->parent
->name
, XINCLUDE_NODE
))) {
2220 xmlXIncludeErr(ctxt
, node
,
2221 XML_XINCLUDE_FALLBACK_NOT_IN_INCLUDE
,
2222 "%s is not the child of an 'include'\n",
2231 * xmlXIncludeDoProcess:
2232 * @ctxt: the XInclude processing context
2233 * @tree: the top of the tree to process
2235 * Implement the XInclude substitution on the XML document @doc
2237 * Returns 0 if no substitution were done, -1 if some processing failed
2238 * or the number of substitutions done.
2241 xmlXIncludeDoProcess(xmlXIncludeCtxtPtr ctxt
, xmlNodePtr tree
) {
2242 xmlXIncludeRefPtr ref
;
2247 if ((tree
== NULL
) || (tree
->type
== XML_NAMESPACE_DECL
))
2253 * First phase: lookup the elements in the document
2255 start
= ctxt
->incNr
;
2258 /* TODO: need to work on entities -> stack */
2259 if (xmlXIncludeTestNode(ctxt
, cur
) == 1) {
2260 ref
= xmlXIncludeExpandNode(ctxt
, cur
);
2262 * Mark direct includes.
2266 } else if ((cur
->children
!= NULL
) &&
2267 ((cur
->type
== XML_DOCUMENT_NODE
) ||
2268 (cur
->type
== XML_ELEMENT_NODE
))) {
2269 cur
= cur
->children
;
2275 if (cur
->next
!= NULL
) {
2280 } while (cur
!= NULL
);
2281 } while ((cur
!= NULL
) && (cur
!= tree
));
2284 * Second phase: extend the original document infoset.
2286 for (i
= start
; i
< ctxt
->incNr
; i
++) {
2287 if (ctxt
->incTab
[i
]->replace
!= 0) {
2288 if ((ctxt
->incTab
[i
]->inc
!= NULL
) ||
2289 (ctxt
->incTab
[i
]->emptyFb
!= 0)) { /* (empty fallback) */
2290 xmlXIncludeIncludeNode(ctxt
, ctxt
->incTab
[i
]);
2292 ctxt
->incTab
[i
]->replace
= 0;
2295 * Ignore includes which were added indirectly, for example
2296 * inside xi:fallback elements.
2298 if (ctxt
->incTab
[i
]->inc
!= NULL
) {
2299 xmlFreeNodeList(ctxt
->incTab
[i
]->inc
);
2300 ctxt
->incTab
[i
]->inc
= NULL
;
2306 if (ctxt
->isStream
) {
2308 * incTab references nodes which will eventually be deleted in
2309 * streaming mode. The table is only required for XPointer
2310 * expressions which aren't allowed in streaming mode.
2312 for (i
= 0;i
< ctxt
->incNr
;i
++) {
2313 xmlXIncludeFreeRef(ctxt
->incTab
[i
]);
2322 * xmlXIncludeSetFlags:
2323 * @ctxt: an XInclude processing context
2324 * @flags: a set of xmlParserOption used for parsing XML includes
2326 * Set the flags used for further processing of XML resources.
2328 * Returns 0 in case of success and -1 in case of error.
2331 xmlXIncludeSetFlags(xmlXIncludeCtxtPtr ctxt
, int flags
) {
2334 ctxt
->parseFlags
= flags
;
2339 * xmlXIncludeSetStreamingMode:
2340 * @ctxt: an XInclude processing context
2341 * @mode: whether streaming mode should be enabled
2343 * In streaming mode, XPointer expressions aren't allowed.
2345 * Returns 0 in case of success and -1 in case of error.
2348 xmlXIncludeSetStreamingMode(xmlXIncludeCtxtPtr ctxt
, int mode
) {
2351 ctxt
->isStream
= !!mode
;
2356 * xmlXIncludeProcessTreeFlagsData:
2357 * @tree: an XML node
2358 * @flags: a set of xmlParserOption used for parsing XML includes
2359 * @data: application data that will be passed to the parser context
2360 * in the _private field of the parser context(s)
2362 * Implement the XInclude substitution on the XML node @tree
2364 * Returns 0 if no substitution were done, -1 if some processing failed
2365 * or the number of substitutions done.
2369 xmlXIncludeProcessTreeFlagsData(xmlNodePtr tree
, int flags
, void *data
) {
2370 xmlXIncludeCtxtPtr ctxt
;
2373 if ((tree
== NULL
) || (tree
->type
== XML_NAMESPACE_DECL
) ||
2374 (tree
->doc
== NULL
))
2377 ctxt
= xmlXIncludeNewContext(tree
->doc
);
2380 ctxt
->_private
= data
;
2381 ctxt
->base
= xmlStrdup((xmlChar
*)tree
->doc
->URL
);
2382 xmlXIncludeSetFlags(ctxt
, flags
);
2383 ret
= xmlXIncludeDoProcess(ctxt
, tree
);
2384 if ((ret
>= 0) && (ctxt
->nbErrors
> 0))
2387 xmlXIncludeFreeContext(ctxt
);
2392 * xmlXIncludeProcessFlagsData:
2393 * @doc: an XML document
2394 * @flags: a set of xmlParserOption used for parsing XML includes
2395 * @data: application data that will be passed to the parser context
2396 * in the _private field of the parser context(s)
2398 * Implement the XInclude substitution on the XML document @doc
2400 * Returns 0 if no substitution were done, -1 if some processing failed
2401 * or the number of substitutions done.
2404 xmlXIncludeProcessFlagsData(xmlDocPtr doc
, int flags
, void *data
) {
2409 tree
= xmlDocGetRootElement(doc
);
2412 return(xmlXIncludeProcessTreeFlagsData(tree
, flags
, data
));
2416 * xmlXIncludeProcessFlags:
2417 * @doc: an XML document
2418 * @flags: a set of xmlParserOption used for parsing XML includes
2420 * Implement the XInclude substitution on the XML document @doc
2422 * Returns 0 if no substitution were done, -1 if some processing failed
2423 * or the number of substitutions done.
2426 xmlXIncludeProcessFlags(xmlDocPtr doc
, int flags
) {
2427 return xmlXIncludeProcessFlagsData(doc
, flags
, NULL
);
2431 * xmlXIncludeProcess:
2432 * @doc: an XML document
2434 * Implement the XInclude substitution on the XML document @doc
2436 * Returns 0 if no substitution were done, -1 if some processing failed
2437 * or the number of substitutions done.
2440 xmlXIncludeProcess(xmlDocPtr doc
) {
2441 return(xmlXIncludeProcessFlags(doc
, 0));
2445 * xmlXIncludeProcessTreeFlags:
2446 * @tree: a node in an XML document
2447 * @flags: a set of xmlParserOption used for parsing XML includes
2449 * Implement the XInclude substitution for the given subtree
2451 * Returns 0 if no substitution were done, -1 if some processing failed
2452 * or the number of substitutions done.
2455 xmlXIncludeProcessTreeFlags(xmlNodePtr tree
, int flags
) {
2456 xmlXIncludeCtxtPtr ctxt
;
2459 if ((tree
== NULL
) || (tree
->type
== XML_NAMESPACE_DECL
) ||
2460 (tree
->doc
== NULL
))
2462 ctxt
= xmlXIncludeNewContext(tree
->doc
);
2465 ctxt
->base
= xmlNodeGetBase(tree
->doc
, tree
);
2466 xmlXIncludeSetFlags(ctxt
, flags
);
2467 ret
= xmlXIncludeDoProcess(ctxt
, tree
);
2468 if ((ret
>= 0) && (ctxt
->nbErrors
> 0))
2471 xmlXIncludeFreeContext(ctxt
);
2476 * xmlXIncludeProcessTree:
2477 * @tree: a node in an XML document
2479 * Implement the XInclude substitution for the given subtree
2481 * Returns 0 if no substitution were done, -1 if some processing failed
2482 * or the number of substitutions done.
2485 xmlXIncludeProcessTree(xmlNodePtr tree
) {
2486 return(xmlXIncludeProcessTreeFlags(tree
, 0));
2490 * xmlXIncludeProcessNode:
2491 * @ctxt: an existing XInclude context
2492 * @node: a node in an XML document
2494 * Implement the XInclude substitution for the given subtree reusing
2495 * the information and data coming from the given context.
2497 * Returns 0 if no substitution were done, -1 if some processing failed
2498 * or the number of substitutions done.
2501 xmlXIncludeProcessNode(xmlXIncludeCtxtPtr ctxt
, xmlNodePtr node
) {
2504 if ((node
== NULL
) || (node
->type
== XML_NAMESPACE_DECL
) ||
2505 (node
->doc
== NULL
) || (ctxt
== NULL
))
2507 ret
= xmlXIncludeDoProcess(ctxt
, node
);
2508 if ((ret
>= 0) && (ctxt
->nbErrors
> 0))
2513 #else /* !LIBXML_XINCLUDE_ENABLED */