forked from irelance/jsc-decompile-mozjs-34
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit b680ae8
Showing
12 changed files
with
1,137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
js/ | ||
compile | ||
libmozglue.dylib | ||
libmozjs-52.dylib | ||
.idea/ | ||
|
||
/vendor/ | ||
composer.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"name": "irelance/jsc-decompile-mozjs-34", | ||
"license": "proprietary", | ||
"type": "project", | ||
"authors": [ | ||
{ | ||
"name": "irelance", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"autoload": { | ||
"psr-4": { | ||
"Irelance\\Mozjs34\\": "src" | ||
} | ||
}, | ||
"require": { | ||
"php": ">=7.0.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: irelance | ||
* Date: 2017/10/1 | ||
* Time: 上午10:47 | ||
*/ | ||
include 'vendor/autoload.php'; | ||
|
||
|
||
$decompile = new Irelance\Mozjs34\Decompile($argv[1]); | ||
$decompile->run(); | ||
$decompile->printOpcodes(); |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?php | ||
|
||
/** | ||
* Created by PhpStorm. | ||
* User: irelance | ||
* Date: 2017/10/1 | ||
* Time: 下午3:25 | ||
*/ | ||
namespace Irelance\Mozjs34; | ||
|
||
class Context | ||
{ | ||
protected $summaries = []; | ||
protected $operations = []; | ||
protected $nodes = []; | ||
protected $atoms = []; | ||
protected $consts = []; | ||
protected $objects = []; | ||
protected $regexps = []; | ||
protected $tryNotes = []; | ||
protected $scopeNotes = []; | ||
protected $hasLazyScript; | ||
|
||
public function addSummary($key, $value) | ||
{ | ||
if (isset($this->summaries[$key])) { | ||
return false; | ||
} | ||
$this->summaries[$key] = $value; | ||
return true; | ||
} | ||
|
||
public function addOperation($op, array $bytes) | ||
{ | ||
$this->operations[] = ['id' => $op, 'params' => $bytes]; | ||
} | ||
|
||
public function addNode($node) | ||
{ | ||
$this->nodes[] = $node; | ||
} | ||
|
||
public function addAtom($atom) | ||
{ | ||
$this->atoms[] = $atom; | ||
} | ||
|
||
public function addConst($const) | ||
{ | ||
$this->consts[] = $const; | ||
} | ||
|
||
public function addObject($classKind, array $extra) | ||
{ | ||
$this->objects[] = array_merge($extra, ['classKind' => $classKind]); | ||
} | ||
|
||
public function addRegexp($source, $flagsword) | ||
{ | ||
$this->regexps[]=[ | ||
'source'=>$source, | ||
'flagsword'=>$flagsword, | ||
]; | ||
} | ||
|
||
public function addTryNote($kind, $stackDepth, $start, $length) | ||
{ | ||
$this->tryNotes[] = ['kind' => $kind, 'stackDepth' => $stackDepth, 'start' => $start, 'length' => $length]; | ||
} | ||
|
||
public function addScopeNote($index, $start, $length, $parent) | ||
{ | ||
$this->tryNotes[$index] = ['parent' => $parent, 'start' => $start, 'length' => $length]; | ||
} | ||
|
||
public function addHasLazyScript($packedFields) | ||
{ | ||
$this->hasLazyScript = $packedFields; | ||
} | ||
|
||
public function getSummary($key) | ||
{ | ||
return $this->summaries[$key]; | ||
} | ||
|
||
public function getSummaries() | ||
{ | ||
return $this->summaries; | ||
} | ||
|
||
public function getOperations() | ||
{ | ||
return $this->operations; | ||
} | ||
|
||
public function getAtoms() | ||
{ | ||
return $this->atoms; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?php | ||
|
||
/** | ||
* Created by PhpStorm. | ||
* User: irelance | ||
* Date: 2017/9/20 | ||
* Time: 上午8:04 | ||
* | ||
* js/src/jsscript.h | ||
* js/src/vm/Xdr.h | ||
*/ | ||
namespace Irelance\Mozjs34; | ||
|
||
class Decompile | ||
{ | ||
use Xdr\Common; | ||
use Xdr\Script; | ||
use Xdr\Atom; | ||
use Xdr\Object; | ||
use Xdr\Scope; | ||
private $fp; | ||
private $CRLF; | ||
protected $parseIndex = 0; | ||
|
||
protected $buildId = ''; | ||
protected $contexts = []; | ||
|
||
public $bytecodes = []; | ||
public $bytecodeLength = 0; | ||
|
||
public function __construct($filename) | ||
{ | ||
$this->fp = fopen($filename, 'rb'); | ||
$this->CRLF = php_sapi_name() == 'cli' ? "\n" : "<hr>"; | ||
$this->init(); | ||
} | ||
|
||
public function __destruct() | ||
{ | ||
fclose($this->fp); | ||
} | ||
|
||
public function init() | ||
{ | ||
$i = 0; | ||
while (!feof($this->fp)) { | ||
$c = fgetc($this->fp); | ||
$this->bytecodes[$i] = ord($c); | ||
$i++; | ||
} | ||
$this->bytecodeLength = count($this->bytecodes); | ||
} | ||
|
||
public function printOpcodes() | ||
{ | ||
/** @var Context $context * */ | ||
foreach ($this->contexts as $index => $context) { | ||
$opcodes = $context->getOperations(); | ||
echo '------------------' . $index . '------------------', $this->CRLF; | ||
foreach ($opcodes as $opcode) { | ||
$op = Constant::_Opcode[$opcode['id']]; | ||
echo $op['name'], ' ', $op['len'], ' ', $op['use'], ' ', $op['def'], ' :'; | ||
echo implode(', ', $opcode['params']), $this->CRLF; | ||
} | ||
echo '----------------------------------------', $this->CRLF; | ||
} | ||
} | ||
|
||
public function printAtoms() | ||
{ | ||
/** @var Context $context * */ | ||
foreach ($this->contexts as $index => $context) { | ||
$atoms = $context->getAtoms(); | ||
echo '------------------' . $index . '------------------', $this->CRLF; | ||
echo implode(' ', $atoms), $this->CRLF; | ||
echo '----------------------------------------', $this->CRLF; | ||
} | ||
} | ||
|
||
public function printSummaries() | ||
{ | ||
/** @var Context $context * */ | ||
foreach ($this->contexts as $index => $context) { | ||
$summaries = $context->getSummaries(); | ||
echo '------------------' . $index . '------------------', $this->CRLF; | ||
foreach ($summaries as $key => $val) { | ||
echo $key, ': ', $val, $this->CRLF; | ||
} | ||
echo '----------------------------------------', $this->CRLF; | ||
} | ||
} | ||
|
||
protected function parserVersion() | ||
{ | ||
$this->parseIndex = 0; | ||
$bytecodeVer = $this->todec(); | ||
return $bytecodeVer; | ||
} | ||
|
||
public function run() | ||
{ | ||
$this->parserVersion(); | ||
$this->XDRScript(); | ||
echo '----------------ByteCode---------------', $this->CRLF; | ||
echo 'file size :', $this->bytecodeLength, $this->CRLF; | ||
echo 'parse size :', $this->parseIndex, $this->CRLF; | ||
echo '---------------------------------------', $this->CRLF; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: irelance | ||
* Date: 2017/10/10 | ||
* Time: 上午11:30 | ||
*/ | ||
|
||
namespace Irelance\Mozjs34\Xdr; | ||
|
||
trait Atom | ||
{ | ||
protected function getLatin1Chars($length) | ||
{ | ||
$end = $this->parseIndex + $length; | ||
$atom = ''; | ||
for (; $this->parseIndex < $end; $this->parseIndex++) { | ||
$atom .= chr($this->bytecodes[$this->parseIndex]); | ||
} | ||
return $atom; | ||
} | ||
|
||
protected function getTwoByteChar() | ||
{ | ||
$char = '\u' . dechex($this->bytecodes[$this->parseIndex + 1]) . dechex($this->bytecodes[$this->parseIndex]); | ||
$this->parseIndex += 2; | ||
return $char; | ||
} | ||
|
||
protected function getTwoByteChars($length) | ||
{ | ||
$atom = ''; | ||
for ($i = 0; $i < $length; $i++) { | ||
$atom .= $this->getTwoByteChar(); | ||
} | ||
return json_decode('"' . $atom . '"'); | ||
} | ||
|
||
public function XDRAtom() | ||
{ | ||
$lengthAndEncoding = $this->todec(); | ||
$hasLatin1Chars = $lengthAndEncoding & 1; | ||
$length = $lengthAndEncoding >> 1; | ||
if ($hasLatin1Chars) { | ||
$atom = $this->getLatin1Chars($length); | ||
} else { | ||
$atom = $this->getTwoByteChars($length); | ||
} | ||
return $atom; | ||
} | ||
} |
Oops, something went wrong.