1
0
mirror of https://gitlab.gnome.org/GNOME/libxml2.git synced 2024-10-26 03:55:04 +03:00

io: Always consume encoding handler when creating output buffers

Also free encoding handler in error case.

Remove xmlAllocOutputBufferInternal which was identical to
xmlAllocOutputBuffer.
This commit is contained in:
Nick Wellnhofer 2024-07-29 14:18:57 +02:00
parent 36ea881b9d
commit a530ff125d
4 changed files with 29 additions and 78 deletions

View File

@ -508,10 +508,8 @@ htmlNodeDumpFileFormat(FILE *out, xmlDocPtr doc,
*/
handler = htmlFindOutputEncoder(encoding);
buf = xmlOutputBufferCreateFile(out, handler);
if (buf == NULL) {
xmlCharEncCloseFunc(handler);
if (buf == NULL)
return(0);
}
htmlNodeDumpFormatOutput(buf, doc, cur, NULL, format);
@ -560,11 +558,9 @@ htmlDocDumpMemoryFormat(xmlDocPtr cur, xmlChar**mem, int *size, int format) {
encoding = (const char *) htmlGetMetaEncoding(cur);
handler = htmlFindOutputEncoder(encoding);
buf = xmlAllocOutputBufferInternal(handler);
if (buf == NULL) {
xmlCharEncCloseFunc(handler);
buf = xmlAllocOutputBuffer(handler);
if (buf == NULL)
return;
}
htmlDocContentDumpFormatOutput(buf, cur, NULL, format);
@ -1029,10 +1025,8 @@ htmlDocDump(FILE *f, xmlDocPtr cur) {
encoding = (const char *) htmlGetMetaEncoding(cur);
handler = htmlFindOutputEncoder(encoding);
buf = xmlOutputBufferCreateFile(f, handler);
if (buf == NULL) {
xmlCharEncCloseFunc(handler);
if (buf == NULL)
return(-1);
}
htmlDocContentDumpOutput(buf, cur, NULL);
ret = xmlOutputBufferClose(buf);
@ -1063,10 +1057,8 @@ htmlSaveFile(const char *filename, xmlDocPtr cur) {
encoding = (const char *) htmlGetMetaEncoding(cur);
handler = htmlFindOutputEncoder(encoding);
buf = xmlOutputBufferCreateFilename(filename, handler, cur->compression);
if (buf == NULL) {
xmlCharEncCloseFunc(handler);
if (buf == NULL)
return(0);
}
htmlDocContentDumpOutput(buf, cur, NULL);
@ -1107,10 +1099,8 @@ htmlSaveFileFormat(const char *filename, xmlDocPtr cur,
* save the content to a temp buffer.
*/
buf = xmlOutputBufferCreateFilename(filename, handler, 0);
if (buf == NULL) {
xmlCharEncCloseFunc(handler);
if (buf == NULL)
return(0);
}
htmlDocContentDumpFormatOutput(buf, cur, encoding, format);

View File

@ -32,8 +32,6 @@ xmlNewInputBufferMemory(const void *mem, size_t size, int flags,
xmlCharEncoding enc);
#ifdef LIBXML_OUTPUT_ENABLED
XML_HIDDEN xmlOutputBufferPtr
xmlAllocOutputBufferInternal(xmlCharEncodingHandlerPtr encoder);
XML_HIDDEN void
xmlOutputBufferWriteQuotedString(xmlOutputBufferPtr buf,
const xmlChar *string);

78
xmlIO.c
View File

@ -1237,6 +1237,8 @@ xmlAllocParserInputBuffer(xmlCharEncoding enc) {
*
* Create a buffered parser output
*
* Consumes @encoder even in error case.
*
* Returns the new parser output or NULL
*/
xmlOutputBufferPtr
@ -1245,11 +1247,13 @@ xmlAllocOutputBuffer(xmlCharEncodingHandlerPtr encoder) {
ret = (xmlOutputBufferPtr) xmlMalloc(sizeof(xmlOutputBuffer));
if (ret == NULL) {
xmlCharEncCloseFunc(encoder);
return(NULL);
}
memset(ret, 0, sizeof(xmlOutputBuffer));
ret->buffer = xmlBufCreate(MINLEN);
if (ret->buffer == NULL) {
xmlCharEncCloseFunc(encoder);
xmlFree(ret);
return(NULL);
}
@ -1258,8 +1262,7 @@ xmlAllocOutputBuffer(xmlCharEncodingHandlerPtr encoder) {
if (encoder != NULL) {
ret->conv = xmlBufCreate(MINLEN);
if (ret->conv == NULL) {
xmlBufFree(ret->buffer);
xmlFree(ret);
xmlOutputBufferClose(ret);
return(NULL);
}
@ -1276,53 +1279,6 @@ xmlAllocOutputBuffer(xmlCharEncodingHandlerPtr encoder) {
return(ret);
}
/**
* xmlAllocOutputBufferInternal:
* @encoder: the encoding converter or NULL
*
* Create a buffered parser output
*
* Returns the new parser output or NULL
*/
xmlOutputBufferPtr
xmlAllocOutputBufferInternal(xmlCharEncodingHandlerPtr encoder) {
xmlOutputBufferPtr ret;
ret = (xmlOutputBufferPtr) xmlMalloc(sizeof(xmlOutputBuffer));
if (ret == NULL) {
return(NULL);
}
memset(ret, 0, sizeof(xmlOutputBuffer));
ret->buffer = xmlBufCreate(MINLEN);
if (ret->buffer == NULL) {
xmlFree(ret);
return(NULL);
}
ret->encoder = encoder;
if (encoder != NULL) {
ret->conv = xmlBufCreate(MINLEN);
if (ret->conv == NULL) {
xmlBufFree(ret->buffer);
xmlFree(ret);
return(NULL);
}
/*
* This call is designed to initiate the encoder state
*/
xmlCharEncOutput(ret, 1);
} else
ret->conv = NULL;
ret->writecallback = NULL;
ret->closecallback = NULL;
ret->context = NULL;
ret->written = 0;
return(ret);
}
#endif /* LIBXML_OUTPUT_ENABLED */
/**
@ -1536,10 +1492,9 @@ __xmlOutputBufferCreateFilename(const char *URI,
/*
* Allocate the Output buffer front-end.
*/
ret = xmlAllocOutputBufferInternal(encoder);
ret = xmlAllocOutputBuffer(encoder);
if (ret == NULL) {
xmlFree(unescaped);
xmlCharEncCloseFunc(encoder);
return(NULL);
}
@ -1589,6 +1544,8 @@ __xmlOutputBufferCreateFilename(const char *URI,
* TODO: currently if compression is set, the library only support
* writing to a local file.
*
* Consumes @encoder even in error case.
*
* Returns the new output or NULL
*/
xmlOutputBufferPtr
@ -1643,6 +1600,8 @@ xmlParserInputBufferCreateFile(FILE *file, xmlCharEncoding enc) {
* Create a buffered output for the progressive saving to a FILE *
* buffered C I/O
*
* Consumes @encoder even in error case.
*
* Returns the new parser output or NULL
*/
xmlOutputBufferPtr
@ -1651,7 +1610,7 @@ xmlOutputBufferCreateFile(FILE *file, xmlCharEncodingHandlerPtr encoder) {
if (file == NULL) return(NULL);
ret = xmlAllocOutputBufferInternal(encoder);
ret = xmlAllocOutputBuffer(encoder);
if (ret != NULL) {
ret->context = file;
ret->writecallback = xmlFileWrite;
@ -1668,6 +1627,8 @@ xmlOutputBufferCreateFile(FILE *file, xmlCharEncodingHandlerPtr encoder) {
*
* Create a buffered output for the progressive saving to a xmlBuffer
*
* Consumes @encoder even in error case.
*
* Returns the new parser output or NULL
*/
xmlOutputBufferPtr
@ -1932,6 +1893,8 @@ xmlNewInputBufferString(const char *str, int flags) {
* Create a buffered output for the progressive saving
* to a file descriptor
*
* Consumes @encoder even in error case.
*
* Returns the new parser output or NULL
*/
xmlOutputBufferPtr
@ -1940,7 +1903,7 @@ xmlOutputBufferCreateFd(int fd, xmlCharEncodingHandlerPtr encoder) {
if (fd < 0) return(NULL);
ret = xmlAllocOutputBufferInternal(encoder);
ret = xmlAllocOutputBuffer(encoder);
if (ret != NULL) {
ret->context = (void *) (ptrdiff_t) fd;
ret->writecallback = xmlFdWrite;
@ -1997,6 +1960,8 @@ xmlParserInputBufferCreateIO(xmlInputReadCallback ioread,
* Create a buffered output for the progressive saving
* to an I/O handler
*
* Consumes @encoder even in error case.
*
* Returns the new parser output or NULL
*/
xmlOutputBufferPtr
@ -2005,9 +1970,12 @@ xmlOutputBufferCreateIO(xmlOutputWriteCallback iowrite,
xmlCharEncodingHandlerPtr encoder) {
xmlOutputBufferPtr ret;
if (iowrite == NULL) return(NULL);
if (iowrite == NULL) {
xmlCharEncCloseFunc(encoder);
return(NULL);
}
ret = xmlAllocOutputBufferInternal(encoder);
ret = xmlAllocOutputBuffer(encoder);
if (ret != NULL) {
ret->context = (void *) ioctx;
ret->writecallback = iowrite;

View File

@ -1960,7 +1960,6 @@ xmlSaveToFd(int fd, const char *encoding, int options)
if (ret == NULL) return(NULL);
ret->buf = xmlOutputBufferCreateFd(fd, ret->handler);
if (ret->buf == NULL) {
xmlCharEncCloseFunc(ret->handler);
xmlFreeSaveCtxt(ret);
return(NULL);
}
@ -1990,7 +1989,6 @@ xmlSaveToFilename(const char *filename, const char *encoding, int options)
ret->buf = xmlOutputBufferCreateFilename(filename, ret->handler,
compression);
if (ret->buf == NULL) {
xmlCharEncCloseFunc(ret->handler);
xmlFreeSaveCtxt(ret);
return(NULL);
}
@ -2018,7 +2016,6 @@ xmlSaveToBuffer(xmlBufferPtr buffer, const char *encoding, int options)
if (ret == NULL) return(NULL);
ret->buf = xmlOutputBufferCreateBuffer(buffer, ret->handler);
if (ret->buf == NULL) {
xmlCharEncCloseFunc(ret->handler);
xmlFreeSaveCtxt(ret);
return(NULL);
}
@ -2049,7 +2046,6 @@ xmlSaveToIO(xmlOutputWriteCallback iowrite,
if (ret == NULL) return(NULL);
ret->buf = xmlOutputBufferCreateIO(iowrite, ioclose, ioctx, ret->handler);
if (ret->buf == NULL) {
xmlCharEncCloseFunc(ret->handler);
xmlFreeSaveCtxt(ret);
return(NULL);
}
@ -2554,7 +2550,6 @@ xmlDocDumpFormatMemoryEnc(xmlDocPtr out_doc, xmlChar **doc_txt_ptr,
out_buff = xmlAllocOutputBuffer(conv_hdlr);
if (out_buff == NULL ) {
xmlSaveErrMemory(NULL);
xmlCharEncCloseFunc(conv_hdlr);
return;
}