bzlib_compress.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. /*-------------------------------------------------------------*/
  2. /*--- Compression machinery (not incl block sorting) ---*/
  3. /*--- compress.c ---*/
  4. /*-------------------------------------------------------------*/
  5. /*--
  6. This file is a part of bzip2 and/or libbzip2, a program and
  7. library for lossless, block-sorting data compression.
  8. Copyright (C) 1996-2002 Julian R Seward. All rights reserved.
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions
  11. are met:
  12. 1. Redistributions of source code must retain the above copyright
  13. notice, this list of conditions and the following disclaimer.
  14. 2. The origin of this software must not be misrepresented; you must
  15. not claim that you wrote the original software. If you use this
  16. software in a product, an acknowledgment in the product
  17. documentation would be appreciated but is not required.
  18. 3. Altered source versions must be plainly marked as such, and must
  19. not be misrepresented as being the original software.
  20. 4. The name of the author may not be used to endorse or promote
  21. products derived from this software without specific prior written
  22. permission.
  23. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
  24. OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  25. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  27. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  29. GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  31. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  32. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  33. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. Julian Seward, Cambridge, UK.
  35. jseward@acm.org
  36. bzip2/libbzip2 version 1.0.6 of 6 September 2010
  37. Copyright (C) 1996-2010 Julian Seward <jseward@bzip.org>
  38. This program is based on (at least) the work of:
  39. Mike Burrows
  40. David Wheeler
  41. Peter Fenwick
  42. Alistair Moffat
  43. Radford Neal
  44. Ian H. Witten
  45. Robert Sedgewick
  46. Jon L. Bentley
  47. For more information on these sources, see the manual.
  48. --*/
  49. /* CHANGES
  50. 0.9.0 -- original version.
  51. 0.9.0a/b -- no changes in this file.
  52. 0.9.0c -- changed setting of nGroups in sendMTFValues()
  53. so as to do a bit better on small files
  54. */
  55. #include "bzlib_private.h"
  56. /*---------------------------------------------------*/
  57. /*--- Bit stream I/O ---*/
  58. /*---------------------------------------------------*/
  59. /*---------------------------------------------------*/
  60. void BZ2_bsInitWrite ( EState* s )
  61. {
  62. s->bsLive = 0;
  63. s->bsBuff = 0;
  64. }
  65. /*---------------------------------------------------*/
  66. static
  67. void bsFinishWrite ( EState* s )
  68. {
  69. while (s->bsLive > 0) {
  70. s->zbits[s->numZ] = (UChar)(s->bsBuff >> 24);
  71. s->numZ++;
  72. s->bsBuff <<= 8;
  73. s->bsLive -= 8;
  74. }
  75. }
  76. /*---------------------------------------------------*/
  77. #define bsNEEDW(nz) \
  78. { \
  79. while (s->bsLive >= 8) { \
  80. s->zbits[s->numZ] \
  81. = (UChar)(s->bsBuff >> 24); \
  82. s->numZ++; \
  83. s->bsBuff <<= 8; \
  84. s->bsLive -= 8; \
  85. } \
  86. }
  87. /*---------------------------------------------------*/
  88. static
  89. __inline__
  90. void bsW ( EState* s, Int32 n, UInt32 v )
  91. {
  92. bsNEEDW ( n );
  93. s->bsBuff |= (v << (32 - s->bsLive - n));
  94. s->bsLive += n;
  95. }
  96. /*---------------------------------------------------*/
  97. static
  98. void bsPutUInt32 ( EState* s, UInt32 u )
  99. {
  100. bsW ( s, 8, (u >> 24) & 0xffL );
  101. bsW ( s, 8, (u >> 16) & 0xffL );
  102. bsW ( s, 8, (u >> 8) & 0xffL );
  103. bsW ( s, 8, u & 0xffL );
  104. }
  105. /*---------------------------------------------------*/
  106. static
  107. void bsPutUChar ( EState* s, UChar c )
  108. {
  109. bsW( s, 8, (UInt32)c );
  110. }
  111. /*---------------------------------------------------*/
  112. /*--- The back end proper ---*/
  113. /*---------------------------------------------------*/
  114. /*---------------------------------------------------*/
  115. static
  116. void makeMaps_e ( EState* s )
  117. {
  118. Int32 i;
  119. s->nInUse = 0;
  120. for (i = 0; i < 256; i++)
  121. if (s->inUse[i]) {
  122. s->unseqToSeq[i] = s->nInUse;
  123. s->nInUse++;
  124. }
  125. }
  126. /*---------------------------------------------------*/
  127. static
  128. void generateMTFValues ( EState* s )
  129. {
  130. UChar yy[256];
  131. Int32 i, j;
  132. Int32 zPend;
  133. Int32 wr;
  134. Int32 EOB;
  135. /*
  136. After sorting (eg, here),
  137. s->arr1 [ 0 .. s->nblock-1 ] holds sorted order,
  138. and
  139. ((UChar*)s->arr2) [ 0 .. s->nblock-1 ]
  140. holds the original block data.
  141. The first thing to do is generate the MTF values,
  142. and put them in
  143. ((UInt16*)s->arr1) [ 0 .. s->nblock-1 ].
  144. Because there are strictly fewer or equal MTF values
  145. than block values, ptr values in this area are overwritten
  146. with MTF values only when they are no longer needed.
  147. The final compressed bitstream is generated into the
  148. area starting at
  149. (UChar*) (&((UChar*)s->arr2)[s->nblock])
  150. These storage aliases are set up in bzCompressInit(),
  151. except for the last one, which is arranged in
  152. compressBlock().
  153. */
  154. UInt32* ptr = s->ptr;
  155. UChar* block = s->block;
  156. UInt16* mtfv = s->mtfv;
  157. makeMaps_e ( s );
  158. EOB = s->nInUse+1;
  159. for (i = 0; i <= EOB; i++) s->mtfFreq[i] = 0;
  160. wr = 0;
  161. zPend = 0;
  162. for (i = 0; i < s->nInUse; i++) yy[i] = (UChar) i;
  163. for (i = 0; i < s->nblock; i++) {
  164. UChar ll_i;
  165. AssertD ( wr <= i, "generateMTFValues(1)" );
  166. j = ptr[i]-1; if (j < 0) j += s->nblock;
  167. ll_i = s->unseqToSeq[block[j]];
  168. AssertD ( ll_i < s->nInUse, "generateMTFValues(2a)" );
  169. if (yy[0] == ll_i) {
  170. zPend++;
  171. } else {
  172. if (zPend > 0) {
  173. zPend--;
  174. while (True) {
  175. if (zPend & 1) {
  176. mtfv[wr] = BZ_RUNB; wr++;
  177. s->mtfFreq[BZ_RUNB]++;
  178. } else {
  179. mtfv[wr] = BZ_RUNA; wr++;
  180. s->mtfFreq[BZ_RUNA]++;
  181. }
  182. if (zPend < 2) break;
  183. zPend = (zPend - 2) / 2;
  184. };
  185. zPend = 0;
  186. }
  187. {
  188. register UChar rtmp;
  189. register UChar* ryy_j;
  190. register UChar rll_i;
  191. rtmp = yy[1];
  192. yy[1] = yy[0];
  193. ryy_j = &(yy[1]);
  194. rll_i = ll_i;
  195. while ( rll_i != rtmp ) {
  196. register UChar rtmp2;
  197. ryy_j++;
  198. rtmp2 = rtmp;
  199. rtmp = *ryy_j;
  200. *ryy_j = rtmp2;
  201. };
  202. yy[0] = rtmp;
  203. j = ryy_j - &(yy[0]);
  204. mtfv[wr] = j+1; wr++; s->mtfFreq[j+1]++;
  205. }
  206. }
  207. }
  208. if (zPend > 0) {
  209. zPend--;
  210. while (True) {
  211. if (zPend & 1) {
  212. mtfv[wr] = BZ_RUNB; wr++;
  213. s->mtfFreq[BZ_RUNB]++;
  214. } else {
  215. mtfv[wr] = BZ_RUNA; wr++;
  216. s->mtfFreq[BZ_RUNA]++;
  217. }
  218. if (zPend < 2) break;
  219. zPend = (zPend - 2) / 2;
  220. };
  221. zPend = 0;
  222. }
  223. mtfv[wr] = EOB; wr++; s->mtfFreq[EOB]++;
  224. s->nMTF = wr;
  225. }
  226. /*---------------------------------------------------*/
  227. #define BZ_LESSER_ICOST 0
  228. #define BZ_GREATER_ICOST 15
  229. static
  230. void sendMTFValues ( EState* s )
  231. {
  232. Int32 v, t, i, j, gs, ge, totc, bt, bc, iter;
  233. Int32 nSelectors, alphaSize, minLen, maxLen, selCtr;
  234. Int32 nGroups, nBytes;
  235. /*--
  236. UChar len [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
  237. is a global since the decoder also needs it.
  238. Int32 code[BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
  239. Int32 rfreq[BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
  240. are also globals only used in this proc.
  241. Made global to keep stack frame size small.
  242. --*/
  243. UInt16 cost[BZ_N_GROUPS];
  244. Int32 fave[BZ_N_GROUPS];
  245. UInt16* mtfv = s->mtfv;
  246. if (s->verbosity >= 3)
  247. VPrintf3( " %d in block, %d after MTF & 1-2 coding, "
  248. "%d+2 syms in use\n",
  249. s->nblock, s->nMTF, s->nInUse );
  250. alphaSize = s->nInUse+2;
  251. for (t = 0; t < BZ_N_GROUPS; t++)
  252. for (v = 0; v < alphaSize; v++)
  253. s->len[t][v] = BZ_GREATER_ICOST;
  254. /*--- Decide how many coding tables to use ---*/
  255. AssertH ( s->nMTF > 0, 3001 );
  256. if (s->nMTF < 200) nGroups = 2; else
  257. if (s->nMTF < 600) nGroups = 3; else
  258. if (s->nMTF < 1200) nGroups = 4; else
  259. if (s->nMTF < 2400) nGroups = 5; else
  260. nGroups = 6;
  261. /*--- Generate an initial set of coding tables ---*/
  262. {
  263. Int32 nPart, remF, tFreq, aFreq;
  264. nPart = nGroups;
  265. remF = s->nMTF;
  266. gs = 0;
  267. while (nPart > 0) {
  268. tFreq = remF / nPart;
  269. ge = gs-1;
  270. aFreq = 0;
  271. while (aFreq < tFreq && ge < alphaSize-1) {
  272. ge++;
  273. aFreq += s->mtfFreq[ge];
  274. }
  275. if (ge > gs
  276. && nPart != nGroups && nPart != 1
  277. && ((nGroups-nPart) % 2 == 1)) {
  278. aFreq -= s->mtfFreq[ge];
  279. ge--;
  280. }
  281. if (s->verbosity >= 3)
  282. VPrintf5( " initial group %d, [%d .. %d], "
  283. "has %d syms (%4.1f%%)\n",
  284. nPart, gs, ge, aFreq,
  285. (100.0 * (float)aFreq) / (float)(s->nMTF) );
  286. for (v = 0; v < alphaSize; v++)
  287. if (v >= gs && v <= ge)
  288. s->len[nPart-1][v] = BZ_LESSER_ICOST; else
  289. s->len[nPart-1][v] = BZ_GREATER_ICOST;
  290. nPart--;
  291. gs = ge+1;
  292. remF -= aFreq;
  293. }
  294. }
  295. /*---
  296. Iterate up to BZ_N_ITERS times to improve the tables.
  297. ---*/
  298. for (iter = 0; iter < BZ_N_ITERS; iter++) {
  299. for (t = 0; t < nGroups; t++) fave[t] = 0;
  300. for (t = 0; t < nGroups; t++)
  301. for (v = 0; v < alphaSize; v++)
  302. s->rfreq[t][v] = 0;
  303. /*---
  304. Set up an auxiliary length table which is used to fast-track
  305. the common case (nGroups == 6).
  306. ---*/
  307. if (nGroups == 6) {
  308. for (v = 0; v < alphaSize; v++) {
  309. s->len_pack[v][0] = (s->len[1][v] << 16) | s->len[0][v];
  310. s->len_pack[v][1] = (s->len[3][v] << 16) | s->len[2][v];
  311. s->len_pack[v][2] = (s->len[5][v] << 16) | s->len[4][v];
  312. }
  313. }
  314. nSelectors = 0;
  315. totc = 0;
  316. gs = 0;
  317. while (True) {
  318. /*--- Set group start & end marks. --*/
  319. if (gs >= s->nMTF) break;
  320. ge = gs + BZ_G_SIZE - 1;
  321. if (ge >= s->nMTF) ge = s->nMTF-1;
  322. /*--
  323. Calculate the cost of this group as coded
  324. by each of the coding tables.
  325. --*/
  326. for (t = 0; t < nGroups; t++) cost[t] = 0;
  327. if (nGroups == 6 && 50 == ge-gs+1) {
  328. /*--- fast track the common case ---*/
  329. register UInt32 cost01, cost23, cost45;
  330. register UInt16 icv;
  331. cost01 = cost23 = cost45 = 0;
  332. # define BZ_ITER(nn) \
  333. icv = mtfv[gs+(nn)]; \
  334. cost01 += s->len_pack[icv][0]; \
  335. cost23 += s->len_pack[icv][1]; \
  336. cost45 += s->len_pack[icv][2]; \
  337. BZ_ITER(0); BZ_ITER(1); BZ_ITER(2); BZ_ITER(3); BZ_ITER(4);
  338. BZ_ITER(5); BZ_ITER(6); BZ_ITER(7); BZ_ITER(8); BZ_ITER(9);
  339. BZ_ITER(10); BZ_ITER(11); BZ_ITER(12); BZ_ITER(13); BZ_ITER(14);
  340. BZ_ITER(15); BZ_ITER(16); BZ_ITER(17); BZ_ITER(18); BZ_ITER(19);
  341. BZ_ITER(20); BZ_ITER(21); BZ_ITER(22); BZ_ITER(23); BZ_ITER(24);
  342. BZ_ITER(25); BZ_ITER(26); BZ_ITER(27); BZ_ITER(28); BZ_ITER(29);
  343. BZ_ITER(30); BZ_ITER(31); BZ_ITER(32); BZ_ITER(33); BZ_ITER(34);
  344. BZ_ITER(35); BZ_ITER(36); BZ_ITER(37); BZ_ITER(38); BZ_ITER(39);
  345. BZ_ITER(40); BZ_ITER(41); BZ_ITER(42); BZ_ITER(43); BZ_ITER(44);
  346. BZ_ITER(45); BZ_ITER(46); BZ_ITER(47); BZ_ITER(48); BZ_ITER(49);
  347. # undef BZ_ITER
  348. cost[0] = cost01 & 0xffff; cost[1] = cost01 >> 16;
  349. cost[2] = cost23 & 0xffff; cost[3] = cost23 >> 16;
  350. cost[4] = cost45 & 0xffff; cost[5] = cost45 >> 16;
  351. } else {
  352. /*--- slow version which correctly handles all situations ---*/
  353. for (i = gs; i <= ge; i++) {
  354. UInt16 icv = mtfv[i];
  355. for (t = 0; t < nGroups; t++) cost[t] += s->len[t][icv];
  356. }
  357. }
  358. /*--
  359. Find the coding table which is best for this group,
  360. and record its identity in the selector table.
  361. --*/
  362. bc = 999999999; bt = -1;
  363. for (t = 0; t < nGroups; t++)
  364. if (cost[t] < bc) { bc = cost[t]; bt = t; };
  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. if (nGroups == 6 && 50 == ge-gs+1) {
  373. /*--- fast track the common case ---*/
  374. # define BZ_ITUR(nn) s->rfreq[bt][ mtfv[gs+(nn)] ]++
  375. BZ_ITUR(0); BZ_ITUR(1); BZ_ITUR(2); BZ_ITUR(3); BZ_ITUR(4);
  376. BZ_ITUR(5); BZ_ITUR(6); BZ_ITUR(7); BZ_ITUR(8); BZ_ITUR(9);
  377. BZ_ITUR(10); BZ_ITUR(11); BZ_ITUR(12); BZ_ITUR(13); BZ_ITUR(14);
  378. BZ_ITUR(15); BZ_ITUR(16); BZ_ITUR(17); BZ_ITUR(18); BZ_ITUR(19);
  379. BZ_ITUR(20); BZ_ITUR(21); BZ_ITUR(22); BZ_ITUR(23); BZ_ITUR(24);
  380. BZ_ITUR(25); BZ_ITUR(26); BZ_ITUR(27); BZ_ITUR(28); BZ_ITUR(29);
  381. BZ_ITUR(30); BZ_ITUR(31); BZ_ITUR(32); BZ_ITUR(33); BZ_ITUR(34);
  382. BZ_ITUR(35); BZ_ITUR(36); BZ_ITUR(37); BZ_ITUR(38); BZ_ITUR(39);
  383. BZ_ITUR(40); BZ_ITUR(41); BZ_ITUR(42); BZ_ITUR(43); BZ_ITUR(44);
  384. BZ_ITUR(45); BZ_ITUR(46); BZ_ITUR(47); BZ_ITUR(48); BZ_ITUR(49);
  385. # undef BZ_ITUR
  386. } else {
  387. /*--- slow version which correctly handles all situations ---*/
  388. for (i = gs; i <= ge; i++)
  389. s->rfreq[bt][ mtfv[i] ]++;
  390. }
  391. gs = ge+1;
  392. }
  393. if (s->verbosity >= 3) {
  394. VPrintf2 ( " pass %d: size is %d, grp uses are ",
  395. iter+1, totc/8 );
  396. for (t = 0; t < nGroups; t++)
  397. VPrintf1 ( "%d ", fave[t] );
  398. VPrintf0 ( "\n" );
  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->len[t][0]), &(s->rfreq[t][0]),
  407. alphaSize, 17 /*20*/ );
  408. }
  409. AssertH( nGroups < 8, 3002 );
  410. AssertH( nSelectors < 32768 &&
  411. nSelectors <= (2 + (900000 / BZ_G_SIZE)),
  412. 3003 );
  413. /*--- Compute MTF values for the selectors. ---*/
  414. {
  415. UChar pos[BZ_N_GROUPS], ll_i, tmp2, tmp;
  416. for (i = 0; i < nGroups; i++) pos[i] = i;
  417. for (i = 0; i < nSelectors; i++) {
  418. ll_i = s->selector[i];
  419. j = 0;
  420. tmp = pos[j];
  421. while ( ll_i != tmp ) {
  422. j++;
  423. tmp2 = tmp;
  424. tmp = pos[j];
  425. pos[j] = tmp2;
  426. };
  427. pos[0] = tmp;
  428. s->selectorMtf[i] = j;
  429. }
  430. };
  431. /*--- Assign actual codes for the tables. --*/
  432. for (t = 0; t < nGroups; t++) {
  433. minLen = 32;
  434. maxLen = 0;
  435. for (i = 0; i < alphaSize; i++) {
  436. if (s->len[t][i] > maxLen) maxLen = s->len[t][i];
  437. if (s->len[t][i] < minLen) minLen = s->len[t][i];
  438. }
  439. AssertH ( !(maxLen > 17 /*20*/ ), 3004 );
  440. AssertH ( !(minLen < 1), 3005 );
  441. BZ2_hbAssignCodes ( &(s->code[t][0]), &(s->len[t][0]),
  442. minLen, maxLen, alphaSize );
  443. }
  444. /*--- Transmit the mapping table. ---*/
  445. {
  446. Bool inUse16[16];
  447. for (i = 0; i < 16; i++) {
  448. inUse16[i] = False;
  449. for (j = 0; j < 16; j++)
  450. if (s->inUse[i * 16 + j]) inUse16[i] = True;
  451. }
  452. nBytes = s->numZ;
  453. for (i = 0; i < 16; i++)
  454. if (inUse16[i]) bsW(s,1,1); else bsW(s,1,0);
  455. for (i = 0; i < 16; i++)
  456. if (inUse16[i])
  457. for (j = 0; j < 16; j++) {
  458. if (s->inUse[i * 16 + j]) bsW(s,1,1); else bsW(s,1,0);
  459. }
  460. if (s->verbosity >= 3)
  461. VPrintf1( " bytes: mapping %d, ", s->numZ-nBytes );
  462. }
  463. /*--- Now the selectors. ---*/
  464. nBytes = s->numZ;
  465. bsW ( s, 3, nGroups );
  466. bsW ( s, 15, nSelectors );
  467. for (i = 0; i < nSelectors; i++) {
  468. for (j = 0; j < s->selectorMtf[i]; j++) bsW(s,1,1);
  469. bsW(s,1,0);
  470. }
  471. if (s->verbosity >= 3)
  472. VPrintf1( "selectors %d, ", s->numZ-nBytes );
  473. /*--- Now the coding tables. ---*/
  474. nBytes = s->numZ;
  475. for (t = 0; t < nGroups; t++) {
  476. Int32 curr = s->len[t][0];
  477. bsW ( s, 5, curr );
  478. for (i = 0; i < alphaSize; i++) {
  479. while (curr < s->len[t][i]) { bsW(s,2,2); curr++; /* 10 */ };
  480. while (curr > s->len[t][i]) { bsW(s,2,3); curr--; /* 11 */ };
  481. bsW ( s, 1, 0 );
  482. }
  483. }
  484. if (s->verbosity >= 3)
  485. VPrintf1 ( "code lengths %d, ", s->numZ-nBytes );
  486. /*--- And finally, the block data proper ---*/
  487. nBytes = s->numZ;
  488. selCtr = 0;
  489. gs = 0;
  490. while (True) {
  491. if (gs >= s->nMTF) break;
  492. ge = gs + BZ_G_SIZE - 1;
  493. if (ge >= s->nMTF) ge = s->nMTF-1;
  494. AssertH ( s->selector[selCtr] < nGroups, 3006 );
  495. if (nGroups == 6 && 50 == ge-gs+1) {
  496. /*--- fast track the common case ---*/
  497. UInt16 mtfv_i;
  498. UChar* s_len_sel_selCtr
  499. = &(s->len[s->selector[selCtr]][0]);
  500. Int32* s_code_sel_selCtr
  501. = &(s->code[s->selector[selCtr]][0]);
  502. # define BZ_ITAH(nn) \
  503. mtfv_i = mtfv[gs+(nn)]; \
  504. bsW ( s, \
  505. s_len_sel_selCtr[mtfv_i], \
  506. s_code_sel_selCtr[mtfv_i] )
  507. BZ_ITAH(0); BZ_ITAH(1); BZ_ITAH(2); BZ_ITAH(3); BZ_ITAH(4);
  508. BZ_ITAH(5); BZ_ITAH(6); BZ_ITAH(7); BZ_ITAH(8); BZ_ITAH(9);
  509. BZ_ITAH(10); BZ_ITAH(11); BZ_ITAH(12); BZ_ITAH(13); BZ_ITAH(14);
  510. BZ_ITAH(15); BZ_ITAH(16); BZ_ITAH(17); BZ_ITAH(18); BZ_ITAH(19);
  511. BZ_ITAH(20); BZ_ITAH(21); BZ_ITAH(22); BZ_ITAH(23); BZ_ITAH(24);
  512. BZ_ITAH(25); BZ_ITAH(26); BZ_ITAH(27); BZ_ITAH(28); BZ_ITAH(29);
  513. BZ_ITAH(30); BZ_ITAH(31); BZ_ITAH(32); BZ_ITAH(33); BZ_ITAH(34);
  514. BZ_ITAH(35); BZ_ITAH(36); BZ_ITAH(37); BZ_ITAH(38); BZ_ITAH(39);
  515. BZ_ITAH(40); BZ_ITAH(41); BZ_ITAH(42); BZ_ITAH(43); BZ_ITAH(44);
  516. BZ_ITAH(45); BZ_ITAH(46); BZ_ITAH(47); BZ_ITAH(48); BZ_ITAH(49);
  517. # undef BZ_ITAH
  518. } else {
  519. /*--- slow version which correctly handles all situations ---*/
  520. for (i = gs; i <= ge; i++) {
  521. bsW ( s,
  522. s->len [s->selector[selCtr]] [mtfv[i]],
  523. s->code [s->selector[selCtr]] [mtfv[i]] );
  524. }
  525. }
  526. gs = ge+1;
  527. selCtr++;
  528. }
  529. AssertH( selCtr == nSelectors, 3007 );
  530. if (s->verbosity >= 3)
  531. VPrintf1( "codes %d\n", s->numZ-nBytes );
  532. else /* squash compiler 'used but not set' warning */
  533. nBytes = nBytes;
  534. }
  535. /*---------------------------------------------------*/
  536. void BZ2_compressBlock ( EState* s, Bool is_last_block )
  537. {
  538. if (s->nblock > 0) {
  539. BZ_FINALISE_CRC ( s->blockCRC );
  540. s->combinedCRC = (s->combinedCRC << 1) | (s->combinedCRC >> 31);
  541. s->combinedCRC ^= s->blockCRC;
  542. if (s->blockNo > 1) s->numZ = 0;
  543. if (s->verbosity >= 2)
  544. VPrintf4( " block %d: crc = 0x%08x, "
  545. "combined CRC = 0x%08x, size = %d\n",
  546. s->blockNo, s->blockCRC, s->combinedCRC, s->nblock );
  547. BZ2_blockSort ( s );
  548. }
  549. s->zbits = (UChar*) (&((UChar*)s->arr2)[s->nblock]);
  550. /*-- If this is the first block, create the stream header. --*/
  551. if (s->blockNo == 1) {
  552. BZ2_bsInitWrite ( s );
  553. bsPutUChar ( s, BZ_HDR_B );
  554. bsPutUChar ( s, BZ_HDR_Z );
  555. bsPutUChar ( s, BZ_HDR_h );
  556. bsPutUChar ( s, (UChar)(BZ_HDR_0 + s->blockSize100k) );
  557. }
  558. if (s->nblock > 0) {
  559. bsPutUChar ( s, 0x31 ); bsPutUChar ( s, 0x41 );
  560. bsPutUChar ( s, 0x59 ); bsPutUChar ( s, 0x26 );
  561. bsPutUChar ( s, 0x53 ); bsPutUChar ( s, 0x59 );
  562. /*-- Now the block's CRC, so it is in a known place. --*/
  563. bsPutUInt32 ( s, s->blockCRC );
  564. /*--
  565. Now a single bit indicating (non-)randomisation.
  566. As of version 0.9.5, we use a better sorting algorithm
  567. which makes randomisation unnecessary. So always set
  568. the randomised bit to 'no'. Of course, the decoder
  569. still needs to be able to handle randomised blocks
  570. so as to maintain backwards compatibility with
  571. older versions of bzip2.
  572. --*/
  573. bsW(s,1,0);
  574. bsW ( s, 24, s->origPtr );
  575. generateMTFValues ( s );
  576. sendMTFValues ( s );
  577. }
  578. /*-- If this is the last block, add the stream trailer. --*/
  579. if (is_last_block) {
  580. bsPutUChar ( s, 0x17 ); bsPutUChar ( s, 0x72 );
  581. bsPutUChar ( s, 0x45 ); bsPutUChar ( s, 0x38 );
  582. bsPutUChar ( s, 0x50 ); bsPutUChar ( s, 0x90 );
  583. bsPutUInt32 ( s, s->combinedCRC );
  584. if (s->verbosity >= 2)
  585. VPrintf1( " final combined CRC = 0x%08x\n ", s->combinedCRC );
  586. bsFinishWrite ( s );
  587. }
  588. }
  589. /*-------------------------------------------------------------*/
  590. /*--- end compress.c ---*/
  591. /*-------------------------------------------------------------*/