blob: fc6af6595de4dc20ae60423cbfc534d7f8450a08 [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/*--- Compression machinery (not incl block sorting) ---*/
9/*--- compress.c ---*/
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/* CHANGES
27 * 0.9.0 -- original version.
28 * 0.9.0a/b -- no changes in this file.
29 * 0.9.0c -- changed setting of nGroups in sendMTFValues()
30 * so as to do a bit better on small files
31*/
32
33/* #include "bzlib_private.h" */
34
35/*---------------------------------------------------*/
36/*--- Bit stream I/O ---*/
37/*---------------------------------------------------*/
38
39/*---------------------------------------------------*/
40static
41void BZ2_bsInitWrite(EState* s)
42{
43 s->bsLive = 0;
44 s->bsBuff = 0;
45}
46
47
48/*---------------------------------------------------*/
49static NOINLINE
50void bsFinishWrite(EState* s)
51{
52 while (s->bsLive > 0) {
Denys Vlasenkoe594fb22018-02-03 01:30:12 +010053 *s->posZ++ = (uint8_t)(s->bsBuff >> 24);
Denis Vlasenko77f1ec12007-10-13 03:36:03 +000054 s->bsBuff <<= 8;
55 s->bsLive -= 8;
56 }
57}
58
59
60/*---------------------------------------------------*/
61static
Denis Vlasenkoef3aabe2007-10-14 00:43:01 +000062/* Helps only on level 5, on other levels hurts. ? */
Denys Vlasenko522041e2011-09-10 13:25:57 +020063#if CONFIG_BZIP2_FAST >= 5
Denis Vlasenkoef3aabe2007-10-14 00:43:01 +000064ALWAYS_INLINE
65#endif
Denis Vlasenko77f1ec12007-10-13 03:36:03 +000066void bsW(EState* s, int32_t n, uint32_t v)
67{
68 while (s->bsLive >= 8) {
Denys Vlasenkoe594fb22018-02-03 01:30:12 +010069 *s->posZ++ = (uint8_t)(s->bsBuff >> 24);
Denis Vlasenko77f1ec12007-10-13 03:36:03 +000070 s->bsBuff <<= 8;
71 s->bsLive -= 8;
72 }
73 s->bsBuff |= (v << (32 - s->bsLive - n));
74 s->bsLive += n;
75}
Denys Vlasenko125c3ff2018-02-02 20:59:28 +010076/* Same with n == 16: */
77static
78#if CONFIG_BZIP2_FAST >= 5
79ALWAYS_INLINE
80#endif
81void bsW16(EState* s, uint32_t v)
82{
83 while (s->bsLive >= 8) {
Denys Vlasenkoe594fb22018-02-03 01:30:12 +010084 *s->posZ++ = (uint8_t)(s->bsBuff >> 24);
Denys Vlasenko125c3ff2018-02-02 20:59:28 +010085 s->bsBuff <<= 8;
86 s->bsLive -= 8;
87 }
88 s->bsBuff |= (v << (16 - s->bsLive));
89 s->bsLive += 16;
90}
Denys Vlasenkofeafb342018-02-03 04:43:46 +010091/* Same with n == 1: */
92static
Denys Vlasenkofc228b42018-02-03 14:56:43 +010093ALWAYS_INLINE /* one callsite */
94void bsW1_1(EState* s)
Denys Vlasenkofeafb342018-02-03 04:43:46 +010095{
96 /* need space for only 1 bit, no need for loop freeing > 8 bits */
97 if (s->bsLive >= 8) {
98 *s->posZ++ = (uint8_t)(s->bsBuff >> 24);
99 s->bsBuff <<= 8;
100 s->bsLive -= 8;
101 }
Denys Vlasenkofc228b42018-02-03 14:56:43 +0100102 s->bsBuff |= (1 << (31 - s->bsLive));
103 s->bsLive += 1;
104}
105static
106#if CONFIG_BZIP2_FAST >= 5
107ALWAYS_INLINE
108#endif
109void bsW1_0(EState* s)
110{
111 /* need space for only 1 bit, no need for loop freeing > 8 bits */
112 if (s->bsLive >= 8) {
113 *s->posZ++ = (uint8_t)(s->bsBuff >> 24);
114 s->bsBuff <<= 8;
115 s->bsLive -= 8;
116 }
117 //s->bsBuff |= (0 << (31 - s->bsLive));
Denys Vlasenkofeafb342018-02-03 04:43:46 +0100118 s->bsLive += 1;
119}
Denys Vlasenko125c3ff2018-02-02 20:59:28 +0100120
121
122/*---------------------------------------------------*/
123static ALWAYS_INLINE
124void bsPutU16(EState* s, unsigned u)
125{
126 bsW16(s, u);
127}
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000128
129
130/*---------------------------------------------------*/
131static
Denis Vlasenkoef3aabe2007-10-14 00:43:01 +0000132void bsPutU32(EState* s, unsigned u)
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000133{
Denys Vlasenko125c3ff2018-02-02 20:59:28 +0100134 //bsW(s, 32, u); // can't use: may try "uint32 << -n"
135 bsW16(s, (u >> 16) & 0xffff);
136 bsW16(s, u & 0xffff);
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000137}
138
139
140/*---------------------------------------------------*/
141/*--- The back end proper ---*/
142/*---------------------------------------------------*/
143
144/*---------------------------------------------------*/
145static
146void makeMaps_e(EState* s)
147{
Denis Vlasenkoef3aabe2007-10-14 00:43:01 +0000148 int i;
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000149 s->nInUse = 0;
150 for (i = 0; i < 256; i++) {
151 if (s->inUse[i]) {
152 s->unseqToSeq[i] = s->nInUse;
153 s->nInUse++;
154 }
155 }
156}
157
158
159/*---------------------------------------------------*/
160static NOINLINE
161void generateMTFValues(EState* s)
162{
Denis Vlasenkoef3aabe2007-10-14 00:43:01 +0000163 uint8_t yy[256];
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000164 int32_t i, j;
165 int32_t zPend;
166 int32_t wr;
167 int32_t EOB;
168
169 /*
170 * After sorting (eg, here),
171 * s->arr1[0 .. s->nblock-1] holds sorted order,
172 * and
Denis Vlasenkoef3aabe2007-10-14 00:43:01 +0000173 * ((uint8_t*)s->arr2)[0 .. s->nblock-1]
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000174 * holds the original block data.
175 *
176 * The first thing to do is generate the MTF values,
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200177 * and put them in ((uint16_t*)s->arr1)[0 .. s->nblock-1].
178 *
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000179 * Because there are strictly fewer or equal MTF values
180 * than block values, ptr values in this area are overwritten
181 * with MTF values only when they are no longer needed.
182 *
183 * The final compressed bitstream is generated into the
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200184 * area starting at &((uint8_t*)s->arr2)[s->nblock]
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000185 *
186 * These storage aliases are set up in bzCompressInit(),
187 * except for the last one, which is arranged in
188 * compressBlock().
189 */
190 uint32_t* ptr = s->ptr;
Denis Vlasenkoef3aabe2007-10-14 00:43:01 +0000191 uint8_t* block = s->block;
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000192 uint16_t* mtfv = s->mtfv;
193
194 makeMaps_e(s);
195 EOB = s->nInUse+1;
196
197 for (i = 0; i <= EOB; i++)
198 s->mtfFreq[i] = 0;
199
200 wr = 0;
201 zPend = 0;
202 for (i = 0; i < s->nInUse; i++)
Denis Vlasenkoef3aabe2007-10-14 00:43:01 +0000203 yy[i] = (uint8_t) i;
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000204
205 for (i = 0; i < s->nblock; i++) {
Denis Vlasenkoef3aabe2007-10-14 00:43:01 +0000206 uint8_t ll_i;
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000207 AssertD(wr <= i, "generateMTFValues(1)");
Denis Vlasenkoef3aabe2007-10-14 00:43:01 +0000208 j = ptr[i] - 1;
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000209 if (j < 0)
210 j += s->nblock;
211 ll_i = s->unseqToSeq[block[j]];
212 AssertD(ll_i < s->nInUse, "generateMTFValues(2a)");
213
214 if (yy[0] == ll_i) {
215 zPend++;
216 } else {
217 if (zPend > 0) {
218 zPend--;
219 while (1) {
220 if (zPend & 1) {
221 mtfv[wr] = BZ_RUNB; wr++;
222 s->mtfFreq[BZ_RUNB]++;
223 } else {
224 mtfv[wr] = BZ_RUNA; wr++;
225 s->mtfFreq[BZ_RUNA]++;
226 }
227 if (zPend < 2) break;
Denis Vlasenko6a9154b2007-10-14 07:49:48 +0000228 zPend = (uint32_t)(zPend - 2) / 2;
229 /* bbox: unsigned div is easier */
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000230 };
231 zPend = 0;
232 }
233 {
Denis Vlasenkoef3aabe2007-10-14 00:43:01 +0000234 register uint8_t rtmp;
235 register uint8_t* ryy_j;
236 register uint8_t rll_i;
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000237 rtmp = yy[1];
238 yy[1] = yy[0];
239 ryy_j = &(yy[1]);
240 rll_i = ll_i;
241 while (rll_i != rtmp) {
Denis Vlasenkoef3aabe2007-10-14 00:43:01 +0000242 register uint8_t rtmp2;
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000243 ryy_j++;
244 rtmp2 = rtmp;
245 rtmp = *ryy_j;
246 *ryy_j = rtmp2;
247 };
248 yy[0] = rtmp;
249 j = ryy_j - &(yy[0]);
250 mtfv[wr] = j+1;
251 wr++;
252 s->mtfFreq[j+1]++;
253 }
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000254 }
255 }
256
257 if (zPend > 0) {
258 zPend--;
259 while (1) {
260 if (zPend & 1) {
Denis Vlasenko6a9154b2007-10-14 07:49:48 +0000261 mtfv[wr] = BZ_RUNB;
262 wr++;
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000263 s->mtfFreq[BZ_RUNB]++;
264 } else {
Denis Vlasenko6a9154b2007-10-14 07:49:48 +0000265 mtfv[wr] = BZ_RUNA;
266 wr++;
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000267 s->mtfFreq[BZ_RUNA]++;
268 }
269 if (zPend < 2)
270 break;
Denis Vlasenko6a9154b2007-10-14 07:49:48 +0000271 zPend = (uint32_t)(zPend - 2) / 2;
272 /* bbox: unsigned div is easier */
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000273 };
274 zPend = 0;
275 }
276
277 mtfv[wr] = EOB;
278 wr++;
279 s->mtfFreq[EOB]++;
280
281 s->nMTF = wr;
282}
283
284
285/*---------------------------------------------------*/
286#define BZ_LESSER_ICOST 0
287#define BZ_GREATER_ICOST 15
288
289static NOINLINE
290void sendMTFValues(EState* s)
291{
Denys Vlasenko3a2c97b2018-02-03 03:12:14 +0100292 int32_t t, i;
293 unsigned iter;
294 unsigned gs;
295 int32_t alphaSize;
296 unsigned nSelectors, selCtr;
Denys Vlasenko60a94142011-05-13 20:57:01 +0200297 int32_t nGroups;
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000298
299 /*
Denis Vlasenkoef3aabe2007-10-14 00:43:01 +0000300 * uint8_t len[BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000301 * is a global since the decoder also needs it.
302 *
303 * int32_t code[BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
304 * int32_t rfreq[BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
305 * are also globals only used in this proc.
306 * Made global to keep stack frame size small.
307 */
Denis Vlasenkoab801872007-12-02 08:35:37 +0000308#define code sendMTFValues__code
309#define rfreq sendMTFValues__rfreq
310#define len_pack sendMTFValues__len_pack
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000311
Denys Vlasenko3a2c97b2018-02-03 03:12:14 +0100312 unsigned /*uint16_t*/ cost[BZ_N_GROUPS];
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000313
314 uint16_t* mtfv = s->mtfv;
315
Denis Vlasenkoab801872007-12-02 08:35:37 +0000316 alphaSize = s->nInUse + 2;
Denys Vlasenko3a2c97b2018-02-03 03:12:14 +0100317 for (t = 0; t < BZ_N_GROUPS; t++) {
318 unsigned v;
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000319 for (v = 0; v < alphaSize; v++)
320 s->len[t][v] = BZ_GREATER_ICOST;
Denys Vlasenko3a2c97b2018-02-03 03:12:14 +0100321 }
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000322
323 /*--- Decide how many coding tables to use ---*/
324 AssertH(s->nMTF > 0, 3001);
Denys Vlasenko359230d2018-02-03 02:03:42 +0100325 // 1..199 = 2
326 // 200..599 = 3
327 // 600..1199 = 4
328 // 1200..2399 = 5
Denys Vlasenko982c44d2018-02-03 03:34:40 +0100329 // 2400..99999 = 6
Denys Vlasenko359230d2018-02-03 02:03:42 +0100330 nGroups = 2;
331 nGroups += (s->nMTF >= 200);
332 nGroups += (s->nMTF >= 600);
333 nGroups += (s->nMTF >= 1200);
334 nGroups += (s->nMTF >= 2400);
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000335
336 /*--- Generate an initial set of coding tables ---*/
337 {
Denys Vlasenko3a2c97b2018-02-03 03:12:14 +0100338 unsigned nPart, remF;
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000339
340 nPart = nGroups;
341 remF = s->nMTF;
342 gs = 0;
343 while (nPart > 0) {
Denys Vlasenko3a2c97b2018-02-03 03:12:14 +0100344 unsigned v;
345 unsigned ge;
346 unsigned tFreq, aFreq;
347
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000348 tFreq = remF / nPart;
Denys Vlasenko982c44d2018-02-03 03:34:40 +0100349 ge = gs;
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000350 aFreq = 0;
Denys Vlasenko982c44d2018-02-03 03:34:40 +0100351 while (aFreq < tFreq && ge < alphaSize) {
352 aFreq += s->mtfFreq[ge++];
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000353 }
Denys Vlasenko982c44d2018-02-03 03:34:40 +0100354 ge--;
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000355
356 if (ge > gs
357 && nPart != nGroups && nPart != 1
Denis Vlasenko6a9154b2007-10-14 07:49:48 +0000358 && ((nGroups - nPart) % 2 == 1) /* bbox: can this be replaced by x & 1? */
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000359 ) {
360 aFreq -= s->mtfFreq[ge];
361 ge--;
362 }
363
364 for (v = 0; v < alphaSize; v++)
365 if (v >= gs && v <= ge)
366 s->len[nPart-1][v] = BZ_LESSER_ICOST;
367 else
368 s->len[nPart-1][v] = BZ_GREATER_ICOST;
369
370 nPart--;
Denis Vlasenko6a9154b2007-10-14 07:49:48 +0000371 gs = ge + 1;
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000372 remF -= aFreq;
373 }
374 }
375
376 /*
377 * Iterate up to BZ_N_ITERS times to improve the tables.
378 */
379 for (iter = 0; iter < BZ_N_ITERS; iter++) {
Denys Vlasenko3a2c97b2018-02-03 03:12:14 +0100380 for (t = 0; t < nGroups; t++) {
381 unsigned v;
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000382 for (v = 0; v < alphaSize; v++)
383 s->rfreq[t][v] = 0;
Denys Vlasenko3a2c97b2018-02-03 03:12:14 +0100384 }
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000385
Denys Vlasenko522041e2011-09-10 13:25:57 +0200386#if CONFIG_BZIP2_FAST >= 5
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000387 /*
388 * Set up an auxiliary length table which is used to fast-track
389 * the common case (nGroups == 6).
390 */
391 if (nGroups == 6) {
Denys Vlasenko3a2c97b2018-02-03 03:12:14 +0100392 unsigned v;
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000393 for (v = 0; v < alphaSize; v++) {
394 s->len_pack[v][0] = (s->len[1][v] << 16) | s->len[0][v];
395 s->len_pack[v][1] = (s->len[3][v] << 16) | s->len[2][v];
396 s->len_pack[v][2] = (s->len[5][v] << 16) | s->len[4][v];
397 }
398 }
399#endif
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000400 nSelectors = 0;
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000401 gs = 0;
402 while (1) {
Denys Vlasenko3a2c97b2018-02-03 03:12:14 +0100403 unsigned ge;
404 unsigned bt, bc;
405
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000406 /*--- Set group start & end marks. --*/
407 if (gs >= s->nMTF)
408 break;
409 ge = gs + BZ_G_SIZE - 1;
410 if (ge >= s->nMTF)
411 ge = s->nMTF-1;
412
413 /*
414 * Calculate the cost of this group as coded
415 * by each of the coding tables.
416 */
417 for (t = 0; t < nGroups; t++)
418 cost[t] = 0;
Denys Vlasenko522041e2011-09-10 13:25:57 +0200419#if CONFIG_BZIP2_FAST >= 5
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000420 if (nGroups == 6 && 50 == ge-gs+1) {
421 /*--- fast track the common case ---*/
422 register uint32_t cost01, cost23, cost45;
423 register uint16_t icv;
424 cost01 = cost23 = cost45 = 0;
425#define BZ_ITER(nn) \
426 icv = mtfv[gs+(nn)]; \
427 cost01 += s->len_pack[icv][0]; \
428 cost23 += s->len_pack[icv][1]; \
429 cost45 += s->len_pack[icv][2];
430 BZ_ITER(0); BZ_ITER(1); BZ_ITER(2); BZ_ITER(3); BZ_ITER(4);
431 BZ_ITER(5); BZ_ITER(6); BZ_ITER(7); BZ_ITER(8); BZ_ITER(9);
432 BZ_ITER(10); BZ_ITER(11); BZ_ITER(12); BZ_ITER(13); BZ_ITER(14);
433 BZ_ITER(15); BZ_ITER(16); BZ_ITER(17); BZ_ITER(18); BZ_ITER(19);
434 BZ_ITER(20); BZ_ITER(21); BZ_ITER(22); BZ_ITER(23); BZ_ITER(24);
435 BZ_ITER(25); BZ_ITER(26); BZ_ITER(27); BZ_ITER(28); BZ_ITER(29);
436 BZ_ITER(30); BZ_ITER(31); BZ_ITER(32); BZ_ITER(33); BZ_ITER(34);
437 BZ_ITER(35); BZ_ITER(36); BZ_ITER(37); BZ_ITER(38); BZ_ITER(39);
438 BZ_ITER(40); BZ_ITER(41); BZ_ITER(42); BZ_ITER(43); BZ_ITER(44);
439 BZ_ITER(45); BZ_ITER(46); BZ_ITER(47); BZ_ITER(48); BZ_ITER(49);
440#undef BZ_ITER
441 cost[0] = cost01 & 0xffff; cost[1] = cost01 >> 16;
442 cost[2] = cost23 & 0xffff; cost[3] = cost23 >> 16;
443 cost[4] = cost45 & 0xffff; cost[5] = cost45 >> 16;
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000444 } else
445#endif
446 {
447 /*--- slow version which correctly handles all situations ---*/
448 for (i = gs; i <= ge; i++) {
Denys Vlasenko3a2c97b2018-02-03 03:12:14 +0100449 unsigned /*uint16_t*/ icv = mtfv[i];
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000450 for (t = 0; t < nGroups; t++)
451 cost[t] += s->len[t][icv];
452 }
453 }
454 /*
455 * Find the coding table which is best for this group,
456 * and record its identity in the selector table.
457 */
Denis Vlasenkoef3aabe2007-10-14 00:43:01 +0000458 /*bc = 999999999;*/
459 /*bt = -1;*/
460 bc = cost[0];
461 bt = 0;
462 for (t = 1 /*0*/; t < nGroups; t++) {
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000463 if (cost[t] < bc) {
464 bc = cost[t];
465 bt = t;
466 }
467 }
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000468 s->selector[nSelectors] = bt;
469 nSelectors++;
470
471 /*
472 * Increment the symbol frequencies for the selected table.
473 */
Denis Vlasenko0de37e12007-10-17 11:08:53 +0000474/* 1% faster compress. +800 bytes */
Denys Vlasenko522041e2011-09-10 13:25:57 +0200475#if CONFIG_BZIP2_FAST >= 4
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000476 if (nGroups == 6 && 50 == ge-gs+1) {
477 /*--- fast track the common case ---*/
478#define BZ_ITUR(nn) s->rfreq[bt][mtfv[gs + (nn)]]++
479 BZ_ITUR(0); BZ_ITUR(1); BZ_ITUR(2); BZ_ITUR(3); BZ_ITUR(4);
480 BZ_ITUR(5); BZ_ITUR(6); BZ_ITUR(7); BZ_ITUR(8); BZ_ITUR(9);
481 BZ_ITUR(10); BZ_ITUR(11); BZ_ITUR(12); BZ_ITUR(13); BZ_ITUR(14);
482 BZ_ITUR(15); BZ_ITUR(16); BZ_ITUR(17); BZ_ITUR(18); BZ_ITUR(19);
483 BZ_ITUR(20); BZ_ITUR(21); BZ_ITUR(22); BZ_ITUR(23); BZ_ITUR(24);
484 BZ_ITUR(25); BZ_ITUR(26); BZ_ITUR(27); BZ_ITUR(28); BZ_ITUR(29);
485 BZ_ITUR(30); BZ_ITUR(31); BZ_ITUR(32); BZ_ITUR(33); BZ_ITUR(34);
486 BZ_ITUR(35); BZ_ITUR(36); BZ_ITUR(37); BZ_ITUR(38); BZ_ITUR(39);
487 BZ_ITUR(40); BZ_ITUR(41); BZ_ITUR(42); BZ_ITUR(43); BZ_ITUR(44);
488 BZ_ITUR(45); BZ_ITUR(46); BZ_ITUR(47); BZ_ITUR(48); BZ_ITUR(49);
489#undef BZ_ITUR
Denis Vlasenkoef3aabe2007-10-14 00:43:01 +0000490 gs = ge + 1;
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000491 } else
492#endif
493 {
494 /*--- slow version which correctly handles all situations ---*/
495 while (gs <= ge) {
496 s->rfreq[bt][mtfv[gs]]++;
497 gs++;
498 }
Denis Vlasenkoef3aabe2007-10-14 00:43:01 +0000499 /* already is: gs = ge + 1; */
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000500 }
501 }
502
503 /*
504 * Recompute the tables based on the accumulated frequencies.
505 */
506 /* maxLen was changed from 20 to 17 in bzip2-1.0.3. See
507 * comment in huffman.c for details. */
508 for (t = 0; t < nGroups; t++)
Denis Vlasenkoab801872007-12-02 08:35:37 +0000509 BZ2_hbMakeCodeLengths(s, &(s->len[t][0]), &(s->rfreq[t][0]), alphaSize, 17 /*20*/);
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000510 }
511
512 AssertH(nGroups < 8, 3002);
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200513 AssertH(nSelectors < 32768 && nSelectors <= (2 + (900000 / BZ_G_SIZE)), 3003);
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000514
515 /*--- Compute MTF values for the selectors. ---*/
516 {
Denis Vlasenkoef3aabe2007-10-14 00:43:01 +0000517 uint8_t pos[BZ_N_GROUPS], ll_i, tmp2, tmp;
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000518
519 for (i = 0; i < nGroups; i++)
520 pos[i] = i;
521 for (i = 0; i < nSelectors; i++) {
Denys Vlasenko3a2c97b2018-02-03 03:12:14 +0100522 unsigned j;
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000523 ll_i = s->selector[i];
524 j = 0;
525 tmp = pos[j];
526 while (ll_i != tmp) {
527 j++;
528 tmp2 = tmp;
529 tmp = pos[j];
530 pos[j] = tmp2;
531 };
532 pos[0] = tmp;
533 s->selectorMtf[i] = j;
534 }
535 };
536
537 /*--- Assign actual codes for the tables. --*/
538 for (t = 0; t < nGroups; t++) {
Denys Vlasenko3a2c97b2018-02-03 03:12:14 +0100539 unsigned minLen = 32; //todo: s->len[t][0];
540 unsigned maxLen = 0; //todo: s->len[t][0];
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000541 for (i = 0; i < alphaSize; i++) {
542 if (s->len[t][i] > maxLen) maxLen = s->len[t][i];
543 if (s->len[t][i] < minLen) minLen = s->len[t][i];
544 }
545 AssertH(!(maxLen > 17 /*20*/), 3004);
546 AssertH(!(minLen < 1), 3005);
547 BZ2_hbAssignCodes(&(s->code[t][0]), &(s->len[t][0]), minLen, maxLen, alphaSize);
548 }
549
550 /*--- Transmit the mapping table. ---*/
551 {
Denis Vlasenkoef3aabe2007-10-14 00:43:01 +0000552 /* bbox: optimized a bit more than in bzip2 */
553 int inUse16 = 0;
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000554 for (i = 0; i < 16; i++) {
Denis Vlasenkoef3aabe2007-10-14 00:43:01 +0000555 if (sizeof(long) <= 4) {
556 inUse16 = inUse16*2 +
Denys Vlasenko5117eff2013-10-16 14:21:20 +0200557 ((*(bb__aliased_uint32_t*)&(s->inUse[i * 16 + 0])
558 | *(bb__aliased_uint32_t*)&(s->inUse[i * 16 + 4])
559 | *(bb__aliased_uint32_t*)&(s->inUse[i * 16 + 8])
560 | *(bb__aliased_uint32_t*)&(s->inUse[i * 16 + 12])) != 0);
Denis Vlasenkoef3aabe2007-10-14 00:43:01 +0000561 } else { /* Our CPU can do better */
562 inUse16 = inUse16*2 +
Denys Vlasenko5117eff2013-10-16 14:21:20 +0200563 ((*(bb__aliased_uint64_t*)&(s->inUse[i * 16 + 0])
564 | *(bb__aliased_uint64_t*)&(s->inUse[i * 16 + 8])) != 0);
Denis Vlasenkoef3aabe2007-10-14 00:43:01 +0000565 }
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000566 }
567
Denys Vlasenko125c3ff2018-02-02 20:59:28 +0100568 bsW16(s, inUse16);
Denis Vlasenkoef3aabe2007-10-14 00:43:01 +0000569
570 inUse16 <<= (sizeof(int)*8 - 16); /* move 15th bit into sign bit */
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000571 for (i = 0; i < 16; i++) {
Denis Vlasenkoef3aabe2007-10-14 00:43:01 +0000572 if (inUse16 < 0) {
573 unsigned v16 = 0;
Denys Vlasenko3a2c97b2018-02-03 03:12:14 +0100574 unsigned j;
Denis Vlasenkoef3aabe2007-10-14 00:43:01 +0000575 for (j = 0; j < 16; j++)
576 v16 = v16*2 + s->inUse[i * 16 + j];
Denys Vlasenko125c3ff2018-02-02 20:59:28 +0100577 bsW16(s, v16);
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000578 }
Denis Vlasenkoef3aabe2007-10-14 00:43:01 +0000579 inUse16 <<= 1;
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000580 }
581 }
582
583 /*--- Now the selectors. ---*/
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000584 bsW(s, 3, nGroups);
585 bsW(s, 15, nSelectors);
586 for (i = 0; i < nSelectors; i++) {
Denys Vlasenko3a2c97b2018-02-03 03:12:14 +0100587 unsigned j;
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000588 for (j = 0; j < s->selectorMtf[i]; j++)
Denys Vlasenkofc228b42018-02-03 14:56:43 +0100589 bsW1_1(s);
590 bsW1_0(s);
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000591 }
592
593 /*--- Now the coding tables. ---*/
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000594 for (t = 0; t < nGroups; t++) {
Denys Vlasenko3a2c97b2018-02-03 03:12:14 +0100595 unsigned curr = s->len[t][0];
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000596 bsW(s, 5, curr);
597 for (i = 0; i < alphaSize; i++) {
598 while (curr < s->len[t][i]) { bsW(s, 2, 2); curr++; /* 10 */ };
599 while (curr > s->len[t][i]) { bsW(s, 2, 3); curr--; /* 11 */ };
Denys Vlasenkofc228b42018-02-03 14:56:43 +0100600 bsW1_0(s);
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000601 }
602 }
603
604 /*--- And finally, the block data proper ---*/
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000605 selCtr = 0;
606 gs = 0;
607 while (1) {
Denys Vlasenko3a2c97b2018-02-03 03:12:14 +0100608 unsigned ge;
609
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000610 if (gs >= s->nMTF)
611 break;
612 ge = gs + BZ_G_SIZE - 1;
613 if (ge >= s->nMTF)
614 ge = s->nMTF-1;
615 AssertH(s->selector[selCtr] < nGroups, 3006);
616
617/* Costs 1300 bytes and is _slower_ (on Intel Core 2) */
618#if 0
619 if (nGroups == 6 && 50 == ge-gs+1) {
620 /*--- fast track the common case ---*/
621 uint16_t mtfv_i;
Denis Vlasenkoef3aabe2007-10-14 00:43:01 +0000622 uint8_t* s_len_sel_selCtr = &(s->len[s->selector[selCtr]][0]);
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000623 int32_t* s_code_sel_selCtr = &(s->code[s->selector[selCtr]][0]);
624#define BZ_ITAH(nn) \
625 mtfv_i = mtfv[gs+(nn)]; \
626 bsW(s, s_len_sel_selCtr[mtfv_i], s_code_sel_selCtr[mtfv_i])
627 BZ_ITAH(0); BZ_ITAH(1); BZ_ITAH(2); BZ_ITAH(3); BZ_ITAH(4);
628 BZ_ITAH(5); BZ_ITAH(6); BZ_ITAH(7); BZ_ITAH(8); BZ_ITAH(9);
629 BZ_ITAH(10); BZ_ITAH(11); BZ_ITAH(12); BZ_ITAH(13); BZ_ITAH(14);
630 BZ_ITAH(15); BZ_ITAH(16); BZ_ITAH(17); BZ_ITAH(18); BZ_ITAH(19);
631 BZ_ITAH(20); BZ_ITAH(21); BZ_ITAH(22); BZ_ITAH(23); BZ_ITAH(24);
632 BZ_ITAH(25); BZ_ITAH(26); BZ_ITAH(27); BZ_ITAH(28); BZ_ITAH(29);
633 BZ_ITAH(30); BZ_ITAH(31); BZ_ITAH(32); BZ_ITAH(33); BZ_ITAH(34);
634 BZ_ITAH(35); BZ_ITAH(36); BZ_ITAH(37); BZ_ITAH(38); BZ_ITAH(39);
635 BZ_ITAH(40); BZ_ITAH(41); BZ_ITAH(42); BZ_ITAH(43); BZ_ITAH(44);
636 BZ_ITAH(45); BZ_ITAH(46); BZ_ITAH(47); BZ_ITAH(48); BZ_ITAH(49);
637#undef BZ_ITAH
638 gs = ge+1;
639 } else
640#endif
641 {
642 /*--- slow version which correctly handles all situations ---*/
643 /* code is bit bigger, but moves multiply out of the loop */
Denis Vlasenkoef3aabe2007-10-14 00:43:01 +0000644 uint8_t* s_len_sel_selCtr = &(s->len [s->selector[selCtr]][0]);
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000645 int32_t* s_code_sel_selCtr = &(s->code[s->selector[selCtr]][0]);
646 while (gs <= ge) {
647 bsW(s,
648 s_len_sel_selCtr[mtfv[gs]],
649 s_code_sel_selCtr[mtfv[gs]]
650 );
651 gs++;
652 }
653 /* already is: gs = ge+1; */
654 }
655 selCtr++;
656 }
657 AssertH(selCtr == nSelectors, 3007);
Denis Vlasenkoab801872007-12-02 08:35:37 +0000658#undef code
659#undef rfreq
660#undef len_pack
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000661}
662
663
664/*---------------------------------------------------*/
665static
Denis Vlasenkoef3aabe2007-10-14 00:43:01 +0000666void BZ2_compressBlock(EState* s, int is_last_block)
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000667{
668 if (s->nblock > 0) {
669 BZ_FINALISE_CRC(s->blockCRC);
670 s->combinedCRC = (s->combinedCRC << 1) | (s->combinedCRC >> 31);
671 s->combinedCRC ^= s->blockCRC;
672 if (s->blockNo > 1)
Denys Vlasenkoe594fb22018-02-03 01:30:12 +0100673 s->posZ = s->zbits; // was: s->numZ = 0;
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000674
675 BZ2_blockSort(s);
676 }
677
Denis Vlasenkoef3aabe2007-10-14 00:43:01 +0000678 s->zbits = &((uint8_t*)s->arr2)[s->nblock];
Denys Vlasenkoe594fb22018-02-03 01:30:12 +0100679 s->posZ = s->zbits;
680 s->state_out_pos = s->zbits;
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000681
682 /*-- If this is the first block, create the stream header. --*/
683 if (s->blockNo == 1) {
684 BZ2_bsInitWrite(s);
Denis Vlasenkoef3aabe2007-10-14 00:43:01 +0000685 /*bsPutU8(s, BZ_HDR_B);*/
686 /*bsPutU8(s, BZ_HDR_Z);*/
687 /*bsPutU8(s, BZ_HDR_h);*/
688 /*bsPutU8(s, BZ_HDR_0 + s->blockSize100k);*/
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000689 bsPutU32(s, BZ_HDR_BZh0 + s->blockSize100k);
690 }
691
692 if (s->nblock > 0) {
Denis Vlasenkoef3aabe2007-10-14 00:43:01 +0000693 /*bsPutU8(s, 0x31);*/
694 /*bsPutU8(s, 0x41);*/
695 /*bsPutU8(s, 0x59);*/
696 /*bsPutU8(s, 0x26);*/
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000697 bsPutU32(s, 0x31415926);
Denis Vlasenkoef3aabe2007-10-14 00:43:01 +0000698 /*bsPutU8(s, 0x53);*/
699 /*bsPutU8(s, 0x59);*/
700 bsPutU16(s, 0x5359);
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000701
702 /*-- Now the block's CRC, so it is in a known place. --*/
703 bsPutU32(s, s->blockCRC);
704
705 /*
706 * Now a single bit indicating (non-)randomisation.
707 * As of version 0.9.5, we use a better sorting algorithm
708 * which makes randomisation unnecessary. So always set
709 * the randomised bit to 'no'. Of course, the decoder
710 * still needs to be able to handle randomised blocks
711 * so as to maintain backwards compatibility with
712 * older versions of bzip2.
713 */
Denys Vlasenkofc228b42018-02-03 14:56:43 +0100714 bsW1_0(s);
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000715
716 bsW(s, 24, s->origPtr);
717 generateMTFValues(s);
718 sendMTFValues(s);
719 }
720
721 /*-- If this is the last block, add the stream trailer. --*/
722 if (is_last_block) {
Denis Vlasenkoef3aabe2007-10-14 00:43:01 +0000723 /*bsPutU8(s, 0x17);*/
724 /*bsPutU8(s, 0x72);*/
725 /*bsPutU8(s, 0x45);*/
726 /*bsPutU8(s, 0x38);*/
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000727 bsPutU32(s, 0x17724538);
Denis Vlasenkoef3aabe2007-10-14 00:43:01 +0000728 /*bsPutU8(s, 0x50);*/
729 /*bsPutU8(s, 0x90);*/
730 bsPutU16(s, 0x5090);
Denis Vlasenko77f1ec12007-10-13 03:36:03 +0000731 bsPutU32(s, s->combinedCRC);
732 bsFinishWrite(s);
733 }
734}
735
736
737/*-------------------------------------------------------------*/
738/*--- end compress.c ---*/
739/*-------------------------------------------------------------*/