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