Coverage Report

Created: 2024-06-09 08:57

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