-
Notifications
You must be signed in to change notification settings - Fork 0
/
huffman.cpp
444 lines (356 loc) · 11.1 KB
/
huffman.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
#include "huffman.hpp"
#include <queue>
#include <stack>
//#include <fstream>
#include <memory>
//#include <cstring>
#define LEFT 0
#define RIGHT 1
//#define PREV 0
//#define NEXT 1
using namespace std;
namespace fs = std::filesystem;
//template <typename T>
//struct greater<T*>
//{
// bool operator()(const T* left, const T* right)
// {
// return *left > *right;
// }
//};
template <typename T>
struct ptr_greater
{
bool operator()(const T* left, const T* right)
{
return *left > *right;
}
};
namespace Huffman
{
size_t WritePath(ostream& os, const NameType* str)
{
auto cur = str;
while (*cur) cur++;
os.write((char*)str, (cur - str) * sizeof(NameType));
return (cur - str);
}
static void BuildCodeTable(const HufNode* tree, size_t size, bool code[], vector<Code>& code_table);
inline void FillTokenTable(istream& is, vector<size_t>& token_table);
// preprocessing for encoding-----------------------------------------------
inline void FillTokenTable(istream& is, vector<size_t>& token_table)
{
while (is.peek() != EOF)
token_table[(token_t)is.get()]++;
}
vector<size_t> MakeTokenTable(istream& is)
{
vector<size_t> token_table(TOKEN_MAX);
FillTokenTable(is, token_table);
return token_table;
}
// 메모리 누수 고려 없음
HufNode* MakePrefixTree(const vector<size_t>& token_table)
{
priority_queue<HufNode*, vector<HufNode*>, ptr_greater<HufNode>> token_queue;
// 큐 채우기
for (int i = 0; i < TOKEN_MAX; i++) {
if (token_table[i] > 0) {
TokenCount token_cnt{ i, token_table[i] };
token_queue.push(new HufNode{ token_cnt });
}
}
if (token_queue.empty()) return nullptr;
// 트리 구성
while (token_queue.size() > 1) {
HufNode* left = token_queue.top();
token_queue.pop();
HufNode* right = token_queue.top();
token_queue.pop();
HufNode* newNode = new HufNode{};
newNode->get().count = left->get().count + right->get().count;
newNode->set_link(LEFT, left);
newNode->set_link(RIGHT, right);
token_queue.push(newNode);
}
return token_queue.top();
}
vector<Code> MakeCodeTable(const HufNode* tree)
{
bool code[TOKEN_MAX];
vector<Code> code_table(TOKEN_MAX);
if (tree) BuildCodeTable(tree, 0, code, code_table);
return code_table;
}
static void BuildCodeTable(const HufNode* node, size_t size, bool code[], vector<Code>& code_table)
{
// 트리의 자식의 개수는 2개이거나 0개이므로 양쪽 다 검사할 필요 없음
if (node->link(LEFT)) {
code[size] = LEFT;
BuildCodeTable(node->link(LEFT), size + 1, code, code_table);
code[size] = RIGHT;
BuildCodeTable(node->link(RIGHT), size + 1, code, code_table);
}
else {
for (int i = 0; i < size; i++)
code_table[node->get().token].code.set(i, code[i]);
code_table[node->get().token].size = size;
}
}
uint16_t BuildTokenRecords(const HufNode* node, TokenRecord token_records[])
{
if (!node) return 0;
stack<const HufNode*> node_stack;
stack<token_t> level_stack;
uint16_t num_of_records = 0;
node_stack.push(node);
level_stack.push(0); // 레벨은 0부터 시작함.
while (!node_stack.empty()) {
node = node_stack.top();
node_stack.pop();
token_t level = level_stack.top();
level_stack.pop();
if (node->link(LEFT)) {
node_stack.push(node->link(RIGHT));
level_stack.push(level + 1);
node_stack.push(node->link(LEFT));
level_stack.push(level + 1);
}
else {
token_records[num_of_records].level = level;
token_records[num_of_records].token = node->get().token;
num_of_records++;
}
}
return num_of_records;
}
// 메모리 누수 고려 없음
HufNode* DecodeTokenRecords(const TokenRecord token_records[], uint16_t records_size)
{
stack<HufNode*> tree_stack;
for (uint16_t i = 0; i < records_size; i++) {
HufNode* new_node = new HufNode{ token_records[i].token,
token_records[i].level };
while (!tree_stack.empty() &&
tree_stack.top()->get().count == new_node->get().count) {
HufNode* left = tree_stack.top();
tree_stack.pop();
HufNode* right = new_node;
new_node = new HufNode{};
new_node->get().count = left->get().count - 1;
new_node->set_link(LEFT, left);
new_node->set_link(RIGHT, right);
}
tree_stack.push(new_node);
}
if (tree_stack.size() == 1)
return tree_stack.top();
else {
while (!tree_stack.empty()) {
DestroyPODNodes(tree_stack.top());
tree_stack.pop();
}
return nullptr;
}
}
// encoding process-------------------------------------------------------------
int ConvertToHufCode(istream& is, ostream& os, const vector<Code>& code_table)
{
token_t character = 0;
int current_bit = 0;
while (is.peek() != EOF) {
token_t idx = is.get();
for (int i = 0; i < code_table[idx].size; i++) {
character |= code_table[idx].code.test(i) << current_bit;
if (++current_bit == TOKEN_BITS) { // 하나씩 하지말고 한번에 하게 바꿀 것
os.put(character);
current_bit = character = 0;
}
}
}
if (current_bit)
os.put(character);
return (TOKEN_BITS - current_bit) % TOKEN_BITS;
}
void Encode(istream& is, ostream& os, const vector<Code>& code_table, const HufNode* tree)
{
// 토큰 리코드 빌드
TokenRecord token_records[TOKEN_MAX];
uint16_t records_size = BuildTokenRecords(tree, token_records);
// 마지막에 쓰기위한 헤더 공간 확보
HufHeader header{ 0, records_size };
auto header_pos = os.tellp();
os.write((char*)&header, sizeof(HufHeader));
// 트리 저장
os.write((char*)token_records, sizeof(TokenRecord) * records_size);
// 허프만 코드로 변환
auto temp_os_pos = os.tellp();
header.padding_bits = ConvertToHufCode(is, os, code_table);
auto last_pos = os.tellp();
header.data_size = last_pos - temp_os_pos;
os.seekp(header_pos);
os.write((char*)&header, sizeof(HufHeader));
os.seekp(last_pos);
}
void Encoding(istream& is, ostream& os)
{
auto first_pos = is.tellg();
// 토큰 테이블
vector<size_t> token_table = MakeTokenTable(is);
// 트리
//PODNodeGuard<HufNode> tree{ MakePrefixTree(token_table) };
unique_ptr<HufNode, void(*)(HufNode*)> tree{ MakePrefixTree(token_table), &DestroyPODNodes };
/*if (!tree)
throw exception{ "Cannot build Huffman tree" };*/
//코드 테이블
vector<Code> code_table = MakeCodeTable(tree.get());
// 파일에 출력
is.clear();
is.seekg(first_pos);
Encode(is, os, code_table, tree.get());
}
void EncodeFile(const fs::path& file_path, ostream& os)
{
ifstream is{ file_path, ios_base::binary };
if (!is.good()) {
auto ec = make_error_code(huf_errc::invalid_fstream);
throw fs::filesystem_error{ "EncodeFile", file_path, ec };
}
FileHeader header{ TYPE_REGULAR_FILE };
auto header_pos = os.tellp();
os.write((char*)&header, sizeof(FileHeader));
header.name_size = WritePath(os, file_path.filename().c_str());
if (header.name_size >= FILENAME_MAX)
throw out_of_range{ "Invalid file name length: " + to_string(header.name_size) };
Encoding(is, os);
auto current_pos = os.tellp();
os.seekp(header_pos);
os.write((char*)&header, sizeof(FileHeader));
os.seekp(current_pos);
}
void EncodeDirectory(const fs::path& dir_path, ostream& os)
{
auto directory_iter = fs::directory_iterator(dir_path);
DirectoryHeader header{ TYPE_DIRECTORY };
auto header_pos = os.tellp();
os.write((char*)&header, sizeof(DirectoryHeader));
header.name_size = WritePath(os, dir_path.filename().c_str());
if (header.name_size >= FILENAME_MAX)
throw out_of_range{ "Invalid file name length: " + to_string(header.name_size) };
for (const auto& path : directory_iter) {
Compress(path, os);
header.data_size++;
}
auto current_pos = os.tellp();
os.seekp(header_pos);
os.write((char*)&header, sizeof(DirectoryHeader));
os.seekp(current_pos);
}
void Compress(const fs::path& path, ostream& os)
{
if (fs::is_directory(path)) {
EncodeDirectory(path, os);
}
else if (fs::is_regular_file(path)) {
EncodeFile(path, os);
}
else {
error_code ec = make_error_code(huf_errc::invalid_file_type);
throw fs::filesystem_error{ "Compress", path, ec };
}
}
// decoding process-------------------------------------------------------------
void ConvertToToken(std::istream& is, std::ostream& os, const HufNode* tree, int padding_bits, size_t max_len)
{
if (!tree) return;
const HufNode* node = tree;
token_t bits = 0;
while (is.peek() != EOF && max_len--) {
bits = is.get();
for (uint8_t i = 0; i < TOKEN_BITS;) {
if (node->link(bits & RIGHT)) {
node = node->link(bits & RIGHT);
bits >>= 1;
i++;
}
else {
os.put(node->get().token);
node = tree;
if (is.peek() == EOF && (i + padding_bits) >= TOKEN_BITS)
break;
}
}
}
if (!node->link(bits & RIGHT))
os.put(node->get().token);
}
void Decode(istream& is, ostream& os)
{
HufHeader header{};
is.read((char*)&header, sizeof(Header));
if (header.records_size > TOKEN_MAX)
throw out_of_range{ "Invalid file header: Invalid token records size: " + to_string(header.records_size) };
TokenRecord token_records[TOKEN_MAX];
is.read((char*)token_records, sizeof(TokenRecord) * header.records_size);
PODNodeGuard<HufNode> tree{ DecodeTokenRecords(token_records, header.records_size) };
if (!tree && header.records_size)
throw exception{ "Invalid file header: Invalid token records: Huffman tree build faild" };
ConvertToToken(is, os, tree.get(), header.padding_bits, header.data_size);
}
void Decoding(istream& is, ostream& os)
{
Decode(is, os);
}
void DecodeFile(istream& is, const fs::path& file_path)
{
ofstream os{ file_path, ios_base::binary };
if (!os.good()) {
error_code ec = make_error_code(huf_errc::invalid_fstream);
throw fs::filesystem_error{ "DecodeFile", file_path, ec };
}
Decoding(is, os);
}
void DecodeDirectory(istream& is, const fs::path& prefix, size_t num_of_file)
{
fs::create_directory(prefix);
for (size_t i = 0; i < num_of_file; i++)
Decompress(is, prefix);
}
void Decompress(istream& is, const fs::path& prefix)
{
Header header{};
is.read((char*)&header, sizeof(Header));
if (header.name_size >= FILENAME_MAX || !header.name_size)
throw out_of_range{ "Invalid file header: Invalid file name length: " + to_string(header.name_size) };
NameType name[FILENAME_MAX];
is.read((char*)name, sizeof(NameType) * header.name_size);
name[header.name_size] = 0;
switch (header.type) {
case TYPE_REGULAR_FILE:
DecodeFile(is, prefix / name);
break;
case TYPE_DIRECTORY:
DecodeDirectory(is, prefix / name, header.data_size);
break;
}
}
fs::path DecompressRetFilename(istream& is, const fs::path& prefix)
{
Header header{};
is.read((char*)&header, sizeof(Header));
if (header.name_size >= FILENAME_MAX || !header.name_size)
throw out_of_range{ "Invalid file header: Invalid file name size: " + to_string(header.name_size) };
NameType name[FILENAME_MAX];
is.read((char*)name, sizeof(NameType) * header.name_size);
name[header.name_size] = 0;
switch (header.type) {
case TYPE_REGULAR_FILE:
DecodeFile(is, prefix / name);
break;
case TYPE_DIRECTORY:
DecodeDirectory(is, prefix / name, header.data_size);
break;
}
return name;
}
}