forked from zrax/pycdc
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpyc_sequence.cpp
169 lines (146 loc) · 3.89 KB
/
pyc_sequence.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
#include "pyc_sequence.h"
#include "pyc_module.h"
#include "data.h"
#include <stdexcept>
/* PycTuple */
void PycTuple::load(PycData* stream, PycModule* mod)
{
if (type() == TYPE_SMALL_TUPLE)
m_size = stream->getByte();
else
m_size = stream->get32();
m_values.resize(m_size);
for (int i=0; i<m_size; i++)
m_values[i] = LoadObject(stream, mod);
}
bool PycTuple::isEqual(PycRef<PycObject> obj) const
{
if (type() != obj.type())
return false;
PycRef<PycTuple> tupleObj = obj.cast<PycTuple>();
if (m_size != tupleObj->m_size)
return false;
auto it1 = m_values.cbegin();
auto it2 = tupleObj->m_values.cbegin();
while (it1 != m_values.cend()) {
if (!(*it1)->isEqual(*it2))
return false;
++it1, ++it2;
}
return true;
}
/* PycList */
void PycList::load(PycData* stream, PycModule* mod)
{
m_size = stream->get32();
for (int i=0; i<m_size; i++)
m_values.push_back(LoadObject(stream, mod));
}
bool PycList::isEqual(PycRef<PycObject> obj) const
{
if (type() != obj.type())
return false;
PycRef<PycList> listObj = obj.cast<PycList>();
if (m_size != listObj->m_size)
return false;
auto it1 = m_values.cbegin();
auto it2 = listObj->m_values.cbegin();
while (it1 != m_values.cend()) {
if (!(*it1)->isEqual(*it2))
return false;
++it1, ++it2;
}
return true;
}
/* PycDict */
void PycDict::load(PycData* stream, PycModule* mod)
{
PycRef<PycObject> key, val;
for (;;) {
key = LoadObject(stream, mod);
if (key == NULL)
break;
val = LoadObject(stream, mod);
m_keys.push_back(key);
m_values.push_back(val);
}
}
bool PycDict::isEqual(PycRef<PycObject> obj) const
{
if (type() != obj.type())
return false;
PycRef<PycDict> dictObj = obj.cast<PycDict>();
if (m_size != dictObj->m_size)
return false;
auto ki1 = m_keys.cbegin();
auto ki2 = dictObj->m_keys.cbegin();
while (ki1 != m_keys.cend()) {
if (!(*ki1)->isEqual(*ki2))
return false;
++ki1, ++ki2;
}
auto vi1 = m_values.cbegin();
auto vi2 = dictObj->m_values.cbegin();
while (vi1 != m_values.cend()) {
if (!(*vi1)->isEqual(*vi2))
return false;
++vi1, ++vi2;
}
return true;
}
PycRef<PycObject> PycDict::get(PycRef<PycObject> key) const
{
auto ki = m_keys.cbegin();
auto vi = m_values.cbegin();
while (ki != m_keys.cend()) {
if ((*ki)->isEqual(key))
return *vi;
++ki, ++vi;
}
return NULL; // Disassembly shouldn't get non-existent keys
}
PycRef<PycObject> PycDict::get(int idx) const
{
if (idx < 0)
throw std::out_of_range("Dict index out of range");
auto it = m_values.cbegin();
while (idx-- && it != m_values.cend())
++it;
if (it == m_values.cend())
throw std::out_of_range("Dict index out of range");
return *it;
}
/* PycSet */
void PycSet::load(PycData* stream, PycModule* mod)
{
m_size = stream->get32();
for (int i=0; i<m_size; i++)
m_values.insert(LoadObject(stream, mod));
}
bool PycSet::isEqual(PycRef<PycObject> obj) const
{
if (type() != obj.type())
return false;
PycRef<PycSet> setObj = obj.cast<PycSet>();
if (m_size != setObj->m_size)
return false;
auto it1 = m_values.cbegin();
auto it2 = setObj->m_values.cbegin();
while (it1 != m_values.cend()) {
if (!(*it1)->isEqual(*it2))
return false;
++it1, ++it2;
}
return true;
}
PycRef<PycObject> PycSet::get(int idx) const
{
if (idx < 0)
throw std::out_of_range("Set index out of range");
auto it = m_values.cbegin();
while (idx-- && it != m_values.cend())
++it;
if (it == m_values.cend())
throw std::out_of_range("Set index out of range");
return *it;
}