forked from zrax/pycdc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyc_module.cpp
260 lines (234 loc) · 5.29 KB
/
pyc_module.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
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
#include "pyc_module.h"
#include "data.h"
#include <stdexcept>
void PycModule::setVersion(unsigned int magic)
{
// Default for versions that don't support unicode selection
m_unicode = false;
switch (magic) {
case MAGIC_1_0:
m_maj = 1;
m_min = 0;
break;
case MAGIC_1_1:
m_maj = 1;
m_min = 1;
break;
case MAGIC_1_3:
m_maj = 1;
m_min = 3;
break;
case MAGIC_1_4:
m_maj = 1;
m_min = 4;
break;
case MAGIC_1_5:
m_maj = 1;
m_min = 5;
break;
/* Starting with 1.6, Python adds +1 for unicode mode (-U) */
case MAGIC_1_6+1:
m_unicode = true;
/* Fall through */
case MAGIC_1_6:
m_maj = 1;
m_min = 6;
break;
case MAGIC_2_0+1:
m_unicode = true;
/* Fall through */
case MAGIC_2_0:
m_maj = 2;
m_min = 0;
break;
case MAGIC_2_1+1:
m_unicode = true;
/* Fall through */
case MAGIC_2_1:
m_maj = 2;
m_min = 1;
break;
case MAGIC_2_2+1:
m_unicode = true;
/* Fall through */
case MAGIC_2_2:
m_maj = 2;
m_min = 2;
break;
case MAGIC_2_3+1:
m_unicode = true;
/* Fall through */
case MAGIC_2_3:
m_maj = 2;
m_min = 3;
break;
case MAGIC_2_4+1:
m_unicode = true;
/* Fall through */
case MAGIC_2_4:
m_maj = 2;
m_min = 4;
break;
case MAGIC_2_5+1:
m_unicode = true;
/* Fall through */
case MAGIC_2_5:
m_maj = 2;
m_min = 5;
break;
case MAGIC_2_6+1:
m_unicode = true;
/* Fall through */
case MAGIC_2_6:
m_maj = 2;
m_min = 6;
break;
case MAGIC_2_7+1:
m_unicode = true;
/* Fall through */
case MAGIC_2_7:
m_maj = 2;
m_min = 7;
break;
/* 3.0 and above are always unicode */
case MAGIC_3_0+1:
m_maj = 3;
m_min = 0;
m_unicode = true;
break;
case MAGIC_3_1+1:
m_maj = 3;
m_min = 1;
m_unicode = true;
break;
/* 3.2 stops using the unicode increment */
case MAGIC_3_2:
m_maj = 3;
m_min = 2;
m_unicode = true;
break;
case MAGIC_3_3:
m_maj = 3;
m_min = 3;
m_unicode = true;
break;
case MAGIC_3_4:
m_maj = 3;
m_min = 4;
m_unicode = true;
break;
case MAGIC_3_5:
/* fall through */
case MAGIC_3_5_3:
m_maj = 3;
m_min = 5;
m_unicode = true;
break;
case MAGIC_3_6:
m_maj = 3;
m_min = 6;
m_unicode = true;
break;
case MAGIC_3_7:
m_maj = 3;
m_min = 7;
m_unicode = true;
break;
case MAGIC_3_8:
m_maj = 3;
m_min = 8;
m_unicode = true;
break;
case MAGIC_3_9:
m_maj = 3;
m_min = 9;
m_unicode = true;
break;
case MAGIC_3_10:
m_maj = 3;
m_min = 10;
m_unicode = true;
break;
case MAGIC_3_11:
m_maj = 3;
m_min = 11;
m_unicode = true;
break;
case MAGIC_3_12:
m_maj = 3;
m_min = 12;
m_unicode = true;
break;
/* Bad Magic detected */
default:
m_maj = -1;
m_min = -1;
}
}
bool PycModule::isSupportedVersion(int major, int minor)
{
switch (major) {
case 1:
return (minor >= 0 && minor <= 6);
case 2:
return (minor >= 0 && minor <= 7);
case 3:
return (minor >= 0 && minor <= 12);
default:
return false;
}
}
void PycModule::loadFromFile(const char* filename)
{
PycFile in(filename);
if (!in.isOpen()) {
fprintf(stderr, "Error opening file %s\n", filename);
return;
}
setVersion(in.get32());
if (!isValid()) {
fputs("Bad MAGIC!\n", stderr);
return;
}
int flags = 0;
if (verCompare(3, 7) >= 0)
flags = in.get32();
if (flags & 0x1) {
// Optional checksum added in Python 3.7
in.get32();
in.get32();
} else {
in.get32(); // Timestamp -- who cares?
if (verCompare(3, 3) >= 0)
in.get32(); // Size parameter added in Python 3.3
}
m_code = LoadObject(&in, this).cast<PycCode>();
}
void PycModule::loadFromMarshalledFile(const char* filename, int major, int minor)
{
PycFile in (filename);
if (!in.isOpen()) {
fprintf(stderr, "Error opening file %s\n", filename);
return;
}
if (!isSupportedVersion(major, minor)) {
fprintf(stderr, "Unsupported version %d.%d\n", major, minor);
return;
}
m_maj = major;
m_min = minor;
m_unicode = (major >= 3);
m_code = LoadObject(&in, this).cast<PycCode>();
}
PycRef<PycString> PycModule::getIntern(int ref) const
{
if (ref < 0 || (size_t)ref >= m_interns.size())
throw std::out_of_range("Intern index out of range");
return m_interns[(size_t)ref];
}
PycRef<PycObject> PycModule::getRef(int ref) const
{
if (ref < 0 || (size_t)ref >= m_refs.size())
throw std::out_of_range("Ref index out of range");
return m_refs[(size_t)ref];
}