1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30typedef unsigned char Bool;
31
32#define True ((Bool)1)
33#define False ((Bool)0)
34
35#if BZ_LIGHT_DEBUG
36static void bz_assert_fail(int errcode) NORETURN;
37#define AssertH(cond, errcode) \
38do { \
39 if (!(cond)) \
40 bz_assert_fail(errcode); \
41} while (0)
42#else
43#define AssertH(cond, msg) do { } while (0)
44#endif
45
46#if BZ_DEBUG
47#define AssertD(cond, msg) \
48do { \
49 if (!(cond)) \
50 bb_error_msg_and_die("(debug build): internal error %s", msg); \
51} while (0)
52#else
53#define AssertD(cond, msg) do { } while (0)
54#endif
55
56
57
58
59#define BZ_HDR_B 0x42
60#define BZ_HDR_Z 0x5a
61#define BZ_HDR_h 0x68
62#define BZ_HDR_0 0x30
63
64#define BZ_HDR_BZh0 0x425a6830
65
66
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
81
82
83#define BZ_INITIALISE_CRC(crcVar) \
84{ \
85 crcVar = 0xffffffffL; \
86}
87
88#define BZ_FINALISE_CRC(crcVar) \
89{ \
90 crcVar = ~(crcVar); \
91}
92
93#define BZ_UPDATE_CRC(s, crcVar, cha) \
94{ \
95 crcVar = (crcVar << 8) ^ s->crc32table[(crcVar >> 24) ^ ((uint8_t)cha)]; \
96}
97
98
99
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
116
117typedef struct EState {
118
119 bz_stream *strm;
120
121
122
123 uint8_t mode;
124 uint8_t state;
125
126
127 uint8_t blockSize100k;
128
129
130
131
132
133
134
135 uint32_t *arr1;
136 uint32_t *arr2;
137 uint32_t *ftab;
138
139 uint16_t *quadrant;
140 int32_t budget;
141
142
143 uint32_t *ptr;
144 uint8_t *block;
145 uint16_t *mtfv;
146 uint8_t *zbits;
147
148
149 uint32_t state_in_ch;
150 int32_t state_in_len;
151
152
153 int32_t nblock;
154 int32_t nblockMAX;
155
156 uint8_t *posZ;
157 uint8_t *state_out_pos;
158
159
160 uint32_t bsBuff;
161 int32_t bsLive;
162
163
164 uint32_t *crc32table;
165
166
167 uint32_t blockCRC;
168 uint32_t combinedCRC;
169
170
171 int32_t blockNo;
172
173
174 int32_t nMTF;
175
176
177 int32_t nInUse;
178 Bool inUse[256] ALIGNED(sizeof(long));
179 uint8_t unseqToSeq[256];
180
181
182 int32_t mtfFreq [BZ_MAX_ALPHA_SIZE];
183 uint8_t selector [BZ_MAX_SELECTORS];
184 uint8_t selectorMtf[BZ_MAX_SELECTORS];
185
186 uint8_t len[BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
187
188
189 int32_t sendMTFValues__code [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
190 int32_t sendMTFValues__rfreq[BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
191#if BZIP2_SPEED >= 5
192
193 uint32_t sendMTFValues__len_pack[BZ_MAX_ALPHA_SIZE][4];
194#endif
195 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
199 int32_t mainSort__copyStart[256];
200 int32_t mainSort__copyEnd[256];
201} EState;
202
203
204
205
206static int32_t
207BZ2_blockSort(EState*);
208
209static void
210BZ2_compressBlock(EState*, int);
211
212static void
213BZ2_bsInitWrite(EState*);
214
215static void
216BZ2_hbAssignCodes(int32_t*, uint8_t*, int32_t, int32_t, int32_t);
217
218static void
219BZ2_hbMakeCodeLengths(EState*, uint8_t*, int32_t*, int32_t, int32_t);
220
221
222
223
224