Blame gettext-tools/gnulib-lib/libxml/buf.c

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