Coverage Report

Created: 2024-08-27 12:19

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