#define IN_LIBXML
#include "libxml.h"
#include <string.h>
#include <limits.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include "private/buf.h"
#ifndef SIZE_MAX
#define SIZE_MAX ((size_t) -1)
#endif
#define BUF_FLAG_OOM (1u << 0)
#define BUF_FLAG_OVERFLOW (1u << 1)
#define BUF_FLAG_STATIC (1u << 2)
#define BUF_ERROR(buf) ((buf)->flags & (BUF_FLAG_OOM | BUF_FLAG_OVERFLOW))
#define BUF_STATIC(buf) ((buf)->flags & BUF_FLAG_STATIC)
struct _xmlBuf {
xmlChar *content;
xmlChar *mem;
size_t use;
size_t size;
size_t maxSize;
unsigned flags;
};
static void
xmlBufMemoryError(xmlBufPtr buf)
{
if (!BUF_ERROR(buf))
buf->flags |= BUF_FLAG_OOM;
}
static void
xmlBufOverflowError(xmlBufPtr buf)
{
if (!BUF_ERROR(buf))
buf->flags |= BUF_FLAG_OVERFLOW;
}
xmlBuf *
xmlBufCreate(size_t size) {
xmlBufPtr ret;
if (size == SIZE_MAX)
return(NULL);
ret = xmlMalloc(sizeof(*ret));
if (ret == NULL)
return(NULL);
ret->use = 0;
ret->flags = 0;
ret->size = size;
ret->maxSize = SIZE_MAX - 1;
ret->mem = xmlMalloc(ret->size + 1);
if (ret->mem == NULL) {
xmlFree(ret);
return(NULL);
}
ret->content = ret->mem;
ret->content[0] = 0;
return(ret);
}
xmlBuf *
xmlBufCreateMem(const xmlChar *mem, size_t size, int isStatic) {
xmlBufPtr ret;
if (mem == NULL)
return(NULL);
if (size == SIZE_MAX)
return(NULL);
ret = xmlMalloc(sizeof(*ret));
if (ret == NULL)
return(NULL);
if (isStatic) {
if (mem[size] != 0) {
xmlFree(ret);
return(NULL);
}
ret->flags = BUF_FLAG_STATIC;
ret->mem = (xmlChar *) mem;
} else {
ret->flags = 0;
ret->mem = xmlMalloc(size + 1);
if (ret->mem == NULL) {
xmlFree(ret);
return(NULL);
}
memcpy(ret->mem, mem, size);
ret->mem[size] = 0;
}
ret->use = size;
ret->size = size;
ret->maxSize = SIZE_MAX - 1;
ret->content = ret->mem;
return(ret);
}
xmlChar *
xmlBufDetach(xmlBuf *buf) {
xmlChar *ret;
if ((buf == NULL) || (BUF_ERROR(buf)) || (BUF_STATIC(buf)))
return(NULL);
if (buf->content != buf->mem) {
ret = xmlStrndup(buf->content, buf->use);
xmlFree(buf->mem);
} else {
ret = buf->mem;
}
buf->content = NULL;
buf->mem = NULL;
buf->size = 0;
buf->use = 0;
return ret;
}
void
xmlBufFree(xmlBuf *buf) {
if (buf == NULL)
return;
if (!BUF_STATIC(buf))
xmlFree(buf->mem);
xmlFree(buf);
}
void
xmlBufEmpty(xmlBuf *buf) {
if ((buf == NULL) || (BUF_ERROR(buf)) || (BUF_STATIC(buf)))
return;
if (buf->mem == NULL)
return;
buf->use = 0;
buf->size += buf->content - buf->mem;
buf->content = buf->mem;
buf->content[0] = 0;
}
size_t
xmlBufShrink(xmlBuf *buf, size_t len) {
if ((buf == NULL) || (BUF_ERROR(buf)))
return(0);
if (len == 0)
return(0);
if (len > buf->use)
return(0);
buf->use -= len;
buf->content += len;
buf->size -= len;
return(len);
}
static int
xmlBufGrowInternal(xmlBufPtr buf, size_t len) {
size_t size;
size_t start;
xmlChar *newbuf;
start = buf->content - buf->mem;
if (len <= start + buf->size - buf->use) {
memmove(buf->mem, buf->content, buf->use + 1);
buf->size += start;
buf->content = buf->mem;
return(0);
}
if (len > buf->maxSize - buf->use) {
xmlBufOverflowError(buf);
return(-1);
}
if (buf->size > (size_t) len) {
if (buf->size <= buf->maxSize / 2)
size = buf->size * 2;
else
size = buf->maxSize;
} else {
size = buf->use + len;
if (size <= buf->maxSize - 100)
size += 100;
}
if (buf->content == buf->mem) {
newbuf = xmlRealloc(buf->mem, size + 1);
if (newbuf == NULL) {
xmlBufMemoryError(buf);
return(-1);
}
} else {
newbuf = xmlMalloc(size + 1);
if (newbuf == NULL) {
xmlBufMemoryError(buf);
return(-1);
}
if (buf->content != NULL)
memcpy(newbuf, buf->content, buf->use + 1);
xmlFree(buf->mem);
}
buf->mem = newbuf;
buf->content = newbuf;
buf->size = size;
return(0);
}
int
xmlBufGrow(xmlBuf *buf, size_t len) {
if ((buf == NULL) || (BUF_ERROR(buf)) || (BUF_STATIC(buf)))
return(-1);
if (len <= buf->size - buf->use)
return(0);
if (xmlBufGrowInternal(buf, len) < 0)
return(-1);
return(0);
}
xmlChar *
xmlBufContent(const xmlBuf *buf)
{
if ((!buf) || (BUF_ERROR(buf)))
return NULL;
return(buf->content);
}
xmlChar *
xmlBufEnd(xmlBuf *buf)
{
if ((!buf) || (BUF_ERROR(buf)))
return NULL;
return(&buf->content[buf->use]);
}
int
xmlBufAddLen(xmlBuf *buf, size_t len) {
if ((buf == NULL) || (BUF_ERROR(buf)) || (BUF_STATIC(buf)))
return(-1);
if (len > buf->size - buf->use)
return(-1);
buf->use += len;
buf->content[buf->use] = 0;
return(0);
}
size_t
xmlBufUse(xmlBuf *buf)
{
if ((!buf) || (BUF_ERROR(buf)))
return 0;
return(buf->use);
}
size_t
xmlBufAvail(xmlBuf *buf)
{
if ((!buf) || (BUF_ERROR(buf)))
return 0;
return(buf->size - buf->use);
}
int
xmlBufIsEmpty(xmlBuf *buf)
{
if ((!buf) || (BUF_ERROR(buf)))
return(-1);
return(buf->use == 0);
}
int
xmlBufAdd(xmlBuf *buf, const xmlChar *str, size_t len) {
if ((buf == NULL) || (BUF_ERROR(buf)) || (BUF_STATIC(buf)))
return(-1);
if (len == 0)
return(0);
if (str == NULL)
return(-1);
if (len > buf->size - buf->use) {
if (xmlBufGrowInternal(buf, len) < 0)
return(-1);
}
memmove(&buf->content[buf->use], str, len);
buf->use += len;
buf->content[buf->use] = 0;
return(0);
}
int
xmlBufCat(xmlBuf *buf, const xmlChar *str) {
if (str == NULL)
return(0);
return(xmlBufAdd(buf, str, strlen((const char *) str)));
}
xmlBuf *
xmlBufFromBuffer(xmlBuffer *buffer) {
xmlBufPtr ret;
if (buffer == NULL)
return(NULL);
ret = xmlMalloc(sizeof(xmlBuf));
if (ret == NULL)
return(NULL);
ret->use = buffer->use;
ret->flags = 0;
ret->maxSize = SIZE_MAX - 1;
if (buffer->content == NULL) {
ret->size = 50;
ret->mem = xmlMalloc(ret->size + 1);
ret->content = ret->mem;
if (ret->mem == NULL)
xmlBufMemoryError(ret);
else
ret->content[0] = 0;
} else {
ret->size = buffer->size - 1;
ret->content = buffer->content;
if (buffer->alloc == XML_BUFFER_ALLOC_IO)
ret->mem = buffer->contentIO;
else
ret->mem = buffer->content;
}
return(ret);
}
int
xmlBufBackToBuffer(xmlBuf *buf, xmlBuffer *ret) {
if ((buf == NULL) || (ret == NULL))
return(-1);
if ((BUF_ERROR(buf)) || (BUF_STATIC(buf)) ||
(buf->use >= INT_MAX)) {
xmlBufFree(buf);
ret->content = NULL;
ret->contentIO = NULL;
ret->use = 0;
ret->size = 0;
return(-1);
}
ret->use = buf->use;
if (buf->size >= INT_MAX) {
ret->size = INT_MAX;
} else {
ret->size = buf->size + 1;
}
ret->alloc = XML_BUFFER_ALLOC_IO;
ret->content = buf->content;
ret->contentIO = buf->mem;
xmlFree(buf);
return(0);
}
int
xmlBufResetInput(xmlBuf *buf, xmlParserInput *input) {
return(xmlBufUpdateInput(buf, input, 0));
}
int
xmlBufUpdateInput(xmlBuf *buf, xmlParserInput *input, size_t pos) {
if ((buf == NULL) || (input == NULL))
return(-1);
input->base = buf->content;
input->cur = input->base + pos;
input->end = &buf->content[buf->use];
return(0);
}
void
xmlSetBufferAllocationScheme(xmlBufferAllocationScheme scheme ATTRIBUTE_UNUSED) {
}
xmlBufferAllocationScheme
xmlGetBufferAllocationScheme(void) {
return(XML_BUFFER_ALLOC_EXACT);
}
xmlBuffer *
xmlBufferCreate(void) {
xmlBufferPtr ret;
ret = xmlMalloc(sizeof(*ret));
if (ret == NULL)
return(NULL);
ret->use = 0;
ret->size = 256;
ret->alloc = XML_BUFFER_ALLOC_IO;
ret->contentIO = xmlMalloc(ret->size);
if (ret->contentIO == NULL) {
xmlFree(ret);
return(NULL);
}
ret->content = ret->contentIO;
ret->content[0] = 0;
return(ret);
}
xmlBuffer *
xmlBufferCreateSize(size_t size) {
xmlBufferPtr ret;
if (size >= INT_MAX)
return(NULL);
ret = xmlMalloc(sizeof(*ret));
if (ret == NULL)
return(NULL);
ret->use = 0;
ret->alloc = XML_BUFFER_ALLOC_IO;
ret->size = (size ? size + 1 : 0);
if (ret->size) {
ret->contentIO = xmlMalloc(ret->size);
if (ret->contentIO == NULL) {
xmlFree(ret);
return(NULL);
}
ret->content = ret->contentIO;
ret->content[0] = 0;
} else {
ret->contentIO = NULL;
ret->content = NULL;
}
return(ret);
}
xmlChar *
xmlBufferDetach(xmlBuffer *buf) {
xmlChar *ret;
if (buf == NULL)
return(NULL);
if ((buf->alloc == XML_BUFFER_ALLOC_IO) &&
(buf->content != buf->contentIO)) {
ret = xmlStrndup(buf->content, buf->use);
xmlFree(buf->contentIO);
} else {
ret = buf->content;
}
buf->contentIO = NULL;
buf->content = NULL;
buf->size = 0;
buf->use = 0;
return ret;
}
xmlBuffer *
xmlBufferCreateStatic(void *mem, size_t size) {
xmlBufferPtr buf = xmlBufferCreateSize(size);
if (buf == NULL)
return(NULL);
if (xmlBufferAdd(buf, mem, size) != XML_ERR_OK) {
xmlBufferFree(buf);
return(NULL);
}
return(buf);
}
void
xmlBufferSetAllocationScheme(xmlBuffer *buf ATTRIBUTE_UNUSED,
xmlBufferAllocationScheme scheme ATTRIBUTE_UNUSED) {
}
void
xmlBufferFree(xmlBuffer *buf) {
if (buf == NULL)
return;
if (buf->alloc == XML_BUFFER_ALLOC_IO)
xmlFree(buf->contentIO);
else
xmlFree(buf->content);
xmlFree(buf);
}
void
xmlBufferEmpty(xmlBuffer *buf) {
if (buf == NULL)
return;
if (buf->content == NULL)
return;
buf->use = 0;
if (buf->alloc == XML_BUFFER_ALLOC_IO) {
buf->size += buf->content - buf->contentIO;
buf->content = buf->contentIO;
buf->content[0] = 0;
} else {
buf->content[0] = 0;
}
}
int
xmlBufferShrink(xmlBuffer *buf, unsigned int len) {
if (buf == NULL)
return(-1);
if (len == 0)
return(0);
if (len > buf->use)
return(-1);
buf->use -= len;
if (buf->alloc == XML_BUFFER_ALLOC_IO) {
buf->content += len;
buf->size -= len;
} else {
memmove(buf->content, &buf->content[len], buf->use + 1);
}
return(len);
}
int
xmlBufferGrow(xmlBuffer *buf, unsigned int len) {
unsigned int size;
xmlChar *newbuf;
if (buf == NULL)
return(-1);
if (len < buf->size - buf->use)
return(0);
if (len >= INT_MAX - buf->use)
return(-1);
if (buf->size > (size_t) len) {
if (buf->size <= INT_MAX / 2)
size = buf->size * 2;
else
size = INT_MAX;
} else {
size = buf->use + len + 1;
if (size <= INT_MAX - 100)
size += 100;
}
if ((buf->alloc == XML_BUFFER_ALLOC_IO) &&
(buf->content != buf->contentIO)) {
newbuf = xmlMalloc(size);
if (newbuf == NULL)
return(-1);
if (buf->content != NULL)
memcpy(newbuf, buf->content, buf->use + 1);
xmlFree(buf->contentIO);
} else {
newbuf = xmlRealloc(buf->content, size);
if (newbuf == NULL)
return(-1);
}
if (buf->alloc == XML_BUFFER_ALLOC_IO)
buf->contentIO = newbuf;
buf->content = newbuf;
buf->size = size;
return(buf->size - buf->use - 1);
}
int
xmlBufferDump(FILE *file, xmlBuffer *buf) {
size_t ret;
if (buf == NULL)
return(0);
if (buf->content == NULL)
return(0);
if (file == NULL)
file = stdout;
ret = fwrite(buf->content, 1, buf->use, file);
return(ret > INT_MAX ? INT_MAX : ret);
}
const xmlChar *
xmlBufferContent(const xmlBuffer *buf)
{
if(!buf)
return NULL;
return buf->content;
}
int
xmlBufferLength(const xmlBuffer *buf)
{
if(!buf)
return 0;
return buf->use;
}
int
xmlBufferResize(xmlBuffer *buf, unsigned int size)
{
int res;
if (buf == NULL)
return(0);
if (size < buf->size)
return(1);
res = xmlBufferGrow(buf, size - buf->use);
return(res < 0 ? 0 : 1);
}
int
xmlBufferAdd(xmlBuffer *buf, const xmlChar *str, int len) {
if ((buf == NULL) || (str == NULL))
return(XML_ERR_ARGUMENT);
if (len < 0)
len = xmlStrlen(str);
if (len == 0)
return(XML_ERR_OK);
if ((unsigned) len >= buf->size - buf->use) {
if (xmlBufferGrow(buf, len) < 0)
return(XML_ERR_NO_MEMORY);
}
memmove(&buf->content[buf->use], str, len);
buf->use += len;
buf->content[buf->use] = 0;
return(XML_ERR_OK);
}
int
xmlBufferAddHead(xmlBuffer *buf, const xmlChar *str, int len) {
unsigned start = 0;
if ((buf == NULL) || (str == NULL))
return(XML_ERR_ARGUMENT);
if (len < 0)
len = xmlStrlen(str);
if (len == 0)
return(XML_ERR_OK);
if (buf->alloc == XML_BUFFER_ALLOC_IO) {
start = buf->content - buf->contentIO;
if ((unsigned) len <= start) {
buf->content -= len;
memmove(&buf->content[0], str, len);
buf->use += len;
buf->size += len;
return(0);
}
if ((unsigned) len < buf->size + start - buf->use) {
memmove(&buf->contentIO[len], buf->content, buf->use + 1);
memmove(buf->contentIO, str, len);
buf->content = buf->contentIO;
buf->use += len;
buf->size += start;
return(0);
}
}
if ((unsigned) len >= buf->size - buf->use) {
if (xmlBufferGrow(buf, len) < 0)
return(-1);
}
memmove(&buf->content[len], buf->content, buf->use + 1);
memmove(buf->content, str, len);
buf->use += len;
return (0);
}
int
xmlBufferCat(xmlBuffer *buf, const xmlChar *str) {
return(xmlBufferAdd(buf, str, -1));
}
int
xmlBufferCCat(xmlBuffer *buf, const char *str) {
return(xmlBufferAdd(buf, (const xmlChar *) str, -1));
}
void
xmlBufferWriteCHAR(xmlBuffer *buf, const xmlChar *string) {
xmlBufferAdd(buf, string, -1);
}
void
xmlBufferWriteChar(xmlBuffer *buf, const char *string) {
xmlBufferAdd(buf, (const xmlChar *) string, -1);
}
void
xmlBufferWriteQuotedString(xmlBuffer *buf, const xmlChar *string) {
const xmlChar *cur, *base;
if (buf == NULL)
return;
if (xmlStrchr(string, '\"')) {
if (xmlStrchr(string, '\'')) {
xmlBufferCCat(buf, "\"");
base = cur = string;
while(*cur != 0){
if(*cur == '"'){
if (base != cur)
xmlBufferAdd(buf, base, cur - base);
xmlBufferAdd(buf, BAD_CAST """, 6);
cur++;
base = cur;
}
else {
cur++;
}
}
if (base != cur)
xmlBufferAdd(buf, base, cur - base);
xmlBufferCCat(buf, "\"");
}
else{
xmlBufferCCat(buf, "\'");
xmlBufferCat(buf, string);
xmlBufferCCat(buf, "\'");
}
} else {
xmlBufferCCat(buf, "\"");
xmlBufferCat(buf, string);
xmlBufferCCat(buf, "\"");
}
}