1 /* gzip.h -- common declarations for all gzip modules
3 Copyright (C) 1997-1999, 2001, 2006-2007, 2009-2024 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 #define memzero(s, n) memset ((voidp)(s), 0, (n))
43 typedef unsigned char uch
;
44 typedef unsigned short ush
;
45 typedef unsigned long ulg
;
47 /* Return codes from gzip */
52 /* Compression methods (see algorithm.doc) */
57 /* methods 4 to 7 reserved */
60 extern int method
; /* compression method */
62 /* To save memory for 16 bit systems, some arrays are overlaid between
63 * the various modules:
64 * deflate: prev+head window d_buf l_buf outbuf
65 * unlzw: tab_prefix tab_suffix stack inbuf outbuf
66 * inflate: window inbuf
67 * unpack: window inbuf prefix_len
68 * unlzh: left+right window c_table inbuf c_len
69 * For compression, input is done in window[]. For decompression, output
70 * is done in window except for unlzw.
75 # define INBUFSIZ 0x2000 /* input buffer size */
77 # define INBUFSIZ 0x40000 /* input buffer size */
80 #define INBUF_EXTRA 64 /* required by unlzw() */
84 # define OUTBUFSIZ 8192 /* output buffer size */
86 # define OUTBUFSIZ 0x40000 /* output buffer size */
89 #define OUTBUF_EXTRA 2048 /* required by unlzw() */
93 # define DIST_BUFSIZE 0x2000 /* buffer for distances, see trees.c */
95 # define DIST_BUFSIZE 0x8000 /* buffer for distances, see trees.c */
100 # define EXTERN(type, array) extern type * near array
101 # define DECLARE(type, array, size) type * near array
102 # define ALLOC(type, array, size) { \
103 array = (type*)fcalloc((size_t)(((size)+1L)/2), 2*sizeof(type)); \
104 if (!array) xalloc_die (); \
106 # define FREE(array) {if (array != NULL) fcfree(array), array=NULL;}
108 # define EXTERN(type, array) extern type array[]
109 # define DECLARE(type, array, size) type array[size]
110 # define ALLOC(type, array, size)
114 EXTERN(uch
, inbuf
); /* input buffer */
115 EXTERN(uch
, outbuf
); /* output buffer */
116 EXTERN(ush
, d_buf
); /* buffer for distances, see trees.c */
117 EXTERN(uch
, window
); /* Sliding window and suffix table (unlzw) */
118 #define tab_suffix window
120 # define tab_prefix prev /* hash link (see deflate.c) */
121 # define head (prev+WSIZE) /* hash head (see deflate.c) */
122 EXTERN(ush
, tab_prefix
); /* prefix code (see unlzw.c) */
124 # define tab_prefix0 prev
125 # define head tab_prefix1
126 EXTERN(ush
, tab_prefix0
); /* prefix for even codes */
127 EXTERN(ush
, tab_prefix1
); /* prefix for odd codes */
130 extern unsigned insize
; /* valid bytes in inbuf */
131 extern unsigned inptr
; /* index of next byte to be processed in inbuf */
132 extern unsigned outcnt
; /* bytes in output buffer */
133 extern int rsync
; /* deflate into rsyncable chunks */
135 extern off_t bytes_in
; /* number of input bytes */
136 extern off_t bytes_out
; /* number of output bytes */
137 extern off_t header_bytes
;/* number of bytes in gzip header */
139 extern int ifd
; /* input file descriptor */
140 extern int ofd
; /* output file descriptor */
141 extern char ifname
[]; /* input file name or "stdin" */
142 extern char ofname
[]; /* output file name or "stdout" */
143 extern char *program_name
; /* program name */
145 extern struct timespec time_stamp
; /* original timestamp (modification time) */
146 extern off_t ifile_size
; /* input file size, -1 for devices (debug only) */
148 typedef int file_t
; /* Do not use stdio */
149 #define NO_FILE (-1) /* in memory compression */
152 #define PACK_MAGIC "\037\036" /* Magic header for packed files */
153 #define GZIP_MAGIC "\037\213" /* Magic header for gzip files, 1F 8B */
154 #define OLD_GZIP_MAGIC "\037\236" /* Magic header for gzip 0.5 = freeze 1.x */
155 #define LZH_MAGIC "\037\240" /* Magic header for SCO LZH Compress files*/
156 #define PKZIP_MAGIC "\120\113\003\004" /* Magic header for pkzip files */
159 #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
160 #define HEADER_CRC 0x02 /* bit 1 set: CRC16 for the gzip header */
161 #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
162 #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
163 #define COMMENT 0x10 /* bit 4 set: file comment present */
164 #define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
165 #define RESERVED 0xC0 /* bit 6,7: reserved */
167 /* internal file attribute */
168 #define UNKNOWN 0xffff
173 # define WSIZE 0x8000 /* window size--must be a power of two, and */
174 #endif /* at least 32K for zip's deflate method */
177 #define MAX_MATCH 258
178 /* The minimum and maximum match lengths */
180 #define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
181 /* Minimum amount of lookahead, except at the end of the input file.
182 * See deflate.c for comments about the MIN_MATCH+1.
185 #define MAX_DIST (WSIZE-MIN_LOOKAHEAD)
186 /* In order to simplify the code, particularly on 16 bit machines, match
187 * distances are limited to MAX_DIST instead of WSIZE.
190 extern int exit_code
; /* program exit code */
191 extern int quiet
; /* be quiet (-q) */
192 extern int level
; /* compression level */
193 extern int test
; /* check .z file integrity */
194 extern int to_stdout
; /* output to stdout (-c) */
195 extern int save_orig_name
; /* set if original name must be saved */
197 #define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf(0))
198 #define try_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf(1))
200 /* put_byte is used for the compressed output, put_ubyte for the
201 * uncompressed output. However unlzw() uses window for its
202 * suffix table instead of its output buffer, so it does not use put_ubyte
203 * (to be cleaned up).
205 #define put_byte(c) {outbuf[outcnt++]=(uch)(c); if (outcnt==OUTBUFSIZ)\
207 #define put_ubyte(c) {window[outcnt++]=(uch)(c); if (outcnt==WSIZE)\
210 /* Output a 16 bit value, lsb first */
211 #define put_short(w) \
212 { if (outcnt < OUTBUFSIZ-2) { \
213 outbuf[outcnt++] = (uch) ((w) & 0xff); \
214 outbuf[outcnt++] = (uch) ((ush)(w) >> 8); \
216 put_byte((uch)((w) & 0xff)); \
217 put_byte((uch)((ush)(w) >> 8)); \
221 /* Output a 32 bit value to the bit stream, lsb first */
222 #define put_long(n) { \
223 put_short((n) & 0xffff); \
224 put_short(((ulg)(n)) >> 16); \
227 #define seekable() 0 /* force sequential output */
228 #define translate_eol 0 /* no option -a yet */
230 #define tolow(c) (isupper (c) ? tolower (c) : (c)) /* force to lower case */
232 /* Macros for getting two-byte and four-byte header values */
233 #define SH(p) ((ush)(uch)((p)[0]) | ((ush)(uch)((p)[1]) << 8))
234 #define LG(p) ((ulg)(SH(p)) | ((ulg)(SH((p)+2)) << 16))
236 /* Diagnostic functions */
238 extern int verbose
; /* be verbose (-v) */
239 # define Assert(cond,msg) {if (!(cond)) gzip_error (msg);}
240 # define Trace(x) fprintf x
241 # define Tracev(x) {if (verbose) fprintf x ;}
242 # define Tracevv(x) {if (verbose>1) fprintf x ;}
243 # define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
244 # define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
246 # define Assert(cond,msg)
251 # define Tracecv(c,x)
259 if (exit_code == OK) \
260 exit_code = WARNING; \
265 extern int zip (int in
, int out
);
266 extern int file_read (char *buf
, unsigned size
);
269 extern ulg unzip_crc
;
270 extern int unzip (int in
, int out
);
271 extern int check_zipfile (int in
);
274 extern int unpack (int in
, int out
);
277 extern int unlzh (int in
, int out
);
280 _Noreturn
extern void finish_up_gzip (int);
281 _Noreturn
extern void abort_gzip (void);
284 extern off_t
deflate (int pack_level
);
287 extern void ct_init (ush
*attr
, int *method
);
288 extern int ct_tally (int dist
, int lc
);
289 extern off_t
flush_block (char *buf
, ulg stored_len
, int pad
, int eof
);
293 extern unsigned short bi_buf
;
296 extern void bi_init (file_t zipfile
);
297 extern void send_bits (int value
, int length
);
298 extern unsigned bi_reverse (unsigned value
, int length
) _GL_ATTRIBUTE_CONST
;
299 extern void bi_windup (void);
300 extern void copy_block (char *buf
, unsigned len
, int header
);
301 extern int (*read_buf
) (char *buf
, unsigned size
);
304 extern int copy (int in
, int out
);
305 extern ulg
updcrc (const uch
*s
, unsigned n
);
306 extern ulg
getcrc (void) _GL_ATTRIBUTE_PURE
;
307 extern void setcrc (ulg c
);
308 extern void clear_bufs (void);
309 extern int fill_inbuf (int eof_ok
);
310 extern void flush_outbuf (void);
311 extern void flush_window (void);
312 extern void write_buf (int fd
, voidp buf
, unsigned cnt
);
313 extern int read_buffer (int fd
, voidp buf
, unsigned int cnt
);
314 extern char *strlwr (char *s
);
315 extern char *gzip_base_name (char *fname
) _GL_ATTRIBUTE_PURE
;
316 extern int xunlink (char *fname
);
317 extern void make_simple_name (char *name
);
318 extern char *add_envopt (int *argcp
, char ***argvp
, char const *env
);
319 _Noreturn
extern void gzip_error (char const *m
);
320 _Noreturn
extern void xalloc_die (void);
321 extern void warning (char const *m
);
322 _Noreturn
extern void read_error (void);
323 _Noreturn
extern void write_error (void);
324 extern void display_ratio (off_t num
, off_t den
, FILE *file
);
327 extern int inflate (void);
331 extern int dfltcc_deflate (int pack_level
);
332 extern int dfltcc_inflate (void);