Debian Codegraph @

Source/bzip2/compress.c

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