Coverage Report

Created: 2023-12-18 13:37

/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
202k
exsltSetsDifferenceFunction (xmlXPathParserContextPtr ctxt, int nargs) {
23
202k
    xmlNodeSetPtr arg1, arg2, ret;
24
25
202k
    if (nargs != 2) {
26
24.3k
  xmlXPathSetArityError(ctxt);
27
24.3k
  return;
28
24.3k
    }
29
30
178k
    arg2 = xmlXPathPopNodeSet(ctxt);
31
178k
    if (xmlXPathCheckError(ctxt))
32
19.7k
  return;
33
34
158k
    arg1 = xmlXPathPopNodeSet(ctxt);
35
158k
    if (xmlXPathCheckError(ctxt)) {
36
28.5k
        xmlXPathFreeNodeSet(arg2);
37
28.5k
  return;
38
28.5k
    }
39
40
129k
    ret = xmlXPathDifference(arg1, arg2);
41
42
129k
    if (ret != arg1)
43
64.3k
  xmlXPathFreeNodeSet(arg1);
44
129k
    xmlXPathFreeNodeSet(arg2);
45
46
129k
    xmlXPathReturnNodeSet(ctxt, ret);
47
129k
}
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
84.6k
exsltSetsIntersectionFunction (xmlXPathParserContextPtr ctxt, int nargs) {
58
84.6k
    xmlNodeSetPtr arg1, arg2, ret;
59
60
84.6k
    if (nargs != 2) {
61
3.91k
  xmlXPathSetArityError(ctxt);
62
3.91k
  return;
63
3.91k
    }
64
65
80.7k
    arg2 = xmlXPathPopNodeSet(ctxt);
66
80.7k
    if (xmlXPathCheckError(ctxt))
67
2.68k
  return;
68
69
78.0k
    arg1 = xmlXPathPopNodeSet(ctxt);
70
78.0k
    if (xmlXPathCheckError(ctxt)) {
71
2.78k
        xmlXPathFreeNodeSet(arg2);
72
2.78k
  return;
73
2.78k
    }
74
75
75.2k
    ret = xmlXPathIntersection(arg1, arg2);
76
77
75.2k
    xmlXPathFreeNodeSet(arg1);
78
75.2k
    xmlXPathFreeNodeSet(arg2);
79
80
75.2k
    xmlXPathReturnNodeSet(ctxt, ret);
81
75.2k
}
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
162k
exsltSetsDistinctFunction (xmlXPathParserContextPtr ctxt, int nargs) {
92
162k
    xmlXPathObjectPtr obj;
93
162k
    xmlNodeSetPtr ns, ret;
94
162k
    int boolval = 0;
95
162k
    void *user = NULL;
96
97
162k
    if (nargs != 1) {
98
2.31k
  xmlXPathSetArityError(ctxt);
99
2.31k
  return;
100
2.31k
    }
101
102
159k
    if (ctxt->value != NULL) {
103
159k
        boolval = ctxt->value->boolval;
104
159k
  user = ctxt->value->user;
105
159k
  ctxt->value->boolval = 0;
106
159k
  ctxt->value->user = NULL;
107
159k
    }
108
159k
    ns = xmlXPathPopNodeSet(ctxt);
109
159k
    if (xmlXPathCheckError(ctxt))
110
643
  return;
111
112
    /* !!! must be sorted !!! */
113
159k
    ret = xmlXPathDistinctSorted(ns);
114
115
159k
  if (ret != ns)
116
87.5k
    xmlXPathFreeNodeSet(ns);
117
118
159k
    obj = xmlXPathWrapNodeSet(ret);
119
159k
    obj->user = user;
120
159k
    obj->boolval = boolval;
121
159k
    valuePush((ctxt), obj);
122
159k
}
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
235k
            int nargs) {
134
235k
    xmlNodeSetPtr arg1, arg2;
135
235k
    int ret;
136
137
235k
    if (nargs != 2) {
138
2.05k
  xmlXPathSetArityError(ctxt);
139
2.05k
  return;
140
2.05k
    }
141
142
233k
    arg2 = xmlXPathPopNodeSet(ctxt);
143
233k
    if (xmlXPathCheckError(ctxt))
144
2.05k
  return;
145
146
231k
    arg1 = xmlXPathPopNodeSet(ctxt);
147
231k
    if (xmlXPathCheckError(ctxt)) {
148
2.03k
        xmlXPathFreeNodeSet(arg2);
149
2.03k
  return;
150
2.03k
    }
151
152
229k
    ret = xmlXPathHasSameNodes(arg1, arg2);
153
154
229k
    xmlXPathFreeNodeSet(arg1);
155
229k
    xmlXPathFreeNodeSet(arg2);
156
157
229k
    xmlXPathReturnBoolean(ctxt, ret);
158
229k
}
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
138k
exsltSetsLeadingFunction (xmlXPathParserContextPtr ctxt, int nargs) {
169
138k
    xmlNodeSetPtr arg1, arg2, ret;
170
171
138k
    if (nargs != 2) {
172
6.90k
  xmlXPathSetArityError(ctxt);
173
6.90k
  return;
174
6.90k
    }
175
176
131k
    arg2 = xmlXPathPopNodeSet(ctxt);
177
131k
    if (xmlXPathCheckError(ctxt))
178
10.9k
  return;
179
180
120k
    arg1 = xmlXPathPopNodeSet(ctxt);
181
120k
    if (xmlXPathCheckError(ctxt)) {
182
3.81k
  xmlXPathFreeNodeSet(arg2);
183
3.81k
  return;
184
3.81k
    }
185
186
    /*  If the second node set is empty, then the first node set is
187
     * returned.
188
     */
189
116k
    if (xmlXPathNodeSetIsEmpty(arg2)) {
190
29.5k
  xmlXPathReturnNodeSet(ctxt, arg1);
191
192
29.5k
  xmlXPathFreeNodeSet(arg2);
193
194
29.5k
  return;
195
29.5k
    }
196
    /* !!! must be sorted */
197
86.8k
    ret = xmlXPathNodeLeadingSorted(arg1, xmlXPathNodeSetItem(arg2, 0));
198
199
86.8k
    xmlXPathFreeNodeSet(arg1);
200
86.8k
    xmlXPathFreeNodeSet(arg2);
201
202
86.8k
    xmlXPathReturnNodeSet(ctxt, ret);
203
86.8k
}
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
148k
exsltSetsTrailingFunction (xmlXPathParserContextPtr ctxt, int nargs) {
214
148k
    xmlNodeSetPtr arg1, arg2, ret;
215
216
148k
    if (nargs != 2) {
217
1.14k
  xmlXPathSetArityError(ctxt);
218
1.14k
  return;
219
1.14k
    }
220
221
147k
    arg2 = xmlXPathPopNodeSet(ctxt);
222
147k
    if (xmlXPathCheckError(ctxt))
223
2.51k
  return;
224
225
144k
    arg1 = xmlXPathPopNodeSet(ctxt);
226
144k
    if (xmlXPathCheckError(ctxt)) {
227
487
  xmlXPathFreeNodeSet(arg2);
228
487
  return;
229
487
    }
230
231
    /*  If the second node set is empty, then the first node set is
232
     * returned.
233
     */
234
144k
    if (xmlXPathNodeSetIsEmpty(arg2)) {
235
78.6k
  xmlXPathReturnNodeSet(ctxt, arg1);
236
237
78.6k
  xmlXPathFreeNodeSet(arg2);
238
239
78.6k
  return;
240
78.6k
    }
241
    /* !!! mist be sorted */
242
65.6k
    ret = xmlXPathNodeTrailingSorted(arg1, xmlXPathNodeSetItem(arg2, 0));
243
244
65.6k
    xmlXPathFreeNodeSet(arg1);
245
65.6k
    xmlXPathFreeNodeSet(arg2);
246
247
65.6k
    xmlXPathReturnNodeSet(ctxt, ret);
248
65.6k
}
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
}