|
|
|
<?php
|
|
|
|
|
|
|
|
define('INDENT', "\t");
|
|
|
|
ini_set('error_reporting', E_ALL);
|
|
|
|
|
|
|
|
function color($str, $color = 33)
|
|
|
|
{
|
|
|
|
return "\x1B[{$color}m$str\x1B[0m";
|
|
|
|
}
|
|
|
|
|
|
|
|
function str($code, $indent = '') // {{{
|
|
|
|
{
|
|
|
|
if (is_array($code)) {
|
|
|
|
$array = array();
|
|
|
|
foreach ($code as $key => $value) {
|
|
|
|
$array[$key] = str($value, $indent);
|
|
|
|
}
|
|
|
|
return $array;
|
|
|
|
}
|
|
|
|
if (is_object($code)) {
|
|
|
|
$code = foldToCode($code, $indent);
|
|
|
|
return $code->toCode($indent);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (string) $code;
|
|
|
|
}
|
|
|
|
// }}}
|
|
|
|
function foldToCode($src, $indent = '') // {{{ wrap or rewrap anything to Decompiler_Code
|
|
|
|
{
|
|
|
|
if (is_array($indent)) {
|
|
|
|
$indent = $indent['indent'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_object($src)) {
|
|
|
|
return new Decompiler_Code($src);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!method_exists($src, 'toCode')) {
|
|
|
|
var_dump($src);
|
|
|
|
exit('no toCode');
|
|
|
|
}
|
|
|
|
if (get_class($src) != 'Decompiler_Code') {
|
|
|
|
// rewrap it
|
|
|
|
$src = new Decompiler_Code($src->toCode($indent));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $src;
|
|
|
|
}
|
|
|
|
// }}}
|
|
|
|
function value($value) // {{{
|
|
|
|
{
|
|
|
|
$spec = xcache_get_special_value($value);
|
|
|
|
if (isset($spec)) {
|
|
|
|
$value = $spec;
|
|
|
|
if (!is_array($value)) {
|
|
|
|
// constant
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_a($value, 'Decompiler_Object')) {
|
|
|
|
// use as is
|
|
|
|
}
|
|
|
|
else if (is_array($value)) {
|
|
|
|
$value = new Decompiler_ConstArray($value);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$value = new Decompiler_Value($value);
|
|
|
|
}
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
// }}}
|
|
|
|
function unquoteName_($str, $asVariableName, $indent = '') // {{{
|
|
|
|
{
|
|
|
|
$str = str($str, $indent);
|
|
|
|
if (preg_match("!^'[\\w_][\\w\\d_\\\\]*'\$!", $str)) {
|
|
|
|
return str_replace('\\\\', '\\', substr($str, 1, -1));
|
|
|
|
}
|
|
|
|
else if ($asVariableName) {
|
|
|
|
return "{" . $str . "}";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return $str;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// }}}
|
|
|
|
function unquoteVariableName($str, $indent = '') // {{{
|
|
|
|
{
|
|
|
|
return unquoteName_($str, true, $indent);
|
|
|
|
}
|
|
|
|
// }}}
|
|
|
|
function unquoteName($str, $indent = '') // {{{
|
|
|
|
{
|
|
|
|
return unquoteName_($str, false, $indent);
|
|
|
|
}
|
|
|
|
// }}}
|
|
|
|
class Decompiler_Object // {{{
|
|
|
|
{
|
|
|
|
}
|
|
|
|
// }}}
|
|
|
|
class Decompiler_Value extends Decompiler_Object // {{{
|
|
|
|
{
|
|
|
|
var $value;
|
|
|
|
|
|
|
|
function Decompiler_Value($value = null)
|
|
|
|
{
|
|
|
|
$this->value = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
function toCode($indent)
|
|
|
|
{
|
|
|
|
$code = var_export($this->value, true);
|
|
|
|
if (gettype($this->value) == 'string') {
|
|
|
|
switch ($this->value) {
|
|
|
|
case "\r":
|
|
|
|
return '"\\r"';
|
|
|
|
case "\n":
|
|
|
|
return '"\\n"';
|
|
|
|
case "\r\n":
|
|
|
|
return '"\\r\\n"';
|
|
|
|
}
|
|
|
|
$code = str_replace("\r\n", '\' . "\\r\\n" . \'', $code);
|
|
|
|
$code = str_replace("\r", '\' . "\\r" . \'', $code);
|
|
|
|
$code = str_replace("\n", '\' . "\\n" . \'', $code);
|
|
|
|
}
|
|
|
|
return $code;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// }}}
|
|
|
|
class Decompiler_Code extends Decompiler_Object // {{{
|
|
|
|
{
|
|
|
|
var $src;
|
|
|
|
|
|
|
|
function Decompiler_Code($src)
|
|
|
|
{
|
|
|
|
assert('isset($src)');
|
|
|
|
$this->src = $src;
|
|
|
|
}
|
|
|
|
|
|
|
|
function toCode($indent)
|
|
|
|
{
|
|
|
|
return $this->src;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// }}}
|
|
|
|
class Decompiler_Binop extends Decompiler_Code // {{{
|
|
|
|
{
|
|
|
|
var $opc;
|
|
|
|
var $op1;
|
|
|
|
var $op2;
|
|
|
|
var $parent;
|
|
|
|
var $indent;
|
|
|
|
|
|
|
|
function Decompiler_Binop($parent, $op1, $opc, $op2)
|
|
|
|
{
|
|
|
|
$this->parent = &$parent;
|
|
|
|
$this->opc = $opc;
|
|
|
|
$this->op1 = $op1;
|
|
|
|
$this->op2 = $op2;
|
|
|
|
}
|
|
|
|
|
|
|
|
function toCode($indent)
|
|
|
|
{
|
|
|
|
$opstr = $this->parent->binops[$this->opc];
|
|
|
|
|
|
|
|
$op1 = foldToCode($this->op1, $indent);
|
|
|
|
if (is_a($this->op1, 'Decompiler_Binop') && $this->op1->opc != $this->opc) {
|
|
|
|
$op1 = "(" . str($op1, $indent) . ")";
|
|
|
|
}
|
|
|
|
$op2 = foldToCode($this->op2, $indent);
|
|
|
|
if (is_a($this->op2, 'Decompiler_Binop') && $this->op2->opc != $this->opc && substr($opstr, -1) != '=') {
|
|
|
|
$op2 = "(" . str($op2, $indent) . ")";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (str($op1) == '0' && ($this->opc == XC_ADD || $this->opc == XC_SUB)) {
|
|
|
|
return $opstr . str($op2, $indent);
|
|
|
|
}
|
|
|
|
|
|
|
|
return str($op1) . ' ' . $opstr . ' ' . str($op2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// }}}
|
|
|
|
class Decompiler_Fetch extends Decompiler_Code // {{{
|
|
|
|
{
|
|
|
|
var $src;
|
|
|
|
var $fetchType;
|
|
|
|
|
|
|
|
function Decompiler_Fetch($src, $type, $globalsrc)
|
|
|
|
{
|
|
|
|
$this->src = $src;
|
|
|
|
$this->fetchType = $type;
|
|
|
|
$this->globalsrc = $globalsrc;
|
|
|
|
}
|
|
|
|
|
|
|
|
function toCode($indent)
|
|
|
|
{
|
|
|
|
switch ($this->fetchType) {
|
|
|
|
case ZEND_FETCH_LOCAL:
|
|
|
|
return '$' . substr($this->src, 1, -1);
|
|
|
|
case ZEND_FETCH_STATIC:
|
|
|
|
if (ZEND_ENGINE_2_3) {
|
|
|
|
// closure local variable?
|
|
|
|
return str($this->src);
|
|
|
|
}
|
|
|
|
die('static fetch cant to string');
|
|
|
|
case ZEND_FETCH_GLOBAL:
|
|
|
|
case ZEND_FETCH_GLOBAL_LOCK:
|
|
|
|
return $this->globalsrc;
|
|
|
|
default:
|
|
|
|
var_dump($this->fetchType);
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// }}}
|
|
|
|
class Decompiler_Box // {{{
|
|
|
|
{
|
|
|
|
var $obj;
|
|
|
|
|
|
|
|
function Decompiler_Box(&$obj)
|
|
|
|
{
|
|
|
|
$this->obj = &$obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
function toCode($indent)
|
|
|
|
{
|
|
|
|
return $this->obj->toCode($indent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// }}}
|
|
|
|
class Decompiler_Dim extends Decompiler_Value // {{{
|
|
|
|
{
|
|
|
|
var $offsets = array();
|
|
|
|
var $isLast = false;
|
|
|
|
var $isObject = false;
|
|
|
|
var $assign = null;
|
|
|
|
|
|
|
|
function toCode($indent)
|
|
|
|
{
|
|
|
|
if (is_a($this->value, 'Decompiler_ListBox')) {
|
|
|
|
$exp = str($this->value->obj->src, $indent);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$exp = str($this->value, $indent);
|
|
|
|
}
|
|
|
|
$last = count($this->offsets) - 1;
|
|
|
|
foreach ($this->offsets as $i => $dim) {
|
|
|
|
if ($this->isObject && $i == $last) {
|
|
|
|
$exp .= '->' . unquoteVariableName($dim, $indent);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$exp .= '[' . str($dim, $indent) . ']';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $exp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// }}}
|
|
|
|
class Decompiler_DimBox extends Decompiler_Box // {{{
|
|
|
|
{
|
|
|
|
}
|
|
|
|
// }}}
|
|
|
|
class Decompiler_List extends Decompiler_Code // {{{
|
|
|
|
{
|
|
|
|
var $src;
|
|
|
|
var $dims = array();
|
|
|
|
var $everLocked = false;
|
|
|
|
|
|
|
|
function toCode($indent)
|
|
|
|
{
|
|
|
|
if (count($this->dims) == 1 && !$this->everLocked) {
|
|
|
|
$dim = $this->dims[0];
|
|
|
|
unset($dim->value);
|
|
|
|
$dim->value = $this->src;
|
|
|
|
if (!isset($dim->assign)) {
|
|
|
|
return str($dim, $indent);
|
|
|
|
}
|
|
|
|
return str($this->dims[0]->assign, $indent) . ' = ' . str($dim, $indent);
|
|
|
|
}
|
|
|
|
/* flatten dims */
|
|
|
|
$assigns = array();
|
|
|
|
foreach ($this->dims as $dim) {
|
|
|
|
$assign = &$assigns;
|
|
|
|
foreach ($dim->offsets as $offset) {
|
|
|
|
$assign = &$assign[$offset];
|
|
|
|
}
|
|
|
|
$assign = foldToCode($dim->assign, $indent);
|
|
|
|
}
|
|
|
|
return str($this->toList($assigns)) . ' = ' . str($this->src, $indent);
|
|
|
|
}
|
|
|
|
|
|
|
|
function toList($assigns)
|
|
|
|
{
|
|
|
|
$keys = array_keys($assigns);
|
|
|
|
if (count($keys) < 2) {
|
|
|
|
$keys[] = 0;
|
|
|
|
}
|
|
|
|
$max = call_user_func_array('max', $keys);
|
|
|
|
$list = 'list(';
|
|
|
|
for ($i = 0; $i <= $max; $i ++) {
|
|
|
|
if ($i) {
|
|
|
|
$list .= ', ';
|
|
|
|
}
|
|
|
|
if (!isset($assigns[$i])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (is_array($assigns[$i])) {
|
|
|
|
$list .= $this->toList($assigns[$i]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$list .= $assigns[$i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $list . ')';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// }}}
|
|
|
|
class Decompiler_ListBox extends Decompiler_Box // {{{
|
|
|
|
{
|
|
|
|
}
|
|
|
|
// }}}
|
|
|
|
class Decompiler_Array extends Decompiler_Value // {{{
|
|
|
|
{
|
|
|
|
// emenets
|
|
|
|
function Decompiler_Array()
|
|
|
|
{
|
|
|
|
$this->value = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
function toCode($indent)
|
|
|
|
{
|
|
|
|
$subindent = $indent . INDENT;
|
|
|
|
|
|
|
|
$elementsCode = array();
|
|
|
|
$index = 0;
|
|
|
|
foreach ($this->value as $element) {
|
|
|
|
list($key, $value) = $element;
|
|
|
|
if (!isset($key)) {
|
|
|
|
$key = $index++;
|
|
|
|
}
|
|
|
|
$elementsCode[] = array(str($key, $subindent), str($value, $subindent), $key, $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
$exp = "array(";
|
|
|
|
$indent = $indent . INDENT;
|
|
|
|
$assocWidth = 0;
|
|
|
|
$multiline = 0;
|
|
|
|
$i = 0;
|
|
|
|
foreach ($elementsCode as $element) {
|
|
|
|
list($keyCode, $valueCode) = $element;
|
|
|
|
if ((string) $i !== $keyCode) {
|
|
|
|
$assocWidth = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++$i;
|
|
|
|
}
|
|
|
|
foreach ($elementsCode as $element) {
|
|
|
|
list($keyCode, $valueCode, $key, $value) = $element;
|
|
|
|
if ($assocWidth) {
|
|
|
|
$len = strlen($keyCode);
|
|
|
|
if ($assocWidth < $len) {
|
|
|
|
$assocWidth = $len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (is_array($value) || is_a($value, 'Decompiler_Array')) {
|
|
|
|
$multiline ++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$i = 0;
|
|
|
|
foreach ($elementsCode as $element) {
|
|
|
|
list($keyCode, $value) = $element;
|
|
|
|
if ($multiline) {
|
|
|
|
if ($i) {
|
|
|
|
$exp .= ",";
|
|
|
|
}
|
|
|
|
$exp .= "\n";
|
|
|
|
$exp .= $indent;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if ($i) {
|
|
|
|
$exp .= ", ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($assocWidth) {
|
|
|
|
if ($multiline) {
|
|
|
|
$exp .= sprintf("%-{$assocWidth}s => ", $keyCode);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$exp .= $keyCode . ' => ';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$exp .= $value;
|
|
|
|
|
|
|
|
$i ++;
|
|
|
|
}
|
|
|
|
if ($multiline) {
|
|
|
|
$exp .= "\n$indent)";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$exp .= ")";
|
|
|
|
}
|
|
|
|
return $exp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// }}}
|
|
|
|
class Decompiler_ConstArray extends Decompiler_Array // {{{
|
|
|
|
{
|
|
|
|
function Decompiler_ConstArray($array)
|
|
|
|
{
|
|
|
|
$elements = array();
|
|
|
|
foreach ($array as $key => $value) {
|
|
|
|
$elements[] = array(value($key), value($value));
|
|
|
|
}
|
|
|
|
$this->value = $elements;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// }}}
|
|
|
|
class Decompiler_ForeachBox extends Decompiler_Box // {{{
|
|
|
|
{
|
|
|
|
var $iskey;
|
|
|
|
|
|
|
|
function toCode($indent)
|
|
|
|
{
|
|
|
|
return 'foreach (' . '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// }}}
|
|
|
|
|
|
|
|
class Decompiler
|
|
|
|
{
|
|
|
|
var $namespace;
|
|
|
|
var $namespaceDecided;
|
|
|
|
|
|
|
|
function Decompiler()
|
|
|
|
{
|
|
|
|
// {{{ testing
|
|
|
|
// XC_UNDEF XC_OP_DATA
|
|
|
|
$this->test = !empty($_ENV['XCACHE_DECOMPILER_TEST']);
|
|
|
|
$this->usedOps = array();
|
|
|
|
|
|
|
|
if ($this->test) {
|
|
|
|
$content = file_get_contents(__FILE__);
|
|
|
|
for ($i = 0; $opname = xcache_get_opcode($i); $i ++) {
|
|
|
|
if (!preg_match("/\\bXC_" . $opname . "\\b(?!')/", $content)) {
|
|
|
|
echo "not recognized opcode ", $opname, "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// }}}
|
|
|
|
// {{{ opinfo
|
|
|
|
$this->unaryops = array(
|
|
|
|
XC_BW_NOT => '~',
|
|
|
|
XC_BOOL_NOT => '!',
|
|
|
|
);
|
|
|
|
$this->binops = array(
|
|
|
|
XC_ADD => "+",
|
|
|
|
XC_ASSIGN_ADD => "+=",
|
|
|
|
XC_SUB => "-",
|
|
|
|
XC_ASSIGN_SUB => "-=",
|
|
|
|
XC_MUL => "*",
|
|
|
|
XC_ASSIGN_MUL => "*=",
|
|
|
|
XC_DIV => "/",
|
|
|
|
XC_ASSIGN_DIV => "/=",
|
|
|
|
XC_MOD => "%",
|
|
|
|
XC_ASSIGN_MOD => "%=",
|
|
|
|
XC_SL => "<<",
|
|
|
|
XC_ASSIGN_SL => "<<=",
|
|
|
|
XC_SR => ">>",
|
|
|
|
XC_ASSIGN_SR => ">>=",
|
|
|
|
XC_CONCAT => ".",
|
|
|
|
XC_ASSIGN_CONCAT => ".=",
|
|
|
|
XC_IS_IDENTICAL => "===",
|
|
|
|
XC_IS_NOT_IDENTICAL => "!==",
|
|
|
|
XC_IS_EQUAL => "==",
|
|
|
|
XC_IS_NOT_EQUAL => "!=",
|
|
|
|
XC_IS_SMALLER => "<",
|
|
|
|
XC_IS_SMALLER_OR_EQUAL => "<=",
|
|
|
|
XC_BW_OR => "|",
|
|
|
|
XC_ASSIGN_BW_OR => "|=",
|
|
|
|
XC_BW_AND => "&",
|
|
|
|
XC_ASSIGN_BW_AND => "&=",
|
|
|
|
XC_BW_XOR => "^",
|
|
|
|
XC_ASSIGN_BW_XOR => "^=",
|
|
|
|
XC_BOOL_XOR => "xor",
|
|
|
|
);
|
|
|
|
// }}}
|
|
|
|
$this->includeTypes = array( // {{{
|
|
|
|
ZEND_EVAL => 'eval',
|
|
|
|
ZEND_INCLUDE => 'include',
|
|
|
|
ZEND_INCLUDE_ONCE => 'include_once',
|
|
|
|
ZEND_REQUIRE => 'require',
|
|
|
|
ZEND_REQUIRE_ONCE => 'require_once',
|
|
|
|
);
|
|
|
|
// }}}
|
|
|
|
}
|
|
|
|
function detectNamespace($name) // {{{
|
|
|
|
{
|
|
|
|
if ($this->namespaceDecided) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strpos($name, '\\') !== false) {
|
|
|
|
$this->namespace = strtok($name, '\\');
|
|
|
|
echo 'namespace ', $this->namespace, ";\n\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->namespaceDecided = true;
|
|
|
|
}
|
|
|
|
// }}}
|
|
|
|
function stripNamespace($name) // {{{
|
|
|
|
{
|
|
|
|
$len = strlen($this->namespace) + 1;
|
|
|
|
if (substr($name, 0, $len) == $this->namespace . '\\') {
|
|
|
|
return substr($name, $len);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return $name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// }}}
|
|
|
|
function outputPhp(&$opcodes, $opline, $last, $indent) // {{{
|
|
|
|
{
|
|
|
|
$origindent = $indent;
|
|
|
|
$curticks = 0;
|
|
|
|
for ($i = $opline; $i <= $last; $i ++) {
|
|
|
|
$op = $opcodes[$i];
|
|
|
|
if (isset($op['php'])) {
|
|
|
|
$toticks = isset($op['ticks']) ? (int) str($op['ticks']) : 0;
|
|
|
|
if ($curticks != $toticks) {
|
|
|
|
$oldticks = $curticks;
|
|
|
|
$curticks = $toticks;
|
|
|
|
if (!$curticks) {
|
|
|
|
echo $origindent, "}\n\n";
|
|
|
|
$indent = $origindent;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if ($oldticks) {
|
|
|
|
echo $origindent, "}\n\n";
|
|
|
|
}
|
|
|
|
else if (!$oldticks) {
|
|
|
|
$indent .= INDENT;
|
|
|
|
}
|
|
|
|
echo $origindent, "declare (ticks=$curticks) {\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
echo $indent, str($op['php'], $indent), ";\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($curticks) {
|
|
|
|
echo $origindent, "}\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// }}}
|
|
|
|
function getOpVal($op, &$EX, $tostr = true, $free = false) // {{{
|
|
|
|
{
|
|
|
|
switch ($op['op_type']) {
|
|
|
|
case XC_IS_CONST:
|
|
|
|
return foldToCode(value($op['constant']), $EX);
|
|
|
|
|
|
|
|
case XC_IS_VAR:
|
|
|
|
case XC_IS_TMP_VAR:
|
|
|
|
$T = &$EX['Ts'];
|
|
|
|
$ret = $T[$op['var']];
|
|
|
|
if ($tostr) {
|
|
|
|
$ret = foldToCode($ret, $EX);
|
|
|
|
}
|
|
|
|
if ($free) {
|
|
|
|
unset($T[$op['var']]);
|
|
|
|
}
|
|
|
|
return $ret;
|
|
|
|
|
|
|
|
case XC_IS_CV:
|
|
|
|
$var = $op['var'];
|
|
|
|
$var = $EX['op_array']['vars'][$var];
|
|
|
|
return '$' . $var['name'];
|
|
|
|
|
|
|
|
case XC_IS_UNUSED:
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// }}}
|
|
|
|
function removeKeyPrefix($array, $prefix) // {{{
|
|
|
|
{
|
|
|
|
$prefixLen = strlen($prefix);
|
|
|
|
$ret = array();
|
|
|
|
foreach ($array as $key => $value) {
|
|
|
|
if (substr($key, 0, $prefixLen) == $prefix) {
|
|
|
|
$key = substr($key, $prefixLen);
|
|
|
|
}
|
|
|
|
$ret[$key] = $value;
|
|
|
|
}
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
// }}}
|
|
|
|
function &fixOpcode($opcodes, $removeTailing = false, $defaultReturnValue = null) // {{{
|
|
|
|
{
|
|
|
|
for ($i = 0, $cnt = count($opcodes); $i < $cnt; $i ++) {
|
|
|
|
if (function_exists('xcache_get_fixed_opcode')) {
|
|
|
|
$opcodes[$i]['opcode'] = xcache_get_fixed_opcode($opcodes[$i]['opcode'], $i);
|
|
|
|
}
|
|
|
|
if (isset($opcodes[$i]['op1'])) {
|
|
|
|
$opcodes[$i]['op1'] = $this->removeKeyPrefix($opcodes[$i]['op1'], 'u.');
|
|
|
|
$opcodes[$i]['op2'] = $this->removeKeyPrefix($opcodes[$i]['op2'], 'u.');
|
|
|
|
$opcodes[$i]['result'] = $this->removeKeyPrefix($opcodes[$i]['result'], 'u.');
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$op = array(
|
|
|
|
'op1' => array(),
|
|
|
|
'op2' => array(),
|
|
|
|
'op3' => array(),
|
|
|
|
);
|
|
|
|
foreach ($opcodes[$i] as $name => $value) {
|
|
|
|
if (preg_match('!^(op1|op2|result)\\.(.*)!', $name, $m)) {
|
|
|
|
list(, $which, $field) = $m;
|
|
|
|
$op[$which][$field] = $value;
|
|
|
|
}
|
|
|
|
else if (preg_match('!^(op1|op2|result)_type$!', $name, $m)) {
|
|
|
|
list(, $which) = $m;
|
|
|
|
$op[$which]['op_type'] = $value;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$op[$name] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$opcodes[$i] = $op;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($removeTailing) {
|
|
|
|
$last = count($opcodes) - 1;
|
|
|
|
if ($opcodes[$last]['opcode'] == XC_HANDLE_EXCEPTION) {
|
|
|
|
$this->usedOps[XC_HANDLE_EXCEPTION] = true;
|
|
|
|
$opcodes[$last]['opcode'] = XC_NOP;
|
|
|
|
--$last;
|
|
|
|
}
|
|
|
|
if ($opcodes[$last]['opcode'] == XC_RETURN) {
|
|
|
|
$op1 = $opcodes[$last]['op1'];
|
|
|
|
if ($op1['op_type'] == XC_IS_CONST && array_key_exists('constant', $op1) && $op1['constant'] === $defaultReturnValue) {
|
|
|
|
$opcodes[$last]['opcode'] = XC_NOP;
|
|
|
|
--$last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $opcodes;
|
|
|
|
}
|
|
|
|
// }}}
|
|
|
|
function &dop_array($op_array, $indent = '') // {{{
|
|
|
|
{
|
|
|
|
$op_array['opcodes'] = $this->fixOpcode($op_array['opcodes'], true, $indent == '' ? 1 : null);
|
|
|
|
$opcodes = &$op_array['opcodes'];
|
|
|
|
$EX['indent'] = '';
|
|
|
|
// {{{ build jmp array
|
|
|
|
for ($i = 0, $cnt = count($opcodes); $i < $cnt; $i ++) {
|
|
|
|
$op = &$opcodes[$i];
|
|
|
|
/*
|
|
|
|
if ($op['opcode'] == XC_JMPZ) {
|
|
|
|
$this->dumpop($op, $EX);
|
|
|
|
var_dump($op);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
*/
|
|
|
|
$op['line'] = $i;
|
|
|
|
switch ($op['opcode']) {
|
|
|
|
case XC_CONT:
|
|
|
|
case XC_BRK:
|
|
|
|
$op['jmpouts'] = array();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case XC_GOTO:
|
|
|
|
case XC_JMP:
|
|
|
|
$target = $op['op1']['var'];
|
|
|
|
$op['jmpouts'] = array($target);
|
|
|
|
$opcodes[$target]['jmpins'][] = $i;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case XC_JMPZNZ:
|
|
|
|
$jmpz = $op['op2']['opline_num'];
|
|
|
|
$jmpnz = $op['extended_value'];
|
|
|
|
$op['jmpouts'] = array($jmpz, $jmpnz);
|
|
|
|
$opcodes[$jmpz]['jmpins'][] = $i;
|
|
|
|
$opcodes[$jmpnz]['jmpins'][] = $i;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case XC_JMPZ:
|
|
|
|
case XC_JMPNZ:
|
|
|
|
case XC_JMPZ_EX:
|
|
|
|
case XC_JMPNZ_EX:
|
|
|
|
case XC_JMP_SET:
|
|
|
|
// case XC_FE_RESET:
|
|
|
|
case XC_FE_FETCH:
|
|
|
|
// case XC_JMP_NO_CTOR:
|
|
|
|
$target = $op['op2']['opline_num'];
|
|
|
|
//if (!isset($target)) {
|
|
|
|
// $this->dumpop($op, $EX);
|
|
|
|
// var_dump($op); exit;
|
|
|
|
//}
|
|
|
|
$op['jmpouts'] = array($target);
|
|
|
|
$opcodes[$target]['jmpins'][] = $i;
|
|
|
|
break;
|
|
|
|
|
|
|
|
/*
|
|
|
|
case XC_RETURN:
|
|
|
|
$op['jmpouts'] = array();
|
|
|
|
break;
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unset($op);
|
|
|
|
// }}}
|
|
|
|
// build semi-basic blocks
|
|
|
|
$nextbbs = array();
|
|
|
|
$starti = 0;
|
|
|
|
for ($i = 1, $cnt = count($opcodes); $i < $cnt; $i ++) {
|
|
|
|
if (isset($opcodes[$i]['jmpins'])
|
|
|
|
|| isset($opcodes[$i - 1]['jmpouts'])) {
|
|
|
|
$nextbbs[$starti] = $i;
|
|
|
|
$starti = $i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$nextbbs[$starti] = $cnt;
|
|
|
|
|
|
|
|
$EX = array();
|
|
|
|
$EX['Ts'] = array();
|
|
|
|
$EX['indent'] = $indent;
|
|
|
|
$EX['nextbbs'] = $nextbbs;
|
|
|
|
$EX['op_array'] = &$op_array;
|
|
|
|
$EX['opcodes'] = &$opcodes;
|
|
|
|
// func call
|
|
|
|
$EX['object'] = null;
|
|
|
|
$EX['called_scope'] = null;
|
|
|
|
$EX['fbc'] = null;
|
|
|
|
$EX['argstack'] = array();
|
|
|
|
$EX['arg_types_stack'] = array();
|
|
|
|
$EX['last'] = count($opcodes) - 1;
|
|
|
|
$EX['silence'] = 0;
|
|
|
|
|
|
|
|
for ($next = 0, $last = $EX['last'];
|
|
|
|
$loop = $this->outputCode($EX, $next, $last, $indent, true);
|
|
|
|
list($next, $last) = $loop) {
|
|
|
|
// empty
|
|
|
|
}
|
|
|
|
return $EX;
|
|
|
|
}
|
|
|
|
// }}}
|
|
|
|
function outputCode(&$EX, $opline, $last, $indent, $loop = false) // {{{
|
|
|
|
{
|
|
|
|
$op = &$EX['opcodes'][$opline];
|
|
|
|
$next = $EX['nextbbs'][$opline];
|
|
|
|
|
|
|
|
$end = $next - 1;
|
|
|
|
if ($end > $last) {
|
|
|
|
$end = $last;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($op['jmpins'])) {
|
|
|
|
echo "\nline", $op['line'], ":\n";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// echo ";;;\n";
|
|
|
|
}
|
|
|
|
$this->dasmBasicBlock($EX, $opline, $end);
|
|
|
|
$this->outputPhp($EX['opcodes'], $opline, $end, $indent);
|
|
|
|
// jmpout op
|
|
|
|
$op = &$EX['opcodes'][$end];
|
|
|
|
$op1 = $op['op1'];
|
|
|
|
$op2 = $op['op2'];
|
|
|
|
$ext = $op['extended_value'];
|
|
|
|
$line = $op['line'];
|
|
|
|
|
|
|
|
if (isset($EX['opcodes'][$next])) {
|
|
|
|
if (isset($last) && $next > $last) {
|
|
|
|
$next = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$next = null;
|
|
|
|
}
|
|
|
|
if ($op['opcode'] == XC_FE_FETCH) {
|
|
|
|
$opline = $next;
|
|
|
|
$next = $op['op2']['opline_num'];
|
|
|
|
$end = $next - 1;
|
|
|
|
|
|
|
|
ob_start();
|
|
|
|
$this->outputCode($EX, $opline, $end /* - 1 skip last jmp */, $indent . INDENT);
|
|
|
|
$body = ob_get_clean();
|
|
|
|
|
|
|
|
$as = foldToCode($op['fe_as'], $EX);
|
|
|
|
if (isset($op['fe_key'])) {
|
|
|
|
$as = str($op['fe_key'], $EX) . ' => ' . str($as);
|
|
|
|
}
|
|
|
|
echo "{$indent}foreach (" . str($op['fe_src'], $EX) . " as $as) {\n";
|
|
|
|
echo $body;
|
|
|
|
echo "{$indent}}";
|
|
|
|
// $this->outputCode($EX, $next, $last, $indent);
|
|
|
|
// return;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
if ($op['opcode'] == XC_JMPZ) {
|
|
|
|
$target = $op2['opline_num'];
|
|
|
|
if ($line + 1) {
|
|
|
|
$nextblock = $EX['nextbbs'][$next];
|
|
|
|
$jmpop = end($nextblock);
|
|
|
|
if ($jmpop['opcode'] == XC_JMP) {
|
|
|
|
$ifendline = $op2['opline_num'];
|
|
|
|
if ($ifendline >= $line) {
|
|
|
|
$cond = $op['cond'];
|
|
|
|
echo "{$indent}if ($cond) {\n";
|
|
|
|
$this->outputCode($EX, $next, $last, INDENT . $indent);
|
|
|
|
echo "$indent}\n";
|
|
|
|
$this->outputCode($EX, $target, $last, $indent);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
if (!isset($next)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (isset($op['jmpouts']) && isset($op['isjmp'])) {
|
|
|
|
if (isset($op['cond'])) {
|
|
|
|
echo "{$indent}check (" . str($op["cond"]) . ") {\n";
|
|
|
|
echo INDENT;
|
|
|
|
}
|
|
|
|
switch ($op['opcode']) {
|
|
|
|
case XC_CONT:
|
|
|
|
case XC_BRK:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case XC_GOTO:
|
|
|
|
echo $indent, 'goto', ' line', $op['jmpouts'][0], ';', "\n";
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
echo $indent;
|
|
|
|
echo xcache_get_opcode($op['opcode']), ' line', $op['jmpouts'][0];
|
|
|
|
if (isset($op['jmpouts'][1])) {
|
|
|
|
echo ', line', $op['jmpouts'][1];
|
|
|
|
}
|
|
|
|
echo ";";
|
|
|
|
// echo ' // <- line', $op['line'];
|
|
|
|
echo "\n";
|
|
|
|
}
|
|
|
|
if (isset($op['cond'])) echo "$indent}\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
// proces JMPZ_EX/JMPNZ_EX for AND,OR
|
|
|
|
$op = &$EX['opcodes'][$next];
|
|
|
|
/*
|
|
|
|
if (isset($op['jmpins'])) {
|
|
|
|
foreach (array_reverse($op['jmpins']) as $fromline) {
|
|
|
|
$fromop = $EX['opcodes'][$fromline];
|
|
|
|
switch ($fromop['opcode']) {
|
|
|
|
case XC_JMPZ_EX: $opstr = 'and'; break;
|
|
|
|
case XC_JMPNZ_EX: $opstr = 'or'; break;
|
|
|
|
case XC_JMPZNZ: var_dump($fromop); exit;
|
|
|
|
default: continue 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
$var = $fromop['result']['var'];
|
|
|
|
var_dump($EX['Ts'][$var]);
|
|
|
|
$EX['Ts'][$var] = '(' . $fromop['and_or'] . " $opstr " . $EX['Ts'][$var] . ')';
|
|
|
|
}
|
|
|
|
#$this->outputCode($EX, $next, $last, $indent);
|
|
|
|
#return;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
if (isset($op['cond_false'])) {
|
|
|
|
// $this->dumpop($op, $EX);
|
|
|
|
// any true comes here, so it's a "or"
|
|
|
|
$cond = implode(' and ', str($op['cond_false']));
|
|
|
|
// var_dump($op['cond'] = $cond);
|
|
|
|
/*
|
|
|
|
$rvalue = implode(' or ', $op['cond_true']) . ' or ' . $rvalue;
|
|
|
|
unset($op['cond_true']);
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($loop) {
|
|
|
|
return array($next, $last);
|
|
|
|
}
|
|
|
|
$this->outputCode($EX, $next, $last, $indent);
|
|
|
|
}
|
|
|
|
// }}}
|
|
|
|
function dasmBasicBlock(&$EX, $opline, $last) // {{{
|
|
|
|
{
|
|
|
|
$T = &$EX['Ts'];
|
|
|
|
$opcodes = &$EX['opcodes'];
|
|
|
|
$lastphpop = null;
|
|
|
|
|
|
|
|
for ($i = $opline, $ic = $last + 1; $i < $ic; $i ++) {
|
|
|
|
// {{{ prepair
|
|
|
|
$op = &$opcodes[$i];
|
|
|
|
$opc = $op['opcode'];
|
|
|
|
if ($opc == XC_NOP) {
|
|
|
|
$this->usedOps[$opc] = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$op1 = $op['op1'];
|
|
|
|
$op2 = $op['op2'];
|
|
|
|
$res = $op['result'];
|
|
|
|
$ext = $op['extended_value'];
|
|
|
|
|
|
|
|
$opname = xcache_get_opcode($opc);
|
|
|
|
|
|
|
|
if ($opname == 'UNDEF' || !isset($opname)) {
|
|
|
|
echo 'UNDEF OP:';
|
|
|
|
$this->dumpop($op, $EX);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// echo $i, ' '; $this->dumpop($op, $EX); //var_dump($op);
|
|
|
|
|
|
|
|
$resvar = null;
|
|
|
|
if ((ZEND_ENGINE_2_4 ? ($res['op_type'] & EXT_TYPE_UNUSED) : ($res['EA.type'] & EXT_TYPE_UNUSED)) || $res['op_type'] == XC_IS_UNUSED) {
|
|
|
|
$istmpres = false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$istmpres = true;
|
|
|
|
}
|
|
|
|
// }}}
|
|
|
|
// echo $opname, "\n";
|
|
|
|
|
|
|
|
$call = array(&$this, $opname);
|
|
|
|
if (is_callable($call)) {
|
|
|
|
$this->usedOps[$opc] = true;
|
|
|
|
$this->{$opname}($op, $EX);
|
|
|
|
}
|
|
|
|
else if (isset($this->binops[$opc])) { // {{{
|
|
|
|
$this->usedOps[$opc] = true;
|
|
|
|
$op1val = $this->getOpVal($op1, $EX, false);
|
|
|
|
$op2val = $this->getOpVal($op2, $EX, false);
|
|
|
|
$rvalue = new Decompiler_Binop($this, $op1val, $opc, $op2val);
|
|
|
|
$resvar = $rvalue;
|
|
|
|
// }}}
|
|
|
|
}
|
|
|
|
else if (isset($this->unaryops[$opc])) { // {{{
|
|
|
|
$this->usedOps[$opc] = true;
|
|
|
|
$op1val = $this->getOpVal($op1, $EX);
|
|
|
|
$myop = $this->unaryops[$opc];
|
|
|
|
$rvalue = $myop . str($op1val);
|
|
|
|
$resvar = $rvalue;
|
|
|
|
// }}}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$covered = true;
|
|
|
|
switch ($opc) {
|
|
|
|
case XC_NEW: // {{{
|
|
|
|
array_push($EX['arg_types_stack'], array($EX['fbc'], $EX['object'], $EX['called_scope']));
|
|
|
|
$EX['object'] = (int) $res['var'];
|
|
|
|
$EX['called_scope'] = null;
|
|
|
|
$EX['fbc'] = 'new ' . unquoteName($this->getOpVal($op1, $EX), $EX);
|
|
|
|
if (!ZEND_ENGINE_2) {
|
|
|
|
$resvar = '$new object$';
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
// }}}
|
|
|
|
case XC_THROW: // {{{
|
|
|
|
$resvar = 'throw ' . str($this->getOpVal($op1, $EX));
|
|
|
|
break;
|
|
|
|
// }}}
|
|
|
|
case XC_CLONE: // {{{
|
|
|
|
$resvar = 'clone ' . str($this->getOpVal($op1, $EX));
|
|
|
|
break;
|
|
|
|
// }}}
|
|
|
|
case XC_CATCH: // {{{
|
|
|
|
$resvar = 'catch (' . str($this->getOpVal($op1, $EX)) . ' ' . str($this->getOpVal($op2, $EX)) . ')';
|
|
|
|
break;
|
|
|
|
// }}}
|
|
|
|
case XC_INSTANCEOF: // {{{
|
|
|
|
$resvar = str($this->getOpVal($op1, $EX)) . ' instanceof ' . str($this->getOpVal($op2, $EX));
|
|
|
|
break;
|
|
|
|
// }}}
|
|
|
|
case XC_FETCH_CLASS: // {{{
|
|
|
|
if ($op2['op_type'] == XC_IS_UNUSED) {
|
|
|
|
switch (($ext & (defined('ZEND_FETCH_CLASS_MASK') ? ZEND_FETCH_CLASS_MASK : 0xFF))) {
|
|
|
|
case ZEND_FETCH_CLASS_SELF:
|
|
|
|
$class = 'self';
|
|
|
|
break;
|
|
|
|
case ZEND_FETCH_CLASS_PARENT:
|
|
|
|
$class = 'parent';
|
|
|
|
break;
|
|
|
|
case ZEND_FETCH_CLASS_STATIC:
|
|
|
|
$class = 'static';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$istmpres = true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$class = $this->getOpVal($op2, $EX);
|
|
|
|
if (isset($op2['constant'])) {
|
|
|
|
$class = $this->stripNamespace(unquoteName($class));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$resvar = $class;
|
|
|
|
break;
|
|
|
|
// }}}
|
|
|
|
case XC_FETCH_CONSTANT: // {{{
|
|
|
|
if ($op1['op_type'] == XC_IS_UNUSED) {
|
|
|
|
|