summaryrefslogtreecommitdiff
path: root/xmlIO.c
diff options
context:
space:
mode:
Diffstat (limited to 'xmlIO.c')
-rw-r--r--xmlIO.c241
1 files changed, 103 insertions, 138 deletions
diff --git a/xmlIO.c b/xmlIO.c
index 847cb7e..73a995d 100644
--- a/xmlIO.c
+++ b/xmlIO.c
@@ -96,9 +96,6 @@
#endif
#include <libxml/globals.h>
-#include "buf.h"
-#include "enc.h"
-
/* #define VERBOSE_FAILURE */
/* #define DEBUG_EXTERNAL_ENTITIES */
/* #define DEBUG_INPUT */
@@ -771,21 +768,13 @@ int
xmlCheckFilename (const char *path)
{
#ifdef HAVE_STAT
- struct stat stat_buffer;
+ struct stat stat_buffer;
#endif
- if (path == NULL)
- return(0);
+ if (path == NULL)
+ return(0);
#ifdef HAVE_STAT
#if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
- /*
- * On Windows stat and wstat do not work with long pathname,
- * which start with '\\?\'
- */
- if ((path[0] == '\\') && (path[1] == '\\') && (path[2] == '?') &&
- (path[3] == '\\') )
- return 1;
-
if (xmlWrapStat(path, &stat_buffer) == -1)
return 0;
#else
@@ -800,7 +789,7 @@ xmlCheckFilename (const char *path)
return 1;
}
-int
+static int
xmlNop(void) {
return(0);
}
@@ -1001,7 +990,7 @@ xmlFileOpenW (const char *filename) {
#if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
fd = xmlWrapOpen(path, 1);
#else
- fd = fopen(path, "wb");
+ fd = fopen(path, "wb");
#endif /* WIN32 */
if (fd == NULL) xmlIOErr(0, path);
@@ -2047,8 +2036,8 @@ xmlIOHTTPCloseWrite( void * context, const char * http_mthd ) {
/* Pull the data out of the memory output buffer */
xmlOutputBufferPtr dctxt = ctxt->doc_buff;
- http_content = (char *) xmlBufContent(dctxt->buffer);
- content_lgth = xmlBufUse(dctxt->buffer);
+ http_content = (char *)dctxt->buffer->content;
+ content_lgth = dctxt->buffer->use;
}
if ( http_content == NULL ) {
@@ -2415,15 +2404,15 @@ xmlAllocParserInputBuffer(xmlCharEncoding enc) {
return(NULL);
}
memset(ret, 0, (size_t) sizeof(xmlParserInputBuffer));
- ret->buffer = xmlBufCreateSize(2 * xmlDefaultBufferSize);
+ ret->buffer = xmlBufferCreateSize(2 * xmlDefaultBufferSize);
if (ret->buffer == NULL) {
xmlFree(ret);
return(NULL);
}
- xmlBufSetAllocationScheme(ret->buffer, XML_BUFFER_ALLOC_DOUBLEIT);
+ ret->buffer->alloc = XML_BUFFER_ALLOC_DOUBLEIT;
ret->encoder = xmlGetCharEncodingHandler(enc);
if (ret->encoder != NULL)
- ret->raw = xmlBufCreateSize(2 * xmlDefaultBufferSize);
+ ret->raw = xmlBufferCreateSize(2 * xmlDefaultBufferSize);
else
ret->raw = NULL;
ret->readcallback = NULL;
@@ -2454,19 +2443,19 @@ xmlAllocOutputBuffer(xmlCharEncodingHandlerPtr encoder) {
return(NULL);
}
memset(ret, 0, (size_t) sizeof(xmlOutputBuffer));
- ret->buffer = xmlBufCreate();
+ ret->buffer = xmlBufferCreate();
if (ret->buffer == NULL) {
xmlFree(ret);
return(NULL);
}
/* try to avoid a performance problem with Windows realloc() */
- if (xmlBufGetAllocationScheme(ret->buffer) == XML_BUFFER_ALLOC_EXACT)
- xmlBufSetAllocationScheme(ret->buffer, XML_BUFFER_ALLOC_DOUBLEIT);
+ if (ret->buffer->alloc == XML_BUFFER_ALLOC_EXACT)
+ ret->buffer->alloc = XML_BUFFER_ALLOC_DOUBLEIT;
ret->encoder = encoder;
if (encoder != NULL) {
- ret->conv = xmlBufCreateSize(4000);
+ ret->conv = xmlBufferCreateSize(4000);
if (ret->conv == NULL) {
xmlFree(ret);
return(NULL);
@@ -2475,7 +2464,7 @@ xmlAllocOutputBuffer(xmlCharEncodingHandlerPtr encoder) {
/*
* This call is designed to initiate the encoder state
*/
- xmlCharEncOutput(ret, 1);
+ xmlCharEncOutFunc(encoder, ret->conv, NULL);
} else
ret->conv = NULL;
ret->writecallback = NULL;
@@ -2504,7 +2493,7 @@ xmlAllocOutputBufferInternal(xmlCharEncodingHandlerPtr encoder) {
return(NULL);
}
memset(ret, 0, (size_t) sizeof(xmlOutputBuffer));
- ret->buffer = xmlBufCreate();
+ ret->buffer = xmlBufferCreate();
if (ret->buffer == NULL) {
xmlFree(ret);
return(NULL);
@@ -2513,12 +2502,15 @@ xmlAllocOutputBufferInternal(xmlCharEncodingHandlerPtr encoder) {
/*
* For conversion buffers we use the special IO handling
+ * We don't do that from the exported API to avoid confusing
+ * user's code.
*/
- xmlBufSetAllocationScheme(ret->buffer, XML_BUFFER_ALLOC_IO);
+ ret->buffer->alloc = XML_BUFFER_ALLOC_IO;
+ ret->buffer->contentIO = ret->buffer->content;
ret->encoder = encoder;
if (encoder != NULL) {
- ret->conv = xmlBufCreateSize(4000);
+ ret->conv = xmlBufferCreateSize(4000);
if (ret->conv == NULL) {
xmlFree(ret);
return(NULL);
@@ -2527,7 +2519,7 @@ xmlAllocOutputBufferInternal(xmlCharEncodingHandlerPtr encoder) {
/*
* This call is designed to initiate the encoder state
*/
- xmlCharEncOutput(ret, 1);
+ xmlCharEncOutFunc(encoder, ret->conv, NULL);
} else
ret->conv = NULL;
ret->writecallback = NULL;
@@ -2551,7 +2543,7 @@ xmlFreeParserInputBuffer(xmlParserInputBufferPtr in) {
if (in == NULL) return;
if (in->raw) {
- xmlBufFree(in->raw);
+ xmlBufferFree(in->raw);
in->raw = NULL;
}
if (in->encoder != NULL) {
@@ -2561,7 +2553,7 @@ xmlFreeParserInputBuffer(xmlParserInputBufferPtr in) {
in->closecallback(in->context);
}
if (in->buffer != NULL) {
- xmlBufFree(in->buffer);
+ xmlBufferFree(in->buffer);
in->buffer = NULL;
}
@@ -2593,14 +2585,14 @@ xmlOutputBufferClose(xmlOutputBufferPtr out)
}
written = out->written;
if (out->conv) {
- xmlBufFree(out->conv);
+ xmlBufferFree(out->conv);
out->conv = NULL;
}
if (out->encoder != NULL) {
xmlCharEncCloseFunc(out->encoder);
}
if (out->buffer != NULL) {
- xmlBufFree(out->buffer);
+ xmlBufferFree(out->buffer);
out->buffer = NULL;
}
@@ -2930,39 +2922,6 @@ xmlOutputBufferCreateBuffer(xmlBufferPtr buffer,
return(ret);
}
-/**
- * xmlOutputBufferGetContent:
- * @out: an xmlOutputBufferPtr
- *
- * Gives a pointer to the data currently held in the output buffer
- *
- * Returns a pointer to the data or NULL in case of error
- */
-const xmlChar *
-xmlOutputBufferGetContent(xmlOutputBufferPtr out) {
- if ((out == NULL) || (out->buffer == NULL))
- return(NULL);
-
- return(xmlBufContent(out->buffer));
-}
-
-/**
- * xmlOutputBufferGetSize:
- * @out: an xmlOutputBufferPtr
- *
- * Gives the length of the data currently held in the output buffer
- *
- * Returns 0 in case or error or no data is held, the size otherwise
- */
-size_t
-xmlOutputBufferGetSize(xmlOutputBufferPtr out) {
- if ((out == NULL) || (out->buffer == NULL))
- return(0);
-
- return(xmlBufUse(out->buffer));
-}
-
-
#endif /* LIBXML_OUTPUT_ENABLED */
/**
@@ -3015,7 +2974,7 @@ xmlParserInputBufferCreateMem(const char *mem, int size, xmlCharEncoding enc) {
ret->context = (void *) mem;
ret->readcallback = (xmlInputReadCallback) xmlNop;
ret->closecallback = NULL;
- errcode = xmlBufAdd(ret->buffer, (const xmlChar *) mem, size);
+ errcode = xmlBufferAdd(ret->buffer, (const xmlChar *) mem, size);
if (errcode != 0) {
xmlFree(ret);
return(NULL);
@@ -3052,14 +3011,14 @@ xmlParserInputBufferCreateStatic(const char *mem, int size,
return(NULL);
}
memset(ret, 0, (size_t) sizeof(xmlParserInputBuffer));
- ret->buffer = xmlBufCreateStatic((void *)mem, (size_t) size);
+ ret->buffer = xmlBufferCreateStatic((void *)mem, (size_t) size);
if (ret->buffer == NULL) {
xmlFree(ret);
return(NULL);
}
ret->encoder = xmlGetCharEncodingHandler(enc);
if (ret->encoder != NULL)
- ret->raw = xmlBufCreateSize(2 * xmlDefaultBufferSize);
+ ret->raw = xmlBufferCreateSize(2 * xmlDefaultBufferSize);
else
ret->raw = NULL;
ret->compressed = -1;
@@ -3228,33 +3187,33 @@ xmlParserInputBufferPush(xmlParserInputBufferPtr in,
* Store the data in the incoming raw buffer
*/
if (in->raw == NULL) {
- in->raw = xmlBufCreate();
+ in->raw = xmlBufferCreate();
}
- ret = xmlBufAdd(in->raw, (const xmlChar *) buf, len);
+ ret = xmlBufferAdd(in->raw, (const xmlChar *) buf, len);
if (ret != 0)
return(-1);
/*
* convert as much as possible to the parser reading buffer.
*/
- use = xmlBufUse(in->raw);
- nbchars = xmlCharEncInput(in, 1);
+ use = in->raw->use;
+ nbchars = xmlCharEncInFunc(in->encoder, in->buffer, in->raw);
if (nbchars < 0) {
xmlIOErr(XML_IO_ENCODER, NULL);
in->error = XML_IO_ENCODER;
return(-1);
}
- in->rawconsumed += (use - xmlBufUse(in->raw));
+ in->rawconsumed += (use - in->raw->use);
} else {
nbchars = len;
- ret = xmlBufAdd(in->buffer, (xmlChar *) buf, nbchars);
+ ret = xmlBufferAdd(in->buffer, (xmlChar *) buf, nbchars);
if (ret != 0)
return(-1);
}
#ifdef DEBUG_INPUT
xmlGenericError(xmlGenericErrorContext,
"I/O: pushed %d chars, buffer %d/%d\n",
- nbchars, xmlBufUse(in->buffer), xmlBufLength(in->buffer));
+ nbchars, in->buffer->use, in->buffer->size);
#endif
return(nbchars);
}
@@ -3292,23 +3251,29 @@ xmlParserInputBufferGrow(xmlParserInputBufferPtr in, int len) {
char *buffer = NULL;
int res = 0;
int nbchars = 0;
+ int buffree;
+ unsigned int needSize;
if ((in == NULL) || (in->error)) return(-1);
if ((len <= MINLEN) && (len != 4))
len = MINLEN;
- if (xmlBufAvail(in->buffer) <= 0) {
+ buffree = in->buffer->size - in->buffer->use;
+ if (buffree <= 0) {
xmlIOErr(XML_IO_BUFFER_FULL, NULL);
in->error = XML_IO_BUFFER_FULL;
return(-1);
}
- if (xmlBufGrow(in->buffer, len + 1) < 0) {
- xmlIOErrMemory("growing input buffer");
- in->error = XML_ERR_NO_MEMORY;
- return(-1);
+ needSize = in->buffer->use + len + 1;
+ if (needSize > in->buffer->size){
+ if (!xmlBufferResize(in->buffer, needSize)){
+ xmlIOErrMemory("growing input buffer");
+ in->error = XML_ERR_NO_MEMORY;
+ return(-1);
+ }
}
- buffer = (char *)xmlBufEnd(in->buffer);
+ buffer = (char *)&in->buffer->content[in->buffer->use];
/*
* Call the read method for this I/O type.
@@ -3333,31 +3298,32 @@ xmlParserInputBufferGrow(xmlParserInputBufferPtr in, int len) {
* Store the data in the incoming raw buffer
*/
if (in->raw == NULL) {
- in->raw = xmlBufCreate();
+ in->raw = xmlBufferCreate();
}
- res = xmlBufAdd(in->raw, (const xmlChar *) buffer, len);
+ res = xmlBufferAdd(in->raw, (const xmlChar *) buffer, len);
if (res != 0)
return(-1);
/*
* convert as much as possible to the parser reading buffer.
*/
- use = xmlBufUse(in->raw);
- nbchars = xmlCharEncInput(in, 1);
+ use = in->raw->use;
+ nbchars = xmlCharEncInFunc(in->encoder, in->buffer, in->raw);
if (nbchars < 0) {
xmlIOErr(XML_IO_ENCODER, NULL);
in->error = XML_IO_ENCODER;
return(-1);
}
- in->rawconsumed += (use - xmlBufUse(in->raw));
+ in->rawconsumed += (use - in->raw->use);
} else {
nbchars = len;
- xmlBufAddLen(in->buffer, nbchars);
+ in->buffer->use += nbchars;
+ buffer[nbchars] = 0;
}
#ifdef DEBUG_INPUT
xmlGenericError(xmlGenericErrorContext,
- "I/O: read %d chars, buffer %d\n",
- nbchars, xmlBufUse(in->buffer));
+ "I/O: read %d chars, buffer %d/%d\n",
+ nbchars, in->buffer->use, in->buffer->size);
#endif
return(nbchars);
}
@@ -3379,7 +3345,8 @@ xmlParserInputBufferRead(xmlParserInputBufferPtr in, int len) {
if ((in == NULL) || (in->error)) return(-1);
if (in->readcallback != NULL)
return(xmlParserInputBufferGrow(in, len));
- else if (xmlBufGetAllocationScheme(in->buffer) == XML_BUFFER_ALLOC_IMMUTABLE)
+ else if ((in->buffer != NULL) &&
+ (in->buffer->alloc == XML_BUFFER_ALLOC_IMMUTABLE))
return(0);
else
return(-1);
@@ -3424,30 +3391,30 @@ xmlOutputBufferWrite(xmlOutputBufferPtr out, int len, const char *buf) {
* Store the data in the incoming raw buffer
*/
if (out->conv == NULL) {
- out->conv = xmlBufCreate();
+ out->conv = xmlBufferCreate();
}
- ret = xmlBufAdd(out->buffer, (const xmlChar *) buf, chunk);
+ ret = xmlBufferAdd(out->buffer, (const xmlChar *) buf, chunk);
if (ret != 0)
return(-1);
- if ((xmlBufUse(out->buffer) < MINLEN) && (chunk == len))
+ if ((out->buffer->use < MINLEN) && (chunk == len))
goto done;
/*
* convert as much as possible to the parser reading buffer.
*/
- ret = xmlCharEncOutput(out, 0);
+ ret = xmlCharEncOutFunc(out->encoder, out->conv, out->buffer);
if ((ret < 0) && (ret != -3)) {
xmlIOErr(XML_IO_ENCODER, NULL);
out->error = XML_IO_ENCODER;
return(-1);
}
- nbchars = xmlBufUse(out->conv);
+ nbchars = out->conv->use;
} else {
- ret = xmlBufAdd(out->buffer, (const xmlChar *) buf, chunk);
+ ret = xmlBufferAdd(out->buffer, (const xmlChar *) buf, chunk);
if (ret != 0)
return(-1);
- nbchars = xmlBufUse(out->buffer);
+ nbchars = out->buffer->use;
}
buf += chunk;
len -= chunk;
@@ -3461,14 +3428,14 @@ xmlOutputBufferWrite(xmlOutputBufferPtr out, int len, const char *buf) {
*/
if (out->encoder != NULL) {
ret = out->writecallback(out->context,
- (const char *)xmlBufContent(out->conv), nbchars);
+ (const char *)out->conv->content, nbchars);
if (ret >= 0)
- xmlBufShrink(out->conv, ret);
+ xmlBufferShrink(out->conv, ret);
} else {
ret = out->writecallback(out->context,
- (const char *)xmlBufContent(out->buffer), nbchars);
+ (const char *)out->buffer->content, nbchars);
if (ret >= 0)
- xmlBufShrink(out->buffer, ret);
+ xmlBufferShrink(out->buffer, ret);
}
if (ret < 0) {
xmlIOErr(XML_IO_WRITE, NULL);
@@ -3512,7 +3479,7 @@ xmlEscapeContent(unsigned char* out, int *outlen,
inend = in + (*inlen);
while ((in < inend) && (out < outend)) {
- if (*in == '<') {
+ if (*in == '<') {
if (outend - out < 4) break;
*out++ = '&';
*out++ = 'l';
@@ -3576,8 +3543,7 @@ xmlOutputBufferWriteEscape(xmlOutputBufferPtr out, const xmlChar *str,
if ((out == NULL) || (out->error) || (str == NULL) ||
(out->buffer == NULL) ||
- (xmlBufGetAllocationScheme(out->buffer) == XML_BUFFER_ALLOC_IMMUTABLE))
- return(-1);
+ (out->buffer->alloc == XML_BUFFER_ALLOC_IMMUTABLE)) return(-1);
len = strlen((const char *)str);
if (len < 0) return(0);
if (out->error) return(-1);
@@ -3590,14 +3556,14 @@ xmlOutputBufferWriteEscape(xmlOutputBufferPtr out, const xmlChar *str,
* how many bytes to consume and how many bytes to store.
*/
cons = len;
- chunk = xmlBufAvail(out->buffer) - 1;
+ chunk = (out->buffer->size - out->buffer->use) - 1;
/*
* make sure we have enough room to save first, if this is
* not the case force a flush, but make sure we stay in the loop
*/
if (chunk < 40) {
- if (xmlBufGrow(out->buffer, 100) < 0)
+ if (xmlBufferGrow(out->buffer, out->buffer->size + 100) < 0)
return(-1);
oldwritten = -1;
continue;
@@ -3611,33 +3577,36 @@ xmlOutputBufferWriteEscape(xmlOutputBufferPtr out, const xmlChar *str,
* Store the data in the incoming raw buffer
*/
if (out->conv == NULL) {
- out->conv = xmlBufCreate();
+ out->conv = xmlBufferCreate();
}
- ret = escaping(xmlBufEnd(out->buffer) ,
+ ret = escaping(out->buffer->content + out->buffer->use ,
&chunk, str, &cons);
if ((ret < 0) || (chunk == 0)) /* chunk==0 => nothing done */
return(-1);
- xmlBufAddLen(out->buffer, chunk);
+ out->buffer->use += chunk;
+ out->buffer->content[out->buffer->use] = 0;
- if ((xmlBufUse(out->buffer) < MINLEN) && (cons == len))
+ if ((out->buffer->use < MINLEN) && (cons == len))
goto done;
/*
* convert as much as possible to the output buffer.
*/
- ret = xmlCharEncOutput(out, 0);
+ ret = xmlCharEncOutFunc(out->encoder, out->conv, out->buffer);
if ((ret < 0) && (ret != -3)) {
xmlIOErr(XML_IO_ENCODER, NULL);
out->error = XML_IO_ENCODER;
return(-1);
}
- nbchars = xmlBufUse(out->conv);
+ nbchars = out->conv->use;
} else {
- ret = escaping(xmlBufEnd(out->buffer), &chunk, str, &cons);
+ ret = escaping(out->buffer->content + out->buffer->use ,
+ &chunk, str, &cons);
if ((ret < 0) || (chunk == 0)) /* chunk==0 => nothing done */
return(-1);
- xmlBufAddLen(out->buffer, chunk);
- nbchars = xmlBufUse(out->buffer);
+ out->buffer->use += chunk;
+ out->buffer->content[out->buffer->use] = 0;
+ nbchars = out->buffer->use;
}
str += cons;
len -= cons;
@@ -3651,14 +3620,14 @@ xmlOutputBufferWriteEscape(xmlOutputBufferPtr out, const xmlChar *str,
*/
if (out->encoder != NULL) {
ret = out->writecallback(out->context,
- (const char *)xmlBufContent(out->conv), nbchars);
+ (const char *)out->conv->content, nbchars);
if (ret >= 0)
- xmlBufShrink(out->conv, ret);
+ xmlBufferShrink(out->conv, ret);
} else {
ret = out->writecallback(out->context,
- (const char *)xmlBufContent(out->buffer), nbchars);
+ (const char *)out->buffer->content, nbchars);
if (ret >= 0)
- xmlBufShrink(out->buffer, ret);
+ xmlBufferShrink(out->buffer, ret);
}
if (ret < 0) {
xmlIOErr(XML_IO_WRITE, NULL);
@@ -3666,8 +3635,8 @@ xmlOutputBufferWriteEscape(xmlOutputBufferPtr out, const xmlChar *str,
return(ret);
}
out->written += ret;
- } else if (xmlBufAvail(out->buffer) < MINLEN) {
- xmlBufGrow(out->buffer, MINLEN);
+ } else if (out->buffer->size - out->buffer->use < MINLEN) {
+ xmlBufferResize(out->buffer, out->buffer->size + MINLEN);
}
written += nbchars;
} while ((len > 0) && (oldwritten != written));
@@ -3725,16 +3694,14 @@ xmlOutputBufferFlush(xmlOutputBufferPtr out) {
*/
if ((out->conv != NULL) && (out->encoder != NULL)) {
/*
- * convert as much as possible to the parser output buffer.
+ * convert as much as possible to the parser reading buffer.
*/
- do {
- nbchars = xmlCharEncOutput(out, 0);
- if (nbchars < 0) {
- xmlIOErr(XML_IO_ENCODER, NULL);
- out->error = XML_IO_ENCODER;
- return(-1);
- }
- } while (nbchars);
+ nbchars = xmlCharEncOutFunc(out->encoder, out->conv, out->buffer);
+ if (nbchars < 0) {
+ xmlIOErr(XML_IO_ENCODER, NULL);
+ out->error = XML_IO_ENCODER;
+ return(-1);
+ }
}
/*
@@ -3743,16 +3710,14 @@ xmlOutputBufferFlush(xmlOutputBufferPtr out) {
if ((out->conv != NULL) && (out->encoder != NULL) &&
(out->writecallback != NULL)) {
ret = out->writecallback(out->context,
- (const char *)xmlBufContent(out->conv),
- xmlBufUse(out->conv));
+ (const char *)out->conv->content, out->conv->use);
if (ret >= 0)
- xmlBufShrink(out->conv, ret);
+ xmlBufferShrink(out->conv, ret);
} else if (out->writecallback != NULL) {
ret = out->writecallback(out->context,
- (const char *)xmlBufContent(out->buffer),
- xmlBufUse(out->buffer));
+ (const char *)out->buffer->content, out->buffer->use);
if (ret >= 0)
- xmlBufShrink(out->buffer, ret);
+ xmlBufferShrink(out->buffer, ret);
}
if (ret < 0) {
xmlIOErr(XML_IO_FLUSH, NULL);