Coverage Report

Created: 2023-09-24 16:02

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