forked from GNOME/libxml2
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathparserInternals.c
More file actions
3748 lines (3284 loc) · 98.4 KB
/
parserInternals.c
File metadata and controls
3748 lines (3284 loc) · 98.4 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
/*
* parserInternals.c : Internal routines (and obsolete ones) needed for the
* XML and HTML parsers.
*
* See Copyright for the status of this software.
*
* Author: Daniel Veillard
*/
#define IN_LIBXML
#include "libxml.h"
#if defined(_WIN32)
#define XML_DIR_SEP '\\'
#else
#define XML_DIR_SEP '/'
#endif
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <libxml/xmlmemory.h>
#include <libxml/tree.h>
#include <libxml/parser.h>
#include <libxml/parserInternals.h>
#include <libxml/entities.h>
#include <libxml/xmlerror.h>
#include <libxml/encoding.h>
#include <libxml/xmlIO.h>
#include <libxml/uri.h>
#include <libxml/dict.h>
#include <libxml/xmlsave.h>
#ifdef LIBXML_CATALOG_ENABLED
#include <libxml/catalog.h>
#endif
#include <libxml/chvalid.h>
#define CUR(ctxt) ctxt->input->cur
#define END(ctxt) ctxt->input->end
#include "private/buf.h"
#include "private/enc.h"
#include "private/error.h"
#include "private/globals.h"
#include "private/io.h"
#include "private/memory.h"
#include "private/parser.h"
#ifndef SIZE_MAX
#define SIZE_MAX ((size_t) -1)
#endif
#define XML_MAX_ERRORS 100
/*
* XML_MAX_AMPLIFICATION_DEFAULT is the default maximum allowed amplification
* factor of serialized output after entity expansion.
*/
#define XML_MAX_AMPLIFICATION_DEFAULT 5
/*
* Various global defaults for parsing
*/
/**
* check the compiled lib version against the include one.
*
* @param version the include version number
*/
void
xmlCheckVersion(int version) {
int myversion = LIBXML_VERSION;
xmlInitParser();
if ((myversion / 10000) != (version / 10000)) {
xmlPrintErrorMessage(
"Fatal: program compiled against libxml %d using libxml %d\n",
(version / 10000), (myversion / 10000));
} else if ((myversion / 100) < (version / 100)) {
xmlPrintErrorMessage(
"Warning: program compiled against libxml %d using older %d\n",
(version / 100), (myversion / 100));
}
}
/************************************************************************
* *
* Some factorized error routines *
* *
************************************************************************/
/**
* Register a callback function that will be called on errors and
* warnings. If handler is NULL, the error handler will be deactivated.
*
* If you only want to disable parser errors being printed to
* stderr, use xmlParserOption XML_PARSE_NOERROR.
*
* This is the recommended way to collect errors from the parser and
* takes precedence over all other error reporting mechanisms.
* These are (in order of precedence):
*
* - per-context structured handler (#xmlCtxtSetErrorHandler)
* - per-context structured "serror" SAX handler
* - global structured handler (#xmlSetStructuredErrorFunc)
* - per-context generic "error" and "warning" SAX handlers
* - global generic handler (#xmlSetGenericErrorFunc)
* - print to stderr
*
* @since 2.13.0
* @param ctxt an XML parser context
* @param handler error handler
* @param data data for error handler
*/
void
xmlCtxtSetErrorHandler(xmlParserCtxt *ctxt, xmlStructuredErrorFunc handler,
void *data)
{
if (ctxt == NULL)
return;
ctxt->errorHandler = handler;
ctxt->errorCtxt = data;
}
/**
* Get the last error raised.
*
* Note that the XML parser typically doesn't stop after
* encountering an error and will often report multiple errors.
* Most of the time, the last error isn't useful. Future
* versions might return the first parser error instead.
*
* @param ctx an XML parser context
* @returns NULL if no error occurred or a pointer to the error
*/
const xmlError *
xmlCtxtGetLastError(void *ctx)
{
xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
if (ctxt == NULL)
return (NULL);
if (ctxt->lastError.code == XML_ERR_OK)
return (NULL);
return (&ctxt->lastError);
}
/**
* Reset the last parser error to success. This does not change
* the well-formedness status.
*
* @param ctx an XML parser context
*/
void
xmlCtxtResetLastError(void *ctx)
{
xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
if (ctxt == NULL)
return;
ctxt->errNo = XML_ERR_OK;
if (ctxt->lastError.code == XML_ERR_OK)
return;
xmlResetError(&ctxt->lastError);
}
/**
* Handle an out-of-memory error.
*
* @since 2.13.0
* @param ctxt an XML parser context
*/
void
xmlCtxtErrMemory(xmlParserCtxt *ctxt)
{
xmlStructuredErrorFunc schannel = NULL;
xmlGenericErrorFunc channel = NULL;
void *data;
if (ctxt == NULL) {
xmlRaiseMemoryError(NULL, NULL, NULL, XML_FROM_PARSER, NULL);
return;
}
ctxt->errNo = XML_ERR_NO_MEMORY;
ctxt->instate = XML_PARSER_EOF; /* TODO: Remove after refactoring */
ctxt->wellFormed = 0;
ctxt->disableSAX = 2;
if (ctxt->errorHandler) {
schannel = ctxt->errorHandler;
data = ctxt->errorCtxt;
} else if ((ctxt->sax->initialized == XML_SAX2_MAGIC) &&
(ctxt->sax->serror != NULL)) {
schannel = ctxt->sax->serror;
data = ctxt->userData;
} else {
channel = ctxt->sax->error;
data = ctxt->userData;
}
xmlRaiseMemoryError(schannel, channel, data, XML_FROM_PARSER,
&ctxt->lastError);
}
/**
* If filename is empty, use the one from context input if available.
*
* Report an IO error to the parser context.
*
* @param ctxt parser context
* @param code xmlParserErrors code
* @param uri filename or URI (optional)
*/
void
xmlCtxtErrIO(xmlParserCtxt *ctxt, int code, const char *uri)
{
const char *errstr, *msg, *str1, *str2;
xmlErrorLevel level;
if (ctxt == NULL)
return;
if (((code == XML_IO_ENOENT) ||
(code == XML_IO_UNKNOWN))) {
/*
* Only report a warning if a file could not be found. This should
* only be done for external entities, but the external entity loader
* of xsltproc can try multiple paths and assumes that ENOENT doesn't
* raise an error and aborts parsing.
*/
if (ctxt->validate == 0)
level = XML_ERR_WARNING;
else
level = XML_ERR_ERROR;
} else if (code == XML_IO_NETWORK_ATTEMPT) {
level = XML_ERR_ERROR;
} else {
level = XML_ERR_FATAL;
}
errstr = xmlErrString(code);
if (uri == NULL) {
msg = "%s\n";
str1 = errstr;
str2 = NULL;
} else {
msg = "failed to load \"%s\": %s\n";
str1 = uri;
str2 = errstr;
}
xmlCtxtErr(ctxt, NULL, XML_FROM_IO, code, level,
(const xmlChar *) uri, NULL, NULL, 0,
msg, str1, str2);
}
/**
* @param ctxt parser context
* @returns true if the last error is catastrophic.
*/
int
xmlCtxtIsCatastrophicError(xmlParserCtxt *ctxt) {
if (ctxt == NULL)
return(1);
return(xmlIsCatastrophicError(ctxt->lastError.level,
ctxt->lastError.code));
}
/**
* Raise a parser error.
*
* @param ctxt a parser context
* @param node the current node or NULL
* @param domain the domain for the error
* @param code the code for the error
* @param level the xmlErrorLevel for the error
* @param str1 extra string info
* @param str2 extra string info
* @param str3 extra string info
* @param int1 extra int info
* @param msg the message to display/transmit
* @param ap extra parameters for the message display
*/
void
xmlCtxtVErr(xmlParserCtxt *ctxt, xmlNode *node, xmlErrorDomain domain,
xmlParserErrors code, xmlErrorLevel level,
const xmlChar *str1, const xmlChar *str2, const xmlChar *str3,
int int1, const char *msg, va_list ap)
{
xmlStructuredErrorFunc schannel = NULL;
xmlGenericErrorFunc channel = NULL;
void *data = NULL;
const char *file = NULL;
int line = 0;
int col = 0;
int res;
if (code == XML_ERR_NO_MEMORY) {
xmlCtxtErrMemory(ctxt);
return;
}
if (ctxt == NULL) {
res = xmlVRaiseError(NULL, NULL, NULL, NULL, node, domain, code,
level, NULL, 0, (const char *) str1,
(const char *) str2, (const char *) str3,
int1, 0, msg, ap);
if (res < 0)
xmlRaiseMemoryError(NULL, NULL, NULL, XML_FROM_PARSER, NULL);
return;
}
if (PARSER_STOPPED(ctxt))
return;
/* Don't overwrite catastrophic errors */
if (xmlCtxtIsCatastrophicError(ctxt))
return;
if (level == XML_ERR_WARNING) {
if (ctxt->nbWarnings >= XML_MAX_ERRORS)
return;
ctxt->nbWarnings += 1;
} else {
/*
* By long-standing design, the parser isn't completely
* stopped on well-formedness errors. Only SAX callbacks
* are disabled.
*
* In some situations, we really want to abort as fast
* as possible.
*/
if (xmlIsCatastrophicError(level, code) ||
code == XML_ERR_RESOURCE_LIMIT ||
code == XML_ERR_ENTITY_LOOP) {
ctxt->disableSAX = 2; /* really stop parser */
} else {
/* Report at least one fatal error. */
if (ctxt->nbErrors >= XML_MAX_ERRORS &&
(level < XML_ERR_FATAL || ctxt->wellFormed == 0))
return;
if (level == XML_ERR_FATAL && ctxt->recovery == 0)
ctxt->disableSAX = 1;
}
if (level == XML_ERR_FATAL)
ctxt->wellFormed = 0;
ctxt->errNo = code;
ctxt->nbErrors += 1;
}
if (((ctxt->options & XML_PARSE_NOERROR) == 0) &&
((level != XML_ERR_WARNING) ||
((ctxt->options & XML_PARSE_NOWARNING) == 0))) {
if (ctxt->errorHandler) {
schannel = ctxt->errorHandler;
data = ctxt->errorCtxt;
} else if ((ctxt->sax->initialized == XML_SAX2_MAGIC) &&
(ctxt->sax->serror != NULL)) {
schannel = ctxt->sax->serror;
data = ctxt->userData;
} else if ((domain == XML_FROM_VALID) || (domain == XML_FROM_DTD)) {
if (level == XML_ERR_WARNING)
channel = ctxt->vctxt.warning;
else
channel = ctxt->vctxt.error;
data = ctxt->vctxt.userData;
} else {
if (level == XML_ERR_WARNING)
channel = ctxt->sax->warning;
else
channel = ctxt->sax->error;
data = ctxt->userData;
}
}
if (ctxt->input != NULL) {
xmlParserInputPtr input = ctxt->input;
if ((input->filename == NULL) &&
(ctxt->inputNr > 1)) {
input = ctxt->inputTab[ctxt->inputNr - 2];
}
file = input->filename;
line = input->line;
col = input->col;
}
res = xmlVRaiseError(schannel, channel, data, ctxt, node, domain, code,
level, file, line, (const char *) str1,
(const char *) str2, (const char *) str3, int1, col,
msg, ap);
if (res < 0) {
xmlCtxtErrMemory(ctxt);
return;
}
}
/**
* Raise a parser error.
*
* @param ctxt a parser context
* @param node the current node or NULL
* @param domain the domain for the error
* @param code the code for the error
* @param level the xmlErrorLevel for the error
* @param str1 extra string info
* @param str2 extra string info
* @param str3 extra string info
* @param int1 extra int info
* @param msg the message to display/transmit
* @param ... extra parameters for the message display
*/
void
xmlCtxtErr(xmlParserCtxt *ctxt, xmlNode *node, xmlErrorDomain domain,
xmlParserErrors code, xmlErrorLevel level,
const xmlChar *str1, const xmlChar *str2, const xmlChar *str3,
int int1, const char *msg, ...)
{
va_list ap;
va_start(ap, msg);
xmlCtxtVErr(ctxt, node, domain, code, level,
str1, str2, str3, int1, msg, ap);
va_end(ap);
}
/**
* Get well-formedness and validation status after parsing. Also
* reports catastrophic errors which are not related to parsing
* like out-of-memory, I/O or other errors.
*
* @since 2.14.0
*
* @param ctxt an XML parser context
* @returns a bitmask of XML_STATUS_* flags ORed together.
*/
xmlParserStatus
xmlCtxtGetStatus(xmlParserCtxt *ctxt) {
xmlParserStatus bits = 0;
if (xmlCtxtIsCatastrophicError(ctxt)) {
bits |= XML_STATUS_CATASTROPHIC_ERROR |
XML_STATUS_NOT_WELL_FORMED |
XML_STATUS_NOT_NS_WELL_FORMED;
if ((ctxt != NULL) && (ctxt->validate))
bits |= XML_STATUS_DTD_VALIDATION_FAILED;
return(bits);
}
if (!ctxt->wellFormed)
bits |= XML_STATUS_NOT_WELL_FORMED;
if (!ctxt->nsWellFormed)
bits |= XML_STATUS_NOT_NS_WELL_FORMED;
if ((ctxt->validate) && (!ctxt->valid))
bits |= XML_STATUS_DTD_VALIDATION_FAILED;
return(bits);
}
/**
* Handle a fatal parser error, i.e. violating Well-Formedness constraints
*
* @param ctxt an XML parser context
* @param code the error number
* @param info extra information string
*/
void
xmlFatalErr(xmlParserCtxt *ctxt, xmlParserErrors code, const char *info)
{
const char *errmsg;
xmlErrorDomain domain = XML_FROM_PARSER;
xmlErrorLevel level = XML_ERR_FATAL;
errmsg = xmlErrString(code);
if ((ctxt != NULL) && (ctxt->html)) {
domain = XML_FROM_HTML;
/* Continue if encoding is unsupported */
if (code == XML_ERR_UNSUPPORTED_ENCODING)
level = XML_ERR_ERROR;
}
if (info == NULL) {
xmlCtxtErr(ctxt, NULL, domain, code, level,
NULL, NULL, NULL, 0, "%s\n", errmsg);
} else {
xmlCtxtErr(ctxt, NULL, domain, code, level,
(const xmlChar *) info, NULL, NULL, 0,
"%s: %s\n", errmsg, info);
}
}
/**
* Return window into current parser data.
*
* @param input parser input
* @param startOut start of window (output)
* @param sizeInOut maximum size of window (in)
* actual size of window (out)
* @param offsetOut offset of current position inside
* window (out)
*/
void
xmlParserInputGetWindow(xmlParserInput *input, const xmlChar **startOut,
int *sizeInOut, int *offsetOut) {
const xmlChar *cur, *base, *start;
int n, col;
int size = *sizeInOut;
cur = input->cur;
base = input->base;
/* skip backwards over any end-of-lines */
while ((cur > base) && ((*(cur) == '\n') || (*(cur) == '\r'))) {
cur--;
}
n = 0;
/* search backwards for beginning-of-line (to max buff size) */
while ((n < size) && (cur > base) &&
(*cur != '\n') && (*cur != '\r')) {
cur--;
n++;
}
if ((n > 0) && ((*cur == '\n') || (*cur == '\r'))) {
cur++;
} else {
/* skip over continuation bytes */
while ((cur < input->cur) && ((*cur & 0xC0) == 0x80))
cur++;
}
/* calculate the error position in terms of the current position */
col = input->cur - cur;
/* search forward for end-of-line (to max buff size) */
n = 0;
start = cur;
/* copy selected text to our buffer */
while ((*cur != 0) && (*(cur) != '\n') && (*(cur) != '\r')) {
int len = input->end - cur;
int c = xmlGetUTF8Char(cur, &len);
if ((c < 0) || (n + len > size))
break;
cur += len;
n += len;
}
/*
* col can only point to the end of the buffer if
* there's space for a marker.
*/
if (col >= n)
col = n < size ? n : size - 1;
*startOut = start;
*sizeInOut = n;
*offsetOut = col;
}
/**
* Check whether the character is allowed by the production
*
* @deprecated Internal function, don't use.
*
* ```
* [84] Letter ::= BaseChar | Ideographic
* ```
*
* @param c an unicode character (int)
* @returns 0 if not, non-zero otherwise
*/
int
xmlIsLetter(int c) {
return(IS_BASECHAR(c) || IS_IDEOGRAPHIC(c));
}
/************************************************************************
* *
* Input handling functions for progressive parsing *
* *
************************************************************************/
/* we need to keep enough input to show errors in context */
#define LINE_LEN 80
/**
* @deprecated This function was internal and is deprecated.
*
* @param in an XML parser input
* @param len an indicative size for the lookahead
* @returns -1 as this is an error to use it.
*/
int
xmlParserInputRead(xmlParserInput *in ATTRIBUTE_UNUSED, int len ATTRIBUTE_UNUSED) {
return(-1);
}
/**
* Grow the input buffer.
*
* @param ctxt an XML parser context
* @returns the number of bytes read or -1 in case of error.
*/
int
xmlParserGrow(xmlParserCtxt *ctxt) {
xmlParserInputPtr in = ctxt->input;
xmlParserInputBufferPtr buf = in->buf;
size_t curEnd = in->end - in->cur;
size_t curBase = in->cur - in->base;
size_t maxLength = (ctxt->options & XML_PARSE_HUGE) ?
XML_MAX_HUGE_LENGTH :
XML_MAX_LOOKUP_LIMIT;
int ret;
if (buf == NULL)
return(0);
/* Don't grow push parser buffer. */
if (PARSER_PROGRESSIVE(ctxt))
return(0);
/* Don't grow memory buffers. */
if ((buf->encoder == NULL) && (buf->readcallback == NULL))
return(0);
if (buf->error != 0)
return(-1);
if (curBase > maxLength) {
xmlFatalErr(ctxt, XML_ERR_RESOURCE_LIMIT,
"Buffer size limit exceeded, try XML_PARSE_HUGE\n");
return(-1);
}
if (curEnd >= INPUT_CHUNK)
return(0);
ret = xmlParserInputBufferGrow(buf, INPUT_CHUNK);
xmlBufUpdateInput(buf->buffer, in, curBase);
if (ret < 0) {
xmlCtxtErrIO(ctxt, buf->error, NULL);
}
return(ret);
}
/**
* Raises an error with `code` if the input wasn't consumed
* completely.
*
* @param ctxt parser ctxt
* @param code error code
*/
void
xmlParserCheckEOF(xmlParserCtxt *ctxt, xmlParserErrors code) {
xmlParserInputPtr in = ctxt->input;
xmlParserInputBufferPtr buf;
if (ctxt->errNo != XML_ERR_OK)
return;
if (in->cur < in->end) {
xmlFatalErr(ctxt, code, NULL);
return;
}
buf = in->buf;
if ((buf != NULL) && (buf->encoder != NULL)) {
size_t curBase = in->cur - in->base;
size_t sizeOut = 64;
xmlCharEncError ret;
/*
* Check for truncated multi-byte sequence
*/
ret = xmlCharEncInput(buf, &sizeOut, /* flush */ 1);
xmlBufUpdateInput(buf->buffer, in, curBase);
if (ret != XML_ENC_ERR_SUCCESS) {
xmlCtxtErrIO(ctxt, buf->error, NULL);
return;
}
/* Shouldn't happen */
if (in->cur < in->end)
xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, "expected EOF");
}
}
/**
* This function increase the input for the parser. It tries to
* preserve pointers to the input buffer, and keep already read data
*
* @deprecated Don't use.
*
* @param in an XML parser input
* @param len an indicative size for the lookahead
* @returns the amount of char read, or -1 in case of error, 0 indicate the
* end of this entity
*/
int
xmlParserInputGrow(xmlParserInput *in, int len) {
int ret;
size_t indx;
if ((in == NULL) || (len < 0)) return(-1);
if (in->buf == NULL) return(-1);
if (in->base == NULL) return(-1);
if (in->cur == NULL) return(-1);
if (in->buf->buffer == NULL) return(-1);
/* Don't grow memory buffers. */
if ((in->buf->encoder == NULL) && (in->buf->readcallback == NULL))
return(0);
indx = in->cur - in->base;
if (xmlBufUse(in->buf->buffer) > (unsigned int) indx + INPUT_CHUNK) {
return(0);
}
ret = xmlParserInputBufferGrow(in->buf, len);
in->base = xmlBufContent(in->buf->buffer);
if (in->base == NULL) {
in->base = BAD_CAST "";
in->cur = in->base;
in->end = in->base;
return(-1);
}
in->cur = in->base + indx;
in->end = xmlBufEnd(in->buf->buffer);
return(ret);
}
/**
* Shrink the input buffer.
*
* @param ctxt an XML parser context
*/
void
xmlParserShrink(xmlParserCtxt *ctxt) {
xmlParserInputPtr in = ctxt->input;
xmlParserInputBufferPtr buf = in->buf;
size_t used, res;
if (buf == NULL)
return;
used = in->cur - in->base;
if (used > LINE_LEN) {
res = xmlBufShrink(buf->buffer, used - LINE_LEN);
if (res > 0) {
used -= res;
xmlSaturatedAddSizeT(&in->consumed, res);
}
xmlBufUpdateInput(buf->buffer, in, used);
}
}
/**
* This function removes used input for the parser.
*
* @deprecated Don't use.
*
* @param in an XML parser input
*/
void
xmlParserInputShrink(xmlParserInput *in) {
size_t used;
size_t ret;
if (in == NULL) return;
if (in->buf == NULL) return;
if (in->base == NULL) return;
if (in->cur == NULL) return;
if (in->buf->buffer == NULL) return;
used = in->cur - in->base;
if (used > LINE_LEN) {
ret = xmlBufShrink(in->buf->buffer, used - LINE_LEN);
if (ret > 0) {
used -= ret;
xmlSaturatedAddSizeT(&in->consumed, ret);
}
xmlBufUpdateInput(in->buf->buffer, in, used);
}
}
/************************************************************************
* *
* UTF8 character input and related functions *
* *
************************************************************************/
/**
* Skip to the next char input char.
*
* @deprecated Internal function, do not use.
*
* @param ctxt the XML parser context
*/
void
xmlNextChar(xmlParserCtxt *ctxt)
{
const unsigned char *cur;
size_t avail;
int c;
if ((ctxt == NULL) || (ctxt->input == NULL))
return;
avail = ctxt->input->end - ctxt->input->cur;
if (avail < INPUT_CHUNK) {
xmlParserGrow(ctxt);
if (ctxt->input->cur >= ctxt->input->end)
return;
avail = ctxt->input->end - ctxt->input->cur;
}
cur = ctxt->input->cur;
c = *cur;
if (c < 0x80) {
if (c == '\n') {
ctxt->input->cur++;
ctxt->input->line++;
ctxt->input->col = 1;
} else if (c == '\r') {
/*
* 2.11 End-of-Line Handling
* the literal two-character sequence "#xD#xA" or a standalone
* literal #xD, an XML processor must pass to the application
* the single character #xA.
*/
ctxt->input->cur += ((cur[1] == '\n') ? 2 : 1);
ctxt->input->line++;
ctxt->input->col = 1;
return;
} else {
ctxt->input->cur++;
ctxt->input->col++;
}
} else {
ctxt->input->col++;
if ((avail < 2) || (cur[1] & 0xc0) != 0x80)
goto encoding_error;
if (c < 0xe0) {
/* 2-byte code */
if (c < 0xc2)
goto encoding_error;
ctxt->input->cur += 2;
} else {
unsigned int val = (c << 8) | cur[1];
if ((avail < 3) || (cur[2] & 0xc0) != 0x80)
goto encoding_error;
if (c < 0xf0) {
/* 3-byte code */
if ((val < 0xe0a0) || ((val >= 0xeda0) && (val < 0xee00)))
goto encoding_error;
ctxt->input->cur += 3;
} else {
if ((avail < 4) || ((cur[3] & 0xc0) != 0x80))
goto encoding_error;
/* 4-byte code */
if ((val < 0xf090) || (val >= 0xf490))
goto encoding_error;
ctxt->input->cur += 4;
}
}
}
return;
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;
}
ctxt->input->cur++;
}
/**
* The current char value, if using UTF-8 this may actually span multiple
* bytes in the input buffer. Implement the end of line normalization:
*
* @deprecated Internal function, do not use.
*
* 2.11 End-of-Line Handling
*
* Wherever an external parsed entity or the literal entity value
* of an internal parsed entity contains either the literal two-character
* sequence "#xD#xA" or a standalone literal \#xD, an XML processor
* must pass to the application the single character \#xA.
* This behavior can conveniently be produced by normalizing all
* line breaks to \#xA on input, before parsing.)
*
* @param ctxt the XML parser context
* @param len pointer to the length of the char read
* @returns the current char value and its length
*/
int
xmlCurrentChar(xmlParserCtxt *ctxt, int *len) {
const unsigned char *cur;
size_t avail;
int c;
if ((ctxt == NULL) || (len == NULL) || (ctxt->input == NULL)) return(0);
avail = ctxt->input->end - ctxt->input->cur;
if (avail < INPUT_CHUNK) {
xmlParserGrow(ctxt);
avail = ctxt->input->end - ctxt->input->cur;
}
cur = ctxt->input->cur;
c = *cur;
if (c < 0x80) {
/* 1-byte code */
if (c < 0x20) {
/*
* 2.11 End-of-Line Handling
* the literal two-character sequence "#xD#xA" or a standalone
* literal #xD, an XML processor must pass to the application
* the single character #xA.
*/
if (c == '\r') {
/*
* TODO: This function shouldn't change the 'cur' pointer
* as side effect, but the NEXTL macro in parser.c relies
* on this behavior when incrementing line numbers.
*/
if (cur[1] == '\n')
ctxt->input->cur++;
*len = 1;
c = '\n';
} else if (c == 0) {
if (ctxt->input->cur >= ctxt->input->end) {
*len = 0;
} else {
*len = 1;
/*
* TODO: Null bytes should be handled by callers,
* but this can be tricky.
*/
xmlFatalErr(ctxt, XML_ERR_INVALID_CHAR,
"Char 0x0 out of allowed range\n");
}
} else {
*len = 1;
}
} else {
*len = 1;
}
return(c);
} else {
int val;
if (avail < 2)
goto incomplete_sequence;
if ((cur[1] & 0xc0) != 0x80)
goto encoding_error;
if (c < 0xe0) {
/* 2-byte code */
if (c < 0xc2)
goto encoding_error;
val = (c & 0x1f) << 6;
val |= cur[1] & 0x3f;
*len = 2;
} else {
if (avail < 3)
goto incomplete_sequence;
if ((cur[2] & 0xc0) != 0x80)
goto encoding_error;