Coverage Report

Created: 2024-05-08 16:11

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