unzip.c 13 KB

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