Coverage Report

Created: 2024-01-21 07:00

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