Blame parserInternals.c

Packit 423ecb
/*
Packit 423ecb
 * parserInternals.c : Internal routines (and obsolete ones) needed for the
Packit 423ecb
 *                     XML and HTML parsers.
Packit 423ecb
 *
Packit 423ecb
 * See Copyright for the status of this software.
Packit 423ecb
 *
Packit 423ecb
 * daniel@veillard.com
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
#define IN_LIBXML
Packit 423ecb
#include "libxml.h"
Packit 423ecb
Packit 423ecb
#if defined(_WIN32) && !defined (__CYGWIN__)
Packit 423ecb
#define XML_DIR_SEP '\\'
Packit 423ecb
#else
Packit 423ecb
#define XML_DIR_SEP '/'
Packit 423ecb
#endif
Packit 423ecb
Packit 423ecb
#include <string.h>
Packit 423ecb
#ifdef HAVE_CTYPE_H
Packit 423ecb
#include <ctype.h>
Packit 423ecb
#endif
Packit 423ecb
#ifdef HAVE_STDLIB_H
Packit 423ecb
#include <stdlib.h>
Packit 423ecb
#endif
Packit 423ecb
#ifdef HAVE_SYS_STAT_H
Packit 423ecb
#include <sys/stat.h>
Packit 423ecb
#endif
Packit 423ecb
#ifdef HAVE_FCNTL_H
Packit 423ecb
#include <fcntl.h>
Packit 423ecb
#endif
Packit 423ecb
#ifdef HAVE_UNISTD_H
Packit 423ecb
#include <unistd.h>
Packit 423ecb
#endif
Packit 423ecb
#ifdef HAVE_ZLIB_H
Packit 423ecb
#include <zlib.h>
Packit 423ecb
#endif
Packit 423ecb
Packit 423ecb
#include <libxml/xmlmemory.h>
Packit 423ecb
#include <libxml/tree.h>
Packit 423ecb
#include <libxml/parser.h>
Packit 423ecb
#include <libxml/parserInternals.h>
Packit 423ecb
#include <libxml/valid.h>
Packit 423ecb
#include <libxml/entities.h>
Packit 423ecb
#include <libxml/xmlerror.h>
Packit 423ecb
#include <libxml/encoding.h>
Packit 423ecb
#include <libxml/valid.h>
Packit 423ecb
#include <libxml/xmlIO.h>
Packit 423ecb
#include <libxml/uri.h>
Packit 423ecb
#include <libxml/dict.h>
Packit 423ecb
#include <libxml/SAX.h>
Packit 423ecb
#ifdef LIBXML_CATALOG_ENABLED
Packit 423ecb
#include <libxml/catalog.h>
Packit 423ecb
#endif
Packit 423ecb
#include <libxml/globals.h>
Packit 423ecb
#include <libxml/chvalid.h>
Packit 423ecb
Packit 423ecb
#define CUR(ctxt) ctxt->input->cur
Packit 423ecb
#define END(ctxt) ctxt->input->end
Packit 423ecb
#define VALID_CTXT(ctxt) (CUR(ctxt) <= END(ctxt))
Packit 423ecb
Packit 423ecb
#include "buf.h"
Packit 423ecb
#include "enc.h"
Packit 423ecb
Packit 423ecb
/*
Packit 423ecb
 * Various global defaults for parsing
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlCheckVersion:
Packit 423ecb
 * @version: the include version number
Packit 423ecb
 *
Packit 423ecb
 * check the compiled lib version against the include one.
Packit 423ecb
 * This can warn or immediately kill the application
Packit 423ecb
 */
Packit 423ecb
void
Packit 423ecb
xmlCheckVersion(int version) {
Packit 423ecb
    int myversion = (int) LIBXML_VERSION;
Packit 423ecb
Packit 423ecb
    xmlInitParser();
Packit 423ecb
Packit 423ecb
    if ((myversion / 10000) != (version / 10000)) {
Packit 423ecb
	xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
		"Fatal: program compiled against libxml %d using libxml %d\n",
Packit 423ecb
		(version / 10000), (myversion / 10000));
Packit 423ecb
	fprintf(stderr,
Packit 423ecb
		"Fatal: program compiled against libxml %d using libxml %d\n",
Packit 423ecb
		(version / 10000), (myversion / 10000));
Packit 423ecb
    }
Packit 423ecb
    if ((myversion / 100) < (version / 100)) {
Packit 423ecb
	xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
		"Warning: program compiled against libxml %d using older %d\n",
Packit 423ecb
		(version / 100), (myversion / 100));
Packit 423ecb
    }
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
Packit 423ecb
/************************************************************************
Packit 423ecb
 *									*
Packit 423ecb
 *		Some factorized error routines				*
Packit 423ecb
 *									*
Packit 423ecb
 ************************************************************************/
Packit 423ecb
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlErrMemory:
Packit 423ecb
 * @ctxt:  an XML parser context
Packit 423ecb
 * @extra:  extra informations
Packit 423ecb
 *
Packit 423ecb
 * Handle a redefinition of attribute error
Packit 423ecb
 */
Packit 423ecb
void
Packit 423ecb
xmlErrMemory(xmlParserCtxtPtr ctxt, const char *extra)
Packit 423ecb
{
Packit 423ecb
    if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
Packit 423ecb
        (ctxt->instate == XML_PARSER_EOF))
Packit 423ecb
	return;
Packit 423ecb
    if (ctxt != NULL) {
Packit 423ecb
        ctxt->errNo = XML_ERR_NO_MEMORY;
Packit 423ecb
        ctxt->instate = XML_PARSER_EOF;
Packit 423ecb
        ctxt->disableSAX = 1;
Packit 423ecb
    }
Packit 423ecb
    if (extra)
Packit 423ecb
        __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER,
Packit 423ecb
                        XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
Packit 423ecb
                        NULL, NULL, 0, 0,
Packit 423ecb
                        "Memory allocation failed : %s\n", extra);
Packit 423ecb
    else
Packit 423ecb
        __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER,
Packit 423ecb
                        XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL,
Packit 423ecb
                        NULL, NULL, 0, 0, "Memory allocation failed\n");
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * __xmlErrEncoding:
Packit 423ecb
 * @ctxt:  an XML parser context
Packit 423ecb
 * @xmlerr:  the error number
Packit 423ecb
 * @msg:  the error message
Packit 423ecb
 * @str1:  an string info
Packit 423ecb
 * @str2:  an string info
Packit 423ecb
 *
Packit 423ecb
 * Handle an encoding error
Packit 423ecb
 */
Packit 423ecb
void
Packit 423ecb
__xmlErrEncoding(xmlParserCtxtPtr ctxt, xmlParserErrors xmlerr,
Packit 423ecb
                 const char *msg, const xmlChar * str1, const xmlChar * str2)
Packit 423ecb
{
Packit 423ecb
    if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
Packit 423ecb
        (ctxt->instate == XML_PARSER_EOF))
Packit 423ecb
	return;
Packit 423ecb
    if (ctxt != NULL)
Packit 423ecb
        ctxt->errNo = xmlerr;
Packit 423ecb
    __xmlRaiseError(NULL, NULL, NULL,
Packit 423ecb
                    ctxt, NULL, XML_FROM_PARSER, xmlerr, XML_ERR_FATAL,
Packit 423ecb
                    NULL, 0, (const char *) str1, (const char *) str2,
Packit 423ecb
                    NULL, 0, 0, msg, str1, str2);
Packit 423ecb
    if (ctxt != NULL) {
Packit 423ecb
        ctxt->wellFormed = 0;
Packit 423ecb
        if (ctxt->recovery == 0)
Packit 423ecb
            ctxt->disableSAX = 1;
Packit 423ecb
    }
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlErrInternal:
Packit 423ecb
 * @ctxt:  an XML parser context
Packit 423ecb
 * @msg:  the error message
Packit 423ecb
 * @str:  error informations
Packit 423ecb
 *
Packit 423ecb
 * Handle an internal error
Packit 423ecb
 */
Packit 423ecb
static void LIBXML_ATTR_FORMAT(2,0)
Packit 423ecb
xmlErrInternal(xmlParserCtxtPtr ctxt, const char *msg, const xmlChar * str)
Packit 423ecb
{
Packit 423ecb
    if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
Packit 423ecb
        (ctxt->instate == XML_PARSER_EOF))
Packit 423ecb
	return;
Packit 423ecb
    if (ctxt != NULL)
Packit 423ecb
        ctxt->errNo = XML_ERR_INTERNAL_ERROR;
Packit 423ecb
    __xmlRaiseError(NULL, NULL, NULL,
Packit 423ecb
                    ctxt, NULL, XML_FROM_PARSER, XML_ERR_INTERNAL_ERROR,
Packit 423ecb
                    XML_ERR_FATAL, NULL, 0, (const char *) str, NULL, NULL,
Packit 423ecb
                    0, 0, msg, str);
Packit 423ecb
    if (ctxt != NULL) {
Packit 423ecb
        ctxt->wellFormed = 0;
Packit 423ecb
        if (ctxt->recovery == 0)
Packit 423ecb
            ctxt->disableSAX = 1;
Packit 423ecb
    }
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlErrEncodingInt:
Packit 423ecb
 * @ctxt:  an XML parser context
Packit 423ecb
 * @error:  the error number
Packit 423ecb
 * @msg:  the error message
Packit 423ecb
 * @val:  an integer value
Packit 423ecb
 *
Packit 423ecb
 * n encoding error
Packit 423ecb
 */
Packit 423ecb
static void LIBXML_ATTR_FORMAT(3,0)
Packit 423ecb
xmlErrEncodingInt(xmlParserCtxtPtr ctxt, xmlParserErrors error,
Packit 423ecb
                  const char *msg, int val)
Packit 423ecb
{
Packit 423ecb
    if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
Packit 423ecb
        (ctxt->instate == XML_PARSER_EOF))
Packit 423ecb
	return;
Packit 423ecb
    if (ctxt != NULL)
Packit 423ecb
        ctxt->errNo = error;
Packit 423ecb
    __xmlRaiseError(NULL, NULL, NULL,
Packit 423ecb
                    ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_FATAL,
Packit 423ecb
                    NULL, 0, NULL, NULL, NULL, val, 0, msg, val);
Packit 423ecb
    if (ctxt != NULL) {
Packit 423ecb
        ctxt->wellFormed = 0;
Packit 423ecb
        if (ctxt->recovery == 0)
Packit 423ecb
            ctxt->disableSAX = 1;
Packit 423ecb
    }
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlIsLetter:
Packit 423ecb
 * @c:  an unicode character (int)
Packit 423ecb
 *
Packit 423ecb
 * Check whether the character is allowed by the production
Packit 423ecb
 * [84] Letter ::= BaseChar | Ideographic
Packit 423ecb
 *
Packit 423ecb
 * Returns 0 if not, non-zero otherwise
Packit 423ecb
 */
Packit 423ecb
int
Packit 423ecb
xmlIsLetter(int c) {
Packit 423ecb
    return(IS_BASECHAR(c) || IS_IDEOGRAPHIC(c));
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/************************************************************************
Packit 423ecb
 *									*
Packit 423ecb
 *		Input handling functions for progressive parsing	*
Packit 423ecb
 *									*
Packit 423ecb
 ************************************************************************/
Packit 423ecb
Packit 423ecb
/* #define DEBUG_INPUT */
Packit 423ecb
/* #define DEBUG_STACK */
Packit 423ecb
/* #define DEBUG_PUSH */
Packit 423ecb
Packit 423ecb
Packit 423ecb
/* we need to keep enough input to show errors in context */
Packit 423ecb
#define LINE_LEN        80
Packit 423ecb
Packit 423ecb
#ifdef DEBUG_INPUT
Packit 423ecb
#define CHECK_BUFFER(in) check_buffer(in)
Packit 423ecb
Packit 423ecb
static
Packit 423ecb
void check_buffer(xmlParserInputPtr in) {
Packit 423ecb
    if (in->base != xmlBufContent(in->buf->buffer)) {
Packit 423ecb
        xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
		"xmlParserInput: base mismatch problem\n");
Packit 423ecb
    }
Packit 423ecb
    if (in->cur < in->base) {
Packit 423ecb
        xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
		"xmlParserInput: cur < base problem\n");
Packit 423ecb
    }
Packit 423ecb
    if (in->cur > in->base + xmlBufUse(in->buf->buffer)) {
Packit 423ecb
        xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
		"xmlParserInput: cur > base + use problem\n");
Packit 423ecb
    }
Packit 423ecb
    xmlGenericError(xmlGenericErrorContext,"buffer %x : content %x, cur %d, use %d\n",
Packit 423ecb
            (int) in, (int) xmlBufContent(in->buf->buffer), in->cur - in->base,
Packit 423ecb
	    xmlBufUse(in->buf->buffer));
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
#else
Packit 423ecb
#define CHECK_BUFFER(in)
Packit 423ecb
#endif
Packit 423ecb
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlParserInputRead:
Packit 423ecb
 * @in:  an XML parser input
Packit 423ecb
 * @len:  an indicative size for the lookahead
Packit 423ecb
 *
Packit 423ecb
 * This function was internal and is deprecated.
Packit 423ecb
 *
Packit 423ecb
 * Returns -1 as this is an error to use it.
Packit 423ecb
 */
Packit 423ecb
int
Packit 423ecb
xmlParserInputRead(xmlParserInputPtr in ATTRIBUTE_UNUSED, int len ATTRIBUTE_UNUSED) {
Packit 423ecb
    return(-1);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlParserInputGrow:
Packit 423ecb
 * @in:  an XML parser input
Packit 423ecb
 * @len:  an indicative size for the lookahead
Packit 423ecb
 *
Packit 423ecb
 * This function increase the input for the parser. It tries to
Packit 423ecb
 * preserve pointers to the input buffer, and keep already read data
Packit 423ecb
 *
Packit 423ecb
 * Returns the amount of char read, or -1 in case of error, 0 indicate the
Packit 423ecb
 * end of this entity
Packit 423ecb
 */
Packit 423ecb
int
Packit 423ecb
xmlParserInputGrow(xmlParserInputPtr in, int len) {
Packit 423ecb
    int ret;
Packit 423ecb
    size_t indx;
Packit 423ecb
    const xmlChar *content;
Packit 423ecb
Packit 423ecb
    if ((in == NULL) || (len < 0)) return(-1);
Packit 423ecb
#ifdef DEBUG_INPUT
Packit 423ecb
    xmlGenericError(xmlGenericErrorContext, "Grow\n");
Packit 423ecb
#endif
Packit 423ecb
    if (in->buf == NULL) return(-1);
Packit 423ecb
    if (in->base == NULL) return(-1);
Packit 423ecb
    if (in->cur == NULL) return(-1);
Packit 423ecb
    if (in->buf->buffer == NULL) return(-1);
Packit 423ecb
Packit 423ecb
    CHECK_BUFFER(in);
Packit 423ecb
Packit 423ecb
    indx = in->cur - in->base;
Packit 423ecb
    if (xmlBufUse(in->buf->buffer) > (unsigned int) indx + INPUT_CHUNK) {
Packit 423ecb
Packit 423ecb
	CHECK_BUFFER(in);
Packit 423ecb
Packit 423ecb
        return(0);
Packit 423ecb
    }
Packit 423ecb
    if (in->buf->readcallback != NULL) {
Packit 423ecb
	ret = xmlParserInputBufferGrow(in->buf, len);
Packit 423ecb
    } else
Packit 423ecb
        return(0);
Packit 423ecb
Packit 423ecb
    /*
Packit 423ecb
     * NOTE : in->base may be a "dangling" i.e. freed pointer in this
Packit 423ecb
     *        block, but we use it really as an integer to do some
Packit 423ecb
     *        pointer arithmetic. Insure will raise it as a bug but in
Packit 423ecb
     *        that specific case, that's not !
Packit 423ecb
     */
Packit 423ecb
Packit 423ecb
    content = xmlBufContent(in->buf->buffer);
Packit 423ecb
    if (in->base != content) {
Packit 423ecb
        /*
Packit 423ecb
	 * the buffer has been reallocated
Packit 423ecb
	 */
Packit 423ecb
	indx = in->cur - in->base;
Packit 423ecb
	in->base = content;
Packit 423ecb
	in->cur = &content[indx];
Packit 423ecb
    }
Packit 423ecb
    in->end = xmlBufEnd(in->buf->buffer);
Packit 423ecb
Packit 423ecb
    CHECK_BUFFER(in);
Packit 423ecb
Packit 423ecb
    return(ret);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlParserInputShrink:
Packit 423ecb
 * @in:  an XML parser input
Packit 423ecb
 *
Packit 423ecb
 * This function removes used input for the parser.
Packit 423ecb
 */
Packit 423ecb
void
Packit 423ecb
xmlParserInputShrink(xmlParserInputPtr in) {
Packit 423ecb
    size_t used;
Packit 423ecb
    size_t ret;
Packit 423ecb
    size_t indx;
Packit 423ecb
    const xmlChar *content;
Packit 423ecb
Packit 423ecb
#ifdef DEBUG_INPUT
Packit 423ecb
    xmlGenericError(xmlGenericErrorContext, "Shrink\n");
Packit 423ecb
#endif
Packit 423ecb
    if (in == NULL) return;
Packit 423ecb
    if (in->buf == NULL) return;
Packit 423ecb
    if (in->base == NULL) return;
Packit 423ecb
    if (in->cur == NULL) return;
Packit 423ecb
    if (in->buf->buffer == NULL) return;
Packit 423ecb
Packit 423ecb
    CHECK_BUFFER(in);
Packit 423ecb
Packit 423ecb
    used = in->cur - xmlBufContent(in->buf->buffer);
Packit 423ecb
    /*
Packit 423ecb
     * Do not shrink on large buffers whose only a tiny fraction
Packit 423ecb
     * was consumed
Packit 423ecb
     */
Packit 423ecb
    if (used > INPUT_CHUNK) {
Packit 423ecb
	ret = xmlBufShrink(in->buf->buffer, used - LINE_LEN);
Packit 423ecb
	if (ret > 0) {
Packit 423ecb
	    in->cur -= ret;
Packit 423ecb
	    in->consumed += ret;
Packit 423ecb
	}
Packit 423ecb
	in->end = xmlBufEnd(in->buf->buffer);
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    CHECK_BUFFER(in);
Packit 423ecb
Packit 423ecb
    if (xmlBufUse(in->buf->buffer) > INPUT_CHUNK) {
Packit 423ecb
        return;
Packit 423ecb
    }
Packit 423ecb
    xmlParserInputBufferRead(in->buf, 2 * INPUT_CHUNK);
Packit 423ecb
    content = xmlBufContent(in->buf->buffer);
Packit 423ecb
    if (in->base != content) {
Packit 423ecb
        /*
Packit 423ecb
	 * the buffer has been reallocated
Packit 423ecb
	 */
Packit 423ecb
	indx = in->cur - in->base;
Packit 423ecb
	in->base = content;
Packit 423ecb
	in->cur = &content[indx];
Packit 423ecb
    }
Packit 423ecb
    in->end = xmlBufEnd(in->buf->buffer);
Packit 423ecb
Packit 423ecb
    CHECK_BUFFER(in);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/************************************************************************
Packit 423ecb
 *									*
Packit 423ecb
 *		UTF8 character input and related functions		*
Packit 423ecb
 *									*
Packit 423ecb
 ************************************************************************/
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlNextChar:
Packit 423ecb
 * @ctxt:  the XML parser context
Packit 423ecb
 *
Packit 423ecb
 * Skip to the next char input char.
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
void
Packit 423ecb
xmlNextChar(xmlParserCtxtPtr ctxt)
Packit 423ecb
{
Packit 423ecb
    if ((ctxt == NULL) || (ctxt->instate == XML_PARSER_EOF) ||
Packit 423ecb
        (ctxt->input == NULL))
Packit 423ecb
        return;
Packit 423ecb
Packit 423ecb
    if (!(VALID_CTXT(ctxt))) {
Packit 423ecb
        xmlErrInternal(ctxt, "Parser input data memory error\n", NULL);
Packit 423ecb
	ctxt->errNo = XML_ERR_INTERNAL_ERROR;
Packit 423ecb
        xmlStopParser(ctxt);
Packit 423ecb
	return;
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    if ((*ctxt->input->cur == 0) &&
Packit 423ecb
        (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) {
Packit 423ecb
        return;
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    if (ctxt->charset == XML_CHAR_ENCODING_UTF8) {
Packit 423ecb
        const unsigned char *cur;
Packit 423ecb
        unsigned char c;
Packit 423ecb
Packit 423ecb
        /*
Packit 423ecb
         *   2.11 End-of-Line Handling
Packit 423ecb
         *   the literal two-character sequence "#xD#xA" or a standalone
Packit 423ecb
         *   literal #xD, an XML processor must pass to the application
Packit 423ecb
         *   the single character #xA.
Packit 423ecb
         */
Packit 423ecb
        if (*(ctxt->input->cur) == '\n') {
Packit 423ecb
            ctxt->input->line++; ctxt->input->col = 1;
Packit 423ecb
        } else
Packit 423ecb
            ctxt->input->col++;
Packit 423ecb
Packit 423ecb
        /*
Packit 423ecb
         * We are supposed to handle UTF8, check it's valid
Packit 423ecb
         * From rfc2044: encoding of the Unicode values on UTF-8:
Packit 423ecb
         *
Packit 423ecb
         * UCS-4 range (hex.)           UTF-8 octet sequence (binary)
Packit 423ecb
         * 0000 0000-0000 007F   0xxxxxxx
Packit 423ecb
         * 0000 0080-0000 07FF   110xxxxx 10xxxxxx
Packit 423ecb
         * 0000 0800-0000 FFFF   1110xxxx 10xxxxxx 10xxxxxx
Packit 423ecb
         *
Packit 423ecb
         * Check for the 0x110000 limit too
Packit 423ecb
         */
Packit 423ecb
        cur = ctxt->input->cur;
Packit 423ecb
Packit 423ecb
        c = *cur;
Packit 423ecb
        if (c & 0x80) {
Packit 423ecb
            if (c == 0xC0)
Packit 423ecb
	        goto encoding_error;
Packit 423ecb
            if (cur[1] == 0) {
Packit 423ecb
                xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
Packit 423ecb
                cur = ctxt->input->cur;
Packit 423ecb
            }
Packit 423ecb
            if ((cur[1] & 0xc0) != 0x80)
Packit 423ecb
                goto encoding_error;
Packit 423ecb
            if ((c & 0xe0) == 0xe0) {
Packit 423ecb
                unsigned int val;
Packit 423ecb
Packit 423ecb
                if (cur[2] == 0) {
Packit 423ecb
                    xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
Packit 423ecb
                    cur = ctxt->input->cur;
Packit 423ecb
                }
Packit 423ecb
                if ((cur[2] & 0xc0) != 0x80)
Packit 423ecb
                    goto encoding_error;
Packit 423ecb
                if ((c & 0xf0) == 0xf0) {
Packit 423ecb
                    if (cur[3] == 0) {
Packit 423ecb
                        xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
Packit 423ecb
                        cur = ctxt->input->cur;
Packit 423ecb
                    }
Packit 423ecb
                    if (((c & 0xf8) != 0xf0) ||
Packit 423ecb
                        ((cur[3] & 0xc0) != 0x80))
Packit 423ecb
                        goto encoding_error;
Packit 423ecb
                    /* 4-byte code */
Packit 423ecb
                    ctxt->input->cur += 4;
Packit 423ecb
                    val = (cur[0] & 0x7) << 18;
Packit 423ecb
                    val |= (cur[1] & 0x3f) << 12;
Packit 423ecb
                    val |= (cur[2] & 0x3f) << 6;
Packit 423ecb
                    val |= cur[3] & 0x3f;
Packit 423ecb
                } else {
Packit 423ecb
                    /* 3-byte code */
Packit 423ecb
                    ctxt->input->cur += 3;
Packit 423ecb
                    val = (cur[0] & 0xf) << 12;
Packit 423ecb
                    val |= (cur[1] & 0x3f) << 6;
Packit 423ecb
                    val |= cur[2] & 0x3f;
Packit 423ecb
                }
Packit 423ecb
                if (((val > 0xd7ff) && (val < 0xe000)) ||
Packit 423ecb
                    ((val > 0xfffd) && (val < 0x10000)) ||
Packit 423ecb
                    (val >= 0x110000)) {
Packit 423ecb
		xmlErrEncodingInt(ctxt, XML_ERR_INVALID_CHAR,
Packit 423ecb
				  "Char 0x%X out of allowed range\n",
Packit 423ecb
				  val);
Packit 423ecb
                }
Packit 423ecb
            } else
Packit 423ecb
                /* 2-byte code */
Packit 423ecb
                ctxt->input->cur += 2;
Packit 423ecb
        } else
Packit 423ecb
            /* 1-byte code */
Packit 423ecb
            ctxt->input->cur++;
Packit 423ecb
Packit 423ecb
        ctxt->nbChars++;
Packit 423ecb
    } else {
Packit 423ecb
        /*
Packit 423ecb
         * Assume it's a fixed length encoding (1) with
Packit 423ecb
         * a compatible encoding for the ASCII set, since
Packit 423ecb
         * XML constructs only use < 128 chars
Packit 423ecb
         */
Packit 423ecb
Packit 423ecb
        if (*(ctxt->input->cur) == '\n') {
Packit 423ecb
            ctxt->input->line++; ctxt->input->col = 1;
Packit 423ecb
        } else
Packit 423ecb
            ctxt->input->col++;
Packit 423ecb
        ctxt->input->cur++;
Packit 423ecb
        ctxt->nbChars++;
Packit 423ecb
    }
Packit 423ecb
    if (*ctxt->input->cur == 0)
Packit 423ecb
        xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
Packit 423ecb
    return;
Packit 423ecb
encoding_error:
Packit 423ecb
    /*
Packit 423ecb
     * If we detect an UTF8 error that probably mean that the
Packit 423ecb
     * input encoding didn't get properly advertised in the
Packit 423ecb
     * declaration header. Report the error and switch the encoding
Packit 423ecb
     * to ISO-Latin-1 (if you don't like this policy, just declare the
Packit 423ecb
     * encoding !)
Packit 423ecb
     */
Packit 423ecb
    if ((ctxt == NULL) || (ctxt->input == NULL) ||
Packit 423ecb
        (ctxt->input->end - ctxt->input->cur < 4)) {
Packit 423ecb
	__xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR,
Packit 423ecb
		     "Input is not proper UTF-8, indicate encoding !\n",
Packit 423ecb
		     NULL, NULL);
Packit 423ecb
    } else {
Packit 423ecb
        char buffer[150];
Packit 423ecb
Packit 423ecb
	snprintf(buffer, 149, "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n",
Packit 423ecb
			ctxt->input->cur[0], ctxt->input->cur[1],
Packit 423ecb
			ctxt->input->cur[2], ctxt->input->cur[3]);
Packit 423ecb
	__xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR,
Packit 423ecb
		     "Input is not proper UTF-8, indicate encoding !\n%s",
Packit 423ecb
		     BAD_CAST buffer, NULL);
Packit 423ecb
    }
Packit 423ecb
    ctxt->charset = XML_CHAR_ENCODING_8859_1;
Packit 423ecb
    ctxt->input->cur++;
Packit 423ecb
    return;
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlCurrentChar:
Packit 423ecb
 * @ctxt:  the XML parser context
Packit 423ecb
 * @len:  pointer to the length of the char read
Packit 423ecb
 *
Packit 423ecb
 * The current char value, if using UTF-8 this may actually span multiple
Packit 423ecb
 * bytes in the input buffer. Implement the end of line normalization:
Packit 423ecb
 * 2.11 End-of-Line Handling
Packit 423ecb
 * Wherever an external parsed entity or the literal entity value
Packit 423ecb
 * of an internal parsed entity contains either the literal two-character
Packit 423ecb
 * sequence "#xD#xA" or a standalone literal #xD, an XML processor
Packit 423ecb
 * must pass to the application the single character #xA.
Packit 423ecb
 * This behavior can conveniently be produced by normalizing all
Packit 423ecb
 * line breaks to #xA on input, before parsing.)
Packit 423ecb
 *
Packit 423ecb
 * Returns the current char value and its length
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
int
Packit 423ecb
xmlCurrentChar(xmlParserCtxtPtr ctxt, int *len) {
Packit 423ecb
    if ((ctxt == NULL) || (len == NULL) || (ctxt->input == NULL)) return(0);
Packit 423ecb
    if (ctxt->instate == XML_PARSER_EOF)
Packit 423ecb
	return(0);
Packit 423ecb
Packit 423ecb
    if ((*ctxt->input->cur >= 0x20) && (*ctxt->input->cur <= 0x7F)) {
Packit 423ecb
	    *len = 1;
Packit 423ecb
	    return((int) *ctxt->input->cur);
Packit 423ecb
    }
Packit 423ecb
    if (ctxt->charset == XML_CHAR_ENCODING_UTF8) {
Packit 423ecb
	/*
Packit 423ecb
	 * We are supposed to handle UTF8, check it's valid
Packit 423ecb
	 * From rfc2044: encoding of the Unicode values on UTF-8:
Packit 423ecb
	 *
Packit 423ecb
	 * UCS-4 range (hex.)           UTF-8 octet sequence (binary)
Packit 423ecb
	 * 0000 0000-0000 007F   0xxxxxxx
Packit 423ecb
	 * 0000 0080-0000 07FF   110xxxxx 10xxxxxx
Packit 423ecb
	 * 0000 0800-0000 FFFF   1110xxxx 10xxxxxx 10xxxxxx
Packit 423ecb
	 *
Packit 423ecb
	 * Check for the 0x110000 limit too
Packit 423ecb
	 */
Packit 423ecb
	const unsigned char *cur = ctxt->input->cur;
Packit 423ecb
	unsigned char c;
Packit 423ecb
	unsigned int val;
Packit 423ecb
Packit 423ecb
	c = *cur;
Packit 423ecb
	if (c & 0x80) {
Packit 423ecb
	    if (((c & 0x40) == 0) || (c == 0xC0))
Packit 423ecb
		goto encoding_error;
Packit 423ecb
	    if (cur[1] == 0) {
Packit 423ecb
		xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
Packit 423ecb
                cur = ctxt->input->cur;
Packit 423ecb
            }
Packit 423ecb
	    if ((cur[1] & 0xc0) != 0x80)
Packit 423ecb
		goto encoding_error;
Packit 423ecb
	    if ((c & 0xe0) == 0xe0) {
Packit 423ecb
		if (cur[2] == 0) {
Packit 423ecb
		    xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
Packit 423ecb
                    cur = ctxt->input->cur;
Packit 423ecb
                }
Packit 423ecb
		if ((cur[2] & 0xc0) != 0x80)
Packit 423ecb
		    goto encoding_error;
Packit 423ecb
		if ((c & 0xf0) == 0xf0) {
Packit 423ecb
		    if (cur[3] == 0) {
Packit 423ecb
			xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
Packit 423ecb
                        cur = ctxt->input->cur;
Packit 423ecb
                    }
Packit 423ecb
		    if (((c & 0xf8) != 0xf0) ||
Packit 423ecb
			((cur[3] & 0xc0) != 0x80))
Packit 423ecb
			goto encoding_error;
Packit 423ecb
		    /* 4-byte code */
Packit 423ecb
		    *len = 4;
Packit 423ecb
		    val = (cur[0] & 0x7) << 18;
Packit 423ecb
		    val |= (cur[1] & 0x3f) << 12;
Packit 423ecb
		    val |= (cur[2] & 0x3f) << 6;
Packit 423ecb
		    val |= cur[3] & 0x3f;
Packit 423ecb
		    if (val < 0x10000)
Packit 423ecb
			goto encoding_error;
Packit 423ecb
		} else {
Packit 423ecb
		  /* 3-byte code */
Packit 423ecb
		    *len = 3;
Packit 423ecb
		    val = (cur[0] & 0xf) << 12;
Packit 423ecb
		    val |= (cur[1] & 0x3f) << 6;
Packit 423ecb
		    val |= cur[2] & 0x3f;
Packit 423ecb
		    if (val < 0x800)
Packit 423ecb
			goto encoding_error;
Packit 423ecb
		}
Packit 423ecb
	    } else {
Packit 423ecb
	      /* 2-byte code */
Packit 423ecb
		*len = 2;
Packit 423ecb
		val = (cur[0] & 0x1f) << 6;
Packit 423ecb
		val |= cur[1] & 0x3f;
Packit 423ecb
		if (val < 0x80)
Packit 423ecb
		    goto encoding_error;
Packit 423ecb
	    }
Packit 423ecb
	    if (!IS_CHAR(val)) {
Packit 423ecb
	        xmlErrEncodingInt(ctxt, XML_ERR_INVALID_CHAR,
Packit 423ecb
				  "Char 0x%X out of allowed range\n", val);
Packit 423ecb
	    }
Packit 423ecb
	    return(val);
Packit 423ecb
	} else {
Packit 423ecb
	    /* 1-byte code */
Packit 423ecb
	    *len = 1;
Packit 423ecb
	    if (*ctxt->input->cur == 0)
Packit 423ecb
		xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
Packit 423ecb
	    if ((*ctxt->input->cur == 0) &&
Packit 423ecb
	        (ctxt->input->end > ctxt->input->cur)) {
Packit 423ecb
	        xmlErrEncodingInt(ctxt, XML_ERR_INVALID_CHAR,
Packit 423ecb
				  "Char 0x0 out of allowed range\n", 0);
Packit 423ecb
	    }
Packit 423ecb
	    if (*ctxt->input->cur == 0xD) {
Packit 423ecb
		if (ctxt->input->cur[1] == 0xA) {
Packit 423ecb
		    ctxt->nbChars++;
Packit 423ecb
		    ctxt->input->cur++;
Packit 423ecb
		}
Packit 423ecb
		return(0xA);
Packit 423ecb
	    }
Packit 423ecb
	    return((int) *ctxt->input->cur);
Packit 423ecb
	}
Packit 423ecb
    }
Packit 423ecb
    /*
Packit 423ecb
     * Assume it's a fixed length encoding (1) with
Packit 423ecb
     * a compatible encoding for the ASCII set, since
Packit 423ecb
     * XML constructs only use < 128 chars
Packit 423ecb
     */
Packit 423ecb
    *len = 1;
Packit 423ecb
    if (*ctxt->input->cur == 0xD) {
Packit 423ecb
	if (ctxt->input->cur[1] == 0xA) {
Packit 423ecb
	    ctxt->nbChars++;
Packit 423ecb
	    ctxt->input->cur++;
Packit 423ecb
	}
Packit 423ecb
	return(0xA);
Packit 423ecb
    }
Packit 423ecb
    return((int) *ctxt->input->cur);
Packit 423ecb
encoding_error:
Packit 423ecb
    /*
Packit 423ecb
     * An encoding problem may arise from a truncated input buffer
Packit 423ecb
     * splitting a character in the middle. In that case do not raise
Packit 423ecb
     * an error but return 0 to endicate an end of stream problem
Packit 423ecb
     */
Packit 423ecb
    if (ctxt->input->end - ctxt->input->cur < 4) {
Packit 423ecb
	*len = 0;
Packit 423ecb
	return(0);
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    /*
Packit 423ecb
     * If we detect an UTF8 error that probably mean that the
Packit 423ecb
     * input encoding didn't get properly advertised in the
Packit 423ecb
     * declaration header. Report the error and switch the encoding
Packit 423ecb
     * to ISO-Latin-1 (if you don't like this policy, just declare the
Packit 423ecb
     * encoding !)
Packit 423ecb
     */
Packit 423ecb
    {
Packit 423ecb
        char buffer[150];
Packit 423ecb
Packit 423ecb
	snprintf(&buffer[0], 149, "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n",
Packit 423ecb
			ctxt->input->cur[0], ctxt->input->cur[1],
Packit 423ecb
			ctxt->input->cur[2], ctxt->input->cur[3]);
Packit 423ecb
	__xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR,
Packit 423ecb
		     "Input is not proper UTF-8, indicate encoding !\n%s",
Packit 423ecb
		     BAD_CAST buffer, NULL);
Packit 423ecb
    }
Packit 423ecb
    ctxt->charset = XML_CHAR_ENCODING_8859_1;
Packit 423ecb
    *len = 1;
Packit 423ecb
    return((int) *ctxt->input->cur);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlStringCurrentChar:
Packit 423ecb
 * @ctxt:  the XML parser context
Packit 423ecb
 * @cur:  pointer to the beginning of the char
Packit 423ecb
 * @len:  pointer to the length of the char read
Packit 423ecb
 *
Packit 423ecb
 * The current char value, if using UTF-8 this may actually span multiple
Packit 423ecb
 * bytes in the input buffer.
Packit 423ecb
 *
Packit 423ecb
 * Returns the current char value and its length
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
int
Packit 423ecb
xmlStringCurrentChar(xmlParserCtxtPtr ctxt, const xmlChar * cur, int *len)
Packit 423ecb
{
Packit 423ecb
    if ((len == NULL) || (cur == NULL)) return(0);
Packit 423ecb
    if ((ctxt == NULL) || (ctxt->charset == XML_CHAR_ENCODING_UTF8)) {
Packit 423ecb
        /*
Packit 423ecb
         * We are supposed to handle UTF8, check it's valid
Packit 423ecb
         * From rfc2044: encoding of the Unicode values on UTF-8:
Packit 423ecb
         *
Packit 423ecb
         * UCS-4 range (hex.)           UTF-8 octet sequence (binary)
Packit 423ecb
         * 0000 0000-0000 007F   0xxxxxxx
Packit 423ecb
         * 0000 0080-0000 07FF   110xxxxx 10xxxxxx
Packit 423ecb
         * 0000 0800-0000 FFFF   1110xxxx 10xxxxxx 10xxxxxx
Packit 423ecb
         *
Packit 423ecb
         * Check for the 0x110000 limit too
Packit 423ecb
         */
Packit 423ecb
        unsigned char c;
Packit 423ecb
        unsigned int val;
Packit 423ecb
Packit 423ecb
        c = *cur;
Packit 423ecb
        if (c & 0x80) {
Packit 423ecb
            if ((cur[1] & 0xc0) != 0x80)
Packit 423ecb
                goto encoding_error;
Packit 423ecb
            if ((c & 0xe0) == 0xe0) {
Packit 423ecb
Packit 423ecb
                if ((cur[2] & 0xc0) != 0x80)
Packit 423ecb
                    goto encoding_error;
Packit 423ecb
                if ((c & 0xf0) == 0xf0) {
Packit 423ecb
                    if (((c & 0xf8) != 0xf0) || ((cur[3] & 0xc0) != 0x80))
Packit 423ecb
                        goto encoding_error;
Packit 423ecb
                    /* 4-byte code */
Packit 423ecb
                    *len = 4;
Packit 423ecb
                    val = (cur[0] & 0x7) << 18;
Packit 423ecb
                    val |= (cur[1] & 0x3f) << 12;
Packit 423ecb
                    val |= (cur[2] & 0x3f) << 6;
Packit 423ecb
                    val |= cur[3] & 0x3f;
Packit 423ecb
                } else {
Packit 423ecb
                    /* 3-byte code */
Packit 423ecb
                    *len = 3;
Packit 423ecb
                    val = (cur[0] & 0xf) << 12;
Packit 423ecb
                    val |= (cur[1] & 0x3f) << 6;
Packit 423ecb
                    val |= cur[2] & 0x3f;
Packit 423ecb
                }
Packit 423ecb
            } else {
Packit 423ecb
                /* 2-byte code */
Packit 423ecb
                *len = 2;
Packit 423ecb
                val = (cur[0] & 0x1f) << 6;
Packit 423ecb
                val |= cur[1] & 0x3f;
Packit 423ecb
            }
Packit 423ecb
            if (!IS_CHAR(val)) {
Packit 423ecb
	        xmlErrEncodingInt(ctxt, XML_ERR_INVALID_CHAR,
Packit 423ecb
				  "Char 0x%X out of allowed range\n", val);
Packit 423ecb
            }
Packit 423ecb
            return (val);
Packit 423ecb
        } else {
Packit 423ecb
            /* 1-byte code */
Packit 423ecb
            *len = 1;
Packit 423ecb
            return ((int) *cur);
Packit 423ecb
        }
Packit 423ecb
    }
Packit 423ecb
    /*
Packit 423ecb
     * Assume it's a fixed length encoding (1) with
Packit 423ecb
     * a compatible encoding for the ASCII set, since
Packit 423ecb
     * XML constructs only use < 128 chars
Packit 423ecb
     */
Packit 423ecb
    *len = 1;
Packit 423ecb
    return ((int) *cur);
Packit 423ecb
encoding_error:
Packit 423ecb
Packit 423ecb
    /*
Packit 423ecb
     * An encoding problem may arise from a truncated input buffer
Packit 423ecb
     * splitting a character in the middle. In that case do not raise
Packit 423ecb
     * an error but return 0 to endicate an end of stream problem
Packit 423ecb
     */
Packit 423ecb
    if ((ctxt == NULL) || (ctxt->input == NULL) ||
Packit 423ecb
        (ctxt->input->end - ctxt->input->cur < 4)) {
Packit 423ecb
	*len = 0;
Packit 423ecb
	return(0);
Packit 423ecb
    }
Packit 423ecb
    /*
Packit 423ecb
     * If we detect an UTF8 error that probably mean that the
Packit 423ecb
     * input encoding didn't get properly advertised in the
Packit 423ecb
     * declaration header. Report the error and switch the encoding
Packit 423ecb
     * to ISO-Latin-1 (if you don't like this policy, just declare the
Packit 423ecb
     * encoding !)
Packit 423ecb
     */
Packit 423ecb
    {
Packit 423ecb
        char buffer[150];
Packit 423ecb
Packit 423ecb
	snprintf(buffer, 149, "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n",
Packit 423ecb
			ctxt->input->cur[0], ctxt->input->cur[1],
Packit 423ecb
			ctxt->input->cur[2], ctxt->input->cur[3]);
Packit 423ecb
	__xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR,
Packit 423ecb
		     "Input is not proper UTF-8, indicate encoding !\n%s",
Packit 423ecb
		     BAD_CAST buffer, NULL);
Packit 423ecb
    }
Packit 423ecb
    *len = 1;
Packit 423ecb
    return ((int) *cur);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlCopyCharMultiByte:
Packit 423ecb
 * @out:  pointer to an array of xmlChar
Packit 423ecb
 * @val:  the char value
Packit 423ecb
 *
Packit 423ecb
 * append the char value in the array
Packit 423ecb
 *
Packit 423ecb
 * Returns the number of xmlChar written
Packit 423ecb
 */
Packit 423ecb
int
Packit 423ecb
xmlCopyCharMultiByte(xmlChar *out, int val) {
Packit 423ecb
    if (out == NULL) return(0);
Packit 423ecb
    /*
Packit 423ecb
     * We are supposed to handle UTF8, check it's valid
Packit 423ecb
     * From rfc2044: encoding of the Unicode values on UTF-8:
Packit 423ecb
     *
Packit 423ecb
     * UCS-4 range (hex.)           UTF-8 octet sequence (binary)
Packit 423ecb
     * 0000 0000-0000 007F   0xxxxxxx
Packit 423ecb
     * 0000 0080-0000 07FF   110xxxxx 10xxxxxx
Packit 423ecb
     * 0000 0800-0000 FFFF   1110xxxx 10xxxxxx 10xxxxxx
Packit 423ecb
     */
Packit 423ecb
    if  (val >= 0x80) {
Packit 423ecb
	xmlChar *savedout = out;
Packit 423ecb
	int bits;
Packit 423ecb
	if (val <   0x800) { *out++= (val >>  6) | 0xC0;  bits=  0; }
Packit 423ecb
	else if (val < 0x10000) { *out++= (val >> 12) | 0xE0;  bits=  6;}
Packit 423ecb
	else if (val < 0x110000)  { *out++= (val >> 18) | 0xF0;  bits=  12; }
Packit 423ecb
	else {
Packit 423ecb
	    xmlErrEncodingInt(NULL, XML_ERR_INVALID_CHAR,
Packit 423ecb
		    "Internal error, xmlCopyCharMultiByte 0x%X out of bound\n",
Packit 423ecb
			      val);
Packit 423ecb
	    return(0);
Packit 423ecb
	}
Packit 423ecb
	for ( ; bits >= 0; bits-= 6)
Packit 423ecb
	    *out++= ((val >> bits) & 0x3F) | 0x80 ;
Packit 423ecb
	return (out - savedout);
Packit 423ecb
    }
Packit 423ecb
    *out = (xmlChar) val;
Packit 423ecb
    return 1;
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlCopyChar:
Packit 423ecb
 * @len:  Ignored, compatibility
Packit 423ecb
 * @out:  pointer to an array of xmlChar
Packit 423ecb
 * @val:  the char value
Packit 423ecb
 *
Packit 423ecb
 * append the char value in the array
Packit 423ecb
 *
Packit 423ecb
 * Returns the number of xmlChar written
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
int
Packit 423ecb
xmlCopyChar(int len ATTRIBUTE_UNUSED, xmlChar *out, int val) {
Packit 423ecb
    if (out == NULL) return(0);
Packit 423ecb
    /* the len parameter is ignored */
Packit 423ecb
    if  (val >= 0x80) {
Packit 423ecb
	return(xmlCopyCharMultiByte (out, val));
Packit 423ecb
    }
Packit 423ecb
    *out = (xmlChar) val;
Packit 423ecb
    return 1;
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/************************************************************************
Packit 423ecb
 *									*
Packit 423ecb
 *		Commodity functions to switch encodings			*
Packit 423ecb
 *									*
Packit 423ecb
 ************************************************************************/
Packit 423ecb
Packit 423ecb
static int
Packit 423ecb
xmlSwitchToEncodingInt(xmlParserCtxtPtr ctxt,
Packit 423ecb
                       xmlCharEncodingHandlerPtr handler, int len);
Packit 423ecb
static int
Packit 423ecb
xmlSwitchInputEncodingInt(xmlParserCtxtPtr ctxt, xmlParserInputPtr input,
Packit 423ecb
                          xmlCharEncodingHandlerPtr handler, int len);
Packit 423ecb
/**
Packit 423ecb
 * xmlSwitchEncoding:
Packit 423ecb
 * @ctxt:  the parser context
Packit 423ecb
 * @enc:  the encoding value (number)
Packit 423ecb
 *
Packit 423ecb
 * change the input functions when discovering the character encoding
Packit 423ecb
 * of a given entity.
Packit 423ecb
 *
Packit 423ecb
 * Returns 0 in case of success, -1 otherwise
Packit 423ecb
 */
Packit 423ecb
int
Packit 423ecb
xmlSwitchEncoding(xmlParserCtxtPtr ctxt, xmlCharEncoding enc)
Packit 423ecb
{
Packit 423ecb
    xmlCharEncodingHandlerPtr handler;
Packit 423ecb
    int len = -1;
Packit 423ecb
    int ret;
Packit 423ecb
Packit 423ecb
    if (ctxt == NULL) return(-1);
Packit 423ecb
    switch (enc) {
Packit 423ecb
	case XML_CHAR_ENCODING_ERROR:
Packit 423ecb
	    __xmlErrEncoding(ctxt, XML_ERR_UNKNOWN_ENCODING,
Packit 423ecb
	                   "encoding unknown\n", NULL, NULL);
Packit 423ecb
	    return(-1);
Packit 423ecb
	case XML_CHAR_ENCODING_NONE:
Packit 423ecb
	    /* let's assume it's UTF-8 without the XML decl */
Packit 423ecb
	    ctxt->charset = XML_CHAR_ENCODING_UTF8;
Packit 423ecb
	    return(0);
Packit 423ecb
	case XML_CHAR_ENCODING_UTF8:
Packit 423ecb
	    /* default encoding, no conversion should be needed */
Packit 423ecb
	    ctxt->charset = XML_CHAR_ENCODING_UTF8;
Packit 423ecb
Packit 423ecb
	    /*
Packit 423ecb
	     * Errata on XML-1.0 June 20 2001
Packit 423ecb
	     * Specific handling of the Byte Order Mark for
Packit 423ecb
	     * UTF-8
Packit 423ecb
	     */
Packit 423ecb
	    if ((ctxt->input != NULL) &&
Packit 423ecb
		(ctxt->input->cur[0] == 0xEF) &&
Packit 423ecb
		(ctxt->input->cur[1] == 0xBB) &&
Packit 423ecb
		(ctxt->input->cur[2] == 0xBF)) {
Packit 423ecb
		ctxt->input->cur += 3;
Packit 423ecb
	    }
Packit 423ecb
	    return(0);
Packit 423ecb
    case XML_CHAR_ENCODING_UTF16LE:
Packit 423ecb
    case XML_CHAR_ENCODING_UTF16BE:
Packit 423ecb
        /*The raw input characters are encoded
Packit 423ecb
         *in UTF-16. As we expect this function
Packit 423ecb
         *to be called after xmlCharEncInFunc, we expect
Packit 423ecb
         *ctxt->input->cur to contain UTF-8 encoded characters.
Packit 423ecb
         *So the raw UTF16 Byte Order Mark
Packit 423ecb
         *has also been converted into
Packit 423ecb
         *an UTF-8 BOM. Let's skip that BOM.
Packit 423ecb
         */
Packit 423ecb
        if ((ctxt->input != NULL) && (ctxt->input->cur != NULL) &&
Packit 423ecb
            (ctxt->input->cur[0] == 0xEF) &&
Packit 423ecb
            (ctxt->input->cur[1] == 0xBB) &&
Packit 423ecb
            (ctxt->input->cur[2] == 0xBF)) {
Packit 423ecb
            ctxt->input->cur += 3;
Packit 423ecb
        }
Packit 423ecb
        len = 90;
Packit 423ecb
	break;
Packit 423ecb
    case XML_CHAR_ENCODING_UCS2:
Packit 423ecb
        len = 90;
Packit 423ecb
	break;
Packit 423ecb
    case XML_CHAR_ENCODING_UCS4BE:
Packit 423ecb
    case XML_CHAR_ENCODING_UCS4LE:
Packit 423ecb
    case XML_CHAR_ENCODING_UCS4_2143:
Packit 423ecb
    case XML_CHAR_ENCODING_UCS4_3412:
Packit 423ecb
        len = 180;
Packit 423ecb
	break;
Packit 423ecb
    case XML_CHAR_ENCODING_EBCDIC:
Packit 423ecb
    case XML_CHAR_ENCODING_8859_1:
Packit 423ecb
    case XML_CHAR_ENCODING_8859_2:
Packit 423ecb
    case XML_CHAR_ENCODING_8859_3:
Packit 423ecb
    case XML_CHAR_ENCODING_8859_4:
Packit 423ecb
    case XML_CHAR_ENCODING_8859_5:
Packit 423ecb
    case XML_CHAR_ENCODING_8859_6:
Packit 423ecb
    case XML_CHAR_ENCODING_8859_7:
Packit 423ecb
    case XML_CHAR_ENCODING_8859_8:
Packit 423ecb
    case XML_CHAR_ENCODING_8859_9:
Packit 423ecb
    case XML_CHAR_ENCODING_ASCII:
Packit 423ecb
    case XML_CHAR_ENCODING_2022_JP:
Packit 423ecb
    case XML_CHAR_ENCODING_SHIFT_JIS:
Packit 423ecb
    case XML_CHAR_ENCODING_EUC_JP:
Packit 423ecb
        len = 45;
Packit 423ecb
	break;
Packit 423ecb
    }
Packit 423ecb
    handler = xmlGetCharEncodingHandler(enc);
Packit 423ecb
    if (handler == NULL) {
Packit 423ecb
	/*
Packit 423ecb
	 * Default handlers.
Packit 423ecb
	 */
Packit 423ecb
	switch (enc) {
Packit 423ecb
	    case XML_CHAR_ENCODING_ASCII:
Packit 423ecb
		/* default encoding, no conversion should be needed */
Packit 423ecb
		ctxt->charset = XML_CHAR_ENCODING_UTF8;
Packit 423ecb
		return(0);
Packit 423ecb
	    case XML_CHAR_ENCODING_UTF16LE:
Packit 423ecb
		break;
Packit 423ecb
	    case XML_CHAR_ENCODING_UTF16BE:
Packit 423ecb
		break;
Packit 423ecb
	    case XML_CHAR_ENCODING_UCS4LE:
Packit 423ecb
		__xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
Packit 423ecb
			       "encoding not supported %s\n",
Packit 423ecb
			       BAD_CAST "USC4 little endian", NULL);
Packit 423ecb
		break;
Packit 423ecb
	    case XML_CHAR_ENCODING_UCS4BE:
Packit 423ecb
		__xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
Packit 423ecb
			       "encoding not supported %s\n",
Packit 423ecb
			       BAD_CAST "USC4 big endian", NULL);
Packit 423ecb
		break;
Packit 423ecb
	    case XML_CHAR_ENCODING_EBCDIC:
Packit 423ecb
		__xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
Packit 423ecb
			       "encoding not supported %s\n",
Packit 423ecb
			       BAD_CAST "EBCDIC", NULL);
Packit 423ecb
		break;
Packit 423ecb
	    case XML_CHAR_ENCODING_UCS4_2143:
Packit 423ecb
		__xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
Packit 423ecb
			       "encoding not supported %s\n",
Packit 423ecb
			       BAD_CAST "UCS4 2143", NULL);
Packit 423ecb
		break;
Packit 423ecb
	    case XML_CHAR_ENCODING_UCS4_3412:
Packit 423ecb
		__xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
Packit 423ecb
			       "encoding not supported %s\n",
Packit 423ecb
			       BAD_CAST "UCS4 3412", NULL);
Packit 423ecb
		break;
Packit 423ecb
	    case XML_CHAR_ENCODING_UCS2:
Packit 423ecb
		__xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
Packit 423ecb
			       "encoding not supported %s\n",
Packit 423ecb
			       BAD_CAST "UCS2", NULL);
Packit 423ecb
		break;
Packit 423ecb
	    case XML_CHAR_ENCODING_8859_1:
Packit 423ecb
	    case XML_CHAR_ENCODING_8859_2:
Packit 423ecb
	    case XML_CHAR_ENCODING_8859_3:
Packit 423ecb
	    case XML_CHAR_ENCODING_8859_4:
Packit 423ecb
	    case XML_CHAR_ENCODING_8859_5:
Packit 423ecb
	    case XML_CHAR_ENCODING_8859_6:
Packit 423ecb
	    case XML_CHAR_ENCODING_8859_7:
Packit 423ecb
	    case XML_CHAR_ENCODING_8859_8:
Packit 423ecb
	    case XML_CHAR_ENCODING_8859_9:
Packit 423ecb
		/*
Packit 423ecb
		 * We used to keep the internal content in the
Packit 423ecb
		 * document encoding however this turns being unmaintainable
Packit 423ecb
		 * So xmlGetCharEncodingHandler() will return non-null
Packit 423ecb
		 * values for this now.
Packit 423ecb
		 */
Packit 423ecb
		if ((ctxt->inputNr == 1) &&
Packit 423ecb
		    (ctxt->encoding == NULL) &&
Packit 423ecb
		    (ctxt->input != NULL) &&
Packit 423ecb
		    (ctxt->input->encoding != NULL)) {
Packit 423ecb
		    ctxt->encoding = xmlStrdup(ctxt->input->encoding);
Packit 423ecb
		}
Packit 423ecb
		ctxt->charset = enc;
Packit 423ecb
		return(0);
Packit 423ecb
	    case XML_CHAR_ENCODING_2022_JP:
Packit 423ecb
		__xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
Packit 423ecb
			       "encoding not supported %s\n",
Packit 423ecb
			       BAD_CAST "ISO-2022-JP", NULL);
Packit 423ecb
		break;
Packit 423ecb
	    case XML_CHAR_ENCODING_SHIFT_JIS:
Packit 423ecb
		__xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
Packit 423ecb
			       "encoding not supported %s\n",
Packit 423ecb
			       BAD_CAST "Shift_JIS", NULL);
Packit 423ecb
		break;
Packit 423ecb
	    case XML_CHAR_ENCODING_EUC_JP:
Packit 423ecb
		__xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
Packit 423ecb
			       "encoding not supported %s\n",
Packit 423ecb
			       BAD_CAST "EUC-JP", NULL);
Packit 423ecb
		break;
Packit 423ecb
	    default:
Packit 423ecb
	        break;
Packit 423ecb
	}
Packit 423ecb
    }
Packit 423ecb
    /*
Packit 423ecb
     * TODO: We could recover from errors in external entites if we
Packit 423ecb
     * didn't stop the parser. But most callers of this function don't
Packit 423ecb
     * check the return value.
Packit 423ecb
     */
Packit 423ecb
    if (handler == NULL) {
Packit 423ecb
        xmlStopParser(ctxt);
Packit 423ecb
	return(-1);
Packit 423ecb
    }
Packit 423ecb
    ctxt->charset = XML_CHAR_ENCODING_UTF8;
Packit 423ecb
    ret = xmlSwitchToEncodingInt(ctxt, handler, len);
Packit 423ecb
    if ((ret < 0) || (ctxt->errNo == XML_I18N_CONV_FAILED)) {
Packit 423ecb
        /*
Packit 423ecb
	 * on encoding conversion errors, stop the parser
Packit 423ecb
	 */
Packit 423ecb
        xmlStopParser(ctxt);
Packit 423ecb
	ctxt->errNo = XML_I18N_CONV_FAILED;
Packit 423ecb
    }
Packit 423ecb
    return(ret);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlSwitchInputEncoding:
Packit 423ecb
 * @ctxt:  the parser context
Packit 423ecb
 * @input:  the input stream
Packit 423ecb
 * @handler:  the encoding handler
Packit 423ecb
 * @len:  the number of bytes to convert for the first line or -1
Packit 423ecb
 *
Packit 423ecb
 * change the input functions when discovering the character encoding
Packit 423ecb
 * of a given entity.
Packit 423ecb
 *
Packit 423ecb
 * Returns 0 in case of success, -1 otherwise
Packit 423ecb
 */
Packit 423ecb
static int
Packit 423ecb
xmlSwitchInputEncodingInt(xmlParserCtxtPtr ctxt, xmlParserInputPtr input,
Packit 423ecb
                          xmlCharEncodingHandlerPtr handler, int len)
Packit 423ecb
{
Packit 423ecb
    int nbchars;
Packit 423ecb
Packit 423ecb
    if (handler == NULL)
Packit 423ecb
        return (-1);
Packit 423ecb
    if (input == NULL)
Packit 423ecb
        return (-1);
Packit 423ecb
    if (input->buf != NULL) {
Packit 423ecb
        if (input->buf->encoder != NULL) {
Packit 423ecb
            /*
Packit 423ecb
             * Check in case the auto encoding detetection triggered
Packit 423ecb
             * in already.
Packit 423ecb
             */
Packit 423ecb
            if (input->buf->encoder == handler)
Packit 423ecb
                return (0);
Packit 423ecb
Packit 423ecb
            /*
Packit 423ecb
             * "UTF-16" can be used for both LE and BE
Packit 423ecb
             if ((!xmlStrncmp(BAD_CAST input->buf->encoder->name,
Packit 423ecb
             BAD_CAST "UTF-16", 6)) &&
Packit 423ecb
             (!xmlStrncmp(BAD_CAST handler->name,
Packit 423ecb
             BAD_CAST "UTF-16", 6))) {
Packit 423ecb
             return(0);
Packit 423ecb
             }
Packit 423ecb
             */
Packit 423ecb
Packit 423ecb
            /*
Packit 423ecb
             * Note: this is a bit dangerous, but that's what it
Packit 423ecb
             * takes to use nearly compatible signature for different
Packit 423ecb
             * encodings.
Packit 423ecb
             */
Packit 423ecb
            xmlCharEncCloseFunc(input->buf->encoder);
Packit 423ecb
            input->buf->encoder = handler;
Packit 423ecb
            return (0);
Packit 423ecb
        }
Packit 423ecb
        input->buf->encoder = handler;
Packit 423ecb
Packit 423ecb
        /*
Packit 423ecb
         * Is there already some content down the pipe to convert ?
Packit 423ecb
         */
Packit 423ecb
        if (xmlBufIsEmpty(input->buf->buffer) == 0) {
Packit 423ecb
            int processed;
Packit 423ecb
	    unsigned int use;
Packit 423ecb
Packit 423ecb
            /*
Packit 423ecb
             * Specific handling of the Byte Order Mark for
Packit 423ecb
             * UTF-16
Packit 423ecb
             */
Packit 423ecb
            if ((handler->name != NULL) &&
Packit 423ecb
                (!strcmp(handler->name, "UTF-16LE") ||
Packit 423ecb
                 !strcmp(handler->name, "UTF-16")) &&
Packit 423ecb
                (input->cur[0] == 0xFF) && (input->cur[1] == 0xFE)) {
Packit 423ecb
                input->cur += 2;
Packit 423ecb
            }
Packit 423ecb
            if ((handler->name != NULL) &&
Packit 423ecb
                (!strcmp(handler->name, "UTF-16BE")) &&
Packit 423ecb
                (input->cur[0] == 0xFE) && (input->cur[1] == 0xFF)) {
Packit 423ecb
                input->cur += 2;
Packit 423ecb
            }
Packit 423ecb
            /*
Packit 423ecb
             * Errata on XML-1.0 June 20 2001
Packit 423ecb
             * Specific handling of the Byte Order Mark for
Packit 423ecb
             * UTF-8
Packit 423ecb
             */
Packit 423ecb
            if ((handler->name != NULL) &&
Packit 423ecb
                (!strcmp(handler->name, "UTF-8")) &&
Packit 423ecb
                (input->cur[0] == 0xEF) &&
Packit 423ecb
                (input->cur[1] == 0xBB) && (input->cur[2] == 0xBF)) {
Packit 423ecb
                input->cur += 3;
Packit 423ecb
            }
Packit 423ecb
Packit 423ecb
            /*
Packit 423ecb
             * Shrink the current input buffer.
Packit 423ecb
             * Move it as the raw buffer and create a new input buffer
Packit 423ecb
             */
Packit 423ecb
            processed = input->cur - input->base;
Packit 423ecb
            xmlBufShrink(input->buf->buffer, processed);
Packit 423ecb
            input->buf->raw = input->buf->buffer;
Packit 423ecb
            input->buf->buffer = xmlBufCreate();
Packit 423ecb
	    input->buf->rawconsumed = processed;
Packit 423ecb
	    use = xmlBufUse(input->buf->raw);
Packit 423ecb
Packit 423ecb
            if (ctxt->html) {
Packit 423ecb
                /*
Packit 423ecb
                 * convert as much as possible of the buffer
Packit 423ecb
                 */
Packit 423ecb
                nbchars = xmlCharEncInput(input->buf, 1);
Packit 423ecb
            } else {
Packit 423ecb
                /*
Packit 423ecb
                 * convert just enough to get
Packit 423ecb
                 * ''
Packit 423ecb
                 * parsed with the autodetected encoding
Packit 423ecb
                 * into the parser reading buffer.
Packit 423ecb
                 */
Packit 423ecb
                nbchars = xmlCharEncFirstLineInput(input->buf, len);
Packit 423ecb
            }
Packit 423ecb
            xmlBufResetInput(input->buf->buffer, input);
Packit 423ecb
            if (nbchars < 0) {
Packit 423ecb
                xmlErrInternal(ctxt,
Packit 423ecb
                               "switching encoding: encoder error\n",
Packit 423ecb
                               NULL);
Packit 423ecb
                return (-1);
Packit 423ecb
            }
Packit 423ecb
	    input->buf->rawconsumed += use - xmlBufUse(input->buf->raw);
Packit 423ecb
        }
Packit 423ecb
        return (0);
Packit 423ecb
    } else if (input->length == 0) {
Packit 423ecb
	/*
Packit 423ecb
	 * When parsing a static memory array one must know the
Packit 423ecb
	 * size to be able to convert the buffer.
Packit 423ecb
	 */
Packit 423ecb
	xmlErrInternal(ctxt, "switching encoding : no input\n", NULL);
Packit 423ecb
	return (-1);
Packit 423ecb
    }
Packit 423ecb
    return (0);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlSwitchInputEncoding:
Packit 423ecb
 * @ctxt:  the parser context
Packit 423ecb
 * @input:  the input stream
Packit 423ecb
 * @handler:  the encoding handler
Packit 423ecb
 *
Packit 423ecb
 * change the input functions when discovering the character encoding
Packit 423ecb
 * of a given entity.
Packit 423ecb
 *
Packit 423ecb
 * Returns 0 in case of success, -1 otherwise
Packit 423ecb
 */
Packit 423ecb
int
Packit 423ecb
xmlSwitchInputEncoding(xmlParserCtxtPtr ctxt, xmlParserInputPtr input,
Packit 423ecb
                          xmlCharEncodingHandlerPtr handler) {
Packit 423ecb
    return(xmlSwitchInputEncodingInt(ctxt, input, handler, -1));
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlSwitchToEncodingInt:
Packit 423ecb
 * @ctxt:  the parser context
Packit 423ecb
 * @handler:  the encoding handler
Packit 423ecb
 * @len: the length to convert or -1
Packit 423ecb
 *
Packit 423ecb
 * change the input functions when discovering the character encoding
Packit 423ecb
 * of a given entity, and convert only @len bytes of the output, this
Packit 423ecb
 * is needed on auto detect to allows any declared encoding later to
Packit 423ecb
 * convert the actual content after the xmlDecl
Packit 423ecb
 *
Packit 423ecb
 * Returns 0 in case of success, -1 otherwise
Packit 423ecb
 */
Packit 423ecb
static int
Packit 423ecb
xmlSwitchToEncodingInt(xmlParserCtxtPtr ctxt,
Packit 423ecb
                       xmlCharEncodingHandlerPtr handler, int len) {
Packit 423ecb
    int ret = 0;
Packit 423ecb
Packit 423ecb
    if (handler != NULL) {
Packit 423ecb
        if (ctxt->input != NULL) {
Packit 423ecb
	    ret = xmlSwitchInputEncodingInt(ctxt, ctxt->input, handler, len);
Packit 423ecb
	} else {
Packit 423ecb
	    xmlErrInternal(ctxt, "xmlSwitchToEncoding : no input\n",
Packit 423ecb
	                   NULL);
Packit 423ecb
	    return(-1);
Packit 423ecb
	}
Packit 423ecb
	/*
Packit 423ecb
	 * The parsing is now done in UTF8 natively
Packit 423ecb
	 */
Packit 423ecb
	ctxt->charset = XML_CHAR_ENCODING_UTF8;
Packit 423ecb
    } else
Packit 423ecb
	return(-1);
Packit 423ecb
    return(ret);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlSwitchToEncoding:
Packit 423ecb
 * @ctxt:  the parser context
Packit 423ecb
 * @handler:  the encoding handler
Packit 423ecb
 *
Packit 423ecb
 * change the input functions when discovering the character encoding
Packit 423ecb
 * of a given entity.
Packit 423ecb
 *
Packit 423ecb
 * Returns 0 in case of success, -1 otherwise
Packit 423ecb
 */
Packit 423ecb
int
Packit 423ecb
xmlSwitchToEncoding(xmlParserCtxtPtr ctxt, xmlCharEncodingHandlerPtr handler)
Packit 423ecb
{
Packit 423ecb
    return (xmlSwitchToEncodingInt(ctxt, handler, -1));
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/************************************************************************
Packit 423ecb
 *									*
Packit 423ecb
 *	Commodity functions to handle entities processing		*
Packit 423ecb
 *									*
Packit 423ecb
 ************************************************************************/
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlFreeInputStream:
Packit 423ecb
 * @input:  an xmlParserInputPtr
Packit 423ecb
 *
Packit 423ecb
 * Free up an input stream.
Packit 423ecb
 */
Packit 423ecb
void
Packit 423ecb
xmlFreeInputStream(xmlParserInputPtr input) {
Packit 423ecb
    if (input == NULL) return;
Packit 423ecb
Packit 423ecb
    if (input->filename != NULL) xmlFree((char *) input->filename);
Packit 423ecb
    if (input->directory != NULL) xmlFree((char *) input->directory);
Packit 423ecb
    if (input->encoding != NULL) xmlFree((char *) input->encoding);
Packit 423ecb
    if (input->version != NULL) xmlFree((char *) input->version);
Packit 423ecb
    if ((input->free != NULL) && (input->base != NULL))
Packit 423ecb
        input->free((xmlChar *) input->base);
Packit 423ecb
    if (input->buf != NULL)
Packit 423ecb
        xmlFreeParserInputBuffer(input->buf);
Packit 423ecb
    xmlFree(input);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlNewInputStream:
Packit 423ecb
 * @ctxt:  an XML parser context
Packit 423ecb
 *
Packit 423ecb
 * Create a new input stream structure.
Packit 423ecb
 *
Packit 423ecb
 * Returns the new input stream or NULL
Packit 423ecb
 */
Packit 423ecb
xmlParserInputPtr
Packit 423ecb
xmlNewInputStream(xmlParserCtxtPtr ctxt) {
Packit 423ecb
    xmlParserInputPtr input;
Packit 423ecb
Packit 423ecb
    input = (xmlParserInputPtr) xmlMalloc(sizeof(xmlParserInput));
Packit 423ecb
    if (input == NULL) {
Packit 423ecb
        xmlErrMemory(ctxt,  "couldn't allocate a new input stream\n");
Packit 423ecb
	return(NULL);
Packit 423ecb
    }
Packit 423ecb
    memset(input, 0, sizeof(xmlParserInput));
Packit 423ecb
    input->line = 1;
Packit 423ecb
    input->col = 1;
Packit 423ecb
    input->standalone = -1;
Packit 423ecb
Packit 423ecb
    /*
Packit 423ecb
     * If the context is NULL the id cannot be initialized, but that
Packit 423ecb
     * should not happen while parsing which is the situation where
Packit 423ecb
     * the id is actually needed.
Packit 423ecb
     */
Packit 423ecb
    if (ctxt != NULL)
Packit 423ecb
        input->id = ctxt->input_id++;
Packit 423ecb
Packit 423ecb
    return(input);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlNewIOInputStream:
Packit 423ecb
 * @ctxt:  an XML parser context
Packit 423ecb
 * @input:  an I/O Input
Packit 423ecb
 * @enc:  the charset encoding if known
Packit 423ecb
 *
Packit 423ecb
 * Create a new input stream structure encapsulating the @input into
Packit 423ecb
 * a stream suitable for the parser.
Packit 423ecb
 *
Packit 423ecb
 * Returns the new input stream or NULL
Packit 423ecb
 */
Packit 423ecb
xmlParserInputPtr
Packit 423ecb
xmlNewIOInputStream(xmlParserCtxtPtr ctxt, xmlParserInputBufferPtr input,
Packit 423ecb
	            xmlCharEncoding enc) {
Packit 423ecb
    xmlParserInputPtr inputStream;
Packit 423ecb
Packit 423ecb
    if (input == NULL) return(NULL);
Packit 423ecb
    if (xmlParserDebugEntities)
Packit 423ecb
	xmlGenericError(xmlGenericErrorContext, "new input from I/O\n");
Packit 423ecb
    inputStream = xmlNewInputStream(ctxt);
Packit 423ecb
    if (inputStream == NULL) {
Packit 423ecb
	return(NULL);
Packit 423ecb
    }
Packit 423ecb
    inputStream->filename = NULL;
Packit 423ecb
    inputStream->buf = input;
Packit 423ecb
    xmlBufResetInput(inputStream->buf->buffer, inputStream);
Packit 423ecb
Packit 423ecb
    if (enc != XML_CHAR_ENCODING_NONE) {
Packit 423ecb
        xmlSwitchEncoding(ctxt, enc);
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    return(inputStream);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlNewEntityInputStream:
Packit 423ecb
 * @ctxt:  an XML parser context
Packit 423ecb
 * @entity:  an Entity pointer
Packit 423ecb
 *
Packit 423ecb
 * Create a new input stream based on an xmlEntityPtr
Packit 423ecb
 *
Packit 423ecb
 * Returns the new input stream or NULL
Packit 423ecb
 */
Packit 423ecb
xmlParserInputPtr
Packit 423ecb
xmlNewEntityInputStream(xmlParserCtxtPtr ctxt, xmlEntityPtr entity) {
Packit 423ecb
    xmlParserInputPtr input;
Packit 423ecb
Packit 423ecb
    if (entity == NULL) {
Packit 423ecb
        xmlErrInternal(ctxt, "xmlNewEntityInputStream entity = NULL\n",
Packit 423ecb
	               NULL);
Packit 423ecb
	return(NULL);
Packit 423ecb
    }
Packit 423ecb
    if (xmlParserDebugEntities)
Packit 423ecb
	xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
		"new input from entity: %s\n", entity->name);
Packit 423ecb
    if (entity->content == NULL) {
Packit 423ecb
	switch (entity->etype) {
Packit 423ecb
            case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
Packit 423ecb
	        xmlErrInternal(ctxt, "Cannot parse entity %s\n",
Packit 423ecb
		               entity->name);
Packit 423ecb
                break;
Packit 423ecb
            case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
Packit 423ecb
            case XML_EXTERNAL_PARAMETER_ENTITY:
Packit 423ecb
		return(xmlLoadExternalEntity((char *) entity->URI,
Packit 423ecb
		       (char *) entity->ExternalID, ctxt));
Packit 423ecb
            case XML_INTERNAL_GENERAL_ENTITY:
Packit 423ecb
	        xmlErrInternal(ctxt,
Packit 423ecb
		      "Internal entity %s without content !\n",
Packit 423ecb
		               entity->name);
Packit 423ecb
                break;
Packit 423ecb
            case XML_INTERNAL_PARAMETER_ENTITY:
Packit 423ecb
	        xmlErrInternal(ctxt,
Packit 423ecb
		      "Internal parameter entity %s without content !\n",
Packit 423ecb
		               entity->name);
Packit 423ecb
                break;
Packit 423ecb
            case XML_INTERNAL_PREDEFINED_ENTITY:
Packit 423ecb
	        xmlErrInternal(ctxt,
Packit 423ecb
		      "Predefined entity %s without content !\n",
Packit 423ecb
		               entity->name);
Packit 423ecb
                break;
Packit 423ecb
	}
Packit 423ecb
	return(NULL);
Packit 423ecb
    }
Packit 423ecb
    input = xmlNewInputStream(ctxt);
Packit 423ecb
    if (input == NULL) {
Packit 423ecb
	return(NULL);
Packit 423ecb
    }
Packit 423ecb
    if (entity->URI != NULL)
Packit 423ecb
	input->filename = (char *) xmlStrdup((xmlChar *) entity->URI);
Packit 423ecb
    input->base = entity->content;
Packit 423ecb
    if (entity->length == 0)
Packit 423ecb
        entity->length = xmlStrlen(entity->content);
Packit 423ecb
    input->cur = entity->content;
Packit 423ecb
    input->length = entity->length;
Packit 423ecb
    input->end = &entity->content[input->length];
Packit 423ecb
    return(input);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlNewStringInputStream:
Packit 423ecb
 * @ctxt:  an XML parser context
Packit 423ecb
 * @buffer:  an memory buffer
Packit 423ecb
 *
Packit 423ecb
 * Create a new input stream based on a memory buffer.
Packit 423ecb
 * Returns the new input stream
Packit 423ecb
 */
Packit 423ecb
xmlParserInputPtr
Packit 423ecb
xmlNewStringInputStream(xmlParserCtxtPtr ctxt, const xmlChar *buffer) {
Packit 423ecb
    xmlParserInputPtr input;
Packit 423ecb
Packit 423ecb
    if (buffer == NULL) {
Packit 423ecb
        xmlErrInternal(ctxt, "xmlNewStringInputStream string = NULL\n",
Packit 423ecb
	               NULL);
Packit 423ecb
	return(NULL);
Packit 423ecb
    }
Packit 423ecb
    if (xmlParserDebugEntities)
Packit 423ecb
	xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
		"new fixed input: %.30s\n", buffer);
Packit 423ecb
    input = xmlNewInputStream(ctxt);
Packit 423ecb
    if (input == NULL) {
Packit 423ecb
        xmlErrMemory(ctxt,  "couldn't allocate a new input stream\n");
Packit 423ecb
	return(NULL);
Packit 423ecb
    }
Packit 423ecb
    input->base = buffer;
Packit 423ecb
    input->cur = buffer;
Packit 423ecb
    input->length = xmlStrlen(buffer);
Packit 423ecb
    input->end = &buffer[input->length];
Packit 423ecb
    return(input);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlNewInputFromFile:
Packit 423ecb
 * @ctxt:  an XML parser context
Packit 423ecb
 * @filename:  the filename to use as entity
Packit 423ecb
 *
Packit 423ecb
 * Create a new input stream based on a file or an URL.
Packit 423ecb
 *
Packit 423ecb
 * Returns the new input stream or NULL in case of error
Packit 423ecb
 */
Packit 423ecb
xmlParserInputPtr
Packit 423ecb
xmlNewInputFromFile(xmlParserCtxtPtr ctxt, const char *filename) {
Packit 423ecb
    xmlParserInputBufferPtr buf;
Packit 423ecb
    xmlParserInputPtr inputStream;
Packit 423ecb
    char *directory = NULL;
Packit 423ecb
    xmlChar *URI = NULL;
Packit 423ecb
Packit 423ecb
    if (xmlParserDebugEntities)
Packit 423ecb
	xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
		"new input from file: %s\n", filename);
Packit 423ecb
    if (ctxt == NULL) return(NULL);
Packit 423ecb
    buf = xmlParserInputBufferCreateFilename(filename, XML_CHAR_ENCODING_NONE);
Packit 423ecb
    if (buf == NULL) {
Packit 423ecb
	if (filename == NULL)
Packit 423ecb
	    __xmlLoaderErr(ctxt,
Packit 423ecb
	                   "failed to load external entity: NULL filename \n",
Packit 423ecb
			   NULL);
Packit 423ecb
	else
Packit 423ecb
	    __xmlLoaderErr(ctxt, "failed to load external entity \"%s\"\n",
Packit 423ecb
			   (const char *) filename);
Packit 423ecb
	return(NULL);
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    inputStream = xmlNewInputStream(ctxt);
Packit 423ecb
    if (inputStream == NULL)
Packit 423ecb
	return(NULL);
Packit 423ecb
Packit 423ecb
    inputStream->buf = buf;
Packit 423ecb
    inputStream = xmlCheckHTTPInput(ctxt, inputStream);
Packit 423ecb
    if (inputStream == NULL)
Packit 423ecb
        return(NULL);
Packit 423ecb
Packit 423ecb
    if (inputStream->filename == NULL)
Packit 423ecb
	URI = xmlStrdup((xmlChar *) filename);
Packit 423ecb
    else
Packit 423ecb
	URI = xmlStrdup((xmlChar *) inputStream->filename);
Packit 423ecb
    directory = xmlParserGetDirectory((const char *) URI);
Packit 423ecb
    if (inputStream->filename != NULL) xmlFree((char *)inputStream->filename);
Packit 423ecb
    inputStream->filename = (char *) xmlCanonicPath((const xmlChar *) URI);
Packit 423ecb
    if (URI != NULL) xmlFree((char *) URI);
Packit 423ecb
    inputStream->directory = directory;
Packit 423ecb
Packit 423ecb
    xmlBufResetInput(inputStream->buf->buffer, inputStream);
Packit 423ecb
    if ((ctxt->directory == NULL) && (directory != NULL))
Packit 423ecb
        ctxt->directory = (char *) xmlStrdup((const xmlChar *) directory);
Packit 423ecb
    return(inputStream);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/************************************************************************
Packit 423ecb
 *									*
Packit 423ecb
 *		Commodity functions to handle parser contexts		*
Packit 423ecb
 *									*
Packit 423ecb
 ************************************************************************/
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlInitParserCtxt:
Packit 423ecb
 * @ctxt:  an XML parser context
Packit 423ecb
 *
Packit 423ecb
 * Initialize a parser context
Packit 423ecb
 *
Packit 423ecb
 * Returns 0 in case of success and -1 in case of error
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
int
Packit 423ecb
xmlInitParserCtxt(xmlParserCtxtPtr ctxt)
Packit 423ecb
{
Packit 423ecb
    xmlParserInputPtr input;
Packit 423ecb
Packit 423ecb
    if(ctxt==NULL) {
Packit 423ecb
        xmlErrInternal(NULL, "Got NULL parser context\n", NULL);
Packit 423ecb
        return(-1);
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    xmlDefaultSAXHandlerInit();
Packit 423ecb
Packit 423ecb
    if (ctxt->dict == NULL)
Packit 423ecb
	ctxt->dict = xmlDictCreate();
Packit 423ecb
    if (ctxt->dict == NULL) {
Packit 423ecb
        xmlErrMemory(NULL, "cannot initialize parser context\n");
Packit 423ecb
	return(-1);
Packit 423ecb
    }
Packit 423ecb
    xmlDictSetLimit(ctxt->dict, XML_MAX_DICTIONARY_LIMIT);
Packit 423ecb
Packit 423ecb
    if (ctxt->sax == NULL)
Packit 423ecb
	ctxt->sax = (xmlSAXHandler *) xmlMalloc(sizeof(xmlSAXHandler));
Packit 423ecb
    if (ctxt->sax == NULL) {
Packit 423ecb
        xmlErrMemory(NULL, "cannot initialize parser context\n");
Packit 423ecb
	return(-1);
Packit 423ecb
    }
Packit 423ecb
    else
Packit 423ecb
        xmlSAXVersion(ctxt->sax, 2);
Packit 423ecb
Packit 423ecb
    ctxt->maxatts = 0;
Packit 423ecb
    ctxt->atts = NULL;
Packit 423ecb
    /* Allocate the Input stack */
Packit 423ecb
    if (ctxt->inputTab == NULL) {
Packit 423ecb
	ctxt->inputTab = (xmlParserInputPtr *)
Packit 423ecb
		    xmlMalloc(5 * sizeof(xmlParserInputPtr));
Packit 423ecb
	ctxt->inputMax = 5;
Packit 423ecb
    }
Packit 423ecb
    if (ctxt->inputTab == NULL) {
Packit 423ecb
        xmlErrMemory(NULL, "cannot initialize parser context\n");
Packit 423ecb
	ctxt->inputNr = 0;
Packit 423ecb
	ctxt->inputMax = 0;
Packit 423ecb
	ctxt->input = NULL;
Packit 423ecb
	return(-1);
Packit 423ecb
    }
Packit 423ecb
    while ((input = inputPop(ctxt)) != NULL) { /* Non consuming */
Packit 423ecb
        xmlFreeInputStream(input);
Packit 423ecb
    }
Packit 423ecb
    ctxt->inputNr = 0;
Packit 423ecb
    ctxt->input = NULL;
Packit 423ecb
Packit 423ecb
    ctxt->version = NULL;
Packit 423ecb
    ctxt->encoding = NULL;
Packit 423ecb
    ctxt->standalone = -1;
Packit 423ecb
    ctxt->hasExternalSubset = 0;
Packit 423ecb
    ctxt->hasPErefs = 0;
Packit 423ecb
    ctxt->html = 0;
Packit 423ecb
    ctxt->external = 0;
Packit 423ecb
    ctxt->instate = XML_PARSER_START;
Packit 423ecb
    ctxt->token = 0;
Packit 423ecb
    ctxt->directory = NULL;
Packit 423ecb
Packit 423ecb
    /* Allocate the Node stack */
Packit 423ecb
    if (ctxt->nodeTab == NULL) {
Packit 423ecb
	ctxt->nodeTab = (xmlNodePtr *) xmlMalloc(10 * sizeof(xmlNodePtr));
Packit 423ecb
	ctxt->nodeMax = 10;
Packit 423ecb
    }
Packit 423ecb
    if (ctxt->nodeTab == NULL) {
Packit 423ecb
        xmlErrMemory(NULL, "cannot initialize parser context\n");
Packit 423ecb
	ctxt->nodeNr = 0;
Packit 423ecb
	ctxt->nodeMax = 0;
Packit 423ecb
	ctxt->node = NULL;
Packit 423ecb
	ctxt->inputNr = 0;
Packit 423ecb
	ctxt->inputMax = 0;
Packit 423ecb
	ctxt->input = NULL;
Packit 423ecb
	return(-1);
Packit 423ecb
    }
Packit 423ecb
    ctxt->nodeNr = 0;
Packit 423ecb
    ctxt->node = NULL;
Packit 423ecb
Packit 423ecb
    /* Allocate the Name stack */
Packit 423ecb
    if (ctxt->nameTab == NULL) {
Packit 423ecb
	ctxt->nameTab = (const xmlChar **) xmlMalloc(10 * sizeof(xmlChar *));
Packit 423ecb
	ctxt->nameMax = 10;
Packit 423ecb
    }
Packit 423ecb
    if (ctxt->nameTab == NULL) {
Packit 423ecb
        xmlErrMemory(NULL, "cannot initialize parser context\n");
Packit 423ecb
	ctxt->nodeNr = 0;
Packit 423ecb
	ctxt->nodeMax = 0;
Packit 423ecb
	ctxt->node = NULL;
Packit 423ecb
	ctxt->inputNr = 0;
Packit 423ecb
	ctxt->inputMax = 0;
Packit 423ecb
	ctxt->input = NULL;
Packit 423ecb
	ctxt->nameNr = 0;
Packit 423ecb
	ctxt->nameMax = 0;
Packit 423ecb
	ctxt->name = NULL;
Packit 423ecb
	return(-1);
Packit 423ecb
    }
Packit 423ecb
    ctxt->nameNr = 0;
Packit 423ecb
    ctxt->name = NULL;
Packit 423ecb
Packit 423ecb
    /* Allocate the space stack */
Packit 423ecb
    if (ctxt->spaceTab == NULL) {
Packit 423ecb
	ctxt->spaceTab = (int *) xmlMalloc(10 * sizeof(int));
Packit 423ecb
	ctxt->spaceMax = 10;
Packit 423ecb
    }
Packit 423ecb
    if (ctxt->spaceTab == NULL) {
Packit 423ecb
        xmlErrMemory(NULL, "cannot initialize parser context\n");
Packit 423ecb
	ctxt->nodeNr = 0;
Packit 423ecb
	ctxt->nodeMax = 0;
Packit 423ecb
	ctxt->node = NULL;
Packit 423ecb
	ctxt->inputNr = 0;
Packit 423ecb
	ctxt->inputMax = 0;
Packit 423ecb
	ctxt->input = NULL;
Packit 423ecb
	ctxt->nameNr = 0;
Packit 423ecb
	ctxt->nameMax = 0;
Packit 423ecb
	ctxt->name = NULL;
Packit 423ecb
	ctxt->spaceNr = 0;
Packit 423ecb
	ctxt->spaceMax = 0;
Packit 423ecb
	ctxt->space = NULL;
Packit 423ecb
	return(-1);
Packit 423ecb
    }
Packit 423ecb
    ctxt->spaceNr = 1;
Packit 423ecb
    ctxt->spaceMax = 10;
Packit 423ecb
    ctxt->spaceTab[0] = -1;
Packit 423ecb
    ctxt->space = &ctxt->spaceTab[0];
Packit 423ecb
    ctxt->userData = ctxt;
Packit 423ecb
    ctxt->myDoc = NULL;
Packit 423ecb
    ctxt->wellFormed = 1;
Packit 423ecb
    ctxt->nsWellFormed = 1;
Packit 423ecb
    ctxt->valid = 1;
Packit 423ecb
    ctxt->loadsubset = xmlLoadExtDtdDefaultValue;
Packit 423ecb
    if (ctxt->loadsubset) {
Packit 423ecb
        ctxt->options |= XML_PARSE_DTDLOAD;
Packit 423ecb
    }
Packit 423ecb
    ctxt->validate = xmlDoValidityCheckingDefaultValue;
Packit 423ecb
    ctxt->pedantic = xmlPedanticParserDefaultValue;
Packit 423ecb
    if (ctxt->pedantic) {
Packit 423ecb
        ctxt->options |= XML_PARSE_PEDANTIC;
Packit 423ecb
    }
Packit 423ecb
    ctxt->linenumbers = xmlLineNumbersDefaultValue;
Packit 423ecb
    ctxt->keepBlanks = xmlKeepBlanksDefaultValue;
Packit 423ecb
    if (ctxt->keepBlanks == 0) {
Packit 423ecb
	ctxt->sax->ignorableWhitespace = xmlSAX2IgnorableWhitespace;
Packit 423ecb
	ctxt->options |= XML_PARSE_NOBLANKS;
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    ctxt->vctxt.finishDtd = XML_CTXT_FINISH_DTD_0;
Packit 423ecb
    ctxt->vctxt.userData = ctxt;
Packit 423ecb
    ctxt->vctxt.error = xmlParserValidityError;
Packit 423ecb
    ctxt->vctxt.warning = xmlParserValidityWarning;
Packit 423ecb
    if (ctxt->validate) {
Packit 423ecb
	if (xmlGetWarningsDefaultValue == 0)
Packit 423ecb
	    ctxt->vctxt.warning = NULL;
Packit 423ecb
	else
Packit 423ecb
	    ctxt->vctxt.warning = xmlParserValidityWarning;
Packit 423ecb
	ctxt->vctxt.nodeMax = 0;
Packit 423ecb
        ctxt->options |= XML_PARSE_DTDVALID;
Packit 423ecb
    }
Packit 423ecb
    ctxt->replaceEntities = xmlSubstituteEntitiesDefaultValue;
Packit 423ecb
    if (ctxt->replaceEntities) {
Packit 423ecb
        ctxt->options |= XML_PARSE_NOENT;
Packit 423ecb
    }
Packit 423ecb
    ctxt->record_info = 0;
Packit 423ecb
    ctxt->nbChars = 0;
Packit 423ecb
    ctxt->checkIndex = 0;
Packit 423ecb
    ctxt->inSubset = 0;
Packit 423ecb
    ctxt->errNo = XML_ERR_OK;
Packit 423ecb
    ctxt->depth = 0;
Packit 423ecb
    ctxt->charset = XML_CHAR_ENCODING_UTF8;
Packit 423ecb
    ctxt->catalogs = NULL;
Packit 423ecb
    ctxt->nbentities = 0;
Packit 423ecb
    ctxt->sizeentities = 0;
Packit 423ecb
    ctxt->sizeentcopy = 0;
Packit 423ecb
    ctxt->input_id = 1;
Packit 423ecb
    xmlInitNodeInfoSeq(&ctxt->node_seq);
Packit 423ecb
    return(0);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlFreeParserCtxt:
Packit 423ecb
 * @ctxt:  an XML parser context
Packit 423ecb
 *
Packit 423ecb
 * Free all the memory used by a parser context. However the parsed
Packit 423ecb
 * document in ctxt->myDoc is not freed.
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
void
Packit 423ecb
xmlFreeParserCtxt(xmlParserCtxtPtr ctxt)
Packit 423ecb
{
Packit 423ecb
    xmlParserInputPtr input;
Packit 423ecb
Packit 423ecb
    if (ctxt == NULL) return;
Packit 423ecb
Packit 423ecb
    while ((input = inputPop(ctxt)) != NULL) { /* Non consuming */
Packit 423ecb
        xmlFreeInputStream(input);
Packit 423ecb
    }
Packit 423ecb
    if (ctxt->spaceTab != NULL) xmlFree(ctxt->spaceTab);
Packit 423ecb
    if (ctxt->nameTab != NULL) xmlFree((xmlChar * *)ctxt->nameTab);
Packit 423ecb
    if (ctxt->nodeTab != NULL) xmlFree(ctxt->nodeTab);
Packit 423ecb
    if (ctxt->nodeInfoTab != NULL) xmlFree(ctxt->nodeInfoTab);
Packit 423ecb
    if (ctxt->inputTab != NULL) xmlFree(ctxt->inputTab);
Packit 423ecb
    if (ctxt->version != NULL) xmlFree((char *) ctxt->version);
Packit 423ecb
    if (ctxt->encoding != NULL) xmlFree((char *) ctxt->encoding);
Packit 423ecb
    if (ctxt->extSubURI != NULL) xmlFree((char *) ctxt->extSubURI);
Packit 423ecb
    if (ctxt->extSubSystem != NULL) xmlFree((char *) ctxt->extSubSystem);
Packit 423ecb
#ifdef LIBXML_SAX1_ENABLED
Packit 423ecb
    if ((ctxt->sax != NULL) &&
Packit 423ecb
        (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler))
Packit 423ecb
#else
Packit 423ecb
    if (ctxt->sax != NULL)
Packit 423ecb
#endif /* LIBXML_SAX1_ENABLED */
Packit 423ecb
        xmlFree(ctxt->sax);
Packit 423ecb
    if (ctxt->directory != NULL) xmlFree((char *) ctxt->directory);
Packit 423ecb
    if (ctxt->vctxt.nodeTab != NULL) xmlFree(ctxt->vctxt.nodeTab);
Packit 423ecb
    if (ctxt->atts != NULL) xmlFree((xmlChar * *)ctxt->atts);
Packit 423ecb
    if (ctxt->dict != NULL) xmlDictFree(ctxt->dict);
Packit 423ecb
    if (ctxt->nsTab != NULL) xmlFree((char *) ctxt->nsTab);
Packit 423ecb
    if (ctxt->pushTab != NULL) xmlFree(ctxt->pushTab);
Packit 423ecb
    if (ctxt->attallocs != NULL) xmlFree(ctxt->attallocs);
Packit 423ecb
    if (ctxt->attsDefault != NULL)
Packit 423ecb
        xmlHashFree(ctxt->attsDefault, (xmlHashDeallocator) xmlFree);
Packit 423ecb
    if (ctxt->attsSpecial != NULL)
Packit 423ecb
        xmlHashFree(ctxt->attsSpecial, NULL);
Packit 423ecb
    if (ctxt->freeElems != NULL) {
Packit 423ecb
        xmlNodePtr cur, next;
Packit 423ecb
Packit 423ecb
	cur = ctxt->freeElems;
Packit 423ecb
	while (cur != NULL) {
Packit 423ecb
	    next = cur->next;
Packit 423ecb
	    xmlFree(cur);
Packit 423ecb
	    cur = next;
Packit 423ecb
	}
Packit 423ecb
    }
Packit 423ecb
    if (ctxt->freeAttrs != NULL) {
Packit 423ecb
        xmlAttrPtr cur, next;
Packit 423ecb
Packit 423ecb
	cur = ctxt->freeAttrs;
Packit 423ecb
	while (cur != NULL) {
Packit 423ecb
	    next = cur->next;
Packit 423ecb
	    xmlFree(cur);
Packit 423ecb
	    cur = next;
Packit 423ecb
	}
Packit 423ecb
    }
Packit 423ecb
    /*
Packit 423ecb
     * cleanup the error strings
Packit 423ecb
     */
Packit 423ecb
    if (ctxt->lastError.message != NULL)
Packit 423ecb
        xmlFree(ctxt->lastError.message);
Packit 423ecb
    if (ctxt->lastError.file != NULL)
Packit 423ecb
        xmlFree(ctxt->lastError.file);
Packit 423ecb
    if (ctxt->lastError.str1 != NULL)
Packit 423ecb
        xmlFree(ctxt->lastError.str1);
Packit 423ecb
    if (ctxt->lastError.str2 != NULL)
Packit 423ecb
        xmlFree(ctxt->lastError.str2);
Packit 423ecb
    if (ctxt->lastError.str3 != NULL)
Packit 423ecb
        xmlFree(ctxt->lastError.str3);
Packit 423ecb
Packit 423ecb
#ifdef LIBXML_CATALOG_ENABLED
Packit 423ecb
    if (ctxt->catalogs != NULL)
Packit 423ecb
	xmlCatalogFreeLocal(ctxt->catalogs);
Packit 423ecb
#endif
Packit 423ecb
    xmlFree(ctxt);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlNewParserCtxt:
Packit 423ecb
 *
Packit 423ecb
 * Allocate and initialize a new parser context.
Packit 423ecb
 *
Packit 423ecb
 * Returns the xmlParserCtxtPtr or NULL
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
xmlParserCtxtPtr
Packit 423ecb
xmlNewParserCtxt(void)
Packit 423ecb
{
Packit 423ecb
    xmlParserCtxtPtr ctxt;
Packit 423ecb
Packit 423ecb
    ctxt = (xmlParserCtxtPtr) xmlMalloc(sizeof(xmlParserCtxt));
Packit 423ecb
    if (ctxt == NULL) {
Packit 423ecb
	xmlErrMemory(NULL, "cannot allocate parser context\n");
Packit 423ecb
	return(NULL);
Packit 423ecb
    }
Packit 423ecb
    memset(ctxt, 0, sizeof(xmlParserCtxt));
Packit 423ecb
    if (xmlInitParserCtxt(ctxt) < 0) {
Packit 423ecb
        xmlFreeParserCtxt(ctxt);
Packit 423ecb
	return(NULL);
Packit 423ecb
    }
Packit 423ecb
    return(ctxt);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/************************************************************************
Packit 423ecb
 *									*
Packit 423ecb
 *		Handling of node informations				*
Packit 423ecb
 *									*
Packit 423ecb
 ************************************************************************/
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlClearParserCtxt:
Packit 423ecb
 * @ctxt:  an XML parser context
Packit 423ecb
 *
Packit 423ecb
 * Clear (release owned resources) and reinitialize a parser context
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
void
Packit 423ecb
xmlClearParserCtxt(xmlParserCtxtPtr ctxt)
Packit 423ecb
{
Packit 423ecb
  if (ctxt==NULL)
Packit 423ecb
    return;
Packit 423ecb
  xmlClearNodeInfoSeq(&ctxt->node_seq);
Packit 423ecb
  xmlCtxtReset(ctxt);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlParserFindNodeInfo:
Packit 423ecb
 * @ctx:  an XML parser context
Packit 423ecb
 * @node:  an XML node within the tree
Packit 423ecb
 *
Packit 423ecb
 * Find the parser node info struct for a given node
Packit 423ecb
 *
Packit 423ecb
 * Returns an xmlParserNodeInfo block pointer or NULL
Packit 423ecb
 */
Packit 423ecb
const xmlParserNodeInfo *
Packit 423ecb
xmlParserFindNodeInfo(const xmlParserCtxtPtr ctx, const xmlNodePtr node)
Packit 423ecb
{
Packit 423ecb
    unsigned long pos;
Packit 423ecb
Packit 423ecb
    if ((ctx == NULL) || (node == NULL))
Packit 423ecb
        return (NULL);
Packit 423ecb
    /* Find position where node should be at */
Packit 423ecb
    pos = xmlParserFindNodeInfoIndex(&ctx->node_seq, node);
Packit 423ecb
    if (pos < ctx->node_seq.length
Packit 423ecb
        && ctx->node_seq.buffer[pos].node == node)
Packit 423ecb
        return &ctx->node_seq.buffer[pos];
Packit 423ecb
    else
Packit 423ecb
        return NULL;
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlInitNodeInfoSeq:
Packit 423ecb
 * @seq:  a node info sequence pointer
Packit 423ecb
 *
Packit 423ecb
 * -- Initialize (set to initial state) node info sequence
Packit 423ecb
 */
Packit 423ecb
void
Packit 423ecb
xmlInitNodeInfoSeq(xmlParserNodeInfoSeqPtr seq)
Packit 423ecb
{
Packit 423ecb
    if (seq == NULL)
Packit 423ecb
        return;
Packit 423ecb
    seq->length = 0;
Packit 423ecb
    seq->maximum = 0;
Packit 423ecb
    seq->buffer = NULL;
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlClearNodeInfoSeq:
Packit 423ecb
 * @seq:  a node info sequence pointer
Packit 423ecb
 *
Packit 423ecb
 * -- Clear (release memory and reinitialize) node
Packit 423ecb
 *   info sequence
Packit 423ecb
 */
Packit 423ecb
void
Packit 423ecb
xmlClearNodeInfoSeq(xmlParserNodeInfoSeqPtr seq)
Packit 423ecb
{
Packit 423ecb
    if (seq == NULL)
Packit 423ecb
        return;
Packit 423ecb
    if (seq->buffer != NULL)
Packit 423ecb
        xmlFree(seq->buffer);
Packit 423ecb
    xmlInitNodeInfoSeq(seq);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlParserFindNodeInfoIndex:
Packit 423ecb
 * @seq:  a node info sequence pointer
Packit 423ecb
 * @node:  an XML node pointer
Packit 423ecb
 *
Packit 423ecb
 *
Packit 423ecb
 * xmlParserFindNodeInfoIndex : Find the index that the info record for
Packit 423ecb
 *   the given node is or should be at in a sorted sequence
Packit 423ecb
 *
Packit 423ecb
 * Returns a long indicating the position of the record
Packit 423ecb
 */
Packit 423ecb
unsigned long
Packit 423ecb
xmlParserFindNodeInfoIndex(const xmlParserNodeInfoSeqPtr seq,
Packit 423ecb
                           const xmlNodePtr node)
Packit 423ecb
{
Packit 423ecb
    unsigned long upper, lower, middle;
Packit 423ecb
    int found = 0;
Packit 423ecb
Packit 423ecb
    if ((seq == NULL) || (node == NULL))
Packit 423ecb
        return ((unsigned long) -1);
Packit 423ecb
Packit 423ecb
    /* Do a binary search for the key */
Packit 423ecb
    lower = 1;
Packit 423ecb
    upper = seq->length;
Packit 423ecb
    middle = 0;
Packit 423ecb
    while (lower <= upper && !found) {
Packit 423ecb
        middle = lower + (upper - lower) / 2;
Packit 423ecb
        if (node == seq->buffer[middle - 1].node)
Packit 423ecb
            found = 1;
Packit 423ecb
        else if (node < seq->buffer[middle - 1].node)
Packit 423ecb
            upper = middle - 1;
Packit 423ecb
        else
Packit 423ecb
            lower = middle + 1;
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    /* Return position */
Packit 423ecb
    if (middle == 0 || seq->buffer[middle - 1].node < node)
Packit 423ecb
        return middle;
Packit 423ecb
    else
Packit 423ecb
        return middle - 1;
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlParserAddNodeInfo:
Packit 423ecb
 * @ctxt:  an XML parser context
Packit 423ecb
 * @info:  a node info sequence pointer
Packit 423ecb
 *
Packit 423ecb
 * Insert node info record into the sorted sequence
Packit 423ecb
 */
Packit 423ecb
void
Packit 423ecb
xmlParserAddNodeInfo(xmlParserCtxtPtr ctxt,
Packit 423ecb
                     const xmlParserNodeInfoPtr info)
Packit 423ecb
{
Packit 423ecb
    unsigned long pos;
Packit 423ecb
Packit 423ecb
    if ((ctxt == NULL) || (info == NULL)) return;
Packit 423ecb
Packit 423ecb
    /* Find pos and check to see if node is already in the sequence */
Packit 423ecb
    pos = xmlParserFindNodeInfoIndex(&ctxt->node_seq, (xmlNodePtr)
Packit 423ecb
                                     info->node);
Packit 423ecb
Packit 423ecb
    if ((pos < ctxt->node_seq.length) &&
Packit 423ecb
        (ctxt->node_seq.buffer != NULL) &&
Packit 423ecb
        (ctxt->node_seq.buffer[pos].node == info->node)) {
Packit 423ecb
        ctxt->node_seq.buffer[pos] = *info;
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    /* Otherwise, we need to add new node to buffer */
Packit 423ecb
    else {
Packit 423ecb
        if ((ctxt->node_seq.length + 1 > ctxt->node_seq.maximum) ||
Packit 423ecb
	    (ctxt->node_seq.buffer == NULL)) {
Packit 423ecb
            xmlParserNodeInfo *tmp_buffer;
Packit 423ecb
            unsigned int byte_size;
Packit 423ecb
Packit 423ecb
            if (ctxt->node_seq.maximum == 0)
Packit 423ecb
                ctxt->node_seq.maximum = 2;
Packit 423ecb
            byte_size = (sizeof(*ctxt->node_seq.buffer) *
Packit 423ecb
			(2 * ctxt->node_seq.maximum));
Packit 423ecb
Packit 423ecb
            if (ctxt->node_seq.buffer == NULL)
Packit 423ecb
                tmp_buffer = (xmlParserNodeInfo *) xmlMalloc(byte_size);
Packit 423ecb
            else
Packit 423ecb
                tmp_buffer =
Packit 423ecb
                    (xmlParserNodeInfo *) xmlRealloc(ctxt->node_seq.buffer,
Packit 423ecb
                                                     byte_size);
Packit 423ecb
Packit 423ecb
            if (tmp_buffer == NULL) {
Packit 423ecb
		xmlErrMemory(ctxt, "failed to allocate buffer\n");
Packit 423ecb
                return;
Packit 423ecb
            }
Packit 423ecb
            ctxt->node_seq.buffer = tmp_buffer;
Packit 423ecb
            ctxt->node_seq.maximum *= 2;
Packit 423ecb
        }
Packit 423ecb
Packit 423ecb
        /* If position is not at end, move elements out of the way */
Packit 423ecb
        if (pos != ctxt->node_seq.length) {
Packit 423ecb
            unsigned long i;
Packit 423ecb
Packit 423ecb
            for (i = ctxt->node_seq.length; i > pos; i--)
Packit 423ecb
                ctxt->node_seq.buffer[i] = ctxt->node_seq.buffer[i - 1];
Packit 423ecb
        }
Packit 423ecb
Packit 423ecb
        /* Copy element and increase length */
Packit 423ecb
        ctxt->node_seq.buffer[pos] = *info;
Packit 423ecb
        ctxt->node_seq.length++;
Packit 423ecb
    }
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/************************************************************************
Packit 423ecb
 *									*
Packit 423ecb
 *		Defaults settings					*
Packit 423ecb
 *									*
Packit 423ecb
 ************************************************************************/
Packit 423ecb
/**
Packit 423ecb
 * xmlPedanticParserDefault:
Packit 423ecb
 * @val:  int 0 or 1
Packit 423ecb
 *
Packit 423ecb
 * Set and return the previous value for enabling pedantic warnings.
Packit 423ecb
 *
Packit 423ecb
 * Returns the last value for 0 for no substitution, 1 for substitution.
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
int
Packit 423ecb
xmlPedanticParserDefault(int val) {
Packit 423ecb
    int old = xmlPedanticParserDefaultValue;
Packit 423ecb
Packit 423ecb
    xmlPedanticParserDefaultValue = val;
Packit 423ecb
    return(old);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlLineNumbersDefault:
Packit 423ecb
 * @val:  int 0 or 1
Packit 423ecb
 *
Packit 423ecb
 * Set and return the previous value for enabling line numbers in elements
Packit 423ecb
 * contents. This may break on old application and is turned off by default.
Packit 423ecb
 *
Packit 423ecb
 * Returns the last value for 0 for no substitution, 1 for substitution.
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
int
Packit 423ecb
xmlLineNumbersDefault(int val) {
Packit 423ecb
    int old = xmlLineNumbersDefaultValue;
Packit 423ecb
Packit 423ecb
    xmlLineNumbersDefaultValue = val;
Packit 423ecb
    return(old);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlSubstituteEntitiesDefault:
Packit 423ecb
 * @val:  int 0 or 1
Packit 423ecb
 *
Packit 423ecb
 * Set and return the previous value for default entity support.
Packit 423ecb
 * Initially the parser always keep entity references instead of substituting
Packit 423ecb
 * entity values in the output. This function has to be used to change the
Packit 423ecb
 * default parser behavior
Packit 423ecb
 * SAX::substituteEntities() has to be used for changing that on a file by
Packit 423ecb
 * file basis.
Packit 423ecb
 *
Packit 423ecb
 * Returns the last value for 0 for no substitution, 1 for substitution.
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
int
Packit 423ecb
xmlSubstituteEntitiesDefault(int val) {
Packit 423ecb
    int old = xmlSubstituteEntitiesDefaultValue;
Packit 423ecb
Packit 423ecb
    xmlSubstituteEntitiesDefaultValue = val;
Packit 423ecb
    return(old);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlKeepBlanksDefault:
Packit 423ecb
 * @val:  int 0 or 1
Packit 423ecb
 *
Packit 423ecb
 * Set and return the previous value for default blanks text nodes support.
Packit 423ecb
 * The 1.x version of the parser used an heuristic to try to detect
Packit 423ecb
 * ignorable white spaces. As a result the SAX callback was generating
Packit 423ecb
 * xmlSAX2IgnorableWhitespace() callbacks instead of characters() one, and when
Packit 423ecb
 * using the DOM output text nodes containing those blanks were not generated.
Packit 423ecb
 * The 2.x and later version will switch to the XML standard way and
Packit 423ecb
 * ignorableWhitespace() are only generated when running the parser in
Packit 423ecb
 * validating mode and when the current element doesn't allow CDATA or
Packit 423ecb
 * mixed content.
Packit 423ecb
 * This function is provided as a way to force the standard behavior
Packit 423ecb
 * on 1.X libs and to switch back to the old mode for compatibility when
Packit 423ecb
 * running 1.X client code on 2.X . Upgrade of 1.X code should be done
Packit 423ecb
 * by using xmlIsBlankNode() commodity function to detect the "empty"
Packit 423ecb
 * nodes generated.
Packit 423ecb
 * This value also affect autogeneration of indentation when saving code
Packit 423ecb
 * if blanks sections are kept, indentation is not generated.
Packit 423ecb
 *
Packit 423ecb
 * Returns the last value for 0 for no substitution, 1 for substitution.
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
int
Packit 423ecb
xmlKeepBlanksDefault(int val) {
Packit 423ecb
    int old = xmlKeepBlanksDefaultValue;
Packit 423ecb
Packit 423ecb
    xmlKeepBlanksDefaultValue = val;
Packit 423ecb
    if (!val) xmlIndentTreeOutput = 1;
Packit 423ecb
    return(old);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
#define bottom_parserInternals
Packit 423ecb
#include "elfgcchack.h"