| 1 |
<?php |
| 2 |
/* |
| 3 |
* This file is part of the PHPASN1 library. |
| 4 |
* |
| 5 |
* For the full copyright and license information, please view the LICENSE |
| 6 |
* file that was distributed with this source code. |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace WP_DEFENDER_VENDOR\FG\Utility; |
| 10 |
|
| 11 |
/** |
| 12 |
* Class BigIntegerBcmath |
| 13 |
* Integer representation of big numbers using the bcmath library to perform large operations. |
| 14 |
* @package WP_DEFENDER_VENDOR\FG\Utility |
| 15 |
* @internal |
| 16 |
*/ |
| 17 |
class BigIntegerBcmath extends BigInteger |
| 18 |
{ |
| 19 |
protected $_str; |
| 20 |
|
| 21 |
public function __clone() |
| 22 |
{ |
| 23 |
// nothing needed to copy |
| 24 |
} |
| 25 |
|
| 26 |
protected function _fromString($str) |
| 27 |
{ |
| 28 |
$this->_str = (string)$str; |
| 29 |
} |
| 30 |
|
| 31 |
protected function _fromInteger($integer) |
| 32 |
{ |
| 33 |
$this->_str = (string)$integer; |
| 34 |
} |
| 35 |
|
| 36 |
public function __toString() |
| 37 |
{ |
| 38 |
return $this->_str; |
| 39 |
} |
| 40 |
|
| 41 |
public function toInteger() |
| 42 |
{ |
| 43 |
if ($this->compare(PHP_INT_MAX) > 0 || $this->compare(PHP_INT_MIN) < 0) { |
| 44 |
throw new \OverflowException(sprintf('Can not represent %s as integer.', $this->_str)); |
| 45 |
} |
| 46 |
return (int)$this->_str; |
| 47 |
} |
| 48 |
|
| 49 |
public function isNegative() |
| 50 |
{ |
| 51 |
return bccomp($this->_str, '0', 0) < 0; |
| 52 |
} |
| 53 |
|
| 54 |
protected function _unwrap($number) |
| 55 |
{ |
| 56 |
if ($number instanceof self) { |
| 57 |
return $number->_str; |
| 58 |
} |
| 59 |
return $number; |
| 60 |
} |
| 61 |
|
| 62 |
public function compare($number) |
| 63 |
{ |
| 64 |
return bccomp($this->_str, $this->_unwrap($number), 0); |
| 65 |
} |
| 66 |
|
| 67 |
public function add($b) |
| 68 |
{ |
| 69 |
$ret = new self(); |
| 70 |
$ret->_str = bcadd($this->_str, $this->_unwrap($b), 0); |
| 71 |
return $ret; |
| 72 |
} |
| 73 |
|
| 74 |
public function subtract($b) |
| 75 |
{ |
| 76 |
$ret = new self(); |
| 77 |
$ret->_str = bcsub($this->_str, $this->_unwrap($b), 0); |
| 78 |
return $ret; |
| 79 |
} |
| 80 |
|
| 81 |
public function multiply($b) |
| 82 |
{ |
| 83 |
$ret = new self(); |
| 84 |
$ret->_str = bcmul($this->_str, $this->_unwrap($b), 0); |
| 85 |
return $ret; |
| 86 |
} |
| 87 |
|
| 88 |
public function modulus($b) |
| 89 |
{ |
| 90 |
$ret = new self(); |
| 91 |
if ($this->isNegative()) { |
| 92 |
// bcmod handles negative numbers differently |
| 93 |
$b = $this->_unwrap($b); |
| 94 |
$ret->_str = bcsub($b, bcmod(bcsub('0', $this->_str, 0), $b), 0); |
| 95 |
} |
| 96 |
else { |
| 97 |
$ret->_str = bcmod($this->_str, $this->_unwrap($b)); |
| 98 |
} |
| 99 |
return $ret; |
| 100 |
} |
| 101 |
|
| 102 |
public function toPower($b) |
| 103 |
{ |
| 104 |
$ret = new self(); |
| 105 |
$ret->_str = bcpow($this->_str, $this->_unwrap($b), 0); |
| 106 |
return $ret; |
| 107 |
} |
| 108 |
|
| 109 |
public function shiftRight($bits = 8) |
| 110 |
{ |
| 111 |
$ret = new self(); |
| 112 |
$ret->_str = bcdiv($this->_str, bcpow('2', $bits)); |
| 113 |
return $ret; |
| 114 |
} |
| 115 |
|
| 116 |
public function shiftLeft($bits = 8) { |
| 117 |
$ret = new self(); |
| 118 |
$ret->_str = bcmul($this->_str, bcpow('2', $bits)); |
| 119 |
return $ret; |
| 120 |
} |
| 121 |
|
| 122 |
public function absoluteValue() |
| 123 |
{ |
| 124 |
$ret = new self(); |
| 125 |
if (-1 === bccomp($this->_str, '0', 0)) { |
| 126 |
$ret->_str = bcsub('0', $this->_str, 0); |
| 127 |
} |
| 128 |
else { |
| 129 |
$ret->_str = $this->_str; |
| 130 |
} |
| 131 |
return $ret; |
| 132 |
} |
| 133 |
} |
| 134 |
|