Coverage Report

Created: 2026-10-17 06:43

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libxslt/libexslt/functions.c
Line
Count
Source
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
676
           ATTRIBUTE_UNUSED const xmlChar *ignored) {
69
676
    exsltFuncFunctionData *data = (exsltFuncFunctionData *) payload;
70
676
    xsltTransformContextPtr ctxt = (xsltTransformContextPtr) vctxt;
71
72
676
    if ((data == NULL) || (ctxt == NULL) || (URI == NULL) || (name == NULL))
73
0
  return;
74
75
676
    xsltGenericDebug(xsltGenericDebugContext,
76
676
         "exsltFuncRegisterFunc: register {%s}%s\n",
77
676
         URI, name);
78
676
    xsltRegisterExtFunction(ctxt, name, URI,
79
676
          exsltFuncFunctionFunction);
80
676
}
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
264
           ATTRIBUTE_UNUSED const xmlChar *ignored) {
96
264
    exsltFuncFunctionData *data = (exsltFuncFunctionData *) payload;
97
264
    exsltFuncImportRegData *ch = (exsltFuncImportRegData *) vctxt;
98
264
    exsltFuncFunctionData *func=NULL;
99
100
264
    if ((data == NULL) || (ch == NULL) || (URI == NULL) || (name == NULL))
101
0
            return;
102
103
264
    if (ch->ctxt == NULL || ch->hash == NULL)
104
40
  return;
105
106
    /* Check if already present */
107
224
    func = (exsltFuncFunctionData*)xmlHashLookup2(ch->hash, URI, name);
108
224
    if (func == NULL) {   /* Not yet present - copy it in */
109
134
  func = exsltFuncNewFunctionData();
110
134
        if (func == NULL)
111
34
            return;
112
100
  memcpy(func, data, sizeof(exsltFuncFunctionData));
113
100
  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
97
  } else {   /* Do the registration */
119
97
      xsltGenericDebug(xsltGenericDebugContext,
120
97
              "exsltFuncRegisterImportFunc: register {%s}%s\n",
121
97
        URI, name);
122
97
      xsltRegisterExtFunction(ch->ctxt, name, URI,
123
97
        exsltFuncFunctionFunction);
124
97
  }
125
100
    }
126
224
}
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
576
exsltFuncInit (xsltTransformContextPtr ctxt, const xmlChar *URI) {
143
576
    exsltFuncData *ret;
144
576
    xsltStylesheetPtr tmp;
145
576
    exsltFuncImportRegData ch;
146
576
    xmlHashTablePtr hash;
147
148
576
    ret = (exsltFuncData *) xmlMalloc (sizeof(exsltFuncData));
149
576
    if (ret == NULL) {
150
2
  xsltGenericError(xsltGenericErrorContext,
151
2
       "exsltFuncInit: not enough memory\n");
152
2
  return(NULL);
153
2
    }
154
574
    memset(ret, 0, sizeof(exsltFuncData));
155
156
574
    ret->result = NULL;
157
574
    ret->error = 0;
158
159
574
    ch.hash = (xmlHashTablePtr) xsltStyleGetExtData(ctxt->style, URI);
160
574
    ret->funcs = ch.hash;
161
574
    xmlHashScanFull(ch.hash, exsltFuncRegisterFunc, ctxt);
162
574
    tmp = ctxt->style;
163
574
    ch.ctxt = ctxt;
164
756
    while ((tmp=xsltNextImport(tmp))!=NULL) {
165
182
  hash = xsltGetExtInfo(tmp, URI);
166
182
  if (hash != NULL) {
167
179
      xmlHashScanFull(hash, exsltFuncRegisterImportFunc, &ch);
168
179
  }
169
182
    }
170
171
574
    return(ret);
172
576
}
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
576
       void *vdata) {
187
576
    exsltFuncData *data = (exsltFuncData *) vdata;
188
189
576
    if (data != NULL) {
190
574
        if (data->result != NULL)
191
1
            xmlXPathFreeObject(data->result);
192
574
        xmlFree(data);
193
574
    }
194
576
}
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
5.76k
        const xmlChar *URI ATTRIBUTE_UNUSED) {
209
5.76k
    return xmlHashCreate(1);
210
5.76k
}
211
212
static void
213
5.47k
exsltFuncFreeDataEntry(void *payload, const xmlChar *name ATTRIBUTE_UNUSED) {
214
5.47k
    xmlFree(payload);
215
5.47k
}
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
5.76k
      void *vdata) {
230
5.76k
    xmlHashTablePtr data = (xmlHashTablePtr) vdata;
231
5.76k
    xmlHashFree(data, exsltFuncFreeDataEntry);
232
5.76k
}
233
234
/**
235
 * exsltFuncNewFunctionData:
236
 *
237
 * Allocates an #exslFuncFunctionData object
238
 *
239
 * Returns the new structure
240
 */
241
static exsltFuncFunctionData *
242
6.06k
exsltFuncNewFunctionData (void) {
243
6.06k
    exsltFuncFunctionData *ret;
244
245
6.06k
    ret = (exsltFuncFunctionData *) xmlMalloc (sizeof(exsltFuncFunctionData));
246
6.06k
    if (ret == NULL) {
247
56
  xsltGenericError(xsltGenericErrorContext,
248
56
       "exsltFuncNewFunctionData: not enough memory\n");
249
56
  return (NULL);
250
56
    }
251
6.00k
    memset(ret, 0, sizeof(exsltFuncFunctionData));
252
253
6.00k
    ret->nargs = 0;
254
6.00k
    ret->content = NULL;
255
256
6.00k
    return(ret);
257
6.06k
}
258
259
/**
260
 * exsltFreeFuncResultPreComp:
261
 * @comp:  the #exsltFuncResultPreComp to free up
262
 *
263
 * Deallocates an #exsltFuncResultPreComp
264
 */
265
static void
266
3.61k
exsltFreeFuncResultPreComp (xsltElemPreCompPtr ecomp) {
267
3.61k
    exsltFuncResultPreComp *comp = (exsltFuncResultPreComp *) ecomp;
268
269
3.61k
    if (comp == NULL)
270
0
  return;
271
272
3.61k
    if (comp->select != NULL)
273
1.20k
  xmlXPathFreeCompExpr (comp->select);
274
3.61k
    if (comp->nsList != NULL)
275
3.61k
        xmlFree(comp->nsList);
276
3.61k
    xmlFree(comp);
277
3.61k
}
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
12.6k
exsltFuncFunctionFunction (xmlXPathParserContextPtr ctxt, int nargs) {
288
12.6k
    xmlXPathObjectPtr oldResult, ret;
289
12.6k
    exsltFuncData *data;
290
12.6k
    exsltFuncFunctionData *func;
291
12.6k
    xmlNodePtr paramNode, oldInsert, oldXPNode, fake;
292
12.6k
    int oldBase, newBase;
293
12.6k
    void *oldCtxtVar;
294
12.6k
    xsltStackElemPtr params = NULL, param;
295
12.6k
    xsltTransformContextPtr tctxt = xsltXPathGetTransformContext(ctxt);
296
12.6k
    int i;
297
12.6k
    xmlXPathObjectPtr *args = NULL;
298
299
    /*
300
     * retrieve func:function template
301
     */
302
12.6k
    data = (exsltFuncData *) xsltGetExtData (tctxt,
303
12.6k
               EXSLT_FUNCTIONS_NAMESPACE);
304
12.6k
    oldResult = data->result;
305
12.6k
    data->result = NULL;
306
307
12.6k
    func = (exsltFuncFunctionData*) xmlHashLookup2 (data->funcs,
308
12.6k
                ctxt->context->functionURI,
309
12.6k
                ctxt->context->function);
310
12.6k
    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
12.6k
    if (nargs > func->nargs) {
323
472
  xsltGenericError(xsltGenericErrorContext,
324
472
       "{%s}%s: called with too many arguments\n",
325
472
       ctxt->context->functionURI, ctxt->context->function);
326
472
  ctxt->error = XPATH_INVALID_ARITY;
327
472
  return;
328
472
    }
329
12.1k
    if (func->content != NULL) {
330
11.8k
  paramNode = func->content->prev;
331
11.8k
    }
332
319
    else
333
319
  paramNode = NULL;
334
12.1k
    if ((paramNode == NULL) && (func->nargs != 0)) {
335
217
  xsltGenericError(xsltGenericErrorContext,
336
217
       "exsltFuncFunctionFunction: nargs != 0 and "
337
217
       "param == NULL\n");
338
217
  return;
339
217
    }
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
11.9k
    if (tctxt->depth >= tctxt->maxTemplateDepth) {
347
215
        xsltTransformError(tctxt, NULL, NULL,
348
215
            "exsltFuncFunctionFunction: Potentially infinite recursion "
349
215
            "detected in function {%s}%s.\n",
350
215
            ctxt->context->functionURI, ctxt->context->function);
351
215
        tctxt->state = XSLT_STATE_STOPPED;
352
215
        return;
353
215
    }
354
11.7k
    tctxt->depth++;
355
356
    /* Evaluating templates can change the XPath context node. */
357
11.7k
    oldXPNode = tctxt->xpathCtxt->node;
358
359
11.7k
    fake = xmlNewDocNode(tctxt->output, NULL,
360
11.7k
       (const xmlChar *)"fake", NULL);
361
11.7k
    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
11.7k
    newBase = tctxt->varsNr;
386
    /* If there are any parameters */
387
11.7k
    if (paramNode != NULL) {
388
8.15k
        if (nargs > 0) {
389
7.77k
            args = (xmlXPathObjectPtr *) xmlMalloc(sizeof(*args) * nargs);
390
7.77k
            if (args == NULL)
391
14
                goto error;
392
            /* Fetch the stored argument values from the caller */
393
19.5k
            for (i = nargs - 1; i >= 0; i--) {
394
11.8k
                args[i] = valuePop(ctxt);
395
11.8k
            }
396
7.76k
        }
397
398
  /*
399
   * Prepare to process params in reverse order.  First, go to
400
   * the beginning of the param chain.
401
   */
402
21.4k
  for (i = 1; i <= func->nargs; i++) {
403
21.4k
      if (paramNode->prev == NULL)
404
8.13k
          break;
405
13.2k
      paramNode = paramNode->prev;
406
13.2k
  }
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
29.1k
  for (i = 0; i < func->nargs; i++) {
413
21.1k
      param = xsltParseStylesheetCallerParam (tctxt, paramNode);
414
21.1k
            if (param == NULL) {
415
123
                xsltLocalVariablePop(tctxt, newBase, -2);
416
123
          xsltFreeStackElemList(params);
417
182
                for (; i < nargs; i++)
418
59
                    xmlXPathFreeObject(args[i]);
419
123
                goto error;
420
123
            }
421
20.9k
      if (i < nargs) { /* if parameter value set */
422
11.7k
    param->computed = 1;
423
11.7k
    if (param->value != NULL)
424
7.46k
        xmlXPathFreeObject(param->value);
425
11.7k
    param->value = args[i];
426
11.7k
      }
427
20.9k
      xsltLocalVariablePush(tctxt, param, -1);
428
20.9k
      param->next = params;
429
20.9k
      params = param;
430
20.9k
      paramNode = paramNode->next;
431
20.9k
  }
432
8.13k
    }
433
    /*
434
     * Actual processing. The context variable is cleared and restored
435
     * when func:result is evaluated.
436
     */
437
11.6k
    oldBase = tctxt->varsBase;
438
11.6k
    oldInsert = tctxt->insert;
439
11.6k
    oldCtxtVar = data->ctxtVar;
440
11.6k
    data->ctxtVar = tctxt->contextVariable;
441
11.6k
    tctxt->varsBase = newBase;
442
11.6k
    tctxt->insert = fake;
443
11.6k
    tctxt->contextVariable = NULL;
444
11.6k
    xsltApplyOneTemplate (tctxt, tctxt->node,
445
11.6k
        func->content, NULL, NULL);
446
11.6k
    xsltLocalVariablePop(tctxt, tctxt->varsBase, -2);
447
11.6k
    tctxt->insert = oldInsert;
448
11.6k
    tctxt->contextVariable = data->ctxtVar;
449
11.6k
    tctxt->varsBase = oldBase;  /* restore original scope */
450
11.6k
    data->ctxtVar = oldCtxtVar;
451
11.6k
    if (params != NULL)
452
8.01k
  xsltFreeStackElemList(params);
453
11.6k
    tctxt->xpathCtxt->node = oldXPNode;
454
455
11.6k
    if (data->error != 0)
456
700
        goto error;
457
458
10.9k
    if (data->result != NULL) {
459
2.31k
  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
2.31k
        xsltFlagRVTs(tctxt, ret, XSLT_RVT_LOCAL);
466
2.31k
    } else
467
8.58k
  ret = xmlXPathNewCString("");
468
469
10.9k
    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
10.9k
    if (fake->children != NULL) {
476
3.92k
  xsltGenericError(xsltGenericErrorContext,
477
3.92k
       "{%s}%s: cannot write to result tree while "
478
3.92k
       "executing a function\n",
479
3.92k
       ctxt->context->functionURI, ctxt->context->function);
480
3.92k
        xmlXPathFreeObject(ret);
481
3.92k
  goto error;
482
3.92k
    }
483
6.98k
    valuePush(ctxt, ret);
484
485
11.7k
error:
486
11.7k
    xmlFree(args);
487
11.7k
    xmlFreeNode(fake);
488
11.7k
    tctxt->depth--;
489
11.7k
}
490
491
492
static void
493
7.53k
exsltFuncFunctionComp (xsltStylesheetPtr style, xmlNodePtr inst) {
494
7.53k
    xmlChar *name, *prefix;
495
7.53k
    xmlNsPtr ns;
496
7.53k
    xmlHashTablePtr data;
497
7.53k
    exsltFuncFunctionData *func;
498
499
7.53k
    if ((style == NULL) || (inst == NULL) || (inst->type != XML_ELEMENT_NODE))
500
0
  return;
501
502
7.53k
    {
503
7.53k
  xmlChar *qname;
504
505
7.53k
  qname = xmlGetProp(inst, (const xmlChar *) "name");
506
7.53k
  name = xmlSplitQName2 (qname, &prefix);
507
7.53k
  xmlFree(qname);
508
7.53k
    }
509
7.53k
    if ((name == NULL) || (prefix == NULL)) {
510
809
  xsltGenericError(xsltGenericErrorContext,
511
809
       "func:function: not a QName\n");
512
809
  if (name != NULL)
513
0
      xmlFree(name);
514
809
  return;
515
809
    }
516
    /* namespace lookup */
517
6.73k
    ns = xmlSearchNs (inst->doc, inst, prefix);
518
6.73k
    if (ns == NULL) {
519
801
  xsltGenericError(xsltGenericErrorContext,
520
801
       "func:function: undeclared prefix %s\n",
521
801
       prefix);
522
801
  xmlFree(name);
523
801
  xmlFree(prefix);
524
801
  return;
525
801
    }
526
5.92k
    xmlFree(prefix);
527
528
5.92k
    xsltParseTemplateContent(style, inst);
529
530
    /*
531
     * Create function data
532
     */
533
5.92k
    func = exsltFuncNewFunctionData();
534
5.92k
    if (func == NULL) {
535
22
        xmlFree(name);
536
22
        return;
537
22
    }
538
5.90k
    func->content = inst->children;
539
7.96k
    while (IS_XSLT_ELEM(func->content) &&
540
2.77k
     IS_XSLT_NAME(func->content, "param")) {
541
2.05k
  func->content = func->content->next;
542
2.05k
  func->nargs++;
543
2.05k
    }
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
5.90k
    data = (xmlHashTablePtr)
560
5.90k
  xsltStyleGetExtData (style, EXSLT_FUNCTIONS_NAMESPACE);
561
5.90k
#endif
562
5.90k
    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
5.90k
    if (xmlHashAddEntry2 (data, ns->href, name, func) < 0) {
571
527
  xsltTransformError(NULL, style, inst,
572
527
      "Failed to register function {%s}%s\n",
573
527
       ns->href, name);
574
527
  style->errors++;
575
527
        xmlFree(func);
576
5.37k
    } else {
577
5.37k
  xsltGenericDebug(xsltGenericDebugContext,
578
5.37k
       "exsltFuncFunctionComp: register {%s}%s\n",
579
5.37k
       ns->href, name);
580
5.37k
    }
581
5.90k
    xmlFree(name);
582
5.90k
}
583
584
static xsltElemPreCompPtr
585
exsltFuncResultComp (xsltStylesheetPtr style, xmlNodePtr inst,
586
9.23k
         xsltTransformFunction function) {
587
9.23k
    xmlNodePtr test;
588
9.23k
    xmlChar *sel;
589
9.23k
    exsltFuncResultPreComp *ret;
590
591
9.23k
    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
15.3k
    for (test = inst->next; test != NULL; test = test->next) {
601
8.88k
  if (test->type != XML_ELEMENT_NODE)
602
5.86k
      continue;
603
3.01k
  if (IS_XSLT_ELEM(test) && IS_XSLT_NAME(test, "fallback"))
604
204
      continue;
605
2.81k
  xsltGenericError(xsltGenericErrorContext,
606
2.81k
       "exsltFuncResultElem: only xsl:fallback is "
607
2.81k
       "allowed to follow func:result\n");
608
2.81k
  style->errors++;
609
2.81k
  return (NULL);
610
3.01k
    }
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
21.1k
    for (test = inst->parent; test != NULL; test = test->parent) {
620
21.1k
  if (/* Traversal has reached the top-level document without
621
         * finding a func:function ancestor. */
622
21.1k
        (test != NULL && test->type == XML_DOCUMENT_NODE) ||
623
        /* Traversal reached a stylesheet-namespace node,
624
         * and has left the function namespace. */
625
21.1k
        (IS_XSLT_ELEM(test) &&
626
3.95k
         IS_XSLT_NAME(test, "stylesheet"))) {
627
1.08k
      xsltGenericError(xsltGenericErrorContext,
628
1.08k
           "func:result element not a descendant "
629
1.08k
           "of a func:function\n");
630
1.08k
      style->errors++;
631
1.08k
      return (NULL);
632
1.08k
  }
633
20.0k
  if ((test->ns != NULL) &&
634
9.92k
      (xmlStrEqual(test->ns->href, EXSLT_FUNCTIONS_NAMESPACE))) {
635
6.26k
      if (xmlStrEqual(test->name, (const xmlChar *) "function")) {
636
3.69k
    break;
637
3.69k
      }
638
2.56k
      if (xmlStrEqual(test->name, (const xmlChar *) "result")) {
639
1.64k
    xsltGenericError(xsltGenericErrorContext,
640
1.64k
         "func:result element not allowed within"
641
1.64k
         " another func:result element\n");
642
1.64k
          style->errors++;
643
1.64k
    return (NULL);
644
1.64k
      }
645
2.56k
  }
646
14.6k
  if (IS_XSLT_ELEM(test) &&
647
2.86k
      (IS_XSLT_NAME(test, "variable") ||
648
2.86k
       IS_XSLT_NAME(test, "param"))) {
649
0
      xsltGenericError(xsltGenericErrorContext,
650
0
           "func:result element not allowed within"
651
0
           " a variable binding element\n");
652
0
            style->errors++;
653
0
      return (NULL);
654
0
  }
655
14.6k
    }
656
657
    /*
658
     * Precomputation
659
     */
660
3.69k
    ret = (exsltFuncResultPreComp *)
661
3.69k
  xmlMalloc (sizeof(exsltFuncResultPreComp));
662
3.69k
    if (ret == NULL) {
663
81
  xsltPrintErrorContext(NULL, NULL, NULL);
664
81
        xsltGenericError(xsltGenericErrorContext,
665
81
                         "exsltFuncResultComp : malloc failed\n");
666
81
        style->errors++;
667
81
        return (NULL);
668
81
    }
669
3.61k
    memset(ret, 0, sizeof(exsltFuncResultPreComp));
670
671
3.61k
    xsltInitElemPreComp ((xsltElemPreCompPtr) ret, style, inst, function,
672
3.61k
     exsltFreeFuncResultPreComp);
673
3.61k
    ret->select = NULL;
674
675
    /*
676
     * Precompute the select attribute
677
     */
678
3.61k
    sel = xmlGetNsProp(inst, (const xmlChar *) "select", NULL);
679
3.61k
    if (sel != NULL) {
680
1.74k
  ret->select = xsltXPathCompileFlags(style, sel, 0);
681
1.74k
  xmlFree(sel);
682
1.74k
    }
683
    /*
684
     * Precompute the namespace list
685
     */
686
3.61k
    ret->nsList = xmlGetNsList(inst->doc, inst);
687
3.61k
    if (ret->nsList != NULL) {
688
3.61k
        int i = 0;
689
20.8k
        while (ret->nsList[i] != NULL)
690
17.2k
      i++;
691
3.61k
  ret->nsNr = i;
692
3.61k
    }
693
3.61k
    return ((xsltElemPreCompPtr) ret);
694
3.69k
}
695
696
static void
697
exsltFuncResultElem (xsltTransformContextPtr ctxt,
698
               xmlNodePtr node ATTRIBUTE_UNUSED, xmlNodePtr inst,
699
7.01k
         xsltElemPreCompPtr ecomp) {
700
7.01k
    exsltFuncResultPreComp *comp = (exsltFuncResultPreComp *) ecomp;
701
7.01k
    exsltFuncData *data;
702
7.01k
    xmlXPathObjectPtr ret;
703
704
705
    /* It is an error if instantiating the content of the
706
     * func:function element results in the instantiation of more than
707
     * one func:result elements.
708
     */
709
7.01k
    data = (exsltFuncData *) xsltGetExtData (ctxt, EXSLT_FUNCTIONS_NAMESPACE);
710
7.01k
    if (data == NULL) {
711
0
  xsltGenericError(xsltGenericErrorContext,
712
0
       "exsltFuncReturnElem: data == NULL\n");
713
0
  return;
714
0
    }
715
7.01k
    if (data->result != NULL) {
716
80
  xsltGenericError(xsltGenericErrorContext,
717
80
       "func:result already instanciated\n");
718
80
  data->error = 1;
719
80
  return;
720
80
    }
721
    /*
722
     * Restore context variable, so that it will receive the function
723
     * result RVTs.
724
     */
725
6.93k
    ctxt->contextVariable = data->ctxtVar;
726
    /*
727
     * Processing
728
     */
729
6.93k
    if (comp->select != NULL) {
730
5.72k
  xmlNsPtr *oldXPNsList;
731
5.72k
  int oldXPNsNr;
732
5.72k
  xmlNodePtr oldXPContextNode;
733
  /* If the func:result element has a select attribute, then the
734
   * value of the attribute must be an expression and the
735
   * returned value is the object that results from evaluating
736
   * the expression. In this case, the content must be empty.
737
   */
738
5.72k
  if (inst->children != NULL) {
739
0
      xsltGenericError(xsltGenericErrorContext,
740
0
           "func:result content must be empty if"
741
0
           " the function has a select attribute\n");
742
0
      data->error = 1;
743
0
      return;
744
0
  }
745
5.72k
  oldXPNsList = ctxt->xpathCtxt->namespaces;
746
5.72k
  oldXPNsNr = ctxt->xpathCtxt->nsNr;
747
5.72k
  oldXPContextNode = ctxt->xpathCtxt->node;
748
749
5.72k
  ctxt->xpathCtxt->namespaces = comp->nsList;
750
5.72k
  ctxt->xpathCtxt->nsNr = comp->nsNr;
751
5.72k
        ctxt->xpathCtxt->node = ctxt->node;
752
753
5.72k
  ret = xmlXPathCompiledEval(comp->select, ctxt->xpathCtxt);
754
755
5.72k
  ctxt->xpathCtxt->node = oldXPContextNode;
756
5.72k
  ctxt->xpathCtxt->nsNr = oldXPNsNr;
757
5.72k
  ctxt->xpathCtxt->namespaces = oldXPNsList;
758
759
5.72k
  if (ret == NULL) {
760
4.33k
      xsltGenericError(xsltGenericErrorContext,
761
4.33k
           "exsltFuncResultElem: ret == NULL\n");
762
4.33k
      return;
763
4.33k
  }
764
  /*
765
  * Mark it as a function result in order to avoid garbage
766
  * collecting of tree fragments before the function exits.
767
  */
768
1.39k
  xsltFlagRVTs(ctxt, ret, XSLT_RVT_FUNC_RESULT);
769
1.39k
    } else if (inst->children != NULL) {
770
  /* If the func:result element does not have a select attribute
771
   * and has non-empty content (i.e. the func:result element has
772
   * one or more child nodes), then the content of the
773
   * func:result element specifies the value.
774
   */
775
723
  xmlNodePtr oldInsert;
776
723
  xmlDocPtr container;
777
778
723
  container = xsltCreateRVT(ctxt);
779
723
  if (container == NULL) {
780
2
      xsltGenericError(xsltGenericErrorContext,
781
2
           "exsltFuncResultElem: out of memory\n");
782
2
      data->error = 1;
783
2
      return;
784
2
  }
785
        /* Mark as function result. */
786
721
        xsltRegisterLocalRVT(ctxt, container);
787
721
        container->compression = XSLT_RVT_FUNC_RESULT;
788
789
721
  oldInsert = ctxt->insert;
790
721
  ctxt->insert = (xmlNodePtr) container;
791
721
  xsltApplyOneTemplate (ctxt, ctxt->node,
792
721
            inst->children, NULL, NULL);
793
721
  ctxt->insert = oldInsert;
794
795
721
  ret = xmlXPathNewValueTree((xmlNodePtr) container);
796
721
  if (ret == NULL) {
797
201
      xsltGenericError(xsltGenericErrorContext,
798
201
           "exsltFuncResultElem: ret == NULL\n");
799
201
      data->error = 1;
800
520
  } else {
801
            /*
802
             * This stops older libxml2 versions from freeing the nodes
803
             * in the tree.
804
             */
805
520
      ret->boolval = 0;
806
520
  }
807
721
    } else {
808
  /* If the func:result element has empty content and does not
809
   * have a select attribute, then the returned value is an
810
   * empty string.
811
   */
812
481
  ret = xmlXPathNewCString("");
813
481
    }
814
2.59k
    data->result = ret;
815
2.59k
}
816
817
/**
818
 * exsltFuncRegister:
819
 *
820
 * Registers the EXSLT - Functions module
821
 */
822
void
823
4
exsltFuncRegister (void) {
824
4
    xsltRegisterExtModuleFull (EXSLT_FUNCTIONS_NAMESPACE,
825
4
           exsltFuncInit,
826
4
           exsltFuncShutdown,
827
4
           exsltFuncStyleInit,
828
4
           exsltFuncStyleShutdown);
829
830
4
    xsltRegisterExtModuleTopLevel ((const xmlChar *) "function",
831
4
           EXSLT_FUNCTIONS_NAMESPACE,
832
4
           exsltFuncFunctionComp);
833
4
    xsltRegisterExtModuleElement ((const xmlChar *) "result",
834
4
        EXSLT_FUNCTIONS_NAMESPACE,
835
4
        exsltFuncResultComp,
836
4
        exsltFuncResultElem);
837
4
}