Coverage Report

Created: 2025-03-12 04:16

/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
83
exsltMathMin (xmlNodeSetPtr ns) {
30
83
    double ret, cur;
31
83
    int i;
32
33
83
    if ((ns == NULL) || (ns->nodeNr == 0))
34
6
  return(xmlXPathNAN);
35
77
    ret = xmlXPathCastNodeToNumber(ns->nodeTab[0]);
36
77
    if (xmlXPathIsNaN(ret))
37
13
  return(xmlXPathNAN);
38
130
    for (i = 1; i < ns->nodeNr; i++) {
39
117
  cur = xmlXPathCastNodeToNumber(ns->nodeTab[i]);
40
117
  if (xmlXPathIsNaN(cur))
41
51
      return(xmlXPathNAN);
42
66
  if (cur < ret)
43
48
      ret = cur;
44
66
    }
45
13
    return(ret);
46
64
}
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
90
exsltMathMinFunction (xmlXPathParserContextPtr ctxt, int nargs) {
57
90
    xmlNodeSetPtr ns;
58
90
    double ret;
59
90
    void *user = NULL;
60
61
90
    if (nargs != 1) {
62
4
  xsltGenericError(xsltGenericErrorContext,
63
4
       "math:min: invalid number of arguments\n");
64
4
  ctxt->error = XPATH_INVALID_ARITY;
65
4
  return;
66
4
    }
67
    /* We need to delay the freeing of value->user */
68
86
    if ((ctxt->value != NULL) && (ctxt->value->boolval != 0)) {
69
1
        user = ctxt->value->user;
70
1
  ctxt->value->boolval = 0;
71
1
  ctxt->value->user = NULL;
72
1
    }
73
86
    ns = xmlXPathPopNodeSet(ctxt);
74
86
    if (xmlXPathCheckError(ctxt))
75
3
  return;
76
77
83
    ret = exsltMathMin(ns);
78
79
83
    xmlXPathFreeNodeSet(ns);
80
83
    if (user != NULL)
81
0
        xmlFreeNodeList((xmlNodePtr)user);
82
83
83
    xmlXPathReturnNumber(ctxt, ret);
84
83
}
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
102
exsltMathMax (xmlNodeSetPtr ns) {
99
102
    double ret, cur;
100
102
    int i;
101
102
102
    if ((ns == NULL) || (ns->nodeNr == 0))
103
12
  return(xmlXPathNAN);
104
90
    ret = xmlXPathCastNodeToNumber(ns->nodeTab[0]);
105
90
    if (xmlXPathIsNaN(ret))
106
17
  return(xmlXPathNAN);
107
178
    for (i = 1; i < ns->nodeNr; i++) {
108
159
  cur = xmlXPathCastNodeToNumber(ns->nodeTab[i]);
109
159
  if (xmlXPathIsNaN(cur))
110
54
      return(xmlXPathNAN);
111
105
  if (cur > ret)
112
6
      ret = cur;
113
105
    }
114
19
    return(ret);
115
73
}
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
111
exsltMathMaxFunction (xmlXPathParserContextPtr ctxt, int nargs) {
126
111
    xmlNodeSetPtr ns;
127
111
    double ret;
128
111
    void *user = NULL;
129
130
111
    if (nargs != 1) {
131
4
  xmlXPathSetArityError(ctxt);
132
4
  return;
133
4
    }
134
135
    /* We need to delay the freeing of value->user */
136
107
    if ((ctxt->value != NULL) && (ctxt->value->boolval != 0)) {
137
2
  user = ctxt->value->user;
138
2
  ctxt->value->boolval = 0;
139
2
  ctxt->value->user = 0;
140
2
    }
141
107
    ns = xmlXPathPopNodeSet(ctxt);
142
107
    if (xmlXPathCheckError(ctxt))
143
5
  return;
144
145
102
    ret = exsltMathMax(ns);
146
147
102
    xmlXPathFreeNodeSet(ns);
148
149
102
    if (user != NULL)
150
0
        xmlFreeNodeList((xmlNodePtr)user);
151
102
    xmlXPathReturnNumber(ctxt, ret);
152
102
}
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
39
exsltMathHighest (xmlNodeSetPtr ns) {
166
39
    xmlNodeSetPtr ret = xmlXPathNodeSetCreate(NULL);
167
39
    double max, cur;
168
39
    int i;
169
170
39
    if ((ns == NULL) || (ns->nodeNr == 0))
171
5
  return(ret);
172
173
34
    max = xmlXPathCastNodeToNumber(ns->nodeTab[0]);
174
34
    if (xmlXPathIsNaN(max))
175
8
  return(ret);
176
26
    else
177
26
  xmlXPathNodeSetAddUnique(ret, ns->nodeTab[0]);
178
179
66
    for (i = 1; i < ns->nodeNr; i++) {
180
55
  cur = xmlXPathCastNodeToNumber(ns->nodeTab[i]);
181
55
  if (xmlXPathIsNaN(cur)) {
182
15
      xmlXPathEmptyNodeSet(ret);
183
15
      return(ret);
184
15
  }
185
40
  if (cur < max)
186
37
      continue;
187
3
  if (cur > max) {
188
3
      max = cur;
189
3
      xmlXPathEmptyNodeSet(ret);
190
3
      xmlXPathNodeSetAddUnique(ret, ns->nodeTab[i]);
191
3
      continue;
192
3
  }
193
0
  xmlXPathNodeSetAddUnique(ret, ns->nodeTab[i]);
194
0
    }
195
11
    return(ret);
196
26
}
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
47
exsltMathHighestFunction (xmlXPathParserContextPtr ctxt, int nargs) {
207
47
    xmlNodeSetPtr ns, ret;
208
47
    void *user = NULL;
209
210
47
    if (nargs != 1) {
211
4
  xmlXPathSetArityError(ctxt);
212
4
  return;
213
4
    }
214
215
    /* We need to delay the freeing of value->user */
216
43
    if ((ctxt->value != NULL) && ctxt->value->boolval != 0) {
217
1
        user = ctxt->value->user;
218
1
  ctxt->value->boolval = 0;
219
1
  ctxt->value->user = NULL;
220
1
    }
221
43
    ns = xmlXPathPopNodeSet(ctxt);
222
43
    if (xmlXPathCheckError(ctxt))
223
4
  return;
224
225
39
    ret = exsltMathHighest(ns);
226
227
39
    xmlXPathFreeNodeSet(ns);
228
39
    if (user != NULL)
229
0
        xmlFreeNodeList((xmlNodePtr)user);
230
231
39
    xmlXPathReturnNodeSet(ctxt, ret);
232
39
}
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
63
exsltMathLowest (xmlNodeSetPtr ns) {
246
63
    xmlNodeSetPtr ret = xmlXPathNodeSetCreate(NULL);
247
63
    double min, cur;
248
63
    int i;
249
250
63
    if ((ns == NULL) || (ns->nodeNr == 0))
251
5
  return(ret);
252
253
58
    min = xmlXPathCastNodeToNumber(ns->nodeTab[0]);
254
58
    if (xmlXPathIsNaN(min))
255
18
  return(ret);
256
40
    else
257
40
  xmlXPathNodeSetAddUnique(ret, ns->nodeTab[0]);
258
259
109
    for (i = 1; i < ns->nodeNr; i++) {
260
93
  cur = xmlXPathCastNodeToNumber(ns->nodeTab[i]);
261
93
  if (xmlXPathIsNaN(cur)) {
262
24
      xmlXPathEmptyNodeSet(ret);
263
24
      return(ret);
264
24
  }
265
69
        if (cur > min)
266
21
      continue;
267
48
  if (cur < min) {
268
45
      min = cur;
269
45
      xmlXPathEmptyNodeSet(ret);
270
45
      xmlXPathNodeSetAddUnique(ret, ns->nodeTab[i]);
271
45
            continue;
272
45
  }
273
3
  xmlXPathNodeSetAddUnique(ret, ns->nodeTab[i]);
274
3
    }
275
16
    return(ret);
276
40
}
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
69
exsltMathLowestFunction (xmlXPathParserContextPtr ctxt, int nargs) {
287
69
    xmlNodeSetPtr ns, ret;
288
69
    void *user = NULL;
289
290
291
69
    if (nargs != 1) {
292
4
  xmlXPathSetArityError(ctxt);
293
4
  return;
294
4
    }
295
296
    /* We need to delay the freeing of value->user */
297
65
    if ((ctxt->value != NULL) && (ctxt->value->boolval != 0)) {
298
1
        user = ctxt->value->user;
299
1
  ctxt->value->boolval = 0;
300
1
  ctxt->value->user = NULL;
301
1
    }
302
65
    ns = xmlXPathPopNodeSet(ctxt);
303
65
    if (xmlXPathCheckError(ctxt))
304
2
  return;
305
306
63
    ret = exsltMathLowest(ns);
307
308
63
    xmlXPathFreeNodeSet(ns);
309
63
    if (user != NULL)
310
0
        xmlFreeNodeList((xmlNodePtr)user);
311
312
63
    xmlXPathReturnNodeSet(ctxt, ret);
313
63
}
314
315
/* math other functions */
316
317
/* constant values */
318
0
#define EXSLT_PI        (const xmlChar *) \
319
0
      "3.1415926535897932384626433832795028841971693993751"
320
12
#define EXSLT_E         (const xmlChar *) \
321
12
      "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
11
exsltMathConstant (xmlChar *name, double precision) {
347
11
    xmlChar *str;
348
11
    double ret;
349
350
11
    if ((name == NULL) || (xmlXPathIsNaN(precision)) || (precision < 1.0)) {
351
3
        return xmlXPathNAN;
352
3
    }
353
354
8
    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
8
    } else if (xmlStrEqual(name, BAD_CAST "E")) {
363
6
        int len = xmlStrlen(EXSLT_E);
364
365
6
        if (precision <= len)
366
6
            len = (int)precision;
367
368
6
        str = xmlStrsub(EXSLT_E, 0, len);
369
370
6
    } 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
2
    } 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
2
    } 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
2
    } 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
2
    } 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
2
    } else {
411
2
  str = NULL;
412
2
    }
413
8
    if (str == NULL)
414
2
        return xmlXPathNAN;
415
6
    ret = xmlXPathCastStringToNumber(str);
416
6
    xmlFree(str);
417
6
    return ret;
418
8
}
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
15
exsltMathConstantFunction (xmlXPathParserContextPtr ctxt, int nargs) {
429
15
    double   ret;
430
15
    xmlChar *name;
431
432
15
    if (nargs != 2) {
433
4
  xmlXPathSetArityError(ctxt);
434
4
  return;
435
4
    }
436
11
    ret = xmlXPathPopNumber(ctxt);
437
11
    if (xmlXPathCheckError(ctxt))
438
0
  return;
439
440
11
    name = xmlXPathPopString(ctxt);
441
11
    if (xmlXPathCheckError(ctxt))
442
0
  return;
443
444
11
    ret = exsltMathConstant(name, ret);
445
11
    if (name != NULL)
446
11
  xmlFree(name);
447
448
11
    xmlXPathReturnNumber(ctxt, ret);
449
11
}
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
5
exsltMathRandom (void) {
461
5
    double ret;
462
5
    int num;
463
464
5
    num = rand();
465
5
    ret = (double)num / (double)RAND_MAX;
466
5
    return(ret);
467
5
}
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
6
exsltMathRandomFunction (xmlXPathParserContextPtr ctxt, int nargs) {
478
6
    double ret;
479
480
6
    if (nargs != 0) {
481
1
  xmlXPathSetArityError(ctxt);
482
1
  return;
483
1
    }
484
485
5
    ret = exsltMathRandom();
486
487
5
    xmlXPathReturnNumber(ctxt, ret);
488
5
}
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
7
exsltMathAbs (double num) {
501
7
    double ret;
502
503
7
    if (xmlXPathIsNaN(num))
504
0
  return(xmlXPathNAN);
505
7
    ret = fabs(num);
506
7
    return(ret);
507
7
}
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
9
exsltMathAbsFunction (xmlXPathParserContextPtr ctxt, int nargs) {
518
9
    double ret;
519
520
9
    if (nargs != 1) {
521
2
  xmlXPathSetArityError(ctxt);
522
2
  return;
523
2
    }
524
7
    ret = xmlXPathPopNumber(ctxt);
525
7
    if (xmlXPathCheckError(ctxt))
526
0
  return;
527
528
7
    ret = exsltMathAbs(ret);
529
530
7
    xmlXPathReturnNumber(ctxt, ret);
531
7
}
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
9
exsltMathSqrt (double num) {
544
9
    double ret;
545
546
9
    if (xmlXPathIsNaN(num))
547
4
  return(xmlXPathNAN);
548
5
    ret = sqrt(num);
549
5
    return(ret);
550
9
}
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
14
exsltMathSqrtFunction (xmlXPathParserContextPtr ctxt, int nargs) {
561
14
    double ret;
562
563
14
    if (nargs != 1) {
564
5
  xmlXPathSetArityError(ctxt);
565
5
  return;
566
5
    }
567
9
    ret = xmlXPathPopNumber(ctxt);
568
9
    if (xmlXPathCheckError(ctxt))
569
0
  return;
570
571
9
    ret = exsltMathSqrt(ret);
572
573
9
    xmlXPathReturnNumber(ctxt, ret);
574
9
}
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
12
exsltMathPower (double base, double power) {
589
12
    double ret;
590
591
12
    if ((xmlXPathIsNaN(base) || xmlXPathIsNaN(power)))
592
6
  return(xmlXPathNAN);
593
6
    ret = pow(base, power);
594
6
    return(ret);
595
12
}
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
16
exsltMathPowerFunction (xmlXPathParserContextPtr ctxt, int nargs) {
606
16
    double ret, base;
607
608
16
    if (nargs != 2) {
609
4
  xmlXPathSetArityError(ctxt);
610
4
  return;
611
4
    }
612
12
    ret = xmlXPathPopNumber(ctxt);
613
12
    if (xmlXPathCheckError(ctxt))
614
0
  return;
615
616
    /* power */
617
12
    base = xmlXPathPopNumber(ctxt);
618
12
    if (xmlXPathCheckError(ctxt))
619
0
  return;
620
621
12
    ret = exsltMathPower(base, ret);
622
623
12
    xmlXPathReturnNumber(ctxt, ret);
624
12
}
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
9
exsltMathLog (double num) {
637
9
    double ret;
638
639
9
    if (xmlXPathIsNaN(num))
640
3
  return(xmlXPathNAN);
641
6
    ret = log(num);
642
6
    return(ret);
643
9
}
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
13
exsltMathLogFunction (xmlXPathParserContextPtr ctxt, int nargs) {
654
13
    double ret;
655
656
13
    if (nargs != 1) {
657
4
  xmlXPathSetArityError(ctxt);
658
4
  return;
659
4
    }
660
9
    ret = xmlXPathPopNumber(ctxt);
661
9
    if (xmlXPathCheckError(ctxt))
662
0
  return;
663
664
9
    ret = exsltMathLog(ret);
665
666
9
    xmlXPathReturnNumber(ctxt, ret);
667
9
}
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
9
exsltMathSin (double num) {
680
9
    double ret;
681
682
9
    if (xmlXPathIsNaN(num))
683
4
  return(xmlXPathNAN);
684
5
    ret = sin(num);
685
5
    return(ret);
686
9
}
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
13
exsltMathSinFunction (xmlXPathParserContextPtr ctxt, int nargs) {
697
13
    double ret;
698
699
13
    if (nargs != 1) {
700
4
  xmlXPathSetArityError(ctxt);
701
4
  return;
702
4
    }
703
9
    ret = xmlXPathPopNumber(ctxt);
704
9
    if (xmlXPathCheckError(ctxt))
705
0
  return;
706
707
9
    ret = exsltMathSin(ret);
708
709
9
    xmlXPathReturnNumber(ctxt, ret);
710
9
}
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
14
exsltMathCos (double num) {
723
14
    double ret;
724
725
14
    if (xmlXPathIsNaN(num))
726
9
  return(xmlXPathNAN);
727
5
    ret = cos(num);
728
5
    return(ret);
729
14
}
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
18
exsltMathCosFunction (xmlXPathParserContextPtr ctxt, int nargs) {
740
18
    double ret;
741
742
18
    if (nargs != 1) {
743
4
  xmlXPathSetArityError(ctxt);
744
4
  return;
745
4
    }
746
14
    ret = xmlXPathPopNumber(ctxt);
747
14
    if (xmlXPathCheckError(ctxt))
748
0
  return;
749
750
14
    ret = exsltMathCos(ret);
751
752
14
    xmlXPathReturnNumber(ctxt, ret);
753
14
}
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
exsltMathTan (double num) {
766
14
    double ret;
767
768
14
    if (xmlXPathIsNaN(num))
769
6
  return(xmlXPathNAN);
770
8
    ret = tan(num);
771
8
    return(ret);
772
14
}
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
18
exsltMathTanFunction (xmlXPathParserContextPtr ctxt, int nargs) {
783
18
    double ret;
784
785
18
    if (nargs != 1) {
786
4
  xmlXPathSetArityError(ctxt);
787
4
  return;
788
4
    }
789
14
    ret = xmlXPathPopNumber(ctxt);
790
14
    if (xmlXPathCheckError(ctxt))
791
0
  return;
792
793
14
    ret = exsltMathTan(ret);
794
795
14
    xmlXPathReturnNumber(ctxt, ret);
796
14
}
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
8
exsltMathAsin (double num) {
809
8
    double ret;
810
811
8
    if (xmlXPathIsNaN(num))
812
3
  return(xmlXPathNAN);
813
5
    ret = asin(num);
814
5
    return(ret);
815
8
}
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
10
exsltMathAsinFunction (xmlXPathParserContextPtr ctxt, int nargs) {
826
10
    double ret;
827
828
10
    if (nargs != 1) {
829
2
  xmlXPathSetArityError(ctxt);
830
2
  return;
831
2
    }
832
8
    ret = xmlXPathPopNumber(ctxt);
833
8
    if (xmlXPathCheckError(ctxt))
834
0
  return;
835
836
8
    ret = exsltMathAsin(ret);
837
838
8
    xmlXPathReturnNumber(ctxt, ret);
839
8
}
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
7
exsltMathAcos (double num) {
852
7
    double ret;
853
854
7
    if (xmlXPathIsNaN(num))
855
2
  return(xmlXPathNAN);
856
5
    ret = acos(num);
857
5
    return(ret);
858
7
}
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
10
exsltMathAcosFunction (xmlXPathParserContextPtr ctxt, int nargs) {
869
10
    double ret;
870
871
10
    if (nargs != 1) {
872
3
  xmlXPathSetArityError(ctxt);
873
3
  return;
874
3
    }
875
7
    ret = xmlXPathPopNumber(ctxt);
876
7
    if (xmlXPathCheckError(ctxt))
877
0
  return;
878
879
7
    ret = exsltMathAcos(ret);
880
881
7
    xmlXPathReturnNumber(ctxt, ret);
882
7
}
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
7
exsltMathAtan (double num) {
895
7
    double ret;
896
897
7
    if (xmlXPathIsNaN(num))
898
2
  return(xmlXPathNAN);
899
5
    ret = atan(num);
900
5
    return(ret);
901
7
}
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
9
exsltMathAtanFunction (xmlXPathParserContextPtr ctxt, int nargs) {
912
9
    double ret;
913
914
9
    if (nargs != 1) {
915
2
  xmlXPathSetArityError(ctxt);
916
2
  return;
917
2
    }
918
7
    ret = xmlXPathPopNumber(ctxt);
919
7
    if (xmlXPathCheckError(ctxt))
920
0
  return;
921
922
7
    ret = exsltMathAtan(ret);
923
924
7
    xmlXPathReturnNumber(ctxt, ret);
925
7
}
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
8
exsltMathAtan2 (double y, double x) {
940
8
    double ret;
941
942
8
    if ((xmlXPathIsNaN(y) || xmlXPathIsNaN(x)))
943
2
  return(xmlXPathNAN);
944
6
    ret = atan2(y, x);
945
6
    return(ret);
946
8
}
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
12
exsltMathAtan2Function (xmlXPathParserContextPtr ctxt, int nargs) {
957
12
    double ret, x;
958
959
12
    if (nargs != 2) {
960
4
  xmlXPathSetArityError(ctxt);
961
4
  return;
962
4
    }
963
8
    x = xmlXPathPopNumber(ctxt);
964
8
    if (xmlXPathCheckError(ctxt))
965
0
  return;
966
967
    /* y */
968
8
    ret = xmlXPathPopNumber(ctxt);
969
8
    if (xmlXPathCheckError(ctxt))
970
0
  return;
971
972
8
    ret = exsltMathAtan2(ret, x);
973
974
8
    xmlXPathReturnNumber(ctxt, ret);
975
8
}
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
10
exsltMathExp (double num) {
989
10
    double ret;
990
991
10
    if (xmlXPathIsNaN(num))
992
5
  return(xmlXPathNAN);
993
5
    ret = exp(num);
994
5
    return(ret);
995
10
}
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
13
exsltMathExpFunction (xmlXPathParserContextPtr ctxt, int nargs) {
1006
13
    double ret;
1007
1008
13
    if (nargs != 1) {
1009
3
  xmlXPathSetArityError(ctxt);
1010
3
  return;
1011
3
    }
1012
10
    ret = xmlXPathPopNumber(ctxt);
1013
10
    if (xmlXPathCheckError(ctxt))
1014
0
  return;
1015
1016
10
    ret = exsltMathExp(ret);
1017
1018
10
    xmlXPathReturnNumber(ctxt, ret);
1019
10
}
1020
1021
/**
1022
 * exsltMathRegister:
1023
 *
1024
 * Registers the EXSLT - Math module
1025
 */
1026
1027
void
1028
50
exsltMathRegister (void) {
1029
50
    xsltRegisterExtModuleFunction ((const xmlChar *) "min",
1030
50
           EXSLT_MATH_NAMESPACE,
1031
50
           exsltMathMinFunction);
1032
50
    xsltRegisterExtModuleFunction ((const xmlChar *) "max",
1033
50
           EXSLT_MATH_NAMESPACE,
1034
50
           exsltMathMaxFunction);
1035
50
    xsltRegisterExtModuleFunction ((const xmlChar *) "highest",
1036
50
           EXSLT_MATH_NAMESPACE,
1037
50
           exsltMathHighestFunction);
1038
50
    xsltRegisterExtModuleFunction ((const xmlChar *) "lowest",
1039
50
           EXSLT_MATH_NAMESPACE,
1040
50
           exsltMathLowestFunction);
1041
50
    xsltRegisterExtModuleFunction ((const xmlChar *) "constant",
1042
50
           EXSLT_MATH_NAMESPACE,
1043
50
           exsltMathConstantFunction);
1044
50
    xsltRegisterExtModuleFunction ((const xmlChar *) "random",
1045
50
           EXSLT_MATH_NAMESPACE,
1046
50
           exsltMathRandomFunction);
1047
50
    xsltRegisterExtModuleFunction ((const xmlChar *) "abs",
1048
50
           EXSLT_MATH_NAMESPACE,
1049
50
           exsltMathAbsFunction);
1050
50
    xsltRegisterExtModuleFunction ((const xmlChar *) "sqrt",
1051
50
           EXSLT_MATH_NAMESPACE,
1052
50
           exsltMathSqrtFunction);
1053
50
    xsltRegisterExtModuleFunction ((const xmlChar *) "power",
1054
50
           EXSLT_MATH_NAMESPACE,
1055
50
           exsltMathPowerFunction);
1056
50
    xsltRegisterExtModuleFunction ((const xmlChar *) "log",
1057
50
           EXSLT_MATH_NAMESPACE,
1058
50
           exsltMathLogFunction);
1059
50
    xsltRegisterExtModuleFunction ((const xmlChar *) "sin",
1060
50
           EXSLT_MATH_NAMESPACE,
1061
50
           exsltMathSinFunction);
1062
50
    xsltRegisterExtModuleFunction ((const xmlChar *) "cos",
1063
50
           EXSLT_MATH_NAMESPACE,
1064
50
           exsltMathCosFunction);
1065
50
    xsltRegisterExtModuleFunction ((const xmlChar *) "tan",
1066
50
           EXSLT_MATH_NAMESPACE,
1067
50
           exsltMathTanFunction);
1068
50
    xsltRegisterExtModuleFunction ((const xmlChar *) "asin",
1069
50
           EXSLT_MATH_NAMESPACE,
1070
50
           exsltMathAsinFunction);
1071
50
    xsltRegisterExtModuleFunction ((const xmlChar *) "acos",
1072
50
           EXSLT_MATH_NAMESPACE,
1073
50
           exsltMathAcosFunction);
1074
50
    xsltRegisterExtModuleFunction ((const xmlChar *) "atan",
1075
50
           EXSLT_MATH_NAMESPACE,
1076
50
           exsltMathAtanFunction);
1077
50
    xsltRegisterExtModuleFunction ((const xmlChar *) "atan2",
1078
50
           EXSLT_MATH_NAMESPACE,
1079
50
           exsltMathAtan2Function);
1080
50
    xsltRegisterExtModuleFunction ((const xmlChar *) "exp",
1081
50
           EXSLT_MATH_NAMESPACE,
1082
50
           exsltMathExpFunction);
1083
50
}
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
}