2 * bzip2 is written by Julian Seward <jseward@bzip.org>.
3 * Adapted for busybox by Denys Vlasenko <vda.linux@googlemail.com>.
4 * See README and LICENSE files in this directory for more information.
7 /*-------------------------------------------------------------*/
8 /*--- Private header file for the library. ---*/
9 /*--- bzlib_private.h ---*/
10 /*-------------------------------------------------------------*/
12 /* ------------------------------------------------------------------
13 This file is part of bzip2/libbzip2, a program and library for
14 lossless, block-sorting data compression.
16 bzip2/libbzip2 version 1.0.4 of 20 December 2006
17 Copyright (C) 1996-2006 Julian Seward <jseward@bzip.org>
19 Please read the WARNING, DISCLAIMER and PATENTS sections in the
22 This program is released under the terms of the license contained
24 ------------------------------------------------------------------ */
26 /* #include "bzlib.h" */
28 /*-- General stuff. --*/
30 typedef unsigned char Bool
;
32 #define True ((Bool)1)
33 #define False ((Bool)0)
36 static void bz_assert_fail(int errcode
) NORETURN
;
37 #define AssertH(cond, errcode) \
40 bz_assert_fail(errcode); \
43 #define AssertH(cond, msg) do { } while (0)
47 #define AssertD(cond, msg) \
50 bb_error_msg_and_die("(debug build): internal error %s", msg); \
53 #define AssertD(cond, msg) do { } while (0)
57 /*-- Header bytes. --*/
59 #define BZ_HDR_B 0x42 /* 'B' */
60 #define BZ_HDR_Z 0x5a /* 'Z' */
61 #define BZ_HDR_h 0x68 /* 'h' */
62 #define BZ_HDR_0 0x30 /* '0' */
64 #define BZ_HDR_BZh0 0x425a6830
66 /*-- Constants for the back end. --*/
68 #define BZ_MAX_ALPHA_SIZE 258
69 #define BZ_MAX_CODE_LEN 23
78 #define BZ_MAX_SELECTORS (2 + (900000 / BZ_G_SIZE))
81 /*-- Stuff for doing CRCs. --*/
83 #define BZ_INITIALISE_CRC(crcVar) \
85 crcVar = 0xffffffffL; \
88 #define BZ_FINALISE_CRC(crcVar) \
93 #define BZ_UPDATE_CRC(s, crcVar, cha) \
95 crcVar = (crcVar << 8) ^ s->crc32table[(crcVar >> 24) ^ ((uint8_t)cha)]; \
99 /*-- States and modes for compression. --*/
102 #define BZ_M_RUNNING 2
103 #define BZ_M_FLUSHING 3
104 #define BZ_M_FINISHING 4
106 #define BZ_S_OUTPUT 1
110 #define BZ_N_QSORT 12
111 #define BZ_N_SHELL 18
112 #define BZ_N_OVERSHOOT (BZ_N_RADIX + BZ_N_QSORT + BZ_N_SHELL + 2)
115 /*-- Structure holding all the compression-side stuff. --*/
117 typedef struct EState
{
118 /* pointer back to the struct bz_stream */
121 /* mode this stream is in, and whether inputting */
122 /* or outputting data */
126 /* remembers avail_in when flush/finish requested */
127 /* bbox: not needed, strm->avail_in always has the same value */
128 /* commented out with '//#' throughout the code */
129 /* uint32_t avail_in_expect; */
131 /* for doing the block sorting */
137 /* aliases for arr1 and arr2 */
144 uint32_t *crc32table
;
146 /* run-length-encoding of the input */
147 uint32_t state_in_ch
;
148 int32_t state_in_len
;
150 /* input and output limits and current posns */
154 int32_t state_out_pos
;
156 /* the buffer for bit stream creation */
160 /* block and combined CRCs */
162 uint32_t combinedCRC
;
164 /* misc administratium */
166 int32_t blockSize100k
;
168 /* stuff for coding the MTF values */
171 /* map of bytes used in block */
173 Bool inUse
[256] ALIGNED(sizeof(long));
174 uint8_t unseqToSeq
[256];
176 /* stuff for coding the MTF values */
177 int32_t mtfFreq
[BZ_MAX_ALPHA_SIZE
];
178 uint8_t selector
[BZ_MAX_SELECTORS
];
179 uint8_t selectorMtf
[BZ_MAX_SELECTORS
];
181 uint8_t len
[BZ_N_GROUPS
][BZ_MAX_ALPHA_SIZE
];
183 /* stack-saving measures: these can be local, but they are too big */
184 int32_t sendMTFValues__code
[BZ_N_GROUPS
][BZ_MAX_ALPHA_SIZE
];
185 int32_t sendMTFValues__rfreq
[BZ_N_GROUPS
][BZ_MAX_ALPHA_SIZE
];
186 #if CONFIG_BZIP2_FEATURE_SPEED >= 5
187 /* second dimension: only 3 needed; 4 makes index calculations faster */
188 uint32_t sendMTFValues__len_pack
[BZ_MAX_ALPHA_SIZE
][4];
190 int32_t BZ2_hbMakeCodeLengths__heap
[BZ_MAX_ALPHA_SIZE
+ 2];
191 int32_t BZ2_hbMakeCodeLengths__weight
[BZ_MAX_ALPHA_SIZE
* 2];
192 int32_t BZ2_hbMakeCodeLengths__parent
[BZ_MAX_ALPHA_SIZE
* 2];
194 int32_t mainSort__runningOrder
[256];
195 int32_t mainSort__copyStart
[256];
196 int32_t mainSort__copyEnd
[256];
200 /*-- compression. --*/
203 BZ2_blockSort(EState
*);
206 BZ2_compressBlock(EState
*, int);
209 BZ2_bsInitWrite(EState
*);
212 BZ2_hbAssignCodes(int32_t*, uint8_t*, int32_t, int32_t, int32_t);
215 BZ2_hbMakeCodeLengths(EState
*, uint8_t*, int32_t*, int32_t, int32_t);
217 /*-------------------------------------------------------------*/
218 /*--- end bzlib_private.h ---*/
219 /*-------------------------------------------------------------*/