Skip to content

Commit 8d0aaf4

Browse files
committed
parser: Remove xmlErrEncoding
Use xmlFatalErr or xmlCtxtErrIO.
1 parent 9fbe46b commit 8d0aaf4

File tree

13 files changed

+66
-113
lines changed

13 files changed

+66
-113
lines changed

HTMLparser.c

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -490,20 +490,7 @@ htmlCurrentChar(xmlParserCtxtPtr ctxt, int *len) {
490490
}
491491

492492
encoding_error:
493-
{
494-
char buffer[150];
495-
496-
if (ctxt->input->end - ctxt->input->cur >= 4) {
497-
snprintf(buffer, 149, "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n",
498-
ctxt->input->cur[0], ctxt->input->cur[1],
499-
ctxt->input->cur[2], ctxt->input->cur[3]);
500-
} else {
501-
snprintf(buffer, 149, "Bytes: 0x%02X\n", ctxt->input->cur[0]);
502-
}
503-
htmlParseErr(ctxt, XML_ERR_INVALID_ENCODING,
504-
"Input is not proper UTF-8, indicate encoding !\n",
505-
BAD_CAST buffer, NULL);
506-
}
493+
xmlCtxtErrIO(ctxt, XML_ERR_INVALID_ENCODING, NULL);
507494

508495
if ((ctxt->input->flags & XML_INPUT_HAS_ENCODING) == 0)
509496
xmlSwitchEncoding(ctxt, XML_CHAR_ENCODING_8859_1);

error.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,23 @@ xmlReportError(xmlParserCtxtPtr ctxt, const xmlError *err)
543543
}
544544

545545
if (ctxt != NULL) {
546+
if ((input != NULL) &&
547+
((input->buf == NULL) || (input->buf->encoder == NULL)) &&
548+
(code == XML_ERR_INVALID_ENCODING) &&
549+
(input->cur < input->end)) {
550+
int i;
551+
552+
channel(data, "Bytes:");
553+
for (i = 0; i < 4; i++) {
554+
if (input->cur + i >= input->end)
555+
break;
556+
channel(data, " 0x%02X", input->cur[i]);
557+
}
558+
channel(data, "\n");
559+
}
560+
546561
xmlParserPrintFileContextInternal(input, channel, data);
562+
547563
if (cur != NULL) {
548564
if (cur->filename)
549565
channel(data, "%s:%d: \n", cur->filename, cur->line);

include/private/parser.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,6 @@ XML_HIDDEN void LIBXML_ATTR_FORMAT(3,0)
4646
xmlWarningMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error,
4747
const char *msg, const xmlChar *str1, const xmlChar *str2);
4848
XML_HIDDEN void
49-
__xmlErrEncoding(xmlParserCtxtPtr ctxt, xmlParserErrors xmlerr,
50-
const char *msg, const xmlChar *str1,
51-
const xmlChar *str2) LIBXML_ATTR_FORMAT(3,0);
52-
XML_HIDDEN void
5349
xmlHaltParser(xmlParserCtxtPtr ctxt);
5450
XML_HIDDEN int
5551
xmlParserGrow(xmlParserCtxtPtr ctxt);

parser.c

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11877,19 +11877,10 @@ xmlParseTryOrFinish(xmlParserCtxtPtr ctxt, int terminate) {
1187711877
done:
1187811878
return(ret);
1187911879
encoding_error:
11880-
if (ctxt->input->end - ctxt->input->cur < 4) {
11881-
__xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR,
11882-
"Input is not proper UTF-8, indicate encoding !\n",
11883-
NULL, NULL);
11884-
} else {
11885-
char buffer[150];
11886-
11887-
snprintf(buffer, 149, "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n",
11888-
ctxt->input->cur[0], ctxt->input->cur[1],
11889-
ctxt->input->cur[2], ctxt->input->cur[3]);
11890-
__xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR,
11891-
"Input is not proper UTF-8, indicate encoding !\n%s",
11892-
BAD_CAST buffer, NULL);
11880+
/* Only report the first error */
11881+
if ((ctxt->input->flags & XML_INPUT_ENCODING_ERROR) == 0) {
11882+
xmlCtxtErrIO(ctxt, XML_ERR_INVALID_ENCODING, NULL);
11883+
ctxt->input->flags |= XML_INPUT_ENCODING_ERROR;
1189311884
}
1189411885
return(0);
1189511886
}

parserInternals.c

Lines changed: 9 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -245,24 +245,6 @@ xmlErrParser(xmlParserCtxtPtr ctxt, xmlNodePtr node,
245245
va_end(ap);
246246
}
247247

248-
/**
249-
* __xmlErrEncoding:
250-
* @ctxt: an XML parser context
251-
* @xmlerr: the error number
252-
* @msg: the error message
253-
* @str1: an string info
254-
* @str2: an string info
255-
*
256-
* Handle an encoding error
257-
*/
258-
void
259-
__xmlErrEncoding(xmlParserCtxtPtr ctxt, xmlParserErrors xmlerr,
260-
const char *msg, const xmlChar * str1, const xmlChar * str2)
261-
{
262-
xmlErrParser(ctxt, NULL, XML_FROM_PARSER, xmlerr, XML_ERR_FATAL,
263-
str1, str2, NULL, 0, msg, str1, str2);
264-
}
265-
266248
/**
267249
* xmlErrInternal:
268250
* @ctxt: an XML parser context
@@ -659,21 +641,7 @@ xmlNextChar(xmlParserCtxtPtr ctxt)
659641
encoding_error:
660642
/* Only report the first error */
661643
if ((ctxt->input->flags & XML_INPUT_ENCODING_ERROR) == 0) {
662-
if ((ctxt == NULL) || (ctxt->input == NULL) ||
663-
(ctxt->input->end - ctxt->input->cur < 4)) {
664-
__xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR,
665-
"Input is not proper UTF-8, indicate encoding !\n",
666-
NULL, NULL);
667-
} else {
668-
char buffer[150];
669-
670-
snprintf(buffer, 149, "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n",
671-
ctxt->input->cur[0], ctxt->input->cur[1],
672-
ctxt->input->cur[2], ctxt->input->cur[3]);
673-
__xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR,
674-
"Input is not proper UTF-8, indicate encoding !\n%s",
675-
BAD_CAST buffer, NULL);
676-
}
644+
xmlCtxtErrIO(ctxt, XML_ERR_INVALID_ENCODING, NULL);
677645
ctxt->input->flags |= XML_INPUT_ENCODING_ERROR;
678646
}
679647
ctxt->input->cur++;
@@ -809,20 +777,7 @@ xmlCurrentChar(xmlParserCtxtPtr ctxt, int *len) {
809777
encoding_error:
810778
/* Only report the first error */
811779
if ((ctxt->input->flags & XML_INPUT_ENCODING_ERROR) == 0) {
812-
if (ctxt->input->end - ctxt->input->cur < 4) {
813-
__xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR,
814-
"Input is not proper UTF-8, indicate encoding !\n",
815-
NULL, NULL);
816-
} else {
817-
char buffer[150];
818-
819-
snprintf(&buffer[0], 149, "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n",
820-
ctxt->input->cur[0], ctxt->input->cur[1],
821-
ctxt->input->cur[2], ctxt->input->cur[3]);
822-
__xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR,
823-
"Input is not proper UTF-8, indicate encoding !\n%s",
824-
BAD_CAST buffer, NULL);
825-
}
780+
xmlCtxtErrIO(ctxt, XML_ERR_INVALID_ENCODING, NULL);
826781
ctxt->input->flags |= XML_INPUT_ENCODING_ERROR;
827782
}
828783
*len = 1;
@@ -1050,14 +1005,9 @@ xmlSwitchEncoding(xmlParserCtxtPtr ctxt, xmlCharEncoding enc)
10501005
}
10511006

10521007
if (res != 0) {
1053-
if (res == XML_ERR_UNSUPPORTED_ENCODING) {
1054-
const char *name = xmlGetCharEncodingName(enc);
1008+
const char *name = xmlGetCharEncodingName(enc);
10551009

1056-
__xmlErrEncoding(ctxt, res, "encoding not supported: %s\n",
1057-
BAD_CAST (name ? name : "<null>"), NULL);
1058-
} else {
1059-
xmlFatalErr(ctxt, res, NULL);
1060-
}
1010+
xmlFatalErr(ctxt, res, (name ? name : "<null>"));
10611011
return(-1);
10621012
}
10631013

@@ -1090,13 +1040,12 @@ xmlSwitchEncodingName(xmlParserCtxtPtr ctxt, const char *encoding) {
10901040
xmlCharEncodingHandlerPtr handler;
10911041
int res;
10921042

1043+
if (encoding == NULL)
1044+
return(-1);
1045+
10931046
res = xmlOpenCharEncodingHandler(encoding, &handler);
10941047
if (res != 0) {
1095-
if (res == XML_ERR_UNSUPPORTED_ENCODING)
1096-
__xmlErrEncoding(ctxt, res, "Unsupported encoding: %s\n",
1097-
(const xmlChar *) encoding, NULL);
1098-
else
1099-
xmlFatalErr(ctxt, res, NULL);
1048+
xmlFatalErr(ctxt, res, encoding);
11001049
return(-1);
11011050
}
11021051

@@ -1188,9 +1137,7 @@ xmlSwitchInputEncoding(xmlParserCtxtPtr ctxt, xmlParserInputPtr input,
11881137
if (nbchars == XML_ENC_ERR_MEMORY) {
11891138
xmlErrMemory(ctxt);
11901139
} else if (nbchars < 0) {
1191-
xmlErrInternal(ctxt,
1192-
"switching encoding: encoder error\n",
1193-
NULL);
1140+
xmlCtxtErrIO(ctxt, in->error, NULL);
11941141
xmlHaltParser(ctxt);
11951142
return (-1);
11961143
}

result/errors/754947.xml.ent

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
./test/errors/754947.xml:1: parser error : Input is not proper UTF-8, indicate encoding !
1+
./test/errors/754947.xml:1: I/O error : Invalid bytes in character encoding
22
Bytes: 0xEE 0x5D 0x5D 0x3E
33
<d><![CDATA[0000000000000
44
^

result/errors/754947.xml.err

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
./test/errors/754947.xml:1: parser error : Input is not proper UTF-8, indicate encoding !
1+
./test/errors/754947.xml:1: I/O error : Invalid bytes in character encoding
22
Bytes: 0xEE 0x5D 0x5D 0x3E
33
<d><![CDATA[0000000000000
44
^

result/errors/754947.xml.str

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
./test/errors/754947.xml:1: parser error : Input is not proper UTF-8, indicate encoding !
1+
./test/errors/754947.xml:1: I/O error : Invalid bytes in character encoding
22
Bytes: 0xEE 0x5D 0x5D 0x3E
33
<d><![CDATA[0000000000000
44
^

result/errors/cdata.xml.ent

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
./test/errors/cdata.xml:2: parser error : Input is not proper UTF-8, indicate encoding !
1+
./test/errors/cdata.xml:2: I/O error : Invalid bytes in character encoding
22
Bytes: 0xE1 0x72 0x5D 0x5D
33
<A><![CDATA[C
44
^

result/errors/cdata.xml.err

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
./test/errors/cdata.xml:2: parser error : Input is not proper UTF-8, indicate encoding !
1+
./test/errors/cdata.xml:2: I/O error : Invalid bytes in character encoding
22
Bytes: 0xE1 0x72 0x5D 0x5D
33
<A><![CDATA[C
44
^

0 commit comments

Comments
 (0)