forked from WerWolv/ImHex-Patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyc.hexpat
340 lines (289 loc) · 9.03 KB
/
pyc.hexpat
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#pragma description Python bytecode files
#include <type/time.pat>
#include <std/mem.pat>
#define FLAG_REF 0x80
#define TYPE_NULL '0'
#define TYPE_NONE 'N'
#define TYPE_FALSE 'F'
#define TYPE_TRUE 'T'
#define TYPE_STOPITER 'S'
#define TYPE_ELLIPSIS '.'
#define TYPE_INT 'i'
#define TYPE_INT64 'I'
#define TYPE_FLOAT 'f'
#define TYPE_BINARY_FLOAT 'g'
#define TYPE_COMPLEX 'x'
#define TYPE_BINARY_COMPLEX 'y'
#define TYPE_LONG 'l'
#define TYPE_STRING 's'
#define TYPE_INTERNED 't'
#define TYPE_REF 'r'
#define TYPE_STRINGREF 'R'
#define TYPE_TUPLE '('
#define TYPE_LIST '['
#define TYPE_DICT '{'
#define TYPE_CODE 'c'
#define TYPE_UNICODE 'u'
#define TYPE_UNKNOWN '?'
#define TYPE_SET '<'
#define TYPE_FROZENSET '>'
#define TYPE_ASCII 'a'
#define TYPE_ASCII_INTERNED 'A'
#define TYPE_SMALL_TUPLE ')'
#define TYPE_SHORT_ASCII 'z'
#define TYPE_SHORT_ASCII_INTERNED 'Z'
#define MAX_REFS 2048
using MarshalObject;
u8 _major;
u8 _minor;
fn above(u8 major, u8 minor) {
return _major >= major && _minor >= minor;
};
fn below(u8 major, u8 minor) {
return _major <=major && _minor < minor;
};
fn is(u8 major, u8 minor) {
return _major == major && _minor == minor;
};
fn between(u8 major, u8 minor, u8 otherMajor, u8 otherMinor) {
return above(major, minor) && below(otherMajor, otherMinor);
};
struct NoneObject {};
struct MarshalString {
if(parent.type == TYPE_SHORT_ASCII || parent.type == TYPE_SHORT_ASCII_INTERNED)
u8 length;
else
u32 length;
char data[length];
};
struct MarshalSequence {
if(parent.type == TYPE_SMALL_TUPLE)
u8 length;
else
u32 length;
MarshalObject values[length];
};
struct MarshalCode {
if(between(1, 3, 2, 3)) u16 numArgs;
else if(above(2, 3)) u32 numArgs;
if(above(3, 8)) u32 numPosOnlyArgs;
if(_major >= 3) u32 numKwOnlyArgs;
if(between(1, 3, 2, 3)) u16 numLocals;
else if(between(2, 3, 3, 11)) u32 numLocals;
if(between(1, 5, 2, 3)) u16 numStack;
else if(above(2, 3)) u32 numStack;
if(between(1, 3, 2, 3)) u16 flags;
else if(above(2, 3)) u32 flags;
MarshalObject code;
MarshalObject constants;
MarshalObject names;
if(above(1, 3)) MarshalObject localNames;
if(above(3, 11)) MarshalObject localKinds;
if(between(2, 1, 3, 11)) {
MarshalObject freeVars;
MarshalObject cellVars;
}
MarshalObject fileName;
MarshalObject name;
if(above(3, 11)) MarshalObject qualifiedName;
if(between(1, 5, 2, 3)) u16 firstLine;
else if(above(2, 3)) u32 firstLine;
if(above(1, 5)) MarshalObject lnTable;
if(above(3, 11)) MarshalObject exceptionTable;
};
struct DictEntry {
MarshalObject key;
if(key.type == TYPE_NULL) break;
MarshalObject value;
};
struct MarshalDict {
DictEntry entries[while(true)];
};
struct LongInt {
u32 len;
u16 buffer[len];
};
struct LongFloat {
u8 len;
u8 buffer[len];
};
struct LongComplex {
LongFloat real;
LongFloat imag;
};
auto section = std::mem::create_section("ref-address");
u128 refs[MAX_REFS] @ 0x0 in section;
u32 refIndex;
fn refMember(u128 address) {
refs[refIndex] = address;
refIndex += 1;
};
fn getRef(u32 index) {
return refs[index];
};
struct MarshalObject {
u8 _type [[hidden]];
u8 flag = _type & FLAG_REF;
u8 type = _type & ~FLAG_REF;
if(flag) {
refMember($-1);
}
match(type) {
(TYPE_NULL | TYPE_NONE | TYPE_STOPITER | TYPE_ELLIPSIS | TYPE_FALSE | TYPE_TRUE): continue;
(TYPE_CODE): {
MarshalCode code [[inline]];
}
(TYPE_STRING |
TYPE_UNICODE |
TYPE_ASCII |
TYPE_ASCII_INTERNED |
TYPE_SHORT_ASCII |
TYPE_SHORT_ASCII_INTERNED): {
MarshalString string [[inline]];
}
(TYPE_SMALL_TUPLE | TYPE_TUPLE | TYPE_LIST | TYPE_SET | TYPE_FROZENSET): {
MarshalSequence sequence [[inline]];
}
(TYPE_DICT): {
MarshalDict dict [[inline]];
}
(TYPE_INT): {
u32 int;
}
(TYPE_INT64): {
u64 long;
}
(TYPE_FLOAT): {
LongFloat number;
}
(TYPE_BINARY_FLOAT): {
double number;
}
(TYPE_COMPLEX): {
LongComplex complex;
}
(TYPE_BINARY_COMPLEX): {
double real;
double imag;
}
(TYPE_LONG): {
LongInt long;
}
(TYPE_REF): {
u32 index;
MarshalObject obj @ getRef(index);
}
(_): {
std::print("Unknown marshal object type: {:c}", type);
}
}
} [[format("format_marshal")]];
fn format_marshal(ref auto obj) {
match(obj.type) {
(TYPE_NONE): return "None";
(TYPE_STOPITER): return "StopIteration";
(TYPE_ELLIPSIS): return "...";
(TYPE_FALSE): return "False";
(TYPE_TRUE): return "True";
(TYPE_INT): return "int";
(TYPE_INT64): return "int64";
(TYPE_FLOAT): return "float";
(TYPE_BINARY_FLOAT): return "float";
(TYPE_COMPLEX): return "complex";
(TYPE_BINARY_COMPLEX): return "complex";
(TYPE_LONG): return "long";
(TYPE_STRING): return "string";
(TYPE_INTERNED): return "interned";
(TYPE_REF): return "ref";
(TYPE_TUPLE): return "tuple";
(TYPE_LIST): return "list";
(TYPE_DICT): return "dict";
(TYPE_CODE): return "code";
(TYPE_UNICODE): return "unicode";
(TYPE_UNKNOWN): return "unknown";
(TYPE_SET): return "set";
(TYPE_FROZENSET): return "frozenset";
(TYPE_ASCII): return "ascii";
(TYPE_ASCII_INTERNED): return "ascii_interned";
(TYPE_SHORT_ASCII): return "short_ascii";
(TYPE_SHORT_ASCII_INTERNED): return "short_ascii_interned";
(TYPE_SMALL_TUPLE): return "small_tuple";
}
};
enum Magic : u32 {
MAGIC_1_0 = 0x00999902,
MAGIC_1_1 = 0x00999903, /* Also covers 1.2 */
MAGIC_1_3 = 0x0A0D2E89,
MAGIC_1_4 = 0x0A0D1704,
MAGIC_1_5 = 0x0A0D4E99,
MAGIC_1_6 = 0x0A0DC4FC,
MAGIC_2_0 = 0x0A0DC687,
MAGIC_2_1 = 0x0A0DEB2A,
MAGIC_2_2 = 0x0A0DED2D,
MAGIC_2_3 = 0x0A0DF23B,
MAGIC_2_4 = 0x0A0DF26D,
MAGIC_2_5 = 0x0A0DF2B3,
MAGIC_2_6 = 0x0A0DF2D1,
MAGIC_2_7 = 0x0A0DF303,
MAGIC_3_0 = 0x0A0D0C3A,
MAGIC_3_1 = 0x0A0D0C4E,
MAGIC_3_2 = 0x0A0D0C6C,
MAGIC_3_3 = 0x0A0D0C9E,
MAGIC_3_4 = 0x0A0D0CEE,
MAGIC_3_5 = 0x0A0D0D16,
MAGIC_3_5_3 = 0x0A0D0D17,
MAGIC_3_6 = 0x0A0D0D33,
MAGIC_3_7 = 0x0A0D0D42,
MAGIC_3_8 = 0x0A0D0D55,
MAGIC_3_9 = 0x0A0D0D61,
MAGIC_3_10 = 0x0A0D0D6F,
MAGIC_3_11 = 0x0A0D0DA7,
INVALID = 0,
};
fn getMajor(Magic magic) {
match(magic) {
(Magic::MAGIC_1_0 | Magic::MAGIC_1_1 | Magic::MAGIC_1_3 | Magic::MAGIC_1_4 | Magic::MAGIC_1_5 | Magic::MAGIC_1_6): return 1;
(Magic::MAGIC_2_0 | Magic::MAGIC_2_1 | Magic::MAGIC_2_2 | Magic::MAGIC_2_3 | Magic::MAGIC_2_4 | Magic::MAGIC_2_5 | Magic::MAGIC_2_6 | Magic::MAGIC_2_7): return 2;
(Magic::MAGIC_3_0 | Magic::MAGIC_3_1 | Magic::MAGIC_3_2 | Magic::MAGIC_3_3 | Magic::MAGIC_3_4 | Magic::MAGIC_3_5 | Magic::MAGIC_3_5_3 | Magic::MAGIC_3_6 | Magic::MAGIC_3_7 | Magic::MAGIC_3_8 | Magic::MAGIC_3_9 | Magic::MAGIC_3_10 | Magic::MAGIC_3_11): return 3;
(Magic::INVALID): return 0;
}
};
fn getMinor(Magic magic) {
match(magic) {
(Magic::MAGIC_1_0 | Magic::MAGIC_2_0 | Magic::MAGIC_3_0): return 0;
(Magic::MAGIC_1_1 | Magic::MAGIC_2_1 | Magic::MAGIC_3_1): return 1;
(Magic::MAGIC_1_3 | Magic::MAGIC_2_2 | Magic::MAGIC_3_2): return 2;
(Magic::MAGIC_1_4 | Magic::MAGIC_2_3 | Magic::MAGIC_3_3): return 3;
(Magic::MAGIC_1_5 | Magic::MAGIC_2_4 | Magic::MAGIC_3_4): return 4;
(Magic::MAGIC_1_6 | Magic::MAGIC_2_5 | Magic::MAGIC_3_5 | Magic::MAGIC_3_5_3): return 5;
(Magic::MAGIC_2_6 | Magic::MAGIC_3_6): return 6;
(Magic::MAGIC_2_7 | Magic::MAGIC_3_7): return 7;
(Magic::MAGIC_3_8): return 8;
(Magic::MAGIC_3_9): return 9;
(Magic::MAGIC_3_10): return 10;
(Magic::MAGIC_3_11): return 11;
(Magic::INVALID): return 0;
}
};
struct PYCHeader {
Magic magic;
_major = getMajor(magic);
_minor = getMinor(magic);
u8 major = _major [[export]];
u8 minor = _minor [[export]];
if(above(3, 7)) {
u32 flags;
} else {
u32 flags;
}
if(flags & 0x1) {
u64 checksum;
} else {
type::time32_t timestamp;
if(above(3, 3)) {
u32 size;
}
}
MarshalObject object;
};
PYCHeader header @ 0x0;