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