LCOV - code coverage report
Current view: top level - contrib/xml2 - xslt_proc.c (source / functions) Coverage Total Hit
Test: Code coverage Lines: 0.0 % 136 0
Test Date: 2026-01-26 10:56:24 Functions: 0.0 % 3 0
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            0 : PG_FUNCTION_INFO_V1(xslt_process);
      44              : 
      45              : Datum
      46            0 : xslt_process(PG_FUNCTION_ARGS)
      47              : {
      48              : #ifdef USE_LIBXSLT
      49              : 
      50            0 :         text       *doct = PG_GETARG_TEXT_PP(0);
      51            0 :         text       *ssheet = PG_GETARG_TEXT_PP(1);
      52            0 :         text       *volatile result = NULL;
      53            0 :         text       *paramstr;
      54            0 :         const char **params;
      55            0 :         PgXmlErrorContext *xmlerrcxt;
      56            0 :         volatile xsltStylesheetPtr stylesheet = NULL;
      57            0 :         volatile xmlDocPtr doctree = NULL;
      58            0 :         volatile xmlDocPtr restree = NULL;
      59            0 :         volatile xsltSecurityPrefsPtr xslt_sec_prefs = NULL;
      60            0 :         volatile xsltTransformContextPtr xslt_ctxt = NULL;
      61            0 :         volatile int resstat = -1;
      62            0 :         xmlChar    *volatile resstr = NULL;
      63              : 
      64            0 :         if (fcinfo->nargs == 3)
      65              :         {
      66            0 :                 paramstr = PG_GETARG_TEXT_PP(2);
      67            0 :                 params = parse_params(paramstr);
      68            0 :         }
      69              :         else
      70              :         {
      71              :                 /* No parameters */
      72            0 :                 params = palloc_object(const char *);
      73            0 :                 params[0] = NULL;
      74              :         }
      75              : 
      76              :         /* Setup parser */
      77            0 :         xmlerrcxt = pgxml_parser_init(PG_XML_STRICTNESS_LEGACY);
      78              : 
      79            0 :         PG_TRY();
      80              :         {
      81            0 :                 xmlDocPtr       ssdoc;
      82            0 :                 bool            xslt_sec_prefs_error;
      83            0 :                 int                     reslen = 0;
      84              : 
      85              :                 /* Parse document */
      86            0 :                 doctree = xmlReadMemory((char *) VARDATA_ANY(doct),
      87            0 :                                                                 VARSIZE_ANY_EXHDR(doct), NULL, NULL,
      88              :                                                                 XML_PARSE_NOENT);
      89              : 
      90            0 :                 if (doctree == NULL || pg_xml_error_occurred(xmlerrcxt))
      91            0 :                         xml_ereport(xmlerrcxt, ERROR, ERRCODE_INVALID_XML_DOCUMENT,
      92              :                                                 "error parsing XML document");
      93              : 
      94              :                 /* Same for stylesheet */
      95            0 :                 ssdoc = xmlReadMemory((char *) VARDATA_ANY(ssheet),
      96            0 :                                                           VARSIZE_ANY_EXHDR(ssheet), NULL, NULL,
      97              :                                                           XML_PARSE_NOENT);
      98              : 
      99            0 :                 if (ssdoc == NULL || pg_xml_error_occurred(xmlerrcxt))
     100            0 :                         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            0 :                 stylesheet = xsltParseStylesheetDoc(ssdoc);
     105              : 
     106            0 :                 if (stylesheet == NULL || pg_xml_error_occurred(xmlerrcxt))
     107            0 :                         xml_ereport(xmlerrcxt, ERROR, ERRCODE_INVALID_ARGUMENT_FOR_XQUERY,
     108              :                                                 "failed to parse stylesheet");
     109              : 
     110            0 :                 xslt_ctxt = xsltNewTransformContext(stylesheet, doctree);
     111              : 
     112            0 :                 xslt_sec_prefs_error = false;
     113            0 :                 if ((xslt_sec_prefs = xsltNewSecurityPrefs()) == NULL)
     114            0 :                         xslt_sec_prefs_error = true;
     115              : 
     116            0 :                 if (xsltSetSecurityPrefs(xslt_sec_prefs, XSLT_SECPREF_READ_FILE,
     117            0 :                                                                  xsltSecurityForbid) != 0)
     118            0 :                         xslt_sec_prefs_error = true;
     119            0 :                 if (xsltSetSecurityPrefs(xslt_sec_prefs, XSLT_SECPREF_WRITE_FILE,
     120            0 :                                                                  xsltSecurityForbid) != 0)
     121            0 :                         xslt_sec_prefs_error = true;
     122            0 :                 if (xsltSetSecurityPrefs(xslt_sec_prefs, XSLT_SECPREF_CREATE_DIRECTORY,
     123            0 :                                                                  xsltSecurityForbid) != 0)
     124            0 :                         xslt_sec_prefs_error = true;
     125            0 :                 if (xsltSetSecurityPrefs(xslt_sec_prefs, XSLT_SECPREF_READ_NETWORK,
     126            0 :                                                                  xsltSecurityForbid) != 0)
     127            0 :                         xslt_sec_prefs_error = true;
     128            0 :                 if (xsltSetSecurityPrefs(xslt_sec_prefs, XSLT_SECPREF_WRITE_NETWORK,
     129            0 :                                                                  xsltSecurityForbid) != 0)
     130            0 :                         xslt_sec_prefs_error = true;
     131            0 :                 if (xsltSetCtxtSecurityPrefs(xslt_sec_prefs, xslt_ctxt) != 0)
     132            0 :                         xslt_sec_prefs_error = true;
     133              : 
     134            0 :                 if (xslt_sec_prefs_error)
     135            0 :                         ereport(ERROR,
     136              :                                         (errmsg("could not set libxslt security preferences")));
     137              : 
     138            0 :                 restree = xsltApplyStylesheetUser(stylesheet, doctree, params,
     139            0 :                                                                                   NULL, NULL, xslt_ctxt);
     140              : 
     141            0 :                 if (restree == NULL || pg_xml_error_occurred(xmlerrcxt))
     142            0 :                         xml_ereport(xmlerrcxt, ERROR, ERRCODE_INVALID_ARGUMENT_FOR_XQUERY,
     143              :                                                 "failed to apply stylesheet");
     144              : 
     145            0 :                 resstat = xsltSaveResultToString((xmlChar **) &resstr, &reslen,
     146            0 :                                                                                  restree, stylesheet);
     147              : 
     148            0 :                 if (resstat >= 0)
     149            0 :                         result = cstring_to_text_with_len((char *) resstr, reslen);
     150            0 :         }
     151            0 :         PG_CATCH();
     152              :         {
     153            0 :                 if (restree != NULL)
     154            0 :                         xmlFreeDoc(restree);
     155            0 :                 if (xslt_ctxt != NULL)
     156            0 :                         xsltFreeTransformContext(xslt_ctxt);
     157            0 :                 if (xslt_sec_prefs != NULL)
     158            0 :                         xsltFreeSecurityPrefs(xslt_sec_prefs);
     159            0 :                 if (stylesheet != NULL)
     160            0 :                         xsltFreeStylesheet(stylesheet);
     161            0 :                 if (doctree != NULL)
     162            0 :                         xmlFreeDoc(doctree);
     163            0 :                 if (resstr != NULL)
     164            0 :                         xmlFree(resstr);
     165            0 :                 xsltCleanupGlobals();
     166              : 
     167            0 :                 pg_xml_done(xmlerrcxt, true);
     168              : 
     169            0 :                 PG_RE_THROW();
     170              :         }
     171            0 :         PG_END_TRY();
     172              : 
     173            0 :         xmlFreeDoc(restree);
     174            0 :         xsltFreeTransformContext(xslt_ctxt);
     175            0 :         xsltFreeSecurityPrefs(xslt_sec_prefs);
     176            0 :         xsltFreeStylesheet(stylesheet);
     177            0 :         xmlFreeDoc(doctree);
     178            0 :         xsltCleanupGlobals();
     179              : 
     180            0 :         if (resstr)
     181            0 :                 xmlFree(resstr);
     182              : 
     183            0 :         pg_xml_done(xmlerrcxt, false);
     184              : 
     185              :         /* XXX this is pretty dubious, really ought to throw error instead */
     186            0 :         if (resstat < 0)
     187            0 :                 PG_RETURN_NULL();
     188              : 
     189            0 :         PG_RETURN_TEXT_P(result);
     190              : #else                                                   /* !USE_LIBXSLT */
     191              : 
     192              :         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            0 : }
     198              : 
     199              : #ifdef USE_LIBXSLT
     200              : 
     201              : static const char **
     202            0 : parse_params(text *paramstr)
     203              : {
     204            0 :         char       *pos;
     205            0 :         char       *pstr;
     206            0 :         char       *nvsep = "=";
     207            0 :         char       *itsep = ",";
     208            0 :         const char **params;
     209            0 :         int                     max_params;
     210            0 :         int                     nparams;
     211              : 
     212            0 :         pstr = text_to_cstring(paramstr);
     213              : 
     214            0 :         max_params = 20;                        /* must be even! */
     215            0 :         params = (const char **) palloc((max_params + 1) * sizeof(char *));
     216            0 :         nparams = 0;
     217              : 
     218            0 :         pos = pstr;
     219              : 
     220            0 :         while (*pos != '\0')
     221              :         {
     222            0 :                 if (nparams >= max_params)
     223              :                 {
     224            0 :                         max_params *= 2;
     225            0 :                         params = (const char **) repalloc(params,
     226            0 :                                                                                           (max_params + 1) * sizeof(char *));
     227            0 :                 }
     228            0 :                 params[nparams++] = pos;
     229            0 :                 pos = strstr(pos, nvsep);
     230            0 :                 if (pos != NULL)
     231              :                 {
     232            0 :                         *pos = '\0';
     233            0 :                         pos++;
     234            0 :                 }
     235              :                 else
     236              :                 {
     237              :                         /* No equal sign, so ignore this "parameter" */
     238            0 :                         nparams--;
     239            0 :                         break;
     240              :                 }
     241              : 
     242              :                 /* since max_params is even, we still have nparams < max_params */
     243            0 :                 params[nparams++] = pos;
     244            0 :                 pos = strstr(pos, itsep);
     245            0 :                 if (pos != NULL)
     246              :                 {
     247            0 :                         *pos = '\0';
     248            0 :                         pos++;
     249            0 :                 }
     250              :                 else
     251            0 :                         break;
     252              :         }
     253              : 
     254              :         /* Add the terminator marker; we left room for it in the palloc's */
     255            0 :         params[nparams] = NULL;
     256              : 
     257            0 :         return params;
     258            0 : }
     259              : 
     260              : #endif                                                  /* USE_LIBXSLT */
        

Generated by: LCOV version 2.3.2-1