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: 716 716 100.0 %
Date: 2014-10-22 Functions: 79 79 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Wed, 22 Oct 2014 07:24:48 +0000 (3 days ago)

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