line |
stmt |
bran |
cond |
sub |
time |
code |
1
|
|
|
|
|
|
|
2
|
|
|
|
|
|
/*-------------------------------------------------------------*/ |
3
|
|
|
|
|
|
/*--- Decompression machinery ---*/ |
4
|
|
|
|
|
|
/*--- decompress.c ---*/ |
5
|
|
|
|
|
|
/*-------------------------------------------------------------*/ |
6
|
|
|
|
|
|
|
7
|
|
|
|
|
|
/* ------------------------------------------------------------------ |
8
|
|
|
|
|
|
This file is part of bzip2/libbzip2, a program and library for |
9
|
|
|
|
|
|
lossless, block-sorting data compression. |
10
|
|
|
|
|
|
|
11
|
|
|
|
|
|
bzip2/libbzip2 version 1.0.6 of 6 September 2010 |
12
|
|
|
|
|
|
Copyright (C) 1996-2010 Julian Seward |
13
|
|
|
|
|
|
|
14
|
|
|
|
|
|
Please read the WARNING, DISCLAIMER and PATENTS sections in the |
15
|
|
|
|
|
|
README file. |
16
|
|
|
|
|
|
|
17
|
|
|
|
|
|
This program is released under the terms of the license contained |
18
|
|
|
|
|
|
in the file LICENSE. |
19
|
|
|
|
|
|
------------------------------------------------------------------ */ |
20
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
22
|
|
|
|
|
|
#include "bzlib_private.h" |
23
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
25
|
|
|
|
|
|
/*---------------------------------------------------*/ |
26
|
|
|
|
|
|
static |
27
|
|
|
|
|
|
void makeMaps_d ( DState* s ) |
28
|
|
|
|
|
|
{ |
29
|
|
|
|
|
|
Int32 i; |
30
|
3964
|
|
|
|
|
s->nInUse = 0; |
31
|
1014784
|
|
|
|
|
for (i = 0; i < 256; i++) |
32
|
1014784
|
|
|
|
|
if (s->inUse[i]) { |
33
|
70030
|
|
|
|
|
s->seqToUnseq[s->nInUse] = i; |
34
|
70030
|
|
|
|
|
s->nInUse++; |
35
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
39
|
|
|
|
|
|
/*---------------------------------------------------*/ |
40
|
|
|
|
|
|
#define RETURN(rrr) \ |
41
|
|
|
|
|
|
{ retVal = rrr; goto save_state_and_return; }; |
42
|
|
|
|
|
|
|
43
|
|
|
|
|
|
#define GET_BITS(lll,vvv,nnn) \ |
44
|
|
|
|
|
|
case lll: s->state = lll; \ |
45
|
|
|
|
|
|
while (True) { \ |
46
|
|
|
|
|
|
if (s->bsLive >= nnn) { \ |
47
|
|
|
|
|
|
UInt32 v; \ |
48
|
|
|
|
|
|
v = (s->bsBuff >> \ |
49
|
|
|
|
|
|
(s->bsLive-nnn)) & ((1 << nnn)-1); \ |
50
|
|
|
|
|
|
s->bsLive -= nnn; \ |
51
|
|
|
|
|
|
vvv = v; \ |
52
|
|
|
|
|
|
break; \ |
53
|
|
|
|
|
|
} \ |
54
|
|
|
|
|
|
if (s->strm->avail_in == 0) RETURN(BZ_OK); \ |
55
|
|
|
|
|
|
s->bsBuff \ |
56
|
|
|
|
|
|
= (s->bsBuff << 8) | \ |
57
|
|
|
|
|
|
((UInt32) \ |
58
|
|
|
|
|
|
(*((UChar*)(s->strm->next_in)))); \ |
59
|
|
|
|
|
|
s->bsLive += 8; \ |
60
|
|
|
|
|
|
s->strm->next_in++; \ |
61
|
|
|
|
|
|
s->strm->avail_in--; \ |
62
|
|
|
|
|
|
s->strm->total_in_lo32++; \ |
63
|
|
|
|
|
|
if (s->strm->total_in_lo32 == 0) \ |
64
|
|
|
|
|
|
s->strm->total_in_hi32++; \ |
65
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
67
|
|
|
|
|
|
#define GET_UCHAR(lll,uuu) \ |
68
|
|
|
|
|
|
GET_BITS(lll,uuu,8) |
69
|
|
|
|
|
|
|
70
|
|
|
|
|
|
#define GET_BIT(lll,uuu) \ |
71
|
|
|
|
|
|
GET_BITS(lll,uuu,1) |
72
|
|
|
|
|
|
|
73
|
|
|
|
|
|
/*---------------------------------------------------*/ |
74
|
|
|
|
|
|
#define GET_MTF_VAL(label1,label2,lval) \ |
75
|
|
|
|
|
|
{ \ |
76
|
|
|
|
|
|
if (groupPos == 0) { \ |
77
|
|
|
|
|
|
groupNo++; \ |
78
|
|
|
|
|
|
if (groupNo >= nSelectors) \ |
79
|
|
|
|
|
|
RETURN(BZ_DATA_ERROR); \ |
80
|
|
|
|
|
|
groupPos = BZ_G_SIZE; \ |
81
|
|
|
|
|
|
gSel = s->selector[groupNo]; \ |
82
|
|
|
|
|
|
gMinlen = s->minLens[gSel]; \ |
83
|
|
|
|
|
|
gLimit = &(s->limit[gSel][0]); \ |
84
|
|
|
|
|
|
gPerm = &(s->perm[gSel][0]); \ |
85
|
|
|
|
|
|
gBase = &(s->base[gSel][0]); \ |
86
|
|
|
|
|
|
} \ |
87
|
|
|
|
|
|
groupPos--; \ |
88
|
|
|
|
|
|
zn = gMinlen; \ |
89
|
|
|
|
|
|
GET_BITS(label1, zvec, zn); \ |
90
|
|
|
|
|
|
while (1) { \ |
91
|
|
|
|
|
|
if (zn > 20 /* the longest code */) \ |
92
|
|
|
|
|
|
RETURN(BZ_DATA_ERROR); \ |
93
|
|
|
|
|
|
if (zvec <= gLimit[zn]) break; \ |
94
|
|
|
|
|
|
zn++; \ |
95
|
|
|
|
|
|
GET_BIT(label2, zj); \ |
96
|
|
|
|
|
|
zvec = (zvec << 1) | zj; \ |
97
|
|
|
|
|
|
}; \ |
98
|
|
|
|
|
|
if (zvec - gBase[zn] < 0 \ |
99
|
|
|
|
|
|
|| zvec - gBase[zn] >= BZ_MAX_ALPHA_SIZE) \ |
100
|
|
|
|
|
|
RETURN(BZ_DATA_ERROR); \ |
101
|
|
|
|
|
|
lval = gPerm[zvec - gBase[zn]]; \ |
102
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
105
|
|
|
|
|
|
/*---------------------------------------------------*/ |
106
|
167312
|
|
|
|
|
Int32 BZ2_decompress ( DState* s ) |
107
|
|
|
|
|
|
{ |
108
|
|
|
|
|
|
UChar uc; |
109
|
|
|
|
|
|
Int32 retVal; |
110
|
|
|
|
|
|
Int32 minLen, maxLen; |
111
|
167312
|
|
|
|
|
bz_stream* strm = s->strm; |
112
|
|
|
|
|
|
|
113
|
|
|
|
|
|
/* stuff that needs to be saved/restored */ |
114
|
|
|
|
|
|
Int32 i; |
115
|
|
|
|
|
|
Int32 j; |
116
|
|
|
|
|
|
Int32 t; |
117
|
|
|
|
|
|
Int32 alphaSize; |
118
|
|
|
|
|
|
Int32 nGroups; |
119
|
|
|
|
|
|
Int32 nSelectors; |
120
|
|
|
|
|
|
Int32 EOB; |
121
|
|
|
|
|
|
Int32 groupNo; |
122
|
|
|
|
|
|
Int32 groupPos; |
123
|
|
|
|
|
|
Int32 nextSym; |
124
|
|
|
|
|
|
Int32 nblockMAX; |
125
|
|
|
|
|
|
Int32 nblock; |
126
|
|
|
|
|
|
Int32 es; |
127
|
|
|
|
|
|
Int32 N; |
128
|
|
|
|
|
|
Int32 curr; |
129
|
|
|
|
|
|
Int32 zt; |
130
|
|
|
|
|
|
Int32 zn; |
131
|
|
|
|
|
|
Int32 zvec; |
132
|
|
|
|
|
|
Int32 zj; |
133
|
|
|
|
|
|
Int32 gSel; |
134
|
|
|
|
|
|
Int32 gMinlen; |
135
|
|
|
|
|
|
Int32* gLimit; |
136
|
|
|
|
|
|
Int32* gBase; |
137
|
|
|
|
|
|
Int32* gPerm; |
138
|
|
|
|
|
|
|
139
|
167312
|
|
|
|
|
if (s->state == BZ_X_MAGIC_1) { |
140
|
|
|
|
|
|
/*initialise the save area*/ |
141
|
4828
|
|
|
|
|
s->save_i = 0; |
142
|
4828
|
|
|
|
|
s->save_j = 0; |
143
|
4828
|
|
|
|
|
s->save_t = 0; |
144
|
4828
|
|
|
|
|
s->save_alphaSize = 0; |
145
|
4828
|
|
|
|
|
s->save_nGroups = 0; |
146
|
4828
|
|
|
|
|
s->save_nSelectors = 0; |
147
|
4828
|
|
|
|
|
s->save_EOB = 0; |
148
|
4828
|
|
|
|
|
s->save_groupNo = 0; |
149
|
4828
|
|
|
|
|
s->save_groupPos = 0; |
150
|
4828
|
|
|
|
|
s->save_nextSym = 0; |
151
|
4828
|
|
|
|
|
s->save_nblockMAX = 0; |
152
|
4828
|
|
|
|
|
s->save_nblock = 0; |
153
|
4828
|
|
|
|
|
s->save_es = 0; |
154
|
4828
|
|
|
|
|
s->save_N = 0; |
155
|
4828
|
|
|
|
|
s->save_curr = 0; |
156
|
4828
|
|
|
|
|
s->save_zt = 0; |
157
|
4828
|
|
|
|
|
s->save_zn = 0; |
158
|
4828
|
|
|
|
|
s->save_zvec = 0; |
159
|
4828
|
|
|
|
|
s->save_zj = 0; |
160
|
4828
|
|
|
|
|
s->save_gSel = 0; |
161
|
4828
|
|
|
|
|
s->save_gMinlen = 0; |
162
|
4828
|
|
|
|
|
s->save_gLimit = NULL; |
163
|
4828
|
|
|
|
|
s->save_gBase = NULL; |
164
|
4828
|
|
|
|
|
s->save_gPerm = NULL; |
165
|
|
|
|
|
|
} |
166
|
|
|
|
|
|
|
167
|
|
|
|
|
|
/*restore from the save area*/ |
168
|
167312
|
|
|
|
|
i = s->save_i; |
169
|
167312
|
|
|
|
|
j = s->save_j; |
170
|
167312
|
|
|
|
|
t = s->save_t; |
171
|
167312
|
|
|
|
|
alphaSize = s->save_alphaSize; |
172
|
167312
|
|
|
|
|
nGroups = s->save_nGroups; |
173
|
167312
|
|
|
|
|
nSelectors = s->save_nSelectors; |
174
|
167312
|
|
|
|
|
EOB = s->save_EOB; |
175
|
167312
|
|
|
|
|
groupNo = s->save_groupNo; |
176
|
167312
|
|
|
|
|
groupPos = s->save_groupPos; |
177
|
167312
|
|
|
|
|
nextSym = s->save_nextSym; |
178
|
167312
|
|
|
|
|
nblockMAX = s->save_nblockMAX; |
179
|
167312
|
|
|
|
|
nblock = s->save_nblock; |
180
|
167312
|
|
|
|
|
es = s->save_es; |
181
|
167312
|
|
|
|
|
N = s->save_N; |
182
|
167312
|
|
|
|
|
curr = s->save_curr; |
183
|
167312
|
|
|
|
|
zt = s->save_zt; |
184
|
167312
|
|
|
|
|
zn = s->save_zn; |
185
|
167312
|
|
|
|
|
zvec = s->save_zvec; |
186
|
167312
|
|
|
|
|
zj = s->save_zj; |
187
|
167312
|
|
|
|
|
gSel = s->save_gSel; |
188
|
167312
|
|
|
|
|
gMinlen = s->save_gMinlen; |
189
|
167312
|
|
|
|
|
gLimit = s->save_gLimit; |
190
|
167312
|
|
|
|
|
gBase = s->save_gBase; |
191
|
167312
|
|
|
|
|
gPerm = s->save_gPerm; |
192
|
|
|
|
|
|
|
193
|
|
|
|
|
|
retVal = BZ_OK; |
194
|
|
|
|
|
|
|
195
|
167312
|
|
|
|
|
switch (s->state) { |
196
|
|
|
|
|
|
|
197
|
9656
|
|
|
|
|
GET_UCHAR(BZ_X_MAGIC_1, uc); |
198
|
4828
|
|
|
|
|
if (uc != BZ_HDR_B) RETURN(BZ_DATA_ERROR_MAGIC); |
199
|
|
|
|
|
|
|
200
|
10424
|
|
|
|
|
GET_UCHAR(BZ_X_MAGIC_2, uc); |
201
|
4828
|
|
|
|
|
if (uc != BZ_HDR_Z) RETURN(BZ_DATA_ERROR_MAGIC); |
202
|
|
|
|
|
|
|
203
|
11160
|
|
|
|
|
GET_UCHAR(BZ_X_MAGIC_3, uc) |
204
|
4828
|
|
|
|
|
if (uc != BZ_HDR_h) RETURN(BZ_DATA_ERROR_MAGIC); |
205
|
|
|
|
|
|
|
206
|
10424
|
|
|
|
|
GET_BITS(BZ_X_MAGIC_4, s->blockSize100k, 8) |
207
|
4828
|
|
|
|
|
if (s->blockSize100k < (BZ_HDR_0 + 1) || |
208
|
|
|
|
|
|
s->blockSize100k > (BZ_HDR_0 + 9)) RETURN(BZ_DATA_ERROR_MAGIC); |
209
|
4828
|
|
|
|
|
s->blockSize100k -= BZ_HDR_0; |
210
|
|
|
|
|
|
|
211
|
4828
|
|
|
|
|
if (s->smallDecompress) { |
212
|
2
|
|
|
|
|
s->ll16 = (UInt16*) BZALLOC( s->blockSize100k * 100000 * sizeof(UInt16) ); |
213
|
2
|
|
|
|
|
s->ll4 = (UChar*) BZALLOC( |
214
|
|
|
|
|
|
((1 + s->blockSize100k * 100000) >> 1) * sizeof(UChar) |
215
|
|
|
|
|
|
); |
216
|
2
|
|
|
|
|
if (s->ll16 == NULL || s->ll4 == NULL) RETURN(BZ_MEM_ERROR); |
217
|
|
|
|
|
|
} else { |
218
|
4826
|
|
|
|
|
s->tt = (UInt32*) BZALLOC( s->blockSize100k * 100000 * sizeof(Int32) ); |
219
|
4826
|
|
|
|
|
if (s->tt == NULL) RETURN(BZ_MEM_ERROR); |
220
|
|
|
|
|
|
} |
221
|
|
|
|
|
|
|
222
|
18112
|
|
|
|
|
GET_UCHAR(BZ_X_BLKHDR_1, uc); |
223
|
|
|
|
|
|
|
224
|
7892
|
|
|
|
|
if (uc == 0x17) goto endhdr_2; |
225
|
4376
|
|
|
|
|
if (uc != 0x31) RETURN(BZ_DATA_ERROR); |
226
|
9440
|
|
|
|
|
GET_UCHAR(BZ_X_BLKHDR_2, uc); |
227
|
4320
|
|
|
|
|
if (uc != 0x41) RETURN(BZ_DATA_ERROR); |
228
|
10136
|
|
|
|
|
GET_UCHAR(BZ_X_BLKHDR_3, uc); |
229
|
4304
|
|
|
|
|
if (uc != 0x59) RETURN(BZ_DATA_ERROR); |
230
|
9376
|
|
|
|
|
GET_UCHAR(BZ_X_BLKHDR_4, uc); |
231
|
4288
|
|
|
|
|
if (uc != 0x26) RETURN(BZ_DATA_ERROR); |
232
|
10072
|
|
|
|
|
GET_UCHAR(BZ_X_BLKHDR_5, uc); |
233
|
4268
|
|
|
|
|
if (uc != 0x53) RETURN(BZ_DATA_ERROR); |
234
|
9304
|
|
|
|
|
GET_UCHAR(BZ_X_BLKHDR_6, uc); |
235
|
4252
|
|
|
|
|
if (uc != 0x59) RETURN(BZ_DATA_ERROR); |
236
|
|
|
|
|
|
|
237
|
4252
|
|
|
|
|
s->currBlockNo++; |
238
|
|
|
|
|
|
if (s->verbosity >= 2) |
239
|
|
|
|
|
|
VPrintf1 ( "\n [%d: huff+mtf ", s->currBlockNo ); |
240
|
|
|
|
|
|
|
241
|
4252
|
|
|
|
|
s->storedBlockCRC = 0; |
242
|
12592
|
|
|
|
|
GET_UCHAR(BZ_X_BCRC_1, uc); |
243
|
4236
|
|
|
|
|
s->storedBlockCRC = (s->storedBlockCRC << 8) | ((UInt32)uc); |
244
|
9240
|
|
|
|
|
GET_UCHAR(BZ_X_BCRC_2, uc); |
245
|
4220
|
|
|
|
|
s->storedBlockCRC = (s->storedBlockCRC << 8) | ((UInt32)uc); |
246
|
9936
|
|
|
|
|
GET_UCHAR(BZ_X_BCRC_3, uc); |
247
|
4204
|
|
|
|
|
s->storedBlockCRC = (s->storedBlockCRC << 8) | ((UInt32)uc); |
248
|
9904
|
|
|
|
|
GET_UCHAR(BZ_X_BCRC_4, uc); |
249
|
4188
|
|
|
|
|
s->storedBlockCRC = (s->storedBlockCRC << 8) | ((UInt32)uc); |
250
|
|
|
|
|
|
|
251
|
9872
|
|
|
|
|
GET_BITS(BZ_X_RANDBIT, s->blockRandomised, 1); |
252
|
|
|
|
|
|
|
253
|
4172
|
|
|
|
|
s->origPtr = 0; |
254
|
9112
|
|
|
|
|
GET_UCHAR(BZ_X_ORIGPTR_1, uc); |
255
|
4156
|
|
|
|
|
s->origPtr = (s->origPtr << 8) | ((Int32)uc); |
256
|
9808
|
|
|
|
|
GET_UCHAR(BZ_X_ORIGPTR_2, uc); |
257
|
4140
|
|
|
|
|
s->origPtr = (s->origPtr << 8) | ((Int32)uc); |
258
|
9048
|
|
|
|
|
GET_UCHAR(BZ_X_ORIGPTR_3, uc); |
259
|
4124
|
|
|
|
|
s->origPtr = (s->origPtr << 8) | ((Int32)uc); |
260
|
|
|
|
|
|
|
261
|
4124
|
|
|
|
|
if (s->origPtr < 0) |
262
|
|
|
|
|
|
RETURN(BZ_DATA_ERROR); |
263
|
4124
|
|
|
|
|
if (s->origPtr > 10 + 100000*s->blockSize100k) |
264
|
|
|
|
|
|
RETURN(BZ_DATA_ERROR); |
265
|
|
|
|
|
|
|
266
|
|
|
|
|
|
/*--- Receive the mapping table ---*/ |
267
|
65824
|
|
|
|
|
for (i = 0; i < 16; i++) { |
268
|
76352
|
|
|
|
|
GET_BIT(BZ_X_MAPPING_1, uc); |
269
|
65824
|
|
|
|
|
if (uc == 1) |
270
|
16646
|
|
|
|
|
s->inUse16[i] = True; else |
271
|
49178
|
|
|
|
|
s->inUse16[i] = False; |
272
|
|
|
|
|
|
} |
273
|
|
|
|
|
|
|
274
|
1047552
|
|
|
|
|
for (i = 0; i < 256; i++) s->inUse[i] = False; |
275
|
|
|
|
|
|
|
276
|
63904
|
|
|
|
|
for (i = 0; i < 16; i++) |
277
|
64032
|
|
|
|
|
if (s->inUse16[i]) |
278
|
260832
|
|
|
|
|
for (j = 0; j < 16; j++) { |
279
|
305772
|
|
|
|
|
GET_BIT(BZ_X_MAPPING_2, uc); |
280
|
331838
|
|
|
|
|
if (uc == 1) s->inUse[i * 16 + j] = True; |
281
|
|
|
|
|
|
} |
282
|
|
|
|
|
|
makeMaps_d ( s ); |
283
|
3964
|
|
|
|
|
if (s->nInUse == 0) RETURN(BZ_DATA_ERROR); |
284
|
3964
|
|
|
|
|
alphaSize = s->nInUse+2; |
285
|
|
|
|
|
|
|
286
|
|
|
|
|
|
/*--- Now the selectors ---*/ |
287
|
3964
|
|
|
|
|
GET_BITS(BZ_X_SELECTOR_1, nGroups, 3); |
288
|
3964
|
|
|
|
|
if (nGroups < 2 || nGroups > 6) RETURN(BZ_DATA_ERROR); |
289
|
14128
|
|
|
|
|
GET_BITS(BZ_X_SELECTOR_2, nSelectors, 15); |
290
|
3928
|
|
|
|
|
if (nSelectors < 1) RETURN(BZ_DATA_ERROR); |
291
|
14730
|
|
|
|
|
for (i = 0; i < nSelectors; i++) { |
292
|
|
|
|
|
|
j = 0; |
293
|
|
|
|
|
|
while (True) { |
294
|
39542
|
|
|
|
|
GET_BIT(BZ_X_SELECTOR_3, uc); |
295
|
34712
|
|
|
|
|
if (uc == 0) break; |
296
|
19982
|
|
|
|
|
j++; |
297
|
19982
|
|
|
|
|
if (j >= nGroups) RETURN(BZ_DATA_ERROR); |
298
|
|
|
|
|
|
} |
299
|
14730
|
|
|
|
|
s->selectorMtf[i] = j; |
300
|
|
|
|
|
|
} |
301
|
|
|
|
|
|
|
302
|
|
|
|
|
|
/*--- Undo the MTF values for the selectors. ---*/ |
303
|
|
|
|
|
|
{ |
304
|
|
|
|
|
|
UChar pos[BZ_N_GROUPS], tmp, v; |
305
|
8034
|
|
|
|
|
for (v = 0; v < nGroups; v++) pos[v] = v; |
306
|
|
|
|
|
|
|
307
|
14730
|
|
|
|
|
for (i = 0; i < nSelectors; i++) { |
308
|
14730
|
|
|
|
|
v = s->selectorMtf[i]; |
309
|
14730
|
|
|
|
|
tmp = pos[v]; |
310
|
14730
|
|
|
|
|
while (v > 0) { pos[v] = pos[v-1]; v--; } |
311
|
14730
|
|
|
|
|
pos[0] = tmp; |
312
|
14730
|
|
|
|
|
s->selector[i] = tmp; |
313
|
|
|
|
|
|
} |
314
|
|
|
|
|
|
} |
315
|
|
|
|
|
|
|
316
|
|
|
|
|
|
/*--- Now the coding tables ---*/ |
317
|
7618
|
|
|
|
|
for (t = 0; t < nGroups; t++) { |
318
|
19004
|
|
|
|
|
GET_BITS(BZ_X_CODING_1, curr, 5); |
319
|
176450
|
|
|
|
|
for (i = 0; i < alphaSize; i++) { |
320
|
|
|
|
|
|
while (True) { |
321
|
290942
|
|
|
|
|
if (curr < 1 || curr > 20) RETURN(BZ_DATA_ERROR); |
322
|
346724
|
|
|
|
|
GET_BIT(BZ_X_CODING_2, uc); |
323
|
290782
|
|
|
|
|
if (uc == 0) break; |
324
|
134642
|
|
|
|
|
GET_BIT(BZ_X_CODING_3, uc); |
325
|
187242
|
|
|
|
|
if (uc == 0) curr++; else curr--; |
326
|
|
|
|
|
|
} |
327
|
168608
|
|
|
|
|
s->len[t][i] = curr; |
328
|
|
|
|
|
|
} |
329
|
|
|
|
|
|
} |
330
|
|
|
|
|
|
|
331
|
|
|
|
|
|
/*--- Create the Huffman decoding tables ---*/ |
332
|
7522
|
|
|
|
|
for (t = 0; t < nGroups; t++) { |
333
|
|
|
|
|
|
minLen = 32; |
334
|
|
|
|
|
|
maxLen = 0; |
335
|
164512
|
|
|
|
|
for (i = 0; i < alphaSize; i++) { |
336
|
164512
|
|
|
|
|
if (s->len[t][i] > maxLen) maxLen = s->len[t][i]; |
337
|
164512
|
|
|
|
|
if (s->len[t][i] < minLen) minLen = s->len[t][i]; |
338
|
|
|
|
|
|
} |
339
|
7522
|
|
|
|
|
BZ2_hbCreateDecodeTables ( |
340
|
|
|
|
|
|
&(s->limit[t][0]), |
341
|
|
|
|
|
|
&(s->base[t][0]), |
342
|
|
|
|
|
|
&(s->perm[t][0]), |
343
|
|
|
|
|
|
&(s->len[t][0]), |
344
|
|
|
|
|
|
minLen, maxLen, alphaSize |
345
|
|
|
|
|
|
); |
346
|
7522
|
|
|
|
|
s->minLens[t] = minLen; |
347
|
|
|
|
|
|
} |
348
|
|
|
|
|
|
|
349
|
|
|
|
|
|
/*--- Now the MTF values ---*/ |
350
|
|
|
|
|
|
|
351
|
3672
|
|
|
|
|
EOB = s->nInUse+1; |
352
|
3672
|
|
|
|
|
nblockMAX = 100000 * s->blockSize100k; |
353
|
|
|
|
|
|
groupNo = -1; |
354
|
|
|
|
|
|
groupPos = 0; |
355
|
|
|
|
|
|
|
356
|
3672
|
|
|
|
|
for (i = 0; i <= 255; i++) s->unzftab[i] = 0; |
357
|
|
|
|
|
|
|
358
|
|
|
|
|
|
/*-- MTF init --*/ |
359
|
|
|
|
|
|
{ |
360
|
|
|
|
|
|
Int32 ii, jj, kk; |
361
|
|
|
|
|
|
kk = MTFA_SIZE-1; |
362
|
58752
|
|
|
|
|
for (ii = 256 / MTFL_SIZE - 1; ii >= 0; ii--) { |
363
|
940032
|
|
|
|
|
for (jj = MTFL_SIZE-1; jj >= 0; jj--) { |
364
|
940032
|
|
|
|
|
s->mtfa[kk] = (UChar)(ii * MTFL_SIZE + jj); |
365
|
940032
|
|
|
|
|
kk--; |
366
|
|
|
|
|
|
} |
367
|
58752
|
|
|
|
|
s->mtfbase[ii] = kk + 1; |
368
|
|
|
|
|
|
} |
369
|
|
|
|
|
|
} |
370
|
|
|
|
|
|
/*-- end MTF init --*/ |
371
|
|
|
|
|
|
|
372
|
|
|
|
|
|
nblock = 0; |
373
|
3672
|
|
|
|
|
GET_MTF_VAL(BZ_X_MTF_1, BZ_X_MTF_2, nextSym); |
374
|
|
|
|
|
|
|
375
|
|
|
|
|
|
while (True) { |
376
|
|
|
|
|
|
|
377
|
533050
|
|
|
|
|
if (nextSym == EOB) break; |
378
|
|
|
|
|
|
|
379
|
529954
|
|
|
|
|
if (nextSym == BZ_RUNA || nextSym == BZ_RUNB) { |
380
|
|
|
|
|
|
|
381
|
|
|
|
|
|
es = -1; |
382
|
|
|
|
|
|
N = 1; |
383
|
|
|
|
|
|
do { |
384
|
|
|
|
|
|
/* Check that N doesn't get too big, so that es doesn't |
385
|
|
|
|
|
|
go negative. The maximum value that can be |
386
|
|
|
|
|
|
RUNA/RUNB encoded is equal to the block size (post |
387
|
|
|
|
|
|
the initial RLE), viz, 900k, so bounding N at 2 |
388
|
|
|
|
|
|
million should guard against overflow without |
389
|
|
|
|
|
|
rejecting any legitimate inputs. */ |
390
|
109900
|
|
|
|
|
if (N >= 2*1024*1024) RETURN(BZ_DATA_ERROR); |
391
|
109900
|
|
|
|
|
if (nextSym == BZ_RUNA) es = es + (0+1) * N; else |
392
|
34492
|
|
|
|
|
if (nextSym == BZ_RUNB) es = es + (1+1) * N; |
393
|
109900
|
|
|
|
|
N = N * 2; |
394
|
109900
|
|
|
|
|
GET_MTF_VAL(BZ_X_MTF_3, BZ_X_MTF_4, nextSym); |
395
|
|
|
|
|
|
} |
396
|
109852
|
|
|
|
|
while (nextSym == BZ_RUNA || nextSym == BZ_RUNB); |
397
|
|
|
|
|
|
|
398
|
61862
|
|
|
|
|
es++; |
399
|
61862
|
|
|
|
|
uc = s->seqToUnseq[ s->mtfa[s->mtfbase[0]] ]; |
400
|
61862
|
|
|
|
|
s->unzftab[uc] += es; |
401
|
|
|
|
|
|
|
402
|
61862
|
|
|
|
|
if (s->smallDecompress) |
403
|
12
|
|
|
|
|
while (es > 0) { |
404
|
6
|
|
|
|
|
if (nblock >= nblockMAX) RETURN(BZ_DATA_ERROR); |
405
|
6
|
|
|
|
|
s->ll16[nblock] = (UInt16)uc; |
406
|
6
|
|
|
|
|
nblock++; |
407
|
6
|
|
|
|
|
es--; |
408
|
|
|
|
|
|
} |
409
|
|
|
|
|
|
else |
410
|
1368800
|
|
|
|
|
while (es > 0) { |
411
|
1306944
|
|
|
|
|
if (nblock >= nblockMAX) RETURN(BZ_DATA_ERROR); |
412
|
1306944
|
|
|
|
|
s->tt[nblock] = (UInt32)uc; |
413
|
1306944
|
|
|
|
|
nblock++; |
414
|
1306944
|
|
|
|
|
es--; |
415
|
|
|
|
|
|
}; |
416
|
|
|
|
|
|
|
417
|
61862
|
|
|
|
|
continue; |
418
|
|
|
|
|
|
|
419
|
|
|
|
|
|
} else { |
420
|
|
|
|
|
|
|
421
|
468044
|
|
|
|
|
if (nblock >= nblockMAX) RETURN(BZ_DATA_ERROR); |
422
|
|
|
|
|
|
|
423
|
|
|
|
|
|
/*-- uc = MTF ( nextSym-1 ) --*/ |
424
|
|
|
|
|
|
{ |
425
|
|
|
|
|
|
Int32 ii, jj, kk, pp, lno, off; |
426
|
|
|
|
|
|
UInt32 nn; |
427
|
468044
|
|
|
|
|
nn = (UInt32)(nextSym - 1); |
428
|
|
|
|
|
|
|
429
|
468044
|
|
|
|
|
if (nn < MTFL_SIZE) { |
430
|
|
|
|
|
|
/* avoid general-case expense */ |
431
|
217194
|
|
|
|
|
pp = s->mtfbase[0]; |
432
|
217194
|
|
|
|
|
uc = s->mtfa[pp+nn]; |
433
|
697598
|
|
|
|
|
while (nn > 3) { |
434
|
263210
|
|
|
|
|
Int32 z = pp+nn; |
435
|
263210
|
|
|
|
|
s->mtfa[(z) ] = s->mtfa[(z)-1]; |
436
|
263210
|
|
|
|
|
s->mtfa[(z)-1] = s->mtfa[(z)-2]; |
437
|
263210
|
|
|
|
|
s->mtfa[(z)-2] = s->mtfa[(z)-3]; |
438
|
263210
|
|
|
|
|
s->mtfa[(z)-3] = s->mtfa[(z)-4]; |
439
|
263210
|
|
|
|
|
nn -= 4; |
440
|
|
|
|
|
|
} |
441
|
552532
|
|
|
|
|
while (nn > 0) { |
442
|
335338
|
|
|
|
|
s->mtfa[(pp+nn)] = s->mtfa[(pp+nn)-1]; nn--; |
443
|
|
|
|
|
|
}; |
444
|
217194
|
|
|
|
|
s->mtfa[pp] = uc; |
445
|
|
|
|
|
|
} else { |
446
|
|
|
|
|
|
/* general case */ |
447
|
250850
|
|
|
|
|
lno = nn / MTFL_SIZE; |
448
|
250850
|
|
|
|
|
off = nn % MTFL_SIZE; |
449
|
250850
|
|
|
|
|
pp = s->mtfbase[lno] + off; |
450
|
250850
|
|
|
|
|
uc = s->mtfa[pp]; |
451
|
2266630
|
|
|
|
|
while (pp > s->mtfbase[lno]) { |
452
|
1764930
|
|
|
|
|
s->mtfa[pp] = s->mtfa[pp-1]; pp--; |
453
|
|
|
|
|
|
}; |
454
|
250850
|
|
|
|
|
s->mtfbase[lno]++; |
455
|
2288486
|
|
|
|
|
while (lno > 0) { |
456
|
1786786
|
|
|
|
|
s->mtfbase[lno]--; |
457
|
1786786
|
|
|
|
|
s->mtfa[s->mtfbase[lno]] |
458
|
1786786
|
|
|
|
|
= s->mtfa[s->mtfbase[lno-1] + MTFL_SIZE - 1]; |
459
|
1786786
|
|
|
|
|
lno--; |
460
|
|
|
|
|
|
} |
461
|
250850
|
|
|
|
|
s->mtfbase[0]--; |
462
|
250850
|
|
|
|
|
s->mtfa[s->mtfbase[0]] = uc; |
463
|
250850
|
|
|
|
|
if (s->mtfbase[0] == 0) { |
464
|
|
|
|
|
|
kk = MTFA_SIZE-1; |
465
|
864
|
|
|
|
|
for (ii = 256 / MTFL_SIZE-1; ii >= 0; ii--) { |
466
|
13824
|
|
|
|
|
for (jj = MTFL_SIZE-1; jj >= 0; jj--) { |
467
|
13824
|
|
|
|
|
s->mtfa[kk] = s->mtfa[s->mtfbase[ii] + jj]; |
468
|
13824
|
|
|
|
|
kk--; |
469
|
|
|
|
|
|
} |
470
|
864
|
|
|
|
|
s->mtfbase[ii] = kk + 1; |
471
|
|
|
|
|
|
} |
472
|
|
|
|
|
|
} |
473
|
|
|
|
|
|
} |
474
|
|
|
|
|
|
} |
475
|
|
|
|
|
|
/*-- end uc = MTF ( nextSym-1 ) --*/ |
476
|
|
|
|
|
|
|
477
|
468044
|
|
|
|
|
s->unzftab[s->seqToUnseq[uc]]++; |
478
|
468044
|
|
|
|
|
if (s->smallDecompress) |
479
|
48
|
|
|
|
|
s->ll16[nblock] = (UInt16)(s->seqToUnseq[uc]); else |
480
|
467996
|
|
|
|
|
s->tt[nblock] = (UInt32)(s->seqToUnseq[uc]); |
481
|
468044
|
|
|
|
|
nblock++; |
482
|
|
|
|
|
|
|
483
|
468044
|
|
|
|
|
GET_MTF_VAL(BZ_X_MTF_5, BZ_X_MTF_6, nextSym); |
484
|
467532
|
|
|
|
|
continue; |
485
|
|
|
|
|
|
} |
486
|
|
|
|
|
|
} |
487
|
|
|
|
|
|
|
488
|
|
|
|
|
|
/* Now we know what nblock is, we can do a better sanity |
489
|
|
|
|
|
|
check on s->origPtr. |
490
|
|
|
|
|
|
*/ |
491
|
3096
|
|
|
|
|
if (s->origPtr < 0 || s->origPtr >= nblock) |
492
|
|
|
|
|
|
RETURN(BZ_DATA_ERROR); |
493
|
|
|
|
|
|
|
494
|
|
|
|
|
|
/*-- Set up cftab to facilitate generation of T^(-1) --*/ |
495
|
|
|
|
|
|
/* Check: unzftab entries in range. */ |
496
|
792576
|
|
|
|
|
for (i = 0; i <= 255; i++) { |
497
|
792576
|
|
|
|
|
if (s->unzftab[i] < 0 || s->unzftab[i] > nblock) |
498
|
|
|
|
|
|
RETURN(BZ_DATA_ERROR); |
499
|
|
|
|
|
|
} |
500
|
|
|
|
|
|
/* Actually generate cftab. */ |
501
|
3096
|
|
|
|
|
s->cftab[0] = 0; |
502
|
3096
|
|
|
|
|
for (i = 1; i <= 256; i++) s->cftab[i] = s->unzftab[i-1]; |
503
|
792576
|
|
|
|
|
for (i = 1; i <= 256; i++) s->cftab[i] += s->cftab[i-1]; |
504
|
|
|
|
|
|
/* Check: cftab entries in range. */ |
505
|
795672
|
|
|
|
|
for (i = 0; i <= 256; i++) { |
506
|
795672
|
|
|
|
|
if (s->cftab[i] < 0 || s->cftab[i] > nblock) { |
507
|
|
|
|
|
|
/* s->cftab[i] can legitimately be == nblock */ |
508
|
|
|
|
|
|
RETURN(BZ_DATA_ERROR); |
509
|
|
|
|
|
|
} |
510
|
|
|
|
|
|
} |
511
|
|
|
|
|
|
/* Check: cftab entries non-descending. */ |
512
|
792576
|
|
|
|
|
for (i = 1; i <= 256; i++) { |
513
|
792576
|
|
|
|
|
if (s->cftab[i-1] > s->cftab[i]) { |
514
|
|
|
|
|
|
RETURN(BZ_DATA_ERROR); |
515
|
|
|
|
|
|
} |
516
|
|
|
|
|
|
} |
517
|
|
|
|
|
|
|
518
|
3096
|
|
|
|
|
s->state_out_len = 0; |
519
|
3096
|
|
|
|
|
s->state_out_ch = 0; |
520
|
3096
|
|
|
|
|
BZ_INITIALISE_CRC ( s->calculatedBlockCRC ); |
521
|
3096
|
|
|
|
|
s->state = BZ_X_OUTPUT; |
522
|
|
|
|
|
|
if (s->verbosity >= 2) VPrintf0 ( "rt+rld" ); |
523
|
|
|
|
|
|
|
524
|
3096
|
|
|
|
|
if (s->smallDecompress) { |
525
|
|
|
|
|
|
|
526
|
|
|
|
|
|
/*-- Make a copy of cftab, used in generation of T --*/ |
527
|
514
|
|
|
|
|
for (i = 0; i <= 256; i++) s->cftabCopy[i] = s->cftab[i]; |
528
|
|
|
|
|
|
|
529
|
|
|
|
|
|
/*-- compute the T vector --*/ |
530
|
54
|
|
|
|
|
for (i = 0; i < nblock; i++) { |
531
|
54
|
|
|
|
|
uc = (UChar)(s->ll16[i]); |
532
|
54
|
|
|
|
|
SET_LL(i, s->cftabCopy[uc]); |
533
|
54
|
|
|
|
|
s->cftabCopy[uc]++; |
534
|
|
|
|
|
|
} |
535
|
|
|
|
|
|
|
536
|
|
|
|
|
|
/*-- Compute T^(-1) by pointer reversal on T --*/ |
537
|
2
|
|
|
|
|
i = s->origPtr; |
538
|
2
|
|
|
|
|
j = GET_LL(i); |
539
|
|
|
|
|
|
do { |
540
|
54
|
|
|
|
|
Int32 tmp = GET_LL(j); |
541
|
54
|
|
|
|
|
SET_LL(j, i); |
542
|
|
|
|
|
|
i = j; |
543
|
|
|
|
|
|
j = tmp; |
544
|
|
|
|
|
|
} |
545
|
54
|
|
|
|
|
while (i != s->origPtr); |
546
|
|
|
|
|
|
|
547
|
2
|
|
|
|
|
s->tPos = s->origPtr; |
548
|
2
|
|
|
|
|
s->nblock_used = 0; |
549
|
2
|
|
|
|
|
if (s->blockRandomised) { |
550
|
0
|
|
|
|
|
BZ_RAND_INIT_MASK; |
551
|
0
|
|
|
|
|
BZ_GET_SMALL(s->k0); s->nblock_used++; |
552
|
0
|
|
|
|
|
BZ_RAND_UPD_MASK; s->k0 ^= BZ_RAND_MASK; |
553
|
|
|
|
|
|
} else { |
554
|
2
|
|
|
|
|
BZ_GET_SMALL(s->k0); s->nblock_used++; |
555
|
|
|
|
|
|
} |
556
|
|
|
|
|
|
|
557
|
|
|
|
|
|
} else { |
558
|
|
|
|
|
|
|
559
|
|
|
|
|
|
/*-- compute the T^(-1) vector --*/ |
560
|
1754940
|
|
|
|
|
for (i = 0; i < nblock; i++) { |
561
|
1754940
|
|
|
|
|
uc = (UChar)(s->tt[i] & 0xff); |
562
|
1754940
|
|
|
|
|
s->tt[s->cftab[uc]] |= (i << 8); |
563
|
1754940
|
|
|
|
|
s->cftab[uc]++; |
564
|
|
|
|
|
|
} |
565
|
|
|
|
|
|
|
566
|
3094
|
|
|
|
|
s->tPos = s->tt[s->origPtr] >> 8; |
567
|
3094
|
|
|
|
|
s->nblock_used = 0; |
568
|
3094
|
|
|
|
|
if (s->blockRandomised) { |
569
|
0
|
|
|
|
|
BZ_RAND_INIT_MASK; |
570
|
0
|
|
|
|
|
BZ_GET_FAST(s->k0); s->nblock_used++; |
571
|
0
|
|
|
|
|
BZ_RAND_UPD_MASK; s->k0 ^= BZ_RAND_MASK; |
572
|
|
|
|
|
|
} else { |
573
|
3094
|
|
|
|
|
BZ_GET_FAST(s->k0); s->nblock_used++; |
574
|
|
|
|
|
|
} |
575
|
|
|
|
|
|
|
576
|
|
|
|
|
|
} |
577
|
|
|
|
|
|
|
578
|
|
|
|
|
|
RETURN(BZ_OK); |
579
|
|
|
|
|
|
|
580
|
|
|
|
|
|
|
581
|
|
|
|
|
|
|
582
|
|
|
|
|
|
endhdr_2: |
583
|
|
|
|
|
|
|
584
|
8528
|
|
|
|
|
GET_UCHAR(BZ_X_ENDHDR_2, uc); |
585
|
3500
|
|
|
|
|
if (uc != 0x72) RETURN(BZ_DATA_ERROR); |
586
|
7768
|
|
|
|
|
GET_UCHAR(BZ_X_ENDHDR_3, uc); |
587
|
3484
|
|
|
|
|
if (uc != 0x45) RETURN(BZ_DATA_ERROR); |
588
|
8464
|
|
|
|
|
GET_UCHAR(BZ_X_ENDHDR_4, uc); |
589
|
3468
|
|
|
|
|
if (uc != 0x38) RETURN(BZ_DATA_ERROR); |
590
|
7704
|
|
|
|
|
GET_UCHAR(BZ_X_ENDHDR_5, uc); |
591
|
3452
|
|
|
|
|
if (uc != 0x50) RETURN(BZ_DATA_ERROR); |
592
|
8400
|
|
|
|
|
GET_UCHAR(BZ_X_ENDHDR_6, uc); |
593
|
3436
|
|
|
|
|
if (uc != 0x90) RETURN(BZ_DATA_ERROR); |
594
|
|
|
|
|
|
|
595
|
3436
|
|
|
|
|
s->storedCombinedCRC = 0; |
596
|
7640
|
|
|
|
|
GET_UCHAR(BZ_X_CCRC_1, uc); |
597
|
3420
|
|
|
|
|
s->storedCombinedCRC = (s->storedCombinedCRC << 8) | ((UInt32)uc); |
598
|
8336
|
|
|
|
|
GET_UCHAR(BZ_X_CCRC_2, uc); |
599
|
3404
|
|
|
|
|
s->storedCombinedCRC = (s->storedCombinedCRC << 8) | ((UInt32)uc); |
600
|
7608
|
|
|
|
|
GET_UCHAR(BZ_X_CCRC_3, uc); |
601
|
3388
|
|
|
|
|
s->storedCombinedCRC = (s->storedCombinedCRC << 8) | ((UInt32)uc); |
602
|
8272
|
|
|
|
|
GET_UCHAR(BZ_X_CCRC_4, uc); |
603
|
3372
|
|
|
|
|
s->storedCombinedCRC = (s->storedCombinedCRC << 8) | ((UInt32)uc); |
604
|
|
|
|
|
|
|
605
|
3372
|
|
|
|
|
s->state = BZ_X_IDLE; |
606
|
3372
|
|
|
|
|
RETURN(BZ_STREAM_END); |
607
|
|
|
|
|
|
|
608
|
0
|
|
|
|
|
default: AssertH ( False, 4001 ); |
609
|
|
|
|
|
|
} |
610
|
|
|
|
|
|
|
611
|
0
|
|
|
|
|
AssertH ( False, 4002 ); |
612
|
|
|
|
|
|
|
613
|
|
|
|
|
|
save_state_and_return: |
614
|
|
|
|
|
|
|
615
|
167312
|
|
|
|
|
s->save_i = i; |
616
|
167312
|
|
|
|
|
s->save_j = j; |
617
|
167312
|
|
|
|
|
s->save_t = t; |
618
|
167312
|
|
|
|
|
s->save_alphaSize = alphaSize; |
619
|
167312
|
|
|
|
|
s->save_nGroups = nGroups; |
620
|
167312
|
|
|
|
|
s->save_nSelectors = nSelectors; |
621
|
167312
|
|
|
|
|
s->save_EOB = EOB; |
622
|
167312
|
|
|
|
|
s->save_groupNo = groupNo; |
623
|
167312
|
|
|
|
|
s->save_groupPos = groupPos; |
624
|
167312
|
|
|
|
|
s->save_nextSym = nextSym; |
625
|
167312
|
|
|
|
|
s->save_nblockMAX = nblockMAX; |
626
|
167312
|
|
|
|
|
s->save_nblock = nblock; |
627
|
167312
|
|
|
|
|
s->save_es = es; |
628
|
167312
|
|
|
|
|
s->save_N = N; |
629
|
167312
|
|
|
|
|
s->save_curr = curr; |
630
|
167312
|
|
|
|
|
s->save_zt = zt; |
631
|
167312
|
|
|
|
|
s->save_zn = zn; |
632
|
167312
|
|
|
|
|
s->save_zvec = zvec; |
633
|
167312
|
|
|
|
|
s->save_zj = zj; |
634
|
167312
|
|
|
|
|
s->save_gSel = gSel; |
635
|
167312
|
|
|
|
|
s->save_gMinlen = gMinlen; |
636
|
167312
|
|
|
|
|
s->save_gLimit = gLimit; |
637
|
167312
|
|
|
|
|
s->save_gBase = gBase; |
638
|
167312
|
|
|
|
|
s->save_gPerm = gPerm; |
639
|
|
|
|
|
|
|
640
|
167312
|
|
|
|
|
return retVal; |
641
|
|
|
|
|
|
} |
642
|
|
|
|
|
|
|
643
|
|
|
|
|
|
|
644
|
|
|
|
|
|
/*-------------------------------------------------------------*/ |
645
|
|
|
|
|
|
/*--- end decompress.c ---*/ |
646
|
|
|
|
|
|
/*-------------------------------------------------------------*/ |