Coverage Report

Created: 2024-08-09 12:03

/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
25.6k
exsltSetsDifferenceFunction (xmlXPathParserContextPtr ctxt, int nargs) {
23
25.6k
    xmlNodeSetPtr arg1, arg2, ret;
24
25
25.6k
    if (nargs != 2) {
26
196
  xmlXPathSetArityError(ctxt);
27
196
  return;
28
196
    }
29
30
25.4k
    arg2 = xmlXPathPopNodeSet(ctxt);
31
25.4k
    if (xmlXPathCheckError(ctxt))
32
267
  return;
33
34
25.1k
    arg1 = xmlXPathPopNodeSet(ctxt);
35
25.1k
    if (xmlXPathCheckError(ctxt)) {
36
299
        xmlXPathFreeNodeSet(arg2);
37
299
  return;
38
299
    }
39
40
24.8k
    ret = xmlXPathDifference(arg1, arg2);
41
42
24.8k
    if (ret != arg1)
43
15.4k
  xmlXPathFreeNodeSet(arg1);
44
24.8k
    xmlXPathFreeNodeSet(arg2);
45
46
24.8k
    xmlXPathReturnNodeSet(ctxt, ret);
47
24.8k
}
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
15.3k
exsltSetsIntersectionFunction (xmlXPathParserContextPtr ctxt, int nargs) {
58
15.3k
    xmlNodeSetPtr arg1, arg2, ret;
59
60
15.3k
    if (nargs != 2) {
61
290
  xmlXPathSetArityError(ctxt);
62
290
  return;
63
290
    }
64
65
15.0k
    arg2 = xmlXPathPopNodeSet(ctxt);
66
15.0k
    if (xmlXPathCheckError(ctxt))
67
110
  return;
68
69
14.9k
    arg1 = xmlXPathPopNodeSet(ctxt);
70
14.9k
    if (xmlXPathCheckError(ctxt)) {
71
91
        xmlXPathFreeNodeSet(arg2);
72
91
  return;
73
91
    }
74
75
14.8k
    ret = xmlXPathIntersection(arg1, arg2);
76
77
14.8k
    xmlXPathFreeNodeSet(arg1);
78
14.8k
    xmlXPathFreeNodeSet(arg2);
79
80
14.8k
    xmlXPathReturnNodeSet(ctxt, ret);
81
14.8k
}
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
44.2k
exsltSetsDistinctFunction (xmlXPathParserContextPtr ctxt, int nargs) {
92
44.2k
    xmlXPathObjectPtr obj;
93
44.2k
    xmlNodeSetPtr ns, ret;
94
44.2k
    int boolval = 0;
95
44.2k
    void *user = NULL;
96
97
44.2k
    if (nargs != 1) {
98
153
  xmlXPathSetArityError(ctxt);
99
153
  return;
100
153
    }
101
102
44.1k
    if (ctxt->value != NULL) {
103
44.1k
        boolval = ctxt->value->boolval;
104
44.1k
  user = ctxt->value->user;
105
44.1k
  ctxt->value->boolval = 0;
106
44.1k
  ctxt->value->user = NULL;
107
44.1k
    }
108
44.1k
    ns = xmlXPathPopNodeSet(ctxt);
109
44.1k
    if (xmlXPathCheckError(ctxt))
110
633
  return;
111
112
    /* !!! must be sorted !!! */
113
43.4k
    ret = xmlXPathDistinctSorted(ns);
114
115
43.4k
  if (ret != ns)
116
24.0k
    xmlXPathFreeNodeSet(ns);
117
118
43.4k
    obj = xmlXPathWrapNodeSet(ret);
119
43.4k
    obj->user = user;
120
43.4k
    obj->boolval = boolval;
121
43.4k
    valuePush((ctxt), obj);
122
43.4k
}
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
15.5k
            int nargs) {
134
15.5k
    xmlNodeSetPtr arg1, arg2;
135
15.5k
    int ret;
136
137
15.5k
    if (nargs != 2) {
138
1.63k
  xmlXPathSetArityError(ctxt);
139
1.63k
  return;
140
1.63k
    }
141
142
13.8k
    arg2 = xmlXPathPopNodeSet(ctxt);
143
13.8k
    if (xmlXPathCheckError(ctxt))
144
219
  return;
145
146
13.6k
    arg1 = xmlXPathPopNodeSet(ctxt);
147
13.6k
    if (xmlXPathCheckError(ctxt)) {
148
66
        xmlXPathFreeNodeSet(arg2);
149
66
  return;
150
66
    }
151
152
13.5k
    ret = xmlXPathHasSameNodes(arg1, arg2);
153
154
13.5k
    xmlXPathFreeNodeSet(arg1);
155
13.5k
    xmlXPathFreeNodeSet(arg2);
156
157
13.5k
    xmlXPathReturnBoolean(ctxt, ret);
158
13.5k
}
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
33.6k
exsltSetsLeadingFunction (xmlXPathParserContextPtr ctxt, int nargs) {
169
33.6k
    xmlNodeSetPtr arg1, arg2, ret;
170
171
33.6k
    if (nargs != 2) {
172
82
  xmlXPathSetArityError(ctxt);
173
82
  return;
174
82
    }
175
176
33.6k
    arg2 = xmlXPathPopNodeSet(ctxt);
177
33.6k
    if (xmlXPathCheckError(ctxt))
178
53
  return;
179
180
33.5k
    arg1 = xmlXPathPopNodeSet(ctxt);
181
33.5k
    if (xmlXPathCheckError(ctxt)) {
182
23
  xmlXPathFreeNodeSet(arg2);
183
23
  return;
184
23
    }
185
186
    /*  If the second node set is empty, then the first node set is
187
     * returned.
188
     */
189
33.5k
    if (xmlXPathNodeSetIsEmpty(arg2)) {
190
13.0k
  xmlXPathReturnNodeSet(ctxt, arg1);
191
192
13.0k
  xmlXPathFreeNodeSet(arg2);
193
194
13.0k
  return;
195
13.0k
    }
196
    /* !!! must be sorted */
197
20.4k
    ret = xmlXPathNodeLeadingSorted(arg1, xmlXPathNodeSetItem(arg2, 0));
198
199
20.4k
    xmlXPathFreeNodeSet(arg1);
200
20.4k
    xmlXPathFreeNodeSet(arg2);
201
202
20.4k
    xmlXPathReturnNodeSet(ctxt, ret);
203
20.4k
}
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
24.7k
exsltSetsTrailingFunction (xmlXPathParserContextPtr ctxt, int nargs) {
214
24.7k
    xmlNodeSetPtr arg1, arg2, ret;
215
216
24.7k
    if (nargs != 2) {
217
2.66k
  xmlXPathSetArityError(ctxt);
218
2.66k
  return;
219
2.66k
    }
220
221
22.1k
    arg2 = xmlXPathPopNodeSet(ctxt);
222
22.1k
    if (xmlXPathCheckError(ctxt))
223
1.11k
  return;
224
225
20.9k
    arg1 = xmlXPathPopNodeSet(ctxt);
226
20.9k
    if (xmlXPathCheckError(ctxt)) {
227
339
  xmlXPathFreeNodeSet(arg2);
228
339
  return;
229
339
    }
230
231
    /*  If the second node set is empty, then the first node set is
232
     * returned.
233
     */
234
20.6k
    if (xmlXPathNodeSetIsEmpty(arg2)) {
235
6.62k
  xmlXPathReturnNodeSet(ctxt, arg1);
236
237
6.62k
  xmlXPathFreeNodeSet(arg2);
238
239
6.62k
  return;
240
6.62k
    }
241
    /* !!! mist be sorted */
242
14.0k
    ret = xmlXPathNodeTrailingSorted(arg1, xmlXPathNodeSetItem(arg2, 0));
243
244
14.0k
    xmlXPathFreeNodeSet(arg1);
245
14.0k
    xmlXPathFreeNodeSet(arg2);
246
247
14.0k
    xmlXPathReturnNodeSet(ctxt, ret);
248
14.0k
}
249
250
/**
251
 * exsltSetsRegister:
252
 *
253
 * Registers the EXSLT - Sets module
254
 */
255
256
void
257
2.12k
exsltSetsRegister (void) {
258
2.12k
    xsltRegisterExtModuleFunction ((const xmlChar *) "difference",
259
2.12k
           EXSLT_SETS_NAMESPACE,
260
2.12k
           exsltSetsDifferenceFunction);
261
2.12k
    xsltRegisterExtModuleFunction ((const xmlChar *) "intersection",
262
2.12k
           EXSLT_SETS_NAMESPACE,
263
2.12k
           exsltSetsIntersectionFunction);
264
2.12k
    xsltRegisterExtModuleFunction ((const xmlChar *) "distinct",
265
2.12k
           EXSLT_SETS_NAMESPACE,
266
2.12k
           exsltSetsDistinctFunction);
267
2.12k
    xsltRegisterExtModuleFunction ((const xmlChar *) "has-same-node",
268
2.12k
           EXSLT_SETS_NAMESPACE,
269
2.12k
           exsltSetsHasSameNodesFunction);
270
2.12k
    xsltRegisterExtModuleFunction ((const xmlChar *) "leading",
271
2.12k
           EXSLT_SETS_NAMESPACE,
272
2.12k
           exsltSetsLeadingFunction);
273
2.12k
    xsltRegisterExtModuleFunction ((const xmlChar *) "trailing",
274
2.12k
           EXSLT_SETS_NAMESPACE,
275
2.12k
           exsltSetsTrailingFunction);
276
2.12k
}
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
}