Coverage Report

Created: 2023-12-14 14:11

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