xref: /plan9/sys/src/cmd/bzip2/lib/bzlib_private.h (revision 59cc4ca53493a3c6d2349fe2b7f7c40f7dce7294)
1 /*
2  * THIS FILE IS NOT IDENTICAL TO THE ORIGINAL
3  * FROM THE BZIP2 DISTRIBUTION.
4  *
5  * It has been modified, mainly to break the library
6  * into smaller pieces.
7  *
8  * Russ Cox
9  * rsc@plan9.bell-labs.com
10  * July 2000
11  */
12 
13 
14 /*-------------------------------------------------------------*/
15 /*--- Private header file for the library.                  ---*/
16 /*---                                       bzlib_private.h ---*/
17 /*-------------------------------------------------------------*/
18 
19 /*--
20   This file is a part of bzip2 and/or libbzip2, a program and
21   library for lossless, block-sorting data compression.
22 
23   Copyright (C) 1996-2000 Julian R Seward.  All rights reserved.
24 
25   Redistribution and use in source and binary forms, with or without
26   modification, are permitted provided that the following conditions
27   are met:
28 
29   1. Redistributions of source code must retain the above copyright
30      notice, this list of conditions and the following disclaimer.
31 
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.
36 
37   3. Altered source versions must be plainly marked as such, and must
38      not be misrepresented as being the original software.
39 
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
42      permission.
43 
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.
55 
56   Julian Seward, Cambridge, UK.
57   jseward@acm.org
58   bzip2/libbzip2 version 1.0 of 21 March 2000
59 
60   This program is based on (at least) the work of:
61      Mike Burrows
62      David Wheeler
63      Peter Fenwick
64      Alistair Moffat
65      Radford Neal
66      Ian H. Witten
67      Robert Sedgewick
68      Jon L. Bentley
69 
70   For more information on these sources, see the manual.
71 --*/
72 
73 
74 #ifndef _BZLIB_PRIVATE_H
75 #define _BZLIB_PRIVATE_H
76 
77 /*-- General stuff. --*/
78 
79 #define BZ_VERSION  "1.0.1, 23-June-2000"
80 
81 #ifndef __GNUC__
82 #define __inline__  /* */
83 #endif
84 
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) /* */
96 
97 #define BZALLOC(nnn) (strm->bzalloc)(strm->opaque,(nnn),1)
98 #define BZFREE(ppp)  (strm->bzfree)(strm->opaque,(ppp))
99 
100 
101 /*-- Constants for the back end. --*/
102 
103 #define BZ_MAX_ALPHA_SIZE 258
104 #define BZ_MAX_CODE_LEN    23
105 
106 #define BZ_RUNA 0
107 #define BZ_RUNB 1
108 
109 #define BZ_N_GROUPS 6
110 #define BZ_G_SIZE   50
111 #define BZ_N_ITERS  4
112 
113 #define BZ_MAX_SELECTORS (2 + (900000 / BZ_G_SIZE))
114 
115 
116 
117 /*-- Stuff for randomising repetitive blocks. --*/
118 
119 extern Int32 BZ2_rNums[512];
120 
121 #define BZ_RAND_DECLS                          \
122    Int32 rNToGo;                               \
123    Int32 rTPos                                 \
124 
125 #define BZ_RAND_INIT_MASK                      \
126    s->rNToGo = 0;                              \
127    s->rTPos  = 0                               \
128 
129 #define BZ_RAND_MASK ((s->rNToGo == 1) ? 1 : 0)
130 
131 #define BZ_RAND_UPD_MASK                       \
132    if (s->rNToGo == 0) {                       \
133       s->rNToGo = BZ2_rNums[s->rTPos];         \
134       s->rTPos++;                              \
135       if (s->rTPos == 512) s->rTPos = 0;       \
136    }                                           \
137    s->rNToGo--;
138 
139 
140 
141 /*-- Stuff for doing CRCs. --*/
142 
143 extern UInt32 BZ2_crc32Table[256];
144 
145 #define BZ_INITIALISE_CRC(crcVar)              \
146 {                                              \
147    crcVar = 0xffffffffL;                       \
148 }
149 
150 #define BZ_FINALISE_CRC(crcVar)                \
151 {                                              \
152    crcVar = ~(crcVar);                         \
153 }
154 
155 #define BZ_UPDATE_CRC(crcVar,cha)              \
156 {                                              \
157    crcVar = (crcVar << 8) ^                    \
158             BZ2_crc32Table[(crcVar >> 24) ^    \
159                            ((UChar)cha)];      \
160 }
161 
162 
163 
164 /*-- States and modes for compression. --*/
165 
166 #define BZ_M_IDLE      1
167 #define BZ_M_RUNNING   2
168 #define BZ_M_FLUSHING  3
169 #define BZ_M_FINISHING 4
170 
171 #define BZ_S_OUTPUT    1
172 #define BZ_S_INPUT     2
173 
174 #define BZ_N_RADIX 2
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)
178 
179 
180 
181 
182 /*-- Structure holding all the compression-side stuff. --*/
183 
184 typedef
185    struct {
186       /* pointer back to the struct bz_stream */
187       bz_stream* strm;
188 
189       /* mode this stream is in, and whether inputting */
190       /* or outputting data */
191       Int32    mode;
192       Int32    state;
193 
194       /* remembers avail_in when flush/finish requested */
195       UInt32   avail_in_expect;
196 
197       /* for doing the block sorting */
198       UInt32*  arr1;
199       UInt32*  arr2;
200       UInt32*  ftab;
201       Int32    origPtr;
202 
203       /* aliases for arr1 and arr2 */
204       UInt32*  ptr;
205       UChar*   block;
206       UInt16*  mtfv;
207       UChar*   zbits;
208 
209       /* for deciding when to use the fallback sorting algorithm */
210       Int32    workFactor;
211 
212       /* run-length-encoding of the input */
213       UInt32   state_in_ch;
214       Int32    state_in_len;
215       BZ_RAND_DECLS;
216 
217       /* input and output limits and current posns */
218       Int32    nblock;
219       Int32    nblockMAX;
220       Int32    numZ;
221       Int32    state_out_pos;
222 
223       /* map of bytes used in block */
224       Int32    nInUse;
225       Bool     inUse[256];
226       UChar    unseqToSeq[256];
227 
228       /* the buffer for bit stream creation */
229       UInt32   bsBuff;
230       Int32    bsLive;
231 
232       /* block and combined CRCs */
233       UInt32   blockCRC;
234       UInt32   combinedCRC;
235 
236       /* misc administratium */
237       Int32    verbosity;
238       Int32    blockNo;
239       Int32    blockSize100k;
240 
241       /* stuff for coding the MTF values */
242       Int32    nMTF;
243       Int32    mtfFreq    [BZ_MAX_ALPHA_SIZE];
244       UChar    selector   [BZ_MAX_SELECTORS];
245       UChar    selectorMtf[BZ_MAX_SELECTORS];
246 
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];
252 
253    }
254    EState;
255 
256 
257 
258 /*-- externs for compression. --*/
259 
260 extern void
261 BZ2_blockSort ( EState* );
262 
263 extern void
264 BZ2_compressBlock ( EState*, Bool );
265 
266 extern void
267 BZ2_bsInitWrite ( EState* );
268 
269 extern void
270 BZ2_hbAssignCodes ( Int32*, UChar*, Int32, Int32, Int32 );
271 
272 extern void
273 BZ2_hbMakeCodeLengths ( UChar*, Int32*, Int32, Int32 );
274 
275 
276 
277 /*-- states for decompression. --*/
278 
279 #define BZ_X_IDLE        1
280 #define BZ_X_OUTPUT      2
281 
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
323 
324 
325 
326 /*-- Constants for the fast MTF decoder. --*/
327 
328 #define MTFA_SIZE 4096
329 #define MTFL_SIZE 16
330 
331 
332 
333 /*-- Structure holding all the decompression-side stuff. --*/
334 
335 typedef
336    struct {
337       /* pointer back to the struct bz_stream */
338       bz_stream* strm;
339 
340       /* state indicator for this stream */
341       Int32    state;
342 
343       /* for doing the final run-length decoding */
344       UChar    state_out_ch;
345       Int32    state_out_len;
346       Bool     blockRandomised;
347       BZ_RAND_DECLS;
348 
349       /* the buffer for bit stream reading */
350       UInt32   bsBuff;
351       Int32    bsLive;
352 
353       /* misc administratium */
354       Int32    blockSize100k;
355       Bool     smallDecompress;
356       Int32    currBlockNo;
357       Int32    verbosity;
358 
359       /* for undoing the Burrows-Wheeler transform */
360       Int32    origPtr;
361       UInt32   tPos;
362       Int32    k0;
363       Int32    unzftab[256];
364       Int32    nblock_used;
365       Int32    cftab[257];
366       Int32    cftabCopy[257];
367 
368       /* for undoing the Burrows-Wheeler transform (FAST) */
369       UInt32   *tt;
370 
371       /* for undoing the Burrows-Wheeler transform (SMALL) */
372       UInt16   *ll16;
373       UChar    *ll4;
374 
375       /* stored and calculated CRCs */
376       UInt32   storedBlockCRC;
377       UInt32   storedCombinedCRC;
378       UInt32   calculatedBlockCRC;
379       UInt32   calculatedCombinedCRC;
380 
381       /* map of bytes used in block */
382       Int32    nInUse;
383       Bool     inUse[256];
384       Bool     inUse16[16];
385       UChar    seqToUnseq[256];
386 
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];
393 
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];
398 
399       /* save area for scalars in the main decompress code */
400       Int32    save_i;
401       Int32    save_j;
402       Int32    save_t;
403       Int32    save_alphaSize;
404       Int32    save_nGroups;
405       Int32    save_nSelectors;
406       Int32    save_EOB;
407       Int32    save_groupNo;
408       Int32    save_groupPos;
409       Int32    save_nextSym;
410       Int32    save_nblockMAX;
411       Int32    save_nblock;
412       Int32    save_es;
413       Int32    save_N;
414       Int32    save_curr;
415       Int32    save_zt;
416       Int32    save_zn;
417       Int32    save_zvec;
418       Int32    save_zj;
419       Int32    save_gSel;
420       Int32    save_gMinlen;
421       Int32*   save_gLimit;
422       Int32*   save_gBase;
423       Int32*   save_gPerm;
424 
425    }
426    DState;
427 
428 
429 
430 /*-- Macros for decompression. --*/
431 
432 #define BZ_GET_FAST(cccc)                     \
433     s->tPos = s->tt[s->tPos];                 \
434     cccc = (UChar)(s->tPos & 0xff);           \
435     s->tPos >>= 8;
436 
437 #define BZ_GET_FAST_C(cccc)                   \
438     c_tPos = c_tt[c_tPos];                    \
439     cccc = (UChar)(c_tPos & 0xff);            \
440     c_tPos >>= 8;
441 
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);  \
446    }
447 
448 #define GET_LL4(i)                             \
449    ((((UInt32)(s->ll4[(i) >> 1])) >> (((i) << 2) & 0x4)) & 0xF)
450 
451 #define SET_LL(i,n)                          \
452    { s->ll16[i] = (UInt16)(n & 0x0000ffff);  \
453      SET_LL4(i, n >> 16);                    \
454    }
455 
456 #define GET_LL(i) \
457    (((UInt32)s->ll16[i]) | (GET_LL4(i) << 16))
458 
459 #define BZ_GET_SMALL(cccc)                            \
460       cccc = BZ2_indexIntoF ( s->tPos, s->cftab );    \
461       s->tPos = GET_LL(s->tPos);
462 
463 
464 /*-- externs for decompression. --*/
465 
466 extern Int32
467 BZ2_indexIntoF ( Int32, Int32* );
468 
469 extern Int32
470 BZ2_decompress ( DState* );
471 
472 extern void
473 BZ2_hbCreateDecodeTables ( Int32*, Int32*, Int32*, UChar*,
474                            Int32,  Int32, Int32 );
475 
476 
477 #endif
478 
479 /* sometimes not including <stdio.h> makes this necessary */
480 #ifndef NULL
481 #define NULL 0
482 #endif
483 
484 /* internal library functions */
485 extern int
486 bz_config_ok( void );
487 
488 extern void*
489 default_bzalloc( void*, Int32, Int32 );
490 
491 extern void
492 default_bzfree( void*, void* );
493 
494 /*-------------------------------------------------------------*/
495 /*--- end                                   bzlib_private.h ---*/
496 /*-------------------------------------------------------------*/
497