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, see <https://www.gnu.org/licenses/>. */
28 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8) || __STRICT_ANSI__
29 # define __attribute__(x)
33 /* I don't like nested includes, but the following headers are used
37 #include <sys/types.h> /* for off_t */
40 #define memzero(s, n) memset ((voidp)(s), 0, (n))
42 typedef unsigned char uch
;
43 typedef unsigned short ush
;
44 typedef unsigned long ulg
;
46 /* Return codes from gzip */
51 /* Compression methods (see algorithm.doc) */
56 /* methods 4 to 7 reserved */
59 extern int method
; /* compression method */
61 /* To save memory for 16 bit systems, some arrays are overlaid between
62 * the various modules:
63 * deflate: prev+head window d_buf l_buf outbuf
64 * unlzw: tab_prefix tab_suffix stack inbuf outbuf
65 * inflate: window inbuf
66 * unpack: window inbuf prefix_len
67 * unlzh: left+right window c_table inbuf c_len
68 * For compression, input is done in window[]. For decompression, output
69 * is done in window except for unlzw.
74 # define INBUFSIZ 0x2000 /* input buffer size */
76 # define INBUFSIZ 0x40000 /* input buffer size */
79 #define INBUF_EXTRA 64 /* required by unlzw() */
83 # define OUTBUFSIZ 8192 /* output buffer size */
85 # define OUTBUFSIZ 0x40000 /* output buffer size */
88 #define OUTBUF_EXTRA 2048 /* required by unlzw() */
92 # define DIST_BUFSIZE 0x2000 /* buffer for distances, see trees.c */
94 # define DIST_BUFSIZE 0x8000 /* buffer for distances, see trees.c */
99 # define EXTERN(type, array) extern type * near array
100 # define DECLARE(type, array, size) type * near array
101 # define ALLOC(type, array, size) { \
102 array = (type*)fcalloc((size_t)(((size)+1L)/2), 2*sizeof(type)); \
103 if (!array) xalloc_die (); \
105 # define FREE(array) {if (array != NULL) fcfree(array), array=NULL;}
107 # define EXTERN(type, array) extern type array[]
108 # define DECLARE(type, array, size) type array[size]
109 # define ALLOC(type, array, size)
113 EXTERN(uch
, inbuf
); /* input buffer */
114 EXTERN(uch
, outbuf
); /* output buffer */
115 EXTERN(ush
, d_buf
); /* buffer for distances, see trees.c */
116 EXTERN(uch
, window
); /* Sliding window and suffix table (unlzw) */
117 #define tab_suffix window
119 # define tab_prefix prev /* hash link (see deflate.c) */
120 # define head (prev+WSIZE) /* hash head (see deflate.c) */
121 EXTERN(ush
, tab_prefix
); /* prefix code (see unlzw.c) */
123 # define tab_prefix0 prev
124 # define head tab_prefix1
125 EXTERN(ush
, tab_prefix0
); /* prefix for even codes */
126 EXTERN(ush
, tab_prefix1
); /* prefix for odd codes */
129 extern unsigned insize
; /* valid bytes in inbuf */
130 extern unsigned inptr
; /* index of next byte to be processed in inbuf */
131 extern unsigned outcnt
; /* bytes in output buffer */
132 extern int rsync
; /* deflate into rsyncable chunks */
134 extern off_t bytes_in
; /* number of input bytes */
135 extern off_t bytes_out
; /* number of output bytes */
136 extern off_t header_bytes
;/* number of bytes in gzip header */
138 extern int ifd
; /* input file descriptor */
139 extern int ofd
; /* output file descriptor */
140 extern char ifname
[]; /* input file name or "stdin" */
141 extern char ofname
[]; /* output file name or "stdout" */
142 extern char *program_name
; /* program name */
144 extern struct timespec time_stamp
; /* original timestamp (modification time) */
145 extern off_t ifile_size
; /* input file size, -1 for devices (debug only) */
147 typedef int file_t
; /* Do not use stdio */
148 #define NO_FILE (-1) /* in memory compression */
151 #define PACK_MAGIC "\037\036" /* Magic header for packed files */
152 #define GZIP_MAGIC "\037\213" /* Magic header for gzip files, 1F 8B */
153 #define OLD_GZIP_MAGIC "\037\236" /* Magic header for gzip 0.5 = freeze 1.x */
154 #define LZH_MAGIC "\037\240" /* Magic header for SCO LZH Compress files*/
155 #define PKZIP_MAGIC "\120\113\003\004" /* Magic header for pkzip files */
158 #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
159 #define HEADER_CRC 0x02 /* bit 1 set: CRC16 for the gzip header */
160 #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
161 #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
162 #define COMMENT 0x10 /* bit 4 set: file comment present */
163 #define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
164 #define RESERVED 0xC0 /* bit 6,7: reserved */
166 /* internal file attribute */
167 #define UNKNOWN 0xffff
172 # define WSIZE 0x8000 /* window size--must be a power of two, and */
173 #endif /* at least 32K for zip's deflate method */
176 #define MAX_MATCH 258
177 /* The minimum and maximum match lengths */
179 #define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
180 /* Minimum amount of lookahead, except at the end of the input file.
181 * See deflate.c for comments about the MIN_MATCH+1.
184 #define MAX_DIST (WSIZE-MIN_LOOKAHEAD)
185 /* In order to simplify the code, particularly on 16 bit machines, match
186 * distances are limited to MAX_DIST instead of WSIZE.
189 extern int exit_code
; /* program exit code */
190 extern int quiet
; /* be quiet (-q) */
191 extern int level
; /* compression level */
192 extern int test
; /* check .z file integrity */
193 extern int to_stdout
; /* output to stdout (-c) */
194 extern int save_orig_name
; /* set if original name must be saved */
196 #define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf(0))
197 #define try_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf(1))
199 /* put_byte is used for the compressed output, put_ubyte for the
200 * uncompressed output. However unlzw() uses window for its
201 * suffix table instead of its output buffer, so it does not use put_ubyte
202 * (to be cleaned up).
204 #define put_byte(c) {outbuf[outcnt++]=(uch)(c); if (outcnt==OUTBUFSIZ)\
206 #define put_ubyte(c) {window[outcnt++]=(uch)(c); if (outcnt==WSIZE)\
209 /* Output a 16 bit value, lsb first */
210 #define put_short(w) \
211 { if (outcnt < OUTBUFSIZ-2) { \
212 outbuf[outcnt++] = (uch) ((w) & 0xff); \
213 outbuf[outcnt++] = (uch) ((ush)(w) >> 8); \
215 put_byte((uch)((w) & 0xff)); \
216 put_byte((uch)((ush)(w) >> 8)); \
220 /* Output a 32 bit value to the bit stream, lsb first */
221 #define put_long(n) { \
222 put_short((n) & 0xffff); \
223 put_short(((ulg)(n)) >> 16); \
226 #define seekable() 0 /* force sequential output */
227 #define translate_eol 0 /* no option -a yet */
229 #define tolow(c) (isupper (c) ? tolower (c) : (c)) /* force to lower case */
231 /* Macros for getting two-byte and four-byte header values */
232 #define SH(p) ((ush)(uch)((p)[0]) | ((ush)(uch)((p)[1]) << 8))
233 #define LG(p) ((ulg)(SH(p)) | ((ulg)(SH((p)+2)) << 16))
235 /* Diagnostic functions */
237 extern int verbose
; /* be verbose (-v) */
238 # define Assert(cond,msg) {if (!(cond)) gzip_error (msg);}
239 # define Trace(x) fprintf x
240 # define Tracev(x) {if (verbose) fprintf x ;}
241 # define Tracevv(x) {if (verbose>1) fprintf x ;}
242 # define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
243 # define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
245 # define Assert(cond,msg)
250 # define Tracecv(c,x)
258 if (exit_code == OK) \
259 exit_code = WARNING; \
264 extern int zip (int in
, int out
);
265 extern int file_read (char *buf
, unsigned size
);
268 extern ulg unzip_crc
;
269 extern int unzip (int in
, int out
);
270 extern int check_zipfile (int in
);
273 extern int unpack (int in
, int out
);
276 extern int unlzh (int in
, int out
);
279 _Noreturn
extern void finish_up_gzip (int);
280 _Noreturn
extern void abort_gzip (void);
283 extern off_t
gzip_deflate (int pack_level
);
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
);
292 extern unsigned short bi_buf
;
295 extern void bi_init (file_t zipfile
);
296 extern void send_bits (int value
, int length
);
297 extern unsigned bi_reverse (unsigned value
, int length
) _GL_ATTRIBUTE_CONST
;
298 extern void bi_windup (void);
299 extern void copy_block (char *buf
, unsigned len
, int header
);
300 extern int (*read_buf
) (char *buf
, unsigned size
);
303 extern int copy (int in
, int out
);
304 extern ulg
updcrc (const uch
*s
, unsigned n
);
305 extern ulg
getcrc (void) _GL_ATTRIBUTE_PURE
;
306 extern void setcrc (ulg c
);
307 extern void clear_bufs (void);
308 extern int fill_inbuf (int eof_ok
);
309 extern void flush_outbuf (void);
310 extern void flush_window (void);
311 extern void write_buf (int fd
, voidp buf
, unsigned cnt
);
312 extern int read_buffer (int fd
, voidp buf
, unsigned int cnt
);
313 extern char *strlwr (char *s
);
314 extern char *gzip_base_name (char *fname
) _GL_ATTRIBUTE_PURE
;
315 extern int xunlink (char *fname
);
316 extern void make_simple_name (char *name
);
317 extern char *add_envopt (int *argcp
, char ***argvp
, char const *env
);
318 _Noreturn
extern void gzip_error (char const *m
);
319 _Noreturn
extern void xalloc_die (void);
320 extern void warning (char const *m
);
321 _Noreturn
extern void read_error (void);
322 _Noreturn
extern void write_error (void);
323 extern void display_ratio (off_t num
, off_t den
, FILE *file
);
326 extern int gzip_inflate (void);
330 extern int dfltcc_deflate (int pack_level
);
331 extern int dfltcc_inflate (void);