-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbin2cpp.cpp
More file actions
274 lines (254 loc) · 7.93 KB
/
bin2cpp.cpp
File metadata and controls
274 lines (254 loc) · 7.93 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
/*
Compile with GCC 13.2 or greater
bin2cpp20 - conversion tool
*/
#include "argz.hpp"
#include <algorithm>
#include <cstdint>
#include <cstdlib>
#include <format>
#include <fstream>
#include <iostream>
#include <memory>
#include <ranges>
#include <regex>
#include <sstream>
#include <string>
#include <vector>
#define VERSION_INFO "1.1.1"
void convertStreamToVector(std::string_view name, std::istream &in, std::ostream &out);
void convertStreamToArray(bool c_expr, std::string_view name, const char *data, std::size_t length, std::ostream &out);
void convertStreamToString(bool unicode, bool sorted, std::string_view name, std::istream &in, std::ostream &out);
void stringOutputVector(const std::vector<unsigned char> &v);
template <std::size_t N>
void stringOutputArray(std::array<unsigned char, N> &a);
bool is_Valid(const std::string &name);
std::string convertString(const std::string &text);
int main(int argc, char **argv) {
if(argc == 1) {
convertStreamToVector("bin_vec", std::cin, std::cout);
return EXIT_SUCCESS;
}
try {
Argz<std::string> argz{argc, argv};
argz.addOptionSingleValue('i', "input file/stdin").addOptionDoubleValue('I', "input", "input file/stdin").addOptionSingle('c', "Use constexpr").addOptionDouble('C', "constexpr", "Use constexpr").addOptionSingleValue('o', "output").addOptionDoubleValue('O', "output", "output file").addOptionSingle('h', "help").addOptionDouble('H', "help", "help message").addOptionSingleValue('v', "variable name").addOptionDoubleValue('V', "variable", "variable name").addOptionSingle('s', "string output").addOptionDouble('S', "string", "string output").addOptionSingle('z', "sort").addOptionDouble('Z', "sort", "sort string").addOptionSingle('u', "Unicode").addOptionDouble('U', "unicode", "unicode");
Argument<std::string> arg;
int value{};
std::string input_file, output_file, variable_name;
bool as_string {false};
bool sorted{false};
bool unicode{false};
bool c_expr{false};
while((value = argz.proc(arg)) != -1) {
switch(value) {
case 'i':
case 'I':
input_file = arg.arg_value;
break;
case 'o':
case 'O':
output_file = arg.arg_value;
break;
case 'H':
case 'h':
std::cout << "bin2cpp20 v" << VERSION_INFO << "\n";
std::cout << "written by Jared Bruni [ http://lostsidedead.biz ]\n";
argz.help(std::cout);
return EXIT_SUCCESS;
case 'v':
case 'V':
variable_name = arg.arg_value;
break;
case 's':
case 'S':
as_string = true;
break;
case 'z':
case 'Z':
sorted = true;
break;
case 'u':
case 'U':
unicode = true;
break;
case 'c':
case 'C':
c_expr = true;
break;
}
}
if(input_file.length() == 0) {
std::cerr << "Error input file not specified...\n";
argz.help(std::cout);
return EXIT_SUCCESS;
} else {
if(variable_name.length() == 0) {
std::cerr << "Requires variable name... use -v\n";
argz.help(std::cout);
return EXIT_FAILURE;
}
if(!is_Valid(variable_name)) {
std::cerr << "Error invalid variable name..\n";
return EXIT_FAILURE;
}
if(input_file == "stdin") {
if(output_file.length() == 0)
convertStreamToString(unicode, sorted, variable_name, std::cin, std::cout);
else {
std::fstream file;
const auto pos{output_file.rfind(".hpp")};
if(pos == std::string::npos)
output_file += ".hpp";
file.open(output_file, std::ios::out);
if(!file.is_open()) {
std::cerr << "Error could not open output file\n";
return EXIT_FAILURE;
}
file << "#ifndef __STR_H_HPP_" << variable_name << "\n";
file << "#define __STR_H_HPP_" << variable_name << "\n";
file << "#include<string>\n";
convertStreamToString(unicode, sorted, variable_name, std::cin, file);
file << "#endif\n\n";
file.close();
}
return EXIT_SUCCESS;
}
std::fstream file;
file.open(input_file, std::ios::in | std::ios::binary | std::ios::ate);
if(!file.is_open()) {
std::cerr << "Could not open file: " << input_file << "\n";
return EXIT_FAILURE;
}
std::streamsize len{file.tellg()};
file.seekg(0, std::ios::beg);
std::unique_ptr<char[]> buf{new char[len + 1]};
file.read(buf.get(), len);
file.close();
if(output_file.length() == 0) {
if(as_string == false) {
variable_name = "bin_" + variable_name;
convertStreamToArray(c_expr, variable_name + "_arr", buf.get(), len, std::cout);
} else {
variable_name = "str_" + variable_name;
std::istringstream in(buf.get());
convertStreamToString(unicode, sorted, variable_name, in, std::cout);
}
return EXIT_SUCCESS;
} else {
std::fstream file;
const auto pos{output_file.rfind(".hpp")};
if(pos == std::string::npos)
output_file += ".hpp";
file.open(output_file, std::ios::out);
if(!file.is_open()) {
std::cerr << "Error could not open output file..\n";
return EXIT_FAILURE;
}
file << "#ifndef __ARR_H_HPP_" << variable_name << "\n";
file << "#define __ARR_H_HPP_" << variable_name << "\n";
if(as_string == false) {
file << "#include<array>\n\n";
variable_name = "bin_" + variable_name;
convertStreamToArray(c_expr, variable_name + "_arr", buf.get(), len, file);
} else {
file << "#include<string>\n\n";
std::istringstream in{buf.get()};
convertStreamToString(unicode, sorted, variable_name, in, file);
}
file << "\n\n#endif\n";
file.close();
}
}
} catch(const ArgException<std::string> &e) {
std::cerr << "Syntax Error: " << e.text() << "\n";
}
return 0;
}
void convertStreamToVector(std::string_view name, std::istream &in, std::ostream &out) {
out << "inline const std::vector<unsigned chassr> " << name << " {";
while(!in.eof()) {
uint8_t c{};
in.read(reinterpret_cast<char *>(&c), sizeof(uint8_t));
const std::string hex{std::format("0x{:X}", static_cast<uint8_t>(c))};
if(in)
out << hex << ",";
else
out << hex;
}
out << "};\n";
}
void convertStreamToArray(bool c_expr, std::string_view name, const char *data, std::size_t length, std::ostream &out) {
std::string e_type{"inline"};
if(c_expr == true)
e_type = "constexpr";
out << e_type << " std::array<unsigned char, " << length + 1 << "> " << name << " {";
for(std::size_t i = 0; i < length; ++i) {
const std::string hex{std::format("0x{:X}", static_cast<uint8_t>(data[i]))};
out << hex << ",";
}
const std::string hex{std::format("0x{:X}", 0x0)};
out << hex << "};\n";
}
void convertStreamToString(bool unicode, bool sorted, std::string_view name, std::istream &in, std::ostream &out) {
if(unicode == false)
out << "inline const std::string " << name << "[] = {";
else
out << "inline const std::wstring " << name << "[] = {";
std::size_t index{};
std::size_t length{};
std::vector<std::string> v;
while(!in.eof()) {
std::string line;
std::getline(in, line);
if(in && line.length() > 0) {
v.push_back(line);
length += line.length() + 1;
}
}
if(sorted) {
std::sort(v.begin(), v.end());
}
for(auto &line : v) {
index += line.length() + 1;
if(line.length() > 0) {
if(unicode == false)
out << "\"" << convertString(line) << "\"";
else
out << "L\"" << convertString(line) << "\"";
if(index < length)
out << ",";
}
}
out << "};\n";
out << "inline unsigned long " << name << "_length {" << v.size() << "};\n";
}
void stringOutputVector(const std::vector<unsigned char> &v) {
for(const auto &e : std::views::all(v)) {
std::cout << e;
}
}
template <std::size_t N>
void stringOutputArray(std::array<unsigned char, N> &a) {
for(const auto &i : std::views::all(a)) {
std::cout << i;
}
}
bool is_Valid(const std::string &name) {
std::regex pattern{"^[a-zA-Z_][a-zA-Z0-9_]*"};
return std::regex_match(name, pattern);
}
std::string convertString(const std::string &s) {
std::string temp;
for(std::size_t i = 0; i < s.length(); ++i) {
if(s[i] == '\r') {
continue; // eat token
} else
if(s[i] == '\"' || s[i] == '\\') {
temp += "\\";
temp += s[i];
} else {
temp += s[i];
}
}
return temp;
}