1
0
mirror of https://gitlab.gnome.org/GNOME/libxml2.git synced 2025-03-09 04:58:16 +03:00

io: Rearrange code in xmlSwitchInputEncodingInt

No functional change.
This commit is contained in:
Nick Wellnhofer 2022-11-13 19:44:00 +01:00
parent 1ef4938fd0
commit 6b57061909

View File

@ -1021,32 +1021,35 @@ xmlSwitchInputEncodingInt(xmlParserCtxtPtr ctxt, xmlParserInputPtr input,
xmlCharEncodingHandlerPtr handler, int len)
{
int nbchars;
xmlParserInputBufferPtr in;
if (handler == NULL)
return (-1);
if (input == NULL)
return (-1);
if (input->buf != NULL) {
in = input->buf;
if (in == NULL) {
xmlErrInternal(ctxt,
"static memory buffer doesn't support encoding\n", NULL);
/*
* Callers assume that the input buffer takes ownership of the
* encoding handler. xmlCharEncCloseFunc frees unregistered
* handlers and avoids a memory leak.
*/
xmlCharEncCloseFunc(handler);
return (-1);
}
ctxt->charset = XML_CHAR_ENCODING_UTF8;
if (input->buf->encoder != NULL) {
if (in->encoder != NULL) {
/*
* Check in case the auto encoding detection triggered
* in already.
*/
if (input->buf->encoder == handler)
if (in->encoder == handler)
return (0);
/*
* "UTF-16" can be used for both LE and BE
if ((!xmlStrncmp(BAD_CAST input->buf->encoder->name,
BAD_CAST "UTF-16", 6)) &&
(!xmlStrncmp(BAD_CAST handler->name,
BAD_CAST "UTF-16", 6))) {
return(0);
}
*/
/*
* Note: this is a bit dangerous, but that's what it
* takes to use nearly compatible signature for different
@ -1057,16 +1060,16 @@ xmlSwitchInputEncodingInt(xmlParserCtxtPtr ctxt, xmlParserInputPtr input,
* make sure that callers never try to switch the encoding
* twice.
*/
xmlCharEncCloseFunc(input->buf->encoder);
input->buf->encoder = handler;
xmlCharEncCloseFunc(in->encoder);
in->encoder = handler;
return (0);
}
input->buf->encoder = handler;
in->encoder = handler;
/*
* Is there already some content down the pipe to convert ?
*/
if (xmlBufIsEmpty(input->buf->buffer) == 0) {
if (xmlBufIsEmpty(in->buffer) == 0) {
int processed;
unsigned int use;
@ -1102,17 +1105,17 @@ xmlSwitchInputEncodingInt(xmlParserCtxtPtr ctxt, xmlParserInputPtr input,
* Move it as the raw buffer and create a new input buffer
*/
processed = input->cur - input->base;
xmlBufShrink(input->buf->buffer, processed);
input->buf->raw = input->buf->buffer;
input->buf->buffer = xmlBufCreate();
input->buf->rawconsumed = processed;
use = xmlBufUse(input->buf->raw);
xmlBufShrink(in->buffer, processed);
in->raw = in->buffer;
in->buffer = xmlBufCreate();
in->rawconsumed = processed;
use = xmlBufUse(in->raw);
if (ctxt->html) {
/*
* convert as much as possible of the buffer
*/
nbchars = xmlCharEncInput(input->buf, 1);
nbchars = xmlCharEncInput(in, 1);
} else {
/*
* convert just enough to get
@ -1120,29 +1123,18 @@ xmlSwitchInputEncodingInt(xmlParserCtxtPtr ctxt, xmlParserInputPtr input,
* parsed with the autodetected encoding
* into the parser reading buffer.
*/
nbchars = xmlCharEncFirstLineInput(input->buf, len);
nbchars = xmlCharEncFirstLineInput(in, len);
}
xmlBufResetInput(input->buf->buffer, input);
xmlBufResetInput(in->buffer, input);
if (nbchars < 0) {
xmlErrInternal(ctxt,
"switching encoding: encoder error\n",
NULL);
return (-1);
}
input->buf->rawconsumed += use - xmlBufUse(input->buf->raw);
in->rawconsumed += use - xmlBufUse(in->raw);
}
return (0);
} else {
xmlErrInternal(ctxt,
"static memory buffer doesn't support encoding\n", NULL);
/*
* Callers assume that the input buffer takes ownership of the
* encoding handler. xmlCharEncCloseFunc frees unregistered
* handlers and avoids a memory leak.
*/
xmlCharEncCloseFunc(handler);
return (-1);
}
}
/**