forked from zrax/pycdc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyc_module.cpp
157 lines (145 loc) · 3.24 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
#include "pyc_module.h"
#include "data.h"
PycModule::PycModule()
: m_maj(-1), m_min(-1), m_unicode(false)
{ }
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;
/* Bad Magic detected */
default:
m_maj = -1;
m_min = -1;
}
}
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()) {
fprintf(stderr, "Bad MAGIC!\n");
return;
}
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>();
}
PycRef<PycString> PycModule::getIntern(int ref)
{
std::list<PycRef<PycString> >::const_iterator it = m_interns.begin();
for (int i=0; i<ref; i++, it++) /* move forward to ref */ ;
return *it;
}