-
Notifications
You must be signed in to change notification settings - Fork 11
/
mmedit.cpp
202 lines (180 loc) · 5.43 KB
/
mmedit.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
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
#include <stdint.h>
#include <string.h>
#include <algorithm>
#define _DISABLE_DEBUG
#include <cpputils/argparse.h>
#include <sys/mman.h>
#include <sys/errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <vector>
#include <cpputils/mmem.h>
#include <cpputils/fhandle.h>
int parsehexnyble(char c)
{
if ('0'<=c && c<='9')
return c-'0';
if ('a'<=c && c<='f')
return c-'a'+10;
if ('A'<=c && c<='F')
return c-'A'+10;
return -1;
}
uint64_t parsehexnumber(const char *first, const char *last)
{
uint64_t num= 0;
const char *p= first;
while (p<last) {
int x= parsehexnyble(*p++);
if (x==-1)
throw "invalid hex digit";
num *= 16;
num += x;
}
return num;
}
const char *findnonhex(const char *first, const char *last)
{
const char* p= first;
while (p<last && parsehexnyble(*p)!=-1)
p++;
return p;
}
const char *findhex(const char *first, const char *last)
{
const char* p= first;
while (p<last && parsehexnyble(*p)==-1)
p++;
return p;
}
template<typename BYTEITER> inline void set8(BYTEITER p, uint8_t v) { *p= v; }
template<typename BYTEITER> inline void set16le(BYTEITER p, uint16_t v) { set8(p, v); set8(p+1, v>>8); }
template<typename BYTEITER> inline void set32le(BYTEITER p, uint32_t v) { set16le(p, v); set16le(p+2, v>>16); }
template<typename BYTEITER> inline void set64le(BYTEITER p, uint64_t v) { set32le(p, (uint32_t)v); set32le(p+4, (uint32_t)(v>>32)); }
// represents one edit
struct edit {
uint64_t ofs;
std::vector<uint8_t> data;
edit(uint64_t ofs, std::vector<uint8_t> data)
: ofs(ofs), data(data)
{
}
static edit parse(const std::string& str)
{
size_t icolon= str.find(':');
if (icolon==str.npos)
throw "edit must have ':'";
uint64_t ofs= parsehexnumber(&str[0], &str[icolon]);
if (icolon+1==str.size())
throw "no data after colon";
// handle string constant
if (str[icolon+1]=='"')
{
if (str[str.size()-1]!='"')
throw "string not terminated";
return edit(ofs, parsestring(&str[icolon+2], &str[str.size()-1]));
}
// handle data from file
if (str[icolon+1]=='@')
{
return edit(ofs, parsefile(str.substr(icolon+2)));
}
// otherwise hex data
return edit(ofs, parsedata(&str[icolon+1], &str[0]+str.size()));
}
static std::vector<uint8_t> parsedata(const char *first, const char *last)
{
std::vector<uint8_t> data;
int width= 0;
const char *p= first;
while (p<last)
{
const char *q= findnonhex(p, last);
if (width && q-p!=width)
throw "inconsistent data width";
if (width==0) {
width= q-p;
if (width&1)
throw "must be even nr of hex digits";
if (width!=2 && width!=4 && width!=8 && width!=16)
throw "data width must be 1,2,4 or 8 bytes";
}
uint64_t val= parsehexnumber(p, q);
append_to_data(data, width/2, val);
p = findhex(q, last);
}
return data;
}
static std::vector<uint8_t> parsefile(const std::string& filename)
{
std::vector<uint8_t> v;
filehandle f= open(filename.c_str(), O_RDONLY);
struct stat st;
if (-1==fstat(f, &st))
throw "fstat error";
v.resize(st.st_size);
if (-1==read(f, &v[0], v.size()))
throw "read error";
return v;
}
static void append_to_data(std::vector<uint8_t>&data, int width, uint64_t value)
{
data.resize(data.size()+width);
uint8_t *p= &data[data.size()-width];
switch(width)
{
case 1: *p= value; break;
case 2: set16le(p, value); break;
case 4: set32le(p, value); break;
case 8: set64le(p, value); break;
}
}
static std::vector<uint8_t> parsestring(const char *first, const char *last)
{
return std::vector<uint8_t>((const uint8_t*)first, (const uint8_t*)last);
}
};
void mmapply(int f, uint64_t ofs, const std::vector<uint8_t>& data)
{
mappedmem m(f, ofs, ofs+data.size(), PROT_READ|PROT_WRITE);
memcpy(m.data(), &data[0], data.size());
}
void usage()
{
printf("Usage: mmedit <device> [edits...]\n");
printf(" edits are formatted like: <hexoffset>:<data>\n");
printf(" data is formated as:\n");
printf(" * 2,4,8 or 16 hex digits -> byte, short, dword, qword\n");
printf(" * \"...\" -> a double quoted string\n");
printf(" * @filename -> read from file\n");
}
int main(int argc, char**argv)
{
std::vector<edit> edits;
std::string filename;
try {
for (auto& arg : ArgParser(argc, argv))
switch (arg.option())
{
case 'h': usage(); return 0;
case -1:
if (filename.empty())
filename= arg.getstr();
else
edits.push_back(edit::parse(arg.getstr()));
}
if (filename.empty() || edits.empty()) {
usage();
return 1;
}
filehandle f = open(filename.c_str(), O_RDWR);
for (auto &e : edits)
mmapply(f, e.ofs, e.data);
}
catch (const char*msg)
{
printf("E: %s\n", msg);
return 1;
}
return 0;
}