-
Notifications
You must be signed in to change notification settings - Fork 550
Expand file tree
/
Copy pathbin2cpp.cpp
More file actions
354 lines (322 loc) · 12.2 KB
/
bin2cpp.cpp
File metadata and controls
354 lines (322 loc) · 12.2 KB
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
// Umar Arshad
// Copyright 2014
// this enables template overloads of standard CRT functions that call the
// more secure variants automatically,
#define _CRT_SECURE_CPP_OVERLOAD_SECURE_NAMES 1
#include <cstring>
// strtok symbol name that keeps context is not on windows and linux
// so, the above overload define won't help with that function
#if defined(OS_WIN)
#define STRTOK_CALL(...) strtok_s(__VA_ARGS__)
#else
#define STRTOK_CALL(...) strtok_r(__VA_ARGS__)
#endif
#include <algorithm>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <sstream> // IWYU pragma: keep
#include <string>
#include <utility>
#include <vector>
#include <common/deterministicHash.hpp>
using namespace std;
using std::cout;
typedef map<string, string> opt_t;
void print_usage() {
cout << R"delimiter(BIN2CPP
Converts files from a binary file to C++ headers. It is similar to bin2c and
xxd but adds support for namespaces.
| --name | name of the variable (default: var) |
| --file | input file |
| --output | output file (If no output is specified then it prints to stdout |
| --type | Type of variable (default: char) |
| --binary | If the file contents are in binary form |
| --nullterm | Add a null character to the end of the file |
| --namespace | A space seperated list of namespaces |
| --formatted | Tabs for formatting |
| --version | Prints my name |
| --help | Prints usage info |
Example
-------
Command:
./bin2cpp --file blah.txt --namespace blah detail --formatted --name blah_var
Will produce:
#pragma once
#include <common/util.hpp>
#include <cstddef>
namespace blah {
namespace detail {
static const unsigned char blah_var_uchar [] = {
0x2f, 0x2f, 0x20, 0x62, 0x6c, 0x61, 0x68, 0x2e, 0x74, 0x78,
0x74, 0xa, 0x62, 0x6c, 0x61, 0x68, 0x20, 0x62, 0x6c, 0x61,
0x68, 0x20, 0x62, 0x6c, 0x61, 0x68, 0xa, };
static const char *blah_var = (const char*)blah_var_uchar;
static const size_t blah_var_len = 27;
static const size_t blah_var_hash = 12345678901234567890ULL;
static const common::Source blah_var_src = {
blah_var,
blah_var_len,
blah_var_hash
};
}
})delimiter";
exit(0);
}
static bool formatted;
static bool binary = false;
static bool nullterm = false;
void add_tabs(const int level) {
if (formatted) {
for (int i = 0; i < level; i++) { cout << "\t"; }
}
}
opt_t parse_options(const vector<string> &args) {
opt_t options;
options["--name"] = "";
options["--type"] = "";
options["--file"] = "";
options["--output"] = "";
options["--namespace"] = "";
// Parse Arguments
string curr_opt;
bool verbose = false;
for (auto arg : args) {
if (arg == "--verbose") {
verbose = true;
} else if (arg == "--binary") {
binary = true;
} else if (arg == "--nullterm") {
nullterm = true;
} else if (arg == "--formatted") {
formatted = true;
} else if (arg == "--version") {
cout << args[0] << " By Umar Arshad" << endl;
} else if (arg == "--help") {
print_usage();
} else if (options.find(arg) != options.end()) {
curr_opt = arg;
} else if (curr_opt.empty()) {
// cerr << "Invalid Argument: " << arg << endl;
} else {
if (options[curr_opt] != "") {
options[curr_opt] += " " + arg;
} else {
options[curr_opt] += arg;
}
}
}
if (verbose) {
for (auto opts : options) {
cout << get<0>(opts) << " " << get<1>(opts) << endl;
}
}
return options;
}
stringstream removeComments(ifstream &input, string &filename) {
stringstream ss;
char line[256]{
'\0'}; // Maximum length of lines in OpenCL code is limited to 256
const char *tokenCommentsStart = "/*";
const char *tokenCommentsEnd = "*/";
const char *tokenCommentsLine = "//";
const char *tokenString = "\"";
const char *delimitors = " \t;"; // Only the subset we need
enum { NO, STRING, ENDOFLINE, MULTILINE } commentsLevel{NO};
while (input.getline(line, sizeof(line) - 1)) {
char local[sizeof(line)];
struct segment {
char *start;
char *end;
} del{commentsLevel == MULTILINE ? line : nullptr, nullptr};
vector<segment> dels;
memcpy(local, line, sizeof(line)); // will be overwritten by strtok
local[sizeof(local) - 1] = '\0'; // string is always terminated
char *context = nullptr;
char *token = STRTOK_CALL(local, delimitors, &context);
do {
char *subtoken = nullptr;
while (token) {
switch (commentsLevel) {
case MULTILINE:
subtoken = strstr(token, tokenCommentsEnd);
if (subtoken != nullptr) {
if (del.start == nullptr) del.start = line;
del.end = subtoken + strlen(tokenCommentsEnd) -
local + line;
dels.push_back(del);
del = {nullptr, nullptr};
token = subtoken + strlen(tokenCommentsEnd);
commentsLevel = NO;
} else {
token = nullptr;
}
break;
case STRING:
subtoken = strstr(token, tokenString);
if (subtoken != nullptr) {
token = subtoken + strlen(tokenString);
commentsLevel = NO;
} else {
token = nullptr;
}
break;
case NO: {
// select first subtoken inside this token
subtoken = strstr(token, tokenCommentsStart);
if (subtoken != nullptr) { commentsLevel = MULTILINE; }
char *ptr = strstr(token, tokenCommentsLine);
if ((ptr != nullptr) &&
((subtoken == nullptr) || (ptr < subtoken))) {
commentsLevel = ENDOFLINE;
subtoken = ptr;
}
ptr = strstr(token, tokenString);
if ((ptr != nullptr) &&
((subtoken == nullptr) || ptr < subtoken)) {
commentsLevel = STRING;
subtoken = ptr;
}
switch (commentsLevel) {
case MULTILINE:
del.start = subtoken - local + line;
token = subtoken + strlen(tokenCommentsStart);
break;
case ENDOFLINE:
del.start = subtoken - local + line;
token = subtoken + strlen(tokenCommentsLine);
break;
case STRING:
token = subtoken + strlen(tokenString);
break;
case NO:
default: token = nullptr;
}
} break;
case ENDOFLINE:
default: token = nullptr;
}
}
token = STRTOK_CALL(nullptr, delimitors, &context);
} while (token != nullptr);
if (del.start != nullptr) {
if (commentsLevel == ENDOFLINE) commentsLevel = NO;
del.end = line + strlen(line);
dels.push_back(del);
del = {nullptr, nullptr};
}
// Delete all segments starting from the end!!!
for (auto d = dels.crbegin(); d != dels.crend(); d++) {
char *ptr1 = d->start;
char *ptr2 = d->end;
// Do not use strncpy, it has problems with overlapping because the
// order isn't defined in the standard
while ((*ptr2 != '\0') && (ptr2 != line + sizeof(line))) { *ptr1++ = *ptr2++; }
*ptr1 = '\0';
}
// Remove trailing blanks
for (long i = static_cast<long>(std::min(sizeof(line),strlen(line))) - 1;
(i >= 0) && (line[i] == ' '); --i) {
line[i] = '\0';
}
// Remove leading blanks
char *linePtr = line;
for (size_t i = 0, len = std::min(sizeof(line),strlen(line));
(i < len) && (line[i] == ' ');
++i, ++linePtr) {}
// Useful text is terminated by '\n';
if (linePtr[0] != '\0') { ss << linePtr << "\n"; }
}
return (ss);
}
int main(int argc, const char *const *const argv) {
vector<string> args(argv, argv + argc);
if (argc == 1) {
print_usage();
return 0;
}
opt_t &&options = parse_options(args);
// Save default cout buffer. Need this to prevent crash.
auto bak = cout.rdbuf();
unique_ptr<ofstream> outfile;
// Set defaults
if (options["--name"] == "") { options["--name"] = "var"; }
if (options["--output"] != "") {
// redirect stream if output file is specified
outfile.reset(new ofstream(options["--output"]));
cout.rdbuf(outfile->rdbuf());
}
cout << "#pragma once\n";
cout << "#include <cstddef>\n"; // defines size_t
cout << "#include <common/Source.hpp>\n"; // defines common::Source
int ns_cnt = 0;
int level = 0;
if (options["--namespace"] != "") {
stringstream namespaces(options["--namespace"]);
string name;
namespaces >> name;
do {
add_tabs(level++);
cout << "namespace " << name << " { \n";
ns_cnt++;
namespaces >> name;
} while (!namespaces.fail());
}
if (options["--type"] == "") { options["--type"] = "char"; }
add_tabs(level);
// Always create unsigned char to avoid narrowing
cout << "static const "
<< "unsigned char"
<< " " << options["--name"] << "_uchar [] = {\n";
ifstream input(options["--file"],
(binary ? std::ios::binary : std::ios::in));
size_t char_cnt = 0;
stringstream ss = removeComments(input, options["--file"]);
add_tabs(++level);
for (char i; ss.get(i);) {
cout << "0x" << std::hex << static_cast<int>(i & 0xff) << ",\t";
char_cnt++;
if (!(char_cnt % 10)) {
cout << endl;
add_tabs(level);
}
}
if (nullterm) {
// Add end of file character
cout << "0x0";
char_cnt++;
}
cout << "};\n";
add_tabs(--level);
// Cast to proper output type
cout << "static const " << options["--type"] << " *" << options["--name"]
<< " = (const " << options["--type"] << " *)" << options["--name"]
<< "_uchar;\n";
add_tabs(level);
cout << "static const size_t " << options["--name"] << "_len"
<< " = " << std::dec << char_cnt << ";\n";
add_tabs(level);
cout << "static const size_t " << options["--name"] << "_hash"
<< " = " << deterministicHash(ss.str()) << "ULL;\n";
add_tabs(level);
cout << "static const common::Source " << options["--name"] << "_src{\n";
add_tabs(++level);
cout << options["--name"] << ",\n";
add_tabs(level);
cout << options["--name"] << "_len,\n";
add_tabs(level);
cout << options["--name"] << "_hash\n";
add_tabs(--level);
cout << "};\n";
while (ns_cnt--) {
add_tabs(--level);
cout << "}\n";
}
cout.rdbuf(bak);
return 0;
}