Coverage Report

Created: 2024-01-23 06:29

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