1 | <?php |
---|
2 | |
---|
3 | define('INDENT', "\t"); |
---|
4 | ini_set('error_reporting', E_ALL); |
---|
5 | |
---|
6 | function color($str, $color = 33) |
---|
7 | { |
---|
8 | return "\x1B[{$color}m$str\x1B[0m"; |
---|
9 | } |
---|
10 | |
---|
11 | function toCode($src, $indent = '') // {{{ |
---|
12 | { |
---|
13 | if (is_array($indent)) { |
---|
14 | $indent = $indent['indent']; |
---|
15 | } |
---|
16 | |
---|
17 | if (is_object($src)) { |
---|
18 | if (!method_exists($src, 'toCode')) { |
---|
19 | var_dump($src); |
---|
20 | exit('no toCode'); |
---|
21 | } |
---|
22 | return $src->toCode($indent); |
---|
23 | } |
---|
24 | |
---|
25 | return $src; |
---|
26 | } |
---|
27 | // }}} |
---|
28 | function value($value) // {{{ |
---|
29 | { |
---|
30 | $spec = xcache_get_special_value($value); |
---|
31 | if (isset($spec)) { |
---|
32 | $value = $spec; |
---|
33 | if (!is_array($value)) { |
---|
34 | // constant |
---|
35 | return $value; |
---|
36 | } |
---|
37 | } |
---|
38 | |
---|
39 | if ($value instanceof Decompiler_Object) { |
---|
40 | // use as is |
---|
41 | } |
---|
42 | else if (is_array($value)) { |
---|
43 | $value = new Decompiler_Array($value); |
---|
44 | } |
---|
45 | else { |
---|
46 | $value = new Decompiler_Value($value); |
---|
47 | } |
---|
48 | return $value; |
---|
49 | } |
---|
50 | // }}} |
---|
51 | class Decompiler_Object // {{{ |
---|
52 | { |
---|
53 | } |
---|
54 | // }}} |
---|
55 | class Decompiler_Value extends Decompiler_Object // {{{ |
---|
56 | { |
---|
57 | var $value; |
---|
58 | |
---|
59 | function Decompiler_Value($value = null) |
---|
60 | { |
---|
61 | $this->value = $value; |
---|
62 | } |
---|
63 | |
---|
64 | function toCode($indent) |
---|
65 | { |
---|
66 | return var_export($this->value, true); |
---|
67 | } |
---|
68 | } |
---|
69 | // }}} |
---|
70 | class Decompiler_Code extends Decompiler_Object // {{{ |
---|
71 | { |
---|
72 | var $src; |
---|
73 | |
---|
74 | function Decompiler_Code($src) |
---|
75 | { |
---|
76 | $this->src = $src; |
---|
77 | } |
---|
78 | |
---|
79 | function toCode($indent) |
---|
80 | { |
---|
81 | return $this->src; |
---|
82 | } |
---|
83 | } |
---|
84 | // }}} |
---|
85 | class Decompiler_Binop extends Decompiler_Code // {{{ |
---|
86 | { |
---|
87 | var $opc; |
---|
88 | var $op1; |
---|
89 | var $op2; |
---|
90 | var $parent; |
---|
91 | var $indent; |
---|
92 | |
---|
93 | function Decompiler_Binop($parent, $op1, $opc, $op2) |
---|
94 | { |
---|
95 | $this->parent = &$parent; |
---|
96 | $this->opc = $opc; |
---|
97 | $this->op1 = $op1; |
---|
98 | $this->op2 = $op2; |
---|
99 | } |
---|
100 | |
---|
101 | function toCode($indent) |
---|
102 | { |
---|
103 | $op1 = toCode($this->op1, $indent); |
---|
104 | if (is_a($this->op1, 'Decompiler_Binop') && $this->op1->opc != $this->opc) { |
---|
105 | $op1 = "($op1)"; |
---|
106 | } |
---|
107 | $opstr = $this->parent->binops[$this->opc]; |
---|
108 | if ($op1 == '0' && $this->opc == XC_SUB) { |
---|
109 | return $opstr . toCode($this->op2, $indent); |
---|
110 | } |
---|
111 | return $op1 . ' ' . $opstr . ' ' . toCode($this->op2, $indent); |
---|
112 | } |
---|
113 | } |
---|
114 | // }}} |
---|
115 | class Decompiler_Fetch extends Decompiler_Code // {{{ |
---|
116 | { |
---|
117 | var $src; |
---|
118 | var $fetchType; |
---|
119 | |
---|
120 | function Decompiler_Fetch($src, $type, $globalsrc) |
---|
121 | { |
---|
122 | $this->src = $src; |
---|
123 | $this->fetchType = $type; |
---|
124 | $this->globalsrc = $globalsrc; |
---|
125 | } |
---|
126 | |
---|
127 | function toCode($indent) |
---|
128 | { |
---|
129 | switch ($this->fetchType) { |
---|
130 | case ZEND_FETCH_LOCAL: |
---|
131 | return '$' . substr($this->src, 1, -1); |
---|
132 | case ZEND_FETCH_STATIC: |
---|
133 | die('static fetch cant to string'); |
---|
134 | case ZEND_FETCH_GLOBAL: |
---|
135 | case ZEND_FETCH_GLOBAL_LOCK: |
---|
136 | return $this->globalsrc; |
---|
137 | default: |
---|
138 | var_dump($this->fetchType); |
---|
139 | assert(0); |
---|
140 | } |
---|
141 | } |
---|
142 | } |
---|
143 | // }}} |
---|
144 | class Decompiler_Box // {{{ |
---|
145 | { |
---|
146 | var $obj; |
---|
147 | |
---|
148 | function Decompiler_Box(&$obj) |
---|
149 | { |
---|
150 | $this->obj = &$obj; |
---|
151 | } |
---|
152 | |
---|
153 | function toCode($indent) |
---|
154 | { |
---|
155 | return $this->obj->toCode($indent); |
---|
156 | } |
---|
157 | } |
---|
158 | // }}} |
---|
159 | class Decompiler_Dim extends Decompiler_Value // {{{ |
---|
160 | { |
---|
161 | var $offsets = array(); |
---|
162 | var $isLast = false; |
---|
163 | var $assign = null; |
---|
164 | |
---|
165 | function toCode($indent) |
---|
166 | { |
---|
167 | if (is_a($this->value, 'Decompiler_ListBox')) { |
---|
168 | $exp = toCode($this->value->obj->src, $indent); |
---|
169 | } |
---|
170 | else { |
---|
171 | $exp = toCode($this->value, $indent); |
---|
172 | } |
---|
173 | foreach ($this->offsets as $dim) { |
---|
174 | $exp .= '[' . toCode($dim, $indent) . ']'; |
---|
175 | } |
---|
176 | return $exp; |
---|
177 | } |
---|
178 | } |
---|
179 | // }}} |
---|
180 | class Decompiler_DimBox extends Decompiler_Box // {{{ |
---|
181 | { |
---|
182 | } |
---|
183 | // }}} |
---|
184 | class Decompiler_List extends Decompiler_Code // {{{ |
---|
185 | { |
---|
186 | var $src; |
---|
187 | var $dims = array(); |
---|
188 | var $everLocked = false; |
---|
189 | |
---|
190 | function toCode($indent) |
---|
191 | { |
---|
192 | if (count($this->dims) == 1 && !$this->everLocked) { |
---|
193 | $dim = $this->dims[0]; |
---|
194 | unset($dim->value); |
---|
195 | $dim->value = $this->src; |
---|
196 | if (!isset($dim->assign)) { |
---|
197 | return toCode($dim, $indent); |
---|
198 | } |
---|
199 | return toCode($this->dims[0]->assign, $indent) . ' = ' . toCode($dim, $indent); |
---|
200 | } |
---|
201 | /* flatten dims */ |
---|
202 | $assigns = array(); |
---|
203 | foreach ($this->dims as $dim) { |
---|
204 | $assign = &$assigns; |
---|
205 | foreach ($dim->offsets as $offset) { |
---|
206 | $assign = &$assign[$offset]; |
---|
207 | } |
---|
208 | $assign = toCode($dim->assign, $indent); |
---|
209 | } |
---|
210 | return $this->toList($assigns) . ' = ' . toCode($this->src, $indent); |
---|
211 | } |
---|
212 | |
---|
213 | function toList($assigns) |
---|
214 | { |
---|
215 | $keys = array_keys($assigns); |
---|
216 | if (count($keys) < 2) { |
---|
217 | $keys[] = 0; |
---|
218 | } |
---|
219 | $max = call_user_func_array('max', $keys); |
---|
220 | $list = 'list('; |
---|
221 | for ($i = 0; $i <= $max; $i ++) { |
---|
222 | if ($i) { |
---|
223 | $list .= ', '; |
---|
224 | } |
---|
225 | if (!isset($assigns[$i])) { |
---|
226 | continue; |
---|
227 | } |
---|
228 | if (is_array($assigns[$i])) { |
---|
229 | $list .= $this->toList($assigns[$i]); |
---|
230 | } |
---|
231 | else { |
---|
232 | $list .= $assigns[$i]; |
---|
233 | } |
---|
234 | } |
---|
235 | return $list . ')'; |
---|
236 | } |
---|
237 | } |
---|
238 | // }}} |
---|
239 | class Decompiler_ListBox extends Decompiler_Box // {{{ |
---|
240 | { |
---|
241 | } |
---|
242 | // }}} |
---|
243 | class Decompiler_Array extends Decompiler_Value // {{{ |
---|
244 | { |
---|
245 | function Decompiler_Array($value = array()) |
---|
246 | { |
---|
247 | $this->value = $value; |
---|
248 | } |
---|
249 | |
---|
250 | function toCode($indent) |
---|
251 | { |
---|
252 | $exp = "array("; |
---|
253 | $indent = $indent . INDENT; |
---|
254 | $assocWidth = 0; |
---|
255 | $multiline = 0; |
---|
256 | $i = 0; |
---|
257 | foreach ($this->value as $k => $v) { |
---|
258 | if ($i !== $k) { |
---|
259 | $assocWidth = 1; |
---|
260 | } |
---|
261 | ++$i; |
---|
262 | } |
---|
263 | foreach ($this->value as $k => $v) { |
---|
264 | if ($assocWidth) { |
---|
265 | $len = strlen($k); |
---|
266 | if ($assocWidth < $len) { |
---|
267 | $assocWidth = $len; |
---|
268 | } |
---|
269 | } |
---|
270 | $spec = xcache_get_special_value($v); |
---|
271 | if (is_array(isset($spec) ? $spec : $v)) { |
---|
272 | $multiline ++; |
---|
273 | } |
---|
274 | } |
---|
275 | if ($assocWidth) { |
---|
276 | $assocWidth += 2; |
---|
277 | } |
---|
278 | |
---|
279 | $i = 0; |
---|
280 | $subindent = $indent . INDENT; |
---|
281 | foreach ($this->value as $k => $v) { |
---|
282 | if ($multiline) { |
---|
283 | if ($i) { |
---|
284 | $exp .= ","; |
---|
285 | } |
---|
286 | $exp .= "\n"; |
---|
287 | $exp .= $indent; |
---|
288 | } |
---|
289 | else { |
---|
290 | if ($i) { |
---|
291 | $exp .= ", "; |
---|
292 | } |
---|
293 | } |
---|
294 | |
---|
295 | $k = var_export($k, true); |
---|
296 | if ($assocWidth) { |
---|
297 | if ($multiline) { |
---|
298 | $exp .= sprintf("%{$assocWidth}s => ", $k); |
---|
299 | } |
---|
300 | else { |
---|
301 | $exp .= $k . ' => '; |
---|
302 | } |
---|
303 | } |
---|
304 | |
---|
305 | $exp .= toCode(value($v), $subindent); |
---|
306 | |
---|
307 | $i ++; |
---|
308 | } |
---|
309 | if ($multiline) { |
---|
310 | $exp .= "\n$indent)"; |
---|
311 | } |
---|
312 | else { |
---|
313 | $exp .= ")"; |
---|
314 | } |
---|
315 | return $exp; |
---|
316 | } |
---|
317 | } |
---|
318 | // }}} |
---|
319 | class Decompiler_ForeachBox extends Decompiler_Box // {{{ |
---|
320 | { |
---|
321 | var $iskey; |
---|
322 | |
---|
323 | function toCode($indent) |
---|
324 | { |
---|
325 | return 'foreach (' . ''; |
---|
326 | } |
---|
327 | } |
---|
328 | // }}} |
---|
329 | |
---|
330 | class Decompiler |
---|
331 | { |
---|
332 | var $rName = '!^[\\w_][\\w\\d_]*$!'; |
---|
333 | var $rQuotedName = "!^'[\\w_][\\w\\d_]*'\$!"; |
---|
334 | |
---|
335 | function Decompiler() |
---|
336 | { |
---|
337 | // {{{ opinfo |
---|
338 | $this->unaryops = array( |
---|
339 | XC_BW_NOT => '~', |
---|
340 | XC_BOOL_NOT => '!', |
---|
341 | ); |
---|
342 | $this->binops = array( |
---|
343 | XC_ADD => "+", |
---|
344 | XC_ASSIGN_ADD => "+=", |
---|
345 | XC_SUB => "-", |
---|
346 | XC_ASSIGN_SUB => "-=", |
---|
347 | XC_MUL => "*", |
---|
348 | XC_ASSIGN_MUL => "*=", |
---|
349 | XC_DIV => "/", |
---|
350 | XC_ASSIGN_DIV => "/=", |
---|
351 | XC_MOD => "%", |
---|
352 | XC_ASSIGN_MOD => "%=", |
---|
353 | XC_SL => "<<", |
---|
354 | XC_ASSIGN_SL => "<<=", |
---|
355 | XC_SR => ">>", |
---|
356 | XC_ASSIGN_SR => ">>=", |
---|
357 | XC_CONCAT => ".", |
---|
358 | XC_ASSIGN_CONCAT => ".=", |
---|
359 | XC_IS_IDENTICAL => "===", |
---|
360 | XC_IS_NOT_IDENTICAL => "!==", |
---|
361 | XC_IS_EQUAL => "==", |
---|
362 | XC_IS_NOT_EQUAL => "!=", |
---|
363 | XC_IS_SMALLER => "<", |
---|
364 | XC_IS_SMALLER_OR_EQUAL => "<=", |
---|
365 | XC_BW_OR => "|", |
---|
366 | XC_ASSIGN_BW_OR => "|=", |
---|
367 | XC_BW_AND => "&", |
---|
368 | XC_ASSIGN_BW_AND => "&=", |
---|
369 | XC_BW_XOR => "^", |
---|
370 | XC_ASSIGN_BW_XOR => "^=", |
---|
371 | XC_BOOL_XOR => "xor", |
---|
372 | ); |
---|
373 | // }}} |
---|
374 | $this->includeTypes = array( // {{{ |
---|
375 | ZEND_EVAL => 'eval', |
---|
376 | ZEND_INCLUDE => 'include', |
---|
377 | ZEND_INCLUDE_ONCE => 'include_once', |
---|
378 | ZEND_REQUIRE => 'require', |
---|
379 | ZEND_REQUIRE_ONCE => 'require_once', |
---|
380 | ); |
---|
381 | // }}} |
---|
382 | } |
---|
383 | function outputPhp(&$opcodes, $opline, $last, $indent) // {{{ |
---|
384 | { |
---|
385 | $origindent = $indent; |
---|
386 | $curticks = 0; |
---|
387 | for ($i = $opline; $i <= $last; $i ++) { |
---|
388 | $op = $opcodes[$i]; |
---|
389 | if (isset($op['php'])) { |
---|
390 | $toticks = isset($op['ticks']) ? $op['ticks'] : 0; |
---|
391 | if ($curticks != $toticks) { |
---|
392 | if (!$toticks) { |
---|
393 | echo $origindent, "}\n"; |
---|
394 | $indent = $origindent; |
---|
395 | } |
---|
396 | else { |
---|
397 | if ($curticks) { |
---|
398 | echo $origindent, "}\n"; |
---|
399 | } |
---|
400 | else if (!$curticks) { |
---|
401 | $indent .= INDENT; |
---|
402 | } |
---|
403 | echo $origindent, "declare(ticks=$curticks) {\n"; |
---|
404 | } |
---|
405 | $curticks = $toticks; |
---|
406 | } |
---|
407 | echo $indent, toCode($op['php'], $indent), ";\n"; |
---|
408 | } |
---|
409 | } |
---|
410 | if ($curticks) { |
---|
411 | echo $origindent, "}\n"; |
---|
412 | } |
---|
413 | } |
---|
414 | // }}} |
---|
415 | function getOpVal($op, &$EX, $tostr = true, $free = false) // {{{ |
---|
416 | { |
---|
417 | switch ($op['op_type']) { |
---|
418 | case XC_IS_CONST: |
---|
419 | return toCode(value($op['constant']), $EX); |
---|
420 | |
---|
421 | case XC_IS_VAR: |
---|
422 | case XC_IS_TMP_VAR: |
---|
423 | $T = &$EX['Ts']; |
---|
424 | $ret = $T[$op['var']]; |
---|
425 | if ($tostr) { |
---|
426 | $ret = toCode($ret, $EX); |
---|
427 | } |
---|
428 | if ($free) { |
---|
429 | unset($T[$op['var']]); |
---|
430 | } |
---|
431 | return $ret; |
---|
432 | |
---|
433 | case XC_IS_CV: |
---|
434 | $var = $op['var']; |
---|
435 | $var = $EX['op_array']['vars'][$var]; |
---|
436 | return '$' . $var['name']; |
---|
437 | |
---|
438 | case XC_IS_UNUSED: |
---|
439 | return null; |
---|
440 | } |
---|
441 | } |
---|
442 | // }}} |
---|
443 | function removeKeyPrefix($array, $prefix) // {{{ |
---|
444 | { |
---|
445 | $prefixLen = strlen($prefix); |
---|
446 | $ret = array(); |
---|
447 | foreach ($array as $key => $value) { |
---|
448 | if (substr($key, 0, $prefixLen) == $prefix) { |
---|
449 | $key = substr($key, $prefixLen); |
---|
450 | } |
---|
451 | $ret[$key] = $value; |
---|
452 | } |
---|
453 | return $ret; |
---|
454 | } |
---|
455 | // }}} |
---|
456 | function &fixOpcode($opcodes, $removeTailing = false, $defaultReturnValue = null) // {{{ |
---|
457 | { |
---|
458 | for ($i = 0, $cnt = count($opcodes); $i < $cnt; $i ++) { |
---|
459 | if (function_exists('xcache_get_fixed_opcode')) { |
---|
460 | $opcodes[$i]['opcode'] = xcache_get_fixed_opcode($opcodes[$i]['opcode'], $i); |
---|
461 | } |
---|
462 | if (isset($opcodes[$i]['op1'])) { |
---|
463 | $opcodes[$i]['op1'] = $this->removeKeyPrefix($opcodes[$i]['op1'], 'u.'); |
---|
464 | $opcodes[$i]['op2'] = $this->removeKeyPrefix($opcodes[$i]['op2'], 'u.'); |
---|
465 | $opcodes[$i]['result'] = $this->removeKeyPrefix($opcodes[$i]['result'], 'u.'); |
---|
466 | } |
---|
467 | else { |
---|
468 | $op = array( |
---|
469 | 'op1' => array(), |
---|
470 | 'op2' => array(), |
---|
471 | 'op3' => array(), |
---|
472 | ); |
---|
473 | foreach ($opcodes[$i] as $name => $value) { |
---|
474 | if (preg_match('!^(op1|op2|result)\\.(.*)!', $name, $m)) { |
---|
475 | list(, $which, $field) = $m; |
---|
476 | $op[$which][$field] = $value; |
---|
477 | } |
---|
478 | else if (preg_match('!^(op1|op2|result)_type$!', $name, $m)) { |
---|
479 | list(, $which) = $m; |
---|
480 | $op[$which]['op_type'] = $value; |
---|
481 | } |
---|
482 | else { |
---|
483 | $op[$name] = $value; |
---|
484 | } |
---|
485 | } |
---|
486 | $opcodes[$i] = $op; |
---|
487 | } |
---|
488 | } |
---|
489 | |
---|
490 | if ($removeTailing) { |
---|
491 | $last = count($opcodes) - 1; |
---|
492 | if ($opcodes[$last]['opcode'] == XC_HANDLE_EXCEPTION) { |
---|
493 | unset($opcodes[$last]); |
---|
494 | --$last; |
---|
495 | } |
---|
496 | if ($opcodes[$last]['opcode'] == XC_RETURN) { |
---|
497 | $op1 = $opcodes[$last]['op1']; |
---|
498 | if ($op1['op_type'] == XC_IS_CONST && array_key_exists('constant', $op1) && $op1['constant'] === $defaultReturnValue) { |
---|
499 | unset($opcodes[$last]); |
---|
500 | --$last; |
---|
501 | } |
---|
502 | } |
---|
503 | } |
---|
504 | return $opcodes; |
---|
505 | } |
---|
506 | // }}} |
---|
507 | function &dop_array($op_array, $indent = '') // {{{ |
---|
508 | { |
---|
509 | $op_array['opcodes'] = $this->fixOpcode($op_array['opcodes'], true, $indent == '' ? 1 : null); |
---|
510 | $opcodes = &$op_array['opcodes']; |
---|
511 | $EX['indent'] = ''; |
---|
512 | // {{{ build jmp array |
---|
513 | for ($i = 0, $cnt = count($opcodes); $i < $cnt; $i ++) { |
---|
514 | $op = &$opcodes[$i]; |
---|
515 | /* |
---|
516 | if ($op['opcode'] == XC_JMPZ) { |
---|
517 | $this->dumpop($op, $EX); |
---|
518 | var_dump($op); |
---|
519 | } |
---|
520 | continue; |
---|
521 | */ |
---|
522 | $op['line'] = $i; |
---|
523 | switch ($op['opcode']) { |
---|
524 | case XC_JMP: |
---|
525 | $target = $op['op1']['var']; |
---|
526 | $op['jmpouts'] = array($target); |
---|
527 | $opcodes[$target]['jmpins'][] = $i; |
---|
528 | break; |
---|
529 | |
---|
530 | case XC_JMPZNZ: |
---|
531 | $jmpz = $op['op2']['opline_num']; |
---|
532 | $jmpnz = $op['extended_value']; |
---|
533 | $op['jmpouts'] = array($jmpz, $jmpnz); |
---|
534 | $opcodes[$jmpz]['jmpins'][] = $i; |
---|
535 | $opcodes[$jmpnz]['jmpins'][] = $i; |
---|
536 | break; |
---|
537 | |
---|
538 | case XC_JMPZ: |
---|
539 | case XC_JMPNZ: |
---|
540 | case XC_JMPZ_EX: |
---|
541 | case XC_JMPNZ_EX: |
---|
542 | // case XC_FE_RESET: |
---|
543 | case XC_FE_FETCH: |
---|
544 | // case XC_JMP_NO_CTOR: |
---|
545 | $target = $op['op2']['opline_num']; |
---|
546 | //if (!isset($target)) { |
---|
547 | // $this->dumpop($op, $EX); |
---|
548 | // var_dump($op); exit; |
---|
549 | //} |
---|
550 | $op['jmpouts'] = array($target); |
---|
551 | $opcodes[$target]['jmpins'][] = $i; |
---|
552 | break; |
---|
553 | |
---|
554 | /* |
---|
555 | case XC_RETURN: |
---|
556 | $op['jmpouts'] = array(); |
---|
557 | break; |
---|
558 | */ |
---|
559 | } |
---|
560 | } |
---|
561 | unset($op); |
---|
562 | // }}} |
---|
563 | // build semi-basic blocks |
---|
564 | $nextbbs = array(); |
---|
565 | $starti = 0; |
---|
566 | for ($i = 1, $cnt = count($opcodes); $i < $cnt; $i ++) { |
---|
567 | if (isset($opcodes[$i]['jmpins']) |
---|
568 | || isset($opcodes[$i - 1]['jmpouts'])) { |
---|
569 | $nextbbs[$starti] = $i; |
---|
570 | $starti = $i; |
---|
571 | } |
---|
572 | } |
---|
573 | $nextbbs[$starti] = $cnt; |
---|
574 | |
---|
575 | $EX = array(); |
---|
576 | $EX['Ts'] = array(); |
---|
577 | $EX['indent'] = $indent; |
---|
578 | $EX['nextbbs'] = $nextbbs; |
---|
579 | $EX['op_array'] = &$op_array; |
---|
580 | $EX['opcodes'] = &$opcodes; |
---|
581 | // func call |
---|
582 | $EX['object'] = null; |
---|
583 | $EX['called_scope'] = null; |
---|
584 | $EX['fbc'] = null; |
---|
585 | $EX['argstack'] = array(); |
---|
586 | $EX['arg_types_stack'] = array(); |
---|
587 | $EX['last'] = count($opcodes) - 1; |
---|
588 | $EX['silence'] = 0; |
---|
589 | |
---|
590 | for ($next = 0, $last = $EX['last']; |
---|
591 | $loop = $this->outputCode($EX, $next, $last, $indent, true); |
---|
592 | list($next, $last) = $loop) { |
---|
593 | // empty |
---|
594 | } |
---|
595 | return $EX; |
---|
596 | } |
---|
597 | // }}} |
---|
598 | function outputCode(&$EX, $opline, $last, $indent, $loop = false) // {{{ |
---|
599 | { |
---|
600 | $op = &$EX['opcodes'][$opline]; |
---|
601 | $next = $EX['nextbbs'][$opline]; |
---|
602 | |
---|
603 | $end = $next - 1; |
---|
604 | if ($end > $last) { |
---|
605 | $end = $last; |
---|
606 | } |
---|
607 | |
---|
608 | if (isset($op['jmpins'])) { |
---|
609 | echo "\nline", $op['line'], ":\n"; |
---|
610 | } |
---|
611 | else { |
---|
612 | // echo ";;;\n"; |
---|
613 | } |
---|
614 | $this->dasmBasicBlock($EX, $opline, $end); |
---|
615 | $this->outputPhp($EX['opcodes'], $opline, $end, $indent); |
---|
616 | // jmpout op |
---|
617 | $op = &$EX['opcodes'][$end]; |
---|
618 | $op1 = $op['op1']; |
---|
619 | $op2 = $op['op2']; |
---|
620 | $ext = $op['extended_value']; |
---|
621 | $line = $op['line']; |
---|
622 | |
---|
623 | if (isset($EX['opcodes'][$next])) { |
---|
624 | if (isset($last) && $next > $last) { |
---|
625 | $next = null; |
---|
626 | } |
---|
627 | } |
---|
628 | else { |
---|
629 | $next = null; |
---|
630 | } |
---|
631 | if ($op['opcode'] == XC_FE_FETCH) { |
---|
632 | $opline = $next; |
---|
633 | $next = $op['op2']['opline_num']; |
---|
634 | $end = $next - 1; |
---|
635 | |
---|
636 | ob_start(); |
---|
637 | $this->outputCode($EX, $opline, $end /* - 1 skip last jmp */, $indent . INDENT); |
---|
638 | $body = ob_get_clean(); |
---|
639 | |
---|
640 | $as = toCode($op['fe_as'], $EX); |
---|
641 | if (isset($op['fe_key'])) { |
---|
642 | $as = toCode($op['fe_key'], $EX) . ' => ' . $as; |
---|
643 | } |
---|
644 | echo "{$indent}foreach (" . toCode($op['fe_src'], $EX) . " as $as) {\n"; |
---|
645 | echo $body; |
---|
646 | echo "{$indent}}"; |
---|
647 | // $this->outputCode($EX, $next, $last, $indent); |
---|
648 | // return; |
---|
649 | } |
---|
650 | /* |
---|
651 | if ($op['opcode'] == XC_JMPZ) { |
---|
652 | $target = $op2['opline_num']; |
---|
653 | if ($line + 1) { |
---|
654 | $nextblock = $EX['nextbbs'][$next]; |
---|
655 | $jmpop = end($nextblock); |
---|
656 | if ($jmpop['opcode'] == XC_JMP) { |
---|
657 | $ifendline = $op2['opline_num']; |
---|
658 | if ($ifendline >= $line) { |
---|
659 | $cond = $op['cond']; |
---|
660 | echo "{$indent}if ($cond) {\n"; |
---|
661 | $this->outputCode($EX, $next, $last, INDENT . $indent); |
---|
662 | echo "$indent}\n"; |
---|
663 | $this->outputCode($EX, $target, $last, $indent); |
---|
664 | return; |
---|
665 | } |
---|
666 | } |
---|
667 | } |
---|
668 | } |
---|
669 | */ |
---|
670 | if (!isset($next)) { |
---|
671 | return; |
---|
672 | } |
---|
673 | if (!empty($op['jmpouts']) && isset($op['isjmp'])) { |
---|
674 | if (isset($op['cond'])) { |
---|
675 | echo "{$indent}check ($op[cond]) {\n"; |
---|
676 | echo INDENT; |
---|
677 | } |
---|
678 | echo $indent; |
---|
679 | echo xcache_get_opcode($op['opcode']), ' line', $op['jmpouts'][0]; |
---|
680 | if (isset($op['jmpouts'][1])) { |
---|
681 | echo ', line', $op['jmpouts'][1]; |
---|
682 | } |
---|
683 | echo ";"; |
---|
684 | // echo ' // <- line', $op['line']; |
---|
685 | echo "\n"; |
---|
686 | if (isset($op['cond'])) echo "$indent}\n"; |
---|
687 | } |
---|
688 | |
---|
689 | // proces JMPZ_EX/JMPNZ_EX for AND,OR |
---|
690 | $op = &$EX['opcodes'][$next]; |
---|
691 | /* |
---|
692 | if (isset($op['jmpins'])) { |
---|
693 | foreach (array_reverse($op['jmpins']) as $fromline) { |
---|
694 | $fromop = $EX['opcodes'][$fromline]; |
---|
695 | switch ($fromop['opcode']) { |
---|
696 | case XC_JMPZ_EX: $opstr = 'and'; break; |
---|
697 | case XC_JMPNZ_EX: $opstr = 'or'; break; |
---|
698 | case XC_JMPZNZ: var_dump($fromop); exit; |
---|
699 | default: continue 2; |
---|
700 | } |
---|
701 | |
---|
702 | $var = $fromop['result']['var']; |
---|
703 | var_dump($EX['Ts'][$var]); |
---|
704 | $EX['Ts'][$var] = '(' . $fromop['and_or'] . " $opstr " . $EX['Ts'][$var] . ')'; |
---|
705 | } |
---|
706 | #$this->outputCode($EX, $next, $last, $indent); |
---|
707 | #return; |
---|
708 | } |
---|
709 | */ |
---|
710 | if (isset($op['cond_false'])) { |
---|
711 | // $this->dumpop($op, $EX); |
---|
712 | // any true comes here, so it's a "or" |
---|
713 | $cond = implode(' and ', $op['cond_false']); |
---|
714 | // var_dump($op['cond'] = $cond); |
---|
715 | /* |
---|
716 | $rvalue = implode(' or ', $op['cond_true']) . ' or ' . $rvalue; |
---|
717 | unset($op['cond_true']); |
---|
718 | */ |
---|
719 | } |
---|
720 | |
---|
721 | if ($loop) { |
---|
722 | return array($next, $last); |
---|
723 | } |
---|
724 | $this->outputCode($EX, $next, $last, $indent); |
---|
725 | } |
---|
726 | // }}} |
---|
727 | function unquoteName($str) // {{{ |
---|
728 | { |
---|
729 | if (preg_match($this->rQuotedName, $str)) { |
---|
730 | $str = substr($str, 1, -1); |
---|
731 | } |
---|
732 | return $str; |
---|
733 | } |
---|
734 | // }}} |
---|
735 | function dasmBasicBlock(&$EX, $opline, $last) // {{{ |
---|
736 | { |
---|
737 | $T = &$EX['Ts']; |
---|
738 | $opcodes = &$EX['opcodes']; |
---|
739 | $lastphpop = null; |
---|
740 | |
---|
741 | for ($i = $opline, $ic = $last + 1; $i < $ic; $i ++) { |
---|
742 | // {{{ prepair |
---|
743 | $op = &$opcodes[$i]; |
---|
744 | $opc = $op['opcode']; |
---|
745 | if ($opc == XC_NOP) { |
---|
746 | continue; |
---|
747 | } |
---|
748 | |
---|
749 | $op1 = $op['op1']; |
---|
750 | $op2 = $op['op2']; |
---|
751 | $res = $op['result']; |
---|
752 | $ext = $op['extended_value']; |
---|
753 | |
---|
754 | $opname = xcache_get_opcode($opc); |
---|
755 | |
---|
756 | if ($opname == 'UNDEF' || !isset($opname)) { |
---|
757 | echo 'UNDEF OP:'; |
---|
758 | $this->dumpop($op, $EX); |
---|
759 | continue; |
---|
760 | } |
---|
761 | // $this->dumpop($op, $EX); //var_dump($op); |
---|
762 | |
---|
763 | $resvar = null; |
---|
764 | if ((ZEND_ENGINE_2_4 ? ($res['op_type'] & EXT_TYPE_UNUSED) : ($res['EA.type'] & EXT_TYPE_UNUSED)) || $res['op_type'] == XC_IS_UNUSED) { |
---|
765 | $istmpres = false; |
---|
766 | } |
---|
767 | else { |
---|
768 | $istmpres = true; |
---|
769 | } |
---|
770 | // }}} |
---|
771 | // echo $opname, "\n"; |
---|
772 | |
---|
773 | $call = array(&$this, $opname); |
---|
774 | if (is_callable($call)) { |
---|
775 | $this->{$opname}($op, $EX); |
---|
776 | } |
---|
777 | else if (isset($this->binops[$opc])) { // {{{ |
---|
778 | $op1val = $this->getOpVal($op1, $EX, false); |
---|
779 | $op2val = $this->getOpVal($op2, $EX, false); |
---|
780 | $rvalue = new Decompiler_Binop($this, $op1val, $opc, $op2val); |
---|
781 | $resvar = $rvalue; |
---|
782 | // }}} |
---|
783 | } |
---|
784 | else if (isset($this->unaryops[$opc])) { // {{{ |
---|
785 | $op1val = $this->getOpVal($op1, $EX); |
---|
786 | $myop = $this->unaryops[$opc]; |
---|
787 | $rvalue = "$myop$op1val"; |
---|
788 | $resvar = $rvalue; |
---|
789 | // }}} |
---|
790 | } |
---|
791 | else { |
---|
792 | switch ($opc) { |
---|
793 | case XC_NEW: // {{{ |
---|
794 | array_push($EX['arg_types_stack'], array($EX['fbc'], $EX['object'], $EX['called_scope'])); |
---|
795 | $EX['object'] = (int) $res['var']; |
---|
796 | $EX['called_scope'] = null; |
---|
797 | $EX['fbc'] = 'new ' . $this->unquoteName($this->getOpVal($op1, $EX)); |
---|
798 | if (!ZEND_ENGINE_2) { |
---|
799 | $resvar = '$new object$'; |
---|
800 | } |
---|
801 | break; |
---|
802 | // }}} |
---|
803 | case XC_FETCH_CLASS: // {{{ |
---|
804 | if ($op2['op_type'] == XC_IS_UNUSED) { |
---|
805 | switch (($ext & (defined('ZEND_FETCH_CLASS_MASK') ? ZEND_FETCH_CLASS_MASK : 0xFF))) { |
---|
806 | case ZEND_FETCH_CLASS_SELF: |
---|
807 | $class = 'self'; |
---|
808 | break; |
---|
809 | case ZEND_FETCH_CLASS_PARENT: |
---|
810 | $class = 'parent'; |
---|
811 | break; |
---|
812 | case ZEND_FETCH_CLASS_STATIC: |
---|
813 | $class = 'static'; |
---|
814 | break; |
---|
815 | } |
---|
816 | $istmpres = true; |
---|
817 | } |
---|
818 | else { |
---|
819 | $class = $op2['constant']; |
---|
820 | if (is_object($class)) { |
---|
821 | $class = get_class($class); |
---|
822 | } |
---|
823 | } |
---|
824 | $resvar = $class; |
---|
825 | break; |
---|
826 | // }}} |
---|
827 | case XC_FETCH_CONSTANT: // {{{ |
---|
828 | if ($op1['op_type'] == XC_IS_CONST) { |
---|
829 | $resvar = $op1['constant']; |
---|
830 | } |
---|
831 | else if ($op1['op_type'] == XC_IS_UNUSED) { |
---|
832 | $resvar = $op2['constant']; |
---|
833 | } |
---|
834 | else { |
---|
835 | $class = $T[$op1['var']]; |
---|
836 | assert($class[0] == 'class'); |
---|
837 | $resvar = $class[1] . '::' . $op2['constant']; |
---|
838 | } |
---|
839 | break; |
---|
840 | // }}} |
---|
841 | // {{{ case XC_FETCH_* |
---|
842 | case XC_FETCH_R: |
---|
843 | case XC_FETCH_W: |
---|
844 | case XC_FETCH_RW: |
---|
845 | case XC_FETCH_FUNC_ARG: |
---|
846 | case XC_FETCH_UNSET: |
---|
847 | case XC_FETCH_IS: |
---|
848 | case XC_UNSET_VAR: |
---|
849 | $rvalue = $this->getOpVal($op1, $EX); |
---|
850 | if (defined('ZEND_FETCH_TYPE_MASK')) { |
---|
851 | $fetchtype = ($ext & ZEND_FETCH_TYPE_MASK); |
---|
852 | } |
---|
853 | else { |
---|
854 | $fetchtype = $op2[!ZEND_ENGINE_2 ? 'fetch_type' : 'EA.type']; |
---|
855 | } |
---|
856 | switch ($fetchtype) { |
---|
857 | case ZEND_FETCH_STATIC_MEMBER: |
---|
858 | $class = $this->getOpVal($op2, $EX); |
---|
859 | $rvalue = $class . '::$' . $this->unquoteName($rvalue); |
---|
860 | break; |
---|
861 | default: |
---|
862 | $name = $this->unquoteName($rvalue); |
---|
863 | $globalname = xcache_is_autoglobal($name) ? "\$$name" : "\$GLOBALS[$rvalue]"; |
---|
864 | $rvalue = new Decompiler_Fetch($rvalue, $fetchtype, $globalname); |
---|
865 | break; |
---|
866 | } |
---|
867 | if ($opc == XC_UNSET_VAR) { |
---|
868 | $op['php'] = "unset(" . toCode($rvalue, $EX) . ")"; |
---|
869 | $lastphpop = &$op; |
---|
870 | } |
---|
871 | else if ($res['op_type'] != XC_IS_UNUSED) { |
---|
872 | $resvar = $rvalue; |
---|
873 | } |
---|
874 | break; |
---|
875 | // }}} |
---|
876 | // {{{ case XC_FETCH_DIM_* |
---|
877 | case XC_FETCH_DIM_TMP_VAR: |
---|
878 | case XC_FETCH_DIM_R: |
---|
879 | case XC_FETCH_DIM_W: |
---|
880 | case XC_FETCH_DIM_RW: |
---|
881 | case XC_FETCH_DIM_FUNC_ARG: |
---|
882 | case XC_FETCH_DIM_UNSET: |
---|
883 | case XC_FETCH_DIM_IS: |
---|
884 | case XC_ASSIGN_DIM: |
---|
885 | case XC_UNSET_DIM: |
---|
886 | case XC_UNSET_DIM_OBJ: |
---|
887 | $src = $this->getOpVal($op1, $EX, false); |
---|
888 | if (is_a($src, "Decompiler_ForeachBox")) { |
---|
889 | $src->iskey = $this->getOpVal($op2, $EX); |
---|
890 | $resvar = $src; |
---|
891 | break; |
---|
892 | } |
---|
893 | else if (is_a($src, "Decompiler_DimBox")) { |
---|
894 | $dimbox = $src; |
---|
895 | } |
---|
896 | else { |
---|
897 | if (!is_a($src, "Decompiler_ListBox")) { |
---|
898 | $list = new Decompiler_List($this->getOpVal($op1, $EX, false)); |
---|
899 | |
---|
900 | $src = new Decompiler_ListBox($list); |
---|
901 | if (!isset($op1['var'])) { |
---|
902 | $this->dumpop($op, $EX); |
---|
903 | var_dump($op); |
---|
904 | die('missing var'); |
---|
905 | } |
---|
906 | $T[$op1['var']] = $src; |
---|
907 | unset($list); |
---|
908 | } |
---|
909 | $dim = new Decompiler_Dim($src); |
---|
910 | $src->obj->dims[] = &$dim; |
---|
911 | |
---|
912 | $dimbox = new Decompiler_DimBox($dim); |
---|
913 | } |
---|
914 | $dim = &$dimbox->obj; |
---|
915 | $dim->offsets[] = $this->getOpVal($op2, $EX); |
---|
916 | if ($ext == ZEND_FETCH_ADD_LOCK) { |
---|
917 | $src->obj->everLocked = true; |
---|
918 | } |
---|
919 | else if ($ext == ZEND_FETCH_STANDARD) { |
---|
920 | $dim->isLast = true; |
---|
921 | } |
---|
922 | unset($dim); |
---|
923 | $rvalue = $dimbox; |
---|
924 | |
---|
925 | if ($opc == XC_ASSIGN_DIM) { |
---|
926 | $lvalue = $rvalue; |
---|
927 | ++ $i; |
---|
928 | $rvalue = $this->getOpVal($opcodes[$i]['op1'], $EX); |
---|
929 | $resvar = toCode($lvalue, $EX) . ' = ' . $rvalue; |
---|
930 | } |
---|
931 | else if ($opc == XC_UNSET_DIM) { |
---|
932 | $op['php'] = "unset(" . toCode($rvalue, $EX) . ")"; |
---|
933 | $lastphpop = &$op; |
---|
934 | } |
---|
935 | else if ($res['op_type'] != XC_IS_UNUSED) { |
---|
936 | $resvar = $rvalue; |
---|
937 | } |
---|
938 | break; |
---|
939 | // }}} |
---|
940 | case XC_ASSIGN: // {{{ |
---|
941 | $lvalue = $this->getOpVal($op1, $EX); |
---|
942 | $rvalue = $this->getOpVal($op2, $EX, false); |
---|
943 | if (is_a($rvalue, 'Decompiler_ForeachBox')) { |
---|
944 | $type = $rvalue->iskey ? 'fe_key' : 'fe_as'; |
---|
945 | $rvalue->obj[$type] = $lvalue; |
---|
946 | unset($T[$op2['var']]); |
---|
947 | break; |
---|
948 | } |
---|
949 | if (is_a($rvalue, "Decompiler_DimBox")) { |
---|
950 | $dim = &$rvalue->obj; |
---|
951 | $dim->assign = $lvalue; |
---|
952 | if ($dim->isLast) { |
---|
953 | $resvar = toCode($dim->value, $EX); |
---|
954 | } |
---|
955 | unset($dim); |
---|
956 | break; |
---|
957 | } |
---|
958 | $resvar = "$lvalue = " . toCode($rvalue, $EX); |
---|
959 | break; |
---|
960 | // }}} |
---|
961 | case XC_ASSIGN_REF: // {{{ |
---|
962 | $lvalue = $this->getOpVal($op1, $EX); |
---|
963 | $rvalue = $this->getOpVal($op2, $EX, false); |
---|
964 | if (is_a($rvalue, 'Decompiler_Fetch')) { |
---|
965 | $src = toCode($rvalue->src, $EX); |
---|
966 | if (substr($src, 1, -1) == substr($lvalue, 1)) { |
---|
967 | switch ($rvalue->fetchType) { |
---|
968 | case ZEND_FETCH_GLOBAL: |
---|
969 | case ZEND_FETCH_GLOBAL_LOCK: |
---|
970 | $resvar = 'global ' . $lvalue; |
---|
971 | break 2; |
---|
972 | case ZEND_FETCH_STATIC: |
---|
973 | $statics = &$EX['op_array']['static_variables']; |
---|
974 | $resvar = 'static ' . $lvalue; |
---|
975 | $name = substr($src, 1, -1); |
---|
976 | if (isset($statics[$name])) { |
---|
977 | $var = $statics[$name]; |
---|
978 | $resvar .= ' = '; |
---|
979 | $resvar .= toCode(value($var), $EX); |
---|
980 | } |
---|
981 | unset($statics); |
---|
982 | break 2; |
---|
983 | default: |
---|
984 | } |
---|
985 | } |
---|
986 | } |
---|
987 | // TODO: PHP_6 global |
---|
988 | $rvalue = toCode($rvalue, $EX); |
---|
989 | $resvar = "$lvalue = &$rvalue"; |
---|
990 | break; |
---|
991 | // }}} |
---|
992 | // {{{ case XC_FETCH_OBJ_* |
---|
993 | case XC_FETCH_OBJ_R: |
---|
994 | case XC_FETCH_OBJ_W: |
---|
995 | case XC_FETCH_OBJ_RW: |
---|
996 | case XC_FETCH_OBJ_FUNC_ARG: |
---|
997 | case XC_FETCH_OBJ_UNSET: |
---|
998 | case XC_FETCH_OBJ_IS: |
---|
999 | case XC_ASSIGN_OBJ: |
---|
1000 | $obj = $this->getOpVal($op1, $EX); |
---|
1001 | if (!isset($obj)) { |
---|
1002 | $obj = '$this'; |
---|
1003 | } |
---|
1004 | $prop = $this->getOpVal($op2, $EX); |
---|
1005 | if (preg_match($this->rQuotedName, $prop)) { |
---|
1006 | $prop = substr($prop, 1, -1);; |
---|
1007 | $rvalue = "{$obj}->$prop"; |
---|
1008 | } |
---|
1009 | else { |
---|
1010 | $rvalue = "{$obj}->{" . "$prop}"; |
---|
1011 | } |
---|
1012 | if ($res['op_type'] != XC_IS_UNUSED) { |
---|
1013 | $resvar = $rvalue; |
---|
1014 | } |
---|
1015 | if ($opc == XC_ASSIGN_OBJ) { |
---|
1016 | ++ $i; |
---|
1017 | $lvalue = $rvalue; |
---|
1018 | $rvalue = $this->getOpVal($opcodes[$i]['op1'], $EX); |
---|
1019 | $resvar = "$lvalue = $rvalue"; |
---|
1020 | } |
---|
1021 | break; |
---|
1022 | // }}} |
---|
1023 | case XC_ISSET_ISEMPTY_DIM_OBJ: |
---|
1024 | case XC_ISSET_ISEMPTY_PROP_OBJ: |
---|
1025 | case XC_ISSET_ISEMPTY: |
---|
1026 | case XC_ISSET_ISEMPTY_VAR: // {{{ |
---|
1027 | if ($opc == XC_ISSET_ISEMPTY_VAR) { |
---|
1028 | $rvalue = $this->getOpVal($op1, $EX);; |
---|
1029 | if (preg_match($this->rQuotedName, $rvalue)) { |
---|
1030 | $rvalue = '$' . substr($rvalue, 1, -1); |
---|
1031 | } |
---|
1032 | else { |
---|
1033 | $rvalue = '${' . $rvalue . '}'; |
---|
1034 | } |
---|
1035 | if ($op2['EA.type'] == ZEND_FETCH_STATIC_MEMBER) { |
---|
1036 | $class = $this->getOpVal($op2, $EX); |
---|
1037 | $rvalue = $class . '::' . $rvalue; |
---|
1038 | } |
---|
1039 | } |
---|
1040 | else if ($opc == XC_ISSET_ISEMPTY) { |
---|
1041 | $rvalue = $this->getOpVal($op1, $EX); |
---|
1042 | } |
---|
1043 | else { |
---|
1044 | $container = $this->getOpVal($op1, $EX); |
---|
1045 | $dim = $this->getOpVal($op2, $EX); |
---|
1046 | if ($opc == XC_ISSET_ISEMPTY_PROP_OBJ) { |
---|
1047 | if (preg_match($this->rQuotedName, $dim)) { |
---|
1048 | $rvalue = $container . "->" . substr($dim, 1, -1); |
---|
1049 | } |
---|
1050 | else { |
---|
1051 | $rvalue = $container . "->{" . $dim . "}"; |
---|
1052 | } |
---|
1053 | } |
---|
1054 | else { |
---|
1055 | $rvalue = $container . "[$dim]"; |
---|
1056 | } |
---|
1057 | } |
---|
1058 | |
---|
1059 | switch ((!ZEND_ENGINE_2 ? $op['op2']['var'] /* constant */ : $ext) & (ZEND_ISSET|ZEND_ISEMPTY)) { |
---|
1060 | case ZEND_ISSET: |
---|
1061 | $rvalue = "isset($rvalue)"; |
---|
1062 | break; |
---|
1063 | case ZEND_ISEMPTY: |
---|
1064 | $rvalue = "empty($rvalue)"; |
---|
1065 | break; |
---|
1066 | } |
---|
1067 | $resvar = $rvalue; |
---|
1068 | break; |
---|
1069 | // }}} |
---|
1070 | case XC_SEND_VAR_NO_REF: |
---|
1071 | case XC_SEND_VAL: |
---|
1072 | case XC_SEND_REF: |
---|
1073 | case XC_SEND_VAR: // {{{ |
---|
1074 | $ref = ($opc == XC_SEND_REF ? '&' : ''); |
---|
1075 | $EX['argstack'][] = $ref . $this->getOpVal($op1, $EX); |
---|
1076 | break; |
---|
1077 | // }}} |
---|
1078 | case XC_INIT_STATIC_METHOD_CALL: |
---|
1079 | case XC_INIT_METHOD_CALL: |
---|
1080 | case XC_INIT_FCALL_BY_FUNC: |
---|
1081 | case XC_INIT_FCALL_BY_NAME: // {{{ |
---|
1082 | if (($ext & ZEND_CTOR_CALL)) { |
---|
1083 | break; |
---|
1084 | } |
---|
1085 | array_push($EX['arg_types_stack'], array($EX['fbc'], $EX['object'], $EX['called_scope'])); |
---|
1086 | if ($opc == XC_INIT_STATIC_METHOD_CALL || $opc == XC_INIT_METHOD_CALL || $op1['op_type'] != XC_IS_UNUSED) { |
---|
1087 | $obj = $this->getOpVal($op1, $EX); |
---|
1088 | if (!isset($obj)) { |
---|
1089 | $obj = '$this'; |
---|
1090 | } |
---|
1091 | if ($opc == XC_INIT_STATIC_METHOD_CALL || /* PHP4 */ isset($op1['constant'])) { |
---|
1092 | $EX['object'] = null; |
---|
1093 | $EX['called_scope'] = $this->unquoteName($obj); |
---|
1094 | } |
---|
1095 | else { |
---|
1096 | $EX['object'] = $obj; |
---|
1097 | $EX['called_scope'] = null; |
---|
1098 | } |
---|
1099 | if ($res['op_type'] != XC_IS_UNUSED) { |
---|
1100 | $resvar = '$obj call$'; |
---|
1101 | } |
---|
1102 | } |
---|
1103 | else { |
---|
1104 | $EX['object'] = null; |
---|
1105 | $EX['called_scope'] = null; |
---|
1106 | } |
---|
1107 | |
---|
1108 | if ($opc == XC_INIT_FCALL_BY_FUNC) { |
---|
1109 | $which = $op1['var']; |
---|
1110 | $EX['fbc'] = $EX['op_array']['funcs'][$which]['name']; |
---|
1111 | } |
---|
1112 | else { |
---|
1113 | $EX['fbc'] = $this->getOpVal($op2, $EX, false); |
---|
1114 | } |
---|
1115 | break; |
---|
1116 | // }}} |
---|
1117 | case XC_DO_FCALL_BY_FUNC: |
---|
1118 | $which = $op1['var']; |
---|
1119 | $fname = $EX['op_array']['funcs'][$which]['name']; |
---|
1120 | $args = $this->popargs($EX, $ext); |
---|
1121 | $resvar = $fname . "($args)"; |
---|
1122 | break; |
---|
1123 | case XC_DO_FCALL: |
---|
1124 | $fname = $this->unquoteName($this->getOpVal($op1, $EX, false)); |
---|
1125 | $args = $this->popargs($EX, $ext); |
---|
1126 | $resvar = $fname . "($args)"; |
---|
1127 | break; |
---|
1128 | case XC_DO_FCALL_BY_NAME: // {{{ |
---|
1129 | $object = null; |
---|
1130 | |
---|
1131 | $fname = $this->unquoteName($EX['fbc']); |
---|
1132 | if (!is_int($EX['object'])) { |
---|
1133 | $object = $EX['object']; |
---|
1134 | } |
---|
1135 | |
---|
1136 | $args = $this->popargs($EX, $ext); |
---|
1137 | |
---|
1138 | $resvar = |
---|
1139 | (isset($object) ? $object . '->' : '' ) |
---|
1140 | . (isset($EX['called_scope']) ? $EX['called_scope'] . '::' : '' ) |
---|
1141 | . $fname . "($args)"; |
---|
1142 | unset($args); |
---|
1143 | |
---|
1144 | if (is_int($EX['object'])) { |
---|
1145 | $T[$EX['object']] = $resvar; |
---|
1146 | $resvar = null; |
---|
1147 | } |
---|
1148 | list($EX['fbc'], $EX['object'], $EX['called_scope']) = array_pop($EX['arg_types_stack']); |
---|
1149 | break; |
---|
1150 | // }}} |
---|
1151 | case XC_VERIFY_ABSTRACT_CLASS: // {{{ |
---|
1152 | //unset($T[$op1['var']]); |
---|
1153 | break; |
---|
1154 | // }}} |
---|
1155 | case XC_DECLARE_CLASS: |
---|
1156 | case XC_DECLARE_INHERITED_CLASS: |
---|
1157 | case XC_DECLARE_INHERITED_CLASS_DELAYED: // {{{ |
---|
1158 | $key = $op1['constant']; |
---|
1159 | if (!isset($this->dc['class_table'][$key])) { |
---|
1160 | echo 'class not found: ', $key, 'existing classes are:', "\n"; |
---|
1161 | var_dump(array_keys($this->dc['class_table'])); |
---|
1162 | exit; |
---|
1163 | } |
---|
1164 | $class = &$this->dc['class_table'][$key]; |
---|
1165 | $class['name'] = $this->unquoteName($this->getOpVal($op2, $EX)); |
---|
1166 | if ($opc == XC_DECLARE_INHERITED_CLASS || $opc == XC_DECLARE_INHERITED_CLASS_DELAYED) { |
---|
1167 | $ext /= XC_SIZEOF_TEMP_VARIABLE; |
---|
1168 | $class['parent'] = $T[$ext]; |
---|
1169 | unset($T[$ext]); |
---|
1170 | } |
---|
1171 | else { |
---|
1172 | $class['parent'] = null; |
---|
1173 | } |
---|
1174 | |
---|
1175 | while ($i + 2 < $ic |
---|
1176 | && $opcodes[$i + 2]['opcode'] == XC_ADD_INTERFACE |
---|
1177 | && $opcodes[$i + 2]['op1']['var'] == $res['var'] |
---|
1178 | && $opcodes[$i + 1]['opcode'] == XC_FETCH_CLASS) { |
---|
1179 | $fetchop = &$opcodes[$i + 1]; |
---|
1180 | $impl = $this->unquoteName($this->getOpVal($fetchop['op2'], $EX)); |
---|
1181 | $addop = &$opcodes[$i + 2]; |
---|
1182 | $class['interfaces'][$addop['extended_value']] = $impl; |
---|
1183 | unset($fetchop, $addop); |
---|
1184 | $i += 2; |
---|
1185 | } |
---|
1186 | $this->dclass($class); |
---|
1187 | unset($class); |
---|
1188 | break; |
---|
1189 | // }}} |
---|
1190 | case XC_INIT_STRING: // {{{ |
---|
1191 | $resvar = "''"; |
---|
1192 | break; |
---|
1193 | // }}} |
---|
1194 | case XC_ADD_CHAR: |
---|
1195 | case XC_ADD_STRING: |
---|
1196 | case XC_ADD_VAR: // {{{ |
---|
1197 | $op1val = $this->getOpVal($op1, $EX); |
---|
1198 | $op2val = $this->getOpVal($op2, $EX); |
---|
1199 | switch ($opc) { |
---|
1200 | case XC_ADD_CHAR: |
---|
1201 | $op2val = toCode(chr($op2val), $EX); |
---|
1202 | break; |
---|
1203 | case XC_ADD_STRING: |
---|
1204 | $op2val = toCode($op2val, $EX); |
---|
1205 | break; |
---|
1206 | case XC_ADD_VAR: |
---|
1207 | break; |
---|
1208 | } |
---|
1209 | if ($op1val == "''") { |
---|
1210 | $rvalue = $op2val; |
---|
1211 | } |
---|
1212 | else if ($op2val == "''") { |
---|
1213 | $rvalue = $op1val; |
---|
1214 | } |
---|
1215 | else { |
---|
1216 | $rvalue = $op1val . ' . ' . $op2val; |
---|
1217 | } |
---|
1218 | $resvar = $rvalue; |
---|
1219 | // }}} |
---|
1220 | break; |
---|
1221 | case XC_PRINT: // {{{ |
---|
1222 | $op1val = $this->getOpVal($op1, $EX); |
---|
1223 | $resvar = "print($op1val)"; |
---|
1224 | break; |
---|
1225 | // }}} |
---|
1226 | case XC_ECHO: // {{{ |
---|
1227 | $op1val = $this->getOpVal($op1, $EX); |
---|
1228 | $resvar = "echo $op1val"; |
---|
1229 | break; |
---|
1230 | // }}} |
---|
1231 | case XC_EXIT: // {{{ |
---|
1232 | $op1val = $this->getOpVal($op1, $EX); |
---|
1233 | $resvar = "exit($op1val)"; |
---|
1234 | break; |
---|
1235 | // }}} |
---|
1236 | case XC_INIT_ARRAY: |
---|
1237 | case XC_ADD_ARRAY_ELEMENT: // {{{ |
---|
1238 | $rvalue = $this->getOpVal($op1, $EX, false, true); |
---|
1239 | |
---|
1240 | if ($opc == XC_ADD_ARRAY_ELEMENT) { |
---|
1241 | $offset = $this->getOpVal($op2, $EX); |
---|
1242 | if (isset($offset)) { |
---|
1243 | $T[$res['var']]->value[$offset] = $rvalue; |
---|
1244 | } |
---|
1245 | else { |
---|
1246 | $T[$res['var']]->value[] = $rvalue; |
---|
1247 | } |
---|
1248 | } |
---|
1249 | else { |
---|
1250 | if ($opc == XC_INIT_ARRAY) { |
---|
1251 | $resvar = new Decompiler_Array(); |
---|
1252 | if (!isset($rvalue)) { |
---|
1253 | continue; |
---|
1254 | } |
---|
1255 | } |
---|
1256 | |
---|
1257 | $offset = $this->getOpVal($op2, $EX); |
---|
1258 | if (isset($offset)) { |
---|
1259 | $resvar->value[$offset] = $rvalue; |
---|
1260 | } |
---|
1261 | else { |
---|
1262 | $resvar->value[] = $rvalue; |
---|
1263 | } |
---|
1264 | } |
---|
1265 | break; |
---|
1266 | // }}} |
---|
1267 | case XC_QM_ASSIGN: // {{{ |
---|
1268 | $resvar = $this->getOpVal($op1, $EX); |
---|
1269 | break; |
---|
1270 | // }}} |
---|
1271 | case XC_BOOL: // {{{ |
---|
1272 | $resvar = /*'(bool) ' .*/ $this->getOpVal($op1, $EX); |
---|
1273 | break; |
---|
1274 | // }}} |
---|
1275 | case XC_RETURN: // {{{ |
---|
1276 | $resvar = "return " . $this->getOpVal($op1, $EX); |
---|
1277 | break; |
---|
1278 | // }}} |
---|
1279 | case XC_INCLUDE_OR_EVAL: // {{{ |
---|
1280 | $type = $op2['var']; // hack |
---|
1281 | $keyword = $this->includeTypes[$type]; |
---|
1282 | $resvar = "$keyword(" . $this->getOpVal($op1, $EX) . ")"; |
---|
1283 | break; |
---|
1284 | // }}} |
---|
1285 | case XC_FE_RESET: // {{{ |
---|
1286 | $resvar = $this->getOpVal($op1, $EX); |
---|
1287 | break; |
---|
1288 | // }}} |
---|
1289 | case XC_FE_FETCH: // {{{ |
---|
1290 | $op['fe_src'] = $this->getOpVal($op1, $EX); |
---|
1291 | $fe = new Decompiler_ForeachBox($op); |
---|
1292 | $fe->iskey = false; |
---|
1293 | $T[$res['var']] = $fe; |
---|
1294 | |
---|
1295 | ++ $i; |
---|
1296 | if (($ext & ZEND_FE_FETCH_WITH_KEY)) { |
---|
1297 | $fe = new Decompiler_ForeachBox($op); |
---|
1298 | $fe->iskey = true; |
---|
1299 | |
---|
1300 | $res = $opcodes[$i]['result']; |
---|
1301 | $T[$res['var']] = $fe; |
---|
1302 | } |
---|
1303 | break; |
---|
1304 | // }}} |
---|
1305 | case XC_SWITCH_FREE: // {{{ |
---|
1306 | // unset($T[$op1['var']]); |
---|
1307 | break; |
---|
1308 | // }}} |
---|
1309 | case XC_FREE: // {{{ |
---|
1310 | $free = $T[$op1['var']]; |
---|
1311 | if (!is_a($free, 'Decompiler_Array') && !is_a($free, 'Decompiler_Box')) { |
---|
1312 | $op['php'] = is_object($free) ? $free : $this->unquote($free, '(', ')'); |
---|
1313 | $lastphpop = &$op; |
---|
1314 | } |
---|
1315 | unset($T[$op1['var']], $free); |
---|
1316 | break; |
---|
1317 | // }}} |
---|
1318 | case XC_JMP_NO_CTOR: |
---|
1319 | break; |
---|
1320 | case XC_JMPNZ: // while |
---|
1321 | case XC_JMPZNZ: // for |
---|
1322 | case XC_JMPZ_EX: // and |
---|
1323 | case XC_JMPNZ_EX: // or |
---|
1324 | case XC_JMPZ: // {{{ |
---|
1325 | if ($opc == XC_JMP_NO_CTOR && $EX['object']) { |
---|
1326 | $rvalue = $EX['object']; |
---|
1327 | } |
---|
1328 | else { |
---|
1329 | $rvalue = $this->getOpVal($op1, $EX); |
---|
1330 | } |
---|
1331 | |
---|
1332 | if (isset($op['cond_true'])) { |
---|
1333 | // any true comes here, so it's a "or" |
---|
1334 | $rvalue = implode(' or ', $op['cond_true']) . ' or ' . $rvalue; |
---|
1335 | unset($op['cond_true']); |
---|
1336 | } |
---|
1337 | if (isset($op['cond_false'])) { |
---|
1338 | echo "TODO(cond_false):\n"; |
---|
1339 | var_dump($op);// exit; |
---|
1340 | } |
---|
1341 | if ($opc == XC_JMPZ_EX || $opc == XC_JMPNZ_EX || $opc == XC_JMPZ) { |
---|
1342 | $targetop = &$EX['opcodes'][$op2['opline_num']]; |
---|
1343 | if ($opc == XC_JMPNZ_EX) { |
---|
1344 | $targetop['cond_true'][] = toCode($rvalue, $EX); |
---|
1345 | } |
---|
1346 | else { |
---|
1347 | $targetop['cond_false'][] = toCode($rvalue, $EX); |
---|
1348 | } |
---|
1349 | unset($targetop); |
---|
1350 | } |
---|
1351 | else { |
---|
1352 | $op['cond'] = $rvalue; |
---|
1353 | $op['isjmp'] = true; |
---|
1354 | } |
---|
1355 | break; |
---|
1356 | // }}} |
---|
1357 | case XC_JMP: // {{{ |
---|
1358 | $op['cond'] = null; |
---|
1359 | $op['isjmp'] = true; |
---|
1360 | break; |
---|
1361 | // }}} |
---|
1362 | case XC_CASE: |
---|
1363 | case XC_BRK: |
---|
1364 | break; |
---|
1365 | case XC_RECV_INIT: |
---|
1366 | case XC_RECV: |
---|
1367 | $offset = $this->getOpVal($op1, $EX); |
---|
1368 | $lvalue = $this->getOpVal($op['result'], $EX); |
---|
1369 | if ($opc == XC_RECV_INIT) { |
---|
1370 | $default = value($op['op2']['constant']); |
---|
1371 | } |
---|
1372 | else { |
---|
1373 | $default = null; |
---|
1374 | } |
---|
1375 | $EX['recvs'][$offset] = array($lvalue, $default); |
---|
1376 | break; |
---|
1377 | case XC_POST_DEC: |
---|
1378 | case XC_POST_INC: |
---|
1379 | case XC_POST_DEC_OBJ: |
---|
1380 | case XC_POST_INC_OBJ: |
---|
1381 | case XC_PRE_DEC: |
---|
1382 | case XC_PRE_INC: |
---|
1383 | case XC_PRE_DEC_OBJ: |
---|
1384 | case XC_PRE_INC_OBJ: // {{{ |
---|
1385 | $flags = array_flip(explode('_', $opname)); |
---|
1386 | if (isset($flags['OBJ'])) { |
---|
1387 | $resvar = $this->getOpVal($op1, $EX); |
---|
1388 | $prop = $this->unquoteName($this->getOpVal($op2, $EX)); |
---|
1389 | if ($prop{0} == '$') { |
---|
1390 | $resvar = $resvar . "{" . $prop . "}"; |
---|
1391 | } |
---|
1392 | else { |
---|
1393 | $resvar = $resvar . "->" . $prop; |
---|
1394 | } |
---|
1395 | } |
---|
1396 | else { |
---|
1397 | $resvar = $this->getOpVal($op1, $EX); |
---|
1398 | } |
---|
1399 | $opstr = isset($flags['DEC']) ? '--' : '++'; |
---|
1400 | if (isset($flags['POST'])) { |
---|
1401 | $resvar .= ' ' . $opstr; |
---|
1402 | } |
---|
1403 | else { |
---|
1404 | $resvar = "$opstr $resvar"; |
---|
1405 | } |
---|
1406 | break; |
---|
1407 | // }}} |
---|
1408 | |
---|
1409 | case XC_BEGIN_SILENCE: // {{{ |
---|
1410 | $EX['silence'] ++; |
---|
1411 | break; |
---|
1412 | // }}} |
---|
1413 | case XC_END_SILENCE: // {{{ |
---|
1414 | $EX['silence'] --; |
---|
1415 | $lastresvar = '@' . toCode($lastresvar, $EX); |
---|
1416 | break; |
---|
1417 | // }}} |
---|
1418 | case XC_CONT: // {{{ |
---|
1419 | break; |
---|
1420 | // }}} |
---|
1421 | case XC_CAST: // {{{ |
---|
1422 | $type = $ext; |
---|
1423 | static $type2cast = array( |
---|
1424 | IS_LONG => '(int)', |
---|
1425 | IS_DOUBLE => '(double)', |
---|
1426 | IS_STRING => '(string)', |
---|
1427 | IS_ARRAY => '(array)', |
---|
1428 | IS_OBJECT => '(object)', |
---|
1429 | IS_BOOL => '(bool)', |
---|
1430 | IS_NULL => '(unset)', |
---|
1431 | ); |
---|
1432 | assert(isset($type2cast[$type])); |
---|
1433 | $cast = $type2cast[$type]; |
---|
1434 | $resvar = $cast . ' ' . $this->getOpVal($op1, $EX); |
---|
1435 | break; |
---|
1436 | // }}} |
---|
1437 | case XC_EXT_STMT: |
---|
1438 | case XC_EXT_FCALL_BEGIN: |
---|
1439 | case XC_EXT_FCALL_END: |
---|
1440 | case XC_EXT_NOP: |
---|
1441 | break; |
---|
1442 | case XC_DECLARE_FUNCTION_OR_CLASS: |
---|
1443 | /* always removed by compiler */ |
---|
1444 | break; |
---|
1445 | case XC_TICKS: |
---|
1446 | $lastphpop['ticks'] = $this->getOpVal($op1, $EX); |
---|
1447 | // $EX['tickschanged'] = true; |
---|
1448 | break; |
---|
1449 | default: // {{{ |
---|
1450 | echo "\x1B[31m * TODO ", $opname, "\x1B[0m\n"; |
---|
1451 | // }}} |
---|
1452 | } |
---|
1453 | } |
---|
1454 | if (isset($resvar)) { |
---|
1455 | if ($istmpres) { |
---|
1456 | $T[$res['var']] = $resvar; |
---|
1457 | $lastresvar = &$T[$res['var']]; |
---|
1458 | } |
---|
1459 | else { |
---|
1460 | $op['php'] = $resvar; |
---|
1461 | $lastphpop = &$op; |
---|
1462 | $lastresvar = &$op['php']; |
---|
1463 | } |
---|
1464 | } |
---|
1465 | } |
---|
1466 | return $T; |
---|
1467 | } |
---|
1468 | // }}} |
---|
1469 | function unquote($str, $st, $ed) // {{{ |
---|
1470 | { |
---|
1471 | $l1 = strlen($st); |
---|
1472 | $l2 = strlen($ed); |
---|
1473 | if (substr($str, 0, $l1) === $st && substr($str, -$l2) === $ed) { |
---|
1474 | $str = substr($str, $l1, -$l2); |
---|
1475 | } |
---|
1476 | return $str; |
---|
1477 | } |
---|
1478 | // }}} |
---|
1479 | function popargs(&$EX, $n) // {{{ |
---|
1480 | { |
---|
1481 | $args = array(); |
---|
1482 | for ($i = 0; $i < $n; $i ++) { |
---|
1483 | $a = array_pop($EX['argstack']); |
---|
1484 | if (is_array($a)) { |
---|
1485 | array_unshift($args, toCode($a, $EX)); |
---|
1486 | } |
---|
1487 | else { |
---|
1488 | array_unshift($args, $a); |
---|
1489 | } |
---|
1490 | } |
---|
1491 | return implode(', ', $args); |
---|
1492 | } |
---|
1493 | // }}} |
---|
1494 | function dumpop($op, &$EX) // {{{ |
---|
1495 | { |
---|
1496 | $op1 = $op['op1']; |
---|
1497 | $op2 = $op['op2']; |
---|
1498 | $d = array('opname' => xcache_get_opcode($op['opcode']), 'opcode' => $op['opcode']); |
---|
1499 | |
---|
1500 | foreach (array('op1' => 'op1', 'op2' => 'op2', 'result' => 'res') as $k => $kk) { |
---|
1501 | switch ($op[$k]['op_type']) { |
---|
1502 | case XC_IS_UNUSED: |
---|
1503 | $d[$kk] = '*UNUSED* ' . $op[$k]['opline_num']; |
---|
1504 | break; |
---|
1505 | |
---|
1506 | case XC_IS_VAR: |
---|
1507 | $d[$kk] = '$' . $op[$k]['var']; |
---|
1508 | if ($kk != 'res') { |
---|
1509 | $d[$kk] .= ':' . $this->getOpVal($op[$k], $EX); |
---|
1510 | } |
---|
1511 | break; |
---|
1512 | |
---|
1513 | case XC_IS_TMP_VAR: |
---|
1514 | $d[$kk] = '#' . $op[$k]['var']; |
---|
1515 | if ($kk != 'res') { |
---|
1516 | $d[$kk] .= ':' . $this->getOpVal($op[$k], $EX); |
---|
1517 | } |
---|
1518 | break; |
---|
1519 | |
---|
1520 | case XC_IS_CV: |
---|
1521 | $d[$kk] = $this->getOpVal($op[$k], $EX); |
---|
1522 | break; |
---|
1523 | |
---|
1524 | default: |
---|
1525 | if ($kk == 'res') { |
---|
1526 | var_dump($op); |
---|
1527 | exit; |
---|
1528 | assert(0); |
---|
1529 | } |
---|
1530 | else { |
---|
1531 | $d[$kk] = $this->getOpVal($op[$k], $EX); |
---|
1532 | } |
---|
1533 | } |
---|
1534 | } |
---|
1535 | $d['ext'] = $op['extended_value']; |
---|
1536 | |
---|
1537 | var_dump($d); |
---|
1538 | } |
---|
1539 | // }}} |
---|
1540 | function dargs(&$EX, $indent) // {{{ |
---|
1541 | { |
---|
1542 | $op_array = &$EX['op_array']; |
---|
1543 | |
---|
1544 | if (isset($op_array['num_args'])) { |
---|
1545 | $c = $op_array['num_args']; |
---|
1546 | } |
---|
1547 | else if ($op_array['arg_types']) { |
---|
1548 | $c = count($op_array['arg_types']); |
---|
1549 | } |
---|
1550 | else { |
---|
1551 | // php4 |
---|
1552 | $c = count($EX['recvs']); |
---|
1553 | } |
---|
1554 | |
---|
1555 | $refrest = false; |
---|
1556 | for ($i = 0; $i < $c; $i ++) { |
---|
1557 | if ($i) { |
---|
1558 | echo ', '; |
---|
1559 | } |
---|
1560 | if (isset($op_array['arg_info'])) { |
---|
1561 | $ai = $op_array['arg_info'][$i]; |
---|
1562 | if (!empty($ai['class_name'])) { |
---|
1563 | echo $ai['class_name'], ' '; |
---|
1564 | if ($ai['allow_null']) { |
---|
1565 | echo 'or NULL '; |
---|
1566 | } |
---|
1567 | } |
---|
1568 | else if (!empty($ai['array_type_hint'])) { |
---|
1569 | echo 'array '; |
---|
1570 | if ($ai['allow_null']) { |
---|
1571 | echo 'or NULL '; |
---|
1572 | } |
---|
1573 | } |
---|
1574 | if ($ai['pass_by_reference']) { |
---|
1575 | echo '&'; |
---|
1576 | } |
---|
1577 | printf("\$%s", $ai['name']); |
---|
1578 | } |
---|
1579 | else { |
---|
1580 | if ($refrest) { |
---|
1581 | echo '&'; |
---|
1582 | } |
---|
1583 | else if (isset($op_array['arg_types'][$i])) { |
---|
1584 | switch ($op_array['arg_types'][$i]) { |
---|
1585 | case BYREF_FORCE_REST: |
---|
1586 | $refrest = true; |
---|
1587 | /* fall */ |
---|
1588 | case BYREF_FORCE: |
---|
1589 | echo '&'; |
---|
1590 | break; |
---|
1591 | |
---|
1592 | case BYREF_NONE: |
---|
1593 | case BYREF_ALLOW: |
---|
1594 | break; |
---|
1595 | default: |
---|
1596 | assert(0); |
---|
1597 | } |
---|
1598 | } |
---|
1599 | $arg = $EX['recvs'][$i + 1]; |
---|
1600 | echo toCode($arg[0], $indent); |
---|
1601 | if (isset($arg[1])) { |
---|
1602 | echo ' = ', toCode($arg[1], $indent); |
---|
1603 | } |
---|
1604 | } |
---|
1605 | } |
---|
1606 | } |
---|
1607 | // }}} |
---|
1608 | function dfunction($func, $indent = '', $nobody = false) // {{{ |
---|
1609 | { |
---|
1610 | if ($nobody) { |
---|
1611 | $body = ";\n"; |
---|
1612 | $EX = array(); |
---|
1613 | $EX['op_array'] = &$func['op_array']; |
---|
1614 | $EX['recvs'] = array(); |
---|
1615 | } |
---|
1616 | else { |
---|
1617 | ob_start(); |
---|
1618 | $newindent = INDENT . $indent; |
---|
1619 | $EX = &$this->dop_array($func['op_array'], $newindent); |
---|
1620 | $body = ob_get_clean(); |
---|
1621 | if (!isset($EX['recvs'])) { |
---|
1622 | $EX['recvs'] = array(); |
---|
1623 | } |
---|
1624 | } |
---|
1625 | |
---|
1626 | echo 'function ', $func['op_array']['function_name'], '('; |
---|
1627 | $this->dargs($EX, $indent); |
---|
1628 | echo ")\n"; |
---|
1629 | echo $indent, "{\n"; |
---|
1630 | echo $body; |
---|
1631 | echo "$indent}\n"; |
---|
1632 | } |
---|
1633 | // }}} |
---|
1634 | function dclass($class, $indent = '') // {{{ |
---|
1635 | { |
---|
1636 | // {{{ class decl |
---|
1637 | if (!empty($class['doc_comment'])) { |
---|
1638 | echo $indent; |
---|
1639 | echo $class['doc_comment']; |
---|
1640 | echo "\n"; |
---|
1641 | } |
---|
1642 | $isinterface = false; |
---|
1643 | if (!empty($class['ce_flags'])) { |
---|
1644 | if ($class['ce_flags'] & ZEND_ACC_INTERFACE) { |
---|
1645 | echo 'interface '; |
---|
1646 | $isinterface = true; |
---|
1647 | } |
---|
1648 | else { |
---|
1649 | if ($class['ce_flags'] & ZEND_ACC_IMPLICIT_ABSTRACT) { |
---|
1650 | echo "abstract "; |
---|
1651 | } |
---|
1652 | if ($class['ce_flags'] & ZEND_ACC_FINAL) { |
---|
1653 | echo "final "; |
---|
1654 | } |
---|
1655 | } |
---|
1656 | } |
---|
1657 | echo 'class ', $class['name']; |
---|
1658 | if ($class['parent']) { |
---|
1659 | echo ' extends ', $class['parent']; |
---|
1660 | } |
---|
1661 | /* TODO */ |
---|
1662 | if (!empty($class['interfaces'])) { |
---|
1663 | echo ' implements '; |
---|
1664 | echo implode(', ', $class['interfaces']); |
---|
1665 | } |
---|
1666 | echo "\n"; |
---|
1667 | echo $indent, "{"; |
---|
1668 | // }}} |
---|
1669 | $newindent = INDENT . $indent; |
---|
1670 | // {{{ const, static |
---|
1671 | foreach (array('constants_table' => 'const ' |
---|
1672 | , 'static_members' => 'static $') as $type => $prefix) { |
---|
1673 | if (!empty($class[$type])) { |
---|
1674 | echo "\n"; |
---|
1675 | // TODO: skip shadow? |
---|
1676 | foreach ($class[$type] as $name => $v) { |
---|
1677 | echo $newindent; |
---|
1678 | echo $prefix, $name, ' = '; |
---|
1679 | echo toCode(value($v), $newindent); |
---|
1680 | echo ";\n"; |
---|
1681 | } |
---|
1682 | } |
---|
1683 | } |
---|
1684 | // }}} |
---|
1685 | // {{{ properties |
---|
1686 | $member_variables = isset($class['properties_info']) ? $class['properties_info'] : ($class['default_static_members'] + $class['default_properties']); |
---|
1687 | if ($member_variables) { |
---|
1688 | echo "\n"; |
---|
1689 | $infos = !empty($class['properties_info']) ? $class['properties_info'] : null; |
---|
1690 | foreach ($member_variables as $name => $dummy) { |
---|
1691 | $info = (isset($infos) && isset($infos[$name])) ? $infos[$name] : null; |
---|
1692 | if (isset($info)) { |
---|
1693 | if (!empty($info['doc_comment'])) { |
---|
1694 | echo $newindent; |
---|
1695 | echo $info['doc_comment']; |
---|
1696 | echo "\n"; |
---|
1697 | } |
---|
1698 | } |
---|
1699 | |
---|
1700 | echo $newindent; |
---|
1701 | $static = false; |
---|
1702 | if (isset($info)) { |
---|
1703 | if ($info['flags'] & ZEND_ACC_STATIC) { |
---|
1704 | $static = true; |
---|
1705 | } |
---|
1706 | } |
---|
1707 | else if (isset($class['default_static_members'][$name])) { |
---|
1708 | $static = true; |
---|
1709 | } |
---|
1710 | |
---|
1711 | if ($static) { |
---|
1712 | echo "static "; |
---|
1713 | } |
---|
1714 | |
---|
1715 | $mangled = false; |
---|
1716 | if (!ZEND_ENGINE_2) { |
---|
1717 | echo 'var '; |
---|
1718 | } |
---|
1719 | else if (!isset($info)) { |
---|
1720 | echo 'public '; |
---|
1721 | } |
---|
1722 | else { |
---|
1723 | if ($info['flags'] & ZEND_ACC_SHADOW) { |
---|
1724 | continue; |
---|
1725 | } |
---|
1726 | switch ($info['flags'] & ZEND_ACC_PPP_MASK) { |
---|
1727 | case ZEND_ACC_PUBLIC: |
---|
1728 | echo "public "; |
---|
1729 | break; |
---|
1730 | case ZEND_ACC_PRIVATE: |
---|
1731 | echo "private "; |
---|
1732 | $mangled = true; |
---|
1733 | break; |
---|
1734 | case ZEND_ACC_PROTECTED: |
---|
1735 | echo "protected "; |
---|
1736 | $mangled = true; |
---|
1737 | break; |
---|
1738 | } |
---|
1739 | } |
---|
1740 | |
---|
1741 | echo '$', $name; |
---|
1742 | |
---|
1743 | if (isset($info['offset'])) { |
---|
1744 | $value = $class[$static ? 'default_static_members_table' : 'default_properties_table'][$info['offset']]; |
---|
1745 | } |
---|
1746 | else { |
---|
1747 | $key = isset($info) ? $info['name'] . ($mangled ? "\000" : "") : $name; |
---|
1748 | |
---|
1749 | $value = $class[$static ? 'default_static_members' : 'default_properties'][$key]; |
---|
1750 | } |
---|
1751 | if (isset($value)) { |
---|
1752 | echo ' = '; |
---|
1753 | echo toCode(value($value), $newindent); |
---|
1754 | } |
---|
1755 | echo ";\n"; |
---|
1756 | } |
---|
1757 | } |
---|
1758 | // }}} |
---|
1759 | // {{{ function_table |
---|
1760 | if (isset($class['function_table'])) { |
---|
1761 | foreach ($class['function_table'] as $func) { |
---|
1762 | if (!isset($func['scope']) || $func['scope'] == $class['name']) { |
---|
1763 | // TODO: skip shadow here |
---|
1764 | echo "\n"; |
---|
1765 | $opa = $func['op_array']; |
---|
1766 | if (!empty($opa['doc_comment'])) { |
---|
1767 | echo $newindent; |
---|
1768 | echo $opa['doc_comment']; |
---|
1769 | echo "\n"; |
---|
1770 | } |
---|
1771 | echo $newindent; |
---|
1772 | if (isset($opa['fn_flags'])) { |
---|
1773 | if ($opa['fn_flags'] & ZEND_ACC_ABSTRACT) { |
---|
1774 | echo "abstract "; |
---|
1775 | } |
---|
1776 | if ($opa['fn_flags'] & ZEND_ACC_FINAL) { |
---|
1777 | echo "final "; |
---|
1778 | } |
---|
1779 | if ($opa['fn_flags'] & ZEND_ACC_STATIC) { |
---|
1780 | echo "static "; |
---|
1781 | } |
---|
1782 | |
---|
1783 | switch ($opa['fn_flags'] & ZEND_ACC_PPP_MASK) { |
---|
1784 | case ZEND_ACC_PUBLIC: |
---|
1785 | echo "public "; |
---|
1786 | break; |
---|
1787 | case ZEND_ACC_PRIVATE: |
---|
1788 | echo "private "; |
---|
1789 | break; |
---|
1790 | case ZEND_ACC_PROTECTED: |
---|
1791 | echo "protected "; |
---|
1792 | break; |
---|
1793 | default: |
---|
1794 | echo "<visibility error> "; |
---|
1795 | break; |
---|
1796 | } |
---|
1797 | } |
---|
1798 | $this->dfunction($func, $newindent, $isinterface); |
---|
1799 | if ($opa['function_name'] == 'Decompiler') { |
---|
1800 | //exit; |
---|
1801 | } |
---|
1802 | } |
---|
1803 | } |
---|
1804 | } |
---|
1805 | // }}} |
---|
1806 | echo $indent, "}\n"; |
---|
1807 | } |
---|
1808 | // }}} |
---|
1809 | function decompileString($string) // {{{ |
---|
1810 | { |
---|
1811 | $this->dc = xcache_dasm_string($string); |
---|
1812 | if ($this->dc === false) { |
---|
1813 | echo "error compling string\n"; |
---|
1814 | return false; |
---|
1815 | } |
---|
1816 | } |
---|
1817 | // }}} |
---|
1818 | function decompileFile($file) // {{{ |
---|
1819 | { |
---|
1820 | $this->dc = xcache_dasm_file($file); |
---|
1821 | if ($this->dc === false) { |
---|
1822 | echo "error compling $file\n"; |
---|
1823 | return false; |
---|
1824 | } |
---|
1825 | } |
---|
1826 | // }}} |
---|
1827 | function output() // {{{ |
---|
1828 | { |
---|
1829 | echo "<?". "php\n"; |
---|
1830 | foreach ($this->dc['class_table'] as $key => $class) { |
---|
1831 | if ($key{0} != "\0") { |
---|
1832 | echo "\n"; |
---|
1833 | $this->dclass($class); |
---|
1834 | } |
---|
1835 | } |
---|
1836 | |
---|
1837 | foreach ($this->dc['function_table'] as $key => $func) { |
---|
1838 | if ($key{0} != "\0") { |
---|
1839 | echo "\n"; |
---|
1840 | $this->dfunction($func); |
---|
1841 | } |
---|
1842 | } |
---|
1843 | |
---|
1844 | echo "\n"; |
---|
1845 | $this->dop_array($this->dc['op_array']); |
---|
1846 | echo "\n?" . ">\n"; |
---|
1847 | return true; |
---|
1848 | } |
---|
1849 | // }}} |
---|
1850 | } |
---|
1851 | |
---|
1852 | // {{{ defines |
---|
1853 | define('ZEND_ACC_STATIC', 0x01); |
---|
1854 | define('ZEND_ACC_ABSTRACT', 0x02); |
---|
1855 | define('ZEND_ACC_FINAL', 0x04); |
---|
1856 | define('ZEND_ACC_IMPLEMENTED_ABSTRACT', 0x08); |
---|
1857 | |
---|
1858 | define('ZEND_ACC_IMPLICIT_ABSTRACT_CLASS', 0x10); |
---|
1859 | define('ZEND_ACC_EXPLICIT_ABSTRACT_CLASS', 0x20); |
---|
1860 | define('ZEND_ACC_FINAL_CLASS', 0x40); |
---|
1861 | define('ZEND_ACC_INTERFACE', 0x80); |
---|
1862 | define('ZEND_ACC_PUBLIC', 0x100); |
---|
1863 | define('ZEND_ACC_PROTECTED', 0x200); |
---|
1864 | define('ZEND_ACC_PRIVATE', 0x400); |
---|
1865 | define('ZEND_ACC_PPP_MASK', (ZEND_ACC_PUBLIC | ZEND_ACC_PROTECTED | ZEND_ACC_PRIVATE)); |
---|
1866 | |
---|
1867 | define('ZEND_ACC_CHANGED', 0x800); |
---|
1868 | define('ZEND_ACC_IMPLICIT_PUBLIC', 0x1000); |
---|
1869 | |
---|
1870 | define('ZEND_ACC_CTOR', 0x2000); |
---|
1871 | define('ZEND_ACC_DTOR', 0x4000); |
---|
1872 | define('ZEND_ACC_CLONE', 0x8000); |
---|
1873 | |
---|
1874 | define('ZEND_ACC_ALLOW_STATIC', 0x10000); |
---|
1875 | |
---|
1876 | define('ZEND_ACC_SHADOW', 0x2000); |
---|
1877 | |
---|
1878 | define('ZEND_ENGINE_2_4', PHP_VERSION >= "5.3.99"); |
---|
1879 | define('ZEND_ENGINE_2_3', ZEND_ENGINE_2_4 || PHP_VERSION >= "5.3."); |
---|
1880 | define('ZEND_ENGINE_2_2', ZEND_ENGINE_2_3 || PHP_VERSION >= "5.2."); |
---|
1881 | define('ZEND_ENGINE_2_1', ZEND_ENGINE_2_2 || PHP_VERSION >= "5.1."); |
---|
1882 | define('ZEND_ENGINE_2', ZEND_ENGINE_2_1 || PHP_VERSION >= "5.0."); |
---|
1883 | |
---|
1884 | if (ZEND_ENGINE_2_4) { |
---|
1885 | define('ZEND_FETCH_GLOBAL', 0x00000000); |
---|
1886 | define('ZEND_FETCH_LOCAL', 0x10000000); |
---|
1887 | define('ZEND_FETCH_STATIC', 0x20000000); |
---|
1888 | define('ZEND_FETCH_STATIC_MEMBER', 0x30000000); |
---|
1889 | define('ZEND_FETCH_GLOBAL_LOCK', 0x40000000); |
---|
1890 | define('ZEND_FETCH_LEXICAL', 0x50000000); |
---|
1891 | |
---|
1892 | define('ZEND_FETCH_TYPE_MASK', 0x70000000); |
---|
1893 | } |
---|
1894 | else { |
---|
1895 | define('ZEND_FETCH_GLOBAL', 0); |
---|
1896 | define('ZEND_FETCH_LOCAL', 1); |
---|
1897 | define('ZEND_FETCH_STATIC', 2); |
---|
1898 | define('ZEND_FETCH_STATIC_MEMBER', 3); |
---|
1899 | define('ZEND_FETCH_GLOBAL_LOCK', 4); |
---|
1900 | } |
---|
1901 | |
---|
1902 | define('ZEND_FETCH_CLASS_DEFAULT', 0); |
---|
1903 | define('ZEND_FETCH_CLASS_SELF', 1); |
---|
1904 | define('ZEND_FETCH_CLASS_PARENT', 2); |
---|
1905 | define('ZEND_FETCH_CLASS_MAIN', 3); |
---|
1906 | define('ZEND_FETCH_CLASS_GLOBAL', 4); |
---|
1907 | define('ZEND_FETCH_CLASS_AUTO', 5); |
---|
1908 | define('ZEND_FETCH_CLASS_INTERFACE', 6); |
---|
1909 | define('ZEND_FETCH_CLASS_STATIC', 7); |
---|
1910 | if (ZEND_ENGINE_2_4) { |
---|
1911 | define('ZEND_FETCH_CLASS_TRAIT', 14); |
---|
1912 | } |
---|
1913 | if (ZEND_ENGINE_2_3) { |
---|
1914 | define('ZEND_FETCH_CLASS_MASK', 0xF); |
---|
1915 | } |
---|
1916 | |
---|
1917 | define('ZEND_EVAL', (1<<0)); |
---|
1918 | define('ZEND_INCLUDE', (1<<1)); |
---|
1919 | define('ZEND_INCLUDE_ONCE', (1<<2)); |
---|
1920 | define('ZEND_REQUIRE', (1<<3)); |
---|
1921 | define('ZEND_REQUIRE_ONCE', (1<<4)); |
---|
1922 | |
---|
1923 | define('ZEND_ISSET', (1<<0)); |
---|
1924 | define('ZEND_ISEMPTY', (1<<1)); |
---|
1925 | if (ZEND_ENGINE_2_4) { |
---|
1926 | define('EXT_TYPE_UNUSED', (1<<5)); |
---|
1927 | } |
---|
1928 | else { |
---|
1929 | define('EXT_TYPE_UNUSED', (1<<0)); |
---|
1930 | } |
---|
1931 | |
---|
1932 | define('ZEND_FETCH_STANDARD', 0); |
---|
1933 | define('ZEND_FETCH_ADD_LOCK', 1); |
---|
1934 | |
---|
1935 | define('ZEND_FE_FETCH_BYREF', 1); |
---|
1936 | define('ZEND_FE_FETCH_WITH_KEY', 2); |
---|
1937 | |
---|
1938 | define('ZEND_MEMBER_FUNC_CALL', 1<<0); |
---|
1939 | define('ZEND_CTOR_CALL', 1<<1); |
---|
1940 | |
---|
1941 | define('ZEND_ARG_SEND_BY_REF', (1<<0)); |
---|
1942 | define('ZEND_ARG_COMPILE_TIME_BOUND', (1<<1)); |
---|
1943 | define('ZEND_ARG_SEND_FUNCTION', (1<<2)); |
---|
1944 | |
---|
1945 | define('BYREF_NONE', 0); |
---|
1946 | define('BYREF_FORCE', 1); |
---|
1947 | define('BYREF_ALLOW', 2); |
---|
1948 | define('BYREF_FORCE_REST', 3); |
---|
1949 | define('IS_NULL', 0); |
---|
1950 | define('IS_LONG', 1); |
---|
1951 | define('IS_DOUBLE', 2); |
---|
1952 | define('IS_STRING', 3); |
---|
1953 | define('IS_ARRAY', 4); |
---|
1954 | define('IS_OBJECT', 5); |
---|
1955 | define('IS_BOOL', 6); |
---|
1956 | define('IS_RESOURCE', 7); |
---|
1957 | define('IS_CONSTANT', 8); |
---|
1958 | define('IS_CONSTANT_ARRAY', 9); |
---|
1959 | |
---|
1960 | @define('XC_IS_CV', 16); |
---|
1961 | |
---|
1962 | /* |
---|
1963 | if (preg_match_all('!XC_[A-Z_]+!', file_get_contents(__FILE__), $ms)) { |
---|
1964 | $verdiff = array(); |
---|
1965 | foreach ($ms[0] as $k) { |
---|
1966 | if (!defined($k)) { |
---|
1967 | $verdiff[$k] = -1; |
---|
1968 | define($k, -1); |
---|
1969 | } |
---|
1970 | } |
---|
1971 | var_export($verdiff); |
---|
1972 | } |
---|
1973 | /*/ |
---|
1974 | foreach (array ( |
---|
1975 | 'XC_HANDLE_EXCEPTION' => -1, |
---|
1976 | 'XC_FETCH_CLASS' => -1, |
---|
1977 | 'XC_FETCH_' => -1, |
---|
1978 | 'XC_FETCH_DIM_' => -1, |
---|
1979 | 'XC_ASSIGN_DIM' => -1, |
---|
1980 | 'XC_UNSET_DIM' => -1, |
---|
1981 | 'XC_FETCH_OBJ_' => -1, |
---|
1982 | 'XC_ASSIGN_OBJ' => -1, |
---|
1983 | 'XC_ISSET_ISEMPTY_DIM_OBJ' => -1, |
---|
1984 | 'XC_ISSET_ISEMPTY_PROP_OBJ' => -1, |
---|
1985 | 'XC_ISSET_ISEMPTY_VAR' => -1, |
---|
1986 | 'XC_INIT_STATIC_METHOD_CALL' => -1, |
---|
1987 | 'XC_INIT_METHOD_CALL' => -1, |
---|
1988 | 'XC_VERIFY_ABSTRACT_CLASS' => -1, |
---|
1989 | 'XC_DECLARE_CLASS' => -1, |
---|
1990 | 'XC_DECLARE_INHERITED_CLASS' => -1, |
---|
1991 | 'XC_DECLARE_INHERITED_CLASS_DELAYED' => -1, |
---|
1992 | 'XC_ADD_INTERFACE' => -1, |
---|
1993 | 'XC_POST_DEC_OBJ' => -1, |
---|
1994 | 'XC_POST_INC_OBJ' => -1, |
---|
1995 | 'XC_PRE_DEC_OBJ' => -1, |
---|
1996 | 'XC_PRE_INC_OBJ' => -1, |
---|
1997 | 'XC_UNSET_OBJ' => -1, |
---|
1998 | 'XC_JMP_NO_CTOR' => -1, |
---|
1999 | 'XC_FETCH_' => -1, |
---|
2000 | 'XC_FETCH_DIM_' => -1, |
---|
2001 | 'XC_UNSET_DIM_OBJ' => -1, |
---|
2002 | 'XC_FETCH_OBJ_' => -1, |
---|
2003 | 'XC_ISSET_ISEMPTY' => -1, |
---|
2004 | 'XC_INIT_FCALL_BY_FUNC' => -1, |
---|
2005 | 'XC_DO_FCALL_BY_FUNC' => -1, |
---|
2006 | 'XC_DECLARE_FUNCTION_OR_CLASS' => -1, |
---|
2007 | ) as $k => $v) { |
---|
2008 | if (!defined($k)) { |
---|
2009 | define($k, $v); |
---|
2010 | } |
---|
2011 | } |
---|
2012 | |
---|
2013 | /* XC_UNDEF XC_OP_DATA |
---|
2014 | $content = file_get_contents(__FILE__); |
---|
2015 | for ($i = 0; $opname = xcache_get_opcode($i); $i ++) { |
---|
2016 | if (!preg_match("/\\bXC_" . $opname . "\\b(?!')/", $content)) { |
---|
2017 | echo "not done ", $opname, "\n"; |
---|
2018 | } |
---|
2019 | } |
---|
2020 | // */ |
---|
2021 | // }}} |
---|
2022 | |
---|