Coverage Report

Created: 2025-12-03 06:15

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