Coverage Report

Created: 2024-01-18 20:17

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