-
Notifications
You must be signed in to change notification settings - Fork 370
/
xinclude.c
2237 lines (2024 loc) · 59.9 KB
/
xinclude.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* xinclude.c : Code to implement XInclude processing
*
* World Wide Web Consortium W3C Last Call Working Draft 10 November 2003
* http://www.w3.org/TR/2003/WD-xinclude-20031110
*
* See Copyright for the status of this software.
*
*/
#define IN_LIBXML
#include "libxml.h"
#include <string.h>
#include <libxml/xmlmemory.h>
#include <libxml/tree.h>
#include <libxml/parser.h>
#include <libxml/uri.h>
#include <libxml/xpath.h>
#include <libxml/xpointer.h>
#include <libxml/parserInternals.h>
#include <libxml/xmlerror.h>
#include <libxml/encoding.h>
#ifdef LIBXML_XINCLUDE_ENABLED
#include <libxml/xinclude.h>
#include "private/buf.h"
#include "private/error.h"
#include "private/parser.h"
#include "private/tree.h"
#include "private/xinclude.h"
#define XINCLUDE_MAX_DEPTH 40
/************************************************************************
* *
* XInclude context handling *
* *
************************************************************************/
/*
* An XInclude context
*/
typedef xmlChar *xmlURL;
typedef struct _xmlXIncludeRef xmlXIncludeRef;
typedef xmlXIncludeRef *xmlXIncludeRefPtr;
struct _xmlXIncludeRef {
xmlChar *URI; /* the fully resolved resource URL */
xmlChar *fragment; /* the fragment in the URI */
xmlChar *base; /* base URI of xi:include element */
xmlNodePtr elem; /* the xi:include element */
xmlNodePtr inc; /* the included copy */
int xml; /* xml or txt */
int fallback; /* fallback was loaded */
int expanding; /* flag to detect inclusion loops */
int replace; /* should the node be replaced? */
};
typedef struct _xmlXIncludeDoc xmlXIncludeDoc;
typedef xmlXIncludeDoc *xmlXIncludeDocPtr;
struct _xmlXIncludeDoc {
xmlDocPtr doc; /* the parsed document */
xmlChar *url; /* the URL */
int expanding; /* flag to detect inclusion loops */
};
typedef struct _xmlXIncludeTxt xmlXIncludeTxt;
typedef xmlXIncludeTxt *xmlXIncludeTxtPtr;
struct _xmlXIncludeTxt {
xmlChar *text; /* text string */
xmlChar *url; /* the URL */
};
struct _xmlXIncludeCtxt {
xmlDocPtr doc; /* the source document */
int incNr; /* number of includes */
int incMax; /* size of includes tab */
xmlXIncludeRefPtr *incTab; /* array of included references */
int txtNr; /* number of unparsed documents */
int txtMax; /* size of unparsed documents tab */
xmlXIncludeTxt *txtTab; /* array of unparsed documents */
int urlNr; /* number of documents stacked */
int urlMax; /* size of document stack */
xmlXIncludeDoc *urlTab; /* document stack */
int nbErrors; /* the number of errors detected */
int fatalErr; /* abort processing */
int errNo; /* error code */
int legacy; /* using XINCLUDE_OLD_NS */
int parseFlags; /* the flags used for parsing XML documents */
void *_private; /* application data */
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
unsigned long incTotal; /* total number of processed inclusions */
#endif
int depth; /* recursion depth */
int isStream; /* streaming mode */
xmlXPathContextPtr xpctxt;
xmlStructuredErrorFunc errorHandler;
void *errorCtxt;
xmlResourceLoader resourceLoader;
void *resourceCtxt;
};
static xmlXIncludeRefPtr
xmlXIncludeExpandNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node);
static int
xmlXIncludeLoadNode(xmlXIncludeCtxtPtr ctxt, xmlXIncludeRefPtr ref);
static int
xmlXIncludeDoProcess(xmlXIncludeCtxtPtr ctxt, xmlNodePtr tree);
/************************************************************************
* *
* XInclude error handler *
* *
************************************************************************/
/**
* xmlXIncludeErrMemory:
* @extra: extra information
*
* Handle an out of memory condition
*/
static void
xmlXIncludeErrMemory(xmlXIncludeCtxtPtr ctxt)
{
ctxt->errNo = XML_ERR_NO_MEMORY;
ctxt->fatalErr = 1;
ctxt->nbErrors++;
xmlRaiseMemoryError(ctxt->errorHandler, NULL, ctxt->errorCtxt,
XML_FROM_XINCLUDE, NULL);
}
/**
* xmlXIncludeErr:
* @ctxt: the XInclude context
* @node: the context node
* @msg: the error message
* @extra: extra information
*
* Handle an XInclude error
*/
static void LIBXML_ATTR_FORMAT(4,0)
xmlXIncludeErr(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node, int error,
const char *msg, const xmlChar *extra)
{
xmlStructuredErrorFunc schannel = NULL;
xmlGenericErrorFunc channel = NULL;
void *data = NULL;
int res;
if (error == XML_ERR_NO_MEMORY) {
xmlXIncludeErrMemory(ctxt);
return;
}
if (ctxt->fatalErr != 0)
return;
ctxt->nbErrors++;
schannel = ctxt->errorHandler;
data = ctxt->errorCtxt;
if (schannel == NULL) {
channel = xmlGenericError;
data = xmlGenericErrorContext;
}
res = xmlRaiseError(schannel, channel, data, ctxt, node,
XML_FROM_XINCLUDE, error, XML_ERR_ERROR,
NULL, 0, (const char *) extra, NULL, NULL, 0, 0,
msg, (const char *) extra);
if (res < 0) {
ctxt->errNo = XML_ERR_NO_MEMORY;
ctxt->fatalErr = 1;
} else {
ctxt->errNo = error;
/*
* Note that we treat IO errors except ENOENT as fatal
* although the XInclude spec could be interpreted in a
* way that at least some IO errors should be handled
* gracefully.
*/
if (xmlIsCatastrophicError(XML_ERR_FATAL, error))
ctxt->fatalErr = 1;
}
}
/**
* xmlXIncludeGetProp:
* @ctxt: the XInclude context
* @cur: the node
* @name: the attribute name
*
* Get an XInclude attribute
*
* Returns the value (to be freed) or NULL if not found
*/
static xmlChar *
xmlXIncludeGetProp(xmlXIncludeCtxtPtr ctxt, xmlNodePtr cur,
const xmlChar *name) {
xmlChar *ret;
if (xmlNodeGetAttrValue(cur, name, XINCLUDE_NS, &ret) < 0)
xmlXIncludeErrMemory(ctxt);
if (ret != NULL)
return(ret);
if (ctxt->legacy != 0) {
if (xmlNodeGetAttrValue(cur, name, XINCLUDE_OLD_NS, &ret) < 0)
xmlXIncludeErrMemory(ctxt);
if (ret != NULL)
return(ret);
}
if (xmlNodeGetAttrValue(cur, name, NULL, &ret) < 0)
xmlXIncludeErrMemory(ctxt);
return(ret);
}
/**
* xmlXIncludeFreeRef:
* @ref: the XInclude reference
*
* Free an XInclude reference
*/
static void
xmlXIncludeFreeRef(xmlXIncludeRefPtr ref) {
if (ref == NULL)
return;
if (ref->URI != NULL)
xmlFree(ref->URI);
if (ref->fragment != NULL)
xmlFree(ref->fragment);
if (ref->base != NULL)
xmlFree(ref->base);
xmlFree(ref);
}
/**
* xmlXIncludeNewContext:
* @doc: an XML Document
*
* Creates a new XInclude context
*
* Returns the new set
*/
xmlXIncludeCtxtPtr
xmlXIncludeNewContext(xmlDocPtr doc) {
xmlXIncludeCtxtPtr ret;
if (doc == NULL)
return(NULL);
ret = (xmlXIncludeCtxtPtr) xmlMalloc(sizeof(xmlXIncludeCtxt));
if (ret == NULL)
return(NULL);
memset(ret, 0, sizeof(xmlXIncludeCtxt));
ret->doc = doc;
ret->incNr = 0;
ret->incMax = 0;
ret->incTab = NULL;
ret->nbErrors = 0;
return(ret);
}
/**
* xmlXIncludeFreeContext:
* @ctxt: the XInclude context
*
* Free an XInclude context
*/
void
xmlXIncludeFreeContext(xmlXIncludeCtxtPtr ctxt) {
int i;
if (ctxt == NULL)
return;
if (ctxt->urlTab != NULL) {
for (i = 0; i < ctxt->urlNr; i++) {
xmlFreeDoc(ctxt->urlTab[i].doc);
xmlFree(ctxt->urlTab[i].url);
}
xmlFree(ctxt->urlTab);
}
for (i = 0;i < ctxt->incNr;i++) {
if (ctxt->incTab[i] != NULL)
xmlXIncludeFreeRef(ctxt->incTab[i]);
}
if (ctxt->incTab != NULL)
xmlFree(ctxt->incTab);
if (ctxt->txtTab != NULL) {
for (i = 0;i < ctxt->txtNr;i++) {
xmlFree(ctxt->txtTab[i].text);
xmlFree(ctxt->txtTab[i].url);
}
xmlFree(ctxt->txtTab);
}
if (ctxt->xpctxt != NULL)
xmlXPathFreeContext(ctxt->xpctxt);
xmlFree(ctxt);
}
/**
* xmlXIncludeParseFile:
* @ctxt: the XInclude context
* @URL: the URL or file path
*
* parse a document for XInclude
*/
static xmlDocPtr
xmlXIncludeParseFile(xmlXIncludeCtxtPtr ctxt, const char *URL) {
xmlDocPtr ret = NULL;
xmlParserCtxtPtr pctxt;
xmlParserInputPtr inputStream;
xmlInitParser();
pctxt = xmlNewParserCtxt();
if (pctxt == NULL) {
xmlXIncludeErrMemory(ctxt);
return(NULL);
}
if (ctxt->errorHandler != NULL)
xmlCtxtSetErrorHandler(pctxt, ctxt->errorHandler, ctxt->errorCtxt);
if (ctxt->resourceLoader != NULL)
xmlCtxtSetResourceLoader(pctxt, ctxt->resourceLoader,
ctxt->resourceCtxt);
/*
* pass in the application data to the parser context.
*/
pctxt->_private = ctxt->_private;
/*
* try to ensure that new documents included are actually
* built with the same dictionary as the including document.
*/
if ((ctxt->doc != NULL) && (ctxt->doc->dict != NULL)) {
if (pctxt->dict != NULL)
xmlDictFree(pctxt->dict);
pctxt->dict = ctxt->doc->dict;
xmlDictReference(pctxt->dict);
}
/*
* We set DTDLOAD to make sure that ID attributes declared in
* external DTDs are detected.
*/
xmlCtxtUseOptions(pctxt, ctxt->parseFlags | XML_PARSE_DTDLOAD);
inputStream = xmlLoadResource(pctxt, URL, NULL, XML_RESOURCE_XINCLUDE);
if (inputStream == NULL)
goto error;
if (xmlCtxtPushInput(pctxt, inputStream) < 0) {
xmlFreeInputStream(inputStream);
goto error;
}
xmlParseDocument(pctxt);
if (pctxt->wellFormed) {
ret = pctxt->myDoc;
}
else {
ret = NULL;
if (pctxt->myDoc != NULL)
xmlFreeDoc(pctxt->myDoc);
pctxt->myDoc = NULL;
}
error:
if (xmlCtxtIsCatastrophicError(pctxt))
xmlXIncludeErr(ctxt, NULL, pctxt->errNo, "parser error", NULL);
xmlFreeParserCtxt(pctxt);
return(ret);
}
/**
* xmlXIncludeAddNode:
* @ctxt: the XInclude context
* @cur: the new node
*
* Add a new node to process to an XInclude context
*/
static xmlXIncludeRefPtr
xmlXIncludeAddNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr cur) {
xmlXIncludeRefPtr ref = NULL;
xmlXIncludeRefPtr ret = NULL;
xmlURIPtr uri = NULL;
xmlChar *href = NULL;
xmlChar *parse = NULL;
xmlChar *fragment = NULL;
xmlChar *base = NULL;
xmlChar *tmp;
int xml = 1;
int local = 0;
int res;
if (ctxt == NULL)
return(NULL);
if (cur == NULL)
return(NULL);
/*
* read the attributes
*/
fragment = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_PARSE_XPOINTER);
href = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_HREF);
if (href == NULL) {
if (fragment == NULL) {
xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_NO_HREF,
"href or xpointer must be present\n", parse);
goto error;
}
href = xmlStrdup(BAD_CAST ""); /* @@@@ href is now optional */
if (href == NULL) {
xmlXIncludeErrMemory(ctxt);
goto error;
}
} else if (xmlStrlen(href) > XML_MAX_URI_LENGTH) {
xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_HREF_URI, "URI too long\n",
NULL);
goto error;
}
parse = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_PARSE);
if (parse != NULL) {
if (xmlStrEqual(parse, XINCLUDE_PARSE_XML))
xml = 1;
else if (xmlStrEqual(parse, XINCLUDE_PARSE_TEXT))
xml = 0;
else {
xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_PARSE_VALUE,
"invalid value %s for 'parse'\n", parse);
goto error;
}
}
/*
* Check the URL and remove any fragment identifier
*/
res = xmlParseURISafe((const char *)href, &uri);
if (uri == NULL) {
if (res < 0)
xmlXIncludeErrMemory(ctxt);
else
xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_HREF_URI,
"invalid value href %s\n", href);
goto error;
}
if (uri->fragment != NULL) {
if (ctxt->legacy != 0) {
if (fragment == NULL) {
fragment = (xmlChar *) uri->fragment;
} else {
xmlFree(uri->fragment);
}
} else {
xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_FRAGMENT_ID,
"Invalid fragment identifier in URI %s use the xpointer attribute\n",
href);
goto error;
}
uri->fragment = NULL;
}
tmp = xmlSaveUri(uri);
if (tmp == NULL) {
xmlXIncludeErrMemory(ctxt);
goto error;
}
xmlFree(href);
href = tmp;
/*
* Resolve URI
*/
if (xmlNodeGetBaseSafe(ctxt->doc, cur, &base) < 0) {
xmlXIncludeErrMemory(ctxt);
goto error;
}
if (href[0] != 0) {
if (xmlBuildURISafe(href, base, &tmp) < 0) {
xmlXIncludeErrMemory(ctxt);
goto error;
}
if (tmp == NULL) {
xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_HREF_URI,
"failed build URL\n", NULL);
goto error;
}
xmlFree(href);
href = tmp;
if (xmlStrEqual(href, ctxt->doc->URL))
local = 1;
} else {
local = 1;
}
/*
* If local and xml then we need a fragment
*/
if ((local == 1) && (xml == 1) &&
((fragment == NULL) || (fragment[0] == 0))) {
xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_RECURSION,
"detected a local recursion with no xpointer in %s\n",
href);
goto error;
}
ref = (xmlXIncludeRefPtr) xmlMalloc(sizeof(xmlXIncludeRef));
if (ref == NULL) {
xmlXIncludeErrMemory(ctxt);
goto error;
}
memset(ref, 0, sizeof(xmlXIncludeRef));
ref->elem = cur;
ref->xml = xml;
ref->URI = href;
href = NULL;
ref->fragment = fragment;
fragment = NULL;
/*
* xml:base fixup
*/
if (((ctxt->parseFlags & XML_PARSE_NOBASEFIX) == 0) &&
(cur->doc != NULL) &&
((cur->doc->parseFlags & XML_PARSE_NOBASEFIX) == 0)) {
if (base != NULL) {
ref->base = base;
base = NULL;
} else {
ref->base = xmlStrdup(BAD_CAST "");
if (ref->base == NULL) {
xmlXIncludeErrMemory(ctxt);
goto error;
}
}
}
if (ctxt->incNr >= ctxt->incMax) {
xmlXIncludeRefPtr *table;
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
size_t newSize = ctxt->incMax ? ctxt->incMax * 2 : 1;
#else
size_t newSize = ctxt->incMax ? ctxt->incMax * 2 : 4;
#endif
table = (xmlXIncludeRefPtr *) xmlRealloc(ctxt->incTab,
newSize * sizeof(ctxt->incTab[0]));
if (table == NULL) {
xmlXIncludeErrMemory(ctxt);
goto error;
}
ctxt->incTab = table;
ctxt->incMax = newSize;
}
ctxt->incTab[ctxt->incNr++] = ref;
ret = ref;
ref = NULL;
error:
xmlXIncludeFreeRef(ref);
xmlFreeURI(uri);
xmlFree(href);
xmlFree(parse);
xmlFree(fragment);
xmlFree(base);
return(ret);
}
/**
* xmlXIncludeRecurseDoc:
* @ctxt: the XInclude context
* @doc: the new document
* @url: the associated URL
*
* The XInclude recursive nature is handled at this point.
*/
static void
xmlXIncludeRecurseDoc(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc) {
xmlDocPtr oldDoc;
xmlXIncludeRefPtr *oldIncTab;
int oldIncMax, oldIncNr, oldIsStream;
int i;
oldDoc = ctxt->doc;
oldIncMax = ctxt->incMax;
oldIncNr = ctxt->incNr;
oldIncTab = ctxt->incTab;
oldIsStream = ctxt->isStream;
ctxt->doc = doc;
ctxt->incMax = 0;
ctxt->incNr = 0;
ctxt->incTab = NULL;
ctxt->isStream = 0;
xmlXIncludeDoProcess(ctxt, xmlDocGetRootElement(doc));
if (ctxt->incTab != NULL) {
for (i = 0; i < ctxt->incNr; i++)
xmlXIncludeFreeRef(ctxt->incTab[i]);
xmlFree(ctxt->incTab);
}
ctxt->doc = oldDoc;
ctxt->incMax = oldIncMax;
ctxt->incNr = oldIncNr;
ctxt->incTab = oldIncTab;
ctxt->isStream = oldIsStream;
}
/************************************************************************
* *
* Node copy with specific semantic *
* *
************************************************************************/
static void
xmlXIncludeBaseFixup(xmlXIncludeCtxtPtr ctxt, xmlNodePtr cur, xmlNodePtr copy,
const xmlChar *targetBase) {
xmlChar *base = NULL;
xmlChar *relBase = NULL;
xmlNs ns;
int res;
if (cur->type != XML_ELEMENT_NODE)
return;
if (xmlNodeGetBaseSafe(cur->doc, cur, &base) < 0)
xmlXIncludeErrMemory(ctxt);
if ((base != NULL) && !xmlStrEqual(base, targetBase)) {
if ((xmlStrlen(base) > XML_MAX_URI_LENGTH) ||
(xmlStrlen(targetBase) > XML_MAX_URI_LENGTH)) {
relBase = xmlStrdup(base);
if (relBase == NULL) {
xmlXIncludeErrMemory(ctxt);
goto done;
}
} else if (xmlBuildRelativeURISafe(base, targetBase, &relBase) < 0) {
xmlXIncludeErrMemory(ctxt);
goto done;
}
if (relBase == NULL) {
xmlXIncludeErr(ctxt, cur,
XML_XINCLUDE_HREF_URI,
"Building relative URI failed: %s\n",
base);
goto done;
}
/*
* If the new base doesn't contain a slash, it can be omitted.
*/
if (xmlStrchr(relBase, '/') != NULL) {
res = xmlNodeSetBase(copy, relBase);
if (res < 0)
xmlXIncludeErrMemory(ctxt);
goto done;
}
}
/*
* Delete existing xml:base if bases are equal
*/
memset(&ns, 0, sizeof(ns));
ns.href = XML_XML_NAMESPACE;
xmlUnsetNsProp(copy, &ns, BAD_CAST "base");
done:
xmlFree(base);
xmlFree(relBase);
}
/**
* xmlXIncludeCopyNode:
* @ctxt: the XInclude context
* @elem: the element
* @copyChildren: copy children instead of node if true
*
* Make a copy of the node while expanding nested XIncludes.
*
* Returns a node list, not a single node.
*/
static xmlNodePtr
xmlXIncludeCopyNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr elem,
int copyChildren, const xmlChar *targetBase) {
xmlNodePtr result = NULL;
xmlNodePtr insertParent = NULL;
xmlNodePtr insertLast = NULL;
xmlNodePtr cur;
xmlNodePtr item;
int depth = 0;
if (copyChildren) {
cur = elem->children;
if (cur == NULL)
return(NULL);
} else {
cur = elem;
}
while (1) {
xmlNodePtr copy = NULL;
int recurse = 0;
if ((cur->type == XML_DOCUMENT_NODE) ||
(cur->type == XML_DTD_NODE)) {
;
} else if ((cur->type == XML_ELEMENT_NODE) &&
(cur->ns != NULL) &&
(xmlStrEqual(cur->name, XINCLUDE_NODE)) &&
((xmlStrEqual(cur->ns->href, XINCLUDE_NS)) ||
(xmlStrEqual(cur->ns->href, XINCLUDE_OLD_NS)))) {
xmlXIncludeRefPtr ref = xmlXIncludeExpandNode(ctxt, cur);
if (ref == NULL)
goto error;
/*
* TODO: Insert XML_XINCLUDE_START and XML_XINCLUDE_END nodes
*/
for (item = ref->inc; item != NULL; item = item->next) {
copy = xmlStaticCopyNode(item, ctxt->doc, insertParent, 1);
if (copy == NULL) {
xmlXIncludeErrMemory(ctxt);
goto error;
}
if (result == NULL)
result = copy;
if (insertLast != NULL) {
insertLast->next = copy;
copy->prev = insertLast;
} else if (insertParent != NULL) {
insertParent->children = copy;
}
insertLast = copy;
if ((depth == 0) && (targetBase != NULL))
xmlXIncludeBaseFixup(ctxt, item, copy, targetBase);
}
} else {
copy = xmlStaticCopyNode(cur, ctxt->doc, insertParent, 2);
if (copy == NULL) {
xmlXIncludeErrMemory(ctxt);
goto error;
}
if (result == NULL)
result = copy;
if (insertLast != NULL) {
insertLast->next = copy;
copy->prev = insertLast;
} else if (insertParent != NULL) {
insertParent->children = copy;
}
insertLast = copy;
if ((depth == 0) && (targetBase != NULL))
xmlXIncludeBaseFixup(ctxt, cur, copy, targetBase);
recurse = (cur->type != XML_ENTITY_REF_NODE) &&
(cur->children != NULL);
}
if (recurse) {
cur = cur->children;
insertParent = insertLast;
insertLast = NULL;
depth += 1;
continue;
}
if (cur == elem)
return(result);
while (cur->next == NULL) {
if (insertParent != NULL)
insertParent->last = insertLast;
cur = cur->parent;
if (cur == elem)
return(result);
insertLast = insertParent;
insertParent = insertParent->parent;
depth -= 1;
}
cur = cur->next;
}
error:
xmlFreeNodeList(result);
return(NULL);
}
#ifdef LIBXML_XPTR_ENABLED
/**
* xmlXIncludeCopyXPointer:
* @ctxt: the XInclude context
* @obj: the XPointer result from the evaluation.
*
* Build a node list tree copy of the XPointer result.
* This will drop Attributes and Namespace declarations.
*
* Returns an xmlNodePtr list or NULL.
* the caller has to free the node tree.
*/
static xmlNodePtr
xmlXIncludeCopyXPointer(xmlXIncludeCtxtPtr ctxt, xmlXPathObjectPtr obj,
const xmlChar *targetBase) {
xmlNodePtr list = NULL, last = NULL, copy;
int i;
if ((ctxt == NULL) || (obj == NULL))
return(NULL);
switch (obj->type) {
case XPATH_NODESET: {
xmlNodeSetPtr set = obj->nodesetval;
if (set == NULL)
break;
for (i = 0;i < set->nodeNr;i++) {
xmlNodePtr node;
if (set->nodeTab[i] == NULL)
continue;
switch (set->nodeTab[i]->type) {
case XML_DOCUMENT_NODE:
case XML_HTML_DOCUMENT_NODE:
node = xmlDocGetRootElement(
(xmlDocPtr) set->nodeTab[i]);
if (node == NULL) {
xmlXIncludeErr(ctxt, set->nodeTab[i],
XML_ERR_INTERNAL_ERROR,
"document without root\n", NULL);
continue;
}
break;
case XML_TEXT_NODE:
case XML_CDATA_SECTION_NODE:
case XML_ELEMENT_NODE:
case XML_PI_NODE:
case XML_COMMENT_NODE:
node = set->nodeTab[i];
break;
default:
xmlXIncludeErr(ctxt, set->nodeTab[i],
XML_XINCLUDE_XPTR_RESULT,
"invalid node type in XPtr result\n",
NULL);
continue; /* for */
}
/*
* OPTIMIZE TODO: External documents should already be
* expanded, so xmlDocCopyNode should work as well.
* xmlXIncludeCopyNode is only required for the initial
* document.
*/
copy = xmlXIncludeCopyNode(ctxt, node, 0, targetBase);
if (copy == NULL) {
xmlFreeNodeList(list);
return(NULL);
}
if (last == NULL) {
list = copy;
} else {
while (last->next != NULL)
last = last->next;
copy->prev = last;
last->next = copy;
}
last = copy;
}
break;
}
default:
break;
}
return(list);
}
#endif
/************************************************************************
* *
* XInclude I/O handling *
* *
************************************************************************/
typedef struct _xmlXIncludeMergeData xmlXIncludeMergeData;
typedef xmlXIncludeMergeData *xmlXIncludeMergeDataPtr;
struct _xmlXIncludeMergeData {
xmlDocPtr doc;
xmlXIncludeCtxtPtr ctxt;
};
/**
* xmlXIncludeMergeOneEntity:
* @ent: the entity
* @doc: the including doc
* @name: the entity name
*
* Implements the merge of one entity
*/
static void
xmlXIncludeMergeEntity(void *payload, void *vdata,
const xmlChar *name ATTRIBUTE_UNUSED) {
xmlEntityPtr ent = (xmlEntityPtr) payload;
xmlXIncludeMergeDataPtr data = (xmlXIncludeMergeDataPtr) vdata;
xmlEntityPtr ret, prev;
xmlDocPtr doc;
xmlXIncludeCtxtPtr ctxt;
if ((ent == NULL) || (data == NULL))
return;
ctxt = data->ctxt;
doc = data->doc;
if ((ctxt == NULL) || (doc == NULL))
return;
switch (ent->etype) {
case XML_INTERNAL_PARAMETER_ENTITY:
case XML_EXTERNAL_PARAMETER_ENTITY:
case XML_INTERNAL_PREDEFINED_ENTITY:
return;
case XML_INTERNAL_GENERAL_ENTITY:
case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
break;
}
prev = xmlGetDocEntity(doc, ent->name);
if (prev == NULL) {
ret = xmlAddDocEntity(doc, ent->name, ent->etype, ent->ExternalID,
ent->SystemID, ent->content);
if (ret == NULL) {
xmlXIncludeErrMemory(ctxt);
return;
}
if (ent->URI != NULL) {
ret->URI = xmlStrdup(ent->URI);
if (ret->URI == 0)
xmlXIncludeErrMemory(ctxt);
}
} else {
if (ent->etype != prev->etype)
goto error;
if ((ent->SystemID != NULL) && (prev->SystemID != NULL)) {
if (!xmlStrEqual(ent->SystemID, prev->SystemID))
goto error;
} else if ((ent->ExternalID != NULL) &&
(prev->ExternalID != NULL)) {
if (!xmlStrEqual(ent->ExternalID, prev->ExternalID))
goto error;
} else if ((ent->content != NULL) && (prev->content != NULL)) {
if (!xmlStrEqual(ent->content, prev->content))
goto error;
} else {
goto error;
}
}
return;
error:
switch (ent->etype) {
case XML_INTERNAL_PARAMETER_ENTITY:
case XML_EXTERNAL_PARAMETER_ENTITY:
case XML_INTERNAL_PREDEFINED_ENTITY:
case XML_INTERNAL_GENERAL_ENTITY:
case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
return;
case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
break;
}
xmlXIncludeErr(ctxt, (xmlNodePtr) ent, XML_XINCLUDE_ENTITY_DEF_MISMATCH,
"mismatch in redefinition of entity %s\n",
ent->name);
}
/**