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