1 /* gzip.h -- common declarations for all gzip modules
3 Copyright (C) 1997-1999, 2001, 2006-2007, 2009-2018 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 /* I don't like nested includes, but the following headers are used
38 #include <sys/types.h> /* for off_t */
41 #include <stdnoreturn.h>
42 #define memzero(s, n) memset ((voidp)(s), 0, (n))
46 typedef unsigned char uch
;
47 typedef unsigned short ush
;
48 typedef unsigned long ulg
;
50 /* Return codes from gzip */
55 /* Compression methods (see algorithm.doc) */
60 /* methods 4 to 7 reserved */
63 extern int method
; /* compression method */
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.
78 # define INBUFSIZ 0x2000 /* input buffer size */
80 # define INBUFSIZ 0x8000 /* input buffer size */
83 #define INBUF_EXTRA 64 /* required by unlzw() */
87 # define OUTBUFSIZ 8192 /* output buffer size */
89 # define OUTBUFSIZ 16384 /* output buffer size */
92 #define OUTBUF_EXTRA 2048 /* required by unlzw() */
96 # define DIST_BUFSIZE 0x2000 /* buffer for distances, see trees.c */
98 # define DIST_BUFSIZE 0x8000 /* buffer for distances, see trees.c */
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 (); \
109 # define FREE(array) {if (array != NULL) fcfree(array), array=NULL;}
111 # define EXTERN(type, array) extern type array[]
112 # define DECLARE(type, array, size) type array[size]
113 # define ALLOC(type, array, size)
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
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) */
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 */
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 */
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 */
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 */
148 extern struct timespec time_stamp
; /* original timestamp (modification time) */
149 extern off_t ifile_size
; /* input file size, -1 for devices (debug only) */
151 typedef int file_t
; /* Do not use stdio */
152 #define NO_FILE (-1) /* in memory compression */
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 */
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 */
170 /* internal file attribute */
171 #define UNKNOWN 0xffff
176 # define WSIZE 0x8000 /* window size--must be a power of two, and */
177 #endif /* at least 32K for zip's deflate method */
180 #define MAX_MATCH 258
181 /* The minimum and maximum match lengths */
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.
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.
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 */
201 #define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf(0))
202 #define try_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf(1))
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).
209 #define put_byte(c) {outbuf[outcnt++]=(uch)(c); if (outcnt==OUTBUFSIZ)\
211 #define put_ubyte(c) {window[outcnt++]=(uch)(c); if (outcnt==WSIZE)\
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); \
220 put_byte((uch)((w) & 0xff)); \
221 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) ? tolower (c) : (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 */
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 ;}
249 # define Assert(cond,msg)
254 # define Tracecv(c,x)
257 #define WARN(msg) {if (!quiet) fprintf msg ; \
258 if (exit_code == OK) exit_code = WARNING;}
261 extern int zip (int in
, int out
);
262 extern int file_read (char *buf
, unsigned size
);
265 extern int unzip (int in
, int out
);
266 extern int check_zipfile (int in
);
269 extern int unpack (int in
, int out
);
272 extern int unlzh (int in
, int out
);
275 extern noreturn
void abort_gzip (void);
278 extern void lm_init (int pack_level
, ush
*flags
);
279 extern off_t
deflate (void);
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
);
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
);
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);
317 extern int inflate (void);