-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.cpp
91 lines (78 loc) · 2.55 KB
/
convert.cpp
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
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
// Find the number of entries to be created in the array
unsigned int entriesCount(string fileName) {
unsigned int size = 0;
ifstream myFile(fileName, ios::binary | ios::ate);
if (myFile.is_open()) {
size = myFile.tellg();
} else {
cout << "Could not open file.";
}
myFile.close();
return size;
}
// Reads the binary file and returns a vector of numbers that are evaluated
// by casting the character to unsigned int as the data originally was
vector<unsigned int> readDafsaBinary(string fileName) {
unsigned int length = entriesCount(fileName);
vector<char> buffer(length, 0);
ifstream myFile(fileName, ios::binary);
if (myFile.is_open()) {
myFile.read(&buffer[0], length);
} else {
cout << "Could not open file.";
}
// Casting characters to unsigned int as observed in make_dafsa.py
vector<unsigned int> numbers;
for (auto buf : buffer) {
unsigned int num = (unsigned char)(buf);
numbers.push_back(num);
}
myFile.close();
return numbers;
}
// Converts the numbers to hex and formats them to follow C++ array syntax as
// created by make_dafsa.py
void makeHexArray(string filename, vector<unsigned int> numbers) {
ofstream outfile(filename);
string text = "/* This file is generated. DO NOT EDIT!\n\n";
text += "The byte array encodes a dictionary of strings and values. See ";
text += "convert.cpp/make_dafsa.py for documentation.";
text += "*/\n\n";
text +=
"const unsigned char kDafsa[" + to_string(numbers.size()) + "] = {\n";
// Used for simple formatting for readability of output
int formatCount = 1;
for (auto num : numbers) {
text += " ";
stringstream stream; // called inside thee loop to reset its value
// same thing as '0x%02x', formats to get atleast two digits after hex
// conversion
stream << setfill('0') << setw(2) << hex << num;
text += "0x" + stream.str();
if (formatCount % 12 == 0) {
text += ",\n";
} else {
text += ",";
}
++formatCount;
}
text += "\n";
text += "};\n";
outfile << text;
outfile.close();
}
int main(int argc, char* argv[]) {
if (argc != 3) {
cout << "Enter the correct number of arguments." << endl;
return -1;
}
vector<unsigned int> numbers = readDafsaBinary(argv[1]);
makeHexArray(argv[2], numbers);
return 0;
}