summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Andersen <andersen@codepoet.org>2001-07-02 17:57:32 +0000
committerEric Andersen <andersen@codepoet.org>2001-07-02 17:57:32 +0000
commit3073dfbf30da26ac60196319866c8e0a931c2789 (patch)
tree6dc9c72ce99d358db4954f89bbd2f8ea06aee1ad
parent22ecf049b634ccd507ed0587526599ed1971c308 (diff)
downloadbusybox-w32-3073dfbf30da26ac60196319866c8e0a931c2789.tar.gz
busybox-w32-3073dfbf30da26ac60196319866c8e0a931c2789.tar.bz2
busybox-w32-3073dfbf30da26ac60196319866c8e0a931c2789.zip
Patch from Aaron Lehmann <aaronl@vitelus.com> to scrub a ton of
portability junk from gzip.c, making it a zillion times more readable.
-rw-r--r--archival/gzip.c1322
-rw-r--r--gzip.c1322
2 files changed, 386 insertions, 2258 deletions
diff --git a/archival/gzip.c b/archival/gzip.c
index c4c592729..cdf226889 100644
--- a/archival/gzip.c
+++ b/archival/gzip.c
@@ -35,14 +35,20 @@
35#define SMALL_MEM 35#define SMALL_MEM
36#define DYN_ALLOC 36#define DYN_ALLOC
37 37
38/* I don't like nested includes, but the string and io functions are used
39 * too often
40 */
41#include <stdlib.h> 38#include <stdlib.h>
42#include <stdio.h> 39#include <stdio.h>
43#include <string.h> 40#include <string.h>
44#include <unistd.h> 41#include <unistd.h>
45#include <errno.h> 42#include <errno.h>
43#include <sys/types.h>
44#include <signal.h>
45#include <utime.h>
46#include <ctype.h>
47#include <sys/types.h>
48#include <unistd.h>
49#include <dirent.h>
50#include <fcntl.h>
51#include <time.h>
46#include "busybox.h" 52#include "busybox.h"
47 53
48#define memzero(s, n) memset ((void *)(s), 0, (n)) 54#define memzero(s, n) memset ((void *)(s), 0, (n))
@@ -51,8 +57,6 @@
51# define RETSIGTYPE void 57# define RETSIGTYPE void
52#endif 58#endif
53 59
54#define local static
55
56typedef unsigned char uch; 60typedef unsigned char uch;
57typedef unsigned short ush; 61typedef unsigned short ush;
58typedef unsigned long ulg; 62typedef unsigned long ulg;
@@ -63,22 +67,16 @@ typedef unsigned long ulg;
63#define WARNING 2 67#define WARNING 2
64 68
65/* Compression methods (see algorithm.doc) */ 69/* Compression methods (see algorithm.doc) */
70/* Only STORED and DEFLATED are supported by this BusyBox module */
66#define STORED 0 71#define STORED 0
67#define COMPRESSED 1
68#define PACKED 2
69#define LZHED 3
70/* methods 4 to 7 reserved */ 72/* methods 4 to 7 reserved */
71#define DEFLATED 8 73#define DEFLATED 8
72#define MAX_METHODS 9
73static int method; /* compression method */ 74static int method; /* compression method */
74 75
75/* 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
76 * the various modules: 77 * the various modules:
77 * deflate: prev+head window d_buf l_buf outbuf 78 * deflate: prev+head window d_buf l_buf outbuf
78 * unlzw: tab_prefix tab_suffix stack inbuf outbuf 79 * unlzw: tab_prefix tab_suffix stack inbuf outbuf
79 * inflate: window inbuf
80 * unpack: window inbuf prefix_len
81 * unlzh: left+right window c_table inbuf c_len
82 * For compression, input is done in window[]. For decompression, output 80 * For compression, input is done in window[]. For decompression, output
83 * is done in window except for unlzw. 81 * is done in window except for unlzw.
84 */ 82 */
@@ -110,56 +108,27 @@ static int method; /* compression method */
110#endif 108#endif
111 109
112#ifdef DYN_ALLOC 110#ifdef DYN_ALLOC
113# define EXTERN(type, array) extern type * array 111# define DECLARE(type, array, size) static type * array
114# define DECLARE(type, array, size) type * array
115# define ALLOC(type, array, size) { \ 112# define ALLOC(type, array, size) { \
116 array = (type*)calloc((size_t)(((size)+1L)/2), 2*sizeof(type)); \ 113 array = (type*)calloc((size_t)(((size)+1L)/2), 2*sizeof(type)); \
117 if (array == NULL) error_msg(memory_exhausted); \ 114 if (array == NULL) error_msg(memory_exhausted); \
118 } 115 }
119# define FREE(array) {if (array != NULL) free(array), array=NULL;} 116# define FREE(array) {if (array != NULL) free(array), array=NULL;}
120#else 117#else
121# define EXTERN(type, array) extern type array[] 118# define DECLARE(type, array, size) static type array[size]
122# define DECLARE(type, array, size) type array[size]
123# define ALLOC(type, array, size) 119# define ALLOC(type, array, size)
124# define FREE(array) 120# define FREE(array)
125#endif 121#endif
126 122
127EXTERN(uch, inbuf); /* input buffer */
128EXTERN(uch, outbuf); /* output buffer */
129EXTERN(ush, d_buf); /* buffer for distances, see trees.c */
130EXTERN(uch, window); /* Sliding window and suffix table (unlzw) */
131#define tab_suffix window 123#define tab_suffix window
132#ifndef MAXSEG_64K 124#define tab_prefix prev /* hash link (see deflate.c) */
133# define tab_prefix prev /* hash link (see deflate.c) */ 125#define head (prev+WSIZE) /* hash head (see deflate.c) */
134# define head (prev+WSIZE) /* hash head (see deflate.c) */
135EXTERN(ush, tab_prefix); /* prefix code (see unlzw.c) */
136#else
137# define tab_prefix0 prev
138# define head tab_prefix1
139EXTERN(ush, tab_prefix0); /* prefix for even codes */
140EXTERN(ush, tab_prefix1); /* prefix for odd codes */
141#endif
142 126
143extern unsigned insize; /* valid bytes in inbuf */ 127static long bytes_in; /* number of input bytes */
144static unsigned inptr; /* index of next byte to be processed in inbuf */
145extern unsigned outcnt; /* bytes in output buffer */
146
147extern long bytes_in; /* number of input bytes */
148extern long bytes_out; /* number of output bytes */
149extern long header_bytes; /* number of bytes in gzip header */
150 128
151#define isize bytes_in 129#define isize bytes_in
152/* for compatibility with old zip sources (to be cleaned) */ 130/* for compatibility with old zip sources (to be cleaned) */
153 131
154extern int ifd; /* input file descriptor */
155extern int ofd; /* output file descriptor */
156extern char ifname[]; /* input file name or "stdin" */
157extern char ofname[]; /* output file name or "stdout" */
158extern char *progname; /* program name */
159
160extern long time_stamp; /* original time stamp (modification time) */
161extern long ifile_size; /* input file size, -1 for devices (debug only) */
162
163typedef int file_t; /* Do not use stdio */ 132typedef int file_t; /* Do not use stdio */
164 133
165#define NO_FILE (-1) /* in memory compression */ 134#define NO_FILE (-1) /* in memory compression */
@@ -177,7 +146,6 @@ typedef int file_t; /* Do not use stdio */
177#define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */ 146#define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
178#define ORIG_NAME 0x08 /* bit 3 set: original file name present */ 147#define ORIG_NAME 0x08 /* bit 3 set: original file name present */
179#define COMMENT 0x10 /* bit 4 set: file comment present */ 148#define COMMENT 0x10 /* bit 4 set: file comment present */
180#define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
181#define RESERVED 0xC0 /* bit 6,7: reserved */ 149#define RESERVED 0xC0 /* bit 6,7: reserved */
182 150
183/* internal file attribute */ 151/* internal file attribute */
@@ -203,25 +171,9 @@ typedef int file_t; /* Do not use stdio */
203 * distances are limited to MAX_DIST instead of WSIZE. 171 * distances are limited to MAX_DIST instead of WSIZE.
204 */ 172 */
205 173
206extern int decrypt; /* flag to turn on decryption */ 174/* put_byte is used for the compressed output */
207extern int exit_code; /* program exit code */
208extern int verbose; /* be verbose (-v) */
209extern int quiet; /* be quiet (-q) */
210extern int test; /* check .z file integrity */
211extern int save_orig_name; /* set if original name must be saved */
212
213#define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf(0))
214#define try_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf(1))
215
216/* put_byte is used for the compressed output, put_ubyte for the
217 * uncompressed output. However unlzw() uses window for its
218 * suffix table instead of its output buffer, so it does not use put_ubyte
219 * (to be cleaned up).
220 */
221#define put_byte(c) {outbuf[outcnt++]=(uch)(c); if (outcnt==OUTBUFSIZ)\ 175#define put_byte(c) {outbuf[outcnt++]=(uch)(c); if (outcnt==OUTBUFSIZ)\
222 flush_outbuf();} 176 flush_outbuf();}
223#define put_ubyte(c) {window[outcnt++]=(uch)(c); if (outcnt==WSIZE)\
224 flush_window();}
225 177
226/* Output a 16 bit value, lsb first */ 178/* Output a 16 bit value, lsb first */
227#define put_short(w) \ 179#define put_short(w) \
@@ -243,12 +195,6 @@ extern int save_orig_name; /* set if original name must be saved */
243#define seekable() 0 /* force sequential output */ 195#define seekable() 0 /* force sequential output */
244#define translate_eol 0 /* no option -a yet */ 196#define translate_eol 0 /* no option -a yet */
245 197
246#define tolow(c) (isupper(c) ? (c)-'A'+'a' : (c)) /* force to lower case */
247
248/* Macros for getting two-byte and four-byte header values */
249#define SH(p) ((ush)(uch)((p)[0]) | ((ush)(uch)((p)[1]) << 8))
250#define LG(p) ((ulg)(SH(p)) | ((ulg)(SH((p)+2)) << 16))
251
252/* Diagnostic functions */ 198/* Diagnostic functions */
253#ifdef DEBUG 199#ifdef DEBUG
254# define Assert(cond,msg) {if(!(cond)) error_msg(msg);} 200# define Assert(cond,msg) {if(!(cond)) error_msg(msg);}
@@ -269,56 +215,38 @@ extern int save_orig_name; /* set if original name must be saved */
269#define WARN(msg) {if (!quiet) fprintf msg ; \ 215#define WARN(msg) {if (!quiet) fprintf msg ; \
270 if (exit_code == OK) exit_code = WARNING;} 216 if (exit_code == OK) exit_code = WARNING;}
271 217
218#ifndef MAX_PATH_LEN
219# define MAX_PATH_LEN 1024 /* max pathname length */
220#endif
221
222
272 223
273 /* in zip.c: */ 224 /* from zip.c: */
274extern int zip (int in, int out); 225static int zip (int in, int out);
275extern int file_read (char *buf, unsigned size); 226static int file_read (char *buf, unsigned size);
276 227
277 /* in unzip.c */ 228 /* from gzip.c */
278extern int check_zipfile (int in); 229static RETSIGTYPE abort_gzip (void);
279 230
280 /* in unpack.c */ 231 /* from deflate.c */
281extern int unpack (int in, int out); 232static void lm_init (ush * flags);
282 233static ulg deflate (void);
283 /* in unlzh.c */ 234
284extern int unlzh (int in, int out); 235 /* from trees.c */
285 236static void ct_init (ush * attr, int *methodp);
286 /* in gzip.c */ 237static int ct_tally (int dist, int lc);
287RETSIGTYPE abort_gzip (void); 238static ulg flush_block (char *buf, ulg stored_len, int eof);
288 239
289 /* in deflate.c */ 240 /* from bits.c */
290void lm_init (ush * flags); 241static void bi_init (file_t zipfile);
291ulg deflate (void); 242static void send_bits (int value, int length);
292 243static unsigned bi_reverse (unsigned value, int length);
293 /* in trees.c */ 244static void bi_windup (void);
294void ct_init (ush * attr, int *methodp); 245static void copy_block (char *buf, unsigned len, int header);
295int ct_tally (int dist, int lc); 246static int (*read_buf) (char *buf, unsigned size);
296ulg flush_block (char *buf, ulg stored_len, int eof); 247
297 248 /* from util.c: */
298 /* in bits.c */ 249static void flush_outbuf (void);
299void bi_init (file_t zipfile);
300void send_bits (int value, int length);
301unsigned bi_reverse (unsigned value, int length);
302void bi_windup (void);
303void copy_block (char *buf, unsigned len, int header);
304extern int (*read_buf) (char *buf, unsigned size);
305
306 /* in util.c: */
307extern int copy (int in, int out);
308//extern ulg updcrc (uch * s, unsigned n);
309//extern void clear_bufs (void);
310extern int fill_inbuf (int eof_ok);
311extern void flush_outbuf (void);
312extern void flush_window (void);
313//extern void write_buf (int fd, void * buf, unsigned cnt);
314extern char *strlwr (char *s);
315extern char *add_envopt (int *argcp, char ***argvp, char *env);
316//extern void read_error_msg (void);
317//extern void write_error_msg (void);
318extern void display_ratio (long num, long den, FILE * file);
319
320 /* in inflate.c */
321extern int inflate (void);
322 250
323/* lzw.h -- define the lzw functions. 251/* lzw.h -- define the lzw functions.
324 * Copyright (C) 1992-1993 Jean-loup Gailly. 252 * Copyright (C) 1992-1993 Jean-loup Gailly.
@@ -345,34 +273,6 @@ extern int inflate (void);
345 * "can only handle 16 bits". 273 * "can only handle 16 bits".
346 */ 274 */
347 275
348#define BLOCK_MODE 0x80
349/* Block compression: if table is full and compression rate is dropping,
350 * clear the dictionary.
351 */
352
353#define LZW_RESERVED 0x60 /* reserved bits */
354
355#define CLEAR 256 /* flush the dictionary */
356#define FIRST (CLEAR+1) /* first free entry */
357
358extern int maxbits; /* max bits per code for LZW */
359extern int block_mode; /* block compress mode -C compatible with 2.0 */
360
361/* revision.h -- define the version number
362 * Copyright (C) 1992-1993 Jean-loup Gailly.
363 * This is free software; you can redistribute it and/or modify it under the
364 * terms of the GNU General Public License, see the file COPYING.
365 */
366
367#define VERSION "1.2.4"
368#define PATCHLEVEL 0
369#define REVDATE "18 Aug 93"
370
371/* This version does not support compression into old compress format: */
372#ifdef LZW
373# undef LZW
374#endif
375
376/* tailor.h -- target dependent definitions 276/* tailor.h -- target dependent definitions
377 * Copyright (C) 1992-1993 Jean-loup Gailly. 277 * Copyright (C) 1992-1993 Jean-loup Gailly.
378 * This is free software; you can redistribute it and/or modify it under the 278 * This is free software; you can redistribute it and/or modify it under the
@@ -384,259 +284,6 @@ extern int block_mode; /* block compress mode -C compatible with 2.0 */
384 */ 284 */
385 285
386 286
387#if defined(__MSDOS__) && !defined(MSDOS)
388# define MSDOS
389#endif
390
391#if defined(__OS2__) && !defined(OS2)
392# define OS2
393#endif
394
395#if defined(OS2) && defined(MSDOS) /* MS C under OS/2 */
396# undef MSDOS
397#endif
398
399#ifdef MSDOS
400# ifdef __GNUC__
401 /* DJGPP version 1.09+ on MS-DOS.
402 * The DJGPP 1.09 stat() function must be upgraded before gzip will
403 * fully work.
404 * No need for DIRENT, since <unistd.h> defines POSIX_SOURCE which
405 * implies DIRENT.
406 */
407# define near
408# else
409# define MAXSEG_64K
410# ifdef __TURBOC__
411# define NO_OFF_T
412# ifdef __BORLANDC__
413# define DIRENT
414# else
415# define NO_UTIME
416# endif
417# else /* MSC */
418# define HAVE_SYS_UTIME_H
419# define NO_UTIME_H
420# endif
421# endif
422# define PATH_SEP2 '\\'
423# define PATH_SEP3 ':'
424# define MAX_PATH_LEN 128
425# define NO_MULTIPLE_DOTS
426# define MAX_EXT_CHARS 3
427# define Z_SUFFIX "z"
428# define NO_CHOWN
429# define PROTO
430# define STDC_HEADERS
431# define NO_SIZE_CHECK
432# define casemap(c) tolow(c) /* Force file names to lower case */
433# include <io.h>
434# define OS_CODE 0x00
435# define SET_BINARY_MODE(fd) setmode(fd, O_BINARY)
436# if !defined(NO_ASM) && !defined(ASMV)
437# define ASMV
438# endif
439#else
440# define near
441#endif
442
443#ifdef OS2
444# define PATH_SEP2 '\\'
445# define PATH_SEP3 ':'
446# define MAX_PATH_LEN 260
447# ifdef OS2FAT
448# define NO_MULTIPLE_DOTS
449# define MAX_EXT_CHARS 3
450# define Z_SUFFIX "z"
451# define casemap(c) tolow(c)
452# endif
453# define NO_CHOWN
454# define PROTO
455# define STDC_HEADERS
456# include <io.h>
457# define OS_CODE 0x06
458# define SET_BINARY_MODE(fd) setmode(fd, O_BINARY)
459# ifdef _MSC_VER
460# define HAVE_SYS_UTIME_H
461# define NO_UTIME_H
462# define MAXSEG_64K
463# undef near
464# define near _near
465# endif
466# ifdef __EMX__
467# define HAVE_SYS_UTIME_H
468# define NO_UTIME_H
469# define DIRENT
470# define EXPAND(argc,argv) \
471 {_response(&argc, &argv); _wildcard(&argc, &argv);}
472# endif
473# ifdef __BORLANDC__
474# define DIRENT
475# endif
476# ifdef __ZTC__
477# define NO_DIR
478# define NO_UTIME_H
479# include <dos.h>
480# define EXPAND(argc,argv) \
481 {response_expand(&argc, &argv);}
482# endif
483#endif
484
485#ifdef WIN32 /* Windows NT */
486# define HAVE_SYS_UTIME_H
487# define NO_UTIME_H
488# define PATH_SEP2 '\\'
489# define PATH_SEP3 ':'
490# define MAX_PATH_LEN 260
491# define NO_CHOWN
492# define PROTO
493# define STDC_HEADERS
494# define SET_BINARY_MODE(fd) setmode(fd, O_BINARY)
495# include <io.h>
496# include <malloc.h>
497# ifdef NTFAT
498# define NO_MULTIPLE_DOTS
499# define MAX_EXT_CHARS 3
500# define Z_SUFFIX "z"
501# define casemap(c) tolow(c) /* Force file names to lower case */
502# endif
503# define OS_CODE 0x0b
504#endif
505
506#ifdef MSDOS
507# ifdef __TURBOC__
508# include <alloc.h>
509# define DYN_ALLOC
510 /* Turbo C 2.0 does not accept static allocations of large arrays */
511void *fcalloc(unsigned items, unsigned size);
512void fcfree(void *ptr);
513# else /* MSC */
514# include <malloc.h>
515# define fcalloc(nitems,itemsize) halloc((long)(nitems),(itemsize))
516# define fcfree(ptr) hfree(ptr)
517# endif
518#else
519# ifdef MAXSEG_64K
520# define fcalloc(items,size) calloc((items),(size))
521# else
522# define fcalloc(items,size) malloc((size_t)(items)*(size_t)(size))
523# endif
524# define fcfree(ptr) free(ptr)
525#endif
526
527#if defined(VAXC) || defined(VMS)
528# define PATH_SEP ']'
529# define PATH_SEP2 ':'
530# define SUFFIX_SEP ';'
531# define NO_MULTIPLE_DOTS
532# define Z_SUFFIX "-gz"
533# define RECORD_IO 1
534# define casemap(c) tolow(c)
535# define OS_CODE 0x02
536# define OPTIONS_VAR "GZIP_OPT"
537# define STDC_HEADERS
538# define NO_UTIME
539# define EXPAND(argc,argv) vms_expand_args(&argc,&argv);
540# include <file.h>
541# define unlink delete
542# ifdef VAXC
543# define NO_FCNTL_H
544# include <unixio.h>
545# endif
546#endif
547
548#ifdef AMIGA
549# define PATH_SEP2 ':'
550# define STDC_HEADERS
551# define OS_CODE 0x01
552# define ASMV
553# ifdef __GNUC__
554# define DIRENT
555# define HAVE_UNISTD_H
556# else /* SASC */
557# define NO_STDIN_FSTAT
558# define SYSDIR
559# define NO_SYMLINK
560# define NO_CHOWN
561# define NO_FCNTL_H
562# include <fcntl.h> /* for read() and write() */
563# define direct dirent
564extern void _expand_args(int *argc, char ***argv);
565
566# define EXPAND(argc,argv) _expand_args(&argc,&argv);
567# undef O_BINARY /* disable useless --ascii option */
568# endif
569#endif
570
571#if defined(ATARI) || defined(atarist)
572# ifndef STDC_HEADERS
573# define STDC_HEADERS
574# define HAVE_UNISTD_H
575# define DIRENT
576# endif
577# define ASMV
578# define OS_CODE 0x05
579# ifdef TOSFS
580# define PATH_SEP2 '\\'
581# define PATH_SEP3 ':'
582# define MAX_PATH_LEN 128
583# define NO_MULTIPLE_DOTS
584# define MAX_EXT_CHARS 3
585# define Z_SUFFIX "z"
586# define NO_CHOWN
587# define casemap(c) tolow(c) /* Force file names to lower case */
588# define NO_SYMLINK
589# endif
590#endif
591
592#ifdef MACOS
593# define PATH_SEP ':'
594# define DYN_ALLOC
595# define PROTO
596# define NO_STDIN_FSTAT
597# define NO_CHOWN
598# define NO_UTIME
599# define chmod(file, mode) (0)
600# define OPEN(name, flags, mode) open(name, flags)
601# define OS_CODE 0x07
602# ifdef MPW
603# define isatty(fd) ((fd) <= 2)
604# endif
605#endif
606
607#ifdef __50SERIES /* Prime/PRIMOS */
608# define PATH_SEP '>'
609# define STDC_HEADERS
610# define NO_MEMORY_H
611# define NO_UTIME_H
612# define NO_UTIME
613# define NO_CHOWN
614# define NO_STDIN_FSTAT
615# define NO_SIZE_CHECK
616# define NO_SYMLINK
617# define RECORD_IO 1
618# define casemap(c) tolow(c) /* Force file names to lower case */
619# define put_char(c) put_byte((c) & 0x7F)
620# define get_char(c) ascii2pascii(get_byte())
621# define OS_CODE 0x0F /* temporary, subject to change */
622# ifdef SIGTERM
623# undef SIGTERM /* We don't want a signal handler for SIGTERM */
624# endif
625#endif
626
627#if defined(pyr) && !defined(NOMEMCPY) /* Pyramid */
628# define NOMEMCPY /* problem with overlapping copies */
629#endif
630
631#ifdef TOPS20
632# define OS_CODE 0x0a
633#endif
634
635#ifndef unix
636# define NO_ST_INO /* don't rely on inode numbers */
637#endif
638
639
640 /* Common defaults */ 287 /* Common defaults */
641 288
642#ifndef OS_CODE 289#ifndef OS_CODE
@@ -647,10 +294,6 @@ extern void _expand_args(int *argc, char ***argv);
647# define PATH_SEP '/' 294# define PATH_SEP '/'
648#endif 295#endif
649 296
650#ifndef casemap
651# define casemap(c) (c)
652#endif
653
654#ifndef OPTIONS_VAR 297#ifndef OPTIONS_VAR
655# define OPTIONS_VAR "GZIP" 298# define OPTIONS_VAR "GZIP"
656#endif 299#endif
@@ -665,49 +308,36 @@ extern void _expand_args(int *argc, char ***argv);
665# define MAX_SUFFIX 30 308# define MAX_SUFFIX 30
666#endif 309#endif
667 310
668#ifndef MAKE_LEGAL_NAME 311 /* global buffers */
669# ifdef NO_MULTIPLE_DOTS
670# define MAKE_LEGAL_NAME(name) make_simple_name(name)
671# else
672# define MAKE_LEGAL_NAME(name)
673# endif
674#endif
675
676#ifndef MIN_PART
677# define MIN_PART 3
678 /* keep at least MIN_PART chars between dots in a file name. */
679#endif
680
681#ifndef EXPAND
682# define EXPAND(argc,argv)
683#endif
684
685#ifndef RECORD_IO
686# define RECORD_IO 0
687#endif
688
689#ifndef SET_BINARY_MODE
690# define SET_BINARY_MODE(fd)
691#endif
692 312
693#ifndef OPEN 313DECLARE(uch, inbuf, INBUFSIZ + INBUF_EXTRA);
694# define OPEN(name, flags, mode) open(name, flags, mode) 314DECLARE(uch, outbuf, OUTBUFSIZ + OUTBUF_EXTRA);
695#endif 315DECLARE(ush, d_buf, DIST_BUFSIZE);
316DECLARE(uch, window, 2L * WSIZE);
317DECLARE(ush, tab_prefix, 1L << BITS);
696 318
697#ifndef get_char 319static int crc_table_empty = 1;
698# define get_char() get_byte()
699#endif
700 320
701#ifndef put_char 321static int foreground; /* set if program run in foreground */
702# define put_char(c) put_byte(c) 322static int method = DEFLATED; /* compression method */
703#endif 323static int exit_code = OK; /* program exit code */
324static int part_nb; /* number of parts in .gz file */
325static long time_stamp; /* original time stamp (modification time) */
326static long ifile_size; /* input file size, -1 for devices (debug only) */
327static char z_suffix[MAX_SUFFIX + 1]; /* default suffix (can be set with --suffix) */
328static int z_len; /* strlen(z_suffix) */
704 329
705int crc_table_empty = 1; 330static char ifname[MAX_PATH_LEN]; /* input file name */
331static char ofname[MAX_PATH_LEN]; /* output file name */
332static int ifd; /* input file descriptor */
333static int ofd; /* output file descriptor */
334static unsigned insize; /* valid bytes in inbuf */
335static unsigned outcnt; /* bytes in output buffer */
706 336
707/* ======================================================================== 337/* ========================================================================
708 * Signal and error handler. 338 * Signal and error handler.
709 */ 339 */
710void abort_gzip() 340static void abort_gzip()
711{ 341{
712 exit(ERROR); 342 exit(ERROR);
713} 343}
@@ -718,8 +348,8 @@ void abort_gzip()
718static void clear_bufs(void) 348static void clear_bufs(void)
719{ 349{
720 outcnt = 0; 350 outcnt = 0;
721 insize = inptr = 0; 351 insize = 0;
722 bytes_in = bytes_out = 0L; 352 bytes_in = 0L;
723} 353}
724 354
725static void write_error_msg() 355static void write_error_msg()
@@ -733,10 +363,7 @@ static void write_error_msg()
733 * Does the same as write(), but also handles partial pipe writes and checks 363 * Does the same as write(), but also handles partial pipe writes and checks
734 * for error return. 364 * for error return.
735 */ 365 */
736static void write_buf(fd, buf, cnt) 366static void write_buf(int fd, void *buf, unsigned cnt)
737int fd;
738void * buf;
739unsigned cnt;
740{ 367{
741 unsigned n; 368 unsigned n;
742 369
@@ -749,43 +376,26 @@ unsigned cnt;
749 } 376 }
750} 377}
751 378
752/* ========================================================================
753 * Error handlers.
754 */
755static void read_error_msg()
756{
757 fprintf(stderr, "\n");
758 if (errno != 0) {
759 perror("");
760 } else {
761 fprintf(stderr, "unexpected end of file\n");
762 }
763 abort_gzip();
764}
765
766/* =========================================================================== 379/* ===========================================================================
767 * Run a set of bytes through the crc shift register. If s is a NULL 380 * Run a set of bytes through the crc shift register. If s is a NULL
768 * pointer, then initialize the crc shift register contents instead. 381 * pointer, then initialize the crc shift register contents instead.
769 * Return the current crc in either case. 382 * Return the current crc in either case.
770 */ 383 */
771static ulg updcrc(s, n) 384static ulg updcrc(uch *s, unsigned n)
772uch *s; /* pointer to bytes to pump through */
773unsigned n; /* number of bytes in s[] */
774{ 385{
775 static ulg crc = (ulg) 0xffffffffL; /* shift register contents */ 386 static ulg crc = (ulg) 0xffffffffL; /* shift register contents */
776 register ulg c; /* temporary variable */ 387 register ulg c; /* temporary variable */
777 static unsigned long crc_32_tab[256]; 388 static unsigned long crc_32_tab[256];
778 if (crc_table_empty) { 389 if (crc_table_empty) {
779 unsigned long csr; /* crc shift register */ 390 unsigned long csr; /* crc shift register */
780 unsigned long e; /* polynomial exclusive-or pattern */ 391 unsigned long e=0; /* polynomial exclusive-or pattern */
781 int i; /* counter for all possible eight bit values */ 392 int i; /* counter for all possible eight bit values */
782 int k; /* byte being shifted into crc apparatus */ 393 int k; /* byte being shifted into crc apparatus */
783 394
784 /* terms of polynomial defining this crc (except x^32): */ 395 /* terms of polynomial defining this crc (except x^32): */
785 static int p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26}; 396 static const int p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};
786 397
787 /* Make exclusive-or pattern from polynomial (0xedb88320) */ 398 /* Make exclusive-or pattern from polynomial (0xedb88320) */
788 e = 0;
789 for (i = 0; i < sizeof(p)/sizeof(int); i++) 399 for (i = 0; i < sizeof(p)/sizeof(int); i++)
790 e |= 1L << (31 - p[i]); 400 e |= 1L << (31 - p[i]);
791 401
@@ -868,17 +478,13 @@ unsigned n; /* number of bytes in s[] */
868 * 478 *
869 */ 479 */
870 480
871#ifdef DEBUG
872# include <stdio.h>
873#endif
874
875/* =========================================================================== 481/* ===========================================================================
876 * Local data used by the "bit string" routines. 482 * Local data used by the "bit string" routines.
877 */ 483 */
878 484
879local file_t zfile; /* output gzip file */ 485static file_t zfile; /* output gzip file */
880 486
881local unsigned short bi_buf; 487static unsigned short bi_buf;
882 488
883/* Output buffer. bits are inserted starting at the bottom (least significant 489/* Output buffer. bits are inserted starting at the bottom (least significant
884 * bits). 490 * bits).
@@ -889,13 +495,7 @@ local unsigned short bi_buf;
889 * more than 16 bits on some systems.) 495 * more than 16 bits on some systems.)
890 */ 496 */
891 497
892local int bi_valid; 498static int bi_valid;
893
894/* Number of valid bits in bi_buf. All bits above the last valid bit
895 * are always zero.
896 */
897
898int (*read_buf) (char *buf, unsigned size);
899 499
900/* Current input function. Set to mem_read for in-memory compression */ 500/* Current input function. Set to mem_read for in-memory compression */
901 501
@@ -906,8 +506,7 @@ ulg bits_sent; /* bit length of the compressed data */
906/* =========================================================================== 506/* ===========================================================================
907 * Initialize the bit string routines. 507 * Initialize the bit string routines.
908 */ 508 */
909void bi_init(zipfile) 509static void bi_init(file_t zipfile)
910file_t zipfile; /* output zip file, NO_FILE for in-memory compression */
911{ 510{
912 zfile = zipfile; 511 zfile = zipfile;
913 bi_buf = 0; 512 bi_buf = 0;
@@ -928,9 +527,7 @@ file_t zipfile; /* output zip file, NO_FILE for in-memory compression */
928 * Send a value on a given number of bits. 527 * Send a value on a given number of bits.
929 * IN assertion: length <= 16 and value fits in length bits. 528 * IN assertion: length <= 16 and value fits in length bits.
930 */ 529 */
931void send_bits(value, length) 530static void send_bits(int value, int length)
932int value; /* value to send */
933int length; /* number of bits */
934{ 531{
935#ifdef DEBUG 532#ifdef DEBUG
936 Tracev((stderr, " l %2d v %4x ", length, value)); 533 Tracev((stderr, " l %2d v %4x ", length, value));
@@ -957,9 +554,7 @@ int length; /* number of bits */
957 * method would use a table) 554 * method would use a table)
958 * IN assertion: 1 <= len <= 15 555 * IN assertion: 1 <= len <= 15
959 */ 556 */
960unsigned bi_reverse(code, len) 557static unsigned bi_reverse(unsigned code, int len)
961unsigned code; /* the value to invert */
962int len; /* its bit length */
963{ 558{
964 register unsigned res = 0; 559 register unsigned res = 0;
965 560
@@ -973,7 +568,7 @@ int len; /* its bit length */
973/* =========================================================================== 568/* ===========================================================================
974 * Write out any remaining bits in an incomplete byte. 569 * Write out any remaining bits in an incomplete byte.
975 */ 570 */
976void bi_windup() 571static void bi_windup()
977{ 572{
978 if (bi_valid > 8) { 573 if (bi_valid > 8) {
979 put_short(bi_buf); 574 put_short(bi_buf);
@@ -991,10 +586,7 @@ void bi_windup()
991 * Copy a stored block to the zip file, storing first the length and its 586 * Copy a stored block to the zip file, storing first the length and its
992 * one's complement if requested. 587 * one's complement if requested.
993 */ 588 */
994void copy_block(buf, len, header) 589static void copy_block(char *buf, unsigned len, int header)
995char *buf; /* the input data */
996unsigned len; /* its length */
997int header; /* true if block header must be written */
998{ 590{
999 bi_windup(); /* align on byte boundary */ 591 bi_windup(); /* align on byte boundary */
1000 592
@@ -1009,12 +601,6 @@ int header; /* true if block header must be written */
1009 bits_sent += (ulg) len << 3; 601 bits_sent += (ulg) len << 3;
1010#endif 602#endif
1011 while (len--) { 603 while (len--) {
1012#ifdef CRYPT
1013 int t;
1014
1015 if (key)
1016 zencode(*buf, t);
1017#endif
1018 put_byte(*buf++); 604 put_byte(*buf++);
1019 } 605 }
1020} 606}
@@ -1082,7 +668,6 @@ int header; /* true if block header must be written */
1082 * attributes. 668 * attributes.
1083 */ 669 */
1084 670
1085#include <stdio.h>
1086 671
1087/* =========================================================================== 672/* ===========================================================================
1088 * Configuration parameters 673 * Configuration parameters
@@ -1111,10 +696,10 @@ int header; /* true if block header must be written */
1111 * window with tab_suffix. Check that we can do this: 696 * window with tab_suffix. Check that we can do this:
1112 */ 697 */
1113#if (WSIZE<<1) > (1<<BITS) 698#if (WSIZE<<1) > (1<<BITS)
1114error:cannot overlay window with tab_suffix and prev with tab_prefix0 699# error cannot overlay window with tab_suffix and prev with tab_prefix0
1115#endif 700#endif
1116#if HASH_BITS > BITS-1 701#if HASH_BITS > BITS-1
1117error:cannot overlay head with tab_prefix1 702# error cannot overlay head with tab_prefix1
1118#endif 703#endif
1119#define HASH_SIZE (unsigned)(1<<HASH_BITS) 704#define HASH_SIZE (unsigned)(1<<HASH_BITS)
1120#define HASH_MASK (HASH_SIZE-1) 705#define HASH_MASK (HASH_SIZE-1)
@@ -1159,19 +744,19 @@ typedef unsigned IPos;
1159/* DECLARE(Pos, head, 1<<HASH_BITS); */ 744/* DECLARE(Pos, head, 1<<HASH_BITS); */
1160/* Heads of the hash chains or NIL. */ 745/* Heads of the hash chains or NIL. */
1161 746
1162ulg window_size = (ulg) 2 * WSIZE; 747static const ulg window_size = (ulg) 2 * WSIZE;
1163 748
1164/* window size, 2*WSIZE except for MMAP or BIG_MEM, where it is the 749/* window size, 2*WSIZE except for MMAP or BIG_MEM, where it is the
1165 * input file length plus MIN_LOOKAHEAD. 750 * input file length plus MIN_LOOKAHEAD.
1166 */ 751 */
1167 752
1168long block_start; 753static long block_start;
1169 754
1170/* window position at the beginning of the current output block. Gets 755/* window position at the beginning of the current output block. Gets
1171 * negative when the window is moved backwards. 756 * negative when the window is moved backwards.
1172 */ 757 */
1173 758
1174local unsigned ins_h; /* hash index of string to be inserted */ 759static unsigned ins_h; /* hash index of string to be inserted */
1175 760
1176#define H_SHIFT ((HASH_BITS+MIN_MATCH-1)/MIN_MATCH) 761#define H_SHIFT ((HASH_BITS+MIN_MATCH-1)/MIN_MATCH)
1177/* Number of bits by which ins_h and del_h must be shifted at each 762/* Number of bits by which ins_h and del_h must be shifted at each
@@ -1180,24 +765,24 @@ local unsigned ins_h; /* hash index of string to be inserted */
1180 * H_SHIFT * MIN_MATCH >= HASH_BITS 765 * H_SHIFT * MIN_MATCH >= HASH_BITS
1181 */ 766 */
1182 767
1183unsigned int near prev_length; 768static unsigned int prev_length;
1184 769
1185/* Length of the best match at previous step. Matches not greater than this 770/* Length of the best match at previous step. Matches not greater than this
1186 * are discarded. This is used in the lazy match evaluation. 771 * are discarded. This is used in the lazy match evaluation.
1187 */ 772 */
1188 773
1189unsigned near strstart; /* start of string to insert */ 774static unsigned strstart; /* start of string to insert */
1190unsigned near match_start; /* start of matching string */ 775static unsigned match_start; /* start of matching string */
1191local int eofile; /* flag set at end of input file */ 776static int eofile; /* flag set at end of input file */
1192local unsigned lookahead; /* number of valid bytes ahead in window */ 777static unsigned lookahead; /* number of valid bytes ahead in window */
1193 778
1194unsigned near max_chain_length; 779static const unsigned max_chain_length=4096;
1195 780
1196/* To speed up deflation, hash chains are never searched beyond this length. 781/* To speed up deflation, hash chains are never searched beyond this length.
1197 * A higher limit improves compression ratio but degrades the speed. 782 * A higher limit improves compression ratio but degrades the speed.
1198 */ 783 */
1199 784
1200local unsigned int max_lazy_match; 785static const unsigned int max_lazy_match=258;
1201 786
1202/* Attempt to find a better match only when the current match is strictly 787/* Attempt to find a better match only when the current match is strictly
1203 * smaller than this value. This mechanism is used only for compression 788 * smaller than this value. This mechanism is used only for compression
@@ -1209,7 +794,7 @@ local unsigned int max_lazy_match;
1209 * max_insert_length is used only for compression levels <= 3. 794 * max_insert_length is used only for compression levels <= 3.
1210 */ 795 */
1211 796
1212unsigned near good_match; 797static const unsigned good_match=32;
1213 798
1214/* Use a faster search when the previous match is longer than this */ 799/* Use a faster search when the previous match is longer than this */
1215 800
@@ -1220,22 +805,7 @@ unsigned near good_match;
1220 * found for specific files. 805 * found for specific files.
1221 */ 806 */
1222 807
1223typedef struct config { 808static const int nice_match=258; /* Stop searching when current match exceeds this */
1224 ush good_length; /* reduce lazy search above this match length */
1225 ush max_lazy; /* do not perform lazy search above this match length */
1226 ush nice_length; /* quit search above this match length */
1227 ush max_chain;
1228} config;
1229
1230#ifdef FULL_SEARCH
1231# define nice_match MAX_MATCH
1232#else
1233int near nice_match; /* Stop searching when current match exceeds this */
1234#endif
1235
1236local config configuration_table =
1237 /* 9 */ { 32, 258, 258, 4096 };
1238 /* maximum compression */
1239 809
1240/* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4 810/* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4
1241 * For deflate_fast() (levels <= 3) good is ignored and lazy has a different 811 * For deflate_fast() (levels <= 3) good is ignored and lazy has a different
@@ -1248,16 +818,12 @@ local config configuration_table =
1248/* =========================================================================== 818/* ===========================================================================
1249 * Prototypes for local functions. 819 * Prototypes for local functions.
1250 */ 820 */
1251local void fill_window (void); 821static void fill_window (void);
1252
1253int longest_match (IPos cur_match);
1254 822
1255#ifdef ASMV 823static int longest_match (IPos cur_match);
1256void match_init (void); /* asm code initialization */
1257#endif
1258 824
1259#ifdef DEBUG 825#ifdef DEBUG
1260local void check_match (IPos start, IPos match, int length); 826static void check_match (IPos start, IPos match, int length);
1261#endif 827#endif
1262 828
1263/* =========================================================================== 829/* ===========================================================================
@@ -1284,36 +850,19 @@ local void check_match (IPos start, IPos match, int length);
1284/* =========================================================================== 850/* ===========================================================================
1285 * Initialize the "longest match" routines for a new file 851 * Initialize the "longest match" routines for a new file
1286 */ 852 */
1287void lm_init(flags) 853static void lm_init(ush *flags)
1288ush *flags; /* general purpose bit flag */
1289{ 854{
1290 register unsigned j; 855 register unsigned j;
1291 856
1292 /* Initialize the hash table. */ 857 /* Initialize the hash table. */
1293#if defined(MAXSEG_64K) && HASH_BITS == 15
1294 for (j = 0; j < HASH_SIZE; j++)
1295 head[j] = NIL;
1296#else
1297 memzero((char *) head, HASH_SIZE * sizeof(*head)); 858 memzero((char *) head, HASH_SIZE * sizeof(*head));
1298#endif
1299 /* prev will be initialized on the fly */ 859 /* prev will be initialized on the fly */
1300 860
1301 /* Set the default configuration parameters:
1302 */
1303 max_lazy_match = configuration_table.max_lazy;
1304 good_match = configuration_table.good_length;
1305#ifndef FULL_SEARCH
1306 nice_match = configuration_table.nice_length;
1307#endif
1308 max_chain_length = configuration_table.max_chain;
1309 *flags |= SLOW; 861 *flags |= SLOW;
1310 /* ??? reduce max_chain_length for binary files */ 862 /* ??? reduce max_chain_length for binary files */
1311 863
1312 strstart = 0; 864 strstart = 0;
1313 block_start = 0L; 865 block_start = 0L;
1314#ifdef ASMV
1315 match_init(); /* initialize the asm code */
1316#endif
1317 866
1318 lookahead = read_buf((char *) window, 867 lookahead = read_buf((char *) window,
1319 sizeof(int) <= 2 ? (unsigned) WSIZE : 2 * WSIZE); 868 sizeof(int) <= 2 ? (unsigned) WSIZE : 2 * WSIZE);
@@ -1345,13 +894,12 @@ ush *flags; /* general purpose bit flag */
1345 * IN assertions: cur_match is the head of the hash chain for the current 894 * IN assertions: cur_match is the head of the hash chain for the current
1346 * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1 895 * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
1347 */ 896 */
1348#ifndef ASMV 897
1349/* For MSDOS, OS/2 and 386 Unix, an optimized version is in match.asm or 898/* For MSDOS, OS/2 and 386 Unix, an optimized version is in match.asm or
1350 * match.s. The code is functionally equivalent, so you can use the C version 899 * match.s. The code is functionally equivalent, so you can use the C version
1351 * if desired. 900 * if desired.
1352 */ 901 */
1353int longest_match(cur_match) 902static int longest_match(IPos cur_match)
1354IPos cur_match; /* current match */
1355{ 903{
1356 unsigned chain_length = max_chain_length; /* max hash chain length */ 904 unsigned chain_length = max_chain_length; /* max hash chain length */
1357 register uch *scan = window + strstart; /* current string */ 905 register uch *scan = window + strstart; /* current string */
@@ -1369,20 +917,11 @@ IPos cur_match; /* current match */
1369 * It is easy to get rid of this optimization if necessary. 917 * It is easy to get rid of this optimization if necessary.
1370 */ 918 */
1371#if HASH_BITS < 8 || MAX_MATCH != 258 919#if HASH_BITS < 8 || MAX_MATCH != 258
1372 error:Code too clever 920# error Code too clever
1373#endif 921#endif
1374#ifdef UNALIGNED_OK
1375 /* Compare two bytes at a time. Note: this is not always beneficial.
1376 * Try with and without -DUNALIGNED_OK to check.
1377 */
1378 register uch *strend = window + strstart + MAX_MATCH - 1;
1379 register ush scan_start = *(ush *) scan;
1380 register ush scan_end = *(ush *) (scan + best_len - 1);
1381#else
1382 register uch *strend = window + strstart + MAX_MATCH; 922 register uch *strend = window + strstart + MAX_MATCH;
1383 register uch scan_end1 = scan[best_len - 1]; 923 register uch scan_end1 = scan[best_len - 1];
1384 register uch scan_end = scan[best_len]; 924 register uch scan_end = scan[best_len];
1385#endif
1386 925
1387 /* Do not waste too much time if we already have a good match: */ 926 /* Do not waste too much time if we already have a good match: */
1388 if (prev_length >= good_match) { 927 if (prev_length >= good_match) {
@@ -1398,42 +937,6 @@ IPos cur_match; /* current match */
1398 /* Skip to next match if the match length cannot increase 937 /* Skip to next match if the match length cannot increase
1399 * or if the match length is less than 2: 938 * or if the match length is less than 2:
1400 */ 939 */
1401#if (defined(UNALIGNED_OK) && MAX_MATCH == 258)
1402 /* This code assumes sizeof(unsigned short) == 2. Do not use
1403 * UNALIGNED_OK if your compiler uses a different size.
1404 */
1405 if (*(ush *) (match + best_len - 1) != scan_end ||
1406 *(ush *) match != scan_start)
1407 continue;
1408
1409 /* It is not necessary to compare scan[2] and match[2] since they are
1410 * always equal when the other bytes match, given that the hash keys
1411 * are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at
1412 * strstart+3, +5, ... up to strstart+257. We check for insufficient
1413 * lookahead only every 4th comparison; the 128th check will be made
1414 * at strstart+257. If MAX_MATCH-2 is not a multiple of 8, it is
1415 * necessary to put more guard bytes at the end of the window, or
1416 * to check more often for insufficient lookahead.
1417 */
1418 scan++, match++;
1419 do {
1420 } while (*(ush *) (scan += 2) == *(ush *) (match += 2) &&
1421 *(ush *) (scan += 2) == *(ush *) (match += 2) &&
1422 *(ush *) (scan += 2) == *(ush *) (match += 2) &&
1423 *(ush *) (scan += 2) == *(ush *) (match += 2) &&
1424 scan < strend);
1425 /* The funny "do {}" generates better code on most compilers */
1426
1427 /* Here, scan <= window+strstart+257 */
1428 Assert(scan <= window + (unsigned) (window_size - 1), "wild scan");
1429 if (*scan == *match)
1430 scan++;
1431
1432 len = (MAX_MATCH - 1) - (int) (strend - scan);
1433 scan = strend - (MAX_MATCH - 1);
1434
1435#else /* UNALIGNED_OK */
1436
1437 if (match[best_len] != scan_end || 940 if (match[best_len] != scan_end ||
1438 match[best_len - 1] != scan_end1 || 941 match[best_len - 1] != scan_end1 ||
1439 *match != *scan || *++match != scan[1]) 942 *match != *scan || *++match != scan[1])
@@ -1460,34 +963,25 @@ IPos cur_match; /* current match */
1460 len = MAX_MATCH - (int) (strend - scan); 963 len = MAX_MATCH - (int) (strend - scan);
1461 scan = strend - MAX_MATCH; 964 scan = strend - MAX_MATCH;
1462 965
1463#endif /* UNALIGNED_OK */
1464
1465 if (len > best_len) { 966 if (len > best_len) {
1466 match_start = cur_match; 967 match_start = cur_match;
1467 best_len = len; 968 best_len = len;
1468 if (len >= nice_match) 969 if (len >= nice_match)
1469 break; 970 break;
1470#ifdef UNALIGNED_OK
1471 scan_end = *(ush *) (scan + best_len - 1);
1472#else
1473 scan_end1 = scan[best_len - 1]; 971 scan_end1 = scan[best_len - 1];
1474 scan_end = scan[best_len]; 972 scan_end = scan[best_len];
1475#endif
1476 } 973 }
1477 } while ((cur_match = prev[cur_match & WMASK]) > limit 974 } while ((cur_match = prev[cur_match & WMASK]) > limit
1478 && --chain_length != 0); 975 && --chain_length != 0);
1479 976
1480 return best_len; 977 return best_len;
1481} 978}
1482#endif /* ASMV */
1483 979
1484#ifdef DEBUG 980#ifdef DEBUG
1485/* =========================================================================== 981/* ===========================================================================
1486 * Check that the match at match_start is indeed a match. 982 * Check that the match at match_start is indeed a match.
1487 */ 983 */
1488local void check_match(start, match, length) 984static void check_match(IPos start, IPos match, int length)
1489IPos start, match;
1490int length;
1491{ 985{
1492 /* check that the match is indeed a match */ 986 /* check that the match is indeed a match */
1493 if (memcmp((char *) window + match, 987 if (memcmp((char *) window + match,
@@ -1515,7 +1009,7 @@ int length;
1515 * file reads are performed for at least two bytes (required for the 1009 * file reads are performed for at least two bytes (required for the
1516 * translate_eol option). 1010 * translate_eol option).
1517 */ 1011 */
1518local void fill_window() 1012static void fill_window()
1519{ 1013{
1520 register unsigned n, m; 1014 register unsigned n, m;
1521 unsigned more = 1015 unsigned more =
@@ -1580,7 +1074,7 @@ local void fill_window()
1580 * evaluation for matches: a match is finally adopted only if there is 1074 * evaluation for matches: a match is finally adopted only if there is
1581 * no better match at the next window position. 1075 * no better match at the next window position.
1582 */ 1076 */
1583ulg deflate() 1077static ulg deflate()
1584{ 1078{
1585 IPos hash_head; /* head of hash chain */ 1079 IPos hash_head; /* head of hash chain */
1586 IPos prev_match; /* previous match */ 1080 IPos prev_match; /* previous match */
@@ -1588,10 +1082,6 @@ ulg deflate()
1588 int match_available = 0; /* set if previous match exists */ 1082 int match_available = 0; /* set if previous match exists */
1589 register unsigned match_length = MIN_MATCH - 1; /* length of best match */ 1083 register unsigned match_length = MIN_MATCH - 1; /* length of best match */
1590 1084
1591#ifdef DEBUG
1592 extern long isize; /* byte length of input file, for debug only */
1593#endif
1594
1595 /* Process the input block. */ 1085 /* Process the input block. */
1596 while (lookahead != 0) { 1086 while (lookahead != 0) {
1597 /* Insert the string window[strstart .. strstart+2] in the 1087 /* Insert the string window[strstart .. strstart+2] in the
@@ -1708,180 +1198,14 @@ ulg deflate()
1708 * or stdout with -c option or if stdin used as input. 1198 * or stdout with -c option or if stdin used as input.
1709 * If the output file name had to be truncated, the original name is kept 1199 * If the output file name had to be truncated, the original name is kept
1710 * in the compressed file. 1200 * in the compressed file.
1711 * On MSDOS, file.tmp -> file.tmz. On VMS, file.tmp -> file.tmp-gz.
1712 *
1713 * Using gz on MSDOS would create too many file name conflicts. For
1714 * example, foo.txt -> foo.tgz (.tgz must be reserved as shorthand for
1715 * tar.gz). Similarly, foo.dir and foo.doc would both be mapped to foo.dgz.
1716 * I also considered 12345678.txt -> 12345txt.gz but this truncates the name
1717 * too heavily. There is no ideal solution given the MSDOS 8+3 limitation.
1718 *
1719 * For the meaning of all compilation flags, see comments in Makefile.in.
1720 */ 1201 */
1721 1202
1722#include <ctype.h>
1723#include <sys/types.h>
1724#include <signal.h>
1725#include <errno.h>
1726
1727 /* configuration */ 1203 /* configuration */
1728 1204
1729#ifdef NO_TIME_H
1730# include <sys/time.h>
1731#else
1732# include <time.h>
1733#endif
1734
1735#ifndef NO_FCNTL_H
1736# include <fcntl.h>
1737#endif
1738
1739#ifdef HAVE_UNISTD_H
1740# include <unistd.h>
1741#endif
1742
1743#if defined(DIRENT)
1744# include <dirent.h>
1745typedef struct dirent dir_type; 1205typedef struct dirent dir_type;
1746 1206
1747# define NLENGTH(dirent) ((int)strlen((dirent)->d_name))
1748# define DIR_OPT "DIRENT"
1749#else
1750# define NLENGTH(dirent) ((dirent)->d_namlen)
1751# ifdef SYSDIR
1752# include <sys/dir.h>
1753typedef struct direct dir_type;
1754
1755# define DIR_OPT "SYSDIR"
1756# else
1757# ifdef SYSNDIR
1758# include <sys/ndir.h>
1759typedef struct direct dir_type;
1760
1761# define DIR_OPT "SYSNDIR"
1762# else
1763# ifdef NDIR
1764# include <ndir.h>
1765typedef struct direct dir_type;
1766
1767# define DIR_OPT "NDIR"
1768# else
1769# define NO_DIR
1770# define DIR_OPT "NO_DIR"
1771# endif
1772# endif
1773# endif
1774#endif
1775
1776#ifndef NO_UTIME
1777# ifndef NO_UTIME_H
1778# include <utime.h>
1779# define TIME_OPT "UTIME"
1780# else
1781# ifdef HAVE_SYS_UTIME_H
1782# include <sys/utime.h>
1783# define TIME_OPT "SYS_UTIME"
1784# else
1785struct utimbuf {
1786 time_t actime;
1787 time_t modtime;
1788};
1789
1790# define TIME_OPT ""
1791# endif
1792# endif
1793#else
1794# define TIME_OPT "NO_UTIME"
1795#endif
1796
1797#if !defined(S_ISDIR) && defined(S_IFDIR)
1798# define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
1799#endif
1800#if !defined(S_ISREG) && defined(S_IFREG)
1801# define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
1802#endif
1803
1804typedef RETSIGTYPE(*sig_type) (int); 1207typedef RETSIGTYPE(*sig_type) (int);
1805 1208
1806#ifndef O_BINARY
1807# define O_BINARY 0 /* creation mode for open() */
1808#endif
1809
1810#ifndef O_CREAT
1811 /* Pure BSD system? */
1812# include <sys/file.h>
1813# ifndef O_CREAT
1814# define O_CREAT FCREAT
1815# endif
1816# ifndef O_EXCL
1817# define O_EXCL FEXCL
1818# endif
1819#endif
1820
1821#ifndef S_IRUSR
1822# define S_IRUSR 0400
1823#endif
1824#ifndef S_IWUSR
1825# define S_IWUSR 0200
1826#endif
1827#define RW_USER (S_IRUSR | S_IWUSR) /* creation mode for open() */
1828
1829#ifndef MAX_PATH_LEN
1830# define MAX_PATH_LEN 1024 /* max pathname length */
1831#endif
1832
1833#ifndef SEEK_END
1834# define SEEK_END 2
1835#endif
1836
1837#ifdef NO_OFF_T
1838typedef long off_t;
1839off_t lseek (int fd, off_t offset, int whence);
1840#endif
1841
1842/* Separator for file name parts (see shorten_name()) */
1843#ifdef NO_MULTIPLE_DOTS
1844# define PART_SEP "-"
1845#else
1846# define PART_SEP "."
1847#endif
1848
1849 /* global buffers */
1850
1851DECLARE(uch, inbuf, INBUFSIZ + INBUF_EXTRA);
1852DECLARE(uch, outbuf, OUTBUFSIZ + OUTBUF_EXTRA);
1853DECLARE(ush, d_buf, DIST_BUFSIZE);
1854DECLARE(uch, window, 2L * WSIZE);
1855#ifndef MAXSEG_64K
1856DECLARE(ush, tab_prefix, 1L << BITS);
1857#else
1858DECLARE(ush, tab_prefix0, 1L << (BITS - 1));
1859DECLARE(ush, tab_prefix1, 1L << (BITS - 1));
1860#endif
1861
1862 /* local variables */
1863
1864static int foreground; /* set if program run in foreground */
1865static int method = DEFLATED; /* compression method */
1866static int exit_code = OK; /* program exit code */
1867static int part_nb; /* number of parts in .gz file */
1868static long time_stamp; /* original time stamp (modification time) */
1869static long ifile_size; /* input file size, -1 for devices (debug only) */
1870static char z_suffix[MAX_SUFFIX + 1]; /* default suffix (can be set with --suffix) */
1871static int z_len; /* strlen(z_suffix) */
1872
1873static long bytes_in; /* number of input bytes */
1874static long bytes_out; /* number of output bytes */
1875static char ifname[MAX_PATH_LEN]; /* input file name */
1876static char ofname[MAX_PATH_LEN]; /* output file name */
1877static int ifd; /* input file descriptor */
1878static int ofd; /* output file descriptor */
1879static unsigned insize; /* valid bytes in inbuf */
1880static unsigned outcnt; /* bytes in output buffer */
1881
1882/* local functions */
1883
1884#define strequ(s1, s2) (strcmp((s1),(s2)) == 0)
1885 1209
1886/* ======================================================================== */ 1210/* ======================================================================== */
1887// int main (argc, argv) 1211// int main (argc, argv)
@@ -1953,12 +1277,7 @@ int gzip_main(int argc, char **argv)
1953 ALLOC(uch, outbuf, OUTBUFSIZ + OUTBUF_EXTRA); 1277 ALLOC(uch, outbuf, OUTBUFSIZ + OUTBUF_EXTRA);
1954 ALLOC(ush, d_buf, DIST_BUFSIZE); 1278 ALLOC(ush, d_buf, DIST_BUFSIZE);
1955 ALLOC(uch, window, 2L * WSIZE); 1279 ALLOC(uch, window, 2L * WSIZE);
1956#ifndef MAXSEG_64K
1957 ALLOC(ush, tab_prefix, 1L << BITS); 1280 ALLOC(ush, tab_prefix, 1L << BITS);
1958#else
1959 ALLOC(ush, tab_prefix0, 1L << (BITS - 1));
1960 ALLOC(ush, tab_prefix1, 1L << (BITS - 1));
1961#endif
1962 1281
1963 if (fromstdin == 1) { 1282 if (fromstdin == 1) {
1964 strcpy(ofname, "stdin"); 1283 strcpy(ofname, "stdin");
@@ -1986,7 +1305,6 @@ int gzip_main(int argc, char **argv)
1986 /* And get to work */ 1305 /* And get to work */
1987 strcpy(ofname, "stdout"); 1306 strcpy(ofname, "stdout");
1988 outFileNum = fileno(stdout); 1307 outFileNum = fileno(stdout);
1989 SET_BINARY_MODE(fileno(stdout));
1990 1308
1991 clear_bufs(); /* clear input and output buffers */ 1309 clear_bufs(); /* clear input and output buffers */
1992 part_nb = 0; 1310 part_nb = 0;
@@ -2009,7 +1327,6 @@ int gzip_main(int argc, char **argv)
2009#endif 1327#endif
2010 if (outFileNum < 0) 1328 if (outFileNum < 0)
2011 perror_msg_and_die("%s", ofname); 1329 perror_msg_and_die("%s", ofname);
2012 SET_BINARY_MODE(outFileNum);
2013 /* Set permissions on the file */ 1330 /* Set permissions on the file */
2014 fchmod(outFileNum, statBuf.st_mode); 1331 fchmod(outFileNum, statBuf.st_mode);
2015 1332
@@ -2088,8 +1405,6 @@ int gzip_main(int argc, char **argv)
2088 * 1405 *
2089 */ 1406 */
2090 1407
2091#include <ctype.h>
2092
2093/* =========================================================================== 1408/* ===========================================================================
2094 * Constants 1409 * Constants
2095 */ 1410 */
@@ -2119,15 +1434,15 @@ int gzip_main(int argc, char **argv)
2119/* number of codes used to transfer the bit lengths */ 1434/* number of codes used to transfer the bit lengths */
2120 1435
2121 1436
2122local int near extra_lbits[LENGTH_CODES] /* extra bits for each length code */ 1437static const int extra_lbits[LENGTH_CODES] /* extra bits for each length code */
2123 = { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 1438 = { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4,
2124 4, 4, 5, 5, 5, 5, 0 }; 1439 4, 4, 5, 5, 5, 5, 0 };
2125 1440
2126local int near extra_dbits[D_CODES] /* extra bits for each distance code */ 1441static const int extra_dbits[D_CODES] /* extra bits for each distance code */
2127 = { 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 1442 = { 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9,
2128 10, 10, 11, 11, 12, 12, 13, 13 }; 1443 10, 10, 11, 11, 12, 12, 13, 13 };
2129 1444
2130local int near extra_blbits[BL_CODES] /* extra bits for each bit length code */ 1445static const int extra_blbits[BL_CODES] /* extra bits for each bit length code */
2131= { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 3, 7 }; 1446= { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 3, 7 };
2132 1447
2133#define STORED_BLOCK 0 1448#define STORED_BLOCK 0
@@ -2198,10 +1513,10 @@ error cannot overlay l_buf and inbuf
2198#define HEAP_SIZE (2*L_CODES+1) 1513#define HEAP_SIZE (2*L_CODES+1)
2199/* maximum heap size */ 1514/* maximum heap size */
2200 1515
2201local ct_data near dyn_ltree[HEAP_SIZE]; /* literal and length tree */ 1516static ct_data dyn_ltree[HEAP_SIZE]; /* literal and length tree */
2202local ct_data near dyn_dtree[2 * D_CODES + 1]; /* distance tree */ 1517static ct_data dyn_dtree[2 * D_CODES + 1]; /* distance tree */
2203 1518
2204local ct_data near static_ltree[L_CODES + 2]; 1519static ct_data static_ltree[L_CODES + 2];
2205 1520
2206/* The static literal tree. Since the bit lengths are imposed, there is no 1521/* The static literal tree. Since the bit lengths are imposed, there is no
2207 * need for the L_CODES extra codes used during heap construction. However 1522 * need for the L_CODES extra codes used during heap construction. However
@@ -2209,77 +1524,77 @@ local ct_data near static_ltree[L_CODES + 2];
2209 * below). 1524 * below).
2210 */ 1525 */
2211 1526
2212local ct_data near static_dtree[D_CODES]; 1527static ct_data static_dtree[D_CODES];
2213 1528
2214/* The static distance tree. (Actually a trivial tree since all codes use 1529/* The static distance tree. (Actually a trivial tree since all codes use
2215 * 5 bits.) 1530 * 5 bits.)
2216 */ 1531 */
2217 1532
2218local ct_data near bl_tree[2 * BL_CODES + 1]; 1533static ct_data bl_tree[2 * BL_CODES + 1];
2219 1534
2220/* Huffman tree for the bit lengths */ 1535/* Huffman tree for the bit lengths */
2221 1536
2222typedef struct tree_desc { 1537typedef struct tree_desc {
2223 ct_data near *dyn_tree; /* the dynamic tree */ 1538 ct_data *dyn_tree; /* the dynamic tree */
2224 ct_data near *static_tree; /* corresponding static tree or NULL */ 1539 ct_data *static_tree; /* corresponding static tree or NULL */
2225 int near *extra_bits; /* extra bits for each code or NULL */ 1540 const int *extra_bits; /* extra bits for each code or NULL */
2226 int extra_base; /* base index for extra_bits */ 1541 int extra_base; /* base index for extra_bits */
2227 int elems; /* max number of elements in the tree */ 1542 int elems; /* max number of elements in the tree */
2228 int max_length; /* max bit length for the codes */ 1543 int max_length; /* max bit length for the codes */
2229 int max_code; /* largest code with non zero frequency */ 1544 int max_code; /* largest code with non zero frequency */
2230} tree_desc; 1545} tree_desc;
2231 1546
2232local tree_desc near l_desc = 1547static tree_desc l_desc =
2233 { dyn_ltree, static_ltree, extra_lbits, LITERALS + 1, L_CODES, 1548 { dyn_ltree, static_ltree, extra_lbits, LITERALS + 1, L_CODES,
2234 MAX_BITS, 0 }; 1549 MAX_BITS, 0 };
2235 1550
2236local tree_desc near d_desc = 1551static tree_desc d_desc =
2237 { dyn_dtree, static_dtree, extra_dbits, 0, D_CODES, MAX_BITS, 0 }; 1552 { dyn_dtree, static_dtree, extra_dbits, 0, D_CODES, MAX_BITS, 0 };
2238 1553
2239local tree_desc near bl_desc = 1554static tree_desc bl_desc =
2240 { bl_tree, (ct_data near *) 0, extra_blbits, 0, BL_CODES, MAX_BL_BITS, 1555 { bl_tree, (ct_data *) 0, extra_blbits, 0, BL_CODES, MAX_BL_BITS,
2241 0 }; 1556 0 };
2242 1557
2243 1558
2244local ush near bl_count[MAX_BITS + 1]; 1559static ush bl_count[MAX_BITS + 1];
2245 1560
2246/* number of codes at each bit length for an optimal tree */ 1561/* number of codes at each bit length for an optimal tree */
2247 1562
2248local uch near bl_order[BL_CODES] 1563static const uch bl_order[BL_CODES]
2249= { 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 }; 1564= { 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 };
2250 1565
2251/* The lengths of the bit length codes are sent in order of decreasing 1566/* The lengths of the bit length codes are sent in order of decreasing
2252 * probability, to avoid transmitting the lengths for unused bit length codes. 1567 * probability, to avoid transmitting the lengths for unused bit length codes.
2253 */ 1568 */
2254 1569
2255local int near heap[2 * L_CODES + 1]; /* heap used to build the Huffman trees */ 1570static int heap[2 * L_CODES + 1]; /* heap used to build the Huffman trees */
2256local int heap_len; /* number of elements in the heap */ 1571static int heap_len; /* number of elements in the heap */
2257local int heap_max; /* element of largest frequency */ 1572static int heap_max; /* element of largest frequency */
2258 1573
2259/* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used. 1574/* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.
2260 * The same heap array is used to build all trees. 1575 * The same heap array is used to build all trees.
2261 */ 1576 */
2262 1577
2263local uch near depth[2 * L_CODES + 1]; 1578static uch depth[2 * L_CODES + 1];
2264 1579
2265/* Depth of each subtree used as tie breaker for trees of equal frequency */ 1580/* Depth of each subtree used as tie breaker for trees of equal frequency */
2266 1581
2267local uch length_code[MAX_MATCH - MIN_MATCH + 1]; 1582static uch length_code[MAX_MATCH - MIN_MATCH + 1];
2268 1583
2269/* length code for each normalized match length (0 == MIN_MATCH) */ 1584/* length code for each normalized match length (0 == MIN_MATCH) */
2270 1585
2271local uch dist_code[512]; 1586static uch dist_code[512];
2272 1587
2273/* distance codes. The first 256 values correspond to the distances 1588/* distance codes. The first 256 values correspond to the distances
2274 * 3 .. 258, the last 256 values correspond to the top 8 bits of 1589 * 3 .. 258, the last 256 values correspond to the top 8 bits of
2275 * the 15 bit distances. 1590 * the 15 bit distances.
2276 */ 1591 */
2277 1592
2278local int near base_length[LENGTH_CODES]; 1593static int base_length[LENGTH_CODES];
2279 1594
2280/* First normalized length for each code (0 = MIN_MATCH) */ 1595/* First normalized length for each code (0 = MIN_MATCH) */
2281 1596
2282local int near base_dist[D_CODES]; 1597static int base_dist[D_CODES];
2283 1598
2284/* First normalized distance for each code (0 = distance of 1) */ 1599/* First normalized distance for each code (0 = distance of 1) */
2285 1600
@@ -2288,58 +1603,47 @@ local int near base_dist[D_CODES];
2288 1603
2289/* DECLARE(ush, d_buf, DIST_BUFSIZE); buffer for distances */ 1604/* DECLARE(ush, d_buf, DIST_BUFSIZE); buffer for distances */
2290 1605
2291local uch near flag_buf[(LIT_BUFSIZE / 8)]; 1606static uch flag_buf[(LIT_BUFSIZE / 8)];
2292 1607
2293/* flag_buf is a bit array distinguishing literals from lengths in 1608/* flag_buf is a bit array distinguishing literals from lengths in
2294 * l_buf, thus indicating the presence or absence of a distance. 1609 * l_buf, thus indicating the presence or absence of a distance.
2295 */ 1610 */
2296 1611
2297local unsigned last_lit; /* running index in l_buf */ 1612static unsigned last_lit; /* running index in l_buf */
2298local unsigned last_dist; /* running index in d_buf */ 1613static unsigned last_dist; /* running index in d_buf */
2299local unsigned last_flags; /* running index in flag_buf */ 1614static unsigned last_flags; /* running index in flag_buf */
2300local uch flags; /* current flags not yet saved in flag_buf */ 1615static uch flags; /* current flags not yet saved in flag_buf */
2301local uch flag_bit; /* current bit used in flags */ 1616static uch flag_bit; /* current bit used in flags */
2302 1617
2303/* bits are filled in flags starting at bit 0 (least significant). 1618/* bits are filled in flags starting at bit 0 (least significant).
2304 * Note: these flags are overkill in the current code since we don't 1619 * Note: these flags are overkill in the current code since we don't
2305 * take advantage of DIST_BUFSIZE == LIT_BUFSIZE. 1620 * take advantage of DIST_BUFSIZE == LIT_BUFSIZE.
2306 */ 1621 */
2307 1622
2308local ulg opt_len; /* bit length of current block with optimal trees */ 1623static ulg opt_len; /* bit length of current block with optimal trees */
2309local ulg static_len; /* bit length of current block with static trees */ 1624static ulg static_len; /* bit length of current block with static trees */
2310
2311local ulg compressed_len; /* total bit length of compressed file */
2312 1625
2313local ulg input_len; /* total byte length of input file */ 1626static ulg compressed_len; /* total bit length of compressed file */
2314 1627
2315/* input_len is for debugging only since we can get it by other means. */
2316 1628
2317ush *file_type; /* pointer to UNKNOWN, BINARY or ASCII */ 1629static ush *file_type; /* pointer to UNKNOWN, BINARY or ASCII */
2318int *file_method; /* pointer to DEFLATE or STORE */ 1630static int *file_method; /* pointer to DEFLATE or STORE */
2319
2320#ifdef DEBUG
2321extern ulg bits_sent; /* bit length of the compressed data */
2322extern long isize; /* byte length of input file */
2323#endif
2324
2325extern long block_start; /* window offset of current block */
2326extern unsigned near strstart; /* window offset of current string */
2327 1631
2328/* =========================================================================== 1632/* ===========================================================================
2329 * Local (static) routines in this file. 1633 * Local (static) routines in this file.
2330 */ 1634 */
2331 1635
2332local void init_block (void); 1636static void init_block (void);
2333local void pqdownheap (ct_data near * tree, int k); 1637static void pqdownheap (ct_data * tree, int k);
2334local void gen_bitlen (tree_desc near * desc); 1638static void gen_bitlen (tree_desc * desc);
2335local void gen_codes (ct_data near * tree, int max_code); 1639static void gen_codes (ct_data * tree, int max_code);
2336local void build_tree (tree_desc near * desc); 1640static void build_tree (tree_desc * desc);
2337local void scan_tree (ct_data near * tree, int max_code); 1641static void scan_tree (ct_data * tree, int max_code);
2338local void send_tree (ct_data near * tree, int max_code); 1642static void send_tree (ct_data * tree, int max_code);
2339local int build_bl_tree (void); 1643static int build_bl_tree (void);
2340local void send_all_trees (int lcodes, int dcodes, int blcodes); 1644static void send_all_trees (int lcodes, int dcodes, int blcodes);
2341local void compress_block (ct_data near * ltree, ct_data near * dtree); 1645static void compress_block (ct_data * ltree, ct_data * dtree);
2342local void set_file_type (void); 1646static void set_file_type (void);
2343 1647
2344 1648
2345#ifndef DEBUG 1649#ifndef DEBUG
@@ -2366,9 +1670,7 @@ local void set_file_type (void);
2366 * location of the internal file attribute (ascii/binary) and method 1670 * location of the internal file attribute (ascii/binary) and method
2367 * (DEFLATE/STORE). 1671 * (DEFLATE/STORE).
2368 */ 1672 */
2369void ct_init(attr, methodp) 1673static void ct_init(ush *attr, int *methodp)
2370ush *attr; /* pointer to internal file attribute */
2371int *methodp; /* pointer to compression method */
2372{ 1674{
2373 int n; /* iterates over tree elements */ 1675 int n; /* iterates over tree elements */
2374 int bits; /* bit counter */ 1676 int bits; /* bit counter */
@@ -2378,7 +1680,7 @@ int *methodp; /* pointer to compression method */
2378 1680
2379 file_type = attr; 1681 file_type = attr;
2380 file_method = methodp; 1682 file_method = methodp;
2381 compressed_len = input_len = 0L; 1683 compressed_len = 0L;
2382 1684
2383 if (static_dtree[0].Len != 0) 1685 if (static_dtree[0].Len != 0)
2384 return; /* ct_init already called */ 1686 return; /* ct_init already called */
@@ -2432,7 +1734,7 @@ int *methodp; /* pointer to compression method */
2432 * tree construction to get a canonical Huffman tree (longest code 1734 * tree construction to get a canonical Huffman tree (longest code
2433 * all ones) 1735 * all ones)
2434 */ 1736 */
2435 gen_codes((ct_data near *) static_ltree, L_CODES + 1); 1737 gen_codes((ct_data *) static_ltree, L_CODES + 1);
2436 1738
2437 /* The static distance tree is trivial: */ 1739 /* The static distance tree is trivial: */
2438 for (n = 0; n < D_CODES; n++) { 1740 for (n = 0; n < D_CODES; n++) {
@@ -2447,7 +1749,7 @@ int *methodp; /* pointer to compression method */
2447/* =========================================================================== 1749/* ===========================================================================
2448 * Initialize a new block. 1750 * Initialize a new block.
2449 */ 1751 */
2450local void init_block() 1752static void init_block()
2451{ 1753{
2452 int n; /* iterates over tree elements */ 1754 int n; /* iterates over tree elements */
2453 1755
@@ -2495,9 +1797,7 @@ local void init_block()
2495 * when the heap property is re-established (each father smaller than its 1797 * when the heap property is re-established (each father smaller than its
2496 * two sons). 1798 * two sons).
2497 */ 1799 */
2498local void pqdownheap(tree, k) 1800static void pqdownheap(ct_data *tree, int k)
2499ct_data near *tree; /* the tree to restore */
2500int k; /* node to move down */
2501{ 1801{
2502 int v = heap[k]; 1802 int v = heap[k];
2503 int j = k << 1; /* left son of k */ 1803 int j = k << 1; /* left son of k */
@@ -2531,15 +1831,14 @@ int k; /* node to move down */
2531 * The length opt_len is updated; static_len is also updated if stree is 1831 * The length opt_len is updated; static_len is also updated if stree is
2532 * not null. 1832 * not null.
2533 */ 1833 */
2534local void gen_bitlen(desc) 1834static void gen_bitlen(tree_desc *desc)
2535tree_desc near *desc; /* the tree descriptor */
2536{ 1835{
2537 ct_data near *tree = desc->dyn_tree; 1836 ct_data *tree = desc->dyn_tree;
2538 int near *extra = desc->extra_bits; 1837 const int *extra = desc->extra_bits;
2539 int base = desc->extra_base; 1838 int base = desc->extra_base;
2540 int max_code = desc->max_code; 1839 int max_code = desc->max_code;
2541 int max_length = desc->max_length; 1840 int max_length = desc->max_length;
2542 ct_data near *stree = desc->static_tree; 1841 ct_data *stree = desc->static_tree;
2543 int h; /* heap index */ 1842 int h; /* heap index */
2544 int n, m; /* iterate over the tree elements */ 1843 int n, m; /* iterate over the tree elements */
2545 int bits; /* bit length */ 1844 int bits; /* bit length */
@@ -2629,9 +1928,7 @@ tree_desc near *desc; /* the tree descriptor */
2629 * OUT assertion: the field code is set for all tree elements of non 1928 * OUT assertion: the field code is set for all tree elements of non
2630 * zero code length. 1929 * zero code length.
2631 */ 1930 */
2632local void gen_codes(tree, max_code) 1931static void gen_codes(ct_data *tree, int max_code)
2633ct_data near *tree; /* the tree to decorate */
2634int max_code; /* largest code with non zero frequency */
2635{ 1932{
2636 ush next_code[MAX_BITS + 1]; /* next code value for each bit length */ 1933 ush next_code[MAX_BITS + 1]; /* next code value for each bit length */
2637 ush code = 0; /* running code value */ 1934 ush code = 0; /* running code value */
@@ -2674,11 +1971,10 @@ int max_code; /* largest code with non zero frequency */
2674 * and corresponding code. The length opt_len is updated; static_len is 1971 * and corresponding code. The length opt_len is updated; static_len is
2675 * also updated if stree is not null. The field max_code is set. 1972 * also updated if stree is not null. The field max_code is set.
2676 */ 1973 */
2677local void build_tree(desc) 1974static void build_tree(tree_desc *desc)
2678tree_desc near *desc; /* the tree descriptor */
2679{ 1975{
2680 ct_data near *tree = desc->dyn_tree; 1976 ct_data *tree = desc->dyn_tree;
2681 ct_data near *stree = desc->static_tree; 1977 ct_data *stree = desc->static_tree;
2682 int elems = desc->elems; 1978 int elems = desc->elems;
2683 int n, m; /* iterate over heap elements */ 1979 int n, m; /* iterate over heap elements */
2684 int max_code = -1; /* largest code with non zero frequency */ 1980 int max_code = -1; /* largest code with non zero frequency */
@@ -2754,10 +2050,10 @@ tree_desc near *desc; /* the tree descriptor */
2754 /* At this point, the fields freq and dad are set. We can now 2050 /* At this point, the fields freq and dad are set. We can now
2755 * generate the bit lengths. 2051 * generate the bit lengths.
2756 */ 2052 */
2757 gen_bitlen((tree_desc near *) desc); 2053 gen_bitlen((tree_desc *) desc);
2758 2054
2759 /* The field len is now set, we can generate the bit codes */ 2055 /* The field len is now set, we can generate the bit codes */
2760 gen_codes((ct_data near *) tree, max_code); 2056 gen_codes((ct_data *) tree, max_code);
2761} 2057}
2762 2058
2763/* =========================================================================== 2059/* ===========================================================================
@@ -2766,9 +2062,7 @@ tree_desc near *desc; /* the tree descriptor */
2766 * counts. (The contribution of the bit length codes will be added later 2062 * counts. (The contribution of the bit length codes will be added later
2767 * during the construction of bl_tree.) 2063 * during the construction of bl_tree.)
2768 */ 2064 */
2769local void scan_tree(tree, max_code) 2065static void scan_tree(ct_data *tree, int max_code)
2770ct_data near *tree; /* the tree to be scanned */
2771int max_code; /* and its largest code of non zero frequency */
2772{ 2066{
2773 int n; /* iterates over all tree elements */ 2067 int n; /* iterates over all tree elements */
2774 int prevlen = -1; /* last emitted length */ 2068 int prevlen = -1; /* last emitted length */
@@ -2814,9 +2108,7 @@ int max_code; /* and its largest code of non zero frequency */
2814 * Send a literal or distance tree in compressed form, using the codes in 2108 * Send a literal or distance tree in compressed form, using the codes in
2815 * bl_tree. 2109 * bl_tree.
2816 */ 2110 */
2817local void send_tree(tree, max_code) 2111static void send_tree(ct_data *tree, int max_code)
2818ct_data near *tree; /* the tree to be scanned */
2819int max_code; /* and its largest code of non zero frequency */
2820{ 2112{
2821 int n; /* iterates over all tree elements */ 2113 int n; /* iterates over all tree elements */
2822 int prevlen = -1; /* last emitted length */ 2114 int prevlen = -1; /* last emitted length */
@@ -2873,16 +2165,16 @@ int max_code; /* and its largest code of non zero frequency */
2873 * Construct the Huffman tree for the bit lengths and return the index in 2165 * Construct the Huffman tree for the bit lengths and return the index in
2874 * bl_order of the last bit length code to send. 2166 * bl_order of the last bit length code to send.
2875 */ 2167 */
2876local int build_bl_tree() 2168static const int build_bl_tree()
2877{ 2169{
2878 int max_blindex; /* index of last bit length code of non zero freq */ 2170 int max_blindex; /* index of last bit length code of non zero freq */
2879 2171
2880 /* Determine the bit length frequencies for literal and distance trees */ 2172 /* Determine the bit length frequencies for literal and distance trees */
2881 scan_tree((ct_data near *) dyn_ltree, l_desc.max_code); 2173 scan_tree((ct_data *) dyn_ltree, l_desc.max_code);
2882 scan_tree((ct_data near *) dyn_dtree, d_desc.max_code); 2174 scan_tree((ct_data *) dyn_dtree, d_desc.max_code);
2883 2175
2884 /* Build the bit length tree: */ 2176 /* Build the bit length tree: */
2885 build_tree((tree_desc near *) (&bl_desc)); 2177 build_tree((tree_desc *) (&bl_desc));
2886 /* opt_len now includes the length of the tree representations, except 2178 /* opt_len now includes the length of the tree representations, except
2887 * the lengths of the bit lengths codes and the 5+5+4 bits for the counts. 2179 * the lengths of the bit lengths codes and the 5+5+4 bits for the counts.
2888 */ 2180 */
@@ -2909,8 +2201,7 @@ local int build_bl_tree()
2909 * lengths of the bit length codes, the literal tree and the distance tree. 2201 * lengths of the bit length codes, the literal tree and the distance tree.
2910 * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4. 2202 * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4.
2911 */ 2203 */
2912local void send_all_trees(lcodes, dcodes, blcodes) 2204static void send_all_trees(int lcodes, int dcodes, int blcodes)
2913int lcodes, dcodes, blcodes; /* number of codes for each tree */
2914{ 2205{
2915 int rank; /* index in bl_order */ 2206 int rank; /* index in bl_order */
2916 2207
@@ -2928,10 +2219,10 @@ int lcodes, dcodes, blcodes; /* number of codes for each tree */
2928 } 2219 }
2929 Tracev((stderr, "\nbl tree: sent %ld", bits_sent)); 2220 Tracev((stderr, "\nbl tree: sent %ld", bits_sent));
2930 2221
2931 send_tree((ct_data near *) dyn_ltree, lcodes - 1); /* send the literal tree */ 2222 send_tree((ct_data *) dyn_ltree, lcodes - 1); /* send the literal tree */
2932 Tracev((stderr, "\nlit tree: sent %ld", bits_sent)); 2223 Tracev((stderr, "\nlit tree: sent %ld", bits_sent));
2933 2224
2934 send_tree((ct_data near *) dyn_dtree, dcodes - 1); /* send the distance tree */ 2225 send_tree((ct_data *) dyn_dtree, dcodes - 1); /* send the distance tree */
2935 Tracev((stderr, "\ndist tree: sent %ld", bits_sent)); 2226 Tracev((stderr, "\ndist tree: sent %ld", bits_sent));
2936} 2227}
2937 2228
@@ -2940,10 +2231,7 @@ int lcodes, dcodes, blcodes; /* number of codes for each tree */
2940 * trees or store, and output the encoded block to the zip file. This function 2231 * trees or store, and output the encoded block to the zip file. This function
2941 * returns the total compressed length for the file so far. 2232 * returns the total compressed length for the file so far.
2942 */ 2233 */
2943ulg flush_block(buf, stored_len, eof) 2234static ulg flush_block(char *buf, ulg stored_len, int eof)
2944char *buf; /* input block, or NULL if too old */
2945ulg stored_len; /* length of input block */
2946int eof; /* true if this is the last block for a file */
2947{ 2235{
2948 ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */ 2236 ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */
2949 int max_blindex; /* index of last bit length code of non zero freq */ 2237 int max_blindex; /* index of last bit length code of non zero freq */
@@ -2955,10 +2243,10 @@ int eof; /* true if this is the last block for a file */
2955 set_file_type(); 2243 set_file_type();
2956 2244
2957 /* Construct the literal and distance trees */ 2245 /* Construct the literal and distance trees */
2958 build_tree((tree_desc near *) (&l_desc)); 2246 build_tree((tree_desc *) (&l_desc));
2959 Tracev((stderr, "\nlit data: dyn %ld, stat %ld", opt_len, static_len)); 2247 Tracev((stderr, "\nlit data: dyn %ld, stat %ld", opt_len, static_len));
2960 2248
2961 build_tree((tree_desc near *) (&d_desc)); 2249 build_tree((tree_desc *) (&d_desc));
2962 Tracev( 2250 Tracev(
2963 (stderr, "\ndist data: dyn %ld, stat %ld", opt_len, 2251 (stderr, "\ndist data: dyn %ld, stat %ld", opt_len,
2964 static_len)); 2252 static_len));
@@ -2974,7 +2262,6 @@ int eof; /* true if this is the last block for a file */
2974 /* Determine the best encoding. Compute first the block length in bytes */ 2262 /* Determine the best encoding. Compute first the block length in bytes */
2975 opt_lenb = (opt_len + 3 + 7) >> 3; 2263 opt_lenb = (opt_len + 3 + 7) >> 3;
2976 static_lenb = (static_len + 3 + 7) >> 3; 2264 static_lenb = (static_len + 3 + 7) >> 3;
2977 input_len += stored_len; /* for debugging only */
2978 2265
2979 Trace( 2266 Trace(
2980 (stderr, 2267 (stderr,
@@ -2989,11 +2276,8 @@ int eof; /* true if this is the last block for a file */
2989 * and if the zip file can be seeked (to rewrite the local header), 2276 * and if the zip file can be seeked (to rewrite the local header),
2990 * the whole file is transformed into a stored file: 2277 * the whole file is transformed into a stored file:
2991 */ 2278 */
2992#ifdef FORCE_METHOD
2993#else
2994 if (stored_len <= opt_lenb && eof && compressed_len == 0L 2279 if (stored_len <= opt_lenb && eof && compressed_len == 0L
2995 && seekable()) { 2280 && seekable()) {
2996#endif
2997 /* Since LIT_BUFSIZE <= 2*WSIZE, the input data must be there: */ 2281 /* Since LIT_BUFSIZE <= 2*WSIZE, the input data must be there: */
2998 if (buf == (char *) 0) 2282 if (buf == (char *) 0)
2999 error_msg("block vanished"); 2283 error_msg("block vanished");
@@ -3002,11 +2286,8 @@ int eof; /* true if this is the last block for a file */
3002 compressed_len = stored_len << 3; 2286 compressed_len = stored_len << 3;
3003 *file_method = STORED; 2287 *file_method = STORED;
3004 2288
3005#ifdef FORCE_METHOD
3006#else
3007 } else if (stored_len + 4 <= opt_lenb && buf != (char *) 0) { 2289 } else if (stored_len + 4 <= opt_lenb && buf != (char *) 0) {
3008 /* 4: two words for the lengths */ 2290 /* 4: two words for the lengths */
3009#endif
3010 /* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE. 2291 /* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE.
3011 * Otherwise we can't have processed more than WSIZE input bytes since 2292 * Otherwise we can't have processed more than WSIZE input bytes since
3012 * the last block flush, because compression would have been 2293 * the last block flush, because compression would have been
@@ -3019,27 +2300,23 @@ int eof; /* true if this is the last block for a file */
3019 2300
3020 copy_block(buf, (unsigned) stored_len, 1); /* with header */ 2301 copy_block(buf, (unsigned) stored_len, 1); /* with header */
3021 2302
3022#ifdef FORCE_METHOD
3023#else
3024 } else if (static_lenb == opt_lenb) { 2303 } else if (static_lenb == opt_lenb) {
3025#endif
3026 send_bits((STATIC_TREES << 1) + eof, 3); 2304 send_bits((STATIC_TREES << 1) + eof, 3);
3027 compress_block((ct_data near *) static_ltree, 2305 compress_block((ct_data *) static_ltree,
3028 (ct_data near *) static_dtree); 2306 (ct_data *) static_dtree);
3029 compressed_len += 3 + static_len; 2307 compressed_len += 3 + static_len;
3030 } else { 2308 } else {
3031 send_bits((DYN_TREES << 1) + eof, 3); 2309 send_bits((DYN_TREES << 1) + eof, 3);
3032 send_all_trees(l_desc.max_code + 1, d_desc.max_code + 1, 2310 send_all_trees(l_desc.max_code + 1, d_desc.max_code + 1,
3033 max_blindex + 1); 2311 max_blindex + 1);
3034 compress_block((ct_data near *) dyn_ltree, 2312 compress_block((ct_data *) dyn_ltree,
3035 (ct_data near *) dyn_dtree); 2313 (ct_data *) dyn_dtree);
3036 compressed_len += 3 + opt_len; 2314 compressed_len += 3 + opt_len;
3037 } 2315 }
3038 Assert(compressed_len == bits_sent, "bad compressed size"); 2316 Assert(compressed_len == bits_sent, "bad compressed size");
3039 init_block(); 2317 init_block();
3040 2318
3041 if (eof) { 2319 if (eof) {
3042 Assert(input_len == isize, "bad input size");
3043 bi_windup(); 2320 bi_windup();
3044 compressed_len += 7; /* align on byte boundary */ 2321 compressed_len += 7; /* align on byte boundary */
3045 } 2322 }
@@ -3053,9 +2330,7 @@ int eof; /* true if this is the last block for a file */
3053 * Save the match info and tally the frequency counts. Return true if 2330 * Save the match info and tally the frequency counts. Return true if
3054 * the current block must be flushed. 2331 * the current block must be flushed.
3055 */ 2332 */
3056int ct_tally(dist, lc) 2333static int ct_tally(int dist, int lc)
3057int dist; /* distance of matched string */
3058int lc; /* match length-MIN_MATCH or unmatched char (if dist==0) */
3059{ 2334{
3060 l_buf[last_lit++] = (uch) lc; 2335 l_buf[last_lit++] = (uch) lc;
3061 if (dist == 0) { 2336 if (dist == 0) {
@@ -3111,9 +2386,7 @@ int lc; /* match length-MIN_MATCH or unmatched char (if dist==0) */
3111/* =========================================================================== 2386/* ===========================================================================
3112 * Send the block data compressed using the given Huffman trees 2387 * Send the block data compressed using the given Huffman trees
3113 */ 2388 */
3114local void compress_block(ltree, dtree) 2389static void compress_block(ct_data *ltree, ct_data *dtree)
3115ct_data near *ltree; /* literal tree */
3116ct_data near *dtree; /* distance tree */
3117{ 2390{
3118 unsigned dist; /* distance of matched string */ 2391 unsigned dist; /* distance of matched string */
3119 int lc; /* match length or unmatched char (if dist == 0) */ 2392 int lc; /* match length or unmatched char (if dist == 0) */
@@ -3165,7 +2438,7 @@ ct_data near *dtree; /* distance tree */
3165 * IN assertion: the fields freq of dyn_ltree are set and the total of all 2438 * IN assertion: the fields freq of dyn_ltree are set and the total of all
3166 * frequencies does not exceed 64K (to fit in an int on 16 bit machines). 2439 * frequencies does not exceed 64K (to fit in an int on 16 bit machines).
3167 */ 2440 */
3168local void set_file_type() 2441static void set_file_type()
3169{ 2442{
3170 int n = 0; 2443 int n = 0;
3171 unsigned ascii_freq = 0; 2444 unsigned ascii_freq = 0;
@@ -3183,228 +2456,22 @@ local void set_file_type()
3183 } 2456 }
3184} 2457}
3185 2458
3186/* util.c -- utility functions for gzip support
3187 * Copyright (C) 1992-1993 Jean-loup Gailly
3188 * This is free software; you can redistribute it and/or modify it under the
3189 * terms of the GNU General Public License, see the file COPYING.
3190 */
3191
3192#include <ctype.h>
3193#include <errno.h>
3194#include <sys/types.h>
3195
3196#ifdef HAVE_UNISTD_H
3197# include <unistd.h>
3198#endif
3199#ifndef NO_FCNTL_H
3200# include <fcntl.h>
3201#endif
3202
3203/* ===========================================================================
3204 * Copy input to output unchanged: zcat == cat with --force.
3205 * IN assertion: insize bytes have already been read in inbuf.
3206 */
3207int copy(in, out)
3208int in, out; /* input and output file descriptors */
3209{
3210 errno = 0;
3211 while (insize != 0 && (int) insize != EOF) {
3212 write_buf(out, (char *) inbuf, insize);
3213 bytes_out += insize;
3214 insize = read(in, (char *) inbuf, INBUFSIZ);
3215 }
3216 if ((int) insize == EOF && errno != 0) {
3217 read_error_msg();
3218 }
3219 bytes_in = bytes_out;
3220 return OK;
3221}
3222
3223/* ========================================================================
3224 * Put string s in lower case, return s.
3225 */
3226char *strlwr(s)
3227char *s;
3228{
3229 char *t;
3230
3231 for (t = s; *t; t++)
3232 *t = tolow(*t);
3233 return s;
3234}
3235
3236#if defined(NO_STRING_H) && !defined(STDC_HEADERS)
3237
3238/* Provide missing strspn and strcspn functions. */
3239
3240int strspn (const char *s, const char *accept);
3241int strcspn (const char *s, const char *reject);
3242
3243/* ========================================================================
3244 * Return the length of the maximum initial segment
3245 * of s which contains only characters in accept.
3246 */
3247int strspn(s, accept)
3248const char *s;
3249const char *accept;
3250{
3251 register const char *p;
3252 register const char *a;
3253 register int count = 0;
3254
3255 for (p = s; *p != '\0'; ++p) {
3256 for (a = accept; *a != '\0'; ++a) {
3257 if (*p == *a)
3258 break;
3259 }
3260 if (*a == '\0')
3261 return count;
3262 ++count;
3263 }
3264 return count;
3265}
3266
3267/* ========================================================================
3268 * Return the length of the maximum inital segment of s
3269 * which contains no characters from reject.
3270 */
3271int strcspn(s, reject)
3272const char *s;
3273const char *reject;
3274{
3275 register int count = 0;
3276
3277 while (*s != '\0') {
3278 if (strchr(reject, *s++) != NULL)
3279 return count;
3280 ++count;
3281 }
3282 return count;
3283}
3284
3285#endif /* NO_STRING_H */
3286
3287/* ========================================================================
3288 * Add an environment variable (if any) before argv, and update argc.
3289 * Return the expanded environment variable to be freed later, or NULL
3290 * if no options were added to argv.
3291 */
3292#define SEPARATOR " \t" /* separators in env variable */
3293
3294char *add_envopt(argcp, argvp, env)
3295int *argcp; /* pointer to argc */
3296char ***argvp; /* pointer to argv */
3297char *env; /* name of environment variable */
3298{
3299 char *p; /* running pointer through env variable */
3300 char **oargv; /* runs through old argv array */
3301 char **nargv; /* runs through new argv array */
3302 int oargc = *argcp; /* old argc */
3303 int nargc = 0; /* number of arguments in env variable */
3304
3305 env = (char *) getenv(env);
3306 if (env == NULL)
3307 return NULL;
3308
3309 p = (char *) xmalloc(strlen(env) + 1);
3310 env = strcpy(p, env); /* keep env variable intact */
3311
3312 for (p = env; *p; nargc++) { /* move through env */
3313 p += strspn(p, SEPARATOR); /* skip leading separators */
3314 if (*p == '\0')
3315 break;
3316
3317 p += strcspn(p, SEPARATOR); /* find end of word */
3318 if (*p)
3319 *p++ = '\0'; /* mark it */
3320 }
3321 if (nargc == 0) {
3322 free(env);
3323 return NULL;
3324 }
3325 *argcp += nargc;
3326 /* Allocate the new argv array, with an extra element just in case
3327 * the original arg list did not end with a NULL.
3328 */
3329 nargv = (char **) calloc(*argcp + 1, sizeof(char *));
3330
3331 if (nargv == NULL)
3332 error_msg(memory_exhausted);
3333 oargv = *argvp;
3334 *argvp = nargv;
3335
3336 /* Copy the program name first */
3337 if (oargc-- < 0)
3338 error_msg("argc<=0");
3339 *(nargv++) = *(oargv++);
3340
3341 /* Then copy the environment args */
3342 for (p = env; nargc > 0; nargc--) {
3343 p += strspn(p, SEPARATOR); /* skip separators */
3344 *(nargv++) = p; /* store start */
3345 while (*p++); /* skip over word */
3346 }
3347
3348 /* Finally copy the old args and add a NULL (usual convention) */
3349 while (oargc--)
3350 *(nargv++) = *(oargv++);
3351 *nargv = NULL;
3352 return env;
3353}
3354
3355/* ========================================================================
3356 * Display compression ratio on the given stream on 6 characters.
3357 */
3358void display_ratio(num, den, file)
3359long num;
3360long den;
3361FILE *file;
3362{
3363 long ratio; /* 1000 times the compression ratio */
3364
3365 if (den == 0) {
3366 ratio = 0; /* no compression */
3367 } else if (den < 2147483L) { /* (2**31 -1)/1000 */
3368 ratio = 1000L * num / den;
3369 } else {
3370 ratio = num / (den / 1000L);
3371 }
3372 if (ratio < 0) {
3373 putc('-', file);
3374 ratio = -ratio;
3375 } else {
3376 putc(' ', file);
3377 }
3378 fprintf(file, "%2ld.%1ld%%", ratio / 10L, ratio % 10L);
3379}
3380
3381
3382/* zip.c -- compress files to the gzip or pkzip format 2459/* zip.c -- compress files to the gzip or pkzip format
3383 * Copyright (C) 1992-1993 Jean-loup Gailly 2460 * Copyright (C) 1992-1993 Jean-loup Gailly
3384 * This is free software; you can redistribute it and/or modify it under the 2461 * This is free software; you can redistribute it and/or modify it under the
3385 * terms of the GNU General Public License, see the file COPYING. 2462 * terms of the GNU General Public License, see the file COPYING.
3386 */ 2463 */
3387 2464
3388#include <ctype.h>
3389#include <sys/types.h>
3390
3391#ifdef HAVE_UNISTD_H
3392# include <unistd.h>
3393#endif
3394#ifndef NO_FCNTL_H
3395# include <fcntl.h>
3396#endif
3397 2465
3398local ulg crc; /* crc on uncompressed file data */ 2466static ulg crc; /* crc on uncompressed file data */
3399long header_bytes; /* number of bytes in gzip header */ 2467static long header_bytes; /* number of bytes in gzip header */
3400 2468
3401/* =========================================================================== 2469/* ===========================================================================
3402 * Deflate in to out. 2470 * Deflate in to out.
3403 * IN assertions: the input and output buffers are cleared. 2471 * IN assertions: the input and output buffers are cleared.
3404 * The variables time_stamp and save_orig_name are initialized. 2472 * The variables time_stamp and save_orig_name are initialized.
3405 */ 2473 */
3406int zip(in, out) 2474static int zip(int in, int out)
3407int in, out; /* input and output file descriptors */
3408{ 2475{
3409 uch my_flags = 0; /* general purpose bit flags */ 2476 uch my_flags = 0; /* general purpose bit flags */
3410 ush attr = 0; /* ascii/binary flag */ 2477 ush attr = 0; /* ascii/binary flag */
@@ -3454,9 +2521,7 @@ int in, out; /* input and output file descriptors */
3454 * translation, and update the crc and input file size. 2521 * translation, and update the crc and input file size.
3455 * IN assertion: size >= 2 (for end-of-line translation) 2522 * IN assertion: size >= 2 (for end-of-line translation)
3456 */ 2523 */
3457int file_read(buf, size) 2524static int file_read(char *buf, unsigned size)
3458char *buf;
3459unsigned size;
3460{ 2525{
3461 unsigned len; 2526 unsigned len;
3462 2527
@@ -3475,12 +2540,11 @@ unsigned size;
3475 * Write the output buffer outbuf[0..outcnt-1] and update bytes_out. 2540 * Write the output buffer outbuf[0..outcnt-1] and update bytes_out.
3476 * (used for the compressed data only) 2541 * (used for the compressed data only)
3477 */ 2542 */
3478void flush_outbuf() 2543static void flush_outbuf()
3479{ 2544{
3480 if (outcnt == 0) 2545 if (outcnt == 0)
3481 return; 2546 return;
3482 2547
3483 write_buf(ofd, (char *) outbuf, outcnt); 2548 write_buf(ofd, (char *) outbuf, outcnt);
3484 bytes_out += (ulg) outcnt;
3485 outcnt = 0; 2549 outcnt = 0;
3486} 2550}
diff --git a/gzip.c b/gzip.c
index c4c592729..cdf226889 100644
--- a/gzip.c
+++ b/gzip.c
@@ -35,14 +35,20 @@
35#define SMALL_MEM 35#define SMALL_MEM
36#define DYN_ALLOC 36#define DYN_ALLOC
37 37
38/* I don't like nested includes, but the string and io functions are used
39 * too often
40 */
41#include <stdlib.h> 38#include <stdlib.h>
42#include <stdio.h> 39#include <stdio.h>
43#include <string.h> 40#include <string.h>
44#include <unistd.h> 41#include <unistd.h>
45#include <errno.h> 42#include <errno.h>
43#include <sys/types.h>
44#include <signal.h>
45#include <utime.h>
46#include <ctype.h>
47#include <sys/types.h>
48#include <unistd.h>
49#include <dirent.h>
50#include <fcntl.h>
51#include <time.h>
46#include "busybox.h" 52#include "busybox.h"
47 53
48#define memzero(s, n) memset ((void *)(s), 0, (n)) 54#define memzero(s, n) memset ((void *)(s), 0, (n))
@@ -51,8 +57,6 @@
51# define RETSIGTYPE void 57# define RETSIGTYPE void
52#endif 58#endif
53 59
54#define local static
55
56typedef unsigned char uch; 60typedef unsigned char uch;
57typedef unsigned short ush; 61typedef unsigned short ush;
58typedef unsigned long ulg; 62typedef unsigned long ulg;
@@ -63,22 +67,16 @@ typedef unsigned long ulg;
63#define WARNING 2 67#define WARNING 2
64 68
65/* Compression methods (see algorithm.doc) */ 69/* Compression methods (see algorithm.doc) */
70/* Only STORED and DEFLATED are supported by this BusyBox module */
66#define STORED 0 71#define STORED 0
67#define COMPRESSED 1
68#define PACKED 2
69#define LZHED 3
70/* methods 4 to 7 reserved */ 72/* methods 4 to 7 reserved */
71#define DEFLATED 8 73#define DEFLATED 8
72#define MAX_METHODS 9
73static int method; /* compression method */ 74static int method; /* compression method */
74 75
75/* 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
76 * the various modules: 77 * the various modules:
77 * deflate: prev+head window d_buf l_buf outbuf 78 * deflate: prev+head window d_buf l_buf outbuf
78 * unlzw: tab_prefix tab_suffix stack inbuf outbuf 79 * unlzw: tab_prefix tab_suffix stack inbuf outbuf
79 * inflate: window inbuf
80 * unpack: window inbuf prefix_len
81 * unlzh: left+right window c_table inbuf c_len
82 * For compression, input is done in window[]. For decompression, output 80 * For compression, input is done in window[]. For decompression, output
83 * is done in window except for unlzw. 81 * is done in window except for unlzw.
84 */ 82 */
@@ -110,56 +108,27 @@ static int method; /* compression method */
110#endif 108#endif
111 109
112#ifdef DYN_ALLOC 110#ifdef DYN_ALLOC
113# define EXTERN(type, array) extern type * array 111# define DECLARE(type, array, size) static type * array
114# define DECLARE(type, array, size) type * array
115# define ALLOC(type, array, size) { \ 112# define ALLOC(type, array, size) { \
116 array = (type*)calloc((size_t)(((size)+1L)/2), 2*sizeof(type)); \ 113 array = (type*)calloc((size_t)(((size)+1L)/2), 2*sizeof(type)); \
117 if (array == NULL) error_msg(memory_exhausted); \ 114 if (array == NULL) error_msg(memory_exhausted); \
118 } 115 }
119# define FREE(array) {if (array != NULL) free(array), array=NULL;} 116# define FREE(array) {if (array != NULL) free(array), array=NULL;}
120#else 117#else
121# define EXTERN(type, array) extern type array[] 118# define DECLARE(type, array, size) static type array[size]
122# define DECLARE(type, array, size) type array[size]
123# define ALLOC(type, array, size) 119# define ALLOC(type, array, size)
124# define FREE(array) 120# define FREE(array)
125#endif 121#endif
126 122
127EXTERN(uch, inbuf); /* input buffer */
128EXTERN(uch, outbuf); /* output buffer */
129EXTERN(ush, d_buf); /* buffer for distances, see trees.c */
130EXTERN(uch, window); /* Sliding window and suffix table (unlzw) */
131#define tab_suffix window 123#define tab_suffix window
132#ifndef MAXSEG_64K 124#define tab_prefix prev /* hash link (see deflate.c) */
133# define tab_prefix prev /* hash link (see deflate.c) */ 125#define head (prev+WSIZE) /* hash head (see deflate.c) */
134# define head (prev+WSIZE) /* hash head (see deflate.c) */
135EXTERN(ush, tab_prefix); /* prefix code (see unlzw.c) */
136#else
137# define tab_prefix0 prev
138# define head tab_prefix1
139EXTERN(ush, tab_prefix0); /* prefix for even codes */
140EXTERN(ush, tab_prefix1); /* prefix for odd codes */
141#endif
142 126
143extern unsigned insize; /* valid bytes in inbuf */ 127static long bytes_in; /* number of input bytes */
144static unsigned inptr; /* index of next byte to be processed in inbuf */
145extern unsigned outcnt; /* bytes in output buffer */
146
147extern long bytes_in; /* number of input bytes */
148extern long bytes_out; /* number of output bytes */
149extern long header_bytes; /* number of bytes in gzip header */
150 128
151#define isize bytes_in 129#define isize bytes_in
152/* for compatibility with old zip sources (to be cleaned) */ 130/* for compatibility with old zip sources (to be cleaned) */
153 131
154extern int ifd; /* input file descriptor */
155extern int ofd; /* output file descriptor */
156extern char ifname[]; /* input file name or "stdin" */
157extern char ofname[]; /* output file name or "stdout" */
158extern char *progname; /* program name */
159
160extern long time_stamp; /* original time stamp (modification time) */
161extern long ifile_size; /* input file size, -1 for devices (debug only) */
162
163typedef int file_t; /* Do not use stdio */ 132typedef int file_t; /* Do not use stdio */
164 133
165#define NO_FILE (-1) /* in memory compression */ 134#define NO_FILE (-1) /* in memory compression */
@@ -177,7 +146,6 @@ typedef int file_t; /* Do not use stdio */
177#define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */ 146#define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
178#define ORIG_NAME 0x08 /* bit 3 set: original file name present */ 147#define ORIG_NAME 0x08 /* bit 3 set: original file name present */
179#define COMMENT 0x10 /* bit 4 set: file comment present */ 148#define COMMENT 0x10 /* bit 4 set: file comment present */
180#define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
181#define RESERVED 0xC0 /* bit 6,7: reserved */ 149#define RESERVED 0xC0 /* bit 6,7: reserved */
182 150
183/* internal file attribute */ 151/* internal file attribute */
@@ -203,25 +171,9 @@ typedef int file_t; /* Do not use stdio */
203 * distances are limited to MAX_DIST instead of WSIZE. 171 * distances are limited to MAX_DIST instead of WSIZE.
204 */ 172 */
205 173
206extern int decrypt; /* flag to turn on decryption */ 174/* put_byte is used for the compressed output */
207extern int exit_code; /* program exit code */
208extern int verbose; /* be verbose (-v) */
209extern int quiet; /* be quiet (-q) */
210extern int test; /* check .z file integrity */
211extern int save_orig_name; /* set if original name must be saved */
212
213#define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf(0))
214#define try_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf(1))
215
216/* put_byte is used for the compressed output, put_ubyte for the
217 * uncompressed output. However unlzw() uses window for its
218 * suffix table instead of its output buffer, so it does not use put_ubyte
219 * (to be cleaned up).
220 */
221#define put_byte(c) {outbuf[outcnt++]=(uch)(c); if (outcnt==OUTBUFSIZ)\ 175#define put_byte(c) {outbuf[outcnt++]=(uch)(c); if (outcnt==OUTBUFSIZ)\
222 flush_outbuf();} 176 flush_outbuf();}
223#define put_ubyte(c) {window[outcnt++]=(uch)(c); if (outcnt==WSIZE)\
224 flush_window();}
225 177
226/* Output a 16 bit value, lsb first */ 178/* Output a 16 bit value, lsb first */
227#define put_short(w) \ 179#define put_short(w) \
@@ -243,12 +195,6 @@ extern int save_orig_name; /* set if original name must be saved */
243#define seekable() 0 /* force sequential output */ 195#define seekable() 0 /* force sequential output */
244#define translate_eol 0 /* no option -a yet */ 196#define translate_eol 0 /* no option -a yet */
245 197
246#define tolow(c) (isupper(c) ? (c)-'A'+'a' : (c)) /* force to lower case */
247
248/* Macros for getting two-byte and four-byte header values */
249#define SH(p) ((ush)(uch)((p)[0]) | ((ush)(uch)((p)[1]) << 8))
250#define LG(p) ((ulg)(SH(p)) | ((ulg)(SH((p)+2)) << 16))
251
252/* Diagnostic functions */ 198/* Diagnostic functions */
253#ifdef DEBUG 199#ifdef DEBUG
254# define Assert(cond,msg) {if(!(cond)) error_msg(msg);} 200# define Assert(cond,msg) {if(!(cond)) error_msg(msg);}
@@ -269,56 +215,38 @@ extern int save_orig_name; /* set if original name must be saved */
269#define WARN(msg) {if (!quiet) fprintf msg ; \ 215#define WARN(msg) {if (!quiet) fprintf msg ; \
270 if (exit_code == OK) exit_code = WARNING;} 216 if (exit_code == OK) exit_code = WARNING;}
271 217
218#ifndef MAX_PATH_LEN
219# define MAX_PATH_LEN 1024 /* max pathname length */
220#endif
221
222
272 223
273 /* in zip.c: */ 224 /* from zip.c: */
274extern int zip (int in, int out); 225static int zip (int in, int out);
275extern int file_read (char *buf, unsigned size); 226static int file_read (char *buf, unsigned size);
276 227
277 /* in unzip.c */ 228 /* from gzip.c */
278extern int check_zipfile (int in); 229static RETSIGTYPE abort_gzip (void);
279 230
280 /* in unpack.c */ 231 /* from deflate.c */
281extern int unpack (int in, int out); 232static void lm_init (ush * flags);
282 233static ulg deflate (void);
283 /* in unlzh.c */ 234
284extern int unlzh (int in, int out); 235 /* from trees.c */
285 236static void ct_init (ush * attr, int *methodp);
286 /* in gzip.c */ 237static int ct_tally (int dist, int lc);
287RETSIGTYPE abort_gzip (void); 238static ulg flush_block (char *buf, ulg stored_len, int eof);
288 239
289 /* in deflate.c */ 240 /* from bits.c */
290void lm_init (ush * flags); 241static void bi_init (file_t zipfile);
291ulg deflate (void); 242static void send_bits (int value, int length);
292 243static unsigned bi_reverse (unsigned value, int length);
293 /* in trees.c */ 244static void bi_windup (void);
294void ct_init (ush * attr, int *methodp); 245static void copy_block (char *buf, unsigned len, int header);
295int ct_tally (int dist, int lc); 246static int (*read_buf) (char *buf, unsigned size);
296ulg flush_block (char *buf, ulg stored_len, int eof); 247
297 248 /* from util.c: */
298 /* in bits.c */ 249static void flush_outbuf (void);
299void bi_init (file_t zipfile);
300void send_bits (int value, int length);
301unsigned bi_reverse (unsigned value, int length);
302void bi_windup (void);
303void copy_block (char *buf, unsigned len, int header);
304extern int (*read_buf) (char *buf, unsigned size);
305
306 /* in util.c: */
307extern int copy (int in, int out);
308//extern ulg updcrc (uch * s, unsigned n);
309//extern void clear_bufs (void);
310extern int fill_inbuf (int eof_ok);
311extern void flush_outbuf (void);
312extern void flush_window (void);
313//extern void write_buf (int fd, void * buf, unsigned cnt);
314extern char *strlwr (char *s);
315extern char *add_envopt (int *argcp, char ***argvp, char *env);
316//extern void read_error_msg (void);
317//extern void write_error_msg (void);
318extern void display_ratio (long num, long den, FILE * file);
319
320 /* in inflate.c */
321extern int inflate (void);
322 250
323/* lzw.h -- define the lzw functions. 251/* lzw.h -- define the lzw functions.
324 * Copyright (C) 1992-1993 Jean-loup Gailly. 252 * Copyright (C) 1992-1993 Jean-loup Gailly.
@@ -345,34 +273,6 @@ extern int inflate (void);
345 * "can only handle 16 bits". 273 * "can only handle 16 bits".
346 */ 274 */
347 275
348#define BLOCK_MODE 0x80
349/* Block compression: if table is full and compression rate is dropping,
350 * clear the dictionary.
351 */
352
353#define LZW_RESERVED 0x60 /* reserved bits */
354
355#define CLEAR 256 /* flush the dictionary */
356#define FIRST (CLEAR+1) /* first free entry */
357
358extern int maxbits; /* max bits per code for LZW */
359extern int block_mode; /* block compress mode -C compatible with 2.0 */
360
361/* revision.h -- define the version number
362 * Copyright (C) 1992-1993 Jean-loup Gailly.
363 * This is free software; you can redistribute it and/or modify it under the
364 * terms of the GNU General Public License, see the file COPYING.
365 */
366
367#define VERSION "1.2.4"
368#define PATCHLEVEL 0
369#define REVDATE "18 Aug 93"
370
371/* This version does not support compression into old compress format: */
372#ifdef LZW
373# undef LZW
374#endif
375
376/* tailor.h -- target dependent definitions 276/* tailor.h -- target dependent definitions
377 * Copyright (C) 1992-1993 Jean-loup Gailly. 277 * Copyright (C) 1992-1993 Jean-loup Gailly.
378 * This is free software; you can redistribute it and/or modify it under the 278 * This is free software; you can redistribute it and/or modify it under the
@@ -384,259 +284,6 @@ extern int block_mode; /* block compress mode -C compatible with 2.0 */
384 */ 284 */
385 285
386 286
387#if defined(__MSDOS__) && !defined(MSDOS)
388# define MSDOS
389#endif
390
391#if defined(__OS2__) && !defined(OS2)
392# define OS2
393#endif
394
395#if defined(OS2) && defined(MSDOS) /* MS C under OS/2 */
396# undef MSDOS
397#endif
398
399#ifdef MSDOS
400# ifdef __GNUC__
401 /* DJGPP version 1.09+ on MS-DOS.
402 * The DJGPP 1.09 stat() function must be upgraded before gzip will
403 * fully work.
404 * No need for DIRENT, since <unistd.h> defines POSIX_SOURCE which
405 * implies DIRENT.
406 */
407# define near
408# else
409# define MAXSEG_64K
410# ifdef __TURBOC__
411# define NO_OFF_T
412# ifdef __BORLANDC__
413# define DIRENT
414# else
415# define NO_UTIME
416# endif
417# else /* MSC */
418# define HAVE_SYS_UTIME_H
419# define NO_UTIME_H
420# endif
421# endif
422# define PATH_SEP2 '\\'
423# define PATH_SEP3 ':'
424# define MAX_PATH_LEN 128
425# define NO_MULTIPLE_DOTS
426# define MAX_EXT_CHARS 3
427# define Z_SUFFIX "z"
428# define NO_CHOWN
429# define PROTO
430# define STDC_HEADERS
431# define NO_SIZE_CHECK
432# define casemap(c) tolow(c) /* Force file names to lower case */
433# include <io.h>
434# define OS_CODE 0x00
435# define SET_BINARY_MODE(fd) setmode(fd, O_BINARY)
436# if !defined(NO_ASM) && !defined(ASMV)
437# define ASMV
438# endif
439#else
440# define near
441#endif
442
443#ifdef OS2
444# define PATH_SEP2 '\\'
445# define PATH_SEP3 ':'
446# define MAX_PATH_LEN 260
447# ifdef OS2FAT
448# define NO_MULTIPLE_DOTS
449# define MAX_EXT_CHARS 3
450# define Z_SUFFIX "z"
451# define casemap(c) tolow(c)
452# endif
453# define NO_CHOWN
454# define PROTO
455# define STDC_HEADERS
456# include <io.h>
457# define OS_CODE 0x06
458# define SET_BINARY_MODE(fd) setmode(fd, O_BINARY)
459# ifdef _MSC_VER
460# define HAVE_SYS_UTIME_H
461# define NO_UTIME_H
462# define MAXSEG_64K
463# undef near
464# define near _near
465# endif
466# ifdef __EMX__
467# define HAVE_SYS_UTIME_H
468# define NO_UTIME_H
469# define DIRENT
470# define EXPAND(argc,argv) \
471 {_response(&argc, &argv); _wildcard(&argc, &argv);}
472# endif
473# ifdef __BORLANDC__
474# define DIRENT
475# endif
476# ifdef __ZTC__
477# define NO_DIR
478# define NO_UTIME_H
479# include <dos.h>
480# define EXPAND(argc,argv) \
481 {response_expand(&argc, &argv);}
482# endif
483#endif
484
485#ifdef WIN32 /* Windows NT */
486# define HAVE_SYS_UTIME_H
487# define NO_UTIME_H
488# define PATH_SEP2 '\\'
489# define PATH_SEP3 ':'
490# define MAX_PATH_LEN 260
491# define NO_CHOWN
492# define PROTO
493# define STDC_HEADERS
494# define SET_BINARY_MODE(fd) setmode(fd, O_BINARY)
495# include <io.h>
496# include <malloc.h>
497# ifdef NTFAT
498# define NO_MULTIPLE_DOTS
499# define MAX_EXT_CHARS 3
500# define Z_SUFFIX "z"
501# define casemap(c) tolow(c) /* Force file names to lower case */
502# endif
503# define OS_CODE 0x0b
504#endif
505
506#ifdef MSDOS
507# ifdef __TURBOC__
508# include <alloc.h>
509# define DYN_ALLOC
510 /* Turbo C 2.0 does not accept static allocations of large arrays */
511void *fcalloc(unsigned items, unsigned size);
512void fcfree(void *ptr);
513# else /* MSC */
514# include <malloc.h>
515# define fcalloc(nitems,itemsize) halloc((long)(nitems),(itemsize))
516# define fcfree(ptr) hfree(ptr)
517# endif
518#else
519# ifdef MAXSEG_64K
520# define fcalloc(items,size) calloc((items),(size))
521# else
522# define fcalloc(items,size) malloc((size_t)(items)*(size_t)(size))
523# endif
524# define fcfree(ptr) free(ptr)
525#endif
526
527#if defined(VAXC) || defined(VMS)
528# define PATH_SEP ']'
529# define PATH_SEP2 ':'
530# define SUFFIX_SEP ';'
531# define NO_MULTIPLE_DOTS
532# define Z_SUFFIX "-gz"
533# define RECORD_IO 1
534# define casemap(c) tolow(c)
535# define OS_CODE 0x02
536# define OPTIONS_VAR "GZIP_OPT"
537# define STDC_HEADERS
538# define NO_UTIME
539# define EXPAND(argc,argv) vms_expand_args(&argc,&argv);
540# include <file.h>
541# define unlink delete
542# ifdef VAXC
543# define NO_FCNTL_H
544# include <unixio.h>
545# endif
546#endif
547
548#ifdef AMIGA
549# define PATH_SEP2 ':'
550# define STDC_HEADERS
551# define OS_CODE 0x01
552# define ASMV
553# ifdef __GNUC__
554# define DIRENT
555# define HAVE_UNISTD_H
556# else /* SASC */
557# define NO_STDIN_FSTAT
558# define SYSDIR
559# define NO_SYMLINK
560# define NO_CHOWN
561# define NO_FCNTL_H
562# include <fcntl.h> /* for read() and write() */
563# define direct dirent
564extern void _expand_args(int *argc, char ***argv);
565
566# define EXPAND(argc,argv) _expand_args(&argc,&argv);
567# undef O_BINARY /* disable useless --ascii option */
568# endif
569#endif
570
571#if defined(ATARI) || defined(atarist)
572# ifndef STDC_HEADERS
573# define STDC_HEADERS
574# define HAVE_UNISTD_H
575# define DIRENT
576# endif
577# define ASMV
578# define OS_CODE 0x05
579# ifdef TOSFS
580# define PATH_SEP2 '\\'
581# define PATH_SEP3 ':'
582# define MAX_PATH_LEN 128
583# define NO_MULTIPLE_DOTS
584# define MAX_EXT_CHARS 3
585# define Z_SUFFIX "z"
586# define NO_CHOWN
587# define casemap(c) tolow(c) /* Force file names to lower case */
588# define NO_SYMLINK
589# endif
590#endif
591
592#ifdef MACOS
593# define PATH_SEP ':'
594# define DYN_ALLOC
595# define PROTO
596# define NO_STDIN_FSTAT
597# define NO_CHOWN
598# define NO_UTIME
599# define chmod(file, mode) (0)
600# define OPEN(name, flags, mode) open(name, flags)
601# define OS_CODE 0x07
602# ifdef MPW
603# define isatty(fd) ((fd) <= 2)
604# endif
605#endif
606
607#ifdef __50SERIES /* Prime/PRIMOS */
608# define PATH_SEP '>'
609# define STDC_HEADERS
610# define NO_MEMORY_H
611# define NO_UTIME_H
612# define NO_UTIME
613# define NO_CHOWN
614# define NO_STDIN_FSTAT
615# define NO_SIZE_CHECK
616# define NO_SYMLINK
617# define RECORD_IO 1
618# define casemap(c) tolow(c) /* Force file names to lower case */
619# define put_char(c) put_byte((c) & 0x7F)
620# define get_char(c) ascii2pascii(get_byte())
621# define OS_CODE 0x0F /* temporary, subject to change */
622# ifdef SIGTERM
623# undef SIGTERM /* We don't want a signal handler for SIGTERM */
624# endif
625#endif
626
627#if defined(pyr) && !defined(NOMEMCPY) /* Pyramid */
628# define NOMEMCPY /* problem with overlapping copies */
629#endif
630
631#ifdef TOPS20
632# define OS_CODE 0x0a
633#endif
634
635#ifndef unix
636# define NO_ST_INO /* don't rely on inode numbers */
637#endif
638
639
640 /* Common defaults */ 287 /* Common defaults */
641 288
642#ifndef OS_CODE 289#ifndef OS_CODE
@@ -647,10 +294,6 @@ extern void _expand_args(int *argc, char ***argv);
647# define PATH_SEP '/' 294# define PATH_SEP '/'
648#endif 295#endif
649 296
650#ifndef casemap
651# define casemap(c) (c)
652#endif
653
654#ifndef OPTIONS_VAR 297#ifndef OPTIONS_VAR
655# define OPTIONS_VAR "GZIP" 298# define OPTIONS_VAR "GZIP"
656#endif 299#endif
@@ -665,49 +308,36 @@ extern void _expand_args(int *argc, char ***argv);
665# define MAX_SUFFIX 30 308# define MAX_SUFFIX 30
666#endif 309#endif
667 310
668#ifndef MAKE_LEGAL_NAME 311 /* global buffers */
669# ifdef NO_MULTIPLE_DOTS
670# define MAKE_LEGAL_NAME(name) make_simple_name(name)
671# else
672# define MAKE_LEGAL_NAME(name)
673# endif
674#endif
675
676#ifndef MIN_PART
677# define MIN_PART 3
678 /* keep at least MIN_PART chars between dots in a file name. */
679#endif
680
681#ifndef EXPAND
682# define EXPAND(argc,argv)
683#endif
684
685#ifndef RECORD_IO
686# define RECORD_IO 0
687#endif
688
689#ifndef SET_BINARY_MODE
690# define SET_BINARY_MODE(fd)
691#endif
692 312
693#ifndef OPEN 313DECLARE(uch, inbuf, INBUFSIZ + INBUF_EXTRA);
694# define OPEN(name, flags, mode) open(name, flags, mode) 314DECLARE(uch, outbuf, OUTBUFSIZ + OUTBUF_EXTRA);
695#endif 315DECLARE(ush, d_buf, DIST_BUFSIZE);
316DECLARE(uch, window, 2L * WSIZE);
317DECLARE(ush, tab_prefix, 1L << BITS);
696 318
697#ifndef get_char 319static int crc_table_empty = 1;
698# define get_char() get_byte()
699#endif
700 320
701#ifndef put_char 321static int foreground; /* set if program run in foreground */
702# define put_char(c) put_byte(c) 322static int method = DEFLATED; /* compression method */
703#endif 323static int exit_code = OK; /* program exit code */
324static int part_nb; /* number of parts in .gz file */
325static long time_stamp; /* original time stamp (modification time) */
326static long ifile_size; /* input file size, -1 for devices (debug only) */
327static char z_suffix[MAX_SUFFIX + 1]; /* default suffix (can be set with --suffix) */
328static int z_len; /* strlen(z_suffix) */
704 329
705int crc_table_empty = 1; 330static char ifname[MAX_PATH_LEN]; /* input file name */
331static char ofname[MAX_PATH_LEN]; /* output file name */
332static int ifd; /* input file descriptor */
333static int ofd; /* output file descriptor */
334static unsigned insize; /* valid bytes in inbuf */
335static unsigned outcnt; /* bytes in output buffer */
706 336
707/* ======================================================================== 337/* ========================================================================
708 * Signal and error handler. 338 * Signal and error handler.
709 */ 339 */
710void abort_gzip() 340static void abort_gzip()
711{ 341{
712 exit(ERROR); 342 exit(ERROR);
713} 343}
@@ -718,8 +348,8 @@ void abort_gzip()
718static void clear_bufs(void) 348static void clear_bufs(void)
719{ 349{
720 outcnt = 0; 350 outcnt = 0;
721 insize = inptr = 0; 351 insize = 0;
722 bytes_in = bytes_out = 0L; 352 bytes_in = 0L;
723} 353}
724 354
725static void write_error_msg() 355static void write_error_msg()
@@ -733,10 +363,7 @@ static void write_error_msg()
733 * Does the same as write(), but also handles partial pipe writes and checks 363 * Does the same as write(), but also handles partial pipe writes and checks
734 * for error return. 364 * for error return.
735 */ 365 */
736static void write_buf(fd, buf, cnt) 366static void write_buf(int fd, void *buf, unsigned cnt)
737int fd;
738void * buf;
739unsigned cnt;
740{ 367{
741 unsigned n; 368 unsigned n;
742 369
@@ -749,43 +376,26 @@ unsigned cnt;
749 } 376 }
750} 377}
751 378
752/* ========================================================================
753 * Error handlers.
754 */
755static void read_error_msg()
756{
757 fprintf(stderr, "\n");
758 if (errno != 0) {
759 perror("");
760 } else {
761 fprintf(stderr, "unexpected end of file\n");
762 }
763 abort_gzip();
764}
765
766/* =========================================================================== 379/* ===========================================================================
767 * Run a set of bytes through the crc shift register. If s is a NULL 380 * Run a set of bytes through the crc shift register. If s is a NULL
768 * pointer, then initialize the crc shift register contents instead. 381 * pointer, then initialize the crc shift register contents instead.
769 * Return the current crc in either case. 382 * Return the current crc in either case.
770 */ 383 */
771static ulg updcrc(s, n) 384static ulg updcrc(uch *s, unsigned n)
772uch *s; /* pointer to bytes to pump through */
773unsigned n; /* number of bytes in s[] */
774{ 385{
775 static ulg crc = (ulg) 0xffffffffL; /* shift register contents */ 386 static ulg crc = (ulg) 0xffffffffL; /* shift register contents */
776 register ulg c; /* temporary variable */ 387 register ulg c; /* temporary variable */
777 static unsigned long crc_32_tab[256]; 388 static unsigned long crc_32_tab[256];
778 if (crc_table_empty) { 389 if (crc_table_empty) {
779 unsigned long csr; /* crc shift register */ 390 unsigned long csr; /* crc shift register */
780 unsigned long e; /* polynomial exclusive-or pattern */ 391 unsigned long e=0; /* polynomial exclusive-or pattern */
781 int i; /* counter for all possible eight bit values */ 392 int i; /* counter for all possible eight bit values */
782 int k; /* byte being shifted into crc apparatus */ 393 int k; /* byte being shifted into crc apparatus */
783 394
784 /* terms of polynomial defining this crc (except x^32): */ 395 /* terms of polynomial defining this crc (except x^32): */
785 static int p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26}; 396 static const int p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};
786 397
787 /* Make exclusive-or pattern from polynomial (0xedb88320) */ 398 /* Make exclusive-or pattern from polynomial (0xedb88320) */
788 e = 0;
789 for (i = 0; i < sizeof(p)/sizeof(int); i++) 399 for (i = 0; i < sizeof(p)/sizeof(int); i++)
790 e |= 1L << (31 - p[i]); 400 e |= 1L << (31 - p[i]);
791 401
@@ -868,17 +478,13 @@ unsigned n; /* number of bytes in s[] */
868 * 478 *
869 */ 479 */
870 480
871#ifdef DEBUG
872# include <stdio.h>
873#endif
874
875/* =========================================================================== 481/* ===========================================================================
876 * Local data used by the "bit string" routines. 482 * Local data used by the "bit string" routines.
877 */ 483 */
878 484
879local file_t zfile; /* output gzip file */ 485static file_t zfile; /* output gzip file */
880 486
881local unsigned short bi_buf; 487static unsigned short bi_buf;
882 488
883/* Output buffer. bits are inserted starting at the bottom (least significant 489/* Output buffer. bits are inserted starting at the bottom (least significant
884 * bits). 490 * bits).
@@ -889,13 +495,7 @@ local unsigned short bi_buf;
889 * more than 16 bits on some systems.) 495 * more than 16 bits on some systems.)
890 */ 496 */
891 497
892local int bi_valid; 498static int bi_valid;
893
894/* Number of valid bits in bi_buf. All bits above the last valid bit
895 * are always zero.
896 */
897
898int (*read_buf) (char *buf, unsigned size);
899 499
900/* Current input function. Set to mem_read for in-memory compression */ 500/* Current input function. Set to mem_read for in-memory compression */
901 501
@@ -906,8 +506,7 @@ ulg bits_sent; /* bit length of the compressed data */
906/* =========================================================================== 506/* ===========================================================================
907 * Initialize the bit string routines. 507 * Initialize the bit string routines.
908 */ 508 */
909void bi_init(zipfile) 509static void bi_init(file_t zipfile)
910file_t zipfile; /* output zip file, NO_FILE for in-memory compression */
911{ 510{
912 zfile = zipfile; 511 zfile = zipfile;
913 bi_buf = 0; 512 bi_buf = 0;
@@ -928,9 +527,7 @@ file_t zipfile; /* output zip file, NO_FILE for in-memory compression */
928 * Send a value on a given number of bits. 527 * Send a value on a given number of bits.
929 * IN assertion: length <= 16 and value fits in length bits. 528 * IN assertion: length <= 16 and value fits in length bits.
930 */ 529 */
931void send_bits(value, length) 530static void send_bits(int value, int length)
932int value; /* value to send */
933int length; /* number of bits */
934{ 531{
935#ifdef DEBUG 532#ifdef DEBUG
936 Tracev((stderr, " l %2d v %4x ", length, value)); 533 Tracev((stderr, " l %2d v %4x ", length, value));
@@ -957,9 +554,7 @@ int length; /* number of bits */
957 * method would use a table) 554 * method would use a table)
958 * IN assertion: 1 <= len <= 15 555 * IN assertion: 1 <= len <= 15
959 */ 556 */
960unsigned bi_reverse(code, len) 557static unsigned bi_reverse(unsigned code, int len)
961unsigned code; /* the value to invert */
962int len; /* its bit length */
963{ 558{
964 register unsigned res = 0; 559 register unsigned res = 0;
965 560
@@ -973,7 +568,7 @@ int len; /* its bit length */
973/* =========================================================================== 568/* ===========================================================================
974 * Write out any remaining bits in an incomplete byte. 569 * Write out any remaining bits in an incomplete byte.
975 */ 570 */
976void bi_windup() 571static void bi_windup()
977{ 572{
978 if (bi_valid > 8) { 573 if (bi_valid > 8) {
979 put_short(bi_buf); 574 put_short(bi_buf);
@@ -991,10 +586,7 @@ void bi_windup()
991 * Copy a stored block to the zip file, storing first the length and its 586 * Copy a stored block to the zip file, storing first the length and its
992 * one's complement if requested. 587 * one's complement if requested.
993 */ 588 */
994void copy_block(buf, len, header) 589static void copy_block(char *buf, unsigned len, int header)
995char *buf; /* the input data */
996unsigned len; /* its length */
997int header; /* true if block header must be written */
998{ 590{
999 bi_windup(); /* align on byte boundary */ 591 bi_windup(); /* align on byte boundary */
1000 592
@@ -1009,12 +601,6 @@ int header; /* true if block header must be written */
1009 bits_sent += (ulg) len << 3; 601 bits_sent += (ulg) len << 3;
1010#endif 602#endif
1011 while (len--) { 603 while (len--) {
1012#ifdef CRYPT
1013 int t;
1014
1015 if (key)
1016 zencode(*buf, t);
1017#endif
1018 put_byte(*buf++); 604 put_byte(*buf++);
1019 } 605 }
1020} 606}
@@ -1082,7 +668,6 @@ int header; /* true if block header must be written */
1082 * attributes. 668 * attributes.
1083 */ 669 */
1084 670
1085#include <stdio.h>
1086 671
1087/* =========================================================================== 672/* ===========================================================================
1088 * Configuration parameters 673 * Configuration parameters
@@ -1111,10 +696,10 @@ int header; /* true if block header must be written */
1111 * window with tab_suffix. Check that we can do this: 696 * window with tab_suffix. Check that we can do this:
1112 */ 697 */
1113#if (WSIZE<<1) > (1<<BITS) 698#if (WSIZE<<1) > (1<<BITS)
1114error:cannot overlay window with tab_suffix and prev with tab_prefix0 699# error cannot overlay window with tab_suffix and prev with tab_prefix0
1115#endif 700#endif
1116#if HASH_BITS > BITS-1 701#if HASH_BITS > BITS-1
1117error:cannot overlay head with tab_prefix1 702# error cannot overlay head with tab_prefix1
1118#endif 703#endif
1119#define HASH_SIZE (unsigned)(1<<HASH_BITS) 704#define HASH_SIZE (unsigned)(1<<HASH_BITS)
1120#define HASH_MASK (HASH_SIZE-1) 705#define HASH_MASK (HASH_SIZE-1)
@@ -1159,19 +744,19 @@ typedef unsigned IPos;
1159/* DECLARE(Pos, head, 1<<HASH_BITS); */ 744/* DECLARE(Pos, head, 1<<HASH_BITS); */
1160/* Heads of the hash chains or NIL. */ 745/* Heads of the hash chains or NIL. */
1161 746
1162ulg window_size = (ulg) 2 * WSIZE; 747static const ulg window_size = (ulg) 2 * WSIZE;
1163 748
1164/* window size, 2*WSIZE except for MMAP or BIG_MEM, where it is the 749/* window size, 2*WSIZE except for MMAP or BIG_MEM, where it is the
1165 * input file length plus MIN_LOOKAHEAD. 750 * input file length plus MIN_LOOKAHEAD.
1166 */ 751 */
1167 752
1168long block_start; 753static long block_start;
1169 754
1170/* window position at the beginning of the current output block. Gets 755/* window position at the beginning of the current output block. Gets
1171 * negative when the window is moved backwards. 756 * negative when the window is moved backwards.
1172 */ 757 */
1173 758
1174local unsigned ins_h; /* hash index of string to be inserted */ 759static unsigned ins_h; /* hash index of string to be inserted */
1175 760
1176#define H_SHIFT ((HASH_BITS+MIN_MATCH-1)/MIN_MATCH) 761#define H_SHIFT ((HASH_BITS+MIN_MATCH-1)/MIN_MATCH)
1177/* Number of bits by which ins_h and del_h must be shifted at each 762/* Number of bits by which ins_h and del_h must be shifted at each
@@ -1180,24 +765,24 @@ local unsigned ins_h; /* hash index of string to be inserted */
1180 * H_SHIFT * MIN_MATCH >= HASH_BITS 765 * H_SHIFT * MIN_MATCH >= HASH_BITS
1181 */ 766 */
1182 767
1183unsigned int near prev_length; 768static unsigned int prev_length;
1184 769
1185/* Length of the best match at previous step. Matches not greater than this 770/* Length of the best match at previous step. Matches not greater than this
1186 * are discarded. This is used in the lazy match evaluation. 771 * are discarded. This is used in the lazy match evaluation.
1187 */ 772 */
1188 773
1189unsigned near strstart; /* start of string to insert */ 774static unsigned strstart; /* start of string to insert */
1190unsigned near match_start; /* start of matching string */ 775static unsigned match_start; /* start of matching string */
1191local int eofile; /* flag set at end of input file */ 776static int eofile; /* flag set at end of input file */
1192local unsigned lookahead; /* number of valid bytes ahead in window */ 777static unsigned lookahead; /* number of valid bytes ahead in window */
1193 778
1194unsigned near max_chain_length; 779static const unsigned max_chain_length=4096;
1195 780
1196/* To speed up deflation, hash chains are never searched beyond this length. 781/* To speed up deflation, hash chains are never searched beyond this length.
1197 * A higher limit improves compression ratio but degrades the speed. 782 * A higher limit improves compression ratio but degrades the speed.
1198 */ 783 */
1199 784
1200local unsigned int max_lazy_match; 785static const unsigned int max_lazy_match=258;
1201 786
1202/* Attempt to find a better match only when the current match is strictly 787/* Attempt to find a better match only when the current match is strictly
1203 * smaller than this value. This mechanism is used only for compression 788 * smaller than this value. This mechanism is used only for compression
@@ -1209,7 +794,7 @@ local unsigned int max_lazy_match;
1209 * max_insert_length is used only for compression levels <= 3. 794 * max_insert_length is used only for compression levels <= 3.
1210 */ 795 */
1211 796
1212unsigned near good_match; 797static const unsigned good_match=32;
1213 798
1214/* Use a faster search when the previous match is longer than this */ 799/* Use a faster search when the previous match is longer than this */
1215 800
@@ -1220,22 +805,7 @@ unsigned near good_match;
1220 * found for specific files. 805 * found for specific files.
1221 */ 806 */
1222 807
1223typedef struct config { 808static const int nice_match=258; /* Stop searching when current match exceeds this */
1224 ush good_length; /* reduce lazy search above this match length */
1225 ush max_lazy; /* do not perform lazy search above this match length */
1226 ush nice_length; /* quit search above this match length */
1227 ush max_chain;
1228} config;
1229
1230#ifdef FULL_SEARCH
1231# define nice_match MAX_MATCH
1232#else
1233int near nice_match; /* Stop searching when current match exceeds this */
1234#endif
1235
1236local config configuration_table =
1237 /* 9 */ { 32, 258, 258, 4096 };
1238 /* maximum compression */
1239 809
1240/* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4 810/* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4
1241 * For deflate_fast() (levels <= 3) good is ignored and lazy has a different 811 * For deflate_fast() (levels <= 3) good is ignored and lazy has a different
@@ -1248,16 +818,12 @@ local config configuration_table =
1248/* =========================================================================== 818/* ===========================================================================
1249 * Prototypes for local functions. 819 * Prototypes for local functions.
1250 */ 820 */
1251local void fill_window (void); 821static void fill_window (void);
1252
1253int longest_match (IPos cur_match);
1254 822
1255#ifdef ASMV 823static int longest_match (IPos cur_match);
1256void match_init (void); /* asm code initialization */
1257#endif
1258 824
1259#ifdef DEBUG 825#ifdef DEBUG
1260local void check_match (IPos start, IPos match, int length); 826static void check_match (IPos start, IPos match, int length);
1261#endif 827#endif
1262 828
1263/* =========================================================================== 829/* ===========================================================================
@@ -1284,36 +850,19 @@ local void check_match (IPos start, IPos match, int length);
1284/* =========================================================================== 850/* ===========================================================================
1285 * Initialize the "longest match" routines for a new file 851 * Initialize the "longest match" routines for a new file
1286 */ 852 */
1287void lm_init(flags) 853static void lm_init(ush *flags)
1288ush *flags; /* general purpose bit flag */
1289{ 854{
1290 register unsigned j; 855 register unsigned j;
1291 856
1292 /* Initialize the hash table. */ 857 /* Initialize the hash table. */
1293#if defined(MAXSEG_64K) && HASH_BITS == 15
1294 for (j = 0; j < HASH_SIZE; j++)
1295 head[j] = NIL;
1296#else
1297 memzero((char *) head, HASH_SIZE * sizeof(*head)); 858 memzero((char *) head, HASH_SIZE * sizeof(*head));
1298#endif
1299 /* prev will be initialized on the fly */ 859 /* prev will be initialized on the fly */
1300 860
1301 /* Set the default configuration parameters:
1302 */
1303 max_lazy_match = configuration_table.max_lazy;
1304 good_match = configuration_table.good_length;
1305#ifndef FULL_SEARCH
1306 nice_match = configuration_table.nice_length;
1307#endif
1308 max_chain_length = configuration_table.max_chain;
1309 *flags |= SLOW; 861 *flags |= SLOW;
1310 /* ??? reduce max_chain_length for binary files */ 862 /* ??? reduce max_chain_length for binary files */
1311 863
1312 strstart = 0; 864 strstart = 0;
1313 block_start = 0L; 865 block_start = 0L;
1314#ifdef ASMV
1315 match_init(); /* initialize the asm code */
1316#endif
1317 866
1318 lookahead = read_buf((char *) window, 867 lookahead = read_buf((char *) window,
1319 sizeof(int) <= 2 ? (unsigned) WSIZE : 2 * WSIZE); 868 sizeof(int) <= 2 ? (unsigned) WSIZE : 2 * WSIZE);
@@ -1345,13 +894,12 @@ ush *flags; /* general purpose bit flag */
1345 * IN assertions: cur_match is the head of the hash chain for the current 894 * IN assertions: cur_match is the head of the hash chain for the current
1346 * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1 895 * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
1347 */ 896 */
1348#ifndef ASMV 897
1349/* For MSDOS, OS/2 and 386 Unix, an optimized version is in match.asm or 898/* For MSDOS, OS/2 and 386 Unix, an optimized version is in match.asm or
1350 * match.s. The code is functionally equivalent, so you can use the C version 899 * match.s. The code is functionally equivalent, so you can use the C version
1351 * if desired. 900 * if desired.
1352 */ 901 */
1353int longest_match(cur_match) 902static int longest_match(IPos cur_match)
1354IPos cur_match; /* current match */
1355{ 903{
1356 unsigned chain_length = max_chain_length; /* max hash chain length */ 904 unsigned chain_length = max_chain_length; /* max hash chain length */
1357 register uch *scan = window + strstart; /* current string */ 905 register uch *scan = window + strstart; /* current string */
@@ -1369,20 +917,11 @@ IPos cur_match; /* current match */
1369 * It is easy to get rid of this optimization if necessary. 917 * It is easy to get rid of this optimization if necessary.
1370 */ 918 */
1371#if HASH_BITS < 8 || MAX_MATCH != 258 919#if HASH_BITS < 8 || MAX_MATCH != 258
1372 error:Code too clever 920# error Code too clever
1373#endif 921#endif
1374#ifdef UNALIGNED_OK
1375 /* Compare two bytes at a time. Note: this is not always beneficial.
1376 * Try with and without -DUNALIGNED_OK to check.
1377 */
1378 register uch *strend = window + strstart + MAX_MATCH - 1;
1379 register ush scan_start = *(ush *) scan;
1380 register ush scan_end = *(ush *) (scan + best_len - 1);
1381#else
1382 register uch *strend = window + strstart + MAX_MATCH; 922 register uch *strend = window + strstart + MAX_MATCH;
1383 register uch scan_end1 = scan[best_len - 1]; 923 register uch scan_end1 = scan[best_len - 1];
1384 register uch scan_end = scan[best_len]; 924 register uch scan_end = scan[best_len];
1385#endif
1386 925
1387 /* Do not waste too much time if we already have a good match: */ 926 /* Do not waste too much time if we already have a good match: */
1388 if (prev_length >= good_match) { 927 if (prev_length >= good_match) {
@@ -1398,42 +937,6 @@ IPos cur_match; /* current match */
1398 /* Skip to next match if the match length cannot increase 937 /* Skip to next match if the match length cannot increase
1399 * or if the match length is less than 2: 938 * or if the match length is less than 2:
1400 */ 939 */
1401#if (defined(UNALIGNED_OK) && MAX_MATCH == 258)
1402 /* This code assumes sizeof(unsigned short) == 2. Do not use
1403 * UNALIGNED_OK if your compiler uses a different size.
1404 */
1405 if (*(ush *) (match + best_len - 1) != scan_end ||
1406 *(ush *) match != scan_start)
1407 continue;
1408
1409 /* It is not necessary to compare scan[2] and match[2] since they are
1410 * always equal when the other bytes match, given that the hash keys
1411 * are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at
1412 * strstart+3, +5, ... up to strstart+257. We check for insufficient
1413 * lookahead only every 4th comparison; the 128th check will be made
1414 * at strstart+257. If MAX_MATCH-2 is not a multiple of 8, it is
1415 * necessary to put more guard bytes at the end of the window, or
1416 * to check more often for insufficient lookahead.
1417 */
1418 scan++, match++;
1419 do {
1420 } while (*(ush *) (scan += 2) == *(ush *) (match += 2) &&
1421 *(ush *) (scan += 2) == *(ush *) (match += 2) &&
1422 *(ush *) (scan += 2) == *(ush *) (match += 2) &&
1423 *(ush *) (scan += 2) == *(ush *) (match += 2) &&
1424 scan < strend);
1425 /* The funny "do {}" generates better code on most compilers */
1426
1427 /* Here, scan <= window+strstart+257 */
1428 Assert(scan <= window + (unsigned) (window_size - 1), "wild scan");
1429 if (*scan == *match)
1430 scan++;
1431
1432 len = (MAX_MATCH - 1) - (int) (strend - scan);
1433 scan = strend - (MAX_MATCH - 1);
1434
1435#else /* UNALIGNED_OK */
1436
1437 if (match[best_len] != scan_end || 940 if (match[best_len] != scan_end ||
1438 match[best_len - 1] != scan_end1 || 941 match[best_len - 1] != scan_end1 ||
1439 *match != *scan || *++match != scan[1]) 942 *match != *scan || *++match != scan[1])
@@ -1460,34 +963,25 @@ IPos cur_match; /* current match */
1460 len = MAX_MATCH - (int) (strend - scan); 963 len = MAX_MATCH - (int) (strend - scan);
1461 scan = strend - MAX_MATCH; 964 scan = strend - MAX_MATCH;
1462 965
1463#endif /* UNALIGNED_OK */
1464
1465 if (len > best_len) { 966 if (len > best_len) {
1466 match_start = cur_match; 967 match_start = cur_match;
1467 best_len = len; 968 best_len = len;
1468 if (len >= nice_match) 969 if (len >= nice_match)
1469 break; 970 break;
1470#ifdef UNALIGNED_OK
1471 scan_end = *(ush *) (scan + best_len - 1);
1472#else
1473 scan_end1 = scan[best_len - 1]; 971 scan_end1 = scan[best_len - 1];
1474 scan_end = scan[best_len]; 972 scan_end = scan[best_len];
1475#endif
1476 } 973 }
1477 } while ((cur_match = prev[cur_match & WMASK]) > limit 974 } while ((cur_match = prev[cur_match & WMASK]) > limit
1478 && --chain_length != 0); 975 && --chain_length != 0);
1479 976
1480 return best_len; 977 return best_len;
1481} 978}
1482#endif /* ASMV */
1483 979
1484#ifdef DEBUG 980#ifdef DEBUG
1485/* =========================================================================== 981/* ===========================================================================
1486 * Check that the match at match_start is indeed a match. 982 * Check that the match at match_start is indeed a match.
1487 */ 983 */
1488local void check_match(start, match, length) 984static void check_match(IPos start, IPos match, int length)
1489IPos start, match;
1490int length;
1491{ 985{
1492 /* check that the match is indeed a match */ 986 /* check that the match is indeed a match */
1493 if (memcmp((char *) window + match, 987 if (memcmp((char *) window + match,
@@ -1515,7 +1009,7 @@ int length;
1515 * file reads are performed for at least two bytes (required for the 1009 * file reads are performed for at least two bytes (required for the
1516 * translate_eol option). 1010 * translate_eol option).
1517 */ 1011 */
1518local void fill_window() 1012static void fill_window()
1519{ 1013{
1520 register unsigned n, m; 1014 register unsigned n, m;
1521 unsigned more = 1015 unsigned more =
@@ -1580,7 +1074,7 @@ local void fill_window()
1580 * evaluation for matches: a match is finally adopted only if there is 1074 * evaluation for matches: a match is finally adopted only if there is
1581 * no better match at the next window position. 1075 * no better match at the next window position.
1582 */ 1076 */
1583ulg deflate() 1077static ulg deflate()
1584{ 1078{
1585 IPos hash_head; /* head of hash chain */ 1079 IPos hash_head; /* head of hash chain */
1586 IPos prev_match; /* previous match */ 1080 IPos prev_match; /* previous match */
@@ -1588,10 +1082,6 @@ ulg deflate()
1588 int match_available = 0; /* set if previous match exists */ 1082 int match_available = 0; /* set if previous match exists */
1589 register unsigned match_length = MIN_MATCH - 1; /* length of best match */ 1083 register unsigned match_length = MIN_MATCH - 1; /* length of best match */
1590 1084
1591#ifdef DEBUG
1592 extern long isize; /* byte length of input file, for debug only */
1593#endif
1594
1595 /* Process the input block. */ 1085 /* Process the input block. */
1596 while (lookahead != 0) { 1086 while (lookahead != 0) {
1597 /* Insert the string window[strstart .. strstart+2] in the 1087 /* Insert the string window[strstart .. strstart+2] in the
@@ -1708,180 +1198,14 @@ ulg deflate()
1708 * or stdout with -c option or if stdin used as input. 1198 * or stdout with -c option or if stdin used as input.
1709 * If the output file name had to be truncated, the original name is kept 1199 * If the output file name had to be truncated, the original name is kept
1710 * in the compressed file. 1200 * in the compressed file.
1711 * On MSDOS, file.tmp -> file.tmz. On VMS, file.tmp -> file.tmp-gz.
1712 *
1713 * Using gz on MSDOS would create too many file name conflicts. For
1714 * example, foo.txt -> foo.tgz (.tgz must be reserved as shorthand for
1715 * tar.gz). Similarly, foo.dir and foo.doc would both be mapped to foo.dgz.
1716 * I also considered 12345678.txt -> 12345txt.gz but this truncates the name
1717 * too heavily. There is no ideal solution given the MSDOS 8+3 limitation.
1718 *
1719 * For the meaning of all compilation flags, see comments in Makefile.in.
1720 */ 1201 */
1721 1202
1722#include <ctype.h>
1723#include <sys/types.h>
1724#include <signal.h>
1725#include <errno.h>
1726
1727 /* configuration */ 1203 /* configuration */
1728 1204
1729#ifdef NO_TIME_H
1730# include <sys/time.h>
1731#else
1732# include <time.h>
1733#endif
1734
1735#ifndef NO_FCNTL_H
1736# include <fcntl.h>
1737#endif
1738
1739#ifdef HAVE_UNISTD_H
1740# include <unistd.h>
1741#endif
1742
1743#if defined(DIRENT)
1744# include <dirent.h>
1745typedef struct dirent dir_type; 1205typedef struct dirent dir_type;
1746 1206
1747# define NLENGTH(dirent) ((int)strlen((dirent)->d_name))
1748# define DIR_OPT "DIRENT"
1749#else
1750# define NLENGTH(dirent) ((dirent)->d_namlen)
1751# ifdef SYSDIR
1752# include <sys/dir.h>
1753typedef struct direct dir_type;
1754
1755# define DIR_OPT "SYSDIR"
1756# else
1757# ifdef SYSNDIR
1758# include <sys/ndir.h>
1759typedef struct direct dir_type;
1760
1761# define DIR_OPT "SYSNDIR"
1762# else
1763# ifdef NDIR
1764# include <ndir.h>
1765typedef struct direct dir_type;
1766
1767# define DIR_OPT "NDIR"
1768# else
1769# define NO_DIR
1770# define DIR_OPT "NO_DIR"
1771# endif
1772# endif
1773# endif
1774#endif
1775
1776#ifndef NO_UTIME
1777# ifndef NO_UTIME_H
1778# include <utime.h>
1779# define TIME_OPT "UTIME"
1780# else
1781# ifdef HAVE_SYS_UTIME_H
1782# include <sys/utime.h>
1783# define TIME_OPT "SYS_UTIME"
1784# else
1785struct utimbuf {
1786 time_t actime;
1787 time_t modtime;
1788};
1789
1790# define TIME_OPT ""
1791# endif
1792# endif
1793#else
1794# define TIME_OPT "NO_UTIME"
1795#endif
1796
1797#if !defined(S_ISDIR) && defined(S_IFDIR)
1798# define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
1799#endif
1800#if !defined(S_ISREG) && defined(S_IFREG)
1801# define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
1802#endif
1803
1804typedef RETSIGTYPE(*sig_type) (int); 1207typedef RETSIGTYPE(*sig_type) (int);
1805 1208
1806#ifndef O_BINARY
1807# define O_BINARY 0 /* creation mode for open() */
1808#endif
1809
1810#ifndef O_CREAT
1811 /* Pure BSD system? */
1812# include <sys/file.h>
1813# ifndef O_CREAT
1814# define O_CREAT FCREAT
1815# endif
1816# ifndef O_EXCL
1817# define O_EXCL FEXCL
1818# endif
1819#endif
1820
1821#ifndef S_IRUSR
1822# define S_IRUSR 0400
1823#endif
1824#ifndef S_IWUSR
1825# define S_IWUSR 0200
1826#endif
1827#define RW_USER (S_IRUSR | S_IWUSR) /* creation mode for open() */
1828
1829#ifndef MAX_PATH_LEN
1830# define MAX_PATH_LEN 1024 /* max pathname length */
1831#endif
1832
1833#ifndef SEEK_END
1834# define SEEK_END 2
1835#endif
1836
1837#ifdef NO_OFF_T
1838typedef long off_t;
1839off_t lseek (int fd, off_t offset, int whence);
1840#endif
1841
1842/* Separator for file name parts (see shorten_name()) */
1843#ifdef NO_MULTIPLE_DOTS
1844# define PART_SEP "-"
1845#else
1846# define PART_SEP "."
1847#endif
1848
1849 /* global buffers */
1850
1851DECLARE(uch, inbuf, INBUFSIZ + INBUF_EXTRA);
1852DECLARE(uch, outbuf, OUTBUFSIZ + OUTBUF_EXTRA);
1853DECLARE(ush, d_buf, DIST_BUFSIZE);
1854DECLARE(uch, window, 2L * WSIZE);
1855#ifndef MAXSEG_64K
1856DECLARE(ush, tab_prefix, 1L << BITS);
1857#else
1858DECLARE(ush, tab_prefix0, 1L << (BITS - 1));
1859DECLARE(ush, tab_prefix1, 1L << (BITS - 1));
1860#endif
1861
1862 /* local variables */
1863
1864static int foreground; /* set if program run in foreground */
1865static int method = DEFLATED; /* compression method */
1866static int exit_code = OK; /* program exit code */
1867static int part_nb; /* number of parts in .gz file */
1868static long time_stamp; /* original time stamp (modification time) */
1869static long ifile_size; /* input file size, -1 for devices (debug only) */
1870static char z_suffix[MAX_SUFFIX + 1]; /* default suffix (can be set with --suffix) */
1871static int z_len; /* strlen(z_suffix) */
1872
1873static long bytes_in; /* number of input bytes */
1874static long bytes_out; /* number of output bytes */
1875static char ifname[MAX_PATH_LEN]; /* input file name */
1876static char ofname[MAX_PATH_LEN]; /* output file name */
1877static int ifd; /* input file descriptor */
1878static int ofd; /* output file descriptor */
1879static unsigned insize; /* valid bytes in inbuf */
1880static unsigned outcnt; /* bytes in output buffer */
1881
1882/* local functions */
1883
1884#define strequ(s1, s2) (strcmp((s1),(s2)) == 0)
1885 1209
1886/* ======================================================================== */ 1210/* ======================================================================== */
1887// int main (argc, argv) 1211// int main (argc, argv)
@@ -1953,12 +1277,7 @@ int gzip_main(int argc, char **argv)
1953 ALLOC(uch, outbuf, OUTBUFSIZ + OUTBUF_EXTRA); 1277 ALLOC(uch, outbuf, OUTBUFSIZ + OUTBUF_EXTRA);
1954 ALLOC(ush, d_buf, DIST_BUFSIZE); 1278 ALLOC(ush, d_buf, DIST_BUFSIZE);
1955 ALLOC(uch, window, 2L * WSIZE); 1279 ALLOC(uch, window, 2L * WSIZE);
1956#ifndef MAXSEG_64K
1957 ALLOC(ush, tab_prefix, 1L << BITS); 1280 ALLOC(ush, tab_prefix, 1L << BITS);
1958#else
1959 ALLOC(ush, tab_prefix0, 1L << (BITS - 1));
1960 ALLOC(ush, tab_prefix1, 1L << (BITS - 1));
1961#endif
1962 1281
1963 if (fromstdin == 1) { 1282 if (fromstdin == 1) {
1964 strcpy(ofname, "stdin"); 1283 strcpy(ofname, "stdin");
@@ -1986,7 +1305,6 @@ int gzip_main(int argc, char **argv)
1986 /* And get to work */ 1305 /* And get to work */
1987 strcpy(ofname, "stdout"); 1306 strcpy(ofname, "stdout");
1988 outFileNum = fileno(stdout); 1307 outFileNum = fileno(stdout);
1989 SET_BINARY_MODE(fileno(stdout));
1990 1308
1991 clear_bufs(); /* clear input and output buffers */ 1309 clear_bufs(); /* clear input and output buffers */
1992 part_nb = 0; 1310 part_nb = 0;
@@ -2009,7 +1327,6 @@ int gzip_main(int argc, char **argv)
2009#endif 1327#endif
2010 if (outFileNum < 0) 1328 if (outFileNum < 0)
2011 perror_msg_and_die("%s", ofname); 1329 perror_msg_and_die("%s", ofname);
2012 SET_BINARY_MODE(outFileNum);
2013 /* Set permissions on the file */ 1330 /* Set permissions on the file */
2014 fchmod(outFileNum, statBuf.st_mode); 1331 fchmod(outFileNum, statBuf.st_mode);
2015 1332
@@ -2088,8 +1405,6 @@ int gzip_main(int argc, char **argv)
2088 * 1405 *
2089 */ 1406 */
2090 1407
2091#include <ctype.h>
2092
2093/* =========================================================================== 1408/* ===========================================================================
2094 * Constants 1409 * Constants
2095 */ 1410 */
@@ -2119,15 +1434,15 @@ int gzip_main(int argc, char **argv)
2119/* number of codes used to transfer the bit lengths */ 1434/* number of codes used to transfer the bit lengths */
2120 1435
2121 1436
2122local int near extra_lbits[LENGTH_CODES] /* extra bits for each length code */ 1437static const int extra_lbits[LENGTH_CODES] /* extra bits for each length code */
2123 = { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 1438 = { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4,
2124 4, 4, 5, 5, 5, 5, 0 }; 1439 4, 4, 5, 5, 5, 5, 0 };
2125 1440
2126local int near extra_dbits[D_CODES] /* extra bits for each distance code */ 1441static const int extra_dbits[D_CODES] /* extra bits for each distance code */
2127 = { 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 1442 = { 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9,
2128 10, 10, 11, 11, 12, 12, 13, 13 }; 1443 10, 10, 11, 11, 12, 12, 13, 13 };
2129 1444
2130local int near extra_blbits[BL_CODES] /* extra bits for each bit length code */ 1445static const int extra_blbits[BL_CODES] /* extra bits for each bit length code */
2131= { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 3, 7 }; 1446= { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 3, 7 };
2132 1447
2133#define STORED_BLOCK 0 1448#define STORED_BLOCK 0
@@ -2198,10 +1513,10 @@ error cannot overlay l_buf and inbuf
2198#define HEAP_SIZE (2*L_CODES+1) 1513#define HEAP_SIZE (2*L_CODES+1)
2199/* maximum heap size */ 1514/* maximum heap size */
2200 1515
2201local ct_data near dyn_ltree[HEAP_SIZE]; /* literal and length tree */ 1516static ct_data dyn_ltree[HEAP_SIZE]; /* literal and length tree */
2202local ct_data near dyn_dtree[2 * D_CODES + 1]; /* distance tree */ 1517static ct_data dyn_dtree[2 * D_CODES + 1]; /* distance tree */
2203 1518
2204local ct_data near static_ltree[L_CODES + 2]; 1519static ct_data static_ltree[L_CODES + 2];
2205 1520
2206/* The static literal tree. Since the bit lengths are imposed, there is no 1521/* The static literal tree. Since the bit lengths are imposed, there is no
2207 * need for the L_CODES extra codes used during heap construction. However 1522 * need for the L_CODES extra codes used during heap construction. However
@@ -2209,77 +1524,77 @@ local ct_data near static_ltree[L_CODES + 2];
2209 * below). 1524 * below).
2210 */ 1525 */
2211 1526
2212local ct_data near static_dtree[D_CODES]; 1527static ct_data static_dtree[D_CODES];
2213 1528
2214/* The static distance tree. (Actually a trivial tree since all codes use 1529/* The static distance tree. (Actually a trivial tree since all codes use
2215 * 5 bits.) 1530 * 5 bits.)
2216 */ 1531 */
2217 1532
2218local ct_data near bl_tree[2 * BL_CODES + 1]; 1533static ct_data bl_tree[2 * BL_CODES + 1];
2219 1534
2220/* Huffman tree for the bit lengths */ 1535/* Huffman tree for the bit lengths */
2221 1536
2222typedef struct tree_desc { 1537typedef struct tree_desc {
2223 ct_data near *dyn_tree; /* the dynamic tree */ 1538 ct_data *dyn_tree; /* the dynamic tree */
2224 ct_data near *static_tree; /* corresponding static tree or NULL */ 1539 ct_data *static_tree; /* corresponding static tree or NULL */
2225 int near *extra_bits; /* extra bits for each code or NULL */ 1540 const int *extra_bits; /* extra bits for each code or NULL */
2226 int extra_base; /* base index for extra_bits */ 1541 int extra_base; /* base index for extra_bits */
2227 int elems; /* max number of elements in the tree */ 1542 int elems; /* max number of elements in the tree */
2228 int max_length; /* max bit length for the codes */ 1543 int max_length; /* max bit length for the codes */
2229 int max_code; /* largest code with non zero frequency */ 1544 int max_code; /* largest code with non zero frequency */
2230} tree_desc; 1545} tree_desc;
2231 1546
2232local tree_desc near l_desc = 1547static tree_desc l_desc =
2233 { dyn_ltree, static_ltree, extra_lbits, LITERALS + 1, L_CODES, 1548 { dyn_ltree, static_ltree, extra_lbits, LITERALS + 1, L_CODES,
2234 MAX_BITS, 0 }; 1549 MAX_BITS, 0 };
2235 1550
2236local tree_desc near d_desc = 1551static tree_desc d_desc =
2237 { dyn_dtree, static_dtree, extra_dbits, 0, D_CODES, MAX_BITS, 0 }; 1552 { dyn_dtree, static_dtree, extra_dbits, 0, D_CODES, MAX_BITS, 0 };
2238 1553
2239local tree_desc near bl_desc = 1554static tree_desc bl_desc =
2240 { bl_tree, (ct_data near *) 0, extra_blbits, 0, BL_CODES, MAX_BL_BITS, 1555 { bl_tree, (ct_data *) 0, extra_blbits, 0, BL_CODES, MAX_BL_BITS,
2241 0 }; 1556 0 };
2242 1557
2243 1558
2244local ush near bl_count[MAX_BITS + 1]; 1559static ush bl_count[MAX_BITS + 1];
2245 1560
2246/* number of codes at each bit length for an optimal tree */ 1561/* number of codes at each bit length for an optimal tree */
2247 1562
2248local uch near bl_order[BL_CODES] 1563static const uch bl_order[BL_CODES]
2249= { 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 }; 1564= { 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 };
2250 1565
2251/* The lengths of the bit length codes are sent in order of decreasing 1566/* The lengths of the bit length codes are sent in order of decreasing
2252 * probability, to avoid transmitting the lengths for unused bit length codes. 1567 * probability, to avoid transmitting the lengths for unused bit length codes.
2253 */ 1568 */
2254 1569
2255local int near heap[2 * L_CODES + 1]; /* heap used to build the Huffman trees */ 1570static int heap[2 * L_CODES + 1]; /* heap used to build the Huffman trees */
2256local int heap_len; /* number of elements in the heap */ 1571static int heap_len; /* number of elements in the heap */
2257local int heap_max; /* element of largest frequency */ 1572static int heap_max; /* element of largest frequency */
2258 1573
2259/* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used. 1574/* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.
2260 * The same heap array is used to build all trees. 1575 * The same heap array is used to build all trees.
2261 */ 1576 */
2262 1577
2263local uch near depth[2 * L_CODES + 1]; 1578static uch depth[2 * L_CODES + 1];
2264 1579
2265/* Depth of each subtree used as tie breaker for trees of equal frequency */ 1580/* Depth of each subtree used as tie breaker for trees of equal frequency */
2266 1581
2267local uch length_code[MAX_MATCH - MIN_MATCH + 1]; 1582static uch length_code[MAX_MATCH - MIN_MATCH + 1];
2268 1583
2269/* length code for each normalized match length (0 == MIN_MATCH) */ 1584/* length code for each normalized match length (0 == MIN_MATCH) */
2270 1585
2271local uch dist_code[512]; 1586static uch dist_code[512];
2272 1587
2273/* distance codes. The first 256 values correspond to the distances 1588/* distance codes. The first 256 values correspond to the distances
2274 * 3 .. 258, the last 256 values correspond to the top 8 bits of 1589 * 3 .. 258, the last 256 values correspond to the top 8 bits of
2275 * the 15 bit distances. 1590 * the 15 bit distances.
2276 */ 1591 */
2277 1592
2278local int near base_length[LENGTH_CODES]; 1593static int base_length[LENGTH_CODES];
2279 1594
2280/* First normalized length for each code (0 = MIN_MATCH) */ 1595/* First normalized length for each code (0 = MIN_MATCH) */
2281 1596
2282local int near base_dist[D_CODES]; 1597static int base_dist[D_CODES];
2283 1598
2284/* First normalized distance for each code (0 = distance of 1) */ 1599/* First normalized distance for each code (0 = distance of 1) */
2285 1600
@@ -2288,58 +1603,47 @@ local int near base_dist[D_CODES];
2288 1603
2289/* DECLARE(ush, d_buf, DIST_BUFSIZE); buffer for distances */ 1604/* DECLARE(ush, d_buf, DIST_BUFSIZE); buffer for distances */
2290 1605
2291local uch near flag_buf[(LIT_BUFSIZE / 8)]; 1606static uch flag_buf[(LIT_BUFSIZE / 8)];
2292 1607
2293/* flag_buf is a bit array distinguishing literals from lengths in 1608/* flag_buf is a bit array distinguishing literals from lengths in
2294 * l_buf, thus indicating the presence or absence of a distance. 1609 * l_buf, thus indicating the presence or absence of a distance.
2295 */ 1610 */
2296 1611
2297local unsigned last_lit; /* running index in l_buf */ 1612static unsigned last_lit; /* running index in l_buf */
2298local unsigned last_dist; /* running index in d_buf */ 1613static unsigned last_dist; /* running index in d_buf */
2299local unsigned last_flags; /* running index in flag_buf */ 1614static unsigned last_flags; /* running index in flag_buf */
2300local uch flags; /* current flags not yet saved in flag_buf */ 1615static uch flags; /* current flags not yet saved in flag_buf */
2301local uch flag_bit; /* current bit used in flags */ 1616static uch flag_bit; /* current bit used in flags */
2302 1617
2303/* bits are filled in flags starting at bit 0 (least significant). 1618/* bits are filled in flags starting at bit 0 (least significant).
2304 * Note: these flags are overkill in the current code since we don't 1619 * Note: these flags are overkill in the current code since we don't
2305 * take advantage of DIST_BUFSIZE == LIT_BUFSIZE. 1620 * take advantage of DIST_BUFSIZE == LIT_BUFSIZE.
2306 */ 1621 */
2307 1622
2308local ulg opt_len; /* bit length of current block with optimal trees */ 1623static ulg opt_len; /* bit length of current block with optimal trees */
2309local ulg static_len; /* bit length of current block with static trees */ 1624static ulg static_len; /* bit length of current block with static trees */
2310
2311local ulg compressed_len; /* total bit length of compressed file */
2312 1625
2313local ulg input_len; /* total byte length of input file */ 1626static ulg compressed_len; /* total bit length of compressed file */
2314 1627
2315/* input_len is for debugging only since we can get it by other means. */
2316 1628
2317ush *file_type; /* pointer to UNKNOWN, BINARY or ASCII */ 1629static ush *file_type; /* pointer to UNKNOWN, BINARY or ASCII */
2318int *file_method; /* pointer to DEFLATE or STORE */ 1630static int *file_method; /* pointer to DEFLATE or STORE */
2319
2320#ifdef DEBUG
2321extern ulg bits_sent; /* bit length of the compressed data */
2322extern long isize; /* byte length of input file */
2323#endif
2324
2325extern long block_start; /* window offset of current block */
2326extern unsigned near strstart; /* window offset of current string */
2327 1631
2328/* =========================================================================== 1632/* ===========================================================================
2329 * Local (static) routines in this file. 1633 * Local (static) routines in this file.
2330 */ 1634 */
2331 1635
2332local void init_block (void); 1636static void init_block (void);
2333local void pqdownheap (ct_data near * tree, int k); 1637static void pqdownheap (ct_data * tree, int k);
2334local void gen_bitlen (tree_desc near * desc); 1638static void gen_bitlen (tree_desc * desc);
2335local void gen_codes (ct_data near * tree, int max_code); 1639static void gen_codes (ct_data * tree, int max_code);
2336local void build_tree (tree_desc near * desc); 1640static void build_tree (tree_desc * desc);
2337local void scan_tree (ct_data near * tree, int max_code); 1641static void scan_tree (ct_data * tree, int max_code);
2338local void send_tree (ct_data near * tree, int max_code); 1642static void send_tree (ct_data * tree, int max_code);
2339local int build_bl_tree (void); 1643static int build_bl_tree (void);
2340local void send_all_trees (int lcodes, int dcodes, int blcodes); 1644static void send_all_trees (int lcodes, int dcodes, int blcodes);
2341local void compress_block (ct_data near * ltree, ct_data near * dtree); 1645static void compress_block (ct_data * ltree, ct_data * dtree);
2342local void set_file_type (void); 1646static void set_file_type (void);
2343 1647
2344 1648
2345#ifndef DEBUG 1649#ifndef DEBUG
@@ -2366,9 +1670,7 @@ local void set_file_type (void);
2366 * location of the internal file attribute (ascii/binary) and method 1670 * location of the internal file attribute (ascii/binary) and method
2367 * (DEFLATE/STORE). 1671 * (DEFLATE/STORE).
2368 */ 1672 */
2369void ct_init(attr, methodp) 1673static void ct_init(ush *attr, int *methodp)
2370ush *attr; /* pointer to internal file attribute */
2371int *methodp; /* pointer to compression method */
2372{ 1674{
2373 int n; /* iterates over tree elements */ 1675 int n; /* iterates over tree elements */
2374 int bits; /* bit counter */ 1676 int bits; /* bit counter */
@@ -2378,7 +1680,7 @@ int *methodp; /* pointer to compression method */
2378 1680
2379 file_type = attr; 1681 file_type = attr;
2380 file_method = methodp; 1682 file_method = methodp;
2381 compressed_len = input_len = 0L; 1683 compressed_len = 0L;
2382 1684
2383 if (static_dtree[0].Len != 0) 1685 if (static_dtree[0].Len != 0)
2384 return; /* ct_init already called */ 1686 return; /* ct_init already called */
@@ -2432,7 +1734,7 @@ int *methodp; /* pointer to compression method */
2432 * tree construction to get a canonical Huffman tree (longest code 1734 * tree construction to get a canonical Huffman tree (longest code
2433 * all ones) 1735 * all ones)
2434 */ 1736 */
2435 gen_codes((ct_data near *) static_ltree, L_CODES + 1); 1737 gen_codes((ct_data *) static_ltree, L_CODES + 1);
2436 1738
2437 /* The static distance tree is trivial: */ 1739 /* The static distance tree is trivial: */
2438 for (n = 0; n < D_CODES; n++) { 1740 for (n = 0; n < D_CODES; n++) {
@@ -2447,7 +1749,7 @@ int *methodp; /* pointer to compression method */
2447/* =========================================================================== 1749/* ===========================================================================
2448 * Initialize a new block. 1750 * Initialize a new block.
2449 */ 1751 */
2450local void init_block() 1752static void init_block()
2451{ 1753{
2452 int n; /* iterates over tree elements */ 1754 int n; /* iterates over tree elements */
2453 1755
@@ -2495,9 +1797,7 @@ local void init_block()
2495 * when the heap property is re-established (each father smaller than its 1797 * when the heap property is re-established (each father smaller than its
2496 * two sons). 1798 * two sons).
2497 */ 1799 */
2498local void pqdownheap(tree, k) 1800static void pqdownheap(ct_data *tree, int k)
2499ct_data near *tree; /* the tree to restore */
2500int k; /* node to move down */
2501{ 1801{
2502 int v = heap[k]; 1802 int v = heap[k];
2503 int j = k << 1; /* left son of k */ 1803 int j = k << 1; /* left son of k */
@@ -2531,15 +1831,14 @@ int k; /* node to move down */
2531 * The length opt_len is updated; static_len is also updated if stree is 1831 * The length opt_len is updated; static_len is also updated if stree is
2532 * not null. 1832 * not null.
2533 */ 1833 */
2534local void gen_bitlen(desc) 1834static void gen_bitlen(tree_desc *desc)
2535tree_desc near *desc; /* the tree descriptor */
2536{ 1835{
2537 ct_data near *tree = desc->dyn_tree; 1836 ct_data *tree = desc->dyn_tree;
2538 int near *extra = desc->extra_bits; 1837 const int *extra = desc->extra_bits;
2539 int base = desc->extra_base; 1838 int base = desc->extra_base;
2540 int max_code = desc->max_code; 1839 int max_code = desc->max_code;
2541 int max_length = desc->max_length; 1840 int max_length = desc->max_length;
2542 ct_data near *stree = desc->static_tree; 1841 ct_data *stree = desc->static_tree;
2543 int h; /* heap index */ 1842 int h; /* heap index */
2544 int n, m; /* iterate over the tree elements */ 1843 int n, m; /* iterate over the tree elements */
2545 int bits; /* bit length */ 1844 int bits; /* bit length */
@@ -2629,9 +1928,7 @@ tree_desc near *desc; /* the tree descriptor */
2629 * OUT assertion: the field code is set for all tree elements of non 1928 * OUT assertion: the field code is set for all tree elements of non
2630 * zero code length. 1929 * zero code length.
2631 */ 1930 */
2632local void gen_codes(tree, max_code) 1931static void gen_codes(ct_data *tree, int max_code)
2633ct_data near *tree; /* the tree to decorate */
2634int max_code; /* largest code with non zero frequency */
2635{ 1932{
2636 ush next_code[MAX_BITS + 1]; /* next code value for each bit length */ 1933 ush next_code[MAX_BITS + 1]; /* next code value for each bit length */
2637 ush code = 0; /* running code value */ 1934 ush code = 0; /* running code value */
@@ -2674,11 +1971,10 @@ int max_code; /* largest code with non zero frequency */
2674 * and corresponding code. The length opt_len is updated; static_len is 1971 * and corresponding code. The length opt_len is updated; static_len is
2675 * also updated if stree is not null. The field max_code is set. 1972 * also updated if stree is not null. The field max_code is set.
2676 */ 1973 */
2677local void build_tree(desc) 1974static void build_tree(tree_desc *desc)
2678tree_desc near *desc; /* the tree descriptor */
2679{ 1975{
2680 ct_data near *tree = desc->dyn_tree; 1976 ct_data *tree = desc->dyn_tree;
2681 ct_data near *stree = desc->static_tree; 1977 ct_data *stree = desc->static_tree;
2682 int elems = desc->elems; 1978 int elems = desc->elems;
2683 int n, m; /* iterate over heap elements */ 1979 int n, m; /* iterate over heap elements */
2684 int max_code = -1; /* largest code with non zero frequency */ 1980 int max_code = -1; /* largest code with non zero frequency */
@@ -2754,10 +2050,10 @@ tree_desc near *desc; /* the tree descriptor */
2754 /* At this point, the fields freq and dad are set. We can now 2050 /* At this point, the fields freq and dad are set. We can now
2755 * generate the bit lengths. 2051 * generate the bit lengths.
2756 */ 2052 */
2757 gen_bitlen((tree_desc near *) desc); 2053 gen_bitlen((tree_desc *) desc);
2758 2054
2759 /* The field len is now set, we can generate the bit codes */ 2055 /* The field len is now set, we can generate the bit codes */
2760 gen_codes((ct_data near *) tree, max_code); 2056 gen_codes((ct_data *) tree, max_code);
2761} 2057}
2762 2058
2763/* =========================================================================== 2059/* ===========================================================================
@@ -2766,9 +2062,7 @@ tree_desc near *desc; /* the tree descriptor */
2766 * counts. (The contribution of the bit length codes will be added later 2062 * counts. (The contribution of the bit length codes will be added later
2767 * during the construction of bl_tree.) 2063 * during the construction of bl_tree.)
2768 */ 2064 */
2769local void scan_tree(tree, max_code) 2065static void scan_tree(ct_data *tree, int max_code)
2770ct_data near *tree; /* the tree to be scanned */
2771int max_code; /* and its largest code of non zero frequency */
2772{ 2066{
2773 int n; /* iterates over all tree elements */ 2067 int n; /* iterates over all tree elements */
2774 int prevlen = -1; /* last emitted length */ 2068 int prevlen = -1; /* last emitted length */
@@ -2814,9 +2108,7 @@ int max_code; /* and its largest code of non zero frequency */
2814 * Send a literal or distance tree in compressed form, using the codes in 2108 * Send a literal or distance tree in compressed form, using the codes in
2815 * bl_tree. 2109 * bl_tree.
2816 */ 2110 */
2817local void send_tree(tree, max_code) 2111static void send_tree(ct_data *tree, int max_code)
2818ct_data near *tree; /* the tree to be scanned */
2819int max_code; /* and its largest code of non zero frequency */
2820{ 2112{
2821 int n; /* iterates over all tree elements */ 2113 int n; /* iterates over all tree elements */
2822 int prevlen = -1; /* last emitted length */ 2114 int prevlen = -1; /* last emitted length */
@@ -2873,16 +2165,16 @@ int max_code; /* and its largest code of non zero frequency */
2873 * Construct the Huffman tree for the bit lengths and return the index in 2165 * Construct the Huffman tree for the bit lengths and return the index in
2874 * bl_order of the last bit length code to send. 2166 * bl_order of the last bit length code to send.
2875 */ 2167 */
2876local int build_bl_tree() 2168static const int build_bl_tree()
2877{ 2169{
2878 int max_blindex; /* index of last bit length code of non zero freq */ 2170 int max_blindex; /* index of last bit length code of non zero freq */
2879 2171
2880 /* Determine the bit length frequencies for literal and distance trees */ 2172 /* Determine the bit length frequencies for literal and distance trees */
2881 scan_tree((ct_data near *) dyn_ltree, l_desc.max_code); 2173 scan_tree((ct_data *) dyn_ltree, l_desc.max_code);
2882 scan_tree((ct_data near *) dyn_dtree, d_desc.max_code); 2174 scan_tree((ct_data *) dyn_dtree, d_desc.max_code);
2883 2175
2884 /* Build the bit length tree: */ 2176 /* Build the bit length tree: */
2885 build_tree((tree_desc near *) (&bl_desc)); 2177 build_tree((tree_desc *) (&bl_desc));
2886 /* opt_len now includes the length of the tree representations, except 2178 /* opt_len now includes the length of the tree representations, except
2887 * the lengths of the bit lengths codes and the 5+5+4 bits for the counts. 2179 * the lengths of the bit lengths codes and the 5+5+4 bits for the counts.
2888 */ 2180 */
@@ -2909,8 +2201,7 @@ local int build_bl_tree()
2909 * lengths of the bit length codes, the literal tree and the distance tree. 2201 * lengths of the bit length codes, the literal tree and the distance tree.
2910 * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4. 2202 * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4.
2911 */ 2203 */
2912local void send_all_trees(lcodes, dcodes, blcodes) 2204static void send_all_trees(int lcodes, int dcodes, int blcodes)
2913int lcodes, dcodes, blcodes; /* number of codes for each tree */
2914{ 2205{
2915 int rank; /* index in bl_order */ 2206 int rank; /* index in bl_order */
2916 2207
@@ -2928,10 +2219,10 @@ int lcodes, dcodes, blcodes; /* number of codes for each tree */
2928 } 2219 }
2929 Tracev((stderr, "\nbl tree: sent %ld", bits_sent)); 2220 Tracev((stderr, "\nbl tree: sent %ld", bits_sent));
2930 2221
2931 send_tree((ct_data near *) dyn_ltree, lcodes - 1); /* send the literal tree */ 2222 send_tree((ct_data *) dyn_ltree, lcodes - 1); /* send the literal tree */
2932 Tracev((stderr, "\nlit tree: sent %ld", bits_sent)); 2223 Tracev((stderr, "\nlit tree: sent %ld", bits_sent));
2933 2224
2934 send_tree((ct_data near *) dyn_dtree, dcodes - 1); /* send the distance tree */ 2225 send_tree((ct_data *) dyn_dtree, dcodes - 1); /* send the distance tree */
2935 Tracev((stderr, "\ndist tree: sent %ld", bits_sent)); 2226 Tracev((stderr, "\ndist tree: sent %ld", bits_sent));
2936} 2227}
2937 2228
@@ -2940,10 +2231,7 @@ int lcodes, dcodes, blcodes; /* number of codes for each tree */
2940 * trees or store, and output the encoded block to the zip file. This function 2231 * trees or store, and output the encoded block to the zip file. This function
2941 * returns the total compressed length for the file so far. 2232 * returns the total compressed length for the file so far.
2942 */ 2233 */
2943ulg flush_block(buf, stored_len, eof) 2234static ulg flush_block(char *buf, ulg stored_len, int eof)
2944char *buf; /* input block, or NULL if too old */
2945ulg stored_len; /* length of input block */
2946int eof; /* true if this is the last block for a file */
2947{ 2235{
2948 ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */ 2236 ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */
2949 int max_blindex; /* index of last bit length code of non zero freq */ 2237 int max_blindex; /* index of last bit length code of non zero freq */
@@ -2955,10 +2243,10 @@ int eof; /* true if this is the last block for a file */
2955 set_file_type(); 2243 set_file_type();
2956 2244
2957 /* Construct the literal and distance trees */ 2245 /* Construct the literal and distance trees */
2958 build_tree((tree_desc near *) (&l_desc)); 2246 build_tree((tree_desc *) (&l_desc));
2959 Tracev((stderr, "\nlit data: dyn %ld, stat %ld", opt_len, static_len)); 2247 Tracev((stderr, "\nlit data: dyn %ld, stat %ld", opt_len, static_len));
2960 2248
2961 build_tree((tree_desc near *) (&d_desc)); 2249 build_tree((tree_desc *) (&d_desc));
2962 Tracev( 2250 Tracev(
2963 (stderr, "\ndist data: dyn %ld, stat %ld", opt_len, 2251 (stderr, "\ndist data: dyn %ld, stat %ld", opt_len,
2964 static_len)); 2252 static_len));
@@ -2974,7 +2262,6 @@ int eof; /* true if this is the last block for a file */
2974 /* Determine the best encoding. Compute first the block length in bytes */ 2262 /* Determine the best encoding. Compute first the block length in bytes */
2975 opt_lenb = (opt_len + 3 + 7) >> 3; 2263 opt_lenb = (opt_len + 3 + 7) >> 3;
2976 static_lenb = (static_len + 3 + 7) >> 3; 2264 static_lenb = (static_len + 3 + 7) >> 3;
2977 input_len += stored_len; /* for debugging only */
2978 2265
2979 Trace( 2266 Trace(
2980 (stderr, 2267 (stderr,
@@ -2989,11 +2276,8 @@ int eof; /* true if this is the last block for a file */
2989 * and if the zip file can be seeked (to rewrite the local header), 2276 * and if the zip file can be seeked (to rewrite the local header),
2990 * the whole file is transformed into a stored file: 2277 * the whole file is transformed into a stored file:
2991 */ 2278 */
2992#ifdef FORCE_METHOD
2993#else
2994 if (stored_len <= opt_lenb && eof && compressed_len == 0L 2279 if (stored_len <= opt_lenb && eof && compressed_len == 0L
2995 && seekable()) { 2280 && seekable()) {
2996#endif
2997 /* Since LIT_BUFSIZE <= 2*WSIZE, the input data must be there: */ 2281 /* Since LIT_BUFSIZE <= 2*WSIZE, the input data must be there: */
2998 if (buf == (char *) 0) 2282 if (buf == (char *) 0)
2999 error_msg("block vanished"); 2283 error_msg("block vanished");
@@ -3002,11 +2286,8 @@ int eof; /* true if this is the last block for a file */
3002 compressed_len = stored_len << 3; 2286 compressed_len = stored_len << 3;
3003 *file_method = STORED; 2287 *file_method = STORED;
3004 2288
3005#ifdef FORCE_METHOD
3006#else
3007 } else if (stored_len + 4 <= opt_lenb && buf != (char *) 0) { 2289 } else if (stored_len + 4 <= opt_lenb && buf != (char *) 0) {
3008 /* 4: two words for the lengths */ 2290 /* 4: two words for the lengths */
3009#endif
3010 /* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE. 2291 /* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE.
3011 * Otherwise we can't have processed more than WSIZE input bytes since 2292 * Otherwise we can't have processed more than WSIZE input bytes since
3012 * the last block flush, because compression would have been 2293 * the last block flush, because compression would have been
@@ -3019,27 +2300,23 @@ int eof; /* true if this is the last block for a file */
3019 2300
3020 copy_block(buf, (unsigned) stored_len, 1); /* with header */ 2301 copy_block(buf, (unsigned) stored_len, 1); /* with header */
3021 2302
3022#ifdef FORCE_METHOD
3023#else
3024 } else if (static_lenb == opt_lenb) { 2303 } else if (static_lenb == opt_lenb) {
3025#endif
3026 send_bits((STATIC_TREES << 1) + eof, 3); 2304 send_bits((STATIC_TREES << 1) + eof, 3);
3027 compress_block((ct_data near *) static_ltree, 2305 compress_block((ct_data *) static_ltree,
3028 (ct_data near *) static_dtree); 2306 (ct_data *) static_dtree);
3029 compressed_len += 3 + static_len; 2307 compressed_len += 3 + static_len;
3030 } else { 2308 } else {
3031 send_bits((DYN_TREES << 1) + eof, 3); 2309 send_bits((DYN_TREES << 1) + eof, 3);
3032 send_all_trees(l_desc.max_code + 1, d_desc.max_code + 1, 2310 send_all_trees(l_desc.max_code + 1, d_desc.max_code + 1,
3033 max_blindex + 1); 2311 max_blindex + 1);
3034 compress_block((ct_data near *) dyn_ltree, 2312 compress_block((ct_data *) dyn_ltree,
3035 (ct_data near *) dyn_dtree); 2313 (ct_data *) dyn_dtree);
3036 compressed_len += 3 + opt_len; 2314 compressed_len += 3 + opt_len;
3037 } 2315 }
3038 Assert(compressed_len == bits_sent, "bad compressed size"); 2316 Assert(compressed_len == bits_sent, "bad compressed size");
3039 init_block(); 2317 init_block();
3040 2318
3041 if (eof) { 2319 if (eof) {
3042 Assert(input_len == isize, "bad input size");
3043 bi_windup(); 2320 bi_windup();
3044 compressed_len += 7; /* align on byte boundary */ 2321 compressed_len += 7; /* align on byte boundary */
3045 } 2322 }
@@ -3053,9 +2330,7 @@ int eof; /* true if this is the last block for a file */
3053 * Save the match info and tally the frequency counts. Return true if 2330 * Save the match info and tally the frequency counts. Return true if
3054 * the current block must be flushed. 2331 * the current block must be flushed.
3055 */ 2332 */
3056int ct_tally(dist, lc) 2333static int ct_tally(int dist, int lc)
3057int dist; /* distance of matched string */
3058int lc; /* match length-MIN_MATCH or unmatched char (if dist==0) */
3059{ 2334{
3060 l_buf[last_lit++] = (uch) lc; 2335 l_buf[last_lit++] = (uch) lc;
3061 if (dist == 0) { 2336 if (dist == 0) {
@@ -3111,9 +2386,7 @@ int lc; /* match length-MIN_MATCH or unmatched char (if dist==0) */
3111/* =========================================================================== 2386/* ===========================================================================
3112 * Send the block data compressed using the given Huffman trees 2387 * Send the block data compressed using the given Huffman trees
3113 */ 2388 */
3114local void compress_block(ltree, dtree) 2389static void compress_block(ct_data *ltree, ct_data *dtree)
3115ct_data near *ltree; /* literal tree */
3116ct_data near *dtree; /* distance tree */
3117{ 2390{
3118 unsigned dist; /* distance of matched string */ 2391 unsigned dist; /* distance of matched string */
3119 int lc; /* match length or unmatched char (if dist == 0) */ 2392 int lc; /* match length or unmatched char (if dist == 0) */
@@ -3165,7 +2438,7 @@ ct_data near *dtree; /* distance tree */
3165 * IN assertion: the fields freq of dyn_ltree are set and the total of all 2438 * IN assertion: the fields freq of dyn_ltree are set and the total of all
3166 * frequencies does not exceed 64K (to fit in an int on 16 bit machines). 2439 * frequencies does not exceed 64K (to fit in an int on 16 bit machines).
3167 */ 2440 */
3168local void set_file_type() 2441static void set_file_type()
3169{ 2442{
3170 int n = 0; 2443 int n = 0;
3171 unsigned ascii_freq = 0; 2444 unsigned ascii_freq = 0;
@@ -3183,228 +2456,22 @@ local void set_file_type()
3183 } 2456 }
3184} 2457}
3185 2458
3186/* util.c -- utility functions for gzip support
3187 * Copyright (C) 1992-1993 Jean-loup Gailly
3188 * This is free software; you can redistribute it and/or modify it under the
3189 * terms of the GNU General Public License, see the file COPYING.
3190 */
3191
3192#include <ctype.h>
3193#include <errno.h>
3194#include <sys/types.h>
3195
3196#ifdef HAVE_UNISTD_H
3197# include <unistd.h>
3198#endif
3199#ifndef NO_FCNTL_H
3200# include <fcntl.h>
3201#endif
3202
3203/* ===========================================================================
3204 * Copy input to output unchanged: zcat == cat with --force.
3205 * IN assertion: insize bytes have already been read in inbuf.
3206 */
3207int copy(in, out)
3208int in, out; /* input and output file descriptors */
3209{
3210 errno = 0;
3211 while (insize != 0 && (int) insize != EOF) {
3212 write_buf(out, (char *) inbuf, insize);
3213 bytes_out += insize;
3214 insize = read(in, (char *) inbuf, INBUFSIZ);
3215 }
3216 if ((int) insize == EOF && errno != 0) {
3217 read_error_msg();
3218 }
3219 bytes_in = bytes_out;
3220 return OK;
3221}
3222
3223/* ========================================================================
3224 * Put string s in lower case, return s.
3225 */
3226char *strlwr(s)
3227char *s;
3228{
3229 char *t;
3230
3231 for (t = s; *t; t++)
3232 *t = tolow(*t);
3233 return s;
3234}
3235
3236#if defined(NO_STRING_H) && !defined(STDC_HEADERS)
3237
3238/* Provide missing strspn and strcspn functions. */
3239
3240int strspn (const char *s, const char *accept);
3241int strcspn (const char *s, const char *reject);
3242
3243/* ========================================================================
3244 * Return the length of the maximum initial segment
3245 * of s which contains only characters in accept.
3246 */
3247int strspn(s, accept)
3248const char *s;
3249const char *accept;
3250{
3251 register const char *p;
3252 register const char *a;
3253 register int count = 0;
3254
3255 for (p = s; *p != '\0'; ++p) {
3256 for (a = accept; *a != '\0'; ++a) {
3257 if (*p == *a)
3258 break;
3259 }
3260 if (*a == '\0')
3261 return count;
3262 ++count;
3263 }
3264 return count;
3265}
3266
3267/* ========================================================================
3268 * Return the length of the maximum inital segment of s
3269 * which contains no characters from reject.
3270 */
3271int strcspn(s, reject)
3272const char *s;
3273const char *reject;
3274{
3275 register int count = 0;
3276
3277 while (*s != '\0') {
3278 if (strchr(reject, *s++) != NULL)
3279 return count;
3280 ++count;
3281 }
3282 return count;
3283}
3284
3285#endif /* NO_STRING_H */
3286
3287/* ========================================================================
3288 * Add an environment variable (if any) before argv, and update argc.
3289 * Return the expanded environment variable to be freed later, or NULL
3290 * if no options were added to argv.
3291 */
3292#define SEPARATOR " \t" /* separators in env variable */
3293
3294char *add_envopt(argcp, argvp, env)
3295int *argcp; /* pointer to argc */
3296char ***argvp; /* pointer to argv */
3297char *env; /* name of environment variable */
3298{
3299 char *p; /* running pointer through env variable */
3300 char **oargv; /* runs through old argv array */
3301 char **nargv; /* runs through new argv array */
3302 int oargc = *argcp; /* old argc */
3303 int nargc = 0; /* number of arguments in env variable */
3304
3305 env = (char *) getenv(env);
3306 if (env == NULL)
3307 return NULL;
3308
3309 p = (char *) xmalloc(strlen(env) + 1);
3310 env = strcpy(p, env); /* keep env variable intact */
3311
3312 for (p = env; *p; nargc++) { /* move through env */
3313 p += strspn(p, SEPARATOR); /* skip leading separators */
3314 if (*p == '\0')
3315 break;
3316
3317 p += strcspn(p, SEPARATOR); /* find end of word */
3318 if (*p)
3319 *p++ = '\0'; /* mark it */
3320 }
3321 if (nargc == 0) {
3322 free(env);
3323 return NULL;
3324 }
3325 *argcp += nargc;
3326 /* Allocate the new argv array, with an extra element just in case
3327 * the original arg list did not end with a NULL.
3328 */
3329 nargv = (char **) calloc(*argcp + 1, sizeof(char *));
3330
3331 if (nargv == NULL)
3332 error_msg(memory_exhausted);
3333 oargv = *argvp;
3334 *argvp = nargv;
3335
3336 /* Copy the program name first */
3337 if (oargc-- < 0)
3338 error_msg("argc<=0");
3339 *(nargv++) = *(oargv++);
3340
3341 /* Then copy the environment args */
3342 for (p = env; nargc > 0; nargc--) {
3343 p += strspn(p, SEPARATOR); /* skip separators */
3344 *(nargv++) = p; /* store start */
3345 while (*p++); /* skip over word */
3346 }
3347
3348 /* Finally copy the old args and add a NULL (usual convention) */
3349 while (oargc--)
3350 *(nargv++) = *(oargv++);
3351 *nargv = NULL;
3352 return env;
3353}
3354
3355/* ========================================================================
3356 * Display compression ratio on the given stream on 6 characters.
3357 */
3358void display_ratio(num, den, file)
3359long num;
3360long den;
3361FILE *file;
3362{
3363 long ratio; /* 1000 times the compression ratio */
3364
3365 if (den == 0) {
3366 ratio = 0; /* no compression */
3367 } else if (den < 2147483L) { /* (2**31 -1)/1000 */
3368 ratio = 1000L * num / den;
3369 } else {
3370 ratio = num / (den / 1000L);
3371 }
3372 if (ratio < 0) {
3373 putc('-', file);
3374 ratio = -ratio;
3375 } else {
3376 putc(' ', file);
3377 }
3378 fprintf(file, "%2ld.%1ld%%", ratio / 10L, ratio % 10L);
3379}
3380
3381
3382/* zip.c -- compress files to the gzip or pkzip format 2459/* zip.c -- compress files to the gzip or pkzip format
3383 * Copyright (C) 1992-1993 Jean-loup Gailly 2460 * Copyright (C) 1992-1993 Jean-loup Gailly
3384 * This is free software; you can redistribute it and/or modify it under the 2461 * This is free software; you can redistribute it and/or modify it under the
3385 * terms of the GNU General Public License, see the file COPYING. 2462 * terms of the GNU General Public License, see the file COPYING.
3386 */ 2463 */
3387 2464
3388#include <ctype.h>
3389#include <sys/types.h>
3390
3391#ifdef HAVE_UNISTD_H
3392# include <unistd.h>
3393#endif
3394#ifndef NO_FCNTL_H
3395# include <fcntl.h>
3396#endif
3397 2465
3398local ulg crc; /* crc on uncompressed file data */ 2466static ulg crc; /* crc on uncompressed file data */
3399long header_bytes; /* number of bytes in gzip header */ 2467static long header_bytes; /* number of bytes in gzip header */
3400 2468
3401/* =========================================================================== 2469/* ===========================================================================
3402 * Deflate in to out. 2470 * Deflate in to out.
3403 * IN assertions: the input and output buffers are cleared. 2471 * IN assertions: the input and output buffers are cleared.
3404 * The variables time_stamp and save_orig_name are initialized. 2472 * The variables time_stamp and save_orig_name are initialized.
3405 */ 2473 */
3406int zip(in, out) 2474static int zip(int in, int out)
3407int in, out; /* input and output file descriptors */
3408{ 2475{
3409 uch my_flags = 0; /* general purpose bit flags */ 2476 uch my_flags = 0; /* general purpose bit flags */
3410 ush attr = 0; /* ascii/binary flag */ 2477 ush attr = 0; /* ascii/binary flag */
@@ -3454,9 +2521,7 @@ int in, out; /* input and output file descriptors */
3454 * translation, and update the crc and input file size. 2521 * translation, and update the crc and input file size.
3455 * IN assertion: size >= 2 (for end-of-line translation) 2522 * IN assertion: size >= 2 (for end-of-line translation)
3456 */ 2523 */
3457int file_read(buf, size) 2524static int file_read(char *buf, unsigned size)
3458char *buf;
3459unsigned size;
3460{ 2525{
3461 unsigned len; 2526 unsigned len;
3462 2527
@@ -3475,12 +2540,11 @@ unsigned size;
3475 * Write the output buffer outbuf[0..outcnt-1] and update bytes_out. 2540 * Write the output buffer outbuf[0..outcnt-1] and update bytes_out.
3476 * (used for the compressed data only) 2541 * (used for the compressed data only)
3477 */ 2542 */
3478void flush_outbuf() 2543static void flush_outbuf()
3479{ 2544{
3480 if (outcnt == 0) 2545 if (outcnt == 0)
3481 return; 2546 return;
3482 2547
3483 write_buf(ofd, (char *) outbuf, outcnt); 2548 write_buf(ofd, (char *) outbuf, outcnt);
3484 bytes_out += (ulg) outcnt;
3485 outcnt = 0; 2549 outcnt = 0;
3486} 2550}