Coverage Report

Created: 2025-03-12 04:17

/src/libxslt/libexslt/dynamic.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * dynamic.c: Implementation of the EXSLT -- Dynamic module
3
 *
4
 * References:
5
 *   http://www.exslt.org/dyn/dyn.html
6
 *
7
 * See Copyright for the status of this software.
8
 *
9
 * Authors:
10
 *   Mark Vakoc <mark_vakoc@jdedwards.com>
11
 *   Thomas Broyer <tbroyer@ltgt.net>
12
 *
13
 * TODO:
14
 * elements:
15
 * functions:
16
 *    min
17
 *    max
18
 *    sum
19
 *    map
20
 *    closure
21
 */
22
23
#define IN_LIBEXSLT
24
#include "libexslt/libexslt.h"
25
26
#include <libxml/tree.h>
27
#include <libxml/xpath.h>
28
#include <libxml/xpathInternals.h>
29
30
#include <libxslt/xsltutils.h>
31
#include <libxslt/xsltInternals.h>
32
#include <libxslt/extensions.h>
33
34
#include "exslt.h"
35
36
/**
37
 * exsltDynEvaluateFunction:
38
 * @ctxt:  an XPath parser context
39
 * @nargs:  the number of arguments
40
 *
41
 * Evaluates the string as an XPath expression and returns the result
42
 * value, which may be a boolean, number, string, node set, result tree
43
 * fragment or external object.
44
 */
45
46
static void
47
5
exsltDynEvaluateFunction(xmlXPathParserContextPtr ctxt, int nargs) {
48
5
  xmlChar *str = NULL;
49
5
  xmlXPathObjectPtr ret = NULL;
50
51
5
  if (ctxt == NULL)
52
0
    return;
53
5
  if (nargs != 1) {
54
0
    xsltPrintErrorContext(xsltXPathGetTransformContext(ctxt), NULL, NULL);
55
0
        xsltGenericError(xsltGenericErrorContext,
56
0
      "dyn:evalute() : invalid number of args %d\n", nargs);
57
0
    ctxt->error = XPATH_INVALID_ARITY;
58
0
    return;
59
0
  }
60
5
  str = xmlXPathPopString(ctxt);
61
  /* return an empty node-set if an empty string is passed in */
62
5
  if (!str||!xmlStrlen(str)) {
63
0
    if (str) xmlFree(str);
64
0
    valuePush(ctxt,xmlXPathNewNodeSet(NULL));
65
0
    return;
66
0
  }
67
5
  ret = xmlXPathEval(str,ctxt->context);
68
5
  if (ret)
69
5
    valuePush(ctxt,ret);
70
0
  else {
71
0
    xsltGenericError(xsltGenericErrorContext,
72
0
      "dyn:evaluate() : unable to evaluate expression '%s'\n",str);
73
0
    valuePush(ctxt,xmlXPathNewNodeSet(NULL));
74
0
  }
75
5
  xmlFree(str);
76
5
  return;
77
5
}
78
79
/**
80
 * exsltDynMapFunction:
81
 * @ctxt:  an XPath parser context
82
 * @nargs:  the number of arguments
83
 *
84
 * Evaluates the string as an XPath expression and returns the result
85
 * value, which may be a boolean, number, string, node set, result tree
86
 * fragment or external object.
87
 */
88
89
static void
90
exsltDynMapFunction(xmlXPathParserContextPtr ctxt, int nargs)
91
5
{
92
5
    xmlChar *str = NULL;
93
5
    xmlNodeSetPtr nodeset = NULL;
94
5
    xsltTransformContextPtr tctxt;
95
5
    xmlXPathCompExprPtr comp = NULL;
96
5
    xmlXPathObjectPtr ret = NULL;
97
5
    xmlDocPtr oldDoc, container = NULL;
98
5
    xmlNodePtr oldNode;
99
5
    int oldContextSize;
100
5
    int oldProximityPosition;
101
5
    int i, j;
102
103
104
5
    if (nargs != 2) {
105
0
        xmlXPathSetArityError(ctxt);
106
0
        return;
107
0
    }
108
5
    str = xmlXPathPopString(ctxt);
109
5
    if (xmlXPathCheckError(ctxt))
110
0
        goto cleanup;
111
112
5
    nodeset = xmlXPathPopNodeSet(ctxt);
113
5
    if (xmlXPathCheckError(ctxt))
114
0
        goto cleanup;
115
116
5
    ret = xmlXPathNewNodeSet(NULL);
117
5
    if (ret == NULL) {
118
0
        xsltGenericError(xsltGenericErrorContext,
119
0
                         "exsltDynMapFunction: ret == NULL\n");
120
0
        goto cleanup;
121
0
    }
122
123
5
    tctxt = xsltXPathGetTransformContext(ctxt);
124
5
    if (tctxt == NULL) {
125
0
  xsltTransformError(xsltXPathGetTransformContext(ctxt), NULL, NULL,
126
0
        "dyn:map : internal error tctxt == NULL\n");
127
0
  goto cleanup;
128
0
    }
129
130
5
    if (str == NULL || !xmlStrlen(str) ||
131
5
        !(comp = xmlXPathCtxtCompile(tctxt->xpathCtxt, str)))
132
0
        goto cleanup;
133
134
5
    oldDoc = ctxt->context->doc;
135
5
    oldNode = ctxt->context->node;
136
5
    oldContextSize = ctxt->context->contextSize;
137
5
    oldProximityPosition = ctxt->context->proximityPosition;
138
139
        /**
140
   * since we really don't know we're going to be adding node(s)
141
   * down the road we create the RVT regardless
142
   */
143
5
    container = xsltCreateRVT(tctxt);
144
5
    if (container == NULL) {
145
0
  xsltTransformError(tctxt, NULL, NULL,
146
0
        "dyn:map : internal error container == NULL\n");
147
0
  goto cleanup;
148
0
    }
149
5
    xsltRegisterLocalRVT(tctxt, container);
150
5
    if (nodeset && nodeset->nodeNr > 0) {
151
5
        xmlXPathNodeSetSort(nodeset);
152
5
        ctxt->context->contextSize = nodeset->nodeNr;
153
5
        ctxt->context->proximityPosition = 0;
154
65
        for (i = 0; i < nodeset->nodeNr; i++) {
155
60
            xmlXPathObjectPtr subResult = NULL;
156
60
            xmlNodePtr cur = nodeset->nodeTab[i];
157
158
60
            ctxt->context->proximityPosition++;
159
60
            ctxt->context->node = cur;
160
161
60
            if (cur->type == XML_NAMESPACE_DECL) {
162
                /*
163
                * The XPath module sets the owner element of a ns-node on
164
                * the ns->next field.
165
                */
166
0
                cur = (xmlNodePtr) ((xmlNsPtr) cur)->next;
167
0
                if ((cur == NULL) || (cur->type != XML_ELEMENT_NODE)) {
168
0
                    xsltGenericError(xsltGenericErrorContext,
169
0
                        "Internal error in exsltDynMapFunction: "
170
0
                        "Cannot retrieve the doc of a namespace node.\n");
171
0
                    continue;
172
0
                }
173
0
                ctxt->context->doc = cur->doc;
174
60
            } else {
175
60
                ctxt->context->doc = cur->doc;
176
60
            }
177
178
60
            subResult = xmlXPathCompiledEval(comp, ctxt->context);
179
60
            if (subResult != NULL) {
180
60
                switch (subResult->type) {
181
60
                    case XPATH_NODESET:
182
60
                        if (subResult->nodesetval != NULL)
183
120
                            for (j = 0; j < subResult->nodesetval->nodeNr;
184
60
                                 j++)
185
60
                                xmlXPathNodeSetAdd(ret->nodesetval,
186
60
                                                   subResult->nodesetval->
187
60
                                                   nodeTab[j]);
188
60
                        break;
189
0
                    case XPATH_BOOLEAN:
190
0
                        if (container != NULL) {
191
0
                            xmlNodePtr newChildNode =
192
0
                                xmlNewTextChild((xmlNodePtr) container, NULL,
193
0
                                                BAD_CAST "boolean",
194
0
                                                BAD_CAST (subResult->
195
0
                                                boolval ? "true" : ""));
196
0
                            if (newChildNode != NULL) {
197
0
                                newChildNode->ns =
198
0
                                    xmlNewNs(newChildNode,
199
0
                                             BAD_CAST
200
0
                                             "http://exslt.org/common",
201
0
                                             BAD_CAST "exsl");
202
0
                                xmlXPathNodeSetAddUnique(ret->nodesetval,
203
0
                                                         newChildNode);
204
0
                            }
205
0
                        }
206
0
                        break;
207
0
                    case XPATH_NUMBER:
208
0
                        if (container != NULL) {
209
0
                            xmlChar *val =
210
0
                                xmlXPathCastNumberToString(subResult->
211
0
                                                           floatval);
212
0
                            xmlNodePtr newChildNode =
213
0
                                xmlNewTextChild((xmlNodePtr) container, NULL,
214
0
                                                BAD_CAST "number", val);
215
0
                            if (val != NULL)
216
0
                                xmlFree(val);
217
218
0
                            if (newChildNode != NULL) {
219
0
                                newChildNode->ns =
220
0
                                    xmlNewNs(newChildNode,
221
0
                                             BAD_CAST
222
0
                                             "http://exslt.org/common",
223
0
                                             BAD_CAST "exsl");
224
0
                                xmlXPathNodeSetAddUnique(ret->nodesetval,
225
0
                                                         newChildNode);
226
0
                            }
227
0
                        }
228
0
                        break;
229
0
                    case XPATH_STRING:
230
0
                        if (container != NULL) {
231
0
                            xmlNodePtr newChildNode =
232
0
                                xmlNewTextChild((xmlNodePtr) container, NULL,
233
0
                                                BAD_CAST "string",
234
0
                                                subResult->stringval);
235
0
                            if (newChildNode != NULL) {
236
0
                                newChildNode->ns =
237
0
                                    xmlNewNs(newChildNode,
238
0
                                             BAD_CAST
239
0
                                             "http://exslt.org/common",
240
0
                                             BAD_CAST "exsl");
241
0
                                xmlXPathNodeSetAddUnique(ret->nodesetval,
242
0
                                                         newChildNode);
243
0
                            }
244
0
                        }
245
0
                        break;
246
0
        default:
247
0
                        break;
248
60
                }
249
60
                xmlXPathFreeObject(subResult);
250
60
            }
251
60
        }
252
5
    }
253
5
    ctxt->context->doc = oldDoc;
254
5
    ctxt->context->node = oldNode;
255
5
    ctxt->context->contextSize = oldContextSize;
256
5
    ctxt->context->proximityPosition = oldProximityPosition;
257
258
259
5
  cleanup:
260
    /* restore the xpath context */
261
5
    if (comp != NULL)
262
5
        xmlXPathFreeCompExpr(comp);
263
5
    if (nodeset != NULL)
264
5
        xmlXPathFreeNodeSet(nodeset);
265
5
    if (str != NULL)
266
5
        xmlFree(str);
267
5
    valuePush(ctxt, ret);
268
5
    return;
269
5
}
270
271
272
/**
273
 * exsltDynRegister:
274
 *
275
 * Registers the EXSLT - Dynamic module
276
 */
277
278
void
279
50
exsltDynRegister (void) {
280
50
    xsltRegisterExtModuleFunction ((const xmlChar *) "evaluate",
281
50
           EXSLT_DYNAMIC_NAMESPACE,
282
50
           exsltDynEvaluateFunction);
283
50
  xsltRegisterExtModuleFunction ((const xmlChar *) "map",
284
50
           EXSLT_DYNAMIC_NAMESPACE,
285
50
           exsltDynMapFunction);
286
287
50
}