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
81 /*-- General stuff. --*/
83 #define BZ_VERSION "1.0.2, 30-Dec-2001"
86 typedef unsigned char Bool;
87 typedef unsigned char UChar;
89 typedef unsigned int UInt32;
91 typedef unsigned short UInt16;
93 #define True ((Bool)1)
94 #define False ((Bool)0)
97 #define __inline__ /* */
101 extern void BZ2_bz__AssertH__fail ( int errcode );
102 #define AssertH(cond,errcode) \
103 { if (!(cond)) BZ2_bz__AssertH__fail ( errcode ); }
105 #define AssertD(cond,msg) \
108 "\n\nlibbzip2(debug build): internal error\n\t%s\n", msg );\
112 #define AssertD(cond,msg) /* */
114 #define VPrintf0(zf) \
116 #define VPrintf1(zf,za1) \
117 fprintf(stderr,zf,za1)
118 #define VPrintf2(zf,za1,za2) \
119 fprintf(stderr,zf,za1,za2)
120 #define VPrintf3(zf,za1,za2,za3) \
121 fprintf(stderr,zf,za1,za2,za3)
122 #define VPrintf4(zf,za1,za2,za3,za4) \
123 fprintf(stderr,zf,za1,za2,za3,za4)
124 #define VPrintf5(zf,za1,za2,za3,za4,za5) \
125 fprintf(stderr,zf,za1,za2,za3,za4,za5)
127 extern void bz_internal_error ( int errcode );
128 #define AssertH(cond,errcode) \
129 { if (!(cond)) bz_internal_error ( errcode ); }
130 #define AssertD(cond,msg) /* */
131 #define VPrintf0(zf) /* */
132 #define VPrintf1(zf,za1) /* */
133 #define VPrintf2(zf,za1,za2) /* */
134 #define VPrintf3(zf,za1,za2,za3) /* */
135 #define VPrintf4(zf,za1,za2,za3,za4) /* */
136 #define VPrintf5(zf,za1,za2,za3,za4,za5) /* */
140 #define BZALLOC(nnn) (strm->bzalloc)(strm->opaque,(nnn),1)
141 #define BZFREE(ppp) (strm->bzfree)(strm->opaque,(ppp))
144 /*-- Header bytes. --*/
146 #define BZ_HDR_B 0x42 /* 'B' */
147 #define BZ_HDR_Z 0x5a /* 'Z' */
148 #define BZ_HDR_h 0x68 /* 'h' */
149 #define BZ_HDR_0 0x30 /* '0' */
151 /*-- Constants for the back end. --*/
153 #define BZ_MAX_ALPHA_SIZE 258
154 #define BZ_MAX_CODE_LEN 23
159 #define BZ_N_GROUPS 6
163 #define BZ_MAX_SELECTORS (2 + (900000 / BZ_G_SIZE))
167 /*-- Stuff for randomising repetitive blocks. --*/
169 extern Int32 BZ2_rNums[512];
171 #define BZ_RAND_DECLS \
175 #define BZ_RAND_INIT_MASK \
179 #define BZ_RAND_MASK ((s->rNToGo == 1) ? 1 : 0)
181 #define BZ_RAND_UPD_MASK \
182 if (s->rNToGo == 0) { \
183 s->rNToGo = BZ2_rNums[s->rTPos]; \
185 if (s->rTPos == 512) s->rTPos = 0; \
191 /*-- Stuff for doing CRCs. --*/
193 extern UInt32 BZ2_crc32Table[256];
195 #define BZ_INITIALISE_CRC(crcVar) \
197 crcVar = 0xffffffffL; \
200 #define BZ_FINALISE_CRC(crcVar) \
202 crcVar = ~(crcVar); \
205 #define BZ_UPDATE_CRC(crcVar,cha) \
207 crcVar = (crcVar << 8) ^ \
208 BZ2_crc32Table[(crcVar >> 24) ^ \
214 /*-- States and modes for compression. --*/
217 #define BZ_M_RUNNING 2
218 #define BZ_M_FLUSHING 3
219 #define BZ_M_FINISHING 4
221 #define BZ_S_OUTPUT 1
225 #define BZ_N_QSORT 12
226 #define BZ_N_SHELL 18
227 #define BZ_N_OVERSHOOT (BZ_N_RADIX + BZ_N_QSORT + BZ_N_SHELL + 2)
232 /*-- Structure holding all the compression-side stuff. --*/
236 /* pointer back to the struct bz_stream */
239 /* mode this stream is in, and whether inputting */
240 /* or outputting data */
244 /* remembers avail_in when flush/finish requested */
245 UInt32 avail_in_expect;
247 /* for doing the block sorting */
253 /* aliases for arr1 and arr2 */
259 /* for deciding when to use the fallback sorting algorithm */
262 /* run-length-encoding of the input */
267 /* input and output limits and current posns */
273 /* map of bytes used in block */
276 UChar unseqToSeq[256];
278 /* the buffer for bit stream creation */
282 /* block and combined CRCs */
286 /* misc administratium */
291 /* stuff for coding the MTF values */
293 Int32 mtfFreq [BZ_MAX_ALPHA_SIZE];
294 UChar selector [BZ_MAX_SELECTORS];
295 UChar selectorMtf[BZ_MAX_SELECTORS];
297 UChar len [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
298 Int32 code [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
299 Int32 rfreq [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
300 /* second dimension: only 3 needed; 4 makes index calculations faster */
301 UInt32 len_pack[BZ_MAX_ALPHA_SIZE][4];
308 /*-- externs for compression. --*/
311 BZ2_blockSort ( EState* );
314 BZ2_compressBlock ( EState*, Bool );
317 BZ2_bsInitWrite ( EState* );
320 BZ2_hbAssignCodes ( Int32*, UChar*, Int32, Int32, Int32 );
323 BZ2_hbMakeCodeLengths ( UChar*, Int32*, Int32, Int32 );
327 /*-- states for decompression. --*/
330 #define BZ_X_OUTPUT 2
332 #define BZ_X_MAGIC_1 10
333 #define BZ_X_MAGIC_2 11
334 #define BZ_X_MAGIC_3 12
335 #define BZ_X_MAGIC_4 13
336 #define BZ_X_BLKHDR_1 14
337 #define BZ_X_BLKHDR_2 15
338 #define BZ_X_BLKHDR_3 16
339 #define BZ_X_BLKHDR_4 17
340 #define BZ_X_BLKHDR_5 18
341 #define BZ_X_BLKHDR_6 19
342 #define BZ_X_BCRC_1 20
343 #define BZ_X_BCRC_2 21
344 #define BZ_X_BCRC_3 22
345 #define BZ_X_BCRC_4 23
346 #define BZ_X_RANDBIT 24
347 #define BZ_X_ORIGPTR_1 25
348 #define BZ_X_ORIGPTR_2 26
349 #define BZ_X_ORIGPTR_3 27
350 #define BZ_X_MAPPING_1 28
351 #define BZ_X_MAPPING_2 29
352 #define BZ_X_SELECTOR_1 30
353 #define BZ_X_SELECTOR_2 31
354 #define BZ_X_SELECTOR_3 32
355 #define BZ_X_CODING_1 33
356 #define BZ_X_CODING_2 34
357 #define BZ_X_CODING_3 35
358 #define BZ_X_MTF_1 36
359 #define BZ_X_MTF_2 37
360 #define BZ_X_MTF_3 38
361 #define BZ_X_MTF_4 39
362 #define BZ_X_MTF_5 40
363 #define BZ_X_MTF_6 41
364 #define BZ_X_ENDHDR_2 42
365 #define BZ_X_ENDHDR_3 43
366 #define BZ_X_ENDHDR_4 44
367 #define BZ_X_ENDHDR_5 45
368 #define BZ_X_ENDHDR_6 46
369 #define BZ_X_CCRC_1 47
370 #define BZ_X_CCRC_2 48
371 #define BZ_X_CCRC_3 49
372 #define BZ_X_CCRC_4 50
376 /*-- Constants for the fast MTF decoder. --*/
378 #define MTFA_SIZE 4096
383 /*-- Structure holding all the decompression-side stuff. --*/
387 /* pointer back to the struct bz_stream */
390 /* state indicator for this stream */
393 /* for doing the final run-length decoding */
396 Bool blockRandomised;
399 /* the buffer for bit stream reading */
403 /* misc administratium */
405 Bool smallDecompress;
409 /* for undoing the Burrows-Wheeler transform */
416 Int32 cftabCopy[257];
418 /* for undoing the Burrows-Wheeler transform (FAST) */
421 /* for undoing the Burrows-Wheeler transform (SMALL) */
425 /* stored and calculated CRCs */
426 UInt32 storedBlockCRC;
427 UInt32 storedCombinedCRC;
428 UInt32 calculatedBlockCRC;
429 UInt32 calculatedCombinedCRC;
431 /* map of bytes used in block */
435 UChar seqToUnseq[256];
437 /* for decoding the MTF values */
438 UChar mtfa [MTFA_SIZE];
439 Int32 mtfbase[256 / MTFL_SIZE];
440 UChar selector [BZ_MAX_SELECTORS];
441 UChar selectorMtf[BZ_MAX_SELECTORS];
442 UChar len [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
444 Int32 limit [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
445 Int32 base [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
446 Int32 perm [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
447 Int32 minLens[BZ_N_GROUPS];
449 /* save area for scalars in the main decompress code */
453 Int32 save_alphaSize;
455 Int32 save_nSelectors;
460 Int32 save_nblockMAX;
480 /*-- Macros for decompression. --*/
482 #define BZ_GET_FAST(cccc) \
483 s->tPos = s->tt[s->tPos]; \
484 cccc = (UChar)(s->tPos & 0xff); \
487 #define BZ_GET_FAST_C(cccc) \
488 c_tPos = c_tt[c_tPos]; \
489 cccc = (UChar)(c_tPos & 0xff); \
492 #define SET_LL4(i,n) \
493 { if (((i) & 0x1) == 0) \
494 s->ll4[(i) >> 1] = (s->ll4[(i) >> 1] & 0xf0) | (n); else \
495 s->ll4[(i) >> 1] = (s->ll4[(i) >> 1] & 0x0f) | ((n) << 4); \
499 ((((UInt32)(s->ll4[(i) >> 1])) >> (((i) << 2) & 0x4)) & 0xF)
501 #define SET_LL(i,n) \
502 { s->ll16[i] = (UInt16)(n & 0x0000ffff); \
503 SET_LL4(i, n >> 16); \
507 (((UInt32)s->ll16[i]) | (GET_LL4(i) << 16))
509 #define BZ_GET_SMALL(cccc) \
510 cccc = BZ2_indexIntoF ( s->tPos, s->cftab ); \
511 s->tPos = GET_LL(s->tPos);
514 /*-- externs for decompression. --*/
517 BZ2_indexIntoF ( Int32, Int32* );
520 BZ2_decompress ( DState* );
523 BZ2_hbCreateDecodeTables ( Int32*, Int32*, Int32*, UChar*,
524 Int32, Int32, Int32 );
530 /*-- BZ_NO_STDIO seems to make NULL disappear on some platforms. --*/
539 /*-------------------------------------------------------------*/
540 /*--- end bzlib_private.h ---*/
541 /*-------------------------------------------------------------*/