Coverage Report

Created: 2026-07-30 06:58

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
506
           ATTRIBUTE_UNUSED const xmlChar *ignored) {
69
506
    exsltFuncFunctionData *data = (exsltFuncFunctionData *) payload;
70
506
    xsltTransformContextPtr ctxt = (xsltTransformContextPtr) vctxt;
71
72
506
    if ((data == NULL) || (ctxt == NULL) || (URI == NULL) || (name == NULL))
73
0
  return;
74
75
506
    xsltGenericDebug(xsltGenericDebugContext,
76
506
         "exsltFuncRegisterFunc: register {%s}%s\n",
77
506
         URI, name);
78
506
    xsltRegisterExtFunction(ctxt, name, URI,
79
506
          exsltFuncFunctionFunction);
80
506
}
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
167
           ATTRIBUTE_UNUSED const xmlChar *ignored) {
96
167
    exsltFuncFunctionData *data = (exsltFuncFunctionData *) payload;
97
167
    exsltFuncImportRegData *ch = (exsltFuncImportRegData *) vctxt;
98
167
    exsltFuncFunctionData *func=NULL;
99
100
167
    if ((data == NULL) || (ch == NULL) || (URI == NULL) || (name == NULL))
101
0
            return;
102
103
167
    if (ch->ctxt == NULL || ch->hash == NULL)
104
22
  return;
105
106
    /* Check if already present */
107
145
    func = (exsltFuncFunctionData*)xmlHashLookup2(ch->hash, URI, name);
108
145
    if (func == NULL) {   /* Not yet present - copy it in */
109
98
  func = exsltFuncNewFunctionData();
110
98
        if (func == NULL)
111
39
            return;
112
59
  memcpy(func, data, sizeof(exsltFuncFunctionData));
113
59
  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
56
  } else {   /* Do the registration */
119
56
      xsltGenericDebug(xsltGenericDebugContext,
120
56
              "exsltFuncRegisterImportFunc: register {%s}%s\n",
121
56
        URI, name);
122
56
      xsltRegisterExtFunction(ch->ctxt, name, URI,
123
56
        exsltFuncFunctionFunction);
124
56
  }
125
59
    }
126
145
}
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
413
exsltFuncInit (xsltTransformContextPtr ctxt, const xmlChar *URI) {
143
413
    exsltFuncData *ret;
144
413
    xsltStylesheetPtr tmp;
145
413
    exsltFuncImportRegData ch;
146
413
    xmlHashTablePtr hash;
147
148
413
    ret = (exsltFuncData *) xmlMalloc (sizeof(exsltFuncData));
149
413
    if (ret == NULL) {
150
3
  xsltGenericError(xsltGenericErrorContext,
151
3
       "exsltFuncInit: not enough memory\n");
152
3
  return(NULL);
153
3
    }
154
410
    memset(ret, 0, sizeof(exsltFuncData));
155
156
410
    ret->result = NULL;
157
410
    ret->error = 0;
158
159
410
    ch.hash = (xmlHashTablePtr) xsltStyleGetExtData(ctxt->style, URI);
160
410
    ret->funcs = ch.hash;
161
410
    xmlHashScanFull(ch.hash, exsltFuncRegisterFunc, ctxt);
162
410
    tmp = ctxt->style;
163
410
    ch.ctxt = ctxt;
164
521
    while ((tmp=xsltNextImport(tmp))!=NULL) {
165
111
  hash = xsltGetExtInfo(tmp, URI);
166
111
  if (hash != NULL) {
167
107
      xmlHashScanFull(hash, exsltFuncRegisterImportFunc, &ch);
168
107
  }
169
111
    }
170
171
410
    return(ret);
172
413
}
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
413
       void *vdata) {
187
413
    exsltFuncData *data = (exsltFuncData *) vdata;
188
189
413
    if (data != NULL) {
190
410
        if (data->result != NULL)
191
5
            xmlXPathFreeObject(data->result);
192
410
        xmlFree(data);
193
410
    }
194
413
}
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
3.90k
        const xmlChar *URI ATTRIBUTE_UNUSED) {
209
3.90k
    return xmlHashCreate(1);
210
3.90k
}
211
212
static void
213
4.06k
exsltFuncFreeDataEntry(void *payload, const xmlChar *name ATTRIBUTE_UNUSED) {
214
4.06k
    xmlFree(payload);
215
4.06k
}
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
3.90k
      void *vdata) {
230
3.90k
    xmlHashTablePtr data = (xmlHashTablePtr) vdata;
231
3.90k
    xmlHashFree(data, exsltFuncFreeDataEntry);
232
3.90k
}
233
234
/**
235
 * exsltFuncNewFunctionData:
236
 *
237
 * Allocates an #exslFuncFunctionData object
238
 *
239
 * Returns the new structure
240
 */
241
static exsltFuncFunctionData *
242
4.38k
exsltFuncNewFunctionData (void) {
243
4.38k
    exsltFuncFunctionData *ret;
244
245
4.38k
    ret = (exsltFuncFunctionData *) xmlMalloc (sizeof(exsltFuncFunctionData));
246
4.38k
    if (ret == NULL) {
247
56
  xsltGenericError(xsltGenericErrorContext,
248
56
       "exsltFuncNewFunctionData: not enough memory\n");
249
56
  return (NULL);
250
56
    }
251
4.32k
    memset(ret, 0, sizeof(exsltFuncFunctionData));
252
253
4.32k
    ret->nargs = 0;
254
4.32k
    ret->content = NULL;
255
256
4.32k
    return(ret);
257
4.38k
}
258
259
/**
260
 * exsltFreeFuncResultPreComp:
261
 * @comp:  the #exsltFuncResultPreComp to free up
262
 *
263
 * Deallocates an #exsltFuncResultPreComp
264
 */
265
static void
266
2.37k
exsltFreeFuncResultPreComp (xsltElemPreCompPtr ecomp) {
267
2.37k
    exsltFuncResultPreComp *comp = (exsltFuncResultPreComp *) ecomp;
268
269
2.37k
    if (comp == NULL)
270
0
  return;
271
272
2.37k
    if (comp->select != NULL)
273
816
  xmlXPathFreeCompExpr (comp->select);
274
2.37k
    if (comp->nsList != NULL)
275
2.37k
        xmlFree(comp->nsList);
276
2.37k
    xmlFree(comp);
277
2.37k
}
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
9.06k
exsltFuncFunctionFunction (xmlXPathParserContextPtr ctxt, int nargs) {
288
9.06k
    xmlXPathObjectPtr oldResult, ret;
289
9.06k
    exsltFuncData *data;
290
9.06k
    exsltFuncFunctionData *func;
291
9.06k
    xmlNodePtr paramNode, oldInsert, oldXPNode, fake;
292
9.06k
    int oldBase, newBase;
293
9.06k
    void *oldCtxtVar;
294
9.06k
    xsltStackElemPtr params = NULL, param;
295
9.06k
    xsltTransformContextPtr tctxt = xsltXPathGetTransformContext(ctxt);
296
9.06k
    int i;
297
9.06k
    xmlXPathObjectPtr *args = NULL;
298
299
    /*
300
     * retrieve func:function template
301
     */
302
9.06k
    data = (exsltFuncData *) xsltGetExtData (tctxt,
303
9.06k
               EXSLT_FUNCTIONS_NAMESPACE);
304
9.06k
    oldResult = data->result;
305
9.06k
    data->result = NULL;
306
307
9.06k
    func = (exsltFuncFunctionData*) xmlHashLookup2 (data->funcs,
308
9.06k
                ctxt->context->functionURI,
309
9.06k
                ctxt->context->function);
310
9.06k
    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
9.06k
    if (nargs > func->nargs) {
323
431
  xsltGenericError(xsltGenericErrorContext,
324
431
       "{%s}%s: called with too many arguments\n",
325
431
       ctxt->context->functionURI, ctxt->context->function);
326
431
  ctxt->error = XPATH_INVALID_ARITY;
327
431
  return;
328
431
    }
329
8.63k
    if (func->content != NULL) {
330
8.52k
  paramNode = func->content->prev;
331
8.52k
    }
332
107
    else
333
107
  paramNode = NULL;
334
8.63k
    if ((paramNode == NULL) && (func->nargs != 0)) {
335
37
  xsltGenericError(xsltGenericErrorContext,
336
37
       "exsltFuncFunctionFunction: nargs != 0 and "
337
37
       "param == NULL\n");
338
37
  return;
339
37
    }
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
8.59k
    if (tctxt->depth >= tctxt->maxTemplateDepth) {
347
234
        xsltTransformError(tctxt, NULL, NULL,
348
234
            "exsltFuncFunctionFunction: Potentially infinite recursion "
349
234
            "detected in function {%s}%s.\n",
350
234
            ctxt->context->functionURI, ctxt->context->function);
351
234
        tctxt->state = XSLT_STATE_STOPPED;
352
234
        return;
353
234
    }
354
8.36k
    tctxt->depth++;
355
356
    /* Evaluating templates can change the XPath context node. */
357
8.36k
    oldXPNode = tctxt->xpathCtxt->node;
358
359
8.36k
    fake = xmlNewDocNode(tctxt->output, NULL,
360
8.36k
       (const xmlChar *)"fake", NULL);
361
8.36k
    if (fake == NULL)
362
11
        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
8.35k
    newBase = tctxt->varsNr;
386
    /* If there are any parameters */
387
8.35k
    if (paramNode != NULL) {
388
5.98k
        if (nargs > 0) {
389
5.44k
            args = (xmlXPathObjectPtr *) xmlMalloc(sizeof(*args) * nargs);
390
5.44k
            if (args == NULL)
391
5
                goto error;
392
            /* Fetch the stored argument values from the caller */
393
13.7k
            for (i = nargs - 1; i >= 0; i--) {
394
8.30k
                args[i] = valuePop(ctxt);
395
8.30k
            }
396
5.44k
        }
397
398
  /*
399
   * Prepare to process params in reverse order.  First, go to
400
   * the beginning of the param chain.
401
   */
402
14.3k
  for (i = 1; i <= func->nargs; i++) {
403
14.3k
      if (paramNode->prev == NULL)
404
5.97k
          break;
405
8.40k
      paramNode = paramNode->prev;
406
8.40k
  }
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
20.0k
  for (i = 0; i < func->nargs; i++) {
413
14.1k
      param = xsltParseStylesheetCallerParam (tctxt, paramNode);
414
14.1k
            if (param == NULL) {
415
82
                xsltLocalVariablePop(tctxt, newBase, -2);
416
82
          xsltFreeStackElemList(params);
417
126
                for (; i < nargs; i++)
418
44
                    xmlXPathFreeObject(args[i]);
419
82
                goto error;
420
82
            }
421
14.0k
      if (i < nargs) { /* if parameter value set */
422
8.26k
    param->computed = 1;
423
8.26k
    if (param->value != NULL)
424
4.93k
        xmlXPathFreeObject(param->value);
425
8.26k
    param->value = args[i];
426
8.26k
      }
427
14.0k
      xsltLocalVariablePush(tctxt, param, -1);
428
14.0k
      param->next = params;
429
14.0k
      params = param;
430
14.0k
      paramNode = paramNode->next;
431
14.0k
  }
432
5.97k
    }
433
    /*
434
     * Actual processing. The context variable is cleared and restored
435
     * when func:result is evaluated.
436
     */
437
8.26k
    oldBase = tctxt->varsBase;
438
8.26k
    oldInsert = tctxt->insert;
439
8.26k
    oldCtxtVar = data->ctxtVar;
440
8.26k
    data->ctxtVar = tctxt->contextVariable;
441
8.26k
    tctxt->varsBase = newBase;
442
8.26k
    tctxt->insert = fake;
443
8.26k
    tctxt->contextVariable = NULL;
444
8.26k
    xsltApplyOneTemplate (tctxt, tctxt->node,
445
8.26k
        func->content, NULL, NULL);
446
8.26k
    xsltLocalVariablePop(tctxt, tctxt->varsBase, -2);
447
8.26k
    tctxt->insert = oldInsert;
448
8.26k
    tctxt->contextVariable = data->ctxtVar;
449
8.26k
    tctxt->varsBase = oldBase;  /* restore original scope */
450
8.26k
    data->ctxtVar = oldCtxtVar;
451
8.26k
    if (params != NULL)
452
5.89k
  xsltFreeStackElemList(params);
453
8.26k
    tctxt->xpathCtxt->node = oldXPNode;
454
455
8.26k
    if (data->error != 0)
456
434
        goto error;
457
458
7.83k
    if (data->result != NULL) {
459
1.69k
  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
1.69k
        xsltFlagRVTs(tctxt, ret, XSLT_RVT_LOCAL);
466
1.69k
    } else
467
6.14k
  ret = xmlXPathNewCString("");
468
469
7.83k
    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
7.83k
    if (fake->children != NULL) {
476
2.32k
  xsltGenericError(xsltGenericErrorContext,
477
2.32k
       "{%s}%s: cannot write to result tree while "
478
2.32k
       "executing a function\n",
479
2.32k
       ctxt->context->functionURI, ctxt->context->function);
480
2.32k
        xmlXPathFreeObject(ret);
481
2.32k
  goto error;
482
2.32k
    }
483
5.51k
    valuePush(ctxt, ret);
484
485
8.36k
error:
486
8.36k
    xmlFree(args);
487
8.36k
    xmlFreeNode(fake);
488
8.36k
    tctxt->depth--;
489
8.36k
}
490
491
492
static void
493
4.98k
exsltFuncFunctionComp (xsltStylesheetPtr style, xmlNodePtr inst) {
494
4.98k
    xmlChar *name, *prefix;
495
4.98k
    xmlNsPtr ns;
496
4.98k
    xmlHashTablePtr data;
497
4.98k
    exsltFuncFunctionData *func;
498
499
4.98k
    if ((style == NULL) || (inst == NULL) || (inst->type != XML_ELEMENT_NODE))
500
0
  return;
501
502
4.98k
    {
503
4.98k
  xmlChar *qname;
504
505
4.98k
  qname = xmlGetProp(inst, (const xmlChar *) "name");
506
4.98k
  name = xmlSplitQName2 (qname, &prefix);
507
4.98k
  xmlFree(qname);
508
4.98k
    }
509
4.98k
    if ((name == NULL) || (prefix == NULL)) {
510
379
  xsltGenericError(xsltGenericErrorContext,
511
379
       "func:function: not a QName\n");
512
379
  if (name != NULL)
513
0
      xmlFree(name);
514
379
  return;
515
379
    }
516
    /* namespace lookup */
517
4.60k
    ns = xmlSearchNs (inst->doc, inst, prefix);
518
4.60k
    if (ns == NULL) {
519
318
  xsltGenericError(xsltGenericErrorContext,
520
318
       "func:function: undeclared prefix %s\n",
521
318
       prefix);
522
318
  xmlFree(name);
523
318
  xmlFree(prefix);
524
318
  return;
525
318
    }
526
4.28k
    xmlFree(prefix);
527
528
4.28k
    xsltParseTemplateContent(style, inst);
529
530
    /*
531
     * Create function data
532
     */
533
4.28k
    func = exsltFuncNewFunctionData();
534
4.28k
    if (func == NULL) {
535
17
        xmlFree(name);
536
17
        return;
537
17
    }
538
4.26k
    func->content = inst->children;
539
5.19k
    while (IS_XSLT_ELEM(func->content) &&
540
1.35k
     IS_XSLT_NAME(func->content, "param")) {
541
923
  func->content = func->content->next;
542
923
  func->nargs++;
543
923
    }
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
4.26k
    data = (xmlHashTablePtr)
560
4.26k
  xsltStyleGetExtData (style, EXSLT_FUNCTIONS_NAMESPACE);
561
4.26k
#endif
562
4.26k
    if (data == NULL) {
563
1
  xsltGenericError(xsltGenericErrorContext,
564
1
       "exsltFuncFunctionComp: no stylesheet data\n");
565
1
  xmlFree(name);
566
1
        xmlFree(func);
567
1
  return;
568
1
    }
569
570
4.26k
    if (xmlHashAddEntry2 (data, ns->href, name, func) < 0) {
571
263
  xsltTransformError(NULL, style, inst,
572
263
      "Failed to register function {%s}%s\n",
573
263
       ns->href, name);
574
263
  style->errors++;
575
263
        xmlFree(func);
576
4.00k
    } else {
577
4.00k
  xsltGenericDebug(xsltGenericDebugContext,
578
4.00k
       "exsltFuncFunctionComp: register {%s}%s\n",
579
4.00k
       ns->href, name);
580
4.00k
    }
581
4.26k
    xmlFree(name);
582
4.26k
}
583
584
static xsltElemPreCompPtr
585
exsltFuncResultComp (xsltStylesheetPtr style, xmlNodePtr inst,
586
4.49k
         xsltTransformFunction function) {
587
4.49k
    xmlNodePtr test;
588
4.49k
    xmlChar *sel;
589
4.49k
    exsltFuncResultPreComp *ret;
590
591
4.49k
    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
6.63k
    for (test = inst->next; test != NULL; test = test->next) {
601
3.28k
  if (test->type != XML_ELEMENT_NODE)
602
2.08k
      continue;
603
1.20k
  if (IS_XSLT_ELEM(test) && IS_XSLT_NAME(test, "fallback"))
604
59
      continue;
605
1.14k
  xsltGenericError(xsltGenericErrorContext,
606
1.14k
       "exsltFuncResultElem: only xsl:fallback is "
607
1.14k
       "allowed to follow func:result\n");
608
1.14k
  style->errors++;
609
1.14k
  return (NULL);
610
1.20k
    }
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
7.00k
    for (test = inst->parent; test != NULL; test = test->parent) {
620
7.00k
  if (/* Traversal has reached the top-level document without
621
         * finding a func:function ancestor. */
622
7.00k
        (test != NULL && test->type == XML_DOCUMENT_NODE) ||
623
        /* Traversal reached a stylesheet-namespace node,
624
         * and has left the function namespace. */
625
7.00k
        (IS_XSLT_ELEM(test) &&
626
1.38k
         IS_XSLT_NAME(test, "stylesheet"))) {
627
283
      xsltGenericError(xsltGenericErrorContext,
628
283
           "func:result element not a descendant "
629
283
           "of a func:function\n");
630
283
      style->errors++;
631
283
      return (NULL);
632
283
  }
633
6.72k
  if ((test->ns != NULL) &&
634
4.19k
      (xmlStrEqual(test->ns->href, EXSLT_FUNCTIONS_NAMESPACE))) {
635
3.07k
      if (xmlStrEqual(test->name, (const xmlChar *) "function")) {
636
2.43k
    break;
637
2.43k
      }
638
646
      if (xmlStrEqual(test->name, (const xmlChar *) "result")) {
639
636
    xsltGenericError(xsltGenericErrorContext,
640
636
         "func:result element not allowed within"
641
636
         " another func:result element\n");
642
636
          style->errors++;
643
636
    return (NULL);
644
636
      }
645
646
  }
646
3.65k
  if (IS_XSLT_ELEM(test) &&
647
1.10k
      (IS_XSLT_NAME(test, "variable") ||
648
1.10k
       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
3.65k
    }
656
657
    /*
658
     * Precomputation
659
     */
660
2.43k
    ret = (exsltFuncResultPreComp *)
661
2.43k
  xmlMalloc (sizeof(exsltFuncResultPreComp));
662
2.43k
    if (ret == NULL) {
663
57
  xsltPrintErrorContext(NULL, NULL, NULL);
664
57
        xsltGenericError(xsltGenericErrorContext,
665
57
                         "exsltFuncResultComp : malloc failed\n");
666
57
        style->errors++;
667
57
        return (NULL);
668
57
    }
669
2.37k
    memset(ret, 0, sizeof(exsltFuncResultPreComp));
670
671
2.37k
    xsltInitElemPreComp ((xsltElemPreCompPtr) ret, style, inst, function,
672
2.37k
     exsltFreeFuncResultPreComp);
673
2.37k
    ret->select = NULL;
674
675
    /*
676
     * Precompute the select attribute
677
     */
678
2.37k
    sel = xmlGetNsProp(inst, (const xmlChar *) "select", NULL);
679
2.37k
    if (sel != NULL) {
680
1.05k
  ret->select = xsltXPathCompileFlags(style, sel, 0);
681
1.05k
  xmlFree(sel);
682
1.05k
    }
683
    /*
684
     * Precompute the namespace list
685
     */
686
2.37k
    ret->nsList = xmlGetNsList(inst->doc, inst);
687
2.37k
    if (ret->nsList != NULL) {
688
2.37k
        int i = 0;
689
14.9k
        while (ret->nsList[i] != NULL)
690
12.5k
      i++;
691
2.37k
  ret->nsNr = i;
692
2.37k
    }
693
2.37k
    return ((xsltElemPreCompPtr) ret);
694
2.43k
}
695
696
static void
697
exsltFuncResultElem (xsltTransformContextPtr ctxt,
698
               xmlNodePtr node ATTRIBUTE_UNUSED, xmlNodePtr inst,
699
5.19k
         xsltElemPreCompPtr ecomp) {
700
5.19k
    exsltFuncResultPreComp *comp = (exsltFuncResultPreComp *) ecomp;
701
5.19k
    exsltFuncData *data;
702
5.19k
    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
5.19k
    data = (exsltFuncData *) xsltGetExtData (ctxt, EXSLT_FUNCTIONS_NAMESPACE);
710
5.19k
    if (data == NULL) {
711
0
  xsltGenericError(xsltGenericErrorContext,
712
0
       "exsltFuncReturnElem: data == NULL\n");
713
0
  return;
714
0
    }
715
5.19k
    if (data->result != NULL) {
716
95
  xsltGenericError(xsltGenericErrorContext,
717
95
       "func:result already instanciated\n");
718
95
  data->error = 1;
719
95
  return;
720
95
    }
721
    /*
722
     * Restore context variable, so that it will receive the function
723
     * result RVTs.
724
     */
725
5.09k
    ctxt->contextVariable = data->ctxtVar;
726
    /*
727
     * Processing
728
     */
729
5.09k
    if (comp->select != NULL) {
730
4.16k
  xmlNsPtr *oldXPNsList;
731
4.16k
  int oldXPNsNr;
732
4.16k
  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
4.16k
  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
4.16k
  oldXPNsList = ctxt->xpathCtxt->namespaces;
746
4.16k
  oldXPNsNr = ctxt->xpathCtxt->nsNr;
747
4.16k
  oldXPContextNode = ctxt->xpathCtxt->node;
748
749
4.16k
  ctxt->xpathCtxt->namespaces = comp->nsList;
750
4.16k
  ctxt->xpathCtxt->nsNr = comp->nsNr;
751
4.16k
        ctxt->xpathCtxt->node = ctxt->node;
752
753
4.16k
  ret = xmlXPathCompiledEval(comp->select, ctxt->xpathCtxt);
754
755
4.16k
  ctxt->xpathCtxt->node = oldXPContextNode;
756
4.16k
  ctxt->xpathCtxt->nsNr = oldXPNsNr;
757
4.16k
  ctxt->xpathCtxt->namespaces = oldXPNsList;
758
759
4.16k
  if (ret == NULL) {
760
3.24k
      xsltGenericError(xsltGenericErrorContext,
761
3.24k
           "exsltFuncResultElem: ret == NULL\n");
762
3.24k
      return;
763
3.24k
  }
764
  /*
765
  * Mark it as a function result in order to avoid garbage
766
  * collecting of tree fragments before the function exits.
767
  */
768
921
  xsltFlagRVTs(ctxt, ret, XSLT_RVT_FUNC_RESULT);
769
932
    } 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
504
  xmlNodePtr oldInsert;
776
504
  xmlDocPtr container;
777
778
504
  container = xsltCreateRVT(ctxt);
779
504
  if (container == NULL) {
780
1
      xsltGenericError(xsltGenericErrorContext,
781
1
           "exsltFuncResultElem: out of memory\n");
782
1
      data->error = 1;
783
1
      return;
784
1
  }
785
        /* Mark as function result. */
786
503
        xsltRegisterLocalRVT(ctxt, container);
787
503
        container->compression = XSLT_RVT_FUNC_RESULT;
788
789
503
  oldInsert = ctxt->insert;
790
503
  ctxt->insert = (xmlNodePtr) container;
791
503
  xsltApplyOneTemplate (ctxt, ctxt->node,
792
503
            inst->children, NULL, NULL);
793
503
  ctxt->insert = oldInsert;
794
795
503
  ret = xmlXPathNewValueTree((xmlNodePtr) container);
796
503
  if (ret == NULL) {
797
112
      xsltGenericError(xsltGenericErrorContext,
798
112
           "exsltFuncResultElem: ret == NULL\n");
799
112
      data->error = 1;
800
391
  } else {
801
            /*
802
             * This stops older libxml2 versions from freeing the nodes
803
             * in the tree.
804
             */
805
391
      ret->boolval = 0;
806
391
  }
807
503
    } 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
428
  ret = xmlXPathNewCString("");
813
428
    }
814
1.85k
    data->result = ret;
815
1.85k
}
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
}