summaryrefslogtreecommitdiff
path: root/buf.c
diff options
context:
space:
mode:
authorDaniel Veillard <veillard@redhat.com>2012-08-06 10:16:41 +0800
committerDaniel Veillard <veillard@redhat.com>2012-08-06 10:16:41 +0800
commit18e1f1f1180c4d48ed52bf995b3c700c2cefb492 (patch)
tree7e1485d3fd374d6c626b5471cce69c27da0960f6 /buf.c
parent3f0c613f28396c2b1acf90ad08382f0c9620d035 (diff)
downloadlibxml2-18e1f1f1180c4d48ed52bf995b3c700c2cefb492.tar.gz
Improvements for old buffer compatibility
Now tree.h exports LIBXML2_NEW_BUFFER macro indicating that the API uses the new buffers, important to keep code working with both versions. * tree.h buf.h: also export xmlBufContent(), xmlBufEnd(), and xmlBufUse() to help port the old code * buf.c: make sure the compatibility counters are updated on buffer usage, to keep proper working of application compiled against the old structures, but take care of int overflow
Diffstat (limited to 'buf.c')
-rw-r--r--buf.c23
1 files changed, 20 insertions, 3 deletions
diff --git a/buf.c b/buf.c
index 7e293e6c..530ffc7b 100644
--- a/buf.c
+++ b/buf.c
@@ -49,6 +49,12 @@ struct _xmlBuf {
int error; /* an error code if a failure occured */
};
+#define UPDATE_COMPAT(buf) \
+ if (buf->size < INT_MAX) buf->compat_size = buf->size; \
+ else buf->compat_size = INT_MAX; \
+ if (buf->use < INT_MAX) buf->compat_use = buf->use; \
+ else buf->compat_use = INT_MAX;
+
/**
* xmlBufMemoryError:
* @extra: extra informations
@@ -130,7 +136,7 @@ xmlBufCreateSize(size_t size) {
return(NULL);
}
ret->compat_use = 0;
- ret->compat_size = 0;
+ ret->compat_size = (int) size;
ret->use = 0;
ret->error = 0;
ret->buffer = NULL;
@@ -177,6 +183,8 @@ xmlBufDetach(xmlBufPtr buf) {
buf->content = NULL;
buf->size = 0;
buf->use = 0;
+ buf->compat_use = 0;
+ buf->compat_size = 0;
return ret;
}
@@ -209,8 +217,8 @@ xmlBufCreateStatic(void *mem, size_t size) {
ret->compat_use = size;
ret->compat_size = size;
} else {
- ret->compat_use = 0;
- ret->compat_size = 0;
+ ret->compat_use = INT_MAX;
+ ret->compat_size = INT_MAX;
}
ret->use = size;
ret->size = size;
@@ -329,6 +337,7 @@ xmlBufEmpty(xmlBufPtr buf) {
} else {
buf->content[0] = 0;
}
+ UPDATE_COMPAT(buf)
}
/**
@@ -376,6 +385,7 @@ xmlBufShrink(xmlBufPtr buf, size_t len) {
memmove(buf->content, &buf->content[len], buf->use);
buf->content[buf->use] = 0;
}
+ UPDATE_COMPAT(buf)
return(len);
}
@@ -435,6 +445,7 @@ xmlBufGrowInternal(xmlBufPtr buf, size_t len) {
buf->content = newbuf;
}
buf->size = size;
+ UPDATE_COMPAT(buf)
return(buf->size - buf->use);
}
@@ -563,6 +574,7 @@ xmlBufAddLen(xmlBufPtr buf, size_t len) {
if ((buf == NULL) || (buf->error) || (len > (buf->size - buf->use)))
return(-1);
buf->use += len;
+ UPDATE_COMPAT(buf)
if (buf->size > buf->use)
buf->content[buf->use] = 0;
else
@@ -585,6 +597,7 @@ xmlBufErase(xmlBufPtr buf, size_t len) {
return(-1);
buf->use -= len;
buf->content[buf->use] = 0;
+ UPDATE_COMPAT(buf)
return(0);
}
@@ -766,6 +779,7 @@ xmlBufResize(xmlBufPtr buf, size_t size)
buf->content = rebuf;
}
buf->size = newSize;
+ UPDATE_COMPAT(buf)
return 1;
}
@@ -816,6 +830,7 @@ xmlBufAdd(xmlBufPtr buf, const xmlChar *str, int len) {
memmove(&buf->content[buf->use], str, len*sizeof(xmlChar));
buf->use += len;
buf->content[buf->use] = 0;
+ UPDATE_COMPAT(buf)
return 0;
}
@@ -885,6 +900,7 @@ xmlBufAddHead(xmlBufPtr buf, const xmlChar *str, int len) {
memmove(&buf->content[0], str, len);
buf->use += len;
buf->content[buf->use] = 0;
+ UPDATE_COMPAT(buf)
return 0;
}
@@ -941,6 +957,7 @@ xmlBufCCat(xmlBufPtr buf, const char *str) {
buf->content[buf->use++] = *cur;
}
buf->content[buf->use] = 0;
+ UPDATE_COMPAT(buf)
return 0;
}