gzip.c revision 1.111 1 /* $NetBSD: gzip.c,v 1.111 2017/08/04 07:27:08 mrg Exp $ */
2
3 /*
4 * Copyright (c) 1997, 1998, 2003, 2004, 2006, 2008, 2009, 2010, 2011, 2015, 2017
5 * Matthew R. Green
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 #include <sys/cdefs.h>
31 #ifndef lint
32 __COPYRIGHT("@(#) Copyright (c) 1997, 1998, 2003, 2004, 2006, 2008,\
33 2009, 2010, 2011, 2015, 2017 Matthew R. Green. All rights reserved.");
34 __RCSID("$NetBSD: gzip.c,v 1.111 2017/08/04 07:27:08 mrg Exp $");
35 #endif /* not lint */
36
37 /*
38 * gzip.c -- GPL free gzip using zlib.
39 *
40 * RFC 1950 covers the zlib format
41 * RFC 1951 covers the deflate format
42 * RFC 1952 covers the gzip format
43 *
44 * TODO:
45 * - use mmap where possible
46 * - handle some signals better (remove outfile?)
47 * - make bzip2/compress -v/-t/-l support work as well as possible
48 */
49
50 #include <sys/param.h>
51 #include <sys/stat.h>
52 #include <sys/time.h>
53
54 #include <inttypes.h>
55 #include <unistd.h>
56 #include <stdio.h>
57 #include <string.h>
58 #include <stdlib.h>
59 #include <err.h>
60 #include <errno.h>
61 #include <fcntl.h>
62 #include <zlib.h>
63 #include <fts.h>
64 #include <libgen.h>
65 #include <stdarg.h>
66 #include <getopt.h>
67 #include <time.h>
68
69 #ifndef PRIdOFF
70 #define PRIdOFF PRId64
71 #endif
72
73 /* what type of file are we dealing with */
74 enum filetype {
75 FT_GZIP,
76 #ifndef NO_BZIP2_SUPPORT
77 FT_BZIP2,
78 #endif
79 #ifndef NO_COMPRESS_SUPPORT
80 FT_Z,
81 #endif
82 #ifndef NO_PACK_SUPPORT
83 FT_PACK,
84 #endif
85 #ifndef NO_XZ_SUPPORT
86 FT_XZ,
87 #endif
88 FT_LAST,
89 FT_UNKNOWN
90 };
91
92 #ifndef NO_BZIP2_SUPPORT
93 #include <bzlib.h>
94
95 #define BZ2_SUFFIX ".bz2"
96 #define BZIP2_MAGIC "\102\132\150"
97 #endif
98
99 #ifndef NO_COMPRESS_SUPPORT
100 #define Z_SUFFIX ".Z"
101 #define Z_MAGIC "\037\235"
102 #endif
103
104 #ifndef NO_PACK_SUPPORT
105 #define PACK_MAGIC "\037\036"
106 #endif
107
108 #ifndef NO_XZ_SUPPORT
109 #include <lzma.h>
110 #define XZ_SUFFIX ".xz"
111 #define XZ_MAGIC "\3757zXZ"
112 #endif
113
114 #define GZ_SUFFIX ".gz"
115
116 #define BUFLEN (64 * 1024)
117
118 #define GZIP_MAGIC0 0x1F
119 #define GZIP_MAGIC1 0x8B
120 #define GZIP_OMAGIC1 0x9E
121
122 #define GZIP_TIMESTAMP (off_t)4
123 #define GZIP_ORIGNAME (off_t)10
124
125 #define HEAD_CRC 0x02
126 #define EXTRA_FIELD 0x04
127 #define ORIG_NAME 0x08
128 #define COMMENT 0x10
129
130 #define OS_CODE 3 /* Unix */
131
132 typedef struct {
133 const char *zipped;
134 int ziplen;
135 const char *normal; /* for unzip - must not be longer than zipped */
136 } suffixes_t;
137 static suffixes_t suffixes[] = {
138 #define SUFFIX(Z, N) {Z, sizeof Z - 1, N}
139 SUFFIX(GZ_SUFFIX, ""), /* Overwritten by -S .xxx */
140 #ifndef SMALL
141 SUFFIX(GZ_SUFFIX, ""),
142 SUFFIX(".z", ""),
143 SUFFIX("-gz", ""),
144 SUFFIX("-z", ""),
145 SUFFIX("_z", ""),
146 SUFFIX(".taz", ".tar"),
147 SUFFIX(".tgz", ".tar"),
148 #ifndef NO_BZIP2_SUPPORT
149 SUFFIX(BZ2_SUFFIX, ""),
150 #endif
151 #ifndef NO_COMPRESS_SUPPORT
152 SUFFIX(Z_SUFFIX, ""),
153 #endif
154 #ifndef NO_XZ_SUPPORT
155 SUFFIX(XZ_SUFFIX, ""),
156 #endif
157 SUFFIX(GZ_SUFFIX, ""), /* Overwritten by -S "" */
158 #endif /* SMALL */
159 #undef SUFFIX
160 };
161 #define NUM_SUFFIXES (sizeof suffixes / sizeof suffixes[0])
162 #define SUFFIX_MAXLEN 30
163
164 static const char gzip_version[] = "NetBSD gzip 20170803";
165
166 static int cflag; /* stdout mode */
167 static int dflag; /* decompress mode */
168 static int lflag; /* list mode */
169 static int numflag = 6; /* gzip -1..-9 value */
170
171 #ifndef SMALL
172 static int fflag; /* force mode */
173 static int kflag; /* don't delete input files */
174 static int nflag; /* don't save name/timestamp */
175 static int Nflag; /* don't restore name/timestamp */
176 static int qflag; /* quiet mode */
177 static int rflag; /* recursive mode */
178 static int tflag; /* test */
179 static int vflag; /* verbose mode */
180 static sig_atomic_t print_info = 0;
181 #else
182 #define qflag 0
183 #define tflag 0
184 #endif
185
186 static int exit_value = 0; /* exit value */
187
188 static const char *infile; /* name of file coming in */
189
190 static void maybe_err(const char *fmt, ...) __printflike(1, 2) __dead;
191 #if !defined(NO_BZIP2_SUPPORT) || !defined(NO_PACK_SUPPORT) || \
192 !defined(NO_XZ_SUPPORT)
193 static void maybe_errx(const char *fmt, ...) __printflike(1, 2) __dead;
194 #endif
195 static void maybe_warn(const char *fmt, ...) __printflike(1, 2);
196 static void maybe_warnx(const char *fmt, ...) __printflike(1, 2);
197 static enum filetype file_gettype(u_char *);
198 #ifdef SMALL
199 #define gz_compress(if, of, sz, fn, tm) gz_compress(if, of, sz)
200 #endif
201 static off_t gz_compress(int, int, off_t *, const char *, uint32_t);
202 static off_t gz_uncompress(int, int, char *, size_t, off_t *, const char *);
203 static off_t file_compress(char *, char *, size_t);
204 static off_t file_uncompress(char *, char *, size_t);
205 static void handle_pathname(char *);
206 static void handle_file(char *, struct stat *);
207 static void handle_stdin(void);
208 static void handle_stdout(void);
209 static void print_ratio(off_t, off_t, FILE *);
210 static void print_list(int fd, off_t, const char *, time_t);
211 __dead static void usage(void);
212 __dead static void display_version(void);
213 static const suffixes_t *check_suffix(char *, int);
214 static ssize_t read_retry(int, void *, size_t);
215
216 #ifdef SMALL
217 #define infile_set(f,t) infile_set(f)
218 #endif
219 static void infile_set(const char *newinfile, off_t total);
220
221 #ifdef SMALL
222 #define unlink_input(f, sb) unlink(f)
223 #define check_siginfo() /* nothing */
224 #define setup_signals() /* nothing */
225 #define infile_newdata(t) /* nothing */
226 #else
227 static off_t infile_total; /* total expected to read/write */
228 static off_t infile_current; /* current read/write */
229
230 static void check_siginfo(void);
231 static off_t cat_fd(unsigned char *, size_t, off_t *, int fd);
232 static void prepend_gzip(char *, int *, char ***);
233 static void handle_dir(char *);
234 static void print_verbage(const char *, const char *, off_t, off_t);
235 static void print_test(const char *, int);
236 static void copymodes(int fd, const struct stat *, const char *file);
237 static int check_outfile(const char *outfile);
238 static void setup_signals(void);
239 static void infile_newdata(size_t newdata);
240 static void infile_clear(void);
241 #endif
242
243 #ifndef NO_BZIP2_SUPPORT
244 static off_t unbzip2(int, int, char *, size_t, off_t *);
245 #endif
246
247 #ifndef NO_COMPRESS_SUPPORT
248 static FILE *zdopen(int);
249 static off_t zuncompress(FILE *, FILE *, char *, size_t, off_t *);
250 #endif
251
252 #ifndef NO_PACK_SUPPORT
253 static off_t unpack(int, int, char *, size_t, off_t *);
254 #endif
255
256 #ifndef NO_XZ_SUPPORT
257 static off_t unxz(int, int, char *, size_t, off_t *);
258 #endif
259
260 #ifdef SMALL
261 #define getopt_long(a,b,c,d,e) getopt(a,b,c)
262 #else
263 static const struct option longopts[] = {
264 { "stdout", no_argument, 0, 'c' },
265 { "to-stdout", no_argument, 0, 'c' },
266 { "decompress", no_argument, 0, 'd' },
267 { "uncompress", no_argument, 0, 'd' },
268 { "force", no_argument, 0, 'f' },
269 { "help", no_argument, 0, 'h' },
270 { "keep", no_argument, 0, 'k' },
271 { "list", no_argument, 0, 'l' },
272 { "no-name", no_argument, 0, 'n' },
273 { "name", no_argument, 0, 'N' },
274 { "quiet", no_argument, 0, 'q' },
275 { "recursive", no_argument, 0, 'r' },
276 { "suffix", required_argument, 0, 'S' },
277 { "test", no_argument, 0, 't' },
278 { "verbose", no_argument, 0, 'v' },
279 { "version", no_argument, 0, 'V' },
280 { "fast", no_argument, 0, '1' },
281 { "best", no_argument, 0, '9' },
282 #if 0
283 /*
284 * This is what else GNU gzip implements. --ascii isn't useful
285 * on NetBSD, and I don't care to have a --license.
286 */
287 { "ascii", no_argument, 0, 'a' },
288 { "license", no_argument, 0, 'L' },
289 #endif
290 { NULL, no_argument, 0, 0 },
291 };
292 #endif
293
294 int
295 main(int argc, char **argv)
296 {
297 const char *progname = getprogname();
298 #ifndef SMALL
299 char *gzip;
300 int len;
301 #endif
302 int ch;
303
304 setup_signals();
305
306 #ifndef SMALL
307 if ((gzip = getenv("GZIP")) != NULL)
308 prepend_gzip(gzip, &argc, &argv);
309 #endif
310
311 /*
312 * XXX
313 * handle being called `gunzip', `zcat' and `gzcat'
314 */
315 if (strcmp(progname, "gunzip") == 0)
316 dflag = 1;
317 else if (strcmp(progname, "zcat") == 0 ||
318 strcmp(progname, "gzcat") == 0)
319 dflag = cflag = 1;
320
321 #ifdef SMALL
322 #define OPT_LIST "123456789cdhlV"
323 #else
324 #define OPT_LIST "123456789cdfhklNnqrS:tVv"
325 #endif
326
327 while ((ch = getopt_long(argc, argv, OPT_LIST, longopts, NULL)) != -1) {
328 switch (ch) {
329 case '1': case '2': case '3':
330 case '4': case '5': case '6':
331 case '7': case '8': case '9':
332 numflag = ch - '0';
333 break;
334 case 'c':
335 cflag = 1;
336 break;
337 case 'd':
338 dflag = 1;
339 break;
340 case 'l':
341 lflag = 1;
342 dflag = 1;
343 break;
344 case 'V':
345 display_version();
346 /* NOTREACHED */
347 #ifndef SMALL
348 case 'f':
349 fflag = 1;
350 break;
351 case 'k':
352 kflag = 1;
353 break;
354 case 'N':
355 nflag = 0;
356 Nflag = 1;
357 break;
358 case 'n':
359 nflag = 1;
360 Nflag = 0;
361 break;
362 case 'q':
363 qflag = 1;
364 break;
365 case 'r':
366 rflag = 1;
367 break;
368 case 'S':
369 len = strlen(optarg);
370 if (len != 0) {
371 if (len > SUFFIX_MAXLEN)
372 errx(1, "incorrect suffix: '%s'", optarg);
373 suffixes[0].zipped = optarg;
374 suffixes[0].ziplen = len;
375 } else {
376 suffixes[NUM_SUFFIXES - 1].zipped = "";
377 suffixes[NUM_SUFFIXES - 1].ziplen = 0;
378 }
379 break;
380 case 't':
381 cflag = 1;
382 tflag = 1;
383 dflag = 1;
384 break;
385 case 'v':
386 vflag = 1;
387 break;
388 #endif
389 default:
390 usage();
391 /* NOTREACHED */
392 }
393 }
394 argv += optind;
395 argc -= optind;
396
397 if (argc == 0) {
398 if (dflag) /* stdin mode */
399 handle_stdin();
400 else /* stdout mode */
401 handle_stdout();
402 } else {
403 do {
404 handle_pathname(argv[0]);
405 } while (*++argv);
406 }
407 #ifndef SMALL
408 if (qflag == 0 && lflag && argc > 1)
409 print_list(-1, 0, "(totals)", 0);
410 #endif
411 exit(exit_value);
412 }
413
414 /* maybe print a warning */
415 void
416 maybe_warn(const char *fmt, ...)
417 {
418 va_list ap;
419
420 if (qflag == 0) {
421 va_start(ap, fmt);
422 vwarn(fmt, ap);
423 va_end(ap);
424 }
425 if (exit_value == 0)
426 exit_value = 1;
427 }
428
429 /* ... without an errno. */
430 void
431 maybe_warnx(const char *fmt, ...)
432 {
433 va_list ap;
434
435 if (qflag == 0) {
436 va_start(ap, fmt);
437 vwarnx(fmt, ap);
438 va_end(ap);
439 }
440 if (exit_value == 0)
441 exit_value = 1;
442 }
443
444 /* maybe print an error */
445 void
446 maybe_err(const char *fmt, ...)
447 {
448 va_list ap;
449
450 if (qflag == 0) {
451 va_start(ap, fmt);
452 vwarn(fmt, ap);
453 va_end(ap);
454 }
455 exit(2);
456 }
457
458 #if !defined(NO_BZIP2_SUPPORT) || !defined(NO_PACK_SUPPORT) || \
459 !defined(NO_XZ_SUPPORT)
460 /* ... without an errno. */
461 void
462 maybe_errx(const char *fmt, ...)
463 {
464 va_list ap;
465
466 if (qflag == 0) {
467 va_start(ap, fmt);
468 vwarnx(fmt, ap);
469 va_end(ap);
470 }
471 exit(2);
472 }
473 #endif
474
475 #ifndef SMALL
476 /* split up $GZIP and prepend it to the argument list */
477 static void
478 prepend_gzip(char *gzip, int *argc, char ***argv)
479 {
480 char *s, **nargv, **ac;
481 int nenvarg = 0, i;
482
483 /* scan how many arguments there are */
484 for (s = gzip;;) {
485 while (*s == ' ' || *s == '\t')
486 s++;
487 if (*s == 0)
488 goto count_done;
489 nenvarg++;
490 while (*s != ' ' && *s != '\t')
491 if (*s++ == 0)
492 goto count_done;
493 }
494 count_done:
495 /* punt early */
496 if (nenvarg == 0)
497 return;
498
499 *argc += nenvarg;
500 ac = *argv;
501
502 nargv = (char **)malloc((*argc + 1) * sizeof(char *));
503 if (nargv == NULL)
504 maybe_err("malloc");
505
506 /* stash this away */
507 *argv = nargv;
508
509 /* copy the program name first */
510 i = 0;
511 nargv[i++] = *(ac++);
512
513 /* take a copy of $GZIP and add it to the array */
514 s = strdup(gzip);
515 if (s == NULL)
516 maybe_err("strdup");
517 for (;;) {
518 /* Skip whitespaces. */
519 while (*s == ' ' || *s == '\t')
520 s++;
521 if (*s == 0)
522 goto copy_done;
523 nargv[i++] = s;
524 /* Find the end of this argument. */
525 while (*s != ' ' && *s != '\t')
526 if (*s++ == 0)
527 /* Argument followed by NUL. */
528 goto copy_done;
529 /* Terminate by overwriting ' ' or '\t' with NUL. */
530 *s++ = 0;
531 }
532 copy_done:
533
534 /* copy the original arguments and a NULL */
535 while (*ac)
536 nargv[i++] = *(ac++);
537 nargv[i] = NULL;
538 }
539 #endif
540
541 /* compress input to output. Return bytes read, -1 on error */
542 static off_t
543 gz_compress(int in, int out, off_t *gsizep, const char *origname, uint32_t mtime)
544 {
545 z_stream z;
546 char *outbufp, *inbufp;
547 off_t in_tot = 0, out_tot = 0;
548 ssize_t in_size;
549 int i, error;
550 uLong crc;
551 #ifdef SMALL
552 static char header[] = { GZIP_MAGIC0, GZIP_MAGIC1, Z_DEFLATED, 0,
553 0, 0, 0, 0,
554 0, OS_CODE };
555 #endif
556
557 outbufp = malloc(BUFLEN);
558 inbufp = malloc(BUFLEN);
559 if (outbufp == NULL || inbufp == NULL) {
560 maybe_err("malloc failed");
561 goto out;
562 }
563
564 memset(&z, 0, sizeof z);
565 z.zalloc = Z_NULL;
566 z.zfree = Z_NULL;
567 z.opaque = 0;
568
569 #ifdef SMALL
570 memcpy(outbufp, header, sizeof header);
571 i = sizeof header;
572 #else
573 if (nflag != 0) {
574 mtime = 0;
575 origname = "";
576 }
577
578 i = snprintf(outbufp, BUFLEN, "%c%c%c%c%c%c%c%c%c%c%s",
579 GZIP_MAGIC0, GZIP_MAGIC1, Z_DEFLATED,
580 *origname ? ORIG_NAME : 0,
581 mtime & 0xff,
582 (mtime >> 8) & 0xff,
583 (mtime >> 16) & 0xff,
584 (mtime >> 24) & 0xff,
585 numflag == 1 ? 4 : numflag == 9 ? 2 : 0,
586 OS_CODE, origname);
587 if (i >= BUFLEN)
588 /* this need PATH_MAX > BUFLEN ... */
589 maybe_err("snprintf");
590 if (*origname)
591 i++;
592 #endif
593
594 z.next_out = (unsigned char *)outbufp + i;
595 z.avail_out = BUFLEN - i;
596
597 error = deflateInit2(&z, numflag, Z_DEFLATED,
598 (-MAX_WBITS), 8, Z_DEFAULT_STRATEGY);
599 if (error != Z_OK) {
600 maybe_warnx("deflateInit2 failed");
601 in_tot = -1;
602 goto out;
603 }
604
605 crc = crc32(0L, Z_NULL, 0);
606 for (;;) {
607 if (z.avail_out == 0) {
608 if (write(out, outbufp, BUFLEN) != BUFLEN) {
609 maybe_warn("write");
610 out_tot = -1;
611 goto out;
612 }
613
614 out_tot += BUFLEN;
615 z.next_out = (unsigned char *)outbufp;
616 z.avail_out = BUFLEN;
617 }
618
619 if (z.avail_in == 0) {
620 in_size = read(in, inbufp, BUFLEN);
621 if (in_size < 0) {
622 maybe_warn("read");
623 in_tot = -1;
624 goto out;
625 }
626 if (in_size == 0)
627 break;
628 infile_newdata(in_size);
629
630 crc = crc32(crc, (const Bytef *)inbufp, (unsigned)in_size);
631 in_tot += in_size;
632 z.next_in = (unsigned char *)inbufp;
633 z.avail_in = in_size;
634 }
635
636 error = deflate(&z, Z_NO_FLUSH);
637 if (error != Z_OK && error != Z_STREAM_END) {
638 maybe_warnx("deflate failed");
639 in_tot = -1;
640 goto out;
641 }
642 }
643
644 /* clean up */
645 for (;;) {
646 size_t len;
647 ssize_t w;
648
649 error = deflate(&z, Z_FINISH);
650 if (error != Z_OK && error != Z_STREAM_END) {
651 maybe_warnx("deflate failed");
652 in_tot = -1;
653 goto out;
654 }
655
656 len = (char *)z.next_out - outbufp;
657
658 w = write(out, outbufp, len);
659 if (w == -1 || (size_t)w != len) {
660 maybe_warn("write");
661 out_tot = -1;
662 goto out;
663 }
664 out_tot += len;
665 z.next_out = (unsigned char *)outbufp;
666 z.avail_out = BUFLEN;
667
668 if (error == Z_STREAM_END)
669 break;
670 }
671
672 if (deflateEnd(&z) != Z_OK) {
673 maybe_warnx("deflateEnd failed");
674 in_tot = -1;
675 goto out;
676 }
677
678 i = snprintf(outbufp, BUFLEN, "%c%c%c%c%c%c%c%c",
679 (int)crc & 0xff,
680 (int)(crc >> 8) & 0xff,
681 (int)(crc >> 16) & 0xff,
682 (int)(crc >> 24) & 0xff,
683 (int)in_tot & 0xff,
684 (int)(in_tot >> 8) & 0xff,
685 (int)(in_tot >> 16) & 0xff,
686 (int)(in_tot >> 24) & 0xff);
687 if (i != 8)
688 maybe_err("snprintf");
689 #if 0
690 if (in_tot > 0xffffffff)
691 maybe_warn("input file size >= 4GB cannot be saved");
692 #endif
693 if (write(out, outbufp, i) != i) {
694 maybe_warn("write");
695 in_tot = -1;
696 } else
697 out_tot += i;
698
699 out:
700 if (inbufp != NULL)
701 free(inbufp);
702 if (outbufp != NULL)
703 free(outbufp);
704 if (gsizep)
705 *gsizep = out_tot;
706 return in_tot;
707 }
708
709 /*
710 * uncompress input to output then close the input. return the
711 * uncompressed size written, and put the compressed sized read
712 * into `*gsizep'.
713 */
714 static off_t
715 gz_uncompress(int in, int out, char *pre, size_t prelen, off_t *gsizep,
716 const char *filename)
717 {
718 z_stream z;
719 char *outbufp, *inbufp;
720 off_t out_tot = -1, in_tot = 0;
721 uint32_t out_sub_tot = 0;
722 enum {
723 GZSTATE_MAGIC0,
724 GZSTATE_MAGIC1,
725 GZSTATE_METHOD,
726 GZSTATE_FLAGS,
727 GZSTATE_SKIPPING,
728 GZSTATE_EXTRA,
729 GZSTATE_EXTRA2,
730 GZSTATE_EXTRA3,
731 GZSTATE_ORIGNAME,
732 GZSTATE_COMMENT,
733 GZSTATE_HEAD_CRC1,
734 GZSTATE_HEAD_CRC2,
735 GZSTATE_INIT,
736 GZSTATE_READ,
737 GZSTATE_CRC,
738 GZSTATE_LEN,
739 } state = GZSTATE_MAGIC0;
740 int flags = 0, skip_count = 0;
741 int error = Z_STREAM_ERROR, done_reading = 0;
742 uLong crc = 0;
743 ssize_t wr;
744 int needmore = 0;
745
746 #define ADVANCE() { z.next_in++; z.avail_in--; }
747
748 if ((outbufp = malloc(BUFLEN)) == NULL) {
749 maybe_err("malloc failed");
750 goto out2;
751 }
752 if ((inbufp = malloc(BUFLEN)) == NULL) {
753 maybe_err("malloc failed");
754 goto out1;
755 }
756
757 memset(&z, 0, sizeof z);
758 z.avail_in = prelen;
759 z.next_in = (unsigned char *)pre;
760 z.avail_out = BUFLEN;
761 z.next_out = (unsigned char *)outbufp;
762 z.zalloc = NULL;
763 z.zfree = NULL;
764 z.opaque = 0;
765
766 in_tot = prelen;
767 out_tot = 0;
768
769 for (;;) {
770 check_siginfo();
771 if ((z.avail_in == 0 || needmore) && done_reading == 0) {
772 ssize_t in_size;
773
774 if (z.avail_in > 0) {
775 memmove(inbufp, z.next_in, z.avail_in);
776 }
777 z.next_in = (unsigned char *)inbufp;
778 in_size = read(in, z.next_in + z.avail_in,
779 BUFLEN - z.avail_in);
780
781 if (in_size == -1) {
782 maybe_warn("failed to read stdin");
783 goto stop_and_fail;
784 } else if (in_size == 0) {
785 done_reading = 1;
786 }
787 infile_newdata(in_size);
788
789 z.avail_in += in_size;
790 needmore = 0;
791
792 in_tot += in_size;
793 }
794 if (z.avail_in == 0) {
795 if (done_reading && state != GZSTATE_MAGIC0) {
796 maybe_warnx("%s: unexpected end of file",
797 filename);
798 goto stop_and_fail;
799 }
800 goto stop;
801 }
802 switch (state) {
803 case GZSTATE_MAGIC0:
804 if (*z.next_in != GZIP_MAGIC0) {
805 if (in_tot > 0) {
806 maybe_warnx("%s: trailing garbage "
807 "ignored", filename);
808 goto stop;
809 }
810 maybe_warnx("input not gziped (MAGIC0)");
811 exit_value = 2;
812 goto stop_and_fail;
813 }
814 ADVANCE();
815 state++;
816 out_sub_tot = 0;
817 crc = crc32(0L, Z_NULL, 0);
818 break;
819
820 case GZSTATE_MAGIC1:
821 if (*z.next_in != GZIP_MAGIC1 &&
822 *z.next_in != GZIP_OMAGIC1) {
823 maybe_warnx("input not gziped (MAGIC1)");
824 goto stop_and_fail;
825 }
826 ADVANCE();
827 state++;
828 break;
829
830 case GZSTATE_METHOD:
831 if (*z.next_in != Z_DEFLATED) {
832 maybe_warnx("unknown compression method");
833 goto stop_and_fail;
834 }
835 ADVANCE();
836 state++;
837 break;
838
839 case GZSTATE_FLAGS:
840 flags = *z.next_in;
841 ADVANCE();
842 skip_count = 6;
843 state++;
844 break;
845
846 case GZSTATE_SKIPPING:
847 if (skip_count > 0) {
848 skip_count--;
849 ADVANCE();
850 } else
851 state++;
852 break;
853
854 case GZSTATE_EXTRA:
855 if ((flags & EXTRA_FIELD) == 0) {
856 state = GZSTATE_ORIGNAME;
857 break;
858 }
859 skip_count = *z.next_in;
860 ADVANCE();
861 state++;
862 break;
863
864 case GZSTATE_EXTRA2:
865 skip_count |= ((*z.next_in) << 8);
866 ADVANCE();
867 state++;
868 break;
869
870 case GZSTATE_EXTRA3:
871 if (skip_count > 0) {
872 skip_count--;
873 ADVANCE();
874 } else
875 state++;
876 break;
877
878 case GZSTATE_ORIGNAME:
879 if ((flags & ORIG_NAME) == 0) {
880 state++;
881 break;
882 }
883 if (*z.next_in == 0)
884 state++;
885 ADVANCE();
886 break;
887
888 case GZSTATE_COMMENT:
889 if ((flags & COMMENT) == 0) {
890 state++;
891 break;
892 }
893 if (*z.next_in == 0)
894 state++;
895 ADVANCE();
896 break;
897
898 case GZSTATE_HEAD_CRC1:
899 if (flags & HEAD_CRC)
900 skip_count = 2;
901 else
902 skip_count = 0;
903 state++;
904 break;
905
906 case GZSTATE_HEAD_CRC2:
907 if (skip_count > 0) {
908 skip_count--;
909 ADVANCE();
910 } else
911 state++;
912 break;
913
914 case GZSTATE_INIT:
915 if (inflateInit2(&z, -MAX_WBITS) != Z_OK) {
916 maybe_warnx("failed to inflateInit");
917 goto stop_and_fail;
918 }
919 state++;
920 break;
921
922 case GZSTATE_READ:
923 error = inflate(&z, Z_FINISH);
924 switch (error) {
925 /* Z_BUF_ERROR goes with Z_FINISH... */
926 case Z_BUF_ERROR:
927 if (z.avail_out > 0 && !done_reading)
928 continue;
929
930 case Z_STREAM_END:
931 case Z_OK:
932 break;
933
934 case Z_NEED_DICT:
935 maybe_warnx("Z_NEED_DICT error");
936 goto stop_and_fail;
937 case Z_DATA_ERROR:
938 maybe_warnx("data stream error");
939 goto stop_and_fail;
940 case Z_STREAM_ERROR:
941 maybe_warnx("internal stream error");
942 goto stop_and_fail;
943 case Z_MEM_ERROR:
944 maybe_warnx("memory allocation error");
945 goto stop_and_fail;
946
947 default:
948 maybe_warn("unknown error from inflate(): %d",
949 error);
950 }
951 wr = BUFLEN - z.avail_out;
952
953 if (wr != 0) {
954 crc = crc32(crc, (const Bytef *)outbufp, (unsigned)wr);
955 if (
956 #ifndef SMALL
957 /* don't write anything with -t */
958 tflag == 0 &&
959 #endif
960 write(out, outbufp, wr) != wr) {
961 maybe_warn("error writing to output");
962 goto stop_and_fail;
963 }
964
965 out_tot += wr;
966 out_sub_tot += wr;
967 }
968
969 if (error == Z_STREAM_END) {
970 inflateEnd(&z);
971 state++;
972 }
973
974 z.next_out = (unsigned char *)outbufp;
975 z.avail_out = BUFLEN;
976
977 break;
978 case GZSTATE_CRC:
979 {
980 uLong origcrc;
981
982 if (z.avail_in < 4) {
983 if (!done_reading) {
984 needmore = 1;
985 continue;
986 }
987 maybe_warnx("truncated input");
988 goto stop_and_fail;
989 }
990 origcrc = ((unsigned)z.next_in[0] & 0xff) |
991 ((unsigned)z.next_in[1] & 0xff) << 8 |
992 ((unsigned)z.next_in[2] & 0xff) << 16 |
993 ((unsigned)z.next_in[3] & 0xff) << 24;
994 if (origcrc != crc) {
995 maybe_warnx("invalid compressed"
996 " data--crc error");
997 goto stop_and_fail;
998 }
999 }
1000
1001 z.avail_in -= 4;
1002 z.next_in += 4;
1003
1004 if (!z.avail_in && done_reading) {
1005 goto stop;
1006 }
1007 state++;
1008 break;
1009 case GZSTATE_LEN:
1010 {
1011 uLong origlen;
1012
1013 if (z.avail_in < 4) {
1014 if (!done_reading) {
1015 needmore = 1;
1016 continue;
1017 }
1018 maybe_warnx("truncated input");
1019 goto stop_and_fail;
1020 }
1021 origlen = ((unsigned)z.next_in[0] & 0xff) |
1022 ((unsigned)z.next_in[1] & 0xff) << 8 |
1023 ((unsigned)z.next_in[2] & 0xff) << 16 |
1024 ((unsigned)z.next_in[3] & 0xff) << 24;
1025
1026 if (origlen != out_sub_tot) {
1027 maybe_warnx("invalid compressed"
1028 " data--length error");
1029 goto stop_and_fail;
1030 }
1031 }
1032
1033 z.avail_in -= 4;
1034 z.next_in += 4;
1035
1036 if (error < 0) {
1037 maybe_warnx("decompression error");
1038 goto stop_and_fail;
1039 }
1040 state = GZSTATE_MAGIC0;
1041 break;
1042 }
1043 continue;
1044 stop_and_fail:
1045 out_tot = -1;
1046 stop:
1047 break;
1048 }
1049 if (state > GZSTATE_INIT)
1050 inflateEnd(&z);
1051
1052 free(inbufp);
1053 out1:
1054 free(outbufp);
1055 out2:
1056 if (gsizep)
1057 *gsizep = in_tot;
1058 return (out_tot);
1059 }
1060
1061 #ifndef SMALL
1062 /*
1063 * set the owner, mode, flags & utimes using the given file descriptor.
1064 * file is only used in possible warning messages.
1065 */
1066 static void
1067 copymodes(int fd, const struct stat *sbp, const char *file)
1068 {
1069 struct timeval times[2];
1070 struct stat sb;
1071
1072 /*
1073 * If we have no info on the input, give this file some
1074 * default values and return..
1075 */
1076 if (sbp == NULL) {
1077 mode_t mask = umask(022);
1078
1079 (void)fchmod(fd, DEFFILEMODE & ~mask);
1080 (void)umask(mask);
1081 return;
1082 }
1083 sb = *sbp;
1084
1085 /* if the chown fails, remove set-id bits as-per compress(1) */
1086 if (fchown(fd, sb.st_uid, sb.st_gid) < 0) {
1087 if (errno != EPERM)
1088 maybe_warn("couldn't fchown: %s", file);
1089 sb.st_mode &= ~(S_ISUID|S_ISGID);
1090 }
1091
1092 /* we only allow set-id and the 9 normal permission bits */
1093 sb.st_mode &= S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
1094 if (fchmod(fd, sb.st_mode) < 0)
1095 maybe_warn("couldn't fchmod: %s", file);
1096
1097 /* only try flags if they exist already */
1098 if (sb.st_flags != 0 && fchflags(fd, sb.st_flags) < 0)
1099 maybe_warn("couldn't fchflags: %s", file);
1100
1101 TIMESPEC_TO_TIMEVAL(×[0], &sb.st_atimespec);
1102 TIMESPEC_TO_TIMEVAL(×[1], &sb.st_mtimespec);
1103 if (futimes(fd, times) < 0)
1104 maybe_warn("couldn't utimes: %s", file);
1105 }
1106 #endif
1107
1108 /* what sort of file is this? */
1109 static enum filetype
1110 file_gettype(u_char *buf)
1111 {
1112
1113 if (buf[0] == GZIP_MAGIC0 &&
1114 (buf[1] == GZIP_MAGIC1 || buf[1] == GZIP_OMAGIC1))
1115 return FT_GZIP;
1116 else
1117 #ifndef NO_BZIP2_SUPPORT
1118 if (memcmp(buf, BZIP2_MAGIC, 3) == 0 &&
1119 buf[3] >= '0' && buf[3] <= '9')
1120 return FT_BZIP2;
1121 else
1122 #endif
1123 #ifndef NO_COMPRESS_SUPPORT
1124 if (memcmp(buf, Z_MAGIC, 2) == 0)
1125 return FT_Z;
1126 else
1127 #endif
1128 #ifndef NO_PACK_SUPPORT
1129 if (memcmp(buf, PACK_MAGIC, 2) == 0)
1130 return FT_PACK;
1131 else
1132 #endif
1133 #ifndef NO_XZ_SUPPORT
1134 if (memcmp(buf, XZ_MAGIC, 4) == 0) /* XXX: We only have 4 bytes */
1135 return FT_XZ;
1136 else
1137 #endif
1138 return FT_UNKNOWN;
1139 }
1140
1141 #ifndef SMALL
1142 /* check the outfile is OK. */
1143 static int
1144 check_outfile(const char *outfile)
1145 {
1146 struct stat sb;
1147 int ok = 1;
1148
1149 if (lflag == 0 && stat(outfile, &sb) == 0) {
1150 if (fflag)
1151 unlink(outfile);
1152 else if (isatty(STDIN_FILENO)) {
1153 char ans[10] = { 'n', '\0' }; /* default */
1154
1155 fprintf(stderr, "%s already exists -- do you wish to "
1156 "overwrite (y or n)? " , outfile);
1157 (void)fgets(ans, sizeof(ans) - 1, stdin);
1158 if (ans[0] != 'y' && ans[0] != 'Y') {
1159 fprintf(stderr, "\tnot overwriting\n");
1160 ok = 0;
1161 } else
1162 unlink(outfile);
1163 } else {
1164 maybe_warnx("%s already exists -- skipping", outfile);
1165 ok = 0;
1166 }
1167 }
1168 return ok;
1169 }
1170
1171 static void
1172 unlink_input(const char *file, const struct stat *sb)
1173 {
1174 struct stat nsb;
1175
1176 if (kflag)
1177 return;
1178 if (stat(file, &nsb) != 0)
1179 /* Must be gone already */
1180 return;
1181 if (nsb.st_dev != sb->st_dev || nsb.st_ino != sb->st_ino)
1182 /* Definitely a different file */
1183 return;
1184 unlink(file);
1185 }
1186
1187 static void
1188 got_siginfo(int signo)
1189 {
1190
1191 print_info = 1;
1192 }
1193
1194 static void
1195 setup_signals(void)
1196 {
1197
1198 signal(SIGINFO, got_siginfo);
1199 }
1200
1201 static void
1202 infile_newdata(size_t newdata)
1203 {
1204
1205 infile_current += newdata;
1206 }
1207 #endif
1208
1209 static void
1210 infile_set(const char *newinfile, off_t total)
1211 {
1212
1213 if (newinfile)
1214 infile = newinfile;
1215 #ifndef SMALL
1216 infile_total = total;
1217 #endif
1218 }
1219
1220 static void
1221 infile_clear(void)
1222 {
1223
1224 infile = NULL;
1225 #ifndef SMALL
1226 infile_total = infile_current = 0;
1227 #endif
1228 }
1229
1230 static const suffixes_t *
1231 check_suffix(char *file, int xlate)
1232 {
1233 const suffixes_t *s;
1234 int len = strlen(file);
1235 char *sp;
1236
1237 for (s = suffixes; s != suffixes + NUM_SUFFIXES; s++) {
1238 /* if it doesn't fit in "a.suf", don't bother */
1239 if (s->ziplen >= len)
1240 continue;
1241 sp = file + len - s->ziplen;
1242 if (strcmp(s->zipped, sp) != 0)
1243 continue;
1244 if (xlate)
1245 strcpy(sp, s->normal);
1246 return s;
1247 }
1248 return NULL;
1249 }
1250
1251 /*
1252 * compress the given file: create a corresponding .gz file and remove the
1253 * original.
1254 */
1255 static off_t
1256 file_compress(char *file, char *outfile, size_t outsize)
1257 {
1258 int in;
1259 int out;
1260 off_t size, in_size;
1261 #ifndef SMALL
1262 struct stat isb, osb;
1263 const suffixes_t *suff;
1264 #endif
1265
1266 in = open(file, O_RDONLY);
1267 if (in == -1) {
1268 maybe_warn("can't open %s", file);
1269 return -1;
1270 }
1271
1272 #ifndef SMALL
1273 if (fstat(in, &isb) != 0) {
1274 close(in);
1275 maybe_warn("can't stat %s", file);
1276 return -1;
1277 }
1278 infile_set(file, isb.st_size);
1279 #endif
1280
1281 if (cflag == 0) {
1282 #ifndef SMALL
1283 if (isb.st_nlink > 1 && fflag == 0) {
1284 maybe_warnx("%s has %d other link%s -- "
1285 "skipping", file, isb.st_nlink - 1,
1286 isb.st_nlink == 1 ? "" : "s");
1287 close(in);
1288 return -1;
1289 }
1290
1291 if (fflag == 0 && (suff = check_suffix(file, 0))
1292 && suff->zipped[0] != 0) {
1293 maybe_warnx("%s already has %s suffix -- unchanged",
1294 file, suff->zipped);
1295 close(in);
1296 return -1;
1297 }
1298 #endif
1299
1300 /* Add (usually) .gz to filename */
1301 if ((size_t)snprintf(outfile, outsize, "%s%s",
1302 file, suffixes[0].zipped) >= outsize)
1303 memcpy(outfile + outsize - suffixes[0].ziplen - 1,
1304 suffixes[0].zipped, suffixes[0].ziplen + 1);
1305
1306 #ifndef SMALL
1307 if (check_outfile(outfile) == 0) {
1308 close(in);
1309 return -1;
1310 }
1311 #endif
1312 }
1313
1314 if (cflag == 0) {
1315 out = open(outfile, O_WRONLY | O_CREAT | O_EXCL, 0600);
1316 if (out == -1) {
1317 maybe_warn("could not create output: %s", outfile);
1318 fclose(stdin);
1319 return -1;
1320 }
1321 } else
1322 out = STDOUT_FILENO;
1323
1324 in_size = gz_compress(in, out, &size, basename(file), (uint32_t)isb.st_mtime);
1325
1326 (void)close(in);
1327
1328 /*
1329 * If there was an error, in_size will be -1.
1330 * If we compressed to stdout, just return the size.
1331 * Otherwise stat the file and check it is the correct size.
1332 * We only blow away the file if we can stat the output and it
1333 * has the expected size.
1334 */
1335 if (cflag != 0)
1336 return in_size == -1 ? -1 : size;
1337
1338 #ifndef SMALL
1339 if (fstat(out, &osb) != 0) {
1340 maybe_warn("couldn't stat: %s", outfile);
1341 goto bad_outfile;
1342 }
1343
1344 if (osb.st_size != size) {
1345 maybe_warnx("output file: %s wrong size (%" PRIdOFF
1346 " != %" PRIdOFF "), deleting",
1347 outfile, osb.st_size, size);
1348 goto bad_outfile;
1349 }
1350
1351 copymodes(out, &isb, outfile);
1352 #endif
1353 if (close(out) == -1)
1354 maybe_warn("couldn't close output");
1355
1356 /* output is good, ok to delete input */
1357 unlink_input(file, &isb);
1358 return size;
1359
1360 #ifndef SMALL
1361 bad_outfile:
1362 if (close(out) == -1)
1363 maybe_warn("couldn't close output");
1364
1365 maybe_warnx("leaving original %s", file);
1366 unlink(outfile);
1367 return size;
1368 #endif
1369 }
1370
1371 /* uncompress the given file and remove the original */
1372 static off_t
1373 file_uncompress(char *file, char *outfile, size_t outsize)
1374 {
1375 struct stat isb, osb;
1376 off_t size;
1377 ssize_t rbytes;
1378 unsigned char header1[4];
1379 enum filetype method;
1380 int fd, ofd, zfd = -1;
1381 size_t in_size;
1382 #ifndef SMALL
1383 ssize_t rv;
1384 time_t timestamp = 0;
1385 char name[PATH_MAX + 1];
1386 #endif
1387
1388 /* gather the old name info */
1389
1390 fd = open(file, O_RDONLY);
1391 if (fd < 0) {
1392 maybe_warn("can't open %s", file);
1393 goto lose;
1394 }
1395 if (fstat(fd, &isb) != 0) {
1396 close(fd);
1397 maybe_warn("can't stat %s", file);
1398 goto lose;
1399 }
1400 if (S_ISREG(isb.st_mode))
1401 in_size = isb.st_size;
1402 else
1403 in_size = 0;
1404 infile_set(file, in_size);
1405
1406 strlcpy(outfile, file, outsize);
1407 if (check_suffix(outfile, 1) == NULL && !(cflag || lflag)) {
1408 maybe_warnx("%s: unknown suffix -- ignored", file);
1409 goto lose;
1410 }
1411
1412 rbytes = read(fd, header1, sizeof header1);
1413 if (rbytes != sizeof header1) {
1414 /* we don't want to fail here. */
1415 #ifndef SMALL
1416 if (fflag)
1417 goto lose;
1418 #endif
1419 if (rbytes == -1)
1420 maybe_warn("can't read %s", file);
1421 else
1422 goto unexpected_EOF;
1423 goto lose;
1424 }
1425 infile_newdata(rbytes);
1426
1427 method = file_gettype(header1);
1428 #ifndef SMALL
1429 if (fflag == 0 && method == FT_UNKNOWN) {
1430 maybe_warnx("%s: not in gzip format", file);
1431 goto lose;
1432 }
1433
1434 #endif
1435
1436 #ifndef SMALL
1437 if (method == FT_GZIP && Nflag) {
1438 unsigned char ts[4]; /* timestamp */
1439
1440 rv = pread(fd, ts, sizeof ts, GZIP_TIMESTAMP);
1441 if (rv >= 0 && rv < (ssize_t)(sizeof ts))
1442 goto unexpected_EOF;
1443 if (rv == -1) {
1444 if (!fflag)
1445 maybe_warn("can't read %s", file);
1446 goto lose;
1447 }
1448 infile_newdata(rv);
1449 timestamp = ts[3] << 24 | ts[2] << 16 | ts[1] << 8 | ts[0];
1450
1451 if (header1[3] & ORIG_NAME) {
1452 rbytes = pread(fd, name, sizeof(name) - 1, GZIP_ORIGNAME);
1453 if (rbytes < 0) {
1454 maybe_warn("can't read %s", file);
1455 goto lose;
1456 }
1457 if (name[0] != '\0') {
1458 char *dp, *nf;
1459
1460 /* Make sure that name is NUL-terminated */
1461 name[rbytes] = '\0';
1462
1463 /* strip saved directory name */
1464 nf = strrchr(name, '/');
1465 if (nf == NULL)
1466 nf = name;
1467 else
1468 nf++;
1469
1470 /* preserve original directory name */
1471 dp = strrchr(file, '/');
1472 if (dp == NULL)
1473 dp = file;
1474 else
1475 dp++;
1476 snprintf(outfile, outsize, "%.*s%.*s",
1477 (int) (dp - file),
1478 file, (int) rbytes, nf);
1479 }
1480 }
1481 }
1482 #endif
1483 lseek(fd, 0, SEEK_SET);
1484
1485 if (cflag == 0 || lflag) {
1486 #ifndef SMALL
1487 if (isb.st_nlink > 1 && lflag == 0 && fflag == 0) {
1488 maybe_warnx("%s has %d other links -- skipping",
1489 file, isb.st_nlink - 1);
1490 goto lose;
1491 }
1492 if (nflag == 0 && timestamp)
1493 isb.st_mtime = timestamp;
1494 if (check_outfile(outfile) == 0)
1495 goto lose;
1496 #endif
1497 }
1498
1499 if (cflag)
1500 zfd = STDOUT_FILENO;
1501 else if (lflag)
1502 zfd = -1;
1503 else {
1504 zfd = open(outfile, O_WRONLY|O_CREAT|O_EXCL, 0600);
1505 if (zfd == STDOUT_FILENO) {
1506 /* We won't close STDOUT_FILENO later... */
1507 zfd = dup(zfd);
1508 close(STDOUT_FILENO);
1509 }
1510 if (zfd == -1) {
1511 maybe_warn("can't open %s", outfile);
1512 goto lose;
1513 }
1514 }
1515
1516 switch (method) {
1517 #ifndef NO_BZIP2_SUPPORT
1518 case FT_BZIP2:
1519 /* XXX */
1520 if (lflag) {
1521 maybe_warnx("no -l with bzip2 files");
1522 goto lose;
1523 }
1524
1525 size = unbzip2(fd, zfd, NULL, 0, NULL);
1526 break;
1527 #endif
1528
1529 #ifndef NO_COMPRESS_SUPPORT
1530 case FT_Z: {
1531 FILE *in, *out;
1532
1533 /* XXX */
1534 if (lflag) {
1535 maybe_warnx("no -l with Lempel-Ziv files");
1536 goto lose;
1537 }
1538
1539 if ((in = zdopen(fd)) == NULL) {
1540 maybe_warn("zdopen for read: %s", file);
1541 goto lose;
1542 }
1543
1544 out = fdopen(dup(zfd), "w");
1545 if (out == NULL) {
1546 maybe_warn("fdopen for write: %s", outfile);
1547 fclose(in);
1548 goto lose;
1549 }
1550
1551 size = zuncompress(in, out, NULL, 0, NULL);
1552 /* need to fclose() if ferror() is true... */
1553 if (ferror(in) | fclose(in)) {
1554 maybe_warn("failed infile fclose");
1555 unlink(outfile);
1556 (void)fclose(out);
1557 }
1558 if (fclose(out) != 0) {
1559 maybe_warn("failed outfile fclose");
1560 unlink(outfile);
1561 goto lose;
1562 }
1563 break;
1564 }
1565 #endif
1566
1567 #ifndef NO_PACK_SUPPORT
1568 case FT_PACK:
1569 if (lflag) {
1570 maybe_warnx("no -l with packed files");
1571 goto lose;
1572 }
1573
1574 size = unpack(fd, zfd, NULL, 0, NULL);
1575 break;
1576 #endif
1577
1578 #ifndef NO_XZ_SUPPORT
1579 case FT_XZ:
1580 if (lflag) {
1581 maybe_warnx("no -l with xz files");
1582 goto lose;
1583 }
1584
1585 size = unxz(fd, zfd, NULL, 0, NULL);
1586 break;
1587 #endif
1588
1589 #ifndef SMALL
1590 case FT_UNKNOWN:
1591 if (lflag) {
1592 maybe_warnx("no -l for unknown filetypes");
1593 goto lose;
1594 }
1595 size = cat_fd(NULL, 0, NULL, fd);
1596 break;
1597 #endif
1598 default:
1599 if (lflag) {
1600 print_list(fd, in_size, outfile, isb.st_mtime);
1601 close(fd);
1602 return -1; /* XXX */
1603 }
1604
1605 size = gz_uncompress(fd, zfd, NULL, 0, NULL, file);
1606 break;
1607 }
1608
1609 if (close(fd) != 0)
1610 maybe_warn("couldn't close input");
1611 if (zfd != STDOUT_FILENO && close(zfd) != 0)
1612 maybe_warn("couldn't close output");
1613
1614 if (size == -1) {
1615 if (cflag == 0)
1616 unlink(outfile);
1617 maybe_warnx("%s: uncompress failed", file);
1618 return -1;
1619 }
1620
1621 /* if testing, or we uncompressed to stdout, this is all we need */
1622 #ifndef SMALL
1623 if (tflag)
1624 return size;
1625 #endif
1626 /* if we are uncompressing to stdin, don't remove the file. */
1627 if (cflag)
1628 return size;
1629
1630 /*
1631 * if we create a file...
1632 */
1633 /*
1634 * if we can't stat the file don't remove the file.
1635 */
1636
1637 ofd = open(outfile, O_RDWR, 0);
1638 if (ofd == -1) {
1639 maybe_warn("couldn't open (leaving original): %s",
1640 outfile);
1641 return -1;
1642 }
1643 if (fstat(ofd, &osb) != 0) {
1644 maybe_warn("couldn't stat (leaving original): %s",
1645 outfile);
1646 close(ofd);
1647 return -1;
1648 }
1649 if (osb.st_size != size) {
1650 maybe_warnx("stat gave different size: %" PRIdOFF
1651 " != %" PRIdOFF " (leaving original)",
1652 size, osb.st_size);
1653 close(ofd);
1654 unlink(outfile);
1655 return -1;
1656 }
1657 unlink_input(file, &isb);
1658 #ifndef SMALL
1659 copymodes(ofd, &isb, outfile);
1660 #endif
1661 close(ofd);
1662 return size;
1663
1664 unexpected_EOF:
1665 maybe_warnx("%s: unexpected end of file", file);
1666 lose:
1667 if (fd != -1)
1668 close(fd);
1669 if (zfd != -1 && zfd != STDOUT_FILENO)
1670 close(fd);
1671 return -1;
1672 }
1673
1674 #ifndef SMALL
1675 static void
1676 check_siginfo(void)
1677 {
1678 if (print_info == 0)
1679 return;
1680 if (infile) {
1681 if (infile_total) {
1682 int pcent = (int)((100.0 * infile_current) / infile_total);
1683
1684 fprintf(stderr, "%s: done %llu/%llu bytes %d%%\n",
1685 infile, (unsigned long long)infile_current,
1686 (unsigned long long)infile_total, pcent);
1687 } else
1688 fprintf(stderr, "%s: done %llu bytes\n",
1689 infile, (unsigned long long)infile_current);
1690 }
1691 print_info = 0;
1692 }
1693
1694 static off_t
1695 cat_fd(unsigned char * prepend, size_t count, off_t *gsizep, int fd)
1696 {
1697 char buf[BUFLEN];
1698 off_t in_tot;
1699 ssize_t w;
1700
1701 in_tot = count;
1702 w = write(STDOUT_FILENO, prepend, count);
1703 if (w == -1 || (size_t)w != count) {
1704 maybe_warn("write to stdout");
1705 return -1;
1706 }
1707 for (;;) {
1708 ssize_t rv;
1709
1710 rv = read(fd, buf, sizeof buf);
1711 if (rv == 0)
1712 break;
1713 if (rv < 0) {
1714 maybe_warn("read from fd %d", fd);
1715 break;
1716 }
1717 infile_newdata(rv);
1718
1719 if (write(STDOUT_FILENO, buf, rv) != rv) {
1720 maybe_warn("write to stdout");
1721 break;
1722 }
1723 in_tot += rv;
1724 }
1725
1726 if (gsizep)
1727 *gsizep = in_tot;
1728 return (in_tot);
1729 }
1730 #endif
1731
1732 static void
1733 handle_stdin(void)
1734 {
1735 struct stat isb;
1736 unsigned char header1[4];
1737 size_t in_size;
1738 off_t usize, gsize;
1739 enum filetype method;
1740 ssize_t bytes_read;
1741 #ifndef NO_COMPRESS_SUPPORT
1742 FILE *in;
1743 #endif
1744
1745 #ifndef SMALL
1746 if (fflag == 0 && lflag == 0 && isatty(STDIN_FILENO)) {
1747 maybe_warnx("standard input is a terminal -- ignoring");
1748 goto out;
1749 }
1750 #endif
1751
1752 if (fstat(STDIN_FILENO, &isb) < 0) {
1753 maybe_warn("fstat");
1754 goto out;
1755 }
1756 if (S_ISREG(isb.st_mode))
1757 in_size = isb.st_size;
1758 else
1759 in_size = 0;
1760 infile_set("(stdin)", in_size);
1761
1762 if (lflag) {
1763 print_list(STDIN_FILENO, in_size, infile, isb.st_mtime);
1764 goto out;
1765 }
1766
1767 bytes_read = read_retry(STDIN_FILENO, header1, sizeof header1);
1768 if (bytes_read == -1) {
1769 maybe_warn("can't read stdin");
1770 goto out;
1771 } else if (bytes_read != sizeof(header1)) {
1772 maybe_warnx("(stdin): unexpected end of file");
1773 goto out;
1774 }
1775
1776 method = file_gettype(header1);
1777 switch (method) {
1778 default:
1779 #ifndef SMALL
1780 if (fflag == 0) {
1781 maybe_warnx("unknown compression format");
1782 goto out;
1783 }
1784 usize = cat_fd(header1, sizeof header1, &gsize, STDIN_FILENO);
1785 break;
1786 #endif
1787 case FT_GZIP:
1788 usize = gz_uncompress(STDIN_FILENO, STDOUT_FILENO,
1789 (char *)header1, sizeof header1, &gsize, "(stdin)");
1790 break;
1791 #ifndef NO_BZIP2_SUPPORT
1792 case FT_BZIP2:
1793 usize = unbzip2(STDIN_FILENO, STDOUT_FILENO,
1794 (char *)header1, sizeof header1, &gsize);
1795 break;
1796 #endif
1797 #ifndef NO_COMPRESS_SUPPORT
1798 case FT_Z:
1799 if ((in = zdopen(STDIN_FILENO)) == NULL) {
1800 maybe_warnx("zopen of stdin");
1801 goto out;
1802 }
1803
1804 usize = zuncompress(in, stdout, (char *)header1,
1805 sizeof header1, &gsize);
1806 fclose(in);
1807 break;
1808 #endif
1809 #ifndef NO_PACK_SUPPORT
1810 case FT_PACK:
1811 usize = unpack(STDIN_FILENO, STDOUT_FILENO,
1812 (char *)header1, sizeof header1, &gsize);
1813 break;
1814 #endif
1815 #ifndef NO_XZ_SUPPORT
1816 case FT_XZ:
1817 usize = unxz(STDIN_FILENO, STDOUT_FILENO,
1818 (char *)header1, sizeof header1, &gsize);
1819 break;
1820 #endif
1821 }
1822
1823 #ifndef SMALL
1824 if (vflag && !tflag && usize != -1 && gsize != -1)
1825 print_verbage(NULL, NULL, usize, gsize);
1826 if (vflag && tflag)
1827 print_test("(stdin)", usize != -1);
1828 #else
1829 (void)&usize;
1830 #endif
1831
1832 out:
1833 infile_clear();
1834 }
1835
1836 static void
1837 handle_stdout(void)
1838 {
1839 off_t gsize;
1840 #ifndef SMALL
1841 off_t usize;
1842 struct stat sb;
1843 time_t systime;
1844 uint32_t mtime;
1845 int ret;
1846
1847 infile_set("(stdout)", 0);
1848
1849 if (fflag == 0 && isatty(STDOUT_FILENO)) {
1850 maybe_warnx("standard output is a terminal -- ignoring");
1851 return;
1852 }
1853
1854 /* If stdin is a file use its mtime, otherwise use current time */
1855 ret = fstat(STDIN_FILENO, &sb);
1856 if (ret < 0) {
1857 maybe_warn("Can't stat stdin");
1858 return;
1859 }
1860
1861 if (S_ISREG(sb.st_mode)) {
1862 infile_set("(stdout)", sb.st_size);
1863 mtime = (uint32_t)sb.st_mtime;
1864 } else {
1865 systime = time(NULL);
1866 if (systime == -1) {
1867 maybe_warn("time");
1868 return;
1869 }
1870 mtime = (uint32_t)systime;
1871 }
1872
1873 usize =
1874 #endif
1875 gz_compress(STDIN_FILENO, STDOUT_FILENO, &gsize, "", mtime);
1876 #ifndef SMALL
1877 if (vflag && !tflag && usize != -1 && gsize != -1)
1878 print_verbage(NULL, NULL, usize, gsize);
1879 #endif
1880 }
1881
1882 /* do what is asked for, for the path name */
1883 static void
1884 handle_pathname(char *path)
1885 {
1886 char *opath = path, *s = NULL;
1887 ssize_t len;
1888 int slen;
1889 struct stat sb;
1890
1891 /* check for stdout/stdin */
1892 if (path[0] == '-' && path[1] == '\0') {
1893 if (dflag)
1894 handle_stdin();
1895 else
1896 handle_stdout();
1897 return;
1898 }
1899
1900 retry:
1901 if (stat(path, &sb) != 0) {
1902 /* lets try <path>.gz if we're decompressing */
1903 if (dflag && s == NULL && errno == ENOENT) {
1904 len = strlen(path);
1905 slen = suffixes[0].ziplen;
1906 s = malloc(len + slen + 1);
1907 if (s == NULL)
1908 maybe_err("malloc");
1909 memcpy(s, path, len);
1910 memcpy(s + len, suffixes[0].zipped, slen + 1);
1911 path = s;
1912 goto retry;
1913 }
1914 maybe_warn("can't stat: %s", opath);
1915 goto out;
1916 }
1917
1918 if (S_ISDIR(sb.st_mode)) {
1919 #ifndef SMALL
1920 if (rflag)
1921 handle_dir(path);
1922 else
1923 #endif
1924 maybe_warnx("%s is a directory", path);
1925 goto out;
1926 }
1927
1928 if (S_ISREG(sb.st_mode))
1929 handle_file(path, &sb);
1930 else
1931 maybe_warnx("%s is not a regular file", path);
1932
1933 out:
1934 if (s)
1935 free(s);
1936 }
1937
1938 /* compress/decompress a file */
1939 static void
1940 handle_file(char *file, struct stat *sbp)
1941 {
1942 off_t usize, gsize;
1943 char outfile[PATH_MAX];
1944
1945 infile_set(file, sbp->st_size);
1946 if (dflag) {
1947 usize = file_uncompress(file, outfile, sizeof(outfile));
1948 #ifndef SMALL
1949 if (vflag && tflag)
1950 print_test(file, usize != -1);
1951 #endif
1952 if (usize == -1)
1953 return;
1954 gsize = sbp->st_size;
1955 } else {
1956 gsize = file_compress(file, outfile, sizeof(outfile));
1957 if (gsize == -1)
1958 return;
1959 usize = sbp->st_size;
1960 }
1961 infile_clear();
1962
1963 #ifndef SMALL
1964 if (vflag && !tflag)
1965 print_verbage(file, (cflag) ? NULL : outfile, usize, gsize);
1966 #endif
1967 }
1968
1969 #ifndef SMALL
1970 /* this is used with -r to recursively descend directories */
1971 static void
1972 handle_dir(char *dir)
1973 {
1974 char *path_argv[2];
1975 FTS *fts;
1976 FTSENT *entry;
1977
1978 path_argv[0] = dir;
1979 path_argv[1] = 0;
1980 fts = fts_open(path_argv, FTS_PHYSICAL, NULL);
1981 if (fts == NULL) {
1982 warn("couldn't fts_open %s", dir);
1983 return;
1984 }
1985
1986 while ((entry = fts_read(fts))) {
1987 switch(entry->fts_info) {
1988 case FTS_D:
1989 case FTS_DP:
1990 continue;
1991
1992 case FTS_DNR:
1993 case FTS_ERR:
1994 case FTS_NS:
1995 maybe_warn("%s", entry->fts_path);
1996 continue;
1997 case FTS_F:
1998 handle_file(entry->fts_name, entry->fts_statp);
1999 }
2000 }
2001 (void)fts_close(fts);
2002 }
2003 #endif
2004
2005 /* print a ratio - size reduction as a fraction of uncompressed size */
2006 static void
2007 print_ratio(off_t in, off_t out, FILE *where)
2008 {
2009 int percent10; /* 10 * percent */
2010 off_t diff;
2011 char buff[8];
2012 int len;
2013
2014 diff = in - out/2;
2015 if (in == 0 && out == 0)
2016 percent10 = 0;
2017 else if (diff < 0)
2018 /*
2019 * Output is more than double size of input! print -99.9%
2020 * Quite possibly we've failed to get the original size.
2021 */
2022 percent10 = -999;
2023 else {
2024 /*
2025 * We only need 12 bits of result from the final division,
2026 * so reduce the values until a 32bit division will suffice.
2027 */
2028 while (in > 0x100000) {
2029 diff >>= 1;
2030 in >>= 1;
2031 }
2032 if (in != 0)
2033 percent10 = ((u_int)diff * 2000) / (u_int)in - 1000;
2034 else
2035 percent10 = 0;
2036 }
2037
2038 len = snprintf(buff, sizeof buff, "%2.2d.", percent10);
2039 /* Move the '.' to before the last digit */
2040 buff[len - 1] = buff[len - 2];
2041 buff[len - 2] = '.';
2042 fprintf(where, "%5s%%", buff);
2043 }
2044
2045 #ifndef SMALL
2046 /* print compression statistics, and the new name (if there is one!) */
2047 static void
2048 print_verbage(const char *file, const char *nfile, off_t usize, off_t gsize)
2049 {
2050 if (file)
2051 fprintf(stderr, "%s:%s ", file,
2052 strlen(file) < 7 ? "\t\t" : "\t");
2053 print_ratio(usize, gsize, stderr);
2054 if (nfile)
2055 fprintf(stderr, " -- replaced with %s", nfile);
2056 fprintf(stderr, "\n");
2057 fflush(stderr);
2058 }
2059
2060 /* print test results */
2061 static void
2062 print_test(const char *file, int ok)
2063 {
2064
2065 if (exit_value == 0 && ok == 0)
2066 exit_value = 1;
2067 fprintf(stderr, "%s:%s %s\n", file,
2068 strlen(file) < 7 ? "\t\t" : "\t", ok ? "OK" : "NOT OK");
2069 fflush(stderr);
2070 }
2071 #endif
2072
2073 /* print a file's info ala --list */
2074 /* eg:
2075 compressed uncompressed ratio uncompressed_name
2076 354841 1679360 78.8% /usr/pkgsrc/distfiles/libglade-2.0.1.tar
2077 */
2078 static void
2079 print_list(int fd, off_t out, const char *outfile, time_t ts)
2080 {
2081 static int first = 1;
2082 #ifndef SMALL
2083 static off_t in_tot, out_tot;
2084 uint32_t crc = 0;
2085 #endif
2086 off_t in = 0, rv;
2087
2088 if (first) {
2089 #ifndef SMALL
2090 if (vflag)
2091 printf("method crc date time ");
2092 #endif
2093 if (qflag == 0)
2094 printf(" compressed uncompressed "
2095 "ratio uncompressed_name\n");
2096 }
2097 first = 0;
2098
2099 /* print totals? */
2100 #ifndef SMALL
2101 if (fd == -1) {
2102 in = in_tot;
2103 out = out_tot;
2104 } else
2105 #endif
2106 {
2107 /* read the last 4 bytes - this is the uncompressed size */
2108 rv = lseek(fd, (off_t)(-8), SEEK_END);
2109 if (rv != -1) {
2110 unsigned char buf[8];
2111 uint32_t usize;
2112
2113 rv = read(fd, (char *)buf, sizeof(buf));
2114 if (rv == -1)
2115 maybe_warn("read of uncompressed size");
2116 else if (rv != sizeof(buf))
2117 maybe_warnx("read of uncompressed size");
2118
2119 else {
2120 usize = buf[4] | buf[5] << 8 |
2121 buf[6] << 16 | buf[7] << 24;
2122 in = (off_t)usize;
2123 #ifndef SMALL
2124 crc = buf[0] | buf[1] << 8 |
2125 buf[2] << 16 | buf[3] << 24;
2126 #endif
2127 }
2128 }
2129 }
2130
2131 #ifndef SMALL
2132 if (vflag && fd == -1)
2133 printf(" ");
2134 else if (vflag) {
2135 char *date = ctime(&ts);
2136
2137 /* skip the day, 1/100th second, and year */
2138 date += 4;
2139 date[12] = 0;
2140 printf("%5s %08x %11s ", "defla"/*XXX*/, crc, date);
2141 }
2142 in_tot += in;
2143 out_tot += out;
2144 #endif
2145 printf("%12llu %12llu ", (unsigned long long)out, (unsigned long long)in);
2146 print_ratio(in, out, stdout);
2147 printf(" %s\n", outfile);
2148 }
2149
2150 /* display the usage of NetBSD gzip */
2151 static void
2152 usage(void)
2153 {
2154
2155 fprintf(stderr, "%s\n", gzip_version);
2156 fprintf(stderr,
2157 "usage: %s [-" OPT_LIST "] [<file> [<file> ...]]\n"
2158 #ifndef SMALL
2159 " -1 --fast fastest (worst) compression\n"
2160 " -2 .. -8 set compression level\n"
2161 " -9 --best best (slowest) compression\n"
2162 " -c --stdout write to stdout, keep original files\n"
2163 " --to-stdout\n"
2164 " -d --decompress uncompress files\n"
2165 " --uncompress\n"
2166 " -f --force force overwriting & compress links\n"
2167 " -h --help display this help\n"
2168 " -k --keep don't delete input files during operation\n"
2169 " -l --list list compressed file contents\n"
2170 " -N --name save or restore original file name and time stamp\n"
2171 " -n --no-name don't save original file name or time stamp\n"
2172 " -q --quiet output no warnings\n"
2173 " -r --recursive recursively compress files in directories\n"
2174 " -S .suf use suffix .suf instead of .gz\n"
2175 " --suffix .suf\n"
2176 " -t --test test compressed file\n"
2177 " -V --version display program version\n"
2178 " -v --verbose print extra statistics\n",
2179 #else
2180 ,
2181 #endif
2182 getprogname());
2183 exit(0);
2184 }
2185
2186 /* display the version of NetBSD gzip */
2187 static void
2188 display_version(void)
2189 {
2190
2191 fprintf(stderr, "%s\n", gzip_version);
2192 exit(0);
2193 }
2194
2195 #ifndef NO_BZIP2_SUPPORT
2196 #include "unbzip2.c"
2197 #endif
2198 #ifndef NO_COMPRESS_SUPPORT
2199 #include "zuncompress.c"
2200 #endif
2201 #ifndef NO_PACK_SUPPORT
2202 #include "unpack.c"
2203 #endif
2204 #ifndef NO_XZ_SUPPORT
2205 #include "unxz.c"
2206 #endif
2207
2208 static ssize_t
2209 read_retry(int fd, void *buf, size_t sz)
2210 {
2211 char *cp = buf;
2212 size_t left = MIN(sz, (size_t) SSIZE_MAX);
2213
2214 while (left > 0) {
2215 ssize_t ret;
2216
2217 ret = read(fd, cp, left);
2218 if (ret == -1) {
2219 return ret;
2220 } else if (ret == 0) {
2221 break; /* EOF */
2222 }
2223 cp += ret;
2224 left -= ret;
2225 }
2226
2227 return sz - left;
2228 }
2229