-
Notifications
You must be signed in to change notification settings - Fork 0
/
deflate.cpp
206 lines (189 loc) · 5.54 KB
/
deflate.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
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#ifdef __STDC_HOSTED__
#include <algorithm>
#include <bitset>
#include <iterator>
#include <stdexcept>
#include <string>
#include <variant>
#include <vector>
using std::for_each_n, std::advance;
using std::runtime_error;
using std::uint16_t;
using std::variant;
using std::vector;
#endif
using std::byte, std::to_integer;
using std::size_t;
#include "deflate.hpp"
namespace zlite {
struct div_t {
unsigned quot, rem;
};
static div_t div(unsigned x, unsigned y) {
div_t lol;
lol.quot = x / y;
lol.rem = x % y;
return lol;
}
static std::string operator+(std::string &s, unsigned i) {
return s + std::to_string(i);
}
static void Huffman_decode() {}
static unsigned short code_lengths[288];
struct range {
unsigned short end, bit_length;
};
static range range[4]{{143, 8}, {255, 9}, {279, 7}, {287, 8}};
struct Huffman_Node {
Huffman_Node() : symbol{0}, parent{0} {}
Huffman_Node(const Huffman_Node &rhs)
: symbol{rhs.symbol}, parent{rhs.parent} {}
Huffman_Node &operator=(const Huffman_Node &rhs) {
symbol = rhs.symbol;
parent = rhs.parent;
return *this;
}
size_t symbol;
union {
size_t parent;
unsigned depth, freq;
};
};
std::vector<byte> deflate::decompress() {
using zlite::compression_mode;
std::vector<byte> lol;
for (const auto &block : blocks) {
// while (!is_final_block) {
switch (block.mode) {
case compression_mode::None: {
auto len = to_integer<unsigned short>((block.data.at(0) << 8) |
block.data.at(1)),
nlen = to_integer<unsigned short>((block.data.at(2) << 8) |
block.data.at(3));
if (~len != nlen) {
std::string message("Wrong length in an uncompressed block."
"\nExpected len:" +
std::to_string(len) +
", nlen:" + std::to_string(~len) +
"\nGot len:" + std::to_string(len) +
", nlen:" + std::to_string(len) + '\n');
throw runtime_error(message);
}
if (block.data.size() - 4 != len)
throw runtime_error(
"Length of uncompressed block does not equal actual size.");
for (unsigned short i = 4; i < len; i++) {
lol.emplace_back(block.data.at(i));
}
} break;
case compression_mode::With_dynamic_Huffman: {
while (true) {
unsigned short sym;
if (sym == 256)
break;
if (sym < 256) {
lol.emplace_back(byte(sym));
} else {
}
}
} break;
case compression_mode::With_fixed_Huffman:
unsigned short len_codes{257};
break;
default:
throw std::runtime_error("Invalid Deflate compression mode");
}
//}
}
return lol;
}
// NOTE: constructor only allows one set of input data to be compressed only one
// way
deflate::deflate(vector<byte> uncompressed_data, compression_mode mode) {
switch (mode) {
case compression_mode::None: {
div_t lol{div(uncompressed_data.size(), 0xffff)};
for (unsigned i = 0; i < lol.quot; i++) {
deflate_block block;
block.is_final_block = false;
block.mode = mode;
block.data = {byte(0xff), byte(0xff), byte(0), byte(0)};
for (auto j = 0; j < 0xffff; j++) {
block.data.emplace_back(uncompressed_data.at(0xffff * i + j));
}
blocks.emplace_back(block);
}
deflate_block block;
block.is_final_block = true;
block.mode = mode;
uint16_t len = lol.rem, nlen = ~len;
block.data = {byte(lol.rem >> 8), byte(lol.rem | 0xff), byte(nlen >> 8),
byte(nlen | 0xff)};
for (auto &&rbeg{uncompressed_data.rbegin()}, it{rbeg};
it != (rbeg + lol.rem); ++it)
block.data.emplace_back(*it);
blocks.emplace_back(block);
} break;
case compression_mode::With_dynamic_Huffman: {
deflate_block lol;
} break;
case compression_mode::With_fixed_Huffman: {
deflate_block lol;
} break;
default:
throw std::invalid_argument("Invalid compression mode");
}
}
deflate_block &deflate::operator[](const size_t index) {
try {
return blocks.at(index);
} catch (std::out_of_range &e) {
throw;
}
}
const deflate_block &deflate::operator[](const size_t index) const {
try {
return blocks.at(index);
} catch (std::out_of_range &e) {
throw;
}
}
void deflate::push(vector<byte> uncompressed_data, compression_mode mode) {
blocks.end()->is_final_block = false;
deflate_block block;
block.mode = mode;
switch (mode) {
case compression_mode::None: {
div_t lol{div(uncompressed_data.size(), 0xffff)};
for (unsigned i = 0; i < lol.quot; i++) {
deflate_block block;
block.is_final_block = false;
block.mode = mode;
block.data = {byte(0xff), byte(0xff), byte(0), byte(0)};
for (auto j = 0; j < 0xffff; j++) {
block.data.emplace_back(uncompressed_data.at(0xffff * i + j));
}
blocks.emplace_back(block);
}
uint16_t len = lol.rem, nlen = ~len;
block.data = {byte(lol.rem >> 8), byte(lol.rem | 0xff), byte(nlen >> 8),
byte(nlen | 0xff)};
for (auto &&rbeg{uncompressed_data.rbegin()}, it{rbeg};
it != (rbeg + lol.rem); ++it)
block.data.emplace_back(*it);
} break;
case compression_mode::With_dynamic_Huffman:
break;
case compression_mode::With_fixed_Huffman:
break;
default:
break;
}
block.is_final_block = true;
blocks.emplace_back(block);
}
const char *deflate::info() {}
} // namespace zlite