aboutsummaryrefslogtreecommitdiff
path: root/archival/gzip.c
diff options
context:
space:
mode:
authorGlenn L McGrath <bug1@ihug.co.nz>2002-08-22 13:21:26 +0000
committerGlenn L McGrath <bug1@ihug.co.nz>2002-08-22 13:21:26 +0000
commitd827e8b665d808aab809e3b4a89e539f1c1d94f4 (patch)
tree8605148a95b6c130cb382e29306f8008b81afc2a /archival/gzip.c
parentb37367aa779d528df43519fac9e1954e2dc170d4 (diff)
downloadbusybox-w32-d827e8b665d808aab809e3b4a89e539f1c1d94f4.tar.gz
busybox-w32-d827e8b665d808aab809e3b4a89e539f1c1d94f4.tar.bz2
busybox-w32-d827e8b665d808aab809e3b4a89e539f1c1d94f4.zip
Run through indent
Diffstat (limited to 'archival/gzip.c')
-rw-r--r--archival/gzip.c479
1 files changed, 238 insertions, 241 deletions
diff --git a/archival/gzip.c b/archival/gzip.c
index 44b5fe386..60ff1f311 100644
--- a/archival/gzip.c
+++ b/archival/gzip.c
@@ -71,7 +71,7 @@ typedef unsigned long ulg;
71#define STORED 0 71#define STORED 0
72/* methods 4 to 7 reserved */ 72/* methods 4 to 7 reserved */
73#define DEFLATED 8 73#define DEFLATED 8
74static int method; /* compression method */ 74static int method; /* compression method */
75 75
76/* To save memory for 16 bit systems, some arrays are overlaid between 76/* To save memory for 16 bit systems, some arrays are overlaid between
77 * the various modules: 77 * the various modules:
@@ -88,7 +88,7 @@ static int method; /* compression method */
88# define INBUFSIZ 0x8000 /* input buffer size */ 88# define INBUFSIZ 0x8000 /* input buffer size */
89# endif 89# endif
90#endif 90#endif
91#define INBUF_EXTRA 64 /* required by unlzw() */ 91#define INBUF_EXTRA 64 /* required by unlzw() */
92 92
93#ifndef OUTBUFSIZ 93#ifndef OUTBUFSIZ
94# ifdef SMALL_MEM 94# ifdef SMALL_MEM
@@ -97,7 +97,7 @@ static int method; /* compression method */
97# define OUTBUFSIZ 16384 /* output buffer size */ 97# define OUTBUFSIZ 16384 /* output buffer size */
98# endif 98# endif
99#endif 99#endif
100#define OUTBUF_EXTRA 2048 /* required by unlzw() */ 100#define OUTBUF_EXTRA 2048 /* required by unlzw() */
101 101
102#ifndef DIST_BUFSIZE 102#ifndef DIST_BUFSIZE
103# ifdef SMALL_MEM 103# ifdef SMALL_MEM
@@ -120,17 +120,17 @@ static int method; /* compression method */
120#endif 120#endif
121 121
122#define tab_suffix window 122#define tab_suffix window
123#define tab_prefix prev /* hash link (see deflate.c) */ 123#define tab_prefix prev /* hash link (see deflate.c) */
124#define head (prev+WSIZE) /* hash head (see deflate.c) */ 124#define head (prev+WSIZE) /* hash head (see deflate.c) */
125 125
126static long bytes_in; /* number of input bytes */ 126static long bytes_in; /* number of input bytes */
127 127
128#define isize bytes_in 128#define isize bytes_in
129/* for compatibility with old zip sources (to be cleaned) */ 129/* for compatibility with old zip sources (to be cleaned) */
130 130
131typedef int file_t; /* Do not use stdio */ 131typedef int file_t; /* Do not use stdio */
132 132
133#define NO_FILE (-1) /* in memory compression */ 133#define NO_FILE (-1) /* in memory compression */
134 134
135 135
136#define PACK_MAGIC "\037\036" /* Magic header for packed files */ 136#define PACK_MAGIC "\037\036" /* Magic header for packed files */
@@ -140,12 +140,12 @@ typedef int file_t; /* Do not use stdio */
140#define PKZIP_MAGIC "\120\113\003\004" /* Magic header for pkzip files */ 140#define PKZIP_MAGIC "\120\113\003\004" /* Magic header for pkzip files */
141 141
142/* gzip flag byte */ 142/* gzip flag byte */
143#define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */ 143#define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
144#define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */ 144#define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
145#define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */ 145#define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
146#define ORIG_NAME 0x08 /* bit 3 set: original file name present */ 146#define ORIG_NAME 0x08 /* bit 3 set: original file name present */
147#define COMMENT 0x10 /* bit 4 set: file comment present */ 147#define COMMENT 0x10 /* bit 4 set: file comment present */
148#define RESERVED 0xC0 /* bit 6,7: reserved */ 148#define RESERVED 0xC0 /* bit 6,7: reserved */
149 149
150/* internal file attribute */ 150/* internal file attribute */
151#define UNKNOWN 0xffff 151#define UNKNOWN 0xffff
@@ -153,7 +153,7 @@ typedef int file_t; /* Do not use stdio */
153#define ASCII 1 153#define ASCII 1
154 154
155#ifndef WSIZE 155#ifndef WSIZE
156# define WSIZE 0x8000 /* window size--must be a power of two, and */ 156# define WSIZE 0x8000 /* window size--must be a power of two, and */
157#endif /* at least 32K for zip's deflate method */ 157#endif /* at least 32K for zip's deflate method */
158 158
159#define MIN_MATCH 3 159#define MIN_MATCH 3
@@ -183,8 +183,8 @@ typedef int file_t; /* Do not use stdio */
183} 183}
184#endif 184#endif
185 185
186#define seekable() 0 /* force sequential output */ 186#define seekable() 0 /* force sequential output */
187#define translate_eol 0 /* no option -a yet */ 187#define translate_eol 0 /* no option -a yet */
188 188
189/* Diagnostic functions */ 189/* Diagnostic functions */
190#ifdef DEBUG 190#ifdef DEBUG
@@ -212,31 +212,31 @@ typedef int file_t; /* Do not use stdio */
212 212
213 213
214 /* from zip.c: */ 214 /* from zip.c: */
215static int zip (int in, int out); 215static int zip(int in, int out);
216static int file_read (char *buf, unsigned size); 216static int file_read(char *buf, unsigned size);
217 217
218 /* from gzip.c */ 218 /* from gzip.c */
219static RETSIGTYPE abort_gzip (void); 219static RETSIGTYPE abort_gzip(void);
220 220
221 /* from deflate.c */ 221 /* from deflate.c */
222static void lm_init (ush * flags); 222static void lm_init(ush * flags);
223static ulg deflate (void); 223static ulg deflate(void);
224 224
225 /* from trees.c */ 225 /* from trees.c */
226static void ct_init (ush * attr, int *methodp); 226static void ct_init(ush * attr, int *methodp);
227static int ct_tally (int dist, int lc); 227static int ct_tally(int dist, int lc);
228static ulg flush_block (char *buf, ulg stored_len, int eof); 228static ulg flush_block(char *buf, ulg stored_len, int eof);
229 229
230 /* from bits.c */ 230 /* from bits.c */
231static void bi_init (file_t zipfile); 231static void bi_init(file_t zipfile);
232static void send_bits (int value, int length); 232static void send_bits(int value, int length);
233static unsigned bi_reverse (unsigned value, int length); 233static unsigned bi_reverse(unsigned value, int length);
234static void bi_windup (void); 234static void bi_windup(void);
235static void copy_block (char *buf, unsigned len, int header); 235static void copy_block(char *buf, unsigned len, int header);
236static int (*read_buf) (char *buf, unsigned size); 236static int (*read_buf) (char *buf, unsigned size);
237 237
238 /* from util.c: */ 238 /* from util.c: */
239static void flush_outbuf (void); 239static void flush_outbuf(void);
240 240
241/* lzw.h -- define the lzw functions. 241/* lzw.h -- define the lzw functions.
242 * Copyright (C) 1992-1993 Jean-loup Gailly. 242 * Copyright (C) 1992-1993 Jean-loup Gailly.
@@ -251,9 +251,9 @@ static void flush_outbuf (void);
251#ifndef BITS 251#ifndef BITS
252# define BITS 16 252# define BITS 16
253#endif 253#endif
254#define INIT_BITS 9 /* Initial number of bits per code */ 254#define INIT_BITS 9 /* Initial number of bits per code */
255 255
256#define BIT_MASK 0x1f /* Mask for 'number of compression bits' */ 256#define BIT_MASK 0x1f /* Mask for 'number of compression bits' */
257/* Mask 0x20 is reserved to mean a fourth header byte, and 0x40 is free. 257/* Mask 0x20 is reserved to mean a fourth header byte, and 0x40 is free.
258 * It's a pity that old uncompress does not check bit 0x20. That makes 258 * It's a pity that old uncompress does not check bit 0x20. That makes
259 * extension of the format actually undesirable because old compress 259 * extension of the format actually undesirable because old compress
@@ -277,7 +277,7 @@ static void flush_outbuf (void);
277 /* Common defaults */ 277 /* Common defaults */
278 278
279#ifndef OS_CODE 279#ifndef OS_CODE
280# define OS_CODE 0x03 /* assume Unix */ 280# define OS_CODE 0x03 /* assume Unix */
281#endif 281#endif
282 282
283#ifndef PATH_SEP 283#ifndef PATH_SEP
@@ -308,31 +308,31 @@ DECLARE(ush, tab_prefix, 1L << BITS);
308 308
309static int crc_table_empty = 1; 309static int crc_table_empty = 1;
310 310
311static int foreground; /* set if program run in foreground */ 311static int foreground; /* set if program run in foreground */
312static int method = DEFLATED; /* compression method */ 312static int method = DEFLATED; /* compression method */
313static int exit_code = OK; /* program exit code */ 313static int exit_code = OK; /* program exit code */
314static int part_nb; /* number of parts in .gz file */ 314static int part_nb; /* number of parts in .gz file */
315static long time_stamp; /* original time stamp (modification time) */ 315static long time_stamp; /* original time stamp (modification time) */
316static long ifile_size; /* input file size, -1 for devices (debug only) */ 316static long ifile_size; /* input file size, -1 for devices (debug only) */
317static char z_suffix[MAX_SUFFIX + 1]; /* default suffix (can be set with --suffix) */ 317static char z_suffix[MAX_SUFFIX + 1]; /* default suffix (can be set with --suffix) */
318static int z_len; /* strlen(z_suffix) */ 318static int z_len; /* strlen(z_suffix) */
319 319
320static int ifd; /* input file descriptor */ 320static int ifd; /* input file descriptor */
321static int ofd; /* output file descriptor */ 321static int ofd; /* output file descriptor */
322static unsigned insize; /* valid bytes in inbuf */ 322static unsigned insize; /* valid bytes in inbuf */
323static unsigned outcnt; /* bytes in output buffer */ 323static unsigned outcnt; /* bytes in output buffer */
324 324
325 325
326/* Output a 16 bit value, lsb first */ 326/* Output a 16 bit value, lsb first */
327static void put_short(ush w) 327static void put_short(ush w)
328{ 328{
329 if (outcnt < OUTBUFSIZ-2) { 329 if (outcnt < OUTBUFSIZ - 2) {
330 outbuf[outcnt++] = (uch) ((w) & 0xff); 330 outbuf[outcnt++] = (uch) ((w) & 0xff);
331 outbuf[outcnt++] = (uch) ((ush)(w) >> 8); 331 outbuf[outcnt++] = (uch) ((ush) (w) >> 8);
332 } else { 332 } else {
333 put_byte((uch)((w) & 0xff)); 333 put_byte((uch) ((w) & 0xff));
334 put_byte((uch)((ush)(w) >> 8)); 334 put_byte((uch) ((ush) (w) >> 8));
335 } 335 }
336} 336}
337 337
338/* ======================================================================== 338/* ========================================================================
@@ -382,27 +382,28 @@ static void write_buf(int fd, void *buf, unsigned cnt)
382 * pointer, then initialize the crc shift register contents instead. 382 * pointer, then initialize the crc shift register contents instead.
383 * Return the current crc in either case. 383 * Return the current crc in either case.
384 */ 384 */
385static ulg updcrc(uch *s, unsigned n) 385static ulg updcrc(uch * s, unsigned n)
386{ 386{
387 static ulg crc = (ulg) 0xffffffffL; /* shift register contents */ 387 static ulg crc = (ulg) 0xffffffffL; /* shift register contents */
388 register ulg c; /* temporary variable */ 388 register ulg c; /* temporary variable */
389 static unsigned long crc_32_tab[256]; 389 static unsigned long crc_32_tab[256];
390
390 if (crc_table_empty) { 391 if (crc_table_empty) {
391 unsigned long csr; /* crc shift register */ 392 unsigned long csr; /* crc shift register */
392 const unsigned long e = 0xedb88320L; /* polynomial exclusive-or pattern */ 393 const unsigned long e = 0xedb88320L; /* polynomial exclusive-or pattern */
393 int i; /* counter for all possible eight bit values */ 394 int i; /* counter for all possible eight bit values */
394 int k; /* byte being shifted into crc apparatus */ 395 int k; /* byte being shifted into crc apparatus */
395 396
396 /* Compute table of CRC's. */ 397 /* Compute table of CRC's. */
397 crc_32_tab[0] = 0x00000000L; 398 crc_32_tab[0] = 0x00000000L;
398 for (i = 1; i < 256; i++) { 399 for (i = 1; i < 256; i++) {
399 csr = i; 400 csr = i;
400 /* The idea to initialize the register with the byte instead of 401 /* The idea to initialize the register with the byte instead of
401 * zero was stolen from Haruhiko Okumura's ar002 402 * zero was stolen from Haruhiko Okumura's ar002
402 */ 403 */
403 for (k = 8; k; k--) 404 for (k = 8; k; k--)
404 csr = csr & 1 ? (csr >> 1) ^ e : csr >> 1; 405 csr = csr & 1 ? (csr >> 1) ^ e : csr >> 1;
405 crc_32_tab[i]=csr; 406 crc_32_tab[i] = csr;
406 } 407 }
407 } 408 }
408 409
@@ -416,7 +417,7 @@ static ulg updcrc(uch *s, unsigned n)
416 } while (--n); 417 } while (--n);
417 } 418 }
418 crc = c; 419 crc = c;
419 return c ^ 0xffffffffL; /* (instead of ~c for 64-bit machines) */ 420 return c ^ 0xffffffffL; /* (instead of ~c for 64-bit machines) */
420} 421}
421 422
422/* bits.c -- output variable-length bit strings 423/* bits.c -- output variable-length bit strings
@@ -476,7 +477,7 @@ static ulg updcrc(uch *s, unsigned n)
476 * Local data used by the "bit string" routines. 477 * Local data used by the "bit string" routines.
477 */ 478 */
478 479
479static file_t zfile; /* output gzip file */ 480static file_t zfile; /* output gzip file */
480 481
481static unsigned short bi_buf; 482static unsigned short bi_buf;
482 483
@@ -494,7 +495,7 @@ static int bi_valid;
494/* Current input function. Set to mem_read for in-memory compression */ 495/* Current input function. Set to mem_read for in-memory compression */
495 496
496#ifdef DEBUG 497#ifdef DEBUG
497ulg bits_sent; /* bit length of the compressed data */ 498ulg bits_sent; /* bit length of the compressed data */
498#endif 499#endif
499 500
500/* =========================================================================== 501/* ===========================================================================
@@ -582,7 +583,7 @@ static void bi_windup()
582 */ 583 */
583static void copy_block(char *buf, unsigned len, int header) 584static void copy_block(char *buf, unsigned len, int header)
584{ 585{
585 bi_windup(); /* align on byte boundary */ 586 bi_windup(); /* align on byte boundary */
586 587
587 if (header) { 588 if (header) {
588 put_short((ush) len); 589 put_short((ush) len);
@@ -676,7 +677,7 @@ static void copy_block(char *buf, unsigned len, int header)
676 */ 677 */
677 678
678#ifdef SMALL_MEM 679#ifdef SMALL_MEM
679# define HASH_BITS 13 /* Number of bits used to hash strings */ 680# define HASH_BITS 13 /* Number of bits used to hash strings */
680#endif 681#endif
681#ifdef MEDIUM_MEM 682#ifdef MEDIUM_MEM
682# define HASH_BITS 14 683# define HASH_BITS 14
@@ -750,7 +751,7 @@ static long block_start;
750 * negative when the window is moved backwards. 751 * negative when the window is moved backwards.
751 */ 752 */
752 753
753static unsigned ins_h; /* hash index of string to be inserted */ 754static unsigned ins_h; /* hash index of string to be inserted */
754 755
755#define H_SHIFT ((HASH_BITS+MIN_MATCH-1)/MIN_MATCH) 756#define H_SHIFT ((HASH_BITS+MIN_MATCH-1)/MIN_MATCH)
756/* Number of bits by which ins_h and del_h must be shifted at each 757/* Number of bits by which ins_h and del_h must be shifted at each
@@ -765,18 +766,18 @@ static unsigned int prev_length;
765 * are discarded. This is used in the lazy match evaluation. 766 * are discarded. This is used in the lazy match evaluation.
766 */ 767 */
767 768
768static unsigned strstart; /* start of string to insert */ 769static unsigned strstart; /* start of string to insert */
769static unsigned match_start; /* start of matching string */ 770static unsigned match_start; /* start of matching string */
770static int eofile; /* flag set at end of input file */ 771static int eofile; /* flag set at end of input file */
771static unsigned lookahead; /* number of valid bytes ahead in window */ 772static unsigned lookahead; /* number of valid bytes ahead in window */
772 773
773static const unsigned max_chain_length=4096; 774static const unsigned max_chain_length = 4096;
774 775
775/* To speed up deflation, hash chains are never searched beyond this length. 776/* To speed up deflation, hash chains are never searched beyond this length.
776 * A higher limit improves compression ratio but degrades the speed. 777 * A higher limit improves compression ratio but degrades the speed.
777 */ 778 */
778 779
779static const unsigned int max_lazy_match=258; 780static const unsigned int max_lazy_match = 258;
780 781
781/* Attempt to find a better match only when the current match is strictly 782/* Attempt to find a better match only when the current match is strictly
782 * smaller than this value. This mechanism is used only for compression 783 * smaller than this value. This mechanism is used only for compression
@@ -788,7 +789,7 @@ static const unsigned int max_lazy_match=258;
788 * max_insert_length is used only for compression levels <= 3. 789 * max_insert_length is used only for compression levels <= 3.
789 */ 790 */
790 791
791static const unsigned good_match=32; 792static const unsigned good_match = 32;
792 793
793/* Use a faster search when the previous match is longer than this */ 794/* Use a faster search when the previous match is longer than this */
794 795
@@ -799,7 +800,7 @@ static const unsigned good_match=32;
799 * found for specific files. 800 * found for specific files.
800 */ 801 */
801 802
802static const int nice_match=258; /* Stop searching when current match exceeds this */ 803static const int nice_match = 258; /* Stop searching when current match exceeds this */
803 804
804/* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4 805/* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4
805 * For deflate_fast() (levels <= 3) good is ignored and lazy has a different 806 * For deflate_fast() (levels <= 3) good is ignored and lazy has a different
@@ -812,12 +813,12 @@ static const int nice_match=258; /* Stop searching when current match exceeds
812/* =========================================================================== 813/* ===========================================================================
813 * Prototypes for local functions. 814 * Prototypes for local functions.
814 */ 815 */
815static void fill_window (void); 816static void fill_window(void);
816 817
817static int longest_match (IPos cur_match); 818static int longest_match(IPos cur_match);
818 819
819#ifdef DEBUG 820#ifdef DEBUG
820static void check_match (IPos start, IPos match, int length); 821static void check_match(IPos start, IPos match, int length);
821#endif 822#endif
822 823
823/* =========================================================================== 824/* ===========================================================================
@@ -844,7 +845,7 @@ static void check_match (IPos start, IPos match, int length);
844/* =========================================================================== 845/* ===========================================================================
845 * Initialize the "longest match" routines for a new file 846 * Initialize the "longest match" routines for a new file
846 */ 847 */
847static void lm_init(ush *flags) 848static void lm_init(ush * flags)
848{ 849{
849 register unsigned j; 850 register unsigned j;
850 851
@@ -897,11 +898,10 @@ static int longest_match(IPos cur_match)
897{ 898{
898 unsigned chain_length = max_chain_length; /* max hash chain length */ 899 unsigned chain_length = max_chain_length; /* max hash chain length */
899 register uch *scan = window + strstart; /* current string */ 900 register uch *scan = window + strstart; /* current string */
900 register uch *match; /* matched string */ 901 register uch *match; /* matched string */
901 register int len; /* length of current match */ 902 register int len; /* length of current match */
902 int best_len = prev_length; /* best match length so far */ 903 int best_len = prev_length; /* best match length so far */
903 IPos limit = 904 IPos limit =
904
905 strstart > (IPos) MAX_DIST ? strstart - (IPos) MAX_DIST : NIL; 905 strstart > (IPos) MAX_DIST ? strstart - (IPos) MAX_DIST : NIL;
906 /* Stop when cur_match becomes <= limit. To simplify the code, 906 /* Stop when cur_match becomes <= limit. To simplify the code,
907 * we prevent matches with the string of window index 0. 907 * we prevent matches with the string of window index 0.
@@ -921,8 +921,7 @@ static int longest_match(IPos cur_match)
921 if (prev_length >= good_match) { 921 if (prev_length >= good_match) {
922 chain_length >>= 2; 922 chain_length >>= 2;
923 } 923 }
924 Assert(strstart <= window_size - MIN_LOOKAHEAD, 924 Assert(strstart <= window_size - MIN_LOOKAHEAD, "insufficient lookahead");
925 "insufficient lookahead");
926 925
927 do { 926 do {
928 Assert(cur_match < strstart, "no future"); 927 Assert(cur_match < strstart, "no future");
@@ -951,8 +950,7 @@ static int longest_match(IPos cur_match)
951 } while (*++scan == *++match && *++scan == *++match && 950 } while (*++scan == *++match && *++scan == *++match &&
952 *++scan == *++match && *++scan == *++match && 951 *++scan == *++match && *++scan == *++match &&
953 *++scan == *++match && *++scan == *++match && 952 *++scan == *++match && *++scan == *++match &&
954 *++scan == *++match && *++scan == *++match && 953 *++scan == *++match && *++scan == *++match && scan < strend);
955 scan < strend);
956 954
957 len = MAX_MATCH - (int) (strend - scan); 955 len = MAX_MATCH - (int) (strend - scan);
958 scan = strend - MAX_MATCH; 956 scan = strend - MAX_MATCH;
@@ -1007,7 +1005,6 @@ static void fill_window()
1007{ 1005{
1008 register unsigned n, m; 1006 register unsigned n, m;
1009 unsigned more = 1007 unsigned more =
1010
1011 (unsigned) (window_size - (ulg) lookahead - (ulg) strstart); 1008 (unsigned) (window_size - (ulg) lookahead - (ulg) strstart);
1012 /* Amount of free space at the end of the window. */ 1009 /* Amount of free space at the end of the window. */
1013 1010
@@ -1027,7 +1024,7 @@ static void fill_window()
1027 1024
1028 memcpy((char *) window, (char *) window + WSIZE, (unsigned) WSIZE); 1025 memcpy((char *) window, (char *) window + WSIZE, (unsigned) WSIZE);
1029 match_start -= WSIZE; 1026 match_start -= WSIZE;
1030 strstart -= WSIZE; /* we now have strstart >= MAX_DIST: */ 1027 strstart -= WSIZE; /* we now have strstart >= MAX_DIST: */
1031 1028
1032 block_start -= (long) WSIZE; 1029 block_start -= (long) WSIZE;
1033 1030
@@ -1070,9 +1067,9 @@ static void fill_window()
1070 */ 1067 */
1071static ulg deflate() 1068static ulg deflate()
1072{ 1069{
1073 IPos hash_head; /* head of hash chain */ 1070 IPos hash_head; /* head of hash chain */
1074 IPos prev_match; /* previous match */ 1071 IPos prev_match; /* previous match */
1075 int flush; /* set if current block must be flushed */ 1072 int flush; /* set if current block must be flushed */
1076 int match_available = 0; /* set if previous match exists */ 1073 int match_available = 0; /* set if previous match exists */
1077 register unsigned match_length = MIN_MATCH - 1; /* length of best match */ 1074 register unsigned match_length = MIN_MATCH - 1; /* length of best match */
1078 1075
@@ -1100,8 +1097,7 @@ static ulg deflate()
1100 match_length = lookahead; 1097 match_length = lookahead;
1101 1098
1102 /* Ignore a length 3 match if it is too distant: */ 1099 /* Ignore a length 3 match if it is too distant: */
1103 if (match_length == MIN_MATCH 1100 if (match_length == MIN_MATCH && strstart - match_start > TOO_FAR) {
1104 && strstart - match_start > TOO_FAR) {
1105 /* If prev_match is also MIN_MATCH, match_start is garbage 1101 /* If prev_match is also MIN_MATCH, match_start is garbage
1106 * but we will ignore the current match anyway. 1102 * but we will ignore the current match anyway.
1107 */ 1103 */
@@ -1116,8 +1112,7 @@ static ulg deflate()
1116 check_match(strstart - 1, prev_match, prev_length); 1112 check_match(strstart - 1, prev_match, prev_length);
1117 1113
1118 flush = 1114 flush =
1119 ct_tally(strstart - 1 - prev_match, 1115 ct_tally(strstart - 1 - prev_match, prev_length - MIN_MATCH);
1120 prev_length - MIN_MATCH);
1121 1116
1122 /* Insert in hash table all strings up to the end of the match. 1117 /* Insert in hash table all strings up to the end of the match.
1123 * strstart-1 and strstart are already inserted. 1118 * strstart-1 and strstart are already inserted.
@@ -1171,7 +1166,7 @@ static ulg deflate()
1171 if (match_available) 1166 if (match_available)
1172 ct_tally(0, window[strstart - 1]); 1167 ct_tally(0, window[strstart - 1]);
1173 1168
1174 return FLUSH_BLOCK(1); /* eof */ 1169 return FLUSH_BLOCK(1); /* eof */
1175} 1170}
1176 1171
1177/* gzip (GNU zip) -- compress files with zip algorithm and 'compress' interface 1172/* gzip (GNU zip) -- compress files with zip algorithm and 'compress' interface
@@ -1201,9 +1196,6 @@ typedef struct dirent dir_type;
1201typedef RETSIGTYPE(*sig_type) (int); 1196typedef RETSIGTYPE(*sig_type) (int);
1202 1197
1203/* ======================================================================== */ 1198/* ======================================================================== */
1204// int main (argc, argv)
1205// int argc;
1206// char **argv;
1207int gzip_main(int argc, char **argv) 1199int gzip_main(int argc, char **argv)
1208{ 1200{
1209 int result; 1201 int result;
@@ -1223,9 +1215,16 @@ int gzip_main(int argc, char **argv)
1223 case 'f': 1215 case 'f':
1224 force = 1; 1216 force = 1;
1225 break; 1217 break;
1226 /* Ignore 1-9 (compression level) options */ 1218 /* Ignore 1-9 (compression level) options */
1227 case '1': case '2': case '3': case '4': case '5': 1219 case '1':
1228 case '6': case '7': case '8': case '9': 1220 case '2':
1221 case '3':
1222 case '4':
1223 case '5':
1224 case '6':
1225 case '7':
1226 case '8':
1227 case '9':
1229 break; 1228 break;
1230 case 'q': 1229 case 'q':
1231 break; 1230 break;
@@ -1284,7 +1283,7 @@ int gzip_main(int argc, char **argv)
1284 outFileNum = STDOUT_FILENO; 1283 outFileNum = STDOUT_FILENO;
1285 } else { 1284 } else {
1286 inFileNum = open(argv[i], O_RDONLY); 1285 inFileNum = open(argv[i], O_RDONLY);
1287 if (inFileNum < 0 || fstat (inFileNum, &statBuf) < 0) 1286 if (inFileNum < 0 || fstat(inFileNum, &statBuf) < 0)
1288 perror_msg_and_die("%s", argv[i]); 1287 perror_msg_and_die("%s", argv[i]);
1289 time_stamp = statBuf.st_ctime; 1288 time_stamp = statBuf.st_ctime;
1290 ifile_size = statBuf.st_size; 1289 ifile_size = statBuf.st_size;
@@ -1296,7 +1295,8 @@ int gzip_main(int argc, char **argv)
1296 1295
1297 /* Open output file */ 1296 /* Open output file */
1298#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1) 1297#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
1299 outFileNum = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW); 1298 outFileNum =
1299 open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW);
1300#else 1300#else
1301 outFileNum = open(path, O_RDWR | O_CREAT | O_EXCL); 1301 outFileNum = open(path, O_RDWR | O_CREAT | O_EXCL);
1302#endif 1302#endif
@@ -1313,7 +1313,8 @@ int gzip_main(int argc, char **argv)
1313 } 1313 }
1314 1314
1315 if (path == NULL && isatty(outFileNum) && force == 0) { 1315 if (path == NULL && isatty(outFileNum) && force == 0) {
1316 error_msg("compressed data not written to a terminal. Use -f to force compression."); 1316 error_msg
1317 ("compressed data not written to a terminal. Use -f to force compression.");
1317 free(path); 1318 free(path);
1318 continue; 1319 continue;
1319 } 1320 }
@@ -1321,8 +1322,8 @@ int gzip_main(int argc, char **argv)
1321 result = zip(inFileNum, outFileNum); 1322 result = zip(inFileNum, outFileNum);
1322 1323
1323 if (path != NULL) { 1324 if (path != NULL) {
1324 close (inFileNum); 1325 close(inFileNum);
1325 close (outFileNum); 1326 close(outFileNum);
1326 1327
1327 /* Delete the original file */ 1328 /* Delete the original file */
1328 if (result == OK) 1329 if (result == OK)
@@ -1338,7 +1339,7 @@ int gzip_main(int argc, char **argv)
1338 } 1339 }
1339 } 1340 }
1340 1341
1341 return(exit_code); 1342 return (exit_code);
1342} 1343}
1343 1344
1344/* trees.c -- output deflated data using Huffman coding 1345/* trees.c -- output deflated data using Huffman coding
@@ -1427,17 +1428,19 @@ int gzip_main(int argc, char **argv)
1427typedef uch extra_bits_t; 1428typedef uch extra_bits_t;
1428 1429
1429/* extra bits for each length code */ 1430/* extra bits for each length code */
1430static const extra_bits_t extra_lbits[LENGTH_CODES] 1431static const extra_bits_t extra_lbits[LENGTH_CODES]
1431 = { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 1432 = { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4,
1432 4, 4, 5, 5, 5, 5, 0 }; 1433 4, 4, 5, 5, 5, 5, 0
1434};
1433 1435
1434/* extra bits for each distance code */ 1436/* extra bits for each distance code */
1435static const extra_bits_t extra_dbits[D_CODES] 1437static const extra_bits_t extra_dbits[D_CODES]
1436 = { 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 1438 = { 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9,
1437 10, 10, 11, 11, 12, 12, 13, 13 }; 1439 10, 10, 11, 11, 12, 12, 13, 13
1440};
1438 1441
1439/* extra bits for each bit length code */ 1442/* extra bits for each bit length code */
1440static const extra_bits_t extra_blbits[BL_CODES] 1443static const extra_bits_t extra_blbits[BL_CODES]
1441= { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 3, 7 }; 1444= { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 3, 7 };
1442 1445
1443#define STORED_BLOCK 0 1446#define STORED_BLOCK 0
@@ -1487,16 +1490,21 @@ static const extra_bits_t extra_blbits[BL_CODES]
1487#define REPZ_3_10 17 1490#define REPZ_3_10 17
1488/* repeat a zero length 3-10 times (3 bits of repeat count) */ 1491/* repeat a zero length 3-10 times (3 bits of repeat count) */
1489#define REPZ_11_138 18 1492#define REPZ_11_138 18
1490/* repeat a zero length 11-138 times (7 bits of repeat count) *//* =========================================================================== 1493/* repeat a zero length 11-138 times (7 bits of repeat count) */
1494
1495/* ===========================================================================
1491 * Local data 1496 * Local data
1492 *//* Data structure describing a single value and its code string. */ typedef struct ct_data { 1497 */
1498
1499/* Data structure describing a single value and its code string. */
1500typedef struct ct_data {
1493 union { 1501 union {
1494 ush freq; /* frequency count */ 1502 ush freq; /* frequency count */
1495 ush code; /* bit string */ 1503 ush code; /* bit string */
1496 } fc; 1504 } fc;
1497 union { 1505 union {
1498 ush dad; /* father node in Huffman tree */ 1506 ush dad; /* father node in Huffman tree */
1499 ush len; /* length of bit string */ 1507 ush len; /* length of bit string */
1500 } dl; 1508 } dl;
1501} ct_data; 1509} ct_data;
1502 1510
@@ -1530,25 +1538,27 @@ static ct_data bl_tree[2 * BL_CODES + 1];
1530/* Huffman tree for the bit lengths */ 1538/* Huffman tree for the bit lengths */
1531 1539
1532typedef struct tree_desc { 1540typedef struct tree_desc {
1533 ct_data *dyn_tree; /* the dynamic tree */ 1541 ct_data *dyn_tree; /* the dynamic tree */
1534 ct_data *static_tree; /* corresponding static tree or NULL */ 1542 ct_data *static_tree; /* corresponding static tree or NULL */
1535 const extra_bits_t *extra_bits; /* extra bits for each code or NULL */ 1543 const extra_bits_t *extra_bits; /* extra bits for each code or NULL */
1536 int extra_base; /* base index for extra_bits */ 1544 int extra_base; /* base index for extra_bits */
1537 int elems; /* max number of elements in the tree */ 1545 int elems; /* max number of elements in the tree */
1538 int max_length; /* max bit length for the codes */ 1546 int max_length; /* max bit length for the codes */
1539 int max_code; /* largest code with non zero frequency */ 1547 int max_code; /* largest code with non zero frequency */
1540} tree_desc; 1548} tree_desc;
1541 1549
1542static tree_desc l_desc = 1550static tree_desc l_desc =
1543 { dyn_ltree, static_ltree, extra_lbits, LITERALS + 1, L_CODES, 1551 { dyn_ltree, static_ltree, extra_lbits, LITERALS + 1, L_CODES,
1544 MAX_BITS, 0 }; 1552 MAX_BITS, 0
1553};
1545 1554
1546static tree_desc d_desc = 1555static tree_desc d_desc =
1547 { dyn_dtree, static_dtree, extra_dbits, 0, D_CODES, MAX_BITS, 0 }; 1556 { dyn_dtree, static_dtree, extra_dbits, 0, D_CODES, MAX_BITS, 0 };
1548 1557
1549static tree_desc bl_desc = 1558static tree_desc bl_desc =
1550 { bl_tree, (ct_data *) 0, extra_blbits, 0, BL_CODES, MAX_BL_BITS, 1559 { bl_tree, (ct_data *) 0, extra_blbits, 0, BL_CODES, MAX_BL_BITS,
1551 0 }; 1560 0
1561};
1552 1562
1553 1563
1554static ush bl_count[MAX_BITS + 1]; 1564static ush bl_count[MAX_BITS + 1];
@@ -1563,8 +1573,8 @@ static const uch bl_order[BL_CODES]
1563 */ 1573 */
1564 1574
1565static int heap[2 * L_CODES + 1]; /* heap used to build the Huffman trees */ 1575static int heap[2 * L_CODES + 1]; /* heap used to build the Huffman trees */
1566static int heap_len; /* number of elements in the heap */ 1576static int heap_len; /* number of elements in the heap */
1567static int heap_max; /* element of largest frequency */ 1577static int heap_max; /* element of largest frequency */
1568 1578
1569/* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used. 1579/* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.
1570 * The same heap array is used to build all trees. 1580 * The same heap array is used to build all trees.
@@ -1604,41 +1614,41 @@ static uch flag_buf[(LIT_BUFSIZE / 8)];
1604 * l_buf, thus indicating the presence or absence of a distance. 1614 * l_buf, thus indicating the presence or absence of a distance.
1605 */ 1615 */
1606 1616
1607static unsigned last_lit; /* running index in l_buf */ 1617static unsigned last_lit; /* running index in l_buf */
1608static unsigned last_dist; /* running index in d_buf */ 1618static unsigned last_dist; /* running index in d_buf */
1609static unsigned last_flags; /* running index in flag_buf */ 1619static unsigned last_flags; /* running index in flag_buf */
1610static uch flags; /* current flags not yet saved in flag_buf */ 1620static uch flags; /* current flags not yet saved in flag_buf */
1611static uch flag_bit; /* current bit used in flags */ 1621static uch flag_bit; /* current bit used in flags */
1612 1622
1613/* bits are filled in flags starting at bit 0 (least significant). 1623/* bits are filled in flags starting at bit 0 (least significant).
1614 * Note: these flags are overkill in the current code since we don't 1624 * Note: these flags are overkill in the current code since we don't
1615 * take advantage of DIST_BUFSIZE == LIT_BUFSIZE. 1625 * take advantage of DIST_BUFSIZE == LIT_BUFSIZE.
1616 */ 1626 */
1617 1627
1618static ulg opt_len; /* bit length of current block with optimal trees */ 1628static ulg opt_len; /* bit length of current block with optimal trees */
1619static ulg static_len; /* bit length of current block with static trees */ 1629static ulg static_len; /* bit length of current block with static trees */
1620 1630
1621static ulg compressed_len; /* total bit length of compressed file */ 1631static ulg compressed_len; /* total bit length of compressed file */
1622 1632
1623 1633
1624static ush *file_type; /* pointer to UNKNOWN, BINARY or ASCII */ 1634static ush *file_type; /* pointer to UNKNOWN, BINARY or ASCII */
1625static int *file_method; /* pointer to DEFLATE or STORE */ 1635static int *file_method; /* pointer to DEFLATE or STORE */
1626 1636
1627/* =========================================================================== 1637/* ===========================================================================
1628 * Local (static) routines in this file. 1638 * Local (static) routines in this file.
1629 */ 1639 */
1630 1640
1631static void init_block (void); 1641static void init_block(void);
1632static void pqdownheap (ct_data * tree, int k); 1642static void pqdownheap(ct_data * tree, int k);
1633static void gen_bitlen (tree_desc * desc); 1643static void gen_bitlen(tree_desc * desc);
1634static void gen_codes (ct_data * tree, int max_code); 1644static void gen_codes(ct_data * tree, int max_code);
1635static void build_tree (tree_desc * desc); 1645static void build_tree(tree_desc * desc);
1636static void scan_tree (ct_data * tree, int max_code); 1646static void scan_tree(ct_data * tree, int max_code);
1637static void send_tree (ct_data * tree, int max_code); 1647static void send_tree(ct_data * tree, int max_code);
1638static int build_bl_tree (void); 1648static int build_bl_tree(void);
1639static void send_all_trees (int lcodes, int dcodes, int blcodes); 1649static void send_all_trees(int lcodes, int dcodes, int blcodes);
1640static void compress_block (ct_data * ltree, ct_data * dtree); 1650static void compress_block(ct_data * ltree, ct_data * dtree);
1641static void set_file_type (void); 1651static void set_file_type(void);
1642 1652
1643 1653
1644#ifndef DEBUG 1654#ifndef DEBUG
@@ -1665,20 +1675,20 @@ static void set_file_type (void);
1665 * location of the internal file attribute (ascii/binary) and method 1675 * location of the internal file attribute (ascii/binary) and method
1666 * (DEFLATE/STORE). 1676 * (DEFLATE/STORE).
1667 */ 1677 */
1668static void ct_init(ush *attr, int *methodp) 1678static void ct_init(ush * attr, int *methodp)
1669{ 1679{
1670 int n; /* iterates over tree elements */ 1680 int n; /* iterates over tree elements */
1671 int bits; /* bit counter */ 1681 int bits; /* bit counter */
1672 int length; /* length value */ 1682 int length; /* length value */
1673 int code; /* code value */ 1683 int code; /* code value */
1674 int dist; /* distance index */ 1684 int dist; /* distance index */
1675 1685
1676 file_type = attr; 1686 file_type = attr;
1677 file_method = methodp; 1687 file_method = methodp;
1678 compressed_len = 0L; 1688 compressed_len = 0L;
1679 1689
1680 if (static_dtree[0].Len != 0) 1690 if (static_dtree[0].Len != 0)
1681 return; /* ct_init already called */ 1691 return; /* ct_init already called */
1682 1692
1683 /* Initialize the mapping length (0..255) -> length code (0..28) */ 1693 /* Initialize the mapping length (0..255) -> length code (0..28) */
1684 length = 0; 1694 length = 0;
@@ -1704,7 +1714,7 @@ static void ct_init(ush *attr, int *methodp)
1704 } 1714 }
1705 } 1715 }
1706 Assert(dist == 256, "ct_init: dist != 256"); 1716 Assert(dist == 256, "ct_init: dist != 256");
1707 dist >>= 7; /* from now on, all distances are divided by 128 */ 1717 dist >>= 7; /* from now on, all distances are divided by 128 */
1708 for (; code < D_CODES; code++) { 1718 for (; code < D_CODES; code++) {
1709 base_dist[code] = dist << 7; 1719 base_dist[code] = dist << 7;
1710 for (n = 0; n < (1 << (extra_dbits[code] - 7)); n++) { 1720 for (n = 0; n < (1 << (extra_dbits[code] - 7)); n++) {
@@ -1746,7 +1756,7 @@ static void ct_init(ush *attr, int *methodp)
1746 */ 1756 */
1747static void init_block() 1757static void init_block()
1748{ 1758{
1749 int n; /* iterates over tree elements */ 1759 int n; /* iterates over tree elements */
1750 1760
1751 /* Initialize the trees. */ 1761 /* Initialize the trees. */
1752 for (n = 0; n < L_CODES; n++) 1762 for (n = 0; n < L_CODES; n++)
@@ -1792,10 +1802,10 @@ static void init_block()
1792 * when the heap property is re-established (each father smaller than its 1802 * when the heap property is re-established (each father smaller than its
1793 * two sons). 1803 * two sons).
1794 */ 1804 */
1795static void pqdownheap(ct_data *tree, int k) 1805static void pqdownheap(ct_data * tree, int k)
1796{ 1806{
1797 int v = heap[k]; 1807 int v = heap[k];
1798 int j = k << 1; /* left son of k */ 1808 int j = k << 1; /* left son of k */
1799 1809
1800 while (j <= heap_len) { 1810 while (j <= heap_len) {
1801 /* Set j to the smallest of the two sons: */ 1811 /* Set j to the smallest of the two sons: */
@@ -1826,7 +1836,7 @@ static void pqdownheap(ct_data *tree, int k)
1826 * The length opt_len is updated; static_len is also updated if stree is 1836 * The length opt_len is updated; static_len is also updated if stree is
1827 * not null. 1837 * not null.
1828 */ 1838 */
1829static void gen_bitlen(tree_desc *desc) 1839static void gen_bitlen(tree_desc * desc)
1830{ 1840{
1831 ct_data *tree = desc->dyn_tree; 1841 ct_data *tree = desc->dyn_tree;
1832 const extra_bits_t *extra = desc->extra_bits; 1842 const extra_bits_t *extra = desc->extra_bits;
@@ -1834,12 +1844,12 @@ static void gen_bitlen(tree_desc *desc)
1834 int max_code = desc->max_code; 1844 int max_code = desc->max_code;
1835 int max_length = desc->max_length; 1845 int max_length = desc->max_length;
1836 ct_data *stree = desc->static_tree; 1846 ct_data *stree = desc->static_tree;
1837 int h; /* heap index */ 1847 int h; /* heap index */
1838 int n, m; /* iterate over the tree elements */ 1848 int n, m; /* iterate over the tree elements */
1839 int bits; /* bit length */ 1849 int bits; /* bit length */
1840 int xbits; /* extra bits */ 1850 int xbits; /* extra bits */
1841 ush f; /* frequency */ 1851 ush f; /* frequency */
1842 int overflow = 0; /* number of elements with bit length too large */ 1852 int overflow = 0; /* number of elements with bit length too large */
1843 1853
1844 for (bits = 0; bits <= MAX_BITS; bits++) 1854 for (bits = 0; bits <= MAX_BITS; bits++)
1845 bl_count[bits] = 0; 1855 bl_count[bits] = 0;
@@ -1858,7 +1868,7 @@ static void gen_bitlen(tree_desc *desc)
1858 /* We overwrite tree[n].Dad which is no longer needed */ 1868 /* We overwrite tree[n].Dad which is no longer needed */
1859 1869
1860 if (n > max_code) 1870 if (n > max_code)
1861 continue; /* not a leaf node */ 1871 continue; /* not a leaf node */
1862 1872
1863 bl_count[bits]++; 1873 bl_count[bits]++;
1864 xbits = 0; 1874 xbits = 0;
@@ -1881,7 +1891,7 @@ static void gen_bitlen(tree_desc *desc)
1881 bits = max_length - 1; 1891 bits = max_length - 1;
1882 while (bl_count[bits] == 0) 1892 while (bl_count[bits] == 0)
1883 bits--; 1893 bits--;
1884 bl_count[bits]--; /* move one leaf down the tree */ 1894 bl_count[bits]--; /* move one leaf down the tree */
1885 bl_count[bits + 1] += 2; /* move one overflow item as its brother */ 1895 bl_count[bits + 1] += 2; /* move one overflow item as its brother */
1886 bl_count[max_length]--; 1896 bl_count[max_length]--;
1887 /* The brother of the overflow item also moves one step up, 1897 /* The brother of the overflow item also moves one step up,
@@ -1902,12 +1912,10 @@ static void gen_bitlen(tree_desc *desc)
1902 if (m > max_code) 1912 if (m > max_code)
1903 continue; 1913 continue;
1904 if (tree[m].Len != (unsigned) bits) { 1914 if (tree[m].Len != (unsigned) bits) {
1905 Trace( 1915 Trace((stderr, "code %d bits %d->%d\n", m, tree[m].Len,
1906 (stderr, "code %d bits %d->%d\n", m, tree[m].Len,
1907 bits)); 1916 bits));
1908 opt_len += 1917 opt_len +=
1909 ((long) bits - 1918 ((long) bits - (long) tree[m].Len) * (long) tree[m].Freq;
1910 (long) tree[m].Len) * (long) tree[m].Freq;
1911 tree[m].Len = (ush) bits; 1919 tree[m].Len = (ush) bits;
1912 } 1920 }
1913 n--; 1921 n--;
@@ -1923,12 +1931,12 @@ static void gen_bitlen(tree_desc *desc)
1923 * OUT assertion: the field code is set for all tree elements of non 1931 * OUT assertion: the field code is set for all tree elements of non
1924 * zero code length. 1932 * zero code length.
1925 */ 1933 */
1926static void gen_codes(ct_data *tree, int max_code) 1934static void gen_codes(ct_data * tree, int max_code)
1927{ 1935{
1928 ush next_code[MAX_BITS + 1]; /* next code value for each bit length */ 1936 ush next_code[MAX_BITS + 1]; /* next code value for each bit length */
1929 ush code = 0; /* running code value */ 1937 ush code = 0; /* running code value */
1930 int bits; /* bit index */ 1938 int bits; /* bit index */
1931 int n; /* code index */ 1939 int n; /* code index */
1932 1940
1933 /* The distribution counts are first used to generate the code values 1941 /* The distribution counts are first used to generate the code values
1934 * without bit reversal. 1942 * without bit reversal.
@@ -1966,14 +1974,14 @@ static void gen_codes(ct_data *tree, int max_code)
1966 * and corresponding code. The length opt_len is updated; static_len is 1974 * and corresponding code. The length opt_len is updated; static_len is
1967 * also updated if stree is not null. The field max_code is set. 1975 * also updated if stree is not null. The field max_code is set.
1968 */ 1976 */
1969static void build_tree(tree_desc *desc) 1977static void build_tree(tree_desc * desc)
1970{ 1978{
1971 ct_data *tree = desc->dyn_tree; 1979 ct_data *tree = desc->dyn_tree;
1972 ct_data *stree = desc->static_tree; 1980 ct_data *stree = desc->static_tree;
1973 int elems = desc->elems; 1981 int elems = desc->elems;
1974 int n, m; /* iterate over heap elements */ 1982 int n, m; /* iterate over heap elements */
1975 int max_code = -1; /* largest code with non zero frequency */ 1983 int max_code = -1; /* largest code with non zero frequency */
1976 int node = elems; /* next internal node of the tree */ 1984 int node = elems; /* next internal node of the tree */
1977 1985
1978 /* Construct the initial heap, with least frequent element in 1986 /* Construct the initial heap, with least frequent element in
1979 * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1]. 1987 * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1].
@@ -2017,8 +2025,8 @@ static void build_tree(tree_desc *desc)
2017 * frequent nodes. 2025 * frequent nodes.
2018 */ 2026 */
2019 do { 2027 do {
2020 pqremove(tree, n); /* n = node of least frequency */ 2028 pqremove(tree, n); /* n = node of least frequency */
2021 m = heap[SMALLEST]; /* m = node of next least frequency */ 2029 m = heap[SMALLEST]; /* m = node of next least frequency */
2022 2030
2023 heap[--heap_max] = n; /* keep the nodes sorted by frequency */ 2031 heap[--heap_max] = n; /* keep the nodes sorted by frequency */
2024 heap[--heap_max] = m; 2032 heap[--heap_max] = m;
@@ -2030,8 +2038,7 @@ static void build_tree(tree_desc *desc)
2030#ifdef DUMP_BL_TREE 2038#ifdef DUMP_BL_TREE
2031 if (tree == bl_tree) { 2039 if (tree == bl_tree) {
2032 fprintf(stderr, "\nnode %d(%d), sons %d(%d) %d(%d)", 2040 fprintf(stderr, "\nnode %d(%d), sons %d(%d) %d(%d)",
2033 node, tree[node].Freq, n, tree[n].Freq, m, 2041 node, tree[node].Freq, n, tree[n].Freq, m, tree[m].Freq);
2034 tree[m].Freq);
2035 } 2042 }
2036#endif 2043#endif
2037 /* and insert the new node in the heap */ 2044 /* and insert the new node in the heap */
@@ -2057,15 +2064,15 @@ static void build_tree(tree_desc *desc)
2057 * counts. (The contribution of the bit length codes will be added later 2064 * counts. (The contribution of the bit length codes will be added later
2058 * during the construction of bl_tree.) 2065 * during the construction of bl_tree.)
2059 */ 2066 */
2060static void scan_tree(ct_data *tree, int max_code) 2067static void scan_tree(ct_data * tree, int max_code)
2061{ 2068{
2062 int n; /* iterates over all tree elements */ 2069 int n; /* iterates over all tree elements */
2063 int prevlen = -1; /* last emitted length */ 2070 int prevlen = -1; /* last emitted length */
2064 int curlen; /* length of current code */ 2071 int curlen; /* length of current code */
2065 int nextlen = tree[0].Len; /* length of next code */ 2072 int nextlen = tree[0].Len; /* length of next code */
2066 int count = 0; /* repeat count of the current code */ 2073 int count = 0; /* repeat count of the current code */
2067 int max_count = 7; /* max repeat count */ 2074 int max_count = 7; /* max repeat count */
2068 int min_count = 4; /* min repeat count */ 2075 int min_count = 4; /* min repeat count */
2069 2076
2070 if (nextlen == 0) 2077 if (nextlen == 0)
2071 max_count = 138, min_count = 3; 2078 max_count = 138, min_count = 3;
@@ -2103,15 +2110,15 @@ static void scan_tree(ct_data *tree, int max_code)
2103 * Send a literal or distance tree in compressed form, using the codes in 2110 * Send a literal or distance tree in compressed form, using the codes in
2104 * bl_tree. 2111 * bl_tree.
2105 */ 2112 */
2106static void send_tree(ct_data *tree, int max_code) 2113static void send_tree(ct_data * tree, int max_code)
2107{ 2114{
2108 int n; /* iterates over all tree elements */ 2115 int n; /* iterates over all tree elements */
2109 int prevlen = -1; /* last emitted length */ 2116 int prevlen = -1; /* last emitted length */
2110 int curlen; /* length of current code */ 2117 int curlen; /* length of current code */
2111 int nextlen = tree[0].Len; /* length of next code */ 2118 int nextlen = tree[0].Len; /* length of next code */
2112 int count = 0; /* repeat count of the current code */ 2119 int count = 0; /* repeat count of the current code */
2113 int max_count = 7; /* max repeat count */ 2120 int max_count = 7; /* max repeat count */
2114 int min_count = 4; /* min repeat count */ 2121 int min_count = 4; /* min repeat count */
2115 2122
2116/* tree[max_code+1].Len = -1; *//* guard already set */ 2123/* tree[max_code+1].Len = -1; *//* guard already set */
2117 if (nextlen == 0) 2124 if (nextlen == 0)
@@ -2162,7 +2169,7 @@ static void send_tree(ct_data *tree, int max_code)
2162 */ 2169 */
2163static const int build_bl_tree() 2170static const int build_bl_tree()
2164{ 2171{
2165 int max_blindex; /* index of last bit length code of non zero freq */ 2172 int max_blindex; /* index of last bit length code of non zero freq */
2166 2173
2167 /* Determine the bit length frequencies for literal and distance trees */ 2174 /* Determine the bit length frequencies for literal and distance trees */
2168 scan_tree((ct_data *) dyn_ltree, l_desc.max_code); 2175 scan_tree((ct_data *) dyn_ltree, l_desc.max_code);
@@ -2184,9 +2191,7 @@ static const int build_bl_tree()
2184 } 2191 }
2185 /* Update opt_len to include the bit length tree and counts */ 2192 /* Update opt_len to include the bit length tree and counts */
2186 opt_len += 3 * (max_blindex + 1) + 5 + 5 + 4; 2193 opt_len += 3 * (max_blindex + 1) + 5 + 5 + 4;
2187 Tracev( 2194 Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld", opt_len, static_len));
2188 (stderr, "\ndyn trees: dyn %ld, stat %ld", opt_len,
2189 static_len));
2190 2195
2191 return max_blindex; 2196 return max_blindex;
2192} 2197}
@@ -2198,10 +2203,9 @@ static const int build_bl_tree()
2198 */ 2203 */
2199static void send_all_trees(int lcodes, int dcodes, int blcodes) 2204static void send_all_trees(int lcodes, int dcodes, int blcodes)
2200{ 2205{
2201 int rank; /* index in bl_order */ 2206 int rank; /* index in bl_order */
2202 2207
2203 Assert(lcodes >= 257 && dcodes >= 1 2208 Assert(lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes");
2204 && blcodes >= 4, "not enough codes");
2205 Assert(lcodes <= L_CODES && dcodes <= D_CODES 2209 Assert(lcodes <= L_CODES && dcodes <= D_CODES
2206 && blcodes <= BL_CODES, "too many codes"); 2210 && blcodes <= BL_CODES, "too many codes");
2207 Tracev((stderr, "\nbl counts: ")); 2211 Tracev((stderr, "\nbl counts: "));
@@ -2229,7 +2233,7 @@ static void send_all_trees(int lcodes, int dcodes, int blcodes)
2229static ulg flush_block(char *buf, ulg stored_len, int eof) 2233static ulg flush_block(char *buf, ulg stored_len, int eof)
2230{ 2234{
2231 ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */ 2235 ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */
2232 int max_blindex; /* index of last bit length code of non zero freq */ 2236 int max_blindex; /* index of last bit length code of non zero freq */
2233 2237
2234 flag_buf[last_flags] = flags; /* Save the flags for the last 8 items */ 2238 flag_buf[last_flags] = flags; /* Save the flags for the last 8 items */
2235 2239
@@ -2242,9 +2246,7 @@ static ulg flush_block(char *buf, ulg stored_len, int eof)
2242 Tracev((stderr, "\nlit data: dyn %ld, stat %ld", opt_len, static_len)); 2246 Tracev((stderr, "\nlit data: dyn %ld, stat %ld", opt_len, static_len));
2243 2247
2244 build_tree((tree_desc *) (&d_desc)); 2248 build_tree((tree_desc *) (&d_desc));
2245 Tracev( 2249 Tracev((stderr, "\ndist data: dyn %ld, stat %ld", opt_len, static_len));
2246 (stderr, "\ndist data: dyn %ld, stat %ld", opt_len,
2247 static_len));
2248 /* At this point, opt_len and static_len are the total bit lengths of 2250 /* At this point, opt_len and static_len are the total bit lengths of
2249 * the compressed block data, excluding the tree representations. 2251 * the compressed block data, excluding the tree representations.
2250 */ 2252 */
@@ -2258,8 +2260,7 @@ static ulg flush_block(char *buf, ulg stored_len, int eof)
2258 opt_lenb = (opt_len + 3 + 7) >> 3; 2260 opt_lenb = (opt_len + 3 + 7) >> 3;
2259 static_lenb = (static_len + 3 + 7) >> 3; 2261 static_lenb = (static_len + 3 + 7) >> 3;
2260 2262
2261 Trace( 2263 Trace((stderr,
2262 (stderr,
2263 "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u dist %u ", 2264 "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u dist %u ",
2264 opt_lenb, opt_len, static_lenb, static_len, stored_len, 2265 opt_lenb, opt_len, static_lenb, static_len, stored_len,
2265 last_lit, last_dist)); 2266 last_lit, last_dist));
@@ -2271,8 +2272,7 @@ static ulg flush_block(char *buf, ulg stored_len, int eof)
2271 * and if the zip file can be seeked (to rewrite the local header), 2272 * and if the zip file can be seeked (to rewrite the local header),
2272 * the whole file is transformed into a stored file: 2273 * the whole file is transformed into a stored file:
2273 */ 2274 */
2274 if (stored_len <= opt_lenb && eof && compressed_len == 0L 2275 if (stored_len <= opt_lenb && eof && compressed_len == 0L && seekable()) {
2275 && seekable()) {
2276 /* Since LIT_BUFSIZE <= 2*WSIZE, the input data must be there: */ 2276 /* Since LIT_BUFSIZE <= 2*WSIZE, the input data must be there: */
2277 if (buf == (char *) 0) 2277 if (buf == (char *) 0)
2278 error_msg("block vanished"); 2278 error_msg("block vanished");
@@ -2297,15 +2297,13 @@ static ulg flush_block(char *buf, ulg stored_len, int eof)
2297 2297
2298 } else if (static_lenb == opt_lenb) { 2298 } else if (static_lenb == opt_lenb) {
2299 send_bits((STATIC_TREES << 1) + eof, 3); 2299 send_bits((STATIC_TREES << 1) + eof, 3);
2300 compress_block((ct_data *) static_ltree, 2300 compress_block((ct_data *) static_ltree, (ct_data *) static_dtree);
2301 (ct_data *) static_dtree);
2302 compressed_len += 3 + static_len; 2301 compressed_len += 3 + static_len;
2303 } else { 2302 } else {
2304 send_bits((DYN_TREES << 1) + eof, 3); 2303 send_bits((DYN_TREES << 1) + eof, 3);
2305 send_all_trees(l_desc.max_code + 1, d_desc.max_code + 1, 2304 send_all_trees(l_desc.max_code + 1, d_desc.max_code + 1,
2306 max_blindex + 1); 2305 max_blindex + 1);
2307 compress_block((ct_data *) dyn_ltree, 2306 compress_block((ct_data *) dyn_ltree, (ct_data *) dyn_dtree);
2308 (ct_data *) dyn_dtree);
2309 compressed_len += 3 + opt_len; 2307 compressed_len += 3 + opt_len;
2310 } 2308 }
2311 Assert(compressed_len == bits_sent, "bad compressed size"); 2309 Assert(compressed_len == bits_sent, "bad compressed size");
@@ -2333,7 +2331,7 @@ static int ct_tally(int dist, int lc)
2333 dyn_ltree[lc].Freq++; 2331 dyn_ltree[lc].Freq++;
2334 } else { 2332 } else {
2335 /* Here, lc is the match length - MIN_MATCH */ 2333 /* Here, lc is the match length - MIN_MATCH */
2336 dist--; /* dist = match distance - 1 */ 2334 dist--; /* dist = match distance - 1 */
2337 Assert((ush) dist < (ush) MAX_DIST && 2335 Assert((ush) dist < (ush) MAX_DIST &&
2338 (ush) lc <= (ush) (MAX_MATCH - MIN_MATCH) && 2336 (ush) lc <= (ush) (MAX_MATCH - MIN_MATCH) &&
2339 (ush) d_code(dist) < (ush) D_CODES, "ct_tally: bad match"); 2337 (ush) d_code(dist) < (ush) D_CODES, "ct_tally: bad match");
@@ -2363,8 +2361,7 @@ static int ct_tally(int dist, int lc)
2363 (ulg) dyn_dtree[dcode].Freq * (5L + extra_dbits[dcode]); 2361 (ulg) dyn_dtree[dcode].Freq * (5L + extra_dbits[dcode]);
2364 } 2362 }
2365 out_length >>= 3; 2363 out_length >>= 3;
2366 Trace( 2364 Trace((stderr,
2367 (stderr,
2368 "\nlast_lit %u, last_dist %u, in %ld, out ~%ld(%ld%%) ", 2365 "\nlast_lit %u, last_dist %u, in %ld, out ~%ld(%ld%%) ",
2369 last_lit, last_dist, in_length, out_length, 2366 last_lit, last_dist, in_length, out_length,
2370 100L - out_length * 100L / in_length)); 2367 100L - out_length * 100L / in_length));
@@ -2381,16 +2378,16 @@ static int ct_tally(int dist, int lc)
2381/* =========================================================================== 2378/* ===========================================================================
2382 * Send the block data compressed using the given Huffman trees 2379 * Send the block data compressed using the given Huffman trees
2383 */ 2380 */
2384static void compress_block(ct_data *ltree, ct_data *dtree) 2381static void compress_block(ct_data * ltree, ct_data * dtree)
2385{ 2382{
2386 unsigned dist; /* distance of matched string */ 2383 unsigned dist; /* distance of matched string */
2387 int lc; /* match length or unmatched char (if dist == 0) */ 2384 int lc; /* match length or unmatched char (if dist == 0) */
2388 unsigned lx = 0; /* running index in l_buf */ 2385 unsigned lx = 0; /* running index in l_buf */
2389 unsigned dx = 0; /* running index in d_buf */ 2386 unsigned dx = 0; /* running index in d_buf */
2390 unsigned fx = 0; /* running index in flag_buf */ 2387 unsigned fx = 0; /* running index in flag_buf */
2391 uch flag = 0; /* current flags */ 2388 uch flag = 0; /* current flags */
2392 unsigned code; /* the code to send */ 2389 unsigned code; /* the code to send */
2393 int extra; /* number of extra bits to send */ 2390 int extra; /* number of extra bits to send */
2394 2391
2395 if (last_lit != 0) 2392 if (last_lit != 0)
2396 do { 2393 do {
@@ -2420,7 +2417,7 @@ static void compress_block(ct_data *ltree, ct_data *dtree)
2420 dist -= base_dist[code]; 2417 dist -= base_dist[code];
2421 send_bits(dist, extra); /* send the extra distance bits */ 2418 send_bits(dist, extra); /* send the extra distance bits */
2422 } 2419 }
2423 } /* literal or match pair ? */ 2420 } /* literal or match pair ? */
2424 flag >>= 1; 2421 flag >>= 1;
2425 } while (lx < last_lit); 2422 } while (lx < last_lit);
2426 2423
@@ -2458,13 +2455,13 @@ static void set_file_type()
2458 */ 2455 */
2459 2456
2460 2457
2461static ulg crc; /* crc on uncompressed file data */ 2458static ulg crc; /* crc on uncompressed file data */
2462static long header_bytes; /* number of bytes in gzip header */ 2459static long header_bytes; /* number of bytes in gzip header */
2463 2460
2464static void put_long(ulg n) 2461static void put_long(ulg n)
2465{ 2462{
2466 put_short((n) & 0xffff); 2463 put_short((n) & 0xffff);
2467 put_short(((ulg)(n)) >> 16); 2464 put_short(((ulg) (n)) >> 16);
2468} 2465}
2469 2466
2470/* put_header_byte is used for the compressed output 2467/* put_header_byte is used for the compressed output
@@ -2479,9 +2476,9 @@ static void put_long(ulg n)
2479 */ 2476 */
2480static int zip(int in, int out) 2477static int zip(int in, int out)
2481{ 2478{
2482 uch my_flags = 0; /* general purpose bit flags */ 2479 uch my_flags = 0; /* general purpose bit flags */
2483 ush attr = 0; /* ascii/binary flag */ 2480 ush attr = 0; /* ascii/binary flag */
2484 ush deflate_flags = 0; /* pkzip -es, -en or -ex equivalent */ 2481 ush deflate_flags = 0; /* pkzip -es, -en or -ex equivalent */
2485 2482
2486 ifd = in; 2483 ifd = in;
2487 ofd = out; 2484 ofd = out;
@@ -2491,11 +2488,11 @@ static int zip(int in, int out)
2491 2488
2492 2489
2493 method = DEFLATED; 2490 method = DEFLATED;
2494 put_header_byte(GZIP_MAGIC[0]); /* magic header */ 2491 put_header_byte(GZIP_MAGIC[0]); /* magic header */
2495 put_header_byte(GZIP_MAGIC[1]); 2492 put_header_byte(GZIP_MAGIC[1]);
2496 put_header_byte(DEFLATED); /* compression method */ 2493 put_header_byte(DEFLATED); /* compression method */
2497 2494
2498 put_header_byte(my_flags); /* general flags */ 2495 put_header_byte(my_flags); /* general flags */
2499 put_long(time_stamp); 2496 put_long(time_stamp);
2500 2497
2501 /* Write deflated file to zip file */ 2498 /* Write deflated file to zip file */
@@ -2506,7 +2503,7 @@ static int zip(int in, int out)
2506 lm_init(&deflate_flags); 2503 lm_init(&deflate_flags);
2507 2504
2508 put_byte((uch) deflate_flags); /* extra flags */ 2505 put_byte((uch) deflate_flags); /* extra flags */
2509 put_byte(OS_CODE); /* OS identifier */ 2506 put_byte(OS_CODE); /* OS identifier */
2510 2507
2511 header_bytes = (long) outcnt; 2508 header_bytes = (long) outcnt;
2512 2509