2 * parserInternals.c : Internal routines (and obsolete ones) needed for the
3 * XML and HTML parsers.
5 * See Copyright for the status of this software.
13 #if defined(_WIN32) && !defined (__CYGWIN__)
14 #define XML_DIR_SEP '\\'
16 #define XML_DIR_SEP '/'
26 #ifdef HAVE_SYS_STAT_H
35 #ifdef LIBXML_ZLIB_ENABLED
39 #include <libxml/xmlmemory.h>
40 #include <libxml/tree.h>
41 #include <libxml/parser.h>
42 #include <libxml/parserInternals.h>
43 #include <libxml/valid.h>
44 #include <libxml/entities.h>
45 #include <libxml/xmlerror.h>
46 #include <libxml/encoding.h>
47 #include <libxml/valid.h>
48 #include <libxml/xmlIO.h>
49 #include <libxml/uri.h>
50 #include <libxml/dict.h>
51 #include <libxml/SAX.h>
52 #ifdef LIBXML_CATALOG_ENABLED
53 #include <libxml/catalog.h>
55 #include <libxml/globals.h>
56 #include <libxml/chvalid.h>
58 #define CUR(ctxt) ctxt->input->cur
59 #define END(ctxt) ctxt->input->end
60 #define VALID_CTXT(ctxt) (CUR(ctxt) <= END(ctxt))
66 * Various global defaults for parsing
71 * @version: the include version number
73 * check the compiled lib version against the include one.
74 * This can warn or immediately kill the application
77 xmlCheckVersion(int version
) {
78 int myversion
= (int) LIBXML_VERSION
;
82 if ((myversion
/ 10000) != (version
/ 10000)) {
83 xmlGenericError(xmlGenericErrorContext
,
84 "Fatal: program compiled against libxml %d using libxml %d\n",
85 (version
/ 10000), (myversion
/ 10000));
87 "Fatal: program compiled against libxml %d using libxml %d\n",
88 (version
/ 10000), (myversion
/ 10000));
90 if ((myversion
/ 100) < (version
/ 100)) {
91 xmlGenericError(xmlGenericErrorContext
,
92 "Warning: program compiled against libxml %d using older %d\n",
93 (version
/ 100), (myversion
/ 100));
98 /************************************************************************
100 * Some factorized error routines *
102 ************************************************************************/
107 * @ctxt: an XML parser context
108 * @extra: extra information
110 * Handle a redefinition of attribute error
113 xmlErrMemory(xmlParserCtxtPtr ctxt
, const char *extra
)
115 if ((ctxt
!= NULL
) && (ctxt
->disableSAX
!= 0) &&
116 (ctxt
->instate
== XML_PARSER_EOF
))
119 ctxt
->errNo
= XML_ERR_NO_MEMORY
;
120 ctxt
->instate
= XML_PARSER_EOF
;
121 ctxt
->disableSAX
= 1;
124 __xmlRaiseError(NULL
, NULL
, NULL
, ctxt
, NULL
, XML_FROM_PARSER
,
125 XML_ERR_NO_MEMORY
, XML_ERR_FATAL
, NULL
, 0, extra
,
127 "Memory allocation failed : %s\n", extra
);
129 __xmlRaiseError(NULL
, NULL
, NULL
, ctxt
, NULL
, XML_FROM_PARSER
,
130 XML_ERR_NO_MEMORY
, XML_ERR_FATAL
, NULL
, 0, NULL
,
131 NULL
, NULL
, 0, 0, "Memory allocation failed\n");
136 * @ctxt: an XML parser context
137 * @xmlerr: the error number
138 * @msg: the error message
139 * @str1: an string info
140 * @str2: an string info
142 * Handle an encoding error
145 __xmlErrEncoding(xmlParserCtxtPtr ctxt
, xmlParserErrors xmlerr
,
146 const char *msg
, const xmlChar
* str1
, const xmlChar
* str2
)
148 if ((ctxt
!= NULL
) && (ctxt
->disableSAX
!= 0) &&
149 (ctxt
->instate
== XML_PARSER_EOF
))
152 ctxt
->errNo
= xmlerr
;
153 __xmlRaiseError(NULL
, NULL
, NULL
,
154 ctxt
, NULL
, XML_FROM_PARSER
, xmlerr
, XML_ERR_FATAL
,
155 NULL
, 0, (const char *) str1
, (const char *) str2
,
156 NULL
, 0, 0, msg
, str1
, str2
);
158 ctxt
->wellFormed
= 0;
159 if (ctxt
->recovery
== 0)
160 ctxt
->disableSAX
= 1;
166 * @ctxt: an XML parser context
167 * @msg: the error message
168 * @str: error information
170 * Handle an internal error
172 static void LIBXML_ATTR_FORMAT(2,0)
173 xmlErrInternal(xmlParserCtxtPtr ctxt
, const char *msg
, const xmlChar
* str
)
175 if ((ctxt
!= NULL
) && (ctxt
->disableSAX
!= 0) &&
176 (ctxt
->instate
== XML_PARSER_EOF
))
179 ctxt
->errNo
= XML_ERR_INTERNAL_ERROR
;
180 __xmlRaiseError(NULL
, NULL
, NULL
,
181 ctxt
, NULL
, XML_FROM_PARSER
, XML_ERR_INTERNAL_ERROR
,
182 XML_ERR_FATAL
, NULL
, 0, (const char *) str
, NULL
, NULL
,
185 ctxt
->wellFormed
= 0;
186 if (ctxt
->recovery
== 0)
187 ctxt
->disableSAX
= 1;
193 * @ctxt: an XML parser context
194 * @error: the error number
195 * @msg: the error message
196 * @val: an integer value
200 static void LIBXML_ATTR_FORMAT(3,0)
201 xmlErrEncodingInt(xmlParserCtxtPtr ctxt
, xmlParserErrors error
,
202 const char *msg
, int val
)
204 if ((ctxt
!= NULL
) && (ctxt
->disableSAX
!= 0) &&
205 (ctxt
->instate
== XML_PARSER_EOF
))
209 __xmlRaiseError(NULL
, NULL
, NULL
,
210 ctxt
, NULL
, XML_FROM_PARSER
, error
, XML_ERR_FATAL
,
211 NULL
, 0, NULL
, NULL
, NULL
, val
, 0, msg
, val
);
213 ctxt
->wellFormed
= 0;
214 if (ctxt
->recovery
== 0)
215 ctxt
->disableSAX
= 1;
221 * @c: an unicode character (int)
223 * Check whether the character is allowed by the production
224 * [84] Letter ::= BaseChar | Ideographic
226 * Returns 0 if not, non-zero otherwise
230 return(IS_BASECHAR(c
) || IS_IDEOGRAPHIC(c
));
233 /************************************************************************
235 * Input handling functions for progressive parsing *
237 ************************************************************************/
239 /* #define DEBUG_INPUT */
240 /* #define DEBUG_STACK */
241 /* #define DEBUG_PUSH */
244 /* we need to keep enough input to show errors in context */
248 #define CHECK_BUFFER(in) check_buffer(in)
251 void check_buffer(xmlParserInputPtr in
) {
252 if (in
->base
!= xmlBufContent(in
->buf
->buffer
)) {
253 xmlGenericError(xmlGenericErrorContext
,
254 "xmlParserInput: base mismatch problem\n");
256 if (in
->cur
< in
->base
) {
257 xmlGenericError(xmlGenericErrorContext
,
258 "xmlParserInput: cur < base problem\n");
260 if (in
->cur
> in
->base
+ xmlBufUse(in
->buf
->buffer
)) {
261 xmlGenericError(xmlGenericErrorContext
,
262 "xmlParserInput: cur > base + use problem\n");
264 xmlGenericError(xmlGenericErrorContext
,"buffer %x : content %x, cur %d, use %d\n",
265 (int) in
, (int) xmlBufContent(in
->buf
->buffer
), in
->cur
- in
->base
,
266 xmlBufUse(in
->buf
->buffer
));
270 #define CHECK_BUFFER(in)
275 * xmlParserInputRead:
276 * @in: an XML parser input
277 * @len: an indicative size for the lookahead
279 * This function was internal and is deprecated.
281 * Returns -1 as this is an error to use it.
284 xmlParserInputRead(xmlParserInputPtr in ATTRIBUTE_UNUSED
, int len ATTRIBUTE_UNUSED
) {
289 * xmlParserInputGrow:
290 * @in: an XML parser input
291 * @len: an indicative size for the lookahead
293 * This function increase the input for the parser. It tries to
294 * preserve pointers to the input buffer, and keep already read data
296 * Returns the amount of char read, or -1 in case of error, 0 indicate the
300 xmlParserInputGrow(xmlParserInputPtr in
, int len
) {
303 const xmlChar
*content
;
305 if ((in
== NULL
) || (len
< 0)) return(-1);
307 xmlGenericError(xmlGenericErrorContext
, "Grow\n");
309 if (in
->buf
== NULL
) return(-1);
310 if (in
->base
== NULL
) return(-1);
311 if (in
->cur
== NULL
) return(-1);
312 if (in
->buf
->buffer
== NULL
) return(-1);
316 indx
= in
->cur
- in
->base
;
317 if (xmlBufUse(in
->buf
->buffer
) > (unsigned int) indx
+ INPUT_CHUNK
) {
323 if (in
->buf
->readcallback
!= NULL
) {
324 ret
= xmlParserInputBufferGrow(in
->buf
, len
);
329 * NOTE : in->base may be a "dangling" i.e. freed pointer in this
330 * block, but we use it really as an integer to do some
331 * pointer arithmetic. Insure will raise it as a bug but in
332 * that specific case, that's not !
335 content
= xmlBufContent(in
->buf
->buffer
);
336 if (in
->base
!= content
) {
338 * the buffer has been reallocated
340 indx
= in
->cur
- in
->base
;
342 in
->cur
= &content
[indx
];
344 in
->end
= xmlBufEnd(in
->buf
->buffer
);
352 * xmlParserInputShrink:
353 * @in: an XML parser input
355 * This function removes used input for the parser.
358 xmlParserInputShrink(xmlParserInputPtr in
) {
362 const xmlChar
*content
;
365 xmlGenericError(xmlGenericErrorContext
, "Shrink\n");
367 if (in
== NULL
) return;
368 if (in
->buf
== NULL
) return;
369 if (in
->base
== NULL
) return;
370 if (in
->cur
== NULL
) return;
371 if (in
->buf
->buffer
== NULL
) return;
375 used
= in
->cur
- xmlBufContent(in
->buf
->buffer
);
377 * Do not shrink on large buffers whose only a tiny fraction
380 if (used
> INPUT_CHUNK
) {
381 ret
= xmlBufShrink(in
->buf
->buffer
, used
- LINE_LEN
);
386 in
->end
= xmlBufEnd(in
->buf
->buffer
);
391 if (xmlBufUse(in
->buf
->buffer
) > INPUT_CHUNK
) {
394 xmlParserInputBufferRead(in
->buf
, 2 * INPUT_CHUNK
);
395 content
= xmlBufContent(in
->buf
->buffer
);
396 if (in
->base
!= content
) {
398 * the buffer has been reallocated
400 indx
= in
->cur
- in
->base
;
402 in
->cur
= &content
[indx
];
404 in
->end
= xmlBufEnd(in
->buf
->buffer
);
409 /************************************************************************
411 * UTF8 character input and related functions *
413 ************************************************************************/
417 * @ctxt: the XML parser context
419 * Skip to the next char input char.
423 xmlNextChar(xmlParserCtxtPtr ctxt
)
425 if ((ctxt
== NULL
) || (ctxt
->instate
== XML_PARSER_EOF
) ||
426 (ctxt
->input
== NULL
))
429 if (!(VALID_CTXT(ctxt
))) {
430 xmlErrInternal(ctxt
, "Parser input data memory error\n", NULL
);
431 ctxt
->errNo
= XML_ERR_INTERNAL_ERROR
;
436 if ((*ctxt
->input
->cur
== 0) &&
437 (xmlParserInputGrow(ctxt
->input
, INPUT_CHUNK
) <= 0)) {
441 if (ctxt
->charset
== XML_CHAR_ENCODING_UTF8
) {
442 const unsigned char *cur
;
446 * 2.11 End-of-Line Handling
447 * the literal two-character sequence "#xD#xA" or a standalone
448 * literal #xD, an XML processor must pass to the application
449 * the single character #xA.
451 if (*(ctxt
->input
->cur
) == '\n') {
452 ctxt
->input
->line
++; ctxt
->input
->col
= 1;
457 * We are supposed to handle UTF8, check it's valid
458 * From rfc2044: encoding of the Unicode values on UTF-8:
460 * UCS-4 range (hex.) UTF-8 octet sequence (binary)
461 * 0000 0000-0000 007F 0xxxxxxx
462 * 0000 0080-0000 07FF 110xxxxx 10xxxxxx
463 * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx
465 * Check for the 0x110000 limit too
467 cur
= ctxt
->input
->cur
;
474 xmlParserInputGrow(ctxt
->input
, INPUT_CHUNK
);
475 cur
= ctxt
->input
->cur
;
477 if ((cur
[1] & 0xc0) != 0x80)
479 if ((c
& 0xe0) == 0xe0) {
483 xmlParserInputGrow(ctxt
->input
, INPUT_CHUNK
);
484 cur
= ctxt
->input
->cur
;
486 if ((cur
[2] & 0xc0) != 0x80)
488 if ((c
& 0xf0) == 0xf0) {
490 xmlParserInputGrow(ctxt
->input
, INPUT_CHUNK
);
491 cur
= ctxt
->input
->cur
;
493 if (((c
& 0xf8) != 0xf0) ||
494 ((cur
[3] & 0xc0) != 0x80))
497 ctxt
->input
->cur
+= 4;
498 val
= (cur
[0] & 0x7) << 18;
499 val
|= (cur
[1] & 0x3f) << 12;
500 val
|= (cur
[2] & 0x3f) << 6;
501 val
|= cur
[3] & 0x3f;
504 ctxt
->input
->cur
+= 3;
505 val
= (cur
[0] & 0xf) << 12;
506 val
|= (cur
[1] & 0x3f) << 6;
507 val
|= cur
[2] & 0x3f;
509 if (((val
> 0xd7ff) && (val
< 0xe000)) ||
510 ((val
> 0xfffd) && (val
< 0x10000)) ||
512 xmlErrEncodingInt(ctxt
, XML_ERR_INVALID_CHAR
,
513 "Char 0x%X out of allowed range\n",
518 ctxt
->input
->cur
+= 2;
524 * Assume it's a fixed length encoding (1) with
525 * a compatible encoding for the ASCII set, since
526 * XML constructs only use < 128 chars
529 if (*(ctxt
->input
->cur
) == '\n') {
530 ctxt
->input
->line
++; ctxt
->input
->col
= 1;
535 if (*ctxt
->input
->cur
== 0)
536 xmlParserInputGrow(ctxt
->input
, INPUT_CHUNK
);
540 * If we detect an UTF8 error that probably mean that the
541 * input encoding didn't get properly advertised in the
542 * declaration header. Report the error and switch the encoding
543 * to ISO-Latin-1 (if you don't like this policy, just declare the
546 if ((ctxt
== NULL
) || (ctxt
->input
== NULL
) ||
547 (ctxt
->input
->end
- ctxt
->input
->cur
< 4)) {
548 __xmlErrEncoding(ctxt
, XML_ERR_INVALID_CHAR
,
549 "Input is not proper UTF-8, indicate encoding !\n",
554 snprintf(buffer
, 149, "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n",
555 ctxt
->input
->cur
[0], ctxt
->input
->cur
[1],
556 ctxt
->input
->cur
[2], ctxt
->input
->cur
[3]);
557 __xmlErrEncoding(ctxt
, XML_ERR_INVALID_CHAR
,
558 "Input is not proper UTF-8, indicate encoding !\n%s",
559 BAD_CAST buffer
, NULL
);
561 ctxt
->charset
= XML_CHAR_ENCODING_8859_1
;
568 * @ctxt: the XML parser context
569 * @len: pointer to the length of the char read
571 * The current char value, if using UTF-8 this may actually span multiple
572 * bytes in the input buffer. Implement the end of line normalization:
573 * 2.11 End-of-Line Handling
574 * Wherever an external parsed entity or the literal entity value
575 * of an internal parsed entity contains either the literal two-character
576 * sequence "#xD#xA" or a standalone literal #xD, an XML processor
577 * must pass to the application the single character #xA.
578 * This behavior can conveniently be produced by normalizing all
579 * line breaks to #xA on input, before parsing.)
581 * Returns the current char value and its length
585 xmlCurrentChar(xmlParserCtxtPtr ctxt
, int *len
) {
586 if ((ctxt
== NULL
) || (len
== NULL
) || (ctxt
->input
== NULL
)) return(0);
587 if (ctxt
->instate
== XML_PARSER_EOF
)
590 if ((*ctxt
->input
->cur
>= 0x20) && (*ctxt
->input
->cur
<= 0x7F)) {
592 return((int) *ctxt
->input
->cur
);
594 if (ctxt
->charset
== XML_CHAR_ENCODING_UTF8
) {
596 * We are supposed to handle UTF8, check it's valid
597 * From rfc2044: encoding of the Unicode values on UTF-8:
599 * UCS-4 range (hex.) UTF-8 octet sequence (binary)
600 * 0000 0000-0000 007F 0xxxxxxx
601 * 0000 0080-0000 07FF 110xxxxx 10xxxxxx
602 * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx
604 * Check for the 0x110000 limit too
606 const unsigned char *cur
= ctxt
->input
->cur
;
612 if (((c
& 0x40) == 0) || (c
== 0xC0))
615 xmlParserInputGrow(ctxt
->input
, INPUT_CHUNK
);
616 cur
= ctxt
->input
->cur
;
618 if ((cur
[1] & 0xc0) != 0x80)
620 if ((c
& 0xe0) == 0xe0) {
622 xmlParserInputGrow(ctxt
->input
, INPUT_CHUNK
);
623 cur
= ctxt
->input
->cur
;
625 if ((cur
[2] & 0xc0) != 0x80)
627 if ((c
& 0xf0) == 0xf0) {
629 xmlParserInputGrow(ctxt
->input
, INPUT_CHUNK
);
630 cur
= ctxt
->input
->cur
;
632 if (((c
& 0xf8) != 0xf0) ||
633 ((cur
[3] & 0xc0) != 0x80))
637 val
= (cur
[0] & 0x7) << 18;
638 val
|= (cur
[1] & 0x3f) << 12;
639 val
|= (cur
[2] & 0x3f) << 6;
640 val
|= cur
[3] & 0x3f;
646 val
= (cur
[0] & 0xf) << 12;
647 val
|= (cur
[1] & 0x3f) << 6;
648 val
|= cur
[2] & 0x3f;
655 val
= (cur
[0] & 0x1f) << 6;
656 val
|= cur
[1] & 0x3f;
661 xmlErrEncodingInt(ctxt
, XML_ERR_INVALID_CHAR
,
662 "Char 0x%X out of allowed range\n", val
);
668 if (*ctxt
->input
->cur
== 0)
669 xmlParserInputGrow(ctxt
->input
, INPUT_CHUNK
);
670 if ((*ctxt
->input
->cur
== 0) &&
671 (ctxt
->input
->end
> ctxt
->input
->cur
)) {
672 xmlErrEncodingInt(ctxt
, XML_ERR_INVALID_CHAR
,
673 "Char 0x0 out of allowed range\n", 0);
675 if (*ctxt
->input
->cur
== 0xD) {
676 if (ctxt
->input
->cur
[1] == 0xA) {
681 return((int) *ctxt
->input
->cur
);
685 * Assume it's a fixed length encoding (1) with
686 * a compatible encoding for the ASCII set, since
687 * XML constructs only use < 128 chars
690 if (*ctxt
->input
->cur
== 0xD) {
691 if (ctxt
->input
->cur
[1] == 0xA) {
696 return((int) *ctxt
->input
->cur
);
699 * An encoding problem may arise from a truncated input buffer
700 * splitting a character in the middle. In that case do not raise
701 * an error but return 0 to indicate an end of stream problem
703 if (ctxt
->input
->end
- ctxt
->input
->cur
< 4) {
709 * If we detect an UTF8 error that probably mean that the
710 * input encoding didn't get properly advertised in the
711 * declaration header. Report the error and switch the encoding
712 * to ISO-Latin-1 (if you don't like this policy, just declare the
718 snprintf(&buffer
[0], 149, "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n",
719 ctxt
->input
->cur
[0], ctxt
->input
->cur
[1],
720 ctxt
->input
->cur
[2], ctxt
->input
->cur
[3]);
721 __xmlErrEncoding(ctxt
, XML_ERR_INVALID_CHAR
,
722 "Input is not proper UTF-8, indicate encoding !\n%s",
723 BAD_CAST buffer
, NULL
);
725 ctxt
->charset
= XML_CHAR_ENCODING_8859_1
;
727 return((int) *ctxt
->input
->cur
);
731 * xmlStringCurrentChar:
732 * @ctxt: the XML parser context
733 * @cur: pointer to the beginning of the char
734 * @len: pointer to the length of the char read
736 * The current char value, if using UTF-8 this may actually span multiple
737 * bytes in the input buffer.
739 * Returns the current char value and its length
743 xmlStringCurrentChar(xmlParserCtxtPtr ctxt
, const xmlChar
* cur
, int *len
)
745 if ((len
== NULL
) || (cur
== NULL
)) return(0);
746 if ((ctxt
== NULL
) || (ctxt
->charset
== XML_CHAR_ENCODING_UTF8
)) {
748 * We are supposed to handle UTF8, check it's valid
749 * From rfc2044: encoding of the Unicode values on UTF-8:
751 * UCS-4 range (hex.) UTF-8 octet sequence (binary)
752 * 0000 0000-0000 007F 0xxxxxxx
753 * 0000 0080-0000 07FF 110xxxxx 10xxxxxx
754 * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx
756 * Check for the 0x110000 limit too
763 if ((cur
[1] & 0xc0) != 0x80)
765 if ((c
& 0xe0) == 0xe0) {
767 if ((cur
[2] & 0xc0) != 0x80)
769 if ((c
& 0xf0) == 0xf0) {
770 if (((c
& 0xf8) != 0xf0) || ((cur
[3] & 0xc0) != 0x80))
774 val
= (cur
[0] & 0x7) << 18;
775 val
|= (cur
[1] & 0x3f) << 12;
776 val
|= (cur
[2] & 0x3f) << 6;
777 val
|= cur
[3] & 0x3f;
781 val
= (cur
[0] & 0xf) << 12;
782 val
|= (cur
[1] & 0x3f) << 6;
783 val
|= cur
[2] & 0x3f;
788 val
= (cur
[0] & 0x1f) << 6;
789 val
|= cur
[1] & 0x3f;
792 xmlErrEncodingInt(ctxt
, XML_ERR_INVALID_CHAR
,
793 "Char 0x%X out of allowed range\n", val
);
803 * Assume it's a fixed length encoding (1) with
804 * a compatible encoding for the ASCII set, since
805 * XML constructs only use < 128 chars
812 * An encoding problem may arise from a truncated input buffer
813 * splitting a character in the middle. In that case do not raise
814 * an error but return 0 to indicate an end of stream problem
816 if ((ctxt
== NULL
) || (ctxt
->input
== NULL
) ||
817 (ctxt
->input
->end
- ctxt
->input
->cur
< 4)) {
822 * If we detect an UTF8 error that probably mean that the
823 * input encoding didn't get properly advertised in the
824 * declaration header. Report the error and switch the encoding
825 * to ISO-Latin-1 (if you don't like this policy, just declare the
831 snprintf(buffer
, 149, "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n",
832 ctxt
->input
->cur
[0], ctxt
->input
->cur
[1],
833 ctxt
->input
->cur
[2], ctxt
->input
->cur
[3]);
834 __xmlErrEncoding(ctxt
, XML_ERR_INVALID_CHAR
,
835 "Input is not proper UTF-8, indicate encoding !\n%s",
836 BAD_CAST buffer
, NULL
);
843 * xmlCopyCharMultiByte:
844 * @out: pointer to an array of xmlChar
845 * @val: the char value
847 * append the char value in the array
849 * Returns the number of xmlChar written
852 xmlCopyCharMultiByte(xmlChar
*out
, int val
) {
853 if (out
== NULL
) return(0);
855 * We are supposed to handle UTF8, check it's valid
856 * From rfc2044: encoding of the Unicode values on UTF-8:
858 * UCS-4 range (hex.) UTF-8 octet sequence (binary)
859 * 0000 0000-0000 007F 0xxxxxxx
860 * 0000 0080-0000 07FF 110xxxxx 10xxxxxx
861 * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx
864 xmlChar
*savedout
= out
;
866 if (val
< 0x800) { *out
++= (val
>> 6) | 0xC0; bits
= 0; }
867 else if (val
< 0x10000) { *out
++= (val
>> 12) | 0xE0; bits
= 6;}
868 else if (val
< 0x110000) { *out
++= (val
>> 18) | 0xF0; bits
= 12; }
870 xmlErrEncodingInt(NULL
, XML_ERR_INVALID_CHAR
,
871 "Internal error, xmlCopyCharMultiByte 0x%X out of bound\n",
875 for ( ; bits
>= 0; bits
-= 6)
876 *out
++= ((val
>> bits
) & 0x3F) | 0x80 ;
877 return (out
- savedout
);
879 *out
= (xmlChar
) val
;
885 * @len: Ignored, compatibility
886 * @out: pointer to an array of xmlChar
887 * @val: the char value
889 * append the char value in the array
891 * Returns the number of xmlChar written
895 xmlCopyChar(int len ATTRIBUTE_UNUSED
, xmlChar
*out
, int val
) {
896 if (out
== NULL
) return(0);
897 /* the len parameter is ignored */
899 return(xmlCopyCharMultiByte (out
, val
));
901 *out
= (xmlChar
) val
;
905 /************************************************************************
907 * Commodity functions to switch encodings *
909 ************************************************************************/
912 xmlSwitchToEncodingInt(xmlParserCtxtPtr ctxt
,
913 xmlCharEncodingHandlerPtr handler
, int len
);
915 xmlSwitchInputEncodingInt(xmlParserCtxtPtr ctxt
, xmlParserInputPtr input
,
916 xmlCharEncodingHandlerPtr handler
, int len
);
919 * @ctxt: the parser context
920 * @enc: the encoding value (number)
922 * change the input functions when discovering the character encoding
925 * Returns 0 in case of success, -1 otherwise
928 xmlSwitchEncoding(xmlParserCtxtPtr ctxt
, xmlCharEncoding enc
)
930 xmlCharEncodingHandlerPtr handler
;
934 if (ctxt
== NULL
) return(-1);
936 case XML_CHAR_ENCODING_ERROR
:
937 __xmlErrEncoding(ctxt
, XML_ERR_UNKNOWN_ENCODING
,
938 "encoding unknown\n", NULL
, NULL
);
940 case XML_CHAR_ENCODING_NONE
:
941 /* let's assume it's UTF-8 without the XML decl */
942 ctxt
->charset
= XML_CHAR_ENCODING_UTF8
;
944 case XML_CHAR_ENCODING_UTF8
:
945 /* default encoding, no conversion should be needed */
946 ctxt
->charset
= XML_CHAR_ENCODING_UTF8
;
949 * Errata on XML-1.0 June 20 2001
950 * Specific handling of the Byte Order Mark for
953 if ((ctxt
->input
!= NULL
) &&
954 (ctxt
->input
->cur
[0] == 0xEF) &&
955 (ctxt
->input
->cur
[1] == 0xBB) &&
956 (ctxt
->input
->cur
[2] == 0xBF)) {
957 ctxt
->input
->cur
+= 3;
960 case XML_CHAR_ENCODING_UTF16LE
:
961 case XML_CHAR_ENCODING_UTF16BE
:
962 /*The raw input characters are encoded
963 *in UTF-16. As we expect this function
964 *to be called after xmlCharEncInFunc, we expect
965 *ctxt->input->cur to contain UTF-8 encoded characters.
966 *So the raw UTF16 Byte Order Mark
967 *has also been converted into
968 *an UTF-8 BOM. Let's skip that BOM.
970 if ((ctxt
->input
!= NULL
) && (ctxt
->input
->cur
!= NULL
) &&
971 (ctxt
->input
->cur
[0] == 0xEF) &&
972 (ctxt
->input
->cur
[1] == 0xBB) &&
973 (ctxt
->input
->cur
[2] == 0xBF)) {
974 ctxt
->input
->cur
+= 3;
978 case XML_CHAR_ENCODING_UCS2
:
981 case XML_CHAR_ENCODING_UCS4BE
:
982 case XML_CHAR_ENCODING_UCS4LE
:
983 case XML_CHAR_ENCODING_UCS4_2143
:
984 case XML_CHAR_ENCODING_UCS4_3412
:
987 case XML_CHAR_ENCODING_EBCDIC
:
988 case XML_CHAR_ENCODING_8859_1
:
989 case XML_CHAR_ENCODING_8859_2
:
990 case XML_CHAR_ENCODING_8859_3
:
991 case XML_CHAR_ENCODING_8859_4
:
992 case XML_CHAR_ENCODING_8859_5
:
993 case XML_CHAR_ENCODING_8859_6
:
994 case XML_CHAR_ENCODING_8859_7
:
995 case XML_CHAR_ENCODING_8859_8
:
996 case XML_CHAR_ENCODING_8859_9
:
997 case XML_CHAR_ENCODING_ASCII
:
998 case XML_CHAR_ENCODING_2022_JP
:
999 case XML_CHAR_ENCODING_SHIFT_JIS
:
1000 case XML_CHAR_ENCODING_EUC_JP
:
1004 handler
= xmlGetCharEncodingHandler(enc
);
1005 if (handler
== NULL
) {
1010 case XML_CHAR_ENCODING_ASCII
:
1011 /* default encoding, no conversion should be needed */
1012 ctxt
->charset
= XML_CHAR_ENCODING_UTF8
;
1014 case XML_CHAR_ENCODING_UTF16LE
:
1016 case XML_CHAR_ENCODING_UTF16BE
:
1018 case XML_CHAR_ENCODING_UCS4LE
:
1019 __xmlErrEncoding(ctxt
, XML_ERR_UNSUPPORTED_ENCODING
,
1020 "encoding not supported %s\n",
1021 BAD_CAST
"USC4 little endian", NULL
);
1023 case XML_CHAR_ENCODING_UCS4BE
:
1024 __xmlErrEncoding(ctxt
, XML_ERR_UNSUPPORTED_ENCODING
,
1025 "encoding not supported %s\n",
1026 BAD_CAST
"USC4 big endian", NULL
);
1028 case XML_CHAR_ENCODING_EBCDIC
:
1029 __xmlErrEncoding(ctxt
, XML_ERR_UNSUPPORTED_ENCODING
,
1030 "encoding not supported %s\n",
1031 BAD_CAST
"EBCDIC", NULL
);
1033 case XML_CHAR_ENCODING_UCS4_2143
:
1034 __xmlErrEncoding(ctxt
, XML_ERR_UNSUPPORTED_ENCODING
,
1035 "encoding not supported %s\n",
1036 BAD_CAST
"UCS4 2143", NULL
);
1038 case XML_CHAR_ENCODING_UCS4_3412
:
1039 __xmlErrEncoding(ctxt
, XML_ERR_UNSUPPORTED_ENCODING
,
1040 "encoding not supported %s\n",
1041 BAD_CAST
"UCS4 3412", NULL
);
1043 case XML_CHAR_ENCODING_UCS2
:
1044 __xmlErrEncoding(ctxt
, XML_ERR_UNSUPPORTED_ENCODING
,
1045 "encoding not supported %s\n",
1046 BAD_CAST
"UCS2", NULL
);
1048 case XML_CHAR_ENCODING_8859_1
:
1049 case XML_CHAR_ENCODING_8859_2
:
1050 case XML_CHAR_ENCODING_8859_3
:
1051 case XML_CHAR_ENCODING_8859_4
:
1052 case XML_CHAR_ENCODING_8859_5
:
1053 case XML_CHAR_ENCODING_8859_6
:
1054 case XML_CHAR_ENCODING_8859_7
:
1055 case XML_CHAR_ENCODING_8859_8
:
1056 case XML_CHAR_ENCODING_8859_9
:
1058 * We used to keep the internal content in the
1059 * document encoding however this turns being unmaintainable
1060 * So xmlGetCharEncodingHandler() will return non-null
1061 * values for this now.
1063 if ((ctxt
->inputNr
== 1) &&
1064 (ctxt
->encoding
== NULL
) &&
1065 (ctxt
->input
!= NULL
) &&
1066 (ctxt
->input
->encoding
!= NULL
)) {
1067 ctxt
->encoding
= xmlStrdup(ctxt
->input
->encoding
);
1069 ctxt
->charset
= enc
;
1071 case XML_CHAR_ENCODING_2022_JP
:
1072 __xmlErrEncoding(ctxt
, XML_ERR_UNSUPPORTED_ENCODING
,
1073 "encoding not supported %s\n",
1074 BAD_CAST
"ISO-2022-JP", NULL
);
1076 case XML_CHAR_ENCODING_SHIFT_JIS
:
1077 __xmlErrEncoding(ctxt
, XML_ERR_UNSUPPORTED_ENCODING
,
1078 "encoding not supported %s\n",
1079 BAD_CAST
"Shift_JIS", NULL
);
1081 case XML_CHAR_ENCODING_EUC_JP
:
1082 __xmlErrEncoding(ctxt
, XML_ERR_UNSUPPORTED_ENCODING
,
1083 "encoding not supported %s\n",
1084 BAD_CAST
"EUC-JP", NULL
);
1091 * TODO: We could recover from errors in external entities if we
1092 * didn't stop the parser. But most callers of this function don't
1093 * check the return value.
1095 if (handler
== NULL
) {
1096 xmlStopParser(ctxt
);
1099 ctxt
->charset
= XML_CHAR_ENCODING_UTF8
;
1100 ret
= xmlSwitchToEncodingInt(ctxt
, handler
, len
);
1101 if ((ret
< 0) || (ctxt
->errNo
== XML_I18N_CONV_FAILED
)) {
1103 * on encoding conversion errors, stop the parser
1105 xmlStopParser(ctxt
);
1106 ctxt
->errNo
= XML_I18N_CONV_FAILED
;
1112 * xmlSwitchInputEncoding:
1113 * @ctxt: the parser context
1114 * @input: the input stream
1115 * @handler: the encoding handler
1116 * @len: the number of bytes to convert for the first line or -1
1118 * change the input functions when discovering the character encoding
1119 * of a given entity.
1121 * Returns 0 in case of success, -1 otherwise
1124 xmlSwitchInputEncodingInt(xmlParserCtxtPtr ctxt
, xmlParserInputPtr input
,
1125 xmlCharEncodingHandlerPtr handler
, int len
)
1129 if (handler
== NULL
)
1133 if (input
->buf
!= NULL
) {
1134 if (input
->buf
->encoder
!= NULL
) {
1136 * Check in case the auto encoding detection triggered
1139 if (input
->buf
->encoder
== handler
)
1143 * "UTF-16" can be used for both LE and BE
1144 if ((!xmlStrncmp(BAD_CAST input->buf->encoder->name,
1145 BAD_CAST "UTF-16", 6)) &&
1146 (!xmlStrncmp(BAD_CAST handler->name,
1147 BAD_CAST "UTF-16", 6))) {
1153 * Note: this is a bit dangerous, but that's what it
1154 * takes to use nearly compatible signature for different
1157 * FIXME: Encoders might buffer partial byte sequences, so
1158 * this probably can't work. We should return an error and
1159 * make sure that callers never try to switch the encoding
1162 xmlCharEncCloseFunc(input
->buf
->encoder
);
1163 input
->buf
->encoder
= handler
;
1166 input
->buf
->encoder
= handler
;
1169 * Is there already some content down the pipe to convert ?
1171 if (xmlBufIsEmpty(input
->buf
->buffer
) == 0) {
1176 * Specific handling of the Byte Order Mark for
1179 if ((handler
->name
!= NULL
) &&
1180 (!strcmp(handler
->name
, "UTF-16LE") ||
1181 !strcmp(handler
->name
, "UTF-16")) &&
1182 (input
->cur
[0] == 0xFF) && (input
->cur
[1] == 0xFE)) {
1185 if ((handler
->name
!= NULL
) &&
1186 (!strcmp(handler
->name
, "UTF-16BE")) &&
1187 (input
->cur
[0] == 0xFE) && (input
->cur
[1] == 0xFF)) {
1191 * Errata on XML-1.0 June 20 2001
1192 * Specific handling of the Byte Order Mark for
1195 if ((handler
->name
!= NULL
) &&
1196 (!strcmp(handler
->name
, "UTF-8")) &&
1197 (input
->cur
[0] == 0xEF) &&
1198 (input
->cur
[1] == 0xBB) && (input
->cur
[2] == 0xBF)) {
1203 * Shrink the current input buffer.
1204 * Move it as the raw buffer and create a new input buffer
1206 processed
= input
->cur
- input
->base
;
1207 xmlBufShrink(input
->buf
->buffer
, processed
);
1208 input
->buf
->raw
= input
->buf
->buffer
;
1209 input
->buf
->buffer
= xmlBufCreate();
1210 input
->buf
->rawconsumed
= processed
;
1211 use
= xmlBufUse(input
->buf
->raw
);
1215 * convert as much as possible of the buffer
1217 nbchars
= xmlCharEncInput(input
->buf
, 1);
1220 * convert just enough to get
1221 * '<?xml version="1.0" encoding="xxx"?>'
1222 * parsed with the autodetected encoding
1223 * into the parser reading buffer.
1225 nbchars
= xmlCharEncFirstLineInput(input
->buf
, len
);
1227 xmlBufResetInput(input
->buf
->buffer
, input
);
1229 xmlErrInternal(ctxt
,
1230 "switching encoding: encoder error\n",
1234 input
->buf
->rawconsumed
+= use
- xmlBufUse(input
->buf
->raw
);
1237 } else if (input
->length
== 0) {
1239 * When parsing a static memory array one must know the
1240 * size to be able to convert the buffer.
1242 xmlErrInternal(ctxt
, "switching encoding : no input\n", NULL
);
1244 * Callers assume that the input buffer takes ownership of the
1245 * encoding handler. xmlCharEncCloseFunc frees unregistered
1246 * handlers and avoids a memory leak.
1248 xmlCharEncCloseFunc(handler
);
1252 * We should actually raise an error here, see issue #34.
1254 xmlCharEncCloseFunc(handler
);
1259 * xmlSwitchInputEncoding:
1260 * @ctxt: the parser context
1261 * @input: the input stream
1262 * @handler: the encoding handler
1264 * change the input functions when discovering the character encoding
1265 * of a given entity.
1267 * Returns 0 in case of success, -1 otherwise
1270 xmlSwitchInputEncoding(xmlParserCtxtPtr ctxt
, xmlParserInputPtr input
,
1271 xmlCharEncodingHandlerPtr handler
) {
1272 return(xmlSwitchInputEncodingInt(ctxt
, input
, handler
, -1));
1276 * xmlSwitchToEncodingInt:
1277 * @ctxt: the parser context
1278 * @handler: the encoding handler
1279 * @len: the length to convert or -1
1281 * change the input functions when discovering the character encoding
1282 * of a given entity, and convert only @len bytes of the output, this
1283 * is needed on auto detect to allows any declared encoding later to
1284 * convert the actual content after the xmlDecl
1286 * Returns 0 in case of success, -1 otherwise
1289 xmlSwitchToEncodingInt(xmlParserCtxtPtr ctxt
,
1290 xmlCharEncodingHandlerPtr handler
, int len
) {
1293 if (handler
!= NULL
) {
1294 if (ctxt
->input
!= NULL
) {
1295 ret
= xmlSwitchInputEncodingInt(ctxt
, ctxt
->input
, handler
, len
);
1297 xmlErrInternal(ctxt
, "xmlSwitchToEncoding : no input\n",
1302 * The parsing is now done in UTF8 natively
1304 ctxt
->charset
= XML_CHAR_ENCODING_UTF8
;
1311 * xmlSwitchToEncoding:
1312 * @ctxt: the parser context
1313 * @handler: the encoding handler
1315 * change the input functions when discovering the character encoding
1316 * of a given entity.
1318 * Returns 0 in case of success, -1 otherwise
1321 xmlSwitchToEncoding(xmlParserCtxtPtr ctxt
, xmlCharEncodingHandlerPtr handler
)
1323 return (xmlSwitchToEncodingInt(ctxt
, handler
, -1));
1326 /************************************************************************
1328 * Commodity functions to handle entities processing *
1330 ************************************************************************/
1333 * xmlFreeInputStream:
1334 * @input: an xmlParserInputPtr
1336 * Free up an input stream.
1339 xmlFreeInputStream(xmlParserInputPtr input
) {
1340 if (input
== NULL
) return;
1342 if (input
->filename
!= NULL
) xmlFree((char *) input
->filename
);
1343 if (input
->directory
!= NULL
) xmlFree((char *) input
->directory
);
1344 if (input
->encoding
!= NULL
) xmlFree((char *) input
->encoding
);
1345 if (input
->version
!= NULL
) xmlFree((char *) input
->version
);
1346 if ((input
->free
!= NULL
) && (input
->base
!= NULL
))
1347 input
->free((xmlChar
*) input
->base
);
1348 if (input
->buf
!= NULL
)
1349 xmlFreeParserInputBuffer(input
->buf
);
1354 * xmlNewInputStream:
1355 * @ctxt: an XML parser context
1357 * Create a new input stream structure.
1359 * Returns the new input stream or NULL
1362 xmlNewInputStream(xmlParserCtxtPtr ctxt
) {
1363 xmlParserInputPtr input
;
1365 input
= (xmlParserInputPtr
) xmlMalloc(sizeof(xmlParserInput
));
1366 if (input
== NULL
) {
1367 xmlErrMemory(ctxt
, "couldn't allocate a new input stream\n");
1370 memset(input
, 0, sizeof(xmlParserInput
));
1373 input
->standalone
= -1;
1376 * If the context is NULL the id cannot be initialized, but that
1377 * should not happen while parsing which is the situation where
1378 * the id is actually needed.
1381 input
->id
= ctxt
->input_id
++;
1387 * xmlNewIOInputStream:
1388 * @ctxt: an XML parser context
1389 * @input: an I/O Input
1390 * @enc: the charset encoding if known
1392 * Create a new input stream structure encapsulating the @input into
1393 * a stream suitable for the parser.
1395 * Returns the new input stream or NULL
1398 xmlNewIOInputStream(xmlParserCtxtPtr ctxt
, xmlParserInputBufferPtr input
,
1399 xmlCharEncoding enc
) {
1400 xmlParserInputPtr inputStream
;
1402 if (input
== NULL
) return(NULL
);
1403 if (xmlParserDebugEntities
)
1404 xmlGenericError(xmlGenericErrorContext
, "new input from I/O\n");
1405 inputStream
= xmlNewInputStream(ctxt
);
1406 if (inputStream
== NULL
) {
1409 inputStream
->filename
= NULL
;
1410 inputStream
->buf
= input
;
1411 xmlBufResetInput(inputStream
->buf
->buffer
, inputStream
);
1413 if (enc
!= XML_CHAR_ENCODING_NONE
) {
1414 xmlSwitchEncoding(ctxt
, enc
);
1417 return(inputStream
);
1421 * xmlNewEntityInputStream:
1422 * @ctxt: an XML parser context
1423 * @entity: an Entity pointer
1425 * Create a new input stream based on an xmlEntityPtr
1427 * Returns the new input stream or NULL
1430 xmlNewEntityInputStream(xmlParserCtxtPtr ctxt
, xmlEntityPtr entity
) {
1431 xmlParserInputPtr input
;
1433 if (entity
== NULL
) {
1434 xmlErrInternal(ctxt
, "xmlNewEntityInputStream entity = NULL\n",
1438 if (xmlParserDebugEntities
)
1439 xmlGenericError(xmlGenericErrorContext
,
1440 "new input from entity: %s\n", entity
->name
);
1441 if (entity
->content
== NULL
) {
1442 switch (entity
->etype
) {
1443 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY
:
1444 xmlErrInternal(ctxt
, "Cannot parse entity %s\n",
1447 case XML_EXTERNAL_GENERAL_PARSED_ENTITY
:
1448 case XML_EXTERNAL_PARAMETER_ENTITY
:
1449 return(xmlLoadExternalEntity((char *) entity
->URI
,
1450 (char *) entity
->ExternalID
, ctxt
));
1451 case XML_INTERNAL_GENERAL_ENTITY
:
1452 xmlErrInternal(ctxt
,
1453 "Internal entity %s without content !\n",
1456 case XML_INTERNAL_PARAMETER_ENTITY
:
1457 xmlErrInternal(ctxt
,
1458 "Internal parameter entity %s without content !\n",
1461 case XML_INTERNAL_PREDEFINED_ENTITY
:
1462 xmlErrInternal(ctxt
,
1463 "Predefined entity %s without content !\n",
1469 input
= xmlNewInputStream(ctxt
);
1470 if (input
== NULL
) {
1473 if (entity
->URI
!= NULL
)
1474 input
->filename
= (char *) xmlStrdup((xmlChar
*) entity
->URI
);
1475 input
->base
= entity
->content
;
1476 if (entity
->length
== 0)
1477 entity
->length
= xmlStrlen(entity
->content
);
1478 input
->cur
= entity
->content
;
1479 input
->length
= entity
->length
;
1480 input
->end
= &entity
->content
[input
->length
];
1485 * xmlNewStringInputStream:
1486 * @ctxt: an XML parser context
1487 * @buffer: an memory buffer
1489 * Create a new input stream based on a memory buffer.
1490 * Returns the new input stream
1493 xmlNewStringInputStream(xmlParserCtxtPtr ctxt
, const xmlChar
*buffer
) {
1494 xmlParserInputPtr input
;
1496 if (buffer
== NULL
) {
1497 xmlErrInternal(ctxt
, "xmlNewStringInputStream string = NULL\n",
1501 if (xmlParserDebugEntities
)
1502 xmlGenericError(xmlGenericErrorContext
,
1503 "new fixed input: %.30s\n", buffer
);
1504 input
= xmlNewInputStream(ctxt
);
1505 if (input
== NULL
) {
1506 xmlErrMemory(ctxt
, "couldn't allocate a new input stream\n");
1509 input
->base
= buffer
;
1510 input
->cur
= buffer
;
1511 input
->length
= xmlStrlen(buffer
);
1512 input
->end
= &buffer
[input
->length
];
1517 * xmlNewInputFromFile:
1518 * @ctxt: an XML parser context
1519 * @filename: the filename to use as entity
1521 * Create a new input stream based on a file or an URL.
1523 * Returns the new input stream or NULL in case of error
1526 xmlNewInputFromFile(xmlParserCtxtPtr ctxt
, const char *filename
) {
1527 xmlParserInputBufferPtr buf
;
1528 xmlParserInputPtr inputStream
;
1529 char *directory
= NULL
;
1530 xmlChar
*URI
= NULL
;
1532 if (xmlParserDebugEntities
)
1533 xmlGenericError(xmlGenericErrorContext
,
1534 "new input from file: %s\n", filename
);
1535 if (ctxt
== NULL
) return(NULL
);
1536 buf
= xmlParserInputBufferCreateFilename(filename
, XML_CHAR_ENCODING_NONE
);
1538 if (filename
== NULL
)
1539 __xmlLoaderErr(ctxt
,
1540 "failed to load external entity: NULL filename \n",
1543 __xmlLoaderErr(ctxt
, "failed to load external entity \"%s\"\n",
1544 (const char *) filename
);
1548 inputStream
= xmlNewInputStream(ctxt
);
1549 if (inputStream
== NULL
) {
1550 xmlFreeParserInputBuffer(buf
);
1554 inputStream
->buf
= buf
;
1555 inputStream
= xmlCheckHTTPInput(ctxt
, inputStream
);
1556 if (inputStream
== NULL
)
1559 if (inputStream
->filename
== NULL
)
1560 URI
= xmlStrdup((xmlChar
*) filename
);
1562 URI
= xmlStrdup((xmlChar
*) inputStream
->filename
);
1563 directory
= xmlParserGetDirectory((const char *) URI
);
1564 if (inputStream
->filename
!= NULL
) xmlFree((char *)inputStream
->filename
);
1565 inputStream
->filename
= (char *) xmlCanonicPath((const xmlChar
*) URI
);
1566 if (URI
!= NULL
) xmlFree((char *) URI
);
1567 inputStream
->directory
= directory
;
1569 xmlBufResetInput(inputStream
->buf
->buffer
, inputStream
);
1570 if ((ctxt
->directory
== NULL
) && (directory
!= NULL
))
1571 ctxt
->directory
= (char *) xmlStrdup((const xmlChar
*) directory
);
1572 return(inputStream
);
1575 /************************************************************************
1577 * Commodity functions to handle parser contexts *
1579 ************************************************************************/
1582 * xmlInitParserCtxt:
1583 * @ctxt: an XML parser context
1585 * Initialize a parser context
1587 * Returns 0 in case of success and -1 in case of error
1591 xmlInitParserCtxt(xmlParserCtxtPtr ctxt
)
1593 xmlParserInputPtr input
;
1596 xmlErrInternal(NULL
, "Got NULL parser context\n", NULL
);
1600 xmlDefaultSAXHandlerInit();
1602 if (ctxt
->dict
== NULL
)
1603 ctxt
->dict
= xmlDictCreate();
1604 if (ctxt
->dict
== NULL
) {
1605 xmlErrMemory(NULL
, "cannot initialize parser context\n");
1608 xmlDictSetLimit(ctxt
->dict
, XML_MAX_DICTIONARY_LIMIT
);
1610 if (ctxt
->sax
== NULL
)
1611 ctxt
->sax
= (xmlSAXHandler
*) xmlMalloc(sizeof(xmlSAXHandler
));
1612 if (ctxt
->sax
== NULL
) {
1613 xmlErrMemory(NULL
, "cannot initialize parser context\n");
1617 xmlSAXVersion(ctxt
->sax
, 2);
1621 /* Allocate the Input stack */
1622 if (ctxt
->inputTab
== NULL
) {
1623 ctxt
->inputTab
= (xmlParserInputPtr
*)
1624 xmlMalloc(5 * sizeof(xmlParserInputPtr
));
1627 if (ctxt
->inputTab
== NULL
) {
1628 xmlErrMemory(NULL
, "cannot initialize parser context\n");
1634 while ((input
= inputPop(ctxt
)) != NULL
) { /* Non consuming */
1635 xmlFreeInputStream(input
);
1640 ctxt
->version
= NULL
;
1641 ctxt
->encoding
= NULL
;
1642 ctxt
->standalone
= -1;
1643 ctxt
->hasExternalSubset
= 0;
1644 ctxt
->hasPErefs
= 0;
1647 ctxt
->instate
= XML_PARSER_START
;
1649 ctxt
->directory
= NULL
;
1651 /* Allocate the Node stack */
1652 if (ctxt
->nodeTab
== NULL
) {
1653 ctxt
->nodeTab
= (xmlNodePtr
*) xmlMalloc(10 * sizeof(xmlNodePtr
));
1656 if (ctxt
->nodeTab
== NULL
) {
1657 xmlErrMemory(NULL
, "cannot initialize parser context\n");
1669 /* Allocate the Name stack */
1670 if (ctxt
->nameTab
== NULL
) {
1671 ctxt
->nameTab
= (const xmlChar
**) xmlMalloc(10 * sizeof(xmlChar
*));
1674 if (ctxt
->nameTab
== NULL
) {
1675 xmlErrMemory(NULL
, "cannot initialize parser context\n");
1690 /* Allocate the space stack */
1691 if (ctxt
->spaceTab
== NULL
) {
1692 ctxt
->spaceTab
= (int *) xmlMalloc(10 * sizeof(int));
1693 ctxt
->spaceMax
= 10;
1695 if (ctxt
->spaceTab
== NULL
) {
1696 xmlErrMemory(NULL
, "cannot initialize parser context\n");
1712 ctxt
->spaceMax
= 10;
1713 ctxt
->spaceTab
[0] = -1;
1714 ctxt
->space
= &ctxt
->spaceTab
[0];
1715 ctxt
->userData
= ctxt
;
1717 ctxt
->wellFormed
= 1;
1718 ctxt
->nsWellFormed
= 1;
1720 ctxt
->loadsubset
= xmlLoadExtDtdDefaultValue
;
1721 if (ctxt
->loadsubset
) {
1722 ctxt
->options
|= XML_PARSE_DTDLOAD
;
1724 ctxt
->validate
= xmlDoValidityCheckingDefaultValue
;
1725 ctxt
->pedantic
= xmlPedanticParserDefaultValue
;
1726 if (ctxt
->pedantic
) {
1727 ctxt
->options
|= XML_PARSE_PEDANTIC
;
1729 ctxt
->linenumbers
= xmlLineNumbersDefaultValue
;
1730 ctxt
->keepBlanks
= xmlKeepBlanksDefaultValue
;
1731 if (ctxt
->keepBlanks
== 0) {
1732 ctxt
->sax
->ignorableWhitespace
= xmlSAX2IgnorableWhitespace
;
1733 ctxt
->options
|= XML_PARSE_NOBLANKS
;
1736 ctxt
->vctxt
.finishDtd
= XML_CTXT_FINISH_DTD_0
;
1737 ctxt
->vctxt
.userData
= ctxt
;
1738 ctxt
->vctxt
.error
= xmlParserValidityError
;
1739 ctxt
->vctxt
.warning
= xmlParserValidityWarning
;
1740 if (ctxt
->validate
) {
1741 if (xmlGetWarningsDefaultValue
== 0)
1742 ctxt
->vctxt
.warning
= NULL
;
1744 ctxt
->vctxt
.warning
= xmlParserValidityWarning
;
1745 ctxt
->vctxt
.nodeMax
= 0;
1746 ctxt
->options
|= XML_PARSE_DTDVALID
;
1748 ctxt
->replaceEntities
= xmlSubstituteEntitiesDefaultValue
;
1749 if (ctxt
->replaceEntities
) {
1750 ctxt
->options
|= XML_PARSE_NOENT
;
1752 ctxt
->record_info
= 0;
1753 ctxt
->checkIndex
= 0;
1755 ctxt
->errNo
= XML_ERR_OK
;
1757 ctxt
->charset
= XML_CHAR_ENCODING_UTF8
;
1758 ctxt
->catalogs
= NULL
;
1759 ctxt
->nbentities
= 0;
1760 ctxt
->sizeentities
= 0;
1761 ctxt
->sizeentcopy
= 0;
1763 xmlInitNodeInfoSeq(&ctxt
->node_seq
);
1768 * xmlFreeParserCtxt:
1769 * @ctxt: an XML parser context
1771 * Free all the memory used by a parser context. However the parsed
1772 * document in ctxt->myDoc is not freed.
1776 xmlFreeParserCtxt(xmlParserCtxtPtr ctxt
)
1778 xmlParserInputPtr input
;
1780 if (ctxt
== NULL
) return;
1782 while ((input
= inputPop(ctxt
)) != NULL
) { /* Non consuming */
1783 xmlFreeInputStream(input
);
1785 if (ctxt
->spaceTab
!= NULL
) xmlFree(ctxt
->spaceTab
);
1786 if (ctxt
->nameTab
!= NULL
) xmlFree((xmlChar
* *)ctxt
->nameTab
);
1787 if (ctxt
->nodeTab
!= NULL
) xmlFree(ctxt
->nodeTab
);
1788 if (ctxt
->nodeInfoTab
!= NULL
) xmlFree(ctxt
->nodeInfoTab
);
1789 if (ctxt
->inputTab
!= NULL
) xmlFree(ctxt
->inputTab
);
1790 if (ctxt
->version
!= NULL
) xmlFree((char *) ctxt
->version
);
1791 if (ctxt
->encoding
!= NULL
) xmlFree((char *) ctxt
->encoding
);
1792 if (ctxt
->extSubURI
!= NULL
) xmlFree((char *) ctxt
->extSubURI
);
1793 if (ctxt
->extSubSystem
!= NULL
) xmlFree((char *) ctxt
->extSubSystem
);
1794 #ifdef LIBXML_SAX1_ENABLED
1795 if ((ctxt
->sax
!= NULL
) &&
1796 (ctxt
->sax
!= (xmlSAXHandlerPtr
) &xmlDefaultSAXHandler
))
1798 if (ctxt
->sax
!= NULL
)
1799 #endif /* LIBXML_SAX1_ENABLED */
1801 if (ctxt
->directory
!= NULL
) xmlFree((char *) ctxt
->directory
);
1802 if (ctxt
->vctxt
.nodeTab
!= NULL
) xmlFree(ctxt
->vctxt
.nodeTab
);
1803 if (ctxt
->atts
!= NULL
) xmlFree((xmlChar
* *)ctxt
->atts
);
1804 if (ctxt
->dict
!= NULL
) xmlDictFree(ctxt
->dict
);
1805 if (ctxt
->nsTab
!= NULL
) xmlFree((char *) ctxt
->nsTab
);
1806 if (ctxt
->pushTab
!= NULL
) xmlFree(ctxt
->pushTab
);
1807 if (ctxt
->attallocs
!= NULL
) xmlFree(ctxt
->attallocs
);
1808 if (ctxt
->attsDefault
!= NULL
)
1809 xmlHashFree(ctxt
->attsDefault
, xmlHashDefaultDeallocator
);
1810 if (ctxt
->attsSpecial
!= NULL
)
1811 xmlHashFree(ctxt
->attsSpecial
, NULL
);
1812 if (ctxt
->freeElems
!= NULL
) {
1813 xmlNodePtr cur
, next
;
1815 cur
= ctxt
->freeElems
;
1816 while (cur
!= NULL
) {
1822 if (ctxt
->freeAttrs
!= NULL
) {
1823 xmlAttrPtr cur
, next
;
1825 cur
= ctxt
->freeAttrs
;
1826 while (cur
!= NULL
) {
1833 * cleanup the error strings
1835 if (ctxt
->lastError
.message
!= NULL
)
1836 xmlFree(ctxt
->lastError
.message
);
1837 if (ctxt
->lastError
.file
!= NULL
)
1838 xmlFree(ctxt
->lastError
.file
);
1839 if (ctxt
->lastError
.str1
!= NULL
)
1840 xmlFree(ctxt
->lastError
.str1
);
1841 if (ctxt
->lastError
.str2
!= NULL
)
1842 xmlFree(ctxt
->lastError
.str2
);
1843 if (ctxt
->lastError
.str3
!= NULL
)
1844 xmlFree(ctxt
->lastError
.str3
);
1846 #ifdef LIBXML_CATALOG_ENABLED
1847 if (ctxt
->catalogs
!= NULL
)
1848 xmlCatalogFreeLocal(ctxt
->catalogs
);
1856 * Allocate and initialize a new parser context.
1858 * Returns the xmlParserCtxtPtr or NULL
1862 xmlNewParserCtxt(void)
1864 xmlParserCtxtPtr ctxt
;
1866 ctxt
= (xmlParserCtxtPtr
) xmlMalloc(sizeof(xmlParserCtxt
));
1868 xmlErrMemory(NULL
, "cannot allocate parser context\n");
1871 memset(ctxt
, 0, sizeof(xmlParserCtxt
));
1872 if (xmlInitParserCtxt(ctxt
) < 0) {
1873 xmlFreeParserCtxt(ctxt
);
1879 /************************************************************************
1881 * Handling of node information *
1883 ************************************************************************/
1886 * xmlClearParserCtxt:
1887 * @ctxt: an XML parser context
1889 * Clear (release owned resources) and reinitialize a parser context
1893 xmlClearParserCtxt(xmlParserCtxtPtr ctxt
)
1897 xmlClearNodeInfoSeq(&ctxt
->node_seq
);
1903 * xmlParserFindNodeInfo:
1904 * @ctx: an XML parser context
1905 * @node: an XML node within the tree
1907 * Find the parser node info struct for a given node
1909 * Returns an xmlParserNodeInfo block pointer or NULL
1911 const xmlParserNodeInfo
*
1912 xmlParserFindNodeInfo(const xmlParserCtxtPtr ctx
, const xmlNodePtr node
)
1916 if ((ctx
== NULL
) || (node
== NULL
))
1918 /* Find position where node should be at */
1919 pos
= xmlParserFindNodeInfoIndex(&ctx
->node_seq
, node
);
1920 if (pos
< ctx
->node_seq
.length
1921 && ctx
->node_seq
.buffer
[pos
].node
== node
)
1922 return &ctx
->node_seq
.buffer
[pos
];
1929 * xmlInitNodeInfoSeq:
1930 * @seq: a node info sequence pointer
1932 * -- Initialize (set to initial state) node info sequence
1935 xmlInitNodeInfoSeq(xmlParserNodeInfoSeqPtr seq
)
1945 * xmlClearNodeInfoSeq:
1946 * @seq: a node info sequence pointer
1948 * -- Clear (release memory and reinitialize) node
1952 xmlClearNodeInfoSeq(xmlParserNodeInfoSeqPtr seq
)
1956 if (seq
->buffer
!= NULL
)
1957 xmlFree(seq
->buffer
);
1958 xmlInitNodeInfoSeq(seq
);
1962 * xmlParserFindNodeInfoIndex:
1963 * @seq: a node info sequence pointer
1964 * @node: an XML node pointer
1967 * xmlParserFindNodeInfoIndex : Find the index that the info record for
1968 * the given node is or should be at in a sorted sequence
1970 * Returns a long indicating the position of the record
1973 xmlParserFindNodeInfoIndex(const xmlParserNodeInfoSeqPtr seq
,
1974 const xmlNodePtr node
)
1976 unsigned long upper
, lower
, middle
;
1979 if ((seq
== NULL
) || (node
== NULL
))
1980 return ((unsigned long) -1);
1982 /* Do a binary search for the key */
1984 upper
= seq
->length
;
1986 while (lower
<= upper
&& !found
) {
1987 middle
= lower
+ (upper
- lower
) / 2;
1988 if (node
== seq
->buffer
[middle
- 1].node
)
1990 else if (node
< seq
->buffer
[middle
- 1].node
)
1996 /* Return position */
1997 if (middle
== 0 || seq
->buffer
[middle
- 1].node
< node
)
2005 * xmlParserAddNodeInfo:
2006 * @ctxt: an XML parser context
2007 * @info: a node info sequence pointer
2009 * Insert node info record into the sorted sequence
2012 xmlParserAddNodeInfo(xmlParserCtxtPtr ctxt
,
2013 const xmlParserNodeInfoPtr info
)
2017 if ((ctxt
== NULL
) || (info
== NULL
)) return;
2019 /* Find pos and check to see if node is already in the sequence */
2020 pos
= xmlParserFindNodeInfoIndex(&ctxt
->node_seq
, (xmlNodePtr
)
2023 if ((pos
< ctxt
->node_seq
.length
) &&
2024 (ctxt
->node_seq
.buffer
!= NULL
) &&
2025 (ctxt
->node_seq
.buffer
[pos
].node
== info
->node
)) {
2026 ctxt
->node_seq
.buffer
[pos
] = *info
;
2029 /* Otherwise, we need to add new node to buffer */
2031 if ((ctxt
->node_seq
.length
+ 1 > ctxt
->node_seq
.maximum
) ||
2032 (ctxt
->node_seq
.buffer
== NULL
)) {
2033 xmlParserNodeInfo
*tmp_buffer
;
2034 unsigned int byte_size
;
2036 if (ctxt
->node_seq
.maximum
== 0)
2037 ctxt
->node_seq
.maximum
= 2;
2038 byte_size
= (sizeof(*ctxt
->node_seq
.buffer
) *
2039 (2 * ctxt
->node_seq
.maximum
));
2041 if (ctxt
->node_seq
.buffer
== NULL
)
2042 tmp_buffer
= (xmlParserNodeInfo
*) xmlMalloc(byte_size
);
2045 (xmlParserNodeInfo
*) xmlRealloc(ctxt
->node_seq
.buffer
,
2048 if (tmp_buffer
== NULL
) {
2049 xmlErrMemory(ctxt
, "failed to allocate buffer\n");
2052 ctxt
->node_seq
.buffer
= tmp_buffer
;
2053 ctxt
->node_seq
.maximum
*= 2;
2056 /* If position is not at end, move elements out of the way */
2057 if (pos
!= ctxt
->node_seq
.length
) {
2060 for (i
= ctxt
->node_seq
.length
; i
> pos
; i
--)
2061 ctxt
->node_seq
.buffer
[i
] = ctxt
->node_seq
.buffer
[i
- 1];
2064 /* Copy element and increase length */
2065 ctxt
->node_seq
.buffer
[pos
] = *info
;
2066 ctxt
->node_seq
.length
++;
2070 /************************************************************************
2072 * Defaults settings *
2074 ************************************************************************/
2076 * xmlPedanticParserDefault:
2079 * Set and return the previous value for enabling pedantic warnings.
2081 * Returns the last value for 0 for no substitution, 1 for substitution.
2085 xmlPedanticParserDefault(int val
) {
2086 int old
= xmlPedanticParserDefaultValue
;
2088 xmlPedanticParserDefaultValue
= val
;
2093 * xmlLineNumbersDefault:
2096 * Set and return the previous value for enabling line numbers in elements
2097 * contents. This may break on old application and is turned off by default.
2099 * Returns the last value for 0 for no substitution, 1 for substitution.
2103 xmlLineNumbersDefault(int val
) {
2104 int old
= xmlLineNumbersDefaultValue
;
2106 xmlLineNumbersDefaultValue
= val
;
2111 * xmlSubstituteEntitiesDefault:
2114 * Set and return the previous value for default entity support.
2115 * Initially the parser always keep entity references instead of substituting
2116 * entity values in the output. This function has to be used to change the
2117 * default parser behavior
2118 * SAX::substituteEntities() has to be used for changing that on a file by
2121 * Returns the last value for 0 for no substitution, 1 for substitution.
2125 xmlSubstituteEntitiesDefault(int val
) {
2126 int old
= xmlSubstituteEntitiesDefaultValue
;
2128 xmlSubstituteEntitiesDefaultValue
= val
;
2133 * xmlKeepBlanksDefault:
2136 * Set and return the previous value for default blanks text nodes support.
2137 * The 1.x version of the parser used an heuristic to try to detect
2138 * ignorable white spaces. As a result the SAX callback was generating
2139 * xmlSAX2IgnorableWhitespace() callbacks instead of characters() one, and when
2140 * using the DOM output text nodes containing those blanks were not generated.
2141 * The 2.x and later version will switch to the XML standard way and
2142 * ignorableWhitespace() are only generated when running the parser in
2143 * validating mode and when the current element doesn't allow CDATA or
2145 * This function is provided as a way to force the standard behavior
2146 * on 1.X libs and to switch back to the old mode for compatibility when
2147 * running 1.X client code on 2.X . Upgrade of 1.X code should be done
2148 * by using xmlIsBlankNode() commodity function to detect the "empty"
2150 * This value also affect autogeneration of indentation when saving code
2151 * if blanks sections are kept, indentation is not generated.
2153 * Returns the last value for 0 for no substitution, 1 for substitution.
2157 xmlKeepBlanksDefault(int val
) {
2158 int old
= xmlKeepBlanksDefaultValue
;
2160 xmlKeepBlanksDefaultValue
= val
;
2161 if (!val
) xmlIndentTreeOutput
= 1;
2165 #define bottom_parserInternals
2166 #include "elfgcchack.h"