Coverage Report

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