1 From: Markus Lehtonen <markus.lehtonen@linux.intel.com>
2 Date: Thu, 16 Aug 2012 14:11:33 +0300
3 Subject: openSUSE HACK: add upstream bzip2 (v1.0.6) sources
5 Only take the selected files that are needed. Needed for being able to
6 correctly re-create bz2 archives on openSUSE.
8 Gbp-Rpm-If: 0%{?suse_version}
10 Signed-off-by: Markus Lehtonen <markus.lehtonen@linux.intel.com>
12 pit/upstream-bzip2/LICENSE | 42 +
13 pit/upstream-bzip2/Makefile | 217 ++++
14 pit/upstream-bzip2/blocksort.c | 1094 +++++++++++++++++++
15 pit/upstream-bzip2/bzip2.c | 2034 ++++++++++++++++++++++++++++++++++++
16 pit/upstream-bzip2/bzlib.c | 1572 ++++++++++++++++++++++++++++
17 pit/upstream-bzip2/bzlib.h | 282 +++++
18 pit/upstream-bzip2/bzlib_private.h | 509 +++++++++
19 pit/upstream-bzip2/compress.c | 672 ++++++++++++
20 pit/upstream-bzip2/crctable.c | 104 ++
21 pit/upstream-bzip2/decompress.c | 646 ++++++++++++
22 pit/upstream-bzip2/huffman.c | 205 ++++
23 pit/upstream-bzip2/randtable.c | 84 ++
24 12 files changed, 7461 insertions(+)
25 create mode 100644 pit/upstream-bzip2/LICENSE
26 create mode 100644 pit/upstream-bzip2/Makefile
27 create mode 100644 pit/upstream-bzip2/blocksort.c
28 create mode 100644 pit/upstream-bzip2/bzip2.c
29 create mode 100644 pit/upstream-bzip2/bzlib.c
30 create mode 100644 pit/upstream-bzip2/bzlib.h
31 create mode 100644 pit/upstream-bzip2/bzlib_private.h
32 create mode 100644 pit/upstream-bzip2/compress.c
33 create mode 100644 pit/upstream-bzip2/crctable.c
34 create mode 100644 pit/upstream-bzip2/decompress.c
35 create mode 100644 pit/upstream-bzip2/huffman.c
36 create mode 100644 pit/upstream-bzip2/randtable.c
38 diff --git a/pit/upstream-bzip2/LICENSE b/pit/upstream-bzip2/LICENSE
40 index 0000000..cc61417
42 +++ b/pit/upstream-bzip2/LICENSE
45 +--------------------------------------------------------------------------
47 +This program, "bzip2", the associated library "libbzip2", and all
48 +documentation, are copyright (C) 1996-2010 Julian R Seward. All
51 +Redistribution and use in source and binary forms, with or without
52 +modification, are permitted provided that the following conditions
55 +1. Redistributions of source code must retain the above copyright
56 + notice, this list of conditions and the following disclaimer.
58 +2. The origin of this software must not be misrepresented; you must
59 + not claim that you wrote the original software. If you use this
60 + software in a product, an acknowledgment in the product
61 + documentation would be appreciated but is not required.
63 +3. Altered source versions must be plainly marked as such, and must
64 + not be misrepresented as being the original software.
66 +4. The name of the author may not be used to endorse or promote
67 + products derived from this software without specific prior written
70 +THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
71 +OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
72 +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
73 +ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
74 +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
75 +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
76 +GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
77 +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
78 +WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
79 +NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
80 +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
82 +Julian Seward, jseward@bzip.org
83 +bzip2/libbzip2 version 1.0.6 of 6 September 2010
85 +--------------------------------------------------------------------------
86 diff --git a/pit/upstream-bzip2/Makefile b/pit/upstream-bzip2/Makefile
88 index 0000000..9754ddf
90 +++ b/pit/upstream-bzip2/Makefile
92 +# ------------------------------------------------------------------
93 +# This file is part of bzip2/libbzip2, a program and library for
94 +# lossless, block-sorting data compression.
96 +# bzip2/libbzip2 version 1.0.6 of 6 September 2010
97 +# Copyright (C) 1996-2010 Julian Seward <jseward@bzip.org>
99 +# Please read the WARNING, DISCLAIMER and PATENTS sections in the
102 +# This program is released under the terms of the license contained
103 +# in the file LICENSE.
104 +# ------------------------------------------------------------------
108 +# To assist in cross-compiling
114 +BIGFILES=-D_FILE_OFFSET_BITS=64
115 +CFLAGS=-Wall -Winline -O2 -g $(BIGFILES)
117 +# Where you want it installed when you do 'make install'
129 +all: libbz2.a bzip2 bzip2recover test
131 +bzip2: libbz2.a bzip2.o
132 + $(CC) $(CFLAGS) $(LDFLAGS) -o bzip2 bzip2.o -L. -lbz2
134 +bzip2recover: bzip2recover.o
135 + $(CC) $(CFLAGS) $(LDFLAGS) -o bzip2recover bzip2recover.o
139 + $(AR) cq libbz2.a $(OBJS)
140 + @if ( test -f $(RANLIB) -o -f /usr/bin/ranlib -o \
141 + -f /bin/ranlib -o -f /usr/ccs/bin/ranlib ) ; then \
142 + echo $(RANLIB) libbz2.a ; \
143 + $(RANLIB) libbz2.a ; \
149 + ./bzip2 -1 < sample1.ref > sample1.rb2
150 + ./bzip2 -2 < sample2.ref > sample2.rb2
151 + ./bzip2 -3 < sample3.ref > sample3.rb2
152 + ./bzip2 -d < sample1.bz2 > sample1.tst
153 + ./bzip2 -d < sample2.bz2 > sample2.tst
154 + ./bzip2 -ds < sample3.bz2 > sample3.tst
155 + cmp sample1.bz2 sample1.rb2
156 + cmp sample2.bz2 sample2.rb2
157 + cmp sample3.bz2 sample3.rb2
158 + cmp sample1.tst sample1.ref
159 + cmp sample2.tst sample2.ref
160 + cmp sample3.tst sample3.ref
163 +install: bzip2 bzip2recover
164 + if ( test ! -d $(PREFIX)/bin ) ; then mkdir -p $(PREFIX)/bin ; fi
165 + if ( test ! -d $(PREFIX)/lib ) ; then mkdir -p $(PREFIX)/lib ; fi
166 + if ( test ! -d $(PREFIX)/man ) ; then mkdir -p $(PREFIX)/man ; fi
167 + if ( test ! -d $(PREFIX)/man/man1 ) ; then mkdir -p $(PREFIX)/man/man1 ; fi
168 + if ( test ! -d $(PREFIX)/include ) ; then mkdir -p $(PREFIX)/include ; fi
169 + cp -f bzip2 $(PREFIX)/bin/bzip2
170 + cp -f bzip2 $(PREFIX)/bin/bunzip2
171 + cp -f bzip2 $(PREFIX)/bin/bzcat
172 + cp -f bzip2recover $(PREFIX)/bin/bzip2recover
173 + chmod a+x $(PREFIX)/bin/bzip2
174 + chmod a+x $(PREFIX)/bin/bunzip2
175 + chmod a+x $(PREFIX)/bin/bzcat
176 + chmod a+x $(PREFIX)/bin/bzip2recover
177 + cp -f bzip2.1 $(PREFIX)/man/man1
178 + chmod a+r $(PREFIX)/man/man1/bzip2.1
179 + cp -f bzlib.h $(PREFIX)/include
180 + chmod a+r $(PREFIX)/include/bzlib.h
181 + cp -f libbz2.a $(PREFIX)/lib
182 + chmod a+r $(PREFIX)/lib/libbz2.a
183 + cp -f bzgrep $(PREFIX)/bin/bzgrep
184 + ln -s -f $(PREFIX)/bin/bzgrep $(PREFIX)/bin/bzegrep
185 + ln -s -f $(PREFIX)/bin/bzgrep $(PREFIX)/bin/bzfgrep
186 + chmod a+x $(PREFIX)/bin/bzgrep
187 + cp -f bzmore $(PREFIX)/bin/bzmore
188 + ln -s -f $(PREFIX)/bin/bzmore $(PREFIX)/bin/bzless
189 + chmod a+x $(PREFIX)/bin/bzmore
190 + cp -f bzdiff $(PREFIX)/bin/bzdiff
191 + ln -s -f $(PREFIX)/bin/bzdiff $(PREFIX)/bin/bzcmp
192 + chmod a+x $(PREFIX)/bin/bzdiff
193 + cp -f bzgrep.1 bzmore.1 bzdiff.1 $(PREFIX)/man/man1
194 + chmod a+r $(PREFIX)/man/man1/bzgrep.1
195 + chmod a+r $(PREFIX)/man/man1/bzmore.1
196 + chmod a+r $(PREFIX)/man/man1/bzdiff.1
197 + echo ".so man1/bzgrep.1" > $(PREFIX)/man/man1/bzegrep.1
198 + echo ".so man1/bzgrep.1" > $(PREFIX)/man/man1/bzfgrep.1
199 + echo ".so man1/bzmore.1" > $(PREFIX)/man/man1/bzless.1
200 + echo ".so man1/bzdiff.1" > $(PREFIX)/man/man1/bzcmp.1
203 + rm -f *.o libbz2.a bzip2 bzip2recover \
204 + sample1.rb2 sample2.rb2 sample3.rb2 \
205 + sample1.tst sample2.tst sample3.tst
207 +blocksort.o: blocksort.c
209 + $(CC) $(CFLAGS) -c blocksort.c
210 +huffman.o: huffman.c
211 + $(CC) $(CFLAGS) -c huffman.c
212 +crctable.o: crctable.c
213 + $(CC) $(CFLAGS) -c crctable.c
214 +randtable.o: randtable.c
215 + $(CC) $(CFLAGS) -c randtable.c
216 +compress.o: compress.c
217 + $(CC) $(CFLAGS) -c compress.c
218 +decompress.o: decompress.c
219 + $(CC) $(CFLAGS) -c decompress.c
221 + $(CC) $(CFLAGS) -c bzlib.c
223 + $(CC) $(CFLAGS) -c bzip2.c
224 +bzip2recover.o: bzip2recover.c
225 + $(CC) $(CFLAGS) -c bzip2recover.c
229 + rm -f manual.ps manual.html manual.pdf
231 +DISTNAME=bzip2-1.0.6
234 + ln -s -f . $(DISTNAME)
235 + tar cvf $(DISTNAME).tar \
236 + $(DISTNAME)/blocksort.c \
237 + $(DISTNAME)/huffman.c \
238 + $(DISTNAME)/crctable.c \
239 + $(DISTNAME)/randtable.c \
240 + $(DISTNAME)/compress.c \
241 + $(DISTNAME)/decompress.c \
242 + $(DISTNAME)/bzlib.c \
243 + $(DISTNAME)/bzip2.c \
244 + $(DISTNAME)/bzip2recover.c \
245 + $(DISTNAME)/bzlib.h \
246 + $(DISTNAME)/bzlib_private.h \
247 + $(DISTNAME)/Makefile \
248 + $(DISTNAME)/LICENSE \
249 + $(DISTNAME)/bzip2.1 \
250 + $(DISTNAME)/bzip2.1.preformatted \
251 + $(DISTNAME)/bzip2.txt \
252 + $(DISTNAME)/words0 \
253 + $(DISTNAME)/words1 \
254 + $(DISTNAME)/words2 \
255 + $(DISTNAME)/words3 \
256 + $(DISTNAME)/sample1.ref \
257 + $(DISTNAME)/sample2.ref \
258 + $(DISTNAME)/sample3.ref \
259 + $(DISTNAME)/sample1.bz2 \
260 + $(DISTNAME)/sample2.bz2 \
261 + $(DISTNAME)/sample3.bz2 \
262 + $(DISTNAME)/dlltest.c \
263 + $(DISTNAME)/manual.html \
264 + $(DISTNAME)/manual.pdf \
265 + $(DISTNAME)/manual.ps \
266 + $(DISTNAME)/README \
267 + $(DISTNAME)/README.COMPILATION.PROBLEMS \
268 + $(DISTNAME)/README.XML.STUFF \
269 + $(DISTNAME)/CHANGES \
270 + $(DISTNAME)/libbz2.def \
271 + $(DISTNAME)/libbz2.dsp \
272 + $(DISTNAME)/dlltest.dsp \
273 + $(DISTNAME)/makefile.msc \
274 + $(DISTNAME)/unzcrash.c \
275 + $(DISTNAME)/spewG.c \
276 + $(DISTNAME)/mk251.c \
277 + $(DISTNAME)/bzdiff \
278 + $(DISTNAME)/bzdiff.1 \
279 + $(DISTNAME)/bzmore \
280 + $(DISTNAME)/bzmore.1 \
281 + $(DISTNAME)/bzgrep \
282 + $(DISTNAME)/bzgrep.1 \
283 + $(DISTNAME)/Makefile-libbz2_so \
284 + $(DISTNAME)/bz-common.xsl \
285 + $(DISTNAME)/bz-fo.xsl \
286 + $(DISTNAME)/bz-html.xsl \
287 + $(DISTNAME)/bzip.css \
288 + $(DISTNAME)/entities.xml \
289 + $(DISTNAME)/manual.xml \
290 + $(DISTNAME)/format.pl \
291 + $(DISTNAME)/xmlproc.sh
292 + gzip -v $(DISTNAME).tar
294 +# For rebuilding the manual from sources on my SuSE 9.1 box
296 +MANUAL_SRCS= bz-common.xsl bz-fo.xsl bz-html.xsl bzip.css \
297 + entities.xml manual.xml
299 +manual: manual.html manual.ps manual.pdf
301 +manual.ps: $(MANUAL_SRCS)
302 + ./xmlproc.sh -ps manual.xml
304 +manual.pdf: $(MANUAL_SRCS)
305 + ./xmlproc.sh -pdf manual.xml
307 +manual.html: $(MANUAL_SRCS)
308 + ./xmlproc.sh -html manual.xml
309 diff --git a/pit/upstream-bzip2/blocksort.c b/pit/upstream-bzip2/blocksort.c
311 index 0000000..d0d662c
313 +++ b/pit/upstream-bzip2/blocksort.c
316 +/*-------------------------------------------------------------*/
317 +/*--- Block sorting machinery ---*/
318 +/*--- blocksort.c ---*/
319 +/*-------------------------------------------------------------*/
321 +/* ------------------------------------------------------------------
322 + This file is part of bzip2/libbzip2, a program and library for
323 + lossless, block-sorting data compression.
325 + bzip2/libbzip2 version 1.0.6 of 6 September 2010
326 + Copyright (C) 1996-2010 Julian Seward <jseward@bzip.org>
328 + Please read the WARNING, DISCLAIMER and PATENTS sections in the
331 + This program is released under the terms of the license contained
332 + in the file LICENSE.
333 + ------------------------------------------------------------------ */
336 +#include "bzlib_private.h"
338 +/*---------------------------------------------*/
339 +/*--- Fallback O(N log(N)^2) sorting ---*/
340 +/*--- algorithm, for repetitive blocks ---*/
341 +/*---------------------------------------------*/
343 +/*---------------------------------------------*/
346 +void fallbackSimpleSort ( UInt32* fmap,
354 + if (lo == hi) return;
357 + for ( i = hi-4; i >= lo; i-- ) {
359 + ec_tmp = eclass[tmp];
360 + for ( j = i+4; j <= hi && ec_tmp > eclass[fmap[j]]; j += 4 )
361 + fmap[j-4] = fmap[j];
366 + for ( i = hi-1; i >= lo; i-- ) {
368 + ec_tmp = eclass[tmp];
369 + for ( j = i+1; j <= hi && ec_tmp > eclass[fmap[j]]; j++ )
370 + fmap[j-1] = fmap[j];
376 +/*---------------------------------------------*/
377 +#define fswap(zz1, zz2) \
378 + { Int32 zztmp = zz1; zz1 = zz2; zz2 = zztmp; }
380 +#define fvswap(zzp1, zzp2, zzn) \
382 + Int32 yyp1 = (zzp1); \
383 + Int32 yyp2 = (zzp2); \
384 + Int32 yyn = (zzn); \
385 + while (yyn > 0) { \
386 + fswap(fmap[yyp1], fmap[yyp2]); \
387 + yyp1++; yyp2++; yyn--; \
392 +#define fmin(a,b) ((a) < (b)) ? (a) : (b)
394 +#define fpush(lz,hz) { stackLo[sp] = lz; \
395 + stackHi[sp] = hz; \
398 +#define fpop(lz,hz) { sp--; \
399 + lz = stackLo[sp]; \
400 + hz = stackHi[sp]; }
402 +#define FALLBACK_QSORT_SMALL_THRESH 10
403 +#define FALLBACK_QSORT_STACK_SIZE 100
407 +void fallbackQSort3 ( UInt32* fmap,
412 + Int32 unLo, unHi, ltLo, gtHi, n, m;
415 + Int32 stackLo[FALLBACK_QSORT_STACK_SIZE];
416 + Int32 stackHi[FALLBACK_QSORT_STACK_SIZE];
421 + fpush ( loSt, hiSt );
425 + AssertH ( sp < FALLBACK_QSORT_STACK_SIZE - 1, 1004 );
428 + if (hi - lo < FALLBACK_QSORT_SMALL_THRESH) {
429 + fallbackSimpleSort ( fmap, eclass, lo, hi );
433 + /* Random partitioning. Median of 3 sometimes fails to
434 + avoid bad cases. Median of 9 seems to help but
435 + looks rather expensive. This too seems to work but
436 + is cheaper. Guidance for the magic constants
437 + 7621 and 32768 is taken from Sedgewick's algorithms
440 + r = ((r * 7621) + 1) % 32768;
442 + if (r3 == 0) med = eclass[fmap[lo]]; else
443 + if (r3 == 1) med = eclass[fmap[(lo+hi)>>1]]; else
444 + med = eclass[fmap[hi]];
451 + if (unLo > unHi) break;
452 + n = (Int32)eclass[fmap[unLo]] - (Int32)med;
454 + fswap(fmap[unLo], fmap[ltLo]);
462 + if (unLo > unHi) break;
463 + n = (Int32)eclass[fmap[unHi]] - (Int32)med;
465 + fswap(fmap[unHi], fmap[gtHi]);
472 + if (unLo > unHi) break;
473 + fswap(fmap[unLo], fmap[unHi]); unLo++; unHi--;
476 + AssertD ( unHi == unLo-1, "fallbackQSort3(2)" );
478 + if (gtHi < ltLo) continue;
480 + n = fmin(ltLo-lo, unLo-ltLo); fvswap(lo, unLo-n, n);
481 + m = fmin(hi-gtHi, gtHi-unHi); fvswap(unLo, hi-m+1, m);
483 + n = lo + unLo - ltLo - 1;
484 + m = hi - (gtHi - unHi) + 1;
486 + if (n - lo > hi - m) {
501 +#undef FALLBACK_QSORT_SMALL_THRESH
502 +#undef FALLBACK_QSORT_STACK_SIZE
505 +/*---------------------------------------------*/
508 + eclass exists for [0 .. nblock-1]
509 + ((UChar*)eclass) [0 .. nblock-1] holds block
510 + ptr exists for [0 .. nblock-1]
513 + ((UChar*)eclass) [0 .. nblock-1] holds block
514 + All other areas of eclass destroyed
515 + fmap [0 .. nblock-1] holds sorted order
516 + bhtab [ 0 .. 2+(nblock/32) ] destroyed
519 +#define SET_BH(zz) bhtab[(zz) >> 5] |= (1 << ((zz) & 31))
520 +#define CLEAR_BH(zz) bhtab[(zz) >> 5] &= ~(1 << ((zz) & 31))
521 +#define ISSET_BH(zz) (bhtab[(zz) >> 5] & (1 << ((zz) & 31)))
522 +#define WORD_BH(zz) bhtab[(zz) >> 5]
523 +#define UNALIGNED_BH(zz) ((zz) & 0x01f)
526 +void fallbackSort ( UInt32* fmap,
533 + Int32 ftabCopy[256];
534 + Int32 H, i, j, k, l, r, cc, cc1;
537 + UChar* eclass8 = (UChar*)eclass;
540 + Initial 1-char radix sort to generate
541 + initial fmap and initial BH bits.
544 + VPrintf0 ( " bucket sorting ...\n" );
545 + for (i = 0; i < 257; i++) ftab[i] = 0;
546 + for (i = 0; i < nblock; i++) ftab[eclass8[i]]++;
547 + for (i = 0; i < 256; i++) ftabCopy[i] = ftab[i];
548 + for (i = 1; i < 257; i++) ftab[i] += ftab[i-1];
550 + for (i = 0; i < nblock; i++) {
557 + nBhtab = 2 + (nblock / 32);
558 + for (i = 0; i < nBhtab; i++) bhtab[i] = 0;
559 + for (i = 0; i < 256; i++) SET_BH(ftab[i]);
562 + Inductively refine the buckets. Kind-of an
563 + "exponential radix sort" (!), inspired by the
564 + Manber-Myers suffix array construction algorithm.
567 + /*-- set sentinel bits for block-end detection --*/
568 + for (i = 0; i < 32; i++) {
569 + SET_BH(nblock + 2*i);
570 + CLEAR_BH(nblock + 2*i + 1);
573 + /*-- the log(N) loop --*/
578 + VPrintf1 ( " depth %6d has ", H );
581 + for (i = 0; i < nblock; i++) {
582 + if (ISSET_BH(i)) j = i;
583 + k = fmap[i] - H; if (k < 0) k += nblock;
591 + /*-- find the next non-singleton bucket --*/
593 + while (ISSET_BH(k) && UNALIGNED_BH(k)) k++;
595 + while (WORD_BH(k) == 0xffffffff) k += 32;
596 + while (ISSET_BH(k)) k++;
599 + if (l >= nblock) break;
600 + while (!ISSET_BH(k) && UNALIGNED_BH(k)) k++;
601 + if (!ISSET_BH(k)) {
602 + while (WORD_BH(k) == 0x00000000) k += 32;
603 + while (!ISSET_BH(k)) k++;
606 + if (r >= nblock) break;
608 + /*-- now [l, r] bracket current bucket --*/
610 + nNotDone += (r - l + 1);
611 + fallbackQSort3 ( fmap, eclass, l, r );
613 + /*-- scan bucket and generate header bits-- */
615 + for (i = l; i <= r; i++) {
616 + cc1 = eclass[fmap[i]];
617 + if (cc != cc1) { SET_BH(i); cc = cc1; };
623 + VPrintf1 ( "%6d unresolved strings\n", nNotDone );
626 + if (H > nblock || nNotDone == 0) break;
630 + Reconstruct the original block in
631 + eclass8 [0 .. nblock-1], since the
632 + previous phase destroyed it.
635 + VPrintf0 ( " reconstructing block ...\n" );
637 + for (i = 0; i < nblock; i++) {
638 + while (ftabCopy[j] == 0) j++;
640 + eclass8[fmap[i]] = (UChar)j;
642 + AssertH ( j < 256, 1005 );
652 +/*---------------------------------------------*/
653 +/*--- The main, O(N^2 log(N)) sorting ---*/
654 +/*--- algorithm. Faster for "normal" ---*/
655 +/*--- non-repetitive blocks. ---*/
656 +/*---------------------------------------------*/
658 +/*---------------------------------------------*/
661 +Bool mainGtU ( UInt32 i1,
672 + AssertD ( i1 != i2, "mainGtU" );
674 + c1 = block[i1]; c2 = block[i2];
675 + if (c1 != c2) return (c1 > c2);
678 + c1 = block[i1]; c2 = block[i2];
679 + if (c1 != c2) return (c1 > c2);
682 + c1 = block[i1]; c2 = block[i2];
683 + if (c1 != c2) return (c1 > c2);
686 + c1 = block[i1]; c2 = block[i2];
687 + if (c1 != c2) return (c1 > c2);
690 + c1 = block[i1]; c2 = block[i2];
691 + if (c1 != c2) return (c1 > c2);
694 + c1 = block[i1]; c2 = block[i2];
695 + if (c1 != c2) return (c1 > c2);
698 + c1 = block[i1]; c2 = block[i2];
699 + if (c1 != c2) return (c1 > c2);
702 + c1 = block[i1]; c2 = block[i2];
703 + if (c1 != c2) return (c1 > c2);
706 + c1 = block[i1]; c2 = block[i2];
707 + if (c1 != c2) return (c1 > c2);
710 + c1 = block[i1]; c2 = block[i2];
711 + if (c1 != c2) return (c1 > c2);
714 + c1 = block[i1]; c2 = block[i2];
715 + if (c1 != c2) return (c1 > c2);
718 + c1 = block[i1]; c2 = block[i2];
719 + if (c1 != c2) return (c1 > c2);
726 + c1 = block[i1]; c2 = block[i2];
727 + if (c1 != c2) return (c1 > c2);
728 + s1 = quadrant[i1]; s2 = quadrant[i2];
729 + if (s1 != s2) return (s1 > s2);
732 + c1 = block[i1]; c2 = block[i2];
733 + if (c1 != c2) return (c1 > c2);
734 + s1 = quadrant[i1]; s2 = quadrant[i2];
735 + if (s1 != s2) return (s1 > s2);
738 + c1 = block[i1]; c2 = block[i2];
739 + if (c1 != c2) return (c1 > c2);
740 + s1 = quadrant[i1]; s2 = quadrant[i2];
741 + if (s1 != s2) return (s1 > s2);
744 + c1 = block[i1]; c2 = block[i2];
745 + if (c1 != c2) return (c1 > c2);
746 + s1 = quadrant[i1]; s2 = quadrant[i2];
747 + if (s1 != s2) return (s1 > s2);
750 + c1 = block[i1]; c2 = block[i2];
751 + if (c1 != c2) return (c1 > c2);
752 + s1 = quadrant[i1]; s2 = quadrant[i2];
753 + if (s1 != s2) return (s1 > s2);
756 + c1 = block[i1]; c2 = block[i2];
757 + if (c1 != c2) return (c1 > c2);
758 + s1 = quadrant[i1]; s2 = quadrant[i2];
759 + if (s1 != s2) return (s1 > s2);
762 + c1 = block[i1]; c2 = block[i2];
763 + if (c1 != c2) return (c1 > c2);
764 + s1 = quadrant[i1]; s2 = quadrant[i2];
765 + if (s1 != s2) return (s1 > s2);
768 + c1 = block[i1]; c2 = block[i2];
769 + if (c1 != c2) return (c1 > c2);
770 + s1 = quadrant[i1]; s2 = quadrant[i2];
771 + if (s1 != s2) return (s1 > s2);
774 + if (i1 >= nblock) i1 -= nblock;
775 + if (i2 >= nblock) i2 -= nblock;
786 +/*---------------------------------------------*/
788 + Knuth's increments seem to work better
789 + than Incerpi-Sedgewick here. Possibly
790 + because the number of elems to sort is
791 + usually small, typically <= 20.
794 +Int32 incs[14] = { 1, 4, 13, 40, 121, 364, 1093, 3280,
795 + 9841, 29524, 88573, 265720,
799 +void mainSimpleSort ( UInt32* ptr,
808 + Int32 i, j, h, bigN, hp;
811 + bigN = hi - lo + 1;
812 + if (bigN < 2) return;
815 + while (incs[hp] < bigN) hp++;
818 + for (; hp >= 0; hp--) {
829 + ptr[j-h]+d, v+d, block, quadrant, nblock, budget
833 + if (j <= (lo + h - 1)) break;
843 + ptr[j-h]+d, v+d, block, quadrant, nblock, budget
847 + if (j <= (lo + h - 1)) break;
857 + ptr[j-h]+d, v+d, block, quadrant, nblock, budget
861 + if (j <= (lo + h - 1)) break;
866 + if (*budget < 0) return;
872 +/*---------------------------------------------*/
874 + The following is an implementation of
875 + an elegant 3-way quicksort for strings,
876 + described in a paper "Fast Algorithms for
877 + Sorting and Searching Strings", by Robert
878 + Sedgewick and Jon L. Bentley.
881 +#define mswap(zz1, zz2) \
882 + { Int32 zztmp = zz1; zz1 = zz2; zz2 = zztmp; }
884 +#define mvswap(zzp1, zzp2, zzn) \
886 + Int32 yyp1 = (zzp1); \
887 + Int32 yyp2 = (zzp2); \
888 + Int32 yyn = (zzn); \
889 + while (yyn > 0) { \
890 + mswap(ptr[yyp1], ptr[yyp2]); \
891 + yyp1++; yyp2++; yyn--; \
897 +UChar mmed3 ( UChar a, UChar b, UChar c )
900 + if (a > b) { t = a; a = b; b = t; };
908 +#define mmin(a,b) ((a) < (b)) ? (a) : (b)
910 +#define mpush(lz,hz,dz) { stackLo[sp] = lz; \
911 + stackHi[sp] = hz; \
912 + stackD [sp] = dz; \
915 +#define mpop(lz,hz,dz) { sp--; \
916 + lz = stackLo[sp]; \
917 + hz = stackHi[sp]; \
918 + dz = stackD [sp]; }
921 +#define mnextsize(az) (nextHi[az]-nextLo[az])
923 +#define mnextswap(az,bz) \
925 + tz = nextLo[az]; nextLo[az] = nextLo[bz]; nextLo[bz] = tz; \
926 + tz = nextHi[az]; nextHi[az] = nextHi[bz]; nextHi[bz] = tz; \
927 + tz = nextD [az]; nextD [az] = nextD [bz]; nextD [bz] = tz; }
930 +#define MAIN_QSORT_SMALL_THRESH 20
931 +#define MAIN_QSORT_DEPTH_THRESH (BZ_N_RADIX + BZ_N_QSORT)
932 +#define MAIN_QSORT_STACK_SIZE 100
935 +void mainQSort3 ( UInt32* ptr,
944 + Int32 unLo, unHi, ltLo, gtHi, n, m, med;
945 + Int32 sp, lo, hi, d;
947 + Int32 stackLo[MAIN_QSORT_STACK_SIZE];
948 + Int32 stackHi[MAIN_QSORT_STACK_SIZE];
949 + Int32 stackD [MAIN_QSORT_STACK_SIZE];
956 + mpush ( loSt, hiSt, dSt );
960 + AssertH ( sp < MAIN_QSORT_STACK_SIZE - 2, 1001 );
962 + mpop ( lo, hi, d );
963 + if (hi - lo < MAIN_QSORT_SMALL_THRESH ||
964 + d > MAIN_QSORT_DEPTH_THRESH) {
965 + mainSimpleSort ( ptr, block, quadrant, nblock, lo, hi, d, budget );
966 + if (*budget < 0) return;
971 + mmed3 ( block[ptr[ lo ]+d],
972 + block[ptr[ hi ]+d],
973 + block[ptr[ (lo+hi)>>1 ]+d] );
980 + if (unLo > unHi) break;
981 + n = ((Int32)block[ptr[unLo]+d]) - med;
983 + mswap(ptr[unLo], ptr[ltLo]);
984 + ltLo++; unLo++; continue;
990 + if (unLo > unHi) break;
991 + n = ((Int32)block[ptr[unHi]+d]) - med;
993 + mswap(ptr[unHi], ptr[gtHi]);
994 + gtHi--; unHi--; continue;
999 + if (unLo > unHi) break;
1000 + mswap(ptr[unLo], ptr[unHi]); unLo++; unHi--;
1003 + AssertD ( unHi == unLo-1, "mainQSort3(2)" );
1005 + if (gtHi < ltLo) {
1006 + mpush(lo, hi, d+1 );
1010 + n = mmin(ltLo-lo, unLo-ltLo); mvswap(lo, unLo-n, n);
1011 + m = mmin(hi-gtHi, gtHi-unHi); mvswap(unLo, hi-m+1, m);
1013 + n = lo + unLo - ltLo - 1;
1014 + m = hi - (gtHi - unHi) + 1;
1016 + nextLo[0] = lo; nextHi[0] = n; nextD[0] = d;
1017 + nextLo[1] = m; nextHi[1] = hi; nextD[1] = d;
1018 + nextLo[2] = n+1; nextHi[2] = m-1; nextD[2] = d+1;
1020 + if (mnextsize(0) < mnextsize(1)) mnextswap(0,1);
1021 + if (mnextsize(1) < mnextsize(2)) mnextswap(1,2);
1022 + if (mnextsize(0) < mnextsize(1)) mnextswap(0,1);
1024 + AssertD (mnextsize(0) >= mnextsize(1), "mainQSort3(8)" );
1025 + AssertD (mnextsize(1) >= mnextsize(2), "mainQSort3(9)" );
1027 + mpush (nextLo[0], nextHi[0], nextD[0]);
1028 + mpush (nextLo[1], nextHi[1], nextD[1]);
1029 + mpush (nextLo[2], nextHi[2], nextD[2]);
1040 +#undef MAIN_QSORT_SMALL_THRESH
1041 +#undef MAIN_QSORT_DEPTH_THRESH
1042 +#undef MAIN_QSORT_STACK_SIZE
1045 +/*---------------------------------------------*/
1047 + nblock > N_OVERSHOOT
1048 + block32 exists for [0 .. nblock-1 +N_OVERSHOOT]
1049 + ((UChar*)block32) [0 .. nblock-1] holds block
1050 + ptr exists for [0 .. nblock-1]
1053 + ((UChar*)block32) [0 .. nblock-1] holds block
1054 + All other areas of block32 destroyed
1055 + ftab [0 .. 65536 ] destroyed
1056 + ptr [0 .. nblock-1] holds sorted order
1057 + if (*budget < 0), sorting was abandoned
1060 +#define BIGFREQ(b) (ftab[((b)+1) << 8] - ftab[(b) << 8])
1061 +#define SETMASK (1 << 21)
1062 +#define CLEARMASK (~(SETMASK))
1065 +void mainSort ( UInt32* ptr,
1073 + Int32 i, j, k, ss, sb;
1074 + Int32 runningOrder[256];
1075 + Bool bigDone[256];
1076 + Int32 copyStart[256];
1077 + Int32 copyEnd [256];
1081 + if (verb >= 4) VPrintf0 ( " main sort initialise ...\n" );
1083 + /*-- set up the 2-byte frequency table --*/
1084 + for (i = 65536; i >= 0; i--) ftab[i] = 0;
1086 + j = block[0] << 8;
1088 + for (; i >= 3; i -= 4) {
1090 + j = (j >> 8) | ( ((UInt16)block[i]) << 8);
1092 + quadrant[i-1] = 0;
1093 + j = (j >> 8) | ( ((UInt16)block[i-1]) << 8);
1095 + quadrant[i-2] = 0;
1096 + j = (j >> 8) | ( ((UInt16)block[i-2]) << 8);
1098 + quadrant[i-3] = 0;
1099 + j = (j >> 8) | ( ((UInt16)block[i-3]) << 8);
1102 + for (; i >= 0; i--) {
1104 + j = (j >> 8) | ( ((UInt16)block[i]) << 8);
1108 + /*-- (emphasises close relationship of block & quadrant) --*/
1109 + for (i = 0; i < BZ_N_OVERSHOOT; i++) {
1110 + block [nblock+i] = block[i];
1111 + quadrant[nblock+i] = 0;
1114 + if (verb >= 4) VPrintf0 ( " bucket sorting ...\n" );
1116 + /*-- Complete the initial radix sort --*/
1117 + for (i = 1; i <= 65536; i++) ftab[i] += ftab[i-1];
1119 + s = block[0] << 8;
1121 + for (; i >= 3; i -= 4) {
1122 + s = (s >> 8) | (block[i] << 8);
1126 + s = (s >> 8) | (block[i-1] << 8);
1130 + s = (s >> 8) | (block[i-2] << 8);
1134 + s = (s >> 8) | (block[i-3] << 8);
1139 + for (; i >= 0; i--) {
1140 + s = (s >> 8) | (block[i] << 8);
1147 + Now ftab contains the first loc of every small bucket.
1148 + Calculate the running order, from smallest to largest
1151 + for (i = 0; i <= 255; i++) {
1152 + bigDone [i] = False;
1153 + runningOrder[i] = i;
1159 + do h = 3 * h + 1; while (h <= 256);
1162 + for (i = h; i <= 255; i++) {
1163 + vv = runningOrder[i];
1165 + while ( BIGFREQ(runningOrder[j-h]) > BIGFREQ(vv) ) {
1166 + runningOrder[j] = runningOrder[j-h];
1168 + if (j <= (h - 1)) goto zero;
1171 + runningOrder[j] = vv;
1177 + The main sorting loop.
1182 + for (i = 0; i <= 255; i++) {
1185 + Process big buckets, starting with the least full.
1186 + Basically this is a 3-step process in which we call
1187 + mainQSort3 to sort the small buckets [ss, j], but
1188 + also make a big effort to avoid the calls if we can.
1190 + ss = runningOrder[i];
1194 + Complete the big bucket [ss] by quicksorting
1195 + any unsorted small buckets [ss, j], for j != ss.
1196 + Hopefully previous pointer-scanning phases have already
1197 + completed many of the small buckets [ss, j], so
1198 + we don't have to sort them at all.
1200 + for (j = 0; j <= 255; j++) {
1202 + sb = (ss << 8) + j;
1203 + if ( ! (ftab[sb] & SETMASK) ) {
1204 + Int32 lo = ftab[sb] & CLEARMASK;
1205 + Int32 hi = (ftab[sb+1] & CLEARMASK) - 1;
1208 + VPrintf4 ( " qsort [0x%x, 0x%x] "
1209 + "done %d this %d\n",
1210 + ss, j, numQSorted, hi - lo + 1 );
1212 + ptr, block, quadrant, nblock,
1213 + lo, hi, BZ_N_RADIX, budget
1215 + numQSorted += (hi - lo + 1);
1216 + if (*budget < 0) return;
1219 + ftab[sb] |= SETMASK;
1223 + AssertH ( !bigDone[ss], 1006 );
1227 + Now scan this big bucket [ss] so as to synthesise the
1228 + sorted order for small buckets [t, ss] for all t,
1229 + including, magically, the bucket [ss,ss] too.
1230 + This will avoid doing Real Work in subsequent Step 1's.
1233 + for (j = 0; j <= 255; j++) {
1234 + copyStart[j] = ftab[(j << 8) + ss] & CLEARMASK;
1235 + copyEnd [j] = (ftab[(j << 8) + ss + 1] & CLEARMASK) - 1;
1237 + for (j = ftab[ss << 8] & CLEARMASK; j < copyStart[ss]; j++) {
1238 + k = ptr[j]-1; if (k < 0) k += nblock;
1241 + ptr[ copyStart[c1]++ ] = k;
1243 + for (j = (ftab[(ss+1) << 8] & CLEARMASK) - 1; j > copyEnd[ss]; j--) {
1244 + k = ptr[j]-1; if (k < 0) k += nblock;
1247 + ptr[ copyEnd[c1]-- ] = k;
1251 + AssertH ( (copyStart[ss]-1 == copyEnd[ss])
1253 + /* Extremely rare case missing in bzip2-1.0.0 and 1.0.1.
1254 + Necessity for this case is demonstrated by compressing
1255 + a sequence of approximately 48.5 million of character
1256 + 251; 1.0.0/1.0.1 will then die here. */
1257 + (copyStart[ss] == 0 && copyEnd[ss] == nblock-1),
1260 + for (j = 0; j <= 255; j++) ftab[(j << 8) + ss] |= SETMASK;
1264 + The [ss] big bucket is now done. Record this fact,
1265 + and update the quadrant descriptors. Remember to
1266 + update quadrants in the overshoot area too, if
1267 + necessary. The "if (i < 255)" test merely skips
1268 + this updating for the last bucket processed, since
1269 + updating for the last bucket is pointless.
1271 + The quadrant array provides a way to incrementally
1272 + cache sort orderings, as they appear, so as to
1273 + make subsequent comparisons in fullGtU() complete
1274 + faster. For repetitive blocks this makes a big
1275 + difference (but not big enough to be able to avoid
1276 + the fallback sorting mechanism, exponential radix sort).
1278 + The precise meaning is: at all times:
1280 + for 0 <= i < nblock and 0 <= j <= nblock
1282 + if block[i] != block[j],
1284 + then the relative values of quadrant[i] and
1285 + quadrant[j] are meaningless.
1288 + if quadrant[i] < quadrant[j]
1289 + then the string starting at i lexicographically
1290 + precedes the string starting at j
1292 + else if quadrant[i] > quadrant[j]
1293 + then the string starting at j lexicographically
1294 + precedes the string starting at i
1297 + the relative ordering of the strings starting
1298 + at i and j has not yet been determined.
1301 + bigDone[ss] = True;
1304 + Int32 bbStart = ftab[ss << 8] & CLEARMASK;
1305 + Int32 bbSize = (ftab[(ss+1) << 8] & CLEARMASK) - bbStart;
1308 + while ((bbSize >> shifts) > 65534) shifts++;
1310 + for (j = bbSize-1; j >= 0; j--) {
1311 + Int32 a2update = ptr[bbStart + j];
1312 + UInt16 qVal = (UInt16)(j >> shifts);
1313 + quadrant[a2update] = qVal;
1314 + if (a2update < BZ_N_OVERSHOOT)
1315 + quadrant[a2update + nblock] = qVal;
1317 + AssertH ( ((bbSize-1) >> shifts) <= 65535, 1002 );
1323 + VPrintf3 ( " %d pointers, %d sorted, %d scanned\n",
1324 + nblock, numQSorted, nblock - numQSorted );
1332 +/*---------------------------------------------*/
1335 + arr2 exists for [0 .. nblock-1 +N_OVERSHOOT]
1336 + ((UChar*)arr2) [0 .. nblock-1] holds block
1337 + arr1 exists for [0 .. nblock-1]
1340 + ((UChar*)arr2) [0 .. nblock-1] holds block
1341 + All other areas of block destroyed
1342 + ftab [ 0 .. 65536 ] destroyed
1343 + arr1 [0 .. nblock-1] holds sorted order
1345 +void BZ2_blockSort ( EState* s )
1347 + UInt32* ptr = s->ptr;
1348 + UChar* block = s->block;
1349 + UInt32* ftab = s->ftab;
1350 + Int32 nblock = s->nblock;
1351 + Int32 verb = s->verbosity;
1352 + Int32 wfact = s->workFactor;
1358 + if (nblock < 10000) {
1359 + fallbackSort ( s->arr1, s->arr2, ftab, nblock, verb );
1361 + /* Calculate the location for quadrant, remembering to get
1362 + the alignment right. Assumes that &(block[0]) is at least
1363 + 2-byte aligned -- this should be ok since block is really
1364 + the first section of arr2.
1366 + i = nblock+BZ_N_OVERSHOOT;
1368 + quadrant = (UInt16*)(&(block[i]));
1370 + /* (wfact-1) / 3 puts the default-factor-30
1371 + transition point at very roughly the same place as
1372 + with v0.1 and v0.9.0.
1373 + Not that it particularly matters any more, since the
1374 + resulting compressed stream is now the same regardless
1375 + of whether or not we use the main sort or fallback sort.
1377 + if (wfact < 1 ) wfact = 1;
1378 + if (wfact > 100) wfact = 100;
1379 + budgetInit = nblock * ((wfact-1) / 3);
1380 + budget = budgetInit;
1382 + mainSort ( ptr, block, quadrant, ftab, nblock, verb, &budget );
1384 + VPrintf3 ( " %d work, %d block, ratio %5.2f\n",
1385 + budgetInit - budget,
1387 + (float)(budgetInit - budget) /
1388 + (float)(nblock==0 ? 1 : nblock) );
1391 + VPrintf0 ( " too repetitive; using fallback"
1392 + " sorting algorithm\n" );
1393 + fallbackSort ( s->arr1, s->arr2, ftab, nblock, verb );
1398 + for (i = 0; i < s->nblock; i++)
1400 + { s->origPtr = i; break; };
1402 + AssertH( s->origPtr != -1, 1003 );
1406 +/*-------------------------------------------------------------*/
1407 +/*--- end blocksort.c ---*/
1408 +/*-------------------------------------------------------------*/
1409 diff --git a/pit/upstream-bzip2/bzip2.c b/pit/upstream-bzip2/bzip2.c
1410 new file mode 100644
1411 index 0000000..6de9d1d
1413 +++ b/pit/upstream-bzip2/bzip2.c
1416 +/*-----------------------------------------------------------*/
1417 +/*--- A block-sorting, lossless compressor bzip2.c ---*/
1418 +/*-----------------------------------------------------------*/
1420 +/* ------------------------------------------------------------------
1421 + This file is part of bzip2/libbzip2, a program and library for
1422 + lossless, block-sorting data compression.
1424 + bzip2/libbzip2 version 1.0.6 of 6 September 2010
1425 + Copyright (C) 1996-2010 Julian Seward <jseward@bzip.org>
1427 + Please read the WARNING, DISCLAIMER and PATENTS sections in the
1430 + This program is released under the terms of the license contained
1431 + in the file LICENSE.
1432 + ------------------------------------------------------------------ */
1435 +/* Place a 1 beside your platform, and 0 elsewhere.
1436 + Generic 32-bit Unix.
1437 + Also works on 64-bit Unix boxes.
1438 + This is the default.
1443 + Win32, as seen by Jacob Navia's excellent
1444 + port of (Chris Fraser & David Hanson)'s excellent
1445 + lcc compiler. Or with MS Visual C.
1446 + This is selected automatically if compiled by a compiler which
1447 + defines _WIN32, not including the Cygwin GCC.
1449 +#define BZ_LCCWIN32 0
1451 +#if defined(_WIN32) && !defined(__CYGWIN__)
1453 +#define BZ_LCCWIN32 1
1459 +/*---------------------------------------------*/
1461 + Some stuff for all platforms.
1465 +#include <stdlib.h>
1466 +#include <string.h>
1467 +#include <signal.h>
1473 +#define ERROR_IF_EOF(i) { if ((i) == EOF) ioError(); }
1474 +#define ERROR_IF_NOT_ZERO(i) { if ((i) != 0) ioError(); }
1475 +#define ERROR_IF_MINUS_ONE(i) { if ((i) == (-1)) ioError(); }
1478 +/*---------------------------------------------*/
1480 + Platform-specific stuff.
1484 +# include <fcntl.h>
1485 +# include <sys/types.h>
1486 +# include <utime.h>
1487 +# include <unistd.h>
1488 +# include <sys/stat.h>
1489 +# include <sys/times.h>
1491 +# define PATH_SEP '/'
1492 +# define MY_LSTAT lstat
1493 +# define MY_STAT stat
1494 +# define MY_S_ISREG S_ISREG
1495 +# define MY_S_ISDIR S_ISDIR
1497 +# define APPEND_FILESPEC(root, name) \
1498 + root=snocString((root), (name))
1500 +# define APPEND_FLAG(root, name) \
1501 + root=snocString((root), (name))
1503 +# define SET_BINARY_MODE(fd) /**/
1506 +# define NORETURN __attribute__ ((noreturn))
1508 +# define NORETURN /**/
1513 +# include <fcntl.h>
1516 +# define MY_LSTAT stat
1517 +# define MY_STAT stat
1518 +# undef SET_BINARY_MODE
1519 +# define SET_BINARY_MODE(fd) \
1521 + int retVal = setmode ( fileno ( fd ), \
1523 + ERROR_IF_MINUS_ONE ( retVal ); \
1529 +# include <fcntl.h>
1530 +# undef SET_BINARY_MODE
1531 +# define SET_BINARY_MODE(fd) \
1533 + int retVal = setmode ( fileno ( fd ), \
1535 + ERROR_IF_MINUS_ONE ( retVal ); \
1538 +#endif /* BZ_UNIX */
1544 +# include <fcntl.h>
1545 +# include <sys\stat.h>
1547 +# define NORETURN /**/
1548 +# define PATH_SEP '\\'
1549 +# define MY_LSTAT _stat
1550 +# define MY_STAT _stat
1551 +# define MY_S_ISREG(x) ((x) & _S_IFREG)
1552 +# define MY_S_ISDIR(x) ((x) & _S_IFDIR)
1554 +# define APPEND_FLAG(root, name) \
1555 + root=snocString((root), (name))
1557 +# define APPEND_FILESPEC(root, name) \
1558 + root = snocString ((root), (name))
1560 +# define SET_BINARY_MODE(fd) \
1562 + int retVal = setmode ( fileno ( fd ), \
1564 + ERROR_IF_MINUS_ONE ( retVal ); \
1567 +#endif /* BZ_LCCWIN32 */
1570 +/*---------------------------------------------*/
1572 + Some more stuff for all platforms :-)
1576 +typedef unsigned char Bool;
1577 +typedef unsigned char UChar;
1579 +typedef unsigned int UInt32;
1580 +typedef short Int16;
1581 +typedef unsigned short UInt16;
1583 +#define True ((Bool)1)
1584 +#define False ((Bool)0)
1587 + IntNative is your platform's `native' int size.
1588 + Only here to avoid probs with 64-bit platforms.
1590 +typedef int IntNative;
1593 +/*---------------------------------------------------*/
1594 +/*--- Misc (file handling) data decls ---*/
1595 +/*---------------------------------------------------*/
1598 +Bool keepInputFiles, smallMode, deleteOutputOnInterrupt;
1599 +Bool forceOverwrite, testFailsExist, unzFailsExist, noisy;
1600 +Int32 numFileNames, numFilesProcessed, blockSize100k;
1603 +/*-- source modes; F==file, I==stdin, O==stdout --*/
1608 +/*-- operation modes --*/
1616 +#define FILE_NAME_LEN 1034
1618 +Int32 longestFileName;
1619 +Char inName [FILE_NAME_LEN];
1620 +Char outName[FILE_NAME_LEN];
1621 +Char tmpName[FILE_NAME_LEN];
1623 +Char progNameReally[FILE_NAME_LEN];
1624 +FILE *outputHandleJustInCase;
1627 +static void panic ( const Char* ) NORETURN;
1628 +static void ioError ( void ) NORETURN;
1629 +static void outOfMemory ( void ) NORETURN;
1630 +static void configError ( void ) NORETURN;
1631 +static void crcError ( void ) NORETURN;
1632 +static void cleanUpAndFail ( Int32 ) NORETURN;
1633 +static void compressedStreamEOF ( void ) NORETURN;
1635 +static void copyFileName ( Char*, Char* );
1636 +static void* myMalloc ( Int32 );
1637 +static void applySavedFileAttrToOutputFile ( IntNative fd );
1641 +/*---------------------------------------------------*/
1642 +/*--- An implementation of 64-bit ints. Sigh. ---*/
1643 +/*--- Roll on widespread deployment of ANSI C9X ! ---*/
1644 +/*---------------------------------------------------*/
1647 + struct { UChar b[8]; }
1652 +void uInt64_from_UInt32s ( UInt64* n, UInt32 lo32, UInt32 hi32 )
1654 + n->b[7] = (UChar)((hi32 >> 24) & 0xFF);
1655 + n->b[6] = (UChar)((hi32 >> 16) & 0xFF);
1656 + n->b[5] = (UChar)((hi32 >> 8) & 0xFF);
1657 + n->b[4] = (UChar) (hi32 & 0xFF);
1658 + n->b[3] = (UChar)((lo32 >> 24) & 0xFF);
1659 + n->b[2] = (UChar)((lo32 >> 16) & 0xFF);
1660 + n->b[1] = (UChar)((lo32 >> 8) & 0xFF);
1661 + n->b[0] = (UChar) (lo32 & 0xFF);
1666 +double uInt64_to_double ( UInt64* n )
1669 + double base = 1.0;
1671 + for (i = 0; i < 8; i++) {
1672 + sum += base * (double)(n->b[i]);
1680 +Bool uInt64_isZero ( UInt64* n )
1683 + for (i = 0; i < 8; i++)
1684 + if (n->b[i] != 0) return 0;
1689 +/* Divide *n by 10, and return the remainder. */
1691 +Int32 uInt64_qrm10 ( UInt64* n )
1696 + for (i = 7; i >= 0; i--) {
1697 + tmp = rem * 256 + n->b[i];
1698 + n->b[i] = tmp / 10;
1705 +/* ... and the Whole Entire Point of all this UInt64 stuff is
1706 + so that we can supply the following function.
1709 +void uInt64_toAscii ( char* outbuf, UInt64* n )
1714 + UInt64 n_copy = *n;
1716 + q = uInt64_qrm10 ( &n_copy );
1717 + buf[nBuf] = q + '0';
1719 + } while (!uInt64_isZero(&n_copy));
1721 + for (i = 0; i < nBuf; i++)
1722 + outbuf[i] = buf[nBuf-i-1];
1726 +/*---------------------------------------------------*/
1727 +/*--- Processing of complete files and streams ---*/
1728 +/*---------------------------------------------------*/
1730 +/*---------------------------------------------*/
1732 +Bool myfeof ( FILE* f )
1734 + Int32 c = fgetc ( f );
1735 + if (c == EOF) return True;
1741 +/*---------------------------------------------*/
1743 +void compressStream ( FILE *stream, FILE *zStream )
1745 + BZFILE* bzf = NULL;
1748 + UInt32 nbytes_in_lo32, nbytes_in_hi32;
1749 + UInt32 nbytes_out_lo32, nbytes_out_hi32;
1750 + Int32 bzerr, bzerr_dummy, ret;
1752 + SET_BINARY_MODE(stream);
1753 + SET_BINARY_MODE(zStream);
1755 + if (ferror(stream)) goto errhandler_io;
1756 + if (ferror(zStream)) goto errhandler_io;
1758 + bzf = BZ2_bzWriteOpen ( &bzerr, zStream,
1759 + blockSize100k, verbosity, workFactor );
1760 + if (bzerr != BZ_OK) goto errhandler;
1762 + if (verbosity >= 2) fprintf ( stderr, "\n" );
1766 + if (myfeof(stream)) break;
1767 + nIbuf = fread ( ibuf, sizeof(UChar), 5000, stream );
1768 + if (ferror(stream)) goto errhandler_io;
1769 + if (nIbuf > 0) BZ2_bzWrite ( &bzerr, bzf, (void*)ibuf, nIbuf );
1770 + if (bzerr != BZ_OK) goto errhandler;
1774 + BZ2_bzWriteClose64 ( &bzerr, bzf, 0,
1775 + &nbytes_in_lo32, &nbytes_in_hi32,
1776 + &nbytes_out_lo32, &nbytes_out_hi32 );
1777 + if (bzerr != BZ_OK) goto errhandler;
1779 + if (ferror(zStream)) goto errhandler_io;
1780 + ret = fflush ( zStream );
1781 + if (ret == EOF) goto errhandler_io;
1782 + if (zStream != stdout) {
1783 + Int32 fd = fileno ( zStream );
1784 + if (fd < 0) goto errhandler_io;
1785 + applySavedFileAttrToOutputFile ( fd );
1786 + ret = fclose ( zStream );
1787 + outputHandleJustInCase = NULL;
1788 + if (ret == EOF) goto errhandler_io;
1790 + outputHandleJustInCase = NULL;
1791 + if (ferror(stream)) goto errhandler_io;
1792 + ret = fclose ( stream );
1793 + if (ret == EOF) goto errhandler_io;
1795 + if (verbosity >= 1) {
1796 + if (nbytes_in_lo32 == 0 && nbytes_in_hi32 == 0) {
1797 + fprintf ( stderr, " no data compressed.\n");
1799 + Char buf_nin[32], buf_nout[32];
1800 + UInt64 nbytes_in, nbytes_out;
1801 + double nbytes_in_d, nbytes_out_d;
1802 + uInt64_from_UInt32s ( &nbytes_in,
1803 + nbytes_in_lo32, nbytes_in_hi32 );
1804 + uInt64_from_UInt32s ( &nbytes_out,
1805 + nbytes_out_lo32, nbytes_out_hi32 );
1806 + nbytes_in_d = uInt64_to_double ( &nbytes_in );
1807 + nbytes_out_d = uInt64_to_double ( &nbytes_out );
1808 + uInt64_toAscii ( buf_nin, &nbytes_in );
1809 + uInt64_toAscii ( buf_nout, &nbytes_out );
1810 + fprintf ( stderr, "%6.3f:1, %6.3f bits/byte, "
1811 + "%5.2f%% saved, %s in, %s out.\n",
1812 + nbytes_in_d / nbytes_out_d,
1813 + (8.0 * nbytes_out_d) / nbytes_in_d,
1814 + 100.0 * (1.0 - nbytes_out_d / nbytes_in_d),
1824 + BZ2_bzWriteClose64 ( &bzerr_dummy, bzf, 1,
1825 + &nbytes_in_lo32, &nbytes_in_hi32,
1826 + &nbytes_out_lo32, &nbytes_out_hi32 );
1828 + case BZ_CONFIG_ERROR:
1829 + configError(); break;
1830 + case BZ_MEM_ERROR:
1831 + outOfMemory (); break;
1836 + panic ( "compress:unexpected error" );
1839 + panic ( "compress:end" );
1845 +/*---------------------------------------------*/
1847 +Bool uncompressStream ( FILE *zStream, FILE *stream )
1849 + BZFILE* bzf = NULL;
1850 + Int32 bzerr, bzerr_dummy, ret, nread, streamNo, i;
1852 + UChar unused[BZ_MAX_UNUSED];
1860 + SET_BINARY_MODE(stream);
1861 + SET_BINARY_MODE(zStream);
1863 + if (ferror(stream)) goto errhandler_io;
1864 + if (ferror(zStream)) goto errhandler_io;
1868 + bzf = BZ2_bzReadOpen (
1869 + &bzerr, zStream, verbosity,
1870 + (int)smallMode, unused, nUnused
1872 + if (bzf == NULL || bzerr != BZ_OK) goto errhandler;
1875 + while (bzerr == BZ_OK) {
1876 + nread = BZ2_bzRead ( &bzerr, bzf, obuf, 5000 );
1877 + if (bzerr == BZ_DATA_ERROR_MAGIC) goto trycat;
1878 + if ((bzerr == BZ_OK || bzerr == BZ_STREAM_END) && nread > 0)
1879 + fwrite ( obuf, sizeof(UChar), nread, stream );
1880 + if (ferror(stream)) goto errhandler_io;
1882 + if (bzerr != BZ_STREAM_END) goto errhandler;
1884 + BZ2_bzReadGetUnused ( &bzerr, bzf, &unusedTmpV, &nUnused );
1885 + if (bzerr != BZ_OK) panic ( "decompress:bzReadGetUnused" );
1887 + unusedTmp = (UChar*)unusedTmpV;
1888 + for (i = 0; i < nUnused; i++) unused[i] = unusedTmp[i];
1890 + BZ2_bzReadClose ( &bzerr, bzf );
1891 + if (bzerr != BZ_OK) panic ( "decompress:bzReadGetUnused" );
1893 + if (nUnused == 0 && myfeof(zStream)) break;
1897 + if (ferror(zStream)) goto errhandler_io;
1898 + if (stream != stdout) {
1899 + Int32 fd = fileno ( stream );
1900 + if (fd < 0) goto errhandler_io;
1901 + applySavedFileAttrToOutputFile ( fd );
1903 + ret = fclose ( zStream );
1904 + if (ret == EOF) goto errhandler_io;
1906 + if (ferror(stream)) goto errhandler_io;
1907 + ret = fflush ( stream );
1908 + if (ret != 0) goto errhandler_io;
1909 + if (stream != stdout) {
1910 + ret = fclose ( stream );
1911 + outputHandleJustInCase = NULL;
1912 + if (ret == EOF) goto errhandler_io;
1914 + outputHandleJustInCase = NULL;
1915 + if (verbosity >= 2) fprintf ( stderr, "\n " );
1919 + if (forceOverwrite) {
1922 + if (myfeof(zStream)) break;
1923 + nread = fread ( obuf, sizeof(UChar), 5000, zStream );
1924 + if (ferror(zStream)) goto errhandler_io;
1925 + if (nread > 0) fwrite ( obuf, sizeof(UChar), nread, stream );
1926 + if (ferror(stream)) goto errhandler_io;
1932 + BZ2_bzReadClose ( &bzerr_dummy, bzf );
1934 + case BZ_CONFIG_ERROR:
1935 + configError(); break;
1939 + case BZ_DATA_ERROR:
1941 + case BZ_MEM_ERROR:
1943 + case BZ_UNEXPECTED_EOF:
1944 + compressedStreamEOF();
1945 + case BZ_DATA_ERROR_MAGIC:
1946 + if (zStream != stdin) fclose(zStream);
1947 + if (stream != stdout) fclose(stream);
1948 + if (streamNo == 1) {
1953 + "\n%s: %s: trailing garbage after EOF ignored\n",
1954 + progName, inName );
1958 + panic ( "decompress:unexpected error" );
1961 + panic ( "decompress:end" );
1962 + return True; /*notreached*/
1966 +/*---------------------------------------------*/
1968 +Bool testStream ( FILE *zStream )
1970 + BZFILE* bzf = NULL;
1971 + Int32 bzerr, bzerr_dummy, ret, nread, streamNo, i;
1973 + UChar unused[BZ_MAX_UNUSED];
1981 + SET_BINARY_MODE(zStream);
1982 + if (ferror(zStream)) goto errhandler_io;
1986 + bzf = BZ2_bzReadOpen (
1987 + &bzerr, zStream, verbosity,
1988 + (int)smallMode, unused, nUnused
1990 + if (bzf == NULL || bzerr != BZ_OK) goto errhandler;
1993 + while (bzerr == BZ_OK) {
1994 + nread = BZ2_bzRead ( &bzerr, bzf, obuf, 5000 );
1995 + if (bzerr == BZ_DATA_ERROR_MAGIC) goto errhandler;
1997 + if (bzerr != BZ_STREAM_END) goto errhandler;
1999 + BZ2_bzReadGetUnused ( &bzerr, bzf, &unusedTmpV, &nUnused );
2000 + if (bzerr != BZ_OK) panic ( "test:bzReadGetUnused" );
2002 + unusedTmp = (UChar*)unusedTmpV;
2003 + for (i = 0; i < nUnused; i++) unused[i] = unusedTmp[i];
2005 + BZ2_bzReadClose ( &bzerr, bzf );
2006 + if (bzerr != BZ_OK) panic ( "test:bzReadGetUnused" );
2007 + if (nUnused == 0 && myfeof(zStream)) break;
2011 + if (ferror(zStream)) goto errhandler_io;
2012 + ret = fclose ( zStream );
2013 + if (ret == EOF) goto errhandler_io;
2015 + if (verbosity >= 2) fprintf ( stderr, "\n " );
2019 + BZ2_bzReadClose ( &bzerr_dummy, bzf );
2020 + if (verbosity == 0)
2021 + fprintf ( stderr, "%s: %s: ", progName, inName );
2023 + case BZ_CONFIG_ERROR:
2024 + configError(); break;
2028 + case BZ_DATA_ERROR:
2030 + "data integrity (CRC) error in data\n" );
2032 + case BZ_MEM_ERROR:
2034 + case BZ_UNEXPECTED_EOF:
2036 + "file ends unexpectedly\n" );
2038 + case BZ_DATA_ERROR_MAGIC:
2039 + if (zStream != stdin) fclose(zStream);
2040 + if (streamNo == 1) {
2042 + "bad magic number (file not created by bzip2)\n" );
2047 + "trailing garbage after EOF ignored\n" );
2051 + panic ( "test:unexpected error" );
2054 + panic ( "test:end" );
2055 + return True; /*notreached*/
2059 +/*---------------------------------------------------*/
2060 +/*--- Error [non-] handling grunge ---*/
2061 +/*---------------------------------------------------*/
2063 +/*---------------------------------------------*/
2065 +void setExit ( Int32 v )
2067 + if (v > exitValue) exitValue = v;
2071 +/*---------------------------------------------*/
2073 +void cadvise ( void )
2078 + "\nIt is possible that the compressed file(s) have become corrupted.\n"
2079 + "You can use the -tvv option to test integrity of such files.\n\n"
2080 + "You can use the `bzip2recover' program to attempt to recover\n"
2081 + "data from undamaged sections of corrupted files.\n\n"
2086 +/*---------------------------------------------*/
2088 +void showFileNames ( void )
2093 + "\tInput file = %s, output file = %s\n",
2099 +/*---------------------------------------------*/
2101 +void cleanUpAndFail ( Int32 ec )
2104 + struct MY_STAT statBuf;
2106 + if ( srcMode == SM_F2F
2107 + && opMode != OM_TEST
2108 + && deleteOutputOnInterrupt ) {
2110 + /* Check whether input file still exists. Delete output file
2111 + only if input exists to avoid loss of data. Joerg Prante, 5
2112 + January 2002. (JRS 06-Jan-2002: other changes in 1.0.2 mean
2113 + this is less likely to happen. But to be ultra-paranoid, we
2114 + do the check anyway.) */
2115 + retVal = MY_STAT ( inName, &statBuf );
2116 + if (retVal == 0) {
2119 + "%s: Deleting output file %s, if it exists.\n",
2120 + progName, outName );
2121 + if (outputHandleJustInCase != NULL)
2122 + fclose ( outputHandleJustInCase );
2123 + retVal = remove ( outName );
2126 + "%s: WARNING: deletion of output file "
2127 + "(apparently) failed.\n",
2131 + "%s: WARNING: deletion of output file suppressed\n",
2134 + "%s: since input file no longer exists. Output file\n",
2137 + "%s: `%s' may be incomplete.\n",
2138 + progName, outName );
2140 + "%s: I suggest doing an integrity test (bzip2 -tv)"
2146 + if (noisy && numFileNames > 0 && numFilesProcessed < numFileNames) {
2148 + "%s: WARNING: some files have not been processed:\n"
2149 + "%s: %d specified on command line, %d not processed yet.\n\n",
2150 + progName, progName,
2151 + numFileNames, numFileNames - numFilesProcessed );
2158 +/*---------------------------------------------*/
2160 +void panic ( const Char* s )
2163 + "\n%s: PANIC -- internal consistency error:\n"
2165 + "\tThis is a BUG. Please report it to me at:\n"
2166 + "\tjseward@bzip.org\n",
2169 + cleanUpAndFail( 3 );
2173 +/*---------------------------------------------*/
2175 +void crcError ( void )
2178 + "\n%s: Data integrity error when decompressing.\n",
2182 + cleanUpAndFail( 2 );
2186 +/*---------------------------------------------*/
2188 +void compressedStreamEOF ( void )
2192 + "\n%s: Compressed file ends unexpectedly;\n\t"
2193 + "perhaps it is corrupted? *Possible* reason follows.\n",
2195 + perror ( progName );
2199 + cleanUpAndFail( 2 );
2203 +/*---------------------------------------------*/
2205 +void ioError ( void )
2208 + "\n%s: I/O or other error, bailing out. "
2209 + "Possible reason follows.\n",
2211 + perror ( progName );
2213 + cleanUpAndFail( 1 );
2217 +/*---------------------------------------------*/
2219 +void mySignalCatcher ( IntNative n )
2222 + "\n%s: Control-C or similar caught, quitting.\n",
2224 + cleanUpAndFail(1);
2228 +/*---------------------------------------------*/
2230 +void mySIGSEGVorSIGBUScatcher ( IntNative n )
2232 + if (opMode == OM_Z)
2235 + "\n%s: Caught a SIGSEGV or SIGBUS whilst compressing.\n"
2237 + " Possible causes are (most likely first):\n"
2238 + " (1) This computer has unreliable memory or cache hardware\n"
2239 + " (a surprisingly common problem; try a different machine.)\n"
2240 + " (2) A bug in the compiler used to create this executable\n"
2241 + " (unlikely, if you didn't compile bzip2 yourself.)\n"
2242 + " (3) A real bug in bzip2 -- I hope this should never be the case.\n"
2243 + " The user's manual, Section 4.3, has more info on (1) and (2).\n"
2245 + " If you suspect this is a bug in bzip2, or are unsure about (1)\n"
2246 + " or (2), feel free to report it to me at: jseward@bzip.org.\n"
2247 + " Section 4.3 of the user's manual describes the info a useful\n"
2248 + " bug report should have. If the manual is available on your\n"
2249 + " system, please try and read it before mailing me. If you don't\n"
2250 + " have the manual or can't be bothered to read it, mail me anyway.\n"
2256 + "\n%s: Caught a SIGSEGV or SIGBUS whilst decompressing.\n"
2258 + " Possible causes are (most likely first):\n"
2259 + " (1) The compressed data is corrupted, and bzip2's usual checks\n"
2260 + " failed to detect this. Try bzip2 -tvv my_file.bz2.\n"
2261 + " (2) This computer has unreliable memory or cache hardware\n"
2262 + " (a surprisingly common problem; try a different machine.)\n"
2263 + " (3) A bug in the compiler used to create this executable\n"
2264 + " (unlikely, if you didn't compile bzip2 yourself.)\n"
2265 + " (4) A real bug in bzip2 -- I hope this should never be the case.\n"
2266 + " The user's manual, Section 4.3, has more info on (2) and (3).\n"
2268 + " If you suspect this is a bug in bzip2, or are unsure about (2)\n"
2269 + " or (3), feel free to report it to me at: jseward@bzip.org.\n"
2270 + " Section 4.3 of the user's manual describes the info a useful\n"
2271 + " bug report should have. If the manual is available on your\n"
2272 + " system, please try and read it before mailing me. If you don't\n"
2273 + " have the manual or can't be bothered to read it, mail me anyway.\n"
2278 + if (opMode == OM_Z)
2279 + cleanUpAndFail( 3 ); else
2280 + { cadvise(); cleanUpAndFail( 2 ); }
2284 +/*---------------------------------------------*/
2286 +void outOfMemory ( void )
2289 + "\n%s: couldn't allocate enough memory\n",
2292 + cleanUpAndFail(1);
2296 +/*---------------------------------------------*/
2298 +void configError ( void )
2301 + "bzip2: I'm not configured correctly for this platform!\n"
2302 + "\tI require Int32, Int16 and Char to have sizes\n"
2303 + "\tof 4, 2 and 1 bytes to run properly, and they don't.\n"
2304 + "\tProbably you can fix this by defining them correctly,\n"
2305 + "\tand recompiling. Bye!\n" );
2311 +/*---------------------------------------------------*/
2312 +/*--- The main driver machinery ---*/
2313 +/*---------------------------------------------------*/
2315 +/* All rather crufty. The main problem is that input files
2316 + are stat()d multiple times before use. This should be
2320 +/*---------------------------------------------*/
2322 +void pad ( Char *s )
2325 + if ( (Int32)strlen(s) >= longestFileName ) return;
2326 + for (i = 1; i <= longestFileName - (Int32)strlen(s); i++)
2327 + fprintf ( stderr, " " );
2331 +/*---------------------------------------------*/
2333 +void copyFileName ( Char* to, Char* from )
2335 + if ( strlen(from) > FILE_NAME_LEN-10 ) {
2338 + "bzip2: file name\n`%s'\n"
2339 + "is suspiciously (more than %d chars) long.\n"
2340 + "Try using a reasonable file name instead. Sorry! :-)\n",
2341 + from, FILE_NAME_LEN-10
2347 + strncpy(to,from,FILE_NAME_LEN-10);
2348 + to[FILE_NAME_LEN-10]='\0';
2352 +/*---------------------------------------------*/
2354 +Bool fileExists ( Char* name )
2356 + FILE *tmp = fopen ( name, "rb" );
2357 + Bool exists = (tmp != NULL);
2358 + if (tmp != NULL) fclose ( tmp );
2363 +/*---------------------------------------------*/
2364 +/* Open an output file safely with O_EXCL and good permissions.
2365 + This avoids a race condition in versions < 1.0.2, in which
2366 + the file was first opened and then had its interim permissions
2367 + set safely. We instead use open() to create the file with
2368 + the interim permissions required. (--- --- rw-).
2370 + For non-Unix platforms, if we are not worrying about
2371 + security issues, simple this simply behaves like fopen.
2374 +FILE* fopen_output_safely ( Char* name, const char* mode )
2379 + fh = open(name, O_WRONLY|O_CREAT|O_EXCL, S_IWUSR|S_IRUSR);
2380 + if (fh == -1) return NULL;
2381 + fp = fdopen(fh, mode);
2382 + if (fp == NULL) close(fh);
2385 + return fopen(name, mode);
2390 +/*---------------------------------------------*/
2392 + if in doubt, return True
2395 +Bool notAStandardFile ( Char* name )
2398 + struct MY_STAT statBuf;
2400 + i = MY_LSTAT ( name, &statBuf );
2401 + if (i != 0) return True;
2402 + if (MY_S_ISREG(statBuf.st_mode)) return False;
2407 +/*---------------------------------------------*/
2409 + rac 11/21/98 see if file has hard links to it
2412 +Int32 countHardLinks ( Char* name )
2415 + struct MY_STAT statBuf;
2417 + i = MY_LSTAT ( name, &statBuf );
2418 + if (i != 0) return 0;
2419 + return (statBuf.st_nlink - 1);
2423 +/*---------------------------------------------*/
2424 +/* Copy modification date, access date, permissions and owner from the
2425 + source to destination file. We have to copy this meta-info off
2426 + into fileMetaInfo before starting to compress / decompress it,
2427 + because doing it afterwards means we get the wrong access time.
2429 + To complicate matters, in compress() and decompress() below, the
2430 + sequence of tests preceding the call to saveInputFileMetaInfo()
2431 + involves calling fileExists(), which in turn establishes its result
2432 + by attempting to fopen() the file, and if successful, immediately
2433 + fclose()ing it again. So we have to assume that the fopen() call
2434 + does not cause the access time field to be updated.
2436 + Reading of the man page for stat() (man 2 stat) on RedHat 7.2 seems
2437 + to imply that merely doing open() will not affect the access time.
2438 + Therefore we merely need to hope that the C library only does
2439 + open() as a result of fopen(), and not any kind of read()-ahead
2442 + It sounds pretty fragile to me. Whether this carries across
2443 + robustly to arbitrary Unix-like platforms (or even works robustly
2444 + on this one, RedHat 7.2) is unknown to me. Nevertheless ...
2448 +struct MY_STAT fileMetaInfo;
2452 +void saveInputFileMetaInfo ( Char *srcName )
2456 + /* Note use of stat here, not lstat. */
2457 + retVal = MY_STAT( srcName, &fileMetaInfo );
2458 + ERROR_IF_NOT_ZERO ( retVal );
2464 +void applySavedTimeInfoToOutputFile ( Char *dstName )
2468 + struct utimbuf uTimBuf;
2470 + uTimBuf.actime = fileMetaInfo.st_atime;
2471 + uTimBuf.modtime = fileMetaInfo.st_mtime;
2473 + retVal = utime ( dstName, &uTimBuf );
2474 + ERROR_IF_NOT_ZERO ( retVal );
2479 +void applySavedFileAttrToOutputFile ( IntNative fd )
2484 + retVal = fchmod ( fd, fileMetaInfo.st_mode );
2485 + ERROR_IF_NOT_ZERO ( retVal );
2487 + (void) fchown ( fd, fileMetaInfo.st_uid, fileMetaInfo.st_gid );
2488 + /* chown() will in many cases return with EPERM, which can
2489 + be safely ignored.
2495 +/*---------------------------------------------*/
2497 +Bool containsDubiousChars ( Char* name )
2500 + /* On unix, files can contain any characters and the file expansion
2501 + * is performed by the shell.
2504 +# else /* ! BZ_UNIX */
2505 + /* On non-unix (Win* platforms), wildcard characters are not allowed in
2508 + for (; *name != '\0'; name++)
2509 + if (*name == '?' || *name == '*') return True;
2511 +# endif /* BZ_UNIX */
2515 +/*---------------------------------------------*/
2516 +#define BZ_N_SUFFIX_PAIRS 4
2518 +const Char* zSuffix[BZ_N_SUFFIX_PAIRS]
2519 + = { ".bz2", ".bz", ".tbz2", ".tbz" };
2520 +const Char* unzSuffix[BZ_N_SUFFIX_PAIRS]
2521 + = { "", "", ".tar", ".tar" };
2524 +Bool hasSuffix ( Char* s, const Char* suffix )
2526 + Int32 ns = strlen(s);
2527 + Int32 nx = strlen(suffix);
2528 + if (ns < nx) return False;
2529 + if (strcmp(s + ns - nx, suffix) == 0) return True;
2534 +Bool mapSuffix ( Char* name,
2535 + const Char* oldSuffix,
2536 + const Char* newSuffix )
2538 + if (!hasSuffix(name,oldSuffix)) return False;
2539 + name[strlen(name)-strlen(oldSuffix)] = 0;
2540 + strcat ( name, newSuffix );
2545 +/*---------------------------------------------*/
2547 +void compress ( Char *name )
2552 + struct MY_STAT statBuf;
2554 + deleteOutputOnInterrupt = False;
2556 + if (name == NULL && srcMode != SM_I2O)
2557 + panic ( "compress: bad modes\n" );
2559 + switch (srcMode) {
2561 + copyFileName ( inName, (Char*)"(stdin)" );
2562 + copyFileName ( outName, (Char*)"(stdout)" );
2565 + copyFileName ( inName, name );
2566 + copyFileName ( outName, name );
2567 + strcat ( outName, ".bz2" );
2570 + copyFileName ( inName, name );
2571 + copyFileName ( outName, (Char*)"(stdout)" );
2575 + if ( srcMode != SM_I2O && containsDubiousChars ( inName ) ) {
2577 + fprintf ( stderr, "%s: There are no files matching `%s'.\n",
2578 + progName, inName );
2582 + if ( srcMode != SM_I2O && !fileExists ( inName ) ) {
2583 + fprintf ( stderr, "%s: Can't open input file %s: %s.\n",
2584 + progName, inName, strerror(errno) );
2588 + for (i = 0; i < BZ_N_SUFFIX_PAIRS; i++) {
2589 + if (hasSuffix(inName, zSuffix[i])) {
2592 + "%s: Input file %s already has %s suffix.\n",
2593 + progName, inName, zSuffix[i] );
2598 + if ( srcMode == SM_F2F || srcMode == SM_F2O ) {
2599 + MY_STAT(inName, &statBuf);
2600 + if ( MY_S_ISDIR(statBuf.st_mode) ) {
2602 + "%s: Input file %s is a directory.\n",
2608 + if ( srcMode == SM_F2F && !forceOverwrite && notAStandardFile ( inName )) {
2610 + fprintf ( stderr, "%s: Input file %s is not a normal file.\n",
2611 + progName, inName );
2615 + if ( srcMode == SM_F2F && fileExists ( outName ) ) {
2616 + if (forceOverwrite) {
2619 + fprintf ( stderr, "%s: Output file %s already exists.\n",
2620 + progName, outName );
2625 + if ( srcMode == SM_F2F && !forceOverwrite &&
2626 + (n=countHardLinks ( inName )) > 0) {
2627 + fprintf ( stderr, "%s: Input file %s has %d other link%s.\n",
2628 + progName, inName, n, n > 1 ? "s" : "" );
2633 + if ( srcMode == SM_F2F ) {
2634 + /* Save the file's meta-info before we open it. Doing it later
2635 + means we mess up the access times. */
2636 + saveInputFileMetaInfo ( inName );
2639 + switch ( srcMode ) {
2644 + if ( isatty ( fileno ( stdout ) ) ) {
2646 + "%s: I won't write compressed data to a terminal.\n",
2648 + fprintf ( stderr, "%s: For help, type: `%s --help'.\n",
2649 + progName, progName );
2656 + inStr = fopen ( inName, "rb" );
2658 + if ( isatty ( fileno ( stdout ) ) ) {
2660 + "%s: I won't write compressed data to a terminal.\n",
2662 + fprintf ( stderr, "%s: For help, type: `%s --help'.\n",
2663 + progName, progName );
2664 + if ( inStr != NULL ) fclose ( inStr );
2668 + if ( inStr == NULL ) {
2669 + fprintf ( stderr, "%s: Can't open input file %s: %s.\n",
2670 + progName, inName, strerror(errno) );
2677 + inStr = fopen ( inName, "rb" );
2678 + outStr = fopen_output_safely ( outName, "wb" );
2679 + if ( outStr == NULL) {
2680 + fprintf ( stderr, "%s: Can't create output file %s: %s.\n",
2681 + progName, outName, strerror(errno) );
2682 + if ( inStr != NULL ) fclose ( inStr );
2686 + if ( inStr == NULL ) {
2687 + fprintf ( stderr, "%s: Can't open input file %s: %s.\n",
2688 + progName, inName, strerror(errno) );
2689 + if ( outStr != NULL ) fclose ( outStr );
2696 + panic ( "compress: bad srcMode" );
2700 + if (verbosity >= 1) {
2701 + fprintf ( stderr, " %s: ", inName );
2703 + fflush ( stderr );
2706 + /*--- Now the input and output handles are sane. Do the Biz. ---*/
2707 + outputHandleJustInCase = outStr;
2708 + deleteOutputOnInterrupt = True;
2709 + compressStream ( inStr, outStr );
2710 + outputHandleJustInCase = NULL;
2712 + /*--- If there was an I/O error, we won't get here. ---*/
2713 + if ( srcMode == SM_F2F ) {
2714 + applySavedTimeInfoToOutputFile ( outName );
2715 + deleteOutputOnInterrupt = False;
2716 + if ( !keepInputFiles ) {
2717 + IntNative retVal = remove ( inName );
2718 + ERROR_IF_NOT_ZERO ( retVal );
2722 + deleteOutputOnInterrupt = False;
2726 +/*---------------------------------------------*/
2728 +void uncompress ( Char *name )
2733 + Bool magicNumberOK;
2735 + struct MY_STAT statBuf;
2737 + deleteOutputOnInterrupt = False;
2739 + if (name == NULL && srcMode != SM_I2O)
2740 + panic ( "uncompress: bad modes\n" );
2742 + cantGuess = False;
2743 + switch (srcMode) {
2745 + copyFileName ( inName, (Char*)"(stdin)" );
2746 + copyFileName ( outName, (Char*)"(stdout)" );
2749 + copyFileName ( inName, name );
2750 + copyFileName ( outName, name );
2751 + for (i = 0; i < BZ_N_SUFFIX_PAIRS; i++)
2752 + if (mapSuffix(outName,zSuffix[i],unzSuffix[i]))
2755 + strcat ( outName, ".out" );
2758 + copyFileName ( inName, name );
2759 + copyFileName ( outName, (Char*)"(stdout)" );
2764 + if ( srcMode != SM_I2O && containsDubiousChars ( inName ) ) {
2766 + fprintf ( stderr, "%s: There are no files matching `%s'.\n",
2767 + progName, inName );
2771 + if ( srcMode != SM_I2O && !fileExists ( inName ) ) {
2772 + fprintf ( stderr, "%s: Can't open input file %s: %s.\n",
2773 + progName, inName, strerror(errno) );
2777 + if ( srcMode == SM_F2F || srcMode == SM_F2O ) {
2778 + MY_STAT(inName, &statBuf);
2779 + if ( MY_S_ISDIR(statBuf.st_mode) ) {
2781 + "%s: Input file %s is a directory.\n",
2787 + if ( srcMode == SM_F2F && !forceOverwrite && notAStandardFile ( inName )) {
2789 + fprintf ( stderr, "%s: Input file %s is not a normal file.\n",
2790 + progName, inName );
2794 + if ( /* srcMode == SM_F2F implied && */ cantGuess ) {
2797 + "%s: Can't guess original name for %s -- using %s\n",
2798 + progName, inName, outName );
2799 + /* just a warning, no return */
2801 + if ( srcMode == SM_F2F && fileExists ( outName ) ) {
2802 + if (forceOverwrite) {
2805 + fprintf ( stderr, "%s: Output file %s already exists.\n",
2806 + progName, outName );
2811 + if ( srcMode == SM_F2F && !forceOverwrite &&
2812 + (n=countHardLinks ( inName ) ) > 0) {
2813 + fprintf ( stderr, "%s: Input file %s has %d other link%s.\n",
2814 + progName, inName, n, n > 1 ? "s" : "" );
2819 + if ( srcMode == SM_F2F ) {
2820 + /* Save the file's meta-info before we open it. Doing it later
2821 + means we mess up the access times. */
2822 + saveInputFileMetaInfo ( inName );
2825 + switch ( srcMode ) {
2830 + if ( isatty ( fileno ( stdin ) ) ) {
2832 + "%s: I won't read compressed data from a terminal.\n",
2834 + fprintf ( stderr, "%s: For help, type: `%s --help'.\n",
2835 + progName, progName );
2842 + inStr = fopen ( inName, "rb" );
2844 + if ( inStr == NULL ) {
2845 + fprintf ( stderr, "%s: Can't open input file %s:%s.\n",
2846 + progName, inName, strerror(errno) );
2847 + if ( inStr != NULL ) fclose ( inStr );
2854 + inStr = fopen ( inName, "rb" );
2855 + outStr = fopen_output_safely ( outName, "wb" );
2856 + if ( outStr == NULL) {
2857 + fprintf ( stderr, "%s: Can't create output file %s: %s.\n",
2858 + progName, outName, strerror(errno) );
2859 + if ( inStr != NULL ) fclose ( inStr );
2863 + if ( inStr == NULL ) {
2864 + fprintf ( stderr, "%s: Can't open input file %s: %s.\n",
2865 + progName, inName, strerror(errno) );
2866 + if ( outStr != NULL ) fclose ( outStr );
2873 + panic ( "uncompress: bad srcMode" );
2877 + if (verbosity >= 1) {
2878 + fprintf ( stderr, " %s: ", inName );
2880 + fflush ( stderr );
2883 + /*--- Now the input and output handles are sane. Do the Biz. ---*/
2884 + outputHandleJustInCase = outStr;
2885 + deleteOutputOnInterrupt = True;
2886 + magicNumberOK = uncompressStream ( inStr, outStr );
2887 + outputHandleJustInCase = NULL;
2889 + /*--- If there was an I/O error, we won't get here. ---*/
2890 + if ( magicNumberOK ) {
2891 + if ( srcMode == SM_F2F ) {
2892 + applySavedTimeInfoToOutputFile ( outName );
2893 + deleteOutputOnInterrupt = False;
2894 + if ( !keepInputFiles ) {
2895 + IntNative retVal = remove ( inName );
2896 + ERROR_IF_NOT_ZERO ( retVal );
2900 + unzFailsExist = True;
2901 + deleteOutputOnInterrupt = False;
2902 + if ( srcMode == SM_F2F ) {
2903 + IntNative retVal = remove ( outName );
2904 + ERROR_IF_NOT_ZERO ( retVal );
2907 + deleteOutputOnInterrupt = False;
2909 + if ( magicNumberOK ) {
2910 + if (verbosity >= 1)
2911 + fprintf ( stderr, "done\n" );
2914 + if (verbosity >= 1)
2915 + fprintf ( stderr, "not a bzip2 file.\n" ); else
2917 + "%s: %s is not a bzip2 file.\n",
2918 + progName, inName );
2924 +/*---------------------------------------------*/
2926 +void testf ( Char *name )
2930 + struct MY_STAT statBuf;
2932 + deleteOutputOnInterrupt = False;
2934 + if (name == NULL && srcMode != SM_I2O)
2935 + panic ( "testf: bad modes\n" );
2937 + copyFileName ( outName, (Char*)"(none)" );
2938 + switch (srcMode) {
2939 + case SM_I2O: copyFileName ( inName, (Char*)"(stdin)" ); break;
2940 + case SM_F2F: copyFileName ( inName, name ); break;
2941 + case SM_F2O: copyFileName ( inName, name ); break;
2944 + if ( srcMode != SM_I2O && containsDubiousChars ( inName ) ) {
2946 + fprintf ( stderr, "%s: There are no files matching `%s'.\n",
2947 + progName, inName );
2951 + if ( srcMode != SM_I2O && !fileExists ( inName ) ) {
2952 + fprintf ( stderr, "%s: Can't open input %s: %s.\n",
2953 + progName, inName, strerror(errno) );
2957 + if ( srcMode != SM_I2O ) {
2958 + MY_STAT(inName, &statBuf);
2959 + if ( MY_S_ISDIR(statBuf.st_mode) ) {
2961 + "%s: Input file %s is a directory.\n",
2968 + switch ( srcMode ) {
2971 + if ( isatty ( fileno ( stdin ) ) ) {
2973 + "%s: I won't read compressed data from a terminal.\n",
2975 + fprintf ( stderr, "%s: For help, type: `%s --help'.\n",
2976 + progName, progName );
2983 + case SM_F2O: case SM_F2F:
2984 + inStr = fopen ( inName, "rb" );
2985 + if ( inStr == NULL ) {
2986 + fprintf ( stderr, "%s: Can't open input file %s:%s.\n",
2987 + progName, inName, strerror(errno) );
2994 + panic ( "testf: bad srcMode" );
2998 + if (verbosity >= 1) {
2999 + fprintf ( stderr, " %s: ", inName );
3001 + fflush ( stderr );
3004 + /*--- Now the input handle is sane. Do the Biz. ---*/
3005 + outputHandleJustInCase = NULL;
3006 + allOK = testStream ( inStr );
3008 + if (allOK && verbosity >= 1) fprintf ( stderr, "ok\n" );
3009 + if (!allOK) testFailsExist = True;
3013 +/*---------------------------------------------*/
3015 +void license ( void )
3019 + "bzip2, a block-sorting file compressor. "
3022 + " Copyright (C) 1996-2010 by Julian Seward.\n"
3024 + " This program is free software; you can redistribute it and/or modify\n"
3025 + " it under the terms set out in the LICENSE file, which is included\n"
3026 + " in the bzip2-1.0.6 source distribution.\n"
3028 + " This program is distributed in the hope that it will be useful,\n"
3029 + " but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
3030 + " MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
3031 + " LICENSE file for more details.\n"
3033 + BZ2_bzlibVersion()
3038 +/*---------------------------------------------*/
3040 +void usage ( Char *fullProgName )
3044 + "bzip2, a block-sorting file compressor. "
3046 + "\n usage: %s [flags and input files in any order]\n"
3048 + " -h --help print this message\n"
3049 + " -d --decompress force decompression\n"
3050 + " -z --compress force compression\n"
3051 + " -k --keep keep (don't delete) input files\n"
3052 + " -f --force overwrite existing output files\n"
3053 + " -t --test test compressed file integrity\n"
3054 + " -c --stdout output to standard out\n"
3055 + " -q --quiet suppress noncritical error messages\n"
3056 + " -v --verbose be verbose (a 2nd -v gives more)\n"
3057 + " -L --license display software version & license\n"
3058 + " -V --version display software version & license\n"
3059 + " -s --small use less memory (at most 2500k)\n"
3060 + " -1 .. -9 set block size to 100k .. 900k\n"
3061 + " --fast alias for -1\n"
3062 + " --best alias for -9\n"
3064 + " If invoked as `bzip2', default action is to compress.\n"
3065 + " as `bunzip2', default action is to decompress.\n"
3066 + " as `bzcat', default action is to decompress to stdout.\n"
3068 + " If no file names are given, bzip2 compresses or decompresses\n"
3069 + " from standard input to standard output. You can combine\n"
3070 + " short flags, so `-v -4' means the same as -v4 or -4v, &c.\n"
3076 + BZ2_bzlibVersion(),
3082 +/*---------------------------------------------*/
3084 +void redundant ( Char* flag )
3088 + "%s: %s is redundant in versions 0.9.5 and above\n",
3093 +/*---------------------------------------------*/
3095 + All the garbage from here to main() is purely to
3096 + implement a linked list of command-line arguments,
3097 + into which main() copies argv[1 .. argc-1].
3099 + The purpose of this exercise is to facilitate
3100 + the expansion of wildcard characters * and ? in
3101 + filenames for OSs which don't know how to do it
3102 + themselves, like MSDOS, Windows 95 and NT.
3104 + The actual Dirty Work is done by the platform-
3105 + specific macro APPEND_FILESPEC.
3111 + struct zzzz *link;
3116 +/*---------------------------------------------*/
3118 +void *myMalloc ( Int32 n )
3122 + p = malloc ( (size_t)n );
3123 + if (p == NULL) outOfMemory ();
3128 +/*---------------------------------------------*/
3130 +Cell *mkCell ( void )
3134 + c = (Cell*) myMalloc ( sizeof ( Cell ) );
3141 +/*---------------------------------------------*/
3143 +Cell *snocString ( Cell *root, Char *name )
3145 + if (root == NULL) {
3146 + Cell *tmp = mkCell();
3147 + tmp->name = (Char*) myMalloc ( 5 + strlen(name) );
3148 + strcpy ( tmp->name, name );
3152 + while (tmp->link != NULL) tmp = tmp->link;
3153 + tmp->link = snocString ( tmp->link, name );
3159 +/*---------------------------------------------*/
3161 +void addFlagsFromEnvVar ( Cell** argList, Char* varName )
3164 + Char *envbase, *p;
3166 + envbase = getenv(varName);
3167 + if (envbase != NULL) {
3171 + if (p[i] == 0) break;
3174 + while (isspace((Int32)(p[0]))) p++;
3175 + while (p[i] != 0 && !isspace((Int32)(p[i]))) i++;
3177 + k = i; if (k > FILE_NAME_LEN-10) k = FILE_NAME_LEN-10;
3178 + for (j = 0; j < k; j++) tmpName[j] = p[j];
3180 + APPEND_FLAG(*argList, tmpName);
3187 +/*---------------------------------------------*/
3188 +#define ISFLAG(s) (strcmp(aa->name, (s))==0)
3190 +IntNative main ( IntNative argc, Char *argv[] )
3198 + /*-- Be really really really paranoid :-) --*/
3199 + if (sizeof(Int32) != 4 || sizeof(UInt32) != 4 ||
3200 + sizeof(Int16) != 2 || sizeof(UInt16) != 2 ||
3201 + sizeof(Char) != 1 || sizeof(UChar) != 1)
3204 + /*-- Initialise --*/
3205 + outputHandleJustInCase = NULL;
3206 + smallMode = False;
3207 + keepInputFiles = False;
3208 + forceOverwrite = False;
3211 + blockSize100k = 9;
3212 + testFailsExist = False;
3213 + unzFailsExist = False;
3215 + numFilesProcessed = 0;
3217 + deleteOutputOnInterrupt = False;
3219 + i = j = 0; /* avoid bogus warning from egcs-1.1.X */
3221 + /*-- Set up signal handlers for mem access errors --*/
3222 + signal (SIGSEGV, mySIGSEGVorSIGBUScatcher);
3225 + signal (SIGBUS, mySIGSEGVorSIGBUScatcher);
3229 + copyFileName ( inName, (Char*)"(none)" );
3230 + copyFileName ( outName, (Char*)"(none)" );
3232 + copyFileName ( progNameReally, argv[0] );
3233 + progName = &progNameReally[0];
3234 + for (tmp = &progNameReally[0]; *tmp != '\0'; tmp++)
3235 + if (*tmp == PATH_SEP) progName = tmp + 1;
3238 + /*-- Copy flags from env var BZIP2, and
3239 + expand filename wildcards in arg list.
3242 + addFlagsFromEnvVar ( &argList, (Char*)"BZIP2" );
3243 + addFlagsFromEnvVar ( &argList, (Char*)"BZIP" );
3244 + for (i = 1; i <= argc-1; i++)
3245 + APPEND_FILESPEC(argList, argv[i]);
3248 + /*-- Find the length of the longest filename --*/
3249 + longestFileName = 7;
3252 + for (aa = argList; aa != NULL; aa = aa->link) {
3253 + if (ISFLAG("--")) { decode = False; continue; }
3254 + if (aa->name[0] == '-' && decode) continue;
3256 + if (longestFileName < (Int32)strlen(aa->name) )
3257 + longestFileName = (Int32)strlen(aa->name);
3261 + /*-- Determine source modes; flag handling may change this too. --*/
3262 + if (numFileNames == 0)
3263 + srcMode = SM_I2O; else srcMode = SM_F2F;
3266 + /*-- Determine what to do (compress/uncompress/test/cat). --*/
3267 + /*-- Note that subsequent flag handling may change this. --*/
3270 + if ( (strstr ( progName, "unzip" ) != 0) ||
3271 + (strstr ( progName, "UNZIP" ) != 0) )
3274 + if ( (strstr ( progName, "z2cat" ) != 0) ||
3275 + (strstr ( progName, "Z2CAT" ) != 0) ||
3276 + (strstr ( progName, "zcat" ) != 0) ||
3277 + (strstr ( progName, "ZCAT" ) != 0) ) {
3279 + srcMode = (numFileNames == 0) ? SM_I2O : SM_F2O;
3283 + /*-- Look at the flags. --*/
3284 + for (aa = argList; aa != NULL; aa = aa->link) {
3285 + if (ISFLAG("--")) break;
3286 + if (aa->name[0] == '-' && aa->name[1] != '-') {
3287 + for (j = 1; aa->name[j] != '\0'; j++) {
3288 + switch (aa->name[j]) {
3289 + case 'c': srcMode = SM_F2O; break;
3290 + case 'd': opMode = OM_UNZ; break;
3291 + case 'z': opMode = OM_Z; break;
3292 + case 'f': forceOverwrite = True; break;
3293 + case 't': opMode = OM_TEST; break;
3294 + case 'k': keepInputFiles = True; break;
3295 + case 's': smallMode = True; break;
3296 + case 'q': noisy = False; break;
3297 + case '1': blockSize100k = 1; break;
3298 + case '2': blockSize100k = 2; break;
3299 + case '3': blockSize100k = 3; break;
3300 + case '4': blockSize100k = 4; break;
3301 + case '5': blockSize100k = 5; break;
3302 + case '6': blockSize100k = 6; break;
3303 + case '7': blockSize100k = 7; break;
3304 + case '8': blockSize100k = 8; break;
3305 + case '9': blockSize100k = 9; break;
3307 + case 'L': license(); break;
3308 + case 'v': verbosity++; break;
3309 + case 'h': usage ( progName );
3312 + default: fprintf ( stderr, "%s: Bad flag `%s'\n",
3313 + progName, aa->name );
3314 + usage ( progName );
3322 + /*-- And again ... --*/
3323 + for (aa = argList; aa != NULL; aa = aa->link) {
3324 + if (ISFLAG("--")) break;
3325 + if (ISFLAG("--stdout")) srcMode = SM_F2O; else
3326 + if (ISFLAG("--decompress")) opMode = OM_UNZ; else
3327 + if (ISFLAG("--compress")) opMode = OM_Z; else
3328 + if (ISFLAG("--force")) forceOverwrite = True; else
3329 + if (ISFLAG("--test")) opMode = OM_TEST; else
3330 + if (ISFLAG("--keep")) keepInputFiles = True; else
3331 + if (ISFLAG("--small")) smallMode = True; else
3332 + if (ISFLAG("--quiet")) noisy = False; else
3333 + if (ISFLAG("--version")) license(); else
3334 + if (ISFLAG("--license")) license(); else
3335 + if (ISFLAG("--exponential")) workFactor = 1; else
3336 + if (ISFLAG("--repetitive-best")) redundant(aa->name); else
3337 + if (ISFLAG("--repetitive-fast")) redundant(aa->name); else
3338 + if (ISFLAG("--fast")) blockSize100k = 1; else
3339 + if (ISFLAG("--best")) blockSize100k = 9; else
3340 + if (ISFLAG("--verbose")) verbosity++; else
3341 + if (ISFLAG("--help")) { usage ( progName ); exit ( 0 ); }
3343 + if (strncmp ( aa->name, "--", 2) == 0) {
3344 + fprintf ( stderr, "%s: Bad flag `%s'\n", progName, aa->name );
3345 + usage ( progName );
3350 + if (verbosity > 4) verbosity = 4;
3351 + if (opMode == OM_Z && smallMode && blockSize100k > 2)
3352 + blockSize100k = 2;
3354 + if (opMode == OM_TEST && srcMode == SM_F2O) {
3355 + fprintf ( stderr, "%s: -c and -t cannot be used together.\n",
3360 + if (srcMode == SM_F2O && numFileNames == 0)
3363 + if (opMode != OM_Z) blockSize100k = 0;
3365 + if (srcMode == SM_F2F) {
3366 + signal (SIGINT, mySignalCatcher);
3367 + signal (SIGTERM, mySignalCatcher);
3369 + signal (SIGHUP, mySignalCatcher);
3373 + if (opMode == OM_Z) {
3374 + if (srcMode == SM_I2O) {
3375 + compress ( NULL );
3378 + for (aa = argList; aa != NULL; aa = aa->link) {
3379 + if (ISFLAG("--")) { decode = False; continue; }
3380 + if (aa->name[0] == '-' && decode) continue;
3381 + numFilesProcessed++;
3382 + compress ( aa->name );
3388 + if (opMode == OM_UNZ) {
3389 + unzFailsExist = False;
3390 + if (srcMode == SM_I2O) {
3391 + uncompress ( NULL );
3394 + for (aa = argList; aa != NULL; aa = aa->link) {
3395 + if (ISFLAG("--")) { decode = False; continue; }
3396 + if (aa->name[0] == '-' && decode) continue;
3397 + numFilesProcessed++;
3398 + uncompress ( aa->name );
3401 + if (unzFailsExist) {
3408 + testFailsExist = False;
3409 + if (srcMode == SM_I2O) {
3413 + for (aa = argList; aa != NULL; aa = aa->link) {
3414 + if (ISFLAG("--")) { decode = False; continue; }
3415 + if (aa->name[0] == '-' && decode) continue;
3416 + numFilesProcessed++;
3417 + testf ( aa->name );
3420 + if (testFailsExist && noisy) {
3423 + "You can use the `bzip2recover' program to attempt to recover\n"
3424 + "data from undamaged sections of corrupted files.\n\n"
3431 + /* Free the argument list memory to mollify leak detectors
3432 + (eg) Purify, Checker. Serves no other useful purpose.
3435 + while (aa != NULL) {
3436 + Cell* aa2 = aa->link;
3437 + if (aa->name != NULL) free(aa->name);
3446 +/*-----------------------------------------------------------*/
3447 +/*--- end bzip2.c ---*/
3448 +/*-----------------------------------------------------------*/
3449 diff --git a/pit/upstream-bzip2/bzlib.c b/pit/upstream-bzip2/bzlib.c
3450 new file mode 100644
3451 index 0000000..bd358a7
3453 +++ b/pit/upstream-bzip2/bzlib.c
3456 +/*-------------------------------------------------------------*/
3457 +/*--- Library top-level functions. ---*/
3458 +/*--- bzlib.c ---*/
3459 +/*-------------------------------------------------------------*/
3461 +/* ------------------------------------------------------------------
3462 + This file is part of bzip2/libbzip2, a program and library for
3463 + lossless, block-sorting data compression.
3465 + bzip2/libbzip2 version 1.0.6 of 6 September 2010
3466 + Copyright (C) 1996-2010 Julian Seward <jseward@bzip.org>
3468 + Please read the WARNING, DISCLAIMER and PATENTS sections in the
3471 + This program is released under the terms of the license contained
3472 + in the file LICENSE.
3473 + ------------------------------------------------------------------ */
3476 + 0.9.0 -- original version.
3477 + 0.9.0a/b -- no changes in this file.
3478 + 0.9.0c -- made zero-length BZ_FLUSH work correctly in bzCompress().
3479 + fixed bzWrite/bzRead to ignore zero-length requests.
3480 + fixed bzread to correctly handle read requests after EOF.
3481 + wrong parameter order in call to bzDecompressInit in
3482 + bzBuffToBuffDecompress. Fixed.
3485 +#include "bzlib_private.h"
3488 +/*---------------------------------------------------*/
3489 +/*--- Compression stuff ---*/
3490 +/*---------------------------------------------------*/
3493 +/*---------------------------------------------------*/
3494 +#ifndef BZ_NO_STDIO
3495 +void BZ2_bz__AssertH__fail ( int errcode )
3498 + "\n\nbzip2/libbzip2: internal error number %d.\n"
3499 + "This is a bug in bzip2/libbzip2, %s.\n"
3500 + "Please report it to me at: jseward@bzip.org. If this happened\n"
3501 + "when you were using some program which uses libbzip2 as a\n"
3502 + "component, you should also report this bug to the author(s)\n"
3503 + "of that program. Please make an effort to report this bug;\n"
3504 + "timely and accurate bug reports eventually lead to higher\n"
3505 + "quality software. Thanks. Julian Seward, 10 December 2007.\n\n",
3507 + BZ2_bzlibVersion()
3510 + if (errcode == 1007) {
3512 + "\n*** A special note about internal error number 1007 ***\n"
3514 + "Experience suggests that a common cause of i.e. 1007\n"
3515 + "is unreliable memory or other hardware. The 1007 assertion\n"
3516 + "just happens to cross-check the results of huge numbers of\n"
3517 + "memory reads/writes, and so acts (unintendedly) as a stress\n"
3518 + "test of your memory system.\n"
3520 + "I suggest the following: try compressing the file again,\n"
3521 + "possibly monitoring progress in detail with the -vv flag.\n"
3523 + "* If the error cannot be reproduced, and/or happens at different\n"
3524 + " points in compression, you may have a flaky memory system.\n"
3525 + " Try a memory-test program. I have used Memtest86\n"
3526 + " (www.memtest86.com). At the time of writing it is free (GPLd).\n"
3527 + " Memtest86 tests memory much more thorougly than your BIOSs\n"
3528 + " power-on test, and may find failures that the BIOS doesn't.\n"
3530 + "* If the error can be repeatably reproduced, this is a bug in\n"
3531 + " bzip2, and I would very much like to hear about it. Please\n"
3532 + " let me know, and, ideally, save a copy of the file causing the\n"
3533 + " problem -- without which I will be unable to investigate it.\n"
3543 +/*---------------------------------------------------*/
3545 +int bz_config_ok ( void )
3547 + if (sizeof(int) != 4) return 0;
3548 + if (sizeof(short) != 2) return 0;
3549 + if (sizeof(char) != 1) return 0;
3554 +/*---------------------------------------------------*/
3556 +void* default_bzalloc ( void* opaque, Int32 items, Int32 size )
3558 + void* v = malloc ( items * size );
3563 +void default_bzfree ( void* opaque, void* addr )
3565 + if (addr != NULL) free ( addr );
3569 +/*---------------------------------------------------*/
3571 +void prepare_new_block ( EState* s )
3576 + s->state_out_pos = 0;
3577 + BZ_INITIALISE_CRC ( s->blockCRC );
3578 + for (i = 0; i < 256; i++) s->inUse[i] = False;
3583 +/*---------------------------------------------------*/
3585 +void init_RL ( EState* s )
3587 + s->state_in_ch = 256;
3588 + s->state_in_len = 0;
3593 +Bool isempty_RL ( EState* s )
3595 + if (s->state_in_ch < 256 && s->state_in_len > 0)
3596 + return False; else
3601 +/*---------------------------------------------------*/
3602 +int BZ_API(BZ2_bzCompressInit)
3603 + ( bz_stream* strm,
3604 + int blockSize100k,
3611 + if (!bz_config_ok()) return BZ_CONFIG_ERROR;
3613 + if (strm == NULL ||
3614 + blockSize100k < 1 || blockSize100k > 9 ||
3615 + workFactor < 0 || workFactor > 250)
3616 + return BZ_PARAM_ERROR;
3618 + if (workFactor == 0) workFactor = 30;
3619 + if (strm->bzalloc == NULL) strm->bzalloc = default_bzalloc;
3620 + if (strm->bzfree == NULL) strm->bzfree = default_bzfree;
3622 + s = BZALLOC( sizeof(EState) );
3623 + if (s == NULL) return BZ_MEM_ERROR;
3630 + n = 100000 * blockSize100k;
3631 + s->arr1 = BZALLOC( n * sizeof(UInt32) );
3632 + s->arr2 = BZALLOC( (n+BZ_N_OVERSHOOT) * sizeof(UInt32) );
3633 + s->ftab = BZALLOC( 65537 * sizeof(UInt32) );
3635 + if (s->arr1 == NULL || s->arr2 == NULL || s->ftab == NULL) {
3636 + if (s->arr1 != NULL) BZFREE(s->arr1);
3637 + if (s->arr2 != NULL) BZFREE(s->arr2);
3638 + if (s->ftab != NULL) BZFREE(s->ftab);
3639 + if (s != NULL) BZFREE(s);
3640 + return BZ_MEM_ERROR;
3644 + s->state = BZ_S_INPUT;
3645 + s->mode = BZ_M_RUNNING;
3646 + s->combinedCRC = 0;
3647 + s->blockSize100k = blockSize100k;
3648 + s->nblockMAX = 100000 * blockSize100k - 19;
3649 + s->verbosity = verbosity;
3650 + s->workFactor = workFactor;
3652 + s->block = (UChar*)s->arr2;
3653 + s->mtfv = (UInt16*)s->arr1;
3655 + s->ptr = (UInt32*)s->arr1;
3658 + strm->total_in_lo32 = 0;
3659 + strm->total_in_hi32 = 0;
3660 + strm->total_out_lo32 = 0;
3661 + strm->total_out_hi32 = 0;
3663 + prepare_new_block ( s );
3668 +/*---------------------------------------------------*/
3670 +void add_pair_to_block ( EState* s )
3673 + UChar ch = (UChar)(s->state_in_ch);
3674 + for (i = 0; i < s->state_in_len; i++) {
3675 + BZ_UPDATE_CRC( s->blockCRC, ch );
3677 + s->inUse[s->state_in_ch] = True;
3678 + switch (s->state_in_len) {
3680 + s->block[s->nblock] = (UChar)ch; s->nblock++;
3683 + s->block[s->nblock] = (UChar)ch; s->nblock++;
3684 + s->block[s->nblock] = (UChar)ch; s->nblock++;
3687 + s->block[s->nblock] = (UChar)ch; s->nblock++;
3688 + s->block[s->nblock] = (UChar)ch; s->nblock++;
3689 + s->block[s->nblock] = (UChar)ch; s->nblock++;
3692 + s->inUse[s->state_in_len-4] = True;
3693 + s->block[s->nblock] = (UChar)ch; s->nblock++;
3694 + s->block[s->nblock] = (UChar)ch; s->nblock++;
3695 + s->block[s->nblock] = (UChar)ch; s->nblock++;
3696 + s->block[s->nblock] = (UChar)ch; s->nblock++;
3697 + s->block[s->nblock] = ((UChar)(s->state_in_len-4));
3704 +/*---------------------------------------------------*/
3706 +void flush_RL ( EState* s )
3708 + if (s->state_in_ch < 256) add_pair_to_block ( s );
3713 +/*---------------------------------------------------*/
3714 +#define ADD_CHAR_TO_BLOCK(zs,zchh0) \
3716 + UInt32 zchh = (UInt32)(zchh0); \
3717 + /*-- fast track the common case --*/ \
3718 + if (zchh != zs->state_in_ch && \
3719 + zs->state_in_len == 1) { \
3720 + UChar ch = (UChar)(zs->state_in_ch); \
3721 + BZ_UPDATE_CRC( zs->blockCRC, ch ); \
3722 + zs->inUse[zs->state_in_ch] = True; \
3723 + zs->block[zs->nblock] = (UChar)ch; \
3725 + zs->state_in_ch = zchh; \
3728 + /*-- general, uncommon cases --*/ \
3729 + if (zchh != zs->state_in_ch || \
3730 + zs->state_in_len == 255) { \
3731 + if (zs->state_in_ch < 256) \
3732 + add_pair_to_block ( zs ); \
3733 + zs->state_in_ch = zchh; \
3734 + zs->state_in_len = 1; \
3736 + zs->state_in_len++; \
3741 +/*---------------------------------------------------*/
3743 +Bool copy_input_until_stop ( EState* s )
3745 + Bool progress_in = False;
3747 + if (s->mode == BZ_M_RUNNING) {
3749 + /*-- fast track the common case --*/
3751 + /*-- block full? --*/
3752 + if (s->nblock >= s->nblockMAX) break;
3753 + /*-- no input? --*/
3754 + if (s->strm->avail_in == 0) break;
3755 + progress_in = True;
3756 + ADD_CHAR_TO_BLOCK ( s, (UInt32)(*((UChar*)(s->strm->next_in))) );
3757 + s->strm->next_in++;
3758 + s->strm->avail_in--;
3759 + s->strm->total_in_lo32++;
3760 + if (s->strm->total_in_lo32 == 0) s->strm->total_in_hi32++;
3765 + /*-- general, uncommon case --*/
3767 + /*-- block full? --*/
3768 + if (s->nblock >= s->nblockMAX) break;
3769 + /*-- no input? --*/
3770 + if (s->strm->avail_in == 0) break;
3771 + /*-- flush/finish end? --*/
3772 + if (s->avail_in_expect == 0) break;
3773 + progress_in = True;
3774 + ADD_CHAR_TO_BLOCK ( s, (UInt32)(*((UChar*)(s->strm->next_in))) );
3775 + s->strm->next_in++;
3776 + s->strm->avail_in--;
3777 + s->strm->total_in_lo32++;
3778 + if (s->strm->total_in_lo32 == 0) s->strm->total_in_hi32++;
3779 + s->avail_in_expect--;
3782 + return progress_in;
3786 +/*---------------------------------------------------*/
3788 +Bool copy_output_until_stop ( EState* s )
3790 + Bool progress_out = False;
3794 + /*-- no output space? --*/
3795 + if (s->strm->avail_out == 0) break;
3797 + /*-- block done? --*/
3798 + if (s->state_out_pos >= s->numZ) break;
3800 + progress_out = True;
3801 + *(s->strm->next_out) = s->zbits[s->state_out_pos];
3802 + s->state_out_pos++;
3803 + s->strm->avail_out--;
3804 + s->strm->next_out++;
3805 + s->strm->total_out_lo32++;
3806 + if (s->strm->total_out_lo32 == 0) s->strm->total_out_hi32++;
3809 + return progress_out;
3813 +/*---------------------------------------------------*/
3815 +Bool handle_compress ( bz_stream* strm )
3817 + Bool progress_in = False;
3818 + Bool progress_out = False;
3819 + EState* s = strm->state;
3823 + if (s->state == BZ_S_OUTPUT) {
3824 + progress_out |= copy_output_until_stop ( s );
3825 + if (s->state_out_pos < s->numZ) break;
3826 + if (s->mode == BZ_M_FINISHING &&
3827 + s->avail_in_expect == 0 &&
3828 + isempty_RL(s)) break;
3829 + prepare_new_block ( s );
3830 + s->state = BZ_S_INPUT;
3831 + if (s->mode == BZ_M_FLUSHING &&
3832 + s->avail_in_expect == 0 &&
3833 + isempty_RL(s)) break;
3836 + if (s->state == BZ_S_INPUT) {
3837 + progress_in |= copy_input_until_stop ( s );
3838 + if (s->mode != BZ_M_RUNNING && s->avail_in_expect == 0) {
3840 + BZ2_compressBlock ( s, (Bool)(s->mode == BZ_M_FINISHING) );
3841 + s->state = BZ_S_OUTPUT;
3844 + if (s->nblock >= s->nblockMAX) {
3845 + BZ2_compressBlock ( s, False );
3846 + s->state = BZ_S_OUTPUT;
3849 + if (s->strm->avail_in == 0) {
3856 + return progress_in || progress_out;
3860 +/*---------------------------------------------------*/
3861 +int BZ_API(BZ2_bzCompress) ( bz_stream *strm, int action )
3865 + if (strm == NULL) return BZ_PARAM_ERROR;
3867 + if (s == NULL) return BZ_PARAM_ERROR;
3868 + if (s->strm != strm) return BZ_PARAM_ERROR;
3871 + switch (s->mode) {
3874 + return BZ_SEQUENCE_ERROR;
3876 + case BZ_M_RUNNING:
3877 + if (action == BZ_RUN) {
3878 + progress = handle_compress ( strm );
3879 + return progress ? BZ_RUN_OK : BZ_PARAM_ERROR;
3882 + if (action == BZ_FLUSH) {
3883 + s->avail_in_expect = strm->avail_in;
3884 + s->mode = BZ_M_FLUSHING;
3888 + if (action == BZ_FINISH) {
3889 + s->avail_in_expect = strm->avail_in;
3890 + s->mode = BZ_M_FINISHING;
3894 + return BZ_PARAM_ERROR;
3896 + case BZ_M_FLUSHING:
3897 + if (action != BZ_FLUSH) return BZ_SEQUENCE_ERROR;
3898 + if (s->avail_in_expect != s->strm->avail_in)
3899 + return BZ_SEQUENCE_ERROR;
3900 + progress = handle_compress ( strm );
3901 + if (s->avail_in_expect > 0 || !isempty_RL(s) ||
3902 + s->state_out_pos < s->numZ) return BZ_FLUSH_OK;
3903 + s->mode = BZ_M_RUNNING;
3906 + case BZ_M_FINISHING:
3907 + if (action != BZ_FINISH) return BZ_SEQUENCE_ERROR;
3908 + if (s->avail_in_expect != s->strm->avail_in)
3909 + return BZ_SEQUENCE_ERROR;
3910 + progress = handle_compress ( strm );
3911 + if (!progress) return BZ_SEQUENCE_ERROR;
3912 + if (s->avail_in_expect > 0 || !isempty_RL(s) ||
3913 + s->state_out_pos < s->numZ) return BZ_FINISH_OK;
3914 + s->mode = BZ_M_IDLE;
3915 + return BZ_STREAM_END;
3917 + return BZ_OK; /*--not reached--*/
3921 +/*---------------------------------------------------*/
3922 +int BZ_API(BZ2_bzCompressEnd) ( bz_stream *strm )
3925 + if (strm == NULL) return BZ_PARAM_ERROR;
3927 + if (s == NULL) return BZ_PARAM_ERROR;
3928 + if (s->strm != strm) return BZ_PARAM_ERROR;
3930 + if (s->arr1 != NULL) BZFREE(s->arr1);
3931 + if (s->arr2 != NULL) BZFREE(s->arr2);
3932 + if (s->ftab != NULL) BZFREE(s->ftab);
3933 + BZFREE(strm->state);
3935 + strm->state = NULL;
3941 +/*---------------------------------------------------*/
3942 +/*--- Decompression stuff ---*/
3943 +/*---------------------------------------------------*/
3945 +/*---------------------------------------------------*/
3946 +int BZ_API(BZ2_bzDecompressInit)
3947 + ( bz_stream* strm,
3953 + if (!bz_config_ok()) return BZ_CONFIG_ERROR;
3955 + if (strm == NULL) return BZ_PARAM_ERROR;
3956 + if (small != 0 && small != 1) return BZ_PARAM_ERROR;
3957 + if (verbosity < 0 || verbosity > 4) return BZ_PARAM_ERROR;
3959 + if (strm->bzalloc == NULL) strm->bzalloc = default_bzalloc;
3960 + if (strm->bzfree == NULL) strm->bzfree = default_bzfree;
3962 + s = BZALLOC( sizeof(DState) );
3963 + if (s == NULL) return BZ_MEM_ERROR;
3966 + s->state = BZ_X_MAGIC_1;
3969 + s->calculatedCombinedCRC = 0;
3970 + strm->total_in_lo32 = 0;
3971 + strm->total_in_hi32 = 0;
3972 + strm->total_out_lo32 = 0;
3973 + strm->total_out_hi32 = 0;
3974 + s->smallDecompress = (Bool)small;
3978 + s->currBlockNo = 0;
3979 + s->verbosity = verbosity;
3985 +/*---------------------------------------------------*/
3986 +/* Return True iff data corruption is discovered.
3987 + Returns False if there is no problem.
3990 +Bool unRLE_obuf_to_output_FAST ( DState* s )
3994 + if (s->blockRandomised) {
3997 + /* try to finish existing run */
3999 + if (s->strm->avail_out == 0) return False;
4000 + if (s->state_out_len == 0) break;
4001 + *( (UChar*)(s->strm->next_out) ) = s->state_out_ch;
4002 + BZ_UPDATE_CRC ( s->calculatedBlockCRC, s->state_out_ch );
4003 + s->state_out_len--;
4004 + s->strm->next_out++;
4005 + s->strm->avail_out--;
4006 + s->strm->total_out_lo32++;
4007 + if (s->strm->total_out_lo32 == 0) s->strm->total_out_hi32++;
4010 + /* can a new run be started? */
4011 + if (s->nblock_used == s->save_nblock+1) return False;
4013 + /* Only caused by corrupt data stream? */
4014 + if (s->nblock_used > s->save_nblock+1)
4017 + s->state_out_len = 1;
4018 + s->state_out_ch = s->k0;
4019 + BZ_GET_FAST(k1); BZ_RAND_UPD_MASK;
4020 + k1 ^= BZ_RAND_MASK; s->nblock_used++;
4021 + if (s->nblock_used == s->save_nblock+1) continue;
4022 + if (k1 != s->k0) { s->k0 = k1; continue; };
4024 + s->state_out_len = 2;
4025 + BZ_GET_FAST(k1); BZ_RAND_UPD_MASK;
4026 + k1 ^= BZ_RAND_MASK; s->nblock_used++;
4027 + if (s->nblock_used == s->save_nblock+1) continue;
4028 + if (k1 != s->k0) { s->k0 = k1; continue; };
4030 + s->state_out_len = 3;
4031 + BZ_GET_FAST(k1); BZ_RAND_UPD_MASK;
4032 + k1 ^= BZ_RAND_MASK; s->nblock_used++;
4033 + if (s->nblock_used == s->save_nblock+1) continue;
4034 + if (k1 != s->k0) { s->k0 = k1; continue; };
4036 + BZ_GET_FAST(k1); BZ_RAND_UPD_MASK;
4037 + k1 ^= BZ_RAND_MASK; s->nblock_used++;
4038 + s->state_out_len = ((Int32)k1) + 4;
4039 + BZ_GET_FAST(s->k0); BZ_RAND_UPD_MASK;
4040 + s->k0 ^= BZ_RAND_MASK; s->nblock_used++;
4046 + UInt32 c_calculatedBlockCRC = s->calculatedBlockCRC;
4047 + UChar c_state_out_ch = s->state_out_ch;
4048 + Int32 c_state_out_len = s->state_out_len;
4049 + Int32 c_nblock_used = s->nblock_used;
4050 + Int32 c_k0 = s->k0;
4051 + UInt32* c_tt = s->tt;
4052 + UInt32 c_tPos = s->tPos;
4053 + char* cs_next_out = s->strm->next_out;
4054 + unsigned int cs_avail_out = s->strm->avail_out;
4055 + Int32 ro_blockSize100k = s->blockSize100k;
4058 + UInt32 avail_out_INIT = cs_avail_out;
4059 + Int32 s_save_nblockPP = s->save_nblock+1;
4060 + unsigned int total_out_lo32_old;
4064 + /* try to finish existing run */
4065 + if (c_state_out_len > 0) {
4067 + if (cs_avail_out == 0) goto return_notr;
4068 + if (c_state_out_len == 1) break;
4069 + *( (UChar*)(cs_next_out) ) = c_state_out_ch;
4070 + BZ_UPDATE_CRC ( c_calculatedBlockCRC, c_state_out_ch );
4071 + c_state_out_len--;
4075 + s_state_out_len_eq_one:
4077 + if (cs_avail_out == 0) {
4078 + c_state_out_len = 1; goto return_notr;
4080 + *( (UChar*)(cs_next_out) ) = c_state_out_ch;
4081 + BZ_UPDATE_CRC ( c_calculatedBlockCRC, c_state_out_ch );
4086 + /* Only caused by corrupt data stream? */
4087 + if (c_nblock_used > s_save_nblockPP)
4090 + /* can a new run be started? */
4091 + if (c_nblock_used == s_save_nblockPP) {
4092 + c_state_out_len = 0; goto return_notr;
4094 + c_state_out_ch = c_k0;
4095 + BZ_GET_FAST_C(k1); c_nblock_used++;
4097 + c_k0 = k1; goto s_state_out_len_eq_one;
4099 + if (c_nblock_used == s_save_nblockPP)
4100 + goto s_state_out_len_eq_one;
4102 + c_state_out_len = 2;
4103 + BZ_GET_FAST_C(k1); c_nblock_used++;
4104 + if (c_nblock_used == s_save_nblockPP) continue;
4105 + if (k1 != c_k0) { c_k0 = k1; continue; };
4107 + c_state_out_len = 3;
4108 + BZ_GET_FAST_C(k1); c_nblock_used++;
4109 + if (c_nblock_used == s_save_nblockPP) continue;
4110 + if (k1 != c_k0) { c_k0 = k1; continue; };
4112 + BZ_GET_FAST_C(k1); c_nblock_used++;
4113 + c_state_out_len = ((Int32)k1) + 4;
4114 + BZ_GET_FAST_C(c_k0); c_nblock_used++;
4118 + total_out_lo32_old = s->strm->total_out_lo32;
4119 + s->strm->total_out_lo32 += (avail_out_INIT - cs_avail_out);
4120 + if (s->strm->total_out_lo32 < total_out_lo32_old)
4121 + s->strm->total_out_hi32++;
4124 + s->calculatedBlockCRC = c_calculatedBlockCRC;
4125 + s->state_out_ch = c_state_out_ch;
4126 + s->state_out_len = c_state_out_len;
4127 + s->nblock_used = c_nblock_used;
4131 + s->strm->next_out = cs_next_out;
4132 + s->strm->avail_out = cs_avail_out;
4140 +/*---------------------------------------------------*/
4141 +__inline__ Int32 BZ2_indexIntoF ( Int32 indx, Int32 *cftab )
4143 + Int32 nb, na, mid;
4147 + mid = (nb + na) >> 1;
4148 + if (indx >= cftab[mid]) nb = mid; else na = mid;
4150 + while (na - nb != 1);
4155 +/*---------------------------------------------------*/
4156 +/* Return True iff data corruption is discovered.
4157 + Returns False if there is no problem.
4160 +Bool unRLE_obuf_to_output_SMALL ( DState* s )
4164 + if (s->blockRandomised) {
4167 + /* try to finish existing run */
4169 + if (s->strm->avail_out == 0) return False;
4170 + if (s->state_out_len == 0) break;
4171 + *( (UChar*)(s->strm->next_out) ) = s->state_out_ch;
4172 + BZ_UPDATE_CRC ( s->calculatedBlockCRC, s->state_out_ch );
4173 + s->state_out_len--;
4174 + s->strm->next_out++;
4175 + s->strm->avail_out--;
4176 + s->strm->total_out_lo32++;
4177 + if (s->strm->total_out_lo32 == 0) s->strm->total_out_hi32++;
4180 + /* can a new run be started? */
4181 + if (s->nblock_used == s->save_nblock+1) return False;
4183 + /* Only caused by corrupt data stream? */
4184 + if (s->nblock_used > s->save_nblock+1)
4187 + s->state_out_len = 1;
4188 + s->state_out_ch = s->k0;
4189 + BZ_GET_SMALL(k1); BZ_RAND_UPD_MASK;
4190 + k1 ^= BZ_RAND_MASK; s->nblock_used++;
4191 + if (s->nblock_used == s->save_nblock+1) continue;
4192 + if (k1 != s->k0) { s->k0 = k1; continue; };
4194 + s->state_out_len = 2;
4195 + BZ_GET_SMALL(k1); BZ_RAND_UPD_MASK;
4196 + k1 ^= BZ_RAND_MASK; s->nblock_used++;
4197 + if (s->nblock_used == s->save_nblock+1) continue;
4198 + if (k1 != s->k0) { s->k0 = k1; continue; };
4200 + s->state_out_len = 3;
4201 + BZ_GET_SMALL(k1); BZ_RAND_UPD_MASK;
4202 + k1 ^= BZ_RAND_MASK; s->nblock_used++;
4203 + if (s->nblock_used == s->save_nblock+1) continue;
4204 + if (k1 != s->k0) { s->k0 = k1; continue; };
4206 + BZ_GET_SMALL(k1); BZ_RAND_UPD_MASK;
4207 + k1 ^= BZ_RAND_MASK; s->nblock_used++;
4208 + s->state_out_len = ((Int32)k1) + 4;
4209 + BZ_GET_SMALL(s->k0); BZ_RAND_UPD_MASK;
4210 + s->k0 ^= BZ_RAND_MASK; s->nblock_used++;
4216 + /* try to finish existing run */
4218 + if (s->strm->avail_out == 0) return False;
4219 + if (s->state_out_len == 0) break;
4220 + *( (UChar*)(s->strm->next_out) ) = s->state_out_ch;
4221 + BZ_UPDATE_CRC ( s->calculatedBlockCRC, s->state_out_ch );
4222 + s->state_out_len--;
4223 + s->strm->next_out++;
4224 + s->strm->avail_out--;
4225 + s->strm->total_out_lo32++;
4226 + if (s->strm->total_out_lo32 == 0) s->strm->total_out_hi32++;
4229 + /* can a new run be started? */
4230 + if (s->nblock_used == s->save_nblock+1) return False;
4232 + /* Only caused by corrupt data stream? */
4233 + if (s->nblock_used > s->save_nblock+1)
4236 + s->state_out_len = 1;
4237 + s->state_out_ch = s->k0;
4238 + BZ_GET_SMALL(k1); s->nblock_used++;
4239 + if (s->nblock_used == s->save_nblock+1) continue;
4240 + if (k1 != s->k0) { s->k0 = k1; continue; };
4242 + s->state_out_len = 2;
4243 + BZ_GET_SMALL(k1); s->nblock_used++;
4244 + if (s->nblock_used == s->save_nblock+1) continue;
4245 + if (k1 != s->k0) { s->k0 = k1; continue; };
4247 + s->state_out_len = 3;
4248 + BZ_GET_SMALL(k1); s->nblock_used++;
4249 + if (s->nblock_used == s->save_nblock+1) continue;
4250 + if (k1 != s->k0) { s->k0 = k1; continue; };
4252 + BZ_GET_SMALL(k1); s->nblock_used++;
4253 + s->state_out_len = ((Int32)k1) + 4;
4254 + BZ_GET_SMALL(s->k0); s->nblock_used++;
4261 +/*---------------------------------------------------*/
4262 +int BZ_API(BZ2_bzDecompress) ( bz_stream *strm )
4266 + if (strm == NULL) return BZ_PARAM_ERROR;
4268 + if (s == NULL) return BZ_PARAM_ERROR;
4269 + if (s->strm != strm) return BZ_PARAM_ERROR;
4272 + if (s->state == BZ_X_IDLE) return BZ_SEQUENCE_ERROR;
4273 + if (s->state == BZ_X_OUTPUT) {
4274 + if (s->smallDecompress)
4275 + corrupt = unRLE_obuf_to_output_SMALL ( s ); else
4276 + corrupt = unRLE_obuf_to_output_FAST ( s );
4277 + if (corrupt) return BZ_DATA_ERROR;
4278 + if (s->nblock_used == s->save_nblock+1 && s->state_out_len == 0) {
4279 + BZ_FINALISE_CRC ( s->calculatedBlockCRC );
4280 + if (s->verbosity >= 3)
4281 + VPrintf2 ( " {0x%08x, 0x%08x}", s->storedBlockCRC,
4282 + s->calculatedBlockCRC );
4283 + if (s->verbosity >= 2) VPrintf0 ( "]" );
4284 + if (s->calculatedBlockCRC != s->storedBlockCRC)
4285 + return BZ_DATA_ERROR;
4286 + s->calculatedCombinedCRC
4287 + = (s->calculatedCombinedCRC << 1) |
4288 + (s->calculatedCombinedCRC >> 31);
4289 + s->calculatedCombinedCRC ^= s->calculatedBlockCRC;
4290 + s->state = BZ_X_BLKHDR_1;
4295 + if (s->state >= BZ_X_MAGIC_1) {
4296 + Int32 r = BZ2_decompress ( s );
4297 + if (r == BZ_STREAM_END) {
4298 + if (s->verbosity >= 3)
4299 + VPrintf2 ( "\n combined CRCs: stored = 0x%08x, computed = 0x%08x",
4300 + s->storedCombinedCRC, s->calculatedCombinedCRC );
4301 + if (s->calculatedCombinedCRC != s->storedCombinedCRC)
4302 + return BZ_DATA_ERROR;
4305 + if (s->state != BZ_X_OUTPUT) return r;
4309 + AssertH ( 0, 6001 );
4311 + return 0; /*NOTREACHED*/
4315 +/*---------------------------------------------------*/
4316 +int BZ_API(BZ2_bzDecompressEnd) ( bz_stream *strm )
4319 + if (strm == NULL) return BZ_PARAM_ERROR;
4321 + if (s == NULL) return BZ_PARAM_ERROR;
4322 + if (s->strm != strm) return BZ_PARAM_ERROR;
4324 + if (s->tt != NULL) BZFREE(s->tt);
4325 + if (s->ll16 != NULL) BZFREE(s->ll16);
4326 + if (s->ll4 != NULL) BZFREE(s->ll4);
4328 + BZFREE(strm->state);
4329 + strm->state = NULL;
4335 +#ifndef BZ_NO_STDIO
4336 +/*---------------------------------------------------*/
4337 +/*--- File I/O stuff ---*/
4338 +/*---------------------------------------------------*/
4340 +#define BZ_SETERR(eee) \
4342 + if (bzerror != NULL) *bzerror = eee; \
4343 + if (bzf != NULL) bzf->lastErr = eee; \
4349 + Char buf[BZ_MAX_UNUSED];
4354 + Bool initialisedOk;
4359 +/*---------------------------------------------*/
4360 +static Bool myfeof ( FILE* f )
4362 + Int32 c = fgetc ( f );
4363 + if (c == EOF) return True;
4369 +/*---------------------------------------------------*/
4370 +BZFILE* BZ_API(BZ2_bzWriteOpen)
4373 + int blockSize100k,
4378 + bzFile* bzf = NULL;
4383 + (blockSize100k < 1 || blockSize100k > 9) ||
4384 + (workFactor < 0 || workFactor > 250) ||
4385 + (verbosity < 0 || verbosity > 4))
4386 + { BZ_SETERR(BZ_PARAM_ERROR); return NULL; };
4389 + { BZ_SETERR(BZ_IO_ERROR); return NULL; };
4391 + bzf = malloc ( sizeof(bzFile) );
4393 + { BZ_SETERR(BZ_MEM_ERROR); return NULL; };
4396 + bzf->initialisedOk = False;
4399 + bzf->writing = True;
4400 + bzf->strm.bzalloc = NULL;
4401 + bzf->strm.bzfree = NULL;
4402 + bzf->strm.opaque = NULL;
4404 + if (workFactor == 0) workFactor = 30;
4405 + ret = BZ2_bzCompressInit ( &(bzf->strm), blockSize100k,
4406 + verbosity, workFactor );
4408 + { BZ_SETERR(ret); free(bzf); return NULL; };
4410 + bzf->strm.avail_in = 0;
4411 + bzf->initialisedOk = True;
4417 +/*---------------------------------------------------*/
4418 +void BZ_API(BZ2_bzWrite)
4425 + bzFile* bzf = (bzFile*)b;
4428 + if (bzf == NULL || buf == NULL || len < 0)
4429 + { BZ_SETERR(BZ_PARAM_ERROR); return; };
4430 + if (!(bzf->writing))
4431 + { BZ_SETERR(BZ_SEQUENCE_ERROR); return; };
4432 + if (ferror(bzf->handle))
4433 + { BZ_SETERR(BZ_IO_ERROR); return; };
4436 + { BZ_SETERR(BZ_OK); return; };
4438 + bzf->strm.avail_in = len;
4439 + bzf->strm.next_in = buf;
4442 + bzf->strm.avail_out = BZ_MAX_UNUSED;
4443 + bzf->strm.next_out = bzf->buf;
4444 + ret = BZ2_bzCompress ( &(bzf->strm), BZ_RUN );
4445 + if (ret != BZ_RUN_OK)
4446 + { BZ_SETERR(ret); return; };
4448 + if (bzf->strm.avail_out < BZ_MAX_UNUSED) {
4449 + n = BZ_MAX_UNUSED - bzf->strm.avail_out;
4450 + n2 = fwrite ( (void*)(bzf->buf), sizeof(UChar),
4452 + if (n != n2 || ferror(bzf->handle))
4453 + { BZ_SETERR(BZ_IO_ERROR); return; };
4456 + if (bzf->strm.avail_in == 0)
4457 + { BZ_SETERR(BZ_OK); return; };
4462 +/*---------------------------------------------------*/
4463 +void BZ_API(BZ2_bzWriteClose)
4467 + unsigned int* nbytes_in,
4468 + unsigned int* nbytes_out )
4470 + BZ2_bzWriteClose64 ( bzerror, b, abandon,
4471 + nbytes_in, NULL, nbytes_out, NULL );
4475 +void BZ_API(BZ2_bzWriteClose64)
4479 + unsigned int* nbytes_in_lo32,
4480 + unsigned int* nbytes_in_hi32,
4481 + unsigned int* nbytes_out_lo32,
4482 + unsigned int* nbytes_out_hi32 )
4485 + bzFile* bzf = (bzFile*)b;
4488 + { BZ_SETERR(BZ_OK); return; };
4489 + if (!(bzf->writing))
4490 + { BZ_SETERR(BZ_SEQUENCE_ERROR); return; };
4491 + if (ferror(bzf->handle))
4492 + { BZ_SETERR(BZ_IO_ERROR); return; };
4494 + if (nbytes_in_lo32 != NULL) *nbytes_in_lo32 = 0;
4495 + if (nbytes_in_hi32 != NULL) *nbytes_in_hi32 = 0;
4496 + if (nbytes_out_lo32 != NULL) *nbytes_out_lo32 = 0;
4497 + if (nbytes_out_hi32 != NULL) *nbytes_out_hi32 = 0;
4499 + if ((!abandon) && bzf->lastErr == BZ_OK) {
4501 + bzf->strm.avail_out = BZ_MAX_UNUSED;
4502 + bzf->strm.next_out = bzf->buf;
4503 + ret = BZ2_bzCompress ( &(bzf->strm), BZ_FINISH );
4504 + if (ret != BZ_FINISH_OK && ret != BZ_STREAM_END)
4505 + { BZ_SETERR(ret); return; };
4507 + if (bzf->strm.avail_out < BZ_MAX_UNUSED) {
4508 + n = BZ_MAX_UNUSED - bzf->strm.avail_out;
4509 + n2 = fwrite ( (void*)(bzf->buf), sizeof(UChar),
4511 + if (n != n2 || ferror(bzf->handle))
4512 + { BZ_SETERR(BZ_IO_ERROR); return; };
4515 + if (ret == BZ_STREAM_END) break;
4519 + if ( !abandon && !ferror ( bzf->handle ) ) {
4520 + fflush ( bzf->handle );
4521 + if (ferror(bzf->handle))
4522 + { BZ_SETERR(BZ_IO_ERROR); return; };
4525 + if (nbytes_in_lo32 != NULL)
4526 + *nbytes_in_lo32 = bzf->strm.total_in_lo32;
4527 + if (nbytes_in_hi32 != NULL)
4528 + *nbytes_in_hi32 = bzf->strm.total_in_hi32;
4529 + if (nbytes_out_lo32 != NULL)
4530 + *nbytes_out_lo32 = bzf->strm.total_out_lo32;
4531 + if (nbytes_out_hi32 != NULL)
4532 + *nbytes_out_hi32 = bzf->strm.total_out_hi32;
4535 + BZ2_bzCompressEnd ( &(bzf->strm) );
4540 +/*---------------------------------------------------*/
4541 +BZFILE* BZ_API(BZ2_bzReadOpen)
4549 + bzFile* bzf = NULL;
4555 + (small != 0 && small != 1) ||
4556 + (verbosity < 0 || verbosity > 4) ||
4557 + (unused == NULL && nUnused != 0) ||
4558 + (unused != NULL && (nUnused < 0 || nUnused > BZ_MAX_UNUSED)))
4559 + { BZ_SETERR(BZ_PARAM_ERROR); return NULL; };
4562 + { BZ_SETERR(BZ_IO_ERROR); return NULL; };
4564 + bzf = malloc ( sizeof(bzFile) );
4566 + { BZ_SETERR(BZ_MEM_ERROR); return NULL; };
4570 + bzf->initialisedOk = False;
4573 + bzf->writing = False;
4574 + bzf->strm.bzalloc = NULL;
4575 + bzf->strm.bzfree = NULL;
4576 + bzf->strm.opaque = NULL;
4578 + while (nUnused > 0) {
4579 + bzf->buf[bzf->bufN] = *((UChar*)(unused)); bzf->bufN++;
4580 + unused = ((void*)( 1 + ((UChar*)(unused)) ));
4584 + ret = BZ2_bzDecompressInit ( &(bzf->strm), verbosity, small );
4586 + { BZ_SETERR(ret); free(bzf); return NULL; };
4588 + bzf->strm.avail_in = bzf->bufN;
4589 + bzf->strm.next_in = bzf->buf;
4591 + bzf->initialisedOk = True;
4596 +/*---------------------------------------------------*/
4597 +void BZ_API(BZ2_bzReadClose) ( int *bzerror, BZFILE *b )
4599 + bzFile* bzf = (bzFile*)b;
4603 + { BZ_SETERR(BZ_OK); return; };
4606 + { BZ_SETERR(BZ_SEQUENCE_ERROR); return; };
4608 + if (bzf->initialisedOk)
4609 + (void)BZ2_bzDecompressEnd ( &(bzf->strm) );
4614 +/*---------------------------------------------------*/
4615 +int BZ_API(BZ2_bzRead)
4622 + bzFile* bzf = (bzFile*)b;
4626 + if (bzf == NULL || buf == NULL || len < 0)
4627 + { BZ_SETERR(BZ_PARAM_ERROR); return 0; };
4630 + { BZ_SETERR(BZ_SEQUENCE_ERROR); return 0; };
4633 + { BZ_SETERR(BZ_OK); return 0; };
4635 + bzf->strm.avail_out = len;
4636 + bzf->strm.next_out = buf;
4640 + if (ferror(bzf->handle))
4641 + { BZ_SETERR(BZ_IO_ERROR); return 0; };
4643 + if (bzf->strm.avail_in == 0 && !myfeof(bzf->handle)) {
4644 + n = fread ( bzf->buf, sizeof(UChar),
4645 + BZ_MAX_UNUSED, bzf->handle );
4646 + if (ferror(bzf->handle))
4647 + { BZ_SETERR(BZ_IO_ERROR); return 0; };
4649 + bzf->strm.avail_in = bzf->bufN;
4650 + bzf->strm.next_in = bzf->buf;
4653 + ret = BZ2_bzDecompress ( &(bzf->strm) );
4655 + if (ret != BZ_OK && ret != BZ_STREAM_END)
4656 + { BZ_SETERR(ret); return 0; };
4658 + if (ret == BZ_OK && myfeof(bzf->handle) &&
4659 + bzf->strm.avail_in == 0 && bzf->strm.avail_out > 0)
4660 + { BZ_SETERR(BZ_UNEXPECTED_EOF); return 0; };
4662 + if (ret == BZ_STREAM_END)
4663 + { BZ_SETERR(BZ_STREAM_END);
4664 + return len - bzf->strm.avail_out; };
4665 + if (bzf->strm.avail_out == 0)
4666 + { BZ_SETERR(BZ_OK); return len; };
4670 + return 0; /*not reached*/
4674 +/*---------------------------------------------------*/
4675 +void BZ_API(BZ2_bzReadGetUnused)
4681 + bzFile* bzf = (bzFile*)b;
4683 + { BZ_SETERR(BZ_PARAM_ERROR); return; };
4684 + if (bzf->lastErr != BZ_STREAM_END)
4685 + { BZ_SETERR(BZ_SEQUENCE_ERROR); return; };
4686 + if (unused == NULL || nUnused == NULL)
4687 + { BZ_SETERR(BZ_PARAM_ERROR); return; };
4690 + *nUnused = bzf->strm.avail_in;
4691 + *unused = bzf->strm.next_in;
4696 +/*---------------------------------------------------*/
4697 +/*--- Misc convenience stuff ---*/
4698 +/*---------------------------------------------------*/
4700 +/*---------------------------------------------------*/
4701 +int BZ_API(BZ2_bzBuffToBuffCompress)
4703 + unsigned int* destLen,
4705 + unsigned int sourceLen,
4706 + int blockSize100k,
4713 + if (dest == NULL || destLen == NULL ||
4715 + blockSize100k < 1 || blockSize100k > 9 ||
4716 + verbosity < 0 || verbosity > 4 ||
4717 + workFactor < 0 || workFactor > 250)
4718 + return BZ_PARAM_ERROR;
4720 + if (workFactor == 0) workFactor = 30;
4721 + strm.bzalloc = NULL;
4722 + strm.bzfree = NULL;
4723 + strm.opaque = NULL;
4724 + ret = BZ2_bzCompressInit ( &strm, blockSize100k,
4725 + verbosity, workFactor );
4726 + if (ret != BZ_OK) return ret;
4728 + strm.next_in = source;
4729 + strm.next_out = dest;
4730 + strm.avail_in = sourceLen;
4731 + strm.avail_out = *destLen;
4733 + ret = BZ2_bzCompress ( &strm, BZ_FINISH );
4734 + if (ret == BZ_FINISH_OK) goto output_overflow;
4735 + if (ret != BZ_STREAM_END) goto errhandler;
4737 + /* normal termination */
4738 + *destLen -= strm.avail_out;
4739 + BZ2_bzCompressEnd ( &strm );
4743 + BZ2_bzCompressEnd ( &strm );
4744 + return BZ_OUTBUFF_FULL;
4747 + BZ2_bzCompressEnd ( &strm );
4752 +/*---------------------------------------------------*/
4753 +int BZ_API(BZ2_bzBuffToBuffDecompress)
4755 + unsigned int* destLen,
4757 + unsigned int sourceLen,
4764 + if (dest == NULL || destLen == NULL ||
4766 + (small != 0 && small != 1) ||
4767 + verbosity < 0 || verbosity > 4)
4768 + return BZ_PARAM_ERROR;
4770 + strm.bzalloc = NULL;
4771 + strm.bzfree = NULL;
4772 + strm.opaque = NULL;
4773 + ret = BZ2_bzDecompressInit ( &strm, verbosity, small );
4774 + if (ret != BZ_OK) return ret;
4776 + strm.next_in = source;
4777 + strm.next_out = dest;
4778 + strm.avail_in = sourceLen;
4779 + strm.avail_out = *destLen;
4781 + ret = BZ2_bzDecompress ( &strm );
4782 + if (ret == BZ_OK) goto output_overflow_or_eof;
4783 + if (ret != BZ_STREAM_END) goto errhandler;
4785 + /* normal termination */
4786 + *destLen -= strm.avail_out;
4787 + BZ2_bzDecompressEnd ( &strm );
4790 + output_overflow_or_eof:
4791 + if (strm.avail_out > 0) {
4792 + BZ2_bzDecompressEnd ( &strm );
4793 + return BZ_UNEXPECTED_EOF;
4795 + BZ2_bzDecompressEnd ( &strm );
4796 + return BZ_OUTBUFF_FULL;
4800 + BZ2_bzDecompressEnd ( &strm );
4805 +/*---------------------------------------------------*/
4807 + Code contributed by Yoshioka Tsuneo (tsuneo@rr.iij4u.or.jp)
4808 + to support better zlib compatibility.
4809 + This code is not _officially_ part of libbzip2 (yet);
4810 + I haven't tested it, documented it, or considered the
4811 + threading-safeness of it.
4812 + If this code breaks, please contact both Yoshioka and me.
4814 +/*---------------------------------------------------*/
4816 +/*---------------------------------------------------*/
4818 + return version like "0.9.5d, 4-Sept-1999".
4820 +const char * BZ_API(BZ2_bzlibVersion)(void)
4822 + return BZ_VERSION;
4826 +#ifndef BZ_NO_STDIO
4827 +/*---------------------------------------------------*/
4829 +#if defined(_WIN32) || defined(OS2) || defined(MSDOS)
4830 +# include <fcntl.h>
4832 +# define SET_BINARY_MODE(file) setmode(fileno(file),O_BINARY)
4834 +# define SET_BINARY_MODE(file)
4837 +BZFILE * bzopen_or_bzdopen
4838 + ( const char *path, /* no use when bzdopen */
4839 + int fd, /* no use when bzdopen */
4841 + int open_mode) /* bzopen: 0, bzdopen:1 */
4844 + char unused[BZ_MAX_UNUSED];
4845 + int blockSize100k = 9;
4847 + char mode2[10] = "";
4849 + BZFILE *bzfp = NULL;
4850 + int verbosity = 0;
4851 + int workFactor = 30;
4852 + int smallMode = 0;
4855 + if (mode == NULL) return NULL;
4859 + writing = 0; break;
4861 + writing = 1; break;
4863 + smallMode = 1; break;
4865 + if (isdigit((int)(*mode))) {
4866 + blockSize100k = *mode-BZ_HDR_0;
4871 + strcat(mode2, writing ? "w" : "r" );
4872 + strcat(mode2,"b"); /* binary mode */
4874 + if (open_mode==0) {
4875 + if (path==NULL || strcmp(path,"")==0) {
4876 + fp = (writing ? stdout : stdin);
4877 + SET_BINARY_MODE(fp);
4879 + fp = fopen(path,mode2);
4882 +#ifdef BZ_STRICT_ANSI
4885 + fp = fdopen(fd,mode2);
4888 + if (fp == NULL) return NULL;
4891 + /* Guard against total chaos and anarchy -- JRS */
4892 + if (blockSize100k < 1) blockSize100k = 1;
4893 + if (blockSize100k > 9) blockSize100k = 9;
4894 + bzfp = BZ2_bzWriteOpen(&bzerr,fp,blockSize100k,
4895 + verbosity,workFactor);
4897 + bzfp = BZ2_bzReadOpen(&bzerr,fp,verbosity,smallMode,
4900 + if (bzfp == NULL) {
4901 + if (fp != stdin && fp != stdout) fclose(fp);
4908 +/*---------------------------------------------------*/
4910 + open file for read or write.
4911 + ex) bzopen("file","w9")
4912 + case path="" or NULL => use stdin or stdout.
4914 +BZFILE * BZ_API(BZ2_bzopen)
4915 + ( const char *path,
4916 + const char *mode )
4918 + return bzopen_or_bzdopen(path,-1,mode,/*bzopen*/0);
4922 +/*---------------------------------------------------*/
4923 +BZFILE * BZ_API(BZ2_bzdopen)
4925 + const char *mode )
4927 + return bzopen_or_bzdopen(NULL,fd,mode,/*bzdopen*/1);
4931 +/*---------------------------------------------------*/
4932 +int BZ_API(BZ2_bzread) (BZFILE* b, void* buf, int len )
4935 + if (((bzFile*)b)->lastErr == BZ_STREAM_END) return 0;
4936 + nread = BZ2_bzRead(&bzerr,b,buf,len);
4937 + if (bzerr == BZ_OK || bzerr == BZ_STREAM_END) {
4945 +/*---------------------------------------------------*/
4946 +int BZ_API(BZ2_bzwrite) (BZFILE* b, void* buf, int len )
4950 + BZ2_bzWrite(&bzerr,b,buf,len);
4951 + if(bzerr == BZ_OK){
4959 +/*---------------------------------------------------*/
4960 +int BZ_API(BZ2_bzflush) (BZFILE *b)
4962 + /* do nothing now... */
4967 +/*---------------------------------------------------*/
4968 +void BZ_API(BZ2_bzclose) (BZFILE* b)
4973 + if (b==NULL) {return;}
4974 + fp = ((bzFile *)b)->handle;
4975 + if(((bzFile*)b)->writing){
4976 + BZ2_bzWriteClose(&bzerr,b,0,NULL,NULL);
4977 + if(bzerr != BZ_OK){
4978 + BZ2_bzWriteClose(NULL,b,1,NULL,NULL);
4981 + BZ2_bzReadClose(&bzerr,b);
4983 + if(fp!=stdin && fp!=stdout){
4989 +/*---------------------------------------------------*/
4991 + return last error code
4993 +static const char *bzerrorstrings[] = {
4999 + ,"DATA_ERROR_MAGIC"
5004 + ,"???" /* for future */
5005 + ,"???" /* for future */
5006 + ,"???" /* for future */
5007 + ,"???" /* for future */
5008 + ,"???" /* for future */
5009 + ,"???" /* for future */
5013 +const char * BZ_API(BZ2_bzerror) (BZFILE *b, int *errnum)
5015 + int err = ((bzFile *)b)->lastErr;
5017 + if(err>0) err = 0;
5019 + return bzerrorstrings[err*-1];
5024 +/*-------------------------------------------------------------*/
5025 +/*--- end bzlib.c ---*/
5026 +/*-------------------------------------------------------------*/
5027 diff --git a/pit/upstream-bzip2/bzlib.h b/pit/upstream-bzip2/bzlib.h
5028 new file mode 100644
5029 index 0000000..8277123
5031 +++ b/pit/upstream-bzip2/bzlib.h
5034 +/*-------------------------------------------------------------*/
5035 +/*--- Public header file for the library. ---*/
5036 +/*--- bzlib.h ---*/
5037 +/*-------------------------------------------------------------*/
5039 +/* ------------------------------------------------------------------
5040 + This file is part of bzip2/libbzip2, a program and library for
5041 + lossless, block-sorting data compression.
5043 + bzip2/libbzip2 version 1.0.6 of 6 September 2010
5044 + Copyright (C) 1996-2010 Julian Seward <jseward@bzip.org>
5046 + Please read the WARNING, DISCLAIMER and PATENTS sections in the
5049 + This program is released under the terms of the license contained
5050 + in the file LICENSE.
5051 + ------------------------------------------------------------------ */
5063 +#define BZ_FINISH 2
5066 +#define BZ_RUN_OK 1
5067 +#define BZ_FLUSH_OK 2
5068 +#define BZ_FINISH_OK 3
5069 +#define BZ_STREAM_END 4
5070 +#define BZ_SEQUENCE_ERROR (-1)
5071 +#define BZ_PARAM_ERROR (-2)
5072 +#define BZ_MEM_ERROR (-3)
5073 +#define BZ_DATA_ERROR (-4)
5074 +#define BZ_DATA_ERROR_MAGIC (-5)
5075 +#define BZ_IO_ERROR (-6)
5076 +#define BZ_UNEXPECTED_EOF (-7)
5077 +#define BZ_OUTBUFF_FULL (-8)
5078 +#define BZ_CONFIG_ERROR (-9)
5083 + unsigned int avail_in;
5084 + unsigned int total_in_lo32;
5085 + unsigned int total_in_hi32;
5088 + unsigned int avail_out;
5089 + unsigned int total_out_lo32;
5090 + unsigned int total_out_hi32;
5094 + void *(*bzalloc)(void *,int,int);
5095 + void (*bzfree)(void *,void *);
5105 +#ifndef BZ_NO_STDIO
5106 +/* Need a definitition for FILE */
5111 +# include <windows.h>
5113 + /* windows.h define small to char */
5117 +# define BZ_API(func) WINAPI func
5118 +# define BZ_EXTERN extern
5120 + /* import windows dll dynamically */
5121 +# define BZ_API(func) (WINAPI * func)
5125 +# define BZ_API(func) func
5126 +# define BZ_EXTERN extern
5130 +/*-- Core (low-level) library functions --*/
5132 +BZ_EXTERN int BZ_API(BZ2_bzCompressInit) (
5134 + int blockSize100k,
5139 +BZ_EXTERN int BZ_API(BZ2_bzCompress) (
5144 +BZ_EXTERN int BZ_API(BZ2_bzCompressEnd) (
5148 +BZ_EXTERN int BZ_API(BZ2_bzDecompressInit) (
5154 +BZ_EXTERN int BZ_API(BZ2_bzDecompress) (
5158 +BZ_EXTERN int BZ_API(BZ2_bzDecompressEnd) (
5164 +/*-- High(er) level library functions --*/
5166 +#ifndef BZ_NO_STDIO
5167 +#define BZ_MAX_UNUSED 5000
5169 +typedef void BZFILE;
5171 +BZ_EXTERN BZFILE* BZ_API(BZ2_bzReadOpen) (
5180 +BZ_EXTERN void BZ_API(BZ2_bzReadClose) (
5185 +BZ_EXTERN void BZ_API(BZ2_bzReadGetUnused) (
5192 +BZ_EXTERN int BZ_API(BZ2_bzRead) (
5199 +BZ_EXTERN BZFILE* BZ_API(BZ2_bzWriteOpen) (
5202 + int blockSize100k,
5207 +BZ_EXTERN void BZ_API(BZ2_bzWrite) (
5214 +BZ_EXTERN void BZ_API(BZ2_bzWriteClose) (
5218 + unsigned int* nbytes_in,
5219 + unsigned int* nbytes_out
5222 +BZ_EXTERN void BZ_API(BZ2_bzWriteClose64) (
5226 + unsigned int* nbytes_in_lo32,
5227 + unsigned int* nbytes_in_hi32,
5228 + unsigned int* nbytes_out_lo32,
5229 + unsigned int* nbytes_out_hi32
5234 +/*-- Utility functions --*/
5236 +BZ_EXTERN int BZ_API(BZ2_bzBuffToBuffCompress) (
5238 + unsigned int* destLen,
5240 + unsigned int sourceLen,
5241 + int blockSize100k,
5246 +BZ_EXTERN int BZ_API(BZ2_bzBuffToBuffDecompress) (
5248 + unsigned int* destLen,
5250 + unsigned int sourceLen,
5257 + Code contributed by Yoshioka Tsuneo (tsuneo@rr.iij4u.or.jp)
5258 + to support better zlib compatibility.
5259 + This code is not _officially_ part of libbzip2 (yet);
5260 + I haven't tested it, documented it, or considered the
5261 + threading-safeness of it.
5262 + If this code breaks, please contact both Yoshioka and me.
5265 +BZ_EXTERN const char * BZ_API(BZ2_bzlibVersion) (
5269 +#ifndef BZ_NO_STDIO
5270 +BZ_EXTERN BZFILE * BZ_API(BZ2_bzopen) (
5275 +BZ_EXTERN BZFILE * BZ_API(BZ2_bzdopen) (
5280 +BZ_EXTERN int BZ_API(BZ2_bzread) (
5286 +BZ_EXTERN int BZ_API(BZ2_bzwrite) (
5292 +BZ_EXTERN int BZ_API(BZ2_bzflush) (
5296 +BZ_EXTERN void BZ_API(BZ2_bzclose) (
5300 +BZ_EXTERN const char * BZ_API(BZ2_bzerror) (
5312 +/*-------------------------------------------------------------*/
5313 +/*--- end bzlib.h ---*/
5314 +/*-------------------------------------------------------------*/
5315 diff --git a/pit/upstream-bzip2/bzlib_private.h b/pit/upstream-bzip2/bzlib_private.h
5316 new file mode 100644
5317 index 0000000..5d0217f
5319 +++ b/pit/upstream-bzip2/bzlib_private.h
5322 +/*-------------------------------------------------------------*/
5323 +/*--- Private header file for the library. ---*/
5324 +/*--- bzlib_private.h ---*/
5325 +/*-------------------------------------------------------------*/
5327 +/* ------------------------------------------------------------------
5328 + This file is part of bzip2/libbzip2, a program and library for
5329 + lossless, block-sorting data compression.
5331 + bzip2/libbzip2 version 1.0.6 of 6 September 2010
5332 + Copyright (C) 1996-2010 Julian Seward <jseward@bzip.org>
5334 + Please read the WARNING, DISCLAIMER and PATENTS sections in the
5337 + This program is released under the terms of the license contained
5338 + in the file LICENSE.
5339 + ------------------------------------------------------------------ */
5342 +#ifndef _BZLIB_PRIVATE_H
5343 +#define _BZLIB_PRIVATE_H
5345 +#include <stdlib.h>
5347 +#ifndef BZ_NO_STDIO
5350 +#include <string.h>
5357 +/*-- General stuff. --*/
5359 +#define BZ_VERSION "1.0.6, 6-Sept-2010"
5362 +typedef unsigned char Bool;
5363 +typedef unsigned char UChar;
5365 +typedef unsigned int UInt32;
5366 +typedef short Int16;
5367 +typedef unsigned short UInt16;
5369 +#define True ((Bool)1)
5370 +#define False ((Bool)0)
5373 +#define __inline__ /* */
5376 +#ifndef BZ_NO_STDIO
5378 +extern void BZ2_bz__AssertH__fail ( int errcode );
5379 +#define AssertH(cond,errcode) \
5380 + { if (!(cond)) BZ2_bz__AssertH__fail ( errcode ); }
5383 +#define AssertD(cond,msg) \
5384 + { if (!(cond)) { \
5385 + fprintf ( stderr, \
5386 + "\n\nlibbzip2(debug build): internal error\n\t%s\n", msg );\
5390 +#define AssertD(cond,msg) /* */
5393 +#define VPrintf0(zf) \
5394 + fprintf(stderr,zf)
5395 +#define VPrintf1(zf,za1) \
5396 + fprintf(stderr,zf,za1)
5397 +#define VPrintf2(zf,za1,za2) \
5398 + fprintf(stderr,zf,za1,za2)
5399 +#define VPrintf3(zf,za1,za2,za3) \
5400 + fprintf(stderr,zf,za1,za2,za3)
5401 +#define VPrintf4(zf,za1,za2,za3,za4) \
5402 + fprintf(stderr,zf,za1,za2,za3,za4)
5403 +#define VPrintf5(zf,za1,za2,za3,za4,za5) \
5404 + fprintf(stderr,zf,za1,za2,za3,za4,za5)
5408 +extern void bz_internal_error ( int errcode );
5409 +#define AssertH(cond,errcode) \
5410 + { if (!(cond)) bz_internal_error ( errcode ); }
5411 +#define AssertD(cond,msg) do { } while (0)
5412 +#define VPrintf0(zf) do { } while (0)
5413 +#define VPrintf1(zf,za1) do { } while (0)
5414 +#define VPrintf2(zf,za1,za2) do { } while (0)
5415 +#define VPrintf3(zf,za1,za2,za3) do { } while (0)
5416 +#define VPrintf4(zf,za1,za2,za3,za4) do { } while (0)
5417 +#define VPrintf5(zf,za1,za2,za3,za4,za5) do { } while (0)
5422 +#define BZALLOC(nnn) (strm->bzalloc)(strm->opaque,(nnn),1)
5423 +#define BZFREE(ppp) (strm->bzfree)(strm->opaque,(ppp))
5426 +/*-- Header bytes. --*/
5428 +#define BZ_HDR_B 0x42 /* 'B' */
5429 +#define BZ_HDR_Z 0x5a /* 'Z' */
5430 +#define BZ_HDR_h 0x68 /* 'h' */
5431 +#define BZ_HDR_0 0x30 /* '0' */
5433 +/*-- Constants for the back end. --*/
5435 +#define BZ_MAX_ALPHA_SIZE 258
5436 +#define BZ_MAX_CODE_LEN 23
5441 +#define BZ_N_GROUPS 6
5442 +#define BZ_G_SIZE 50
5443 +#define BZ_N_ITERS 4
5445 +#define BZ_MAX_SELECTORS (2 + (900000 / BZ_G_SIZE))
5449 +/*-- Stuff for randomising repetitive blocks. --*/
5451 +extern Int32 BZ2_rNums[512];
5453 +#define BZ_RAND_DECLS \
5457 +#define BZ_RAND_INIT_MASK \
5461 +#define BZ_RAND_MASK ((s->rNToGo == 1) ? 1 : 0)
5463 +#define BZ_RAND_UPD_MASK \
5464 + if (s->rNToGo == 0) { \
5465 + s->rNToGo = BZ2_rNums[s->rTPos]; \
5467 + if (s->rTPos == 512) s->rTPos = 0; \
5473 +/*-- Stuff for doing CRCs. --*/
5475 +extern UInt32 BZ2_crc32Table[256];
5477 +#define BZ_INITIALISE_CRC(crcVar) \
5479 + crcVar = 0xffffffffL; \
5482 +#define BZ_FINALISE_CRC(crcVar) \
5484 + crcVar = ~(crcVar); \
5487 +#define BZ_UPDATE_CRC(crcVar,cha) \
5489 + crcVar = (crcVar << 8) ^ \
5490 + BZ2_crc32Table[(crcVar >> 24) ^ \
5496 +/*-- States and modes for compression. --*/
5498 +#define BZ_M_IDLE 1
5499 +#define BZ_M_RUNNING 2
5500 +#define BZ_M_FLUSHING 3
5501 +#define BZ_M_FINISHING 4
5503 +#define BZ_S_OUTPUT 1
5504 +#define BZ_S_INPUT 2
5506 +#define BZ_N_RADIX 2
5507 +#define BZ_N_QSORT 12
5508 +#define BZ_N_SHELL 18
5509 +#define BZ_N_OVERSHOOT (BZ_N_RADIX + BZ_N_QSORT + BZ_N_SHELL + 2)
5514 +/*-- Structure holding all the compression-side stuff. --*/
5518 + /* pointer back to the struct bz_stream */
5521 + /* mode this stream is in, and whether inputting */
5522 + /* or outputting data */
5526 + /* remembers avail_in when flush/finish requested */
5527 + UInt32 avail_in_expect;
5529 + /* for doing the block sorting */
5535 + /* aliases for arr1 and arr2 */
5541 + /* for deciding when to use the fallback sorting algorithm */
5544 + /* run-length-encoding of the input */
5545 + UInt32 state_in_ch;
5546 + Int32 state_in_len;
5549 + /* input and output limits and current posns */
5553 + Int32 state_out_pos;
5555 + /* map of bytes used in block */
5558 + UChar unseqToSeq[256];
5560 + /* the buffer for bit stream creation */
5564 + /* block and combined CRCs */
5566 + UInt32 combinedCRC;
5568 + /* misc administratium */
5571 + Int32 blockSize100k;
5573 + /* stuff for coding the MTF values */
5575 + Int32 mtfFreq [BZ_MAX_ALPHA_SIZE];
5576 + UChar selector [BZ_MAX_SELECTORS];
5577 + UChar selectorMtf[BZ_MAX_SELECTORS];
5579 + UChar len [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
5580 + Int32 code [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
5581 + Int32 rfreq [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
5582 + /* second dimension: only 3 needed; 4 makes index calculations faster */
5583 + UInt32 len_pack[BZ_MAX_ALPHA_SIZE][4];
5590 +/*-- externs for compression. --*/
5593 +BZ2_blockSort ( EState* );
5596 +BZ2_compressBlock ( EState*, Bool );
5599 +BZ2_bsInitWrite ( EState* );
5602 +BZ2_hbAssignCodes ( Int32*, UChar*, Int32, Int32, Int32 );
5605 +BZ2_hbMakeCodeLengths ( UChar*, Int32*, Int32, Int32 );
5609 +/*-- states for decompression. --*/
5611 +#define BZ_X_IDLE 1
5612 +#define BZ_X_OUTPUT 2
5614 +#define BZ_X_MAGIC_1 10
5615 +#define BZ_X_MAGIC_2 11
5616 +#define BZ_X_MAGIC_3 12
5617 +#define BZ_X_MAGIC_4 13
5618 +#define BZ_X_BLKHDR_1 14
5619 +#define BZ_X_BLKHDR_2 15
5620 +#define BZ_X_BLKHDR_3 16
5621 +#define BZ_X_BLKHDR_4 17
5622 +#define BZ_X_BLKHDR_5 18
5623 +#define BZ_X_BLKHDR_6 19
5624 +#define BZ_X_BCRC_1 20
5625 +#define BZ_X_BCRC_2 21
5626 +#define BZ_X_BCRC_3 22
5627 +#define BZ_X_BCRC_4 23
5628 +#define BZ_X_RANDBIT 24
5629 +#define BZ_X_ORIGPTR_1 25
5630 +#define BZ_X_ORIGPTR_2 26
5631 +#define BZ_X_ORIGPTR_3 27
5632 +#define BZ_X_MAPPING_1 28
5633 +#define BZ_X_MAPPING_2 29
5634 +#define BZ_X_SELECTOR_1 30
5635 +#define BZ_X_SELECTOR_2 31
5636 +#define BZ_X_SELECTOR_3 32
5637 +#define BZ_X_CODING_1 33
5638 +#define BZ_X_CODING_2 34
5639 +#define BZ_X_CODING_3 35
5640 +#define BZ_X_MTF_1 36
5641 +#define BZ_X_MTF_2 37
5642 +#define BZ_X_MTF_3 38
5643 +#define BZ_X_MTF_4 39
5644 +#define BZ_X_MTF_5 40
5645 +#define BZ_X_MTF_6 41
5646 +#define BZ_X_ENDHDR_2 42
5647 +#define BZ_X_ENDHDR_3 43
5648 +#define BZ_X_ENDHDR_4 44
5649 +#define BZ_X_ENDHDR_5 45
5650 +#define BZ_X_ENDHDR_6 46
5651 +#define BZ_X_CCRC_1 47
5652 +#define BZ_X_CCRC_2 48
5653 +#define BZ_X_CCRC_3 49
5654 +#define BZ_X_CCRC_4 50
5658 +/*-- Constants for the fast MTF decoder. --*/
5660 +#define MTFA_SIZE 4096
5661 +#define MTFL_SIZE 16
5665 +/*-- Structure holding all the decompression-side stuff. --*/
5669 + /* pointer back to the struct bz_stream */
5672 + /* state indicator for this stream */
5675 + /* for doing the final run-length decoding */
5676 + UChar state_out_ch;
5677 + Int32 state_out_len;
5678 + Bool blockRandomised;
5681 + /* the buffer for bit stream reading */
5685 + /* misc administratium */
5686 + Int32 blockSize100k;
5687 + Bool smallDecompress;
5688 + Int32 currBlockNo;
5691 + /* for undoing the Burrows-Wheeler transform */
5695 + Int32 unzftab[256];
5696 + Int32 nblock_used;
5698 + Int32 cftabCopy[257];
5700 + /* for undoing the Burrows-Wheeler transform (FAST) */
5703 + /* for undoing the Burrows-Wheeler transform (SMALL) */
5707 + /* stored and calculated CRCs */
5708 + UInt32 storedBlockCRC;
5709 + UInt32 storedCombinedCRC;
5710 + UInt32 calculatedBlockCRC;
5711 + UInt32 calculatedCombinedCRC;
5713 + /* map of bytes used in block */
5717 + UChar seqToUnseq[256];
5719 + /* for decoding the MTF values */
5720 + UChar mtfa [MTFA_SIZE];
5721 + Int32 mtfbase[256 / MTFL_SIZE];
5722 + UChar selector [BZ_MAX_SELECTORS];
5723 + UChar selectorMtf[BZ_MAX_SELECTORS];
5724 + UChar len [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
5726 + Int32 limit [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
5727 + Int32 base [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
5728 + Int32 perm [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
5729 + Int32 minLens[BZ_N_GROUPS];
5731 + /* save area for scalars in the main decompress code */
5735 + Int32 save_alphaSize;
5736 + Int32 save_nGroups;
5737 + Int32 save_nSelectors;
5739 + Int32 save_groupNo;
5740 + Int32 save_groupPos;
5741 + Int32 save_nextSym;
5742 + Int32 save_nblockMAX;
5743 + Int32 save_nblock;
5752 + Int32 save_gMinlen;
5753 + Int32* save_gLimit;
5754 + Int32* save_gBase;
5755 + Int32* save_gPerm;
5762 +/*-- Macros for decompression. --*/
5764 +#define BZ_GET_FAST(cccc) \
5765 + /* c_tPos is unsigned, hence test < 0 is pointless. */ \
5766 + if (s->tPos >= (UInt32)100000 * (UInt32)s->blockSize100k) return True; \
5767 + s->tPos = s->tt[s->tPos]; \
5768 + cccc = (UChar)(s->tPos & 0xff); \
5771 +#define BZ_GET_FAST_C(cccc) \
5772 + /* c_tPos is unsigned, hence test < 0 is pointless. */ \
5773 + if (c_tPos >= (UInt32)100000 * (UInt32)ro_blockSize100k) return True; \
5774 + c_tPos = c_tt[c_tPos]; \
5775 + cccc = (UChar)(c_tPos & 0xff); \
5778 +#define SET_LL4(i,n) \
5779 + { if (((i) & 0x1) == 0) \
5780 + s->ll4[(i) >> 1] = (s->ll4[(i) >> 1] & 0xf0) | (n); else \
5781 + s->ll4[(i) >> 1] = (s->ll4[(i) >> 1] & 0x0f) | ((n) << 4); \
5784 +#define GET_LL4(i) \
5785 + ((((UInt32)(s->ll4[(i) >> 1])) >> (((i) << 2) & 0x4)) & 0xF)
5787 +#define SET_LL(i,n) \
5788 + { s->ll16[i] = (UInt16)(n & 0x0000ffff); \
5789 + SET_LL4(i, n >> 16); \
5792 +#define GET_LL(i) \
5793 + (((UInt32)s->ll16[i]) | (GET_LL4(i) << 16))
5795 +#define BZ_GET_SMALL(cccc) \
5796 + /* c_tPos is unsigned, hence test < 0 is pointless. */ \
5797 + if (s->tPos >= (UInt32)100000 * (UInt32)s->blockSize100k) return True; \
5798 + cccc = BZ2_indexIntoF ( s->tPos, s->cftab ); \
5799 + s->tPos = GET_LL(s->tPos);
5802 +/*-- externs for decompression. --*/
5805 +BZ2_indexIntoF ( Int32, Int32* );
5808 +BZ2_decompress ( DState* );
5811 +BZ2_hbCreateDecodeTables ( Int32*, Int32*, Int32*, UChar*,
5812 + Int32, Int32, Int32 );
5818 +/*-- BZ_NO_STDIO seems to make NULL disappear on some platforms. --*/
5827 +/*-------------------------------------------------------------*/
5828 +/*--- end bzlib_private.h ---*/
5829 +/*-------------------------------------------------------------*/
5830 diff --git a/pit/upstream-bzip2/compress.c b/pit/upstream-bzip2/compress.c
5831 new file mode 100644
5832 index 0000000..caf7696
5834 +++ b/pit/upstream-bzip2/compress.c
5837 +/*-------------------------------------------------------------*/
5838 +/*--- Compression machinery (not incl block sorting) ---*/
5839 +/*--- compress.c ---*/
5840 +/*-------------------------------------------------------------*/
5842 +/* ------------------------------------------------------------------
5843 + This file is part of bzip2/libbzip2, a program and library for
5844 + lossless, block-sorting data compression.
5846 + bzip2/libbzip2 version 1.0.6 of 6 September 2010
5847 + Copyright (C) 1996-2010 Julian Seward <jseward@bzip.org>
5849 + Please read the WARNING, DISCLAIMER and PATENTS sections in the
5852 + This program is released under the terms of the license contained
5853 + in the file LICENSE.
5854 + ------------------------------------------------------------------ */
5858 + 0.9.0 -- original version.
5859 + 0.9.0a/b -- no changes in this file.
5860 + 0.9.0c -- changed setting of nGroups in sendMTFValues()
5861 + so as to do a bit better on small files
5864 +#include "bzlib_private.h"
5867 +/*---------------------------------------------------*/
5868 +/*--- Bit stream I/O ---*/
5869 +/*---------------------------------------------------*/
5871 +/*---------------------------------------------------*/
5872 +void BZ2_bsInitWrite ( EState* s )
5879 +/*---------------------------------------------------*/
5881 +void bsFinishWrite ( EState* s )
5883 + while (s->bsLive > 0) {
5884 + s->zbits[s->numZ] = (UChar)(s->bsBuff >> 24);
5892 +/*---------------------------------------------------*/
5893 +#define bsNEEDW(nz) \
5895 + while (s->bsLive >= 8) { \
5896 + s->zbits[s->numZ] \
5897 + = (UChar)(s->bsBuff >> 24); \
5899 + s->bsBuff <<= 8; \
5905 +/*---------------------------------------------------*/
5908 +void bsW ( EState* s, Int32 n, UInt32 v )
5911 + s->bsBuff |= (v << (32 - s->bsLive - n));
5916 +/*---------------------------------------------------*/
5918 +void bsPutUInt32 ( EState* s, UInt32 u )
5920 + bsW ( s, 8, (u >> 24) & 0xffL );
5921 + bsW ( s, 8, (u >> 16) & 0xffL );
5922 + bsW ( s, 8, (u >> 8) & 0xffL );
5923 + bsW ( s, 8, u & 0xffL );
5927 +/*---------------------------------------------------*/
5929 +void bsPutUChar ( EState* s, UChar c )
5931 + bsW( s, 8, (UInt32)c );
5935 +/*---------------------------------------------------*/
5936 +/*--- The back end proper ---*/
5937 +/*---------------------------------------------------*/
5939 +/*---------------------------------------------------*/
5941 +void makeMaps_e ( EState* s )
5945 + for (i = 0; i < 256; i++)
5946 + if (s->inUse[i]) {
5947 + s->unseqToSeq[i] = s->nInUse;
5953 +/*---------------------------------------------------*/
5955 +void generateMTFValues ( EState* s )
5964 + After sorting (eg, here),
5965 + s->arr1 [ 0 .. s->nblock-1 ] holds sorted order,
5967 + ((UChar*)s->arr2) [ 0 .. s->nblock-1 ]
5968 + holds the original block data.
5970 + The first thing to do is generate the MTF values,
5972 + ((UInt16*)s->arr1) [ 0 .. s->nblock-1 ].
5973 + Because there are strictly fewer or equal MTF values
5974 + than block values, ptr values in this area are overwritten
5975 + with MTF values only when they are no longer needed.
5977 + The final compressed bitstream is generated into the
5979 + (UChar*) (&((UChar*)s->arr2)[s->nblock])
5981 + These storage aliases are set up in bzCompressInit(),
5982 + except for the last one, which is arranged in
5985 + UInt32* ptr = s->ptr;
5986 + UChar* block = s->block;
5987 + UInt16* mtfv = s->mtfv;
5990 + EOB = s->nInUse+1;
5992 + for (i = 0; i <= EOB; i++) s->mtfFreq[i] = 0;
5996 + for (i = 0; i < s->nInUse; i++) yy[i] = (UChar) i;
5998 + for (i = 0; i < s->nblock; i++) {
6000 + AssertD ( wr <= i, "generateMTFValues(1)" );
6001 + j = ptr[i]-1; if (j < 0) j += s->nblock;
6002 + ll_i = s->unseqToSeq[block[j]];
6003 + AssertD ( ll_i < s->nInUse, "generateMTFValues(2a)" );
6005 + if (yy[0] == ll_i) {
6013 + mtfv[wr] = BZ_RUNB; wr++;
6014 + s->mtfFreq[BZ_RUNB]++;
6016 + mtfv[wr] = BZ_RUNA; wr++;
6017 + s->mtfFreq[BZ_RUNA]++;
6019 + if (zPend < 2) break;
6020 + zPend = (zPend - 2) / 2;
6025 + register UChar rtmp;
6026 + register UChar* ryy_j;
6027 + register UChar rll_i;
6032 + while ( rll_i != rtmp ) {
6033 + register UChar rtmp2;
6040 + j = ryy_j - &(yy[0]);
6041 + mtfv[wr] = j+1; wr++; s->mtfFreq[j+1]++;
6051 + mtfv[wr] = BZ_RUNB; wr++;
6052 + s->mtfFreq[BZ_RUNB]++;
6054 + mtfv[wr] = BZ_RUNA; wr++;
6055 + s->mtfFreq[BZ_RUNA]++;
6057 + if (zPend < 2) break;
6058 + zPend = (zPend - 2) / 2;
6063 + mtfv[wr] = EOB; wr++; s->mtfFreq[EOB]++;
6069 +/*---------------------------------------------------*/
6070 +#define BZ_LESSER_ICOST 0
6071 +#define BZ_GREATER_ICOST 15
6074 +void sendMTFValues ( EState* s )
6076 + Int32 v, t, i, j, gs, ge, totc, bt, bc, iter;
6077 + Int32 nSelectors, alphaSize, minLen, maxLen, selCtr;
6078 + Int32 nGroups, nBytes;
6081 + UChar len [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
6082 + is a global since the decoder also needs it.
6084 + Int32 code[BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
6085 + Int32 rfreq[BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
6086 + are also globals only used in this proc.
6087 + Made global to keep stack frame size small.
6091 + UInt16 cost[BZ_N_GROUPS];
6092 + Int32 fave[BZ_N_GROUPS];
6094 + UInt16* mtfv = s->mtfv;
6096 + if (s->verbosity >= 3)
6097 + VPrintf3( " %d in block, %d after MTF & 1-2 coding, "
6098 + "%d+2 syms in use\n",
6099 + s->nblock, s->nMTF, s->nInUse );
6101 + alphaSize = s->nInUse+2;
6102 + for (t = 0; t < BZ_N_GROUPS; t++)
6103 + for (v = 0; v < alphaSize; v++)
6104 + s->len[t][v] = BZ_GREATER_ICOST;
6106 + /*--- Decide how many coding tables to use ---*/
6107 + AssertH ( s->nMTF > 0, 3001 );
6108 + if (s->nMTF < 200) nGroups = 2; else
6109 + if (s->nMTF < 600) nGroups = 3; else
6110 + if (s->nMTF < 1200) nGroups = 4; else
6111 + if (s->nMTF < 2400) nGroups = 5; else
6114 + /*--- Generate an initial set of coding tables ---*/
6116 + Int32 nPart, remF, tFreq, aFreq;
6121 + while (nPart > 0) {
6122 + tFreq = remF / nPart;
6125 + while (aFreq < tFreq && ge < alphaSize-1) {
6127 + aFreq += s->mtfFreq[ge];
6131 + && nPart != nGroups && nPart != 1
6132 + && ((nGroups-nPart) % 2 == 1)) {
6133 + aFreq -= s->mtfFreq[ge];
6137 + if (s->verbosity >= 3)
6138 + VPrintf5( " initial group %d, [%d .. %d], "
6139 + "has %d syms (%4.1f%%)\n",
6140 + nPart, gs, ge, aFreq,
6141 + (100.0 * (float)aFreq) / (float)(s->nMTF) );
6143 + for (v = 0; v < alphaSize; v++)
6144 + if (v >= gs && v <= ge)
6145 + s->len[nPart-1][v] = BZ_LESSER_ICOST; else
6146 + s->len[nPart-1][v] = BZ_GREATER_ICOST;
6155 + Iterate up to BZ_N_ITERS times to improve the tables.
6157 + for (iter = 0; iter < BZ_N_ITERS; iter++) {
6159 + for (t = 0; t < nGroups; t++) fave[t] = 0;
6161 + for (t = 0; t < nGroups; t++)
6162 + for (v = 0; v < alphaSize; v++)
6163 + s->rfreq[t][v] = 0;
6166 + Set up an auxiliary length table which is used to fast-track
6167 + the common case (nGroups == 6).
6169 + if (nGroups == 6) {
6170 + for (v = 0; v < alphaSize; v++) {
6171 + s->len_pack[v][0] = (s->len[1][v] << 16) | s->len[0][v];
6172 + s->len_pack[v][1] = (s->len[3][v] << 16) | s->len[2][v];
6173 + s->len_pack[v][2] = (s->len[5][v] << 16) | s->len[4][v];
6182 + /*--- Set group start & end marks. --*/
6183 + if (gs >= s->nMTF) break;
6184 + ge = gs + BZ_G_SIZE - 1;
6185 + if (ge >= s->nMTF) ge = s->nMTF-1;
6188 + Calculate the cost of this group as coded
6189 + by each of the coding tables.
6191 + for (t = 0; t < nGroups; t++) cost[t] = 0;
6193 + if (nGroups == 6 && 50 == ge-gs+1) {
6194 + /*--- fast track the common case ---*/
6195 + register UInt32 cost01, cost23, cost45;
6196 + register UInt16 icv;
6197 + cost01 = cost23 = cost45 = 0;
6199 +# define BZ_ITER(nn) \
6200 + icv = mtfv[gs+(nn)]; \
6201 + cost01 += s->len_pack[icv][0]; \
6202 + cost23 += s->len_pack[icv][1]; \
6203 + cost45 += s->len_pack[icv][2]; \
6205 + BZ_ITER(0); BZ_ITER(1); BZ_ITER(2); BZ_ITER(3); BZ_ITER(4);
6206 + BZ_ITER(5); BZ_ITER(6); BZ_ITER(7); BZ_ITER(8); BZ_ITER(9);
6207 + BZ_ITER(10); BZ_ITER(11); BZ_ITER(12); BZ_ITER(13); BZ_ITER(14);
6208 + BZ_ITER(15); BZ_ITER(16); BZ_ITER(17); BZ_ITER(18); BZ_ITER(19);
6209 + BZ_ITER(20); BZ_ITER(21); BZ_ITER(22); BZ_ITER(23); BZ_ITER(24);
6210 + BZ_ITER(25); BZ_ITER(26); BZ_ITER(27); BZ_ITER(28); BZ_ITER(29);
6211 + BZ_ITER(30); BZ_ITER(31); BZ_ITER(32); BZ_ITER(33); BZ_ITER(34);
6212 + BZ_ITER(35); BZ_ITER(36); BZ_ITER(37); BZ_ITER(38); BZ_ITER(39);
6213 + BZ_ITER(40); BZ_ITER(41); BZ_ITER(42); BZ_ITER(43); BZ_ITER(44);
6214 + BZ_ITER(45); BZ_ITER(46); BZ_ITER(47); BZ_ITER(48); BZ_ITER(49);
6218 + cost[0] = cost01 & 0xffff; cost[1] = cost01 >> 16;
6219 + cost[2] = cost23 & 0xffff; cost[3] = cost23 >> 16;
6220 + cost[4] = cost45 & 0xffff; cost[5] = cost45 >> 16;
6223 + /*--- slow version which correctly handles all situations ---*/
6224 + for (i = gs; i <= ge; i++) {
6225 + UInt16 icv = mtfv[i];
6226 + for (t = 0; t < nGroups; t++) cost[t] += s->len[t][icv];
6231 + Find the coding table which is best for this group,
6232 + and record its identity in the selector table.
6234 + bc = 999999999; bt = -1;
6235 + for (t = 0; t < nGroups; t++)
6236 + if (cost[t] < bc) { bc = cost[t]; bt = t; };
6239 + s->selector[nSelectors] = bt;
6243 + Increment the symbol frequencies for the selected table.
6245 + if (nGroups == 6 && 50 == ge-gs+1) {
6246 + /*--- fast track the common case ---*/
6248 +# define BZ_ITUR(nn) s->rfreq[bt][ mtfv[gs+(nn)] ]++
6250 + BZ_ITUR(0); BZ_ITUR(1); BZ_ITUR(2); BZ_ITUR(3); BZ_ITUR(4);
6251 + BZ_ITUR(5); BZ_ITUR(6); BZ_ITUR(7); BZ_ITUR(8); BZ_ITUR(9);
6252 + BZ_ITUR(10); BZ_ITUR(11); BZ_ITUR(12); BZ_ITUR(13); BZ_ITUR(14);
6253 + BZ_ITUR(15); BZ_ITUR(16); BZ_ITUR(17); BZ_ITUR(18); BZ_ITUR(19);
6254 + BZ_ITUR(20); BZ_ITUR(21); BZ_ITUR(22); BZ_ITUR(23); BZ_ITUR(24);
6255 + BZ_ITUR(25); BZ_ITUR(26); BZ_ITUR(27); BZ_ITUR(28); BZ_ITUR(29);
6256 + BZ_ITUR(30); BZ_ITUR(31); BZ_ITUR(32); BZ_ITUR(33); BZ_ITUR(34);
6257 + BZ_ITUR(35); BZ_ITUR(36); BZ_ITUR(37); BZ_ITUR(38); BZ_ITUR(39);
6258 + BZ_ITUR(40); BZ_ITUR(41); BZ_ITUR(42); BZ_ITUR(43); BZ_ITUR(44);
6259 + BZ_ITUR(45); BZ_ITUR(46); BZ_ITUR(47); BZ_ITUR(48); BZ_ITUR(49);
6264 + /*--- slow version which correctly handles all situations ---*/
6265 + for (i = gs; i <= ge; i++)
6266 + s->rfreq[bt][ mtfv[i] ]++;
6271 + if (s->verbosity >= 3) {
6272 + VPrintf2 ( " pass %d: size is %d, grp uses are ",
6274 + for (t = 0; t < nGroups; t++)
6275 + VPrintf1 ( "%d ", fave[t] );
6276 + VPrintf0 ( "\n" );
6280 + Recompute the tables based on the accumulated frequencies.
6282 + /* maxLen was changed from 20 to 17 in bzip2-1.0.3. See
6283 + comment in huffman.c for details. */
6284 + for (t = 0; t < nGroups; t++)
6285 + BZ2_hbMakeCodeLengths ( &(s->len[t][0]), &(s->rfreq[t][0]),
6286 + alphaSize, 17 /*20*/ );
6290 + AssertH( nGroups < 8, 3002 );
6291 + AssertH( nSelectors < 32768 &&
6292 + nSelectors <= (2 + (900000 / BZ_G_SIZE)),
6296 + /*--- Compute MTF values for the selectors. ---*/
6298 + UChar pos[BZ_N_GROUPS], ll_i, tmp2, tmp;
6299 + for (i = 0; i < nGroups; i++) pos[i] = i;
6300 + for (i = 0; i < nSelectors; i++) {
6301 + ll_i = s->selector[i];
6304 + while ( ll_i != tmp ) {
6311 + s->selectorMtf[i] = j;
6315 + /*--- Assign actual codes for the tables. --*/
6316 + for (t = 0; t < nGroups; t++) {
6319 + for (i = 0; i < alphaSize; i++) {
6320 + if (s->len[t][i] > maxLen) maxLen = s->len[t][i];
6321 + if (s->len[t][i] < minLen) minLen = s->len[t][i];
6323 + AssertH ( !(maxLen > 17 /*20*/ ), 3004 );
6324 + AssertH ( !(minLen < 1), 3005 );
6325 + BZ2_hbAssignCodes ( &(s->code[t][0]), &(s->len[t][0]),
6326 + minLen, maxLen, alphaSize );
6329 + /*--- Transmit the mapping table. ---*/
6332 + for (i = 0; i < 16; i++) {
6333 + inUse16[i] = False;
6334 + for (j = 0; j < 16; j++)
6335 + if (s->inUse[i * 16 + j]) inUse16[i] = True;
6339 + for (i = 0; i < 16; i++)
6340 + if (inUse16[i]) bsW(s,1,1); else bsW(s,1,0);
6342 + for (i = 0; i < 16; i++)
6344 + for (j = 0; j < 16; j++) {
6345 + if (s->inUse[i * 16 + j]) bsW(s,1,1); else bsW(s,1,0);
6348 + if (s->verbosity >= 3)
6349 + VPrintf1( " bytes: mapping %d, ", s->numZ-nBytes );
6352 + /*--- Now the selectors. ---*/
6354 + bsW ( s, 3, nGroups );
6355 + bsW ( s, 15, nSelectors );
6356 + for (i = 0; i < nSelectors; i++) {
6357 + for (j = 0; j < s->selectorMtf[i]; j++) bsW(s,1,1);
6360 + if (s->verbosity >= 3)
6361 + VPrintf1( "selectors %d, ", s->numZ-nBytes );
6363 + /*--- Now the coding tables. ---*/
6366 + for (t = 0; t < nGroups; t++) {
6367 + Int32 curr = s->len[t][0];
6368 + bsW ( s, 5, curr );
6369 + for (i = 0; i < alphaSize; i++) {
6370 + while (curr < s->len[t][i]) { bsW(s,2,2); curr++; /* 10 */ };
6371 + while (curr > s->len[t][i]) { bsW(s,2,3); curr--; /* 11 */ };
6376 + if (s->verbosity >= 3)
6377 + VPrintf1 ( "code lengths %d, ", s->numZ-nBytes );
6379 + /*--- And finally, the block data proper ---*/
6384 + if (gs >= s->nMTF) break;
6385 + ge = gs + BZ_G_SIZE - 1;
6386 + if (ge >= s->nMTF) ge = s->nMTF-1;
6387 + AssertH ( s->selector[selCtr] < nGroups, 3006 );
6389 + if (nGroups == 6 && 50 == ge-gs+1) {
6390 + /*--- fast track the common case ---*/
6392 + UChar* s_len_sel_selCtr
6393 + = &(s->len[s->selector[selCtr]][0]);
6394 + Int32* s_code_sel_selCtr
6395 + = &(s->code[s->selector[selCtr]][0]);
6397 +# define BZ_ITAH(nn) \
6398 + mtfv_i = mtfv[gs+(nn)]; \
6400 + s_len_sel_selCtr[mtfv_i], \
6401 + s_code_sel_selCtr[mtfv_i] )
6403 + BZ_ITAH(0); BZ_ITAH(1); BZ_ITAH(2); BZ_ITAH(3); BZ_ITAH(4);
6404 + BZ_ITAH(5); BZ_ITAH(6); BZ_ITAH(7); BZ_ITAH(8); BZ_ITAH(9);
6405 + BZ_ITAH(10); BZ_ITAH(11); BZ_ITAH(12); BZ_ITAH(13); BZ_ITAH(14);
6406 + BZ_ITAH(15); BZ_ITAH(16); BZ_ITAH(17); BZ_ITAH(18); BZ_ITAH(19);
6407 + BZ_ITAH(20); BZ_ITAH(21); BZ_ITAH(22); BZ_ITAH(23); BZ_ITAH(24);
6408 + BZ_ITAH(25); BZ_ITAH(26); BZ_ITAH(27); BZ_ITAH(28); BZ_ITAH(29);
6409 + BZ_ITAH(30); BZ_ITAH(31); BZ_ITAH(32); BZ_ITAH(33); BZ_ITAH(34);
6410 + BZ_ITAH(35); BZ_ITAH(36); BZ_ITAH(37); BZ_ITAH(38); BZ_ITAH(39);
6411 + BZ_ITAH(40); BZ_ITAH(41); BZ_ITAH(42); BZ_ITAH(43); BZ_ITAH(44);
6412 + BZ_ITAH(45); BZ_ITAH(46); BZ_ITAH(47); BZ_ITAH(48); BZ_ITAH(49);
6417 + /*--- slow version which correctly handles all situations ---*/
6418 + for (i = gs; i <= ge; i++) {
6420 + s->len [s->selector[selCtr]] [mtfv[i]],
6421 + s->code [s->selector[selCtr]] [mtfv[i]] );
6429 + AssertH( selCtr == nSelectors, 3007 );
6431 + if (s->verbosity >= 3)
6432 + VPrintf1( "codes %d\n", s->numZ-nBytes );
6436 +/*---------------------------------------------------*/
6437 +void BZ2_compressBlock ( EState* s, Bool is_last_block )
6439 + if (s->nblock > 0) {
6441 + BZ_FINALISE_CRC ( s->blockCRC );
6442 + s->combinedCRC = (s->combinedCRC << 1) | (s->combinedCRC >> 31);
6443 + s->combinedCRC ^= s->blockCRC;
6444 + if (s->blockNo > 1) s->numZ = 0;
6446 + if (s->verbosity >= 2)
6447 + VPrintf4( " block %d: crc = 0x%08x, "
6448 + "combined CRC = 0x%08x, size = %d\n",
6449 + s->blockNo, s->blockCRC, s->combinedCRC, s->nblock );
6451 + BZ2_blockSort ( s );
6454 + s->zbits = (UChar*) (&((UChar*)s->arr2)[s->nblock]);
6456 + /*-- If this is the first block, create the stream header. --*/
6457 + if (s->blockNo == 1) {
6458 + BZ2_bsInitWrite ( s );
6459 + bsPutUChar ( s, BZ_HDR_B );
6460 + bsPutUChar ( s, BZ_HDR_Z );
6461 + bsPutUChar ( s, BZ_HDR_h );
6462 + bsPutUChar ( s, (UChar)(BZ_HDR_0 + s->blockSize100k) );
6465 + if (s->nblock > 0) {
6467 + bsPutUChar ( s, 0x31 ); bsPutUChar ( s, 0x41 );
6468 + bsPutUChar ( s, 0x59 ); bsPutUChar ( s, 0x26 );
6469 + bsPutUChar ( s, 0x53 ); bsPutUChar ( s, 0x59 );
6471 + /*-- Now the block's CRC, so it is in a known place. --*/
6472 + bsPutUInt32 ( s, s->blockCRC );
6475 + Now a single bit indicating (non-)randomisation.
6476 + As of version 0.9.5, we use a better sorting algorithm
6477 + which makes randomisation unnecessary. So always set
6478 + the randomised bit to 'no'. Of course, the decoder
6479 + still needs to be able to handle randomised blocks
6480 + so as to maintain backwards compatibility with
6481 + older versions of bzip2.
6485 + bsW ( s, 24, s->origPtr );
6486 + generateMTFValues ( s );
6487 + sendMTFValues ( s );
6491 + /*-- If this is the last block, add the stream trailer. --*/
6492 + if (is_last_block) {
6494 + bsPutUChar ( s, 0x17 ); bsPutUChar ( s, 0x72 );
6495 + bsPutUChar ( s, 0x45 ); bsPutUChar ( s, 0x38 );
6496 + bsPutUChar ( s, 0x50 ); bsPutUChar ( s, 0x90 );
6497 + bsPutUInt32 ( s, s->combinedCRC );
6498 + if (s->verbosity >= 2)
6499 + VPrintf1( " final combined CRC = 0x%08x\n ", s->combinedCRC );
6500 + bsFinishWrite ( s );
6505 +/*-------------------------------------------------------------*/
6506 +/*--- end compress.c ---*/
6507 +/*-------------------------------------------------------------*/
6508 diff --git a/pit/upstream-bzip2/crctable.c b/pit/upstream-bzip2/crctable.c
6509 new file mode 100644
6510 index 0000000..1fea7e9
6512 +++ b/pit/upstream-bzip2/crctable.c
6515 +/*-------------------------------------------------------------*/
6516 +/*--- Table for doing CRCs ---*/
6517 +/*--- crctable.c ---*/
6518 +/*-------------------------------------------------------------*/
6520 +/* ------------------------------------------------------------------
6521 + This file is part of bzip2/libbzip2, a program and library for
6522 + lossless, block-sorting data compression.
6524 + bzip2/libbzip2 version 1.0.6 of 6 September 2010
6525 + Copyright (C) 1996-2010 Julian Seward <jseward@bzip.org>
6527 + Please read the WARNING, DISCLAIMER and PATENTS sections in the
6530 + This program is released under the terms of the license contained
6531 + in the file LICENSE.
6532 + ------------------------------------------------------------------ */
6535 +#include "bzlib_private.h"
6538 + I think this is an implementation of the AUTODIN-II,
6539 + Ethernet & FDDI 32-bit CRC standard. Vaguely derived
6540 + from code by Rob Warnock, in Section 51 of the
6541 + comp.compression FAQ.
6544 +UInt32 BZ2_crc32Table[256] = {
6546 + /*-- Ugly, innit? --*/
6548 + 0x00000000L, 0x04c11db7L, 0x09823b6eL, 0x0d4326d9L,
6549 + 0x130476dcL, 0x17c56b6bL, 0x1a864db2L, 0x1e475005L,
6550 + 0x2608edb8L, 0x22c9f00fL, 0x2f8ad6d6L, 0x2b4bcb61L,
6551 + 0x350c9b64L, 0x31cd86d3L, 0x3c8ea00aL, 0x384fbdbdL,
6552 + 0x4c11db70L, 0x48d0c6c7L, 0x4593e01eL, 0x4152fda9L,
6553 + 0x5f15adacL, 0x5bd4b01bL, 0x569796c2L, 0x52568b75L,
6554 + 0x6a1936c8L, 0x6ed82b7fL, 0x639b0da6L, 0x675a1011L,
6555 + 0x791d4014L, 0x7ddc5da3L, 0x709f7b7aL, 0x745e66cdL,
6556 + 0x9823b6e0L, 0x9ce2ab57L, 0x91a18d8eL, 0x95609039L,
6557 + 0x8b27c03cL, 0x8fe6dd8bL, 0x82a5fb52L, 0x8664e6e5L,
6558 + 0xbe2b5b58L, 0xbaea46efL, 0xb7a96036L, 0xb3687d81L,
6559 + 0xad2f2d84L, 0xa9ee3033L, 0xa4ad16eaL, 0xa06c0b5dL,
6560 + 0xd4326d90L, 0xd0f37027L, 0xddb056feL, 0xd9714b49L,
6561 + 0xc7361b4cL, 0xc3f706fbL, 0xceb42022L, 0xca753d95L,
6562 + 0xf23a8028L, 0xf6fb9d9fL, 0xfbb8bb46L, 0xff79a6f1L,
6563 + 0xe13ef6f4L, 0xe5ffeb43L, 0xe8bccd9aL, 0xec7dd02dL,
6564 + 0x34867077L, 0x30476dc0L, 0x3d044b19L, 0x39c556aeL,
6565 + 0x278206abL, 0x23431b1cL, 0x2e003dc5L, 0x2ac12072L,
6566 + 0x128e9dcfL, 0x164f8078L, 0x1b0ca6a1L, 0x1fcdbb16L,
6567 + 0x018aeb13L, 0x054bf6a4L, 0x0808d07dL, 0x0cc9cdcaL,
6568 + 0x7897ab07L, 0x7c56b6b0L, 0x71159069L, 0x75d48ddeL,
6569 + 0x6b93dddbL, 0x6f52c06cL, 0x6211e6b5L, 0x66d0fb02L,
6570 + 0x5e9f46bfL, 0x5a5e5b08L, 0x571d7dd1L, 0x53dc6066L,
6571 + 0x4d9b3063L, 0x495a2dd4L, 0x44190b0dL, 0x40d816baL,
6572 + 0xaca5c697L, 0xa864db20L, 0xa527fdf9L, 0xa1e6e04eL,
6573 + 0xbfa1b04bL, 0xbb60adfcL, 0xb6238b25L, 0xb2e29692L,
6574 + 0x8aad2b2fL, 0x8e6c3698L, 0x832f1041L, 0x87ee0df6L,
6575 + 0x99a95df3L, 0x9d684044L, 0x902b669dL, 0x94ea7b2aL,
6576 + 0xe0b41de7L, 0xe4750050L, 0xe9362689L, 0xedf73b3eL,
6577 + 0xf3b06b3bL, 0xf771768cL, 0xfa325055L, 0xfef34de2L,
6578 + 0xc6bcf05fL, 0xc27dede8L, 0xcf3ecb31L, 0xcbffd686L,
6579 + 0xd5b88683L, 0xd1799b34L, 0xdc3abdedL, 0xd8fba05aL,
6580 + 0x690ce0eeL, 0x6dcdfd59L, 0x608edb80L, 0x644fc637L,
6581 + 0x7a089632L, 0x7ec98b85L, 0x738aad5cL, 0x774bb0ebL,
6582 + 0x4f040d56L, 0x4bc510e1L, 0x46863638L, 0x42472b8fL,
6583 + 0x5c007b8aL, 0x58c1663dL, 0x558240e4L, 0x51435d53L,
6584 + 0x251d3b9eL, 0x21dc2629L, 0x2c9f00f0L, 0x285e1d47L,
6585 + 0x36194d42L, 0x32d850f5L, 0x3f9b762cL, 0x3b5a6b9bL,
6586 + 0x0315d626L, 0x07d4cb91L, 0x0a97ed48L, 0x0e56f0ffL,
6587 + 0x1011a0faL, 0x14d0bd4dL, 0x19939b94L, 0x1d528623L,
6588 + 0xf12f560eL, 0xf5ee4bb9L, 0xf8ad6d60L, 0xfc6c70d7L,
6589 + 0xe22b20d2L, 0xe6ea3d65L, 0xeba91bbcL, 0xef68060bL,
6590 + 0xd727bbb6L, 0xd3e6a601L, 0xdea580d8L, 0xda649d6fL,
6591 + 0xc423cd6aL, 0xc0e2d0ddL, 0xcda1f604L, 0xc960ebb3L,
6592 + 0xbd3e8d7eL, 0xb9ff90c9L, 0xb4bcb610L, 0xb07daba7L,
6593 + 0xae3afba2L, 0xaafbe615L, 0xa7b8c0ccL, 0xa379dd7bL,
6594 + 0x9b3660c6L, 0x9ff77d71L, 0x92b45ba8L, 0x9675461fL,
6595 + 0x8832161aL, 0x8cf30badL, 0x81b02d74L, 0x857130c3L,
6596 + 0x5d8a9099L, 0x594b8d2eL, 0x5408abf7L, 0x50c9b640L,
6597 + 0x4e8ee645L, 0x4a4ffbf2L, 0x470cdd2bL, 0x43cdc09cL,
6598 + 0x7b827d21L, 0x7f436096L, 0x7200464fL, 0x76c15bf8L,
6599 + 0x68860bfdL, 0x6c47164aL, 0x61043093L, 0x65c52d24L,
6600 + 0x119b4be9L, 0x155a565eL, 0x18197087L, 0x1cd86d30L,
6601 + 0x029f3d35L, 0x065e2082L, 0x0b1d065bL, 0x0fdc1becL,
6602 + 0x3793a651L, 0x3352bbe6L, 0x3e119d3fL, 0x3ad08088L,
6603 + 0x2497d08dL, 0x2056cd3aL, 0x2d15ebe3L, 0x29d4f654L,
6604 + 0xc5a92679L, 0xc1683bceL, 0xcc2b1d17L, 0xc8ea00a0L,
6605 + 0xd6ad50a5L, 0xd26c4d12L, 0xdf2f6bcbL, 0xdbee767cL,
6606 + 0xe3a1cbc1L, 0xe760d676L, 0xea23f0afL, 0xeee2ed18L,
6607 + 0xf0a5bd1dL, 0xf464a0aaL, 0xf9278673L, 0xfde69bc4L,
6608 + 0x89b8fd09L, 0x8d79e0beL, 0x803ac667L, 0x84fbdbd0L,
6609 + 0x9abc8bd5L, 0x9e7d9662L, 0x933eb0bbL, 0x97ffad0cL,
6610 + 0xafb010b1L, 0xab710d06L, 0xa6322bdfL, 0xa2f33668L,
6611 + 0xbcb4666dL, 0xb8757bdaL, 0xb5365d03L, 0xb1f740b4L
6615 +/*-------------------------------------------------------------*/
6616 +/*--- end crctable.c ---*/
6617 +/*-------------------------------------------------------------*/
6618 diff --git a/pit/upstream-bzip2/decompress.c b/pit/upstream-bzip2/decompress.c
6619 new file mode 100644
6620 index 0000000..311f566
6622 +++ b/pit/upstream-bzip2/decompress.c
6625 +/*-------------------------------------------------------------*/
6626 +/*--- Decompression machinery ---*/
6627 +/*--- decompress.c ---*/
6628 +/*-------------------------------------------------------------*/
6630 +/* ------------------------------------------------------------------
6631 + This file is part of bzip2/libbzip2, a program and library for
6632 + lossless, block-sorting data compression.
6634 + bzip2/libbzip2 version 1.0.6 of 6 September 2010
6635 + Copyright (C) 1996-2010 Julian Seward <jseward@bzip.org>
6637 + Please read the WARNING, DISCLAIMER and PATENTS sections in the
6640 + This program is released under the terms of the license contained
6641 + in the file LICENSE.
6642 + ------------------------------------------------------------------ */
6645 +#include "bzlib_private.h"
6648 +/*---------------------------------------------------*/
6650 +void makeMaps_d ( DState* s )
6654 + for (i = 0; i < 256; i++)
6655 + if (s->inUse[i]) {
6656 + s->seqToUnseq[s->nInUse] = i;
6662 +/*---------------------------------------------------*/
6663 +#define RETURN(rrr) \
6664 + { retVal = rrr; goto save_state_and_return; };
6666 +#define GET_BITS(lll,vvv,nnn) \
6667 + case lll: s->state = lll; \
6669 + if (s->bsLive >= nnn) { \
6671 + v = (s->bsBuff >> \
6672 + (s->bsLive-nnn)) & ((1 << nnn)-1); \
6673 + s->bsLive -= nnn; \
6677 + if (s->strm->avail_in == 0) RETURN(BZ_OK); \
6679 + = (s->bsBuff << 8) | \
6681 + (*((UChar*)(s->strm->next_in)))); \
6683 + s->strm->next_in++; \
6684 + s->strm->avail_in--; \
6685 + s->strm->total_in_lo32++; \
6686 + if (s->strm->total_in_lo32 == 0) \
6687 + s->strm->total_in_hi32++; \
6690 +#define GET_UCHAR(lll,uuu) \
6691 + GET_BITS(lll,uuu,8)
6693 +#define GET_BIT(lll,uuu) \
6694 + GET_BITS(lll,uuu,1)
6696 +/*---------------------------------------------------*/
6697 +#define GET_MTF_VAL(label1,label2,lval) \
6699 + if (groupPos == 0) { \
6701 + if (groupNo >= nSelectors) \
6702 + RETURN(BZ_DATA_ERROR); \
6703 + groupPos = BZ_G_SIZE; \
6704 + gSel = s->selector[groupNo]; \
6705 + gMinlen = s->minLens[gSel]; \
6706 + gLimit = &(s->limit[gSel][0]); \
6707 + gPerm = &(s->perm[gSel][0]); \
6708 + gBase = &(s->base[gSel][0]); \
6712 + GET_BITS(label1, zvec, zn); \
6714 + if (zn > 20 /* the longest code */) \
6715 + RETURN(BZ_DATA_ERROR); \
6716 + if (zvec <= gLimit[zn]) break; \
6718 + GET_BIT(label2, zj); \
6719 + zvec = (zvec << 1) | zj; \
6721 + if (zvec - gBase[zn] < 0 \
6722 + || zvec - gBase[zn] >= BZ_MAX_ALPHA_SIZE) \
6723 + RETURN(BZ_DATA_ERROR); \
6724 + lval = gPerm[zvec - gBase[zn]]; \
6728 +/*---------------------------------------------------*/
6729 +Int32 BZ2_decompress ( DState* s )
6733 + Int32 minLen, maxLen;
6734 + bz_stream* strm = s->strm;
6736 + /* stuff that needs to be saved/restored */
6762 + if (s->state == BZ_X_MAGIC_1) {
6763 + /*initialise the save area*/
6767 + s->save_alphaSize = 0;
6768 + s->save_nGroups = 0;
6769 + s->save_nSelectors = 0;
6771 + s->save_groupNo = 0;
6772 + s->save_groupPos = 0;
6773 + s->save_nextSym = 0;
6774 + s->save_nblockMAX = 0;
6775 + s->save_nblock = 0;
6784 + s->save_gMinlen = 0;
6785 + s->save_gLimit = NULL;
6786 + s->save_gBase = NULL;
6787 + s->save_gPerm = NULL;
6790 + /*restore from the save area*/
6794 + alphaSize = s->save_alphaSize;
6795 + nGroups = s->save_nGroups;
6796 + nSelectors = s->save_nSelectors;
6797 + EOB = s->save_EOB;
6798 + groupNo = s->save_groupNo;
6799 + groupPos = s->save_groupPos;
6800 + nextSym = s->save_nextSym;
6801 + nblockMAX = s->save_nblockMAX;
6802 + nblock = s->save_nblock;
6805 + curr = s->save_curr;
6808 + zvec = s->save_zvec;
6810 + gSel = s->save_gSel;
6811 + gMinlen = s->save_gMinlen;
6812 + gLimit = s->save_gLimit;
6813 + gBase = s->save_gBase;
6814 + gPerm = s->save_gPerm;
6818 + switch (s->state) {
6820 + GET_UCHAR(BZ_X_MAGIC_1, uc);
6821 + if (uc != BZ_HDR_B) RETURN(BZ_DATA_ERROR_MAGIC);
6823 + GET_UCHAR(BZ_X_MAGIC_2, uc);
6824 + if (uc != BZ_HDR_Z) RETURN(BZ_DATA_ERROR_MAGIC);
6826 + GET_UCHAR(BZ_X_MAGIC_3, uc)
6827 + if (uc != BZ_HDR_h) RETURN(BZ_DATA_ERROR_MAGIC);
6829 + GET_BITS(BZ_X_MAGIC_4, s->blockSize100k, 8)
6830 + if (s->blockSize100k < (BZ_HDR_0 + 1) ||
6831 + s->blockSize100k > (BZ_HDR_0 + 9)) RETURN(BZ_DATA_ERROR_MAGIC);
6832 + s->blockSize100k -= BZ_HDR_0;
6834 + if (s->smallDecompress) {
6835 + s->ll16 = BZALLOC( s->blockSize100k * 100000 * sizeof(UInt16) );
6837 + ((1 + s->blockSize100k * 100000) >> 1) * sizeof(UChar)
6839 + if (s->ll16 == NULL || s->ll4 == NULL) RETURN(BZ_MEM_ERROR);
6841 + s->tt = BZALLOC( s->blockSize100k * 100000 * sizeof(Int32) );
6842 + if (s->tt == NULL) RETURN(BZ_MEM_ERROR);
6845 + GET_UCHAR(BZ_X_BLKHDR_1, uc);
6847 + if (uc == 0x17) goto endhdr_2;
6848 + if (uc != 0x31) RETURN(BZ_DATA_ERROR);
6849 + GET_UCHAR(BZ_X_BLKHDR_2, uc);
6850 + if (uc != 0x41) RETURN(BZ_DATA_ERROR);
6851 + GET_UCHAR(BZ_X_BLKHDR_3, uc);
6852 + if (uc != 0x59) RETURN(BZ_DATA_ERROR);
6853 + GET_UCHAR(BZ_X_BLKHDR_4, uc);
6854 + if (uc != 0x26) RETURN(BZ_DATA_ERROR);
6855 + GET_UCHAR(BZ_X_BLKHDR_5, uc);
6856 + if (uc != 0x53) RETURN(BZ_DATA_ERROR);
6857 + GET_UCHAR(BZ_X_BLKHDR_6, uc);
6858 + if (uc != 0x59) RETURN(BZ_DATA_ERROR);
6861 + if (s->verbosity >= 2)
6862 + VPrintf1 ( "\n [%d: huff+mtf ", s->currBlockNo );
6864 + s->storedBlockCRC = 0;
6865 + GET_UCHAR(BZ_X_BCRC_1, uc);
6866 + s->storedBlockCRC = (s->storedBlockCRC << 8) | ((UInt32)uc);
6867 + GET_UCHAR(BZ_X_BCRC_2, uc);
6868 + s->storedBlockCRC = (s->storedBlockCRC << 8) | ((UInt32)uc);
6869 + GET_UCHAR(BZ_X_BCRC_3, uc);
6870 + s->storedBlockCRC = (s->storedBlockCRC << 8) | ((UInt32)uc);
6871 + GET_UCHAR(BZ_X_BCRC_4, uc);
6872 + s->storedBlockCRC = (s->storedBlockCRC << 8) | ((UInt32)uc);
6874 + GET_BITS(BZ_X_RANDBIT, s->blockRandomised, 1);
6877 + GET_UCHAR(BZ_X_ORIGPTR_1, uc);
6878 + s->origPtr = (s->origPtr << 8) | ((Int32)uc);
6879 + GET_UCHAR(BZ_X_ORIGPTR_2, uc);
6880 + s->origPtr = (s->origPtr << 8) | ((Int32)uc);
6881 + GET_UCHAR(BZ_X_ORIGPTR_3, uc);
6882 + s->origPtr = (s->origPtr << 8) | ((Int32)uc);
6884 + if (s->origPtr < 0)
6885 + RETURN(BZ_DATA_ERROR);
6886 + if (s->origPtr > 10 + 100000*s->blockSize100k)
6887 + RETURN(BZ_DATA_ERROR);
6889 + /*--- Receive the mapping table ---*/
6890 + for (i = 0; i < 16; i++) {
6891 + GET_BIT(BZ_X_MAPPING_1, uc);
6893 + s->inUse16[i] = True; else
6894 + s->inUse16[i] = False;
6897 + for (i = 0; i < 256; i++) s->inUse[i] = False;
6899 + for (i = 0; i < 16; i++)
6900 + if (s->inUse16[i])
6901 + for (j = 0; j < 16; j++) {
6902 + GET_BIT(BZ_X_MAPPING_2, uc);
6903 + if (uc == 1) s->inUse[i * 16 + j] = True;
6906 + if (s->nInUse == 0) RETURN(BZ_DATA_ERROR);
6907 + alphaSize = s->nInUse+2;
6909 + /*--- Now the selectors ---*/
6910 + GET_BITS(BZ_X_SELECTOR_1, nGroups, 3);
6911 + if (nGroups < 2 || nGroups > 6) RETURN(BZ_DATA_ERROR);
6912 + GET_BITS(BZ_X_SELECTOR_2, nSelectors, 15);
6913 + if (nSelectors < 1) RETURN(BZ_DATA_ERROR);
6914 + for (i = 0; i < nSelectors; i++) {
6917 + GET_BIT(BZ_X_SELECTOR_3, uc);
6918 + if (uc == 0) break;
6920 + if (j >= nGroups) RETURN(BZ_DATA_ERROR);
6922 + s->selectorMtf[i] = j;
6925 + /*--- Undo the MTF values for the selectors. ---*/
6927 + UChar pos[BZ_N_GROUPS], tmp, v;
6928 + for (v = 0; v < nGroups; v++) pos[v] = v;
6930 + for (i = 0; i < nSelectors; i++) {
6931 + v = s->selectorMtf[i];
6933 + while (v > 0) { pos[v] = pos[v-1]; v--; }
6935 + s->selector[i] = tmp;
6939 + /*--- Now the coding tables ---*/
6940 + for (t = 0; t < nGroups; t++) {
6941 + GET_BITS(BZ_X_CODING_1, curr, 5);
6942 + for (i = 0; i < alphaSize; i++) {
6944 + if (curr < 1 || curr > 20) RETURN(BZ_DATA_ERROR);
6945 + GET_BIT(BZ_X_CODING_2, uc);
6946 + if (uc == 0) break;
6947 + GET_BIT(BZ_X_CODING_3, uc);
6948 + if (uc == 0) curr++; else curr--;
6950 + s->len[t][i] = curr;
6954 + /*--- Create the Huffman decoding tables ---*/
6955 + for (t = 0; t < nGroups; t++) {
6958 + for (i = 0; i < alphaSize; i++) {
6959 + if (s->len[t][i] > maxLen) maxLen = s->len[t][i];
6960 + if (s->len[t][i] < minLen) minLen = s->len[t][i];
6962 + BZ2_hbCreateDecodeTables (
6963 + &(s->limit[t][0]),
6967 + minLen, maxLen, alphaSize
6969 + s->minLens[t] = minLen;
6972 + /*--- Now the MTF values ---*/
6974 + EOB = s->nInUse+1;
6975 + nblockMAX = 100000 * s->blockSize100k;
6979 + for (i = 0; i <= 255; i++) s->unzftab[i] = 0;
6981 + /*-- MTF init --*/
6985 + for (ii = 256 / MTFL_SIZE - 1; ii >= 0; ii--) {
6986 + for (jj = MTFL_SIZE-1; jj >= 0; jj--) {
6987 + s->mtfa[kk] = (UChar)(ii * MTFL_SIZE + jj);
6990 + s->mtfbase[ii] = kk + 1;
6993 + /*-- end MTF init --*/
6996 + GET_MTF_VAL(BZ_X_MTF_1, BZ_X_MTF_2, nextSym);
7000 + if (nextSym == EOB) break;
7002 + if (nextSym == BZ_RUNA || nextSym == BZ_RUNB) {
7007 + /* Check that N doesn't get too big, so that es doesn't
7008 + go negative. The maximum value that can be
7009 + RUNA/RUNB encoded is equal to the block size (post
7010 + the initial RLE), viz, 900k, so bounding N at 2
7011 + million should guard against overflow without
7012 + rejecting any legitimate inputs. */
7013 + if (N >= 2*1024*1024) RETURN(BZ_DATA_ERROR);
7014 + if (nextSym == BZ_RUNA) es = es + (0+1) * N; else
7015 + if (nextSym == BZ_RUNB) es = es + (1+1) * N;
7017 + GET_MTF_VAL(BZ_X_MTF_3, BZ_X_MTF_4, nextSym);
7019 + while (nextSym == BZ_RUNA || nextSym == BZ_RUNB);
7022 + uc = s->seqToUnseq[ s->mtfa[s->mtfbase[0]] ];
7023 + s->unzftab[uc] += es;
7025 + if (s->smallDecompress)
7027 + if (nblock >= nblockMAX) RETURN(BZ_DATA_ERROR);
7028 + s->ll16[nblock] = (UInt16)uc;
7034 + if (nblock >= nblockMAX) RETURN(BZ_DATA_ERROR);
7035 + s->tt[nblock] = (UInt32)uc;
7044 + if (nblock >= nblockMAX) RETURN(BZ_DATA_ERROR);
7046 + /*-- uc = MTF ( nextSym-1 ) --*/
7048 + Int32 ii, jj, kk, pp, lno, off;
7050 + nn = (UInt32)(nextSym - 1);
7052 + if (nn < MTFL_SIZE) {
7053 + /* avoid general-case expense */
7054 + pp = s->mtfbase[0];
7055 + uc = s->mtfa[pp+nn];
7058 + s->mtfa[(z) ] = s->mtfa[(z)-1];
7059 + s->mtfa[(z)-1] = s->mtfa[(z)-2];
7060 + s->mtfa[(z)-2] = s->mtfa[(z)-3];
7061 + s->mtfa[(z)-3] = s->mtfa[(z)-4];
7065 + s->mtfa[(pp+nn)] = s->mtfa[(pp+nn)-1]; nn--;
7069 + /* general case */
7070 + lno = nn / MTFL_SIZE;
7071 + off = nn % MTFL_SIZE;
7072 + pp = s->mtfbase[lno] + off;
7074 + while (pp > s->mtfbase[lno]) {
7075 + s->mtfa[pp] = s->mtfa[pp-1]; pp--;
7077 + s->mtfbase[lno]++;
7079 + s->mtfbase[lno]--;
7080 + s->mtfa[s->mtfbase[lno]]
7081 + = s->mtfa[s->mtfbase[lno-1] + MTFL_SIZE - 1];
7085 + s->mtfa[s->mtfbase[0]] = uc;
7086 + if (s->mtfbase[0] == 0) {
7088 + for (ii = 256 / MTFL_SIZE-1; ii >= 0; ii--) {
7089 + for (jj = MTFL_SIZE-1; jj >= 0; jj--) {
7090 + s->mtfa[kk] = s->mtfa[s->mtfbase[ii] + jj];
7093 + s->mtfbase[ii] = kk + 1;
7098 + /*-- end uc = MTF ( nextSym-1 ) --*/
7100 + s->unzftab[s->seqToUnseq[uc]]++;
7101 + if (s->smallDecompress)
7102 + s->ll16[nblock] = (UInt16)(s->seqToUnseq[uc]); else
7103 + s->tt[nblock] = (UInt32)(s->seqToUnseq[uc]);
7106 + GET_MTF_VAL(BZ_X_MTF_5, BZ_X_MTF_6, nextSym);
7111 + /* Now we know what nblock is, we can do a better sanity
7112 + check on s->origPtr.
7114 + if (s->origPtr < 0 || s->origPtr >= nblock)
7115 + RETURN(BZ_DATA_ERROR);
7117 + /*-- Set up cftab to facilitate generation of T^(-1) --*/
7118 + /* Check: unzftab entries in range. */
7119 + for (i = 0; i <= 255; i++) {
7120 + if (s->unzftab[i] < 0 || s->unzftab[i] > nblock)
7121 + RETURN(BZ_DATA_ERROR);
7123 + /* Actually generate cftab. */
7125 + for (i = 1; i <= 256; i++) s->cftab[i] = s->unzftab[i-1];
7126 + for (i = 1; i <= 256; i++) s->cftab[i] += s->cftab[i-1];
7127 + /* Check: cftab entries in range. */
7128 + for (i = 0; i <= 256; i++) {
7129 + if (s->cftab[i] < 0 || s->cftab[i] > nblock) {
7130 + /* s->cftab[i] can legitimately be == nblock */
7131 + RETURN(BZ_DATA_ERROR);
7134 + /* Check: cftab entries non-descending. */
7135 + for (i = 1; i <= 256; i++) {
7136 + if (s->cftab[i-1] > s->cftab[i]) {
7137 + RETURN(BZ_DATA_ERROR);
7141 + s->state_out_len = 0;
7142 + s->state_out_ch = 0;
7143 + BZ_INITIALISE_CRC ( s->calculatedBlockCRC );
7144 + s->state = BZ_X_OUTPUT;
7145 + if (s->verbosity >= 2) VPrintf0 ( "rt+rld" );
7147 + if (s->smallDecompress) {
7149 + /*-- Make a copy of cftab, used in generation of T --*/
7150 + for (i = 0; i <= 256; i++) s->cftabCopy[i] = s->cftab[i];
7152 + /*-- compute the T vector --*/
7153 + for (i = 0; i < nblock; i++) {
7154 + uc = (UChar)(s->ll16[i]);
7155 + SET_LL(i, s->cftabCopy[uc]);
7156 + s->cftabCopy[uc]++;
7159 + /*-- Compute T^(-1) by pointer reversal on T --*/
7163 + Int32 tmp = GET_LL(j);
7168 + while (i != s->origPtr);
7170 + s->tPos = s->origPtr;
7171 + s->nblock_used = 0;
7172 + if (s->blockRandomised) {
7173 + BZ_RAND_INIT_MASK;
7174 + BZ_GET_SMALL(s->k0); s->nblock_used++;
7175 + BZ_RAND_UPD_MASK; s->k0 ^= BZ_RAND_MASK;
7177 + BZ_GET_SMALL(s->k0); s->nblock_used++;
7182 + /*-- compute the T^(-1) vector --*/
7183 + for (i = 0; i < nblock; i++) {
7184 + uc = (UChar)(s->tt[i] & 0xff);
7185 + s->tt[s->cftab[uc]] |= (i << 8);
7189 + s->tPos = s->tt[s->origPtr] >> 8;
7190 + s->nblock_used = 0;
7191 + if (s->blockRandomised) {
7192 + BZ_RAND_INIT_MASK;
7193 + BZ_GET_FAST(s->k0); s->nblock_used++;
7194 + BZ_RAND_UPD_MASK; s->k0 ^= BZ_RAND_MASK;
7196 + BZ_GET_FAST(s->k0); s->nblock_used++;
7207 + GET_UCHAR(BZ_X_ENDHDR_2, uc);
7208 + if (uc != 0x72) RETURN(BZ_DATA_ERROR);
7209 + GET_UCHAR(BZ_X_ENDHDR_3, uc);
7210 + if (uc != 0x45) RETURN(BZ_DATA_ERROR);
7211 + GET_UCHAR(BZ_X_ENDHDR_4, uc);
7212 + if (uc != 0x38) RETURN(BZ_DATA_ERROR);
7213 + GET_UCHAR(BZ_X_ENDHDR_5, uc);
7214 + if (uc != 0x50) RETURN(BZ_DATA_ERROR);
7215 + GET_UCHAR(BZ_X_ENDHDR_6, uc);
7216 + if (uc != 0x90) RETURN(BZ_DATA_ERROR);
7218 + s->storedCombinedCRC = 0;
7219 + GET_UCHAR(BZ_X_CCRC_1, uc);
7220 + s->storedCombinedCRC = (s->storedCombinedCRC << 8) | ((UInt32)uc);
7221 + GET_UCHAR(BZ_X_CCRC_2, uc);
7222 + s->storedCombinedCRC = (s->storedCombinedCRC << 8) | ((UInt32)uc);
7223 + GET_UCHAR(BZ_X_CCRC_3, uc);
7224 + s->storedCombinedCRC = (s->storedCombinedCRC << 8) | ((UInt32)uc);
7225 + GET_UCHAR(BZ_X_CCRC_4, uc);
7226 + s->storedCombinedCRC = (s->storedCombinedCRC << 8) | ((UInt32)uc);
7228 + s->state = BZ_X_IDLE;
7229 + RETURN(BZ_STREAM_END);
7231 + default: AssertH ( False, 4001 );
7234 + AssertH ( False, 4002 );
7236 + save_state_and_return:
7241 + s->save_alphaSize = alphaSize;
7242 + s->save_nGroups = nGroups;
7243 + s->save_nSelectors = nSelectors;
7244 + s->save_EOB = EOB;
7245 + s->save_groupNo = groupNo;
7246 + s->save_groupPos = groupPos;
7247 + s->save_nextSym = nextSym;
7248 + s->save_nblockMAX = nblockMAX;
7249 + s->save_nblock = nblock;
7252 + s->save_curr = curr;
7255 + s->save_zvec = zvec;
7257 + s->save_gSel = gSel;
7258 + s->save_gMinlen = gMinlen;
7259 + s->save_gLimit = gLimit;
7260 + s->save_gBase = gBase;
7261 + s->save_gPerm = gPerm;
7267 +/*-------------------------------------------------------------*/
7268 +/*--- end decompress.c ---*/
7269 +/*-------------------------------------------------------------*/
7270 diff --git a/pit/upstream-bzip2/huffman.c b/pit/upstream-bzip2/huffman.c
7271 new file mode 100644
7272 index 0000000..2283fdb
7274 +++ b/pit/upstream-bzip2/huffman.c
7277 +/*-------------------------------------------------------------*/
7278 +/*--- Huffman coding low-level stuff ---*/
7279 +/*--- huffman.c ---*/
7280 +/*-------------------------------------------------------------*/
7282 +/* ------------------------------------------------------------------
7283 + This file is part of bzip2/libbzip2, a program and library for
7284 + lossless, block-sorting data compression.
7286 + bzip2/libbzip2 version 1.0.6 of 6 September 2010
7287 + Copyright (C) 1996-2010 Julian Seward <jseward@bzip.org>
7289 + Please read the WARNING, DISCLAIMER and PATENTS sections in the
7292 + This program is released under the terms of the license contained
7293 + in the file LICENSE.
7294 + ------------------------------------------------------------------ */
7297 +#include "bzlib_private.h"
7299 +/*---------------------------------------------------*/
7300 +#define WEIGHTOF(zz0) ((zz0) & 0xffffff00)
7301 +#define DEPTHOF(zz1) ((zz1) & 0x000000ff)
7302 +#define MYMAX(zz2,zz3) ((zz2) > (zz3) ? (zz2) : (zz3))
7304 +#define ADDWEIGHTS(zw1,zw2) \
7305 + (WEIGHTOF(zw1)+WEIGHTOF(zw2)) | \
7306 + (1 + MYMAX(DEPTHOF(zw1),DEPTHOF(zw2)))
7308 +#define UPHEAP(z) \
7311 + zz = z; tmp = heap[zz]; \
7312 + while (weight[tmp] < weight[heap[zz >> 1]]) { \
7313 + heap[zz] = heap[zz >> 1]; \
7319 +#define DOWNHEAP(z) \
7321 + Int32 zz, yy, tmp; \
7322 + zz = z; tmp = heap[zz]; \
7325 + if (yy > nHeap) break; \
7326 + if (yy < nHeap && \
7327 + weight[heap[yy+1]] < weight[heap[yy]]) \
7329 + if (weight[tmp] < weight[heap[yy]]) break; \
7330 + heap[zz] = heap[yy]; \
7337 +/*---------------------------------------------------*/
7338 +void BZ2_hbMakeCodeLengths ( UChar *len,
7344 + Nodes and heap entries run from 1. Entry 0
7345 + for both the heap and nodes is a sentinel.
7347 + Int32 nNodes, nHeap, n1, n2, i, j, k;
7350 + Int32 heap [ BZ_MAX_ALPHA_SIZE + 2 ];
7351 + Int32 weight [ BZ_MAX_ALPHA_SIZE * 2 ];
7352 + Int32 parent [ BZ_MAX_ALPHA_SIZE * 2 ];
7354 + for (i = 0; i < alphaSize; i++)
7355 + weight[i+1] = (freq[i] == 0 ? 1 : freq[i]) << 8;
7359 + nNodes = alphaSize;
7366 + for (i = 1; i <= alphaSize; i++) {
7373 + AssertH( nHeap < (BZ_MAX_ALPHA_SIZE+2), 2001 );
7375 + while (nHeap > 1) {
7376 + n1 = heap[1]; heap[1] = heap[nHeap]; nHeap--; DOWNHEAP(1);
7377 + n2 = heap[1]; heap[1] = heap[nHeap]; nHeap--; DOWNHEAP(1);
7379 + parent[n1] = parent[n2] = nNodes;
7380 + weight[nNodes] = ADDWEIGHTS(weight[n1], weight[n2]);
7381 + parent[nNodes] = -1;
7383 + heap[nHeap] = nNodes;
7387 + AssertH( nNodes < (BZ_MAX_ALPHA_SIZE * 2), 2002 );
7390 + for (i = 1; i <= alphaSize; i++) {
7393 + while (parent[k] >= 0) { k = parent[k]; j++; }
7395 + if (j > maxLen) tooLong = True;
7398 + if (! tooLong) break;
7400 + /* 17 Oct 04: keep-going condition for the following loop used
7401 + to be 'i < alphaSize', which missed the last element,
7402 + theoretically leading to the possibility of the compressor
7403 + looping. However, this count-scaling step is only needed if
7404 + one of the generated Huffman code words is longer than
7405 + maxLen, which up to and including version 1.0.2 was 20 bits,
7406 + which is extremely unlikely. In version 1.0.3 maxLen was
7407 + changed to 17 bits, which has minimal effect on compression
7408 + ratio, but does mean this scaling step is used from time to
7409 + time, enough to verify that it works.
7411 + This means that bzip2-1.0.3 and later will only produce
7412 + Huffman codes with a maximum length of 17 bits. However, in
7413 + order to preserve backwards compatibility with bitstreams
7414 + produced by versions pre-1.0.3, the decompressor must still
7415 + handle lengths of up to 20. */
7417 + for (i = 1; i <= alphaSize; i++) {
7418 + j = weight[i] >> 8;
7420 + weight[i] = j << 8;
7426 +/*---------------------------------------------------*/
7427 +void BZ2_hbAssignCodes ( Int32 *code,
7436 + for (n = minLen; n <= maxLen; n++) {
7437 + for (i = 0; i < alphaSize; i++)
7438 + if (length[i] == n) { code[i] = vec; vec++; };
7444 +/*---------------------------------------------------*/
7445 +void BZ2_hbCreateDecodeTables ( Int32 *limit,
7453 + Int32 pp, i, j, vec;
7456 + for (i = minLen; i <= maxLen; i++)
7457 + for (j = 0; j < alphaSize; j++)
7458 + if (length[j] == i) { perm[pp] = j; pp++; };
7460 + for (i = 0; i < BZ_MAX_CODE_LEN; i++) base[i] = 0;
7461 + for (i = 0; i < alphaSize; i++) base[length[i]+1]++;
7463 + for (i = 1; i < BZ_MAX_CODE_LEN; i++) base[i] += base[i-1];
7465 + for (i = 0; i < BZ_MAX_CODE_LEN; i++) limit[i] = 0;
7468 + for (i = minLen; i <= maxLen; i++) {
7469 + vec += (base[i+1] - base[i]);
7473 + for (i = minLen + 1; i <= maxLen; i++)
7474 + base[i] = ((limit[i-1] + 1) << 1) - base[i];
7478 +/*-------------------------------------------------------------*/
7479 +/*--- end huffman.c ---*/
7480 +/*-------------------------------------------------------------*/
7481 diff --git a/pit/upstream-bzip2/randtable.c b/pit/upstream-bzip2/randtable.c
7482 new file mode 100644
7483 index 0000000..6d62459
7485 +++ b/pit/upstream-bzip2/randtable.c
7488 +/*-------------------------------------------------------------*/
7489 +/*--- Table for randomising repetitive blocks ---*/
7490 +/*--- randtable.c ---*/
7491 +/*-------------------------------------------------------------*/
7493 +/* ------------------------------------------------------------------
7494 + This file is part of bzip2/libbzip2, a program and library for
7495 + lossless, block-sorting data compression.
7497 + bzip2/libbzip2 version 1.0.6 of 6 September 2010
7498 + Copyright (C) 1996-2010 Julian Seward <jseward@bzip.org>
7500 + Please read the WARNING, DISCLAIMER and PATENTS sections in the
7503 + This program is released under the terms of the license contained
7504 + in the file LICENSE.
7505 + ------------------------------------------------------------------ */
7508 +#include "bzlib_private.h"
7511 +/*---------------------------------------------*/
7512 +Int32 BZ2_rNums[512] = {
7513 + 619, 720, 127, 481, 931, 816, 813, 233, 566, 247,
7514 + 985, 724, 205, 454, 863, 491, 741, 242, 949, 214,
7515 + 733, 859, 335, 708, 621, 574, 73, 654, 730, 472,
7516 + 419, 436, 278, 496, 867, 210, 399, 680, 480, 51,
7517 + 878, 465, 811, 169, 869, 675, 611, 697, 867, 561,
7518 + 862, 687, 507, 283, 482, 129, 807, 591, 733, 623,
7519 + 150, 238, 59, 379, 684, 877, 625, 169, 643, 105,
7520 + 170, 607, 520, 932, 727, 476, 693, 425, 174, 647,
7521 + 73, 122, 335, 530, 442, 853, 695, 249, 445, 515,
7522 + 909, 545, 703, 919, 874, 474, 882, 500, 594, 612,
7523 + 641, 801, 220, 162, 819, 984, 589, 513, 495, 799,
7524 + 161, 604, 958, 533, 221, 400, 386, 867, 600, 782,
7525 + 382, 596, 414, 171, 516, 375, 682, 485, 911, 276,
7526 + 98, 553, 163, 354, 666, 933, 424, 341, 533, 870,
7527 + 227, 730, 475, 186, 263, 647, 537, 686, 600, 224,
7528 + 469, 68, 770, 919, 190, 373, 294, 822, 808, 206,
7529 + 184, 943, 795, 384, 383, 461, 404, 758, 839, 887,
7530 + 715, 67, 618, 276, 204, 918, 873, 777, 604, 560,
7531 + 951, 160, 578, 722, 79, 804, 96, 409, 713, 940,
7532 + 652, 934, 970, 447, 318, 353, 859, 672, 112, 785,
7533 + 645, 863, 803, 350, 139, 93, 354, 99, 820, 908,
7534 + 609, 772, 154, 274, 580, 184, 79, 626, 630, 742,
7535 + 653, 282, 762, 623, 680, 81, 927, 626, 789, 125,
7536 + 411, 521, 938, 300, 821, 78, 343, 175, 128, 250,
7537 + 170, 774, 972, 275, 999, 639, 495, 78, 352, 126,
7538 + 857, 956, 358, 619, 580, 124, 737, 594, 701, 612,
7539 + 669, 112, 134, 694, 363, 992, 809, 743, 168, 974,
7540 + 944, 375, 748, 52, 600, 747, 642, 182, 862, 81,
7541 + 344, 805, 988, 739, 511, 655, 814, 334, 249, 515,
7542 + 897, 955, 664, 981, 649, 113, 974, 459, 893, 228,
7543 + 433, 837, 553, 268, 926, 240, 102, 654, 459, 51,
7544 + 686, 754, 806, 760, 493, 403, 415, 394, 687, 700,
7545 + 946, 670, 656, 610, 738, 392, 760, 799, 887, 653,
7546 + 978, 321, 576, 617, 626, 502, 894, 679, 243, 440,
7547 + 680, 879, 194, 572, 640, 724, 926, 56, 204, 700,
7548 + 707, 151, 457, 449, 797, 195, 791, 558, 945, 679,
7549 + 297, 59, 87, 824, 713, 663, 412, 693, 342, 606,
7550 + 134, 108, 571, 364, 631, 212, 174, 643, 304, 329,
7551 + 343, 97, 430, 751, 497, 314, 983, 374, 822, 928,
7552 + 140, 206, 73, 263, 980, 736, 876, 478, 430, 305,
7553 + 170, 514, 364, 692, 829, 82, 855, 953, 676, 246,
7554 + 369, 970, 294, 750, 807, 827, 150, 790, 288, 923,
7555 + 804, 378, 215, 828, 592, 281, 565, 555, 710, 82,
7556 + 896, 831, 547, 261, 524, 462, 293, 465, 502, 56,
7557 + 661, 821, 976, 991, 658, 869, 905, 758, 745, 193,
7558 + 768, 550, 608, 933, 378, 286, 215, 979, 792, 961,
7559 + 61, 688, 793, 644, 986, 403, 106, 366, 905, 644,
7560 + 372, 567, 466, 434, 645, 210, 389, 550, 919, 135,
7561 + 780, 773, 635, 389, 707, 100, 626, 958, 165, 504,
7562 + 920, 176, 193, 713, 857, 265, 203, 50, 668, 108,
7563 + 645, 990, 626, 197, 510, 357, 358, 850, 858, 364,
7568 +/*-------------------------------------------------------------*/
7569 +/*--- end randtable.c ---*/
7570 +/*-------------------------------------------------------------*/