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