cannam@128
|
1 /* gun.c -- simple gunzip to give an example of the use of inflateBack()
|
cannam@128
|
2 * Copyright (C) 2003, 2005, 2008, 2010, 2012 Mark Adler
|
cannam@128
|
3 * For conditions of distribution and use, see copyright notice in zlib.h
|
cannam@128
|
4 Version 1.7 12 August 2012 Mark Adler */
|
cannam@128
|
5
|
cannam@128
|
6 /* Version history:
|
cannam@128
|
7 1.0 16 Feb 2003 First version for testing of inflateBack()
|
cannam@128
|
8 1.1 21 Feb 2005 Decompress concatenated gzip streams
|
cannam@128
|
9 Remove use of "this" variable (C++ keyword)
|
cannam@128
|
10 Fix return value for in()
|
cannam@128
|
11 Improve allocation failure checking
|
cannam@128
|
12 Add typecasting for void * structures
|
cannam@128
|
13 Add -h option for command version and usage
|
cannam@128
|
14 Add a bunch of comments
|
cannam@128
|
15 1.2 20 Mar 2005 Add Unix compress (LZW) decompression
|
cannam@128
|
16 Copy file attributes from input file to output file
|
cannam@128
|
17 1.3 12 Jun 2005 Add casts for error messages [Oberhumer]
|
cannam@128
|
18 1.4 8 Dec 2006 LZW decompression speed improvements
|
cannam@128
|
19 1.5 9 Feb 2008 Avoid warning in latest version of gcc
|
cannam@128
|
20 1.6 17 Jan 2010 Avoid signed/unsigned comparison warnings
|
cannam@128
|
21 1.7 12 Aug 2012 Update for z_const usage in zlib 1.2.8
|
cannam@128
|
22 */
|
cannam@128
|
23
|
cannam@128
|
24 /*
|
cannam@128
|
25 gun [ -t ] [ name ... ]
|
cannam@128
|
26
|
cannam@128
|
27 decompresses the data in the named gzip files. If no arguments are given,
|
cannam@128
|
28 gun will decompress from stdin to stdout. The names must end in .gz, -gz,
|
cannam@128
|
29 .z, -z, _z, or .Z. The uncompressed data will be written to a file name
|
cannam@128
|
30 with the suffix stripped. On success, the original file is deleted. On
|
cannam@128
|
31 failure, the output file is deleted. For most failures, the command will
|
cannam@128
|
32 continue to process the remaining names on the command line. A memory
|
cannam@128
|
33 allocation failure will abort the command. If -t is specified, then the
|
cannam@128
|
34 listed files or stdin will be tested as gzip files for integrity (without
|
cannam@128
|
35 checking for a proper suffix), no output will be written, and no files
|
cannam@128
|
36 will be deleted.
|
cannam@128
|
37
|
cannam@128
|
38 Like gzip, gun allows concatenated gzip streams and will decompress them,
|
cannam@128
|
39 writing all of the uncompressed data to the output. Unlike gzip, gun allows
|
cannam@128
|
40 an empty file on input, and will produce no error writing an empty output
|
cannam@128
|
41 file.
|
cannam@128
|
42
|
cannam@128
|
43 gun will also decompress files made by Unix compress, which uses LZW
|
cannam@128
|
44 compression. These files are automatically detected by virtue of their
|
cannam@128
|
45 magic header bytes. Since the end of Unix compress stream is marked by the
|
cannam@128
|
46 end-of-file, they cannot be concantenated. If a Unix compress stream is
|
cannam@128
|
47 encountered in an input file, it is the last stream in that file.
|
cannam@128
|
48
|
cannam@128
|
49 Like gunzip and uncompress, the file attributes of the orignal compressed
|
cannam@128
|
50 file are maintained in the final uncompressed file, to the extent that the
|
cannam@128
|
51 user permissions allow it.
|
cannam@128
|
52
|
cannam@128
|
53 On my Mac OS X PowerPC G4, gun is almost twice as fast as gunzip (version
|
cannam@128
|
54 1.2.4) is on the same file, when gun is linked with zlib 1.2.2. Also the
|
cannam@128
|
55 LZW decompression provided by gun is about twice as fast as the standard
|
cannam@128
|
56 Unix uncompress command.
|
cannam@128
|
57 */
|
cannam@128
|
58
|
cannam@128
|
59 /* external functions and related types and constants */
|
cannam@128
|
60 #include <stdio.h> /* fprintf() */
|
cannam@128
|
61 #include <stdlib.h> /* malloc(), free() */
|
cannam@128
|
62 #include <string.h> /* strerror(), strcmp(), strlen(), memcpy() */
|
cannam@128
|
63 #include <errno.h> /* errno */
|
cannam@128
|
64 #include <fcntl.h> /* open() */
|
cannam@128
|
65 #include <unistd.h> /* read(), write(), close(), chown(), unlink() */
|
cannam@128
|
66 #include <sys/types.h>
|
cannam@128
|
67 #include <sys/stat.h> /* stat(), chmod() */
|
cannam@128
|
68 #include <utime.h> /* utime() */
|
cannam@128
|
69 #include "zlib.h" /* inflateBackInit(), inflateBack(), */
|
cannam@128
|
70 /* inflateBackEnd(), crc32() */
|
cannam@128
|
71
|
cannam@128
|
72 /* function declaration */
|
cannam@128
|
73 #define local static
|
cannam@128
|
74
|
cannam@128
|
75 /* buffer constants */
|
cannam@128
|
76 #define SIZE 32768U /* input and output buffer sizes */
|
cannam@128
|
77 #define PIECE 16384 /* limits i/o chunks for 16-bit int case */
|
cannam@128
|
78
|
cannam@128
|
79 /* structure for infback() to pass to input function in() -- it maintains the
|
cannam@128
|
80 input file and a buffer of size SIZE */
|
cannam@128
|
81 struct ind {
|
cannam@128
|
82 int infile;
|
cannam@128
|
83 unsigned char *inbuf;
|
cannam@128
|
84 };
|
cannam@128
|
85
|
cannam@128
|
86 /* Load input buffer, assumed to be empty, and return bytes loaded and a
|
cannam@128
|
87 pointer to them. read() is called until the buffer is full, or until it
|
cannam@128
|
88 returns end-of-file or error. Return 0 on error. */
|
cannam@128
|
89 local unsigned in(void *in_desc, z_const unsigned char **buf)
|
cannam@128
|
90 {
|
cannam@128
|
91 int ret;
|
cannam@128
|
92 unsigned len;
|
cannam@128
|
93 unsigned char *next;
|
cannam@128
|
94 struct ind *me = (struct ind *)in_desc;
|
cannam@128
|
95
|
cannam@128
|
96 next = me->inbuf;
|
cannam@128
|
97 *buf = next;
|
cannam@128
|
98 len = 0;
|
cannam@128
|
99 do {
|
cannam@128
|
100 ret = PIECE;
|
cannam@128
|
101 if ((unsigned)ret > SIZE - len)
|
cannam@128
|
102 ret = (int)(SIZE - len);
|
cannam@128
|
103 ret = (int)read(me->infile, next, ret);
|
cannam@128
|
104 if (ret == -1) {
|
cannam@128
|
105 len = 0;
|
cannam@128
|
106 break;
|
cannam@128
|
107 }
|
cannam@128
|
108 next += ret;
|
cannam@128
|
109 len += ret;
|
cannam@128
|
110 } while (ret != 0 && len < SIZE);
|
cannam@128
|
111 return len;
|
cannam@128
|
112 }
|
cannam@128
|
113
|
cannam@128
|
114 /* structure for infback() to pass to output function out() -- it maintains the
|
cannam@128
|
115 output file, a running CRC-32 check on the output and the total number of
|
cannam@128
|
116 bytes output, both for checking against the gzip trailer. (The length in
|
cannam@128
|
117 the gzip trailer is stored modulo 2^32, so it's ok if a long is 32 bits and
|
cannam@128
|
118 the output is greater than 4 GB.) */
|
cannam@128
|
119 struct outd {
|
cannam@128
|
120 int outfile;
|
cannam@128
|
121 int check; /* true if checking crc and total */
|
cannam@128
|
122 unsigned long crc;
|
cannam@128
|
123 unsigned long total;
|
cannam@128
|
124 };
|
cannam@128
|
125
|
cannam@128
|
126 /* Write output buffer and update the CRC-32 and total bytes written. write()
|
cannam@128
|
127 is called until all of the output is written or an error is encountered.
|
cannam@128
|
128 On success out() returns 0. For a write failure, out() returns 1. If the
|
cannam@128
|
129 output file descriptor is -1, then nothing is written.
|
cannam@128
|
130 */
|
cannam@128
|
131 local int out(void *out_desc, unsigned char *buf, unsigned len)
|
cannam@128
|
132 {
|
cannam@128
|
133 int ret;
|
cannam@128
|
134 struct outd *me = (struct outd *)out_desc;
|
cannam@128
|
135
|
cannam@128
|
136 if (me->check) {
|
cannam@128
|
137 me->crc = crc32(me->crc, buf, len);
|
cannam@128
|
138 me->total += len;
|
cannam@128
|
139 }
|
cannam@128
|
140 if (me->outfile != -1)
|
cannam@128
|
141 do {
|
cannam@128
|
142 ret = PIECE;
|
cannam@128
|
143 if ((unsigned)ret > len)
|
cannam@128
|
144 ret = (int)len;
|
cannam@128
|
145 ret = (int)write(me->outfile, buf, ret);
|
cannam@128
|
146 if (ret == -1)
|
cannam@128
|
147 return 1;
|
cannam@128
|
148 buf += ret;
|
cannam@128
|
149 len -= ret;
|
cannam@128
|
150 } while (len != 0);
|
cannam@128
|
151 return 0;
|
cannam@128
|
152 }
|
cannam@128
|
153
|
cannam@128
|
154 /* next input byte macro for use inside lunpipe() and gunpipe() */
|
cannam@128
|
155 #define NEXT() (have ? 0 : (have = in(indp, &next)), \
|
cannam@128
|
156 last = have ? (have--, (int)(*next++)) : -1)
|
cannam@128
|
157
|
cannam@128
|
158 /* memory for gunpipe() and lunpipe() --
|
cannam@128
|
159 the first 256 entries of prefix[] and suffix[] are never used, could
|
cannam@128
|
160 have offset the index, but it's faster to waste the memory */
|
cannam@128
|
161 unsigned char inbuf[SIZE]; /* input buffer */
|
cannam@128
|
162 unsigned char outbuf[SIZE]; /* output buffer */
|
cannam@128
|
163 unsigned short prefix[65536]; /* index to LZW prefix string */
|
cannam@128
|
164 unsigned char suffix[65536]; /* one-character LZW suffix */
|
cannam@128
|
165 unsigned char match[65280 + 2]; /* buffer for reversed match or gzip
|
cannam@128
|
166 32K sliding window */
|
cannam@128
|
167
|
cannam@128
|
168 /* throw out what's left in the current bits byte buffer (this is a vestigial
|
cannam@128
|
169 aspect of the compressed data format derived from an implementation that
|
cannam@128
|
170 made use of a special VAX machine instruction!) */
|
cannam@128
|
171 #define FLUSHCODE() \
|
cannam@128
|
172 do { \
|
cannam@128
|
173 left = 0; \
|
cannam@128
|
174 rem = 0; \
|
cannam@128
|
175 if (chunk > have) { \
|
cannam@128
|
176 chunk -= have; \
|
cannam@128
|
177 have = 0; \
|
cannam@128
|
178 if (NEXT() == -1) \
|
cannam@128
|
179 break; \
|
cannam@128
|
180 chunk--; \
|
cannam@128
|
181 if (chunk > have) { \
|
cannam@128
|
182 chunk = have = 0; \
|
cannam@128
|
183 break; \
|
cannam@128
|
184 } \
|
cannam@128
|
185 } \
|
cannam@128
|
186 have -= chunk; \
|
cannam@128
|
187 next += chunk; \
|
cannam@128
|
188 chunk = 0; \
|
cannam@128
|
189 } while (0)
|
cannam@128
|
190
|
cannam@128
|
191 /* Decompress a compress (LZW) file from indp to outfile. The compress magic
|
cannam@128
|
192 header (two bytes) has already been read and verified. There are have bytes
|
cannam@128
|
193 of buffered input at next. strm is used for passing error information back
|
cannam@128
|
194 to gunpipe().
|
cannam@128
|
195
|
cannam@128
|
196 lunpipe() will return Z_OK on success, Z_BUF_ERROR for an unexpected end of
|
cannam@128
|
197 file, read error, or write error (a write error indicated by strm->next_in
|
cannam@128
|
198 not equal to Z_NULL), or Z_DATA_ERROR for invalid input.
|
cannam@128
|
199 */
|
cannam@128
|
200 local int lunpipe(unsigned have, z_const unsigned char *next, struct ind *indp,
|
cannam@128
|
201 int outfile, z_stream *strm)
|
cannam@128
|
202 {
|
cannam@128
|
203 int last; /* last byte read by NEXT(), or -1 if EOF */
|
cannam@128
|
204 unsigned chunk; /* bytes left in current chunk */
|
cannam@128
|
205 int left; /* bits left in rem */
|
cannam@128
|
206 unsigned rem; /* unused bits from input */
|
cannam@128
|
207 int bits; /* current bits per code */
|
cannam@128
|
208 unsigned code; /* code, table traversal index */
|
cannam@128
|
209 unsigned mask; /* mask for current bits codes */
|
cannam@128
|
210 int max; /* maximum bits per code for this stream */
|
cannam@128
|
211 unsigned flags; /* compress flags, then block compress flag */
|
cannam@128
|
212 unsigned end; /* last valid entry in prefix/suffix tables */
|
cannam@128
|
213 unsigned temp; /* current code */
|
cannam@128
|
214 unsigned prev; /* previous code */
|
cannam@128
|
215 unsigned final; /* last character written for previous code */
|
cannam@128
|
216 unsigned stack; /* next position for reversed string */
|
cannam@128
|
217 unsigned outcnt; /* bytes in output buffer */
|
cannam@128
|
218 struct outd outd; /* output structure */
|
cannam@128
|
219 unsigned char *p;
|
cannam@128
|
220
|
cannam@128
|
221 /* set up output */
|
cannam@128
|
222 outd.outfile = outfile;
|
cannam@128
|
223 outd.check = 0;
|
cannam@128
|
224
|
cannam@128
|
225 /* process remainder of compress header -- a flags byte */
|
cannam@128
|
226 flags = NEXT();
|
cannam@128
|
227 if (last == -1)
|
cannam@128
|
228 return Z_BUF_ERROR;
|
cannam@128
|
229 if (flags & 0x60) {
|
cannam@128
|
230 strm->msg = (char *)"unknown lzw flags set";
|
cannam@128
|
231 return Z_DATA_ERROR;
|
cannam@128
|
232 }
|
cannam@128
|
233 max = flags & 0x1f;
|
cannam@128
|
234 if (max < 9 || max > 16) {
|
cannam@128
|
235 strm->msg = (char *)"lzw bits out of range";
|
cannam@128
|
236 return Z_DATA_ERROR;
|
cannam@128
|
237 }
|
cannam@128
|
238 if (max == 9) /* 9 doesn't really mean 9 */
|
cannam@128
|
239 max = 10;
|
cannam@128
|
240 flags &= 0x80; /* true if block compress */
|
cannam@128
|
241
|
cannam@128
|
242 /* clear table */
|
cannam@128
|
243 bits = 9;
|
cannam@128
|
244 mask = 0x1ff;
|
cannam@128
|
245 end = flags ? 256 : 255;
|
cannam@128
|
246
|
cannam@128
|
247 /* set up: get first 9-bit code, which is the first decompressed byte, but
|
cannam@128
|
248 don't create a table entry until the next code */
|
cannam@128
|
249 if (NEXT() == -1) /* no compressed data is ok */
|
cannam@128
|
250 return Z_OK;
|
cannam@128
|
251 final = prev = (unsigned)last; /* low 8 bits of code */
|
cannam@128
|
252 if (NEXT() == -1) /* missing a bit */
|
cannam@128
|
253 return Z_BUF_ERROR;
|
cannam@128
|
254 if (last & 1) { /* code must be < 256 */
|
cannam@128
|
255 strm->msg = (char *)"invalid lzw code";
|
cannam@128
|
256 return Z_DATA_ERROR;
|
cannam@128
|
257 }
|
cannam@128
|
258 rem = (unsigned)last >> 1; /* remaining 7 bits */
|
cannam@128
|
259 left = 7;
|
cannam@128
|
260 chunk = bits - 2; /* 7 bytes left in this chunk */
|
cannam@128
|
261 outbuf[0] = (unsigned char)final; /* write first decompressed byte */
|
cannam@128
|
262 outcnt = 1;
|
cannam@128
|
263
|
cannam@128
|
264 /* decode codes */
|
cannam@128
|
265 stack = 0;
|
cannam@128
|
266 for (;;) {
|
cannam@128
|
267 /* if the table will be full after this, increment the code size */
|
cannam@128
|
268 if (end >= mask && bits < max) {
|
cannam@128
|
269 FLUSHCODE();
|
cannam@128
|
270 bits++;
|
cannam@128
|
271 mask <<= 1;
|
cannam@128
|
272 mask++;
|
cannam@128
|
273 }
|
cannam@128
|
274
|
cannam@128
|
275 /* get a code of length bits */
|
cannam@128
|
276 if (chunk == 0) /* decrement chunk modulo bits */
|
cannam@128
|
277 chunk = bits;
|
cannam@128
|
278 code = rem; /* low bits of code */
|
cannam@128
|
279 if (NEXT() == -1) { /* EOF is end of compressed data */
|
cannam@128
|
280 /* write remaining buffered output */
|
cannam@128
|
281 if (outcnt && out(&outd, outbuf, outcnt)) {
|
cannam@128
|
282 strm->next_in = outbuf; /* signal write error */
|
cannam@128
|
283 return Z_BUF_ERROR;
|
cannam@128
|
284 }
|
cannam@128
|
285 return Z_OK;
|
cannam@128
|
286 }
|
cannam@128
|
287 code += (unsigned)last << left; /* middle (or high) bits of code */
|
cannam@128
|
288 left += 8;
|
cannam@128
|
289 chunk--;
|
cannam@128
|
290 if (bits > left) { /* need more bits */
|
cannam@128
|
291 if (NEXT() == -1) /* can't end in middle of code */
|
cannam@128
|
292 return Z_BUF_ERROR;
|
cannam@128
|
293 code += (unsigned)last << left; /* high bits of code */
|
cannam@128
|
294 left += 8;
|
cannam@128
|
295 chunk--;
|
cannam@128
|
296 }
|
cannam@128
|
297 code &= mask; /* mask to current code length */
|
cannam@128
|
298 left -= bits; /* number of unused bits */
|
cannam@128
|
299 rem = (unsigned)last >> (8 - left); /* unused bits from last byte */
|
cannam@128
|
300
|
cannam@128
|
301 /* process clear code (256) */
|
cannam@128
|
302 if (code == 256 && flags) {
|
cannam@128
|
303 FLUSHCODE();
|
cannam@128
|
304 bits = 9; /* initialize bits and mask */
|
cannam@128
|
305 mask = 0x1ff;
|
cannam@128
|
306 end = 255; /* empty table */
|
cannam@128
|
307 continue; /* get next code */
|
cannam@128
|
308 }
|
cannam@128
|
309
|
cannam@128
|
310 /* special code to reuse last match */
|
cannam@128
|
311 temp = code; /* save the current code */
|
cannam@128
|
312 if (code > end) {
|
cannam@128
|
313 /* Be picky on the allowed code here, and make sure that the code
|
cannam@128
|
314 we drop through (prev) will be a valid index so that random
|
cannam@128
|
315 input does not cause an exception. The code != end + 1 check is
|
cannam@128
|
316 empirically derived, and not checked in the original uncompress
|
cannam@128
|
317 code. If this ever causes a problem, that check could be safely
|
cannam@128
|
318 removed. Leaving this check in greatly improves gun's ability
|
cannam@128
|
319 to detect random or corrupted input after a compress header.
|
cannam@128
|
320 In any case, the prev > end check must be retained. */
|
cannam@128
|
321 if (code != end + 1 || prev > end) {
|
cannam@128
|
322 strm->msg = (char *)"invalid lzw code";
|
cannam@128
|
323 return Z_DATA_ERROR;
|
cannam@128
|
324 }
|
cannam@128
|
325 match[stack++] = (unsigned char)final;
|
cannam@128
|
326 code = prev;
|
cannam@128
|
327 }
|
cannam@128
|
328
|
cannam@128
|
329 /* walk through linked list to generate output in reverse order */
|
cannam@128
|
330 p = match + stack;
|
cannam@128
|
331 while (code >= 256) {
|
cannam@128
|
332 *p++ = suffix[code];
|
cannam@128
|
333 code = prefix[code];
|
cannam@128
|
334 }
|
cannam@128
|
335 stack = p - match;
|
cannam@128
|
336 match[stack++] = (unsigned char)code;
|
cannam@128
|
337 final = code;
|
cannam@128
|
338
|
cannam@128
|
339 /* link new table entry */
|
cannam@128
|
340 if (end < mask) {
|
cannam@128
|
341 end++;
|
cannam@128
|
342 prefix[end] = (unsigned short)prev;
|
cannam@128
|
343 suffix[end] = (unsigned char)final;
|
cannam@128
|
344 }
|
cannam@128
|
345
|
cannam@128
|
346 /* set previous code for next iteration */
|
cannam@128
|
347 prev = temp;
|
cannam@128
|
348
|
cannam@128
|
349 /* write output in forward order */
|
cannam@128
|
350 while (stack > SIZE - outcnt) {
|
cannam@128
|
351 while (outcnt < SIZE)
|
cannam@128
|
352 outbuf[outcnt++] = match[--stack];
|
cannam@128
|
353 if (out(&outd, outbuf, outcnt)) {
|
cannam@128
|
354 strm->next_in = outbuf; /* signal write error */
|
cannam@128
|
355 return Z_BUF_ERROR;
|
cannam@128
|
356 }
|
cannam@128
|
357 outcnt = 0;
|
cannam@128
|
358 }
|
cannam@128
|
359 p = match + stack;
|
cannam@128
|
360 do {
|
cannam@128
|
361 outbuf[outcnt++] = *--p;
|
cannam@128
|
362 } while (p > match);
|
cannam@128
|
363 stack = 0;
|
cannam@128
|
364
|
cannam@128
|
365 /* loop for next code with final and prev as the last match, rem and
|
cannam@128
|
366 left provide the first 0..7 bits of the next code, end is the last
|
cannam@128
|
367 valid table entry */
|
cannam@128
|
368 }
|
cannam@128
|
369 }
|
cannam@128
|
370
|
cannam@128
|
371 /* Decompress a gzip file from infile to outfile. strm is assumed to have been
|
cannam@128
|
372 successfully initialized with inflateBackInit(). The input file may consist
|
cannam@128
|
373 of a series of gzip streams, in which case all of them will be decompressed
|
cannam@128
|
374 to the output file. If outfile is -1, then the gzip stream(s) integrity is
|
cannam@128
|
375 checked and nothing is written.
|
cannam@128
|
376
|
cannam@128
|
377 The return value is a zlib error code: Z_MEM_ERROR if out of memory,
|
cannam@128
|
378 Z_DATA_ERROR if the header or the compressed data is invalid, or if the
|
cannam@128
|
379 trailer CRC-32 check or length doesn't match, Z_BUF_ERROR if the input ends
|
cannam@128
|
380 prematurely or a write error occurs, or Z_ERRNO if junk (not a another gzip
|
cannam@128
|
381 stream) follows a valid gzip stream.
|
cannam@128
|
382 */
|
cannam@128
|
383 local int gunpipe(z_stream *strm, int infile, int outfile)
|
cannam@128
|
384 {
|
cannam@128
|
385 int ret, first, last;
|
cannam@128
|
386 unsigned have, flags, len;
|
cannam@128
|
387 z_const unsigned char *next = NULL;
|
cannam@128
|
388 struct ind ind, *indp;
|
cannam@128
|
389 struct outd outd;
|
cannam@128
|
390
|
cannam@128
|
391 /* setup input buffer */
|
cannam@128
|
392 ind.infile = infile;
|
cannam@128
|
393 ind.inbuf = inbuf;
|
cannam@128
|
394 indp = &ind;
|
cannam@128
|
395
|
cannam@128
|
396 /* decompress concatenated gzip streams */
|
cannam@128
|
397 have = 0; /* no input data read in yet */
|
cannam@128
|
398 first = 1; /* looking for first gzip header */
|
cannam@128
|
399 strm->next_in = Z_NULL; /* so Z_BUF_ERROR means EOF */
|
cannam@128
|
400 for (;;) {
|
cannam@128
|
401 /* look for the two magic header bytes for a gzip stream */
|
cannam@128
|
402 if (NEXT() == -1) {
|
cannam@128
|
403 ret = Z_OK;
|
cannam@128
|
404 break; /* empty gzip stream is ok */
|
cannam@128
|
405 }
|
cannam@128
|
406 if (last != 31 || (NEXT() != 139 && last != 157)) {
|
cannam@128
|
407 strm->msg = (char *)"incorrect header check";
|
cannam@128
|
408 ret = first ? Z_DATA_ERROR : Z_ERRNO;
|
cannam@128
|
409 break; /* not a gzip or compress header */
|
cannam@128
|
410 }
|
cannam@128
|
411 first = 0; /* next non-header is junk */
|
cannam@128
|
412
|
cannam@128
|
413 /* process a compress (LZW) file -- can't be concatenated after this */
|
cannam@128
|
414 if (last == 157) {
|
cannam@128
|
415 ret = lunpipe(have, next, indp, outfile, strm);
|
cannam@128
|
416 break;
|
cannam@128
|
417 }
|
cannam@128
|
418
|
cannam@128
|
419 /* process remainder of gzip header */
|
cannam@128
|
420 ret = Z_BUF_ERROR;
|
cannam@128
|
421 if (NEXT() != 8) { /* only deflate method allowed */
|
cannam@128
|
422 if (last == -1) break;
|
cannam@128
|
423 strm->msg = (char *)"unknown compression method";
|
cannam@128
|
424 ret = Z_DATA_ERROR;
|
cannam@128
|
425 break;
|
cannam@128
|
426 }
|
cannam@128
|
427 flags = NEXT(); /* header flags */
|
cannam@128
|
428 NEXT(); /* discard mod time, xflgs, os */
|
cannam@128
|
429 NEXT();
|
cannam@128
|
430 NEXT();
|
cannam@128
|
431 NEXT();
|
cannam@128
|
432 NEXT();
|
cannam@128
|
433 NEXT();
|
cannam@128
|
434 if (last == -1) break;
|
cannam@128
|
435 if (flags & 0xe0) {
|
cannam@128
|
436 strm->msg = (char *)"unknown header flags set";
|
cannam@128
|
437 ret = Z_DATA_ERROR;
|
cannam@128
|
438 break;
|
cannam@128
|
439 }
|
cannam@128
|
440 if (flags & 4) { /* extra field */
|
cannam@128
|
441 len = NEXT();
|
cannam@128
|
442 len += (unsigned)(NEXT()) << 8;
|
cannam@128
|
443 if (last == -1) break;
|
cannam@128
|
444 while (len > have) {
|
cannam@128
|
445 len -= have;
|
cannam@128
|
446 have = 0;
|
cannam@128
|
447 if (NEXT() == -1) break;
|
cannam@128
|
448 len--;
|
cannam@128
|
449 }
|
cannam@128
|
450 if (last == -1) break;
|
cannam@128
|
451 have -= len;
|
cannam@128
|
452 next += len;
|
cannam@128
|
453 }
|
cannam@128
|
454 if (flags & 8) /* file name */
|
cannam@128
|
455 while (NEXT() != 0 && last != -1)
|
cannam@128
|
456 ;
|
cannam@128
|
457 if (flags & 16) /* comment */
|
cannam@128
|
458 while (NEXT() != 0 && last != -1)
|
cannam@128
|
459 ;
|
cannam@128
|
460 if (flags & 2) { /* header crc */
|
cannam@128
|
461 NEXT();
|
cannam@128
|
462 NEXT();
|
cannam@128
|
463 }
|
cannam@128
|
464 if (last == -1) break;
|
cannam@128
|
465
|
cannam@128
|
466 /* set up output */
|
cannam@128
|
467 outd.outfile = outfile;
|
cannam@128
|
468 outd.check = 1;
|
cannam@128
|
469 outd.crc = crc32(0L, Z_NULL, 0);
|
cannam@128
|
470 outd.total = 0;
|
cannam@128
|
471
|
cannam@128
|
472 /* decompress data to output */
|
cannam@128
|
473 strm->next_in = next;
|
cannam@128
|
474 strm->avail_in = have;
|
cannam@128
|
475 ret = inflateBack(strm, in, indp, out, &outd);
|
cannam@128
|
476 if (ret != Z_STREAM_END) break;
|
cannam@128
|
477 next = strm->next_in;
|
cannam@128
|
478 have = strm->avail_in;
|
cannam@128
|
479 strm->next_in = Z_NULL; /* so Z_BUF_ERROR means EOF */
|
cannam@128
|
480
|
cannam@128
|
481 /* check trailer */
|
cannam@128
|
482 ret = Z_BUF_ERROR;
|
cannam@128
|
483 if (NEXT() != (int)(outd.crc & 0xff) ||
|
cannam@128
|
484 NEXT() != (int)((outd.crc >> 8) & 0xff) ||
|
cannam@128
|
485 NEXT() != (int)((outd.crc >> 16) & 0xff) ||
|
cannam@128
|
486 NEXT() != (int)((outd.crc >> 24) & 0xff)) {
|
cannam@128
|
487 /* crc error */
|
cannam@128
|
488 if (last != -1) {
|
cannam@128
|
489 strm->msg = (char *)"incorrect data check";
|
cannam@128
|
490 ret = Z_DATA_ERROR;
|
cannam@128
|
491 }
|
cannam@128
|
492 break;
|
cannam@128
|
493 }
|
cannam@128
|
494 if (NEXT() != (int)(outd.total & 0xff) ||
|
cannam@128
|
495 NEXT() != (int)((outd.total >> 8) & 0xff) ||
|
cannam@128
|
496 NEXT() != (int)((outd.total >> 16) & 0xff) ||
|
cannam@128
|
497 NEXT() != (int)((outd.total >> 24) & 0xff)) {
|
cannam@128
|
498 /* length error */
|
cannam@128
|
499 if (last != -1) {
|
cannam@128
|
500 strm->msg = (char *)"incorrect length check";
|
cannam@128
|
501 ret = Z_DATA_ERROR;
|
cannam@128
|
502 }
|
cannam@128
|
503 break;
|
cannam@128
|
504 }
|
cannam@128
|
505
|
cannam@128
|
506 /* go back and look for another gzip stream */
|
cannam@128
|
507 }
|
cannam@128
|
508
|
cannam@128
|
509 /* clean up and return */
|
cannam@128
|
510 return ret;
|
cannam@128
|
511 }
|
cannam@128
|
512
|
cannam@128
|
513 /* Copy file attributes, from -> to, as best we can. This is best effort, so
|
cannam@128
|
514 no errors are reported. The mode bits, including suid, sgid, and the sticky
|
cannam@128
|
515 bit are copied (if allowed), the owner's user id and group id are copied
|
cannam@128
|
516 (again if allowed), and the access and modify times are copied. */
|
cannam@128
|
517 local void copymeta(char *from, char *to)
|
cannam@128
|
518 {
|
cannam@128
|
519 struct stat was;
|
cannam@128
|
520 struct utimbuf when;
|
cannam@128
|
521
|
cannam@128
|
522 /* get all of from's Unix meta data, return if not a regular file */
|
cannam@128
|
523 if (stat(from, &was) != 0 || (was.st_mode & S_IFMT) != S_IFREG)
|
cannam@128
|
524 return;
|
cannam@128
|
525
|
cannam@128
|
526 /* set to's mode bits, ignore errors */
|
cannam@128
|
527 (void)chmod(to, was.st_mode & 07777);
|
cannam@128
|
528
|
cannam@128
|
529 /* copy owner's user and group, ignore errors */
|
cannam@128
|
530 (void)chown(to, was.st_uid, was.st_gid);
|
cannam@128
|
531
|
cannam@128
|
532 /* copy access and modify times, ignore errors */
|
cannam@128
|
533 when.actime = was.st_atime;
|
cannam@128
|
534 when.modtime = was.st_mtime;
|
cannam@128
|
535 (void)utime(to, &when);
|
cannam@128
|
536 }
|
cannam@128
|
537
|
cannam@128
|
538 /* Decompress the file inname to the file outnname, of if test is true, just
|
cannam@128
|
539 decompress without writing and check the gzip trailer for integrity. If
|
cannam@128
|
540 inname is NULL or an empty string, read from stdin. If outname is NULL or
|
cannam@128
|
541 an empty string, write to stdout. strm is a pre-initialized inflateBack
|
cannam@128
|
542 structure. When appropriate, copy the file attributes from inname to
|
cannam@128
|
543 outname.
|
cannam@128
|
544
|
cannam@128
|
545 gunzip() returns 1 if there is an out-of-memory error or an unexpected
|
cannam@128
|
546 return code from gunpipe(). Otherwise it returns 0.
|
cannam@128
|
547 */
|
cannam@128
|
548 local int gunzip(z_stream *strm, char *inname, char *outname, int test)
|
cannam@128
|
549 {
|
cannam@128
|
550 int ret;
|
cannam@128
|
551 int infile, outfile;
|
cannam@128
|
552
|
cannam@128
|
553 /* open files */
|
cannam@128
|
554 if (inname == NULL || *inname == 0) {
|
cannam@128
|
555 inname = "-";
|
cannam@128
|
556 infile = 0; /* stdin */
|
cannam@128
|
557 }
|
cannam@128
|
558 else {
|
cannam@128
|
559 infile = open(inname, O_RDONLY, 0);
|
cannam@128
|
560 if (infile == -1) {
|
cannam@128
|
561 fprintf(stderr, "gun cannot open %s\n", inname);
|
cannam@128
|
562 return 0;
|
cannam@128
|
563 }
|
cannam@128
|
564 }
|
cannam@128
|
565 if (test)
|
cannam@128
|
566 outfile = -1;
|
cannam@128
|
567 else if (outname == NULL || *outname == 0) {
|
cannam@128
|
568 outname = "-";
|
cannam@128
|
569 outfile = 1; /* stdout */
|
cannam@128
|
570 }
|
cannam@128
|
571 else {
|
cannam@128
|
572 outfile = open(outname, O_CREAT | O_TRUNC | O_WRONLY, 0666);
|
cannam@128
|
573 if (outfile == -1) {
|
cannam@128
|
574 close(infile);
|
cannam@128
|
575 fprintf(stderr, "gun cannot create %s\n", outname);
|
cannam@128
|
576 return 0;
|
cannam@128
|
577 }
|
cannam@128
|
578 }
|
cannam@128
|
579 errno = 0;
|
cannam@128
|
580
|
cannam@128
|
581 /* decompress */
|
cannam@128
|
582 ret = gunpipe(strm, infile, outfile);
|
cannam@128
|
583 if (outfile > 2) close(outfile);
|
cannam@128
|
584 if (infile > 2) close(infile);
|
cannam@128
|
585
|
cannam@128
|
586 /* interpret result */
|
cannam@128
|
587 switch (ret) {
|
cannam@128
|
588 case Z_OK:
|
cannam@128
|
589 case Z_ERRNO:
|
cannam@128
|
590 if (infile > 2 && outfile > 2) {
|
cannam@128
|
591 copymeta(inname, outname); /* copy attributes */
|
cannam@128
|
592 unlink(inname);
|
cannam@128
|
593 }
|
cannam@128
|
594 if (ret == Z_ERRNO)
|
cannam@128
|
595 fprintf(stderr, "gun warning: trailing garbage ignored in %s\n",
|
cannam@128
|
596 inname);
|
cannam@128
|
597 break;
|
cannam@128
|
598 case Z_DATA_ERROR:
|
cannam@128
|
599 if (outfile > 2) unlink(outname);
|
cannam@128
|
600 fprintf(stderr, "gun data error on %s: %s\n", inname, strm->msg);
|
cannam@128
|
601 break;
|
cannam@128
|
602 case Z_MEM_ERROR:
|
cannam@128
|
603 if (outfile > 2) unlink(outname);
|
cannam@128
|
604 fprintf(stderr, "gun out of memory error--aborting\n");
|
cannam@128
|
605 return 1;
|
cannam@128
|
606 case Z_BUF_ERROR:
|
cannam@128
|
607 if (outfile > 2) unlink(outname);
|
cannam@128
|
608 if (strm->next_in != Z_NULL) {
|
cannam@128
|
609 fprintf(stderr, "gun write error on %s: %s\n",
|
cannam@128
|
610 outname, strerror(errno));
|
cannam@128
|
611 }
|
cannam@128
|
612 else if (errno) {
|
cannam@128
|
613 fprintf(stderr, "gun read error on %s: %s\n",
|
cannam@128
|
614 inname, strerror(errno));
|
cannam@128
|
615 }
|
cannam@128
|
616 else {
|
cannam@128
|
617 fprintf(stderr, "gun unexpected end of file on %s\n",
|
cannam@128
|
618 inname);
|
cannam@128
|
619 }
|
cannam@128
|
620 break;
|
cannam@128
|
621 default:
|
cannam@128
|
622 if (outfile > 2) unlink(outname);
|
cannam@128
|
623 fprintf(stderr, "gun internal error--aborting\n");
|
cannam@128
|
624 return 1;
|
cannam@128
|
625 }
|
cannam@128
|
626 return 0;
|
cannam@128
|
627 }
|
cannam@128
|
628
|
cannam@128
|
629 /* Process the gun command line arguments. See the command syntax near the
|
cannam@128
|
630 beginning of this source file. */
|
cannam@128
|
631 int main(int argc, char **argv)
|
cannam@128
|
632 {
|
cannam@128
|
633 int ret, len, test;
|
cannam@128
|
634 char *outname;
|
cannam@128
|
635 unsigned char *window;
|
cannam@128
|
636 z_stream strm;
|
cannam@128
|
637
|
cannam@128
|
638 /* initialize inflateBack state for repeated use */
|
cannam@128
|
639 window = match; /* reuse LZW match buffer */
|
cannam@128
|
640 strm.zalloc = Z_NULL;
|
cannam@128
|
641 strm.zfree = Z_NULL;
|
cannam@128
|
642 strm.opaque = Z_NULL;
|
cannam@128
|
643 ret = inflateBackInit(&strm, 15, window);
|
cannam@128
|
644 if (ret != Z_OK) {
|
cannam@128
|
645 fprintf(stderr, "gun out of memory error--aborting\n");
|
cannam@128
|
646 return 1;
|
cannam@128
|
647 }
|
cannam@128
|
648
|
cannam@128
|
649 /* decompress each file to the same name with the suffix removed */
|
cannam@128
|
650 argc--;
|
cannam@128
|
651 argv++;
|
cannam@128
|
652 test = 0;
|
cannam@128
|
653 if (argc && strcmp(*argv, "-h") == 0) {
|
cannam@128
|
654 fprintf(stderr, "gun 1.6 (17 Jan 2010)\n");
|
cannam@128
|
655 fprintf(stderr, "Copyright (C) 2003-2010 Mark Adler\n");
|
cannam@128
|
656 fprintf(stderr, "usage: gun [-t] [file1.gz [file2.Z ...]]\n");
|
cannam@128
|
657 return 0;
|
cannam@128
|
658 }
|
cannam@128
|
659 if (argc && strcmp(*argv, "-t") == 0) {
|
cannam@128
|
660 test = 1;
|
cannam@128
|
661 argc--;
|
cannam@128
|
662 argv++;
|
cannam@128
|
663 }
|
cannam@128
|
664 if (argc)
|
cannam@128
|
665 do {
|
cannam@128
|
666 if (test)
|
cannam@128
|
667 outname = NULL;
|
cannam@128
|
668 else {
|
cannam@128
|
669 len = (int)strlen(*argv);
|
cannam@128
|
670 if (strcmp(*argv + len - 3, ".gz") == 0 ||
|
cannam@128
|
671 strcmp(*argv + len - 3, "-gz") == 0)
|
cannam@128
|
672 len -= 3;
|
cannam@128
|
673 else if (strcmp(*argv + len - 2, ".z") == 0 ||
|
cannam@128
|
674 strcmp(*argv + len - 2, "-z") == 0 ||
|
cannam@128
|
675 strcmp(*argv + len - 2, "_z") == 0 ||
|
cannam@128
|
676 strcmp(*argv + len - 2, ".Z") == 0)
|
cannam@128
|
677 len -= 2;
|
cannam@128
|
678 else {
|
cannam@128
|
679 fprintf(stderr, "gun error: no gz type on %s--skipping\n",
|
cannam@128
|
680 *argv);
|
cannam@128
|
681 continue;
|
cannam@128
|
682 }
|
cannam@128
|
683 outname = malloc(len + 1);
|
cannam@128
|
684 if (outname == NULL) {
|
cannam@128
|
685 fprintf(stderr, "gun out of memory error--aborting\n");
|
cannam@128
|
686 ret = 1;
|
cannam@128
|
687 break;
|
cannam@128
|
688 }
|
cannam@128
|
689 memcpy(outname, *argv, len);
|
cannam@128
|
690 outname[len] = 0;
|
cannam@128
|
691 }
|
cannam@128
|
692 ret = gunzip(&strm, *argv, outname, test);
|
cannam@128
|
693 if (outname != NULL) free(outname);
|
cannam@128
|
694 if (ret) break;
|
cannam@128
|
695 } while (argv++, --argc);
|
cannam@128
|
696 else
|
cannam@128
|
697 ret = gunzip(&strm, NULL, NULL, test);
|
cannam@128
|
698
|
cannam@128
|
699 /* clean up */
|
cannam@128
|
700 inflateBackEnd(&strm);
|
cannam@128
|
701 return ret;
|
cannam@128
|
702 }
|