blob: ea0f29b7cf9f1f0c064c8461565f94048d17fef8 [file] [log] [blame]
Denis Vlasenko77f1ec12007-10-13 03:36:03 +00001/*
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.
5 */
6
7/*-------------------------------------------------------------*/
8/*--- Private header file for the library. ---*/
9/*--- bzlib_private.h ---*/
10/*-------------------------------------------------------------*/
11
12/* ------------------------------------------------------------------
13This file is part of bzip2/libbzip2, a program and library for
14lossless, block-sorting data compression.
15
16bzip2/libbzip2 version 1.0.4 of 20 December 2006
17Copyright (C) 1996-2006 Julian Seward <jseward@bzip.org>
18
19Please read the WARNING, DISCLAIMER and PATENTS sections in the
20README file.
21
22This program is released under the terms of the license contained
23in the file LICENSE.
24------------------------------------------------------------------ */
25
26/* #include "bzlib.h" */
27
Denis Vlasenko77f1ec12007-10-13 03:36:03 +000028/*-- General stuff. --*/
29
30typedef unsigned char Bool;
Denis Vlasenko77f1ec12007-10-13 03:36:03 +000031
32#define True ((Bool)1)
33#define False ((Bool)0)
34
Denis Vlasenkoef3aabe2007-10-14 00:43:01 +000035#if BZ_LIGHT_DEBUG
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000036static void bz_assert_fail(int errcode) NORETURN;
Denis Vlasenko77f1ec12007-10-13 03:36:03 +000037#define AssertH(cond, errcode) \
Denis Vlasenkoef3aabe2007-10-14 00:43:01 +000038do { \
Denis Vlasenko77f1ec12007-10-13 03:36:03 +000039 if (!(cond)) \
40 bz_assert_fail(errcode); \
Denis Vlasenkoef3aabe2007-10-14 00:43:01 +000041} while (0)
42#else
43#define AssertH(cond, msg) do { } while (0)
44#endif
Denis Vlasenko77f1ec12007-10-13 03:36:03 +000045
46#if BZ_DEBUG
47#define AssertD(cond, msg) \
Denis Vlasenkoef3aabe2007-10-14 00:43:01 +000048do { \
Denis Vlasenko77f1ec12007-10-13 03:36:03 +000049 if (!(cond)) \
50 bb_error_msg_and_die("(debug build): internal error %s", msg); \
Denis Vlasenkoef3aabe2007-10-14 00:43:01 +000051} while (0)
Denis Vlasenko77f1ec12007-10-13 03:36:03 +000052#else
53#define AssertD(cond, msg) do { } while (0)
54#endif
55
56
57/*-- Header bytes. --*/
58
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' */
63
64#define BZ_HDR_BZh0 0x425a6830
65
66/*-- Constants for the back end. --*/
67
68#define BZ_MAX_ALPHA_SIZE 258
69#define BZ_MAX_CODE_LEN 23
70
71#define BZ_RUNA 0
72#define BZ_RUNB 1
73
74#define BZ_N_GROUPS 6
75#define BZ_G_SIZE 50
76#define BZ_N_ITERS 4
77
78#define BZ_MAX_SELECTORS (2 + (900000 / BZ_G_SIZE))
79
80
Denis Vlasenko77f1ec12007-10-13 03:36:03 +000081/*-- Stuff for doing CRCs. --*/
82
Denis Vlasenko77f1ec12007-10-13 03:36:03 +000083#define BZ_INITIALISE_CRC(crcVar) \
84{ \
85 crcVar = 0xffffffffL; \
86}
87
88#define BZ_FINALISE_CRC(crcVar) \
89{ \
90 crcVar = ~(crcVar); \
91}
92
Denis Vlasenkoef3aabe2007-10-14 00:43:01 +000093#define BZ_UPDATE_CRC(s, crcVar, cha) \
Denis Vlasenko77f1ec12007-10-13 03:36:03 +000094{ \
Denis Vlasenkoef3aabe2007-10-14 00:43:01 +000095 crcVar = (crcVar << 8) ^ s->crc32table[(crcVar >> 24) ^ ((uint8_t)cha)]; \
Denis Vlasenko77f1ec12007-10-13 03:36:03 +000096}
97
98
99/*-- States and modes for compression. --*/
100
101#define BZ_M_IDLE 1
102#define BZ_M_RUNNING 2
103#define BZ_M_FLUSHING 3
104#define BZ_M_FINISHING 4
105
106#define BZ_S_OUTPUT 1
107#define BZ_S_INPUT 2
108
109#define BZ_N_RADIX 2
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)
113
114
115/*-- Structure holding all the compression-side stuff. --*/
116
117typedef struct EState {
118 /* pointer back to the struct bz_stream */
119 bz_stream *strm;
120
121 /* mode this stream is in, and whether inputting */
122 /* or outputting data */
Denys Vlasenko86be6d52018-02-03 20:50:20 +0100123 uint8_t mode;
124 uint8_t state;
125
126 /* misc administratium */
127 uint8_t blockSize100k;
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000128
129 /* remembers avail_in when flush/finish requested */
Denis Vlasenkoef3aabe2007-10-14 00:43:01 +0000130/* bbox: not needed, strm->avail_in always has the same value */
131/* commented out with '//#' throughout the code */
132 /* uint32_t avail_in_expect; */
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000133
134 /* for doing the block sorting */
135 uint32_t *arr1;
136 uint32_t *arr2;
137 uint32_t *ftab;
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000138
Denys Vlasenko86be6d52018-02-03 20:50:20 +0100139 uint16_t *quadrant;
Denys Vlasenkoc9ae8d72018-02-03 20:19:51 +0100140 int32_t budget;
141
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000142 /* aliases for arr1 and arr2 */
143 uint32_t *ptr;
Denis Vlasenkoef3aabe2007-10-14 00:43:01 +0000144 uint8_t *block;
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000145 uint16_t *mtfv;
Denis Vlasenkoef3aabe2007-10-14 00:43:01 +0000146 uint8_t *zbits;
147
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000148 /* run-length-encoding of the input */
149 uint32_t state_in_ch;
150 int32_t state_in_len;
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000151
152 /* input and output limits and current posns */
153 int32_t nblock;
154 int32_t nblockMAX;
Denys Vlasenkoe594fb22018-02-03 01:30:12 +0100155 //int32_t numZ; // index into s->zbits[], replaced by pointer:
Denys Vlasenko86be6d52018-02-03 20:50:20 +0100156 uint8_t *posZ;
157 uint8_t *state_out_pos;
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000158
159 /* the buffer for bit stream creation */
160 uint32_t bsBuff;
161 int32_t bsLive;
162
Denys Vlasenko86be6d52018-02-03 20:50:20 +0100163 /* guess what */
164 uint32_t *crc32table;
165
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000166 /* block and combined CRCs */
167 uint32_t blockCRC;
168 uint32_t combinedCRC;
169
170 /* misc administratium */
171 int32_t blockNo;
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000172
173 /* stuff for coding the MTF values */
174 int32_t nMTF;
175
176 /* map of bytes used in block */
177 int32_t nInUse;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000178 Bool inUse[256] ALIGNED(sizeof(long));
Denis Vlasenkoef3aabe2007-10-14 00:43:01 +0000179 uint8_t unseqToSeq[256];
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000180
181 /* stuff for coding the MTF values */
182 int32_t mtfFreq [BZ_MAX_ALPHA_SIZE];
Denis Vlasenkoef3aabe2007-10-14 00:43:01 +0000183 uint8_t selector [BZ_MAX_SELECTORS];
184 uint8_t selectorMtf[BZ_MAX_SELECTORS];
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000185
Denis Vlasenkoab801872007-12-02 08:35:37 +0000186 uint8_t len[BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
187
188 /* stack-saving measures: these can be local, but they are too big */
189 int32_t sendMTFValues__code [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
190 int32_t sendMTFValues__rfreq[BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
Denys Vlasenko4cae0442018-02-07 01:33:25 +0100191#if BZIP2_SPEED >= 5
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000192 /* second dimension: only 3 needed; 4 makes index calculations faster */
Denis Vlasenkoab801872007-12-02 08:35:37 +0000193 uint32_t sendMTFValues__len_pack[BZ_MAX_ALPHA_SIZE][4];
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000194#endif
Denis Vlasenkoab801872007-12-02 08:35:37 +0000195 int32_t BZ2_hbMakeCodeLengths__heap [BZ_MAX_ALPHA_SIZE + 2];
196 int32_t BZ2_hbMakeCodeLengths__weight[BZ_MAX_ALPHA_SIZE * 2];
197 int32_t BZ2_hbMakeCodeLengths__parent[BZ_MAX_ALPHA_SIZE * 2];
198
Denis Vlasenkoab801872007-12-02 08:35:37 +0000199 int32_t mainSort__copyStart[256];
200 int32_t mainSort__copyEnd[256];
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000201} EState;
202
203
204/*-- compression. --*/
205
Denys Vlasenko86be6d52018-02-03 20:50:20 +0100206static int32_t
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000207BZ2_blockSort(EState*);
208
209static void
Denis Vlasenkoef3aabe2007-10-14 00:43:01 +0000210BZ2_compressBlock(EState*, int);
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000211
212static void
213BZ2_bsInitWrite(EState*);
214
215static void
Denis Vlasenkoef3aabe2007-10-14 00:43:01 +0000216BZ2_hbAssignCodes(int32_t*, uint8_t*, int32_t, int32_t, int32_t);
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000217
218static void
Denis Vlasenkoab801872007-12-02 08:35:37 +0000219BZ2_hbMakeCodeLengths(EState*, uint8_t*, int32_t*, int32_t, int32_t);
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000220
221/*-------------------------------------------------------------*/
222/*--- end bzlib_private.h ---*/
223/*-------------------------------------------------------------*/