compress.c 18 KB

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