3
0

unzip.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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 the GPL v2 or later, see the file LICENSE in this tarball.
  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. * Endian issues
  18. * Zip64 + other methods
  19. * Improve handling of zip format, ie.
  20. * - deferred CRC, comp. & uncomp. lengths (zip header flags bit 3)
  21. * - unix file permissions, etc.
  22. * - central directory
  23. */
  24. #include "libbb.h"
  25. #include "unarchive.h"
  26. #define ZIP_FILEHEADER_MAGIC SWAP_LE32(0x04034b50)
  27. #define ZIP_CDS_MAGIC SWAP_LE32(0x02014b50)
  28. #define ZIP_CDS_END_MAGIC SWAP_LE32(0x06054b50)
  29. #define ZIP_DD_MAGIC SWAP_LE32(0x08074b50)
  30. typedef union {
  31. unsigned char raw[26];
  32. struct {
  33. unsigned short version; /* 0-1 */
  34. unsigned short flags; /* 2-3 */
  35. unsigned short method; /* 4-5 */
  36. unsigned short modtime; /* 6-7 */
  37. unsigned short moddate; /* 8-9 */
  38. unsigned int crc32 ATTRIBUTE_PACKED; /* 10-13 */
  39. unsigned int cmpsize ATTRIBUTE_PACKED; /* 14-17 */
  40. unsigned int ucmpsize ATTRIBUTE_PACKED; /* 18-21 */
  41. unsigned short filename_len; /* 22-23 */
  42. unsigned short extra_len; /* 24-25 */
  43. } formatted ATTRIBUTE_PACKED;
  44. } zip_header_t;
  45. static void unzip_skip(int fd, off_t skip)
  46. {
  47. if (lseek(fd, skip, SEEK_CUR) == (off_t)-1) {
  48. if (errno != ESPIPE)
  49. bb_error_msg_and_die("seek failure");
  50. bb_copyfd_exact_size(fd, -1, skip);
  51. }
  52. }
  53. static void unzip_create_leading_dirs(char *fn)
  54. {
  55. /* Create all leading directories */
  56. char *name = xstrdup(fn);
  57. if (bb_make_directory(dirname(name), 0777, FILEUTILS_RECUR)) {
  58. bb_error_msg_and_die("exiting"); /* bb_make_directory is noisy */
  59. }
  60. free(name);
  61. }
  62. static int unzip_extract(zip_header_t *zip_header, int src_fd, int dst_fd)
  63. {
  64. if (zip_header->formatted.method == 0) {
  65. /* Method 0 - stored (not compressed) */
  66. off_t size = zip_header->formatted.ucmpsize;
  67. if (size)
  68. bb_copyfd_exact_size(src_fd, dst_fd, size);
  69. } else {
  70. /* Method 8 - inflate */
  71. inflate_unzip_result res;
  72. /* err = */ inflate_unzip(&res, zip_header->formatted.cmpsize, src_fd, dst_fd);
  73. // we should check for -1 error return
  74. /* Validate decompression - crc */
  75. if (zip_header->formatted.crc32 != (res.crc ^ 0xffffffffL)) {
  76. bb_error_msg("invalid compressed data--%s error", "crc");
  77. return 1;
  78. }
  79. /* Validate decompression - size */
  80. if (zip_header->formatted.ucmpsize != res.bytes_out) {
  81. bb_error_msg("invalid compressed data--%s error", "length");
  82. return 1;
  83. }
  84. }
  85. return 0;
  86. }
  87. int unzip_main(int argc, char **argv);
  88. int unzip_main(int argc, char **argv)
  89. {
  90. zip_header_t zip_header;
  91. enum {v_silent, v_normal, v_list} verbosity = v_normal;
  92. enum {o_prompt, o_never, o_always} overwrite = o_prompt;
  93. unsigned int total_size = 0;
  94. unsigned int total_entries = 0;
  95. int src_fd = -1, dst_fd = -1;
  96. char *src_fn = NULL, *dst_fn = NULL;
  97. llist_t *zaccept = NULL;
  98. llist_t *zreject = NULL;
  99. char *base_dir = NULL;
  100. int failed, i, opt, opt_range = 0, list_header_done = 0;
  101. char key_buf[512];
  102. struct stat stat_buf;
  103. while ((opt = getopt(argc, argv, "-d:lnopqx")) != -1) {
  104. switch (opt_range) {
  105. case 0: /* Options */
  106. switch (opt) {
  107. case 'l': /* List */
  108. verbosity = v_list;
  109. break;
  110. case 'n': /* Never overwrite existing files */
  111. overwrite = o_never;
  112. break;
  113. case 'o': /* Always overwrite existing files */
  114. overwrite = o_always;
  115. break;
  116. case 'p': /* Extract files to stdout and fall through to set verbosity */
  117. dst_fd = STDOUT_FILENO;
  118. case 'q': /* Be quiet */
  119. verbosity = (verbosity == v_normal) ? v_silent : verbosity;
  120. break;
  121. case 1 : /* The zip file */
  122. src_fn = xmalloc(strlen(optarg)+4);
  123. strcpy(src_fn, optarg);
  124. opt_range++;
  125. break;
  126. default:
  127. bb_show_usage();
  128. }
  129. break;
  130. case 1: /* Include files */
  131. if (opt == 1) {
  132. llist_add_to(&zaccept, optarg);
  133. } else if (opt == 'd') {
  134. base_dir = optarg;
  135. opt_range += 2;
  136. } else if (opt == 'x') {
  137. opt_range++;
  138. } else {
  139. bb_show_usage();
  140. }
  141. break;
  142. case 2 : /* Exclude files */
  143. if (opt == 1) {
  144. llist_add_to(&zreject, optarg);
  145. } else if (opt == 'd') { /* Extract to base directory */
  146. base_dir = optarg;
  147. opt_range++;
  148. } else {
  149. bb_show_usage();
  150. }
  151. break;
  152. default:
  153. bb_show_usage();
  154. }
  155. }
  156. if (src_fn == NULL) {
  157. bb_show_usage();
  158. }
  159. /* Open input file */
  160. if (LONE_DASH(src_fn)) {
  161. src_fd = STDIN_FILENO;
  162. /* Cannot use prompt mode since zip data is arriving on STDIN */
  163. overwrite = (overwrite == o_prompt) ? o_never : overwrite;
  164. } else {
  165. static const char *const extn[] = {"", ".zip", ".ZIP"};
  166. int orig_src_fn_len = strlen(src_fn);
  167. for (i = 0; (i < 3) && (src_fd == -1); i++) {
  168. strcpy(src_fn + orig_src_fn_len, extn[i]);
  169. src_fd = open(src_fn, O_RDONLY);
  170. }
  171. if (src_fd == -1) {
  172. src_fn[orig_src_fn_len] = '\0';
  173. bb_error_msg_and_die("cannot open %s, %s.zip, %s.ZIP", src_fn, src_fn, src_fn);
  174. }
  175. }
  176. /* Change dir if necessary */
  177. if (base_dir)
  178. xchdir(base_dir);
  179. if (verbosity != v_silent)
  180. printf("Archive: %s\n", src_fn);
  181. failed = 0;
  182. while (1) {
  183. unsigned int magic;
  184. /* Check magic number */
  185. xread(src_fd, &magic, 4);
  186. if (magic == ZIP_CDS_MAGIC) {
  187. break;
  188. } else if (magic != ZIP_FILEHEADER_MAGIC) {
  189. bb_error_msg_and_die("invalid zip magic %08X", magic);
  190. }
  191. /* Read the file header */
  192. xread(src_fd, zip_header.raw, 26);
  193. zip_header.formatted.version = SWAP_LE32(zip_header.formatted.version);
  194. zip_header.formatted.flags = SWAP_LE32(zip_header.formatted.flags);
  195. zip_header.formatted.method = SWAP_LE32(zip_header.formatted.method);
  196. zip_header.formatted.modtime = SWAP_LE32(zip_header.formatted.modtime);
  197. zip_header.formatted.moddate = SWAP_LE32(zip_header.formatted.moddate);
  198. zip_header.formatted.crc32 = SWAP_LE32(zip_header.formatted.crc32);
  199. zip_header.formatted.cmpsize = SWAP_LE32(zip_header.formatted.cmpsize);
  200. zip_header.formatted.ucmpsize = SWAP_LE32(zip_header.formatted.ucmpsize);
  201. zip_header.formatted.filename_len = SWAP_LE32(zip_header.formatted.filename_len);
  202. zip_header.formatted.extra_len = SWAP_LE32(zip_header.formatted.extra_len);
  203. if ((zip_header.formatted.method != 0) && (zip_header.formatted.method != 8)) {
  204. bb_error_msg_and_die("unsupported compression method %d", zip_header.formatted.method);
  205. }
  206. /* Read filename */
  207. free(dst_fn);
  208. dst_fn = xzalloc(zip_header.formatted.filename_len + 1);
  209. xread(src_fd, dst_fn, zip_header.formatted.filename_len);
  210. /* Skip extra header bytes */
  211. unzip_skip(src_fd, zip_header.formatted.extra_len);
  212. if ((verbosity == v_list) && !list_header_done){
  213. puts(" Length Date Time Name\n"
  214. " -------- ---- ---- ----");
  215. list_header_done = 1;
  216. }
  217. /* Filter zip entries */
  218. if (find_list_entry(zreject, dst_fn) ||
  219. (zaccept && !find_list_entry(zaccept, dst_fn))) { /* Skip entry */
  220. i = 'n';
  221. } else { /* Extract entry */
  222. total_size += zip_header.formatted.ucmpsize;
  223. if (verbosity == v_list) { /* List entry */
  224. unsigned int dostime = zip_header.formatted.modtime | (zip_header.formatted.moddate << 16);
  225. printf("%9u %02u-%02u-%02u %02u:%02u %s\n",
  226. zip_header.formatted.ucmpsize,
  227. (dostime & 0x01e00000) >> 21,
  228. (dostime & 0x001f0000) >> 16,
  229. (((dostime & 0xfe000000) >> 25) + 1980) % 100,
  230. (dostime & 0x0000f800) >> 11,
  231. (dostime & 0x000007e0) >> 5,
  232. dst_fn);
  233. total_entries++;
  234. i = 'n';
  235. } else if (dst_fd == STDOUT_FILENO) { /* Extracting to STDOUT */
  236. i = -1;
  237. } else if (last_char_is(dst_fn, '/')) { /* Extract directory */
  238. if (stat(dst_fn, &stat_buf) == -1) {
  239. if (errno != ENOENT) {
  240. bb_perror_msg_and_die("cannot stat '%s'",dst_fn);
  241. }
  242. if (verbosity == v_normal) {
  243. printf(" creating: %s\n", dst_fn);
  244. }
  245. unzip_create_leading_dirs(dst_fn);
  246. if (bb_make_directory(dst_fn, 0777, 0)) {
  247. bb_error_msg_and_die("exiting");
  248. }
  249. } else {
  250. if (!S_ISDIR(stat_buf.st_mode)) {
  251. bb_error_msg_and_die("'%s' exists but is not directory", dst_fn);
  252. }
  253. }
  254. i = 'n';
  255. } else { /* Extract file */
  256. _check_file:
  257. if (stat(dst_fn, &stat_buf) == -1) { /* File does not exist */
  258. if (errno != ENOENT) {
  259. bb_perror_msg_and_die("cannot stat '%s'",dst_fn);
  260. }
  261. i = 'y';
  262. } else { /* File already exists */
  263. if (overwrite == o_never) {
  264. i = 'n';
  265. } else if (S_ISREG(stat_buf.st_mode)) { /* File is regular file */
  266. if (overwrite == o_always) {
  267. i = 'y';
  268. } else {
  269. printf("replace %s? [y]es, [n]o, [A]ll, [N]one, [r]ename: ", dst_fn);
  270. if (!fgets(key_buf, 512, stdin)) {
  271. bb_perror_msg_and_die("cannot read input");
  272. }
  273. i = key_buf[0];
  274. }
  275. } else { /* File is not regular file */
  276. bb_error_msg_and_die("'%s' exists but is not regular file",dst_fn);
  277. }
  278. }
  279. }
  280. }
  281. switch (i) {
  282. case 'A':
  283. overwrite = o_always;
  284. case 'y': /* Open file and fall into unzip */
  285. unzip_create_leading_dirs(dst_fn);
  286. dst_fd = xopen(dst_fn, O_WRONLY | O_CREAT | O_TRUNC);
  287. case -1: /* Unzip */
  288. if (verbosity == v_normal) {
  289. printf(" inflating: %s\n", dst_fn);
  290. }
  291. if (unzip_extract(&zip_header, src_fd, dst_fd)) {
  292. failed = 1;
  293. }
  294. if (dst_fd != STDOUT_FILENO) {
  295. /* closing STDOUT is potentially bad for future business */
  296. close(dst_fd);
  297. }
  298. break;
  299. case 'N':
  300. overwrite = o_never;
  301. case 'n':
  302. /* Skip entry data */
  303. unzip_skip(src_fd, zip_header.formatted.cmpsize);
  304. break;
  305. case 'r':
  306. /* Prompt for new name */
  307. printf("new name: ");
  308. if (!fgets(key_buf, 512, stdin)) {
  309. bb_perror_msg_and_die("cannot read input");
  310. }
  311. free(dst_fn);
  312. dst_fn = xstrdup(key_buf);
  313. chomp(dst_fn);
  314. goto _check_file;
  315. default:
  316. printf("error: invalid response [%c]\n",(char)i);
  317. goto _check_file;
  318. }
  319. /* Data descriptor section */
  320. if (zip_header.formatted.flags & 4) {
  321. /* skip over duplicate crc, compressed size and uncompressed size */
  322. unzip_skip(src_fd, 12);
  323. }
  324. }
  325. if (verbosity == v_list) {
  326. printf(" -------- -------\n"
  327. "%9d %d files\n", total_size, total_entries);
  328. }
  329. return failed;
  330. }