Coverage Report

Created: 2023-03-02 15:26

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