From 526ebc5abfc2a51c012b6ca4969d87d80c3cc84e Mon Sep 17 00:00:00 2001 From: divinity76 Date: Tue, 1 Nov 2016 19:39:52 +0100 Subject: [PATCH 1/3] validate input, and support up to PHP_INT_MAX with GMP/BCMath --- PHP/ShortURL.php | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/PHP/ShortURL.php b/PHP/ShortURL.php index 6b03374..2fb0931 100644 --- a/PHP/ShortURL.php +++ b/PHP/ShortURL.php @@ -26,11 +26,33 @@ class ShortURL { const BASE = 51; // strlen(self::ALPHABET) public static function encode($num) { - $str = ''; + static $mode=-1; + if($mode===-1){ + if(is_callable('gmp_div_q')){ + $mode=1; + }elseif(is_callable('bcdiv')){ + $mode=2; + } else { + $mode=0; + } + } + if(!is_int($num) || $num<1 || ($mode===0 && $num>0x7FFFFFFF)){ + throw new InvalidArgumentException('argument 1 MUST be an int between 1 and '.($mode!==0?PHP_INT_MAX:0x7FFFFFFF.' (PS, if you install the GMP or BCMath extensions, the limit will be upgraded to '.PHP_INT_MAX.')')); + } + $str = ''; while ($num > 0) { $str = self::ALPHABET[($num % self::BASE)] . $str; - $num = (int) ($num / self::BASE); + if($num<=0x7FFFFFFF) + { + $num = (int) ($num / self::BASE); + } elseif($mode===1) { + $num=gmp_intval(gmp_div_q($num,self::BASE)); + } elseif($mode===2){ + $num=(int)bcdiv($num,SELF::BASE); + }else { + throw new LogicException('unreachable code reached!'); + } } return $str; From ae27df5e03116d435ec4171eb757c56b5a86e86c Mon Sep 17 00:00:00 2001 From: divinity76 Date: Wed, 2 Nov 2016 11:01:25 +0100 Subject: [PATCH 2/3] 0 scale for bcdiv --- PHP/ShortURL.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PHP/ShortURL.php b/PHP/ShortURL.php index 2fb0931..0a3508a 100644 --- a/PHP/ShortURL.php +++ b/PHP/ShortURL.php @@ -49,7 +49,7 @@ public static function encode($num) { } elseif($mode===1) { $num=gmp_intval(gmp_div_q($num,self::BASE)); } elseif($mode===2){ - $num=(int)bcdiv($num,SELF::BASE); + $num=(int)bcdiv($num,SELF::BASE,0); }else { throw new LogicException('unreachable code reached!'); } From b7111caca50c2c3d750cd81b1b929df9d2aeac4d Mon Sep 17 00:00:00 2001 From: divinity76 Date: Wed, 2 Nov 2016 14:32:51 +0100 Subject: [PATCH 3/3] don't mention upgrade size in 32bit php, because 0x7FFFFFFF is max. --- PHP/ShortURL.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PHP/ShortURL.php b/PHP/ShortURL.php index 0a3508a..0b20bb8 100644 --- a/PHP/ShortURL.php +++ b/PHP/ShortURL.php @@ -37,7 +37,7 @@ public static function encode($num) { } } if(!is_int($num) || $num<1 || ($mode===0 && $num>0x7FFFFFFF)){ - throw new InvalidArgumentException('argument 1 MUST be an int between 1 and '.($mode!==0?PHP_INT_MAX:0x7FFFFFFF.' (PS, if you install the GMP or BCMath extensions, the limit will be upgraded to '.PHP_INT_MAX.')')); + throw new InvalidArgumentException('argument 1 MUST be an int between 1 and '.(($mode!==0 || PHP_INT_MAX===0x7FFFFFFF)?PHP_INT_MAX:0x7FFFFFFF.' (PS, if you install the GMP or BCMath extensions, the limit will be upgraded to '.PHP_INT_MAX.')')); } $str = '';