Coverage Report

Created: 2025-07-12 06:41

/src/libxslt/libexslt/functions.c
Line
Count
Source (jump to first uncovered line)
1
#define IN_LIBEXSLT
2
#include "libexslt/libexslt.h"
3
4
#include <string.h>
5
6
#include <libxml/tree.h>
7
#include <libxml/xpath.h>
8
#include <libxml/xpathInternals.h>
9
#include <libxml/hash.h>
10
#include <libxml/debugXML.h>
11
12
#include <libxslt/xsltutils.h>
13
#include <libxslt/variables.h>
14
#include <libxslt/xsltInternals.h>
15
#include <libxslt/extensions.h>
16
#include <libxslt/transform.h>
17
#include <libxslt/imports.h>
18
19
#include "exslt.h"
20
21
typedef struct _exsltFuncFunctionData exsltFuncFunctionData;
22
struct _exsltFuncFunctionData {
23
    int nargs;      /* number of arguments to the function */
24
    xmlNodePtr content;   /* the func:fuction template content */
25
};
26
27
typedef struct _exsltFuncData exsltFuncData;
28
struct _exsltFuncData {
29
    xmlHashTablePtr funcs;  /* pointer to the stylesheet module data */
30
    xmlXPathObjectPtr result; /* returned by func:result */
31
    xsltStackElemPtr ctxtVar;   /* context variable */
32
    int error;      /* did an error occur? */
33
};
34
35
typedef struct _exsltFuncResultPreComp exsltFuncResultPreComp;
36
struct _exsltFuncResultPreComp {
37
    xsltElemPreComp comp;
38
    xmlXPathCompExprPtr select;
39
    xmlNsPtr *nsList;
40
    int nsNr;
41
};
42
43
/* Used for callback function in exsltInitFunc */
44
typedef struct _exsltFuncImportRegData exsltFuncImportRegData;
45
struct _exsltFuncImportRegData {
46
    xsltTransformContextPtr ctxt;
47
    xmlHashTablePtr hash;
48
};
49
50
static void exsltFuncFunctionFunction (xmlXPathParserContextPtr ctxt,
51
               int nargs);
52
static exsltFuncFunctionData *exsltFuncNewFunctionData(void);
53
54
/*static const xmlChar *exsltResultDataID = (const xmlChar *) "EXSLT Result";*/
55
56
/**
57
 * exsltFuncRegisterFunc:
58
 * @func:  the #exsltFuncFunctionData for the function
59
 * @ctxt:  an XSLT transformation context
60
 * @URI:  the function namespace URI
61
 * @name: the function name
62
 *
63
 * Registers a function declared by a func:function element
64
 */
65
static void
66
exsltFuncRegisterFunc (void *payload, void *vctxt,
67
           const xmlChar *URI, const xmlChar *name,
68
1.13k
           ATTRIBUTE_UNUSED const xmlChar *ignored) {
69
1.13k
    exsltFuncFunctionData *data = (exsltFuncFunctionData *) payload;
70
1.13k
    xsltTransformContextPtr ctxt = (xsltTransformContextPtr) vctxt;
71
72
1.13k
    if ((data == NULL) || (ctxt == NULL) || (URI == NULL) || (name == NULL))
73
0
  return;
74
75
1.13k
    xsltGenericDebug(xsltGenericDebugContext,
76
1.13k
         "exsltFuncRegisterFunc: register {%s}%s\n",
77
1.13k
         URI, name);
78
1.13k
    xsltRegisterExtFunction(ctxt, name, URI,
79
1.13k
          exsltFuncFunctionFunction);
80
1.13k
}
81
82
/*
83
 * exsltFuncRegisterImportFunc
84
 * @data:    the exsltFuncFunctionData for the function
85
 * @ch:      structure containing context and hash table
86
 * @URI:     the function namespace URI
87
 * @name:    the function name
88
 *
89
 * Checks if imported function is already registered in top-level
90
 * stylesheet.  If not, copies function data and registers function
91
 */
92
static void
93
exsltFuncRegisterImportFunc (void *payload, void *vctxt,
94
           const xmlChar *URI, const xmlChar *name,
95
168
           ATTRIBUTE_UNUSED const xmlChar *ignored) {
96
168
    exsltFuncFunctionData *data = (exsltFuncFunctionData *) payload;
97
168
    exsltFuncImportRegData *ch = (exsltFuncImportRegData *) vctxt;
98
168
    exsltFuncFunctionData *func=NULL;
99
100
168
    if ((data == NULL) || (ch == NULL) || (URI == NULL) || (name == NULL))
101
0
            return;
102
103
168
    if (ch->ctxt == NULL || ch->hash == NULL)
104
22
  return;
105
106
    /* Check if already present */
107
146
    func = (exsltFuncFunctionData*)xmlHashLookup2(ch->hash, URI, name);
108
146
    if (func == NULL) {   /* Not yet present - copy it in */
109
79
  func = exsltFuncNewFunctionData();
110
79
        if (func == NULL)
111
40
            return;
112
39
  memcpy(func, data, sizeof(exsltFuncFunctionData));
113
39
  if (xmlHashAddEntry2(ch->hash, URI, name, func) < 0) {
114
3
      xsltGenericError(xsltGenericErrorContext,
115
3
        "Failed to register function {%s}%s\n",
116
3
        URI, name);
117
3
            xmlFree(func);
118
36
  } else {   /* Do the registration */
119
36
      xsltGenericDebug(xsltGenericDebugContext,
120
36
              "exsltFuncRegisterImportFunc: register {%s}%s\n",
121
36
        URI, name);
122
36
      xsltRegisterExtFunction(ch->ctxt, name, URI,
123
36
        exsltFuncFunctionFunction);
124
36
  }
125
39
    }
126
146
}
127
128
/**
129
 * exsltFuncInit:
130
 * @ctxt: an XSLT transformation context
131
 * @URI: the namespace URI for the extension
132
 *
133
 * Initializes the EXSLT - Functions module.
134
 * Called at transformation-time; merges all
135
 * functions declared in the import tree taking
136
 * import precedence into account, i.e. overriding
137
 * functions with lower import precedence.
138
 *
139
 * Returns the data for this transformation
140
 */
141
static void *
142
750
exsltFuncInit (xsltTransformContextPtr ctxt, const xmlChar *URI) {
143
750
    exsltFuncData *ret;
144
750
    xsltStylesheetPtr tmp;
145
750
    exsltFuncImportRegData ch;
146
750
    xmlHashTablePtr hash;
147
148
750
    ret = (exsltFuncData *) xmlMalloc (sizeof(exsltFuncData));
149
750
    if (ret == NULL) {
150
2
  xsltGenericError(xsltGenericErrorContext,
151
2
       "exsltFuncInit: not enough memory\n");
152
2
  return(NULL);
153
2
    }
154
748
    memset(ret, 0, sizeof(exsltFuncData));
155
156
748
    ret->result = NULL;
157
748
    ret->error = 0;
158
159
748
    ch.hash = (xmlHashTablePtr) xsltStyleGetExtData(ctxt->style, URI);
160
748
    ret->funcs = ch.hash;
161
748
    xmlHashScanFull(ch.hash, exsltFuncRegisterFunc, ctxt);
162
748
    tmp = ctxt->style;
163
748
    ch.ctxt = ctxt;
164
869
    while ((tmp=xsltNextImport(tmp))!=NULL) {
165
121
  hash = xsltGetExtInfo(tmp, URI);
166
121
  if (hash != NULL) {
167
114
      xmlHashScanFull(hash, exsltFuncRegisterImportFunc, &ch);
168
114
  }
169
121
    }
170
171
748
    return(ret);
172
750
}
173
174
/**
175
 * exsltFuncShutdown:
176
 * @ctxt: an XSLT transformation context
177
 * @URI: the namespace URI for the extension
178
 * @data: the module data to free up
179
 *
180
 * Shutdown the EXSLT - Functions module
181
 * Called at transformation-time.
182
 */
183
static void
184
exsltFuncShutdown (xsltTransformContextPtr ctxt ATTRIBUTE_UNUSED,
185
       const xmlChar *URI ATTRIBUTE_UNUSED,
186
750
       void *vdata) {
187
750
    exsltFuncData *data = (exsltFuncData *) vdata;
188
189
750
    if (data != NULL) {
190
748
        if (data->result != NULL)
191
9
            xmlXPathFreeObject(data->result);
192
748
        xmlFree(data);
193
748
    }
194
750
}
195
196
/**
197
 * exsltFuncStyleInit:
198
 * @style: an XSLT stylesheet
199
 * @URI: the namespace URI for the extension
200
 *
201
 * Allocates the stylesheet data for EXSLT - Function
202
 * Called at compile-time.
203
 *
204
 * Returns the allocated data
205
 */
206
static void *
207
exsltFuncStyleInit (xsltStylesheetPtr style ATTRIBUTE_UNUSED,
208
10.1k
        const xmlChar *URI ATTRIBUTE_UNUSED) {
209
10.1k
    return xmlHashCreate(1);
210
10.1k
}
211
212
static void
213
12.5k
exsltFuncFreeDataEntry(void *payload, const xmlChar *name ATTRIBUTE_UNUSED) {
214
12.5k
    xmlFree(payload);
215
12.5k
}
216
217
/**
218
 * exsltFuncStyleShutdown:
219
 * @style: an XSLT stylesheet
220
 * @URI: the namespace URI for the extension
221
 * @data: the stylesheet data to free up
222
 *
223
 * Shutdown the EXSLT - Function module
224
 * Called at compile-time.
225
 */
226
static void
227
exsltFuncStyleShutdown (xsltStylesheetPtr style ATTRIBUTE_UNUSED,
228
      const xmlChar *URI ATTRIBUTE_UNUSED,
229
10.1k
      void *vdata) {
230
10.1k
    xmlHashTablePtr data = (xmlHashTablePtr) vdata;
231
10.1k
    xmlHashFree(data, exsltFuncFreeDataEntry);
232
10.1k
}
233
234
/**
235
 * exsltFuncNewFunctionData:
236
 *
237
 * Allocates an #exslFuncFunctionData object
238
 *
239
 * Returns the new structure
240
 */
241
static exsltFuncFunctionData *
242
13.8k
exsltFuncNewFunctionData (void) {
243
13.8k
    exsltFuncFunctionData *ret;
244
245
13.8k
    ret = (exsltFuncFunctionData *) xmlMalloc (sizeof(exsltFuncFunctionData));
246
13.8k
    if (ret == NULL) {
247
63
  xsltGenericError(xsltGenericErrorContext,
248
63
       "exsltFuncNewFunctionData: not enough memory\n");
249
63
  return (NULL);
250
63
    }
251
13.7k
    memset(ret, 0, sizeof(exsltFuncFunctionData));
252
253
13.7k
    ret->nargs = 0;
254
13.7k
    ret->content = NULL;
255
256
13.7k
    return(ret);
257
13.8k
}
258
259
/**
260
 * exsltFreeFuncResultPreComp:
261
 * @comp:  the #exsltFuncResultPreComp to free up
262
 *
263
 * Deallocates an #exsltFuncResultPreComp
264
 */
265
static void
266
6.05k
exsltFreeFuncResultPreComp (xsltElemPreCompPtr ecomp) {
267
6.05k
    exsltFuncResultPreComp *comp = (exsltFuncResultPreComp *) ecomp;
268
269
6.05k
    if (comp == NULL)
270
0
  return;
271
272
6.05k
    if (comp->select != NULL)
273
3.24k
  xmlXPathFreeCompExpr (comp->select);
274
6.05k
    if (comp->nsList != NULL)
275
6.04k
        xmlFree(comp->nsList);
276
6.05k
    xmlFree(comp);
277
6.05k
}
278
279
/**
280
 * exsltFuncFunctionFunction:
281
 * @ctxt:  an XPath parser context
282
 * @nargs:  the number of arguments
283
 *
284
 * Evaluates the func:function element that defines the called function.
285
 */
286
static void
287
15.5k
exsltFuncFunctionFunction (xmlXPathParserContextPtr ctxt, int nargs) {
288
15.5k
    xmlXPathObjectPtr oldResult, ret;
289
15.5k
    exsltFuncData *data;
290
15.5k
    exsltFuncFunctionData *func;
291
15.5k
    xmlNodePtr paramNode, oldInsert, oldXPNode, fake;
292
15.5k
    int oldBase, newBase;
293
15.5k
    void *oldCtxtVar;
294
15.5k
    xsltStackElemPtr params = NULL, param;
295
15.5k
    xsltTransformContextPtr tctxt = xsltXPathGetTransformContext(ctxt);
296
15.5k
    int i;
297
15.5k
    xmlXPathObjectPtr *args = NULL;
298
299
    /*
300
     * retrieve func:function template
301
     */
302
15.5k
    data = (exsltFuncData *) xsltGetExtData (tctxt,
303
15.5k
               EXSLT_FUNCTIONS_NAMESPACE);
304
15.5k
    oldResult = data->result;
305
15.5k
    data->result = NULL;
306
307
15.5k
    func = (exsltFuncFunctionData*) xmlHashLookup2 (data->funcs,
308
15.5k
                ctxt->context->functionURI,
309
15.5k
                ctxt->context->function);
310
15.5k
    if (func == NULL) {
311
        /* Should never happen */
312
0
        xsltGenericError(xsltGenericErrorContext,
313
0
                         "{%s}%s: not found\n",
314
0
                         ctxt->context->functionURI, ctxt->context->function);
315
0
        ctxt->error = XPATH_UNKNOWN_FUNC_ERROR;
316
0
        return;
317
0
    }
318
319
    /*
320
     * params handling
321
     */
322
15.5k
    if (nargs > func->nargs) {
323
246
  xsltGenericError(xsltGenericErrorContext,
324
246
       "{%s}%s: called with too many arguments\n",
325
246
       ctxt->context->functionURI, ctxt->context->function);
326
246
  ctxt->error = XPATH_INVALID_ARITY;
327
246
  return;
328
246
    }
329
15.2k
    if (func->content != NULL) {
330
14.8k
  paramNode = func->content->prev;
331
14.8k
    }
332
425
    else
333
425
  paramNode = NULL;
334
15.2k
    if ((paramNode == NULL) && (func->nargs != 0)) {
335
339
  xsltGenericError(xsltGenericErrorContext,
336
339
       "exsltFuncFunctionFunction: nargs != 0 and "
337
339
       "param == NULL\n");
338
339
  return;
339
339
    }
340
341
    /*
342
    * When a function is called recursively during evaluation of its
343
    * arguments, the recursion check in xsltApplySequenceConstructor
344
    * isn't reached.
345
    */
346
14.9k
    if (tctxt->depth >= tctxt->maxTemplateDepth) {
347
19
        xsltTransformError(tctxt, NULL, NULL,
348
19
            "exsltFuncFunctionFunction: Potentially infinite recursion "
349
19
            "detected in function {%s}%s.\n",
350
19
            ctxt->context->functionURI, ctxt->context->function);
351
19
        tctxt->state = XSLT_STATE_STOPPED;
352
19
        return;
353
19
    }
354
14.9k
    tctxt->depth++;
355
356
    /* Evaluating templates can change the XPath context node. */
357
14.9k
    oldXPNode = tctxt->xpathCtxt->node;
358
359
14.9k
    fake = xmlNewDocNode(tctxt->output, NULL,
360
14.9k
       (const xmlChar *)"fake", NULL);
361
14.9k
    if (fake == NULL)
362
20
        goto error;
363
    /*
364
     * We have a problem with the evaluation of function parameters.
365
     * The original library code did not evaluate XPath expressions until
366
     * the last moment.  After version 1.1.17 of the libxslt, the logic
367
     * of other parts of the library was changed, and the evaluation of
368
     * XPath expressions within parameters now takes place as soon as the
369
     * parameter is parsed/evaluated (xsltParseStylesheetCallerParam).
370
     * This means that the parameters need to be evaluated in lexical
371
     * order (since a variable is "in scope" as soon as it is declared).
372
     * However, on entry to this routine, the values (from the caller) are
373
     * in reverse order (held on the XPath context variable stack).  To
374
     * accomplish what is required, I have added code to pop the XPath
375
     * objects off of the stack at the beginning and save them, then use
376
     * them (in the reverse order) as the params are evaluated.  This
377
     * requires an xmlMalloc/xmlFree for each param set by the caller,
378
     * which is not very nice.  There is probably a much better solution
379
     * (like change other code to delay the evaluation).
380
     */
381
    /*
382
     * In order to give the function params and variables a new 'scope'
383
     * we change varsBase in the context.
384
     */
385
14.8k
    newBase = tctxt->varsNr;
386
    /* If there are any parameters */
387
14.8k
    if (paramNode != NULL) {
388
10.1k
        if (nargs > 0) {
389
9.91k
            args = (xmlXPathObjectPtr *) xmlMalloc(sizeof(*args) * nargs);
390
9.91k
            if (args == NULL)
391
4
                goto error;
392
            /* Fetch the stored argument values from the caller */
393
21.3k
            for (i = nargs - 1; i >= 0; i--) {
394
11.4k
                args[i] = valuePop(ctxt);
395
11.4k
            }
396
9.90k
        }
397
398
  /*
399
   * Prepare to process params in reverse order.  First, go to
400
   * the beginning of the param chain.
401
   */
402
12.7k
  for (i = 1; i <= func->nargs; i++) {
403
12.7k
      if (paramNode->prev == NULL)
404
10.1k
          break;
405
2.63k
      paramNode = paramNode->prev;
406
2.63k
  }
407
  /*
408
   * i has total # params found, nargs is number which are present
409
   * as arguments from the caller
410
   * Calculate the number of un-set parameters
411
   */
412
22.8k
  for (i = 0; i < func->nargs; i++) {
413
12.7k
      param = xsltParseStylesheetCallerParam (tctxt, paramNode);
414
12.7k
            if (param == NULL) {
415
13
                xsltLocalVariablePop(tctxt, newBase, -2);
416
13
          xsltFreeStackElemList(params);
417
32
                for (; i < nargs; i++)
418
19
                    xmlXPathFreeObject(args[i]);
419
13
                goto error;
420
13
            }
421
12.7k
      if (i < nargs) { /* if parameter value set */
422
11.4k
    param->computed = 1;
423
11.4k
    if (param->value != NULL)
424
11.2k
        xmlXPathFreeObject(param->value);
425
11.4k
    param->value = args[i];
426
11.4k
      }
427
12.7k
      xsltLocalVariablePush(tctxt, param, -1);
428
12.7k
      param->next = params;
429
12.7k
      params = param;
430
12.7k
      paramNode = paramNode->next;
431
12.7k
  }
432
10.1k
    }
433
    /*
434
     * Actual processing. The context variable is cleared and restored
435
     * when func:result is evaluated.
436
     */
437
14.8k
    oldBase = tctxt->varsBase;
438
14.8k
    oldInsert = tctxt->insert;
439
14.8k
    oldCtxtVar = data->ctxtVar;
440
14.8k
    data->ctxtVar = tctxt->contextVariable;
441
14.8k
    tctxt->varsBase = newBase;
442
14.8k
    tctxt->insert = fake;
443
14.8k
    tctxt->contextVariable = NULL;
444
14.8k
    xsltApplyOneTemplate (tctxt, tctxt->node,
445
14.8k
        func->content, NULL, NULL);
446
14.8k
    xsltLocalVariablePop(tctxt, tctxt->varsBase, -2);
447
14.8k
    tctxt->insert = oldInsert;
448
14.8k
    tctxt->contextVariable = data->ctxtVar;
449
14.8k
    tctxt->varsBase = oldBase;  /* restore original scope */
450
14.8k
    data->ctxtVar = oldCtxtVar;
451
14.8k
    if (params != NULL)
452
10.1k
  xsltFreeStackElemList(params);
453
14.8k
    tctxt->xpathCtxt->node = oldXPNode;
454
455
14.8k
    if (data->error != 0)
456
574
        goto error;
457
458
14.2k
    if (data->result != NULL) {
459
5.16k
  ret = data->result;
460
        /*
461
        * IMPORTANT: This enables previously tree fragments marked as
462
        * being results of a function, to be garbage-collected after
463
        * the calling process exits.
464
        */
465
5.16k
        xsltFlagRVTs(tctxt, ret, XSLT_RVT_LOCAL);
466
5.16k
    } else
467
9.13k
  ret = xmlXPathNewCString("");
468
469
14.2k
    data->result = oldResult;
470
471
    /*
472
     * It is an error if the instantiation of the template results in
473
     * the generation of result nodes.
474
     */
475
14.2k
    if (fake->children != NULL) {
476
4.55k
  xsltGenericError(xsltGenericErrorContext,
477
4.55k
       "{%s}%s: cannot write to result tree while "
478
4.55k
       "executing a function\n",
479
4.55k
       ctxt->context->functionURI, ctxt->context->function);
480
4.55k
        xmlXPathFreeObject(ret);
481
4.55k
  goto error;
482
4.55k
    }
483
9.73k
    valuePush(ctxt, ret);
484
485
14.9k
error:
486
14.9k
    xmlFree(args);
487
14.9k
    xmlFreeNode(fake);
488
14.9k
    tctxt->depth--;
489
14.9k
}
490
491
492
static void
493
15.0k
exsltFuncFunctionComp (xsltStylesheetPtr style, xmlNodePtr inst) {
494
15.0k
    xmlChar *name, *prefix;
495
15.0k
    xmlNsPtr ns;
496
15.0k
    xmlHashTablePtr data;
497
15.0k
    exsltFuncFunctionData *func;
498
499
15.0k
    if ((style == NULL) || (inst == NULL) || (inst->type != XML_ELEMENT_NODE))
500
0
  return;
501
502
15.0k
    {
503
15.0k
  xmlChar *qname;
504
505
15.0k
  qname = xmlGetProp(inst, (const xmlChar *) "name");
506
15.0k
  name = xmlSplitQName2 (qname, &prefix);
507
15.0k
  xmlFree(qname);
508
15.0k
    }
509
15.0k
    if ((name == NULL) || (prefix == NULL)) {
510
771
  xsltGenericError(xsltGenericErrorContext,
511
771
       "func:function: not a QName\n");
512
771
  if (name != NULL)
513
0
      xmlFree(name);
514
771
  return;
515
771
    }
516
    /* namespace lookup */
517
14.3k
    ns = xmlSearchNs (inst->doc, inst, prefix);
518
14.3k
    if (ns == NULL) {
519
569
  xsltGenericError(xsltGenericErrorContext,
520
569
       "func:function: undeclared prefix %s\n",
521
569
       prefix);
522
569
  xmlFree(name);
523
569
  xmlFree(prefix);
524
569
  return;
525
569
    }
526
13.7k
    xmlFree(prefix);
527
528
13.7k
    xsltParseTemplateContent(style, inst);
529
530
    /*
531
     * Create function data
532
     */
533
13.7k
    func = exsltFuncNewFunctionData();
534
13.7k
    if (func == NULL) {
535
23
        xmlFree(name);
536
23
        return;
537
23
    }
538
13.7k
    func->content = inst->children;
539
15.4k
    while (IS_XSLT_ELEM(func->content) &&
540
15.4k
     IS_XSLT_NAME(func->content, "param")) {
541
1.73k
  func->content = func->content->next;
542
1.73k
  func->nargs++;
543
1.73k
    }
544
545
    /*
546
     * Register the function data such that it can be retrieved
547
     * by exslFuncFunctionFunction
548
     */
549
#ifdef XSLT_REFACTORED
550
    /*
551
    * Ensure that the hash table will be stored in the *current*
552
    * stylesheet level in order to correctly evaluate the
553
    * import precedence.
554
    */
555
    data = (xmlHashTablePtr)
556
  xsltStyleStylesheetLevelGetExtData(style,
557
      EXSLT_FUNCTIONS_NAMESPACE);
558
#else
559
13.7k
    data = (xmlHashTablePtr)
560
13.7k
  xsltStyleGetExtData (style, EXSLT_FUNCTIONS_NAMESPACE);
561
13.7k
#endif
562
13.7k
    if (data == NULL) {
563
2
  xsltGenericError(xsltGenericErrorContext,
564
2
       "exsltFuncFunctionComp: no stylesheet data\n");
565
2
  xmlFree(name);
566
2
        xmlFree(func);
567
2
  return;
568
2
    }
569
570
13.7k
    if (xmlHashAddEntry2 (data, ns->href, name, func) < 0) {
571
1.19k
  xsltTransformError(NULL, style, inst,
572
1.19k
      "Failed to register function {%s}%s\n",
573
1.19k
       ns->href, name);
574
1.19k
  style->errors++;
575
1.19k
        xmlFree(func);
576
12.5k
    } else {
577
12.5k
  xsltGenericDebug(xsltGenericDebugContext,
578
12.5k
       "exsltFuncFunctionComp: register {%s}%s\n",
579
12.5k
       ns->href, name);
580
12.5k
    }
581
13.7k
    xmlFree(name);
582
13.7k
}
583
584
static xsltElemPreCompPtr
585
exsltFuncResultComp (xsltStylesheetPtr style, xmlNodePtr inst,
586
11.1k
         xsltTransformFunction function) {
587
11.1k
    xmlNodePtr test;
588
11.1k
    xmlChar *sel;
589
11.1k
    exsltFuncResultPreComp *ret;
590
591
11.1k
    if ((style == NULL) || (inst == NULL) || (inst->type != XML_ELEMENT_NODE))
592
0
        return (NULL);
593
594
    /*
595
     * "Validity" checking
596
     */
597
    /* it is an error to have any following sibling elements aside
598
     * from the xsl:fallback element.
599
     */
600
18.3k
    for (test = inst->next; test != NULL; test = test->next) {
601
9.56k
  if (test->type != XML_ELEMENT_NODE)
602
6.39k
      continue;
603
3.17k
  if (IS_XSLT_ELEM(test) && IS_XSLT_NAME(test, "fallback"))
604
801
      continue;
605
2.37k
  xsltGenericError(xsltGenericErrorContext,
606
2.37k
       "exsltFuncResultElem: only xsl:fallback is "
607
2.37k
       "allowed to follow func:result\n");
608
2.37k
  style->errors++;
609
2.37k
  return (NULL);
610
3.17k
    }
611
    /* it is an error for a func:result element to not be a descendant
612
     * of func:function.
613
     * it is an error if a func:result occurs within a func:result
614
     * element.
615
     * it is an error if instanciating the content of a variable
616
     * binding element (i.e. xsl:variable, xsl:param) results in the
617
     * instanciation of a func:result element.
618
     */
619
13.5k
    for (test = inst->parent; test != NULL; test = test->parent) {
620
13.5k
  if (IS_XSLT_ELEM(test) &&
621
13.5k
      IS_XSLT_NAME(test, "stylesheet")) {
622
334
      xsltGenericError(xsltGenericErrorContext,
623
334
           "func:result element not a descendant "
624
334
           "of a func:function\n");
625
334
      style->errors++;
626
334
      return (NULL);
627
334
  }
628
13.1k
  if ((test->ns != NULL) &&
629
13.1k
      (xmlStrEqual(test->ns->href, EXSLT_FUNCTIONS_NAMESPACE))) {
630
8.09k
      if (xmlStrEqual(test->name, (const xmlChar *) "function")) {
631
6.09k
    break;
632
6.09k
      }
633
2.00k
      if (xmlStrEqual(test->name, (const xmlChar *) "result")) {
634
2.00k
    xsltGenericError(xsltGenericErrorContext,
635
2.00k
         "func:result element not allowed within"
636
2.00k
         " another func:result element\n");
637
2.00k
          style->errors++;
638
2.00k
    return (NULL);
639
2.00k
      }
640
2.00k
  }
641
5.07k
  if (IS_XSLT_ELEM(test) &&
642
5.07k
      (IS_XSLT_NAME(test, "variable") ||
643
3.72k
       IS_XSLT_NAME(test, "param"))) {
644
303
      xsltGenericError(xsltGenericErrorContext,
645
303
           "func:result element not allowed within"
646
303
           " a variable binding element\n");
647
303
            style->errors++;
648
303
      return (NULL);
649
303
  }
650
5.07k
    }
651
652
    /*
653
     * Precomputation
654
     */
655
6.09k
    ret = (exsltFuncResultPreComp *)
656
6.09k
  xmlMalloc (sizeof(exsltFuncResultPreComp));
657
6.09k
    if (ret == NULL) {
658
38
  xsltPrintErrorContext(NULL, NULL, NULL);
659
38
        xsltGenericError(xsltGenericErrorContext,
660
38
                         "exsltFuncResultComp : malloc failed\n");
661
38
        style->errors++;
662
38
        return (NULL);
663
38
    }
664
6.05k
    memset(ret, 0, sizeof(exsltFuncResultPreComp));
665
666
6.05k
    xsltInitElemPreComp ((xsltElemPreCompPtr) ret, style, inst, function,
667
6.05k
     exsltFreeFuncResultPreComp);
668
6.05k
    ret->select = NULL;
669
670
    /*
671
     * Precompute the select attribute
672
     */
673
6.05k
    sel = xmlGetNsProp(inst, (const xmlChar *) "select", NULL);
674
6.05k
    if (sel != NULL) {
675
3.77k
  ret->select = xsltXPathCompileFlags(style, sel, 0);
676
3.77k
  xmlFree(sel);
677
3.77k
    }
678
    /*
679
     * Precompute the namespace list
680
     */
681
6.05k
    ret->nsList = xmlGetNsList(inst->doc, inst);
682
6.05k
    if (ret->nsList != NULL) {
683
6.04k
        int i = 0;
684
37.8k
        while (ret->nsList[i] != NULL)
685
31.8k
      i++;
686
6.04k
  ret->nsNr = i;
687
6.04k
    }
688
6.05k
    return ((xsltElemPreCompPtr) ret);
689
6.09k
}
690
691
static void
692
exsltFuncResultElem (xsltTransformContextPtr ctxt,
693
               xmlNodePtr node ATTRIBUTE_UNUSED, xmlNodePtr inst,
694
32.7k
         xsltElemPreCompPtr ecomp) {
695
32.7k
    exsltFuncResultPreComp *comp = (exsltFuncResultPreComp *) ecomp;
696
32.7k
    exsltFuncData *data;
697
32.7k
    xmlXPathObjectPtr ret;
698
699
700
    /* It is an error if instantiating the content of the
701
     * func:function element results in the instantiation of more than
702
     * one func:result elements.
703
     */
704
32.7k
    data = (exsltFuncData *) xsltGetExtData (ctxt, EXSLT_FUNCTIONS_NAMESPACE);
705
32.7k
    if (data == NULL) {
706
0
  xsltGenericError(xsltGenericErrorContext,
707
0
       "exsltFuncReturnElem: data == NULL\n");
708
0
  return;
709
0
    }
710
32.7k
    if (data->result != NULL) {
711
19.9k
  xsltGenericError(xsltGenericErrorContext,
712
19.9k
       "func:result already instanciated\n");
713
19.9k
  data->error = 1;
714
19.9k
  return;
715
19.9k
    }
716
    /*
717
     * Restore context variable, so that it will receive the function
718
     * result RVTs.
719
     */
720
12.8k
    ctxt->contextVariable = data->ctxtVar;
721
    /*
722
     * Processing
723
     */
724
12.8k
    if (comp->select != NULL) {
725
11.1k
  xmlNsPtr *oldXPNsList;
726
11.1k
  int oldXPNsNr;
727
11.1k
  xmlNodePtr oldXPContextNode;
728
  /* If the func:result element has a select attribute, then the
729
   * value of the attribute must be an expression and the
730
   * returned value is the object that results from evaluating
731
   * the expression. In this case, the content must be empty.
732
   */
733
11.1k
  if (inst->children != NULL) {
734
0
      xsltGenericError(xsltGenericErrorContext,
735
0
           "func:result content must be empty if"
736
0
           " the function has a select attribute\n");
737
0
      data->error = 1;
738
0
      return;
739
0
  }
740
11.1k
  oldXPNsList = ctxt->xpathCtxt->namespaces;
741
11.1k
  oldXPNsNr = ctxt->xpathCtxt->nsNr;
742
11.1k
  oldXPContextNode = ctxt->xpathCtxt->node;
743
744
11.1k
  ctxt->xpathCtxt->namespaces = comp->nsList;
745
11.1k
  ctxt->xpathCtxt->nsNr = comp->nsNr;
746
11.1k
        ctxt->xpathCtxt->node = ctxt->node;
747
748
11.1k
  ret = xmlXPathCompiledEval(comp->select, ctxt->xpathCtxt);
749
750
11.1k
  ctxt->xpathCtxt->node = oldXPContextNode;
751
11.1k
  ctxt->xpathCtxt->nsNr = oldXPNsNr;
752
11.1k
  ctxt->xpathCtxt->namespaces = oldXPNsList;
753
754
11.1k
  if (ret == NULL) {
755
7.45k
      xsltGenericError(xsltGenericErrorContext,
756
7.45k
           "exsltFuncResultElem: ret == NULL\n");
757
7.45k
      return;
758
7.45k
  }
759
  /*
760
  * Mark it as a function result in order to avoid garbage
761
  * collecting of tree fragments before the function exits.
762
  */
763
3.72k
  xsltFlagRVTs(ctxt, ret, XSLT_RVT_FUNC_RESULT);
764
3.72k
    } else if (inst->children != NULL) {
765
  /* If the func:result element does not have a select attribute
766
   * and has non-empty content (i.e. the func:result element has
767
   * one or more child nodes), then the content of the
768
   * func:result element specifies the value.
769
   */
770
872
  xmlNodePtr oldInsert;
771
872
  xmlDocPtr container;
772
773
872
  container = xsltCreateRVT(ctxt);
774
872
  if (container == NULL) {
775
3
      xsltGenericError(xsltGenericErrorContext,
776
3
           "exsltFuncResultElem: out of memory\n");
777
3
      data->error = 1;
778
3
      return;
779
3
  }
780
        /* Mark as function result. */
781
869
        xsltRegisterLocalRVT(ctxt, container);
782
869
        container->compression = XSLT_RVT_FUNC_RESULT;
783
784
869
  oldInsert = ctxt->insert;
785
869
  ctxt->insert = (xmlNodePtr) container;
786
869
  xsltApplyOneTemplate (ctxt, ctxt->node,
787
869
            inst->children, NULL, NULL);
788
869
  ctxt->insert = oldInsert;
789
790
869
  ret = xmlXPathNewValueTree((xmlNodePtr) container);
791
869
  if (ret == NULL) {
792
178
      xsltGenericError(xsltGenericErrorContext,
793
178
           "exsltFuncResultElem: ret == NULL\n");
794
178
      data->error = 1;
795
691
  } else {
796
            /*
797
             * This stops older libxml2 versions from freeing the nodes
798
             * in the tree.
799
             */
800
691
      ret->boolval = 0;
801
691
  }
802
869
    } else {
803
  /* If the func:result element has empty content and does not
804
   * have a select attribute, then the returned value is an
805
   * empty string.
806
   */
807
750
  ret = xmlXPathNewCString("");
808
750
    }
809
5.34k
    data->result = ret;
810
5.34k
}
811
812
/**
813
 * exsltFuncRegister:
814
 *
815
 * Registers the EXSLT - Functions module
816
 */
817
void
818
2
exsltFuncRegister (void) {
819
2
    xsltRegisterExtModuleFull (EXSLT_FUNCTIONS_NAMESPACE,
820
2
           exsltFuncInit,
821
2
           exsltFuncShutdown,
822
2
           exsltFuncStyleInit,
823
2
           exsltFuncStyleShutdown);
824
825
2
    xsltRegisterExtModuleTopLevel ((const xmlChar *) "function",
826
2
           EXSLT_FUNCTIONS_NAMESPACE,
827
2
           exsltFuncFunctionComp);
828
2
    xsltRegisterExtModuleElement ((const xmlChar *) "result",
829
2
        EXSLT_FUNCTIONS_NAMESPACE,
830
2
        exsltFuncResultComp,
831
2
        exsltFuncResultElem);
832
2
}