Coverage Report

Created: 2024-04-26 11:09

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