forked from zrax/pycdc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyc_sequence.cpp
112 lines (97 loc) · 2.61 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
#include "pyc_sequence.h"
#include "pyc_module.h"
#include "data.h"
#include <stdexcept>
/* PycSimpleSequence */
void PycSimpleSequence::load(PycData* stream, PycModule* mod)
{
m_size = stream->get32();
m_values.reserve(m_size);
for (int i=0; i<m_size; i++)
m_values.push_back(LoadObject(stream, mod));
}
bool PycSimpleSequence::isEqual(PycRef<PycObject> obj) const
{
if (type() != obj.type())
return false;
PycRef<PycSimpleSequence> seqObj = obj.cast<PycSimpleSequence>();
if (m_size != seqObj->m_size)
return false;
auto it1 = m_values.cbegin();
auto it2 = seqObj->m_values.cbegin();
while (it1 != m_values.cend()) {
if (!(*it1)->isEqual(*it2))
return false;
++it1, ++it2;
}
return true;
}
/* 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);
}
/* 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;
}