1
0
mirror of https://gitlab.gnome.org/GNOME/libxml2.git synced 2025-03-24 06:50:08 +03:00

xmlreader: Fix xmlTextReaderConstEncoding

Regression from commit f1c1f5c6.

Fixes #697.
This commit is contained in:
Nick Wellnhofer 2024-02-26 15:14:28 +01:00
parent b55ee729de
commit 84a71860a8
5 changed files with 76 additions and 26 deletions

12
SAX2.c
View File

@ -846,17 +846,7 @@ xmlSAX2EndDocument(void *ctx)
doc = ctxt->myDoc;
if ((doc != NULL) && (doc->encoding == NULL)) {
const xmlChar *encoding = NULL;
if ((ctxt->input->flags & XML_INPUT_USES_ENC_DECL) ||
(ctxt->input->flags & XML_INPUT_AUTO_ENCODING)) {
/* Preserve encoding exactly */
encoding = ctxt->encoding;
} else if ((ctxt->input->buf) && (ctxt->input->buf->encoder)) {
encoding = BAD_CAST ctxt->input->buf->encoder->name;
} else if (ctxt->input->flags & XML_INPUT_HAS_ENCODING) {
encoding = BAD_CAST "UTF-8";
}
const xmlChar *encoding = xmlGetActualEncoding(ctxt);
if (encoding != NULL) {
doc->encoding = xmlStrdup(encoding);

View File

@ -68,6 +68,8 @@ XML_HIDDEN void
xmlDetectEncoding(xmlParserCtxtPtr ctxt);
XML_HIDDEN void
xmlSetDeclaredEncoding(xmlParserCtxtPtr ctxt, xmlChar *encoding);
XML_HIDDEN const xmlChar *
xmlGetActualEncoding(xmlParserCtxtPtr ctxt);
XML_HIDDEN xmlParserNsData *
xmlParserNsCreate(void);

View File

@ -1398,6 +1398,30 @@ xmlSetDeclaredEncoding(xmlParserCtxtPtr ctxt, xmlChar *encoding) {
ctxt->encoding = encoding;
}
/**
* xmlGetActualEncoding:
* @ctxt: the parser context
*
* Returns the actual used to parse the document. This can differ from
* the declared encoding.
*/
const xmlChar *
xmlGetActualEncoding(xmlParserCtxtPtr ctxt) {
const xmlChar *encoding = NULL;
if ((ctxt->input->flags & XML_INPUT_USES_ENC_DECL) ||
(ctxt->input->flags & XML_INPUT_AUTO_ENCODING)) {
/* Preserve encoding exactly */
encoding = ctxt->encoding;
} else if ((ctxt->input->buf) && (ctxt->input->buf->encoder)) {
encoding = BAD_CAST ctxt->input->buf->encoder->name;
} else if (ctxt->input->flags & XML_INPUT_HAS_ENCODING) {
encoding = BAD_CAST "UTF-8";
}
return(encoding);
}
/************************************************************************
* *
* Commodity functions to handle entities processing *

View File

@ -176,7 +176,40 @@ testHtmlPushWithEncoding(void) {
#endif
#endif
#if defined(LIBXML_READER_ENABLED) && defined(LIBXML_XINCLUDE_ENABLED)
#ifdef LIBXML_READER_ENABLED
static int
testReaderEncoding(void) {
xmlBuffer *buf;
xmlTextReader *reader;
xmlChar *xml;
const xmlChar *encoding;
int err = 0;
int i;
buf = xmlBufferCreate();
xmlBufferCCat(buf, "<?xml version='1.0' encoding='ISO-8859-1'?>\n");
xmlBufferCCat(buf, "<doc>");
for (i = 0; i < 8192; i++)
xmlBufferCCat(buf, "x");
xmlBufferCCat(buf, "</doc>");
xml = xmlBufferDetach(buf);
xmlBufferFree(buf);
reader = xmlReaderForDoc(BAD_CAST xml, NULL, NULL, 0);
xmlTextReaderRead(reader);
encoding = xmlTextReaderConstEncoding(reader);
if (!xmlStrEqual(encoding, BAD_CAST "ISO-8859-1")) {
fprintf(stderr, "testReaderEncoding failed\n");
err = 1;
}
xmlFreeTextReader(reader);
xmlFree(xml);
return err;
}
#ifdef LIBXML_XINCLUDE_ENABLED
typedef struct {
char *message;
int code;
@ -254,6 +287,7 @@ testReaderXIncludeError(void) {
return err;
}
#endif
#endif
#ifdef LIBXML_WRITER_ENABLED
static int
@ -314,9 +348,12 @@ main(void) {
err |= testHtmlPushWithEncoding();
#endif
#endif
#if defined(LIBXML_READER_ENABLED) && defined(LIBXML_XINCLUDE_ENABLED)
#ifdef LIBXML_READER_ENABLED
err |= testReaderEncoding();
#ifdef LIBXML_XINCLUDE_ENABLED
err |= testReaderXIncludeError();
#endif
#endif
#ifdef LIBXML_WRITER_ENABLED
err |= testWriterClose();
#endif

View File

@ -2866,20 +2866,17 @@ xmlTextReaderReadAttributeValue(xmlTextReaderPtr reader) {
*/
const xmlChar *
xmlTextReaderConstEncoding(xmlTextReaderPtr reader) {
xmlDocPtr doc = NULL;
if (reader == NULL)
return(NULL);
if (reader->doc != NULL)
doc = reader->doc;
else if (reader->ctxt != NULL)
doc = reader->ctxt->myDoc;
if (doc == NULL)
return(NULL);
const xmlChar *encoding = NULL;
if (doc->encoding == NULL)
return(NULL);
else
return(CONSTSTR(doc->encoding));
if (reader == NULL)
return(NULL);
if (reader->ctxt != NULL)
encoding = xmlGetActualEncoding(reader->ctxt);
else if (reader->doc != NULL)
encoding = reader->doc->encoding;
return(CONSTSTR(encoding));
}