Coverage Report

Created: 2025-03-12 04:16

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