unzip.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. #include <flate.h>
  5. #include "zip.h"
  6. enum
  7. {
  8. BufSize = 4096
  9. };
  10. static int cheader(Biobuf *bin, ZipHead *zh);
  11. static int copyout(int ofd, Biobuf *bin, long len);
  12. static int crcwrite(void *ofd, void *buf, int n);
  13. static int findCDir(Biobuf *bin, char *file);
  14. static int get1(Biobuf *b);
  15. static int get2(Biobuf *b);
  16. static ulong get4(Biobuf *b);
  17. static char *getname(Biobuf *b, int len);
  18. static int header(Biobuf *bin, ZipHead *zh);
  19. static long msdos2time(int time, int date);
  20. static int sunzip(Biobuf *bin);
  21. static int sunztable(Biobuf *bin);
  22. static void trailer(Biobuf *bin, ZipHead *zh);
  23. static int unzip(Biobuf *bin, char *file);
  24. static int unzipEntry(Biobuf *bin, ZipHead *czh);
  25. static int unztable(Biobuf *bin, char *file);
  26. static int wantFile(char *file);
  27. static void *emalloc(ulong);
  28. static void error(char*, ...);
  29. #pragma varargck argpos error 1
  30. static Biobuf bin;
  31. static ulong crc;
  32. static ulong *crctab;
  33. static int debug;
  34. static char *delfile;
  35. static int lower;
  36. static int nwant;
  37. static ulong rlen;
  38. static int settimes;
  39. static int stdout;
  40. static int verbose;
  41. static char **want;
  42. static int wbad;
  43. static ulong wlen;
  44. static jmp_buf zjmp;
  45. static jmp_buf seekjmp;
  46. static void
  47. usage(void)
  48. {
  49. fprint(2, "usage: unzip [-tsv] [-f zipfile] [file ...]\n");
  50. exits("usage");
  51. }
  52. void
  53. main(int argc, char *argv[])
  54. {
  55. char *zfile;
  56. int fd, ok, table, stream;
  57. table = 0;
  58. stream = 0;
  59. zfile = nil;
  60. ARGBEGIN{
  61. case 'D':
  62. debug++;
  63. break;
  64. case 'c':
  65. stdout++;
  66. break;
  67. case 'i':
  68. lower++;
  69. break;
  70. case 'f':
  71. zfile = ARGF();
  72. if(zfile == nil)
  73. usage();
  74. break;
  75. case 's':
  76. stream++;
  77. break;
  78. case 't':
  79. table++;
  80. break;
  81. case 'T':
  82. settimes++;
  83. break;
  84. case 'v':
  85. verbose++;
  86. break;
  87. default:
  88. usage();
  89. break;
  90. }ARGEND
  91. nwant = argc;
  92. want = argv;
  93. crctab = mkcrctab(ZCrcPoly);
  94. ok = inflateinit();
  95. if(ok != FlateOk)
  96. sysfatal("inflateinit failed: %s\n", flateerr(ok));
  97. if(zfile == nil){
  98. Binit(&bin, 0, OREAD);
  99. zfile = "<stdin>";
  100. }else{
  101. fd = open(zfile, OREAD);
  102. if(fd < 0)
  103. sysfatal("can't open %s: %r", zfile);
  104. Binit(&bin, fd, OREAD);
  105. }
  106. if(setjmp(seekjmp)){
  107. fprint(2, "trying to re-run assuming -s\n");
  108. stream = 1;
  109. Bseek(&bin, 0, 0);
  110. }
  111. if(table){
  112. if(stream)
  113. ok = sunztable(&bin);
  114. else
  115. ok = unztable(&bin, zfile);
  116. }else{
  117. if(stream)
  118. ok = sunzip(&bin);
  119. else
  120. ok = unzip(&bin, zfile);
  121. }
  122. exits(ok ? nil: "errors");
  123. }
  124. /*
  125. * print the table of contents from the "central directory structure"
  126. */
  127. static int
  128. unztable(Biobuf *bin, char *file)
  129. {
  130. ZipHead zh;
  131. int entries;
  132. entries = findCDir(bin, file);
  133. if(entries < 0)
  134. return 0;
  135. if(verbose > 1)
  136. print("%d items in the archive\n", entries);
  137. while(entries-- > 0){
  138. if(setjmp(zjmp)){
  139. free(zh.file);
  140. return 0;
  141. }
  142. memset(&zh, 0, sizeof(zh));
  143. if(!cheader(bin, &zh))
  144. return 1;
  145. if(wantFile(zh.file)){
  146. if(verbose)
  147. print("%-32s %10lud %s", zh.file, zh.uncsize, ctime(msdos2time(zh.modtime, zh.moddate)));
  148. else
  149. print("%s\n", zh.file);
  150. if(verbose > 1){
  151. print("\tmade by os %d vers %d.%d\n", zh.madeos, zh.madevers/10, zh.madevers % 10);
  152. print("\textract by os %d vers %d.%d\n", zh.extos, zh.extvers/10, zh.extvers % 10);
  153. print("\tflags %x\n", zh.flags);
  154. print("\tmethod %d\n", zh.meth);
  155. print("\tmod time %d\n", zh.modtime);
  156. print("\tmod date %d\n", zh.moddate);
  157. print("\tcrc %lux\n", zh.crc);
  158. print("\tcompressed size %lud\n", zh.csize);
  159. print("\tuncompressed size %lud\n", zh.uncsize);
  160. print("\tinternal attributes %ux\n", zh.iattr);
  161. print("\texternal attributes %lux\n", zh.eattr);
  162. print("\tstarts at %ld\n", zh.off);
  163. }
  164. }
  165. free(zh.file);
  166. zh.file = nil;
  167. }
  168. return 1;
  169. }
  170. /*
  171. * print the "local file header" table of contents
  172. */
  173. static int
  174. sunztable(Biobuf *bin)
  175. {
  176. ZipHead zh;
  177. vlong off;
  178. ulong hcrc, hcsize, huncsize;
  179. int ok, err;
  180. ok = 1;
  181. for(;;){
  182. if(setjmp(zjmp)){
  183. free(zh.file);
  184. return 0;
  185. }
  186. memset(&zh, 0, sizeof(zh));
  187. if(!header(bin, &zh))
  188. return ok;
  189. hcrc = zh.crc;
  190. hcsize = zh.csize;
  191. huncsize = zh.uncsize;
  192. wlen = 0;
  193. rlen = 0;
  194. crc = 0;
  195. wbad = 0;
  196. if(zh.meth == 0){
  197. if(!copyout(-1, bin, zh.csize))
  198. error("reading data for %s failed: %r", zh.file);
  199. }else if(zh.meth == 8){
  200. off = Boffset(bin);
  201. err = inflate((void*)-1, crcwrite, bin, (int(*)(void*))Bgetc);
  202. if(err != FlateOk)
  203. error("inflate %s failed: %s", zh.file, flateerr(err));
  204. rlen = Boffset(bin) - off;
  205. }else
  206. error("can't handle compression method %d for %s", zh.meth, zh.file);
  207. trailer(bin, &zh);
  208. if(wantFile(zh.file)){
  209. if(verbose)
  210. print("%-32s %10lud %s", zh.file, zh.uncsize, ctime(msdos2time(zh.modtime, zh.moddate)));
  211. else
  212. print("%s\n", zh.file);
  213. if(verbose > 1){
  214. print("\textract by os %d vers %d.%d\n", zh.extos, zh.extvers / 10, zh.extvers % 10);
  215. print("\tflags %x\n", zh.flags);
  216. print("\tmethod %d\n", zh.meth);
  217. print("\tmod time %d\n", zh.modtime);
  218. print("\tmod date %d\n", zh.moddate);
  219. print("\tcrc %lux\n", zh.crc);
  220. print("\tcompressed size %lud\n", zh.csize);
  221. print("\tuncompressed size %lud\n", zh.uncsize);
  222. if((zh.flags & ZTrailInfo) && (hcrc || hcsize || huncsize)){
  223. print("\theader crc %lux\n", zh.crc);
  224. print("\theader compressed size %lud\n", zh.csize);
  225. print("\theader uncompressed size %lud\n", zh.uncsize);
  226. }
  227. }
  228. }
  229. if(zh.crc != crc)
  230. error("crc mismatch for %s", zh.file);
  231. if(zh.uncsize != wlen)
  232. error("output size mismatch for %s", zh.file);
  233. if(zh.csize != rlen)
  234. error("input size mismatch for %s", zh.file);
  235. free(zh.file);
  236. zh.file = nil;
  237. }
  238. }
  239. /*
  240. * extract files using the info in the central directory structure
  241. */
  242. static int
  243. unzip(Biobuf *bin, char *file)
  244. {
  245. ZipHead zh;
  246. vlong off;
  247. int ok, eok, entries;
  248. entries = findCDir(bin, file);
  249. if(entries < 0)
  250. return 0;
  251. ok = 1;
  252. while(entries-- > 0){
  253. if(setjmp(zjmp)){
  254. free(zh.file);
  255. return 0;
  256. }
  257. memset(&zh, 0, sizeof(zh));
  258. if(!cheader(bin, &zh))
  259. return ok;
  260. off = Boffset(bin);
  261. if(wantFile(zh.file)){
  262. if(Bseek(bin, zh.off, 0) < 0){
  263. fprint(2, "unzip: can't seek to start of %s, skipping\n", zh.file);
  264. ok = 0;
  265. }else{
  266. eok = unzipEntry(bin, &zh);
  267. if(eok <= 0){
  268. fprint(2, "unzip: skipping %s\n", zh.file);
  269. ok = 0;
  270. }
  271. }
  272. }
  273. free(zh.file);
  274. zh.file = nil;
  275. if(Bseek(bin, off, 0) < 0){
  276. fprint(2, "unzip: can't seek to start of next entry, terminating extraction\n");
  277. return 0;
  278. }
  279. }
  280. return ok;
  281. }
  282. /*
  283. * extract files using the info the "local file headers"
  284. */
  285. static int
  286. sunzip(Biobuf *bin)
  287. {
  288. int eok;
  289. for(;;){
  290. eok = unzipEntry(bin, nil);
  291. if(eok == 0)
  292. return 1;
  293. if(eok < 0)
  294. return 0;
  295. }
  296. }
  297. /*
  298. * extracts a single entry from a zip file
  299. * czh is the optional corresponding central directory entry
  300. */
  301. static int
  302. unzipEntry(Biobuf *bin, ZipHead *czh)
  303. {
  304. Dir *d;
  305. ZipHead zh;
  306. char *p;
  307. vlong off;
  308. int fd, isdir, ok, err;
  309. zh.file = nil;
  310. if(setjmp(zjmp)){
  311. delfile = nil;
  312. free(zh.file);
  313. return -1;
  314. }
  315. memset(&zh, 0, sizeof(zh));
  316. if(!header(bin, &zh))
  317. return 0;
  318. ok = 1;
  319. isdir = 0;
  320. fd = -1;
  321. if(wantFile(zh.file)){
  322. if(verbose)
  323. fprint(2, "extracting %s\n", zh.file);
  324. if(czh != nil && czh->extos == ZDos){
  325. isdir = czh->eattr & ZDDir;
  326. if(isdir && zh.uncsize != 0)
  327. fprint(2, "unzip: ignoring directory data for %s\n", zh.file);
  328. }
  329. if(zh.meth == 0 && zh.uncsize == 0){
  330. p = strchr(zh.file, '\0');
  331. if(p > zh.file && p[-1] == '/')
  332. isdir = 1;
  333. }
  334. if(stdout){
  335. if(ok && !isdir)
  336. fd = 1;
  337. }else if(isdir){
  338. fd = create(zh.file, OREAD, DMDIR | 0775);
  339. if(fd < 0){
  340. d = dirstat(zh.file);
  341. if(d == nil || (d->mode & DMDIR) != DMDIR){
  342. fprint(2, "unzip: can't create directory %s: %r\n", zh.file);
  343. ok = 0;
  344. }
  345. free(d);
  346. }
  347. }else if(ok){
  348. fd = create(zh.file, OWRITE, 0664);
  349. if(fd < 0){
  350. fprint(2, "unzip: can't create %s: %r\n", zh.file);
  351. ok = 0;
  352. }else
  353. delfile = zh.file;
  354. }
  355. }
  356. wlen = 0;
  357. rlen = 0;
  358. crc = 0;
  359. wbad = 0;
  360. if(zh.meth == 0){
  361. if(!copyout(fd, bin, zh.csize))
  362. error("copying data for %s failed: %r", zh.file);
  363. }else if(zh.meth == 8){
  364. off = Boffset(bin);
  365. err = inflate((void*)fd, crcwrite, bin, (int(*)(void*))Bgetc);
  366. if(err != FlateOk)
  367. error("inflate failed: %s", flateerr(err));
  368. rlen = Boffset(bin) - off;
  369. }else
  370. error("can't handle compression method %d for %s", zh.meth, zh.file);
  371. trailer(bin, &zh);
  372. if(zh.crc != crc)
  373. error("crc mismatch for %s", zh.file);
  374. if(zh.uncsize != wlen)
  375. error("output size mismatch for %s", zh.file);
  376. if(zh.csize != rlen)
  377. error("input size mismatch for %s", zh.file);
  378. delfile = nil;
  379. free(zh.file);
  380. if(fd >= 0 && !stdout){
  381. if(settimes){
  382. d = dirfstat(fd);
  383. if(d != nil){
  384. d->mtime = msdos2time(zh.modtime, zh.moddate);
  385. if(d->mtime)
  386. dirfwstat(fd, d);
  387. }
  388. }
  389. close(fd);
  390. }
  391. return ok;
  392. }
  393. static int
  394. wantFile(char *file)
  395. {
  396. int i, n;
  397. if(nwant == 0)
  398. return 1;
  399. for(i = 0; i < nwant; i++){
  400. if(strcmp(want[i], file) == 0)
  401. return 1;
  402. n = strlen(want[i]);
  403. if(strncmp(want[i], file, n) == 0 && file[n] == '/')
  404. return 1;
  405. }
  406. return 0;
  407. }
  408. /*
  409. * find the start of the central directory
  410. * returns the number of entries in the directory,
  411. * or -1 if there was an error
  412. */
  413. static int
  414. findCDir(Biobuf *bin, char *file)
  415. {
  416. vlong ecoff;
  417. long off, size, m;
  418. int entries, zclen, dn, ds, de;
  419. ecoff = Bseek(bin, -ZECHeadSize, 2);
  420. if(ecoff < 0){
  421. fprint(2, "unzip: can't seek to contents of %s\n", file);
  422. longjmp(seekjmp, 1);
  423. return -1;
  424. }
  425. if(setjmp(zjmp))
  426. return -1;
  427. if((m=get4(bin)) != ZECHeader){
  428. fprint(2, "unzip: bad magic number for table of contents of %s: %#.8lx\n", file, m);
  429. longjmp(seekjmp, 1);
  430. return -1;
  431. }
  432. dn = get2(bin);
  433. ds = get2(bin);
  434. de = get2(bin);
  435. entries = get2(bin);
  436. size = get4(bin);
  437. off = get4(bin);
  438. zclen = get2(bin);
  439. while(zclen-- > 0)
  440. get1(bin);
  441. if(verbose > 1){
  442. print("table starts at %ld for %ld bytes\n", off, size);
  443. if(ecoff - size != off)
  444. print("\ttable should start at %lld-%ld=%lld\n", ecoff, size, ecoff-size);
  445. if(dn || ds || de != entries)
  446. print("\tcurrent disk=%d start disk=%d table entries on this disk=%d\n", dn, ds, de);
  447. }
  448. if(Bseek(bin, off, 0) != off){
  449. fprint(2, "unzip: can't seek to start of contents of %s\n", file);
  450. longjmp(seekjmp, 1);
  451. return -1;
  452. }
  453. return entries;
  454. }
  455. static int
  456. cheader(Biobuf *bin, ZipHead *zh)
  457. {
  458. ulong v;
  459. int flen, xlen, fclen;
  460. v = get4(bin);
  461. if(v != ZCHeader){
  462. if(v == ZECHeader)
  463. return 0;
  464. error("bad magic number %lux", v);
  465. }
  466. zh->madevers = get1(bin);
  467. zh->madeos = get1(bin);
  468. zh->extvers = get1(bin);
  469. zh->extos = get1(bin);
  470. zh->flags = get2(bin);
  471. zh->meth = get2(bin);
  472. zh->modtime = get2(bin);
  473. zh->moddate = get2(bin);
  474. zh->crc = get4(bin);
  475. zh->csize = get4(bin);
  476. zh->uncsize = get4(bin);
  477. flen = get2(bin);
  478. xlen = get2(bin);
  479. fclen = get2(bin);
  480. get2(bin); /* disk number start */
  481. zh->iattr = get2(bin);
  482. zh->eattr = get4(bin);
  483. zh->off = get4(bin);
  484. zh->file = getname(bin, flen);
  485. while(xlen-- > 0)
  486. get1(bin);
  487. while(fclen-- > 0)
  488. get1(bin);
  489. return 1;
  490. }
  491. static int
  492. header(Biobuf *bin, ZipHead *zh)
  493. {
  494. ulong v;
  495. int flen, xlen;
  496. v = get4(bin);
  497. if(v != ZHeader){
  498. if(v == ZCHeader)
  499. return 0;
  500. error("bad magic number %lux at %lld", v, Boffset(bin)-4);
  501. }
  502. zh->extvers = get1(bin);
  503. zh->extos = get1(bin);
  504. zh->flags = get2(bin);
  505. zh->meth = get2(bin);
  506. zh->modtime = get2(bin);
  507. zh->moddate = get2(bin);
  508. zh->crc = get4(bin);
  509. zh->csize = get4(bin);
  510. zh->uncsize = get4(bin);
  511. flen = get2(bin);
  512. xlen = get2(bin);
  513. zh->file = getname(bin, flen);
  514. while(xlen-- > 0)
  515. get1(bin);
  516. return 1;
  517. }
  518. static void
  519. trailer(Biobuf *bin, ZipHead *zh)
  520. {
  521. if(zh->flags & ZTrailInfo){
  522. zh->crc = get4(bin);
  523. zh->csize = get4(bin);
  524. zh->uncsize = get4(bin);
  525. }
  526. }
  527. static char*
  528. getname(Biobuf *bin, int len)
  529. {
  530. char *s;
  531. int i, c;
  532. s = emalloc(len + 1);
  533. for(i = 0; i < len; i++){
  534. c = get1(bin);
  535. if(lower)
  536. c = tolower(c);
  537. s[i] = c;
  538. }
  539. s[i] = '\0';
  540. return s;
  541. }
  542. static int
  543. crcwrite(void *out, void *buf, int n)
  544. {
  545. int fd, nw;
  546. wlen += n;
  547. crc = blockcrc(crctab, crc, buf, n);
  548. fd = (int)(uintptr)out;
  549. if(fd < 0)
  550. return n;
  551. nw = write(fd, buf, n);
  552. if(nw != n)
  553. wbad = 1;
  554. return nw;
  555. }
  556. static int
  557. copyout(int ofd, Biobuf *bin, long len)
  558. {
  559. char buf[BufSize];
  560. int n;
  561. for(; len > 0; len -= n){
  562. n = len;
  563. if(n > BufSize)
  564. n = BufSize;
  565. n = Bread(bin, buf, n);
  566. if(n <= 0)
  567. return 0;
  568. rlen += n;
  569. if(crcwrite((void*)ofd, buf, n) != n)
  570. return 0;
  571. }
  572. return 1;
  573. }
  574. static ulong
  575. get4(Biobuf *b)
  576. {
  577. ulong v;
  578. int i, c;
  579. v = 0;
  580. for(i = 0; i < 4; i++){
  581. c = Bgetc(b);
  582. if(c < 0)
  583. error("unexpected eof reading file information");
  584. v |= c << (i * 8);
  585. }
  586. return v;
  587. }
  588. static int
  589. get2(Biobuf *b)
  590. {
  591. int i, c, v;
  592. v = 0;
  593. for(i = 0; i < 2; i++){
  594. c = Bgetc(b);
  595. if(c < 0)
  596. error("unexpected eof reading file information");
  597. v |= c << (i * 8);
  598. }
  599. return v;
  600. }
  601. static int
  602. get1(Biobuf *b)
  603. {
  604. int c;
  605. c = Bgetc(b);
  606. if(c < 0)
  607. error("unexpected eof reading file information");
  608. return c;
  609. }
  610. static long
  611. msdos2time(int time, int date)
  612. {
  613. Tm tm;
  614. tm.hour = time >> 11;
  615. tm.min = (time >> 5) & 63;
  616. tm.sec = (time & 31) << 1;
  617. tm.year = 80 + (date >> 9);
  618. tm.mon = ((date >> 5) & 15) - 1;
  619. tm.mday = date & 31;
  620. tm.zone[0] = '\0';
  621. tm.yday = 0;
  622. return tm2sec(&tm);
  623. }
  624. static void*
  625. emalloc(ulong n)
  626. {
  627. void *p;
  628. p = malloc(n);
  629. if(p == nil)
  630. sysfatal("out of memory");
  631. return p;
  632. }
  633. static void
  634. error(char *fmt, ...)
  635. {
  636. va_list arg;
  637. fprint(2, "unzip: ");
  638. va_start(arg, fmt);
  639. vfprint(2, fmt, arg);
  640. va_end(arg);
  641. fprint(2, "\n");
  642. if(delfile != nil){
  643. fprint(2, "unzip: removing output file %s\n", delfile);
  644. remove(delfile);
  645. delfile = nil;
  646. }
  647. longjmp(zjmp, 1);
  648. }