5 /*-------------------------------------------------------------*/
6 /*--- Decompression machinery ---*/
7 /*--- decompress.c ---*/
8 /*-------------------------------------------------------------*/
11 This file is a part of bzip2 and/or libbzip2, a program and
12 library for lossless, block-sorting data compression.
14 Copyright (C) 1996-2002 Julian R Seward. All rights reserved.
16 Redistribution and use in source and binary forms, with or without
17 modification, are permitted provided that the following conditions
20 1. Redistributions of source code must retain the above copyright
21 notice, this list of conditions and the following disclaimer.
23 2. The origin of this software must not be misrepresented; you must
24 not claim that you wrote the original software. If you use this
25 software in a product, an acknowledgment in the product
26 documentation would be appreciated but is not required.
28 3. Altered source versions must be plainly marked as such, and must
29 not be misrepresented as being the original software.
31 4. The name of the author may not be used to endorse or promote
32 products derived from this software without specific prior written
35 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
36 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
37 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38 ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
39 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
41 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
42 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
43 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
44 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
45 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47 Julian Seward, Cambridge, UK.
49 bzip2/libbzip2 version 1.0 of 21 March 2000
51 This program is based on (at least) the work of:
61 For more information on these sources, see the manual.
65 #include "bzlib_private.h"
68 /*---------------------------------------------------*/
70 void makeMaps_d ( DState
* s
)
74 for (i
= 0; i
< 256; i
++)
76 s
->seqToUnseq
[s
->nInUse
] = i
;
82 /*---------------------------------------------------*/
84 { retVal = rrr; goto save_state_and_return; };
86 #define GET_BITS(lll,vvv,nnn) \
87 case lll: s->state = lll; \
89 if (s->bsLive >= nnn) { \
92 (s->bsLive-nnn)) & ((1 << nnn)-1); \
97 if (s->strm->avail_in == 0) RETURN(BZ_OK); \
99 = (s->bsBuff << 8) | \
101 (*((UChar*)(s->strm->next_in)))); \
103 s->strm->next_in++; \
104 s->strm->avail_in--; \
105 s->strm->total_in_lo32++; \
106 if (s->strm->total_in_lo32 == 0) \
107 s->strm->total_in_hi32++; \
110 #define GET_UCHAR(lll,uuu) \
113 #define GET_BIT(lll,uuu) \
116 /*---------------------------------------------------*/
117 #define GET_MTF_VAL(label1,label2,lval) \
119 if (groupPos == 0) { \
121 if (groupNo >= nSelectors) \
122 RETURN(BZ_DATA_ERROR); \
123 groupPos = BZ_G_SIZE; \
124 gSel = s->selector[groupNo]; \
125 gMinlen = s->minLens[gSel]; \
126 gLimit = &(s->limit[gSel][0]); \
127 gPerm = &(s->perm[gSel][0]); \
128 gBase = &(s->base[gSel][0]); \
132 GET_BITS(label1, zvec, zn); \
134 if (zn > 20 /* the longest code */) \
135 RETURN(BZ_DATA_ERROR); \
136 if (zvec <= gLimit[zn]) break; \
138 GET_BIT(label2, zj); \
139 zvec = (zvec << 1) | zj; \
141 if (zvec - gBase[zn] < 0 \
142 || zvec - gBase[zn] >= BZ_MAX_ALPHA_SIZE) \
143 RETURN(BZ_DATA_ERROR); \
144 lval = gPerm[zvec - gBase[zn]]; \
148 /*---------------------------------------------------*/
149 Int32
BZ2_decompress ( DState
* s
)
153 Int32 minLen
, maxLen
;
154 bz_stream
* strm
= s
->strm
;
156 /* stuff that needs to be saved/restored */
182 if (s
->state
== BZ_X_MAGIC_1
) {
183 /*initialise the save area*/
187 s
->save_alphaSize
= 0;
189 s
->save_nSelectors
= 0;
192 s
->save_groupPos
= 0;
194 s
->save_nblockMAX
= 0;
205 s
->save_gLimit
= NULL
;
206 s
->save_gBase
= NULL
;
207 s
->save_gPerm
= NULL
;
210 /*restore from the save area*/
214 alphaSize
= s
->save_alphaSize
;
215 nGroups
= s
->save_nGroups
;
216 nSelectors
= s
->save_nSelectors
;
218 groupNo
= s
->save_groupNo
;
219 groupPos
= s
->save_groupPos
;
220 nextSym
= s
->save_nextSym
;
221 nblockMAX
= s
->save_nblockMAX
;
222 nblock
= s
->save_nblock
;
231 gMinlen
= s
->save_gMinlen
;
232 gLimit
= s
->save_gLimit
;
233 gBase
= s
->save_gBase
;
234 gPerm
= s
->save_gPerm
;
240 GET_UCHAR(BZ_X_MAGIC_1
, uc
);
241 if (uc
!= BZ_HDR_B
) RETURN(BZ_DATA_ERROR_MAGIC
);
243 GET_UCHAR(BZ_X_MAGIC_2
, uc
);
244 if (uc
!= BZ_HDR_Z
) RETURN(BZ_DATA_ERROR_MAGIC
);
246 GET_UCHAR(BZ_X_MAGIC_3
, uc
)
247 if (uc
!= BZ_HDR_h
) RETURN(BZ_DATA_ERROR_MAGIC
);
249 GET_BITS(BZ_X_MAGIC_4
, s
->blockSize100k
, 8)
250 if (s
->blockSize100k
< (BZ_HDR_0
+ 1) ||
251 s
->blockSize100k
> (BZ_HDR_0
+ 9)) RETURN(BZ_DATA_ERROR_MAGIC
);
252 s
->blockSize100k
-= BZ_HDR_0
;
254 if (s
->smallDecompress
) {
255 s
->ll16
= BZALLOC( s
->blockSize100k
* 100000 * sizeof(UInt16
) );
257 ((1 + s
->blockSize100k
* 100000) >> 1) * sizeof(UChar
)
259 if (s
->ll16
== NULL
|| s
->ll4
== NULL
) RETURN(BZ_MEM_ERROR
);
261 s
->tt
= BZALLOC( s
->blockSize100k
* 100000 * sizeof(Int32
) );
262 if (s
->tt
== NULL
) RETURN(BZ_MEM_ERROR
);
265 GET_UCHAR(BZ_X_BLKHDR_1
, uc
);
267 if (uc
== 0x17) goto endhdr_2
;
268 if (uc
!= 0x31) RETURN(BZ_DATA_ERROR
);
269 GET_UCHAR(BZ_X_BLKHDR_2
, uc
);
270 if (uc
!= 0x41) RETURN(BZ_DATA_ERROR
);
271 GET_UCHAR(BZ_X_BLKHDR_3
, uc
);
272 if (uc
!= 0x59) RETURN(BZ_DATA_ERROR
);
273 GET_UCHAR(BZ_X_BLKHDR_4
, uc
);
274 if (uc
!= 0x26) RETURN(BZ_DATA_ERROR
);
275 GET_UCHAR(BZ_X_BLKHDR_5
, uc
);
276 if (uc
!= 0x53) RETURN(BZ_DATA_ERROR
);
277 GET_UCHAR(BZ_X_BLKHDR_6
, uc
);
278 if (uc
!= 0x59) RETURN(BZ_DATA_ERROR
);
281 if (s
->verbosity
>= 2)
282 VPrintf1 ( "\n [%d: huff+mtf ", s
->currBlockNo
);
284 s
->storedBlockCRC
= 0;
285 GET_UCHAR(BZ_X_BCRC_1
, uc
);
286 s
->storedBlockCRC
= (s
->storedBlockCRC
<< 8) | ((UInt32
)uc
);
287 GET_UCHAR(BZ_X_BCRC_2
, uc
);
288 s
->storedBlockCRC
= (s
->storedBlockCRC
<< 8) | ((UInt32
)uc
);
289 GET_UCHAR(BZ_X_BCRC_3
, uc
);
290 s
->storedBlockCRC
= (s
->storedBlockCRC
<< 8) | ((UInt32
)uc
);
291 GET_UCHAR(BZ_X_BCRC_4
, uc
);
292 s
->storedBlockCRC
= (s
->storedBlockCRC
<< 8) | ((UInt32
)uc
);
294 GET_BITS(BZ_X_RANDBIT
, s
->blockRandomised
, 1);
297 GET_UCHAR(BZ_X_ORIGPTR_1
, uc
);
298 s
->origPtr
= (s
->origPtr
<< 8) | ((Int32
)uc
);
299 GET_UCHAR(BZ_X_ORIGPTR_2
, uc
);
300 s
->origPtr
= (s
->origPtr
<< 8) | ((Int32
)uc
);
301 GET_UCHAR(BZ_X_ORIGPTR_3
, uc
);
302 s
->origPtr
= (s
->origPtr
<< 8) | ((Int32
)uc
);
305 RETURN(BZ_DATA_ERROR
);
306 if (s
->origPtr
> 10 + 100000*s
->blockSize100k
)
307 RETURN(BZ_DATA_ERROR
);
309 /*--- Receive the mapping table ---*/
310 for (i
= 0; i
< 16; i
++) {
311 GET_BIT(BZ_X_MAPPING_1
, uc
);
313 s
->inUse16
[i
] = True
; else
314 s
->inUse16
[i
] = False
;
317 for (i
= 0; i
< 256; i
++) s
->inUse
[i
] = False
;
319 for (i
= 0; i
< 16; i
++)
321 for (j
= 0; j
< 16; j
++) {
322 GET_BIT(BZ_X_MAPPING_2
, uc
);
323 if (uc
== 1) s
->inUse
[i
* 16 + j
] = True
;
326 if (s
->nInUse
== 0) RETURN(BZ_DATA_ERROR
);
327 alphaSize
= s
->nInUse
+2;
329 /*--- Now the selectors ---*/
330 GET_BITS(BZ_X_SELECTOR_1
, nGroups
, 3);
331 if (nGroups
< 2 || nGroups
> 6) RETURN(BZ_DATA_ERROR
);
332 GET_BITS(BZ_X_SELECTOR_2
, nSelectors
, 15);
333 if (nSelectors
< 1) RETURN(BZ_DATA_ERROR
);
334 for (i
= 0; i
< nSelectors
; i
++) {
337 GET_BIT(BZ_X_SELECTOR_3
, uc
);
340 if (j
>= nGroups
) RETURN(BZ_DATA_ERROR
);
342 s
->selectorMtf
[i
] = j
;
345 /*--- Undo the MTF values for the selectors. ---*/
347 UChar pos
[BZ_N_GROUPS
], tmp
, v
;
348 for (v
= 0; v
< nGroups
; v
++) pos
[v
] = v
;
350 for (i
= 0; i
< nSelectors
; i
++) {
351 v
= s
->selectorMtf
[i
];
353 while (v
> 0) { pos
[v
] = pos
[v
-1]; v
--; }
355 s
->selector
[i
] = tmp
;
359 /*--- Now the coding tables ---*/
360 for (t
= 0; t
< nGroups
; t
++) {
361 GET_BITS(BZ_X_CODING_1
, curr
, 5);
362 for (i
= 0; i
< alphaSize
; i
++) {
364 if (curr
< 1 || curr
> 20) RETURN(BZ_DATA_ERROR
);
365 GET_BIT(BZ_X_CODING_2
, uc
);
367 GET_BIT(BZ_X_CODING_3
, uc
);
368 if (uc
== 0) curr
++; else curr
--;
374 /*--- Create the Huffman decoding tables ---*/
375 for (t
= 0; t
< nGroups
; t
++) {
378 for (i
= 0; i
< alphaSize
; i
++) {
379 if (s
->len
[t
][i
] > maxLen
) maxLen
= s
->len
[t
][i
];
380 if (s
->len
[t
][i
] < minLen
) minLen
= s
->len
[t
][i
];
382 BZ2_hbCreateDecodeTables (
387 minLen
, maxLen
, alphaSize
389 s
->minLens
[t
] = minLen
;
392 /*--- Now the MTF values ---*/
395 nblockMAX
= 100000 * s
->blockSize100k
;
399 for (i
= 0; i
<= 255; i
++) s
->unzftab
[i
] = 0;
405 for (ii
= 256 / MTFL_SIZE
- 1; ii
>= 0; ii
--) {
406 for (jj
= MTFL_SIZE
-1; jj
>= 0; jj
--) {
407 s
->mtfa
[kk
] = (UChar
)(ii
* MTFL_SIZE
+ jj
);
410 s
->mtfbase
[ii
] = kk
+ 1;
413 /*-- end MTF init --*/
416 GET_MTF_VAL(BZ_X_MTF_1
, BZ_X_MTF_2
, nextSym
);
420 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
423 if (nextSym
== EOB
) break;
425 if (nextSym
== BZ_RUNA
|| nextSym
== BZ_RUNB
) {
430 if (nextSym
== BZ_RUNA
) es
= es
+ (0+1) * N
; else
431 if (nextSym
== BZ_RUNB
) es
= es
+ (1+1) * N
;
433 GET_MTF_VAL(BZ_X_MTF_3
, BZ_X_MTF_4
, nextSym
);
435 while (nextSym
== BZ_RUNA
|| nextSym
== BZ_RUNB
);
438 uc
= s
->seqToUnseq
[ s
->mtfa
[s
->mtfbase
[0]] ];
439 s
->unzftab
[uc
] += es
;
441 if (s
->smallDecompress
)
443 if (nblock
>= nblockMAX
) RETURN(BZ_DATA_ERROR
);
444 s
->ll16
[nblock
] = (UInt16
)uc
;
450 if (nblock
>= nblockMAX
) RETURN(BZ_DATA_ERROR
);
451 s
->tt
[nblock
] = (UInt32
)uc
;
460 if (nblock
>= nblockMAX
) RETURN(BZ_DATA_ERROR
);
462 /*-- uc = MTF ( nextSym-1 ) --*/
464 Int32 ii
, jj
, kk
, pp
, lno
, off
;
466 nn
= (UInt32
)(nextSym
- 1);
468 if (nn
< MTFL_SIZE
) {
469 /* avoid general-case expense */
474 s
->mtfa
[(z
) ] = s
->mtfa
[(z
)-1];
475 s
->mtfa
[(z
)-1] = s
->mtfa
[(z
)-2];
476 s
->mtfa
[(z
)-2] = s
->mtfa
[(z
)-3];
477 s
->mtfa
[(z
)-3] = s
->mtfa
[(z
)-4];
481 s
->mtfa
[(pp
+nn
)] = s
->mtfa
[(pp
+nn
)-1]; nn
--;
486 lno
= nn
/ MTFL_SIZE
;
487 off
= nn
% MTFL_SIZE
;
488 pp
= s
->mtfbase
[lno
] + off
;
490 while (pp
> s
->mtfbase
[lno
]) {
491 s
->mtfa
[pp
] = s
->mtfa
[pp
-1]; pp
--;
496 s
->mtfa
[s
->mtfbase
[lno
]]
497 = s
->mtfa
[s
->mtfbase
[lno
-1] + MTFL_SIZE
- 1];
501 s
->mtfa
[s
->mtfbase
[0]] = uc
;
502 if (s
->mtfbase
[0] == 0) {
504 for (ii
= 256 / MTFL_SIZE
-1; ii
>= 0; ii
--) {
505 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
508 for (jj
= MTFL_SIZE
-1; jj
>= 0; jj
--) {
509 s
->mtfa
[kk
] = s
->mtfa
[s
->mtfbase
[ii
] + jj
];
512 s
->mtfbase
[ii
] = kk
+ 1;
517 /*-- end uc = MTF ( nextSym-1 ) --*/
519 s
->unzftab
[s
->seqToUnseq
[uc
]]++;
520 if (s
->smallDecompress
)
521 s
->ll16
[nblock
] = (UInt16
)(s
->seqToUnseq
[uc
]); else
522 s
->tt
[nblock
] = (UInt32
)(s
->seqToUnseq
[uc
]);
525 GET_MTF_VAL(BZ_X_MTF_5
, BZ_X_MTF_6
, nextSym
);
530 /* Now we know what nblock is, we can do a better sanity
533 if (s
->origPtr
< 0 || s
->origPtr
>= nblock
)
534 RETURN(BZ_DATA_ERROR
);
536 s
->state_out_len
= 0;
538 BZ_INITIALISE_CRC ( s
->calculatedBlockCRC
);
539 s
->state
= BZ_X_OUTPUT
;
540 if (s
->verbosity
>= 2) VPrintf0 ( "rt+rld" );
542 /*-- Set up cftab to facilitate generation of T^(-1) --*/
544 for (i
= 1; i
<= 256; i
++) s
->cftab
[i
] = s
->unzftab
[i
-1];
545 for (i
= 1; i
<= 256; i
++) s
->cftab
[i
] += s
->cftab
[i
-1];
547 if (s
->smallDecompress
) {
549 /*-- Make a copy of cftab, used in generation of T --*/
550 for (i
= 0; i
<= 256; i
++) s
->cftabCopy
[i
] = s
->cftab
[i
];
552 /*-- compute the T vector --*/
553 for (i
= 0; i
< nblock
; i
++) {
554 uc
= (UChar
)(s
->ll16
[i
]);
555 SET_LL(i
, s
->cftabCopy
[uc
]);
559 /*-- Compute T^(-1) by pointer reversal on T --*/
563 Int32 tmp
= GET_LL(j
);
568 while (i
!= s
->origPtr
);
570 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
573 s
->tPos
= s
->origPtr
;
575 if (s
->blockRandomised
) {
577 BZ_GET_SMALL(s
->k0
); s
->nblock_used
++;
578 BZ_RAND_UPD_MASK
; s
->k0
^= BZ_RAND_MASK
;
580 BZ_GET_SMALL(s
->k0
); s
->nblock_used
++;
585 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
588 /*-- compute the T^(-1) vector --*/
589 for (i
= 0; i
< nblock
; i
++) {
590 uc
= (UChar
)(s
->tt
[i
] & 0xff);
591 s
->tt
[s
->cftab
[uc
]] |= (i
<< 8);
595 s
->tPos
= s
->tt
[s
->origPtr
] >> 8;
597 if (s
->blockRandomised
) {
599 BZ_GET_FAST(s
->k0
); s
->nblock_used
++;
600 BZ_RAND_UPD_MASK
; s
->k0
^= BZ_RAND_MASK
;
602 BZ_GET_FAST(s
->k0
); s
->nblock_used
++;
612 GET_UCHAR(BZ_X_ENDHDR_2
, uc
);
613 if (uc
!= 0x72) RETURN(BZ_DATA_ERROR
);
614 GET_UCHAR(BZ_X_ENDHDR_3
, uc
);
615 if (uc
!= 0x45) RETURN(BZ_DATA_ERROR
);
616 GET_UCHAR(BZ_X_ENDHDR_4
, uc
);
617 if (uc
!= 0x38) RETURN(BZ_DATA_ERROR
);
618 GET_UCHAR(BZ_X_ENDHDR_5
, uc
);
619 if (uc
!= 0x50) RETURN(BZ_DATA_ERROR
);
620 GET_UCHAR(BZ_X_ENDHDR_6
, uc
);
621 if (uc
!= 0x90) RETURN(BZ_DATA_ERROR
);
623 s
->storedCombinedCRC
= 0;
624 GET_UCHAR(BZ_X_CCRC_1
, uc
);
625 s
->storedCombinedCRC
= (s
->storedCombinedCRC
<< 8) | ((UInt32
)uc
);
626 GET_UCHAR(BZ_X_CCRC_2
, uc
);
627 s
->storedCombinedCRC
= (s
->storedCombinedCRC
<< 8) | ((UInt32
)uc
);
628 GET_UCHAR(BZ_X_CCRC_3
, uc
);
629 s
->storedCombinedCRC
= (s
->storedCombinedCRC
<< 8) | ((UInt32
)uc
);
630 GET_UCHAR(BZ_X_CCRC_4
, uc
);
631 s
->storedCombinedCRC
= (s
->storedCombinedCRC
<< 8) | ((UInt32
)uc
);
633 s
->state
= BZ_X_IDLE
;
634 RETURN(BZ_STREAM_END
);
636 default: AssertH ( False
, 4001 );
639 AssertH ( False
, 4002 );
641 save_state_and_return
:
646 s
->save_alphaSize
= alphaSize
;
647 s
->save_nGroups
= nGroups
;
648 s
->save_nSelectors
= nSelectors
;
650 s
->save_groupNo
= groupNo
;
651 s
->save_groupPos
= groupPos
;
652 s
->save_nextSym
= nextSym
;
653 s
->save_nblockMAX
= nblockMAX
;
654 s
->save_nblock
= nblock
;
663 s
->save_gMinlen
= gMinlen
;
664 s
->save_gLimit
= gLimit
;
665 s
->save_gBase
= gBase
;
666 s
->save_gPerm
= gPerm
;
672 /*-------------------------------------------------------------*/
673 /*--- end decompress.c ---*/
674 /*-------------------------------------------------------------*/