unzip.c 22 KB

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