Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 1 | /* |
| 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 | /* ------------------------------------------------------------------ |
| 13 | This file is part of bzip2/libbzip2, a program and library for |
| 14 | lossless, block-sorting data compression. |
| 15 | |
| 16 | bzip2/libbzip2 version 1.0.4 of 20 December 2006 |
| 17 | Copyright (C) 1996-2006 Julian Seward <jseward@bzip.org> |
| 18 | |
| 19 | Please read the WARNING, DISCLAIMER and PATENTS sections in the |
| 20 | README file. |
| 21 | |
| 22 | This program is released under the terms of the license contained |
| 23 | in 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 | /*---------------------------------------------------*/ |
| 40 | static |
| 41 | void BZ2_bsInitWrite(EState* s) |
| 42 | { |
| 43 | s->bsLive = 0; |
| 44 | s->bsBuff = 0; |
| 45 | } |
| 46 | |
| 47 | |
| 48 | /*---------------------------------------------------*/ |
| 49 | static NOINLINE |
| 50 | void bsFinishWrite(EState* s) |
| 51 | { |
| 52 | while (s->bsLive > 0) { |
Denys Vlasenko | e594fb2 | 2018-02-03 01:30:12 +0100 | [diff] [blame] | 53 | *s->posZ++ = (uint8_t)(s->bsBuff >> 24); |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 54 | s->bsBuff <<= 8; |
| 55 | s->bsLive -= 8; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | |
| 60 | /*---------------------------------------------------*/ |
| 61 | static |
Denis Vlasenko | ef3aabe | 2007-10-14 00:43:01 +0000 | [diff] [blame] | 62 | /* Helps only on level 5, on other levels hurts. ? */ |
Denys Vlasenko | 522041e | 2011-09-10 13:25:57 +0200 | [diff] [blame] | 63 | #if CONFIG_BZIP2_FAST >= 5 |
Denis Vlasenko | ef3aabe | 2007-10-14 00:43:01 +0000 | [diff] [blame] | 64 | ALWAYS_INLINE |
| 65 | #endif |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 66 | void bsW(EState* s, int32_t n, uint32_t v) |
| 67 | { |
| 68 | while (s->bsLive >= 8) { |
Denys Vlasenko | e594fb2 | 2018-02-03 01:30:12 +0100 | [diff] [blame] | 69 | *s->posZ++ = (uint8_t)(s->bsBuff >> 24); |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 70 | s->bsBuff <<= 8; |
| 71 | s->bsLive -= 8; |
| 72 | } |
| 73 | s->bsBuff |= (v << (32 - s->bsLive - n)); |
| 74 | s->bsLive += n; |
| 75 | } |
Denys Vlasenko | 125c3ff | 2018-02-02 20:59:28 +0100 | [diff] [blame] | 76 | /* Same with n == 16: */ |
| 77 | static |
| 78 | #if CONFIG_BZIP2_FAST >= 5 |
| 79 | ALWAYS_INLINE |
| 80 | #endif |
| 81 | void bsW16(EState* s, uint32_t v) |
| 82 | { |
| 83 | while (s->bsLive >= 8) { |
Denys Vlasenko | e594fb2 | 2018-02-03 01:30:12 +0100 | [diff] [blame] | 84 | *s->posZ++ = (uint8_t)(s->bsBuff >> 24); |
Denys Vlasenko | 125c3ff | 2018-02-02 20:59:28 +0100 | [diff] [blame] | 85 | s->bsBuff <<= 8; |
| 86 | s->bsLive -= 8; |
| 87 | } |
| 88 | s->bsBuff |= (v << (16 - s->bsLive)); |
| 89 | s->bsLive += 16; |
| 90 | } |
Denys Vlasenko | feafb34 | 2018-02-03 04:43:46 +0100 | [diff] [blame] | 91 | /* Same with n == 1: */ |
| 92 | static |
Denys Vlasenko | fc228b4 | 2018-02-03 14:56:43 +0100 | [diff] [blame^] | 93 | ALWAYS_INLINE /* one callsite */ |
| 94 | void bsW1_1(EState* s) |
Denys Vlasenko | feafb34 | 2018-02-03 04:43:46 +0100 | [diff] [blame] | 95 | { |
| 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 Vlasenko | fc228b4 | 2018-02-03 14:56:43 +0100 | [diff] [blame^] | 102 | s->bsBuff |= (1 << (31 - s->bsLive)); |
| 103 | s->bsLive += 1; |
| 104 | } |
| 105 | static |
| 106 | #if CONFIG_BZIP2_FAST >= 5 |
| 107 | ALWAYS_INLINE |
| 108 | #endif |
| 109 | void 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 Vlasenko | feafb34 | 2018-02-03 04:43:46 +0100 | [diff] [blame] | 118 | s->bsLive += 1; |
| 119 | } |
Denys Vlasenko | 125c3ff | 2018-02-02 20:59:28 +0100 | [diff] [blame] | 120 | |
| 121 | |
| 122 | /*---------------------------------------------------*/ |
| 123 | static ALWAYS_INLINE |
| 124 | void bsPutU16(EState* s, unsigned u) |
| 125 | { |
| 126 | bsW16(s, u); |
| 127 | } |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 128 | |
| 129 | |
| 130 | /*---------------------------------------------------*/ |
| 131 | static |
Denis Vlasenko | ef3aabe | 2007-10-14 00:43:01 +0000 | [diff] [blame] | 132 | void bsPutU32(EState* s, unsigned u) |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 133 | { |
Denys Vlasenko | 125c3ff | 2018-02-02 20:59:28 +0100 | [diff] [blame] | 134 | //bsW(s, 32, u); // can't use: may try "uint32 << -n" |
| 135 | bsW16(s, (u >> 16) & 0xffff); |
| 136 | bsW16(s, u & 0xffff); |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | |
| 140 | /*---------------------------------------------------*/ |
| 141 | /*--- The back end proper ---*/ |
| 142 | /*---------------------------------------------------*/ |
| 143 | |
| 144 | /*---------------------------------------------------*/ |
| 145 | static |
| 146 | void makeMaps_e(EState* s) |
| 147 | { |
Denis Vlasenko | ef3aabe | 2007-10-14 00:43:01 +0000 | [diff] [blame] | 148 | int i; |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 149 | 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 | /*---------------------------------------------------*/ |
| 160 | static NOINLINE |
| 161 | void generateMTFValues(EState* s) |
| 162 | { |
Denis Vlasenko | ef3aabe | 2007-10-14 00:43:01 +0000 | [diff] [blame] | 163 | uint8_t yy[256]; |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 164 | 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 Vlasenko | ef3aabe | 2007-10-14 00:43:01 +0000 | [diff] [blame] | 173 | * ((uint8_t*)s->arr2)[0 .. s->nblock-1] |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 174 | * holds the original block data. |
| 175 | * |
| 176 | * The first thing to do is generate the MTF values, |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 177 | * and put them in ((uint16_t*)s->arr1)[0 .. s->nblock-1]. |
| 178 | * |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 179 | * 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 Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 184 | * area starting at &((uint8_t*)s->arr2)[s->nblock] |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 185 | * |
| 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 Vlasenko | ef3aabe | 2007-10-14 00:43:01 +0000 | [diff] [blame] | 191 | uint8_t* block = s->block; |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 192 | 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 Vlasenko | ef3aabe | 2007-10-14 00:43:01 +0000 | [diff] [blame] | 203 | yy[i] = (uint8_t) i; |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 204 | |
| 205 | for (i = 0; i < s->nblock; i++) { |
Denis Vlasenko | ef3aabe | 2007-10-14 00:43:01 +0000 | [diff] [blame] | 206 | uint8_t ll_i; |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 207 | AssertD(wr <= i, "generateMTFValues(1)"); |
Denis Vlasenko | ef3aabe | 2007-10-14 00:43:01 +0000 | [diff] [blame] | 208 | j = ptr[i] - 1; |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 209 | 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 Vlasenko | 6a9154b | 2007-10-14 07:49:48 +0000 | [diff] [blame] | 228 | zPend = (uint32_t)(zPend - 2) / 2; |
| 229 | /* bbox: unsigned div is easier */ |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 230 | }; |
| 231 | zPend = 0; |
| 232 | } |
| 233 | { |
Denis Vlasenko | ef3aabe | 2007-10-14 00:43:01 +0000 | [diff] [blame] | 234 | register uint8_t rtmp; |
| 235 | register uint8_t* ryy_j; |
| 236 | register uint8_t rll_i; |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 237 | rtmp = yy[1]; |
| 238 | yy[1] = yy[0]; |
| 239 | ryy_j = &(yy[1]); |
| 240 | rll_i = ll_i; |
| 241 | while (rll_i != rtmp) { |
Denis Vlasenko | ef3aabe | 2007-10-14 00:43:01 +0000 | [diff] [blame] | 242 | register uint8_t rtmp2; |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 243 | 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 Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | |
| 257 | if (zPend > 0) { |
| 258 | zPend--; |
| 259 | while (1) { |
| 260 | if (zPend & 1) { |
Denis Vlasenko | 6a9154b | 2007-10-14 07:49:48 +0000 | [diff] [blame] | 261 | mtfv[wr] = BZ_RUNB; |
| 262 | wr++; |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 263 | s->mtfFreq[BZ_RUNB]++; |
| 264 | } else { |
Denis Vlasenko | 6a9154b | 2007-10-14 07:49:48 +0000 | [diff] [blame] | 265 | mtfv[wr] = BZ_RUNA; |
| 266 | wr++; |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 267 | s->mtfFreq[BZ_RUNA]++; |
| 268 | } |
| 269 | if (zPend < 2) |
| 270 | break; |
Denis Vlasenko | 6a9154b | 2007-10-14 07:49:48 +0000 | [diff] [blame] | 271 | zPend = (uint32_t)(zPend - 2) / 2; |
| 272 | /* bbox: unsigned div is easier */ |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 273 | }; |
| 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 | |
| 289 | static NOINLINE |
| 290 | void sendMTFValues(EState* s) |
| 291 | { |
Denys Vlasenko | 3a2c97b | 2018-02-03 03:12:14 +0100 | [diff] [blame] | 292 | int32_t t, i; |
| 293 | unsigned iter; |
| 294 | unsigned gs; |
| 295 | int32_t alphaSize; |
| 296 | unsigned nSelectors, selCtr; |
Denys Vlasenko | 60a9414 | 2011-05-13 20:57:01 +0200 | [diff] [blame] | 297 | int32_t nGroups; |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 298 | |
| 299 | /* |
Denis Vlasenko | ef3aabe | 2007-10-14 00:43:01 +0000 | [diff] [blame] | 300 | * uint8_t len[BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE]; |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 301 | * 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 Vlasenko | ab80187 | 2007-12-02 08:35:37 +0000 | [diff] [blame] | 308 | #define code sendMTFValues__code |
| 309 | #define rfreq sendMTFValues__rfreq |
| 310 | #define len_pack sendMTFValues__len_pack |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 311 | |
Denys Vlasenko | 3a2c97b | 2018-02-03 03:12:14 +0100 | [diff] [blame] | 312 | unsigned /*uint16_t*/ cost[BZ_N_GROUPS]; |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 313 | |
| 314 | uint16_t* mtfv = s->mtfv; |
| 315 | |
Denis Vlasenko | ab80187 | 2007-12-02 08:35:37 +0000 | [diff] [blame] | 316 | alphaSize = s->nInUse + 2; |
Denys Vlasenko | 3a2c97b | 2018-02-03 03:12:14 +0100 | [diff] [blame] | 317 | for (t = 0; t < BZ_N_GROUPS; t++) { |
| 318 | unsigned v; |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 319 | for (v = 0; v < alphaSize; v++) |
| 320 | s->len[t][v] = BZ_GREATER_ICOST; |
Denys Vlasenko | 3a2c97b | 2018-02-03 03:12:14 +0100 | [diff] [blame] | 321 | } |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 322 | |
| 323 | /*--- Decide how many coding tables to use ---*/ |
| 324 | AssertH(s->nMTF > 0, 3001); |
Denys Vlasenko | 359230d | 2018-02-03 02:03:42 +0100 | [diff] [blame] | 325 | // 1..199 = 2 |
| 326 | // 200..599 = 3 |
| 327 | // 600..1199 = 4 |
| 328 | // 1200..2399 = 5 |
Denys Vlasenko | 982c44d | 2018-02-03 03:34:40 +0100 | [diff] [blame] | 329 | // 2400..99999 = 6 |
Denys Vlasenko | 359230d | 2018-02-03 02:03:42 +0100 | [diff] [blame] | 330 | nGroups = 2; |
| 331 | nGroups += (s->nMTF >= 200); |
| 332 | nGroups += (s->nMTF >= 600); |
| 333 | nGroups += (s->nMTF >= 1200); |
| 334 | nGroups += (s->nMTF >= 2400); |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 335 | |
| 336 | /*--- Generate an initial set of coding tables ---*/ |
| 337 | { |
Denys Vlasenko | 3a2c97b | 2018-02-03 03:12:14 +0100 | [diff] [blame] | 338 | unsigned nPart, remF; |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 339 | |
| 340 | nPart = nGroups; |
| 341 | remF = s->nMTF; |
| 342 | gs = 0; |
| 343 | while (nPart > 0) { |
Denys Vlasenko | 3a2c97b | 2018-02-03 03:12:14 +0100 | [diff] [blame] | 344 | unsigned v; |
| 345 | unsigned ge; |
| 346 | unsigned tFreq, aFreq; |
| 347 | |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 348 | tFreq = remF / nPart; |
Denys Vlasenko | 982c44d | 2018-02-03 03:34:40 +0100 | [diff] [blame] | 349 | ge = gs; |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 350 | aFreq = 0; |
Denys Vlasenko | 982c44d | 2018-02-03 03:34:40 +0100 | [diff] [blame] | 351 | while (aFreq < tFreq && ge < alphaSize) { |
| 352 | aFreq += s->mtfFreq[ge++]; |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 353 | } |
Denys Vlasenko | 982c44d | 2018-02-03 03:34:40 +0100 | [diff] [blame] | 354 | ge--; |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 355 | |
| 356 | if (ge > gs |
| 357 | && nPart != nGroups && nPart != 1 |
Denis Vlasenko | 6a9154b | 2007-10-14 07:49:48 +0000 | [diff] [blame] | 358 | && ((nGroups - nPart) % 2 == 1) /* bbox: can this be replaced by x & 1? */ |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 359 | ) { |
| 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 Vlasenko | 6a9154b | 2007-10-14 07:49:48 +0000 | [diff] [blame] | 371 | gs = ge + 1; |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 372 | 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 Vlasenko | 3a2c97b | 2018-02-03 03:12:14 +0100 | [diff] [blame] | 380 | for (t = 0; t < nGroups; t++) { |
| 381 | unsigned v; |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 382 | for (v = 0; v < alphaSize; v++) |
| 383 | s->rfreq[t][v] = 0; |
Denys Vlasenko | 3a2c97b | 2018-02-03 03:12:14 +0100 | [diff] [blame] | 384 | } |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 385 | |
Denys Vlasenko | 522041e | 2011-09-10 13:25:57 +0200 | [diff] [blame] | 386 | #if CONFIG_BZIP2_FAST >= 5 |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 387 | /* |
| 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 Vlasenko | 3a2c97b | 2018-02-03 03:12:14 +0100 | [diff] [blame] | 392 | unsigned v; |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 393 | 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 Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 400 | nSelectors = 0; |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 401 | gs = 0; |
| 402 | while (1) { |
Denys Vlasenko | 3a2c97b | 2018-02-03 03:12:14 +0100 | [diff] [blame] | 403 | unsigned ge; |
| 404 | unsigned bt, bc; |
| 405 | |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 406 | /*--- 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 Vlasenko | 522041e | 2011-09-10 13:25:57 +0200 | [diff] [blame] | 419 | #if CONFIG_BZIP2_FAST >= 5 |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 420 | 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 Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 444 | } else |
| 445 | #endif |
| 446 | { |
| 447 | /*--- slow version which correctly handles all situations ---*/ |
| 448 | for (i = gs; i <= ge; i++) { |
Denys Vlasenko | 3a2c97b | 2018-02-03 03:12:14 +0100 | [diff] [blame] | 449 | unsigned /*uint16_t*/ icv = mtfv[i]; |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 450 | 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 Vlasenko | ef3aabe | 2007-10-14 00:43:01 +0000 | [diff] [blame] | 458 | /*bc = 999999999;*/ |
| 459 | /*bt = -1;*/ |
| 460 | bc = cost[0]; |
| 461 | bt = 0; |
| 462 | for (t = 1 /*0*/; t < nGroups; t++) { |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 463 | if (cost[t] < bc) { |
| 464 | bc = cost[t]; |
| 465 | bt = t; |
| 466 | } |
| 467 | } |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 468 | s->selector[nSelectors] = bt; |
| 469 | nSelectors++; |
| 470 | |
| 471 | /* |
| 472 | * Increment the symbol frequencies for the selected table. |
| 473 | */ |
Denis Vlasenko | 0de37e1 | 2007-10-17 11:08:53 +0000 | [diff] [blame] | 474 | /* 1% faster compress. +800 bytes */ |
Denys Vlasenko | 522041e | 2011-09-10 13:25:57 +0200 | [diff] [blame] | 475 | #if CONFIG_BZIP2_FAST >= 4 |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 476 | 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 Vlasenko | ef3aabe | 2007-10-14 00:43:01 +0000 | [diff] [blame] | 490 | gs = ge + 1; |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 491 | } 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 Vlasenko | ef3aabe | 2007-10-14 00:43:01 +0000 | [diff] [blame] | 499 | /* already is: gs = ge + 1; */ |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 500 | } |
| 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 Vlasenko | ab80187 | 2007-12-02 08:35:37 +0000 | [diff] [blame] | 509 | BZ2_hbMakeCodeLengths(s, &(s->len[t][0]), &(s->rfreq[t][0]), alphaSize, 17 /*20*/); |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | AssertH(nGroups < 8, 3002); |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 513 | AssertH(nSelectors < 32768 && nSelectors <= (2 + (900000 / BZ_G_SIZE)), 3003); |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 514 | |
| 515 | /*--- Compute MTF values for the selectors. ---*/ |
| 516 | { |
Denis Vlasenko | ef3aabe | 2007-10-14 00:43:01 +0000 | [diff] [blame] | 517 | uint8_t pos[BZ_N_GROUPS], ll_i, tmp2, tmp; |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 518 | |
| 519 | for (i = 0; i < nGroups; i++) |
| 520 | pos[i] = i; |
| 521 | for (i = 0; i < nSelectors; i++) { |
Denys Vlasenko | 3a2c97b | 2018-02-03 03:12:14 +0100 | [diff] [blame] | 522 | unsigned j; |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 523 | 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 Vlasenko | 3a2c97b | 2018-02-03 03:12:14 +0100 | [diff] [blame] | 539 | unsigned minLen = 32; //todo: s->len[t][0]; |
| 540 | unsigned maxLen = 0; //todo: s->len[t][0]; |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 541 | 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 Vlasenko | ef3aabe | 2007-10-14 00:43:01 +0000 | [diff] [blame] | 552 | /* bbox: optimized a bit more than in bzip2 */ |
| 553 | int inUse16 = 0; |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 554 | for (i = 0; i < 16; i++) { |
Denis Vlasenko | ef3aabe | 2007-10-14 00:43:01 +0000 | [diff] [blame] | 555 | if (sizeof(long) <= 4) { |
| 556 | inUse16 = inUse16*2 + |
Denys Vlasenko | 5117eff | 2013-10-16 14:21:20 +0200 | [diff] [blame] | 557 | ((*(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 Vlasenko | ef3aabe | 2007-10-14 00:43:01 +0000 | [diff] [blame] | 561 | } else { /* Our CPU can do better */ |
| 562 | inUse16 = inUse16*2 + |
Denys Vlasenko | 5117eff | 2013-10-16 14:21:20 +0200 | [diff] [blame] | 563 | ((*(bb__aliased_uint64_t*)&(s->inUse[i * 16 + 0]) |
| 564 | | *(bb__aliased_uint64_t*)&(s->inUse[i * 16 + 8])) != 0); |
Denis Vlasenko | ef3aabe | 2007-10-14 00:43:01 +0000 | [diff] [blame] | 565 | } |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 566 | } |
| 567 | |
Denys Vlasenko | 125c3ff | 2018-02-02 20:59:28 +0100 | [diff] [blame] | 568 | bsW16(s, inUse16); |
Denis Vlasenko | ef3aabe | 2007-10-14 00:43:01 +0000 | [diff] [blame] | 569 | |
| 570 | inUse16 <<= (sizeof(int)*8 - 16); /* move 15th bit into sign bit */ |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 571 | for (i = 0; i < 16; i++) { |
Denis Vlasenko | ef3aabe | 2007-10-14 00:43:01 +0000 | [diff] [blame] | 572 | if (inUse16 < 0) { |
| 573 | unsigned v16 = 0; |
Denys Vlasenko | 3a2c97b | 2018-02-03 03:12:14 +0100 | [diff] [blame] | 574 | unsigned j; |
Denis Vlasenko | ef3aabe | 2007-10-14 00:43:01 +0000 | [diff] [blame] | 575 | for (j = 0; j < 16; j++) |
| 576 | v16 = v16*2 + s->inUse[i * 16 + j]; |
Denys Vlasenko | 125c3ff | 2018-02-02 20:59:28 +0100 | [diff] [blame] | 577 | bsW16(s, v16); |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 578 | } |
Denis Vlasenko | ef3aabe | 2007-10-14 00:43:01 +0000 | [diff] [blame] | 579 | inUse16 <<= 1; |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 580 | } |
| 581 | } |
| 582 | |
| 583 | /*--- Now the selectors. ---*/ |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 584 | bsW(s, 3, nGroups); |
| 585 | bsW(s, 15, nSelectors); |
| 586 | for (i = 0; i < nSelectors; i++) { |
Denys Vlasenko | 3a2c97b | 2018-02-03 03:12:14 +0100 | [diff] [blame] | 587 | unsigned j; |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 588 | for (j = 0; j < s->selectorMtf[i]; j++) |
Denys Vlasenko | fc228b4 | 2018-02-03 14:56:43 +0100 | [diff] [blame^] | 589 | bsW1_1(s); |
| 590 | bsW1_0(s); |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | /*--- Now the coding tables. ---*/ |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 594 | for (t = 0; t < nGroups; t++) { |
Denys Vlasenko | 3a2c97b | 2018-02-03 03:12:14 +0100 | [diff] [blame] | 595 | unsigned curr = s->len[t][0]; |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 596 | 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 Vlasenko | fc228b4 | 2018-02-03 14:56:43 +0100 | [diff] [blame^] | 600 | bsW1_0(s); |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 601 | } |
| 602 | } |
| 603 | |
| 604 | /*--- And finally, the block data proper ---*/ |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 605 | selCtr = 0; |
| 606 | gs = 0; |
| 607 | while (1) { |
Denys Vlasenko | 3a2c97b | 2018-02-03 03:12:14 +0100 | [diff] [blame] | 608 | unsigned ge; |
| 609 | |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 610 | 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 Vlasenko | ef3aabe | 2007-10-14 00:43:01 +0000 | [diff] [blame] | 622 | uint8_t* s_len_sel_selCtr = &(s->len[s->selector[selCtr]][0]); |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 623 | 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 Vlasenko | ef3aabe | 2007-10-14 00:43:01 +0000 | [diff] [blame] | 644 | uint8_t* s_len_sel_selCtr = &(s->len [s->selector[selCtr]][0]); |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 645 | 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 Vlasenko | ab80187 | 2007-12-02 08:35:37 +0000 | [diff] [blame] | 658 | #undef code |
| 659 | #undef rfreq |
| 660 | #undef len_pack |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | |
| 664 | /*---------------------------------------------------*/ |
| 665 | static |
Denis Vlasenko | ef3aabe | 2007-10-14 00:43:01 +0000 | [diff] [blame] | 666 | void BZ2_compressBlock(EState* s, int is_last_block) |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 667 | { |
| 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 Vlasenko | e594fb2 | 2018-02-03 01:30:12 +0100 | [diff] [blame] | 673 | s->posZ = s->zbits; // was: s->numZ = 0; |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 674 | |
| 675 | BZ2_blockSort(s); |
| 676 | } |
| 677 | |
Denis Vlasenko | ef3aabe | 2007-10-14 00:43:01 +0000 | [diff] [blame] | 678 | s->zbits = &((uint8_t*)s->arr2)[s->nblock]; |
Denys Vlasenko | e594fb2 | 2018-02-03 01:30:12 +0100 | [diff] [blame] | 679 | s->posZ = s->zbits; |
| 680 | s->state_out_pos = s->zbits; |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 681 | |
| 682 | /*-- If this is the first block, create the stream header. --*/ |
| 683 | if (s->blockNo == 1) { |
| 684 | BZ2_bsInitWrite(s); |
Denis Vlasenko | ef3aabe | 2007-10-14 00:43:01 +0000 | [diff] [blame] | 685 | /*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 Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 689 | bsPutU32(s, BZ_HDR_BZh0 + s->blockSize100k); |
| 690 | } |
| 691 | |
| 692 | if (s->nblock > 0) { |
Denis Vlasenko | ef3aabe | 2007-10-14 00:43:01 +0000 | [diff] [blame] | 693 | /*bsPutU8(s, 0x31);*/ |
| 694 | /*bsPutU8(s, 0x41);*/ |
| 695 | /*bsPutU8(s, 0x59);*/ |
| 696 | /*bsPutU8(s, 0x26);*/ |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 697 | bsPutU32(s, 0x31415926); |
Denis Vlasenko | ef3aabe | 2007-10-14 00:43:01 +0000 | [diff] [blame] | 698 | /*bsPutU8(s, 0x53);*/ |
| 699 | /*bsPutU8(s, 0x59);*/ |
| 700 | bsPutU16(s, 0x5359); |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 701 | |
| 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 Vlasenko | fc228b4 | 2018-02-03 14:56:43 +0100 | [diff] [blame^] | 714 | bsW1_0(s); |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 715 | |
| 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 Vlasenko | ef3aabe | 2007-10-14 00:43:01 +0000 | [diff] [blame] | 723 | /*bsPutU8(s, 0x17);*/ |
| 724 | /*bsPutU8(s, 0x72);*/ |
| 725 | /*bsPutU8(s, 0x45);*/ |
| 726 | /*bsPutU8(s, 0x38);*/ |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 727 | bsPutU32(s, 0x17724538); |
Denis Vlasenko | ef3aabe | 2007-10-14 00:43:01 +0000 | [diff] [blame] | 728 | /*bsPutU8(s, 0x50);*/ |
| 729 | /*bsPutU8(s, 0x90);*/ |
| 730 | bsPutU16(s, 0x5090); |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 731 | bsPutU32(s, s->combinedCRC); |
| 732 | bsFinishWrite(s); |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | |
| 737 | /*-------------------------------------------------------------*/ |
| 738 | /*--- end compress.c ---*/ |
| 739 | /*-------------------------------------------------------------*/ |