LCOV - code coverage report
Current view: top level - contrib/xml2 - xslt_proc.c (source / functions) Coverage Total Hit
Test: PostgreSQL 19devel Lines: 100.0 % 3 3
Test Date: 2026-02-28 18:14:58 Functions: 100.0 % 2 2
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /*
       2              :  * contrib/xml2/xslt_proc.c
       3              :  *
       4              :  * XSLT processing functions (requiring libxslt)
       5              :  *
       6              :  * John Gray, for Torchbox 2003-04-01
       7              :  */
       8              : #include "postgres.h"
       9              : 
      10              : #include "fmgr.h"
      11              : #include "utils/builtins.h"
      12              : #include "utils/xml.h"
      13              : #include "varatt.h"
      14              : 
      15              : #ifdef USE_LIBXSLT
      16              : 
      17              : /* libxml includes */
      18              : 
      19              : #include <libxml/xpath.h>
      20              : #include <libxml/tree.h>
      21              : #include <libxml/xmlmemory.h>
      22              : 
      23              : /* libxslt includes */
      24              : 
      25              : #include <libxslt/xslt.h>
      26              : #include <libxslt/xsltInternals.h>
      27              : #include <libxslt/security.h>
      28              : #include <libxslt/transform.h>
      29              : #include <libxslt/xsltutils.h>
      30              : #endif                          /* USE_LIBXSLT */
      31              : 
      32              : 
      33              : #ifdef USE_LIBXSLT
      34              : 
      35              : /* declarations to come from xpath.c */
      36              : extern PgXmlErrorContext *pgxml_parser_init(PgXmlStrictness strictness);
      37              : 
      38              : /* local defs */
      39              : static const char **parse_params(text *paramstr);
      40              : #endif                          /* USE_LIBXSLT */
      41              : 
      42              : 
      43            4 : PG_FUNCTION_INFO_V1(xslt_process);
      44              : 
      45              : Datum
      46            5 : xslt_process(PG_FUNCTION_ARGS)
      47              : {
      48              : #ifdef USE_LIBXSLT
      49              : 
      50              :     text       *doct = PG_GETARG_TEXT_PP(0);
      51              :     text       *ssheet = PG_GETARG_TEXT_PP(1);
      52              :     text       *volatile result = NULL;
      53              :     text       *paramstr;
      54              :     const char **params;
      55              :     PgXmlErrorContext *xmlerrcxt;
      56              :     volatile xsltStylesheetPtr stylesheet = NULL;
      57              :     volatile xmlDocPtr doctree = NULL;
      58              :     volatile xmlDocPtr restree = NULL;
      59              :     volatile xsltSecurityPrefsPtr xslt_sec_prefs = NULL;
      60              :     volatile xsltTransformContextPtr xslt_ctxt = NULL;
      61              :     volatile int resstat = -1;
      62              :     xmlChar    *volatile resstr = NULL;
      63              : 
      64              :     if (fcinfo->nargs == 3)
      65              :     {
      66              :         paramstr = PG_GETARG_TEXT_PP(2);
      67              :         params = parse_params(paramstr);
      68              :     }
      69              :     else
      70              :     {
      71              :         /* No parameters */
      72              :         params = palloc_object(const char *);
      73              :         params[0] = NULL;
      74              :     }
      75              : 
      76              :     /* Setup parser */
      77              :     xmlerrcxt = pgxml_parser_init(PG_XML_STRICTNESS_LEGACY);
      78              : 
      79              :     PG_TRY();
      80              :     {
      81              :         xmlDocPtr   ssdoc;
      82              :         bool        xslt_sec_prefs_error;
      83              :         int         reslen = 0;
      84              : 
      85              :         /* Parse document */
      86              :         doctree = xmlReadMemory((char *) VARDATA_ANY(doct),
      87              :                                 VARSIZE_ANY_EXHDR(doct), NULL, NULL,
      88              :                                 XML_PARSE_NOENT);
      89              : 
      90              :         if (doctree == NULL || pg_xml_error_occurred(xmlerrcxt))
      91              :             xml_ereport(xmlerrcxt, ERROR, ERRCODE_INVALID_XML_DOCUMENT,
      92              :                         "error parsing XML document");
      93              : 
      94              :         /* Same for stylesheet */
      95              :         ssdoc = xmlReadMemory((char *) VARDATA_ANY(ssheet),
      96              :                               VARSIZE_ANY_EXHDR(ssheet), NULL, NULL,
      97              :                               XML_PARSE_NOENT);
      98              : 
      99              :         if (ssdoc == NULL || pg_xml_error_occurred(xmlerrcxt))
     100              :             xml_ereport(xmlerrcxt, ERROR, ERRCODE_INVALID_XML_DOCUMENT,
     101              :                         "error parsing stylesheet as XML document");
     102              : 
     103              :         /* After this call we need not free ssdoc separately */
     104              :         stylesheet = xsltParseStylesheetDoc(ssdoc);
     105              : 
     106              :         if (stylesheet == NULL || pg_xml_error_occurred(xmlerrcxt))
     107              :             xml_ereport(xmlerrcxt, ERROR, ERRCODE_INVALID_ARGUMENT_FOR_XQUERY,
     108              :                         "failed to parse stylesheet");
     109              : 
     110              :         xslt_ctxt = xsltNewTransformContext(stylesheet, doctree);
     111              : 
     112              :         xslt_sec_prefs_error = false;
     113              :         if ((xslt_sec_prefs = xsltNewSecurityPrefs()) == NULL)
     114              :             xslt_sec_prefs_error = true;
     115              : 
     116              :         if (xsltSetSecurityPrefs(xslt_sec_prefs, XSLT_SECPREF_READ_FILE,
     117              :                                  xsltSecurityForbid) != 0)
     118              :             xslt_sec_prefs_error = true;
     119              :         if (xsltSetSecurityPrefs(xslt_sec_prefs, XSLT_SECPREF_WRITE_FILE,
     120              :                                  xsltSecurityForbid) != 0)
     121              :             xslt_sec_prefs_error = true;
     122              :         if (xsltSetSecurityPrefs(xslt_sec_prefs, XSLT_SECPREF_CREATE_DIRECTORY,
     123              :                                  xsltSecurityForbid) != 0)
     124              :             xslt_sec_prefs_error = true;
     125              :         if (xsltSetSecurityPrefs(xslt_sec_prefs, XSLT_SECPREF_READ_NETWORK,
     126              :                                  xsltSecurityForbid) != 0)
     127              :             xslt_sec_prefs_error = true;
     128              :         if (xsltSetSecurityPrefs(xslt_sec_prefs, XSLT_SECPREF_WRITE_NETWORK,
     129              :                                  xsltSecurityForbid) != 0)
     130              :             xslt_sec_prefs_error = true;
     131              :         if (xsltSetCtxtSecurityPrefs(xslt_sec_prefs, xslt_ctxt) != 0)
     132              :             xslt_sec_prefs_error = true;
     133              : 
     134              :         if (xslt_sec_prefs_error)
     135              :             ereport(ERROR,
     136              :                     (errmsg("could not set libxslt security preferences")));
     137              : 
     138              :         restree = xsltApplyStylesheetUser(stylesheet, doctree, params,
     139              :                                           NULL, NULL, xslt_ctxt);
     140              : 
     141              :         if (restree == NULL || pg_xml_error_occurred(xmlerrcxt))
     142              :             xml_ereport(xmlerrcxt, ERROR, ERRCODE_INVALID_ARGUMENT_FOR_XQUERY,
     143              :                         "failed to apply stylesheet");
     144              : 
     145              :         resstat = xsltSaveResultToString((xmlChar **) &resstr, &reslen,
     146              :                                          restree, stylesheet);
     147              : 
     148              :         if (resstat >= 0)
     149              :             result = cstring_to_text_with_len((char *) resstr, reslen);
     150              :     }
     151              :     PG_CATCH();
     152              :     {
     153              :         if (restree != NULL)
     154              :             xmlFreeDoc(restree);
     155              :         if (xslt_ctxt != NULL)
     156              :             xsltFreeTransformContext(xslt_ctxt);
     157              :         if (xslt_sec_prefs != NULL)
     158              :             xsltFreeSecurityPrefs(xslt_sec_prefs);
     159              :         if (stylesheet != NULL)
     160              :             xsltFreeStylesheet(stylesheet);
     161              :         if (doctree != NULL)
     162              :             xmlFreeDoc(doctree);
     163              :         if (resstr != NULL)
     164              :             xmlFree(resstr);
     165              :         xsltCleanupGlobals();
     166              : 
     167              :         pg_xml_done(xmlerrcxt, true);
     168              : 
     169              :         PG_RE_THROW();
     170              :     }
     171              :     PG_END_TRY();
     172              : 
     173              :     xmlFreeDoc(restree);
     174              :     xsltFreeTransformContext(xslt_ctxt);
     175              :     xsltFreeSecurityPrefs(xslt_sec_prefs);
     176              :     xsltFreeStylesheet(stylesheet);
     177              :     xmlFreeDoc(doctree);
     178              :     xsltCleanupGlobals();
     179              : 
     180              :     if (resstr)
     181              :         xmlFree(resstr);
     182              : 
     183              :     pg_xml_done(xmlerrcxt, false);
     184              : 
     185              :     /* XXX this is pretty dubious, really ought to throw error instead */
     186              :     if (resstat < 0)
     187              :         PG_RETURN_NULL();
     188              : 
     189              :     PG_RETURN_TEXT_P(result);
     190              : #else                           /* !USE_LIBXSLT */
     191              : 
     192            5 :     ereport(ERROR,
     193              :             (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     194              :              errmsg("xslt_process() is not available without libxslt")));
     195              :     PG_RETURN_NULL();
     196              : #endif                          /* USE_LIBXSLT */
     197              : }
     198              : 
     199              : #ifdef USE_LIBXSLT
     200              : 
     201              : static const char **
     202              : parse_params(text *paramstr)
     203              : {
     204              :     char       *pos;
     205              :     char       *pstr;
     206              :     char       *nvsep = "=";
     207              :     char       *itsep = ",";
     208              :     const char **params;
     209              :     int         max_params;
     210              :     int         nparams;
     211              : 
     212              :     pstr = text_to_cstring(paramstr);
     213              : 
     214              :     max_params = 20;            /* must be even! */
     215              :     params = (const char **) palloc((max_params + 1) * sizeof(char *));
     216              :     nparams = 0;
     217              : 
     218              :     pos = pstr;
     219              : 
     220              :     while (*pos != '\0')
     221              :     {
     222              :         if (nparams >= max_params)
     223              :         {
     224              :             max_params *= 2;
     225              :             params = (const char **) repalloc(params,
     226              :                                               (max_params + 1) * sizeof(char *));
     227              :         }
     228              :         params[nparams++] = pos;
     229              :         pos = strstr(pos, nvsep);
     230              :         if (pos != NULL)
     231              :         {
     232              :             *pos = '\0';
     233              :             pos++;
     234              :         }
     235              :         else
     236              :         {
     237              :             /* No equal sign, so ignore this "parameter" */
     238              :             nparams--;
     239              :             break;
     240              :         }
     241              : 
     242              :         /* since max_params is even, we still have nparams < max_params */
     243              :         params[nparams++] = pos;
     244              :         pos = strstr(pos, itsep);
     245              :         if (pos != NULL)
     246              :         {
     247              :             *pos = '\0';
     248              :             pos++;
     249              :         }
     250              :         else
     251              :             break;
     252              :     }
     253              : 
     254              :     /* Add the terminator marker; we left room for it in the palloc's */
     255              :     params[nparams] = NULL;
     256              : 
     257              :     return params;
     258              : }
     259              : 
     260              : #endif                          /* USE_LIBXSLT */
        

Generated by: LCOV version 2.0-1