Bug Summary

File:obj-scan-build/libstore/../../libstore/do-bunzip2.c
Location:line 1476, column 13
Description:Value stored to 'retVal' is never read

Annotated Source Code

1/* bunzip2 engine, modified by okuji@kuicr.kyoto-u.ac.jp. */
2
3/* Stolen from util.c. */
4#include <stdio.h>
5#include <sys/types.h>
6
7/* I/O interface */
8extern int (*unzip_read) (char *buf, size_t maxread);
9extern void (*unzip_write) (const char *buf, size_t nwrite);
10extern void (*unzip_read_error) (void);
11extern void (*unzip_error) (const char *msg);
12
13/* bzip2 doesn't require window sliding. Just for buffering. */
14#define INBUFSIZ0x1000 0x1000
15#define OUTBUFSIZ0x1000 0x1000
16
17static unsigned char inbuf[INBUFSIZ0x1000];
18static unsigned char outbuf[OUTBUFSIZ0x1000];
19static unsigned inptr;
20static unsigned insize;
21static unsigned outcnt;
22
23/* ===========================================================================
24 * Fill the input buffer. This is called only when the buffer is empty.
25 */
26static int
27fill_inbuf (int eof_ok)
28{
29 int len;
30
31 /* Read as much as possible */
32 insize = 0;
33 do
34 {
35 len = (*unzip_read)((char*)inbuf+insize, INBUFSIZ0x1000-insize);
36 if (len == 0 || len == EOF(-1))
37 break;
38 insize += len;
39 }
40 while (insize < INBUFSIZ0x1000);
41
42 if (insize == 0)
43 {
44 if (eof_ok)
45 return EOF(-1);
46 unzip_read_error();
47 }
48
49 inptr = 1;
50 return inbuf[0];
51}
52
53static void
54flush_outbuf (void)
55{
56 if (outcnt == 0)
57 return;
58
59 (*unzip_write) ((char *) outbuf, outcnt);
60 outcnt = 0;
61}
62
63static inline int
64bz2_getc (void *stream)
65{
66 return inptr < insize ? inbuf[inptr++] : fill_inbuf (1);
67}
68
69static inline int
70bz2_putc (int c, void *stream)
71{
72 if (outcnt == OUTBUFSIZ0x1000)
73 flush_outbuf ();
74 outbuf[outcnt++] = c;
75 return c;
76}
77
78static inline int
79bz2_ferror (void *stream)
80{
81 return 0;
82}
83
84static inline int
85bz2_fflush (void *stream)
86{
87 flush_outbuf ();
88 return 0;
89}
90
91static inline int
92bz2_fclose (void *stream)
93{
94 flush_outbuf ();
95 return 0;
96}
97
98#define fprintf(s, f...) /**/
99
100
101/*-----------------------------------------------------------*/
102/*--- A block-sorting, lossless compressor bzip2.c ---*/
103/*-----------------------------------------------------------*/
104
105/*--
106 This program is bzip2, a lossless, block-sorting data compressor,
107 version 0.1pl2, dated 29-Aug-1997.
108
109 Copyright (C) 1996, 1997 by Julian Seward.
110 Guildford, Surrey, UK
111 email: jseward@acm.org
112
113 This program is free software; you can redistribute it and/or modify
114 it under the terms of the GNU General Public License as published by
115 the Free Software Foundation; either version 2 of the License, or
116 (at your option) any later version.
117
118 This program is distributed in the hope that it will be useful,
119 but WITHOUT ANY WARRANTY; without even the implied warranty of
120 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
121 GNU General Public License for more details.
122
123 You should have received a copy of the GNU General Public License
124 along with this program; if not, write to the Free Software
125 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
126
127 The GNU General Public License is contained in the file LICENSE.
128
129 This program is based on (at least) the work of:
130 Mike Burrows
131 David Wheeler
132 Peter Fenwick
133 Alistair Moffat
134 Radford Neal
135 Ian H. Witten
136 Robert Sedgewick
137 Jon L. Bentley
138
139 For more information on these sources, see the file ALGORITHMS.
140--*/
141
142/*----------------------------------------------------*/
143/*--- IMPORTANT ---*/
144/*----------------------------------------------------*/
145
146/*--
147 WARNING:
148 This program (attempts to) compress data by performing several
149 non-trivial transformations on it. Unless you are 100% familiar
150 with *all* the algorithms contained herein, and with the
151 consequences of modifying them, you should NOT meddle with the
152 compression or decompression machinery. Incorrect changes can
153 and very likely *will* lead to disastrous loss of data.
154
155 DISCLAIMER:
156 I TAKE NO RESPONSIBILITY FOR ANY LOSS OF DATA ARISING FROM THE
157 USE OF THIS PROGRAM, HOWSOEVER CAUSED.
158
159 Every compression of a file implies an assumption that the
160 compressed file can be decompressed to reproduce the original.
161 Great efforts in design, coding and testing have been made to
162 ensure that this program works correctly. However, the
163 complexity of the algorithms, and, in particular, the presence
164 of various special cases in the code which occur with very low
165 but non-zero probability make it impossible to rule out the
166 possibility of bugs remaining in the program. DO NOT COMPRESS
167 ANY DATA WITH THIS PROGRAM UNLESS YOU ARE PREPARED TO ACCEPT THE
168 POSSIBILITY, HOWEVER SMALL, THAT THE DATA WILL NOT BE RECOVERABLE.
169
170 That is not to say this program is inherently unreliable.
171 Indeed, I very much hope the opposite is true. bzip2 has been
172 carefully constructed and extensively tested.
173
174 PATENTS:
175 To the best of my knowledge, bzip2 does not use any patented
176 algorithms. However, I do not have the resources available to
177 carry out a full patent search. Therefore I cannot give any
178 guarantee of the above statement.
179--*/
180
181
182
183/*----------------------------------------------------*/
184/*--- and now for something much more pleasant :-) ---*/
185/*----------------------------------------------------*/
186
187/*---------------------------------------------*/
188/*--
189 Place a 1 beside your platform, and 0 elsewhere.
190--*/
191
192/*--
193 Generic 32-bit Unix.
194 Also works on 64-bit Unix boxes.
195--*/
196#define BZ_UNIX1 1
197
198/*--
199 Win32, as seen by Jacob Navia's excellent
200 port of (Chris Fraser & David Hanson)'s excellent
201 lcc compiler.
202--*/
203#define BZ_LCCWIN320 0
204
205
206
207/*---------------------------------------------*/
208/*--
209 Some stuff for all platforms.
210--*/
211
212#include <stdio.h>
213#include <stdlib.h>
214#if DEBUG0
215 #include <assert.h>
216#endif
217#include <string.h>
218#include <signal.h>
219#include <math.h>
220
221#define ERROR_IF_EOF(i){ if ((i) == (-1)) ioError(); } { if ((i) == EOF(-1)) ioError(); }
222#define ERROR_IF_NOT_ZERO(i){ if ((i) != 0) ioError(); } { if ((i) != 0) ioError(); }
223#define ERROR_IF_MINUS_ONE(i){ if ((i) == (-1)) ioError(); } { if ((i) == (-1)) ioError(); }
224
225
226/*---------------------------------------------*/
227/*--
228 Platform-specific stuff.
229--*/
230
231#if BZ_UNIX1
232 #include <sys/types.h>
233 #include <utime.h>
234 #include <unistd.h>
235 #include <malloc.h>
236 #include <sys/stat.h>
237 #include <sys/times.h>
238
239 #define Int32int int
240 #define UInt32unsigned int unsigned int
241 #define Charchar char
242 #define UCharunsigned char unsigned char
243 #define Int16short short
244 #define UInt16unsigned short unsigned short
245
246 #define PATH_SEP'/' '/'
247 #define MY_LSTATlstat lstat
248 #define MY_S_IFREGS_ISREG S_ISREG
249 #define MY_STATstat stat
250
251 #define APPEND_FILESPEC(root, name)root=snocString((root), (name)) \
252 root=snocString((root), (name))
253
254 #define SET_BINARY_MODE(fd) /**/
255
256 /*--
257 You should try very hard to persuade your C compiler
258 to inline the bits marked INLINE. Otherwise bzip2 will
259 run rather slowly. gcc version 2.x is recommended.
260 --*/
261 #ifdef __GNUC__4
262 #define INLINEinline inline
263 #define NORETURN__attribute__ ((noreturn)) __attribute__ ((noreturn))
264 #else
265 #define INLINEinline /**/
266 #define NORETURN__attribute__ ((noreturn)) /**/
267 #endif
268#endif
269
270
271
272#if BZ_LCCWIN320
273 #include <io.h>
274 #include <fcntl.h>
275 #include <sys\stat.h>
276
277 #define Int32int int
278 #define UInt32unsigned int unsigned int
279 #define Int16short short
280 #define UInt16unsigned short unsigned short
281 #define Charchar char
282 #define UCharunsigned char unsigned char
283
284 #define INLINEinline /**/
285 #define NORETURN__attribute__ ((noreturn)) /**/
286 #define PATH_SEP'/' '\\'
287 #define MY_LSTATlstat _stat
288 #define MY_STATstat _stat
289 #define MY_S_IFREG(x)((((x)) & 0170000) == (0100000)) ((x) & _S_IFREG)
290
291 #if 0
292 /*-- lcc-win32 seems to expand wildcards itself --*/
293 #define APPEND_FILESPEC(root, spec)root=snocString((root), (spec)) \
294 do { \
295 if ((spec)[0] == '-') { \
296 root = snocString((root), (spec)); \
297 } else { \
298 struct _finddata_t c_file; \
299 long hFile; \
300 hFile = _findfirst((spec), &c_file); \
301 if ( hFile == -1L ) { \
302 root = snocString ((root), (spec)); \
303 } else { \
304 int anInt = 0; \
305 while ( anInt == 0 ) { \
306 root = snocString((root), \
307 &c_file.name[0]); \
308 anInt = _findnext(hFile, &c_file); \
309 } \
310 } \
311 } \
312 } while ( 0 )
313 #else
314 #define APPEND_FILESPEC(root, name)root=snocString((root), (name)) \
315 root = snocString ((root), (name))
316 #endif
317
318 #define SET_BINARY_MODE(fd) \
319 do { \
320 int retVal = setmode ( fileno ( fd ), \
321 O_BINARY ); \
322 ERROR_IF_MINUS_ONE ( retVal ){ if ((retVal) == (-1)) ioError(); }; \
323 } while ( 0 )
324
325#endif
326
327
328/*---------------------------------------------*/
329/*--
330 Some more stuff for all platforms :-)
331--*/
332
333#define Boolunsigned char unsigned char
334#define True1 1
335#define False0 0
336
337/*--
338 IntNative is your platform's `native' int size.
339 Only here to avoid probs with 64-bit platforms.
340--*/
341#define IntNativeint int
342
343
344/*--
345 change to 1, or compile with -DDEBUG=1 to debug
346--*/
347#ifndef DEBUG0
348#define DEBUG0 0
349#endif
350
351
352/*---------------------------------------------------*/
353/*--- ---*/
354/*---------------------------------------------------*/
355
356/*--
357 Implementation notes, July 1997
358 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
359
360 Memory allocation
361 ~~~~~~~~~~~~~~~~~
362 All large data structures are allocated on the C heap,
363 for better or for worse. That includes the various
364 arrays of pointers, striped words, bytes, frequency
365 tables and buffers for compression and decompression.
366
367 bzip2 can operate at various block-sizes, ranging from
368 100k to 900k in 100k steps, and it allocates only as
369 much as it needs to. When compressing, we know from the
370 command-line options what the block-size is going to be,
371 so all allocation can be done at start-up; if that
372 succeeds, there can be no further allocation problems.
373
374 Decompression is more complicated. Each compressed file
375 contains, in its header, a byte indicating the block
376 size used for compression. This means bzip2 potentially
377 needs to reallocate memory for each file it deals with,
378 which in turn opens the possibility for a memory allocation
379 failure part way through a run of files, by encountering
380 a file requiring a much larger block size than all the
381 ones preceding it.
382
383 The policy is to simply give up if a memory allocation
384 failure occurs. During decompression, it would be
385 possible to move on to subsequent files in the hope that
386 some might ask for a smaller block size, but the
387 complications for doing this seem more trouble than they
388 are worth.
389
390
391 Compressed file formats
392 ~~~~~~~~~~~~~~~~~~~~~~~
393 [This is now entirely different from both 0.21, and from
394 any previous Huffman-coded variant of bzip.
395 See the associated file bzip2.txt for details.]
396
397
398 Error conditions
399 ~~~~~~~~~~~~~~~~
400 Dealing with error conditions is the least satisfactory
401 aspect of bzip2. The policy is to try and leave the
402 filesystem in a consistent state, then quit, even if it
403 means not processing some of the files mentioned in the
404 command line. `A consistent state' means that a file
405 exists either in its compressed or uncompressed form,
406 but not both. This boils down to the rule `delete the
407 output file if an error condition occurs, leaving the
408 input intact'. Input files are only deleted when we can
409 be pretty sure the output file has been written and
410 closed successfully.
411
412 Errors are a dog because there's so many things to
413 deal with. The following can happen mid-file, and
414 require cleaning up.
415
416 internal `panics' -- indicating a bug
417 corrupted or inconsistent compressed file
418 can't allocate enough memory to decompress this file
419 I/O error reading/writing/opening/closing
420 signal catches -- Control-C, SIGTERM, SIGHUP.
421
422 Other conditions, primarily pertaining to file names,
423 can be checked in-between files, which makes dealing
424 with them easier.
425--*/
426
427
428
429/*---------------------------------------------------*/
430/*--- Misc (file handling) data decls ---*/
431/*---------------------------------------------------*/
432
433static UInt32unsigned int bytesIn, bytesOut;
434static Int32int verbosity;
435static Boolunsigned char smallMode;
436static UInt32unsigned int globalCrc;
437
438
439
440
441static void panic ( Charchar* );
442static void ioError ( void );
443static void uncompressOutOfMemory ( Int32int, Int32int );
444static void blockOverrun ( void );
445static void badBlockHeader ( void );
446static void crcError ( UInt32unsigned int, UInt32unsigned int );
447static void cleanUpAndFail ( Int32int );
448static void compressedStreamEOF ( void );
449
450
451
452/*---------------------------------------------------*/
453/*--- Data decls for the front end ---*/
454/*---------------------------------------------------*/
455
456/*--
457 The overshoot bytes allow us to avoid most of
458 the cost of pointer renormalisation during
459 comparison of rotations in sorting.
460 The figure of 20 is derived as follows:
461 qSort3 allows an overshoot of up to 10.
462 It then calls simpleSort, which calls
463 fullGtU, also with max overshoot 10.
464 fullGtU does up to 10 comparisons without
465 renormalising, giving 10+10 == 20.
466--*/
467#define NUM_OVERSHOOT_BYTES20 20
468
469/*--
470 These are the main data structures for
471 the Burrows-Wheeler transform.
472--*/
473
474/*--
475 Pointers to compression and decompression
476 structures. Set by
477 allocateCompressStructures and
478 setDecompressStructureSizes
479
480 The structures are always set to be suitable
481 for a block of size 100000 * blockSize100k.
482--*/
483static UInt16unsigned short *ll16; /*-- small decompress --*/
484static UCharunsigned char *ll4; /*-- small decompress --*/
485
486static Int32int *tt; /*-- fast decompress --*/
487static UCharunsigned char *ll8; /*-- fast decompress --*/
488
489
490/*--
491 freq table collected to save a pass over the data
492 during decompression.
493--*/
494static Int32int unzftab[256];
495
496
497/*--
498 index of the last char in the block, so
499 the block size == last + 1.
500--*/
501static Int32int last;
502
503
504/*--
505 index in zptr[] of original string after sorting.
506--*/
507static Int32int origPtr;
508
509
510/*--
511 always: in the range 0 .. 9.
512 The current block size is 100000 * this number.
513--*/
514static Int32int blockSize100k;
515
516
517/*--
518 Used when sorting. If too many long comparisons
519 happen, we stop sorting, randomise the block
520 slightly, and try again.
521--*/
522
523static Boolunsigned char blockRandomised;
524
525
526
527/*---------------------------------------------------*/
528/*--- Data decls for the back end ---*/
529/*---------------------------------------------------*/
530
531#define MAX_ALPHA_SIZE258 258
532#define MAX_CODE_LEN23 23
533
534#define RUNA0 0
535#define RUNB1 1
536
537#define N_GROUPS6 6
538#define G_SIZE50 50
539#define N_ITERS4 4
540
541#define MAX_SELECTORS(2 + (900000 / 50)) (2 + (900000 / G_SIZE50))
542
543static Boolunsigned char inUse[256];
544static Int32int nInUse;
545
546static UCharunsigned char seqToUnseq[256];
547static UCharunsigned char unseqToSeq[256];
548
549static UCharunsigned char selector [MAX_SELECTORS(2 + (900000 / 50))];
550static UCharunsigned char selectorMtf[MAX_SELECTORS(2 + (900000 / 50))];
551
552static UCharunsigned char len [N_GROUPS6][MAX_ALPHA_SIZE258];
553
554/*-- decompress only --*/
555static Int32int limit [N_GROUPS6][MAX_ALPHA_SIZE258];
556static Int32int base [N_GROUPS6][MAX_ALPHA_SIZE258];
557static Int32int perm [N_GROUPS6][MAX_ALPHA_SIZE258];
558static Int32int minLens[N_GROUPS6];
559
560
561/*---------------------------------------------------*/
562/*--- 32-bit CRC grunge ---*/
563/*---------------------------------------------------*/
564
565/*--
566 I think this is an implementation of the AUTODIN-II,
567 Ethernet & FDDI 32-bit CRC standard. Vaguely derived
568 from code by Rob Warnock, in Section 51 of the
569 comp.compression FAQ.
570--*/
571
572static UInt32unsigned int crc32Table[256] = {
573
574 /*-- Ugly, innit? --*/
575
576 0x00000000UL, 0x04c11db7UL, 0x09823b6eUL, 0x0d4326d9UL,
577 0x130476dcUL, 0x17c56b6bUL, 0x1a864db2UL, 0x1e475005UL,
578 0x2608edb8UL, 0x22c9f00fUL, 0x2f8ad6d6UL, 0x2b4bcb61UL,
579 0x350c9b64UL, 0x31cd86d3UL, 0x3c8ea00aUL, 0x384fbdbdUL,
580 0x4c11db70UL, 0x48d0c6c7UL, 0x4593e01eUL, 0x4152fda9UL,
581 0x5f15adacUL, 0x5bd4b01bUL, 0x569796c2UL, 0x52568b75UL,
582 0x6a1936c8UL, 0x6ed82b7fUL, 0x639b0da6UL, 0x675a1011UL,
583 0x791d4014UL, 0x7ddc5da3UL, 0x709f7b7aUL, 0x745e66cdUL,
584 0x9823b6e0UL, 0x9ce2ab57UL, 0x91a18d8eUL, 0x95609039UL,
585 0x8b27c03cUL, 0x8fe6dd8bUL, 0x82a5fb52UL, 0x8664e6e5UL,
586 0xbe2b5b58UL, 0xbaea46efUL, 0xb7a96036UL, 0xb3687d81UL,
587 0xad2f2d84UL, 0xa9ee3033UL, 0xa4ad16eaUL, 0xa06c0b5dUL,
588 0xd4326d90UL, 0xd0f37027UL, 0xddb056feUL, 0xd9714b49UL,
589 0xc7361b4cUL, 0xc3f706fbUL, 0xceb42022UL, 0xca753d95UL,
590 0xf23a8028UL, 0xf6fb9d9fUL, 0xfbb8bb46UL, 0xff79a6f1UL,
591 0xe13ef6f4UL, 0xe5ffeb43UL, 0xe8bccd9aUL, 0xec7dd02dUL,
592 0x34867077UL, 0x30476dc0UL, 0x3d044b19UL, 0x39c556aeUL,
593 0x278206abUL, 0x23431b1cUL, 0x2e003dc5UL, 0x2ac12072UL,
594 0x128e9dcfUL, 0x164f8078UL, 0x1b0ca6a1UL, 0x1fcdbb16UL,
595 0x018aeb13UL, 0x054bf6a4UL, 0x0808d07dUL, 0x0cc9cdcaUL,
596 0x7897ab07UL, 0x7c56b6b0UL, 0x71159069UL, 0x75d48ddeUL,
597 0x6b93dddbUL, 0x6f52c06cUL, 0x6211e6b5UL, 0x66d0fb02UL,
598 0x5e9f46bfUL, 0x5a5e5b08UL, 0x571d7dd1UL, 0x53dc6066UL,
599 0x4d9b3063UL, 0x495a2dd4UL, 0x44190b0dUL, 0x40d816baUL,
600 0xaca5c697UL, 0xa864db20UL, 0xa527fdf9UL, 0xa1e6e04eUL,
601 0xbfa1b04bUL, 0xbb60adfcUL, 0xb6238b25UL, 0xb2e29692UL,
602 0x8aad2b2fUL, 0x8e6c3698UL, 0x832f1041UL, 0x87ee0df6UL,
603 0x99a95df3UL, 0x9d684044UL, 0x902b669dUL, 0x94ea7b2aUL,
604 0xe0b41de7UL, 0xe4750050UL, 0xe9362689UL, 0xedf73b3eUL,
605 0xf3b06b3bUL, 0xf771768cUL, 0xfa325055UL, 0xfef34de2UL,
606 0xc6bcf05fUL, 0xc27dede8UL, 0xcf3ecb31UL, 0xcbffd686UL,
607 0xd5b88683UL, 0xd1799b34UL, 0xdc3abdedUL, 0xd8fba05aUL,
608 0x690ce0eeUL, 0x6dcdfd59UL, 0x608edb80UL, 0x644fc637UL,
609 0x7a089632UL, 0x7ec98b85UL, 0x738aad5cUL, 0x774bb0ebUL,
610 0x4f040d56UL, 0x4bc510e1UL, 0x46863638UL, 0x42472b8fUL,
611 0x5c007b8aUL, 0x58c1663dUL, 0x558240e4UL, 0x51435d53UL,
612 0x251d3b9eUL, 0x21dc2629UL, 0x2c9f00f0UL, 0x285e1d47UL,
613 0x36194d42UL, 0x32d850f5UL, 0x3f9b762cUL, 0x3b5a6b9bUL,
614 0x0315d626UL, 0x07d4cb91UL, 0x0a97ed48UL, 0x0e56f0ffUL,
615 0x1011a0faUL, 0x14d0bd4dUL, 0x19939b94UL, 0x1d528623UL,
616 0xf12f560eUL, 0xf5ee4bb9UL, 0xf8ad6d60UL, 0xfc6c70d7UL,
617 0xe22b20d2UL, 0xe6ea3d65UL, 0xeba91bbcUL, 0xef68060bUL,
618 0xd727bbb6UL, 0xd3e6a601UL, 0xdea580d8UL, 0xda649d6fUL,
619 0xc423cd6aUL, 0xc0e2d0ddUL, 0xcda1f604UL, 0xc960ebb3UL,
620 0xbd3e8d7eUL, 0xb9ff90c9UL, 0xb4bcb610UL, 0xb07daba7UL,
621 0xae3afba2UL, 0xaafbe615UL, 0xa7b8c0ccUL, 0xa379dd7bUL,
622 0x9b3660c6UL, 0x9ff77d71UL, 0x92b45ba8UL, 0x9675461fUL,
623 0x8832161aUL, 0x8cf30badUL, 0x81b02d74UL, 0x857130c3UL,
624 0x5d8a9099UL, 0x594b8d2eUL, 0x5408abf7UL, 0x50c9b640UL,
625 0x4e8ee645UL, 0x4a4ffbf2UL, 0x470cdd2bUL, 0x43cdc09cUL,
626 0x7b827d21UL, 0x7f436096UL, 0x7200464fUL, 0x76c15bf8UL,
627 0x68860bfdUL, 0x6c47164aUL, 0x61043093UL, 0x65c52d24UL,
628 0x119b4be9UL, 0x155a565eUL, 0x18197087UL, 0x1cd86d30UL,
629 0x029f3d35UL, 0x065e2082UL, 0x0b1d065bUL, 0x0fdc1becUL,
630 0x3793a651UL, 0x3352bbe6UL, 0x3e119d3fUL, 0x3ad08088UL,
631 0x2497d08dUL, 0x2056cd3aUL, 0x2d15ebe3UL, 0x29d4f654UL,
632 0xc5a92679UL, 0xc1683bceUL, 0xcc2b1d17UL, 0xc8ea00a0UL,
633 0xd6ad50a5UL, 0xd26c4d12UL, 0xdf2f6bcbUL, 0xdbee767cUL,
634 0xe3a1cbc1UL, 0xe760d676UL, 0xea23f0afUL, 0xeee2ed18UL,
635 0xf0a5bd1dUL, 0xf464a0aaUL, 0xf9278673UL, 0xfde69bc4UL,
636 0x89b8fd09UL, 0x8d79e0beUL, 0x803ac667UL, 0x84fbdbd0UL,
637 0x9abc8bd5UL, 0x9e7d9662UL, 0x933eb0bbUL, 0x97ffad0cUL,
638 0xafb010b1UL, 0xab710d06UL, 0xa6322bdfUL, 0xa2f33668UL,
639 0xbcb4666dUL, 0xb8757bdaUL, 0xb5365d03UL, 0xb1f740b4UL
640};
641
642
643/*---------------------------------------------*/
644
645static void initialiseCRC ( void )
646{
647 globalCrc = 0xffffffffUL;
648}
649
650
651/*---------------------------------------------*/
652
653static UInt32unsigned int getFinalCRC ( void )
654{
655 return ~globalCrc;
656}
657
658
659/*---------------------------------------------*/
660
661static UInt32unsigned int getGlobalCRC ( void )
662{
663 return globalCrc;
664}
665
666
667/*---------------------------------------------*/
668
669static void setGlobalCRC ( UInt32unsigned int newCrc )
670{
671 globalCrc = newCrc;
672}
673
674
675/*---------------------------------------------*/
676
677#define UPDATE_CRC(crcVar,cha){ crcVar = (crcVar << 8) ^ crc32Table[(crcVar >> 24
) ^ ((unsigned char)cha)]; }
\
678{ \
679 crcVar = (crcVar << 8) ^ \
680 crc32Table[(crcVar >> 24) ^ \
681 ((UCharunsigned char)cha)]; \
682}
683
684
685/*---------------------------------------------------*/
686/*--- Bit stream I/O ---*/
687/*---------------------------------------------------*/
688
689
690static UInt32unsigned int bsBuff;
691static Int32int bsLive;
692static void* bsStream;
693static Boolunsigned char bsWriting;
694
695
696/*---------------------------------------------*/
697
698static void bsSetStream ( void* f, Boolunsigned char wr )
699{
700 if (bsStream != NULL((void*)0)) panic ( "bsSetStream" );
701 bsStream = f;
702 bsLive = 0;
703 bsBuff = 0;
704 bytesOut = 0;
705 bytesIn = 0;
706 bsWriting = wr;
707}
708
709
710/*---------------------------------------------*/
711
712static void bsFinishedWithStream ( void )
713{
714 if (bsWriting)
715 while (bsLive > 0) {
716 bz2_putc ( (UCharunsigned char)(bsBuff >> 24), bsStream );
717 bsBuff <<= 8;
718 bsLive -= 8;
719 bytesOut++;
720 }
721 bsStream = NULL((void*)0);
722}
723
724
725/*---------------------------------------------*/
726
727#define bsNEEDR(nz){ while (bsLive < nz) { int zzi = bz2_getc ( bsStream ); if
(zzi == (-1)) compressedStreamEOF(); bsBuff = (bsBuff <<
8) | (zzi & 0xffL); bsLive += 8; } }
\
728{ \
729 while (bsLive < nz) { \
730 Int32int zzi = bz2_getc ( bsStream ); \
731 if (zzi == EOF(-1)) compressedStreamEOF(); \
732 bsBuff = (bsBuff << 8) | (zzi & 0xffL); \
733 bsLive += 8; \
734 } \
735}
736
737
738/*---------------------------------------------*/
739
740#define bsR1(vz){ { while (bsLive < 1) { int zzi = bz2_getc ( bsStream ); if
(zzi == (-1)) compressedStreamEOF(); bsBuff = (bsBuff <<
8) | (zzi & 0xffL); bsLive += 8; } }; vz = (bsBuff >>
(bsLive-1)) & 1; bsLive--; }
\
741{ \
742 bsNEEDR(1){ while (bsLive < 1) { int zzi = bz2_getc ( bsStream ); if
(zzi == (-1)) compressedStreamEOF(); bsBuff = (bsBuff <<
8) | (zzi & 0xffL); bsLive += 8; } }
; \
743 vz = (bsBuff >> (bsLive-1)) & 1; \
744 bsLive--; \
745}
746
747
748/*---------------------------------------------*/
749
750static INLINEinline UInt32unsigned int bsR ( Int32int n )
751{
752 UInt32unsigned int v;
753 bsNEEDR ( n ){ while (bsLive < n) { int zzi = bz2_getc ( bsStream ); if
(zzi == (-1)) compressedStreamEOF(); bsBuff = (bsBuff <<
8) | (zzi & 0xffL); bsLive += 8; } }
;
754 v = (bsBuff >> (bsLive-n)) & ((1 << n)-1);
755 bsLive -= n;
756 return v;
757}
758
759
760/*---------------------------------------------*/
761
762static UCharunsigned char bsGetUChar ( void )
763{
764 return (UCharunsigned char)bsR(8);
765}
766
767
768
769/*---------------------------------------------*/
770
771static Int32int bsGetUInt32 ( void )
772{
773 UInt32unsigned int u;
774 u = 0;
775 u = (u << 8) | bsR(8);
776 u = (u << 8) | bsR(8);
777 u = (u << 8) | bsR(8);
778 u = (u << 8) | bsR(8);
779 return u;
780}
781
782
783/*---------------------------------------------*/
784
785static UInt32unsigned int bsGetIntVS ( UInt32unsigned int numBits )
786{
787 return (UInt32unsigned int)bsR(numBits);
788}
789
790
791
792/*---------------------------------------------------*/
793/*--- Huffman coding low-level stuff ---*/
794/*---------------------------------------------------*/
795
796
797/*---------------------------------------------*/
798
799static void hbCreateDecodeTables ( Int32int *limit,
800 Int32int *base,
801 Int32int *perm,
802 UCharunsigned char *length,
803 Int32int minLen,
804 Int32int maxLen,
805 Int32int alphaSize )
806{
807 Int32int pp, i, j, vec;
808
809 pp = 0;
810 for (i = minLen; i <= maxLen; i++)
811 for (j = 0; j < alphaSize; j++)
812 if (length[j] == i) { perm[pp] = j; pp++; };
813
814 for (i = 0; i < MAX_CODE_LEN23; i++) base[i] = 0;
815 for (i = 0; i < alphaSize; i++) base[length[i]+1]++;
816
817 for (i = 1; i < MAX_CODE_LEN23; i++) base[i] += base[i-1];
818
819 for (i = 0; i < MAX_CODE_LEN23; i++) limit[i] = 0;
820 vec = 0;
821
822 for (i = minLen; i <= maxLen; i++) {
823 vec += (base[i+1] - base[i]);
824 limit[i] = vec-1;
825 vec <<= 1;
826 }
827 for (i = minLen + 1; i <= maxLen; i++)
828 base[i] = ((limit[i-1] + 1) << 1) - base[i];
829}
830
831
832
833/*---------------------------------------------------*/
834/*--- Undoing the reversible transformation ---*/
835/*---------------------------------------------------*/
836
837/*---------------------------------------------*/
838
839#define SET_LL4(i,n){ if (((i) & 0x1) == 0) ll4[(i) >> 1] = (ll4[(i) >>
1] & 0xf0) | (n); else ll4[(i) >> 1] = (ll4[(i) >>
1] & 0x0f) | ((n) << 4); }
\
840 { if (((i) & 0x1) == 0) \
841 ll4[(i) >> 1] = (ll4[(i) >> 1] & 0xf0) | (n); else \
842 ll4[(i) >> 1] = (ll4[(i) >> 1] & 0x0f) | ((n) << 4); \
843 }
844
845
846#define GET_LL4(i)(((unsigned int)(ll4[(i) >> 1])) >> (((i) <<
2) & 0x4) & 0xF)
\
847 (((UInt32unsigned int)(ll4[(i) >> 1])) >> (((i) << 2) & 0x4) & 0xF)
848
849
850#define SET_LL(i,n){ ll16[i] = (unsigned short)(n & 0x0000ffff); { if (((i) &
0x1) == 0) ll4[(i) >> 1] = (ll4[(i) >> 1] & 0xf0
) | (n >> 16); else ll4[(i) >> 1] = (ll4[(i) >>
1] & 0x0f) | ((n >> 16) << 4); }; }
\
851 { ll16[i] = (UInt16unsigned short)(n & 0x0000ffff); \
852 SET_LL4(i, n >> 16){ if (((i) & 0x1) == 0) ll4[(i) >> 1] = (ll4[(i) >>
1] & 0xf0) | (n >> 16); else ll4[(i) >> 1] =
(ll4[(i) >> 1] & 0x0f) | ((n >> 16) <<
4); }
; \
853 }
854
855
856#define GET_LL(i)(((unsigned int)ll16[i]) | ((((unsigned int)(ll4[(i) >>
1])) >> (((i) << 2) & 0x4) & 0xF) <<
16))
\
857 (((UInt32unsigned int)ll16[i]) | (GET_LL4(i)(((unsigned int)(ll4[(i) >> 1])) >> (((i) <<
2) & 0x4) & 0xF)
<< 16))
858
859
860/*---------------------------------------------*/
861/*--
862 Manage memory for compression/decompression.
863 When compressing, a single block size applies to
864 all files processed, and that's set when the
865 program starts. But when decompressing, each file
866 processed could have been compressed with a
867 different block size, so we may have to free
868 and reallocate on a per-file basis.
869
870 A call with argument of zero means
871 `free up everything.' And a value of zero for
872 blockSize100k means no memory is currently allocated.
873--*/
874
875
876/*---------------------------------------------*/
877
878static void setDecompressStructureSizes ( Int32int newSize100k )
879{
880 if (! (0 <= newSize100k && newSize100k <= 9 &&
881 0 <= blockSize100k && blockSize100k <= 9))
882 panic ( "setDecompressStructureSizes" );
883
884 if (newSize100k == blockSize100k) return;
885
886 blockSize100k = newSize100k;
887
888 if (ll16 != NULL((void*)0)) free ( ll16 );
889 if (ll4 != NULL((void*)0)) free ( ll4 );
890 if (ll8 != NULL((void*)0)) free ( ll8 );
891 if (tt != NULL((void*)0)) free ( tt );
892
893 if (newSize100k == 0) return;
894
895 if (smallMode) {
896
897 Int32int n = 100000 * newSize100k;
898 ll16 = malloc ( n * sizeof(UInt16unsigned short) );
899 ll4 = malloc ( ((n+1) >> 1) * sizeof(UCharunsigned char) );
900
901 if (ll4 == NULL((void*)0) || ll16 == NULL((void*)0)) {
902 Int32int totalDraw
903 = n * sizeof(Int16short) + ((n+1) >> 1) * sizeof(UCharunsigned char);
904 uncompressOutOfMemory ( totalDraw, n );
905 }
906
907 } else {
908
909 Int32int n = 100000 * newSize100k;
910 ll8 = malloc ( n * sizeof(UCharunsigned char) );
911 tt = malloc ( n * sizeof(Int32int) );
912
913 if (ll8 == NULL((void*)0) || tt == NULL((void*)0)) {
914 Int32int totalDraw
915 = n * sizeof(UCharunsigned char) + n * sizeof(UInt32unsigned int);
916 uncompressOutOfMemory ( totalDraw, n );
917 }
918
919 }
920}
921
922
923
924/*---------------------------------------------------*/
925/*--- The new back end ---*/
926/*---------------------------------------------------*/
927
928/*---------------------------------------------*/
929
930static void makeMaps ( void )
931{
932 Int32int i;
933 nInUse = 0;
934 for (i = 0; i < 256; i++)
935 if (inUse[i]) {
936 seqToUnseq[nInUse] = i;
937 unseqToSeq[i] = nInUse;
938 nInUse++;
939 }
940}
941
942
943
944/*---------------------------------------------*/
945
946static void recvDecodingTables ( void )
947{
948 Int32int i, j, t, nGroups, nSelectors, alphaSize;
949 Int32int minLen, maxLen;
950 Boolunsigned char inUse16[16];
951
952 /*--- Receive the mapping table ---*/
953 for (i = 0; i < 16; i++)
954 if (bsR(1) == 1)
955 inUse16[i] = True1; else
956 inUse16[i] = False0;
957
958 for (i = 0; i < 256; i++) inUse[i] = False0;
959
960 for (i = 0; i < 16; i++)
961 if (inUse16[i])
962 for (j = 0; j < 16; j++)
963 if (bsR(1) == 1) inUse[i * 16 + j] = True1;
964
965 makeMaps();
966 alphaSize = nInUse+2;
967
968 /*--- Now the selectors ---*/
969 nGroups = bsR ( 3 );
970 nSelectors = bsR ( 15 );
971 for (i = 0; i < nSelectors; i++) {
972 j = 0;
973 while (bsR(1) == 1) j++;
974 selectorMtf[i] = j;
975 }
976
977 /*--- Undo the MTF values for the selectors. ---*/
978 {
979 UCharunsigned char pos[N_GROUPS6], tmp, v;
980 for (v = 0; v < nGroups; v++) pos[v] = v;
981
982 for (i = 0; i < nSelectors; i++) {
983 v = selectorMtf[i];
984 tmp = pos[v];
985 while (v > 0) { pos[v] = pos[v-1]; v--; }
986 pos[0] = tmp;
987 selector[i] = tmp;
988 }
989 }
990
991 /*--- Now the coding tables ---*/
992 for (t = 0; t < nGroups; t++) {
993 Int32int curr = bsR ( 5 );
994 for (i = 0; i < alphaSize; i++) {
995 while (bsR(1) == 1) {
996 if (bsR(1) == 0) curr++; else curr--;
997 }
998 len[t][i] = curr;
999 }
1000 }
1001
1002 /*--- Create the Huffman decoding tables ---*/
1003 for (t = 0; t < nGroups; t++) {
1004 minLen = 32;
1005 maxLen = 0;
1006 for (i = 0; i < alphaSize; i++) {
1007 if (len[t][i] > maxLen) maxLen = len[t][i];
1008 if (len[t][i] < minLen) minLen = len[t][i];
1009 }
1010 hbCreateDecodeTables (
1011 &limit[t][0], &base[t][0], &perm[t][0], &len[t][0],
1012 minLen, maxLen, alphaSize
1013 );
1014 minLens[t] = minLen;
1015 }
1016}
1017
1018
1019/*---------------------------------------------*/
1020
1021#define GET_MTF_VAL(lval){ int zt, zn, zvec, zj; if (groupPos == 0) { groupNo++; groupPos
= 50; } groupPos--; zt = selector[groupNo]; zn = minLens[zt]
; zvec = bsR ( zn ); while (zvec > limit[zt][zn]) { zn++; {
{ while (bsLive < 1) { int zzi = bz2_getc ( bsStream ); if
(zzi == (-1)) compressedStreamEOF(); bsBuff = (bsBuff <<
8) | (zzi & 0xffL); bsLive += 8; } }; zj = (bsBuff >>
(bsLive-1)) & 1; bsLive--; }; zvec = (zvec << 1) |
zj; }; lval = perm[zt][zvec - base[zt][zn]]; }
\
1022{ \
1023 Int32int zt, zn, zvec, zj; \
1024 if (groupPos == 0) { \
1025 groupNo++; \
1026 groupPos = G_SIZE50; \
1027 } \
1028 groupPos--; \
1029 zt = selector[groupNo]; \
1030 zn = minLens[zt]; \
1031 zvec = bsR ( zn ); \
1032 while (zvec > limit[zt][zn]) { \
1033 zn++; bsR1(zj){ { while (bsLive < 1) { int zzi = bz2_getc ( bsStream ); if
(zzi == (-1)) compressedStreamEOF(); bsBuff = (bsBuff <<
8) | (zzi & 0xffL); bsLive += 8; } }; zj = (bsBuff >>
(bsLive-1)) & 1; bsLive--; }
; \
1034 zvec = (zvec << 1) | zj; \
1035 }; \
1036 lval = perm[zt][zvec - base[zt][zn]]; \
1037}
1038
1039
1040/*---------------------------------------------*/
1041
1042static void getAndMoveToFrontDecode ( void )
1043{
1044 UCharunsigned char yy[256];
1045 Int32int i, j, nextSym, limitLast;
1046 Int32int EOB, groupNo, groupPos;
1047
1048 limitLast = 100000 * blockSize100k;
1049 origPtr = bsGetIntVS ( 24 );
1050
1051 recvDecodingTables();
1052 EOB = nInUse+1;
1053 groupNo = -1;
1054 groupPos = 0;
1055
1056 /*--
1057 Setting up the unzftab entries here is not strictly
1058 necessary, but it does save having to do it later
1059 in a separate pass, and so saves a block's worth of
1060 cache misses.
1061 --*/
1062 for (i = 0; i <= 255; i++) unzftab[i] = 0;
1063
1064 for (i = 0; i <= 255; i++) yy[i] = (UCharunsigned char) i;
1065
1066 last = -1;
1067
1068 GET_MTF_VAL(nextSym){ int zt, zn, zvec, zj; if (groupPos == 0) { groupNo++; groupPos
= 50; } groupPos--; zt = selector[groupNo]; zn = minLens[zt]
; zvec = bsR ( zn ); while (zvec > limit[zt][zn]) { zn++; {
{ while (bsLive < 1) { int zzi = bz2_getc ( bsStream ); if
(zzi == (-1)) compressedStreamEOF(); bsBuff = (bsBuff <<
8) | (zzi & 0xffL); bsLive += 8; } }; zj = (bsBuff >>
(bsLive-1)) & 1; bsLive--; }; zvec = (zvec << 1) |
zj; }; nextSym = perm[zt][zvec - base[zt][zn]]; }
;
1069
1070 while (True1) {
1071
1072 if (nextSym == EOB) break;
1073
1074 if (nextSym == RUNA0 || nextSym == RUNB1) {
1075 UCharunsigned char ch;
1076 Int32int s = -1;
1077 Int32int N = 1;
1078 do {
1079 if (nextSym == RUNA0) s = s + (0+1) * N; else
1080 if (nextSym == RUNB1) s = s + (1+1) * N;
1081 N = N * 2;
1082 GET_MTF_VAL(nextSym){ int zt, zn, zvec, zj; if (groupPos == 0) { groupNo++; groupPos
= 50; } groupPos--; zt = selector[groupNo]; zn = minLens[zt]
; zvec = bsR ( zn ); while (zvec > limit[zt][zn]) { zn++; {
{ while (bsLive < 1) { int zzi = bz2_getc ( bsStream ); if
(zzi == (-1)) compressedStreamEOF(); bsBuff = (bsBuff <<
8) | (zzi & 0xffL); bsLive += 8; } }; zj = (bsBuff >>
(bsLive-1)) & 1; bsLive--; }; zvec = (zvec << 1) |
zj; }; nextSym = perm[zt][zvec - base[zt][zn]]; }
;
1083 }
1084 while (nextSym == RUNA0 || nextSym == RUNB1);
1085
1086 s++;
1087 ch = seqToUnseq[yy[0]];
1088 unzftab[ch] += s;
1089
1090 if (smallMode)
1091 while (s > 0) {
1092 last++;
1093 ll16[last] = ch;
1094 s--;
1095 }
1096 else
1097 while (s > 0) {
1098 last++;
1099 ll8[last] = ch;
1100 s--;
1101 };
1102
1103 if (last >= limitLast) blockOverrun();
1104 continue;
1105
1106 } else {
1107
1108 UCharunsigned char tmp;
1109 last++; if (last >= limitLast) blockOverrun();
1110
1111 tmp = yy[nextSym-1];
1112 unzftab[seqToUnseq[tmp]]++;
1113 if (smallMode)
1114 ll16[last] = seqToUnseq[tmp]; else
1115 ll8[last] = seqToUnseq[tmp];
1116
1117 /*--
1118 This loop is hammered during decompression,
1119 hence the unrolling.
1120
1121 for (j = nextSym-1; j > 0; j--) yy[j] = yy[j-1];
1122 --*/
1123
1124 j = nextSym-1;
1125 for (; j > 3; j -= 4) {
1126 yy[j] = yy[j-1];
1127 yy[j-1] = yy[j-2];
1128 yy[j-2] = yy[j-3];
1129 yy[j-3] = yy[j-4];
1130 }
1131 for (; j > 0; j--) yy[j] = yy[j-1];
1132
1133 yy[0] = tmp;
1134 GET_MTF_VAL(nextSym){ int zt, zn, zvec, zj; if (groupPos == 0) { groupNo++; groupPos
= 50; } groupPos--; zt = selector[groupNo]; zn = minLens[zt]
; zvec = bsR ( zn ); while (zvec > limit[zt][zn]) { zn++; {
{ while (bsLive < 1) { int zzi = bz2_getc ( bsStream ); if
(zzi == (-1)) compressedStreamEOF(); bsBuff = (bsBuff <<
8) | (zzi & 0xffL); bsLive += 8; } }; zj = (bsBuff >>
(bsLive-1)) & 1; bsLive--; }; zvec = (zvec << 1) |
zj; }; nextSym = perm[zt][zvec - base[zt][zn]]; }
;
1135 continue;
1136 }
1137 }
1138}
1139
1140
1141/*---------------------------------------------------*/
1142/*--- Stuff for randomising repetitive blocks ---*/
1143/*---------------------------------------------------*/
1144
1145/*---------------------------------------------*/
1146static Int32int rNums[512] = {
1147 619, 720, 127, 481, 931, 816, 813, 233, 566, 247,
1148 985, 724, 205, 454, 863, 491, 741, 242, 949, 214,
1149 733, 859, 335, 708, 621, 574, 73, 654, 730, 472,
1150 419, 436, 278, 496, 867, 210, 399, 680, 480, 51,
1151 878, 465, 811, 169, 869, 675, 611, 697, 867, 561,
1152 862, 687, 507, 283, 482, 129, 807, 591, 733, 623,
1153 150, 238, 59, 379, 684, 877, 625, 169, 643, 105,
1154 170, 607, 520, 932, 727, 476, 693, 425, 174, 647,
1155 73, 122, 335, 530, 442, 853, 695, 249, 445, 515,
1156 909, 545, 703, 919, 874, 474, 882, 500, 594, 612,
1157 641, 801, 220, 162, 819, 984, 589, 513, 495, 799,
1158 161, 604, 958, 533, 221, 400, 386, 867, 600, 782,
1159 382, 596, 414, 171, 516, 375, 682, 485, 911, 276,
1160 98, 553, 163, 354, 666, 933, 424, 341, 533, 870,
1161 227, 730, 475, 186, 263, 647, 537, 686, 600, 224,
1162 469, 68, 770, 919, 190, 373, 294, 822, 808, 206,
1163 184, 943, 795, 384, 383, 461, 404, 758, 839, 887,
1164 715, 67, 618, 276, 204, 918, 873, 777, 604, 560,
1165 951, 160, 578, 722, 79, 804, 96, 409, 713, 940,
1166 652, 934, 970, 447, 318, 353, 859, 672, 112, 785,
1167 645, 863, 803, 350, 139, 93, 354, 99, 820, 908,
1168 609, 772, 154, 274, 580, 184, 79, 626, 630, 742,
1169 653, 282, 762, 623, 680, 81, 927, 626, 789, 125,
1170 411, 521, 938, 300, 821, 78, 343, 175, 128, 250,
1171 170, 774, 972, 275, 999, 639, 495, 78, 352, 126,
1172 857, 956, 358, 619, 580, 124, 737, 594, 701, 612,
1173 669, 112, 134, 694, 363, 992, 809, 743, 168, 974,
1174 944, 375, 748, 52, 600, 747, 642, 182, 862, 81,
1175 344, 805, 988, 739, 511, 655, 814, 334, 249, 515,
1176 897, 955, 664, 981, 649, 113, 974, 459, 893, 228,
1177 433, 837, 553, 268, 926, 240, 102, 654, 459, 51,
1178 686, 754, 806, 760, 493, 403, 415, 394, 687, 700,
1179 946, 670, 656, 610, 738, 392, 760, 799, 887, 653,
1180 978, 321, 576, 617, 626, 502, 894, 679, 243, 440,
1181 680, 879, 194, 572, 640, 724, 926, 56, 204, 700,
1182 707, 151, 457, 449, 797, 195, 791, 558, 945, 679,
1183 297, 59, 87, 824, 713, 663, 412, 693, 342, 606,
1184 134, 108, 571, 364, 631, 212, 174, 643, 304, 329,
1185 343, 97, 430, 751, 497, 314, 983, 374, 822, 928,
1186 140, 206, 73, 263, 980, 736, 876, 478, 430, 305,
1187 170, 514, 364, 692, 829, 82, 855, 953, 676, 246,
1188 369, 970, 294, 750, 807, 827, 150, 790, 288, 923,
1189 804, 378, 215, 828, 592, 281, 565, 555, 710, 82,
1190 896, 831, 547, 261, 524, 462, 293, 465, 502, 56,
1191 661, 821, 976, 991, 658, 869, 905, 758, 745, 193,
1192 768, 550, 608, 933, 378, 286, 215, 979, 792, 961,
1193 61, 688, 793, 644, 986, 403, 106, 366, 905, 644,
1194 372, 567, 466, 434, 645, 210, 389, 550, 919, 135,
1195 780, 773, 635, 389, 707, 100, 626, 958, 165, 504,
1196 920, 176, 193, 713, 857, 265, 203, 50, 668, 108,
1197 645, 990, 626, 197, 510, 357, 358, 850, 858, 364,
1198 936, 638
1199};
1200
1201
1202#define RAND_DECLSint rNToGo = 0; int rTPos = 0; \
1203 Int32int rNToGo = 0; \
1204 Int32int rTPos = 0; \
1205
1206#define RAND_MASK((rNToGo == 1) ? 1 : 0) ((rNToGo == 1) ? 1 : 0)
1207
1208#define RAND_UPD_MASKif (rNToGo == 0) { rNToGo = rNums[rTPos]; rTPos++; if (rTPos ==
512) rTPos = 0; } rNToGo--;
\
1209 if (rNToGo == 0) { \
1210 rNToGo = rNums[rTPos]; \
1211 rTPos++; if (rTPos == 512) rTPos = 0; \
1212 } \
1213 rNToGo--;
1214
1215
1216
1217/*---------------------------------------------------*/
1218/*--- The Reversible Transformation (tm) ---*/
1219/*---------------------------------------------------*/
1220
1221/*---------------------------------------------*/
1222
1223
1224static INLINEinline Int32int indexIntoF ( Int32int indx, Int32int *cftab )
1225{
1226 Int32int nb, na, mid;
1227 nb = 0;
1228 na = 256;
1229 do {
1230 mid = (nb + na) >> 1;
1231 if (indx >= cftab[mid]) nb = mid; else na = mid;
1232 }
1233 while (na - nb != 1);
1234 return nb;
1235}
1236
1237
1238
1239#define GET_SMALL(cccc) \
1240 \
1241 cccc = indexIntoF ( tPos, cftab ); \
1242 tPos = GET_LL(tPos)(((unsigned int)ll16[tPos]) | ((((unsigned int)(ll4[(tPos) >>
1])) >> (((tPos) << 2) & 0x4) & 0xF) <<
16))
;
1243
1244
1245
1246static void undoReversibleTransformation_small ( void* dst )
1247{
1248 Int32int cftab[257], cftabAlso[257];
1249 Int32int i, j, tmp, tPos;
1250 UCharunsigned char ch;
1251
1252 /*--
1253 We assume here that the global array unzftab will
1254 already be holding the frequency counts for
1255 ll8[0 .. last].
1256 --*/
1257
1258 /*-- Set up cftab to facilitate generation of indexIntoF --*/
1259 cftab[0] = 0;
1260 for (i = 1; i <= 256; i++) cftab[i] = unzftab[i-1];
1261 for (i = 1; i <= 256; i++) cftab[i] += cftab[i-1];
1262
1263 /*-- Make a copy of it, used in generation of T --*/
1264 for (i = 0; i <= 256; i++) cftabAlso[i] = cftab[i];
1265
1266 /*-- compute the T vector --*/
1267 for (i = 0; i <= last; i++) {
1268 ch = (UCharunsigned char)ll16[i];
1269 SET_LL(i, cftabAlso[ch]){ ll16[i] = (unsigned short)(cftabAlso[ch] & 0x0000ffff);
{ if (((i) & 0x1) == 0) ll4[(i) >> 1] = (ll4[(i) >>
1] & 0xf0) | (cftabAlso[ch] >> 16); else ll4[(i) >>
1] = (ll4[(i) >> 1] & 0x0f) | ((cftabAlso[ch] >>
16) << 4); }; }
;
1270 cftabAlso[ch]++;
1271 }
1272
1273 /*--
1274 Compute T^(-1) by pointer reversal on T. This is rather
1275 subtle, in that, if the original block was two or more
1276 (in general, N) concatenated copies of the same thing,
1277 the T vector will consist of N cycles, each of length
1278 blocksize / N, and decoding will involve traversing one
1279 of these cycles N times. Which particular cycle doesn't
1280 matter -- they are all equivalent. The tricky part is to
1281 make sure that the pointer reversal creates a correct
1282 reversed cycle for us to traverse. So, the code below
1283 simply reverses whatever cycle origPtr happens to fall into,
1284 without regard to the cycle length. That gives one reversed
1285 cycle, which for normal blocks, is the entire block-size long.
1286 For repeated blocks, it will be interspersed with the other
1287 N-1 non-reversed cycles. Providing that the F-subscripting
1288 phase which follows starts at origPtr, all then works ok.
1289 --*/
1290 i = origPtr;
1291 j = GET_LL(i)(((unsigned int)ll16[i]) | ((((unsigned int)(ll4[(i) >>
1])) >> (((i) << 2) & 0x4) & 0xF) <<
16))
;
1292 do {
1293 tmp = GET_LL(j)(((unsigned int)ll16[j]) | ((((unsigned int)(ll4[(j) >>
1])) >> (((j) << 2) & 0x4) & 0xF) <<
16))
;
1294 SET_LL(j, i){ ll16[j] = (unsigned short)(i & 0x0000ffff); { if (((j) &
0x1) == 0) ll4[(j) >> 1] = (ll4[(j) >> 1] & 0xf0
) | (i >> 16); else ll4[(j) >> 1] = (ll4[(j) >>
1] & 0x0f) | ((i >> 16) << 4); }; }
;
1295 i = j;
1296 j = tmp;
1297 }
1298 while (i != origPtr);
1299
1300 /*--
1301 We recreate the original by subscripting F through T^(-1).
1302 The run-length-decoder below requires characters incrementally,
1303 so tPos is set to a starting value, and is updated by
1304 the GET_SMALL macro.
1305 --*/
1306 tPos = origPtr;
1307
1308 /*-------------------------------------------------*/
1309 /*--
1310 This is pretty much a verbatim copy of the
1311 run-length decoder present in the distribution
1312 bzip-0.21; it has to be here to avoid creating
1313 block[] as an intermediary structure. As in 0.21,
1314 this code derives from some sent to me by
1315 Christian von Roques.
1316
1317 It allows dst==NULL, so as to support the test (-t)
1318 option without slowing down the fast decompression
1319 code.
1320 --*/
1321 {
1322 IntNativeint retVal;
1323 Int32int i2, count, chPrev, ch2;
1324 UInt32unsigned int localCrc;
1325
1326 count = 0;
1327 i2 = 0;
1328 ch2 = 256; /*-- not a char and not EOF --*/
1329 localCrc = getGlobalCRC();
1330
1331 {
1332 RAND_DECLSint rNToGo = 0; int rTPos = 0;;
1333 while ( i2 <= last ) {
1334 chPrev = ch2;
1335 GET_SMALL(ch2);
1336 if (blockRandomised) {
1337 RAND_UPD_MASKif (rNToGo == 0) { rNToGo = rNums[rTPos]; rTPos++; if (rTPos ==
512) rTPos = 0; } rNToGo--;
;
1338 ch2 ^= (UInt32unsigned int)RAND_MASK((rNToGo == 1) ? 1 : 0);
1339 }
1340 i2++;
1341
1342 if (dst)
1343 retVal = bz2_putc ( ch2, dst );
1344
1345 UPDATE_CRC ( localCrc, (UChar)ch2 ){ localCrc = (localCrc << 8) ^ crc32Table[(localCrc >>
24) ^ ((unsigned char)(unsigned char)ch2)]; }
;
1346
1347 if (ch2 != chPrev) {
1348 count = 1;
1349 } else {
1350 count++;
1351 if (count >= 4) {
1352 Int32int j2;
1353 UCharunsigned char z;
1354 GET_SMALL(z);
1355 if (blockRandomised) {
1356 RAND_UPD_MASKif (rNToGo == 0) { rNToGo = rNums[rTPos]; rTPos++; if (rTPos ==
512) rTPos = 0; } rNToGo--;
;
1357 z ^= RAND_MASK((rNToGo == 1) ? 1 : 0);
1358 }
1359 for (j2 = 0; j2 < (Int32int)z; j2++) {
1360 if (dst) retVal = bz2_putc (ch2, dst);
1361 UPDATE_CRC ( localCrc, (UChar)ch2 ){ localCrc = (localCrc << 8) ^ crc32Table[(localCrc >>
24) ^ ((unsigned char)(unsigned char)ch2)]; }
;
1362 }
1363 i2++;
1364 count = 0;
1365 }
1366 }
1367 }
1368 }
1369
1370 setGlobalCRC ( localCrc );
1371 }
1372 /*-- end of the in-line run-length-decoder. --*/
1373}
1374#undef GET_SMALL
1375
1376
1377/*---------------------------------------------*/
1378
1379#define GET_FAST(cccc) \
1380 \
1381 cccc = ll8[tPos]; \
1382 tPos = tt[tPos];
1383
1384
1385
1386static void undoReversibleTransformation_fast ( void* dst )
1387{
1388 Int32int cftab[257];
1389 Int32int i, tPos;
1390 UCharunsigned char ch;
1391
1392 /*--
1393 We assume here that the global array unzftab will
1394 already be holding the frequency counts for
1395 ll8[0 .. last].
1396 --*/
1397
1398 /*-- Set up cftab to facilitate generation of T^(-1) --*/
1399 cftab[0] = 0;
1400 for (i = 1; i <= 256; i++) cftab[i] = unzftab[i-1];
1401 for (i = 1; i <= 256; i++) cftab[i] += cftab[i-1];
1402
1403 /*-- compute the T^(-1) vector --*/
1404 for (i = 0; i <= last; i++) {
1405 ch = (UCharunsigned char)ll8[i];
1406 tt[cftab[ch]] = i;
1407 cftab[ch]++;
1408 }
1409
1410 /*--
1411 We recreate the original by subscripting L through T^(-1).
1412 The run-length-decoder below requires characters incrementally,
1413 so tPos is set to a starting value, and is updated by
1414 the GET_FAST macro.
1415 --*/
1416 tPos = tt[origPtr];
1417
1418 /*-------------------------------------------------*/
1419 /*--
1420 This is pretty much a verbatim copy of the
1421 run-length decoder present in the distribution
1422 bzip-0.21; it has to be here to avoid creating
1423 block[] as an intermediary structure. As in 0.21,
1424 this code derives from some sent to me by
1425 Christian von Roques.
1426 --*/
1427 {
1428 IntNativeint retVal;
1429 Int32int i2, count, chPrev, ch2;
1430 UInt32unsigned int localCrc;
1431
1432 count = 0;
1433 i2 = 0;
1434 ch2 = 256; /*-- not a char and not EOF --*/
1435 localCrc = getGlobalCRC();
1436
1437 if (blockRandomised) {
1438 RAND_DECLSint rNToGo = 0; int rTPos = 0;;
1439 while ( i2 <= last ) {
1440 chPrev = ch2;
1441 GET_FAST(ch2);
1442 RAND_UPD_MASKif (rNToGo == 0) { rNToGo = rNums[rTPos]; rTPos++; if (rTPos ==
512) rTPos = 0; } rNToGo--;
;
1443 ch2 ^= (UInt32unsigned int)RAND_MASK((rNToGo == 1) ? 1 : 0);
1444 i2++;
1445
1446 retVal = bz2_putc ( ch2, dst );
1447 UPDATE_CRC ( localCrc, (UChar)ch2 ){ localCrc = (localCrc << 8) ^ crc32Table[(localCrc >>
24) ^ ((unsigned char)(unsigned char)ch2)]; }
;
1448
1449 if (ch2 != chPrev) {
1450 count = 1;
1451 } else {
1452 count++;
1453 if (count >= 4) {
1454 Int32int j2;
1455 UCharunsigned char z;
1456 GET_FAST(z);
1457 RAND_UPD_MASKif (rNToGo == 0) { rNToGo = rNums[rTPos]; rTPos++; if (rTPos ==
512) rTPos = 0; } rNToGo--;
;
1458 z ^= RAND_MASK((rNToGo == 1) ? 1 : 0);
1459 for (j2 = 0; j2 < (Int32int)z; j2++) {
1460 retVal = bz2_putc (ch2, dst);
1461 UPDATE_CRC ( localCrc, (UChar)ch2 ){ localCrc = (localCrc << 8) ^ crc32Table[(localCrc >>
24) ^ ((unsigned char)(unsigned char)ch2)]; }
;
1462 }
1463 i2++;
1464 count = 0;
1465 }
1466 }
1467 }
1468
1469 } else {
1470
1471 while ( i2 <= last ) {
1472 chPrev = ch2;
1473 GET_FAST(ch2);
1474 i2++;
1475
1476 retVal = bz2_putc ( ch2, dst );
Value stored to 'retVal' is never read
1477 UPDATE_CRC ( localCrc, (UChar)ch2 ){ localCrc = (localCrc << 8) ^ crc32Table[(localCrc >>
24) ^ ((unsigned char)(unsigned char)ch2)]; }
;
1478
1479 if (ch2 != chPrev) {
1480 count = 1;
1481 } else {
1482 count++;
1483 if (count >= 4) {
1484 Int32int j2;
1485 UCharunsigned char z;
1486 GET_FAST(z);
1487 for (j2 = 0; j2 < (Int32int)z; j2++) {
1488 retVal = bz2_putc (ch2, dst);
1489 UPDATE_CRC ( localCrc, (UChar)ch2 ){ localCrc = (localCrc << 8) ^ crc32Table[(localCrc >>
24) ^ ((unsigned char)(unsigned char)ch2)]; }
;
1490 }
1491 i2++;
1492 count = 0;
1493 }
1494 }
1495 }
1496
1497 } /*-- if (blockRandomised) --*/
1498
1499 setGlobalCRC ( localCrc );
1500 }
1501 /*-- end of the in-line run-length-decoder. --*/
1502}
1503#undef GET_FAST
1504
1505
1506
1507/*---------------------------------------------------*/
1508/*--- Processing of complete files and streams ---*/
1509/*---------------------------------------------------*/
1510
1511/*---------------------------------------------*/
1512
1513static Boolunsigned char uncompressStream ( void *zStream, void *stream )
1514{
1515 UCharunsigned char magic1, magic2, magic3, magic4;
1516 UCharunsigned char magic5, magic6;
1517 UInt32unsigned int storedBlockCRC, storedCombinedCRC;
1518 UInt32unsigned int computedBlockCRC, computedCombinedCRC;
1519 Int32int currBlockNo;
1520 IntNativeint retVal;
1521
1522 SET_BINARY_MODE(stream);
1523 SET_BINARY_MODE(zStream);
1524
1525 ERROR_IF_NOT_ZERO ( bz2_ferror(stream) ){ if ((bz2_ferror(stream)) != 0) ioError(); };
1526 ERROR_IF_NOT_ZERO ( bz2_ferror(zStream) ){ if ((bz2_ferror(zStream)) != 0) ioError(); };
1527
1528 bsSetStream ( zStream, False0 );
1529
1530 /*--
1531 A bad magic number is `recoverable from';
1532 return with False so the caller skips the file.
1533 --*/
1534 magic1 = bsGetUChar ();
1535 magic2 = bsGetUChar ();
1536 magic3 = bsGetUChar ();
1537 magic4 = bsGetUChar ();
1538 if (magic1 != 'B' ||
1539 magic2 != 'Z' ||
1540 magic3 != 'h' ||
1541 magic4 < '1' ||
1542 magic4 > '9') {
1543 bsFinishedWithStream();
1544 retVal = bz2_fclose ( stream );
1545 ERROR_IF_EOF ( retVal ){ if ((retVal) == (-1)) ioError(); };
1546 return False0;
1547 }
1548
1549 setDecompressStructureSizes ( magic4 - '0' );
1550 computedCombinedCRC = 0;
1551
1552 if (verbosity >= 2) fprintf ( stderr, "\n " );
1553 currBlockNo = 0;
1554
1555 while (True1) {
1556 magic1 = bsGetUChar ();
1557 magic2 = bsGetUChar ();
1558 magic3 = bsGetUChar ();
1559 magic4 = bsGetUChar ();
1560 magic5 = bsGetUChar ();
1561 magic6 = bsGetUChar ();
1562 if (magic1 == 0x17 && magic2 == 0x72 &&
1563 magic3 == 0x45 && magic4 == 0x38 &&
1564 magic5 == 0x50 && magic6 == 0x90) break;
1565
1566 if (magic1 != 0x31 || magic2 != 0x41 ||
1567 magic3 != 0x59 || magic4 != 0x26 ||
1568 magic5 != 0x53 || magic6 != 0x59) badBlockHeader();
1569
1570 storedBlockCRC = bsGetUInt32 ();
1571
1572 if (bsR(1) == 1)
1573 blockRandomised = True1; else
1574 blockRandomised = False0;
1575
1576 currBlockNo++;
1577 if (verbosity >= 2)
1578 fprintf ( stderr, "[%d: huff+mtf ", currBlockNo );
1579 getAndMoveToFrontDecode ();
1580 ERROR_IF_NOT_ZERO ( bz2_ferror(zStream) ){ if ((bz2_ferror(zStream)) != 0) ioError(); };
1581
1582 initialiseCRC();
1583 if (verbosity >= 2) fprintf ( stderr, "rt+rld" );
1584 if (smallMode)
1585 undoReversibleTransformation_small ( stream );
1586 else
1587 undoReversibleTransformation_fast ( stream );
1588
1589 ERROR_IF_NOT_ZERO ( bz2_ferror(stream) ){ if ((bz2_ferror(stream)) != 0) ioError(); };
1590
1591 computedBlockCRC = getFinalCRC();
1592 if (verbosity >= 3)
1593 fprintf ( stderr, " {0x%x, 0x%x}", storedBlockCRC, computedBlockCRC );
1594 if (verbosity >= 2) fprintf ( stderr, "] " );
1595
1596 /*-- A bad CRC is considered a fatal error. --*/
1597 if (storedBlockCRC != computedBlockCRC)
1598 crcError ( storedBlockCRC, computedBlockCRC );
1599
1600 computedCombinedCRC = (computedCombinedCRC << 1) | (computedCombinedCRC >> 31);
1601 computedCombinedCRC ^= computedBlockCRC;
1602 };
1603
1604 if (verbosity >= 2) fprintf ( stderr, "\n " );
1605
1606 storedCombinedCRC = bsGetUInt32 ();
1607 if (verbosity >= 2)
1608 fprintf ( stderr,
1609 "combined CRCs: stored = 0x%x, computed = 0x%x\n ",
1610 storedCombinedCRC, computedCombinedCRC );
1611 if (storedCombinedCRC != computedCombinedCRC)
1612 crcError ( storedCombinedCRC, computedCombinedCRC );
1613
1614
1615 bsFinishedWithStream ();
1616 ERROR_IF_NOT_ZERO ( bz2_ferror(zStream) ){ if ((bz2_ferror(zStream)) != 0) ioError(); };
1617 retVal = bz2_fclose ( zStream );
1618 ERROR_IF_EOF ( retVal ){ if ((retVal) == (-1)) ioError(); };
1619
1620 ERROR_IF_NOT_ZERO ( bz2_ferror(stream) ){ if ((bz2_ferror(stream)) != 0) ioError(); };
1621 retVal = bz2_fflush ( stream );
1622 ERROR_IF_NOT_ZERO ( retVal ){ if ((retVal) != 0) ioError(); };
1623 return True1;
1624}
1625
1626
1627#if 0
1628
1629#endif
1630/*---------------------------------------------------*/
1631/*--- Error [non-] handling grunge ---*/
1632/*---------------------------------------------------*/
1633
1634
1635
1636static void
1637myFree (void **p)
1638{
1639 free (*p);
1640 *p = NULL((void*)0);
1641}
1642
1643/*---------------------------------------------*/
1644/* Ugg... Orignal code doesn't free dynamic allocated memories. */
1645
1646static void cleanUpAndFail ( Int32int ec )
1647{
1648 myFree ((void **) &ll16);
1649 myFree ((void **) &ll4);
1650 myFree ((void **) &ll8);
1651 myFree ((void **) &tt);
1652
1653 (*unzip_error) (NULL((void*)0));
1654}
1655
1656
1657/*---------------------------------------------*/
1658
1659static void panic ( Charchar* s )
1660{
1661 cleanUpAndFail( 3 );
1662}
1663
1664
1665
1666/*---------------------------------------------*/
1667
1668static void crcError ( UInt32unsigned int crcStored, UInt32unsigned int crcComputed )
1669{
1670 cleanUpAndFail( 2 );
1671}
1672
1673
1674/*---------------------------------------------*/
1675
1676static void compressedStreamEOF ( void )
1677{
1678 cleanUpAndFail( 2 );
1679}
1680
1681
1682/*---------------------------------------------*/
1683
1684static void ioError ( )
1685{
1686 cleanUpAndFail( 1 );
1687}
1688
1689
1690/*---------------------------------------------*/
1691
1692static void blockOverrun ()
1693{
1694 cleanUpAndFail( 2 );
1695}
1696
1697
1698/*---------------------------------------------*/
1699
1700static void badBlockHeader ()
1701{
1702 cleanUpAndFail( 2 );
1703}
1704
1705
1706
1707/*---------------------------------------------*/
1708static void uncompressOutOfMemory ( Int32int draw, Int32int blockSize )
1709{
1710 cleanUpAndFail(1);
1711}
1712
1713
1714
1715/*-----------------------------------------------------------*/
1716/*--- end bzip2.c ---*/
1717/*-----------------------------------------------------------*/
1718
1719void
1720do_bunzip2 (void)
1721{
1722 Boolunsigned char ret;
1723
1724 /*-- Initialise --*/
1725 ll4 = NULL((void*)0);
1726 ll16 = NULL((void*)0);
1727 ll8 = NULL((void*)0);
1728 tt = NULL((void*)0);
1729#ifdef SMALL_BZIP2
1730 smallMode = True1;
1731#else
1732 smallMode = False0;
1733#endif
1734 verbosity = 0;
1735 blockSize100k = 0;
1736 bsStream = NULL((void*)0);
1737
1738 outcnt = 0;
1739 inptr = 0;
1740 insize = 0;
1741
1742 ret = uncompressStream ((void *)1, (void *)2); /* Arguments ignored. */
1743 if (ret != True1)
1744 cleanUpAndFail(1);
1745}