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