7 typedef struct GZHead GZHead;
15 static int crcwrite(void *bout, void *buf, int n);
16 static int get1(Biobuf *b);
17 static ulong get4(Biobuf *b);
18 static int gunzipf(char *file, int stdout);
19 static int gunzip(int ofd, char *ofile, Biobuf *bin);
20 static void header(Biobuf *bin, GZHead *h);
21 static void trailer(Biobuf *bin, long wlen);
22 static void error(char*, ...);
23 #pragma varargck argpos error 1
42 fprint(2, "usage: gunzip [-ctvTD] [file ....]\n");
47 main(int argc, char *argv[])
73 crctab = mkcrctab(GZCRCPOLY);
76 sysfatal("inflateinit failed: %s", flateerr(ok));
79 Binit(&bin, 0, OREAD);
82 ok = gunzip(1, "<stdout>", &bin);
87 for(i = 0; i < argc; i++)
88 ok &= gunzipf(argv[i], stdout);
91 exits(ok ? nil: "errors");
95 gunzipf(char *file, int stdout)
101 ifd = open(file, OREAD);
103 fprint(2, "gunzip: can't open %s: %r\n", file);
107 Binit(&bin, ifd, OREAD);
108 if(Bgetc(&bin) != GZMAGIC1 || Bgetc(&bin) != GZMAGIC2 || Bgetc(&bin) != GZDEFLATE){
109 fprint(2, "gunzip: %s is not a gzip deflate file\n", file);
122 strcpy(ofile, "<stdout>");
124 s = strrchr(file, '/');
129 strecpy(ofile, ofile+sizeof ofile, s);
130 s = strrchr(ofile, '.');
131 if(s != nil && s != ofile && strcmp(s, ".gz") == 0)
133 else if(s != nil && strcmp(s, ".tgz") == 0)
135 else if(strcmp(file, ofile) == 0){
136 fprint(2, "gunzip: can't overwrite %s\n", file);
142 ofd = create(ofile, OWRITE, 0666);
144 fprint(2, "gunzip: can't create %s: %r\n", ofile);
153 ok = gunzip(ofd, ofile, &bin);
157 fprint(2, "gunzip: can't write %s: %r\n", ofile);
162 if(!stdout && ofd >= 0)
168 gunzip(int ofd, char *ofile, Biobuf *bin)
189 if(!table && verbose)
190 fprint(2, "extracting %s to %s\n", h.file, ofile);
192 err = inflate((void*)ofd, crcwrite, bin, (int(*)(void*))Bgetc);
194 error("inflate failed: %s", flateerr(err));
200 print("%-32s %10ld %s", h.file, wlen, ctime(h.mtime));
202 print("%s\n", h.file);
203 }else if(settimes && h.mtime && (d=dirfstat(ofd)) != nil){
216 header(Biobuf *bin, GZHead *h)
219 int i, c, flag, ns, nsa;
221 if(get1(bin) != GZMAGIC1 || get1(bin) != GZMAGIC2)
222 error("bad gzip file magic");
223 if(get1(bin) != GZDEFLATE)
224 error("unknown compression type");
227 if(flag & ~(GZFTEXT|GZFEXTRA|GZFNAME|GZFCOMMENT|GZFHCRC))
228 fprint(2, "gunzip: reserved flags set, data may not be decompressed correctly\n");
231 h->mtime = get4(bin);
252 error("out of memory");
253 while((c = get1(bin)) != 0){
259 error("out of memory");
265 h->file = strdup("<unnamed file>");
268 if(flag & GZFCOMMENT)
269 while(get1(bin) != 0)
280 trailer(Biobuf *bin, long wlen)
287 error("crc mismatch");
292 error("bad output length: expected %lud got %lud", wlen, len);
302 for(i = 0; i < 4; i++){
305 error("unexpected eof reading file information");
318 error("unexpected eof reading file information");
323 crcwrite(void *out, void *buf, int n)
328 crc = blockcrc(crctab, crc, buf, n);
329 fd = (int)(uintptr)out;
332 nw = write(fd, buf, n);
339 error(char *fmt, ...)
344 fprint(2, "gunzip: %s: corrupted data after byte %lld ignored\n", infile, gzok);
346 fprint(2, "gunzip: ");
348 fprint(2, "%s: ", infile);
350 vfprint(2, fmt, arg);
355 fprint(2, "gunzip: removing output file %s\n", delfile);