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