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