document mingw linker fix and close associated bug
[debian/gzip] / gzip.h
1 /* gzip.h -- common declarations for all gzip modules
2
3    Copyright (C) 1997-1999, 2001, 2006-2007, 2009-2018 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 3, 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 #ifdef __STDC__
23    typedef void *voidp;
24 #else
25    typedef char *voidp;
26 #endif
27
28 #ifndef __attribute__
29 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8) || __STRICT_ANSI__
30 #  define __attribute__(x)
31 # endif
32 #endif
33
34 /* I don't like nested includes, but the following headers are used
35  * too often
36  */
37 #include <stdio.h>
38 #include <sys/types.h> /* for off_t */
39 #include <time.h>
40 #include <string.h>
41 #include <stdnoreturn.h>
42 #define memzero(s, n) memset ((voidp)(s), 0, (n))
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) xalloc_die (); \
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 extern int rsync;  /* deflate into rsyncable chunks */
137
138 extern off_t bytes_in;   /* number of input bytes */
139 extern off_t bytes_out;  /* number of output bytes */
140 extern off_t header_bytes;/* number of bytes in gzip header */
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 *program_name;  /* program name */
147
148 extern struct timespec time_stamp; /* original timestamp (modification time) */
149 extern off_t 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 HEADER_CRC   0x02 /* bit 1 set: CRC16 for the gzip header */
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 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)) gzip_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        (int in, int out);
262 extern int file_read  (char *buf,  unsigned size);
263
264         /* in unzip.c */
265 extern int unzip      (int in, int out);
266 extern int check_zipfile (int in);
267
268         /* in unpack.c */
269 extern int unpack     (int in, int out);
270
271         /* in unlzh.c */
272 extern int unlzh      (int in, int out);
273
274         /* in gzip.c */
275 extern noreturn void abort_gzip (void);
276
277         /* in deflate.c */
278 extern void lm_init (int pack_level, ush *flags);
279 extern off_t deflate (void);
280
281         /* in trees.c */
282 extern void ct_init     (ush *attr, int *method);
283 extern int  ct_tally    (int dist, int lc);
284 extern off_t flush_block (char *buf, ulg stored_len, int pad, int eof);
285
286         /* in bits.c */
287 extern void     bi_init    (file_t zipfile);
288 extern void     send_bits  (int value, int length);
289 extern unsigned bi_reverse (unsigned value, int length) _GL_ATTRIBUTE_CONST;
290 extern void     bi_windup  (void);
291 extern void     copy_block (char *buf, unsigned len, int header);
292 extern int     (*read_buf) (char *buf, unsigned size);
293
294         /* in util.c: */
295 extern int copy           (int in, int out);
296 extern ulg  updcrc        (uch *s, unsigned n);
297 extern void clear_bufs    (void);
298 extern int  fill_inbuf    (int eof_ok);
299 extern void flush_outbuf  (void);
300 extern void flush_window  (void);
301 extern void write_buf     (int fd, voidp buf, unsigned cnt);
302 extern int read_buffer    (int fd, voidp buf, unsigned int cnt);
303 extern char *strlwr       (char *s);
304 extern char *gzip_base_name (char *fname) _GL_ATTRIBUTE_PURE;
305 extern int xunlink        (char *fname);
306 extern void make_simple_name (char *name);
307 extern char *add_envopt   (int *argcp, char ***argvp, char const *env);
308 extern noreturn void gzip_error    (char const *m);
309 extern noreturn void xalloc_die    (void);
310 extern void warning       (char const *m);
311 extern noreturn void read_error    (void);
312 extern noreturn void write_error   (void);
313 extern void display_ratio (off_t num, off_t den, FILE *file);
314 extern void fprint_off    (FILE *, off_t, int);
315
316         /* in inflate.c */
317 extern int inflate (void);