-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbin2cpp.cpp
More file actions
139 lines (107 loc) · 3.95 KB
/
bin2cpp.cpp
File metadata and controls
139 lines (107 loc) · 3.95 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
#include <iostream>
#include <algorithm>
#include <filesystem>
#include <string>
#include <fstream>
#include <cmath>
#include <ctime>
namespace fs = std::filesystem;
// Quick String Filter
bool filterString(std::string& str, const std::string& substr, const std::string& newstr){
auto pos = str.find(substr);
if(pos == std::string::npos) return false;
str.replace(pos, substr.size(), newstr);
return true;
}
// Convert To C++ Char Array
bool bin2cpp(const std::string& name, const std::string& in, const std::string& out) {
std::string out_cpp = out + ".cpp", out_h = out + ".h", macrodefine = name;
std::ifstream input(in, std::ios::binary | std::ios::in);
if(!input.is_open()){
std::cout << "Failed to read binary file" << std::endl;
return false;
}
input.seekg(0, input.end);
size_t flen = input.tellg(); // get file length
input.seekg(0, input.beg);
fs::path cppfile(out_cpp);
std::ofstream output(out_cpp, std::ios::binary | std::ios::out);
if(!output.is_open()){
std::cout << "Failed to open " << (cppfile.filename().string() + cppfile.extension().string()) << " file" << std::endl;
input.close();
return false;
}
std::cout << "Converting binary to c++..." << std::endl;
output << "#include \"" << out_h << "\"\nconst char " << name << "[" << flen << "] = {"; // header information
bool begin = true;
while(!input.eof()){ // append data converted to char value array
char buf[1024];
size_t len = input.readsome(buf, 1024);
for(size_t i=0; i < len; ++i){
if(begin){
output << (int)buf[i];
begin = false;
} else {
output << "," << (int)buf[i];
}
}
if(len == 0) break;
}
output << "};";
input.close();
output.close();
fs::path hfile(out_h);
// Write header file
std::ofstream header(out_h, std::ios::binary | std::ios::out);
if(!header.is_open()){
std::cout << "Failed to open " << (hfile.filename().string() + hfile.extension().string()) << " file" << std::endl;
return false;
}
// Upper case macro definition
std::transform(macrodefine.begin(), macrodefine.end(), macrodefine.begin(),
[](unsigned char c) -> unsigned char { return std::toupper(c); });
header <<
"#ifndef __" << macrodefine << "__\n" <<
"#define __" << macrodefine << "__\n\n" <<
"extern const char " << name << "[" << flen << "];\n\n" <<
"#endif // __" << macrodefine << "__";
header.close();
return true;
}
// Main Program
int main(int argc, char** argv) {
srand(time(0));
// Display Help
if(argc < 2) {
std::cout << "==== Bin2Cpp -> Convert a binary file to a raw cpp file for embedded data ====\n"
<< "Usage:\n"
<< "bin2cpp <input-file> *<resource-name> (resource$n) *<output-file> (binary$n)\n"
<< "$n = random number\n"
<< "<argument value>\n"
<< "*<optional argument>\n"
<< "(default value)\n"
<< "\n"
<< "Examples:\n"
<< "bin2cpp file.ext\n"
<< "bin2cpp test.txt TestTextFile\n"
<< "bin2cpp run.exe RunExe bin_runexe\n";
return 0;
}
std::string input = argv[1],
name = "resource$n",
output = "binary$n";
if(argc > 2){
name = argv[2]; // update resource name
}
if(argc > 3){
output = argv[3]; // update output filename
}
do {
// random number substitution
if(filterString(name, "$n", std::to_string(rand()))) continue;
if(filterString(input, "$n", std::to_string(rand()))) continue;
if(filterString(output, "$n", std::to_string(rand()))) continue;
break;
} while(1);
return !bin2cpp(name, input, output);
}