blob: dd15b819f76b8e5ac376543de281b1fe6cec270b [file] [log] [blame]
Glenn L McGrath60bce492002-11-03 07:28:38 +00001/*--
2 This file is a part of bzip2 and/or libbzip2, a program and
3 library for lossless, block-sorting data compression.
4
5 Copyright (C) 1996-2000 Julian R Seward. All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions
9 are met:
10
11 1. Redistributions of source code must retain the above copyright
12 notice, this list of conditions and the following disclaimer.
13
14 2. The origin of this software must not be misrepresented; you must
15 not claim that you wrote the original software. If you use this
16 software in a product, an acknowledgment in the product
17 documentation would be appreciated but is not required.
18
19 3. Altered source versions must be plainly marked as such, and must
20 not be misrepresented as being the original software.
21
22 4. The name of the author may not be used to endorse or promote
23 products derived from this software without specific prior written
24 permission.
25
26 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
27 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
28 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
30 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
32 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
34 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37
38 Julian Seward, Cambridge, UK.
39 jseward@acm.org
40 bzip2/libbzip2 version 1.0 of 21 March 2000
41
42 This program is based on (at least) the work of:
43 Mike Burrows
44 David Wheeler
45 Peter Fenwick
46 Alistair Moffat
47 Radford Neal
48 Ian H. Witten
49 Robert Sedgewick
50 Jon L. Bentley
51
52 For more information on these sources, see the manual.
53--*/
54
55#include <stdlib.h>
56#include <stdio.h>
57#include <string.h>
58#include <getopt.h>
59#include <unistd.h>
60#include <busybox.h>
61
62//#define TRUE 1
63//#define FALSE 0
64
65#define MTFA_SIZE 4096
66#define MTFL_SIZE 16
67#define BZ_N_GROUPS 6
68#define BZ_G_SIZE 50
69#define BZ_MAX_ALPHA_SIZE 258
70
71#define BZ_OK 0
72#define BZ_STREAM_END 4
73#define BZ_SEQUENCE_ERROR (-1)
74#define BZ_DATA_ERROR (-4)
75#define BZ_DATA_ERROR_MAGIC (-5)
76#define BZ_IO_ERROR (-6)
77#define BZ_UNEXPECTED_EOF (-7)
78
79#define BZ_RUNA 0
80#define BZ_RUNB 1
81
82#define BZ_MAX_UNUSED 5000
83#define FILE_NAME_LEN 1034
84/*-- states for decompression. --*/
85
86#define BZ_X_IDLE 1
87#define BZ_X_OUTPUT 2
88
89#define BZ_X_MAGIC_1 10
90#define BZ_X_MAGIC_2 11
91#define BZ_X_MAGIC_3 12
92#define BZ_X_MAGIC_4 13
93#define BZ_X_BLKHDR_1 14
94#define BZ_X_BLKHDR_2 15
95#define BZ_X_BLKHDR_3 16
96#define BZ_X_BLKHDR_4 17
97#define BZ_X_BLKHDR_5 18
98#define BZ_X_BLKHDR_6 19
99#define BZ_X_BCRC_1 20
100#define BZ_X_BCRC_2 21
101#define BZ_X_BCRC_3 22
102#define BZ_X_BCRC_4 23
103#define BZ_X_RANDBIT 24
104#define BZ_X_ORIGPTR_1 25
105#define BZ_X_ORIGPTR_2 26
106#define BZ_X_ORIGPTR_3 27
107#define BZ_X_MAPPING_1 28
108#define BZ_X_MAPPING_2 29
109#define BZ_X_SELECTOR_1 30
110#define BZ_X_SELECTOR_2 31
111#define BZ_X_SELECTOR_3 32
112#define BZ_X_CODING_1 33
113#define BZ_X_CODING_2 34
114#define BZ_X_CODING_3 35
115#define BZ_X_MTF_1 36
116#define BZ_X_MTF_2 37
117#define BZ_X_MTF_3 38
118#define BZ_X_MTF_4 39
119#define BZ_X_MTF_5 40
120#define BZ_X_MTF_6 41
121#define BZ_X_ENDHDR_2 42
122#define BZ_X_ENDHDR_3 43
123#define BZ_X_ENDHDR_4 44
124#define BZ_X_ENDHDR_5 45
125#define BZ_X_ENDHDR_6 46
126#define BZ_X_CCRC_1 47
127#define BZ_X_CCRC_2 48
128#define BZ_X_CCRC_3 49
129#define BZ_X_CCRC_4 50
130
131#define BZ_MAX_CODE_LEN 23
132#define OM_TEST 3
133
134typedef struct {
135 char *next_in;
136 unsigned int avail_in;
137
138 char *next_out;
139 unsigned int avail_out;
140
141 void *state;
142
143} bz_stream;
144
145typedef struct {
146 bz_stream strm;
147 FILE *handle;
148 unsigned char initialisedOk;
149 char buf[BZ_MAX_UNUSED];
150 int lastErr;
151 int bufN;
152} bzFile;
153
154/*-- Structure holding all the decompression-side stuff. --*/
155typedef struct {
156 /* pointer back to the struct bz_stream */
157 bz_stream* strm;
158
159 /* state indicator for this stream */
160 int state;
161
162 /* for doing the final run-length decoding */
163 unsigned char state_out_ch;
164 int state_out_len;
165 unsigned char blockRandomised;
166 int rNToGo;
167 int rTPos;
168
169 /* the buffer for bit stream reading */
170 unsigned int bsBuff;
171 int bsLive;
172
173 /* misc administratium */
174 int blockSize100k;
175 int currBlockNo;
176
177 /* for undoing the Burrows-Wheeler transform */
178 int origPtr;
179 unsigned int tPos;
180 int k0;
181 int unzftab[256];
182 int nblock_used;
183 int cftab[257];
184 int cftabCopy[257];
185
186 /* for undoing the Burrows-Wheeler transform (FAST) */
187 unsigned int *tt;
188
189 /* stored and calculated CRCs */
190 unsigned int storedBlockCRC;
191 unsigned int storedCombinedCRC;
192 unsigned int calculatedBlockCRC;
193 unsigned int calculatedCombinedCRC;
194
195 /* map of bytes used in block */
196 int nInUse;
197 unsigned char inUse[256];
198 unsigned char inUse16[16];
199 unsigned char seqToUnseq[256];
200
201 /* for decoding the MTF values */
202 unsigned char mtfa [MTFA_SIZE];
203 unsigned char selector [2 + (900000 / BZ_G_SIZE)];
204 unsigned char selectorMtf[2 + (900000 / BZ_G_SIZE)];
205 unsigned char len [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
206 int mtfbase[256 / MTFL_SIZE];
207
208 int limit [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
209 int base [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
210 int perm [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
211 int minLens[BZ_N_GROUPS];
212
213 /* save area for scalars in the main decompress code */
214 int save_i;
215 int save_j;
216 int save_t;
217 int save_alphaSize;
218 int save_nGroups;
219 int save_nSelectors;
220 int save_EOB;
221 int save_groupNo;
222 int save_groupPos;
223 int save_nextSym;
224 int save_nblockMAX;
225 int save_nblock;
226 int save_es;
227 int save_N;
228 int save_curr;
229 int save_zt;
230 int save_zn;
231 int save_zvec;
232 int save_zj;
233 int save_gSel;
234 int save_gMinlen;
235 int *save_gLimit;
236 int *save_gBase;
237 int *save_gPerm;
238} DState;
239
240int BZ2_rNums[512];
241char inName[FILE_NAME_LEN];
242char outName[FILE_NAME_LEN];
243int srcMode;
244int opMode;
245unsigned char deleteOutputOnInterrupt;
246FILE *outputHandleJustInCase;
247int numFileNames;
248int numFilesProcessed;
249int exitValue;
250
251const unsigned int BZ2_crc32Table[256] = {
252
253 /*-- Ugly, innit? --*/
254
255 0x00000000L, 0x04c11db7L, 0x09823b6eL, 0x0d4326d9L,
256 0x130476dcL, 0x17c56b6bL, 0x1a864db2L, 0x1e475005L,
257 0x2608edb8L, 0x22c9f00fL, 0x2f8ad6d6L, 0x2b4bcb61L,
258 0x350c9b64L, 0x31cd86d3L, 0x3c8ea00aL, 0x384fbdbdL,
259 0x4c11db70L, 0x48d0c6c7L, 0x4593e01eL, 0x4152fda9L,
260 0x5f15adacL, 0x5bd4b01bL, 0x569796c2L, 0x52568b75L,
261 0x6a1936c8L, 0x6ed82b7fL, 0x639b0da6L, 0x675a1011L,
262 0x791d4014L, 0x7ddc5da3L, 0x709f7b7aL, 0x745e66cdL,
263 0x9823b6e0L, 0x9ce2ab57L, 0x91a18d8eL, 0x95609039L,
264 0x8b27c03cL, 0x8fe6dd8bL, 0x82a5fb52L, 0x8664e6e5L,
265 0xbe2b5b58L, 0xbaea46efL, 0xb7a96036L, 0xb3687d81L,
266 0xad2f2d84L, 0xa9ee3033L, 0xa4ad16eaL, 0xa06c0b5dL,
267 0xd4326d90L, 0xd0f37027L, 0xddb056feL, 0xd9714b49L,
268 0xc7361b4cL, 0xc3f706fbL, 0xceb42022L, 0xca753d95L,
269 0xf23a8028L, 0xf6fb9d9fL, 0xfbb8bb46L, 0xff79a6f1L,
270 0xe13ef6f4L, 0xe5ffeb43L, 0xe8bccd9aL, 0xec7dd02dL,
271 0x34867077L, 0x30476dc0L, 0x3d044b19L, 0x39c556aeL,
272 0x278206abL, 0x23431b1cL, 0x2e003dc5L, 0x2ac12072L,
273 0x128e9dcfL, 0x164f8078L, 0x1b0ca6a1L, 0x1fcdbb16L,
274 0x018aeb13L, 0x054bf6a4L, 0x0808d07dL, 0x0cc9cdcaL,
275 0x7897ab07L, 0x7c56b6b0L, 0x71159069L, 0x75d48ddeL,
276 0x6b93dddbL, 0x6f52c06cL, 0x6211e6b5L, 0x66d0fb02L,
277 0x5e9f46bfL, 0x5a5e5b08L, 0x571d7dd1L, 0x53dc6066L,
278 0x4d9b3063L, 0x495a2dd4L, 0x44190b0dL, 0x40d816baL,
279 0xaca5c697L, 0xa864db20L, 0xa527fdf9L, 0xa1e6e04eL,
280 0xbfa1b04bL, 0xbb60adfcL, 0xb6238b25L, 0xb2e29692L,
281 0x8aad2b2fL, 0x8e6c3698L, 0x832f1041L, 0x87ee0df6L,
282 0x99a95df3L, 0x9d684044L, 0x902b669dL, 0x94ea7b2aL,
283 0xe0b41de7L, 0xe4750050L, 0xe9362689L, 0xedf73b3eL,
284 0xf3b06b3bL, 0xf771768cL, 0xfa325055L, 0xfef34de2L,
285 0xc6bcf05fL, 0xc27dede8L, 0xcf3ecb31L, 0xcbffd686L,
286 0xd5b88683L, 0xd1799b34L, 0xdc3abdedL, 0xd8fba05aL,
287 0x690ce0eeL, 0x6dcdfd59L, 0x608edb80L, 0x644fc637L,
288 0x7a089632L, 0x7ec98b85L, 0x738aad5cL, 0x774bb0ebL,
289 0x4f040d56L, 0x4bc510e1L, 0x46863638L, 0x42472b8fL,
290 0x5c007b8aL, 0x58c1663dL, 0x558240e4L, 0x51435d53L,
291 0x251d3b9eL, 0x21dc2629L, 0x2c9f00f0L, 0x285e1d47L,
292 0x36194d42L, 0x32d850f5L, 0x3f9b762cL, 0x3b5a6b9bL,
293 0x0315d626L, 0x07d4cb91L, 0x0a97ed48L, 0x0e56f0ffL,
294 0x1011a0faL, 0x14d0bd4dL, 0x19939b94L, 0x1d528623L,
295 0xf12f560eL, 0xf5ee4bb9L, 0xf8ad6d60L, 0xfc6c70d7L,
296 0xe22b20d2L, 0xe6ea3d65L, 0xeba91bbcL, 0xef68060bL,
297 0xd727bbb6L, 0xd3e6a601L, 0xdea580d8L, 0xda649d6fL,
298 0xc423cd6aL, 0xc0e2d0ddL, 0xcda1f604L, 0xc960ebb3L,
299 0xbd3e8d7eL, 0xb9ff90c9L, 0xb4bcb610L, 0xb07daba7L,
300 0xae3afba2L, 0xaafbe615L, 0xa7b8c0ccL, 0xa379dd7bL,
301 0x9b3660c6L, 0x9ff77d71L, 0x92b45ba8L, 0x9675461fL,
302 0x8832161aL, 0x8cf30badL, 0x81b02d74L, 0x857130c3L,
303 0x5d8a9099L, 0x594b8d2eL, 0x5408abf7L, 0x50c9b640L,
304 0x4e8ee645L, 0x4a4ffbf2L, 0x470cdd2bL, 0x43cdc09cL,
305 0x7b827d21L, 0x7f436096L, 0x7200464fL, 0x76c15bf8L,
306 0x68860bfdL, 0x6c47164aL, 0x61043093L, 0x65c52d24L,
307 0x119b4be9L, 0x155a565eL, 0x18197087L, 0x1cd86d30L,
308 0x029f3d35L, 0x065e2082L, 0x0b1d065bL, 0x0fdc1becL,
309 0x3793a651L, 0x3352bbe6L, 0x3e119d3fL, 0x3ad08088L,
310 0x2497d08dL, 0x2056cd3aL, 0x2d15ebe3L, 0x29d4f654L,
311 0xc5a92679L, 0xc1683bceL, 0xcc2b1d17L, 0xc8ea00a0L,
312 0xd6ad50a5L, 0xd26c4d12L, 0xdf2f6bcbL, 0xdbee767cL,
313 0xe3a1cbc1L, 0xe760d676L, 0xea23f0afL, 0xeee2ed18L,
314 0xf0a5bd1dL, 0xf464a0aaL, 0xf9278673L, 0xfde69bc4L,
315 0x89b8fd09L, 0x8d79e0beL, 0x803ac667L, 0x84fbdbd0L,
316 0x9abc8bd5L, 0x9e7d9662L, 0x933eb0bbL, 0x97ffad0cL,
317 0xafb010b1L, 0xab710d06L, 0xa6322bdfL, 0xa2f33668L,
318 0xbcb4666dL, 0xb8757bdaL, 0xb5365d03L, 0xb1f740b4L
319};
320
321static void bz_rand_udp_mask(DState *s)
322{
323 if (s->rNToGo == 0) {
324 s->rNToGo = BZ2_rNums[s->rTPos];
325 s->rTPos++;
326 if (s->rTPos == 512) {
327 s->rTPos = 0;
328 }
329 }
330 s->rNToGo--;
331}
332
333static unsigned char myfeof(FILE *f)
334{
335 int c = fgetc(f);
336 if (c == EOF) {
337 return(TRUE);
338 }
339 ungetc(c, f);
340 return(FALSE);
341}
342
343static void BZ2_hbCreateDecodeTables(int *limit, int *base, int *perm, unsigned char *length, int minLen, int maxLen, int alphaSize )
344{
345 int pp, i, j, vec;
346
347 pp = 0;
348 for (i = minLen; i <= maxLen; i++) {
349 for (j = 0; j < alphaSize; j++) {
350 if (length[j] == i) {
351 perm[pp] = j;
352 pp++;
353 }
354 }
355 }
356
357 for (i = 0; i < BZ_MAX_CODE_LEN; i++) {
358 base[i] = 0;
359 }
360
361 for (i = 0; i < alphaSize; i++) {
362 base[length[i]+1]++;
363 }
364
365 for (i = 1; i < BZ_MAX_CODE_LEN; i++) {
366 base[i] += base[i-1];
367 }
368
369 for (i = 0; i < BZ_MAX_CODE_LEN; i++) {
370 limit[i] = 0;
371 }
372 vec = 0;
373
374 for (i = minLen; i <= maxLen; i++) {
375 vec += (base[i+1] - base[i]);
376 limit[i] = vec-1;
377 vec <<= 1;
378 }
379 for (i = minLen + 1; i <= maxLen; i++) {
380 base[i] = ((limit[i-1] + 1) << 1) - base[i];
381 }
382}
383
384
385static int get_bits(DState *s, int *vvv, char nnn)
386{
387 while (1) {
388 if (s->bsLive >= nnn) {
389 *vvv = (s->bsBuff >> (s->bsLive-nnn)) & ((1 << nnn)-1);
390 s->bsLive -= nnn;
391 break;
392 }
393 if (s->strm->avail_in == 0) {
394 return(FALSE);
395 }
396 s->bsBuff = (s->bsBuff << 8) | ((unsigned int) (*((unsigned char*)(s->strm->next_in))));
397 s->bsLive += 8;
398 s->strm->next_in++;
399 s->strm->avail_in--;
400 }
401 return(TRUE);
402}
403
404static int bz_get_fast(DState *s)
405{
406 int cccc;
407 s->tPos = s->tt[s->tPos];
408 cccc = (unsigned char)(s->tPos & 0xff);
409 s->tPos >>= 8;
410 return(cccc);
411}
412
413/*---------------------------------------------------*/
414static inline int BZ2_decompress(DState *s)
415{
416 int uc = 0;
417 int retVal;
418 int minLen, maxLen;
419
420 /* stuff that needs to be saved/restored */
421 int i;
422 int j;
423 int t;
424 int alphaSize;
425 int nGroups;
426 int nSelectors;
427 int EOB;
428 int groupNo;
429 int groupPos;
430 int nextSym;
431 int nblockMAX;
432 int nblock;
433 int es;
434 int N;
435 int curr;
436 int zt;
437 int zn;
438 int zvec;
439 int zj;
440 int gSel;
441 int gMinlen;
442 int *gLimit;
443 int *gBase;
444 int *gPerm;
445 int switch_val;
446
447 int get_mtf_val_init(void)
448 {
449 if (groupPos == 0) {
450 groupNo++;
451 if (groupNo >= nSelectors) {
452 retVal = BZ_DATA_ERROR;
453 return(FALSE);
454 }
455 groupPos = BZ_G_SIZE;
456 gSel = s->selector[groupNo];
457 gMinlen = s->minLens[gSel];
458 gLimit = &(s->limit[gSel][0]);
459 gPerm = &(s->perm[gSel][0]);
460 gBase = &(s->base[gSel][0]);
461 }
462 groupPos--;
463 zn = gMinlen;
464 return(TRUE);
465 }
466
467 if (s->state == BZ_X_MAGIC_1) {
468 /*initialise the save area*/
469 s->save_i = 0;
470 s->save_j = 0;
471 s->save_t = 0;
472 s->save_alphaSize = 0;
473 s->save_nGroups = 0;
474 s->save_nSelectors = 0;
475 s->save_EOB = 0;
476 s->save_groupNo = 0;
477 s->save_groupPos = 0;
478 s->save_nextSym = 0;
479 s->save_nblockMAX = 0;
480 s->save_nblock = 0;
481 s->save_es = 0;
482 s->save_N = 0;
483 s->save_curr = 0;
484 s->save_zt = 0;
485 s->save_zn = 0;
486 s->save_zvec = 0;
487 s->save_zj = 0;
488 s->save_gSel = 0;
489 s->save_gMinlen = 0;
490 s->save_gLimit = NULL;
491 s->save_gBase = NULL;
492 s->save_gPerm = NULL;
493 }
494
495 /*restore from the save area*/
496 i = s->save_i;
497 j = s->save_j;
498 t = s->save_t;
499 alphaSize = s->save_alphaSize;
500 nGroups = s->save_nGroups;
501 nSelectors = s->save_nSelectors;
502 EOB = s->save_EOB;
503 groupNo = s->save_groupNo;
504 groupPos = s->save_groupPos;
505 nextSym = s->save_nextSym;
506 nblockMAX = s->save_nblockMAX;
507 nblock = s->save_nblock;
508 es = s->save_es;
509 N = s->save_N;
510 curr = s->save_curr;
511 zt = s->save_zt;
512 zn = s->save_zn;
513 zvec = s->save_zvec;
514 zj = s->save_zj;
515 gSel = s->save_gSel;
516 gMinlen = s->save_gMinlen;
517 gLimit = s->save_gLimit;
518 gBase = s->save_gBase;
519 gPerm = s->save_gPerm;
520
521 retVal = BZ_OK;
522 switch_val = s->state;
523 switch (switch_val) {
524 case BZ_X_MAGIC_1:
525 s->state = BZ_X_MAGIC_1;
526 if (! get_bits(s, &uc, 8)) {
527 retVal = BZ_OK;
528 goto save_state_and_return;
529 }
530 if (uc != 'B') {
531 retVal = BZ_DATA_ERROR_MAGIC;
532 goto save_state_and_return;
533 }
534
535 case BZ_X_MAGIC_2:
536 s->state = BZ_X_MAGIC_2;
537 if (! get_bits(s, &uc, 8)) {
538 retVal = BZ_OK;
539 goto save_state_and_return;
540 }
541 if (uc != 'Z') {
542 retVal = BZ_DATA_ERROR_MAGIC;
543 goto save_state_and_return;
544 }
545
546 case BZ_X_MAGIC_3:
547 s->state = BZ_X_MAGIC_3;
548 if (! get_bits(s, &uc, 8)) {
549 retVal = BZ_OK;
550 goto save_state_and_return;
551 }
552 if (uc != 'h') {
553 retVal = BZ_DATA_ERROR_MAGIC;
554 goto save_state_and_return;
555 }
556
557 case BZ_X_MAGIC_4:
558 s->state = BZ_X_MAGIC_4;
559 if (! get_bits(s, &s->blockSize100k, 8)) {
560 retVal = BZ_OK;
561 goto save_state_and_return;
562 }
563 if ((s->blockSize100k < '1') || (s->blockSize100k > '9')) {
564 retVal = BZ_DATA_ERROR_MAGIC;
565 goto save_state_and_return;
566 }
567 s->blockSize100k -= '0';
568
569 s->tt = xmalloc(s->blockSize100k * 100000 * sizeof(int));
570
571 case BZ_X_BLKHDR_1:
572 s->state = BZ_X_BLKHDR_1;
573 if (! get_bits(s, &uc, 8)) {
574 retVal = BZ_OK;
575 goto save_state_and_return;
576 }
577
578 if (uc == 0x17) {
579 goto endhdr_2;
580 }
581 if (uc != 0x31) {
582 retVal = BZ_DATA_ERROR;
583 goto save_state_and_return;
584 }
585
586 case BZ_X_BLKHDR_2:
587 s->state = BZ_X_BLKHDR_2;
588 if (! get_bits(s, &uc, 8)) {
589 retVal = BZ_OK;
590 goto save_state_and_return;
591 }
592 if (uc != 0x41) {
593 retVal = BZ_DATA_ERROR;
594 goto save_state_and_return;
595 }
596
597 case BZ_X_BLKHDR_3:
598 s->state = BZ_X_BLKHDR_3;
599 if (! get_bits(s, &uc, 8)) {
600 retVal = BZ_OK;
601 goto save_state_and_return;
602 }
603 if (uc != 0x59) {
604 retVal = BZ_DATA_ERROR;
605 goto save_state_and_return;
606 }
607
608 case BZ_X_BLKHDR_4:
609 s->state = BZ_X_BLKHDR_4;
610 if (! get_bits(s, &uc, 8)) {
611 retVal = BZ_OK;
612 goto save_state_and_return;
613 }
614 if (uc != 0x26) {
615 retVal = BZ_DATA_ERROR;
616 goto save_state_and_return;
617 }
618
619 case BZ_X_BLKHDR_5:
620 s->state = BZ_X_BLKHDR_5;
621 if (! get_bits(s, &uc, 8)) {
622 retVal = BZ_OK;
623 goto save_state_and_return;
624 }
625 if (uc != 0x53) {
626 retVal = BZ_DATA_ERROR;
627 goto save_state_and_return;
628 }
629
630 case BZ_X_BLKHDR_6:
631 s->state = BZ_X_BLKHDR_6;
632 if (! get_bits(s, &uc, 8)) {
633 retVal = BZ_OK;
634 goto save_state_and_return;
635 }
636 if (uc != 0x59) {
637 retVal = BZ_DATA_ERROR;
638 goto save_state_and_return;
639 }
640
641 s->currBlockNo++;
642 s->storedBlockCRC = 0;
643
644 case BZ_X_BCRC_1:
645 s->state = BZ_X_BCRC_1;
646 if (! get_bits(s, &uc, 8)) {
647 retVal = BZ_OK;
648 goto save_state_and_return;
649 }
650 s->storedBlockCRC = (s->storedBlockCRC << 8) | ((unsigned int)uc);
651
652 case BZ_X_BCRC_2:
653 s->state = BZ_X_BCRC_2;
654 if (! get_bits(s, &uc, 8)) {
655 retVal = BZ_OK;
656 goto save_state_and_return;
657 }
658 s->storedBlockCRC = (s->storedBlockCRC << 8) | ((unsigned int)uc);
659
660 case BZ_X_BCRC_3:
661 s->state = BZ_X_BCRC_3;
662 if (! get_bits(s, &uc, 8)) {
663 retVal = BZ_OK;
664 goto save_state_and_return;
665 }
666 s->storedBlockCRC = (s->storedBlockCRC << 8) | ((unsigned int)uc);
667
668 case BZ_X_BCRC_4:
669 s->state = BZ_X_BCRC_4;
670 if (! get_bits(s, &uc, 8)) {
671 retVal = BZ_OK;
672 goto save_state_and_return;
673 }
674 s->storedBlockCRC = (s->storedBlockCRC << 8) | ((unsigned int)uc);
675
676 case BZ_X_RANDBIT:
677 s->state = BZ_X_RANDBIT;
678 {
679 int tmp = s->blockRandomised;
680 const int ret = get_bits(s, &tmp, 1);
681 s->blockRandomised = tmp;
682 if (! ret) {
683 retVal = BZ_OK;
684 goto save_state_and_return;
685 }
686 }
687
688 s->origPtr = 0;
689
690 case BZ_X_ORIGPTR_1:
691 s->state = BZ_X_ORIGPTR_1;
692 if (! get_bits(s, &uc, 8)) {
693 retVal = BZ_OK;
694 goto save_state_and_return;
695 }
696 s->origPtr = (s->origPtr << 8) | ((int)uc);
697
698 case BZ_X_ORIGPTR_2:
699 s->state = BZ_X_ORIGPTR_2;
700 if (! get_bits(s, &uc, 8)) {
701 retVal = BZ_OK;
702 goto save_state_and_return;
703 }
704 s->origPtr = (s->origPtr << 8) | ((int)uc);
705
706 case BZ_X_ORIGPTR_3:
707 s->state = BZ_X_ORIGPTR_3;
708 if (! get_bits(s, &uc, 8)) {
709 retVal = BZ_OK;
710 goto save_state_and_return;
711 }
712 s->origPtr = (s->origPtr << 8) | ((int)uc);
713
714 if (s->origPtr < 0) {
715 retVal = BZ_DATA_ERROR;
716 goto save_state_and_return;
717 }
718 if (s->origPtr > 10 + 100000*s->blockSize100k) {
719 retVal = BZ_DATA_ERROR;
720 goto save_state_and_return;
721 }
722
723 /*--- Receive the mapping table ---*/
724 case BZ_X_MAPPING_1:
725 for (i = 0; i < 16; i++) {
726 s->state = BZ_X_MAPPING_1;
727 if (! get_bits(s, &uc, 1)) {
728 retVal = BZ_OK;
729 goto save_state_and_return;
730 }
731 if (uc == 1) {
732 s->inUse16[i] = TRUE;
733 } else {
734 s->inUse16[i] = FALSE;
735 }
736 }
737
738 for (i = 0; i < 256; i++) {
739 s->inUse[i] = FALSE;
740 }
741
742 for (i = 0; i < 16; i++) {
743 if (s->inUse16[i]) {
744 for (j = 0; j < 16; j++) {
745 case BZ_X_MAPPING_2:
746 s->state = BZ_X_MAPPING_2;
747 if (! get_bits(s, &uc, 1)) {
748 retVal = BZ_OK;
749 goto save_state_and_return;
750 }
751 if (uc == 1) {
752 s->inUse[i * 16 + j] = TRUE;
753 }
754 }
755 }
756 }
757
758 s->nInUse = 0;
759 for (i = 0; i < 256; i++) {
760 if (s->inUse[i]) {
761 s->seqToUnseq[s->nInUse] = i;
762 s->nInUse++;
763 }
764 }
765 if (s->nInUse == 0) {
766 retVal = BZ_DATA_ERROR;
767 goto save_state_and_return;
768 }
769 alphaSize = s->nInUse+2;
770
771 /*--- Now the selectors ---*/
772 case BZ_X_SELECTOR_1:
773 s->state = BZ_X_SELECTOR_1;
774 if (! get_bits(s, &nGroups, 3)) {
775 retVal = BZ_OK;
776 goto save_state_and_return;
777 }
778 if (nGroups < 2 || nGroups > 6) {
779 retVal = BZ_DATA_ERROR;
780 goto save_state_and_return;
781 }
782
783 case BZ_X_SELECTOR_2:
784 s->state = BZ_X_SELECTOR_2;
785 if (! get_bits(s, &nSelectors, 15)) {
786 retVal = BZ_OK;
787 goto save_state_and_return;
788 }
789 if (nSelectors < 1) {
790 retVal = BZ_DATA_ERROR;
791 goto save_state_and_return;
792 }
793
794
795
796 for (i = 0; i < nSelectors; i++) {
797 j = 0;
798 while (1) {
799 case BZ_X_SELECTOR_3:
800 s->state = BZ_X_SELECTOR_3;
801 if (! get_bits(s, &uc, 1)) {
802 retVal = BZ_OK;
803 goto save_state_and_return;
804 }
805 if (uc == 0) {
806 break;
807 }
808 j++;
809 if (j >= nGroups) {
810 retVal = BZ_DATA_ERROR;
811 goto save_state_and_return;
812 }
813 }
814 s->selectorMtf[i] = j;
815 }
816
817 /*--- Undo the MTF values for the selectors. ---*/
818 {
819 unsigned char pos[BZ_N_GROUPS], tmp, v;
820 for (v = 0; v < nGroups; v++) {
821 pos[v] = v;
822 }
823 for (i = 0; i < nSelectors; i++) {
824 v = s->selectorMtf[i];
825 tmp = pos[v];
826 while (v > 0) {
827 pos[v] = pos[v-1];
828 v--;
829 }
830 pos[0] = tmp;
831 s->selector[i] = tmp;
832 }
833 }
834
835 /*--- Now the coding tables ---*/
836 for (t = 0; t < nGroups; t++) {
837 case BZ_X_CODING_1:
838 s->state = BZ_X_CODING_1;
839 if (! get_bits(s, &curr, 5)) {
840 retVal = BZ_OK;
841 goto save_state_and_return;
842 }
843 for (i = 0; i < alphaSize; i++) {
844 while (TRUE) {
845 if (curr < 1 || curr > 20) {
846 retVal = BZ_DATA_ERROR;
847 goto save_state_and_return;
848 }
849
850 case BZ_X_CODING_2:
851 s->state = BZ_X_CODING_2;
852 if (! get_bits(s, &uc, 1)) {
853 retVal = BZ_OK;
854 goto save_state_and_return;
855 }
856 if (uc == 0) {
857 break;
858 }
859
860 case BZ_X_CODING_3:
861 s->state = BZ_X_CODING_3;
862 if (! get_bits(s, &uc, 1)) {
863 retVal = BZ_OK;
864 goto save_state_and_return;
865 }
866 if (uc == 0) {
867 curr++;
868 } else {
869 curr--;
870 }
871 }
872 s->len[t][i] = curr;
873 }
874 }
875
876 /*--- Create the Huffman decoding tables ---*/
877 for (t = 0; t < nGroups; t++) {
878 minLen = 32;
879 maxLen = 0;
880 for (i = 0; i < alphaSize; i++) {
881 if (s->len[t][i] > maxLen) {
882 maxLen = s->len[t][i];
883 }
884 if (s->len[t][i] < minLen) {
885 minLen = s->len[t][i];
886 }
887 }
888
889 BZ2_hbCreateDecodeTables (
890 &(s->limit[t][0]),
891 &(s->base[t][0]),
892 &(s->perm[t][0]),
893 &(s->len[t][0]),
894 minLen, maxLen, alphaSize
895 );
896
897
898 s->minLens[t] = minLen;
899 }
900
901 /*--- Now the MTF values ---*/
902
903 EOB = s->nInUse+1;
904 nblockMAX = 100000 * s->blockSize100k;
905 groupNo = -1;
906 groupPos = 0;
907
908 for (i = 0; i <= 255; i++) {
909 s->unzftab[i] = 0;
910 }
911 /*-- MTF init --*/
912 {
913 int ii, jj, kk;
914 kk = MTFA_SIZE-1;
915 for (ii = 256 / MTFL_SIZE - 1; ii >= 0; ii--) {
916 for (jj = MTFL_SIZE-1; jj >= 0; jj--) {
917 s->mtfa[kk] = (unsigned char)(ii * MTFL_SIZE + jj);
918 kk--;
919 }
920 s->mtfbase[ii] = kk + 1;
921 }
922 }
923 /*-- end MTF init --*/
924
925 nblock = 0;
926
927 if (! get_mtf_val_init()) {
928 goto save_state_and_return;
929 }
930 case BZ_X_MTF_1:
931 s->state = BZ_X_MTF_1;
932 if (! get_bits(s, &zvec, zn)) {
933 retVal = BZ_OK;
934 goto save_state_and_return;
935 }
936 while (1) {
937 if (zn > 20 /* the longest code */) {
938 retVal = BZ_DATA_ERROR;
939 goto save_state_and_return;
940 }
941 if (zvec <= gLimit[zn]) {
942 break;
943 }
944 zn++;
945
946 case BZ_X_MTF_2:
947 s->state = BZ_X_MTF_2;
948 if (! get_bits(s, &zj, 1)) {
949 retVal = BZ_OK;
950 goto save_state_and_return;
951 }
952 zvec = (zvec << 1) | zj;
953 }
954 if (zvec - gBase[zn] < 0 || zvec - gBase[zn] >= BZ_MAX_ALPHA_SIZE) {
955 retVal = BZ_DATA_ERROR;
956 goto save_state_and_return;
957 }
958 nextSym = gPerm[zvec - gBase[zn]];
959
960 while (1) {
961 if (nextSym == EOB) {
962 break;
963 }
964
965 if (nextSym == BZ_RUNA || nextSym == BZ_RUNB) {
966 es = -1;
967 N = 1;
968 do {
969 if (nextSym == BZ_RUNA) {
970 es = es + (0+1) * N;
971 } else {
972 if (nextSym == BZ_RUNB) {
973 es = es + (1+1) * N;
974 }
975 }
976 N = N * 2;
977 if (! get_mtf_val_init()) {
978 goto save_state_and_return;
979 }
980 case BZ_X_MTF_3:
981 s->state = BZ_X_MTF_3;
982 if (! get_bits(s, &zvec, zn)) {
983 retVal = BZ_OK;
984 goto save_state_and_return;
985 }
986 while (1) {
987 if (zn > 20 /* the longest code */) {
988 retVal = BZ_DATA_ERROR;
989 goto save_state_and_return;
990 }
991 if (zvec <= gLimit[zn]) {
992 break;
993 }
994 zn++;
995
996 case BZ_X_MTF_4:
997 s->state = BZ_X_MTF_4;
998 if (! get_bits(s, &zj, 1)) {
999 retVal = BZ_OK;
1000 goto save_state_and_return;
1001 }
1002 zvec = (zvec << 1) | zj;
1003 }
1004 if (zvec - gBase[zn] < 0 || zvec - gBase[zn] >= BZ_MAX_ALPHA_SIZE) {
1005 retVal = BZ_DATA_ERROR;
1006 goto save_state_and_return;
1007
1008 }
1009 nextSym = gPerm[zvec - gBase[zn]];
1010 }
1011 while (nextSym == BZ_RUNA || nextSym == BZ_RUNB);
1012
1013 es++;
1014 uc = s->seqToUnseq[ s->mtfa[s->mtfbase[0]] ];
1015 s->unzftab[uc] += es;
1016
1017 while (es > 0) {
1018 if (nblock >= nblockMAX) {
1019 retVal = BZ_DATA_ERROR;
1020 goto save_state_and_return;
1021 }
1022 s->tt[nblock] = (unsigned int)uc;
1023 nblock++;
1024 es--;
1025 }
1026 continue;
1027 } else {
1028 if (nblock >= nblockMAX) {
1029 retVal = BZ_DATA_ERROR;
1030 goto save_state_and_return;
1031 }
1032 /*-- uc = MTF ( nextSym-1 ) --*/
1033 {
1034 int ii, jj, kk, pp, lno, off;
1035 unsigned int nn;
1036 nn = (unsigned int)(nextSym - 1);
1037
1038 if (nn < MTFL_SIZE) {
1039 /* avoid general-case expense */
1040 pp = s->mtfbase[0];
1041 uc = s->mtfa[pp+nn];
1042 while (nn > 3) {
1043 int z = pp+nn;
1044 s->mtfa[(z) ] = s->mtfa[(z)-1];
1045 s->mtfa[(z)-1] = s->mtfa[(z)-2];
1046 s->mtfa[(z)-2] = s->mtfa[(z)-3];
1047 s->mtfa[(z)-3] = s->mtfa[(z)-4];
1048 nn -= 4;
1049 }
1050 while (nn > 0) {
1051 s->mtfa[(pp+nn)] = s->mtfa[(pp+nn)-1]; nn--;
1052 }
1053 s->mtfa[pp] = uc;
1054 } else {
1055 /* general case */
1056 lno = nn / MTFL_SIZE;
1057 off = nn % MTFL_SIZE;
1058 pp = s->mtfbase[lno] + off;
1059 uc = s->mtfa[pp];
1060 while (pp > s->mtfbase[lno]) {
1061 s->mtfa[pp] = s->mtfa[pp-1];
1062 pp--;
1063 }
1064 s->mtfbase[lno]++;
1065 while (lno > 0) {
1066 s->mtfbase[lno]--;
1067 s->mtfa[s->mtfbase[lno]] = s->mtfa[s->mtfbase[lno-1] + MTFL_SIZE - 1];
1068 lno--;
1069 }
1070 s->mtfbase[0]--;
1071 s->mtfa[s->mtfbase[0]] = uc;
1072 if (s->mtfbase[0] == 0) {
1073 kk = MTFA_SIZE-1;
1074 for (ii = 256 / MTFL_SIZE-1; ii >= 0; ii--) {
1075 for (jj = MTFL_SIZE-1; jj >= 0; jj--) {
1076 s->mtfa[kk] = s->mtfa[s->mtfbase[ii] + jj];
1077 kk--;
1078 }
1079 s->mtfbase[ii] = kk + 1;
1080 }
1081 }
1082 }
1083 }
1084 /*-- end uc = MTF ( nextSym-1 ) --*/
1085
1086 s->unzftab[s->seqToUnseq[uc]]++;
1087 s->tt[nblock] = (unsigned int)(s->seqToUnseq[uc]);
1088 nblock++;
1089
1090 if (! get_mtf_val_init()) {
1091 goto save_state_and_return;
1092 }
1093 case BZ_X_MTF_5:
1094 s->state = BZ_X_MTF_5;
1095 if (! get_bits(s, &zvec, zn)) {
1096 retVal = BZ_OK;
1097 goto save_state_and_return;
1098 }
1099 while (1) {
1100 if (zn > 20 /* the longest code */) {
1101 retVal = BZ_DATA_ERROR;
1102 goto save_state_and_return;
1103 }
1104 if (zvec <= gLimit[zn]) {
1105 break;
1106 }
1107 zn++;
1108
1109 case BZ_X_MTF_6:
1110 s->state = BZ_X_MTF_6;
1111 if (! get_bits(s, &zj, 1)) {
1112 retVal = BZ_OK;
1113 goto save_state_and_return;
1114 }
1115 zvec = (zvec << 1) | zj;
1116 }
1117 if (zvec - gBase[zn] < 0 || zvec - gBase[zn] >= BZ_MAX_ALPHA_SIZE) {
1118 retVal = BZ_DATA_ERROR;
1119 goto save_state_and_return;
1120 }
1121 nextSym = gPerm[zvec - gBase[zn]];
1122 continue;
1123 }
1124 }
1125
1126 /* Now we know what nblock is, we can do a better sanity
1127 check on s->origPtr.
1128 */
1129 if (s->origPtr < 0 || s->origPtr >= nblock) {
1130 retVal = BZ_DATA_ERROR;
1131 goto save_state_and_return;
1132 }
1133 s->state_out_len = 0;
1134 s->state_out_ch = 0;
1135 s->calculatedBlockCRC = 0xffffffffL;
1136 s->state = BZ_X_OUTPUT;
1137
1138 /*-- Set up cftab to facilitate generation of T^(-1) --*/
1139 s->cftab[0] = 0;
1140 for (i = 1; i <= 256; i++) {
1141 s->cftab[i] = s->unzftab[i-1];
1142 }
1143 for (i = 1; i <= 256; i++) {
1144 s->cftab[i] += s->cftab[i-1];
1145 }
1146
1147 /*-- compute the T^(-1) vector --*/
1148 for (i = 0; i < nblock; i++) {
1149 uc = (unsigned char)(s->tt[i] & 0xff);
1150 s->tt[s->cftab[uc]] |= (i << 8);
1151 s->cftab[uc]++;
1152 }
1153
1154 s->tPos = s->tt[s->origPtr] >> 8;
1155 s->nblock_used = 0;
1156 if (s->blockRandomised) {
1157 s->rNToGo = 0;
1158 s->rTPos = 0;
1159 s->k0 = bz_get_fast(s);
1160
1161 s->nblock_used++;
1162 bz_rand_udp_mask(s);
1163 s->k0 ^= ((s->rNToGo == 1) ? 1 : 0);
1164 } else {
1165 s->k0 = bz_get_fast(s);
1166 s->nblock_used++;
1167 }
1168
1169 retVal = BZ_OK;
1170 goto save_state_and_return;
1171
1172endhdr_2:
1173 case BZ_X_ENDHDR_2:
1174 s->state = BZ_X_ENDHDR_2;
1175 if (! get_bits(s, &uc, 8)) {
1176 retVal = BZ_OK;
1177 goto save_state_and_return;
1178 }
1179 if (uc != 0x72) {
1180 retVal = BZ_DATA_ERROR;
1181 goto save_state_and_return;
1182 }
1183
1184 case BZ_X_ENDHDR_3:
1185 s->state = BZ_X_ENDHDR_3;
1186 if (! get_bits(s, &uc, 8)) {
1187 retVal = BZ_OK;
1188 goto save_state_and_return;
1189 }
1190 if (uc != 0x45) {
1191 retVal = BZ_DATA_ERROR;
1192 goto save_state_and_return;
1193 }
1194
1195 case BZ_X_ENDHDR_4:
1196 s->state = BZ_X_ENDHDR_4;
1197 if (! get_bits(s, &uc, 8)) {
1198 retVal = BZ_OK;
1199 goto save_state_and_return;
1200 }
1201 if (uc != 0x38) {
1202 retVal = BZ_DATA_ERROR;
1203 goto save_state_and_return;
1204 }
1205
1206 case BZ_X_ENDHDR_5:
1207 s->state = BZ_X_ENDHDR_5;
1208 if (! get_bits(s, &uc, 8)) {
1209 retVal = BZ_OK;
1210 goto save_state_and_return;
1211 }
1212 if (uc != 0x50) {
1213 retVal = BZ_DATA_ERROR;
1214 goto save_state_and_return;
1215 }
1216
1217 case BZ_X_ENDHDR_6:
1218 s->state = BZ_X_ENDHDR_6;
1219 if (! get_bits(s, &uc, 8)) {
1220 retVal = BZ_OK;
1221 goto save_state_and_return;
1222 }
1223 if (uc != 0x90) {
1224 retVal = BZ_DATA_ERROR;
1225 goto save_state_and_return;
1226 }
1227 s->storedCombinedCRC = 0;
1228
1229 case BZ_X_CCRC_1:
1230 s->state = BZ_X_CCRC_1;
1231 if (! get_bits(s, &uc, 8)) {
1232 retVal = BZ_OK;
1233 goto save_state_and_return;
1234 }
1235 s->storedCombinedCRC = (s->storedCombinedCRC << 8) | ((unsigned int)uc);
1236 case BZ_X_CCRC_2:
1237 s->state = BZ_X_CCRC_2;
1238 if (! get_bits(s, &uc, 8)) {
1239 retVal = BZ_OK;
1240 goto save_state_and_return;
1241 }
1242 s->storedCombinedCRC = (s->storedCombinedCRC << 8) | ((unsigned int)uc);
1243
1244 case BZ_X_CCRC_3:
1245 s->state = BZ_X_CCRC_3;
1246 if (! get_bits(s, &uc, 8)) {
1247 retVal = BZ_OK;
1248 goto save_state_and_return;
1249 }
1250 s->storedCombinedCRC = (s->storedCombinedCRC << 8) | ((unsigned int)uc);
1251
1252 case BZ_X_CCRC_4:
1253 s->state = BZ_X_CCRC_4;
1254 if (! get_bits(s, &uc, 8)) {
1255 retVal = BZ_OK;
1256 goto save_state_and_return;
1257 }
1258 s->storedCombinedCRC = (s->storedCombinedCRC << 8) | ((unsigned int)uc);
1259
1260 s->state = BZ_X_IDLE;
1261 retVal = BZ_STREAM_END;
1262 goto save_state_and_return;
1263
1264 }
1265
1266save_state_and_return:
1267 s->save_i = i;
1268 s->save_j = j;
1269 s->save_t = t;
1270 s->save_alphaSize = alphaSize;
1271 s->save_nGroups = nGroups;
1272 s->save_nSelectors = nSelectors;
1273 s->save_EOB = EOB;
1274 s->save_groupNo = groupNo;
1275 s->save_groupPos = groupPos;
1276 s->save_nextSym = nextSym;
1277 s->save_nblockMAX = nblockMAX;
1278 s->save_nblock = nblock;
1279 s->save_es = es;
1280 s->save_N = N;
1281 s->save_curr = curr;
1282 s->save_zt = zt;
1283 s->save_zn = zn;
1284 s->save_zvec = zvec;
1285 s->save_zj = zj;
1286 s->save_gSel = gSel;
1287 s->save_gMinlen = gMinlen;
1288 s->save_gLimit = gLimit;
1289 s->save_gBase = gBase;
1290 s->save_gPerm = gPerm;
1291
1292 return retVal;
1293}
1294
1295//int BZ2_bzDecompressInit(bz_stream* strm, int verbosity_level, int small)
1296static inline int BZ2_bzDecompressInit(bz_stream* strm)
1297{
1298 DState* s;
1299
1300// if (verbosity_level < 0 || verbosity_level > 4) {
1301// return BZ_PARAM_ERROR;
1302// }
1303 s = xmalloc(sizeof(DState));
1304 s->strm = strm;
1305 strm->state = s;
1306 s->state = BZ_X_MAGIC_1;
1307 s->bsLive = 0;
1308 s->bsBuff = 0;
1309 s->calculatedCombinedCRC = 0;
1310 s->tt = NULL;
1311 s->currBlockNo = 0;
1312
1313 return BZ_OK;
1314}
1315
1316static void bz_seterr(int eee, int *bzerror, bzFile **bzf)
1317{
1318 if (bzerror != NULL) {
1319 *bzerror = eee;
1320 }
1321 if (*bzf != NULL) {
1322 (*bzf)->lastErr = eee;
1323 }
1324}
1325
1326static void BZ2_bzReadClose(int *bzerror, void *b)
1327{
1328 bzFile* bzf = (bzFile*)b;
1329
1330 bz_seterr(BZ_OK, bzerror, &bzf);
1331
1332 if (bzf->initialisedOk) {
1333 bz_stream *strm = &(bzf->strm);
1334 DState *s;
1335 if (strm == NULL) {
1336 return;
1337 }
1338 s = strm->state;
1339 if ((s == NULL) || (s->strm != strm)) {
1340 return;
1341 }
1342 free(s->tt);
1343 free(strm->state);
1344 strm->state = NULL;
1345 return;
1346 }
1347 free(bzf);
1348}
1349
1350static void unRLE_obuf_to_output_FAST(DState *s)
1351{
1352 unsigned char k1;
1353
1354 if (s->blockRandomised) {
1355 while (1) {
1356 /* try to finish existing run */
1357 while (1) {
1358 if (s->strm->avail_out == 0) {
1359 return;
1360 }
1361 if (s->state_out_len == 0) {
1362 break;
1363 }
1364 *((unsigned char *)(s->strm->next_out)) = s->state_out_ch;
1365 s->calculatedBlockCRC = (s->calculatedBlockCRC << 8) ^
1366 BZ2_crc32Table[(s->calculatedBlockCRC >> 24) ^
1367 ((unsigned char)s->state_out_ch)];
1368 s->state_out_len--;
1369 s->strm->next_out++;
1370 s->strm->avail_out--;
1371 }
1372
1373 /* can a new run be started? */
1374 if (s->nblock_used == s->save_nblock+1) {
1375 return;
1376 }
1377 s->state_out_len = 1;
1378 s->state_out_ch = s->k0;
1379 k1 = bz_get_fast(s);
1380 bz_rand_udp_mask(s);
1381 k1 ^= ((s->rNToGo == 1) ? 1 : 0);
1382 s->nblock_used++;
1383 if (s->nblock_used == s->save_nblock+1) {
1384 continue;
1385 }
1386 if (k1 != s->k0) {
1387 s->k0 = k1;
1388 continue;
1389 }
1390
1391 s->state_out_len = 2;
1392 k1 = bz_get_fast(s);
1393 bz_rand_udp_mask(s);
1394 k1 ^= ((s->rNToGo == 1) ? 1 : 0);
1395 s->nblock_used++;
1396 if (s->nblock_used == s->save_nblock+1) {
1397 continue;
1398 }
1399 if (k1 != s->k0) {
1400 s->k0 = k1;
1401 continue;
1402 }
1403 s->state_out_len = 3;
1404 k1 = bz_get_fast(s);
1405 bz_rand_udp_mask(s);
1406 k1 ^= ((s->rNToGo == 1) ? 1 : 0);
1407 s->nblock_used++;
1408 if (s->nblock_used == s->save_nblock+1) {
1409 continue;
1410 }
1411 if (k1 != s->k0) {
1412 s->k0 = k1;
1413 continue;
1414 }
1415
1416 k1 = bz_get_fast(s);
1417 bz_rand_udp_mask(s);
1418 k1 ^= ((s->rNToGo == 1) ? 1 : 0);
1419 s->nblock_used++;
1420 s->state_out_len = ((int)k1) + 4;
1421 s->k0 = bz_get_fast(s);
1422 bz_rand_udp_mask(s);
1423 s->k0 ^= ((s->rNToGo == 1) ? 1 : 0);
1424 s->nblock_used++;
1425 }
1426 } else {
1427 /* restore */
1428 unsigned int c_calculatedBlockCRC = s->calculatedBlockCRC;
1429 unsigned char c_state_out_ch = s->state_out_ch;
1430 int c_state_out_len = s->state_out_len;
1431 int c_nblock_used = s->nblock_used;
1432 int c_k0 = s->k0;
1433 unsigned int *c_tt = s->tt;
1434 unsigned int c_tPos = s->tPos;
1435 char *cs_next_out = s->strm->next_out;
1436 unsigned int cs_avail_out = s->strm->avail_out;
1437 /* end restore */
1438
1439 int s_save_nblockPP = s->save_nblock+1;
1440
1441 while (1) {
1442 /* try to finish existing run */
1443 if (c_state_out_len > 0) {
1444 while (TRUE) {
1445 if (cs_avail_out == 0) {
1446 goto return_notr;
1447 }
1448 if (c_state_out_len == 1) {
1449 break;
1450 }
1451 *((unsigned char *)(cs_next_out)) = c_state_out_ch;
1452 c_calculatedBlockCRC = (c_calculatedBlockCRC << 8) ^
1453 BZ2_crc32Table[(c_calculatedBlockCRC >> 24) ^
1454 ((unsigned char)c_state_out_ch)];
1455 c_state_out_len--;
1456 cs_next_out++;
1457 cs_avail_out--;
1458 }
1459s_state_out_len_eq_one:
1460 {
1461 if (cs_avail_out == 0) {
1462 c_state_out_len = 1;
1463 goto return_notr;
1464 }
1465 *((unsigned char *)(cs_next_out)) = c_state_out_ch;
1466 c_calculatedBlockCRC = (c_calculatedBlockCRC << 8) ^
1467 BZ2_crc32Table[(c_calculatedBlockCRC >> 24) ^
1468 ((unsigned char)c_state_out_ch)];
1469 cs_next_out++;
1470 cs_avail_out--;
1471 }
1472 }
1473 /* can a new run be started? */
1474 if (c_nblock_used == s_save_nblockPP) {
1475 c_state_out_len = 0; goto return_notr;
1476 }
1477 c_state_out_ch = c_k0;
1478 c_tPos = c_tt[c_tPos];
1479 k1 = (unsigned char)(c_tPos & 0xff);
1480 c_tPos >>= 8;
1481
1482 c_nblock_used++;
1483
1484 if (k1 != c_k0) {
1485 c_k0 = k1;
1486 goto s_state_out_len_eq_one;
1487 }
1488
1489 if (c_nblock_used == s_save_nblockPP) {
1490 goto s_state_out_len_eq_one;
1491 }
1492
1493 c_state_out_len = 2;
1494 c_tPos = c_tt[c_tPos];
1495 k1 = (unsigned char)(c_tPos & 0xff);
1496 c_tPos >>= 8;
1497
1498 c_nblock_used++;
1499 if (c_nblock_used == s_save_nblockPP) {
1500 continue;
1501 }
1502 if (k1 != c_k0) {
1503 c_k0 = k1;
1504 continue;
1505 }
1506
1507 c_state_out_len = 3;
1508 c_tPos = c_tt[c_tPos];
1509 k1 = (unsigned char)(c_tPos & 0xff);
1510 c_tPos >>= 8;
1511
1512 c_nblock_used++;
1513 if (c_nblock_used == s_save_nblockPP) {
1514 continue;
1515 }
1516 if (k1 != c_k0) {
1517 c_k0 = k1;
1518 continue;
1519 }
1520
1521 c_tPos = c_tt[c_tPos];
1522 k1 = (unsigned char)(c_tPos & 0xff);
1523 c_tPos >>= 8;
1524
1525 c_nblock_used++;
1526 c_state_out_len = ((int)k1) + 4;
1527
1528 c_tPos = c_tt[c_tPos];
1529 c_k0 = (unsigned char)(c_tPos & 0xff);
1530 c_tPos >>= 8;
1531
1532 c_nblock_used++;
1533 }
1534
1535return_notr:
1536
1537 /* save */
1538 s->calculatedBlockCRC = c_calculatedBlockCRC;
1539 s->state_out_ch = c_state_out_ch;
1540 s->state_out_len = c_state_out_len;
1541 s->nblock_used = c_nblock_used;
1542 s->k0 = c_k0;
1543 s->tt = c_tt;
1544 s->tPos = c_tPos;
1545 s->strm->next_out = cs_next_out;
1546 s->strm->avail_out = cs_avail_out;
1547 /* end save */
1548 }
1549}
1550static inline
1551int BZ2_bzDecompress(bz_stream *strm)
1552{
1553 DState* s;
1554 s = strm->state;
1555
1556 while (1) {
1557 if (s->state == BZ_X_IDLE) {
1558 return BZ_SEQUENCE_ERROR;
1559 }
1560 if (s->state == BZ_X_OUTPUT) {
1561 unRLE_obuf_to_output_FAST(s);
1562 if (s->nblock_used == s->save_nblock+1 && s->state_out_len == 0) {
1563 s->calculatedBlockCRC = ~(s->calculatedBlockCRC);
1564 if (s->calculatedBlockCRC != s->storedBlockCRC) {
1565 return BZ_DATA_ERROR;
1566 }
1567 s->calculatedCombinedCRC = (s->calculatedCombinedCRC << 1) | (s->calculatedCombinedCRC >> 31);
1568 s->calculatedCombinedCRC ^= s->calculatedBlockCRC;
1569 s->state = BZ_X_BLKHDR_1;
1570 } else {
1571 return BZ_OK;
1572 }
1573 }
1574 if (s->state >= BZ_X_MAGIC_1) {
1575 int r = BZ2_decompress(s);
1576 if (r == BZ_STREAM_END) {
1577 if (s->calculatedCombinedCRC != s->storedCombinedCRC) {
1578 return BZ_DATA_ERROR;
1579 }
1580 return r;
1581 }
1582 if (s->state != BZ_X_OUTPUT) {
1583 return r;
1584 }
1585 }
1586 }
1587
1588 return(0); /*NOTREACHED*/
1589}
1590
1591static inline int BZ2_bzRead(int *bzerror, void *b, void *buf, int len)
1592{
1593 int n, ret;
1594 bzFile *bzf = (bzFile*)b;
1595
1596 bz_seterr(BZ_OK, bzerror, &bzf);
1597
1598 if (len == 0) {
1599 bz_seterr(BZ_OK, bzerror, &bzf);
1600 return 0;
1601 }
1602
1603 bzf->strm.avail_out = len;
1604 bzf->strm.next_out = buf;
1605
1606 while (1) {
1607 if (ferror(bzf->handle)) {
1608 bz_seterr(BZ_IO_ERROR, bzerror, &bzf);
1609 return 0;
1610 }
1611 if ((bzf->strm.avail_in == 0) && !myfeof(bzf->handle)) {
1612 n = fread(bzf->buf, sizeof(unsigned char), BZ_MAX_UNUSED, bzf->handle);
1613 if (ferror(bzf->handle)) {
1614 bz_seterr(BZ_IO_ERROR, bzerror, &bzf);
1615 return 0;
1616 }
1617 bzf->bufN = n;
1618 bzf->strm.avail_in = bzf->bufN;
1619 bzf->strm.next_in = bzf->buf;
1620 }
1621
1622 ret = BZ2_bzDecompress(&(bzf->strm));
1623
1624 if ((ret != BZ_OK) && (ret != BZ_STREAM_END)) {
1625 bz_seterr(ret, bzerror, &bzf);
1626 return 0;
1627 }
1628
1629 if ((ret == BZ_OK) && myfeof(bzf->handle) &&
1630 (bzf->strm.avail_in == 0) && (bzf->strm.avail_out > 0)) {
1631 bz_seterr(BZ_UNEXPECTED_EOF, bzerror, &bzf);
1632 return(0);
1633 }
1634
1635 if (ret == BZ_STREAM_END) {
1636 bz_seterr(BZ_STREAM_END, bzerror, &bzf);
1637 return(len - bzf->strm.avail_out);
1638 }
1639 if (bzf->strm.avail_out == 0) {
1640 bz_seterr(BZ_OK, bzerror, &bzf);
1641 return(len);
1642 }
1643 }
1644 return(0); /*not reached*/
1645}
1646
1647static inline void *BZ2_bzReadOpen(int *bzerror, FILE *f, void *unused, int nUnused)
1648{
1649 bzFile *bzf = xmalloc(sizeof(bzFile));
1650 int ret;
1651
1652 bz_seterr(BZ_OK, bzerror, &bzf);
1653
1654 bzf->initialisedOk = FALSE;
1655 bzf->handle = f;
1656 bzf->bufN = 0;
1657
1658 ret = BZ2_bzDecompressInit(&(bzf->strm));
1659 if (ret != BZ_OK) {
1660 bz_seterr(ret, bzerror, &bzf);
1661 free(bzf);
1662 return NULL;
1663 }
1664
1665 while (nUnused > 0) {
1666 bzf->buf[bzf->bufN] = *((unsigned char *)(unused)); bzf->bufN++;
1667 unused = ((void *)( 1 + ((unsigned char *)(unused)) ));
1668 nUnused--;
1669 }
1670 bzf->strm.avail_in = bzf->bufN;
1671 bzf->strm.next_in = bzf->buf;
1672
1673 bzf->initialisedOk = TRUE;
1674 return bzf;
1675}
1676
1677extern unsigned char uncompressStream(FILE *zStream, FILE *stream)
1678{
1679 unsigned char unused[BZ_MAX_UNUSED];
1680 unsigned char *unusedTmp;
1681 unsigned char obuf[5000];
1682 bzFile *bzf = NULL;
1683 int bzerr_dummy;
1684 int bzerr;
1685 int nread;
1686 int nUnused;
1687 int streamNo;
1688 int ret;
1689 int i;
1690
1691 nUnused = 0;
1692 streamNo = 0;
1693
1694 if (ferror(stream)) {
1695 goto errhandler_io;
1696 }
1697 if (ferror(zStream)) {
1698 goto errhandler_io;
1699 }
1700
1701 while(1) {
1702 bzf = BZ2_bzReadOpen(&bzerr, zStream, unused, nUnused);
1703 if (bzf == NULL || bzerr != BZ_OK) {
1704 goto errhandler;
1705 }
1706 streamNo++;
1707
1708 while (bzerr == BZ_OK) {
1709 nread = BZ2_bzRead(&bzerr, bzf, obuf, 5000);
1710 if (bzerr == BZ_DATA_ERROR_MAGIC) {
1711 goto errhandler;
1712 }
1713 if ((bzerr == BZ_OK || bzerr == BZ_STREAM_END) && nread > 0) {
1714 fwrite(obuf, sizeof(unsigned char), nread, stream);
1715 }
1716 if (ferror(stream)) {
1717 goto errhandler_io;
1718 }
1719 }
1720 if (bzerr != BZ_STREAM_END) {
1721 goto errhandler;
1722 }
1723 nUnused = bzf->strm.avail_in;
1724 unusedTmp = bzf->strm.next_in;
1725 bz_seterr(BZ_OK, &bzerr, &bzf);
1726 for (i = 0; i < nUnused; i++) {
1727 unused[i] = unusedTmp[i];
1728 }
1729 BZ2_bzReadClose(&bzerr, bzf);
1730 if ((nUnused == 0) && myfeof(zStream)) {
1731 break;
1732 }
1733 }
1734
1735 if (ferror(zStream)) {
1736 goto errhandler_io;
1737 }
1738 ret = fclose(zStream);
1739 if (ret == EOF) {
1740 goto errhandler_io;
1741 }
1742 if (ferror(stream)) {
1743 goto errhandler_io;
1744 }
1745 ret = fflush(stream);
1746 if (ret != 0) {
1747 goto errhandler_io;
1748 }
1749 if (stream != stdout) {
1750 ret = fclose(stream);
1751 if (ret == EOF) {
1752 goto errhandler_io;
1753 }
1754 }
1755 return TRUE;
1756
1757errhandler:
1758 BZ2_bzReadClose ( &bzerr_dummy, bzf );
1759 switch (bzerr) {
1760 case BZ_IO_ERROR:
1761errhandler_io:
1762 error_msg("\n%s: I/O or other error, bailing out. "
1763 "Possible reason follows.\n", applet_name);
1764 perror(applet_name);
1765 exit(1);
1766 case BZ_DATA_ERROR:
1767 error_msg("\n%s: Data integrity error when decompressing.\n", applet_name);
1768 exit(2);
1769 case BZ_UNEXPECTED_EOF:
1770 error_msg("\n%s: Compressed file ends unexpectedly;\n\t"
1771 "perhaps it is corrupted? *Possible* reason follows.\n", applet_name);
1772 perror(applet_name);
1773 exit(2);
1774 case BZ_DATA_ERROR_MAGIC:
1775 if (zStream != stdin) {
1776 fclose(zStream);
1777 }
1778 if (stream != stdout) {
1779 fclose(stream);
1780 }
1781 if (streamNo == 1) {
1782 return FALSE;
1783 } else {
1784 return TRUE;
1785 }
1786 }
1787
1788 return(TRUE); /*notreached*/
1789}