1
/* gzip.h -- common declarations for all gzip modules
2
* Copyright (C) 1992-1993 Jean-loup Gailly.
3
* This is free software; you can redistribute it and/or modify it under the
4
* terms of the GNU General Public License, see the file COPYING.
7
#if defined(__STDC__) || defined(PROTO)
19
/* I don't like nested includes, but the string and io functions are used
24
#define memzero(s, n) memset ((voidp)(s), 0, (n))
28
typedef unsigned char uch;
29
typedef unsigned short ush;
30
typedef unsigned long ulg;
32
/* Return codes from gzip */
37
/* Compression methods (see algorithm.doc) */
42
/* methods 4 to 7 reserved */
45
extern int method; /* compression method */
47
/* To save memory for 16 bit systems, some arrays are overlaid between
48
* the various modules:
49
* deflate: prev+head window d_buf l_buf outbuf
50
* unlzw: tab_prefix tab_suffix stack inbuf outbuf
51
* inflate: window inbuf
52
* unpack: window inbuf prefix_len
53
* unlzh: left+right window c_table inbuf c_len
54
* For compression, input is done in window[]. For decompression, output
55
* is done in window except for unlzw.
60
# define INBUFSIZ 0x2000 /* input buffer size */
62
# define INBUFSIZ 0x8000 /* input buffer size */
65
#define INBUF_EXTRA 64 /* required by unlzw() */
69
# define OUTBUFSIZ 8192 /* output buffer size */
71
# define OUTBUFSIZ 16384 /* output buffer size */
74
#define OUTBUF_EXTRA 2048 /* required by unlzw() */
78
# define DIST_BUFSIZE 0x2000 /* buffer for distances, see trees.c */
80
# define DIST_BUFSIZE 0x8000 /* buffer for distances, see trees.c */
85
# define EXTERN(type, array) extern type * near array
86
# define DECLARE(type, array, size) type * near array
87
# define ALLOC(type, array, size) { \
88
array = (type*)fcalloc((size_t)(((size)+1L)/2), 2*sizeof(type)); \
89
if (array == NULL) error("insufficient memory"); \
91
# define FREE(array) {if (array != NULL) fcfree(array), array=NULL;}
93
# define EXTERN(type, array) extern type array[]
94
# define DECLARE(type, array, size) type array[size]
95
# define ALLOC(type, array, size)
99
EXTERN(uch, inbuf); /* input buffer */
100
EXTERN(uch, outbuf); /* output buffer */
101
EXTERN(ush, d_buf); /* buffer for distances, see trees.c */
102
EXTERN(uch, window); /* Sliding window and suffix table (unlzw) */
103
#define tab_suffix window
105
# define tab_prefix prev /* hash link (see deflate.c) */
106
# define head (prev+WSIZE) /* hash head (see deflate.c) */
107
EXTERN(ush, tab_prefix); /* prefix code (see unlzw.c) */
109
# define tab_prefix0 prev
110
# define head tab_prefix1
111
EXTERN(ush, tab_prefix0); /* prefix for even codes */
112
EXTERN(ush, tab_prefix1); /* prefix for odd codes */
115
extern unsigned insize; /* valid bytes in inbuf */
116
extern unsigned inptr; /* index of next byte to be processed in inbuf */
117
extern unsigned outcnt; /* bytes in output buffer */
119
extern long bytes_in; /* number of input bytes */
120
extern long bytes_out; /* number of output bytes */
121
extern long header_bytes;/* number of bytes in gzip header */
123
#define isize bytes_in
124
/* for compatibility with old zip sources (to be cleaned) */
126
extern int ifd; /* input file descriptor */
127
extern int ofd; /* output file descriptor */
128
extern char ifname[]; /* input file name or "stdin" */
129
extern char ofname[]; /* output file name or "stdout" */
130
extern char *progname; /* program name */
132
extern time_t time_stamp; /* original time stamp (modification time) */
133
extern long ifile_size; /* input file size, -1 for devices (debug only) */
135
typedef int file_t; /* Do not use stdio */
136
#define NO_FILE (-1) /* in memory compression */
139
#define PACK_MAGIC "\037\036" /* Magic header for packed files */
140
#define GZIP_MAGIC "\037\213" /* Magic header for gzip files, 1F 8B */
141
#define OLD_GZIP_MAGIC "\037\236" /* Magic header for gzip 0.5 = freeze 1.x */
142
#define LZH_MAGIC "\037\240" /* Magic header for SCO LZH Compress files*/
143
#define PKZIP_MAGIC "\120\113\003\004" /* Magic header for pkzip files */
146
#define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
147
#define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
148
#define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
149
#define ORIG_NAME 0x08 /* bit 3 set: original file name present */
150
#define COMMENT 0x10 /* bit 4 set: file comment present */
151
#define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
152
#define RESERVED 0xC0 /* bit 6,7: reserved */
154
/* internal file attribute */
155
#define UNKNOWN 0xffff
160
# define WSIZE 0x8000 /* window size--must be a power of two, and */
161
#endif /* at least 32K for zip's deflate method */
164
#define MAX_MATCH 258
165
/* The minimum and maximum match lengths */
167
#define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
168
/* Minimum amount of lookahead, except at the end of the input file.
169
* See deflate.c for comments about the MIN_MATCH+1.
172
#define MAX_DIST (WSIZE-MIN_LOOKAHEAD)
173
/* In order to simplify the code, particularly on 16 bit machines, match
174
* distances are limited to MAX_DIST instead of WSIZE.
177
extern int decrypt; /* flag to turn on decryption */
178
extern int exit_code; /* program exit code */
179
extern int verbose; /* be verbose (-v) */
180
extern int quiet; /* be quiet (-q) */
181
extern int level; /* compression level */
182
extern int test; /* check .z file integrity */
183
extern int to_stdout; /* output to stdout (-c) */
184
extern int save_orig_name; /* set if original name must be saved */
186
#define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf(0))
187
#define try_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf(1))
189
/* put_byte is used for the compressed output, put_ubyte for the
190
* uncompressed output. However unlzw() uses window for its
191
* suffix table instead of its output buffer, so it does not use put_ubyte
192
* (to be cleaned up).
194
#define put_byte(c) {outbuf[outcnt++]=(uch)(c); if (outcnt==OUTBUFSIZ)\
196
#define put_ubyte(c) {window[outcnt++]=(uch)(c); if (outcnt==WSIZE)\
199
/* Output a 16 bit value, lsb first */
200
#define put_short(w) \
201
{ if (outcnt < OUTBUFSIZ-2) { \
202
outbuf[outcnt++] = (uch) ((w) & 0xff); \
203
outbuf[outcnt++] = (uch) ((ush)(w) >> 8); \
205
put_byte((uch)((w) & 0xff)); \
206
put_byte((uch)((ush)(w) >> 8)); \
210
/* Output a 32 bit value to the bit stream, lsb first */
211
#define put_long(n) { \
212
put_short((n) & 0xffff); \
213
put_short(((ulg)(n)) >> 16); \
216
#define seekable() 0 /* force sequential output */
217
#define translate_eol 0 /* no option -a yet */
219
#define tolow(c) (isupper(c) ? (c)-'A'+'a' : (c)) /* force to lower case */
221
/* Macros for getting two-byte and four-byte header values */
222
#define SH(p) ((ush)(uch)((p)[0]) | ((ush)(uch)((p)[1]) << 8))
223
#define LG(p) ((ulg)(SH(p)) | ((ulg)(SH((p)+2)) << 16))
225
/* Diagnostic functions */
227
# define Assert(cond,msg) {if(!(cond)) error(msg);}
228
# define Trace(x) fprintf x
229
# define Tracev(x) {if (verbose) fprintf x ;}
230
# define Tracevv(x) {if (verbose>1) fprintf x ;}
231
# define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
232
# define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
234
# define Assert(cond,msg)
239
# define Tracecv(c,x)
242
#define WARN(msg) {if (!quiet) fprintf msg ; \
243
if (exit_code == OK) exit_code = WARNING;}
246
extern int zip OF((int in, int out));
247
extern int file_read OF((char *buf, unsigned size));
250
extern int unzip OF((int in, int out));
251
extern int check_zipfile OF((int in));
254
void abort_gzip OF((void));
257
void lm_init OF((int pack_level, ush *flags));
258
ulg deflate OF((void));
261
void ct_init OF((ush *attr, int *method));
262
int ct_tally OF((int dist, int lc));
263
ulg flush_block OF((char *buf, ulg stored_len, int eof));
266
void bi_init OF((file_t zipfile));
267
void send_bits OF((int value, int length));
268
unsigned bi_reverse OF((unsigned value, int length));
269
void bi_windup OF((void));
270
void copy_block OF((char *buf, unsigned len, int header));
271
extern int (*read_buf) OF((char *buf, unsigned size));
274
extern int copy OF((int in, int out));
275
extern ulg updcrc OF((uch *s, unsigned n));
276
extern void clear_bufs OF((void));
277
extern int fill_inbuf OF((int eof_ok));
278
extern void flush_outbuf OF((void));
279
extern void flush_window OF((void));
280
extern void write_buf OF((int fd, voidp buf, unsigned cnt));
281
extern char *strlwr OF((char *s));
282
extern char *basename OF((char *fname));
283
extern void make_simple_name OF((char *name));
284
extern char *add_envopt OF((int *argcp, char ***argvp, char *env));
285
extern void error OF((char *m));
286
extern void warn OF((char *a, char *b));
287
extern void read_error OF((void));
288
extern void write_error OF((void));
289
extern void display_ratio OF((long num, long den, FILE *file));
290
extern voidp xmalloc OF((unsigned int size));
293
extern int inflate OF((void));
295
/* stuff from lzw.h */