2 * THIS FILE IS NOT IDENTICAL TO THE ORIGINAL
3 * FROM THE BZIP2 DISTRIBUTION.
5 * It has been modified, mainly to break the library
9 * rsc@plan9.bell-labs.com
14 /*-------------------------------------------------------------*/
15 /*--- Private header file for the library. ---*/
16 /*--- bzlib_private.h ---*/
17 /*-------------------------------------------------------------*/
20 This file is a part of bzip2 and/or libbzip2, a program and
21 library for lossless, block-sorting data compression.
23 Copyright (C) 1996-2000 Julian R Seward. All rights reserved.
25 Redistribution and use in source and binary forms, with or without
26 modification, are permitted provided that the following conditions
29 1. Redistributions of source code must retain the above copyright
30 notice, this list of conditions and the following disclaimer.
32 2. The origin of this software must not be misrepresented; you must
33 not claim that you wrote the original software. If you use this
34 software in a product, an acknowledgment in the product
35 documentation would be appreciated but is not required.
37 3. Altered source versions must be plainly marked as such, and must
38 not be misrepresented as being the original software.
40 4. The name of the author may not be used to endorse or promote
41 products derived from this software without specific prior written
44 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
45 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
46 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
48 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
50 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
51 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
52 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
53 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
54 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
56 Julian Seward, Cambridge, UK.
58 bzip2/libbzip2 version 1.0 of 21 March 2000
60 This program is based on (at least) the work of:
70 For more information on these sources, see the manual.
74 #ifndef _BZLIB_PRIVATE_H
75 #define _BZLIB_PRIVATE_H
77 /*-- General stuff. --*/
79 #define BZ_VERSION "1.0.1, 23-June-2000"
82 #define __inline__ /* */
85 /* these #defines can be overridden by bzlib_stdio.h */
86 extern void bz_internal_error ( int errcode );
87 #define AssertH(cond,errcode) \
88 { if (!(cond)) bz_internal_error ( errcode ); }
89 #define AssertD(cond,msg) /* */
90 #define VPrintf0(zf) /* */
91 #define VPrintf1(zf,za1) /* */
92 #define VPrintf2(zf,za1,za2) /* */
93 #define VPrintf3(zf,za1,za2,za3) /* */
94 #define VPrintf4(zf,za1,za2,za3,za4) /* */
95 #define VPrintf5(zf,za1,za2,za3,za4,za5) /* */
97 #define BZALLOC(nnn) (strm->bzalloc)(strm->opaque,(nnn),1)
98 #define BZFREE(ppp) (strm->bzfree)(strm->opaque,(ppp))
101 /*-- Constants for the back end. --*/
103 #define BZ_MAX_ALPHA_SIZE 258
104 #define BZ_MAX_CODE_LEN 23
109 #define BZ_N_GROUPS 6
113 #define BZ_MAX_SELECTORS (2 + (900000 / BZ_G_SIZE))
117 /*-- Stuff for randomising repetitive blocks. --*/
119 extern Int32 BZ2_rNums[512];
121 #define BZ_RAND_DECLS \
125 #define BZ_RAND_INIT_MASK \
129 #define BZ_RAND_MASK ((s->rNToGo == 1) ? 1 : 0)
131 #define BZ_RAND_UPD_MASK \
132 if (s->rNToGo == 0) { \
133 s->rNToGo = BZ2_rNums[s->rTPos]; \
135 if (s->rTPos == 512) s->rTPos = 0; \
141 /*-- Stuff for doing CRCs. --*/
143 extern UInt32 BZ2_crc32Table[256];
145 #define BZ_INITIALISE_CRC(crcVar) \
147 crcVar = 0xffffffffL; \
150 #define BZ_FINALISE_CRC(crcVar) \
152 crcVar = ~(crcVar); \
155 #define BZ_UPDATE_CRC(crcVar,cha) \
157 crcVar = (crcVar << 8) ^ \
158 BZ2_crc32Table[(crcVar >> 24) ^ \
164 /*-- States and modes for compression. --*/
167 #define BZ_M_RUNNING 2
168 #define BZ_M_FLUSHING 3
169 #define BZ_M_FINISHING 4
171 #define BZ_S_OUTPUT 1
175 #define BZ_N_QSORT 12
176 #define BZ_N_SHELL 18
177 #define BZ_N_OVERSHOOT (BZ_N_RADIX + BZ_N_QSORT + BZ_N_SHELL + 2)
182 /*-- Structure holding all the compression-side stuff. --*/
186 /* pointer back to the struct bz_stream */
189 /* mode this stream is in, and whether inputting */
190 /* or outputting data */
194 /* remembers avail_in when flush/finish requested */
195 UInt32 avail_in_expect;
197 /* for doing the block sorting */
203 /* aliases for arr1 and arr2 */
209 /* for deciding when to use the fallback sorting algorithm */
212 /* run-length-encoding of the input */
217 /* input and output limits and current posns */
223 /* map of bytes used in block */
226 UChar unseqToSeq[256];
228 /* the buffer for bit stream creation */
232 /* block and combined CRCs */
236 /* misc administratium */
241 /* stuff for coding the MTF values */
243 Int32 mtfFreq [BZ_MAX_ALPHA_SIZE];
244 UChar selector [BZ_MAX_SELECTORS];
245 UChar selectorMtf[BZ_MAX_SELECTORS];
247 UChar len [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
248 Int32 code [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
249 Int32 rfreq [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
250 /* second dimension: only 3 needed; 4 makes index calculations faster */
251 UInt32 len_pack[BZ_MAX_ALPHA_SIZE][4];
258 /*-- externs for compression. --*/
261 BZ2_blockSort ( EState* );
264 BZ2_compressBlock ( EState*, Bool );
267 BZ2_bsInitWrite ( EState* );
270 BZ2_hbAssignCodes ( Int32*, UChar*, Int32, Int32, Int32 );
273 BZ2_hbMakeCodeLengths ( UChar*, Int32*, Int32, Int32 );
277 /*-- states for decompression. --*/
280 #define BZ_X_OUTPUT 2
282 #define BZ_X_MAGIC_1 10
283 #define BZ_X_MAGIC_2 11
284 #define BZ_X_MAGIC_3 12
285 #define BZ_X_MAGIC_4 13
286 #define BZ_X_BLKHDR_1 14
287 #define BZ_X_BLKHDR_2 15
288 #define BZ_X_BLKHDR_3 16
289 #define BZ_X_BLKHDR_4 17
290 #define BZ_X_BLKHDR_5 18
291 #define BZ_X_BLKHDR_6 19
292 #define BZ_X_BCRC_1 20
293 #define BZ_X_BCRC_2 21
294 #define BZ_X_BCRC_3 22
295 #define BZ_X_BCRC_4 23
296 #define BZ_X_RANDBIT 24
297 #define BZ_X_ORIGPTR_1 25
298 #define BZ_X_ORIGPTR_2 26
299 #define BZ_X_ORIGPTR_3 27
300 #define BZ_X_MAPPING_1 28
301 #define BZ_X_MAPPING_2 29
302 #define BZ_X_SELECTOR_1 30
303 #define BZ_X_SELECTOR_2 31
304 #define BZ_X_SELECTOR_3 32
305 #define BZ_X_CODING_1 33
306 #define BZ_X_CODING_2 34
307 #define BZ_X_CODING_3 35
308 #define BZ_X_MTF_1 36
309 #define BZ_X_MTF_2 37
310 #define BZ_X_MTF_3 38
311 #define BZ_X_MTF_4 39
312 #define BZ_X_MTF_5 40
313 #define BZ_X_MTF_6 41
314 #define BZ_X_ENDHDR_2 42
315 #define BZ_X_ENDHDR_3 43
316 #define BZ_X_ENDHDR_4 44
317 #define BZ_X_ENDHDR_5 45
318 #define BZ_X_ENDHDR_6 46
319 #define BZ_X_CCRC_1 47
320 #define BZ_X_CCRC_2 48
321 #define BZ_X_CCRC_3 49
322 #define BZ_X_CCRC_4 50
326 /*-- Constants for the fast MTF decoder. --*/
328 #define MTFA_SIZE 4096
333 /*-- Structure holding all the decompression-side stuff. --*/
337 /* pointer back to the struct bz_stream */
340 /* state indicator for this stream */
343 /* for doing the final run-length decoding */
346 Bool blockRandomised;
349 /* the buffer for bit stream reading */
353 /* misc administratium */
355 Bool smallDecompress;
359 /* for undoing the Burrows-Wheeler transform */
366 Int32 cftabCopy[257];
368 /* for undoing the Burrows-Wheeler transform (FAST) */
371 /* for undoing the Burrows-Wheeler transform (SMALL) */
375 /* stored and calculated CRCs */
376 UInt32 storedBlockCRC;
377 UInt32 storedCombinedCRC;
378 UInt32 calculatedBlockCRC;
379 UInt32 calculatedCombinedCRC;
381 /* map of bytes used in block */
385 UChar seqToUnseq[256];
387 /* for decoding the MTF values */
388 UChar mtfa [MTFA_SIZE];
389 Int32 mtfbase[256 / MTFL_SIZE];
390 UChar selector [BZ_MAX_SELECTORS];
391 UChar selectorMtf[BZ_MAX_SELECTORS];
392 UChar len [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
394 Int32 limit [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
395 Int32 base [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
396 Int32 perm [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
397 Int32 minLens[BZ_N_GROUPS];
399 /* save area for scalars in the main decompress code */
403 Int32 save_alphaSize;
405 Int32 save_nSelectors;
410 Int32 save_nblockMAX;
430 /*-- Macros for decompression. --*/
432 #define BZ_GET_FAST(cccc) \
433 s->tPos = s->tt[s->tPos]; \
434 cccc = (UChar)(s->tPos & 0xff); \
437 #define BZ_GET_FAST_C(cccc) \
438 c_tPos = c_tt[c_tPos]; \
439 cccc = (UChar)(c_tPos & 0xff); \
442 #define SET_LL4(i,n) \
443 { if (((i) & 0x1) == 0) \
444 s->ll4[(i) >> 1] = (s->ll4[(i) >> 1] & 0xf0) | (n); else \
445 s->ll4[(i) >> 1] = (s->ll4[(i) >> 1] & 0x0f) | ((n) << 4); \
449 ((((UInt32)(s->ll4[(i) >> 1])) >> (((i) << 2) & 0x4)) & 0xF)
451 #define SET_LL(i,n) \
452 { s->ll16[i] = (UInt16)(n & 0x0000ffff); \
453 SET_LL4(i, n >> 16); \
457 (((UInt32)s->ll16[i]) | (GET_LL4(i) << 16))
459 #define BZ_GET_SMALL(cccc) \
460 cccc = BZ2_indexIntoF ( s->tPos, s->cftab ); \
461 s->tPos = GET_LL(s->tPos);
464 /*-- externs for decompression. --*/
467 BZ2_indexIntoF ( Int32, Int32* );
470 BZ2_decompress ( DState* );
473 BZ2_hbCreateDecodeTables ( Int32*, Int32*, Int32*, UChar*,
474 Int32, Int32, Int32 );
479 /* sometimes not including <stdio.h> makes this necessary */
484 /* internal library functions */
486 bz_config_ok( void );
489 default_bzalloc( void*, Int32, Int32 );
492 default_bzfree( void*, void* );
494 /*-------------------------------------------------------------*/
495 /*--- end bzlib_private.h ---*/
496 /*-------------------------------------------------------------*/