summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHeikki Linnakangas2009-09-04 10:49:29 +0000
committerHeikki Linnakangas2009-09-04 10:49:29 +0000
commitf934cf1e3cc3bb5af2a9539e906531cfa4b7603a (patch)
tree420828376256bec25afea58ee0d99475ac21e979
parentf6d6ba00beb4a95777350954f0e0f6715fbc7325 (diff)
Fix encoding handling in xml binary input function. If the XML header didn't
specify an encoding explicitly, we used to treat it as being in database encoding when we parsed it, but then perform a UTF-8 -> database encoding conversion on it, which was completely bogus. It's now consistently treated as UTF-8.
-rw-r--r--src/backend/utils/adt/xml.c33
1 files changed, 20 insertions, 13 deletions
diff --git a/src/backend/utils/adt/xml.c b/src/backend/utils/adt/xml.c
index 0a38d37f23..bb0d083298 100644
--- a/src/backend/utils/adt/xml.c
+++ b/src/backend/utils/adt/xml.c
@@ -109,7 +109,7 @@ static int parse_xml_decl(const xmlChar *str, size_t *lenp,
static bool print_xml_decl(StringInfo buf, const xmlChar *version,
pg_enc encoding, int standalone);
static xmlDocPtr xml_parse(text *data, XmlOptionType xmloption_arg,
- bool preserve_whitespace, xmlChar *encoding);
+ bool preserve_whitespace, int encoding);
static text *xml_xmlnodetoxmltype(xmlNodePtr cur);
#endif /* USE_LIBXML */
@@ -183,7 +183,7 @@ xml_in(PG_FUNCTION_ARGS)
* Parse the data to check if it is well-formed XML data. Assume that
* ERROR occurred if parsing failed.
*/
- doc = xml_parse(vardata, xmloption, true, NULL);
+ doc = xml_parse(vardata, xmloption, true, GetDatabaseEncoding());
xmlFreeDoc(doc);
PG_RETURN_XML_P(vardata);
@@ -272,7 +272,8 @@ xml_recv(PG_FUNCTION_ARGS)
char *newstr;
int nbytes;
xmlDocPtr doc;
- xmlChar *encoding = NULL;
+ xmlChar *encodingStr = NULL;
+ int encoding;
/*
* Read the data in raw format. We don't know yet what the encoding is, as
@@ -293,7 +294,15 @@ xml_recv(PG_FUNCTION_ARGS)
str = VARDATA(result);
str[nbytes] = '\0';
- parse_xml_decl((xmlChar *) str, NULL, NULL, &encoding, NULL);
+ parse_xml_decl((xmlChar *) str, NULL, NULL, &encodingStr, NULL);
+
+ /*
+ * If encoding wasn't explicitly specified in the XML header, treat it as
+ * UTF-8, as that's the default in XML. This is different from xml_in(),
+ * where the input has to go through the normal client to server encoding
+ * conversion.
+ */
+ encoding = encodingStr ? xmlChar_to_encoding(encodingStr) : PG_UTF8;
/*
* Parse the data to check if it is well-formed XML data. Assume that
@@ -305,9 +314,7 @@ xml_recv(PG_FUNCTION_ARGS)
/* Now that we know what we're dealing with, convert to server encoding */
newstr = (char *) pg_do_encoding_conversion((unsigned char *) str,
nbytes,
- encoding ?
- xmlChar_to_encoding(encoding) :
- PG_UTF8,
+ encoding,
GetDatabaseEncoding());
if (newstr != str)
@@ -659,7 +666,8 @@ xmlparse(text *data, XmlOptionType xmloption_arg, bool preserve_whitespace)
#ifdef USE_LIBXML
xmlDocPtr doc;
- doc = xml_parse(data, xmloption_arg, preserve_whitespace, NULL);
+ doc = xml_parse(data, xmloption_arg, preserve_whitespace,
+ GetDatabaseEncoding());
xmlFreeDoc(doc);
return (xmltype *) data;
@@ -799,7 +807,8 @@ xml_is_document(xmltype *arg)
/* We want to catch ereport(INVALID_XML_DOCUMENT) and return false */
PG_TRY();
{
- doc = xml_parse((text *) arg, XMLOPTION_DOCUMENT, true, NULL);
+ doc = xml_parse((text *) arg, XMLOPTION_DOCUMENT, true,
+ GetDatabaseEncoding());
result = true;
}
PG_CATCH();
@@ -1152,7 +1161,7 @@ print_xml_decl(StringInfo buf, const xmlChar *version,
*/
static xmlDocPtr
xml_parse(text *data, XmlOptionType xmloption_arg, bool preserve_whitespace,
- xmlChar *encoding)
+ int encoding)
{
int32 len;
xmlChar *string;
@@ -1165,9 +1174,7 @@ xml_parse(text *data, XmlOptionType xmloption_arg, bool preserve_whitespace,
utf8string = pg_do_encoding_conversion(string,
len,
- encoding ?
- xmlChar_to_encoding(encoding) :
- GetDatabaseEncoding(),
+ encoding,
PG_UTF8);
/* Start up libxml and its parser (no-ops if already done) */