-
Notifications
You must be signed in to change notification settings - Fork 0
/
bin2hpp.hpp
374 lines (314 loc) · 11.7 KB
/
bin2hpp.hpp
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#ifndef BIN2HPP_HEADER
#define BIN2HPP_HEADER
#include <random>
#include <string>
#include <vector>
#include <array>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cassert>
#include <sstream> // std::stringstream
#include <iostream>
#include <cassert>
namespace hexutils{
constexpr char elems[] = "0123456789ABCDEF";
inline constexpr std::array<char,2> to_hex(const unsigned char c) noexcept {
static_assert(((std::numeric_limits<unsigned char>::max() & 0xff) >> 4 )<16, "Impossible");
static_assert(((std::numeric_limits<unsigned char>::max() & 0x0f) )<16, "Impossible");
return {{elems[(c & '\xff') >> 4], elems[c & 0x0f]}};
}
}
namespace bin2hpp{
const std::string comment = "This file has been autogenerated by bin2hpp";
const std::string defaultnamespace = "resource";
enum class lang_id{ cpp, c, java };
enum class cpp_rev{ cpp98, cpp03, cpp11, cpp14, cpp17};
enum class c_rev{ c89, c99, c11};
enum class java_rev{j1_3, j1_4, j1_5, j1_6, j1_7, j1_8, j1_9};
enum class resource_type_cpp{std_arr, c_arr, std_string, c_string};
enum class resource_type_c{c_arr, c_string};
enum class resource_type_java{byte_arr, Byte_arr, String, list};
namespace constid {
const std::string _enum = "enum";
const std::string _const = "const";
const std::string _constexpr = "constexpr";
}
namespace array_id {
// c++
const std::string _std_arr = "std::array";
const std::string _std_string = "std::string";
// c and c++
const std::string _c_arr = "c_arr";
const std::string _c_string = "c_str";
// java
const std::string _byte_arr = "byte[]";
const std::string _Byte_arr = "Byte[]";
const std::string _string = "String";
const std::string _list = "List<Byte>";
inline resource_type_c to_res_type_c(const std::string& res){
if(res == _c_arr){
return resource_type_c::c_arr;
}
if(res == _c_string){
return resource_type_c::c_string;
}
throw std::runtime_error("no valid cpp resource type");
}
inline resource_type_cpp to_res_type_cpp(const std::string& res){
if(res == _std_arr){
return resource_type_cpp::std_arr;
}
if(res == _std_string){
return resource_type_cpp::std_string;
}
if(res == _c_arr){
return resource_type_cpp::c_arr;
}
if(res == _c_string){
return resource_type_cpp::c_string;
}
throw std::runtime_error("no valid cpp resource type");
}
inline resource_type_java to_res_type_java(const std::string& res){
if(res == _byte_arr){
return resource_type_java::byte_arr;
}
if(res == _byte_arr){
return resource_type_java::Byte_arr;
}
if(res == _byte_arr){
return resource_type_java::String;
}
if(res == _byte_arr){
return resource_type_java::list;
}
throw std::runtime_error("no valid java resource type");
}
}
struct language{
lang_id id = lang_id::cpp;
union{
cpp_rev _cpprev = cpp_rev::cpp17;
c_rev _crev;
java_rev _javarev;
};
};
enum class constid_array{ _const, _constexpr};
enum class constid_size{ _const, _constexpr, _enum};
inline std::string constid_tostr(constid_array id){
switch(id){
case constid_array::_const: return constid::_const;
case constid_array::_constexpr: return constid::_constexpr;
default: assert(false); return "";
}
}
inline bool has_constexpxr(const cpp_rev r){
return r >= cpp_rev::cpp11;
}
inline bool has_stdarr(const cpp_rev r){
return has_constexpxr(r); // both where introduced with c++11
}
inline bool has_generics(const java_rev r){
return r >= java_rev::j1_5;
}
struct lang_options_cpp{
bin2hpp::cpp_rev _rev;
bin2hpp::resource_type_cpp res;
bin2hpp::constid_array const_arr;
bin2hpp::constid_size const_size;
bool usepragma = false;
std::string _namespace = defaultnamespace;
explicit lang_options_cpp(const cpp_rev rev = cpp_rev::cpp17) :
_rev(rev),
res(has_stdarr(rev) ? resource_type_cpp::std_arr : resource_type_cpp::c_arr),
const_arr(has_constexpxr(rev) ? constid_array::_constexpr : constid_array::_const),
const_size(has_constexpxr(rev) ? constid_size::_constexpr : constid_size::_const)
{
}
};
struct lang_options_c{
bin2hpp::c_rev rev;
bin2hpp::constid_array const_arr = bin2hpp::constid_array::_const;
bin2hpp::constid_size const_size = bin2hpp::constid_size::_enum;
bool usepragma = false;
std::string _namespace = defaultnamespace;
explicit lang_options_c(const bin2hpp::c_rev rev_ = bin2hpp::c_rev::c11) : rev(rev_){}
};
struct lang_options_java{
bin2hpp::java_rev rev;
bin2hpp::resource_type_java res = bin2hpp::resource_type_java::byte_arr;
std::string class_name = defaultnamespace;
explicit lang_options_java(const bin2hpp::java_rev rev_ = bin2hpp::java_rev::j1_9) : rev(rev_){}
};
inline std::string constid_tostr(constid_size id){
switch(id){
case constid_size::_const: return constid::_const;
case constid_size::_constexpr: return constid::_constexpr;
case constid_size::_enum: return constid::_enum;
default: assert(false); return "";
}
}
// crea sequenza " 0xAB,0xCD,... " ritorna lunghezza dell'array
// fixme: controllare se servono cast per alcuni warnings
enum class format{c, java_byte, java_Byte, string};
inline size_t convertstreamtohexnotation(std::istream& in, std::ostream& out, const format f = format::c){
size_t totalsize = {};
std::array<unsigned char, 1024> buffer;
while(!in.eof()){
const auto readed = in.read(reinterpret_cast<char*>(&buffer.at(0)), buffer.size()).gcount();
assert(readed >= 0);
const auto unsigned_readed = static_cast<std::make_unsigned<decltype(readed)>::type>(readed);
// fixme: if totalsize >int_max-readed throw error "file is too big" as it can' be saved in an array
totalsize += unsigned_readed;
if(in.fail() && !in.eof()){ // something went wrong, but we where not at the end of the file
throw std::runtime_error("error reading stream");
}
// fixme: error checking
// suppongo legga tutto --> altrimenti exception!
//if(in.) --> check se qualcosa andato male
for(auto i = decltype(unsigned_readed){0} ; i != unsigned_readed; ++i){
auto c = hexutils::to_hex(buffer.at(i));
if(f == format::c){
// notation '\xaa' can lead to problems..
out << "0x" << c[0] << c[1] << ","; // fixme: format should be specified with out param...
} else if (f == format::java_byte){
out << "(byte)0x" << c[0] << c[1] << ","; // fixme: format should be specified with out param...
}
}
}
return totalsize;
}
// crea constexpr_id std::array<unsigned char, size> variablename = { "0xAB", "0xCD" };
// tutto tabbed di 1\t
inline void create_std_array(std::istream& in, const std::string& variablename, constid_array c_a, std::ostream& out){
std::stringstream tmp;
const auto size = convertstreamtohexnotation(in, tmp);
out << constid_tostr(c_a) << " std::array<unsigned char," << size << "> " << variablename << " = {\n";
out << tmp.rdbuf() << "\n";
out << "};\n";
}
inline void create_std_string(std::istream& in, const std::string& variablename, constid_array c_a, std::ostream& out){
out << "const std::string " << variablename << " = \"";
std::string tmp;
while(std::getline (in,tmp)){
out << tmp << "\\n\\\n";
}
// fixme: ad error checking!
out << "\";";
}
inline void create_c_array(std::istream& in, const std::string& variablename, constid_array c_a, constid_size c_s, bool iscpp, std::ostream& out){
assert(iscpp || c_a != constid_array::_constexpr && "c does not support constexpr");
assert(iscpp || c_s != constid_size::_constexpr && "c does not support constexpr");
std::stringstream tmp;
const auto size = convertstreamtohexnotation(in, tmp);
if(c_s==constid_size::_enum){
out << constid_tostr(c_s) << (iscpp ? " : std::size_t " : " ") << "{" << variablename << "_size = " << size << "};\n";
} else {
out << (iscpp ? "" : "static ") << constid_tostr(c_s) << (iscpp ? " std::" : " ") << "size_t " << variablename << "_size = " << size << ";\n";
}
out << (iscpp ? "" : "static ") << constid_tostr(c_a) << " unsigned char " << variablename << "[" << size << "] " << "= {\n";
out << tmp.rdbuf() << "\n";
out << "};\n";
}
inline void create_byte_array(std::istream& in, const std::string& variablename, std::ostream& out){
out << "public static final byte[] " << variablename << " = { ";
convertstreamtohexnotation(in, out, format::java_byte);
out << "};\n";
}
inline void create_file(std::istream& in, const lang_options_cpp& op, const std::string& variablename, std::ostream& out) {
if(op.usepragma){
out << "#pragma once\n\n";
} else {
// fixme --> uni as external dependency to unittest
std::random_device rd; // only used once to initialise (seed) engine
std::mt19937 rng(rd()); // random-number engine used (Mersenne-Twister in this case)
std::uniform_int_distribution<int> uni(0,std::numeric_limits<int>::max()); // guaranteed unbiased
auto random_integer = uni(rng);
const std::string header = "HEADER_"+variablename+"_" +std::to_string(random_integer);
out << "#ifndef " << header << "\n";
out << "#define " << header << "\n\n";
}
out << "// " << comment << "\n\n";
switch (op.res) {
case bin2hpp::resource_type_cpp::std_arr:
out << "#include <array>\n\n";
break;
case bin2hpp::resource_type_cpp::std_string:
out << "#include <string>\n\n";
break;
case bin2hpp::resource_type_cpp::c_arr:
case bin2hpp::resource_type_cpp::c_string:
out << "#include <cstddef>\n\n";
break;
default:
assert(false);
break;
}
if(!op._namespace.empty()){
out << "namespace " << op._namespace << " {\n\n";
}
switch (op.res) {
case bin2hpp::resource_type_cpp::std_arr:
create_std_array(in, variablename, op.const_arr, out);
break;
case bin2hpp::resource_type_cpp::std_string:
create_std_string(in, variablename, op.const_arr, out);
break;
case bin2hpp::resource_type_cpp::c_arr:
create_c_array(in, variablename, op.const_arr, op.const_size, true, out);
break;
case bin2hpp::resource_type_cpp::c_string:
create_c_array(in, variablename, op.const_arr, op.const_size, true, out);
break;
default:
assert(false);
break;
}
// close namespace
if(!op._namespace.empty()){
out << "\n}\n\n";
}
// close header guard
if(!op.usepragma){
out << "\n#endif\n";
}
}
inline void create_file(std::istream& in, const lang_options_c& op, const std::string& variablename, std::ostream& out) {
if(op.usepragma){
out << "#pragma once\n\n";
} else {
// fixme --> uni as external dependency to unittest
std::random_device rd; // only used once to initialise (seed) engine
std::mt19937 rng(rd()); // random-number engine used (Mersenne-Twister in this case)
std::uniform_int_distribution<int> uni(0,std::numeric_limits<int>::max()); // guaranteed unbiased
auto random_integer = uni(rng);
const std::string header = "HEADER_"+variablename+"_" +std::to_string(random_integer);
out << "#ifndef " << header << "\n";
out << "#define " << header << "\n\n";
}
if(op.rev == bin2hpp::c_rev::c89){
out << "/* " << comment << "*/\n\n";
} else {
out << "// " << comment << "\n\n";
}
if(op.const_size != constid_size::_enum){
out << "#include <stddef.h>\n\n";
}
create_c_array(in, op._namespace + "_" + variablename, constid_array::_const, op.const_size, false, out);
// close header guard
if(!op.usepragma){
out << "\n#endif\n";
}
}
inline void create_file(std::istream& in, const lang_options_java& op, const std::string& variablename, const std::string& package, std::ostream& out) {
if(!package.empty()){
out << "package " << package << ";\n";
}
out << "// " << comment << "\n\n";
out << "class " << op.class_name << "{\n";
create_byte_array(in, variablename, out);
out << "}\n\n";
}
}
#endif