Coverage Report

Created: 2024-01-17 17:02

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