Coverage Report

Created: 2023-06-07 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
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
        args = (xmlXPathObjectPtr *) xmlMalloc(sizeof(*args) * nargs);
389
0
        if (args == NULL)
390
0
            goto error;
391
        /* Fetch the stored argument values from the caller */
392
0
  for (i = nargs - 1; i >= 0; i--) {
393
0
            args[i] = valuePop(ctxt);
394
0
  }
395
396
  /*
397
   * Prepare to process params in reverse order.  First, go to
398
   * the beginning of the param chain.
399
   */
400
0
  for (i = 1; i <= func->nargs; i++) {
401
0
      if (paramNode->prev == NULL)
402
0
          break;
403
0
      paramNode = paramNode->prev;
404
0
  }
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
0
  for (i = 0; i < func->nargs; i++) {
411
0
      param = xsltParseStylesheetCallerParam (tctxt, paramNode);
412
0
            if (param == NULL) {
413
0
                xsltLocalVariablePop(tctxt, newBase, -2);
414
0
          xsltFreeStackElemList(params);
415
0
                for (; i < nargs; i++)
416
0
                    xmlXPathFreeObject(args[i]);
417
0
                goto error;
418
0
            }
419
0
      if (i < nargs) { /* if parameter value set */
420
0
    param->computed = 1;
421
0
    if (param->value != NULL)
422
0
        xmlXPathFreeObject(param->value);
423
0
    param->value = args[i];
424
0
      }
425
0
      xsltLocalVariablePush(tctxt, param, -1);
426
0
      param->next = params;
427
0
      params = param;
428
0
      paramNode = paramNode->next;
429
0
  }
430
0
    }
431
    /*
432
     * Actual processing. The context variable is cleared and restored
433
     * when func:result is evaluated.
434
     */
435
0
    oldBase = tctxt->varsBase;
436
0
    oldInsert = tctxt->insert;
437
0
    oldCtxtVar = data->ctxtVar;
438
0
    data->ctxtVar = tctxt->contextVariable;
439
0
    tctxt->varsBase = newBase;
440
0
    tctxt->insert = fake;
441
0
    tctxt->contextVariable = NULL;
442
0
    xsltApplyOneTemplate (tctxt, tctxt->node,
443
0
        func->content, NULL, NULL);
444
0
    xsltLocalVariablePop(tctxt, tctxt->varsBase, -2);
445
0
    tctxt->insert = oldInsert;
446
0
    tctxt->contextVariable = data->ctxtVar;
447
0
    tctxt->varsBase = oldBase;  /* restore original scope */
448
0
    data->ctxtVar = oldCtxtVar;
449
0
    if (params != NULL)
450
0
  xsltFreeStackElemList(params);
451
0
    tctxt->xpathCtxt->node = oldXPNode;
452
453
0
    if (data->error != 0)
454
0
        goto error;
455
456
0
    if (data->result != NULL) {
457
0
  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
0
        xsltFlagRVTs(tctxt, ret, XSLT_RVT_LOCAL);
464
0
    } else
465
0
  ret = xmlXPathNewCString("");
466
467
0
    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
0
    if (fake->children != NULL) {
474
0
  xsltGenericError(xsltGenericErrorContext,
475
0
       "{%s}%s: cannot write to result tree while "
476
0
       "executing a function\n",
477
0
       ctxt->context->functionURI, ctxt->context->function);
478
0
        xmlXPathFreeObject(ret);
479
0
  goto error;
480
0
    }
481
0
    valuePush(ctxt, ret);
482
483
0
error:
484
0
    xmlFree(args);
485
0
    xmlFreeNode(fake);
486
0
    tctxt->depth--;
487
0
}
488
489
490
static void
491
0
exsltFuncFunctionComp (xsltStylesheetPtr style, xmlNodePtr inst) {
492
0
    xmlChar *name, *prefix;
493
0
    xmlNsPtr ns;
494
0
    xmlHashTablePtr data;
495
0
    exsltFuncFunctionData *func;
496
497
0
    if ((style == NULL) || (inst == NULL) || (inst->type != XML_ELEMENT_NODE))
498
0
  return;
499
500
0
    {
501
0
  xmlChar *qname;
502
503
0
  qname = xmlGetProp(inst, (const xmlChar *) "name");
504
0
  name = xmlSplitQName2 (qname, &prefix);
505
0
  xmlFree(qname);
506
0
    }
507
0
    if ((name == NULL) || (prefix == NULL)) {
508
0
  xsltGenericError(xsltGenericErrorContext,
509
0
       "func:function: not a QName\n");
510
0
  if (name != NULL)
511
0
      xmlFree(name);
512
0
  return;
513
0
    }
514
    /* namespace lookup */
515
0
    ns = xmlSearchNs (inst->doc, inst, prefix);
516
0
    if (ns == NULL) {
517
0
  xsltGenericError(xsltGenericErrorContext,
518
0
       "func:function: undeclared prefix %s\n",
519
0
       prefix);
520
0
  xmlFree(name);
521
0
  xmlFree(prefix);
522
0
  return;
523
0
    }
524
0
    xmlFree(prefix);
525
526
0
    xsltParseTemplateContent(style, inst);
527
528
    /*
529
     * Create function data
530
     */
531
0
    func = exsltFuncNewFunctionData();
532
0
    if (func == NULL) {
533
0
        xmlFree(name);
534
0
        return;
535
0
    }
536
0
    func->content = inst->children;
537
0
    while (IS_XSLT_ELEM(func->content) &&
538
0
     IS_XSLT_NAME(func->content, "param")) {
539
0
  func->content = func->content->next;
540
0
  func->nargs++;
541
0
    }
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
0
    data = (xmlHashTablePtr)
558
0
  xsltStyleGetExtData (style, EXSLT_FUNCTIONS_NAMESPACE);
559
0
#endif
560
0
    if (data == NULL) {
561
0
  xsltGenericError(xsltGenericErrorContext,
562
0
       "exsltFuncFunctionComp: no stylesheet data\n");
563
0
  xmlFree(name);
564
0
        xmlFree(func);
565
0
  return;
566
0
    }
567
568
0
    if (xmlHashAddEntry2 (data, ns->href, name, func) < 0) {
569
0
  xsltTransformError(NULL, style, inst,
570
0
      "Failed to register function {%s}%s\n",
571
0
       ns->href, name);
572
0
  style->errors++;
573
0
        xmlFree(func);
574
0
    } else {
575
0
  xsltGenericDebug(xsltGenericDebugContext,
576
0
       "exsltFuncFunctionComp: register {%s}%s\n",
577
0
       ns->href, name);
578
0
    }
579
0
    xmlFree(name);
580
0
}
581
582
static xsltElemPreCompPtr
583
exsltFuncResultComp (xsltStylesheetPtr style, xmlNodePtr inst,
584
0
         xsltTransformFunction function) {
585
0
    xmlNodePtr test;
586
0
    xmlChar *sel;
587
0
    exsltFuncResultPreComp *ret;
588
589
0
    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
0
    for (test = inst->next; test != NULL; test = test->next) {
599
0
  if (test->type != XML_ELEMENT_NODE)
600
0
      continue;
601
0
  if (IS_XSLT_ELEM(test) && IS_XSLT_NAME(test, "fallback"))
602
0
      continue;
603
0
  xsltGenericError(xsltGenericErrorContext,
604
0
       "exsltFuncResultElem: only xsl:fallback is "
605
0
       "allowed to follow func:result\n");
606
0
  style->errors++;
607
0
  return (NULL);
608
0
    }
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
0
    for (test = inst->parent; test != NULL; test = test->parent) {
618
0
  if (IS_XSLT_ELEM(test) &&
619
0
      IS_XSLT_NAME(test, "stylesheet")) {
620
0
      xsltGenericError(xsltGenericErrorContext,
621
0
           "func:result element not a descendant "
622
0
           "of a func:function\n");
623
0
      style->errors++;
624
0
      return (NULL);
625
0
  }
626
0
  if ((test->ns != NULL) &&
627
0
      (xmlStrEqual(test->ns->href, EXSLT_FUNCTIONS_NAMESPACE))) {
628
0
      if (xmlStrEqual(test->name, (const xmlChar *) "function")) {
629
0
    break;
630
0
      }
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
0
  if (IS_XSLT_ELEM(test) &&
640
0
      (IS_XSLT_NAME(test, "variable") ||
641
0
       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
0
    }
649
650
    /*
651
     * Precomputation
652
     */
653
0
    ret = (exsltFuncResultPreComp *)
654
0
  xmlMalloc (sizeof(exsltFuncResultPreComp));
655
0
    if (ret == NULL) {
656
0
  xsltPrintErrorContext(NULL, NULL, NULL);
657
0
        xsltGenericError(xsltGenericErrorContext,
658
0
                         "exsltFuncResultComp : malloc failed\n");
659
0
        style->errors++;
660
0
        return (NULL);
661
0
    }
662
0
    memset(ret, 0, sizeof(exsltFuncResultPreComp));
663
664
0
    xsltInitElemPreComp ((xsltElemPreCompPtr) ret, style, inst, function,
665
0
     exsltFreeFuncResultPreComp);
666
0
    ret->select = NULL;
667
668
    /*
669
     * Precompute the select attribute
670
     */
671
0
    sel = xmlGetNsProp(inst, (const xmlChar *) "select", NULL);
672
0
    if (sel != NULL) {
673
0
  ret->select = xsltXPathCompileFlags(style, sel, 0);
674
0
  xmlFree(sel);
675
0
    }
676
    /*
677
     * Precompute the namespace list
678
     */
679
0
    ret->nsList = xmlGetNsList(inst->doc, inst);
680
0
    if (ret->nsList != NULL) {
681
0
        int i = 0;
682
0
        while (ret->nsList[i] != NULL)
683
0
      i++;
684
0
  ret->nsNr = i;
685
0
    }
686
0
    return ((xsltElemPreCompPtr) ret);
687
0
}
688
689
static void
690
exsltFuncResultElem (xsltTransformContextPtr ctxt,
691
               xmlNodePtr node ATTRIBUTE_UNUSED, xmlNodePtr inst,
692
0
         xsltElemPreCompPtr ecomp) {
693
0
    exsltFuncResultPreComp *comp = (exsltFuncResultPreComp *) ecomp;
694
0
    exsltFuncData *data;
695
0
    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
0
    data = (exsltFuncData *) xsltGetExtData (ctxt, EXSLT_FUNCTIONS_NAMESPACE);
703
0
    if (data == NULL) {
704
0
  xsltGenericError(xsltGenericErrorContext,
705
0
       "exsltFuncReturnElem: data == NULL\n");
706
0
  return;
707
0
    }
708
0
    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
0
    ctxt->contextVariable = data->ctxtVar;
719
    /*
720
     * Processing
721
     */
722
0
    if (comp->select != NULL) {
723
0
  xmlNsPtr *oldXPNsList;
724
0
  int oldXPNsNr;
725
0
  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
0
  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
0
  oldXPNsList = ctxt->xpathCtxt->namespaces;
739
0
  oldXPNsNr = ctxt->xpathCtxt->nsNr;
740
0
  oldXPContextNode = ctxt->xpathCtxt->node;
741
742
0
  ctxt->xpathCtxt->namespaces = comp->nsList;
743
0
  ctxt->xpathCtxt->nsNr = comp->nsNr;
744
0
        ctxt->xpathCtxt->node = ctxt->node;
745
746
0
  ret = xmlXPathCompiledEval(comp->select, ctxt->xpathCtxt);
747
748
0
  ctxt->xpathCtxt->node = oldXPContextNode;
749
0
  ctxt->xpathCtxt->nsNr = oldXPNsNr;
750
0
  ctxt->xpathCtxt->namespaces = oldXPNsList;
751
752
0
  if (ret == NULL) {
753
0
      xsltGenericError(xsltGenericErrorContext,
754
0
           "exsltFuncResultElem: ret == NULL\n");
755
0
      return;
756
0
  }
757
  /*
758
  * Mark it as a function result in order to avoid garbage
759
  * collecting of tree fragments before the function exits.
760
  */
761
0
  xsltFlagRVTs(ctxt, ret, XSLT_RVT_FUNC_RESULT);
762
0
    } 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
0
  xmlNodePtr oldInsert;
769
0
  xmlDocPtr container;
770
771
0
  container = xsltCreateRVT(ctxt);
772
0
  if (container == NULL) {
773
0
      xsltGenericError(xsltGenericErrorContext,
774
0
           "exsltFuncResultElem: out of memory\n");
775
0
      data->error = 1;
776
0
      return;
777
0
  }
778
        /* Mark as function result. */
779
0
        xsltRegisterLocalRVT(ctxt, container);
780
0
        container->compression = XSLT_RVT_FUNC_RESULT;
781
782
0
  oldInsert = ctxt->insert;
783
0
  ctxt->insert = (xmlNodePtr) container;
784
0
  xsltApplyOneTemplate (ctxt, ctxt->node,
785
0
            inst->children, NULL, NULL);
786
0
  ctxt->insert = oldInsert;
787
788
0
  ret = xmlXPathNewValueTree((xmlNodePtr) container);
789
0
  if (ret == NULL) {
790
0
      xsltGenericError(xsltGenericErrorContext,
791
0
           "exsltFuncResultElem: ret == NULL\n");
792
0
      data->error = 1;
793
0
  } else {
794
0
      ret->boolval = 0; /* Freeing is not handled there anymore */
795
0
  }
796
0
    } 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
0
  ret = xmlXPathNewCString("");
802
0
    }
803
0
    data->result = ret;
804
0
}
805
806
/**
807
 * exsltFuncRegister:
808
 *
809
 * Registers the EXSLT - Functions module
810
 */
811
void
812
2
exsltFuncRegister (void) {
813
2
    xsltRegisterExtModuleFull (EXSLT_FUNCTIONS_NAMESPACE,
814
2
           exsltFuncInit,
815
2
           exsltFuncShutdown,
816
2
           exsltFuncStyleInit,
817
2
           exsltFuncStyleShutdown);
818
819
2
    xsltRegisterExtModuleTopLevel ((const xmlChar *) "function",
820
2
           EXSLT_FUNCTIONS_NAMESPACE,
821
2
           exsltFuncFunctionComp);
822
2
    xsltRegisterExtModuleElement ((const xmlChar *) "result",
823
2
        EXSLT_FUNCTIONS_NAMESPACE,
824
2
        exsltFuncResultComp,
825
2
        exsltFuncResultElem);
826
2
}