-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecompressor.cpp
More file actions
138 lines (122 loc) · 3.15 KB
/
decompressor.cpp
File metadata and controls
138 lines (122 loc) · 3.15 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
#include "decompressor.h"
#include <cstddef>
#include <fstream>
#include <memory>
#include <vector>
#include "binary_trie.h"
#include "bit_stream.h"
#include "constants.h"
#include "exceptions.h"
#include "files.h"
#include "huffman.h"
Decompressor::Decompressor(Path filename) : is_(filename), input_(is_) {
}
void Decompressor::OpenFile(Path filename) {
if (!ValidateOutput(filename)) {
throw OutputError();
}
os_ = std::ofstream(filename);
}
void Decompressor::Reset() {
codes_.clear();
trie_ = std::make_shared<BinaryTrie>();
}
Char Decompressor::ReadNumber() {
Char number = 0;
try {
number = input_.ReadBits<Char>(9);
} catch (const BitReader::EndOfFile& ex) {
throw InvalidFormat();
}
if (number > 259) {
throw InvalidFormat();
}
return number;
}
void Decompressor::ReadHeader() {
std::size_t count = ReadNumber();
std::vector<Char> alphabet(count);
for (Char& c : alphabet) {
c = ReadNumber();
}
std::size_t current = 0;
CodeSizes sizes(count);
for (std::size_t size = 1; current < count; ++size) {
std::size_t size_count = ReadNumber();
if (current + size_count > count) {
throw InvalidFormat();
}
for (std::size_t i = 0; i < size_count; ++i, ++current) {
sizes[current] = {alphabet[current], size};
}
}
codes_ = CanonicalCodes(sizes);
}
void Decompressor::GenerateTrie() {
for (const auto& [key, code] : codes_) {
try {
trie_->AddCode(code.code, code.size, key);
} catch (const BinaryTrie::CodeAlreadyExists& ex) {
throw InvalidFormat();
}
}
}
Char Decompressor::ReadSymbol(const BinaryTrie& node) {
if (node.IsTerminal()) {
return node.key_;
}
if (node.IsLeaf()) {
throw InvalidFormat();
}
bool bit = false;
try {
bit = input_.ReadBit();
} catch (const BitReader::EndOfFile& ex) {
throw InvalidFormat();
}
if (!bit) {
return ReadSymbol(node.CLeft());
} else {
return ReadSymbol(node.CRight());
}
}
Char Decompressor::ReadSymbol() {
return ReadSymbol(*trie_);
}
std::string Decompressor::ReadFilename() {
std::string filename;
while (true) {
Char symbol = ReadSymbol();
if (symbol > FILENAME_END) {
throw InvalidFormat();
} else if (symbol == FILENAME_END) {
return filename;
} else {
filename += static_cast<char>(symbol);
}
}
}
bool Decompressor::DecompressFile() {
Reset();
ReadHeader();
GenerateTrie();
Path filename = ReadFilename();
OpenFile(filename);
while (true) {
Char symbol = ReadSymbol();
if (symbol == FILENAME_END) {
throw InvalidFormat();
} else if (symbol == ONE_MORE_FILE) {
return true;
} else if (symbol == END_OF_ARCHIVE) {
return false;
} else {
os_.put(static_cast<char>(symbol));
}
}
}
void Decompress(Path archive_name) {
Decompressor decompressor(archive_name);
while (decompressor.DecompressFile()) {
}
}