]> git.cworth.org Git - gzip/blob - gzip.h
Imported Upstream version 1.3.2
[gzip] / gzip.h
1 /* gzip.h -- common declarations for all gzip modules
2  * Copyright (C) 1997, 1998, 1999, 2001 Free Software Foundation, Inc.
3  * Copyright (C) 1992-1993 Jean-loup Gailly.
4  * This is free software; you can redistribute it and/or modify it under the
5  * terms of the GNU General Public License, see the file COPYING.
6  */
7
8 #if defined(__STDC__) || defined(PROTO)
9 #  define OF(args)  args
10 #else
11 #  define OF(args)  ()
12 #endif
13
14 #ifdef __STDC__
15    typedef void *voidp;
16 #else
17    typedef char *voidp;
18 #endif
19
20 /* I don't like nested includes, but the following headers are used
21  * too often
22  */
23 #include <stdio.h>
24 #include <sys/types.h> /* for off_t, time_t */
25 #if defined HAVE_STRING_H || defined STDC_HEADERS
26 #  include <string.h>
27 #  if !defined STDC_HEADERS && defined HAVE_MEMORY_H && !defined __GNUC__
28 #    include <memory.h>
29 #  endif
30 #  define memzero(s, n)     memset ((voidp)(s), 0, (n))
31 #else
32 #  include <strings.h>
33 #  define strchr            index 
34 #  define strrchr           rindex
35 #  define memcpy(d, s, n)   bcopy((s), (d), (n)) 
36 #  define memcmp(s1, s2, n) bcmp((s1), (s2), (n)) 
37 #  define memzero(s, n)     bzero((s), (n))
38 #endif
39
40 #ifndef RETSIGTYPE
41 #  define RETSIGTYPE void
42 #endif
43
44 #define local static
45
46 typedef unsigned char  uch;
47 typedef unsigned short ush;
48 typedef unsigned long  ulg;
49
50 /* Return codes from gzip */
51 #define OK      0
52 #define ERROR   1
53 #define WARNING 2
54
55 /* Compression methods (see algorithm.doc) */
56 #define STORED      0
57 #define COMPRESSED  1
58 #define PACKED      2
59 #define LZHED       3
60 /* methods 4 to 7 reserved */
61 #define DEFLATED    8
62 #define MAX_METHODS 9
63 extern int method;         /* compression method */
64
65 /* To save memory for 16 bit systems, some arrays are overlaid between
66  * the various modules:
67  * deflate:  prev+head   window      d_buf  l_buf  outbuf
68  * unlzw:    tab_prefix  tab_suffix  stack  inbuf  outbuf
69  * inflate:              window             inbuf
70  * unpack:               window             inbuf  prefix_len
71  * unlzh:    left+right  window      c_table inbuf c_len
72  * For compression, input is done in window[]. For decompression, output
73  * is done in window except for unlzw.
74  */
75
76 #ifndef INBUFSIZ
77 #  ifdef SMALL_MEM
78 #    define INBUFSIZ  0x2000  /* input buffer size */
79 #  else
80 #    define INBUFSIZ  0x8000  /* input buffer size */
81 #  endif
82 #endif
83 #define INBUF_EXTRA  64     /* required by unlzw() */
84
85 #ifndef OUTBUFSIZ
86 #  ifdef SMALL_MEM
87 #    define OUTBUFSIZ   8192  /* output buffer size */
88 #  else
89 #    define OUTBUFSIZ  16384  /* output buffer size */
90 #  endif
91 #endif
92 #define OUTBUF_EXTRA 2048   /* required by unlzw() */
93
94 #ifndef DIST_BUFSIZE
95 #  ifdef SMALL_MEM
96 #    define DIST_BUFSIZE 0x2000 /* buffer for distances, see trees.c */
97 #  else
98 #    define DIST_BUFSIZE 0x8000 /* buffer for distances, see trees.c */
99 #  endif
100 #endif
101
102 #ifdef DYN_ALLOC
103 #  define EXTERN(type, array)  extern type * near array
104 #  define DECLARE(type, array, size)  type * near array
105 #  define ALLOC(type, array, size) { \
106       array = (type*)fcalloc((size_t)(((size)+1L)/2), 2*sizeof(type)); \
107       if (array == NULL) error("insufficient memory"); \
108    }
109 #  define FREE(array) {if (array != NULL) fcfree(array), array=NULL;}
110 #else
111 #  define EXTERN(type, array)  extern type array[]
112 #  define DECLARE(type, array, size)  type array[size]
113 #  define ALLOC(type, array, size)
114 #  define FREE(array)
115 #endif
116
117 EXTERN(uch, inbuf);          /* input buffer */
118 EXTERN(uch, outbuf);         /* output buffer */
119 EXTERN(ush, d_buf);          /* buffer for distances, see trees.c */
120 EXTERN(uch, window);         /* Sliding window and suffix table (unlzw) */
121 #define tab_suffix window
122 #ifndef MAXSEG_64K
123 #  define tab_prefix prev    /* hash link (see deflate.c) */
124 #  define head (prev+WSIZE)  /* hash head (see deflate.c) */
125    EXTERN(ush, tab_prefix);  /* prefix code (see unlzw.c) */
126 #else
127 #  define tab_prefix0 prev
128 #  define head tab_prefix1
129    EXTERN(ush, tab_prefix0); /* prefix for even codes */
130    EXTERN(ush, tab_prefix1); /* prefix for odd  codes */
131 #endif
132
133 extern unsigned insize; /* valid bytes in inbuf */
134 extern unsigned inptr;  /* index of next byte to be processed in inbuf */
135 extern unsigned outcnt; /* bytes in output buffer */
136
137 extern off_t bytes_in;   /* number of input bytes */
138 extern off_t bytes_out;  /* number of output bytes */
139 extern off_t header_bytes;/* number of bytes in gzip header */
140
141 extern int  ifd;        /* input file descriptor */
142 extern int  ofd;        /* output file descriptor */
143 extern char ifname[];   /* input file name or "stdin" */
144 extern char ofname[];   /* output file name or "stdout" */
145 extern char *progname;  /* program name */
146
147 extern time_t time_stamp; /* original time stamp (modification time) */
148 extern off_t ifile_size; /* input file size, -1 for devices (debug only) */
149
150 typedef int file_t;     /* Do not use stdio */
151 #define NO_FILE  (-1)   /* in memory compression */
152
153
154 #define PACK_MAGIC     "\037\036" /* Magic header for packed files */
155 #define GZIP_MAGIC     "\037\213" /* Magic header for gzip files, 1F 8B */
156 #define OLD_GZIP_MAGIC "\037\236" /* Magic header for gzip 0.5 = freeze 1.x */
157 #define LZH_MAGIC      "\037\240" /* Magic header for SCO LZH Compress files*/
158 #define PKZIP_MAGIC    "\120\113\003\004" /* Magic header for pkzip files */
159
160 /* gzip flag byte */
161 #define ASCII_FLAG   0x01 /* bit 0 set: file probably ascii text */
162 #define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
163 #define EXTRA_FIELD  0x04 /* bit 2 set: extra field present */
164 #define ORIG_NAME    0x08 /* bit 3 set: original file name present */
165 #define COMMENT      0x10 /* bit 4 set: file comment present */
166 #define ENCRYPTED    0x20 /* bit 5 set: file is encrypted */
167 #define RESERVED     0xC0 /* bit 6,7:   reserved */
168
169 /* internal file attribute */
170 #define UNKNOWN 0xffff
171 #define BINARY  0
172 #define ASCII   1
173
174 #ifndef WSIZE
175 #  define WSIZE 0x8000     /* window size--must be a power of two, and */
176 #endif                     /*  at least 32K for zip's deflate method */
177
178 #define MIN_MATCH  3
179 #define MAX_MATCH  258
180 /* The minimum and maximum match lengths */
181
182 #define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
183 /* Minimum amount of lookahead, except at the end of the input file.
184  * See deflate.c for comments about the MIN_MATCH+1.
185  */
186
187 #define MAX_DIST  (WSIZE-MIN_LOOKAHEAD)
188 /* In order to simplify the code, particularly on 16 bit machines, match
189  * distances are limited to MAX_DIST instead of WSIZE.
190  */
191
192 extern int decrypt;        /* flag to turn on decryption */
193 extern int exit_code;      /* program exit code */
194 extern int verbose;        /* be verbose (-v) */
195 extern int quiet;          /* be quiet (-q) */
196 extern int level;          /* compression level */
197 extern int test;           /* check .z file integrity */
198 extern int to_stdout;      /* output to stdout (-c) */
199 extern int save_orig_name; /* set if original name must be saved */
200
201 #define get_byte()  (inptr < insize ? inbuf[inptr++] : fill_inbuf(0))
202 #define try_byte()  (inptr < insize ? inbuf[inptr++] : fill_inbuf(1))
203
204 /* put_byte is used for the compressed output, put_ubyte for the
205  * uncompressed output. However unlzw() uses window for its
206  * suffix table instead of its output buffer, so it does not use put_ubyte
207  * (to be cleaned up).
208  */
209 #define put_byte(c) {outbuf[outcnt++]=(uch)(c); if (outcnt==OUTBUFSIZ)\
210    flush_outbuf();}
211 #define put_ubyte(c) {window[outcnt++]=(uch)(c); if (outcnt==WSIZE)\
212    flush_window();}
213
214 /* Output a 16 bit value, lsb first */
215 #define put_short(w) \
216 { if (outcnt < OUTBUFSIZ-2) { \
217     outbuf[outcnt++] = (uch) ((w) & 0xff); \
218     outbuf[outcnt++] = (uch) ((ush)(w) >> 8); \
219   } else { \
220     put_byte((uch)((w) & 0xff)); \
221     put_byte((uch)((ush)(w) >> 8)); \
222   } \
223 }
224
225 /* Output a 32 bit value to the bit stream, lsb first */
226 #define put_long(n) { \
227     put_short((n) & 0xffff); \
228     put_short(((ulg)(n)) >> 16); \
229 }
230
231 #define seekable()    0  /* force sequential output */
232 #define translate_eol 0  /* no option -a yet */
233
234 #define tolow(c)  (isupper (c) ? tolower (c) : (c))  /* force to lower case */
235
236 /* Macros for getting two-byte and four-byte header values */
237 #define SH(p) ((ush)(uch)((p)[0]) | ((ush)(uch)((p)[1]) << 8))
238 #define LG(p) ((ulg)(SH(p)) | ((ulg)(SH((p)+2)) << 16))
239
240 /* Diagnostic functions */
241 #ifdef DEBUG
242 #  define Assert(cond,msg) {if(!(cond)) error(msg);}
243 #  define Trace(x) fprintf x
244 #  define Tracev(x) {if (verbose) fprintf x ;}
245 #  define Tracevv(x) {if (verbose>1) fprintf x ;}
246 #  define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
247 #  define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
248 #else
249 #  define Assert(cond,msg)
250 #  define Trace(x)
251 #  define Tracev(x)
252 #  define Tracevv(x)
253 #  define Tracec(c,x)
254 #  define Tracecv(c,x)
255 #endif
256
257 #define WARN(msg) {if (!quiet) fprintf msg ; \
258                    if (exit_code == OK) exit_code = WARNING;}
259
260         /* in zip.c: */
261 extern int zip        OF((int in, int out));
262 extern int file_read  OF((char *buf,  unsigned size));
263
264         /* in unzip.c */
265 extern int unzip      OF((int in, int out));
266 extern int check_zipfile OF((int in));
267
268         /* in unpack.c */
269 extern int unpack     OF((int in, int out));
270
271         /* in unlzh.c */
272 extern int unlzh      OF((int in, int out));
273
274         /* in gzip.c */
275 RETSIGTYPE abort_gzip OF((void));
276
277         /* in deflate.c */
278 void lm_init OF((int pack_level, ush *flags));
279 off_t deflate OF((void));
280
281         /* in trees.c */
282 void ct_init     OF((ush *attr, int *method));
283 int  ct_tally    OF((int dist, int lc));
284 off_t flush_block OF((char *buf, ulg stored_len, int eof));
285
286         /* in bits.c */
287 void     bi_init    OF((file_t zipfile));
288 void     send_bits  OF((int value, int length));
289 unsigned bi_reverse OF((unsigned value, int length));
290 void     bi_windup  OF((void));
291 void     copy_block OF((char *buf, unsigned len, int header));
292 extern   int (*read_buf) OF((char *buf, unsigned size));
293
294         /* in util.c: */
295 extern int copy           OF((int in, int out));
296 extern ulg  updcrc        OF((uch *s, unsigned n));
297 extern void clear_bufs    OF((void));
298 extern int  fill_inbuf    OF((int eof_ok));
299 extern void flush_outbuf  OF((void));
300 extern void flush_window  OF((void));
301 extern void write_buf     OF((int fd, voidp buf, unsigned cnt));
302 extern char *strlwr       OF((char *s));
303 extern char *base_name    OF((char *fname));
304 extern int xunlink        OF((char *fname));
305 extern void make_simple_name OF((char *name));
306 extern char *add_envopt   OF((int *argcp, char ***argvp, char *env));
307 extern void error         OF((char *m));
308 extern void warning       OF((char *m));
309 extern void read_error    OF((void));
310 extern void write_error   OF((void));
311 extern void display_ratio OF((off_t num, off_t den, FILE *file));
312 extern void fprint_off    OF((FILE *, off_t, int));
313 extern voidp xmalloc      OF((unsigned int size));
314
315         /* in inflate.c */
316 extern int inflate OF((void));
317
318         /* in yesno.c */
319 extern int yesno OF((void));