Coverage Report

Created: 2023-03-26 06:14

/src/libxslt/libexslt/functions.c
Line
Count
Source (jump to first uncovered line)
1
#define IN_LIBEXSLT
2
#include "libexslt/libexslt.h"
3
4
#include <string.h>
5
6
#include <libxml/tree.h>
7
#include <libxml/xpath.h>
8
#include <libxml/xpathInternals.h>
9
#include <libxml/hash.h>
10
#include <libxml/debugXML.h>
11
12
#include <libxslt/xsltutils.h>
13
#include <libxslt/variables.h>
14
#include <libxslt/xsltInternals.h>
15
#include <libxslt/extensions.h>
16
#include <libxslt/transform.h>
17
#include <libxslt/imports.h>
18
19
#include "exslt.h"
20
21
typedef struct _exsltFuncFunctionData exsltFuncFunctionData;
22
struct _exsltFuncFunctionData {
23
    int nargs;      /* number of arguments to the function */
24
    xmlNodePtr content;   /* the func:fuction template content */
25
};
26
27
typedef struct _exsltFuncData exsltFuncData;
28
struct _exsltFuncData {
29
    xmlHashTablePtr funcs;  /* pointer to the stylesheet module data */
30
    xmlXPathObjectPtr result; /* returned by func:result */
31
    xsltStackElemPtr ctxtVar;   /* context variable */
32
    int error;      /* did an error occur? */
33
};
34
35
typedef struct _exsltFuncResultPreComp exsltFuncResultPreComp;
36
struct _exsltFuncResultPreComp {
37
    xsltElemPreComp comp;
38
    xmlXPathCompExprPtr select;
39
    xmlNsPtr *nsList;
40
    int nsNr;
41
};
42
43
/* Used for callback function in exsltInitFunc */
44
typedef struct _exsltFuncImportRegData exsltFuncImportRegData;
45
struct _exsltFuncImportRegData {
46
    xsltTransformContextPtr ctxt;
47
    xmlHashTablePtr hash;
48
};
49
50
static void exsltFuncFunctionFunction (xmlXPathParserContextPtr ctxt,
51
               int nargs);
52
static exsltFuncFunctionData *exsltFuncNewFunctionData(void);
53
54
/*static const xmlChar *exsltResultDataID = (const xmlChar *) "EXSLT Result";*/
55
56
/**
57
 * exsltFuncRegisterFunc:
58
 * @func:  the #exsltFuncFunctionData for the function
59
 * @ctxt:  an XSLT transformation context
60
 * @URI:  the function namespace URI
61
 * @name: the function name
62
 *
63
 * Registers a function declared by a func:function element
64
 */
65
static void
66
exsltFuncRegisterFunc (void *payload, void *vctxt,
67
           const xmlChar *URI, const xmlChar *name,
68
1.52k
           ATTRIBUTE_UNUSED const xmlChar *ignored) {
69
1.52k
    exsltFuncFunctionData *data = (exsltFuncFunctionData *) payload;
70
1.52k
    xsltTransformContextPtr ctxt = (xsltTransformContextPtr) vctxt;
71
72
1.52k
    if ((data == NULL) || (ctxt == NULL) || (URI == NULL) || (name == NULL))
73
0
  return;
74
75
1.52k
    xsltGenericDebug(xsltGenericDebugContext,
76
1.52k
         "exsltFuncRegisterFunc: register {%s}%s\n",
77
1.52k
         URI, name);
78
1.52k
    xsltRegisterExtFunction(ctxt, name, URI,
79
1.52k
          exsltFuncFunctionFunction);
80
1.52k
}
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
120
           ATTRIBUTE_UNUSED const xmlChar *ignored) {
96
120
    exsltFuncFunctionData *data = (exsltFuncFunctionData *) payload;
97
120
    exsltFuncImportRegData *ch = (exsltFuncImportRegData *) vctxt;
98
120
    exsltFuncFunctionData *func=NULL;
99
100
120
    if ((data == NULL) || (ch == NULL) || (URI == NULL) || (name == NULL))
101
0
            return;
102
103
120
    if (ch->ctxt == NULL || ch->hash == NULL)
104
0
  return;
105
106
    /* Check if already present */
107
120
    func = (exsltFuncFunctionData*)xmlHashLookup2(ch->hash, URI, name);
108
120
    if (func == NULL) {   /* Not yet present - copy it in */
109
45
  func = exsltFuncNewFunctionData();
110
45
        if (func == NULL)
111
10
            return;
112
35
  memcpy(func, data, sizeof(exsltFuncFunctionData));
113
35
  if (xmlHashAddEntry2(ch->hash, URI, name, func) < 0) {
114
1
      xsltGenericError(xsltGenericErrorContext,
115
1
        "Failed to register function {%s}%s\n",
116
1
        URI, name);
117
1
            xmlFree(func);
118
34
  } else {   /* Do the registration */
119
34
      xsltGenericDebug(xsltGenericDebugContext,
120
34
              "exsltFuncRegisterImportFunc: register {%s}%s\n",
121
34
        URI, name);
122
34
      xsltRegisterExtFunction(ch->ctxt, name, URI,
123
34
        exsltFuncFunctionFunction);
124
34
  }
125
35
    }
126
120
}
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
792
exsltFuncInit (xsltTransformContextPtr ctxt, const xmlChar *URI) {
143
792
    exsltFuncData *ret;
144
792
    xsltStylesheetPtr tmp;
145
792
    exsltFuncImportRegData ch;
146
792
    xmlHashTablePtr hash;
147
148
792
    ret = (exsltFuncData *) xmlMalloc (sizeof(exsltFuncData));
149
792
    if (ret == NULL) {
150
2
  xsltGenericError(xsltGenericErrorContext,
151
2
       "exsltFuncInit: not enough memory\n");
152
2
  return(NULL);
153
2
    }
154
790
    memset(ret, 0, sizeof(exsltFuncData));
155
156
790
    ret->result = NULL;
157
790
    ret->error = 0;
158
159
790
    ch.hash = (xmlHashTablePtr) xsltStyleGetExtData(ctxt->style, URI);
160
790
    ret->funcs = ch.hash;
161
790
    xmlHashScanFull(ch.hash, exsltFuncRegisterFunc, ctxt);
162
790
    tmp = ctxt->style;
163
790
    ch.ctxt = ctxt;
164
884
    while ((tmp=xsltNextImport(tmp))!=NULL) {
165
94
  hash = xsltGetExtInfo(tmp, URI);
166
94
  if (hash != NULL) {
167
54
      xmlHashScanFull(hash, exsltFuncRegisterImportFunc, &ch);
168
54
  }
169
94
    }
170
171
790
    return(ret);
172
792
}
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
792
       void *vdata) {
187
792
    exsltFuncData *data = (exsltFuncData *) vdata;
188
189
792
    if (data != NULL) {
190
790
        if (data->result != NULL)
191
0
            xmlXPathFreeObject(data->result);
192
790
        xmlFree(data);
193
790
    }
194
792
}
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
4.77k
        const xmlChar *URI ATTRIBUTE_UNUSED) {
209
4.77k
    return xmlHashCreate(1);
210
4.77k
}
211
212
static void
213
8.74k
exsltFuncFreeDataEntry(void *payload, const xmlChar *name ATTRIBUTE_UNUSED) {
214
8.74k
    xmlFree(payload);
215
8.74k
}
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
4.77k
      void *vdata) {
230
4.77k
    xmlHashTablePtr data = (xmlHashTablePtr) vdata;
231
4.77k
    xmlHashFree(data, exsltFuncFreeDataEntry);
232
4.77k
}
233
234
/**
235
 * exsltFuncNewFunctionData:
236
 *
237
 * Allocates an #exslFuncFunctionData object
238
 *
239
 * Returns the new structure
240
 */
241
static exsltFuncFunctionData *
242
16.9k
exsltFuncNewFunctionData (void) {
243
16.9k
    exsltFuncFunctionData *ret;
244
245
16.9k
    ret = (exsltFuncFunctionData *) xmlMalloc (sizeof(exsltFuncFunctionData));
246
16.9k
    if (ret == NULL) {
247
35
  xsltGenericError(xsltGenericErrorContext,
248
35
       "exsltFuncNewFunctionData: not enough memory\n");
249
35
  return (NULL);
250
35
    }
251
16.9k
    memset(ret, 0, sizeof(exsltFuncFunctionData));
252
253
16.9k
    ret->nargs = 0;
254
16.9k
    ret->content = NULL;
255
256
16.9k
    return(ret);
257
16.9k
}
258
259
/**
260
 * exsltFreeFuncResultPreComp:
261
 * @comp:  the #exsltFuncResultPreComp to free up
262
 *
263
 * Deallocates an #exsltFuncResultPreComp
264
 */
265
static void
266
9.42k
exsltFreeFuncResultPreComp (xsltElemPreCompPtr ecomp) {
267
9.42k
    exsltFuncResultPreComp *comp = (exsltFuncResultPreComp *) ecomp;
268
269
9.42k
    if (comp == NULL)
270
0
  return;
271
272
9.42k
    if (comp->select != NULL)
273
4.58k
  xmlXPathFreeCompExpr (comp->select);
274
9.42k
    if (comp->nsList != NULL)
275
9.41k
        xmlFree(comp->nsList);
276
9.42k
    xmlFree(comp);
277
9.42k
}
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
13.9k
exsltFuncFunctionFunction (xmlXPathParserContextPtr ctxt, int nargs) {
288
13.9k
    xmlXPathObjectPtr oldResult, ret;
289
13.9k
    exsltFuncData *data;
290
13.9k
    exsltFuncFunctionData *func;
291
13.9k
    xmlNodePtr paramNode, oldInsert, oldXPNode, fake;
292
13.9k
    int oldBase, newBase;
293
13.9k
    void *oldCtxtVar;
294
13.9k
    xsltStackElemPtr params = NULL, param;
295
13.9k
    xsltTransformContextPtr tctxt = xsltXPathGetTransformContext(ctxt);
296
13.9k
    int i;
297
13.9k
    xmlXPathObjectPtr *args = NULL;
298
299
    /*
300
     * retrieve func:function template
301
     */
302
13.9k
    data = (exsltFuncData *) xsltGetExtData (tctxt,
303
13.9k
               EXSLT_FUNCTIONS_NAMESPACE);
304
13.9k
    oldResult = data->result;
305
13.9k
    data->result = NULL;
306
307
13.9k
    func = (exsltFuncFunctionData*) xmlHashLookup2 (data->funcs,
308
13.9k
                ctxt->context->functionURI,
309
13.9k
                ctxt->context->function);
310
13.9k
    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
13.9k
    if (nargs > func->nargs) {
323
153
  xsltGenericError(xsltGenericErrorContext,
324
153
       "{%s}%s: called with too many arguments\n",
325
153
       ctxt->context->functionURI, ctxt->context->function);
326
153
  ctxt->error = XPATH_INVALID_ARITY;
327
153
  return;
328
153
    }
329
13.7k
    if (func->content != NULL) {
330
13.4k
  paramNode = func->content->prev;
331
13.4k
    }
332
298
    else
333
298
  paramNode = NULL;
334
13.7k
    if ((paramNode == NULL) && (func->nargs != 0)) {
335
297
  xsltGenericError(xsltGenericErrorContext,
336
297
       "exsltFuncFunctionFunction: nargs != 0 and "
337
297
       "param == NULL\n");
338
297
  return;
339
297
    }
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
13.4k
    if (tctxt->depth >= tctxt->maxTemplateDepth) {
347
56
        xsltTransformError(tctxt, NULL, NULL,
348
56
            "exsltFuncFunctionFunction: Potentially infinite recursion "
349
56
            "detected in function {%s}%s.\n",
350
56
            ctxt->context->functionURI, ctxt->context->function);
351
56
        tctxt->state = XSLT_STATE_STOPPED;
352
56
        return;
353
56
    }
354
13.4k
    tctxt->depth++;
355
356
    /* Evaluating templates can change the XPath context node. */
357
13.4k
    oldXPNode = tctxt->xpathCtxt->node;
358
359
13.4k
    fake = xmlNewDocNode(tctxt->output, NULL,
360
13.4k
       (const xmlChar *)"fake", NULL);
361
13.4k
    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
13.4k
    newBase = tctxt->varsNr;
386
    /* If there are any parameters */
387
13.4k
    if (paramNode != NULL) {
388
12.8k
        args = (xmlXPathObjectPtr *) xmlMalloc(sizeof(*args) * nargs);
389
12.8k
        if (args == NULL)
390
9
            goto error;
391
        /* Fetch the stored argument values from the caller */
392
26.0k
  for (i = nargs - 1; i >= 0; i--) {
393
13.1k
            args[i] = valuePop(ctxt);
394
13.1k
  }
395
396
  /*
397
   * Prepare to process params in reverse order.  First, go to
398
   * the beginning of the param chain.
399
   */
400
13.3k
  for (i = 1; i <= func->nargs; i++) {
401
13.3k
      if (paramNode->prev == NULL)
402
12.8k
          break;
403
500
      paramNode = paramNode->prev;
404
500
  }
405
  /*
406
   * i has total # params found, nargs is number which are present
407
   * as arguments from the caller
408
   * Calculate the number of un-set parameters
409
   */
410
26.2k
  for (i = 0; i < func->nargs; i++) {
411
13.3k
      param = xsltParseStylesheetCallerParam (tctxt, paramNode);
412
13.3k
            if (param == NULL) {
413
8
                xsltLocalVariablePop(tctxt, newBase, -2);
414
8
          xsltFreeStackElemList(params);
415
16
                for (; i < nargs; i++)
416
8
                    xmlXPathFreeObject(args[i]);
417
8
                goto error;
418
8
            }
419
13.3k
      if (i < nargs) { /* if parameter value set */
420
13.1k
    param->computed = 1;
421
13.1k
    if (param->value != NULL)
422
13.1k
        xmlXPathFreeObject(param->value);
423
13.1k
    param->value = args[i];
424
13.1k
      }
425
13.3k
      xsltLocalVariablePush(tctxt, param, -1);
426
13.3k
      param->next = params;
427
13.3k
      params = param;
428
13.3k
      paramNode = paramNode->next;
429
13.3k
  }
430
12.8k
    }
431
    /*
432
     * Actual processing. The context variable is cleared and restored
433
     * when func:result is evaluated.
434
     */
435
13.4k
    oldBase = tctxt->varsBase;
436
13.4k
    oldInsert = tctxt->insert;
437
13.4k
    oldCtxtVar = data->ctxtVar;
438
13.4k
    data->ctxtVar = tctxt->contextVariable;
439
13.4k
    tctxt->varsBase = newBase;
440
13.4k
    tctxt->insert = fake;
441
13.4k
    tctxt->contextVariable = NULL;
442
13.4k
    xsltApplyOneTemplate (tctxt, tctxt->node,
443
13.4k
        func->content, NULL, NULL);
444
13.4k
    xsltLocalVariablePop(tctxt, tctxt->varsBase, -2);
445
13.4k
    tctxt->insert = oldInsert;
446
13.4k
    tctxt->contextVariable = data->ctxtVar;
447
13.4k
    tctxt->varsBase = oldBase;  /* restore original scope */
448
13.4k
    data->ctxtVar = oldCtxtVar;
449
13.4k
    if (params != NULL)
450
12.8k
  xsltFreeStackElemList(params);
451
13.4k
    tctxt->xpathCtxt->node = oldXPNode;
452
453
13.4k
    if (data->error != 0)
454
5
        goto error;
455
456
13.4k
    if (data->result != NULL) {
457
5.77k
  ret = data->result;
458
        /*
459
        * IMPORTANT: This enables previously tree fragments marked as
460
        * being results of a function, to be garbage-collected after
461
        * the calling process exits.
462
        */
463
5.77k
        xsltFlagRVTs(tctxt, ret, XSLT_RVT_LOCAL);
464
5.77k
    } else
465
7.62k
  ret = xmlXPathNewCString("");
466
467
13.4k
    data->result = oldResult;
468
469
    /*
470
     * It is an error if the instantiation of the template results in
471
     * the generation of result nodes.
472
     */
473
13.4k
    if (fake->children != NULL) {
474
1.69k
  xsltGenericError(xsltGenericErrorContext,
475
1.69k
       "{%s}%s: cannot write to result tree while "
476
1.69k
       "executing a function\n",
477
1.69k
       ctxt->context->functionURI, ctxt->context->function);
478
1.69k
        xmlXPathFreeObject(ret);
479
1.69k
  goto error;
480
1.69k
    }
481
11.7k
    valuePush(ctxt, ret);
482
483
13.4k
error:
484
13.4k
    xmlFree(args);
485
13.4k
    xmlFreeNode(fake);
486
13.4k
    tctxt->depth--;
487
13.4k
}
488
489
490
static void
491
18.4k
exsltFuncFunctionComp (xsltStylesheetPtr style, xmlNodePtr inst) {
492
18.4k
    xmlChar *name, *prefix;
493
18.4k
    xmlNsPtr ns;
494
18.4k
    xmlHashTablePtr data;
495
18.4k
    exsltFuncFunctionData *func;
496
497
18.4k
    if ((style == NULL) || (inst == NULL) || (inst->type != XML_ELEMENT_NODE))
498
0
  return;
499
500
18.4k
    {
501
18.4k
  xmlChar *qname;
502
503
18.4k
  qname = xmlGetProp(inst, (const xmlChar *) "name");
504
18.4k
  name = xmlSplitQName2 (qname, &prefix);
505
18.4k
  xmlFree(qname);
506
18.4k
    }
507
18.4k
    if ((name == NULL) || (prefix == NULL)) {
508
1.09k
  xsltGenericError(xsltGenericErrorContext,
509
1.09k
       "func:function: not a QName\n");
510
1.09k
  if (name != NULL)
511
0
      xmlFree(name);
512
1.09k
  return;
513
1.09k
    }
514
    /* namespace lookup */
515
17.3k
    ns = xmlSearchNs (inst->doc, inst, prefix);
516
17.3k
    if (ns == NULL) {
517
460
  xsltGenericError(xsltGenericErrorContext,
518
460
       "func:function: undeclared prefix %s\n",
519
460
       prefix);
520
460
  xmlFree(name);
521
460
  xmlFree(prefix);
522
460
  return;
523
460
    }
524
16.8k
    xmlFree(prefix);
525
526
16.8k
    xsltParseTemplateContent(style, inst);
527
528
    /*
529
     * Create function data
530
     */
531
16.8k
    func = exsltFuncNewFunctionData();
532
16.8k
    if (func == NULL) {
533
25
        xmlFree(name);
534
25
        return;
535
25
    }
536
16.8k
    func->content = inst->children;
537
20.7k
    while (IS_XSLT_ELEM(func->content) &&
538
20.7k
     IS_XSLT_NAME(func->content, "param")) {
539
3.92k
  func->content = func->content->next;
540
3.92k
  func->nargs++;
541
3.92k
    }
542
543
    /*
544
     * Register the function data such that it can be retrieved
545
     * by exslFuncFunctionFunction
546
     */
547
#ifdef XSLT_REFACTORED
548
    /*
549
    * Ensure that the hash table will be stored in the *current*
550
    * stylesheet level in order to correctly evaluate the
551
    * import precedence.
552
    */
553
    data = (xmlHashTablePtr)
554
  xsltStyleStylesheetLevelGetExtData(style,
555
      EXSLT_FUNCTIONS_NAMESPACE);
556
#else
557
16.8k
    data = (xmlHashTablePtr)
558
16.8k
  xsltStyleGetExtData (style, EXSLT_FUNCTIONS_NAMESPACE);
559
16.8k
#endif
560
16.8k
    if (data == NULL) {
561
3
  xsltGenericError(xsltGenericErrorContext,
562
3
       "exsltFuncFunctionComp: no stylesheet data\n");
563
3
  xmlFree(name);
564
3
        xmlFree(func);
565
3
  return;
566
3
    }
567
568
16.8k
    if (xmlHashAddEntry2 (data, ns->href, name, func) < 0) {
569
8.15k
  xsltTransformError(NULL, style, inst,
570
8.15k
      "Failed to register function {%s}%s\n",
571
8.15k
       ns->href, name);
572
8.15k
  style->errors++;
573
8.15k
        xmlFree(func);
574
8.71k
    } else {
575
8.71k
  xsltGenericDebug(xsltGenericDebugContext,
576
8.71k
       "exsltFuncFunctionComp: register {%s}%s\n",
577
8.71k
       ns->href, name);
578
8.71k
    }
579
16.8k
    xmlFree(name);
580
16.8k
}
581
582
static xsltElemPreCompPtr
583
exsltFuncResultComp (xsltStylesheetPtr style, xmlNodePtr inst,
584
10.0k
         xsltTransformFunction function) {
585
10.0k
    xmlNodePtr test;
586
10.0k
    xmlChar *sel;
587
10.0k
    exsltFuncResultPreComp *ret;
588
589
10.0k
    if ((style == NULL) || (inst == NULL) || (inst->type != XML_ELEMENT_NODE))
590
0
        return (NULL);
591
592
    /*
593
     * "Validity" checking
594
     */
595
    /* it is an error to have any following sibling elements aside
596
     * from the xsl:fallback element.
597
     */
598
11.0k
    for (test = inst->next; test != NULL; test = test->next) {
599
1.42k
  if (test->type != XML_ELEMENT_NODE)
600
992
      continue;
601
431
  if (IS_XSLT_ELEM(test) && IS_XSLT_NAME(test, "fallback"))
602
0
      continue;
603
431
  xsltGenericError(xsltGenericErrorContext,
604
431
       "exsltFuncResultElem: only xsl:fallback is "
605
431
       "allowed to follow func:result\n");
606
431
  style->errors++;
607
431
  return (NULL);
608
431
    }
609
    /* it is an error for a func:result element to not be a descendant
610
     * of func:function.
611
     * it is an error if a func:result occurs within a func:result
612
     * element.
613
     * it is an error if instanciating the content of a variable
614
     * binding element (i.e. xsl:variable, xsl:param) results in the
615
     * instanciation of a func:result element.
616
     */
617
14.0k
    for (test = inst->parent; test != NULL; test = test->parent) {
618
14.0k
  if (IS_XSLT_ELEM(test) &&
619
14.0k
      IS_XSLT_NAME(test, "stylesheet")) {
620
72
      xsltGenericError(xsltGenericErrorContext,
621
72
           "func:result element not a descendant "
622
72
           "of a func:function\n");
623
72
      style->errors++;
624
72
      return (NULL);
625
72
  }
626
13.9k
  if ((test->ns != NULL) &&
627
13.9k
      (xmlStrEqual(test->ns->href, EXSLT_FUNCTIONS_NAMESPACE))) {
628
9.53k
      if (xmlStrEqual(test->name, (const xmlChar *) "function")) {
629
9.53k
    break;
630
9.53k
      }
631
0
      if (xmlStrEqual(test->name, (const xmlChar *) "result")) {
632
0
    xsltGenericError(xsltGenericErrorContext,
633
0
         "func:result element not allowed within"
634
0
         " another func:result element\n");
635
0
          style->errors++;
636
0
    return (NULL);
637
0
      }
638
0
  }
639
4.45k
  if (IS_XSLT_ELEM(test) &&
640
4.45k
      (IS_XSLT_NAME(test, "variable") ||
641
4.41k
       IS_XSLT_NAME(test, "param"))) {
642
0
      xsltGenericError(xsltGenericErrorContext,
643
0
           "func:result element not allowed within"
644
0
           " a variable binding element\n");
645
0
            style->errors++;
646
0
      return (NULL);
647
0
  }
648
4.45k
    }
649
650
    /*
651
     * Precomputation
652
     */
653
9.53k
    ret = (exsltFuncResultPreComp *)
654
9.53k
  xmlMalloc (sizeof(exsltFuncResultPreComp));
655
9.53k
    if (ret == NULL) {
656
117
  xsltPrintErrorContext(NULL, NULL, NULL);
657
117
        xsltGenericError(xsltGenericErrorContext,
658
117
                         "exsltFuncResultComp : malloc failed\n");
659
117
        style->errors++;
660
117
        return (NULL);
661
117
    }
662
9.42k
    memset(ret, 0, sizeof(exsltFuncResultPreComp));
663
664
9.42k
    xsltInitElemPreComp ((xsltElemPreCompPtr) ret, style, inst, function,
665
9.42k
     exsltFreeFuncResultPreComp);
666
9.42k
    ret->select = NULL;
667
668
    /*
669
     * Precompute the select attribute
670
     */
671
9.42k
    sel = xmlGetNsProp(inst, (const xmlChar *) "select", NULL);
672
9.42k
    if (sel != NULL) {
673
5.82k
  ret->select = xsltXPathCompileFlags(style, sel, 0);
674
5.82k
  xmlFree(sel);
675
5.82k
    }
676
    /*
677
     * Precompute the namespace list
678
     */
679
9.42k
    ret->nsList = xmlGetNsList(inst->doc, inst);
680
9.42k
    if (ret->nsList != NULL) {
681
9.41k
        int i = 0;
682
60.4k
        while (ret->nsList[i] != NULL)
683
50.9k
      i++;
684
9.41k
  ret->nsNr = i;
685
9.41k
    }
686
9.42k
    return ((xsltElemPreCompPtr) ret);
687
9.53k
}
688
689
static void
690
exsltFuncResultElem (xsltTransformContextPtr ctxt,
691
               xmlNodePtr node ATTRIBUTE_UNUSED, xmlNodePtr inst,
692
10.9k
         xsltElemPreCompPtr ecomp) {
693
10.9k
    exsltFuncResultPreComp *comp = (exsltFuncResultPreComp *) ecomp;
694
10.9k
    exsltFuncData *data;
695
10.9k
    xmlXPathObjectPtr ret;
696
697
698
    /* It is an error if instantiating the content of the
699
     * func:function element results in the instantiation of more than
700
     * one func:result elements.
701
     */
702
10.9k
    data = (exsltFuncData *) xsltGetExtData (ctxt, EXSLT_FUNCTIONS_NAMESPACE);
703
10.9k
    if (data == NULL) {
704
0
  xsltGenericError(xsltGenericErrorContext,
705
0
       "exsltFuncReturnElem: data == NULL\n");
706
0
  return;
707
0
    }
708
10.9k
    if (data->result != NULL) {
709
0
  xsltGenericError(xsltGenericErrorContext,
710
0
       "func:result already instanciated\n");
711
0
  data->error = 1;
712
0
  return;
713
0
    }
714
    /*
715
     * Restore context variable, so that it will receive the function
716
     * result RVTs.
717
     */
718
10.9k
    ctxt->contextVariable = data->ctxtVar;
719
    /*
720
     * Processing
721
     */
722
10.9k
    if (comp->select != NULL) {
723
9.66k
  xmlNsPtr *oldXPNsList;
724
9.66k
  int oldXPNsNr;
725
9.66k
  xmlNodePtr oldXPContextNode;
726
  /* If the func:result element has a select attribute, then the
727
   * value of the attribute must be an expression and the
728
   * returned value is the object that results from evaluating
729
   * the expression. In this case, the content must be empty.
730
   */
731
9.66k
  if (inst->children != NULL) {
732
0
      xsltGenericError(xsltGenericErrorContext,
733
0
           "func:result content must be empty if"
734
0
           " the function has a select attribute\n");
735
0
      data->error = 1;
736
0
      return;
737
0
  }
738
9.66k
  oldXPNsList = ctxt->xpathCtxt->namespaces;
739
9.66k
  oldXPNsNr = ctxt->xpathCtxt->nsNr;
740
9.66k
  oldXPContextNode = ctxt->xpathCtxt->node;
741
742
9.66k
  ctxt->xpathCtxt->namespaces = comp->nsList;
743
9.66k
  ctxt->xpathCtxt->nsNr = comp->nsNr;
744
9.66k
        ctxt->xpathCtxt->node = ctxt->node;
745
746
9.66k
  ret = xmlXPathCompiledEval(comp->select, ctxt->xpathCtxt);
747
748
9.66k
  ctxt->xpathCtxt->node = oldXPContextNode;
749
9.66k
  ctxt->xpathCtxt->nsNr = oldXPNsNr;
750
9.66k
  ctxt->xpathCtxt->namespaces = oldXPNsList;
751
752
9.66k
  if (ret == NULL) {
753
5.21k
      xsltGenericError(xsltGenericErrorContext,
754
5.21k
           "exsltFuncResultElem: ret == NULL\n");
755
5.21k
      return;
756
5.21k
  }
757
  /*
758
  * Mark it as a function result in order to avoid garbage
759
  * collecting of tree fragments before the function exits.
760
  */
761
4.45k
  xsltFlagRVTs(ctxt, ret, XSLT_RVT_FUNC_RESULT);
762
4.45k
    } else if (inst->children != NULL) {
763
  /* If the func:result element does not have a select attribute
764
   * and has non-empty content (i.e. the func:result element has
765
   * one or more child nodes), then the content of the
766
   * func:result element specifies the value.
767
   */
768
26
  xmlNodePtr oldInsert;
769
26
  xmlDocPtr container;
770
771
26
  container = xsltCreateRVT(ctxt);
772
26
  if (container == NULL) {
773
1
      xsltGenericError(xsltGenericErrorContext,
774
1
           "exsltFuncResultElem: out of memory\n");
775
1
      data->error = 1;
776
1
      return;
777
1
  }
778
        /* Mark as function result. */
779
25
        xsltRegisterLocalRVT(ctxt, container);
780
25
        container->compression = XSLT_RVT_FUNC_RESULT;
781
782
25
  oldInsert = ctxt->insert;
783
25
  ctxt->insert = (xmlNodePtr) container;
784
25
  xsltApplyOneTemplate (ctxt, ctxt->node,
785
25
            inst->children, NULL, NULL);
786
25
  ctxt->insert = oldInsert;
787
788
25
  ret = xmlXPathNewValueTree((xmlNodePtr) container);
789
25
  if (ret == NULL) {
790
2
      xsltGenericError(xsltGenericErrorContext,
791
2
           "exsltFuncResultElem: ret == NULL\n");
792
2
      data->error = 1;
793
23
  } else {
794
23
      ret->boolval = 0; /* Freeing is not handled there anymore */
795
23
  }
796
1.29k
    } else {
797
  /* If the func:result element has empty content and does not
798
   * have a select attribute, then the returned value is an
799
   * empty string.
800
   */
801
1.29k
  ret = xmlXPathNewCString("");
802
1.29k
    }
803
5.77k
    data->result = ret;
804
5.77k
}
805
806
/**
807
 * exsltFuncRegister:
808
 *
809
 * Registers the EXSLT - Functions module
810
 */
811
void
812
4
exsltFuncRegister (void) {
813
4
    xsltRegisterExtModuleFull (EXSLT_FUNCTIONS_NAMESPACE,
814
4
           exsltFuncInit,
815
4
           exsltFuncShutdown,
816
4
           exsltFuncStyleInit,
817
4
           exsltFuncStyleShutdown);
818
819
4
    xsltRegisterExtModuleTopLevel ((const xmlChar *) "function",
820
4
           EXSLT_FUNCTIONS_NAMESPACE,
821
4
           exsltFuncFunctionComp);
822
4
    xsltRegisterExtModuleElement ((const xmlChar *) "result",
823
4
        EXSLT_FUNCTIONS_NAMESPACE,
824
4
        exsltFuncResultComp,
825
4
        exsltFuncResultElem);
826
4
}