compress.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  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. /*--- Compression machinery (not incl block sorting) ---*/
  8. /*--- compress.c ---*/
  9. /*-------------------------------------------------------------*/
  10. /* ------------------------------------------------------------------
  11. This file is part of bzip2/libbzip2, a program and library for
  12. lossless, block-sorting data compression.
  13. bzip2/libbzip2 version 1.0.4 of 20 December 2006
  14. Copyright (C) 1996-2006 Julian Seward <jseward@bzip.org>
  15. Please read the WARNING, DISCLAIMER and PATENTS sections in the
  16. README file.
  17. This program is released under the terms of the license contained
  18. in the file LICENSE.
  19. ------------------------------------------------------------------ */
  20. /* CHANGES
  21. * 0.9.0 -- original version.
  22. * 0.9.0a/b -- no changes in this file.
  23. * 0.9.0c -- changed setting of nGroups in sendMTFValues()
  24. * so as to do a bit better on small files
  25. */
  26. /* #include "bzlib_private.h" */
  27. /*---------------------------------------------------*/
  28. /*--- Bit stream I/O ---*/
  29. /*---------------------------------------------------*/
  30. /*---------------------------------------------------*/
  31. static
  32. void BZ2_bsInitWrite(EState* s)
  33. {
  34. s->bsLive = 0;
  35. s->bsBuff = 0;
  36. }
  37. /*---------------------------------------------------*/
  38. static NOINLINE
  39. void bsFinishWrite(EState* s)
  40. {
  41. while (s->bsLive > 0) {
  42. s->zbits[s->numZ] = (uint8_t)(s->bsBuff >> 24);
  43. s->numZ++;
  44. s->bsBuff <<= 8;
  45. s->bsLive -= 8;
  46. }
  47. }
  48. /*---------------------------------------------------*/
  49. static
  50. /* Helps only on level 5, on other levels hurts. ? */
  51. #if CONFIG_BZIP2_FEATURE_SPEED >= 5
  52. ALWAYS_INLINE
  53. #endif
  54. void bsW(EState* s, int32_t n, uint32_t v)
  55. {
  56. while (s->bsLive >= 8) {
  57. s->zbits[s->numZ] = (uint8_t)(s->bsBuff >> 24);
  58. s->numZ++;
  59. s->bsBuff <<= 8;
  60. s->bsLive -= 8;
  61. }
  62. s->bsBuff |= (v << (32 - s->bsLive - n));
  63. s->bsLive += n;
  64. }
  65. /*---------------------------------------------------*/
  66. static
  67. void bsPutU32(EState* s, unsigned u)
  68. {
  69. bsW(s, 8, (u >> 24) & 0xff);
  70. bsW(s, 8, (u >> 16) & 0xff);
  71. bsW(s, 8, (u >> 8) & 0xff);
  72. bsW(s, 8, u & 0xff);
  73. }
  74. /*---------------------------------------------------*/
  75. static
  76. void bsPutU16(EState* s, unsigned u)
  77. {
  78. bsW(s, 8, (u >> 8) & 0xff);
  79. bsW(s, 8, u & 0xff);
  80. }
  81. /*---------------------------------------------------*/
  82. /*--- The back end proper ---*/
  83. /*---------------------------------------------------*/
  84. /*---------------------------------------------------*/
  85. static
  86. void makeMaps_e(EState* s)
  87. {
  88. int i;
  89. s->nInUse = 0;
  90. for (i = 0; i < 256; i++) {
  91. if (s->inUse[i]) {
  92. s->unseqToSeq[i] = s->nInUse;
  93. s->nInUse++;
  94. }
  95. }
  96. }
  97. /*---------------------------------------------------*/
  98. static NOINLINE
  99. void generateMTFValues(EState* s)
  100. {
  101. uint8_t yy[256];
  102. int32_t i, j;
  103. int32_t zPend;
  104. int32_t wr;
  105. int32_t EOB;
  106. /*
  107. * After sorting (eg, here),
  108. * s->arr1[0 .. s->nblock-1] holds sorted order,
  109. * and
  110. * ((uint8_t*)s->arr2)[0 .. s->nblock-1]
  111. * holds the original block data.
  112. *
  113. * The first thing to do is generate the MTF values,
  114. * and put them in ((uint16_t*)s->arr1)[0 .. s->nblock-1].
  115. *
  116. * Because there are strictly fewer or equal MTF values
  117. * than block values, ptr values in this area are overwritten
  118. * with MTF values only when they are no longer needed.
  119. *
  120. * The final compressed bitstream is generated into the
  121. * area starting at &((uint8_t*)s->arr2)[s->nblock]
  122. *
  123. * These storage aliases are set up in bzCompressInit(),
  124. * except for the last one, which is arranged in
  125. * compressBlock().
  126. */
  127. uint32_t* ptr = s->ptr;
  128. uint8_t* block = s->block;
  129. uint16_t* mtfv = s->mtfv;
  130. makeMaps_e(s);
  131. EOB = s->nInUse+1;
  132. for (i = 0; i <= EOB; i++)
  133. s->mtfFreq[i] = 0;
  134. wr = 0;
  135. zPend = 0;
  136. for (i = 0; i < s->nInUse; i++)
  137. yy[i] = (uint8_t) i;
  138. for (i = 0; i < s->nblock; i++) {
  139. uint8_t ll_i;
  140. AssertD(wr <= i, "generateMTFValues(1)");
  141. j = ptr[i] - 1;
  142. if (j < 0)
  143. j += s->nblock;
  144. ll_i = s->unseqToSeq[block[j]];
  145. AssertD(ll_i < s->nInUse, "generateMTFValues(2a)");
  146. if (yy[0] == ll_i) {
  147. zPend++;
  148. } else {
  149. if (zPend > 0) {
  150. zPend--;
  151. while (1) {
  152. if (zPend & 1) {
  153. mtfv[wr] = BZ_RUNB; wr++;
  154. s->mtfFreq[BZ_RUNB]++;
  155. } else {
  156. mtfv[wr] = BZ_RUNA; wr++;
  157. s->mtfFreq[BZ_RUNA]++;
  158. }
  159. if (zPend < 2) break;
  160. zPend = (uint32_t)(zPend - 2) / 2;
  161. /* bbox: unsigned div is easier */
  162. };
  163. zPend = 0;
  164. }
  165. {
  166. register uint8_t rtmp;
  167. register uint8_t* ryy_j;
  168. register uint8_t rll_i;
  169. rtmp = yy[1];
  170. yy[1] = yy[0];
  171. ryy_j = &(yy[1]);
  172. rll_i = ll_i;
  173. while (rll_i != rtmp) {
  174. register uint8_t rtmp2;
  175. ryy_j++;
  176. rtmp2 = rtmp;
  177. rtmp = *ryy_j;
  178. *ryy_j = rtmp2;
  179. };
  180. yy[0] = rtmp;
  181. j = ryy_j - &(yy[0]);
  182. mtfv[wr] = j+1;
  183. wr++;
  184. s->mtfFreq[j+1]++;
  185. }
  186. }
  187. }
  188. if (zPend > 0) {
  189. zPend--;
  190. while (1) {
  191. if (zPend & 1) {
  192. mtfv[wr] = BZ_RUNB;
  193. wr++;
  194. s->mtfFreq[BZ_RUNB]++;
  195. } else {
  196. mtfv[wr] = BZ_RUNA;
  197. wr++;
  198. s->mtfFreq[BZ_RUNA]++;
  199. }
  200. if (zPend < 2)
  201. break;
  202. zPend = (uint32_t)(zPend - 2) / 2;
  203. /* bbox: unsigned div is easier */
  204. };
  205. zPend = 0;
  206. }
  207. mtfv[wr] = EOB;
  208. wr++;
  209. s->mtfFreq[EOB]++;
  210. s->nMTF = wr;
  211. }
  212. /*---------------------------------------------------*/
  213. #define BZ_LESSER_ICOST 0
  214. #define BZ_GREATER_ICOST 15
  215. static NOINLINE
  216. void sendMTFValues(EState* s)
  217. {
  218. int32_t v, t, i, j, gs, ge, totc, bt, bc, iter;
  219. int32_t nSelectors, alphaSize, minLen, maxLen, selCtr;
  220. int32_t nGroups;
  221. /*
  222. * uint8_t len[BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
  223. * is a global since the decoder also needs it.
  224. *
  225. * int32_t code[BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
  226. * int32_t rfreq[BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
  227. * are also globals only used in this proc.
  228. * Made global to keep stack frame size small.
  229. */
  230. #define code sendMTFValues__code
  231. #define rfreq sendMTFValues__rfreq
  232. #define len_pack sendMTFValues__len_pack
  233. uint16_t cost[BZ_N_GROUPS];
  234. int32_t fave[BZ_N_GROUPS];
  235. uint16_t* mtfv = s->mtfv;
  236. alphaSize = s->nInUse + 2;
  237. for (t = 0; t < BZ_N_GROUPS; t++)
  238. for (v = 0; v < alphaSize; v++)
  239. s->len[t][v] = BZ_GREATER_ICOST;
  240. /*--- Decide how many coding tables to use ---*/
  241. AssertH(s->nMTF > 0, 3001);
  242. if (s->nMTF < 200) nGroups = 2; else
  243. if (s->nMTF < 600) nGroups = 3; else
  244. if (s->nMTF < 1200) nGroups = 4; else
  245. if (s->nMTF < 2400) nGroups = 5; else
  246. nGroups = 6;
  247. /*--- Generate an initial set of coding tables ---*/
  248. {
  249. int32_t nPart, remF, tFreq, aFreq;
  250. nPart = nGroups;
  251. remF = s->nMTF;
  252. gs = 0;
  253. while (nPart > 0) {
  254. tFreq = remF / nPart;
  255. ge = gs - 1;
  256. aFreq = 0;
  257. while (aFreq < tFreq && ge < alphaSize-1) {
  258. ge++;
  259. aFreq += s->mtfFreq[ge];
  260. }
  261. if (ge > gs
  262. && nPart != nGroups && nPart != 1
  263. && ((nGroups - nPart) % 2 == 1) /* bbox: can this be replaced by x & 1? */
  264. ) {
  265. aFreq -= s->mtfFreq[ge];
  266. ge--;
  267. }
  268. for (v = 0; v < alphaSize; v++)
  269. if (v >= gs && v <= ge)
  270. s->len[nPart-1][v] = BZ_LESSER_ICOST;
  271. else
  272. s->len[nPart-1][v] = BZ_GREATER_ICOST;
  273. nPart--;
  274. gs = ge + 1;
  275. remF -= aFreq;
  276. }
  277. }
  278. /*
  279. * Iterate up to BZ_N_ITERS times to improve the tables.
  280. */
  281. for (iter = 0; iter < BZ_N_ITERS; iter++) {
  282. for (t = 0; t < nGroups; t++)
  283. fave[t] = 0;
  284. for (t = 0; t < nGroups; t++)
  285. for (v = 0; v < alphaSize; v++)
  286. s->rfreq[t][v] = 0;
  287. #if CONFIG_BZIP2_FEATURE_SPEED >= 5
  288. /*
  289. * Set up an auxiliary length table which is used to fast-track
  290. * the common case (nGroups == 6).
  291. */
  292. if (nGroups == 6) {
  293. for (v = 0; v < alphaSize; v++) {
  294. s->len_pack[v][0] = (s->len[1][v] << 16) | s->len[0][v];
  295. s->len_pack[v][1] = (s->len[3][v] << 16) | s->len[2][v];
  296. s->len_pack[v][2] = (s->len[5][v] << 16) | s->len[4][v];
  297. }
  298. }
  299. #endif
  300. nSelectors = 0;
  301. totc = 0;
  302. gs = 0;
  303. while (1) {
  304. /*--- Set group start & end marks. --*/
  305. if (gs >= s->nMTF)
  306. break;
  307. ge = gs + BZ_G_SIZE - 1;
  308. if (ge >= s->nMTF)
  309. ge = s->nMTF-1;
  310. /*
  311. * Calculate the cost of this group as coded
  312. * by each of the coding tables.
  313. */
  314. for (t = 0; t < nGroups; t++)
  315. cost[t] = 0;
  316. #if CONFIG_BZIP2_FEATURE_SPEED >= 5
  317. if (nGroups == 6 && 50 == ge-gs+1) {
  318. /*--- fast track the common case ---*/
  319. register uint32_t cost01, cost23, cost45;
  320. register uint16_t icv;
  321. cost01 = cost23 = cost45 = 0;
  322. #define BZ_ITER(nn) \
  323. icv = mtfv[gs+(nn)]; \
  324. cost01 += s->len_pack[icv][0]; \
  325. cost23 += s->len_pack[icv][1]; \
  326. cost45 += s->len_pack[icv][2];
  327. BZ_ITER(0); BZ_ITER(1); BZ_ITER(2); BZ_ITER(3); BZ_ITER(4);
  328. BZ_ITER(5); BZ_ITER(6); BZ_ITER(7); BZ_ITER(8); BZ_ITER(9);
  329. BZ_ITER(10); BZ_ITER(11); BZ_ITER(12); BZ_ITER(13); BZ_ITER(14);
  330. BZ_ITER(15); BZ_ITER(16); BZ_ITER(17); BZ_ITER(18); BZ_ITER(19);
  331. BZ_ITER(20); BZ_ITER(21); BZ_ITER(22); BZ_ITER(23); BZ_ITER(24);
  332. BZ_ITER(25); BZ_ITER(26); BZ_ITER(27); BZ_ITER(28); BZ_ITER(29);
  333. BZ_ITER(30); BZ_ITER(31); BZ_ITER(32); BZ_ITER(33); BZ_ITER(34);
  334. BZ_ITER(35); BZ_ITER(36); BZ_ITER(37); BZ_ITER(38); BZ_ITER(39);
  335. BZ_ITER(40); BZ_ITER(41); BZ_ITER(42); BZ_ITER(43); BZ_ITER(44);
  336. BZ_ITER(45); BZ_ITER(46); BZ_ITER(47); BZ_ITER(48); BZ_ITER(49);
  337. #undef BZ_ITER
  338. cost[0] = cost01 & 0xffff; cost[1] = cost01 >> 16;
  339. cost[2] = cost23 & 0xffff; cost[3] = cost23 >> 16;
  340. cost[4] = cost45 & 0xffff; cost[5] = cost45 >> 16;
  341. } else
  342. #endif
  343. {
  344. /*--- slow version which correctly handles all situations ---*/
  345. for (i = gs; i <= ge; i++) {
  346. uint16_t icv = mtfv[i];
  347. for (t = 0; t < nGroups; t++)
  348. cost[t] += s->len[t][icv];
  349. }
  350. }
  351. /*
  352. * Find the coding table which is best for this group,
  353. * and record its identity in the selector table.
  354. */
  355. /*bc = 999999999;*/
  356. /*bt = -1;*/
  357. bc = cost[0];
  358. bt = 0;
  359. for (t = 1 /*0*/; t < nGroups; t++) {
  360. if (cost[t] < bc) {
  361. bc = cost[t];
  362. bt = t;
  363. }
  364. }
  365. totc += bc;
  366. fave[bt]++;
  367. s->selector[nSelectors] = bt;
  368. nSelectors++;
  369. /*
  370. * Increment the symbol frequencies for the selected table.
  371. */
  372. /* 1% faster compress. +800 bytes */
  373. #if CONFIG_BZIP2_FEATURE_SPEED >= 4
  374. if (nGroups == 6 && 50 == ge-gs+1) {
  375. /*--- fast track the common case ---*/
  376. #define BZ_ITUR(nn) s->rfreq[bt][mtfv[gs + (nn)]]++
  377. BZ_ITUR(0); BZ_ITUR(1); BZ_ITUR(2); BZ_ITUR(3); BZ_ITUR(4);
  378. BZ_ITUR(5); BZ_ITUR(6); BZ_ITUR(7); BZ_ITUR(8); BZ_ITUR(9);
  379. BZ_ITUR(10); BZ_ITUR(11); BZ_ITUR(12); BZ_ITUR(13); BZ_ITUR(14);
  380. BZ_ITUR(15); BZ_ITUR(16); BZ_ITUR(17); BZ_ITUR(18); BZ_ITUR(19);
  381. BZ_ITUR(20); BZ_ITUR(21); BZ_ITUR(22); BZ_ITUR(23); BZ_ITUR(24);
  382. BZ_ITUR(25); BZ_ITUR(26); BZ_ITUR(27); BZ_ITUR(28); BZ_ITUR(29);
  383. BZ_ITUR(30); BZ_ITUR(31); BZ_ITUR(32); BZ_ITUR(33); BZ_ITUR(34);
  384. BZ_ITUR(35); BZ_ITUR(36); BZ_ITUR(37); BZ_ITUR(38); BZ_ITUR(39);
  385. BZ_ITUR(40); BZ_ITUR(41); BZ_ITUR(42); BZ_ITUR(43); BZ_ITUR(44);
  386. BZ_ITUR(45); BZ_ITUR(46); BZ_ITUR(47); BZ_ITUR(48); BZ_ITUR(49);
  387. #undef BZ_ITUR
  388. gs = ge + 1;
  389. } else
  390. #endif
  391. {
  392. /*--- slow version which correctly handles all situations ---*/
  393. while (gs <= ge) {
  394. s->rfreq[bt][mtfv[gs]]++;
  395. gs++;
  396. }
  397. /* already is: gs = ge + 1; */
  398. }
  399. }
  400. /*
  401. * Recompute the tables based on the accumulated frequencies.
  402. */
  403. /* maxLen was changed from 20 to 17 in bzip2-1.0.3. See
  404. * comment in huffman.c for details. */
  405. for (t = 0; t < nGroups; t++)
  406. BZ2_hbMakeCodeLengths(s, &(s->len[t][0]), &(s->rfreq[t][0]), alphaSize, 17 /*20*/);
  407. }
  408. AssertH(nGroups < 8, 3002);
  409. AssertH(nSelectors < 32768 && nSelectors <= (2 + (900000 / BZ_G_SIZE)), 3003);
  410. /*--- Compute MTF values for the selectors. ---*/
  411. {
  412. uint8_t pos[BZ_N_GROUPS], ll_i, tmp2, tmp;
  413. for (i = 0; i < nGroups; i++)
  414. pos[i] = i;
  415. for (i = 0; i < nSelectors; i++) {
  416. ll_i = s->selector[i];
  417. j = 0;
  418. tmp = pos[j];
  419. while (ll_i != tmp) {
  420. j++;
  421. tmp2 = tmp;
  422. tmp = pos[j];
  423. pos[j] = tmp2;
  424. };
  425. pos[0] = tmp;
  426. s->selectorMtf[i] = j;
  427. }
  428. };
  429. /*--- Assign actual codes for the tables. --*/
  430. for (t = 0; t < nGroups; t++) {
  431. minLen = 32;
  432. maxLen = 0;
  433. for (i = 0; i < alphaSize; i++) {
  434. if (s->len[t][i] > maxLen) maxLen = s->len[t][i];
  435. if (s->len[t][i] < minLen) minLen = s->len[t][i];
  436. }
  437. AssertH(!(maxLen > 17 /*20*/), 3004);
  438. AssertH(!(minLen < 1), 3005);
  439. BZ2_hbAssignCodes(&(s->code[t][0]), &(s->len[t][0]), minLen, maxLen, alphaSize);
  440. }
  441. /*--- Transmit the mapping table. ---*/
  442. {
  443. /* bbox: optimized a bit more than in bzip2 */
  444. int inUse16 = 0;
  445. for (i = 0; i < 16; i++) {
  446. if (sizeof(long) <= 4) {
  447. inUse16 = inUse16*2 +
  448. ((*(uint32_t*)&(s->inUse[i * 16 + 0])
  449. | *(uint32_t*)&(s->inUse[i * 16 + 4])
  450. | *(uint32_t*)&(s->inUse[i * 16 + 8])
  451. | *(uint32_t*)&(s->inUse[i * 16 + 12])) != 0);
  452. } else { /* Our CPU can do better */
  453. inUse16 = inUse16*2 +
  454. ((*(uint64_t*)&(s->inUse[i * 16 + 0])
  455. | *(uint64_t*)&(s->inUse[i * 16 + 8])) != 0);
  456. }
  457. }
  458. bsW(s, 16, inUse16);
  459. inUse16 <<= (sizeof(int)*8 - 16); /* move 15th bit into sign bit */
  460. for (i = 0; i < 16; i++) {
  461. if (inUse16 < 0) {
  462. unsigned v16 = 0;
  463. for (j = 0; j < 16; j++)
  464. v16 = v16*2 + s->inUse[i * 16 + j];
  465. bsW(s, 16, v16);
  466. }
  467. inUse16 <<= 1;
  468. }
  469. }
  470. /*--- Now the selectors. ---*/
  471. bsW(s, 3, nGroups);
  472. bsW(s, 15, nSelectors);
  473. for (i = 0; i < nSelectors; i++) {
  474. for (j = 0; j < s->selectorMtf[i]; j++)
  475. bsW(s, 1, 1);
  476. bsW(s, 1, 0);
  477. }
  478. /*--- Now the coding tables. ---*/
  479. for (t = 0; t < nGroups; t++) {
  480. int32_t curr = s->len[t][0];
  481. bsW(s, 5, curr);
  482. for (i = 0; i < alphaSize; i++) {
  483. while (curr < s->len[t][i]) { bsW(s, 2, 2); curr++; /* 10 */ };
  484. while (curr > s->len[t][i]) { bsW(s, 2, 3); curr--; /* 11 */ };
  485. bsW(s, 1, 0);
  486. }
  487. }
  488. /*--- And finally, the block data proper ---*/
  489. selCtr = 0;
  490. gs = 0;
  491. while (1) {
  492. if (gs >= s->nMTF)
  493. break;
  494. ge = gs + BZ_G_SIZE - 1;
  495. if (ge >= s->nMTF)
  496. ge = s->nMTF-1;
  497. AssertH(s->selector[selCtr] < nGroups, 3006);
  498. /* Costs 1300 bytes and is _slower_ (on Intel Core 2) */
  499. #if 0
  500. if (nGroups == 6 && 50 == ge-gs+1) {
  501. /*--- fast track the common case ---*/
  502. uint16_t mtfv_i;
  503. uint8_t* s_len_sel_selCtr = &(s->len[s->selector[selCtr]][0]);
  504. int32_t* s_code_sel_selCtr = &(s->code[s->selector[selCtr]][0]);
  505. #define BZ_ITAH(nn) \
  506. mtfv_i = mtfv[gs+(nn)]; \
  507. bsW(s, s_len_sel_selCtr[mtfv_i], s_code_sel_selCtr[mtfv_i])
  508. BZ_ITAH(0); BZ_ITAH(1); BZ_ITAH(2); BZ_ITAH(3); BZ_ITAH(4);
  509. BZ_ITAH(5); BZ_ITAH(6); BZ_ITAH(7); BZ_ITAH(8); BZ_ITAH(9);
  510. BZ_ITAH(10); BZ_ITAH(11); BZ_ITAH(12); BZ_ITAH(13); BZ_ITAH(14);
  511. BZ_ITAH(15); BZ_ITAH(16); BZ_ITAH(17); BZ_ITAH(18); BZ_ITAH(19);
  512. BZ_ITAH(20); BZ_ITAH(21); BZ_ITAH(22); BZ_ITAH(23); BZ_ITAH(24);
  513. BZ_ITAH(25); BZ_ITAH(26); BZ_ITAH(27); BZ_ITAH(28); BZ_ITAH(29);
  514. BZ_ITAH(30); BZ_ITAH(31); BZ_ITAH(32); BZ_ITAH(33); BZ_ITAH(34);
  515. BZ_ITAH(35); BZ_ITAH(36); BZ_ITAH(37); BZ_ITAH(38); BZ_ITAH(39);
  516. BZ_ITAH(40); BZ_ITAH(41); BZ_ITAH(42); BZ_ITAH(43); BZ_ITAH(44);
  517. BZ_ITAH(45); BZ_ITAH(46); BZ_ITAH(47); BZ_ITAH(48); BZ_ITAH(49);
  518. #undef BZ_ITAH
  519. gs = ge+1;
  520. } else
  521. #endif
  522. {
  523. /*--- slow version which correctly handles all situations ---*/
  524. /* code is bit bigger, but moves multiply out of the loop */
  525. uint8_t* s_len_sel_selCtr = &(s->len [s->selector[selCtr]][0]);
  526. int32_t* s_code_sel_selCtr = &(s->code[s->selector[selCtr]][0]);
  527. while (gs <= ge) {
  528. bsW(s,
  529. s_len_sel_selCtr[mtfv[gs]],
  530. s_code_sel_selCtr[mtfv[gs]]
  531. );
  532. gs++;
  533. }
  534. /* already is: gs = ge+1; */
  535. }
  536. selCtr++;
  537. }
  538. AssertH(selCtr == nSelectors, 3007);
  539. #undef code
  540. #undef rfreq
  541. #undef len_pack
  542. }
  543. /*---------------------------------------------------*/
  544. static
  545. void BZ2_compressBlock(EState* s, int is_last_block)
  546. {
  547. if (s->nblock > 0) {
  548. BZ_FINALISE_CRC(s->blockCRC);
  549. s->combinedCRC = (s->combinedCRC << 1) | (s->combinedCRC >> 31);
  550. s->combinedCRC ^= s->blockCRC;
  551. if (s->blockNo > 1)
  552. s->numZ = 0;
  553. BZ2_blockSort(s);
  554. }
  555. s->zbits = &((uint8_t*)s->arr2)[s->nblock];
  556. /*-- If this is the first block, create the stream header. --*/
  557. if (s->blockNo == 1) {
  558. BZ2_bsInitWrite(s);
  559. /*bsPutU8(s, BZ_HDR_B);*/
  560. /*bsPutU8(s, BZ_HDR_Z);*/
  561. /*bsPutU8(s, BZ_HDR_h);*/
  562. /*bsPutU8(s, BZ_HDR_0 + s->blockSize100k);*/
  563. bsPutU32(s, BZ_HDR_BZh0 + s->blockSize100k);
  564. }
  565. if (s->nblock > 0) {
  566. /*bsPutU8(s, 0x31);*/
  567. /*bsPutU8(s, 0x41);*/
  568. /*bsPutU8(s, 0x59);*/
  569. /*bsPutU8(s, 0x26);*/
  570. bsPutU32(s, 0x31415926);
  571. /*bsPutU8(s, 0x53);*/
  572. /*bsPutU8(s, 0x59);*/
  573. bsPutU16(s, 0x5359);
  574. /*-- Now the block's CRC, so it is in a known place. --*/
  575. bsPutU32(s, s->blockCRC);
  576. /*
  577. * Now a single bit indicating (non-)randomisation.
  578. * As of version 0.9.5, we use a better sorting algorithm
  579. * which makes randomisation unnecessary. So always set
  580. * the randomised bit to 'no'. Of course, the decoder
  581. * still needs to be able to handle randomised blocks
  582. * so as to maintain backwards compatibility with
  583. * older versions of bzip2.
  584. */
  585. bsW(s, 1, 0);
  586. bsW(s, 24, s->origPtr);
  587. generateMTFValues(s);
  588. sendMTFValues(s);
  589. }
  590. /*-- If this is the last block, add the stream trailer. --*/
  591. if (is_last_block) {
  592. /*bsPutU8(s, 0x17);*/
  593. /*bsPutU8(s, 0x72);*/
  594. /*bsPutU8(s, 0x45);*/
  595. /*bsPutU8(s, 0x38);*/
  596. bsPutU32(s, 0x17724538);
  597. /*bsPutU8(s, 0x50);*/
  598. /*bsPutU8(s, 0x90);*/
  599. bsPutU16(s, 0x5090);
  600. bsPutU32(s, s->combinedCRC);
  601. bsFinishWrite(s);
  602. }
  603. }
  604. /*-------------------------------------------------------------*/
  605. /*--- end compress.c ---*/
  606. /*-------------------------------------------------------------*/