Skip to content
This repository was archived by the owner on Jan 26, 2026. It is now read-only.

Commit 4365a5e

Browse files
committed
xmlreader: Fix xmlTextReaderConstEncoding
Regression from commit f1c1f5c. Fixes #697.
1 parent 0b56500 commit 4365a5e

4 files changed

Lines changed: 37 additions & 23 deletions

File tree

SAX2.c

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -955,17 +955,7 @@ xmlSAX2EndDocument(void *ctx)
955955

956956
doc = ctxt->myDoc;
957957
if ((doc != NULL) && (doc->encoding == NULL)) {
958-
const xmlChar *encoding = NULL;
959-
960-
if ((ctxt->input->flags & XML_INPUT_USES_ENC_DECL) ||
961-
(ctxt->input->flags & XML_INPUT_AUTO_ENCODING)) {
962-
/* Preserve encoding exactly */
963-
encoding = ctxt->encoding;
964-
} else if ((ctxt->input->buf) && (ctxt->input->buf->encoder)) {
965-
encoding = BAD_CAST ctxt->input->buf->encoder->name;
966-
} else if (ctxt->input->flags & XML_INPUT_HAS_ENCODING) {
967-
encoding = BAD_CAST "UTF-8";
968-
}
958+
const xmlChar *encoding = xmlGetActualEncoding(ctxt);
969959

970960
if (encoding != NULL) {
971961
doc->encoding = xmlStrdup(encoding);

include/private/parser.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ XML_HIDDEN void
4848
xmlDetectEncoding(xmlParserCtxtPtr ctxt);
4949
XML_HIDDEN void
5050
xmlSetDeclaredEncoding(xmlParserCtxtPtr ctxt, xmlChar *encoding);
51+
XML_HIDDEN const xmlChar *
52+
xmlGetActualEncoding(xmlParserCtxtPtr ctxt);
5153

5254
XML_HIDDEN xmlParserNsData *
5355
xmlParserNsCreate(void);

parserInternals.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1479,6 +1479,30 @@ xmlSetDeclaredEncoding(xmlParserCtxtPtr ctxt, xmlChar *encoding) {
14791479
}
14801480
}
14811481

1482+
/**
1483+
* xmlGetActualEncoding:
1484+
* @ctxt: the parser context
1485+
*
1486+
* Returns the actual used to parse the document. This can differ from
1487+
* the declared encoding.
1488+
*/
1489+
const xmlChar *
1490+
xmlGetActualEncoding(xmlParserCtxtPtr ctxt) {
1491+
const xmlChar *encoding = NULL;
1492+
1493+
if ((ctxt->input->flags & XML_INPUT_USES_ENC_DECL) ||
1494+
(ctxt->input->flags & XML_INPUT_AUTO_ENCODING)) {
1495+
/* Preserve encoding exactly */
1496+
encoding = ctxt->encoding;
1497+
} else if ((ctxt->input->buf) && (ctxt->input->buf->encoder)) {
1498+
encoding = BAD_CAST ctxt->input->buf->encoder->name;
1499+
} else if (ctxt->input->flags & XML_INPUT_HAS_ENCODING) {
1500+
encoding = BAD_CAST "UTF-8";
1501+
}
1502+
1503+
return(encoding);
1504+
}
1505+
14821506
/************************************************************************
14831507
* *
14841508
* Commodity functions to handle entities processing *

xmlreader.c

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#endif
4141

4242
#include "private/buf.h"
43+
#include "private/parser.h"
4344
#include "private/tree.h"
4445
#ifdef LIBXML_XINCLUDE_ENABLED
4546
#include "private/xinclude.h"
@@ -2795,20 +2796,17 @@ xmlTextReaderReadAttributeValue(xmlTextReaderPtr reader) {
27952796
*/
27962797
const xmlChar *
27972798
xmlTextReaderConstEncoding(xmlTextReaderPtr reader) {
2798-
xmlDocPtr doc = NULL;
2799+
const xmlChar *encoding = NULL;
2800+
27992801
if (reader == NULL)
2800-
return(NULL);
2801-
if (reader->doc != NULL)
2802-
doc = reader->doc;
2803-
else if (reader->ctxt != NULL)
2804-
doc = reader->ctxt->myDoc;
2805-
if (doc == NULL)
2806-
return(NULL);
2802+
return(NULL);
28072803

2808-
if (doc->encoding == NULL)
2809-
return(NULL);
2810-
else
2811-
return(CONSTSTR(doc->encoding));
2804+
if (reader->ctxt != NULL)
2805+
encoding = xmlGetActualEncoding(reader->ctxt);
2806+
else if (reader->doc != NULL)
2807+
encoding = reader->doc->encoding;
2808+
2809+
return(CONSTSTR(encoding));
28122810
}
28132811

28142812

0 commit comments

Comments
 (0)