1
0
mirror of https://gitlab.gnome.org/GNOME/libxml2.git synced 2025-10-24 13:33:01 +03:00

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
This commit is contained in:
Daniel Veillard
2012-07-16 18:03:01 +08:00
parent 61551a1eb7
commit 00ac0d3b96
4 changed files with 69 additions and 32 deletions

View File

@@ -5975,8 +5975,8 @@ htmlParseChunk(htmlParserCtxtPtr ctxt, const char *chunk, int size,
} }
if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) && if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) &&
(ctxt->input->buf != NULL) && (ctxt->instate != XML_PARSER_EOF)) { (ctxt->input->buf != NULL) && (ctxt->instate != XML_PARSER_EOF)) {
int base = ctxt->input->base - xmlBufContent(ctxt->input->buf->buffer); size_t base = xmlBufGetInputBase(ctxt->input->buf->buffer, ctxt->input);
int cur = ctxt->input->cur - ctxt->input->base; size_t cur = ctxt->input->cur - ctxt->input->base;
int res; int res;
res = xmlParserInputBufferPush(ctxt->input->buf, size, chunk); res = xmlParserInputBufferPush(ctxt->input->buf, size, chunk);
@@ -5985,9 +5985,7 @@ htmlParseChunk(htmlParserCtxtPtr ctxt, const char *chunk, int size,
ctxt->disableSAX = 1; ctxt->disableSAX = 1;
return (XML_PARSER_EOF); return (XML_PARSER_EOF);
} }
ctxt->input->base = xmlBufContent(ctxt->input->buf->buffer) + base; xmlBufSetInputBaseCur(ctxt->input->buf->buffer, ctxt->input, base, cur);
ctxt->input->cur = ctxt->input->base + cur;
ctxt->input->end = xmlBufEnd(ctxt->input->buf->buffer);
#ifdef DEBUG_PUSH #ifdef DEBUG_PUSH
xmlGenericError(xmlGenericErrorContext, "HPP: pushed %d\n", size); xmlGenericError(xmlGenericErrorContext, "HPP: pushed %d\n", size);
#endif #endif
@@ -6108,14 +6106,12 @@ htmlCreatePushParserCtxt(htmlSAXHandlerPtr sax, void *user_data,
if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) && if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) &&
(ctxt->input->buf != NULL)) { (ctxt->input->buf != NULL)) {
int base = ctxt->input->base - xmlBufContent(ctxt->input->buf->buffer); size_t base = xmlBufGetInputBase(ctxt->input->buf->buffer, ctxt->input);
int cur = ctxt->input->cur - ctxt->input->base; size_t cur = ctxt->input->cur - ctxt->input->base;
xmlParserInputBufferPush(ctxt->input->buf, size, chunk); xmlParserInputBufferPush(ctxt->input->buf, size, chunk);
ctxt->input->base = xmlBufContent(ctxt->input->buf->buffer) + base; xmlBufSetInputBaseCur(ctxt->input->buf->buffer, ctxt->input, base, cur);
ctxt->input->cur = ctxt->input->base + cur;
ctxt->input->end = xmlBufEnd(ctxt->input->buf->buffer);
#ifdef DEBUG_PUSH #ifdef DEBUG_PUSH
xmlGenericError(xmlGenericErrorContext, "HPP: pushed %d\n", size); xmlGenericError(xmlGenericErrorContext, "HPP: pushed %d\n", size);
#endif #endif

48
buf.c
View File

@@ -1144,7 +1144,7 @@ xmlBufMergeBuffer(xmlBufPtr buf, xmlBufferPtr buffer) {
* *
* Update the input to use the current set of pointers from the 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 int
xmlBufResetInput(xmlBufPtr buf, xmlParserInputPtr input) { xmlBufResetInput(xmlBufPtr buf, xmlParserInputPtr input) {
@@ -1154,3 +1154,49 @@ xmlBufResetInput(xmlBufPtr buf, xmlParserInputPtr input) {
input->end = &buf->content[buf->use]; input->end = &buf->content[buf->use];
return(0); 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);
}

3
buf.h
View File

@@ -58,6 +58,9 @@ xmlBufferPtr xmlBufBackToBuffer(xmlBufPtr buf);
int xmlBufMergeBuffer(xmlBufPtr buf, xmlBufferPtr buffer); int xmlBufMergeBuffer(xmlBufPtr buf, xmlBufferPtr buffer);
int xmlBufResetInput(xmlBufPtr buf, xmlParserInputPtr input); 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 #ifdef __cplusplus
} }
#endif #endif

View File

@@ -10835,15 +10835,13 @@ xmlParseTryOrFinish(xmlParserCtxtPtr ctxt, int terminate) {
* buffer. * buffer.
*/ */
if (xmlBufIsEmpty(ctxt->input->buf->buffer) == 0) { if (xmlBufIsEmpty(ctxt->input->buf->buffer) == 0) {
size_t base = ctxt->input->base - size_t base = xmlBufGetInputBase(ctxt->input->buf->buffer,
xmlBufContent(ctxt->input->buf->buffer); ctxt->input);
size_t current = ctxt->input->cur - ctxt->input->base; size_t current = ctxt->input->cur - ctxt->input->base;
xmlParserInputBufferPush(ctxt->input->buf, 0, ""); xmlParserInputBufferPush(ctxt->input->buf, 0, "");
ctxt->input->base = xmlBufContent(ctxt->input->buf->buffer) + xmlBufSetInputBaseCur(ctxt->input->buf->buffer, ctxt->input,
base; base, current);
ctxt->input->cur = ctxt->input->base + current;
ctxt->input->end = xmlBufEnd(ctxt->input->buf->buffer);
} }
avail = xmlBufUse(ctxt->input->buf->buffer) - avail = xmlBufUse(ctxt->input->buf->buffer) -
(ctxt->input->cur - ctxt->input->base); (ctxt->input->cur - ctxt->input->base);
@@ -11707,8 +11705,8 @@ xmldecl_done:
if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) && if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) &&
(ctxt->input->buf != NULL) && (ctxt->instate != XML_PARSER_EOF)) { (ctxt->input->buf != NULL) && (ctxt->instate != XML_PARSER_EOF)) {
int base = ctxt->input->base - xmlBufContent(ctxt->input->buf->buffer); size_t base = xmlBufGetInputBase(ctxt->input->buf->buffer, ctxt->input);
int cur = ctxt->input->cur - ctxt->input->base; size_t cur = ctxt->input->cur - ctxt->input->base;
int res; int res;
/* /*
@@ -11752,9 +11750,7 @@ xmldecl_done:
ctxt->disableSAX = 1; ctxt->disableSAX = 1;
return (XML_PARSER_EOF); return (XML_PARSER_EOF);
} }
ctxt->input->base = xmlBufContent(ctxt->input->buf->buffer) + base; xmlBufSetInputBaseCur(ctxt->input->buf->buffer, ctxt->input, base, cur);
ctxt->input->cur = ctxt->input->base + cur;
ctxt->input->end = xmlBufEnd(ctxt->input->buf->buffer);
#ifdef DEBUG_PUSH #ifdef DEBUG_PUSH
xmlGenericError(xmlGenericErrorContext, "PP: pushed %d\n", size); xmlGenericError(xmlGenericErrorContext, "PP: pushed %d\n", size);
#endif #endif
@@ -11936,14 +11932,12 @@ xmlCreatePushParserCtxt(xmlSAXHandlerPtr sax, void *user_data,
if ((size == 0) || (chunk == NULL)) { if ((size == 0) || (chunk == NULL)) {
ctxt->charset = XML_CHAR_ENCODING_NONE; ctxt->charset = XML_CHAR_ENCODING_NONE;
} else if ((ctxt->input != NULL) && (ctxt->input->buf != NULL)) { } else if ((ctxt->input != NULL) && (ctxt->input->buf != NULL)) {
int base = ctxt->input->base - xmlBufContent(ctxt->input->buf->buffer); size_t base = xmlBufGetInputBase(ctxt->input->buf->buffer, ctxt->input);
int cur = ctxt->input->cur - ctxt->input->base; size_t cur = ctxt->input->cur - ctxt->input->base;
xmlParserInputBufferPush(ctxt->input->buf, size, chunk); xmlParserInputBufferPush(ctxt->input->buf, size, chunk);
ctxt->input->base = xmlBufContent(ctxt->input->buf->buffer) + base; xmlBufSetInputBaseCur(ctxt->input->buf->buffer, ctxt->input, base, cur);
ctxt->input->cur = ctxt->input->base + cur;
ctxt->input->end = xmlBufEnd(ctxt->input->buf->buffer);
#ifdef DEBUG_PUSH #ifdef DEBUG_PUSH
xmlGenericError(xmlGenericErrorContext, "PP: pushed %d\n", size); xmlGenericError(xmlGenericErrorContext, "PP: pushed %d\n", size);
#endif #endif
@@ -14443,14 +14437,12 @@ xmlCtxtResetPush(xmlParserCtxtPtr ctxt, const char *chunk,
if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) && if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) &&
(ctxt->input->buf != NULL)) { (ctxt->input->buf != NULL)) {
int base = ctxt->input->base - xmlBufContent(ctxt->input->buf->buffer); size_t base = xmlBufGetInputBase(ctxt->input->buf->buffer, ctxt->input);
int cur = ctxt->input->cur - ctxt->input->base; size_t cur = ctxt->input->cur - ctxt->input->base;
xmlParserInputBufferPush(ctxt->input->buf, size, chunk); xmlParserInputBufferPush(ctxt->input->buf, size, chunk);
ctxt->input->base = xmlBufContent(ctxt->input->buf->buffer) + base; xmlBufSetInputBaseCur(ctxt->input->buf->buffer, ctxt->input, base, cur);
ctxt->input->cur = ctxt->input->base + cur;
ctxt->input->end = xmlBufEnd(ctxt->input->buf->buffer);
#ifdef DEBUG_PUSH #ifdef DEBUG_PUSH
xmlGenericError(xmlGenericErrorContext, "PP: pushed %d\n", size); xmlGenericError(xmlGenericErrorContext, "PP: pushed %d\n", size);
#endif #endif