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