1 /* Modified for busybox by Glenn McGrath <bug1@optushome.com.au> */
2 /* Added support output to stdout by Thomas Lundquist <thomasez@zelow.no> */
4 This file is a part of bzip2 and/or libbzip2, a program and
5 library for lossless, block-sorting data compression.
7 Copyright (C) 1996-2000 Julian R Seward. All rights reserved.
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions
13 1. Redistributions of source code must retain the above copyright
14 notice, this list of conditions and the following disclaimer.
16 2. The origin of this software must not be misrepresented; you must
17 not claim that you wrote the original software. If you use this
18 software in a product, an acknowledgment in the product
19 documentation would be appreciated but is not required.
21 3. Altered source versions must be plainly marked as such, and must
22 not be misrepresented as being the original software.
24 4. The name of the author may not be used to endorse or promote
25 products derived from this software without specific prior written
28 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
29 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
30 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
32 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
34 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
36 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
37 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
38 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 Julian Seward, Cambridge, UK.
42 bzip2/libbzip2 version 1.0 of 21 March 2000
44 This program is based on (at least) the work of:
54 For more information on these sources, see the manual.
66 #define MTFA_SIZE 4096
70 #define BZ_MAX_ALPHA_SIZE 258
75 #define BZ_FINISH_OK 3
76 #define BZ_STREAM_END 4
77 #define BZ_SEQUENCE_ERROR (-1)
78 #define BZ_PARAM_ERROR (-2)
79 #define BZ_MEM_ERROR (-3)
80 #define BZ_DATA_ERROR (-4)
81 #define BZ_DATA_ERROR_MAGIC (-5)
82 #define BZ_IO_ERROR (-6)
83 #define BZ_UNEXPECTED_EOF (-7)
84 #define BZ_OUTBUFF_FULL (-8)
85 #define BZ_CONFIG_ERROR (-9)
90 #define BZ_MAX_UNUSED 5000
91 #define FILE_NAME_LEN 1034
92 /*-- states for decompression. --*/
97 #define BZ_X_MAGIC_1 10
98 #define BZ_X_MAGIC_2 11
99 #define BZ_X_MAGIC_3 12
100 #define BZ_X_MAGIC_4 13
101 #define BZ_X_BLKHDR_1 14
102 #define BZ_X_BLKHDR_2 15
103 #define BZ_X_BLKHDR_3 16
104 #define BZ_X_BLKHDR_4 17
105 #define BZ_X_BLKHDR_5 18
106 #define BZ_X_BLKHDR_6 19
107 #define BZ_X_BCRC_1 20
108 #define BZ_X_BCRC_2 21
109 #define BZ_X_BCRC_3 22
110 #define BZ_X_BCRC_4 23
111 #define BZ_X_RANDBIT 24
112 #define BZ_X_ORIGPTR_1 25
113 #define BZ_X_ORIGPTR_2 26
114 #define BZ_X_ORIGPTR_3 27
115 #define BZ_X_MAPPING_1 28
116 #define BZ_X_MAPPING_2 29
117 #define BZ_X_SELECTOR_1 30
118 #define BZ_X_SELECTOR_2 31
119 #define BZ_X_SELECTOR_3 32
120 #define BZ_X_CODING_1 33
121 #define BZ_X_CODING_2 34
122 #define BZ_X_CODING_3 35
123 #define BZ_X_MTF_1 36
124 #define BZ_X_MTF_2 37
125 #define BZ_X_MTF_3 38
126 #define BZ_X_MTF_4 39
127 #define BZ_X_MTF_5 40
128 #define BZ_X_MTF_6 41
129 #define BZ_X_ENDHDR_2 42
130 #define BZ_X_ENDHDR_3 43
131 #define BZ_X_ENDHDR_4 44
132 #define BZ_X_ENDHDR_5 45
133 #define BZ_X_ENDHDR_6 46
134 #define BZ_X_CCRC_1 47
135 #define BZ_X_CCRC_2 48
136 #define BZ_X_CCRC_3 49
137 #define BZ_X_CCRC_4 50
139 #define BZ_MAX_CODE_LEN 23
140 #define BZ_VERSION "1.0.1, 23-June-2000"
146 unsigned int avail_in;
147 unsigned int total_in_lo32;
148 unsigned int total_in_hi32;
151 unsigned int avail_out;
152 unsigned int total_out_lo32;
153 unsigned int total_out_hi32;
157 void *(*bzalloc)(void *,int,int);
158 void (*bzfree)(void *,void *);
165 unsigned char initialisedOk;
166 unsigned char writing;
167 char buf[BZ_MAX_UNUSED];
172 /*-- Structure holding all the decompression-side stuff. --*/
174 /* pointer back to the struct bz_stream */
177 /* state indicator for this stream */
180 /* for doing the final run-length decoding */
181 unsigned char state_out_ch;
183 unsigned char blockRandomised;
187 /* the buffer for bit stream reading */
191 /* misc administratium */
193 unsigned char smallDecompress;
197 /* for undoing the Burrows-Wheeler transform */
206 /* for undoing the Burrows-Wheeler transform (FAST) */
209 /* for undoing the Burrows-Wheeler transform (SMALL) */
210 unsigned short *ll16;
213 /* stored and calculated CRCs */
214 unsigned int storedBlockCRC;
215 unsigned int storedCombinedCRC;
216 unsigned int calculatedBlockCRC;
217 unsigned int calculatedCombinedCRC;
219 /* map of bytes used in block */
221 unsigned char inUse[256];
222 unsigned char inUse16[16];
223 unsigned char seqToUnseq[256];
225 /* for decoding the MTF values */
226 unsigned char mtfa [MTFA_SIZE];
227 unsigned char selector [2 + (900000 / BZ_G_SIZE)];
228 unsigned char selectorMtf[2 + (900000 / BZ_G_SIZE)];
229 unsigned char len [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
230 int mtfbase[256 / MTFL_SIZE];
232 int limit [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
233 int base [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
234 int perm [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
235 int minLens[BZ_N_GROUPS];
237 /* save area for scalars in the main decompress code */
265 //int verbosity_level;
266 unsigned char smallMode;
269 char inName[FILE_NAME_LEN];
270 char outName[FILE_NAME_LEN];
273 unsigned char deleteOutputOnInterrupt;
274 FILE *outputHandleJustInCase;
276 int numFilesProcessed;
279 const unsigned int BZ2_crc32Table[256] = {
281 /*-- Ugly, innit? --*/
283 0x00000000L, 0x04c11db7L, 0x09823b6eL, 0x0d4326d9L,
284 0x130476dcL, 0x17c56b6bL, 0x1a864db2L, 0x1e475005L,
285 0x2608edb8L, 0x22c9f00fL, 0x2f8ad6d6L, 0x2b4bcb61L,
286 0x350c9b64L, 0x31cd86d3L, 0x3c8ea00aL, 0x384fbdbdL,
287 0x4c11db70L, 0x48d0c6c7L, 0x4593e01eL, 0x4152fda9L,
288 0x5f15adacL, 0x5bd4b01bL, 0x569796c2L, 0x52568b75L,
289 0x6a1936c8L, 0x6ed82b7fL, 0x639b0da6L, 0x675a1011L,
290 0x791d4014L, 0x7ddc5da3L, 0x709f7b7aL, 0x745e66cdL,
291 0x9823b6e0L, 0x9ce2ab57L, 0x91a18d8eL, 0x95609039L,
292 0x8b27c03cL, 0x8fe6dd8bL, 0x82a5fb52L, 0x8664e6e5L,
293 0xbe2b5b58L, 0xbaea46efL, 0xb7a96036L, 0xb3687d81L,
294 0xad2f2d84L, 0xa9ee3033L, 0xa4ad16eaL, 0xa06c0b5dL,
295 0xd4326d90L, 0xd0f37027L, 0xddb056feL, 0xd9714b49L,
296 0xc7361b4cL, 0xc3f706fbL, 0xceb42022L, 0xca753d95L,
297 0xf23a8028L, 0xf6fb9d9fL, 0xfbb8bb46L, 0xff79a6f1L,
298 0xe13ef6f4L, 0xe5ffeb43L, 0xe8bccd9aL, 0xec7dd02dL,
299 0x34867077L, 0x30476dc0L, 0x3d044b19L, 0x39c556aeL,
300 0x278206abL, 0x23431b1cL, 0x2e003dc5L, 0x2ac12072L,
301 0x128e9dcfL, 0x164f8078L, 0x1b0ca6a1L, 0x1fcdbb16L,
302 0x018aeb13L, 0x054bf6a4L, 0x0808d07dL, 0x0cc9cdcaL,
303 0x7897ab07L, 0x7c56b6b0L, 0x71159069L, 0x75d48ddeL,
304 0x6b93dddbL, 0x6f52c06cL, 0x6211e6b5L, 0x66d0fb02L,
305 0x5e9f46bfL, 0x5a5e5b08L, 0x571d7dd1L, 0x53dc6066L,
306 0x4d9b3063L, 0x495a2dd4L, 0x44190b0dL, 0x40d816baL,
307 0xaca5c697L, 0xa864db20L, 0xa527fdf9L, 0xa1e6e04eL,
308 0xbfa1b04bL, 0xbb60adfcL, 0xb6238b25L, 0xb2e29692L,
309 0x8aad2b2fL, 0x8e6c3698L, 0x832f1041L, 0x87ee0df6L,
310 0x99a95df3L, 0x9d684044L, 0x902b669dL, 0x94ea7b2aL,
311 0xe0b41de7L, 0xe4750050L, 0xe9362689L, 0xedf73b3eL,
312 0xf3b06b3bL, 0xf771768cL, 0xfa325055L, 0xfef34de2L,
313 0xc6bcf05fL, 0xc27dede8L, 0xcf3ecb31L, 0xcbffd686L,
314 0xd5b88683L, 0xd1799b34L, 0xdc3abdedL, 0xd8fba05aL,
315 0x690ce0eeL, 0x6dcdfd59L, 0x608edb80L, 0x644fc637L,
316 0x7a089632L, 0x7ec98b85L, 0x738aad5cL, 0x774bb0ebL,
317 0x4f040d56L, 0x4bc510e1L, 0x46863638L, 0x42472b8fL,
318 0x5c007b8aL, 0x58c1663dL, 0x558240e4L, 0x51435d53L,
319 0x251d3b9eL, 0x21dc2629L, 0x2c9f00f0L, 0x285e1d47L,
320 0x36194d42L, 0x32d850f5L, 0x3f9b762cL, 0x3b5a6b9bL,
321 0x0315d626L, 0x07d4cb91L, 0x0a97ed48L, 0x0e56f0ffL,
322 0x1011a0faL, 0x14d0bd4dL, 0x19939b94L, 0x1d528623L,
323 0xf12f560eL, 0xf5ee4bb9L, 0xf8ad6d60L, 0xfc6c70d7L,
324 0xe22b20d2L, 0xe6ea3d65L, 0xeba91bbcL, 0xef68060bL,
325 0xd727bbb6L, 0xd3e6a601L, 0xdea580d8L, 0xda649d6fL,
326 0xc423cd6aL, 0xc0e2d0ddL, 0xcda1f604L, 0xc960ebb3L,
327 0xbd3e8d7eL, 0xb9ff90c9L, 0xb4bcb610L, 0xb07daba7L,
328 0xae3afba2L, 0xaafbe615L, 0xa7b8c0ccL, 0xa379dd7bL,
329 0x9b3660c6L, 0x9ff77d71L, 0x92b45ba8L, 0x9675461fL,
330 0x8832161aL, 0x8cf30badL, 0x81b02d74L, 0x857130c3L,
331 0x5d8a9099L, 0x594b8d2eL, 0x5408abf7L, 0x50c9b640L,
332 0x4e8ee645L, 0x4a4ffbf2L, 0x470cdd2bL, 0x43cdc09cL,
333 0x7b827d21L, 0x7f436096L, 0x7200464fL, 0x76c15bf8L,
334 0x68860bfdL, 0x6c47164aL, 0x61043093L, 0x65c52d24L,
335 0x119b4be9L, 0x155a565eL, 0x18197087L, 0x1cd86d30L,
336 0x029f3d35L, 0x065e2082L, 0x0b1d065bL, 0x0fdc1becL,
337 0x3793a651L, 0x3352bbe6L, 0x3e119d3fL, 0x3ad08088L,
338 0x2497d08dL, 0x2056cd3aL, 0x2d15ebe3L, 0x29d4f654L,
339 0xc5a92679L, 0xc1683bceL, 0xcc2b1d17L, 0xc8ea00a0L,
340 0xd6ad50a5L, 0xd26c4d12L, 0xdf2f6bcbL, 0xdbee767cL,
341 0xe3a1cbc1L, 0xe760d676L, 0xea23f0afL, 0xeee2ed18L,
342 0xf0a5bd1dL, 0xf464a0aaL, 0xf9278673L, 0xfde69bc4L,
343 0x89b8fd09L, 0x8d79e0beL, 0x803ac667L, 0x84fbdbd0L,
344 0x9abc8bd5L, 0x9e7d9662L, 0x933eb0bbL, 0x97ffad0cL,
345 0xafb010b1L, 0xab710d06L, 0xa6322bdfL, 0xa2f33668L,
346 0xbcb4666dL, 0xb8757bdaL, 0xb5365d03L, 0xb1f740b4L
349 void bz_rand_udp_mask(DState *s)
351 if (s->rNToGo == 0) {
352 s->rNToGo = BZ2_rNums[s->rTPos];
354 if (s->rTPos == 512) {
361 static unsigned char myfeof(FILE *f)
371 static void cleanUpAndFail(int ec)
375 if ((srcMode == SM_F2F) && (opMode != OM_TEST) && deleteOutputOnInterrupt) {
377 error_msg("%s: Deleting output file %s, if it exists.\n", progName, outName);
379 if (outputHandleJustInCase != NULL) {
380 fclose(outputHandleJustInCase);
382 retVal = remove(outName);
384 error_msg("%s: WARNING: deletion of output file (apparently) failed.\n", progName);
387 if (noisy && (numFileNames > 0) && (numFilesProcessed < numFileNames)) {
388 error_msg("%s: WARNING: some files have not been processed:\n"
389 "\t%d specified on command line, %d not processed yet.\n\n",
390 progName, numFileNames, numFileNames - numFilesProcessed );
399 error_msg("\n%s: PANIC -- internal consistency error:\n"
401 "\tThis is a BUG. Please report it to me at:\n"
402 "\tjseward@acm.org\n",
407 void BZ2_hbCreateDecodeTables(int *limit, int *base, int *perm, unsigned char *length, int minLen, int maxLen, int alphaSize )
412 for (i = minLen; i <= maxLen; i++) {
413 for (j = 0; j < alphaSize; j++) {
414 if (length[j] == i) {
421 for (i = 0; i < BZ_MAX_CODE_LEN; i++) {
425 for (i = 0; i < alphaSize; i++) {
429 for (i = 1; i < BZ_MAX_CODE_LEN; i++) {
430 base[i] += base[i-1];
433 for (i = 0; i < BZ_MAX_CODE_LEN; i++) {
438 for (i = minLen; i <= maxLen; i++) {
439 vec += (base[i+1] - base[i]);
443 for (i = minLen + 1; i <= maxLen; i++) {
444 base[i] = ((limit[i-1] + 1) << 1) - base[i];
448 int bz_get_small(DState *s)
455 mid = (nb + na) >> 1;
456 if (s->tPos >= s->cftab[mid]) {
462 while (na - nb != 1);
464 s->tPos = (((unsigned int)s->ll16[s->tPos]) |
465 (((((unsigned int)(s->ll4[(s->tPos) >> 1])) >>
466 (((s->tPos) << 2) & 0x4)) & 0xF) << 16));
470 void assert_h(int errcode)
472 error_msg_and_die("\n\nbzip2/libbzip2: internal error number %d.\n"
473 "This is a bug in bzip2/libbzip2, %s.\n"
474 "Please report it to me at: jseward@acm.org. If this happened\n"
475 "when you were using some program which uses libbzip2 as a\n"
476 "component, you should also report this bug to the author(s)\n"
477 "of that program. Please make an effort to report this bug;\n"
478 "timely and accurate bug reports eventually lead to higher\n"
479 "quality software. Thanks. Julian Seward, 21 March 2000.\n\n",
480 errcode, BZ_VERSION);
483 static int get_bits(DState *s, int *vvv, char nnn)
486 if (s->bsLive >= nnn) {
487 *vvv = (s->bsBuff >> (s->bsLive-nnn)) & ((1 << nnn)-1);
491 if (s->strm->avail_in == 0) {
494 s->bsBuff = (s->bsBuff << 8) | ((unsigned int) (*((unsigned char*)(s->strm->next_in))));
498 s->strm->total_in_lo32++;
499 if (s->strm->total_in_lo32 == 0) {
500 s->strm->total_in_hi32++;
506 int bz_get_fast(DState *s)
509 s->tPos = s->tt[s->tPos];
510 cccc = (unsigned char)(s->tPos & 0xff);
515 /*---------------------------------------------------*/
516 int BZ2_decompress(DState *s)
521 bz_stream *strm = s->strm;
523 /* stuff that needs to be saved/restored */
550 int get_mtf_val_init(void)
554 if (groupNo >= nSelectors) {
555 retVal = BZ_DATA_ERROR;
558 groupPos = BZ_G_SIZE;
559 gSel = s->selector[groupNo];
560 gMinlen = s->minLens[gSel];
561 gLimit = &(s->limit[gSel][0]);
562 gPerm = &(s->perm[gSel][0]);
563 gBase = &(s->base[gSel][0]);
570 if (s->state == BZ_X_MAGIC_1) {
571 /*initialise the save area*/
575 s->save_alphaSize = 0;
577 s->save_nSelectors = 0;
580 s->save_groupPos = 0;
582 s->save_nblockMAX = 0;
593 s->save_gLimit = NULL;
594 s->save_gBase = NULL;
595 s->save_gPerm = NULL;
598 /*restore from the save area*/
602 alphaSize = s->save_alphaSize;
603 nGroups = s->save_nGroups;
604 nSelectors = s->save_nSelectors;
606 groupNo = s->save_groupNo;
607 groupPos = s->save_groupPos;
608 nextSym = s->save_nextSym;
609 nblockMAX = s->save_nblockMAX;
610 nblock = s->save_nblock;
619 gMinlen = s->save_gMinlen;
620 gLimit = s->save_gLimit;
621 gBase = s->save_gBase;
622 gPerm = s->save_gPerm;
625 switch_val = s->state;
626 switch (switch_val) {
628 s->state = BZ_X_MAGIC_1;
629 if (! get_bits(s, &uc, 8)) {
631 goto save_state_and_return;
634 retVal = BZ_DATA_ERROR_MAGIC;
635 goto save_state_and_return;
639 s->state = BZ_X_MAGIC_2;
640 if (! get_bits(s, &uc, 8)) {
642 goto save_state_and_return;
645 retVal = BZ_DATA_ERROR_MAGIC;
646 goto save_state_and_return;
650 s->state = BZ_X_MAGIC_3;
651 if (! get_bits(s, &uc, 8)) {
653 goto save_state_and_return;
656 retVal = BZ_DATA_ERROR_MAGIC;
657 goto save_state_and_return;
661 s->state = BZ_X_MAGIC_4;
662 if (! get_bits(s, &s->blockSize100k, 8)) {
664 goto save_state_and_return;
666 if ((s->blockSize100k < '1') || (s->blockSize100k > '9')) {
667 retVal = BZ_DATA_ERROR_MAGIC;
668 goto save_state_and_return;
670 s->blockSize100k -= '0';
672 if (s->smallDecompress) {
673 s->ll16 = (strm->bzalloc)(strm->opaque, s->blockSize100k * 100000 * sizeof(unsigned short), 1);
674 s->ll4 = (strm->bzalloc)(strm->opaque, ((1 + s->blockSize100k * 100000) >> 1) * sizeof(unsigned char), 1);
676 if (s->ll16 == NULL || s->ll4 == NULL) {
677 retVal = BZ_MEM_ERROR;
678 goto save_state_and_return;
681 s->tt = (strm->bzalloc)(strm->opaque, s->blockSize100k * 100000 * sizeof(int), 1);
683 retVal = BZ_MEM_ERROR;
684 goto save_state_and_return;
689 s->state = BZ_X_BLKHDR_1;
690 if (! get_bits(s, &uc, 8)) {
692 goto save_state_and_return;
699 retVal = BZ_DATA_ERROR;
700 goto save_state_and_return;
704 s->state = BZ_X_BLKHDR_2;
705 if (! get_bits(s, &uc, 8)) {
707 goto save_state_and_return;
710 retVal = BZ_DATA_ERROR;
711 goto save_state_and_return;
715 s->state = BZ_X_BLKHDR_3;
716 if (! get_bits(s, &uc, 8)) {
718 goto save_state_and_return;
721 retVal = BZ_DATA_ERROR;
722 goto save_state_and_return;
726 s->state = BZ_X_BLKHDR_4;
727 if (! get_bits(s, &uc, 8)) {
729 goto save_state_and_return;
732 retVal = BZ_DATA_ERROR;
733 goto save_state_and_return;
737 s->state = BZ_X_BLKHDR_5;
738 if (! get_bits(s, &uc, 8)) {
740 goto save_state_and_return;
743 retVal = BZ_DATA_ERROR;
744 goto save_state_and_return;
748 s->state = BZ_X_BLKHDR_6;
749 if (! get_bits(s, &uc, 8)) {
751 goto save_state_and_return;
754 retVal = BZ_DATA_ERROR;
755 goto save_state_and_return;
759 if (s->verbosity >= 2) {
760 error_msg("\n [%d: huff+mtf ", s->currBlockNo);
762 s->storedBlockCRC = 0;
765 s->state = BZ_X_BCRC_1;
766 if (! get_bits(s, &uc, 8)) {
768 goto save_state_and_return;
770 s->storedBlockCRC = (s->storedBlockCRC << 8) | ((unsigned int)uc);
773 s->state = BZ_X_BCRC_2;
774 if (! get_bits(s, &uc, 8)) {
776 goto save_state_and_return;
778 s->storedBlockCRC = (s->storedBlockCRC << 8) | ((unsigned int)uc);
781 s->state = BZ_X_BCRC_3;
782 if (! get_bits(s, &uc, 8)) {
784 goto save_state_and_return;
786 s->storedBlockCRC = (s->storedBlockCRC << 8) | ((unsigned int)uc);
789 s->state = BZ_X_BCRC_4;
790 if (! get_bits(s, &uc, 8)) {
792 goto save_state_and_return;
794 s->storedBlockCRC = (s->storedBlockCRC << 8) | ((unsigned int)uc);
797 s->state = BZ_X_RANDBIT;
799 int tmp = s->blockRandomised;
800 const int ret = get_bits(s, &tmp, 1);
801 s->blockRandomised = tmp;
804 goto save_state_and_return;
811 s->state = BZ_X_ORIGPTR_1;
812 if (! get_bits(s, &uc, 8)) {
814 goto save_state_and_return;
816 s->origPtr = (s->origPtr << 8) | ((int)uc);
819 s->state = BZ_X_ORIGPTR_2;
820 if (! get_bits(s, &uc, 8)) {
822 goto save_state_and_return;
824 s->origPtr = (s->origPtr << 8) | ((int)uc);
827 s->state = BZ_X_ORIGPTR_3;
828 if (! get_bits(s, &uc, 8)) {
830 goto save_state_and_return;
832 s->origPtr = (s->origPtr << 8) | ((int)uc);
834 if (s->origPtr < 0) {
835 retVal = BZ_DATA_ERROR;
836 goto save_state_and_return;
838 if (s->origPtr > 10 + 100000*s->blockSize100k) {
839 retVal = BZ_DATA_ERROR;
840 goto save_state_and_return;
843 /*--- Receive the mapping table ---*/
845 for (i = 0; i < 16; i++) {
846 s->state = BZ_X_MAPPING_1;
847 if (! get_bits(s, &uc, 1)) {
849 goto save_state_and_return;
852 s->inUse16[i] = TRUE;
854 s->inUse16[i] = FALSE;
858 for (i = 0; i < 256; i++) {
862 for (i = 0; i < 16; i++) {
864 for (j = 0; j < 16; j++) {
866 s->state = BZ_X_MAPPING_2;
867 if (! get_bits(s, &uc, 1)) {
869 goto save_state_and_return;
872 s->inUse[i * 16 + j] = TRUE;
879 for (i = 0; i < 256; i++) {
881 s->seqToUnseq[s->nInUse] = i;
885 if (s->nInUse == 0) {
886 retVal = BZ_DATA_ERROR;
887 goto save_state_and_return;
889 alphaSize = s->nInUse+2;
891 /*--- Now the selectors ---*/
892 case BZ_X_SELECTOR_1:
893 s->state = BZ_X_SELECTOR_1;
894 if (! get_bits(s, &nGroups, 3)) {
896 goto save_state_and_return;
898 if (nGroups < 2 || nGroups > 6) {
899 retVal = BZ_DATA_ERROR;
900 goto save_state_and_return;
903 case BZ_X_SELECTOR_2:
904 s->state = BZ_X_SELECTOR_2;
905 if (! get_bits(s, &nSelectors, 15)) {
907 goto save_state_and_return;
909 if (nSelectors < 1) {
910 retVal = BZ_DATA_ERROR;
911 goto save_state_and_return;
916 for (i = 0; i < nSelectors; i++) {
919 case BZ_X_SELECTOR_3:
920 s->state = BZ_X_SELECTOR_3;
921 if (! get_bits(s, &uc, 1)) {
923 goto save_state_and_return;
930 retVal = BZ_DATA_ERROR;
931 goto save_state_and_return;
934 s->selectorMtf[i] = j;
937 /*--- Undo the MTF values for the selectors. ---*/
939 unsigned char pos[BZ_N_GROUPS], tmp, v;
940 for (v = 0; v < nGroups; v++) {
943 for (i = 0; i < nSelectors; i++) {
944 v = s->selectorMtf[i];
951 s->selector[i] = tmp;
955 /*--- Now the coding tables ---*/
956 for (t = 0; t < nGroups; t++) {
958 s->state = BZ_X_CODING_1;
959 if (! get_bits(s, &curr, 5)) {
961 goto save_state_and_return;
963 for (i = 0; i < alphaSize; i++) {
965 if (curr < 1 || curr > 20) {
966 retVal = BZ_DATA_ERROR;
967 goto save_state_and_return;
971 s->state = BZ_X_CODING_2;
972 if (! get_bits(s, &uc, 1)) {
974 goto save_state_and_return;
981 s->state = BZ_X_CODING_3;
982 if (! get_bits(s, &uc, 1)) {
984 goto save_state_and_return;
996 /*--- Create the Huffman decoding tables ---*/
997 for (t = 0; t < nGroups; t++) {
1000 for (i = 0; i < alphaSize; i++) {
1001 if (s->len[t][i] > maxLen) {
1002 maxLen = s->len[t][i];
1004 if (s->len[t][i] < minLen) {
1005 minLen = s->len[t][i];
1009 BZ2_hbCreateDecodeTables (
1014 minLen, maxLen, alphaSize
1018 s->minLens[t] = minLen;
1021 /*--- Now the MTF values ---*/
1024 nblockMAX = 100000 * s->blockSize100k;
1028 for (i = 0; i <= 255; i++) {
1035 for (ii = 256 / MTFL_SIZE - 1; ii >= 0; ii--) {
1036 for (jj = MTFL_SIZE-1; jj >= 0; jj--) {
1037 s->mtfa[kk] = (unsigned char)(ii * MTFL_SIZE + jj);
1040 s->mtfbase[ii] = kk + 1;
1043 /*-- end MTF init --*/
1047 if (! get_mtf_val_init()) {
1048 goto save_state_and_return;
1051 s->state = BZ_X_MTF_1;
1052 if (! get_bits(s, &zvec, zn)) {
1054 goto save_state_and_return;
1057 if (zn > 20 /* the longest code */) {
1058 retVal = BZ_DATA_ERROR;
1059 goto save_state_and_return;
1061 if (zvec <= gLimit[zn]) {
1067 s->state = BZ_X_MTF_2;
1068 if (! get_bits(s, &zj, 1)) {
1070 goto save_state_and_return;
1072 zvec = (zvec << 1) | zj;
1074 if (zvec - gBase[zn] < 0 || zvec - gBase[zn] >= BZ_MAX_ALPHA_SIZE) {
1075 retVal = BZ_DATA_ERROR;
1076 goto save_state_and_return;
1078 nextSym = gPerm[zvec - gBase[zn]];
1081 if (nextSym == EOB) {
1085 if (nextSym == BZ_RUNA || nextSym == BZ_RUNB) {
1089 if (nextSym == BZ_RUNA) {
1090 es = es + (0+1) * N;
1092 if (nextSym == BZ_RUNB) {
1093 es = es + (1+1) * N;
1097 if (! get_mtf_val_init()) {
1098 goto save_state_and_return;
1101 s->state = BZ_X_MTF_3;
1102 if (! get_bits(s, &zvec, zn)) {
1104 goto save_state_and_return;
1107 if (zn > 20 /* the longest code */) {
1108 retVal = BZ_DATA_ERROR;
1109 goto save_state_and_return;
1111 if (zvec <= gLimit[zn]) {
1117 s->state = BZ_X_MTF_4;
1118 if (! get_bits(s, &zj, 1)) {
1120 goto save_state_and_return;
1122 zvec = (zvec << 1) | zj;
1124 if (zvec - gBase[zn] < 0 || zvec - gBase[zn] >= BZ_MAX_ALPHA_SIZE) {
1125 retVal = BZ_DATA_ERROR;
1126 goto save_state_and_return;
1129 nextSym = gPerm[zvec - gBase[zn]];
1131 while (nextSym == BZ_RUNA || nextSym == BZ_RUNB);
1134 uc = s->seqToUnseq[ s->mtfa[s->mtfbase[0]] ];
1135 s->unzftab[uc] += es;
1137 if (s->smallDecompress) {
1139 if (nblock >= nblockMAX) {
1140 retVal = BZ_DATA_ERROR;
1141 goto save_state_and_return;
1143 s->ll16[nblock] = (unsigned short)uc;
1149 if (nblock >= nblockMAX) {
1150 retVal = BZ_DATA_ERROR;
1151 goto save_state_and_return;
1153 s->tt[nblock] = (unsigned int)uc;
1160 if (nblock >= nblockMAX) {
1161 retVal = BZ_DATA_ERROR;
1162 goto save_state_and_return;
1164 /*-- uc = MTF ( nextSym-1 ) --*/
1166 int ii, jj, kk, pp, lno, off;
1168 nn = (unsigned int)(nextSym - 1);
1170 if (nn < MTFL_SIZE) {
1171 /* avoid general-case expense */
1173 uc = s->mtfa[pp+nn];
1176 s->mtfa[(z) ] = s->mtfa[(z)-1];
1177 s->mtfa[(z)-1] = s->mtfa[(z)-2];
1178 s->mtfa[(z)-2] = s->mtfa[(z)-3];
1179 s->mtfa[(z)-3] = s->mtfa[(z)-4];
1183 s->mtfa[(pp+nn)] = s->mtfa[(pp+nn)-1]; nn--;
1188 lno = nn / MTFL_SIZE;
1189 off = nn % MTFL_SIZE;
1190 pp = s->mtfbase[lno] + off;
1192 while (pp > s->mtfbase[lno]) {
1193 s->mtfa[pp] = s->mtfa[pp-1];
1199 s->mtfa[s->mtfbase[lno]] = s->mtfa[s->mtfbase[lno-1] + MTFL_SIZE - 1];
1203 s->mtfa[s->mtfbase[0]] = uc;
1204 if (s->mtfbase[0] == 0) {
1206 for (ii = 256 / MTFL_SIZE-1; ii >= 0; ii--) {
1207 for (jj = MTFL_SIZE-1; jj >= 0; jj--) {
1208 s->mtfa[kk] = s->mtfa[s->mtfbase[ii] + jj];
1211 s->mtfbase[ii] = kk + 1;
1216 /*-- end uc = MTF ( nextSym-1 ) --*/
1218 s->unzftab[s->seqToUnseq[uc]]++;
1219 if (s->smallDecompress) {
1220 s->ll16[nblock] = (unsigned short)(s->seqToUnseq[uc]);
1222 s->tt[nblock] = (unsigned int)(s->seqToUnseq[uc]);
1226 if (! get_mtf_val_init()) {
1227 goto save_state_and_return;
1230 s->state = BZ_X_MTF_5;
1231 if (! get_bits(s, &zvec, zn)) {
1233 goto save_state_and_return;
1236 if (zn > 20 /* the longest code */) {
1237 retVal = BZ_DATA_ERROR;
1238 goto save_state_and_return;
1240 if (zvec <= gLimit[zn]) {
1246 s->state = BZ_X_MTF_6;
1247 if (! get_bits(s, &zj, 1)) {
1249 goto save_state_and_return;
1251 zvec = (zvec << 1) | zj;
1253 if (zvec - gBase[zn] < 0 || zvec - gBase[zn] >= BZ_MAX_ALPHA_SIZE) {
1254 retVal = BZ_DATA_ERROR;
1255 goto save_state_and_return;
1257 nextSym = gPerm[zvec - gBase[zn]];
1262 /* Now we know what nblock is, we can do a better sanity
1263 check on s->origPtr.
1265 if (s->origPtr < 0 || s->origPtr >= nblock) {
1266 retVal = BZ_DATA_ERROR;
1267 goto save_state_and_return;
1269 s->state_out_len = 0;
1270 s->state_out_ch = 0;
1271 s->calculatedBlockCRC = 0xffffffffL;
1272 s->state = BZ_X_OUTPUT;
1273 if (s->verbosity >= 2) {
1274 error_msg("rt+rld");
1277 /*-- Set up cftab to facilitate generation of T^(-1) --*/
1279 for (i = 1; i <= 256; i++) {
1280 s->cftab[i] = s->unzftab[i-1];
1282 for (i = 1; i <= 256; i++) {
1283 s->cftab[i] += s->cftab[i-1];
1286 if (s->smallDecompress) {
1288 /*-- Make a copy of cftab, used in generation of T --*/
1289 for (i = 0; i <= 256; i++) {
1290 s->cftabCopy[i] = s->cftab[i];
1293 /*-- compute the T vector --*/
1294 for (i = 0; i < nblock; i++) {
1295 uc = (unsigned char)(s->ll16[i]);
1296 s->ll16[i] = (unsigned short)(s->cftabCopy[uc] & 0x0000ffff);
1297 if (((i) & 0x1) == 0) {
1298 s->ll4[(i) >> 1] = (s->ll4[(i) >> 1] & 0xf0) | (s->cftabCopy[uc] >> 16);
1300 s->ll4[(i) >> 1] = (s->ll4[(i) >> 1] & 0x0f) | ((s->cftabCopy[uc] >> 16) << 4);
1305 /*-- Compute T^(-1) by pointer reversal on T --*/
1307 j = (((unsigned int)s->ll16[i]) |
1308 (((((unsigned int)(s->ll4[(i) >> 1])) >>
1309 (((i) << 2) & 0x4)) & 0xF) << 16));
1312 const int tmp = (((unsigned int)s->ll16[j]) |
1313 (((((unsigned int)(s->ll4[(j) >> 1])) >>
1314 (((j) << 2) & 0x4)) & 0xF) << 16));
1316 s->ll16[j] = (unsigned short)(i & 0x0000ffff);
1317 if (((j) & 0x1) == 0) {
1318 s->ll4[(j) >> 1] = (s->ll4[(j) >> 1] & 0xf0) | (i >> 16);
1320 s->ll4[(j) >> 1] = (s->ll4[(j) >> 1] & 0x0f) | ((i >> 16) << 4);
1325 while (i != s->origPtr);
1326 s->tPos = s->origPtr;
1328 if (s->blockRandomised) {
1331 s->k0 = bz_get_small(s);
1333 bz_rand_udp_mask(s);
1334 s->k0 ^= ((s->rNToGo == 1) ? 1 : 0);
1336 s->k0 = bz_get_small(s);
1340 /*-- compute the T^(-1) vector --*/
1341 for (i = 0; i < nblock; i++) {
1342 uc = (unsigned char)(s->tt[i] & 0xff);
1343 s->tt[s->cftab[uc]] |= (i << 8);
1347 s->tPos = s->tt[s->origPtr] >> 8;
1349 if (s->blockRandomised) {
1352 s->k0 = bz_get_fast(s);
1355 bz_rand_udp_mask(s);
1356 s->k0 ^= ((s->rNToGo == 1) ? 1 : 0);
1358 s->k0 = bz_get_fast(s);
1364 goto save_state_and_return;
1368 s->state = BZ_X_ENDHDR_2;
1369 if (! get_bits(s, &uc, 8)) {
1371 goto save_state_and_return;
1374 retVal = BZ_DATA_ERROR;
1375 goto save_state_and_return;
1379 s->state = BZ_X_ENDHDR_3;
1380 if (! get_bits(s, &uc, 8)) {
1382 goto save_state_and_return;
1385 retVal = BZ_DATA_ERROR;
1386 goto save_state_and_return;
1390 s->state = BZ_X_ENDHDR_4;
1391 if (! get_bits(s, &uc, 8)) {
1393 goto save_state_and_return;
1396 retVal = BZ_DATA_ERROR;
1397 goto save_state_and_return;
1401 s->state = BZ_X_ENDHDR_5;
1402 if (! get_bits(s, &uc, 8)) {
1404 goto save_state_and_return;
1407 retVal = BZ_DATA_ERROR;
1408 goto save_state_and_return;
1412 s->state = BZ_X_ENDHDR_6;
1413 if (! get_bits(s, &uc, 8)) {
1415 goto save_state_and_return;
1418 retVal = BZ_DATA_ERROR;
1419 goto save_state_and_return;
1421 s->storedCombinedCRC = 0;
1424 s->state = BZ_X_CCRC_1;
1425 if (! get_bits(s, &uc, 8)) {
1427 goto save_state_and_return;
1429 s->storedCombinedCRC = (s->storedCombinedCRC << 8) | ((unsigned int)uc);
1431 s->state = BZ_X_CCRC_2;
1432 if (! get_bits(s, &uc, 8)) {
1434 goto save_state_and_return;
1436 s->storedCombinedCRC = (s->storedCombinedCRC << 8) | ((unsigned int)uc);
1439 s->state = BZ_X_CCRC_3;
1440 if (! get_bits(s, &uc, 8)) {
1442 goto save_state_and_return;
1444 s->storedCombinedCRC = (s->storedCombinedCRC << 8) | ((unsigned int)uc);
1447 s->state = BZ_X_CCRC_4;
1448 if (! get_bits(s, &uc, 8)) {
1450 goto save_state_and_return;
1452 s->storedCombinedCRC = (s->storedCombinedCRC << 8) | ((unsigned int)uc);
1454 s->state = BZ_X_IDLE;
1455 retVal = BZ_STREAM_END;
1456 goto save_state_and_return;
1459 printf("switch val is %d\n", switch_val);
1465 save_state_and_return:
1469 s->save_alphaSize = alphaSize;
1470 s->save_nGroups = nGroups;
1471 s->save_nSelectors = nSelectors;
1473 s->save_groupNo = groupNo;
1474 s->save_groupPos = groupPos;
1475 s->save_nextSym = nextSym;
1476 s->save_nblockMAX = nblockMAX;
1477 s->save_nblock = nblock;
1480 s->save_curr = curr;
1483 s->save_zvec = zvec;
1485 s->save_gSel = gSel;
1486 s->save_gMinlen = gMinlen;
1487 s->save_gLimit = gLimit;
1488 s->save_gBase = gBase;
1489 s->save_gPerm = gPerm;
1494 static void *default_bzalloc(void *opaque, int items, int size)
1496 void *v = xmalloc(items *size);
1500 static void default_bzfree(void *opaque, void *addr)
1507 //int BZ2_bzDecompressInit(bz_stream* strm, int verbosity_level, int small)
1508 int BZ2_bzDecompressInit(bz_stream* strm, int small)
1512 if (sizeof(int) != 4) {
1513 return BZ_CONFIG_ERROR;
1515 if (sizeof(short) != 2) {
1516 return BZ_CONFIG_ERROR;
1518 if (sizeof(char) != 1) {
1519 return BZ_CONFIG_ERROR;
1522 return BZ_PARAM_ERROR;
1524 if (small != 0 && small != 1) {
1525 return BZ_PARAM_ERROR;
1527 // if (verbosity_level < 0 || verbosity_level > 4) {
1528 // return BZ_PARAM_ERROR;
1530 if (strm->bzalloc == NULL) {
1531 strm->bzalloc = default_bzalloc;
1533 if (strm->bzfree == NULL) {
1534 strm->bzfree = default_bzfree;
1536 s = (strm->bzalloc)(strm->opaque, sizeof(DState), 1);
1538 return BZ_MEM_ERROR;
1542 s->state = BZ_X_MAGIC_1;
1545 s->calculatedCombinedCRC = 0;
1546 strm->total_in_lo32 = 0;
1547 strm->total_in_hi32 = 0;
1548 strm->total_out_lo32 = 0;
1549 strm->total_out_hi32 = 0;
1550 s->smallDecompress = (unsigned char)small;
1555 // s->verbosity = verbosity_level;
1560 void bz_seterr(int eee, int *bzerror, bzFile **bzf)
1562 if (bzerror != NULL) {
1566 (*bzf)->lastErr = eee;
1570 void BZ2_bzReadClose(int *bzerror, void *b)
1572 bzFile* bzf = (bzFile*)b;
1574 bz_seterr(BZ_OK, bzerror, &bzf);
1576 bz_seterr(BZ_OK, bzerror, &bzf);
1581 bz_seterr(BZ_SEQUENCE_ERROR, bzerror, &bzf);
1585 if (bzf->initialisedOk) {
1586 bz_stream *strm = &(bzf->strm);
1592 if ((s == NULL) || (s->strm != strm)) {
1595 if (s->tt != NULL) {
1596 (strm->bzfree)(strm->opaque,(s->tt));
1598 if (s->ll16 != NULL) {
1599 (strm->bzfree)(strm->opaque,(s->ll16));
1601 if (s->ll4 != NULL) {
1602 (strm->bzfree)(strm->opaque,(s->ll4));
1604 (strm->bzfree)(strm->opaque,(strm->state));
1611 static void unRLE_obuf_to_output_FAST(DState *s)
1615 if (s->blockRandomised) {
1617 /* try to finish existing run */
1619 if (s->strm->avail_out == 0) {
1622 if (s->state_out_len == 0) {
1625 *((unsigned char *)(s->strm->next_out)) = s->state_out_ch;
1626 s->calculatedBlockCRC = (s->calculatedBlockCRC << 8) ^
1627 BZ2_crc32Table[(s->calculatedBlockCRC >> 24) ^
1628 ((unsigned char)s->state_out_ch)];
1630 s->strm->next_out++;
1631 s->strm->avail_out--;
1632 s->strm->total_out_lo32++;
1633 if (s->strm->total_out_lo32 == 0) {
1634 s->strm->total_out_hi32++;
1638 /* can a new run be started? */
1639 if (s->nblock_used == s->save_nblock+1) {
1642 s->state_out_len = 1;
1643 s->state_out_ch = s->k0;
1644 k1 = bz_get_fast(s);
1645 bz_rand_udp_mask(s);
1646 k1 ^= ((s->rNToGo == 1) ? 1 : 0);
1648 if (s->nblock_used == s->save_nblock+1) {
1656 s->state_out_len = 2;
1657 k1 = bz_get_fast(s);
1658 bz_rand_udp_mask(s);
1659 k1 ^= ((s->rNToGo == 1) ? 1 : 0);
1661 if (s->nblock_used == s->save_nblock+1) {
1668 s->state_out_len = 3;
1669 k1 = bz_get_fast(s);
1670 bz_rand_udp_mask(s);
1671 k1 ^= ((s->rNToGo == 1) ? 1 : 0);
1673 if (s->nblock_used == s->save_nblock+1) {
1681 k1 = bz_get_fast(s);
1682 bz_rand_udp_mask(s);
1683 k1 ^= ((s->rNToGo == 1) ? 1 : 0);
1685 s->state_out_len = ((int)k1) + 4;
1686 s->k0 = bz_get_fast(s);
1687 bz_rand_udp_mask(s);
1688 s->k0 ^= ((s->rNToGo == 1) ? 1 : 0);
1693 unsigned int c_calculatedBlockCRC = s->calculatedBlockCRC;
1694 unsigned char c_state_out_ch = s->state_out_ch;
1695 int c_state_out_len = s->state_out_len;
1696 int c_nblock_used = s->nblock_used;
1698 unsigned int *c_tt = s->tt;
1699 unsigned int c_tPos = s->tPos;
1700 char *cs_next_out = s->strm->next_out;
1701 unsigned int cs_avail_out = s->strm->avail_out;
1704 unsigned int avail_out_INIT = cs_avail_out;
1705 int s_save_nblockPP = s->save_nblock+1;
1706 unsigned int total_out_lo32_old;
1709 /* try to finish existing run */
1710 if (c_state_out_len > 0) {
1712 if (cs_avail_out == 0) {
1715 if (c_state_out_len == 1) {
1718 *((unsigned char *)(cs_next_out)) = c_state_out_ch;
1719 c_calculatedBlockCRC = (c_calculatedBlockCRC << 8) ^
1720 BZ2_crc32Table[(c_calculatedBlockCRC >> 24) ^
1721 ((unsigned char)c_state_out_ch)];
1726 s_state_out_len_eq_one:
1728 if (cs_avail_out == 0) {
1729 c_state_out_len = 1;
1732 *((unsigned char *)(cs_next_out)) = c_state_out_ch;
1733 c_calculatedBlockCRC = (c_calculatedBlockCRC << 8) ^
1734 BZ2_crc32Table[(c_calculatedBlockCRC >> 24) ^
1735 ((unsigned char)c_state_out_ch)];
1740 /* can a new run be started? */
1741 if (c_nblock_used == s_save_nblockPP) {
1742 c_state_out_len = 0; goto return_notr;
1744 c_state_out_ch = c_k0;
1745 c_tPos = c_tt[c_tPos];
1746 k1 = (unsigned char)(c_tPos & 0xff);
1753 goto s_state_out_len_eq_one;
1756 if (c_nblock_used == s_save_nblockPP) {
1757 goto s_state_out_len_eq_one;
1760 c_state_out_len = 2;
1761 c_tPos = c_tt[c_tPos];
1762 k1 = (unsigned char)(c_tPos & 0xff);
1766 if (c_nblock_used == s_save_nblockPP) {
1774 c_state_out_len = 3;
1775 c_tPos = c_tt[c_tPos];
1776 k1 = (unsigned char)(c_tPos & 0xff);
1780 if (c_nblock_used == s_save_nblockPP) {
1788 c_tPos = c_tt[c_tPos];
1789 k1 = (unsigned char)(c_tPos & 0xff);
1793 c_state_out_len = ((int)k1) + 4;
1795 c_tPos = c_tt[c_tPos];
1796 c_k0 = (unsigned char)(c_tPos & 0xff);
1803 total_out_lo32_old = s->strm->total_out_lo32;
1804 s->strm->total_out_lo32 += (avail_out_INIT - cs_avail_out);
1805 if (s->strm->total_out_lo32 < total_out_lo32_old) {
1806 s->strm->total_out_hi32++;
1810 s->calculatedBlockCRC = c_calculatedBlockCRC;
1811 s->state_out_ch = c_state_out_ch;
1812 s->state_out_len = c_state_out_len;
1813 s->nblock_used = c_nblock_used;
1817 s->strm->next_out = cs_next_out;
1818 s->strm->avail_out = cs_avail_out;
1823 static void unRLE_obuf_to_output_SMALL(DState *s)
1827 if (s->blockRandomised) {
1829 /* try to finish existing run */
1831 if (s->strm->avail_out == 0) {
1834 if (s->state_out_len == 0) {
1837 *((unsigned char *)(s->strm->next_out)) = s->state_out_ch;
1838 s->calculatedBlockCRC = (s->calculatedBlockCRC << 8) ^
1839 BZ2_crc32Table[(s->calculatedBlockCRC >> 24) ^
1840 ((unsigned char)s->state_out_ch)];
1842 s->strm->next_out++;
1843 s->strm->avail_out--;
1844 s->strm->total_out_lo32++;
1845 if (s->strm->total_out_lo32 == 0) {
1846 s->strm->total_out_hi32++;
1850 /* can a new run be started? */
1851 if (s->nblock_used == s->save_nblock+1) {
1855 s->state_out_len = 1;
1856 s->state_out_ch = s->k0;
1857 k1 = bz_get_small(s);
1858 bz_rand_udp_mask(s);
1859 k1 ^= ((s->rNToGo == 1) ? 1 : 0);
1861 if (s->nblock_used == s->save_nblock+1) {
1869 s->state_out_len = 2;
1870 k1 = bz_get_small(s);
1871 bz_rand_udp_mask(s);
1872 k1 ^= ((s->rNToGo == 1) ? 1 : 0);
1874 if (s->nblock_used == s->save_nblock+1) {
1882 s->state_out_len = 3;
1883 k1 = bz_get_small(s);
1884 bz_rand_udp_mask(s);
1885 k1 ^= ((s->rNToGo == 1) ? 1 : 0);
1887 if (s->nblock_used == s->save_nblock+1) {
1894 k1 = bz_get_small(s);
1895 bz_rand_udp_mask(s);
1896 k1 ^= ((s->rNToGo == 1) ? 1 : 0);
1898 s->state_out_len = ((int)k1) + 4;
1899 s->k0 = bz_get_small(s);
1900 bz_rand_udp_mask(s);
1901 s->k0 ^= ((s->rNToGo == 1) ? 1 : 0);
1906 /* try to finish existing run */
1908 if (s->strm->avail_out == 0) {
1911 if (s->state_out_len == 0) {
1914 *((unsigned char *)(s->strm->next_out)) = s->state_out_ch;
1915 s->calculatedBlockCRC = (s->calculatedBlockCRC << 8) ^
1916 BZ2_crc32Table[(s->calculatedBlockCRC >> 24) ^
1917 ((unsigned char)s->state_out_ch)];
1919 s->strm->next_out++;
1920 s->strm->avail_out--;
1921 s->strm->total_out_lo32++;
1922 if (s->strm->total_out_lo32 == 0) {
1923 s->strm->total_out_hi32++;
1927 /* can a new run be started? */
1928 if (s->nblock_used == s->save_nblock+1) {
1932 s->state_out_len = 1;
1933 s->state_out_ch = s->k0;
1934 k1 = bz_get_small(s);
1936 if (s->nblock_used == s->save_nblock+1) {
1944 s->state_out_len = 2;
1945 k1 = bz_get_small(s);
1947 if (s->nblock_used == s->save_nblock+1) {
1955 s->state_out_len = 3;
1956 k1 = bz_get_small(s);
1958 if (s->nblock_used == s->save_nblock+1) {
1966 k1 = bz_get_small(s);
1968 s->state_out_len = ((int)k1) + 4;
1969 s->k0 = bz_get_small(s);
1975 int BZ2_bzDecompress(bz_stream *strm)
1979 return BZ_PARAM_ERROR;
1983 return BZ_PARAM_ERROR;
1985 if (s->strm != strm) {
1986 return BZ_PARAM_ERROR;
1990 if (s->state == BZ_X_IDLE) {
1991 return BZ_SEQUENCE_ERROR;
1993 if (s->state == BZ_X_OUTPUT) {
1994 if (s->smallDecompress) {
1995 unRLE_obuf_to_output_SMALL(s);
1997 unRLE_obuf_to_output_FAST(s);
1999 if (s->nblock_used == s->save_nblock+1 && s->state_out_len == 0) {
2000 s->calculatedBlockCRC = ~(s->calculatedBlockCRC);
2001 if (s->verbosity >= 3) {
2002 error_msg("{0x%x, 0x%x}", s->storedBlockCRC, s->calculatedBlockCRC);
2004 if (s->verbosity >= 2) {
2007 if (s->calculatedBlockCRC != s->storedBlockCRC) {
2008 return BZ_DATA_ERROR;
2010 s->calculatedCombinedCRC = (s->calculatedCombinedCRC << 1) | (s->calculatedCombinedCRC >> 31);
2011 s->calculatedCombinedCRC ^= s->calculatedBlockCRC;
2012 s->state = BZ_X_BLKHDR_1;
2017 if (s->state >= BZ_X_MAGIC_1) {
2018 int r = BZ2_decompress(s);
2019 if (r == BZ_STREAM_END) {
2020 if (s->verbosity >= 3) {
2021 error_msg("\n combined CRCs: stored = 0x%x, computed = 0x%x",
2022 s->storedCombinedCRC, s->calculatedCombinedCRC );
2024 if (s->calculatedCombinedCRC != s->storedCombinedCRC) {
2025 return BZ_DATA_ERROR;
2029 if (s->state != BZ_X_OUTPUT) {
2037 return(0); /*NOTREACHED*/
2040 int BZ2_bzRead(int *bzerror, void *b, void *buf, int len)
2043 bzFile *bzf = (bzFile*)b;
2045 bz_seterr(BZ_OK, bzerror, &bzf);
2047 if (bzf == NULL || buf == NULL || len < 0) {
2048 bz_seterr(BZ_PARAM_ERROR, bzerror, &bzf);
2053 bz_seterr(BZ_SEQUENCE_ERROR, bzerror, &bzf);
2058 bz_seterr(BZ_OK, bzerror, &bzf);
2062 bzf->strm.avail_out = len;
2063 bzf->strm.next_out = buf;
2066 if (ferror(bzf->handle)) {
2067 bz_seterr(BZ_IO_ERROR, bzerror, &bzf);
2070 if ((bzf->strm.avail_in == 0) && !myfeof(bzf->handle)) {
2071 n = fread(bzf->buf, sizeof(unsigned char), BZ_MAX_UNUSED, bzf->handle);
2072 if (ferror(bzf->handle)) {
2073 bz_seterr(BZ_IO_ERROR, bzerror, &bzf);
2077 bzf->strm.avail_in = bzf->bufN;
2078 bzf->strm.next_in = bzf->buf;
2081 ret = BZ2_bzDecompress(&(bzf->strm));
2083 if ((ret != BZ_OK) && (ret != BZ_STREAM_END)) {
2084 bz_seterr(ret, bzerror, &bzf);
2088 if ((ret == BZ_OK) && myfeof(bzf->handle) &&
2089 (bzf->strm.avail_in == 0) && (bzf->strm.avail_out > 0)) {
2090 bz_seterr(BZ_UNEXPECTED_EOF, bzerror, &bzf);
2094 if (ret == BZ_STREAM_END) {
2095 bz_seterr(BZ_STREAM_END, bzerror, &bzf);
2096 return(len - bzf->strm.avail_out);
2098 if (bzf->strm.avail_out == 0) {
2099 bz_seterr(BZ_OK, bzerror, &bzf);
2103 return(0); /*not reached*/
2106 void BZ2_bzReadGetUnused(int *bzerror, void *b, void **unused, int *nUnused)
2108 bzFile *bzf = (bzFile*)b;
2110 bz_seterr(BZ_PARAM_ERROR, bzerror, &bzf);
2113 if (bzf->lastErr != BZ_STREAM_END) {
2114 bz_seterr(BZ_SEQUENCE_ERROR, bzerror, &bzf);
2117 if (unused == NULL || nUnused == NULL) {
2118 bz_seterr(BZ_PARAM_ERROR, bzerror, &bzf);
2122 bz_seterr(BZ_OK, bzerror, &bzf);
2123 *nUnused = bzf->strm.avail_in;
2124 *unused = bzf->strm.next_in;
2127 void *BZ2_bzReadOpen(int *bzerror, FILE *f, int small, void *unused, int nUnused)
2132 bz_seterr(BZ_OK, bzerror, &bzf);
2134 if (f == NULL || (small != 0 && small != 1) ||
2135 (unused != NULL && (nUnused < 0 || nUnused > BZ_MAX_UNUSED)) ||
2136 // (verbosity_level < 0 || verbosity_level > 4) ||
2137 (unused == NULL && nUnused != 0)) {
2138 bz_seterr(BZ_PARAM_ERROR, bzerror, &bzf);
2143 bz_seterr(BZ_IO_ERROR, bzerror, &bzf);
2147 bzf = xmalloc(sizeof(bzFile));
2149 bz_seterr(BZ_MEM_ERROR, bzerror, &bzf);
2152 bz_seterr(BZ_OK, bzerror, &bzf);
2154 bzf->initialisedOk = FALSE;
2157 bzf->writing = FALSE;
2158 bzf->strm.bzalloc = NULL;
2159 bzf->strm.bzfree = NULL;
2160 bzf->strm.opaque = NULL;
2162 while (nUnused > 0) {
2163 bzf->buf[bzf->bufN] = *((unsigned char *)(unused)); bzf->bufN++;
2164 unused = ((void *)( 1 + ((unsigned char *)(unused)) ));
2168 ret = BZ2_bzDecompressInit(&(bzf->strm), small);
2170 bz_seterr(ret, bzerror, &bzf);
2175 bzf->strm.avail_in = bzf->bufN;
2176 bzf->strm.next_in = bzf->buf;
2178 bzf->initialisedOk = TRUE;
2182 static unsigned char uncompressStream(FILE *zStream, FILE *stream)
2184 unsigned char unused[BZ_MAX_UNUSED];
2185 unsigned char *unusedTmp;
2186 unsigned char obuf[5000];
2199 if (ferror(stream)) {
2202 if (ferror(zStream)) {
2207 bzf = BZ2_bzReadOpen(&bzerr, zStream, (int)smallMode, unused, nUnused);
2208 if (bzf == NULL || bzerr != BZ_OK) {
2213 while (bzerr == BZ_OK) {
2214 nread = BZ2_bzRead(&bzerr, bzf, obuf, 5000);
2215 if (bzerr == BZ_DATA_ERROR_MAGIC) {
2218 if ((bzerr == BZ_OK || bzerr == BZ_STREAM_END) && nread > 0) {
2219 fwrite(obuf, sizeof(unsigned char), nread, stream);
2221 if (ferror(stream)) {
2225 if (bzerr != BZ_STREAM_END) {
2228 BZ2_bzReadGetUnused(&bzerr, bzf, (void **)(&unusedTmp), &nUnused);
2229 if (bzerr != BZ_OK) {
2230 panic("decompress:bzReadGetUnused");
2232 for (i = 0; i < nUnused; i++) {
2233 unused[i] = unusedTmp[i];
2235 BZ2_bzReadClose(&bzerr, bzf);
2236 if (bzerr != BZ_OK) {
2237 panic("decompress:bzReadGetUnused");
2239 if ((nUnused == 0) && myfeof(zStream)) {
2244 if (ferror(zStream)) {
2247 ret = fclose(zStream);
2251 if (ferror(stream)) {
2254 ret = fflush(stream);
2258 if (stream != stdout) {
2259 ret = fclose(stream);
2264 // if (verbosity_level >= 2) {
2265 // fprintf(stderr,"\n ");
2270 BZ2_bzReadClose ( &bzerr_dummy, bzf );
2272 case BZ_CONFIG_ERROR:
2273 error_msg("bzip2: I'm not configured correctly for this platform!\n"
2274 "\tI require Int32, Int16 and Char to have sizes\n"
2275 "\tof 4, 2 and 1 bytes to run properly, and they don't.\n"
2276 "\tProbably you can fix this by defining them correctly,\n"
2277 "\tand recompiling. Bye!\n" );
2281 error_msg("\n%s: I/O or other error, bailing out. "
2282 "Possible reason follows.\n", progName);
2286 error_msg("\n%s: Data integrity error when decompressing.\n", progName);
2289 error_msg("\n%s: couldn't allocate enough memory\n", progName);
2291 case BZ_UNEXPECTED_EOF:
2292 error_msg("\n%s: Compressed file ends unexpectedly;\n\t"
2293 "perhaps it is corrupted? *Possible* reason follows.\n", progName);
2296 case BZ_DATA_ERROR_MAGIC:
2297 if (zStream != stdin) {
2300 if (stream != stdout) {
2303 if (streamNo == 1) {
2307 error_msg("\n%s: %s: trailing garbage after EOF ignored\n", progName, inName );
2312 panic ( "decompress:unexpected error" );
2315 panic("decompress:end");
2316 return(TRUE); /*notreached*/
2319 int bunzip2_main(int argc, char **argv)
2321 const int bunzip_to_stdout = 1;
2328 char *save_name_ptr;
2330 /* if called as bzcat */
2331 if (strcmp(applet_name, "bzcat") == 0)
2332 flags |= bunzip_to_stdout;
2334 while ((opt = getopt(argc, argv, "ch")) != -1) {
2337 flags |= bunzip_to_stdout;
2341 show_usage(); /* exit's inside usage */
2345 save_name = xstrdup(argv[optind]);
2347 if (save_name == NULL) {
2351 src_stream = xfopen(argv[optind], "r");
2353 save_name_ptr = strrchr(save_name, '.');
2354 if (save_name_ptr == NULL) {
2357 if (strcmp(save_name_ptr, ".bz2") != 0) {
2358 error_msg("Invalid extension, expected .bz2");
2360 *save_name_ptr = '\0';
2362 if (flags & bunzip_to_stdout) {
2363 dst_stream = stdout;
2365 dst_stream = xfopen(save_name, "w");
2367 uncompressStream(src_stream, dst_stream);