--- a/frontends/xml/vcd_xml_common.c 2018-01-03 21:17:37.000000000 +0100 +++ b/frontends/xml/vcd_xml_common.c 2026-10-19 17:17:09.245370091 +0200 @@ -222,48 +222,42 @@ void vcd_xml_print_version (void) static char * _convert (const char in[], const char encoding[], bool from) { - unsigned char *out; - int ret, size, out_size, temp; + xmlBufferPtr inbuf, outbuf; + char *out = 0; + int ret; xmlCharEncodingHandlerPtr handler = 0; if (!in) return 0; + if (xmlParseCharEncoding (encoding) == XML_CHAR_ENCODING_UTF8) + return strdup (in); + if (!(handler = xmlFindCharEncodingHandler (encoding))) { vcd_error ("could not find charset conversion handler for '%s'", encoding); return 0; } - size = (int) strlen (in) + 1; - out_size = size * 2 - 1; - out = malloc ((size_t) out_size); - - vcd_assert (out != 0); - - temp = size - 1; - if (from) { - if (NULL != handler->output) - ret = handler->output (out, &out_size, (const unsigned char *) in, &temp); - else - return strdup(in); - } else { - if (NULL != handler->input) - ret = handler->input (out, &out_size, (const unsigned char *) in, &temp); - else - return strdup(in); - } - - if (ret < 0 || (temp - size + 1)) - { - free (out); - vcd_error ("charset conversion failed for encoding '%s'", encoding); - return 0; - } - - out = realloc (out, out_size + 1); - out[out_size] = 0; + inbuf = xmlBufferCreate (); + outbuf = xmlBufferCreate (); + vcd_assert (inbuf != 0 && outbuf != 0); + xmlBufferCat (inbuf, (const xmlChar *) in); + + if (from) + ret = xmlCharEncOutFunc (handler, outbuf, inbuf); + else + ret = xmlCharEncInFunc (handler, outbuf, inbuf); + + if (ret < 0 || xmlBufferLength (inbuf)) + vcd_error ("charset conversion failed for encoding '%s'", encoding); + else + out = strdup ((const char *) xmlBufferContent (outbuf)); + + xmlBufferFree (inbuf); + xmlBufferFree (outbuf); + xmlCharEncCloseFunc (handler); - return (char *) out; + return out; }