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 | 1224 | public function __construct(Math $math, ScriptInterface $script) |
|
59 | { |
||
60 | 1224 | $this->math = $math; |
|
61 | 1224 | $buffer = $script->getBuffer(); |
|
62 | 1224 | $this->data = $buffer->getBinary(); |
|
63 | 1224 | $this->end = $buffer->getSize(); |
|
64 | 1224 | $this->script = $script; |
|
65 | 1224 | $this->empty = new Buffer('', 0, $math); |
|
66 | 1224 | } |
|
67 | |||
68 | /** |
||
69 | * @return int |
||
70 | */ |
||
71 | public function getPosition() |
||
75 | |||
76 | /** |
||
77 | * @param string $packFormat |
||
78 | * @param integer $strSize |
||
79 | * @return array|bool |
||
80 | */ |
||
81 | 60 | private function unpackSize($packFormat, $strSize) |
|
82 | { |
||
83 | 60 | if ($this->end - $this->position < $strSize) { |
|
84 | return false; |
||
85 | } |
||
86 | |||
87 | 60 | $size = unpack($packFormat, substr($this->data, $this->position, $strSize)); |
|
88 | 60 | $size = $size[1]; |
|
89 | 60 | $this->position += $strSize; |
|
90 | |||
91 | 60 | return $size; |
|
92 | } |
||
93 | |||
94 | /** |
||
95 | * @param int $ptr |
||
96 | * @return Operation |
||
97 | */ |
||
98 | 1152 | private function doNext($ptr) |
|
99 | { |
||
100 | 1152 | if ($this->math->cmp($this->position, $this->end) >= 0) { |
|
101 | throw new \RuntimeException('Position exceeds end of script!'); |
||
102 | } |
||
103 | |||
104 | 1152 | $opCode = ord($this->data[$this->position++]); |
|
105 | 1152 | $pushData = $this->empty; |
|
106 | 1152 | $dataSize = 0; |
|
107 | |||
108 | 1152 | if ($opCode <= Opcodes::OP_PUSHDATA4) { |
|
109 | 834 | if ($opCode < Opcodes::OP_PUSHDATA1) { |
|
110 | 804 | $dataSize = $opCode; |
|
111 | 834 | } else if ($opCode === Opcodes::OP_PUSHDATA1) { |
|
112 | 30 | $dataSize = $this->unpackSize('C', 1); |
|
113 | 60 | } else if ($opCode === Opcodes::OP_PUSHDATA2) { |
|
114 | 18 | $dataSize = $this->unpackSize('v', 2); |
|
115 | 18 | } else { |
|
116 | 12 | $dataSize = $this->unpackSize('V', 4); |
|
117 | } |
||
118 | |||
119 | 834 | $delta = ($this->end - $this->position); |
|
120 | 834 | if ($dataSize === false || $delta < 0 || $delta < $dataSize) { |
|
121 | 6 | throw new \RuntimeException('Failed to unpack data from Script'); |
|
122 | } |
||
123 | |||
124 | 834 | if ($dataSize > 0) { |
|
125 | 696 | $pushData = new Buffer(substr($this->data, $this->position, $dataSize), $dataSize, $this->math); |
|
126 | 696 | } |
|
127 | |||
128 | 834 | $this->position += $dataSize; |
|
129 | 834 | } |
|
130 | |||
131 | 1152 | $this->array[$ptr] = new Operation($opCode, $pushData, $dataSize); |
|
132 | |||
133 | 1152 | return $this->array[$ptr]; |
|
134 | } |
||
135 | |||
136 | /** |
||
137 | * |
||
138 | */ |
||
139 | 1182 | public function rewind() |
|
143 | |||
144 | /** |
||
145 | * @return Operation |
||
146 | */ |
||
147 | 1152 | public function current() |
|
157 | |||
158 | /** |
||
159 | * @return int |
||
160 | */ |
||
161 | public function key() |
||
165 | |||
166 | /** |
||
167 | * @return Operation |
||
168 | */ |
||
169 | 1170 | public function next() |
|
179 | |||
180 | /** |
||
181 | * @return bool |
||
182 | */ |
||
183 | 1182 | public function valid() |
|
187 | |||
188 | /** |
||
189 | * @return Operation[] |
||
190 | */ |
||
191 | 1080 | public function decode() |
|
200 | |||
201 | /** |
||
202 | * @return string |
||
203 | */ |
||
204 | 702 | public function getHumanReadable() |
|
215 | } |
||
216 |