1 | <?php |
||
11 | class Parser implements \Iterator |
||
12 | { |
||
13 | /** |
||
14 | * @var Math |
||
15 | */ |
||
16 | private $math; |
||
17 | |||
18 | /** |
||
19 | * @var BufferInterface |
||
20 | */ |
||
21 | private $empty; |
||
22 | |||
23 | /** |
||
24 | * @var ScriptInterface |
||
25 | */ |
||
26 | private $script; |
||
27 | |||
28 | /** |
||
29 | * @var int |
||
30 | */ |
||
31 | private $position = 0; |
||
32 | |||
33 | /** |
||
34 | * @var int |
||
35 | */ |
||
36 | private $end = 0; |
||
37 | |||
38 | /** |
||
39 | * @var int |
||
40 | */ |
||
41 | private $execPtr = 0; |
||
42 | |||
43 | /** |
||
44 | * @var string |
||
45 | */ |
||
46 | private $data = ''; |
||
47 | |||
48 | /** |
||
49 | * @var Operation[] |
||
50 | */ |
||
51 | private $array = array(); |
||
52 | |||
53 | /** |
||
54 | * ScriptParser constructor. |
||
55 | * @param Math $math |
||
56 | * @param ScriptInterface $script |
||
57 | */ |
||
58 | 1446 | public function __construct(Math $math, ScriptInterface $script) |
|
67 | |||
68 | /** |
||
69 | * @return int |
||
70 | */ |
||
71 | 1 | public function getPosition() |
|
72 | { |
||
73 | 1 | return $this->position; |
|
74 | } |
||
75 | |||
76 | /** |
||
77 | * @param string $packFormat |
||
78 | * @param integer $strSize |
||
79 | * @return array|bool |
||
80 | */ |
||
81 | 42 | private function unpackSize($packFormat, $strSize) |
|
93 | |||
94 | /** |
||
95 | * @param int $ptr |
||
96 | * @return Operation |
||
97 | */ |
||
98 | 1421 | private function doNext($ptr) |
|
99 | { |
||
100 | 1421 | if ($this->position >= $this->end) { |
|
101 | throw new \RuntimeException('Position exceeds end of script!'); |
||
102 | } |
||
103 | |||
104 | 1421 | $opCode = ord($this->data[$this->position++]); |
|
105 | 1421 | $pushData = $this->empty; |
|
106 | 1421 | $dataSize = 0; |
|
107 | |||
108 | 1421 | if ($opCode <= Opcodes::OP_PUSHDATA4) { |
|
109 | 1119 | if ($opCode < Opcodes::OP_PUSHDATA1) { |
|
110 | 1095 | $dataSize = $opCode; |
|
111 | 42 | } else if ($opCode === Opcodes::OP_PUSHDATA1) { |
|
112 | 22 | $dataSize = $this->unpackSize('C', 1); |
|
113 | 22 | } else if ($opCode === Opcodes::OP_PUSHDATA2) { |
|
114 | 16 | $dataSize = $this->unpackSize('v', 2); |
|
115 | } else { |
||
116 | 6 | $dataSize = $this->unpackSize('V', 4); |
|
117 | } |
||
118 | |||
119 | 1119 | $delta = ($this->end - $this->position); |
|
120 | 1119 | if ($dataSize === false || $delta < 0 || $delta < $dataSize) { |
|
121 | 7 | throw new \RuntimeException('Failed to unpack data from Script'); |
|
122 | } |
||
123 | |||
124 | 1116 | if ($dataSize > 0) { |
|
125 | 868 | $pushData = new Buffer(substr($this->data, $this->position, $dataSize), $dataSize, $this->math); |
|
126 | } |
||
127 | |||
128 | 1116 | $this->position += $dataSize; |
|
129 | } |
||
130 | |||
131 | 1418 | $this->array[$ptr] = new Operation($opCode, $pushData, $dataSize); |
|
132 | |||
133 | 1418 | return $this->array[$ptr]; |
|
134 | } |
||
135 | |||
136 | /** |
||
137 | * |
||
138 | */ |
||
139 | 1432 | public function rewind() |
|
143 | |||
144 | /** |
||
145 | * @return Operation |
||
146 | */ |
||
147 | 1421 | public function current() |
|
148 | { |
||
149 | 1421 | if (isset($this->array[$this->execPtr])) { |
|
150 | $exec = $this->array[$this->execPtr]; |
||
151 | } else { |
||
152 | 1421 | $exec = $this->doNext($this->execPtr); |
|
153 | } |
||
154 | |||
155 | 1418 | return $exec; |
|
156 | } |
||
157 | |||
158 | /** |
||
159 | * @return int |
||
160 | */ |
||
161 | public function key() |
||
165 | |||
166 | /** |
||
167 | * @return Operation |
||
168 | */ |
||
169 | 1394 | public function next() |
|
179 | |||
180 | /** |
||
181 | * @return bool |
||
182 | */ |
||
183 | 1432 | public function valid() |
|
187 | |||
188 | /** |
||
189 | * @return Operation[] |
||
190 | */ |
||
191 | 749 | public function decode() |
|
200 | |||
201 | /** |
||
202 | * @return string |
||
203 | */ |
||
204 | 6 | public function getHumanReadable() |
|
220 | } |
||
221 |