VirtualBox

source: vbox/trunk/src/libs/libxml2-2.9.14/xinclude.c@ 102797

Last change on this file since 102797 was 95312, checked in by vboxsync, 3 years ago

libs/{curl,libxml2}: OSE export fixes, bugref:8515

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette