Coverage Report

Created: 2024-10-19 16:35

/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
41.6k
exsltDynEvaluateFunction(xmlXPathParserContextPtr ctxt, int nargs) {
48
41.6k
  xmlChar *str = NULL;
49
41.6k
  xmlXPathObjectPtr ret = NULL;
50
51
41.6k
  if (ctxt == NULL)
52
0
    return;
53
41.6k
  if (nargs != 1) {
54
39
    xsltPrintErrorContext(xsltXPathGetTransformContext(ctxt), NULL, NULL);
55
39
        xsltGenericError(xsltGenericErrorContext,
56
39
      "dyn:evalute() : invalid number of args %d\n", nargs);
57
39
    ctxt->error = XPATH_INVALID_ARITY;
58
39
    return;
59
39
  }
60
41.6k
  str = xmlXPathPopString(ctxt);
61
  /* return an empty node-set if an empty string is passed in */
62
41.6k
  if (!str||!xmlStrlen(str)) {
63
14.2k
    if (str) xmlFree(str);
64
14.2k
    valuePush(ctxt,xmlXPathNewNodeSet(NULL));
65
14.2k
    return;
66
14.2k
  }
67
27.4k
  ret = xmlXPathEval(str,ctxt->context);
68
27.4k
  if (ret)
69
12.1k
    valuePush(ctxt,ret);
70
15.2k
  else {
71
15.2k
    xsltGenericError(xsltGenericErrorContext,
72
15.2k
      "dyn:evaluate() : unable to evaluate expression '%s'\n",str);
73
15.2k
    valuePush(ctxt,xmlXPathNewNodeSet(NULL));
74
15.2k
  }
75
27.4k
  xmlFree(str);
76
27.4k
  return;
77
41.6k
}
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
2.71M
{
92
2.71M
    xmlChar *str = NULL;
93
2.71M
    xmlNodeSetPtr nodeset = NULL;
94
2.71M
    xsltTransformContextPtr tctxt;
95
2.71M
    xmlXPathCompExprPtr comp = NULL;
96
2.71M
    xmlXPathObjectPtr ret = NULL;
97
2.71M
    xmlDocPtr oldDoc, container = NULL;
98
2.71M
    xmlNodePtr oldNode;
99
2.71M
    int oldContextSize;
100
2.71M
    int oldProximityPosition;
101
2.71M
    int i, j;
102
103
104
2.71M
    if (nargs != 2) {
105
11.9k
        xmlXPathSetArityError(ctxt);
106
11.9k
        return;
107
11.9k
    }
108
2.69M
    str = xmlXPathPopString(ctxt);
109
2.69M
    if (xmlXPathCheckError(ctxt))
110
0
        goto cleanup;
111
112
2.69M
    nodeset = xmlXPathPopNodeSet(ctxt);
113
2.69M
    if (xmlXPathCheckError(ctxt))
114
2.40k
        goto cleanup;
115
116
2.69M
    ret = xmlXPathNewNodeSet(NULL);
117
2.69M
    if (ret == NULL) {
118
0
        xsltGenericError(xsltGenericErrorContext,
119
0
                         "exsltDynMapFunction: ret == NULL\n");
120
0
        goto cleanup;
121
0
    }
122
123
2.69M
    tctxt = xsltXPathGetTransformContext(ctxt);
124
2.69M
    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
2.69M
    if (str == NULL || !xmlStrlen(str) ||
131
2.69M
        !(comp = xmlXPathCtxtCompile(tctxt->xpathCtxt, str)))
132
2.00M
        goto cleanup;
133
134
690k
    oldDoc = ctxt->context->doc;
135
690k
    oldNode = ctxt->context->node;
136
690k
    oldContextSize = ctxt->context->contextSize;
137
690k
    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
690k
    container = xsltCreateRVT(tctxt);
144
690k
    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
690k
    xsltRegisterLocalRVT(tctxt, container);
150
690k
    if (nodeset && nodeset->nodeNr > 0) {
151
453k
        xmlXPathNodeSetSort(nodeset);
152
453k
        ctxt->context->contextSize = nodeset->nodeNr;
153
453k
        ctxt->context->proximityPosition = 0;
154
3.64M
        for (i = 0; i < nodeset->nodeNr; i++) {
155
3.18M
            xmlXPathObjectPtr subResult = NULL;
156
3.18M
            xmlNodePtr cur = nodeset->nodeTab[i];
157
158
3.18M
            ctxt->context->proximityPosition++;
159
3.18M
            ctxt->context->node = cur;
160
161
3.18M
            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
65.3k
                cur = (xmlNodePtr) ((xmlNsPtr) cur)->next;
167
65.3k
                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
65.3k
                ctxt->context->doc = cur->doc;
174
3.12M
            } else {
175
3.12M
                ctxt->context->doc = cur->doc;
176
3.12M
            }
177
178
3.18M
            subResult = xmlXPathCompiledEval(comp, ctxt->context);
179
3.18M
            if (subResult != NULL) {
180
2.40M
                switch (subResult->type) {
181
1.45M
                    case XPATH_NODESET:
182
1.45M
                        if (subResult->nodesetval != NULL)
183
3.02M
                            for (j = 0; j < subResult->nodesetval->nodeNr;
184
1.61M
                                 j++)
185
1.61M
                                xmlXPathNodeSetAdd(ret->nodesetval,
186
1.61M
                                                   subResult->nodesetval->
187
1.61M
                                                   nodeTab[j]);
188
1.45M
                        break;
189
584k
                    case XPATH_BOOLEAN:
190
584k
                        if (container != NULL) {
191
584k
                            xmlNodePtr newChildNode =
192
584k
                                xmlNewTextChild((xmlNodePtr) container, NULL,
193
584k
                                                BAD_CAST "boolean",
194
584k
                                                BAD_CAST (subResult->
195
584k
                                                boolval ? "true" : ""));
196
584k
                            if (newChildNode != NULL) {
197
584k
                                newChildNode->ns =
198
584k
                                    xmlNewNs(newChildNode,
199
584k
                                             BAD_CAST
200
584k
                                             "http://exslt.org/common",
201
584k
                                             BAD_CAST "exsl");
202
584k
                                xmlXPathNodeSetAddUnique(ret->nodesetval,
203
584k
                                                         newChildNode);
204
584k
                            }
205
584k
                        }
206
584k
                        break;
207
356k
                    case XPATH_NUMBER:
208
356k
                        if (container != NULL) {
209
356k
                            xmlChar *val =
210
356k
                                xmlXPathCastNumberToString(subResult->
211
356k
                                                           floatval);
212
356k
                            xmlNodePtr newChildNode =
213
356k
                                xmlNewTextChild((xmlNodePtr) container, NULL,
214
356k
                                                BAD_CAST "number", val);
215
356k
                            if (val != NULL)
216
356k
                                xmlFree(val);
217
218
356k
                            if (newChildNode != NULL) {
219
356k
                                newChildNode->ns =
220
356k
                                    xmlNewNs(newChildNode,
221
356k
                                             BAD_CAST
222
356k
                                             "http://exslt.org/common",
223
356k
                                             BAD_CAST "exsl");
224
356k
                                xmlXPathNodeSetAddUnique(ret->nodesetval,
225
356k
                                                         newChildNode);
226
356k
                            }
227
356k
                        }
228
356k
                        break;
229
15.8k
                    case XPATH_STRING:
230
15.8k
                        if (container != NULL) {
231
15.8k
                            xmlNodePtr newChildNode =
232
15.8k
                                xmlNewTextChild((xmlNodePtr) container, NULL,
233
15.8k
                                                BAD_CAST "string",
234
15.8k
                                                subResult->stringval);
235
15.8k
                            if (newChildNode != NULL) {
236
15.8k
                                newChildNode->ns =
237
15.8k
                                    xmlNewNs(newChildNode,
238
15.8k
                                             BAD_CAST
239
15.8k
                                             "http://exslt.org/common",
240
15.8k
                                             BAD_CAST "exsl");
241
15.8k
                                xmlXPathNodeSetAddUnique(ret->nodesetval,
242
15.8k
                                                         newChildNode);
243
15.8k
                            }
244
15.8k
                        }
245
15.8k
                        break;
246
0
        default:
247
0
                        break;
248
2.40M
                }
249
2.40M
                xmlXPathFreeObject(subResult);
250
2.40M
            }
251
3.18M
        }
252
453k
    }
253
690k
    ctxt->context->doc = oldDoc;
254
690k
    ctxt->context->node = oldNode;
255
690k
    ctxt->context->contextSize = oldContextSize;
256
690k
    ctxt->context->proximityPosition = oldProximityPosition;
257
258
259
2.69M
  cleanup:
260
    /* restore the xpath context */
261
2.69M
    if (comp != NULL)
262
690k
        xmlXPathFreeCompExpr(comp);
263
2.69M
    if (nodeset != NULL)
264
2.22M
        xmlXPathFreeNodeSet(nodeset);
265
2.69M
    if (str != NULL)
266
2.69M
        xmlFree(str);
267
2.69M
    valuePush(ctxt, ret);
268
2.69M
    return;
269
690k
}
270
271
272
/**
273
 * exsltDynRegister:
274
 *
275
 * Registers the EXSLT - Dynamic module
276
 */
277
278
void
279
3.51k
exsltDynRegister (void) {
280
3.51k
    xsltRegisterExtModuleFunction ((const xmlChar *) "evaluate",
281
3.51k
           EXSLT_DYNAMIC_NAMESPACE,
282
3.51k
           exsltDynEvaluateFunction);
283
3.51k
  xsltRegisterExtModuleFunction ((const xmlChar *) "map",
284
3.51k
           EXSLT_DYNAMIC_NAMESPACE,
285
3.51k
           exsltDynMapFunction);
286
287
3.51k
}