Re: [PHP-DEV] Re: gmp_init with a base argument

2001-08-05 Thread Troels Arvin

On Thu, 02 Aug 2001 19:16:26 +0200, Stanislav Malyshev [EMAIL PROTECTED]
wrote:

 on the quick glance your patch looks OK.
OK. It seems that I don't have sufficient karma.

I tried to commit the below patch with the following commit message:
(gmp_init) Added extra (optional) argument to gmp_init():
   a base argument which indicates the number base. E.g.:
   gmp_init('1010101010',2); // feed gmp a binary value.
@- Added optional extra argument to gmp_init(). The extra argument
@  indicates which number base gmp should use when converting a
@  string to the gmp-number. (Troels)

Suggested patch is attached below (the patch is against current CVS).

/Troels

--- php4-orig/ext/gmp/gmp.c Sun Aug  5 12:51:42 2001
+++ php4/ext/gmp/gmp.c  Sun Aug  5 13:24:02 2001
@@ -178,7 +178,7 @@
 if(Z_TYPE_PP(zval) == IS_RESOURCE) { \
ZEND_FETCH_RESOURCE(gmpnumber, mpz_t *, zval, -1, GMP_RESOURCE_NAME, le_gmp);\
 } else {\
-   if(convert_to_gmp(gmpnumber,zval) == FAILURE) {\
+   if(convert_to_gmp(gmpnumber,zval,0) == FAILURE) {\
RETURN_FALSE;\
}\
ZEND_REGISTER_RESOURCE(NULL, gmpnumber, le_gmp);\
@@ -190,7 +190,7 @@
 
 /* {{{ convert_to_gmp
  * Convert zval to be gmp number */
-static int convert_to_gmp(mpz_t * *gmpnumber, zval **val) 
+static int convert_to_gmp(mpz_t * *gmpnumber, zval **val, int base) 
 {
int ret = 0;
 
@@ -207,11 +207,14 @@
case IS_STRING:
{
char *numstr = Z_STRVAL_PP(val);
-   if(numstr[0] == '0'  (numstr[1] == 'x' || numstr[1] == 'X')) 
{
-   ret = mpz_init_set_str(**gmpnumber, numstr+2, 16);
-   } else {
-   ret = mpz_init_set_str(**gmpnumber, numstr, 10);
+   if (base==0) {
+   if(numstr[0] == '0'  (numstr[1] == 'x' || numstr[1] 
+== 'X')) {
+   base=16;
+   } else {
+   base=10;
+   }
}
+   ret = mpz_init_set_str(**gmpnumber, numstr, base);
}
break;
default:
@@ -434,22 +437,30 @@
 }
 /* }}} */
 
-/* Remove the following function when you have succesfully modified config.m4
-   so that your module can be compiled into PHP, it exists only for testing
-   purposes. */
-
-/* {{{ proto resource gmp_init(mixed number)
+/* {{{ proto resource gmp_init(mixed number [, int base])
Initializes GMP number */
 ZEND_FUNCTION(gmp_init)
 {
-   zval **number_arg;
+   zval **number_arg, **base_arg;
mpz_t * gmpnumber;
+   int argc;
+   int base=0;
 
-   if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, number_arg) == FAILURE){
+   argc = ZEND_NUM_ARGS();
+   if (argc  1 || argc  2 || zend_get_parameters_ex(argc, number_arg, 
+base_arg) == FAILURE){
WRONG_PARAM_COUNT;
}
 
-   if(convert_to_gmp(gmpnumber,number_arg) == FAILURE) {
+   if (argc==2) {
+   convert_to_long_ex(base_arg);
+   base = Z_LVAL_PP(base_arg);
+   if(base  2 || base  36) {
+   zend_error(E_WARNING, Bad base for conversion: %d (should be 
+between 2 and 36), base);
+   RETURN_FALSE;
+   }
+   }
+
+   if(convert_to_gmp(gmpnumber,number_arg,base) == FAILURE) {
RETURN_FALSE;
}

-- 
PHP Development Mailing List http://www.php.net/
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




Re: [PHP-DEV] Re: gmp_init with a base argument

2001-08-05 Thread Troels Arvin

On Thu, 02 Aug 2001 19:16:26 +0200, Stanislav Malyshev [EMAIL PROTECTED]
wrote:

 TA By the way:
 TA I think that the decbin() function should bail out if you pass it
 a TA value which contains more bits than PHP is able to handle. -
 Perhaps TA with a hint about using GMP for large numbers.
 
 I'd say give a warning istead of bail out, but that's a good idea,
 generally.

OK. Here is a suggestion for an adjustment in math.c. The patch could
probably be improved: The check for strings with more than 31 chars
should probably be less hard-coded, but I'm not sure which contant to
use for determining maximum size of a LONG.

/Troels

--- php4-orig/ext/standard/math.c   Sun Aug  5 12:51:38 2001
+++ php4/ext/standard/math.cSun Aug  5 12:52:35 2001
@@ -813,6 +813,11 @@
}
 
convert_to_string_ex(arg);
+
+   if ((*arg)-value.str.len  31) {
+   php_error(E_WARNING, bindec: input string too long (max is a 31-bit 
+value));
+   }
+
ret = _php_math_basetolong(*arg, 2);
 
RETVAL_LONG(ret);

-- 
PHP Development Mailing List http://www.php.net/
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




Re: [PHP-DEV] Re: gmp_init with a base argument

2001-08-05 Thread Stanislav Malyshev

TA OK. Here is a suggestion for an adjustment in math.c. The patch
TA could probably be improved: The check for strings with more than
TA 31 chars should probably be less hard-coded, but I'm not sure
TA which contant to use for determining maximum size of a LONG.

I have made a bit more general patch, see CVS of math.c
-- 
Stanislav Malyshev, Zend Products Engineer
[EMAIL PROTECTED]  http://www.zend.com/ +972-3-6139665 ext.115



-- 
PHP Development Mailing List http://www.php.net/
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




Re: [PHP-DEV] Re: gmp_init with a base argument

2001-08-02 Thread Stanislav Malyshev

TA Does the lack of comments mean that I may commit the patch? (I think
TA that I still have CVS access.)

I was pretty busy last week, sorry, so I couldn't look at it thoroughly.
But on the quick glance your patch looks OK.

TA By the way:
TA I think that the decbin() function should bail out if you pass it a
TA value which contains more bits than PHP is able to handle. - Perhaps
TA with a hint about using GMP for large numbers.

I'd say give a warning istead of bail out, but that's a good idea,
generally.

-- 
Stanislav Malyshev, Zend Products Engineer
[EMAIL PROTECTED]  http://www.zend.com/ +972-3-6139665 ext.115




-- 
PHP Development Mailing List http://www.php.net/
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]