Skip to content

Commit

Permalink
feat: add first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed Oct 10, 2017
0 parents commit b680ae8
Show file tree
Hide file tree
Showing 12 changed files with 1,137 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitignore
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
1 change: 1 addition & 0 deletions a.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

19 changes: 19 additions & 0 deletions composer.json
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"
}
}
13 changes: 13 additions & 0 deletions run.php
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();
310 changes: 310 additions & 0 deletions src/Constant.php

Large diffs are not rendered by default.

100 changes: 100 additions & 0 deletions src/Context.php
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;
}
}
109 changes: 109 additions & 0 deletions src/Decompile.php
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;
}
}
51 changes: 51 additions & 0 deletions src/Xdr/Atom.php
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;
}
}
Loading

0 comments on commit b680ae8

Please sign in to comment.