Blame buf.c

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