gun.c 25 KB

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