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
diff --git a/buf.c b/buf.c
index 7e293e6..530ffc7 100644
--- a/buf.c
+++ b/buf.c
@@ -49,6 +49,12 @@
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 @@
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 @@
buf->content = NULL;
buf->size = 0;
buf->use = 0;
+ buf->compat_use = 0;
+ buf->compat_size = 0;
return ret;
}
@@ -209,8 +217,8 @@
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 @@
} else {
buf->content[0] = 0;
}
+ UPDATE_COMPAT(buf)
}
/**
@@ -376,6 +385,7 @@
memmove(buf->content, &buf->content[len], buf->use);
buf->content[buf->use] = 0;
}
+ UPDATE_COMPAT(buf)
return(len);
}
@@ -435,6 +445,7 @@
buf->content = newbuf;
}
buf->size = size;
+ UPDATE_COMPAT(buf)
return(buf->size - buf->use);
}
@@ -563,6 +574,7 @@
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 @@
return(-1);
buf->use -= len;
buf->content[buf->use] = 0;
+ UPDATE_COMPAT(buf)
return(0);
}
@@ -766,6 +779,7 @@
buf->content = rebuf;
}
buf->size = newSize;
+ UPDATE_COMPAT(buf)
return 1;
}
@@ -816,6 +830,7 @@
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 @@
memmove(&buf->content[0], str, len);
buf->use += len;
buf->content[buf->use] = 0;
+ UPDATE_COMPAT(buf)
return 0;
}
@@ -941,6 +957,7 @@
buf->content[buf->use++] = *cur;
}
buf->content[buf->use] = 0;
+ UPDATE_COMPAT(buf)
return 0;
}