Coverage Report

Created: 2024-01-21 07:00

/src/libxslt/libexslt/sets.c
Line
Count
Source (jump to first uncovered line)
1
#define IN_LIBEXSLT
2
#include "libexslt/libexslt.h"
3
4
#include <libxml/tree.h>
5
#include <libxml/xpath.h>
6
#include <libxml/xpathInternals.h>
7
8
#include <libxslt/xsltutils.h>
9
#include <libxslt/xsltInternals.h>
10
#include <libxslt/extensions.h>
11
12
#include "exslt.h"
13
14
/**
15
 * exsltSetsDifferenceFunction:
16
 * @ctxt:  an XPath parser context
17
 * @nargs:  the number of arguments
18
 *
19
 * Wraps #xmlXPathDifference for use by the XPath processor
20
 */
21
static void
22
66.6k
exsltSetsDifferenceFunction (xmlXPathParserContextPtr ctxt, int nargs) {
23
66.6k
    xmlNodeSetPtr arg1, arg2, ret;
24
25
66.6k
    if (nargs != 2) {
26
2.99k
  xmlXPathSetArityError(ctxt);
27
2.99k
  return;
28
2.99k
    }
29
30
63.6k
    arg2 = xmlXPathPopNodeSet(ctxt);
31
63.6k
    if (xmlXPathCheckError(ctxt))
32
579
  return;
33
34
63.0k
    arg1 = xmlXPathPopNodeSet(ctxt);
35
63.0k
    if (xmlXPathCheckError(ctxt)) {
36
671
        xmlXPathFreeNodeSet(arg2);
37
671
  return;
38
671
    }
39
40
62.4k
    ret = xmlXPathDifference(arg1, arg2);
41
42
62.4k
    if (ret != arg1)
43
47.8k
  xmlXPathFreeNodeSet(arg1);
44
62.4k
    xmlXPathFreeNodeSet(arg2);
45
46
62.4k
    xmlXPathReturnNodeSet(ctxt, ret);
47
62.4k
}
48
49
/**
50
 * exsltSetsIntersectionFunction:
51
 * @ctxt:  an XPath parser context
52
 * @nargs:  the number of arguments
53
 *
54
 * Wraps #xmlXPathIntersection for use by the XPath processor
55
 */
56
static void
57
166k
exsltSetsIntersectionFunction (xmlXPathParserContextPtr ctxt, int nargs) {
58
166k
    xmlNodeSetPtr arg1, arg2, ret;
59
60
166k
    if (nargs != 2) {
61
19.2k
  xmlXPathSetArityError(ctxt);
62
19.2k
  return;
63
19.2k
    }
64
65
147k
    arg2 = xmlXPathPopNodeSet(ctxt);
66
147k
    if (xmlXPathCheckError(ctxt))
67
5.13k
  return;
68
69
141k
    arg1 = xmlXPathPopNodeSet(ctxt);
70
141k
    if (xmlXPathCheckError(ctxt)) {
71
5.18k
        xmlXPathFreeNodeSet(arg2);
72
5.18k
  return;
73
5.18k
    }
74
75
136k
    ret = xmlXPathIntersection(arg1, arg2);
76
77
136k
    xmlXPathFreeNodeSet(arg1);
78
136k
    xmlXPathFreeNodeSet(arg2);
79
80
136k
    xmlXPathReturnNodeSet(ctxt, ret);
81
136k
}
82
83
/**
84
 * exsltSetsDistinctFunction:
85
 * @ctxt:  an XPath parser context
86
 * @nargs:  the number of arguments
87
 *
88
 * Wraps #xmlXPathDistinct for use by the XPath processor
89
 */
90
static void
91
86.3k
exsltSetsDistinctFunction (xmlXPathParserContextPtr ctxt, int nargs) {
92
86.3k
    xmlXPathObjectPtr obj;
93
86.3k
    xmlNodeSetPtr ns, ret;
94
86.3k
    int boolval = 0;
95
86.3k
    void *user = NULL;
96
97
86.3k
    if (nargs != 1) {
98
2.07k
  xmlXPathSetArityError(ctxt);
99
2.07k
  return;
100
2.07k
    }
101
102
84.2k
    if (ctxt->value != NULL) {
103
84.2k
        boolval = ctxt->value->boolval;
104
84.2k
  user = ctxt->value->user;
105
84.2k
  ctxt->value->boolval = 0;
106
84.2k
  ctxt->value->user = NULL;
107
84.2k
    }
108
84.2k
    ns = xmlXPathPopNodeSet(ctxt);
109
84.2k
    if (xmlXPathCheckError(ctxt))
110
2.66k
  return;
111
112
    /* !!! must be sorted !!! */
113
81.6k
    ret = xmlXPathDistinctSorted(ns);
114
115
81.6k
  if (ret != ns)
116
45.7k
    xmlXPathFreeNodeSet(ns);
117
118
81.6k
    obj = xmlXPathWrapNodeSet(ret);
119
81.6k
    obj->user = user;
120
81.6k
    obj->boolval = boolval;
121
81.6k
    valuePush((ctxt), obj);
122
81.6k
}
123
124
/**
125
 * exsltSetsHasSameNodesFunction:
126
 * @ctxt:  an XPath parser context
127
 * @nargs:  the number of arguments
128
 *
129
 * Wraps #xmlXPathHasSameNodes for use by the XPath processor
130
 */
131
static void
132
exsltSetsHasSameNodesFunction (xmlXPathParserContextPtr ctxt,
133
132k
            int nargs) {
134
132k
    xmlNodeSetPtr arg1, arg2;
135
132k
    int ret;
136
137
132k
    if (nargs != 2) {
138
1.12k
  xmlXPathSetArityError(ctxt);
139
1.12k
  return;
140
1.12k
    }
141
142
131k
    arg2 = xmlXPathPopNodeSet(ctxt);
143
131k
    if (xmlXPathCheckError(ctxt))
144
281
  return;
145
146
130k
    arg1 = xmlXPathPopNodeSet(ctxt);
147
130k
    if (xmlXPathCheckError(ctxt)) {
148
472
        xmlXPathFreeNodeSet(arg2);
149
472
  return;
150
472
    }
151
152
130k
    ret = xmlXPathHasSameNodes(arg1, arg2);
153
154
130k
    xmlXPathFreeNodeSet(arg1);
155
130k
    xmlXPathFreeNodeSet(arg2);
156
157
130k
    xmlXPathReturnBoolean(ctxt, ret);
158
130k
}
159
160
/**
161
 * exsltSetsLeadingFunction:
162
 * @ctxt:  an XPath parser context
163
 * @nargs:  the number of arguments
164
 *
165
 * Wraps #xmlXPathLeading for use by the XPath processor
166
 */
167
static void
168
169k
exsltSetsLeadingFunction (xmlXPathParserContextPtr ctxt, int nargs) {
169
169k
    xmlNodeSetPtr arg1, arg2, ret;
170
171
169k
    if (nargs != 2) {
172
4.63k
  xmlXPathSetArityError(ctxt);
173
4.63k
  return;
174
4.63k
    }
175
176
164k
    arg2 = xmlXPathPopNodeSet(ctxt);
177
164k
    if (xmlXPathCheckError(ctxt))
178
1.84k
  return;
179
180
162k
    arg1 = xmlXPathPopNodeSet(ctxt);
181
162k
    if (xmlXPathCheckError(ctxt)) {
182
1.19k
  xmlXPathFreeNodeSet(arg2);
183
1.19k
  return;
184
1.19k
    }
185
186
    /*  If the second node set is empty, then the first node set is
187
     * returned.
188
     */
189
161k
    if (xmlXPathNodeSetIsEmpty(arg2)) {
190
43.0k
  xmlXPathReturnNodeSet(ctxt, arg1);
191
192
43.0k
  xmlXPathFreeNodeSet(arg2);
193
194
43.0k
  return;
195
43.0k
    }
196
    /* !!! must be sorted */
197
118k
    ret = xmlXPathNodeLeadingSorted(arg1, xmlXPathNodeSetItem(arg2, 0));
198
199
118k
    xmlXPathFreeNodeSet(arg1);
200
118k
    xmlXPathFreeNodeSet(arg2);
201
202
118k
    xmlXPathReturnNodeSet(ctxt, ret);
203
118k
}
204
205
/**
206
 * exsltSetsTrailingFunction:
207
 * @ctxt:  an XPath parser context
208
 * @nargs:  the number of arguments
209
 *
210
 * Wraps #xmlXPathTrailing for use by the XPath processor
211
 */
212
static void
213
275k
exsltSetsTrailingFunction (xmlXPathParserContextPtr ctxt, int nargs) {
214
275k
    xmlNodeSetPtr arg1, arg2, ret;
215
216
275k
    if (nargs != 2) {
217
8.45k
  xmlXPathSetArityError(ctxt);
218
8.45k
  return;
219
8.45k
    }
220
221
266k
    arg2 = xmlXPathPopNodeSet(ctxt);
222
266k
    if (xmlXPathCheckError(ctxt))
223
5.85k
  return;
224
225
261k
    arg1 = xmlXPathPopNodeSet(ctxt);
226
261k
    if (xmlXPathCheckError(ctxt)) {
227
4.83k
  xmlXPathFreeNodeSet(arg2);
228
4.83k
  return;
229
4.83k
    }
230
231
    /*  If the second node set is empty, then the first node set is
232
     * returned.
233
     */
234
256k
    if (xmlXPathNodeSetIsEmpty(arg2)) {
235
64.0k
  xmlXPathReturnNodeSet(ctxt, arg1);
236
237
64.0k
  xmlXPathFreeNodeSet(arg2);
238
239
64.0k
  return;
240
64.0k
    }
241
    /* !!! mist be sorted */
242
192k
    ret = xmlXPathNodeTrailingSorted(arg1, xmlXPathNodeSetItem(arg2, 0));
243
244
192k
    xmlXPathFreeNodeSet(arg1);
245
192k
    xmlXPathFreeNodeSet(arg2);
246
247
192k
    xmlXPathReturnNodeSet(ctxt, ret);
248
192k
}
249
250
/**
251
 * exsltSetsRegister:
252
 *
253
 * Registers the EXSLT - Sets module
254
 */
255
256
void
257
3.48k
exsltSetsRegister (void) {
258
3.48k
    xsltRegisterExtModuleFunction ((const xmlChar *) "difference",
259
3.48k
           EXSLT_SETS_NAMESPACE,
260
3.48k
           exsltSetsDifferenceFunction);
261
3.48k
    xsltRegisterExtModuleFunction ((const xmlChar *) "intersection",
262
3.48k
           EXSLT_SETS_NAMESPACE,
263
3.48k
           exsltSetsIntersectionFunction);
264
3.48k
    xsltRegisterExtModuleFunction ((const xmlChar *) "distinct",
265
3.48k
           EXSLT_SETS_NAMESPACE,
266
3.48k
           exsltSetsDistinctFunction);
267
3.48k
    xsltRegisterExtModuleFunction ((const xmlChar *) "has-same-node",
268
3.48k
           EXSLT_SETS_NAMESPACE,
269
3.48k
           exsltSetsHasSameNodesFunction);
270
3.48k
    xsltRegisterExtModuleFunction ((const xmlChar *) "leading",
271
3.48k
           EXSLT_SETS_NAMESPACE,
272
3.48k
           exsltSetsLeadingFunction);
273
3.48k
    xsltRegisterExtModuleFunction ((const xmlChar *) "trailing",
274
3.48k
           EXSLT_SETS_NAMESPACE,
275
3.48k
           exsltSetsTrailingFunction);
276
3.48k
}
277
278
/**
279
 * exsltSetsXpathCtxtRegister:
280
 *
281
 * Registers the EXSLT - Sets module for use outside XSLT
282
 */
283
int
284
exsltSetsXpathCtxtRegister (xmlXPathContextPtr ctxt, const xmlChar *prefix)
285
0
{
286
0
    if (ctxt
287
0
        && prefix
288
0
        && !xmlXPathRegisterNs(ctxt,
289
0
                               prefix,
290
0
                               (const xmlChar *) EXSLT_SETS_NAMESPACE)
291
0
        && !xmlXPathRegisterFuncNS(ctxt,
292
0
                                   (const xmlChar *) "difference",
293
0
                                   (const xmlChar *) EXSLT_SETS_NAMESPACE,
294
0
                                   exsltSetsDifferenceFunction)
295
0
        && !xmlXPathRegisterFuncNS(ctxt,
296
0
                                   (const xmlChar *) "intersection",
297
0
                                   (const xmlChar *) EXSLT_SETS_NAMESPACE,
298
0
                                   exsltSetsIntersectionFunction)
299
0
        && !xmlXPathRegisterFuncNS(ctxt,
300
0
                                   (const xmlChar *) "distinct",
301
0
                                   (const xmlChar *) EXSLT_SETS_NAMESPACE,
302
0
                                   exsltSetsDistinctFunction)
303
0
        && !xmlXPathRegisterFuncNS(ctxt,
304
0
                                   (const xmlChar *) "has-same-node",
305
0
                                   (const xmlChar *) EXSLT_SETS_NAMESPACE,
306
0
                                   exsltSetsHasSameNodesFunction)
307
0
        && !xmlXPathRegisterFuncNS(ctxt,
308
0
                                   (const xmlChar *) "leading",
309
0
                                   (const xmlChar *) EXSLT_SETS_NAMESPACE,
310
0
                                   exsltSetsLeadingFunction)
311
0
        && !xmlXPathRegisterFuncNS(ctxt,
312
0
                                   (const xmlChar *) "trailing",
313
0
                                   (const xmlChar *) EXSLT_SETS_NAMESPACE,
314
0
                                   exsltSetsTrailingFunction)) {
315
0
        return 0;
316
0
    }
317
0
    return -1;
318
0
}