unzip.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini unzip implementation for busybox
  4. *
  5. * Copyright (C) 2004 by Ed Clark
  6. *
  7. * Loosely based on original busybox unzip applet by Laurence Anderson.
  8. * All options and features should work in this version.
  9. *
  10. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  11. */
  12. /* For reference see
  13. * http://www.pkware.com/company/standards/appnote/
  14. * http://www.info-zip.org/pub/infozip/doc/appnote-iz-latest.zip
  15. *
  16. * TODO
  17. * Zip64 + other methods
  18. */
  19. //config:config UNZIP
  20. //config: bool "unzip"
  21. //config: default y
  22. //config: help
  23. //config: unzip will list or extract files from a ZIP archive,
  24. //config: commonly found on DOS/WIN systems. The default behavior
  25. //config: (with no options) is to extract the archive into the
  26. //config: current directory. Use the `-d' option to extract to a
  27. //config: directory of your choice.
  28. //applet:IF_UNZIP(APPLET(unzip, BB_DIR_USR_BIN, BB_SUID_DROP))
  29. //kbuild:lib-$(CONFIG_UNZIP) += unzip.o
  30. //usage:#define unzip_trivial_usage
  31. //usage: "[-lnopq] FILE[.zip] [FILE]... [-x FILE...] [-d DIR]"
  32. //usage:#define unzip_full_usage "\n\n"
  33. //usage: "Extract FILEs from ZIP archive\n"
  34. //usage: "\n -l List contents (with -q for short form)"
  35. //usage: "\n -n Never overwrite files (default: ask)"
  36. //usage: "\n -o Overwrite"
  37. //usage: "\n -p Print to stdout"
  38. //usage: "\n -q Quiet"
  39. //usage: "\n -x FILE Exclude FILEs"
  40. //usage: "\n -d DIR Extract into DIR"
  41. #include "libbb.h"
  42. #include "bb_archive.h"
  43. enum {
  44. #if BB_BIG_ENDIAN
  45. ZIP_FILEHEADER_MAGIC = 0x504b0304,
  46. ZIP_CDF_MAGIC = 0x504b0102, /* central directory's file header */
  47. ZIP_CDE_MAGIC = 0x504b0506, /* "end of central directory" record */
  48. ZIP_DD_MAGIC = 0x504b0708,
  49. #else
  50. ZIP_FILEHEADER_MAGIC = 0x04034b50,
  51. ZIP_CDF_MAGIC = 0x02014b50,
  52. ZIP_CDE_MAGIC = 0x06054b50,
  53. ZIP_DD_MAGIC = 0x08074b50,
  54. #endif
  55. };
  56. #define ZIP_HEADER_LEN 26
  57. typedef union {
  58. uint8_t raw[ZIP_HEADER_LEN];
  59. struct {
  60. uint16_t version; /* 0-1 */
  61. uint16_t zip_flags; /* 2-3 */
  62. uint16_t method; /* 4-5 */
  63. uint16_t modtime; /* 6-7 */
  64. uint16_t moddate; /* 8-9 */
  65. uint32_t crc32 PACKED; /* 10-13 */
  66. uint32_t cmpsize PACKED; /* 14-17 */
  67. uint32_t ucmpsize PACKED; /* 18-21 */
  68. uint16_t filename_len; /* 22-23 */
  69. uint16_t extra_len; /* 24-25 */
  70. } formatted PACKED;
  71. } zip_header_t; /* PACKED - gcc 4.2.1 doesn't like it (spews warning) */
  72. /* Check the offset of the last element, not the length. This leniency
  73. * allows for poor packing, whereby the overall struct may be too long,
  74. * even though the elements are all in the right place.
  75. */
  76. struct BUG_zip_header_must_be_26_bytes {
  77. char BUG_zip_header_must_be_26_bytes[
  78. offsetof(zip_header_t, formatted.extra_len) + 2
  79. == ZIP_HEADER_LEN ? 1 : -1];
  80. };
  81. #define FIX_ENDIANNESS_ZIP(zip_header) do { \
  82. (zip_header).formatted.version = SWAP_LE16((zip_header).formatted.version ); \
  83. (zip_header).formatted.method = SWAP_LE16((zip_header).formatted.method ); \
  84. (zip_header).formatted.modtime = SWAP_LE16((zip_header).formatted.modtime ); \
  85. (zip_header).formatted.moddate = SWAP_LE16((zip_header).formatted.moddate ); \
  86. (zip_header).formatted.crc32 = SWAP_LE32((zip_header).formatted.crc32 ); \
  87. (zip_header).formatted.cmpsize = SWAP_LE32((zip_header).formatted.cmpsize ); \
  88. (zip_header).formatted.ucmpsize = SWAP_LE32((zip_header).formatted.ucmpsize ); \
  89. (zip_header).formatted.filename_len = SWAP_LE16((zip_header).formatted.filename_len); \
  90. (zip_header).formatted.extra_len = SWAP_LE16((zip_header).formatted.extra_len ); \
  91. } while (0)
  92. #define CDF_HEADER_LEN 42
  93. typedef union {
  94. uint8_t raw[CDF_HEADER_LEN];
  95. struct {
  96. /* uint32_t signature; 50 4b 01 02 */
  97. uint16_t version_made_by; /* 0-1 */
  98. uint16_t version_needed; /* 2-3 */
  99. uint16_t cdf_flags; /* 4-5 */
  100. uint16_t method; /* 6-7 */
  101. uint16_t mtime; /* 8-9 */
  102. uint16_t mdate; /* 10-11 */
  103. uint32_t crc32; /* 12-15 */
  104. uint32_t cmpsize; /* 16-19 */
  105. uint32_t ucmpsize; /* 20-23 */
  106. uint16_t file_name_length; /* 24-25 */
  107. uint16_t extra_field_length; /* 26-27 */
  108. uint16_t file_comment_length; /* 28-29 */
  109. uint16_t disk_number_start; /* 30-31 */
  110. uint16_t internal_file_attributes; /* 32-33 */
  111. uint32_t external_file_attributes PACKED; /* 34-37 */
  112. uint32_t relative_offset_of_local_header PACKED; /* 38-41 */
  113. } formatted PACKED;
  114. } cdf_header_t;
  115. struct BUG_cdf_header_must_be_42_bytes {
  116. char BUG_cdf_header_must_be_42_bytes[
  117. offsetof(cdf_header_t, formatted.relative_offset_of_local_header) + 4
  118. == CDF_HEADER_LEN ? 1 : -1];
  119. };
  120. #define FIX_ENDIANNESS_CDF(cdf_header) do { \
  121. (cdf_header).formatted.crc32 = SWAP_LE32((cdf_header).formatted.crc32 ); \
  122. (cdf_header).formatted.cmpsize = SWAP_LE32((cdf_header).formatted.cmpsize ); \
  123. (cdf_header).formatted.ucmpsize = SWAP_LE32((cdf_header).formatted.ucmpsize ); \
  124. (cdf_header).formatted.file_name_length = SWAP_LE16((cdf_header).formatted.file_name_length); \
  125. (cdf_header).formatted.extra_field_length = SWAP_LE16((cdf_header).formatted.extra_field_length); \
  126. (cdf_header).formatted.file_comment_length = SWAP_LE16((cdf_header).formatted.file_comment_length); \
  127. IF_DESKTOP( \
  128. (cdf_header).formatted.version_made_by = SWAP_LE16((cdf_header).formatted.version_made_by); \
  129. (cdf_header).formatted.external_file_attributes = SWAP_LE32((cdf_header).formatted.external_file_attributes); \
  130. ) \
  131. } while (0)
  132. #define CDE_HEADER_LEN 16
  133. typedef union {
  134. uint8_t raw[CDE_HEADER_LEN];
  135. struct {
  136. /* uint32_t signature; 50 4b 05 06 */
  137. uint16_t this_disk_no;
  138. uint16_t disk_with_cdf_no;
  139. uint16_t cdf_entries_on_this_disk;
  140. uint16_t cdf_entries_total;
  141. uint32_t cdf_size;
  142. uint32_t cdf_offset;
  143. /* uint16_t file_comment_length; */
  144. /* .ZIP file comment (variable size) */
  145. } formatted PACKED;
  146. } cde_header_t;
  147. struct BUG_cde_header_must_be_16_bytes {
  148. char BUG_cde_header_must_be_16_bytes[
  149. sizeof(cde_header_t) == CDE_HEADER_LEN ? 1 : -1];
  150. };
  151. #define FIX_ENDIANNESS_CDE(cde_header) do { \
  152. (cde_header).formatted.cdf_offset = SWAP_LE32((cde_header).formatted.cdf_offset); \
  153. } while (0)
  154. enum { zip_fd = 3 };
  155. #if ENABLE_DESKTOP
  156. /* Seen in the wild:
  157. * Self-extracting PRO2K3XP_32.exe contains 19078464 byte zip archive,
  158. * where CDE was nearly 48 kbytes before EOF.
  159. * (Surprisingly, it also apparently has *another* CDE structure
  160. * closer to the end, with bogus cdf_offset).
  161. * To make extraction work, bumped PEEK_FROM_END from 16k to 64k.
  162. */
  163. #define PEEK_FROM_END (64*1024)
  164. /* This value means that we failed to find CDF */
  165. #define BAD_CDF_OFFSET ((uint32_t)0xffffffff)
  166. /* NB: does not preserve file position! */
  167. static uint32_t find_cdf_offset(void)
  168. {
  169. cde_header_t cde_header;
  170. unsigned char *p;
  171. off_t end;
  172. unsigned char *buf = xzalloc(PEEK_FROM_END);
  173. end = xlseek(zip_fd, 0, SEEK_END);
  174. end -= PEEK_FROM_END;
  175. if (end < 0)
  176. end = 0;
  177. xlseek(zip_fd, end, SEEK_SET);
  178. full_read(zip_fd, buf, PEEK_FROM_END);
  179. cde_header.formatted.cdf_offset = BAD_CDF_OFFSET;
  180. p = buf;
  181. while (p <= buf + PEEK_FROM_END - CDE_HEADER_LEN - 4) {
  182. if (*p != 'P') {
  183. p++;
  184. continue;
  185. }
  186. if (*++p != 'K')
  187. continue;
  188. if (*++p != 5)
  189. continue;
  190. if (*++p != 6)
  191. continue;
  192. /* we found CDE! */
  193. memcpy(cde_header.raw, p + 1, CDE_HEADER_LEN);
  194. FIX_ENDIANNESS_CDE(cde_header);
  195. /*
  196. * I've seen .ZIP files with seemingly valid CDEs
  197. * where cdf_offset points past EOF - ??
  198. * Ignore such CDEs:
  199. */
  200. if (cde_header.formatted.cdf_offset < end + (p - buf))
  201. break;
  202. cde_header.formatted.cdf_offset = BAD_CDF_OFFSET;
  203. }
  204. free(buf);
  205. return cde_header.formatted.cdf_offset;
  206. };
  207. static uint32_t read_next_cdf(uint32_t cdf_offset, cdf_header_t *cdf_ptr)
  208. {
  209. off_t org;
  210. org = xlseek(zip_fd, 0, SEEK_CUR);
  211. if (!cdf_offset)
  212. cdf_offset = find_cdf_offset();
  213. if (cdf_offset != BAD_CDF_OFFSET) {
  214. xlseek(zip_fd, cdf_offset + 4, SEEK_SET);
  215. xread(zip_fd, cdf_ptr->raw, CDF_HEADER_LEN);
  216. FIX_ENDIANNESS_CDF(*cdf_ptr);
  217. cdf_offset += 4 + CDF_HEADER_LEN
  218. + cdf_ptr->formatted.file_name_length
  219. + cdf_ptr->formatted.extra_field_length
  220. + cdf_ptr->formatted.file_comment_length;
  221. }
  222. xlseek(zip_fd, org, SEEK_SET);
  223. return cdf_offset;
  224. };
  225. #endif
  226. static void unzip_skip(off_t skip)
  227. {
  228. if (skip != 0)
  229. if (lseek(zip_fd, skip, SEEK_CUR) == (off_t)-1)
  230. bb_copyfd_exact_size(zip_fd, -1, skip);
  231. }
  232. static void unzip_create_leading_dirs(const char *fn)
  233. {
  234. /* Create all leading directories */
  235. char *name = xstrdup(fn);
  236. if (bb_make_directory(dirname(name), 0777, FILEUTILS_RECUR)) {
  237. xfunc_die(); /* bb_make_directory is noisy */
  238. }
  239. free(name);
  240. }
  241. static void unzip_extract(zip_header_t *zip_header, int dst_fd)
  242. {
  243. if (zip_header->formatted.method == 0) {
  244. /* Method 0 - stored (not compressed) */
  245. off_t size = zip_header->formatted.ucmpsize;
  246. if (size)
  247. bb_copyfd_exact_size(zip_fd, dst_fd, size);
  248. } else {
  249. /* Method 8 - inflate */
  250. transformer_state_t xstate;
  251. init_transformer_state(&xstate);
  252. xstate.bytes_in = zip_header->formatted.cmpsize;
  253. xstate.src_fd = zip_fd;
  254. xstate.dst_fd = dst_fd;
  255. if (inflate_unzip(&xstate) < 0)
  256. bb_error_msg_and_die("inflate error");
  257. /* Validate decompression - crc */
  258. if (zip_header->formatted.crc32 != (xstate.crc32 ^ 0xffffffffL)) {
  259. bb_error_msg_and_die("crc error");
  260. }
  261. /* Validate decompression - size */
  262. if (zip_header->formatted.ucmpsize != xstate.bytes_out) {
  263. /* Don't die. Who knows, maybe len calculation
  264. * was botched somewhere. After all, crc matched! */
  265. bb_error_msg("bad length");
  266. }
  267. }
  268. }
  269. static void my_fgets80(char *buf80)
  270. {
  271. fflush_all();
  272. if (!fgets(buf80, 80, stdin)) {
  273. bb_perror_msg_and_die("can't read standard input");
  274. }
  275. }
  276. int unzip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  277. int unzip_main(int argc, char **argv)
  278. {
  279. enum { O_PROMPT, O_NEVER, O_ALWAYS };
  280. zip_header_t zip_header;
  281. smallint quiet = 0;
  282. IF_NOT_DESKTOP(const) smallint verbose = 0;
  283. smallint listing = 0;
  284. smallint overwrite = O_PROMPT;
  285. smallint x_opt_seen;
  286. #if ENABLE_DESKTOP
  287. uint32_t cdf_offset;
  288. #endif
  289. unsigned long total_usize;
  290. unsigned long total_size;
  291. unsigned total_entries;
  292. int dst_fd = -1;
  293. char *src_fn = NULL;
  294. char *dst_fn = NULL;
  295. llist_t *zaccept = NULL;
  296. llist_t *zreject = NULL;
  297. char *base_dir = NULL;
  298. int i, opt;
  299. char key_buf[80]; /* must match size used by my_fgets80 */
  300. struct stat stat_buf;
  301. /* -q, -l and -v: UnZip 5.52 of 28 February 2005, by Info-ZIP:
  302. *
  303. * # /usr/bin/unzip -qq -v decompress_unlzma.i.zip
  304. * 204372 Defl:N 35278 83% 09-06-09 14:23 0d056252 decompress_unlzma.i
  305. * # /usr/bin/unzip -q -v decompress_unlzma.i.zip
  306. * Length Method Size Ratio Date Time CRC-32 Name
  307. * -------- ------ ------- ----- ---- ---- ------ ----
  308. * 204372 Defl:N 35278 83% 09-06-09 14:23 0d056252 decompress_unlzma.i
  309. * -------- ------- --- -------
  310. * 204372 35278 83% 1 file
  311. * # /usr/bin/unzip -v decompress_unlzma.i.zip
  312. * Archive: decompress_unlzma.i.zip
  313. * Length Method Size Ratio Date Time CRC-32 Name
  314. * -------- ------ ------- ----- ---- ---- ------ ----
  315. * 204372 Defl:N 35278 83% 09-06-09 14:23 0d056252 decompress_unlzma.i
  316. * -------- ------- --- -------
  317. * 204372 35278 83% 1 file
  318. * # unzip -v decompress_unlzma.i.zip
  319. * Archive: decompress_unlzma.i.zip
  320. * Length Date Time Name
  321. * -------- ---- ---- ----
  322. * 204372 09-06-09 14:23 decompress_unlzma.i
  323. * -------- -------
  324. * 204372 1 files
  325. * # /usr/bin/unzip -l -qq decompress_unlzma.i.zip
  326. * 204372 09-06-09 14:23 decompress_unlzma.i
  327. * # /usr/bin/unzip -l -q decompress_unlzma.i.zip
  328. * Length Date Time Name
  329. * -------- ---- ---- ----
  330. * 204372 09-06-09 14:23 decompress_unlzma.i
  331. * -------- -------
  332. * 204372 1 file
  333. * # /usr/bin/unzip -l decompress_unlzma.i.zip
  334. * Archive: decompress_unlzma.i.zip
  335. * Length Date Time Name
  336. * -------- ---- ---- ----
  337. * 204372 09-06-09 14:23 decompress_unlzma.i
  338. * -------- -------
  339. * 204372 1 file
  340. */
  341. x_opt_seen = 0;
  342. /* '-' makes getopt return 1 for non-options */
  343. while ((opt = getopt(argc, argv, "-d:lnopqxv")) != -1) {
  344. switch (opt) {
  345. case 'd': /* Extract to base directory */
  346. base_dir = optarg;
  347. break;
  348. case 'l': /* List */
  349. listing = 1;
  350. break;
  351. case 'n': /* Never overwrite existing files */
  352. overwrite = O_NEVER;
  353. break;
  354. case 'o': /* Always overwrite existing files */
  355. overwrite = O_ALWAYS;
  356. break;
  357. case 'p': /* Extract files to stdout and fall through to set verbosity */
  358. dst_fd = STDOUT_FILENO;
  359. case 'q': /* Be quiet */
  360. quiet++;
  361. break;
  362. case 'v': /* Verbose list */
  363. IF_DESKTOP(verbose++;)
  364. listing = 1;
  365. break;
  366. case 'x':
  367. x_opt_seen = 1;
  368. break;
  369. case 1:
  370. if (!src_fn) {
  371. /* The zip file */
  372. /* +5: space for ".zip" and NUL */
  373. src_fn = xmalloc(strlen(optarg) + 5);
  374. strcpy(src_fn, optarg);
  375. } else if (!x_opt_seen) {
  376. /* Include files */
  377. llist_add_to(&zaccept, optarg);
  378. } else {
  379. /* Exclude files */
  380. llist_add_to(&zreject, optarg);
  381. }
  382. break;
  383. default:
  384. bb_show_usage();
  385. }
  386. }
  387. #ifndef __GLIBC__
  388. /*
  389. * This code is needed for non-GNU getopt
  390. * which doesn't understand "-" in option string.
  391. * The -x option won't work properly in this case:
  392. * "unzip a.zip q -x w e" will be interpreted as
  393. * "unzip a.zip q w e -x" = "unzip a.zip q w e"
  394. */
  395. argv += optind;
  396. if (argv[0]) {
  397. /* +5: space for ".zip" and NUL */
  398. src_fn = xmalloc(strlen(argv[0]) + 5);
  399. strcpy(src_fn, argv[0]);
  400. while (*++argv)
  401. llist_add_to(&zaccept, *argv);
  402. }
  403. #endif
  404. if (!src_fn) {
  405. bb_show_usage();
  406. }
  407. /* Open input file */
  408. if (LONE_DASH(src_fn)) {
  409. xdup2(STDIN_FILENO, zip_fd);
  410. /* Cannot use prompt mode since zip data is arriving on STDIN */
  411. if (overwrite == O_PROMPT)
  412. overwrite = O_NEVER;
  413. } else {
  414. static const char extn[][5] = { ".zip", ".ZIP" };
  415. char *ext = src_fn + strlen(src_fn);
  416. int src_fd;
  417. i = 0;
  418. for (;;) {
  419. src_fd = open(src_fn, O_RDONLY);
  420. if (src_fd >= 0)
  421. break;
  422. if (++i > 2) {
  423. *ext = '\0';
  424. bb_error_msg_and_die("can't open %s[.zip]", src_fn);
  425. }
  426. strcpy(ext, extn[i - 1]);
  427. }
  428. xmove_fd(src_fd, zip_fd);
  429. }
  430. /* Change dir if necessary */
  431. if (base_dir)
  432. xchdir(base_dir);
  433. if (quiet <= 1) { /* not -qq */
  434. if (quiet == 0)
  435. printf("Archive: %s\n", src_fn);
  436. if (listing) {
  437. puts(verbose ?
  438. " Length Method Size Ratio Date Time CRC-32 Name\n"
  439. "-------- ------ ------- ----- ---- ---- ------ ----"
  440. :
  441. " Length Date Time Name\n"
  442. " -------- ---- ---- ----"
  443. );
  444. }
  445. }
  446. /* Example of an archive with one 0-byte long file named 'z'
  447. * created by Zip 2.31 on Unix:
  448. * 0000 [50 4b]03 04 0a 00 00 00 00 00 42 1a b8 3c 00 00 |PK........B..<..|
  449. * sig........ vneed flags compr mtime mdate crc32>
  450. * 0010 00 00 00 00 00 00 00 00 00 00 01 00 15 00 7a 55 |..............zU|
  451. * >..... csize...... usize...... fnlen exlen fn ex>
  452. * 0020 54 09 00 03 cc d3 f9 4b cc d3 f9 4b 55 78 04 00 |T......K...KUx..|
  453. * >tra_field......................................
  454. * 0030 00 00 00 00[50 4b]01 02 17 03 0a 00 00 00 00 00 |....PK..........|
  455. * ........... sig........ vmade vneed flags compr
  456. * 0040 42 1a b8 3c 00 00 00 00 00 00 00 00 00 00 00 00 |B..<............|
  457. * mtime mdate crc32...... csize...... usize......
  458. * 0050 01 00 0d 00 00 00 00 00 00 00 00 00 a4 81 00 00 |................|
  459. * fnlen exlen clen. dnum. iattr eattr...... relofs> (eattr = rw-r--r--)
  460. * 0060 00 00 7a 55 54 05 00 03 cc d3 f9 4b 55 78 00 00 |..zUT......KUx..|
  461. * >..... fn extra_field...........................
  462. * 0070 [50 4b]05 06 00 00 00 00 01 00 01 00 3c 00 00 00 |PK..........<...|
  463. * 0080 34 00 00 00 00 00 |4.....|
  464. */
  465. total_usize = 0;
  466. total_size = 0;
  467. total_entries = 0;
  468. #if ENABLE_DESKTOP
  469. cdf_offset = 0;
  470. #endif
  471. while (1) {
  472. uint32_t magic;
  473. mode_t dir_mode = 0777;
  474. #if ENABLE_DESKTOP
  475. mode_t file_mode = 0666;
  476. #endif
  477. /* Check magic number */
  478. xread(zip_fd, &magic, 4);
  479. /* Central directory? It's at the end, so exit */
  480. if (magic == ZIP_CDF_MAGIC)
  481. break;
  482. #if ENABLE_DESKTOP
  483. /* Data descriptor? It was a streaming file, go on */
  484. if (magic == ZIP_DD_MAGIC) {
  485. /* skip over duplicate crc32, cmpsize and ucmpsize */
  486. unzip_skip(3 * 4);
  487. continue;
  488. }
  489. #endif
  490. if (magic != ZIP_FILEHEADER_MAGIC)
  491. bb_error_msg_and_die("invalid zip magic %08X", (int)magic);
  492. /* Read the file header */
  493. xread(zip_fd, zip_header.raw, ZIP_HEADER_LEN);
  494. FIX_ENDIANNESS_ZIP(zip_header);
  495. if ((zip_header.formatted.method != 0) && (zip_header.formatted.method != 8)) {
  496. bb_error_msg_and_die("unsupported method %d", zip_header.formatted.method);
  497. }
  498. #if !ENABLE_DESKTOP
  499. if (zip_header.formatted.zip_flags & SWAP_LE16(0x0009)) {
  500. bb_error_msg_and_die("zip flags 1 and 8 are not supported");
  501. }
  502. #else
  503. if (zip_header.formatted.zip_flags & SWAP_LE16(0x0001)) {
  504. /* 0x0001 - encrypted */
  505. bb_error_msg_and_die("zip flag 1 (encryption) is not supported");
  506. }
  507. if (cdf_offset != BAD_CDF_OFFSET) {
  508. cdf_header_t cdf_header;
  509. cdf_offset = read_next_cdf(cdf_offset, &cdf_header);
  510. /*
  511. * Note: cdf_offset can become BAD_CDF_OFFSET after the above call.
  512. */
  513. if (zip_header.formatted.zip_flags & SWAP_LE16(0x0008)) {
  514. /* 0x0008 - streaming. [u]cmpsize can be reliably gotten
  515. * only from Central Directory. See unzip_doc.txt
  516. */
  517. zip_header.formatted.crc32 = cdf_header.formatted.crc32;
  518. zip_header.formatted.cmpsize = cdf_header.formatted.cmpsize;
  519. zip_header.formatted.ucmpsize = cdf_header.formatted.ucmpsize;
  520. }
  521. if ((cdf_header.formatted.version_made_by >> 8) == 3) {
  522. /* This archive is created on Unix */
  523. dir_mode = file_mode = (cdf_header.formatted.external_file_attributes >> 16);
  524. }
  525. }
  526. if (cdf_offset == BAD_CDF_OFFSET
  527. && (zip_header.formatted.zip_flags & SWAP_LE16(0x0008))
  528. ) {
  529. /* If it's a streaming zip, we _require_ CDF */
  530. bb_error_msg_and_die("can't find file table");
  531. }
  532. #endif
  533. /* Read filename */
  534. free(dst_fn);
  535. dst_fn = xzalloc(zip_header.formatted.filename_len + 1);
  536. xread(zip_fd, dst_fn, zip_header.formatted.filename_len);
  537. /* Skip extra header bytes */
  538. unzip_skip(zip_header.formatted.extra_len);
  539. /* Guard against "/abspath", "/../" and similar attacks */
  540. overlapping_strcpy(dst_fn, strip_unsafe_prefix(dst_fn));
  541. /* Filter zip entries */
  542. if (find_list_entry(zreject, dst_fn)
  543. || (zaccept && !find_list_entry(zaccept, dst_fn))
  544. ) { /* Skip entry */
  545. i = 'n';
  546. } else {
  547. if (listing) {
  548. /* List entry */
  549. unsigned dostime = zip_header.formatted.modtime | (zip_header.formatted.moddate << 16);
  550. if (!verbose) {
  551. // " Length Date Time Name\n"
  552. // " -------- ---- ---- ----"
  553. printf( "%9u %02u-%02u-%02u %02u:%02u %s\n",
  554. (unsigned)zip_header.formatted.ucmpsize,
  555. (dostime & 0x01e00000) >> 21,
  556. (dostime & 0x001f0000) >> 16,
  557. (((dostime & 0xfe000000) >> 25) + 1980) % 100,
  558. (dostime & 0x0000f800) >> 11,
  559. (dostime & 0x000007e0) >> 5,
  560. dst_fn);
  561. total_usize += zip_header.formatted.ucmpsize;
  562. } else {
  563. unsigned long percents = zip_header.formatted.ucmpsize - zip_header.formatted.cmpsize;
  564. percents = percents * 100;
  565. if (zip_header.formatted.ucmpsize)
  566. percents /= zip_header.formatted.ucmpsize;
  567. // " Length Method Size Ratio Date Time CRC-32 Name\n"
  568. // "-------- ------ ------- ----- ---- ---- ------ ----"
  569. printf( "%8u Defl:N" "%9u%4u%% %02u-%02u-%02u %02u:%02u %08x %s\n",
  570. (unsigned)zip_header.formatted.ucmpsize,
  571. (unsigned)zip_header.formatted.cmpsize,
  572. (unsigned)percents,
  573. (dostime & 0x01e00000) >> 21,
  574. (dostime & 0x001f0000) >> 16,
  575. (((dostime & 0xfe000000) >> 25) + 1980) % 100,
  576. (dostime & 0x0000f800) >> 11,
  577. (dostime & 0x000007e0) >> 5,
  578. zip_header.formatted.crc32,
  579. dst_fn);
  580. total_usize += zip_header.formatted.ucmpsize;
  581. total_size += zip_header.formatted.cmpsize;
  582. }
  583. i = 'n';
  584. } else if (dst_fd == STDOUT_FILENO) {
  585. /* Extracting to STDOUT */
  586. i = -1;
  587. } else if (last_char_is(dst_fn, '/')) {
  588. /* Extract directory */
  589. if (stat(dst_fn, &stat_buf) == -1) {
  590. if (errno != ENOENT) {
  591. bb_perror_msg_and_die("can't stat '%s'", dst_fn);
  592. }
  593. if (!quiet) {
  594. printf(" creating: %s\n", dst_fn);
  595. }
  596. unzip_create_leading_dirs(dst_fn);
  597. if (bb_make_directory(dst_fn, dir_mode, FILEUTILS_IGNORE_CHMOD_ERR)) {
  598. xfunc_die();
  599. }
  600. } else {
  601. if (!S_ISDIR(stat_buf.st_mode)) {
  602. bb_error_msg_and_die("'%s' exists but is not a %s",
  603. dst_fn, "directory");
  604. }
  605. }
  606. i = 'n';
  607. } else {
  608. /* Extract file */
  609. check_file:
  610. if (stat(dst_fn, &stat_buf) == -1) {
  611. /* File does not exist */
  612. if (errno != ENOENT) {
  613. bb_perror_msg_and_die("can't stat '%s'", dst_fn);
  614. }
  615. i = 'y';
  616. } else {
  617. /* File already exists */
  618. if (overwrite == O_NEVER) {
  619. i = 'n';
  620. } else if (S_ISREG(stat_buf.st_mode)) {
  621. /* File is regular file */
  622. if (overwrite == O_ALWAYS) {
  623. i = 'y';
  624. } else {
  625. printf("replace %s? [y]es, [n]o, [A]ll, [N]one, [r]ename: ", dst_fn);
  626. my_fgets80(key_buf);
  627. i = key_buf[0];
  628. }
  629. } else {
  630. /* File is not regular file */
  631. bb_error_msg_and_die("'%s' exists but is not a %s",
  632. dst_fn, "regular file");
  633. }
  634. }
  635. }
  636. }
  637. switch (i) {
  638. case 'A':
  639. overwrite = O_ALWAYS;
  640. case 'y': /* Open file and fall into unzip */
  641. unzip_create_leading_dirs(dst_fn);
  642. #if ENABLE_DESKTOP
  643. dst_fd = xopen3(dst_fn, O_WRONLY | O_CREAT | O_TRUNC, file_mode);
  644. #else
  645. dst_fd = xopen(dst_fn, O_WRONLY | O_CREAT | O_TRUNC);
  646. #endif
  647. case -1: /* Unzip */
  648. if (!quiet) {
  649. printf(" inflating: %s\n", dst_fn);
  650. }
  651. unzip_extract(&zip_header, dst_fd);
  652. if (dst_fd != STDOUT_FILENO) {
  653. /* closing STDOUT is potentially bad for future business */
  654. close(dst_fd);
  655. }
  656. break;
  657. case 'N':
  658. overwrite = O_NEVER;
  659. case 'n':
  660. /* Skip entry data */
  661. unzip_skip(zip_header.formatted.cmpsize);
  662. break;
  663. case 'r':
  664. /* Prompt for new name */
  665. printf("new name: ");
  666. my_fgets80(key_buf);
  667. free(dst_fn);
  668. dst_fn = xstrdup(key_buf);
  669. chomp(dst_fn);
  670. goto check_file;
  671. default:
  672. printf("error: invalid response [%c]\n", (char)i);
  673. goto check_file;
  674. }
  675. total_entries++;
  676. }
  677. if (listing && quiet <= 1) {
  678. if (!verbose) {
  679. // " Length Date Time Name\n"
  680. // " -------- ---- ---- ----"
  681. printf( " -------- -------\n"
  682. "%9lu" " %u files\n",
  683. total_usize, total_entries);
  684. } else {
  685. unsigned long percents = total_usize - total_size;
  686. percents = percents * 100;
  687. if (total_usize)
  688. percents /= total_usize;
  689. // " Length Method Size Ratio Date Time CRC-32 Name\n"
  690. // "-------- ------ ------- ----- ---- ---- ------ ----"
  691. printf( "-------- ------- --- -------\n"
  692. "%8lu" "%17lu%4u%% %u files\n",
  693. total_usize, total_size, (unsigned)percents,
  694. total_entries);
  695. }
  696. }
  697. return 0;
  698. }