xref: /php-src/ext/bcmath/bcmath.c (revision 2c8662d6)
1 /*
2    +----------------------------------------------------------------------+
3    | Copyright (c) The PHP Group                                          |
4    +----------------------------------------------------------------------+
5    | This source file is subject to version 3.01 of the PHP license,      |
6    | that is bundled with this package in the file LICENSE, and is        |
7    | available through the world-wide-web at the following url:           |
8    | https://www.php.net/license/3_01.txt                                 |
9    | If you did not receive a copy of the PHP license and are unable to   |
10    | obtain it through the world-wide-web, please send a note to          |
11    | license@php.net so we can mail you a copy immediately.               |
12    +----------------------------------------------------------------------+
13    | Author: Andi Gutmans <andi@php.net>                                  |
14    +----------------------------------------------------------------------+
15 */
16 
17 #ifdef HAVE_CONFIG_H
18 #include <config.h>
19 #endif
20 
21 #include "php.h"
22 
23 #ifdef HAVE_BCMATH
24 
25 #include "php_ini.h"
26 #include "zend_exceptions.h"
27 #include "zend_interfaces.h"
28 #include "bcmath_arginfo.h"
29 #include "ext/standard/info.h"
30 #include "php_bcmath.h"
31 #include "libbcmath/src/bcmath.h"
32 
33 /* Always pair SETUP with TEARDOWN, and do so in the outer scope!
34  * Should not be used when data can escape the function. */
35 #define BC_ARENA_SETUP \
36 	char bc_arena[BC_ARENA_SIZE]; \
37 	BCG(arena) = bc_arena;
38 
39 #define BC_ARENA_TEARDOWN \
40 	BCG(arena) = NULL; \
41 	BCG(arena_offset) = 0;
42 
43 ZEND_DECLARE_MODULE_GLOBALS(bcmath)
44 static PHP_GINIT_FUNCTION(bcmath);
45 static PHP_GSHUTDOWN_FUNCTION(bcmath);
46 static PHP_MINIT_FUNCTION(bcmath);
47 static PHP_MSHUTDOWN_FUNCTION(bcmath);
48 static PHP_MINFO_FUNCTION(bcmath);
49 
50 zend_module_entry bcmath_module_entry = {
51 	STANDARD_MODULE_HEADER,
52 	"bcmath",
53 	ext_functions,
54 	PHP_MINIT(bcmath),
55 	PHP_MSHUTDOWN(bcmath),
56 	NULL,
57 	NULL,
58 	PHP_MINFO(bcmath),
59 	PHP_BCMATH_VERSION,
60 	PHP_MODULE_GLOBALS(bcmath),
61 	PHP_GINIT(bcmath),
62 	PHP_GSHUTDOWN(bcmath),
63 	NULL,
64 	STANDARD_MODULE_PROPERTIES_EX
65 };
66 
67 #ifdef COMPILE_DL_BCMATH
68 #ifdef ZTS
69 ZEND_TSRMLS_CACHE_DEFINE()
70 #endif
ZEND_GET_MODULE(bcmath)71 ZEND_GET_MODULE(bcmath)
72 #endif
73 
74 ZEND_INI_MH(OnUpdateScale)
75 {
76 	int *p;
77 	zend_long tmp;
78 
79 	tmp = zend_ini_parse_quantity_warn(new_value, entry->name);
80 	if (tmp < 0 || tmp > INT_MAX) {
81 		return FAILURE;
82 	}
83 
84 	p = (int *) ZEND_INI_GET_ADDR();
85 	*p = (int) tmp;
86 
87 	return SUCCESS;
88 }
89 
90 /* {{{ PHP_INI */
91 PHP_INI_BEGIN()
92 	STD_PHP_INI_ENTRY("bcmath.scale", "0", PHP_INI_ALL, OnUpdateScale, bc_precision, zend_bcmath_globals, bcmath_globals)
PHP_INI_END()93 PHP_INI_END()
94 /* }}} */
95 
96 /* {{{ PHP_GINIT_FUNCTION */
97 static PHP_GINIT_FUNCTION(bcmath)
98 {
99 #if defined(COMPILE_DL_BCMATH) && defined(ZTS)
100 	ZEND_TSRMLS_CACHE_UPDATE();
101 #endif
102 	bcmath_globals->bc_precision = 0;
103 	bcmath_globals->arena = NULL;
104 	bcmath_globals->arena_offset = 0;
105 	bc_init_numbers();
106 }
107 /* }}} */
108 
109 /* {{{ PHP_GSHUTDOWN_FUNCTION */
PHP_GSHUTDOWN_FUNCTION(bcmath)110 static PHP_GSHUTDOWN_FUNCTION(bcmath)
111 {
112 	_bc_free_num_ex(&bcmath_globals->_zero_, 1);
113 	_bc_free_num_ex(&bcmath_globals->_one_, 1);
114 	_bc_free_num_ex(&bcmath_globals->_two_, 1);
115 	bcmath_globals->arena = NULL;
116 	bcmath_globals->arena_offset = 0;
117 }
118 /* }}} */
119 
120 static void bcmath_number_register_class(void);
121 
122 /* {{{ PHP_MINIT_FUNCTION */
PHP_MINIT_FUNCTION(bcmath)123 PHP_MINIT_FUNCTION(bcmath)
124 {
125 	REGISTER_INI_ENTRIES();
126 	bcmath_number_register_class();
127 
128 	return SUCCESS;
129 }
130 /* }}} */
131 
132 /* {{{ PHP_MSHUTDOWN_FUNCTION */
PHP_MSHUTDOWN_FUNCTION(bcmath)133 PHP_MSHUTDOWN_FUNCTION(bcmath)
134 {
135 	UNREGISTER_INI_ENTRIES();
136 
137 	return SUCCESS;
138 }
139 /* }}} */
140 
141 /* {{{ PHP_MINFO_FUNCTION */
PHP_MINFO_FUNCTION(bcmath)142 PHP_MINFO_FUNCTION(bcmath)
143 {
144 	php_info_print_table_start();
145 	php_info_print_table_row(2, "BCMath support", "enabled");
146 	php_info_print_table_end();
147 	DISPLAY_INI_ENTRIES();
148 }
149 /* }}} */
150 
bcmath_check_scale(zend_long scale,uint32_t arg_num)151 static zend_always_inline zend_result bcmath_check_scale(zend_long scale, uint32_t arg_num)
152 {
153 	if (UNEXPECTED(scale < 0 || scale > INT_MAX)) {
154 		zend_argument_value_error(arg_num, "must be between 0 and %d", INT_MAX);
155 		return FAILURE;
156 	}
157 	return SUCCESS;
158 }
159 
php_long2num(bc_num * num,zend_long lval)160 static void php_long2num(bc_num *num, zend_long lval)
161 {
162 	*num = bc_long2num(lval);
163 }
164 
php_str2num_ex(bc_num * num,const zend_string * str,size_t * full_scale)165 static zend_result php_str2num_ex(bc_num *num, const zend_string *str, size_t *full_scale)
166 {
167 	if (!bc_str2num(num, ZSTR_VAL(str), ZSTR_VAL(str) + ZSTR_LEN(str), 0, full_scale, true)) {
168 		return FAILURE;
169 	}
170 
171 	return SUCCESS;
172 }
173 
174 /* {{{ php_str2num
175    Convert to bc_num detecting scale */
php_str2num(bc_num * num,const zend_string * str)176 static zend_result php_str2num(bc_num *num, const zend_string *str)
177 {
178 	return php_str2num_ex(num, str, NULL);
179 }
180 /* }}} */
181 
182 /* {{{ Returns the sum of two arbitrary precision numbers */
PHP_FUNCTION(bcadd)183 PHP_FUNCTION(bcadd)
184 {
185 	zend_string *left, *right;
186 	zend_long scale_param;
187 	bool scale_param_is_null = 1;
188 	bc_num first = NULL, second = NULL, result = NULL;
189 	int scale;
190 
191 	ZEND_PARSE_PARAMETERS_START(2, 3)
192 		Z_PARAM_STR(left)
193 		Z_PARAM_STR(right)
194 		Z_PARAM_OPTIONAL
195 		Z_PARAM_LONG_OR_NULL(scale_param, scale_param_is_null)
196 	ZEND_PARSE_PARAMETERS_END();
197 
198 	if (scale_param_is_null) {
199 		scale = BCG(bc_precision);
200 	} else if (bcmath_check_scale(scale_param, 3) == FAILURE) {
201 		RETURN_THROWS();
202 	} else {
203 		scale = (int) scale_param;
204 	}
205 
206 	BC_ARENA_SETUP;
207 
208 	if (php_str2num(&first, left) == FAILURE) {
209 		zend_argument_value_error(1, "is not well-formed");
210 		goto cleanup;
211 	}
212 
213 	if (php_str2num(&second, right) == FAILURE) {
214 		zend_argument_value_error(2, "is not well-formed");
215 		goto cleanup;
216 	}
217 
218 	result = bc_add (first, second, scale);
219 
220 	RETVAL_NEW_STR(bc_num2str_ex(result, scale));
221 
222 	cleanup: {
223 		bc_free_num(&first);
224 		bc_free_num(&second);
225 		bc_free_num(&result);
226 		BC_ARENA_TEARDOWN;
227 	};
228 }
229 /* }}} */
230 
231 /* {{{ Returns the difference between two arbitrary precision numbers */
PHP_FUNCTION(bcsub)232 PHP_FUNCTION(bcsub)
233 {
234 	zend_string *left, *right;
235 	zend_long scale_param;
236 	bool scale_param_is_null = 1;
237 	bc_num first = NULL, second = NULL, result = NULL;
238 	int scale;
239 
240 	ZEND_PARSE_PARAMETERS_START(2, 3)
241 		Z_PARAM_STR(left)
242 		Z_PARAM_STR(right)
243 		Z_PARAM_OPTIONAL
244 		Z_PARAM_LONG_OR_NULL(scale_param, scale_param_is_null)
245 	ZEND_PARSE_PARAMETERS_END();
246 
247 	if (scale_param_is_null) {
248 		scale = BCG(bc_precision);
249 	} else if (bcmath_check_scale(scale_param, 3) == FAILURE) {
250 		RETURN_THROWS();
251 	} else {
252 		scale = (int) scale_param;
253 	}
254 
255 	BC_ARENA_SETUP;
256 
257 	if (php_str2num(&first, left) == FAILURE) {
258 		zend_argument_value_error(1, "is not well-formed");
259 		goto cleanup;
260 	}
261 
262 	if (php_str2num(&second, right) == FAILURE) {
263 		zend_argument_value_error(2, "is not well-formed");
264 		goto cleanup;
265 	}
266 
267 	result = bc_sub (first, second, scale);
268 
269 	RETVAL_NEW_STR(bc_num2str_ex(result, scale));
270 
271 	cleanup: {
272 		bc_free_num(&first);
273 		bc_free_num(&second);
274 		bc_free_num(&result);
275 		BC_ARENA_TEARDOWN;
276 	};
277 }
278 /* }}} */
279 
280 /* {{{ Returns the multiplication of two arbitrary precision numbers */
PHP_FUNCTION(bcmul)281 PHP_FUNCTION(bcmul)
282 {
283 	zend_string *left, *right;
284 	zend_long scale_param;
285 	bool scale_param_is_null = 1;
286 	bc_num first = NULL, second = NULL, result = NULL;
287 	int scale;
288 
289 	ZEND_PARSE_PARAMETERS_START(2, 3)
290 		Z_PARAM_STR(left)
291 		Z_PARAM_STR(right)
292 		Z_PARAM_OPTIONAL
293 		Z_PARAM_LONG_OR_NULL(scale_param, scale_param_is_null)
294 	ZEND_PARSE_PARAMETERS_END();
295 
296 	if (scale_param_is_null) {
297 		scale = BCG(bc_precision);
298 	} else if (bcmath_check_scale(scale_param, 3) == FAILURE) {
299 		RETURN_THROWS();
300 	} else {
301 		scale = (int) scale_param;
302 	}
303 
304 	BC_ARENA_SETUP;
305 
306 	if (php_str2num(&first, left) == FAILURE) {
307 		zend_argument_value_error(1, "is not well-formed");
308 		goto cleanup;
309 	}
310 
311 	if (php_str2num(&second, right) == FAILURE) {
312 		zend_argument_value_error(2, "is not well-formed");
313 		goto cleanup;
314 	}
315 
316 	result = bc_multiply (first, second, scale);
317 
318 	RETVAL_NEW_STR(bc_num2str_ex(result, scale));
319 
320 	cleanup: {
321 		bc_free_num(&first);
322 		bc_free_num(&second);
323 		bc_free_num(&result);
324 		BC_ARENA_TEARDOWN;
325 	};
326 }
327 /* }}} */
328 
329 /* {{{ Returns the quotient of two arbitrary precision numbers (division) */
PHP_FUNCTION(bcdiv)330 PHP_FUNCTION(bcdiv)
331 {
332 	zend_string *left, *right;
333 	zend_long scale_param;
334 	bool scale_param_is_null = 1;
335 	bc_num first = NULL, second = NULL, result;
336 	int scale = BCG(bc_precision);
337 
338 	ZEND_PARSE_PARAMETERS_START(2, 3)
339 		Z_PARAM_STR(left)
340 		Z_PARAM_STR(right)
341 		Z_PARAM_OPTIONAL
342 		Z_PARAM_LONG_OR_NULL(scale_param, scale_param_is_null)
343 	ZEND_PARSE_PARAMETERS_END();
344 
345 	if (scale_param_is_null) {
346 		scale = BCG(bc_precision);
347 	} else if (bcmath_check_scale(scale_param, 3) == FAILURE) {
348 		RETURN_THROWS();
349 	} else {
350 		scale = (int) scale_param;
351 	}
352 
353 	BC_ARENA_SETUP;
354 
355 	bc_init_num(&result);
356 
357 	if (php_str2num(&first, left) == FAILURE) {
358 		zend_argument_value_error(1, "is not well-formed");
359 		goto cleanup;
360 	}
361 
362 	if (php_str2num(&second, right) == FAILURE) {
363 		zend_argument_value_error(2, "is not well-formed");
364 		goto cleanup;
365 	}
366 
367 	if (!bc_divide(first, second, &result, scale)) {
368 		zend_throw_exception_ex(zend_ce_division_by_zero_error, 0, "Division by zero");
369 		goto cleanup;
370 	}
371 
372 	RETVAL_NEW_STR(bc_num2str_ex(result, scale));
373 
374 	cleanup: {
375 		bc_free_num(&first);
376 		bc_free_num(&second);
377 		bc_free_num(&result);
378 		BC_ARENA_TEARDOWN;
379 	};
380 }
381 /* }}} */
382 
383 /* {{{ Returns the modulus of the two arbitrary precision operands */
PHP_FUNCTION(bcmod)384 PHP_FUNCTION(bcmod)
385 {
386 	zend_string *left, *right;
387 	zend_long scale_param;
388 	bool scale_param_is_null = 1;
389 	bc_num first = NULL, second = NULL, result;
390 	int scale = BCG(bc_precision);
391 
392 	ZEND_PARSE_PARAMETERS_START(2, 3)
393 		Z_PARAM_STR(left)
394 		Z_PARAM_STR(right)
395 		Z_PARAM_OPTIONAL
396 		Z_PARAM_LONG_OR_NULL(scale_param, scale_param_is_null)
397 	ZEND_PARSE_PARAMETERS_END();
398 
399 	if (scale_param_is_null) {
400 		scale = BCG(bc_precision);
401 	} else if (bcmath_check_scale(scale_param, 3) == FAILURE) {
402 		RETURN_THROWS();
403 	} else {
404 		scale = (int) scale_param;
405 	}
406 
407 	BC_ARENA_SETUP;
408 
409 	bc_init_num(&result);
410 
411 	if (php_str2num(&first, left) == FAILURE) {
412 		zend_argument_value_error(1, "is not well-formed");
413 		goto cleanup;
414 	}
415 
416 	if (php_str2num(&second, right) == FAILURE) {
417 		zend_argument_value_error(2, "is not well-formed");
418 		goto cleanup;
419 	}
420 
421 	if (!bc_modulo(first, second, &result, scale)) {
422 		zend_throw_exception_ex(zend_ce_division_by_zero_error, 0, "Modulo by zero");
423 		goto cleanup;
424 	}
425 
426 	RETVAL_NEW_STR(bc_num2str_ex(result, scale));
427 
428 	cleanup: {
429 		bc_free_num(&first);
430 		bc_free_num(&second);
431 		bc_free_num(&result);
432 		BC_ARENA_TEARDOWN;
433 	};
434 }
435 /* }}} */
436 
PHP_FUNCTION(bcdivmod)437 PHP_FUNCTION(bcdivmod)
438 {
439 	zend_string *left, *right;
440 	zend_long scale_param;
441 	bool scale_param_is_null = 1;
442 	bc_num first = NULL, second = NULL, quot = NULL, rem = NULL;
443 	int scale = BCG(bc_precision);
444 
445 	ZEND_PARSE_PARAMETERS_START(2, 3)
446 		Z_PARAM_STR(left)
447 		Z_PARAM_STR(right)
448 		Z_PARAM_OPTIONAL
449 		Z_PARAM_LONG_OR_NULL(scale_param, scale_param_is_null)
450 	ZEND_PARSE_PARAMETERS_END();
451 
452 	if (scale_param_is_null) {
453 		scale = BCG(bc_precision);
454 	} else if (bcmath_check_scale(scale_param, 3) == FAILURE) {
455 		RETURN_THROWS();
456 	} else {
457 		scale = (int) scale_param;
458 	}
459 
460 	BC_ARENA_SETUP;
461 
462 	if (php_str2num(&first, left) == FAILURE) {
463 		zend_argument_value_error(1, "is not well-formed");
464 		goto cleanup;
465 	}
466 
467 	if (php_str2num(&second, right) == FAILURE) {
468 		zend_argument_value_error(2, "is not well-formed");
469 		goto cleanup;
470 	}
471 
472 	if (!bc_divmod(first, second, &quot, &rem, scale)) {
473 		zend_throw_exception_ex(zend_ce_division_by_zero_error, 0, "Division by zero");
474 		goto cleanup;
475 	}
476 
477 	zval z_quot, z_rem;
478 	ZVAL_STR(&z_quot, bc_num2str_ex(quot, 0));
479 	ZVAL_STR(&z_rem, bc_num2str_ex(rem, scale));
480 
481 	RETVAL_ARR(zend_new_pair(&z_quot, &z_rem));
482 
483 	cleanup: {
484 		bc_free_num(&first);
485 		bc_free_num(&second);
486 		bc_free_num(&quot);
487 		bc_free_num(&rem);
488 		BC_ARENA_TEARDOWN;
489 	};
490 }
491 
492 /* {{{ Returns the value of an arbitrary precision number raised to the power of another reduced by a modulus */
PHP_FUNCTION(bcpowmod)493 PHP_FUNCTION(bcpowmod)
494 {
495 	zend_string *base_str, *exponent_str, *modulus_str;
496 	zend_long scale_param;
497 	bool scale_param_is_null = 1;
498 	bc_num bc_base = NULL, bc_expo = NULL, bc_modulus = NULL, result;
499 	int scale = BCG(bc_precision);
500 
501 	ZEND_PARSE_PARAMETERS_START(3, 4)
502 		Z_PARAM_STR(base_str)
503 		Z_PARAM_STR(exponent_str)
504 		Z_PARAM_STR(modulus_str)
505 		Z_PARAM_OPTIONAL
506 		Z_PARAM_LONG_OR_NULL(scale_param, scale_param_is_null)
507 	ZEND_PARSE_PARAMETERS_END();
508 
509 	if (scale_param_is_null) {
510 		scale = BCG(bc_precision);
511 	} else if (bcmath_check_scale(scale_param, 4) == FAILURE) {
512 		RETURN_THROWS();
513 	} else {
514 		scale = (int) scale_param;
515 	}
516 
517 	BC_ARENA_SETUP;
518 
519 	bc_init_num(&result);
520 
521 	if (php_str2num(&bc_base, base_str) == FAILURE) {
522 		zend_argument_value_error(1, "is not well-formed");
523 		goto cleanup;
524 	}
525 
526 	if (php_str2num(&bc_expo, exponent_str) == FAILURE) {
527 		zend_argument_value_error(2, "is not well-formed");
528 		goto cleanup;
529 	}
530 
531 	if (php_str2num(&bc_modulus, modulus_str) == FAILURE) {
532 		zend_argument_value_error(3, "is not well-formed");
533 		goto cleanup;
534 	}
535 
536 	raise_mod_status status = bc_raisemod(bc_base, bc_expo, bc_modulus, &result, scale);
537 	switch (status) {
538 		case BASE_HAS_FRACTIONAL:
539 			zend_argument_value_error(1, "cannot have a fractional part");
540 			goto cleanup;
541 		case EXPO_HAS_FRACTIONAL:
542 			zend_argument_value_error(2, "cannot have a fractional part");
543 			goto cleanup;
544 		case EXPO_IS_NEGATIVE:
545 			zend_argument_value_error(2, "must be greater than or equal to 0");
546 			goto cleanup;
547 		case MOD_HAS_FRACTIONAL:
548 			zend_argument_value_error(3, "cannot have a fractional part");
549 			goto cleanup;
550 		case MOD_IS_ZERO:
551 			zend_throw_exception_ex(zend_ce_division_by_zero_error, 0, "Modulo by zero");
552 			goto cleanup;
553 		case OK:
554 			RETVAL_NEW_STR(bc_num2str_ex(result, scale));
555 			break;
556 		EMPTY_SWITCH_DEFAULT_CASE();
557 	}
558 
559 	cleanup: {
560 		bc_free_num(&bc_base);
561 		bc_free_num(&bc_expo);
562 		bc_free_num(&bc_modulus);
563 		bc_free_num(&result);
564 		BC_ARENA_TEARDOWN;
565 	};
566 }
567 /* }}} */
568 
569 /* {{{ Returns the value of an arbitrary precision number raised to the power of another */
PHP_FUNCTION(bcpow)570 PHP_FUNCTION(bcpow)
571 {
572 	zend_string *base_str, *exponent_str;
573 	zend_long scale_param;
574 	bool scale_param_is_null = 1;
575 	bc_num first = NULL, bc_exponent = NULL, result;
576 	int scale = BCG(bc_precision);
577 
578 	ZEND_PARSE_PARAMETERS_START(2, 3)
579 		Z_PARAM_STR(base_str)
580 		Z_PARAM_STR(exponent_str)
581 		Z_PARAM_OPTIONAL
582 		Z_PARAM_LONG_OR_NULL(scale_param, scale_param_is_null)
583 	ZEND_PARSE_PARAMETERS_END();
584 
585 	if (scale_param_is_null) {
586 		scale = BCG(bc_precision);
587 	} else if (bcmath_check_scale(scale_param, 3) == FAILURE) {
588 		RETURN_THROWS();
589 	} else {
590 		scale = (int) scale_param;
591 	}
592 
593 	BC_ARENA_SETUP;
594 
595 	bc_init_num(&result);
596 
597 	if (php_str2num(&first, base_str) == FAILURE) {
598 		zend_argument_value_error(1, "is not well-formed");
599 		goto cleanup;
600 	}
601 
602 	if (php_str2num(&bc_exponent, exponent_str) == FAILURE) {
603 		zend_argument_value_error(2, "is not well-formed");
604 		goto cleanup;
605 	}
606 
607 	/* Check the exponent for scale digits and convert to a long. */
608 	if (bc_exponent->n_scale != 0) {
609 		zend_argument_value_error(2, "cannot have a fractional part");
610 		goto cleanup;
611 	}
612 	long exponent = bc_num2long(bc_exponent);
613 	if (exponent == 0 && (bc_exponent->n_len > 1 || bc_exponent->n_value[0] != 0)) {
614 		zend_argument_value_error(2, "is too large");
615 		goto cleanup;
616 	}
617 
618 	if (!bc_raise(first, exponent, &result, scale)) {
619 		zend_throw_exception_ex(zend_ce_division_by_zero_error, 0, "Negative power of zero");
620 		goto cleanup;
621 	}
622 
623 	RETVAL_NEW_STR(bc_num2str_ex(result, scale));
624 
625 	cleanup: {
626 		bc_free_num(&first);
627 		bc_free_num(&bc_exponent);
628 		bc_free_num(&result);
629 		BC_ARENA_TEARDOWN;
630 	};
631 }
632 /* }}} */
633 
634 /* {{{ Returns the square root of an arbitrary precision number */
PHP_FUNCTION(bcsqrt)635 PHP_FUNCTION(bcsqrt)
636 {
637 	zend_string *left;
638 	zend_long scale_param;
639 	bool scale_param_is_null = 1;
640 	bc_num result = NULL;
641 	int scale = BCG(bc_precision);
642 
643 	ZEND_PARSE_PARAMETERS_START(1, 2)
644 		Z_PARAM_STR(left)
645 		Z_PARAM_OPTIONAL
646 		Z_PARAM_LONG_OR_NULL(scale_param, scale_param_is_null)
647 	ZEND_PARSE_PARAMETERS_END();
648 
649 	if (scale_param_is_null) {
650 		scale = BCG(bc_precision);
651 	} else if (bcmath_check_scale(scale_param, 2) == FAILURE) {
652 		RETURN_THROWS();
653 	} else {
654 		scale = (int) scale_param;
655 	}
656 
657 	BC_ARENA_SETUP;
658 
659 	if (php_str2num(&result, left) == FAILURE) {
660 		zend_argument_value_error(1, "is not well-formed");
661 		goto cleanup;
662 	}
663 
664 	if (bc_sqrt (&result, scale) != 0) {
665 		RETVAL_NEW_STR(bc_num2str_ex(result, scale));
666 	} else {
667 		zend_argument_value_error(1, "must be greater than or equal to 0");
668 	}
669 
670 	cleanup: {
671 		bc_free_num(&result);
672 		BC_ARENA_TEARDOWN;
673 	};
674 }
675 /* }}} */
676 
677 /* {{{ Compares two arbitrary precision numbers */
PHP_FUNCTION(bccomp)678 PHP_FUNCTION(bccomp)
679 {
680 	zend_string *left, *right;
681 	zend_long scale_param;
682 	bool scale_param_is_null = 1;
683 	bc_num first = NULL, second = NULL;
684 	int scale = BCG(bc_precision);
685 
686 	ZEND_PARSE_PARAMETERS_START(2, 3)
687 		Z_PARAM_STR(left)
688 		Z_PARAM_STR(right)
689 		Z_PARAM_OPTIONAL
690 		Z_PARAM_LONG_OR_NULL(scale_param, scale_param_is_null)
691 	ZEND_PARSE_PARAMETERS_END();
692 
693 	if (scale_param_is_null) {
694 		scale = BCG(bc_precision);
695 	} else if (bcmath_check_scale(scale_param, 3) == FAILURE) {
696 		RETURN_THROWS();
697 	} else {
698 		scale = (int) scale_param;
699 	}
700 
701 	BC_ARENA_SETUP;
702 
703 	if (!bc_str2num(&first, ZSTR_VAL(left), ZSTR_VAL(left) + ZSTR_LEN(left), scale, NULL, false)) {
704 		zend_argument_value_error(1, "is not well-formed");
705 		goto cleanup;
706 	}
707 
708 	if (!bc_str2num(&second, ZSTR_VAL(right), ZSTR_VAL(right) + ZSTR_LEN(right), scale, NULL, false)) {
709 		zend_argument_value_error(2, "is not well-formed");
710 		goto cleanup;
711 	}
712 
713 	RETVAL_LONG(bc_compare(first, second, scale));
714 
715 	cleanup: {
716 		bc_free_num(&first);
717 		bc_free_num(&second);
718 		BC_ARENA_TEARDOWN;
719 	};
720 }
721 /* }}} */
722 
723 /* {{{ floor or ceil */
bcfloor_or_bcceil(INTERNAL_FUNCTION_PARAMETERS,bool is_floor)724 static void bcfloor_or_bcceil(INTERNAL_FUNCTION_PARAMETERS, bool is_floor)
725 {
726 	zend_string *numstr;
727 	bc_num num = NULL, result = NULL;
728 
729 	ZEND_PARSE_PARAMETERS_START(1, 1)
730 		Z_PARAM_STR(numstr)
731 	ZEND_PARSE_PARAMETERS_END();
732 
733 	BC_ARENA_SETUP;
734 
735 	if (php_str2num(&num, numstr) == FAILURE) {
736 		zend_argument_value_error(1, "is not well-formed");
737 		goto cleanup;
738 	}
739 
740 	result = bc_floor_or_ceil(num, is_floor);
741 	RETVAL_NEW_STR(bc_num2str_ex(result, 0));
742 
743 	cleanup: {
744 		bc_free_num(&num);
745 		bc_free_num(&result);
746 		BC_ARENA_TEARDOWN;
747 	};
748 }
749 /* }}} */
750 
751 /* {{{ Returns floor of num */
PHP_FUNCTION(bcfloor)752 PHP_FUNCTION(bcfloor)
753 {
754 	bcfloor_or_bcceil(INTERNAL_FUNCTION_PARAM_PASSTHRU, true);
755 }
756 /* }}} */
757 
758 /* {{{ Returns ceil of num */
PHP_FUNCTION(bcceil)759 PHP_FUNCTION(bcceil)
760 {
761 	bcfloor_or_bcceil(INTERNAL_FUNCTION_PARAM_PASSTHRU, false);
762 }
763 /* }}} */
764 
765 /* {{{ Returns num rounded to the digits specified by precision. */
PHP_FUNCTION(bcround)766 PHP_FUNCTION(bcround)
767 {
768 	zend_string *numstr;
769 	zend_long precision = 0;
770 	zend_long mode = PHP_ROUND_HALF_UP;
771 	zend_object *mode_object = NULL;
772 	bc_num num = NULL, result;
773 
774 	ZEND_PARSE_PARAMETERS_START(1, 3)
775 		Z_PARAM_STR(numstr)
776 		Z_PARAM_OPTIONAL
777 		Z_PARAM_LONG(precision)
778 		Z_PARAM_OBJ_OF_CLASS(mode_object, rounding_mode_ce)
779 	ZEND_PARSE_PARAMETERS_END();
780 
781 	if (mode_object != NULL) {
782 		mode = php_math_round_mode_from_enum(mode_object);
783 	}
784 
785 	switch (mode) {
786 		case PHP_ROUND_HALF_UP:
787 		case PHP_ROUND_HALF_DOWN:
788 		case PHP_ROUND_HALF_EVEN:
789 		case PHP_ROUND_HALF_ODD:
790 		case PHP_ROUND_CEILING:
791 		case PHP_ROUND_FLOOR:
792 		case PHP_ROUND_TOWARD_ZERO:
793 		case PHP_ROUND_AWAY_FROM_ZERO:
794 			break;
795 		default:
796 			/* This is currently unreachable, but might become reachable when new modes are added. */
797 			zend_argument_value_error(3, "is an unsupported rounding mode");
798 			return;
799 	}
800 
801 	BC_ARENA_SETUP;
802 
803 	bc_init_num(&result);
804 
805 	if (php_str2num(&num, numstr) == FAILURE) {
806 		zend_argument_value_error(1, "is not well-formed");
807 		goto cleanup;
808 	}
809 
810 	bc_round(num, precision, mode, &result);
811 	RETVAL_NEW_STR(bc_num2str_ex(result, result->n_scale));
812 
813 	cleanup: {
814 		bc_free_num(&num);
815 		bc_free_num(&result);
816 		BC_ARENA_TEARDOWN;
817 	};
818 }
819 /* }}} */
820 
821 /* {{{ Sets default scale parameter for all bc math functions */
PHP_FUNCTION(bcscale)822 PHP_FUNCTION(bcscale)
823 {
824 	zend_long old_scale, new_scale;
825 	bool new_scale_is_null = 1;
826 
827 	ZEND_PARSE_PARAMETERS_START(0, 1)
828 		Z_PARAM_OPTIONAL
829 		Z_PARAM_LONG_OR_NULL(new_scale, new_scale_is_null)
830 	ZEND_PARSE_PARAMETERS_END();
831 
832 	old_scale = BCG(bc_precision);
833 
834 	if (!new_scale_is_null) {
835 		if (bcmath_check_scale(new_scale, 1) == FAILURE) {
836 			RETURN_THROWS();
837 		}
838 
839 		zend_string *ini_name = ZSTR_INIT_LITERAL("bcmath.scale", 0);
840 		zend_string *new_scale_str = zend_long_to_str(new_scale);
841 		zend_alter_ini_entry(ini_name, new_scale_str, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
842 		zend_string_release(new_scale_str);
843 		zend_string_release(ini_name);
844 	}
845 
846 	RETURN_LONG(old_scale);
847 }
848 /* }}} */
849 
850 
851 
852 static zend_class_entry *bcmath_number_ce;
853 static zend_object_handlers bcmath_number_obj_handlers;
854 
855 static zend_result bcmath_number_do_operation(uint8_t opcode, zval *ret_val, zval *op1, zval *op2);
856 static int bcmath_number_compare(zval *op1, zval *op2);
857 
858 #if SIZEOF_SIZE_T >= 8
859 #  define CHECK_RET_SCALE_OVERFLOW(scale, origin_scale) (scale > INT_MAX)
860 #else
861 #  define CHECK_RET_SCALE_OVERFLOW(scale, origin_scale) (scale > INT_MAX || scale < origin_scale)
862 #endif
863 #define CHECK_SCALE_OVERFLOW(scale) (scale > INT_MAX)
864 
get_bcmath_number_from_obj(const zend_object * obj)865 static zend_always_inline bcmath_number_obj_t *get_bcmath_number_from_obj(const zend_object *obj)
866 {
867 	return (bcmath_number_obj_t*)((char*)(obj) - XtOffsetOf(bcmath_number_obj_t, std));
868 }
869 
get_bcmath_number_from_zval(const zval * zv)870 static zend_always_inline bcmath_number_obj_t *get_bcmath_number_from_zval(const zval *zv)
871 {
872 	return get_bcmath_number_from_obj(Z_OBJ_P(zv));
873 }
874 
bcmath_number_value_to_str(bcmath_number_obj_t * intern)875 static zend_always_inline zend_string *bcmath_number_value_to_str(bcmath_number_obj_t *intern)
876 {
877 	if (intern->value == NULL) {
878 		intern->value = bc_num2str_ex(intern->num, intern->scale);
879 	}
880 	return intern->value;
881 }
882 
bcmath_number_create(zend_class_entry * ce)883 static zend_object *bcmath_number_create(zend_class_entry *ce)
884 {
885 	bcmath_number_obj_t *intern = zend_object_alloc(sizeof(bcmath_number_obj_t), ce);
886 
887 	zend_object_std_init(&intern->std, ce);
888 	object_properties_init(&intern->std, ce);
889 
890 	intern->scale = 1;
891 
892 	return &intern->std;
893 }
894 
bcmath_number_free(zend_object * obj)895 static void bcmath_number_free(zend_object *obj)
896 {
897 	bcmath_number_obj_t *intern = get_bcmath_number_from_obj(obj);
898 	if (intern->num) {
899 		bc_free_num(&intern->num);
900 		intern->num = NULL;
901 	}
902 	if (intern->value) {
903 		zend_string_release(intern->value);
904 		intern->value = NULL;
905 	}
906 	zend_object_std_dtor(&intern->std);
907 }
908 
bcmath_number_clone(zend_object * obj)909 static zend_object *bcmath_number_clone(zend_object *obj)
910 {
911 	bcmath_number_obj_t *original = get_bcmath_number_from_obj(obj);
912 	bcmath_number_obj_t *clone = get_bcmath_number_from_obj(bcmath_number_create(bcmath_number_ce));
913 
914 	clone->num = bc_copy_num(original->num);
915 	if (original->value) {
916 		clone->value = zend_string_copy(original->value);
917 	}
918 	clone->scale = original->scale;
919 
920 	return &clone->std;
921 }
922 
bcmath_number_get_properties_for(zend_object * obj,zend_prop_purpose purpose)923 static HashTable *bcmath_number_get_properties_for(zend_object *obj, zend_prop_purpose purpose)
924 {
925 	zval zv;
926 	bcmath_number_obj_t *intern = get_bcmath_number_from_obj(obj);
927 	HashTable *props = zend_array_dup(zend_std_get_properties(obj));
928 
929 	ZVAL_STR_COPY(&zv, bcmath_number_value_to_str(intern));
930 	zend_hash_update(props, ZSTR_KNOWN(ZEND_STR_VALUE), &zv);
931 	ZVAL_LONG(&zv, intern->scale);
932 	zend_hash_str_update(props, ZEND_STRL("scale"), &zv);
933 
934 	return props;
935 }
936 
bcmath_number_write_property(zend_object * obj,zend_string * name,zval * value,void ** cache_slot)937 static zval *bcmath_number_write_property(zend_object *obj, zend_string *name, zval *value, void **cache_slot)
938 {
939 	if (zend_string_equals(name, ZSTR_KNOWN(ZEND_STR_VALUE)) || zend_string_equals_literal(name, "scale")) {
940 		zend_readonly_property_modification_error_ex(ZSTR_VAL(obj->ce->name), ZSTR_VAL(name));
941 		return &EG(error_zval);
942 	}
943 
944 	return zend_std_write_property(obj, name, value, cache_slot);
945 }
946 
bcmath_number_unset_property(zend_object * obj,zend_string * name,void ** cache_slot)947 static void bcmath_number_unset_property(zend_object *obj, zend_string *name, void **cache_slot)
948 {
949 	if (zend_string_equals(name, ZSTR_KNOWN(ZEND_STR_VALUE)) || zend_string_equals_literal(name, "scale")) {
950 		zend_throw_error(NULL, "Cannot unset readonly property %s::$%s", ZSTR_VAL(obj->ce->name), ZSTR_VAL(name));
951 		return;
952 	}
953 
954 	zend_std_unset_property(obj, name, cache_slot);
955 }
956 
bcmath_number_read_property(zend_object * obj,zend_string * name,int type,void ** cache_slot,zval * rv)957 static zval *bcmath_number_read_property(zend_object *obj, zend_string *name, int type, void **cache_slot, zval *rv)
958 {
959 	bcmath_number_obj_t *intern = get_bcmath_number_from_obj(obj);
960 
961 	if (zend_string_equals(name, ZSTR_KNOWN(ZEND_STR_VALUE))) {
962 		ZVAL_STR_COPY(rv, bcmath_number_value_to_str(intern));
963 		return rv;
964 	}
965 
966 	if (zend_string_equals_literal(name, "scale")) {
967 		ZVAL_LONG(rv, intern->scale);
968 		return rv;
969 	}
970 
971 	return zend_std_read_property(obj, name, type, cache_slot, rv);
972 }
973 
bcmath_number_has_property(zend_object * obj,zend_string * name,int check_empty,void ** cache_slot)974 static int bcmath_number_has_property(zend_object *obj, zend_string *name, int check_empty, void **cache_slot)
975 {
976 	if (check_empty == ZEND_PROPERTY_NOT_EMPTY) {
977 		bcmath_number_obj_t *intern = get_bcmath_number_from_obj(obj);
978 
979 		if (zend_string_equals(name, ZSTR_KNOWN(ZEND_STR_VALUE))) {
980 			return !bc_is_zero(intern->num);
981 		}
982 
983 		if (zend_string_equals_literal(name, "scale")) {
984 			return intern->scale != 0;
985 		}
986 	}
987 	return zend_string_equals(name, ZSTR_KNOWN(ZEND_STR_VALUE)) || zend_string_equals_literal(name, "scale");
988 }
989 
bcmath_number_cast_object(zend_object * obj,zval * ret,int type)990 static zend_result bcmath_number_cast_object(zend_object *obj, zval *ret, int type)
991 {
992 	if (type == _IS_BOOL) {
993 		bcmath_number_obj_t *intern = get_bcmath_number_from_obj(obj);
994 		ZVAL_BOOL(ret, !bc_is_zero(intern->num));
995 		return SUCCESS;
996 	}
997 
998 	return zend_std_cast_object_tostring(obj, ret, type);
999 }
1000 
bcmath_number_register_class(void)1001 static void bcmath_number_register_class(void)
1002 {
1003 	bcmath_number_ce = register_class_BcMath_Number(zend_ce_stringable);
1004 	bcmath_number_ce->create_object = bcmath_number_create;
1005 	bcmath_number_ce->default_object_handlers = &bcmath_number_obj_handlers;
1006 
1007 	memcpy(&bcmath_number_obj_handlers, &std_object_handlers, sizeof(zend_object_handlers));
1008 	bcmath_number_obj_handlers.offset = XtOffsetOf(bcmath_number_obj_t, std);
1009 	bcmath_number_obj_handlers.free_obj = bcmath_number_free;
1010 	bcmath_number_obj_handlers.clone_obj = bcmath_number_clone;
1011 	bcmath_number_obj_handlers.do_operation = bcmath_number_do_operation;
1012 	bcmath_number_obj_handlers.compare = bcmath_number_compare;
1013 	bcmath_number_obj_handlers.write_property = bcmath_number_write_property;
1014 	bcmath_number_obj_handlers.unset_property = bcmath_number_unset_property;
1015 	bcmath_number_obj_handlers.has_property = bcmath_number_has_property;
1016 	bcmath_number_obj_handlers.read_property = bcmath_number_read_property;
1017 	bcmath_number_obj_handlers.get_properties_for = bcmath_number_get_properties_for;
1018 	bcmath_number_obj_handlers.cast_object = bcmath_number_cast_object;
1019 }
1020 
bcmath_number_add_internal(bc_num n1,bc_num n2,bc_num * ret,size_t n1_full_scale,size_t n2_full_scale,size_t * scale,bool auto_scale)1021 static zend_always_inline void bcmath_number_add_internal(
1022 	bc_num n1, bc_num n2, bc_num *ret,
1023 	size_t n1_full_scale, size_t n2_full_scale, size_t *scale, bool auto_scale
1024 ) {
1025 	if (auto_scale) {
1026 		*scale = MAX(n1_full_scale, n2_full_scale);
1027 	}
1028 	*ret = bc_add(n1, n2, *scale);
1029 	(*ret)->n_scale = MIN(*scale, (*ret)->n_scale);
1030 	bc_rm_trailing_zeros(*ret);
1031 }
1032 
bcmath_number_sub_internal(bc_num n1,bc_num n2,bc_num * ret,size_t n1_full_scale,size_t n2_full_scale,size_t * scale,bool auto_scale)1033 static zend_always_inline void bcmath_number_sub_internal(
1034 	bc_num n1, bc_num n2, bc_num *ret,
1035 	size_t n1_full_scale, size_t n2_full_scale, size_t *scale, bool auto_scale
1036 ) {
1037 	if (auto_scale) {
1038 		*scale = MAX(n1_full_scale, n2_full_scale);
1039 	}
1040 	*ret = bc_sub(n1, n2, *scale);
1041 	(*ret)->n_scale = MIN(*scale, (*ret)->n_scale);
1042 	bc_rm_trailing_zeros(*ret);
1043 }
1044 
bcmath_number_mul_internal(bc_num n1,bc_num n2,bc_num * ret,size_t n1_full_scale,size_t n2_full_scale,size_t * scale,bool auto_scale)1045 static zend_always_inline zend_result bcmath_number_mul_internal(
1046 	bc_num n1, bc_num n2, bc_num *ret,
1047 	size_t n1_full_scale, size_t n2_full_scale, size_t *scale, bool auto_scale
1048 ) {
1049 	if (auto_scale) {
1050 		*scale = n1_full_scale + n2_full_scale;
1051 		if (UNEXPECTED(CHECK_RET_SCALE_OVERFLOW(*scale, n1_full_scale))) {
1052 			zend_value_error("scale of the result is too large");
1053 			return FAILURE;
1054 		}
1055 	}
1056 	*ret = bc_multiply(n1, n2, *scale);
1057 	(*ret)->n_scale = MIN(*scale, (*ret)->n_scale);
1058 	bc_rm_trailing_zeros(*ret);
1059 	return SUCCESS;
1060 }
1061 
bcmath_number_div_internal(bc_num n1,bc_num n2,bc_num * ret,size_t n1_full_scale,size_t * scale,bool auto_scale)1062 static zend_always_inline zend_result bcmath_number_div_internal(
1063 	bc_num n1, bc_num n2, bc_num *ret,
1064 	size_t n1_full_scale, size_t *scale, bool auto_scale
1065 ) {
1066 	if (auto_scale) {
1067 		*scale = n1_full_scale + BC_MATH_NUMBER_EXPAND_SCALE;
1068 		if (UNEXPECTED(CHECK_RET_SCALE_OVERFLOW(*scale, n1_full_scale))) {
1069 			zend_value_error("scale of the result is too large");
1070 			return FAILURE;
1071 		}
1072 	}
1073 	if (!bc_divide(n1, n2, ret, *scale)) {
1074 		zend_throw_exception_ex(zend_ce_division_by_zero_error, 0, "Division by zero");
1075 		return FAILURE;
1076 	}
1077 	bc_rm_trailing_zeros(*ret);
1078 	if (auto_scale) {
1079 		size_t diff = *scale - (*ret)->n_scale;
1080 		*scale -= diff > BC_MATH_NUMBER_EXPAND_SCALE ? BC_MATH_NUMBER_EXPAND_SCALE : diff;
1081 	}
1082 	return SUCCESS;
1083 }
1084 
bcmath_number_mod_internal(bc_num n1,bc_num n2,bc_num * ret,size_t n1_full_scale,size_t n2_full_scale,size_t * scale,bool auto_scale)1085 static zend_always_inline zend_result bcmath_number_mod_internal(
1086 	bc_num n1, bc_num n2, bc_num *ret,
1087 	size_t n1_full_scale, size_t n2_full_scale, size_t *scale, bool auto_scale
1088 ) {
1089 	if (auto_scale) {
1090 		*scale = MAX(n1_full_scale, n2_full_scale);
1091 	}
1092 	if (!bc_modulo(n1, n2, ret, *scale)) {
1093 		zend_throw_exception_ex(zend_ce_division_by_zero_error, 0, "Modulo by zero");
1094 		return FAILURE;
1095 	}
1096 	bc_rm_trailing_zeros(*ret);
1097 	return SUCCESS;
1098 }
1099 
bcmath_number_pow_internal(bc_num n1,bc_num n2,bc_num * ret,size_t n1_full_scale,size_t * scale,bool auto_scale,bool is_op)1100 static zend_result bcmath_number_pow_internal(
1101 	bc_num n1, bc_num n2, bc_num *ret,
1102 	size_t n1_full_scale, size_t *scale, bool auto_scale, bool is_op
1103 ) {
1104 	/* Check the exponent for scale digits and convert to a long. */
1105 	if (UNEXPECTED(n2->n_scale != 0)) {
1106 		if (is_op) {
1107 			zend_value_error("exponent cannot have a fractional part");
1108 		} else {
1109 			zend_argument_value_error(1, "exponent cannot have a fractional part");
1110 		}
1111 		return FAILURE;
1112 	}
1113 	long exponent = bc_num2long(n2);
1114 
1115 	bool scale_expand = false;
1116 	if (auto_scale) {
1117 		if (exponent > 0) {
1118 			*scale = n1_full_scale * exponent;
1119 			if (UNEXPECTED(*scale > INT_MAX || *scale < n1_full_scale)) {
1120 				zend_value_error("scale of the result is too large");
1121 				return FAILURE;
1122 			}
1123 		} else if (exponent < 0) {
1124 			*scale = n1_full_scale + BC_MATH_NUMBER_EXPAND_SCALE;
1125 			if (UNEXPECTED(CHECK_RET_SCALE_OVERFLOW(*scale, n1_full_scale))) {
1126 				zend_value_error("scale of the result is too large");
1127 				return FAILURE;
1128 			}
1129 			scale_expand = true;
1130 		} else {
1131 			*scale = 0;
1132 		}
1133 	}
1134 
1135 	/**
1136 	 * bc_num2long() returns 0 if exponent is too large.
1137 	 * Here, if n2->n_value is not 0 but exponent is 0, it is considered too large.
1138 	 */
1139 	if (UNEXPECTED(exponent == 0 && (n2->n_len > 1 || n2->n_value[0] != 0))) {
1140 		if (is_op) {
1141 			zend_value_error("exponent is too large");
1142 		} else {
1143 			zend_argument_value_error(1, "exponent is too large");
1144 		}
1145 		return FAILURE;
1146 	}
1147 	if (!bc_raise(n1, exponent, ret, *scale)) {
1148 		zend_throw_exception_ex(zend_ce_division_by_zero_error, 0, "Negative power of zero");
1149 		return FAILURE;
1150 	}
1151 	bc_rm_trailing_zeros(*ret);
1152 	if (scale_expand) {
1153 		size_t diff = *scale - (*ret)->n_scale;
1154 		*scale -= diff > BC_MATH_NUMBER_EXPAND_SCALE ? BC_MATH_NUMBER_EXPAND_SCALE : diff;
1155 	}
1156 	return SUCCESS;
1157 }
1158 
bcmath_number_new_obj(bc_num ret,size_t scale)1159 static zend_always_inline bcmath_number_obj_t *bcmath_number_new_obj(bc_num ret, size_t scale)
1160 {
1161 	bcmath_number_obj_t *intern = get_bcmath_number_from_obj(bcmath_number_create(bcmath_number_ce));
1162 	intern->num = ret;
1163 	intern->scale = scale;
1164 	return intern;
1165 }
1166 
bcmath_number_parse_num(zval * zv,zend_object ** obj,zend_string ** str,zend_long * lval)1167 static zend_result bcmath_number_parse_num(zval *zv, zend_object **obj, zend_string **str, zend_long *lval)
1168 {
1169 	if (Z_TYPE_P(zv) == IS_OBJECT && instanceof_function(Z_OBJCE_P(zv), bcmath_number_ce)) {
1170 		*obj = Z_OBJ_P(zv);
1171 		return SUCCESS;
1172 	} else {
1173 		switch (Z_TYPE_P(zv)) {
1174 			case IS_LONG:
1175 				*lval = Z_LVAL_P(zv);
1176 				return SUCCESS;
1177 
1178 			case IS_STRING:
1179 				*str = Z_STR_P(zv);
1180 				return SUCCESS;
1181 
1182 			case IS_NULL:
1183 				*lval = 0;
1184 				return FAILURE;
1185 
1186 			default:
1187 				return zend_parse_arg_long_slow(zv, lval, 1 /* dummy */) ? SUCCESS : FAILURE;
1188 		}
1189 	}
1190 }
1191 
bc_num_from_obj_or_str_or_long(bc_num * num,size_t * full_scale,const zend_object * obj,const zend_string * str,zend_long lval)1192 static zend_result bc_num_from_obj_or_str_or_long(
1193 	bc_num *num, size_t *full_scale, const zend_object *obj, const zend_string *str, zend_long lval)
1194 {
1195 	if (obj) {
1196 		bcmath_number_obj_t *intern = get_bcmath_number_from_obj(obj);
1197 		*num = intern->num;
1198 		if (full_scale) {
1199 			*full_scale = intern->scale;
1200 		}
1201 		return SUCCESS;
1202 	} else if (str) {
1203 		return php_str2num_ex(num, str, full_scale);
1204 	} else {
1205 		php_long2num(num, lval);
1206 		if (full_scale) {
1207 			*full_scale = 0;
1208 		}
1209 		return SUCCESS;
1210 	}
1211 }
1212 
bcmath_number_do_operation(uint8_t opcode,zval * ret_val,zval * op1,zval * op2)1213 static zend_result bcmath_number_do_operation(uint8_t opcode, zval *ret_val, zval *op1, zval *op2)
1214 {
1215 	switch (opcode) {
1216 		case ZEND_ADD:
1217 		case ZEND_SUB:
1218 		case ZEND_MUL:
1219 		case ZEND_DIV:
1220 		case ZEND_MOD:
1221 		case ZEND_POW:
1222 			break;
1223 		default:
1224 			return FAILURE;
1225 	}
1226 
1227 	zend_object *obj1 = NULL;
1228 	zend_string *str1 = NULL;
1229 	zend_long lval1 = 0;
1230 
1231 	zend_object *obj2 = NULL;
1232 	zend_string *str2 = NULL;
1233 	zend_long lval2 = 0;
1234 
1235 	if (UNEXPECTED(bcmath_number_parse_num(op1, &obj1, &str1, &lval1) == FAILURE || bcmath_number_parse_num(op2, &obj2, &str2, &lval2) == FAILURE)) {
1236 		return FAILURE;
1237 	}
1238 
1239 	bc_num n1 = NULL;
1240 	bc_num n2 = NULL;
1241 	size_t n1_full_scale;
1242 	size_t n2_full_scale;
1243 	if (UNEXPECTED(bc_num_from_obj_or_str_or_long(&n1, &n1_full_scale, obj1, str1, lval1) == FAILURE)) {
1244 		zend_value_error("Left string operand cannot be converted to BcMath\\Number");
1245 		goto fail;
1246 	}
1247 	if (UNEXPECTED(bc_num_from_obj_or_str_or_long(&n2, &n2_full_scale, obj2, str2, lval2) == FAILURE)) {
1248 		zend_value_error("Right string operand cannot be converted to BcMath\\Number");
1249 		goto fail;
1250 	}
1251 
1252 	if (UNEXPECTED(CHECK_SCALE_OVERFLOW(n1_full_scale) || CHECK_SCALE_OVERFLOW(n2_full_scale))) {
1253 		zend_value_error("scale must be between 0 and %d", INT_MAX);
1254 		goto fail;
1255 	}
1256 
1257 	bc_num ret = NULL;
1258 	size_t scale;
1259 	switch (opcode) {
1260 		case ZEND_ADD:
1261 			bcmath_number_add_internal(n1, n2, &ret, n1_full_scale, n2_full_scale, &scale, true);
1262 			break;
1263 		case ZEND_SUB:
1264 			bcmath_number_sub_internal(n1, n2, &ret, n1_full_scale, n2_full_scale, &scale, true);
1265 			break;
1266 		case ZEND_MUL:
1267 			if (UNEXPECTED(bcmath_number_mul_internal(n1, n2, &ret, n1_full_scale, n2_full_scale, &scale, true) == FAILURE)) {
1268 				goto fail;
1269 			}
1270 			break;
1271 		case ZEND_DIV:
1272 			if (UNEXPECTED(bcmath_number_div_internal(n1, n2, &ret, n1_full_scale, &scale, true) == FAILURE)) {
1273 				goto fail;
1274 			}
1275 			break;
1276 		case ZEND_MOD:
1277 			if (UNEXPECTED(bcmath_number_mod_internal(n1, n2, &ret, n1_full_scale, n2_full_scale, &scale, true) == FAILURE)) {
1278 				goto fail;
1279 			}
1280 			break;
1281 		case ZEND_POW:
1282 			if (UNEXPECTED(bcmath_number_pow_internal(n1, n2, &ret, n1_full_scale, &scale, true, true) == FAILURE)) {
1283 				goto fail;
1284 			}
1285 			break;
1286 		EMPTY_SWITCH_DEFAULT_CASE();
1287 	}
1288 
1289 	if (Z_TYPE_P(op1) != IS_OBJECT) {
1290 		bc_free_num(&n1);
1291 	}
1292 	if (Z_TYPE_P(op2) != IS_OBJECT) {
1293 		bc_free_num(&n2);
1294 	}
1295 
1296 	bcmath_number_obj_t *intern = bcmath_number_new_obj(ret, scale);
1297 
1298 	/* For increment and decrement, etc */
1299 	if (ret_val == op1) {
1300 		zval_ptr_dtor(ret_val);
1301 	}
1302 	ZVAL_OBJ(ret_val, &intern->std);
1303 
1304 	return SUCCESS;
1305 
1306 fail:
1307 	if (Z_TYPE_P(op1) != IS_OBJECT) {
1308 		bc_free_num(&n1);
1309 	}
1310 	if (Z_TYPE_P(op2) != IS_OBJECT) {
1311 		bc_free_num(&n2);
1312 	}
1313 	return FAILURE;
1314 }
1315 
bcmath_number_compare(zval * op1,zval * op2)1316 static int bcmath_number_compare(zval *op1, zval *op2)
1317 {
1318 	zend_object *obj1 = NULL;
1319 	zend_string *str1 = NULL;
1320 	zend_long lval1 = 0;
1321 
1322 	zend_object *obj2 = NULL;
1323 	zend_string *str2 = NULL;
1324 	zend_long lval2 = 0;
1325 
1326 	bc_num n1 = NULL;
1327 	bc_num n2 = NULL;
1328 
1329 	int ret = ZEND_UNCOMPARABLE;
1330 
1331 	if (UNEXPECTED(bcmath_number_parse_num(op1, &obj1, &str1, &lval1) == FAILURE)) {
1332 		goto failure;
1333 	}
1334 
1335 	if (UNEXPECTED(bcmath_number_parse_num(op2, &obj2, &str2, &lval2) == FAILURE)) {
1336 		goto failure;
1337 	}
1338 
1339 	size_t n1_full_scale;
1340 	size_t n2_full_scale;
1341 	if (UNEXPECTED(bc_num_from_obj_or_str_or_long(&n1, &n1_full_scale, obj1, str1, lval1) == FAILURE ||
1342 		bc_num_from_obj_or_str_or_long(&n2, &n2_full_scale, obj2, str2, lval2) == FAILURE)) {
1343 		goto failure;
1344 	}
1345 
1346 	if (UNEXPECTED(CHECK_SCALE_OVERFLOW(n1_full_scale) || CHECK_SCALE_OVERFLOW(n2_full_scale))) {
1347 		zend_value_error("scale must be between 0 and %d", INT_MAX);
1348 		goto failure;
1349 	}
1350 
1351 	ret = bc_compare(n1, n2, MAX(n1->n_scale, n2->n_scale));
1352 
1353 failure:
1354 	if (Z_TYPE_P(op1) != IS_OBJECT) {
1355 		bc_free_num(&n1);
1356 	}
1357 	if (Z_TYPE_P(op2) != IS_OBJECT) {
1358 		bc_free_num(&n2);
1359 	}
1360 
1361 	return ret;
1362 }
1363 
1364 #define BCMATH_PARAM_NUMBER_OR_STR_OR_LONG(dest_obj, ce, dest_str, dest_long) \
1365 	Z_PARAM_PROLOGUE(0, 0); \
1366 	if (UNEXPECTED(!(zend_parse_arg_obj(_arg, &(dest_obj), ce, 0) || \
1367 		zend_parse_arg_str_or_long(_arg, &(dest_str), &(dest_long), &_dummy, 0, _i)))) { \
1368 		zend_argument_type_error(_i, "must be of type int, string, or %s, %s given", \
1369 			ZSTR_VAL(bcmath_number_ce->name), zend_zval_value_name(_arg));             \
1370 		_error_code = ZPP_ERROR_FAILURE; \
1371  		break; \
1372  	}
1373 
bc_num_from_obj_or_str_or_long_with_err(bc_num * num,size_t * scale,zend_object * obj,zend_string * str,zend_long lval,uint32_t arg_num)1374 static zend_always_inline zend_result bc_num_from_obj_or_str_or_long_with_err(
1375 	bc_num *num, size_t *scale, zend_object *obj, zend_string *str, zend_long lval, uint32_t arg_num)
1376 {
1377 	size_t full_scale = 0;
1378 	if (UNEXPECTED(bc_num_from_obj_or_str_or_long(num, &full_scale, obj, str, lval) == FAILURE)) {
1379 		zend_argument_value_error(arg_num, "is not well-formed");
1380 		return FAILURE;
1381 	}
1382 	if (UNEXPECTED(CHECK_SCALE_OVERFLOW(full_scale))) {
1383 		zend_argument_value_error(arg_num, "must be between 0 and %d", INT_MAX);
1384 		return FAILURE;
1385 	}
1386 	if (scale != NULL) {
1387 		*scale = full_scale;
1388 	}
1389 	return SUCCESS;
1390 }
1391 
PHP_METHOD(BcMath_Number,__construct)1392 PHP_METHOD(BcMath_Number, __construct)
1393 {
1394 	zend_string *str = NULL;
1395 	zend_long lval = 0;
1396 
1397 	ZEND_PARSE_PARAMETERS_START(1, 1)
1398 		Z_PARAM_STR_OR_LONG(str, lval);
1399 	ZEND_PARSE_PARAMETERS_END();
1400 
1401 	bcmath_number_obj_t *intern = get_bcmath_number_from_zval(ZEND_THIS);
1402 	if (UNEXPECTED(intern->num != NULL)) {
1403 		zend_readonly_property_modification_error_ex(ZSTR_VAL(bcmath_number_ce->name), "value");
1404 		RETURN_THROWS();
1405 	}
1406 
1407 	bc_num num = NULL;
1408 	size_t scale = 0;
1409 	if (bc_num_from_obj_or_str_or_long_with_err(&num, &scale, NULL, str, lval, 1) == FAILURE) {
1410 		bc_free_num(&num);
1411 		RETURN_THROWS();
1412 	}
1413 
1414 	intern->num = num;
1415 	intern->scale = scale;
1416 }
1417 
bcmath_number_calc_method(INTERNAL_FUNCTION_PARAMETERS,uint8_t opcode)1418 static void bcmath_number_calc_method(INTERNAL_FUNCTION_PARAMETERS, uint8_t opcode)
1419 {
1420 	zend_object *num_obj = NULL;
1421 	zend_string *num_str = NULL;
1422 	zend_long num_lval = 0;
1423 	zend_long scale_lval = 0;
1424 	bool scale_is_null = true;
1425 
1426 	ZEND_PARSE_PARAMETERS_START(1, 2)
1427 		BCMATH_PARAM_NUMBER_OR_STR_OR_LONG(num_obj, bcmath_number_ce, num_str, num_lval);
1428 		Z_PARAM_OPTIONAL
1429 		Z_PARAM_LONG_OR_NULL(scale_lval, scale_is_null);
1430 	ZEND_PARSE_PARAMETERS_END();
1431 
1432 	bc_num num = NULL;
1433 	size_t num_full_scale = 0;
1434 	if (bc_num_from_obj_or_str_or_long_with_err(&num, &num_full_scale, num_obj, num_str, num_lval, 1) == FAILURE) {
1435 		goto fail;
1436 	}
1437 	if (bcmath_check_scale(scale_lval, 2) == FAILURE) {
1438 		goto fail;
1439 	}
1440 
1441 	bc_num ret = NULL;
1442 	size_t scale = scale_lval;
1443 	bcmath_number_obj_t *intern = get_bcmath_number_from_zval(ZEND_THIS);
1444 
1445 	switch (opcode) {
1446 		case ZEND_ADD:
1447 			bcmath_number_add_internal(intern->num, num, &ret, intern->scale, num_full_scale, &scale, scale_is_null);
1448 			break;
1449 		case ZEND_SUB:
1450 			bcmath_number_sub_internal(intern->num, num, &ret, intern->scale, num_full_scale, &scale, scale_is_null);
1451 			break;
1452 		case ZEND_MUL:
1453 			if (UNEXPECTED(bcmath_number_mul_internal(intern->num, num, &ret, intern->scale, num_full_scale, &scale, scale_is_null) == FAILURE)) {
1454 				goto fail;
1455 			}
1456 			break;
1457 		case ZEND_DIV:
1458 			if (UNEXPECTED(bcmath_number_div_internal(intern->num, num, &ret, intern->scale, &scale, scale_is_null) == FAILURE)) {
1459 				goto fail;
1460 			}
1461 			break;
1462 		case ZEND_MOD:
1463 			if (UNEXPECTED(bcmath_number_mod_internal(intern->num, num, &ret, intern->scale, num_full_scale, &scale, scale_is_null) == FAILURE)) {
1464 				goto fail;
1465 			}
1466 			break;
1467 		case ZEND_POW:
1468 			if (UNEXPECTED(bcmath_number_pow_internal(intern->num, num, &ret, intern->scale, &scale, scale_is_null, false) == FAILURE)) {
1469 				goto fail;
1470 			}
1471 			break;
1472 		EMPTY_SWITCH_DEFAULT_CASE();
1473 	}
1474 
1475 	if (num_obj == NULL) {
1476 		bc_free_num(&num);
1477 	}
1478 
1479 	bcmath_number_obj_t *new_intern = bcmath_number_new_obj(ret, scale);
1480 	RETURN_OBJ(&new_intern->std);
1481 
1482 fail:
1483 	if (num_obj == NULL) {
1484 		bc_free_num(&num);
1485 	}
1486 	RETURN_THROWS();
1487 }
1488 
PHP_METHOD(BcMath_Number,add)1489 PHP_METHOD(BcMath_Number, add)
1490 {
1491 	bcmath_number_calc_method(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ADD);
1492 }
1493 
PHP_METHOD(BcMath_Number,sub)1494 PHP_METHOD(BcMath_Number, sub)
1495 {
1496 	bcmath_number_calc_method(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_SUB);
1497 }
1498 
PHP_METHOD(BcMath_Number,mul)1499 PHP_METHOD(BcMath_Number, mul)
1500 {
1501 	bcmath_number_calc_method(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_MUL);
1502 }
1503 
PHP_METHOD(BcMath_Number,div)1504 PHP_METHOD(BcMath_Number, div)
1505 {
1506 	bcmath_number_calc_method(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_DIV);
1507 }
1508 
PHP_METHOD(BcMath_Number,mod)1509 PHP_METHOD(BcMath_Number, mod)
1510 {
1511 	bcmath_number_calc_method(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_MOD);
1512 }
1513 
PHP_METHOD(BcMath_Number,pow)1514 PHP_METHOD(BcMath_Number, pow)
1515 {
1516 	bcmath_number_calc_method(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_POW);
1517 }
1518 
PHP_METHOD(BcMath_Number,divmod)1519 PHP_METHOD(BcMath_Number, divmod)
1520 {
1521 	zend_object *num_obj = NULL;
1522 	zend_string *num_str = NULL;
1523 	zend_long num_lval = 0;
1524 	zend_long scale_lval = 0;
1525 	bool scale_is_null = true;
1526 
1527 	ZEND_PARSE_PARAMETERS_START(1, 2)
1528 		BCMATH_PARAM_NUMBER_OR_STR_OR_LONG(num_obj, bcmath_number_ce, num_str, num_lval);
1529 		Z_PARAM_OPTIONAL
1530 		Z_PARAM_LONG_OR_NULL(scale_lval, scale_is_null);
1531 	ZEND_PARSE_PARAMETERS_END();
1532 
1533 	bc_num num = NULL;
1534 	size_t num_full_scale;
1535 	if (bc_num_from_obj_or_str_or_long_with_err(&num, &num_full_scale, num_obj, num_str, num_lval, 1) == FAILURE) {
1536 		goto fail;
1537 	}
1538 	if (bcmath_check_scale(scale_lval, 2) == FAILURE) {
1539 		goto fail;
1540 	}
1541 
1542 	bc_num quot = NULL;
1543 	bc_num rem = NULL;
1544 	size_t scale = scale_lval;
1545 	bcmath_number_obj_t *intern = get_bcmath_number_from_zval(ZEND_THIS);
1546 
1547 	if (scale_is_null) {
1548 		scale = MAX(intern->scale, num_full_scale);
1549 	}
1550 
1551 	if (!bc_divmod(intern->num, num, &quot, &rem, scale)) {
1552 		zend_throw_exception_ex(zend_ce_division_by_zero_error, 0, "Division by zero");
1553 		goto fail;
1554 	}
1555 	bc_rm_trailing_zeros(quot);
1556 	bc_rm_trailing_zeros(rem);
1557 
1558 	if (num_obj == NULL) {
1559 		bc_free_num(&num);
1560 	}
1561 
1562 	bcmath_number_obj_t *quot_intern = bcmath_number_new_obj(quot, 0);
1563 	bcmath_number_obj_t *rem_intern = bcmath_number_new_obj(rem, scale);
1564 
1565 	zval z_quot, z_rem;
1566 	ZVAL_OBJ(&z_quot, &quot_intern->std);
1567 	ZVAL_OBJ(&z_rem, &rem_intern->std);
1568 
1569 	RETURN_ARR(zend_new_pair(&z_quot, &z_rem));
1570 
1571 fail:
1572 	if (num_obj == NULL) {
1573 		bc_free_num(&num);
1574 	}
1575 	RETURN_THROWS();
1576 }
1577 
PHP_METHOD(BcMath_Number,powmod)1578 PHP_METHOD(BcMath_Number, powmod)
1579 {
1580 	zend_object *exponent_obj = NULL;
1581 	zend_string *exponent_str = NULL;
1582 	zend_long exponent_lval = 0;
1583 
1584 	zend_object *modulus_obj = NULL;
1585 	zend_string *modulus_str = NULL;
1586 	zend_long modulus_lval = 0;
1587 
1588 	zend_long scale_lval = 0;
1589 	bool scale_is_null = true;
1590 
1591 	ZEND_PARSE_PARAMETERS_START(2, 3)
1592 		BCMATH_PARAM_NUMBER_OR_STR_OR_LONG(exponent_obj, bcmath_number_ce, exponent_str, exponent_lval);
1593 		BCMATH_PARAM_NUMBER_OR_STR_OR_LONG(modulus_obj, bcmath_number_ce, modulus_str, modulus_lval);
1594 		Z_PARAM_OPTIONAL
1595 		Z_PARAM_LONG_OR_NULL(scale_lval, scale_is_null);
1596 	ZEND_PARSE_PARAMETERS_END();
1597 
1598 	bc_num exponent_num = NULL;
1599 	bc_num modulus_num = NULL;
1600 	if (bc_num_from_obj_or_str_or_long_with_err(&exponent_num, NULL, exponent_obj, exponent_str, exponent_lval, 1) == FAILURE) {
1601 		goto cleanup;
1602 	}
1603 	if (bc_num_from_obj_or_str_or_long_with_err(&modulus_num, NULL, modulus_obj, modulus_str, modulus_lval, 2) == FAILURE) {
1604 		goto cleanup;
1605 	}
1606 	if (bcmath_check_scale(scale_lval, 3) == FAILURE) {
1607 		goto cleanup;
1608 	}
1609 
1610 	bcmath_number_obj_t *intern = get_bcmath_number_from_zval(ZEND_THIS);
1611 	bc_num ret = NULL;
1612 	size_t scale = scale_lval;
1613 	raise_mod_status status = bc_raisemod(intern->num, exponent_num, modulus_num, &ret, scale);
1614 	switch (status) {
1615 		case BASE_HAS_FRACTIONAL:
1616 			zend_value_error("Base number cannot have a fractional part");
1617 			goto cleanup;
1618 		case EXPO_HAS_FRACTIONAL:
1619 			zend_argument_value_error(1, "cannot have a fractional part");
1620 			goto cleanup;
1621 		case EXPO_IS_NEGATIVE:
1622 			zend_argument_value_error(1, "must be greater than or equal to 0");
1623 			goto cleanup;
1624 		case MOD_HAS_FRACTIONAL:
1625 			zend_argument_value_error(2, "cannot have a fractional part");
1626 			goto cleanup;
1627 		case MOD_IS_ZERO:
1628 			zend_throw_exception_ex(zend_ce_division_by_zero_error, 0, "Modulo by zero");
1629 			goto cleanup;
1630 		case OK:
1631 			break;
1632 		EMPTY_SWITCH_DEFAULT_CASE();
1633 	}
1634 
1635 	bc_rm_trailing_zeros(ret);
1636 
1637 	if (exponent_obj == NULL) {
1638 		bc_free_num(&exponent_num);
1639 	}
1640 	if (modulus_obj == NULL) {
1641 		bc_free_num(&modulus_num);
1642 	}
1643 
1644 	bcmath_number_obj_t *new_intern = bcmath_number_new_obj(ret, scale);
1645 	RETURN_OBJ(&new_intern->std);
1646 
1647 cleanup:
1648 	if (exponent_obj == NULL) {
1649 		bc_free_num(&exponent_num);
1650 	}
1651 	if (modulus_obj == NULL) {
1652 		bc_free_num(&modulus_num);
1653 	}
1654 	RETURN_THROWS();
1655 }
1656 
PHP_METHOD(BcMath_Number,sqrt)1657 PHP_METHOD(BcMath_Number, sqrt)
1658 {
1659 	zend_long scale_lval = 0;
1660 	bool scale_is_null = true;
1661 
1662 	ZEND_PARSE_PARAMETERS_START(0, 1)
1663 		Z_PARAM_OPTIONAL
1664 		Z_PARAM_LONG_OR_NULL(scale_lval, scale_is_null);
1665 	ZEND_PARSE_PARAMETERS_END();
1666 
1667 	if (bcmath_check_scale(scale_lval, 1) == FAILURE) {
1668 		RETURN_THROWS();
1669 	}
1670 
1671 	bcmath_number_obj_t *intern = get_bcmath_number_from_zval(ZEND_THIS);
1672 
1673 	size_t scale;
1674 	if (scale_is_null) {
1675 		scale = intern->scale + BC_MATH_NUMBER_EXPAND_SCALE;
1676 		if (UNEXPECTED(CHECK_RET_SCALE_OVERFLOW(scale, intern->scale))) {
1677 			zend_value_error("scale of the result is too large");
1678 			RETURN_THROWS();
1679 		}
1680 	} else {
1681 		scale = scale_lval;
1682 	}
1683 
1684 	bc_num ret = bc_copy_num(intern->num);
1685 	if (!bc_sqrt (&ret, scale)) {
1686 		zend_value_error("Base number must be greater than or equal to 0");
1687 		bc_free_num(&ret);
1688 		RETURN_THROWS();
1689 	}
1690 
1691 	ret->n_scale = MIN(scale, ret->n_scale);
1692 	bc_rm_trailing_zeros(ret);
1693 	if (scale_is_null) {
1694 		size_t diff = scale - ret->n_scale;
1695 		scale -= diff > BC_MATH_NUMBER_EXPAND_SCALE ? BC_MATH_NUMBER_EXPAND_SCALE : diff;
1696 	}
1697 
1698 	bcmath_number_obj_t *new_intern = bcmath_number_new_obj(ret, scale);
1699 	RETURN_OBJ(&new_intern->std);
1700 }
1701 
PHP_METHOD(BcMath_Number,compare)1702 PHP_METHOD(BcMath_Number, compare)
1703 {
1704 	zend_object *num_obj = NULL;
1705 	zend_string *num_str = NULL;
1706 	zend_long num_lval = 0;
1707 	zend_long scale_lval = 0;
1708 	bool scale_is_null = true;
1709 
1710 	ZEND_PARSE_PARAMETERS_START(1, 2)
1711 		BCMATH_PARAM_NUMBER_OR_STR_OR_LONG(num_obj, bcmath_number_ce, num_str, num_lval);
1712 		Z_PARAM_OPTIONAL
1713 		Z_PARAM_LONG_OR_NULL(scale_lval, scale_is_null);
1714 	ZEND_PARSE_PARAMETERS_END();
1715 
1716 	bc_num num = NULL;
1717 	size_t num_full_scale = 0;
1718 	if (bc_num_from_obj_or_str_or_long_with_err(&num, &num_full_scale, num_obj, num_str, num_lval, 1) == FAILURE) {
1719 		goto fail;
1720 	}
1721 	if (bcmath_check_scale(scale_lval, 2) == FAILURE) {
1722 		goto fail;
1723 	}
1724 
1725 	size_t scale;
1726 	bcmath_number_obj_t *intern = get_bcmath_number_from_zval(ZEND_THIS);
1727 	if (scale_is_null) {
1728 		scale = MAX(intern->num->n_scale, num->n_scale);
1729 	} else {
1730 		scale = scale_lval;
1731 	}
1732 	zend_long ret = bc_compare(intern->num, num, scale);
1733 
1734 	if (num_obj == NULL) {
1735 		bc_free_num(&num);
1736 	}
1737 	RETURN_LONG(ret);
1738 
1739 fail:
1740 	if (num_obj == NULL) {
1741 		bc_free_num(&num);
1742 	}
1743 	RETURN_THROWS();
1744 }
1745 
bcmath_number_floor_or_ceil(INTERNAL_FUNCTION_PARAMETERS,bool is_floor)1746 static void bcmath_number_floor_or_ceil(INTERNAL_FUNCTION_PARAMETERS, bool is_floor)
1747 {
1748 	ZEND_PARSE_PARAMETERS_NONE();
1749 
1750 	bcmath_number_obj_t *intern = get_bcmath_number_from_zval(ZEND_THIS);
1751 
1752 	bc_num ret = bc_floor_or_ceil(intern->num, is_floor);
1753 
1754 	bcmath_number_obj_t *new_intern = bcmath_number_new_obj(ret, 0);
1755 	RETURN_OBJ(&new_intern->std);
1756 }
1757 
PHP_METHOD(BcMath_Number,floor)1758 PHP_METHOD(BcMath_Number, floor)
1759 {
1760 	bcmath_number_floor_or_ceil(INTERNAL_FUNCTION_PARAM_PASSTHRU, true);
1761 }
1762 
PHP_METHOD(BcMath_Number,ceil)1763 PHP_METHOD(BcMath_Number, ceil)
1764 {
1765 	bcmath_number_floor_or_ceil(INTERNAL_FUNCTION_PARAM_PASSTHRU, false);
1766 }
1767 
PHP_METHOD(BcMath_Number,round)1768 PHP_METHOD(BcMath_Number, round)
1769 {
1770 	zend_long precision = 0;
1771 	zend_long rounding_mode = PHP_ROUND_HALF_UP;
1772 	zend_object *mode_object = NULL;
1773 
1774 	ZEND_PARSE_PARAMETERS_START(0, 2)
1775 		Z_PARAM_OPTIONAL
1776 		Z_PARAM_LONG(precision);
1777 		Z_PARAM_OBJ_OF_CLASS(mode_object, rounding_mode_ce);
1778 	ZEND_PARSE_PARAMETERS_END();
1779 
1780 	if (mode_object != NULL) {
1781 		rounding_mode = php_math_round_mode_from_enum(mode_object);
1782 	}
1783 
1784 	switch (rounding_mode) {
1785 		case PHP_ROUND_HALF_UP:
1786 		case PHP_ROUND_HALF_DOWN:
1787 		case PHP_ROUND_HALF_EVEN:
1788 		case PHP_ROUND_HALF_ODD:
1789 		case PHP_ROUND_CEILING:
1790 		case PHP_ROUND_FLOOR:
1791 		case PHP_ROUND_TOWARD_ZERO:
1792 		case PHP_ROUND_AWAY_FROM_ZERO:
1793 			break;
1794 		default:
1795 			zend_argument_value_error(2, "is an unsupported rounding mode");
1796 			RETURN_THROWS();
1797 	}
1798 
1799 	bcmath_number_obj_t *intern = get_bcmath_number_from_zval(ZEND_THIS);
1800 
1801 	bc_num ret = NULL;
1802 	bc_round(intern->num, precision, rounding_mode, &ret);
1803 
1804 	bc_rm_trailing_zeros(ret);
1805 
1806 	bcmath_number_obj_t *new_intern = bcmath_number_new_obj(ret, ret->n_scale);
1807 	RETURN_OBJ(&new_intern->std);
1808 }
1809 
PHP_METHOD(BcMath_Number,__toString)1810 PHP_METHOD(BcMath_Number, __toString)
1811 {
1812 	ZEND_PARSE_PARAMETERS_NONE();
1813 	bcmath_number_obj_t *intern = get_bcmath_number_from_zval(ZEND_THIS);
1814 	RETURN_STR_COPY(bcmath_number_value_to_str(intern));
1815 }
1816 
PHP_METHOD(BcMath_Number,__serialize)1817 PHP_METHOD(BcMath_Number, __serialize)
1818 {
1819 	ZEND_PARSE_PARAMETERS_NONE();
1820 
1821 	bcmath_number_obj_t *intern = get_bcmath_number_from_zval(ZEND_THIS);
1822 
1823 	array_init(return_value);
1824 	HashTable *props = Z_ARRVAL_P(return_value);
1825 
1826 	zval zv;
1827 	ZVAL_STR_COPY(&zv, bcmath_number_value_to_str(intern));
1828 	zend_hash_update(props, ZSTR_KNOWN(ZEND_STR_VALUE), &zv);
1829 }
1830 
PHP_METHOD(BcMath_Number,__unserialize)1831 PHP_METHOD(BcMath_Number, __unserialize)
1832 {
1833 	HashTable *props;
1834 
1835 	ZEND_PARSE_PARAMETERS_START(1, 1)
1836 		Z_PARAM_ARRAY_HT(props)
1837 	ZEND_PARSE_PARAMETERS_END();
1838 
1839 	zval *zv = zend_hash_find(props, ZSTR_KNOWN(ZEND_STR_VALUE));
1840 	if (!zv || Z_TYPE_P(zv) != IS_STRING || Z_STRLEN_P(zv) == 0) {
1841 		goto fail;
1842 	}
1843 
1844 	bcmath_number_obj_t *intern = get_bcmath_number_from_zval(ZEND_THIS);
1845 	if (UNEXPECTED(intern->num != NULL)) {
1846 		zend_readonly_property_modification_error_ex(ZSTR_VAL(bcmath_number_ce->name), "value");
1847 		RETURN_THROWS();
1848 	}
1849 
1850 	bc_num num = NULL;
1851 	size_t scale = 0;
1852 	if (php_str2num_ex(&num, Z_STR_P(zv), &scale) == FAILURE || CHECK_SCALE_OVERFLOW(scale)) {
1853 		bc_free_num(&num);
1854 		goto fail;
1855 	}
1856 
1857 	intern->num = num;
1858 	intern->scale = scale;
1859 
1860 	return;
1861 
1862 fail:
1863 	zend_throw_exception_ex(NULL, 0, "Invalid serialization data for %s object", ZSTR_VAL(bcmath_number_ce->name));
1864 	RETURN_THROWS();
1865 }
1866 
1867 #endif
1868