Coverage Report

Created: 2024-01-23 06:29

/src/libxslt/libexslt/math.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 <math.h>
13
#include <stdlib.h>
14
15
#include "exslt.h"
16
17
/**
18
 * exsltMathMin:
19
 * @ns:  a node-set
20
 *
21
 * Implements the EXSLT - Math min() function:
22
 *    number math:min (node-set)
23
 *
24
 * Returns the minimum value of the nodes passed as the argument, or
25
 *         xmlXPathNAN if @ns is NULL or empty or if one of the nodes
26
 *         turns into NaN.
27
 */
28
static double
29
19.0k
exsltMathMin (xmlNodeSetPtr ns) {
30
19.0k
    double ret, cur;
31
19.0k
    int i;
32
33
19.0k
    if ((ns == NULL) || (ns->nodeNr == 0))
34
4.39k
  return(xmlXPathNAN);
35
14.6k
    ret = xmlXPathCastNodeToNumber(ns->nodeTab[0]);
36
14.6k
    if (xmlXPathIsNaN(ret))
37
5.21k
  return(xmlXPathNAN);
38
22.3k
    for (i = 1; i < ns->nodeNr; i++) {
39
19.8k
  cur = xmlXPathCastNodeToNumber(ns->nodeTab[i]);
40
19.8k
  if (xmlXPathIsNaN(cur))
41
7.00k
      return(xmlXPathNAN);
42
12.8k
  if (cur < ret)
43
3.30k
      ret = cur;
44
12.8k
    }
45
2.48k
    return(ret);
46
9.48k
}
47
48
/**
49
 * exsltMathMinFunction:
50
 * @ctxt:  an XPath parser context
51
 * @nargs:  the number of arguments
52
 *
53
 * Wraps #exsltMathMin for use by the XPath processor.
54
 */
55
static void
56
19.5k
exsltMathMinFunction (xmlXPathParserContextPtr ctxt, int nargs) {
57
19.5k
    xmlNodeSetPtr ns;
58
19.5k
    double ret;
59
19.5k
    void *user = NULL;
60
61
19.5k
    if (nargs != 1) {
62
14
  xsltGenericError(xsltGenericErrorContext,
63
14
       "math:min: invalid number of arguments\n");
64
14
  ctxt->error = XPATH_INVALID_ARITY;
65
14
  return;
66
14
    }
67
    /* We need to delay the freeing of value->user */
68
19.5k
    if ((ctxt->value != NULL) && (ctxt->value->boolval != 0)) {
69
315
        user = ctxt->value->user;
70
315
  ctxt->value->boolval = 0;
71
315
  ctxt->value->user = NULL;
72
315
    }
73
19.5k
    ns = xmlXPathPopNodeSet(ctxt);
74
19.5k
    if (xmlXPathCheckError(ctxt))
75
435
  return;
76
77
19.0k
    ret = exsltMathMin(ns);
78
79
19.0k
    xmlXPathFreeNodeSet(ns);
80
19.0k
    if (user != NULL)
81
0
        xmlFreeNodeList((xmlNodePtr)user);
82
83
19.0k
    xmlXPathReturnNumber(ctxt, ret);
84
19.0k
}
85
86
/**
87
 * exsltMathMax:
88
 * @ns:  a node-set
89
 *
90
 * Implements the EXSLT - Math max() function:
91
 *    number math:max (node-set)
92
 *
93
 * Returns the maximum value of the nodes passed as arguments, or
94
 *         xmlXPathNAN if @ns is NULL or empty or if one of the nodes
95
 *         turns into NaN.
96
 */
97
static double
98
42.7k
exsltMathMax (xmlNodeSetPtr ns) {
99
42.7k
    double ret, cur;
100
42.7k
    int i;
101
102
42.7k
    if ((ns == NULL) || (ns->nodeNr == 0))
103
25.4k
  return(xmlXPathNAN);
104
17.2k
    ret = xmlXPathCastNodeToNumber(ns->nodeTab[0]);
105
17.2k
    if (xmlXPathIsNaN(ret))
106
7.70k
  return(xmlXPathNAN);
107
18.0k
    for (i = 1; i < ns->nodeNr; i++) {
108
14.1k
  cur = xmlXPathCastNodeToNumber(ns->nodeTab[i]);
109
14.1k
  if (xmlXPathIsNaN(cur))
110
5.67k
      return(xmlXPathNAN);
111
8.49k
  if (cur > ret)
112
3.30k
      ret = cur;
113
8.49k
    }
114
3.87k
    return(ret);
115
9.54k
}
116
117
/**
118
 * exsltMathMaxFunction:
119
 * @ctxt:  an XPath parser context
120
 * @nargs:  the number of arguments
121
 *
122
 * Wraps #exsltMathMax for use by the XPath processor.
123
 */
124
static void
125
42.8k
exsltMathMaxFunction (xmlXPathParserContextPtr ctxt, int nargs) {
126
42.8k
    xmlNodeSetPtr ns;
127
42.8k
    double ret;
128
42.8k
    void *user = NULL;
129
130
42.8k
    if (nargs != 1) {
131
16
  xmlXPathSetArityError(ctxt);
132
16
  return;
133
16
    }
134
135
    /* We need to delay the freeing of value->user */
136
42.8k
    if ((ctxt->value != NULL) && (ctxt->value->boolval != 0)) {
137
58
  user = ctxt->value->user;
138
58
  ctxt->value->boolval = 0;
139
58
  ctxt->value->user = 0;
140
58
    }
141
42.8k
    ns = xmlXPathPopNodeSet(ctxt);
142
42.8k
    if (xmlXPathCheckError(ctxt))
143
90
  return;
144
145
42.7k
    ret = exsltMathMax(ns);
146
147
42.7k
    xmlXPathFreeNodeSet(ns);
148
149
42.7k
    if (user != NULL)
150
0
        xmlFreeNodeList((xmlNodePtr)user);
151
42.7k
    xmlXPathReturnNumber(ctxt, ret);
152
42.7k
}
153
154
/**
155
 * exsltMathHighest:
156
 * @ns:  a node-set
157
 *
158
 * Implements the EXSLT - Math highest() function:
159
 *    node-set math:highest (node-set)
160
 *
161
 * Returns the nodes in the node-set whose value is the maximum value
162
 *         for the node-set.
163
 */
164
static xmlNodeSetPtr
165
35.7k
exsltMathHighest (xmlNodeSetPtr ns) {
166
35.7k
    xmlNodeSetPtr ret = xmlXPathNodeSetCreate(NULL);
167
35.7k
    double max, cur;
168
35.7k
    int i;
169
170
35.7k
    if ((ns == NULL) || (ns->nodeNr == 0))
171
2.44k
  return(ret);
172
173
33.3k
    max = xmlXPathCastNodeToNumber(ns->nodeTab[0]);
174
33.3k
    if (xmlXPathIsNaN(max))
175
5.12k
  return(ret);
176
28.1k
    else
177
28.1k
  xmlXPathNodeSetAddUnique(ret, ns->nodeTab[0]);
178
179
230k
    for (i = 1; i < ns->nodeNr; i++) {
180
227k
  cur = xmlXPathCastNodeToNumber(ns->nodeTab[i]);
181
227k
  if (xmlXPathIsNaN(cur)) {
182
25.3k
      xmlXPathEmptyNodeSet(ret);
183
25.3k
      return(ret);
184
25.3k
  }
185
201k
  if (cur < max)
186
55.4k
      continue;
187
146k
  if (cur > max) {
188
21.9k
      max = cur;
189
21.9k
      xmlXPathEmptyNodeSet(ret);
190
21.9k
      xmlXPathNodeSetAddUnique(ret, ns->nodeTab[i]);
191
21.9k
      continue;
192
21.9k
  }
193
124k
  xmlXPathNodeSetAddUnique(ret, ns->nodeTab[i]);
194
124k
    }
195
2.81k
    return(ret);
196
28.1k
}
197
198
/**
199
 * exsltMathHighestFunction:
200
 * @ctxt:  an XPath parser context
201
 * @nargs:  the number of arguments
202
 *
203
 * Wraps #exsltMathHighest for use by the XPath processor
204
 */
205
static void
206
36.0k
exsltMathHighestFunction (xmlXPathParserContextPtr ctxt, int nargs) {
207
36.0k
    xmlNodeSetPtr ns, ret;
208
36.0k
    void *user = NULL;
209
210
36.0k
    if (nargs != 1) {
211
294
  xmlXPathSetArityError(ctxt);
212
294
  return;
213
294
    }
214
215
    /* We need to delay the freeing of value->user */
216
35.7k
    if ((ctxt->value != NULL) && ctxt->value->boolval != 0) {
217
6
        user = ctxt->value->user;
218
6
  ctxt->value->boolval = 0;
219
6
  ctxt->value->user = NULL;
220
6
    }
221
35.7k
    ns = xmlXPathPopNodeSet(ctxt);
222
35.7k
    if (xmlXPathCheckError(ctxt))
223
10
  return;
224
225
35.7k
    ret = exsltMathHighest(ns);
226
227
35.7k
    xmlXPathFreeNodeSet(ns);
228
35.7k
    if (user != NULL)
229
0
        xmlFreeNodeList((xmlNodePtr)user);
230
231
35.7k
    xmlXPathReturnNodeSet(ctxt, ret);
232
35.7k
}
233
234
/**
235
 * exsltMathLowest:
236
 * @ns:  a node-set
237
 *
238
 * Implements the EXSLT - Math lowest() function
239
 *    node-set math:lowest (node-set)
240
 *
241
 * Returns the nodes in the node-set whose value is the minimum value
242
 *         for the node-set.
243
 */
244
static xmlNodeSetPtr
245
12.4k
exsltMathLowest (xmlNodeSetPtr ns) {
246
12.4k
    xmlNodeSetPtr ret = xmlXPathNodeSetCreate(NULL);
247
12.4k
    double min, cur;
248
12.4k
    int i;
249
250
12.4k
    if ((ns == NULL) || (ns->nodeNr == 0))
251
3.89k
  return(ret);
252
253
8.52k
    min = xmlXPathCastNodeToNumber(ns->nodeTab[0]);
254
8.52k
    if (xmlXPathIsNaN(min))
255
4.04k
  return(ret);
256
4.47k
    else
257
4.47k
  xmlXPathNodeSetAddUnique(ret, ns->nodeTab[0]);
258
259
31.4k
    for (i = 1; i < ns->nodeNr; i++) {
260
30.3k
  cur = xmlXPathCastNodeToNumber(ns->nodeTab[i]);
261
30.3k
  if (xmlXPathIsNaN(cur)) {
262
3.35k
      xmlXPathEmptyNodeSet(ret);
263
3.35k
      return(ret);
264
3.35k
  }
265
27.0k
        if (cur > min)
266
17.4k
      continue;
267
9.51k
  if (cur < min) {
268
2.10k
      min = cur;
269
2.10k
      xmlXPathEmptyNodeSet(ret);
270
2.10k
      xmlXPathNodeSetAddUnique(ret, ns->nodeTab[i]);
271
2.10k
            continue;
272
2.10k
  }
273
7.41k
  xmlXPathNodeSetAddUnique(ret, ns->nodeTab[i]);
274
7.41k
    }
275
1.12k
    return(ret);
276
4.47k
}
277
278
/**
279
 * exsltMathLowestFunction:
280
 * @ctxt:  an XPath parser context
281
 * @nargs:  the number of arguments
282
 *
283
 * Wraps #exsltMathLowest for use by the XPath processor
284
 */
285
static void
286
12.4k
exsltMathLowestFunction (xmlXPathParserContextPtr ctxt, int nargs) {
287
12.4k
    xmlNodeSetPtr ns, ret;
288
12.4k
    void *user = NULL;
289
290
291
12.4k
    if (nargs != 1) {
292
6
  xmlXPathSetArityError(ctxt);
293
6
  return;
294
6
    }
295
296
    /* We need to delay the freeing of value->user */
297
12.4k
    if ((ctxt->value != NULL) && (ctxt->value->boolval != 0)) {
298
6
        user = ctxt->value->user;
299
6
  ctxt->value->boolval = 0;
300
6
  ctxt->value->user = NULL;
301
6
    }
302
12.4k
    ns = xmlXPathPopNodeSet(ctxt);
303
12.4k
    if (xmlXPathCheckError(ctxt))
304
15
  return;
305
306
12.4k
    ret = exsltMathLowest(ns);
307
308
12.4k
    xmlXPathFreeNodeSet(ns);
309
12.4k
    if (user != NULL)
310
0
        xmlFreeNodeList((xmlNodePtr)user);
311
312
12.4k
    xmlXPathReturnNodeSet(ctxt, ret);
313
12.4k
}
314
315
/* math other functions */
316
317
/* constant values */
318
0
#define EXSLT_PI        (const xmlChar *) \
319
0
      "3.1415926535897932384626433832795028841971693993751"
320
48
#define EXSLT_E         (const xmlChar *) \
321
48
      "2.71828182845904523536028747135266249775724709369996"
322
0
#define EXSLT_SQRRT2    (const xmlChar *) \
323
0
      "1.41421356237309504880168872420969807856967187537694"
324
0
#define EXSLT_LN2       (const xmlChar *) \
325
0
      "0.69314718055994530941723212145817656807550013436025"
326
0
#define EXSLT_LN10      (const xmlChar *) \
327
0
      "2.30258509299404568402"
328
0
#define EXSLT_LOG2E     (const xmlChar *) \
329
0
      "1.4426950408889634074"
330
0
#define EXSLT_SQRT1_2   (const xmlChar *) \
331
0
      "0.70710678118654752440"
332
333
/**
334
 * exsltMathConstant
335
 * @name: string
336
 * @precision:  number
337
 *
338
 * Implements the EXSLT - Math constant function:
339
 *     number math:constant(string, number)
340
 *
341
 * Returns a number value of the given constant with the given precision or
342
 * xmlXPathNAN if name is unknown.
343
 * The constants are PI, E, SQRRT2, LN2, LN10, LOG2E, and SQRT1_2
344
 */
345
static double
346
642
exsltMathConstant (xmlChar *name, double precision) {
347
642
    xmlChar *str;
348
642
    double ret;
349
350
642
    if ((name == NULL) || (xmlXPathIsNaN(precision)) || (precision < 1.0)) {
351
387
        return xmlXPathNAN;
352
387
    }
353
354
255
    if (xmlStrEqual(name, BAD_CAST "PI")) {
355
0
        int len = xmlStrlen(EXSLT_PI);
356
357
0
        if (precision <= len)
358
0
            len = (int)precision;
359
360
0
        str = xmlStrsub(EXSLT_PI, 0, len);
361
362
255
    } else if (xmlStrEqual(name, BAD_CAST "E")) {
363
24
        int len = xmlStrlen(EXSLT_E);
364
365
24
        if (precision <= len)
366
22
            len = (int)precision;
367
368
24
        str = xmlStrsub(EXSLT_E, 0, len);
369
370
231
    } else if (xmlStrEqual(name, BAD_CAST "SQRRT2")) {
371
0
        int len = xmlStrlen(EXSLT_SQRRT2);
372
373
0
        if (precision <= len)
374
0
            len = (int)precision;
375
376
0
        str = xmlStrsub(EXSLT_SQRRT2, 0, len);
377
378
231
    } else if (xmlStrEqual(name, BAD_CAST "LN2")) {
379
0
        int len = xmlStrlen(EXSLT_LN2);
380
381
0
        if (precision <= len)
382
0
            len = (int)precision;
383
384
0
        str = xmlStrsub(EXSLT_LN2, 0, len);
385
386
231
    } else if (xmlStrEqual(name, BAD_CAST "LN10")) {
387
0
        int len = xmlStrlen(EXSLT_LN10);
388
389
0
        if (precision <= len)
390
0
            len = (int)precision;
391
392
0
        str = xmlStrsub(EXSLT_LN10, 0, len);
393
394
231
    } else if (xmlStrEqual(name, BAD_CAST "LOG2E")) {
395
0
        int len = xmlStrlen(EXSLT_LOG2E);
396
397
0
        if (precision <= len)
398
0
            len = (int)precision;
399
400
0
        str = xmlStrsub(EXSLT_LOG2E, 0, len);
401
402
231
    } else if (xmlStrEqual(name, BAD_CAST "SQRT1_2")) {
403
0
        int len = xmlStrlen(EXSLT_SQRT1_2);
404
405
0
        if (precision <= len)
406
0
            len = (int)precision;
407
408
0
        str = xmlStrsub(EXSLT_SQRT1_2, 0, len);
409
410
231
    } else {
411
231
  str = NULL;
412
231
    }
413
255
    if (str == NULL)
414
231
        return xmlXPathNAN;
415
24
    ret = xmlXPathCastStringToNumber(str);
416
24
    xmlFree(str);
417
24
    return ret;
418
255
}
419
420
/**
421
 * exsltMathConstantFunction:
422
 * @ctxt:  an XPath parser context
423
 * @nargs:  the number of arguments
424
 *
425
 * Wraps #exsltMathConstant for use by the XPath processor.
426
 */
427
static void
428
646
exsltMathConstantFunction (xmlXPathParserContextPtr ctxt, int nargs) {
429
646
    double   ret;
430
646
    xmlChar *name;
431
432
646
    if (nargs != 2) {
433
4
  xmlXPathSetArityError(ctxt);
434
4
  return;
435
4
    }
436
642
    ret = xmlXPathPopNumber(ctxt);
437
642
    if (xmlXPathCheckError(ctxt))
438
0
  return;
439
440
642
    name = xmlXPathPopString(ctxt);
441
642
    if (xmlXPathCheckError(ctxt))
442
0
  return;
443
444
642
    ret = exsltMathConstant(name, ret);
445
642
    if (name != NULL)
446
642
  xmlFree(name);
447
448
642
    xmlXPathReturnNumber(ctxt, ret);
449
642
}
450
451
/**
452
 * exsltMathRandom:
453
 *
454
 * Implements the EXSLT - Math random() function:
455
 *    number math:random ()
456
 *
457
 * Returns a random number between 0 and 1 inclusive.
458
 */
459
static double
460
23
exsltMathRandom (void) {
461
23
    double ret;
462
23
    int num;
463
464
23
    num = rand();
465
23
    ret = (double)num / (double)RAND_MAX;
466
23
    return(ret);
467
23
}
468
469
/**
470
 * exsltMathRandomFunction:
471
 * @ctxt:  an XPath parser context
472
 * @nargs:  the number of arguments
473
 *
474
 * Wraps #exsltMathRandom for use by the XPath processor.
475
 */
476
static void
477
27
exsltMathRandomFunction (xmlXPathParserContextPtr ctxt, int nargs) {
478
27
    double ret;
479
480
27
    if (nargs != 0) {
481
4
  xmlXPathSetArityError(ctxt);
482
4
  return;
483
4
    }
484
485
23
    ret = exsltMathRandom();
486
487
23
    xmlXPathReturnNumber(ctxt, ret);
488
23
}
489
490
/**
491
 * exsltMathAbs:
492
 * @num:  a double
493
 *
494
 * Implements the EXSLT - Math abs() function:
495
 *    number math:abs (number)
496
 *
497
 * Returns the absolute value of the argument, or xmlXPathNAN if @num is Nan.
498
 */
499
static double
500
5.99k
exsltMathAbs (double num) {
501
5.99k
    double ret;
502
503
5.99k
    if (xmlXPathIsNaN(num))
504
4.11k
  return(xmlXPathNAN);
505
1.88k
    ret = fabs(num);
506
1.88k
    return(ret);
507
5.99k
}
508
509
/**
510
 * exsltMathAbsFunction:
511
 * @ctxt:  an XPath parser context
512
 * @nargs:  the number of arguments
513
 *
514
 * Wraps #exsltMathAbs for use by the XPath processor.
515
 */
516
static void
517
6.00k
exsltMathAbsFunction (xmlXPathParserContextPtr ctxt, int nargs) {
518
6.00k
    double ret;
519
520
6.00k
    if (nargs != 1) {
521
9
  xmlXPathSetArityError(ctxt);
522
9
  return;
523
9
    }
524
5.99k
    ret = xmlXPathPopNumber(ctxt);
525
5.99k
    if (xmlXPathCheckError(ctxt))
526
0
  return;
527
528
5.99k
    ret = exsltMathAbs(ret);
529
530
5.99k
    xmlXPathReturnNumber(ctxt, ret);
531
5.99k
}
532
533
/**
534
 * exsltMathSqrt:
535
 * @num:  a double
536
 *
537
 * Implements the EXSLT - Math sqrt() function:
538
 *    number math:sqrt (number)
539
 *
540
 * Returns the square root of the argument, or xmlXPathNAN if @num is Nan.
541
 */
542
static double
543
732
exsltMathSqrt (double num) {
544
732
    double ret;
545
546
732
    if (xmlXPathIsNaN(num))
547
238
  return(xmlXPathNAN);
548
494
    ret = sqrt(num);
549
494
    return(ret);
550
732
}
551
552
/**
553
 * exsltMathSqrtFunction:
554
 * @ctxt:  an XPath parser context
555
 * @nargs:  the number of arguments
556
 *
557
 * Wraps #exsltMathSqrt for use by the XPath processor.
558
 */
559
static void
560
736
exsltMathSqrtFunction (xmlXPathParserContextPtr ctxt, int nargs) {
561
736
    double ret;
562
563
736
    if (nargs != 1) {
564
4
  xmlXPathSetArityError(ctxt);
565
4
  return;
566
4
    }
567
732
    ret = xmlXPathPopNumber(ctxt);
568
732
    if (xmlXPathCheckError(ctxt))
569
0
  return;
570
571
732
    ret = exsltMathSqrt(ret);
572
573
732
    xmlXPathReturnNumber(ctxt, ret);
574
732
}
575
576
/**
577
 * exsltMathPower:
578
 * @base:  a double
579
 * @power:  a double
580
 *
581
 * Implements the EXSLT - Math power() function:
582
 *    number math:power (number, number)
583
 *
584
 * Returns the power base and power arguments, or xmlXPathNAN
585
 * if either @base or @power is Nan.
586
 */
587
static double
588
1.60k
exsltMathPower (double base, double power) {
589
1.60k
    double ret;
590
591
1.60k
    if ((xmlXPathIsNaN(base) || xmlXPathIsNaN(power)))
592
858
  return(xmlXPathNAN);
593
743
    ret = pow(base, power);
594
743
    return(ret);
595
1.60k
}
596
597
/**
598
 * exsltMathPower:
599
 * @ctxt:  an XPath parser context
600
 * @nargs:  the number of arguments
601
 *
602
 * Wraps #exsltMathPower for use by the XPath processor.
603
 */
604
static void
605
1.60k
exsltMathPowerFunction (xmlXPathParserContextPtr ctxt, int nargs) {
606
1.60k
    double ret, base;
607
608
1.60k
    if (nargs != 2) {
609
4
  xmlXPathSetArityError(ctxt);
610
4
  return;
611
4
    }
612
1.60k
    ret = xmlXPathPopNumber(ctxt);
613
1.60k
    if (xmlXPathCheckError(ctxt))
614
0
  return;
615
616
    /* power */
617
1.60k
    base = xmlXPathPopNumber(ctxt);
618
1.60k
    if (xmlXPathCheckError(ctxt))
619
0
  return;
620
621
1.60k
    ret = exsltMathPower(base, ret);
622
623
1.60k
    xmlXPathReturnNumber(ctxt, ret);
624
1.60k
}
625
626
/**
627
 * exsltMathLog:
628
 * @num:  a double
629
 *
630
 * Implements the EXSLT - Math log() function:
631
 *    number math:log (number)
632
 *
633
 * Returns the natural log of the argument, or xmlXPathNAN if @num is Nan.
634
 */
635
static double
636
3.00k
exsltMathLog (double num) {
637
3.00k
    double ret;
638
639
3.00k
    if (xmlXPathIsNaN(num))
640
1.35k
  return(xmlXPathNAN);
641
1.64k
    ret = log(num);
642
1.64k
    return(ret);
643
3.00k
}
644
645
/**
646
 * exsltMathLogFunction:
647
 * @ctxt:  an XPath parser context
648
 * @nargs:  the number of arguments
649
 *
650
 * Wraps #exsltMathLog for use by the XPath processor.
651
 */
652
static void
653
3.01k
exsltMathLogFunction (xmlXPathParserContextPtr ctxt, int nargs) {
654
3.01k
    double ret;
655
656
3.01k
    if (nargs != 1) {
657
8
  xmlXPathSetArityError(ctxt);
658
8
  return;
659
8
    }
660
3.00k
    ret = xmlXPathPopNumber(ctxt);
661
3.00k
    if (xmlXPathCheckError(ctxt))
662
0
  return;
663
664
3.00k
    ret = exsltMathLog(ret);
665
666
3.00k
    xmlXPathReturnNumber(ctxt, ret);
667
3.00k
}
668
669
/**
670
 * exsltMathSin:
671
 * @num:  a double
672
 *
673
 * Implements the EXSLT - Math sin() function:
674
 *    number math:sin (number)
675
 *
676
 * Returns the sine of the argument, or xmlXPathNAN if @num is Nan.
677
 */
678
static double
679
7.47k
exsltMathSin (double num) {
680
7.47k
    double ret;
681
682
7.47k
    if (xmlXPathIsNaN(num))
683
4.45k
  return(xmlXPathNAN);
684
3.02k
    ret = sin(num);
685
3.02k
    return(ret);
686
7.47k
}
687
688
/**
689
 * exsltMathSinFunction:
690
 * @ctxt:  an XPath parser context
691
 * @nargs:  the number of arguments
692
 *
693
 * Wraps #exsltMathSin for use by the XPath processor.
694
 */
695
static void
696
7.48k
exsltMathSinFunction (xmlXPathParserContextPtr ctxt, int nargs) {
697
7.48k
    double ret;
698
699
7.48k
    if (nargs != 1) {
700
12
  xmlXPathSetArityError(ctxt);
701
12
  return;
702
12
    }
703
7.47k
    ret = xmlXPathPopNumber(ctxt);
704
7.47k
    if (xmlXPathCheckError(ctxt))
705
0
  return;
706
707
7.47k
    ret = exsltMathSin(ret);
708
709
7.47k
    xmlXPathReturnNumber(ctxt, ret);
710
7.47k
}
711
712
/**
713
 * exsltMathCos:
714
 * @num:  a double
715
 *
716
 * Implements the EXSLT - Math cos() function:
717
 *    number math:cos (number)
718
 *
719
 * Returns the cosine of the argument, or xmlXPathNAN if @num is Nan.
720
 */
721
static double
722
5.56k
exsltMathCos (double num) {
723
5.56k
    double ret;
724
725
5.56k
    if (xmlXPathIsNaN(num))
726
3.77k
  return(xmlXPathNAN);
727
1.79k
    ret = cos(num);
728
1.79k
    return(ret);
729
5.56k
}
730
731
/**
732
 * exsltMathCosFunction:
733
 * @ctxt:  an XPath parser context
734
 * @nargs:  the number of arguments
735
 *
736
 * Wraps #exsltMathCos for use by the XPath processor.
737
 */
738
static void
739
5.57k
exsltMathCosFunction (xmlXPathParserContextPtr ctxt, int nargs) {
740
5.57k
    double ret;
741
742
5.57k
    if (nargs != 1) {
743
9
  xmlXPathSetArityError(ctxt);
744
9
  return;
745
9
    }
746
5.56k
    ret = xmlXPathPopNumber(ctxt);
747
5.56k
    if (xmlXPathCheckError(ctxt))
748
0
  return;
749
750
5.56k
    ret = exsltMathCos(ret);
751
752
5.56k
    xmlXPathReturnNumber(ctxt, ret);
753
5.56k
}
754
755
/**
756
 * exsltMathTan:
757
 * @num:  a double
758
 *
759
 * Implements the EXSLT - Math tan() function:
760
 *    number math:tan (number)
761
 *
762
 * Returns the tangent of the argument, or xmlXPathNAN if @num is Nan.
763
 */
764
static double
765
14.7k
exsltMathTan (double num) {
766
14.7k
    double ret;
767
768
14.7k
    if (xmlXPathIsNaN(num))
769
11.5k
  return(xmlXPathNAN);
770
3.23k
    ret = tan(num);
771
3.23k
    return(ret);
772
14.7k
}
773
774
/**
775
 * exsltMathTanFunction:
776
 * @ctxt:  an XPath parser context
777
 * @nargs:  the number of arguments
778
 *
779
 * Wraps #exsltMathTan for use by the XPath processor.
780
 */
781
static void
782
14.7k
exsltMathTanFunction (xmlXPathParserContextPtr ctxt, int nargs) {
783
14.7k
    double ret;
784
785
14.7k
    if (nargs != 1) {
786
13
  xmlXPathSetArityError(ctxt);
787
13
  return;
788
13
    }
789
14.7k
    ret = xmlXPathPopNumber(ctxt);
790
14.7k
    if (xmlXPathCheckError(ctxt))
791
0
  return;
792
793
14.7k
    ret = exsltMathTan(ret);
794
795
14.7k
    xmlXPathReturnNumber(ctxt, ret);
796
14.7k
}
797
798
/**
799
 * exsltMathAsin:
800
 * @num:  a double
801
 *
802
 * Implements the EXSLT - Math asin() function:
803
 *    number math:asin (number)
804
 *
805
 * Returns the arc sine of the argument, or xmlXPathNAN if @num is Nan.
806
 */
807
static double
808
4.42k
exsltMathAsin (double num) {
809
4.42k
    double ret;
810
811
4.42k
    if (xmlXPathIsNaN(num))
812
2.53k
  return(xmlXPathNAN);
813
1.88k
    ret = asin(num);
814
1.88k
    return(ret);
815
4.42k
}
816
817
/**
818
 * exsltMathAsinFunction:
819
 * @ctxt:  an XPath parser context
820
 * @nargs:  the number of arguments
821
 *
822
 * Wraps #exsltMathAsin for use by the XPath processor.
823
 */
824
static void
825
4.43k
exsltMathAsinFunction (xmlXPathParserContextPtr ctxt, int nargs) {
826
4.43k
    double ret;
827
828
4.43k
    if (nargs != 1) {
829
14
  xmlXPathSetArityError(ctxt);
830
14
  return;
831
14
    }
832
4.42k
    ret = xmlXPathPopNumber(ctxt);
833
4.42k
    if (xmlXPathCheckError(ctxt))
834
0
  return;
835
836
4.42k
    ret = exsltMathAsin(ret);
837
838
4.42k
    xmlXPathReturnNumber(ctxt, ret);
839
4.42k
}
840
841
/**
842
 * exsltMathAcos:
843
 * @num:  a double
844
 *
845
 * Implements the EXSLT - Math acos() function:
846
 *    number math:acos (number)
847
 *
848
 * Returns the arc cosine of the argument, or xmlXPathNAN if @num is Nan.
849
 */
850
static double
851
1.94k
exsltMathAcos (double num) {
852
1.94k
    double ret;
853
854
1.94k
    if (xmlXPathIsNaN(num))
855
1.16k
  return(xmlXPathNAN);
856
775
    ret = acos(num);
857
775
    return(ret);
858
1.94k
}
859
860
/**
861
 * exsltMathAcosFunction:
862
 * @ctxt:  an XPath parser context
863
 * @nargs:  the number of arguments
864
 *
865
 * Wraps #exsltMathAcos for use by the XPath processor.
866
 */
867
static void
868
1.94k
exsltMathAcosFunction (xmlXPathParserContextPtr ctxt, int nargs) {
869
1.94k
    double ret;
870
871
1.94k
    if (nargs != 1) {
872
2
  xmlXPathSetArityError(ctxt);
873
2
  return;
874
2
    }
875
1.94k
    ret = xmlXPathPopNumber(ctxt);
876
1.94k
    if (xmlXPathCheckError(ctxt))
877
0
  return;
878
879
1.94k
    ret = exsltMathAcos(ret);
880
881
1.94k
    xmlXPathReturnNumber(ctxt, ret);
882
1.94k
}
883
884
/**
885
 * exsltMathAtan:
886
 * @num:  a double
887
 *
888
 * Implements the EXSLT - Math atan() function:
889
 *    number math:atan (number)
890
 *
891
 * Returns the arc tangent of the argument, or xmlXPathNAN if @num is Nan.
892
 */
893
static double
894
2.07k
exsltMathAtan (double num) {
895
2.07k
    double ret;
896
897
2.07k
    if (xmlXPathIsNaN(num))
898
1.04k
  return(xmlXPathNAN);
899
1.02k
    ret = atan(num);
900
1.02k
    return(ret);
901
2.07k
}
902
903
/**
904
 * exsltMathAtanFunction:
905
 * @ctxt:  an XPath parser context
906
 * @nargs:  the number of arguments
907
 *
908
 * Wraps #exsltMathAtan for use by the XPath processor.
909
 */
910
static void
911
2.08k
exsltMathAtanFunction (xmlXPathParserContextPtr ctxt, int nargs) {
912
2.08k
    double ret;
913
914
2.08k
    if (nargs != 1) {
915
7
  xmlXPathSetArityError(ctxt);
916
7
  return;
917
7
    }
918
2.07k
    ret = xmlXPathPopNumber(ctxt);
919
2.07k
    if (xmlXPathCheckError(ctxt))
920
0
  return;
921
922
2.07k
    ret = exsltMathAtan(ret);
923
924
2.07k
    xmlXPathReturnNumber(ctxt, ret);
925
2.07k
}
926
927
/**
928
 * exsltMathAtan2:
929
 * @y:  a double
930
 * @x:  a double
931
 *
932
 * Implements the EXSLT - Math atan2() function:
933
 *    number math:atan2 (number, number)
934
 *
935
 * Returns the arc tangent function of the y/x arguments, or xmlXPathNAN
936
 * if either @y or @x is Nan.
937
 */
938
static double
939
884
exsltMathAtan2 (double y, double x) {
940
884
    double ret;
941
942
884
    if ((xmlXPathIsNaN(y) || xmlXPathIsNaN(x)))
943
556
  return(xmlXPathNAN);
944
328
    ret = atan2(y, x);
945
328
    return(ret);
946
884
}
947
948
/**
949
 * exsltMathAtan2Function:
950
 * @ctxt:  an XPath parser context
951
 * @nargs:  the number of arguments
952
 *
953
 * Wraps #exsltMathAtan2 for use by the XPath processor.
954
 */
955
static void
956
892
exsltMathAtan2Function (xmlXPathParserContextPtr ctxt, int nargs) {
957
892
    double ret, x;
958
959
892
    if (nargs != 2) {
960
8
  xmlXPathSetArityError(ctxt);
961
8
  return;
962
8
    }
963
884
    x = xmlXPathPopNumber(ctxt);
964
884
    if (xmlXPathCheckError(ctxt))
965
0
  return;
966
967
    /* y */
968
884
    ret = xmlXPathPopNumber(ctxt);
969
884
    if (xmlXPathCheckError(ctxt))
970
0
  return;
971
972
884
    ret = exsltMathAtan2(ret, x);
973
974
884
    xmlXPathReturnNumber(ctxt, ret);
975
884
}
976
977
/**
978
 * exsltMathExp:
979
 * @num:  a double
980
 *
981
 * Implements the EXSLT - Math exp() function:
982
 *    number math:exp (number)
983
 *
984
 * Returns the exponential function of the argument, or xmlXPathNAN if
985
 * @num is Nan.
986
 */
987
static double
988
3.63k
exsltMathExp (double num) {
989
3.63k
    double ret;
990
991
3.63k
    if (xmlXPathIsNaN(num))
992
1.25k
  return(xmlXPathNAN);
993
2.38k
    ret = exp(num);
994
2.38k
    return(ret);
995
3.63k
}
996
997
/**
998
 * exsltMathExpFunction:
999
 * @ctxt:  an XPath parser context
1000
 * @nargs:  the number of arguments
1001
 *
1002
 * Wraps #exsltMathExp for use by the XPath processor.
1003
 */
1004
static void
1005
3.64k
exsltMathExpFunction (xmlXPathParserContextPtr ctxt, int nargs) {
1006
3.64k
    double ret;
1007
1008
3.64k
    if (nargs != 1) {
1009
11
  xmlXPathSetArityError(ctxt);
1010
11
  return;
1011
11
    }
1012
3.63k
    ret = xmlXPathPopNumber(ctxt);
1013
3.63k
    if (xmlXPathCheckError(ctxt))
1014
0
  return;
1015
1016
3.63k
    ret = exsltMathExp(ret);
1017
1018
3.63k
    xmlXPathReturnNumber(ctxt, ret);
1019
3.63k
}
1020
1021
/**
1022
 * exsltMathRegister:
1023
 *
1024
 * Registers the EXSLT - Math module
1025
 */
1026
1027
void
1028
3.70k
exsltMathRegister (void) {
1029
3.70k
    xsltRegisterExtModuleFunction ((const xmlChar *) "min",
1030
3.70k
           EXSLT_MATH_NAMESPACE,
1031
3.70k
           exsltMathMinFunction);
1032
3.70k
    xsltRegisterExtModuleFunction ((const xmlChar *) "max",
1033
3.70k
           EXSLT_MATH_NAMESPACE,
1034
3.70k
           exsltMathMaxFunction);
1035
3.70k
    xsltRegisterExtModuleFunction ((const xmlChar *) "highest",
1036
3.70k
           EXSLT_MATH_NAMESPACE,
1037
3.70k
           exsltMathHighestFunction);
1038
3.70k
    xsltRegisterExtModuleFunction ((const xmlChar *) "lowest",
1039
3.70k
           EXSLT_MATH_NAMESPACE,
1040
3.70k
           exsltMathLowestFunction);
1041
3.70k
    xsltRegisterExtModuleFunction ((const xmlChar *) "constant",
1042
3.70k
           EXSLT_MATH_NAMESPACE,
1043
3.70k
           exsltMathConstantFunction);
1044
3.70k
    xsltRegisterExtModuleFunction ((const xmlChar *) "random",
1045
3.70k
           EXSLT_MATH_NAMESPACE,
1046
3.70k
           exsltMathRandomFunction);
1047
3.70k
    xsltRegisterExtModuleFunction ((const xmlChar *) "abs",
1048
3.70k
           EXSLT_MATH_NAMESPACE,
1049
3.70k
           exsltMathAbsFunction);
1050
3.70k
    xsltRegisterExtModuleFunction ((const xmlChar *) "sqrt",
1051
3.70k
           EXSLT_MATH_NAMESPACE,
1052
3.70k
           exsltMathSqrtFunction);
1053
3.70k
    xsltRegisterExtModuleFunction ((const xmlChar *) "power",
1054
3.70k
           EXSLT_MATH_NAMESPACE,
1055
3.70k
           exsltMathPowerFunction);
1056
3.70k
    xsltRegisterExtModuleFunction ((const xmlChar *) "log",
1057
3.70k
           EXSLT_MATH_NAMESPACE,
1058
3.70k
           exsltMathLogFunction);
1059
3.70k
    xsltRegisterExtModuleFunction ((const xmlChar *) "sin",
1060
3.70k
           EXSLT_MATH_NAMESPACE,
1061
3.70k
           exsltMathSinFunction);
1062
3.70k
    xsltRegisterExtModuleFunction ((const xmlChar *) "cos",
1063
3.70k
           EXSLT_MATH_NAMESPACE,
1064
3.70k
           exsltMathCosFunction);
1065
3.70k
    xsltRegisterExtModuleFunction ((const xmlChar *) "tan",
1066
3.70k
           EXSLT_MATH_NAMESPACE,
1067
3.70k
           exsltMathTanFunction);
1068
3.70k
    xsltRegisterExtModuleFunction ((const xmlChar *) "asin",
1069
3.70k
           EXSLT_MATH_NAMESPACE,
1070
3.70k
           exsltMathAsinFunction);
1071
3.70k
    xsltRegisterExtModuleFunction ((const xmlChar *) "acos",
1072
3.70k
           EXSLT_MATH_NAMESPACE,
1073
3.70k
           exsltMathAcosFunction);
1074
3.70k
    xsltRegisterExtModuleFunction ((const xmlChar *) "atan",
1075
3.70k
           EXSLT_MATH_NAMESPACE,
1076
3.70k
           exsltMathAtanFunction);
1077
3.70k
    xsltRegisterExtModuleFunction ((const xmlChar *) "atan2",
1078
3.70k
           EXSLT_MATH_NAMESPACE,
1079
3.70k
           exsltMathAtan2Function);
1080
3.70k
    xsltRegisterExtModuleFunction ((const xmlChar *) "exp",
1081
3.70k
           EXSLT_MATH_NAMESPACE,
1082
3.70k
           exsltMathExpFunction);
1083
3.70k
}
1084
1085
/**
1086
 * exsltMathXpathCtxtRegister:
1087
 *
1088
 * Registers the EXSLT - Math module for use outside XSLT
1089
 */
1090
int
1091
exsltMathXpathCtxtRegister (xmlXPathContextPtr ctxt, const xmlChar *prefix)
1092
0
{
1093
0
    if (ctxt
1094
0
        && prefix
1095
0
        && !xmlXPathRegisterNs(ctxt,
1096
0
                               prefix,
1097
0
                               (const xmlChar *) EXSLT_MATH_NAMESPACE)
1098
0
        && !xmlXPathRegisterFuncNS(ctxt,
1099
0
                                   (const xmlChar *) "min",
1100
0
                                   (const xmlChar *) EXSLT_MATH_NAMESPACE,
1101
0
                                   exsltMathMinFunction)
1102
0
        && !xmlXPathRegisterFuncNS(ctxt,
1103
0
                                   (const xmlChar *) "max",
1104
0
                                   (const xmlChar *) EXSLT_MATH_NAMESPACE,
1105
0
                                   exsltMathMaxFunction)
1106
0
        && !xmlXPathRegisterFuncNS(ctxt,
1107
0
                                   (const xmlChar *) "highest",
1108
0
                                   (const xmlChar *) EXSLT_MATH_NAMESPACE,
1109
0
                                   exsltMathHighestFunction)
1110
0
        && !xmlXPathRegisterFuncNS(ctxt,
1111
0
                                   (const xmlChar *) "lowest",
1112
0
                                   (const xmlChar *) EXSLT_MATH_NAMESPACE,
1113
0
                                   exsltMathLowestFunction)
1114
0
        && !xmlXPathRegisterFuncNS(ctxt,
1115
0
                                   (const xmlChar *) "random",
1116
0
                                   (const xmlChar *) EXSLT_MATH_NAMESPACE,
1117
0
                                   exsltMathRandomFunction)
1118
0
        && !xmlXPathRegisterFuncNS(ctxt,
1119
0
                                   (const xmlChar *) "abs",
1120
0
                                   (const xmlChar *) EXSLT_MATH_NAMESPACE,
1121
0
                                   exsltMathAbsFunction)
1122
0
        && !xmlXPathRegisterFuncNS(ctxt,
1123
0
                                   (const xmlChar *) "sqrt",
1124
0
                                   (const xmlChar *) EXSLT_MATH_NAMESPACE,
1125
0
                                   exsltMathSqrtFunction)
1126
0
        && !xmlXPathRegisterFuncNS(ctxt,
1127
0
                                   (const xmlChar *) "power",
1128
0
                                   (const xmlChar *) EXSLT_MATH_NAMESPACE,
1129
0
                                   exsltMathPowerFunction)
1130
0
        && !xmlXPathRegisterFuncNS(ctxt,
1131
0
                                   (const xmlChar *) "log",
1132
0
                                   (const xmlChar *) EXSLT_MATH_NAMESPACE,
1133
0
                                   exsltMathLogFunction)
1134
0
        && !xmlXPathRegisterFuncNS(ctxt,
1135
0
                                   (const xmlChar *) "sin",
1136
0
                                   (const xmlChar *) EXSLT_MATH_NAMESPACE,
1137
0
                                   exsltMathSinFunction)
1138
0
        && !xmlXPathRegisterFuncNS(ctxt,
1139
0
                                   (const xmlChar *) "cos",
1140
0
                                   (const xmlChar *) EXSLT_MATH_NAMESPACE,
1141
0
                                   exsltMathCosFunction)
1142
0
        && !xmlXPathRegisterFuncNS(ctxt,
1143
0
                                   (const xmlChar *) "tan",
1144
0
                                   (const xmlChar *) EXSLT_MATH_NAMESPACE,
1145
0
                                   exsltMathTanFunction)
1146
0
        && !xmlXPathRegisterFuncNS(ctxt,
1147
0
                                   (const xmlChar *) "asin",
1148
0
                                   (const xmlChar *) EXSLT_MATH_NAMESPACE,
1149
0
                                   exsltMathAsinFunction)
1150
0
        && !xmlXPathRegisterFuncNS(ctxt,
1151
0
                                   (const xmlChar *) "acos",
1152
0
                                   (const xmlChar *) EXSLT_MATH_NAMESPACE,
1153
0
                                   exsltMathAcosFunction)
1154
0
        && !xmlXPathRegisterFuncNS(ctxt,
1155
0
                                   (const xmlChar *) "atan",
1156
0
                                   (const xmlChar *) EXSLT_MATH_NAMESPACE,
1157
0
                                   exsltMathAtanFunction)
1158
0
        && !xmlXPathRegisterFuncNS(ctxt,
1159
0
                                   (const xmlChar *) "atan2",
1160
0
                                   (const xmlChar *) EXSLT_MATH_NAMESPACE,
1161
0
                                   exsltMathAtan2Function)
1162
0
        && !xmlXPathRegisterFuncNS(ctxt,
1163
0
                                   (const xmlChar *) "exp",
1164
0
                                   (const xmlChar *) EXSLT_MATH_NAMESPACE,
1165
0
                                   exsltMathExpFunction)
1166
0
        && !xmlXPathRegisterFuncNS(ctxt,
1167
0
                                   (const xmlChar *) "constant",
1168
0
                                   (const xmlChar *) EXSLT_MATH_NAMESPACE,
1169
0
                                   exsltMathConstantFunction)) {
1170
0
        return 0;
1171
0
    }
1172
0
    return -1;
1173
0
}