Exemplo n.º 1
0
 /**
  * Converts base-2, base-10, base-16, and binary strings (base-256) to BigIntegers.
  *
  * If the second parameter - $base - is negative, then it will be assumed that the number's are encoded using
  * two's compliment.  The sole exception to this is -10, which is treated the same as 10 is.
  *
  * Here's an example:
  * <code>
  * <?php
  *    $a = new \phpseclib\Math\BigInteger('0x32', 16); // 50 in base-16
  *
  *    echo $a->toString(); // outputs 50
  * ?>
  * </code>
  *
  * @param optional $x base-10 number or base-$base number if $base set.
  * @param optional integer $base
  * @return \phpseclib\Math\BigInteger
  * @access public
  */
 function __construct($x = 0, $base = 10)
 {
     if (!defined('MATH_BIGINTEGER_MODE')) {
         switch (true) {
             case extension_loaded('gmp'):
                 define('MATH_BIGINTEGER_MODE', self::MODE_GMP);
                 break;
             case extension_loaded('bcmath'):
                 define('MATH_BIGINTEGER_MODE', self::MODE_BCMATH);
                 break;
             default:
                 define('MATH_BIGINTEGER_MODE', self::MODE_INTERNAL);
         }
     }
     if (function_exists('openssl_public_encrypt') && !defined('MATH_BIGINTEGER_OPENSSL_DISABLE') && !defined('MATH_BIGINTEGER_OPENSSL_ENABLED')) {
         // some versions of XAMPP have mismatched versions of OpenSSL which causes it not to work
         ob_start();
         @phpinfo();
         $content = ob_get_contents();
         ob_end_clean();
         preg_match_all('#OpenSSL (Header|Library) Version(.*)#im', $content, $matches);
         $versions = array();
         if (!empty($matches[1])) {
             for ($i = 0; $i < count($matches[1]); $i++) {
                 $fullVersion = trim(str_replace('=>', '', strip_tags($matches[2][$i])));
                 // Remove letter part in OpenSSL version
                 if (!preg_match('/(\\d+\\.\\d+\\.\\d+)/i', $fullVersion, $m)) {
                     $versions[$matches[1][$i]] = $fullVersion;
                 } else {
                     $versions[$matches[1][$i]] = $m[0];
                 }
             }
         }
         // it doesn't appear that OpenSSL versions were reported upon until PHP 5.3+
         switch (true) {
             case !isset($versions['Header']):
             case !isset($versions['Library']):
             case $versions['Header'] == $versions['Library']:
                 define('MATH_BIGINTEGER_OPENSSL_ENABLED', true);
                 break;
             default:
                 define('MATH_BIGINTEGER_OPENSSL_DISABLE', true);
         }
     }
     if (!defined('PHP_INT_SIZE')) {
         define('PHP_INT_SIZE', 4);
     }
     if (empty(self::$base) && MATH_BIGINTEGER_MODE == self::MODE_INTERNAL) {
         switch (PHP_INT_SIZE) {
             case 8:
                 // use 64-bit integers if int size is 8 bytes
                 self::$base = 31;
                 self::$baseFull = 0x80000000;
                 self::$maxDigit = 0x7fffffff;
                 self::$msb = 0x40000000;
                 self::$max10 = 1000000000;
                 self::$max10Len = 9;
                 self::$maxDigit2 = pow(2, 62);
                 break;
                 //case 4: // use 64-bit floats if int size is 4 bytes
             //case 4: // use 64-bit floats if int size is 4 bytes
             default:
                 self::$base = 26;
                 self::$baseFull = 0x4000000;
                 self::$maxDigit = 0x3ffffff;
                 self::$msb = 0x2000000;
                 self::$max10 = 10000000;
                 self::$max10Len = 7;
                 self::$maxDigit2 = pow(2, 52);
                 // pow() prevents truncation
                 break;
         }
     }
     switch (MATH_BIGINTEGER_MODE) {
         case self::MODE_GMP:
             switch (true) {
                 case is_resource($x) && get_resource_type($x) == 'GMP integer':
                     // PHP 5.6 switched GMP from using resources to objects
                 // PHP 5.6 switched GMP from using resources to objects
                 case $x instanceof \GMP:
                     $this->value = $x;
                     return;
             }
             $this->value = gmp_init(0);
             break;
         case self::MODE_BCMATH:
             $this->value = '0';
             break;
         default:
             $this->value = array();
     }
     // '0' counts as empty() but when the base is 256 '0' is equal to ord('0') or 48
     // '0' is the only value like this per http://php.net/empty
     if (empty($x) && (abs($base) != 256 || $x !== '0')) {
         return;
     }
     switch ($base) {
         case -256:
             if (ord($x[0]) & 0x80) {
                 $x = ~$x;
                 $this->is_negative = true;
             }
         case 256:
             switch (MATH_BIGINTEGER_MODE) {
                 case self::MODE_GMP:
                     $sign = $this->is_negative ? '-' : '';
                     $this->value = gmp_init($sign . '0x' . bin2hex($x));
                     break;
                 case self::MODE_BCMATH:
                     // round $len to the nearest 4 (thanks, DavidMJ!)
                     $len = strlen($x) + 3 & 0xfffffffc;
                     $x = str_pad($x, $len, chr(0), STR_PAD_LEFT);
                     for ($i = 0; $i < $len; $i += 4) {
                         $this->value = bcmul($this->value, '4294967296', 0);
                         // 4294967296 == 2**32
                         $this->value = bcadd($this->value, 0x1000000 * ord($x[$i]) + (ord($x[$i + 1]) << 16 | ord($x[$i + 2]) << 8 | ord($x[$i + 3])), 0);
                     }
                     if ($this->is_negative) {
                         $this->value = '-' . $this->value;
                     }
                     break;
                     // converts a base-2**8 (big endian / msb) number to base-2**26 (little endian / lsb)
                 // converts a base-2**8 (big endian / msb) number to base-2**26 (little endian / lsb)
                 default:
                     while (strlen($x)) {
                         $this->value[] = $this->_bytes2int($this->_base256_rshift($x, self::$base));
                     }
             }
             if ($this->is_negative) {
                 if (MATH_BIGINTEGER_MODE != self::MODE_INTERNAL) {
                     $this->is_negative = false;
                 }
                 $temp = $this->add(new static('-1'));
                 $this->value = $temp->value;
             }
             break;
         case 16:
         case -16:
             if ($base > 0 && $x[0] == '-') {
                 $this->is_negative = true;
                 $x = substr($x, 1);
             }
             $x = preg_replace('#^(?:0x)?([A-Fa-f0-9]*).*#', '$1', $x);
             $is_negative = false;
             if ($base < 0 && hexdec($x[0]) >= 8) {
                 $this->is_negative = $is_negative = true;
                 $x = bin2hex(~pack('H*', $x));
             }
             switch (MATH_BIGINTEGER_MODE) {
                 case self::MODE_GMP:
                     $temp = $this->is_negative ? '-0x' . $x : '0x' . $x;
                     $this->value = gmp_init($temp);
                     $this->is_negative = false;
                     break;
                 case self::MODE_BCMATH:
                     $x = strlen($x) & 1 ? '0' . $x : $x;
                     $temp = new static(pack('H*', $x), 256);
                     $this->value = $this->is_negative ? '-' . $temp->value : $temp->value;
                     $this->is_negative = false;
                     break;
                 default:
                     $x = strlen($x) & 1 ? '0' . $x : $x;
                     $temp = new static(pack('H*', $x), 256);
                     $this->value = $temp->value;
             }
             if ($is_negative) {
                 $temp = $this->add(new static('-1'));
                 $this->value = $temp->value;
             }
             break;
         case 10:
         case -10:
             // (?<!^)(?:-).*: find any -'s that aren't at the beginning and then any characters that follow that
             // (?<=^|-)0*: find any 0's that are preceded by the start of the string or by a - (ie. octals)
             // [^-0-9].*: find any non-numeric characters and then any characters that follow that
             $x = preg_replace('#(?<!^)(?:-).*|(?<=^|-)0*|[^-0-9].*#', '', $x);
             switch (MATH_BIGINTEGER_MODE) {
                 case self::MODE_GMP:
                     $this->value = gmp_init($x);
                     break;
                 case self::MODE_BCMATH:
                     // explicitly casting $x to a string is necessary, here, since doing $x[0] on -1 yields different
                     // results then doing it on '-1' does (modInverse does $x[0])
                     $this->value = $x === '-' ? '0' : (string) $x;
                     break;
                 default:
                     $temp = new static();
                     $multiplier = new static();
                     $multiplier->value = array(self::$max10);
                     if ($x[0] == '-') {
                         $this->is_negative = true;
                         $x = substr($x, 1);
                     }
                     $x = str_pad($x, strlen($x) + (self::$max10Len - 1) * strlen($x) % self::$max10Len, 0, STR_PAD_LEFT);
                     while (strlen($x)) {
                         $temp = $temp->multiply($multiplier);
                         $temp = $temp->add(new static($this->_int2bytes(substr($x, 0, self::$max10Len)), 256));
                         $x = substr($x, self::$max10Len);
                     }
                     $this->value = $temp->value;
             }
             break;
         case 2:
             // base-2 support originally implemented by Lluis Pamies - thanks!
         // base-2 support originally implemented by Lluis Pamies - thanks!
         case -2:
             if ($base > 0 && $x[0] == '-') {
                 $this->is_negative = true;
                 $x = substr($x, 1);
             }
             $x = preg_replace('#^([01]*).*#', '$1', $x);
             $x = str_pad($x, strlen($x) + 3 * strlen($x) % 4, 0, STR_PAD_LEFT);
             $str = '0x';
             while (strlen($x)) {
                 $part = substr($x, 0, 4);
                 $str .= dechex(bindec($part));
                 $x = substr($x, 4);
             }
             if ($this->is_negative) {
                 $str = '-' . $str;
             }
             $temp = new static($str, 8 * $base);
             // ie. either -16 or +16
             $this->value = $temp->value;
             $this->is_negative = $temp->is_negative;
             break;
         default:
             // base not supported, so we'll let $this == 0
     }
 }