Blame buf.c

Packit 423ecb
/*
Packit 423ecb
 * buf.c: memory buffers for libxml2
Packit 423ecb
 *
Packit 423ecb
 * new buffer structures and entry points to simplify the maintainance
Packit 423ecb
 * of libxml2 and ensure we keep good control over memory allocations
Packit 423ecb
 * and stay 64 bits clean.
Packit 423ecb
 * The new entry point use the xmlBufPtr opaque structure and
Packit 423ecb
 * xmlBuf...() counterparts to the old xmlBuf...() functions
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
#include <string.h> /* for memset() only ! */
Packit 423ecb
#include <limits.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
Packit 423ecb
#include <libxml/tree.h>
Packit 423ecb
#include <libxml/globals.h>
Packit 423ecb
#include <libxml/tree.h>
Packit 423ecb
#include <libxml/parserInternals.h> /* for XML_MAX_TEXT_LENGTH */
Packit 423ecb
#include "buf.h"
Packit 423ecb
Packit 423ecb
#define WITH_BUFFER_COMPAT
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlBuf:
Packit 423ecb
 *
Packit 423ecb
 * A buffer structure. The base of the structure is somehow compatible
Packit 423ecb
 * with struct _xmlBuffer to limit risks on application which accessed
Packit 423ecb
 * directly the input->buf->buffer structures.
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
struct _xmlBuf {
Packit 423ecb
    xmlChar *content;		/* The buffer content UTF8 */
Packit 423ecb
    unsigned int compat_use;    /* for binary compatibility */
Packit 423ecb
    unsigned int compat_size;   /* for binary compatibility */
Packit 423ecb
    xmlBufferAllocationScheme alloc; /* The realloc method */
Packit 423ecb
    xmlChar *contentIO;		/* in IO mode we may have a different base */
Packit 423ecb
    size_t use;		        /* The buffer size used */
Packit 423ecb
    size_t size;		/* The buffer size */
Packit 423ecb
    xmlBufferPtr buffer;        /* wrapper for an old buffer */
Packit 423ecb
    int error;                  /* an error code if a failure occurred */
Packit 423ecb
};
Packit 423ecb
Packit 423ecb
#ifdef WITH_BUFFER_COMPAT
Packit 423ecb
/*
Packit 423ecb
 * Macro for compatibility with xmlBuffer to be used after an xmlBuf
Packit 423ecb
 * is updated. This makes sure the compat fields are updated too.
Packit 423ecb
 */
Packit 423ecb
#define UPDATE_COMPAT(buf)				    \
Packit 423ecb
     if (buf->size < INT_MAX) buf->compat_size = buf->size; \
Packit 423ecb
     else buf->compat_size = INT_MAX;			    \
Packit 423ecb
     if (buf->use < INT_MAX) buf->compat_use = buf->use; \
Packit 423ecb
     else buf->compat_use = INT_MAX;
Packit 423ecb
Packit 423ecb
/*
Packit 423ecb
 * Macro for compatibility with xmlBuffer to be used in all the xmlBuf
Packit 423ecb
 * entry points, it checks that the compat fields have not been modified
Packit 423ecb
 * by direct call to xmlBuffer function from code compiled before 2.9.0 .
Packit 423ecb
 */
Packit 423ecb
#define CHECK_COMPAT(buf)				    \
Packit 423ecb
     if (buf->size != (size_t) buf->compat_size)	    \
Packit 423ecb
         if (buf->compat_size < INT_MAX)		    \
Packit 423ecb
	     buf->size = buf->compat_size;		    \
Packit 423ecb
     if (buf->use != (size_t) buf->compat_use)		    \
Packit 423ecb
         if (buf->compat_use < INT_MAX)			    \
Packit 423ecb
	     buf->use = buf->compat_use;
Packit 423ecb
Packit 423ecb
#else /* ! WITH_BUFFER_COMPAT */
Packit 423ecb
#define UPDATE_COMPAT(buf)
Packit 423ecb
#define CHECK_COMPAT(buf)
Packit 423ecb
#endif /* WITH_BUFFER_COMPAT */
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlBufMemoryError:
Packit 423ecb
 * @extra:  extra informations
Packit 423ecb
 *
Packit 423ecb
 * Handle an out of memory condition
Packit 423ecb
 * To be improved...
Packit 423ecb
 */
Packit 423ecb
static void
Packit 423ecb
xmlBufMemoryError(xmlBufPtr buf, const char *extra)
Packit 423ecb
{
Packit 423ecb
    __xmlSimpleError(XML_FROM_BUFFER, XML_ERR_NO_MEMORY, NULL, NULL, extra);
Packit 423ecb
    if ((buf) && (buf->error == 0))
Packit 423ecb
        buf->error = XML_ERR_NO_MEMORY;
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlBufOverflowError:
Packit 423ecb
 * @extra:  extra informations
Packit 423ecb
 *
Packit 423ecb
 * Handle a buffer overflow error
Packit 423ecb
 * To be improved...
Packit 423ecb
 */
Packit 423ecb
static void
Packit 423ecb
xmlBufOverflowError(xmlBufPtr buf, const char *extra)
Packit 423ecb
{
Packit 423ecb
    __xmlSimpleError(XML_FROM_BUFFER, XML_BUF_OVERFLOW, NULL, NULL, extra);
Packit 423ecb
    if ((buf) && (buf->error == 0))
Packit 423ecb
        buf->error = XML_BUF_OVERFLOW;
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlBufCreate:
Packit 423ecb
 *
Packit 423ecb
 * routine to create an XML buffer.
Packit 423ecb
 * returns the new structure.
Packit 423ecb
 */
Packit 423ecb
xmlBufPtr
Packit 423ecb
xmlBufCreate(void) {
Packit 423ecb
    xmlBufPtr ret;
Packit 423ecb
Packit 423ecb
    ret = (xmlBufPtr) xmlMalloc(sizeof(xmlBuf));
Packit 423ecb
    if (ret == NULL) {
Packit 423ecb
	xmlBufMemoryError(NULL, "creating buffer");
Packit 423ecb
        return(NULL);
Packit 423ecb
    }
Packit 423ecb
    ret->compat_use = 0;
Packit 423ecb
    ret->use = 0;
Packit 423ecb
    ret->error = 0;
Packit 423ecb
    ret->buffer = NULL;
Packit 423ecb
    ret->size = xmlDefaultBufferSize;
Packit 423ecb
    ret->compat_size = xmlDefaultBufferSize;
Packit 423ecb
    ret->alloc = xmlBufferAllocScheme;
Packit 423ecb
    ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar));
Packit 423ecb
    if (ret->content == NULL) {
Packit 423ecb
	xmlBufMemoryError(ret, "creating buffer");
Packit 423ecb
	xmlFree(ret);
Packit 423ecb
        return(NULL);
Packit 423ecb
    }
Packit 423ecb
    ret->content[0] = 0;
Packit 423ecb
    ret->contentIO = NULL;
Packit 423ecb
    return(ret);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlBufCreateSize:
Packit 423ecb
 * @size: initial size of buffer
Packit 423ecb
 *
Packit 423ecb
 * routine to create an XML buffer.
Packit 423ecb
 * returns the new structure.
Packit 423ecb
 */
Packit 423ecb
xmlBufPtr
Packit 423ecb
xmlBufCreateSize(size_t size) {
Packit 423ecb
    xmlBufPtr ret;
Packit 423ecb
Packit 423ecb
    ret = (xmlBufPtr) xmlMalloc(sizeof(xmlBuf));
Packit 423ecb
    if (ret == NULL) {
Packit 423ecb
	xmlBufMemoryError(NULL, "creating buffer");
Packit 423ecb
        return(NULL);
Packit 423ecb
    }
Packit 423ecb
    ret->compat_use = 0;
Packit 423ecb
    ret->use = 0;
Packit 423ecb
    ret->error = 0;
Packit 423ecb
    ret->buffer = NULL;
Packit 423ecb
    ret->alloc = xmlBufferAllocScheme;
Packit 423ecb
    ret->size = (size ? size+2 : 0);         /* +1 for ending null */
Packit 423ecb
    ret->compat_size = (int) ret->size;
Packit 423ecb
    if (ret->size){
Packit 423ecb
        ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar));
Packit 423ecb
        if (ret->content == NULL) {
Packit 423ecb
	    xmlBufMemoryError(ret, "creating buffer");
Packit 423ecb
            xmlFree(ret);
Packit 423ecb
            return(NULL);
Packit 423ecb
        }
Packit 423ecb
        ret->content[0] = 0;
Packit 423ecb
    } else
Packit 423ecb
	ret->content = NULL;
Packit 423ecb
    ret->contentIO = NULL;
Packit 423ecb
    return(ret);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlBufDetach:
Packit 423ecb
 * @buf:  the buffer
Packit 423ecb
 *
Packit 423ecb
 * Remove the string contained in a buffer and give it back to the
Packit 423ecb
 * caller. The buffer is reset to an empty content.
Packit 423ecb
 * This doesn't work with immutable buffers as they can't be reset.
Packit 423ecb
 *
Packit 423ecb
 * Returns the previous string contained by the buffer.
Packit 423ecb
 */
Packit 423ecb
xmlChar *
Packit 423ecb
xmlBufDetach(xmlBufPtr buf) {
Packit 423ecb
    xmlChar *ret;
Packit 423ecb
Packit 423ecb
    if (buf == NULL)
Packit 423ecb
        return(NULL);
Packit 423ecb
    if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE)
Packit 423ecb
        return(NULL);
Packit 423ecb
    if (buf->buffer != NULL)
Packit 423ecb
        return(NULL);
Packit 423ecb
    if (buf->error)
Packit 423ecb
        return(NULL);
Packit 423ecb
Packit 423ecb
    ret = buf->content;
Packit 423ecb
    buf->content = NULL;
Packit 423ecb
    buf->size = 0;
Packit 423ecb
    buf->use = 0;
Packit 423ecb
    buf->compat_use = 0;
Packit 423ecb
    buf->compat_size = 0;
Packit 423ecb
Packit 423ecb
    return ret;
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlBufCreateStatic:
Packit 423ecb
 * @mem: the memory area
Packit 423ecb
 * @size:  the size in byte
Packit 423ecb
 *
Packit 423ecb
 * routine to create an XML buffer from an immutable memory area.
Packit 423ecb
 * The area won't be modified nor copied, and is expected to be
Packit 423ecb
 * present until the end of the buffer lifetime.
Packit 423ecb
 *
Packit 423ecb
 * returns the new structure.
Packit 423ecb
 */
Packit 423ecb
xmlBufPtr
Packit 423ecb
xmlBufCreateStatic(void *mem, size_t size) {
Packit 423ecb
    xmlBufPtr ret;
Packit 423ecb
Packit 423ecb
    if (mem == NULL)
Packit 423ecb
        return(NULL);
Packit 423ecb
Packit 423ecb
    ret = (xmlBufPtr) xmlMalloc(sizeof(xmlBuf));
Packit 423ecb
    if (ret == NULL) {
Packit 423ecb
	xmlBufMemoryError(NULL, "creating buffer");
Packit 423ecb
        return(NULL);
Packit 423ecb
    }
Packit 423ecb
    if (size < INT_MAX) {
Packit 423ecb
        ret->compat_use = size;
Packit 423ecb
        ret->compat_size = size;
Packit 423ecb
    } else {
Packit 423ecb
        ret->compat_use = INT_MAX;
Packit 423ecb
        ret->compat_size = INT_MAX;
Packit 423ecb
    }
Packit 423ecb
    ret->use = size;
Packit 423ecb
    ret->size = size;
Packit 423ecb
    ret->alloc = XML_BUFFER_ALLOC_IMMUTABLE;
Packit 423ecb
    ret->content = (xmlChar *) mem;
Packit 423ecb
    ret->error = 0;
Packit 423ecb
    ret->buffer = NULL;
Packit 423ecb
    return(ret);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlBufGetAllocationScheme:
Packit 423ecb
 * @buf:  the buffer
Packit 423ecb
 *
Packit 423ecb
 * Get the buffer allocation scheme
Packit 423ecb
 *
Packit 423ecb
 * Returns the scheme or -1 in case of error
Packit 423ecb
 */
Packit 423ecb
int
Packit 423ecb
xmlBufGetAllocationScheme(xmlBufPtr buf) {
Packit 423ecb
    if (buf == NULL) {
Packit 423ecb
#ifdef DEBUG_BUFFER
Packit 423ecb
        xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
		"xmlBufGetAllocationScheme: buf == NULL\n");
Packit 423ecb
#endif
Packit 423ecb
        return(-1);
Packit 423ecb
    }
Packit 423ecb
    return(buf->alloc);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlBufSetAllocationScheme:
Packit 423ecb
 * @buf:  the buffer to tune
Packit 423ecb
 * @scheme:  allocation scheme to use
Packit 423ecb
 *
Packit 423ecb
 * Sets the allocation scheme for this buffer
Packit 423ecb
 *
Packit 423ecb
 * returns 0 in case of success and -1 in case of failure
Packit 423ecb
 */
Packit 423ecb
int
Packit 423ecb
xmlBufSetAllocationScheme(xmlBufPtr buf,
Packit 423ecb
                          xmlBufferAllocationScheme scheme) {
Packit 423ecb
    if ((buf == NULL) || (buf->error != 0)) {
Packit 423ecb
#ifdef DEBUG_BUFFER
Packit 423ecb
        xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
		"xmlBufSetAllocationScheme: buf == NULL or in error\n");
Packit 423ecb
#endif
Packit 423ecb
        return(-1);
Packit 423ecb
    }
Packit 423ecb
    if ((buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) ||
Packit 423ecb
        (buf->alloc == XML_BUFFER_ALLOC_IO))
Packit 423ecb
        return(-1);
Packit 423ecb
    if ((scheme == XML_BUFFER_ALLOC_DOUBLEIT) ||
Packit 423ecb
        (scheme == XML_BUFFER_ALLOC_EXACT) ||
Packit 423ecb
        (scheme == XML_BUFFER_ALLOC_HYBRID) ||
Packit 423ecb
        (scheme == XML_BUFFER_ALLOC_IMMUTABLE) ||
Packit 423ecb
	(scheme == XML_BUFFER_ALLOC_BOUNDED)) {
Packit 423ecb
	buf->alloc = scheme;
Packit 423ecb
        if (buf->buffer)
Packit 423ecb
            buf->buffer->alloc = scheme;
Packit 423ecb
        return(0);
Packit 423ecb
    }
Packit 423ecb
    /*
Packit 423ecb
     * Switching a buffer ALLOC_IO has the side effect of initializing
Packit 423ecb
     * the contentIO field with the current content
Packit 423ecb
     */
Packit 423ecb
    if (scheme == XML_BUFFER_ALLOC_IO) {
Packit 423ecb
        buf->alloc = XML_BUFFER_ALLOC_IO;
Packit 423ecb
        buf->contentIO = buf->content;
Packit 423ecb
    }
Packit 423ecb
    return(-1);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlBufFree:
Packit 423ecb
 * @buf:  the buffer to free
Packit 423ecb
 *
Packit 423ecb
 * Frees an XML buffer. It frees both the content and the structure which
Packit 423ecb
 * encapsulate it.
Packit 423ecb
 */
Packit 423ecb
void
Packit 423ecb
xmlBufFree(xmlBufPtr buf) {
Packit 423ecb
    if (buf == NULL) {
Packit 423ecb
#ifdef DEBUG_BUFFER
Packit 423ecb
        xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
		"xmlBufFree: buf == NULL\n");
Packit 423ecb
#endif
Packit 423ecb
	return;
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    if ((buf->alloc == XML_BUFFER_ALLOC_IO) &&
Packit 423ecb
        (buf->contentIO != NULL)) {
Packit 423ecb
        xmlFree(buf->contentIO);
Packit 423ecb
    } else if ((buf->content != NULL) &&
Packit 423ecb
        (buf->alloc != XML_BUFFER_ALLOC_IMMUTABLE)) {
Packit 423ecb
        xmlFree(buf->content);
Packit 423ecb
    }
Packit 423ecb
    xmlFree(buf);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlBufEmpty:
Packit 423ecb
 * @buf:  the buffer
Packit 423ecb
 *
Packit 423ecb
 * empty a buffer.
Packit 423ecb
 */
Packit 423ecb
void
Packit 423ecb
xmlBufEmpty(xmlBufPtr buf) {
Packit 423ecb
    if ((buf == NULL) || (buf->error != 0)) return;
Packit 423ecb
    if (buf->content == NULL) return;
Packit 423ecb
    CHECK_COMPAT(buf)
Packit 423ecb
    buf->use = 0;
Packit 423ecb
    if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) {
Packit 423ecb
        buf->content = BAD_CAST "";
Packit 423ecb
    } else if ((buf->alloc == XML_BUFFER_ALLOC_IO) &&
Packit 423ecb
               (buf->contentIO != NULL)) {
Packit 423ecb
        size_t start_buf = buf->content - buf->contentIO;
Packit 423ecb
Packit 423ecb
	buf->size += start_buf;
Packit 423ecb
        buf->content = buf->contentIO;
Packit 423ecb
        buf->content[0] = 0;
Packit 423ecb
    } else {
Packit 423ecb
        buf->content[0] = 0;
Packit 423ecb
    }
Packit 423ecb
    UPDATE_COMPAT(buf)
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlBufShrink:
Packit 423ecb
 * @buf:  the buffer to dump
Packit 423ecb
 * @len:  the number of xmlChar to remove
Packit 423ecb
 *
Packit 423ecb
 * Remove the beginning of an XML buffer.
Packit 423ecb
 * NOTE that this routine behaviour differs from xmlBufferShrink()
Packit 423ecb
 * as it will return 0 on error instead of -1 due to size_t being
Packit 423ecb
 * used as the return type.
Packit 423ecb
 *
Packit 423ecb
 * Returns the number of byte removed or 0 in case of failure
Packit 423ecb
 */
Packit 423ecb
size_t
Packit 423ecb
xmlBufShrink(xmlBufPtr buf, size_t len) {
Packit 423ecb
    if ((buf == NULL) || (buf->error != 0)) return(0);
Packit 423ecb
    CHECK_COMPAT(buf)
Packit 423ecb
    if (len == 0) return(0);
Packit 423ecb
    if (len > buf->use) return(0);
Packit 423ecb
Packit 423ecb
    buf->use -= len;
Packit 423ecb
    if ((buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) ||
Packit 423ecb
        ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL))) {
Packit 423ecb
	/*
Packit 423ecb
	 * we just move the content pointer, but also make sure
Packit 423ecb
	 * the perceived buffer size has shrinked accordingly
Packit 423ecb
	 */
Packit 423ecb
        buf->content += len;
Packit 423ecb
	buf->size -= len;
Packit 423ecb
Packit 423ecb
        /*
Packit 423ecb
	 * sometimes though it maybe be better to really shrink
Packit 423ecb
	 * on IO buffers
Packit 423ecb
	 */
Packit 423ecb
	if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
Packit 423ecb
	    size_t start_buf = buf->content - buf->contentIO;
Packit 423ecb
	    if (start_buf >= buf->size) {
Packit 423ecb
		memmove(buf->contentIO, &buf->content[0], buf->use);
Packit 423ecb
		buf->content = buf->contentIO;
Packit 423ecb
		buf->content[buf->use] = 0;
Packit 423ecb
		buf->size += start_buf;
Packit 423ecb
	    }
Packit 423ecb
	}
Packit 423ecb
    } else {
Packit 423ecb
	memmove(buf->content, &buf->content[len], buf->use);
Packit 423ecb
	buf->content[buf->use] = 0;
Packit 423ecb
    }
Packit 423ecb
    UPDATE_COMPAT(buf)
Packit 423ecb
    return(len);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlBufGrowInternal:
Packit 423ecb
 * @buf:  the buffer
Packit 423ecb
 * @len:  the minimum free size to allocate
Packit 423ecb
 *
Packit 423ecb
 * Grow the available space of an XML buffer, @len is the target value
Packit 423ecb
 * Error checking should be done on buf->error since using the return
Packit 423ecb
 * value doesn't work that well
Packit 423ecb
 *
Packit 423ecb
 * Returns 0 in case of error or the length made available otherwise
Packit 423ecb
 */
Packit 423ecb
static size_t
Packit 423ecb
xmlBufGrowInternal(xmlBufPtr buf, size_t len) {
Packit 423ecb
    size_t size;
Packit 423ecb
    xmlChar *newbuf;
Packit 423ecb
Packit 423ecb
    if ((buf == NULL) || (buf->error != 0)) return(0);
Packit 423ecb
    CHECK_COMPAT(buf)
Packit 423ecb
Packit 423ecb
    if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0);
Packit 423ecb
    if (buf->use + len < buf->size)
Packit 423ecb
        return(buf->size - buf->use);
Packit 423ecb
Packit 423ecb
    /*
Packit 423ecb
     * Windows has a BIG problem on realloc timing, so we try to double
Packit 423ecb
     * the buffer size (if that's enough) (bug 146697)
Packit 423ecb
     * Apparently BSD too, and it's probably best for linux too
Packit 423ecb
     * On an embedded system this may be something to change
Packit 423ecb
     */
Packit 423ecb
#if 1
Packit 423ecb
    if (buf->size > (size_t) len)
Packit 423ecb
        size = buf->size * 2;
Packit 423ecb
    else
Packit 423ecb
        size = buf->use + len + 100;
Packit 423ecb
#else
Packit 423ecb
    size = buf->use + len + 100;
Packit 423ecb
#endif
Packit 423ecb
Packit 423ecb
    if (buf->alloc == XML_BUFFER_ALLOC_BOUNDED) {
Packit 423ecb
        /*
Packit 423ecb
	 * Used to provide parsing limits
Packit 423ecb
	 */
Packit 423ecb
        if ((buf->use + len >= XML_MAX_TEXT_LENGTH) ||
Packit 423ecb
	    (buf->size >= XML_MAX_TEXT_LENGTH)) {
Packit 423ecb
	    xmlBufMemoryError(buf, "buffer error: text too long\n");
Packit 423ecb
	    return(0);
Packit 423ecb
	}
Packit 423ecb
	if (size >= XML_MAX_TEXT_LENGTH)
Packit 423ecb
	    size = XML_MAX_TEXT_LENGTH;
Packit 423ecb
    }
Packit 423ecb
    if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
Packit 423ecb
        size_t start_buf = buf->content - buf->contentIO;
Packit 423ecb
Packit 423ecb
	newbuf = (xmlChar *) xmlRealloc(buf->contentIO, start_buf + size);
Packit 423ecb
	if (newbuf == NULL) {
Packit 423ecb
	    xmlBufMemoryError(buf, "growing buffer");
Packit 423ecb
	    return(0);
Packit 423ecb
	}
Packit 423ecb
	buf->contentIO = newbuf;
Packit 423ecb
	buf->content = newbuf + start_buf;
Packit 423ecb
    } else {
Packit 423ecb
	newbuf = (xmlChar *) xmlRealloc(buf->content, size);
Packit 423ecb
	if (newbuf == NULL) {
Packit 423ecb
	    xmlBufMemoryError(buf, "growing buffer");
Packit 423ecb
	    return(0);
Packit 423ecb
	}
Packit 423ecb
	buf->content = newbuf;
Packit 423ecb
    }
Packit 423ecb
    buf->size = size;
Packit 423ecb
    UPDATE_COMPAT(buf)
Packit 423ecb
    return(buf->size - buf->use);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlBufGrow:
Packit 423ecb
 * @buf:  the buffer
Packit 423ecb
 * @len:  the minimum free size to allocate
Packit 423ecb
 *
Packit 423ecb
 * Grow the available space of an XML buffer, @len is the target value
Packit 423ecb
 * This is been kept compatible with xmlBufferGrow() as much as possible
Packit 423ecb
 *
Packit 423ecb
 * Returns -1 in case of error or the length made available otherwise
Packit 423ecb
 */
Packit 423ecb
int
Packit 423ecb
xmlBufGrow(xmlBufPtr buf, int len) {
Packit 423ecb
    size_t ret;
Packit 423ecb
Packit 423ecb
    if ((buf == NULL) || (len < 0)) return(-1);
Packit 423ecb
    if (len == 0)
Packit 423ecb
        return(0);
Packit 423ecb
    ret = xmlBufGrowInternal(buf, len);
Packit 423ecb
    if (buf->error != 0)
Packit 423ecb
        return(-1);
Packit 423ecb
    return((int) ret);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlBufInflate:
Packit 423ecb
 * @buf:  the buffer
Packit 423ecb
 * @len:  the minimum extra free size to allocate
Packit 423ecb
 *
Packit 423ecb
 * Grow the available space of an XML buffer, adding at least @len bytes
Packit 423ecb
 *
Packit 423ecb
 * Returns 0 if successful or -1 in case of error
Packit 423ecb
 */
Packit 423ecb
int
Packit 423ecb
xmlBufInflate(xmlBufPtr buf, size_t len) {
Packit 423ecb
    if (buf == NULL) return(-1);
Packit 423ecb
    xmlBufGrowInternal(buf, len + buf->size);
Packit 423ecb
    if (buf->error)
Packit 423ecb
        return(-1);
Packit 423ecb
    return(0);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlBufDump:
Packit 423ecb
 * @file:  the file output
Packit 423ecb
 * @buf:  the buffer to dump
Packit 423ecb
 *
Packit 423ecb
 * Dumps an XML buffer to  a FILE *.
Packit 423ecb
 * Returns the number of #xmlChar written
Packit 423ecb
 */
Packit 423ecb
size_t
Packit 423ecb
xmlBufDump(FILE *file, xmlBufPtr buf) {
Packit 423ecb
    size_t ret;
Packit 423ecb
Packit 423ecb
    if ((buf == NULL) || (buf->error != 0)) {
Packit 423ecb
#ifdef DEBUG_BUFFER
Packit 423ecb
        xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
		"xmlBufDump: buf == NULL or in error\n");
Packit 423ecb
#endif
Packit 423ecb
	return(0);
Packit 423ecb
    }
Packit 423ecb
    if (buf->content == NULL) {
Packit 423ecb
#ifdef DEBUG_BUFFER
Packit 423ecb
        xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
		"xmlBufDump: buf->content == NULL\n");
Packit 423ecb
#endif
Packit 423ecb
	return(0);
Packit 423ecb
    }
Packit 423ecb
    CHECK_COMPAT(buf)
Packit 423ecb
    if (file == NULL)
Packit 423ecb
	file = stdout;
Packit 423ecb
    ret = fwrite(buf->content, sizeof(xmlChar), buf->use, file);
Packit 423ecb
    return(ret);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlBufContent:
Packit 423ecb
 * @buf:  the buffer
Packit 423ecb
 *
Packit 423ecb
 * Function to extract the content of a buffer
Packit 423ecb
 *
Packit 423ecb
 * Returns the internal content
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
xmlChar *
Packit 423ecb
xmlBufContent(const xmlBuf *buf)
Packit 423ecb
{
Packit 423ecb
    if ((!buf) || (buf->error))
Packit 423ecb
        return NULL;
Packit 423ecb
Packit 423ecb
    return(buf->content);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlBufEnd:
Packit 423ecb
 * @buf:  the buffer
Packit 423ecb
 *
Packit 423ecb
 * Function to extract the end of the content of a buffer
Packit 423ecb
 *
Packit 423ecb
 * Returns the end of the internal content or NULL in case of error
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
xmlChar *
Packit 423ecb
xmlBufEnd(xmlBufPtr buf)
Packit 423ecb
{
Packit 423ecb
    if ((!buf) || (buf->error))
Packit 423ecb
        return NULL;
Packit 423ecb
    CHECK_COMPAT(buf)
Packit 423ecb
Packit 423ecb
    return(&buf->content[buf->use]);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlBufAddLen:
Packit 423ecb
 * @buf:  the buffer
Packit 423ecb
 * @len:  the size which were added at the end
Packit 423ecb
 *
Packit 423ecb
 * Sometime data may be added at the end of the buffer without
Packit 423ecb
 * using the xmlBuf APIs that is used to expand the used space
Packit 423ecb
 * and set the zero terminating at the end of the buffer
Packit 423ecb
 *
Packit 423ecb
 * Returns -1 in case of error and 0 otherwise
Packit 423ecb
 */
Packit 423ecb
int
Packit 423ecb
xmlBufAddLen(xmlBufPtr buf, size_t len) {
Packit 423ecb
    if ((buf == NULL) || (buf->error))
Packit 423ecb
        return(-1);
Packit 423ecb
    CHECK_COMPAT(buf)
Packit 423ecb
    if (len > (buf->size - buf->use))
Packit 423ecb
        return(-1);
Packit 423ecb
    buf->use += len;
Packit 423ecb
    UPDATE_COMPAT(buf)
Packit 423ecb
    if (buf->size > buf->use)
Packit 423ecb
        buf->content[buf->use] = 0;
Packit 423ecb
    else
Packit 423ecb
        return(-1);
Packit 423ecb
    return(0);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlBufErase:
Packit 423ecb
 * @buf:  the buffer
Packit 423ecb
 * @len:  the size to erase at the end
Packit 423ecb
 *
Packit 423ecb
 * Sometime data need to be erased at the end of the buffer
Packit 423ecb
 *
Packit 423ecb
 * Returns -1 in case of error and 0 otherwise
Packit 423ecb
 */
Packit 423ecb
int
Packit 423ecb
xmlBufErase(xmlBufPtr buf, size_t len) {
Packit 423ecb
    if ((buf == NULL) || (buf->error))
Packit 423ecb
        return(-1);
Packit 423ecb
    CHECK_COMPAT(buf)
Packit 423ecb
    if (len > buf->use)
Packit 423ecb
        return(-1);
Packit 423ecb
    buf->use -= len;
Packit 423ecb
    buf->content[buf->use] = 0;
Packit 423ecb
    UPDATE_COMPAT(buf)
Packit 423ecb
    return(0);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlBufLength:
Packit 423ecb
 * @buf:  the buffer
Packit 423ecb
 *
Packit 423ecb
 * Function to get the length of a buffer
Packit 423ecb
 *
Packit 423ecb
 * Returns the length of data in the internal content
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
size_t
Packit 423ecb
xmlBufLength(const xmlBufPtr buf)
Packit 423ecb
{
Packit 423ecb
    if ((!buf) || (buf->error))
Packit 423ecb
        return 0;
Packit 423ecb
    CHECK_COMPAT(buf)
Packit 423ecb
Packit 423ecb
    return(buf->use);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlBufUse:
Packit 423ecb
 * @buf:  the buffer
Packit 423ecb
 *
Packit 423ecb
 * Function to get the length of a buffer
Packit 423ecb
 *
Packit 423ecb
 * Returns the length of data in the internal content
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
size_t
Packit 423ecb
xmlBufUse(const xmlBufPtr buf)
Packit 423ecb
{
Packit 423ecb
    if ((!buf) || (buf->error))
Packit 423ecb
        return 0;
Packit 423ecb
    CHECK_COMPAT(buf)
Packit 423ecb
Packit 423ecb
    return(buf->use);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlBufAvail:
Packit 423ecb
 * @buf:  the buffer
Packit 423ecb
 *
Packit 423ecb
 * Function to find how much free space is allocated but not
Packit 423ecb
 * used in the buffer. It does not account for the terminating zero
Packit 423ecb
 * usually needed
Packit 423ecb
 *
Packit 423ecb
 * Returns the amount or 0 if none or an error occurred
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
size_t
Packit 423ecb
xmlBufAvail(const xmlBufPtr buf)
Packit 423ecb
{
Packit 423ecb
    if ((!buf) || (buf->error))
Packit 423ecb
        return 0;
Packit 423ecb
    CHECK_COMPAT(buf)
Packit 423ecb
Packit 423ecb
    return(buf->size - buf->use);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlBufIsEmpty:
Packit 423ecb
 * @buf:  the buffer
Packit 423ecb
 *
Packit 423ecb
 * Tell if a buffer is empty
Packit 423ecb
 *
Packit 423ecb
 * Returns 0 if no, 1 if yes and -1 in case of error
Packit 423ecb
 */
Packit 423ecb
int
Packit 423ecb
xmlBufIsEmpty(const xmlBufPtr buf)
Packit 423ecb
{
Packit 423ecb
    if ((!buf) || (buf->error))
Packit 423ecb
        return(-1);
Packit 423ecb
    CHECK_COMPAT(buf)
Packit 423ecb
Packit 423ecb
    return(buf->use == 0);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlBufResize:
Packit 423ecb
 * @buf:  the buffer to resize
Packit 423ecb
 * @size:  the desired size
Packit 423ecb
 *
Packit 423ecb
 * Resize a buffer to accommodate minimum size of @size.
Packit 423ecb
 *
Packit 423ecb
 * Returns  0 in case of problems, 1 otherwise
Packit 423ecb
 */
Packit 423ecb
int
Packit 423ecb
xmlBufResize(xmlBufPtr buf, size_t size)
Packit 423ecb
{
Packit 423ecb
    unsigned int newSize;
Packit 423ecb
    xmlChar* rebuf = NULL;
Packit 423ecb
    size_t start_buf;
Packit 423ecb
Packit 423ecb
    if ((buf == NULL) || (buf->error))
Packit 423ecb
        return(0);
Packit 423ecb
    CHECK_COMPAT(buf)
Packit 423ecb
Packit 423ecb
    if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0);
Packit 423ecb
    if (buf->alloc == XML_BUFFER_ALLOC_BOUNDED) {
Packit 423ecb
        /*
Packit 423ecb
	 * Used to provide parsing limits
Packit 423ecb
	 */
Packit 423ecb
        if (size >= XML_MAX_TEXT_LENGTH) {
Packit 423ecb
	    xmlBufMemoryError(buf, "buffer error: text too long\n");
Packit 423ecb
	    return(0);
Packit 423ecb
	}
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    /* Don't resize if we don't have to */
Packit 423ecb
    if (size < buf->size)
Packit 423ecb
        return 1;
Packit 423ecb
Packit 423ecb
    /* figure out new size */
Packit 423ecb
    switch (buf->alloc){
Packit 423ecb
	case XML_BUFFER_ALLOC_IO:
Packit 423ecb
	case XML_BUFFER_ALLOC_DOUBLEIT:
Packit 423ecb
	    /*take care of empty case*/
Packit 423ecb
	    newSize = (buf->size ? buf->size*2 : size + 10);
Packit 423ecb
	    while (size > newSize) {
Packit 423ecb
	        if (newSize > UINT_MAX / 2) {
Packit 423ecb
	            xmlBufMemoryError(buf, "growing buffer");
Packit 423ecb
	            return 0;
Packit 423ecb
	        }
Packit 423ecb
	        newSize *= 2;
Packit 423ecb
	    }
Packit 423ecb
	    break;
Packit 423ecb
	case XML_BUFFER_ALLOC_EXACT:
Packit 423ecb
	    newSize = size+10;
Packit 423ecb
	    break;
Packit 423ecb
        case XML_BUFFER_ALLOC_HYBRID:
Packit 423ecb
            if (buf->use < BASE_BUFFER_SIZE)
Packit 423ecb
                newSize = size;
Packit 423ecb
            else {
Packit 423ecb
                newSize = buf->size * 2;
Packit 423ecb
                while (size > newSize) {
Packit 423ecb
                    if (newSize > UINT_MAX / 2) {
Packit 423ecb
                        xmlBufMemoryError(buf, "growing buffer");
Packit 423ecb
                        return 0;
Packit 423ecb
                    }
Packit 423ecb
                    newSize *= 2;
Packit 423ecb
                }
Packit 423ecb
            }
Packit 423ecb
            break;
Packit 423ecb
Packit 423ecb
	default:
Packit 423ecb
	    newSize = size+10;
Packit 423ecb
	    break;
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
Packit 423ecb
        start_buf = buf->content - buf->contentIO;
Packit 423ecb
Packit 423ecb
        if (start_buf > newSize) {
Packit 423ecb
	    /* move data back to start */
Packit 423ecb
	    memmove(buf->contentIO, buf->content, buf->use);
Packit 423ecb
	    buf->content = buf->contentIO;
Packit 423ecb
	    buf->content[buf->use] = 0;
Packit 423ecb
	    buf->size += start_buf;
Packit 423ecb
	} else {
Packit 423ecb
	    rebuf = (xmlChar *) xmlRealloc(buf->contentIO, start_buf + newSize);
Packit 423ecb
	    if (rebuf == NULL) {
Packit 423ecb
		xmlBufMemoryError(buf, "growing buffer");
Packit 423ecb
		return 0;
Packit 423ecb
	    }
Packit 423ecb
	    buf->contentIO = rebuf;
Packit 423ecb
	    buf->content = rebuf + start_buf;
Packit 423ecb
	}
Packit 423ecb
    } else {
Packit 423ecb
	if (buf->content == NULL) {
Packit 423ecb
	    rebuf = (xmlChar *) xmlMallocAtomic(newSize);
Packit 423ecb
	} else if (buf->size - buf->use < 100) {
Packit 423ecb
	    rebuf = (xmlChar *) xmlRealloc(buf->content, newSize);
Packit 423ecb
        } else {
Packit 423ecb
	    /*
Packit 423ecb
	     * if we are reallocating a buffer far from being full, it's
Packit 423ecb
	     * better to make a new allocation and copy only the used range
Packit 423ecb
	     * and free the old one.
Packit 423ecb
	     */
Packit 423ecb
	    rebuf = (xmlChar *) xmlMallocAtomic(newSize);
Packit 423ecb
	    if (rebuf != NULL) {
Packit 423ecb
		memcpy(rebuf, buf->content, buf->use);
Packit 423ecb
		xmlFree(buf->content);
Packit 423ecb
		rebuf[buf->use] = 0;
Packit 423ecb
	    }
Packit 423ecb
	}
Packit 423ecb
	if (rebuf == NULL) {
Packit 423ecb
	    xmlBufMemoryError(buf, "growing buffer");
Packit 423ecb
	    return 0;
Packit 423ecb
	}
Packit 423ecb
	buf->content = rebuf;
Packit 423ecb
    }
Packit 423ecb
    buf->size = newSize;
Packit 423ecb
    UPDATE_COMPAT(buf)
Packit 423ecb
Packit 423ecb
    return 1;
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlBufAdd:
Packit 423ecb
 * @buf:  the buffer to dump
Packit 423ecb
 * @str:  the #xmlChar string
Packit 423ecb
 * @len:  the number of #xmlChar to add
Packit 423ecb
 *
Packit 423ecb
 * Add a string range to an XML buffer. if len == -1, the length of
Packit 423ecb
 * str is recomputed.
Packit 423ecb
 *
Packit 423ecb
 * Returns 0 successful, a positive error code number otherwise
Packit 423ecb
 *         and -1 in case of internal or API error.
Packit 423ecb
 */
Packit 423ecb
int
Packit 423ecb
xmlBufAdd(xmlBufPtr buf, const xmlChar *str, int len) {
Packit 423ecb
    unsigned int needSize;
Packit 423ecb
Packit 423ecb
    if ((str == NULL) || (buf == NULL) || (buf->error))
Packit 423ecb
	return -1;
Packit 423ecb
    CHECK_COMPAT(buf)
Packit 423ecb
Packit 423ecb
    if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
Packit 423ecb
    if (len < -1) {
Packit 423ecb
#ifdef DEBUG_BUFFER
Packit 423ecb
        xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
		"xmlBufAdd: len < 0\n");
Packit 423ecb
#endif
Packit 423ecb
	return -1;
Packit 423ecb
    }
Packit 423ecb
    if (len == 0) return 0;
Packit 423ecb
Packit 423ecb
    if (len < 0)
Packit 423ecb
        len = xmlStrlen(str);
Packit 423ecb
Packit 423ecb
    if (len < 0) return -1;
Packit 423ecb
    if (len == 0) return 0;
Packit 423ecb
Packit 423ecb
    needSize = buf->use + len + 2;
Packit 423ecb
    if (needSize > buf->size){
Packit 423ecb
	if (buf->alloc == XML_BUFFER_ALLOC_BOUNDED) {
Packit 423ecb
	    /*
Packit 423ecb
	     * Used to provide parsing limits
Packit 423ecb
	     */
Packit 423ecb
	    if (needSize >= XML_MAX_TEXT_LENGTH) {
Packit 423ecb
		xmlBufMemoryError(buf, "buffer error: text too long\n");
Packit 423ecb
		return(-1);
Packit 423ecb
	    }
Packit 423ecb
	}
Packit 423ecb
        if (!xmlBufResize(buf, needSize)){
Packit 423ecb
	    xmlBufMemoryError(buf, "growing buffer");
Packit 423ecb
            return XML_ERR_NO_MEMORY;
Packit 423ecb
        }
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    memmove(&buf->content[buf->use], str, len*sizeof(xmlChar));
Packit 423ecb
    buf->use += len;
Packit 423ecb
    buf->content[buf->use] = 0;
Packit 423ecb
    UPDATE_COMPAT(buf)
Packit 423ecb
    return 0;
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlBufAddHead:
Packit 423ecb
 * @buf:  the buffer
Packit 423ecb
 * @str:  the #xmlChar string
Packit 423ecb
 * @len:  the number of #xmlChar to add
Packit 423ecb
 *
Packit 423ecb
 * Add a string range to the beginning of an XML buffer.
Packit 423ecb
 * if len == -1, the length of @str is recomputed.
Packit 423ecb
 *
Packit 423ecb
 * Returns 0 successful, a positive error code number otherwise
Packit 423ecb
 *         and -1 in case of internal or API error.
Packit 423ecb
 */
Packit 423ecb
int
Packit 423ecb
xmlBufAddHead(xmlBufPtr buf, const xmlChar *str, int len) {
Packit 423ecb
    unsigned int needSize;
Packit 423ecb
Packit 423ecb
    if ((buf == NULL) || (buf->error))
Packit 423ecb
        return(-1);
Packit 423ecb
    CHECK_COMPAT(buf)
Packit 423ecb
    if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
Packit 423ecb
    if (str == NULL) {
Packit 423ecb
#ifdef DEBUG_BUFFER
Packit 423ecb
        xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
		"xmlBufAddHead: str == NULL\n");
Packit 423ecb
#endif
Packit 423ecb
	return -1;
Packit 423ecb
    }
Packit 423ecb
    if (len < -1) {
Packit 423ecb
#ifdef DEBUG_BUFFER
Packit 423ecb
        xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
		"xmlBufAddHead: len < 0\n");
Packit 423ecb
#endif
Packit 423ecb
	return -1;
Packit 423ecb
    }
Packit 423ecb
    if (len == 0) return 0;
Packit 423ecb
Packit 423ecb
    if (len < 0)
Packit 423ecb
        len = xmlStrlen(str);
Packit 423ecb
Packit 423ecb
    if (len <= 0) return -1;
Packit 423ecb
Packit 423ecb
    if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
Packit 423ecb
        size_t start_buf = buf->content - buf->contentIO;
Packit 423ecb
Packit 423ecb
	if (start_buf > (unsigned int) len) {
Packit 423ecb
	    /*
Packit 423ecb
	     * We can add it in the space previously shrinked
Packit 423ecb
	     */
Packit 423ecb
	    buf->content -= len;
Packit 423ecb
            memmove(&buf->content[0], str, len);
Packit 423ecb
	    buf->use += len;
Packit 423ecb
	    buf->size += len;
Packit 423ecb
	    UPDATE_COMPAT(buf)
Packit 423ecb
	    return(0);
Packit 423ecb
	}
Packit 423ecb
    }
Packit 423ecb
    needSize = buf->use + len + 2;
Packit 423ecb
    if (needSize > buf->size){
Packit 423ecb
	if (buf->alloc == XML_BUFFER_ALLOC_BOUNDED) {
Packit 423ecb
	    /*
Packit 423ecb
	     * Used to provide parsing limits
Packit 423ecb
	     */
Packit 423ecb
	    if (needSize >= XML_MAX_TEXT_LENGTH) {
Packit 423ecb
		xmlBufMemoryError(buf, "buffer error: text too long\n");
Packit 423ecb
		return(-1);
Packit 423ecb
	    }
Packit 423ecb
	}
Packit 423ecb
        if (!xmlBufResize(buf, needSize)){
Packit 423ecb
	    xmlBufMemoryError(buf, "growing buffer");
Packit 423ecb
            return XML_ERR_NO_MEMORY;
Packit 423ecb
        }
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    memmove(&buf->content[len], &buf->content[0], buf->use);
Packit 423ecb
    memmove(&buf->content[0], str, len);
Packit 423ecb
    buf->use += len;
Packit 423ecb
    buf->content[buf->use] = 0;
Packit 423ecb
    UPDATE_COMPAT(buf)
Packit 423ecb
    return 0;
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlBufCat:
Packit 423ecb
 * @buf:  the buffer to add to
Packit 423ecb
 * @str:  the #xmlChar string
Packit 423ecb
 *
Packit 423ecb
 * Append a zero terminated string to an XML buffer.
Packit 423ecb
 *
Packit 423ecb
 * Returns 0 successful, a positive error code number otherwise
Packit 423ecb
 *         and -1 in case of internal or API error.
Packit 423ecb
 */
Packit 423ecb
int
Packit 423ecb
xmlBufCat(xmlBufPtr buf, const xmlChar *str) {
Packit 423ecb
    if ((buf == NULL) || (buf->error))
Packit 423ecb
        return(-1);
Packit 423ecb
    CHECK_COMPAT(buf)
Packit 423ecb
    if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
Packit 423ecb
    if (str == NULL) return -1;
Packit 423ecb
    return xmlBufAdd(buf, str, -1);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlBufCCat:
Packit 423ecb
 * @buf:  the buffer to dump
Packit 423ecb
 * @str:  the C char string
Packit 423ecb
 *
Packit 423ecb
 * Append a zero terminated C string to an XML buffer.
Packit 423ecb
 *
Packit 423ecb
 * Returns 0 successful, a positive error code number otherwise
Packit 423ecb
 *         and -1 in case of internal or API error.
Packit 423ecb
 */
Packit 423ecb
int
Packit 423ecb
xmlBufCCat(xmlBufPtr buf, const char *str) {
Packit 423ecb
    const char *cur;
Packit 423ecb
Packit 423ecb
    if ((buf == NULL) || (buf->error))
Packit 423ecb
        return(-1);
Packit 423ecb
    CHECK_COMPAT(buf)
Packit 423ecb
    if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1;
Packit 423ecb
    if (str == NULL) {
Packit 423ecb
#ifdef DEBUG_BUFFER
Packit 423ecb
        xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
		"xmlBufCCat: str == NULL\n");
Packit 423ecb
#endif
Packit 423ecb
	return -1;
Packit 423ecb
    }
Packit 423ecb
    for (cur = str;*cur != 0;cur++) {
Packit 423ecb
        if (buf->use  + 10 >= buf->size) {
Packit 423ecb
            if (!xmlBufResize(buf, buf->use+10)){
Packit 423ecb
		xmlBufMemoryError(buf, "growing buffer");
Packit 423ecb
                return XML_ERR_NO_MEMORY;
Packit 423ecb
            }
Packit 423ecb
        }
Packit 423ecb
        buf->content[buf->use++] = *cur;
Packit 423ecb
    }
Packit 423ecb
    buf->content[buf->use] = 0;
Packit 423ecb
    UPDATE_COMPAT(buf)
Packit 423ecb
    return 0;
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlBufWriteCHAR:
Packit 423ecb
 * @buf:  the XML buffer
Packit 423ecb
 * @string:  the string to add
Packit 423ecb
 *
Packit 423ecb
 * routine which manages and grows an output buffer. This one adds
Packit 423ecb
 * xmlChars at the end of the buffer.
Packit 423ecb
 *
Packit 423ecb
 * Returns 0 if successful, a positive error code number otherwise
Packit 423ecb
 *         and -1 in case of internal or API error.
Packit 423ecb
 */
Packit 423ecb
int
Packit 423ecb
xmlBufWriteCHAR(xmlBufPtr buf, const xmlChar *string) {
Packit 423ecb
    if ((buf == NULL) || (buf->error))
Packit 423ecb
        return(-1);
Packit 423ecb
    CHECK_COMPAT(buf)
Packit 423ecb
    if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE)
Packit 423ecb
        return(-1);
Packit 423ecb
    return(xmlBufCat(buf, string));
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlBufWriteChar:
Packit 423ecb
 * @buf:  the XML buffer output
Packit 423ecb
 * @string:  the string to add
Packit 423ecb
 *
Packit 423ecb
 * routine which manage and grows an output buffer. This one add
Packit 423ecb
 * C chars at the end of the array.
Packit 423ecb
 *
Packit 423ecb
 * Returns 0 if successful, a positive error code number otherwise
Packit 423ecb
 *         and -1 in case of internal or API error.
Packit 423ecb
 */
Packit 423ecb
int
Packit 423ecb
xmlBufWriteChar(xmlBufPtr buf, const char *string) {
Packit 423ecb
    if ((buf == NULL) || (buf->error))
Packit 423ecb
        return(-1);
Packit 423ecb
    CHECK_COMPAT(buf)
Packit 423ecb
    if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE)
Packit 423ecb
        return(-1);
Packit 423ecb
    return(xmlBufCCat(buf, string));
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlBufWriteQuotedString:
Packit 423ecb
 * @buf:  the XML buffer output
Packit 423ecb
 * @string:  the string to add
Packit 423ecb
 *
Packit 423ecb
 * routine which manage and grows an output buffer. This one writes
Packit 423ecb
 * a quoted or double quoted #xmlChar string, checking first if it holds
Packit 423ecb
 * quote or double-quotes internally
Packit 423ecb
 *
Packit 423ecb
 * Returns 0 if successful, a positive error code number otherwise
Packit 423ecb
 *         and -1 in case of internal or API error.
Packit 423ecb
 */
Packit 423ecb
int
Packit 423ecb
xmlBufWriteQuotedString(xmlBufPtr buf, const xmlChar *string) {
Packit 423ecb
    const xmlChar *cur, *base;
Packit 423ecb
    if ((buf == NULL) || (buf->error))
Packit 423ecb
        return(-1);
Packit 423ecb
    CHECK_COMPAT(buf)
Packit 423ecb
    if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE)
Packit 423ecb
        return(-1);
Packit 423ecb
    if (xmlStrchr(string, '\"')) {
Packit 423ecb
        if (xmlStrchr(string, '\'')) {
Packit 423ecb
#ifdef DEBUG_BUFFER
Packit 423ecb
	    xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
 "xmlBufWriteQuotedString: string contains quote and double-quotes !\n");
Packit 423ecb
#endif
Packit 423ecb
	    xmlBufCCat(buf, "\"");
Packit 423ecb
            base = cur = string;
Packit 423ecb
            while(*cur != 0){
Packit 423ecb
                if(*cur == '"'){
Packit 423ecb
                    if (base != cur)
Packit 423ecb
                        xmlBufAdd(buf, base, cur - base);
Packit 423ecb
                    xmlBufAdd(buf, BAD_CAST """, 6);
Packit 423ecb
                    cur++;
Packit 423ecb
                    base = cur;
Packit 423ecb
                }
Packit 423ecb
                else {
Packit 423ecb
                    cur++;
Packit 423ecb
                }
Packit 423ecb
            }
Packit 423ecb
            if (base != cur)
Packit 423ecb
                xmlBufAdd(buf, base, cur - base);
Packit 423ecb
	    xmlBufCCat(buf, "\"");
Packit 423ecb
	}
Packit 423ecb
        else{
Packit 423ecb
	    xmlBufCCat(buf, "\'");
Packit 423ecb
            xmlBufCat(buf, string);
Packit 423ecb
	    xmlBufCCat(buf, "\'");
Packit 423ecb
        }
Packit 423ecb
    } else {
Packit 423ecb
        xmlBufCCat(buf, "\"");
Packit 423ecb
        xmlBufCat(buf, string);
Packit 423ecb
        xmlBufCCat(buf, "\"");
Packit 423ecb
    }
Packit 423ecb
    return(0);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlBufFromBuffer:
Packit 423ecb
 * @buffer: incoming old buffer to convert to a new one
Packit 423ecb
 *
Packit 423ecb
 * Helper routine to switch from the old buffer structures in use
Packit 423ecb
 * in various APIs. It creates a wrapper xmlBufPtr which will be
Packit 423ecb
 * used for internal processing until the xmlBufBackToBuffer() is
Packit 423ecb
 * issued.
Packit 423ecb
 *
Packit 423ecb
 * Returns a new xmlBufPtr unless the call failed and NULL is returned
Packit 423ecb
 */
Packit 423ecb
xmlBufPtr
Packit 423ecb
xmlBufFromBuffer(xmlBufferPtr buffer) {
Packit 423ecb
    xmlBufPtr ret;
Packit 423ecb
Packit 423ecb
    if (buffer == NULL)
Packit 423ecb
        return(NULL);
Packit 423ecb
Packit 423ecb
    ret = (xmlBufPtr) xmlMalloc(sizeof(xmlBuf));
Packit 423ecb
    if (ret == NULL) {
Packit 423ecb
	xmlBufMemoryError(NULL, "creating buffer");
Packit 423ecb
        return(NULL);
Packit 423ecb
    }
Packit 423ecb
    ret->use = buffer->use;
Packit 423ecb
    ret->size = buffer->size;
Packit 423ecb
    ret->compat_use = buffer->use;
Packit 423ecb
    ret->compat_size = buffer->size;
Packit 423ecb
    ret->error = 0;
Packit 423ecb
    ret->buffer = buffer;
Packit 423ecb
    ret->alloc = buffer->alloc;
Packit 423ecb
    ret->content = buffer->content;
Packit 423ecb
    ret->contentIO = buffer->contentIO;
Packit 423ecb
Packit 423ecb
    return(ret);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlBufBackToBuffer:
Packit 423ecb
 * @buf: new buffer wrapping the old one
Packit 423ecb
 *
Packit 423ecb
 * Function to be called once internal processing had been done to
Packit 423ecb
 * update back the buffer provided by the user. This can lead to
Packit 423ecb
 * a failure in case the size accumulated in the xmlBuf is larger
Packit 423ecb
 * than what an xmlBuffer can support on 64 bits (INT_MAX)
Packit 423ecb
 * The xmlBufPtr @buf wrapper is deallocated by this call in any case.
Packit 423ecb
 *
Packit 423ecb
 * Returns the old xmlBufferPtr unless the call failed and NULL is returned
Packit 423ecb
 */
Packit 423ecb
xmlBufferPtr
Packit 423ecb
xmlBufBackToBuffer(xmlBufPtr buf) {
Packit 423ecb
    xmlBufferPtr ret;
Packit 423ecb
Packit 423ecb
    if ((buf == NULL) || (buf->error))
Packit 423ecb
        return(NULL);
Packit 423ecb
    CHECK_COMPAT(buf)
Packit 423ecb
    if (buf->buffer == NULL) {
Packit 423ecb
        xmlBufFree(buf);
Packit 423ecb
        return(NULL);
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    ret = buf->buffer;
Packit 423ecb
    /*
Packit 423ecb
     * What to do in case of error in the buffer ???
Packit 423ecb
     */
Packit 423ecb
    if (buf->use > INT_MAX) {
Packit 423ecb
        /*
Packit 423ecb
         * Worse case, we really allocated and used more than the
Packit 423ecb
         * maximum allowed memory for an xmlBuffer on this architecture.
Packit 423ecb
         * Keep the buffer but provide a truncated size value.
Packit 423ecb
         */
Packit 423ecb
        xmlBufOverflowError(buf, "Used size too big for xmlBuffer");
Packit 423ecb
        ret->use = INT_MAX;
Packit 423ecb
        ret->size = INT_MAX;
Packit 423ecb
    } else if (buf->size > INT_MAX) {
Packit 423ecb
        /*
Packit 423ecb
         * milder case, we allocated more than the maximum allowed memory
Packit 423ecb
         * for an xmlBuffer on this architecture, but used less than the
Packit 423ecb
         * limit.
Packit 423ecb
         * Keep the buffer but provide a truncated size value.
Packit 423ecb
         */
Packit 423ecb
        xmlBufOverflowError(buf, "Allocated size too big for xmlBuffer");
Packit 423ecb
        ret->size = INT_MAX;
Packit 423ecb
    }
Packit 423ecb
    ret->use = (int) buf->use;
Packit 423ecb
    ret->size = (int) buf->size;
Packit 423ecb
    ret->alloc = buf->alloc;
Packit 423ecb
    ret->content = buf->content;
Packit 423ecb
    ret->contentIO = buf->contentIO;
Packit 423ecb
    xmlFree(buf);
Packit 423ecb
    return(ret);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlBufMergeBuffer:
Packit 423ecb
 * @buf: an xmlBufPtr
Packit 423ecb
 * @buffer: the buffer to consume into @buf
Packit 423ecb
 *
Packit 423ecb
 * The content of @buffer is appended to @buf and @buffer is freed
Packit 423ecb
 *
Packit 423ecb
 * Returns -1 in case of error, 0 otherwise, in any case @buffer is freed
Packit 423ecb
 */
Packit 423ecb
int
Packit 423ecb
xmlBufMergeBuffer(xmlBufPtr buf, xmlBufferPtr buffer) {
Packit 423ecb
    int ret = 0;
Packit 423ecb
Packit 423ecb
    if ((buf == NULL) || (buf->error)) {
Packit 423ecb
	xmlBufferFree(buffer);
Packit 423ecb
        return(-1);
Packit 423ecb
    }
Packit 423ecb
    CHECK_COMPAT(buf)
Packit 423ecb
    if ((buffer != NULL) && (buffer->content != NULL) &&
Packit 423ecb
             (buffer->use > 0)) {
Packit 423ecb
        ret = xmlBufAdd(buf, buffer->content, buffer->use);
Packit 423ecb
    }
Packit 423ecb
    xmlBufferFree(buffer);
Packit 423ecb
    return(ret);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlBufResetInput:
Packit 423ecb
 * @buf: an xmlBufPtr
Packit 423ecb
 * @input: an xmlParserInputPtr
Packit 423ecb
 *
Packit 423ecb
 * Update the input to use the current set of pointers from the buffer.
Packit 423ecb
 *
Packit 423ecb
 * Returns -1 in case of error, 0 otherwise
Packit 423ecb
 */
Packit 423ecb
int
Packit 423ecb
xmlBufResetInput(xmlBufPtr buf, xmlParserInputPtr input) {
Packit 423ecb
    if ((input == NULL) || (buf == NULL) || (buf->error))
Packit 423ecb
        return(-1);
Packit 423ecb
    CHECK_COMPAT(buf)
Packit 423ecb
    input->base = input->cur = buf->content;
Packit 423ecb
    input->end = &buf->content[buf->use];
Packit 423ecb
    return(0);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlBufGetInputBase:
Packit 423ecb
 * @buf: an xmlBufPtr
Packit 423ecb
 * @input: an xmlParserInputPtr
Packit 423ecb
 *
Packit 423ecb
 * Get the base of the @input relative to the beginning of the buffer
Packit 423ecb
 *
Packit 423ecb
 * Returns the size_t corresponding to the displacement
Packit 423ecb
 */
Packit 423ecb
size_t
Packit 423ecb
xmlBufGetInputBase(xmlBufPtr buf, xmlParserInputPtr input) {
Packit 423ecb
    size_t base;
Packit 423ecb
Packit 423ecb
    if ((input == NULL) || (buf == NULL) || (buf->error))
Packit 423ecb
        return(-1);
Packit 423ecb
    CHECK_COMPAT(buf)
Packit 423ecb
    base = input->base - buf->content;
Packit 423ecb
    /*
Packit 423ecb
     * We could do some pointer arythmetic checks but that's probably
Packit 423ecb
     * sufficient.
Packit 423ecb
     */
Packit 423ecb
    if (base > buf->size) {
Packit 423ecb
        xmlBufOverflowError(buf, "Input reference outside of the buffer");
Packit 423ecb
        base = 0;
Packit 423ecb
    }
Packit 423ecb
    return(base);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlBufSetInputBaseCur:
Packit 423ecb
 * @buf: an xmlBufPtr
Packit 423ecb
 * @input: an xmlParserInputPtr
Packit 423ecb
 * @base: the base value relative to the beginning of the buffer
Packit 423ecb
 * @cur: the cur value relative to the beginning of the buffer
Packit 423ecb
 *
Packit 423ecb
 * Update the input to use the base and cur relative to the buffer
Packit 423ecb
 * after a possible reallocation of its content
Packit 423ecb
 *
Packit 423ecb
 * Returns -1 in case of error, 0 otherwise
Packit 423ecb
 */
Packit 423ecb
int
Packit 423ecb
xmlBufSetInputBaseCur(xmlBufPtr buf, xmlParserInputPtr input,
Packit 423ecb
                      size_t base, size_t cur) {
Packit 423ecb
    if ((input == NULL) || (buf == NULL) || (buf->error))
Packit 423ecb
        return(-1);
Packit 423ecb
    CHECK_COMPAT(buf)
Packit 423ecb
    input->base = &buf->content[base];
Packit 423ecb
    input->cur = input->base + cur;
Packit 423ecb
    input->end = &buf->content[buf->use];
Packit 423ecb
    return(0);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
#define bottom_buf
Packit 423ecb
#include "elfgcchack.h"