-
Notifications
You must be signed in to change notification settings - Fork 366
/
SAX2.c
2827 lines (2583 loc) · 76.8 KB
/
SAX2.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
/*
* SAX2.c : Default SAX2 handler to build a tree.
*
* See Copyright for the status of this software.
*
* Daniel Veillard <[email protected]>
*/
#define IN_LIBXML
#include "libxml.h"
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <stddef.h>
#include <libxml/SAX2.h>
#include <libxml/xmlmemory.h>
#include <libxml/tree.h>
#include <libxml/parser.h>
#include <libxml/parserInternals.h>
#include <libxml/valid.h>
#include <libxml/entities.h>
#include <libxml/xmlerror.h>
#include <libxml/debugXML.h>
#include <libxml/xmlIO.h>
#include <libxml/uri.h>
#include <libxml/valid.h>
#include <libxml/HTMLtree.h>
#include "private/error.h"
#include "private/parser.h"
#include "private/tree.h"
/*
* xmlSAX2ErrMemory:
* @ctxt: an XML validation parser context
* @msg: a string to accompany the error message
*/
static void
xmlSAX2ErrMemory(xmlParserCtxtPtr ctxt) {
xmlCtxtErrMemory(ctxt);
}
/**
* xmlValidError:
* @ctxt: an XML validation parser context
* @error: the error number
* @msg: the error message
* @str1: extra data
* @str2: extra data
*
* Handle a validation error
*/
static void LIBXML_ATTR_FORMAT(3,0)
xmlErrValid(xmlParserCtxtPtr ctxt, xmlParserErrors error,
const char *msg, const xmlChar *str1, const xmlChar *str2)
{
xmlCtxtErr(ctxt, NULL, XML_FROM_DTD, error, XML_ERR_ERROR,
str1, str2, NULL, 0, msg, str1, str2);
if (ctxt != NULL)
ctxt->valid = 0;
}
/**
* xmlFatalErrMsg:
* @ctxt: an XML parser context
* @error: the error number
* @msg: the error message
* @str1: an error string
* @str2: an error string
*
* Handle a fatal parser error, i.e. violating Well-Formedness constraints
*/
static void LIBXML_ATTR_FORMAT(3,0)
xmlFatalErrMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error,
const char *msg, const xmlChar *str1, const xmlChar *str2)
{
xmlCtxtErr(ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_FATAL,
str1, str2, NULL, 0, msg, str1, str2);
}
/**
* xmlWarnMsg:
* @ctxt: an XML parser context
* @error: the error number
* @msg: the error message
* @str1: an error string
* @str2: an error string
*
* Handle a parser warning
*/
static void LIBXML_ATTR_FORMAT(3,0)
xmlWarnMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error,
const char *msg, const xmlChar *str1)
{
xmlCtxtErr(ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_WARNING,
str1, NULL, NULL, 0, msg, str1);
}
/**
* xmlNsWarnMsg:
* @ctxt: an XML parser context
* @error: the error number
* @msg: the error message
* @str1: an error string
*
* Handle a namespace warning
*/
static void LIBXML_ATTR_FORMAT(3,0)
xmlNsWarnMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error,
const char *msg, const xmlChar *str1, const xmlChar *str2)
{
xmlCtxtErr(ctxt, NULL, XML_FROM_NAMESPACE, error, XML_ERR_WARNING,
str1, str2, NULL, 0, msg, str1, str2);
}
/**
* xmlSAX2GetPublicId:
* @ctx: the user data (XML parser context)
*
* Provides the public ID e.g. "-//SGMLSOURCE//DTD DEMO//EN"
*
* Returns a xmlChar *
*/
const xmlChar *
xmlSAX2GetPublicId(void *ctx ATTRIBUTE_UNUSED)
{
/* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
return(NULL);
}
/**
* xmlSAX2GetSystemId:
* @ctx: the user data (XML parser context)
*
* Provides the system ID, basically URL or filename e.g.
* http://www.sgmlsource.com/dtds/memo.dtd
*
* Returns a xmlChar *
*/
const xmlChar *
xmlSAX2GetSystemId(void *ctx)
{
xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
if ((ctx == NULL) || (ctxt->input == NULL)) return(NULL);
return((const xmlChar *) ctxt->input->filename);
}
/**
* xmlSAX2GetLineNumber:
* @ctx: the user data (XML parser context)
*
* Provide the line number of the current parsing point.
*
* Returns an int
*/
int
xmlSAX2GetLineNumber(void *ctx)
{
xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
if ((ctx == NULL) || (ctxt->input == NULL)) return(0);
return(ctxt->input->line);
}
/**
* xmlSAX2GetColumnNumber:
* @ctx: the user data (XML parser context)
*
* Provide the column number of the current parsing point.
*
* Returns an int
*/
int
xmlSAX2GetColumnNumber(void *ctx)
{
xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
if ((ctx == NULL) || (ctxt->input == NULL)) return(0);
return(ctxt->input->col);
}
/**
* xmlSAX2IsStandalone:
* @ctx: the user data (XML parser context)
*
* Is this document tagged standalone ?
*
* Returns 1 if true
*/
int
xmlSAX2IsStandalone(void *ctx)
{
xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
if ((ctx == NULL) || (ctxt->myDoc == NULL)) return(0);
return(ctxt->myDoc->standalone == 1);
}
/**
* xmlSAX2HasInternalSubset:
* @ctx: the user data (XML parser context)
*
* Does this document has an internal subset
*
* Returns 1 if true
*/
int
xmlSAX2HasInternalSubset(void *ctx)
{
xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
if ((ctxt == NULL) || (ctxt->myDoc == NULL)) return(0);
return(ctxt->myDoc->intSubset != NULL);
}
/**
* xmlSAX2HasExternalSubset:
* @ctx: the user data (XML parser context)
*
* Does this document has an external subset
*
* Returns 1 if true
*/
int
xmlSAX2HasExternalSubset(void *ctx)
{
xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
if ((ctxt == NULL) || (ctxt->myDoc == NULL)) return(0);
return(ctxt->myDoc->extSubset != NULL);
}
/**
* xmlSAX2InternalSubset:
* @ctx: the user data (XML parser context)
* @name: the root element name
* @ExternalID: the external ID
* @SystemID: the SYSTEM ID (e.g. filename or URL)
*
* Callback on internal subset declaration.
*/
void
xmlSAX2InternalSubset(void *ctx, const xmlChar *name,
const xmlChar *ExternalID, const xmlChar *SystemID)
{
xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
xmlDtdPtr dtd;
if (ctx == NULL) return;
if (ctxt->myDoc == NULL)
return;
if ((ctxt->html) && (ctxt->instate != XML_PARSER_MISC))
return;
dtd = xmlGetIntSubset(ctxt->myDoc);
if (dtd != NULL) {
xmlUnlinkNode((xmlNodePtr) dtd);
xmlFreeDtd(dtd);
ctxt->myDoc->intSubset = NULL;
}
ctxt->myDoc->intSubset =
xmlCreateIntSubset(ctxt->myDoc, name, ExternalID, SystemID);
if (ctxt->myDoc->intSubset == NULL)
xmlSAX2ErrMemory(ctxt);
}
/**
* xmlSAX2ExternalSubset:
* @ctx: the user data (XML parser context)
* @name: the root element name
* @ExternalID: the external ID
* @SystemID: the SYSTEM ID (e.g. filename or URL)
*
* Callback on external subset declaration.
*/
void
xmlSAX2ExternalSubset(void *ctx, const xmlChar *name,
const xmlChar *ExternalID, const xmlChar *SystemID)
{
xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
if (ctx == NULL) return;
if ((SystemID != NULL) &&
((ctxt->options & XML_PARSE_NO_XXE) == 0) &&
(((ctxt->validate) || (ctxt->loadsubset)) &&
(ctxt->wellFormed && ctxt->myDoc))) {
/*
* Try to fetch and parse the external subset.
*/
xmlParserInputPtr oldinput;
int oldinputNr;
int oldinputMax;
xmlParserInputPtr *oldinputTab;
xmlParserInputPtr input = NULL;
const xmlChar *oldencoding;
unsigned long consumed;
size_t buffered;
/*
* Ask the Entity resolver to load the damn thing
*/
if ((ctxt->sax != NULL) && (ctxt->sax->resolveEntity != NULL))
input = ctxt->sax->resolveEntity(ctxt->userData, ExternalID,
SystemID);
if (input == NULL) {
return;
}
if (xmlNewDtd(ctxt->myDoc, name, ExternalID, SystemID) == NULL) {
xmlSAX2ErrMemory(ctxt);
xmlFreeInputStream(input);
return;
}
/*
* make sure we won't destroy the main document context
*/
oldinput = ctxt->input;
oldinputNr = ctxt->inputNr;
oldinputMax = ctxt->inputMax;
oldinputTab = ctxt->inputTab;
oldencoding = ctxt->encoding;
ctxt->encoding = NULL;
ctxt->inputTab = (xmlParserInputPtr *)
xmlMalloc(5 * sizeof(xmlParserInputPtr));
if (ctxt->inputTab == NULL) {
xmlSAX2ErrMemory(ctxt);
goto error;
}
ctxt->inputNr = 0;
ctxt->inputMax = 5;
ctxt->input = NULL;
if (xmlPushInput(ctxt, input) < 0)
goto error;
if (input->filename == NULL)
input->filename = (char *) xmlCanonicPath(SystemID);
input->line = 1;
input->col = 1;
input->base = ctxt->input->cur;
input->cur = ctxt->input->cur;
input->free = NULL;
/*
* let's parse that entity knowing it's an external subset.
*/
xmlParseExternalSubset(ctxt, ExternalID, SystemID);
/*
* Free up the external entities
*/
while (ctxt->inputNr > 1)
xmlPopInput(ctxt);
consumed = ctxt->input->consumed;
buffered = ctxt->input->cur - ctxt->input->base;
if (buffered > ULONG_MAX - consumed)
consumed = ULONG_MAX;
else
consumed += buffered;
if (consumed > ULONG_MAX - ctxt->sizeentities)
ctxt->sizeentities = ULONG_MAX;
else
ctxt->sizeentities += consumed;
error:
xmlFreeInputStream(input);
xmlFree(ctxt->inputTab);
/*
* Restore the parsing context of the main entity
*/
ctxt->input = oldinput;
ctxt->inputNr = oldinputNr;
ctxt->inputMax = oldinputMax;
ctxt->inputTab = oldinputTab;
if ((ctxt->encoding != NULL) &&
((ctxt->dict == NULL) ||
(!xmlDictOwns(ctxt->dict, ctxt->encoding))))
xmlFree((xmlChar *) ctxt->encoding);
ctxt->encoding = oldencoding;
/* ctxt->wellFormed = oldwellFormed; */
}
}
/**
* xmlSAX2ResolveEntity:
* @ctx: the user data (XML parser context)
* @publicId: The public ID of the entity
* @systemId: The system ID of the entity
*
* This is only used to load DTDs. The preferred way to install
* custom resolvers is xmlCtxtSetResourceLoader.
*
* Returns a parser input.
*/
xmlParserInputPtr
xmlSAX2ResolveEntity(void *ctx, const xmlChar *publicId,
const xmlChar *systemId)
{
xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
xmlParserInputPtr ret = NULL;
xmlChar *URI;
const xmlChar *base = NULL;
int res;
if (ctx == NULL) return(NULL);
if (ctxt->input != NULL)
base = BAD_CAST ctxt->input->filename;
/*
* We don't really need the 'directory' struct member, but some
* users set it manually to a base URI for memory streams.
*/
if (base == NULL)
base = BAD_CAST ctxt->directory;
if ((xmlStrlen(systemId) > XML_MAX_URI_LENGTH) ||
(xmlStrlen(base) > XML_MAX_URI_LENGTH)) {
xmlFatalErr(ctxt, XML_ERR_RESOURCE_LIMIT, "URI too long");
return(NULL);
}
res = xmlBuildURISafe(systemId, base, &URI);
if (URI == NULL) {
if (res < 0)
xmlSAX2ErrMemory(ctxt);
else
xmlWarnMsg(ctxt, XML_ERR_INVALID_URI,
"Can't resolve URI: %s\n", systemId);
return(NULL);
}
if (xmlStrlen(URI) > XML_MAX_URI_LENGTH) {
xmlFatalErr(ctxt, XML_ERR_RESOURCE_LIMIT, "URI too long");
} else {
ret = xmlLoadResource(ctxt, (const char *) URI,
(const char *) publicId, XML_RESOURCE_DTD);
}
xmlFree(URI);
return(ret);
}
/**
* xmlSAX2GetEntity:
* @ctx: the user data (XML parser context)
* @name: The entity name
*
* Get an entity by name
*
* Returns the xmlEntityPtr if found.
*/
xmlEntityPtr
xmlSAX2GetEntity(void *ctx, const xmlChar *name)
{
xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
xmlEntityPtr ret = NULL;
if (ctx == NULL) return(NULL);
if (ctxt->inSubset == 0) {
ret = xmlGetPredefinedEntity(name);
if (ret != NULL)
return(ret);
}
if ((ctxt->myDoc != NULL) && (ctxt->myDoc->standalone == 1)) {
if (ctxt->inSubset == 2) {
ctxt->myDoc->standalone = 0;
ret = xmlGetDocEntity(ctxt->myDoc, name);
ctxt->myDoc->standalone = 1;
} else {
ret = xmlGetDocEntity(ctxt->myDoc, name);
if (ret == NULL) {
ctxt->myDoc->standalone = 0;
ret = xmlGetDocEntity(ctxt->myDoc, name);
if (ret != NULL) {
xmlFatalErrMsg(ctxt, XML_ERR_NOT_STANDALONE,
"Entity(%s) document marked standalone but requires external subset\n",
name, NULL);
}
ctxt->myDoc->standalone = 1;
}
}
} else {
ret = xmlGetDocEntity(ctxt->myDoc, name);
}
return(ret);
}
/**
* xmlSAX2GetParameterEntity:
* @ctx: the user data (XML parser context)
* @name: The entity name
*
* Get a parameter entity by name
*
* Returns the xmlEntityPtr if found.
*/
xmlEntityPtr
xmlSAX2GetParameterEntity(void *ctx, const xmlChar *name)
{
xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
xmlEntityPtr ret;
if (ctx == NULL) return(NULL);
ret = xmlGetParameterEntity(ctxt->myDoc, name);
return(ret);
}
/**
* xmlSAX2EntityDecl:
* @ctx: the user data (XML parser context)
* @name: the entity name
* @type: the entity type
* @publicId: The public ID of the entity
* @systemId: The system ID of the entity
* @content: the entity value (without processing).
*
* An entity definition has been parsed
*/
void
xmlSAX2EntityDecl(void *ctx, const xmlChar *name, int type,
const xmlChar *publicId, const xmlChar *systemId, xmlChar *content)
{
xmlEntityPtr ent;
xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
int extSubset;
int res;
if ((ctxt == NULL) || (ctxt->myDoc == NULL))
return;
extSubset = ctxt->inSubset == 2;
res = xmlAddEntity(ctxt->myDoc, extSubset, name, type, publicId, systemId,
content, &ent);
switch (res) {
case XML_ERR_OK:
break;
case XML_ERR_NO_MEMORY:
xmlSAX2ErrMemory(ctxt);
return;
case XML_WAR_ENTITY_REDEFINED:
if (ctxt->pedantic) {
if (extSubset)
xmlWarnMsg(ctxt, res, "Entity(%s) already defined in the"
" external subset\n", name);
else
xmlWarnMsg(ctxt, res, "Entity(%s) already defined in the"
" internal subset\n", name);
}
return;
case XML_ERR_REDECL_PREDEF_ENTITY:
/*
* Technically an error but it's a common mistake to get double
* escaping according to "4.6 Predefined Entities" wrong.
*/
xmlWarnMsg(ctxt, res, "Invalid redeclaration of predefined"
" entity '%s'", name);
return;
default:
xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR,
"Unexpected error code from xmlAddEntity\n",
NULL, NULL);
return;
}
if ((ent->URI == NULL) && (systemId != NULL)) {
xmlChar *URI;
const char *base = NULL;
int i;
for (i = ctxt->inputNr - 1; i >= 0; i--) {
if (ctxt->inputTab[i]->filename != NULL) {
base = ctxt->inputTab[i]->filename;
break;
}
}
/*
* We don't really need the 'directory' struct member, but some
* users set it manually to a base URI for memory streams.
*/
if (base == NULL)
base = ctxt->directory;
res = xmlBuildURISafe(systemId, (const xmlChar *) base, &URI);
if (URI == NULL) {
if (res < 0) {
xmlSAX2ErrMemory(ctxt);
} else {
xmlWarnMsg(ctxt, XML_ERR_INVALID_URI,
"Can't resolve URI: %s\n", systemId);
}
} else if (xmlStrlen(URI) > XML_MAX_URI_LENGTH) {
xmlFatalErr(ctxt, XML_ERR_RESOURCE_LIMIT, "URI too long");
xmlFree(URI);
} else {
ent->URI = URI;
}
}
}
/**
* xmlSAX2AttributeDecl:
* @ctx: the user data (XML parser context)
* @elem: the name of the element
* @fullname: the attribute name
* @type: the attribute type
* @def: the type of default value
* @defaultValue: the attribute default value
* @tree: the tree of enumerated value set
*
* An attribute definition has been parsed
*/
void
xmlSAX2AttributeDecl(void *ctx, const xmlChar *elem, const xmlChar *fullname,
int type, int def, const xmlChar *defaultValue,
xmlEnumerationPtr tree)
{
xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
xmlAttributePtr attr;
const xmlChar *name = NULL;
xmlChar *prefix = NULL;
/* Avoid unused variable warning if features are disabled. */
(void) attr;
if ((ctxt == NULL) || (ctxt->myDoc == NULL))
return;
if ((xmlStrEqual(fullname, BAD_CAST "xml:id")) &&
(type != XML_ATTRIBUTE_ID)) {
/*
* Raise the error but keep the validity flag
*/
int tmp = ctxt->valid;
xmlErrValid(ctxt, XML_DTD_XMLID_TYPE,
"xml:id : attribute type should be ID\n", NULL, NULL);
ctxt->valid = tmp;
}
name = xmlSplitQName4(fullname, &prefix);
if (name == NULL)
xmlSAX2ErrMemory(ctxt);
ctxt->vctxt.valid = 1;
if (ctxt->inSubset == 1)
attr = xmlAddAttributeDecl(&ctxt->vctxt, ctxt->myDoc->intSubset, elem,
name, prefix, (xmlAttributeType) type,
(xmlAttributeDefault) def, defaultValue, tree);
else if (ctxt->inSubset == 2)
attr = xmlAddAttributeDecl(&ctxt->vctxt, ctxt->myDoc->extSubset, elem,
name, prefix, (xmlAttributeType) type,
(xmlAttributeDefault) def, defaultValue, tree);
else {
xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR,
"SAX.xmlSAX2AttributeDecl(%s) called while not in subset\n",
name, NULL);
xmlFree(prefix);
xmlFreeEnumeration(tree);
return;
}
#ifdef LIBXML_VALID_ENABLED
if (ctxt->vctxt.valid == 0)
ctxt->valid = 0;
if ((attr != NULL) && (ctxt->validate) && (ctxt->wellFormed) &&
(ctxt->myDoc->intSubset != NULL))
ctxt->valid &= xmlValidateAttributeDecl(&ctxt->vctxt, ctxt->myDoc,
attr);
#endif /* LIBXML_VALID_ENABLED */
if (prefix != NULL)
xmlFree(prefix);
}
/**
* xmlSAX2ElementDecl:
* @ctx: the user data (XML parser context)
* @name: the element name
* @type: the element type
* @content: the element value tree
*
* An element definition has been parsed
*/
void
xmlSAX2ElementDecl(void *ctx, const xmlChar * name, int type,
xmlElementContentPtr content)
{
xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
xmlElementPtr elem = NULL;
/* Avoid unused variable warning if features are disabled. */
(void) elem;
if ((ctxt == NULL) || (ctxt->myDoc == NULL))
return;
if (ctxt->inSubset == 1)
elem = xmlAddElementDecl(&ctxt->vctxt, ctxt->myDoc->intSubset,
name, (xmlElementTypeVal) type, content);
else if (ctxt->inSubset == 2)
elem = xmlAddElementDecl(&ctxt->vctxt, ctxt->myDoc->extSubset,
name, (xmlElementTypeVal) type, content);
else {
xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR,
"SAX.xmlSAX2ElementDecl(%s) called while not in subset\n",
name, NULL);
return;
}
#ifdef LIBXML_VALID_ENABLED
if (elem == NULL)
ctxt->valid = 0;
if (ctxt->validate && ctxt->wellFormed &&
ctxt->myDoc && ctxt->myDoc->intSubset)
ctxt->valid &=
xmlValidateElementDecl(&ctxt->vctxt, ctxt->myDoc, elem);
#endif /* LIBXML_VALID_ENABLED */
}
/**
* xmlSAX2NotationDecl:
* @ctx: the user data (XML parser context)
* @name: The name of the notation
* @publicId: The public ID of the entity
* @systemId: The system ID of the entity
*
* What to do when a notation declaration has been parsed.
*/
void
xmlSAX2NotationDecl(void *ctx, const xmlChar *name,
const xmlChar *publicId, const xmlChar *systemId)
{
xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
xmlNotationPtr nota = NULL;
/* Avoid unused variable warning if features are disabled. */
(void) nota;
if ((ctxt == NULL) || (ctxt->myDoc == NULL))
return;
if ((publicId == NULL) && (systemId == NULL)) {
xmlFatalErrMsg(ctxt, XML_ERR_NOTATION_PROCESSING,
"SAX.xmlSAX2NotationDecl(%s) externalID or PublicID missing\n",
name, NULL);
return;
} else if (ctxt->inSubset == 1)
nota = xmlAddNotationDecl(&ctxt->vctxt, ctxt->myDoc->intSubset, name,
publicId, systemId);
else if (ctxt->inSubset == 2)
nota = xmlAddNotationDecl(&ctxt->vctxt, ctxt->myDoc->extSubset, name,
publicId, systemId);
else {
xmlFatalErrMsg(ctxt, XML_ERR_NOTATION_PROCESSING,
"SAX.xmlSAX2NotationDecl(%s) called while not in subset\n",
name, NULL);
return;
}
#ifdef LIBXML_VALID_ENABLED
if (nota == NULL) ctxt->valid = 0;
if ((ctxt->validate) && (ctxt->wellFormed) &&
(ctxt->myDoc->intSubset != NULL))
ctxt->valid &= xmlValidateNotationDecl(&ctxt->vctxt, ctxt->myDoc,
nota);
#endif /* LIBXML_VALID_ENABLED */
}
/**
* xmlSAX2UnparsedEntityDecl:
* @ctx: the user data (XML parser context)
* @name: The name of the entity
* @publicId: The public ID of the entity
* @systemId: The system ID of the entity
* @notationName: the name of the notation
*
* What to do when an unparsed entity declaration is parsed
*/
void
xmlSAX2UnparsedEntityDecl(void *ctx, const xmlChar *name,
const xmlChar *publicId, const xmlChar *systemId,
const xmlChar *notationName)
{
xmlSAX2EntityDecl(ctx, name, XML_EXTERNAL_GENERAL_UNPARSED_ENTITY,
publicId, systemId, (xmlChar *) notationName);
}
/**
* xmlSAX2SetDocumentLocator:
* @ctx: the user data (XML parser context)
* @loc: A SAX Locator
*
* Receive the document locator at startup, actually xmlDefaultSAXLocator
* Everything is available on the context, so this is useless in our case.
*/
void
xmlSAX2SetDocumentLocator(void *ctx ATTRIBUTE_UNUSED, xmlSAXLocatorPtr loc ATTRIBUTE_UNUSED)
{
}
/**
* xmlSAX2StartDocument:
* @ctx: the user data (XML parser context)
*
* called when the document start being processed.
*/
void
xmlSAX2StartDocument(void *ctx)
{
xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
xmlDocPtr doc;
if (ctx == NULL) return;
#ifdef LIBXML_HTML_ENABLED
if (ctxt->html) {
if (ctxt->myDoc == NULL)
ctxt->myDoc = htmlNewDocNoDtD(NULL, NULL);
if (ctxt->myDoc == NULL) {
xmlSAX2ErrMemory(ctxt);
return;
}
ctxt->myDoc->properties = XML_DOC_HTML;
ctxt->myDoc->parseFlags = ctxt->options;
} else
#endif
{
doc = ctxt->myDoc = xmlNewDoc(ctxt->version);
if (doc != NULL) {
doc->properties = 0;
if (ctxt->options & XML_PARSE_OLD10)
doc->properties |= XML_DOC_OLD10;
doc->parseFlags = ctxt->options;
doc->standalone = ctxt->standalone;
} else {
xmlSAX2ErrMemory(ctxt);
return;
}
if ((ctxt->dictNames) && (doc != NULL)) {
doc->dict = ctxt->dict;
xmlDictReference(doc->dict);
}
}
if ((ctxt->myDoc != NULL) && (ctxt->myDoc->URL == NULL) &&
(ctxt->input != NULL) && (ctxt->input->filename != NULL)) {
ctxt->myDoc->URL = xmlPathToURI((const xmlChar *)ctxt->input->filename);
if (ctxt->myDoc->URL == NULL)
xmlSAX2ErrMemory(ctxt);
}
}
/**
* xmlSAX2EndDocument:
* @ctx: the user data (XML parser context)
*
* called when the document end has been detected.
*/
void
xmlSAX2EndDocument(void *ctx)
{
xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
xmlDocPtr doc;
if (ctx == NULL) return;
#ifdef LIBXML_VALID_ENABLED
if (ctxt->validate && ctxt->wellFormed &&
ctxt->myDoc && ctxt->myDoc->intSubset)
ctxt->valid &= xmlValidateDocumentFinal(&ctxt->vctxt, ctxt->myDoc);
#endif /* LIBXML_VALID_ENABLED */
doc = ctxt->myDoc;
if ((doc != NULL) && (doc->encoding == NULL)) {
const xmlChar *encoding = xmlGetActualEncoding(ctxt);
if (encoding != NULL) {
doc->encoding = xmlStrdup(encoding);
if (doc->encoding == NULL)
xmlSAX2ErrMemory(ctxt);
}
}
}
static void
xmlSAX2AppendChild(xmlParserCtxtPtr ctxt, xmlNodePtr node) {
xmlNodePtr parent;
xmlNodePtr last;
if (ctxt->inSubset == 1) {
parent = (xmlNodePtr) ctxt->myDoc->intSubset;
} else if (ctxt->inSubset == 2) {
parent = (xmlNodePtr) ctxt->myDoc->extSubset;
} else {
parent = ctxt->node;
if (parent == NULL)
parent = (xmlNodePtr) ctxt->myDoc;
}
last = parent->last;
if (last == NULL) {
parent->children = node;
} else {
last->next = node;
node->prev = last;
}
parent->last = node;
node->parent = parent;
if ((node->type != XML_TEXT_NODE) &&
(ctxt->linenumbers) &&
(ctxt->input != NULL)) {
if ((unsigned) ctxt->input->line < (unsigned) USHRT_MAX)
node->line = ctxt->input->line;
else
node->line = USHRT_MAX;
}
}
#if defined(LIBXML_SAX1_ENABLED)
/**
* xmlNsErrMsg:
* @ctxt: an XML parser context
* @error: the error number
* @msg: the error message
* @str1: an error string
* @str2: an error string
*
* Handle a namespace error
*/
static void LIBXML_ATTR_FORMAT(3,0)
xmlNsErrMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error,
const char *msg, const xmlChar *str1, const xmlChar *str2)
{
xmlCtxtErr(ctxt, NULL, XML_FROM_NAMESPACE, error, XML_ERR_ERROR,
str1, str2, NULL, 0, msg, str1, str2);
}
/**
* xmlSAX1Attribute:
* @ctx: the user data (XML parser context)
* @fullname: The attribute name, including namespace prefix
* @value: The attribute value
*
* Handle an attribute that has been read by the parser.
*
* Deprecated SAX1 interface.
*/
static void
xmlSAX1Attribute(xmlParserCtxtPtr ctxt, const xmlChar *fullname,
const xmlChar *value, const xmlChar *prefix)
{
xmlAttrPtr ret;
const xmlChar *name;
xmlChar *ns;
xmlNsPtr namespace;
/*
* Split the full name into a namespace prefix and the tag name
*/
name = xmlSplitQName4(fullname, &ns);
if (name == NULL) {
xmlSAX2ErrMemory(ctxt);
return;
}
/*
* Check whether it's a namespace definition
*/
if ((ns == NULL) &&
(name[0] == 'x') && (name[1] == 'm') && (name[2] == 'l') &&
(name[3] == 'n') && (name[4] == 's') && (name[5] == 0)) {
xmlNsPtr nsret;
xmlChar *val;
/* Avoid unused variable warning if features are disabled. */
(void) nsret;
if (!ctxt->replaceEntities) {
/* TODO: normalize if needed */
val = xmlExpandEntitiesInAttValue(ctxt, value, /* normalize */ 0);
if (val == NULL) {
xmlSAX2ErrMemory(ctxt);
return;
}
} else {
val = (xmlChar *) value;
}
if (val[0] != 0) {
xmlURIPtr uri;
if (xmlParseURISafe((const char *)val, &uri) < 0)
xmlSAX2ErrMemory(ctxt);
if (uri == NULL) {
xmlNsWarnMsg(ctxt, XML_WAR_NS_URI,
"xmlns:%s: %s not a valid URI\n", name, value);
} else {
if (uri->scheme == NULL) {
xmlNsWarnMsg(ctxt, XML_WAR_NS_URI_RELATIVE,
"xmlns:%s: URI %s is not absolute\n",
name, value);
}
xmlFreeURI(uri);
}
}