bzlib_compress.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  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. #include <compiler.h>
  57. /*---------------------------------------------------*/
  58. /*--- Bit stream I/O ---*/
  59. /*---------------------------------------------------*/
  60. /*---------------------------------------------------*/
  61. void BZ2_bsInitWrite ( EState* s )
  62. {
  63. s->bsLive = 0;
  64. s->bsBuff = 0;
  65. }
  66. /*---------------------------------------------------*/
  67. static
  68. void bsFinishWrite ( EState* s )
  69. {
  70. while (s->bsLive > 0) {
  71. s->zbits[s->numZ] = (UChar)(s->bsBuff >> 24);
  72. s->numZ++;
  73. s->bsBuff <<= 8;
  74. s->bsLive -= 8;
  75. }
  76. }
  77. /*---------------------------------------------------*/
  78. #define bsNEEDW(nz) \
  79. { \
  80. while (s->bsLive >= 8) { \
  81. s->zbits[s->numZ] \
  82. = (UChar)(s->bsBuff >> 24); \
  83. s->numZ++; \
  84. s->bsBuff <<= 8; \
  85. s->bsLive -= 8; \
  86. } \
  87. }
  88. /*---------------------------------------------------*/
  89. static
  90. __inline__
  91. void bsW ( EState* s, Int32 n, UInt32 v )
  92. {
  93. bsNEEDW ( n );
  94. s->bsBuff |= (v << (32 - s->bsLive - n));
  95. s->bsLive += n;
  96. }
  97. /*---------------------------------------------------*/
  98. static
  99. void bsPutUInt32 ( EState* s, UInt32 u )
  100. {
  101. bsW ( s, 8, (u >> 24) & 0xffL );
  102. bsW ( s, 8, (u >> 16) & 0xffL );
  103. bsW ( s, 8, (u >> 8) & 0xffL );
  104. bsW ( s, 8, u & 0xffL );
  105. }
  106. /*---------------------------------------------------*/
  107. static
  108. void bsPutUChar ( EState* s, UChar c )
  109. {
  110. bsW( s, 8, (UInt32)c );
  111. }
  112. /*---------------------------------------------------*/
  113. /*--- The back end proper ---*/
  114. /*---------------------------------------------------*/
  115. /*---------------------------------------------------*/
  116. static
  117. void makeMaps_e ( EState* s )
  118. {
  119. Int32 i;
  120. s->nInUse = 0;
  121. for (i = 0; i < 256; i++)
  122. if (s->inUse[i]) {
  123. s->unseqToSeq[i] = s->nInUse;
  124. s->nInUse++;
  125. }
  126. }
  127. /*---------------------------------------------------*/
  128. static
  129. void generateMTFValues ( EState* s )
  130. {
  131. UChar yy[256];
  132. Int32 i, j;
  133. Int32 zPend;
  134. Int32 wr;
  135. Int32 EOB;
  136. /*
  137. After sorting (eg, here),
  138. s->arr1 [ 0 .. s->nblock-1 ] holds sorted order,
  139. and
  140. ((UChar*)s->arr2) [ 0 .. s->nblock-1 ]
  141. holds the original block data.
  142. The first thing to do is generate the MTF values,
  143. and put them in
  144. ((UInt16*)s->arr1) [ 0 .. s->nblock-1 ].
  145. Because there are strictly fewer or equal MTF values
  146. than block values, ptr values in this area are overwritten
  147. with MTF values only when they are no longer needed.
  148. The final compressed bitstream is generated into the
  149. area starting at
  150. (UChar*) (&((UChar*)s->arr2)[s->nblock])
  151. These storage aliases are set up in bzCompressInit(),
  152. except for the last one, which is arranged in
  153. compressBlock().
  154. */
  155. UInt32* ptr = s->ptr;
  156. UChar* block = s->block;
  157. UInt16* mtfv = s->mtfv;
  158. makeMaps_e ( s );
  159. EOB = s->nInUse+1;
  160. for (i = 0; i <= EOB; i++) s->mtfFreq[i] = 0;
  161. wr = 0;
  162. zPend = 0;
  163. for (i = 0; i < s->nInUse; i++) yy[i] = (UChar) i;
  164. for (i = 0; i < s->nblock; i++) {
  165. UChar ll_i;
  166. AssertD ( wr <= i, "generateMTFValues(1)" );
  167. j = ptr[i]-1; if (j < 0) j += s->nblock;
  168. ll_i = s->unseqToSeq[block[j]];
  169. AssertD ( ll_i < s->nInUse, "generateMTFValues(2a)" );
  170. if (yy[0] == ll_i) {
  171. zPend++;
  172. } else {
  173. if (zPend > 0) {
  174. zPend--;
  175. while (True) {
  176. if (zPend & 1) {
  177. mtfv[wr] = BZ_RUNB; wr++;
  178. s->mtfFreq[BZ_RUNB]++;
  179. } else {
  180. mtfv[wr] = BZ_RUNA; wr++;
  181. s->mtfFreq[BZ_RUNA]++;
  182. }
  183. if (zPend < 2) break;
  184. zPend = (zPend - 2) / 2;
  185. };
  186. zPend = 0;
  187. }
  188. {
  189. register UChar rtmp;
  190. register UChar* ryy_j;
  191. register UChar rll_i;
  192. rtmp = yy[1];
  193. yy[1] = yy[0];
  194. ryy_j = &(yy[1]);
  195. rll_i = ll_i;
  196. while ( rll_i != rtmp ) {
  197. register UChar rtmp2;
  198. ryy_j++;
  199. rtmp2 = rtmp;
  200. rtmp = *ryy_j;
  201. *ryy_j = rtmp2;
  202. };
  203. yy[0] = rtmp;
  204. j = ryy_j - &(yy[0]);
  205. mtfv[wr] = j+1; wr++; s->mtfFreq[j+1]++;
  206. }
  207. }
  208. }
  209. if (zPend > 0) {
  210. zPend--;
  211. while (True) {
  212. if (zPend & 1) {
  213. mtfv[wr] = BZ_RUNB; wr++;
  214. s->mtfFreq[BZ_RUNB]++;
  215. } else {
  216. mtfv[wr] = BZ_RUNA; wr++;
  217. s->mtfFreq[BZ_RUNA]++;
  218. }
  219. if (zPend < 2) break;
  220. zPend = (zPend - 2) / 2;
  221. };
  222. zPend = 0;
  223. }
  224. mtfv[wr] = EOB; wr++; s->mtfFreq[EOB]++;
  225. s->nMTF = wr;
  226. }
  227. /*---------------------------------------------------*/
  228. #define BZ_LESSER_ICOST 0
  229. #define BZ_GREATER_ICOST 15
  230. static
  231. void sendMTFValues ( EState* s )
  232. {
  233. Int32 v, t, i, j, gs, ge, totc, bt, bc, iter;
  234. Int32 nSelectors, alphaSize, minLen, maxLen, selCtr;
  235. Int32 nGroups;
  236. Int32 nBytes __maybe_unused;
  237. /*--
  238. UChar len [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
  239. is a global since the decoder also needs it.
  240. Int32 code[BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
  241. Int32 rfreq[BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
  242. are also globals only used in this proc.
  243. Made global to keep stack frame size small.
  244. --*/
  245. UInt16 cost[BZ_N_GROUPS];
  246. Int32 fave[BZ_N_GROUPS];
  247. UInt16* mtfv = s->mtfv;
  248. if (s->verbosity >= 3)
  249. VPrintf3( " %d in block, %d after MTF & 1-2 coding, "
  250. "%d+2 syms in use\n",
  251. s->nblock, s->nMTF, s->nInUse );
  252. alphaSize = s->nInUse+2;
  253. for (t = 0; t < BZ_N_GROUPS; t++)
  254. for (v = 0; v < alphaSize; v++)
  255. s->len[t][v] = BZ_GREATER_ICOST;
  256. /*--- Decide how many coding tables to use ---*/
  257. AssertH ( s->nMTF > 0, 3001 );
  258. if (s->nMTF < 200) nGroups = 2; else
  259. if (s->nMTF < 600) nGroups = 3; else
  260. if (s->nMTF < 1200) nGroups = 4; else
  261. if (s->nMTF < 2400) nGroups = 5; else
  262. nGroups = 6;
  263. /*--- Generate an initial set of coding tables ---*/
  264. {
  265. Int32 nPart, remF, tFreq, aFreq;
  266. nPart = nGroups;
  267. remF = s->nMTF;
  268. gs = 0;
  269. while (nPart > 0) {
  270. tFreq = remF / nPart;
  271. ge = gs-1;
  272. aFreq = 0;
  273. while (aFreq < tFreq && ge < alphaSize-1) {
  274. ge++;
  275. aFreq += s->mtfFreq[ge];
  276. }
  277. if (ge > gs
  278. && nPart != nGroups && nPart != 1
  279. && ((nGroups-nPart) % 2 == 1)) {
  280. aFreq -= s->mtfFreq[ge];
  281. ge--;
  282. }
  283. if (s->verbosity >= 3)
  284. VPrintf5( " initial group %d, [%d .. %d], "
  285. "has %d syms (%4.1f%%)\n",
  286. nPart, gs, ge, aFreq,
  287. (100.0 * (float)aFreq) / (float)(s->nMTF) );
  288. for (v = 0; v < alphaSize; v++)
  289. if (v >= gs && v <= ge)
  290. s->len[nPart-1][v] = BZ_LESSER_ICOST; else
  291. s->len[nPart-1][v] = BZ_GREATER_ICOST;
  292. nPart--;
  293. gs = ge+1;
  294. remF -= aFreq;
  295. }
  296. }
  297. /*---
  298. Iterate up to BZ_N_ITERS times to improve the tables.
  299. ---*/
  300. for (iter = 0; iter < BZ_N_ITERS; iter++) {
  301. for (t = 0; t < nGroups; t++) fave[t] = 0;
  302. for (t = 0; t < nGroups; t++)
  303. for (v = 0; v < alphaSize; v++)
  304. s->rfreq[t][v] = 0;
  305. /*---
  306. Set up an auxiliary length table which is used to fast-track
  307. the common case (nGroups == 6).
  308. ---*/
  309. if (nGroups == 6) {
  310. for (v = 0; v < alphaSize; v++) {
  311. s->len_pack[v][0] = (s->len[1][v] << 16) | s->len[0][v];
  312. s->len_pack[v][1] = (s->len[3][v] << 16) | s->len[2][v];
  313. s->len_pack[v][2] = (s->len[5][v] << 16) | s->len[4][v];
  314. }
  315. }
  316. nSelectors = 0;
  317. totc = 0;
  318. gs = 0;
  319. while (True) {
  320. /*--- Set group start & end marks. --*/
  321. if (gs >= s->nMTF) break;
  322. ge = gs + BZ_G_SIZE - 1;
  323. if (ge >= s->nMTF) ge = s->nMTF-1;
  324. /*--
  325. Calculate the cost of this group as coded
  326. by each of the coding tables.
  327. --*/
  328. for (t = 0; t < nGroups; t++) cost[t] = 0;
  329. if (nGroups == 6 && 50 == ge-gs+1) {
  330. /*--- fast track the common case ---*/
  331. register UInt32 cost01, cost23, cost45;
  332. register UInt16 icv;
  333. cost01 = cost23 = cost45 = 0;
  334. # define BZ_ITER(nn) \
  335. icv = mtfv[gs+(nn)]; \
  336. cost01 += s->len_pack[icv][0]; \
  337. cost23 += s->len_pack[icv][1]; \
  338. cost45 += s->len_pack[icv][2]; \
  339. BZ_ITER(0); BZ_ITER(1); BZ_ITER(2); BZ_ITER(3); BZ_ITER(4);
  340. BZ_ITER(5); BZ_ITER(6); BZ_ITER(7); BZ_ITER(8); BZ_ITER(9);
  341. BZ_ITER(10); BZ_ITER(11); BZ_ITER(12); BZ_ITER(13); BZ_ITER(14);
  342. BZ_ITER(15); BZ_ITER(16); BZ_ITER(17); BZ_ITER(18); BZ_ITER(19);
  343. BZ_ITER(20); BZ_ITER(21); BZ_ITER(22); BZ_ITER(23); BZ_ITER(24);
  344. BZ_ITER(25); BZ_ITER(26); BZ_ITER(27); BZ_ITER(28); BZ_ITER(29);
  345. BZ_ITER(30); BZ_ITER(31); BZ_ITER(32); BZ_ITER(33); BZ_ITER(34);
  346. BZ_ITER(35); BZ_ITER(36); BZ_ITER(37); BZ_ITER(38); BZ_ITER(39);
  347. BZ_ITER(40); BZ_ITER(41); BZ_ITER(42); BZ_ITER(43); BZ_ITER(44);
  348. BZ_ITER(45); BZ_ITER(46); BZ_ITER(47); BZ_ITER(48); BZ_ITER(49);
  349. # undef BZ_ITER
  350. cost[0] = cost01 & 0xffff; cost[1] = cost01 >> 16;
  351. cost[2] = cost23 & 0xffff; cost[3] = cost23 >> 16;
  352. cost[4] = cost45 & 0xffff; cost[5] = cost45 >> 16;
  353. } else {
  354. /*--- slow version which correctly handles all situations ---*/
  355. for (i = gs; i <= ge; i++) {
  356. UInt16 icv = mtfv[i];
  357. for (t = 0; t < nGroups; t++) cost[t] += s->len[t][icv];
  358. }
  359. }
  360. /*--
  361. Find the coding table which is best for this group,
  362. and record its identity in the selector table.
  363. --*/
  364. bc = 999999999; bt = -1;
  365. for (t = 0; t < nGroups; t++)
  366. if (cost[t] < bc) { bc = cost[t]; bt = t; };
  367. totc += bc;
  368. fave[bt]++;
  369. s->selector[nSelectors] = bt;
  370. nSelectors++;
  371. /*--
  372. Increment the symbol frequencies for the selected table.
  373. --*/
  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. } else {
  389. /*--- slow version which correctly handles all situations ---*/
  390. for (i = gs; i <= ge; i++)
  391. s->rfreq[bt][ mtfv[i] ]++;
  392. }
  393. gs = ge+1;
  394. }
  395. if (s->verbosity >= 3) {
  396. VPrintf2 ( " pass %d: size is %d, grp uses are ",
  397. iter+1, totc/8 );
  398. for (t = 0; t < nGroups; t++)
  399. VPrintf1 ( "%d ", fave[t] );
  400. VPrintf0 ( "\n" );
  401. }
  402. /*--
  403. Recompute the tables based on the accumulated frequencies.
  404. --*/
  405. /* maxLen was changed from 20 to 17 in bzip2-1.0.3. See
  406. comment in huffman.c for details. */
  407. for (t = 0; t < nGroups; t++)
  408. BZ2_hbMakeCodeLengths ( &(s->len[t][0]), &(s->rfreq[t][0]),
  409. alphaSize, 17 /*20*/ );
  410. }
  411. AssertH( nGroups < 8, 3002 );
  412. AssertH( nSelectors < 32768 &&
  413. nSelectors <= (2 + (900000 / BZ_G_SIZE)),
  414. 3003 );
  415. /*--- Compute MTF values for the selectors. ---*/
  416. {
  417. UChar pos[BZ_N_GROUPS], ll_i, tmp2, tmp;
  418. for (i = 0; i < nGroups; i++) pos[i] = i;
  419. for (i = 0; i < nSelectors; i++) {
  420. ll_i = s->selector[i];
  421. j = 0;
  422. tmp = pos[j];
  423. while ( ll_i != tmp ) {
  424. j++;
  425. tmp2 = tmp;
  426. tmp = pos[j];
  427. pos[j] = tmp2;
  428. };
  429. pos[0] = tmp;
  430. s->selectorMtf[i] = j;
  431. }
  432. };
  433. /*--- Assign actual codes for the tables. --*/
  434. for (t = 0; t < nGroups; t++) {
  435. minLen = 32;
  436. maxLen = 0;
  437. for (i = 0; i < alphaSize; i++) {
  438. if (s->len[t][i] > maxLen) maxLen = s->len[t][i];
  439. if (s->len[t][i] < minLen) minLen = s->len[t][i];
  440. }
  441. AssertH ( !(maxLen > 17 /*20*/ ), 3004 );
  442. AssertH ( !(minLen < 1), 3005 );
  443. BZ2_hbAssignCodes ( &(s->code[t][0]), &(s->len[t][0]),
  444. minLen, maxLen, alphaSize );
  445. }
  446. /*--- Transmit the mapping table. ---*/
  447. {
  448. Bool inUse16[16];
  449. for (i = 0; i < 16; i++) {
  450. inUse16[i] = False;
  451. for (j = 0; j < 16; j++)
  452. if (s->inUse[i * 16 + j]) inUse16[i] = True;
  453. }
  454. nBytes = s->numZ;
  455. for (i = 0; i < 16; i++)
  456. if (inUse16[i]) bsW(s,1,1); else bsW(s,1,0);
  457. for (i = 0; i < 16; i++)
  458. if (inUse16[i])
  459. for (j = 0; j < 16; j++) {
  460. if (s->inUse[i * 16 + j]) bsW(s,1,1); else bsW(s,1,0);
  461. }
  462. if (s->verbosity >= 3)
  463. VPrintf1( " bytes: mapping %d, ", s->numZ-nBytes );
  464. }
  465. /*--- Now the selectors. ---*/
  466. nBytes = s->numZ;
  467. bsW ( s, 3, nGroups );
  468. bsW ( s, 15, nSelectors );
  469. for (i = 0; i < nSelectors; i++) {
  470. for (j = 0; j < s->selectorMtf[i]; j++) bsW(s,1,1);
  471. bsW(s,1,0);
  472. }
  473. if (s->verbosity >= 3)
  474. VPrintf1( "selectors %d, ", s->numZ-nBytes );
  475. /*--- Now the coding tables. ---*/
  476. nBytes = s->numZ;
  477. for (t = 0; t < nGroups; t++) {
  478. Int32 curr = s->len[t][0];
  479. bsW ( s, 5, curr );
  480. for (i = 0; i < alphaSize; i++) {
  481. while (curr < s->len[t][i]) { bsW(s,2,2); curr++; /* 10 */ };
  482. while (curr > s->len[t][i]) { bsW(s,2,3); curr--; /* 11 */ };
  483. bsW ( s, 1, 0 );
  484. }
  485. }
  486. if (s->verbosity >= 3)
  487. VPrintf1 ( "code lengths %d, ", s->numZ-nBytes );
  488. /*--- And finally, the block data proper ---*/
  489. nBytes = s->numZ;
  490. selCtr = 0;
  491. gs = 0;
  492. while (True) {
  493. if (gs >= s->nMTF) break;
  494. ge = gs + BZ_G_SIZE - 1;
  495. if (ge >= s->nMTF) ge = s->nMTF-1;
  496. AssertH ( s->selector[selCtr] < nGroups, 3006 );
  497. if (nGroups == 6 && 50 == ge-gs+1) {
  498. /*--- fast track the common case ---*/
  499. UInt16 mtfv_i;
  500. UChar* s_len_sel_selCtr
  501. = &(s->len[s->selector[selCtr]][0]);
  502. Int32* s_code_sel_selCtr
  503. = &(s->code[s->selector[selCtr]][0]);
  504. # define BZ_ITAH(nn) \
  505. mtfv_i = mtfv[gs+(nn)]; \
  506. bsW ( s, \
  507. s_len_sel_selCtr[mtfv_i], \
  508. s_code_sel_selCtr[mtfv_i] )
  509. BZ_ITAH(0); BZ_ITAH(1); BZ_ITAH(2); BZ_ITAH(3); BZ_ITAH(4);
  510. BZ_ITAH(5); BZ_ITAH(6); BZ_ITAH(7); BZ_ITAH(8); BZ_ITAH(9);
  511. BZ_ITAH(10); BZ_ITAH(11); BZ_ITAH(12); BZ_ITAH(13); BZ_ITAH(14);
  512. BZ_ITAH(15); BZ_ITAH(16); BZ_ITAH(17); BZ_ITAH(18); BZ_ITAH(19);
  513. BZ_ITAH(20); BZ_ITAH(21); BZ_ITAH(22); BZ_ITAH(23); BZ_ITAH(24);
  514. BZ_ITAH(25); BZ_ITAH(26); BZ_ITAH(27); BZ_ITAH(28); BZ_ITAH(29);
  515. BZ_ITAH(30); BZ_ITAH(31); BZ_ITAH(32); BZ_ITAH(33); BZ_ITAH(34);
  516. BZ_ITAH(35); BZ_ITAH(36); BZ_ITAH(37); BZ_ITAH(38); BZ_ITAH(39);
  517. BZ_ITAH(40); BZ_ITAH(41); BZ_ITAH(42); BZ_ITAH(43); BZ_ITAH(44);
  518. BZ_ITAH(45); BZ_ITAH(46); BZ_ITAH(47); BZ_ITAH(48); BZ_ITAH(49);
  519. # undef BZ_ITAH
  520. } else {
  521. /*--- slow version which correctly handles all situations ---*/
  522. for (i = gs; i <= ge; i++) {
  523. bsW ( s,
  524. s->len [s->selector[selCtr]] [mtfv[i]],
  525. s->code [s->selector[selCtr]] [mtfv[i]] );
  526. }
  527. }
  528. gs = ge+1;
  529. selCtr++;
  530. }
  531. AssertH( selCtr == nSelectors, 3007 );
  532. if (s->verbosity >= 3)
  533. VPrintf1( "codes %d\n", s->numZ-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. /*-------------------------------------------------------------*/