Blob


1 #include <u.h>
2 #include <libc.h>
3 #include <bio.h>
4 #include <flate.h>
5 #include "zip.h"
7 enum
8 {
9 BufSize = 4096
10 };
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 */
34 static Biobuf bin;
35 static ulong crc;
36 static ulong *crctab;
37 static int debug;
38 static char *delfile;
39 static int lower;
40 static int nwant;
41 static ulong rlen;
42 static int settimes;
43 static int stdout;
44 static int verbose;
45 static char **want;
46 static int wbad;
47 static ulong wlen;
48 static jmp_buf zjmp;
50 static void
51 usage(void)
52 {
53 fprint(2, "usage: unzip [-tsv] [-f zipfile] [file ...]\n");
54 exits("usage");
55 }
57 void
58 main(int argc, char *argv[])
59 {
60 char *zfile;
61 int fd, ok, table, stream;
63 table = 0;
64 stream = 0;
65 zfile = nil;
66 ARGBEGIN{
67 case 'D':
68 debug++;
69 break;
70 case 'c':
71 stdout++;
72 break;
73 case 'i':
74 lower++;
75 break;
76 case 'f':
77 zfile = ARGF();
78 if(zfile == nil)
79 usage();
80 break;
81 case 's':
82 stream++;
83 break;
84 case 't':
85 table++;
86 break;
87 case 'T':
88 settimes++;
89 break;
90 case 'v':
91 verbose++;
92 break;
93 default:
94 usage();
95 break;
96 }ARGEND
98 nwant = argc;
99 want = argv;
101 crctab = mkcrctab(ZCrcPoly);
102 ok = inflateinit();
103 if(ok != FlateOk)
104 sysfatal("inflateinit failed: %s\n", flateerr(ok));
106 if(zfile == nil){
107 Binit(&bin, 0, OREAD);
108 zfile = "<stdin>";
109 }else{
110 fd = open(zfile, OREAD);
111 if(fd < 0)
112 sysfatal("can't open %s: %r", zfile);
113 Binit(&bin, fd, OREAD);
116 if(table){
117 if(stream)
118 ok = sunztable(&bin);
119 else
120 ok = unztable(&bin, zfile);
121 }else{
122 if(stream)
123 ok = sunzip(&bin);
124 else
125 ok = unzip(&bin, zfile);
128 exits(ok ? nil: "errors");
131 /*
132 * print the table of contents from the "central directory structure"
133 */
134 static int
135 unztable(Biobuf *bin, char *file)
137 ZipHead zh;
138 int volatile entries;
140 entries = findCDir(bin, file);
141 if(entries < 0)
142 return 0;
144 if(verbose > 1)
145 print("%d items in the archive\n", entries);
146 while(entries-- > 0){
147 if(setjmp(zjmp)){
148 free(zh.file);
149 return 0;
152 memset(&zh, 0, sizeof(zh));
153 if(!cheader(bin, &zh))
154 return 1;
156 if(wantFile(zh.file)){
157 if(verbose)
158 print("%-32s %10lud %s", zh.file, zh.uncsize, ctime(msdos2time(zh.modtime, zh.moddate)));
159 else
160 print("%s\n", zh.file);
162 if(verbose > 1){
163 print("\tmade by os %d vers %d.%d\n", zh.madeos, zh.madevers/10, zh.madevers % 10);
164 print("\textract by os %d vers %d.%d\n", zh.extos, zh.extvers/10, zh.extvers % 10);
165 print("\tflags %x\n", zh.flags);
166 print("\tmethod %d\n", zh.meth);
167 print("\tmod time %d\n", zh.modtime);
168 print("\tmod date %d\n", zh.moddate);
169 print("\tcrc %lux\n", zh.crc);
170 print("\tcompressed size %lud\n", zh.csize);
171 print("\tuncompressed size %lud\n", zh.uncsize);
172 print("\tinternal attributes %ux\n", zh.iattr);
173 print("\texternal attributes %lux\n", zh.eattr);
174 print("\tstarts at %ld\n", zh.off);
178 free(zh.file);
179 zh.file = nil;
182 return 1;
185 /*
186 * print the "local file header" table of contents
187 */
188 static int
189 sunztable(Biobuf *bin)
191 ZipHead zh;
192 vlong off;
193 ulong hcrc, hcsize, huncsize;
194 int ok, err;
196 ok = 1;
197 for(;;){
198 if(setjmp(zjmp)){
199 free(zh.file);
200 return 0;
203 memset(&zh, 0, sizeof(zh));
204 if(!header(bin, &zh))
205 return ok;
207 hcrc = zh.crc;
208 hcsize = zh.csize;
209 huncsize = zh.uncsize;
211 wlen = 0;
212 rlen = 0;
213 crc = 0;
214 wbad = 0;
216 if(zh.meth == 0){
217 if(!copyout(-1, bin, zh.csize))
218 error("reading data for %s failed: %r", zh.file);
219 }else if(zh.meth == 8){
220 off = Boffset(bin);
221 err = inflate((void*)-1, crcwrite, bin, (int(*)(void*))Bgetc);
222 if(err != FlateOk)
223 error("inflate %s failed: %s", zh.file, flateerr(err));
224 rlen = Boffset(bin) - off;
225 }else
226 error("can't handle compression method %d for %s", zh.meth, zh.file);
228 trailer(bin, &zh);
230 if(wantFile(zh.file)){
231 if(verbose)
232 print("%-32s %10lud %s", zh.file, zh.uncsize, ctime(msdos2time(zh.modtime, zh.moddate)));
233 else
234 print("%s\n", zh.file);
236 if(verbose > 1){
237 print("\textract by os %d vers %d.%d\n", zh.extos, zh.extvers / 10, zh.extvers % 10);
238 print("\tflags %x\n", zh.flags);
239 print("\tmethod %d\n", zh.meth);
240 print("\tmod time %d\n", zh.modtime);
241 print("\tmod date %d\n", zh.moddate);
242 print("\tcrc %lux\n", zh.crc);
243 print("\tcompressed size %lud\n", zh.csize);
244 print("\tuncompressed size %lud\n", zh.uncsize);
245 if((zh.flags & ZTrailInfo) && (hcrc || hcsize || huncsize)){
246 print("\theader crc %lux\n", zh.crc);
247 print("\theader compressed size %lud\n", zh.csize);
248 print("\theader uncompressed size %lud\n", zh.uncsize);
253 if(zh.crc != crc)
254 error("crc mismatch for %s", zh.file);
255 if(zh.uncsize != wlen)
256 error("output size mismatch for %s", zh.file);
257 if(zh.csize != rlen)
258 error("input size mismatch for %s", zh.file);
261 free(zh.file);
262 zh.file = nil;
266 /*
267 * extract files using the info in the central directory structure
268 */
269 static int
270 unzip(Biobuf *bin, char *file)
272 ZipHead zh;
273 vlong off;
274 int volatile ok, eok, entries;
276 entries = findCDir(bin, file);
277 if(entries < 0)
278 return 0;
280 ok = 1;
281 while(entries-- > 0){
282 if(setjmp(zjmp)){
283 free(zh.file);
284 return 0;
286 memset(&zh, 0, sizeof(zh));
287 if(!cheader(bin, &zh))
288 return ok;
291 off = Boffset(bin);
292 if(wantFile(zh.file)){
293 if(Bseek(bin, zh.off, 0) < 0){
294 fprint(2, "unzip: can't seek to start of %s, skipping\n", zh.file);
295 ok = 0;
296 }else{
297 eok = unzipEntry(bin, &zh);
298 if(eok <= 0){
299 fprint(2, "unzip: skipping %s\n", zh.file);
300 ok = 0;
305 free(zh.file);
306 zh.file = nil;
308 if(Bseek(bin, off, 0) < 0){
309 fprint(2, "unzip: can't seek to start of next entry, terminating extraction\n");
310 return 0;
314 return ok;
317 /*
318 * extract files using the info the "local file headers"
319 */
320 static int
321 sunzip(Biobuf *bin)
323 int eok;
325 for(;;){
326 eok = unzipEntry(bin, nil);
327 if(eok == 0)
328 return 1;
329 if(eok < 0)
330 return 0;
334 /*
335 * extracts a single entry from a zip file
336 * czh is the optional corresponding central directory entry
337 */
338 static int
339 unzipEntry(Biobuf *bin, ZipHead *czh)
341 Dir *d;
342 ZipHead zh;
343 char *p;
344 vlong off;
345 int fd, isdir, ok, err;
347 zh.file = nil;
348 if(setjmp(zjmp)){
349 delfile = nil;
350 free(zh.file);
351 return -1;
354 memset(&zh, 0, sizeof(zh));
355 if(!header(bin, &zh))
356 return 0;
358 ok = 1;
359 isdir = 0;
361 fd = -1;
362 if(wantFile(zh.file)){
363 if(verbose)
364 fprint(2, "extracting %s\n", zh.file);
366 if(czh != nil && czh->extos == ZDos){
367 isdir = czh->eattr & ZDDir;
368 if(isdir && zh.uncsize != 0)
369 fprint(2, "unzip: ignoring directory data for %s\n", zh.file);
371 if(zh.meth == 0 && zh.uncsize == 0){
372 p = strchr(zh.file, '\0');
373 if(p > zh.file && p[-1] == '/')
374 isdir = 1;
377 if(stdout){
378 if(ok && !isdir)
379 fd = 1;
380 }else if(isdir){
381 fd = create(zh.file, OREAD, DMDIR | 0775);
382 if(fd < 0){
383 d = dirstat(zh.file);
384 if(d == nil || (d->mode & DMDIR) != DMDIR){
385 fprint(2, "unzip: can't create directory %s: %r\n", zh.file);
386 ok = 0;
388 free(d);
390 }else if(ok){
391 fd = create(zh.file, OWRITE, 0664);
392 if(fd < 0){
393 fprint(2, "unzip: can't create %s: %r\n", zh.file);
394 ok = 0;
395 }else
396 delfile = zh.file;
400 wlen = 0;
401 rlen = 0;
402 crc = 0;
403 wbad = 0;
405 if(zh.meth == 0){
406 if(!copyout(fd, bin, zh.csize))
407 error("copying data for %s failed: %r", zh.file);
408 }else if(zh.meth == 8){
409 off = Boffset(bin);
410 err = inflate((void*)fd, crcwrite, bin, (int(*)(void*))Bgetc);
411 if(err != FlateOk)
412 error("inflate failed: %s", flateerr(err));
413 rlen = Boffset(bin) - off;
414 }else
415 error("can't handle compression method %d for %s", zh.meth, zh.file);
417 trailer(bin, &zh);
419 if(zh.crc != crc)
420 error("crc mismatch for %s", zh.file);
421 if(zh.uncsize != wlen)
422 error("output size mismatch for %s", zh.file);
423 if(zh.csize != rlen)
424 error("input size mismatch for %s", zh.file);
426 delfile = nil;
427 free(zh.file);
429 if(fd >= 0 && !stdout){
430 if(settimes){
431 d = dirfstat(fd);
432 if(d != nil){
433 d->mtime = msdos2time(zh.modtime, zh.moddate);
434 if(d->mtime)
435 dirfwstat(fd, d);
438 close(fd);
441 return ok;
444 static int
445 wantFile(char *file)
447 int i, n;
449 if(nwant == 0)
450 return 1;
451 for(i = 0; i < nwant; i++){
452 if(strcmp(want[i], file) == 0)
453 return 1;
454 n = strlen(want[i]);
455 if(strncmp(want[i], file, n) == 0 && file[n] == '/')
456 return 1;
458 return 0;
461 /*
462 * find the start of the central directory
463 * returns the number of entries in the directory,
464 * or -1 if there was an error
465 */
466 static int
467 findCDir(Biobuf *bin, char *file)
469 vlong ecoff;
470 long off, size;
471 int entries, zclen, dn, ds, de;
473 ecoff = Bseek(bin, -ZECHeadSize, 2);
474 if(ecoff < 0){
475 fprint(2, "unzip: can't seek to contents of %s; try adding -s\n", file);
476 return -1;
478 if(setjmp(zjmp))
479 return -1;
481 if(get4(bin) != ZECHeader){
482 fprint(2, "unzip: bad magic number for contents of %s\n", file);
483 return -1;
485 dn = get2(bin);
486 ds = get2(bin);
487 de = get2(bin);
488 entries = get2(bin);
489 size = get4(bin);
490 off = get4(bin);
491 zclen = get2(bin);
492 while(zclen-- > 0)
493 get1(bin);
495 if(verbose > 1){
496 print("table starts at %ld for %ld bytes\n", off, size);
497 if(ecoff - size != off)
498 print("\ttable should start at %lld-%ld=%lld\n", ecoff, size, ecoff-size);
499 if(dn || ds || de != entries)
500 print("\tcurrent disk=%d start disk=%d table entries on this disk=%d\n", dn, ds, de);
503 if(Bseek(bin, off, 0) != off){
504 fprint(2, "unzip: can't seek to start of contents of %s\n", file);
505 return -1;
508 return entries;
511 static int
512 cheader(Biobuf *bin, ZipHead *zh)
514 ulong v;
515 int flen, xlen, fclen;
517 v = get4(bin);
518 if(v != ZCHeader){
519 if(v == ZECHeader)
520 return 0;
521 error("bad magic number %lux", v);
523 zh->madevers = get1(bin);
524 zh->madeos = get1(bin);
525 zh->extvers = get1(bin);
526 zh->extos = get1(bin);
527 zh->flags = get2(bin);
528 zh->meth = get2(bin);
529 zh->modtime = get2(bin);
530 zh->moddate = get2(bin);
531 zh->crc = get4(bin);
532 zh->csize = get4(bin);
533 zh->uncsize = get4(bin);
534 flen = get2(bin);
535 xlen = get2(bin);
536 fclen = get2(bin);
537 get2(bin); /* disk number start */
538 zh->iattr = get2(bin);
539 zh->eattr = get4(bin);
540 zh->off = get4(bin);
542 zh->file = getname(bin, flen);
544 while(xlen-- > 0)
545 get1(bin);
547 while(fclen-- > 0)
548 get1(bin);
550 return 1;
553 static int
554 header(Biobuf *bin, ZipHead *zh)
556 ulong v;
557 int flen, xlen;
559 v = get4(bin);
560 if(v != ZHeader){
561 if(v == ZCHeader)
562 return 0;
563 error("bad magic number %lux at %lld", v, Boffset(bin)-4);
565 zh->extvers = get1(bin);
566 zh->extos = get1(bin);
567 zh->flags = get2(bin);
568 zh->meth = get2(bin);
569 zh->modtime = get2(bin);
570 zh->moddate = get2(bin);
571 zh->crc = get4(bin);
572 zh->csize = get4(bin);
573 zh->uncsize = get4(bin);
574 flen = get2(bin);
575 xlen = get2(bin);
577 zh->file = getname(bin, flen);
579 while(xlen-- > 0)
580 get1(bin);
582 return 1;
585 static void
586 trailer(Biobuf *bin, ZipHead *zh)
588 if(zh->flags & ZTrailInfo){
589 zh->crc = get4(bin);
590 zh->csize = get4(bin);
591 zh->uncsize = get4(bin);
595 static char*
596 getname(Biobuf *bin, int len)
598 char *s;
599 int i, c;
601 s = emalloc(len + 1);
602 for(i = 0; i < len; i++){
603 c = get1(bin);
604 if(lower)
605 c = tolower(c);
606 s[i] = c;
608 s[i] = '\0';
609 return s;
612 static int
613 crcwrite(void *out, void *buf, int n)
615 int fd, nw;
617 wlen += n;
618 crc = blockcrc(crctab, crc, buf, n);
619 fd = (int)out;
620 if(fd < 0)
621 return n;
622 nw = write(fd, buf, n);
623 if(nw != n)
624 wbad = 1;
625 return nw;
628 static int
629 copyout(int ofd, Biobuf *bin, long len)
631 char buf[BufSize];
632 int n;
634 for(; len > 0; len -= n){
635 n = len;
636 if(n > BufSize)
637 n = BufSize;
638 n = Bread(bin, buf, n);
639 if(n <= 0)
640 return 0;
641 rlen += n;
642 if(crcwrite((void*)ofd, buf, n) != n)
643 return 0;
645 return 1;
648 static ulong
649 get4(Biobuf *b)
651 ulong v;
652 int i, c;
654 v = 0;
655 for(i = 0; i < 4; i++){
656 c = Bgetc(b);
657 if(c < 0)
658 error("unexpected eof reading file information");
659 v |= c << (i * 8);
661 return v;
664 static int
665 get2(Biobuf *b)
667 int i, c, v;
669 v = 0;
670 for(i = 0; i < 2; i++){
671 c = Bgetc(b);
672 if(c < 0)
673 error("unexpected eof reading file information");
674 v |= c << (i * 8);
676 return v;
679 static int
680 get1(Biobuf *b)
682 int c;
684 c = Bgetc(b);
685 if(c < 0)
686 error("unexpected eof reading file information");
687 return c;
690 static long
691 msdos2time(int time, int date)
693 Tm tm;
695 tm.hour = time >> 11;
696 tm.min = (time >> 5) & 63;
697 tm.sec = (time & 31) << 1;
698 tm.year = 80 + (date >> 9);
699 tm.mon = ((date >> 5) & 15) - 1;
700 tm.mday = date & 31;
701 tm.zone[0] = '\0';
702 tm.yday = 0;
704 return tm2sec(&tm);
707 static void*
708 emalloc(ulong n)
710 void *p;
712 p = malloc(n);
713 if(p == nil)
714 sysfatal("out of memory");
715 return p;
718 static void
719 error(char *fmt, ...)
721 va_list arg;
723 fprint(2, "unzip: ");
724 va_start(arg, fmt);
725 vfprint(2, fmt, arg);
726 va_end(arg);
727 fprint(2, "\n");
729 if(delfile != nil){
730 fprint(2, "unzip: removing output file %s\n", delfile);
731 remove(delfile);
732 delfile = nil;
735 longjmp(zjmp, 1);