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