diff options
Diffstat (limited to 'ext/gmp')
-rw-r--r-- | ext/gmp/config.m4 | 15 | ||||
-rw-r--r-- | ext/gmp/config.w32 | 2 | ||||
-rw-r--r-- | ext/gmp/gmp.c | 624 | ||||
-rw-r--r-- | ext/gmp/php_gmp.h | 23 | ||||
-rw-r--r-- | ext/gmp/tests/bug50283.phpt | 1 | ||||
-rw-r--r-- | ext/gmp/tests/bug70284.phpt | 2 | ||||
-rw-r--r-- | ext/gmp/tests/gmp_clrbit.phpt | 5 | ||||
-rw-r--r-- | ext/gmp/tests/gmp_php_int_max.phpt | 29 | ||||
-rw-r--r-- | ext/gmp/tests/gmp_pow.phpt | 4 | ||||
-rw-r--r-- | ext/gmp/tests/gmp_random.phpt | 6 | ||||
-rw-r--r-- | ext/gmp/tests/gmp_random_seed-32bit.phpt | 230 | ||||
-rw-r--r-- | ext/gmp/tests/gmp_random_seed.phpt | 230 | ||||
-rw-r--r-- | ext/gmp/tests/gmp_setbit.phpt | 5 | ||||
-rw-r--r-- | ext/gmp/tests/gmp_strval.phpt | 8 |
14 files changed, 836 insertions, 348 deletions
diff --git a/ext/gmp/config.m4 b/ext/gmp/config.m4 index 2140aaf701..8265fb8dfa 100644 --- a/ext/gmp/config.m4 +++ b/ext/gmp/config.m4 @@ -1,7 +1,3 @@ -dnl -dnl $Id$ -dnl - PHP_ARG_WITH(gmp, for GNU MP support, [ --with-gmp[=DIR] Include GNU MP support]) @@ -15,14 +11,9 @@ if test "$PHP_GMP" != "no"; then AC_MSG_ERROR(Unable to locate gmp.h) fi - PHP_CHECK_LIBRARY(gmp, __gmp_randinit_lc_2exp_size, + PHP_CHECK_LIBRARY(gmp, __gmpz_rootrem, [],[ - PHP_CHECK_LIBRARY(gmp, gmp_randinit_lc_2exp_size, - [],[ - AC_MSG_ERROR([GNU MP Library version 4.1.2 or greater required.]) - ],[ - -L$GMP_DIR/$PHP_LIBDIR - ]) + AC_MSG_ERROR([GNU MP Library version 4.2 or greater required.]) ],[ -L$GMP_DIR/$PHP_LIBDIR ]) @@ -30,7 +21,7 @@ if test "$PHP_GMP" != "no"; then PHP_ADD_LIBRARY_WITH_PATH(gmp, $GMP_DIR/$PHP_LIBDIR, GMP_SHARED_LIBADD) PHP_ADD_INCLUDE($GMP_DIR/include) - PHP_NEW_EXTENSION(gmp, gmp.c, $ext_shared) + PHP_NEW_EXTENSION(gmp, gmp.c, $ext_shared,, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1) PHP_SUBST(GMP_SHARED_LIBADD) AC_DEFINE(HAVE_GMP, 1, [ ]) fi diff --git a/ext/gmp/config.w32 b/ext/gmp/config.w32 index 8c863f9bc8..7ea36150b2 100644 --- a/ext/gmp/config.w32 +++ b/ext/gmp/config.w32 @@ -6,7 +6,7 @@ ARG_WITH("gmp", "Include GNU MP support.", "no"); if (PHP_GMP != "no") { if (CHECK_LIB("mpir_a.lib", "gmp", PHP_GMP) && CHECK_HEADER_ADD_INCLUDE("gmp.h", "CFLAGS_GMP", PHP_GMP + ";" + PHP_PHP_BUILD + "\\include\\mpir")) { - EXTENSION("gmp", "gmp.c"); + EXTENSION("gmp", "gmp.c", null, "/DZEND_ENABLE_STATIC_TSRMLS_CACHE=1"); AC_DEFINE('HAVE_GMP', 1, 'GMP support'); AC_DEFINE('HAVE_MPIR', 1, 'MPIR support'); } else { diff --git a/ext/gmp/gmp.c b/ext/gmp/gmp.c index f563a63e4b..84b375aad8 100644 --- a/ext/gmp/gmp.c +++ b/ext/gmp/gmp.c @@ -1,6 +1,6 @@ /* +----------------------------------------------------------------------+ - | PHP Version 5 | + | PHP Version 7 | +----------------------------------------------------------------------+ | Copyright (c) 1997-2016 The PHP Group | +----------------------------------------------------------------------+ @@ -25,7 +25,7 @@ #include "php_gmp.h" #include "ext/standard/info.h" #include "ext/standard/php_var.h" -#include "ext/standard/php_smart_str_public.h" +#include "zend_smart_str_public.h" #include "zend_exceptions.h" #if HAVE_GMP @@ -104,6 +104,10 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_gmp_random, 0, 0, 0) ZEND_ARG_INFO(0, limiter) ZEND_END_ARG_INFO() +ZEND_BEGIN_ARG_INFO_EX(arginfo_gmp_random_seed, 0, 0, 1) + ZEND_ARG_INFO(0, seed) +ZEND_END_ARG_INFO() + ZEND_BEGIN_ARG_INFO_EX(arginfo_gmp_random_bits, 0, 0, 1) ZEND_ARG_INFO(0, bits) ZEND_END_ARG_INFO() @@ -170,6 +174,7 @@ const zend_function_entry gmp_functions[] = { ZEND_FE(gmp_cmp, arginfo_gmp_binary) ZEND_FE(gmp_sign, arginfo_gmp_unary) ZEND_FE(gmp_random, arginfo_gmp_random) + ZEND_FE(gmp_random_seed, arginfo_gmp_random_seed) ZEND_FE(gmp_random_bits, arginfo_gmp_random_bits) ZEND_FE(gmp_random_range, arginfo_gmp_random_range) ZEND_FE(gmp_and, arginfo_gmp_binary) @@ -199,7 +204,7 @@ zend_module_entry gmp_module_entry = { NULL, ZEND_MODULE_DEACTIVATE_N(gmp), ZEND_MODULE_INFO_N(gmp), - NO_VERSION_YET, + PHP_GMP_VERSION, ZEND_MODULE_GLOBALS(gmp), ZEND_GINIT(gmp), NULL, @@ -209,6 +214,9 @@ zend_module_entry gmp_module_entry = { /* }}} */ #ifdef COMPILE_DL_GMP +#ifdef ZTS +ZEND_TSRMLS_CACHE_DEFINE(); +#endif ZEND_GET_MODULE(gmp) #endif @@ -216,8 +224,8 @@ zend_class_entry *gmp_ce; static zend_object_handlers gmp_object_handlers; typedef struct _gmp_object { - zend_object std; mpz_t num; + zend_object std; } gmp_object; typedef struct _gmp_temp { @@ -235,25 +243,21 @@ typedef struct _gmp_temp { #define GMP_BIG_ENDIAN (1 << 3) #define GMP_NATIVE_ENDIAN (1 << 4) -#define GMP_42_OR_NEWER \ - ((__GNU_MP_VERSION >= 5) || (__GNU_MP_VERSION >= 4 && __GNU_MP_VERSION_MINOR >= 2)) +#define GMP_MAX_BASE 62 #define GMP_51_OR_NEWER \ ((__GNU_MP_VERSION >= 6) || (__GNU_MP_VERSION >= 5 && __GNU_MP_VERSION_MINOR >= 1)) -/* The maximum base for input and output conversions is 62 from GMP 4.2 - * onwards. */ -#if GMP_42_OR_NEWER -# define MAX_BASE 62 -#else -# define MAX_BASE 36 -#endif - #define IS_GMP(zval) \ - (Z_TYPE_P(zval) == IS_OBJECT && instanceof_function(Z_OBJCE_P(zval), gmp_ce TSRMLS_CC)) + (Z_TYPE_P(zval) == IS_OBJECT && instanceof_function(Z_OBJCE_P(zval), gmp_ce)) + +#define GET_GMP_OBJECT_FROM_OBJ(obj) \ + ((gmp_object *) ((char *) (obj) - XtOffsetOf(gmp_object, std))) +#define GET_GMP_OBJECT_FROM_ZVAL(zv) \ + GET_GMP_OBJECT_FROM_OBJ(Z_OBJ_P(zv)) #define GET_GMP_FROM_ZVAL(zval) \ - (((gmp_object *) zend_object_store_get_object((zval) TSRMLS_CC))->num) + GET_GMP_OBJECT_FROM_OBJ(Z_OBJ_P(zval))->num /* The FETCH_GMP_ZVAL_* family of macros is used to fetch a gmp number * (mpz_ptr) from a zval. If the zval is not a GMP instance, then we @@ -285,7 +289,7 @@ if (IS_GMP(zval)) { \ temp.is_used = 0; \ } else { \ mpz_init(temp.num); \ - if (convert_to_gmp(temp.num, zval, 0 TSRMLS_CC) == FAILURE) { \ + if (convert_to_gmp(temp.num, zval, 0) == FAILURE) { \ mpz_clear(temp.num); \ FREE_GMP_TEMP(dep1); \ FREE_GMP_TEMP(dep2); \ @@ -301,7 +305,7 @@ if (IS_GMP(zval)) { \ temp.is_used = 0; \ } else { \ mpz_init(temp.num); \ - if (convert_to_gmp(temp.num, zval, 0 TSRMLS_CC) == FAILURE) { \ + if (convert_to_gmp(temp.num, zval, 0) == FAILURE) { \ mpz_clear(temp.num); \ FREE_GMP_TEMP(dep); \ RETURN_FALSE; \ @@ -316,7 +320,7 @@ if (IS_GMP(zval)) { \ temp.is_used = 0; \ } else { \ mpz_init(temp.num); \ - if (convert_to_gmp(temp.num, zval, 0 TSRMLS_CC) == FAILURE) { \ + if (convert_to_gmp(temp.num, zval, 0) == FAILURE) { \ mpz_clear(temp.num); \ RETURN_FALSE; \ } \ @@ -325,11 +329,11 @@ if (IS_GMP(zval)) { \ } #define INIT_GMP_RETVAL(gmpnumber) \ - gmp_create_ex(return_value, &gmpnumber TSRMLS_CC) + gmp_create(return_value, &gmpnumber) -static void gmp_strval(zval *result, mpz_t gmpnum, long base); -static int convert_to_gmp(mpz_t gmpnumber, zval *val, int base TSRMLS_DC); -static void gmp_cmp(zval *return_value, zval *a_arg, zval *b_arg TSRMLS_DC); +static void gmp_strval(zval *result, mpz_t gmpnum, int base); +static int convert_to_gmp(mpz_t gmpnumber, zval *val, zend_long base); +static void gmp_cmp(zval *return_value, zval *a_arg, zval *b_arg); /* * The gmp_*_op functions provide an implementation for several common types @@ -340,19 +344,19 @@ static void gmp_cmp(zval *return_value, zval *a_arg, zval *b_arg TSRMLS_DC); typedef void (*gmp_unary_op_t)(mpz_ptr, mpz_srcptr); typedef int (*gmp_unary_opl_t)(mpz_srcptr); -typedef void (*gmp_unary_ui_op_t)(mpz_ptr, unsigned long); +typedef void (*gmp_unary_ui_op_t)(mpz_ptr, gmp_ulong); typedef void (*gmp_binary_op_t)(mpz_ptr, mpz_srcptr, mpz_srcptr); typedef int (*gmp_binary_opl_t)(mpz_srcptr, mpz_srcptr); -typedef void (*gmp_binary_ui_op_t)(mpz_ptr, mpz_srcptr, unsigned long); +typedef void (*gmp_binary_ui_op_t)(mpz_ptr, mpz_srcptr, gmp_ulong); typedef void (*gmp_binary_op2_t)(mpz_ptr, mpz_ptr, mpz_srcptr, mpz_srcptr); -typedef void (*gmp_binary_ui_op2_t)(mpz_ptr, mpz_ptr, mpz_srcptr, unsigned long); +typedef void (*gmp_binary_ui_op2_t)(mpz_ptr, mpz_ptr, mpz_srcptr, gmp_ulong); -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); -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); -static inline void gmp_zval_unary_op(zval *return_value, zval *a_arg, gmp_unary_op_t gmp_op TSRMLS_DC); -static inline void gmp_zval_unary_ui_op(zval *return_value, zval *a_arg, gmp_unary_ui_op_t gmp_op TSRMLS_DC); +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); +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); +static inline void gmp_zval_unary_op(zval *return_value, zval *a_arg, gmp_unary_op_t gmp_op); +static inline void gmp_zval_unary_ui_op(zval *return_value, zval *a_arg, gmp_unary_ui_op_t gmp_op); /* Binary operations */ #define gmp_binary_ui_op(op, uop) _gmp_binary_ui_op(INTERNAL_FUNCTION_PARAM_PASSTHRU, op, uop, 0) @@ -366,90 +370,57 @@ static inline void gmp_zval_unary_ui_op(zval *return_value, zval *a_arg, gmp_una #define gmp_unary_opl(op) _gmp_unary_opl(INTERNAL_FUNCTION_PARAM_PASSTHRU, op) #define gmp_unary_ui_op(op) _gmp_unary_ui_op(INTERNAL_FUNCTION_PARAM_PASSTHRU, op) -static inline long gmp_get_long(zval *zv) /* {{{ */ +static void gmp_free_object_storage(zend_object *obj) /* {{{ */ { - if (Z_TYPE_P(zv) == IS_LONG) { - return Z_LVAL_P(zv); - } else { - zval tmp_zv; - MAKE_COPY_ZVAL(&zv, &tmp_zv); - convert_to_long(&tmp_zv); - return Z_LVAL(tmp_zv); - } -} -/* }}} */ + gmp_object *intern = GET_GMP_OBJECT_FROM_OBJ(obj); -static void gmp_free_object_storage(gmp_object *intern TSRMLS_DC) /* {{{ */ -{ mpz_clear(intern->num); - - zend_object_std_dtor(&intern->std TSRMLS_CC); - efree(intern); + zend_object_std_dtor(&intern->std); } /* }}} */ -static inline zend_object_value gmp_create_object_ex(zend_class_entry *ce, mpz_ptr *gmpnum_target TSRMLS_DC) /* {{{ */ +static inline zend_object *gmp_create_object_ex(zend_class_entry *ce, mpz_ptr *gmpnum_target) /* {{{ */ { - zend_object_value retval; - gmp_object *intern = emalloc(sizeof(gmp_object)); + gmp_object *intern = emalloc(sizeof(gmp_object) + zend_object_properties_size(ce)); - zend_object_std_init(&intern->std, ce TSRMLS_CC); + zend_object_std_init(&intern->std, ce); object_properties_init(&intern->std, ce); mpz_init(intern->num); *gmpnum_target = intern->num; + intern->std.handlers = &gmp_object_handlers; - retval.handle = zend_objects_store_put( - intern, (zend_objects_store_dtor_t) zend_objects_destroy_object, - (zend_objects_free_object_storage_t) gmp_free_object_storage, - NULL TSRMLS_CC - ); - retval.handlers = &gmp_object_handlers; - - return retval; + return &intern->std; } /* }}} */ -static zend_object_value gmp_create_object(zend_class_entry *ce TSRMLS_DC) /* {{{ */ +static zend_object *gmp_create_object(zend_class_entry *ce) /* {{{ */ { mpz_ptr gmpnum_dummy; - return gmp_create_object_ex(ce, &gmpnum_dummy TSRMLS_CC); + return gmp_create_object_ex(ce, &gmpnum_dummy); } /* }}} */ -static inline void gmp_create_ex(zval *target, mpz_ptr *gmpnum_target TSRMLS_DC) /* {{{ */ +static inline void gmp_create(zval *target, mpz_ptr *gmpnum_target) /* {{{ */ { - Z_TYPE_P(target) = IS_OBJECT; - Z_OBJVAL_P(target) = gmp_create_object_ex(gmp_ce, gmpnum_target TSRMLS_CC); + ZVAL_OBJ(target, gmp_create_object_ex(gmp_ce, gmpnum_target)); } /* }}} */ -static zval *gmp_create(mpz_ptr *gmpnum_target TSRMLS_DC) /* {{{ */ -{ - zval *obj; - MAKE_STD_ZVAL(obj); - gmp_create_ex(obj, gmpnum_target TSRMLS_CC); - return obj; -} -/* }}} */ - -static int gmp_cast_object(zval *readobj, zval *writeobj, int type TSRMLS_DC) /* {{{ */ +static int gmp_cast_object(zval *readobj, zval *writeobj, int type) /* {{{ */ { mpz_ptr gmpnum; switch (type) { case IS_STRING: gmpnum = GET_GMP_FROM_ZVAL(readobj); - INIT_PZVAL(writeobj); gmp_strval(writeobj, gmpnum, 10); return SUCCESS; case IS_LONG: gmpnum = GET_GMP_FROM_ZVAL(readobj); - INIT_PZVAL(writeobj); ZVAL_LONG(writeobj, mpz_get_si(gmpnum)); return SUCCESS; case IS_DOUBLE: gmpnum = GET_GMP_FROM_ZVAL(readobj); - INIT_PZVAL(writeobj); ZVAL_DOUBLE(writeobj, mpz_get_d(gmpnum)); return SUCCESS; default: @@ -458,55 +429,40 @@ static int gmp_cast_object(zval *readobj, zval *writeobj, int type TSRMLS_DC) /* } /* }}} */ -static HashTable *gmp_get_debug_info(zval *obj, int *is_temp TSRMLS_DC) /* {{{ */ +static HashTable *gmp_get_debug_info(zval *obj, int *is_temp) /* {{{ */ { - HashTable *ht, *props = zend_std_get_properties(obj TSRMLS_CC); + HashTable *ht, *props = zend_std_get_properties(obj); mpz_ptr gmpnum = GET_GMP_FROM_ZVAL(obj); - zval *zv; + zval zv; *is_temp = 1; - ALLOC_HASHTABLE(ht); - ZEND_INIT_SYMTABLE_EX(ht, zend_hash_num_elements(props) + 1, 0); - zend_hash_copy(ht, props, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *)); + ht = zend_array_dup(props); - MAKE_STD_ZVAL(zv); - gmp_strval(zv, gmpnum, 10); - zend_hash_update(ht, "num", sizeof("num"), &zv, sizeof(zval *), NULL); + gmp_strval(&zv, gmpnum, 10); + zend_hash_str_update(ht, "num", sizeof("num")-1, &zv); return ht; } /* }}} */ -static zend_object_value gmp_clone_obj(zval *obj TSRMLS_DC) /* {{{ */ +static zend_object *gmp_clone_obj(zval *obj) /* {{{ */ { - gmp_object *old_object = zend_object_store_get_object(obj TSRMLS_CC); - zend_object_value new_object_val = gmp_create_object(Z_OBJCE_P(obj) TSRMLS_CC); - gmp_object *new_object = zend_object_store_get_object_by_handle( - new_object_val.handle TSRMLS_CC - ); + gmp_object *old_object = GET_GMP_OBJECT_FROM_ZVAL(obj); + gmp_object *new_object = GET_GMP_OBJECT_FROM_OBJ(gmp_create_object(Z_OBJCE_P(obj))); - zend_objects_clone_members( - &new_object->std, new_object_val, - &old_object->std, Z_OBJ_HANDLE_P(obj) TSRMLS_CC - ); + zend_objects_clone_members( &new_object->std, &old_object->std); mpz_set(new_object->num, old_object->num); - return new_object_val; + return &new_object->std; } /* }}} */ -static void shift_operator_helper(gmp_binary_ui_op_t op, zval *return_value, zval *op1, zval *op2 TSRMLS_DC) { - zval op2_copy; - if (Z_TYPE_P(op2) != IS_LONG) { - op2_copy = *op2; - zval_copy_ctor(&op2_copy); - convert_to_long(&op2_copy); - op2 = &op2_copy; - } +static void shift_operator_helper(gmp_binary_ui_op_t op, zval *return_value, zval *op1, zval *op2) { + zend_long shift = zval_get_long(op2); - if (Z_LVAL_P(op2) < 0) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Shift cannot be negative"); + if (shift < 0) { + php_error_docref(NULL, E_WARNING, "Shift cannot be negative"); RETVAL_FALSE; } else { mpz_ptr gmpnum_op, gmpnum_result; @@ -514,7 +470,7 @@ static void shift_operator_helper(gmp_binary_ui_op_t op, zval *return_value, zva FETCH_GMP_ZVAL(gmpnum_op, op1, temp); INIT_GMP_RETVAL(gmpnum_result); - op(gmpnum_result, gmpnum_op, (unsigned long) Z_LVAL_P(op2)); + op(gmpnum_result, gmpnum_op, (gmp_ulong) shift); FREE_GMP_TEMP(temp); } } @@ -522,7 +478,7 @@ static void shift_operator_helper(gmp_binary_ui_op_t op, zval *return_value, zva #define DO_BINARY_UI_OP_EX(op, uop, check_b_zero) \ gmp_zval_binary_ui_op( \ result, op1, op2, op, (gmp_binary_ui_op_t) uop, \ - check_b_zero TSRMLS_CC \ + check_b_zero \ ); \ return SUCCESS; @@ -530,10 +486,10 @@ static void shift_operator_helper(gmp_binary_ui_op_t op, zval *return_value, zva #define DO_BINARY_OP(op) DO_BINARY_UI_OP_EX(op, NULL, 0) #define DO_UNARY_OP(op) \ - gmp_zval_unary_op(result, op1, op TSRMLS_CC); \ + gmp_zval_unary_op(result, op1, op); \ return SUCCESS; -static int gmp_do_operation_ex(zend_uchar opcode, zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ */ +static int gmp_do_operation_ex(zend_uchar opcode, zval *result, zval *op1, zval *op2) /* {{{ */ { switch (opcode) { case ZEND_ADD: @@ -543,17 +499,17 @@ static int gmp_do_operation_ex(zend_uchar opcode, zval *result, zval *op1, zval case ZEND_MUL: DO_BINARY_UI_OP(mpz_mul); case ZEND_POW: - shift_operator_helper(mpz_pow_ui, result, op1, op2 TSRMLS_CC); + shift_operator_helper(mpz_pow_ui, result, op1, op2); return SUCCESS; case ZEND_DIV: DO_BINARY_UI_OP_EX(mpz_tdiv_q, mpz_tdiv_q_ui, 1); case ZEND_MOD: DO_BINARY_UI_OP_EX(mpz_mod, mpz_mod_ui, 1); case ZEND_SL: - shift_operator_helper(mpz_mul_2exp, result, op1, op2 TSRMLS_CC); + shift_operator_helper(mpz_mul_2exp, result, op1, op2); return SUCCESS; case ZEND_SR: - shift_operator_helper(mpz_fdiv_q_2exp, result, op1, op2 TSRMLS_CC); + shift_operator_helper(mpz_fdiv_q_2exp, result, op1, op2); return SUCCESS; case ZEND_BW_OR: DO_BINARY_OP(mpz_ior); @@ -570,7 +526,7 @@ static int gmp_do_operation_ex(zend_uchar opcode, zval *result, zval *op1, zval } /* }}} */ -static int gmp_do_operation(zend_uchar opcode, zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ */ +static int gmp_do_operation(zend_uchar opcode, zval *result, zval *op1, zval *op2) /* {{{ */ { zval op1_copy; int retval; @@ -580,7 +536,7 @@ static int gmp_do_operation(zend_uchar opcode, zval *result, zval *op1, zval *op op1 = &op1_copy; } - retval = gmp_do_operation_ex(opcode, result, op1, op2 TSRMLS_CC); + retval = gmp_do_operation_ex(opcode, result, op1, op2); if (retval == SUCCESS && op1 == &op1_copy) { zval_dtor(op1); @@ -590,84 +546,81 @@ static int gmp_do_operation(zend_uchar opcode, zval *result, zval *op1, zval *op } /* }}} */ -static int gmp_compare(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ */ +static int gmp_compare(zval *result, zval *op1, zval *op2) /* {{{ */ { - gmp_cmp(result, op1, op2 TSRMLS_CC); - if (Z_TYPE_P(result) == IS_BOOL) { + gmp_cmp(result, op1, op2); + if (Z_TYPE_P(result) == IS_FALSE) { ZVAL_LONG(result, 1); } return SUCCESS; } /* }}} */ -static int gmp_serialize(zval *object, unsigned char **buffer, zend_uint *buf_len, zend_serialize_data *data TSRMLS_DC) /* {{{ */ +static int gmp_serialize(zval *object, unsigned char **buffer, size_t *buf_len, zend_serialize_data *data) /* {{{ */ { mpz_ptr gmpnum = GET_GMP_FROM_ZVAL(object); smart_str buf = {0}; - zval zv, *zv_ptr = &zv; + zval zv; php_serialize_data_t serialize_data = (php_serialize_data_t) data; PHP_VAR_SERIALIZE_INIT(serialize_data); - INIT_PZVAL(zv_ptr); - gmp_strval(zv_ptr, gmpnum, 10); - php_var_serialize(&buf, &zv_ptr, &serialize_data TSRMLS_CC); - zval_dtor(zv_ptr); + gmp_strval(&zv, gmpnum, 10); + php_var_serialize(&buf, &zv, &serialize_data); + zval_dtor(&zv); - Z_ARRVAL_P(zv_ptr) = zend_std_get_properties(object TSRMLS_CC); - Z_TYPE_P(zv_ptr) = IS_ARRAY; - php_var_serialize(&buf, &zv_ptr, &serialize_data TSRMLS_CC); + ZVAL_ARR(&zv, zend_std_get_properties(object)); + php_var_serialize(&buf, &zv, &serialize_data); PHP_VAR_SERIALIZE_DESTROY(serialize_data); - *buffer = (unsigned char *) buf.c; - *buf_len = buf.len; + *buffer = (unsigned char *) estrndup(ZSTR_VAL(buf.s), ZSTR_LEN(buf.s)); + *buf_len = ZSTR_LEN(buf.s); + zend_string_release(buf.s); return SUCCESS; } /* }}} */ -static int gmp_unserialize(zval **object, zend_class_entry *ce, const unsigned char *buf, zend_uint buf_len, zend_unserialize_data *data TSRMLS_DC) /* {{{ */ +static int gmp_unserialize(zval *object, zend_class_entry *ce, const unsigned char *buf, size_t buf_len, zend_unserialize_data *data) /* {{{ */ { mpz_ptr gmpnum; const unsigned char *p, *max; - zval *zv_ptr; + zval *zv; int retval = FAILURE; php_unserialize_data_t unserialize_data = (php_unserialize_data_t) data; PHP_VAR_UNSERIALIZE_INIT(unserialize_data); - gmp_create_ex(*object, &gmpnum TSRMLS_CC); + gmp_create(object, &gmpnum); p = buf; max = buf + buf_len; - ALLOC_INIT_ZVAL(zv_ptr); - if (!php_var_unserialize(&zv_ptr, &p, max, &unserialize_data TSRMLS_CC) - || Z_TYPE_P(zv_ptr) != IS_STRING - || convert_to_gmp(gmpnum, zv_ptr, 10 TSRMLS_CC) == FAILURE + zv = var_tmp_var(&unserialize_data); + if (!php_var_unserialize(zv, &p, max, &unserialize_data) + || Z_TYPE_P(zv) != IS_STRING + || convert_to_gmp(gmpnum, zv, 10) == FAILURE ) { - zend_throw_exception(NULL, "Could not unserialize number", 0 TSRMLS_CC); + zend_throw_exception(NULL, "Could not unserialize number", 0); goto exit; } - var_push_dtor_no_addref(&unserialize_data, &zv_ptr); - ALLOC_INIT_ZVAL(zv_ptr); - if (!php_var_unserialize(&zv_ptr, &p, max, &unserialize_data TSRMLS_CC) - || Z_TYPE_P(zv_ptr) != IS_ARRAY + zv = var_tmp_var(&unserialize_data); + if (!php_var_unserialize(zv, &p, max, &unserialize_data) + || Z_TYPE_P(zv) != IS_ARRAY ) { - zend_throw_exception(NULL, "Could not unserialize properties", 0 TSRMLS_CC); + zend_throw_exception(NULL, "Could not unserialize properties", 0); goto exit; } - if (zend_hash_num_elements(Z_ARRVAL_P(zv_ptr)) != 0) { + if (zend_hash_num_elements(Z_ARRVAL_P(zv)) != 0) { zend_hash_copy( - zend_std_get_properties(*object TSRMLS_CC), Z_ARRVAL_P(zv_ptr), - (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *) + zend_std_get_properties(object), Z_ARRVAL_P(zv), + (copy_ctor_func_t) zval_add_ref ); } retval = SUCCESS; exit: - var_push_dtor_no_addref(&unserialize_data, &zv_ptr); PHP_VAR_UNSERIALIZE_DESTROY(unserialize_data); return retval; } @@ -677,6 +630,9 @@ exit: */ static ZEND_GINIT_FUNCTION(gmp) { +#if defined(COMPILE_DL_GMP) && defined(ZTS) + ZEND_TSRMLS_CACHE_UPDATE(); +#endif gmp_globals->rand_initialized = 0; } /* }}} */ @@ -687,12 +643,14 @@ ZEND_MINIT_FUNCTION(gmp) { zend_class_entry tmp_ce; INIT_CLASS_ENTRY(tmp_ce, "GMP", NULL); - gmp_ce = zend_register_internal_class(&tmp_ce TSRMLS_CC); + gmp_ce = zend_register_internal_class(&tmp_ce); gmp_ce->create_object = gmp_create_object; gmp_ce->serialize = gmp_serialize; gmp_ce->unserialize = gmp_unserialize; memcpy(&gmp_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); + gmp_object_handlers.offset = XtOffsetOf(gmp_object, std); + gmp_object_handlers.free_obj = gmp_free_object_storage; gmp_object_handlers.cast_object = gmp_cast_object; gmp_object_handlers.get_debug_info = gmp_get_debug_info; gmp_object_handlers.clone_obj = gmp_clone_obj; @@ -748,17 +706,18 @@ ZEND_MODULE_INFO_D(gmp) /* {{{ convert_to_gmp * Convert zval to be gmp number */ -static int convert_to_gmp(mpz_t gmpnumber, zval *val, int base TSRMLS_DC) +static int convert_to_gmp(mpz_t gmpnumber, zval *val, zend_long base) { switch (Z_TYPE_P(val)) { case IS_LONG: - case IS_BOOL: { - mpz_set_si(gmpnumber, gmp_get_long(val)); + case IS_FALSE: + case IS_TRUE: { + mpz_set_si(gmpnumber, zval_get_long(val)); return SUCCESS; } case IS_STRING: { char *numstr = Z_STRVAL_P(val); - int skip_lead = 0; + zend_bool skip_lead = 0; int ret; if (Z_STRLEN_P(val) > 2 && numstr[0] == '0') { @@ -771,9 +730,9 @@ static int convert_to_gmp(mpz_t gmpnumber, zval *val, int base TSRMLS_DC) } } - ret = mpz_set_str(gmpnumber, (skip_lead ? &numstr[2] : numstr), base); + ret = mpz_set_str(gmpnumber, (skip_lead ? &numstr[2] : numstr), (int) base); if (-1 == ret) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, + php_error_docref(NULL, E_WARNING, "Unable to convert variable to GMP - string is not an integer"); return FAILURE; } @@ -781,25 +740,25 @@ static int convert_to_gmp(mpz_t gmpnumber, zval *val, int base TSRMLS_DC) return SUCCESS; } default: - php_error_docref(NULL TSRMLS_CC, E_WARNING, + php_error_docref(NULL, E_WARNING, "Unable to convert variable to GMP - wrong type"); return FAILURE; } } /* }}} */ -static void gmp_strval(zval *result, mpz_t gmpnum, long base) /* {{{ */ +static void gmp_strval(zval *result, mpz_t gmpnum, int base) /* {{{ */ { - int num_len; - char *out_string; + size_t num_len; + zend_string *str; num_len = mpz_sizeinbase(gmpnum, abs(base)); if (mpz_sgn(gmpnum) < 0) { num_len++; } - out_string = emalloc(num_len + 1); - mpz_get_str(out_string, base, gmpnum); + str = zend_string_alloc(num_len, 0); + mpz_get_str(ZSTR_VAL(str), base, gmpnum); /* * From GMP documentation for mpz_sizeinbase(): @@ -809,22 +768,22 @@ static void gmp_strval(zval *result, mpz_t gmpnum, long base) /* {{{ */ * So let's check to see if we already have a \0 byte... */ - if (out_string[num_len - 1] == '\0') { - num_len--; + if (ZSTR_VAL(str)[ZSTR_LEN(str) - 1] == '\0') { + ZSTR_LEN(str)--; } else { - out_string[num_len] = '\0'; + ZSTR_VAL(str)[ZSTR_LEN(str)] = '\0'; } - ZVAL_STRINGL(result, out_string, num_len, 0); + ZVAL_NEW_STR(result, str); } /* }}} */ -static void gmp_cmp(zval *return_value, zval *a_arg, zval *b_arg TSRMLS_DC) /* {{{ */ +static void gmp_cmp(zval *return_value, zval *a_arg, zval *b_arg) /* {{{ */ { mpz_ptr gmpnum_a, gmpnum_b; gmp_temp_t temp_a, temp_b; zend_bool use_si = 0; - long res; + zend_long res; FETCH_GMP_ZVAL(gmpnum_a, a_arg, temp_a); @@ -851,7 +810,7 @@ static void gmp_cmp(zval *return_value, zval *a_arg, zval *b_arg TSRMLS_DC) /* { /* {{{ gmp_zval_binary_ui_op Execute GMP binary operation. */ -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) +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) { mpz_ptr gmpnum_a, gmpnum_b, gmpnum_result; int use_ui = 0; @@ -875,7 +834,7 @@ static inline void gmp_zval_binary_ui_op(zval *return_value, zval *a_arg, zval * } if (b_is_zero) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Zero operand not allowed"); + php_error_docref(NULL, E_WARNING, "Zero operand not allowed"); FREE_GMP_TEMP(temp_a); FREE_GMP_TEMP(temp_b); RETURN_FALSE; @@ -885,7 +844,7 @@ static inline void gmp_zval_binary_ui_op(zval *return_value, zval *a_arg, zval * INIT_GMP_RETVAL(gmpnum_result); if (use_ui) { - gmp_ui_op(gmpnum_result, gmpnum_a, (unsigned long) Z_LVAL_P(b_arg)); + gmp_ui_op(gmpnum_result, gmpnum_a, (gmp_ulong) Z_LVAL_P(b_arg)); } else { gmp_op(gmpnum_result, gmpnum_a, gmpnum_b); } @@ -898,11 +857,12 @@ static inline void gmp_zval_binary_ui_op(zval *return_value, zval *a_arg, zval * /* {{{ gmp_zval_binary_ui_op2 Execute GMP binary operation which returns 2 values. */ -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) +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) { mpz_ptr gmpnum_a, gmpnum_b, gmpnum_result1, gmpnum_result2; int use_ui = 0; gmp_temp_t temp_a, temp_b; + zval result1, result2; FETCH_GMP_ZVAL(gmpnum_a, a_arg, temp_a); @@ -923,19 +883,22 @@ static inline void gmp_zval_binary_ui_op2(zval *return_value, zval *a_arg, zval } if (b_is_zero) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Zero operand not allowed"); + php_error_docref(NULL, E_WARNING, "Zero operand not allowed"); FREE_GMP_TEMP(temp_a); FREE_GMP_TEMP(temp_b); RETURN_FALSE; } } + gmp_create(&result1, &gmpnum_result1); + gmp_create(&result2, &gmpnum_result2); + array_init(return_value); - add_index_zval(return_value, 0, gmp_create(&gmpnum_result1 TSRMLS_CC)); - add_index_zval(return_value, 1, gmp_create(&gmpnum_result2 TSRMLS_CC)); + add_next_index_zval(return_value, &result1); + add_next_index_zval(return_value, &result2); if (use_ui) { - gmp_ui_op(gmpnum_result1, gmpnum_result2, gmpnum_a, (unsigned long) Z_LVAL_P(b_arg)); + gmp_ui_op(gmpnum_result1, gmpnum_result2, gmpnum_a, (gmp_ulong) Z_LVAL_P(b_arg)); } else { gmp_op(gmpnum_result1, gmpnum_result2, gmpnum_a, gmpnum_b); } @@ -951,11 +914,11 @@ static inline void _gmp_binary_ui_op(INTERNAL_FUNCTION_PARAMETERS, gmp_binary_op { zval *a_arg, *b_arg; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz", &a_arg, &b_arg) == FAILURE){ + if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &a_arg, &b_arg) == FAILURE){ return; } - gmp_zval_binary_ui_op(return_value, a_arg, b_arg, gmp_op, gmp_ui_op, check_b_zero TSRMLS_CC); + gmp_zval_binary_ui_op(return_value, a_arg, b_arg, gmp_op, gmp_ui_op, check_b_zero); } /* }}} */ @@ -963,7 +926,7 @@ static inline void _gmp_binary_ui_op(INTERNAL_FUNCTION_PARAMETERS, gmp_binary_op /* {{{ gmp_zval_unary_op */ -static inline void gmp_zval_unary_op(zval *return_value, zval *a_arg, gmp_unary_op_t gmp_op TSRMLS_DC) +static inline void gmp_zval_unary_op(zval *return_value, zval *a_arg, gmp_unary_op_t gmp_op) { mpz_ptr gmpnum_a, gmpnum_result; gmp_temp_t temp_a; @@ -979,12 +942,12 @@ static inline void gmp_zval_unary_op(zval *return_value, zval *a_arg, gmp_unary_ /* {{{ gmp_zval_unary_ui_op */ -static inline void gmp_zval_unary_ui_op(zval *return_value, zval *a_arg, gmp_unary_ui_op_t gmp_op TSRMLS_DC) +static inline void gmp_zval_unary_ui_op(zval *return_value, zval *a_arg, gmp_unary_ui_op_t gmp_op) { mpz_ptr gmpnum_result; INIT_GMP_RETVAL(gmpnum_result); - gmp_op(gmpnum_result, gmp_get_long(a_arg)); + gmp_op(gmpnum_result, zval_get_long(a_arg)); } /* }}} */ @@ -995,11 +958,11 @@ static inline void _gmp_unary_ui_op(INTERNAL_FUNCTION_PARAMETERS, gmp_unary_ui_o { zval *a_arg; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &a_arg) == FAILURE){ + if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &a_arg) == FAILURE){ return; } - gmp_zval_unary_ui_op(return_value, a_arg, gmp_op TSRMLS_CC); + gmp_zval_unary_ui_op(return_value, a_arg, gmp_op); } /* }}} */ @@ -1009,11 +972,11 @@ static inline void _gmp_unary_op(INTERNAL_FUNCTION_PARAMETERS, gmp_unary_op_t gm { zval *a_arg; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &a_arg) == FAILURE){ + if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &a_arg) == FAILURE){ return; } - gmp_zval_unary_op(return_value, a_arg, gmp_op TSRMLS_CC); + gmp_zval_unary_op(return_value, a_arg, gmp_op); } /* }}} */ @@ -1025,7 +988,7 @@ static inline void _gmp_unary_opl(INTERNAL_FUNCTION_PARAMETERS, gmp_unary_opl_t mpz_ptr gmpnum_a; gmp_temp_t temp_a; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &a_arg) == FAILURE){ + if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &a_arg) == FAILURE){ return; } @@ -1043,7 +1006,7 @@ static inline void _gmp_binary_opl(INTERNAL_FUNCTION_PARAMETERS, gmp_binary_opl_ mpz_ptr gmpnum_a, gmpnum_b; gmp_temp_t temp_a, temp_b; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz", &a_arg, &b_arg) == FAILURE){ + if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &a_arg, &b_arg) == FAILURE){ return; } @@ -1063,30 +1026,30 @@ ZEND_FUNCTION(gmp_init) { zval *number_arg; mpz_ptr gmpnumber; - long base = 0; + zend_long base = 0; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|l", &number_arg, &base) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|l", &number_arg, &base) == FAILURE) { return; } - if (base && (base < 2 || base > MAX_BASE)) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Bad base for conversion: %ld (should be between 2 and %d)", base, MAX_BASE); + if (base && (base < 2 || base > GMP_MAX_BASE)) { + php_error_docref(NULL, E_WARNING, "Bad base for conversion: %pd (should be between 2 and %d)", base, GMP_MAX_BASE); RETURN_FALSE; } INIT_GMP_RETVAL(gmpnumber); - if (convert_to_gmp(gmpnumber, number_arg, base TSRMLS_CC) == FAILURE) { + if (convert_to_gmp(gmpnumber, number_arg, base) == FAILURE) { zval_dtor(return_value); RETURN_FALSE; } } /* }}} */ -int gmp_import_export_validate(long size, long options, int *order, int *endian TSRMLS_DC) +int gmp_import_export_validate(zend_long size, zend_long options, int *order, int *endian) { if (size < 1) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, - "Word size must be positive, %ld given", size); + php_error_docref(NULL, E_WARNING, + "Word size must be positive, %pd given", size); return FAILURE; } @@ -1099,7 +1062,7 @@ int gmp_import_export_validate(long size, long options, int *order, int *endian *order = 1; break; default: - php_error_docref(NULL TSRMLS_CC, E_WARNING, + php_error_docref(NULL, E_WARNING, "Invalid options: Conflicting word orders"); return FAILURE; } @@ -1116,7 +1079,7 @@ int gmp_import_export_validate(long size, long options, int *order, int *endian *endian = 0; break; default: - php_error_docref(NULL TSRMLS_CC, E_WARNING, + php_error_docref(NULL, E_WARNING, "Invalid options: Conflicting word endianness"); return FAILURE; } @@ -1129,22 +1092,22 @@ int gmp_import_export_validate(long size, long options, int *order, int *endian ZEND_FUNCTION(gmp_import) { char *data; - int data_len; - long size = 1; - long options = GMP_MSW_FIRST | GMP_NATIVE_ENDIAN; + size_t data_len; + zend_long size = 1; + zend_long options = GMP_MSW_FIRST | GMP_NATIVE_ENDIAN; int order, endian; mpz_ptr gmpnumber; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|ll", &data, &data_len, &size, &options) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|ll", &data, &data_len, &size, &options) == FAILURE) { return; } - if (gmp_import_export_validate(size, options, &order, &endian TSRMLS_CC) == FAILURE) { + if (gmp_import_export_validate(size, options, &order, &endian) == FAILURE) { RETURN_FALSE; } if ((data_len % size) != 0) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, + php_error_docref(NULL, E_WARNING, "Input length must be a multiple of word size"); RETURN_FALSE; } @@ -1160,17 +1123,17 @@ ZEND_FUNCTION(gmp_import) ZEND_FUNCTION(gmp_export) { zval *gmpnumber_arg; - long size = 1; - long options = GMP_MSW_FIRST | GMP_NATIVE_ENDIAN; + zend_long size = 1; + zend_long options = GMP_MSW_FIRST | GMP_NATIVE_ENDIAN; int order, endian; mpz_ptr gmpnumber; gmp_temp_t temp_a; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|ll", &gmpnumber_arg, &size, &options) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|ll", &gmpnumber_arg, &size, &options) == FAILURE) { return; } - if (gmp_import_export_validate(size, options, &order, &endian TSRMLS_CC) == FAILURE) { + if (gmp_import_export_validate(size, options, &order, &endian) == FAILURE) { RETURN_FALSE; } @@ -1183,11 +1146,11 @@ ZEND_FUNCTION(gmp_export) size_t count = (mpz_sizeinbase(gmpnumber, 2) + bits_per_word - 1) / bits_per_word; size_t out_len = count * size; - char *out_string = emalloc(out_len + 1); - mpz_export(out_string, NULL, order, size, endian, 0, gmpnumber); - out_string[out_len] = '\0'; + zend_string *out_string = zend_string_alloc(out_len, 0); + mpz_export(ZSTR_VAL(out_string), NULL, order, size, endian, 0, gmpnumber); + ZSTR_VAL(out_string)[out_len] = '\0'; - RETURN_STRINGL(out_string, out_len, 0); + RETURN_NEW_STR(out_string); } FREE_GMP_TEMP(temp_a); @@ -1200,14 +1163,14 @@ ZEND_FUNCTION(gmp_intval) { zval *gmpnumber_arg; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &gmpnumber_arg) == FAILURE){ + if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &gmpnumber_arg) == FAILURE){ return; } if (IS_GMP(gmpnumber_arg)) { RETVAL_LONG(mpz_get_si(GET_GMP_FROM_ZVAL(gmpnumber_arg))); } else { - RETVAL_LONG(gmp_get_long(gmpnumber_arg)); + RETVAL_LONG(zval_get_long(gmpnumber_arg)); } } /* }}} */ @@ -1217,29 +1180,24 @@ ZEND_FUNCTION(gmp_intval) ZEND_FUNCTION(gmp_strval) { zval *gmpnumber_arg; - long base = 10; + zend_long base = 10; mpz_ptr gmpnum; gmp_temp_t temp_a; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|l", &gmpnumber_arg, &base) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|l", &gmpnumber_arg, &base) == FAILURE) { return; } -#if MAX_BASE == 62 - /* Although the maximum base in general in GMP >= 4.2 is 62, mpz_get_str() + /* Although the maximum base in general in GMP is 62, mpz_get_str() * is explicitly limited to -36 when dealing with negative bases. */ - if ((base < 2 && base > -2) || base > MAX_BASE || base < -36) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Bad base for conversion: %ld (should be between 2 and %d or -2 and -36)", base, MAX_BASE); -#else - if (base < 2 || base > MAX_BASE) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Bad base for conversion: %ld (should be between 2 and %d)", base, MAX_BASE); -#endif + if ((base < 2 && base > -2) || base > GMP_MAX_BASE || base < -36) { + php_error_docref(NULL, E_WARNING, "Bad base for conversion: %pd (should be between 2 and %d or -2 and -36)", base, GMP_MAX_BASE); RETURN_FALSE; } FETCH_GMP_ZVAL(gmpnum, gmpnumber_arg, temp_a); - gmp_strval(return_value, gmpnum, base); + gmp_strval(return_value, gmpnum, (int)base); FREE_GMP_TEMP(temp_a); } @@ -1274,24 +1232,24 @@ ZEND_FUNCTION(gmp_mul) ZEND_FUNCTION(gmp_div_qr) { zval *a_arg, *b_arg; - long round = GMP_ROUND_ZERO; + zend_long round = GMP_ROUND_ZERO; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz|l", &a_arg, &b_arg, &round) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz|l", &a_arg, &b_arg, &round) == FAILURE) { return; } switch (round) { case GMP_ROUND_ZERO: - 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); + gmp_zval_binary_ui_op2(return_value, a_arg, b_arg, mpz_tdiv_qr, (gmp_binary_ui_op2_t) mpz_tdiv_qr_ui, 1); break; case GMP_ROUND_PLUSINF: - 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); + gmp_zval_binary_ui_op2(return_value, a_arg, b_arg, mpz_cdiv_qr, (gmp_binary_ui_op2_t) mpz_cdiv_qr_ui, 1); break; case GMP_ROUND_MINUSINF: - 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); + gmp_zval_binary_ui_op2(return_value, a_arg, b_arg, mpz_fdiv_qr, (gmp_binary_ui_op2_t) mpz_fdiv_qr_ui, 1); break; default: - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid rounding mode"); + php_error_docref(NULL, E_WARNING, "Invalid rounding mode"); RETURN_FALSE; } } @@ -1302,24 +1260,24 @@ ZEND_FUNCTION(gmp_div_qr) ZEND_FUNCTION(gmp_div_r) { zval *a_arg, *b_arg; - long round = GMP_ROUND_ZERO; + zend_long round = GMP_ROUND_ZERO; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz|l", &a_arg, &b_arg, &round) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz|l", &a_arg, &b_arg, &round) == FAILURE) { return; } switch (round) { case GMP_ROUND_ZERO: - 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); + gmp_zval_binary_ui_op(return_value, a_arg, b_arg, mpz_tdiv_r, (gmp_binary_ui_op_t) mpz_tdiv_r_ui, 1); break; case GMP_ROUND_PLUSINF: - 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); + gmp_zval_binary_ui_op(return_value, a_arg, b_arg, mpz_cdiv_r, (gmp_binary_ui_op_t) mpz_cdiv_r_ui, 1); break; case GMP_ROUND_MINUSINF: - 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); + gmp_zval_binary_ui_op(return_value, a_arg, b_arg, mpz_fdiv_r, (gmp_binary_ui_op_t) mpz_fdiv_r_ui, 1); break; default: - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid rounding mode"); + php_error_docref(NULL, E_WARNING, "Invalid rounding mode"); RETURN_FALSE; } } @@ -1330,24 +1288,24 @@ ZEND_FUNCTION(gmp_div_r) ZEND_FUNCTION(gmp_div_q) { zval *a_arg, *b_arg; - long round = GMP_ROUND_ZERO; + zend_long round = GMP_ROUND_ZERO; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz|l", &a_arg, &b_arg, &round) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz|l", &a_arg, &b_arg, &round) == FAILURE) { return; } switch (round) { case GMP_ROUND_ZERO: - 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); + gmp_zval_binary_ui_op(return_value, a_arg, b_arg, mpz_tdiv_q, (gmp_binary_ui_op_t) mpz_tdiv_q_ui, 1); break; case GMP_ROUND_PLUSINF: - 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); + gmp_zval_binary_ui_op(return_value, a_arg, b_arg, mpz_cdiv_q, (gmp_binary_ui_op_t) mpz_cdiv_q_ui, 1); break; case GMP_ROUND_MINUSINF: - 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); + gmp_zval_binary_ui_op(return_value, a_arg, b_arg, mpz_fdiv_q, (gmp_binary_ui_op_t) mpz_fdiv_q_ui, 1); break; default: - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid rounding mode"); + php_error_docref(NULL, E_WARNING, "Invalid rounding mode"); RETURN_FALSE; } @@ -1392,24 +1350,24 @@ ZEND_FUNCTION(gmp_fact) { zval *a_arg; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &a_arg) == FAILURE){ + if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &a_arg) == FAILURE){ return; } if (IS_GMP(a_arg)) { mpz_ptr gmpnum_tmp = GET_GMP_FROM_ZVAL(a_arg); if (mpz_sgn(gmpnum_tmp) < 0) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Number has to be greater than or equal to 0"); + php_error_docref(NULL, E_WARNING, "Number has to be greater than or equal to 0"); RETURN_FALSE; } } else { - if (gmp_get_long(a_arg) < 0) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Number has to be greater than or equal to 0"); + if (zval_get_long(a_arg) < 0) { + php_error_docref(NULL, E_WARNING, "Number has to be greater than or equal to 0"); RETURN_FALSE; } } - gmp_zval_unary_ui_op(return_value, a_arg, mpz_fac_ui TSRMLS_CC); + gmp_zval_unary_ui_op(return_value, a_arg, mpz_fac_ui); } /* }}} */ @@ -1420,14 +1378,14 @@ ZEND_FUNCTION(gmp_pow) zval *base_arg; mpz_ptr gmpnum_result, gmpnum_base; gmp_temp_t temp_base; - long exp; + zend_long exp; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zl", &base_arg, &exp) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "zl", &base_arg, &exp) == FAILURE) { return; } if (exp < 0) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Negative exponent not supported"); + php_error_docref(NULL, E_WARNING, "Negative exponent not supported"); RETURN_FALSE; } @@ -1452,7 +1410,7 @@ ZEND_FUNCTION(gmp_powm) int use_ui = 0; gmp_temp_t temp_base, temp_exp, temp_mod; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zzz", &base_arg, &exp_arg, &mod_arg) == FAILURE){ + if (zend_parse_parameters(ZEND_NUM_ARGS(), "zzz", &base_arg, &exp_arg, &mod_arg) == FAILURE){ return; } @@ -1464,7 +1422,7 @@ ZEND_FUNCTION(gmp_powm) } else { FETCH_GMP_ZVAL_DEP(gmpnum_exp, exp_arg, temp_exp, temp_base); if (mpz_sgn(gmpnum_exp) < 0) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Second parameter cannot be less than 0"); + php_error_docref(NULL, E_WARNING, "Second parameter cannot be less than 0"); FREE_GMP_TEMP(temp_base); FREE_GMP_TEMP(temp_exp); RETURN_FALSE; @@ -1473,7 +1431,7 @@ ZEND_FUNCTION(gmp_powm) FETCH_GMP_ZVAL_DEP_DEP(gmpnum_mod, mod_arg, temp_mod, temp_exp, temp_base); if (!mpz_cmp_ui(gmpnum_mod, 0)) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Modulus may not be zero"); + php_error_docref(NULL, E_WARNING, "Modulus may not be zero"); FREE_GMP_TEMP(temp_base); FREE_GMP_TEMP(temp_exp); FREE_GMP_TEMP(temp_mod); @@ -1482,7 +1440,7 @@ ZEND_FUNCTION(gmp_powm) INIT_GMP_RETVAL(gmpnum_result); if (use_ui) { - mpz_powm_ui(gmpnum_result, gmpnum_base, (unsigned long) Z_LVAL_P(exp_arg), gmpnum_mod); + mpz_powm_ui(gmpnum_result, gmpnum_base, (zend_ulong) Z_LVAL_P(exp_arg), gmpnum_mod); } else { mpz_powm(gmpnum_result, gmpnum_base, gmpnum_exp, gmpnum_mod); FREE_GMP_TEMP(temp_exp); @@ -1501,14 +1459,14 @@ ZEND_FUNCTION(gmp_sqrt) mpz_ptr gmpnum_a, gmpnum_result; gmp_temp_t temp_a; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &a_arg) == FAILURE){ + if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &a_arg) == FAILURE){ return; } FETCH_GMP_ZVAL(gmpnum_a, a_arg, temp_a); if (mpz_sgn(gmpnum_a) < 0) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Number has to be greater than or equal to 0"); + php_error_docref(NULL, E_WARNING, "Number has to be greater than or equal to 0"); FREE_GMP_TEMP(temp_a); RETURN_FALSE; } @@ -1526,22 +1484,26 @@ ZEND_FUNCTION(gmp_sqrtrem) zval *a_arg; mpz_ptr gmpnum_a, gmpnum_result1, gmpnum_result2; gmp_temp_t temp_a; + zval result1, result2; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &a_arg) == FAILURE){ + if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &a_arg) == FAILURE){ return; } FETCH_GMP_ZVAL(gmpnum_a, a_arg, temp_a); if (mpz_sgn(gmpnum_a) < 0) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Number has to be greater than or equal to 0"); + php_error_docref(NULL, E_WARNING, "Number has to be greater than or equal to 0"); FREE_GMP_TEMP(temp_a); RETURN_FALSE; } + gmp_create(&result1, &gmpnum_result1); + gmp_create(&result2, &gmpnum_result2); + array_init(return_value); - add_index_zval(return_value, 0, gmp_create(&gmpnum_result1 TSRMLS_CC)); - add_index_zval(return_value, 1, gmp_create(&gmpnum_result2 TSRMLS_CC)); + add_next_index_zval(return_value, &result1); + add_next_index_zval(return_value, &result2); mpz_sqrtrem(gmpnum_result1, gmpnum_result2, gmpnum_a); FREE_GMP_TEMP(temp_a); @@ -1553,29 +1515,29 @@ ZEND_FUNCTION(gmp_sqrtrem) ZEND_FUNCTION(gmp_root) { zval *a_arg; - long nth; + zend_long nth; mpz_ptr gmpnum_a, gmpnum_result; gmp_temp_t temp_a; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zl", &a_arg, &nth) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "zl", &a_arg, &nth) == FAILURE) { return; } if (nth <= 0) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "The root must be positive"); + php_error_docref(NULL, E_WARNING, "The root must be positive"); RETURN_FALSE; } FETCH_GMP_ZVAL(gmpnum_a, a_arg, temp_a); if (nth % 2 == 0 && mpz_sgn(gmpnum_a) < 0) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Can't take even root of negative number"); + php_error_docref(NULL, E_WARNING, "Can't take even root of negative number"); FREE_GMP_TEMP(temp_a); RETURN_FALSE; } INIT_GMP_RETVAL(gmpnum_result); - mpz_root(gmpnum_result, gmpnum_a, (unsigned long) nth); + mpz_root(gmpnum_result, gmpnum_a, (gmp_ulong) nth); FREE_GMP_TEMP(temp_a); } /* }}} */ @@ -1585,38 +1547,42 @@ ZEND_FUNCTION(gmp_root) ZEND_FUNCTION(gmp_rootrem) { zval *a_arg; - long nth; + zend_long nth; mpz_ptr gmpnum_a, gmpnum_result1, gmpnum_result2; gmp_temp_t temp_a; + zval result1, result2; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zl", &a_arg, &nth) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "zl", &a_arg, &nth) == FAILURE) { return; } if (nth <= 0) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "The root must be positive"); + php_error_docref(NULL, E_WARNING, "The root must be positive"); RETURN_FALSE; } FETCH_GMP_ZVAL(gmpnum_a, a_arg, temp_a); if (nth % 2 == 0 && mpz_sgn(gmpnum_a) < 0) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Can't take even root of negative number"); + php_error_docref(NULL, E_WARNING, "Can't take even root of negative number"); FREE_GMP_TEMP(temp_a); RETURN_FALSE; } + gmp_create(&result1, &gmpnum_result1); + gmp_create(&result2, &gmpnum_result2); + array_init(return_value); - add_index_zval(return_value, 0, gmp_create(&gmpnum_result1 TSRMLS_CC)); - add_index_zval(return_value, 1, gmp_create(&gmpnum_result2 TSRMLS_CC)); + add_next_index_zval(return_value, &result1); + add_next_index_zval(return_value, &result2); #if GMP_51_OR_NEWER /* mpz_rootrem() is supported since GMP 4.2, but buggy wrt odd roots * of negative numbers */ - mpz_rootrem(gmpnum_result1, gmpnum_result2, gmpnum_a, (unsigned long) nth); + mpz_rootrem(gmpnum_result1, gmpnum_result2, gmpnum_a, (gmp_ulong) nth); #else - mpz_root(gmpnum_result1, gmpnum_a, (unsigned long) nth); - mpz_pow_ui(gmpnum_result2, gmpnum_result1, (unsigned long) nth); + mpz_root(gmpnum_result1, gmpnum_a, (gmp_ulong) nth); + mpz_pow_ui(gmpnum_result2, gmpnum_result1, (gmp_ulong) nth); mpz_sub(gmpnum_result2, gmpnum_a, gmpnum_result2); #endif @@ -1632,7 +1598,7 @@ ZEND_FUNCTION(gmp_perfect_square) mpz_ptr gmpnum_a; gmp_temp_t temp_a; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &a_arg) == FAILURE){ + if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &a_arg) == FAILURE){ return; } @@ -1649,10 +1615,10 @@ ZEND_FUNCTION(gmp_prob_prime) { zval *gmpnumber_arg; mpz_ptr gmpnum_a; - long reps = 10; + zend_long reps = 10; gmp_temp_t temp_a; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|l", &gmpnumber_arg, &reps) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|l", &gmpnumber_arg, &reps) == FAILURE) { return; } @@ -1678,18 +1644,23 @@ ZEND_FUNCTION(gmp_gcdext) zval *a_arg, *b_arg; mpz_ptr gmpnum_a, gmpnum_b, gmpnum_t, gmpnum_s, gmpnum_g; gmp_temp_t temp_a, temp_b; + zval result_g, result_s, result_t; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz", &a_arg, &b_arg) == FAILURE){ + if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &a_arg, &b_arg) == FAILURE){ return; } FETCH_GMP_ZVAL(gmpnum_a, a_arg, temp_a); FETCH_GMP_ZVAL_DEP(gmpnum_b, b_arg, temp_b, temp_a); + gmp_create(&result_g, &gmpnum_g); + gmp_create(&result_s, &gmpnum_s); + gmp_create(&result_t, &gmpnum_t); + array_init(return_value); - add_assoc_zval(return_value, "g", gmp_create(&gmpnum_g TSRMLS_CC)); - add_assoc_zval(return_value, "s", gmp_create(&gmpnum_s TSRMLS_CC)); - add_assoc_zval(return_value, "t", gmp_create(&gmpnum_t TSRMLS_CC)); + add_assoc_zval(return_value, "g", &result_g); + add_assoc_zval(return_value, "s", &result_s); + add_assoc_zval(return_value, "t", &result_t); mpz_gcdext(gmpnum_g, gmpnum_s, gmpnum_t, gmpnum_a, gmpnum_b); FREE_GMP_TEMP(temp_a); @@ -1706,7 +1677,7 @@ ZEND_FUNCTION(gmp_invert) gmp_temp_t temp_a, temp_b; int res; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz", &a_arg, &b_arg) == FAILURE){ + if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &a_arg, &b_arg) == FAILURE){ return; } @@ -1746,11 +1717,11 @@ ZEND_FUNCTION(gmp_cmp) { zval *a_arg, *b_arg; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz", &a_arg, &b_arg) == FAILURE){ + if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &a_arg, &b_arg) == FAILURE){ return; } - gmp_cmp(return_value, a_arg, b_arg TSRMLS_CC); + gmp_cmp(return_value, a_arg, b_arg); } /* }}} */ @@ -1763,7 +1734,7 @@ ZEND_FUNCTION(gmp_sign) mpz_ptr gmpnum_a; gmp_temp_t temp_a; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &a_arg) == FAILURE){ + if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &a_arg) == FAILURE){ return; } @@ -1774,15 +1745,11 @@ ZEND_FUNCTION(gmp_sign) } /* }}} */ -static void gmp_init_random(TSRMLS_D) +static void gmp_init_random(void) { if (!GMPG(rand_initialized)) { /* Initialize */ -#if GMP_42_OR_NEWER gmp_randinit_mt(GMPG(rand_state)); -#else - gmp_randinit_lc_2exp_size(GMPG(rand_state), 32L); -#endif /* Seed */ gmp_randseed_ui(GMPG(rand_state), GENERATE_SEED()); @@ -1794,15 +1761,15 @@ static void gmp_init_random(TSRMLS_D) Gets random number */ ZEND_FUNCTION(gmp_random) { - long limiter = 20; + zend_long limiter = 20; mpz_ptr gmpnum_result; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &limiter) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &limiter) == FAILURE) { return; } INIT_GMP_RETVAL(gmpnum_result); - gmp_init_random(TSRMLS_C); + gmp_init_random(); #ifdef GMP_LIMB_BITS mpz_urandomb(gmpnum_result, GMPG(rand_state), GMP_ABS (limiter) * GMP_LIMB_BITS); @@ -1812,24 +1779,51 @@ ZEND_FUNCTION(gmp_random) } /* }}} */ +/* {{{ proto GMP gmp_random_seed(mixed seed) + Seed the RNG */ +ZEND_FUNCTION(gmp_random_seed) +{ + zval *seed; + mpz_ptr gmpnum_seed; + gmp_temp_t temp_a; + + if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &seed) == FAILURE) { + return; + } + + gmp_init_random(); + + if (Z_TYPE_P(seed) == IS_LONG && Z_LVAL_P(seed) >= 0) { + gmp_randseed_ui(GMPG(rand_state), Z_LVAL_P(seed)); + } + else { + FETCH_GMP_ZVAL(gmpnum_seed, seed, temp_a); + + gmp_randseed(GMPG(rand_state), gmpnum_seed); + + FREE_GMP_TEMP(temp_a); + } +} +/* }}} */ + /* {{{ proto GMP gmp_random_bits(int bits) Gets a random number in the range 0 to (2 ** n) - 1 */ ZEND_FUNCTION(gmp_random_bits) { - long bits; + zend_long bits; mpz_ptr gmpnum_result; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &bits) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &bits) == FAILURE) { return; } if (bits <= 0) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "The number of bits must be positive"); + php_error_docref(NULL, E_WARNING, "The number of bits must be positive"); RETURN_FALSE; } INIT_GMP_RETVAL(gmpnum_result); - gmp_init_random(TSRMLS_C); + gmp_init_random(); mpz_urandomb(gmpnum_result, GMPG(rand_state), bits); } @@ -1844,18 +1838,18 @@ ZEND_FUNCTION(gmp_random_range) mpz_t gmpnum_range; gmp_temp_t temp_a, temp_b; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz", &min_arg, &max_arg) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &min_arg, &max_arg) == FAILURE) { return; } - gmp_init_random(TSRMLS_C); + gmp_init_random(); FETCH_GMP_ZVAL(gmpnum_max, max_arg, temp_a); if (Z_TYPE_P(min_arg) == IS_LONG && Z_LVAL_P(min_arg) >= 0) { if (mpz_cmp_ui(gmpnum_max, Z_LVAL_P(min_arg)) <= 0) { FREE_GMP_TEMP(temp_a); - php_error_docref(NULL TSRMLS_CC, E_WARNING, "The minimum value must be less than the maximum value"); + php_error_docref(NULL, E_WARNING, "The minimum value must be less than the maximum value"); RETURN_FALSE; } @@ -1882,7 +1876,7 @@ ZEND_FUNCTION(gmp_random_range) if (mpz_cmp(gmpnum_max, gmpnum_min) <= 0) { FREE_GMP_TEMP(temp_b); FREE_GMP_TEMP(temp_a); - php_error_docref(NULL TSRMLS_CC, E_WARNING, "The minimum value must be less than the maximum value"); + php_error_docref(NULL, E_WARNING, "The minimum value must be less than the maximum value"); RETURN_FALSE; } @@ -1946,17 +1940,17 @@ ZEND_FUNCTION(gmp_xor) ZEND_FUNCTION(gmp_setbit) { zval *a_arg; - long index; + zend_long index; zend_bool set = 1; mpz_ptr gmpnum_a; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Ol|b", &a_arg, gmp_ce, &index, &set) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ol|b", &a_arg, gmp_ce, &index, &set) == FAILURE) { return; } if (index < 0) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Index must be greater than or equal to zero"); - return; + php_error_docref(NULL, E_WARNING, "Index must be greater than or equal to zero"); + RETURN_FALSE; } gmpnum_a = GET_GMP_FROM_ZVAL(a_arg); @@ -1974,16 +1968,16 @@ ZEND_FUNCTION(gmp_setbit) ZEND_FUNCTION(gmp_clrbit) { zval *a_arg; - long index; + zend_long index; mpz_ptr gmpnum_a; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Ol", &a_arg, gmp_ce, &index) == FAILURE){ + if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ol", &a_arg, gmp_ce, &index) == FAILURE){ return; } if (index < 0) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Index must be greater than or equal to zero"); - return; + php_error_docref(NULL, E_WARNING, "Index must be greater than or equal to zero"); + RETURN_FALSE; } gmpnum_a = GET_GMP_FROM_ZVAL(a_arg); @@ -1996,16 +1990,16 @@ ZEND_FUNCTION(gmp_clrbit) ZEND_FUNCTION(gmp_testbit) { zval *a_arg; - long index; + zend_long index; mpz_ptr gmpnum_a; gmp_temp_t temp_a; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zl", &a_arg, &index) == FAILURE){ + if (zend_parse_parameters(ZEND_NUM_ARGS(), "zl", &a_arg, &index) == FAILURE){ return; } if (index < 0) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Index must be greater than or equal to zero"); + php_error_docref(NULL, E_WARNING, "Index must be greater than or equal to zero"); RETURN_FALSE; } @@ -2038,14 +2032,14 @@ ZEND_FUNCTION(gmp_scan0) zval *a_arg; mpz_ptr gmpnum_a; gmp_temp_t temp_a; - long start; + zend_long start; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zl", &a_arg, &start) == FAILURE){ + if (zend_parse_parameters(ZEND_NUM_ARGS(), "zl", &a_arg, &start) == FAILURE){ return; } if (start < 0) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Starting index must be greater than or equal to zero"); + php_error_docref(NULL, E_WARNING, "Starting index must be greater than or equal to zero"); RETURN_FALSE; } @@ -2063,14 +2057,14 @@ ZEND_FUNCTION(gmp_scan1) zval *a_arg; mpz_ptr gmpnum_a; gmp_temp_t temp_a; - long start; + zend_long start; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zl", &a_arg, &start) == FAILURE){ + if (zend_parse_parameters(ZEND_NUM_ARGS(), "zl", &a_arg, &start) == FAILURE){ return; } if (start < 0) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Starting index must be greater than or equal to zero"); + php_error_docref(NULL, E_WARNING, "Starting index must be greater than or equal to zero"); RETURN_FALSE; } diff --git a/ext/gmp/php_gmp.h b/ext/gmp/php_gmp.h index 78dbca5294..5f5f136b33 100644 --- a/ext/gmp/php_gmp.h +++ b/ext/gmp/php_gmp.h @@ -1,6 +1,6 @@ /* +----------------------------------------------------------------------+ - | PHP Version 5 | + | PHP Version 7 | +----------------------------------------------------------------------+ | Copyright (c) 1997-2016 The PHP Group | +----------------------------------------------------------------------+ @@ -26,6 +26,9 @@ extern zend_module_entry gmp_module_entry; #define phpext_gmp_ptr &gmp_module_entry +#include "php_version.h" +#define PHP_GMP_VERSION PHP_VERSION + ZEND_MODULE_STARTUP_D(gmp); ZEND_MODULE_DEACTIVATE_D(gmp); ZEND_MODULE_INFO_D(gmp); @@ -66,6 +69,7 @@ ZEND_FUNCTION(gmp_or); ZEND_FUNCTION(gmp_com); ZEND_FUNCTION(gmp_xor); ZEND_FUNCTION(gmp_random); +ZEND_FUNCTION(gmp_random_seed); ZEND_FUNCTION(gmp_random_bits); ZEND_FUNCTION(gmp_random_range); ZEND_FUNCTION(gmp_setbit); @@ -77,15 +81,24 @@ ZEND_FUNCTION(gmp_popcount); ZEND_FUNCTION(gmp_hamdist); ZEND_FUNCTION(gmp_nextprime); +/* GMP and MPIR use different datatypes on different platforms */ +#ifdef PHP_WIN32 +typedef zend_long gmp_long; +typedef zend_ulong gmp_ulong; +#else +typedef long gmp_long; +typedef unsigned long gmp_ulong; +#endif + ZEND_BEGIN_MODULE_GLOBALS(gmp) zend_bool rand_initialized; gmp_randstate_t rand_state; ZEND_END_MODULE_GLOBALS(gmp) -#ifdef ZTS -#define GMPG(v) TSRMG(gmp_globals_id, zend_gmp_globals *, v) -#else -#define GMPG(v) (gmp_globals.v) +#define GMPG(v) ZEND_MODULE_GLOBALS_ACCESSOR(gmp, v) + +#if defined(ZTS) && defined(COMPILE_DL_GMP) +ZEND_TSRMLS_CACHE_EXTERN(); #endif #else diff --git a/ext/gmp/tests/bug50283.phpt b/ext/gmp/tests/bug50283.phpt index 13eef54804..561cd3d68c 100644 --- a/ext/gmp/tests/bug50283.phpt +++ b/ext/gmp/tests/bug50283.phpt @@ -2,7 +2,6 @@ Feature Request #50283 (allow base in gmp_strval to use full range: 2 to 62, and -2 to -36) --SKIPIF-- <?php if (!extension_loaded("gmp")) print "skip"; ?> -<?php if (version_compare(GMP_VERSION, "4.2.0", "<")) print "skip"; ?> --FILE-- <?php $a = gmp_init("0x41682179fbf5"); diff --git a/ext/gmp/tests/bug70284.phpt b/ext/gmp/tests/bug70284.phpt index ab17c0fc1c..58fc50cf2d 100644 --- a/ext/gmp/tests/bug70284.phpt +++ b/ext/gmp/tests/bug70284.phpt @@ -42,7 +42,7 @@ array(2) { [0]=> array(1) { [0]=> - NULL + string(1) "1" } ["num"]=> string(1) "1" diff --git a/ext/gmp/tests/gmp_clrbit.phpt b/ext/gmp/tests/gmp_clrbit.phpt index 079d5d669f..0aab89dd37 100644 --- a/ext/gmp/tests/gmp_clrbit.phpt +++ b/ext/gmp/tests/gmp_clrbit.phpt @@ -10,7 +10,7 @@ gmp_clrbit($n, 0); var_dump(gmp_strval($n)); $n = gmp_init(-1); -gmp_clrbit($n, -1); +var_dump(gmp_clrbit($n, -1)); var_dump(gmp_strval($n)); $n = gmp_init("1000000"); @@ -35,10 +35,11 @@ gmp_clrbit(); echo "Done\n"; ?> ---EXPECTF-- +--EXPECTF-- string(1) "0" Warning: gmp_clrbit(): Index must be greater than or equal to zero in %s on line %d +bool(false) string(2) "-1" Warning: gmp_clrbit(): Index must be greater than or equal to zero in %s on line %d diff --git a/ext/gmp/tests/gmp_php_int_max.phpt b/ext/gmp/tests/gmp_php_int_max.phpt new file mode 100644 index 0000000000..1f680a1490 --- /dev/null +++ b/ext/gmp/tests/gmp_php_int_max.phpt @@ -0,0 +1,29 @@ +--TEST-- +PHP_INT_MAX tests +--SKIPIF-- +<?php +if (!extension_loaded("gmp")) print "skip"; +if (PHP_INT_SIZE != 8) print "skip"; +?> +--FILE-- +<?php + +var_dump(gmp_mul(PHP_INT_MAX, PHP_INT_MAX)); +var_dump(gmp_add(PHP_INT_MAX, PHP_INT_MAX)); +var_dump(gmp_mul(PHP_INT_MAX, PHP_INT_MIN)); +?> +DONE +--EXPECTF-- +object(GMP)#%d (%d) { + ["num"]=> + string(38) "85070591730234615847396907784232501249" +} +object(GMP)#%d (%d) { + ["num"]=> + string(20) "18446744073709551614" +} +object(GMP)#%d (%d) { + ["num"]=> + string(39) "-85070591730234615856620279821087277056" +} +DONE diff --git a/ext/gmp/tests/gmp_pow.phpt b/ext/gmp/tests/gmp_pow.phpt index e17ecc7bad..6140b0cebf 100644 --- a/ext/gmp/tests/gmp_pow.phpt +++ b/ext/gmp/tests/gmp_pow.phpt @@ -57,10 +57,10 @@ NULL Warning: gmp_pow() expects exactly 2 parameters, 0 given in %s on line %d NULL -Warning: gmp_pow() expects parameter 2 to be long, array given in %s on line %d +Warning: gmp_pow() expects parameter 2 to be integer, array given in %s on line %d NULL -Warning: gmp_pow() expects parameter 2 to be long, array given in %s on line %d +Warning: gmp_pow() expects parameter 2 to be integer, array given in %s on line %d NULL Warning: gmp_pow(): Unable to convert variable to GMP - wrong type in %s on line %d diff --git a/ext/gmp/tests/gmp_random.phpt b/ext/gmp/tests/gmp_random.phpt index 3a876aa60c..d1351bbd1e 100644 --- a/ext/gmp/tests/gmp_random.phpt +++ b/ext/gmp/tests/gmp_random.phpt @@ -27,15 +27,15 @@ string(%d) "%d" string(%d) "%d" string(%d) "%d" -Warning: gmp_random() expects parameter 1 to be long, array given in %s on line %d +Warning: gmp_random() expects parameter 1 to be integer, array given in %s on line %d NULL Warning: gmp_random() expects at most 1 parameter, 2 given in %s on line %d NULL -Warning: gmp_random() expects parameter 1 to be long, string given in %s on line %d +Warning: gmp_random() expects parameter 1 to be integer, string given in %s on line %d NULL -Warning: gmp_random() expects parameter 1 to be long, string given in %s on line %d +Warning: gmp_random() expects parameter 1 to be integer, string given in %s on line %d NULL Done diff --git a/ext/gmp/tests/gmp_random_seed-32bit.phpt b/ext/gmp/tests/gmp_random_seed-32bit.phpt new file mode 100644 index 0000000000..f0f4fca6e3 --- /dev/null +++ b/ext/gmp/tests/gmp_random_seed-32bit.phpt @@ -0,0 +1,230 @@ +--TEST-- +gmp_random_seed() basic tests +--SKIPIF-- +<?php if (!extension_loaded("gmp")) print "skip"; ?> +<?php if (PHP_INT_SIZE != 4) die("skip this test is for 32bit platform only"); ?> +--FILE-- +<?php + +// zero int +var_dump(gmp_random_seed(0)); + +var_dump(gmp_strval(gmp_random())); +var_dump(gmp_strval(gmp_random(1))); +var_dump(gmp_strval(gmp_random(10))); + +var_dump(gmp_strval(gmp_random_bits(10))); +var_dump(gmp_strval(gmp_random_bits(100))); +var_dump(gmp_strval(gmp_random_bits(1000))); + +var_dump(gmp_strval(gmp_random_range(0, 10000))); +var_dump(gmp_strval(gmp_random_range(-10000, 10000))); +var_dump(gmp_strval(gmp_random_range(-10000, 0))); + + +// zero gmp +var_dump(gmp_random_seed(gmp_init(0))); + +var_dump(gmp_strval(gmp_random())); +var_dump(gmp_strval(gmp_random(1))); +var_dump(gmp_strval(gmp_random(10))); + +var_dump(gmp_strval(gmp_random_bits(10))); +var_dump(gmp_strval(gmp_random_bits(100))); +var_dump(gmp_strval(gmp_random_bits(1000))); + +var_dump(gmp_strval(gmp_random_range(0, 10000))); +var_dump(gmp_strval(gmp_random_range(-10000, 10000))); +var_dump(gmp_strval(gmp_random_range(-10000, 0))); + + +// negative int +var_dump(gmp_random_seed(-100)); + +var_dump(gmp_strval(gmp_random())); +var_dump(gmp_strval(gmp_random(1))); +var_dump(gmp_strval(gmp_random(10))); + +var_dump(gmp_strval(gmp_random_bits(10))); +var_dump(gmp_strval(gmp_random_bits(100))); +var_dump(gmp_strval(gmp_random_bits(1000))); + +var_dump(gmp_strval(gmp_random_range(0, 10000))); +var_dump(gmp_strval(gmp_random_range(-10000, 10000))); +var_dump(gmp_strval(gmp_random_range(-10000, 0))); + + +// negative gmp +var_dump(gmp_random_seed(gmp_init(-100))); + +var_dump(gmp_strval(gmp_random())); +var_dump(gmp_strval(gmp_random(1))); +var_dump(gmp_strval(gmp_random(10))); + +var_dump(gmp_strval(gmp_random_bits(10))); +var_dump(gmp_strval(gmp_random_bits(100))); +var_dump(gmp_strval(gmp_random_bits(1000))); + +var_dump(gmp_strval(gmp_random_range(0, 10000))); +var_dump(gmp_strval(gmp_random_range(-10000, 10000))); +var_dump(gmp_strval(gmp_random_range(-10000, 0))); + + +// positive int +var_dump(gmp_random_seed(100)); + +var_dump(gmp_strval(gmp_random())); +var_dump(gmp_strval(gmp_random(1))); +var_dump(gmp_strval(gmp_random(10))); + +var_dump(gmp_strval(gmp_random_bits(10))); +var_dump(gmp_strval(gmp_random_bits(100))); +var_dump(gmp_strval(gmp_random_bits(1000))); + +var_dump(gmp_strval(gmp_random_range(0, 10000))); +var_dump(gmp_strval(gmp_random_range(-10000, 10000))); +var_dump(gmp_strval(gmp_random_range(-10000, 0))); + + +// positive gmp +var_dump(gmp_random_seed(100)); + +var_dump(gmp_strval(gmp_random())); +var_dump(gmp_strval(gmp_random(1))); +var_dump(gmp_strval(gmp_random(10))); + +var_dump(gmp_strval(gmp_random_bits(10))); +var_dump(gmp_strval(gmp_random_bits(100))); +var_dump(gmp_strval(gmp_random_bits(1000))); + +var_dump(gmp_strval(gmp_random_range(0, 10000))); +var_dump(gmp_strval(gmp_random_range(-10000, 10000))); +var_dump(gmp_strval(gmp_random_range(-10000, 0))); + + +$seed = gmp_init(1); +$seed <<= 512; + +// large negative gmp +var_dump(gmp_random_seed($seed * -1)); + +var_dump(gmp_strval(gmp_random())); +var_dump(gmp_strval(gmp_random(1))); +var_dump(gmp_strval(gmp_random(10))); + +var_dump(gmp_strval(gmp_random_bits(10))); +var_dump(gmp_strval(gmp_random_bits(100))); +var_dump(gmp_strval(gmp_random_bits(1000))); + +var_dump(gmp_strval(gmp_random_range(0, 10000))); +var_dump(gmp_strval(gmp_random_range(-10000, 10000))); +var_dump(gmp_strval(gmp_random_range(-10000, 0))); + + +// large positive gmp +var_dump(gmp_random_seed($seed)); + +var_dump(gmp_strval(gmp_random())); +var_dump(gmp_strval(gmp_random(1))); +var_dump(gmp_strval(gmp_random(10))); + +var_dump(gmp_strval(gmp_random_bits(10))); +var_dump(gmp_strval(gmp_random_bits(100))); +var_dump(gmp_strval(gmp_random_bits(1000))); + +var_dump(gmp_strval(gmp_random_range(0, 10000))); +var_dump(gmp_strval(gmp_random_range(-10000, 10000))); +var_dump(gmp_strval(gmp_random_range(-10000, 0))); + + +// standard non conversion error +var_dump(gmp_random_seed('not a number')); + + +echo "Done\n"; +?> +--EXPECTF-- +NULL +string(193) "2000022451473107283132031190545479155678097978829612864726689673263990484954900429188146529905019042225609517482723730948135765344007937183893986134210303199345433818579954244943136664096571499" +string(10) "3792281639" +string(97) "1281406676617847191505318987040099388645126071814252743902229506206669580396236451813261179688680" +string(2) "86" +string(30) "539590049119295715487546581833" +string(301) "1421393257467327495225454368915273750356127352353055226245692071178379257048985726109650766437957017507708821700349355658110489309871463201057688894986035545794429493563972043777521547094922637182497036750765553664196591958728194711096442220858890056073974348364544481270013556644174056049511198143353" +string(4) "8971" +string(4) "7838" +string(5) "-7823" +NULL +string(193) "2000022451473107283132031190545479155678097978829612864726689673263990484954900429188146529905019042225609517482723730948135765344007937183893986134210303199345433818579954244943136664096571499" +string(10) "3792281639" +string(97) "1281406676617847191505318987040099388645126071814252743902229506206669580396236451813261179688680" +string(2) "86" +string(30) "539590049119295715487546581833" +string(301) "1421393257467327495225454368915273750356127352353055226245692071178379257048985726109650766437957017507708821700349355658110489309871463201057688894986035545794429493563972043777521547094922637182497036750765553664196591958728194711096442220858890056073974348364544481270013556644174056049511198143353" +string(4) "8971" +string(4) "7838" +string(5) "-7823" +NULL +string(193) "2481908463276861025880840116973116041137167231715512003123396619946362384184452775649311777755189162364804428739438733886421190841599393284032499618736594054572838636962845446571583771860379424" +string(9) "513032517" +string(97) "1269389652350468424962405917839748437534426606829298977422985976022226588797299164880342257267822" +string(3) "111" +string(31) "1007352579363975542750518386428" +string(301) "5972672173406546932378461222050826195392618168845902231231085819967980051373112173617569638344495916340820360974139193579303652817960325137050189614326029375858951250371622253667077551034119958866481056760107123567590230789917209543206262943739654964107980961862387602012190907147667678648223791029527" +string(4) "2362" +string(5) "-9377" +string(5) "-8435" +NULL +string(193) "2481908463276861025880840116973116041137167231715512003123396619946362384184452775649311777755189162364804428739438733886421190841599393284032499618736594054572838636962845446571583771860379424" +string(9) "513032517" +string(97) "1269389652350468424962405917839748437534426606829298977422985976022226588797299164880342257267822" +string(3) "111" +string(31) "1007352579363975542750518386428" +string(301) "5972672173406546932378461222050826195392618168845902231231085819967980051373112173617569638344495916340820360974139193579303652817960325137050189614326029375858951250371622253667077551034119958866481056760107123567590230789917209543206262943739654964107980961862387602012190907147667678648223791029527" +string(4) "2362" +string(5) "-9377" +string(5) "-8435" +NULL +string(193) "2481908463276861025880840116973116041137167231715512003123396619946362384184452775649311777755189162364804428739438733886421190841599393284032499618736594054572838636962845446571583771860379424" +string(9) "513032517" +string(97) "1269389652350468424962405917839748437534426606829298977422985976022226588797299164880342257267822" +string(3) "111" +string(31) "1007352579363975542750518386428" +string(301) "5972672173406546932378461222050826195392618168845902231231085819967980051373112173617569638344495916340820360974139193579303652817960325137050189614326029375858951250371622253667077551034119958866481056760107123567590230789917209543206262943739654964107980961862387602012190907147667678648223791029527" +string(4) "2362" +string(5) "-9377" +string(5) "-8435" +NULL +string(193) "2481908463276861025880840116973116041137167231715512003123396619946362384184452775649311777755189162364804428739438733886421190841599393284032499618736594054572838636962845446571583771860379424" +string(9) "513032517" +string(97) "1269389652350468424962405917839748437534426606829298977422985976022226588797299164880342257267822" +string(3) "111" +string(31) "1007352579363975542750518386428" +string(301) "5972672173406546932378461222050826195392618168845902231231085819967980051373112173617569638344495916340820360974139193579303652817960325137050189614326029375858951250371622253667077551034119958866481056760107123567590230789917209543206262943739654964107980961862387602012190907147667678648223791029527" +string(4) "2362" +string(5) "-9377" +string(5) "-8435" +NULL +string(193) "4314861641594785951065006537821497084129700255567731661104366342587560122488351847486220156468888511744226746306538524023190734733433083354034308520246964938498767672539252131810891590624763642" +string(10) "3301287097" +string(97) "1532710025135934106005683287301200805525218110207399702226596673360222079570223526337866801028045" +string(3) "317" +string(30) "949682013136255600092941702271" +string(302) "10419061168407861659407303769436775291881708415032871985692089124472434944956010985892897595466043265819000974537553172663152640727594826474137173310039809413311432062725623183603946900548710858191078057179743726366436832870919212340219523613153893742764101120396858424887711932475871305848021245801132" +string(4) "2417" +string(5) "-7975" +string(4) "-378" +NULL +string(193) "4314861641594785951065006537821497084129700255567731661104366342587560122488351847486220156468888511744226746306538524023190734733433083354034308520246964938498767672539252131810891590624763642" +string(10) "3301287097" +string(97) "1532710025135934106005683287301200805525218110207399702226596673360222079570223526337866801028045" +string(3) "317" +string(30) "949682013136255600092941702271" +string(302) "10419061168407861659407303769436775291881708415032871985692089124472434944956010985892897595466043265819000974537553172663152640727594826474137173310039809413311432062725623183603946900548710858191078057179743726366436832870919212340219523613153893742764101120396858424887711932475871305848021245801132" +string(4) "2417" +string(5) "-7975" +string(4) "-378" + +Warning: gmp_random_seed(): Unable to convert variable to GMP - string is not an integer in %s on line %d +bool(false) +Done
\ No newline at end of file diff --git a/ext/gmp/tests/gmp_random_seed.phpt b/ext/gmp/tests/gmp_random_seed.phpt new file mode 100644 index 0000000000..d28704e124 --- /dev/null +++ b/ext/gmp/tests/gmp_random_seed.phpt @@ -0,0 +1,230 @@ +--TEST-- +gmp_random_seed() basic tests +--SKIPIF-- +<?php if (!extension_loaded("gmp")) print "skip"; ?> +<?php if (PHP_INT_SIZE != 8) die("skip this test is for 64bit platform only"); ?> +--FILE-- +<?php + +// zero int +var_dump(gmp_random_seed(0)); + +var_dump(gmp_strval(gmp_random())); +var_dump(gmp_strval(gmp_random(1))); +var_dump(gmp_strval(gmp_random(10))); + +var_dump(gmp_strval(gmp_random_bits(10))); +var_dump(gmp_strval(gmp_random_bits(100))); +var_dump(gmp_strval(gmp_random_bits(1000))); + +var_dump(gmp_strval(gmp_random_range(0, 10000))); +var_dump(gmp_strval(gmp_random_range(-10000, 10000))); +var_dump(gmp_strval(gmp_random_range(-10000, 0))); + + +// zero gmp +var_dump(gmp_random_seed(gmp_init(0))); + +var_dump(gmp_strval(gmp_random())); +var_dump(gmp_strval(gmp_random(1))); +var_dump(gmp_strval(gmp_random(10))); + +var_dump(gmp_strval(gmp_random_bits(10))); +var_dump(gmp_strval(gmp_random_bits(100))); +var_dump(gmp_strval(gmp_random_bits(1000))); + +var_dump(gmp_strval(gmp_random_range(0, 10000))); +var_dump(gmp_strval(gmp_random_range(-10000, 10000))); +var_dump(gmp_strval(gmp_random_range(-10000, 0))); + + +// negative int +var_dump(gmp_random_seed(-100)); + +var_dump(gmp_strval(gmp_random())); +var_dump(gmp_strval(gmp_random(1))); +var_dump(gmp_strval(gmp_random(10))); + +var_dump(gmp_strval(gmp_random_bits(10))); +var_dump(gmp_strval(gmp_random_bits(100))); +var_dump(gmp_strval(gmp_random_bits(1000))); + +var_dump(gmp_strval(gmp_random_range(0, 10000))); +var_dump(gmp_strval(gmp_random_range(-10000, 10000))); +var_dump(gmp_strval(gmp_random_range(-10000, 0))); + + +// negative gmp +var_dump(gmp_random_seed(gmp_init(-100))); + +var_dump(gmp_strval(gmp_random())); +var_dump(gmp_strval(gmp_random(1))); +var_dump(gmp_strval(gmp_random(10))); + +var_dump(gmp_strval(gmp_random_bits(10))); +var_dump(gmp_strval(gmp_random_bits(100))); +var_dump(gmp_strval(gmp_random_bits(1000))); + +var_dump(gmp_strval(gmp_random_range(0, 10000))); +var_dump(gmp_strval(gmp_random_range(-10000, 10000))); +var_dump(gmp_strval(gmp_random_range(-10000, 0))); + + +// positive int +var_dump(gmp_random_seed(100)); + +var_dump(gmp_strval(gmp_random())); +var_dump(gmp_strval(gmp_random(1))); +var_dump(gmp_strval(gmp_random(10))); + +var_dump(gmp_strval(gmp_random_bits(10))); +var_dump(gmp_strval(gmp_random_bits(100))); +var_dump(gmp_strval(gmp_random_bits(1000))); + +var_dump(gmp_strval(gmp_random_range(0, 10000))); +var_dump(gmp_strval(gmp_random_range(-10000, 10000))); +var_dump(gmp_strval(gmp_random_range(-10000, 0))); + + +// positive gmp +var_dump(gmp_random_seed(100)); + +var_dump(gmp_strval(gmp_random())); +var_dump(gmp_strval(gmp_random(1))); +var_dump(gmp_strval(gmp_random(10))); + +var_dump(gmp_strval(gmp_random_bits(10))); +var_dump(gmp_strval(gmp_random_bits(100))); +var_dump(gmp_strval(gmp_random_bits(1000))); + +var_dump(gmp_strval(gmp_random_range(0, 10000))); +var_dump(gmp_strval(gmp_random_range(-10000, 10000))); +var_dump(gmp_strval(gmp_random_range(-10000, 0))); + + +$seed = gmp_init(1); +$seed <<= 512; + +// large negative gmp +var_dump(gmp_random_seed($seed * -1)); + +var_dump(gmp_strval(gmp_random())); +var_dump(gmp_strval(gmp_random(1))); +var_dump(gmp_strval(gmp_random(10))); + +var_dump(gmp_strval(gmp_random_bits(10))); +var_dump(gmp_strval(gmp_random_bits(100))); +var_dump(gmp_strval(gmp_random_bits(1000))); + +var_dump(gmp_strval(gmp_random_range(0, 10000))); +var_dump(gmp_strval(gmp_random_range(-10000, 10000))); +var_dump(gmp_strval(gmp_random_range(-10000, 0))); + + +// large positive gmp +var_dump(gmp_random_seed($seed)); + +var_dump(gmp_strval(gmp_random())); +var_dump(gmp_strval(gmp_random(1))); +var_dump(gmp_strval(gmp_random(10))); + +var_dump(gmp_strval(gmp_random_bits(10))); +var_dump(gmp_strval(gmp_random_bits(100))); +var_dump(gmp_strval(gmp_random_bits(1000))); + +var_dump(gmp_strval(gmp_random_range(0, 10000))); +var_dump(gmp_strval(gmp_random_range(-10000, 10000))); +var_dump(gmp_strval(gmp_random_range(-10000, 0))); + + +// standard non conversion error +var_dump(gmp_random_seed('not a number')); + + +echo "Done\n"; +?> +--EXPECTF-- +NULL +string(386) "16100871751340485642888774479422205950971474538471317276388238970713821926852258806210387669237144400278914671533438653274777493140545293541785377162348524402063489947660558889561219968642920852870483050552936324125257259316643328803697665037881088889859735075814746314563786538493931260996669892959501637800179548654075887300734264333417283208357503038004080669367070111848040502362219" +string(18) "255344473360201232" +string(192) "566276705882089203328999735915155615747289398229935944715725865523491463654289449864817867794422824157675456435165973986660058784111212531276312901205233176071526587181942240113004108328736022" +string(3) "766" +string(31) "1251852006013618829761115383588" +string(301) "2904442664575028522451529381233481137998826790384445089758175726247096826023839957531211794198483328480161675791738894500687706952157332727908305084432443942315866545175274665372161864357698401817740956147940095302549920711069038378541222669595494627580205085300332122174778540693048337420608925104417" +string(4) "5969" +string(5) "-4126" +string(4) "-926" +NULL +string(386) "16100871751340485642888774479422205950971474538471317276388238970713821926852258806210387669237144400278914671533438653274777493140545293541785377162348524402063489947660558889561219968642920852870483050552936324125257259316643328803697665037881088889859735075814746314563786538493931260996669892959501637800179548654075887300734264333417283208357503038004080669367070111848040502362219" +string(18) "255344473360201232" +string(192) "566276705882089203328999735915155615747289398229935944715725865523491463654289449864817867794422824157675456435165973986660058784111212531276312901205233176071526587181942240113004108328736022" +string(3) "766" +string(31) "1251852006013618829761115383588" +string(301) "2904442664575028522451529381233481137998826790384445089758175726247096826023839957531211794198483328480161675791738894500687706952157332727908305084432443942315866545175274665372161864357698401817740956147940095302549920711069038378541222669595494627580205085300332122174778540693048337420608925104417" +string(4) "5969" +string(5) "-4126" +string(4) "-926" +NULL +string(386) "13477111096113160882601567427091178332669645276785709413953468738199940626922635042144840457533224221336117027441609364710893482124071124759231943384805378201041406842697962243732316555316214869988749798708139879922380266366387589101775891621221881149417841139463207495993669582399783202126977651864760442797681787747348653884279195479310922110107643437514016795836672871442926389274400" +string(20) "15370156633245019617" +string(192) "294354325919119835375781661354719128667828860233586416953977190644006896604022494655398295674227944872858213051595447565156112646032890737200590095517623075051828676500990477704073251304424133" +string(3) "683" +string(31) "1105092118036828878542238774672" +string(301) "2700084798786584694260166508009114488318099110808331607090845844712329387915039325877090587052399841255219556028410036280510827424748532204766771994624650610348058361519239518625728955462297681525123214377383395734875500143425080808436274385326255154393544373636015993206705180032889399161843788895374" +string(4) "7268" +string(5) "-3518" +string(5) "-8432" +NULL +string(386) "13477111096113160882601567427091178332669645276785709413953468738199940626922635042144840457533224221336117027441609364710893482124071124759231943384805378201041406842697962243732316555316214869988749798708139879922380266366387589101775891621221881149417841139463207495993669582399783202126977651864760442797681787747348653884279195479310922110107643437514016795836672871442926389274400" +string(20) "15370156633245019617" +string(192) "294354325919119835375781661354719128667828860233586416953977190644006896604022494655398295674227944872858213051595447565156112646032890737200590095517623075051828676500990477704073251304424133" +string(3) "683" +string(31) "1105092118036828878542238774672" +string(301) "2700084798786584694260166508009114488318099110808331607090845844712329387915039325877090587052399841255219556028410036280510827424748532204766771994624650610348058361519239518625728955462297681525123214377383395734875500143425080808436274385326255154393544373636015993206705180032889399161843788895374" +string(4) "7268" +string(5) "-3518" +string(5) "-8432" +NULL +string(386) "13477111096113160882601567427091178332669645276785709413953468738199940626922635042144840457533224221336117027441609364710893482124071124759231943384805378201041406842697962243732316555316214869988749798708139879922380266366387589101775891621221881149417841139463207495993669582399783202126977651864760442797681787747348653884279195479310922110107643437514016795836672871442926389274400" +string(20) "15370156633245019617" +string(192) "294354325919119835375781661354719128667828860233586416953977190644006896604022494655398295674227944872858213051595447565156112646032890737200590095517623075051828676500990477704073251304424133" +string(3) "683" +string(31) "1105092118036828878542238774672" +string(301) "2700084798786584694260166508009114488318099110808331607090845844712329387915039325877090587052399841255219556028410036280510827424748532204766771994624650610348058361519239518625728955462297681525123214377383395734875500143425080808436274385326255154393544373636015993206705180032889399161843788895374" +string(4) "7268" +string(5) "-3518" +string(5) "-8432" +NULL +string(386) "13477111096113160882601567427091178332669645276785709413953468738199940626922635042144840457533224221336117027441609364710893482124071124759231943384805378201041406842697962243732316555316214869988749798708139879922380266366387589101775891621221881149417841139463207495993669582399783202126977651864760442797681787747348653884279195479310922110107643437514016795836672871442926389274400" +string(20) "15370156633245019617" +string(192) "294354325919119835375781661354719128667828860233586416953977190644006896604022494655398295674227944872858213051595447565156112646032890737200590095517623075051828676500990477704073251304424133" +string(3) "683" +string(31) "1105092118036828878542238774672" +string(301) "2700084798786584694260166508009114488318099110808331607090845844712329387915039325877090587052399841255219556028410036280510827424748532204766771994624650610348058361519239518625728955462297681525123214377383395734875500143425080808436274385326255154393544373636015993206705180032889399161843788895374" +string(4) "7268" +string(5) "-3518" +string(5) "-8432" +NULL +string(386) "17517289823903393220742578279919954815229524740463730368402128237511862318453381595675765692750750649609755422480004471234960388086555321894591036872550129477305413674775698107868844953599169316550102271816620108199930104365341610775602960735862041722613145476720452800951958891882288668416542937408952006310656170195090436314902430700708511047189929836145291647101130135292078875631354" +string(19) "1662391866670215057" +string(193) "1951928859951518261564127834731454911658112769477733872890285741065126442731035642243573666695893929882207432512593006044657806021743917753379619843420559355572830613932424235592411658293328273" +string(3) "888" +string(30) "136524289584478309125073026188" +string(301) "4487372666528061674404740793683112894444118579769413902123304803304884162086348577960502430419080687314731489440882833272125181594897832730214825704339272207090970657364333461383490282984012738008555512699878911293400686609929745464733074891420787002129849587668122219953473716759349853748437799165176" +string(4) "8559" +string(4) "9426" +string(5) "-2932" +NULL +string(386) "17517289823903393220742578279919954815229524740463730368402128237511862318453381595675765692750750649609755422480004471234960388086555321894591036872550129477305413674775698107868844953599169316550102271816620108199930104365341610775602960735862041722613145476720452800951958891882288668416542937408952006310656170195090436314902430700708511047189929836145291647101130135292078875631354" +string(19) "1662391866670215057" +string(193) "1951928859951518261564127834731454911658112769477733872890285741065126442731035642243573666695893929882207432512593006044657806021743917753379619843420559355572830613932424235592411658293328273" +string(3) "888" +string(30) "136524289584478309125073026188" +string(301) "4487372666528061674404740793683112894444118579769413902123304803304884162086348577960502430419080687314731489440882833272125181594897832730214825704339272207090970657364333461383490282984012738008555512699878911293400686609929745464733074891420787002129849587668122219953473716759349853748437799165176" +string(4) "8559" +string(4) "9426" +string(5) "-2932" + +Warning: gmp_random_seed(): Unable to convert variable to GMP - string is not an integer in %s on line %d +bool(false) +Done diff --git a/ext/gmp/tests/gmp_setbit.phpt b/ext/gmp/tests/gmp_setbit.phpt index 99848959d5..2eac23db0e 100644 --- a/ext/gmp/tests/gmp_setbit.phpt +++ b/ext/gmp/tests/gmp_setbit.phpt @@ -10,7 +10,7 @@ gmp_setbit($n, 10, -1); var_dump(gmp_strval($n)); $n = gmp_init(5); -gmp_setbit($n, -20, 0); +var_dump(gmp_setbit($n, -20, 0)); var_dump(gmp_strval($n)); $n = gmp_init(5); @@ -41,10 +41,11 @@ gmp_setbit($a,array()); echo "Done\n"; ?> ---EXPECTF-- +--EXPECTF-- string(2) "-1" Warning: gmp_setbit(): Index must be greater than or equal to zero in %s on line %d +bool(false) string(1) "5" string(1) "1" string(1) "7" diff --git a/ext/gmp/tests/gmp_strval.phpt b/ext/gmp/tests/gmp_strval.phpt index 79fd73ecf8..df3e596a19 100644 --- a/ext/gmp/tests/gmp_strval.phpt +++ b/ext/gmp/tests/gmp_strval.phpt @@ -40,13 +40,13 @@ NULL Warning: gmp_strval(): Unable to convert variable to GMP - string is not an integer in %s on line %d bool(false) -Warning: gmp_strval() expects parameter 2 to be long, string given in %s on line %d +Warning: gmp_strval() expects parameter 2 to be integer, string given in %s on line %d NULL Warning: gmp_strval(): Bad base for conversion: -1 (should be between 2 and %d or -2 and -%d) in %s on line %d bool(false) -Warning: gmp_strval() expects parameter 2 to be long, string given in %s on line %d +Warning: gmp_strval() expects parameter 2 to be integer, string given in %s on line %d NULL Warning: gmp_strval(): Unable to convert variable to GMP - wrong type in %s on line %d @@ -68,10 +68,10 @@ Warning: gmp_strval(): Bad base for conversion: 100000 (should be between 2 and bool(false) string(8) "-3373333" -Warning: gmp_strval() expects parameter 2 to be long, array given in %s on line %d +Warning: gmp_strval() expects parameter 2 to be integer, array given in %s on line %d NULL -Warning: gmp_strval() expects parameter 2 to be long, object given in %s on line %d +Warning: gmp_strval() expects parameter 2 to be integer, object given in %s on line %d NULL Warning: gmp_strval(): Unable to convert variable to GMP - wrong type in %s on line %d |