This repository has been archived by the owner on Jul 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
incre.cc
58 lines (53 loc) · 1.4 KB
/
incre.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
#include <iostream>
#include <fstream>
#include <vector>
#include <assert.h>
const char* tbl = "0123456789abcdef";
using namespace std;
char hex(const char& x) {
if('a' <= x && x <= 'f')
return x - 'a' + 10;
if('0' <= x && x <= '9')
return x - '0';
assert(0 && "non lowered hex");
return - 1;
}
int main(int argc, char* argv[]) {
assert(4 < argc);
vector<uint8_t> buf;
buf.resize(atoi(argv[1]), 0);
if(argv[2][0] != 'S') {
assert(strlen(argv[1]) & 1);
for(int i = 0; i < atoi(argv[1]) && i * 2 < strlen(argv[2]) &&
i < buf.size(); i ++)
buf[i] = (hex(argv[2][i * 2]) << 4) | hex(argv[2][i * 2 + 1]);
}
ofstream output;
output.open(argv[3]);
if(output.is_open()) {
output << "__asm (";
for(int i = 0; i < buf.size(); i ++)
output << "\".byte 0x" << tbl[(buf[i] >> 4) & 0x0f] << tbl[buf[i] & 0x0f] << ";\"" << endl;
for(int i = 0; i < atoi(argv[4]); i ++)
output << "\"nop;\"" << endl;
output << ");" << endl;
output.close();
auto buf2(buf);
int ii;
for(ii = 0; ii < buf2.size(); ii ++) {
if(buf2[ii] == 0xff) ++ buf2[ii];
else {
++ buf2[ii];
break;
}
}
if(ii < buf2.size())
for(int i = 0; i < buf2.size(); i ++)
cout << tbl[(buf2[i] >> 4) & 0x0f] << tbl[buf2[i] & 0x0f];
else
cout << "E" << endl;
cout << endl;
} else
cout << "error";
return 0;
}