-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdecompress.cpp
355 lines (310 loc) · 9.17 KB
/
decompress.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
#include "codec.h"
/*
Huffman
*/
void HUFFMAN::build_cann_from_length() {
UBMP32 i,j;
int temp;
CANN tempcann;
//sort by length
for(i = 0;i < MAX_LEAFS;i++) {
for(j = i + 1;j < MAX_LEAFS;j++) {
temp = cann[j].length - cann[i].length;
if(temp == 0) {
temp = cann[j].symbol - cann[i].symbol;
temp = -temp;
}
if(temp < 0) {
tempcann = cann[j];
cann[j] = cann[i];
cann[i] = tempcann;
}
}
}
//assign code
UBMP32 code = cann[MAX_LEAFS - 1].code;
UBMP8 length = cann[MAX_LEAFS - 1].length;
for(int k = MAX_LEAFS - 2; k >= 0; k--) {
if (cann[k].length == 0) {
break;
}
if(cann[k].length != length) {
code >>= (length - cann[k].length);
length = cann[k].length;
}
code++;
cann[k].code = code;
}
//sort equal lengths lexically
for(i = 0;i < MAX_LEAFS;i++) {
for(j = i + 1;j < MAX_LEAFS;j++) {
temp = cann[j].length - cann[i].length;
if(temp == 0) {
temp = cann[j].symbol - cann[i].symbol;
}
if(temp < 0) {
tempcann = cann[j];
cann[j] = cann[i];
cann[i] = tempcann;
}
}
}
//mark start of each length
for (i = 0; i < MAX_LEN; i++)
pstart[i] = 0;
min_length = MAX_LEN;
max_length = 0;
length = 0;
for (i = 0; i < MAX_LEAFS; i++) {
if (cann[i].length > length) {
length = cann[i].length;
pstart[length] = &cann[i];
if(length < min_length)
min_length = length;
if(length > max_length)
max_length = length;
}
}
}
/*
decode
*/
template<bool dolz>
int COMP_INFO::_decode(
const UBMP8* in_table,
UBMP8* out_table,
const UBMP32 size
) {
const UBMP8* in = in_table;
const UBMP8* ine = in_table + size;
UBMP8* out = out_table;
UBMP8* ptr;
PAIR pair;
UBMP16 j,extra;
UBMP32 v;
CANN *pcann;
HUFFMAN* phuf;
int diff;
BITSET bs(in,out);
BITSET bso(in,out);
#define HUFFMAN_DECODE(huf,x) { \
phuf = &huf; \
bs.addbits(phuf->max_length); \
for(j = phuf->min_length; j <= phuf->max_length;j++) { \
if(!(pcann = phuf->pstart[j])) \
continue; \
diff = UBMP32(bs.read(j) & pcann->mask) - pcann->code; \
if(diff >= 0) { \
x = (pcann + diff)->symbol; \
bs.trim(j); \
break; \
} \
} \
};
while(in < ine) {
HUFFMAN_DECODE(huffman,v);
if(v == EOB_MARKER)
break;
if(v > EOB_MARKER) {
//length
v -= LENGTH_MARKER;
pair.len = base_length[v];
extra = extra_lbits[v];
if(extra != 0)
pair.len += bs.getbits(extra);
pair.len += MIN_MATCH_LENGTH;
//distance
HUFFMAN_DECODE(huffman_pos,v);
pair.pos = base_dist[v];
extra = extra_dbits[v];
if(extra != 0)
pair.pos += bs.getbits(extra);
//copy bytes
if(dolz) {
bso.writepair(pair);
} else {
ptr = out - pair.pos;
for(int i = 0; i < pair.len;i++)
*out++ = *ptr++;
}
} else {
//write literal
if(dolz)
bso.writeliteral(v);
else
*out++ = (UBMP8)v;
}
if(dolz)
bso.writebits();
}
if(dolz)
bso.flushbits();
return int(out - out_table);
}
/*
decode
*/
int COMP_INFO::decode_huff(
const UBMP8* in_table,
UBMP8* out_table,
const UBMP32 size
) {
return _decode<true>(in_table,out_table,size);
}
/*
decode
*/
int COMP_INFO::decode(
const UBMP8* in_table,
UBMP8* out_table,
const UBMP32 size
) {
return _decode<false>(in_table,out_table,size);
}
/*
decode lz
*/
int COMP_INFO::decode_lz(
const UBMP8* in_table,
UBMP8* out_table,
const UBMP32 size
) {
const UBMP8* in = in_table;
const UBMP8* ine = in_table + size;
UBMP8* out = out_table;
UBMP8* ptr;
BITSET bs(in,out);
PAIR pair;
UBMP32 v;
while(in < ine) {
v = bs.getbits(1);
if(v == 1) {
v = bs.getbits(PAIR_BITS);
pair.len = (v >> DISTANCE_BITS);
pair.pos = (v & (_byte_32 >> (32 - DISTANCE_BITS)));
pair.len += MIN_MATCH_LENGTH;
//copy bytes
ptr = out - pair.pos;
for(int i = 0; i < pair.len;i++)
*out++ = *ptr++;
} else {
v = bs.getbits(LITERAL_BITS);
//literal byte
*out++ = (UBMP8)v;
}
}
return int(out - out_table);
}
/*
open encoded file
*/
bool COMP_INFO::open(FILE* myf,int type) {
UBMP32 i;
pf = myf;
//read counts
fread(&orgsize,4,1,pf);
fread(&cmpsize,4,1,pf);
fread(&n_blocks,4,1,pf);
fread(&block_size,4,1,pf);
#ifdef BIGENDIAN
bswap32(orgsize);
bswap32(cmpsize);
bswap32(n_blocks);
bswap32(block_size);
#endif
//skip reserve bytes
fseek(pf,40,SEEK_CUR);
if(type <= 1) {
//huffman tree
huffman.cann = new CANN[huffman.MAX_LEAFS];
huffman_pos.cann = new CANN[huffman_pos.MAX_LEAFS];
//read length
for(i = 0; i < huffman.MAX_LEAFS; i++) {
fread(&huffman.cann[i].length,1,1,pf);
huffman.cann[i].symbol = i;
huffman.cann[i].code = 0;
huffman.cann[i].mask = (1 << huffman.cann[i].length) - 1;
}
//read length
for(i = 0; i < huffman_pos.MAX_LEAFS; i++) {
fread(&huffman_pos.cann[i].length,1,1,pf);
huffman_pos.cann[i].symbol = i;
huffman_pos.cann[i].code = 0;
huffman_pos.cann[i].mask = (1 << huffman_pos.cann[i].length) - 1;
}
//build cannoncial huffman
huffman.build_cann_from_length();
huffman_pos.build_cann_from_length();
}
//read index table
index_table = new UBMP32[n_blocks + 1];
fread(index_table,4,(n_blocks+1),pf);
#ifdef BIGENDIAN
for(i = 0; i < int(n_blocks + 1);i++)
bswap32(index_table[i]);
#endif
read_start = ftell(pf);
return true;
}
/*
decompress file
*/
#include <time.h>
#ifndef CLOCKS_PER_SEC
#define CLOCKS_PER_SEC CLK_TCK
#endif
void COMP_INFO::decompress(FILE* inf,FILE* outf, int type) {
UBMP8 buffer[2 * BLOCK_SIZE];
UBMP8 buffer2[2 * BLOCK_SIZE];
UBMP32 i,buffer_size;
//open compressed file
open(inf,type);
//time
clock_t start_t, end_t;
double seconds = 0.0,dseconds = 0.0;
start_t = clock();
//header when writing to LZ file
if(type == 1) {
//write counts
fwrite(&orgsize,4,1,outf);
fwrite(&cmpsize,4,1,outf);
fwrite(&n_blocks,4,1,outf);
fwrite(&block_size,4,1,outf);
//skip reserve bytes
fseek(outf,40,SEEK_CUR);
//index table
fwrite(index_table,4,n_blocks + 1,outf);
}
//decompress each block
size = 0;
for(i = 0; i < n_blocks;i++) {
buffer_size = index_table[i + 1] - index_table[i];
fread(&buffer,1,buffer_size,pf);
end_t = clock();
if(type == 0)
buffer_size = decode(buffer,buffer2,buffer_size);
else if(type == 1)
buffer_size = decode_huff(buffer,buffer2,buffer_size);
else
buffer_size = decode_lz(buffer,buffer2,buffer_size);
dseconds += (double)(clock() - end_t) / CLOCKS_PER_SEC;
fwrite(&buffer2,1,buffer_size,outf);
index_table[i] = size;
size += buffer_size;
}
index_table[i] = size;
//rewrite index table
if(type == 1) {
fseek(outf,56,SEEK_SET);
fwrite(index_table,4,n_blocks + 1,outf);
}
//time
end_t = clock();
seconds = (double)(end_t - start_t) / CLOCKS_PER_SEC;
printf ("%g sec total, %g sec decompressing, %d Mb file, %g Mb/sec, %g Mb/sec\n",
seconds,dseconds, size/(1024*1024),
(size/(1024*1024))/seconds, (size/(1024*1024))/dseconds);
}
/*
end
*/