-
Notifications
You must be signed in to change notification settings - Fork 21
/
FactoryOwner.cc
252 lines (227 loc) · 8.02 KB
/
FactoryOwner.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// FactoryOwner.cc -- May 24, 2009
// by geohot
// part of "The Embedded Disassembler"
// released under GPLv3, see http://gplv3.fsf.org/
//
// FactoryOwners run the EDA show
// /Address/<expression>/
// Name
// Changelists(Changelists that it created)
// Xrefs(Changelists that modified it)
// Instruction/Parsed
// Instruction/StatelessChangelist
// /Changelist/<number>
// provided for debugging only, or not
// /Eval/
#include <iomanip>
#include "data.h"
#include "debug.h"
#include "util.h"
#include "FactoryOwner.h"
#include "InstructionFactoryISDF.h"
#include "File.h"
#include "JSON.h"
using namespace eda;
using namespace std;
FactoryOwner::FactoryOwner() {
//instruction_factory_ = new InstructionFactoryARM;
//instruction_factory_ = new InstructionFactoryISDF("thumb.isdf", &memory_);
//instruction_factory_ = new InstructionFactoryISDF("arm.isdf", &memory_);
instruction_factory_ = new InstructionFactoryISDF("spu.isdf", &memory_);
//instruction_factory_->InitRegisters(&memory_);
}
// These are the things the web browser can render
bool FactoryOwner::HandleGetRequest(const std::vector<string>& argv, std::string* out) {
if(argv.size() == 0) {
//(*out) += "<html><head><title>EDA</title></head><body>I am an EDA backend, go talk to a frontend</body></html>";
File::ReadFileToString(kDataDirectory + "index.html", out);
} else if(argv[0] == "favicon.ico") {
File::ReadFileToString(kDataDirectory + argv[0], out);
} else if(argv[0] == "Data" && argv.size() >= 2) {
string filename = kDataDirectory + argv[1];
for(int i = 2; i < argv.size(); i++) filename += "/" + argv[i];
File::ReadFileToString(filename, out);
} else
(*out) += "<html><head><title>EDA</title></head><body><h1>Resource not found</h1></body></html>";
return true;
}
// This is where the web browser sends things, like files
bool FactoryOwner::HandlePostRequest(const std::vector<string>& argv, std::string* out) {
return true;
}
// Read because a browser can't see these
bool FactoryOwner::HandleReadRequest(const std::vector<string>& argv, std::string* out) {
if(argv.size() == 0)
return false;
JSON data;
if(argv[0] == "Address" && argv.size() >= 2) {
Address* a = memory_.ResolveToAddress(0, argv[1]);
/*if(a != 0 && a->get_instruction() == NULL) { // Auto-Disassembly
instruction_factory_->Process(a);
LOG(INFO) << "Disassembled";
}*/
if (a != 0 && argv.size() >= 3) {
if(argv[2] == "Name") {
data.add("Name", a->get_name());
} else if(argv[2] == "Owned") {
data.add("Owned", *(memory_.history_.get_owned(a)));
} else if(argv[2] == "Xrefs") {
data.add("Xrefs", *(memory_.history_.get_xrefs(a)));
} else if(argv[2] == "Instruction") {
if(a->get_instruction() == NULL) { // Auto-Disassembly
instruction_factory_->Process(a);
LOG(INFO) << "Disassembled";
}
if(a->get_instruction() != NULL && argv.size() >= 4) {
if(argv[3]=="Parsed")
a->get_instruction()->parsed_->SerializeToJSON(&data);
else if(argv[3] == "StatelessChangelist")
a->get_instruction()->change_->SerializeToJSON(&data);
} else if(a->get_instruction() != NULL) {
a->get_instruction()->SerializeToJSON(&data);
}
} else {
return false;
}
} else if(a != 0) {
/*ostringstream ss;
ss << kXMLHeader;
LOG(DEBUG) << "making xml";
a->SerializeToXML(ss);
LOG(DEBUG) << "done making xml";
(*out) += ss.str();*/
a->SerializeToJSON(&data);
} else {
LOG(INFO) << "Address not found";
return false;
}
(*out) = data.serialize();
} /*else if(argv[0] == "Function" && argv.size() >= 2) {
Address* a = memory_.ResolveToAddress(0, argv[1]);
if(a != NULL && a->get_instruction() != NULL) {
set<Address*> addresses;
a->get_instruction()->GetFunction(&addresses);
ostringstream ss;
ss << kXMLHeader;
ss << "<Function>";
if(argv.size() >= 3 && argv[2] == "List") {
for(set<Address*>::iterator it = addresses.begin(); it != addresses.end(); ++it) {
ss << std::hex << "<location>" << (*it)->get_location() << "</location>";
}
} else {
for(set<Address*>::iterator it = addresses.begin(); it != addresses.end(); ++it) {
(*it)->SerializeToXML(ss);
}
}
ss << "</Function>";
(*out) = ss.str();
}
} else if(argv[0] == "Changelist" && argv.size() >= 2) {
Changelist* c = memory_.history_.get_changelist(stoi(argv[1]));
ostringstream ss;
ss << kXMLHeader;
if(c != NULL)
c->SerializeToXML(ss);
else
ss << "<failure></failure>";
(*out) = ss.str();
} else if(argv[0] == "State") {
ostringstream ss;
ss << kXMLHeader;
ss << "<State>";
instruction_factory_->StateToXML(ss);
ss << "<currentchangelistnumber>" << changelist_factory_.get_current_changelist_number() << "</currentchangelistnumber>";
ss << "</State>";
(*out) = ss.str();
}*/ else if(argv[0] == "Memory" && argv.size() >= 4) {
// Dump memory raw
Address* a = memory_.get_address_by_location(stoi(argv[1]));
int len = stoi(argv[2]);
int clnum = stoi(argv[3]);
ostringstream ss;
for(int i = 0; i < len; i++) {
uint8_t data;
a = a->get8(clnum, &data);
ss << setfill('0') << setw(2) << hex << (int)data;
}
(*out) = ss.str();
}
return true;
}
bool FactoryOwner::HandleEvalRequest(const std::vector<string>& argv, std::string* out) {
if(argv.size() > 0) {
ostringstream ss;
ss << std::hex << memory_.ResolveToNumber(0, argv[0]);
JSON ret;
ret.add("result", ss.str());
(*out) = ret.serialize();
} else {
return false;
}
return true;
}
bool FactoryOwner::HandleRenameRequest(const std::vector<string>& argv, std::string* out) {
if(argv[0] == "Address" && argv.size() >= 3) {
Address* a = memory_.ResolveToAddress(0, argv[1]);
LOG(INFO) << "Renaming " << a->get_name() << " to " << argv[2];
memory_.Rename(a, argv[2]);
}
return true;
}
bool FactoryOwner::HandleStepRequest(const std::vector<string>& argv, std::string* out) {
if(argv[0] == "Address" && argv.size() >= 2) {
Address* stop;
Address* a = memory_.ResolveToAddress(0, argv[1]);
if(argv.size() == 3) { // Run until
stop = memory_.ResolveToAddress(0, argv[2]);
while (a != stop) {
if(a->get_instruction() == NULL) instruction_factory_->Process(a);
Changelist* c = changelist_factory_.CreateFromStatelessChangelist(a, *a->get_instruction()->change_, &memory_);
memory_.Commit(c);
a = memory_.ResolveToAddress(0, argv[1]);
}
} else {
if (a != 0) {
if(a->get_instruction() == NULL) {
instruction_factory_->Process(a);
LOG(INFO) << "Disassembled";
}
StatelessChangelist* slcl = a->get_instruction()->change_;
DebugPrint(slcl);
LOG(DEBUG) << "got address, creating changelist";
Changelist* c = changelist_factory_.CreateFromStatelessChangelist(a, *slcl, &memory_);
LOG(DEBUG) << "changelist created";
memory_.Commit(c);
/*ostringstream ss;
ss << kXMLHeader;
c->SerializeToXML(ss);
(*out) = ss.str();*/
JSON ret;
c->SerializeToJSON(&ret);
(*out) = ret.serialize();
}
}
}
return true;
}
bool FactoryOwner::HandleDisassembleRequest(const std::vector<string>& argv, std::string* out) {
if(argv[0] == "Address" && argv.size() >= 2) {
Address* a = memory_.ResolveToAddress(0, argv[1]);
if (a != 0) {
if(a->get_instruction() == NULL) {
instruction_factory_->Process(a);
LOG(INFO) << "Disassembled";
}
/*LOG(INFO) << a->get_name();
DebugPrint(a->get_instruction()->parsed_);
DebugPrint(a->get_instruction()->change_);*/
//a->get_instruction()->change_->SerializeToXML(ss);
//(*out) += ss.str();
(*out) = "Done";
} else {
LOG(INFO) << "run address not found";
return false;
}
}
return true;
}