LCOV - code coverage report
Current view: top level - src/backend/utils/adt - xml.c (source / functions) Hit Total Coverage
Test: PostgreSQL 19devel Lines: 1442 1754 82.2 %
Date: 2025-07-30 23:18:15 Functions: 92 105 87.6 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * xml.c
       4             :  *    XML data type support.
       5             :  *
       6             :  *
       7             :  * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
       8             :  * Portions Copyright (c) 1994, Regents of the University of California
       9             :  *
      10             :  * src/backend/utils/adt/xml.c
      11             :  *
      12             :  *-------------------------------------------------------------------------
      13             :  */
      14             : 
      15             : /*
      16             :  * Generally, XML type support is only available when libxml use was
      17             :  * configured during the build.  But even if that is not done, the
      18             :  * type and all the functions are available, but most of them will
      19             :  * fail.  For one thing, this avoids having to manage variant catalog
      20             :  * installations.  But it also has nice effects such as that you can
      21             :  * dump a database containing XML type data even if the server is not
      22             :  * linked with libxml.  Thus, make sure xml_out() works even if nothing
      23             :  * else does.
      24             :  */
      25             : 
      26             : /*
      27             :  * Notes on memory management:
      28             :  *
      29             :  * Sometimes libxml allocates global structures in the hope that it can reuse
      30             :  * them later on.  This makes it impractical to change the xmlMemSetup
      31             :  * functions on-the-fly; that is likely to lead to trying to pfree() chunks
      32             :  * allocated with malloc() or vice versa.  Since libxml might be used by
      33             :  * loadable modules, eg libperl, our only safe choices are to change the
      34             :  * functions at postmaster/backend launch or not at all.  Since we'd rather
      35             :  * not activate libxml in sessions that might never use it, the latter choice
      36             :  * is the preferred one.  However, for debugging purposes it can be awfully
      37             :  * handy to constrain libxml's allocations to be done in a specific palloc
      38             :  * context, where they're easy to track.  Therefore there is code here that
      39             :  * can be enabled in debug builds to redirect libxml's allocations into a
      40             :  * special context LibxmlContext.  It's not recommended to turn this on in
      41             :  * a production build because of the possibility of bad interactions with
      42             :  * external modules.
      43             :  */
      44             : /* #define USE_LIBXMLCONTEXT */
      45             : 
      46             : #include "postgres.h"
      47             : 
      48             : #ifdef USE_LIBXML
      49             : #include <libxml/chvalid.h>
      50             : #include <libxml/entities.h>
      51             : #include <libxml/parser.h>
      52             : #include <libxml/parserInternals.h>
      53             : #include <libxml/tree.h>
      54             : #include <libxml/uri.h>
      55             : #include <libxml/xmlerror.h>
      56             : #include <libxml/xmlsave.h>
      57             : #include <libxml/xmlversion.h>
      58             : #include <libxml/xmlwriter.h>
      59             : #include <libxml/xpath.h>
      60             : #include <libxml/xpathInternals.h>
      61             : 
      62             : /*
      63             :  * We used to check for xmlStructuredErrorContext via a configure test; but
      64             :  * that doesn't work on Windows, so instead use this grottier method of
      65             :  * testing the library version number.
      66             :  */
      67             : #if LIBXML_VERSION >= 20704
      68             : #define HAVE_XMLSTRUCTUREDERRORCONTEXT 1
      69             : #endif
      70             : 
      71             : /*
      72             :  * libxml2 2.12 decided to insert "const" into the error handler API.
      73             :  */
      74             : #if LIBXML_VERSION >= 21200
      75             : #define PgXmlErrorPtr const xmlError *
      76             : #else
      77             : #define PgXmlErrorPtr xmlErrorPtr
      78             : #endif
      79             : 
      80             : #endif                          /* USE_LIBXML */
      81             : 
      82             : #include "access/htup_details.h"
      83             : #include "access/table.h"
      84             : #include "catalog/namespace.h"
      85             : #include "catalog/pg_class.h"
      86             : #include "catalog/pg_type.h"
      87             : #include "commands/dbcommands.h"
      88             : #include "executor/spi.h"
      89             : #include "executor/tablefunc.h"
      90             : #include "fmgr.h"
      91             : #include "lib/stringinfo.h"
      92             : #include "libpq/pqformat.h"
      93             : #include "mb/pg_wchar.h"
      94             : #include "miscadmin.h"
      95             : #include "nodes/execnodes.h"
      96             : #include "nodes/miscnodes.h"
      97             : #include "nodes/nodeFuncs.h"
      98             : #include "utils/array.h"
      99             : #include "utils/builtins.h"
     100             : #include "utils/date.h"
     101             : #include "utils/datetime.h"
     102             : #include "utils/lsyscache.h"
     103             : #include "utils/rel.h"
     104             : #include "utils/syscache.h"
     105             : #include "utils/xml.h"
     106             : 
     107             : 
     108             : /* GUC variables */
     109             : int         xmlbinary = XMLBINARY_BASE64;
     110             : int         xmloption = XMLOPTION_CONTENT;
     111             : 
     112             : #ifdef USE_LIBXML
     113             : 
     114             : /* random number to identify PgXmlErrorContext */
     115             : #define ERRCXT_MAGIC    68275028
     116             : 
     117             : struct PgXmlErrorContext
     118             : {
     119             :     int         magic;
     120             :     /* strictness argument passed to pg_xml_init */
     121             :     PgXmlStrictness strictness;
     122             :     /* current error status and accumulated message, if any */
     123             :     bool        err_occurred;
     124             :     StringInfoData err_buf;
     125             :     /* previous libxml error handling state (saved by pg_xml_init) */
     126             :     xmlStructuredErrorFunc saved_errfunc;
     127             :     void       *saved_errcxt;
     128             :     /* previous libxml entity handler (saved by pg_xml_init) */
     129             :     xmlExternalEntityLoader saved_entityfunc;
     130             : };
     131             : 
     132             : static xmlParserInputPtr xmlPgEntityLoader(const char *URL, const char *ID,
     133             :                                            xmlParserCtxtPtr ctxt);
     134             : static void xml_errsave(Node *escontext, PgXmlErrorContext *errcxt,
     135             :                         int sqlcode, const char *msg);
     136             : static void xml_errorHandler(void *data, PgXmlErrorPtr error);
     137             : static int  errdetail_for_xml_code(int code);
     138             : static void chopStringInfoNewlines(StringInfo str);
     139             : static void appendStringInfoLineSeparator(StringInfo str);
     140             : 
     141             : #ifdef USE_LIBXMLCONTEXT
     142             : 
     143             : static MemoryContext LibxmlContext = NULL;
     144             : 
     145             : static void xml_memory_init(void);
     146             : static void *xml_palloc(size_t size);
     147             : static void *xml_repalloc(void *ptr, size_t size);
     148             : static void xml_pfree(void *ptr);
     149             : static char *xml_pstrdup(const char *string);
     150             : #endif                          /* USE_LIBXMLCONTEXT */
     151             : 
     152             : static xmlChar *xml_text2xmlChar(text *in);
     153             : static int  parse_xml_decl(const xmlChar *str, size_t *lenp,
     154             :                            xmlChar **version, xmlChar **encoding, int *standalone);
     155             : static bool print_xml_decl(StringInfo buf, const xmlChar *version,
     156             :                            pg_enc encoding, int standalone);
     157             : static bool xml_doctype_in_content(const xmlChar *str);
     158             : static xmlDocPtr xml_parse(text *data, XmlOptionType xmloption_arg,
     159             :                            bool preserve_whitespace, int encoding,
     160             :                            XmlOptionType *parsed_xmloptiontype,
     161             :                            xmlNodePtr *parsed_nodes,
     162             :                            Node *escontext);
     163             : static text *xml_xmlnodetoxmltype(xmlNodePtr cur, PgXmlErrorContext *xmlerrcxt);
     164             : static int  xml_xpathobjtoxmlarray(xmlXPathObjectPtr xpathobj,
     165             :                                    ArrayBuildState *astate,
     166             :                                    PgXmlErrorContext *xmlerrcxt);
     167             : static xmlChar *pg_xmlCharStrndup(const char *str, size_t len);
     168             : #endif                          /* USE_LIBXML */
     169             : 
     170             : static void xmldata_root_element_start(StringInfo result, const char *eltname,
     171             :                                        const char *xmlschema, const char *targetns,
     172             :                                        bool top_level);
     173             : static void xmldata_root_element_end(StringInfo result, const char *eltname);
     174             : static StringInfo query_to_xml_internal(const char *query, char *tablename,
     175             :                                         const char *xmlschema, bool nulls, bool tableforest,
     176             :                                         const char *targetns, bool top_level);
     177             : static const char *map_sql_table_to_xmlschema(TupleDesc tupdesc, Oid relid,
     178             :                                               bool nulls, bool tableforest, const char *targetns);
     179             : static const char *map_sql_schema_to_xmlschema_types(Oid nspid,
     180             :                                                      List *relid_list, bool nulls,
     181             :                                                      bool tableforest, const char *targetns);
     182             : static const char *map_sql_catalog_to_xmlschema_types(List *nspid_list,
     183             :                                                       bool nulls, bool tableforest,
     184             :                                                       const char *targetns);
     185             : static const char *map_sql_type_to_xml_name(Oid typeoid, int typmod);
     186             : static const char *map_sql_typecoll_to_xmlschema_types(List *tupdesc_list);
     187             : static const char *map_sql_type_to_xmlschema_type(Oid typeoid, int typmod);
     188             : static void SPI_sql_row_to_xmlelement(uint64 rownum, StringInfo result,
     189             :                                       char *tablename, bool nulls, bool tableforest,
     190             :                                       const char *targetns, bool top_level);
     191             : 
     192             : /* XMLTABLE support */
     193             : #ifdef USE_LIBXML
     194             : /* random number to identify XmlTableContext */
     195             : #define XMLTABLE_CONTEXT_MAGIC  46922182
     196             : typedef struct XmlTableBuilderData
     197             : {
     198             :     int         magic;
     199             :     int         natts;
     200             :     long int    row_count;
     201             :     PgXmlErrorContext *xmlerrcxt;
     202             :     xmlParserCtxtPtr ctxt;
     203             :     xmlDocPtr   doc;
     204             :     xmlXPathContextPtr xpathcxt;
     205             :     xmlXPathCompExprPtr xpathcomp;
     206             :     xmlXPathObjectPtr xpathobj;
     207             :     xmlXPathCompExprPtr *xpathscomp;
     208             : } XmlTableBuilderData;
     209             : #endif
     210             : 
     211             : static void XmlTableInitOpaque(struct TableFuncScanState *state, int natts);
     212             : static void XmlTableSetDocument(struct TableFuncScanState *state, Datum value);
     213             : static void XmlTableSetNamespace(struct TableFuncScanState *state, const char *name,
     214             :                                  const char *uri);
     215             : static void XmlTableSetRowFilter(struct TableFuncScanState *state, const char *path);
     216             : static void XmlTableSetColumnFilter(struct TableFuncScanState *state,
     217             :                                     const char *path, int colnum);
     218             : static bool XmlTableFetchRow(struct TableFuncScanState *state);
     219             : static Datum XmlTableGetValue(struct TableFuncScanState *state, int colnum,
     220             :                               Oid typid, int32 typmod, bool *isnull);
     221             : static void XmlTableDestroyOpaque(struct TableFuncScanState *state);
     222             : 
     223             : const TableFuncRoutine XmlTableRoutine =
     224             : {
     225             :     .InitOpaque = XmlTableInitOpaque,
     226             :     .SetDocument = XmlTableSetDocument,
     227             :     .SetNamespace = XmlTableSetNamespace,
     228             :     .SetRowFilter = XmlTableSetRowFilter,
     229             :     .SetColumnFilter = XmlTableSetColumnFilter,
     230             :     .FetchRow = XmlTableFetchRow,
     231             :     .GetValue = XmlTableGetValue,
     232             :     .DestroyOpaque = XmlTableDestroyOpaque
     233             : };
     234             : 
     235             : #define NO_XML_SUPPORT() \
     236             :     ereport(ERROR, \
     237             :             (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), \
     238             :              errmsg("unsupported XML feature"), \
     239             :              errdetail("This functionality requires the server to be built with libxml support.")))
     240             : 
     241             : 
     242             : /* from SQL/XML:2008 section 4.9 */
     243             : #define NAMESPACE_XSD "http://www.w3.org/2001/XMLSchema"
     244             : #define NAMESPACE_XSI "http://www.w3.org/2001/XMLSchema-instance"
     245             : #define NAMESPACE_SQLXML "http://standards.iso.org/iso/9075/2003/sqlxml"
     246             : 
     247             : 
     248             : #ifdef USE_LIBXML
     249             : 
     250             : static int
     251           0 : xmlChar_to_encoding(const xmlChar *encoding_name)
     252             : {
     253           0 :     int         encoding = pg_char_to_encoding((const char *) encoding_name);
     254             : 
     255           0 :     if (encoding < 0)
     256           0 :         ereport(ERROR,
     257             :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
     258             :                  errmsg("invalid encoding name \"%s\"",
     259             :                         (const char *) encoding_name)));
     260           0 :     return encoding;
     261             : }
     262             : #endif
     263             : 
     264             : 
     265             : /*
     266             :  * xml_in uses a plain C string to VARDATA conversion, so for the time being
     267             :  * we use the conversion function for the text datatype.
     268             :  *
     269             :  * This is only acceptable so long as xmltype and text use the same
     270             :  * representation.
     271             :  */
     272             : Datum
     273         900 : xml_in(PG_FUNCTION_ARGS)
     274             : {
     275             : #ifdef USE_LIBXML
     276         900 :     char       *s = PG_GETARG_CSTRING(0);
     277             :     xmltype    *vardata;
     278             :     xmlDocPtr   doc;
     279             : 
     280             :     /* Build the result object. */
     281         900 :     vardata = (xmltype *) cstring_to_text(s);
     282             : 
     283             :     /*
     284             :      * Parse the data to check if it is well-formed XML data.
     285             :      *
     286             :      * Note: we don't need to worry about whether a soft error is detected.
     287             :      */
     288         900 :     doc = xml_parse(vardata, xmloption, true, GetDatabaseEncoding(),
     289         900 :                     NULL, NULL, fcinfo->context);
     290         852 :     if (doc != NULL)
     291         840 :         xmlFreeDoc(doc);
     292             : 
     293         852 :     PG_RETURN_XML_P(vardata);
     294             : #else
     295             :     NO_XML_SUPPORT();
     296             :     return 0;
     297             : #endif
     298             : }
     299             : 
     300             : 
     301             : #define PG_XML_DEFAULT_VERSION "1.0"
     302             : 
     303             : 
     304             : /*
     305             :  * xml_out_internal uses a plain VARDATA to C string conversion, so for the
     306             :  * time being we use the conversion function for the text datatype.
     307             :  *
     308             :  * This is only acceptable so long as xmltype and text use the same
     309             :  * representation.
     310             :  */
     311             : static char *
     312       23750 : xml_out_internal(xmltype *x, pg_enc target_encoding)
     313             : {
     314       23750 :     char       *str = text_to_cstring((text *) x);
     315             : 
     316             : #ifdef USE_LIBXML
     317       23750 :     size_t      len = strlen(str);
     318             :     xmlChar    *version;
     319             :     int         standalone;
     320             :     int         res_code;
     321             : 
     322       23750 :     if ((res_code = parse_xml_decl((xmlChar *) str,
     323             :                                    &len, &version, NULL, &standalone)) == 0)
     324             :     {
     325             :         StringInfoData buf;
     326             : 
     327       23750 :         initStringInfo(&buf);
     328             : 
     329       23750 :         if (!print_xml_decl(&buf, version, target_encoding, standalone))
     330             :         {
     331             :             /*
     332             :              * If we are not going to produce an XML declaration, eat a single
     333             :              * newline in the original string to prevent empty first lines in
     334             :              * the output.
     335             :              */
     336       23702 :             if (*(str + len) == '\n')
     337           6 :                 len += 1;
     338             :         }
     339       23750 :         appendStringInfoString(&buf, str + len);
     340             : 
     341       23750 :         pfree(str);
     342             : 
     343       23750 :         return buf.data;
     344             :     }
     345             : 
     346           0 :     ereport(WARNING,
     347             :             errcode(ERRCODE_DATA_CORRUPTED),
     348             :             errmsg_internal("could not parse XML declaration in stored value"),
     349             :             errdetail_for_xml_code(res_code));
     350             : #endif
     351           0 :     return str;
     352             : }
     353             : 
     354             : 
     355             : Datum
     356       23486 : xml_out(PG_FUNCTION_ARGS)
     357             : {
     358       23486 :     xmltype    *x = PG_GETARG_XML_P(0);
     359             : 
     360             :     /*
     361             :      * xml_out removes the encoding property in all cases.  This is because we
     362             :      * cannot control from here whether the datum will be converted to a
     363             :      * different client encoding, so we'd do more harm than good by including
     364             :      * it.
     365             :      */
     366       23486 :     PG_RETURN_CSTRING(xml_out_internal(x, 0));
     367             : }
     368             : 
     369             : 
     370             : Datum
     371           0 : xml_recv(PG_FUNCTION_ARGS)
     372             : {
     373             : #ifdef USE_LIBXML
     374           0 :     StringInfo  buf = (StringInfo) PG_GETARG_POINTER(0);
     375             :     xmltype    *result;
     376             :     char       *str;
     377             :     char       *newstr;
     378             :     int         nbytes;
     379             :     xmlDocPtr   doc;
     380           0 :     xmlChar    *encodingStr = NULL;
     381             :     int         encoding;
     382             : 
     383             :     /*
     384             :      * Read the data in raw format. We don't know yet what the encoding is, as
     385             :      * that information is embedded in the xml declaration; so we have to
     386             :      * parse that before converting to server encoding.
     387             :      */
     388           0 :     nbytes = buf->len - buf->cursor;
     389           0 :     str = (char *) pq_getmsgbytes(buf, nbytes);
     390             : 
     391             :     /*
     392             :      * We need a null-terminated string to pass to parse_xml_decl().  Rather
     393             :      * than make a separate copy, make the temporary result one byte bigger
     394             :      * than it needs to be.
     395             :      */
     396           0 :     result = palloc(nbytes + 1 + VARHDRSZ);
     397           0 :     SET_VARSIZE(result, nbytes + VARHDRSZ);
     398           0 :     memcpy(VARDATA(result), str, nbytes);
     399           0 :     str = VARDATA(result);
     400           0 :     str[nbytes] = '\0';
     401             : 
     402           0 :     parse_xml_decl((const xmlChar *) str, NULL, NULL, &encodingStr, NULL);
     403             : 
     404             :     /*
     405             :      * If encoding wasn't explicitly specified in the XML header, treat it as
     406             :      * UTF-8, as that's the default in XML. This is different from xml_in(),
     407             :      * where the input has to go through the normal client to server encoding
     408             :      * conversion.
     409             :      */
     410           0 :     encoding = encodingStr ? xmlChar_to_encoding(encodingStr) : PG_UTF8;
     411             : 
     412             :     /*
     413             :      * Parse the data to check if it is well-formed XML data.  Assume that
     414             :      * xml_parse will throw ERROR if not.
     415             :      */
     416           0 :     doc = xml_parse(result, xmloption, true, encoding, NULL, NULL, NULL);
     417           0 :     xmlFreeDoc(doc);
     418             : 
     419             :     /* Now that we know what we're dealing with, convert to server encoding */
     420           0 :     newstr = pg_any_to_server(str, nbytes, encoding);
     421             : 
     422           0 :     if (newstr != str)
     423             :     {
     424           0 :         pfree(result);
     425           0 :         result = (xmltype *) cstring_to_text(newstr);
     426           0 :         pfree(newstr);
     427             :     }
     428             : 
     429           0 :     PG_RETURN_XML_P(result);
     430             : #else
     431             :     NO_XML_SUPPORT();
     432             :     return 0;
     433             : #endif
     434             : }
     435             : 
     436             : 
     437             : Datum
     438           0 : xml_send(PG_FUNCTION_ARGS)
     439             : {
     440           0 :     xmltype    *x = PG_GETARG_XML_P(0);
     441             :     char       *outval;
     442             :     StringInfoData buf;
     443             : 
     444             :     /*
     445             :      * xml_out_internal doesn't convert the encoding, it just prints the right
     446             :      * declaration. pq_sendtext will do the conversion.
     447             :      */
     448           0 :     outval = xml_out_internal(x, pg_get_client_encoding());
     449             : 
     450           0 :     pq_begintypsend(&buf);
     451           0 :     pq_sendtext(&buf, outval, strlen(outval));
     452           0 :     pfree(outval);
     453           0 :     PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
     454             : }
     455             : 
     456             : 
     457             : #ifdef USE_LIBXML
     458             : static void
     459         132 : appendStringInfoText(StringInfo str, const text *t)
     460             : {
     461         132 :     appendBinaryStringInfo(str, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t));
     462         132 : }
     463             : #endif
     464             : 
     465             : 
     466             : static xmltype *
     467       22566 : stringinfo_to_xmltype(StringInfo buf)
     468             : {
     469       22566 :     return (xmltype *) cstring_to_text_with_len(buf->data, buf->len);
     470             : }
     471             : 
     472             : 
     473             : static xmltype *
     474          78 : cstring_to_xmltype(const char *string)
     475             : {
     476          78 :     return (xmltype *) cstring_to_text(string);
     477             : }
     478             : 
     479             : 
     480             : #ifdef USE_LIBXML
     481             : static xmltype *
     482       22664 : xmlBuffer_to_xmltype(xmlBufferPtr buf)
     483             : {
     484       22664 :     return (xmltype *) cstring_to_text_with_len((const char *) xmlBufferContent(buf),
     485             :                                                 xmlBufferLength(buf));
     486             : }
     487             : #endif
     488             : 
     489             : 
     490             : Datum
     491          42 : xmlcomment(PG_FUNCTION_ARGS)
     492             : {
     493             : #ifdef USE_LIBXML
     494          42 :     text       *arg = PG_GETARG_TEXT_PP(0);
     495          42 :     char       *argdata = VARDATA_ANY(arg);
     496          42 :     int         len = VARSIZE_ANY_EXHDR(arg);
     497             :     StringInfoData buf;
     498             :     int         i;
     499             : 
     500             :     /* check for "--" in string or "-" at the end */
     501         180 :     for (i = 1; i < len; i++)
     502             :     {
     503         144 :         if (argdata[i] == '-' && argdata[i - 1] == '-')
     504           6 :             ereport(ERROR,
     505             :                     (errcode(ERRCODE_INVALID_XML_COMMENT),
     506             :                      errmsg("invalid XML comment")));
     507             :     }
     508          36 :     if (len > 0 && argdata[len - 1] == '-')
     509           6 :         ereport(ERROR,
     510             :                 (errcode(ERRCODE_INVALID_XML_COMMENT),
     511             :                  errmsg("invalid XML comment")));
     512             : 
     513          30 :     initStringInfo(&buf);
     514          30 :     appendStringInfoString(&buf, "<!--");
     515          30 :     appendStringInfoText(&buf, arg);
     516          30 :     appendStringInfoString(&buf, "-->");
     517             : 
     518          30 :     PG_RETURN_XML_P(stringinfo_to_xmltype(&buf));
     519             : #else
     520             :     NO_XML_SUPPORT();
     521             :     return 0;
     522             : #endif
     523             : }
     524             : 
     525             : 
     526             : Datum
     527          30 : xmltext(PG_FUNCTION_ARGS)
     528             : {
     529             : #ifdef USE_LIBXML
     530          30 :     text       *arg = PG_GETARG_TEXT_PP(0);
     531             :     text       *result;
     532          30 :     volatile xmlChar *xmlbuf = NULL;
     533             :     PgXmlErrorContext *xmlerrcxt;
     534             : 
     535             :     /* First we gotta spin up some error handling. */
     536          30 :     xmlerrcxt = pg_xml_init(PG_XML_STRICTNESS_ALL);
     537             : 
     538          30 :     PG_TRY();
     539             :     {
     540          30 :         xmlbuf = xmlEncodeSpecialChars(NULL, xml_text2xmlChar(arg));
     541             : 
     542          30 :         if (xmlbuf == NULL || xmlerrcxt->err_occurred)
     543           0 :             xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
     544             :                         "could not allocate xmlChar");
     545             : 
     546          30 :         result = cstring_to_text_with_len((const char *) xmlbuf,
     547             :                                           xmlStrlen((const xmlChar *) xmlbuf));
     548             :     }
     549           0 :     PG_CATCH();
     550             :     {
     551           0 :         if (xmlbuf)
     552           0 :             xmlFree((xmlChar *) xmlbuf);
     553             : 
     554           0 :         pg_xml_done(xmlerrcxt, true);
     555           0 :         PG_RE_THROW();
     556             :     }
     557          30 :     PG_END_TRY();
     558             : 
     559          30 :     xmlFree((xmlChar *) xmlbuf);
     560          30 :     pg_xml_done(xmlerrcxt, false);
     561             : 
     562          30 :     PG_RETURN_XML_P(result);
     563             : #else
     564             :     NO_XML_SUPPORT();
     565             :     return 0;
     566             : #endif                          /* not USE_LIBXML */
     567             : }
     568             : 
     569             : 
     570             : /*
     571             :  * TODO: xmlconcat needs to merge the notations and unparsed entities
     572             :  * of the argument values.  Not very important in practice, though.
     573             :  */
     574             : xmltype *
     575       22316 : xmlconcat(List *args)
     576             : {
     577             : #ifdef USE_LIBXML
     578       22316 :     int         global_standalone = 1;
     579       22316 :     xmlChar    *global_version = NULL;
     580       22316 :     bool        global_version_no_value = false;
     581             :     StringInfoData buf;
     582             :     ListCell   *v;
     583             : 
     584       22316 :     initStringInfo(&buf);
     585       66954 :     foreach(v, args)
     586             :     {
     587       44638 :         xmltype    *x = DatumGetXmlP(PointerGetDatum(lfirst(v)));
     588             :         size_t      len;
     589             :         xmlChar    *version;
     590             :         int         standalone;
     591             :         char       *str;
     592             : 
     593       44638 :         len = VARSIZE(x) - VARHDRSZ;
     594       44638 :         str = text_to_cstring((text *) x);
     595             : 
     596       44638 :         parse_xml_decl((xmlChar *) str, &len, &version, NULL, &standalone);
     597             : 
     598       44638 :         if (standalone == 0 && global_standalone == 1)
     599           0 :             global_standalone = 0;
     600       44638 :         if (standalone < 0)
     601       44626 :             global_standalone = -1;
     602             : 
     603       44638 :         if (!version)
     604       44620 :             global_version_no_value = true;
     605          18 :         else if (!global_version)
     606          12 :             global_version = version;
     607           6 :         else if (xmlStrcmp(version, global_version) != 0)
     608           0 :             global_version_no_value = true;
     609             : 
     610       44638 :         appendStringInfoString(&buf, str + len);
     611       44638 :         pfree(str);
     612             :     }
     613             : 
     614       22316 :     if (!global_version_no_value || global_standalone >= 0)
     615             :     {
     616             :         StringInfoData buf2;
     617             : 
     618           6 :         initStringInfo(&buf2);
     619             : 
     620           6 :         print_xml_decl(&buf2,
     621           6 :                        (!global_version_no_value) ? global_version : NULL,
     622             :                        0,
     623             :                        global_standalone);
     624             : 
     625           6 :         appendBinaryStringInfo(&buf2, buf.data, buf.len);
     626           6 :         buf = buf2;
     627             :     }
     628             : 
     629       22316 :     return stringinfo_to_xmltype(&buf);
     630             : #else
     631             :     NO_XML_SUPPORT();
     632             :     return NULL;
     633             : #endif
     634             : }
     635             : 
     636             : 
     637             : /*
     638             :  * XMLAGG support
     639             :  */
     640             : Datum
     641       22292 : xmlconcat2(PG_FUNCTION_ARGS)
     642             : {
     643       22292 :     if (PG_ARGISNULL(0))
     644             :     {
     645          18 :         if (PG_ARGISNULL(1))
     646           0 :             PG_RETURN_NULL();
     647             :         else
     648          18 :             PG_RETURN_XML_P(PG_GETARG_XML_P(1));
     649             :     }
     650       22274 :     else if (PG_ARGISNULL(1))
     651           0 :         PG_RETURN_XML_P(PG_GETARG_XML_P(0));
     652             :     else
     653       22274 :         PG_RETURN_XML_P(xmlconcat(list_make2(PG_GETARG_XML_P(0),
     654             :                                              PG_GETARG_XML_P(1))));
     655             : }
     656             : 
     657             : 
     658             : Datum
     659           6 : texttoxml(PG_FUNCTION_ARGS)
     660             : {
     661           6 :     text       *data = PG_GETARG_TEXT_PP(0);
     662             : 
     663           6 :     PG_RETURN_XML_P(xmlparse(data, xmloption, true));
     664             : }
     665             : 
     666             : 
     667             : Datum
     668           0 : xmltotext(PG_FUNCTION_ARGS)
     669             : {
     670           0 :     xmltype    *data = PG_GETARG_XML_P(0);
     671             : 
     672             :     /* It's actually binary compatible. */
     673           0 :     PG_RETURN_TEXT_P((text *) data);
     674             : }
     675             : 
     676             : 
     677             : text *
     678         180 : xmltotext_with_options(xmltype *data, XmlOptionType xmloption_arg, bool indent)
     679             : {
     680             : #ifdef USE_LIBXML
     681             :     text       *volatile result;
     682             :     xmlDocPtr   doc;
     683             :     XmlOptionType parsed_xmloptiontype;
     684             :     xmlNodePtr  content_nodes;
     685         180 :     volatile xmlBufferPtr buf = NULL;
     686         180 :     volatile xmlSaveCtxtPtr ctxt = NULL;
     687         180 :     ErrorSaveContext escontext = {T_ErrorSaveContext};
     688         180 :     PgXmlErrorContext *volatile xmlerrcxt = NULL;
     689             : #endif
     690             : 
     691         180 :     if (xmloption_arg != XMLOPTION_DOCUMENT && !indent)
     692             :     {
     693             :         /*
     694             :          * We don't actually need to do anything, so just return the
     695             :          * binary-compatible input.  For backwards-compatibility reasons,
     696             :          * allow such cases to succeed even without USE_LIBXML.
     697             :          */
     698          36 :         return (text *) data;
     699             :     }
     700             : 
     701             : #ifdef USE_LIBXML
     702             : 
     703             :     /*
     704             :      * Parse the input according to the xmloption.
     705             :      *
     706             :      * preserve_whitespace is set to false in case we are indenting, otherwise
     707             :      * libxml2 will fail to indent elements that have whitespace between them.
     708             :      */
     709         144 :     doc = xml_parse(data, xmloption_arg, !indent, GetDatabaseEncoding(),
     710             :                     &parsed_xmloptiontype, &content_nodes,
     711         144 :                     (Node *) &escontext);
     712         144 :     if (doc == NULL || escontext.error_occurred)
     713             :     {
     714          30 :         if (doc)
     715           0 :             xmlFreeDoc(doc);
     716             :         /* A soft error must be failure to conform to XMLOPTION_DOCUMENT */
     717          30 :         ereport(ERROR,
     718             :                 (errcode(ERRCODE_NOT_AN_XML_DOCUMENT),
     719             :                  errmsg("not an XML document")));
     720             :     }
     721             : 
     722             :     /* If we weren't asked to indent, we're done. */
     723         114 :     if (!indent)
     724             :     {
     725          18 :         xmlFreeDoc(doc);
     726          18 :         return (text *) data;
     727             :     }
     728             : 
     729             :     /*
     730             :      * Otherwise, we gotta spin up some error handling.  Unlike most other
     731             :      * routines in this module, we already have a libxml "doc" structure to
     732             :      * free, so we need to call pg_xml_init() inside the PG_TRY and be
     733             :      * prepared for it to fail (typically due to palloc OOM).
     734             :      */
     735          96 :     PG_TRY();
     736             :     {
     737          96 :         size_t      decl_len = 0;
     738             : 
     739          96 :         xmlerrcxt = pg_xml_init(PG_XML_STRICTNESS_ALL);
     740             : 
     741             :         /* The serialized data will go into this buffer. */
     742          96 :         buf = xmlBufferCreate();
     743             : 
     744          96 :         if (buf == NULL || xmlerrcxt->err_occurred)
     745           0 :             xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
     746             :                         "could not allocate xmlBuffer");
     747             : 
     748             :         /* Detect whether there's an XML declaration */
     749          96 :         parse_xml_decl(xml_text2xmlChar(data), &decl_len, NULL, NULL, NULL);
     750             : 
     751             :         /*
     752             :          * Emit declaration only if the input had one.  Note: some versions of
     753             :          * xmlSaveToBuffer leak memory if a non-null encoding argument is
     754             :          * passed, so don't do that.  We don't want any encoding conversion
     755             :          * anyway.
     756             :          */
     757          96 :         if (decl_len == 0)
     758          84 :             ctxt = xmlSaveToBuffer(buf, NULL,
     759             :                                    XML_SAVE_NO_DECL | XML_SAVE_FORMAT);
     760             :         else
     761          12 :             ctxt = xmlSaveToBuffer(buf, NULL,
     762             :                                    XML_SAVE_FORMAT);
     763             : 
     764          96 :         if (ctxt == NULL || xmlerrcxt->err_occurred)
     765           0 :             xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
     766             :                         "could not allocate xmlSaveCtxt");
     767             : 
     768          96 :         if (parsed_xmloptiontype == XMLOPTION_DOCUMENT)
     769             :         {
     770             :             /* If it's a document, saving is easy. */
     771          42 :             if (xmlSaveDoc(ctxt, doc) == -1 || xmlerrcxt->err_occurred)
     772           0 :                 xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
     773             :                             "could not save document to xmlBuffer");
     774             :         }
     775          54 :         else if (content_nodes != NULL)
     776             :         {
     777             :             /*
     778             :              * Deal with the case where we have non-singly-rooted XML.
     779             :              * libxml's dump functions don't work well for that without help.
     780             :              * We build a fake root node that serves as a container for the
     781             :              * content nodes, and then iterate over the nodes.
     782             :              */
     783             :             xmlNodePtr  root;
     784             :             xmlNodePtr  oldroot;
     785             :             xmlNodePtr  newline;
     786             : 
     787          48 :             root = xmlNewNode(NULL, (const xmlChar *) "content-root");
     788          48 :             if (root == NULL || xmlerrcxt->err_occurred)
     789           0 :                 xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
     790             :                             "could not allocate xml node");
     791             : 
     792             :             /*
     793             :              * This attaches root to doc, so we need not free it separately...
     794             :              * but instead, we have to free the old root if there was one.
     795             :              */
     796          48 :             oldroot = xmlDocSetRootElement(doc, root);
     797          48 :             if (oldroot != NULL)
     798           0 :                 xmlFreeNode(oldroot);
     799             : 
     800          48 :             if (xmlAddChildList(root, content_nodes) == NULL ||
     801          48 :                 xmlerrcxt->err_occurred)
     802           0 :                 xml_ereport(xmlerrcxt, ERROR, ERRCODE_INTERNAL_ERROR,
     803             :                             "could not append xml node list");
     804             : 
     805             :             /*
     806             :              * We use this node to insert newlines in the dump.  Note: in at
     807             :              * least some libxml versions, xmlNewDocText would not attach the
     808             :              * node to the document even if we passed it.  Therefore, manage
     809             :              * freeing of this node manually, and pass NULL here to make sure
     810             :              * there's not a dangling link.
     811             :              */
     812          48 :             newline = xmlNewDocText(NULL, (const xmlChar *) "\n");
     813          48 :             if (newline == NULL || xmlerrcxt->err_occurred)
     814           0 :                 xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
     815             :                             "could not allocate xml node");
     816             : 
     817         126 :             for (xmlNodePtr node = root->children; node; node = node->next)
     818             :             {
     819             :                 /* insert newlines between nodes */
     820          78 :                 if (node->type != XML_TEXT_NODE && node->prev != NULL)
     821             :                 {
     822          24 :                     if (xmlSaveTree(ctxt, newline) == -1 || xmlerrcxt->err_occurred)
     823             :                     {
     824           0 :                         xmlFreeNode(newline);
     825           0 :                         xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
     826             :                                     "could not save newline to xmlBuffer");
     827             :                     }
     828             :                 }
     829             : 
     830          78 :                 if (xmlSaveTree(ctxt, node) == -1 || xmlerrcxt->err_occurred)
     831             :                 {
     832           0 :                     xmlFreeNode(newline);
     833           0 :                     xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
     834             :                                 "could not save content to xmlBuffer");
     835             :                 }
     836             :             }
     837             : 
     838          48 :             xmlFreeNode(newline);
     839             :         }
     840             : 
     841          96 :         if (xmlSaveClose(ctxt) == -1 || xmlerrcxt->err_occurred)
     842             :         {
     843           0 :             ctxt = NULL;        /* don't try to close it again */
     844           0 :             xml_ereport(xmlerrcxt, ERROR, ERRCODE_INTERNAL_ERROR,
     845             :                         "could not close xmlSaveCtxtPtr");
     846             :         }
     847             : 
     848             :         /*
     849             :          * xmlDocContentDumpOutput may add a trailing newline, so remove that.
     850             :          */
     851          96 :         if (xmloption_arg == XMLOPTION_DOCUMENT)
     852             :         {
     853          36 :             const char *str = (const char *) xmlBufferContent(buf);
     854          36 :             int         len = xmlBufferLength(buf);
     855             : 
     856          72 :             while (len > 0 && (str[len - 1] == '\n' ||
     857          36 :                                str[len - 1] == '\r'))
     858          36 :                 len--;
     859             : 
     860          36 :             result = cstring_to_text_with_len(str, len);
     861             :         }
     862             :         else
     863          60 :             result = (text *) xmlBuffer_to_xmltype(buf);
     864             :     }
     865           0 :     PG_CATCH();
     866             :     {
     867           0 :         if (ctxt)
     868           0 :             xmlSaveClose(ctxt);
     869           0 :         if (buf)
     870           0 :             xmlBufferFree(buf);
     871           0 :         xmlFreeDoc(doc);
     872             : 
     873           0 :         if (xmlerrcxt)
     874           0 :             pg_xml_done(xmlerrcxt, true);
     875             : 
     876           0 :         PG_RE_THROW();
     877             :     }
     878          96 :     PG_END_TRY();
     879             : 
     880          96 :     xmlBufferFree(buf);
     881          96 :     xmlFreeDoc(doc);
     882             : 
     883          96 :     pg_xml_done(xmlerrcxt, false);
     884             : 
     885          96 :     return result;
     886             : #else
     887             :     NO_XML_SUPPORT();
     888             :     return NULL;
     889             : #endif
     890             : }
     891             : 
     892             : 
     893             : xmltype *
     894       22448 : xmlelement(XmlExpr *xexpr,
     895             :            Datum *named_argvalue, bool *named_argnull,
     896             :            Datum *argvalue, bool *argnull)
     897             : {
     898             : #ifdef USE_LIBXML
     899             :     xmltype    *result;
     900             :     List       *named_arg_strings;
     901             :     List       *arg_strings;
     902             :     int         i;
     903             :     ListCell   *arg;
     904             :     ListCell   *narg;
     905             :     PgXmlErrorContext *xmlerrcxt;
     906       22448 :     volatile xmlBufferPtr buf = NULL;
     907       22448 :     volatile xmlTextWriterPtr writer = NULL;
     908             : 
     909             :     /*
     910             :      * All arguments are already evaluated, and their values are passed in the
     911             :      * named_argvalue/named_argnull or argvalue/argnull arrays.  This avoids
     912             :      * issues if one of the arguments involves a call to some other function
     913             :      * or subsystem that wants to use libxml on its own terms.  We examine the
     914             :      * original XmlExpr to identify the numbers and types of the arguments.
     915             :      */
     916       22448 :     named_arg_strings = NIL;
     917       22448 :     i = 0;
     918       22496 :     foreach(arg, xexpr->named_args)
     919             :     {
     920          54 :         Expr       *e = (Expr *) lfirst(arg);
     921             :         char       *str;
     922             : 
     923          54 :         if (named_argnull[i])
     924           0 :             str = NULL;
     925             :         else
     926          54 :             str = map_sql_value_to_xml_value(named_argvalue[i],
     927             :                                              exprType((Node *) e),
     928             :                                              false);
     929          48 :         named_arg_strings = lappend(named_arg_strings, str);
     930          48 :         i++;
     931             :     }
     932             : 
     933       22442 :     arg_strings = NIL;
     934       22442 :     i = 0;
     935       44860 :     foreach(arg, xexpr->args)
     936             :     {
     937       22418 :         Expr       *e = (Expr *) lfirst(arg);
     938             :         char       *str;
     939             : 
     940             :         /* here we can just forget NULL elements immediately */
     941       22418 :         if (!argnull[i])
     942             :         {
     943       22418 :             str = map_sql_value_to_xml_value(argvalue[i],
     944             :                                              exprType((Node *) e),
     945             :                                              true);
     946       22418 :             arg_strings = lappend(arg_strings, str);
     947             :         }
     948       22418 :         i++;
     949             :     }
     950             : 
     951       22442 :     xmlerrcxt = pg_xml_init(PG_XML_STRICTNESS_ALL);
     952             : 
     953       22442 :     PG_TRY();
     954             :     {
     955       22442 :         buf = xmlBufferCreate();
     956       22442 :         if (buf == NULL || xmlerrcxt->err_occurred)
     957           0 :             xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
     958             :                         "could not allocate xmlBuffer");
     959       22442 :         writer = xmlNewTextWriterMemory(buf, 0);
     960       22442 :         if (writer == NULL || xmlerrcxt->err_occurred)
     961           0 :             xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
     962             :                         "could not allocate xmlTextWriter");
     963             : 
     964       22442 :         if (xmlTextWriterStartElement(writer, (xmlChar *) xexpr->name) < 0 ||
     965       22442 :             xmlerrcxt->err_occurred)
     966           0 :             xml_ereport(xmlerrcxt, ERROR, ERRCODE_INTERNAL_ERROR,
     967             :                         "could not start xml element");
     968             : 
     969       22490 :         forboth(arg, named_arg_strings, narg, xexpr->arg_names)
     970             :         {
     971          48 :             char       *str = (char *) lfirst(arg);
     972          48 :             char       *argname = strVal(lfirst(narg));
     973             : 
     974          48 :             if (str)
     975             :             {
     976          48 :                 if (xmlTextWriterWriteAttribute(writer,
     977             :                                                 (xmlChar *) argname,
     978          48 :                                                 (xmlChar *) str) < 0 ||
     979          48 :                     xmlerrcxt->err_occurred)
     980           0 :                     xml_ereport(xmlerrcxt, ERROR, ERRCODE_INTERNAL_ERROR,
     981             :                                 "could not write xml attribute");
     982             :             }
     983             :         }
     984             : 
     985       44860 :         foreach(arg, arg_strings)
     986             :         {
     987       22418 :             char       *str = (char *) lfirst(arg);
     988             : 
     989       22418 :             if (xmlTextWriterWriteRaw(writer, (xmlChar *) str) < 0 ||
     990       22418 :                 xmlerrcxt->err_occurred)
     991           0 :                 xml_ereport(xmlerrcxt, ERROR, ERRCODE_INTERNAL_ERROR,
     992             :                             "could not write raw xml text");
     993             :         }
     994             : 
     995       22442 :         if (xmlTextWriterEndElement(writer) < 0 ||
     996       22442 :             xmlerrcxt->err_occurred)
     997           0 :             xml_ereport(xmlerrcxt, ERROR, ERRCODE_INTERNAL_ERROR,
     998             :                         "could not end xml element");
     999             : 
    1000             :         /* we MUST do this now to flush data out to the buffer ... */
    1001       22442 :         xmlFreeTextWriter(writer);
    1002       22442 :         writer = NULL;
    1003             : 
    1004       22442 :         result = xmlBuffer_to_xmltype(buf);
    1005             :     }
    1006           0 :     PG_CATCH();
    1007             :     {
    1008           0 :         if (writer)
    1009           0 :             xmlFreeTextWriter(writer);
    1010           0 :         if (buf)
    1011           0 :             xmlBufferFree(buf);
    1012             : 
    1013           0 :         pg_xml_done(xmlerrcxt, true);
    1014             : 
    1015           0 :         PG_RE_THROW();
    1016             :     }
    1017       22442 :     PG_END_TRY();
    1018             : 
    1019       22442 :     xmlBufferFree(buf);
    1020             : 
    1021       22442 :     pg_xml_done(xmlerrcxt, false);
    1022             : 
    1023       22442 :     return result;
    1024             : #else
    1025             :     NO_XML_SUPPORT();
    1026             :     return NULL;
    1027             : #endif
    1028             : }
    1029             : 
    1030             : 
    1031             : xmltype *
    1032         138 : xmlparse(text *data, XmlOptionType xmloption_arg, bool preserve_whitespace)
    1033             : {
    1034             : #ifdef USE_LIBXML
    1035             :     xmlDocPtr   doc;
    1036             : 
    1037         138 :     doc = xml_parse(data, xmloption_arg, preserve_whitespace,
    1038             :                     GetDatabaseEncoding(), NULL, NULL, NULL);
    1039          90 :     xmlFreeDoc(doc);
    1040             : 
    1041          90 :     return (xmltype *) data;
    1042             : #else
    1043             :     NO_XML_SUPPORT();
    1044             :     return NULL;
    1045             : #endif
    1046             : }
    1047             : 
    1048             : 
    1049             : xmltype *
    1050          72 : xmlpi(const char *target, text *arg, bool arg_is_null, bool *result_is_null)
    1051             : {
    1052             : #ifdef USE_LIBXML
    1053             :     xmltype    *result;
    1054             :     StringInfoData buf;
    1055             : 
    1056          72 :     if (pg_strcasecmp(target, "xml") == 0)
    1057          12 :         ereport(ERROR,
    1058             :                 (errcode(ERRCODE_INVALID_XML_PROCESSING_INSTRUCTION),
    1059             :                  errmsg("invalid XML processing instruction"),
    1060             :                  errdetail("XML processing instruction target name cannot be \"%s\".", target)));
    1061             : 
    1062             :     /*
    1063             :      * Following the SQL standard, the null check comes after the syntax check
    1064             :      * above.
    1065             :      */
    1066          60 :     *result_is_null = arg_is_null;
    1067          60 :     if (*result_is_null)
    1068          12 :         return NULL;
    1069             : 
    1070          48 :     initStringInfo(&buf);
    1071             : 
    1072          48 :     appendStringInfo(&buf, "<?%s", target);
    1073             : 
    1074          48 :     if (arg != NULL)
    1075             :     {
    1076             :         char       *string;
    1077             : 
    1078          24 :         string = text_to_cstring(arg);
    1079          24 :         if (strstr(string, "?>") != NULL)
    1080           6 :             ereport(ERROR,
    1081             :                     (errcode(ERRCODE_INVALID_XML_PROCESSING_INSTRUCTION),
    1082             :                      errmsg("invalid XML processing instruction"),
    1083             :                      errdetail("XML processing instruction cannot contain \"?>\".")));
    1084             : 
    1085          18 :         appendStringInfoChar(&buf, ' ');
    1086          18 :         appendStringInfoString(&buf, string + strspn(string, " "));
    1087          18 :         pfree(string);
    1088             :     }
    1089          42 :     appendStringInfoString(&buf, "?>");
    1090             : 
    1091          42 :     result = stringinfo_to_xmltype(&buf);
    1092          42 :     pfree(buf.data);
    1093          42 :     return result;
    1094             : #else
    1095             :     NO_XML_SUPPORT();
    1096             :     return NULL;
    1097             : #endif
    1098             : }
    1099             : 
    1100             : 
    1101             : xmltype *
    1102          60 : xmlroot(xmltype *data, text *version, int standalone)
    1103             : {
    1104             : #ifdef USE_LIBXML
    1105             :     char       *str;
    1106             :     size_t      len;
    1107             :     xmlChar    *orig_version;
    1108             :     int         orig_standalone;
    1109             :     StringInfoData buf;
    1110             : 
    1111          60 :     len = VARSIZE(data) - VARHDRSZ;
    1112          60 :     str = text_to_cstring((text *) data);
    1113             : 
    1114          60 :     parse_xml_decl((xmlChar *) str, &len, &orig_version, NULL, &orig_standalone);
    1115             : 
    1116          60 :     if (version)
    1117          24 :         orig_version = xml_text2xmlChar(version);
    1118             :     else
    1119          36 :         orig_version = NULL;
    1120             : 
    1121          60 :     switch (standalone)
    1122             :     {
    1123          18 :         case XML_STANDALONE_YES:
    1124          18 :             orig_standalone = 1;
    1125          18 :             break;
    1126          12 :         case XML_STANDALONE_NO:
    1127          12 :             orig_standalone = 0;
    1128          12 :             break;
    1129          12 :         case XML_STANDALONE_NO_VALUE:
    1130          12 :             orig_standalone = -1;
    1131          12 :             break;
    1132          18 :         case XML_STANDALONE_OMITTED:
    1133             :             /* leave original value */
    1134          18 :             break;
    1135             :     }
    1136             : 
    1137          60 :     initStringInfo(&buf);
    1138          60 :     print_xml_decl(&buf, orig_version, 0, orig_standalone);
    1139          60 :     appendStringInfoString(&buf, str + len);
    1140             : 
    1141          60 :     return stringinfo_to_xmltype(&buf);
    1142             : #else
    1143             :     NO_XML_SUPPORT();
    1144             :     return NULL;
    1145             : #endif
    1146             : }
    1147             : 
    1148             : 
    1149             : /*
    1150             :  * Validate document (given as string) against DTD (given as external link)
    1151             :  *
    1152             :  * This has been removed because it is a security hole: unprivileged users
    1153             :  * should not be able to use Postgres to fetch arbitrary external files,
    1154             :  * which unfortunately is exactly what libxml is willing to do with the DTD
    1155             :  * parameter.
    1156             :  */
    1157             : Datum
    1158           0 : xmlvalidate(PG_FUNCTION_ARGS)
    1159             : {
    1160           0 :     ereport(ERROR,
    1161             :             (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    1162             :              errmsg("xmlvalidate is not implemented")));
    1163             :     return 0;
    1164             : }
    1165             : 
    1166             : 
    1167             : bool
    1168          24 : xml_is_document(xmltype *arg)
    1169             : {
    1170             : #ifdef USE_LIBXML
    1171             :     xmlDocPtr   doc;
    1172          24 :     ErrorSaveContext escontext = {T_ErrorSaveContext};
    1173             : 
    1174             :     /*
    1175             :      * We'll report "true" if no soft error is reported by xml_parse().
    1176             :      */
    1177          24 :     doc = xml_parse((text *) arg, XMLOPTION_DOCUMENT, true,
    1178             :                     GetDatabaseEncoding(), NULL, NULL, (Node *) &escontext);
    1179          24 :     if (doc)
    1180          12 :         xmlFreeDoc(doc);
    1181             : 
    1182          24 :     return !escontext.error_occurred;
    1183             : #else                           /* not USE_LIBXML */
    1184             :     NO_XML_SUPPORT();
    1185             :     return false;
    1186             : #endif                          /* not USE_LIBXML */
    1187             : }
    1188             : 
    1189             : 
    1190             : #ifdef USE_LIBXML
    1191             : 
    1192             : /*
    1193             :  * pg_xml_init_library --- set up for use of libxml
    1194             :  *
    1195             :  * This should be called by each function that is about to use libxml
    1196             :  * facilities but doesn't require error handling.  It initializes libxml
    1197             :  * and verifies compatibility with the loaded libxml version.  These are
    1198             :  * once-per-session activities.
    1199             :  *
    1200             :  * TODO: xmlChar is utf8-char, make proper tuning (initdb with enc!=utf8 and
    1201             :  * check)
    1202             :  */
    1203             : void
    1204       94904 : pg_xml_init_library(void)
    1205             : {
    1206             :     static bool first_time = true;
    1207             : 
    1208       94904 :     if (first_time)
    1209             :     {
    1210             :         /* Stuff we need do only once per session */
    1211             : 
    1212             :         /*
    1213             :          * Currently, we have no pure UTF-8 support for internals -- check if
    1214             :          * we can work.
    1215             :          */
    1216             :         if (sizeof(char) != sizeof(xmlChar))
    1217             :             ereport(ERROR,
    1218             :                     (errmsg("could not initialize XML library"),
    1219             :                      errdetail("libxml2 has incompatible char type: sizeof(char)=%zu, sizeof(xmlChar)=%zu.",
    1220             :                                sizeof(char), sizeof(xmlChar))));
    1221             : 
    1222             : #ifdef USE_LIBXMLCONTEXT
    1223             :         /* Set up libxml's memory allocation our way */
    1224             :         xml_memory_init();
    1225             : #endif
    1226             : 
    1227             :         /* Check library compatibility */
    1228          40 :         LIBXML_TEST_VERSION;
    1229             : 
    1230          40 :         first_time = false;
    1231             :     }
    1232       94904 : }
    1233             : 
    1234             : /*
    1235             :  * pg_xml_init --- set up for use of libxml and register an error handler
    1236             :  *
    1237             :  * This should be called by each function that is about to use libxml
    1238             :  * facilities and requires error handling.  It initializes libxml with
    1239             :  * pg_xml_init_library() and establishes our libxml error handler.
    1240             :  *
    1241             :  * strictness determines which errors are reported and which are ignored.
    1242             :  *
    1243             :  * Calls to this function MUST be followed by a PG_TRY block that guarantees
    1244             :  * that pg_xml_done() is called during either normal or error exit.
    1245             :  *
    1246             :  * This is exported for use by contrib/xml2, as well as other code that might
    1247             :  * wish to share use of this module's libxml error handler.
    1248             :  */
    1249             : PgXmlErrorContext *
    1250       24782 : pg_xml_init(PgXmlStrictness strictness)
    1251             : {
    1252             :     PgXmlErrorContext *errcxt;
    1253             :     void       *new_errcxt;
    1254             : 
    1255             :     /* Do one-time setup if needed */
    1256       24782 :     pg_xml_init_library();
    1257             : 
    1258             :     /* Create error handling context structure */
    1259       24782 :     errcxt = (PgXmlErrorContext *) palloc(sizeof(PgXmlErrorContext));
    1260       24782 :     errcxt->magic = ERRCXT_MAGIC;
    1261       24782 :     errcxt->strictness = strictness;
    1262       24782 :     errcxt->err_occurred = false;
    1263       24782 :     initStringInfo(&errcxt->err_buf);
    1264             : 
    1265             :     /*
    1266             :      * Save original error handler and install ours. libxml originally didn't
    1267             :      * distinguish between the contexts for generic and for structured error
    1268             :      * handlers.  If we're using an old libxml version, we must thus save the
    1269             :      * generic error context, even though we're using a structured error
    1270             :      * handler.
    1271             :      */
    1272       24782 :     errcxt->saved_errfunc = xmlStructuredError;
    1273             : 
    1274             : #ifdef HAVE_XMLSTRUCTUREDERRORCONTEXT
    1275       24782 :     errcxt->saved_errcxt = xmlStructuredErrorContext;
    1276             : #else
    1277             :     errcxt->saved_errcxt = xmlGenericErrorContext;
    1278             : #endif
    1279             : 
    1280       24782 :     xmlSetStructuredErrorFunc(errcxt, xml_errorHandler);
    1281             : 
    1282             :     /*
    1283             :      * Verify that xmlSetStructuredErrorFunc set the context variable we
    1284             :      * expected it to.  If not, the error context pointer we just saved is not
    1285             :      * the correct thing to restore, and since that leaves us without a way to
    1286             :      * restore the context in pg_xml_done, we must fail.
    1287             :      *
    1288             :      * The only known situation in which this test fails is if we compile with
    1289             :      * headers from a libxml2 that doesn't track the structured error context
    1290             :      * separately (< 2.7.4), but at runtime use a version that does, or vice
    1291             :      * versa.  The libxml2 authors did not treat that change as constituting
    1292             :      * an ABI break, so the LIBXML_TEST_VERSION test in pg_xml_init_library
    1293             :      * fails to protect us from this.
    1294             :      */
    1295             : 
    1296             : #ifdef HAVE_XMLSTRUCTUREDERRORCONTEXT
    1297       24782 :     new_errcxt = xmlStructuredErrorContext;
    1298             : #else
    1299             :     new_errcxt = xmlGenericErrorContext;
    1300             : #endif
    1301             : 
    1302       24782 :     if (new_errcxt != errcxt)
    1303           0 :         ereport(ERROR,
    1304             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    1305             :                  errmsg("could not set up XML error handler"),
    1306             :                  errhint("This probably indicates that the version of libxml2"
    1307             :                          " being used is not compatible with the libxml2"
    1308             :                          " header files that PostgreSQL was built with.")));
    1309             : 
    1310             :     /*
    1311             :      * Also, install an entity loader to prevent unwanted fetches of external
    1312             :      * files and URLs.
    1313             :      */
    1314       24782 :     errcxt->saved_entityfunc = xmlGetExternalEntityLoader();
    1315       24782 :     xmlSetExternalEntityLoader(xmlPgEntityLoader);
    1316             : 
    1317       24782 :     return errcxt;
    1318             : }
    1319             : 
    1320             : 
    1321             : /*
    1322             :  * pg_xml_done --- restore previous libxml error handling
    1323             :  *
    1324             :  * Resets libxml's global error-handling state to what it was before
    1325             :  * pg_xml_init() was called.
    1326             :  *
    1327             :  * This routine verifies that all pending errors have been dealt with
    1328             :  * (in assert-enabled builds, anyway).
    1329             :  */
    1330             : void
    1331       24782 : pg_xml_done(PgXmlErrorContext *errcxt, bool isError)
    1332             : {
    1333             :     void       *cur_errcxt;
    1334             : 
    1335             :     /* An assert seems like enough protection here */
    1336             :     Assert(errcxt->magic == ERRCXT_MAGIC);
    1337             : 
    1338             :     /*
    1339             :      * In a normal exit, there should be no un-handled libxml errors.  But we
    1340             :      * shouldn't try to enforce this during error recovery, since the longjmp
    1341             :      * could have been thrown before xml_ereport had a chance to run.
    1342             :      */
    1343             :     Assert(!errcxt->err_occurred || isError);
    1344             : 
    1345             :     /*
    1346             :      * Check that libxml's global state is correct, warn if not.  This is a
    1347             :      * real test and not an Assert because it has a higher probability of
    1348             :      * happening.
    1349             :      */
    1350             : #ifdef HAVE_XMLSTRUCTUREDERRORCONTEXT
    1351       24782 :     cur_errcxt = xmlStructuredErrorContext;
    1352             : #else
    1353             :     cur_errcxt = xmlGenericErrorContext;
    1354             : #endif
    1355             : 
    1356       24782 :     if (cur_errcxt != errcxt)
    1357           0 :         elog(WARNING, "libxml error handling state is out of sync with xml.c");
    1358             : 
    1359             :     /* Restore the saved handlers */
    1360       24782 :     xmlSetStructuredErrorFunc(errcxt->saved_errcxt, errcxt->saved_errfunc);
    1361       24782 :     xmlSetExternalEntityLoader(errcxt->saved_entityfunc);
    1362             : 
    1363             :     /*
    1364             :      * Mark the struct as invalid, just in case somebody somehow manages to
    1365             :      * call xml_errorHandler or xml_ereport with it.
    1366             :      */
    1367       24782 :     errcxt->magic = 0;
    1368             : 
    1369             :     /* Release memory */
    1370       24782 :     pfree(errcxt->err_buf.data);
    1371       24782 :     pfree(errcxt);
    1372       24782 : }
    1373             : 
    1374             : 
    1375             : /*
    1376             :  * pg_xml_error_occurred() --- test the error flag
    1377             :  */
    1378             : bool
    1379          78 : pg_xml_error_occurred(PgXmlErrorContext *errcxt)
    1380             : {
    1381          78 :     return errcxt->err_occurred;
    1382             : }
    1383             : 
    1384             : 
    1385             : /*
    1386             :  * SQL/XML allows storing "XML documents" or "XML content".  "XML
    1387             :  * documents" are specified by the XML specification and are parsed
    1388             :  * easily by libxml.  "XML content" is specified by SQL/XML as the
    1389             :  * production "XMLDecl? content".  But libxml can only parse the
    1390             :  * "content" part, so we have to parse the XML declaration ourselves
    1391             :  * to complete this.
    1392             :  */
    1393             : 
    1394             : #define CHECK_XML_SPACE(p) \
    1395             :     do { \
    1396             :         if (!xmlIsBlank_ch(*(p))) \
    1397             :             return XML_ERR_SPACE_REQUIRED; \
    1398             :     } while (0)
    1399             : 
    1400             : #define SKIP_XML_SPACE(p) \
    1401             :     while (xmlIsBlank_ch(*(p))) (p)++
    1402             : 
    1403             : /* Letter | Digit | '.' | '-' | '_' | ':' | CombiningChar | Extender */
    1404             : /* Beware of multiple evaluations of argument! */
    1405             : #define PG_XMLISNAMECHAR(c) \
    1406             :     (xmlIsBaseChar_ch(c) || xmlIsIdeographicQ(c) \
    1407             :             || xmlIsDigit_ch(c) \
    1408             :             || c == '.' || c == '-' || c == '_' || c == ':' \
    1409             :             || xmlIsCombiningQ(c) \
    1410             :             || xmlIsExtender_ch(c))
    1411             : 
    1412             : /* pnstrdup, but deal with xmlChar not char; len is measured in xmlChars */
    1413             : static xmlChar *
    1414         192 : xml_pnstrdup(const xmlChar *str, size_t len)
    1415             : {
    1416             :     xmlChar    *result;
    1417             : 
    1418         192 :     result = (xmlChar *) palloc((len + 1) * sizeof(xmlChar));
    1419         192 :     memcpy(result, str, len * sizeof(xmlChar));
    1420         192 :     result[len] = 0;
    1421         192 :     return result;
    1422             : }
    1423             : 
    1424             : /* Ditto, except input is char* */
    1425             : static xmlChar *
    1426        2424 : pg_xmlCharStrndup(const char *str, size_t len)
    1427             : {
    1428             :     xmlChar    *result;
    1429             : 
    1430        2424 :     result = (xmlChar *) palloc((len + 1) * sizeof(xmlChar));
    1431        2424 :     memcpy(result, str, len);
    1432        2424 :     result[len] = '\0';
    1433             : 
    1434        2424 :     return result;
    1435             : }
    1436             : 
    1437             : /*
    1438             :  * Copy xmlChar string to PostgreSQL-owned memory, freeing the input.
    1439             :  *
    1440             :  * The input xmlChar is freed regardless of success of the copy.
    1441             :  */
    1442             : static char *
    1443      112828 : xml_pstrdup_and_free(xmlChar *str)
    1444             : {
    1445             :     char       *result;
    1446             : 
    1447      112828 :     if (str)
    1448             :     {
    1449      112828 :         PG_TRY();
    1450             :         {
    1451      112828 :             result = pstrdup((char *) str);
    1452             :         }
    1453           0 :         PG_FINALLY();
    1454             :         {
    1455      112828 :             xmlFree(str);
    1456             :         }
    1457      112828 :         PG_END_TRY();
    1458             :     }
    1459             :     else
    1460           0 :         result = NULL;
    1461             : 
    1462      112828 :     return result;
    1463             : }
    1464             : 
    1465             : /*
    1466             :  * str is the null-terminated input string.  Remaining arguments are
    1467             :  * output arguments; each can be NULL if value is not wanted.
    1468             :  * version and encoding are returned as locally-palloc'd strings.
    1469             :  * Result is 0 if OK, an error code if not.
    1470             :  */
    1471             : static int
    1472       70122 : parse_xml_decl(const xmlChar *str, size_t *lenp,
    1473             :                xmlChar **version, xmlChar **encoding, int *standalone)
    1474             : {
    1475             :     const xmlChar *p;
    1476             :     const xmlChar *save_p;
    1477             :     size_t      len;
    1478             :     int         utf8char;
    1479             :     int         utf8len;
    1480             : 
    1481             :     /*
    1482             :      * Only initialize libxml.  We don't need error handling here, but we do
    1483             :      * need to make sure libxml is initialized before calling any of its
    1484             :      * functions.  Note that this is safe (and a no-op) if caller has already
    1485             :      * done pg_xml_init().
    1486             :      */
    1487       70122 :     pg_xml_init_library();
    1488             : 
    1489             :     /* Initialize output arguments to "not present" */
    1490       70122 :     if (version)
    1491       69474 :         *version = NULL;
    1492       70122 :     if (encoding)
    1493           0 :         *encoding = NULL;
    1494       70122 :     if (standalone)
    1495       69474 :         *standalone = -1;
    1496             : 
    1497       70122 :     p = str;
    1498             : 
    1499       70122 :     if (xmlStrncmp(p, (xmlChar *) "<?xml", 5) != 0)
    1500       69900 :         goto finished;
    1501             : 
    1502             :     /*
    1503             :      * If next char is a name char, it's a PI like <?xml-stylesheet ...?>
    1504             :      * rather than an XMLDecl, so we have done what we came to do and found no
    1505             :      * XMLDecl.
    1506             :      *
    1507             :      * We need an input length value for xmlGetUTF8Char, but there's no need
    1508             :      * to count the whole document size, so use strnlen not strlen.
    1509             :      */
    1510         222 :     utf8len = strnlen((const char *) (p + 5), MAX_MULTIBYTE_CHAR_LEN);
    1511         222 :     utf8char = xmlGetUTF8Char(p + 5, &utf8len);
    1512         222 :     if (PG_XMLISNAMECHAR(utf8char))
    1513          12 :         goto finished;
    1514             : 
    1515         210 :     p += 5;
    1516             : 
    1517             :     /* version */
    1518         210 :     CHECK_XML_SPACE(p);
    1519         420 :     SKIP_XML_SPACE(p);
    1520         210 :     if (xmlStrncmp(p, (xmlChar *) "version", 7) != 0)
    1521           0 :         return XML_ERR_VERSION_MISSING;
    1522         210 :     p += 7;
    1523         210 :     SKIP_XML_SPACE(p);
    1524         210 :     if (*p != '=')
    1525           0 :         return XML_ERR_VERSION_MISSING;
    1526         210 :     p += 1;
    1527         210 :     SKIP_XML_SPACE(p);
    1528             : 
    1529         210 :     if (*p == '\'' || *p == '"')
    1530         210 :     {
    1531             :         const xmlChar *q;
    1532             : 
    1533         210 :         q = xmlStrchr(p + 1, *p);
    1534         210 :         if (!q)
    1535           0 :             return XML_ERR_VERSION_MISSING;
    1536             : 
    1537         210 :         if (version)
    1538         192 :             *version = xml_pnstrdup(p + 1, q - p - 1);
    1539         210 :         p = q + 1;
    1540             :     }
    1541             :     else
    1542           0 :         return XML_ERR_VERSION_MISSING;
    1543             : 
    1544             :     /* encoding */
    1545         210 :     save_p = p;
    1546         372 :     SKIP_XML_SPACE(p);
    1547         210 :     if (xmlStrncmp(p, (xmlChar *) "encoding", 8) == 0)
    1548             :     {
    1549          54 :         CHECK_XML_SPACE(save_p);
    1550          54 :         p += 8;
    1551          54 :         SKIP_XML_SPACE(p);
    1552          54 :         if (*p != '=')
    1553           0 :             return XML_ERR_MISSING_ENCODING;
    1554          54 :         p += 1;
    1555          54 :         SKIP_XML_SPACE(p);
    1556             : 
    1557          54 :         if (*p == '\'' || *p == '"')
    1558          54 :         {
    1559             :             const xmlChar *q;
    1560             : 
    1561          54 :             q = xmlStrchr(p + 1, *p);
    1562          54 :             if (!q)
    1563           0 :                 return XML_ERR_MISSING_ENCODING;
    1564             : 
    1565          54 :             if (encoding)
    1566           0 :                 *encoding = xml_pnstrdup(p + 1, q - p - 1);
    1567          54 :             p = q + 1;
    1568             :         }
    1569             :         else
    1570           0 :             return XML_ERR_MISSING_ENCODING;
    1571             :     }
    1572             :     else
    1573             :     {
    1574         156 :         p = save_p;
    1575             :     }
    1576             : 
    1577             :     /* standalone */
    1578         210 :     save_p = p;
    1579         318 :     SKIP_XML_SPACE(p);
    1580         210 :     if (xmlStrncmp(p, (xmlChar *) "standalone", 10) == 0)
    1581             :     {
    1582         108 :         CHECK_XML_SPACE(save_p);
    1583         108 :         p += 10;
    1584         108 :         SKIP_XML_SPACE(p);
    1585         108 :         if (*p != '=')
    1586           0 :             return XML_ERR_STANDALONE_VALUE;
    1587         108 :         p += 1;
    1588         108 :         SKIP_XML_SPACE(p);
    1589         216 :         if (xmlStrncmp(p, (xmlChar *) "'yes'", 5) == 0 ||
    1590         108 :             xmlStrncmp(p, (xmlChar *) "\"yes\"", 5) == 0)
    1591             :         {
    1592          60 :             if (standalone)
    1593          60 :                 *standalone = 1;
    1594          60 :             p += 5;
    1595             :         }
    1596          96 :         else if (xmlStrncmp(p, (xmlChar *) "'no'", 4) == 0 ||
    1597          48 :                  xmlStrncmp(p, (xmlChar *) "\"no\"", 4) == 0)
    1598             :         {
    1599          36 :             if (standalone)
    1600          36 :                 *standalone = 0;
    1601          36 :             p += 4;
    1602             :         }
    1603             :         else
    1604          12 :             return XML_ERR_STANDALONE_VALUE;
    1605             :     }
    1606             :     else
    1607             :     {
    1608         102 :         p = save_p;
    1609             :     }
    1610             : 
    1611         198 :     SKIP_XML_SPACE(p);
    1612         198 :     if (xmlStrncmp(p, (xmlChar *) "?>", 2) != 0)
    1613           0 :         return XML_ERR_XMLDECL_NOT_FINISHED;
    1614         198 :     p += 2;
    1615             : 
    1616       70110 : finished:
    1617       70110 :     len = p - str;
    1618             : 
    1619       76842 :     for (p = str; p < str + len; p++)
    1620        6732 :         if (*p > 127)
    1621           0 :             return XML_ERR_INVALID_CHAR;
    1622             : 
    1623       70110 :     if (lenp)
    1624       70110 :         *lenp = len;
    1625             : 
    1626       70110 :     return XML_ERR_OK;
    1627             : }
    1628             : 
    1629             : 
    1630             : /*
    1631             :  * Write an XML declaration.  On output, we adjust the XML declaration
    1632             :  * as follows.  (These rules are the moral equivalent of the clause
    1633             :  * "Serialization of an XML value" in the SQL standard.)
    1634             :  *
    1635             :  * We try to avoid generating an XML declaration if possible.  This is
    1636             :  * so that you don't get trivial things like xml '<foo/>' resulting in
    1637             :  * '<?xml version="1.0"?><foo/>', which would surely be annoying.  We
    1638             :  * must provide a declaration if the standalone property is specified
    1639             :  * or if we include an encoding declaration.  If we have a
    1640             :  * declaration, we must specify a version (XML requires this).
    1641             :  * Otherwise we only make a declaration if the version is not "1.0",
    1642             :  * which is the default version specified in SQL:2003.
    1643             :  */
    1644             : static bool
    1645       23816 : print_xml_decl(StringInfo buf, const xmlChar *version,
    1646             :                pg_enc encoding, int standalone)
    1647             : {
    1648       23816 :     if ((version && strcmp((const char *) version, PG_XML_DEFAULT_VERSION) != 0)
    1649       23780 :         || (encoding && encoding != PG_UTF8)
    1650       23780 :         || standalone != -1)
    1651             :     {
    1652          96 :         appendStringInfoString(buf, "<?xml");
    1653             : 
    1654          96 :         if (version)
    1655          72 :             appendStringInfo(buf, " version=\"%s\"", version);
    1656             :         else
    1657          24 :             appendStringInfo(buf, " version=\"%s\"", PG_XML_DEFAULT_VERSION);
    1658             : 
    1659          96 :         if (encoding && encoding != PG_UTF8)
    1660             :         {
    1661             :             /*
    1662             :              * XXX might be useful to convert this to IANA names (ISO-8859-1
    1663             :              * instead of LATIN1 etc.); needs field experience
    1664             :              */
    1665           0 :             appendStringInfo(buf, " encoding=\"%s\"",
    1666             :                              pg_encoding_to_char(encoding));
    1667             :         }
    1668             : 
    1669          96 :         if (standalone == 1)
    1670          48 :             appendStringInfoString(buf, " standalone=\"yes\"");
    1671          48 :         else if (standalone == 0)
    1672          24 :             appendStringInfoString(buf, " standalone=\"no\"");
    1673          96 :         appendStringInfoString(buf, "?>");
    1674             : 
    1675          96 :         return true;
    1676             :     }
    1677             :     else
    1678       23720 :         return false;
    1679             : }
    1680             : 
    1681             : /*
    1682             :  * Test whether an input that is to be parsed as CONTENT contains a DTD.
    1683             :  *
    1684             :  * The SQL/XML:2003 definition of CONTENT ("XMLDecl? content") is not
    1685             :  * satisfied by a document with a DTD, which is a bit of a wart, as it means
    1686             :  * the CONTENT type is not a proper superset of DOCUMENT.  SQL/XML:2006 and
    1687             :  * later fix that, by redefining content with reference to the "more
    1688             :  * permissive" Document Node of the XQuery/XPath Data Model, such that any
    1689             :  * DOCUMENT value is indeed also a CONTENT value.  That definition is more
    1690             :  * useful, as CONTENT becomes usable for parsing input of unknown form (think
    1691             :  * pg_restore).
    1692             :  *
    1693             :  * As used below in parse_xml when parsing for CONTENT, libxml does not give
    1694             :  * us the 2006+ behavior, but only the 2003; it will choke if the input has
    1695             :  * a DTD.  But we can provide the 2006+ definition of CONTENT easily enough,
    1696             :  * by detecting this case first and simply doing the parse as DOCUMENT.
    1697             :  *
    1698             :  * A DTD can be found arbitrarily far in, but that would be a contrived case;
    1699             :  * it will ordinarily start within a few dozen characters.  The only things
    1700             :  * that can precede it are an XMLDecl (here, the caller will have called
    1701             :  * parse_xml_decl already), whitespace, comments, and processing instructions.
    1702             :  * This function need only return true if it sees a valid sequence of such
    1703             :  * things leading to <!DOCTYPE.  It can simply return false in any other
    1704             :  * cases, including malformed input; that will mean the input gets parsed as
    1705             :  * CONTENT as originally planned, with libxml reporting any errors.
    1706             :  *
    1707             :  * This is only to be called from xml_parse, when pg_xml_init has already
    1708             :  * been called.  The input is already in UTF8 encoding.
    1709             :  */
    1710             : static bool
    1711        1014 : xml_doctype_in_content(const xmlChar *str)
    1712             : {
    1713        1014 :     const xmlChar *p = str;
    1714             : 
    1715             :     for (;;)
    1716          36 :     {
    1717             :         const xmlChar *e;
    1718             : 
    1719        1140 :         SKIP_XML_SPACE(p);
    1720        1050 :         if (*p != '<')
    1721         214 :             return false;
    1722         836 :         p++;
    1723             : 
    1724         836 :         if (*p == '!')
    1725             :         {
    1726          72 :             p++;
    1727             : 
    1728             :             /* if we see <!DOCTYPE, we can return true */
    1729          72 :             if (xmlStrncmp(p, (xmlChar *) "DOCTYPE", 7) == 0)
    1730          42 :                 return true;
    1731             : 
    1732             :             /* otherwise, if it's not a comment, fail */
    1733          30 :             if (xmlStrncmp(p, (xmlChar *) "--", 2) != 0)
    1734           0 :                 return false;
    1735             :             /* find end of comment: find -- and a > must follow */
    1736          30 :             p = xmlStrstr(p + 2, (xmlChar *) "--");
    1737          30 :             if (!p || p[2] != '>')
    1738           0 :                 return false;
    1739             :             /* advance over comment, and keep scanning */
    1740          30 :             p += 3;
    1741          30 :             continue;
    1742             :         }
    1743             : 
    1744             :         /* otherwise, if it's not a PI <?target something?>, fail */
    1745         764 :         if (*p != '?')
    1746         758 :             return false;
    1747           6 :         p++;
    1748             : 
    1749             :         /* find end of PI (the string ?> is forbidden within a PI) */
    1750           6 :         e = xmlStrstr(p, (xmlChar *) "?>");
    1751           6 :         if (!e)
    1752           0 :             return false;
    1753             : 
    1754             :         /* advance over PI, keep scanning */
    1755           6 :         p = e + 2;
    1756             :     }
    1757             : }
    1758             : 
    1759             : 
    1760             : /*
    1761             :  * Convert a text object to XML internal representation
    1762             :  *
    1763             :  * data is the source data (must not be toasted!), encoding is its encoding,
    1764             :  * and xmloption_arg and preserve_whitespace are options for the
    1765             :  * transformation.
    1766             :  *
    1767             :  * If parsed_xmloptiontype isn't NULL, *parsed_xmloptiontype is set to the
    1768             :  * XmlOptionType actually used to parse the input (typically the same as
    1769             :  * xmloption_arg, but a DOCTYPE node in the input can force DOCUMENT mode).
    1770             :  *
    1771             :  * If parsed_nodes isn't NULL and we parse in CONTENT mode, the list
    1772             :  * of parsed nodes from the xmlParseBalancedChunkMemory call will be returned
    1773             :  * to *parsed_nodes.  (It is caller's responsibility to free that.)
    1774             :  *
    1775             :  * Errors normally result in ereport(ERROR), but if escontext is an
    1776             :  * ErrorSaveContext, then "safe" errors are reported there instead, and the
    1777             :  * caller must check SOFT_ERROR_OCCURRED() to see whether that happened.
    1778             :  *
    1779             :  * Note: it is caller's responsibility to xmlFreeDoc() the result,
    1780             :  * else a permanent memory leak will ensue!  But note the result could
    1781             :  * be NULL after a soft error.
    1782             :  *
    1783             :  * TODO maybe libxml2's xmlreader is better? (do not construct DOM,
    1784             :  * yet do not use SAX - see xmlreader.c)
    1785             :  */
    1786             : static xmlDocPtr
    1787        1320 : xml_parse(text *data, XmlOptionType xmloption_arg,
    1788             :           bool preserve_whitespace, int encoding,
    1789             :           XmlOptionType *parsed_xmloptiontype, xmlNodePtr *parsed_nodes,
    1790             :           Node *escontext)
    1791             : {
    1792             :     int32       len;
    1793             :     xmlChar    *string;
    1794             :     xmlChar    *utf8string;
    1795             :     PgXmlErrorContext *xmlerrcxt;
    1796        1320 :     volatile xmlParserCtxtPtr ctxt = NULL;
    1797        1320 :     volatile xmlDocPtr doc = NULL;
    1798        1320 :     volatile int save_keep_blanks = -1;
    1799             : 
    1800             :     /*
    1801             :      * This step looks annoyingly redundant, but we must do it to have a
    1802             :      * null-terminated string in case encoding conversion isn't required.
    1803             :      */
    1804        1320 :     len = VARSIZE_ANY_EXHDR(data);  /* will be useful later */
    1805        1320 :     string = xml_text2xmlChar(data);
    1806             : 
    1807             :     /*
    1808             :      * If the data isn't UTF8, we must translate before giving it to libxml.
    1809             :      *
    1810             :      * XXX ideally, we'd catch any encoding conversion failure and return a
    1811             :      * soft error.  However, failure to convert to UTF8 should be pretty darn
    1812             :      * rare, so for now this is left undone.
    1813             :      */
    1814        1320 :     utf8string = pg_do_encoding_conversion(string,
    1815             :                                            len,
    1816             :                                            encoding,
    1817             :                                            PG_UTF8);
    1818             : 
    1819             :     /* Start up libxml and its parser */
    1820        1320 :     xmlerrcxt = pg_xml_init(PG_XML_STRICTNESS_WELLFORMED);
    1821             : 
    1822             :     /* Use a TRY block to ensure we clean up correctly */
    1823        1320 :     PG_TRY();
    1824             :     {
    1825        1320 :         bool        parse_as_document = false;
    1826             :         int         res_code;
    1827        1320 :         size_t      count = 0;
    1828        1320 :         xmlChar    *version = NULL;
    1829        1320 :         int         standalone = 0;
    1830             : 
    1831             :         /* Any errors here are reported as hard ereport's */
    1832        1320 :         xmlInitParser();
    1833             : 
    1834             :         /* Decide whether to parse as document or content */
    1835        1320 :         if (xmloption_arg == XMLOPTION_DOCUMENT)
    1836         294 :             parse_as_document = true;
    1837             :         else
    1838             :         {
    1839             :             /* Parse and skip over the XML declaration, if any */
    1840        1026 :             res_code = parse_xml_decl(utf8string,
    1841             :                                       &count, &version, NULL, &standalone);
    1842        1026 :             if (res_code != 0)
    1843             :             {
    1844          12 :                 errsave(escontext,
    1845             :                         errcode(ERRCODE_INVALID_XML_CONTENT),
    1846             :                         errmsg_internal("invalid XML content: invalid XML declaration"),
    1847             :                         errdetail_for_xml_code(res_code));
    1848          12 :                 goto fail;
    1849             :             }
    1850             : 
    1851             :             /* Is there a DOCTYPE element? */
    1852        1014 :             if (xml_doctype_in_content(utf8string + count))
    1853          42 :                 parse_as_document = true;
    1854             :         }
    1855             : 
    1856             :         /* initialize output parameters */
    1857        1308 :         if (parsed_xmloptiontype != NULL)
    1858         144 :             *parsed_xmloptiontype = parse_as_document ? XMLOPTION_DOCUMENT :
    1859             :                 XMLOPTION_CONTENT;
    1860        1308 :         if (parsed_nodes != NULL)
    1861         144 :             *parsed_nodes = NULL;
    1862             : 
    1863        1308 :         if (parse_as_document)
    1864             :         {
    1865             :             int         options;
    1866             : 
    1867             :             /* set up parser context used by xmlCtxtReadDoc */
    1868         336 :             ctxt = xmlNewParserCtxt();
    1869         336 :             if (ctxt == NULL || xmlerrcxt->err_occurred)
    1870           0 :                 xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
    1871             :                             "could not allocate parser context");
    1872             : 
    1873             :             /*
    1874             :              * Select parse options.
    1875             :              *
    1876             :              * Note that here we try to apply DTD defaults (XML_PARSE_DTDATTR)
    1877             :              * according to SQL/XML:2008 GR 10.16.7.d: 'Default values defined
    1878             :              * by internal DTD are applied'.  As for external DTDs, we try to
    1879             :              * support them too (see SQL/XML:2008 GR 10.16.7.e), but that
    1880             :              * doesn't really happen because xmlPgEntityLoader prevents it.
    1881             :              */
    1882         336 :             options = XML_PARSE_NOENT | XML_PARSE_DTDATTR
    1883         336 :                 | (preserve_whitespace ? 0 : XML_PARSE_NOBLANKS);
    1884             : 
    1885         336 :             doc = xmlCtxtReadDoc(ctxt, utf8string,
    1886             :                                  NULL,  /* no URL */
    1887             :                                  "UTF-8",
    1888             :                                  options);
    1889             : 
    1890         336 :             if (doc == NULL || xmlerrcxt->err_occurred)
    1891             :             {
    1892             :                 /* Use original option to decide which error code to report */
    1893         144 :                 if (xmloption_arg == XMLOPTION_DOCUMENT)
    1894         138 :                     xml_errsave(escontext, xmlerrcxt,
    1895             :                                 ERRCODE_INVALID_XML_DOCUMENT,
    1896             :                                 "invalid XML document");
    1897             :                 else
    1898           6 :                     xml_errsave(escontext, xmlerrcxt,
    1899             :                                 ERRCODE_INVALID_XML_CONTENT,
    1900             :                                 "invalid XML content");
    1901          96 :                 goto fail;
    1902             :             }
    1903             :         }
    1904             :         else
    1905             :         {
    1906             :             /* set up document that xmlParseBalancedChunkMemory will add to */
    1907         972 :             doc = xmlNewDoc(version);
    1908         972 :             if (doc == NULL || xmlerrcxt->err_occurred)
    1909           0 :                 xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
    1910             :                             "could not allocate XML document");
    1911             : 
    1912             :             Assert(doc->encoding == NULL);
    1913         972 :             doc->encoding = xmlStrdup((const xmlChar *) "UTF-8");
    1914         972 :             if (doc->encoding == NULL || xmlerrcxt->err_occurred)
    1915           0 :                 xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
    1916             :                             "could not allocate XML document");
    1917         972 :             doc->standalone = standalone;
    1918             : 
    1919             :             /* set parse options --- have to do this the ugly way */
    1920         972 :             save_keep_blanks = xmlKeepBlanksDefault(preserve_whitespace ? 1 : 0);
    1921             : 
    1922             :             /* allow empty content */
    1923         972 :             if (*(utf8string + count))
    1924             :             {
    1925        1896 :                 res_code = xmlParseBalancedChunkMemory(doc, NULL, NULL, 0,
    1926         948 :                                                        utf8string + count,
    1927             :                                                        parsed_nodes);
    1928         948 :                 if (res_code != 0 || xmlerrcxt->err_occurred)
    1929             :                 {
    1930          60 :                     xml_errsave(escontext, xmlerrcxt,
    1931             :                                 ERRCODE_INVALID_XML_CONTENT,
    1932             :                                 "invalid XML content");
    1933          12 :                     goto fail;
    1934             :                 }
    1935             :             }
    1936             :         }
    1937             : 
    1938        1224 : fail:
    1939             :         ;
    1940             :     }
    1941          96 :     PG_CATCH();
    1942             :     {
    1943          96 :         if (save_keep_blanks != -1)
    1944          48 :             xmlKeepBlanksDefault(save_keep_blanks);
    1945          96 :         if (doc != NULL)
    1946          48 :             xmlFreeDoc(doc);
    1947          96 :         if (ctxt != NULL)
    1948          48 :             xmlFreeParserCtxt(ctxt);
    1949             : 
    1950          96 :         pg_xml_done(xmlerrcxt, true);
    1951             : 
    1952          96 :         PG_RE_THROW();
    1953             :     }
    1954        1224 :     PG_END_TRY();
    1955             : 
    1956        1224 :     if (save_keep_blanks != -1)
    1957         924 :         xmlKeepBlanksDefault(save_keep_blanks);
    1958             : 
    1959        1224 :     if (ctxt != NULL)
    1960         288 :         xmlFreeParserCtxt(ctxt);
    1961             : 
    1962        1224 :     pg_xml_done(xmlerrcxt, false);
    1963             : 
    1964        1224 :     return doc;
    1965             : }
    1966             : 
    1967             : 
    1968             : /*
    1969             :  * xmlChar<->text conversions
    1970             :  */
    1971             : static xmlChar *
    1972        1470 : xml_text2xmlChar(text *in)
    1973             : {
    1974        1470 :     return (xmlChar *) text_to_cstring(in);
    1975             : }
    1976             : 
    1977             : 
    1978             : #ifdef USE_LIBXMLCONTEXT
    1979             : 
    1980             : /*
    1981             :  * Manage the special context used for all libxml allocations (but only
    1982             :  * in special debug builds; see notes at top of file)
    1983             :  */
    1984             : static void
    1985             : xml_memory_init(void)
    1986             : {
    1987             :     /* Create memory context if not there already */
    1988             :     if (LibxmlContext == NULL)
    1989             :         LibxmlContext = AllocSetContextCreate(TopMemoryContext,
    1990             :                                               "Libxml context",
    1991             :                                               ALLOCSET_DEFAULT_SIZES);
    1992             : 
    1993             :     /* Re-establish the callbacks even if already set */
    1994             :     xmlMemSetup(xml_pfree, xml_palloc, xml_repalloc, xml_pstrdup);
    1995             : }
    1996             : 
    1997             : /*
    1998             :  * Wrappers for memory management functions
    1999             :  */
    2000             : static void *
    2001             : xml_palloc(size_t size)
    2002             : {
    2003             :     return MemoryContextAlloc(LibxmlContext, size);
    2004             : }
    2005             : 
    2006             : 
    2007             : static void *
    2008             : xml_repalloc(void *ptr, size_t size)
    2009             : {
    2010             :     return repalloc(ptr, size);
    2011             : }
    2012             : 
    2013             : 
    2014             : static void
    2015             : xml_pfree(void *ptr)
    2016             : {
    2017             :     /* At least some parts of libxml assume xmlFree(NULL) is allowed */
    2018             :     if (ptr)
    2019             :         pfree(ptr);
    2020             : }
    2021             : 
    2022             : 
    2023             : static char *
    2024             : xml_pstrdup(const char *string)
    2025             : {
    2026             :     return MemoryContextStrdup(LibxmlContext, string);
    2027             : }
    2028             : #endif                          /* USE_LIBXMLCONTEXT */
    2029             : 
    2030             : 
    2031             : /*
    2032             :  * xmlPgEntityLoader --- entity loader callback function
    2033             :  *
    2034             :  * Silently prevent any external entity URL from being loaded.  We don't want
    2035             :  * to throw an error, so instead make the entity appear to expand to an empty
    2036             :  * string.
    2037             :  *
    2038             :  * We would prefer to allow loading entities that exist in the system's
    2039             :  * global XML catalog; but the available libxml2 APIs make that a complex
    2040             :  * and fragile task.  For now, just shut down all external access.
    2041             :  */
    2042             : static xmlParserInputPtr
    2043          18 : xmlPgEntityLoader(const char *URL, const char *ID,
    2044             :                   xmlParserCtxtPtr ctxt)
    2045             : {
    2046          18 :     return xmlNewStringInputStream(ctxt, (const xmlChar *) "");
    2047             : }
    2048             : 
    2049             : 
    2050             : /*
    2051             :  * xml_ereport --- report an XML-related error
    2052             :  *
    2053             :  * The "msg" is the SQL-level message; some can be adopted from the SQL/XML
    2054             :  * standard.  This function adds libxml's native error message, if any, as
    2055             :  * detail.
    2056             :  *
    2057             :  * This is exported for modules that want to share the core libxml error
    2058             :  * handler.  Note that pg_xml_init() *must* have been called previously.
    2059             :  */
    2060             : void
    2061          12 : xml_ereport(PgXmlErrorContext *errcxt, int level, int sqlcode, const char *msg)
    2062             : {
    2063             :     char       *detail;
    2064             : 
    2065             :     /* Defend against someone passing us a bogus context struct */
    2066          12 :     if (errcxt->magic != ERRCXT_MAGIC)
    2067           0 :         elog(ERROR, "xml_ereport called with invalid PgXmlErrorContext");
    2068             : 
    2069             :     /* Flag that the current libxml error has been reported */
    2070          12 :     errcxt->err_occurred = false;
    2071             : 
    2072             :     /* Include detail only if we have some text from libxml */
    2073          12 :     if (errcxt->err_buf.len > 0)
    2074          12 :         detail = errcxt->err_buf.data;
    2075             :     else
    2076           0 :         detail = NULL;
    2077             : 
    2078          12 :     ereport(level,
    2079             :             (errcode(sqlcode),
    2080             :              errmsg_internal("%s", msg),
    2081             :              detail ? errdetail_internal("%s", detail) : 0));
    2082           0 : }
    2083             : 
    2084             : 
    2085             : /*
    2086             :  * xml_errsave --- save an XML-related error
    2087             :  *
    2088             :  * If escontext is an ErrorSaveContext, error details are saved into it,
    2089             :  * and control returns normally.
    2090             :  *
    2091             :  * Otherwise, the error is thrown, so that this is equivalent to
    2092             :  * xml_ereport() with level == ERROR.
    2093             :  *
    2094             :  * This should be used only for errors that we're sure we do not need
    2095             :  * a transaction abort to clean up after.
    2096             :  */
    2097             : static void
    2098         204 : xml_errsave(Node *escontext, PgXmlErrorContext *errcxt,
    2099             :             int sqlcode, const char *msg)
    2100             : {
    2101             :     char       *detail;
    2102             : 
    2103             :     /* Defend against someone passing us a bogus context struct */
    2104         204 :     if (errcxt->magic != ERRCXT_MAGIC)
    2105           0 :         elog(ERROR, "xml_errsave called with invalid PgXmlErrorContext");
    2106             : 
    2107             :     /* Flag that the current libxml error has been reported */
    2108         204 :     errcxt->err_occurred = false;
    2109             : 
    2110             :     /* Include detail only if we have some text from libxml */
    2111         204 :     if (errcxt->err_buf.len > 0)
    2112         204 :         detail = errcxt->err_buf.data;
    2113             :     else
    2114           0 :         detail = NULL;
    2115             : 
    2116         204 :     errsave(escontext,
    2117             :             (errcode(sqlcode),
    2118             :              errmsg_internal("%s", msg),
    2119             :              detail ? errdetail_internal("%s", detail) : 0));
    2120         108 : }
    2121             : 
    2122             : 
    2123             : /*
    2124             :  * Error handler for libxml errors and warnings
    2125             :  */
    2126             : static void
    2127         398 : xml_errorHandler(void *data, PgXmlErrorPtr error)
    2128             : {
    2129         398 :     PgXmlErrorContext *xmlerrcxt = (PgXmlErrorContext *) data;
    2130         398 :     xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) error->ctxt;
    2131         398 :     xmlParserInputPtr input = (ctxt != NULL) ? ctxt->input : NULL;
    2132         398 :     xmlNodePtr  node = error->node;
    2133         398 :     const xmlChar *name = (node != NULL &&
    2134         398 :                            node->type == XML_ELEMENT_NODE) ? node->name : NULL;
    2135         398 :     int         domain = error->domain;
    2136         398 :     int         level = error->level;
    2137             :     StringInfo  errorBuf;
    2138             : 
    2139             :     /*
    2140             :      * Defend against someone passing us a bogus context struct.
    2141             :      *
    2142             :      * We force a backend exit if this check fails because longjmp'ing out of
    2143             :      * libxml would likely render it unsafe to use further.
    2144             :      */
    2145         398 :     if (xmlerrcxt->magic != ERRCXT_MAGIC)
    2146           0 :         elog(FATAL, "xml_errorHandler called with invalid PgXmlErrorContext");
    2147             : 
    2148             :     /*----------
    2149             :      * Older libxml versions report some errors differently.
    2150             :      * First, some errors were previously reported as coming from the parser
    2151             :      * domain but are now reported as coming from the namespace domain.
    2152             :      * Second, some warnings were upgraded to errors.
    2153             :      * We attempt to compensate for that here.
    2154             :      *----------
    2155             :      */
    2156         398 :     switch (error->code)
    2157             :     {
    2158          30 :         case XML_WAR_NS_URI:
    2159          30 :             level = XML_ERR_ERROR;
    2160          30 :             domain = XML_FROM_NAMESPACE;
    2161          30 :             break;
    2162             : 
    2163          54 :         case XML_ERR_NS_DECL_ERROR:
    2164             :         case XML_WAR_NS_URI_RELATIVE:
    2165             :         case XML_WAR_NS_COLUMN:
    2166             :         case XML_NS_ERR_XML_NAMESPACE:
    2167             :         case XML_NS_ERR_UNDEFINED_NAMESPACE:
    2168             :         case XML_NS_ERR_QNAME:
    2169             :         case XML_NS_ERR_ATTRIBUTE_REDEFINED:
    2170             :         case XML_NS_ERR_EMPTY:
    2171          54 :             domain = XML_FROM_NAMESPACE;
    2172          54 :             break;
    2173             :     }
    2174             : 
    2175             :     /* Decide whether to act on the error or not */
    2176         398 :     switch (domain)
    2177             :     {
    2178         314 :         case XML_FROM_PARSER:
    2179             : 
    2180             :             /*
    2181             :              * XML_ERR_NOT_WELL_BALANCED is typically reported after some
    2182             :              * other, more on-point error.  Furthermore, libxml2 2.13 reports
    2183             :              * it under a completely different set of rules than prior
    2184             :              * versions.  To avoid cross-version behavioral differences,
    2185             :              * suppress it so long as we already logged some error.
    2186             :              */
    2187         314 :             if (error->code == XML_ERR_NOT_WELL_BALANCED &&
    2188          30 :                 xmlerrcxt->err_occurred)
    2189          30 :                 return;
    2190             :             /* fall through */
    2191             : 
    2192             :         case XML_FROM_NONE:
    2193             :         case XML_FROM_MEMORY:
    2194             :         case XML_FROM_IO:
    2195             : 
    2196             :             /*
    2197             :              * Suppress warnings about undeclared entities.  We need to do
    2198             :              * this to avoid problems due to not loading DTD definitions.
    2199             :              */
    2200         284 :             if (error->code == XML_WAR_UNDECLARED_ENTITY)
    2201           6 :                 return;
    2202             : 
    2203             :             /* Otherwise, accept error regardless of the parsing purpose */
    2204         278 :             break;
    2205             : 
    2206          84 :         default:
    2207             :             /* Ignore error if only doing well-formedness check */
    2208          84 :             if (xmlerrcxt->strictness == PG_XML_STRICTNESS_WELLFORMED)
    2209          66 :                 return;
    2210          18 :             break;
    2211             :     }
    2212             : 
    2213             :     /* Prepare error message in errorBuf */
    2214         296 :     errorBuf = makeStringInfo();
    2215             : 
    2216         296 :     if (error->line > 0)
    2217         296 :         appendStringInfo(errorBuf, "line %d: ", error->line);
    2218         296 :     if (name != NULL)
    2219           0 :         appendStringInfo(errorBuf, "element %s: ", name);
    2220         296 :     if (error->message != NULL)
    2221         296 :         appendStringInfoString(errorBuf, error->message);
    2222             :     else
    2223           0 :         appendStringInfoString(errorBuf, "(no message provided)");
    2224             : 
    2225             :     /*
    2226             :      * Append context information to errorBuf.
    2227             :      *
    2228             :      * xmlParserPrintFileContext() uses libxml's "generic" error handler to
    2229             :      * write the context.  Since we don't want to duplicate libxml
    2230             :      * functionality here, we set up a generic error handler temporarily.
    2231             :      *
    2232             :      * We use appendStringInfo() directly as libxml's generic error handler.
    2233             :      * This should work because it has essentially the same signature as
    2234             :      * libxml expects, namely (void *ptr, const char *msg, ...).
    2235             :      */
    2236         296 :     if (input != NULL)
    2237             :     {
    2238         296 :         xmlGenericErrorFunc errFuncSaved = xmlGenericError;
    2239         296 :         void       *errCtxSaved = xmlGenericErrorContext;
    2240             : 
    2241         296 :         xmlSetGenericErrorFunc(errorBuf,
    2242             :                                (xmlGenericErrorFunc) appendStringInfo);
    2243             : 
    2244             :         /* Add context information to errorBuf */
    2245         296 :         appendStringInfoLineSeparator(errorBuf);
    2246             : 
    2247         296 :         xmlParserPrintFileContext(input);
    2248             : 
    2249             :         /* Restore generic error func */
    2250         296 :         xmlSetGenericErrorFunc(errCtxSaved, errFuncSaved);
    2251             :     }
    2252             : 
    2253             :     /* Get rid of any trailing newlines in errorBuf */
    2254         296 :     chopStringInfoNewlines(errorBuf);
    2255             : 
    2256             :     /*
    2257             :      * Legacy error handling mode.  err_occurred is never set, we just add the
    2258             :      * message to err_buf.  This mode exists because the xml2 contrib module
    2259             :      * uses our error-handling infrastructure, but we don't want to change its
    2260             :      * behaviour since it's deprecated anyway.  This is also why we don't
    2261             :      * distinguish between notices, warnings and errors here --- the old-style
    2262             :      * generic error handler wouldn't have done that either.
    2263             :      */
    2264         296 :     if (xmlerrcxt->strictness == PG_XML_STRICTNESS_LEGACY)
    2265             :     {
    2266           2 :         appendStringInfoLineSeparator(&xmlerrcxt->err_buf);
    2267           2 :         appendBinaryStringInfo(&xmlerrcxt->err_buf, errorBuf->data,
    2268             :                                errorBuf->len);
    2269             : 
    2270           2 :         destroyStringInfo(errorBuf);
    2271           2 :         return;
    2272             :     }
    2273             : 
    2274             :     /*
    2275             :      * We don't want to ereport() here because that'd probably leave libxml in
    2276             :      * an inconsistent state.  Instead, we remember the error and ereport()
    2277             :      * from xml_ereport().
    2278             :      *
    2279             :      * Warnings and notices can be reported immediately since they won't cause
    2280             :      * a longjmp() out of libxml.
    2281             :      */
    2282         294 :     if (level >= XML_ERR_ERROR)
    2283             :     {
    2284         288 :         appendStringInfoLineSeparator(&xmlerrcxt->err_buf);
    2285         288 :         appendBinaryStringInfo(&xmlerrcxt->err_buf, errorBuf->data,
    2286             :                                errorBuf->len);
    2287             : 
    2288         288 :         xmlerrcxt->err_occurred = true;
    2289             :     }
    2290           6 :     else if (level >= XML_ERR_WARNING)
    2291             :     {
    2292           6 :         ereport(WARNING,
    2293             :                 (errmsg_internal("%s", errorBuf->data)));
    2294             :     }
    2295             :     else
    2296             :     {
    2297           0 :         ereport(NOTICE,
    2298             :                 (errmsg_internal("%s", errorBuf->data)));
    2299             :     }
    2300             : 
    2301         294 :     destroyStringInfo(errorBuf);
    2302             : }
    2303             : 
    2304             : 
    2305             : /*
    2306             :  * Convert libxml error codes into textual errdetail messages.
    2307             :  *
    2308             :  * This should be called within an ereport or errsave invocation,
    2309             :  * just as errdetail would be.
    2310             :  *
    2311             :  * At the moment, we only need to cover those codes that we
    2312             :  * may raise in this file.
    2313             :  */
    2314             : static int
    2315           6 : errdetail_for_xml_code(int code)
    2316             : {
    2317             :     const char *det;
    2318             : 
    2319           6 :     switch (code)
    2320             :     {
    2321           0 :         case XML_ERR_INVALID_CHAR:
    2322           0 :             det = gettext_noop("Invalid character value.");
    2323           0 :             break;
    2324           0 :         case XML_ERR_SPACE_REQUIRED:
    2325           0 :             det = gettext_noop("Space required.");
    2326           0 :             break;
    2327           6 :         case XML_ERR_STANDALONE_VALUE:
    2328           6 :             det = gettext_noop("standalone accepts only 'yes' or 'no'.");
    2329           6 :             break;
    2330           0 :         case XML_ERR_VERSION_MISSING:
    2331           0 :             det = gettext_noop("Malformed declaration: missing version.");
    2332           0 :             break;
    2333           0 :         case XML_ERR_MISSING_ENCODING:
    2334           0 :             det = gettext_noop("Missing encoding in text declaration.");
    2335           0 :             break;
    2336           0 :         case XML_ERR_XMLDECL_NOT_FINISHED:
    2337           0 :             det = gettext_noop("Parsing XML declaration: '?>' expected.");
    2338           0 :             break;
    2339           0 :         default:
    2340           0 :             det = gettext_noop("Unrecognized libxml error code: %d.");
    2341           0 :             break;
    2342             :     }
    2343             : 
    2344           6 :     return errdetail(det, code);
    2345             : }
    2346             : 
    2347             : 
    2348             : /*
    2349             :  * Remove all trailing newlines from a StringInfo string
    2350             :  */
    2351             : static void
    2352         882 : chopStringInfoNewlines(StringInfo str)
    2353             : {
    2354        1474 :     while (str->len > 0 && str->data[str->len - 1] == '\n')
    2355         592 :         str->data[--str->len] = '\0';
    2356         882 : }
    2357             : 
    2358             : 
    2359             : /*
    2360             :  * Append a newline after removing any existing trailing newlines
    2361             :  */
    2362             : static void
    2363         586 : appendStringInfoLineSeparator(StringInfo str)
    2364             : {
    2365         586 :     chopStringInfoNewlines(str);
    2366         586 :     if (str->len > 0)
    2367         368 :         appendStringInfoChar(str, '\n');
    2368         586 : }
    2369             : 
    2370             : 
    2371             : /*
    2372             :  * Convert one char in the current server encoding to a Unicode codepoint.
    2373             :  */
    2374             : static pg_wchar
    2375       18350 : sqlchar_to_unicode(const char *s)
    2376             : {
    2377             :     char       *utf8string;
    2378             :     pg_wchar    ret[2];         /* need space for trailing zero */
    2379             : 
    2380             :     /* note we're not assuming s is null-terminated */
    2381       18350 :     utf8string = pg_server_to_any(s, pg_mblen(s), PG_UTF8);
    2382             : 
    2383       18350 :     pg_encoding_mb2wchar_with_len(PG_UTF8, utf8string, ret,
    2384             :                                   pg_encoding_mblen(PG_UTF8, utf8string));
    2385             : 
    2386       18350 :     if (utf8string != s)
    2387           0 :         pfree(utf8string);
    2388             : 
    2389       18350 :     return ret[0];
    2390             : }
    2391             : 
    2392             : 
    2393             : static bool
    2394        3652 : is_valid_xml_namefirst(pg_wchar c)
    2395             : {
    2396             :     /* (Letter | '_' | ':') */
    2397        3658 :     return (xmlIsBaseCharQ(c) || xmlIsIdeographicQ(c)
    2398        7310 :             || c == '_' || c == ':');
    2399             : }
    2400             : 
    2401             : 
    2402             : static bool
    2403       14698 : is_valid_xml_namechar(pg_wchar c)
    2404             : {
    2405             :     /* Letter | Digit | '.' | '-' | '_' | ':' | CombiningChar | Extender */
    2406       15590 :     return (xmlIsBaseCharQ(c) || xmlIsIdeographicQ(c)
    2407         892 :             || xmlIsDigitQ(c)
    2408         256 :             || c == '.' || c == '-' || c == '_' || c == ':'
    2409          12 :             || xmlIsCombiningQ(c)
    2410       31180 :             || xmlIsExtenderQ(c));
    2411             : }
    2412             : #endif                          /* USE_LIBXML */
    2413             : 
    2414             : 
    2415             : /*
    2416             :  * Map SQL identifier to XML name; see SQL/XML:2008 section 9.1.
    2417             :  */
    2418             : char *
    2419        3668 : map_sql_identifier_to_xml_name(const char *ident, bool fully_escaped,
    2420             :                                bool escape_period)
    2421             : {
    2422             : #ifdef USE_LIBXML
    2423             :     StringInfoData buf;
    2424             :     const char *p;
    2425             : 
    2426             :     /*
    2427             :      * SQL/XML doesn't make use of this case anywhere, so it's probably a
    2428             :      * mistake.
    2429             :      */
    2430             :     Assert(fully_escaped || !escape_period);
    2431             : 
    2432        3668 :     initStringInfo(&buf);
    2433             : 
    2434       22040 :     for (p = ident; *p; p += pg_mblen(p))
    2435             :     {
    2436       18372 :         if (*p == ':' && (p == ident || fully_escaped))
    2437          16 :             appendStringInfoString(&buf, "_x003A_");
    2438       18356 :         else if (*p == '_' && *(p + 1) == 'x')
    2439           6 :             appendStringInfoString(&buf, "_x005F_");
    2440       21638 :         else if (fully_escaped && p == ident &&
    2441        3288 :                  pg_strncasecmp(p, "xml", 3) == 0)
    2442             :         {
    2443           0 :             if (*p == 'x')
    2444           0 :                 appendStringInfoString(&buf, "_x0078_");
    2445             :             else
    2446           0 :                 appendStringInfoString(&buf, "_x0058_");
    2447             :         }
    2448       18350 :         else if (escape_period && *p == '.')
    2449           0 :             appendStringInfoString(&buf, "_x002E_");
    2450             :         else
    2451             :         {
    2452       18350 :             pg_wchar    u = sqlchar_to_unicode(p);
    2453             : 
    2454       36700 :             if ((p == ident)
    2455        3652 :                 ? !is_valid_xml_namefirst(u)
    2456       14698 :                 : !is_valid_xml_namechar(u))
    2457          18 :                 appendStringInfo(&buf, "_x%04X_", (unsigned int) u);
    2458             :             else
    2459       18332 :                 appendBinaryStringInfo(&buf, p, pg_mblen(p));
    2460             :         }
    2461             :     }
    2462             : 
    2463        3668 :     return buf.data;
    2464             : #else                           /* not USE_LIBXML */
    2465             :     NO_XML_SUPPORT();
    2466             :     return NULL;
    2467             : #endif                          /* not USE_LIBXML */
    2468             : }
    2469             : 
    2470             : 
    2471             : /*
    2472             :  * Map XML name to SQL identifier; see SQL/XML:2008 section 9.3.
    2473             :  */
    2474             : char *
    2475         176 : map_xml_name_to_sql_identifier(const char *name)
    2476             : {
    2477             :     StringInfoData buf;
    2478             :     const char *p;
    2479             : 
    2480         176 :     initStringInfo(&buf);
    2481             : 
    2482         968 :     for (p = name; *p; p += pg_mblen(p))
    2483             :     {
    2484         792 :         if (*p == '_' && *(p + 1) == 'x'
    2485          22 :             && isxdigit((unsigned char) *(p + 2))
    2486          22 :             && isxdigit((unsigned char) *(p + 3))
    2487          22 :             && isxdigit((unsigned char) *(p + 4))
    2488          22 :             && isxdigit((unsigned char) *(p + 5))
    2489          22 :             && *(p + 6) == '_')
    2490          22 :         {
    2491             :             char        cbuf[MAX_UNICODE_EQUIVALENT_STRING + 1];
    2492             :             unsigned int u;
    2493             : 
    2494          22 :             sscanf(p + 2, "%X", &u);
    2495          22 :             pg_unicode_to_server(u, (unsigned char *) cbuf);
    2496          22 :             appendStringInfoString(&buf, cbuf);
    2497          22 :             p += 6;
    2498             :         }
    2499             :         else
    2500         770 :             appendBinaryStringInfo(&buf, p, pg_mblen(p));
    2501             :     }
    2502             : 
    2503         176 :     return buf.data;
    2504             : }
    2505             : 
    2506             : /*
    2507             :  * Map SQL value to XML value; see SQL/XML:2008 section 9.8.
    2508             :  *
    2509             :  * When xml_escape_strings is true, then certain characters in string
    2510             :  * values are replaced by entity references (&lt; etc.), as specified
    2511             :  * in SQL/XML:2008 section 9.8 GR 9) a) iii).   This is normally what is
    2512             :  * wanted.  The false case is mainly useful when the resulting value
    2513             :  * is used with xmlTextWriterWriteAttribute() to write out an
    2514             :  * attribute, because that function does the escaping itself.
    2515             :  */
    2516             : char *
    2517      134916 : map_sql_value_to_xml_value(Datum value, Oid type, bool xml_escape_strings)
    2518             : {
    2519      134916 :     if (type_is_array_domain(type))
    2520             :     {
    2521             :         ArrayType  *array;
    2522             :         Oid         elmtype;
    2523             :         int16       elmlen;
    2524             :         bool        elmbyval;
    2525             :         char        elmalign;
    2526             :         int         num_elems;
    2527             :         Datum      *elem_values;
    2528             :         bool       *elem_nulls;
    2529             :         StringInfoData buf;
    2530             :         int         i;
    2531             : 
    2532           6 :         array = DatumGetArrayTypeP(value);
    2533           6 :         elmtype = ARR_ELEMTYPE(array);
    2534           6 :         get_typlenbyvalalign(elmtype, &elmlen, &elmbyval, &elmalign);
    2535             : 
    2536           6 :         deconstruct_array(array, elmtype,
    2537             :                           elmlen, elmbyval, elmalign,
    2538             :                           &elem_values, &elem_nulls,
    2539             :                           &num_elems);
    2540             : 
    2541           6 :         initStringInfo(&buf);
    2542             : 
    2543          24 :         for (i = 0; i < num_elems; i++)
    2544             :         {
    2545          18 :             if (elem_nulls[i])
    2546           0 :                 continue;
    2547          18 :             appendStringInfoString(&buf, "<element>");
    2548          18 :             appendStringInfoString(&buf,
    2549          18 :                                    map_sql_value_to_xml_value(elem_values[i],
    2550             :                                                               elmtype, true));
    2551          18 :             appendStringInfoString(&buf, "</element>");
    2552             :         }
    2553             : 
    2554           6 :         pfree(elem_values);
    2555           6 :         pfree(elem_nulls);
    2556             : 
    2557           6 :         return buf.data;
    2558             :     }
    2559             :     else
    2560             :     {
    2561             :         Oid         typeOut;
    2562             :         bool        isvarlena;
    2563             :         char       *str;
    2564             : 
    2565             :         /*
    2566             :          * Flatten domains; the special-case treatments below should apply to,
    2567             :          * eg, domains over boolean not just boolean.
    2568             :          */
    2569      134910 :         type = getBaseType(type);
    2570             : 
    2571             :         /*
    2572             :          * Special XSD formatting for some data types
    2573             :          */
    2574      134910 :         switch (type)
    2575             :         {
    2576          66 :             case BOOLOID:
    2577          66 :                 if (DatumGetBool(value))
    2578          60 :                     return "true";
    2579             :                 else
    2580           6 :                     return "false";
    2581             : 
    2582          48 :             case DATEOID:
    2583             :                 {
    2584             :                     DateADT     date;
    2585             :                     struct pg_tm tm;
    2586             :                     char        buf[MAXDATELEN + 1];
    2587             : 
    2588          48 :                     date = DatumGetDateADT(value);
    2589             :                     /* XSD doesn't support infinite values */
    2590          48 :                     if (DATE_NOT_FINITE(date))
    2591           0 :                         ereport(ERROR,
    2592             :                                 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
    2593             :                                  errmsg("date out of range"),
    2594             :                                  errdetail("XML does not support infinite date values.")));
    2595          48 :                     j2date(date + POSTGRES_EPOCH_JDATE,
    2596             :                            &(tm.tm_year), &(tm.tm_mon), &(tm.tm_mday));
    2597          48 :                     EncodeDateOnly(&tm, USE_XSD_DATES, buf);
    2598             : 
    2599          48 :                     return pstrdup(buf);
    2600             :                 }
    2601             : 
    2602          36 :             case TIMESTAMPOID:
    2603             :                 {
    2604             :                     Timestamp   timestamp;
    2605             :                     struct pg_tm tm;
    2606             :                     fsec_t      fsec;
    2607             :                     char        buf[MAXDATELEN + 1];
    2608             : 
    2609          36 :                     timestamp = DatumGetTimestamp(value);
    2610             : 
    2611             :                     /* XSD doesn't support infinite values */
    2612          36 :                     if (TIMESTAMP_NOT_FINITE(timestamp))
    2613           6 :                         ereport(ERROR,
    2614             :                                 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
    2615             :                                  errmsg("timestamp out of range"),
    2616             :                                  errdetail("XML does not support infinite timestamp values.")));
    2617          30 :                     else if (timestamp2tm(timestamp, NULL, &tm, &fsec, NULL, NULL) == 0)
    2618          30 :                         EncodeDateTime(&tm, fsec, false, 0, NULL, USE_XSD_DATES, buf);
    2619             :                     else
    2620           0 :                         ereport(ERROR,
    2621             :                                 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
    2622             :                                  errmsg("timestamp out of range")));
    2623             : 
    2624          30 :                     return pstrdup(buf);
    2625             :                 }
    2626             : 
    2627          24 :             case TIMESTAMPTZOID:
    2628             :                 {
    2629             :                     TimestampTz timestamp;
    2630             :                     struct pg_tm tm;
    2631             :                     int         tz;
    2632             :                     fsec_t      fsec;
    2633          24 :                     const char *tzn = NULL;
    2634             :                     char        buf[MAXDATELEN + 1];
    2635             : 
    2636          24 :                     timestamp = DatumGetTimestamp(value);
    2637             : 
    2638             :                     /* XSD doesn't support infinite values */
    2639          24 :                     if (TIMESTAMP_NOT_FINITE(timestamp))
    2640           0 :                         ereport(ERROR,
    2641             :                                 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
    2642             :                                  errmsg("timestamp out of range"),
    2643             :                                  errdetail("XML does not support infinite timestamp values.")));
    2644          24 :                     else if (timestamp2tm(timestamp, &tz, &tm, &fsec, &tzn, NULL) == 0)
    2645          24 :                         EncodeDateTime(&tm, fsec, true, tz, tzn, USE_XSD_DATES, buf);
    2646             :                     else
    2647           0 :                         ereport(ERROR,
    2648             :                                 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
    2649             :                                  errmsg("timestamp out of range")));
    2650             : 
    2651          24 :                     return pstrdup(buf);
    2652             :                 }
    2653             : 
    2654             : #ifdef USE_LIBXML
    2655          36 :             case BYTEAOID:
    2656             :                 {
    2657          36 :                     bytea      *bstr = DatumGetByteaPP(value);
    2658             :                     PgXmlErrorContext *xmlerrcxt;
    2659          36 :                     volatile xmlBufferPtr buf = NULL;
    2660          36 :                     volatile xmlTextWriterPtr writer = NULL;
    2661             :                     char       *result;
    2662             : 
    2663          36 :                     xmlerrcxt = pg_xml_init(PG_XML_STRICTNESS_ALL);
    2664             : 
    2665          36 :                     PG_TRY();
    2666             :                     {
    2667          36 :                         buf = xmlBufferCreate();
    2668          36 :                         if (buf == NULL || xmlerrcxt->err_occurred)
    2669           0 :                             xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
    2670             :                                         "could not allocate xmlBuffer");
    2671          36 :                         writer = xmlNewTextWriterMemory(buf, 0);
    2672          36 :                         if (writer == NULL || xmlerrcxt->err_occurred)
    2673           0 :                             xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
    2674             :                                         "could not allocate xmlTextWriter");
    2675             : 
    2676          36 :                         if (xmlbinary == XMLBINARY_BASE64)
    2677          30 :                             xmlTextWriterWriteBase64(writer, VARDATA_ANY(bstr),
    2678          30 :                                                      0, VARSIZE_ANY_EXHDR(bstr));
    2679             :                         else
    2680           6 :                             xmlTextWriterWriteBinHex(writer, VARDATA_ANY(bstr),
    2681           6 :                                                      0, VARSIZE_ANY_EXHDR(bstr));
    2682             : 
    2683             :                         /* we MUST do this now to flush data out to the buffer */
    2684          36 :                         xmlFreeTextWriter(writer);
    2685          36 :                         writer = NULL;
    2686             : 
    2687          36 :                         result = pstrdup((const char *) xmlBufferContent(buf));
    2688             :                     }
    2689           0 :                     PG_CATCH();
    2690             :                     {
    2691           0 :                         if (writer)
    2692           0 :                             xmlFreeTextWriter(writer);
    2693           0 :                         if (buf)
    2694           0 :                             xmlBufferFree(buf);
    2695             : 
    2696           0 :                         pg_xml_done(xmlerrcxt, true);
    2697             : 
    2698           0 :                         PG_RE_THROW();
    2699             :                     }
    2700          36 :                     PG_END_TRY();
    2701             : 
    2702          36 :                     xmlBufferFree(buf);
    2703             : 
    2704          36 :                     pg_xml_done(xmlerrcxt, false);
    2705             : 
    2706          36 :                     return result;
    2707             :                 }
    2708             : #endif                          /* USE_LIBXML */
    2709             : 
    2710             :         }
    2711             : 
    2712             :         /*
    2713             :          * otherwise, just use the type's native text representation
    2714             :          */
    2715      134700 :         getTypeOutputInfo(type, &typeOut, &isvarlena);
    2716      134700 :         str = OidOutputFunctionCall(typeOut, value);
    2717             : 
    2718             :         /* ... exactly as-is for XML, and when escaping is not wanted */
    2719      134700 :         if (type == XMLOID || !xml_escape_strings)
    2720       22358 :             return str;
    2721             : 
    2722             :         /* otherwise, translate special characters as needed */
    2723      112342 :         return escape_xml(str);
    2724             :     }
    2725             : }
    2726             : 
    2727             : 
    2728             : /*
    2729             :  * Escape characters in text that have special meanings in XML.
    2730             :  *
    2731             :  * Returns a palloc'd string.
    2732             :  *
    2733             :  * NB: this is intentionally not dependent on libxml.
    2734             :  */
    2735             : char *
    2736      112814 : escape_xml(const char *str)
    2737             : {
    2738             :     StringInfoData buf;
    2739             :     const char *p;
    2740             : 
    2741      112814 :     initStringInfo(&buf);
    2742      708908 :     for (p = str; *p; p++)
    2743             :     {
    2744      596094 :         switch (*p)
    2745             :         {
    2746           0 :             case '&':
    2747           0 :                 appendStringInfoString(&buf, "&amp;");
    2748           0 :                 break;
    2749          36 :             case '<':
    2750          36 :                 appendStringInfoString(&buf, "&lt;");
    2751          36 :                 break;
    2752          24 :             case '>':
    2753          24 :                 appendStringInfoString(&buf, "&gt;");
    2754          24 :                 break;
    2755           0 :             case '\r':
    2756           0 :                 appendStringInfoString(&buf, "&#x0d;");
    2757           0 :                 break;
    2758      596034 :             default:
    2759      596034 :                 appendStringInfoCharMacro(&buf, *p);
    2760      596034 :                 break;
    2761             :         }
    2762             :     }
    2763      112814 :     return buf.data;
    2764             : }
    2765             : 
    2766             : 
    2767             : static char *
    2768          24 : _SPI_strdup(const char *s)
    2769             : {
    2770          24 :     size_t      len = strlen(s) + 1;
    2771          24 :     char       *ret = SPI_palloc(len);
    2772             : 
    2773          24 :     memcpy(ret, s, len);
    2774          24 :     return ret;
    2775             : }
    2776             : 
    2777             : 
    2778             : /*
    2779             :  * SQL to XML mapping functions
    2780             :  *
    2781             :  * What follows below was at one point intentionally organized so that
    2782             :  * you can read along in the SQL/XML standard. The functions are
    2783             :  * mostly split up the way the clauses lay out in the standards
    2784             :  * document, and the identifiers are also aligned with the standard
    2785             :  * text.  Unfortunately, SQL/XML:2006 reordered the clauses
    2786             :  * differently than SQL/XML:2003, so the order below doesn't make much
    2787             :  * sense anymore.
    2788             :  *
    2789             :  * There are many things going on there:
    2790             :  *
    2791             :  * There are two kinds of mappings: Mapping SQL data (table contents)
    2792             :  * to XML documents, and mapping SQL structure (the "schema") to XML
    2793             :  * Schema.  And there are functions that do both at the same time.
    2794             :  *
    2795             :  * Then you can map a database, a schema, or a table, each in both
    2796             :  * ways.  This breaks down recursively: Mapping a database invokes
    2797             :  * mapping schemas, which invokes mapping tables, which invokes
    2798             :  * mapping rows, which invokes mapping columns, although you can't
    2799             :  * call the last two from the outside.  Because of this, there are a
    2800             :  * number of xyz_internal() functions which are to be called both from
    2801             :  * the function manager wrapper and from some upper layer in a
    2802             :  * recursive call.
    2803             :  *
    2804             :  * See the documentation about what the common function arguments
    2805             :  * nulls, tableforest, and targetns mean.
    2806             :  *
    2807             :  * Some style guidelines for XML output: Use double quotes for quoting
    2808             :  * XML attributes.  Indent XML elements by two spaces, but remember
    2809             :  * that a lot of code is called recursively at different levels, so
    2810             :  * it's better not to indent rather than create output that indents
    2811             :  * and outdents weirdly.  Add newlines to make the output look nice.
    2812             :  */
    2813             : 
    2814             : 
    2815             : /*
    2816             :  * Visibility of objects for XML mappings; see SQL/XML:2008 section
    2817             :  * 4.10.8.
    2818             :  */
    2819             : 
    2820             : /*
    2821             :  * Given a query, which must return type oid as first column, produce
    2822             :  * a list of Oids with the query results.
    2823             :  */
    2824             : static List *
    2825          36 : query_to_oid_list(const char *query)
    2826             : {
    2827             :     uint64      i;
    2828          36 :     List       *list = NIL;
    2829             :     int         spi_result;
    2830             : 
    2831          36 :     spi_result = SPI_execute(query, true, 0);
    2832          36 :     if (spi_result != SPI_OK_SELECT)
    2833           0 :         elog(ERROR, "SPI_execute returned %s for %s",
    2834             :              SPI_result_code_string(spi_result), query);
    2835             : 
    2836         108 :     for (i = 0; i < SPI_processed; i++)
    2837             :     {
    2838             :         Datum       oid;
    2839             :         bool        isnull;
    2840             : 
    2841          72 :         oid = SPI_getbinval(SPI_tuptable->vals[i],
    2842          72 :                             SPI_tuptable->tupdesc,
    2843             :                             1,
    2844             :                             &isnull);
    2845          72 :         if (!isnull)
    2846          72 :             list = lappend_oid(list, DatumGetObjectId(oid));
    2847             :     }
    2848             : 
    2849          36 :     return list;
    2850             : }
    2851             : 
    2852             : 
    2853             : static List *
    2854          36 : schema_get_xml_visible_tables(Oid nspid)
    2855             : {
    2856             :     StringInfoData query;
    2857             : 
    2858          36 :     initStringInfo(&query);
    2859          36 :     appendStringInfo(&query, "SELECT oid FROM pg_catalog.pg_class"
    2860             :                      " WHERE relnamespace = %u AND relkind IN ("
    2861             :                      CppAsString2(RELKIND_RELATION) ","
    2862             :                      CppAsString2(RELKIND_MATVIEW) ","
    2863             :                      CppAsString2(RELKIND_VIEW) ")"
    2864             :                      " AND pg_catalog.has_table_privilege (oid, 'SELECT')"
    2865             :                      " ORDER BY relname;", nspid);
    2866             : 
    2867          36 :     return query_to_oid_list(query.data);
    2868             : }
    2869             : 
    2870             : 
    2871             : /*
    2872             :  * Including the system schemas is probably not useful for a database
    2873             :  * mapping.
    2874             :  */
    2875             : #define XML_VISIBLE_SCHEMAS_EXCLUDE "(nspname ~ '^pg_' OR nspname = 'information_schema')"
    2876             : 
    2877             : #define XML_VISIBLE_SCHEMAS "SELECT oid FROM pg_catalog.pg_namespace WHERE pg_catalog.has_schema_privilege (oid, 'USAGE') AND NOT " XML_VISIBLE_SCHEMAS_EXCLUDE
    2878             : 
    2879             : 
    2880             : static List *
    2881           0 : database_get_xml_visible_schemas(void)
    2882             : {
    2883           0 :     return query_to_oid_list(XML_VISIBLE_SCHEMAS " ORDER BY nspname;");
    2884             : }
    2885             : 
    2886             : 
    2887             : static List *
    2888           0 : database_get_xml_visible_tables(void)
    2889             : {
    2890             :     /* At the moment there is no order required here. */
    2891           0 :     return query_to_oid_list("SELECT oid FROM pg_catalog.pg_class"
    2892             :                              " WHERE relkind IN ("
    2893             :                              CppAsString2(RELKIND_RELATION) ","
    2894             :                              CppAsString2(RELKIND_MATVIEW) ","
    2895             :                              CppAsString2(RELKIND_VIEW) ")"
    2896             :                              " AND pg_catalog.has_table_privilege(pg_class.oid, 'SELECT')"
    2897             :                              " AND relnamespace IN (" XML_VISIBLE_SCHEMAS ");");
    2898             : }
    2899             : 
    2900             : 
    2901             : /*
    2902             :  * Map SQL table to XML and/or XML Schema document; see SQL/XML:2008
    2903             :  * section 9.11.
    2904             :  */
    2905             : 
    2906             : static StringInfo
    2907          96 : table_to_xml_internal(Oid relid,
    2908             :                       const char *xmlschema, bool nulls, bool tableforest,
    2909             :                       const char *targetns, bool top_level)
    2910             : {
    2911             :     StringInfoData query;
    2912             : 
    2913          96 :     initStringInfo(&query);
    2914          96 :     appendStringInfo(&query, "SELECT * FROM %s",
    2915             :                      DatumGetCString(DirectFunctionCall1(regclassout,
    2916             :                                                          ObjectIdGetDatum(relid))));
    2917          96 :     return query_to_xml_internal(query.data, get_rel_name(relid),
    2918             :                                  xmlschema, nulls, tableforest,
    2919             :                                  targetns, top_level);
    2920             : }
    2921             : 
    2922             : 
    2923             : Datum
    2924          36 : table_to_xml(PG_FUNCTION_ARGS)
    2925             : {
    2926          36 :     Oid         relid = PG_GETARG_OID(0);
    2927          36 :     bool        nulls = PG_GETARG_BOOL(1);
    2928          36 :     bool        tableforest = PG_GETARG_BOOL(2);
    2929          36 :     const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
    2930             : 
    2931          36 :     PG_RETURN_XML_P(stringinfo_to_xmltype(table_to_xml_internal(relid, NULL,
    2932             :                                                                 nulls, tableforest,
    2933             :                                                                 targetns, true)));
    2934             : }
    2935             : 
    2936             : 
    2937             : Datum
    2938          10 : query_to_xml(PG_FUNCTION_ARGS)
    2939             : {
    2940          10 :     char       *query = text_to_cstring(PG_GETARG_TEXT_PP(0));
    2941          10 :     bool        nulls = PG_GETARG_BOOL(1);
    2942          10 :     bool        tableforest = PG_GETARG_BOOL(2);
    2943          10 :     const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
    2944             : 
    2945          10 :     PG_RETURN_XML_P(stringinfo_to_xmltype(query_to_xml_internal(query, NULL,
    2946             :                                                                 NULL, nulls, tableforest,
    2947             :                                                                 targetns, true)));
    2948             : }
    2949             : 
    2950             : 
    2951             : Datum
    2952          12 : cursor_to_xml(PG_FUNCTION_ARGS)
    2953             : {
    2954          12 :     char       *name = text_to_cstring(PG_GETARG_TEXT_PP(0));
    2955          12 :     int32       count = PG_GETARG_INT32(1);
    2956          12 :     bool        nulls = PG_GETARG_BOOL(2);
    2957          12 :     bool        tableforest = PG_GETARG_BOOL(3);
    2958          12 :     const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(4));
    2959             : 
    2960             :     StringInfoData result;
    2961             :     Portal      portal;
    2962             :     uint64      i;
    2963             : 
    2964          12 :     initStringInfo(&result);
    2965             : 
    2966          12 :     if (!tableforest)
    2967             :     {
    2968           6 :         xmldata_root_element_start(&result, "table", NULL, targetns, true);
    2969           6 :         appendStringInfoChar(&result, '\n');
    2970             :     }
    2971             : 
    2972          12 :     SPI_connect();
    2973          12 :     portal = SPI_cursor_find(name);
    2974          12 :     if (portal == NULL)
    2975           0 :         ereport(ERROR,
    2976             :                 (errcode(ERRCODE_UNDEFINED_CURSOR),
    2977             :                  errmsg("cursor \"%s\" does not exist", name)));
    2978             : 
    2979          12 :     SPI_cursor_fetch(portal, true, count);
    2980          48 :     for (i = 0; i < SPI_processed; i++)
    2981          36 :         SPI_sql_row_to_xmlelement(i, &result, NULL, nulls,
    2982             :                                   tableforest, targetns, true);
    2983             : 
    2984          12 :     SPI_finish();
    2985             : 
    2986          12 :     if (!tableforest)
    2987           6 :         xmldata_root_element_end(&result, "table");
    2988             : 
    2989          12 :     PG_RETURN_XML_P(stringinfo_to_xmltype(&result));
    2990             : }
    2991             : 
    2992             : 
    2993             : /*
    2994             :  * Write the start tag of the root element of a data mapping.
    2995             :  *
    2996             :  * top_level means that this is the very top level of the eventual
    2997             :  * output.  For example, when the user calls table_to_xml, then a call
    2998             :  * with a table name to this function is the top level.  When the user
    2999             :  * calls database_to_xml, then a call with a schema name to this
    3000             :  * function is not the top level.  If top_level is false, then the XML
    3001             :  * namespace declarations are omitted, because they supposedly already
    3002             :  * appeared earlier in the output.  Repeating them is not wrong, but
    3003             :  * it looks ugly.
    3004             :  */
    3005             : static void
    3006         238 : xmldata_root_element_start(StringInfo result, const char *eltname,
    3007             :                            const char *xmlschema, const char *targetns,
    3008             :                            bool top_level)
    3009             : {
    3010             :     /* This isn't really wrong but currently makes no sense. */
    3011             :     Assert(top_level || !xmlschema);
    3012             : 
    3013         238 :     appendStringInfo(result, "<%s", eltname);
    3014         238 :     if (top_level)
    3015             :     {
    3016         178 :         appendStringInfoString(result, " xmlns:xsi=\"" NAMESPACE_XSI "\"");
    3017         178 :         if (strlen(targetns) > 0)
    3018          30 :             appendStringInfo(result, " xmlns=\"%s\"", targetns);
    3019             :     }
    3020         238 :     if (xmlschema)
    3021             :     {
    3022             :         /* FIXME: better targets */
    3023          18 :         if (strlen(targetns) > 0)
    3024           6 :             appendStringInfo(result, " xsi:schemaLocation=\"%s #\"", targetns);
    3025             :         else
    3026          12 :             appendStringInfoString(result, " xsi:noNamespaceSchemaLocation=\"#\"");
    3027             :     }
    3028         238 :     appendStringInfoString(result, ">\n");
    3029         238 : }
    3030             : 
    3031             : 
    3032             : static void
    3033         238 : xmldata_root_element_end(StringInfo result, const char *eltname)
    3034             : {
    3035         238 :     appendStringInfo(result, "</%s>\n", eltname);
    3036         238 : }
    3037             : 
    3038             : 
    3039             : static StringInfo
    3040         112 : query_to_xml_internal(const char *query, char *tablename,
    3041             :                       const char *xmlschema, bool nulls, bool tableforest,
    3042             :                       const char *targetns, bool top_level)
    3043             : {
    3044             :     StringInfo  result;
    3045             :     char       *xmltn;
    3046             :     uint64      i;
    3047             : 
    3048         112 :     if (tablename)
    3049          96 :         xmltn = map_sql_identifier_to_xml_name(tablename, true, false);
    3050             :     else
    3051          16 :         xmltn = "table";
    3052             : 
    3053         112 :     result = makeStringInfo();
    3054             : 
    3055         112 :     SPI_connect();
    3056         112 :     if (SPI_execute(query, true, 0) != SPI_OK_SELECT)
    3057           0 :         ereport(ERROR,
    3058             :                 (errcode(ERRCODE_DATA_EXCEPTION),
    3059             :                  errmsg("invalid query")));
    3060             : 
    3061         112 :     if (!tableforest)
    3062             :     {
    3063          52 :         xmldata_root_element_start(result, xmltn, xmlschema,
    3064             :                                    targetns, top_level);
    3065          52 :         appendStringInfoChar(result, '\n');
    3066             :     }
    3067             : 
    3068         112 :     if (xmlschema)
    3069          30 :         appendStringInfo(result, "%s\n\n", xmlschema);
    3070             : 
    3071         388 :     for (i = 0; i < SPI_processed; i++)
    3072         276 :         SPI_sql_row_to_xmlelement(i, result, tablename, nulls,
    3073             :                                   tableforest, targetns, top_level);
    3074             : 
    3075         112 :     if (!tableforest)
    3076          52 :         xmldata_root_element_end(result, xmltn);
    3077             : 
    3078         112 :     SPI_finish();
    3079             : 
    3080         112 :     return result;
    3081             : }
    3082             : 
    3083             : 
    3084             : Datum
    3085          30 : table_to_xmlschema(PG_FUNCTION_ARGS)
    3086             : {
    3087          30 :     Oid         relid = PG_GETARG_OID(0);
    3088          30 :     bool        nulls = PG_GETARG_BOOL(1);
    3089          30 :     bool        tableforest = PG_GETARG_BOOL(2);
    3090          30 :     const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
    3091             :     const char *result;
    3092             :     Relation    rel;
    3093             : 
    3094          30 :     rel = table_open(relid, AccessShareLock);
    3095          30 :     result = map_sql_table_to_xmlschema(rel->rd_att, relid, nulls,
    3096             :                                         tableforest, targetns);
    3097          30 :     table_close(rel, NoLock);
    3098             : 
    3099          30 :     PG_RETURN_XML_P(cstring_to_xmltype(result));
    3100             : }
    3101             : 
    3102             : 
    3103             : Datum
    3104           6 : query_to_xmlschema(PG_FUNCTION_ARGS)
    3105             : {
    3106           6 :     char       *query = text_to_cstring(PG_GETARG_TEXT_PP(0));
    3107           6 :     bool        nulls = PG_GETARG_BOOL(1);
    3108           6 :     bool        tableforest = PG_GETARG_BOOL(2);
    3109           6 :     const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
    3110             :     const char *result;
    3111             :     SPIPlanPtr  plan;
    3112             :     Portal      portal;
    3113             : 
    3114           6 :     SPI_connect();
    3115             : 
    3116           6 :     if ((plan = SPI_prepare(query, 0, NULL)) == NULL)
    3117           0 :         elog(ERROR, "SPI_prepare(\"%s\") failed", query);
    3118             : 
    3119           6 :     if ((portal = SPI_cursor_open(NULL, plan, NULL, NULL, true)) == NULL)
    3120           0 :         elog(ERROR, "SPI_cursor_open(\"%s\") failed", query);
    3121             : 
    3122           6 :     result = _SPI_strdup(map_sql_table_to_xmlschema(portal->tupDesc,
    3123             :                                                     InvalidOid, nulls,
    3124             :                                                     tableforest, targetns));
    3125           6 :     SPI_cursor_close(portal);
    3126           6 :     SPI_finish();
    3127             : 
    3128           6 :     PG_RETURN_XML_P(cstring_to_xmltype(result));
    3129             : }
    3130             : 
    3131             : 
    3132             : Datum
    3133          12 : cursor_to_xmlschema(PG_FUNCTION_ARGS)
    3134             : {
    3135          12 :     char       *name = text_to_cstring(PG_GETARG_TEXT_PP(0));
    3136          12 :     bool        nulls = PG_GETARG_BOOL(1);
    3137          12 :     bool        tableforest = PG_GETARG_BOOL(2);
    3138          12 :     const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
    3139             :     const char *xmlschema;
    3140             :     Portal      portal;
    3141             : 
    3142          12 :     SPI_connect();
    3143          12 :     portal = SPI_cursor_find(name);
    3144          12 :     if (portal == NULL)
    3145           0 :         ereport(ERROR,
    3146             :                 (errcode(ERRCODE_UNDEFINED_CURSOR),
    3147             :                  errmsg("cursor \"%s\" does not exist", name)));
    3148          12 :     if (portal->tupDesc == NULL)
    3149           0 :         ereport(ERROR,
    3150             :                 (errcode(ERRCODE_INVALID_CURSOR_STATE),
    3151             :                  errmsg("portal \"%s\" does not return tuples", name)));
    3152             : 
    3153          12 :     xmlschema = _SPI_strdup(map_sql_table_to_xmlschema(portal->tupDesc,
    3154             :                                                        InvalidOid, nulls,
    3155             :                                                        tableforest, targetns));
    3156          12 :     SPI_finish();
    3157             : 
    3158          12 :     PG_RETURN_XML_P(cstring_to_xmltype(xmlschema));
    3159             : }
    3160             : 
    3161             : 
    3162             : Datum
    3163          24 : table_to_xml_and_xmlschema(PG_FUNCTION_ARGS)
    3164             : {
    3165          24 :     Oid         relid = PG_GETARG_OID(0);
    3166          24 :     bool        nulls = PG_GETARG_BOOL(1);
    3167          24 :     bool        tableforest = PG_GETARG_BOOL(2);
    3168          24 :     const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
    3169             :     Relation    rel;
    3170             :     const char *xmlschema;
    3171             : 
    3172          24 :     rel = table_open(relid, AccessShareLock);
    3173          24 :     xmlschema = map_sql_table_to_xmlschema(rel->rd_att, relid, nulls,
    3174             :                                            tableforest, targetns);
    3175          24 :     table_close(rel, NoLock);
    3176             : 
    3177          24 :     PG_RETURN_XML_P(stringinfo_to_xmltype(table_to_xml_internal(relid,
    3178             :                                                                 xmlschema, nulls, tableforest,
    3179             :                                                                 targetns, true)));
    3180             : }
    3181             : 
    3182             : 
    3183             : Datum
    3184           6 : query_to_xml_and_xmlschema(PG_FUNCTION_ARGS)
    3185             : {
    3186           6 :     char       *query = text_to_cstring(PG_GETARG_TEXT_PP(0));
    3187           6 :     bool        nulls = PG_GETARG_BOOL(1);
    3188           6 :     bool        tableforest = PG_GETARG_BOOL(2);
    3189           6 :     const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
    3190             : 
    3191             :     const char *xmlschema;
    3192             :     SPIPlanPtr  plan;
    3193             :     Portal      portal;
    3194             : 
    3195           6 :     SPI_connect();
    3196             : 
    3197           6 :     if ((plan = SPI_prepare(query, 0, NULL)) == NULL)
    3198           0 :         elog(ERROR, "SPI_prepare(\"%s\") failed", query);
    3199             : 
    3200           6 :     if ((portal = SPI_cursor_open(NULL, plan, NULL, NULL, true)) == NULL)
    3201           0 :         elog(ERROR, "SPI_cursor_open(\"%s\") failed", query);
    3202             : 
    3203           6 :     xmlschema = _SPI_strdup(map_sql_table_to_xmlschema(portal->tupDesc,
    3204             :                                                        InvalidOid, nulls, tableforest, targetns));
    3205           6 :     SPI_cursor_close(portal);
    3206           6 :     SPI_finish();
    3207             : 
    3208           6 :     PG_RETURN_XML_P(stringinfo_to_xmltype(query_to_xml_internal(query, NULL,
    3209             :                                                                 xmlschema, nulls, tableforest,
    3210             :                                                                 targetns, true)));
    3211             : }
    3212             : 
    3213             : 
    3214             : /*
    3215             :  * Map SQL schema to XML and/or XML Schema document; see SQL/XML:2008
    3216             :  * sections 9.13, 9.14.
    3217             :  */
    3218             : 
    3219             : static StringInfo
    3220          18 : schema_to_xml_internal(Oid nspid, const char *xmlschema, bool nulls,
    3221             :                        bool tableforest, const char *targetns, bool top_level)
    3222             : {
    3223             :     StringInfo  result;
    3224             :     char       *xmlsn;
    3225             :     List       *relid_list;
    3226             :     ListCell   *cell;
    3227             : 
    3228          18 :     xmlsn = map_sql_identifier_to_xml_name(get_namespace_name(nspid),
    3229             :                                            true, false);
    3230          18 :     result = makeStringInfo();
    3231             : 
    3232          18 :     xmldata_root_element_start(result, xmlsn, xmlschema, targetns, top_level);
    3233          18 :     appendStringInfoChar(result, '\n');
    3234             : 
    3235          18 :     if (xmlschema)
    3236           6 :         appendStringInfo(result, "%s\n\n", xmlschema);
    3237             : 
    3238          18 :     SPI_connect();
    3239             : 
    3240          18 :     relid_list = schema_get_xml_visible_tables(nspid);
    3241             : 
    3242          54 :     foreach(cell, relid_list)
    3243             :     {
    3244          36 :         Oid         relid = lfirst_oid(cell);
    3245             :         StringInfo  subres;
    3246             : 
    3247          36 :         subres = table_to_xml_internal(relid, NULL, nulls, tableforest,
    3248             :                                        targetns, false);
    3249             : 
    3250          36 :         appendBinaryStringInfo(result, subres->data, subres->len);
    3251          36 :         appendStringInfoChar(result, '\n');
    3252             :     }
    3253             : 
    3254          18 :     SPI_finish();
    3255             : 
    3256          18 :     xmldata_root_element_end(result, xmlsn);
    3257             : 
    3258          18 :     return result;
    3259             : }
    3260             : 
    3261             : 
    3262             : Datum
    3263          12 : schema_to_xml(PG_FUNCTION_ARGS)
    3264             : {
    3265          12 :     Name        name = PG_GETARG_NAME(0);
    3266          12 :     bool        nulls = PG_GETARG_BOOL(1);
    3267          12 :     bool        tableforest = PG_GETARG_BOOL(2);
    3268          12 :     const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
    3269             : 
    3270             :     char       *schemaname;
    3271             :     Oid         nspid;
    3272             : 
    3273          12 :     schemaname = NameStr(*name);
    3274          12 :     nspid = LookupExplicitNamespace(schemaname, false);
    3275             : 
    3276          12 :     PG_RETURN_XML_P(stringinfo_to_xmltype(schema_to_xml_internal(nspid, NULL,
    3277             :                                                                  nulls, tableforest, targetns, true)));
    3278             : }
    3279             : 
    3280             : 
    3281             : /*
    3282             :  * Write the start element of the root element of an XML Schema mapping.
    3283             :  */
    3284             : static void
    3285          96 : xsd_schema_element_start(StringInfo result, const char *targetns)
    3286             : {
    3287          96 :     appendStringInfoString(result,
    3288             :                            "<xsd:schema\n"
    3289             :                            "    xmlns:xsd=\"" NAMESPACE_XSD "\"");
    3290          96 :     if (strlen(targetns) > 0)
    3291          18 :         appendStringInfo(result,
    3292             :                          "\n"
    3293             :                          "    targetNamespace=\"%s\"\n"
    3294             :                          "    elementFormDefault=\"qualified\"",
    3295             :                          targetns);
    3296          96 :     appendStringInfoString(result,
    3297             :                            ">\n\n");
    3298          96 : }
    3299             : 
    3300             : 
    3301             : static void
    3302          96 : xsd_schema_element_end(StringInfo result)
    3303             : {
    3304          96 :     appendStringInfoString(result, "</xsd:schema>");
    3305          96 : }
    3306             : 
    3307             : 
    3308             : static StringInfo
    3309          18 : schema_to_xmlschema_internal(const char *schemaname, bool nulls,
    3310             :                              bool tableforest, const char *targetns)
    3311             : {
    3312             :     Oid         nspid;
    3313             :     List       *relid_list;
    3314             :     List       *tupdesc_list;
    3315             :     ListCell   *cell;
    3316             :     StringInfo  result;
    3317             : 
    3318          18 :     result = makeStringInfo();
    3319             : 
    3320          18 :     nspid = LookupExplicitNamespace(schemaname, false);
    3321             : 
    3322          18 :     xsd_schema_element_start(result, targetns);
    3323             : 
    3324          18 :     SPI_connect();
    3325             : 
    3326          18 :     relid_list = schema_get_xml_visible_tables(nspid);
    3327             : 
    3328          18 :     tupdesc_list = NIL;
    3329          54 :     foreach(cell, relid_list)
    3330             :     {
    3331             :         Relation    rel;
    3332             : 
    3333          36 :         rel = table_open(lfirst_oid(cell), AccessShareLock);
    3334          36 :         tupdesc_list = lappend(tupdesc_list, CreateTupleDescCopy(rel->rd_att));
    3335          36 :         table_close(rel, NoLock);
    3336             :     }
    3337             : 
    3338          18 :     appendStringInfoString(result,
    3339             :                            map_sql_typecoll_to_xmlschema_types(tupdesc_list));
    3340             : 
    3341          18 :     appendStringInfoString(result,
    3342             :                            map_sql_schema_to_xmlschema_types(nspid, relid_list,
    3343             :                                                              nulls, tableforest, targetns));
    3344             : 
    3345          18 :     xsd_schema_element_end(result);
    3346             : 
    3347          18 :     SPI_finish();
    3348             : 
    3349          18 :     return result;
    3350             : }
    3351             : 
    3352             : 
    3353             : Datum
    3354          12 : schema_to_xmlschema(PG_FUNCTION_ARGS)
    3355             : {
    3356          12 :     Name        name = PG_GETARG_NAME(0);
    3357          12 :     bool        nulls = PG_GETARG_BOOL(1);
    3358          12 :     bool        tableforest = PG_GETARG_BOOL(2);
    3359          12 :     const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
    3360             : 
    3361          12 :     PG_RETURN_XML_P(stringinfo_to_xmltype(schema_to_xmlschema_internal(NameStr(*name),
    3362             :                                                                        nulls, tableforest, targetns)));
    3363             : }
    3364             : 
    3365             : 
    3366             : Datum
    3367           6 : schema_to_xml_and_xmlschema(PG_FUNCTION_ARGS)
    3368             : {
    3369           6 :     Name        name = PG_GETARG_NAME(0);
    3370           6 :     bool        nulls = PG_GETARG_BOOL(1);
    3371           6 :     bool        tableforest = PG_GETARG_BOOL(2);
    3372           6 :     const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3));
    3373             :     char       *schemaname;
    3374             :     Oid         nspid;
    3375             :     StringInfo  xmlschema;
    3376             : 
    3377           6 :     schemaname = NameStr(*name);
    3378           6 :     nspid = LookupExplicitNamespace(schemaname, false);
    3379             : 
    3380           6 :     xmlschema = schema_to_xmlschema_internal(schemaname, nulls,
    3381             :                                              tableforest, targetns);
    3382             : 
    3383           6 :     PG_RETURN_XML_P(stringinfo_to_xmltype(schema_to_xml_internal(nspid,
    3384             :                                                                  xmlschema->data, nulls,
    3385             :                                                                  tableforest, targetns, true)));
    3386             : }
    3387             : 
    3388             : 
    3389             : /*
    3390             :  * Map SQL database to XML and/or XML Schema document; see SQL/XML:2008
    3391             :  * sections 9.16, 9.17.
    3392             :  */
    3393             : 
    3394             : static StringInfo
    3395           0 : database_to_xml_internal(const char *xmlschema, bool nulls,
    3396             :                          bool tableforest, const char *targetns)
    3397             : {
    3398             :     StringInfo  result;
    3399             :     List       *nspid_list;
    3400             :     ListCell   *cell;
    3401             :     char       *xmlcn;
    3402             : 
    3403           0 :     xmlcn = map_sql_identifier_to_xml_name(get_database_name(MyDatabaseId),
    3404             :                                            true, false);
    3405           0 :     result = makeStringInfo();
    3406             : 
    3407           0 :     xmldata_root_element_start(result, xmlcn, xmlschema, targetns, true);
    3408           0 :     appendStringInfoChar(result, '\n');
    3409             : 
    3410           0 :     if (xmlschema)
    3411           0 :         appendStringInfo(result, "%s\n\n", xmlschema);
    3412             : 
    3413           0 :     SPI_connect();
    3414             : 
    3415           0 :     nspid_list = database_get_xml_visible_schemas();
    3416             : 
    3417           0 :     foreach(cell, nspid_list)
    3418             :     {
    3419           0 :         Oid         nspid = lfirst_oid(cell);
    3420             :         StringInfo  subres;
    3421             : 
    3422           0 :         subres = schema_to_xml_internal(nspid, NULL, nulls,
    3423             :                                         tableforest, targetns, false);
    3424             : 
    3425           0 :         appendBinaryStringInfo(result, subres->data, subres->len);
    3426           0 :         appendStringInfoChar(result, '\n');
    3427             :     }
    3428             : 
    3429           0 :     SPI_finish();
    3430             : 
    3431           0 :     xmldata_root_element_end(result, xmlcn);
    3432             : 
    3433           0 :     return result;
    3434             : }
    3435             : 
    3436             : 
    3437             : Datum
    3438           0 : database_to_xml(PG_FUNCTION_ARGS)
    3439             : {
    3440           0 :     bool        nulls = PG_GETARG_BOOL(0);
    3441           0 :     bool        tableforest = PG_GETARG_BOOL(1);
    3442           0 :     const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(2));
    3443             : 
    3444           0 :     PG_RETURN_XML_P(stringinfo_to_xmltype(database_to_xml_internal(NULL, nulls,
    3445             :                                                                    tableforest, targetns)));
    3446             : }
    3447             : 
    3448             : 
    3449             : static StringInfo
    3450           0 : database_to_xmlschema_internal(bool nulls, bool tableforest,
    3451             :                                const char *targetns)
    3452             : {
    3453             :     List       *relid_list;
    3454             :     List       *nspid_list;
    3455             :     List       *tupdesc_list;
    3456             :     ListCell   *cell;
    3457             :     StringInfo  result;
    3458             : 
    3459           0 :     result = makeStringInfo();
    3460             : 
    3461           0 :     xsd_schema_element_start(result, targetns);
    3462             : 
    3463           0 :     SPI_connect();
    3464             : 
    3465           0 :     relid_list = database_get_xml_visible_tables();
    3466           0 :     nspid_list = database_get_xml_visible_schemas();
    3467             : 
    3468           0 :     tupdesc_list = NIL;
    3469           0 :     foreach(cell, relid_list)
    3470             :     {
    3471             :         Relation    rel;
    3472             : 
    3473           0 :         rel = table_open(lfirst_oid(cell), AccessShareLock);
    3474           0 :         tupdesc_list = lappend(tupdesc_list, CreateTupleDescCopy(rel->rd_att));
    3475           0 :         table_close(rel, NoLock);
    3476             :     }
    3477             : 
    3478           0 :     appendStringInfoString(result,
    3479             :                            map_sql_typecoll_to_xmlschema_types(tupdesc_list));
    3480             : 
    3481           0 :     appendStringInfoString(result,
    3482             :                            map_sql_catalog_to_xmlschema_types(nspid_list, nulls, tableforest, targetns));
    3483             : 
    3484           0 :     xsd_schema_element_end(result);
    3485             : 
    3486           0 :     SPI_finish();
    3487             : 
    3488           0 :     return result;
    3489             : }
    3490             : 
    3491             : 
    3492             : Datum
    3493           0 : database_to_xmlschema(PG_FUNCTION_ARGS)
    3494             : {
    3495           0 :     bool        nulls = PG_GETARG_BOOL(0);
    3496           0 :     bool        tableforest = PG_GETARG_BOOL(1);
    3497           0 :     const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(2));
    3498             : 
    3499           0 :     PG_RETURN_XML_P(stringinfo_to_xmltype(database_to_xmlschema_internal(nulls,
    3500             :                                                                          tableforest, targetns)));
    3501             : }
    3502             : 
    3503             : 
    3504             : Datum
    3505           0 : database_to_xml_and_xmlschema(PG_FUNCTION_ARGS)
    3506             : {
    3507           0 :     bool        nulls = PG_GETARG_BOOL(0);
    3508           0 :     bool        tableforest = PG_GETARG_BOOL(1);
    3509           0 :     const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(2));
    3510             :     StringInfo  xmlschema;
    3511             : 
    3512           0 :     xmlschema = database_to_xmlschema_internal(nulls, tableforest, targetns);
    3513             : 
    3514           0 :     PG_RETURN_XML_P(stringinfo_to_xmltype(database_to_xml_internal(xmlschema->data,
    3515             :                                                                    nulls, tableforest, targetns)));
    3516             : }
    3517             : 
    3518             : 
    3519             : /*
    3520             :  * Map a multi-part SQL name to an XML name; see SQL/XML:2008 section
    3521             :  * 9.2.
    3522             :  */
    3523             : static char *
    3524         384 : map_multipart_sql_identifier_to_xml_name(const char *a, const char *b, const char *c, const char *d)
    3525             : {
    3526             :     StringInfoData result;
    3527             : 
    3528         384 :     initStringInfo(&result);
    3529             : 
    3530         384 :     if (a)
    3531         384 :         appendStringInfoString(&result,
    3532         384 :                                map_sql_identifier_to_xml_name(a, true, true));
    3533         384 :     if (b)
    3534         384 :         appendStringInfo(&result, ".%s",
    3535             :                          map_sql_identifier_to_xml_name(b, true, true));
    3536         384 :     if (c)
    3537         384 :         appendStringInfo(&result, ".%s",
    3538             :                          map_sql_identifier_to_xml_name(c, true, true));
    3539         384 :     if (d)
    3540         366 :         appendStringInfo(&result, ".%s",
    3541             :                          map_sql_identifier_to_xml_name(d, true, true));
    3542             : 
    3543         384 :     return result.data;
    3544             : }
    3545             : 
    3546             : 
    3547             : /*
    3548             :  * Map an SQL table to an XML Schema document; see SQL/XML:2008
    3549             :  * section 9.11.
    3550             :  *
    3551             :  * Map an SQL table to XML Schema data types; see SQL/XML:2008 section
    3552             :  * 9.9.
    3553             :  */
    3554             : static const char *
    3555          78 : map_sql_table_to_xmlschema(TupleDesc tupdesc, Oid relid, bool nulls,
    3556             :                            bool tableforest, const char *targetns)
    3557             : {
    3558             :     int         i;
    3559             :     char       *xmltn;
    3560             :     char       *tabletypename;
    3561             :     char       *rowtypename;
    3562             :     StringInfoData result;
    3563             : 
    3564          78 :     initStringInfo(&result);
    3565             : 
    3566          78 :     if (OidIsValid(relid))
    3567             :     {
    3568             :         HeapTuple   tuple;
    3569             :         Form_pg_class reltuple;
    3570             : 
    3571          54 :         tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
    3572          54 :         if (!HeapTupleIsValid(tuple))
    3573           0 :             elog(ERROR, "cache lookup failed for relation %u", relid);
    3574          54 :         reltuple = (Form_pg_class) GETSTRUCT(tuple);
    3575             : 
    3576          54 :         xmltn = map_sql_identifier_to_xml_name(NameStr(reltuple->relname),
    3577             :                                                true, false);
    3578             : 
    3579          54 :         tabletypename = map_multipart_sql_identifier_to_xml_name("TableType",
    3580          54 :                                                                  get_database_name(MyDatabaseId),
    3581          54 :                                                                  get_namespace_name(reltuple->relnamespace),
    3582          54 :                                                                  NameStr(reltuple->relname));
    3583             : 
    3584          54 :         rowtypename = map_multipart_sql_identifier_to_xml_name("RowType",
    3585          54 :                                                                get_database_name(MyDatabaseId),
    3586          54 :                                                                get_namespace_name(reltuple->relnamespace),
    3587          54 :                                                                NameStr(reltuple->relname));
    3588             : 
    3589          54 :         ReleaseSysCache(tuple);
    3590             :     }
    3591             :     else
    3592             :     {
    3593          24 :         if (tableforest)
    3594          12 :             xmltn = "row";
    3595             :         else
    3596          12 :             xmltn = "table";
    3597             : 
    3598          24 :         tabletypename = "TableType";
    3599          24 :         rowtypename = "RowType";
    3600             :     }
    3601             : 
    3602          78 :     xsd_schema_element_start(&result, targetns);
    3603             : 
    3604          78 :     appendStringInfoString(&result,
    3605          78 :                            map_sql_typecoll_to_xmlschema_types(list_make1(tupdesc)));
    3606             : 
    3607          78 :     appendStringInfo(&result,
    3608             :                      "<xsd:complexType name=\"%s\">\n"
    3609             :                      "  <xsd:sequence>\n",
    3610             :                      rowtypename);
    3611             : 
    3612         324 :     for (i = 0; i < tupdesc->natts; i++)
    3613             :     {
    3614         246 :         Form_pg_attribute att = TupleDescAttr(tupdesc, i);
    3615             : 
    3616         246 :         if (att->attisdropped)
    3617           6 :             continue;
    3618         480 :         appendStringInfo(&result,
    3619             :                          "    <xsd:element name=\"%s\" type=\"%s\"%s></xsd:element>\n",
    3620         240 :                          map_sql_identifier_to_xml_name(NameStr(att->attname),
    3621             :                                                         true, false),
    3622             :                          map_sql_type_to_xml_name(att->atttypid, -1),
    3623             :                          nulls ? " nillable=\"true\"" : " minOccurs=\"0\"");
    3624             :     }
    3625             : 
    3626          78 :     appendStringInfoString(&result,
    3627             :                            "  </xsd:sequence>\n"
    3628             :                            "</xsd:complexType>\n\n");
    3629             : 
    3630          78 :     if (!tableforest)
    3631             :     {
    3632          42 :         appendStringInfo(&result,
    3633             :                          "<xsd:complexType name=\"%s\">\n"
    3634             :                          "  <xsd:sequence>\n"
    3635             :                          "    <xsd:element name=\"row\" type=\"%s\" minOccurs=\"0\" maxOccurs=\"unbounded\"/>\n"
    3636             :                          "  </xsd:sequence>\n"
    3637             :                          "</xsd:complexType>\n\n",
    3638             :                          tabletypename, rowtypename);
    3639             : 
    3640          42 :         appendStringInfo(&result,
    3641             :                          "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
    3642             :                          xmltn, tabletypename);
    3643             :     }
    3644             :     else
    3645          36 :         appendStringInfo(&result,
    3646             :                          "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
    3647             :                          xmltn, rowtypename);
    3648             : 
    3649          78 :     xsd_schema_element_end(&result);
    3650             : 
    3651          78 :     return result.data;
    3652             : }
    3653             : 
    3654             : 
    3655             : /*
    3656             :  * Map an SQL schema to XML Schema data types; see SQL/XML:2008
    3657             :  * section 9.12.
    3658             :  */
    3659             : static const char *
    3660          18 : map_sql_schema_to_xmlschema_types(Oid nspid, List *relid_list, bool nulls,
    3661             :                                   bool tableforest, const char *targetns)
    3662             : {
    3663             :     char       *dbname;
    3664             :     char       *nspname;
    3665             :     char       *xmlsn;
    3666             :     char       *schematypename;
    3667             :     StringInfoData result;
    3668             :     ListCell   *cell;
    3669             : 
    3670          18 :     dbname = get_database_name(MyDatabaseId);
    3671          18 :     nspname = get_namespace_name(nspid);
    3672             : 
    3673          18 :     initStringInfo(&result);
    3674             : 
    3675          18 :     xmlsn = map_sql_identifier_to_xml_name(nspname, true, false);
    3676             : 
    3677          18 :     schematypename = map_multipart_sql_identifier_to_xml_name("SchemaType",
    3678             :                                                               dbname,
    3679             :                                                               nspname,
    3680             :                                                               NULL);
    3681             : 
    3682          18 :     appendStringInfo(&result,
    3683             :                      "<xsd:complexType name=\"%s\">\n", schematypename);
    3684          18 :     if (!tableforest)
    3685           6 :         appendStringInfoString(&result,
    3686             :                                "  <xsd:all>\n");
    3687             :     else
    3688          12 :         appendStringInfoString(&result,
    3689             :                                "  <xsd:sequence>\n");
    3690             : 
    3691          54 :     foreach(cell, relid_list)
    3692             :     {
    3693          36 :         Oid         relid = lfirst_oid(cell);
    3694          36 :         char       *relname = get_rel_name(relid);
    3695          36 :         char       *xmltn = map_sql_identifier_to_xml_name(relname, true, false);
    3696          36 :         char       *tabletypename = map_multipart_sql_identifier_to_xml_name(tableforest ? "RowType" : "TableType",
    3697             :                                                                              dbname,
    3698             :                                                                              nspname,
    3699             :                                                                              relname);
    3700             : 
    3701          36 :         if (!tableforest)
    3702          12 :             appendStringInfo(&result,
    3703             :                              "    <xsd:element name=\"%s\" type=\"%s\"/>\n",
    3704             :                              xmltn, tabletypename);
    3705             :         else
    3706          24 :             appendStringInfo(&result,
    3707             :                              "    <xsd:element name=\"%s\" type=\"%s\" minOccurs=\"0\" maxOccurs=\"unbounded\"/>\n",
    3708             :                              xmltn, tabletypename);
    3709             :     }
    3710             : 
    3711          18 :     if (!tableforest)
    3712           6 :         appendStringInfoString(&result,
    3713             :                                "  </xsd:all>\n");
    3714             :     else
    3715          12 :         appendStringInfoString(&result,
    3716             :                                "  </xsd:sequence>\n");
    3717          18 :     appendStringInfoString(&result,
    3718             :                            "</xsd:complexType>\n\n");
    3719             : 
    3720          18 :     appendStringInfo(&result,
    3721             :                      "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
    3722             :                      xmlsn, schematypename);
    3723             : 
    3724          18 :     return result.data;
    3725             : }
    3726             : 
    3727             : 
    3728             : /*
    3729             :  * Map an SQL catalog to XML Schema data types; see SQL/XML:2008
    3730             :  * section 9.15.
    3731             :  */
    3732             : static const char *
    3733           0 : map_sql_catalog_to_xmlschema_types(List *nspid_list, bool nulls,
    3734             :                                    bool tableforest, const char *targetns)
    3735             : {
    3736             :     char       *dbname;
    3737             :     char       *xmlcn;
    3738             :     char       *catalogtypename;
    3739             :     StringInfoData result;
    3740             :     ListCell   *cell;
    3741             : 
    3742           0 :     dbname = get_database_name(MyDatabaseId);
    3743             : 
    3744           0 :     initStringInfo(&result);
    3745             : 
    3746           0 :     xmlcn = map_sql_identifier_to_xml_name(dbname, true, false);
    3747             : 
    3748           0 :     catalogtypename = map_multipart_sql_identifier_to_xml_name("CatalogType",
    3749             :                                                                dbname,
    3750             :                                                                NULL,
    3751             :                                                                NULL);
    3752             : 
    3753           0 :     appendStringInfo(&result,
    3754             :                      "<xsd:complexType name=\"%s\">\n", catalogtypename);
    3755           0 :     appendStringInfoString(&result,
    3756             :                            "  <xsd:all>\n");
    3757             : 
    3758           0 :     foreach(cell, nspid_list)
    3759             :     {
    3760           0 :         Oid         nspid = lfirst_oid(cell);
    3761           0 :         char       *nspname = get_namespace_name(nspid);
    3762           0 :         char       *xmlsn = map_sql_identifier_to_xml_name(nspname, true, false);
    3763           0 :         char       *schematypename = map_multipart_sql_identifier_to_xml_name("SchemaType",
    3764             :                                                                               dbname,
    3765             :                                                                               nspname,
    3766             :                                                                               NULL);
    3767             : 
    3768           0 :         appendStringInfo(&result,
    3769             :                          "    <xsd:element name=\"%s\" type=\"%s\"/>\n",
    3770             :                          xmlsn, schematypename);
    3771             :     }
    3772             : 
    3773           0 :     appendStringInfoString(&result,
    3774             :                            "  </xsd:all>\n");
    3775           0 :     appendStringInfoString(&result,
    3776             :                            "</xsd:complexType>\n\n");
    3777             : 
    3778           0 :     appendStringInfo(&result,
    3779             :                      "<xsd:element name=\"%s\" type=\"%s\"/>\n\n",
    3780             :                      xmlcn, catalogtypename);
    3781             : 
    3782           0 :     return result.data;
    3783             : }
    3784             : 
    3785             : 
    3786             : /*
    3787             :  * Map an SQL data type to an XML name; see SQL/XML:2008 section 9.4.
    3788             :  */
    3789             : static const char *
    3790         810 : map_sql_type_to_xml_name(Oid typeoid, int typmod)
    3791             : {
    3792             :     StringInfoData result;
    3793             : 
    3794         810 :     initStringInfo(&result);
    3795             : 
    3796         810 :     switch (typeoid)
    3797             :     {
    3798          30 :         case BPCHAROID:
    3799          30 :             if (typmod == -1)
    3800          30 :                 appendStringInfoString(&result, "CHAR");
    3801             :             else
    3802           0 :                 appendStringInfo(&result, "CHAR_%d", typmod - VARHDRSZ);
    3803          30 :             break;
    3804          54 :         case VARCHAROID:
    3805          54 :             if (typmod == -1)
    3806          54 :                 appendStringInfoString(&result, "VARCHAR");
    3807             :             else
    3808           0 :                 appendStringInfo(&result, "VARCHAR_%d", typmod - VARHDRSZ);
    3809          54 :             break;
    3810          30 :         case NUMERICOID:
    3811          30 :             if (typmod == -1)
    3812          30 :                 appendStringInfoString(&result, "NUMERIC");
    3813             :             else
    3814           0 :                 appendStringInfo(&result, "NUMERIC_%d_%d",
    3815           0 :                                  ((typmod - VARHDRSZ) >> 16) & 0xffff,
    3816           0 :                                  (typmod - VARHDRSZ) & 0xffff);
    3817          30 :             break;
    3818         174 :         case INT4OID:
    3819         174 :             appendStringInfoString(&result, "INTEGER");
    3820         174 :             break;
    3821          30 :         case INT2OID:
    3822          30 :             appendStringInfoString(&result, "SMALLINT");
    3823          30 :             break;
    3824          30 :         case INT8OID:
    3825          30 :             appendStringInfoString(&result, "BIGINT");
    3826          30 :             break;
    3827          30 :         case FLOAT4OID:
    3828          30 :             appendStringInfoString(&result, "REAL");
    3829          30 :             break;
    3830           0 :         case FLOAT8OID:
    3831           0 :             appendStringInfoString(&result, "DOUBLE");
    3832           0 :             break;
    3833          30 :         case BOOLOID:
    3834          30 :             appendStringInfoString(&result, "BOOLEAN");
    3835          30 :             break;
    3836          30 :         case TIMEOID:
    3837          30 :             if (typmod == -1)
    3838          30 :                 appendStringInfoString(&result, "TIME");
    3839             :             else
    3840           0 :                 appendStringInfo(&result, "TIME_%d", typmod);
    3841          30 :             break;
    3842          30 :         case TIMETZOID:
    3843          30 :             if (typmod == -1)
    3844          30 :                 appendStringInfoString(&result, "TIME_WTZ");
    3845             :             else
    3846           0 :                 appendStringInfo(&result, "TIME_WTZ_%d", typmod);
    3847          30 :             break;
    3848          30 :         case TIMESTAMPOID:
    3849          30 :             if (typmod == -1)
    3850          30 :                 appendStringInfoString(&result, "TIMESTAMP");
    3851             :             else
    3852           0 :                 appendStringInfo(&result, "TIMESTAMP_%d", typmod);
    3853          30 :             break;
    3854          30 :         case TIMESTAMPTZOID:
    3855          30 :             if (typmod == -1)
    3856          30 :                 appendStringInfoString(&result, "TIMESTAMP_WTZ");
    3857             :             else
    3858           0 :                 appendStringInfo(&result, "TIMESTAMP_WTZ_%d", typmod);
    3859          30 :             break;
    3860          30 :         case DATEOID:
    3861          30 :             appendStringInfoString(&result, "DATE");
    3862          30 :             break;
    3863          30 :         case XMLOID:
    3864          30 :             appendStringInfoString(&result, "XML");
    3865          30 :             break;
    3866         222 :         default:
    3867             :             {
    3868             :                 HeapTuple   tuple;
    3869             :                 Form_pg_type typtuple;
    3870             : 
    3871         222 :                 tuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typeoid));
    3872         222 :                 if (!HeapTupleIsValid(tuple))
    3873           0 :                     elog(ERROR, "cache lookup failed for type %u", typeoid);
    3874         222 :                 typtuple = (Form_pg_type) GETSTRUCT(tuple);
    3875             : 
    3876         222 :                 appendStringInfoString(&result,
    3877         222 :                                        map_multipart_sql_identifier_to_xml_name((typtuple->typtype == TYPTYPE_DOMAIN) ? "Domain" : "UDT",
    3878         222 :                                                                                 get_database_name(MyDatabaseId),
    3879         222 :                                                                                 get_namespace_name(typtuple->typnamespace),
    3880         222 :                                                                                 NameStr(typtuple->typname)));
    3881             : 
    3882         222 :                 ReleaseSysCache(tuple);
    3883             :             }
    3884             :     }
    3885             : 
    3886         810 :     return result.data;
    3887             : }
    3888             : 
    3889             : 
    3890             : /*
    3891             :  * Map a collection of SQL data types to XML Schema data types; see
    3892             :  * SQL/XML:2008 section 9.7.
    3893             :  */
    3894             : static const char *
    3895          96 : map_sql_typecoll_to_xmlschema_types(List *tupdesc_list)
    3896             : {
    3897          96 :     List       *uniquetypes = NIL;
    3898             :     int         i;
    3899             :     StringInfoData result;
    3900             :     ListCell   *cell0;
    3901             : 
    3902             :     /* extract all column types used in the set of TupleDescs */
    3903         210 :     foreach(cell0, tupdesc_list)
    3904             :     {
    3905         114 :         TupleDesc   tupdesc = (TupleDesc) lfirst(cell0);
    3906             : 
    3907         702 :         for (i = 0; i < tupdesc->natts; i++)
    3908             :         {
    3909         588 :             Form_pg_attribute att = TupleDescAttr(tupdesc, i);
    3910             : 
    3911         588 :             if (att->attisdropped)
    3912          24 :                 continue;
    3913         564 :             uniquetypes = list_append_unique_oid(uniquetypes, att->atttypid);
    3914             :         }
    3915             :     }
    3916             : 
    3917             :     /* add base types of domains */
    3918         642 :     foreach(cell0, uniquetypes)
    3919             :     {
    3920         546 :         Oid         typid = lfirst_oid(cell0);
    3921         546 :         Oid         basetypid = getBaseType(typid);
    3922             : 
    3923         546 :         if (basetypid != typid)
    3924          24 :             uniquetypes = list_append_unique_oid(uniquetypes, basetypid);
    3925             :     }
    3926             : 
    3927             :     /* Convert to textual form */
    3928          96 :     initStringInfo(&result);
    3929             : 
    3930         642 :     foreach(cell0, uniquetypes)
    3931             :     {
    3932         546 :         appendStringInfo(&result, "%s\n",
    3933             :                          map_sql_type_to_xmlschema_type(lfirst_oid(cell0),
    3934             :                                                         -1));
    3935             :     }
    3936             : 
    3937          96 :     return result.data;
    3938             : }
    3939             : 
    3940             : 
    3941             : /*
    3942             :  * Map an SQL data type to a named XML Schema data type; see
    3943             :  * SQL/XML:2008 sections 9.5 and 9.6.
    3944             :  *
    3945             :  * (The distinction between 9.5 and 9.6 is basically that 9.6 adds
    3946             :  * a name attribute, which this function does.  The name-less version
    3947             :  * 9.5 doesn't appear to be required anywhere.)
    3948             :  */
    3949             : static const char *
    3950         546 : map_sql_type_to_xmlschema_type(Oid typeoid, int typmod)
    3951             : {
    3952             :     StringInfoData result;
    3953         546 :     const char *typename = map_sql_type_to_xml_name(typeoid, typmod);
    3954             : 
    3955         546 :     initStringInfo(&result);
    3956             : 
    3957         546 :     if (typeoid == XMLOID)
    3958             :     {
    3959          24 :         appendStringInfoString(&result,
    3960             :                                "<xsd:complexType mixed=\"true\">\n"
    3961             :                                "  <xsd:sequence>\n"
    3962             :                                "    <xsd:any name=\"element\" minOccurs=\"0\" maxOccurs=\"unbounded\" processContents=\"skip\"/>\n"
    3963             :                                "  </xsd:sequence>\n"
    3964             :                                "</xsd:complexType>\n");
    3965             :     }
    3966             :     else
    3967             :     {
    3968         522 :         appendStringInfo(&result,
    3969             :                          "<xsd:simpleType name=\"%s\">\n", typename);
    3970             : 
    3971         522 :         switch (typeoid)
    3972             :         {
    3973         138 :             case BPCHAROID:
    3974             :             case VARCHAROID:
    3975             :             case TEXTOID:
    3976         138 :                 appendStringInfoString(&result,
    3977             :                                        "  <xsd:restriction base=\"xsd:string\">\n");
    3978         138 :                 if (typmod != -1)
    3979           0 :                     appendStringInfo(&result,
    3980             :                                      "    <xsd:maxLength value=\"%d\"/>\n",
    3981             :                                      typmod - VARHDRSZ);
    3982         138 :                 appendStringInfoString(&result, "  </xsd:restriction>\n");
    3983         138 :                 break;
    3984             : 
    3985          24 :             case BYTEAOID:
    3986          24 :                 appendStringInfo(&result,
    3987             :                                  "  <xsd:restriction base=\"xsd:%s\">\n"
    3988             :                                  "  </xsd:restriction>\n",
    3989          24 :                                  xmlbinary == XMLBINARY_BASE64 ? "base64Binary" : "hexBinary");
    3990          24 :                 break;
    3991             : 
    3992          24 :             case NUMERICOID:
    3993          24 :                 if (typmod != -1)
    3994           0 :                     appendStringInfo(&result,
    3995             :                                      "  <xsd:restriction base=\"xsd:decimal\">\n"
    3996             :                                      "    <xsd:totalDigits value=\"%d\"/>\n"
    3997             :                                      "    <xsd:fractionDigits value=\"%d\"/>\n"
    3998             :                                      "  </xsd:restriction>\n",
    3999           0 :                                      ((typmod - VARHDRSZ) >> 16) & 0xffff,
    4000           0 :                                      (typmod - VARHDRSZ) & 0xffff);
    4001          24 :                 break;
    4002             : 
    4003          24 :             case INT2OID:
    4004          24 :                 appendStringInfo(&result,
    4005             :                                  "  <xsd:restriction base=\"xsd:short\">\n"
    4006             :                                  "    <xsd:maxInclusive value=\"%d\"/>\n"
    4007             :                                  "    <xsd:minInclusive value=\"%d\"/>\n"
    4008             :                                  "  </xsd:restriction>\n",
    4009             :                                  SHRT_MAX, SHRT_MIN);
    4010          24 :                 break;
    4011             : 
    4012          96 :             case INT4OID:
    4013          96 :                 appendStringInfo(&result,
    4014             :                                  "  <xsd:restriction base=\"xsd:int\">\n"
    4015             :                                  "    <xsd:maxInclusive value=\"%d\"/>\n"
    4016             :                                  "    <xsd:minInclusive value=\"%d\"/>\n"
    4017             :                                  "  </xsd:restriction>\n",
    4018             :                                  INT_MAX, INT_MIN);
    4019          96 :                 break;
    4020             : 
    4021          24 :             case INT8OID:
    4022          24 :                 appendStringInfo(&result,
    4023             :                                  "  <xsd:restriction base=\"xsd:long\">\n"
    4024             :                                  "    <xsd:maxInclusive value=\"" INT64_FORMAT "\"/>\n"
    4025             :                                  "    <xsd:minInclusive value=\"" INT64_FORMAT "\"/>\n"
    4026             :                                  "  </xsd:restriction>\n",
    4027             :                                  PG_INT64_MAX,
    4028             :                                  PG_INT64_MIN);
    4029          24 :                 break;
    4030             : 
    4031          24 :             case FLOAT4OID:
    4032          24 :                 appendStringInfoString(&result,
    4033             :                                        "  <xsd:restriction base=\"xsd:float\"></xsd:restriction>\n");
    4034          24 :                 break;
    4035             : 
    4036           0 :             case FLOAT8OID:
    4037           0 :                 appendStringInfoString(&result,
    4038             :                                        "  <xsd:restriction base=\"xsd:double\"></xsd:restriction>\n");
    4039           0 :                 break;
    4040             : 
    4041          24 :             case BOOLOID:
    4042          24 :                 appendStringInfoString(&result,
    4043             :                                        "  <xsd:restriction base=\"xsd:boolean\"></xsd:restriction>\n");
    4044          24 :                 break;
    4045             : 
    4046          48 :             case TIMEOID:
    4047             :             case TIMETZOID:
    4048             :                 {
    4049          48 :                     const char *tz = (typeoid == TIMETZOID ? "(\\+|-)\\p{Nd}{2}:\\p{Nd}{2}" : "");
    4050             : 
    4051          48 :                     if (typmod == -1)
    4052          48 :                         appendStringInfo(&result,
    4053             :                                          "  <xsd:restriction base=\"xsd:time\">\n"
    4054             :                                          "    <xsd:pattern value=\"\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}(.\\p{Nd}+)?%s\"/>\n"
    4055             :                                          "  </xsd:restriction>\n", tz);
    4056           0 :                     else if (typmod == 0)
    4057           0 :                         appendStringInfo(&result,
    4058             :                                          "  <xsd:restriction base=\"xsd:time\">\n"
    4059             :                                          "    <xsd:pattern value=\"\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}%s\"/>\n"
    4060             :                                          "  </xsd:restriction>\n", tz);
    4061             :                     else
    4062           0 :                         appendStringInfo(&result,
    4063             :                                          "  <xsd:restriction base=\"xsd:time\">\n"
    4064             :                                          "    <xsd:pattern value=\"\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}.\\p{Nd}{%d}%s\"/>\n"
    4065             :                                          "  </xsd:restriction>\n", typmod - VARHDRSZ, tz);
    4066          48 :                     break;
    4067             :                 }
    4068             : 
    4069          48 :             case TIMESTAMPOID:
    4070             :             case TIMESTAMPTZOID:
    4071             :                 {
    4072          48 :                     const char *tz = (typeoid == TIMESTAMPTZOID ? "(\\+|-)\\p{Nd}{2}:\\p{Nd}{2}" : "");
    4073             : 
    4074          48 :                     if (typmod == -1)
    4075          48 :                         appendStringInfo(&result,
    4076             :                                          "  <xsd:restriction base=\"xsd:dateTime\">\n"
    4077             :                                          "    <xsd:pattern value=\"\\p{Nd}{4}-\\p{Nd}{2}-\\p{Nd}{2}T\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}(.\\p{Nd}+)?%s\"/>\n"
    4078             :                                          "  </xsd:restriction>\n", tz);
    4079           0 :                     else if (typmod == 0)
    4080           0 :                         appendStringInfo(&result,
    4081             :                                          "  <xsd:restriction base=\"xsd:dateTime\">\n"
    4082             :                                          "    <xsd:pattern value=\"\\p{Nd}{4}-\\p{Nd}{2}-\\p{Nd}{2}T\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}%s\"/>\n"
    4083             :                                          "  </xsd:restriction>\n", tz);
    4084             :                     else
    4085           0 :                         appendStringInfo(&result,
    4086             :                                          "  <xsd:restriction base=\"xsd:dateTime\">\n"
    4087             :                                          "    <xsd:pattern value=\"\\p{Nd}{4}-\\p{Nd}{2}-\\p{Nd}{2}T\\p{Nd}{2}:\\p{Nd}{2}:\\p{Nd}{2}.\\p{Nd}{%d}%s\"/>\n"
    4088             :                                          "  </xsd:restriction>\n", typmod - VARHDRSZ, tz);
    4089          48 :                     break;
    4090             :                 }
    4091             : 
    4092          24 :             case DATEOID:
    4093          24 :                 appendStringInfoString(&result,
    4094             :                                        "  <xsd:restriction base=\"xsd:date\">\n"
    4095             :                                        "    <xsd:pattern value=\"\\p{Nd}{4}-\\p{Nd}{2}-\\p{Nd}{2}\"/>\n"
    4096             :                                        "  </xsd:restriction>\n");
    4097          24 :                 break;
    4098             : 
    4099          24 :             default:
    4100          24 :                 if (get_typtype(typeoid) == TYPTYPE_DOMAIN)
    4101             :                 {
    4102             :                     Oid         base_typeoid;
    4103          24 :                     int32       base_typmod = -1;
    4104             : 
    4105          24 :                     base_typeoid = getBaseTypeAndTypmod(typeoid, &base_typmod);
    4106             : 
    4107          24 :                     appendStringInfo(&result,
    4108             :                                      "  <xsd:restriction base=\"%s\"/>\n",
    4109             :                                      map_sql_type_to_xml_name(base_typeoid, base_typmod));
    4110             :                 }
    4111          24 :                 break;
    4112             :         }
    4113         522 :         appendStringInfoString(&result, "</xsd:simpleType>\n");
    4114             :     }
    4115             : 
    4116         546 :     return result.data;
    4117             : }
    4118             : 
    4119             : 
    4120             : /*
    4121             :  * Map an SQL row to an XML element, taking the row from the active
    4122             :  * SPI cursor.  See also SQL/XML:2008 section 9.10.
    4123             :  */
    4124             : static void
    4125         312 : SPI_sql_row_to_xmlelement(uint64 rownum, StringInfo result, char *tablename,
    4126             :                           bool nulls, bool tableforest,
    4127             :                           const char *targetns, bool top_level)
    4128             : {
    4129             :     int         i;
    4130             :     char       *xmltn;
    4131             : 
    4132         312 :     if (tablename)
    4133         228 :         xmltn = map_sql_identifier_to_xml_name(tablename, true, false);
    4134             :     else
    4135             :     {
    4136          84 :         if (tableforest)
    4137          36 :             xmltn = "row";
    4138             :         else
    4139          48 :             xmltn = "table";
    4140             :     }
    4141             : 
    4142         312 :     if (tableforest)
    4143         162 :         xmldata_root_element_start(result, xmltn, NULL, targetns, top_level);
    4144             :     else
    4145         150 :         appendStringInfoString(result, "<row>\n");
    4146             : 
    4147        1272 :     for (i = 1; i <= SPI_tuptable->tupdesc->natts; i++)
    4148             :     {
    4149             :         char       *colname;
    4150             :         Datum       colval;
    4151             :         bool        isnull;
    4152             : 
    4153         960 :         colname = map_sql_identifier_to_xml_name(SPI_fname(SPI_tuptable->tupdesc, i),
    4154             :                                                  true, false);
    4155         960 :         colval = SPI_getbinval(SPI_tuptable->vals[rownum],
    4156         960 :                                SPI_tuptable->tupdesc,
    4157             :                                i,
    4158             :                                &isnull);
    4159         960 :         if (isnull)
    4160             :         {
    4161         114 :             if (nulls)
    4162          60 :                 appendStringInfo(result, "  <%s xsi:nil=\"true\"/>\n", colname);
    4163             :         }
    4164             :         else
    4165         846 :             appendStringInfo(result, "  <%s>%s</%s>\n",
    4166             :                              colname,
    4167             :                              map_sql_value_to_xml_value(colval,
    4168         846 :                                                         SPI_gettypeid(SPI_tuptable->tupdesc, i), true),
    4169             :                              colname);
    4170             :     }
    4171             : 
    4172         312 :     if (tableforest)
    4173             :     {
    4174         162 :         xmldata_root_element_end(result, xmltn);
    4175         162 :         appendStringInfoChar(result, '\n');
    4176             :     }
    4177             :     else
    4178         150 :         appendStringInfoString(result, "</row>\n\n");
    4179         312 : }
    4180             : 
    4181             : 
    4182             : /*
    4183             :  * XPath related functions
    4184             :  */
    4185             : 
    4186             : #ifdef USE_LIBXML
    4187             : 
    4188             : /*
    4189             :  * Convert XML node to text.
    4190             :  *
    4191             :  * For attribute and text nodes, return the escaped text.  For anything else,
    4192             :  * dump the whole subtree.
    4193             :  */
    4194             : static text *
    4195         192 : xml_xmlnodetoxmltype(xmlNodePtr cur, PgXmlErrorContext *xmlerrcxt)
    4196             : {
    4197         192 :     xmltype    *result = NULL;
    4198             : 
    4199         192 :     if (cur->type != XML_ATTRIBUTE_NODE && cur->type != XML_TEXT_NODE)
    4200         162 :     {
    4201         162 :         void        (*volatile nodefree) (xmlNodePtr) = NULL;
    4202         162 :         volatile xmlBufferPtr buf = NULL;
    4203         162 :         volatile xmlNodePtr cur_copy = NULL;
    4204             : 
    4205         162 :         PG_TRY();
    4206             :         {
    4207             :             int         bytes;
    4208             : 
    4209         162 :             buf = xmlBufferCreate();
    4210         162 :             if (buf == NULL || xmlerrcxt->err_occurred)
    4211           0 :                 xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
    4212             :                             "could not allocate xmlBuffer");
    4213             : 
    4214             :             /*
    4215             :              * Produce a dump of the node that we can serialize.  xmlNodeDump
    4216             :              * does that, but the result of that function won't contain
    4217             :              * namespace definitions from ancestor nodes, so we first do a
    4218             :              * xmlCopyNode() which duplicates the node along with its required
    4219             :              * namespace definitions.
    4220             :              *
    4221             :              * Some old libxml2 versions such as 2.7.6 produce partially
    4222             :              * broken XML_DOCUMENT_NODE nodes (unset content field) when
    4223             :              * copying them.  xmlNodeDump of such a node works fine, but
    4224             :              * xmlFreeNode crashes; set us up to call xmlFreeDoc instead.
    4225             :              */
    4226         162 :             cur_copy = xmlCopyNode(cur, 1);
    4227         162 :             if (cur_copy == NULL || xmlerrcxt->err_occurred)
    4228           0 :                 xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
    4229             :                             "could not copy node");
    4230         324 :             nodefree = (cur_copy->type == XML_DOCUMENT_NODE) ?
    4231         162 :                 (void (*) (xmlNodePtr)) xmlFreeDoc : xmlFreeNode;
    4232             : 
    4233         162 :             bytes = xmlNodeDump(buf, NULL, cur_copy, 0, 0);
    4234         162 :             if (bytes == -1 || xmlerrcxt->err_occurred)
    4235           0 :                 xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
    4236             :                             "could not dump node");
    4237             : 
    4238         162 :             result = xmlBuffer_to_xmltype(buf);
    4239             :         }
    4240           0 :         PG_FINALLY();
    4241             :         {
    4242         162 :             if (nodefree)
    4243         162 :                 nodefree(cur_copy);
    4244         162 :             if (buf)
    4245         162 :                 xmlBufferFree(buf);
    4246             :         }
    4247         162 :         PG_END_TRY();
    4248             :     }
    4249             :     else
    4250             :     {
    4251          30 :         volatile xmlChar *str = NULL;
    4252             : 
    4253          30 :         PG_TRY();
    4254             :         {
    4255             :             char       *escaped;
    4256             : 
    4257          30 :             str = xmlXPathCastNodeToString(cur);
    4258          30 :             if (str == NULL || xmlerrcxt->err_occurred)
    4259           0 :                 xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
    4260             :                             "could not allocate xmlChar");
    4261             : 
    4262             :             /* Here we rely on XML having the same representation as TEXT */
    4263          30 :             escaped = escape_xml((char *) str);
    4264             : 
    4265          30 :             result = (xmltype *) cstring_to_text(escaped);
    4266          30 :             pfree(escaped);
    4267             :         }
    4268           0 :         PG_FINALLY();
    4269             :         {
    4270          30 :             if (str)
    4271          30 :                 xmlFree((xmlChar *) str);
    4272             :         }
    4273          30 :         PG_END_TRY();
    4274             :     }
    4275             : 
    4276         192 :     return result;
    4277             : }
    4278             : 
    4279             : /*
    4280             :  * Convert an XML XPath object (the result of evaluating an XPath expression)
    4281             :  * to an array of xml values, which are appended to astate.  The function
    4282             :  * result value is the number of elements in the array.
    4283             :  *
    4284             :  * If "astate" is NULL then we don't generate the array value, but we still
    4285             :  * return the number of elements it would have had.
    4286             :  *
    4287             :  * Nodesets are converted to an array containing the nodes' textual
    4288             :  * representations.  Primitive values (float, double, string) are converted
    4289             :  * to a single-element array containing the value's string representation.
    4290             :  */
    4291             : static int
    4292         540 : xml_xpathobjtoxmlarray(xmlXPathObjectPtr xpathobj,
    4293             :                        ArrayBuildState *astate,
    4294             :                        PgXmlErrorContext *xmlerrcxt)
    4295             : {
    4296         540 :     int         result = 0;
    4297             :     Datum       datum;
    4298             :     Oid         datumtype;
    4299             :     char       *result_str;
    4300             : 
    4301         540 :     switch (xpathobj->type)
    4302             :     {
    4303         498 :         case XPATH_NODESET:
    4304         498 :             if (xpathobj->nodesetval != NULL)
    4305             :             {
    4306         354 :                 result = xpathobj->nodesetval->nodeNr;
    4307         354 :                 if (astate != NULL)
    4308             :                 {
    4309             :                     int         i;
    4310             : 
    4311         168 :                     for (i = 0; i < result; i++)
    4312             :                     {
    4313          90 :                         datum = PointerGetDatum(xml_xmlnodetoxmltype(xpathobj->nodesetval->nodeTab[i],
    4314             :                                                                      xmlerrcxt));
    4315          90 :                         (void) accumArrayResult(astate, datum, false,
    4316             :                                                 XMLOID, CurrentMemoryContext);
    4317             :                     }
    4318             :                 }
    4319             :             }
    4320         498 :             return result;
    4321             : 
    4322          12 :         case XPATH_BOOLEAN:
    4323          12 :             if (astate == NULL)
    4324           0 :                 return 1;
    4325          12 :             datum = BoolGetDatum(xpathobj->boolval);
    4326          12 :             datumtype = BOOLOID;
    4327          12 :             break;
    4328             : 
    4329          18 :         case XPATH_NUMBER:
    4330          18 :             if (astate == NULL)
    4331          12 :                 return 1;
    4332           6 :             datum = Float8GetDatum(xpathobj->floatval);
    4333           6 :             datumtype = FLOAT8OID;
    4334           6 :             break;
    4335             : 
    4336          12 :         case XPATH_STRING:
    4337          12 :             if (astate == NULL)
    4338           0 :                 return 1;
    4339          12 :             datum = CStringGetDatum((char *) xpathobj->stringval);
    4340          12 :             datumtype = CSTRINGOID;
    4341          12 :             break;
    4342             : 
    4343           0 :         default:
    4344           0 :             elog(ERROR, "xpath expression result type %d is unsupported",
    4345             :                  xpathobj->type);
    4346             :             return 0;           /* keep compiler quiet */
    4347             :     }
    4348             : 
    4349             :     /* Common code for scalar-value cases */
    4350          30 :     result_str = map_sql_value_to_xml_value(datum, datumtype, true);
    4351          30 :     datum = PointerGetDatum(cstring_to_xmltype(result_str));
    4352          30 :     (void) accumArrayResult(astate, datum, false,
    4353             :                             XMLOID, CurrentMemoryContext);
    4354          30 :     return 1;
    4355             : }
    4356             : 
    4357             : 
    4358             : /*
    4359             :  * Common code for xpath() and xmlexists()
    4360             :  *
    4361             :  * Evaluate XPath expression and return number of nodes in res_nitems
    4362             :  * and array of XML values in astate.  Either of those pointers can be
    4363             :  * NULL if the corresponding result isn't wanted.
    4364             :  *
    4365             :  * It is up to the user to ensure that the XML passed is in fact
    4366             :  * an XML document - XPath doesn't work easily on fragments without
    4367             :  * a context node being known.
    4368             :  */
    4369             : static void
    4370         558 : xpath_internal(text *xpath_expr_text, xmltype *data, ArrayType *namespaces,
    4371             :                int *res_nitems, ArrayBuildState *astate)
    4372             : {
    4373             :     PgXmlErrorContext *xmlerrcxt;
    4374         558 :     volatile xmlParserCtxtPtr ctxt = NULL;
    4375         558 :     volatile xmlDocPtr doc = NULL;
    4376         558 :     volatile xmlXPathContextPtr xpathctx = NULL;
    4377         558 :     volatile xmlXPathCompExprPtr xpathcomp = NULL;
    4378         558 :     volatile xmlXPathObjectPtr xpathobj = NULL;
    4379             :     char       *datastr;
    4380             :     int32       len;
    4381             :     int32       xpath_len;
    4382             :     xmlChar    *string;
    4383             :     xmlChar    *xpath_expr;
    4384         558 :     size_t      xmldecl_len = 0;
    4385             :     int         i;
    4386             :     int         ndim;
    4387             :     Datum      *ns_names_uris;
    4388             :     bool       *ns_names_uris_nulls;
    4389             :     int         ns_count;
    4390             : 
    4391             :     /*
    4392             :      * Namespace mappings are passed as text[].  If an empty array is passed
    4393             :      * (ndim = 0, "0-dimensional"), then there are no namespace mappings.
    4394             :      * Else, a 2-dimensional array with length of the second axis being equal
    4395             :      * to 2 should be passed, i.e., every subarray contains 2 elements, the
    4396             :      * first element defining the name, the second one the URI.  Example:
    4397             :      * ARRAY[ARRAY['myns', 'http://example.com'], ARRAY['myns2',
    4398             :      * 'http://example2.com']].
    4399             :      */
    4400         558 :     ndim = namespaces ? ARR_NDIM(namespaces) : 0;
    4401         558 :     if (ndim != 0)
    4402             :     {
    4403             :         int        *dims;
    4404             : 
    4405         126 :         dims = ARR_DIMS(namespaces);
    4406             : 
    4407         126 :         if (ndim != 2 || dims[1] != 2)
    4408           0 :             ereport(ERROR,
    4409             :                     (errcode(ERRCODE_DATA_EXCEPTION),
    4410             :                      errmsg("invalid array for XML namespace mapping"),
    4411             :                      errdetail("The array must be two-dimensional with length of the second axis equal to 2.")));
    4412             : 
    4413             :         Assert(ARR_ELEMTYPE(namespaces) == TEXTOID);
    4414             : 
    4415         126 :         deconstruct_array_builtin(namespaces, TEXTOID,
    4416             :                                   &ns_names_uris, &ns_names_uris_nulls,
    4417             :                                   &ns_count);
    4418             : 
    4419             :         Assert((ns_count % 2) == 0);    /* checked above */
    4420         126 :         ns_count /= 2;          /* count pairs only */
    4421             :     }
    4422             :     else
    4423             :     {
    4424         432 :         ns_names_uris = NULL;
    4425         432 :         ns_names_uris_nulls = NULL;
    4426         432 :         ns_count = 0;
    4427             :     }
    4428             : 
    4429         558 :     datastr = VARDATA(data);
    4430         558 :     len = VARSIZE(data) - VARHDRSZ;
    4431         558 :     xpath_len = VARSIZE_ANY_EXHDR(xpath_expr_text);
    4432         558 :     if (xpath_len == 0)
    4433           6 :         ereport(ERROR,
    4434             :                 (errcode(ERRCODE_INVALID_ARGUMENT_FOR_XQUERY),
    4435             :                  errmsg("empty XPath expression")));
    4436             : 
    4437         552 :     string = pg_xmlCharStrndup(datastr, len);
    4438         552 :     xpath_expr = pg_xmlCharStrndup(VARDATA_ANY(xpath_expr_text), xpath_len);
    4439             : 
    4440             :     /*
    4441             :      * In a UTF8 database, skip any xml declaration, which might assert
    4442             :      * another encoding.  Ignore parse_xml_decl() failure, letting
    4443             :      * xmlCtxtReadMemory() report parse errors.  Documentation disclaims
    4444             :      * xpath() support for non-ASCII data in non-UTF8 databases, so leave
    4445             :      * those scenarios bug-compatible with historical behavior.
    4446             :      */
    4447         552 :     if (GetDatabaseEncoding() == PG_UTF8)
    4448         552 :         parse_xml_decl(string, &xmldecl_len, NULL, NULL, NULL);
    4449             : 
    4450         552 :     xmlerrcxt = pg_xml_init(PG_XML_STRICTNESS_ALL);
    4451             : 
    4452         552 :     PG_TRY();
    4453             :     {
    4454         552 :         xmlInitParser();
    4455             : 
    4456             :         /*
    4457             :          * redundant XML parsing (two parsings for the same value during one
    4458             :          * command execution are possible)
    4459             :          */
    4460         552 :         ctxt = xmlNewParserCtxt();
    4461         552 :         if (ctxt == NULL || xmlerrcxt->err_occurred)
    4462           0 :             xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
    4463             :                         "could not allocate parser context");
    4464        1104 :         doc = xmlCtxtReadMemory(ctxt, (char *) string + xmldecl_len,
    4465         552 :                                 len - xmldecl_len, NULL, NULL, 0);
    4466         552 :         if (doc == NULL || xmlerrcxt->err_occurred)
    4467          12 :             xml_ereport(xmlerrcxt, ERROR, ERRCODE_INVALID_XML_DOCUMENT,
    4468             :                         "could not parse XML document");
    4469         540 :         xpathctx = xmlXPathNewContext(doc);
    4470         540 :         if (xpathctx == NULL || xmlerrcxt->err_occurred)
    4471           0 :             xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
    4472             :                         "could not allocate XPath context");
    4473         540 :         xpathctx->node = (xmlNodePtr) doc;
    4474             : 
    4475             :         /* register namespaces, if any */
    4476         540 :         if (ns_count > 0)
    4477             :         {
    4478         252 :             for (i = 0; i < ns_count; i++)
    4479             :             {
    4480             :                 char       *ns_name;
    4481             :                 char       *ns_uri;
    4482             : 
    4483         126 :                 if (ns_names_uris_nulls[i * 2] ||
    4484         126 :                     ns_names_uris_nulls[i * 2 + 1])
    4485           0 :                     ereport(ERROR,
    4486             :                             (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
    4487             :                              errmsg("neither namespace name nor URI may be null")));
    4488         126 :                 ns_name = TextDatumGetCString(ns_names_uris[i * 2]);
    4489         126 :                 ns_uri = TextDatumGetCString(ns_names_uris[i * 2 + 1]);
    4490         126 :                 if (xmlXPathRegisterNs(xpathctx,
    4491             :                                        (xmlChar *) ns_name,
    4492             :                                        (xmlChar *) ns_uri) != 0)
    4493           0 :                     ereport(ERROR,  /* is this an internal error??? */
    4494             :                             (errmsg("could not register XML namespace with name \"%s\" and URI \"%s\"",
    4495             :                                     ns_name, ns_uri)));
    4496             :             }
    4497             :         }
    4498             : 
    4499             :         /*
    4500             :          * Note: here and elsewhere, be careful to use xmlXPathCtxtCompile not
    4501             :          * xmlXPathCompile.  In libxml2 2.13.3 and older, the latter function
    4502             :          * fails to defend itself against recursion-to-stack-overflow.  See
    4503             :          * https://gitlab.gnome.org/GNOME/libxml2/-/issues/799
    4504             :          */
    4505         540 :         xpathcomp = xmlXPathCtxtCompile(xpathctx, xpath_expr);
    4506         540 :         if (xpathcomp == NULL || xmlerrcxt->err_occurred)
    4507           0 :             xml_ereport(xmlerrcxt, ERROR, ERRCODE_INVALID_ARGUMENT_FOR_XQUERY,
    4508             :                         "invalid XPath expression");
    4509             : 
    4510             :         /*
    4511             :          * Version 2.6.27 introduces a function named
    4512             :          * xmlXPathCompiledEvalToBoolean, which would be enough for xmlexists,
    4513             :          * but we can derive the existence by whether any nodes are returned,
    4514             :          * thereby preventing a library version upgrade and keeping the code
    4515             :          * the same.
    4516             :          */
    4517         540 :         xpathobj = xmlXPathCompiledEval(xpathcomp, xpathctx);
    4518         540 :         if (xpathobj == NULL || xmlerrcxt->err_occurred)
    4519           0 :             xml_ereport(xmlerrcxt, ERROR, ERRCODE_INVALID_ARGUMENT_FOR_XQUERY,
    4520             :                         "could not create XPath object");
    4521             : 
    4522             :         /*
    4523             :          * Extract the results as requested.
    4524             :          */
    4525         540 :         if (res_nitems != NULL)
    4526         432 :             *res_nitems = xml_xpathobjtoxmlarray(xpathobj, astate, xmlerrcxt);
    4527             :         else
    4528         108 :             (void) xml_xpathobjtoxmlarray(xpathobj, astate, xmlerrcxt);
    4529             :     }
    4530          12 :     PG_CATCH();
    4531             :     {
    4532          12 :         if (xpathobj)
    4533           0 :             xmlXPathFreeObject(xpathobj);
    4534          12 :         if (xpathcomp)
    4535           0 :             xmlXPathFreeCompExpr(xpathcomp);
    4536          12 :         if (xpathctx)
    4537           0 :             xmlXPathFreeContext(xpathctx);
    4538          12 :         if (doc)
    4539          12 :             xmlFreeDoc(doc);
    4540          12 :         if (ctxt)
    4541          12 :             xmlFreeParserCtxt(ctxt);
    4542             : 
    4543          12 :         pg_xml_done(xmlerrcxt, true);
    4544             : 
    4545          12 :         PG_RE_THROW();
    4546             :     }
    4547         540 :     PG_END_TRY();
    4548             : 
    4549         540 :     xmlXPathFreeObject(xpathobj);
    4550         540 :     xmlXPathFreeCompExpr(xpathcomp);
    4551         540 :     xmlXPathFreeContext(xpathctx);
    4552         540 :     xmlFreeDoc(doc);
    4553         540 :     xmlFreeParserCtxt(ctxt);
    4554             : 
    4555         540 :     pg_xml_done(xmlerrcxt, false);
    4556         540 : }
    4557             : #endif                          /* USE_LIBXML */
    4558             : 
    4559             : /*
    4560             :  * Evaluate XPath expression and return array of XML values.
    4561             :  *
    4562             :  * As we have no support of XQuery sequences yet, this function seems
    4563             :  * to be the most useful one (array of XML functions plays a role of
    4564             :  * some kind of substitution for XQuery sequences).
    4565             :  */
    4566             : Datum
    4567         126 : xpath(PG_FUNCTION_ARGS)
    4568             : {
    4569             : #ifdef USE_LIBXML
    4570         126 :     text       *xpath_expr_text = PG_GETARG_TEXT_PP(0);
    4571         126 :     xmltype    *data = PG_GETARG_XML_P(1);
    4572         126 :     ArrayType  *namespaces = PG_GETARG_ARRAYTYPE_P(2);
    4573             :     ArrayBuildState *astate;
    4574             : 
    4575         126 :     astate = initArrayResult(XMLOID, CurrentMemoryContext, true);
    4576         126 :     xpath_internal(xpath_expr_text, data, namespaces,
    4577             :                    NULL, astate);
    4578         108 :     PG_RETURN_DATUM(makeArrayResult(astate, CurrentMemoryContext));
    4579             : #else
    4580             :     NO_XML_SUPPORT();
    4581             :     return 0;
    4582             : #endif
    4583             : }
    4584             : 
    4585             : /*
    4586             :  * Determines if the node specified by the supplied XPath exists
    4587             :  * in a given XML document, returning a boolean.
    4588             :  */
    4589             : Datum
    4590         198 : xmlexists(PG_FUNCTION_ARGS)
    4591             : {
    4592             : #ifdef USE_LIBXML
    4593         198 :     text       *xpath_expr_text = PG_GETARG_TEXT_PP(0);
    4594         198 :     xmltype    *data = PG_GETARG_XML_P(1);
    4595             :     int         res_nitems;
    4596             : 
    4597         198 :     xpath_internal(xpath_expr_text, data, NULL,
    4598             :                    &res_nitems, NULL);
    4599             : 
    4600         198 :     PG_RETURN_BOOL(res_nitems > 0);
    4601             : #else
    4602             :     NO_XML_SUPPORT();
    4603             :     return 0;
    4604             : #endif
    4605             : }
    4606             : 
    4607             : /*
    4608             :  * Determines if the node specified by the supplied XPath exists
    4609             :  * in a given XML document, returning a boolean. Differs from
    4610             :  * xmlexists as it supports namespaces and is not defined in SQL/XML.
    4611             :  */
    4612             : Datum
    4613         234 : xpath_exists(PG_FUNCTION_ARGS)
    4614             : {
    4615             : #ifdef USE_LIBXML
    4616         234 :     text       *xpath_expr_text = PG_GETARG_TEXT_PP(0);
    4617         234 :     xmltype    *data = PG_GETARG_XML_P(1);
    4618         234 :     ArrayType  *namespaces = PG_GETARG_ARRAYTYPE_P(2);
    4619             :     int         res_nitems;
    4620             : 
    4621         234 :     xpath_internal(xpath_expr_text, data, namespaces,
    4622             :                    &res_nitems, NULL);
    4623             : 
    4624         234 :     PG_RETURN_BOOL(res_nitems > 0);
    4625             : #else
    4626             :     NO_XML_SUPPORT();
    4627             :     return 0;
    4628             : #endif
    4629             : }
    4630             : 
    4631             : /*
    4632             :  * Functions for checking well-formed-ness
    4633             :  */
    4634             : 
    4635             : #ifdef USE_LIBXML
    4636             : static bool
    4637         114 : wellformed_xml(text *data, XmlOptionType xmloption_arg)
    4638             : {
    4639             :     xmlDocPtr   doc;
    4640         114 :     ErrorSaveContext escontext = {T_ErrorSaveContext};
    4641             : 
    4642             :     /*
    4643             :      * We'll report "true" if no soft error is reported by xml_parse().
    4644             :      */
    4645         114 :     doc = xml_parse(data, xmloption_arg, true,
    4646             :                     GetDatabaseEncoding(), NULL, NULL, (Node *) &escontext);
    4647         114 :     if (doc)
    4648          60 :         xmlFreeDoc(doc);
    4649             : 
    4650         114 :     return !escontext.error_occurred;
    4651             : }
    4652             : #endif
    4653             : 
    4654             : Datum
    4655          90 : xml_is_well_formed(PG_FUNCTION_ARGS)
    4656             : {
    4657             : #ifdef USE_LIBXML
    4658          90 :     text       *data = PG_GETARG_TEXT_PP(0);
    4659             : 
    4660          90 :     PG_RETURN_BOOL(wellformed_xml(data, xmloption));
    4661             : #else
    4662             :     NO_XML_SUPPORT();
    4663             :     return 0;
    4664             : #endif                          /* not USE_LIBXML */
    4665             : }
    4666             : 
    4667             : Datum
    4668          12 : xml_is_well_formed_document(PG_FUNCTION_ARGS)
    4669             : {
    4670             : #ifdef USE_LIBXML
    4671          12 :     text       *data = PG_GETARG_TEXT_PP(0);
    4672             : 
    4673          12 :     PG_RETURN_BOOL(wellformed_xml(data, XMLOPTION_DOCUMENT));
    4674             : #else
    4675             :     NO_XML_SUPPORT();
    4676             :     return 0;
    4677             : #endif                          /* not USE_LIBXML */
    4678             : }
    4679             : 
    4680             : Datum
    4681          12 : xml_is_well_formed_content(PG_FUNCTION_ARGS)
    4682             : {
    4683             : #ifdef USE_LIBXML
    4684          12 :     text       *data = PG_GETARG_TEXT_PP(0);
    4685             : 
    4686          12 :     PG_RETURN_BOOL(wellformed_xml(data, XMLOPTION_CONTENT));
    4687             : #else
    4688             :     NO_XML_SUPPORT();
    4689             :     return 0;
    4690             : #endif                          /* not USE_LIBXML */
    4691             : }
    4692             : 
    4693             : /*
    4694             :  * support functions for XMLTABLE
    4695             :  *
    4696             :  */
    4697             : #ifdef USE_LIBXML
    4698             : 
    4699             : /*
    4700             :  * Returns private data from executor state. Ensure validity by check with
    4701             :  * MAGIC number.
    4702             :  */
    4703             : static inline XmlTableBuilderData *
    4704      160256 : GetXmlTableBuilderPrivateData(TableFuncScanState *state, const char *fname)
    4705             : {
    4706             :     XmlTableBuilderData *result;
    4707             : 
    4708      160256 :     if (!IsA(state, TableFuncScanState))
    4709           0 :         elog(ERROR, "%s called with invalid TableFuncScanState", fname);
    4710      160256 :     result = (XmlTableBuilderData *) state->opaque;
    4711      160256 :     if (result->magic != XMLTABLE_CONTEXT_MAGIC)
    4712           0 :         elog(ERROR, "%s called with invalid TableFuncScanState", fname);
    4713             : 
    4714      160256 :     return result;
    4715             : }
    4716             : #endif
    4717             : 
    4718             : /*
    4719             :  * XmlTableInitOpaque
    4720             :  *      Fill in TableFuncScanState->opaque for XmlTable processor; initialize
    4721             :  *      the XML parser.
    4722             :  *
    4723             :  * Note: Because we call pg_xml_init() here and pg_xml_done() in
    4724             :  * XmlTableDestroyOpaque, it is critical for robustness that no other
    4725             :  * executor nodes run until this node is processed to completion.  Caller
    4726             :  * must execute this to completion (probably filling a tuplestore to exhaust
    4727             :  * this node in a single pass) instead of using row-per-call mode.
    4728             :  */
    4729             : static void
    4730         264 : XmlTableInitOpaque(TableFuncScanState *state, int natts)
    4731             : {
    4732             : #ifdef USE_LIBXML
    4733         264 :     volatile xmlParserCtxtPtr ctxt = NULL;
    4734             :     XmlTableBuilderData *xtCxt;
    4735             :     PgXmlErrorContext *xmlerrcxt;
    4736             : 
    4737         264 :     xtCxt = palloc0(sizeof(XmlTableBuilderData));
    4738         264 :     xtCxt->magic = XMLTABLE_CONTEXT_MAGIC;
    4739         264 :     xtCxt->natts = natts;
    4740         264 :     xtCxt->xpathscomp = palloc0(sizeof(xmlXPathCompExprPtr) * natts);
    4741             : 
    4742         264 :     xmlerrcxt = pg_xml_init(PG_XML_STRICTNESS_ALL);
    4743             : 
    4744         264 :     PG_TRY();
    4745             :     {
    4746         264 :         xmlInitParser();
    4747             : 
    4748         264 :         ctxt = xmlNewParserCtxt();
    4749         264 :         if (ctxt == NULL || xmlerrcxt->err_occurred)
    4750           0 :             xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
    4751             :                         "could not allocate parser context");
    4752             :     }
    4753           0 :     PG_CATCH();
    4754             :     {
    4755           0 :         if (ctxt != NULL)
    4756           0 :             xmlFreeParserCtxt(ctxt);
    4757             : 
    4758           0 :         pg_xml_done(xmlerrcxt, true);
    4759             : 
    4760           0 :         PG_RE_THROW();
    4761             :     }
    4762         264 :     PG_END_TRY();
    4763             : 
    4764         264 :     xtCxt->xmlerrcxt = xmlerrcxt;
    4765         264 :     xtCxt->ctxt = ctxt;
    4766             : 
    4767         264 :     state->opaque = xtCxt;
    4768             : #else
    4769             :     NO_XML_SUPPORT();
    4770             : #endif                          /* not USE_LIBXML */
    4771         264 : }
    4772             : 
    4773             : /*
    4774             :  * XmlTableSetDocument
    4775             :  *      Install the input document
    4776             :  */
    4777             : static void
    4778         264 : XmlTableSetDocument(TableFuncScanState *state, Datum value)
    4779             : {
    4780             : #ifdef USE_LIBXML
    4781             :     XmlTableBuilderData *xtCxt;
    4782         264 :     xmltype    *xmlval = DatumGetXmlP(value);
    4783             :     char       *str;
    4784             :     xmlChar    *xstr;
    4785             :     int         length;
    4786         264 :     volatile xmlDocPtr doc = NULL;
    4787         264 :     volatile xmlXPathContextPtr xpathcxt = NULL;
    4788             : 
    4789         264 :     xtCxt = GetXmlTableBuilderPrivateData(state, "XmlTableSetDocument");
    4790             : 
    4791             :     /*
    4792             :      * Use out function for casting to string (remove encoding property). See
    4793             :      * comment in xml_out.
    4794             :      */
    4795         264 :     str = xml_out_internal(xmlval, 0);
    4796             : 
    4797         264 :     length = strlen(str);
    4798         264 :     xstr = pg_xmlCharStrndup(str, length);
    4799             : 
    4800         264 :     PG_TRY();
    4801             :     {
    4802         264 :         doc = xmlCtxtReadMemory(xtCxt->ctxt, (char *) xstr, length, NULL, NULL, 0);
    4803         264 :         if (doc == NULL || xtCxt->xmlerrcxt->err_occurred)
    4804           0 :             xml_ereport(xtCxt->xmlerrcxt, ERROR, ERRCODE_INVALID_XML_DOCUMENT,
    4805             :                         "could not parse XML document");
    4806         264 :         xpathcxt = xmlXPathNewContext(doc);
    4807         264 :         if (xpathcxt == NULL || xtCxt->xmlerrcxt->err_occurred)
    4808           0 :             xml_ereport(xtCxt->xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
    4809             :                         "could not allocate XPath context");
    4810         264 :         xpathcxt->node = (xmlNodePtr) doc;
    4811             :     }
    4812           0 :     PG_CATCH();
    4813             :     {
    4814           0 :         if (xpathcxt != NULL)
    4815           0 :             xmlXPathFreeContext(xpathcxt);
    4816           0 :         if (doc != NULL)
    4817           0 :             xmlFreeDoc(doc);
    4818             : 
    4819           0 :         PG_RE_THROW();
    4820             :     }
    4821         264 :     PG_END_TRY();
    4822             : 
    4823         264 :     xtCxt->doc = doc;
    4824         264 :     xtCxt->xpathcxt = xpathcxt;
    4825             : #else
    4826             :     NO_XML_SUPPORT();
    4827             : #endif                          /* not USE_LIBXML */
    4828         264 : }
    4829             : 
    4830             : /*
    4831             :  * XmlTableSetNamespace
    4832             :  *      Add a namespace declaration
    4833             :  */
    4834             : static void
    4835          18 : XmlTableSetNamespace(TableFuncScanState *state, const char *name, const char *uri)
    4836             : {
    4837             : #ifdef USE_LIBXML
    4838             :     XmlTableBuilderData *xtCxt;
    4839             : 
    4840          18 :     if (name == NULL)
    4841           6 :         ereport(ERROR,
    4842             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    4843             :                  errmsg("DEFAULT namespace is not supported")));
    4844          12 :     xtCxt = GetXmlTableBuilderPrivateData(state, "XmlTableSetNamespace");
    4845             : 
    4846          12 :     if (xmlXPathRegisterNs(xtCxt->xpathcxt,
    4847          12 :                            pg_xmlCharStrndup(name, strlen(name)),
    4848          12 :                            pg_xmlCharStrndup(uri, strlen(uri))))
    4849           0 :         xml_ereport(xtCxt->xmlerrcxt, ERROR, ERRCODE_INVALID_ARGUMENT_FOR_XQUERY,
    4850             :                     "could not set XML namespace");
    4851             : #else
    4852             :     NO_XML_SUPPORT();
    4853             : #endif                          /* not USE_LIBXML */
    4854          12 : }
    4855             : 
    4856             : /*
    4857             :  * XmlTableSetRowFilter
    4858             :  *      Install the row-filter Xpath expression.
    4859             :  */
    4860             : static void
    4861         258 : XmlTableSetRowFilter(TableFuncScanState *state, const char *path)
    4862             : {
    4863             : #ifdef USE_LIBXML
    4864             :     XmlTableBuilderData *xtCxt;
    4865             :     xmlChar    *xstr;
    4866             : 
    4867         258 :     xtCxt = GetXmlTableBuilderPrivateData(state, "XmlTableSetRowFilter");
    4868             : 
    4869         258 :     if (*path == '\0')
    4870           0 :         ereport(ERROR,
    4871             :                 (errcode(ERRCODE_INVALID_ARGUMENT_FOR_XQUERY),
    4872             :                  errmsg("row path filter must not be empty string")));
    4873             : 
    4874         258 :     xstr = pg_xmlCharStrndup(path, strlen(path));
    4875             : 
    4876             :     /* We require XmlTableSetDocument to have been done already */
    4877             :     Assert(xtCxt->xpathcxt != NULL);
    4878             : 
    4879         258 :     xtCxt->xpathcomp = xmlXPathCtxtCompile(xtCxt->xpathcxt, xstr);
    4880         258 :     if (xtCxt->xpathcomp == NULL || xtCxt->xmlerrcxt->err_occurred)
    4881           0 :         xml_ereport(xtCxt->xmlerrcxt, ERROR, ERRCODE_INVALID_ARGUMENT_FOR_XQUERY,
    4882             :                     "invalid XPath expression");
    4883             : #else
    4884             :     NO_XML_SUPPORT();
    4885             : #endif                          /* not USE_LIBXML */
    4886         258 : }
    4887             : 
    4888             : /*
    4889             :  * XmlTableSetColumnFilter
    4890             :  *      Install the column-filter Xpath expression, for the given column.
    4891             :  */
    4892             : static void
    4893         774 : XmlTableSetColumnFilter(TableFuncScanState *state, const char *path, int colnum)
    4894             : {
    4895             : #ifdef USE_LIBXML
    4896             :     XmlTableBuilderData *xtCxt;
    4897             :     xmlChar    *xstr;
    4898             : 
    4899             :     Assert(PointerIsValid(path));
    4900             : 
    4901         774 :     xtCxt = GetXmlTableBuilderPrivateData(state, "XmlTableSetColumnFilter");
    4902             : 
    4903         774 :     if (*path == '\0')
    4904           0 :         ereport(ERROR,
    4905             :                 (errcode(ERRCODE_INVALID_ARGUMENT_FOR_XQUERY),
    4906             :                  errmsg("column path filter must not be empty string")));
    4907             : 
    4908         774 :     xstr = pg_xmlCharStrndup(path, strlen(path));
    4909             : 
    4910             :     /* We require XmlTableSetDocument to have been done already */
    4911             :     Assert(xtCxt->xpathcxt != NULL);
    4912             : 
    4913         774 :     xtCxt->xpathscomp[colnum] = xmlXPathCtxtCompile(xtCxt->xpathcxt, xstr);
    4914         774 :     if (xtCxt->xpathscomp[colnum] == NULL || xtCxt->xmlerrcxt->err_occurred)
    4915           0 :         xml_ereport(xtCxt->xmlerrcxt, ERROR, ERRCODE_INVALID_ARGUMENT_FOR_XQUERY,
    4916             :                     "invalid XPath expression");
    4917             : #else
    4918             :     NO_XML_SUPPORT();
    4919             : #endif                          /* not USE_LIBXML */
    4920         774 : }
    4921             : 
    4922             : /*
    4923             :  * XmlTableFetchRow
    4924             :  *      Prepare the next "current" tuple for upcoming GetValue calls.
    4925             :  *      Returns false if the row-filter expression returned no more rows.
    4926             :  */
    4927             : static bool
    4928       23030 : XmlTableFetchRow(TableFuncScanState *state)
    4929             : {
    4930             : #ifdef USE_LIBXML
    4931             :     XmlTableBuilderData *xtCxt;
    4932             : 
    4933       23030 :     xtCxt = GetXmlTableBuilderPrivateData(state, "XmlTableFetchRow");
    4934             : 
    4935             :     /* Propagate our own error context to libxml2 */
    4936       23030 :     xmlSetStructuredErrorFunc(xtCxt->xmlerrcxt, xml_errorHandler);
    4937             : 
    4938       23030 :     if (xtCxt->xpathobj == NULL)
    4939             :     {
    4940         258 :         xtCxt->xpathobj = xmlXPathCompiledEval(xtCxt->xpathcomp, xtCxt->xpathcxt);
    4941         258 :         if (xtCxt->xpathobj == NULL || xtCxt->xmlerrcxt->err_occurred)
    4942           0 :             xml_ereport(xtCxt->xmlerrcxt, ERROR, ERRCODE_INVALID_ARGUMENT_FOR_XQUERY,
    4943             :                         "could not create XPath object");
    4944             : 
    4945         258 :         xtCxt->row_count = 0;
    4946             :     }
    4947             : 
    4948       23030 :     if (xtCxt->xpathobj->type == XPATH_NODESET)
    4949             :     {
    4950       23030 :         if (xtCxt->xpathobj->nodesetval != NULL)
    4951             :         {
    4952       23030 :             if (xtCxt->row_count++ < xtCxt->xpathobj->nodesetval->nodeNr)
    4953       22784 :                 return true;
    4954             :         }
    4955             :     }
    4956             : 
    4957         246 :     return false;
    4958             : #else
    4959             :     NO_XML_SUPPORT();
    4960             :     return false;
    4961             : #endif                          /* not USE_LIBXML */
    4962             : }
    4963             : 
    4964             : /*
    4965             :  * XmlTableGetValue
    4966             :  *      Return the value for column number 'colnum' for the current row.  If
    4967             :  *      column -1 is requested, return representation of the whole row.
    4968             :  *
    4969             :  * This leaks memory, so be sure to reset often the context in which it's
    4970             :  * called.
    4971             :  */
    4972             : static Datum
    4973      135654 : XmlTableGetValue(TableFuncScanState *state, int colnum,
    4974             :                  Oid typid, int32 typmod, bool *isnull)
    4975             : {
    4976             : #ifdef USE_LIBXML
    4977      135654 :     Datum       result = (Datum) 0;
    4978             :     XmlTableBuilderData *xtCxt;
    4979      135654 :     volatile xmlXPathObjectPtr xpathobj = NULL;
    4980             : 
    4981      135654 :     xtCxt = GetXmlTableBuilderPrivateData(state, "XmlTableGetValue");
    4982             : 
    4983             :     Assert(xtCxt->xpathobj &&
    4984             :            xtCxt->xpathobj->type == XPATH_NODESET &&
    4985             :            xtCxt->xpathobj->nodesetval != NULL);
    4986             : 
    4987             :     /* Propagate our own error context to libxml2 */
    4988      135654 :     xmlSetStructuredErrorFunc(xtCxt->xmlerrcxt, xml_errorHandler);
    4989             : 
    4990      135654 :     *isnull = false;
    4991             : 
    4992             :     Assert(xtCxt->xpathscomp[colnum] != NULL);
    4993             : 
    4994      135654 :     PG_TRY();
    4995             :     {
    4996             :         xmlNodePtr  cur;
    4997      135654 :         char       *cstr = NULL;
    4998             : 
    4999             :         /* Set current node as entry point for XPath evaluation */
    5000      135654 :         cur = xtCxt->xpathobj->nodesetval->nodeTab[xtCxt->row_count - 1];
    5001      135654 :         xtCxt->xpathcxt->node = cur;
    5002             : 
    5003             :         /* Evaluate column path */
    5004      135654 :         xpathobj = xmlXPathCompiledEval(xtCxt->xpathscomp[colnum], xtCxt->xpathcxt);
    5005      135654 :         if (xpathobj == NULL || xtCxt->xmlerrcxt->err_occurred)
    5006           0 :             xml_ereport(xtCxt->xmlerrcxt, ERROR, ERRCODE_INVALID_ARGUMENT_FOR_XQUERY,
    5007             :                         "could not create XPath object");
    5008             : 
    5009             :         /*
    5010             :          * There are four possible cases, depending on the number of nodes
    5011             :          * returned by the XPath expression and the type of the target column:
    5012             :          * a) XPath returns no nodes.  b) The target type is XML (return all
    5013             :          * as XML).  For non-XML return types:  c) One node (return content).
    5014             :          * d) Multiple nodes (error).
    5015             :          */
    5016      135654 :         if (xpathobj->type == XPATH_NODESET)
    5017             :         {
    5018      135624 :             int         count = 0;
    5019             : 
    5020      135624 :             if (xpathobj->nodesetval != NULL)
    5021      135414 :                 count = xpathobj->nodesetval->nodeNr;
    5022             : 
    5023      135624 :             if (xpathobj->nodesetval == NULL || count == 0)
    5024             :             {
    5025       22730 :                 *isnull = true;
    5026             :             }
    5027             :             else
    5028             :             {
    5029      112894 :                 if (typid == XMLOID)
    5030             :                 {
    5031             :                     text       *textstr;
    5032             :                     StringInfoData str;
    5033             : 
    5034             :                     /* Concatenate serialized values */
    5035          72 :                     initStringInfo(&str);
    5036         174 :                     for (int i = 0; i < count; i++)
    5037             :                     {
    5038             :                         textstr =
    5039         102 :                             xml_xmlnodetoxmltype(xpathobj->nodesetval->nodeTab[i],
    5040             :                                                  xtCxt->xmlerrcxt);
    5041             : 
    5042         102 :                         appendStringInfoText(&str, textstr);
    5043             :                     }
    5044          72 :                     cstr = str.data;
    5045             :                 }
    5046             :                 else
    5047             :                 {
    5048             :                     xmlChar    *str;
    5049             : 
    5050      112822 :                     if (count > 1)
    5051           6 :                         ereport(ERROR,
    5052             :                                 (errcode(ERRCODE_CARDINALITY_VIOLATION),
    5053             :                                  errmsg("more than one value returned by column XPath expression")));
    5054             : 
    5055      112816 :                     str = xmlXPathCastNodeSetToString(xpathobj->nodesetval);
    5056      112816 :                     cstr = str ? xml_pstrdup_and_free(str) : "";
    5057             :                 }
    5058             :             }
    5059             :         }
    5060          30 :         else if (xpathobj->type == XPATH_STRING)
    5061             :         {
    5062             :             /* Content should be escaped when target will be XML */
    5063          18 :             if (typid == XMLOID)
    5064           6 :                 cstr = escape_xml((char *) xpathobj->stringval);
    5065             :             else
    5066          12 :                 cstr = (char *) xpathobj->stringval;
    5067             :         }
    5068          12 :         else if (xpathobj->type == XPATH_BOOLEAN)
    5069             :         {
    5070             :             char        typcategory;
    5071             :             bool        typispreferred;
    5072             :             xmlChar    *str;
    5073             : 
    5074             :             /* Allow implicit casting from boolean to numbers */
    5075           6 :             get_type_category_preferred(typid, &typcategory, &typispreferred);
    5076             : 
    5077           6 :             if (typcategory != TYPCATEGORY_NUMERIC)
    5078           6 :                 str = xmlXPathCastBooleanToString(xpathobj->boolval);
    5079             :             else
    5080           0 :                 str = xmlXPathCastNumberToString(xmlXPathCastBooleanToNumber(xpathobj->boolval));
    5081             : 
    5082           6 :             cstr = xml_pstrdup_and_free(str);
    5083             :         }
    5084           6 :         else if (xpathobj->type == XPATH_NUMBER)
    5085             :         {
    5086             :             xmlChar    *str;
    5087             : 
    5088           6 :             str = xmlXPathCastNumberToString(xpathobj->floatval);
    5089           6 :             cstr = xml_pstrdup_and_free(str);
    5090             :         }
    5091             :         else
    5092           0 :             elog(ERROR, "unexpected XPath object type %u", xpathobj->type);
    5093             : 
    5094             :         /*
    5095             :          * By here, either cstr contains the result value, or the isnull flag
    5096             :          * has been set.
    5097             :          */
    5098             :         Assert(cstr || *isnull);
    5099             : 
    5100      135648 :         if (!*isnull)
    5101      112918 :             result = InputFunctionCall(&state->in_functions[colnum],
    5102             :                                        cstr,
    5103      112918 :                                        state->typioparams[colnum],
    5104             :                                        typmod);
    5105             :     }
    5106           6 :     PG_FINALLY();
    5107             :     {
    5108      135654 :         if (xpathobj != NULL)
    5109      135654 :             xmlXPathFreeObject(xpathobj);
    5110             :     }
    5111      135654 :     PG_END_TRY();
    5112             : 
    5113      135648 :     return result;
    5114             : #else
    5115             :     NO_XML_SUPPORT();
    5116             :     return 0;
    5117             : #endif                          /* not USE_LIBXML */
    5118             : }
    5119             : 
    5120             : /*
    5121             :  * XmlTableDestroyOpaque
    5122             :  *      Release all libxml2 resources
    5123             :  */
    5124             : static void
    5125         264 : XmlTableDestroyOpaque(TableFuncScanState *state)
    5126             : {
    5127             : #ifdef USE_LIBXML
    5128             :     XmlTableBuilderData *xtCxt;
    5129             : 
    5130         264 :     xtCxt = GetXmlTableBuilderPrivateData(state, "XmlTableDestroyOpaque");
    5131             : 
    5132             :     /* Propagate our own error context to libxml2 */
    5133         264 :     xmlSetStructuredErrorFunc(xtCxt->xmlerrcxt, xml_errorHandler);
    5134             : 
    5135         264 :     if (xtCxt->xpathscomp != NULL)
    5136             :     {
    5137             :         int         i;
    5138             : 
    5139        1116 :         for (i = 0; i < xtCxt->natts; i++)
    5140         852 :             if (xtCxt->xpathscomp[i] != NULL)
    5141         774 :                 xmlXPathFreeCompExpr(xtCxt->xpathscomp[i]);
    5142             :     }
    5143             : 
    5144         264 :     if (xtCxt->xpathobj != NULL)
    5145         258 :         xmlXPathFreeObject(xtCxt->xpathobj);
    5146         264 :     if (xtCxt->xpathcomp != NULL)
    5147         258 :         xmlXPathFreeCompExpr(xtCxt->xpathcomp);
    5148         264 :     if (xtCxt->xpathcxt != NULL)
    5149         264 :         xmlXPathFreeContext(xtCxt->xpathcxt);
    5150         264 :     if (xtCxt->doc != NULL)
    5151         264 :         xmlFreeDoc(xtCxt->doc);
    5152         264 :     if (xtCxt->ctxt != NULL)
    5153         264 :         xmlFreeParserCtxt(xtCxt->ctxt);
    5154             : 
    5155         264 :     pg_xml_done(xtCxt->xmlerrcxt, true);
    5156             : 
    5157             :     /* not valid anymore */
    5158         264 :     xtCxt->magic = 0;
    5159         264 :     state->opaque = NULL;
    5160             : 
    5161             : #else
    5162             :     NO_XML_SUPPORT();
    5163             : #endif                          /* not USE_LIBXML */
    5164         264 : }

Generated by: LCOV version 1.16