6 /*-------------------------------------------------------------*/
7 /*--- Decompression machinery ---*/
8 /*--- decompress.c ---*/
9 /*-------------------------------------------------------------*/
12 This file is a part of bzip2 and/or libbzip2, a program and
13 library for lossless, block-sorting data compression.
15 Copyright (C) 1996-2002 Julian R Seward. All rights reserved.
17 Redistribution and use in source and binary forms, with or without
18 modification, are permitted provided that the following conditions
21 1. Redistributions of source code must retain the above copyright
22 notice, this list of conditions and the following disclaimer.
24 2. The origin of this software must not be misrepresented; you must
25 not claim that you wrote the original software. If you use this
26 software in a product, an acknowledgment in the product
27 documentation would be appreciated but is not required.
29 3. Altered source versions must be plainly marked as such, and must
30 not be misrepresented as being the original software.
32 4. The name of the author may not be used to endorse or promote
33 products derived from this software without specific prior written
36 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
37 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
38 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
39 ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
40 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
41 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
42 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
43 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
44 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
45 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
46 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48 Julian Seward, Cambridge, UK.
50 bzip2/libbzip2 version 1.0 of 21 March 2000
52 This program is based on (at least) the work of:
62 For more information on these sources, see the manual.
66 #include "bzlib_private.h"
69 /*---------------------------------------------------*/
71 void makeMaps_d ( DState* s )
75 for (i = 0; i < 256; i++)
77 s->seqToUnseq[s->nInUse] = i;
83 /*---------------------------------------------------*/
85 { retVal = rrr; goto save_state_and_return; };
87 #define GET_BITS(lll,vvv,nnn) \
88 case lll: s->state = lll; \
90 if (s->bsLive >= nnn) { \
93 (s->bsLive-nnn)) & ((1 << nnn)-1); \
98 if (s->strm->avail_in == 0) RETURN(BZ_OK); \
100 = (s->bsBuff << 8) | \
102 (*((UChar*)(s->strm->next_in)))); \
104 s->strm->next_in++; \
105 s->strm->avail_in--; \
106 s->strm->total_in_lo32++; \
107 if (s->strm->total_in_lo32 == 0) \
108 s->strm->total_in_hi32++; \
111 #define GET_UCHAR(lll,uuu) \
114 #define GET_BIT(lll,uuu) \
117 /*---------------------------------------------------*/
118 #define GET_MTF_VAL(label1,label2,lval) \
120 if (groupPos == 0) { \
122 if (groupNo >= nSelectors) \
123 RETURN(BZ_DATA_ERROR); \
124 groupPos = BZ_G_SIZE; \
125 gSel = s->selector[groupNo]; \
126 gMinlen = s->minLens[gSel]; \
127 gLimit = &(s->limit[gSel][0]); \
128 gPerm = &(s->perm[gSel][0]); \
129 gBase = &(s->base[gSel][0]); \
133 GET_BITS(label1, zvec, zn); \
135 if (zn > 20 /* the longest code */) \
136 RETURN(BZ_DATA_ERROR); \
137 if (zvec <= gLimit[zn]) break; \
139 GET_BIT(label2, zj); \
140 zvec = (zvec << 1) | zj; \
142 if (zvec - gBase[zn] < 0 \
143 || zvec - gBase[zn] >= BZ_MAX_ALPHA_SIZE) \
144 RETURN(BZ_DATA_ERROR); \
145 lval = gPerm[zvec - gBase[zn]]; \
149 /*---------------------------------------------------*/
150 Int32 BZ2_decompress ( DState* s )
154 Int32 minLen, maxLen;
155 bz_stream* strm = s->strm;
157 /* stuff that needs to be saved/restored */
183 if (s->state == BZ_X_MAGIC_1) {
184 /*initialise the save area*/
188 s->save_alphaSize = 0;
190 s->save_nSelectors = 0;
193 s->save_groupPos = 0;
195 s->save_nblockMAX = 0;
206 s->save_gLimit = NULL;
207 s->save_gBase = NULL;
208 s->save_gPerm = NULL;
211 /*restore from the save area*/
215 alphaSize = s->save_alphaSize;
216 nGroups = s->save_nGroups;
217 nSelectors = s->save_nSelectors;
219 groupNo = s->save_groupNo;
220 groupPos = s->save_groupPos;
221 nextSym = s->save_nextSym;
222 nblockMAX = s->save_nblockMAX;
223 nblock = s->save_nblock;
232 gMinlen = s->save_gMinlen;
233 gLimit = s->save_gLimit;
234 gBase = s->save_gBase;
235 gPerm = s->save_gPerm;
241 GET_UCHAR(BZ_X_MAGIC_1, uc);
242 if (uc != BZ_HDR_B) RETURN(BZ_DATA_ERROR_MAGIC);
244 GET_UCHAR(BZ_X_MAGIC_2, uc);
245 if (uc != BZ_HDR_Z) RETURN(BZ_DATA_ERROR_MAGIC);
247 GET_UCHAR(BZ_X_MAGIC_3, uc)
248 if (uc != BZ_HDR_h) RETURN(BZ_DATA_ERROR_MAGIC);
250 GET_BITS(BZ_X_MAGIC_4, s->blockSize100k, 8)
251 if (s->blockSize100k < (BZ_HDR_0 + 1) ||
252 s->blockSize100k > (BZ_HDR_0 + 9)) RETURN(BZ_DATA_ERROR_MAGIC);
253 s->blockSize100k -= BZ_HDR_0;
255 if (s->smallDecompress) {
256 s->ll16 = BZALLOC( s->blockSize100k * 100000 * sizeof(UInt16) );
258 ((1 + s->blockSize100k * 100000) >> 1) * sizeof(UChar)
260 if (s->ll16 == NULL || s->ll4 == NULL) RETURN(BZ_MEM_ERROR);
262 s->tt = BZALLOC( s->blockSize100k * 100000 * sizeof(Int32) );
263 if (s->tt == NULL) RETURN(BZ_MEM_ERROR);
266 GET_UCHAR(BZ_X_BLKHDR_1, uc);
268 if (uc == 0x17) goto endhdr_2;
269 if (uc != 0x31) RETURN(BZ_DATA_ERROR);
270 GET_UCHAR(BZ_X_BLKHDR_2, uc);
271 if (uc != 0x41) RETURN(BZ_DATA_ERROR);
272 GET_UCHAR(BZ_X_BLKHDR_3, uc);
273 if (uc != 0x59) RETURN(BZ_DATA_ERROR);
274 GET_UCHAR(BZ_X_BLKHDR_4, uc);
275 if (uc != 0x26) RETURN(BZ_DATA_ERROR);
276 GET_UCHAR(BZ_X_BLKHDR_5, uc);
277 if (uc != 0x53) RETURN(BZ_DATA_ERROR);
278 GET_UCHAR(BZ_X_BLKHDR_6, uc);
279 if (uc != 0x59) RETURN(BZ_DATA_ERROR);
282 if (s->verbosity >= 2)
283 VPrintf1 ( "\n [%d: huff+mtf ", s->currBlockNo );
285 s->storedBlockCRC = 0;
286 GET_UCHAR(BZ_X_BCRC_1, uc);
287 s->storedBlockCRC = (s->storedBlockCRC << 8) | ((UInt32)uc);
288 GET_UCHAR(BZ_X_BCRC_2, uc);
289 s->storedBlockCRC = (s->storedBlockCRC << 8) | ((UInt32)uc);
290 GET_UCHAR(BZ_X_BCRC_3, uc);
291 s->storedBlockCRC = (s->storedBlockCRC << 8) | ((UInt32)uc);
292 GET_UCHAR(BZ_X_BCRC_4, uc);
293 s->storedBlockCRC = (s->storedBlockCRC << 8) | ((UInt32)uc);
295 GET_BITS(BZ_X_RANDBIT, s->blockRandomised, 1);
298 GET_UCHAR(BZ_X_ORIGPTR_1, uc);
299 s->origPtr = (s->origPtr << 8) | ((Int32)uc);
300 GET_UCHAR(BZ_X_ORIGPTR_2, uc);
301 s->origPtr = (s->origPtr << 8) | ((Int32)uc);
302 GET_UCHAR(BZ_X_ORIGPTR_3, uc);
303 s->origPtr = (s->origPtr << 8) | ((Int32)uc);
306 RETURN(BZ_DATA_ERROR);
307 if (s->origPtr > 10 + 100000*s->blockSize100k)
308 RETURN(BZ_DATA_ERROR);
310 /*--- Receive the mapping table ---*/
311 for (i = 0; i < 16; i++) {
312 GET_BIT(BZ_X_MAPPING_1, uc);
314 s->inUse16[i] = True; else
315 s->inUse16[i] = False;
318 for (i = 0; i < 256; i++) s->inUse[i] = False;
320 for (i = 0; i < 16; i++)
322 for (j = 0; j < 16; j++) {
323 GET_BIT(BZ_X_MAPPING_2, uc);
324 if (uc == 1) s->inUse[i * 16 + j] = True;
327 if (s->nInUse == 0) RETURN(BZ_DATA_ERROR);
328 alphaSize = s->nInUse+2;
330 /*--- Now the selectors ---*/
331 GET_BITS(BZ_X_SELECTOR_1, nGroups, 3);
332 if (nGroups < 2 || nGroups > 6) RETURN(BZ_DATA_ERROR);
333 GET_BITS(BZ_X_SELECTOR_2, nSelectors, 15);
334 if (nSelectors < 1) RETURN(BZ_DATA_ERROR);
335 for (i = 0; i < nSelectors; i++) {
338 GET_BIT(BZ_X_SELECTOR_3, uc);
341 if (j >= nGroups) RETURN(BZ_DATA_ERROR);
343 s->selectorMtf[i] = j;
346 /*--- Undo the MTF values for the selectors. ---*/
348 UChar pos[BZ_N_GROUPS], tmp, v;
349 for (v = 0; v < nGroups; v++) pos[v] = v;
351 for (i = 0; i < nSelectors; i++) {
352 v = s->selectorMtf[i];
354 while (v > 0) { pos[v] = pos[v-1]; v--; }
356 s->selector[i] = tmp;
360 /*--- Now the coding tables ---*/
361 for (t = 0; t < nGroups; t++) {
362 GET_BITS(BZ_X_CODING_1, curr, 5);
363 for (i = 0; i < alphaSize; i++) {
365 if (curr < 1 || curr > 20) RETURN(BZ_DATA_ERROR);
366 GET_BIT(BZ_X_CODING_2, uc);
368 GET_BIT(BZ_X_CODING_3, uc);
369 if (uc == 0) curr++; else curr--;
375 /*--- Create the Huffman decoding tables ---*/
376 for (t = 0; t < nGroups; t++) {
379 for (i = 0; i < alphaSize; i++) {
380 if (s->len[t][i] > maxLen) maxLen = s->len[t][i];
381 if (s->len[t][i] < minLen) minLen = s->len[t][i];
383 BZ2_hbCreateDecodeTables (
388 minLen, maxLen, alphaSize
390 s->minLens[t] = minLen;
393 /*--- Now the MTF values ---*/
396 nblockMAX = 100000 * s->blockSize100k;
400 for (i = 0; i <= 255; i++) s->unzftab[i] = 0;
406 for (ii = 256 / MTFL_SIZE - 1; ii >= 0; ii--) {
407 for (jj = MTFL_SIZE-1; jj >= 0; jj--) {
408 s->mtfa[kk] = (UChar)(ii * MTFL_SIZE + jj);
411 s->mtfbase[ii] = kk + 1;
414 /*-- end MTF init --*/
417 GET_MTF_VAL(BZ_X_MTF_1, BZ_X_MTF_2, nextSym);
421 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
424 if (nextSym == EOB) break;
426 if (nextSym == BZ_RUNA || nextSym == BZ_RUNB) {
431 if (nextSym == BZ_RUNA) es = es + (0+1) * N; else
432 if (nextSym == BZ_RUNB) es = es + (1+1) * N;
434 GET_MTF_VAL(BZ_X_MTF_3, BZ_X_MTF_4, nextSym);
436 while (nextSym == BZ_RUNA || nextSym == BZ_RUNB);
439 uc = s->seqToUnseq[ s->mtfa[s->mtfbase[0]] ];
440 s->unzftab[uc] += es;
442 if (s->smallDecompress)
444 if (nblock >= nblockMAX) RETURN(BZ_DATA_ERROR);
445 s->ll16[nblock] = (UInt16)uc;
451 if (nblock >= nblockMAX) RETURN(BZ_DATA_ERROR);
452 s->tt[nblock] = (UInt32)uc;
461 if (nblock >= nblockMAX) RETURN(BZ_DATA_ERROR);
463 /*-- uc = MTF ( nextSym-1 ) --*/
465 Int32 ii, jj, kk, pp, lno, off;
467 nn = (UInt32)(nextSym - 1);
469 if (nn < MTFL_SIZE) {
470 /* avoid general-case expense */
475 s->mtfa[(z) ] = s->mtfa[(z)-1];
476 s->mtfa[(z)-1] = s->mtfa[(z)-2];
477 s->mtfa[(z)-2] = s->mtfa[(z)-3];
478 s->mtfa[(z)-3] = s->mtfa[(z)-4];
482 s->mtfa[(pp+nn)] = s->mtfa[(pp+nn)-1]; nn--;
487 lno = nn / MTFL_SIZE;
488 off = nn % MTFL_SIZE;
489 pp = s->mtfbase[lno] + off;
491 while (pp > s->mtfbase[lno]) {
492 s->mtfa[pp] = s->mtfa[pp-1]; pp--;
497 s->mtfa[s->mtfbase[lno]]
498 = s->mtfa[s->mtfbase[lno-1] + MTFL_SIZE - 1];
502 s->mtfa[s->mtfbase[0]] = uc;
503 if (s->mtfbase[0] == 0) {
505 for (ii = 256 / MTFL_SIZE-1; ii >= 0; ii--) {
506 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
509 for (jj = MTFL_SIZE-1; jj >= 0; jj--) {
510 s->mtfa[kk] = s->mtfa[s->mtfbase[ii] + jj];
513 s->mtfbase[ii] = kk + 1;
518 /*-- end uc = MTF ( nextSym-1 ) --*/
520 s->unzftab[s->seqToUnseq[uc]]++;
521 if (s->smallDecompress)
522 s->ll16[nblock] = (UInt16)(s->seqToUnseq[uc]); else
523 s->tt[nblock] = (UInt32)(s->seqToUnseq[uc]);
526 GET_MTF_VAL(BZ_X_MTF_5, BZ_X_MTF_6, nextSym);
531 /* Now we know what nblock is, we can do a better sanity
534 if (s->origPtr < 0 || s->origPtr >= nblock)
535 RETURN(BZ_DATA_ERROR);
537 s->state_out_len = 0;
539 BZ_INITIALISE_CRC ( s->calculatedBlockCRC );
540 s->state = BZ_X_OUTPUT;
541 if (s->verbosity >= 2) VPrintf0 ( "rt+rld" );
543 /*-- Set up cftab to facilitate generation of T^(-1) --*/
545 for (i = 1; i <= 256; i++) s->cftab[i] = s->unzftab[i-1];
546 for (i = 1; i <= 256; i++) s->cftab[i] += s->cftab[i-1];
548 if (s->smallDecompress) {
550 /*-- Make a copy of cftab, used in generation of T --*/
551 for (i = 0; i <= 256; i++) s->cftabCopy[i] = s->cftab[i];
553 /*-- compute the T vector --*/
554 for (i = 0; i < nblock; i++) {
555 uc = (UChar)(s->ll16[i]);
556 SET_LL(i, s->cftabCopy[uc]);
560 /*-- Compute T^(-1) by pointer reversal on T --*/
564 Int32 tmp = GET_LL(j);
569 while (i != s->origPtr);
571 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
574 s->tPos = s->origPtr;
576 if (s->blockRandomised) {
578 BZ_GET_SMALL(s->k0); s->nblock_used++;
579 BZ_RAND_UPD_MASK; s->k0 ^= BZ_RAND_MASK;
581 BZ_GET_SMALL(s->k0); s->nblock_used++;
586 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
589 /*-- compute the T^(-1) vector --*/
590 for (i = 0; i < nblock; i++) {
591 uc = (UChar)(s->tt[i] & 0xff);
592 s->tt[s->cftab[uc]] |= (i << 8);
596 s->tPos = s->tt[s->origPtr] >> 8;
598 if (s->blockRandomised) {
600 BZ_GET_FAST(s->k0); s->nblock_used++;
601 BZ_RAND_UPD_MASK; s->k0 ^= BZ_RAND_MASK;
603 BZ_GET_FAST(s->k0); s->nblock_used++;
613 GET_UCHAR(BZ_X_ENDHDR_2, uc);
614 if (uc != 0x72) RETURN(BZ_DATA_ERROR);
615 GET_UCHAR(BZ_X_ENDHDR_3, uc);
616 if (uc != 0x45) RETURN(BZ_DATA_ERROR);
617 GET_UCHAR(BZ_X_ENDHDR_4, uc);
618 if (uc != 0x38) RETURN(BZ_DATA_ERROR);
619 GET_UCHAR(BZ_X_ENDHDR_5, uc);
620 if (uc != 0x50) RETURN(BZ_DATA_ERROR);
621 GET_UCHAR(BZ_X_ENDHDR_6, uc);
622 if (uc != 0x90) RETURN(BZ_DATA_ERROR);
624 s->storedCombinedCRC = 0;
625 GET_UCHAR(BZ_X_CCRC_1, uc);
626 s->storedCombinedCRC = (s->storedCombinedCRC << 8) | ((UInt32)uc);
627 GET_UCHAR(BZ_X_CCRC_2, uc);
628 s->storedCombinedCRC = (s->storedCombinedCRC << 8) | ((UInt32)uc);
629 GET_UCHAR(BZ_X_CCRC_3, uc);
630 s->storedCombinedCRC = (s->storedCombinedCRC << 8) | ((UInt32)uc);
631 GET_UCHAR(BZ_X_CCRC_4, uc);
632 s->storedCombinedCRC = (s->storedCombinedCRC << 8) | ((UInt32)uc);
634 s->state = BZ_X_IDLE;
635 RETURN(BZ_STREAM_END);
637 default: AssertH ( False, 4001 );
640 AssertH ( False, 4002 );
642 save_state_and_return:
647 s->save_alphaSize = alphaSize;
648 s->save_nGroups = nGroups;
649 s->save_nSelectors = nSelectors;
651 s->save_groupNo = groupNo;
652 s->save_groupPos = groupPos;
653 s->save_nextSym = nextSym;
654 s->save_nblockMAX = nblockMAX;
655 s->save_nblock = nblock;
664 s->save_gMinlen = gMinlen;
665 s->save_gLimit = gLimit;
666 s->save_gBase = gBase;
667 s->save_gPerm = gPerm;
673 /*-------------------------------------------------------------*/
674 /*--- end decompress.c ---*/
675 /*-------------------------------------------------------------*/
677 #endif /* CONFIG_BZIP2 */