forked from zrax/pycdc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pycdas.cpp
219 lines (203 loc) · 7.5 KB
/
pycdas.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include <cstdio>
#include <cstring>
#include <cstdarg>
#include "pyc_module.h"
#include "pyc_numeric.h"
#include "bytecode.h"
#ifdef WIN32
# define PATHSEP '\\'
#else
# define PATHSEP '/'
#endif
static const char* flag_names[] = {
"CO_OPTIMIZED", "CO_NEWLOCALS", "CO_VARARGS", "CO_VARKEYWORDS",
"CO_NESTED", "CO_GENERATOR", "CO_NOFREE", "<0x80>", "<0x100>", "<0x200>",
"<0x400>", "<0x800>", "CO_GENERATOR_ALLOWED", "CO_FUTURE_DIVISION",
"CO_FUTURE_ABSOLUTE_IMPORT", "CO_FUTURE_WITH_STATEMENT",
"CO_FUTURE_PRINT_FUNCTION", "CO_FUTURE_UNICODE_LITERALS",
"CO_FUTURE_BARRY_AS_BDFL", "<0x80000>", "<0x100000>", "<0x200000>",
"<0x400000>", "<0x800000>", "<0x1000000>", "<0x2000000>", "<0x4000000>",
"<0x8000000>", "<0x10000000>", "<0x20000000>", "<0x40000000>",
"<0x80000000>"
};
static void print_coflags(unsigned long flags)
{
if (flags == 0) {
fprintf(pyc_output, "\n");
return;
}
fprintf(pyc_output, " (");
unsigned long f = 1;
int k = 0;
while (k < 32) {
if ((flags & f) != 0) {
flags &= ~f;
if (flags == 0)
fprintf(pyc_output, "%s", flag_names[k]);
else
fprintf(pyc_output, "%s | ", flag_names[k]);
}
++k;
f <<= 1;
}
fprintf(pyc_output, ")\n");
}
static void ivprintf(int indent, const char* fmt, va_list varargs)
{
for (int i=0; i<indent; i++)
fprintf(pyc_output, " ");
vprintf(fmt, varargs);
}
static void iprintf(int indent, const char* fmt, ...)
{
va_list varargs;
va_start(varargs, fmt);
ivprintf(indent, fmt, varargs);
va_end(varargs);
}
void output_object(PycRef<PycObject> obj, PycModule* mod, int indent)
{
switch (obj->type()) {
case PycObject::TYPE_CODE:
case PycObject::TYPE_CODE2:
{
PycRef<PycCode> codeObj = obj.cast<PycCode>();
iprintf(indent, "[Code]\n");
iprintf(indent + 1, "File Name: %s\n", codeObj->fileName()->value());
iprintf(indent + 1, "Object Name: %s\n", codeObj->name()->value());
iprintf(indent + 1, "Arg Count: %d\n", codeObj->argCount());
iprintf(indent + 1, "Locals: %d\n", codeObj->numLocals());
iprintf(indent + 1, "Stack Size: %d\n", codeObj->stackSize());
iprintf(indent + 1, "Flags: 0x%08X", codeObj->flags());
print_coflags(codeObj->flags());
if (codeObj->names() != Pyc_NULL) {
iprintf(indent + 1, "[Names]\n");
for (int i=0; i<codeObj->names()->size(); i++)
output_object(codeObj->names()->get(i), mod, indent + 2);
}
if (codeObj->varNames() != Pyc_NULL) {
iprintf(indent + 1, "[Var Names]\n");
for (int i=0; i<codeObj->varNames()->size(); i++)
output_object(codeObj->varNames()->get(i), mod, indent + 2);
}
if (codeObj->freeVars() != Pyc_NULL) {
iprintf(indent + 1, "[Free Vars]\n");
for (int i=0; i<codeObj->freeVars()->size(); i++)
output_object(codeObj->freeVars()->get(i), mod, indent + 2);
}
if (codeObj->cellVars() != Pyc_NULL) {
iprintf(indent + 1, "[Cell Vars]\n");
for (int i=0; i<codeObj->cellVars()->size(); i++)
output_object(codeObj->cellVars()->get(i), mod, indent + 2);
}
if (codeObj->consts() != Pyc_NULL) {
iprintf(indent + 1, "[Constants]\n");
for (int i=0; i<codeObj->consts()->size(); i++)
output_object(codeObj->consts()->get(i), mod, indent + 2);
}
iprintf(indent + 1, "[Disassembly]\n");
bc_disasm(codeObj, mod, indent + 2);
}
break;
case PycObject::TYPE_STRING:
case PycObject::TYPE_STRINGREF:
case PycObject::TYPE_INTERNED:
iprintf(indent, "");
OutputString(obj.cast<PycString>(), (mod->majorVer() == 3) ? 'b' : 0);
fprintf(pyc_output, "\n");
break;
case PycObject::TYPE_UNICODE:
iprintf(indent, "");
OutputString(obj.cast<PycString>(), (mod->majorVer() == 3) ? 0 : 'u');
fprintf(pyc_output, "\n");
break;
case PycObject::TYPE_TUPLE:
{
iprintf(indent, "(\n");
PycTuple::value_t values = obj.cast<PycTuple>()->values();
for (PycTuple::value_t::const_iterator i = values.begin(); i != values.end(); i++)
output_object(*i, mod, indent + 1);
iprintf(indent, ")\n");
}
break;
case PycObject::TYPE_LIST:
{
iprintf(indent, "[\n");
PycList::value_t values = obj.cast<PycList>()->values();
for (PycList::value_t::const_iterator i = values.begin(); i != values.end(); i++)
output_object(*i, mod, indent + 1);
iprintf(indent, "]\n");
}
break;
case PycObject::TYPE_DICT:
{
iprintf(indent, "{\n");
PycDict::key_t keys = obj.cast<PycDict>()->keys();
PycDict::value_t values = obj.cast<PycDict>()->values();
PycDict::key_t::const_iterator ki = keys.begin();
PycDict::value_t::const_iterator vi = values.begin();
while (ki != keys.end()) {
output_object(*ki, mod, indent + 1);
output_object(*vi, mod, indent + 2);
++ki, ++vi;
}
iprintf(indent, "}\n");
}
break;
case PycObject::TYPE_SET:
{
iprintf(indent, "{\n");
PycSet::value_t values = obj.cast<PycSet>()->values();
for (PycSet::value_t::const_iterator i = values.begin(); i != values.end(); i++)
output_object(*i, mod, indent + 1);
iprintf(indent, "}\n");
}
break;
case PycObject::TYPE_NONE:
iprintf(indent, "None\n");
break;
case PycObject::TYPE_FALSE:
iprintf(indent, "False\n");
break;
case PycObject::TYPE_TRUE:
iprintf(indent, "True\n");
break;
case PycObject::TYPE_INT:
iprintf(indent, "%d\n", obj.cast<PycInt>()->value());
break;
case PycObject::TYPE_LONG:
iprintf(indent, "%s\n", obj.cast<PycLong>()->repr().c_str());
break;
case PycObject::TYPE_FLOAT:
iprintf(indent, "%s\n", obj.cast<PycFloat>()->value());
break;
case PycObject::TYPE_COMPLEX:
iprintf(indent, "(%s+%sj)\n", obj.cast<PycComplex>()->value(),
obj.cast<PycComplex>()->imag());
break;
case PycObject::TYPE_BINARY_FLOAT:
iprintf(indent, "%g\n", obj.cast<PycCFloat>()->value());
break;
case PycObject::TYPE_BINARY_COMPLEX:
iprintf(indent, "(%g+%gj)\n", obj.cast<PycCComplex>()->value(),
obj.cast<PycCComplex>()->imag());
break;
default:
iprintf(indent, "<TYPE: %d>\n", obj->type());
}
}
int main(int argc, char* argv[])
{
if (argc < 2) {
fprintf(stderr, "No input file specified\n");
return 1;
}
PycModule mod;
mod.loadFromFile(argv[1]);
const char* dispname = strrchr(argv[1], PATHSEP);
dispname = (dispname == NULL) ? argv[1] : dispname + 1;
fprintf(pyc_output, "%s (Python %d.%d%s)\n", dispname, mod.majorVer(), mod.minorVer(),
(mod.majorVer() < 3 && mod.isUnicode()) ? " -U" : "");
output_object(mod.code().cast<PycObject>(), &mod, 0);
return 0;
}