New upstream version 1.9
[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 #ifndef RETSIGTYPE
45 #  define RETSIGTYPE void
46 #endif
47
48 #define local static
49
50 typedef unsigned char  uch;
51 typedef unsigned short ush;
52 typedef unsigned long  ulg;
53
54 /* Return codes from gzip */
55 #define OK      0
56 #define ERROR   1
57 #define WARNING 2
58
59 /* Compression methods (see algorithm.doc) */
60 #define STORED      0
61 #define COMPRESSED  1
62 #define PACKED      2
63 #define LZHED       3
64 /* methods 4 to 7 reserved */
65 #define DEFLATED    8
66 #define MAX_METHODS 9
67 extern int method;         /* compression method */
68
69 /* To save memory for 16 bit systems, some arrays are overlaid between
70  * the various modules:
71  * deflate:  prev+head   window      d_buf  l_buf  outbuf
72  * unlzw:    tab_prefix  tab_suffix  stack  inbuf  outbuf
73  * inflate:              window             inbuf
74  * unpack:               window             inbuf  prefix_len
75  * unlzh:    left+right  window      c_table inbuf c_len
76  * For compression, input is done in window[]. For decompression, output
77  * is done in window except for unlzw.
78  */
79
80 #ifndef INBUFSIZ
81 #  ifdef SMALL_MEM
82 #    define INBUFSIZ  0x2000  /* input buffer size */
83 #  else
84 #    define INBUFSIZ  0x8000  /* input buffer size */
85 #  endif
86 #endif
87 #define INBUF_EXTRA  64     /* required by unlzw() */
88
89 #ifndef OUTBUFSIZ
90 #  ifdef SMALL_MEM
91 #    define OUTBUFSIZ   8192  /* output buffer size */
92 #  else
93 #    define OUTBUFSIZ  16384  /* output buffer size */
94 #  endif
95 #endif
96 #define OUTBUF_EXTRA 2048   /* required by unlzw() */
97
98 #ifndef DIST_BUFSIZE
99 #  ifdef SMALL_MEM
100 #    define DIST_BUFSIZE 0x2000 /* buffer for distances, see trees.c */
101 #  else
102 #    define DIST_BUFSIZE 0x8000 /* buffer for distances, see trees.c */
103 #  endif
104 #endif
105
106 #ifdef DYN_ALLOC
107 #  define EXTERN(type, array)  extern type * near array
108 #  define DECLARE(type, array, size)  type * near array
109 #  define ALLOC(type, array, size) { \
110       array = (type*)fcalloc((size_t)(((size)+1L)/2), 2*sizeof(type)); \
111       if (!array) xalloc_die (); \
112    }
113 #  define FREE(array) {if (array != NULL) fcfree(array), array=NULL;}
114 #else
115 #  define EXTERN(type, array)  extern type array[]
116 #  define DECLARE(type, array, size)  type array[size]
117 #  define ALLOC(type, array, size)
118 #  define FREE(array)
119 #endif
120
121 EXTERN(uch, inbuf);          /* input buffer */
122 EXTERN(uch, outbuf);         /* output buffer */
123 EXTERN(ush, d_buf);          /* buffer for distances, see trees.c */
124 EXTERN(uch, window);         /* Sliding window and suffix table (unlzw) */
125 #define tab_suffix window
126 #ifndef MAXSEG_64K
127 #  define tab_prefix prev    /* hash link (see deflate.c) */
128 #  define head (prev+WSIZE)  /* hash head (see deflate.c) */
129    EXTERN(ush, tab_prefix);  /* prefix code (see unlzw.c) */
130 #else
131 #  define tab_prefix0 prev
132 #  define head tab_prefix1
133    EXTERN(ush, tab_prefix0); /* prefix for even codes */
134    EXTERN(ush, tab_prefix1); /* prefix for odd  codes */
135 #endif
136
137 extern unsigned insize; /* valid bytes in inbuf */
138 extern unsigned inptr;  /* index of next byte to be processed in inbuf */
139 extern unsigned outcnt; /* bytes in output buffer */
140 extern int rsync;  /* deflate into rsyncable chunks */
141
142 extern off_t bytes_in;   /* number of input bytes */
143 extern off_t bytes_out;  /* number of output bytes */
144 extern off_t header_bytes;/* number of bytes in gzip header */
145
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 *program_name;  /* program name */
151
152 extern struct timespec time_stamp; /* original timestamp (modification time) */
153 extern off_t ifile_size; /* input file size, -1 for devices (debug only) */
154
155 typedef int file_t;     /* Do not use stdio */
156 #define NO_FILE  (-1)   /* in memory compression */
157
158
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 */
164
165 /* gzip flag byte */
166 #define ASCII_FLAG   0x01 /* bit 0 set: file probably ascii text */
167 #define HEADER_CRC   0x02 /* bit 1 set: CRC16 for the gzip header */
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 */
173
174 /* internal file attribute */
175 #define UNKNOWN 0xffff
176 #define BINARY  0
177 #define ASCII   1
178
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 */
182
183 #define MIN_MATCH  3
184 #define MAX_MATCH  258
185 /* The minimum and maximum match lengths */
186
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.
190  */
191
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.
195  */
196
197 extern int exit_code;      /* program exit code */
198 extern int verbose;        /* be verbose (-v) */
199 extern int quiet;          /* be quiet (-q) */
200 extern int level;          /* compression level */
201 extern int test;           /* check .z file integrity */
202 extern int to_stdout;      /* output to stdout (-c) */
203 extern int save_orig_name; /* set if original name must be saved */
204
205 #define get_byte()  (inptr < insize ? inbuf[inptr++] : fill_inbuf(0))
206 #define try_byte()  (inptr < insize ? inbuf[inptr++] : fill_inbuf(1))
207
208 /* put_byte is used for the compressed output, put_ubyte for the
209  * uncompressed output. However unlzw() uses window for its
210  * suffix table instead of its output buffer, so it does not use put_ubyte
211  * (to be cleaned up).
212  */
213 #define put_byte(c) {outbuf[outcnt++]=(uch)(c); if (outcnt==OUTBUFSIZ)\
214    flush_outbuf();}
215 #define put_ubyte(c) {window[outcnt++]=(uch)(c); if (outcnt==WSIZE)\
216    flush_window();}
217
218 /* Output a 16 bit value, lsb first */
219 #define put_short(w) \
220 { if (outcnt < OUTBUFSIZ-2) { \
221     outbuf[outcnt++] = (uch) ((w) & 0xff); \
222     outbuf[outcnt++] = (uch) ((ush)(w) >> 8); \
223   } else { \
224     put_byte((uch)((w) & 0xff)); \
225     put_byte((uch)((ush)(w) >> 8)); \
226   } \
227 }
228
229 /* Output a 32 bit value to the bit stream, lsb first */
230 #define put_long(n) { \
231     put_short((n) & 0xffff); \
232     put_short(((ulg)(n)) >> 16); \
233 }
234
235 #define seekable()    0  /* force sequential output */
236 #define translate_eol 0  /* no option -a yet */
237
238 #define tolow(c)  (isupper (c) ? tolower (c) : (c))  /* force to lower case */
239
240 /* Macros for getting two-byte and four-byte header values */
241 #define SH(p) ((ush)(uch)((p)[0]) | ((ush)(uch)((p)[1]) << 8))
242 #define LG(p) ((ulg)(SH(p)) | ((ulg)(SH((p)+2)) << 16))
243
244 /* Diagnostic functions */
245 #ifdef DEBUG
246 #  define Assert(cond,msg) {if (!(cond)) gzip_error (msg);}
247 #  define Trace(x) fprintf x
248 #  define Tracev(x) {if (verbose) fprintf x ;}
249 #  define Tracevv(x) {if (verbose>1) fprintf x ;}
250 #  define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
251 #  define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
252 #else
253 #  define Assert(cond,msg)
254 #  define Trace(x)
255 #  define Tracev(x)
256 #  define Tracevv(x)
257 #  define Tracec(c,x)
258 #  define Tracecv(c,x)
259 #endif
260
261 #define WARN(msg) {if (!quiet) fprintf msg ; \
262                    if (exit_code == OK) exit_code = WARNING;}
263
264         /* in zip.c: */
265 extern int zip        (int in, int out);
266 extern int file_read  (char *buf,  unsigned size);
267
268         /* in unzip.c */
269 extern int unzip      (int in, int out);
270 extern int check_zipfile (int in);
271
272         /* in unpack.c */
273 extern int unpack     (int in, int out);
274
275         /* in unlzh.c */
276 extern int unlzh      (int in, int out);
277
278         /* in gzip.c */
279 extern noreturn void abort_gzip (void);
280
281         /* in deflate.c */
282 extern void lm_init (int pack_level, ush *flags);
283 extern off_t deflate (void);
284
285         /* in trees.c */
286 extern void ct_init     (ush *attr, int *method);
287 extern int  ct_tally    (int dist, int lc);
288 extern off_t flush_block (char *buf, ulg stored_len, int pad, int eof);
289
290         /* in bits.c */
291 extern void     bi_init    (file_t zipfile);
292 extern void     send_bits  (int value, int length);
293 extern unsigned bi_reverse (unsigned value, int length) _GL_ATTRIBUTE_CONST;
294 extern void     bi_windup  (void);
295 extern void     copy_block (char *buf, unsigned len, int header);
296 extern int     (*read_buf) (char *buf, unsigned size);
297
298         /* in util.c: */
299 extern int copy           (int in, int out);
300 extern ulg  updcrc        (uch *s, unsigned n);
301 extern void clear_bufs    (void);
302 extern int  fill_inbuf    (int eof_ok);
303 extern void flush_outbuf  (void);
304 extern void flush_window  (void);
305 extern void write_buf     (int fd, voidp buf, unsigned cnt);
306 extern int read_buffer    (int fd, voidp buf, unsigned int cnt);
307 extern char *strlwr       (char *s);
308 extern char *gzip_base_name (char *fname) _GL_ATTRIBUTE_PURE;
309 extern int xunlink        (char *fname);
310 extern void make_simple_name (char *name);
311 extern char *add_envopt   (int *argcp, char ***argvp, char const *env);
312 extern noreturn void gzip_error    (char const *m);
313 extern noreturn void xalloc_die    (void);
314 extern void warning       (char const *m);
315 extern noreturn void read_error    (void);
316 extern noreturn void write_error   (void);
317 extern void display_ratio (off_t num, off_t den, FILE *file);
318 extern void fprint_off    (FILE *, off_t, int);
319
320         /* in inflate.c */
321 extern int inflate (void);