Coverage Report

Created: 2026-09-01 06:53

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