forked from zrax/pycdc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pyc_numeric.cpp
177 lines (151 loc) · 4.07 KB
/
pyc_numeric.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
#include "pyc_numeric.h"
#include "pyc_module.h"
#include "data.h"
#include <cstring>
#ifdef _MSC_VER
#define snprintf sprintf_s
#endif
/* PycInt */
void PycInt::load(PycData* stream, PycModule*, int currentDepth, int maxDepth)
{
if (CheckRecursionDepth(currentDepth, maxDepth))
{
m_value = stream->get32();
}
}
/* PycLong */
void PycLong::load(PycData* stream, PycModule*, int currentDepth, int maxDepth)
{
if (CheckRecursionDepth(currentDepth, maxDepth))
{
if (type() == TYPE_INT64) {
int lo = stream->get32();
int hi = stream->get32();
m_value.push_back((lo ) & 0xFFFF);
m_value.push_back((lo >> 16) & 0xFFFF);
m_value.push_back((hi ) & 0xFFFF);
m_value.push_back((hi >> 16) & 0xFFFF);
m_size = (hi & 0x80000000) != 0 ? -4 : 4;
} else {
m_size = stream->get32();
int actualSize = m_size >= 0 ? m_size : -m_size;
for (int i=0; i<actualSize; i++)
m_value.push_back(stream->get16());
}
}
}
bool PycLong::isEqual(PycRef<PycObject> obj) const
{
if (type() != obj.type())
return false;
PycRef<PycLong> longObj = obj.cast<PycLong>();
if (m_size != longObj->m_size)
return false;
std::list<int>::const_iterator it1 = m_value.begin();
std::list<int>::const_iterator it2 = longObj->m_value.begin();
while (it1 != m_value.end()) {
if (*it1 != *it2)
return false;
++it1, ++it2;
}
return true;
}
std::string PycLong::repr() const
{
// Longs are printed as hex, since it's easier (and faster) to convert
// arbitrary-length integers to a power of two than an arbitrary base
if (m_size == 0)
return "0x0L";
// Realign to 32 bits, since Python uses only 15
std::list<unsigned> bits;
int shift = 0, temp = 0;
for (auto bit : m_value) {
temp |= unsigned(bit & 0xFFFF) << shift;
shift += 15;
if (shift >= 32) {
bits.push_back(temp);
shift -= 32;
temp = unsigned(bit & 0xFFFF) >> (15 - shift);
}
}
if (temp)
bits.push_back(temp);
std::string accum;
accum.resize(3 + (bits.size() * 8) + 2);
char* aptr = &accum[0];
if (m_size < 0)
*aptr++ = '-';
*aptr++ = '0';
*aptr++ = 'x';
std::list<unsigned>::const_reverse_iterator iter = bits.rbegin();
aptr += snprintf(aptr, 9, "%X", *iter++);
while (iter != bits.rend())
aptr += snprintf(aptr, 9, "%08X", *iter++);
*aptr++ = 'L';
*aptr = 0;
return accum;
}
/* PycFloat */
void PycFloat::load(PycData* stream, PycModule*, int currentDepth, int maxDepth)
{
if (CheckRecursionDepth(currentDepth, maxDepth))
{
int len = stream->getByte();
if (len < 0)
throw std::bad_alloc();
m_value.resize(len);
if (len > 0)
stream->getBuffer(len, &m_value.front());
}
}
bool PycFloat::isEqual(PycRef<PycObject> obj) const
{
if (type() != obj.type())
return false;
PycRef<PycFloat> floatObj = obj.cast<PycFloat>();
return m_value == floatObj->m_value;
}
/* PycComplex */
void PycComplex::load(PycData* stream, PycModule* mod, int currentDepth, int maxDepth)
{
if (CheckRecursionDepth(currentDepth, maxDepth))
{
PycFloat::load(stream, mod, currentDepth, maxDepth);
int len = stream->getByte();
if (len < 0)
{
throw std::bad_alloc();
}
m_imag.resize(len);
if (len > 0)
{
stream->getBuffer(len, &m_imag.front());
}
}
}
bool PycComplex::isEqual(PycRef<PycObject> obj) const
{
if (!PycFloat::isEqual(obj))
return false;
PycRef<PycComplex> floatObj = obj.cast<PycComplex>();
return m_imag == floatObj->m_imag;
}
/* PycCFloat */
void PycCFloat::load(PycData* stream, PycModule*, int currentDepth, int maxDepth)
{
if (CheckRecursionDepth(currentDepth, maxDepth))
{
Pyc_INT64 bits = stream->get64();
memcpy(&m_value, &bits, sizeof(bits));
}
}
/* PycCComplex */
void PycCComplex::load(PycData* stream, PycModule* mod, int currentDepth, int maxDepth)
{
if (CheckRecursionDepth(currentDepth, maxDepth))
{
PycCFloat::load(stream, mod, currentDepth, maxDepth);
Pyc_INT64 bits = stream->get64();
memcpy(&m_imag, &bits, sizeof(bits));
}
}