-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
377 lines (305 loc) · 9.96 KB
/
main.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
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
375
376
377
#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
#include "bifstream.h"
#include "bofstream.h"
#include "Byte.h"
#include "charCountNode.h"
typedef std::vector<bool> Bitset;
typedef std::map<Byte, Bitset> CharMap;
const bool LEFT = true;
const bool RIGHT = false;
CharMap getCharMap(std::vector<unsigned char> &dataString);
std::vector<CharCountNode> getCharFreqs(std::vector<unsigned char> &dataString);
void traverseTree(CharCountNode *currentNode, CharMap &charMap, Bitset &path);
std::vector<unsigned char> getString(std::string inputFilePath, bool &worked);
bool writeKeyToFile(bofstream &outputFile, CharMap &map);
bool writeHumanReadableKeyToFile(std::ofstream &outputFile, CharMap &map);
bool writeMessageToFile(std::vector<unsigned char> &dataString, bofstream &outputFile, CharMap &map);
void printBitset(Bitset bits);
Bitset charToBitset(char c);
CharMap readKeyFromFile(bifstream &input);
std::string readMessageFromFile(bifstream &input, CharMap &charMap);
void writeKeyAndMessage();
void decodeMessage();
int main()
{
writeKeyAndMessage();
decodeMessage();
}
void writeKeyAndMessage()
{
bool worked;
std::string originalTextPath = "Huffman\\original.txt";
std::vector<unsigned char> dataString = getString(originalTextPath, worked);
if (!worked)
{
std::cout << "Failed opening the file at '" << originalTextPath.c_str() << "'. Aborting\n";
return;
}
CharMap map = getCharMap(dataString);
bofstream keyOutput;
std::string keyOutputPath = "Huffman\\keyOutput.key";
keyOutput.open(keyOutputPath.c_str());
if (keyOutput.fail())
{
std::cout << "Failed opening the file at '" << keyOutputPath.c_str() << "'. Aborting\n";
return;
}
writeKeyToFile(keyOutput, map);
bofstream messageOutput;
std::string messageOutputPath = "Huffman\\messageOutput.huff";
messageOutput.open(messageOutputPath.c_str());
if (messageOutput.fail())
{
std::cout << "Failed opening the file at '" << messageOutputPath.c_str() << "'. Aborting\n";
return;
}
std::ofstream humanReadableOutput;
std::string humanReadableOutputPath = "Huffman\\humanReadableKey.txt";
humanReadableOutput.open(humanReadableOutputPath.c_str());
if (humanReadableOutput.fail())
{
std::cout << "Failed opening the file at '" << humanReadableOutputPath.c_str() << "'. Aborting\n";
return;
}
writeHumanReadableKeyToFile(humanReadableOutput, map);
writeMessageToFile(dataString, messageOutput, map);
keyOutput.close();
messageOutput.close();
}
void decodeMessage()
{
bifstream keyInput;
std::string keyInputPath = "Huffman\\keyOutput.key";
keyInput.open(keyInputPath.c_str());
if (keyInput.fail())
{
std::cout << "Failed opening the file at '" << keyInputPath.c_str() << "'. Aborting\n";
return;
}
CharMap mapFromFile = readKeyFromFile(keyInput);
bifstream messageInput;
std::string messageInputPath = "Huffman\\messageOutput.huff";
messageInput.open(keyInputPath.c_str());
if (messageInput.fail())
{
std::cout << "Failed opening the file at '" << messageInputPath.c_str() << "'. Aborting\n";
return;
}
std::string message = readMessageFromFile(messageInput, mapFromFile);
std::ofstream outputOriginal;
std::string outputOriginalPath = "Huffman\\decodedMessage.txt";
outputOriginal.open(outputOriginalPath.c_str());
if (outputOriginal.fail())
{
std::cout << "Failed opening the file at '" << outputOriginalPath.c_str() << "'. Aborting";
return;
}
outputOriginal << message.c_str();
std::ofstream humanReadableOutput;
std::string humanReadableOutputPath = "Huffman\\humanReadableKeyDecoded.txt";
humanReadableOutput.open(humanReadableOutputPath.c_str());
if (humanReadableOutput.fail())
{
std::cout << "Failed opening the file at '" << humanReadableOutputPath.c_str() << "'. Aborting\n";
return;
}
writeHumanReadableKeyToFile(humanReadableOutput, mapFromFile);
outputOriginal.close();
keyInput.close();
messageInput.close();
}
std::string readMessageFromFile(bifstream &NOTinput, CharMap &charMap)
{
//Construct a map of <Bitset, Byte> rather than <Byte, Bitset>
std::map<Bitset, Byte> bitMap;
for (auto iter = charMap.begin(); iter != charMap.end(); iter++)
bitMap[iter->second] = iter->first;
std::string result;
Bitset currentBits;
bifstream input;
input.open("Huffman\\messageOutput.huff");
if (input.fail())
return "";
while (!input.eof())
{
currentBits.push_back(input.get());
if (bitMap.find(currentBits) != bitMap.end())
{
if (bitMap[currentBits].getIsTerminator())
break;
result += bitMap[currentBits].getChar();
currentBits = {};
}
}
return result;
}
std::vector<unsigned char> getString(std::string inputFilePath, bool &worked)
{
std::vector<unsigned char> string;
std::ifstream inputFile;
inputFile.open(inputFilePath.c_str(), std::ios::in | std::ios::binary);
worked = inputFile.good();
if (worked)
{
while (true)
{
unsigned char c = inputFile.get();
if (inputFile.fail())
break;
string.push_back(c);
}
}
inputFile.close();
return string;
}
CharMap readKeyFromFile(bifstream &input)
{
CharMap map;
bool firstByte = true;
while (true)
{
unsigned char c = input.getByte();
Bitset key;
Bitset nextBitPair = input.get(2);
while (nextBitPair[0])
{
key.push_back(nextBitPair[1]);
nextBitPair = input.get(2);
if (input.fail())
break;
}
map.insert(std::pair<Byte, Bitset>(Byte(c, firstByte), key));
if (nextBitPair[1] == 1)
break;
firstByte = false;
}
return map;
}
bool writeKeyToFile(bofstream &outputFile, CharMap &map)
{
for (auto iter = map.begin(); iter != map.end(); iter++)
{
unsigned char c = iter->first.getChar();
Bitset key = iter->second;
outputFile.put(charToBitset(c));
for (auto bit: key)
{
outputFile.put(true);
outputFile.put(bit);
}
outputFile.put(false);
if (std::next(iter) == map.end())
outputFile.put(true);
else
outputFile.put(false);
}
return true;
}
bool writeHumanReadableKeyToFile(std::ofstream &outputFile, CharMap &map)
{
auto i = map.find(Byte(0x00, false));
if (i != map.end())
int i = 7;
for (auto iter = map.begin(); iter != map.end(); iter++)
{
outputFile << iter->first.getPrintable() << iter->first.getHexPrintable();
for (auto i: iter->second)
outputFile << int(i);
outputFile << "\n";
}
return true;
}
bool writeMessageToFile(std::vector<unsigned char> &dataString, bofstream &outputFile, CharMap &map)
{
for (auto c: dataString)
{
outputFile.put(map[c]);
}
outputFile.put(map.begin()->second); //The first value in the map is always the terminator
outputFile.close();
return true;
}
CharMap getCharMap(std::vector<unsigned char> &dataString)
{
CharMap map;
std::vector<CharCountNode> freqs = getCharFreqs(dataString);
freqs.push_back(CharCountNode(Byte(0x00, true), 1)); //The terminator
if (freqs.size() == 1) //In case the program is given an empty file. Otherwise, we wouuld get an error dereferencing the top of an empty heap.
{
map[freqs[0].byte] = std::vector<bool> {true};
return map;
}
std::priority_queue<CharCountNode, std::vector<CharCountNode>, std::greater<CharCountNode>> heap(freqs.begin(), freqs.end());
while (heap.size() > 2)
{
CharCountNode *left = new CharCountNode(heap.top());
std::ifstream originalInputFile;
heap.pop();
CharCountNode *right = new CharCountNode(heap.top());
heap.pop();
CharCountNode newNode(0, right->count + left->count); //The "char" value (0) is irrelevant since we will traverse through this node without looking at the char.
newNode.left = left;
newNode.right = right;
heap.push(newNode);
}
CharCountNode *left = new CharCountNode(heap.top());
heap.pop();
CharCountNode *right = new CharCountNode(heap.top());
heap.pop();
CharCountNode *head = new CharCountNode(0, right->count + left->count);
head->left = left;
head->right = right;
Bitset path = {};
traverseTree(head, map, path);
return map;
}
std::vector<CharCountNode> getCharFreqs(std::vector<unsigned char> &dataString)
{
std::vector<CharCountNode> freqs;
for (unsigned int i = 0; i < 256; i++)
freqs.push_back(CharCountNode((unsigned char) i, 0));
std::string data = "";
for (auto byte: dataString)
{
freqs[byte].count++;
}
freqs.erase(std::remove_if(freqs.begin(), freqs.end(), isEmptyNode), freqs.end());
return freqs;
}
void traverseTree(CharCountNode *currentNode, CharMap &map, Bitset &path)
{
if (currentNode->left != nullptr && currentNode->right != nullptr) //Not to the bottom yet, recurse again.
{
path.push_back(LEFT);
traverseTree(currentNode->left, map, path);
path.pop_back();
path.push_back(RIGHT);
traverseTree(currentNode->right, map, path);
path.pop_back();
}
else //Hit the buttom, add this value to the map
{
auto iter = map.begin();
map.insert(iter, std::pair<Byte, Bitset>(currentNode->byte, path));
}
delete currentNode;
}
void printBitset(Bitset bits)
{
for (auto i : bits)
std::cout << (i) ? "1" : "0";
}
Bitset charToBitset(char c)
{
Bitset bits = {};
for (int i = 7; i >= 0; i--)
{
bits.push_back((c >> i) & 0x01);
}
return bits;
}