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
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>
62 * Various global defaults for parsing
67 * @version: the include version number
69 * check the compiled lib version against the include one.
70 * This can warn or immediately kill the application
73 xmlCheckVersion(int version) {
74 int myversion = (int) LIBXML_VERSION;
78 if ((myversion / 10000) != (version / 10000)) {
79 xmlGenericError(xmlGenericErrorContext,
80 "Fatal: program compiled against libxml %d using libxml %d\n",
81 (version / 10000), (myversion / 10000));
83 "Fatal: program compiled against libxml %d using libxml %d\n",
84 (version / 10000), (myversion / 10000));
86 if ((myversion / 100) < (version / 100)) {
87 xmlGenericError(xmlGenericErrorContext,
88 "Warning: program compiled against libxml %d using older %d\n",
89 (version / 100), (myversion / 100));
94 /************************************************************************
96 * Some factorized error routines *
98 ************************************************************************/
103 * @ctxt: an XML parser context
104 * @extra: extra informations
106 * Handle a redefinition of attribute error
109 xmlErrMemory(xmlParserCtxtPtr ctxt, const char *extra)
111 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
112 (ctxt->instate == XML_PARSER_EOF))
115 ctxt->errNo = XML_ERR_NO_MEMORY;
116 ctxt->instate = XML_PARSER_EOF;
117 ctxt->disableSAX = 1;
120 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER,
121 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
123 "Memory allocation failed : %s\n", extra);
125 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER,
126 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL,
127 NULL, NULL, 0, 0, "Memory allocation failed\n");
132 * @ctxt: an XML parser context
133 * @xmlerr: the error number
134 * @msg: the error message
135 * @str1: an string info
136 * @str2: an string info
138 * Handle an encoding error
141 __xmlErrEncoding(xmlParserCtxtPtr ctxt, xmlParserErrors xmlerr,
142 const char *msg, const xmlChar * str1, const xmlChar * str2)
144 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
145 (ctxt->instate == XML_PARSER_EOF))
148 ctxt->errNo = xmlerr;
149 __xmlRaiseError(NULL, NULL, NULL,
150 ctxt, NULL, XML_FROM_PARSER, xmlerr, XML_ERR_FATAL,
151 NULL, 0, (const char *) str1, (const char *) str2,
152 NULL, 0, 0, msg, str1, str2);
154 ctxt->wellFormed = 0;
155 if (ctxt->recovery == 0)
156 ctxt->disableSAX = 1;
162 * @ctxt: an XML parser context
163 * @msg: the error message
164 * @str: error informations
166 * Handle an internal error
169 xmlErrInternal(xmlParserCtxtPtr ctxt, const char *msg, const xmlChar * str)
171 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
172 (ctxt->instate == XML_PARSER_EOF))
175 ctxt->errNo = XML_ERR_INTERNAL_ERROR;
176 __xmlRaiseError(NULL, NULL, NULL,
177 ctxt, NULL, XML_FROM_PARSER, XML_ERR_INTERNAL_ERROR,
178 XML_ERR_FATAL, NULL, 0, (const char *) str, NULL, NULL,
181 ctxt->wellFormed = 0;
182 if (ctxt->recovery == 0)
183 ctxt->disableSAX = 1;
189 * @ctxt: an XML parser context
190 * @error: the error number
191 * @msg: the error message
192 * @val: an integer value
197 xmlErrEncodingInt(xmlParserCtxtPtr ctxt, xmlParserErrors error,
198 const char *msg, int val)
200 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
201 (ctxt->instate == XML_PARSER_EOF))
205 __xmlRaiseError(NULL, NULL, NULL,
206 ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_FATAL,
207 NULL, 0, NULL, NULL, NULL, val, 0, msg, val);
209 ctxt->wellFormed = 0;
210 if (ctxt->recovery == 0)
211 ctxt->disableSAX = 1;
217 * @c: an unicode character (int)
219 * Check whether the character is allowed by the production
220 * [84] Letter ::= BaseChar | Ideographic
222 * Returns 0 if not, non-zero otherwise
226 return(IS_BASECHAR(c) || IS_IDEOGRAPHIC(c));
229 /************************************************************************
231 * Input handling functions for progressive parsing *
233 ************************************************************************/
235 /* #define DEBUG_INPUT */
236 /* #define DEBUG_STACK */
237 /* #define DEBUG_PUSH */
240 /* we need to keep enough input to show errors in context */
244 #define CHECK_BUFFER(in) check_buffer(in)
247 void check_buffer(xmlParserInputPtr in) {
248 if (in->base != xmlBufContent(in->buf->buffer)) {
249 xmlGenericError(xmlGenericErrorContext,
250 "xmlParserInput: base mismatch problem\n");
252 if (in->cur < in->base) {
253 xmlGenericError(xmlGenericErrorContext,
254 "xmlParserInput: cur < base problem\n");
256 if (in->cur > in->base + xmlBufUse(in->buf->buffer)) {
257 xmlGenericError(xmlGenericErrorContext,
258 "xmlParserInput: cur > base + use problem\n");
260 xmlGenericError(xmlGenericErrorContext,"buffer %x : content %x, cur %d, use %d\n",
261 (int) in, (int) xmlBufContent(in->buf->buffer), in->cur - in->base,
262 xmlBufUse(in->buf->buffer));
266 #define CHECK_BUFFER(in)
271 * xmlParserInputRead:
272 * @in: an XML parser input
273 * @len: an indicative size for the lookahead
275 * This function was internal and is deprecated.
277 * Returns -1 as this is an error to use it.
280 xmlParserInputRead(xmlParserInputPtr in ATTRIBUTE_UNUSED, int len ATTRIBUTE_UNUSED) {
285 * xmlParserInputGrow:
286 * @in: an XML parser input
287 * @len: an indicative size for the lookahead
289 * This function increase the input for the parser. It tries to
290 * preserve pointers to the input buffer, and keep already read data
292 * Returns the amount of char read, or -1 in case of error, 0 indicate the
296 xmlParserInputGrow(xmlParserInputPtr in, int len) {
299 const xmlChar *content;
301 if ((in == NULL) || (len < 0)) return(-1);
303 xmlGenericError(xmlGenericErrorContext, "Grow\n");
305 if (in->buf == NULL) return(-1);
306 if (in->base == NULL) return(-1);
307 if (in->cur == NULL) return(-1);
308 if (in->buf->buffer == NULL) return(-1);
312 indx = in->cur - in->base;
313 if (xmlBufUse(in->buf->buffer) > (unsigned int) indx + INPUT_CHUNK) {
319 if (in->buf->readcallback != NULL) {
320 ret = xmlParserInputBufferGrow(in->buf, len);
325 * NOTE : in->base may be a "dangling" i.e. freed pointer in this
326 * block, but we use it really as an integer to do some
327 * pointer arithmetic. Insure will raise it as a bug but in
328 * that specific case, that's not !
331 content = xmlBufContent(in->buf->buffer);
332 if (in->base != content) {
334 * the buffer has been reallocated
336 indx = in->cur - in->base;
338 in->cur = &content[indx];
340 in->end = xmlBufEnd(in->buf->buffer);
348 * xmlParserInputShrink:
349 * @in: an XML parser input
351 * This function removes used input for the parser.
354 xmlParserInputShrink(xmlParserInputPtr in) {
358 const xmlChar *content;
361 xmlGenericError(xmlGenericErrorContext, "Shrink\n");
363 if (in == NULL) return;
364 if (in->buf == NULL) return;
365 if (in->base == NULL) return;
366 if (in->cur == NULL) return;
367 if (in->buf->buffer == NULL) return;
371 used = in->cur - xmlBufContent(in->buf->buffer);
373 * Do not shrink on large buffers whose only a tiny fraction
376 if (used > INPUT_CHUNK) {
377 ret = xmlBufShrink(in->buf->buffer, used - LINE_LEN);
382 in->end = xmlBufEnd(in->buf->buffer);
387 if (xmlBufUse(in->buf->buffer) > INPUT_CHUNK) {
390 xmlParserInputBufferRead(in->buf, 2 * INPUT_CHUNK);
391 content = xmlBufContent(in->buf->buffer);
392 if (in->base != content) {
394 * the buffer has been reallocated
396 indx = in->cur - in->base;
398 in->cur = &content[indx];
400 in->end = xmlBufEnd(in->buf->buffer);
405 /************************************************************************
407 * UTF8 character input and related functions *
409 ************************************************************************/
413 * @ctxt: the XML parser context
415 * Skip to the next char input char.
419 xmlNextChar(xmlParserCtxtPtr ctxt)
421 if ((ctxt == NULL) || (ctxt->instate == XML_PARSER_EOF) ||
422 (ctxt->input == NULL))
425 if (ctxt->charset == XML_CHAR_ENCODING_UTF8) {
426 if ((*ctxt->input->cur == 0) &&
427 (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0) &&
428 (ctxt->instate != XML_PARSER_COMMENT)) {
430 * If we are at the end of the current entity and
431 * the context allows it, we pop consumed entities
433 * the auto closing should be blocked in other cases
437 const unsigned char *cur;
441 * 2.11 End-of-Line Handling
442 * the literal two-character sequence "#xD#xA" or a standalone
443 * literal #xD, an XML processor must pass to the application
444 * the single character #xA.
446 if (*(ctxt->input->cur) == '\n') {
447 ctxt->input->line++; ctxt->input->col = 1;
452 * We are supposed to handle UTF8, check it's valid
453 * From rfc2044: encoding of the Unicode values on UTF-8:
455 * UCS-4 range (hex.) UTF-8 octet sequence (binary)
456 * 0000 0000-0000 007F 0xxxxxxx
457 * 0000 0080-0000 07FF 110xxxxx 10xxxxxx
458 * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx
460 * Check for the 0x110000 limit too
462 cur = ctxt->input->cur;
469 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
470 cur = ctxt->input->cur;
472 if ((cur[1] & 0xc0) != 0x80)
474 if ((c & 0xe0) == 0xe0) {
478 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
479 cur = ctxt->input->cur;
481 if ((cur[2] & 0xc0) != 0x80)
483 if ((c & 0xf0) == 0xf0) {
485 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
486 cur = ctxt->input->cur;
488 if (((c & 0xf8) != 0xf0) ||
489 ((cur[3] & 0xc0) != 0x80))
492 ctxt->input->cur += 4;
493 val = (cur[0] & 0x7) << 18;
494 val |= (cur[1] & 0x3f) << 12;
495 val |= (cur[2] & 0x3f) << 6;
496 val |= cur[3] & 0x3f;
499 ctxt->input->cur += 3;
500 val = (cur[0] & 0xf) << 12;
501 val |= (cur[1] & 0x3f) << 6;
502 val |= cur[2] & 0x3f;
504 if (((val > 0xd7ff) && (val < 0xe000)) ||
505 ((val > 0xfffd) && (val < 0x10000)) ||
507 xmlErrEncodingInt(ctxt, XML_ERR_INVALID_CHAR,
508 "Char 0x%X out of allowed range\n",
513 ctxt->input->cur += 2;
519 if (*ctxt->input->cur == 0)
520 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
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);
538 if ((*ctxt->input->cur == '%') && (!ctxt->html))
539 xmlParserHandlePEReference(ctxt);
540 if ((*ctxt->input->cur == 0) &&
541 (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0))
546 * If we detect an UTF8 error that probably mean that the
547 * input encoding didn't get properly advertised in the
548 * declaration header. Report the error and switch the encoding
549 * to ISO-Latin-1 (if you don't like this policy, just declare the
552 if ((ctxt == NULL) || (ctxt->input == NULL) ||
553 (ctxt->input->end - ctxt->input->cur < 4)) {
554 __xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR,
555 "Input is not proper UTF-8, indicate encoding !\n",
560 snprintf(buffer, 149, "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n",
561 ctxt->input->cur[0], ctxt->input->cur[1],
562 ctxt->input->cur[2], ctxt->input->cur[3]);
563 __xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR,
564 "Input is not proper UTF-8, indicate encoding !\n%s",
565 BAD_CAST buffer, NULL);
567 ctxt->charset = XML_CHAR_ENCODING_8859_1;
574 * @ctxt: the XML parser context
575 * @len: pointer to the length of the char read
577 * The current char value, if using UTF-8 this may actually span multiple
578 * bytes in the input buffer. Implement the end of line normalization:
579 * 2.11 End-of-Line Handling
580 * Wherever an external parsed entity or the literal entity value
581 * of an internal parsed entity contains either the literal two-character
582 * sequence "#xD#xA" or a standalone literal #xD, an XML processor
583 * must pass to the application the single character #xA.
584 * This behavior can conveniently be produced by normalizing all
585 * line breaks to #xA on input, before parsing.)
587 * Returns the current char value and its length
591 xmlCurrentChar(xmlParserCtxtPtr ctxt, int *len) {
592 if ((ctxt == NULL) || (len == NULL) || (ctxt->input == NULL)) return(0);
593 if (ctxt->instate == XML_PARSER_EOF)
596 if ((*ctxt->input->cur >= 0x20) && (*ctxt->input->cur <= 0x7F)) {
598 return((int) *ctxt->input->cur);
600 if (ctxt->charset == XML_CHAR_ENCODING_UTF8) {
602 * We are supposed to handle UTF8, check it's valid
603 * From rfc2044: encoding of the Unicode values on UTF-8:
605 * UCS-4 range (hex.) UTF-8 octet sequence (binary)
606 * 0000 0000-0000 007F 0xxxxxxx
607 * 0000 0080-0000 07FF 110xxxxx 10xxxxxx
608 * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx
610 * Check for the 0x110000 limit too
612 const unsigned char *cur = ctxt->input->cur;
618 if (((c & 0x40) == 0) || (c == 0xC0))
621 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
622 cur = ctxt->input->cur;
624 if ((cur[1] & 0xc0) != 0x80)
626 if ((c & 0xe0) == 0xe0) {
628 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
629 cur = ctxt->input->cur;
631 if ((cur[2] & 0xc0) != 0x80)
633 if ((c & 0xf0) == 0xf0) {
635 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
636 cur = ctxt->input->cur;
638 if (((c & 0xf8) != 0xf0) ||
639 ((cur[3] & 0xc0) != 0x80))
643 val = (cur[0] & 0x7) << 18;
644 val |= (cur[1] & 0x3f) << 12;
645 val |= (cur[2] & 0x3f) << 6;
646 val |= cur[3] & 0x3f;
652 val = (cur[0] & 0xf) << 12;
653 val |= (cur[1] & 0x3f) << 6;
654 val |= cur[2] & 0x3f;
661 val = (cur[0] & 0x1f) << 6;
662 val |= cur[1] & 0x3f;
667 xmlErrEncodingInt(ctxt, XML_ERR_INVALID_CHAR,
668 "Char 0x%X out of allowed range\n", val);
674 if (*ctxt->input->cur == 0)
675 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
676 if ((*ctxt->input->cur == 0) &&
677 (ctxt->input->end > ctxt->input->cur)) {
678 xmlErrEncodingInt(ctxt, XML_ERR_INVALID_CHAR,
679 "Char 0x0 out of allowed range\n", 0);
681 if (*ctxt->input->cur == 0xD) {
682 if (ctxt->input->cur[1] == 0xA) {
688 return((int) *ctxt->input->cur);
692 * Assume it's a fixed length encoding (1) with
693 * a compatible encoding for the ASCII set, since
694 * XML constructs only use < 128 chars
697 if (*ctxt->input->cur == 0xD) {
698 if (ctxt->input->cur[1] == 0xA) {
704 return((int) *ctxt->input->cur);
707 * An encoding problem may arise from a truncated input buffer
708 * splitting a character in the middle. In that case do not raise
709 * an error but return 0 to endicate an end of stream problem
711 if (ctxt->input->end - ctxt->input->cur < 4) {
717 * If we detect an UTF8 error that probably mean that the
718 * input encoding didn't get properly advertised in the
719 * declaration header. Report the error and switch the encoding
720 * to ISO-Latin-1 (if you don't like this policy, just declare the
726 snprintf(&buffer[0], 149, "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n",
727 ctxt->input->cur[0], ctxt->input->cur[1],
728 ctxt->input->cur[2], ctxt->input->cur[3]);
729 __xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR,
730 "Input is not proper UTF-8, indicate encoding !\n%s",
731 BAD_CAST buffer, NULL);
733 ctxt->charset = XML_CHAR_ENCODING_8859_1;
735 return((int) *ctxt->input->cur);
739 * xmlStringCurrentChar:
740 * @ctxt: the XML parser context
741 * @cur: pointer to the beginning of the char
742 * @len: pointer to the length of the char read
744 * The current char value, if using UTF-8 this may actually span multiple
745 * bytes in the input buffer.
747 * Returns the current char value and its length
751 xmlStringCurrentChar(xmlParserCtxtPtr ctxt, const xmlChar * cur, int *len)
753 if ((len == NULL) || (cur == NULL)) return(0);
754 if ((ctxt == NULL) || (ctxt->charset == XML_CHAR_ENCODING_UTF8)) {
756 * We are supposed to handle UTF8, check it's valid
757 * From rfc2044: encoding of the Unicode values on UTF-8:
759 * UCS-4 range (hex.) UTF-8 octet sequence (binary)
760 * 0000 0000-0000 007F 0xxxxxxx
761 * 0000 0080-0000 07FF 110xxxxx 10xxxxxx
762 * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx
764 * Check for the 0x110000 limit too
771 if ((cur[1] & 0xc0) != 0x80)
773 if ((c & 0xe0) == 0xe0) {
775 if ((cur[2] & 0xc0) != 0x80)
777 if ((c & 0xf0) == 0xf0) {
778 if (((c & 0xf8) != 0xf0) || ((cur[3] & 0xc0) != 0x80))
782 val = (cur[0] & 0x7) << 18;
783 val |= (cur[1] & 0x3f) << 12;
784 val |= (cur[2] & 0x3f) << 6;
785 val |= cur[3] & 0x3f;
789 val = (cur[0] & 0xf) << 12;
790 val |= (cur[1] & 0x3f) << 6;
791 val |= cur[2] & 0x3f;
796 val = (cur[0] & 0x1f) << 6;
797 val |= cur[1] & 0x3f;
800 xmlErrEncodingInt(ctxt, XML_ERR_INVALID_CHAR,
801 "Char 0x%X out of allowed range\n", val);
811 * Assume it's a fixed length encoding (1) with
812 * a compatible encoding for the ASCII set, since
813 * XML constructs only use < 128 chars
820 * An encoding problem may arise from a truncated input buffer
821 * splitting a character in the middle. In that case do not raise
822 * an error but return 0 to endicate an end of stream problem
824 if ((ctxt == NULL) || (ctxt->input == NULL) ||
825 (ctxt->input->end - ctxt->input->cur < 4)) {
830 * If we detect an UTF8 error that probably mean that the
831 * input encoding didn't get properly advertised in the
832 * declaration header. Report the error and switch the encoding
833 * to ISO-Latin-1 (if you don't like this policy, just declare the
839 snprintf(buffer, 149, "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n",
840 ctxt->input->cur[0], ctxt->input->cur[1],
841 ctxt->input->cur[2], ctxt->input->cur[3]);
842 __xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR,
843 "Input is not proper UTF-8, indicate encoding !\n%s",
844 BAD_CAST buffer, NULL);
851 * xmlCopyCharMultiByte:
852 * @out: pointer to an array of xmlChar
853 * @val: the char value
855 * append the char value in the array
857 * Returns the number of xmlChar written
860 xmlCopyCharMultiByte(xmlChar *out, int val) {
861 if (out == NULL) return(0);
863 * We are supposed to handle UTF8, check it's valid
864 * From rfc2044: encoding of the Unicode values on UTF-8:
866 * UCS-4 range (hex.) UTF-8 octet sequence (binary)
867 * 0000 0000-0000 007F 0xxxxxxx
868 * 0000 0080-0000 07FF 110xxxxx 10xxxxxx
869 * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx
872 xmlChar *savedout = out;
874 if (val < 0x800) { *out++= (val >> 6) | 0xC0; bits= 0; }
875 else if (val < 0x10000) { *out++= (val >> 12) | 0xE0; bits= 6;}
876 else if (val < 0x110000) { *out++= (val >> 18) | 0xF0; bits= 12; }
878 xmlErrEncodingInt(NULL, XML_ERR_INVALID_CHAR,
879 "Internal error, xmlCopyCharMultiByte 0x%X out of bound\n",
883 for ( ; bits >= 0; bits-= 6)
884 *out++= ((val >> bits) & 0x3F) | 0x80 ;
885 return (out - savedout);
887 *out = (xmlChar) val;
893 * @len: Ignored, compatibility
894 * @out: pointer to an array of xmlChar
895 * @val: the char value
897 * append the char value in the array
899 * Returns the number of xmlChar written
903 xmlCopyChar(int len ATTRIBUTE_UNUSED, xmlChar *out, int val) {
904 if (out == NULL) return(0);
905 /* the len parameter is ignored */
907 return(xmlCopyCharMultiByte (out, val));
909 *out = (xmlChar) val;
913 /************************************************************************
915 * Commodity functions to switch encodings *
917 ************************************************************************/
920 xmlSwitchToEncodingInt(xmlParserCtxtPtr ctxt,
921 xmlCharEncodingHandlerPtr handler, int len);
923 xmlSwitchInputEncodingInt(xmlParserCtxtPtr ctxt, xmlParserInputPtr input,
924 xmlCharEncodingHandlerPtr handler, int len);
927 * @ctxt: the parser context
928 * @enc: the encoding value (number)
930 * change the input functions when discovering the character encoding
933 * Returns 0 in case of success, -1 otherwise
936 xmlSwitchEncoding(xmlParserCtxtPtr ctxt, xmlCharEncoding enc)
938 xmlCharEncodingHandlerPtr handler;
942 if (ctxt == NULL) return(-1);
944 case XML_CHAR_ENCODING_ERROR:
945 __xmlErrEncoding(ctxt, XML_ERR_UNKNOWN_ENCODING,
946 "encoding unknown\n", NULL, NULL);
948 case XML_CHAR_ENCODING_NONE:
949 /* let's assume it's UTF-8 without the XML decl */
950 ctxt->charset = XML_CHAR_ENCODING_UTF8;
952 case XML_CHAR_ENCODING_UTF8:
953 /* default encoding, no conversion should be needed */
954 ctxt->charset = XML_CHAR_ENCODING_UTF8;
957 * Errata on XML-1.0 June 20 2001
958 * Specific handling of the Byte Order Mark for
961 if ((ctxt->input != NULL) &&
962 (ctxt->input->cur[0] == 0xEF) &&
963 (ctxt->input->cur[1] == 0xBB) &&
964 (ctxt->input->cur[2] == 0xBF)) {
965 ctxt->input->cur += 3;
968 case XML_CHAR_ENCODING_UTF16LE:
969 case XML_CHAR_ENCODING_UTF16BE:
970 /*The raw input characters are encoded
971 *in UTF-16. As we expect this function
972 *to be called after xmlCharEncInFunc, we expect
973 *ctxt->input->cur to contain UTF-8 encoded characters.
974 *So the raw UTF16 Byte Order Mark
975 *has also been converted into
976 *an UTF-8 BOM. Let's skip that BOM.
978 if ((ctxt->input != NULL) && (ctxt->input->cur != NULL) &&
979 (ctxt->input->cur[0] == 0xEF) &&
980 (ctxt->input->cur[1] == 0xBB) &&
981 (ctxt->input->cur[2] == 0xBF)) {
982 ctxt->input->cur += 3;
986 case XML_CHAR_ENCODING_UCS2:
989 case XML_CHAR_ENCODING_UCS4BE:
990 case XML_CHAR_ENCODING_UCS4LE:
991 case XML_CHAR_ENCODING_UCS4_2143:
992 case XML_CHAR_ENCODING_UCS4_3412:
995 case XML_CHAR_ENCODING_EBCDIC:
996 case XML_CHAR_ENCODING_8859_1:
997 case XML_CHAR_ENCODING_8859_2:
998 case XML_CHAR_ENCODING_8859_3:
999 case XML_CHAR_ENCODING_8859_4:
1000 case XML_CHAR_ENCODING_8859_5:
1001 case XML_CHAR_ENCODING_8859_6:
1002 case XML_CHAR_ENCODING_8859_7:
1003 case XML_CHAR_ENCODING_8859_8:
1004 case XML_CHAR_ENCODING_8859_9:
1005 case XML_CHAR_ENCODING_ASCII:
1006 case XML_CHAR_ENCODING_2022_JP:
1007 case XML_CHAR_ENCODING_SHIFT_JIS:
1008 case XML_CHAR_ENCODING_EUC_JP:
1012 handler = xmlGetCharEncodingHandler(enc);
1013 if (handler == NULL) {
1018 case XML_CHAR_ENCODING_ASCII:
1019 /* default encoding, no conversion should be needed */
1020 ctxt->charset = XML_CHAR_ENCODING_UTF8;
1022 case XML_CHAR_ENCODING_UTF16LE:
1024 case XML_CHAR_ENCODING_UTF16BE:
1026 case XML_CHAR_ENCODING_UCS4LE:
1027 __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
1028 "encoding not supported %s\n",
1029 BAD_CAST "USC4 little endian", NULL);
1031 case XML_CHAR_ENCODING_UCS4BE:
1032 __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
1033 "encoding not supported %s\n",
1034 BAD_CAST "USC4 big endian", NULL);
1036 case XML_CHAR_ENCODING_EBCDIC:
1037 __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
1038 "encoding not supported %s\n",
1039 BAD_CAST "EBCDIC", NULL);
1041 case XML_CHAR_ENCODING_UCS4_2143:
1042 __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
1043 "encoding not supported %s\n",
1044 BAD_CAST "UCS4 2143", NULL);
1046 case XML_CHAR_ENCODING_UCS4_3412:
1047 __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
1048 "encoding not supported %s\n",
1049 BAD_CAST "UCS4 3412", NULL);
1051 case XML_CHAR_ENCODING_UCS2:
1052 __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
1053 "encoding not supported %s\n",
1054 BAD_CAST "UCS2", NULL);
1056 case XML_CHAR_ENCODING_8859_1:
1057 case XML_CHAR_ENCODING_8859_2:
1058 case XML_CHAR_ENCODING_8859_3:
1059 case XML_CHAR_ENCODING_8859_4:
1060 case XML_CHAR_ENCODING_8859_5:
1061 case XML_CHAR_ENCODING_8859_6:
1062 case XML_CHAR_ENCODING_8859_7:
1063 case XML_CHAR_ENCODING_8859_8:
1064 case XML_CHAR_ENCODING_8859_9:
1066 * We used to keep the internal content in the
1067 * document encoding however this turns being unmaintainable
1068 * So xmlGetCharEncodingHandler() will return non-null
1069 * values for this now.
1071 if ((ctxt->inputNr == 1) &&
1072 (ctxt->encoding == NULL) &&
1073 (ctxt->input != NULL) &&
1074 (ctxt->input->encoding != NULL)) {
1075 ctxt->encoding = xmlStrdup(ctxt->input->encoding);
1077 ctxt->charset = enc;
1079 case XML_CHAR_ENCODING_2022_JP:
1080 __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
1081 "encoding not supported %s\n",
1082 BAD_CAST "ISO-2022-JP", NULL);
1084 case XML_CHAR_ENCODING_SHIFT_JIS:
1085 __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
1086 "encoding not supported %s\n",
1087 BAD_CAST "Shift_JIS", NULL);
1089 case XML_CHAR_ENCODING_EUC_JP:
1090 __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
1091 "encoding not supported %s\n",
1092 BAD_CAST "EUC-JP", NULL);
1098 if (handler == NULL)
1100 ctxt->charset = XML_CHAR_ENCODING_UTF8;
1101 ret = xmlSwitchToEncodingInt(ctxt, handler, len);
1102 if ((ret < 0) || (ctxt->errNo == XML_I18N_CONV_FAILED)) {
1104 * on encoding conversion errors, stop the parser
1106 xmlStopParser(ctxt);
1107 ctxt->errNo = XML_I18N_CONV_FAILED;
1113 * xmlSwitchInputEncoding:
1114 * @ctxt: the parser context
1115 * @input: the input stream
1116 * @handler: the encoding handler
1117 * @len: the number of bytes to convert for the first line or -1
1119 * change the input functions when discovering the character encoding
1120 * of a given entity.
1122 * Returns 0 in case of success, -1 otherwise
1125 xmlSwitchInputEncodingInt(xmlParserCtxtPtr ctxt, xmlParserInputPtr input,
1126 xmlCharEncodingHandlerPtr handler, int len)
1130 if (handler == NULL)
1134 if (input->buf != NULL) {
1135 if (input->buf->encoder != NULL) {
1137 * Check in case the auto encoding detetection triggered
1140 if (input->buf->encoder == handler)
1144 * "UTF-16" can be used for both LE and BE
1145 if ((!xmlStrncmp(BAD_CAST input->buf->encoder->name,
1146 BAD_CAST "UTF-16", 6)) &&
1147 (!xmlStrncmp(BAD_CAST handler->name,
1148 BAD_CAST "UTF-16", 6))) {
1154 * Note: this is a bit dangerous, but that's what it
1155 * takes to use nearly compatible signature for different
1158 xmlCharEncCloseFunc(input->buf->encoder);
1159 input->buf->encoder = handler;
1162 input->buf->encoder = handler;
1165 * Is there already some content down the pipe to convert ?
1167 if (xmlBufIsEmpty(input->buf->buffer) == 0) {
1172 * Specific handling of the Byte Order Mark for
1175 if ((handler->name != NULL) &&
1176 (!strcmp(handler->name, "UTF-16LE") ||
1177 !strcmp(handler->name, "UTF-16")) &&
1178 (input->cur[0] == 0xFF) && (input->cur[1] == 0xFE)) {
1181 if ((handler->name != NULL) &&
1182 (!strcmp(handler->name, "UTF-16BE")) &&
1183 (input->cur[0] == 0xFE) && (input->cur[1] == 0xFF)) {
1187 * Errata on XML-1.0 June 20 2001
1188 * Specific handling of the Byte Order Mark for
1191 if ((handler->name != NULL) &&
1192 (!strcmp(handler->name, "UTF-8")) &&
1193 (input->cur[0] == 0xEF) &&
1194 (input->cur[1] == 0xBB) && (input->cur[2] == 0xBF)) {
1199 * Shrink the current input buffer.
1200 * Move it as the raw buffer and create a new input buffer
1202 processed = input->cur - input->base;
1203 xmlBufShrink(input->buf->buffer, processed);
1204 input->buf->raw = input->buf->buffer;
1205 input->buf->buffer = xmlBufCreate();
1206 input->buf->rawconsumed = processed;
1207 use = xmlBufUse(input->buf->raw);
1211 * convert as much as possible of the buffer
1213 nbchars = xmlCharEncInput(input->buf, 1);
1216 * convert just enough to get
1217 * '<?xml version="1.0" encoding="xxx"?>'
1218 * parsed with the autodetected encoding
1219 * into the parser reading buffer.
1221 nbchars = xmlCharEncFirstLineInput(input->buf, len);
1224 xmlErrInternal(ctxt,
1225 "switching encoding: encoder error\n",
1229 input->buf->rawconsumed += use - xmlBufUse(input->buf->raw);
1230 xmlBufResetInput(input->buf->buffer, input);
1233 } else if (input->length == 0) {
1235 * When parsing a static memory array one must know the
1236 * size to be able to convert the buffer.
1238 xmlErrInternal(ctxt, "switching encoding : no input\n", NULL);
1245 * xmlSwitchInputEncoding:
1246 * @ctxt: the parser context
1247 * @input: the input stream
1248 * @handler: the encoding handler
1250 * change the input functions when discovering the character encoding
1251 * of a given entity.
1253 * Returns 0 in case of success, -1 otherwise
1256 xmlSwitchInputEncoding(xmlParserCtxtPtr ctxt, xmlParserInputPtr input,
1257 xmlCharEncodingHandlerPtr handler) {
1258 return(xmlSwitchInputEncodingInt(ctxt, input, handler, -1));
1262 * xmlSwitchToEncodingInt:
1263 * @ctxt: the parser context
1264 * @handler: the encoding handler
1265 * @len: the length to convert or -1
1267 * change the input functions when discovering the character encoding
1268 * of a given entity, and convert only @len bytes of the output, this
1269 * is needed on auto detect to allows any declared encoding later to
1270 * convert the actual content after the xmlDecl
1272 * Returns 0 in case of success, -1 otherwise
1275 xmlSwitchToEncodingInt(xmlParserCtxtPtr ctxt,
1276 xmlCharEncodingHandlerPtr handler, int len) {
1279 if (handler != NULL) {
1280 if (ctxt->input != NULL) {
1281 ret = xmlSwitchInputEncodingInt(ctxt, ctxt->input, handler, len);
1283 xmlErrInternal(ctxt, "xmlSwitchToEncoding : no input\n",
1288 * The parsing is now done in UTF8 natively
1290 ctxt->charset = XML_CHAR_ENCODING_UTF8;
1297 * xmlSwitchToEncoding:
1298 * @ctxt: the parser context
1299 * @handler: the encoding handler
1301 * change the input functions when discovering the character encoding
1302 * of a given entity.
1304 * Returns 0 in case of success, -1 otherwise
1307 xmlSwitchToEncoding(xmlParserCtxtPtr ctxt, xmlCharEncodingHandlerPtr handler)
1309 return (xmlSwitchToEncodingInt(ctxt, handler, -1));
1312 /************************************************************************
1314 * Commodity functions to handle entities processing *
1316 ************************************************************************/
1319 * xmlFreeInputStream:
1320 * @input: an xmlParserInputPtr
1322 * Free up an input stream.
1325 xmlFreeInputStream(xmlParserInputPtr input) {
1326 if (input == NULL) return;
1328 if (input->filename != NULL) xmlFree((char *) input->filename);
1329 if (input->directory != NULL) xmlFree((char *) input->directory);
1330 if (input->encoding != NULL) xmlFree((char *) input->encoding);
1331 if (input->version != NULL) xmlFree((char *) input->version);
1332 if ((input->free != NULL) && (input->base != NULL))
1333 input->free((xmlChar *) input->base);
1334 if (input->buf != NULL)
1335 xmlFreeParserInputBuffer(input->buf);
1340 * xmlNewInputStream:
1341 * @ctxt: an XML parser context
1343 * Create a new input stream structure.
1345 * Returns the new input stream or NULL
1348 xmlNewInputStream(xmlParserCtxtPtr ctxt) {
1349 xmlParserInputPtr input;
1351 input = (xmlParserInputPtr) xmlMalloc(sizeof(xmlParserInput));
1352 if (input == NULL) {
1353 xmlErrMemory(ctxt, "couldn't allocate a new input stream\n");
1356 memset(input, 0, sizeof(xmlParserInput));
1359 input->standalone = -1;
1362 * If the context is NULL the id cannot be initialized, but that
1363 * should not happen while parsing which is the situation where
1364 * the id is actually needed.
1367 input->id = ctxt->input_id++;
1373 * xmlNewIOInputStream:
1374 * @ctxt: an XML parser context
1375 * @input: an I/O Input
1376 * @enc: the charset encoding if known
1378 * Create a new input stream structure encapsulating the @input into
1379 * a stream suitable for the parser.
1381 * Returns the new input stream or NULL
1384 xmlNewIOInputStream(xmlParserCtxtPtr ctxt, xmlParserInputBufferPtr input,
1385 xmlCharEncoding enc) {
1386 xmlParserInputPtr inputStream;
1388 if (input == NULL) return(NULL);
1389 if (xmlParserDebugEntities)
1390 xmlGenericError(xmlGenericErrorContext, "new input from I/O\n");
1391 inputStream = xmlNewInputStream(ctxt);
1392 if (inputStream == NULL) {
1395 inputStream->filename = NULL;
1396 inputStream->buf = input;
1397 xmlBufResetInput(inputStream->buf->buffer, inputStream);
1399 if (enc != XML_CHAR_ENCODING_NONE) {
1400 xmlSwitchEncoding(ctxt, enc);
1403 return(inputStream);
1407 * xmlNewEntityInputStream:
1408 * @ctxt: an XML parser context
1409 * @entity: an Entity pointer
1411 * Create a new input stream based on an xmlEntityPtr
1413 * Returns the new input stream or NULL
1416 xmlNewEntityInputStream(xmlParserCtxtPtr ctxt, xmlEntityPtr entity) {
1417 xmlParserInputPtr input;
1419 if (entity == NULL) {
1420 xmlErrInternal(ctxt, "xmlNewEntityInputStream entity = NULL\n",
1424 if (xmlParserDebugEntities)
1425 xmlGenericError(xmlGenericErrorContext,
1426 "new input from entity: %s\n", entity->name);
1427 if (entity->content == NULL) {
1428 switch (entity->etype) {
1429 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
1430 xmlErrInternal(ctxt, "Cannot parse entity %s\n",
1433 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
1434 case XML_EXTERNAL_PARAMETER_ENTITY:
1435 return(xmlLoadExternalEntity((char *) entity->URI,
1436 (char *) entity->ExternalID, ctxt));
1437 case XML_INTERNAL_GENERAL_ENTITY:
1438 xmlErrInternal(ctxt,
1439 "Internal entity %s without content !\n",
1442 case XML_INTERNAL_PARAMETER_ENTITY:
1443 xmlErrInternal(ctxt,
1444 "Internal parameter entity %s without content !\n",
1447 case XML_INTERNAL_PREDEFINED_ENTITY:
1448 xmlErrInternal(ctxt,
1449 "Predefined entity %s without content !\n",
1455 input = xmlNewInputStream(ctxt);
1456 if (input == NULL) {
1459 if (entity->URI != NULL)
1460 input->filename = (char *) xmlStrdup((xmlChar *) entity->URI);
1461 input->base = entity->content;
1462 if (entity->length == 0)
1463 entity->length = xmlStrlen(entity->content);
1464 input->cur = entity->content;
1465 input->length = entity->length;
1466 input->end = &entity->content[input->length];
1471 * xmlNewStringInputStream:
1472 * @ctxt: an XML parser context
1473 * @buffer: an memory buffer
1475 * Create a new input stream based on a memory buffer.
1476 * Returns the new input stream
1479 xmlNewStringInputStream(xmlParserCtxtPtr ctxt, const xmlChar *buffer) {
1480 xmlParserInputPtr input;
1482 if (buffer == NULL) {
1483 xmlErrInternal(ctxt, "xmlNewStringInputStream string = NULL\n",
1487 if (xmlParserDebugEntities)
1488 xmlGenericError(xmlGenericErrorContext,
1489 "new fixed input: %.30s\n", buffer);
1490 input = xmlNewInputStream(ctxt);
1491 if (input == NULL) {
1492 xmlErrMemory(ctxt, "couldn't allocate a new input stream\n");
1495 input->base = buffer;
1496 input->cur = buffer;
1497 input->length = xmlStrlen(buffer);
1498 input->end = &buffer[input->length];
1503 * xmlNewInputFromFile:
1504 * @ctxt: an XML parser context
1505 * @filename: the filename to use as entity
1507 * Create a new input stream based on a file or an URL.
1509 * Returns the new input stream or NULL in case of error
1512 xmlNewInputFromFile(xmlParserCtxtPtr ctxt, const char *filename) {
1513 xmlParserInputBufferPtr buf;
1514 xmlParserInputPtr inputStream;
1515 char *directory = NULL;
1516 xmlChar *URI = NULL;
1518 if (xmlParserDebugEntities)
1519 xmlGenericError(xmlGenericErrorContext,
1520 "new input from file: %s\n", filename);
1521 if (ctxt == NULL) return(NULL);
1522 buf = xmlParserInputBufferCreateFilename(filename, XML_CHAR_ENCODING_NONE);
1524 if (filename == NULL)
1525 __xmlLoaderErr(ctxt,
1526 "failed to load external entity: NULL filename \n",
1529 __xmlLoaderErr(ctxt, "failed to load external entity \"%s\"\n",
1530 (const char *) filename);
1534 inputStream = xmlNewInputStream(ctxt);
1535 if (inputStream == NULL)
1538 inputStream->buf = buf;
1539 inputStream = xmlCheckHTTPInput(ctxt, inputStream);
1540 if (inputStream == NULL)
1543 if (inputStream->filename == NULL)
1544 URI = xmlStrdup((xmlChar *) filename);
1546 URI = xmlStrdup((xmlChar *) inputStream->filename);
1547 directory = xmlParserGetDirectory((const char *) URI);
1548 if (inputStream->filename != NULL) xmlFree((char *)inputStream->filename);
1549 inputStream->filename = (char *) xmlCanonicPath((const xmlChar *) URI);
1550 if (URI != NULL) xmlFree((char *) URI);
1551 inputStream->directory = directory;
1553 xmlBufResetInput(inputStream->buf->buffer, inputStream);
1554 if ((ctxt->directory == NULL) && (directory != NULL))
1555 ctxt->directory = (char *) xmlStrdup((const xmlChar *) directory);
1556 return(inputStream);
1559 /************************************************************************
1561 * Commodity functions to handle parser contexts *
1563 ************************************************************************/
1566 * xmlInitParserCtxt:
1567 * @ctxt: an XML parser context
1569 * Initialize a parser context
1571 * Returns 0 in case of success and -1 in case of error
1575 xmlInitParserCtxt(xmlParserCtxtPtr ctxt)
1577 xmlParserInputPtr input;
1580 xmlErrInternal(NULL, "Got NULL parser context\n", NULL);
1584 xmlDefaultSAXHandlerInit();
1586 if (ctxt->dict == NULL)
1587 ctxt->dict = xmlDictCreate();
1588 if (ctxt->dict == NULL) {
1589 xmlErrMemory(NULL, "cannot initialize parser context\n");
1592 xmlDictSetLimit(ctxt->dict, XML_MAX_DICTIONARY_LIMIT);
1594 if (ctxt->sax == NULL)
1595 ctxt->sax = (xmlSAXHandler *) xmlMalloc(sizeof(xmlSAXHandler));
1596 if (ctxt->sax == NULL) {
1597 xmlErrMemory(NULL, "cannot initialize parser context\n");
1601 xmlSAXVersion(ctxt->sax, 2);
1605 /* Allocate the Input stack */
1606 if (ctxt->inputTab == NULL) {
1607 ctxt->inputTab = (xmlParserInputPtr *)
1608 xmlMalloc(5 * sizeof(xmlParserInputPtr));
1611 if (ctxt->inputTab == NULL) {
1612 xmlErrMemory(NULL, "cannot initialize parser context\n");
1618 while ((input = inputPop(ctxt)) != NULL) { /* Non consuming */
1619 xmlFreeInputStream(input);
1624 ctxt->version = NULL;
1625 ctxt->encoding = NULL;
1626 ctxt->standalone = -1;
1627 ctxt->hasExternalSubset = 0;
1628 ctxt->hasPErefs = 0;
1631 ctxt->instate = XML_PARSER_START;
1633 ctxt->directory = NULL;
1635 /* Allocate the Node stack */
1636 if (ctxt->nodeTab == NULL) {
1637 ctxt->nodeTab = (xmlNodePtr *) xmlMalloc(10 * sizeof(xmlNodePtr));
1640 if (ctxt->nodeTab == NULL) {
1641 xmlErrMemory(NULL, "cannot initialize parser context\n");
1653 /* Allocate the Name stack */
1654 if (ctxt->nameTab == NULL) {
1655 ctxt->nameTab = (const xmlChar **) xmlMalloc(10 * sizeof(xmlChar *));
1658 if (ctxt->nameTab == NULL) {
1659 xmlErrMemory(NULL, "cannot initialize parser context\n");
1674 /* Allocate the space stack */
1675 if (ctxt->spaceTab == NULL) {
1676 ctxt->spaceTab = (int *) xmlMalloc(10 * sizeof(int));
1677 ctxt->spaceMax = 10;
1679 if (ctxt->spaceTab == NULL) {
1680 xmlErrMemory(NULL, "cannot initialize parser context\n");
1696 ctxt->spaceMax = 10;
1697 ctxt->spaceTab[0] = -1;
1698 ctxt->space = &ctxt->spaceTab[0];
1699 ctxt->userData = ctxt;
1701 ctxt->wellFormed = 1;
1702 ctxt->nsWellFormed = 1;
1704 ctxt->loadsubset = xmlLoadExtDtdDefaultValue;
1705 if (ctxt->loadsubset) {
1706 ctxt->options |= XML_PARSE_DTDLOAD;
1708 ctxt->validate = xmlDoValidityCheckingDefaultValue;
1709 ctxt->pedantic = xmlPedanticParserDefaultValue;
1710 if (ctxt->pedantic) {
1711 ctxt->options |= XML_PARSE_PEDANTIC;
1713 ctxt->linenumbers = xmlLineNumbersDefaultValue;
1714 ctxt->keepBlanks = xmlKeepBlanksDefaultValue;
1715 if (ctxt->keepBlanks == 0) {
1716 ctxt->sax->ignorableWhitespace = xmlSAX2IgnorableWhitespace;
1717 ctxt->options |= XML_PARSE_NOBLANKS;
1720 ctxt->vctxt.finishDtd = XML_CTXT_FINISH_DTD_0;
1721 ctxt->vctxt.userData = ctxt;
1722 ctxt->vctxt.error = xmlParserValidityError;
1723 ctxt->vctxt.warning = xmlParserValidityWarning;
1724 if (ctxt->validate) {
1725 if (xmlGetWarningsDefaultValue == 0)
1726 ctxt->vctxt.warning = NULL;
1728 ctxt->vctxt.warning = xmlParserValidityWarning;
1729 ctxt->vctxt.nodeMax = 0;
1730 ctxt->options |= XML_PARSE_DTDVALID;
1732 ctxt->replaceEntities = xmlSubstituteEntitiesDefaultValue;
1733 if (ctxt->replaceEntities) {
1734 ctxt->options |= XML_PARSE_NOENT;
1736 ctxt->record_info = 0;
1738 ctxt->checkIndex = 0;
1740 ctxt->errNo = XML_ERR_OK;
1742 ctxt->charset = XML_CHAR_ENCODING_UTF8;
1743 ctxt->catalogs = NULL;
1744 ctxt->nbentities = 0;
1745 ctxt->sizeentities = 0;
1746 ctxt->sizeentcopy = 0;
1748 xmlInitNodeInfoSeq(&ctxt->node_seq);
1753 * xmlFreeParserCtxt:
1754 * @ctxt: an XML parser context
1756 * Free all the memory used by a parser context. However the parsed
1757 * document in ctxt->myDoc is not freed.
1761 xmlFreeParserCtxt(xmlParserCtxtPtr ctxt)
1763 xmlParserInputPtr input;
1765 if (ctxt == NULL) return;
1767 while ((input = inputPop(ctxt)) != NULL) { /* Non consuming */
1768 xmlFreeInputStream(input);
1770 if (ctxt->spaceTab != NULL) xmlFree(ctxt->spaceTab);
1771 if (ctxt->nameTab != NULL) xmlFree((xmlChar * *)ctxt->nameTab);
1772 if (ctxt->nodeTab != NULL) xmlFree(ctxt->nodeTab);
1773 if (ctxt->nodeInfoTab != NULL) xmlFree(ctxt->nodeInfoTab);
1774 if (ctxt->inputTab != NULL) xmlFree(ctxt->inputTab);
1775 if (ctxt->version != NULL) xmlFree((char *) ctxt->version);
1776 if (ctxt->encoding != NULL) xmlFree((char *) ctxt->encoding);
1777 if (ctxt->extSubURI != NULL) xmlFree((char *) ctxt->extSubURI);
1778 if (ctxt->extSubSystem != NULL) xmlFree((char *) ctxt->extSubSystem);
1779 #ifdef LIBXML_SAX1_ENABLED
1780 if ((ctxt->sax != NULL) &&
1781 (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler))
1783 if (ctxt->sax != NULL)
1784 #endif /* LIBXML_SAX1_ENABLED */
1786 if (ctxt->directory != NULL) xmlFree((char *) ctxt->directory);
1787 if (ctxt->vctxt.nodeTab != NULL) xmlFree(ctxt->vctxt.nodeTab);
1788 if (ctxt->atts != NULL) xmlFree((xmlChar * *)ctxt->atts);
1789 if (ctxt->dict != NULL) xmlDictFree(ctxt->dict);
1790 if (ctxt->nsTab != NULL) xmlFree((char *) ctxt->nsTab);
1791 if (ctxt->pushTab != NULL) xmlFree(ctxt->pushTab);
1792 if (ctxt->attallocs != NULL) xmlFree(ctxt->attallocs);
1793 if (ctxt->attsDefault != NULL)
1794 xmlHashFree(ctxt->attsDefault, (xmlHashDeallocator) xmlFree);
1795 if (ctxt->attsSpecial != NULL)
1796 xmlHashFree(ctxt->attsSpecial, NULL);
1797 if (ctxt->freeElems != NULL) {
1798 xmlNodePtr cur, next;
1800 cur = ctxt->freeElems;
1801 while (cur != NULL) {
1807 if (ctxt->freeAttrs != NULL) {
1808 xmlAttrPtr cur, next;
1810 cur = ctxt->freeAttrs;
1811 while (cur != NULL) {
1818 * cleanup the error strings
1820 if (ctxt->lastError.message != NULL)
1821 xmlFree(ctxt->lastError.message);
1822 if (ctxt->lastError.file != NULL)
1823 xmlFree(ctxt->lastError.file);
1824 if (ctxt->lastError.str1 != NULL)
1825 xmlFree(ctxt->lastError.str1);
1826 if (ctxt->lastError.str2 != NULL)
1827 xmlFree(ctxt->lastError.str2);
1828 if (ctxt->lastError.str3 != NULL)
1829 xmlFree(ctxt->lastError.str3);
1831 #ifdef LIBXML_CATALOG_ENABLED
1832 if (ctxt->catalogs != NULL)
1833 xmlCatalogFreeLocal(ctxt->catalogs);
1841 * Allocate and initialize a new parser context.
1843 * Returns the xmlParserCtxtPtr or NULL
1847 xmlNewParserCtxt(void)
1849 xmlParserCtxtPtr ctxt;
1851 ctxt = (xmlParserCtxtPtr) xmlMalloc(sizeof(xmlParserCtxt));
1853 xmlErrMemory(NULL, "cannot allocate parser context\n");
1856 memset(ctxt, 0, sizeof(xmlParserCtxt));
1857 if (xmlInitParserCtxt(ctxt) < 0) {
1858 xmlFreeParserCtxt(ctxt);
1864 /************************************************************************
1866 * Handling of node informations *
1868 ************************************************************************/
1871 * xmlClearParserCtxt:
1872 * @ctxt: an XML parser context
1874 * Clear (release owned resources) and reinitialize a parser context
1878 xmlClearParserCtxt(xmlParserCtxtPtr ctxt)
1882 xmlClearNodeInfoSeq(&ctxt->node_seq);
1888 * xmlParserFindNodeInfo:
1889 * @ctx: an XML parser context
1890 * @node: an XML node within the tree
1892 * Find the parser node info struct for a given node
1894 * Returns an xmlParserNodeInfo block pointer or NULL
1896 const xmlParserNodeInfo *
1897 xmlParserFindNodeInfo(const xmlParserCtxtPtr ctx, const xmlNodePtr node)
1901 if ((ctx == NULL) || (node == NULL))
1903 /* Find position where node should be at */
1904 pos = xmlParserFindNodeInfoIndex(&ctx->node_seq, node);
1905 if (pos < ctx->node_seq.length
1906 && ctx->node_seq.buffer[pos].node == node)
1907 return &ctx->node_seq.buffer[pos];
1914 * xmlInitNodeInfoSeq:
1915 * @seq: a node info sequence pointer
1917 * -- Initialize (set to initial state) node info sequence
1920 xmlInitNodeInfoSeq(xmlParserNodeInfoSeqPtr seq)
1930 * xmlClearNodeInfoSeq:
1931 * @seq: a node info sequence pointer
1933 * -- Clear (release memory and reinitialize) node
1937 xmlClearNodeInfoSeq(xmlParserNodeInfoSeqPtr seq)
1941 if (seq->buffer != NULL)
1942 xmlFree(seq->buffer);
1943 xmlInitNodeInfoSeq(seq);
1947 * xmlParserFindNodeInfoIndex:
1948 * @seq: a node info sequence pointer
1949 * @node: an XML node pointer
1952 * xmlParserFindNodeInfoIndex : Find the index that the info record for
1953 * the given node is or should be at in a sorted sequence
1955 * Returns a long indicating the position of the record
1958 xmlParserFindNodeInfoIndex(const xmlParserNodeInfoSeqPtr seq,
1959 const xmlNodePtr node)
1961 unsigned long upper, lower, middle;
1964 if ((seq == NULL) || (node == NULL))
1965 return ((unsigned long) -1);
1967 /* Do a binary search for the key */
1969 upper = seq->length;
1971 while (lower <= upper && !found) {
1972 middle = lower + (upper - lower) / 2;
1973 if (node == seq->buffer[middle - 1].node)
1975 else if (node < seq->buffer[middle - 1].node)
1981 /* Return position */
1982 if (middle == 0 || seq->buffer[middle - 1].node < node)
1990 * xmlParserAddNodeInfo:
1991 * @ctxt: an XML parser context
1992 * @info: a node info sequence pointer
1994 * Insert node info record into the sorted sequence
1997 xmlParserAddNodeInfo(xmlParserCtxtPtr ctxt,
1998 const xmlParserNodeInfoPtr info)
2002 if ((ctxt == NULL) || (info == NULL)) return;
2004 /* Find pos and check to see if node is already in the sequence */
2005 pos = xmlParserFindNodeInfoIndex(&ctxt->node_seq, (xmlNodePtr)
2008 if ((pos < ctxt->node_seq.length) &&
2009 (ctxt->node_seq.buffer != NULL) &&
2010 (ctxt->node_seq.buffer[pos].node == info->node)) {
2011 ctxt->node_seq.buffer[pos] = *info;
2014 /* Otherwise, we need to add new node to buffer */
2016 if ((ctxt->node_seq.length + 1 > ctxt->node_seq.maximum) ||
2017 (ctxt->node_seq.buffer == NULL)) {
2018 xmlParserNodeInfo *tmp_buffer;
2019 unsigned int byte_size;
2021 if (ctxt->node_seq.maximum == 0)
2022 ctxt->node_seq.maximum = 2;
2023 byte_size = (sizeof(*ctxt->node_seq.buffer) *
2024 (2 * ctxt->node_seq.maximum));
2026 if (ctxt->node_seq.buffer == NULL)
2027 tmp_buffer = (xmlParserNodeInfo *) xmlMalloc(byte_size);
2030 (xmlParserNodeInfo *) xmlRealloc(ctxt->node_seq.buffer,
2033 if (tmp_buffer == NULL) {
2034 xmlErrMemory(ctxt, "failed to allocate buffer\n");
2037 ctxt->node_seq.buffer = tmp_buffer;
2038 ctxt->node_seq.maximum *= 2;
2041 /* If position is not at end, move elements out of the way */
2042 if (pos != ctxt->node_seq.length) {
2045 for (i = ctxt->node_seq.length; i > pos; i--)
2046 ctxt->node_seq.buffer[i] = ctxt->node_seq.buffer[i - 1];
2049 /* Copy element and increase length */
2050 ctxt->node_seq.buffer[pos] = *info;
2051 ctxt->node_seq.length++;
2055 /************************************************************************
2057 * Defaults settings *
2059 ************************************************************************/
2061 * xmlPedanticParserDefault:
2064 * Set and return the previous value for enabling pedantic warnings.
2066 * Returns the last value for 0 for no substitution, 1 for substitution.
2070 xmlPedanticParserDefault(int val) {
2071 int old = xmlPedanticParserDefaultValue;
2073 xmlPedanticParserDefaultValue = val;
2078 * xmlLineNumbersDefault:
2081 * Set and return the previous value for enabling line numbers in elements
2082 * contents. This may break on old application and is turned off by default.
2084 * Returns the last value for 0 for no substitution, 1 for substitution.
2088 xmlLineNumbersDefault(int val) {
2089 int old = xmlLineNumbersDefaultValue;
2091 xmlLineNumbersDefaultValue = val;
2096 * xmlSubstituteEntitiesDefault:
2099 * Set and return the previous value for default entity support.
2100 * Initially the parser always keep entity references instead of substituting
2101 * entity values in the output. This function has to be used to change the
2102 * default parser behavior
2103 * SAX::substituteEntities() has to be used for changing that on a file by
2106 * Returns the last value for 0 for no substitution, 1 for substitution.
2110 xmlSubstituteEntitiesDefault(int val) {
2111 int old = xmlSubstituteEntitiesDefaultValue;
2113 xmlSubstituteEntitiesDefaultValue = val;
2118 * xmlKeepBlanksDefault:
2121 * Set and return the previous value for default blanks text nodes support.
2122 * The 1.x version of the parser used an heuristic to try to detect
2123 * ignorable white spaces. As a result the SAX callback was generating
2124 * xmlSAX2IgnorableWhitespace() callbacks instead of characters() one, and when
2125 * using the DOM output text nodes containing those blanks were not generated.
2126 * The 2.x and later version will switch to the XML standard way and
2127 * ignorableWhitespace() are only generated when running the parser in
2128 * validating mode and when the current element doesn't allow CDATA or
2130 * This function is provided as a way to force the standard behavior
2131 * on 1.X libs and to switch back to the old mode for compatibility when
2132 * running 1.X client code on 2.X . Upgrade of 1.X code should be done
2133 * by using xmlIsBlankNode() commodity function to detect the "empty"
2135 * This value also affect autogeneration of indentation when saving code
2136 * if blanks sections are kept, indentation is not generated.
2138 * Returns the last value for 0 for no substitution, 1 for substitution.
2142 xmlKeepBlanksDefault(int val) {
2143 int old = xmlKeepBlanksDefaultValue;
2145 xmlKeepBlanksDefaultValue = val;
2146 if (!val) xmlIndentTreeOutput = 1;
2150 #define bottom_parserInternals
2151 #include "elfgcchack.h"