Add README
[xapian-trec.git] / gzip.h
blobbb97fc5c3772579bb2c8a2ed15327f36b6b5e1a6
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 */
7 #if defined(__STDC__) || defined(PROTO)
8 # define OF(args) args
9 #else
10 # define OF(args) ()
11 #endif
13 #ifdef __STDC__
14 typedef void *voidp;
15 #else
16 typedef char *voidp;
17 #endif
19 /* I don't like nested includes, but the string and io functions are used
20 * too often
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
38 #ifndef RETSIGTYPE
39 # define RETSIGTYPE void
40 #endif
42 #define local static
44 typedef unsigned char uch;
45 typedef unsigned short ush;
46 typedef unsigned long ulg;
48 /* Return codes from gzip */
49 #define OK 0
50 #define ERROR 1
51 #define WARNING 2
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 */
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.
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() */
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() */
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
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"); \
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
115 EXTERN(uch, inbuf); /* input buffer */
116 /*EXTERN(uch, outbuf); /* output buffer */
117 extern uch *outbuf;
118 EXTERN(ush, d_buf); /* buffer for distances, see trees.c */
119 EXTERN(uch, window); /* Sliding window and suffix table (unlzw) */
120 #define tab_suffix window
121 #ifndef MAXSEG_64K
122 # define tab_prefix prev /* hash link (see deflate.c) */
123 # define head (prev+WSIZE) /* hash head (see deflate.c) */
124 EXTERN(ush, tab_prefix); /* prefix code (see unlzw.c) */
125 #else
126 # define tab_prefix0 prev
127 # define head tab_prefix1
128 EXTERN(ush, tab_prefix0); /* prefix for even codes */
129 EXTERN(ush, tab_prefix1); /* prefix for odd codes */
130 #endif
132 extern unsigned insize; /* valid bytes in inbuf */
133 extern unsigned inptr; /* index of next byte to be processed in inbuf */
134 extern unsigned outcnt; /* bytes in output buffer , used for both window
135 and outbuf?????*/
136 extern unsigned outbuflen; /* length of output buffer */
137 extern unsigned outbufptr; /* bytes in output buffer */
139 extern long bytes_in; /* number of input bytes */
140 extern long bytes_out; /* number of output bytes */
141 extern long header_bytes;/* number of bytes in gzip header */
143 #define isize bytes_in
144 /* for compatibility with old zip sources (to be cleaned) */
146 extern int ifd; /* input file descriptor */
147 extern int ofd; /* output file descriptor */
148 extern char ifname[]; /* input file name or "stdin" */
149 extern char ofname[]; /* output file name or "stdout" */
150 extern char progname[]; /* program name */
152 extern long time_stamp; /* original time stamp (modification time) */
153 extern long ifile_size; /* input file size, -1 for devices (debug only) */
155 typedef int file_t; /* Do not use stdio */
156 #define NO_FILE (-1) /* in memory compression */
159 #define PACK_MAGIC "\037\036" /* Magic header for packed files */
160 #define GZIP_MAGIC "\037\213" /* Magic header for gzip files, 1F 8B */
161 #define OLD_GZIP_MAGIC "\037\236" /* Magic header for gzip 0.5 = freeze 1.x */
162 #define LZH_MAGIC "\037\240" /* Magic header for SCO LZH Compress files*/
163 #define PKZIP_MAGIC "\120\113\003\004" /* Magic header for pkzip files */
165 /* gzip flag byte */
166 #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
167 #define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
168 #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
169 #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
170 #define COMMENT 0x10 /* bit 4 set: file comment present */
171 #define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
172 #define RESERVED 0xC0 /* bit 6,7: reserved */
174 /* internal file attribute */
175 #define UNKNOWN 0xffff
176 #define BINARY 0
177 #define ASCII 1
179 #ifndef WSIZE
180 # define WSIZE 0x8000 /* window size--must be a power of two, and */
181 #endif /* at least 32K for zip's deflate method */
183 #define MIN_MATCH 3
184 #define MAX_MATCH 258
185 /* The minimum and maximum match lengths */
187 #define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
188 /* Minimum amount of lookahead, except at the end of the input file.
189 * See deflate.c for comments about the MIN_MATCH+1.
192 #define MAX_DIST (WSIZE-MIN_LOOKAHEAD)
193 /* In order to simplify the code, particularly on 16 bit machines, match
194 * distances are limited to MAX_DIST instead of WSIZE.
197 extern int decrypt; /* flag to turn on decryption */
198 extern int exit_code; /* program exit code */
199 extern int verbose; /* be verbose (-v) */
200 extern int quiet; /* be quiet (-q) */
201 extern int level; /* compression level */
202 extern int test; /* check .z file integrity */
203 extern int to_stdout; /* output to stdout (-c) */
204 extern int save_orig_name; /* set if original name must be saved */
206 #define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf(0))
207 #define try_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf(1))
209 /* put_byte is used for the compressed output, put_ubyte for the
210 * uncompressed output. However unlzw() uses window for its
211 * suffix table instead of its output buffer, so it does not use put_ubyte
212 * (to be cleaned up).
214 #define put_byte(c) {outbuf[outbufptr++]=(uch)(c); if (outbufptr>=outbuflen)\
215 {fprintf(stderr, "Error: Chamber overflow \n"); outbufptr=0;}}
216 #define put_ubyte(c) {window[outcnt++]=(uch)(c); if (outcnt==WSIZE)\
217 flush_window();}
219 /* Output a 16 bit value, lsb first */
220 #define put_short(w) \
221 { put_byte((uch)((w) & 0xff)); \
222 put_byte((uch)((ush)(w) >> 8)); \
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); \
231 #define seekable() 0 /* force sequential output */
232 #define translate_eol 0 /* no option -a yet */
234 #define tolow(c) (isupper(c) ? (c)-'A'+'a' : (c)) /* force to lower case */
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))
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
257 #define WARN(msg) {if (!quiet) fprintf msg ; \
258 if (exit_code == OK) exit_code = WARNING;}
260 /* in zip.c: */
261 extern int zip OF((int in, int out));
262 extern int file_read OF((char *buf, unsigned size));
264 /* in unzip.c */
265 extern int unzip OF((int in, int out));
266 extern int check_zipfile OF((int in));
268 /* in unpack.c */
269 extern int unpack OF((int in, int out));
271 /* in unlzh.c */
272 extern int unlzh OF((int in, int out));
274 /* in gzip.c */
275 RETSIGTYPE abort_gzip OF((void));
277 /* in deflate.c */
278 void lm_init OF((int pack_level, ush *flags));
279 ulg deflate OF((void));
281 /* in trees.c */
282 void ct_init OF((ush *attr, int *method));
283 int ct_tally OF((int dist, int lc));
284 ulg flush_block OF((char *buf, ulg stored_len, int eof));
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));
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 *basename OF((char *fname));
304 extern void make_simple_name OF((char *name));
305 extern char *add_envopt OF((int *argcp, char ***argvp, char *env));
306 extern void error OF((char *m));
307 extern void warn OF((char *a, char *b));
308 extern void read_error OF((void));
309 extern void write_error OF((void));
310 extern void display_ratio OF((long num, long den, FILE *file));
311 extern voidp xmalloc OF((unsigned int size));
313 /* in inflate.c */
314 extern int inflate OF((void));