forked from GNOME/libxml2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.c
14233 lines (12835 loc) · 385 KB
/
parser.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
/*
* parser.c : an XML 1.0 parser, namespaces and validity support are mostly
* implemented on top of the SAX interfaces
*
* References:
* The XML specification:
* http://www.w3.org/TR/REC-xml
* Original 1.0 version:
* http://www.w3.org/TR/1998/REC-xml-19980210
* XML second edition working draft
* http://www.w3.org/TR/2000/WD-xml-2e-20000814
*
* Okay this is a big file, the parser core is around 7000 lines, then it
* is followed by the progressive parser top routines, then the various
* high level APIs to call the parser and a few miscellaneous functions.
* A number of helper functions and deprecated ones have been moved to
* parserInternals.c to reduce this file size.
* As much as possible the functions are associated with their relative
* production in the XML specification. A few productions defining the
* different ranges of character are actually implanted either in
* parserInternals.h or parserInternals.c
* The DOM tree build is realized from the default SAX callbacks in
* the module SAX2.c.
* The routines doing the validation checks are in valid.c and called either
* from the SAX callbacks or as standalone functions using a preparsed
* document.
*
* See Copyright for the status of this software.
*
* daniel@veillard.com
*/
/* To avoid EBCDIC trouble when parsing on zOS */
#if defined(__MVS__)
#pragma convert("ISO8859-1")
#endif
#define IN_LIBXML
#include "libxml.h"
#if defined(_WIN32)
#define XML_DIR_SEP '\\'
#else
#define XML_DIR_SEP '/'
#endif
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <stdarg.h>
#include <stddef.h>
#include <ctype.h>
#include <stdlib.h>
#include <libxml/parser.h>
#include <libxml/xmlmemory.h>
#include <libxml/tree.h>
#include <libxml/parserInternals.h>
#include <libxml/valid.h>
#include <libxml/entities.h>
#include <libxml/xmlerror.h>
#include <libxml/encoding.h>
#include <libxml/xmlIO.h>
#include <libxml/uri.h>
#include <libxml/SAX2.h>
#include <libxml/HTMLparser.h>
#ifdef LIBXML_CATALOG_ENABLED
#include <libxml/catalog.h>
#endif
#include "private/buf.h"
#include "private/dict.h"
#include "private/entities.h"
#include "private/error.h"
#include "private/html.h"
#include "private/io.h"
#include "private/parser.h"
#define NS_INDEX_EMPTY INT_MAX
#define NS_INDEX_XML (INT_MAX - 1)
#define URI_HASH_EMPTY 0xD943A04E
#define URI_HASH_XML 0xF0451F02
#ifndef STDIN_FILENO
#define STDIN_FILENO 0
#endif
#ifndef SIZE_MAX
#define SIZE_MAX ((size_t) -1)
#endif
struct _xmlStartTag {
const xmlChar *prefix;
const xmlChar *URI;
int line;
int nsNr;
};
typedef struct {
void *saxData;
unsigned prefixHashValue;
unsigned uriHashValue;
unsigned elementId;
int oldIndex;
} xmlParserNsExtra;
typedef struct {
unsigned hashValue;
int index;
} xmlParserNsBucket;
struct _xmlParserNsData {
xmlParserNsExtra *extra;
unsigned hashSize;
unsigned hashElems;
xmlParserNsBucket *hash;
unsigned elementId;
int defaultNsIndex;
int minNsIndex;
};
static int
xmlParseElementStart(xmlParserCtxtPtr ctxt);
static void
xmlParseElementEnd(xmlParserCtxtPtr ctxt);
static xmlEntityPtr
xmlLookupGeneralEntity(xmlParserCtxtPtr ctxt, const xmlChar *name, int inAttr);
static const xmlChar *
xmlParseEntityRefInternal(xmlParserCtxtPtr ctxt);
/************************************************************************
* *
* Arbitrary limits set in the parser. See XML_PARSE_HUGE *
* *
************************************************************************/
#define XML_PARSER_BIG_ENTITY 1000
#define XML_PARSER_LOT_ENTITY 5000
/*
* Constants for protection against abusive entity expansion
* ("billion laughs").
*/
/*
* A certain amount of entity expansion which is always allowed.
*/
#define XML_PARSER_ALLOWED_EXPANSION 1000000
/*
* Fixed cost for each entity reference. This crudely models processing time
* as well to protect, for example, against exponential expansion of empty
* or very short entities.
*/
#define XML_ENT_FIXED_COST 20
/**
* xmlParserMaxDepth:
*
* arbitrary depth limit for the XML documents that we allow to
* process. This is not a limitation of the parser but a safety
* boundary feature. It can be disabled with the XML_PARSE_HUGE
* parser option.
*/
const unsigned int xmlParserMaxDepth = 256;
#define XML_PARSER_BIG_BUFFER_SIZE 300
#define XML_PARSER_BUFFER_SIZE 100
#define SAX_COMPAT_MODE BAD_CAST "SAX compatibility mode document"
/**
* XML_PARSER_CHUNK_SIZE
*
* When calling GROW that's the minimal amount of data
* the parser expected to have received. It is not a hard
* limit but an optimization when reading strings like Names
* It is not strictly needed as long as inputs available characters
* are followed by 0, which should be provided by the I/O level
*/
#define XML_PARSER_CHUNK_SIZE 100
/**
* xmlParserVersion:
*
* Constant string describing the internal version of the library
*/
const char *const
xmlParserVersion = LIBXML_VERSION_STRING LIBXML_VERSION_EXTRA;
/*
* List of XML prefixed PI allowed by W3C specs
*/
static const char* const xmlW3CPIs[] = {
"xml-stylesheet",
"xml-model",
NULL
};
/* DEPR void xmlParserHandleReference(xmlParserCtxtPtr ctxt); */
static xmlEntityPtr xmlParseStringPEReference(xmlParserCtxtPtr ctxt,
const xmlChar **str);
static void
xmlCtxtParseEntity(xmlParserCtxtPtr ctxt, xmlEntityPtr ent);
static int
xmlLoadEntityContent(xmlParserCtxtPtr ctxt, xmlEntityPtr entity);
/************************************************************************
* *
* Some factorized error routines *
* *
************************************************************************/
static void
xmlErrMemory(xmlParserCtxtPtr ctxt) {
xmlCtxtErrMemory(ctxt);
}
/**
* xmlErrAttributeDup:
* @ctxt: an XML parser context
* @prefix: the attribute prefix
* @localname: the attribute localname
*
* Handle a redefinition of attribute error
*/
static void
xmlErrAttributeDup(xmlParserCtxtPtr ctxt, const xmlChar * prefix,
const xmlChar * localname)
{
if (prefix == NULL)
xmlCtxtErr(ctxt, NULL, XML_FROM_PARSER, XML_ERR_ATTRIBUTE_REDEFINED,
XML_ERR_FATAL, localname, NULL, NULL, 0,
"Attribute %s redefined\n", localname);
else
xmlCtxtErr(ctxt, NULL, XML_FROM_PARSER, XML_ERR_ATTRIBUTE_REDEFINED,
XML_ERR_FATAL, prefix, localname, NULL, 0,
"Attribute %s:%s redefined\n", prefix, localname);
}
/**
* xmlFatalErrMsg:
* @ctxt: an XML parser context
* @error: the error number
* @msg: the error message
*
* 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)
{
xmlCtxtErr(ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_FATAL,
NULL, NULL, NULL, 0, "%s", msg);
}
/**
* xmlWarningMsg:
* @ctxt: an XML parser context
* @error: the error number
* @msg: the error message
* @str1: extra data
* @str2: extra data
*
* Handle a warning.
*/
void LIBXML_ATTR_FORMAT(3,0)
xmlWarningMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error,
const char *msg, const xmlChar *str1, const xmlChar *str2)
{
xmlCtxtErr(ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_WARNING,
str1, str2, NULL, 0, msg, str1, str2);
}
/**
* xmlValidityError:
* @ctxt: an XML parser context
* @error: the error number
* @msg: the error message
* @str1: extra data
*
* Handle a validity error.
*/
static void LIBXML_ATTR_FORMAT(3,0)
xmlValidityError(xmlParserCtxtPtr ctxt, xmlParserErrors error,
const char *msg, const xmlChar *str1, const xmlChar *str2)
{
ctxt->valid = 0;
xmlCtxtErr(ctxt, NULL, XML_FROM_DTD, error, XML_ERR_ERROR,
str1, str2, NULL, 0, msg, str1, str2);
}
/**
* xmlFatalErrMsgInt:
* @ctxt: an XML parser context
* @error: the error number
* @msg: the error message
* @val: an integer value
*
* Handle a fatal parser error, i.e. violating Well-Formedness constraints
*/
static void LIBXML_ATTR_FORMAT(3,0)
xmlFatalErrMsgInt(xmlParserCtxtPtr ctxt, xmlParserErrors error,
const char *msg, int val)
{
xmlCtxtErr(ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_FATAL,
NULL, NULL, NULL, val, msg, val);
}
/**
* xmlFatalErrMsgStrIntStr:
* @ctxt: an XML parser context
* @error: the error number
* @msg: the error message
* @str1: an string info
* @val: an integer value
* @str2: an string info
*
* Handle a fatal parser error, i.e. violating Well-Formedness constraints
*/
static void LIBXML_ATTR_FORMAT(3,0)
xmlFatalErrMsgStrIntStr(xmlParserCtxtPtr ctxt, xmlParserErrors error,
const char *msg, const xmlChar *str1, int val,
const xmlChar *str2)
{
xmlCtxtErr(ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_FATAL,
str1, str2, NULL, val, msg, str1, val, str2);
}
/**
* xmlFatalErrMsgStr:
* @ctxt: an XML parser context
* @error: the error number
* @msg: the error message
* @val: a string value
*
* Handle a fatal parser error, i.e. violating Well-Formedness constraints
*/
static void LIBXML_ATTR_FORMAT(3,0)
xmlFatalErrMsgStr(xmlParserCtxtPtr ctxt, xmlParserErrors error,
const char *msg, const xmlChar * val)
{
xmlCtxtErr(ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_FATAL,
val, NULL, NULL, 0, msg, val);
}
/**
* xmlErrMsgStr:
* @ctxt: an XML parser context
* @error: the error number
* @msg: the error message
* @val: a string value
*
* Handle a non fatal parser error
*/
static void LIBXML_ATTR_FORMAT(3,0)
xmlErrMsgStr(xmlParserCtxtPtr ctxt, xmlParserErrors error,
const char *msg, const xmlChar * val)
{
xmlCtxtErr(ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_ERROR,
val, NULL, NULL, 0, msg, val);
}
/**
* xmlNsErr:
* @ctxt: an XML parser context
* @error: the error number
* @msg: the message
* @info1: extra information string
* @info2: extra information string
*
* Handle a fatal parser error, i.e. violating Well-Formedness constraints
*/
static void LIBXML_ATTR_FORMAT(3,0)
xmlNsErr(xmlParserCtxtPtr ctxt, xmlParserErrors error,
const char *msg,
const xmlChar * info1, const xmlChar * info2,
const xmlChar * info3)
{
ctxt->nsWellFormed = 0;
xmlCtxtErr(ctxt, NULL, XML_FROM_NAMESPACE, error, XML_ERR_ERROR,
info1, info2, info3, 0, msg, info1, info2, info3);
}
/**
* xmlNsWarn
* @ctxt: an XML parser context
* @error: the error number
* @msg: the message
* @info1: extra information string
* @info2: extra information string
*
* Handle a namespace warning error
*/
static void LIBXML_ATTR_FORMAT(3,0)
xmlNsWarn(xmlParserCtxtPtr ctxt, xmlParserErrors error,
const char *msg,
const xmlChar * info1, const xmlChar * info2,
const xmlChar * info3)
{
xmlCtxtErr(ctxt, NULL, XML_FROM_NAMESPACE, error, XML_ERR_WARNING,
info1, info2, info3, 0, msg, info1, info2, info3);
}
static void
xmlSaturatedAdd(unsigned long *dst, unsigned long val) {
if (val > ULONG_MAX - *dst)
*dst = ULONG_MAX;
else
*dst += val;
}
static void
xmlSaturatedAddSizeT(unsigned long *dst, unsigned long val) {
if (val > ULONG_MAX - *dst)
*dst = ULONG_MAX;
else
*dst += val;
}
/**
* xmlParserEntityCheck:
* @ctxt: parser context
* @extra: sum of unexpanded entity sizes
*
* Check for non-linear entity expansion behaviour.
*
* In some cases like xmlExpandEntityInAttValue, this function is called
* for each, possibly nested entity and its unexpanded content length.
*
* In other cases like xmlParseReference, it's only called for each
* top-level entity with its unexpanded content length plus the sum of
* the unexpanded content lengths (plus fixed cost) of all nested
* entities.
*
* Summing the unexpanded lengths also adds the length of the reference.
* This is by design. Taking the length of the entity name into account
* discourages attacks that try to waste CPU time with abusively long
* entity names. See test/recurse/lol6.xml for example. Each call also
* adds some fixed cost XML_ENT_FIXED_COST to discourage attacks with
* short entities.
*
* Returns 1 on error, 0 on success.
*/
static int
xmlParserEntityCheck(xmlParserCtxtPtr ctxt, unsigned long extra)
{
unsigned long consumed;
unsigned long *expandedSize;
xmlParserInputPtr input = ctxt->input;
xmlEntityPtr entity = input->entity;
if ((entity) && (entity->flags & XML_ENT_CHECKED))
return(0);
/*
* Compute total consumed bytes so far, including input streams of
* external entities.
*/
consumed = input->consumed;
xmlSaturatedAddSizeT(&consumed, input->cur - input->base);
xmlSaturatedAdd(&consumed, ctxt->sizeentities);
if (entity)
expandedSize = &entity->expandedSize;
else
expandedSize = &ctxt->sizeentcopy;
/*
* Add extra cost and some fixed cost.
*/
xmlSaturatedAdd(expandedSize, extra);
xmlSaturatedAdd(expandedSize, XML_ENT_FIXED_COST);
/*
* It's important to always use saturation arithmetic when tracking
* entity sizes to make the size checks reliable. If "sizeentcopy"
* overflows, we have to abort.
*/
if ((*expandedSize > XML_PARSER_ALLOWED_EXPANSION) &&
((*expandedSize >= ULONG_MAX) ||
(*expandedSize / ctxt->maxAmpl > consumed))) {
xmlFatalErrMsg(ctxt, XML_ERR_RESOURCE_LIMIT,
"Maximum entity amplification factor exceeded, see "
"xmlCtxtSetMaxAmplification.\n");
xmlHaltParser(ctxt);
return(1);
}
return(0);
}
/************************************************************************
* *
* Library wide options *
* *
************************************************************************/
/**
* xmlHasFeature:
* @feature: the feature to be examined
*
* Examines if the library has been compiled with a given feature.
*
* Returns a non-zero value if the feature exist, otherwise zero.
* Returns zero (0) if the feature does not exist or an unknown
* unknown feature is requested, non-zero otherwise.
*/
int
xmlHasFeature(xmlFeature feature)
{
switch (feature) {
case XML_WITH_THREAD:
#ifdef LIBXML_THREAD_ENABLED
return(1);
#else
return(0);
#endif
case XML_WITH_TREE:
return(1);
case XML_WITH_OUTPUT:
#ifdef LIBXML_OUTPUT_ENABLED
return(1);
#else
return(0);
#endif
case XML_WITH_PUSH:
#ifdef LIBXML_PUSH_ENABLED
return(1);
#else
return(0);
#endif
case XML_WITH_READER:
#ifdef LIBXML_READER_ENABLED
return(1);
#else
return(0);
#endif
case XML_WITH_PATTERN:
#ifdef LIBXML_PATTERN_ENABLED
return(1);
#else
return(0);
#endif
case XML_WITH_WRITER:
#ifdef LIBXML_WRITER_ENABLED
return(1);
#else
return(0);
#endif
case XML_WITH_SAX1:
#ifdef LIBXML_SAX1_ENABLED
return(1);
#else
return(0);
#endif
case XML_WITH_HTTP:
#ifdef LIBXML_HTTP_ENABLED
return(1);
#else
return(0);
#endif
case XML_WITH_VALID:
#ifdef LIBXML_VALID_ENABLED
return(1);
#else
return(0);
#endif
case XML_WITH_HTML:
#ifdef LIBXML_HTML_ENABLED
return(1);
#else
return(0);
#endif
case XML_WITH_LEGACY:
#ifdef LIBXML_LEGACY_ENABLED
return(1);
#else
return(0);
#endif
case XML_WITH_C14N:
#ifdef LIBXML_C14N_ENABLED
return(1);
#else
return(0);
#endif
case XML_WITH_CATALOG:
#ifdef LIBXML_CATALOG_ENABLED
return(1);
#else
return(0);
#endif
case XML_WITH_XPATH:
#ifdef LIBXML_XPATH_ENABLED
return(1);
#else
return(0);
#endif
case XML_WITH_XPTR:
#ifdef LIBXML_XPTR_ENABLED
return(1);
#else
return(0);
#endif
case XML_WITH_XINCLUDE:
#ifdef LIBXML_XINCLUDE_ENABLED
return(1);
#else
return(0);
#endif
case XML_WITH_ICONV:
#ifdef LIBXML_ICONV_ENABLED
return(1);
#else
return(0);
#endif
case XML_WITH_ISO8859X:
#ifdef LIBXML_ISO8859X_ENABLED
return(1);
#else
return(0);
#endif
case XML_WITH_UNICODE:
#ifdef LIBXML_UNICODE_ENABLED
return(1);
#else
return(0);
#endif
case XML_WITH_REGEXP:
#ifdef LIBXML_REGEXP_ENABLED
return(1);
#else
return(0);
#endif
case XML_WITH_AUTOMATA:
#ifdef LIBXML_REGEXP_ENABLED
return(1);
#else
return(0);
#endif
case XML_WITH_EXPR:
#ifdef LIBXML_EXPR_ENABLED
return(1);
#else
return(0);
#endif
case XML_WITH_SCHEMAS:
#ifdef LIBXML_SCHEMAS_ENABLED
return(1);
#else
return(0);
#endif
case XML_WITH_SCHEMATRON:
#ifdef LIBXML_SCHEMATRON_ENABLED
return(1);
#else
return(0);
#endif
case XML_WITH_MODULES:
#ifdef LIBXML_MODULES_ENABLED
return(1);
#else
return(0);
#endif
case XML_WITH_DEBUG:
#ifdef LIBXML_DEBUG_ENABLED
return(1);
#else
return(0);
#endif
case XML_WITH_DEBUG_MEM:
return(0);
case XML_WITH_ZLIB:
#ifdef LIBXML_ZLIB_ENABLED
return(1);
#else
return(0);
#endif
case XML_WITH_LZMA:
#ifdef LIBXML_LZMA_ENABLED
return(1);
#else
return(0);
#endif
case XML_WITH_ICU:
#ifdef LIBXML_ICU_ENABLED
return(1);
#else
return(0);
#endif
default:
break;
}
return(0);
}
/************************************************************************
* *
* Simple string buffer *
* *
************************************************************************/
typedef struct {
xmlChar *mem;
unsigned size;
unsigned cap; /* size < cap */
unsigned max; /* size <= max */
xmlParserErrors code;
} xmlSBuf;
static void
xmlSBufInit(xmlSBuf *buf, unsigned max) {
buf->mem = NULL;
buf->size = 0;
buf->cap = 0;
buf->max = max;
buf->code = XML_ERR_OK;
}
static int
xmlSBufGrow(xmlSBuf *buf, unsigned len) {
xmlChar *mem;
unsigned cap;
if (len >= UINT_MAX / 2 - buf->size) {
if (buf->code == XML_ERR_OK)
buf->code = XML_ERR_RESOURCE_LIMIT;
return(-1);
}
cap = (buf->size + len) * 2;
if (cap < 240)
cap = 240;
mem = xmlRealloc(buf->mem, cap);
if (mem == NULL) {
buf->code = XML_ERR_NO_MEMORY;
return(-1);
}
buf->mem = mem;
buf->cap = cap;
return(0);
}
static void
xmlSBufAddString(xmlSBuf *buf, const xmlChar *str, unsigned len) {
if (buf->max - buf->size < len) {
if (buf->code == XML_ERR_OK)
buf->code = XML_ERR_RESOURCE_LIMIT;
return;
}
if (buf->cap - buf->size <= len) {
if (xmlSBufGrow(buf, len) < 0)
return;
}
if (len > 0)
memcpy(buf->mem + buf->size, str, len);
buf->size += len;
}
static void
xmlSBufAddCString(xmlSBuf *buf, const char *str, unsigned len) {
xmlSBufAddString(buf, (const xmlChar *) str, len);
}
static void
xmlSBufAddChar(xmlSBuf *buf, int c) {
xmlChar *end;
if (buf->max - buf->size < 4) {
if (buf->code == XML_ERR_OK)
buf->code = XML_ERR_RESOURCE_LIMIT;
return;
}
if (buf->cap - buf->size <= 4) {
if (xmlSBufGrow(buf, 4) < 0)
return;
}
end = buf->mem + buf->size;
if (c < 0x80) {
*end = (xmlChar) c;
buf->size += 1;
} else {
buf->size += xmlCopyCharMultiByte(end, c);
}
}
static void
xmlSBufAddReplChar(xmlSBuf *buf) {
xmlSBufAddCString(buf, "\xEF\xBF\xBD", 3);
}
static void
xmlSBufReportError(xmlSBuf *buf, xmlParserCtxtPtr ctxt, const char *errMsg) {
if (buf->code == XML_ERR_NO_MEMORY)
xmlCtxtErrMemory(ctxt);
else
xmlFatalErr(ctxt, buf->code, errMsg);
}
static xmlChar *
xmlSBufFinish(xmlSBuf *buf, int *sizeOut, xmlParserCtxtPtr ctxt,
const char *errMsg) {
if (buf->mem == NULL) {
buf->mem = xmlMalloc(1);
if (buf->mem == NULL) {
buf->code = XML_ERR_NO_MEMORY;
} else {
buf->mem[0] = 0;
}
} else {
buf->mem[buf->size] = 0;
}
if (buf->code == XML_ERR_OK) {
if (sizeOut != NULL)
*sizeOut = buf->size;
return(buf->mem);
}
xmlSBufReportError(buf, ctxt, errMsg);
xmlFree(buf->mem);
if (sizeOut != NULL)
*sizeOut = 0;
return(NULL);
}
static void
xmlSBufCleanup(xmlSBuf *buf, xmlParserCtxtPtr ctxt, const char *errMsg) {
if (buf->code != XML_ERR_OK)
xmlSBufReportError(buf, ctxt, errMsg);
xmlFree(buf->mem);
}
static int
xmlUTF8MultibyteLen(xmlParserCtxtPtr ctxt, const xmlChar *str,
const char *errMsg) {
int c = str[0];
int c1 = str[1];
if ((c1 & 0xC0) != 0x80)
goto encoding_error;
if (c < 0xE0) {
/* 2-byte sequence */
if (c < 0xC2)
goto encoding_error;
return(2);
} else {
int c2 = str[2];
if ((c2 & 0xC0) != 0x80)
goto encoding_error;
if (c < 0xF0) {
/* 3-byte sequence */
if (c == 0xE0) {
/* overlong */
if (c1 < 0xA0)
goto encoding_error;
} else if (c == 0xED) {
/* surrogate */
if (c1 >= 0xA0)
goto encoding_error;
} else if (c == 0xEF) {
/* U+FFFE and U+FFFF are invalid Chars */
if ((c1 == 0xBF) && (c2 >= 0xBE))
xmlFatalErrMsg(ctxt, XML_ERR_INVALID_CHAR, errMsg);
}
return(3);
} else {
/* 4-byte sequence */
if ((str[3] & 0xC0) != 0x80)
goto encoding_error;
if (c == 0xF0) {
/* overlong */
if (c1 < 0x90)
goto encoding_error;
} else if (c >= 0xF4) {
/* greater than 0x10FFFF */
if ((c > 0xF4) || (c1 >= 0x90))
goto encoding_error;
}
return(4);
}
}
encoding_error:
/* Only report the first error */
if ((ctxt->input->flags & XML_INPUT_ENCODING_ERROR) == 0) {
xmlCtxtErrIO(ctxt, XML_ERR_INVALID_ENCODING, NULL);
ctxt->input->flags |= XML_INPUT_ENCODING_ERROR;
}
return(0);
}
/************************************************************************
* *
* SAX2 defaulted attributes handling *
* *
************************************************************************/
/**
* xmlCtxtInitializeLate:
* @ctxt: an XML parser context
*
* Final initialization of the parser context before starting to parse.
*
* This accounts for users modifying struct members of parser context
* directly.
*/
static void
xmlCtxtInitializeLate(xmlParserCtxtPtr ctxt) {
xmlSAXHandlerPtr sax;
/* Avoid unused variable warning if features are disabled. */
(void) sax;
/*
* Changing the SAX struct directly is still widespread practice
* in internal and external code.
*/
if (ctxt == NULL) return;
sax = ctxt->sax;
#ifdef LIBXML_SAX1_ENABLED
/*
* Only enable SAX2 if there SAX2 element handlers, except when there
* are no element handlers at all.
*/
if (((ctxt->options & XML_PARSE_SAX1) == 0) &&
(sax) &&
(sax->initialized == XML_SAX2_MAGIC) &&
((sax->startElementNs != NULL) ||
(sax->endElementNs != NULL) ||
((sax->startElement == NULL) && (sax->endElement == NULL))))
ctxt->sax2 = 1;
#else
ctxt->sax2 = 1;
#endif /* LIBXML_SAX1_ENABLED */
/*
* Some users replace the dictionary directly in the context struct.
* We really need an API function to do that cleanly.
*/
ctxt->str_xml = xmlDictLookup(ctxt->dict, BAD_CAST "xml", 3);
ctxt->str_xmlns = xmlDictLookup(ctxt->dict, BAD_CAST "xmlns", 5);
ctxt->str_xml_ns = xmlDictLookup(ctxt->dict, XML_XML_NAMESPACE, 36);
if ((ctxt->str_xml==NULL) || (ctxt->str_xmlns==NULL) ||
(ctxt->str_xml_ns == NULL)) {
xmlErrMemory(ctxt);
}
xmlDictSetLimit(ctxt->dict,
(ctxt->options & XML_PARSE_HUGE) ?
0 :
XML_MAX_DICTIONARY_LIMIT);
}
typedef struct {
xmlHashedString prefix;
xmlHashedString name;
xmlHashedString value;
const xmlChar *valueEnd;
int external;
int expandedSize;
} xmlDefAttr;
typedef struct _xmlDefAttrs xmlDefAttrs;
typedef xmlDefAttrs *xmlDefAttrsPtr;
struct _xmlDefAttrs {
int nbAttrs; /* number of defaulted attributes on that element */
int maxAttrs; /* the size of the array */
#if __STDC_VERSION__ >= 199901L
/* Using a C99 flexible array member avoids UBSan errors. */
xmlDefAttr attrs[] ATTRIBUTE_COUNTED_BY(maxAttrs);