-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecrypt.cpp
More file actions
65 lines (64 loc) · 1.7 KB
/
decrypt.cpp
File metadata and controls
65 lines (64 loc) · 1.7 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
#include "decrypt.h"
#include "treelib.h"
#include <algorithm>
void decrypt(IStreamWrapper & in, OStreamWrapper & out)
{
long unsigned int filesize = in.size();
//return if empty
if (!filesize)
return;
bool cont = true;
std::vector<short> encNodes;
//filesize now represents size of output file
filesize = 0;
//keep track of all bits read
unsigned long int bitct = 0;
//get all encnodes
while (cont)
{
//read node header
unsigned char b1 = in.readBit();
bitct++;
unsigned char b2 = in.readBit();
bitct++;
//file is either corrupt or only header is present
//(both of which are bad)
if (b1 == -1 || b2 == -1)
{
cont = false;
continue;
}
//no node has 11 as header
if (b1 == 1 && b2 == 1)
{
//read enough zeros to space out
for (long unsigned int i = 0; i < (8 - (bitct % 8)); i++)
{
in.readBit();
}
//get output filesize
filesize = in.readLong();
//we have now reached beginning of data
cont = false;
continue;
}
//read 8 bits and add to cur
int cur = 0;
for (int i = 0; i < 8; i++)
{
cur += (in.readBit()) << i;
bitct++;
}
//add to encnodes
short nodeheader = ((b1 << 1) + b2) << 8;
encNodes.push_back(nodeheader + cur);
}
//construct huffman tree
Tree tree;
//build huffman tree
tree.build(encNodes);
//initialize symbol dictionary
tree.initSymbs();
//decode file
tree.decode(in, out, filesize);
}