1 /* gzip.h -- common declarations for all gzip modules
3 Copyright (C) 1997-1999, 2001, 2006-2007, 2009-2016 Free Software
6 Copyright (C) 1992-1993 Jean-loup Gailly.
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)
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.
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. */
29 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8) || __STRICT_ANSI__
30 # define __attribute__(x)
34 #ifndef ATTRIBUTE_NORETURN
35 # define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
38 /* I don't like nested includes, but the following headers are used
42 #include <sys/types.h> /* for off_t */
45 #define memzero(s, n) memset ((voidp)(s), 0, (n))
48 # define RETSIGTYPE void
53 typedef unsigned char uch
;
54 typedef unsigned short ush
;
55 typedef unsigned long ulg
;
57 /* Return codes from gzip */
62 /* Compression methods (see algorithm.doc) */
67 /* methods 4 to 7 reserved */
70 extern int method
; /* compression method */
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.
85 # define INBUFSIZ 0x2000 /* input buffer size */
87 # define INBUFSIZ 0x8000 /* input buffer size */
90 #define INBUF_EXTRA 64 /* required by unlzw() */
94 # define OUTBUFSIZ 8192 /* output buffer size */
96 # define OUTBUFSIZ 16384 /* output buffer size */
99 #define OUTBUF_EXTRA 2048 /* required by unlzw() */
103 # define DIST_BUFSIZE 0x2000 /* buffer for distances, see trees.c */
105 # define DIST_BUFSIZE 0x8000 /* buffer for distances, see trees.c */
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 (); \
116 # define FREE(array) {if (array != NULL) fcfree(array), array=NULL;}
118 # define EXTERN(type, array) extern type array[]
119 # define DECLARE(type, array, size) type array[size]
120 # define ALLOC(type, array, size)
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
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) */
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 */
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 */
144 extern off_t bytes_in
; /* number of input bytes */
145 extern off_t bytes_out
; /* number of output bytes */
146 extern off_t header_bytes
;/* number of bytes in gzip header */
148 extern int ifd
; /* input file descriptor */
149 extern int ofd
; /* output file descriptor */
150 extern char ifname
[]; /* input file name or "stdin" */
151 extern char ofname
[]; /* output file name or "stdout" */
152 extern char *program_name
; /* program name */
154 extern struct timespec time_stamp
; /* original time stamp (modification time) */
155 extern off_t ifile_size
; /* input file size, -1 for devices (debug only) */
157 typedef int file_t
; /* Do not use stdio */
158 #define NO_FILE (-1) /* in memory compression */
161 #define PACK_MAGIC "\037\036" /* Magic header for packed files */
162 #define GZIP_MAGIC "\037\213" /* Magic header for gzip files, 1F 8B */
163 #define OLD_GZIP_MAGIC "\037\236" /* Magic header for gzip 0.5 = freeze 1.x */
164 #define LZH_MAGIC "\037\240" /* Magic header for SCO LZH Compress files*/
165 #define PKZIP_MAGIC "\120\113\003\004" /* Magic header for pkzip files */
168 #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
169 #define HEADER_CRC 0x02 /* bit 1 set: CRC16 for the gzip header */
170 #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
171 #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
172 #define COMMENT 0x10 /* bit 4 set: file comment present */
173 #define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
174 #define RESERVED 0xC0 /* bit 6,7: reserved */
176 /* internal file attribute */
177 #define UNKNOWN 0xffff
182 # define WSIZE 0x8000 /* window size--must be a power of two, and */
183 #endif /* at least 32K for zip's deflate method */
186 #define MAX_MATCH 258
187 /* The minimum and maximum match lengths */
189 #define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
190 /* Minimum amount of lookahead, except at the end of the input file.
191 * See deflate.c for comments about the MIN_MATCH+1.
194 #define MAX_DIST (WSIZE-MIN_LOOKAHEAD)
195 /* In order to simplify the code, particularly on 16 bit machines, match
196 * distances are limited to MAX_DIST instead of WSIZE.
199 extern int exit_code
; /* program exit code */
200 extern int verbose
; /* be verbose (-v) */
201 extern int quiet
; /* be quiet (-q) */
202 extern int level
; /* compression level */
203 extern int test
; /* check .z file integrity */
204 extern int to_stdout
; /* output to stdout (-c) */
205 extern int save_orig_name
; /* set if original name must be saved */
207 #define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf(0))
208 #define try_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf(1))
210 /* put_byte is used for the compressed output, put_ubyte for the
211 * uncompressed output. However unlzw() uses window for its
212 * suffix table instead of its output buffer, so it does not use put_ubyte
213 * (to be cleaned up).
215 #define put_byte(c) {outbuf[outcnt++]=(uch)(c); if (outcnt==OUTBUFSIZ)\
217 #define put_ubyte(c) {window[outcnt++]=(uch)(c); if (outcnt==WSIZE)\
220 /* Output a 16 bit value, lsb first */
221 #define put_short(w) \
222 { if (outcnt < OUTBUFSIZ-2) { \
223 outbuf[outcnt++] = (uch) ((w) & 0xff); \
224 outbuf[outcnt++] = (uch) ((ush)(w) >> 8); \
226 put_byte((uch)((w) & 0xff)); \
227 put_byte((uch)((ush)(w) >> 8)); \
231 /* Output a 32 bit value to the bit stream, lsb first */
232 #define put_long(n) { \
233 put_short((n) & 0xffff); \
234 put_short(((ulg)(n)) >> 16); \
237 #define seekable() 0 /* force sequential output */
238 #define translate_eol 0 /* no option -a yet */
240 #define tolow(c) (isupper (c) ? tolower (c) : (c)) /* force to lower case */
242 /* Macros for getting two-byte and four-byte header values */
243 #define SH(p) ((ush)(uch)((p)[0]) | ((ush)(uch)((p)[1]) << 8))
244 #define LG(p) ((ulg)(SH(p)) | ((ulg)(SH((p)+2)) << 16))
246 /* Diagnostic functions */
248 # define Assert(cond,msg) {if (!(cond)) gzip_error (msg);}
249 # define Trace(x) fprintf x
250 # define Tracev(x) {if (verbose) fprintf x ;}
251 # define Tracevv(x) {if (verbose>1) fprintf x ;}
252 # define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
253 # define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
255 # define Assert(cond,msg)
260 # define Tracecv(c,x)
263 #define WARN(msg) {if (!quiet) fprintf msg ; \
264 if (exit_code == OK) exit_code = WARNING;}
267 extern int zip (int in
, int out
);
268 extern int file_read (char *buf
, unsigned size
);
271 extern int unzip (int in
, int out
);
272 extern int check_zipfile (int in
);
275 extern int unpack (int in
, int out
);
278 extern int unlzh (int in
, int out
);
281 extern void abort_gzip (void) ATTRIBUTE_NORETURN
;
284 extern void lm_init (int pack_level
, ush
*flags
);
285 extern off_t
deflate (void);
288 extern void ct_init (ush
*attr
, int *method
);
289 extern int ct_tally (int dist
, int lc
);
290 extern off_t
flush_block (char *buf
, ulg stored_len
, int eof
);
293 extern void bi_init (file_t zipfile
);
294 extern void send_bits (int value
, int length
);
295 extern unsigned bi_reverse (unsigned value
, int length
) _GL_ATTRIBUTE_CONST
;
296 extern void bi_windup (void);
297 extern void copy_block (char *buf
, unsigned len
, int header
);
298 extern int (*read_buf
) (char *buf
, unsigned size
);
301 extern int copy (int in
, int out
);
302 extern ulg
updcrc (uch
*s
, unsigned n
);
303 extern void clear_bufs (void);
304 extern int fill_inbuf (int eof_ok
);
305 extern void flush_outbuf (void);
306 extern void flush_window (void);
307 extern void write_buf (int fd
, voidp buf
, unsigned cnt
);
308 extern int read_buffer (int fd
, voidp buf
, unsigned int cnt
);
309 extern char *strlwr (char *s
);
310 extern char *gzip_base_name (char *fname
) _GL_ATTRIBUTE_PURE
;
311 extern int xunlink (char *fname
);
312 extern void make_simple_name (char *name
);
313 extern char *add_envopt (int *argcp
, char ***argvp
, char const *env
);
314 extern void gzip_error (char const *m
) ATTRIBUTE_NORETURN
;
315 extern void xalloc_die (void) ATTRIBUTE_NORETURN
;
316 extern void warning (char const *m
);
317 extern void read_error (void) ATTRIBUTE_NORETURN
;
318 extern void write_error (void) ATTRIBUTE_NORETURN
;
319 extern void display_ratio (off_t num
, off_t den
, FILE *file
);
320 extern void fprint_off (FILE *, off_t
, int);
323 extern int inflate (void);