-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmlreader.c
5920 lines (5464 loc) · 163 KB
/
xmlreader.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
/*
* xmlreader.c: implements the xmlTextReader streaming node API
*
* NOTE:
* XmlTextReader.Normalization Property won't be supported, since
* it makes the parser non compliant to the XML recommendation
*
* See Copyright for the status of this software.
*
*/
/*
* TODOs:
* - XML Schemas validation
*/
#define IN_LIBXML
#include "libxml.h"
#ifdef LIBXML_READER_ENABLED
#include <string.h> /* for memset() only ! */
#include <stdarg.h>
#ifdef HAVE_CTYPE_H
#include <ctype.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#include <libxml/xmlmemory.h>
#include <libxml/xmlIO.h>
#include <libxml/xmlreader.h>
#include <libxml/parserInternals.h>
#ifdef LIBXML_SCHEMAS_ENABLED
#include <libxml/relaxng.h>
#include <libxml/xmlschemas.h>
#endif
#include <libxml/uri.h>
#ifdef LIBXML_XINCLUDE_ENABLED
#include <libxml/xinclude.h>
#endif
#ifdef LIBXML_PATTERN_ENABLED
#include <libxml/pattern.h>
#endif
#include "buf.h"
#define MAX_ERR_MSG_SIZE 64000
/*
* The following VA_COPY was coded following an example in
* the Samba project. It may not be sufficient for some
* esoteric implementations of va_list (i.e. it may need
* something involving a memcpy) but (hopefully) will be
* sufficient for libxml2.
*/
#ifndef VA_COPY
#ifdef HAVE_VA_COPY
#define VA_COPY(dest, src) va_copy(dest, src)
#else
#ifdef HAVE___VA_COPY
#define VA_COPY(dest,src) __va_copy(dest, src)
#else
#define VA_COPY(dest,src) (dest) = (src)
#endif
#endif
#endif
/* #define DEBUG_CALLBACKS */
/* #define DEBUG_READER */
/**
* TODO:
*
* macro to flag unimplemented blocks
*/
#define TODO \
xmlGenericError(xmlGenericErrorContext, \
"Unimplemented block at %s:%d\n", \
__FILE__, __LINE__);
#ifdef DEBUG_READER
#define DUMP_READER xmlTextReaderDebug(reader);
#else
#define DUMP_READER
#endif
#define CHUNK_SIZE 512
/************************************************************************
* *
* The parser: maps the Text Reader API on top of the existing *
* parsing routines building a tree *
* *
************************************************************************/
#define XML_TEXTREADER_INPUT 1
#define XML_TEXTREADER_CTXT 2
typedef enum {
XML_TEXTREADER_NONE = -1,
XML_TEXTREADER_START= 0,
XML_TEXTREADER_ELEMENT= 1,
XML_TEXTREADER_END= 2,
XML_TEXTREADER_EMPTY= 3,
XML_TEXTREADER_BACKTRACK= 4,
XML_TEXTREADER_DONE= 5,
XML_TEXTREADER_ERROR= 6
} xmlTextReaderState;
typedef enum {
XML_TEXTREADER_NOT_VALIDATE = 0,
XML_TEXTREADER_VALIDATE_DTD = 1,
XML_TEXTREADER_VALIDATE_RNG = 2,
XML_TEXTREADER_VALIDATE_XSD = 4
} xmlTextReaderValidate;
struct _xmlTextReader {
int mode; /* the parsing mode */
xmlDocPtr doc; /* when walking an existing doc */
xmlTextReaderValidate validate;/* is there any validation */
int allocs; /* what structure were deallocated */
xmlTextReaderState state;
xmlParserCtxtPtr ctxt; /* the parser context */
xmlSAXHandlerPtr sax; /* the parser SAX callbacks */
xmlParserInputBufferPtr input; /* the input */
startElementSAXFunc startElement;/* initial SAX callbacks */
endElementSAXFunc endElement; /* idem */
startElementNsSAX2Func startElementNs;/* idem */
endElementNsSAX2Func endElementNs; /* idem */
charactersSAXFunc characters;
cdataBlockSAXFunc cdataBlock;
unsigned int base; /* base of the segment in the input */
unsigned int cur; /* current position in the input */
xmlNodePtr node; /* current node */
xmlNodePtr curnode;/* current attribute node */
int depth; /* depth of the current node */
xmlNodePtr faketext;/* fake xmlNs chld */
int preserve;/* preserve the resulting document */
xmlBufPtr buffer; /* used to return const xmlChar * */
xmlDictPtr dict; /* the context dictionnary */
/* entity stack when traversing entities content */
xmlNodePtr ent; /* Current Entity Ref Node */
int entNr; /* Depth of the entities stack */
int entMax; /* Max depth of the entities stack */
xmlNodePtr *entTab; /* array of entities */
/* error handling */
xmlTextReaderErrorFunc errorFunc; /* callback function */
void *errorFuncArg; /* callback function user argument */
#ifdef LIBXML_SCHEMAS_ENABLED
/* Handling of RelaxNG validation */
xmlRelaxNGPtr rngSchemas; /* The Relax NG schemas */
xmlRelaxNGValidCtxtPtr rngValidCtxt;/* The Relax NG validation context */
int rngPreserveCtxt; /* 1 if the context was provided by the user */
int rngValidErrors;/* The number of errors detected */
xmlNodePtr rngFullNode; /* the node if RNG not progressive */
/* Handling of Schemas validation */
xmlSchemaPtr xsdSchemas; /* The Schemas schemas */
xmlSchemaValidCtxtPtr xsdValidCtxt;/* The Schemas validation context */
int xsdPreserveCtxt; /* 1 if the context was provided by the user */
int xsdValidErrors;/* The number of errors detected */
xmlSchemaSAXPlugPtr xsdPlug; /* the schemas plug in SAX pipeline */
#endif
#ifdef LIBXML_XINCLUDE_ENABLED
/* Handling of XInclude processing */
int xinclude; /* is xinclude asked for */
const xmlChar * xinclude_name; /* the xinclude name from dict */
xmlXIncludeCtxtPtr xincctxt; /* the xinclude context */
int in_xinclude; /* counts for xinclude */
#endif
#ifdef LIBXML_PATTERN_ENABLED
int patternNr; /* number of preserve patterns */
int patternMax; /* max preserve patterns */
xmlPatternPtr *patternTab; /* array of preserve patterns */
#endif
int preserves; /* level of preserves */
int parserFlags; /* the set of options set */
/* Structured error handling */
xmlStructuredErrorFunc sErrorFunc; /* callback function */
};
#define NODE_IS_EMPTY 0x1
#define NODE_IS_PRESERVED 0x2
#define NODE_IS_SPRESERVED 0x4
/**
* CONSTSTR:
*
* Macro used to return an interned string
*/
#define CONSTSTR(str) xmlDictLookup(reader->dict, (str), -1)
#define CONSTQSTR(p, str) xmlDictQLookup(reader->dict, (p), (str))
static int xmlTextReaderReadTree(xmlTextReaderPtr reader);
static int xmlTextReaderNextTree(xmlTextReaderPtr reader);
/************************************************************************
* *
* Our own version of the freeing routines as we recycle nodes *
* *
************************************************************************/
/**
* DICT_FREE:
* @str: a string
*
* Free a string if it is not owned by the "dict" dictionnary in the
* current scope
*/
#define DICT_FREE(str) \
if ((str) && ((!dict) || \
(xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \
xmlFree((char *)(str));
static void xmlTextReaderFreeNode(xmlTextReaderPtr reader, xmlNodePtr cur);
static void xmlTextReaderFreeNodeList(xmlTextReaderPtr reader, xmlNodePtr cur);
/**
* xmlFreeID:
* @not: A id
*
* Deallocate the memory used by an id definition
*/
static void
xmlFreeID(xmlIDPtr id) {
xmlDictPtr dict = NULL;
if (id == NULL) return;
if (id->doc != NULL)
dict = id->doc->dict;
if (id->value != NULL)
DICT_FREE(id->value)
xmlFree(id);
}
/**
* xmlTextReaderRemoveID:
* @doc: the document
* @attr: the attribute
*
* Remove the given attribute from the ID table maintained internally.
*
* Returns -1 if the lookup failed and 0 otherwise
*/
static int
xmlTextReaderRemoveID(xmlDocPtr doc, xmlAttrPtr attr) {
xmlIDTablePtr table;
xmlIDPtr id;
xmlChar *ID;
if (doc == NULL) return(-1);
if (attr == NULL) return(-1);
table = (xmlIDTablePtr) doc->ids;
if (table == NULL)
return(-1);
ID = xmlNodeListGetString(doc, attr->children, 1);
if (ID == NULL)
return(-1);
id = xmlHashLookup(table, ID);
xmlFree(ID);
if (id == NULL || id->attr != attr) {
return(-1);
}
id->name = attr->name;
id->attr = NULL;
return(0);
}
/**
* xmlTextReaderFreeProp:
* @reader: the xmlTextReaderPtr used
* @cur: the node
*
* Free a node.
*/
static void
xmlTextReaderFreeProp(xmlTextReaderPtr reader, xmlAttrPtr cur) {
xmlDictPtr dict;
dict = reader->ctxt->dict;
if (cur == NULL) return;
if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
xmlDeregisterNodeDefaultValue((xmlNodePtr) cur);
/* Check for ID removal -> leading to invalid references ! */
if ((cur->parent != NULL) && (cur->parent->doc != NULL) &&
((cur->parent->doc->intSubset != NULL) ||
(cur->parent->doc->extSubset != NULL))) {
if (xmlIsID(cur->parent->doc, cur->parent, cur))
xmlTextReaderRemoveID(cur->parent->doc, cur);
}
if (cur->children != NULL)
xmlTextReaderFreeNodeList(reader, cur->children);
DICT_FREE(cur->name);
if ((reader != NULL) && (reader->ctxt != NULL) &&
(reader->ctxt->freeAttrsNr < 100)) {
cur->next = reader->ctxt->freeAttrs;
reader->ctxt->freeAttrs = cur;
reader->ctxt->freeAttrsNr++;
} else {
xmlFree(cur);
}
}
/**
* xmlTextReaderFreePropList:
* @reader: the xmlTextReaderPtr used
* @cur: the first property in the list
*
* Free a property and all its siblings, all the children are freed too.
*/
static void
xmlTextReaderFreePropList(xmlTextReaderPtr reader, xmlAttrPtr cur) {
xmlAttrPtr next;
if (cur == NULL) return;
while (cur != NULL) {
next = cur->next;
xmlTextReaderFreeProp(reader, cur);
cur = next;
}
}
/**
* xmlTextReaderFreeNodeList:
* @reader: the xmlTextReaderPtr used
* @cur: the first node in the list
*
* Free a node and all its siblings, this is a recursive behaviour, all
* the children are freed too.
*/
static void
xmlTextReaderFreeNodeList(xmlTextReaderPtr reader, xmlNodePtr cur) {
xmlNodePtr next;
xmlDictPtr dict;
dict = reader->ctxt->dict;
if (cur == NULL) return;
if (cur->type == XML_NAMESPACE_DECL) {
xmlFreeNsList((xmlNsPtr) cur);
return;
}
if ((cur->type == XML_DOCUMENT_NODE) ||
(cur->type == XML_HTML_DOCUMENT_NODE)) {
xmlFreeDoc((xmlDocPtr) cur);
return;
}
while (cur != NULL) {
next = cur->next;
/* unroll to speed up freeing the document */
if (cur->type != XML_DTD_NODE) {
if ((cur->children != NULL) &&
(cur->type != XML_ENTITY_REF_NODE)) {
if (cur->children->parent == cur)
xmlTextReaderFreeNodeList(reader, cur->children);
cur->children = NULL;
}
if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
xmlDeregisterNodeDefaultValue(cur);
if (((cur->type == XML_ELEMENT_NODE) ||
(cur->type == XML_XINCLUDE_START) ||
(cur->type == XML_XINCLUDE_END)) &&
(cur->properties != NULL))
xmlTextReaderFreePropList(reader, cur->properties);
if ((cur->content != (xmlChar *) &(cur->properties)) &&
(cur->type != XML_ELEMENT_NODE) &&
(cur->type != XML_XINCLUDE_START) &&
(cur->type != XML_XINCLUDE_END) &&
(cur->type != XML_ENTITY_REF_NODE)) {
DICT_FREE(cur->content);
}
if (((cur->type == XML_ELEMENT_NODE) ||
(cur->type == XML_XINCLUDE_START) ||
(cur->type == XML_XINCLUDE_END)) &&
(cur->nsDef != NULL))
xmlFreeNsList(cur->nsDef);
/*
* we don't free element names here they are interned now
*/
if ((cur->type != XML_TEXT_NODE) &&
(cur->type != XML_COMMENT_NODE))
DICT_FREE(cur->name);
if (((cur->type == XML_ELEMENT_NODE) ||
(cur->type == XML_TEXT_NODE)) &&
(reader != NULL) && (reader->ctxt != NULL) &&
(reader->ctxt->freeElemsNr < 100)) {
cur->next = reader->ctxt->freeElems;
reader->ctxt->freeElems = cur;
reader->ctxt->freeElemsNr++;
} else {
xmlFree(cur);
}
}
cur = next;
}
}
/**
* xmlTextReaderFreeNode:
* @reader: the xmlTextReaderPtr used
* @cur: the node
*
* Free a node, this is a recursive behaviour, all the children are freed too.
* This doesn't unlink the child from the list, use xmlUnlinkNode() first.
*/
static void
xmlTextReaderFreeNode(xmlTextReaderPtr reader, xmlNodePtr cur) {
xmlDictPtr dict;
dict = reader->ctxt->dict;
if (cur->type == XML_DTD_NODE) {
xmlFreeDtd((xmlDtdPtr) cur);
return;
}
if (cur->type == XML_NAMESPACE_DECL) {
xmlFreeNs((xmlNsPtr) cur);
return;
}
if (cur->type == XML_ATTRIBUTE_NODE) {
xmlTextReaderFreeProp(reader, (xmlAttrPtr) cur);
return;
}
if ((cur->children != NULL) &&
(cur->type != XML_ENTITY_REF_NODE)) {
if (cur->children->parent == cur)
xmlTextReaderFreeNodeList(reader, cur->children);
cur->children = NULL;
}
if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
xmlDeregisterNodeDefaultValue(cur);
if (((cur->type == XML_ELEMENT_NODE) ||
(cur->type == XML_XINCLUDE_START) ||
(cur->type == XML_XINCLUDE_END)) &&
(cur->properties != NULL))
xmlTextReaderFreePropList(reader, cur->properties);
if ((cur->content != (xmlChar *) &(cur->properties)) &&
(cur->type != XML_ELEMENT_NODE) &&
(cur->type != XML_XINCLUDE_START) &&
(cur->type != XML_XINCLUDE_END) &&
(cur->type != XML_ENTITY_REF_NODE)) {
DICT_FREE(cur->content);
}
if (((cur->type == XML_ELEMENT_NODE) ||
(cur->type == XML_XINCLUDE_START) ||
(cur->type == XML_XINCLUDE_END)) &&
(cur->nsDef != NULL))
xmlFreeNsList(cur->nsDef);
/*
* we don't free names here they are interned now
*/
if ((cur->type != XML_TEXT_NODE) &&
(cur->type != XML_COMMENT_NODE))
DICT_FREE(cur->name);
if (((cur->type == XML_ELEMENT_NODE) ||
(cur->type == XML_TEXT_NODE)) &&
(reader != NULL) && (reader->ctxt != NULL) &&
(reader->ctxt->freeElemsNr < 100)) {
cur->next = reader->ctxt->freeElems;
reader->ctxt->freeElems = cur;
reader->ctxt->freeElemsNr++;
} else {
xmlFree(cur);
}
}
/**
* xmlTextReaderFreeIDTable:
* @table: An id table
*
* Deallocate the memory used by an ID hash table.
*/
static void
xmlTextReaderFreeIDTable(xmlIDTablePtr table) {
xmlHashFree(table, (xmlHashDeallocator) xmlFreeID);
}
/**
* xmlTextReaderFreeDoc:
* @reader: the xmlTextReaderPtr used
* @cur: pointer to the document
*
* Free up all the structures used by a document, tree included.
*/
static void
xmlTextReaderFreeDoc(xmlTextReaderPtr reader, xmlDocPtr cur) {
xmlDtdPtr extSubset, intSubset;
if (cur == NULL) return;
if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
xmlDeregisterNodeDefaultValue((xmlNodePtr) cur);
/*
* Do this before freeing the children list to avoid ID lookups
*/
if (cur->ids != NULL) xmlTextReaderFreeIDTable((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) {
xmlUnlinkNode((xmlNodePtr) cur->extSubset);
cur->extSubset = NULL;
xmlFreeDtd(extSubset);
}
if (intSubset != NULL) {
xmlUnlinkNode((xmlNodePtr) cur->intSubset);
cur->intSubset = NULL;
xmlFreeDtd(intSubset);
}
if (cur->children != NULL) xmlTextReaderFreeNodeList(reader, cur->children);
if (cur->version != NULL) xmlFree((char *) cur->version);
if (cur->name != NULL) xmlFree((char *) cur->name);
if (cur->encoding != NULL) xmlFree((char *) cur->encoding);
if (cur->oldNs != NULL) xmlFreeNsList(cur->oldNs);
if (cur->URL != NULL) xmlFree((char *) cur->URL);
if (cur->dict != NULL) xmlDictFree(cur->dict);
xmlFree(cur);
}
/************************************************************************
* *
* The reader core parser *
* *
************************************************************************/
#ifdef DEBUG_READER
static void
xmlTextReaderDebug(xmlTextReaderPtr reader) {
if ((reader == NULL) || (reader->ctxt == NULL)) {
fprintf(stderr, "xmlTextReader NULL\n");
return;
}
fprintf(stderr, "xmlTextReader: state %d depth %d ",
reader->state, reader->depth);
if (reader->node == NULL) {
fprintf(stderr, "node = NULL\n");
} else {
fprintf(stderr, "node %s\n", reader->node->name);
}
fprintf(stderr, " input: base %d, cur %d, depth %d: ",
reader->base, reader->cur, reader->ctxt->nodeNr);
if (reader->input->buffer == NULL) {
fprintf(stderr, "buffer is NULL\n");
} else {
#ifdef LIBXML_DEBUG_ENABLED
xmlDebugDumpString(stderr,
&reader->input->buffer->content[reader->cur]);
#endif
fprintf(stderr, "\n");
}
}
#endif
/**
* xmlTextReaderEntPush:
* @reader: the xmlTextReaderPtr used
* @value: the entity reference node
*
* Pushes a new entity reference node on top of the entities stack
*
* Returns 0 in case of error, the index in the stack otherwise
*/
static int
xmlTextReaderEntPush(xmlTextReaderPtr reader, xmlNodePtr value)
{
if (reader->entMax <= 0) {
reader->entMax = 10;
reader->entTab = (xmlNodePtr *) xmlMalloc(reader->entMax *
sizeof(reader->entTab[0]));
if (reader->entTab == NULL) {
xmlGenericError(xmlGenericErrorContext, "xmlMalloc failed !\n");
return (0);
}
}
if (reader->entNr >= reader->entMax) {
reader->entMax *= 2;
reader->entTab =
(xmlNodePtr *) xmlRealloc(reader->entTab,
reader->entMax *
sizeof(reader->entTab[0]));
if (reader->entTab == NULL) {
xmlGenericError(xmlGenericErrorContext, "xmlRealloc failed !\n");
return (0);
}
}
reader->entTab[reader->entNr] = value;
reader->ent = value;
return (reader->entNr++);
}
/**
* xmlTextReaderEntPop:
* @reader: the xmlTextReaderPtr used
*
* Pops the top element entity from the entities stack
*
* Returns the entity just removed
*/
static xmlNodePtr
xmlTextReaderEntPop(xmlTextReaderPtr reader)
{
xmlNodePtr ret;
if (reader->entNr <= 0)
return (NULL);
reader->entNr--;
if (reader->entNr > 0)
reader->ent = reader->entTab[reader->entNr - 1];
else
reader->ent = NULL;
ret = reader->entTab[reader->entNr];
reader->entTab[reader->entNr] = NULL;
return (ret);
}
/**
* xmlTextReaderStartElement:
* @ctx: the user data (XML parser context)
* @fullname: The element name, including namespace prefix
* @atts: An array of name/value attributes pairs, NULL terminated
*
* called when an opening tag has been processed.
*/
static void
xmlTextReaderStartElement(void *ctx, const xmlChar *fullname,
const xmlChar **atts) {
xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
xmlTextReaderPtr reader = ctxt->_private;
#ifdef DEBUG_CALLBACKS
printf("xmlTextReaderStartElement(%s)\n", fullname);
#endif
if ((reader != NULL) && (reader->startElement != NULL)) {
reader->startElement(ctx, fullname, atts);
if ((ctxt->node != NULL) && (ctxt->input != NULL) &&
(ctxt->input->cur != NULL) && (ctxt->input->cur[0] == '/') &&
(ctxt->input->cur[1] == '>'))
ctxt->node->extra = NODE_IS_EMPTY;
}
if (reader != NULL)
reader->state = XML_TEXTREADER_ELEMENT;
}
/**
* xmlTextReaderEndElement:
* @ctx: the user data (XML parser context)
* @fullname: The element name, including namespace prefix
*
* called when an ending tag has been processed.
*/
static void
xmlTextReaderEndElement(void *ctx, const xmlChar *fullname) {
xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
xmlTextReaderPtr reader = ctxt->_private;
#ifdef DEBUG_CALLBACKS
printf("xmlTextReaderEndElement(%s)\n", fullname);
#endif
if ((reader != NULL) && (reader->endElement != NULL)) {
reader->endElement(ctx, fullname);
}
}
/**
* xmlTextReaderStartElementNs:
* @ctx: the user data (XML parser context)
* @localname: the local name of the element
* @prefix: the element namespace prefix if available
* @URI: the element namespace name if available
* @nb_namespaces: number of namespace definitions on that node
* @namespaces: pointer to the array of prefix/URI pairs namespace definitions
* @nb_attributes: the number of attributes on that node
* nb_defaulted: the number of defaulted attributes.
* @attributes: pointer to the array of (localname/prefix/URI/value/end)
* attribute values.
*
* called when an opening tag has been processed.
*/
static void
xmlTextReaderStartElementNs(void *ctx,
const xmlChar *localname,
const xmlChar *prefix,
const xmlChar *URI,
int nb_namespaces,
const xmlChar **namespaces,
int nb_attributes,
int nb_defaulted,
const xmlChar **attributes)
{
xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
xmlTextReaderPtr reader = ctxt->_private;
#ifdef DEBUG_CALLBACKS
printf("xmlTextReaderStartElementNs(%s)\n", localname);
#endif
if ((reader != NULL) && (reader->startElementNs != NULL)) {
reader->startElementNs(ctx, localname, prefix, URI, nb_namespaces,
namespaces, nb_attributes, nb_defaulted,
attributes);
if ((ctxt->node != NULL) && (ctxt->input != NULL) &&
(ctxt->input->cur != NULL) && (ctxt->input->cur[0] == '/') &&
(ctxt->input->cur[1] == '>'))
ctxt->node->extra = NODE_IS_EMPTY;
}
if (reader != NULL)
reader->state = XML_TEXTREADER_ELEMENT;
}
/**
* xmlTextReaderEndElementNs:
* @ctx: the user data (XML parser context)
* @localname: the local name of the element
* @prefix: the element namespace prefix if available
* @URI: the element namespace name if available
*
* called when an ending tag has been processed.
*/
static void
xmlTextReaderEndElementNs(void *ctx,
const xmlChar * localname,
const xmlChar * prefix,
const xmlChar * URI)
{
xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
xmlTextReaderPtr reader = ctxt->_private;
#ifdef DEBUG_CALLBACKS
printf("xmlTextReaderEndElementNs(%s)\n", localname);
#endif
if ((reader != NULL) && (reader->endElementNs != NULL)) {
reader->endElementNs(ctx, localname, prefix, URI);
}
}
/**
* xmlTextReaderCharacters:
* @ctx: the user data (XML parser context)
* @ch: a xmlChar string
* @len: the number of xmlChar
*
* receiving some chars from the parser.
*/
static void
xmlTextReaderCharacters(void *ctx, const xmlChar *ch, int len)
{
xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
xmlTextReaderPtr reader = ctxt->_private;
#ifdef DEBUG_CALLBACKS
printf("xmlTextReaderCharacters()\n");
#endif
if ((reader != NULL) && (reader->characters != NULL)) {
reader->characters(ctx, ch, len);
}
}
/**
* xmlTextReaderCDataBlock:
* @ctx: the user data (XML parser context)
* @value: The pcdata content
* @len: the block length
*
* called when a pcdata block has been parsed
*/
static void
xmlTextReaderCDataBlock(void *ctx, const xmlChar *ch, int len)
{
xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
xmlTextReaderPtr reader = ctxt->_private;
#ifdef DEBUG_CALLBACKS
printf("xmlTextReaderCDataBlock()\n");
#endif
if ((reader != NULL) && (reader->cdataBlock != NULL)) {
reader->cdataBlock(ctx, ch, len);
}
}
/**
* xmlTextReaderPushData:
* @reader: the xmlTextReaderPtr used
*
* Push data down the progressive parser until a significant callback
* got raised.
*
* Returns -1 in case of failure, 0 otherwise
*/
static int
xmlTextReaderPushData(xmlTextReaderPtr reader) {
xmlBufPtr inbuf;
int val, s;
xmlTextReaderState oldstate;
int alloc;
if ((reader->input == NULL) || (reader->input->buffer == NULL))
return(-1);
oldstate = reader->state;
reader->state = XML_TEXTREADER_NONE;
inbuf = reader->input->buffer;
alloc = xmlBufGetAllocationScheme(inbuf);
while (reader->state == XML_TEXTREADER_NONE) {
if (xmlBufUse(inbuf) < reader->cur + CHUNK_SIZE) {
/*
* Refill the buffer unless we are at the end of the stream
*/
if (reader->mode != XML_TEXTREADER_MODE_EOF) {
val = xmlParserInputBufferRead(reader->input, 4096);
if ((val == 0) &&
(alloc == XML_BUFFER_ALLOC_IMMUTABLE)) {
if (xmlBufUse(inbuf) == reader->cur) {
reader->mode = XML_TEXTREADER_MODE_EOF;
reader->state = oldstate;
}
} else if (val < 0) {
reader->mode = XML_TEXTREADER_MODE_EOF;
reader->state = oldstate;
if ((oldstate != XML_TEXTREADER_START) ||
(reader->ctxt->myDoc != NULL))
return(val);
} else if (val == 0) {
/* mark the end of the stream and process the remains */
reader->mode = XML_TEXTREADER_MODE_EOF;
break;
}
} else
break;
}
/*
* parse by block of CHUNK_SIZE bytes, various tests show that
* it's the best tradeoff at least on a 1.2GH Duron
*/
if (xmlBufUse(inbuf) >= reader->cur + CHUNK_SIZE) {
val = xmlParseChunk(reader->ctxt,
(const char *) xmlBufContent(inbuf) + reader->cur,
CHUNK_SIZE, 0);
reader->cur += CHUNK_SIZE;
if (val != 0)
reader->ctxt->wellFormed = 0;
if (reader->ctxt->wellFormed == 0)
break;
} else {
s = xmlBufUse(inbuf) - reader->cur;
val = xmlParseChunk(reader->ctxt,
(const char *) xmlBufContent(inbuf) + reader->cur,
s, 0);
reader->cur += s;
if (val != 0)
reader->ctxt->wellFormed = 0;
break;
}
}
/*
* Discard the consumed input when needed and possible
*/
if (reader->mode == XML_TEXTREADER_MODE_INTERACTIVE) {
if (alloc != XML_BUFFER_ALLOC_IMMUTABLE) {
if ((reader->cur >= 4096) &&
(xmlBufUse(inbuf) - reader->cur <= CHUNK_SIZE)) {
val = xmlBufShrink(inbuf, reader->cur);
if (val >= 0) {
reader->cur -= val;
}
}
}
}
/*
* At the end of the stream signal that the work is done to the Push
* parser.
*/
else if (reader->mode == XML_TEXTREADER_MODE_EOF) {
if (reader->state != XML_TEXTREADER_DONE) {
s = xmlBufUse(inbuf) - reader->cur;
val = xmlParseChunk(reader->ctxt,
(const char *) xmlBufContent(inbuf) + reader->cur,
s, 1);
reader->cur = xmlBufUse(inbuf);
reader->state = XML_TEXTREADER_DONE;
if (val != 0) {
if (reader->ctxt->wellFormed)
reader->ctxt->wellFormed = 0;
else
return(-1);
}
}
}
reader->state = oldstate;
if (reader->ctxt->wellFormed == 0) {
reader->mode = XML_TEXTREADER_MODE_EOF;
return(-1);
}
return(0);
}
#ifdef LIBXML_REGEXP_ENABLED
/**
* xmlTextReaderValidatePush:
* @reader: the xmlTextReaderPtr used
*
* Push the current node for validation
*/
static void
xmlTextReaderValidatePush(xmlTextReaderPtr reader ATTRIBUTE_UNUSED) {
xmlNodePtr node = reader->node;
#ifdef LIBXML_VALID_ENABLED
if ((reader->validate == XML_TEXTREADER_VALIDATE_DTD) &&
(reader->ctxt != NULL) && (reader->ctxt->validate == 1)) {
if ((node->ns == NULL) || (node->ns->prefix == NULL)) {
reader->ctxt->valid &= xmlValidatePushElement(&reader->ctxt->vctxt,
reader->ctxt->myDoc, node, node->name);
} else {
/* TODO use the BuildQName interface */
xmlChar *qname;
qname = xmlStrdup(node->ns->prefix);
qname = xmlStrcat(qname, BAD_CAST ":");
qname = xmlStrcat(qname, node->name);
reader->ctxt->valid &= xmlValidatePushElement(&reader->ctxt->vctxt,
reader->ctxt->myDoc, node, qname);
if (qname != NULL)
xmlFree(qname);
}
}
#endif /* LIBXML_VALID_ENABLED */
#ifdef LIBXML_SCHEMAS_ENABLED
if ((reader->validate == XML_TEXTREADER_VALIDATE_RNG) &&
(reader->rngValidCtxt != NULL)) {
int ret;
if (reader->rngFullNode != NULL) return;
ret = xmlRelaxNGValidatePushElement(reader->rngValidCtxt,
reader->ctxt->myDoc,
node);
if (ret == 0) {
/*
* this element requires a full tree
*/
node = xmlTextReaderExpand(reader);
if (node == NULL) {
printf("Expand failed !\n");
ret = -1;
} else {
ret = xmlRelaxNGValidateFullElement(reader->rngValidCtxt,
reader->ctxt->myDoc,
node);
reader->rngFullNode = node;
}
}
if (ret != 1)
reader->rngValidErrors++;
}
#endif
}
/**
* xmlTextReaderValidateCData:
* @reader: the xmlTextReaderPtr used
* @data: pointer to the CData
* @len: lenght of the CData block in bytes.
*
* Push some CData for validation
*/
static void
xmlTextReaderValidateCData(xmlTextReaderPtr reader,
const xmlChar *data, int len) {
#ifdef LIBXML_VALID_ENABLED
if ((reader->validate == XML_TEXTREADER_VALIDATE_DTD) &&
(reader->ctxt != NULL) && (reader->ctxt->validate == 1)) {
reader->ctxt->valid &= xmlValidatePushCData(&reader->ctxt->vctxt,
data, len);
}
#endif /* LIBXML_VALID_ENABLED */