Decompyles classes and imports

This commit is contained in:
Michael Hansen
2009-07-27 03:00:55 +00:00
parent ef30d16b58
commit 835b391979
5 changed files with 177 additions and 36 deletions

View File

@@ -11,7 +11,10 @@ public:
enum Type {
NODE_INVALID, NODE_LIST, NODE_OBJECT, NODE_UNARY, NODE_BINARY,
NODE_COMPARE, NODE_STORE, NODE_RETURN, NODE_NAME, NODE_DELETE,
NODE_FUNCTION, NODE_CLASS, NODE_CALL, NODE_PASS
NODE_FUNCTION, NODE_CLASS, NODE_CALL, NODE_IMPORT, NODE_TUPLE,
// Empty nodes
NODE_PASS, NODE_LOCALS
};
ASTNode(int type = NODE_INVALID) : m_refs(0), m_type(type) { }
@@ -77,7 +80,8 @@ class ASTBinary : public ASTNode {
public:
enum BinOp {
BIN_POWER, BIN_MULTIPLY, BIN_DIVIDE, BIN_MODULO, BIN_ADD,
BIN_SUBTRACT, BIN_LSHIFT, BIN_RSHIFT, BIN_AND, BIN_XOR, BIN_OR
BIN_SUBTRACT, BIN_LSHIFT, BIN_RSHIFT, BIN_AND, BIN_XOR,
BIN_OR, BIN_ATTR
};
ASTBinary(PycRef<ASTNode> left, PycRef<ASTNode> right, int op,
@@ -141,16 +145,13 @@ private:
class ASTName : public ASTNode {
public:
typedef std::list<PycRef<PycString> > name_t;
ASTName(PycRef<PycString> name)
: ASTNode(NODE_NAME) { m_name.push_back(name); }
: ASTNode(NODE_NAME), m_name(name) { }
const name_t& name() const { return m_name; }
void add(PycRef<PycString> name) { m_name.push_back(name); }
PycRef<PycString> name() const { return m_name; }
private:
name_t m_name;
PycRef<PycString> m_name;
};
@@ -184,13 +185,17 @@ private:
class ASTClass : public ASTNode {
public:
ASTClass(PycRef<ASTNode> code)
: ASTNode(NODE_CLASS), m_code(code) { }
ASTClass(PycRef<ASTNode> code, PycRef<ASTNode> bases, PycRef<ASTNode> name)
: ASTNode(NODE_CLASS), m_code(code), m_bases(bases), m_name(name) { }
PycRef<ASTNode> code() const { return m_code; }
PycRef<ASTNode> bases() const { return m_bases; }
PycRef<ASTNode> name() const { return m_name; }
private:
PycRef<ASTNode> m_code;
PycRef<ASTNode> m_bases;
PycRef<ASTNode> m_name;
};
@@ -212,4 +217,32 @@ private:
kwparam_t m_kwparams;
};
class ASTImport : public ASTNode {
public:
ASTImport(PycRef<ASTNode> name, PycRef<ASTNode> fromlist)
: ASTNode(NODE_IMPORT), m_name(name), m_fromlist(fromlist) { }
PycRef<ASTNode> name() const { return m_name; }
PycRef<ASTNode> fromlist() const { return m_fromlist; }
private:
PycRef<ASTNode> m_name;
PycRef<ASTNode> m_fromlist;
};
class ASTTuple : public ASTNode {
public:
typedef std::vector<PycRef<ASTNode> > value_t;
ASTTuple(value_t values)
: ASTNode(NODE_TUPLE), m_values(values) { }
const value_t& values() const { return m_values; }
private:
value_t m_values;
};
#endif

View File

@@ -47,6 +47,18 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
stack.push(new ASTBinary(left, right, ASTBinary::BIN_AND));
}
break;
case (PY_1000 | Py1k::BUILD_CLASS):
case (PY_2000 | Py2k::BUILD_CLASS):
{
PycRef<ASTNode> code = stack.top();
stack.pop();
PycRef<ASTNode> bases = stack.top();
stack.pop();
PycRef<ASTNode> name = stack.top();
stack.pop();
stack.push(new ASTClass(code, bases, name));
}
break;
case (PY_1000 | Py1k::BUILD_FUNCTION):
{
PycRef<ASTNode> code = stack.top();
@@ -54,6 +66,19 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
stack.push(new ASTFunction(code, ASTFunction::defarg_t()));
}
break;
case (PY_1000 | Py1k::BUILD_TUPLE):
case (PY_2000 | Py2k::BUILD_TUPLE):
case (PY_3000 | Py3k::BUILD_TUPLE):
{
ASTTuple::value_t tuple;
tuple.resize(operand);
for (int i=0; i<operand; i++) {
tuple[i] = stack.top();
stack.pop();
}
stack.push(new ASTTuple(tuple));
}
break;
case (PY_1000 | Py1k::CALL_FUNCTION):
case (PY_2000 | Py2k::CALL_FUNCTION):
case (PY_3000 | Py3k::CALL_FUNCTION):
@@ -89,6 +114,35 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
stack.push(new ASTCompare(left, right, operand));
}
break;
case (PY_1000 | Py1k::IMPORT_NAME):
stack.push(new ASTImport(new ASTName(code->getName(operand)), Node_NULL));
break;
case (PY_2000 | Py2k::IMPORT_NAME):
{
PycRef<ASTNode> fromlist = stack.top();
stack.pop();
if (mod->minorVer() >= 5)
stack.pop(); // Level -- we don't care
stack.push(new ASTImport(new ASTName(code->getName(operand)), fromlist));
}
break;
case (PY_3000 | Py3k::IMPORT_NAME):
{
PycRef<ASTNode> fromlist = stack.top();
stack.pop();
stack.pop(); // Level -- we don't care
stack.push(new ASTImport(new ASTName(code->getName(operand)), fromlist));
}
break;
case (PY_1000 | Py1k::LOAD_ATTR):
case (PY_2000 | Py2k::LOAD_ATTR):
case (PY_3000 | Py3k::LOAD_ATTR):
{
PycRef<ASTNode> name = stack.top();
stack.pop();
stack.push(new ASTBinary(name, new ASTName(code->getName(operand)), ASTBinary::BIN_ATTR));
}
break;
case (PY_1000 | Py1k::LOAD_CONST):
case (PY_2000 | Py2k::LOAD_CONST):
case (PY_3000 | Py3k::LOAD_CONST):
@@ -107,6 +161,12 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
case (PY_1000 | Py1k::LOAD_GLOBAL):
case (PY_2000 | Py2k::LOAD_GLOBAL):
case (PY_3000 | Py3k::LOAD_GLOBAL):
stack.push(new ASTName(code->getName(operand)));
break;
case (PY_1000 | Py1k::LOAD_LOCALS):
case (PY_2000 | Py2k::LOAD_LOCALS):
stack.push(new ASTNode(ASTNode::NODE_LOCALS));
break;
case (PY_1000 | Py1k::LOAD_NAME):
case (PY_2000 | Py2k::LOAD_NAME):
case (PY_3000 | Py3k::LOAD_NAME):
@@ -174,11 +234,17 @@ void print_src(PycRef<ASTNode> node, PycModule* mod, int indent)
case ASTNode::NODE_COMPARE:
{
PycRef<ASTBinary> bin = node.cast<ASTBinary>();
printf("(");
print_src(bin->left(), mod);
printf(" %s ", bin->op_str());
print_src(bin->right(), mod);
printf(")");
if (bin->op() == ASTBinary::BIN_ATTR) {
print_src(bin->left(), mod);
printf(".");
print_src(bin->right(), mod);
} else {
printf("(");
print_src(bin->left(), mod);
printf(" %s ", bin->op_str());
print_src(bin->right(), mod);
printf(")");
}
}
break;
case ASTNode::NODE_CALL:
@@ -231,6 +297,46 @@ void print_src(PycRef<ASTNode> node, PycModule* mod, int indent)
}
printf("):\n");
print_src(code, mod, indent + 1);
} else if (src->type() == ASTNode::NODE_CLASS) {
printf("\n");
start_indent(indent);
printf("class ");
print_src(dest, mod);
printf("(");
PycRef<ASTTuple> bases = src.cast<ASTClass>()->bases().cast<ASTTuple>();
bool first = true;
for (ASTTuple::value_t::const_iterator b = bases->values().begin(); b != bases->values().end(); ++b) {
if (!first) printf(", ");
print_src(*b, mod);
first = false;
}
printf("):\n");
PycRef<ASTNode> code = src.cast<ASTClass>()->code().cast<ASTCall>()
->func().cast<ASTFunction>()->code();
print_src(code, mod, indent + 1);
} else if (src->type() == ASTNode::NODE_IMPORT) {
start_indent(indent);
PycRef<ASTImport> import = src.cast<ASTImport>();
if (import->fromlist() != Node_NULL) {
PycRef<PycTuple> fromlist = import->fromlist().cast<ASTObject>()->object().cast<PycTuple>();
if (fromlist != Pyc_None && fromlist->size() != 0) {
printf("from ");
print_src(import->name(), mod);
printf(" import ");
bool first = true;
PycTuple::value_t::const_iterator ii = fromlist->values().begin();
for (; ii != fromlist->values().end(); ++ii) {
if (first) printf(", ");
printf("%s", ii->cast<PycString>()->value());
}
} else {
printf("import ");
print_src(import->name(), mod);
}
} else {
printf("import ");
print_src(import->name(), mod);
}
} else {
start_indent(indent);
print_src(dest, mod);
@@ -240,13 +346,7 @@ void print_src(PycRef<ASTNode> node, PycModule* mod, int indent)
}
break;
case ASTNode::NODE_NAME:
{
ASTName::name_t name = node.cast<ASTName>()->name();
ASTName::name_t::const_iterator n = name.begin();
printf("%s", (*n)->value());
while (++n != name.end())
printf(".%s", (*n)->value());
}
printf("%s", node.cast<ASTName>()->name()->value());
break;
case ASTNode::NODE_OBJECT:
{
@@ -266,6 +366,22 @@ void print_src(PycRef<ASTNode> node, PycModule* mod, int indent)
printf("return ");
print_src(node.cast<ASTReturn>()->value(), mod);
break;
case ASTNode::NODE_TUPLE:
{
PycRef<ASTTuple> tuple = node.cast<ASTTuple>();
printf("(");
bool first = true;
for (ASTTuple::value_t::const_iterator b = tuple->values().begin(); b != tuple->values().end(); ++b) {
if (!first) printf(", ");
print_src(*b, mod);
first = false;
}
if (tuple->values().size() == 1)
printf(",)");
else
printf(")");
}
break;
default:
fprintf(stderr, "Unsupported Node type: %d\n", node->type());
}
@@ -287,9 +403,8 @@ void decompyle(PycRef<PycCode> code, PycModule* mod, int indent)
store->dest()->type() == ASTNode::NODE_NAME) {
PycRef<ASTName> src = store->src().cast<ASTName>();
PycRef<ASTName> dest = store->dest().cast<ASTName>();
if (src->name().size() == 1 && dest->name().size() == 1 &&
src->name().front()->isEqual("__name__") &&
dest->name().front()->isEqual("__module__")) {
if (src->name()->isEqual("__name__") &&
dest->name()->isEqual("__module__")) {
// __module__ = __name__
clean->removeFirst();
}

View File

@@ -258,7 +258,10 @@ void print_const(PycRef<PycObject> obj, PycModule* mod)
print_const(*it, mod);
}
}
printf(")");
if (values.size() == 1)
printf(",)");
else
printf(")");
}
break;
case PycObject::TYPE_LIST:

View File

@@ -3,15 +3,6 @@
#include "module.h"
/* PycTuple */
PycRef<PycTuple> PycTuple::Build(const value_t& items)
{
PycRef<PycTuple> tupleObj = new PycTuple();
tupleObj->m_size = items.size();
tupleObj->m_values.resize(tupleObj->m_size);
std::copy(items.begin(), items.end(), tupleObj->m_values.begin());
return tupleObj;
}
void PycTuple::load(PycData* stream, PycModule* mod)
{
m_size = stream->get32();

View File

@@ -22,7 +22,6 @@ public:
typedef std::vector<PycRef<PycObject> > value_t;
PycTuple(int type = TYPE_TUPLE) : PycSequence(type) { }
static PycRef<PycTuple> Build(const value_t& items);
bool isEqual(PycRef<PycObject> obj) const;