PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/gmp - gmp.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 800 814 98.3 %
Date: 2019-05-06 Functions: 80 81 98.8 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 7                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) The PHP Group                                          |
       6             :    +----------------------------------------------------------------------+
       7             :    | This source file is subject to version 3.01 of the PHP license,      |
       8             :    | that is bundled with this package in the file LICENSE, and is        |
       9             :    | available through the world-wide-web at the following url:           |
      10             :    | http://www.php.net/license/3_01.txt                                  |
      11             :    | If you did not receive a copy of the PHP license and are unable to   |
      12             :    | obtain it through the world-wide-web, please send a note to          |
      13             :    | license@php.net so we can mail you a copy immediately.               |
      14             :    +----------------------------------------------------------------------+
      15             :    | Author: Stanislav Malyshev <stas@php.net>                            |
      16             :    +----------------------------------------------------------------------+
      17             :  */
      18             : 
      19             : #ifdef HAVE_CONFIG_H
      20             : #include "config.h"
      21             : #endif
      22             : 
      23             : #include "php.h"
      24             : #include "php_ini.h"
      25             : #include "php_gmp.h"
      26             : #include "php_gmp_int.h"
      27             : #include "ext/standard/info.h"
      28             : #include "ext/standard/php_var.h"
      29             : #include "zend_smart_str_public.h"
      30             : #include "zend_exceptions.h"
      31             : 
      32             : #include <gmp.h>
      33             : 
      34             : /* Needed for gmp_random() */
      35             : #include "ext/standard/php_rand.h"
      36             : #include "ext/standard/php_lcg.h"
      37             : #define GMP_ABS(x) ((x) >= 0 ? (x) : -(x))
      38             : 
      39             : /* {{{ arginfo */
      40             : ZEND_BEGIN_ARG_INFO_EX(arginfo_gmp_init, 0, 0, 1)
      41             :         ZEND_ARG_INFO(0, number)
      42             :         ZEND_ARG_INFO(0, base)
      43             : ZEND_END_ARG_INFO()
      44             : 
      45             : ZEND_BEGIN_ARG_INFO_EX(arginfo_gmp_import, 0, 0, 1)
      46             :         ZEND_ARG_INFO(0, data)
      47             :         ZEND_ARG_INFO(0, word_size)
      48             :         ZEND_ARG_INFO(0, options)
      49             : ZEND_END_ARG_INFO()
      50             : 
      51             : ZEND_BEGIN_ARG_INFO_EX(arginfo_gmp_export, 0, 0, 1)
      52             :         ZEND_ARG_INFO(0, gmpnumber)
      53             :         ZEND_ARG_INFO(0, word_size)
      54             :         ZEND_ARG_INFO(0, options)
      55             : ZEND_END_ARG_INFO()
      56             : 
      57             : ZEND_BEGIN_ARG_INFO_EX(arginfo_gmp_intval, 0, 0, 1)
      58             :         ZEND_ARG_INFO(0, gmpnumber)
      59             : ZEND_END_ARG_INFO()
      60             : 
      61             : ZEND_BEGIN_ARG_INFO_EX(arginfo_gmp_strval, 0, 0, 1)
      62             :         ZEND_ARG_INFO(0, gmpnumber)
      63             :         ZEND_ARG_INFO(0, base)
      64             : ZEND_END_ARG_INFO()
      65             : 
      66             : ZEND_BEGIN_ARG_INFO_EX(arginfo_gmp_unary, 0, 0, 1)
      67             :         ZEND_ARG_INFO(0, a)
      68             : ZEND_END_ARG_INFO()
      69             : 
      70             : ZEND_BEGIN_ARG_INFO_EX(arginfo_gmp_binary, 0, 0, 2)
      71             :         ZEND_ARG_INFO(0, a)
      72             :         ZEND_ARG_INFO(0, b)
      73             : ZEND_END_ARG_INFO()
      74             : 
      75             : ZEND_BEGIN_ARG_INFO_EX(arginfo_gmp_div, 0, 0, 2)
      76             :         ZEND_ARG_INFO(0, a)
      77             :         ZEND_ARG_INFO(0, b)
      78             :         ZEND_ARG_INFO(0, round)
      79             : ZEND_END_ARG_INFO()
      80             : 
      81             : ZEND_BEGIN_ARG_INFO_EX(arginfo_gmp_pow, 0, 0, 2)
      82             :         ZEND_ARG_INFO(0, base)
      83             :         ZEND_ARG_INFO(0, exp)
      84             : ZEND_END_ARG_INFO()
      85             : 
      86             : ZEND_BEGIN_ARG_INFO_EX(arginfo_gmp_powm, 0, 0, 3)
      87             :         ZEND_ARG_INFO(0, base)
      88             :         ZEND_ARG_INFO(0, exp)
      89             :         ZEND_ARG_INFO(0, mod)
      90             : ZEND_END_ARG_INFO()
      91             : 
      92             : ZEND_BEGIN_ARG_INFO_EX(arginfo_gmp_root, 0, 0, 2)
      93             :         ZEND_ARG_INFO(0, a)
      94             :         ZEND_ARG_INFO(0, nth)
      95             : ZEND_END_ARG_INFO()
      96             : 
      97             : ZEND_BEGIN_ARG_INFO_EX(arginfo_gmp_prob_prime, 0, 0, 1)
      98             :         ZEND_ARG_INFO(0, a)
      99             :         ZEND_ARG_INFO(0, reps)
     100             : ZEND_END_ARG_INFO()
     101             : 
     102             : ZEND_BEGIN_ARG_INFO_EX(arginfo_gmp_random_seed, 0, 0, 1)
     103             :         ZEND_ARG_INFO(0, seed)
     104             : ZEND_END_ARG_INFO()
     105             : 
     106             : ZEND_BEGIN_ARG_INFO_EX(arginfo_gmp_random_bits, 0, 0, 1)
     107             :         ZEND_ARG_INFO(0, bits)
     108             : ZEND_END_ARG_INFO()
     109             : 
     110             : ZEND_BEGIN_ARG_INFO_EX(arginfo_gmp_random_range, 0, 0, 2)
     111             :         ZEND_ARG_INFO(0, min)
     112             :         ZEND_ARG_INFO(0, max)
     113             : ZEND_END_ARG_INFO()
     114             : 
     115             : ZEND_BEGIN_ARG_INFO_EX(arginfo_gmp_setbit, 0, 0, 2)
     116             :         ZEND_ARG_INFO(0, a)
     117             :         ZEND_ARG_INFO(0, index)
     118             :         ZEND_ARG_INFO(0, set_clear)
     119             : ZEND_END_ARG_INFO()
     120             : 
     121             : ZEND_BEGIN_ARG_INFO_EX(arginfo_gmp_bit, 0, 0, 2)
     122             :         ZEND_ARG_INFO(0, a)
     123             :         ZEND_ARG_INFO(0, index)
     124             : ZEND_END_ARG_INFO()
     125             : 
     126             : ZEND_BEGIN_ARG_INFO_EX(arginfo_gmp_scan, 0, 0, 2)
     127             :         ZEND_ARG_INFO(0, a)
     128             :         ZEND_ARG_INFO(0, start)
     129             : ZEND_END_ARG_INFO()
     130             : 
     131             : /* }}} */
     132             : 
     133             : ZEND_DECLARE_MODULE_GLOBALS(gmp)
     134             : static ZEND_GINIT_FUNCTION(gmp);
     135             : 
     136             : /* {{{ gmp_functions[]
     137             :  */
     138             : static const zend_function_entry gmp_functions[] = {
     139             :         ZEND_FE(gmp_init,               arginfo_gmp_init)
     140             :         ZEND_FE(gmp_import,             arginfo_gmp_import)
     141             :         ZEND_FE(gmp_export,             arginfo_gmp_export)
     142             :         ZEND_FE(gmp_intval,             arginfo_gmp_intval)
     143             :         ZEND_FE(gmp_strval,             arginfo_gmp_strval)
     144             :         ZEND_FE(gmp_add,                arginfo_gmp_binary)
     145             :         ZEND_FE(gmp_sub,                arginfo_gmp_binary)
     146             :         ZEND_FE(gmp_mul,                arginfo_gmp_binary)
     147             :         ZEND_FE(gmp_div_qr,             arginfo_gmp_div)
     148             :         ZEND_FE(gmp_div_q,              arginfo_gmp_div)
     149             :         ZEND_FE(gmp_div_r,              arginfo_gmp_div)
     150             :         ZEND_FALIAS(gmp_div, gmp_div_q, arginfo_gmp_div)
     151             :         ZEND_FE(gmp_mod,                arginfo_gmp_binary)
     152             :         ZEND_FE(gmp_divexact,   arginfo_gmp_binary)
     153             :         ZEND_FE(gmp_neg,                arginfo_gmp_unary)
     154             :         ZEND_FE(gmp_abs,                arginfo_gmp_unary)
     155             :         ZEND_FE(gmp_fact,               arginfo_gmp_unary)
     156             :         ZEND_FE(gmp_sqrt,               arginfo_gmp_unary)
     157             :         ZEND_FE(gmp_sqrtrem,    arginfo_gmp_unary)
     158             :         ZEND_FE(gmp_root,               arginfo_gmp_root)
     159             :         ZEND_FE(gmp_rootrem,    arginfo_gmp_root)
     160             :         ZEND_FE(gmp_pow,                arginfo_gmp_pow)
     161             :         ZEND_FE(gmp_powm,               arginfo_gmp_powm)
     162             :         ZEND_FE(gmp_perfect_square,     arginfo_gmp_unary)
     163             :         ZEND_FE(gmp_perfect_power,      arginfo_gmp_unary)
     164             :         ZEND_FE(gmp_prob_prime, arginfo_gmp_prob_prime)
     165             :         ZEND_FE(gmp_gcd,                arginfo_gmp_binary)
     166             :         ZEND_FE(gmp_gcdext,             arginfo_gmp_binary)
     167             :         ZEND_FE(gmp_lcm,                arginfo_gmp_binary)
     168             :         ZEND_FE(gmp_invert,             arginfo_gmp_binary)
     169             :         ZEND_FE(gmp_jacobi,             arginfo_gmp_binary)
     170             :         ZEND_FE(gmp_legendre,   arginfo_gmp_binary)
     171             :         ZEND_FE(gmp_kronecker,  arginfo_gmp_binary)
     172             :         ZEND_FE(gmp_cmp,                arginfo_gmp_binary)
     173             :         ZEND_FE(gmp_sign,               arginfo_gmp_unary)
     174             :         ZEND_FE(gmp_random_seed,        arginfo_gmp_random_seed)
     175             :         ZEND_FE(gmp_random_bits,  arginfo_gmp_random_bits)
     176             :         ZEND_FE(gmp_random_range, arginfo_gmp_random_range)
     177             :         ZEND_FE(gmp_and,                arginfo_gmp_binary)
     178             :         ZEND_FE(gmp_or,                 arginfo_gmp_binary)
     179             :         ZEND_FE(gmp_com,                arginfo_gmp_unary)
     180             :         ZEND_FE(gmp_xor,                arginfo_gmp_binary)
     181             :         ZEND_FE(gmp_setbit,             arginfo_gmp_setbit)
     182             :         ZEND_FE(gmp_clrbit,             arginfo_gmp_bit)
     183             :         ZEND_FE(gmp_testbit,    arginfo_gmp_bit)
     184             :         ZEND_FE(gmp_scan0,      arginfo_gmp_scan)
     185             :         ZEND_FE(gmp_scan1,      arginfo_gmp_scan)
     186             :         ZEND_FE(gmp_popcount,   arginfo_gmp_unary)
     187             :         ZEND_FE(gmp_hamdist,    arginfo_gmp_binary)
     188             :         ZEND_FE(gmp_nextprime,  arginfo_gmp_unary)
     189             :         ZEND_FE(gmp_binomial,   arginfo_gmp_binary)
     190             :         PHP_FE_END
     191             : };
     192             : /* }}} */
     193             : 
     194             : /* {{{ gmp_module_entry
     195             :  */
     196             : zend_module_entry gmp_module_entry = {
     197             :         STANDARD_MODULE_HEADER,
     198             :         "gmp",
     199             :         gmp_functions,
     200             :         ZEND_MODULE_STARTUP_N(gmp),
     201             :         NULL,
     202             :         NULL,
     203             :         ZEND_MODULE_DEACTIVATE_N(gmp),
     204             :         ZEND_MODULE_INFO_N(gmp),
     205             :         PHP_GMP_VERSION,
     206             :         ZEND_MODULE_GLOBALS(gmp),
     207             :         ZEND_GINIT(gmp),
     208             :         NULL,
     209             :         NULL,
     210             :         STANDARD_MODULE_PROPERTIES_EX
     211             : };
     212             : /* }}} */
     213             : 
     214             : #ifdef COMPILE_DL_GMP
     215             : #ifdef ZTS
     216             : ZEND_TSRMLS_CACHE_DEFINE()
     217             : #endif
     218             : ZEND_GET_MODULE(gmp)
     219             : #endif
     220             : 
     221             : static zend_class_entry *gmp_ce;
     222             : static zend_object_handlers gmp_object_handlers;
     223             : 
     224           0 : PHP_GMP_API zend_class_entry *php_gmp_class_entry() {
     225           0 :         return gmp_ce;
     226             : }
     227             : 
     228             : typedef struct _gmp_temp {
     229             :         mpz_t num;
     230             :         zend_bool is_used;
     231             : } gmp_temp_t;
     232             : 
     233             : #define GMP_ROUND_ZERO      0
     234             : #define GMP_ROUND_PLUSINF   1
     235             : #define GMP_ROUND_MINUSINF  2
     236             : 
     237             : #define GMP_MSW_FIRST     (1 << 0)
     238             : #define GMP_LSW_FIRST     (1 << 1)
     239             : #define GMP_LITTLE_ENDIAN (1 << 2)
     240             : #define GMP_BIG_ENDIAN    (1 << 3)
     241             : #define GMP_NATIVE_ENDIAN (1 << 4)
     242             : 
     243             : #define GMP_MAX_BASE 62
     244             : 
     245             : #define GMP_51_OR_NEWER \
     246             :         ((__GNU_MP_VERSION >= 6) || (__GNU_MP_VERSION >= 5 && __GNU_MP_VERSION_MINOR >= 1))
     247             : 
     248             : #define IS_GMP(zval) \
     249             :         (Z_TYPE_P(zval) == IS_OBJECT && instanceof_function(Z_OBJCE_P(zval), gmp_ce))
     250             : 
     251             : #define GET_GMP_OBJECT_FROM_OBJ(obj) \
     252             :         php_gmp_object_from_zend_object(obj)
     253             : #define GET_GMP_OBJECT_FROM_ZVAL(zv) \
     254             :         GET_GMP_OBJECT_FROM_OBJ(Z_OBJ_P(zv))
     255             : 
     256             : #define GET_GMP_FROM_ZVAL(zval) \
     257             :         GET_GMP_OBJECT_FROM_OBJ(Z_OBJ_P(zval))->num
     258             : 
     259             : /* The FETCH_GMP_ZVAL_* family of macros is used to fetch a gmp number
     260             :  * (mpz_ptr) from a zval. If the zval is not a GMP instance, then we
     261             :  * try to convert the value to a temporary gmp number using convert_to_gmp.
     262             :  * This temporary number is stored in the temp argument, which is of type
     263             :  * gmp_temp_t. This temporary value needs to be freed lateron using the
     264             :  * FREE_GMP_TEMP macro.
     265             :  *
     266             :  * If the conversion to a gmp number fails, the macros return false.
     267             :  * The _DEP / _DEP_DEP variants additionally free the temporary values
     268             :  * passed in the last / last two arguments.
     269             :  *
     270             :  * If one zval can sometimes be fetched as a long you have to set the
     271             :  * is_used member of the corresponding gmp_temp_t value to 0, otherwise
     272             :  * the FREE_GMP_TEMP and *_DEP macros will not work properly.
     273             :  *
     274             :  * The three FETCH_GMP_ZVAL_* macros below are mostly copy & paste code
     275             :  * as I couldn't find a way to combine them.
     276             :  */
     277             : 
     278             : #define FREE_GMP_TEMP(temp)  \
     279             :         if (temp.is_used) {      \
     280             :                 mpz_clear(temp.num); \
     281             :         }
     282             : 
     283             : #define FETCH_GMP_ZVAL_DEP_DEP(gmpnumber, zval, temp, dep1, dep2) \
     284             : if (IS_GMP(zval)) {                                               \
     285             :         gmpnumber = GET_GMP_FROM_ZVAL(zval);                          \
     286             :         temp.is_used = 0;                                             \
     287             : } else {                                                          \
     288             :         mpz_init(temp.num);                                           \
     289             :         if (convert_to_gmp(temp.num, zval, 0) == FAILURE) { \
     290             :                 mpz_clear(temp.num);                                      \
     291             :                 FREE_GMP_TEMP(dep1);                                      \
     292             :                 FREE_GMP_TEMP(dep2);                                      \
     293             :                 RETURN_FALSE;                                             \
     294             :         }                                                             \
     295             :         temp.is_used = 1;                                             \
     296             :         gmpnumber = temp.num;                                         \
     297             : }
     298             : 
     299             : #define FETCH_GMP_ZVAL_DEP(gmpnumber, zval, temp, dep)            \
     300             : if (IS_GMP(zval)) {                                               \
     301             :         gmpnumber = GET_GMP_FROM_ZVAL(zval);                          \
     302             :         temp.is_used = 0;                                             \
     303             : } else {                                                          \
     304             :         mpz_init(temp.num);                                           \
     305             :         if (convert_to_gmp(temp.num, zval, 0) == FAILURE) { \
     306             :                 mpz_clear(temp.num);                                      \
     307             :                 FREE_GMP_TEMP(dep);                                       \
     308             :                 RETURN_FALSE;                                             \
     309             :         }                                                             \
     310             :         temp.is_used = 1;                                             \
     311             :         gmpnumber = temp.num;                                         \
     312             : }
     313             : 
     314             : #define FETCH_GMP_ZVAL(gmpnumber, zval, temp)                     \
     315             : if (IS_GMP(zval)) {                                               \
     316             :         gmpnumber = GET_GMP_FROM_ZVAL(zval);                          \
     317             :         temp.is_used = 0;                                             \
     318             : } else {                                                          \
     319             :         mpz_init(temp.num);                                           \
     320             :         if (convert_to_gmp(temp.num, zval, 0) == FAILURE) { \
     321             :                 mpz_clear(temp.num);                                      \
     322             :                 RETURN_FALSE;                                             \
     323             :         }                                                             \
     324             :         temp.is_used = 1;                                             \
     325             :         gmpnumber = temp.num;                                         \
     326             : }
     327             : 
     328             : #define INIT_GMP_RETVAL(gmpnumber) \
     329             :         gmp_create(return_value, &gmpnumber)
     330             : 
     331             : static void gmp_strval(zval *result, mpz_t gmpnum, int base);
     332             : static int convert_to_gmp(mpz_t gmpnumber, zval *val, zend_long base);
     333             : static void gmp_cmp(zval *return_value, zval *a_arg, zval *b_arg);
     334             : 
     335             : /*
     336             :  * The gmp_*_op functions provide an implementation for several common types
     337             :  * of GMP functions. The gmp_zval_(unary|binary)_*_op functions have to be manually
     338             :  * passed zvals to work on, whereas the gmp_(unary|binary)_*_op macros already
     339             :  * include parameter parsing.
     340             :  */
     341             : typedef void (*gmp_unary_op_t)(mpz_ptr, mpz_srcptr);
     342             : typedef int (*gmp_unary_opl_t)(mpz_srcptr);
     343             : 
     344             : typedef void (*gmp_unary_ui_op_t)(mpz_ptr, gmp_ulong);
     345             : 
     346             : typedef void (*gmp_binary_op_t)(mpz_ptr, mpz_srcptr, mpz_srcptr);
     347             : typedef int (*gmp_binary_opl_t)(mpz_srcptr, mpz_srcptr);
     348             : 
     349             : typedef void (*gmp_binary_ui_op_t)(mpz_ptr, mpz_srcptr, gmp_ulong);
     350             : typedef void (*gmp_binary_op2_t)(mpz_ptr, mpz_ptr, mpz_srcptr, mpz_srcptr);
     351             : typedef void (*gmp_binary_ui_op2_t)(mpz_ptr, mpz_ptr, mpz_srcptr, gmp_ulong);
     352             : 
     353             : static inline void gmp_zval_binary_ui_op(zval *return_value, zval *a_arg, zval *b_arg, gmp_binary_op_t gmp_op, gmp_binary_ui_op_t gmp_ui_op, int check_b_zero);
     354             : static inline void gmp_zval_binary_ui_op2(zval *return_value, zval *a_arg, zval *b_arg, gmp_binary_op2_t gmp_op, gmp_binary_ui_op2_t gmp_ui_op, int check_b_zero);
     355             : static inline void gmp_zval_unary_op(zval *return_value, zval *a_arg, gmp_unary_op_t gmp_op);
     356             : static inline void gmp_zval_unary_ui_op(zval *return_value, zval *a_arg, gmp_unary_ui_op_t gmp_op);
     357             : 
     358             : /* Binary operations */
     359             : #define gmp_binary_ui_op(op, uop) _gmp_binary_ui_op(INTERNAL_FUNCTION_PARAM_PASSTHRU, op, uop, 0)
     360             : #define gmp_binary_op(op)         _gmp_binary_ui_op(INTERNAL_FUNCTION_PARAM_PASSTHRU, op, NULL, 0)
     361             : #define gmp_binary_opl(op) _gmp_binary_opl(INTERNAL_FUNCTION_PARAM_PASSTHRU, op)
     362             : #define gmp_binary_ui_op_no_zero(op, uop) \
     363             :         _gmp_binary_ui_op(INTERNAL_FUNCTION_PARAM_PASSTHRU, op, uop, 1)
     364             : 
     365             : /* Unary operations */
     366             : #define gmp_unary_op(op)         _gmp_unary_op(INTERNAL_FUNCTION_PARAM_PASSTHRU, op)
     367             : #define gmp_unary_opl(op)         _gmp_unary_opl(INTERNAL_FUNCTION_PARAM_PASSTHRU, op)
     368             : #define gmp_unary_ui_op(op)      _gmp_unary_ui_op(INTERNAL_FUNCTION_PARAM_PASSTHRU, op)
     369             : 
     370       28605 : static void gmp_free_object_storage(zend_object *obj) /* {{{ */
     371             : {
     372       28605 :         gmp_object *intern = GET_GMP_OBJECT_FROM_OBJ(obj);
     373             : 
     374       28605 :         mpz_clear(intern->num);
     375       28605 :         zend_object_std_dtor(&intern->std);
     376       28605 : }
     377             : /* }}} */
     378             : 
     379       28605 : static inline zend_object *gmp_create_object_ex(zend_class_entry *ce, mpz_ptr *gmpnum_target) /* {{{ */
     380             : {
     381       28605 :         gmp_object *intern = emalloc(sizeof(gmp_object) + zend_object_properties_size(ce));
     382             : 
     383       28605 :         zend_object_std_init(&intern->std, ce);
     384       28605 :         object_properties_init(&intern->std, ce);
     385             : 
     386       28605 :         mpz_init(intern->num);
     387       28605 :         *gmpnum_target = intern->num;
     388       28605 :         intern->std.handlers = &gmp_object_handlers;
     389             : 
     390       28605 :         return &intern->std;
     391             : }
     392             : /* }}} */
     393             : 
     394           2 : static zend_object *gmp_create_object(zend_class_entry *ce) /* {{{ */
     395             : {
     396             :         mpz_ptr gmpnum_dummy;
     397           2 :         return gmp_create_object_ex(ce, &gmpnum_dummy);
     398             : }
     399             : /* }}} */
     400             : 
     401       28603 : static inline void gmp_create(zval *target, mpz_ptr *gmpnum_target) /* {{{ */
     402             : {
     403       28603 :         ZVAL_OBJ(target, gmp_create_object_ex(gmp_ce, gmpnum_target));
     404       28603 : }
     405             : /* }}} */
     406             : 
     407          24 : static int gmp_cast_object(zend_object *readobj, zval *writeobj, int type) /* {{{ */
     408             : {
     409             :         mpz_ptr gmpnum;
     410          24 :         switch (type) {
     411          15 :         case IS_STRING:
     412          15 :                 gmpnum = GET_GMP_OBJECT_FROM_OBJ(readobj)->num;
     413          15 :                 gmp_strval(writeobj, gmpnum, 10);
     414          15 :                 return SUCCESS;
     415           6 :         case IS_LONG:
     416           6 :                 gmpnum = GET_GMP_OBJECT_FROM_OBJ(readobj)->num;
     417           6 :                 ZVAL_LONG(writeobj, mpz_get_si(gmpnum));
     418           6 :                 return SUCCESS;
     419           1 :         case IS_DOUBLE:
     420           1 :                 gmpnum = GET_GMP_OBJECT_FROM_OBJ(readobj)->num;
     421           1 :                 ZVAL_DOUBLE(writeobj, mpz_get_d(gmpnum));
     422           1 :                 return SUCCESS;
     423           0 :         case _IS_NUMBER:
     424           0 :                 gmpnum = GET_GMP_OBJECT_FROM_OBJ(readobj)->num;
     425           0 :                 if (mpz_fits_slong_p(gmpnum)) {
     426           0 :                         ZVAL_LONG(writeobj, mpz_get_si(gmpnum));
     427             :                 } else {
     428           0 :                         ZVAL_DOUBLE(writeobj, mpz_get_d(gmpnum));
     429             :                 }
     430           0 :                 return SUCCESS;
     431           2 :         default:
     432           2 :                 return FAILURE;
     433             :         }
     434             : }
     435             : /* }}} */
     436             : 
     437         134 : static HashTable *gmp_get_debug_info(zend_object *obj, int *is_temp) /* {{{ */
     438             : {
     439         134 :         HashTable *ht, *props = zend_std_get_properties(obj);
     440         134 :         mpz_ptr gmpnum = GET_GMP_OBJECT_FROM_OBJ(obj)->num;
     441             :         zval zv;
     442             : 
     443         134 :         *is_temp = 1;
     444         134 :         ht = zend_array_dup(props);
     445             : 
     446         134 :         gmp_strval(&zv, gmpnum, 10);
     447         134 :         zend_hash_str_update(ht, "num", sizeof("num")-1, &zv);
     448             : 
     449         134 :         return ht;
     450             : }
     451             : /* }}} */
     452             : 
     453           1 : static zend_object *gmp_clone_obj(zend_object *obj) /* {{{ */
     454             : {
     455           1 :         gmp_object *old_object = GET_GMP_OBJECT_FROM_OBJ(obj);
     456           1 :         gmp_object *new_object = GET_GMP_OBJECT_FROM_OBJ(gmp_create_object(obj->ce));
     457             : 
     458           1 :         zend_objects_clone_members( &new_object->std, &old_object->std);
     459             : 
     460           1 :         mpz_set(new_object->num, old_object->num);
     461             : 
     462           1 :         return &new_object->std;
     463             : }
     464             : /* }}} */
     465             : 
     466        1011 : static void shift_operator_helper(gmp_binary_ui_op_t op, zval *return_value, zval *op1, zval *op2) {
     467        1011 :         zend_long shift = zval_get_long(op2);
     468             : 
     469        1011 :         if (shift < 0) {
     470           2 :                 php_error_docref(NULL, E_WARNING, "Shift cannot be negative");
     471           2 :                 RETVAL_FALSE;
     472             :         } else {
     473             :                 mpz_ptr gmpnum_op, gmpnum_result;
     474             :                 gmp_temp_t temp;
     475             : 
     476        1009 :                 FETCH_GMP_ZVAL(gmpnum_op, op1, temp);
     477        1009 :                 INIT_GMP_RETVAL(gmpnum_result);
     478        1009 :                 op(gmpnum_result, gmpnum_op, (gmp_ulong) shift);
     479        1009 :                 FREE_GMP_TEMP(temp);
     480             :         }
     481             : }
     482             : 
     483             : #define DO_BINARY_UI_OP_EX(op, uop, check_b_zero)       \
     484             :         gmp_zval_binary_ui_op(                              \
     485             :                 result, op1, op2, op, (gmp_binary_ui_op_t) uop, \
     486             :                 check_b_zero                          \
     487             :         );                                                  \
     488             :         return SUCCESS;
     489             : 
     490             : #define DO_BINARY_UI_OP(op) DO_BINARY_UI_OP_EX(op, op ## _ui, 0)
     491             : #define DO_BINARY_OP(op) DO_BINARY_UI_OP_EX(op, NULL, 0)
     492             : 
     493             : #define DO_UNARY_OP(op) \
     494             :         gmp_zval_unary_op(result, op1, op); \
     495             :         return SUCCESS;
     496             : 
     497        1062 : static int gmp_do_operation_ex(zend_uchar opcode, zval *result, zval *op1, zval *op2) /* {{{ */
     498             : {
     499        1062 :         switch (opcode) {
     500           7 :         case ZEND_ADD:
     501           7 :                 DO_BINARY_UI_OP(mpz_add);
     502           6 :         case ZEND_SUB:
     503           6 :                 DO_BINARY_UI_OP(mpz_sub);
     504           7 :         case ZEND_MUL:
     505           7 :                 DO_BINARY_UI_OP(mpz_mul);
     506           3 :         case ZEND_POW:
     507           3 :                 shift_operator_helper(mpz_pow_ui, result, op1, op2);
     508           3 :                 return SUCCESS;
     509           4 :         case ZEND_DIV:
     510           4 :                 DO_BINARY_UI_OP_EX(mpz_tdiv_q, mpz_tdiv_q_ui, 1);
     511           4 :         case ZEND_MOD:
     512           4 :                 DO_BINARY_UI_OP_EX(mpz_mod, mpz_mod_ui, 1);
     513        1005 :         case ZEND_SL:
     514        1005 :                 shift_operator_helper(mpz_mul_2exp, result, op1, op2);
     515        1005 :                 return SUCCESS;
     516           3 :         case ZEND_SR:
     517           3 :                 shift_operator_helper(mpz_fdiv_q_2exp, result, op1, op2);
     518           3 :                 return SUCCESS;
     519           3 :         case ZEND_BW_OR:
     520           3 :                 DO_BINARY_OP(mpz_ior);
     521           3 :         case ZEND_BW_AND:
     522           3 :                 DO_BINARY_OP(mpz_and);
     523           3 :         case ZEND_BW_XOR:
     524           3 :                 DO_BINARY_OP(mpz_xor);
     525           1 :         case ZEND_BW_NOT:
     526           1 :                 DO_UNARY_OP(mpz_com);
     527             : 
     528          13 :         default:
     529          13 :                 return FAILURE;
     530             :         }
     531             : }
     532             : /* }}} */
     533             : 
     534        1062 : static int gmp_do_operation(zend_uchar opcode, zval *result, zval *op1, zval *op2) /* {{{ */
     535             : {
     536             :         zval op1_copy;
     537             :         int retval;
     538             : 
     539        1062 :         if (result == op1) {
     540        1008 :                 ZVAL_COPY_VALUE(&op1_copy, op1);
     541        1008 :                 op1 = &op1_copy;
     542             :         }
     543             : 
     544        1062 :         retval = gmp_do_operation_ex(opcode, result, op1, op2);
     545             : 
     546        1062 :         if (retval == SUCCESS && op1 == &op1_copy) {
     547        1007 :                 zval_ptr_dtor(op1);
     548             :         }
     549             : 
     550        1062 :         return retval;
     551             : }
     552             : /* }}} */
     553             : 
     554       50022 : static int gmp_compare(zval *result, zval *op1, zval *op2) /* {{{ */
     555             : {
     556       50022 :         gmp_cmp(result, op1, op2);
     557       50022 :         if (Z_TYPE_P(result) == IS_FALSE) {
     558           1 :                 ZVAL_LONG(result, 1);
     559             :         }
     560       50022 :         return SUCCESS;
     561             : }
     562             : /* }}} */
     563             : 
     564           2 : static int gmp_serialize(zval *object, unsigned char **buffer, size_t *buf_len, zend_serialize_data *data) /* {{{ */
     565             : {
     566           2 :         mpz_ptr gmpnum = GET_GMP_FROM_ZVAL(object);
     567           2 :         smart_str buf = {0};
     568             :         zval zv;
     569             :         php_serialize_data_t serialize_data;
     570             : 
     571           2 :         PHP_VAR_SERIALIZE_INIT(serialize_data);
     572             : 
     573           2 :         gmp_strval(&zv, gmpnum, 10);
     574           2 :         php_var_serialize(&buf, &zv, &serialize_data);
     575             :         zval_ptr_dtor_str(&zv);
     576             : 
     577           2 :         ZVAL_ARR(&zv, zend_std_get_properties(Z_OBJ_P(object)));
     578           2 :         php_var_serialize(&buf, &zv, &serialize_data);
     579             : 
     580           2 :         PHP_VAR_SERIALIZE_DESTROY(serialize_data);
     581           2 :         *buffer = (unsigned char *) estrndup(ZSTR_VAL(buf.s), ZSTR_LEN(buf.s));
     582           2 :         *buf_len = ZSTR_LEN(buf.s);
     583           2 :         zend_string_release_ex(buf.s, 0);
     584             : 
     585           2 :         return SUCCESS;
     586             : }
     587             : /* }}} */
     588             : 
     589           7 : static int gmp_unserialize(zval *object, zend_class_entry *ce, const unsigned char *buf, size_t buf_len, zend_unserialize_data *data) /* {{{ */
     590             : {
     591             :         mpz_ptr gmpnum;
     592             :         const unsigned char *p, *max;
     593             :         zval *zv;
     594           7 :         int retval = FAILURE;
     595             :         php_unserialize_data_t unserialize_data;
     596             :         zend_object *zobj;
     597             : 
     598           7 :         PHP_VAR_UNSERIALIZE_INIT(unserialize_data);
     599           7 :         gmp_create(object, &gmpnum);
     600             : 
     601           7 :         zobj = Z_OBJ_P(object);
     602             : 
     603           7 :         p = buf;
     604           7 :         max = buf + buf_len;
     605             : 
     606           7 :         zv = var_tmp_var(&unserialize_data);
     607           7 :         if (!php_var_unserialize(zv, &p, max, &unserialize_data)
     608           5 :                 || Z_TYPE_P(zv) != IS_STRING
     609           5 :                 || convert_to_gmp(gmpnum, zv, 10) == FAILURE
     610             :         ) {
     611           2 :                 zend_throw_exception(NULL, "Could not unserialize number", 0);
     612           2 :                 goto exit;
     613             :         }
     614             : 
     615           5 :         zv = var_tmp_var(&unserialize_data);
     616           5 :         if (!php_var_unserialize(zv, &p, max, &unserialize_data)
     617           4 :                 || Z_TYPE_P(zv) != IS_ARRAY
     618             :         ) {
     619           1 :                 zend_throw_exception(NULL, "Could not unserialize properties", 0);
     620           1 :                 goto exit;
     621             :         }
     622             : 
     623           4 :         if (zend_hash_num_elements(Z_ARRVAL_P(zv)) != 0) {
     624           3 :                 zend_hash_copy(
     625           3 :                         zend_std_get_properties(zobj), Z_ARRVAL_P(zv),
     626             :                         (copy_ctor_func_t) zval_add_ref
     627             :                 );
     628             :         }
     629             : 
     630           4 :         retval = SUCCESS;
     631           7 : exit:
     632           7 :         PHP_VAR_UNSERIALIZE_DESTROY(unserialize_data);
     633           7 :         return retval;
     634             : }
     635             : /* }}} */
     636             : 
     637             : /* {{{ ZEND_GINIT_FUNCTION
     638             :  */
     639       23043 : static ZEND_GINIT_FUNCTION(gmp)
     640             : {
     641             : #if defined(COMPILE_DL_GMP) && defined(ZTS)
     642             :         ZEND_TSRMLS_CACHE_UPDATE();
     643             : #endif
     644       23043 :         gmp_globals->rand_initialized = 0;
     645       23043 : }
     646             : /* }}} */
     647             : 
     648             : /* {{{ ZEND_MINIT_FUNCTION
     649             :  */
     650       23043 : ZEND_MINIT_FUNCTION(gmp)
     651             : {
     652             :         zend_class_entry tmp_ce;
     653       23043 :         INIT_CLASS_ENTRY(tmp_ce, "GMP", NULL);
     654       23043 :         gmp_ce = zend_register_internal_class(&tmp_ce);
     655       23043 :         gmp_ce->create_object = gmp_create_object;
     656       23043 :         gmp_ce->serialize = gmp_serialize;
     657       23043 :         gmp_ce->unserialize = gmp_unserialize;
     658             : 
     659       23043 :         memcpy(&gmp_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
     660       23043 :         gmp_object_handlers.offset = XtOffsetOf(gmp_object, std);
     661       23043 :         gmp_object_handlers.free_obj = gmp_free_object_storage;
     662       23043 :         gmp_object_handlers.cast_object = gmp_cast_object;
     663       23043 :         gmp_object_handlers.get_debug_info = gmp_get_debug_info;
     664       23043 :         gmp_object_handlers.clone_obj = gmp_clone_obj;
     665       23043 :         gmp_object_handlers.do_operation = gmp_do_operation;
     666       23043 :         gmp_object_handlers.compare = gmp_compare;
     667             : 
     668       23043 :         REGISTER_LONG_CONSTANT("GMP_ROUND_ZERO", GMP_ROUND_ZERO, CONST_CS | CONST_PERSISTENT);
     669       23043 :         REGISTER_LONG_CONSTANT("GMP_ROUND_PLUSINF", GMP_ROUND_PLUSINF, CONST_CS | CONST_PERSISTENT);
     670       23043 :         REGISTER_LONG_CONSTANT("GMP_ROUND_MINUSINF", GMP_ROUND_MINUSINF, CONST_CS | CONST_PERSISTENT);
     671             : #ifdef mpir_version
     672             :         REGISTER_STRING_CONSTANT("GMP_MPIR_VERSION", (char *)mpir_version, CONST_CS | CONST_PERSISTENT);
     673             : #endif
     674       23043 :         REGISTER_STRING_CONSTANT("GMP_VERSION", (char *)gmp_version, CONST_CS | CONST_PERSISTENT);
     675             : 
     676       23043 :         REGISTER_LONG_CONSTANT("GMP_MSW_FIRST", GMP_MSW_FIRST, CONST_CS | CONST_PERSISTENT);
     677       23043 :         REGISTER_LONG_CONSTANT("GMP_LSW_FIRST", GMP_LSW_FIRST, CONST_CS | CONST_PERSISTENT);
     678       23043 :         REGISTER_LONG_CONSTANT("GMP_LITTLE_ENDIAN", GMP_LITTLE_ENDIAN, CONST_CS | CONST_PERSISTENT);
     679       23043 :         REGISTER_LONG_CONSTANT("GMP_BIG_ENDIAN", GMP_BIG_ENDIAN, CONST_CS | CONST_PERSISTENT);
     680       23043 :         REGISTER_LONG_CONSTANT("GMP_NATIVE_ENDIAN", GMP_NATIVE_ENDIAN, CONST_CS | CONST_PERSISTENT);
     681             : 
     682       23043 :         return SUCCESS;
     683             : }
     684             : /* }}} */
     685             : 
     686             : /* {{{ ZEND_RSHUTDOWN_FUNCTION
     687             :  */
     688       23122 : ZEND_MODULE_DEACTIVATE_D(gmp)
     689             : {
     690       23122 :         if (GMPG(rand_initialized)) {
     691           4 :                 gmp_randclear(GMPG(rand_state));
     692           4 :                 GMPG(rand_initialized) = 0;
     693             :         }
     694             : 
     695       23122 :         return SUCCESS;
     696             : }
     697             : /* }}} */
     698             : 
     699             : /* {{{ ZEND_MINFO_FUNCTION
     700             :  */
     701         146 : ZEND_MODULE_INFO_D(gmp)
     702             : {
     703         146 :         php_info_print_table_start();
     704         146 :         php_info_print_table_row(2, "gmp support", "enabled");
     705             : #ifdef mpir_version
     706             :         php_info_print_table_row(2, "MPIR version", mpir_version);
     707             : #else
     708         146 :         php_info_print_table_row(2, "GMP version", gmp_version);
     709             : #endif
     710         146 :         php_info_print_table_end();
     711         146 : }
     712             : /* }}} */
     713             : 
     714             : 
     715             : /* {{{ convert_to_gmp
     716             :  * Convert zval to be gmp number */
     717       52797 : static int convert_to_gmp(mpz_t gmpnumber, zval *val, zend_long base)
     718             : {
     719       52797 :         switch (Z_TYPE_P(val)) {
     720       52512 :         case IS_LONG:
     721             :         case IS_FALSE:
     722             :         case IS_TRUE: {
     723       52512 :                 mpz_set_si(gmpnumber, zval_get_long(val));
     724       52512 :                 return SUCCESS;
     725             :         }
     726         228 :         case IS_STRING: {
     727         228 :                 char *numstr = Z_STRVAL_P(val);
     728         228 :                 zend_bool skip_lead = 0;
     729             :                 int ret;
     730             : 
     731         228 :                 if (Z_STRLEN_P(val) > 2 && numstr[0] == '0') {
     732          12 :                         if ((base == 0 || base == 16) && (numstr[1] == 'x' || numstr[1] == 'X')) {
     733           3 :                                 base = 16;
     734           3 :                                 skip_lead = 1;
     735           9 :                         } else if ((base == 0 || base == 2) && (numstr[1] == 'b' || numstr[1] == 'B')) {
     736           2 :                                 base = 2;
     737           2 :                                 skip_lead = 1;
     738             :                         }
     739             :                 }
     740             : 
     741         228 :                 ret = mpz_set_str(gmpnumber, (skip_lead ? &numstr[2] : numstr), (int) base);
     742         228 :                 if (-1 == ret) {
     743          20 :                         php_error_docref(NULL, E_WARNING,
     744             :                                 "Unable to convert variable to GMP - string is not an integer");
     745          20 :                         return FAILURE;
     746             :                 }
     747             : 
     748         208 :                 return SUCCESS;
     749             :         }
     750          57 :         default:
     751          57 :                 php_error_docref(NULL, E_WARNING,
     752             :                         "Unable to convert variable to GMP - wrong type");
     753          57 :                 return FAILURE;
     754             :         }
     755             : }
     756             : /* }}} */
     757             : 
     758         487 : static void gmp_strval(zval *result, mpz_t gmpnum, int base) /* {{{ */
     759             : {
     760             :         size_t num_len;
     761             :         zend_string *str;
     762             : 
     763         487 :         num_len = mpz_sizeinbase(gmpnum, abs(base));
     764         487 :         if (mpz_sgn(gmpnum) < 0) {
     765          55 :                 num_len++;
     766             :         }
     767             : 
     768         487 :         str = zend_string_alloc(num_len, 0);
     769         487 :         mpz_get_str(ZSTR_VAL(str), base, gmpnum);
     770             : 
     771             :         /*
     772             :          * From GMP documentation for mpz_sizeinbase():
     773             :          * The returned value will be exact or 1 too big.  If base is a power of
     774             :          * 2, the returned value will always be exact.
     775             :          *
     776             :          * So let's check to see if we already have a \0 byte...
     777             :          */
     778             : 
     779         487 :         if (ZSTR_VAL(str)[ZSTR_LEN(str) - 1] == '\0') {
     780          77 :                 ZSTR_LEN(str)--;
     781             :         } else {
     782         410 :                 ZSTR_VAL(str)[ZSTR_LEN(str)] = '\0';
     783             :         }
     784             : 
     785         487 :         ZVAL_NEW_STR(result, str);
     786         487 : }
     787             : /* }}} */
     788             : 
     789       50031 : static void gmp_cmp(zval *return_value, zval *a_arg, zval *b_arg) /* {{{ */
     790             : {
     791             :         mpz_ptr gmpnum_a, gmpnum_b;
     792             :         gmp_temp_t temp_a, temp_b;
     793       50031 :         zend_bool use_si = 0;
     794             :         zend_long res;
     795             : 
     796       50031 :         FETCH_GMP_ZVAL(gmpnum_a, a_arg, temp_a);
     797             : 
     798       50030 :         if (Z_TYPE_P(b_arg) == IS_LONG) {
     799       25012 :                 use_si = 1;
     800       25012 :                 temp_b.is_used = 0;
     801             :         } else {
     802       25018 :                 FETCH_GMP_ZVAL_DEP(gmpnum_b, b_arg, temp_b, temp_a);
     803             :         }
     804             : 
     805       50029 :         if (use_si) {
     806       25012 :                 res = mpz_cmp_si(gmpnum_a, Z_LVAL_P(b_arg));
     807             :         } else {
     808       25017 :                 res = mpz_cmp(gmpnum_a, gmpnum_b);
     809             :         }
     810             : 
     811       50029 :         FREE_GMP_TEMP(temp_a);
     812       50029 :         FREE_GMP_TEMP(temp_b);
     813             : 
     814       50029 :         RETURN_LONG(res);
     815             : }
     816             : /* }}} */
     817             : 
     818             : /* {{{ gmp_zval_binary_ui_op
     819             :    Execute GMP binary operation.
     820             : */
     821        1195 : static inline void gmp_zval_binary_ui_op(zval *return_value, zval *a_arg, zval *b_arg, gmp_binary_op_t gmp_op, gmp_binary_ui_op_t gmp_ui_op, int check_b_zero)
     822             : {
     823             :         mpz_ptr gmpnum_a, gmpnum_b, gmpnum_result;
     824             :         gmp_temp_t temp_a, temp_b;
     825             : 
     826        1224 :         FETCH_GMP_ZVAL(gmpnum_a, a_arg, temp_a);
     827             : 
     828        2314 :         if (gmp_ui_op && Z_TYPE_P(b_arg) == IS_LONG && Z_LVAL_P(b_arg) >= 0) {
     829          68 :                 gmpnum_b = NULL;
     830          68 :                 temp_b.is_used = 0;
     831             :         } else {
     832        1109 :                 FETCH_GMP_ZVAL_DEP(gmpnum_b, b_arg, temp_b, temp_a);
     833             :         }
     834             : 
     835        1173 :         if (check_b_zero) {
     836          56 :                 int b_is_zero = 0;
     837          56 :                 if (!gmpnum_b) {
     838          30 :                         b_is_zero = (Z_LVAL_P(b_arg) == 0);
     839             :                 } else {
     840          26 :                         b_is_zero = !mpz_cmp_ui(gmpnum_b, 0);
     841             :                 }
     842             : 
     843          56 :                 if (b_is_zero) {
     844           7 :                         php_error_docref(NULL, E_WARNING, "Zero operand not allowed");
     845           7 :                         FREE_GMP_TEMP(temp_a);
     846           7 :                         FREE_GMP_TEMP(temp_b);
     847           7 :                         RETURN_FALSE;
     848             :                 }
     849             :         }
     850             : 
     851        1166 :         INIT_GMP_RETVAL(gmpnum_result);
     852             : 
     853        1166 :         if (!gmpnum_b) {
     854          62 :                 gmp_ui_op(gmpnum_result, gmpnum_a, (gmp_ulong) Z_LVAL_P(b_arg));
     855             :         } else {
     856        1104 :                 gmp_op(gmpnum_result, gmpnum_a, gmpnum_b);
     857             :         }
     858             : 
     859        1166 :         FREE_GMP_TEMP(temp_a);
     860        1166 :         FREE_GMP_TEMP(temp_b);
     861             : }
     862             : /* }}} */
     863             : 
     864             : /* {{{ gmp_zval_binary_ui_op2
     865             :    Execute GMP binary operation which returns 2 values.
     866             : */
     867          14 : static inline void gmp_zval_binary_ui_op2(zval *return_value, zval *a_arg, zval *b_arg, gmp_binary_op2_t gmp_op, gmp_binary_ui_op2_t gmp_ui_op, int check_b_zero)
     868             : {
     869             :         mpz_ptr gmpnum_a, gmpnum_b, gmpnum_result1, gmpnum_result2;
     870             :         gmp_temp_t temp_a, temp_b;
     871             :         zval result1, result2;
     872             : 
     873          19 :         FETCH_GMP_ZVAL(gmpnum_a, a_arg, temp_a);
     874             : 
     875          24 :         if (gmp_ui_op && Z_TYPE_P(b_arg) == IS_LONG && Z_LVAL_P(b_arg) >= 0) {
     876          10 :                 gmpnum_b = NULL;
     877          10 :                 temp_b.is_used = 0;
     878             :         } else {
     879           2 :                 FETCH_GMP_ZVAL_DEP(gmpnum_b, b_arg, temp_b, temp_a);
     880             :         }
     881             : 
     882          12 :         if (check_b_zero) {
     883          12 :                 int b_is_zero = 0;
     884          12 :                 if (!gmpnum_b) {
     885          10 :                         b_is_zero = (Z_LVAL_P(b_arg) == 0);
     886             :                 } else {
     887           2 :                         b_is_zero = !mpz_cmp_ui(gmpnum_b, 0);
     888             :                 }
     889             : 
     890          12 :                 if (b_is_zero) {
     891           3 :                         php_error_docref(NULL, E_WARNING, "Zero operand not allowed");
     892           3 :                         FREE_GMP_TEMP(temp_a);
     893           3 :                         FREE_GMP_TEMP(temp_b);
     894           3 :                         RETURN_FALSE;
     895             :                 }
     896             :         }
     897             : 
     898           9 :         gmp_create(&result1, &gmpnum_result1);
     899           9 :         gmp_create(&result2, &gmpnum_result2);
     900             : 
     901           9 :         array_init(return_value);
     902           9 :         add_next_index_zval(return_value, &result1);
     903           9 :         add_next_index_zval(return_value, &result2);
     904             : 
     905           9 :         if (!gmpnum_b) {
     906           8 :                 gmp_ui_op(gmpnum_result1, gmpnum_result2, gmpnum_a, (gmp_ulong) Z_LVAL_P(b_arg));
     907             :         } else {
     908           1 :                 gmp_op(gmpnum_result1, gmpnum_result2, gmpnum_a, gmpnum_b);
     909             :         }
     910             : 
     911           9 :         FREE_GMP_TEMP(temp_a);
     912           9 :         FREE_GMP_TEMP(temp_b);
     913             : }
     914             : /* }}} */
     915             : 
     916             : /* {{{ _gmp_binary_ui_op
     917             :  */
     918        1135 : static inline void _gmp_binary_ui_op(INTERNAL_FUNCTION_PARAMETERS, gmp_binary_op_t gmp_op, gmp_binary_ui_op_t gmp_ui_op, int check_b_zero)
     919             : {
     920             :         zval *a_arg, *b_arg;
     921             : 
     922        1135 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &a_arg, &b_arg) == FAILURE){
     923           0 :                 return;
     924             :         }
     925             : 
     926        1135 :         gmp_zval_binary_ui_op(return_value, a_arg, b_arg, gmp_op, gmp_ui_op, check_b_zero);
     927             : }
     928             : /* }}} */
     929             : 
     930             : /* Unary operations */
     931             : 
     932             : /* {{{ gmp_zval_unary_op
     933             :  */
     934          38 : static inline void gmp_zval_unary_op(zval *return_value, zval *a_arg, gmp_unary_op_t gmp_op)
     935             : {
     936             :         mpz_ptr gmpnum_a, gmpnum_result;
     937             :         gmp_temp_t temp_a;
     938             : 
     939          38 :         FETCH_GMP_ZVAL(gmpnum_a, a_arg, temp_a);
     940             : 
     941          26 :         INIT_GMP_RETVAL(gmpnum_result);
     942          26 :         gmp_op(gmpnum_result, gmpnum_a);
     943             : 
     944          26 :         FREE_GMP_TEMP(temp_a);
     945             : }
     946             : /* }}} */
     947             : 
     948             : /* {{{ gmp_zval_unary_ui_op
     949             :  */
     950          11 : static inline void gmp_zval_unary_ui_op(zval *return_value, zval *a_arg, gmp_unary_ui_op_t gmp_op)
     951             : {
     952             :         mpz_ptr gmpnum_result;
     953             : 
     954          11 :         INIT_GMP_RETVAL(gmpnum_result);
     955          11 :         gmp_op(gmpnum_result, zval_get_long(a_arg));
     956          11 : }
     957             : /* }}} */
     958             : 
     959             : /* {{{ _gmp_unary_ui_op
     960             :    Execute GMP unary operation.
     961             : */
     962             : static inline void _gmp_unary_ui_op(INTERNAL_FUNCTION_PARAMETERS, gmp_unary_ui_op_t gmp_op)
     963             : {
     964             :         zval *a_arg;
     965             : 
     966             :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &a_arg) == FAILURE){
     967             :                 return;
     968             :         }
     969             : 
     970             :         gmp_zval_unary_ui_op(return_value, a_arg, gmp_op);
     971             : }
     972             : /* }}} */
     973             : 
     974             : /* {{{ _gmp_unary_op
     975             :  */
     976          37 : static inline void _gmp_unary_op(INTERNAL_FUNCTION_PARAMETERS, gmp_unary_op_t gmp_op)
     977             : {
     978             :         zval *a_arg;
     979             : 
     980          37 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &a_arg) == FAILURE){
     981           0 :                 return;
     982             :         }
     983             : 
     984          37 :         gmp_zval_unary_op(return_value, a_arg, gmp_op);
     985             : }
     986             : /* }}} */
     987             : 
     988             : /* {{{ _gmp_unary_opl
     989             :  */
     990           7 : static inline void _gmp_unary_opl(INTERNAL_FUNCTION_PARAMETERS, gmp_unary_opl_t gmp_op)
     991             : {
     992             :         zval *a_arg;
     993             :         mpz_ptr gmpnum_a;
     994             :         gmp_temp_t temp_a;
     995             : 
     996           7 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &a_arg) == FAILURE){
     997           1 :                 return;
     998             :         }
     999             : 
    1000          14 :         FETCH_GMP_ZVAL(gmpnum_a, a_arg, temp_a);
    1001           6 :         RETVAL_LONG(gmp_op(gmpnum_a));
    1002           6 :         FREE_GMP_TEMP(temp_a);
    1003             : }
    1004             : /* }}} */
    1005             : 
    1006             : /* {{{ _gmp_binary_opl
    1007             :  */
    1008          50 : static inline void _gmp_binary_opl(INTERNAL_FUNCTION_PARAMETERS, gmp_binary_opl_t gmp_op)
    1009             : {
    1010             :         zval *a_arg, *b_arg;
    1011             :         mpz_ptr gmpnum_a, gmpnum_b;
    1012             :         gmp_temp_t temp_a, temp_b;
    1013             : 
    1014          50 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &a_arg, &b_arg) == FAILURE){
    1015           9 :                 return;
    1016             :         }
    1017             : 
    1018         100 :         FETCH_GMP_ZVAL(gmpnum_a, a_arg, temp_a);
    1019          88 :         FETCH_GMP_ZVAL_DEP(gmpnum_b, b_arg, temp_b, temp_a);
    1020             : 
    1021          41 :         RETVAL_LONG(gmp_op(gmpnum_a, gmpnum_b));
    1022             : 
    1023          41 :         FREE_GMP_TEMP(temp_a);
    1024          41 :         FREE_GMP_TEMP(temp_b);
    1025             : }
    1026             : /* }}} */
    1027             : 
    1028             : /* {{{ proto GMP gmp_init(mixed number [, int base])
    1029             :    Initializes GMP number */
    1030        1176 : ZEND_FUNCTION(gmp_init)
    1031             : {
    1032             :         zval *number_arg;
    1033             :         mpz_ptr gmpnumber;
    1034        1176 :         zend_long base = 0;
    1035             : 
    1036        1176 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|l", &number_arg, &base) == FAILURE) {
    1037           6 :                 return;
    1038             :         }
    1039             : 
    1040        1176 :         if (base && (base < 2 || base > GMP_MAX_BASE)) {
    1041           1 :                 php_error_docref(NULL, E_WARNING, "Bad base for conversion: " ZEND_LONG_FMT " (should be between 2 and %d)", base, GMP_MAX_BASE);
    1042           1 :                 RETURN_FALSE;
    1043             :         }
    1044             : 
    1045        1175 :         INIT_GMP_RETVAL(gmpnumber);
    1046        1175 :         if (convert_to_gmp(gmpnumber, number_arg, base) == FAILURE) {
    1047           5 :                 zval_ptr_dtor(return_value);
    1048           5 :                 RETURN_FALSE;
    1049             :         }
    1050             : }
    1051             : /* }}} */
    1052             : 
    1053          51 : int gmp_import_export_validate(zend_long size, zend_long options, int *order, int *endian)
    1054             : {
    1055          51 :         if (size < 1) {
    1056           4 :                 php_error_docref(NULL, E_WARNING,
    1057             :                         "Word size must be positive, " ZEND_LONG_FMT " given", size);
    1058           4 :                 return FAILURE;
    1059             :         }
    1060             : 
    1061          47 :         switch (options & (GMP_LSW_FIRST | GMP_MSW_FIRST)) {
    1062          18 :                 case GMP_LSW_FIRST:
    1063          18 :                         *order = -1;
    1064          18 :                         break;
    1065          27 :                 case GMP_MSW_FIRST:
    1066             :                 case 0: /* default */
    1067          27 :                         *order = 1;
    1068          27 :                         break;
    1069           2 :                 default:
    1070           2 :                         php_error_docref(NULL, E_WARNING,
    1071             :                                 "Invalid options: Conflicting word orders");
    1072           2 :                         return FAILURE;
    1073             :         }
    1074             : 
    1075          45 :         switch (options & (GMP_LITTLE_ENDIAN | GMP_BIG_ENDIAN | GMP_NATIVE_ENDIAN)) {
    1076          18 :                 case GMP_LITTLE_ENDIAN:
    1077          18 :                         *endian = -1;
    1078          18 :                         break;
    1079          22 :                 case GMP_BIG_ENDIAN:
    1080          22 :                         *endian = 1;
    1081          22 :                         break;
    1082           3 :                 case GMP_NATIVE_ENDIAN:
    1083             :                 case 0: /* default */
    1084           3 :                         *endian = 0;
    1085           3 :                         break;
    1086           2 :                 default:
    1087           2 :                         php_error_docref(NULL, E_WARNING,
    1088             :                                 "Invalid options: Conflicting word endianness");
    1089           2 :                         return FAILURE;
    1090             :         }
    1091             : 
    1092          43 :         return SUCCESS;
    1093             : }
    1094             : 
    1095             : /* {{{ proto GMP gmp_import(string data [, int word_size = 1, int options = GMP_MSW_FIRST | GMP_NATIVE_ENDIAN])
    1096             :    Imports a GMP number from a binary string */
    1097          26 : ZEND_FUNCTION(gmp_import)
    1098             : {
    1099             :         char *data;
    1100             :         size_t data_len;
    1101          26 :         zend_long size = 1;
    1102          26 :         zend_long options = GMP_MSW_FIRST | GMP_NATIVE_ENDIAN;
    1103             :         int order, endian;
    1104             :         mpz_ptr gmpnumber;
    1105             : 
    1106          26 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|ll", &data, &data_len, &size, &options) == FAILURE) {
    1107           7 :                 return;
    1108             :         }
    1109             : 
    1110          26 :         if (gmp_import_export_validate(size, options, &order, &endian) == FAILURE) {
    1111           4 :                 RETURN_FALSE;
    1112             :         }
    1113             : 
    1114          22 :         if ((data_len % size) != 0) {
    1115           3 :                 php_error_docref(NULL, E_WARNING,
    1116             :                         "Input length must be a multiple of word size");
    1117           3 :                 RETURN_FALSE;
    1118             :         }
    1119             : 
    1120          19 :         INIT_GMP_RETVAL(gmpnumber);
    1121             : 
    1122          19 :         mpz_import(gmpnumber, data_len / size, order, size, endian, 0, data);
    1123             : }
    1124             : /* }}} */
    1125             : 
    1126             : /* {{{ proto string gmp_export(GMP gmpnumber [, int word_size = 1, int options = GMP_MSW_FIRST | GMP_NATIVE_ENDIAN])
    1127             :    Exports a GMP number to a binary string */
    1128          25 : ZEND_FUNCTION(gmp_export)
    1129             : {
    1130             :         zval *gmpnumber_arg;
    1131          25 :         zend_long size = 1;
    1132          25 :         zend_long options = GMP_MSW_FIRST | GMP_NATIVE_ENDIAN;
    1133             :         int order, endian;
    1134             :         mpz_ptr gmpnumber;
    1135             :         gmp_temp_t temp_a;
    1136             : 
    1137          25 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|ll", &gmpnumber_arg, &size, &options) == FAILURE) {
    1138          25 :                 return;
    1139             :         }
    1140             : 
    1141          25 :         if (gmp_import_export_validate(size, options, &order, &endian) == FAILURE) {
    1142           4 :                 RETURN_FALSE;
    1143             :         }
    1144             : 
    1145          42 :         FETCH_GMP_ZVAL(gmpnumber, gmpnumber_arg, temp_a);
    1146             : 
    1147          21 :         if (mpz_sgn(gmpnumber) == 0) {
    1148           3 :                 RETURN_EMPTY_STRING();
    1149             :         } else {
    1150          18 :                 size_t bits_per_word = size * 8;
    1151          18 :                 size_t count = (mpz_sizeinbase(gmpnumber, 2) + bits_per_word - 1) / bits_per_word;
    1152             : 
    1153          36 :                 zend_string *out_string = zend_string_safe_alloc(count, size, 0, 0);
    1154          18 :                 mpz_export(ZSTR_VAL(out_string), NULL, order, size, endian, 0, gmpnumber);
    1155          18 :                 ZSTR_VAL(out_string)[ZSTR_LEN(out_string)] = '\0';
    1156             : 
    1157          18 :                 RETURN_NEW_STR(out_string);
    1158             :         }
    1159             : 
    1160             :         FREE_GMP_TEMP(temp_a);
    1161             : }
    1162             : /* }}} */
    1163             : 
    1164             : /* {{{ proto int gmp_intval(mixed gmpnumber)
    1165             :    Gets signed long value of GMP number */
    1166          18 : ZEND_FUNCTION(gmp_intval)
    1167             : {
    1168             :         zval *gmpnumber_arg;
    1169             : 
    1170          18 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &gmpnumber_arg) == FAILURE){
    1171           0 :                 return;
    1172             :         }
    1173             : 
    1174          36 :         if (IS_GMP(gmpnumber_arg)) {
    1175           7 :                 RETVAL_LONG(mpz_get_si(GET_GMP_FROM_ZVAL(gmpnumber_arg)));
    1176             :         } else {
    1177          22 :                 RETVAL_LONG(zval_get_long(gmpnumber_arg));
    1178             :         }
    1179             : }
    1180             : /* }}} */
    1181             : 
    1182             : /* {{{ proto string gmp_strval(mixed gmpnumber [, int base])
    1183             :    Gets string representation of GMP number  */
    1184         349 : ZEND_FUNCTION(gmp_strval)
    1185             : {
    1186             :         zval *gmpnumber_arg;
    1187         349 :         zend_long base = 10;
    1188             :         mpz_ptr gmpnum;
    1189             :         gmp_temp_t temp_a;
    1190             : 
    1191         349 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|l", &gmpnumber_arg, &base) == FAILURE) {
    1192          13 :                 return;
    1193             :         }
    1194             : 
    1195             :         /* Although the maximum base in general in GMP is 62, mpz_get_str()
    1196             :          * is explicitly limited to -36 when dealing with negative bases. */
    1197         349 :         if ((base < 2 && base > -2) || base > GMP_MAX_BASE || base < -36) {
    1198           9 :                 php_error_docref(NULL, E_WARNING, "Bad base for conversion: " ZEND_LONG_FMT " (should be between 2 and %d or -2 and -36)", base, GMP_MAX_BASE);
    1199           9 :                 RETURN_FALSE;
    1200             :         }
    1201             : 
    1202         680 :         FETCH_GMP_ZVAL(gmpnum, gmpnumber_arg, temp_a);
    1203             : 
    1204         336 :         gmp_strval(return_value, gmpnum, (int)base);
    1205             : 
    1206         336 :         FREE_GMP_TEMP(temp_a);
    1207             : }
    1208             : /* }}} */
    1209             : 
    1210             : /* {{{ proto GMP gmp_add(mixed a, mixed b)
    1211             :    Add a and b */
    1212          26 : ZEND_FUNCTION(gmp_add)
    1213             : {
    1214          26 :         gmp_binary_ui_op(mpz_add, mpz_add_ui);
    1215          26 : }
    1216             : /* }}} */
    1217             : 
    1218             : /* {{{ proto GMP gmp_sub(mixed a, mixed b)
    1219             :    Subtract b from a */
    1220           6 : ZEND_FUNCTION(gmp_sub)
    1221             : {
    1222           6 :         gmp_binary_ui_op(mpz_sub, mpz_sub_ui);
    1223           6 : }
    1224             : /* }}} */
    1225             : 
    1226             : /* {{{ proto GMP gmp_mul(mixed a, mixed b)
    1227             :    Multiply a and b */
    1228        1021 : ZEND_FUNCTION(gmp_mul)
    1229             : {
    1230        1021 :         gmp_binary_ui_op(mpz_mul, mpz_mul_ui);
    1231        1021 : }
    1232             : /* }}} */
    1233             : 
    1234             : /* {{{ proto array gmp_div_qr(mixed a, mixed b [, int round])
    1235             :    Divide a by b, returns quotient and reminder */
    1236          15 : ZEND_FUNCTION(gmp_div_qr)
    1237             : {
    1238             :         zval *a_arg, *b_arg;
    1239          15 :         zend_long round = GMP_ROUND_ZERO;
    1240             : 
    1241          15 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz|l", &a_arg, &b_arg, &round) == FAILURE) {
    1242           1 :                 return;
    1243             :         }
    1244             : 
    1245          15 :         switch (round) {
    1246          10 :         case GMP_ROUND_ZERO:
    1247          10 :                 gmp_zval_binary_ui_op2(return_value, a_arg, b_arg, mpz_tdiv_qr, (gmp_binary_ui_op2_t) mpz_tdiv_qr_ui, 1);
    1248          10 :                 break;
    1249           2 :         case GMP_ROUND_PLUSINF:
    1250           2 :                 gmp_zval_binary_ui_op2(return_value, a_arg, b_arg, mpz_cdiv_qr, (gmp_binary_ui_op2_t) mpz_cdiv_qr_ui, 1);
    1251           2 :                 break;
    1252           2 :         case GMP_ROUND_MINUSINF:
    1253           2 :                 gmp_zval_binary_ui_op2(return_value, a_arg, b_arg, mpz_fdiv_qr, (gmp_binary_ui_op2_t) mpz_fdiv_qr_ui, 1);
    1254           2 :                 break;
    1255           1 :         default:
    1256           1 :                 php_error_docref(NULL, E_WARNING, "Invalid rounding mode");
    1257           1 :                 RETURN_FALSE;
    1258             :         }
    1259             : }
    1260             : /* }}} */
    1261             : 
    1262             : /* {{{ proto GMP gmp_div_r(mixed a, mixed b [, int round])
    1263             :    Divide a by b, returns reminder only */
    1264          12 : ZEND_FUNCTION(gmp_div_r)
    1265             : {
    1266             :         zval *a_arg, *b_arg;
    1267          12 :         zend_long round = GMP_ROUND_ZERO;
    1268             : 
    1269          12 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz|l", &a_arg, &b_arg, &round) == FAILURE) {
    1270           1 :                 return;
    1271             :         }
    1272             : 
    1273          12 :         switch (round) {
    1274           7 :         case GMP_ROUND_ZERO:
    1275           7 :                 gmp_zval_binary_ui_op(return_value, a_arg, b_arg, mpz_tdiv_r, (gmp_binary_ui_op_t) mpz_tdiv_r_ui, 1);
    1276           7 :                 break;
    1277           2 :         case GMP_ROUND_PLUSINF:
    1278           2 :                 gmp_zval_binary_ui_op(return_value, a_arg, b_arg, mpz_cdiv_r, (gmp_binary_ui_op_t) mpz_cdiv_r_ui, 1);
    1279           2 :                 break;
    1280           2 :         case GMP_ROUND_MINUSINF:
    1281           2 :                 gmp_zval_binary_ui_op(return_value, a_arg, b_arg, mpz_fdiv_r, (gmp_binary_ui_op_t) mpz_fdiv_r_ui, 1);
    1282           2 :                 break;
    1283           1 :         default:
    1284           1 :                 php_error_docref(NULL, E_WARNING, "Invalid rounding mode");
    1285           1 :                 RETURN_FALSE;
    1286             :         }
    1287             : }
    1288             : /* }}} */
    1289             : 
    1290             : /* {{{ proto GMP gmp_div_q(mixed a, mixed b [, int round])
    1291             :    Divide a by b, returns quotient only */
    1292          13 : ZEND_FUNCTION(gmp_div_q)
    1293             : {
    1294             :         zval *a_arg, *b_arg;
    1295          13 :         zend_long round = GMP_ROUND_ZERO;
    1296             : 
    1297          13 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz|l", &a_arg, &b_arg, &round) == FAILURE) {
    1298           1 :                 return;
    1299             :         }
    1300             : 
    1301          13 :         switch (round) {
    1302           8 :         case GMP_ROUND_ZERO:
    1303           8 :                 gmp_zval_binary_ui_op(return_value, a_arg, b_arg, mpz_tdiv_q, (gmp_binary_ui_op_t) mpz_tdiv_q_ui, 1);
    1304           8 :                 break;
    1305           2 :         case GMP_ROUND_PLUSINF:
    1306           2 :                 gmp_zval_binary_ui_op(return_value, a_arg, b_arg, mpz_cdiv_q, (gmp_binary_ui_op_t) mpz_cdiv_q_ui, 1);
    1307           2 :                 break;
    1308           2 :         case GMP_ROUND_MINUSINF:
    1309           2 :                 gmp_zval_binary_ui_op(return_value, a_arg, b_arg, mpz_fdiv_q, (gmp_binary_ui_op_t) mpz_fdiv_q_ui, 1);
    1310           2 :                 break;
    1311           1 :         default:
    1312           1 :                 php_error_docref(NULL, E_WARNING, "Invalid rounding mode");
    1313           1 :                 RETURN_FALSE;
    1314             :         }
    1315             : 
    1316             : }
    1317             : /* }}} */
    1318             : 
    1319             : /* {{{ proto GMP gmp_mod(mixed a, mixed b)
    1320             :    Computes a modulo b */
    1321          24 : ZEND_FUNCTION(gmp_mod)
    1322             : {
    1323          24 :         gmp_binary_ui_op_no_zero(mpz_mod, (gmp_binary_ui_op_t) mpz_mod_ui);
    1324          24 : }
    1325             : /* }}} */
    1326             : 
    1327             : /* {{{ proto GMP gmp_divexact(mixed a, mixed b)
    1328             :    Divide a by b using exact division algorithm */
    1329           7 : ZEND_FUNCTION(gmp_divexact)
    1330             : {
    1331           7 :         gmp_binary_ui_op_no_zero(mpz_divexact, NULL);
    1332           7 : }
    1333             : /* }}} */
    1334             : 
    1335             : /* {{{ proto GMP gmp_neg(mixed a)
    1336             :    Negates a number */
    1337           9 : ZEND_FUNCTION(gmp_neg)
    1338             : {
    1339           9 :         gmp_unary_op(mpz_neg);
    1340           9 : }
    1341             : /* }}} */
    1342             : 
    1343             : /* {{{ proto GMP gmp_abs(mixed a)
    1344             :    Calculates absolute value */
    1345          10 : ZEND_FUNCTION(gmp_abs)
    1346             : {
    1347          10 :         gmp_unary_op(mpz_abs);
    1348          10 : }
    1349             : /* }}} */
    1350             : 
    1351             : /* {{{ proto GMP gmp_fact(int a)
    1352             :    Calculates factorial function */
    1353          14 : ZEND_FUNCTION(gmp_fact)
    1354             : {
    1355             :         zval *a_arg;
    1356             : 
    1357          14 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &a_arg) == FAILURE){
    1358           3 :                 return;
    1359             :         }
    1360             : 
    1361          29 :         if (IS_GMP(a_arg)) {
    1362           2 :                 mpz_ptr gmpnum_tmp = GET_GMP_FROM_ZVAL(a_arg);
    1363           2 :                 if (mpz_sgn(gmpnum_tmp) < 0) {
    1364           1 :                         php_error_docref(NULL, E_WARNING, "Number has to be greater than or equal to 0");
    1365           1 :                         RETURN_FALSE;
    1366             :                 }
    1367             :         } else {
    1368             :                 /* Use convert_to_number first to detect getting non-integer */
    1369          12 :                 convert_scalar_to_number(a_arg);
    1370          24 :                 if (Z_TYPE_P(a_arg) != IS_LONG) {
    1371           3 :                         convert_to_long(a_arg);
    1372           3 :                         if (Z_LVAL_P(a_arg) >= 0) {
    1373             :                                 /* Only warn if we'll make it past the non-negative check */
    1374           3 :                                 php_error_docref(NULL, E_WARNING, "Number has to be an integer");
    1375             :                         }
    1376             :                 }
    1377          12 :                 if (Z_LVAL_P(a_arg) < 0) {
    1378           2 :                         php_error_docref(NULL, E_WARNING, "Number has to be greater than or equal to 0");
    1379           2 :                         RETURN_FALSE;
    1380             :                 }
    1381             :         }
    1382             : 
    1383          11 :         gmp_zval_unary_ui_op(return_value, a_arg, mpz_fac_ui);
    1384             : }
    1385             : /* }}} */
    1386             : 
    1387             : /* {{{ proto GMP gmp_binomial(mixed n, int k)
    1388             :  * Calculates binomial coefficient */
    1389          11 : ZEND_FUNCTION(gmp_binomial)
    1390             : {
    1391             :         zval *n_arg;
    1392             :         zend_long k;
    1393             :         mpz_ptr gmpnum_result;
    1394             : 
    1395          11 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "zl", &n_arg, &k) == FAILURE) {
    1396           1 :                 return;
    1397             :         }
    1398             : 
    1399          11 :         if (k < 0) {
    1400           1 :                 php_error_docref(NULL, E_WARNING, "k cannot be negative");
    1401           1 :                 RETURN_FALSE;
    1402             :         }
    1403             : 
    1404          10 :         INIT_GMP_RETVAL(gmpnum_result);
    1405          20 :         if (Z_TYPE_P(n_arg) == IS_LONG && Z_LVAL_P(n_arg) >= 0) {
    1406           6 :                 mpz_bin_uiui(gmpnum_result, (gmp_ulong) Z_LVAL_P(n_arg), (gmp_ulong) k);
    1407             :         } else {
    1408             :                 mpz_ptr gmpnum_n;
    1409             :                 gmp_temp_t temp_n;
    1410           8 :                 FETCH_GMP_ZVAL(gmpnum_n, n_arg, temp_n);
    1411           4 :                 mpz_bin_ui(gmpnum_result, gmpnum_n, (gmp_ulong) k);
    1412           4 :                 FREE_GMP_TEMP(temp_n);
    1413             :         }
    1414             : }
    1415             : /* }}} */
    1416             : 
    1417             : /* {{{ proto GMP gmp_pow(mixed base, int exp)
    1418             :    Raise base to power exp */
    1419          14 : ZEND_FUNCTION(gmp_pow)
    1420             : {
    1421             :         zval *base_arg;
    1422             :         mpz_ptr gmpnum_result;
    1423             :         gmp_temp_t temp_base;
    1424             :         zend_long exp;
    1425             : 
    1426          14 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "zl", &base_arg, &exp) == FAILURE) {
    1427           5 :                 return;
    1428             :         }
    1429             : 
    1430          13 :         if (exp < 0) {
    1431           2 :                 php_error_docref(NULL, E_WARNING, "Negative exponent not supported");
    1432           2 :                 RETURN_FALSE;
    1433             :         }
    1434             : 
    1435          22 :         if (Z_TYPE_P(base_arg) == IS_LONG && Z_LVAL_P(base_arg) >= 0) {
    1436           3 :                 INIT_GMP_RETVAL(gmpnum_result);
    1437           3 :                 mpz_ui_pow_ui(gmpnum_result, Z_LVAL_P(base_arg), exp);
    1438             :         } else {
    1439             :                 mpz_ptr gmpnum_base;
    1440          16 :                 FETCH_GMP_ZVAL(gmpnum_base, base_arg, temp_base);
    1441           7 :                 INIT_GMP_RETVAL(gmpnum_result);
    1442           7 :                 mpz_pow_ui(gmpnum_result, gmpnum_base, exp);
    1443           7 :                 FREE_GMP_TEMP(temp_base);
    1444             :         }
    1445             : }
    1446             : /* }}} */
    1447             : 
    1448             : /* {{{ proto GMP gmp_powm(mixed base, mixed exp, mixed mod)
    1449             :    Raise base to power exp and take result modulo mod */
    1450          17 : ZEND_FUNCTION(gmp_powm)
    1451             : {
    1452             :         zval *base_arg, *exp_arg, *mod_arg;
    1453             :         mpz_ptr gmpnum_base, gmpnum_exp, gmpnum_mod, gmpnum_result;
    1454          17 :         int use_ui = 0;
    1455             :         gmp_temp_t temp_base, temp_exp, temp_mod;
    1456             : 
    1457          17 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "zzz", &base_arg, &exp_arg, &mod_arg) == FAILURE){
    1458           7 :                 return;
    1459             :         }
    1460             : 
    1461          34 :         FETCH_GMP_ZVAL(gmpnum_base, base_arg, temp_base);
    1462             : 
    1463          30 :         if (Z_TYPE_P(exp_arg) == IS_LONG && Z_LVAL_P(exp_arg) >= 0) {
    1464           8 :                 use_ui = 1;
    1465           8 :                 temp_exp.is_used = 0;
    1466             :         } else {
    1467          14 :                 FETCH_GMP_ZVAL_DEP(gmpnum_exp, exp_arg, temp_exp, temp_base);
    1468           6 :                 if (mpz_sgn(gmpnum_exp) < 0) {
    1469           1 :                         php_error_docref(NULL, E_WARNING, "Second parameter cannot be less than 0");
    1470           1 :                         FREE_GMP_TEMP(temp_base);
    1471           1 :                         FREE_GMP_TEMP(temp_exp);
    1472           1 :                         RETURN_FALSE;
    1473             :                 }
    1474             :         }
    1475          26 :         FETCH_GMP_ZVAL_DEP_DEP(gmpnum_mod, mod_arg, temp_mod, temp_exp, temp_base);
    1476             : 
    1477          12 :         if (!mpz_cmp_ui(gmpnum_mod, 0)) {
    1478           2 :                 php_error_docref(NULL, E_WARNING, "Modulus may not be zero");
    1479           2 :                 FREE_GMP_TEMP(temp_base);
    1480           2 :                 FREE_GMP_TEMP(temp_exp);
    1481           2 :                 FREE_GMP_TEMP(temp_mod);
    1482           2 :                 RETURN_FALSE;
    1483             :         }
    1484             : 
    1485          10 :         INIT_GMP_RETVAL(gmpnum_result);
    1486          10 :         if (use_ui) {
    1487           7 :                 mpz_powm_ui(gmpnum_result, gmpnum_base, (zend_ulong) Z_LVAL_P(exp_arg), gmpnum_mod);
    1488             :         } else {
    1489           3 :                 mpz_powm(gmpnum_result, gmpnum_base, gmpnum_exp, gmpnum_mod);
    1490           3 :                 FREE_GMP_TEMP(temp_exp);
    1491             :         }
    1492             : 
    1493          10 :         FREE_GMP_TEMP(temp_base);
    1494          10 :         FREE_GMP_TEMP(temp_mod);
    1495             : }
    1496             : /* }}} */
    1497             : 
    1498             : /* {{{ proto GMP gmp_sqrt(mixed a)
    1499             :    Takes integer part of square root of a */
    1500           9 : ZEND_FUNCTION(gmp_sqrt)
    1501             : {
    1502             :         zval *a_arg;
    1503             :         mpz_ptr gmpnum_a, gmpnum_result;
    1504             :         gmp_temp_t temp_a;
    1505             : 
    1506           9 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &a_arg) == FAILURE){
    1507           4 :                 return;
    1508             :         }
    1509             : 
    1510          18 :         FETCH_GMP_ZVAL(gmpnum_a, a_arg, temp_a);
    1511             : 
    1512           8 :         if (mpz_sgn(gmpnum_a) < 0) {
    1513           3 :                 php_error_docref(NULL, E_WARNING, "Number has to be greater than or equal to 0");
    1514           3 :                 FREE_GMP_TEMP(temp_a);
    1515           3 :                 RETURN_FALSE;
    1516             :         }
    1517             : 
    1518           5 :         INIT_GMP_RETVAL(gmpnum_result);
    1519           5 :         mpz_sqrt(gmpnum_result, gmpnum_a);
    1520           5 :         FREE_GMP_TEMP(temp_a);
    1521             : }
    1522             : /* }}} */
    1523             : 
    1524             : /* {{{ proto array gmp_sqrtrem(mixed a)
    1525             :    Square root with remainder */
    1526          12 : ZEND_FUNCTION(gmp_sqrtrem)
    1527             : {
    1528             :         zval *a_arg;
    1529             :         mpz_ptr gmpnum_a, gmpnum_result1, gmpnum_result2;
    1530             :         gmp_temp_t temp_a;
    1531             :         zval result1, result2;
    1532             : 
    1533          12 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &a_arg) == FAILURE){
    1534           3 :                 return;
    1535             :         }
    1536             : 
    1537          24 :         FETCH_GMP_ZVAL(gmpnum_a, a_arg, temp_a);
    1538             : 
    1539          11 :         if (mpz_sgn(gmpnum_a) < 0) {
    1540           2 :                 php_error_docref(NULL, E_WARNING, "Number has to be greater than or equal to 0");
    1541           2 :                 FREE_GMP_TEMP(temp_a);
    1542           2 :                 RETURN_FALSE;
    1543             :         }
    1544             : 
    1545           9 :         gmp_create(&result1, &gmpnum_result1);
    1546           9 :         gmp_create(&result2, &gmpnum_result2);
    1547             : 
    1548           9 :         array_init(return_value);
    1549           9 :         add_next_index_zval(return_value, &result1);
    1550           9 :         add_next_index_zval(return_value, &result2);
    1551             : 
    1552           9 :         mpz_sqrtrem(gmpnum_result1, gmpnum_result2, gmpnum_a);
    1553           9 :         FREE_GMP_TEMP(temp_a);
    1554             : }
    1555             : /* }}} */
    1556             : 
    1557             : /* {{{ proto GMP gmp_root(mixed a, int nth)
    1558             :    Takes integer part of nth root */
    1559           9 : ZEND_FUNCTION(gmp_root)
    1560             : {
    1561             :         zval *a_arg;
    1562             :         zend_long nth;
    1563             :         mpz_ptr gmpnum_a, gmpnum_result;
    1564             :         gmp_temp_t temp_a;
    1565             : 
    1566           9 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "zl", &a_arg, &nth) == FAILURE) {
    1567           3 :                 return;
    1568             :         }
    1569             : 
    1570           9 :         if (nth <= 0) {
    1571           2 :                 php_error_docref(NULL, E_WARNING, "The root must be positive");
    1572           2 :                 RETURN_FALSE;
    1573             :         }
    1574             : 
    1575          14 :         FETCH_GMP_ZVAL(gmpnum_a, a_arg, temp_a);
    1576             : 
    1577           7 :         if (nth % 2 == 0 && mpz_sgn(gmpnum_a) < 0) {
    1578           1 :                 php_error_docref(NULL, E_WARNING, "Can't take even root of negative number");
    1579           1 :                 FREE_GMP_TEMP(temp_a);
    1580           1 :                 RETURN_FALSE;
    1581             :         }
    1582             : 
    1583           6 :         INIT_GMP_RETVAL(gmpnum_result);
    1584           6 :         mpz_root(gmpnum_result, gmpnum_a, (gmp_ulong) nth);
    1585           6 :         FREE_GMP_TEMP(temp_a);
    1586             : }
    1587             : /* }}} */
    1588             : 
    1589             : /* {{{ proto GMP gmp_rootrem(mixed a, int nth)
    1590             :    Calculates integer part of nth root and remainder */
    1591           9 : ZEND_FUNCTION(gmp_rootrem)
    1592             : {
    1593             :         zval *a_arg;
    1594             :         zend_long nth;
    1595             :         mpz_ptr gmpnum_a, gmpnum_result1, gmpnum_result2;
    1596             :         gmp_temp_t temp_a;
    1597             :         zval result1, result2;
    1598             : 
    1599           9 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "zl", &a_arg, &nth) == FAILURE) {
    1600           3 :                 return;
    1601             :         }
    1602             : 
    1603           9 :         if (nth <= 0) {
    1604           2 :                 php_error_docref(NULL, E_WARNING, "The root must be positive");
    1605           2 :                 RETURN_FALSE;
    1606             :         }
    1607             : 
    1608          14 :         FETCH_GMP_ZVAL(gmpnum_a, a_arg, temp_a);
    1609             : 
    1610           7 :         if (nth % 2 == 0 && mpz_sgn(gmpnum_a) < 0) {
    1611           1 :                 php_error_docref(NULL, E_WARNING, "Can't take even root of negative number");
    1612           1 :                 FREE_GMP_TEMP(temp_a);
    1613           1 :                 RETURN_FALSE;
    1614             :         }
    1615             : 
    1616           6 :         gmp_create(&result1, &gmpnum_result1);
    1617           6 :         gmp_create(&result2, &gmpnum_result2);
    1618             : 
    1619           6 :         array_init(return_value);
    1620           6 :         add_next_index_zval(return_value, &result1);
    1621           6 :         add_next_index_zval(return_value, &result2);
    1622             : 
    1623             : #if GMP_51_OR_NEWER
    1624             :         /* mpz_rootrem() is supported since GMP 4.2, but buggy wrt odd roots
    1625             :          * of negative numbers */
    1626             :         mpz_rootrem(gmpnum_result1, gmpnum_result2, gmpnum_a, (gmp_ulong) nth);
    1627             : #else
    1628           6 :         mpz_root(gmpnum_result1, gmpnum_a, (gmp_ulong) nth);
    1629           6 :         mpz_pow_ui(gmpnum_result2, gmpnum_result1, (gmp_ulong) nth);
    1630           6 :         mpz_sub(gmpnum_result2, gmpnum_a, gmpnum_result2);
    1631             : #endif
    1632             : 
    1633           6 :         FREE_GMP_TEMP(temp_a);
    1634             : }
    1635             : /* }}} */
    1636             : 
    1637             : /* {{{ proto bool gmp_perfect_square(mixed a)
    1638             :    Checks if a is an exact square */
    1639          12 : ZEND_FUNCTION(gmp_perfect_square)
    1640             : {
    1641             :         zval *a_arg;
    1642             :         mpz_ptr gmpnum_a;
    1643             :         gmp_temp_t temp_a;
    1644             : 
    1645          12 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &a_arg) == FAILURE){
    1646           1 :                 return;
    1647             :         }
    1648             : 
    1649          24 :         FETCH_GMP_ZVAL(gmpnum_a, a_arg, temp_a);
    1650             : 
    1651          11 :         RETVAL_BOOL((mpz_perfect_square_p(gmpnum_a) != 0));
    1652          11 :         FREE_GMP_TEMP(temp_a);
    1653             : }
    1654             : /* }}} */
    1655             : 
    1656             : /* {{{ proto bool gmp_perfect_power(mixed a)
    1657             :    Checks if a is a perfect power */
    1658          13 : ZEND_FUNCTION(gmp_perfect_power)
    1659             : {
    1660             :         zval *a_arg;
    1661             :         mpz_ptr gmpnum_a;
    1662             :         gmp_temp_t temp_a;
    1663             : 
    1664          13 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &a_arg) == FAILURE){
    1665           0 :                 return;
    1666             :         }
    1667             : 
    1668          26 :         FETCH_GMP_ZVAL(gmpnum_a, a_arg, temp_a);
    1669             : 
    1670          13 :         RETVAL_BOOL((mpz_perfect_power_p(gmpnum_a) != 0));
    1671          13 :         FREE_GMP_TEMP(temp_a);
    1672             : }
    1673             : /* }}} */
    1674             : 
    1675             : /* {{{ proto int gmp_prob_prime(mixed a[, int reps])
    1676             :    Checks if a is "probably prime" */
    1677          39 : ZEND_FUNCTION(gmp_prob_prime)
    1678             : {
    1679             :         zval *gmpnumber_arg;
    1680             :         mpz_ptr gmpnum_a;
    1681          39 :         zend_long reps = 10;
    1682             :         gmp_temp_t temp_a;
    1683             : 
    1684          39 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|l", &gmpnumber_arg, &reps) == FAILURE) {
    1685           1 :                 return;
    1686             :         }
    1687             : 
    1688          78 :         FETCH_GMP_ZVAL(gmpnum_a, gmpnumber_arg, temp_a);
    1689             : 
    1690          38 :         RETVAL_LONG(mpz_probab_prime_p(gmpnum_a, (int)reps));
    1691          38 :         FREE_GMP_TEMP(temp_a);
    1692             : }
    1693             : /* }}} */
    1694             : 
    1695             : /* {{{ proto GMP gmp_gcd(mixed a, mixed b)
    1696             :    Computes greatest common denominator (gcd) of a and b */
    1697          11 : ZEND_FUNCTION(gmp_gcd)
    1698             : {
    1699          11 :         gmp_binary_ui_op(mpz_gcd, (gmp_binary_ui_op_t) mpz_gcd_ui);
    1700          11 : }
    1701             : /* }}} */
    1702             : 
    1703             : /* {{{ proto GMP gmp_lcm(mixed a, mixed b)
    1704             :    Computes least common multiple (lcm) of a and b */
    1705           7 : ZEND_FUNCTION(gmp_lcm)
    1706             : {
    1707           7 :         gmp_binary_ui_op(mpz_lcm, (gmp_binary_ui_op_t) mpz_lcm_ui);
    1708           7 : }
    1709             : /* }}} */
    1710             : 
    1711             : /* {{{ proto array gmp_gcdext(mixed a, mixed b)
    1712             :    Computes G, S, and T, such that AS + BT = G = `gcd' (A, B) */
    1713          12 : ZEND_FUNCTION(gmp_gcdext)
    1714             : {
    1715             :         zval *a_arg, *b_arg;
    1716             :         mpz_ptr gmpnum_a, gmpnum_b, gmpnum_t, gmpnum_s, gmpnum_g;
    1717             :         gmp_temp_t temp_a, temp_b;
    1718             :         zval result_g, result_s, result_t;
    1719             : 
    1720          12 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &a_arg, &b_arg) == FAILURE){
    1721           2 :                 return;
    1722             :         }
    1723             : 
    1724          24 :         FETCH_GMP_ZVAL(gmpnum_a, a_arg, temp_a);
    1725          22 :         FETCH_GMP_ZVAL_DEP(gmpnum_b, b_arg, temp_b, temp_a);
    1726             : 
    1727          10 :         gmp_create(&result_g, &gmpnum_g);
    1728          10 :         gmp_create(&result_s, &gmpnum_s);
    1729          10 :         gmp_create(&result_t, &gmpnum_t);
    1730             : 
    1731          10 :         array_init(return_value);
    1732          10 :         add_assoc_zval(return_value, "g", &result_g);
    1733          10 :         add_assoc_zval(return_value, "s", &result_s);
    1734          10 :         add_assoc_zval(return_value, "t", &result_t);
    1735             : 
    1736          10 :         mpz_gcdext(gmpnum_g, gmpnum_s, gmpnum_t, gmpnum_a, gmpnum_b);
    1737          10 :         FREE_GMP_TEMP(temp_a);
    1738          10 :         FREE_GMP_TEMP(temp_b);
    1739             : }
    1740             : /* }}} */
    1741             : 
    1742             : /* {{{ proto GMP gmp_invert(mixed a, mixed b)
    1743             :    Computes the inverse of a modulo b */
    1744          12 : ZEND_FUNCTION(gmp_invert)
    1745             : {
    1746             :         zval *a_arg, *b_arg;
    1747             :         mpz_ptr gmpnum_a, gmpnum_b, gmpnum_result;
    1748             :         gmp_temp_t temp_a, temp_b;
    1749             :         int res;
    1750             : 
    1751          12 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &a_arg, &b_arg) == FAILURE){
    1752           8 :                 return;
    1753             :         }
    1754             : 
    1755          24 :         FETCH_GMP_ZVAL(gmpnum_a, a_arg, temp_a);
    1756          20 :         FETCH_GMP_ZVAL_DEP(gmpnum_b, b_arg, temp_b, temp_a);
    1757             : 
    1758           9 :         INIT_GMP_RETVAL(gmpnum_result);
    1759           9 :         res = mpz_invert(gmpnum_result, gmpnum_a, gmpnum_b);
    1760           9 :         FREE_GMP_TEMP(temp_a);
    1761           9 :         FREE_GMP_TEMP(temp_b);
    1762           9 :         if (!res) {
    1763           5 :                 zval_ptr_dtor(return_value);
    1764           5 :                 RETURN_FALSE;
    1765             :         }
    1766             : }
    1767             : /* }}} */
    1768             : 
    1769             : /* {{{ proto int gmp_jacobi(mixed a, mixed b)
    1770             :    Computes Jacobi symbol */
    1771          20 : ZEND_FUNCTION(gmp_jacobi)
    1772             : {
    1773          20 :         gmp_binary_opl(mpz_jacobi);
    1774          20 : }
    1775             : /* }}} */
    1776             : 
    1777             : /* {{{ proto int gmp_legendre(mixed a, mixed b)
    1778             :    Computes Legendre symbol */
    1779          20 : ZEND_FUNCTION(gmp_legendre)
    1780             : {
    1781          20 :         gmp_binary_opl(mpz_legendre);
    1782          20 : }
    1783             : /* }}} */
    1784             : 
    1785             : /* {{{ proto int gmp_kronecker(mixed a, mixed b)
    1786             :    Computes the Kronecker symbol */
    1787          10 : ZEND_FUNCTION(gmp_kronecker)
    1788             : {
    1789             :         zval *a_arg, *b_arg;
    1790             :         mpz_ptr gmpnum_a, gmpnum_b;
    1791             :         gmp_temp_t temp_a, temp_b;
    1792          10 :         zend_bool use_a_si = 0, use_b_si = 0;
    1793             :         int result;
    1794             : 
    1795          10 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &a_arg, &b_arg) == FAILURE){
    1796           0 :                 return;
    1797             :         }
    1798             : 
    1799          26 :         if (Z_TYPE_P(a_arg) == IS_LONG && Z_TYPE_P(b_arg) != IS_LONG) {
    1800           2 :                 use_a_si = 1;
    1801           2 :                 temp_a.is_used = 0;
    1802             :         } else {
    1803          16 :                 FETCH_GMP_ZVAL(gmpnum_a, a_arg, temp_a);
    1804             :         }
    1805             : 
    1806          20 :         if (Z_TYPE_P(b_arg) == IS_LONG) {
    1807           6 :                 use_b_si = 1;
    1808           6 :                 temp_b.is_used = 0;
    1809             :         } else {
    1810           8 :                 FETCH_GMP_ZVAL_DEP(gmpnum_b, b_arg, temp_b, temp_a);
    1811             :         }
    1812             : 
    1813          10 :         if (use_a_si) {
    1814           2 :                 ZEND_ASSERT(use_b_si == 0);
    1815           2 :                 result = mpz_si_kronecker((gmp_long) Z_LVAL_P(a_arg), gmpnum_b);
    1816           8 :         } else if (use_b_si) {
    1817           6 :                 result = mpz_kronecker_si(gmpnum_a, (gmp_long) Z_LVAL_P(b_arg));
    1818             :         } else {
    1819           2 :                 result = mpz_kronecker(gmpnum_a, gmpnum_b);
    1820             :         }
    1821             : 
    1822          10 :         FREE_GMP_TEMP(temp_a);
    1823          10 :         FREE_GMP_TEMP(temp_b);
    1824             : 
    1825          10 :         RETURN_LONG(result);
    1826             : }
    1827             : /* }}} */
    1828             : 
    1829             : /* {{{ proto int gmp_cmp(mixed a, mixed b)
    1830             :    Compares two numbers */
    1831           9 : ZEND_FUNCTION(gmp_cmp)
    1832             : {
    1833             :         zval *a_arg, *b_arg;
    1834             : 
    1835           9 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &a_arg, &b_arg) == FAILURE){
    1836           0 :                 return;
    1837             :         }
    1838             : 
    1839           9 :         gmp_cmp(return_value, a_arg, b_arg);
    1840             : }
    1841             : /* }}} */
    1842             : 
    1843             : /* {{{ proto int gmp_sign(mixed a)
    1844             :    Gets the sign of the number */
    1845           8 : ZEND_FUNCTION(gmp_sign)
    1846             : {
    1847             :         /* Can't use gmp_unary_opl here, because mpz_sgn is a macro */
    1848             :         zval *a_arg;
    1849             :         mpz_ptr gmpnum_a;
    1850             :         gmp_temp_t temp_a;
    1851             : 
    1852           8 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &a_arg) == FAILURE){
    1853           2 :                 return;
    1854             :         }
    1855             : 
    1856          16 :         FETCH_GMP_ZVAL(gmpnum_a, a_arg, temp_a);
    1857             : 
    1858           6 :         RETVAL_LONG(mpz_sgn(gmpnum_a));
    1859           6 :         FREE_GMP_TEMP(temp_a);
    1860             : }
    1861             : /* }}} */
    1862             : 
    1863       25074 : static void gmp_init_random(void)
    1864             : {
    1865       25074 :         if (!GMPG(rand_initialized)) {
    1866             :                 /* Initialize */
    1867           4 :                 gmp_randinit_mt(GMPG(rand_state));
    1868             :                 /* Seed */
    1869           4 :                 gmp_randseed_ui(GMPG(rand_state), GENERATE_SEED());
    1870             : 
    1871           4 :                 GMPG(rand_initialized) = 1;
    1872             :         }
    1873       25074 : }
    1874             : 
    1875             : /* {{{ proto GMP gmp_random_seed(mixed seed)
    1876             :    Seed the RNG */
    1877           9 : ZEND_FUNCTION(gmp_random_seed)
    1878             : {
    1879             :         zval *seed;
    1880             :         gmp_temp_t temp_a;
    1881             : 
    1882           9 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &seed) == FAILURE) {
    1883           1 :                 return;
    1884             :         }
    1885             : 
    1886           9 :         gmp_init_random();
    1887             : 
    1888          18 :         if (Z_TYPE_P(seed) == IS_LONG && Z_LVAL_P(seed) >= 0) {
    1889           3 :                 gmp_randseed_ui(GMPG(rand_state), Z_LVAL_P(seed));
    1890             :         }
    1891             :         else {
    1892             :                 mpz_ptr gmpnum_seed;
    1893             : 
    1894          12 :                 FETCH_GMP_ZVAL(gmpnum_seed, seed, temp_a);
    1895             : 
    1896           5 :                 gmp_randseed(GMPG(rand_state), gmpnum_seed);
    1897             : 
    1898           5 :                 FREE_GMP_TEMP(temp_a);
    1899             :         }
    1900             : }
    1901             : /* }}} */
    1902             : 
    1903             : /* {{{ proto GMP gmp_random_bits(int bits)
    1904             :    Gets a random number in the range 0 to (2 ** n) - 1 */
    1905       10028 : ZEND_FUNCTION(gmp_random_bits)
    1906             : {
    1907             :         zend_long bits;
    1908             :         mpz_ptr gmpnum_result;
    1909             : 
    1910       10028 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &bits) == FAILURE) {
    1911           2 :                 return;
    1912             :         }
    1913             : 
    1914       10028 :         if (bits <= 0) {
    1915           2 :                 php_error_docref(NULL, E_WARNING, "The number of bits must be positive");
    1916           2 :                 RETURN_FALSE;
    1917             :         }
    1918             : 
    1919       10026 :         INIT_GMP_RETVAL(gmpnum_result);
    1920       10026 :         gmp_init_random();
    1921             : 
    1922       10026 :         mpz_urandomb(gmpnum_result, GMPG(rand_state), bits);
    1923             : }
    1924             : /* }}} */
    1925             : 
    1926             : /* {{{ proto GMP gmp_random_range(mixed min, mixed max)
    1927             :    Gets a random number in the range min to max */
    1928       15039 : ZEND_FUNCTION(gmp_random_range)
    1929             : {
    1930             :         zval *min_arg, *max_arg;
    1931             :         mpz_ptr gmpnum_max, gmpnum_result;
    1932             :         mpz_t gmpnum_range;
    1933             :         gmp_temp_t temp_a, temp_b;
    1934             : 
    1935       15039 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &min_arg, &max_arg) == FAILURE) {
    1936           3 :                 return;
    1937             :         }
    1938             : 
    1939       15039 :         gmp_init_random();
    1940             : 
    1941       30078 :         FETCH_GMP_ZVAL(gmpnum_max, max_arg, temp_a);
    1942             : 
    1943       30078 :         if (Z_TYPE_P(min_arg) == IS_LONG && Z_LVAL_P(min_arg) >= 0) {
    1944        5014 :                 if (mpz_cmp_ui(gmpnum_max, Z_LVAL_P(min_arg)) <= 0) {
    1945           1 :                         FREE_GMP_TEMP(temp_a);
    1946           1 :                         php_error_docref(NULL, E_WARNING, "The minimum value must be less than the maximum value");
    1947           1 :                         RETURN_FALSE;
    1948             :                 }
    1949             : 
    1950        5013 :                 INIT_GMP_RETVAL(gmpnum_result);
    1951        5013 :                 mpz_init(gmpnum_range);
    1952             : 
    1953        5013 :                 if (Z_LVAL_P(min_arg) != 0) {
    1954           3 :                         mpz_sub_ui(gmpnum_range, gmpnum_max, Z_LVAL_P(min_arg) - 1);
    1955             :                 } else {
    1956        5010 :                         mpz_add_ui(gmpnum_range, gmpnum_max, 1);
    1957             :                 }
    1958             : 
    1959        5013 :                 mpz_urandomm(gmpnum_result, GMPG(rand_state), gmpnum_range);
    1960             : 
    1961        5013 :                 if (Z_LVAL_P(min_arg) != 0) {
    1962           3 :                         mpz_add_ui(gmpnum_result, gmpnum_result, Z_LVAL_P(min_arg));
    1963             :                 }
    1964             : 
    1965        5013 :                 mpz_clear(gmpnum_range);
    1966        5013 :                 FREE_GMP_TEMP(temp_a);
    1967             :         } else {
    1968             :                 mpz_ptr gmpnum_min;
    1969             : 
    1970       20050 :                 FETCH_GMP_ZVAL_DEP(gmpnum_min, min_arg, temp_b, temp_a);
    1971             : 
    1972       10025 :                 if (mpz_cmp(gmpnum_max, gmpnum_min) <= 0) {
    1973           2 :                         FREE_GMP_TEMP(temp_b);
    1974           2 :                         FREE_GMP_TEMP(temp_a);
    1975           2 :                         php_error_docref(NULL, E_WARNING, "The minimum value must be less than the maximum value");
    1976           2 :                         RETURN_FALSE;
    1977             :                 }
    1978             : 
    1979       10023 :                 INIT_GMP_RETVAL(gmpnum_result);
    1980       10023 :                 mpz_init(gmpnum_range);
    1981             : 
    1982       10023 :                 mpz_sub(gmpnum_range, gmpnum_max, gmpnum_min);
    1983       10023 :                 mpz_add_ui(gmpnum_range, gmpnum_range, 1);
    1984       10023 :                 mpz_urandomm(gmpnum_result, GMPG(rand_state), gmpnum_range);
    1985       10023 :                 mpz_add(gmpnum_result, gmpnum_result, gmpnum_min);
    1986             : 
    1987       10023 :                 mpz_clear(gmpnum_range);
    1988       10023 :                 FREE_GMP_TEMP(temp_b);
    1989       10023 :                 FREE_GMP_TEMP(temp_a);
    1990             :         }
    1991             : }
    1992             : /* }}} */
    1993             : 
    1994             : /* {{{ proto GMP gmp_and(mixed a, mixed b)
    1995             :    Calculates logical AND of a and b */
    1996          11 : ZEND_FUNCTION(gmp_and)
    1997             : {
    1998          11 :         gmp_binary_op(mpz_and);
    1999          11 : }
    2000             : /* }}} */
    2001             : 
    2002             : /* {{{ proto GMP gmp_or(mixed a, mixed b)
    2003             :    Calculates logical OR of a and b */
    2004          11 : ZEND_FUNCTION(gmp_or)
    2005             : {
    2006          11 :         gmp_binary_op(mpz_ior);
    2007          11 : }
    2008             : /* }}} */
    2009             : 
    2010             : /* {{{ proto GMP gmp_com(mixed a)
    2011             :    Calculates one's complement of a */
    2012          10 : ZEND_FUNCTION(gmp_com)
    2013             : {
    2014          10 :         gmp_unary_op(mpz_com);
    2015          10 : }
    2016             : /* }}} */
    2017             : 
    2018             : /* {{{ proto GMP gmp_nextprime(mixed a)
    2019             :    Finds next prime of a */
    2020           8 : ZEND_FUNCTION(gmp_nextprime)
    2021             : {
    2022           8 :    gmp_unary_op(mpz_nextprime);
    2023           8 : }
    2024             : /* }}} */
    2025             : 
    2026             : /* {{{ proto GMP gmp_xor(mixed a, mixed b)
    2027             :    Calculates logical exclusive OR of a and b */
    2028          11 : ZEND_FUNCTION(gmp_xor)
    2029             : {
    2030          11 :         gmp_binary_op(mpz_xor);
    2031          11 : }
    2032             : /* }}} */
    2033             : 
    2034             : /* {{{ proto void gmp_setbit(GMP a, int index[, bool set_clear])
    2035             :    Sets or clear bit in a */
    2036          17 : ZEND_FUNCTION(gmp_setbit)
    2037             : {
    2038             :         zval *a_arg;
    2039             :         zend_long index;
    2040          17 :         zend_bool set = 1;
    2041             :         mpz_ptr gmpnum_a;
    2042             : 
    2043          17 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ol|b", &a_arg, gmp_ce, &index, &set) == FAILURE) {
    2044           6 :                 return;
    2045             :         }
    2046             : 
    2047          15 :         if (index < 0) {
    2048           1 :                 php_error_docref(NULL, E_WARNING, "Index must be greater than or equal to zero");
    2049           1 :                 RETURN_FALSE;
    2050             :         }
    2051          14 :     if (index / GMP_NUMB_BITS >= INT_MAX) {
    2052           1 :         php_error_docref(NULL, E_WARNING, "Index must be less than %d * %d", INT_MAX, GMP_NUMB_BITS);
    2053           1 :         RETURN_FALSE;
    2054             :     }
    2055             : 
    2056          13 :         gmpnum_a = GET_GMP_FROM_ZVAL(a_arg);
    2057             : 
    2058          13 :         if (set) {
    2059          11 :                 mpz_setbit(gmpnum_a, index);
    2060             :         } else {
    2061           2 :                 mpz_clrbit(gmpnum_a, index);
    2062             :         }
    2063             : }
    2064             : /* }}} */
    2065             : 
    2066             : /* {{{ proto void gmp_clrbit(GMP a, int index)
    2067             :    Clears bit in a */
    2068          10 : ZEND_FUNCTION(gmp_clrbit)
    2069             : {
    2070             :         zval *a_arg;
    2071             :         zend_long index;
    2072             :         mpz_ptr gmpnum_a;
    2073             : 
    2074          10 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ol", &a_arg, gmp_ce, &index) == FAILURE){
    2075           4 :                 return;
    2076             :         }
    2077             : 
    2078           9 :         if (index < 0) {
    2079           2 :                 php_error_docref(NULL, E_WARNING, "Index must be greater than or equal to zero");
    2080           2 :                 RETURN_FALSE;
    2081             :         }
    2082             : 
    2083           7 :         gmpnum_a = GET_GMP_FROM_ZVAL(a_arg);
    2084           7 :         mpz_clrbit(gmpnum_a, index);
    2085             : }
    2086             : /* }}} */
    2087             : 
    2088             : /* {{{ proto bool gmp_testbit(mixed a, int index)
    2089             :    Tests if bit is set in a */
    2090          13 : ZEND_FUNCTION(gmp_testbit)
    2091             : {
    2092             :         zval *a_arg;
    2093             :         zend_long index;
    2094             :         mpz_ptr gmpnum_a;
    2095             :         gmp_temp_t temp_a;
    2096             : 
    2097          13 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "zl", &a_arg, &index) == FAILURE){
    2098           3 :                 return;
    2099             :         }
    2100             : 
    2101          13 :         if (index < 0) {
    2102           2 :                 php_error_docref(NULL, E_WARNING, "Index must be greater than or equal to zero");
    2103           2 :                 RETURN_FALSE;
    2104             :         }
    2105             : 
    2106          22 :         FETCH_GMP_ZVAL(gmpnum_a, a_arg, temp_a);
    2107          10 :         RETVAL_BOOL(mpz_tstbit(gmpnum_a, index));
    2108          10 :         FREE_GMP_TEMP(temp_a);
    2109             : }
    2110             : /* }}} */
    2111             : 
    2112             : /* {{{ proto int gmp_popcount(mixed a)
    2113             :    Calculates the population count of a */
    2114           7 : ZEND_FUNCTION(gmp_popcount)
    2115             : {
    2116           7 :         gmp_unary_opl((gmp_unary_opl_t) mpz_popcount);
    2117           7 : }
    2118             : /* }}} */
    2119             : 
    2120             : /* {{{ proto int gmp_hamdist(mixed a, mixed b)
    2121             :    Calculates hamming distance between a and b */
    2122          10 : ZEND_FUNCTION(gmp_hamdist)
    2123             : {
    2124          10 :         gmp_binary_opl((gmp_binary_opl_t) mpz_hamdist);
    2125          10 : }
    2126             : /* }}} */
    2127             : 
    2128             : /* {{{ proto int gmp_scan0(mixed a, int start)
    2129             :    Finds first zero bit */
    2130           7 : ZEND_FUNCTION(gmp_scan0)
    2131             : {
    2132             :         zval *a_arg;
    2133             :         mpz_ptr gmpnum_a;
    2134             :         gmp_temp_t temp_a;
    2135             :         zend_long start;
    2136             : 
    2137           7 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "zl", &a_arg, &start) == FAILURE){
    2138           2 :                 return;
    2139             :         }
    2140             : 
    2141           7 :         if (start < 0) {
    2142           1 :                 php_error_docref(NULL, E_WARNING, "Starting index must be greater than or equal to zero");
    2143           1 :                 RETURN_FALSE;
    2144             :         }
    2145             : 
    2146          12 :         FETCH_GMP_ZVAL(gmpnum_a, a_arg, temp_a);
    2147             : 
    2148           5 :         RETVAL_LONG(mpz_scan0(gmpnum_a, start));
    2149           5 :         FREE_GMP_TEMP(temp_a);
    2150             : }
    2151             : /* }}} */
    2152             : 
    2153             : /* {{{ proto int gmp_scan1(mixed a, int start)
    2154             :    Finds first non-zero bit */
    2155           7 : ZEND_FUNCTION(gmp_scan1)
    2156             : {
    2157             :         zval *a_arg;
    2158             :         mpz_ptr gmpnum_a;
    2159             :         gmp_temp_t temp_a;
    2160             :         zend_long start;
    2161             : 
    2162           7 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "zl", &a_arg, &start) == FAILURE){
    2163           2 :                 return;
    2164             :         }
    2165             : 
    2166           7 :         if (start < 0) {
    2167           1 :                 php_error_docref(NULL, E_WARNING, "Starting index must be greater than or equal to zero");
    2168           1 :                 RETURN_FALSE;
    2169             :         }
    2170             : 
    2171          12 :         FETCH_GMP_ZVAL(gmpnum_a, a_arg, temp_a);
    2172             : 
    2173           5 :         RETVAL_LONG(mpz_scan1(gmpnum_a, start));
    2174           5 :         FREE_GMP_TEMP(temp_a);
    2175             : }
    2176             : /* }}} */

Generated by: LCOV version 1.10

Generated at Mon, 06 May 2019 17:58:14 +0000 (992 days ago)

Copyright © 2005-2022 The PHP Group
All rights reserved.