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>
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 informations
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 informations
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;
526 * Assume it's a fixed length encoding (1) with
527 * a compatible encoding for the ASCII set, since
528 * XML constructs only use < 128 chars
531 if (*(ctxt->input->cur) == '\n') {
532 ctxt->input->line++; ctxt->input->col = 1;
538 if (*ctxt->input->cur == 0)
539 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
543 * If we detect an UTF8 error that probably mean that the
544 * input encoding didn't get properly advertised in the
545 * declaration header. Report the error and switch the encoding
546 * to ISO-Latin-1 (if you don't like this policy, just declare the
549 if ((ctxt == NULL) || (ctxt->input == NULL) ||
550 (ctxt->input->end - ctxt->input->cur < 4)) {
551 __xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR,
552 "Input is not proper UTF-8, indicate encoding !\n",
557 snprintf(buffer, 149, "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n",
558 ctxt->input->cur[0], ctxt->input->cur[1],
559 ctxt->input->cur[2], ctxt->input->cur[3]);
560 __xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR,
561 "Input is not proper UTF-8, indicate encoding !\n%s",
562 BAD_CAST buffer, NULL);
564 ctxt->charset = XML_CHAR_ENCODING_8859_1;
571 * @ctxt: the XML parser context
572 * @len: pointer to the length of the char read
574 * The current char value, if using UTF-8 this may actually span multiple
575 * bytes in the input buffer. Implement the end of line normalization:
576 * 2.11 End-of-Line Handling
577 * Wherever an external parsed entity or the literal entity value
578 * of an internal parsed entity contains either the literal two-character
579 * sequence "#xD#xA" or a standalone literal #xD, an XML processor
580 * must pass to the application the single character #xA.
581 * This behavior can conveniently be produced by normalizing all
582 * line breaks to #xA on input, before parsing.)
584 * Returns the current char value and its length
588 xmlCurrentChar(xmlParserCtxtPtr ctxt, int *len) {
589 if ((ctxt == NULL) || (len == NULL) || (ctxt->input == NULL)) return(0);
590 if (ctxt->instate == XML_PARSER_EOF)
593 if ((*ctxt->input->cur >= 0x20) && (*ctxt->input->cur <= 0x7F)) {
595 return((int) *ctxt->input->cur);
597 if (ctxt->charset == XML_CHAR_ENCODING_UTF8) {
599 * We are supposed to handle UTF8, check it's valid
600 * From rfc2044: encoding of the Unicode values on UTF-8:
602 * UCS-4 range (hex.) UTF-8 octet sequence (binary)
603 * 0000 0000-0000 007F 0xxxxxxx
604 * 0000 0080-0000 07FF 110xxxxx 10xxxxxx
605 * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx
607 * Check for the 0x110000 limit too
609 const unsigned char *cur = ctxt->input->cur;
615 if (((c & 0x40) == 0) || (c == 0xC0))
618 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
619 cur = ctxt->input->cur;
621 if ((cur[1] & 0xc0) != 0x80)
623 if ((c & 0xe0) == 0xe0) {
625 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
626 cur = ctxt->input->cur;
628 if ((cur[2] & 0xc0) != 0x80)
630 if ((c & 0xf0) == 0xf0) {
632 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
633 cur = ctxt->input->cur;
635 if (((c & 0xf8) != 0xf0) ||
636 ((cur[3] & 0xc0) != 0x80))
640 val = (cur[0] & 0x7) << 18;
641 val |= (cur[1] & 0x3f) << 12;
642 val |= (cur[2] & 0x3f) << 6;
643 val |= cur[3] & 0x3f;
649 val = (cur[0] & 0xf) << 12;
650 val |= (cur[1] & 0x3f) << 6;
651 val |= cur[2] & 0x3f;
658 val = (cur[0] & 0x1f) << 6;
659 val |= cur[1] & 0x3f;
664 xmlErrEncodingInt(ctxt, XML_ERR_INVALID_CHAR,
665 "Char 0x%X out of allowed range\n", val);
671 if (*ctxt->input->cur == 0)
672 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
673 if ((*ctxt->input->cur == 0) &&
674 (ctxt->input->end > ctxt->input->cur)) {
675 xmlErrEncodingInt(ctxt, XML_ERR_INVALID_CHAR,
676 "Char 0x0 out of allowed range\n", 0);
678 if (*ctxt->input->cur == 0xD) {
679 if (ctxt->input->cur[1] == 0xA) {
685 return((int) *ctxt->input->cur);
689 * Assume it's a fixed length encoding (1) with
690 * a compatible encoding for the ASCII set, since
691 * XML constructs only use < 128 chars
694 if (*ctxt->input->cur == 0xD) {
695 if (ctxt->input->cur[1] == 0xA) {
701 return((int) *ctxt->input->cur);
704 * If we detect an UTF8 error that probably mean that the
705 * input encoding didn't get properly advertised in the
706 * declaration header. Report the error and switch the encoding
707 * to ISO-Latin-1 (if you don't like this policy, just declare the
713 if (ctxt->input->cur[1] == 0) {
714 snprintf(&buffer[0], 149, "Bytes: 0x%02X EOF\n",
715 ctxt->input->cur[0]);
716 } else if (ctxt->input->cur[2] == 0) {
717 snprintf(&buffer[0], 149, "Bytes: 0x%02X 0x%02X EOF\n",
718 ctxt->input->cur[0], ctxt->input->cur[1]);
719 } else if (ctxt->input->cur[3] == 0) {
720 snprintf(&buffer[0], 149, "Bytes: 0x%02X 0x%02X 0x%02X EOF\n",
721 ctxt->input->cur[0], ctxt->input->cur[1],
722 ctxt->input->cur[2]);
724 snprintf(&buffer[0], 149, "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n",
725 ctxt->input->cur[0], ctxt->input->cur[1],
726 ctxt->input->cur[2], ctxt->input->cur[3]);
728 __xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR,
729 "Input is not proper UTF-8, indicate encoding !\n%s",
730 BAD_CAST buffer, NULL);
732 ctxt->charset = XML_CHAR_ENCODING_8859_1;
734 return((int) *ctxt->input->cur);
738 * xmlStringCurrentChar:
739 * @ctxt: the XML parser context
740 * @cur: pointer to the beginning of the char
741 * @len: pointer to the length of the char read
743 * The current char value, if using UTF-8 this may actually span multiple
744 * bytes in the input buffer.
746 * Returns the current char value and its length
750 xmlStringCurrentChar(xmlParserCtxtPtr ctxt, const xmlChar * cur, int *len)
752 if ((len == NULL) || (cur == NULL)) return(0);
753 if ((ctxt == NULL) || (ctxt->charset == XML_CHAR_ENCODING_UTF8)) {
755 * We are supposed to handle UTF8, check it's valid
756 * From rfc2044: encoding of the Unicode values on UTF-8:
758 * UCS-4 range (hex.) UTF-8 octet sequence (binary)
759 * 0000 0000-0000 007F 0xxxxxxx
760 * 0000 0080-0000 07FF 110xxxxx 10xxxxxx
761 * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx
763 * Check for the 0x110000 limit too
770 if ((cur[1] & 0xc0) != 0x80)
772 if ((c & 0xe0) == 0xe0) {
774 if ((cur[2] & 0xc0) != 0x80)
776 if ((c & 0xf0) == 0xf0) {
777 if (((c & 0xf8) != 0xf0) || ((cur[3] & 0xc0) != 0x80))
781 val = (cur[0] & 0x7) << 18;
782 val |= (cur[1] & 0x3f) << 12;
783 val |= (cur[2] & 0x3f) << 6;
784 val |= cur[3] & 0x3f;
788 val = (cur[0] & 0xf) << 12;
789 val |= (cur[1] & 0x3f) << 6;
790 val |= cur[2] & 0x3f;
795 val = (cur[0] & 0x1f) << 6;
796 val |= cur[1] & 0x3f;
799 xmlErrEncodingInt(ctxt, XML_ERR_INVALID_CHAR,
800 "Char 0x%X out of allowed range\n", val);
810 * Assume it's a fixed length encoding (1) with
811 * a compatible encoding for the ASCII set, since
812 * XML constructs only use < 128 chars
818 * If we detect an UTF8 error that probably mean that the
819 * input encoding didn't get properly advertised in the
820 * declaration header. Report the error and switch the encoding
821 * to ISO-Latin-1 (if you don't like this policy, just declare the
828 snprintf(&buffer[0], 149, "Bytes: 0x%02X EOF\n",
830 } else if (cur[2] == 0) {
831 snprintf(&buffer[0], 149, "Bytes: 0x%02X 0x%02X EOF\n",
833 } else if (cur[3] == 0) {
834 snprintf(&buffer[0], 149, "Bytes: 0x%02X 0x%02X 0x%02X EOF\n",
835 cur[0], cur[1], cur[2]);
837 snprintf(&buffer[0], 149, "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n",
838 cur[0], cur[1], cur[2], cur[3]);
840 __xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR,
841 "Input is not proper UTF-8, indicate encoding !\n%s",
842 BAD_CAST buffer, NULL);
849 * xmlCopyCharMultiByte:
850 * @out: pointer to an array of xmlChar
851 * @val: the char value
853 * append the char value in the array
855 * Returns the number of xmlChar written
858 xmlCopyCharMultiByte(xmlChar *out, int val) {
859 if (out == NULL) return(0);
861 * We are supposed to handle UTF8, check it's valid
862 * From rfc2044: encoding of the Unicode values on UTF-8:
864 * UCS-4 range (hex.) UTF-8 octet sequence (binary)
865 * 0000 0000-0000 007F 0xxxxxxx
866 * 0000 0080-0000 07FF 110xxxxx 10xxxxxx
867 * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx
870 xmlChar *savedout = out;
872 if (val < 0x800) { *out++= (val >> 6) | 0xC0; bits= 0; }
873 else if (val < 0x10000) { *out++= (val >> 12) | 0xE0; bits= 6;}
874 else if (val < 0x110000) { *out++= (val >> 18) | 0xF0; bits= 12; }
876 xmlErrEncodingInt(NULL, XML_ERR_INVALID_CHAR,
877 "Internal error, xmlCopyCharMultiByte 0x%X out of bound\n",
881 for ( ; bits >= 0; bits-= 6)
882 *out++= ((val >> bits) & 0x3F) | 0x80 ;
883 return (out - savedout);
885 *out = (xmlChar) val;
891 * @len: Ignored, compatibility
892 * @out: pointer to an array of xmlChar
893 * @val: the char value
895 * append the char value in the array
897 * Returns the number of xmlChar written
901 xmlCopyChar(int len ATTRIBUTE_UNUSED, xmlChar *out, int val) {
902 if (out == NULL) return(0);
903 /* the len parameter is ignored */
905 return(xmlCopyCharMultiByte (out, val));
907 *out = (xmlChar) val;
911 /************************************************************************
913 * Commodity functions to switch encodings *
915 ************************************************************************/
918 xmlSwitchToEncodingInt(xmlParserCtxtPtr ctxt,
919 xmlCharEncodingHandlerPtr handler, int len);
921 xmlSwitchInputEncodingInt(xmlParserCtxtPtr ctxt, xmlParserInputPtr input,
922 xmlCharEncodingHandlerPtr handler, int len);
925 * @ctxt: the parser context
926 * @enc: the encoding value (number)
928 * change the input functions when discovering the character encoding
931 * Returns 0 in case of success, -1 otherwise
934 xmlSwitchEncoding(xmlParserCtxtPtr ctxt, xmlCharEncoding enc)
936 xmlCharEncodingHandlerPtr handler;
940 if (ctxt == NULL) return(-1);
942 case XML_CHAR_ENCODING_ERROR:
943 __xmlErrEncoding(ctxt, XML_ERR_UNKNOWN_ENCODING,
944 "encoding unknown\n", NULL, NULL);
946 case XML_CHAR_ENCODING_NONE:
947 /* let's assume it's UTF-8 without the XML decl */
948 ctxt->charset = XML_CHAR_ENCODING_UTF8;
950 case XML_CHAR_ENCODING_UTF8:
951 /* default encoding, no conversion should be needed */
952 ctxt->charset = XML_CHAR_ENCODING_UTF8;
955 * Errata on XML-1.0 June 20 2001
956 * Specific handling of the Byte Order Mark for
959 if ((ctxt->input != NULL) &&
960 (ctxt->input->cur[0] == 0xEF) &&
961 (ctxt->input->cur[1] == 0xBB) &&
962 (ctxt->input->cur[2] == 0xBF)) {
963 ctxt->input->cur += 3;
966 case XML_CHAR_ENCODING_UTF16LE:
967 case XML_CHAR_ENCODING_UTF16BE:
968 /*The raw input characters are encoded
969 *in UTF-16. As we expect this function
970 *to be called after xmlCharEncInFunc, we expect
971 *ctxt->input->cur to contain UTF-8 encoded characters.
972 *So the raw UTF16 Byte Order Mark
973 *has also been converted into
974 *an UTF-8 BOM. Let's skip that BOM.
976 if ((ctxt->input != NULL) && (ctxt->input->cur != NULL) &&
977 (ctxt->input->cur[0] == 0xEF) &&
978 (ctxt->input->cur[1] == 0xBB) &&
979 (ctxt->input->cur[2] == 0xBF)) {
980 ctxt->input->cur += 3;
984 case XML_CHAR_ENCODING_UCS2:
987 case XML_CHAR_ENCODING_UCS4BE:
988 case XML_CHAR_ENCODING_UCS4LE:
989 case XML_CHAR_ENCODING_UCS4_2143:
990 case XML_CHAR_ENCODING_UCS4_3412:
993 case XML_CHAR_ENCODING_EBCDIC:
994 case XML_CHAR_ENCODING_8859_1:
995 case XML_CHAR_ENCODING_8859_2:
996 case XML_CHAR_ENCODING_8859_3:
997 case XML_CHAR_ENCODING_8859_4:
998 case XML_CHAR_ENCODING_8859_5:
999 case XML_CHAR_ENCODING_8859_6:
1000 case XML_CHAR_ENCODING_8859_7:
1001 case XML_CHAR_ENCODING_8859_8:
1002 case XML_CHAR_ENCODING_8859_9:
1003 case XML_CHAR_ENCODING_ASCII:
1004 case XML_CHAR_ENCODING_2022_JP:
1005 case XML_CHAR_ENCODING_SHIFT_JIS:
1006 case XML_CHAR_ENCODING_EUC_JP:
1010 handler = xmlGetCharEncodingHandler(enc);
1011 if (handler == NULL) {
1016 case XML_CHAR_ENCODING_ASCII:
1017 /* default encoding, no conversion should be needed */
1018 ctxt->charset = XML_CHAR_ENCODING_UTF8;
1020 case XML_CHAR_ENCODING_UTF16LE:
1022 case XML_CHAR_ENCODING_UTF16BE:
1024 case XML_CHAR_ENCODING_UCS4LE:
1025 __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
1026 "encoding not supported %s\n",
1027 BAD_CAST "USC4 little endian", NULL);
1029 case XML_CHAR_ENCODING_UCS4BE:
1030 __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
1031 "encoding not supported %s\n",
1032 BAD_CAST "USC4 big endian", NULL);
1034 case XML_CHAR_ENCODING_EBCDIC:
1035 __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
1036 "encoding not supported %s\n",
1037 BAD_CAST "EBCDIC", NULL);
1039 case XML_CHAR_ENCODING_UCS4_2143:
1040 __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
1041 "encoding not supported %s\n",
1042 BAD_CAST "UCS4 2143", NULL);
1044 case XML_CHAR_ENCODING_UCS4_3412:
1045 __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
1046 "encoding not supported %s\n",
1047 BAD_CAST "UCS4 3412", NULL);
1049 case XML_CHAR_ENCODING_UCS2:
1050 __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
1051 "encoding not supported %s\n",
1052 BAD_CAST "UCS2", NULL);
1054 case XML_CHAR_ENCODING_8859_1:
1055 case XML_CHAR_ENCODING_8859_2:
1056 case XML_CHAR_ENCODING_8859_3:
1057 case XML_CHAR_ENCODING_8859_4:
1058 case XML_CHAR_ENCODING_8859_5:
1059 case XML_CHAR_ENCODING_8859_6:
1060 case XML_CHAR_ENCODING_8859_7:
1061 case XML_CHAR_ENCODING_8859_8:
1062 case XML_CHAR_ENCODING_8859_9:
1064 * We used to keep the internal content in the
1065 * document encoding however this turns being unmaintainable
1066 * So xmlGetCharEncodingHandler() will return non-null
1067 * values for this now.
1069 if ((ctxt->inputNr == 1) &&
1070 (ctxt->encoding == NULL) &&
1071 (ctxt->input != NULL) &&
1072 (ctxt->input->encoding != NULL)) {
1073 ctxt->encoding = xmlStrdup(ctxt->input->encoding);
1075 ctxt->charset = enc;
1077 case XML_CHAR_ENCODING_2022_JP:
1078 __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
1079 "encoding not supported %s\n",
1080 BAD_CAST "ISO-2022-JP", NULL);
1082 case XML_CHAR_ENCODING_SHIFT_JIS:
1083 __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
1084 "encoding not supported %s\n",
1085 BAD_CAST "Shift_JIS", NULL);
1087 case XML_CHAR_ENCODING_EUC_JP:
1088 __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
1089 "encoding not supported %s\n",
1090 BAD_CAST "EUC-JP", NULL);
1097 * TODO: We could recover from errors in external entites if we
1098 * didn't stop the parser. But most callers of this function don't
1099 * check the return value.
1101 if (handler == NULL) {
1102 xmlStopParser(ctxt);
1105 ctxt->charset = XML_CHAR_ENCODING_UTF8;
1106 ret = xmlSwitchToEncodingInt(ctxt, handler, len);
1107 if ((ret < 0) || (ctxt->errNo == XML_I18N_CONV_FAILED)) {
1109 * on encoding conversion errors, stop the parser
1111 xmlStopParser(ctxt);
1112 ctxt->errNo = XML_I18N_CONV_FAILED;
1118 * xmlSwitchInputEncoding:
1119 * @ctxt: the parser context
1120 * @input: the input stream
1121 * @handler: the encoding handler
1122 * @len: the number of bytes to convert for the first line or -1
1124 * change the input functions when discovering the character encoding
1125 * of a given entity.
1127 * Returns 0 in case of success, -1 otherwise
1130 xmlSwitchInputEncodingInt(xmlParserCtxtPtr ctxt, xmlParserInputPtr input,
1131 xmlCharEncodingHandlerPtr handler, int len)
1135 if (handler == NULL)
1139 if (input->buf != NULL) {
1140 if (input->buf->encoder != NULL) {
1142 * Check in case the auto encoding detetection triggered
1145 if (input->buf->encoder == handler)
1149 * "UTF-16" can be used for both LE and BE
1150 if ((!xmlStrncmp(BAD_CAST input->buf->encoder->name,
1151 BAD_CAST "UTF-16", 6)) &&
1152 (!xmlStrncmp(BAD_CAST handler->name,
1153 BAD_CAST "UTF-16", 6))) {
1159 * Note: this is a bit dangerous, but that's what it
1160 * takes to use nearly compatible signature for different
1163 xmlCharEncCloseFunc(input->buf->encoder);
1164 input->buf->encoder = handler;
1167 input->buf->encoder = handler;
1170 * Is there already some content down the pipe to convert ?
1172 if (xmlBufIsEmpty(input->buf->buffer) == 0) {
1177 * Specific handling of the Byte Order Mark for
1180 if ((handler->name != NULL) &&
1181 (!strcmp(handler->name, "UTF-16LE") ||
1182 !strcmp(handler->name, "UTF-16")) &&
1183 (input->cur[0] == 0xFF) && (input->cur[1] == 0xFE)) {
1186 if ((handler->name != NULL) &&
1187 (!strcmp(handler->name, "UTF-16BE")) &&
1188 (input->cur[0] == 0xFE) && (input->cur[1] == 0xFF)) {
1192 * Errata on XML-1.0 June 20 2001
1193 * Specific handling of the Byte Order Mark for
1196 if ((handler->name != NULL) &&
1197 (!strcmp(handler->name, "UTF-8")) &&
1198 (input->cur[0] == 0xEF) &&
1199 (input->cur[1] == 0xBB) && (input->cur[2] == 0xBF)) {
1204 * Shrink the current input buffer.
1205 * Move it as the raw buffer and create a new input buffer
1207 processed = input->cur - input->base;
1208 xmlBufShrink(input->buf->buffer, processed);
1209 input->buf->raw = input->buf->buffer;
1210 input->buf->buffer = xmlBufCreate();
1211 input->buf->rawconsumed = processed;
1212 use = xmlBufUse(input->buf->raw);
1216 * convert as much as possible of the buffer
1218 nbchars = xmlCharEncInput(input->buf, 1);
1221 * convert just enough to get
1222 * '<?xml version="1.0" encoding="xxx"?>'
1223 * parsed with the autodetected encoding
1224 * into the parser reading buffer.
1226 nbchars = xmlCharEncFirstLineInput(input->buf, len);
1228 xmlBufResetInput(input->buf->buffer, input);
1230 xmlErrInternal(ctxt,
1231 "switching encoding: encoder error\n",
1235 input->buf->rawconsumed += use - xmlBufUse(input->buf->raw);
1238 } else if (input->length == 0) {
1240 * When parsing a static memory array one must know the
1241 * size to be able to convert the buffer.
1243 xmlErrInternal(ctxt, "switching encoding : no input\n", NULL);
1250 * xmlSwitchInputEncoding:
1251 * @ctxt: the parser context
1252 * @input: the input stream
1253 * @handler: the encoding handler
1255 * change the input functions when discovering the character encoding
1256 * of a given entity.
1258 * Returns 0 in case of success, -1 otherwise
1261 xmlSwitchInputEncoding(xmlParserCtxtPtr ctxt, xmlParserInputPtr input,
1262 xmlCharEncodingHandlerPtr handler) {
1263 return(xmlSwitchInputEncodingInt(ctxt, input, handler, -1));
1267 * xmlSwitchToEncodingInt:
1268 * @ctxt: the parser context
1269 * @handler: the encoding handler
1270 * @len: the length to convert or -1
1272 * change the input functions when discovering the character encoding
1273 * of a given entity, and convert only @len bytes of the output, this
1274 * is needed on auto detect to allows any declared encoding later to
1275 * convert the actual content after the xmlDecl
1277 * Returns 0 in case of success, -1 otherwise
1280 xmlSwitchToEncodingInt(xmlParserCtxtPtr ctxt,
1281 xmlCharEncodingHandlerPtr handler, int len) {
1284 if (handler != NULL) {
1285 if (ctxt->input != NULL) {
1286 ret = xmlSwitchInputEncodingInt(ctxt, ctxt->input, handler, len);
1288 xmlErrInternal(ctxt, "xmlSwitchToEncoding : no input\n",
1293 * The parsing is now done in UTF8 natively
1295 ctxt->charset = XML_CHAR_ENCODING_UTF8;
1302 * xmlSwitchToEncoding:
1303 * @ctxt: the parser context
1304 * @handler: the encoding handler
1306 * change the input functions when discovering the character encoding
1307 * of a given entity.
1309 * Returns 0 in case of success, -1 otherwise
1312 xmlSwitchToEncoding(xmlParserCtxtPtr ctxt, xmlCharEncodingHandlerPtr handler)
1314 return (xmlSwitchToEncodingInt(ctxt, handler, -1));
1317 /************************************************************************
1319 * Commodity functions to handle entities processing *
1321 ************************************************************************/
1324 * xmlFreeInputStream:
1325 * @input: an xmlParserInputPtr
1327 * Free up an input stream.
1330 xmlFreeInputStream(xmlParserInputPtr input) {
1331 if (input == NULL) return;
1333 if (input->filename != NULL) xmlFree((char *) input->filename);
1334 if (input->directory != NULL) xmlFree((char *) input->directory);
1335 if (input->encoding != NULL) xmlFree((char *) input->encoding);
1336 if (input->version != NULL) xmlFree((char *) input->version);
1337 if ((input->free != NULL) && (input->base != NULL))
1338 input->free((xmlChar *) input->base);
1339 if (input->buf != NULL)
1340 xmlFreeParserInputBuffer(input->buf);
1345 * xmlNewInputStream:
1346 * @ctxt: an XML parser context
1348 * Create a new input stream structure.
1350 * Returns the new input stream or NULL
1353 xmlNewInputStream(xmlParserCtxtPtr ctxt) {
1354 xmlParserInputPtr input;
1356 input = (xmlParserInputPtr) xmlMalloc(sizeof(xmlParserInput));
1357 if (input == NULL) {
1358 xmlErrMemory(ctxt, "couldn't allocate a new input stream\n");
1361 memset(input, 0, sizeof(xmlParserInput));
1364 input->standalone = -1;
1367 * If the context is NULL the id cannot be initialized, but that
1368 * should not happen while parsing which is the situation where
1369 * the id is actually needed.
1372 input->id = ctxt->input_id++;
1378 * xmlNewIOInputStream:
1379 * @ctxt: an XML parser context
1380 * @input: an I/O Input
1381 * @enc: the charset encoding if known
1383 * Create a new input stream structure encapsulating the @input into
1384 * a stream suitable for the parser.
1386 * Returns the new input stream or NULL
1389 xmlNewIOInputStream(xmlParserCtxtPtr ctxt, xmlParserInputBufferPtr input,
1390 xmlCharEncoding enc) {
1391 xmlParserInputPtr inputStream;
1393 if (input == NULL) return(NULL);
1394 if (xmlParserDebugEntities)
1395 xmlGenericError(xmlGenericErrorContext, "new input from I/O\n");
1396 inputStream = xmlNewInputStream(ctxt);
1397 if (inputStream == NULL) {
1400 inputStream->filename = NULL;
1401 inputStream->buf = input;
1402 xmlBufResetInput(inputStream->buf->buffer, inputStream);
1404 if (enc != XML_CHAR_ENCODING_NONE) {
1405 xmlSwitchEncoding(ctxt, enc);
1408 return(inputStream);
1412 * xmlNewEntityInputStream:
1413 * @ctxt: an XML parser context
1414 * @entity: an Entity pointer
1416 * Create a new input stream based on an xmlEntityPtr
1418 * Returns the new input stream or NULL
1421 xmlNewEntityInputStream(xmlParserCtxtPtr ctxt, xmlEntityPtr entity) {
1422 xmlParserInputPtr input;
1424 if (entity == NULL) {
1425 xmlErrInternal(ctxt, "xmlNewEntityInputStream entity = NULL\n",
1429 if (xmlParserDebugEntities)
1430 xmlGenericError(xmlGenericErrorContext,
1431 "new input from entity: %s\n", entity->name);
1432 if (entity->content == NULL) {
1433 switch (entity->etype) {
1434 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
1435 xmlErrInternal(ctxt, "Cannot parse entity %s\n",
1438 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
1439 case XML_EXTERNAL_PARAMETER_ENTITY:
1440 return(xmlLoadExternalEntity((char *) entity->URI,
1441 (char *) entity->ExternalID, ctxt));
1442 case XML_INTERNAL_GENERAL_ENTITY:
1443 xmlErrInternal(ctxt,
1444 "Internal entity %s without content !\n",
1447 case XML_INTERNAL_PARAMETER_ENTITY:
1448 xmlErrInternal(ctxt,
1449 "Internal parameter entity %s without content !\n",
1452 case XML_INTERNAL_PREDEFINED_ENTITY:
1453 xmlErrInternal(ctxt,
1454 "Predefined entity %s without content !\n",
1460 input = xmlNewInputStream(ctxt);
1461 if (input == NULL) {
1464 if (entity->URI != NULL)
1465 input->filename = (char *) xmlStrdup((xmlChar *) entity->URI);
1466 input->base = entity->content;
1467 if (entity->length == 0)
1468 entity->length = xmlStrlen(entity->content);
1469 input->cur = entity->content;
1470 input->length = entity->length;
1471 input->end = &entity->content[input->length];
1476 * xmlNewStringInputStream:
1477 * @ctxt: an XML parser context
1478 * @buffer: an memory buffer
1480 * Create a new input stream based on a memory buffer.
1481 * Returns the new input stream
1484 xmlNewStringInputStream(xmlParserCtxtPtr ctxt, const xmlChar *buffer) {
1485 xmlParserInputPtr input;
1487 if (buffer == NULL) {
1488 xmlErrInternal(ctxt, "xmlNewStringInputStream string = NULL\n",
1492 if (xmlParserDebugEntities)
1493 xmlGenericError(xmlGenericErrorContext,
1494 "new fixed input: %.30s\n", buffer);
1495 input = xmlNewInputStream(ctxt);
1496 if (input == NULL) {
1497 xmlErrMemory(ctxt, "couldn't allocate a new input stream\n");
1500 input->base = buffer;
1501 input->cur = buffer;
1502 input->length = xmlStrlen(buffer);
1503 input->end = &buffer[input->length];
1508 * xmlNewInputFromFile:
1509 * @ctxt: an XML parser context
1510 * @filename: the filename to use as entity
1512 * Create a new input stream based on a file or an URL.
1514 * Returns the new input stream or NULL in case of error
1517 xmlNewInputFromFile(xmlParserCtxtPtr ctxt, const char *filename) {
1518 xmlParserInputBufferPtr buf;
1519 xmlParserInputPtr inputStream;
1520 char *directory = NULL;
1521 xmlChar *URI = NULL;
1523 if (xmlParserDebugEntities)
1524 xmlGenericError(xmlGenericErrorContext,
1525 "new input from file: %s\n", filename);
1526 if (ctxt == NULL) return(NULL);
1527 buf = xmlParserInputBufferCreateFilename(filename, XML_CHAR_ENCODING_NONE);
1529 if (filename == NULL)
1530 __xmlLoaderErr(ctxt,
1531 "failed to load external entity: NULL filename \n",
1534 __xmlLoaderErr(ctxt, "failed to load external entity \"%s\"\n",
1535 (const char *) filename);
1539 inputStream = xmlNewInputStream(ctxt);
1540 if (inputStream == NULL)
1543 inputStream->buf = buf;
1544 inputStream = xmlCheckHTTPInput(ctxt, inputStream);
1545 if (inputStream == NULL)
1548 if (inputStream->filename == NULL)
1549 URI = xmlStrdup((xmlChar *) filename);
1551 URI = xmlStrdup((xmlChar *) inputStream->filename);
1552 directory = xmlParserGetDirectory((const char *) URI);
1553 if (inputStream->filename != NULL) xmlFree((char *)inputStream->filename);
1554 inputStream->filename = (char *) xmlCanonicPath((const xmlChar *) URI);
1555 if (URI != NULL) xmlFree((char *) URI);
1556 inputStream->directory = directory;
1558 xmlBufResetInput(inputStream->buf->buffer, inputStream);
1559 if ((ctxt->directory == NULL) && (directory != NULL))
1560 ctxt->directory = (char *) xmlStrdup((const xmlChar *) directory);
1561 return(inputStream);
1564 /************************************************************************
1566 * Commodity functions to handle parser contexts *
1568 ************************************************************************/
1571 * xmlInitParserCtxt:
1572 * @ctxt: an XML parser context
1574 * Initialize a parser context
1576 * Returns 0 in case of success and -1 in case of error
1580 xmlInitParserCtxt(xmlParserCtxtPtr ctxt)
1582 xmlParserInputPtr input;
1585 xmlErrInternal(NULL, "Got NULL parser context\n", NULL);
1589 xmlDefaultSAXHandlerInit();
1591 if (ctxt->dict == NULL)
1592 ctxt->dict = xmlDictCreate();
1593 if (ctxt->dict == NULL) {
1594 xmlErrMemory(NULL, "cannot initialize parser context\n");
1597 xmlDictSetLimit(ctxt->dict, XML_MAX_DICTIONARY_LIMIT);
1599 if (ctxt->sax == NULL)
1600 ctxt->sax = (xmlSAXHandler *) xmlMalloc(sizeof(xmlSAXHandler));
1601 if (ctxt->sax == NULL) {
1602 xmlErrMemory(NULL, "cannot initialize parser context\n");
1606 xmlSAXVersion(ctxt->sax, 2);
1610 /* Allocate the Input stack */
1611 if (ctxt->inputTab == NULL) {
1612 ctxt->inputTab = (xmlParserInputPtr *)
1613 xmlMalloc(5 * sizeof(xmlParserInputPtr));
1616 if (ctxt->inputTab == NULL) {
1617 xmlErrMemory(NULL, "cannot initialize parser context\n");
1623 while ((input = inputPop(ctxt)) != NULL) { /* Non consuming */
1624 xmlFreeInputStream(input);
1629 ctxt->version = NULL;
1630 ctxt->encoding = NULL;
1631 ctxt->standalone = -1;
1632 ctxt->hasExternalSubset = 0;
1633 ctxt->hasPErefs = 0;
1636 ctxt->instate = XML_PARSER_START;
1638 ctxt->directory = NULL;
1640 /* Allocate the Node stack */
1641 if (ctxt->nodeTab == NULL) {
1642 ctxt->nodeTab = (xmlNodePtr *) xmlMalloc(10 * sizeof(xmlNodePtr));
1645 if (ctxt->nodeTab == NULL) {
1646 xmlErrMemory(NULL, "cannot initialize parser context\n");
1658 /* Allocate the Name stack */
1659 if (ctxt->nameTab == NULL) {
1660 ctxt->nameTab = (const xmlChar **) xmlMalloc(10 * sizeof(xmlChar *));
1663 if (ctxt->nameTab == NULL) {
1664 xmlErrMemory(NULL, "cannot initialize parser context\n");
1679 /* Allocate the space stack */
1680 if (ctxt->spaceTab == NULL) {
1681 ctxt->spaceTab = (int *) xmlMalloc(10 * sizeof(int));
1682 ctxt->spaceMax = 10;
1684 if (ctxt->spaceTab == NULL) {
1685 xmlErrMemory(NULL, "cannot initialize parser context\n");
1701 ctxt->spaceMax = 10;
1702 ctxt->spaceTab[0] = -1;
1703 ctxt->space = &ctxt->spaceTab[0];
1704 ctxt->userData = ctxt;
1706 ctxt->wellFormed = 1;
1707 ctxt->nsWellFormed = 1;
1709 ctxt->loadsubset = xmlLoadExtDtdDefaultValue;
1710 if (ctxt->loadsubset) {
1711 ctxt->options |= XML_PARSE_DTDLOAD;
1713 ctxt->validate = xmlDoValidityCheckingDefaultValue;
1714 ctxt->pedantic = xmlPedanticParserDefaultValue;
1715 if (ctxt->pedantic) {
1716 ctxt->options |= XML_PARSE_PEDANTIC;
1718 ctxt->linenumbers = xmlLineNumbersDefaultValue;
1719 ctxt->keepBlanks = xmlKeepBlanksDefaultValue;
1720 if (ctxt->keepBlanks == 0) {
1721 ctxt->sax->ignorableWhitespace = xmlSAX2IgnorableWhitespace;
1722 ctxt->options |= XML_PARSE_NOBLANKS;
1725 ctxt->vctxt.finishDtd = XML_CTXT_FINISH_DTD_0;
1726 ctxt->vctxt.userData = ctxt;
1727 ctxt->vctxt.error = xmlParserValidityError;
1728 ctxt->vctxt.warning = xmlParserValidityWarning;
1729 if (ctxt->validate) {
1730 if (xmlGetWarningsDefaultValue == 0)
1731 ctxt->vctxt.warning = NULL;
1733 ctxt->vctxt.warning = xmlParserValidityWarning;
1734 ctxt->vctxt.nodeMax = 0;
1735 ctxt->options |= XML_PARSE_DTDVALID;
1737 ctxt->replaceEntities = xmlSubstituteEntitiesDefaultValue;
1738 if (ctxt->replaceEntities) {
1739 ctxt->options |= XML_PARSE_NOENT;
1741 ctxt->record_info = 0;
1743 ctxt->checkIndex = 0;
1745 ctxt->errNo = XML_ERR_OK;
1747 ctxt->charset = XML_CHAR_ENCODING_UTF8;
1748 ctxt->catalogs = NULL;
1749 ctxt->nbentities = 0;
1750 ctxt->sizeentities = 0;
1751 ctxt->sizeentcopy = 0;
1753 xmlInitNodeInfoSeq(&ctxt->node_seq);
1758 * xmlFreeParserCtxt:
1759 * @ctxt: an XML parser context
1761 * Free all the memory used by a parser context. However the parsed
1762 * document in ctxt->myDoc is not freed.
1766 xmlFreeParserCtxt(xmlParserCtxtPtr ctxt)
1768 xmlParserInputPtr input;
1770 if (ctxt == NULL) return;
1772 while ((input = inputPop(ctxt)) != NULL) { /* Non consuming */
1773 xmlFreeInputStream(input);
1775 if (ctxt->spaceTab != NULL) xmlFree(ctxt->spaceTab);
1776 if (ctxt->nameTab != NULL) xmlFree((xmlChar * *)ctxt->nameTab);
1777 if (ctxt->nodeTab != NULL) xmlFree(ctxt->nodeTab);
1778 if (ctxt->nodeInfoTab != NULL) xmlFree(ctxt->nodeInfoTab);
1779 if (ctxt->inputTab != NULL) xmlFree(ctxt->inputTab);
1780 if (ctxt->version != NULL) xmlFree((char *) ctxt->version);
1781 if (ctxt->encoding != NULL) xmlFree((char *) ctxt->encoding);
1782 if (ctxt->extSubURI != NULL) xmlFree((char *) ctxt->extSubURI);
1783 if (ctxt->extSubSystem != NULL) xmlFree((char *) ctxt->extSubSystem);
1784 #ifdef LIBXML_SAX1_ENABLED
1785 if ((ctxt->sax != NULL) &&
1786 (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler))
1788 if (ctxt->sax != NULL)
1789 #endif /* LIBXML_SAX1_ENABLED */
1791 if (ctxt->directory != NULL) xmlFree((char *) ctxt->directory);
1792 if (ctxt->vctxt.nodeTab != NULL) xmlFree(ctxt->vctxt.nodeTab);
1793 if (ctxt->atts != NULL) xmlFree((xmlChar * *)ctxt->atts);
1794 if (ctxt->dict != NULL) xmlDictFree(ctxt->dict);
1795 if (ctxt->nsTab != NULL) xmlFree((char *) ctxt->nsTab);
1796 if (ctxt->pushTab != NULL) xmlFree(ctxt->pushTab);
1797 if (ctxt->attallocs != NULL) xmlFree(ctxt->attallocs);
1798 if (ctxt->attsDefault != NULL)
1799 xmlHashFree(ctxt->attsDefault, (xmlHashDeallocator) xmlFree);
1800 if (ctxt->attsSpecial != NULL)
1801 xmlHashFree(ctxt->attsSpecial, NULL);
1802 if (ctxt->freeElems != NULL) {
1803 xmlNodePtr cur, next;
1805 cur = ctxt->freeElems;
1806 while (cur != NULL) {
1812 if (ctxt->freeAttrs != NULL) {
1813 xmlAttrPtr cur, next;
1815 cur = ctxt->freeAttrs;
1816 while (cur != NULL) {
1823 * cleanup the error strings
1825 if (ctxt->lastError.message != NULL)
1826 xmlFree(ctxt->lastError.message);
1827 if (ctxt->lastError.file != NULL)
1828 xmlFree(ctxt->lastError.file);
1829 if (ctxt->lastError.str1 != NULL)
1830 xmlFree(ctxt->lastError.str1);
1831 if (ctxt->lastError.str2 != NULL)
1832 xmlFree(ctxt->lastError.str2);
1833 if (ctxt->lastError.str3 != NULL)
1834 xmlFree(ctxt->lastError.str3);
1836 #ifdef LIBXML_CATALOG_ENABLED
1837 if (ctxt->catalogs != NULL)
1838 xmlCatalogFreeLocal(ctxt->catalogs);
1846 * Allocate and initialize a new parser context.
1848 * Returns the xmlParserCtxtPtr or NULL
1852 xmlNewParserCtxt(void)
1854 xmlParserCtxtPtr ctxt;
1856 ctxt = (xmlParserCtxtPtr) xmlMalloc(sizeof(xmlParserCtxt));
1858 xmlErrMemory(NULL, "cannot allocate parser context\n");
1861 memset(ctxt, 0, sizeof(xmlParserCtxt));
1862 if (xmlInitParserCtxt(ctxt) < 0) {
1863 xmlFreeParserCtxt(ctxt);
1869 /************************************************************************
1871 * Handling of node informations *
1873 ************************************************************************/
1876 * xmlClearParserCtxt:
1877 * @ctxt: an XML parser context
1879 * Clear (release owned resources) and reinitialize a parser context
1883 xmlClearParserCtxt(xmlParserCtxtPtr ctxt)
1887 xmlClearNodeInfoSeq(&ctxt->node_seq);
1893 * xmlParserFindNodeInfo:
1894 * @ctx: an XML parser context
1895 * @node: an XML node within the tree
1897 * Find the parser node info struct for a given node
1899 * Returns an xmlParserNodeInfo block pointer or NULL
1901 const xmlParserNodeInfo *
1902 xmlParserFindNodeInfo(const xmlParserCtxtPtr ctx, const xmlNodePtr node)
1906 if ((ctx == NULL) || (node == NULL))
1908 /* Find position where node should be at */
1909 pos = xmlParserFindNodeInfoIndex(&ctx->node_seq, node);
1910 if (pos < ctx->node_seq.length
1911 && ctx->node_seq.buffer[pos].node == node)
1912 return &ctx->node_seq.buffer[pos];
1919 * xmlInitNodeInfoSeq:
1920 * @seq: a node info sequence pointer
1922 * -- Initialize (set to initial state) node info sequence
1925 xmlInitNodeInfoSeq(xmlParserNodeInfoSeqPtr seq)
1935 * xmlClearNodeInfoSeq:
1936 * @seq: a node info sequence pointer
1938 * -- Clear (release memory and reinitialize) node
1942 xmlClearNodeInfoSeq(xmlParserNodeInfoSeqPtr seq)
1946 if (seq->buffer != NULL)
1947 xmlFree(seq->buffer);
1948 xmlInitNodeInfoSeq(seq);
1952 * xmlParserFindNodeInfoIndex:
1953 * @seq: a node info sequence pointer
1954 * @node: an XML node pointer
1957 * xmlParserFindNodeInfoIndex : Find the index that the info record for
1958 * the given node is or should be at in a sorted sequence
1960 * Returns a long indicating the position of the record
1963 xmlParserFindNodeInfoIndex(const xmlParserNodeInfoSeqPtr seq,
1964 const xmlNodePtr node)
1966 unsigned long upper, lower, middle;
1969 if ((seq == NULL) || (node == NULL))
1970 return ((unsigned long) -1);
1972 /* Do a binary search for the key */
1974 upper = seq->length;
1976 while (lower <= upper && !found) {
1977 middle = lower + (upper - lower) / 2;
1978 if (node == seq->buffer[middle - 1].node)
1980 else if (node < seq->buffer[middle - 1].node)
1986 /* Return position */
1987 if (middle == 0 || seq->buffer[middle - 1].node < node)
1995 * xmlParserAddNodeInfo:
1996 * @ctxt: an XML parser context
1997 * @info: a node info sequence pointer
1999 * Insert node info record into the sorted sequence
2002 xmlParserAddNodeInfo(xmlParserCtxtPtr ctxt,
2003 const xmlParserNodeInfoPtr info)
2007 if ((ctxt == NULL) || (info == NULL)) return;
2009 /* Find pos and check to see if node is already in the sequence */
2010 pos = xmlParserFindNodeInfoIndex(&ctxt->node_seq, (xmlNodePtr)
2013 if ((pos < ctxt->node_seq.length) &&
2014 (ctxt->node_seq.buffer != NULL) &&
2015 (ctxt->node_seq.buffer[pos].node == info->node)) {
2016 ctxt->node_seq.buffer[pos] = *info;
2019 /* Otherwise, we need to add new node to buffer */
2021 if ((ctxt->node_seq.length + 1 > ctxt->node_seq.maximum) ||
2022 (ctxt->node_seq.buffer == NULL)) {
2023 xmlParserNodeInfo *tmp_buffer;
2024 unsigned int byte_size;
2026 if (ctxt->node_seq.maximum == 0)
2027 ctxt->node_seq.maximum = 2;
2028 byte_size = (sizeof(*ctxt->node_seq.buffer) *
2029 (2 * ctxt->node_seq.maximum));
2031 if (ctxt->node_seq.buffer == NULL)
2032 tmp_buffer = (xmlParserNodeInfo *) xmlMalloc(byte_size);
2035 (xmlParserNodeInfo *) xmlRealloc(ctxt->node_seq.buffer,
2038 if (tmp_buffer == NULL) {
2039 xmlErrMemory(ctxt, "failed to allocate buffer\n");
2042 ctxt->node_seq.buffer = tmp_buffer;
2043 ctxt->node_seq.maximum *= 2;
2046 /* If position is not at end, move elements out of the way */
2047 if (pos != ctxt->node_seq.length) {
2050 for (i = ctxt->node_seq.length; i > pos; i--)
2051 ctxt->node_seq.buffer[i] = ctxt->node_seq.buffer[i - 1];
2054 /* Copy element and increase length */
2055 ctxt->node_seq.buffer[pos] = *info;
2056 ctxt->node_seq.length++;
2060 /************************************************************************
2062 * Defaults settings *
2064 ************************************************************************/
2066 * xmlPedanticParserDefault:
2069 * Set and return the previous value for enabling pedantic warnings.
2071 * Returns the last value for 0 for no substitution, 1 for substitution.
2075 xmlPedanticParserDefault(int val) {
2076 int old = xmlPedanticParserDefaultValue;
2078 xmlPedanticParserDefaultValue = val;
2083 * xmlLineNumbersDefault:
2086 * Set and return the previous value for enabling line numbers in elements
2087 * contents. This may break on old application and is turned off by default.
2089 * Returns the last value for 0 for no substitution, 1 for substitution.
2093 xmlLineNumbersDefault(int val) {
2094 int old = xmlLineNumbersDefaultValue;
2096 xmlLineNumbersDefaultValue = val;
2101 * xmlSubstituteEntitiesDefault:
2104 * Set and return the previous value for default entity support.
2105 * Initially the parser always keep entity references instead of substituting
2106 * entity values in the output. This function has to be used to change the
2107 * default parser behavior
2108 * SAX::substituteEntities() has to be used for changing that on a file by
2111 * Returns the last value for 0 for no substitution, 1 for substitution.
2115 xmlSubstituteEntitiesDefault(int val) {
2116 int old = xmlSubstituteEntitiesDefaultValue;
2118 xmlSubstituteEntitiesDefaultValue = val;
2123 * xmlKeepBlanksDefault:
2126 * Set and return the previous value for default blanks text nodes support.
2127 * The 1.x version of the parser used an heuristic to try to detect
2128 * ignorable white spaces. As a result the SAX callback was generating
2129 * xmlSAX2IgnorableWhitespace() callbacks instead of characters() one, and when
2130 * using the DOM output text nodes containing those blanks were not generated.
2131 * The 2.x and later version will switch to the XML standard way and
2132 * ignorableWhitespace() are only generated when running the parser in
2133 * validating mode and when the current element doesn't allow CDATA or
2135 * This function is provided as a way to force the standard behavior
2136 * on 1.X libs and to switch back to the old mode for compatibility when
2137 * running 1.X client code on 2.X . Upgrade of 1.X code should be done
2138 * by using xmlIsBlankNode() commodity function to detect the "empty"
2140 * This value also affect autogeneration of indentation when saving code
2141 * if blanks sections are kept, indentation is not generated.
2143 * Returns the last value for 0 for no substitution, 1 for substitution.
2147 xmlKeepBlanksDefault(int val) {
2148 int old = xmlKeepBlanksDefaultValue;
2150 xmlKeepBlanksDefaultValue = val;
2151 if (!val) xmlIndentTreeOutput = 1;
2155 #define bottom_parserInternals
2156 #include "elfgcchack.h"