Move RPM packaging files into separate dir
[tools/pristine-tar.git] / packaging / 0002-openSUSE-HACK-add-upstream-bzip2-v1.0.6-sources.patch
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
4
5 Only take the selected files that are needed. Needed for being able to
6 correctly re-create bz2 archives on openSUSE.
7
8 Gbp-Rpm-If: 0%{?suse_version}
9
10 Signed-off-by: Markus Lehtonen <markus.lehtonen@linux.intel.com>
11 ---
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
37
38 diff --git a/pit/upstream-bzip2/LICENSE b/pit/upstream-bzip2/LICENSE
39 new file mode 100644
40 index 0000000..cc61417
41 --- /dev/null
42 +++ b/pit/upstream-bzip2/LICENSE
43 @@ -0,0 +1,42 @@
44 +
45 +--------------------------------------------------------------------------
46 +
47 +This program, "bzip2", the associated library "libbzip2", and all
48 +documentation, are copyright (C) 1996-2010 Julian R Seward.  All
49 +rights reserved.
50 +
51 +Redistribution and use in source and binary forms, with or without
52 +modification, are permitted provided that the following conditions
53 +are met:
54 +
55 +1. Redistributions of source code must retain the above copyright
56 +   notice, this list of conditions and the following disclaimer.
57 +
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.
62 +
63 +3. Altered source versions must be plainly marked as such, and must
64 +   not be misrepresented as being the original software.
65 +
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 
68 +   permission.
69 +
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.
81 +
82 +Julian Seward, jseward@bzip.org
83 +bzip2/libbzip2 version 1.0.6 of 6 September 2010
84 +
85 +--------------------------------------------------------------------------
86 diff --git a/pit/upstream-bzip2/Makefile b/pit/upstream-bzip2/Makefile
87 new file mode 100644
88 index 0000000..9754ddf
89 --- /dev/null
90 +++ b/pit/upstream-bzip2/Makefile
91 @@ -0,0 +1,217 @@
92 +# ------------------------------------------------------------------
93 +# This file is part of bzip2/libbzip2, a program and library for
94 +# lossless, block-sorting data compression.
95 +#
96 +# bzip2/libbzip2 version 1.0.6 of 6 September 2010
97 +# Copyright (C) 1996-2010 Julian Seward <jseward@bzip.org>
98 +#
99 +# Please read the WARNING, DISCLAIMER and PATENTS sections in the 
100 +# README file.
101 +#
102 +# This program is released under the terms of the license contained
103 +# in the file LICENSE.
104 +# ------------------------------------------------------------------
105 +
106 +SHELL=/bin/sh
107 +
108 +# To assist in cross-compiling
109 +CC=gcc
110 +AR=ar
111 +RANLIB=ranlib
112 +LDFLAGS=
113 +
114 +BIGFILES=-D_FILE_OFFSET_BITS=64
115 +CFLAGS=-Wall -Winline -O2 -g $(BIGFILES)
116 +
117 +# Where you want it installed when you do 'make install'
118 +PREFIX=/usr/local
119 +
120 +
121 +OBJS= blocksort.o  \
122 +      huffman.o    \
123 +      crctable.o   \
124 +      randtable.o  \
125 +      compress.o   \
126 +      decompress.o \
127 +      bzlib.o
128 +
129 +all: libbz2.a bzip2 bzip2recover test
130 +
131 +bzip2: libbz2.a bzip2.o
132 +       $(CC) $(CFLAGS) $(LDFLAGS) -o bzip2 bzip2.o -L. -lbz2
133 +
134 +bzip2recover: bzip2recover.o
135 +       $(CC) $(CFLAGS) $(LDFLAGS) -o bzip2recover bzip2recover.o
136 +
137 +libbz2.a: $(OBJS)
138 +       rm -f libbz2.a
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 ; \
144 +       fi
145 +
146 +check: test
147 +test: bzip2
148 +       @cat words1
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
161 +       @cat words3
162 +
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
201 +
202 +clean: 
203 +       rm -f *.o libbz2.a bzip2 bzip2recover \
204 +       sample1.rb2 sample2.rb2 sample3.rb2 \
205 +       sample1.tst sample2.tst sample3.tst
206 +
207 +blocksort.o: blocksort.c
208 +       @cat words0
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
220 +bzlib.o: bzlib.c
221 +       $(CC) $(CFLAGS) -c bzlib.c
222 +bzip2.o: bzip2.c
223 +       $(CC) $(CFLAGS) -c bzip2.c
224 +bzip2recover.o: bzip2recover.c
225 +       $(CC) $(CFLAGS) -c bzip2recover.c
226 +
227 +
228 +distclean: clean
229 +       rm -f manual.ps manual.html manual.pdf
230 +
231 +DISTNAME=bzip2-1.0.6
232 +dist: check manual
233 +       rm -f $(DISTNAME)
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
293 +
294 +# For rebuilding the manual from sources on my SuSE 9.1 box
295 +
296 +MANUAL_SRCS=   bz-common.xsl bz-fo.xsl bz-html.xsl bzip.css \
297 +               entities.xml manual.xml 
298 +
299 +manual: manual.html manual.ps manual.pdf
300 +
301 +manual.ps: $(MANUAL_SRCS)
302 +       ./xmlproc.sh -ps manual.xml
303 +
304 +manual.pdf: $(MANUAL_SRCS)
305 +       ./xmlproc.sh -pdf manual.xml
306 +
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
310 new file mode 100644
311 index 0000000..d0d662c
312 --- /dev/null
313 +++ b/pit/upstream-bzip2/blocksort.c
314 @@ -0,0 +1,1094 @@
315 +
316 +/*-------------------------------------------------------------*/
317 +/*--- Block sorting machinery                               ---*/
318 +/*---                                           blocksort.c ---*/
319 +/*-------------------------------------------------------------*/
320 +
321 +/* ------------------------------------------------------------------
322 +   This file is part of bzip2/libbzip2, a program and library for
323 +   lossless, block-sorting data compression.
324 +
325 +   bzip2/libbzip2 version 1.0.6 of 6 September 2010
326 +   Copyright (C) 1996-2010 Julian Seward <jseward@bzip.org>
327 +
328 +   Please read the WARNING, DISCLAIMER and PATENTS sections in the 
329 +   README file.
330 +
331 +   This program is released under the terms of the license contained
332 +   in the file LICENSE.
333 +   ------------------------------------------------------------------ */
334 +
335 +
336 +#include "bzlib_private.h"
337 +
338 +/*---------------------------------------------*/
339 +/*--- Fallback O(N log(N)^2) sorting        ---*/
340 +/*--- algorithm, for repetitive blocks      ---*/
341 +/*---------------------------------------------*/
342 +
343 +/*---------------------------------------------*/
344 +static 
345 +__inline__
346 +void fallbackSimpleSort ( UInt32* fmap, 
347 +                          UInt32* eclass, 
348 +                          Int32   lo, 
349 +                          Int32   hi )
350 +{
351 +   Int32 i, j, tmp;
352 +   UInt32 ec_tmp;
353 +
354 +   if (lo == hi) return;
355 +
356 +   if (hi - lo > 3) {
357 +      for ( i = hi-4; i >= lo; i-- ) {
358 +         tmp = fmap[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];
362 +         fmap[j-4] = tmp;
363 +      }
364 +   }
365 +
366 +   for ( i = hi-1; i >= lo; i-- ) {
367 +      tmp = fmap[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];
371 +      fmap[j-1] = tmp;
372 +   }
373 +}
374 +
375 +
376 +/*---------------------------------------------*/
377 +#define fswap(zz1, zz2) \
378 +   { Int32 zztmp = zz1; zz1 = zz2; zz2 = zztmp; }
379 +
380 +#define fvswap(zzp1, zzp2, zzn)       \
381 +{                                     \
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--;          \
388 +   }                                  \
389 +}
390 +
391 +
392 +#define fmin(a,b) ((a) < (b)) ? (a) : (b)
393 +
394 +#define fpush(lz,hz) { stackLo[sp] = lz; \
395 +                       stackHi[sp] = hz; \
396 +                       sp++; }
397 +
398 +#define fpop(lz,hz) { sp--;              \
399 +                      lz = stackLo[sp];  \
400 +                      hz = stackHi[sp]; }
401 +
402 +#define FALLBACK_QSORT_SMALL_THRESH 10
403 +#define FALLBACK_QSORT_STACK_SIZE   100
404 +
405 +
406 +static
407 +void fallbackQSort3 ( UInt32* fmap, 
408 +                      UInt32* eclass,
409 +                      Int32   loSt, 
410 +                      Int32   hiSt )
411 +{
412 +   Int32 unLo, unHi, ltLo, gtHi, n, m;
413 +   Int32 sp, lo, hi;
414 +   UInt32 med, r, r3;
415 +   Int32 stackLo[FALLBACK_QSORT_STACK_SIZE];
416 +   Int32 stackHi[FALLBACK_QSORT_STACK_SIZE];
417 +
418 +   r = 0;
419 +
420 +   sp = 0;
421 +   fpush ( loSt, hiSt );
422 +
423 +   while (sp > 0) {
424 +
425 +      AssertH ( sp < FALLBACK_QSORT_STACK_SIZE - 1, 1004 );
426 +
427 +      fpop ( lo, hi );
428 +      if (hi - lo < FALLBACK_QSORT_SMALL_THRESH) {
429 +         fallbackSimpleSort ( fmap, eclass, lo, hi );
430 +         continue;
431 +      }
432 +
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
438 +         book, chapter 35.
439 +      */
440 +      r = ((r * 7621) + 1) % 32768;
441 +      r3 = r % 3;
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]];
445 +
446 +      unLo = ltLo = lo;
447 +      unHi = gtHi = hi;
448 +
449 +      while (1) {
450 +         while (1) {
451 +            if (unLo > unHi) break;
452 +            n = (Int32)eclass[fmap[unLo]] - (Int32)med;
453 +            if (n == 0) { 
454 +               fswap(fmap[unLo], fmap[ltLo]); 
455 +               ltLo++; unLo++; 
456 +               continue; 
457 +            };
458 +            if (n > 0) break;
459 +            unLo++;
460 +         }
461 +         while (1) {
462 +            if (unLo > unHi) break;
463 +            n = (Int32)eclass[fmap[unHi]] - (Int32)med;
464 +            if (n == 0) { 
465 +               fswap(fmap[unHi], fmap[gtHi]); 
466 +               gtHi--; unHi--; 
467 +               continue; 
468 +            };
469 +            if (n < 0) break;
470 +            unHi--;
471 +         }
472 +         if (unLo > unHi) break;
473 +         fswap(fmap[unLo], fmap[unHi]); unLo++; unHi--;
474 +      }
475 +
476 +      AssertD ( unHi == unLo-1, "fallbackQSort3(2)" );
477 +
478 +      if (gtHi < ltLo) continue;
479 +
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);
482 +
483 +      n = lo + unLo - ltLo - 1;
484 +      m = hi - (gtHi - unHi) + 1;
485 +
486 +      if (n - lo > hi - m) {
487 +         fpush ( lo, n );
488 +         fpush ( m, hi );
489 +      } else {
490 +         fpush ( m, hi );
491 +         fpush ( lo, n );
492 +      }
493 +   }
494 +}
495 +
496 +#undef fmin
497 +#undef fpush
498 +#undef fpop
499 +#undef fswap
500 +#undef fvswap
501 +#undef FALLBACK_QSORT_SMALL_THRESH
502 +#undef FALLBACK_QSORT_STACK_SIZE
503 +
504 +
505 +/*---------------------------------------------*/
506 +/* Pre:
507 +      nblock > 0
508 +      eclass exists for [0 .. nblock-1]
509 +      ((UChar*)eclass) [0 .. nblock-1] holds block
510 +      ptr exists for [0 .. nblock-1]
511 +
512 +   Post:
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
517 +*/
518 +
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)
524 +
525 +static
526 +void fallbackSort ( UInt32* fmap, 
527 +                    UInt32* eclass, 
528 +                    UInt32* bhtab,
529 +                    Int32   nblock,
530 +                    Int32   verb )
531 +{
532 +   Int32 ftab[257];
533 +   Int32 ftabCopy[256];
534 +   Int32 H, i, j, k, l, r, cc, cc1;
535 +   Int32 nNotDone;
536 +   Int32 nBhtab;
537 +   UChar* eclass8 = (UChar*)eclass;
538 +
539 +   /*--
540 +      Initial 1-char radix sort to generate
541 +      initial fmap and initial BH bits.
542 +   --*/
543 +   if (verb >= 4)
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];
549 +
550 +   for (i = 0; i < nblock; i++) {
551 +      j = eclass8[i];
552 +      k = ftab[j] - 1;
553 +      ftab[j] = k;
554 +      fmap[k] = i;
555 +   }
556 +
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]);
560 +
561 +   /*--
562 +      Inductively refine the buckets.  Kind-of an
563 +      "exponential radix sort" (!), inspired by the
564 +      Manber-Myers suffix array construction algorithm.
565 +   --*/
566 +
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);
571 +   }
572 +
573 +   /*-- the log(N) loop --*/
574 +   H = 1;
575 +   while (1) {
576 +
577 +      if (verb >= 4) 
578 +         VPrintf1 ( "        depth %6d has ", H );
579 +
580 +      j = 0;
581 +      for (i = 0; i < nblock; i++) {
582 +         if (ISSET_BH(i)) j = i;
583 +         k = fmap[i] - H; if (k < 0) k += nblock;
584 +         eclass[k] = j;
585 +      }
586 +
587 +      nNotDone = 0;
588 +      r = -1;
589 +      while (1) {
590 +
591 +        /*-- find the next non-singleton bucket --*/
592 +         k = r + 1;
593 +         while (ISSET_BH(k) && UNALIGNED_BH(k)) k++;
594 +         if (ISSET_BH(k)) {
595 +            while (WORD_BH(k) == 0xffffffff) k += 32;
596 +            while (ISSET_BH(k)) k++;
597 +         }
598 +         l = k - 1;
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++;
604 +         }
605 +         r = k - 1;
606 +         if (r >= nblock) break;
607 +
608 +         /*-- now [l, r] bracket current bucket --*/
609 +         if (r > l) {
610 +            nNotDone += (r - l + 1);
611 +            fallbackQSort3 ( fmap, eclass, l, r );
612 +
613 +            /*-- scan bucket and generate header bits-- */
614 +            cc = -1;
615 +            for (i = l; i <= r; i++) {
616 +               cc1 = eclass[fmap[i]];
617 +               if (cc != cc1) { SET_BH(i); cc = cc1; };
618 +            }
619 +         }
620 +      }
621 +
622 +      if (verb >= 4) 
623 +         VPrintf1 ( "%6d unresolved strings\n", nNotDone );
624 +
625 +      H *= 2;
626 +      if (H > nblock || nNotDone == 0) break;
627 +   }
628 +
629 +   /*-- 
630 +      Reconstruct the original block in
631 +      eclass8 [0 .. nblock-1], since the
632 +      previous phase destroyed it.
633 +   --*/
634 +   if (verb >= 4)
635 +      VPrintf0 ( "        reconstructing block ...\n" );
636 +   j = 0;
637 +   for (i = 0; i < nblock; i++) {
638 +      while (ftabCopy[j] == 0) j++;
639 +      ftabCopy[j]--;
640 +      eclass8[fmap[i]] = (UChar)j;
641 +   }
642 +   AssertH ( j < 256, 1005 );
643 +}
644 +
645 +#undef       SET_BH
646 +#undef     CLEAR_BH
647 +#undef     ISSET_BH
648 +#undef      WORD_BH
649 +#undef UNALIGNED_BH
650 +
651 +
652 +/*---------------------------------------------*/
653 +/*--- The main, O(N^2 log(N)) sorting       ---*/
654 +/*--- algorithm.  Faster for "normal"       ---*/
655 +/*--- non-repetitive blocks.                ---*/
656 +/*---------------------------------------------*/
657 +
658 +/*---------------------------------------------*/
659 +static
660 +__inline__
661 +Bool mainGtU ( UInt32  i1, 
662 +               UInt32  i2,
663 +               UChar*  block, 
664 +               UInt16* quadrant,
665 +               UInt32  nblock,
666 +               Int32*  budget )
667 +{
668 +   Int32  k;
669 +   UChar  c1, c2;
670 +   UInt16 s1, s2;
671 +
672 +   AssertD ( i1 != i2, "mainGtU" );
673 +   /* 1 */
674 +   c1 = block[i1]; c2 = block[i2];
675 +   if (c1 != c2) return (c1 > c2);
676 +   i1++; i2++;
677 +   /* 2 */
678 +   c1 = block[i1]; c2 = block[i2];
679 +   if (c1 != c2) return (c1 > c2);
680 +   i1++; i2++;
681 +   /* 3 */
682 +   c1 = block[i1]; c2 = block[i2];
683 +   if (c1 != c2) return (c1 > c2);
684 +   i1++; i2++;
685 +   /* 4 */
686 +   c1 = block[i1]; c2 = block[i2];
687 +   if (c1 != c2) return (c1 > c2);
688 +   i1++; i2++;
689 +   /* 5 */
690 +   c1 = block[i1]; c2 = block[i2];
691 +   if (c1 != c2) return (c1 > c2);
692 +   i1++; i2++;
693 +   /* 6 */
694 +   c1 = block[i1]; c2 = block[i2];
695 +   if (c1 != c2) return (c1 > c2);
696 +   i1++; i2++;
697 +   /* 7 */
698 +   c1 = block[i1]; c2 = block[i2];
699 +   if (c1 != c2) return (c1 > c2);
700 +   i1++; i2++;
701 +   /* 8 */
702 +   c1 = block[i1]; c2 = block[i2];
703 +   if (c1 != c2) return (c1 > c2);
704 +   i1++; i2++;
705 +   /* 9 */
706 +   c1 = block[i1]; c2 = block[i2];
707 +   if (c1 != c2) return (c1 > c2);
708 +   i1++; i2++;
709 +   /* 10 */
710 +   c1 = block[i1]; c2 = block[i2];
711 +   if (c1 != c2) return (c1 > c2);
712 +   i1++; i2++;
713 +   /* 11 */
714 +   c1 = block[i1]; c2 = block[i2];
715 +   if (c1 != c2) return (c1 > c2);
716 +   i1++; i2++;
717 +   /* 12 */
718 +   c1 = block[i1]; c2 = block[i2];
719 +   if (c1 != c2) return (c1 > c2);
720 +   i1++; i2++;
721 +
722 +   k = nblock + 8;
723 +
724 +   do {
725 +      /* 1 */
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);
730 +      i1++; i2++;
731 +      /* 2 */
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);
736 +      i1++; i2++;
737 +      /* 3 */
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);
742 +      i1++; i2++;
743 +      /* 4 */
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);
748 +      i1++; i2++;
749 +      /* 5 */
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);
754 +      i1++; i2++;
755 +      /* 6 */
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);
760 +      i1++; i2++;
761 +      /* 7 */
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);
766 +      i1++; i2++;
767 +      /* 8 */
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);
772 +      i1++; i2++;
773 +
774 +      if (i1 >= nblock) i1 -= nblock;
775 +      if (i2 >= nblock) i2 -= nblock;
776 +
777 +      k -= 8;
778 +      (*budget)--;
779 +   }
780 +      while (k >= 0);
781 +
782 +   return False;
783 +}
784 +
785 +
786 +/*---------------------------------------------*/
787 +/*--
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.
792 +--*/
793 +static
794 +Int32 incs[14] = { 1, 4, 13, 40, 121, 364, 1093, 3280,
795 +                   9841, 29524, 88573, 265720,
796 +                   797161, 2391484 };
797 +
798 +static
799 +void mainSimpleSort ( UInt32* ptr,
800 +                      UChar*  block,
801 +                      UInt16* quadrant,
802 +                      Int32   nblock,
803 +                      Int32   lo, 
804 +                      Int32   hi, 
805 +                      Int32   d,
806 +                      Int32*  budget )
807 +{
808 +   Int32 i, j, h, bigN, hp;
809 +   UInt32 v;
810 +
811 +   bigN = hi - lo + 1;
812 +   if (bigN < 2) return;
813 +
814 +   hp = 0;
815 +   while (incs[hp] < bigN) hp++;
816 +   hp--;
817 +
818 +   for (; hp >= 0; hp--) {
819 +      h = incs[hp];
820 +
821 +      i = lo + h;
822 +      while (True) {
823 +
824 +         /*-- copy 1 --*/
825 +         if (i > hi) break;
826 +         v = ptr[i];
827 +         j = i;
828 +         while ( mainGtU ( 
829 +                    ptr[j-h]+d, v+d, block, quadrant, nblock, budget 
830 +                 ) ) {
831 +            ptr[j] = ptr[j-h];
832 +            j = j - h;
833 +            if (j <= (lo + h - 1)) break;
834 +         }
835 +         ptr[j] = v;
836 +         i++;
837 +
838 +         /*-- copy 2 --*/
839 +         if (i > hi) break;
840 +         v = ptr[i];
841 +         j = i;
842 +         while ( mainGtU ( 
843 +                    ptr[j-h]+d, v+d, block, quadrant, nblock, budget 
844 +                 ) ) {
845 +            ptr[j] = ptr[j-h];
846 +            j = j - h;
847 +            if (j <= (lo + h - 1)) break;
848 +         }
849 +         ptr[j] = v;
850 +         i++;
851 +
852 +         /*-- copy 3 --*/
853 +         if (i > hi) break;
854 +         v = ptr[i];
855 +         j = i;
856 +         while ( mainGtU ( 
857 +                    ptr[j-h]+d, v+d, block, quadrant, nblock, budget 
858 +                 ) ) {
859 +            ptr[j] = ptr[j-h];
860 +            j = j - h;
861 +            if (j <= (lo + h - 1)) break;
862 +         }
863 +         ptr[j] = v;
864 +         i++;
865 +
866 +         if (*budget < 0) return;
867 +      }
868 +   }
869 +}
870 +
871 +
872 +/*---------------------------------------------*/
873 +/*--
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.
879 +--*/
880 +
881 +#define mswap(zz1, zz2) \
882 +   { Int32 zztmp = zz1; zz1 = zz2; zz2 = zztmp; }
883 +
884 +#define mvswap(zzp1, zzp2, zzn)       \
885 +{                                     \
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--;          \
892 +   }                                  \
893 +}
894 +
895 +static 
896 +__inline__
897 +UChar mmed3 ( UChar a, UChar b, UChar c )
898 +{
899 +   UChar t;
900 +   if (a > b) { t = a; a = b; b = t; };
901 +   if (b > c) { 
902 +      b = c;
903 +      if (a > b) b = a;
904 +   }
905 +   return b;
906 +}
907 +
908 +#define mmin(a,b) ((a) < (b)) ? (a) : (b)
909 +
910 +#define mpush(lz,hz,dz) { stackLo[sp] = lz; \
911 +                          stackHi[sp] = hz; \
912 +                          stackD [sp] = dz; \
913 +                          sp++; }
914 +
915 +#define mpop(lz,hz,dz) { sp--;             \
916 +                         lz = stackLo[sp]; \
917 +                         hz = stackHi[sp]; \
918 +                         dz = stackD [sp]; }
919 +
920 +
921 +#define mnextsize(az) (nextHi[az]-nextLo[az])
922 +
923 +#define mnextswap(az,bz)                                        \
924 +   { Int32 tz;                                                  \
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; }
928 +
929 +
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
933 +
934 +static
935 +void mainQSort3 ( UInt32* ptr,
936 +                  UChar*  block,
937 +                  UInt16* quadrant,
938 +                  Int32   nblock,
939 +                  Int32   loSt, 
940 +                  Int32   hiSt, 
941 +                  Int32   dSt,
942 +                  Int32*  budget )
943 +{
944 +   Int32 unLo, unHi, ltLo, gtHi, n, m, med;
945 +   Int32 sp, lo, hi, d;
946 +
947 +   Int32 stackLo[MAIN_QSORT_STACK_SIZE];
948 +   Int32 stackHi[MAIN_QSORT_STACK_SIZE];
949 +   Int32 stackD [MAIN_QSORT_STACK_SIZE];
950 +
951 +   Int32 nextLo[3];
952 +   Int32 nextHi[3];
953 +   Int32 nextD [3];
954 +
955 +   sp = 0;
956 +   mpush ( loSt, hiSt, dSt );
957 +
958 +   while (sp > 0) {
959 +
960 +      AssertH ( sp < MAIN_QSORT_STACK_SIZE - 2, 1001 );
961 +
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;
967 +         continue;
968 +      }
969 +
970 +      med = (Int32) 
971 +            mmed3 ( block[ptr[ lo         ]+d],
972 +                    block[ptr[ hi         ]+d],
973 +                    block[ptr[ (lo+hi)>>1 ]+d] );
974 +
975 +      unLo = ltLo = lo;
976 +      unHi = gtHi = hi;
977 +
978 +      while (True) {
979 +         while (True) {
980 +            if (unLo > unHi) break;
981 +            n = ((Int32)block[ptr[unLo]+d]) - med;
982 +            if (n == 0) { 
983 +               mswap(ptr[unLo], ptr[ltLo]); 
984 +               ltLo++; unLo++; continue; 
985 +            };
986 +            if (n >  0) break;
987 +            unLo++;
988 +         }
989 +         while (True) {
990 +            if (unLo > unHi) break;
991 +            n = ((Int32)block[ptr[unHi]+d]) - med;
992 +            if (n == 0) { 
993 +               mswap(ptr[unHi], ptr[gtHi]); 
994 +               gtHi--; unHi--; continue; 
995 +            };
996 +            if (n <  0) break;
997 +            unHi--;
998 +         }
999 +         if (unLo > unHi) break;
1000 +         mswap(ptr[unLo], ptr[unHi]); unLo++; unHi--;
1001 +      }
1002 +
1003 +      AssertD ( unHi == unLo-1, "mainQSort3(2)" );
1004 +
1005 +      if (gtHi < ltLo) {
1006 +         mpush(lo, hi, d+1 );
1007 +         continue;
1008 +      }
1009 +
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);
1012 +
1013 +      n = lo + unLo - ltLo - 1;
1014 +      m = hi - (gtHi - unHi) + 1;
1015 +
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;
1019 +
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);
1023 +
1024 +      AssertD (mnextsize(0) >= mnextsize(1), "mainQSort3(8)" );
1025 +      AssertD (mnextsize(1) >= mnextsize(2), "mainQSort3(9)" );
1026 +
1027 +      mpush (nextLo[0], nextHi[0], nextD[0]);
1028 +      mpush (nextLo[1], nextHi[1], nextD[1]);
1029 +      mpush (nextLo[2], nextHi[2], nextD[2]);
1030 +   }
1031 +}
1032 +
1033 +#undef mswap
1034 +#undef mvswap
1035 +#undef mpush
1036 +#undef mpop
1037 +#undef mmin
1038 +#undef mnextsize
1039 +#undef mnextswap
1040 +#undef MAIN_QSORT_SMALL_THRESH
1041 +#undef MAIN_QSORT_DEPTH_THRESH
1042 +#undef MAIN_QSORT_STACK_SIZE
1043 +
1044 +
1045 +/*---------------------------------------------*/
1046 +/* Pre:
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]
1051 +
1052 +   Post:
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
1058 +*/
1059 +
1060 +#define BIGFREQ(b) (ftab[((b)+1) << 8] - ftab[(b) << 8])
1061 +#define SETMASK (1 << 21)
1062 +#define CLEARMASK (~(SETMASK))
1063 +
1064 +static
1065 +void mainSort ( UInt32* ptr, 
1066 +                UChar*  block,
1067 +                UInt16* quadrant, 
1068 +                UInt32* ftab,
1069 +                Int32   nblock,
1070 +                Int32   verb,
1071 +                Int32*  budget )
1072 +{
1073 +   Int32  i, j, k, ss, sb;
1074 +   Int32  runningOrder[256];
1075 +   Bool   bigDone[256];
1076 +   Int32  copyStart[256];
1077 +   Int32  copyEnd  [256];
1078 +   UChar  c1;
1079 +   Int32  numQSorted;
1080 +   UInt16 s;
1081 +   if (verb >= 4) VPrintf0 ( "        main sort initialise ...\n" );
1082 +
1083 +   /*-- set up the 2-byte frequency table --*/
1084 +   for (i = 65536; i >= 0; i--) ftab[i] = 0;
1085 +
1086 +   j = block[0] << 8;
1087 +   i = nblock-1;
1088 +   for (; i >= 3; i -= 4) {
1089 +      quadrant[i] = 0;
1090 +      j = (j >> 8) | ( ((UInt16)block[i]) << 8);
1091 +      ftab[j]++;
1092 +      quadrant[i-1] = 0;
1093 +      j = (j >> 8) | ( ((UInt16)block[i-1]) << 8);
1094 +      ftab[j]++;
1095 +      quadrant[i-2] = 0;
1096 +      j = (j >> 8) | ( ((UInt16)block[i-2]) << 8);
1097 +      ftab[j]++;
1098 +      quadrant[i-3] = 0;
1099 +      j = (j >> 8) | ( ((UInt16)block[i-3]) << 8);
1100 +      ftab[j]++;
1101 +   }
1102 +   for (; i >= 0; i--) {
1103 +      quadrant[i] = 0;
1104 +      j = (j >> 8) | ( ((UInt16)block[i]) << 8);
1105 +      ftab[j]++;
1106 +   }
1107 +
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;
1112 +   }
1113 +
1114 +   if (verb >= 4) VPrintf0 ( "        bucket sorting ...\n" );
1115 +
1116 +   /*-- Complete the initial radix sort --*/
1117 +   for (i = 1; i <= 65536; i++) ftab[i] += ftab[i-1];
1118 +
1119 +   s = block[0] << 8;
1120 +   i = nblock-1;
1121 +   for (; i >= 3; i -= 4) {
1122 +      s = (s >> 8) | (block[i] << 8);
1123 +      j = ftab[s] -1;
1124 +      ftab[s] = j;
1125 +      ptr[j] = i;
1126 +      s = (s >> 8) | (block[i-1] << 8);
1127 +      j = ftab[s] -1;
1128 +      ftab[s] = j;
1129 +      ptr[j] = i-1;
1130 +      s = (s >> 8) | (block[i-2] << 8);
1131 +      j = ftab[s] -1;
1132 +      ftab[s] = j;
1133 +      ptr[j] = i-2;
1134 +      s = (s >> 8) | (block[i-3] << 8);
1135 +      j = ftab[s] -1;
1136 +      ftab[s] = j;
1137 +      ptr[j] = i-3;
1138 +   }
1139 +   for (; i >= 0; i--) {
1140 +      s = (s >> 8) | (block[i] << 8);
1141 +      j = ftab[s] -1;
1142 +      ftab[s] = j;
1143 +      ptr[j] = i;
1144 +   }
1145 +
1146 +   /*--
1147 +      Now ftab contains the first loc of every small bucket.
1148 +      Calculate the running order, from smallest to largest
1149 +      big bucket.
1150 +   --*/
1151 +   for (i = 0; i <= 255; i++) {
1152 +      bigDone     [i] = False;
1153 +      runningOrder[i] = i;
1154 +   }
1155 +
1156 +   {
1157 +      Int32 vv;
1158 +      Int32 h = 1;
1159 +      do h = 3 * h + 1; while (h <= 256);
1160 +      do {
1161 +         h = h / 3;
1162 +         for (i = h; i <= 255; i++) {
1163 +            vv = runningOrder[i];
1164 +            j = i;
1165 +            while ( BIGFREQ(runningOrder[j-h]) > BIGFREQ(vv) ) {
1166 +               runningOrder[j] = runningOrder[j-h];
1167 +               j = j - h;
1168 +               if (j <= (h - 1)) goto zero;
1169 +            }
1170 +            zero:
1171 +            runningOrder[j] = vv;
1172 +         }
1173 +      } while (h != 1);
1174 +   }
1175 +
1176 +   /*--
1177 +      The main sorting loop.
1178 +   --*/
1179 +
1180 +   numQSorted = 0;
1181 +
1182 +   for (i = 0; i <= 255; i++) {
1183 +
1184 +      /*--
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.
1189 +      --*/
1190 +      ss = runningOrder[i];
1191 +
1192 +      /*--
1193 +         Step 1:
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.
1199 +      --*/
1200 +      for (j = 0; j <= 255; j++) {
1201 +         if (j != ss) {
1202 +            sb = (ss << 8) + j;
1203 +            if ( ! (ftab[sb] & SETMASK) ) {
1204 +               Int32 lo = ftab[sb]   & CLEARMASK;
1205 +               Int32 hi = (ftab[sb+1] & CLEARMASK) - 1;
1206 +               if (hi > lo) {
1207 +                  if (verb >= 4)
1208 +                     VPrintf4 ( "        qsort [0x%x, 0x%x]   "
1209 +                                "done %d   this %d\n",
1210 +                                ss, j, numQSorted, hi - lo + 1 );
1211 +                  mainQSort3 ( 
1212 +                     ptr, block, quadrant, nblock, 
1213 +                     lo, hi, BZ_N_RADIX, budget 
1214 +                  );   
1215 +                  numQSorted += (hi - lo + 1);
1216 +                  if (*budget < 0) return;
1217 +               }
1218 +            }
1219 +            ftab[sb] |= SETMASK;
1220 +         }
1221 +      }
1222 +
1223 +      AssertH ( !bigDone[ss], 1006 );
1224 +
1225 +      /*--
1226 +         Step 2:
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.
1231 +      --*/
1232 +      {
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;
1236 +         }
1237 +         for (j = ftab[ss << 8] & CLEARMASK; j < copyStart[ss]; j++) {
1238 +            k = ptr[j]-1; if (k < 0) k += nblock;
1239 +            c1 = block[k];
1240 +            if (!bigDone[c1])
1241 +               ptr[ copyStart[c1]++ ] = k;
1242 +         }
1243 +         for (j = (ftab[(ss+1) << 8] & CLEARMASK) - 1; j > copyEnd[ss]; j--) {
1244 +            k = ptr[j]-1; if (k < 0) k += nblock;
1245 +            c1 = block[k];
1246 +            if (!bigDone[c1]) 
1247 +               ptr[ copyEnd[c1]-- ] = k;
1248 +         }
1249 +      }
1250 +
1251 +      AssertH ( (copyStart[ss]-1 == copyEnd[ss])
1252 +                || 
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),
1258 +                1007 )
1259 +
1260 +      for (j = 0; j <= 255; j++) ftab[(j << 8) + ss] |= SETMASK;
1261 +
1262 +      /*--
1263 +         Step 3:
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.
1270 +
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).
1277 +
1278 +         The precise meaning is: at all times:
1279 +
1280 +            for 0 <= i < nblock and 0 <= j <= nblock
1281 +
1282 +            if block[i] != block[j], 
1283 +
1284 +               then the relative values of quadrant[i] and 
1285 +                    quadrant[j] are meaningless.
1286 +
1287 +               else {
1288 +                  if quadrant[i] < quadrant[j]
1289 +                     then the string starting at i lexicographically
1290 +                     precedes the string starting at j
1291 +
1292 +                  else if quadrant[i] > quadrant[j]
1293 +                     then the string starting at j lexicographically
1294 +                     precedes the string starting at i
1295 +
1296 +                  else
1297 +                     the relative ordering of the strings starting
1298 +                     at i and j has not yet been determined.
1299 +               }
1300 +      --*/
1301 +      bigDone[ss] = True;
1302 +
1303 +      if (i < 255) {
1304 +         Int32 bbStart  = ftab[ss << 8] & CLEARMASK;
1305 +         Int32 bbSize   = (ftab[(ss+1) << 8] & CLEARMASK) - bbStart;
1306 +         Int32 shifts   = 0;
1307 +
1308 +         while ((bbSize >> shifts) > 65534) shifts++;
1309 +
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;
1316 +         }
1317 +         AssertH ( ((bbSize-1) >> shifts) <= 65535, 1002 );
1318 +      }
1319 +
1320 +   }
1321 +
1322 +   if (verb >= 4)
1323 +      VPrintf3 ( "        %d pointers, %d sorted, %d scanned\n",
1324 +                 nblock, numQSorted, nblock - numQSorted );
1325 +}
1326 +
1327 +#undef BIGFREQ
1328 +#undef SETMASK
1329 +#undef CLEARMASK
1330 +
1331 +
1332 +/*---------------------------------------------*/
1333 +/* Pre:
1334 +      nblock > 0
1335 +      arr2 exists for [0 .. nblock-1 +N_OVERSHOOT]
1336 +      ((UChar*)arr2)  [0 .. nblock-1] holds block
1337 +      arr1 exists for [0 .. nblock-1]
1338 +
1339 +   Post:
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
1344 +*/
1345 +void BZ2_blockSort ( EState* s )
1346 +{
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;
1353 +   UInt16* quadrant;
1354 +   Int32   budget;
1355 +   Int32   budgetInit;
1356 +   Int32   i;
1357 +
1358 +   if (nblock < 10000) {
1359 +      fallbackSort ( s->arr1, s->arr2, ftab, nblock, verb );
1360 +   } else {
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.
1365 +      */
1366 +      i = nblock+BZ_N_OVERSHOOT;
1367 +      if (i & 1) i++;
1368 +      quadrant = (UInt16*)(&(block[i]));
1369 +
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.
1376 +      */
1377 +      if (wfact < 1  ) wfact = 1;
1378 +      if (wfact > 100) wfact = 100;
1379 +      budgetInit = nblock * ((wfact-1) / 3);
1380 +      budget = budgetInit;
1381 +
1382 +      mainSort ( ptr, block, quadrant, ftab, nblock, verb, &budget );
1383 +      if (verb >= 3) 
1384 +         VPrintf3 ( "      %d work, %d block, ratio %5.2f\n",
1385 +                    budgetInit - budget,
1386 +                    nblock, 
1387 +                    (float)(budgetInit - budget) /
1388 +                    (float)(nblock==0 ? 1 : nblock) ); 
1389 +      if (budget < 0) {
1390 +         if (verb >= 2) 
1391 +            VPrintf0 ( "    too repetitive; using fallback"
1392 +                       " sorting algorithm\n" );
1393 +         fallbackSort ( s->arr1, s->arr2, ftab, nblock, verb );
1394 +      }
1395 +   }
1396 +
1397 +   s->origPtr = -1;
1398 +   for (i = 0; i < s->nblock; i++)
1399 +      if (ptr[i] == 0)
1400 +         { s->origPtr = i; break; };
1401 +
1402 +   AssertH( s->origPtr != -1, 1003 );
1403 +}
1404 +
1405 +
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
1412 --- /dev/null
1413 +++ b/pit/upstream-bzip2/bzip2.c
1414 @@ -0,0 +1,2034 @@
1415 +
1416 +/*-----------------------------------------------------------*/
1417 +/*--- A block-sorting, lossless compressor        bzip2.c ---*/
1418 +/*-----------------------------------------------------------*/
1419 +
1420 +/* ------------------------------------------------------------------
1421 +   This file is part of bzip2/libbzip2, a program and library for
1422 +   lossless, block-sorting data compression.
1423 +
1424 +   bzip2/libbzip2 version 1.0.6 of 6 September 2010
1425 +   Copyright (C) 1996-2010 Julian Seward <jseward@bzip.org>
1426 +
1427 +   Please read the WARNING, DISCLAIMER and PATENTS sections in the 
1428 +   README file.
1429 +
1430 +   This program is released under the terms of the license contained
1431 +   in the file LICENSE.
1432 +   ------------------------------------------------------------------ */
1433 +
1434 +
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.
1439 +*/
1440 +#define BZ_UNIX      1
1441 +
1442 +/*--
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.
1448 +--*/
1449 +#define BZ_LCCWIN32  0
1450 +
1451 +#if defined(_WIN32) && !defined(__CYGWIN__)
1452 +#undef  BZ_LCCWIN32
1453 +#define BZ_LCCWIN32 1
1454 +#undef  BZ_UNIX
1455 +#define BZ_UNIX 0
1456 +#endif
1457 +
1458 +
1459 +/*---------------------------------------------*/
1460 +/*--
1461 +  Some stuff for all platforms.
1462 +--*/
1463 +
1464 +#include <stdio.h>
1465 +#include <stdlib.h>
1466 +#include <string.h>
1467 +#include <signal.h>
1468 +#include <math.h>
1469 +#include <errno.h>
1470 +#include <ctype.h>
1471 +#include "bzlib.h"
1472 +
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(); }
1476 +
1477 +
1478 +/*---------------------------------------------*/
1479 +/*--
1480 +   Platform-specific stuff.
1481 +--*/
1482 +
1483 +#if BZ_UNIX
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>
1490 +
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
1496 +
1497 +#   define APPEND_FILESPEC(root, name) \
1498 +      root=snocString((root), (name))
1499 +
1500 +#   define APPEND_FLAG(root, name) \
1501 +      root=snocString((root), (name))
1502 +
1503 +#   define SET_BINARY_MODE(fd) /**/
1504 +
1505 +#   ifdef __GNUC__
1506 +#      define NORETURN __attribute__ ((noreturn))
1507 +#   else
1508 +#      define NORETURN /**/
1509 +#   endif
1510 +
1511 +#   ifdef __DJGPP__
1512 +#     include <io.h>
1513 +#     include <fcntl.h>
1514 +#     undef MY_LSTAT
1515 +#     undef MY_STAT
1516 +#     define MY_LSTAT stat
1517 +#     define MY_STAT stat
1518 +#     undef SET_BINARY_MODE
1519 +#     define SET_BINARY_MODE(fd)                        \
1520 +        do {                                            \
1521 +           int retVal = setmode ( fileno ( fd ),        \
1522 +                                  O_BINARY );           \
1523 +           ERROR_IF_MINUS_ONE ( retVal );               \
1524 +        } while ( 0 )
1525 +#   endif
1526 +
1527 +#   ifdef __CYGWIN__
1528 +#     include <io.h>
1529 +#     include <fcntl.h>
1530 +#     undef SET_BINARY_MODE
1531 +#     define SET_BINARY_MODE(fd)                        \
1532 +        do {                                            \
1533 +           int retVal = setmode ( fileno ( fd ),        \
1534 +                                  O_BINARY );           \
1535 +           ERROR_IF_MINUS_ONE ( retVal );               \
1536 +        } while ( 0 )
1537 +#   endif
1538 +#endif /* BZ_UNIX */
1539 +
1540 +
1541 +
1542 +#if BZ_LCCWIN32
1543 +#   include <io.h>
1544 +#   include <fcntl.h>
1545 +#   include <sys\stat.h>
1546 +
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)
1553 +
1554 +#   define APPEND_FLAG(root, name) \
1555 +      root=snocString((root), (name))
1556 +
1557 +#   define APPEND_FILESPEC(root, name)                \
1558 +      root = snocString ((root), (name))
1559 +
1560 +#   define SET_BINARY_MODE(fd)                        \
1561 +      do {                                            \
1562 +         int retVal = setmode ( fileno ( fd ),        \
1563 +                                O_BINARY );           \
1564 +         ERROR_IF_MINUS_ONE ( retVal );               \
1565 +      } while ( 0 )
1566 +
1567 +#endif /* BZ_LCCWIN32 */
1568 +
1569 +
1570 +/*---------------------------------------------*/
1571 +/*--
1572 +  Some more stuff for all platforms :-)
1573 +--*/
1574 +
1575 +typedef char            Char;
1576 +typedef unsigned char   Bool;
1577 +typedef unsigned char   UChar;
1578 +typedef int             Int32;
1579 +typedef unsigned int    UInt32;
1580 +typedef short           Int16;
1581 +typedef unsigned short  UInt16;
1582 +                                       
1583 +#define True  ((Bool)1)
1584 +#define False ((Bool)0)
1585 +
1586 +/*--
1587 +  IntNative is your platform's `native' int size.
1588 +  Only here to avoid probs with 64-bit platforms.
1589 +--*/
1590 +typedef int IntNative;
1591 +
1592 +
1593 +/*---------------------------------------------------*/
1594 +/*--- Misc (file handling) data decls             ---*/
1595 +/*---------------------------------------------------*/
1596 +
1597 +Int32   verbosity;
1598 +Bool    keepInputFiles, smallMode, deleteOutputOnInterrupt;
1599 +Bool    forceOverwrite, testFailsExist, unzFailsExist, noisy;
1600 +Int32   numFileNames, numFilesProcessed, blockSize100k;
1601 +Int32   exitValue;
1602 +
1603 +/*-- source modes; F==file, I==stdin, O==stdout --*/
1604 +#define SM_I2O           1
1605 +#define SM_F2O           2
1606 +#define SM_F2F           3
1607 +
1608 +/*-- operation modes --*/
1609 +#define OM_Z             1
1610 +#define OM_UNZ           2
1611 +#define OM_TEST          3
1612 +
1613 +Int32   opMode;
1614 +Int32   srcMode;
1615 +
1616 +#define FILE_NAME_LEN 1034
1617 +
1618 +Int32   longestFileName;
1619 +Char    inName [FILE_NAME_LEN];
1620 +Char    outName[FILE_NAME_LEN];
1621 +Char    tmpName[FILE_NAME_LEN];
1622 +Char    *progName;
1623 +Char    progNameReally[FILE_NAME_LEN];
1624 +FILE    *outputHandleJustInCase;
1625 +Int32   workFactor;
1626 +
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;
1634 +
1635 +static void    copyFileName ( Char*, Char* );
1636 +static void*   myMalloc     ( Int32 );
1637 +static void    applySavedFileAttrToOutputFile ( IntNative fd );
1638 +
1639 +
1640 +
1641 +/*---------------------------------------------------*/
1642 +/*--- An implementation of 64-bit ints.  Sigh.    ---*/
1643 +/*--- Roll on widespread deployment of ANSI C9X ! ---*/
1644 +/*---------------------------------------------------*/
1645 +
1646 +typedef
1647 +   struct { UChar b[8]; } 
1648 +   UInt64;
1649 +
1650 +
1651 +static
1652 +void uInt64_from_UInt32s ( UInt64* n, UInt32 lo32, UInt32 hi32 )
1653 +{
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);
1662 +}
1663 +
1664 +
1665 +static
1666 +double uInt64_to_double ( UInt64* n )
1667 +{
1668 +   Int32  i;
1669 +   double base = 1.0;
1670 +   double sum  = 0.0;
1671 +   for (i = 0; i < 8; i++) {
1672 +      sum  += base * (double)(n->b[i]);
1673 +      base *= 256.0;
1674 +   }
1675 +   return sum;
1676 +}
1677 +
1678 +
1679 +static
1680 +Bool uInt64_isZero ( UInt64* n )
1681 +{
1682 +   Int32 i;
1683 +   for (i = 0; i < 8; i++)
1684 +      if (n->b[i] != 0) return 0;
1685 +   return 1;
1686 +}
1687 +
1688 +
1689 +/* Divide *n by 10, and return the remainder.  */
1690 +static 
1691 +Int32 uInt64_qrm10 ( UInt64* n )
1692 +{
1693 +   UInt32 rem, tmp;
1694 +   Int32  i;
1695 +   rem = 0;
1696 +   for (i = 7; i >= 0; i--) {
1697 +      tmp = rem * 256 + n->b[i];
1698 +      n->b[i] = tmp / 10;
1699 +      rem = tmp % 10;
1700 +   }
1701 +   return rem;
1702 +}
1703 +
1704 +
1705 +/* ... and the Whole Entire Point of all this UInt64 stuff is
1706 +   so that we can supply the following function.
1707 +*/
1708 +static
1709 +void uInt64_toAscii ( char* outbuf, UInt64* n )
1710 +{
1711 +   Int32  i, q;
1712 +   UChar  buf[32];
1713 +   Int32  nBuf   = 0;
1714 +   UInt64 n_copy = *n;
1715 +   do {
1716 +      q = uInt64_qrm10 ( &n_copy );
1717 +      buf[nBuf] = q + '0';
1718 +      nBuf++;
1719 +   } while (!uInt64_isZero(&n_copy));
1720 +   outbuf[nBuf] = 0;
1721 +   for (i = 0; i < nBuf; i++) 
1722 +      outbuf[i] = buf[nBuf-i-1];
1723 +}
1724 +
1725 +
1726 +/*---------------------------------------------------*/
1727 +/*--- Processing of complete files and streams    ---*/
1728 +/*---------------------------------------------------*/
1729 +
1730 +/*---------------------------------------------*/
1731 +static 
1732 +Bool myfeof ( FILE* f )
1733 +{
1734 +   Int32 c = fgetc ( f );
1735 +   if (c == EOF) return True;
1736 +   ungetc ( c, f );
1737 +   return False;
1738 +}
1739 +
1740 +
1741 +/*---------------------------------------------*/
1742 +static 
1743 +void compressStream ( FILE *stream, FILE *zStream )
1744 +{
1745 +   BZFILE* bzf = NULL;
1746 +   UChar   ibuf[5000];
1747 +   Int32   nIbuf;
1748 +   UInt32  nbytes_in_lo32, nbytes_in_hi32;
1749 +   UInt32  nbytes_out_lo32, nbytes_out_hi32;
1750 +   Int32   bzerr, bzerr_dummy, ret;
1751 +
1752 +   SET_BINARY_MODE(stream);
1753 +   SET_BINARY_MODE(zStream);
1754 +
1755 +   if (ferror(stream)) goto errhandler_io;
1756 +   if (ferror(zStream)) goto errhandler_io;
1757 +
1758 +   bzf = BZ2_bzWriteOpen ( &bzerr, zStream, 
1759 +                           blockSize100k, verbosity, workFactor );   
1760 +   if (bzerr != BZ_OK) goto errhandler;
1761 +
1762 +   if (verbosity >= 2) fprintf ( stderr, "\n" );
1763 +
1764 +   while (True) {
1765 +
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;
1771 +
1772 +   }
1773 +
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;
1778 +
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;
1789 +   }
1790 +   outputHandleJustInCase = NULL;
1791 +   if (ferror(stream)) goto errhandler_io;
1792 +   ret = fclose ( stream );
1793 +   if (ret == EOF) goto errhandler_io;
1794 +
1795 +   if (verbosity >= 1) {
1796 +      if (nbytes_in_lo32 == 0 && nbytes_in_hi32 == 0) {
1797 +        fprintf ( stderr, " no data compressed.\n");
1798 +      } else {
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),
1815 +                  buf_nin,
1816 +                  buf_nout
1817 +                );
1818 +      }
1819 +   }
1820 +
1821 +   return;
1822 +
1823 +   errhandler:
1824 +   BZ2_bzWriteClose64 ( &bzerr_dummy, bzf, 1, 
1825 +                        &nbytes_in_lo32, &nbytes_in_hi32,
1826 +                        &nbytes_out_lo32, &nbytes_out_hi32 );
1827 +   switch (bzerr) {
1828 +      case BZ_CONFIG_ERROR:
1829 +         configError(); break;
1830 +      case BZ_MEM_ERROR:
1831 +         outOfMemory (); break;
1832 +      case BZ_IO_ERROR:
1833 +         errhandler_io:
1834 +         ioError(); break;
1835 +      default:
1836 +         panic ( "compress:unexpected error" );
1837 +   }
1838 +
1839 +   panic ( "compress:end" );
1840 +   /*notreached*/
1841 +}
1842 +
1843 +
1844 +
1845 +/*---------------------------------------------*/
1846 +static 
1847 +Bool uncompressStream ( FILE *zStream, FILE *stream )
1848 +{
1849 +   BZFILE* bzf = NULL;
1850 +   Int32   bzerr, bzerr_dummy, ret, nread, streamNo, i;
1851 +   UChar   obuf[5000];
1852 +   UChar   unused[BZ_MAX_UNUSED];
1853 +   Int32   nUnused;
1854 +   void*   unusedTmpV;
1855 +   UChar*  unusedTmp;
1856 +
1857 +   nUnused = 0;
1858 +   streamNo = 0;
1859 +
1860 +   SET_BINARY_MODE(stream);
1861 +   SET_BINARY_MODE(zStream);
1862 +
1863 +   if (ferror(stream)) goto errhandler_io;
1864 +   if (ferror(zStream)) goto errhandler_io;
1865 +
1866 +   while (True) {
1867 +
1868 +      bzf = BZ2_bzReadOpen ( 
1869 +               &bzerr, zStream, verbosity, 
1870 +               (int)smallMode, unused, nUnused
1871 +            );
1872 +      if (bzf == NULL || bzerr != BZ_OK) goto errhandler;
1873 +      streamNo++;
1874 +
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;
1881 +      }
1882 +      if (bzerr != BZ_STREAM_END) goto errhandler;
1883 +
1884 +      BZ2_bzReadGetUnused ( &bzerr, bzf, &unusedTmpV, &nUnused );
1885 +      if (bzerr != BZ_OK) panic ( "decompress:bzReadGetUnused" );
1886 +
1887 +      unusedTmp = (UChar*)unusedTmpV;
1888 +      for (i = 0; i < nUnused; i++) unused[i] = unusedTmp[i];
1889 +
1890 +      BZ2_bzReadClose ( &bzerr, bzf );
1891 +      if (bzerr != BZ_OK) panic ( "decompress:bzReadGetUnused" );
1892 +
1893 +      if (nUnused == 0 && myfeof(zStream)) break;
1894 +   }
1895 +
1896 +   closeok:
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 );
1902 +   }
1903 +   ret = fclose ( zStream );
1904 +   if (ret == EOF) goto errhandler_io;
1905 +
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;
1913 +   }
1914 +   outputHandleJustInCase = NULL;
1915 +   if (verbosity >= 2) fprintf ( stderr, "\n    " );
1916 +   return True;
1917 +
1918 +   trycat: 
1919 +   if (forceOverwrite) {
1920 +      rewind(zStream);
1921 +      while (True) {
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;
1927 +      }
1928 +      goto closeok;
1929 +   }
1930 +  
1931 +   errhandler:
1932 +   BZ2_bzReadClose ( &bzerr_dummy, bzf );
1933 +   switch (bzerr) {
1934 +      case BZ_CONFIG_ERROR:
1935 +         configError(); break;
1936 +      case BZ_IO_ERROR:
1937 +         errhandler_io:
1938 +         ioError(); break;
1939 +      case BZ_DATA_ERROR:
1940 +         crcError();
1941 +      case BZ_MEM_ERROR:
1942 +         outOfMemory();
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) {
1949 +            return False;
1950 +         } else {
1951 +            if (noisy)
1952 +            fprintf ( stderr, 
1953 +                      "\n%s: %s: trailing garbage after EOF ignored\n",
1954 +                      progName, inName );
1955 +            return True;       
1956 +         }
1957 +      default:
1958 +         panic ( "decompress:unexpected error" );
1959 +   }
1960 +
1961 +   panic ( "decompress:end" );
1962 +   return True; /*notreached*/
1963 +}
1964 +
1965 +
1966 +/*---------------------------------------------*/
1967 +static 
1968 +Bool testStream ( FILE *zStream )
1969 +{
1970 +   BZFILE* bzf = NULL;
1971 +   Int32   bzerr, bzerr_dummy, ret, nread, streamNo, i;
1972 +   UChar   obuf[5000];
1973 +   UChar   unused[BZ_MAX_UNUSED];
1974 +   Int32   nUnused;
1975 +   void*   unusedTmpV;
1976 +   UChar*  unusedTmp;
1977 +
1978 +   nUnused = 0;
1979 +   streamNo = 0;
1980 +
1981 +   SET_BINARY_MODE(zStream);
1982 +   if (ferror(zStream)) goto errhandler_io;
1983 +
1984 +   while (True) {
1985 +
1986 +      bzf = BZ2_bzReadOpen ( 
1987 +               &bzerr, zStream, verbosity, 
1988 +               (int)smallMode, unused, nUnused
1989 +            );
1990 +      if (bzf == NULL || bzerr != BZ_OK) goto errhandler;
1991 +      streamNo++;
1992 +
1993 +      while (bzerr == BZ_OK) {
1994 +         nread = BZ2_bzRead ( &bzerr, bzf, obuf, 5000 );
1995 +         if (bzerr == BZ_DATA_ERROR_MAGIC) goto errhandler;
1996 +      }
1997 +      if (bzerr != BZ_STREAM_END) goto errhandler;
1998 +
1999 +      BZ2_bzReadGetUnused ( &bzerr, bzf, &unusedTmpV, &nUnused );
2000 +      if (bzerr != BZ_OK) panic ( "test:bzReadGetUnused" );
2001 +
2002 +      unusedTmp = (UChar*)unusedTmpV;
2003 +      for (i = 0; i < nUnused; i++) unused[i] = unusedTmp[i];
2004 +
2005 +      BZ2_bzReadClose ( &bzerr, bzf );
2006 +      if (bzerr != BZ_OK) panic ( "test:bzReadGetUnused" );
2007 +      if (nUnused == 0 && myfeof(zStream)) break;
2008 +
2009 +   }
2010 +
2011 +   if (ferror(zStream)) goto errhandler_io;
2012 +   ret = fclose ( zStream );
2013 +   if (ret == EOF) goto errhandler_io;
2014 +
2015 +   if (verbosity >= 2) fprintf ( stderr, "\n    " );
2016 +   return True;
2017 +
2018 +   errhandler:
2019 +   BZ2_bzReadClose ( &bzerr_dummy, bzf );
2020 +   if (verbosity == 0) 
2021 +      fprintf ( stderr, "%s: %s: ", progName, inName );
2022 +   switch (bzerr) {
2023 +      case BZ_CONFIG_ERROR:
2024 +         configError(); break;
2025 +      case BZ_IO_ERROR:
2026 +         errhandler_io:
2027 +         ioError(); break;
2028 +      case BZ_DATA_ERROR:
2029 +         fprintf ( stderr,
2030 +                   "data integrity (CRC) error in data\n" );
2031 +         return False;
2032 +      case BZ_MEM_ERROR:
2033 +         outOfMemory();
2034 +      case BZ_UNEXPECTED_EOF:
2035 +         fprintf ( stderr,
2036 +                   "file ends unexpectedly\n" );
2037 +         return False;
2038 +      case BZ_DATA_ERROR_MAGIC:
2039 +         if (zStream != stdin) fclose(zStream);
2040 +         if (streamNo == 1) {
2041 +          fprintf ( stderr, 
2042 +                    "bad magic number (file not created by bzip2)\n" );
2043 +            return False;
2044 +         } else {
2045 +            if (noisy)
2046 +            fprintf ( stderr, 
2047 +                      "trailing garbage after EOF ignored\n" );
2048 +            return True;       
2049 +         }
2050 +      default:
2051 +         panic ( "test:unexpected error" );
2052 +   }
2053 +
2054 +   panic ( "test:end" );
2055 +   return True; /*notreached*/
2056 +}
2057 +
2058 +
2059 +/*---------------------------------------------------*/
2060 +/*--- Error [non-] handling grunge                ---*/
2061 +/*---------------------------------------------------*/
2062 +
2063 +/*---------------------------------------------*/
2064 +static
2065 +void setExit ( Int32 v )
2066 +{
2067 +   if (v > exitValue) exitValue = v;
2068 +}
2069 +
2070 +
2071 +/*---------------------------------------------*/
2072 +static 
2073 +void cadvise ( void )
2074 +{
2075 +   if (noisy)
2076 +   fprintf (
2077 +      stderr,
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"
2082 +    );
2083 +}
2084 +
2085 +
2086 +/*---------------------------------------------*/
2087 +static 
2088 +void showFileNames ( void )
2089 +{
2090 +   if (noisy)
2091 +   fprintf (
2092 +      stderr,
2093 +      "\tInput file = %s, output file = %s\n",
2094 +      inName, outName 
2095 +   );
2096 +}
2097 +
2098 +
2099 +/*---------------------------------------------*/
2100 +static 
2101 +void cleanUpAndFail ( Int32 ec )
2102 +{
2103 +   IntNative      retVal;
2104 +   struct MY_STAT statBuf;
2105 +
2106 +   if ( srcMode == SM_F2F 
2107 +        && opMode != OM_TEST
2108 +        && deleteOutputOnInterrupt ) {
2109 +
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) {
2117 +         if (noisy)
2118 +            fprintf ( stderr, 
2119 +                      "%s: Deleting output file %s, if it exists.\n",
2120 +                      progName, outName );
2121 +         if (outputHandleJustInCase != NULL)
2122 +            fclose ( outputHandleJustInCase );
2123 +         retVal = remove ( outName );
2124 +         if (retVal != 0)
2125 +            fprintf ( stderr,
2126 +                      "%s: WARNING: deletion of output file "
2127 +                      "(apparently) failed.\n",
2128 +                      progName );
2129 +      } else {
2130 +         fprintf ( stderr,
2131 +                   "%s: WARNING: deletion of output file suppressed\n",
2132 +                    progName );
2133 +         fprintf ( stderr,
2134 +                   "%s:    since input file no longer exists.  Output file\n",
2135 +                   progName );
2136 +         fprintf ( stderr,
2137 +                   "%s:    `%s' may be incomplete.\n",
2138 +                   progName, outName );
2139 +         fprintf ( stderr, 
2140 +                   "%s:    I suggest doing an integrity test (bzip2 -tv)"
2141 +                   " of it.\n",
2142 +                   progName );
2143 +      }
2144 +   }
2145 +
2146 +   if (noisy && numFileNames > 0 && numFilesProcessed < numFileNames) {
2147 +      fprintf ( stderr, 
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 );
2152 +   }
2153 +   setExit(ec);
2154 +   exit(exitValue);
2155 +}
2156 +
2157 +
2158 +/*---------------------------------------------*/
2159 +static 
2160 +void panic ( const Char* s )
2161 +{
2162 +   fprintf ( stderr,
2163 +             "\n%s: PANIC -- internal consistency error:\n"
2164 +             "\t%s\n"
2165 +             "\tThis is a BUG.  Please report it to me at:\n"
2166 +             "\tjseward@bzip.org\n",
2167 +             progName, s );
2168 +   showFileNames();
2169 +   cleanUpAndFail( 3 );
2170 +}
2171 +
2172 +
2173 +/*---------------------------------------------*/
2174 +static 
2175 +void crcError ( void )
2176 +{
2177 +   fprintf ( stderr,
2178 +             "\n%s: Data integrity error when decompressing.\n",
2179 +             progName );
2180 +   showFileNames();
2181 +   cadvise();
2182 +   cleanUpAndFail( 2 );
2183 +}
2184 +
2185 +
2186 +/*---------------------------------------------*/
2187 +static 
2188 +void compressedStreamEOF ( void )
2189 +{
2190 +  if (noisy) {
2191 +    fprintf ( stderr,
2192 +             "\n%s: Compressed file ends unexpectedly;\n\t"
2193 +             "perhaps it is corrupted?  *Possible* reason follows.\n",
2194 +             progName );
2195 +    perror ( progName );
2196 +    showFileNames();
2197 +    cadvise();
2198 +  }
2199 +  cleanUpAndFail( 2 );
2200 +}
2201 +
2202 +
2203 +/*---------------------------------------------*/
2204 +static 
2205 +void ioError ( void )
2206 +{
2207 +   fprintf ( stderr,
2208 +             "\n%s: I/O or other error, bailing out.  "
2209 +             "Possible reason follows.\n",
2210 +             progName );
2211 +   perror ( progName );
2212 +   showFileNames();
2213 +   cleanUpAndFail( 1 );
2214 +}
2215 +
2216 +
2217 +/*---------------------------------------------*/
2218 +static 
2219 +void mySignalCatcher ( IntNative n )
2220 +{
2221 +   fprintf ( stderr,
2222 +             "\n%s: Control-C or similar caught, quitting.\n",
2223 +             progName );
2224 +   cleanUpAndFail(1);
2225 +}
2226 +
2227 +
2228 +/*---------------------------------------------*/
2229 +static 
2230 +void mySIGSEGVorSIGBUScatcher ( IntNative n )
2231 +{
2232 +   if (opMode == OM_Z)
2233 +      fprintf ( 
2234 +      stderr,
2235 +      "\n%s: Caught a SIGSEGV or SIGBUS whilst compressing.\n"
2236 +      "\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"
2244 +      "   \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"
2251 +      "\n",
2252 +      progName );
2253 +      else
2254 +      fprintf ( 
2255 +      stderr,
2256 +      "\n%s: Caught a SIGSEGV or SIGBUS whilst decompressing.\n"
2257 +      "\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"
2267 +      "   \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"
2274 +      "\n",
2275 +      progName );
2276 +
2277 +   showFileNames();
2278 +   if (opMode == OM_Z)
2279 +      cleanUpAndFail( 3 ); else
2280 +      { cadvise(); cleanUpAndFail( 2 ); }
2281 +}
2282 +
2283 +
2284 +/*---------------------------------------------*/
2285 +static 
2286 +void outOfMemory ( void )
2287 +{
2288 +   fprintf ( stderr,
2289 +             "\n%s: couldn't allocate enough memory\n",
2290 +             progName );
2291 +   showFileNames();
2292 +   cleanUpAndFail(1);
2293 +}
2294 +
2295 +
2296 +/*---------------------------------------------*/
2297 +static 
2298 +void configError ( void )
2299 +{
2300 +   fprintf ( stderr,
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" );
2306 +   setExit(3);
2307 +   exit(exitValue);
2308 +}
2309 +
2310 +
2311 +/*---------------------------------------------------*/
2312 +/*--- The main driver machinery                   ---*/
2313 +/*---------------------------------------------------*/
2314 +
2315 +/* All rather crufty.  The main problem is that input files
2316 +   are stat()d multiple times before use.  This should be
2317 +   cleaned up. 
2318 +*/
2319 +
2320 +/*---------------------------------------------*/
2321 +static 
2322 +void pad ( Char *s )
2323 +{
2324 +   Int32 i;
2325 +   if ( (Int32)strlen(s) >= longestFileName ) return;
2326 +   for (i = 1; i <= longestFileName - (Int32)strlen(s); i++)
2327 +      fprintf ( stderr, " " );
2328 +}
2329 +
2330 +
2331 +/*---------------------------------------------*/
2332 +static 
2333 +void copyFileName ( Char* to, Char* from ) 
2334 +{
2335 +   if ( strlen(from) > FILE_NAME_LEN-10 )  {
2336 +      fprintf (
2337 +         stderr,
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
2342 +      );
2343 +      setExit(1);
2344 +      exit(exitValue);
2345 +   }
2346 +
2347 +  strncpy(to,from,FILE_NAME_LEN-10);
2348 +  to[FILE_NAME_LEN-10]='\0';
2349 +}
2350 +
2351 +
2352 +/*---------------------------------------------*/
2353 +static 
2354 +Bool fileExists ( Char* name )
2355 +{
2356 +   FILE *tmp   = fopen ( name, "rb" );
2357 +   Bool exists = (tmp != NULL);
2358 +   if (tmp != NULL) fclose ( tmp );
2359 +   return exists;
2360 +}
2361 +
2362 +
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-).
2369 +
2370 +   For non-Unix platforms, if we are not worrying about
2371 +   security issues, simple this simply behaves like fopen.
2372 +*/
2373 +static
2374 +FILE* fopen_output_safely ( Char* name, const char* mode )
2375 +{
2376 +#  if BZ_UNIX
2377 +   FILE*     fp;
2378 +   IntNative fh;
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);
2383 +   return fp;
2384 +#  else
2385 +   return fopen(name, mode);
2386 +#  endif
2387 +}
2388 +
2389 +
2390 +/*---------------------------------------------*/
2391 +/*--
2392 +  if in doubt, return True
2393 +--*/
2394 +static 
2395 +Bool notAStandardFile ( Char* name )
2396 +{
2397 +   IntNative      i;
2398 +   struct MY_STAT statBuf;
2399 +
2400 +   i = MY_LSTAT ( name, &statBuf );
2401 +   if (i != 0) return True;
2402 +   if (MY_S_ISREG(statBuf.st_mode)) return False;
2403 +   return True;
2404 +}
2405 +
2406 +
2407 +/*---------------------------------------------*/
2408 +/*--
2409 +  rac 11/21/98 see if file has hard links to it
2410 +--*/
2411 +static 
2412 +Int32 countHardLinks ( Char* name )
2413 +{  
2414 +   IntNative      i;
2415 +   struct MY_STAT statBuf;
2416 +
2417 +   i = MY_LSTAT ( name, &statBuf );
2418 +   if (i != 0) return 0;
2419 +   return (statBuf.st_nlink - 1);
2420 +}
2421 +
2422 +
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.
2428 +
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.
2435 +
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
2440 +   cleverness.
2441 +
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 ...  
2445 +*/
2446 +#if BZ_UNIX
2447 +static 
2448 +struct MY_STAT fileMetaInfo;
2449 +#endif
2450 +
2451 +static 
2452 +void saveInputFileMetaInfo ( Char *srcName )
2453 +{
2454 +#  if BZ_UNIX
2455 +   IntNative retVal;
2456 +   /* Note use of stat here, not lstat. */
2457 +   retVal = MY_STAT( srcName, &fileMetaInfo );
2458 +   ERROR_IF_NOT_ZERO ( retVal );
2459 +#  endif
2460 +}
2461 +
2462 +
2463 +static 
2464 +void applySavedTimeInfoToOutputFile ( Char *dstName )
2465 +{
2466 +#  if BZ_UNIX
2467 +   IntNative      retVal;
2468 +   struct utimbuf uTimBuf;
2469 +
2470 +   uTimBuf.actime = fileMetaInfo.st_atime;
2471 +   uTimBuf.modtime = fileMetaInfo.st_mtime;
2472 +
2473 +   retVal = utime ( dstName, &uTimBuf );
2474 +   ERROR_IF_NOT_ZERO ( retVal );
2475 +#  endif
2476 +}
2477 +
2478 +static 
2479 +void applySavedFileAttrToOutputFile ( IntNative fd )
2480 +{
2481 +#  if BZ_UNIX
2482 +   IntNative retVal;
2483 +
2484 +   retVal = fchmod ( fd, fileMetaInfo.st_mode );
2485 +   ERROR_IF_NOT_ZERO ( retVal );
2486 +
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.
2490 +   */
2491 +#  endif
2492 +}
2493 +
2494 +
2495 +/*---------------------------------------------*/
2496 +static 
2497 +Bool containsDubiousChars ( Char* name )
2498 +{
2499 +#  if BZ_UNIX
2500 +   /* On unix, files can contain any characters and the file expansion
2501 +    * is performed by the shell.
2502 +    */
2503 +   return False;
2504 +#  else /* ! BZ_UNIX */
2505 +   /* On non-unix (Win* platforms), wildcard characters are not allowed in 
2506 +    * filenames.
2507 +    */
2508 +   for (; *name != '\0'; name++)
2509 +      if (*name == '?' || *name == '*') return True;
2510 +   return False;
2511 +#  endif /* BZ_UNIX */
2512 +}
2513 +
2514 +
2515 +/*---------------------------------------------*/
2516 +#define BZ_N_SUFFIX_PAIRS 4
2517 +
2518 +const Char* zSuffix[BZ_N_SUFFIX_PAIRS] 
2519 +   = { ".bz2", ".bz", ".tbz2", ".tbz" };
2520 +const Char* unzSuffix[BZ_N_SUFFIX_PAIRS] 
2521 +   = { "", "", ".tar", ".tar" };
2522 +
2523 +static 
2524 +Bool hasSuffix ( Char* s, const Char* suffix )
2525 +{
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;
2530 +   return False;
2531 +}
2532 +
2533 +static 
2534 +Bool mapSuffix ( Char* name, 
2535 +                 const Char* oldSuffix, 
2536 +                 const Char* newSuffix )
2537 +{
2538 +   if (!hasSuffix(name,oldSuffix)) return False;
2539 +   name[strlen(name)-strlen(oldSuffix)] = 0;
2540 +   strcat ( name, newSuffix );
2541 +   return True;
2542 +}
2543 +
2544 +
2545 +/*---------------------------------------------*/
2546 +static 
2547 +void compress ( Char *name )
2548 +{
2549 +   FILE  *inStr;
2550 +   FILE  *outStr;
2551 +   Int32 n, i;
2552 +   struct MY_STAT statBuf;
2553 +
2554 +   deleteOutputOnInterrupt = False;
2555 +
2556 +   if (name == NULL && srcMode != SM_I2O)
2557 +      panic ( "compress: bad modes\n" );
2558 +
2559 +   switch (srcMode) {
2560 +      case SM_I2O: 
2561 +         copyFileName ( inName, (Char*)"(stdin)" );
2562 +         copyFileName ( outName, (Char*)"(stdout)" ); 
2563 +         break;
2564 +      case SM_F2F: 
2565 +         copyFileName ( inName, name );
2566 +         copyFileName ( outName, name );
2567 +         strcat ( outName, ".bz2" ); 
2568 +         break;
2569 +      case SM_F2O: 
2570 +         copyFileName ( inName, name );
2571 +         copyFileName ( outName, (Char*)"(stdout)" ); 
2572 +         break;
2573 +   }
2574 +
2575 +   if ( srcMode != SM_I2O && containsDubiousChars ( inName ) ) {
2576 +      if (noisy)
2577 +      fprintf ( stderr, "%s: There are no files matching `%s'.\n",
2578 +                progName, inName );
2579 +      setExit(1);
2580 +      return;
2581 +   }
2582 +   if ( srcMode != SM_I2O && !fileExists ( inName ) ) {
2583 +      fprintf ( stderr, "%s: Can't open input file %s: %s.\n",
2584 +                progName, inName, strerror(errno) );
2585 +      setExit(1);
2586 +      return;
2587 +   }
2588 +   for (i = 0; i < BZ_N_SUFFIX_PAIRS; i++) {
2589 +      if (hasSuffix(inName, zSuffix[i])) {
2590 +         if (noisy)
2591 +         fprintf ( stderr, 
2592 +                   "%s: Input file %s already has %s suffix.\n",
2593 +                   progName, inName, zSuffix[i] );
2594 +         setExit(1);
2595 +         return;
2596 +      }
2597 +   }
2598 +   if ( srcMode == SM_F2F || srcMode == SM_F2O ) {
2599 +      MY_STAT(inName, &statBuf);
2600 +      if ( MY_S_ISDIR(statBuf.st_mode) ) {
2601 +         fprintf( stderr,
2602 +                  "%s: Input file %s is a directory.\n",
2603 +                  progName,inName);
2604 +         setExit(1);
2605 +         return;
2606 +      }
2607 +   }
2608 +   if ( srcMode == SM_F2F && !forceOverwrite && notAStandardFile ( inName )) {
2609 +      if (noisy)
2610 +      fprintf ( stderr, "%s: Input file %s is not a normal file.\n",
2611 +                progName, inName );
2612 +      setExit(1);
2613 +      return;
2614 +   }
2615 +   if ( srcMode == SM_F2F && fileExists ( outName ) ) {
2616 +      if (forceOverwrite) {
2617 +        remove(outName);
2618 +      } else {
2619 +        fprintf ( stderr, "%s: Output file %s already exists.\n",
2620 +                  progName, outName );
2621 +        setExit(1);
2622 +        return;
2623 +      }
2624 +   }
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" : "" );
2629 +      setExit(1);
2630 +      return;
2631 +   }
2632 +
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 );
2637 +   }
2638 +
2639 +   switch ( srcMode ) {
2640 +
2641 +      case SM_I2O:
2642 +         inStr = stdin;
2643 +         outStr = stdout;
2644 +         if ( isatty ( fileno ( stdout ) ) ) {
2645 +            fprintf ( stderr,
2646 +                      "%s: I won't write compressed data to a terminal.\n",
2647 +                      progName );
2648 +            fprintf ( stderr, "%s: For help, type: `%s --help'.\n",
2649 +                              progName, progName );
2650 +            setExit(1);
2651 +            return;
2652 +         };
2653 +         break;
2654 +
2655 +      case SM_F2O:
2656 +         inStr = fopen ( inName, "rb" );
2657 +         outStr = stdout;
2658 +         if ( isatty ( fileno ( stdout ) ) ) {
2659 +            fprintf ( stderr,
2660 +                      "%s: I won't write compressed data to a terminal.\n",
2661 +                      progName );
2662 +            fprintf ( stderr, "%s: For help, type: `%s --help'.\n",
2663 +                              progName, progName );
2664 +            if ( inStr != NULL ) fclose ( inStr );
2665 +            setExit(1);
2666 +            return;
2667 +         };
2668 +         if ( inStr == NULL ) {
2669 +            fprintf ( stderr, "%s: Can't open input file %s: %s.\n",
2670 +                      progName, inName, strerror(errno) );
2671 +            setExit(1);
2672 +            return;
2673 +         };
2674 +         break;
2675 +
2676 +      case SM_F2F:
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 );
2683 +            setExit(1);
2684 +            return;
2685 +         }
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 );
2690 +            setExit(1);
2691 +            return;
2692 +         };
2693 +         break;
2694 +
2695 +      default:
2696 +         panic ( "compress: bad srcMode" );
2697 +         break;
2698 +   }
2699 +
2700 +   if (verbosity >= 1) {
2701 +      fprintf ( stderr,  "  %s: ", inName );
2702 +      pad ( inName );
2703 +      fflush ( stderr );
2704 +   }
2705 +
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;
2711 +
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 );
2719 +      }
2720 +   }
2721 +
2722 +   deleteOutputOnInterrupt = False;
2723 +}
2724 +
2725 +
2726 +/*---------------------------------------------*/
2727 +static 
2728 +void uncompress ( Char *name )
2729 +{
2730 +   FILE  *inStr;
2731 +   FILE  *outStr;
2732 +   Int32 n, i;
2733 +   Bool  magicNumberOK;
2734 +   Bool  cantGuess;
2735 +   struct MY_STAT statBuf;
2736 +
2737 +   deleteOutputOnInterrupt = False;
2738 +
2739 +   if (name == NULL && srcMode != SM_I2O)
2740 +      panic ( "uncompress: bad modes\n" );
2741 +
2742 +   cantGuess = False;
2743 +   switch (srcMode) {
2744 +      case SM_I2O: 
2745 +         copyFileName ( inName, (Char*)"(stdin)" );
2746 +         copyFileName ( outName, (Char*)"(stdout)" ); 
2747 +         break;
2748 +      case SM_F2F: 
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]))
2753 +               goto zzz; 
2754 +         cantGuess = True;
2755 +         strcat ( outName, ".out" );
2756 +         break;
2757 +      case SM_F2O: 
2758 +         copyFileName ( inName, name );
2759 +         copyFileName ( outName, (Char*)"(stdout)" ); 
2760 +         break;
2761 +   }
2762 +
2763 +   zzz:
2764 +   if ( srcMode != SM_I2O && containsDubiousChars ( inName ) ) {
2765 +      if (noisy)
2766 +      fprintf ( stderr, "%s: There are no files matching `%s'.\n",
2767 +                progName, inName );
2768 +      setExit(1);
2769 +      return;
2770 +   }
2771 +   if ( srcMode != SM_I2O && !fileExists ( inName ) ) {
2772 +      fprintf ( stderr, "%s: Can't open input file %s: %s.\n",
2773 +                progName, inName, strerror(errno) );
2774 +      setExit(1);
2775 +      return;
2776 +   }
2777 +   if ( srcMode == SM_F2F || srcMode == SM_F2O ) {
2778 +      MY_STAT(inName, &statBuf);
2779 +      if ( MY_S_ISDIR(statBuf.st_mode) ) {
2780 +         fprintf( stderr,
2781 +                  "%s: Input file %s is a directory.\n",
2782 +                  progName,inName);
2783 +         setExit(1);
2784 +         return;
2785 +      }
2786 +   }
2787 +   if ( srcMode == SM_F2F && !forceOverwrite && notAStandardFile ( inName )) {
2788 +      if (noisy)
2789 +      fprintf ( stderr, "%s: Input file %s is not a normal file.\n",
2790 +                progName, inName );
2791 +      setExit(1);
2792 +      return;
2793 +   }
2794 +   if ( /* srcMode == SM_F2F implied && */ cantGuess ) {
2795 +      if (noisy)
2796 +      fprintf ( stderr, 
2797 +                "%s: Can't guess original name for %s -- using %s\n",
2798 +                progName, inName, outName );
2799 +      /* just a warning, no return */
2800 +   }   
2801 +   if ( srcMode == SM_F2F && fileExists ( outName ) ) {
2802 +      if (forceOverwrite) {
2803 +       remove(outName);
2804 +      } else {
2805 +        fprintf ( stderr, "%s: Output file %s already exists.\n",
2806 +                  progName, outName );
2807 +        setExit(1);
2808 +        return;
2809 +      }
2810 +   }
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" : "" );
2815 +      setExit(1);
2816 +      return;
2817 +   }
2818 +
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 );
2823 +   }
2824 +
2825 +   switch ( srcMode ) {
2826 +
2827 +      case SM_I2O:
2828 +         inStr = stdin;
2829 +         outStr = stdout;
2830 +         if ( isatty ( fileno ( stdin ) ) ) {
2831 +            fprintf ( stderr,
2832 +                      "%s: I won't read compressed data from a terminal.\n",
2833 +                      progName );
2834 +            fprintf ( stderr, "%s: For help, type: `%s --help'.\n",
2835 +                              progName, progName );
2836 +            setExit(1);
2837 +            return;
2838 +         };
2839 +         break;
2840 +
2841 +      case SM_F2O:
2842 +         inStr = fopen ( inName, "rb" );
2843 +         outStr = stdout;
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 );
2848 +            setExit(1);
2849 +            return;
2850 +         };
2851 +         break;
2852 +
2853 +      case SM_F2F:
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 );
2860 +            setExit(1);
2861 +            return;
2862 +         }
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 );
2867 +            setExit(1);
2868 +            return;
2869 +         };
2870 +         break;
2871 +
2872 +      default:
2873 +         panic ( "uncompress: bad srcMode" );
2874 +         break;
2875 +   }
2876 +
2877 +   if (verbosity >= 1) {
2878 +      fprintf ( stderr, "  %s: ", inName );
2879 +      pad ( inName );
2880 +      fflush ( stderr );
2881 +   }
2882 +
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;
2888 +
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 );
2897 +         }
2898 +      }
2899 +   } else {
2900 +      unzFailsExist = True;
2901 +      deleteOutputOnInterrupt = False;
2902 +      if ( srcMode == SM_F2F ) {
2903 +         IntNative retVal = remove ( outName );
2904 +         ERROR_IF_NOT_ZERO ( retVal );
2905 +      }
2906 +   }
2907 +   deleteOutputOnInterrupt = False;
2908 +
2909 +   if ( magicNumberOK ) {
2910 +      if (verbosity >= 1)
2911 +         fprintf ( stderr, "done\n" );
2912 +   } else {
2913 +      setExit(2);
2914 +      if (verbosity >= 1)
2915 +         fprintf ( stderr, "not a bzip2 file.\n" ); else
2916 +         fprintf ( stderr,
2917 +                   "%s: %s is not a bzip2 file.\n",
2918 +                   progName, inName );
2919 +   }
2920 +
2921 +}
2922 +
2923 +
2924 +/*---------------------------------------------*/
2925 +static 
2926 +void testf ( Char *name )
2927 +{
2928 +   FILE *inStr;
2929 +   Bool allOK;
2930 +   struct MY_STAT statBuf;
2931 +
2932 +   deleteOutputOnInterrupt = False;
2933 +
2934 +   if (name == NULL && srcMode != SM_I2O)
2935 +      panic ( "testf: bad modes\n" );
2936 +
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;
2942 +   }
2943 +
2944 +   if ( srcMode != SM_I2O && containsDubiousChars ( inName ) ) {
2945 +      if (noisy)
2946 +      fprintf ( stderr, "%s: There are no files matching `%s'.\n",
2947 +                progName, inName );
2948 +      setExit(1);
2949 +      return;
2950 +   }
2951 +   if ( srcMode != SM_I2O && !fileExists ( inName ) ) {
2952 +      fprintf ( stderr, "%s: Can't open input %s: %s.\n",
2953 +                progName, inName, strerror(errno) );
2954 +      setExit(1);
2955 +      return;
2956 +   }
2957 +   if ( srcMode != SM_I2O ) {
2958 +      MY_STAT(inName, &statBuf);
2959 +      if ( MY_S_ISDIR(statBuf.st_mode) ) {
2960 +         fprintf( stderr,
2961 +                  "%s: Input file %s is a directory.\n",
2962 +                  progName,inName);
2963 +         setExit(1);
2964 +         return;
2965 +      }
2966 +   }
2967 +
2968 +   switch ( srcMode ) {
2969 +
2970 +      case SM_I2O:
2971 +         if ( isatty ( fileno ( stdin ) ) ) {
2972 +            fprintf ( stderr,
2973 +                      "%s: I won't read compressed data from a terminal.\n",
2974 +                      progName );
2975 +            fprintf ( stderr, "%s: For help, type: `%s --help'.\n",
2976 +                              progName, progName );
2977 +            setExit(1);
2978 +            return;
2979 +         };
2980 +         inStr = stdin;
2981 +         break;
2982 +
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) );
2988 +            setExit(1);
2989 +            return;
2990 +         };
2991 +         break;
2992 +
2993 +      default:
2994 +         panic ( "testf: bad srcMode" );
2995 +         break;
2996 +   }
2997 +
2998 +   if (verbosity >= 1) {
2999 +      fprintf ( stderr, "  %s: ", inName );
3000 +      pad ( inName );
3001 +      fflush ( stderr );
3002 +   }
3003 +
3004 +   /*--- Now the input handle is sane.  Do the Biz. ---*/
3005 +   outputHandleJustInCase = NULL;
3006 +   allOK = testStream ( inStr );
3007 +
3008 +   if (allOK && verbosity >= 1) fprintf ( stderr, "ok\n" );
3009 +   if (!allOK) testFailsExist = True;
3010 +}
3011 +
3012 +
3013 +/*---------------------------------------------*/
3014 +static 
3015 +void license ( void )
3016 +{
3017 +   fprintf ( stderr,
3018 +
3019 +    "bzip2, a block-sorting file compressor.  "
3020 +    "Version %s.\n"
3021 +    "   \n"
3022 +    "   Copyright (C) 1996-2010 by Julian Seward.\n"
3023 +    "   \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"
3027 +    "   \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"
3032 +    "   \n",
3033 +    BZ2_bzlibVersion()
3034 +   );
3035 +}
3036 +
3037 +
3038 +/*---------------------------------------------*/
3039 +static 
3040 +void usage ( Char *fullProgName )
3041 +{
3042 +   fprintf (
3043 +      stderr,
3044 +      "bzip2, a block-sorting file compressor.  "
3045 +      "Version %s.\n"
3046 +      "\n   usage: %s [flags and input files in any order]\n"
3047 +      "\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"
3063 +      "\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"
3067 +      "\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"
3071 +#     if BZ_UNIX
3072 +      "\n"
3073 +#     endif
3074 +      ,
3075 +
3076 +      BZ2_bzlibVersion(),
3077 +      fullProgName
3078 +   );
3079 +}
3080 +
3081 +
3082 +/*---------------------------------------------*/
3083 +static 
3084 +void redundant ( Char* flag )
3085 +{
3086 +   fprintf ( 
3087 +      stderr, 
3088 +      "%s: %s is redundant in versions 0.9.5 and above\n",
3089 +      progName, flag );
3090 +}
3091 +
3092 +
3093 +/*---------------------------------------------*/
3094 +/*--
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].
3098 +
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.
3103 +
3104 +  The actual Dirty Work is done by the platform-
3105 +  specific macro APPEND_FILESPEC.
3106 +--*/
3107 +
3108 +typedef
3109 +   struct zzzz {
3110 +      Char        *name;
3111 +      struct zzzz *link;
3112 +   }
3113 +   Cell;
3114 +
3115 +
3116 +/*---------------------------------------------*/
3117 +static 
3118 +void *myMalloc ( Int32 n )
3119 +{
3120 +   void* p;
3121 +
3122 +   p = malloc ( (size_t)n );
3123 +   if (p == NULL) outOfMemory ();
3124 +   return p;
3125 +}
3126 +
3127 +
3128 +/*---------------------------------------------*/
3129 +static 
3130 +Cell *mkCell ( void )
3131 +{
3132 +   Cell *c;
3133 +
3134 +   c = (Cell*) myMalloc ( sizeof ( Cell ) );
3135 +   c->name = NULL;
3136 +   c->link = NULL;
3137 +   return c;
3138 +}
3139 +
3140 +
3141 +/*---------------------------------------------*/
3142 +static 
3143 +Cell *snocString ( Cell *root, Char *name )
3144 +{
3145 +   if (root == NULL) {
3146 +      Cell *tmp = mkCell();
3147 +      tmp->name = (Char*) myMalloc ( 5 + strlen(name) );
3148 +      strcpy ( tmp->name, name );
3149 +      return tmp;
3150 +   } else {
3151 +      Cell *tmp = root;
3152 +      while (tmp->link != NULL) tmp = tmp->link;
3153 +      tmp->link = snocString ( tmp->link, name );
3154 +      return root;
3155 +   }
3156 +}
3157 +
3158 +
3159 +/*---------------------------------------------*/
3160 +static 
3161 +void addFlagsFromEnvVar ( Cell** argList, Char* varName ) 
3162 +{
3163 +   Int32 i, j, k;
3164 +   Char *envbase, *p;
3165 +
3166 +   envbase = getenv(varName);
3167 +   if (envbase != NULL) {
3168 +      p = envbase;
3169 +      i = 0;
3170 +      while (True) {
3171 +         if (p[i] == 0) break;
3172 +         p += i;
3173 +         i = 0;
3174 +         while (isspace((Int32)(p[0]))) p++;
3175 +         while (p[i] != 0 && !isspace((Int32)(p[i]))) i++;
3176 +         if (i > 0) {
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];
3179 +            tmpName[k] = 0;
3180 +            APPEND_FLAG(*argList, tmpName);
3181 +         }
3182 +      }
3183 +   }
3184 +}
3185 +
3186 +
3187 +/*---------------------------------------------*/
3188 +#define ISFLAG(s) (strcmp(aa->name, (s))==0)
3189 +
3190 +IntNative main ( IntNative argc, Char *argv[] )
3191 +{
3192 +   Int32  i, j;
3193 +   Char   *tmp;
3194 +   Cell   *argList;
3195 +   Cell   *aa;
3196 +   Bool   decode;
3197 +
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)
3202 +      configError();
3203 +
3204 +   /*-- Initialise --*/
3205 +   outputHandleJustInCase  = NULL;
3206 +   smallMode               = False;
3207 +   keepInputFiles          = False;
3208 +   forceOverwrite          = False;
3209 +   noisy                   = True;
3210 +   verbosity               = 0;
3211 +   blockSize100k           = 9;
3212 +   testFailsExist          = False;
3213 +   unzFailsExist           = False;
3214 +   numFileNames            = 0;
3215 +   numFilesProcessed       = 0;
3216 +   workFactor              = 30;
3217 +   deleteOutputOnInterrupt = False;
3218 +   exitValue               = 0;
3219 +   i = j = 0; /* avoid bogus warning from egcs-1.1.X */
3220 +
3221 +   /*-- Set up signal handlers for mem access errors --*/
3222 +   signal (SIGSEGV, mySIGSEGVorSIGBUScatcher);
3223 +#  if BZ_UNIX
3224 +#  ifndef __DJGPP__
3225 +   signal (SIGBUS,  mySIGSEGVorSIGBUScatcher);
3226 +#  endif
3227 +#  endif
3228 +
3229 +   copyFileName ( inName,  (Char*)"(none)" );
3230 +   copyFileName ( outName, (Char*)"(none)" );
3231 +
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;
3236 +
3237 +
3238 +   /*-- Copy flags from env var BZIP2, and 
3239 +        expand filename wildcards in arg list.
3240 +   --*/
3241 +   argList = NULL;
3242 +   addFlagsFromEnvVar ( &argList,  (Char*)"BZIP2" );
3243 +   addFlagsFromEnvVar ( &argList,  (Char*)"BZIP" );
3244 +   for (i = 1; i <= argc-1; i++)
3245 +      APPEND_FILESPEC(argList, argv[i]);
3246 +
3247 +
3248 +   /*-- Find the length of the longest filename --*/
3249 +   longestFileName = 7;
3250 +   numFileNames    = 0;
3251 +   decode          = True;
3252 +   for (aa = argList; aa != NULL; aa = aa->link) {
3253 +      if (ISFLAG("--")) { decode = False; continue; }
3254 +      if (aa->name[0] == '-' && decode) continue;
3255 +      numFileNames++;
3256 +      if (longestFileName < (Int32)strlen(aa->name) )
3257 +         longestFileName = (Int32)strlen(aa->name);
3258 +   }
3259 +
3260 +
3261 +   /*-- Determine source modes; flag handling may change this too. --*/
3262 +   if (numFileNames == 0)
3263 +      srcMode = SM_I2O; else srcMode = SM_F2F;
3264 +
3265 +
3266 +   /*-- Determine what to do (compress/uncompress/test/cat). --*/
3267 +   /*-- Note that subsequent flag handling may change this. --*/
3268 +   opMode = OM_Z;
3269 +
3270 +   if ( (strstr ( progName, "unzip" ) != 0) ||
3271 +        (strstr ( progName, "UNZIP" ) != 0) )
3272 +      opMode = OM_UNZ;
3273 +
3274 +   if ( (strstr ( progName, "z2cat" ) != 0) ||
3275 +        (strstr ( progName, "Z2CAT" ) != 0) ||
3276 +        (strstr ( progName, "zcat" ) != 0)  ||
3277 +        (strstr ( progName, "ZCAT" ) != 0) )  {
3278 +      opMode = OM_UNZ;
3279 +      srcMode = (numFileNames == 0) ? SM_I2O : SM_F2O;
3280 +   }
3281 +
3282 +
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;
3306 +               case 'V':
3307 +               case 'L': license();            break;
3308 +               case 'v': verbosity++; break;
3309 +               case 'h': usage ( progName );
3310 +                         exit ( 0 );
3311 +                         break;
3312 +               default:  fprintf ( stderr, "%s: Bad flag `%s'\n",
3313 +                                   progName, aa->name );
3314 +                         usage ( progName );
3315 +                         exit ( 1 );
3316 +                         break;
3317 +            }
3318 +         }
3319 +      }
3320 +   }
3321 +   
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 ); }
3342 +         else
3343 +         if (strncmp ( aa->name, "--", 2) == 0) {
3344 +            fprintf ( stderr, "%s: Bad flag `%s'\n", progName, aa->name );
3345 +            usage ( progName );
3346 +            exit ( 1 );
3347 +         }
3348 +   }
3349 +
3350 +   if (verbosity > 4) verbosity = 4;
3351 +   if (opMode == OM_Z && smallMode && blockSize100k > 2) 
3352 +      blockSize100k = 2;
3353 +
3354 +   if (opMode == OM_TEST && srcMode == SM_F2O) {
3355 +      fprintf ( stderr, "%s: -c and -t cannot be used together.\n",
3356 +                progName );
3357 +      exit ( 1 );
3358 +   }
3359 +
3360 +   if (srcMode == SM_F2O && numFileNames == 0)
3361 +      srcMode = SM_I2O;
3362 +
3363 +   if (opMode != OM_Z) blockSize100k = 0;
3364 +
3365 +   if (srcMode == SM_F2F) {
3366 +      signal (SIGINT,  mySignalCatcher);
3367 +      signal (SIGTERM, mySignalCatcher);
3368 +#     if BZ_UNIX
3369 +      signal (SIGHUP,  mySignalCatcher);
3370 +#     endif
3371 +   }
3372 +
3373 +   if (opMode == OM_Z) {
3374 +     if (srcMode == SM_I2O) {
3375 +        compress ( NULL );
3376 +     } else {
3377 +        decode = True;
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 );
3383 +        }
3384 +     }
3385 +   } 
3386 +   else
3387 +
3388 +   if (opMode == OM_UNZ) {
3389 +      unzFailsExist = False;
3390 +      if (srcMode == SM_I2O) {
3391 +         uncompress ( NULL );
3392 +      } else {
3393 +         decode = True;
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 );
3399 +         }      
3400 +      }
3401 +      if (unzFailsExist) { 
3402 +         setExit(2); 
3403 +         exit(exitValue);
3404 +      }
3405 +   } 
3406 +
3407 +   else {
3408 +      testFailsExist = False;
3409 +      if (srcMode == SM_I2O) {
3410 +         testf ( NULL );
3411 +      } else {
3412 +         decode = True;
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 );
3418 +        }
3419 +      }
3420 +      if (testFailsExist && noisy) {
3421 +         fprintf ( stderr,
3422 +           "\n"
3423 +           "You can use the `bzip2recover' program to attempt to recover\n"
3424 +           "data from undamaged sections of corrupted files.\n\n"
3425 +         );
3426 +         setExit(2);
3427 +         exit(exitValue);
3428 +      }
3429 +   }
3430 +
3431 +   /* Free the argument list memory to mollify leak detectors 
3432 +      (eg) Purify, Checker.  Serves no other useful purpose.
3433 +   */
3434 +   aa = argList;
3435 +   while (aa != NULL) {
3436 +      Cell* aa2 = aa->link;
3437 +      if (aa->name != NULL) free(aa->name);
3438 +      free(aa);
3439 +      aa = aa2;
3440 +   }
3441 +
3442 +   return exitValue;
3443 +}
3444 +
3445 +
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
3452 --- /dev/null
3453 +++ b/pit/upstream-bzip2/bzlib.c
3454 @@ -0,0 +1,1572 @@
3455 +
3456 +/*-------------------------------------------------------------*/
3457 +/*--- Library top-level functions.                          ---*/
3458 +/*---                                               bzlib.c ---*/
3459 +/*-------------------------------------------------------------*/
3460 +
3461 +/* ------------------------------------------------------------------
3462 +   This file is part of bzip2/libbzip2, a program and library for
3463 +   lossless, block-sorting data compression.
3464 +
3465 +   bzip2/libbzip2 version 1.0.6 of 6 September 2010
3466 +   Copyright (C) 1996-2010 Julian Seward <jseward@bzip.org>
3467 +
3468 +   Please read the WARNING, DISCLAIMER and PATENTS sections in the 
3469 +   README file.
3470 +
3471 +   This program is released under the terms of the license contained
3472 +   in the file LICENSE.
3473 +   ------------------------------------------------------------------ */
3474 +
3475 +/* CHANGES
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.
3483 +*/
3484 +
3485 +#include "bzlib_private.h"
3486 +
3487 +
3488 +/*---------------------------------------------------*/
3489 +/*--- Compression stuff                           ---*/
3490 +/*---------------------------------------------------*/
3491 +
3492 +
3493 +/*---------------------------------------------------*/
3494 +#ifndef BZ_NO_STDIO
3495 +void BZ2_bz__AssertH__fail ( int errcode )
3496 +{
3497 +   fprintf(stderr, 
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",
3506 +      errcode,
3507 +      BZ2_bzlibVersion()
3508 +   );
3509 +
3510 +   if (errcode == 1007) {
3511 +   fprintf(stderr,
3512 +      "\n*** A special note about internal error number 1007 ***\n"
3513 +      "\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"
3519 +      "\n"
3520 +      "I suggest the following: try compressing the file again,\n"
3521 +      "possibly monitoring progress in detail with the -vv flag.\n"
3522 +      "\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"
3529 +      "\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"
3534 +      "\n"
3535 +   );
3536 +   }
3537 +
3538 +   exit(3);
3539 +}
3540 +#endif
3541 +
3542 +
3543 +/*---------------------------------------------------*/
3544 +static
3545 +int bz_config_ok ( void )
3546 +{
3547 +   if (sizeof(int)   != 4) return 0;
3548 +   if (sizeof(short) != 2) return 0;
3549 +   if (sizeof(char)  != 1) return 0;
3550 +   return 1;
3551 +}
3552 +
3553 +
3554 +/*---------------------------------------------------*/
3555 +static
3556 +void* default_bzalloc ( void* opaque, Int32 items, Int32 size )
3557 +{
3558 +   void* v = malloc ( items * size );
3559 +   return v;
3560 +}
3561 +
3562 +static
3563 +void default_bzfree ( void* opaque, void* addr )
3564 +{
3565 +   if (addr != NULL) free ( addr );
3566 +}
3567 +
3568 +
3569 +/*---------------------------------------------------*/
3570 +static
3571 +void prepare_new_block ( EState* s )
3572 +{
3573 +   Int32 i;
3574 +   s->nblock = 0;
3575 +   s->numZ = 0;
3576 +   s->state_out_pos = 0;
3577 +   BZ_INITIALISE_CRC ( s->blockCRC );
3578 +   for (i = 0; i < 256; i++) s->inUse[i] = False;
3579 +   s->blockNo++;
3580 +}
3581 +
3582 +
3583 +/*---------------------------------------------------*/
3584 +static
3585 +void init_RL ( EState* s )
3586 +{
3587 +   s->state_in_ch  = 256;
3588 +   s->state_in_len = 0;
3589 +}
3590 +
3591 +
3592 +static
3593 +Bool isempty_RL ( EState* s )
3594 +{
3595 +   if (s->state_in_ch < 256 && s->state_in_len > 0)
3596 +      return False; else
3597 +      return True;
3598 +}
3599 +
3600 +
3601 +/*---------------------------------------------------*/
3602 +int BZ_API(BZ2_bzCompressInit) 
3603 +                    ( bz_stream* strm, 
3604 +                     int        blockSize100k,
3605 +                     int        verbosity,
3606 +                     int        workFactor )
3607 +{
3608 +   Int32   n;
3609 +   EState* s;
3610 +
3611 +   if (!bz_config_ok()) return BZ_CONFIG_ERROR;
3612 +
3613 +   if (strm == NULL || 
3614 +       blockSize100k < 1 || blockSize100k > 9 ||
3615 +       workFactor < 0 || workFactor > 250)
3616 +     return BZ_PARAM_ERROR;
3617 +
3618 +   if (workFactor == 0) workFactor = 30;
3619 +   if (strm->bzalloc == NULL) strm->bzalloc = default_bzalloc;
3620 +   if (strm->bzfree == NULL) strm->bzfree = default_bzfree;
3621 +
3622 +   s = BZALLOC( sizeof(EState) );
3623 +   if (s == NULL) return BZ_MEM_ERROR;
3624 +   s->strm = strm;
3625 +
3626 +   s->arr1 = NULL;
3627 +   s->arr2 = NULL;
3628 +   s->ftab = NULL;
3629 +
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) );
3634 +
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;
3641 +   }
3642 +
3643 +   s->blockNo           = 0;
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;
3651 +
3652 +   s->block             = (UChar*)s->arr2;
3653 +   s->mtfv              = (UInt16*)s->arr1;
3654 +   s->zbits             = NULL;
3655 +   s->ptr               = (UInt32*)s->arr1;
3656 +
3657 +   strm->state          = s;
3658 +   strm->total_in_lo32  = 0;
3659 +   strm->total_in_hi32  = 0;
3660 +   strm->total_out_lo32 = 0;
3661 +   strm->total_out_hi32 = 0;
3662 +   init_RL ( s );
3663 +   prepare_new_block ( s );
3664 +   return BZ_OK;
3665 +}
3666 +
3667 +
3668 +/*---------------------------------------------------*/
3669 +static
3670 +void add_pair_to_block ( EState* s )
3671 +{
3672 +   Int32 i;
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 );
3676 +   }
3677 +   s->inUse[s->state_in_ch] = True;
3678 +   switch (s->state_in_len) {
3679 +      case 1:
3680 +         s->block[s->nblock] = (UChar)ch; s->nblock++;
3681 +         break;
3682 +      case 2:
3683 +         s->block[s->nblock] = (UChar)ch; s->nblock++;
3684 +         s->block[s->nblock] = (UChar)ch; s->nblock++;
3685 +         break;
3686 +      case 3:
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++;
3690 +         break;
3691 +      default:
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));
3698 +         s->nblock++;
3699 +         break;
3700 +   }
3701 +}
3702 +
3703 +
3704 +/*---------------------------------------------------*/
3705 +static
3706 +void flush_RL ( EState* s )
3707 +{
3708 +   if (s->state_in_ch < 256) add_pair_to_block ( s );
3709 +   init_RL ( s );
3710 +}
3711 +
3712 +
3713 +/*---------------------------------------------------*/
3714 +#define ADD_CHAR_TO_BLOCK(zs,zchh0)               \
3715 +{                                                 \
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;          \
3724 +      zs->nblock++;                               \
3725 +      zs->state_in_ch = zchh;                     \
3726 +   }                                              \
3727 +   else                                           \
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;                       \
3735 +   } else {                                       \
3736 +      zs->state_in_len++;                         \
3737 +   }                                              \
3738 +}
3739 +
3740 +
3741 +/*---------------------------------------------------*/
3742 +static
3743 +Bool copy_input_until_stop ( EState* s )
3744 +{
3745 +   Bool progress_in = False;
3746 +
3747 +   if (s->mode == BZ_M_RUNNING) {
3748 +
3749 +      /*-- fast track the common case --*/
3750 +      while (True) {
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++;
3761 +      }
3762 +
3763 +   } else {
3764 +
3765 +      /*-- general, uncommon case --*/
3766 +      while (True) {
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--;
3780 +      }
3781 +   }
3782 +   return progress_in;
3783 +}
3784 +
3785 +
3786 +/*---------------------------------------------------*/
3787 +static
3788 +Bool copy_output_until_stop ( EState* s )
3789 +{
3790 +   Bool progress_out = False;
3791 +
3792 +   while (True) {
3793 +
3794 +      /*-- no output space? --*/
3795 +      if (s->strm->avail_out == 0) break;
3796 +
3797 +      /*-- block done? --*/
3798 +      if (s->state_out_pos >= s->numZ) break;
3799 +
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++;
3807 +   }
3808 +
3809 +   return progress_out;
3810 +}
3811 +
3812 +
3813 +/*---------------------------------------------------*/
3814 +static
3815 +Bool handle_compress ( bz_stream* strm )
3816 +{
3817 +   Bool progress_in  = False;
3818 +   Bool progress_out = False;
3819 +   EState* s = strm->state;
3820 +   
3821 +   while (True) {
3822 +
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;
3834 +      }
3835 +
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) {
3839 +            flush_RL ( s );
3840 +            BZ2_compressBlock ( s, (Bool)(s->mode == BZ_M_FINISHING) );
3841 +            s->state = BZ_S_OUTPUT;
3842 +         }
3843 +         else
3844 +         if (s->nblock >= s->nblockMAX) {
3845 +            BZ2_compressBlock ( s, False );
3846 +            s->state = BZ_S_OUTPUT;
3847 +         }
3848 +         else
3849 +         if (s->strm->avail_in == 0) {
3850 +            break;
3851 +         }
3852 +      }
3853 +
3854 +   }
3855 +
3856 +   return progress_in || progress_out;
3857 +}
3858 +
3859 +
3860 +/*---------------------------------------------------*/
3861 +int BZ_API(BZ2_bzCompress) ( bz_stream *strm, int action )
3862 +{
3863 +   Bool progress;
3864 +   EState* s;
3865 +   if (strm == NULL) return BZ_PARAM_ERROR;
3866 +   s = strm->state;
3867 +   if (s == NULL) return BZ_PARAM_ERROR;
3868 +   if (s->strm != strm) return BZ_PARAM_ERROR;
3869 +
3870 +   preswitch:
3871 +   switch (s->mode) {
3872 +
3873 +      case BZ_M_IDLE:
3874 +         return BZ_SEQUENCE_ERROR;
3875 +
3876 +      case BZ_M_RUNNING:
3877 +         if (action == BZ_RUN) {
3878 +            progress = handle_compress ( strm );
3879 +            return progress ? BZ_RUN_OK : BZ_PARAM_ERROR;
3880 +         } 
3881 +         else
3882 +        if (action == BZ_FLUSH) {
3883 +            s->avail_in_expect = strm->avail_in;
3884 +            s->mode = BZ_M_FLUSHING;
3885 +            goto preswitch;
3886 +         }
3887 +         else
3888 +         if (action == BZ_FINISH) {
3889 +            s->avail_in_expect = strm->avail_in;
3890 +            s->mode = BZ_M_FINISHING;
3891 +            goto preswitch;
3892 +         }
3893 +         else 
3894 +            return BZ_PARAM_ERROR;
3895 +
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;
3904 +         return BZ_RUN_OK;
3905 +
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;
3916 +   }
3917 +   return BZ_OK; /*--not reached--*/
3918 +}
3919 +
3920 +
3921 +/*---------------------------------------------------*/
3922 +int BZ_API(BZ2_bzCompressEnd)  ( bz_stream *strm )
3923 +{
3924 +   EState* s;
3925 +   if (strm == NULL) return BZ_PARAM_ERROR;
3926 +   s = strm->state;
3927 +   if (s == NULL) return BZ_PARAM_ERROR;
3928 +   if (s->strm != strm) return BZ_PARAM_ERROR;
3929 +
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);
3934 +
3935 +   strm->state = NULL;   
3936 +
3937 +   return BZ_OK;
3938 +}
3939 +
3940 +
3941 +/*---------------------------------------------------*/
3942 +/*--- Decompression stuff                         ---*/
3943 +/*---------------------------------------------------*/
3944 +
3945 +/*---------------------------------------------------*/
3946 +int BZ_API(BZ2_bzDecompressInit) 
3947 +                     ( bz_stream* strm, 
3948 +                       int        verbosity,
3949 +                       int        small )
3950 +{
3951 +   DState* s;
3952 +
3953 +   if (!bz_config_ok()) return BZ_CONFIG_ERROR;
3954 +
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;
3958 +
3959 +   if (strm->bzalloc == NULL) strm->bzalloc = default_bzalloc;
3960 +   if (strm->bzfree == NULL) strm->bzfree = default_bzfree;
3961 +
3962 +   s = BZALLOC( sizeof(DState) );
3963 +   if (s == NULL) return BZ_MEM_ERROR;
3964 +   s->strm                  = strm;
3965 +   strm->state              = s;
3966 +   s->state                 = BZ_X_MAGIC_1;
3967 +   s->bsLive                = 0;
3968 +   s->bsBuff                = 0;
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;
3975 +   s->ll4                   = NULL;
3976 +   s->ll16                  = NULL;
3977 +   s->tt                    = NULL;
3978 +   s->currBlockNo           = 0;
3979 +   s->verbosity             = verbosity;
3980 +
3981 +   return BZ_OK;
3982 +}
3983 +
3984 +
3985 +/*---------------------------------------------------*/
3986 +/* Return  True iff data corruption is discovered.
3987 +   Returns False if there is no problem.
3988 +*/
3989 +static
3990 +Bool unRLE_obuf_to_output_FAST ( DState* s )
3991 +{
3992 +   UChar k1;
3993 +
3994 +   if (s->blockRandomised) {
3995 +
3996 +      while (True) {
3997 +         /* try to finish existing run */
3998 +         while (True) {
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++;
4008 +         }
4009 +
4010 +         /* can a new run be started? */
4011 +         if (s->nblock_used == s->save_nblock+1) return False;
4012 +               
4013 +         /* Only caused by corrupt data stream? */
4014 +         if (s->nblock_used > s->save_nblock+1)
4015 +            return True;
4016 +   
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; };
4023 +   
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; };
4029 +   
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; };
4035 +   
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++;
4041 +      }
4042 +
4043 +   } else {
4044 +
4045 +      /* restore */
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;
4056 +      /* end restore */
4057 +
4058 +      UInt32       avail_out_INIT = cs_avail_out;
4059 +      Int32        s_save_nblockPP = s->save_nblock+1;
4060 +      unsigned int total_out_lo32_old;
4061 +
4062 +      while (True) {
4063 +
4064 +         /* try to finish existing run */
4065 +         if (c_state_out_len > 0) {
4066 +            while (True) {
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--;
4072 +               cs_next_out++;
4073 +               cs_avail_out--;
4074 +            }
4075 +            s_state_out_len_eq_one:
4076 +            {
4077 +               if (cs_avail_out == 0) { 
4078 +                  c_state_out_len = 1; goto return_notr;
4079 +               };
4080 +               *( (UChar*)(cs_next_out) ) = c_state_out_ch;
4081 +               BZ_UPDATE_CRC ( c_calculatedBlockCRC, c_state_out_ch );
4082 +               cs_next_out++;
4083 +               cs_avail_out--;
4084 +            }
4085 +         }   
4086 +         /* Only caused by corrupt data stream? */
4087 +         if (c_nblock_used > s_save_nblockPP)
4088 +            return True;
4089 +
4090 +         /* can a new run be started? */
4091 +         if (c_nblock_used == s_save_nblockPP) {
4092 +            c_state_out_len = 0; goto return_notr;
4093 +         };   
4094 +         c_state_out_ch = c_k0;
4095 +         BZ_GET_FAST_C(k1); c_nblock_used++;
4096 +         if (k1 != c_k0) { 
4097 +            c_k0 = k1; goto s_state_out_len_eq_one; 
4098 +         };
4099 +         if (c_nblock_used == s_save_nblockPP) 
4100 +            goto s_state_out_len_eq_one;
4101 +   
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; };
4106 +   
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; };
4111 +   
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++;
4115 +      }
4116 +
4117 +      return_notr:
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++;
4122 +
4123 +      /* save */
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;
4128 +      s->k0                 = c_k0;
4129 +      s->tt                 = c_tt;
4130 +      s->tPos               = c_tPos;
4131 +      s->strm->next_out     = cs_next_out;
4132 +      s->strm->avail_out    = cs_avail_out;
4133 +      /* end save */
4134 +   }
4135 +   return False;
4136 +}
4137 +
4138 +
4139 +
4140 +/*---------------------------------------------------*/
4141 +__inline__ Int32 BZ2_indexIntoF ( Int32 indx, Int32 *cftab )
4142 +{
4143 +   Int32 nb, na, mid;
4144 +   nb = 0;
4145 +   na = 256;
4146 +   do {
4147 +      mid = (nb + na) >> 1;
4148 +      if (indx >= cftab[mid]) nb = mid; else na = mid;
4149 +   }
4150 +   while (na - nb != 1);
4151 +   return nb;
4152 +}
4153 +
4154 +
4155 +/*---------------------------------------------------*/
4156 +/* Return  True iff data corruption is discovered.
4157 +   Returns False if there is no problem.
4158 +*/
4159 +static
4160 +Bool unRLE_obuf_to_output_SMALL ( DState* s )
4161 +{
4162 +   UChar k1;
4163 +
4164 +   if (s->blockRandomised) {
4165 +
4166 +      while (True) {
4167 +         /* try to finish existing run */
4168 +         while (True) {
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++;
4178 +         }
4179 +   
4180 +         /* can a new run be started? */
4181 +         if (s->nblock_used == s->save_nblock+1) return False;
4182 +
4183 +         /* Only caused by corrupt data stream? */
4184 +         if (s->nblock_used > s->save_nblock+1)
4185 +            return True;
4186 +   
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; };
4193 +   
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; };
4199 +   
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; };
4205 +   
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++;
4211 +      }
4212 +
4213 +   } else {
4214 +
4215 +      while (True) {
4216 +         /* try to finish existing run */
4217 +         while (True) {
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++;
4227 +         }
4228 +   
4229 +         /* can a new run be started? */
4230 +         if (s->nblock_used == s->save_nblock+1) return False;
4231 +
4232 +         /* Only caused by corrupt data stream? */
4233 +         if (s->nblock_used > s->save_nblock+1)
4234 +            return True;
4235 +   
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; };
4241 +   
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; };
4246 +   
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; };
4251 +   
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++;
4255 +      }
4256 +
4257 +   }
4258 +}
4259 +
4260 +
4261 +/*---------------------------------------------------*/
4262 +int BZ_API(BZ2_bzDecompress) ( bz_stream *strm )
4263 +{
4264 +   Bool    corrupt;
4265 +   DState* s;
4266 +   if (strm == NULL) return BZ_PARAM_ERROR;
4267 +   s = strm->state;
4268 +   if (s == NULL) return BZ_PARAM_ERROR;
4269 +   if (s->strm != strm) return BZ_PARAM_ERROR;
4270 +
4271 +   while (True) {
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;
4291 +         } else {
4292 +            return BZ_OK;
4293 +         }
4294 +      }
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;
4303 +            return r;
4304 +         }
4305 +         if (s->state != BZ_X_OUTPUT) return r;
4306 +      }
4307 +   }
4308 +
4309 +   AssertH ( 0, 6001 );
4310 +
4311 +   return 0;  /*NOTREACHED*/
4312 +}
4313 +
4314 +
4315 +/*---------------------------------------------------*/
4316 +int BZ_API(BZ2_bzDecompressEnd)  ( bz_stream *strm )
4317 +{
4318 +   DState* s;
4319 +   if (strm == NULL) return BZ_PARAM_ERROR;
4320 +   s = strm->state;
4321 +   if (s == NULL) return BZ_PARAM_ERROR;
4322 +   if (s->strm != strm) return BZ_PARAM_ERROR;
4323 +
4324 +   if (s->tt   != NULL) BZFREE(s->tt);
4325 +   if (s->ll16 != NULL) BZFREE(s->ll16);
4326 +   if (s->ll4  != NULL) BZFREE(s->ll4);
4327 +
4328 +   BZFREE(strm->state);
4329 +   strm->state = NULL;
4330 +
4331 +   return BZ_OK;
4332 +}
4333 +
4334 +
4335 +#ifndef BZ_NO_STDIO
4336 +/*---------------------------------------------------*/
4337 +/*--- File I/O stuff                              ---*/
4338 +/*---------------------------------------------------*/
4339 +
4340 +#define BZ_SETERR(eee)                    \
4341 +{                                         \
4342 +   if (bzerror != NULL) *bzerror = eee;   \
4343 +   if (bzf != NULL) bzf->lastErr = eee;   \
4344 +}
4345 +
4346 +typedef 
4347 +   struct {
4348 +      FILE*     handle;
4349 +      Char      buf[BZ_MAX_UNUSED];
4350 +      Int32     bufN;
4351 +      Bool      writing;
4352 +      bz_stream strm;
4353 +      Int32     lastErr;
4354 +      Bool      initialisedOk;
4355 +   }
4356 +   bzFile;
4357 +
4358 +
4359 +/*---------------------------------------------*/
4360 +static Bool myfeof ( FILE* f )
4361 +{
4362 +   Int32 c = fgetc ( f );
4363 +   if (c == EOF) return True;
4364 +   ungetc ( c, f );
4365 +   return False;
4366 +}
4367 +
4368 +
4369 +/*---------------------------------------------------*/
4370 +BZFILE* BZ_API(BZ2_bzWriteOpen) 
4371 +                    ( int*  bzerror,      
4372 +                      FILE* f, 
4373 +                      int   blockSize100k, 
4374 +                      int   verbosity,
4375 +                      int   workFactor )
4376 +{
4377 +   Int32   ret;
4378 +   bzFile* bzf = NULL;
4379 +
4380 +   BZ_SETERR(BZ_OK);
4381 +
4382 +   if (f == NULL ||
4383 +       (blockSize100k < 1 || blockSize100k > 9) ||
4384 +       (workFactor < 0 || workFactor > 250) ||
4385 +       (verbosity < 0 || verbosity > 4))
4386 +      { BZ_SETERR(BZ_PARAM_ERROR); return NULL; };
4387 +
4388 +   if (ferror(f))
4389 +      { BZ_SETERR(BZ_IO_ERROR); return NULL; };
4390 +
4391 +   bzf = malloc ( sizeof(bzFile) );
4392 +   if (bzf == NULL)
4393 +      { BZ_SETERR(BZ_MEM_ERROR); return NULL; };
4394 +
4395 +   BZ_SETERR(BZ_OK);
4396 +   bzf->initialisedOk = False;
4397 +   bzf->bufN          = 0;
4398 +   bzf->handle        = f;
4399 +   bzf->writing       = True;
4400 +   bzf->strm.bzalloc  = NULL;
4401 +   bzf->strm.bzfree   = NULL;
4402 +   bzf->strm.opaque   = NULL;
4403 +
4404 +   if (workFactor == 0) workFactor = 30;
4405 +   ret = BZ2_bzCompressInit ( &(bzf->strm), blockSize100k, 
4406 +                              verbosity, workFactor );
4407 +   if (ret != BZ_OK)
4408 +      { BZ_SETERR(ret); free(bzf); return NULL; };
4409 +
4410 +   bzf->strm.avail_in = 0;
4411 +   bzf->initialisedOk = True;
4412 +   return bzf;   
4413 +}
4414 +
4415 +
4416 +
4417 +/*---------------------------------------------------*/
4418 +void BZ_API(BZ2_bzWrite)
4419 +             ( int*    bzerror, 
4420 +               BZFILE* b, 
4421 +               void*   buf, 
4422 +               int     len )
4423 +{
4424 +   Int32 n, n2, ret;
4425 +   bzFile* bzf = (bzFile*)b;
4426 +
4427 +   BZ_SETERR(BZ_OK);
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; };
4434 +
4435 +   if (len == 0)
4436 +      { BZ_SETERR(BZ_OK); return; };
4437 +
4438 +   bzf->strm.avail_in = len;
4439 +   bzf->strm.next_in  = buf;
4440 +
4441 +   while (True) {
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; };
4447 +
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), 
4451 +                       n, bzf->handle );
4452 +         if (n != n2 || ferror(bzf->handle))
4453 +            { BZ_SETERR(BZ_IO_ERROR); return; };
4454 +      }
4455 +
4456 +      if (bzf->strm.avail_in == 0)
4457 +         { BZ_SETERR(BZ_OK); return; };
4458 +   }
4459 +}
4460 +
4461 +
4462 +/*---------------------------------------------------*/
4463 +void BZ_API(BZ2_bzWriteClose)
4464 +                  ( int*          bzerror, 
4465 +                    BZFILE*       b, 
4466 +                    int           abandon,
4467 +                    unsigned int* nbytes_in,
4468 +                    unsigned int* nbytes_out )
4469 +{
4470 +   BZ2_bzWriteClose64 ( bzerror, b, abandon, 
4471 +                        nbytes_in, NULL, nbytes_out, NULL );
4472 +}
4473 +
4474 +
4475 +void BZ_API(BZ2_bzWriteClose64)
4476 +                  ( int*          bzerror, 
4477 +                    BZFILE*       b, 
4478 +                    int           abandon,
4479 +                    unsigned int* nbytes_in_lo32,
4480 +                    unsigned int* nbytes_in_hi32,
4481 +                    unsigned int* nbytes_out_lo32,
4482 +                    unsigned int* nbytes_out_hi32 )
4483 +{
4484 +   Int32   n, n2, ret;
4485 +   bzFile* bzf = (bzFile*)b;
4486 +
4487 +   if (bzf == NULL)
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; };
4493 +
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;
4498 +
4499 +   if ((!abandon) && bzf->lastErr == BZ_OK) {
4500 +      while (True) {
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; };
4506 +
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), 
4510 +                          n, bzf->handle );
4511 +            if (n != n2 || ferror(bzf->handle))
4512 +               { BZ_SETERR(BZ_IO_ERROR); return; };
4513 +         }
4514 +
4515 +         if (ret == BZ_STREAM_END) break;
4516 +      }
4517 +   }
4518 +
4519 +   if ( !abandon && !ferror ( bzf->handle ) ) {
4520 +      fflush ( bzf->handle );
4521 +      if (ferror(bzf->handle))
4522 +         { BZ_SETERR(BZ_IO_ERROR); return; };
4523 +   }
4524 +
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;
4533 +
4534 +   BZ_SETERR(BZ_OK);
4535 +   BZ2_bzCompressEnd ( &(bzf->strm) );
4536 +   free ( bzf );
4537 +}
4538 +
4539 +
4540 +/*---------------------------------------------------*/
4541 +BZFILE* BZ_API(BZ2_bzReadOpen) 
4542 +                   ( int*  bzerror, 
4543 +                     FILE* f, 
4544 +                     int   verbosity,
4545 +                     int   small,
4546 +                     void* unused,
4547 +                     int   nUnused )
4548 +{
4549 +   bzFile* bzf = NULL;
4550 +   int     ret;
4551 +
4552 +   BZ_SETERR(BZ_OK);
4553 +
4554 +   if (f == 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; };
4560 +
4561 +   if (ferror(f))
4562 +      { BZ_SETERR(BZ_IO_ERROR); return NULL; };
4563 +
4564 +   bzf = malloc ( sizeof(bzFile) );
4565 +   if (bzf == NULL) 
4566 +      { BZ_SETERR(BZ_MEM_ERROR); return NULL; };
4567 +
4568 +   BZ_SETERR(BZ_OK);
4569 +
4570 +   bzf->initialisedOk = False;
4571 +   bzf->handle        = f;
4572 +   bzf->bufN          = 0;
4573 +   bzf->writing       = False;
4574 +   bzf->strm.bzalloc  = NULL;
4575 +   bzf->strm.bzfree   = NULL;
4576 +   bzf->strm.opaque   = NULL;
4577 +   
4578 +   while (nUnused > 0) {
4579 +      bzf->buf[bzf->bufN] = *((UChar*)(unused)); bzf->bufN++;
4580 +      unused = ((void*)( 1 + ((UChar*)(unused))  ));
4581 +      nUnused--;
4582 +   }
4583 +
4584 +   ret = BZ2_bzDecompressInit ( &(bzf->strm), verbosity, small );
4585 +   if (ret != BZ_OK)
4586 +      { BZ_SETERR(ret); free(bzf); return NULL; };
4587 +
4588 +   bzf->strm.avail_in = bzf->bufN;
4589 +   bzf->strm.next_in  = bzf->buf;
4590 +
4591 +   bzf->initialisedOk = True;
4592 +   return bzf;   
4593 +}
4594 +
4595 +
4596 +/*---------------------------------------------------*/
4597 +void BZ_API(BZ2_bzReadClose) ( int *bzerror, BZFILE *b )
4598 +{
4599 +   bzFile* bzf = (bzFile*)b;
4600 +
4601 +   BZ_SETERR(BZ_OK);
4602 +   if (bzf == NULL)
4603 +      { BZ_SETERR(BZ_OK); return; };
4604 +
4605 +   if (bzf->writing)
4606 +      { BZ_SETERR(BZ_SEQUENCE_ERROR); return; };
4607 +
4608 +   if (bzf->initialisedOk)
4609 +      (void)BZ2_bzDecompressEnd ( &(bzf->strm) );
4610 +   free ( bzf );
4611 +}
4612 +
4613 +
4614 +/*---------------------------------------------------*/
4615 +int BZ_API(BZ2_bzRead) 
4616 +           ( int*    bzerror, 
4617 +             BZFILE* b, 
4618 +             void*   buf, 
4619 +             int     len )
4620 +{
4621 +   Int32   n, ret;
4622 +   bzFile* bzf = (bzFile*)b;
4623 +
4624 +   BZ_SETERR(BZ_OK);
4625 +
4626 +   if (bzf == NULL || buf == NULL || len < 0)
4627 +      { BZ_SETERR(BZ_PARAM_ERROR); return 0; };
4628 +
4629 +   if (bzf->writing)
4630 +      { BZ_SETERR(BZ_SEQUENCE_ERROR); return 0; };
4631 +
4632 +   if (len == 0)
4633 +      { BZ_SETERR(BZ_OK); return 0; };
4634 +
4635 +   bzf->strm.avail_out = len;
4636 +   bzf->strm.next_out = buf;
4637 +
4638 +   while (True) {
4639 +
4640 +      if (ferror(bzf->handle)) 
4641 +         { BZ_SETERR(BZ_IO_ERROR); return 0; };
4642 +
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; };
4648 +         bzf->bufN = n;
4649 +         bzf->strm.avail_in = bzf->bufN;
4650 +         bzf->strm.next_in = bzf->buf;
4651 +      }
4652 +
4653 +      ret = BZ2_bzDecompress ( &(bzf->strm) );
4654 +
4655 +      if (ret != BZ_OK && ret != BZ_STREAM_END)
4656 +         { BZ_SETERR(ret); return 0; };
4657 +
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; };
4661 +
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; };
4667 +      
4668 +   }
4669 +
4670 +   return 0; /*not reached*/
4671 +}
4672 +
4673 +
4674 +/*---------------------------------------------------*/
4675 +void BZ_API(BZ2_bzReadGetUnused) 
4676 +                     ( int*    bzerror, 
4677 +                       BZFILE* b, 
4678 +                       void**  unused, 
4679 +                       int*    nUnused )
4680 +{
4681 +   bzFile* bzf = (bzFile*)b;
4682 +   if (bzf == NULL)
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; };
4688 +
4689 +   BZ_SETERR(BZ_OK);
4690 +   *nUnused = bzf->strm.avail_in;
4691 +   *unused = bzf->strm.next_in;
4692 +}
4693 +#endif
4694 +
4695 +
4696 +/*---------------------------------------------------*/
4697 +/*--- Misc convenience stuff                      ---*/
4698 +/*---------------------------------------------------*/
4699 +
4700 +/*---------------------------------------------------*/
4701 +int BZ_API(BZ2_bzBuffToBuffCompress) 
4702 +                         ( char*         dest, 
4703 +                           unsigned int* destLen,
4704 +                           char*         source, 
4705 +                           unsigned int  sourceLen,
4706 +                           int           blockSize100k, 
4707 +                           int           verbosity, 
4708 +                           int           workFactor )
4709 +{
4710 +   bz_stream strm;
4711 +   int ret;
4712 +
4713 +   if (dest == NULL || destLen == NULL || 
4714 +       source == NULL ||
4715 +       blockSize100k < 1 || blockSize100k > 9 ||
4716 +       verbosity < 0 || verbosity > 4 ||
4717 +       workFactor < 0 || workFactor > 250) 
4718 +      return BZ_PARAM_ERROR;
4719 +
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;
4727 +
4728 +   strm.next_in = source;
4729 +   strm.next_out = dest;
4730 +   strm.avail_in = sourceLen;
4731 +   strm.avail_out = *destLen;
4732 +
4733 +   ret = BZ2_bzCompress ( &strm, BZ_FINISH );
4734 +   if (ret == BZ_FINISH_OK) goto output_overflow;
4735 +   if (ret != BZ_STREAM_END) goto errhandler;
4736 +
4737 +   /* normal termination */
4738 +   *destLen -= strm.avail_out;   
4739 +   BZ2_bzCompressEnd ( &strm );
4740 +   return BZ_OK;
4741 +
4742 +   output_overflow:
4743 +   BZ2_bzCompressEnd ( &strm );
4744 +   return BZ_OUTBUFF_FULL;
4745 +
4746 +   errhandler:
4747 +   BZ2_bzCompressEnd ( &strm );
4748 +   return ret;
4749 +}
4750 +
4751 +
4752 +/*---------------------------------------------------*/
4753 +int BZ_API(BZ2_bzBuffToBuffDecompress) 
4754 +                           ( char*         dest, 
4755 +                             unsigned int* destLen,
4756 +                             char*         source, 
4757 +                             unsigned int  sourceLen,
4758 +                             int           small,
4759 +                             int           verbosity )
4760 +{
4761 +   bz_stream strm;
4762 +   int ret;
4763 +
4764 +   if (dest == NULL || destLen == NULL || 
4765 +       source == NULL ||
4766 +       (small != 0 && small != 1) ||
4767 +       verbosity < 0 || verbosity > 4) 
4768 +          return BZ_PARAM_ERROR;
4769 +
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;
4775 +
4776 +   strm.next_in = source;
4777 +   strm.next_out = dest;
4778 +   strm.avail_in = sourceLen;
4779 +   strm.avail_out = *destLen;
4780 +
4781 +   ret = BZ2_bzDecompress ( &strm );
4782 +   if (ret == BZ_OK) goto output_overflow_or_eof;
4783 +   if (ret != BZ_STREAM_END) goto errhandler;
4784 +
4785 +   /* normal termination */
4786 +   *destLen -= strm.avail_out;
4787 +   BZ2_bzDecompressEnd ( &strm );
4788 +   return BZ_OK;
4789 +
4790 +   output_overflow_or_eof:
4791 +   if (strm.avail_out > 0) {
4792 +      BZ2_bzDecompressEnd ( &strm );
4793 +      return BZ_UNEXPECTED_EOF;
4794 +   } else {
4795 +      BZ2_bzDecompressEnd ( &strm );
4796 +      return BZ_OUTBUFF_FULL;
4797 +   };      
4798 +
4799 +   errhandler:
4800 +   BZ2_bzDecompressEnd ( &strm );
4801 +   return ret; 
4802 +}
4803 +
4804 +
4805 +/*---------------------------------------------------*/
4806 +/*--
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.
4813 +--*/
4814 +/*---------------------------------------------------*/
4815 +
4816 +/*---------------------------------------------------*/
4817 +/*--
4818 +   return version like "0.9.5d, 4-Sept-1999".
4819 +--*/
4820 +const char * BZ_API(BZ2_bzlibVersion)(void)
4821 +{
4822 +   return BZ_VERSION;
4823 +}
4824 +
4825 +
4826 +#ifndef BZ_NO_STDIO
4827 +/*---------------------------------------------------*/
4828 +
4829 +#if defined(_WIN32) || defined(OS2) || defined(MSDOS)
4830 +#   include <fcntl.h>
4831 +#   include <io.h>
4832 +#   define SET_BINARY_MODE(file) setmode(fileno(file),O_BINARY)
4833 +#else
4834 +#   define SET_BINARY_MODE(file)
4835 +#endif
4836 +static
4837 +BZFILE * bzopen_or_bzdopen
4838 +               ( const char *path,   /* no use when bzdopen */
4839 +                 int fd,             /* no use when bzdopen */
4840 +                 const char *mode,
4841 +                 int open_mode)      /* bzopen: 0, bzdopen:1 */
4842 +{
4843 +   int    bzerr;
4844 +   char   unused[BZ_MAX_UNUSED];
4845 +   int    blockSize100k = 9;
4846 +   int    writing       = 0;
4847 +   char   mode2[10]     = "";
4848 +   FILE   *fp           = NULL;
4849 +   BZFILE *bzfp         = NULL;
4850 +   int    verbosity     = 0;
4851 +   int    workFactor    = 30;
4852 +   int    smallMode     = 0;
4853 +   int    nUnused       = 0; 
4854 +
4855 +   if (mode == NULL) return NULL;
4856 +   while (*mode) {
4857 +      switch (*mode) {
4858 +      case 'r':
4859 +         writing = 0; break;
4860 +      case 'w':
4861 +         writing = 1; break;
4862 +      case 's':
4863 +         smallMode = 1; break;
4864 +      default:
4865 +         if (isdigit((int)(*mode))) {
4866 +            blockSize100k = *mode-BZ_HDR_0;
4867 +         }
4868 +      }
4869 +      mode++;
4870 +   }
4871 +   strcat(mode2, writing ? "w" : "r" );
4872 +   strcat(mode2,"b");   /* binary mode */
4873 +
4874 +   if (open_mode==0) {
4875 +      if (path==NULL || strcmp(path,"")==0) {
4876 +        fp = (writing ? stdout : stdin);
4877 +        SET_BINARY_MODE(fp);
4878 +      } else {
4879 +        fp = fopen(path,mode2);
4880 +      }
4881 +   } else {
4882 +#ifdef BZ_STRICT_ANSI
4883 +      fp = NULL;
4884 +#else
4885 +      fp = fdopen(fd,mode2);
4886 +#endif
4887 +   }
4888 +   if (fp == NULL) return NULL;
4889 +
4890 +   if (writing) {
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);
4896 +   } else {
4897 +      bzfp = BZ2_bzReadOpen(&bzerr,fp,verbosity,smallMode,
4898 +                            unused,nUnused);
4899 +   }
4900 +   if (bzfp == NULL) {
4901 +      if (fp != stdin && fp != stdout) fclose(fp);
4902 +      return NULL;
4903 +   }
4904 +   return bzfp;
4905 +}
4906 +
4907 +
4908 +/*---------------------------------------------------*/
4909 +/*--
4910 +   open file for read or write.
4911 +      ex) bzopen("file","w9")
4912 +      case path="" or NULL => use stdin or stdout.
4913 +--*/
4914 +BZFILE * BZ_API(BZ2_bzopen)
4915 +               ( const char *path,
4916 +                 const char *mode )
4917 +{
4918 +   return bzopen_or_bzdopen(path,-1,mode,/*bzopen*/0);
4919 +}
4920 +
4921 +
4922 +/*---------------------------------------------------*/
4923 +BZFILE * BZ_API(BZ2_bzdopen)
4924 +               ( int fd,
4925 +                 const char *mode )
4926 +{
4927 +   return bzopen_or_bzdopen(NULL,fd,mode,/*bzdopen*/1);
4928 +}
4929 +
4930 +
4931 +/*---------------------------------------------------*/
4932 +int BZ_API(BZ2_bzread) (BZFILE* b, void* buf, int len )
4933 +{
4934 +   int bzerr, nread;
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) {
4938 +      return nread;
4939 +   } else {
4940 +      return -1;
4941 +   }
4942 +}
4943 +
4944 +
4945 +/*---------------------------------------------------*/
4946 +int BZ_API(BZ2_bzwrite) (BZFILE* b, void* buf, int len )
4947 +{
4948 +   int bzerr;
4949 +
4950 +   BZ2_bzWrite(&bzerr,b,buf,len);
4951 +   if(bzerr == BZ_OK){
4952 +      return len;
4953 +   }else{
4954 +      return -1;
4955 +   }
4956 +}
4957 +
4958 +
4959 +/*---------------------------------------------------*/
4960 +int BZ_API(BZ2_bzflush) (BZFILE *b)
4961 +{
4962 +   /* do nothing now... */
4963 +   return 0;
4964 +}
4965 +
4966 +
4967 +/*---------------------------------------------------*/
4968 +void BZ_API(BZ2_bzclose) (BZFILE* b)
4969 +{
4970 +   int bzerr;
4971 +   FILE *fp;
4972 +   
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);
4979 +      }
4980 +   }else{
4981 +      BZ2_bzReadClose(&bzerr,b);
4982 +   }
4983 +   if(fp!=stdin && fp!=stdout){
4984 +      fclose(fp);
4985 +   }
4986 +}
4987 +
4988 +
4989 +/*---------------------------------------------------*/
4990 +/*--
4991 +   return last error code 
4992 +--*/
4993 +static const char *bzerrorstrings[] = {
4994 +       "OK"
4995 +      ,"SEQUENCE_ERROR"
4996 +      ,"PARAM_ERROR"
4997 +      ,"MEM_ERROR"
4998 +      ,"DATA_ERROR"
4999 +      ,"DATA_ERROR_MAGIC"
5000 +      ,"IO_ERROR"
5001 +      ,"UNEXPECTED_EOF"
5002 +      ,"OUTBUFF_FULL"
5003 +      ,"CONFIG_ERROR"
5004 +      ,"???"   /* for future */
5005 +      ,"???"   /* for future */
5006 +      ,"???"   /* for future */
5007 +      ,"???"   /* for future */
5008 +      ,"???"   /* for future */
5009 +      ,"???"   /* for future */
5010 +};
5011 +
5012 +
5013 +const char * BZ_API(BZ2_bzerror) (BZFILE *b, int *errnum)
5014 +{
5015 +   int err = ((bzFile *)b)->lastErr;
5016 +
5017 +   if(err>0) err = 0;
5018 +   *errnum = err;
5019 +   return bzerrorstrings[err*-1];
5020 +}
5021 +#endif
5022 +
5023 +
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
5030 --- /dev/null
5031 +++ b/pit/upstream-bzip2/bzlib.h
5032 @@ -0,0 +1,282 @@
5033 +
5034 +/*-------------------------------------------------------------*/
5035 +/*--- Public header file for the library.                   ---*/
5036 +/*---                                               bzlib.h ---*/
5037 +/*-------------------------------------------------------------*/
5038 +
5039 +/* ------------------------------------------------------------------
5040 +   This file is part of bzip2/libbzip2, a program and library for
5041 +   lossless, block-sorting data compression.
5042 +
5043 +   bzip2/libbzip2 version 1.0.6 of 6 September 2010
5044 +   Copyright (C) 1996-2010 Julian Seward <jseward@bzip.org>
5045 +
5046 +   Please read the WARNING, DISCLAIMER and PATENTS sections in the 
5047 +   README file.
5048 +
5049 +   This program is released under the terms of the license contained
5050 +   in the file LICENSE.
5051 +   ------------------------------------------------------------------ */
5052 +
5053 +
5054 +#ifndef _BZLIB_H
5055 +#define _BZLIB_H
5056 +
5057 +#ifdef __cplusplus
5058 +extern "C" {
5059 +#endif
5060 +
5061 +#define BZ_RUN               0
5062 +#define BZ_FLUSH             1
5063 +#define BZ_FINISH            2
5064 +
5065 +#define BZ_OK                0
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)
5079 +
5080 +typedef 
5081 +   struct {
5082 +      char *next_in;
5083 +      unsigned int avail_in;
5084 +      unsigned int total_in_lo32;
5085 +      unsigned int total_in_hi32;
5086 +
5087 +      char *next_out;
5088 +      unsigned int avail_out;
5089 +      unsigned int total_out_lo32;
5090 +      unsigned int total_out_hi32;
5091 +
5092 +      void *state;
5093 +
5094 +      void *(*bzalloc)(void *,int,int);
5095 +      void (*bzfree)(void *,void *);
5096 +      void *opaque;
5097 +   } 
5098 +   bz_stream;
5099 +
5100 +
5101 +#ifndef BZ_IMPORT
5102 +#define BZ_EXPORT
5103 +#endif
5104 +
5105 +#ifndef BZ_NO_STDIO
5106 +/* Need a definitition for FILE */
5107 +#include <stdio.h>
5108 +#endif
5109 +
5110 +#ifdef _WIN32
5111 +#   include <windows.h>
5112 +#   ifdef small
5113 +      /* windows.h define small to char */
5114 +#      undef small
5115 +#   endif
5116 +#   ifdef BZ_EXPORT
5117 +#   define BZ_API(func) WINAPI func
5118 +#   define BZ_EXTERN extern
5119 +#   else
5120 +   /* import windows dll dynamically */
5121 +#   define BZ_API(func) (WINAPI * func)
5122 +#   define BZ_EXTERN
5123 +#   endif
5124 +#else
5125 +#   define BZ_API(func) func
5126 +#   define BZ_EXTERN extern
5127 +#endif
5128 +
5129 +
5130 +/*-- Core (low-level) library functions --*/
5131 +
5132 +BZ_EXTERN int BZ_API(BZ2_bzCompressInit) ( 
5133 +      bz_stream* strm, 
5134 +      int        blockSize100k, 
5135 +      int        verbosity, 
5136 +      int        workFactor 
5137 +   );
5138 +
5139 +BZ_EXTERN int BZ_API(BZ2_bzCompress) ( 
5140 +      bz_stream* strm, 
5141 +      int action 
5142 +   );
5143 +
5144 +BZ_EXTERN int BZ_API(BZ2_bzCompressEnd) ( 
5145 +      bz_stream* strm 
5146 +   );
5147 +
5148 +BZ_EXTERN int BZ_API(BZ2_bzDecompressInit) ( 
5149 +      bz_stream *strm, 
5150 +      int       verbosity, 
5151 +      int       small
5152 +   );
5153 +
5154 +BZ_EXTERN int BZ_API(BZ2_bzDecompress) ( 
5155 +      bz_stream* strm 
5156 +   );
5157 +
5158 +BZ_EXTERN int BZ_API(BZ2_bzDecompressEnd) ( 
5159 +      bz_stream *strm 
5160 +   );
5161 +
5162 +
5163 +
5164 +/*-- High(er) level library functions --*/
5165 +
5166 +#ifndef BZ_NO_STDIO
5167 +#define BZ_MAX_UNUSED 5000
5168 +
5169 +typedef void BZFILE;
5170 +
5171 +BZ_EXTERN BZFILE* BZ_API(BZ2_bzReadOpen) ( 
5172 +      int*  bzerror,   
5173 +      FILE* f, 
5174 +      int   verbosity, 
5175 +      int   small,
5176 +      void* unused,    
5177 +      int   nUnused 
5178 +   );
5179 +
5180 +BZ_EXTERN void BZ_API(BZ2_bzReadClose) ( 
5181 +      int*    bzerror, 
5182 +      BZFILE* b 
5183 +   );
5184 +
5185 +BZ_EXTERN void BZ_API(BZ2_bzReadGetUnused) ( 
5186 +      int*    bzerror, 
5187 +      BZFILE* b, 
5188 +      void**  unused,  
5189 +      int*    nUnused 
5190 +   );
5191 +
5192 +BZ_EXTERN int BZ_API(BZ2_bzRead) ( 
5193 +      int*    bzerror, 
5194 +      BZFILE* b, 
5195 +      void*   buf, 
5196 +      int     len 
5197 +   );
5198 +
5199 +BZ_EXTERN BZFILE* BZ_API(BZ2_bzWriteOpen) ( 
5200 +      int*  bzerror,      
5201 +      FILE* f, 
5202 +      int   blockSize100k, 
5203 +      int   verbosity, 
5204 +      int   workFactor 
5205 +   );
5206 +
5207 +BZ_EXTERN void BZ_API(BZ2_bzWrite) ( 
5208 +      int*    bzerror, 
5209 +      BZFILE* b, 
5210 +      void*   buf, 
5211 +      int     len 
5212 +   );
5213 +
5214 +BZ_EXTERN void BZ_API(BZ2_bzWriteClose) ( 
5215 +      int*          bzerror, 
5216 +      BZFILE*       b, 
5217 +      int           abandon, 
5218 +      unsigned int* nbytes_in, 
5219 +      unsigned int* nbytes_out 
5220 +   );
5221 +
5222 +BZ_EXTERN void BZ_API(BZ2_bzWriteClose64) ( 
5223 +      int*          bzerror, 
5224 +      BZFILE*       b, 
5225 +      int           abandon, 
5226 +      unsigned int* nbytes_in_lo32, 
5227 +      unsigned int* nbytes_in_hi32, 
5228 +      unsigned int* nbytes_out_lo32, 
5229 +      unsigned int* nbytes_out_hi32
5230 +   );
5231 +#endif
5232 +
5233 +
5234 +/*-- Utility functions --*/
5235 +
5236 +BZ_EXTERN int BZ_API(BZ2_bzBuffToBuffCompress) ( 
5237 +      char*         dest, 
5238 +      unsigned int* destLen,
5239 +      char*         source, 
5240 +      unsigned int  sourceLen,
5241 +      int           blockSize100k, 
5242 +      int           verbosity, 
5243 +      int           workFactor 
5244 +   );
5245 +
5246 +BZ_EXTERN int BZ_API(BZ2_bzBuffToBuffDecompress) ( 
5247 +      char*         dest, 
5248 +      unsigned int* destLen,
5249 +      char*         source, 
5250 +      unsigned int  sourceLen,
5251 +      int           small, 
5252 +      int           verbosity 
5253 +   );
5254 +
5255 +
5256 +/*--
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.
5263 +--*/
5264 +
5265 +BZ_EXTERN const char * BZ_API(BZ2_bzlibVersion) (
5266 +      void
5267 +   );
5268 +
5269 +#ifndef BZ_NO_STDIO
5270 +BZ_EXTERN BZFILE * BZ_API(BZ2_bzopen) (
5271 +      const char *path,
5272 +      const char *mode
5273 +   );
5274 +
5275 +BZ_EXTERN BZFILE * BZ_API(BZ2_bzdopen) (
5276 +      int        fd,
5277 +      const char *mode
5278 +   );
5279 +         
5280 +BZ_EXTERN int BZ_API(BZ2_bzread) (
5281 +      BZFILE* b, 
5282 +      void* buf, 
5283 +      int len 
5284 +   );
5285 +
5286 +BZ_EXTERN int BZ_API(BZ2_bzwrite) (
5287 +      BZFILE* b, 
5288 +      void*   buf, 
5289 +      int     len 
5290 +   );
5291 +
5292 +BZ_EXTERN int BZ_API(BZ2_bzflush) (
5293 +      BZFILE* b
5294 +   );
5295 +
5296 +BZ_EXTERN void BZ_API(BZ2_bzclose) (
5297 +      BZFILE* b
5298 +   );
5299 +
5300 +BZ_EXTERN const char * BZ_API(BZ2_bzerror) (
5301 +      BZFILE *b, 
5302 +      int    *errnum
5303 +   );
5304 +#endif
5305 +
5306 +#ifdef __cplusplus
5307 +}
5308 +#endif
5309 +
5310 +#endif
5311 +
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
5318 --- /dev/null
5319 +++ b/pit/upstream-bzip2/bzlib_private.h
5320 @@ -0,0 +1,509 @@
5321 +
5322 +/*-------------------------------------------------------------*/
5323 +/*--- Private header file for the library.                  ---*/
5324 +/*---                                       bzlib_private.h ---*/
5325 +/*-------------------------------------------------------------*/
5326 +
5327 +/* ------------------------------------------------------------------
5328 +   This file is part of bzip2/libbzip2, a program and library for
5329 +   lossless, block-sorting data compression.
5330 +
5331 +   bzip2/libbzip2 version 1.0.6 of 6 September 2010
5332 +   Copyright (C) 1996-2010 Julian Seward <jseward@bzip.org>
5333 +
5334 +   Please read the WARNING, DISCLAIMER and PATENTS sections in the 
5335 +   README file.
5336 +
5337 +   This program is released under the terms of the license contained
5338 +   in the file LICENSE.
5339 +   ------------------------------------------------------------------ */
5340 +
5341 +
5342 +#ifndef _BZLIB_PRIVATE_H
5343 +#define _BZLIB_PRIVATE_H
5344 +
5345 +#include <stdlib.h>
5346 +
5347 +#ifndef BZ_NO_STDIO
5348 +#include <stdio.h>
5349 +#include <ctype.h>
5350 +#include <string.h>
5351 +#endif
5352 +
5353 +#include "bzlib.h"
5354 +
5355 +
5356 +
5357 +/*-- General stuff. --*/
5358 +
5359 +#define BZ_VERSION  "1.0.6, 6-Sept-2010"
5360 +
5361 +typedef char            Char;
5362 +typedef unsigned char   Bool;
5363 +typedef unsigned char   UChar;
5364 +typedef int             Int32;
5365 +typedef unsigned int    UInt32;
5366 +typedef short           Int16;
5367 +typedef unsigned short  UInt16;
5368 +
5369 +#define True  ((Bool)1)
5370 +#define False ((Bool)0)
5371 +
5372 +#ifndef __GNUC__
5373 +#define __inline__  /* */
5374 +#endif 
5375 +
5376 +#ifndef BZ_NO_STDIO
5377 +
5378 +extern void BZ2_bz__AssertH__fail ( int errcode );
5379 +#define AssertH(cond,errcode) \
5380 +   { if (!(cond)) BZ2_bz__AssertH__fail ( errcode ); }
5381 +
5382 +#if BZ_DEBUG
5383 +#define AssertD(cond,msg) \
5384 +   { if (!(cond)) {       \
5385 +      fprintf ( stderr,   \
5386 +        "\n\nlibbzip2(debug build): internal error\n\t%s\n", msg );\
5387 +      exit(1); \
5388 +   }}
5389 +#else
5390 +#define AssertD(cond,msg) /* */
5391 +#endif
5392 +
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)
5405 +
5406 +#else
5407 +
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)
5418 +
5419 +#endif
5420 +
5421 +
5422 +#define BZALLOC(nnn) (strm->bzalloc)(strm->opaque,(nnn),1)
5423 +#define BZFREE(ppp)  (strm->bzfree)(strm->opaque,(ppp))
5424 +
5425 +
5426 +/*-- Header bytes. --*/
5427 +
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' */
5432 +  
5433 +/*-- Constants for the back end. --*/
5434 +
5435 +#define BZ_MAX_ALPHA_SIZE 258
5436 +#define BZ_MAX_CODE_LEN    23
5437 +
5438 +#define BZ_RUNA 0
5439 +#define BZ_RUNB 1
5440 +
5441 +#define BZ_N_GROUPS 6
5442 +#define BZ_G_SIZE   50
5443 +#define BZ_N_ITERS  4
5444 +
5445 +#define BZ_MAX_SELECTORS (2 + (900000 / BZ_G_SIZE))
5446 +
5447 +
5448 +
5449 +/*-- Stuff for randomising repetitive blocks. --*/
5450 +
5451 +extern Int32 BZ2_rNums[512];
5452 +
5453 +#define BZ_RAND_DECLS                          \
5454 +   Int32 rNToGo;                               \
5455 +   Int32 rTPos                                 \
5456 +
5457 +#define BZ_RAND_INIT_MASK                      \
5458 +   s->rNToGo = 0;                              \
5459 +   s->rTPos  = 0                               \
5460 +
5461 +#define BZ_RAND_MASK ((s->rNToGo == 1) ? 1 : 0)
5462 +
5463 +#define BZ_RAND_UPD_MASK                       \
5464 +   if (s->rNToGo == 0) {                       \
5465 +      s->rNToGo = BZ2_rNums[s->rTPos];         \
5466 +      s->rTPos++;                              \
5467 +      if (s->rTPos == 512) s->rTPos = 0;       \
5468 +   }                                           \
5469 +   s->rNToGo--;
5470 +
5471 +
5472 +
5473 +/*-- Stuff for doing CRCs. --*/
5474 +
5475 +extern UInt32 BZ2_crc32Table[256];
5476 +
5477 +#define BZ_INITIALISE_CRC(crcVar)              \
5478 +{                                              \
5479 +   crcVar = 0xffffffffL;                       \
5480 +}
5481 +
5482 +#define BZ_FINALISE_CRC(crcVar)                \
5483 +{                                              \
5484 +   crcVar = ~(crcVar);                         \
5485 +}
5486 +
5487 +#define BZ_UPDATE_CRC(crcVar,cha)              \
5488 +{                                              \
5489 +   crcVar = (crcVar << 8) ^                    \
5490 +            BZ2_crc32Table[(crcVar >> 24) ^    \
5491 +                           ((UChar)cha)];      \
5492 +}
5493 +
5494 +
5495 +
5496 +/*-- States and modes for compression. --*/
5497 +
5498 +#define BZ_M_IDLE      1
5499 +#define BZ_M_RUNNING   2
5500 +#define BZ_M_FLUSHING  3
5501 +#define BZ_M_FINISHING 4
5502 +
5503 +#define BZ_S_OUTPUT    1
5504 +#define BZ_S_INPUT     2
5505 +
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)
5510 +
5511 +
5512 +
5513 +
5514 +/*-- Structure holding all the compression-side stuff. --*/
5515 +
5516 +typedef
5517 +   struct {
5518 +      /* pointer back to the struct bz_stream */
5519 +      bz_stream* strm;
5520 +
5521 +      /* mode this stream is in, and whether inputting */
5522 +      /* or outputting data */
5523 +      Int32    mode;
5524 +      Int32    state;
5525 +
5526 +      /* remembers avail_in when flush/finish requested */
5527 +      UInt32   avail_in_expect;
5528 +
5529 +      /* for doing the block sorting */
5530 +      UInt32*  arr1;
5531 +      UInt32*  arr2;
5532 +      UInt32*  ftab;
5533 +      Int32    origPtr;
5534 +
5535 +      /* aliases for arr1 and arr2 */
5536 +      UInt32*  ptr;
5537 +      UChar*   block;
5538 +      UInt16*  mtfv;
5539 +      UChar*   zbits;
5540 +
5541 +      /* for deciding when to use the fallback sorting algorithm */
5542 +      Int32    workFactor;
5543 +
5544 +      /* run-length-encoding of the input */
5545 +      UInt32   state_in_ch;
5546 +      Int32    state_in_len;
5547 +      BZ_RAND_DECLS;
5548 +
5549 +      /* input and output limits and current posns */
5550 +      Int32    nblock;
5551 +      Int32    nblockMAX;
5552 +      Int32    numZ;
5553 +      Int32    state_out_pos;
5554 +
5555 +      /* map of bytes used in block */
5556 +      Int32    nInUse;
5557 +      Bool     inUse[256];
5558 +      UChar    unseqToSeq[256];
5559 +
5560 +      /* the buffer for bit stream creation */
5561 +      UInt32   bsBuff;
5562 +      Int32    bsLive;
5563 +
5564 +      /* block and combined CRCs */
5565 +      UInt32   blockCRC;
5566 +      UInt32   combinedCRC;
5567 +
5568 +      /* misc administratium */
5569 +      Int32    verbosity;
5570 +      Int32    blockNo;
5571 +      Int32    blockSize100k;
5572 +
5573 +      /* stuff for coding the MTF values */
5574 +      Int32    nMTF;
5575 +      Int32    mtfFreq    [BZ_MAX_ALPHA_SIZE];
5576 +      UChar    selector   [BZ_MAX_SELECTORS];
5577 +      UChar    selectorMtf[BZ_MAX_SELECTORS];
5578 +
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];
5584 +
5585 +   }
5586 +   EState;
5587 +
5588 +
5589 +
5590 +/*-- externs for compression. --*/
5591 +
5592 +extern void 
5593 +BZ2_blockSort ( EState* );
5594 +
5595 +extern void 
5596 +BZ2_compressBlock ( EState*, Bool );
5597 +
5598 +extern void 
5599 +BZ2_bsInitWrite ( EState* );
5600 +
5601 +extern void 
5602 +BZ2_hbAssignCodes ( Int32*, UChar*, Int32, Int32, Int32 );
5603 +
5604 +extern void 
5605 +BZ2_hbMakeCodeLengths ( UChar*, Int32*, Int32, Int32 );
5606 +
5607 +
5608 +
5609 +/*-- states for decompression. --*/
5610 +
5611 +#define BZ_X_IDLE        1
5612 +#define BZ_X_OUTPUT      2
5613 +
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
5655 +
5656 +
5657 +
5658 +/*-- Constants for the fast MTF decoder. --*/
5659 +
5660 +#define MTFA_SIZE 4096
5661 +#define MTFL_SIZE 16
5662 +
5663 +
5664 +
5665 +/*-- Structure holding all the decompression-side stuff. --*/
5666 +
5667 +typedef
5668 +   struct {
5669 +      /* pointer back to the struct bz_stream */
5670 +      bz_stream* strm;
5671 +
5672 +      /* state indicator for this stream */
5673 +      Int32    state;
5674 +
5675 +      /* for doing the final run-length decoding */
5676 +      UChar    state_out_ch;
5677 +      Int32    state_out_len;
5678 +      Bool     blockRandomised;
5679 +      BZ_RAND_DECLS;
5680 +
5681 +      /* the buffer for bit stream reading */
5682 +      UInt32   bsBuff;
5683 +      Int32    bsLive;
5684 +
5685 +      /* misc administratium */
5686 +      Int32    blockSize100k;
5687 +      Bool     smallDecompress;
5688 +      Int32    currBlockNo;
5689 +      Int32    verbosity;
5690 +
5691 +      /* for undoing the Burrows-Wheeler transform */
5692 +      Int32    origPtr;
5693 +      UInt32   tPos;
5694 +      Int32    k0;
5695 +      Int32    unzftab[256];
5696 +      Int32    nblock_used;
5697 +      Int32    cftab[257];
5698 +      Int32    cftabCopy[257];
5699 +
5700 +      /* for undoing the Burrows-Wheeler transform (FAST) */
5701 +      UInt32   *tt;
5702 +
5703 +      /* for undoing the Burrows-Wheeler transform (SMALL) */
5704 +      UInt16   *ll16;
5705 +      UChar    *ll4;
5706 +
5707 +      /* stored and calculated CRCs */
5708 +      UInt32   storedBlockCRC;
5709 +      UInt32   storedCombinedCRC;
5710 +      UInt32   calculatedBlockCRC;
5711 +      UInt32   calculatedCombinedCRC;
5712 +
5713 +      /* map of bytes used in block */
5714 +      Int32    nInUse;
5715 +      Bool     inUse[256];
5716 +      Bool     inUse16[16];
5717 +      UChar    seqToUnseq[256];
5718 +
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];
5725 +
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];
5730 +
5731 +      /* save area for scalars in the main decompress code */
5732 +      Int32    save_i;
5733 +      Int32    save_j;
5734 +      Int32    save_t;
5735 +      Int32    save_alphaSize;
5736 +      Int32    save_nGroups;
5737 +      Int32    save_nSelectors;
5738 +      Int32    save_EOB;
5739 +      Int32    save_groupNo;
5740 +      Int32    save_groupPos;
5741 +      Int32    save_nextSym;
5742 +      Int32    save_nblockMAX;
5743 +      Int32    save_nblock;
5744 +      Int32    save_es;
5745 +      Int32    save_N;
5746 +      Int32    save_curr;
5747 +      Int32    save_zt;
5748 +      Int32    save_zn; 
5749 +      Int32    save_zvec;
5750 +      Int32    save_zj;
5751 +      Int32    save_gSel;
5752 +      Int32    save_gMinlen;
5753 +      Int32*   save_gLimit;
5754 +      Int32*   save_gBase;
5755 +      Int32*   save_gPerm;
5756 +
5757 +   }
5758 +   DState;
5759 +
5760 +
5761 +
5762 +/*-- Macros for decompression. --*/
5763 +
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);           \
5769 +    s->tPos >>= 8;
5770 +
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);            \
5776 +    c_tPos >>= 8;
5777 +
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);  \
5782 +   }
5783 +
5784 +#define GET_LL4(i)                             \
5785 +   ((((UInt32)(s->ll4[(i) >> 1])) >> (((i) << 2) & 0x4)) & 0xF)
5786 +
5787 +#define SET_LL(i,n)                          \
5788 +   { s->ll16[i] = (UInt16)(n & 0x0000ffff);  \
5789 +     SET_LL4(i, n >> 16);                    \
5790 +   }
5791 +
5792 +#define GET_LL(i) \
5793 +   (((UInt32)s->ll16[i]) | (GET_LL4(i) << 16))
5794 +
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);
5800 +
5801 +
5802 +/*-- externs for decompression. --*/
5803 +
5804 +extern Int32 
5805 +BZ2_indexIntoF ( Int32, Int32* );
5806 +
5807 +extern Int32 
5808 +BZ2_decompress ( DState* );
5809 +
5810 +extern void 
5811 +BZ2_hbCreateDecodeTables ( Int32*, Int32*, Int32*, UChar*,
5812 +                           Int32,  Int32, Int32 );
5813 +
5814 +
5815 +#endif
5816 +
5817 +
5818 +/*-- BZ_NO_STDIO seems to make NULL disappear on some platforms. --*/
5819 +
5820 +#ifdef BZ_NO_STDIO
5821 +#ifndef NULL
5822 +#define NULL 0
5823 +#endif
5824 +#endif
5825 +
5826 +
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
5833 --- /dev/null
5834 +++ b/pit/upstream-bzip2/compress.c
5835 @@ -0,0 +1,672 @@
5836 +
5837 +/*-------------------------------------------------------------*/
5838 +/*--- Compression machinery (not incl block sorting)        ---*/
5839 +/*---                                            compress.c ---*/
5840 +/*-------------------------------------------------------------*/
5841 +
5842 +/* ------------------------------------------------------------------
5843 +   This file is part of bzip2/libbzip2, a program and library for
5844 +   lossless, block-sorting data compression.
5845 +
5846 +   bzip2/libbzip2 version 1.0.6 of 6 September 2010
5847 +   Copyright (C) 1996-2010 Julian Seward <jseward@bzip.org>
5848 +
5849 +   Please read the WARNING, DISCLAIMER and PATENTS sections in the 
5850 +   README file.
5851 +
5852 +   This program is released under the terms of the license contained
5853 +   in the file LICENSE.
5854 +   ------------------------------------------------------------------ */
5855 +
5856 +
5857 +/* CHANGES
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
5862 +*/
5863 +
5864 +#include "bzlib_private.h"
5865 +
5866 +
5867 +/*---------------------------------------------------*/
5868 +/*--- Bit stream I/O                              ---*/
5869 +/*---------------------------------------------------*/
5870 +
5871 +/*---------------------------------------------------*/
5872 +void BZ2_bsInitWrite ( EState* s )
5873 +{
5874 +   s->bsLive = 0;
5875 +   s->bsBuff = 0;
5876 +}
5877 +
5878 +
5879 +/*---------------------------------------------------*/
5880 +static
5881 +void bsFinishWrite ( EState* s )
5882 +{
5883 +   while (s->bsLive > 0) {
5884 +      s->zbits[s->numZ] = (UChar)(s->bsBuff >> 24);
5885 +      s->numZ++;
5886 +      s->bsBuff <<= 8;
5887 +      s->bsLive -= 8;
5888 +   }
5889 +}
5890 +
5891 +
5892 +/*---------------------------------------------------*/
5893 +#define bsNEEDW(nz)                           \
5894 +{                                             \
5895 +   while (s->bsLive >= 8) {                   \
5896 +      s->zbits[s->numZ]                       \
5897 +         = (UChar)(s->bsBuff >> 24);          \
5898 +      s->numZ++;                              \
5899 +      s->bsBuff <<= 8;                        \
5900 +      s->bsLive -= 8;                         \
5901 +   }                                          \
5902 +}
5903 +
5904 +
5905 +/*---------------------------------------------------*/
5906 +static
5907 +__inline__
5908 +void bsW ( EState* s, Int32 n, UInt32 v )
5909 +{
5910 +   bsNEEDW ( n );
5911 +   s->bsBuff |= (v << (32 - s->bsLive - n));
5912 +   s->bsLive += n;
5913 +}
5914 +
5915 +
5916 +/*---------------------------------------------------*/
5917 +static
5918 +void bsPutUInt32 ( EState* s, UInt32 u )
5919 +{
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 );
5924 +}
5925 +
5926 +
5927 +/*---------------------------------------------------*/
5928 +static
5929 +void bsPutUChar ( EState* s, UChar c )
5930 +{
5931 +   bsW( s, 8, (UInt32)c );
5932 +}
5933 +
5934 +
5935 +/*---------------------------------------------------*/
5936 +/*--- The back end proper                         ---*/
5937 +/*---------------------------------------------------*/
5938 +
5939 +/*---------------------------------------------------*/
5940 +static
5941 +void makeMaps_e ( EState* s )
5942 +{
5943 +   Int32 i;
5944 +   s->nInUse = 0;
5945 +   for (i = 0; i < 256; i++)
5946 +      if (s->inUse[i]) {
5947 +         s->unseqToSeq[i] = s->nInUse;
5948 +         s->nInUse++;
5949 +      }
5950 +}
5951 +
5952 +
5953 +/*---------------------------------------------------*/
5954 +static
5955 +void generateMTFValues ( EState* s )
5956 +{
5957 +   UChar   yy[256];
5958 +   Int32   i, j;
5959 +   Int32   zPend;
5960 +   Int32   wr;
5961 +   Int32   EOB;
5962 +
5963 +   /* 
5964 +      After sorting (eg, here),
5965 +         s->arr1 [ 0 .. s->nblock-1 ] holds sorted order,
5966 +         and
5967 +         ((UChar*)s->arr2) [ 0 .. s->nblock-1 ] 
5968 +         holds the original block data.
5969 +
5970 +      The first thing to do is generate the MTF values,
5971 +      and put them in
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.
5976 +
5977 +      The final compressed bitstream is generated into the
5978 +      area starting at
5979 +         (UChar*) (&((UChar*)s->arr2)[s->nblock])
5980 +
5981 +      These storage aliases are set up in bzCompressInit(),
5982 +      except for the last one, which is arranged in 
5983 +      compressBlock().
5984 +   */
5985 +   UInt32* ptr   = s->ptr;
5986 +   UChar* block  = s->block;
5987 +   UInt16* mtfv  = s->mtfv;
5988 +
5989 +   makeMaps_e ( s );
5990 +   EOB = s->nInUse+1;
5991 +
5992 +   for (i = 0; i <= EOB; i++) s->mtfFreq[i] = 0;
5993 +
5994 +   wr = 0;
5995 +   zPend = 0;
5996 +   for (i = 0; i < s->nInUse; i++) yy[i] = (UChar) i;
5997 +
5998 +   for (i = 0; i < s->nblock; i++) {
5999 +      UChar ll_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)" );
6004 +
6005 +      if (yy[0] == ll_i) { 
6006 +         zPend++;
6007 +      } else {
6008 +
6009 +         if (zPend > 0) {
6010 +            zPend--;
6011 +            while (True) {
6012 +               if (zPend & 1) {
6013 +                  mtfv[wr] = BZ_RUNB; wr++; 
6014 +                  s->mtfFreq[BZ_RUNB]++; 
6015 +               } else {
6016 +                  mtfv[wr] = BZ_RUNA; wr++; 
6017 +                  s->mtfFreq[BZ_RUNA]++; 
6018 +               }
6019 +               if (zPend < 2) break;
6020 +               zPend = (zPend - 2) / 2;
6021 +            };
6022 +            zPend = 0;
6023 +         }
6024 +         {
6025 +            register UChar  rtmp;
6026 +            register UChar* ryy_j;
6027 +            register UChar  rll_i;
6028 +            rtmp  = yy[1];
6029 +            yy[1] = yy[0];
6030 +            ryy_j = &(yy[1]);
6031 +            rll_i = ll_i;
6032 +            while ( rll_i != rtmp ) {
6033 +               register UChar rtmp2;
6034 +               ryy_j++;
6035 +               rtmp2  = rtmp;
6036 +               rtmp   = *ryy_j;
6037 +               *ryy_j = rtmp2;
6038 +            };
6039 +            yy[0] = rtmp;
6040 +            j = ryy_j - &(yy[0]);
6041 +            mtfv[wr] = j+1; wr++; s->mtfFreq[j+1]++;
6042 +         }
6043 +
6044 +      }
6045 +   }
6046 +
6047 +   if (zPend > 0) {
6048 +      zPend--;
6049 +      while (True) {
6050 +         if (zPend & 1) {
6051 +            mtfv[wr] = BZ_RUNB; wr++; 
6052 +            s->mtfFreq[BZ_RUNB]++; 
6053 +         } else {
6054 +            mtfv[wr] = BZ_RUNA; wr++; 
6055 +            s->mtfFreq[BZ_RUNA]++; 
6056 +         }
6057 +         if (zPend < 2) break;
6058 +         zPend = (zPend - 2) / 2;
6059 +      };
6060 +      zPend = 0;
6061 +   }
6062 +
6063 +   mtfv[wr] = EOB; wr++; s->mtfFreq[EOB]++;
6064 +
6065 +   s->nMTF = wr;
6066 +}
6067 +
6068 +
6069 +/*---------------------------------------------------*/
6070 +#define BZ_LESSER_ICOST  0
6071 +#define BZ_GREATER_ICOST 15
6072 +
6073 +static
6074 +void sendMTFValues ( EState* s )
6075 +{
6076 +   Int32 v, t, i, j, gs, ge, totc, bt, bc, iter;
6077 +   Int32 nSelectors, alphaSize, minLen, maxLen, selCtr;
6078 +   Int32 nGroups, nBytes;
6079 +
6080 +   /*--
6081 +   UChar  len [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
6082 +   is a global since the decoder also needs it.
6083 +
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.
6088 +   --*/
6089 +
6090 +
6091 +   UInt16 cost[BZ_N_GROUPS];
6092 +   Int32  fave[BZ_N_GROUPS];
6093 +
6094 +   UInt16* mtfv = s->mtfv;
6095 +
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 );
6100 +
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;
6105 +
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
6112 +                       nGroups = 6;
6113 +
6114 +   /*--- Generate an initial set of coding tables ---*/
6115 +   { 
6116 +      Int32 nPart, remF, tFreq, aFreq;
6117 +
6118 +      nPart = nGroups;
6119 +      remF  = s->nMTF;
6120 +      gs = 0;
6121 +      while (nPart > 0) {
6122 +         tFreq = remF / nPart;
6123 +         ge = gs-1;
6124 +         aFreq = 0;
6125 +         while (aFreq < tFreq && ge < alphaSize-1) {
6126 +            ge++;
6127 +            aFreq += s->mtfFreq[ge];
6128 +         }
6129 +
6130 +         if (ge > gs 
6131 +             && nPart != nGroups && nPart != 1 
6132 +             && ((nGroups-nPart) % 2 == 1)) {
6133 +            aFreq -= s->mtfFreq[ge];
6134 +            ge--;
6135 +         }
6136 +
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) );
6142
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;
6147
6148 +         nPart--;
6149 +         gs = ge+1;
6150 +         remF -= aFreq;
6151 +      }
6152 +   }
6153 +
6154 +   /*--- 
6155 +      Iterate up to BZ_N_ITERS times to improve the tables.
6156 +   ---*/
6157 +   for (iter = 0; iter < BZ_N_ITERS; iter++) {
6158 +
6159 +      for (t = 0; t < nGroups; t++) fave[t] = 0;
6160 +
6161 +      for (t = 0; t < nGroups; t++)
6162 +         for (v = 0; v < alphaSize; v++)
6163 +            s->rfreq[t][v] = 0;
6164 +
6165 +      /*---
6166 +        Set up an auxiliary length table which is used to fast-track
6167 +       the common case (nGroups == 6). 
6168 +      ---*/
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];
6174 +        }
6175 +      }
6176 +
6177 +      nSelectors = 0;
6178 +      totc = 0;
6179 +      gs = 0;
6180 +      while (True) {
6181 +
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;
6186 +
6187 +         /*-- 
6188 +            Calculate the cost of this group as coded
6189 +            by each of the coding tables.
6190 +         --*/
6191 +         for (t = 0; t < nGroups; t++) cost[t] = 0;
6192 +
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;
6198 +
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]; \
6204 +
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);
6215 +
6216 +#           undef BZ_ITER
6217 +
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;
6221 +
6222 +         } else {
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];
6227 +            }
6228 +         }
6229
6230 +         /*-- 
6231 +            Find the coding table which is best for this group,
6232 +            and record its identity in the selector table.
6233 +         --*/
6234 +         bc = 999999999; bt = -1;
6235 +         for (t = 0; t < nGroups; t++)
6236 +            if (cost[t] < bc) { bc = cost[t]; bt = t; };
6237 +         totc += bc;
6238 +         fave[bt]++;
6239 +         s->selector[nSelectors] = bt;
6240 +         nSelectors++;
6241 +
6242 +         /*-- 
6243 +            Increment the symbol frequencies for the selected table.
6244 +          --*/
6245 +         if (nGroups == 6 && 50 == ge-gs+1) {
6246 +            /*--- fast track the common case ---*/
6247 +
6248 +#           define BZ_ITUR(nn) s->rfreq[bt][ mtfv[gs+(nn)] ]++
6249 +
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);
6260 +
6261 +#           undef BZ_ITUR
6262 +
6263 +         } else {
6264 +           /*--- slow version which correctly handles all situations ---*/
6265 +            for (i = gs; i <= ge; i++)
6266 +               s->rfreq[bt][ mtfv[i] ]++;
6267 +         }
6268 +
6269 +         gs = ge+1;
6270 +      }
6271 +      if (s->verbosity >= 3) {
6272 +         VPrintf2 ( "      pass %d: size is %d, grp uses are ", 
6273 +                   iter+1, totc/8 );
6274 +         for (t = 0; t < nGroups; t++)
6275 +            VPrintf1 ( "%d ", fave[t] );
6276 +         VPrintf0 ( "\n" );
6277 +      }
6278 +
6279 +      /*--
6280 +        Recompute the tables based on the accumulated frequencies.
6281 +      --*/
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*/ );
6287 +   }
6288 +
6289 +
6290 +   AssertH( nGroups < 8, 3002 );
6291 +   AssertH( nSelectors < 32768 &&
6292 +            nSelectors <= (2 + (900000 / BZ_G_SIZE)),
6293 +            3003 );
6294 +
6295 +
6296 +   /*--- Compute MTF values for the selectors. ---*/
6297 +   {
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];
6302 +         j = 0;
6303 +         tmp = pos[j];
6304 +         while ( ll_i != tmp ) {
6305 +            j++;
6306 +            tmp2 = tmp;
6307 +            tmp = pos[j];
6308 +            pos[j] = tmp2;
6309 +         };
6310 +         pos[0] = tmp;
6311 +         s->selectorMtf[i] = j;
6312 +      }
6313 +   };
6314 +
6315 +   /*--- Assign actual codes for the tables. --*/
6316 +   for (t = 0; t < nGroups; t++) {
6317 +      minLen = 32;
6318 +      maxLen = 0;
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];
6322 +      }
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 );
6327 +   }
6328 +
6329 +   /*--- Transmit the mapping table. ---*/
6330 +   { 
6331 +      Bool inUse16[16];
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;
6336 +      }
6337 +     
6338 +      nBytes = s->numZ;
6339 +      for (i = 0; i < 16; i++)
6340 +         if (inUse16[i]) bsW(s,1,1); else bsW(s,1,0);
6341 +
6342 +      for (i = 0; i < 16; i++)
6343 +         if (inUse16[i])
6344 +            for (j = 0; j < 16; j++) {
6345 +               if (s->inUse[i * 16 + j]) bsW(s,1,1); else bsW(s,1,0);
6346 +            }
6347 +
6348 +      if (s->verbosity >= 3) 
6349 +         VPrintf1( "      bytes: mapping %d, ", s->numZ-nBytes );
6350 +   }
6351 +
6352 +   /*--- Now the selectors. ---*/
6353 +   nBytes = s->numZ;
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);
6358 +      bsW(s,1,0);
6359 +   }
6360 +   if (s->verbosity >= 3)
6361 +      VPrintf1( "selectors %d, ", s->numZ-nBytes );
6362 +
6363 +   /*--- Now the coding tables. ---*/
6364 +   nBytes = s->numZ;
6365 +
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 */ };
6372 +         bsW ( s, 1, 0 );
6373 +      }
6374 +   }
6375 +
6376 +   if (s->verbosity >= 3)
6377 +      VPrintf1 ( "code lengths %d, ", s->numZ-nBytes );
6378 +
6379 +   /*--- And finally, the block data proper ---*/
6380 +   nBytes = s->numZ;
6381 +   selCtr = 0;
6382 +   gs = 0;
6383 +   while (True) {
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 );
6388 +
6389 +      if (nGroups == 6 && 50 == ge-gs+1) {
6390 +            /*--- fast track the common case ---*/
6391 +            UInt16 mtfv_i;
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]);
6396 +
6397 +#           define BZ_ITAH(nn)                      \
6398 +               mtfv_i = mtfv[gs+(nn)];              \
6399 +               bsW ( s,                             \
6400 +                     s_len_sel_selCtr[mtfv_i],      \
6401 +                     s_code_sel_selCtr[mtfv_i] )
6402 +
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);
6413 +
6414 +#           undef BZ_ITAH
6415 +
6416 +      } else {
6417 +        /*--- slow version which correctly handles all situations ---*/
6418 +         for (i = gs; i <= ge; i++) {
6419 +            bsW ( s, 
6420 +                  s->len  [s->selector[selCtr]] [mtfv[i]],
6421 +                  s->code [s->selector[selCtr]] [mtfv[i]] );
6422 +         }
6423 +      }
6424 +
6425 +
6426 +      gs = ge+1;
6427 +      selCtr++;
6428 +   }
6429 +   AssertH( selCtr == nSelectors, 3007 );
6430 +
6431 +   if (s->verbosity >= 3)
6432 +      VPrintf1( "codes %d\n", s->numZ-nBytes );
6433 +}
6434 +
6435 +
6436 +/*---------------------------------------------------*/
6437 +void BZ2_compressBlock ( EState* s, Bool is_last_block )
6438 +{
6439 +   if (s->nblock > 0) {
6440 +
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;
6445 +
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 );
6450 +
6451 +      BZ2_blockSort ( s );
6452 +   }
6453 +
6454 +   s->zbits = (UChar*) (&((UChar*)s->arr2)[s->nblock]);
6455 +
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) );
6463 +   }
6464 +
6465 +   if (s->nblock > 0) {
6466 +
6467 +      bsPutUChar ( s, 0x31 ); bsPutUChar ( s, 0x41 );
6468 +      bsPutUChar ( s, 0x59 ); bsPutUChar ( s, 0x26 );
6469 +      bsPutUChar ( s, 0x53 ); bsPutUChar ( s, 0x59 );
6470 +
6471 +      /*-- Now the block's CRC, so it is in a known place. --*/
6472 +      bsPutUInt32 ( s, s->blockCRC );
6473 +
6474 +      /*-- 
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.
6482 +      --*/
6483 +      bsW(s,1,0);
6484 +
6485 +      bsW ( s, 24, s->origPtr );
6486 +      generateMTFValues ( s );
6487 +      sendMTFValues ( s );
6488 +   }
6489 +
6490 +
6491 +   /*-- If this is the last block, add the stream trailer. --*/
6492 +   if (is_last_block) {
6493 +
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 );
6501 +   }
6502 +}
6503 +
6504 +
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
6511 --- /dev/null
6512 +++ b/pit/upstream-bzip2/crctable.c
6513 @@ -0,0 +1,104 @@
6514 +
6515 +/*-------------------------------------------------------------*/
6516 +/*--- Table for doing CRCs                                  ---*/
6517 +/*---                                            crctable.c ---*/
6518 +/*-------------------------------------------------------------*/
6519 +
6520 +/* ------------------------------------------------------------------
6521 +   This file is part of bzip2/libbzip2, a program and library for
6522 +   lossless, block-sorting data compression.
6523 +
6524 +   bzip2/libbzip2 version 1.0.6 of 6 September 2010
6525 +   Copyright (C) 1996-2010 Julian Seward <jseward@bzip.org>
6526 +
6527 +   Please read the WARNING, DISCLAIMER and PATENTS sections in the 
6528 +   README file.
6529 +
6530 +   This program is released under the terms of the license contained
6531 +   in the file LICENSE.
6532 +   ------------------------------------------------------------------ */
6533 +
6534 +
6535 +#include "bzlib_private.h"
6536 +
6537 +/*--
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.
6542 +--*/
6543 +
6544 +UInt32 BZ2_crc32Table[256] = {
6545 +
6546 +   /*-- Ugly, innit? --*/
6547 +
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
6612 +};
6613 +
6614 +
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
6621 --- /dev/null
6622 +++ b/pit/upstream-bzip2/decompress.c
6623 @@ -0,0 +1,646 @@
6624 +
6625 +/*-------------------------------------------------------------*/
6626 +/*--- Decompression machinery                               ---*/
6627 +/*---                                          decompress.c ---*/
6628 +/*-------------------------------------------------------------*/
6629 +
6630 +/* ------------------------------------------------------------------
6631 +   This file is part of bzip2/libbzip2, a program and library for
6632 +   lossless, block-sorting data compression.
6633 +
6634 +   bzip2/libbzip2 version 1.0.6 of 6 September 2010
6635 +   Copyright (C) 1996-2010 Julian Seward <jseward@bzip.org>
6636 +
6637 +   Please read the WARNING, DISCLAIMER and PATENTS sections in the 
6638 +   README file.
6639 +
6640 +   This program is released under the terms of the license contained
6641 +   in the file LICENSE.
6642 +   ------------------------------------------------------------------ */
6643 +
6644 +
6645 +#include "bzlib_private.h"
6646 +
6647 +
6648 +/*---------------------------------------------------*/
6649 +static
6650 +void makeMaps_d ( DState* s )
6651 +{
6652 +   Int32 i;
6653 +   s->nInUse = 0;
6654 +   for (i = 0; i < 256; i++)
6655 +      if (s->inUse[i]) {
6656 +         s->seqToUnseq[s->nInUse] = i;
6657 +         s->nInUse++;
6658 +      }
6659 +}
6660 +
6661 +
6662 +/*---------------------------------------------------*/
6663 +#define RETURN(rrr)                               \
6664 +   { retVal = rrr; goto save_state_and_return; };
6665 +
6666 +#define GET_BITS(lll,vvv,nnn)                     \
6667 +   case lll: s->state = lll;                      \
6668 +   while (True) {                                 \
6669 +      if (s->bsLive >= nnn) {                     \
6670 +         UInt32 v;                                \
6671 +         v = (s->bsBuff >>                        \
6672 +             (s->bsLive-nnn)) & ((1 << nnn)-1);   \
6673 +         s->bsLive -= nnn;                        \
6674 +         vvv = v;                                 \
6675 +         break;                                   \
6676 +      }                                           \
6677 +      if (s->strm->avail_in == 0) RETURN(BZ_OK);  \
6678 +      s->bsBuff                                   \
6679 +         = (s->bsBuff << 8) |                     \
6680 +           ((UInt32)                              \
6681 +              (*((UChar*)(s->strm->next_in))));   \
6682 +      s->bsLive += 8;                             \
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++;                \
6688 +   }
6689 +
6690 +#define GET_UCHAR(lll,uuu)                        \
6691 +   GET_BITS(lll,uuu,8)
6692 +
6693 +#define GET_BIT(lll,uuu)                          \
6694 +   GET_BITS(lll,uuu,1)
6695 +
6696 +/*---------------------------------------------------*/
6697 +#define GET_MTF_VAL(label1,label2,lval)           \
6698 +{                                                 \
6699 +   if (groupPos == 0) {                           \
6700 +      groupNo++;                                  \
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]);                \
6709 +   }                                              \
6710 +   groupPos--;                                    \
6711 +   zn = gMinlen;                                  \
6712 +   GET_BITS(label1, zvec, zn);                    \
6713 +   while (1) {                                    \
6714 +      if (zn > 20 /* the longest code */)         \
6715 +         RETURN(BZ_DATA_ERROR);                   \
6716 +      if (zvec <= gLimit[zn]) break;              \
6717 +      zn++;                                       \
6718 +      GET_BIT(label2, zj);                        \
6719 +      zvec = (zvec << 1) | zj;                    \
6720 +   };                                             \
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]];                \
6725 +}
6726 +
6727 +
6728 +/*---------------------------------------------------*/
6729 +Int32 BZ2_decompress ( DState* s )
6730 +{
6731 +   UChar      uc;
6732 +   Int32      retVal;
6733 +   Int32      minLen, maxLen;
6734 +   bz_stream* strm = s->strm;
6735 +
6736 +   /* stuff that needs to be saved/restored */
6737 +   Int32  i;
6738 +   Int32  j;
6739 +   Int32  t;
6740 +   Int32  alphaSize;
6741 +   Int32  nGroups;
6742 +   Int32  nSelectors;
6743 +   Int32  EOB;
6744 +   Int32  groupNo;
6745 +   Int32  groupPos;
6746 +   Int32  nextSym;
6747 +   Int32  nblockMAX;
6748 +   Int32  nblock;
6749 +   Int32  es;
6750 +   Int32  N;
6751 +   Int32  curr;
6752 +   Int32  zt;
6753 +   Int32  zn; 
6754 +   Int32  zvec;
6755 +   Int32  zj;
6756 +   Int32  gSel;
6757 +   Int32  gMinlen;
6758 +   Int32* gLimit;
6759 +   Int32* gBase;
6760 +   Int32* gPerm;
6761 +
6762 +   if (s->state == BZ_X_MAGIC_1) {
6763 +      /*initialise the save area*/
6764 +      s->save_i           = 0;
6765 +      s->save_j           = 0;
6766 +      s->save_t           = 0;
6767 +      s->save_alphaSize   = 0;
6768 +      s->save_nGroups     = 0;
6769 +      s->save_nSelectors  = 0;
6770 +      s->save_EOB         = 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;
6776 +      s->save_es          = 0;
6777 +      s->save_N           = 0;
6778 +      s->save_curr        = 0;
6779 +      s->save_zt          = 0;
6780 +      s->save_zn          = 0;
6781 +      s->save_zvec        = 0;
6782 +      s->save_zj          = 0;
6783 +      s->save_gSel        = 0;
6784 +      s->save_gMinlen     = 0;
6785 +      s->save_gLimit      = NULL;
6786 +      s->save_gBase       = NULL;
6787 +      s->save_gPerm       = NULL;
6788 +   }
6789 +
6790 +   /*restore from the save area*/
6791 +   i           = s->save_i;
6792 +   j           = s->save_j;
6793 +   t           = s->save_t;
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;
6803 +   es          = s->save_es;
6804 +   N           = s->save_N;
6805 +   curr        = s->save_curr;
6806 +   zt          = s->save_zt;
6807 +   zn          = s->save_zn; 
6808 +   zvec        = s->save_zvec;
6809 +   zj          = s->save_zj;
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;
6815 +
6816 +   retVal = BZ_OK;
6817 +
6818 +   switch (s->state) {
6819 +
6820 +      GET_UCHAR(BZ_X_MAGIC_1, uc);
6821 +      if (uc != BZ_HDR_B) RETURN(BZ_DATA_ERROR_MAGIC);
6822 +
6823 +      GET_UCHAR(BZ_X_MAGIC_2, uc);
6824 +      if (uc != BZ_HDR_Z) RETURN(BZ_DATA_ERROR_MAGIC);
6825 +
6826 +      GET_UCHAR(BZ_X_MAGIC_3, uc)
6827 +      if (uc != BZ_HDR_h) RETURN(BZ_DATA_ERROR_MAGIC);
6828 +
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;
6833 +
6834 +      if (s->smallDecompress) {
6835 +         s->ll16 = BZALLOC( s->blockSize100k * 100000 * sizeof(UInt16) );
6836 +         s->ll4  = BZALLOC( 
6837 +                      ((1 + s->blockSize100k * 100000) >> 1) * sizeof(UChar) 
6838 +                   );
6839 +         if (s->ll16 == NULL || s->ll4 == NULL) RETURN(BZ_MEM_ERROR);
6840 +      } else {
6841 +         s->tt  = BZALLOC( s->blockSize100k * 100000 * sizeof(Int32) );
6842 +         if (s->tt == NULL) RETURN(BZ_MEM_ERROR);
6843 +      }
6844 +
6845 +      GET_UCHAR(BZ_X_BLKHDR_1, uc);
6846 +
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);
6859 +
6860 +      s->currBlockNo++;
6861 +      if (s->verbosity >= 2)
6862 +         VPrintf1 ( "\n    [%d: huff+mtf ", s->currBlockNo );
6863
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);
6873 +
6874 +      GET_BITS(BZ_X_RANDBIT, s->blockRandomised, 1);
6875 +
6876 +      s->origPtr = 0;
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);
6883 +
6884 +      if (s->origPtr < 0)
6885 +         RETURN(BZ_DATA_ERROR);
6886 +      if (s->origPtr > 10 + 100000*s->blockSize100k) 
6887 +         RETURN(BZ_DATA_ERROR);
6888 +
6889 +      /*--- Receive the mapping table ---*/
6890 +      for (i = 0; i < 16; i++) {
6891 +         GET_BIT(BZ_X_MAPPING_1, uc);
6892 +         if (uc == 1) 
6893 +            s->inUse16[i] = True; else 
6894 +            s->inUse16[i] = False;
6895 +      }
6896 +
6897 +      for (i = 0; i < 256; i++) s->inUse[i] = False;
6898 +
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;
6904 +            }
6905 +      makeMaps_d ( s );
6906 +      if (s->nInUse == 0) RETURN(BZ_DATA_ERROR);
6907 +      alphaSize = s->nInUse+2;
6908 +
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++) {
6915 +         j = 0;
6916 +         while (True) {
6917 +            GET_BIT(BZ_X_SELECTOR_3, uc);
6918 +            if (uc == 0) break;
6919 +            j++;
6920 +            if (j >= nGroups) RETURN(BZ_DATA_ERROR);
6921 +         }
6922 +         s->selectorMtf[i] = j;
6923 +      }
6924 +
6925 +      /*--- Undo the MTF values for the selectors. ---*/
6926 +      {
6927 +         UChar pos[BZ_N_GROUPS], tmp, v;
6928 +         for (v = 0; v < nGroups; v++) pos[v] = v;
6929 +   
6930 +         for (i = 0; i < nSelectors; i++) {
6931 +            v = s->selectorMtf[i];
6932 +            tmp = pos[v];
6933 +            while (v > 0) { pos[v] = pos[v-1]; v--; }
6934 +            pos[0] = tmp;
6935 +            s->selector[i] = tmp;
6936 +         }
6937 +      }
6938 +
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++) {
6943 +            while (True) {
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--;
6949 +            }
6950 +            s->len[t][i] = curr;
6951 +         }
6952 +      }
6953 +
6954 +      /*--- Create the Huffman decoding tables ---*/
6955 +      for (t = 0; t < nGroups; t++) {
6956 +         minLen = 32;
6957 +         maxLen = 0;
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];
6961 +         }
6962 +         BZ2_hbCreateDecodeTables ( 
6963 +            &(s->limit[t][0]), 
6964 +            &(s->base[t][0]), 
6965 +            &(s->perm[t][0]), 
6966 +            &(s->len[t][0]),
6967 +            minLen, maxLen, alphaSize
6968 +         );
6969 +         s->minLens[t] = minLen;
6970 +      }
6971 +
6972 +      /*--- Now the MTF values ---*/
6973 +
6974 +      EOB      = s->nInUse+1;
6975 +      nblockMAX = 100000 * s->blockSize100k;
6976 +      groupNo  = -1;
6977 +      groupPos = 0;
6978 +
6979 +      for (i = 0; i <= 255; i++) s->unzftab[i] = 0;
6980 +
6981 +      /*-- MTF init --*/
6982 +      {
6983 +         Int32 ii, jj, kk;
6984 +         kk = MTFA_SIZE-1;
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);
6988 +               kk--;
6989 +            }
6990 +            s->mtfbase[ii] = kk + 1;
6991 +         }
6992 +      }
6993 +      /*-- end MTF init --*/
6994 +
6995 +      nblock = 0;
6996 +      GET_MTF_VAL(BZ_X_MTF_1, BZ_X_MTF_2, nextSym);
6997 +
6998 +      while (True) {
6999 +
7000 +         if (nextSym == EOB) break;
7001 +
7002 +         if (nextSym == BZ_RUNA || nextSym == BZ_RUNB) {
7003 +
7004 +            es = -1;
7005 +            N = 1;
7006 +            do {
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;
7016 +               N = N * 2;
7017 +               GET_MTF_VAL(BZ_X_MTF_3, BZ_X_MTF_4, nextSym);
7018 +            }
7019 +               while (nextSym == BZ_RUNA || nextSym == BZ_RUNB);
7020 +
7021 +            es++;
7022 +            uc = s->seqToUnseq[ s->mtfa[s->mtfbase[0]] ];
7023 +            s->unzftab[uc] += es;
7024 +
7025 +            if (s->smallDecompress)
7026 +               while (es > 0) {
7027 +                  if (nblock >= nblockMAX) RETURN(BZ_DATA_ERROR);
7028 +                  s->ll16[nblock] = (UInt16)uc;
7029 +                  nblock++;
7030 +                  es--;
7031 +               }
7032 +            else
7033 +               while (es > 0) {
7034 +                  if (nblock >= nblockMAX) RETURN(BZ_DATA_ERROR);
7035 +                  s->tt[nblock] = (UInt32)uc;
7036 +                  nblock++;
7037 +                  es--;
7038 +               };
7039 +
7040 +            continue;
7041 +
7042 +         } else {
7043 +
7044 +            if (nblock >= nblockMAX) RETURN(BZ_DATA_ERROR);
7045 +
7046 +            /*-- uc = MTF ( nextSym-1 ) --*/
7047 +            {
7048 +               Int32 ii, jj, kk, pp, lno, off;
7049 +               UInt32 nn;
7050 +               nn = (UInt32)(nextSym - 1);
7051 +
7052 +               if (nn < MTFL_SIZE) {
7053 +                  /* avoid general-case expense */
7054 +                  pp = s->mtfbase[0];
7055 +                  uc = s->mtfa[pp+nn];
7056 +                  while (nn > 3) {
7057 +                     Int32 z = 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];
7062 +                     nn -= 4;
7063 +                  }
7064 +                  while (nn > 0) { 
7065 +                     s->mtfa[(pp+nn)] = s->mtfa[(pp+nn)-1]; nn--; 
7066 +                  };
7067 +                  s->mtfa[pp] = uc;
7068 +               } else { 
7069 +                  /* general case */
7070 +                  lno = nn / MTFL_SIZE;
7071 +                  off = nn % MTFL_SIZE;
7072 +                  pp = s->mtfbase[lno] + off;
7073 +                  uc = s->mtfa[pp];
7074 +                  while (pp > s->mtfbase[lno]) { 
7075 +                     s->mtfa[pp] = s->mtfa[pp-1]; pp--; 
7076 +                  };
7077 +                  s->mtfbase[lno]++;
7078 +                  while (lno > 0) {
7079 +                     s->mtfbase[lno]--;
7080 +                     s->mtfa[s->mtfbase[lno]] 
7081 +                        = s->mtfa[s->mtfbase[lno-1] + MTFL_SIZE - 1];
7082 +                     lno--;
7083 +                  }
7084 +                  s->mtfbase[0]--;
7085 +                  s->mtfa[s->mtfbase[0]] = uc;
7086 +                  if (s->mtfbase[0] == 0) {
7087 +                     kk = MTFA_SIZE-1;
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];
7091 +                           kk--;
7092 +                        }
7093 +                        s->mtfbase[ii] = kk + 1;
7094 +                     }
7095 +                  }
7096 +               }
7097 +            }
7098 +            /*-- end uc = MTF ( nextSym-1 ) --*/
7099 +
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]);
7104 +            nblock++;
7105 +
7106 +            GET_MTF_VAL(BZ_X_MTF_5, BZ_X_MTF_6, nextSym);
7107 +            continue;
7108 +         }
7109 +      }
7110 +
7111 +      /* Now we know what nblock is, we can do a better sanity
7112 +         check on s->origPtr.
7113 +      */
7114 +      if (s->origPtr < 0 || s->origPtr >= nblock)
7115 +         RETURN(BZ_DATA_ERROR);
7116 +
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);
7122 +      }
7123 +      /* Actually generate cftab. */
7124 +      s->cftab[0] = 0;
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);
7132 +         }
7133 +      }
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);
7138 +         }
7139 +      }
7140 +
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" );
7146 +
7147 +      if (s->smallDecompress) {
7148 +
7149 +         /*-- Make a copy of cftab, used in generation of T --*/
7150 +         for (i = 0; i <= 256; i++) s->cftabCopy[i] = s->cftab[i];
7151 +
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]++;
7157 +         }
7158 +
7159 +         /*-- Compute T^(-1) by pointer reversal on T --*/
7160 +         i = s->origPtr;
7161 +         j = GET_LL(i);
7162 +         do {
7163 +            Int32 tmp = GET_LL(j);
7164 +            SET_LL(j, i);
7165 +            i = j;
7166 +            j = tmp;
7167 +         }
7168 +            while (i != s->origPtr);
7169 +
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; 
7176 +         } else {
7177 +            BZ_GET_SMALL(s->k0); s->nblock_used++;
7178 +         }
7179 +
7180 +      } else {
7181 +
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);
7186 +            s->cftab[uc]++;
7187 +         }
7188 +
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; 
7195 +         } else {
7196 +            BZ_GET_FAST(s->k0); s->nblock_used++;
7197 +         }
7198 +
7199 +      }
7200 +
7201 +      RETURN(BZ_OK);
7202 +
7203 +
7204 +
7205 +    endhdr_2:
7206 +
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);
7217 +
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);
7227 +
7228 +      s->state = BZ_X_IDLE;
7229 +      RETURN(BZ_STREAM_END);
7230 +
7231 +      default: AssertH ( False, 4001 );
7232 +   }
7233 +
7234 +   AssertH ( False, 4002 );
7235 +
7236 +   save_state_and_return:
7237 +
7238 +   s->save_i           = i;
7239 +   s->save_j           = j;
7240 +   s->save_t           = t;
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;
7250 +   s->save_es          = es;
7251 +   s->save_N           = N;
7252 +   s->save_curr        = curr;
7253 +   s->save_zt          = zt;
7254 +   s->save_zn          = zn;
7255 +   s->save_zvec        = zvec;
7256 +   s->save_zj          = zj;
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;
7262 +
7263 +   return retVal;   
7264 +}
7265 +
7266 +
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
7273 --- /dev/null
7274 +++ b/pit/upstream-bzip2/huffman.c
7275 @@ -0,0 +1,205 @@
7276 +
7277 +/*-------------------------------------------------------------*/
7278 +/*--- Huffman coding low-level stuff                        ---*/
7279 +/*---                                             huffman.c ---*/
7280 +/*-------------------------------------------------------------*/
7281 +
7282 +/* ------------------------------------------------------------------
7283 +   This file is part of bzip2/libbzip2, a program and library for
7284 +   lossless, block-sorting data compression.
7285 +
7286 +   bzip2/libbzip2 version 1.0.6 of 6 September 2010
7287 +   Copyright (C) 1996-2010 Julian Seward <jseward@bzip.org>
7288 +
7289 +   Please read the WARNING, DISCLAIMER and PATENTS sections in the 
7290 +   README file.
7291 +
7292 +   This program is released under the terms of the license contained
7293 +   in the file LICENSE.
7294 +   ------------------------------------------------------------------ */
7295 +
7296 +
7297 +#include "bzlib_private.h"
7298 +
7299 +/*---------------------------------------------------*/
7300 +#define WEIGHTOF(zz0)  ((zz0) & 0xffffff00)
7301 +#define DEPTHOF(zz1)   ((zz1) & 0x000000ff)
7302 +#define MYMAX(zz2,zz3) ((zz2) > (zz3) ? (zz2) : (zz3))
7303 +
7304 +#define ADDWEIGHTS(zw1,zw2)                           \
7305 +   (WEIGHTOF(zw1)+WEIGHTOF(zw2)) |                    \
7306 +   (1 + MYMAX(DEPTHOF(zw1),DEPTHOF(zw2)))
7307 +
7308 +#define UPHEAP(z)                                     \
7309 +{                                                     \
7310 +   Int32 zz, tmp;                                     \
7311 +   zz = z; tmp = heap[zz];                            \
7312 +   while (weight[tmp] < weight[heap[zz >> 1]]) {      \
7313 +      heap[zz] = heap[zz >> 1];                       \
7314 +      zz >>= 1;                                       \
7315 +   }                                                  \
7316 +   heap[zz] = tmp;                                    \
7317 +}
7318 +
7319 +#define DOWNHEAP(z)                                   \
7320 +{                                                     \
7321 +   Int32 zz, yy, tmp;                                 \
7322 +   zz = z; tmp = heap[zz];                            \
7323 +   while (True) {                                     \
7324 +      yy = zz << 1;                                   \
7325 +      if (yy > nHeap) break;                          \
7326 +      if (yy < nHeap &&                               \
7327 +          weight[heap[yy+1]] < weight[heap[yy]])      \
7328 +         yy++;                                        \
7329 +      if (weight[tmp] < weight[heap[yy]]) break;      \
7330 +      heap[zz] = heap[yy];                            \
7331 +      zz = yy;                                        \
7332 +   }                                                  \
7333 +   heap[zz] = tmp;                                    \
7334 +}
7335 +
7336 +
7337 +/*---------------------------------------------------*/
7338 +void BZ2_hbMakeCodeLengths ( UChar *len, 
7339 +                             Int32 *freq,
7340 +                             Int32 alphaSize,
7341 +                             Int32 maxLen )
7342 +{
7343 +   /*--
7344 +      Nodes and heap entries run from 1.  Entry 0
7345 +      for both the heap and nodes is a sentinel.
7346 +   --*/
7347 +   Int32 nNodes, nHeap, n1, n2, i, j, k;
7348 +   Bool  tooLong;
7349 +
7350 +   Int32 heap   [ BZ_MAX_ALPHA_SIZE + 2 ];
7351 +   Int32 weight [ BZ_MAX_ALPHA_SIZE * 2 ];
7352 +   Int32 parent [ BZ_MAX_ALPHA_SIZE * 2 ]; 
7353 +
7354 +   for (i = 0; i < alphaSize; i++)
7355 +      weight[i+1] = (freq[i] == 0 ? 1 : freq[i]) << 8;
7356 +
7357 +   while (True) {
7358 +
7359 +      nNodes = alphaSize;
7360 +      nHeap = 0;
7361 +
7362 +      heap[0] = 0;
7363 +      weight[0] = 0;
7364 +      parent[0] = -2;
7365 +
7366 +      for (i = 1; i <= alphaSize; i++) {
7367 +         parent[i] = -1;
7368 +         nHeap++;
7369 +         heap[nHeap] = i;
7370 +         UPHEAP(nHeap);
7371 +      }
7372 +
7373 +      AssertH( nHeap < (BZ_MAX_ALPHA_SIZE+2), 2001 );
7374 +   
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);
7378 +         nNodes++;
7379 +         parent[n1] = parent[n2] = nNodes;
7380 +         weight[nNodes] = ADDWEIGHTS(weight[n1], weight[n2]);
7381 +         parent[nNodes] = -1;
7382 +         nHeap++;
7383 +         heap[nHeap] = nNodes;
7384 +         UPHEAP(nHeap);
7385 +      }
7386 +
7387 +      AssertH( nNodes < (BZ_MAX_ALPHA_SIZE * 2), 2002 );
7388 +
7389 +      tooLong = False;
7390 +      for (i = 1; i <= alphaSize; i++) {
7391 +         j = 0;
7392 +         k = i;
7393 +         while (parent[k] >= 0) { k = parent[k]; j++; }
7394 +         len[i-1] = j;
7395 +         if (j > maxLen) tooLong = True;
7396 +      }
7397 +      
7398 +      if (! tooLong) break;
7399 +
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.
7410 +
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. */
7416 +
7417 +      for (i = 1; i <= alphaSize; i++) {
7418 +         j = weight[i] >> 8;
7419 +         j = 1 + (j / 2);
7420 +         weight[i] = j << 8;
7421 +      }
7422 +   }
7423 +}
7424 +
7425 +
7426 +/*---------------------------------------------------*/
7427 +void BZ2_hbAssignCodes ( Int32 *code,
7428 +                         UChar *length,
7429 +                         Int32 minLen,
7430 +                         Int32 maxLen,
7431 +                         Int32 alphaSize )
7432 +{
7433 +   Int32 n, vec, i;
7434 +
7435 +   vec = 0;
7436 +   for (n = minLen; n <= maxLen; n++) {
7437 +      for (i = 0; i < alphaSize; i++)
7438 +         if (length[i] == n) { code[i] = vec; vec++; };
7439 +      vec <<= 1;
7440 +   }
7441 +}
7442 +
7443 +
7444 +/*---------------------------------------------------*/
7445 +void BZ2_hbCreateDecodeTables ( Int32 *limit,
7446 +                                Int32 *base,
7447 +                                Int32 *perm,
7448 +                                UChar *length,
7449 +                                Int32 minLen,
7450 +                                Int32 maxLen,
7451 +                                Int32 alphaSize )
7452 +{
7453 +   Int32 pp, i, j, vec;
7454 +
7455 +   pp = 0;
7456 +   for (i = minLen; i <= maxLen; i++)
7457 +      for (j = 0; j < alphaSize; j++)
7458 +         if (length[j] == i) { perm[pp] = j; pp++; };
7459 +
7460 +   for (i = 0; i < BZ_MAX_CODE_LEN; i++) base[i] = 0;
7461 +   for (i = 0; i < alphaSize; i++) base[length[i]+1]++;
7462 +
7463 +   for (i = 1; i < BZ_MAX_CODE_LEN; i++) base[i] += base[i-1];
7464 +
7465 +   for (i = 0; i < BZ_MAX_CODE_LEN; i++) limit[i] = 0;
7466 +   vec = 0;
7467 +
7468 +   for (i = minLen; i <= maxLen; i++) {
7469 +      vec += (base[i+1] - base[i]);
7470 +      limit[i] = vec-1;
7471 +      vec <<= 1;
7472 +   }
7473 +   for (i = minLen + 1; i <= maxLen; i++)
7474 +      base[i] = ((limit[i-1] + 1) << 1) - base[i];
7475 +}
7476 +
7477 +
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
7484 --- /dev/null
7485 +++ b/pit/upstream-bzip2/randtable.c
7486 @@ -0,0 +1,84 @@
7487 +
7488 +/*-------------------------------------------------------------*/
7489 +/*--- Table for randomising repetitive blocks               ---*/
7490 +/*---                                           randtable.c ---*/
7491 +/*-------------------------------------------------------------*/
7492 +
7493 +/* ------------------------------------------------------------------
7494 +   This file is part of bzip2/libbzip2, a program and library for
7495 +   lossless, block-sorting data compression.
7496 +
7497 +   bzip2/libbzip2 version 1.0.6 of 6 September 2010
7498 +   Copyright (C) 1996-2010 Julian Seward <jseward@bzip.org>
7499 +
7500 +   Please read the WARNING, DISCLAIMER and PATENTS sections in the 
7501 +   README file.
7502 +
7503 +   This program is released under the terms of the license contained
7504 +   in the file LICENSE.
7505 +   ------------------------------------------------------------------ */
7506 +
7507 +
7508 +#include "bzlib_private.h"
7509 +
7510 +
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, 
7564 +   936, 638
7565 +};
7566 +
7567 +
7568 +/*-------------------------------------------------------------*/
7569 +/*--- end                                       randtable.c ---*/
7570 +/*-------------------------------------------------------------*/