2 * This file is a modified version of bzlib_private.h from the bzip2-1.0.2
3 * distribution which can be found at http://sources.redhat.com/bzip2/
6 /*-------------------------------------------------------------*/
7 /*--- Private header file for the library. ---*/
8 /*--- bzlib_private.h ---*/
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 #ifndef _BZLIB_PRIVATE_H
67 #define _BZLIB_PRIVATE_H
80 /*-- General stuff. --*/
82 #define BZ_VERSION "1.0.2, 30-Dec-2001"
85 typedef unsigned char Bool;
86 typedef unsigned char UChar;
88 typedef unsigned int UInt32;
90 typedef unsigned short UInt16;
92 #define True ((Bool)1)
93 #define False ((Bool)0)
96 #define __inline__ /* */
100 extern void BZ2_bz__AssertH__fail ( int errcode );
101 #define AssertH(cond,errcode) \
102 { if (!(cond)) BZ2_bz__AssertH__fail ( errcode ); }
104 #define AssertD(cond,msg) \
107 "\n\nlibbzip2(debug build): internal error\n\t%s\n", msg );\
111 #define AssertD(cond,msg) /* */
113 #define VPrintf0(zf) \
115 #define VPrintf1(zf,za1) \
116 fprintf(stderr,zf,za1)
117 #define VPrintf2(zf,za1,za2) \
118 fprintf(stderr,zf,za1,za2)
119 #define VPrintf3(zf,za1,za2,za3) \
120 fprintf(stderr,zf,za1,za2,za3)
121 #define VPrintf4(zf,za1,za2,za3,za4) \
122 fprintf(stderr,zf,za1,za2,za3,za4)
123 #define VPrintf5(zf,za1,za2,za3,za4,za5) \
124 fprintf(stderr,zf,za1,za2,za3,za4,za5)
126 extern void bz_internal_error ( int errcode );
127 #define AssertH(cond,errcode) \
128 { if (!(cond)) bz_internal_error ( errcode ); }
129 #define AssertD(cond,msg) /* */
130 #define VPrintf0(zf) /* */
131 #define VPrintf1(zf,za1) /* */
132 #define VPrintf2(zf,za1,za2) /* */
133 #define VPrintf3(zf,za1,za2,za3) /* */
134 #define VPrintf4(zf,za1,za2,za3,za4) /* */
135 #define VPrintf5(zf,za1,za2,za3,za4,za5) /* */
139 #define BZALLOC(nnn) (strm->bzalloc)(strm->opaque,(nnn),1)
140 #define BZFREE(ppp) (strm->bzfree)(strm->opaque,(ppp))
143 /*-- Header bytes. --*/
145 #define BZ_HDR_B 0x42 /* 'B' */
146 #define BZ_HDR_Z 0x5a /* 'Z' */
147 #define BZ_HDR_h 0x68 /* 'h' */
148 #define BZ_HDR_0 0x30 /* '0' */
150 /*-- Constants for the back end. --*/
152 #define BZ_MAX_ALPHA_SIZE 258
153 #define BZ_MAX_CODE_LEN 23
158 #define BZ_N_GROUPS 6
162 #define BZ_MAX_SELECTORS (2 + (900000 / BZ_G_SIZE))
165 /*-- Stuff for randomising repetitive blocks. --*/
167 extern Int32 BZ2_rNums[512];
169 #define BZ_RAND_DECLS \
173 #define BZ_RAND_INIT_MASK \
177 #define BZ_RAND_MASK ((s->rNToGo == 1) ? 1 : 0)
179 #define BZ_RAND_UPD_MASK \
180 if (s->rNToGo == 0) { \
181 s->rNToGo = BZ2_rNums[s->rTPos]; \
183 if (s->rTPos == 512) s->rTPos = 0; \
188 /*-- Stuff for doing CRCs. --*/
190 extern UInt32 BZ2_crc32Table[256];
192 #define BZ_INITIALISE_CRC(crcVar) \
194 crcVar = 0xffffffffL; \
197 #define BZ_FINALISE_CRC(crcVar) \
199 crcVar = ~(crcVar); \
202 #define BZ_UPDATE_CRC(crcVar,cha) \
204 crcVar = (crcVar << 8) ^ \
205 BZ2_crc32Table[(crcVar >> 24) ^ \
210 /*-- States and modes for compression. --*/
213 #define BZ_M_RUNNING 2
214 #define BZ_M_FLUSHING 3
215 #define BZ_M_FINISHING 4
217 #define BZ_S_OUTPUT 1
221 #define BZ_N_QSORT 12
222 #define BZ_N_SHELL 18
223 #define BZ_N_OVERSHOOT (BZ_N_RADIX + BZ_N_QSORT + BZ_N_SHELL + 2)
226 /*-- Structure holding all the compression-side stuff. --*/
230 /* pointer back to the struct bz_stream */
233 /* mode this stream is in, and whether inputting */
234 /* or outputting data */
238 /* remembers avail_in when flush/finish requested */
239 UInt32 avail_in_expect;
241 /* for doing the block sorting */
247 /* aliases for arr1 and arr2 */
253 /* for deciding when to use the fallback sorting algorithm */
256 /* run-length-encoding of the input */
261 /* input and output limits and current posns */
267 /* map of bytes used in block */
270 UChar unseqToSeq[256];
272 /* the buffer for bit stream creation */
276 /* block and combined CRCs */
280 /* misc administratium */
285 /* stuff for coding the MTF values */
287 Int32 mtfFreq [BZ_MAX_ALPHA_SIZE];
288 UChar selector [BZ_MAX_SELECTORS];
289 UChar selectorMtf[BZ_MAX_SELECTORS];
291 UChar len [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
292 Int32 code [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
293 Int32 rfreq [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
294 /* second dimension: only 3 needed; 4 makes index calculations faster */
295 UInt32 len_pack[BZ_MAX_ALPHA_SIZE][4];
301 /*-- externs for compression. --*/
304 BZ2_blockSort ( EState* );
307 BZ2_compressBlock ( EState*, Bool );
310 BZ2_bsInitWrite ( EState* );
313 BZ2_hbAssignCodes ( Int32*, UChar*, Int32, Int32, Int32 );
316 BZ2_hbMakeCodeLengths ( UChar*, Int32*, Int32, Int32 );
319 /*-- states for decompression. --*/
322 #define BZ_X_OUTPUT 2
324 #define BZ_X_MAGIC_1 10
325 #define BZ_X_MAGIC_2 11
326 #define BZ_X_MAGIC_3 12
327 #define BZ_X_MAGIC_4 13
328 #define BZ_X_BLKHDR_1 14
329 #define BZ_X_BLKHDR_2 15
330 #define BZ_X_BLKHDR_3 16
331 #define BZ_X_BLKHDR_4 17
332 #define BZ_X_BLKHDR_5 18
333 #define BZ_X_BLKHDR_6 19
334 #define BZ_X_BCRC_1 20
335 #define BZ_X_BCRC_2 21
336 #define BZ_X_BCRC_3 22
337 #define BZ_X_BCRC_4 23
338 #define BZ_X_RANDBIT 24
339 #define BZ_X_ORIGPTR_1 25
340 #define BZ_X_ORIGPTR_2 26
341 #define BZ_X_ORIGPTR_3 27
342 #define BZ_X_MAPPING_1 28
343 #define BZ_X_MAPPING_2 29
344 #define BZ_X_SELECTOR_1 30
345 #define BZ_X_SELECTOR_2 31
346 #define BZ_X_SELECTOR_3 32
347 #define BZ_X_CODING_1 33
348 #define BZ_X_CODING_2 34
349 #define BZ_X_CODING_3 35
350 #define BZ_X_MTF_1 36
351 #define BZ_X_MTF_2 37
352 #define BZ_X_MTF_3 38
353 #define BZ_X_MTF_4 39
354 #define BZ_X_MTF_5 40
355 #define BZ_X_MTF_6 41
356 #define BZ_X_ENDHDR_2 42
357 #define BZ_X_ENDHDR_3 43
358 #define BZ_X_ENDHDR_4 44
359 #define BZ_X_ENDHDR_5 45
360 #define BZ_X_ENDHDR_6 46
361 #define BZ_X_CCRC_1 47
362 #define BZ_X_CCRC_2 48
363 #define BZ_X_CCRC_3 49
364 #define BZ_X_CCRC_4 50
367 /*-- Constants for the fast MTF decoder. --*/
369 #define MTFA_SIZE 4096
373 /*-- Structure holding all the decompression-side stuff. --*/
377 /* pointer back to the struct bz_stream */
380 /* state indicator for this stream */
383 /* for doing the final run-length decoding */
386 Bool blockRandomised;
389 /* the buffer for bit stream reading */
393 /* misc administratium */
395 Bool smallDecompress;
399 /* for undoing the Burrows-Wheeler transform */
406 Int32 cftabCopy[257];
408 /* for undoing the Burrows-Wheeler transform (FAST) */
411 /* for undoing the Burrows-Wheeler transform (SMALL) */
415 /* stored and calculated CRCs */
416 UInt32 storedBlockCRC;
417 UInt32 storedCombinedCRC;
418 UInt32 calculatedBlockCRC;
419 UInt32 calculatedCombinedCRC;
421 /* map of bytes used in block */
425 UChar seqToUnseq[256];
427 /* for decoding the MTF values */
428 UChar mtfa [MTFA_SIZE];
429 Int32 mtfbase[256 / MTFL_SIZE];
430 UChar selector [BZ_MAX_SELECTORS];
431 UChar selectorMtf[BZ_MAX_SELECTORS];
432 UChar len [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
434 Int32 limit [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
435 Int32 base [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
436 Int32 perm [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
437 Int32 minLens[BZ_N_GROUPS];
439 /* save area for scalars in the main decompress code */
443 Int32 save_alphaSize;
445 Int32 save_nSelectors;
450 Int32 save_nblockMAX;
469 /*-- Macros for decompression. --*/
471 #define BZ_GET_FAST(cccc) \
472 s->tPos = s->tt[s->tPos]; \
473 cccc = (UChar)(s->tPos & 0xff); \
476 #define BZ_GET_FAST_C(cccc) \
477 c_tPos = c_tt[c_tPos]; \
478 cccc = (UChar)(c_tPos & 0xff); \
481 #define SET_LL4(i,n) \
482 { if (((i) & 0x1) == 0) \
483 s->ll4[(i) >> 1] = (s->ll4[(i) >> 1] & 0xf0) | (n); else \
484 s->ll4[(i) >> 1] = (s->ll4[(i) >> 1] & 0x0f) | ((n) << 4); \
488 ((((UInt32)(s->ll4[(i) >> 1])) >> (((i) << 2) & 0x4)) & 0xF)
490 #define SET_LL(i,n) \
491 { s->ll16[i] = (UInt16)(n & 0x0000ffff); \
492 SET_LL4(i, n >> 16); \
496 (((UInt32)s->ll16[i]) | (GET_LL4(i) << 16))
498 #define BZ_GET_SMALL(cccc) \
499 cccc = BZ2_indexIntoF ( s->tPos, s->cftab ); \
500 s->tPos = GET_LL(s->tPos);
503 /*-- externs for decompression. --*/
506 BZ2_indexIntoF ( Int32, Int32* );
509 BZ2_decompress ( DState* );
512 BZ2_hbCreateDecodeTables ( Int32*, Int32*, Int32*, UChar*,
513 Int32, Int32, Int32 );
519 /*-- BZ_NO_STDIO seems to make NULL disappear on some platforms. --*/
528 /*-------------------------------------------------------------*/
529 /*--- end bzlib_private.h ---*/
530 /*-------------------------------------------------------------*/