Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

714 lines
22 KiB

  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 of 21 March 2000
  37. This program is based on (at least) the work of:
  38. Mike Burrows
  39. David Wheeler
  40. Peter Fenwick
  41. Alistair Moffat
  42. Radford Neal
  43. Ian H. Witten
  44. Robert Sedgewick
  45. Jon L. Bentley
  46. For more information on these sources, see the manual.
  47. --*/
  48. /*--
  49. CHANGES
  50. ~~~~~~~
  51. 0.9.0 -- original version.
  52. 0.9.0a/b -- no changes in this file.
  53. 0.9.0c
  54. * changed setting of nGroups in sendMTFValues() so as to
  55. do a bit better on small files
  56. --*/
  57. #include "bzlib_private.h"
  58. /*---------------------------------------------------*/
  59. /*--- Bit stream I/O ---*/
  60. /*---------------------------------------------------*/
  61. /*---------------------------------------------------*/
  62. void BZ2_bsInitWrite ( EState* s )
  63. {
  64. s->bsLive = 0;
  65. s->bsBuff = 0;
  66. }
  67. /*---------------------------------------------------*/
  68. static
  69. void bsFinishWrite ( EState* s )
  70. {
  71. while (s->bsLive > 0) {
  72. s->zbits[s->numZ] = (UChar)(s->bsBuff >> 24);
  73. s->numZ++;
  74. s->bsBuff <<= 8;
  75. s->bsLive -= 8;
  76. }
  77. }
  78. /*---------------------------------------------------*/
  79. #define bsNEEDW(nz) \
  80. { \
  81. while (s->bsLive >= 8) { \
  82. s->zbits[s->numZ] \
  83. = (UChar)(s->bsBuff >> 24); \
  84. s->numZ++; \
  85. s->bsBuff <<= 8; \
  86. s->bsLive -= 8; \
  87. } \
  88. }
  89. /*---------------------------------------------------*/
  90. static
  91. __inline__
  92. void bsW ( EState* s, Int32 n, UInt32 v )
  93. {
  94. bsNEEDW ( n );
  95. s->bsBuff |= (v << (32 - s->bsLive - n));
  96. s->bsLive += n;
  97. }
  98. /*---------------------------------------------------*/
  99. static
  100. void bsPutUInt32 ( EState* s, UInt32 u )
  101. {
  102. bsW ( s, 8, (u >> 24) & 0xffL );
  103. bsW ( s, 8, (u >> 16) & 0xffL );
  104. bsW ( s, 8, (u >> 8) & 0xffL );
  105. bsW ( s, 8, u & 0xffL );
  106. }
  107. /*---------------------------------------------------*/
  108. static
  109. void bsPutUChar ( EState* s, UChar c )
  110. {
  111. bsW( s, 8, (UInt32)c );
  112. }
  113. /*---------------------------------------------------*/
  114. /*--- The back end proper ---*/
  115. /*---------------------------------------------------*/
  116. /*---------------------------------------------------*/
  117. static
  118. void makeMaps_e ( EState* s )
  119. {
  120. Int32 i;
  121. s->nInUse = 0;
  122. for (i = 0; i < 256; i++)
  123. if (s->inUse[i]) {
  124. s->unseqToSeq[i] = s->nInUse;
  125. s->nInUse++;
  126. }
  127. }
  128. /*---------------------------------------------------*/
  129. static
  130. void generateMTFValues ( EState* s )
  131. {
  132. UChar yy[256];
  133. Int32 i, j;
  134. Int32 zPend;
  135. Int32 wr;
  136. Int32 EOB;
  137. /*
  138. After sorting (eg, here),
  139. s->arr1 [ 0 .. s->nblock-1 ] holds sorted order,
  140. and
  141. ((UChar*)s->arr2) [ 0 .. s->nblock-1 ]
  142. holds the original block data.
  143. The first thing to do is generate the MTF values,
  144. and put them in
  145. ((UInt16*)s->arr1) [ 0 .. s->nblock-1 ].
  146. Because there are strictly fewer or equal MTF values
  147. than block values, ptr values in this area are overwritten
  148. with MTF values only when they are no longer needed.
  149. The final compressed bitstream is generated into the
  150. area starting at
  151. (UChar*) (&((UChar*)s->arr2)[s->nblock])
  152. These storage aliases are set up in bzCompressInit(),
  153. except for the last one, which is arranged in
  154. compressBlock().
  155. */
  156. UInt32* ptr = s->ptr;
  157. UChar* block = s->block;
  158. UInt16* mtfv = s->mtfv;
  159. makeMaps_e ( s );
  160. EOB = s->nInUse+1;
  161. for (i = 0; i <= EOB; i++) s->mtfFreq[i] = 0;
  162. wr = 0;
  163. zPend = 0;
  164. for (i = 0; i < s->nInUse; i++) yy[i] = (UChar) i;
  165. for (i = 0; i < s->nblock; i++) {
  166. UChar ll_i;
  167. AssertD ( wr <= i, "generateMTFValues(1)" );
  168. j = ptr[i]-1; if (j < 0) j += s->nblock;
  169. ll_i = s->unseqToSeq[block[j]];
  170. AssertD ( ll_i < s->nInUse, "generateMTFValues(2a)" );
  171. if (yy[0] == ll_i) {
  172. zPend++;
  173. } else {
  174. if (zPend > 0) {
  175. zPend--;
  176. while (True) {
  177. if (zPend & 1) {
  178. mtfv[wr] = BZ_RUNB; wr++;
  179. s->mtfFreq[BZ_RUNB]++;
  180. } else {
  181. mtfv[wr] = BZ_RUNA; wr++;
  182. s->mtfFreq[BZ_RUNA]++;
  183. }
  184. if (zPend < 2) break;
  185. zPend = (zPend - 2) / 2;
  186. };
  187. zPend = 0;
  188. }
  189. {
  190. register UChar rtmp;
  191. register UChar* ryy_j;
  192. register UChar rll_i;
  193. rtmp = yy[1];
  194. yy[1] = yy[0];
  195. ryy_j = &(yy[1]);
  196. rll_i = ll_i;
  197. while ( rll_i != rtmp ) {
  198. register UChar rtmp2;
  199. ryy_j++;
  200. rtmp2 = rtmp;
  201. rtmp = *ryy_j;
  202. *ryy_j = rtmp2;
  203. };
  204. yy[0] = rtmp;
  205. j = ryy_j - &(yy[0]);
  206. mtfv[wr] = j+1; wr++; s->mtfFreq[j+1]++;
  207. }
  208. }
  209. }
  210. if (zPend > 0) {
  211. zPend--;
  212. while (True) {
  213. if (zPend & 1) {
  214. mtfv[wr] = BZ_RUNB; wr++;
  215. s->mtfFreq[BZ_RUNB]++;
  216. } else {
  217. mtfv[wr] = BZ_RUNA; wr++;
  218. s->mtfFreq[BZ_RUNA]++;
  219. }
  220. if (zPend < 2) break;
  221. zPend = (zPend - 2) / 2;
  222. };
  223. zPend = 0;
  224. }
  225. mtfv[wr] = EOB; wr++; s->mtfFreq[EOB]++;
  226. s->nMTF = wr;
  227. }
  228. /*---------------------------------------------------*/
  229. #define BZ_LESSER_ICOST 0
  230. #define BZ_GREATER_ICOST 15
  231. static
  232. void sendMTFValues ( EState* s )
  233. {
  234. Int32 v, t, i, j, gs, ge, totc, bt, bc, iter;
  235. Int32 nSelectors=0, alphaSize, minLen, maxLen, selCtr;
  236. Int32 nGroups, nBytes;
  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. for (t = 0; t < nGroups; t++)
  406. BZ2_hbMakeCodeLengths ( &(s->len[t][0]), &(s->rfreq[t][0]),
  407. alphaSize, 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 > 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. }
  533. /*---------------------------------------------------*/
  534. void BZ2_compressBlock ( EState* s, Bool is_last_block )
  535. {
  536. if (s->nblock > 0) {
  537. BZ_FINALISE_CRC ( s->blockCRC );
  538. s->combinedCRC = (s->combinedCRC << 1) | (s->combinedCRC >> 31);
  539. s->combinedCRC ^= s->blockCRC;
  540. if (s->blockNo > 1) s->numZ = 0;
  541. if (s->verbosity >= 2)
  542. VPrintf4( " block %d: crc = 0x%8x, "
  543. "combined CRC = 0x%8x, size = %d\n",
  544. s->blockNo, s->blockCRC, s->combinedCRC, s->nblock );
  545. BZ2_blockSort ( s );
  546. }
  547. s->zbits = (UChar*) (&((UChar*)s->arr2)[s->nblock]);
  548. /*-- If this is the first block, create the stream header. --*/
  549. if (s->blockNo == 1) {
  550. BZ2_bsInitWrite ( s );
  551. bsPutUChar ( s, BZ_HDR_B );
  552. bsPutUChar ( s, BZ_HDR_Z );
  553. bsPutUChar ( s, BZ_HDR_h );
  554. bsPutUChar ( s, (UChar)(BZ_HDR_0 + s->blockSize100k) );
  555. }
  556. if (s->nblock > 0) {
  557. bsPutUChar ( s, 0x31 ); bsPutUChar ( s, 0x41 );
  558. bsPutUChar ( s, 0x59 ); bsPutUChar ( s, 0x26 );
  559. bsPutUChar ( s, 0x53 ); bsPutUChar ( s, 0x59 );
  560. /*-- Now the block's CRC, so it is in a known place. --*/
  561. bsPutUInt32 ( s, s->blockCRC );
  562. /*--
  563. Now a single bit indicating (non-)randomisation.
  564. As of version 0.9.5, we use a better sorting algorithm
  565. which makes randomisation unnecessary. So always set
  566. the randomised bit to 'no'. Of course, the decoder
  567. still needs to be able to handle randomised blocks
  568. so as to maintain backwards compatibility with
  569. older versions of bzip2.
  570. --*/
  571. bsW(s,1,0);
  572. bsW ( s, 24, s->origPtr );
  573. generateMTFValues ( s );
  574. sendMTFValues ( s );
  575. }
  576. /*-- If this is the last block, add the stream trailer. --*/
  577. if (is_last_block) {
  578. bsPutUChar ( s, 0x17 ); bsPutUChar ( s, 0x72 );
  579. bsPutUChar ( s, 0x45 ); bsPutUChar ( s, 0x38 );
  580. bsPutUChar ( s, 0x50 ); bsPutUChar ( s, 0x90 );
  581. bsPutUInt32 ( s, s->combinedCRC );
  582. if (s->verbosity >= 2)
  583. VPrintf1( " final combined CRC = 0x%x\n ", s->combinedCRC );
  584. bsFinishWrite ( s );
  585. }
  586. }
  587. /*-------------------------------------------------------------*/
  588. /*--- end compress.c ---*/
  589. /*-------------------------------------------------------------*/