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: 613 628 97.6 %
Date: 2014-08-04 Functions: 74 74 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Mon, 04 Aug 2014 15:49:04 +0000 (18 days ago)

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