Coverage Report

Created: 2024-01-23 06:30

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