-
Notifications
You must be signed in to change notification settings - Fork 21
/
Address.cc
169 lines (144 loc) · 4.06 KB
/
Address.cc
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
// Address.cc -- May 10, 2009
// by geohot
// part of "The Embedded Disassembler"
// released under GPLv3, see http://gplv3.fsf.org/
#include "data.h"
#include "util.h"
#include "JSON.h"
namespace eda {
int Address::current_gai_ = 0;
void Address::Clear() {
datamap_.clear();
datamap_.insert(make_pair(0,0));
}
void Address::Clear32() {
Clear();
next_->Clear();
next_->next_->Clear();
next_->next_->next_->Clear();
}
void Address::SerializeToXML(ostringstream& out) {
out << "<Address>" << std::hex;
out << "<name>" << name_ << "</name>";
out << "<size>" << size_ << "</size>";
if(location_ != 0xFFFFFFFF) {
out << "<location>" << immed(location_) << "</location>";
}
//LOG(DEBUG) << " wrote location";
out << "<values>";
for(map<int, uint8_t>::iterator it = datamap_.begin(); it != datamap_.end(); ++it) {
if(it->first != 0) {
uint32_t data;
get(it->first, &data);
out << std::dec << "<C_" << it->first << ">" << std::hex << data << "</C_" << std::dec << it->first << ">";
}
}
//LOG(DEBUG) << " wrote values";
out << "</values>";
if(instruction_ != NULL)
instruction_->SerializeToXML(out);
//LOG(DEBUG) << " wrote instruction";
out << "</Address>";
}
void Address::SerializeToJSON(JSON* json) {
JSON addr;
addr.add("gai", gai_);
addr.add("name", name_);
addr.add("size", size_);
addr.add("type", type_);
if(location_ != 0xFFFFFFFF) {
addr.add("location", location_);
}
// add values
vector<JSON> values;
for(map<int, uint8_t>::iterator it = datamap_.begin(); it != datamap_.end(); ++it) {
if(it->first != 0) {
uint32_t data = 0;
get(it->first, &data);
JSON value;
value.add("change", it->first);
value.add("data", data);
values.push_back(value);
}
}
addr.add("values", values);
if(instruction_ != NULL)
instruction_->SerializeToJSON(&addr);
json->add("Address", addr);
}
// Currently, the host system endianness must match the systems
Address* Address::get(int changelist_number, uint32_t* data) {
uint8_t* m = (uint8_t*)data;
Address* n = this;
for(int i = 0; i < size_; i++) {
n = n->get8(changelist_number, m+i);
}
return n;
}
Address* Address::set(int changelist_number, uint32_t data) {
uint8_t* m = (uint8_t*)&data;
Address* n = this;
for(int i = 0; i < size_; i++) {
n = n->set8(changelist_number, m[i]);
}
return n;
}
Address* Address::get8(int changelist_number, uint8_t* data) {
if(changelist_number == 0) // 0 is a link to the latest changelist
(*data) = datamap_.rbegin()->second;
else
(*data) = (--datamap_.upper_bound(changelist_number))->second;
return next_;
}
Address* Address::get16(int changelist_number, uint16_t* data) {
uint8_t* m = (uint8_t*)data;
Address* n;
n = get8(changelist_number, m);
return n->get8(changelist_number, m+1);
}
Address* Address::get32(int changelist_number, uint32_t* data) {
size_ = 4;
type_ = "data";
uint8_t* m = (uint8_t*)data;
Address* n;
n = get8(changelist_number, m);
n = n->get8(changelist_number, m+1);
n = n->get8(changelist_number, m+2);
return n->get8(changelist_number, m+3);
}
Address* Address::set8(int changelist_number, uint8_t data) {
datamap_[changelist_number] = data;
return next_;
}
Address* Address::set16(int changelist_number, uint16_t data) {
Address* n;
n = set8(changelist_number, data & 0xFF);
return n->set8(changelist_number, (data>>8) & 0xFF);
}
Address* Address::set32(int changelist_number, uint32_t data) {
Address* n;
n = set8(changelist_number, data & 0xFF);
n = n->set8(changelist_number, (data>>8) & 0xFF);
n = n->set8(changelist_number, (data>>16) & 0xFF);
return n->set8(changelist_number, (data>>24) & 0xFF);
}
void Address::set_next(Address* next) {
next_ = next;
}
Address* Address::get_next() {
return next_;
}
bool Address::set_name(const string& name) {
name_ = name;
return true;
}
const string& Address::get_name() {
return name_;
}
void Address::set_location(uint32_t location) {
location_ = location;
}
uint32_t Address::get_location() {
return location_;
}
}