1 /* gzip.h -- common declarations for all gzip modules
3 Copyright (C) 1997, 1998, 1999, 2001, 2006 Free Software Foundation, Inc.
4 Copyright (C) 1992-1993 Jean-loup Gailly.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software Foundation,
18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
20 #if defined(__STDC__) || defined(PROTO)
21 # define OF(args) args
33 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8) || __STRICT_ANSI__
34 # define __attribute__(x)
38 #ifndef ATTRIBUTE_NORETURN
39 # define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
42 /* I don't like nested includes, but the following headers are used
46 #include <sys/types.h> /* for off_t, time_t */
47 #if defined HAVE_STRING_H || defined STDC_HEADERS
49 # if !defined STDC_HEADERS && defined HAVE_MEMORY_H && !defined __GNUC__
52 # define memzero(s, n) memset ((voidp)(s), 0, (n))
56 # define strrchr rindex
57 # define memcpy(d, s, n) bcopy((s), (d), (n))
58 # define memcmp(s1, s2, n) bcmp((s1), (s2), (n))
59 # define memzero(s, n) bzero((s), (n))
67 # define RETSIGTYPE void
72 typedef unsigned char uch;
73 typedef unsigned short ush;
74 typedef unsigned long ulg;
76 /* Return codes from gzip */
81 /* Compression methods (see algorithm.doc) */
86 /* methods 4 to 7 reserved */
89 extern int method; /* compression method */
91 /* To save memory for 16 bit systems, some arrays are overlaid between
92 * the various modules:
93 * deflate: prev+head window d_buf l_buf outbuf
94 * unlzw: tab_prefix tab_suffix stack inbuf outbuf
95 * inflate: window inbuf
96 * unpack: window inbuf prefix_len
97 * unlzh: left+right window c_table inbuf c_len
98 * For compression, input is done in window[]. For decompression, output
99 * is done in window except for unlzw.
104 # define INBUFSIZ 0x2000 /* input buffer size */
106 # define INBUFSIZ 0x8000 /* input buffer size */
108 # if defined SSIZE_MAX && SSIZE_MAX < INBUFSIZ
110 # define INBUFSIZ SSIZE_MAX
113 #define INBUF_EXTRA 64 /* required by unlzw() */
117 # define OUTBUFSIZ 8192 /* output buffer size */
119 # define OUTBUFSIZ 16384 /* output buffer size */
122 #define OUTBUF_EXTRA 2048 /* required by unlzw() */
126 # define DIST_BUFSIZE 0x2000 /* buffer for distances, see trees.c */
128 # define DIST_BUFSIZE 0x8000 /* buffer for distances, see trees.c */
133 # define EXTERN(type, array) extern type * near array
134 # define DECLARE(type, array, size) type * near array
135 # define ALLOC(type, array, size) { \
136 array = (type*)fcalloc((size_t)(((size)+1L)/2), 2*sizeof(type)); \
137 if (!array) xalloc_die (); \
139 # define FREE(array) {if (array != NULL) fcfree(array), array=NULL;}
141 # define EXTERN(type, array) extern type array[]
142 # define DECLARE(type, array, size) type array[size]
143 # define ALLOC(type, array, size)
147 EXTERN(uch, inbuf); /* input buffer */
148 EXTERN(uch, outbuf); /* output buffer */
149 EXTERN(ush, d_buf); /* buffer for distances, see trees.c */
150 EXTERN(uch, window); /* Sliding window and suffix table (unlzw) */
151 #define tab_suffix window
153 # define tab_prefix prev /* hash link (see deflate.c) */
154 # define head (prev+WSIZE) /* hash head (see deflate.c) */
155 EXTERN(ush, tab_prefix); /* prefix code (see unlzw.c) */
157 # define tab_prefix0 prev
158 # define head tab_prefix1
159 EXTERN(ush, tab_prefix0); /* prefix for even codes */
160 EXTERN(ush, tab_prefix1); /* prefix for odd codes */
163 extern unsigned insize; /* valid bytes in inbuf */
164 extern unsigned inptr; /* index of next byte to be processed in inbuf */
165 extern unsigned outcnt; /* bytes in output buffer */
167 extern off_t bytes_in; /* number of input bytes */
168 extern off_t bytes_out; /* number of output bytes */
169 extern off_t header_bytes;/* number of bytes in gzip header */
171 extern int ifd; /* input file descriptor */
172 extern int ofd; /* output file descriptor */
173 extern char ifname[]; /* input file name or "stdin" */
174 extern char ofname[]; /* output file name or "stdout" */
175 extern char *program_name; /* program name */
177 extern struct timespec time_stamp; /* original time stamp (modification time) */
178 extern off_t ifile_size; /* input file size, -1 for devices (debug only) */
180 typedef int file_t; /* Do not use stdio */
181 #define NO_FILE (-1) /* in memory compression */
184 #define PACK_MAGIC "\037\036" /* Magic header for packed files */
185 #define GZIP_MAGIC "\037\213" /* Magic header for gzip files, 1F 8B */
186 #define OLD_GZIP_MAGIC "\037\236" /* Magic header for gzip 0.5 = freeze 1.x */
187 #define LZH_MAGIC "\037\240" /* Magic header for SCO LZH Compress files*/
188 #define PKZIP_MAGIC "\120\113\003\004" /* Magic header for pkzip files */
191 #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
192 #define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
193 #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
194 #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
195 #define COMMENT 0x10 /* bit 4 set: file comment present */
196 #define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
197 #define RESERVED 0xC0 /* bit 6,7: reserved */
199 /* internal file attribute */
200 #define UNKNOWN 0xffff
205 # define WSIZE 0x8000 /* window size--must be a power of two, and */
206 #endif /* at least 32K for zip's deflate method */
209 #define MAX_MATCH 258
210 /* The minimum and maximum match lengths */
212 #define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
213 /* Minimum amount of lookahead, except at the end of the input file.
214 * See deflate.c for comments about the MIN_MATCH+1.
217 #define MAX_DIST (WSIZE-MIN_LOOKAHEAD)
218 /* In order to simplify the code, particularly on 16 bit machines, match
219 * distances are limited to MAX_DIST instead of WSIZE.
222 extern int decrypt; /* flag to turn on decryption */
223 extern int exit_code; /* program exit code */
224 extern int verbose; /* be verbose (-v) */
225 extern int quiet; /* be quiet (-q) */
226 extern int level; /* compression level */
227 extern int test; /* check .z file integrity */
228 extern int to_stdout; /* output to stdout (-c) */
229 extern int save_orig_name; /* set if original name must be saved */
231 #define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf(0))
232 #define try_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf(1))
234 /* put_byte is used for the compressed output, put_ubyte for the
235 * uncompressed output. However unlzw() uses window for its
236 * suffix table instead of its output buffer, so it does not use put_ubyte
237 * (to be cleaned up).
239 #define put_byte(c) {outbuf[outcnt++]=(uch)(c); if (outcnt==OUTBUFSIZ)\
241 #define put_ubyte(c) {window[outcnt++]=(uch)(c); if (outcnt==WSIZE)\
244 /* Output a 16 bit value, lsb first */
245 #define put_short(w) \
246 { if (outcnt < OUTBUFSIZ-2) { \
247 outbuf[outcnt++] = (uch) ((w) & 0xff); \
248 outbuf[outcnt++] = (uch) ((ush)(w) >> 8); \
250 put_byte((uch)((w) & 0xff)); \
251 put_byte((uch)((ush)(w) >> 8)); \
255 /* Output a 32 bit value to the bit stream, lsb first */
256 #define put_long(n) { \
257 put_short((n) & 0xffff); \
258 put_short(((ulg)(n)) >> 16); \
261 #define seekable() 0 /* force sequential output */
262 #define translate_eol 0 /* no option -a yet */
264 #define tolow(c) (isupper (c) ? tolower (c) : (c)) /* force to lower case */
266 /* Macros for getting two-byte and four-byte header values */
267 #define SH(p) ((ush)(uch)((p)[0]) | ((ush)(uch)((p)[1]) << 8))
268 #define LG(p) ((ulg)(SH(p)) | ((ulg)(SH((p)+2)) << 16))
270 /* Diagnostic functions */
272 # define Assert(cond,msg) {if (!(cond)) gzip_error (msg);}
273 # define Trace(x) fprintf x
274 # define Tracev(x) {if (verbose) fprintf x ;}
275 # define Tracevv(x) {if (verbose>1) fprintf x ;}
276 # define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
277 # define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
279 # define Assert(cond,msg)
284 # define Tracecv(c,x)
287 #define WARN(msg) {if (!quiet) fprintf msg ; \
288 if (exit_code == OK) exit_code = WARNING;}
291 extern int zip OF((int in, int out));
292 extern int file_read OF((char *buf, unsigned size));
295 extern int unzip OF((int in, int out));
296 extern int check_zipfile OF((int in));
299 extern int unpack OF((int in, int out));
302 extern int unlzh OF((int in, int out));
305 void abort_gzip OF((void)) ATTRIBUTE_NORETURN;
308 void lm_init OF((int pack_level, ush *flags));
309 off_t deflate OF((void));
312 void ct_init OF((ush *attr, int *method));
313 int ct_tally OF((int dist, int lc));
314 off_t flush_block OF((char *buf, ulg stored_len, int eof));
317 void bi_init OF((file_t zipfile));
318 void send_bits OF((int value, int length));
319 unsigned bi_reverse OF((unsigned value, int length));
320 void bi_windup OF((void));
321 void copy_block OF((char *buf, unsigned len, int header));
322 extern int (*read_buf) OF((char *buf, unsigned size));
325 extern int copy OF((int in, int out));
326 extern ulg updcrc OF((uch *s, unsigned n));
327 extern void clear_bufs OF((void));
328 extern int fill_inbuf OF((int eof_ok));
329 extern void flush_outbuf OF((void));
330 extern void flush_window OF((void));
331 extern void write_buf OF((int fd, voidp buf, unsigned cnt));
332 extern char *strlwr OF((char *s));
333 extern char *gzip_base_name OF((char *fname));
334 extern int xunlink OF((char *fname));
335 extern void make_simple_name OF((char *name));
336 extern char *add_envopt OF((int *argcp, char ***argvp, char *env));
337 extern void gzip_error OF((char *m));
338 extern void xalloc_die OF((void)) ATTRIBUTE_NORETURN;
339 extern void warning OF((char *m));
340 extern void read_error OF((void));
341 extern void write_error OF((void));
342 extern void display_ratio OF((off_t num, off_t den, FILE *file));
343 extern void fprint_off OF((FILE *, off_t, int));
346 extern int inflate OF((void));
349 extern int yesno OF((void));