-
Notifications
You must be signed in to change notification settings - Fork 0
/
Merchant.cpp
128 lines (107 loc) · 2.46 KB
/
Merchant.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
#include "Merchant.h"
#include "Socket.h"
#include <list>
#include <vector>
#include <sstream>
#include <cassert>
#include <iostream>
// Temporary implementation while we build full structure
// Sucks information out of a local elf file
struct Merchant::Impl
{
Socket client;
};
Merchant::Merchant(const std::string& host, const std::string& elf_path, int port):pimpl(std::make_unique<Impl>())
{
pimpl->client.connect(host, port);
pimpl->client.send(elf_path);
pimpl->client.receive();
}
void Merchant::fetchPatches(const void* exact_address, Merchant::PatchList& patches)
{
std::stringstream ss;
ss<<"fetch ";
ss<<exact_address;
pimpl->client.send(ss.str());
std::string response = pimpl->client.receive();
auto toNumeric = [](std::string s){
std::stringstream ss;
ss<<s;
size_t res;
ss>>res;
return res;
};
for(unsigned i=0;i<response.size();i++)
{
Patch patch;
// Get start
std::string start_string = "";
while(response[i] != ' '){start_string+=response[i];i++;}
patch.start = toNumeric(start_string);
i++;
// Get size
std::string size_string = "";
while(response[i] != ' '){size_string+=response[i];i++;}
patch.size = toNumeric(size_string);
i++;
for(unsigned j=0;j<patch.size;j++,i++)
{
patch.content+=response[i];
}
i--;
patches.push_back(patch);
}
}
void* Merchant::entryPoint()
{
pimpl->client.send("get_text_start");
auto response = pimpl->client.receive();
std::stringstream ss;
ss<<response;
void* address;
ss>>address;
return address;
}
void* Merchant::memoryStart()
{
pimpl->client.send("get_memory_start");
auto response = pimpl->client.receive();
std::stringstream ss;
ss<<response;
void* address;
ss>>address;
return address;
}
size_t Merchant::memorySize()
{
pimpl->client.send("get_memory_size");
auto response = pimpl->client.receive();
std::stringstream ss;
ss<<response;
size_t address;
ss>>address;
return address;
}
std::string Merchant::getBlankElf(std::vector<Range>& ranges)
{
pimpl->client.send("get_blank_elf");
const auto blank_elf = pimpl->client.receive();
pimpl->client.send("get_wiped_ranges");
auto ranges_serialized = pimpl->client.receive();
// Deserialize ranges
std::stringstream ss;
size_t num_ranges;
ss<<ranges_serialized;
ss>>num_ranges;
for(unsigned i=0;i<num_ranges;i++)
{
size_t start, size;
ss<<ranges_serialized;
ss>>start;
ss<<ranges_serialized;
ss>>size;
ranges.emplace_back(start, size);
}
return blank_elf;
}
Merchant::~Merchant() = default;