forked from GNOME/libxml2
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtree.c
More file actions
8901 lines (8082 loc) · 225 KB
/
tree.c
File metadata and controls
8901 lines (8082 loc) · 225 KB
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
/*
* tree.c : implementation of access function for an XML tree.
*
* References:
* XHTML 1.0 W3C REC: http://www.w3.org/TR/2002/REC-xhtml1-20020801/
*
* See Copyright for the status of this software.
*
* Author: Daniel Veillard
*
*/
/* To avoid EBCDIC trouble when parsing on zOS */
#if defined(__MVS__)
#pragma convert("ISO8859-1")
#endif
#define IN_LIBXML
#include "libxml.h"
#include <string.h> /* for memset() only ! */
#include <stddef.h>
#include <limits.h>
#include <ctype.h>
#include <stdlib.h>
#ifdef LIBXML_ZLIB_ENABLED
#include <zlib.h>
#endif
#include <libxml/tree.h>
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
#include <libxml/uri.h>
#include <libxml/entities.h>
#include <libxml/xmlerror.h>
#include <libxml/parserInternals.h>
#ifdef LIBXML_HTML_ENABLED
#include <libxml/HTMLtree.h>
#endif
#include "private/buf.h"
#include "private/entities.h"
#include "private/error.h"
#include "private/memory.h"
#include "private/io.h"
#include "private/parser.h"
#include "private/tree.h"
#ifndef SIZE_MAX
#define SIZE_MAX ((size_t) -1)
#endif
/*
* Internal variable indicating whether a callback has been registered
* for node creation/destruction. This avoids looking up thread-local
* data if no callback was ever registered.
*/
int xmlRegisterCallbacks = 0;
/************************************************************************
* *
* Forward declarations *
* *
************************************************************************/
static xmlNodePtr
xmlNewEntityRef(xmlDocPtr doc, xmlChar *name);
static xmlNsPtr
xmlNewReconciledNs(xmlNodePtr tree, xmlNsPtr ns);
static xmlAttrPtr
xmlGetPropNodeInternal(const xmlNode *node, const xmlChar *name,
const xmlChar *nsName, int useDTD);
static xmlChar* xmlGetPropNodeValueInternal(const xmlAttr *prop);
static void
xmlBufGetChildContent(xmlBufPtr buf, const xmlNode *tree);
static void
xmlUnlinkNodeInternal(xmlNodePtr cur);
/************************************************************************
* *
* A few static variables and macros *
* *
************************************************************************/
/* #undef xmlStringText */
const xmlChar xmlStringText[] = { 't', 'e', 'x', 't', 0 };
/* #undef xmlStringTextNoenc */
const xmlChar xmlStringTextNoenc[] =
{ 't', 'e', 'x', 't', 'n', 'o', 'e', 'n', 'c', 0 };
/* #undef xmlStringComment */
const xmlChar xmlStringComment[] = { 'c', 'o', 'm', 'm', 'e', 'n', 't', 0 };
static int xmlCompressMode = 0;
#define IS_STR_XML(str) ((str != NULL) && (str[0] == 'x') && \
(str[1] == 'm') && (str[2] == 'l') && (str[3] == 0))
/************************************************************************
* *
* Functions to move to entities.c once the *
* API freeze is smoothen and they can be made public. *
* *
************************************************************************/
#include <libxml/hash.h>
/**
* Do an entity lookup in the DTD entity hash table and
*
* @param dtd A pointer to the DTD to search
* @param name The entity name
* @returns the corresponding entity, if found.
*/
static xmlEntityPtr
xmlGetEntityFromDtd(const xmlDtd *dtd, const xmlChar *name) {
xmlEntitiesTablePtr table;
if((dtd != NULL) && (dtd->entities != NULL)) {
table = (xmlEntitiesTablePtr) dtd->entities;
return((xmlEntityPtr) xmlHashLookup(table, name));
/* return(xmlGetEntityFromTable(table, name)); */
}
return(NULL);
}
/**
* Do an entity lookup in the DTD parameter entity hash table and
*
* @param dtd A pointer to the DTD to search
* @param name The entity name
* @returns the corresponding entity, if found.
*/
static xmlEntityPtr
xmlGetParameterEntityFromDtd(const xmlDtd *dtd, const xmlChar *name) {
xmlEntitiesTablePtr table;
if ((dtd != NULL) && (dtd->pentities != NULL)) {
table = (xmlEntitiesTablePtr) dtd->pentities;
return((xmlEntityPtr) xmlHashLookup(table, name));
/* return(xmlGetEntityFromTable(table, name)); */
}
return(NULL);
}
/************************************************************************
* *
* QName handling helper *
* *
************************************************************************/
/**
* Build a QName from prefix and local name.
*
* Builds the QName `prefix:ncname` in `memory` if there is enough space
* and prefix is not NULL nor empty, otherwise allocate a new string.
* If prefix is NULL or empty it returns ncname.
*
* @param ncname the Name
* @param prefix the prefix
* @param memory preallocated memory
* @param len preallocated memory length
* @returns the new string which must be freed by the caller if different from
* `memory` and `ncname` or NULL in case of error
*/
xmlChar *
xmlBuildQName(const xmlChar *ncname, const xmlChar *prefix,
xmlChar *memory, int len) {
size_t lenn, lenp;
xmlChar *ret;
if ((ncname == NULL) || (len < 0)) return(NULL);
if (prefix == NULL) return((xmlChar *) ncname);
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
/* Make allocation more likely */
if (len > 8)
len = 8;
#endif
lenn = strlen((char *) ncname);
lenp = strlen((char *) prefix);
if (lenn >= SIZE_MAX - lenp - 1)
return(NULL);
if ((memory == NULL) || ((size_t) len < lenn + lenp + 2)) {
ret = xmlMalloc(lenn + lenp + 2);
if (ret == NULL)
return(NULL);
} else {
ret = memory;
}
memcpy(&ret[0], prefix, lenp);
ret[lenp] = ':';
memcpy(&ret[lenp + 1], ncname, lenn);
ret[lenn + lenp + 1] = 0;
return(ret);
}
/**
* Parse an XML qualified name.
*
* @deprecated This function doesn't report malloc failures.
*
* [NS 5] QName ::= (Prefix ':')? LocalPart
*
* [NS 6] Prefix ::= NCName
*
* [NS 7] LocalPart ::= NCName
*
* @param name the full QName
* @param prefix a xmlChar **
* @returns NULL if the name doesn't have a prefix. Otherwise, returns the
* local part, and prefix is updated to get the Prefix. Both the return value
* and the prefix must be freed by the caller.
*/
xmlChar *
xmlSplitQName2(const xmlChar *name, xmlChar **prefix) {
int len = 0;
xmlChar *ret = NULL;
if (prefix == NULL) return(NULL);
*prefix = NULL;
if (name == NULL) return(NULL);
/* nasty but valid */
if (name[0] == ':')
return(NULL);
/*
* we are not trying to validate but just to cut, and yes it will
* work even if this is as set of UTF-8 encoded chars
*/
while ((name[len] != 0) && (name[len] != ':'))
len++;
if ((name[len] == 0) || (name[len+1] == 0))
return(NULL);
*prefix = xmlStrndup(name, len);
if (*prefix == NULL)
return(NULL);
ret = xmlStrdup(&name[len + 1]);
if (ret == NULL) {
if (*prefix != NULL) {
xmlFree(*prefix);
*prefix = NULL;
}
return(NULL);
}
return(ret);
}
/**
* Parse an XML qualified name.
*
* @param name the full QName
* @param len an int *
* @returns NULL if it is not a Qualified Name, otherwise, update len
* with the length in byte of the prefix and return a pointer
* to the start of the name without the prefix
*/
const xmlChar *
xmlSplitQName3(const xmlChar *name, int *len) {
int l = 0;
if (name == NULL) return(NULL);
if (len == NULL) return(NULL);
/* nasty but valid */
if (name[0] == ':')
return(NULL);
/*
* we are not trying to validate but just to cut, and yes it will
* work even if this is as set of UTF-8 encoded chars
*/
while ((name[l] != 0) && (name[l] != ':'))
l++;
if ((name[l] == 0) || (name[l+1] == 0))
return(NULL);
*len = l;
return(&name[l+1]);
}
/**
* Parse a QName.
*
* The return value points to the start of the local
* name in the input string. If the QName has a prefix, it will be
* allocated and stored in `prefixPtr`. This string must be freed by
* the caller. If there's no prefix, `prefixPtr` is set to NULL.
*
* @param name the full QName
* @param prefixPtr pointer to resulting prefix
* @returns the local name or NULL if a memory allocation failed.
*/
const xmlChar *
xmlSplitQName4(const xmlChar *name, xmlChar **prefixPtr) {
xmlChar *prefix;
int l = 0;
if ((name == NULL) || (prefixPtr == NULL))
return(NULL);
*prefixPtr = NULL;
/* nasty but valid */
if (name[0] == ':')
return(name);
/*
* we are not trying to validate but just to cut, and yes it will
* work even if this is as set of UTF-8 encoded chars
*/
while ((name[l] != 0) && (name[l] != ':'))
l++;
/*
* TODO: What about names with multiple colons?
*/
if ((name[l] == 0) || (name[l+1] == 0))
return(name);
prefix = xmlStrndup(name, l);
if (prefix == NULL)
return(NULL);
*prefixPtr = prefix;
return(&name[l+1]);
}
/************************************************************************
* *
* Check Name, NCName and QName strings *
* *
************************************************************************/
/**
* Check that a value conforms to the lexical space of NCName
*
* @param value the value to check
* @param space allow spaces in front and end of the string
* @returns 0 if this validates, a positive error code number otherwise
* and -1 in case of internal or API error.
*/
int
xmlValidateNCName(const xmlChar *value, int space) {
const xmlChar *cur;
if (value == NULL)
return(-1);
cur = value;
if (space) {
while (IS_BLANK_CH(*cur))
cur++;
}
value = cur;
cur = xmlScanName(value, SIZE_MAX, XML_SCAN_NC);
if ((cur == NULL) || (cur == value))
return(1);
if (space) {
while (IS_BLANK_CH(*cur))
cur++;
}
return(*cur != 0);
}
/**
* Check that a value conforms to the lexical space of QName
*
* @param value the value to check
* @param space allow spaces in front and end of the string
* @returns 0 if this validates, a positive error code number otherwise
* and -1 in case of internal or API error.
*/
int
xmlValidateQName(const xmlChar *value, int space) {
const xmlChar *cur;
if (value == NULL)
return(-1);
cur = value;
if (space) {
while (IS_BLANK_CH(*cur))
cur++;
}
value = cur;
cur = xmlScanName(value, SIZE_MAX, XML_SCAN_NC);
if ((cur == NULL) || (cur == value))
return(1);
if (*cur == ':') {
cur += 1;
value = cur;
cur = xmlScanName(value, SIZE_MAX, XML_SCAN_NC);
if ((cur == NULL) || (cur == value))
return(1);
}
if (space) {
while (IS_BLANK_CH(*cur))
cur++;
}
return(*cur != 0);
}
/**
* Check that a value conforms to the lexical space of Name
*
* @param value the value to check
* @param space allow spaces in front and end of the string
* @returns 0 if this validates, a positive error code number otherwise
* and -1 in case of internal or API error.
*/
int
xmlValidateName(const xmlChar *value, int space) {
const xmlChar *cur;
if (value == NULL)
return(-1);
cur = value;
if (space) {
while (IS_BLANK_CH(*cur))
cur++;
}
value = cur;
cur = xmlScanName(value, SIZE_MAX, 0);
if ((cur == NULL) || (cur == value))
return(1);
if (space) {
while (IS_BLANK_CH(*cur))
cur++;
}
return(*cur != 0);
}
/**
* Check that a value conforms to the lexical space of NMToken
*
* @param value the value to check
* @param space allow spaces in front and end of the string
* @returns 0 if this validates, a positive error code number otherwise
* and -1 in case of internal or API error.
*/
int
xmlValidateNMToken(const xmlChar *value, int space) {
const xmlChar *cur;
if (value == NULL)
return(-1);
cur = value;
if (space) {
while (IS_BLANK_CH(*cur))
cur++;
}
value = cur;
cur = xmlScanName(value, SIZE_MAX, XML_SCAN_NMTOKEN);
if ((cur == NULL) || (cur == value))
return(1);
if (space) {
while (IS_BLANK_CH(*cur))
cur++;
}
return(*cur != 0);
}
/************************************************************************
* *
* Allocation and deallocation of basic structures *
* *
************************************************************************/
/**
* Create a new namespace. For a default namespace, `prefix` should be
* NULL. The namespace URI in `href` is not checked. You should make sure
* to pass a valid URI.
*
* If `node` is provided, it must be an element node. The namespace will
* be appended to the node's namespace declarations. It is an error if
* the node already has a definition for the prefix or default
* namespace.
*
* @param node the element carrying the namespace (optional)
* @param href the URI associated
* @param prefix the prefix for the namespace (optional)
* @returns a new namespace pointer or NULL if arguments are invalid,
* the prefix is already in use or a memory allocation failed.
*/
xmlNs *
xmlNewNs(xmlNode *node, const xmlChar *href, const xmlChar *prefix) {
xmlNsPtr cur;
if ((node != NULL) && (node->type != XML_ELEMENT_NODE))
return(NULL);
/*
* Allocate a new Namespace and fill the fields.
*/
cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
if (cur == NULL)
return(NULL);
memset(cur, 0, sizeof(xmlNs));
cur->type = XML_LOCAL_NAMESPACE;
if (href != NULL) {
cur->href = xmlStrdup(href);
if (cur->href == NULL)
goto error;
}
if (prefix != NULL) {
cur->prefix = xmlStrdup(prefix);
if (cur->prefix == NULL)
goto error;
}
/*
* Add it at the end to preserve parsing order ...
* and checks for existing use of the prefix
*/
if (node != NULL) {
if (node->nsDef == NULL) {
node->nsDef = cur;
} else {
xmlNsPtr prev = node->nsDef;
if ((xmlStrEqual(prev->prefix, cur->prefix)) &&
(prev->href != NULL))
goto error;
while (prev->next != NULL) {
prev = prev->next;
if ((xmlStrEqual(prev->prefix, cur->prefix)) &&
(prev->href != NULL))
goto error;
}
prev->next = cur;
}
}
return(cur);
error:
xmlFreeNs(cur);
return(NULL);
}
/**
* Set the namespace of an element or attribute node. Passing a NULL
* namespace unsets the namespace.
*
* @param node a node in the document
* @param ns a namespace pointer (optional)
*/
void
xmlSetNs(xmlNode *node, xmlNs *ns) {
if (node == NULL) {
return;
}
if ((node->type == XML_ELEMENT_NODE) ||
(node->type == XML_ATTRIBUTE_NODE))
node->ns = ns;
}
/**
* Free an xmlNs object.
*
* @param cur the namespace pointer
*/
void
xmlFreeNs(xmlNs *cur) {
if (cur == NULL) {
return;
}
if (cur->href != NULL) xmlFree((char *) cur->href);
if (cur->prefix != NULL) xmlFree((char *) cur->prefix);
xmlFree(cur);
}
/**
* Free a list of xmlNs objects.
*
* @param cur the first namespace pointer
*/
void
xmlFreeNsList(xmlNs *cur) {
xmlNsPtr next;
if (cur == NULL) {
return;
}
while (cur != NULL) {
next = cur->next;
xmlFreeNs(cur);
cur = next;
}
}
/**
* Create a DTD node.
*
* If a document is provided, it is an error if it already has an
* external subset. If the document has no external subset, it
* will be set to the created DTD.
*
* To create an internal subset, use #xmlCreateIntSubset.
*
* @param doc the document pointer (optional)
* @param name the DTD name (optional)
* @param publicId public identifier of the DTD (optional)
* @param systemId system identifier (URL) of the DTD (optional)
* @returns a pointer to the new DTD object or NULL if arguments are
* invalid or a memory allocation failed.
*/
xmlDtd *
xmlNewDtd(xmlDoc *doc, const xmlChar *name, const xmlChar *publicId,
const xmlChar *systemId) {
xmlDtdPtr cur;
if ((doc != NULL) && (doc->extSubset != NULL)) {
return(NULL);
}
/*
* Allocate a new DTD and fill the fields.
*/
cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd));
if (cur == NULL)
return(NULL);
memset(cur, 0 , sizeof(xmlDtd));
cur->type = XML_DTD_NODE;
if (name != NULL) {
cur->name = xmlStrdup(name);
if (cur->name == NULL)
goto error;
}
if (publicId != NULL) {
cur->ExternalID = xmlStrdup(publicId);
if (cur->ExternalID == NULL)
goto error;
}
if (systemId != NULL) {
cur->SystemID = xmlStrdup(systemId);
if (cur->SystemID == NULL)
goto error;
}
if (doc != NULL)
doc->extSubset = cur;
cur->doc = doc;
if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
return(cur);
error:
xmlFreeDtd(cur);
return(NULL);
}
/**
* Get the internal subset of a document.
*
* @param doc the document pointer
* @returns a pointer to the DTD object or NULL if not found.
*/
xmlDtd *
xmlGetIntSubset(const xmlDoc *doc) {
xmlNodePtr cur;
if (doc == NULL)
return(NULL);
cur = doc->children;
while (cur != NULL) {
if (cur->type == XML_DTD_NODE)
return((xmlDtdPtr) cur);
cur = cur->next;
}
return((xmlDtdPtr) doc->intSubset);
}
/**
* Create a DTD node.
*
* If a document is provided and it already has an internal subset,
* the existing DTD object is returned without creating a new object.
* If the document has no internal subset, it will be set to the
* created DTD.
*
* @param doc the document pointer (optional)
* @param name the DTD name (optional)
* @param publicId public identifier of the DTD (optional)
* @param systemId system identifier (URL) of the DTD (optional)
* @returns a pointer to the new or existing DTD object or NULL if
* arguments are invalid or a memory allocation failed.
*/
xmlDtd *
xmlCreateIntSubset(xmlDoc *doc, const xmlChar *name, const xmlChar *publicId,
const xmlChar *systemId) {
xmlDtdPtr cur;
if (doc != NULL) {
cur = xmlGetIntSubset(doc);
if (cur != NULL)
return(cur);
}
/*
* Allocate a new DTD and fill the fields.
*/
cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd));
if (cur == NULL)
return(NULL);
memset(cur, 0, sizeof(xmlDtd));
cur->type = XML_DTD_NODE;
if (name != NULL) {
cur->name = xmlStrdup(name);
if (cur->name == NULL)
goto error;
}
if (publicId != NULL) {
cur->ExternalID = xmlStrdup(publicId);
if (cur->ExternalID == NULL)
goto error;
}
if (systemId != NULL) {
cur->SystemID = xmlStrdup(systemId);
if (cur->SystemID == NULL)
goto error;
}
if (doc != NULL) {
doc->intSubset = cur;
cur->parent = doc;
cur->doc = doc;
if (doc->children == NULL) {
doc->children = (xmlNodePtr) cur;
doc->last = (xmlNodePtr) cur;
} else {
if (doc->type == XML_HTML_DOCUMENT_NODE) {
xmlNodePtr prev;
prev = doc->children;
prev->prev = (xmlNodePtr) cur;
cur->next = prev;
doc->children = (xmlNodePtr) cur;
} else {
xmlNodePtr next;
next = doc->children;
while ((next != NULL) && (next->type != XML_ELEMENT_NODE))
next = next->next;
if (next == NULL) {
cur->prev = doc->last;
cur->prev->next = (xmlNodePtr) cur;
cur->next = NULL;
doc->last = (xmlNodePtr) cur;
} else {
cur->next = next;
cur->prev = next->prev;
if (cur->prev == NULL)
doc->children = (xmlNodePtr) cur;
else
cur->prev->next = (xmlNodePtr) cur;
next->prev = (xmlNodePtr) cur;
}
}
}
}
if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
return(cur);
error:
xmlFreeDtd(cur);
return(NULL);
}
/**
* Free a string if it is not owned by the "dict" dictionary in the
* current scope
*
* @param str a string
*/
#define DICT_FREE(str) \
if ((str) && ((!dict) || \
(xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \
xmlFree((char *)(str));
/**
* Free a DTD structure.
*
* @param cur the DTD structure to free up
*/
void
xmlFreeDtd(xmlDtd *cur) {
xmlDictPtr dict = NULL;
if (cur == NULL) {
return;
}
if (cur->doc != NULL) dict = cur->doc->dict;
if ((xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
xmlDeregisterNodeDefaultValue((xmlNodePtr)cur);
if (cur->children != NULL) {
xmlNodePtr next, c = cur->children;
/*
* Cleanup all nodes which are not part of the specific lists
* of notations, elements, attributes and entities.
*/
while (c != NULL) {
next = c->next;
if ((c->type != XML_ELEMENT_DECL) &&
(c->type != XML_ATTRIBUTE_DECL) &&
(c->type != XML_ENTITY_DECL)) {
xmlUnlinkNodeInternal(c);
xmlFreeNode(c);
}
c = next;
}
}
DICT_FREE(cur->name)
if (cur->SystemID != NULL)
xmlFree(cur->SystemID);
if (cur->ExternalID != NULL)
xmlFree(cur->ExternalID);
/* TODO !!! */
if (cur->notations != NULL)
xmlFreeNotationTable((xmlNotationTablePtr) cur->notations);
if (cur->elements != NULL)
xmlFreeElementTable((xmlElementTablePtr) cur->elements);
if (cur->attributes != NULL)
xmlFreeAttributeTable((xmlAttributeTablePtr) cur->attributes);
if (cur->entities != NULL)
xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->entities);
if (cur->pentities != NULL)
xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->pentities);
xmlFree(cur);
}
/**
* Creates a new XML document. If version is NULL, `"1.0"` is used.
*
* @param version XML version string like `"1.0"` (optional)
* @returns a new document or NULL if a memory allocation failed.
*/
xmlDoc *
xmlNewDoc(const xmlChar *version) {
xmlDocPtr cur;
if (version == NULL)
version = (const xmlChar *) "1.0";
/*
* Allocate a new document and fill the fields.
*/
cur = (xmlDocPtr) xmlMalloc(sizeof(xmlDoc));
if (cur == NULL)
return(NULL);
memset(cur, 0, sizeof(xmlDoc));
cur->type = XML_DOCUMENT_NODE;
cur->version = xmlStrdup(version);
if (cur->version == NULL) {
xmlFree(cur);
return(NULL);
}
cur->standalone = -1;
cur->compression = -1; /* not initialized */
cur->doc = cur;
cur->parseFlags = 0;
cur->properties = XML_DOC_USERBUILT;
/*
* The in memory encoding is always UTF8
* This field will never change and would
* be obsolete if not for binary compatibility.
*/
cur->charset = XML_CHAR_ENCODING_UTF8;
if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
return(cur);
}
/**
* Free a document including all children and associated DTDs.
*
* @param cur pointer to the document
*/
void
xmlFreeDoc(xmlDoc *cur) {
xmlDtdPtr extSubset, intSubset;
xmlDictPtr dict = NULL;
if (cur == NULL) {
return;
}
dict = cur->dict;
if ((xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
xmlDeregisterNodeDefaultValue((xmlNodePtr)cur);
/*
* Do this before freeing the children list to avoid ID lookups
*/
if (cur->ids != NULL) xmlFreeIDTable((xmlIDTablePtr) cur->ids);
cur->ids = NULL;
if (cur->refs != NULL) xmlFreeRefTable((xmlRefTablePtr) cur->refs);
cur->refs = NULL;
extSubset = cur->extSubset;
intSubset = cur->intSubset;
if (intSubset == extSubset)
extSubset = NULL;
if (extSubset != NULL) {
xmlUnlinkNodeInternal((xmlNodePtr) cur->extSubset);
cur->extSubset = NULL;
xmlFreeDtd(extSubset);
}
if (intSubset != NULL) {
xmlUnlinkNodeInternal((xmlNodePtr) cur->intSubset);
cur->intSubset = NULL;
xmlFreeDtd(intSubset);
}
if (cur->children != NULL) xmlFreeNodeList(cur->children);
if (cur->oldNs != NULL) xmlFreeNsList(cur->oldNs);
DICT_FREE(cur->name)
if (cur->version != NULL)
xmlFree(cur->version);
if (cur->encoding != NULL)
xmlFree(cur->encoding);
if (cur->URL != NULL)
xmlFree(cur->URL);
xmlFree(cur);
if (dict) xmlDictFree(dict);
}
/**
* Parse an attribute value and replace the node's children with
* the resulting node list.
*
* `content` is expected to be a valid XML attribute value possibly
* containing character and entity references. Syntax errors
* and references to undeclared entities are ignored silently.
* Only references are handled, nested elements, comments or PIs are
* not.
*
* This function is used by the SAX parser to parse attribute
* values a second time if entities aren't substituted. It's also
* used to parse node content in the tree API, but this is a
* historical mistake.
*
* @param doc a document (optional)
* @param attr an attribute or element (optional)
* @param value an attribute value
* @param len maximum length of the attribute value
* @param listPtr pointer to the resulting node list (optional)
* @returns 0 on success, -1 if a memory allocation failed.
*/
int
xmlNodeParseAttValue(const xmlDoc *doc, xmlAttr *attr,
const xmlChar *value, size_t len, xmlNode **listPtr) {
xmlNodePtr head = NULL, last = NULL;
xmlNodePtr node;