1 | <?php |
||
11 | class Number extends Serializable |
||
12 | { |
||
13 | const MAX_NUM_SIZE = 4; |
||
14 | const MAX = 2**31-1; |
||
15 | const MIN = -2**31+1; |
||
16 | |||
17 | /** |
||
18 | * @var Math |
||
19 | */ |
||
20 | private $math; |
||
21 | |||
22 | /** |
||
23 | * @var int |
||
24 | */ |
||
25 | private $number; |
||
26 | |||
27 | /** |
||
28 | * Number constructor. |
||
29 | * @param int $number |
||
30 | * @param Math $math |
||
31 | */ |
||
32 | 907 | public function __construct($number, Math $math) |
|
37 | |||
38 | /** |
||
39 | * @param int $number |
||
40 | * @param Math|null $math |
||
41 | * @return self |
||
42 | */ |
||
43 | 824 | public static function int($number, Math $math = null) |
|
50 | |||
51 | /** |
||
52 | * @param \GMP $number |
||
53 | * @param Math|null $math |
||
54 | * @return self |
||
55 | */ |
||
56 | public static function gmp(\GMP $number, Math $math = null) |
||
63 | |||
64 | /** |
||
65 | * @param BufferInterface $vch |
||
66 | * @param bool $fRequireMinimal |
||
67 | * @param int $maxNumSize |
||
68 | * @param Math|null $math |
||
69 | * @return self |
||
70 | */ |
||
71 | 671 | public static function buffer(BufferInterface $vch, $fRequireMinimal, $maxNumSize = self::MAX_NUM_SIZE, Math $math = null) |
|
72 | { |
||
73 | 671 | $size = $vch->getSize(); |
|
74 | 671 | if ($size > $maxNumSize) { |
|
75 | 11 | throw new \RuntimeException('Script number overflow'); |
|
76 | } |
||
77 | |||
78 | 662 | if ($fRequireMinimal && $size > 0) { |
|
79 | 55 | $binary = $vch->getBinary(); |
|
80 | //$chars = array_values(unpack("C*", $binary)); |
||
81 | 55 | if ((ord($binary[$size - 1]) & 0x7f) == 0) { |
|
82 | 55 | if ($size <= 1 || (ord($binary[$size - 2]) & 0x80) == 0) { |
|
83 | 55 | throw new \RuntimeException('Non-minimally encoded script number'); |
|
84 | } |
||
85 | } |
||
86 | } |
||
87 | |||
88 | 625 | $math = $math ?: Bitcoin::getMath(); |
|
89 | 625 | $number = new self(0, $math); |
|
90 | 625 | $number->number = $number->parseBuffer($vch); |
|
|
|||
91 | 625 | return $number; |
|
92 | } |
||
93 | |||
94 | /** |
||
95 | * @param BufferInterface $buffer |
||
96 | * @return int |
||
97 | */ |
||
98 | 625 | private function parseBuffer(BufferInterface $buffer) |
|
121 | |||
122 | /** |
||
123 | * @return BufferInterface |
||
124 | */ |
||
125 | 841 | private function serialize() |
|
126 | { |
||
127 | 841 | if ($this->number == 0) { |
|
128 | 267 | return new Buffer('', 0); |
|
129 | } |
||
130 | |||
131 | 785 | $zero = gmp_init(0); |
|
132 | // Using array of integers instead of bytes |
||
133 | 785 | $result = []; |
|
134 | 785 | $negative = $this->math->cmp(gmp_init($this->number), $zero) < 0; |
|
135 | 785 | $abs = $negative ? $this->math->sub($zero, gmp_init($this->number, 10)) : gmp_init($this->number, 10); |
|
136 | 785 | $mask = gmp_init(0xff); |
|
137 | 785 | while ($this->math->cmp($abs, $zero) > 0) { |
|
138 | 785 | $result[] = (int) gmp_strval($this->math->bitwiseAnd($abs, $mask), 10); |
|
139 | 785 | $abs = $this->math->rightShift($abs, 8); |
|
140 | 24 | } |
|
141 | |||
142 | 785 | if ($result[count($result) - 1] & 0x80) { |
|
143 | 12 | $result[] = $negative ? 0x80 : 0; |
|
144 | 777 | } else if ($negative) { |
|
145 | 21 | $result[count($result) - 1] |= 0x80; |
|
146 | 4 | } |
|
147 | |||
148 | 785 | $s = ''; |
|
149 | 785 | foreach ($result as $i) { |
|
150 | 785 | $s .= chr($i); |
|
151 | 24 | } |
|
152 | |||
153 | 785 | return new Buffer($s, null, $this->math); |
|
154 | } |
||
155 | |||
156 | /** |
||
157 | * @return BufferInterface |
||
158 | */ |
||
159 | 841 | public function getBuffer() |
|
163 | |||
164 | /** |
||
165 | * @return int |
||
166 | */ |
||
167 | 161 | public function getInt() |
|
177 | |||
178 | /** |
||
179 | * @return \GMP |
||
180 | */ |
||
181 | 234 | public function getGmp() |
|
185 | } |
||
186 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.