Chris@4: /* gun.c -- simple gunzip to give an example of the use of inflateBack() Chris@4: * Copyright (C) 2003, 2005, 2008, 2010 Mark Adler Chris@4: * For conditions of distribution and use, see copyright notice in zlib.h Chris@4: Version 1.6 17 January 2010 Mark Adler */ Chris@4: Chris@4: /* Version history: Chris@4: 1.0 16 Feb 2003 First version for testing of inflateBack() Chris@4: 1.1 21 Feb 2005 Decompress concatenated gzip streams Chris@4: Remove use of "this" variable (C++ keyword) Chris@4: Fix return value for in() Chris@4: Improve allocation failure checking Chris@4: Add typecasting for void * structures Chris@4: Add -h option for command version and usage Chris@4: Add a bunch of comments Chris@4: 1.2 20 Mar 2005 Add Unix compress (LZW) decompression Chris@4: Copy file attributes from input file to output file Chris@4: 1.3 12 Jun 2005 Add casts for error messages [Oberhumer] Chris@4: 1.4 8 Dec 2006 LZW decompression speed improvements Chris@4: 1.5 9 Feb 2008 Avoid warning in latest version of gcc Chris@4: 1.6 17 Jan 2010 Avoid signed/unsigned comparison warnings Chris@4: */ Chris@4: Chris@4: /* Chris@4: gun [ -t ] [ name ... ] Chris@4: Chris@4: decompresses the data in the named gzip files. If no arguments are given, Chris@4: gun will decompress from stdin to stdout. The names must end in .gz, -gz, Chris@4: .z, -z, _z, or .Z. The uncompressed data will be written to a file name Chris@4: with the suffix stripped. On success, the original file is deleted. On Chris@4: failure, the output file is deleted. For most failures, the command will Chris@4: continue to process the remaining names on the command line. A memory Chris@4: allocation failure will abort the command. If -t is specified, then the Chris@4: listed files or stdin will be tested as gzip files for integrity (without Chris@4: checking for a proper suffix), no output will be written, and no files Chris@4: will be deleted. Chris@4: Chris@4: Like gzip, gun allows concatenated gzip streams and will decompress them, Chris@4: writing all of the uncompressed data to the output. Unlike gzip, gun allows Chris@4: an empty file on input, and will produce no error writing an empty output Chris@4: file. Chris@4: Chris@4: gun will also decompress files made by Unix compress, which uses LZW Chris@4: compression. These files are automatically detected by virtue of their Chris@4: magic header bytes. Since the end of Unix compress stream is marked by the Chris@4: end-of-file, they cannot be concantenated. If a Unix compress stream is Chris@4: encountered in an input file, it is the last stream in that file. Chris@4: Chris@4: Like gunzip and uncompress, the file attributes of the orignal compressed Chris@4: file are maintained in the final uncompressed file, to the extent that the Chris@4: user permissions allow it. Chris@4: Chris@4: On my Mac OS X PowerPC G4, gun is almost twice as fast as gunzip (version Chris@4: 1.2.4) is on the same file, when gun is linked with zlib 1.2.2. Also the Chris@4: LZW decompression provided by gun is about twice as fast as the standard Chris@4: Unix uncompress command. Chris@4: */ Chris@4: Chris@4: /* external functions and related types and constants */ Chris@4: #include /* fprintf() */ Chris@4: #include /* malloc(), free() */ Chris@4: #include /* strerror(), strcmp(), strlen(), memcpy() */ Chris@4: #include /* errno */ Chris@4: #include /* open() */ Chris@4: #include /* read(), write(), close(), chown(), unlink() */ Chris@4: #include Chris@4: #include /* stat(), chmod() */ Chris@4: #include /* utime() */ Chris@4: #include "zlib.h" /* inflateBackInit(), inflateBack(), */ Chris@4: /* inflateBackEnd(), crc32() */ Chris@4: Chris@4: /* function declaration */ Chris@4: #define local static Chris@4: Chris@4: /* buffer constants */ Chris@4: #define SIZE 32768U /* input and output buffer sizes */ Chris@4: #define PIECE 16384 /* limits i/o chunks for 16-bit int case */ Chris@4: Chris@4: /* structure for infback() to pass to input function in() -- it maintains the Chris@4: input file and a buffer of size SIZE */ Chris@4: struct ind { Chris@4: int infile; Chris@4: unsigned char *inbuf; Chris@4: }; Chris@4: Chris@4: /* Load input buffer, assumed to be empty, and return bytes loaded and a Chris@4: pointer to them. read() is called until the buffer is full, or until it Chris@4: returns end-of-file or error. Return 0 on error. */ Chris@4: local unsigned in(void *in_desc, unsigned char **buf) Chris@4: { Chris@4: int ret; Chris@4: unsigned len; Chris@4: unsigned char *next; Chris@4: struct ind *me = (struct ind *)in_desc; Chris@4: Chris@4: next = me->inbuf; Chris@4: *buf = next; Chris@4: len = 0; Chris@4: do { Chris@4: ret = PIECE; Chris@4: if ((unsigned)ret > SIZE - len) Chris@4: ret = (int)(SIZE - len); Chris@4: ret = (int)read(me->infile, next, ret); Chris@4: if (ret == -1) { Chris@4: len = 0; Chris@4: break; Chris@4: } Chris@4: next += ret; Chris@4: len += ret; Chris@4: } while (ret != 0 && len < SIZE); Chris@4: return len; Chris@4: } Chris@4: Chris@4: /* structure for infback() to pass to output function out() -- it maintains the Chris@4: output file, a running CRC-32 check on the output and the total number of Chris@4: bytes output, both for checking against the gzip trailer. (The length in Chris@4: the gzip trailer is stored modulo 2^32, so it's ok if a long is 32 bits and Chris@4: the output is greater than 4 GB.) */ Chris@4: struct outd { Chris@4: int outfile; Chris@4: int check; /* true if checking crc and total */ Chris@4: unsigned long crc; Chris@4: unsigned long total; Chris@4: }; Chris@4: Chris@4: /* Write output buffer and update the CRC-32 and total bytes written. write() Chris@4: is called until all of the output is written or an error is encountered. Chris@4: On success out() returns 0. For a write failure, out() returns 1. If the Chris@4: output file descriptor is -1, then nothing is written. Chris@4: */ Chris@4: local int out(void *out_desc, unsigned char *buf, unsigned len) Chris@4: { Chris@4: int ret; Chris@4: struct outd *me = (struct outd *)out_desc; Chris@4: Chris@4: if (me->check) { Chris@4: me->crc = crc32(me->crc, buf, len); Chris@4: me->total += len; Chris@4: } Chris@4: if (me->outfile != -1) Chris@4: do { Chris@4: ret = PIECE; Chris@4: if ((unsigned)ret > len) Chris@4: ret = (int)len; Chris@4: ret = (int)write(me->outfile, buf, ret); Chris@4: if (ret == -1) Chris@4: return 1; Chris@4: buf += ret; Chris@4: len -= ret; Chris@4: } while (len != 0); Chris@4: return 0; Chris@4: } Chris@4: Chris@4: /* next input byte macro for use inside lunpipe() and gunpipe() */ Chris@4: #define NEXT() (have ? 0 : (have = in(indp, &next)), \ Chris@4: last = have ? (have--, (int)(*next++)) : -1) Chris@4: Chris@4: /* memory for gunpipe() and lunpipe() -- Chris@4: the first 256 entries of prefix[] and suffix[] are never used, could Chris@4: have offset the index, but it's faster to waste the memory */ Chris@4: unsigned char inbuf[SIZE]; /* input buffer */ Chris@4: unsigned char outbuf[SIZE]; /* output buffer */ Chris@4: unsigned short prefix[65536]; /* index to LZW prefix string */ Chris@4: unsigned char suffix[65536]; /* one-character LZW suffix */ Chris@4: unsigned char match[65280 + 2]; /* buffer for reversed match or gzip Chris@4: 32K sliding window */ Chris@4: Chris@4: /* throw out what's left in the current bits byte buffer (this is a vestigial Chris@4: aspect of the compressed data format derived from an implementation that Chris@4: made use of a special VAX machine instruction!) */ Chris@4: #define FLUSHCODE() \ Chris@4: do { \ Chris@4: left = 0; \ Chris@4: rem = 0; \ Chris@4: if (chunk > have) { \ Chris@4: chunk -= have; \ Chris@4: have = 0; \ Chris@4: if (NEXT() == -1) \ Chris@4: break; \ Chris@4: chunk--; \ Chris@4: if (chunk > have) { \ Chris@4: chunk = have = 0; \ Chris@4: break; \ Chris@4: } \ Chris@4: } \ Chris@4: have -= chunk; \ Chris@4: next += chunk; \ Chris@4: chunk = 0; \ Chris@4: } while (0) Chris@4: Chris@4: /* Decompress a compress (LZW) file from indp to outfile. The compress magic Chris@4: header (two bytes) has already been read and verified. There are have bytes Chris@4: of buffered input at next. strm is used for passing error information back Chris@4: to gunpipe(). Chris@4: Chris@4: lunpipe() will return Z_OK on success, Z_BUF_ERROR for an unexpected end of Chris@4: file, read error, or write error (a write error indicated by strm->next_in Chris@4: not equal to Z_NULL), or Z_DATA_ERROR for invalid input. Chris@4: */ Chris@4: local int lunpipe(unsigned have, unsigned char *next, struct ind *indp, Chris@4: int outfile, z_stream *strm) Chris@4: { Chris@4: int last; /* last byte read by NEXT(), or -1 if EOF */ Chris@4: unsigned chunk; /* bytes left in current chunk */ Chris@4: int left; /* bits left in rem */ Chris@4: unsigned rem; /* unused bits from input */ Chris@4: int bits; /* current bits per code */ Chris@4: unsigned code; /* code, table traversal index */ Chris@4: unsigned mask; /* mask for current bits codes */ Chris@4: int max; /* maximum bits per code for this stream */ Chris@4: unsigned flags; /* compress flags, then block compress flag */ Chris@4: unsigned end; /* last valid entry in prefix/suffix tables */ Chris@4: unsigned temp; /* current code */ Chris@4: unsigned prev; /* previous code */ Chris@4: unsigned final; /* last character written for previous code */ Chris@4: unsigned stack; /* next position for reversed string */ Chris@4: unsigned outcnt; /* bytes in output buffer */ Chris@4: struct outd outd; /* output structure */ Chris@4: unsigned char *p; Chris@4: Chris@4: /* set up output */ Chris@4: outd.outfile = outfile; Chris@4: outd.check = 0; Chris@4: Chris@4: /* process remainder of compress header -- a flags byte */ Chris@4: flags = NEXT(); Chris@4: if (last == -1) Chris@4: return Z_BUF_ERROR; Chris@4: if (flags & 0x60) { Chris@4: strm->msg = (char *)"unknown lzw flags set"; Chris@4: return Z_DATA_ERROR; Chris@4: } Chris@4: max = flags & 0x1f; Chris@4: if (max < 9 || max > 16) { Chris@4: strm->msg = (char *)"lzw bits out of range"; Chris@4: return Z_DATA_ERROR; Chris@4: } Chris@4: if (max == 9) /* 9 doesn't really mean 9 */ Chris@4: max = 10; Chris@4: flags &= 0x80; /* true if block compress */ Chris@4: Chris@4: /* clear table */ Chris@4: bits = 9; Chris@4: mask = 0x1ff; Chris@4: end = flags ? 256 : 255; Chris@4: Chris@4: /* set up: get first 9-bit code, which is the first decompressed byte, but Chris@4: don't create a table entry until the next code */ Chris@4: if (NEXT() == -1) /* no compressed data is ok */ Chris@4: return Z_OK; Chris@4: final = prev = (unsigned)last; /* low 8 bits of code */ Chris@4: if (NEXT() == -1) /* missing a bit */ Chris@4: return Z_BUF_ERROR; Chris@4: if (last & 1) { /* code must be < 256 */ Chris@4: strm->msg = (char *)"invalid lzw code"; Chris@4: return Z_DATA_ERROR; Chris@4: } Chris@4: rem = (unsigned)last >> 1; /* remaining 7 bits */ Chris@4: left = 7; Chris@4: chunk = bits - 2; /* 7 bytes left in this chunk */ Chris@4: outbuf[0] = (unsigned char)final; /* write first decompressed byte */ Chris@4: outcnt = 1; Chris@4: Chris@4: /* decode codes */ Chris@4: stack = 0; Chris@4: for (;;) { Chris@4: /* if the table will be full after this, increment the code size */ Chris@4: if (end >= mask && bits < max) { Chris@4: FLUSHCODE(); Chris@4: bits++; Chris@4: mask <<= 1; Chris@4: mask++; Chris@4: } Chris@4: Chris@4: /* get a code of length bits */ Chris@4: if (chunk == 0) /* decrement chunk modulo bits */ Chris@4: chunk = bits; Chris@4: code = rem; /* low bits of code */ Chris@4: if (NEXT() == -1) { /* EOF is end of compressed data */ Chris@4: /* write remaining buffered output */ Chris@4: if (outcnt && out(&outd, outbuf, outcnt)) { Chris@4: strm->next_in = outbuf; /* signal write error */ Chris@4: return Z_BUF_ERROR; Chris@4: } Chris@4: return Z_OK; Chris@4: } Chris@4: code += (unsigned)last << left; /* middle (or high) bits of code */ Chris@4: left += 8; Chris@4: chunk--; Chris@4: if (bits > left) { /* need more bits */ Chris@4: if (NEXT() == -1) /* can't end in middle of code */ Chris@4: return Z_BUF_ERROR; Chris@4: code += (unsigned)last << left; /* high bits of code */ Chris@4: left += 8; Chris@4: chunk--; Chris@4: } Chris@4: code &= mask; /* mask to current code length */ Chris@4: left -= bits; /* number of unused bits */ Chris@4: rem = (unsigned)last >> (8 - left); /* unused bits from last byte */ Chris@4: Chris@4: /* process clear code (256) */ Chris@4: if (code == 256 && flags) { Chris@4: FLUSHCODE(); Chris@4: bits = 9; /* initialize bits and mask */ Chris@4: mask = 0x1ff; Chris@4: end = 255; /* empty table */ Chris@4: continue; /* get next code */ Chris@4: } Chris@4: Chris@4: /* special code to reuse last match */ Chris@4: temp = code; /* save the current code */ Chris@4: if (code > end) { Chris@4: /* Be picky on the allowed code here, and make sure that the code Chris@4: we drop through (prev) will be a valid index so that random Chris@4: input does not cause an exception. The code != end + 1 check is Chris@4: empirically derived, and not checked in the original uncompress Chris@4: code. If this ever causes a problem, that check could be safely Chris@4: removed. Leaving this check in greatly improves gun's ability Chris@4: to detect random or corrupted input after a compress header. Chris@4: In any case, the prev > end check must be retained. */ Chris@4: if (code != end + 1 || prev > end) { Chris@4: strm->msg = (char *)"invalid lzw code"; Chris@4: return Z_DATA_ERROR; Chris@4: } Chris@4: match[stack++] = (unsigned char)final; Chris@4: code = prev; Chris@4: } Chris@4: Chris@4: /* walk through linked list to generate output in reverse order */ Chris@4: p = match + stack; Chris@4: while (code >= 256) { Chris@4: *p++ = suffix[code]; Chris@4: code = prefix[code]; Chris@4: } Chris@4: stack = p - match; Chris@4: match[stack++] = (unsigned char)code; Chris@4: final = code; Chris@4: Chris@4: /* link new table entry */ Chris@4: if (end < mask) { Chris@4: end++; Chris@4: prefix[end] = (unsigned short)prev; Chris@4: suffix[end] = (unsigned char)final; Chris@4: } Chris@4: Chris@4: /* set previous code for next iteration */ Chris@4: prev = temp; Chris@4: Chris@4: /* write output in forward order */ Chris@4: while (stack > SIZE - outcnt) { Chris@4: while (outcnt < SIZE) Chris@4: outbuf[outcnt++] = match[--stack]; Chris@4: if (out(&outd, outbuf, outcnt)) { Chris@4: strm->next_in = outbuf; /* signal write error */ Chris@4: return Z_BUF_ERROR; Chris@4: } Chris@4: outcnt = 0; Chris@4: } Chris@4: p = match + stack; Chris@4: do { Chris@4: outbuf[outcnt++] = *--p; Chris@4: } while (p > match); Chris@4: stack = 0; Chris@4: Chris@4: /* loop for next code with final and prev as the last match, rem and Chris@4: left provide the first 0..7 bits of the next code, end is the last Chris@4: valid table entry */ Chris@4: } Chris@4: } Chris@4: Chris@4: /* Decompress a gzip file from infile to outfile. strm is assumed to have been Chris@4: successfully initialized with inflateBackInit(). The input file may consist Chris@4: of a series of gzip streams, in which case all of them will be decompressed Chris@4: to the output file. If outfile is -1, then the gzip stream(s) integrity is Chris@4: checked and nothing is written. Chris@4: Chris@4: The return value is a zlib error code: Z_MEM_ERROR if out of memory, Chris@4: Z_DATA_ERROR if the header or the compressed data is invalid, or if the Chris@4: trailer CRC-32 check or length doesn't match, Z_BUF_ERROR if the input ends Chris@4: prematurely or a write error occurs, or Z_ERRNO if junk (not a another gzip Chris@4: stream) follows a valid gzip stream. Chris@4: */ Chris@4: local int gunpipe(z_stream *strm, int infile, int outfile) Chris@4: { Chris@4: int ret, first, last; Chris@4: unsigned have, flags, len; Chris@4: unsigned char *next = NULL; Chris@4: struct ind ind, *indp; Chris@4: struct outd outd; Chris@4: Chris@4: /* setup input buffer */ Chris@4: ind.infile = infile; Chris@4: ind.inbuf = inbuf; Chris@4: indp = &ind; Chris@4: Chris@4: /* decompress concatenated gzip streams */ Chris@4: have = 0; /* no input data read in yet */ Chris@4: first = 1; /* looking for first gzip header */ Chris@4: strm->next_in = Z_NULL; /* so Z_BUF_ERROR means EOF */ Chris@4: for (;;) { Chris@4: /* look for the two magic header bytes for a gzip stream */ Chris@4: if (NEXT() == -1) { Chris@4: ret = Z_OK; Chris@4: break; /* empty gzip stream is ok */ Chris@4: } Chris@4: if (last != 31 || (NEXT() != 139 && last != 157)) { Chris@4: strm->msg = (char *)"incorrect header check"; Chris@4: ret = first ? Z_DATA_ERROR : Z_ERRNO; Chris@4: break; /* not a gzip or compress header */ Chris@4: } Chris@4: first = 0; /* next non-header is junk */ Chris@4: Chris@4: /* process a compress (LZW) file -- can't be concatenated after this */ Chris@4: if (last == 157) { Chris@4: ret = lunpipe(have, next, indp, outfile, strm); Chris@4: break; Chris@4: } Chris@4: Chris@4: /* process remainder of gzip header */ Chris@4: ret = Z_BUF_ERROR; Chris@4: if (NEXT() != 8) { /* only deflate method allowed */ Chris@4: if (last == -1) break; Chris@4: strm->msg = (char *)"unknown compression method"; Chris@4: ret = Z_DATA_ERROR; Chris@4: break; Chris@4: } Chris@4: flags = NEXT(); /* header flags */ Chris@4: NEXT(); /* discard mod time, xflgs, os */ Chris@4: NEXT(); Chris@4: NEXT(); Chris@4: NEXT(); Chris@4: NEXT(); Chris@4: NEXT(); Chris@4: if (last == -1) break; Chris@4: if (flags & 0xe0) { Chris@4: strm->msg = (char *)"unknown header flags set"; Chris@4: ret = Z_DATA_ERROR; Chris@4: break; Chris@4: } Chris@4: if (flags & 4) { /* extra field */ Chris@4: len = NEXT(); Chris@4: len += (unsigned)(NEXT()) << 8; Chris@4: if (last == -1) break; Chris@4: while (len > have) { Chris@4: len -= have; Chris@4: have = 0; Chris@4: if (NEXT() == -1) break; Chris@4: len--; Chris@4: } Chris@4: if (last == -1) break; Chris@4: have -= len; Chris@4: next += len; Chris@4: } Chris@4: if (flags & 8) /* file name */ Chris@4: while (NEXT() != 0 && last != -1) Chris@4: ; Chris@4: if (flags & 16) /* comment */ Chris@4: while (NEXT() != 0 && last != -1) Chris@4: ; Chris@4: if (flags & 2) { /* header crc */ Chris@4: NEXT(); Chris@4: NEXT(); Chris@4: } Chris@4: if (last == -1) break; Chris@4: Chris@4: /* set up output */ Chris@4: outd.outfile = outfile; Chris@4: outd.check = 1; Chris@4: outd.crc = crc32(0L, Z_NULL, 0); Chris@4: outd.total = 0; Chris@4: Chris@4: /* decompress data to output */ Chris@4: strm->next_in = next; Chris@4: strm->avail_in = have; Chris@4: ret = inflateBack(strm, in, indp, out, &outd); Chris@4: if (ret != Z_STREAM_END) break; Chris@4: next = strm->next_in; Chris@4: have = strm->avail_in; Chris@4: strm->next_in = Z_NULL; /* so Z_BUF_ERROR means EOF */ Chris@4: Chris@4: /* check trailer */ Chris@4: ret = Z_BUF_ERROR; Chris@4: if (NEXT() != (int)(outd.crc & 0xff) || Chris@4: NEXT() != (int)((outd.crc >> 8) & 0xff) || Chris@4: NEXT() != (int)((outd.crc >> 16) & 0xff) || Chris@4: NEXT() != (int)((outd.crc >> 24) & 0xff)) { Chris@4: /* crc error */ Chris@4: if (last != -1) { Chris@4: strm->msg = (char *)"incorrect data check"; Chris@4: ret = Z_DATA_ERROR; Chris@4: } Chris@4: break; Chris@4: } Chris@4: if (NEXT() != (int)(outd.total & 0xff) || Chris@4: NEXT() != (int)((outd.total >> 8) & 0xff) || Chris@4: NEXT() != (int)((outd.total >> 16) & 0xff) || Chris@4: NEXT() != (int)((outd.total >> 24) & 0xff)) { Chris@4: /* length error */ Chris@4: if (last != -1) { Chris@4: strm->msg = (char *)"incorrect length check"; Chris@4: ret = Z_DATA_ERROR; Chris@4: } Chris@4: break; Chris@4: } Chris@4: Chris@4: /* go back and look for another gzip stream */ Chris@4: } Chris@4: Chris@4: /* clean up and return */ Chris@4: return ret; Chris@4: } Chris@4: Chris@4: /* Copy file attributes, from -> to, as best we can. This is best effort, so Chris@4: no errors are reported. The mode bits, including suid, sgid, and the sticky Chris@4: bit are copied (if allowed), the owner's user id and group id are copied Chris@4: (again if allowed), and the access and modify times are copied. */ Chris@4: local void copymeta(char *from, char *to) Chris@4: { Chris@4: struct stat was; Chris@4: struct utimbuf when; Chris@4: Chris@4: /* get all of from's Unix meta data, return if not a regular file */ Chris@4: if (stat(from, &was) != 0 || (was.st_mode & S_IFMT) != S_IFREG) Chris@4: return; Chris@4: Chris@4: /* set to's mode bits, ignore errors */ Chris@4: (void)chmod(to, was.st_mode & 07777); Chris@4: Chris@4: /* copy owner's user and group, ignore errors */ Chris@4: (void)chown(to, was.st_uid, was.st_gid); Chris@4: Chris@4: /* copy access and modify times, ignore errors */ Chris@4: when.actime = was.st_atime; Chris@4: when.modtime = was.st_mtime; Chris@4: (void)utime(to, &when); Chris@4: } Chris@4: Chris@4: /* Decompress the file inname to the file outnname, of if test is true, just Chris@4: decompress without writing and check the gzip trailer for integrity. If Chris@4: inname is NULL or an empty string, read from stdin. If outname is NULL or Chris@4: an empty string, write to stdout. strm is a pre-initialized inflateBack Chris@4: structure. When appropriate, copy the file attributes from inname to Chris@4: outname. Chris@4: Chris@4: gunzip() returns 1 if there is an out-of-memory error or an unexpected Chris@4: return code from gunpipe(). Otherwise it returns 0. Chris@4: */ Chris@4: local int gunzip(z_stream *strm, char *inname, char *outname, int test) Chris@4: { Chris@4: int ret; Chris@4: int infile, outfile; Chris@4: Chris@4: /* open files */ Chris@4: if (inname == NULL || *inname == 0) { Chris@4: inname = "-"; Chris@4: infile = 0; /* stdin */ Chris@4: } Chris@4: else { Chris@4: infile = open(inname, O_RDONLY, 0); Chris@4: if (infile == -1) { Chris@4: fprintf(stderr, "gun cannot open %s\n", inname); Chris@4: return 0; Chris@4: } Chris@4: } Chris@4: if (test) Chris@4: outfile = -1; Chris@4: else if (outname == NULL || *outname == 0) { Chris@4: outname = "-"; Chris@4: outfile = 1; /* stdout */ Chris@4: } Chris@4: else { Chris@4: outfile = open(outname, O_CREAT | O_TRUNC | O_WRONLY, 0666); Chris@4: if (outfile == -1) { Chris@4: close(infile); Chris@4: fprintf(stderr, "gun cannot create %s\n", outname); Chris@4: return 0; Chris@4: } Chris@4: } Chris@4: errno = 0; Chris@4: Chris@4: /* decompress */ Chris@4: ret = gunpipe(strm, infile, outfile); Chris@4: if (outfile > 2) close(outfile); Chris@4: if (infile > 2) close(infile); Chris@4: Chris@4: /* interpret result */ Chris@4: switch (ret) { Chris@4: case Z_OK: Chris@4: case Z_ERRNO: Chris@4: if (infile > 2 && outfile > 2) { Chris@4: copymeta(inname, outname); /* copy attributes */ Chris@4: unlink(inname); Chris@4: } Chris@4: if (ret == Z_ERRNO) Chris@4: fprintf(stderr, "gun warning: trailing garbage ignored in %s\n", Chris@4: inname); Chris@4: break; Chris@4: case Z_DATA_ERROR: Chris@4: if (outfile > 2) unlink(outname); Chris@4: fprintf(stderr, "gun data error on %s: %s\n", inname, strm->msg); Chris@4: break; Chris@4: case Z_MEM_ERROR: Chris@4: if (outfile > 2) unlink(outname); Chris@4: fprintf(stderr, "gun out of memory error--aborting\n"); Chris@4: return 1; Chris@4: case Z_BUF_ERROR: Chris@4: if (outfile > 2) unlink(outname); Chris@4: if (strm->next_in != Z_NULL) { Chris@4: fprintf(stderr, "gun write error on %s: %s\n", Chris@4: outname, strerror(errno)); Chris@4: } Chris@4: else if (errno) { Chris@4: fprintf(stderr, "gun read error on %s: %s\n", Chris@4: inname, strerror(errno)); Chris@4: } Chris@4: else { Chris@4: fprintf(stderr, "gun unexpected end of file on %s\n", Chris@4: inname); Chris@4: } Chris@4: break; Chris@4: default: Chris@4: if (outfile > 2) unlink(outname); Chris@4: fprintf(stderr, "gun internal error--aborting\n"); Chris@4: return 1; Chris@4: } Chris@4: return 0; Chris@4: } Chris@4: Chris@4: /* Process the gun command line arguments. See the command syntax near the Chris@4: beginning of this source file. */ Chris@4: int main(int argc, char **argv) Chris@4: { Chris@4: int ret, len, test; Chris@4: char *outname; Chris@4: unsigned char *window; Chris@4: z_stream strm; Chris@4: Chris@4: /* initialize inflateBack state for repeated use */ Chris@4: window = match; /* reuse LZW match buffer */ Chris@4: strm.zalloc = Z_NULL; Chris@4: strm.zfree = Z_NULL; Chris@4: strm.opaque = Z_NULL; Chris@4: ret = inflateBackInit(&strm, 15, window); Chris@4: if (ret != Z_OK) { Chris@4: fprintf(stderr, "gun out of memory error--aborting\n"); Chris@4: return 1; Chris@4: } Chris@4: Chris@4: /* decompress each file to the same name with the suffix removed */ Chris@4: argc--; Chris@4: argv++; Chris@4: test = 0; Chris@4: if (argc && strcmp(*argv, "-h") == 0) { Chris@4: fprintf(stderr, "gun 1.6 (17 Jan 2010)\n"); Chris@4: fprintf(stderr, "Copyright (C) 2003-2010 Mark Adler\n"); Chris@4: fprintf(stderr, "usage: gun [-t] [file1.gz [file2.Z ...]]\n"); Chris@4: return 0; Chris@4: } Chris@4: if (argc && strcmp(*argv, "-t") == 0) { Chris@4: test = 1; Chris@4: argc--; Chris@4: argv++; Chris@4: } Chris@4: if (argc) Chris@4: do { Chris@4: if (test) Chris@4: outname = NULL; Chris@4: else { Chris@4: len = (int)strlen(*argv); Chris@4: if (strcmp(*argv + len - 3, ".gz") == 0 || Chris@4: strcmp(*argv + len - 3, "-gz") == 0) Chris@4: len -= 3; Chris@4: else if (strcmp(*argv + len - 2, ".z") == 0 || Chris@4: strcmp(*argv + len - 2, "-z") == 0 || Chris@4: strcmp(*argv + len - 2, "_z") == 0 || Chris@4: strcmp(*argv + len - 2, ".Z") == 0) Chris@4: len -= 2; Chris@4: else { Chris@4: fprintf(stderr, "gun error: no gz type on %s--skipping\n", Chris@4: *argv); Chris@4: continue; Chris@4: } Chris@4: outname = malloc(len + 1); Chris@4: if (outname == NULL) { Chris@4: fprintf(stderr, "gun out of memory error--aborting\n"); Chris@4: ret = 1; Chris@4: break; Chris@4: } Chris@4: memcpy(outname, *argv, len); Chris@4: outname[len] = 0; Chris@4: } Chris@4: ret = gunzip(&strm, *argv, outname, test); Chris@4: if (outname != NULL) free(outname); Chris@4: if (ret) break; Chris@4: } while (argv++, --argc); Chris@4: else Chris@4: ret = gunzip(&strm, NULL, NULL, test); Chris@4: Chris@4: /* clean up */ Chris@4: inflateBackEnd(&strm); Chris@4: return ret; Chris@4: }