Bug Summary

File:xinclude.c
Location:line 977, column 4
Description:Value stored to 'index1' is never read

Annotated Source Code

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