12 static int cheader(Biobuf *bin, ZipHead *zh);
13 static int copyout(int ofd, Biobuf *bin, long len);
14 static int crcwrite(void *ofd, void *buf, int n);
15 static int findCDir(Biobuf *bin, char *file);
16 static int get1(Biobuf *b);
17 static int get2(Biobuf *b);
18 static ulong get4(Biobuf *b);
19 static char *getname(Biobuf *b, int len);
20 static int header(Biobuf *bin, ZipHead *zh);
21 static long msdos2time(int time, int date);
22 static int sunzip(Biobuf *bin);
23 static int sunztable(Biobuf *bin);
24 static void trailer(Biobuf *bin, ZipHead *zh);
25 static int unzip(Biobuf *bin, char *file);
26 static int unzipEntry(Biobuf *bin, ZipHead *czh);
27 static int unztable(Biobuf *bin, char *file);
28 static int wantFile(char *file);
30 static void *emalloc(ulong);
31 static void error(char*, ...);
32 #pragma varargck argpos error 1
49 static jmp_buf seekjmp;
55 fprint(2, "usage: unzip [-acistTvD] [-f zipfile] [file ...]\n");
60 main(int argc, char *argv[])
63 int fd, ok, table, stream;
106 crctab = mkcrctab(ZCrcPoly);
109 sysfatal("inflateinit failed: %s", flateerr(ok));
112 Binit(&bin, 0, OREAD);
115 fd = open(zfile, OREAD);
117 sysfatal("can't open %s: %r", zfile);
118 Binit(&bin, fd, OREAD);
122 fprint(2, "trying to re-run assuming -s\n");
129 ok = sunztable(&bin);
131 ok = unztable(&bin, zfile);
136 ok = unzip(&bin, zfile);
139 exits(ok ? nil: "errors");
143 * print the table of contents from the "central directory structure"
146 unztable(Biobuf *bin, char *file)
151 entries = findCDir(bin, file);
156 print("%d items in the archive\n", entries);
157 while(entries-- > 0){
163 memset(&zh, 0, sizeof(zh));
164 if(!cheader(bin, &zh))
167 if(wantFile(zh.file)){
169 print("%-32s %10lud %s", zh.file, zh.uncsize, ctime(msdos2time(zh.modtime, zh.moddate)));
171 print("%s\n", zh.file);
174 print("\tmade by os %d vers %d.%d\n", zh.madeos, zh.madevers/10, zh.madevers % 10);
175 print("\textract by os %d vers %d.%d\n", zh.extos, zh.extvers/10, zh.extvers % 10);
176 print("\tflags %x\n", zh.flags);
177 print("\tmethod %d\n", zh.meth);
178 print("\tmod time %d\n", zh.modtime);
179 print("\tmod date %d\n", zh.moddate);
180 print("\tcrc %lux\n", zh.crc);
181 print("\tcompressed size %lud\n", zh.csize);
182 print("\tuncompressed size %lud\n", zh.uncsize);
183 print("\tinternal attributes %ux\n", zh.iattr);
184 print("\texternal attributes %lux\n", zh.eattr);
185 print("\tstarts at %ld\n", zh.off);
197 * print the "local file header" table of contents
200 sunztable(Biobuf *bin)
204 ulong hcrc, hcsize, huncsize;
214 memset(&zh, 0, sizeof(zh));
215 if(!header(bin, &zh))
220 huncsize = zh.uncsize;
228 if(!copyout(-1, bin, zh.csize))
229 error("reading data for %s failed: %r", zh.file);
230 }else if(zh.meth == 8){
232 err = inflate((void*)-1, crcwrite, bin, (int(*)(void*))Bgetc);
234 error("inflate %s failed: %s", zh.file, flateerr(err));
235 rlen = Boffset(bin) - off;
237 error("can't handle compression method %d for %s", zh.meth, zh.file);
241 if(wantFile(zh.file)){
243 print("%-32s %10lud %s", zh.file, zh.uncsize, ctime(msdos2time(zh.modtime, zh.moddate)));
245 print("%s\n", zh.file);
248 print("\textract by os %d vers %d.%d\n", zh.extos, zh.extvers / 10, zh.extvers % 10);
249 print("\tflags %x\n", zh.flags);
250 print("\tmethod %d\n", zh.meth);
251 print("\tmod time %d\n", zh.modtime);
252 print("\tmod date %d\n", zh.moddate);
253 print("\tcrc %lux\n", zh.crc);
254 print("\tcompressed size %lud\n", zh.csize);
255 print("\tuncompressed size %lud\n", zh.uncsize);
256 if((zh.flags & ZTrailInfo) && (hcrc || hcsize || huncsize)){
257 print("\theader crc %lux\n", zh.crc);
258 print("\theader compressed size %lud\n", zh.csize);
259 print("\theader uncompressed size %lud\n", zh.uncsize);
265 error("crc mismatch for %s", zh.file);
266 if(zh.uncsize != wlen)
267 error("output size mismatch for %s", zh.file);
269 error("input size mismatch for %s", zh.file);
278 * extract files using the info in the central directory structure
281 unzip(Biobuf *bin, char *file)
285 int ok, eok, entries;
287 entries = findCDir(bin, file);
292 while(entries-- > 0){
297 memset(&zh, 0, sizeof(zh));
298 if(!cheader(bin, &zh))
303 if(wantFile(zh.file)){
304 if(Bseek(bin, zh.off, 0) < 0){
305 fprint(2, "unzip: can't seek to start of %s, skipping\n", zh.file);
308 eok = unzipEntry(bin, &zh);
310 fprint(2, "unzip: skipping %s\n", zh.file);
319 if(Bseek(bin, off, 0) < 0){
320 fprint(2, "unzip: can't seek to start of next entry, terminating extraction\n");
329 * extract files using the info the "local file headers"
337 eok = unzipEntry(bin, nil);
345 static int mkdirs(char *);
348 * if any directories leading up to path don't exist, create them.
349 * modifies but restores path.
355 char *sl = strrchr(path, '/');
366 * if path or any directories leading up to it don't exist, create them.
367 * modifies but restores path.
374 if (access(path, AEXIST) >= 0)
377 /* make presumed-missing intermediate directories */
378 if (mkpdirs(path) < 0)
381 /* make final directory */
382 fd = create(path, OREAD, 0755|DMDIR);
385 * we may have lost a race; if the directory now exists,
388 return access(path, AEXIST) < 0? -1: 0;
395 * extracts a single entry from a zip file
396 * czh is the optional corresponding central directory entry
399 unzipEntry(Biobuf *bin, ZipHead *czh)
405 int fd, isdir, ok, err;
414 memset(&zh, 0, sizeof(zh));
415 if(!header(bin, &zh))
422 if(wantFile(zh.file)){
424 fprint(2, "extracting %s\n", zh.file);
426 if(czh != nil && czh->extos == ZDos){
427 isdir = czh->eattr & ZDDir;
428 if(isdir && zh.uncsize != 0)
429 fprint(2, "unzip: ignoring directory data for %s\n", zh.file);
431 if(zh.meth == 0 && zh.uncsize == 0){
432 p = strchr(zh.file, '\0');
433 if(p > zh.file && p[-1] == '/')
441 fd = create(zh.file, OREAD, DMDIR | 0775);
443 d = dirstat(zh.file);
444 if(d == nil || (d->mode & DMDIR) != DMDIR){
445 fprint(2, "unzip: can't create directory %s: %r\n", zh.file);
453 fd = create(zh.file, OWRITE, 0664);
455 fprint(2, "unzip: can't create %s: %r\n", zh.file);
468 if(!copyout(fd, bin, zh.csize))
469 error("copying data for %s failed: %r", zh.file);
470 }else if(zh.meth == 8){
472 err = inflate((void*)fd, crcwrite, bin, (int(*)(void*))Bgetc);
474 error("inflate failed: %s", flateerr(err));
475 rlen = Boffset(bin) - off;
477 error("can't handle compression method %d for %s", zh.meth, zh.file);
482 error("crc mismatch for %s", zh.file);
483 if(zh.uncsize != wlen)
484 error("output size mismatch for %s", zh.file);
486 error("input size mismatch for %s", zh.file);
491 if(fd >= 0 && !stdout){
495 d->mtime = msdos2time(zh.modtime, zh.moddate);
513 for(i = 0; i < nwant; i++){
514 if(strcmp(want[i], file) == 0)
517 if(strncmp(want[i], file, n) == 0 && file[n] == '/')
524 * find the start of the central directory
525 * returns the number of entries in the directory,
526 * or -1 if there was an error
529 findCDir(Biobuf *bin, char *file)
533 int entries, zclen, dn, ds, de;
535 ecoff = Bseek(bin, -ZECHeadSize, 2);
537 fprint(2, "unzip: can't seek to contents of %s\n", file);
544 while(get4(bin) != ZECHeader){
545 if(ecoff <= 0 || off >= 1024){
546 fprint(2, "unzip: cannot find end of table of contents in %s\n", file);
552 Bseek(bin, ecoff, 0);
565 print("table starts at %ld for %ld bytes\n", off, size);
566 if(ecoff - size != off)
567 print("\ttable should start at %lld-%ld=%lld\n", ecoff, size, ecoff-size);
568 if(dn || ds || de != entries)
569 print("\tcurrent disk=%d start disk=%d table entries on this disk=%d\n", dn, ds, de);
572 if(Bseek(bin, off, 0) != off){
573 fprint(2, "unzip: can't seek to start of contents of %s\n", file);
582 cheader(Biobuf *bin, ZipHead *zh)
585 int flen, xlen, fclen;
591 error("bad magic number %lux", v);
593 zh->madevers = get1(bin);
594 zh->madeos = get1(bin);
595 zh->extvers = get1(bin);
596 zh->extos = get1(bin);
597 zh->flags = get2(bin);
598 zh->meth = get2(bin);
599 zh->modtime = get2(bin);
600 zh->moddate = get2(bin);
602 zh->csize = get4(bin);
603 zh->uncsize = get4(bin);
607 get2(bin); /* disk number start */
608 zh->iattr = get2(bin);
609 zh->eattr = get4(bin);
612 zh->file = getname(bin, flen);
624 header(Biobuf *bin, ZipHead *zh)
633 error("bad magic number %lux at %lld", v, Boffset(bin)-4);
635 zh->extvers = get1(bin);
636 zh->extos = get1(bin);
637 zh->flags = get2(bin);
638 zh->meth = get2(bin);
639 zh->modtime = get2(bin);
640 zh->moddate = get2(bin);
642 zh->csize = get4(bin);
643 zh->uncsize = get4(bin);
647 zh->file = getname(bin, flen);
656 trailer(Biobuf *bin, ZipHead *zh)
658 if(zh->flags & ZTrailInfo){
660 if(zh->crc == 0x08074b50) /* thanks apple */
662 zh->csize = get4(bin);
663 zh->uncsize = get4(bin);
668 getname(Biobuf *bin, int len)
673 s = emalloc(len + 1);
674 for(i = 0; i < len; i++){
685 crcwrite(void *out, void *buf, int n)
690 crc = blockcrc(crctab, crc, buf, n);
691 fd = (int)(uintptr)out;
694 nw = write(fd, buf, n);
701 copyout(int ofd, Biobuf *bin, long len)
706 for(; len > 0; len -= n){
710 n = Bread(bin, buf, n);
714 if(crcwrite((void*)ofd, buf, n) != n)
727 for(i = 0; i < 4; i++){
730 error("unexpected eof reading file information");
742 for(i = 0; i < 2; i++){
745 error("unexpected eof reading file information");
758 error("unexpected eof reading file information");
763 msdos2time(int time, int date)
767 tm.hour = time >> 11;
768 tm.min = (time >> 5) & 63;
769 tm.sec = (time & 31) << 1;
770 tm.year = 80 + (date >> 9);
771 tm.mon = ((date >> 5) & 15) - 1;
786 sysfatal("out of memory");
791 error(char *fmt, ...)
795 fprint(2, "unzip: ");
797 vfprint(2, fmt, arg);
802 fprint(2, "unzip: removing output file %s\n", delfile);