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
|
#include "zlib.h"
#include <bitset>
#include <cstdint>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <stdexcept>
#define MAXBITS 15
using std::cout, std::endl;
void ZLibInflator::calculateCodes(uint8_t* lengths, uint16_t* codesOut, int codeCount)
{
const int biggestLen = 15;
uint16_t lenCounts[biggestLen + 1];
memset(lenCounts, 0, (biggestLen + 1)*sizeof(uint16_t));
for(int i = 0; i < codeCount; i++)
lenCounts[lengths[i]]++;
lenCounts[0] = 0;
uint16_t nextCodes[biggestLen + 1];
uint16_t code = 0;
for(int bits = 1; bits < biggestLen + 1; bits++)
{
code = (code + lenCounts[bits - 1]) << 1;
nextCodes[bits] = code;
}
for(int i = 0; i < codeCount; i++)
{
uint8_t len = lengths[i];
if(len == 0)
continue;
codesOut[i] = nextCodes[len]++;
}
}
void ZLibInflator::buildHuffmanTree(uint8_t* lengths, uint16_t* codes, int codeCount)
{
buildHuffmanTree(lengths, codes, codeCount, &tree);
}
void ZLibInflator::buildHuffmanTree(uint8_t* lengths, uint16_t* codes, int codeCount, HuffmanTree* tree)
{
for(uint16_t i = 0; i < codeCount; i++)
{
uint16_t code = codes[i];
uint8_t len = lengths[i];
if(len == 0)
continue;
HuffmanTree* current = tree;
for(int j = 0; j < len; j++)
{
bool right = code & (0b1 << (len - 1 - j));
if(right)
{
if(current->right != nullptr)
current = current->right;
else
{
current->right = new HuffmanTree();
current = current->right;
}
}
else
{
if(current->left != nullptr)
current = current->left;
else
{
current->left = new HuffmanTree();
current = current->left;
}
}
}
current->val = i;
}
}
void ZLibInflator::buildStaticHuffmanTree()
{
if(staticTree)
return;
cout << "Building static tree" << endl;
buildStaticHuffmanTree(&tree, &distTree);
staticTree = true;
haveTree = true;
}
void ZLibInflator::buildStaticHuffmanTree(HuffmanTree* treeOut, HuffmanTree* distTreeOut)
{
const int codeCount = 288;
uint8_t lens[codeCount];
uint16_t codes[codeCount];
for(int i = 0; i < codeCount; i++)
{
if(i < 144)
lens[i] = 8;
else if(i < 256)
lens[i] = 9;
else if(i < 280)
lens[i] = 7;
else if(i < 288)
lens[i] = 8;
}
calculateCodes(lens, codes, codeCount);
buildHuffmanTree(lens, codes, codeCount, treeOut);
uint8_t nDistCodes = 32;
uint8_t distCodeLens[nDistCodes];
uint16_t distCodes[nDistCodes];
for(int i = 0; i < nDistCodes; i++)
distCodeLens[i] = i;
calculateCodes(distCodeLens, distCodes, nDistCodes);
buildHuffmanTree(distCodeLens, distCodes, nDistCodes, distTreeOut);
}
void ZLibInflator::buildDynamicHuffmanTree(StreamData* stream)
{
if(haveTree)
{
tree.free();
distTree.free();
}
buildDynamicHuffmanTree(stream, &tree, &distTree);
haveTree = true;
}
void ZLibInflator::buildDynamicHuffmanTree(StreamData* stream, HuffmanTree* treeOut, HuffmanTree* distTreeOut)
{
unsigned int nLitCodes = nextBits(stream, 5) + 257;
uint16_t litCodes[nLitCodes];
unsigned int nDistCodes = nextBits(stream, 5) + 1;
uint16_t distCodes[nDistCodes];
uint8_t codeLens[nLitCodes + nDistCodes];
uint8_t nLenCodes = nextBits(stream, 4) + 4;
uint8_t lenCodeLens[19];
memset(lenCodeLens, 0, sizeof(uint8_t)*19);
uint16_t lenCodes[19];
const static uint8_t lenCodeOrder[] = { 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 };
for(int i = 0; i < nLenCodes; i++)
lenCodeLens[lenCodeOrder[i]] = nextBits(stream, 3);
calculateCodes(lenCodeLens, lenCodes, 19);
HuffmanTree lenCodeTree;
buildHuffmanTree(lenCodeLens, lenCodes, 19, &lenCodeTree);
int i = 0;
uint8_t extraBits[] = {2, 3, 7};
uint8_t repStarts[] = {3, 3, 11};
while(i < nLitCodes + nDistCodes)
{
uint16_t code = getNextCode(stream, &lenCodeTree);
if(code < 16)
codeLens[i++] = (uint8_t)code;
else if(code < 19)
{
code -= 16;
int reps = repStarts[code] + nextBits(stream, extraBits[code]);
uint8_t len = 0;
if(code == 0)
{
if(i == 0)
cout << "Trying to repeat non existent value in dynamic huffman code creation" << endl;
else
len = codeLens[i - 1];
}
if(i + reps > nLitCodes + nDistCodes)
cout << "other big errror oh no " << i << " " << 0+reps << endl;
for(int j = 0; j < reps; j++)
{
codeLens[i++] = len;
}
}
else
{
cout << "big error oh no" << endl;
}
}
calculateCodes(codeLens, litCodes, nLitCodes);
buildHuffmanTree(codeLens, litCodes, nLitCodes, treeOut);
calculateCodes(&codeLens[nLitCodes], distCodes, nDistCodes);
buildHuffmanTree(&codeLens[nLitCodes], distCodes, nDistCodes, distTreeOut);
}
void HuffmanTree::free()
{
if(left != nullptr)
{
delete left;
left = nullptr;
}
if(right != nullptr)
{
delete right;
right = nullptr;
}
val = 0xFFFF;
}
HuffmanTree::~HuffmanTree()
{
free();
}
uint16_t ZLibInflator::getNextCode(StreamData* stream)
{
return getNextCode(stream, &tree);
}
uint16_t ZLibInflator::getNextCode(StreamData* stream, HuffmanTree* tree)
{
while(tree->val == 0xFFFF)
{
bool right = nextBit(stream);
if(tree->left == nullptr && !right)
cout << "bad left" << endl;
if(tree->right == nullptr && right)
cout << "bad right" << endl;
tree = (right)?tree->right:tree->left;
}
return tree->val;
}
bool ZLibInflator::nextBit(StreamData* stream)
{
long bit = stream->pos % 8;
long byte = stream->pos / 8;
if(byte >= stream->length)
{
cout << byte << " " << stream->length << endl;
throw std::out_of_range("Ran out of compressed data");
}
stream->pos++;
//cout << ((stream->data[byte] & (0b1 << bit))?"1":"0");
return (stream->data[byte] & (0b1 << bit))?0b1:0b0;
}
uint16_t ZLibInflator::nextBits(StreamData* stream, int bits)
{
if(bits > 16)
cout << "Too many bits(" << bits << ")!!!" << endl;
uint16_t out = 0;
for(int i = 0; i < bits; i++)
{
out |= nextBit(stream) << i;
}
return out;
}
int ZLibInflator::decodeData(uint8_t* data, unsigned long length, uint8_t* out, unsigned long outLength)
{
staticTree = false;
const unsigned int lenStart[] = { /* Size base for length codes 257..285 */
3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258};
const unsigned int lenExtra[] = { /* Extra bits for length codes 257..285 */
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0};
const unsigned int distStart[] = { /* Offset base for distance codes 0..29 */
1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
8193, 12289, 16385, 24577};
const unsigned int distExtra[] = { /* Extra bits for distance codes 0..29 */
0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
12, 12, 13, 13};
StreamData stream = { data, length, 0};
long outPos = 0;
bool final;
do
{
final = nextBit(&stream);
int method = nextBits(&stream, 2);
//cout << (final?"Final chunk!\n":"") << "Compression method: " << method << endl;
//cout << outPos << " " << outLength << endl;
if(method == 1)
buildStaticHuffmanTree();
else if(method == 2)
buildDynamicHuffmanTree(&stream);
else if(method == 0)
{
while(stream.pos%8 != 0)
stream.pos++;
uint16_t LEN = nextBits(&stream, 16);
uint16_t NLEN = nextBits(&stream, 16);
NLEN++;
if(LEN + NLEN != 0)
throw std::invalid_argument("NLEN and LEN don't match");
for(int i = 0; i < LEN; i++)
out[outPos++] = nextBits(&stream, 8);
continue;
}
else
{
cout << "Reserved???" << endl;
return -1;
}
uint16_t code;
do
{
code = getNextCode(&stream);
if(outPos > outLength && code != 256)
{
throw std::out_of_range("No more space left in image (normal)");
}
if(code < 256)
out[outPos++] = (uint8_t)code;
else if(code > 256)
{
unsigned int len = lenStart[code-257] + (int)nextBits(&stream, lenExtra[code-257]);
unsigned int distCode = getNextCode(&stream, &distTree);
unsigned int dist = distStart[distCode] + (int)nextBits(&stream, distExtra[distCode]);
if(outPos + len > outLength)
{
throw std::out_of_range("No more space left in image (RLE error)");
}
for(int i = 0; i < len; i++)
{
out[outPos] = out[outPos - dist];
outPos++;
}
}
}
while(code != 256);
}
while(!final);
return 0;
}
|