-
Notifications
You must be signed in to change notification settings - Fork 0
/
gzip.c
2455 lines (2181 loc) · 51.3 KB
/
gzip.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* $NetBSD: gzip.c,v 1.125 2024/04/02 12:42:35 christos Exp $ */
/*
* Copyright (c) 1997-2024 Matthew R. Green
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#if HAVE_NBTOOL_CONFIG_H
#include "nbtool_config.h"
#endif
/*
* gzip.c -- GPL free gzip using zlib.
*
* RFC 1950 covers the zlib format
* RFC 1951 covers the deflate format
* RFC 1952 covers the gzip format
*
* TODO:
* - use mmap where possible
* - handle some signals better (remove outfile?)
* - make bzip2/compress -v/-t/-l support work as well as possible
*/
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <inttypes.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <zlib.h>
#include <fts.h>
#include <libgen.h>
#include <stdarg.h>
#include <getopt.h>
#include <time.h>
#include <paths.h>
#include <sys/attr.h>
#include <copyfile.h>
#include <get_compat.h>
#define nitems(x) (sizeof((x)) / sizeof((x)[0]))
#ifndef PRIdOFF
#define PRIdOFF PRId64
#endif
/* what type of file are we dealing with */
enum filetype {
FT_GZIP,
#ifndef NO_BZIP2_SUPPORT
FT_BZIP2,
#endif
#ifndef NO_COMPRESS_SUPPORT
FT_Z,
#endif
#ifndef NO_PACK_SUPPORT
FT_PACK,
#endif
#ifndef NO_XZ_SUPPORT
FT_XZ,
#endif
#ifndef NO_LZ_SUPPORT
FT_LZ,
#endif
#ifndef NO_ZSTD_SUPPORT
FT_ZSTD,
#endif
FT_LAST,
FT_UNKNOWN
};
#ifndef NO_BZIP2_SUPPORT
#include <bzlib.h>
#define BZ2_SUFFIX ".bz2"
#define BZIP2_MAGIC "\102\132\150"
#endif
#ifndef NO_COMPRESS_SUPPORT
#define Z_SUFFIX ".Z"
#define Z_MAGIC "\037\235"
#endif
#ifndef NO_PACK_SUPPORT
#define PACK_MAGIC "\037\036"
#endif
#ifndef NO_XZ_SUPPORT
#include <lzma.h>
#define XZ_SUFFIX ".xz"
#define XZ_MAGIC "\3757zXZ"
#endif
#ifndef NO_LZ_SUPPORT
#define LZ_SUFFIX ".lz"
#define LZ_MAGIC "LZIP"
#endif
#ifndef NO_ZSTD_SUPPORT
#include <zstd.h>
#include <stdbool.h>
#define ZSTD_SUFFIX ".zst"
#define ZSTD_MAGIC "\050\265\057\375"
#endif
#define GZ_SUFFIX ".gz"
#define BUFLEN (64 * 1024)
#define GZIP_MAGIC0 0x1F
#define GZIP_MAGIC1 0x8B
#define GZIP_OMAGIC1 0x9E
#define GZIP_TIMESTAMP (off_t)4
#define GZIP_ORIGNAME (off_t)10
#define HEAD_CRC 0x02
#define EXTRA_FIELD 0x04
#define ORIG_NAME 0x08
#define COMMENT 0x10
#define OS_CODE 3 /* Unix */
typedef struct {
const char *zipped;
int ziplen;
const char *normal; /* for unzip - must not be longer than zipped */
} suffixes_t;
static suffixes_t suffixes[] = {
#define SUFFIX(Z, N) {Z, sizeof Z - 1, N}
SUFFIX(GZ_SUFFIX, ""), /* Overwritten by -S .xxx */
#ifndef SMALL
SUFFIX(GZ_SUFFIX, ""),
SUFFIX(".z", ""),
SUFFIX("-gz", ""),
SUFFIX("-z", ""),
SUFFIX("_z", ""),
SUFFIX(".taz", ".tar"),
SUFFIX(".tgz", ".tar"),
#ifndef NO_BZIP2_SUPPORT
SUFFIX(BZ2_SUFFIX, ""),
#endif
#ifndef NO_COMPRESS_SUPPORT
SUFFIX(Z_SUFFIX, ""),
#endif
#ifndef NO_XZ_SUPPORT
SUFFIX(XZ_SUFFIX, ""),
#endif
#ifndef NO_LZ_SUPPORT
SUFFIX(LZ_SUFFIX, ""),
#endif
#ifndef NO_ZSTD_SUPPORT
SUFFIX(ZSTD_SUFFIX, ""),
#endif
SUFFIX(GZ_SUFFIX, ""), /* Overwritten by -S "" */
#endif /* SMALL */
#undef SUFFIX
};
#define NUM_SUFFIXES (sizeof suffixes / sizeof suffixes[0])
#define SUFFIX_MAXLEN 30
static const char gzip_version[] = "NetBSD gzip 20240203";
static const char gzip_copyright[] = \
" Copyright (c) 1997, 1998, 2003, 2004, 2006 Matthew R. Green\n"
" All rights reserved.\n"
"\n"
" Redistribution and use in source and binary forms, with or without\n"
" modification, are permitted provided that the following conditions\n"
" are met:\n"
" 1. Redistributions of source code must retain the above copyright\n"
" notice, this list of conditions and the following disclaimer.\n"
" 2. Redistributions in binary form must reproduce the above copyright\n"
" notice, this list of conditions and the following disclaimer in the\n"
" documentation and/or other materials provided with the distribution.\n"
"\n"
" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR\n"
" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\n"
" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.\n"
" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,\n"
" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,\n"
" BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\n"
" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED\n"
" AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,\n"
" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY\n"
" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF\n"
" SUCH DAMAGE.";
static int cflag; /* stdout mode */
static int dflag; /* decompress mode */
static int lflag; /* list mode */
static int numflag = 6; /* gzip -1..-9 value */
#ifndef SMALL
static int fflag; /* force mode */
static int kflag; /* don't delete input files */
static int nflag; /* don't save name/timestamp */
static int Nflag; /* don't restore name/timestamp */
static int qflag; /* quiet mode */
static int rflag; /* recursive mode */
static int tflag; /* test */
static int vflag; /* verbose mode */
#ifdef SIGINFO
static sig_atomic_t print_info = 0;
#endif
#else
#define qflag 0
#define tflag 0
#endif
static int exit_value = 0; /* exit value */
static const char *infile; /* name of file coming in */
static bool zcat;
static void maybe_err(const char *fmt, ...) __printflike(1, 2) __dead;
#if !defined(NO_BZIP2_SUPPORT) || !defined(NO_PACK_SUPPORT) || \
!defined(NO_XZ_SUPPORT) || !defined(NO_ZSTD_SUPPORT)
static void maybe_errx(const char *fmt, ...) __printflike(1, 2) __dead;
#endif
static void maybe_warn(const char *fmt, ...) __printflike(1, 2);
static void maybe_warnx(const char *fmt, ...) __printflike(1, 2);
static enum filetype file_gettype(u_char *);
#ifdef SMALL
#define gz_compress(if, of, sz, fn, tm) gz_compress(if, of, sz)
#endif
static off_t gz_compress(int, int, off_t *, const char *, uint32_t);
static off_t gz_uncompress(int, int, char *, size_t, off_t *, const char *);
static off_t file_compress(char *, char *, size_t);
static off_t file_uncompress(char *, char *, size_t);
static void handle_pathname(char *);
static void handle_file(char *, struct stat *);
static void handle_stdin(void);
static void handle_stdout(void);
static void print_ratio(off_t, off_t, FILE *);
static void print_list(int fd, off_t, const char *, time_t);
__dead static void usage(void);
__dead static void display_version(void);
__dead static void display_license(void);
static const suffixes_t *check_suffix(char *, int);
static ssize_t read_retry(int, void *, size_t);
static ssize_t write_retry(int, const void *, size_t);
static void print_list_out(off_t, off_t, const char*);
#ifdef SMALL
#define infile_set(f,t) infile_set(f)
#endif
static void infile_set(const char *newinfile, off_t total);
#ifdef SMALL
#define unlink_input(f, sb) unlink(f)
#define check_siginfo() /* nothing */
#define setup_signals() /* nothing */
#define infile_newdata(t) /* nothing */
#else
static off_t infile_total; /* total expected to read/write */
static off_t infile_current; /* current read/write */
#ifdef SIGINFO
static void check_siginfo(void);
#else
#define check_siginfo() /* nothing */
#endif
static off_t cat_fd(unsigned char *, size_t, off_t *, int fd);
static void prepend_gzip(char *, int *, char ***);
static void handle_dir(char *);
static void print_verbage(const char *, const char *, off_t, off_t);
static void print_test(const char *, int);
static void copymodes(int fd, const struct stat *, const char *file);
static int check_outfile(const char *outfile);
static void setup_signals(void);
static void infile_newdata(size_t newdata);
static void infile_clear(void);
#endif
#ifndef NO_BZIP2_SUPPORT
static off_t unbzip2(int, int, char *, size_t, off_t *);
#endif
#ifndef NO_COMPRESS_SUPPORT
static FILE *zdopen(int);
static off_t zuncompress(FILE *, FILE *, char *, size_t, off_t *);
#endif
#ifndef NO_PACK_SUPPORT
static off_t unpack(int, int, char *, size_t, off_t *);
#endif
#ifndef NO_XZ_SUPPORT
static off_t unxz(int, int, char *, size_t, off_t *);
static off_t unxz_len(int);
#endif
#ifndef NO_LZ_SUPPORT
static off_t unlz(int, int, char *, size_t, off_t *);
#endif
#ifndef NO_ZSTD_SUPPORT
static off_t unzstd(int, int, char *, size_t, off_t *);
#endif
#ifdef SMALL
#define getopt_long(a,b,c,d,e) getopt(a,b,c)
#else
static const struct option longopts[] = {
{ "stdout", no_argument, 0, 'c' },
{ "to-stdout", no_argument, 0, 'c' },
{ "decompress", no_argument, 0, 'd' },
{ "uncompress", no_argument, 0, 'd' },
{ "force", no_argument, 0, 'f' },
{ "help", no_argument, 0, 'h' },
{ "keep", no_argument, 0, 'k' },
{ "list", no_argument, 0, 'l' },
{ "no-name", no_argument, 0, 'n' },
{ "name", no_argument, 0, 'N' },
{ "quiet", no_argument, 0, 'q' },
{ "recursive", no_argument, 0, 'r' },
{ "suffix", required_argument, 0, 'S' },
{ "test", no_argument, 0, 't' },
{ "verbose", no_argument, 0, 'v' },
{ "version", no_argument, 0, 'V' },
{ "fast", no_argument, 0, '1' },
{ "best", no_argument, 0, '9' },
{ "ascii", no_argument, 0, 'a' },
{ "license", no_argument, 0, 'L' },
{ NULL, no_argument, 0, 0 },
};
#endif
int
main(int argc, char **argv)
{
const char *progname = getprogname();
#ifndef SMALL
char *gzip;
int len;
#endif
int ch;
setup_signals();
#ifndef SMALL
if ((gzip = getenv("GZIP")) != NULL)
prepend_gzip(gzip, &argc, &argv);
#endif
/*
* XXX
* handle being called `gunzip', `zcat' and `gzcat'
*/
if (strcmp(progname, "gunzip") == 0)
dflag = 1;
else if (strcmp(progname, "zcat") == 0 ||
strcmp(progname, "gzcat") == 0)
dflag = cflag = 1;
if (strcmp(progname, "zcat") == 0) {
zcat = true;
}
#ifdef SMALL
#define OPT_LIST "123456789cdhlVn"
#else
#define OPT_LIST "123456789acdfhklLNnqrS:tVv"
#endif
while ((ch = getopt_long(argc, argv, OPT_LIST, longopts, NULL)) != -1) {
switch (ch) {
case '1': case '2': case '3':
case '4': case '5': case '6':
case '7': case '8': case '9':
numflag = ch - '0';
break;
case 'c':
cflag = 1;
break;
case 'd':
dflag = 1;
break;
case 'l':
lflag = 1;
dflag = 1;
break;
case 'V':
display_version();
/* NOTREACHED */
case 'a':
fprintf(stderr, "%s: option --ascii ignored on this system\n", progname);
break;
#ifndef SMALL
case 'f':
fflag = 1;
break;
case 'k':
kflag = 1;
break;
case 'L':
display_license();
/* NOT REACHED */
case 'N':
nflag = 0;
Nflag = 1;
break;
case 'n':
nflag = 1;
Nflag = 0;
break;
case 'q':
qflag = 1;
break;
case 'r':
rflag = 1;
break;
case 'S':
len = strlen(optarg);
if (len != 0) {
if (len > SUFFIX_MAXLEN)
errx(1, "incorrect suffix: '%s'", optarg);
suffixes[0].zipped = optarg;
suffixes[0].ziplen = len;
} else {
suffixes[NUM_SUFFIXES - 1].zipped = "";
suffixes[NUM_SUFFIXES - 1].ziplen = 0;
}
break;
case 't':
cflag = 1;
tflag = 1;
dflag = 1;
break;
case 'v':
vflag = 1;
break;
#else
case 'n':
break;
#endif
default:
usage();
/* NOTREACHED */
}
}
argv += optind;
argc -= optind;
if (argc == 0) {
if (dflag) /* stdin mode */
handle_stdin();
else /* stdout mode */
handle_stdout();
} else {
do {
handle_pathname(argv[0]);
} while (*++argv);
}
#ifndef SMALL
if (qflag == 0 && lflag && argc > 1)
print_list(-1, 0, "(totals)", 0);
#endif
if (exit_value == 0 && (ferror(stdout) != 0 || fflush(stdout) != 0))
err(1, "stdout");
exit(exit_value);
}
/* maybe print a warning */
void
maybe_warn(const char *fmt, ...)
{
va_list ap;
if (qflag == 0) {
va_start(ap, fmt);
vwarn(fmt, ap);
va_end(ap);
}
if (exit_value == 0)
exit_value = 1;
}
/* ... without an errno. */
void
maybe_warnx(const char *fmt, ...)
{
va_list ap;
if (qflag == 0) {
va_start(ap, fmt);
vwarnx(fmt, ap);
va_end(ap);
}
if (exit_value == 0)
exit_value = 1;
}
/* maybe print an error */
void
maybe_err(const char *fmt, ...)
{
va_list ap;
if (qflag == 0) {
va_start(ap, fmt);
vwarn(fmt, ap);
va_end(ap);
}
exit(2);
}
#if !defined(NO_BZIP2_SUPPORT) || !defined(NO_PACK_SUPPORT) || \
!defined(NO_XZ_SUPPORT) || !defined(NO_ZSTD_SUPPORT)
/* ... without an errno. */
void
maybe_errx(const char *fmt, ...)
{
va_list ap;
if (qflag == 0) {
va_start(ap, fmt);
vwarnx(fmt, ap);
va_end(ap);
}
exit(2);
}
#endif
#ifndef SMALL
/* split up $GZIP and prepend it to the argument list */
static void
prepend_gzip(char *gzip, int *argc, char ***argv)
{
char *s, **nargv, **ac;
int nenvarg = 0, i;
/* scan how many arguments there are */
for (s = gzip;;) {
while (*s == ' ' || *s == '\t')
s++;
if (*s == 0)
goto count_done;
nenvarg++;
while (*s != ' ' && *s != '\t')
if (*s++ == 0)
goto count_done;
}
count_done:
/* punt early */
if (nenvarg == 0)
return;
*argc += nenvarg;
ac = *argv;
nargv = (char **)malloc((*argc + 1) * sizeof(char *));
if (nargv == NULL)
maybe_err("malloc");
/* stash this away */
*argv = nargv;
/* copy the program name first */
i = 0;
nargv[i++] = *(ac++);
/* take a copy of $GZIP and add it to the array */
s = strdup(gzip);
if (s == NULL)
maybe_err("strdup");
for (;;) {
/* Skip whitespaces. */
while (*s == ' ' || *s == '\t')
s++;
if (*s == 0)
goto copy_done;
nargv[i++] = s;
/* Find the end of this argument. */
while (*s != ' ' && *s != '\t')
if (*s++ == 0)
/* Argument followed by NUL. */
goto copy_done;
/* Terminate by overwriting ' ' or '\t' with NUL. */
*s++ = 0;
}
copy_done:
/* copy the original arguments and a NULL */
while (*ac)
nargv[i++] = *(ac++);
nargv[i] = NULL;
}
#endif
/* compress input to output. Return bytes read, -1 on error */
static off_t
gz_compress(int in, int out, off_t *gsizep, const char *origname, uint32_t mtime)
{
z_stream z;
char *outbufp, *inbufp;
off_t in_tot = 0, out_tot = 0;
ssize_t in_size;
int i, error;
uLong crc;
#ifdef SMALL
static char header[] = { GZIP_MAGIC0, GZIP_MAGIC1, Z_DEFLATED, 0,
0, 0, 0, 0,
0, OS_CODE };
#endif
outbufp = malloc(BUFLEN);
inbufp = malloc(BUFLEN);
if (outbufp == NULL || inbufp == NULL) {
maybe_err("malloc failed");
goto out;
}
memset(&z, 0, sizeof z);
z.zalloc = Z_NULL;
z.zfree = Z_NULL;
z.opaque = 0;
#ifdef SMALL
memcpy(outbufp, header, sizeof header);
i = sizeof header;
#else
if (nflag != 0) {
mtime = 0;
origname = "";
}
i = snprintf(outbufp, BUFLEN, "%c%c%c%c%c%c%c%c%c%c%s",
GZIP_MAGIC0, GZIP_MAGIC1, Z_DEFLATED,
*origname ? ORIG_NAME : 0,
mtime & 0xff,
(mtime >> 8) & 0xff,
(mtime >> 16) & 0xff,
(mtime >> 24) & 0xff,
numflag == 1 ? 4 : numflag == 9 ? 2 : 0,
OS_CODE, origname);
if (i >= BUFLEN)
/* this need PATH_MAX > BUFLEN ... */
maybe_err("snprintf");
if (*origname)
i++;
#endif
z.next_out = (unsigned char *)outbufp + i;
z.avail_out = BUFLEN - i;
error = deflateInit2(&z, numflag, Z_DEFLATED,
(-MAX_WBITS), 8, Z_DEFAULT_STRATEGY);
if (error != Z_OK) {
maybe_warnx("deflateInit2 failed");
in_tot = -1;
goto out;
}
crc = crc32(0L, Z_NULL, 0);
for (;;) {
check_siginfo();
if (z.avail_out == 0) {
if (write_retry(out, outbufp, BUFLEN) != BUFLEN) {
maybe_warn("write");
out_tot = -1;
goto out;
}
out_tot += BUFLEN;
z.next_out = (unsigned char *)outbufp;
z.avail_out = BUFLEN;
}
if (z.avail_in == 0) {
in_size = read(in, inbufp, BUFLEN);
if (in_size < 0) {
maybe_warn("read");
in_tot = -1;
goto out;
}
if (in_size == 0)
break;
infile_newdata(in_size);
crc = crc32(crc, (const Bytef *)inbufp, (unsigned)in_size);
in_tot += in_size;
z.next_in = (unsigned char *)inbufp;
z.avail_in = in_size;
}
error = deflate(&z, Z_NO_FLUSH);
if (error != Z_OK && error != Z_STREAM_END) {
maybe_warnx("deflate failed");
in_tot = -1;
goto out;
}
}
/* clean up */
for (;;) {
size_t len;
ssize_t w;
error = deflate(&z, Z_FINISH);
if (error != Z_OK && error != Z_STREAM_END) {
maybe_warnx("deflate failed");
in_tot = -1;
goto out;
}
len = (char *)z.next_out - outbufp;
w = write_retry(out, outbufp, len);
if (w == -1 || (size_t)w != len) {
maybe_warn("write");
out_tot = -1;
goto out;
}
out_tot += len;
z.next_out = (unsigned char *)outbufp;
z.avail_out = BUFLEN;
if (error == Z_STREAM_END)
break;
}
if (deflateEnd(&z) != Z_OK) {
maybe_warnx("deflateEnd failed");
in_tot = -1;
goto out;
}
i = snprintf(outbufp, BUFLEN, "%c%c%c%c%c%c%c%c",
(int)crc & 0xff,
(int)(crc >> 8) & 0xff,
(int)(crc >> 16) & 0xff,
(int)(crc >> 24) & 0xff,
(int)in_tot & 0xff,
(int)(in_tot >> 8) & 0xff,
(int)(in_tot >> 16) & 0xff,
(int)(in_tot >> 24) & 0xff);
if (i != 8)
maybe_err("snprintf");
#if 0
if (in_tot > 0xffffffff)
maybe_warn("input file size >= 4GB cannot be saved");
#endif
if (write_retry(out, outbufp, i) != i) {
maybe_warn("write");
in_tot = -1;
} else
out_tot += i;
out:
if (inbufp != NULL)
free(inbufp);
if (outbufp != NULL)
free(outbufp);
if (gsizep)
*gsizep = out_tot;
return in_tot;
}
/*
* uncompress input to output then close the input. return the
* uncompressed size written, and put the compressed sized read
* into `*gsizep'.
*/
static off_t
gz_uncompress(int in, int out, char *pre, size_t prelen, off_t *gsizep,
const char *filename)
{
z_stream z;
char *outbufp, *inbufp;
off_t out_tot = -1, in_tot = 0;
uint32_t out_sub_tot = 0;
enum {
GZSTATE_MAGIC0,
GZSTATE_MAGIC1,
GZSTATE_METHOD,
GZSTATE_FLAGS,
GZSTATE_SKIPPING,
GZSTATE_EXTRA,
GZSTATE_EXTRA2,
GZSTATE_EXTRA3,
GZSTATE_ORIGNAME,
GZSTATE_COMMENT,
GZSTATE_HEAD_CRC1,
GZSTATE_HEAD_CRC2,
GZSTATE_INIT,
GZSTATE_READ,
GZSTATE_CRC,
GZSTATE_LEN,
} state = GZSTATE_MAGIC0;
int flags = 0, skip_count = 0;
int error = Z_STREAM_ERROR, done_reading = 0;
uLong crc = 0;
ssize_t wr;
int needmore = 0;
#define ADVANCE() { z.next_in++; z.avail_in--; }
if ((outbufp = malloc(BUFLEN)) == NULL) {
maybe_err("malloc failed");
goto out2;
}
if ((inbufp = malloc(BUFLEN)) == NULL) {
maybe_err("malloc failed");
goto out1;
}
memset(&z, 0, sizeof z);
z.avail_in = prelen;
z.next_in = (unsigned char *)pre;
z.avail_out = BUFLEN;
z.next_out = (unsigned char *)outbufp;
z.zalloc = NULL;
z.zfree = NULL;
z.opaque = 0;
in_tot = prelen;
out_tot = 0;
for (;;) {
check_siginfo();
if ((z.avail_in == 0 || needmore) && done_reading == 0) {
ssize_t in_size;
if (z.avail_in > 0) {
memmove(inbufp, z.next_in, z.avail_in);
}
z.next_in = (unsigned char *)inbufp;
in_size = read(in, z.next_in + z.avail_in,
BUFLEN - z.avail_in);
if (in_size == -1) {
maybe_warn("failed to read stdin");
goto stop_and_fail;
} else if (in_size == 0) {
done_reading = 1;
}
infile_newdata(in_size);
z.avail_in += in_size;
needmore = 0;
in_tot += in_size;
}
if (z.avail_in == 0) {
if (done_reading && state != GZSTATE_MAGIC0) {
maybe_warnx("%s: unexpected end of file",
filename);
goto stop_and_fail;
}
goto stop;
}
switch (state) {
case GZSTATE_MAGIC0:
if (*z.next_in != GZIP_MAGIC0) {
if (in_tot > 0) {
maybe_warnx("%s: trailing garbage "
"ignored", filename);
goto stop;
}
maybe_warnx("input not gziped (MAGIC0)");
exit_value = 2;
goto stop_and_fail;
}
ADVANCE();
state++;
out_sub_tot = 0;
crc = crc32(0L, Z_NULL, 0);
break;
case GZSTATE_MAGIC1:
if (*z.next_in != GZIP_MAGIC1 &&
*z.next_in != GZIP_OMAGIC1) {
maybe_warnx("input not gziped (MAGIC1)");
goto stop_and_fail;
}
ADVANCE();
state++;
break;
case GZSTATE_METHOD:
if (*z.next_in != Z_DEFLATED) {
maybe_warnx("unknown compression method");
goto stop_and_fail;
}
ADVANCE();
state++;
break;
case GZSTATE_FLAGS:
flags = *z.next_in;
ADVANCE();
skip_count = 6;
state++;
break;
case GZSTATE_SKIPPING:
if (skip_count > 0) {
skip_count--;
ADVANCE();
} else
state++;
break;
case GZSTATE_EXTRA:
if ((flags & EXTRA_FIELD) == 0) {
state = GZSTATE_ORIGNAME;
break;
}
skip_count = *z.next_in;
ADVANCE();
state++;
break;
case GZSTATE_EXTRA2:
skip_count |= ((*z.next_in) << 8);
ADVANCE();
state++;
break;
case GZSTATE_EXTRA3:
if (skip_count > 0) {
skip_count--;
ADVANCE();
} else
state++;
break;
case GZSTATE_ORIGNAME:
if ((flags & ORIG_NAME) == 0) {
state++;
break;
}
if (*z.next_in == 0)
state++;
ADVANCE();
break;
case GZSTATE_COMMENT:
if ((flags & COMMENT) == 0) {
state++;
break;
}
if (*z.next_in == 0)
state++;
ADVANCE();
break;
case GZSTATE_HEAD_CRC1:
if (flags & HEAD_CRC)
skip_count = 2;
else
skip_count = 0;
state++;
break;
case GZSTATE_HEAD_CRC2:
if (skip_count > 0) {
skip_count--;
ADVANCE();
} else
state++;
break;
case GZSTATE_INIT:
if (inflateInit2(&z, -MAX_WBITS) != Z_OK) {
maybe_warnx("failed to inflateInit");
goto stop_and_fail;
}