blob: b69eadc96a18a64aac319054b401451d5baf5a5a [file] [log] [blame]
wdenk710e3502003-08-29 20:57:53 +00001/*
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/
4 */
5
6/*-------------------------------------------------------------*/
7/*--- Private header file for the library. ---*/
8/*--- bzlib_private.h ---*/
9/*-------------------------------------------------------------*/
10
11/*--
12 This file is a part of bzip2 and/or libbzip2, a program and
13 library for lossless, block-sorting data compression.
14
15 Copyright (C) 1996-2002 Julian R Seward. All rights reserved.
16
17 Redistribution and use in source and binary forms, with or without
18 modification, are permitted provided that the following conditions
19 are met:
20
21 1. Redistributions of source code must retain the above copyright
22 notice, this list of conditions and the following disclaimer.
23
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.
28
29 3. Altered source versions must be plainly marked as such, and must
30 not be misrepresented as being the original software.
31
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
34 permission.
35
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.
47
48 Julian Seward, Cambridge, UK.
49 jseward@acm.org
50 bzip2/libbzip2 version 1.0 of 21 March 2000
51
52 This program is based on (at least) the work of:
53 Mike Burrows
54 David Wheeler
55 Peter Fenwick
56 Alistair Moffat
57 Radford Neal
58 Ian H. Witten
59 Robert Sedgewick
60 Jon L. Bentley
61
62 For more information on these sources, see the manual.
63--*/
64
wdenk710e3502003-08-29 20:57:53 +000065#ifndef _BZLIB_PRIVATE_H
66#define _BZLIB_PRIVATE_H
67
68#include <malloc.h>
69
70#include "bzlib.h"
71
72#ifndef BZ_NO_STDIO
73#include <stdio.h>
74#include <ctype.h>
75#include <string.h>
76#endif
77
wdenk710e3502003-08-29 20:57:53 +000078/*-- General stuff. --*/
79
80#define BZ_VERSION "1.0.2, 30-Dec-2001"
81
82typedef char Char;
83typedef unsigned char Bool;
84typedef unsigned char UChar;
85typedef int Int32;
86typedef unsigned int UInt32;
87typedef short Int16;
88typedef unsigned short UInt16;
89
90#define True ((Bool)1)
91#define False ((Bool)0)
92
93#ifndef __GNUC__
94#define __inline__ /* */
95#endif
96
97#ifndef BZ_NO_STDIO
98extern void BZ2_bz__AssertH__fail ( int errcode );
99#define AssertH(cond,errcode) \
100 { if (!(cond)) BZ2_bz__AssertH__fail ( errcode ); }
101#if BZ_DEBUG
102#define AssertD(cond,msg) \
103 { if (!(cond)) { \
104 fprintf ( stderr, \
wdenk9c53f402003-10-15 23:53:47 +0000105 "\n\nlibbzip2(debug build): internal error\n\t%s\n", msg );\
wdenk710e3502003-08-29 20:57:53 +0000106 exit(1); \
107 }}
108#else
109#define AssertD(cond,msg) /* */
110#endif
111#define VPrintf0(zf) \
112 fprintf(stderr,zf)
113#define VPrintf1(zf,za1) \
114 fprintf(stderr,zf,za1)
115#define VPrintf2(zf,za1,za2) \
116 fprintf(stderr,zf,za1,za2)
117#define VPrintf3(zf,za1,za2,za3) \
118 fprintf(stderr,zf,za1,za2,za3)
119#define VPrintf4(zf,za1,za2,za3,za4) \
120 fprintf(stderr,zf,za1,za2,za3,za4)
121#define VPrintf5(zf,za1,za2,za3,za4,za5) \
122 fprintf(stderr,zf,za1,za2,za3,za4,za5)
123#else
124extern void bz_internal_error ( int errcode );
125#define AssertH(cond,errcode) \
126 { if (!(cond)) bz_internal_error ( errcode ); }
127#define AssertD(cond,msg) /* */
128#define VPrintf0(zf) /* */
129#define VPrintf1(zf,za1) /* */
130#define VPrintf2(zf,za1,za2) /* */
131#define VPrintf3(zf,za1,za2,za3) /* */
132#define VPrintf4(zf,za1,za2,za3,za4) /* */
133#define VPrintf5(zf,za1,za2,za3,za4,za5) /* */
134#endif
135
wdenk710e3502003-08-29 20:57:53 +0000136#define BZALLOC(nnn) (strm->bzalloc)(strm->opaque,(nnn),1)
137#define BZFREE(ppp) (strm->bzfree)(strm->opaque,(ppp))
138
wdenk710e3502003-08-29 20:57:53 +0000139/*-- Header bytes. --*/
140
141#define BZ_HDR_B 0x42 /* 'B' */
142#define BZ_HDR_Z 0x5a /* 'Z' */
143#define BZ_HDR_h 0x68 /* 'h' */
144#define BZ_HDR_0 0x30 /* '0' */
145
146/*-- Constants for the back end. --*/
147
148#define BZ_MAX_ALPHA_SIZE 258
149#define BZ_MAX_CODE_LEN 23
150
151#define BZ_RUNA 0
152#define BZ_RUNB 1
153
154#define BZ_N_GROUPS 6
155#define BZ_G_SIZE 50
156#define BZ_N_ITERS 4
157
158#define BZ_MAX_SELECTORS (2 + (900000 / BZ_G_SIZE))
159
wdenk710e3502003-08-29 20:57:53 +0000160/*-- Stuff for randomising repetitive blocks. --*/
161
162extern Int32 BZ2_rNums[512];
163
164#define BZ_RAND_DECLS \
165 Int32 rNToGo; \
166 Int32 rTPos \
167
168#define BZ_RAND_INIT_MASK \
169 s->rNToGo = 0; \
170 s->rTPos = 0 \
171
172#define BZ_RAND_MASK ((s->rNToGo == 1) ? 1 : 0)
173
174#define BZ_RAND_UPD_MASK \
175 if (s->rNToGo == 0) { \
176 s->rNToGo = BZ2_rNums[s->rTPos]; \
177 s->rTPos++; \
178 if (s->rTPos == 512) s->rTPos = 0; \
179 } \
180 s->rNToGo--;
181
wdenk710e3502003-08-29 20:57:53 +0000182/*-- Stuff for doing CRCs. --*/
183
184extern UInt32 BZ2_crc32Table[256];
185
186#define BZ_INITIALISE_CRC(crcVar) \
187{ \
188 crcVar = 0xffffffffL; \
189}
190
191#define BZ_FINALISE_CRC(crcVar) \
192{ \
193 crcVar = ~(crcVar); \
194}
195
196#define BZ_UPDATE_CRC(crcVar,cha) \
197{ \
198 crcVar = (crcVar << 8) ^ \
wdenk9c53f402003-10-15 23:53:47 +0000199 BZ2_crc32Table[(crcVar >> 24) ^ \
200 ((UChar)cha)]; \
wdenk710e3502003-08-29 20:57:53 +0000201}
202
wdenk710e3502003-08-29 20:57:53 +0000203/*-- States and modes for compression. --*/
204
205#define BZ_M_IDLE 1
206#define BZ_M_RUNNING 2
207#define BZ_M_FLUSHING 3
208#define BZ_M_FINISHING 4
209
210#define BZ_S_OUTPUT 1
211#define BZ_S_INPUT 2
212
213#define BZ_N_RADIX 2
214#define BZ_N_QSORT 12
215#define BZ_N_SHELL 18
216#define BZ_N_OVERSHOOT (BZ_N_RADIX + BZ_N_QSORT + BZ_N_SHELL + 2)
217
wdenk710e3502003-08-29 20:57:53 +0000218/*-- Structure holding all the compression-side stuff. --*/
219
220typedef
221 struct {
222 /* pointer back to the struct bz_stream */
223 bz_stream* strm;
224
225 /* mode this stream is in, and whether inputting */
226 /* or outputting data */
227 Int32 mode;
228 Int32 state;
229
230 /* remembers avail_in when flush/finish requested */
231 UInt32 avail_in_expect;
232
233 /* for doing the block sorting */
234 UInt32* arr1;
235 UInt32* arr2;
236 UInt32* ftab;
237 Int32 origPtr;
238
239 /* aliases for arr1 and arr2 */
240 UInt32* ptr;
241 UChar* block;
242 UInt16* mtfv;
243 UChar* zbits;
244
245 /* for deciding when to use the fallback sorting algorithm */
246 Int32 workFactor;
247
248 /* run-length-encoding of the input */
249 UInt32 state_in_ch;
250 Int32 state_in_len;
251 BZ_RAND_DECLS;
252
253 /* input and output limits and current posns */
254 Int32 nblock;
255 Int32 nblockMAX;
256 Int32 numZ;
257 Int32 state_out_pos;
258
259 /* map of bytes used in block */
260 Int32 nInUse;
261 Bool inUse[256];
262 UChar unseqToSeq[256];
263
264 /* the buffer for bit stream creation */
265 UInt32 bsBuff;
266 Int32 bsLive;
267
268 /* block and combined CRCs */
269 UInt32 blockCRC;
270 UInt32 combinedCRC;
271
272 /* misc administratium */
273 Int32 verbosity;
274 Int32 blockNo;
275 Int32 blockSize100k;
276
277 /* stuff for coding the MTF values */
278 Int32 nMTF;
279 Int32 mtfFreq [BZ_MAX_ALPHA_SIZE];
280 UChar selector [BZ_MAX_SELECTORS];
281 UChar selectorMtf[BZ_MAX_SELECTORS];
282
283 UChar len [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
284 Int32 code [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
285 Int32 rfreq [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
286 /* second dimension: only 3 needed; 4 makes index calculations faster */
287 UInt32 len_pack[BZ_MAX_ALPHA_SIZE][4];
288
289 }
290 EState;
291
wdenk710e3502003-08-29 20:57:53 +0000292/*-- externs for compression. --*/
293
294extern void
295BZ2_blockSort ( EState* );
296
297extern void
298BZ2_compressBlock ( EState*, Bool );
299
300extern void
301BZ2_bsInitWrite ( EState* );
302
303extern void
304BZ2_hbAssignCodes ( Int32*, UChar*, Int32, Int32, Int32 );
305
306extern void
307BZ2_hbMakeCodeLengths ( UChar*, Int32*, Int32, Int32 );
308
wdenk710e3502003-08-29 20:57:53 +0000309/*-- states for decompression. --*/
310
311#define BZ_X_IDLE 1
312#define BZ_X_OUTPUT 2
313
314#define BZ_X_MAGIC_1 10
315#define BZ_X_MAGIC_2 11
316#define BZ_X_MAGIC_3 12
317#define BZ_X_MAGIC_4 13
318#define BZ_X_BLKHDR_1 14
319#define BZ_X_BLKHDR_2 15
320#define BZ_X_BLKHDR_3 16
321#define BZ_X_BLKHDR_4 17
322#define BZ_X_BLKHDR_5 18
323#define BZ_X_BLKHDR_6 19
324#define BZ_X_BCRC_1 20
325#define BZ_X_BCRC_2 21
326#define BZ_X_BCRC_3 22
327#define BZ_X_BCRC_4 23
328#define BZ_X_RANDBIT 24
329#define BZ_X_ORIGPTR_1 25
330#define BZ_X_ORIGPTR_2 26
331#define BZ_X_ORIGPTR_3 27
332#define BZ_X_MAPPING_1 28
333#define BZ_X_MAPPING_2 29
334#define BZ_X_SELECTOR_1 30
335#define BZ_X_SELECTOR_2 31
336#define BZ_X_SELECTOR_3 32
337#define BZ_X_CODING_1 33
338#define BZ_X_CODING_2 34
339#define BZ_X_CODING_3 35
340#define BZ_X_MTF_1 36
341#define BZ_X_MTF_2 37
342#define BZ_X_MTF_3 38
343#define BZ_X_MTF_4 39
344#define BZ_X_MTF_5 40
345#define BZ_X_MTF_6 41
346#define BZ_X_ENDHDR_2 42
347#define BZ_X_ENDHDR_3 43
348#define BZ_X_ENDHDR_4 44
349#define BZ_X_ENDHDR_5 45
350#define BZ_X_ENDHDR_6 46
351#define BZ_X_CCRC_1 47
352#define BZ_X_CCRC_2 48
353#define BZ_X_CCRC_3 49
354#define BZ_X_CCRC_4 50
355
wdenk710e3502003-08-29 20:57:53 +0000356/*-- Constants for the fast MTF decoder. --*/
357
358#define MTFA_SIZE 4096
359#define MTFL_SIZE 16
360
wdenk710e3502003-08-29 20:57:53 +0000361/*-- Structure holding all the decompression-side stuff. --*/
362
363typedef
364 struct {
365 /* pointer back to the struct bz_stream */
366 bz_stream* strm;
367
368 /* state indicator for this stream */
369 Int32 state;
370
371 /* for doing the final run-length decoding */
372 UChar state_out_ch;
373 Int32 state_out_len;
374 Bool blockRandomised;
375 BZ_RAND_DECLS;
376
377 /* the buffer for bit stream reading */
378 UInt32 bsBuff;
379 Int32 bsLive;
380
381 /* misc administratium */
382 Int32 blockSize100k;
383 Bool smallDecompress;
384 Int32 currBlockNo;
385 Int32 verbosity;
386
387 /* for undoing the Burrows-Wheeler transform */
388 Int32 origPtr;
389 UInt32 tPos;
390 Int32 k0;
391 Int32 unzftab[256];
392 Int32 nblock_used;
393 Int32 cftab[257];
394 Int32 cftabCopy[257];
395
396 /* for undoing the Burrows-Wheeler transform (FAST) */
397 UInt32 *tt;
398
399 /* for undoing the Burrows-Wheeler transform (SMALL) */
400 UInt16 *ll16;
401 UChar *ll4;
402
403 /* stored and calculated CRCs */
404 UInt32 storedBlockCRC;
405 UInt32 storedCombinedCRC;
406 UInt32 calculatedBlockCRC;
407 UInt32 calculatedCombinedCRC;
408
409 /* map of bytes used in block */
410 Int32 nInUse;
411 Bool inUse[256];
412 Bool inUse16[16];
413 UChar seqToUnseq[256];
414
415 /* for decoding the MTF values */
416 UChar mtfa [MTFA_SIZE];
417 Int32 mtfbase[256 / MTFL_SIZE];
418 UChar selector [BZ_MAX_SELECTORS];
419 UChar selectorMtf[BZ_MAX_SELECTORS];
420 UChar len [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
421
422 Int32 limit [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
423 Int32 base [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
424 Int32 perm [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
425 Int32 minLens[BZ_N_GROUPS];
426
427 /* save area for scalars in the main decompress code */
428 Int32 save_i;
429 Int32 save_j;
430 Int32 save_t;
431 Int32 save_alphaSize;
432 Int32 save_nGroups;
433 Int32 save_nSelectors;
434 Int32 save_EOB;
435 Int32 save_groupNo;
436 Int32 save_groupPos;
437 Int32 save_nextSym;
438 Int32 save_nblockMAX;
439 Int32 save_nblock;
440 Int32 save_es;
441 Int32 save_N;
442 Int32 save_curr;
443 Int32 save_zt;
444 Int32 save_zn;
445 Int32 save_zvec;
446 Int32 save_zj;
447 Int32 save_gSel;
448 Int32 save_gMinlen;
449 Int32* save_gLimit;
450 Int32* save_gBase;
451 Int32* save_gPerm;
452
453 }
454 DState;
455
wdenk710e3502003-08-29 20:57:53 +0000456/*-- Macros for decompression. --*/
457
458#define BZ_GET_FAST(cccc) \
459 s->tPos = s->tt[s->tPos]; \
460 cccc = (UChar)(s->tPos & 0xff); \
461 s->tPos >>= 8;
462
463#define BZ_GET_FAST_C(cccc) \
464 c_tPos = c_tt[c_tPos]; \
465 cccc = (UChar)(c_tPos & 0xff); \
466 c_tPos >>= 8;
467
468#define SET_LL4(i,n) \
469 { if (((i) & 0x1) == 0) \
wdenk9c53f402003-10-15 23:53:47 +0000470 s->ll4[(i) >> 1] = (s->ll4[(i) >> 1] & 0xf0) | (n); else \
471 s->ll4[(i) >> 1] = (s->ll4[(i) >> 1] & 0x0f) | ((n) << 4); \
wdenk710e3502003-08-29 20:57:53 +0000472 }
473
474#define GET_LL4(i) \
475 ((((UInt32)(s->ll4[(i) >> 1])) >> (((i) << 2) & 0x4)) & 0xF)
476
477#define SET_LL(i,n) \
478 { s->ll16[i] = (UInt16)(n & 0x0000ffff); \
479 SET_LL4(i, n >> 16); \
480 }
481
482#define GET_LL(i) \
483 (((UInt32)s->ll16[i]) | (GET_LL4(i) << 16))
484
485#define BZ_GET_SMALL(cccc) \
486 cccc = BZ2_indexIntoF ( s->tPos, s->cftab ); \
487 s->tPos = GET_LL(s->tPos);
488
wdenk710e3502003-08-29 20:57:53 +0000489/*-- externs for decompression. --*/
490
491extern Int32
492BZ2_indexIntoF ( Int32, Int32* );
493
494extern Int32
495BZ2_decompress ( DState* );
496
497extern void
498BZ2_hbCreateDecodeTables ( Int32*, Int32*, Int32*, UChar*,
wdenk9c53f402003-10-15 23:53:47 +0000499 Int32, Int32, Int32 );
wdenk710e3502003-08-29 20:57:53 +0000500
wdenk710e3502003-08-29 20:57:53 +0000501#endif
502
wdenk710e3502003-08-29 20:57:53 +0000503/*-- BZ_NO_STDIO seems to make NULL disappear on some platforms. --*/
504
505#ifdef BZ_NO_STDIO
506#ifndef NULL
507#define NULL 0
508#endif
509#endif
510
wdenk710e3502003-08-29 20:57:53 +0000511/*-------------------------------------------------------------*/
512/*--- end bzlib_private.h ---*/
513/*-------------------------------------------------------------*/