-
Notifications
You must be signed in to change notification settings - Fork 2
/
bzip2.c
4703 lines (3957 loc) · 131 KB
/
bzip2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#ifdef TIMING_OUTPUT
#include <time.h>
#include <sys/time.h>
#endif
#include <string.h>
void *MYmymemcpy(void *dest, const void *src, size_t n);
void *MYmymemset(void *s, int c, size_t n);
void* MYmymalloc(size_t);
void MYmyexit(int);
#define SPEC_CPU2000
// #define DEBUG_DUMP
/*-----------------------------------------------------------*/
/*--- A block-sorting, lossless compressor bzip2.c ---*/
/*-----------------------------------------------------------*/
/*--
This program is bzip2, a lossless, block-sorting data compressor,
version 0.1pl2, dated 29-Aug-1997.
Copyright (C) 1996, 1997 by Julian Seward.
Guildford, Surrey, UK
email: [email protected]
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
The GNU General Public License is contained in the file LICENSE.
This program is based on (at least) the work of:
Mike Burrows
David Wheeler
Peter Fenwick
Alistair Moffat
Radford Neal
Ian H. Witten
Robert Sedgewick
Jon L. Bentley
For more information on these sources, see the file ALGORITHMS.
--*/
/*----------------------------------------------------*/
/*--- IMPORTANT ---*/
/*----------------------------------------------------*/
/*--
WARNING:
This program (attempts to) compress data by performing several
non-trivial transformations on it. Unless you are 100% familiar
with *all* the algorithms contained herein, and with the
consequences of modifying them, you should NOT meddle with the
compression or decompression machinery. Incorrect changes can
and very likely *will* lead to disasterous loss of data.
DISCLAIMER:
I TAKE NO RESPONSIBILITY FOR ANY LOSS OF DATA ARISING FROM THE
USE OF THIS PROGRAM, HOWSOEVER CAUSED.
Every compression of a file implies an assumption that the
compressed file can be decompressed to reproduce the original.
Great efforts in design, coding and testing have been made to
ensure that this program works correctly. However, the
complexity of the algorithms, and, in particular, the presence
of various special cases in the code which occur with very low
but non-zero probability make it impossible to rule out the
possibility of bugs remaining in the program. DO NOT COMPRESS
ANY DATA WITH THIS PROGRAM UNLESS YOU ARE PREPARED TO ACCEPT THE
POSSIBILITY, HOWEVER SMALL, THAT THE DATA WILL NOT BE RECOVERABLE.
That is not to say this program is inherently unreliable.
Indeed, I very much hope the opposite is true. bzip2 has been
carefully constructed and extensively tested.
PATENTS:
To the best of my knowledge, bzip2 does not use any patented
algorithms. However, I do not have the resources available to
carry out a full patent search. Therefore I cannot give any
guarantee of the above statement.
--*/
/*----------------------------------------------------*/
/*--- and now for something much more pleasant :-) ---*/
/*----------------------------------------------------*/
/*---------------------------------------------*/
/*--
Place a 1 beside your platform, and 0 elsewhere.
--*/
/*--
Generic 32-bit Unix.
Also works on 64-bit Unix boxes.
--*/
#define BZ_UNIX 0
/*--
Win32, as seen by Jacob Navia's excellent
port of (Chris Fraser & David Hanson)'s excellent
lcc compiler.
--*/
#define BZ_LCCWIN32 0
/*---------------------------------------------*/
/*--
Some stuff for all platforms.
--*/
#include <stdio.h>
#include <stdlib.h>
#if DEBUG
#include <assert.h>
#endif
#include <string.h>
#include <signal.h>
#include <math.h>
#define ERROR_IF_EOF(i) { if ((i) == EOF) ioError(); }
#define ERROR_IF_NOT_ZERO(i) { if ((i) != 0) ioError(); }
#define ERROR_IF_MINUS_ONE(i) { if ((i) == (-1)) ioError(); }
/*---------------------------------------------*/
/*--
Platform-specific stuff.
--*/
#ifdef SPEC_CPU2000
#include <sys/types.h>
#define Int32 int
#define UInt32 unsigned int
#define Char char
#define UChar unsigned char
#define Int16 short
#define UInt16 unsigned short
/*--
You should try very hard to persuade your C compiler
to inline the bits marked INLINE. Otherwise bzip2 will
run rather slowly. gcc version 2.x is recommended.
--*/
#define INLINE /**/
#define NORETURN /**/
#define SET_BINARY_MODE(fd) /**/
#ifdef read
#undef read
#endif
#define read(a,b,c) spec_read(a,b,c)
#ifdef write
#undef write
#endif
#define write(a,b,c) spec_write(a,b,c)
#ifdef ferror
#undef ferror
#endif
#define ferror(x) 0
#ifdef fopen
#undef fopen
#endif
#define fopen(x) 0
#ifdef fflush
#undef fflush
#endif
#define fflush(x) 0
#ifdef fclose
#undef fclose
#endif
#define fclose(x) 0
#ifdef getc
#undef getc
#endif
#define getc(f) spec_getc((int)f)
#ifdef fgetc
#undef fgetc
#endif
#define fgetc(f) spec_getc((int)f)
#ifdef fputc
#undef fputc
#endif
#define fputc(c, f) spec_putc(c, (int)f)
#ifdef putc
#undef putc
#endif
#define putc(c, f) spec_putc(c, (int)f)
#ifdef ungetc
#undef ungetc
#endif
#define ungetc(c,f) spec_ungetc(c,(int)f)
#endif
#if BZ_UNIX
#include <sys/types.h>
#include <utime.h>
#include <unistd.h>
#include <malloc.h>
#include <sys/stat.h>
#include <sys/times.h>
#define Int32 int
#define UInt32 unsigned int
#define Char char
#define UChar unsigned char
#define Int16 short
#define UInt16 unsigned short
#define PATH_SEP '/'
#define MY_LSTAT lstat
#define MY_S_IFREG S_ISREG
#define MY_STAT stat
#define APPEND_FILESPEC(root, name) \
root=snocString((root), (name))
#define SET_BINARY_MODE(fd) /**/
/*--
You should try very hard to persuade your C compiler
to inline the bits marked INLINE. Otherwise bzip2 will
run rather slowly. gcc version 2.x is recommended.
--*/
#ifdef __GNUC__
#define INLINE inline
#define NORETURN __attribute__ ((noreturn))
#else
#define INLINE /**/
#define NORETURN /**/
#endif
#endif
#if BZ_LCCWIN32
#include <io.h>
#include <fcntl.h>
#include <sys\stat.h>
#define Int32 int
#define UInt32 unsigned int
#define Int16 short
#define UInt16 unsigned short
#define Char char
#define UChar unsigned char
#define INLINE /**/
#define NORETURN /**/
#define PATH_SEP '\\'
#define MY_LSTAT _stat
#define MY_STAT _stat
#define MY_S_IFREG(x) ((x) & _S_IFREG)
#if 0
/*-- lcc-win32 seems to expand wildcards itself --*/
#define APPEND_FILESPEC(root, spec) \
do { \
if ((spec)[0] == '-') { \
root = snocString((root), (spec)); \
} else { \
struct _finddata_t c_file; \
long hFile; \
hFile = _findfirst((spec), &c_file); \
if ( hFile == -1L ) { \
root = snocString ((root), (spec)); \
} else { \
int anInt = 0; \
while ( anInt == 0 ) { \
root = snocString((root), \
&c_file.name[0]); \
anInt = _findnext(hFile, &c_file); \
} \
} \
} \
} while ( 0 )
#else
#define APPEND_FILESPEC(root, name) \
root = snocString ((root), (name))
#endif
#define SET_BINARY_MODE(fd) \
do { \
int retVal = setmode ( fileno ( fd ), \
O_BINARY ); \
ERROR_IF_MINUS_ONE ( retVal ); \
} while ( 0 )
#endif
#ifdef SPEC
#ifndef IM_SPEC_ALREADY
#define read(a,b,c) spec_read(a,b,c)
#define write(a,b,c) spec_write(a,b,c)
#define ferror(x) 0
#define getc(f) spec_getc(f)
#define ungetc(c,f) spec_ungetc(c,f)
#endif
#endif
/*---------------------------------------------*/
/*--
Some more stuff for all platforms :-)
--*/
#define Bool unsigned char
#define True 1
#define False 0
/* Prototypes for stuff in bzip2.c */
Bool uncompressStream ( int zStream, int stream );
void compressStream ( int stream, int zStream );
void allocateCompressStructures ( void );
/* Prototypes for stuff in spec.c */
void spec_initbufs();
void spec_compress(int in, int out, int level);
void spec_uncompress(int in, int out, int level);
int spec_init ();
int spec_random_load (int fd);
int spec_load (int num, char *filename, int size);
int spec_read (int fd, unsigned char *buf, int size);
int spec_getc (int fd);
int spec_ungetc (unsigned char ch, int fd);
int spec_rewind(int fd);
int spec_reset(int fd);
int spec_write(int fd, unsigned char *buf, int size);
int spec_putc(unsigned char ch, int fd);
int debug_time();
/*--
IntNative is your platform's `native' int size.
Only here to avoid probs with 64-bit platforms.
--*/
#define IntNative int
// /*--
// change to 1, or compile with -DDEBUG=1 to debug
// --*/
// #ifndef DEBUG
// #define DEBUG 0
// #endif
/*---------------------------------------------------*/
/*--- ---*/
/*---------------------------------------------------*/
/*--
Implementation notes, July 1997
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Memory allocation
~~~~~~~~~~~~~~~~~
All large data structures are allocated on the C heap,
for better or for worse. That includes the various
arrays of pointers, striped words, bytes, frequency
tables and buffers for compression and decompression.
bzip2 can operate at various block-sizes, ranging from
100k to 900k in 100k steps, and it allocates only as
much as it needs to. When compressing, we know from the
command-line options what the block-size is going to be,
so all allocation can be done at start-up; if that
succeeds, there can be no further allocation problems.
Decompression is more complicated. Each compressed file
contains, in its header, a byte indicating the block
size used for compression. This means bzip2 potentially
needs to reallocate memory for each file it deals with,
which in turn opens the possibility for a memory allocation
failure part way through a run of files, by encountering
a file requiring a much larger block size than all the
ones preceding it.
The policy is to simply give up if a memory allocation
failure occurs. During decompression, it would be
possible to move on to subsequent files in the hope that
some might ask for a smaller block size, but the
complications for doing this seem more trouble than they
are worth.
Compressed file formats
~~~~~~~~~~~~~~~~~~~~~~~
[This is now entirely different from both 0.21, and from
any previous Huffman-coded variant of bzip.
See the associated file bzip2.txt for details.]
Error conditions
~~~~~~~~~~~~~~~~
Dealing with error conditions is the least satisfactory
aspect of bzip2. The policy is to try and leave the
filesystem in a consistent state, then quit, even if it
means not processing some of the files mentioned in the
command line. `A consistent state' means that a file
exists either in its compressed or uncompressed form,
but not both. This boils down to the rule `delete the
output file if an error condition occurs, leaving the
input intact'. Input files are only deleted when we can
be pretty sure the output file has been written and
closed successfully.
Errors are a dog because there's so many things to
deal with. The following can happen mid-file, and
require cleaning up.
internal `panics' -- indicating a bug
corrupted or inconsistent compressed file
can't allocate enough memory to decompress this file
I/O error reading/writing/opening/closing
signal catches -- Control-C, SIGTERM, SIGHUP.
Other conditions, primarily pertaining to file names,
can be checked in-between files, which makes dealing
with them easier.
--*/
/*---------------------------------------------------*/
/*--- Misc (file handling) data decls ---*/
/*---------------------------------------------------*/
UInt32 bytesIn, bytesOut;
#ifdef SPEC_CPU2000
int verbosity;
#else
Int32 verbosity;
#endif
Bool keepInputFiles, smallMode, testFailsExist;
UInt32 globalCrc;
Int32 numFileNames = 0, numFilesProcessed = 0;
/*-- source modes; F==file, I==stdin, O==stdout --*/
#define SM_I2O 1
#define SM_F2O 2
#define SM_F2F 3
/*-- operation modes --*/
#define OM_Z 1
#define OM_UNZ 2
#define OM_TEST 3
Int32 opMode;
Int32 srcMode;
Int32 longestFileName;
Char inName[1024];
Char outName[1024];
Char *progName;
Char progNameReally[1024];
#ifdef SPEC_CPU2000
int outputHandleJustInCase;
#else
FILE *outputHandleJustInCase;
#endif
void panic ( Char* ) NORETURN;
void ioError ( void ) NORETURN;
void compressOutOfMemory ( Int32, Int32 ) NORETURN;
void uncompressOutOfMemory ( Int32, Int32 ) NORETURN;
void blockOverrun ( void ) NORETURN;
void badBlockHeader ( void ) NORETURN;
void badBGLengths ( void ) NORETURN;
void crcError ( UInt32, UInt32 ) NORETURN;
void bitStreamEOF ( void ) NORETURN;
void cleanUpAndFail ( Int32 ) NORETURN;
void compressedStreamEOF ( void ) NORETURN;
void* myMalloc ( Int32 );
/*---------------------------------------------------*/
/*--- Data decls for the front end ---*/
/*---------------------------------------------------*/
/*--
The overshoot bytes allow us to avoid most of
the cost of pointer renormalisation during
comparison of rotations in sorting.
The figure of 20 is derived as follows:
qSort3 allows an overshoot of up to 10.
It then calls simpleSort, which calls
fullGtU, also with max overshoot 10.
fullGtU does up to 10 comparisons without
renormalising, giving 10+10 == 20.
--*/
#define NUM_OVERSHOOT_BYTES 20
/*--
These are the main data structures for
the Burrows-Wheeler transform.
--*/
/*--
Pointers to compression and decompression
structures. Set by
allocateCompressStructures and
setDecompressStructureSizes
The structures are always set to be suitable
for a block of size 100000 * blockSize100k.
--*/
UChar *block; /*-- compress --*/
UInt16 *quadrant; /*-- compress --*/
Int32 *zptr; /*-- compress --*/
UInt16 *szptr; /*-- overlays zptr ---*/
Int32 *ftab; /*-- compress --*/
UInt16 *ll16; /*-- small decompress --*/
UChar *ll4; /*-- small decompress --*/
Int32 *tt; /*-- fast decompress --*/
UChar *ll8; /*-- fast decompress --*/
/*--
freq table collected to save a pass over the data
during decompression.
--*/
Int32 unzftab[256];
/*--
index of the last char in the block, so
the block size == last + 1.
--*/
Int32 last;
/*--
index in zptr[] of original string after sorting.
--*/
Int32 origPtr;
/*--
always: in the range 0 .. 9.
The current block size is 100000 * this number.
--*/
#ifndef SPEC_CPU2000
Int32 blockSize100k;
#else
int blockSize100k;
#endif
/*--
Used when sorting. If too many long comparisons
happen, we stop sorting, randomise the block
slightly, and try again.
--*/
#ifndef SPEC_CPU2000
Int32 workFactor;
#else
int workFactor;
#endif
Int32 workDone;
Int32 workLimit;
Bool blockRandomised;
Bool firstAttempt;
Int32 nBlocksRandomised;
/*---------------------------------------------------*/
/*--- Data decls for the back end ---*/
/*---------------------------------------------------*/
#define MAX_ALPHA_SIZE 258
#define MAX_CODE_LEN 23
#define RUNA 0
#define RUNB 1
#define N_GROUPS 6
#define G_SIZE 50
#define N_ITERS 4
#define MAX_SELECTORS (2 + (900000 / G_SIZE))
Bool inUse[256];
Int32 nInUse;
UChar seqToUnseq[256];
UChar unseqToSeq[256];
UChar selector [MAX_SELECTORS];
UChar selectorMtf[MAX_SELECTORS];
Int32 nMTF;
Int32 mtfFreq[MAX_ALPHA_SIZE];
UChar len [N_GROUPS][MAX_ALPHA_SIZE];
/*-- decompress only --*/
Int32 limit [N_GROUPS][MAX_ALPHA_SIZE];
Int32 base [N_GROUPS][MAX_ALPHA_SIZE];
Int32 perm [N_GROUPS][MAX_ALPHA_SIZE];
Int32 minLens[N_GROUPS];
/*-- compress only --*/
Int32 code [N_GROUPS][MAX_ALPHA_SIZE];
Int32 rfreq[N_GROUPS][MAX_ALPHA_SIZE];
/*---------------------------------------------------*/
/*--- 32-bit CRC grunge ---*/
/*---------------------------------------------------*/
/*--
I think this is an implementation of the AUTODIN-II,
Ethernet & FDDI 32-bit CRC standard. Vaguely derived
from code by Rob Warnock, in Section 51 of the
comp.compression FAQ.
--*/
UInt32 crc32Table[256] = {
/*-- Ugly, innit? --*/
0x00000000UL, 0x04c11db7UL, 0x09823b6eUL, 0x0d4326d9UL,
0x130476dcUL, 0x17c56b6bUL, 0x1a864db2UL, 0x1e475005UL,
0x2608edb8UL, 0x22c9f00fUL, 0x2f8ad6d6UL, 0x2b4bcb61UL,
0x350c9b64UL, 0x31cd86d3UL, 0x3c8ea00aUL, 0x384fbdbdUL,
0x4c11db70UL, 0x48d0c6c7UL, 0x4593e01eUL, 0x4152fda9UL,
0x5f15adacUL, 0x5bd4b01bUL, 0x569796c2UL, 0x52568b75UL,
0x6a1936c8UL, 0x6ed82b7fUL, 0x639b0da6UL, 0x675a1011UL,
0x791d4014UL, 0x7ddc5da3UL, 0x709f7b7aUL, 0x745e66cdUL,
0x9823b6e0UL, 0x9ce2ab57UL, 0x91a18d8eUL, 0x95609039UL,
0x8b27c03cUL, 0x8fe6dd8bUL, 0x82a5fb52UL, 0x8664e6e5UL,
0xbe2b5b58UL, 0xbaea46efUL, 0xb7a96036UL, 0xb3687d81UL,
0xad2f2d84UL, 0xa9ee3033UL, 0xa4ad16eaUL, 0xa06c0b5dUL,
0xd4326d90UL, 0xd0f37027UL, 0xddb056feUL, 0xd9714b49UL,
0xc7361b4cUL, 0xc3f706fbUL, 0xceb42022UL, 0xca753d95UL,
0xf23a8028UL, 0xf6fb9d9fUL, 0xfbb8bb46UL, 0xff79a6f1UL,
0xe13ef6f4UL, 0xe5ffeb43UL, 0xe8bccd9aUL, 0xec7dd02dUL,
0x34867077UL, 0x30476dc0UL, 0x3d044b19UL, 0x39c556aeUL,
0x278206abUL, 0x23431b1cUL, 0x2e003dc5UL, 0x2ac12072UL,
0x128e9dcfUL, 0x164f8078UL, 0x1b0ca6a1UL, 0x1fcdbb16UL,
0x018aeb13UL, 0x054bf6a4UL, 0x0808d07dUL, 0x0cc9cdcaUL,
0x7897ab07UL, 0x7c56b6b0UL, 0x71159069UL, 0x75d48ddeUL,
0x6b93dddbUL, 0x6f52c06cUL, 0x6211e6b5UL, 0x66d0fb02UL,
0x5e9f46bfUL, 0x5a5e5b08UL, 0x571d7dd1UL, 0x53dc6066UL,
0x4d9b3063UL, 0x495a2dd4UL, 0x44190b0dUL, 0x40d816baUL,
0xaca5c697UL, 0xa864db20UL, 0xa527fdf9UL, 0xa1e6e04eUL,
0xbfa1b04bUL, 0xbb60adfcUL, 0xb6238b25UL, 0xb2e29692UL,
0x8aad2b2fUL, 0x8e6c3698UL, 0x832f1041UL, 0x87ee0df6UL,
0x99a95df3UL, 0x9d684044UL, 0x902b669dUL, 0x94ea7b2aUL,
0xe0b41de7UL, 0xe4750050UL, 0xe9362689UL, 0xedf73b3eUL,
0xf3b06b3bUL, 0xf771768cUL, 0xfa325055UL, 0xfef34de2UL,
0xc6bcf05fUL, 0xc27dede8UL, 0xcf3ecb31UL, 0xcbffd686UL,
0xd5b88683UL, 0xd1799b34UL, 0xdc3abdedUL, 0xd8fba05aUL,
0x690ce0eeUL, 0x6dcdfd59UL, 0x608edb80UL, 0x644fc637UL,
0x7a089632UL, 0x7ec98b85UL, 0x738aad5cUL, 0x774bb0ebUL,
0x4f040d56UL, 0x4bc510e1UL, 0x46863638UL, 0x42472b8fUL,
0x5c007b8aUL, 0x58c1663dUL, 0x558240e4UL, 0x51435d53UL,
0x251d3b9eUL, 0x21dc2629UL, 0x2c9f00f0UL, 0x285e1d47UL,
0x36194d42UL, 0x32d850f5UL, 0x3f9b762cUL, 0x3b5a6b9bUL,
0x0315d626UL, 0x07d4cb91UL, 0x0a97ed48UL, 0x0e56f0ffUL,
0x1011a0faUL, 0x14d0bd4dUL, 0x19939b94UL, 0x1d528623UL,
0xf12f560eUL, 0xf5ee4bb9UL, 0xf8ad6d60UL, 0xfc6c70d7UL,
0xe22b20d2UL, 0xe6ea3d65UL, 0xeba91bbcUL, 0xef68060bUL,
0xd727bbb6UL, 0xd3e6a601UL, 0xdea580d8UL, 0xda649d6fUL,
0xc423cd6aUL, 0xc0e2d0ddUL, 0xcda1f604UL, 0xc960ebb3UL,
0xbd3e8d7eUL, 0xb9ff90c9UL, 0xb4bcb610UL, 0xb07daba7UL,
0xae3afba2UL, 0xaafbe615UL, 0xa7b8c0ccUL, 0xa379dd7bUL,
0x9b3660c6UL, 0x9ff77d71UL, 0x92b45ba8UL, 0x9675461fUL,
0x8832161aUL, 0x8cf30badUL, 0x81b02d74UL, 0x857130c3UL,
0x5d8a9099UL, 0x594b8d2eUL, 0x5408abf7UL, 0x50c9b640UL,
0x4e8ee645UL, 0x4a4ffbf2UL, 0x470cdd2bUL, 0x43cdc09cUL,
0x7b827d21UL, 0x7f436096UL, 0x7200464fUL, 0x76c15bf8UL,
0x68860bfdUL, 0x6c47164aUL, 0x61043093UL, 0x65c52d24UL,
0x119b4be9UL, 0x155a565eUL, 0x18197087UL, 0x1cd86d30UL,
0x029f3d35UL, 0x065e2082UL, 0x0b1d065bUL, 0x0fdc1becUL,
0x3793a651UL, 0x3352bbe6UL, 0x3e119d3fUL, 0x3ad08088UL,
0x2497d08dUL, 0x2056cd3aUL, 0x2d15ebe3UL, 0x29d4f654UL,
0xc5a92679UL, 0xc1683bceUL, 0xcc2b1d17UL, 0xc8ea00a0UL,
0xd6ad50a5UL, 0xd26c4d12UL, 0xdf2f6bcbUL, 0xdbee767cUL,
0xe3a1cbc1UL, 0xe760d676UL, 0xea23f0afUL, 0xeee2ed18UL,
0xf0a5bd1dUL, 0xf464a0aaUL, 0xf9278673UL, 0xfde69bc4UL,
0x89b8fd09UL, 0x8d79e0beUL, 0x803ac667UL, 0x84fbdbd0UL,
0x9abc8bd5UL, 0x9e7d9662UL, 0x933eb0bbUL, 0x97ffad0cUL,
0xafb010b1UL, 0xab710d06UL, 0xa6322bdfUL, 0xa2f33668UL,
0xbcb4666dUL, 0xb8757bdaUL, 0xb5365d03UL, 0xb1f740b4UL
};
/*---------------------------------------------*/
void initialiseCRC ( void )
{
globalCrc = 0xffffffffUL;
}
/*---------------------------------------------*/
UInt32 getFinalCRC ( void )
{
return ~globalCrc;
}
/*---------------------------------------------*/
UInt32 getGlobalCRC ( void )
{
return globalCrc;
}
/*---------------------------------------------*/
void setGlobalCRC ( UInt32 newCrc )
{
globalCrc = newCrc;
}
/*---------------------------------------------*/
#define UPDATE_CRC(crcVar,cha) \
{ \
crcVar = (crcVar << 8) ^ \
crc32Table[(crcVar >> 24) ^ \
((UChar)cha)]; \
}
/*---------------------------------------------------*/
/*--- Bit stream I/O ---*/
/*---------------------------------------------------*/
UInt32 bsBuff;
Int32 bsLive;
#if defined(SPEC_CPU2000)
int bsStream;
#else
FILE* bsStream;
#endif
Bool bsWriting;
/*---------------------------------------------*/
#ifdef SPEC_CPU2000
void bsSetStream ( int f, Bool wr )
#else
void bsSetStream ( FILE* f, Bool wr )
#endif
{
#if !defined(SPEC_CPU2000)
if (bsStream != NULL) panic ( "bsSetStream" );
#endif
bsStream = f;
bsLive = 0;
bsBuff = 0;
bytesOut = 0;
bytesIn = 0;
bsWriting = wr;
}
/*---------------------------------------------*/
void bsFinishedWithStream ( void )
{
if (bsWriting)
while (bsLive > 0) {
fputc ( (UChar)(bsBuff >> 24), bsStream );
bsBuff <<= 8;
bsLive -= 8;
bytesOut++;
}
#ifdef SPEC_CPU2000
/* This is just to quiet warnings */
bsStream = 0;
#else
bsStream = NULL;
#endif
}
/*---------------------------------------------*/
#define bsNEEDR(nz) \
{ \
while (bsLive < nz) { \
Int32 zzi = fgetc ( bsStream ); \
if (zzi == EOF) compressedStreamEOF(); \
bsBuff = (bsBuff << 8) | (zzi & 0xffL); \
bsLive += 8; \
} \
}
/*---------------------------------------------*/
#define bsNEEDW(nz) \
{ \
while (bsLive >= 8) { \
fputc ( (UChar)(bsBuff >> 24), \
bsStream ); \
bsBuff <<= 8; \
bsLive -= 8; \
bytesOut++; \
} \
}
/*---------------------------------------------*/
#define bsR1(vz) \
{ \
bsNEEDR(1); \
vz = (bsBuff >> (bsLive-1)) & 1; \
bsLive--; \
}
/*---------------------------------------------*/
INLINE UInt32 bsR ( Int32 n )
{
UInt32 v;
bsNEEDR ( n );
v = (bsBuff >> (bsLive-n)) & ((1 << n)-1);
bsLive -= n;
return v;
}
/*---------------------------------------------*/
INLINE void bsW ( Int32 n, UInt32 v )
{
bsNEEDW ( n );
bsBuff |= (v << (32 - bsLive - n));
bsLive += n;
}
/*---------------------------------------------*/
UChar bsGetUChar ( void )
{
return (UChar)bsR(8);
}
/*---------------------------------------------*/
void bsPutUChar ( UChar c )
{
bsW(8, (UInt32)c );
}
/*---------------------------------------------*/
Int32 bsGetUInt32 ( void )
{
UInt32 u;
u = 0;
u = (u << 8) | bsR(8);
u = (u << 8) | bsR(8);
u = (u << 8) | bsR(8);
u = (u << 8) | bsR(8);
return u;
}
/*---------------------------------------------*/
UInt32 bsGetIntVS ( UInt32 numBits )
{
return (UInt32)bsR(numBits);
}
/*---------------------------------------------*/
UInt32 bsGetInt32 ( void )
{
return (Int32)bsGetUInt32();
}
/*---------------------------------------------*/
void bsPutUInt32 ( UInt32 u )
{
bsW ( 8, (u >> 24) & 0xffL );
bsW ( 8, (u >> 16) & 0xffL );
bsW ( 8, (u >> 8) & 0xffL );
bsW ( 8, u & 0xffL );
}
/*---------------------------------------------*/
void bsPutInt32 ( Int32 c )
{
bsPutUInt32 ( (UInt32)c );
}
/*---------------------------------------------*/
void bsPutIntVS ( Int32 numBits, UInt32 c )
{
bsW ( numBits, c );
}
/*---------------------------------------------------*/
/*--- Huffman coding low-level stuff ---*/
/*---------------------------------------------------*/
#define WEIGHTOF(zz0) ((zz0) & 0xffffff00)
#define DEPTHOF(zz1) ((zz1) & 0x000000ff)
#define MYMAX(zz2,zz3) ((zz2) > (zz3) ? (zz2) : (zz3))
#define ADDWEIGHTS(zw1,zw2) \
(WEIGHTOF(zw1)+WEIGHTOF(zw2)) | \
(1 + MYMAX(DEPTHOF(zw1),DEPTHOF(zw2)))
#define UPHEAP(z) \
{ \
Int32 zz, tmp; \
zz = z; tmp = heap[zz]; \
while (weight[tmp] < weight[heap[zz >> 1]]) { \
heap[zz] = heap[zz >> 1]; \
zz >>= 1; \
} \
heap[zz] = tmp; \
}
#define DOWNHEAP(z) \
{ \
Int32 zz, yy, tmp; \
zz = z; tmp = heap[zz]; \
while (True) { \
yy = zz << 1; \
if (yy > nHeap) break; \
if (yy < nHeap && \
weight[heap[yy+1]] < weight[heap[yy]]) \
yy++; \
if (weight[tmp] < weight[heap[yy]]) break; \
heap[zz] = heap[yy]; \
zz = yy; \
} \
heap[zz] = tmp; \
}
/*---------------------------------------------*/
// 3 local arrays
void hbMakeCodeLengths ( UChar *len,
Int32 *freq,
Int32 alphaSize,
Int32 maxLen )
{
/*--
Nodes and heap entries run from 1. Entry 0
for both the heap and nodes is a sentinel.
--*/
Int32 nNodes, nHeap, n1, n2, i, j, k;
Bool tooLong;
Int32 heap [ MAX_ALPHA_SIZE + 2 ];
Int32 weight [ MAX_ALPHA_SIZE * 2 ];
Int32 parent [ MAX_ALPHA_SIZE * 2 ];
for (i = 0; i < alphaSize; i++)
weight[i+1] = (freq[i] == 0 ? 1 : freq[i]) << 8;
while (True) {