[libxml2] More cleanups for input/buffers code



commit 00ac0d3b96459865684fc9378e420e93418f9ef9
Author: Daniel Veillard <veillard redhat com>
Date:   Mon Jul 16 18:03:01 2012 +0800

    More cleanups for input/buffers code
    
    When calling xmlParserInputBufferPush, the buffer may be reallocated
    and at the input level the pointers for base, cur and end need to
    be reevaluated.
    * buf.c buf.h: add two new functions, one to get the base from the
      input of the buffer, and another one to reset the pointers based
      on the cur and base inded
    * HTMLparser.c parser.c: cleanup to use the new helper functions
      as well as making sure size_t is used for the indexes computations

 HTMLparser.c |   16 ++++++----------
 buf.c        |   48 +++++++++++++++++++++++++++++++++++++++++++++++-
 buf.h        |    3 +++
 parser.c     |   34 +++++++++++++---------------------
 4 files changed, 69 insertions(+), 32 deletions(-)
---
diff --git a/HTMLparser.c b/HTMLparser.c
index fdbef80..63befed 100644
--- a/HTMLparser.c
+++ b/HTMLparser.c
@@ -5975,8 +5975,8 @@ htmlParseChunk(htmlParserCtxtPtr ctxt, const char *chunk, int size,
     }
     if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) &&
         (ctxt->input->buf != NULL) && (ctxt->instate != XML_PARSER_EOF))  {
-	int base = ctxt->input->base - xmlBufContent(ctxt->input->buf->buffer);
-	int cur = ctxt->input->cur - ctxt->input->base;
+	size_t base = xmlBufGetInputBase(ctxt->input->buf->buffer, ctxt->input);
+	size_t cur = ctxt->input->cur - ctxt->input->base;
 	int res;
 
 	res = xmlParserInputBufferPush(ctxt->input->buf, size, chunk);
@@ -5985,9 +5985,7 @@ htmlParseChunk(htmlParserCtxtPtr ctxt, const char *chunk, int size,
 	    ctxt->disableSAX = 1;
 	    return (XML_PARSER_EOF);
 	}
-	ctxt->input->base = xmlBufContent(ctxt->input->buf->buffer) + base;
-	ctxt->input->cur = ctxt->input->base + cur;
-	ctxt->input->end = xmlBufEnd(ctxt->input->buf->buffer);
+        xmlBufSetInputBaseCur(ctxt->input->buf->buffer, ctxt->input, base, cur);
 #ifdef DEBUG_PUSH
 	xmlGenericError(xmlGenericErrorContext, "HPP: pushed %d\n", size);
 #endif
@@ -6108,14 +6106,12 @@ htmlCreatePushParserCtxt(htmlSAXHandlerPtr sax, void *user_data,
 
     if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) &&
         (ctxt->input->buf != NULL))  {
-	int base = ctxt->input->base - xmlBufContent(ctxt->input->buf->buffer);
-	int cur = ctxt->input->cur - ctxt->input->base;
+	size_t base = xmlBufGetInputBase(ctxt->input->buf->buffer, ctxt->input);
+	size_t cur = ctxt->input->cur - ctxt->input->base;
 
 	xmlParserInputBufferPush(ctxt->input->buf, size, chunk);
 
-	ctxt->input->base = xmlBufContent(ctxt->input->buf->buffer) + base;
-	ctxt->input->cur = ctxt->input->base + cur;
-	ctxt->input->end = xmlBufEnd(ctxt->input->buf->buffer);
+        xmlBufSetInputBaseCur(ctxt->input->buf->buffer, ctxt->input, base, cur);
 #ifdef DEBUG_PUSH
 	xmlGenericError(xmlGenericErrorContext, "HPP: pushed %d\n", size);
 #endif
diff --git a/buf.c b/buf.c
index 8cf6199..6ab7106 100644
--- a/buf.c
+++ b/buf.c
@@ -1144,7 +1144,7 @@ xmlBufMergeBuffer(xmlBufPtr buf, xmlBufferPtr buffer) {
  *
  * Update the input to use the current set of pointers from the buffer.
  *
- * Returns -1 in case of error, 0 otherwise, in any case @buffer is freed
+ * Returns -1 in case of error, 0 otherwise
  */
 int
 xmlBufResetInput(xmlBufPtr buf, xmlParserInputPtr input) {
@@ -1154,3 +1154,49 @@ xmlBufResetInput(xmlBufPtr buf, xmlParserInputPtr input) {
     input->end = &buf->content[buf->use];
     return(0);
 }
+
+/**
+ * xmlBufGetInputBase:
+ * @buf: an xmlBufPtr
+ * @input: an xmlParserInputPtr
+ *
+ * Get the base of the @input relative to the beginning of the buffer
+ *
+ * Returns the size_t corresponding to the displacement
+ */
+size_t
+xmlBufGetInputBase(xmlBufPtr buf, xmlParserInputPtr input) {
+    size_t base;
+
+    base = input->base - buf->content;
+    /*
+     * We could do some pointer arythmetic checks but that's probably
+     * sufficient.
+     */
+    if (base > buf->size) {
+        xmlBufOverflowError(buf, "Input reference outside of the buffer");
+        base = 0;
+    }
+    return(base);
+}
+
+/**
+ * xmlBufSetInputBaseCur:
+ * @buf: an xmlBufPtr
+ * @input: an xmlParserInputPtr
+ *
+ * Update the input to use the base and cur relative to the buffer
+ * after a possible reallocation of its content
+ *
+ * Returns -1 in case of error, 0 otherwise
+ */
+int
+xmlBufSetInputBaseCur(xmlBufPtr buf, xmlParserInputPtr input,
+                      size_t base, size_t cur) {
+    if ((input == NULL) || (buf == NULL))
+        return(-1);
+    input->base = &buf->content[base];
+    input->cur = input->base + cur;
+    input->end = &buf->content[buf->use];
+    return(0);
+}
diff --git a/buf.h b/buf.h
index da97e86..d103746 100644
--- a/buf.h
+++ b/buf.h
@@ -58,6 +58,9 @@ xmlBufferPtr xmlBufBackToBuffer(xmlBufPtr buf);
 int xmlBufMergeBuffer(xmlBufPtr buf, xmlBufferPtr buffer);
 
 int xmlBufResetInput(xmlBufPtr buf, xmlParserInputPtr input);
+size_t xmlBufGetInputBase(xmlBufPtr buf, xmlParserInputPtr input);
+int xmlBufSetInputBaseCur(xmlBufPtr buf, xmlParserInputPtr input,
+                          size_t base, size_t cur);
 #ifdef __cplusplus
 }
 #endif
diff --git a/parser.c b/parser.c
index d5102c8..f36e84b 100644
--- a/parser.c
+++ b/parser.c
@@ -10835,15 +10835,13 @@ xmlParseTryOrFinish(xmlParserCtxtPtr ctxt, int terminate) {
 	     * buffer.
 	     */
 	    if (xmlBufIsEmpty(ctxt->input->buf->buffer) == 0) {
-		size_t base = ctxt->input->base -
-		           xmlBufContent(ctxt->input->buf->buffer);
+                size_t base = xmlBufGetInputBase(ctxt->input->buf->buffer,
+                                                 ctxt->input);
 		size_t current = ctxt->input->cur - ctxt->input->base;
 
 		xmlParserInputBufferPush(ctxt->input->buf, 0, "");
-		ctxt->input->base = xmlBufContent(ctxt->input->buf->buffer) +
-                                    base;
-		ctxt->input->cur = ctxt->input->base + current;
-		ctxt->input->end = xmlBufEnd(ctxt->input->buf->buffer);
+                xmlBufSetInputBaseCur(ctxt->input->buf->buffer, ctxt->input,
+                                      base, current);
 	    }
 	    avail = xmlBufUse(ctxt->input->buf->buffer) -
 		    (ctxt->input->cur - ctxt->input->base);
@@ -11707,8 +11705,8 @@ xmldecl_done:
 
     if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) &&
         (ctxt->input->buf != NULL) && (ctxt->instate != XML_PARSER_EOF))  {
-	int base = ctxt->input->base - xmlBufContent(ctxt->input->buf->buffer);
-	int cur = ctxt->input->cur - ctxt->input->base;
+	size_t base = xmlBufGetInputBase(ctxt->input->buf->buffer, ctxt->input);
+	size_t cur = ctxt->input->cur - ctxt->input->base;
 	int res;
 
         /*
@@ -11752,9 +11750,7 @@ xmldecl_done:
 	    ctxt->disableSAX = 1;
 	    return (XML_PARSER_EOF);
 	}
-	ctxt->input->base = xmlBufContent(ctxt->input->buf->buffer) + base;
-	ctxt->input->cur = ctxt->input->base + cur;
-	ctxt->input->end = xmlBufEnd(ctxt->input->buf->buffer);
+        xmlBufSetInputBaseCur(ctxt->input->buf->buffer, ctxt->input, base, cur);
 #ifdef DEBUG_PUSH
 	xmlGenericError(xmlGenericErrorContext, "PP: pushed %d\n", size);
 #endif
@@ -11936,14 +11932,12 @@ xmlCreatePushParserCtxt(xmlSAXHandlerPtr sax, void *user_data,
     if ((size == 0) || (chunk == NULL)) {
 	ctxt->charset = XML_CHAR_ENCODING_NONE;
     } else if ((ctxt->input != NULL) && (ctxt->input->buf != NULL)) {
-	int base = ctxt->input->base - xmlBufContent(ctxt->input->buf->buffer);
-	int cur = ctxt->input->cur - ctxt->input->base;
+	size_t base = xmlBufGetInputBase(ctxt->input->buf->buffer, ctxt->input);
+	size_t cur = ctxt->input->cur - ctxt->input->base;
 
 	xmlParserInputBufferPush(ctxt->input->buf, size, chunk);
 
-	ctxt->input->base = xmlBufContent(ctxt->input->buf->buffer) + base;
-	ctxt->input->cur = ctxt->input->base + cur;
-	ctxt->input->end = xmlBufEnd(ctxt->input->buf->buffer);
+        xmlBufSetInputBaseCur(ctxt->input->buf->buffer, ctxt->input, base, cur);
 #ifdef DEBUG_PUSH
 	xmlGenericError(xmlGenericErrorContext, "PP: pushed %d\n", size);
 #endif
@@ -14443,14 +14437,12 @@ xmlCtxtResetPush(xmlParserCtxtPtr ctxt, const char *chunk,
 
     if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) &&
         (ctxt->input->buf != NULL)) {
-        int base = ctxt->input->base - xmlBufContent(ctxt->input->buf->buffer);
-        int cur = ctxt->input->cur - ctxt->input->base;
+	size_t base = xmlBufGetInputBase(ctxt->input->buf->buffer, ctxt->input);
+        size_t cur = ctxt->input->cur - ctxt->input->base;
 
         xmlParserInputBufferPush(ctxt->input->buf, size, chunk);
 
-        ctxt->input->base = xmlBufContent(ctxt->input->buf->buffer) + base;
-        ctxt->input->cur = ctxt->input->base + cur;
-        ctxt->input->end = xmlBufEnd(ctxt->input->buf->buffer);
+        xmlBufSetInputBaseCur(ctxt->input->buf->buffer, ctxt->input, base, cur);
 #ifdef DEBUG_PUSH
         xmlGenericError(xmlGenericErrorContext, "PP: pushed %d\n", size);
 #endif



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]