blob: 1d3291ab8e3a025380ad57613ad7ec830dc84292 [file] [log] [blame]
Glenn L McGrath87ac7022002-01-02 13:52:26 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Mini unzip implementation for busybox
4 *
Paul Fox0840b762005-07-20 20:26:49 +00005 * 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.
Glenn L McGrath87ac7022002-01-02 13:52:26 +00009 *
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +000010 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Glenn L McGrath87ac7022002-01-02 13:52:26 +000011 */
12
Glenn L McGrathf34b0e92004-06-06 10:22:43 +000013/* For reference see
Paul Fox0840b762005-07-20 20:26:49 +000014 * http://www.pkware.com/company/standards/appnote/
Glenn L McGrathf34b0e92004-06-06 10:22:43 +000015 * http://www.info-zip.org/pub/infozip/doc/appnote-iz-latest.zip
16 */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000017
Paul Fox0840b762005-07-20 20:26:49 +000018/* TODO
Paul Fox0840b762005-07-20 20:26:49 +000019 * Zip64 + other methods
Paul Fox0840b762005-07-20 20:26:49 +000020 */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000021
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000022#include "libbb.h"
Rob Landleyd921b2e2006-08-03 15:41:12 +000023#include "unarchive.h"
Glenn L McGrath87ac7022002-01-02 13:52:26 +000024
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +000025enum {
26#if BB_BIG_ENDIAN
27 ZIP_FILEHEADER_MAGIC = 0x504b0304,
Denys Vlasenko4e8ff732010-05-24 04:33:02 +020028 ZIP_CDF_MAGIC = 0x504b0102, /* central directory's file header */
29 ZIP_CDE_MAGIC = 0x504b0506, /* "end of central directory" record */
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +000030 ZIP_DD_MAGIC = 0x504b0708,
31#else
32 ZIP_FILEHEADER_MAGIC = 0x04034b50,
Denys Vlasenko4e8ff732010-05-24 04:33:02 +020033 ZIP_CDF_MAGIC = 0x02014b50,
Denis Vlasenko006e8622008-09-21 01:01:46 +000034 ZIP_CDE_MAGIC = 0x06054b50,
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +000035 ZIP_DD_MAGIC = 0x08074b50,
36#endif
37};
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000038
Paul Foxcb981632007-11-05 23:09:03 +000039#define ZIP_HEADER_LEN 26
40
Paul Fox0840b762005-07-20 20:26:49 +000041typedef union {
Paul Foxcb981632007-11-05 23:09:03 +000042 uint8_t raw[ZIP_HEADER_LEN];
Paul Fox0840b762005-07-20 20:26:49 +000043 struct {
Denis Vlasenko006e8622008-09-21 01:01:46 +000044 uint16_t version; /* 0-1 */
Denys Vlasenkoe98884b2010-05-24 04:46:18 +020045 uint16_t zip_flags; /* 2-3 */
Denis Vlasenko006e8622008-09-21 01:01:46 +000046 uint16_t method; /* 4-5 */
47 uint16_t modtime; /* 6-7 */
48 uint16_t moddate; /* 8-9 */
49 uint32_t crc32 PACKED; /* 10-13 */
50 uint32_t cmpsize PACKED; /* 14-17 */
51 uint32_t ucmpsize PACKED; /* 18-21 */
52 uint16_t filename_len; /* 22-23 */
53 uint16_t extra_len; /* 24-25 */
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000054 } formatted PACKED;
55} zip_header_t; /* PACKED - gcc 4.2.1 doesn't like it (spews warning) */
Paul Fox0840b762005-07-20 20:26:49 +000056
Paul Foxcb981632007-11-05 23:09:03 +000057/* Check the offset of the last element, not the length. This leniency
58 * allows for poor packing, whereby the overall struct may be too long,
59 * even though the elements are all in the right place.
60 */
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +000061struct BUG_zip_header_must_be_26_bytes {
Paul Foxcb981632007-11-05 23:09:03 +000062 char BUG_zip_header_must_be_26_bytes[
Denis Vlasenko006e8622008-09-21 01:01:46 +000063 offsetof(zip_header_t, formatted.extra_len) + 2
64 == ZIP_HEADER_LEN ? 1 : -1];
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +000065};
66
Denis Vlasenko006e8622008-09-21 01:01:46 +000067#define FIX_ENDIANNESS_ZIP(zip_header) do { \
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +000068 (zip_header).formatted.version = SWAP_LE16((zip_header).formatted.version ); \
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +000069 (zip_header).formatted.method = SWAP_LE16((zip_header).formatted.method ); \
70 (zip_header).formatted.modtime = SWAP_LE16((zip_header).formatted.modtime ); \
71 (zip_header).formatted.moddate = SWAP_LE16((zip_header).formatted.moddate ); \
72 (zip_header).formatted.crc32 = SWAP_LE32((zip_header).formatted.crc32 ); \
73 (zip_header).formatted.cmpsize = SWAP_LE32((zip_header).formatted.cmpsize ); \
74 (zip_header).formatted.ucmpsize = SWAP_LE32((zip_header).formatted.ucmpsize ); \
75 (zip_header).formatted.filename_len = SWAP_LE16((zip_header).formatted.filename_len); \
76 (zip_header).formatted.extra_len = SWAP_LE16((zip_header).formatted.extra_len ); \
77} while (0)
78
Denys Vlasenko4e8ff732010-05-24 04:33:02 +020079#define CDF_HEADER_LEN 42
Denis Vlasenko006e8622008-09-21 01:01:46 +000080
81typedef union {
Denys Vlasenko4e8ff732010-05-24 04:33:02 +020082 uint8_t raw[CDF_HEADER_LEN];
Denis Vlasenko006e8622008-09-21 01:01:46 +000083 struct {
84 /* uint32_t signature; 50 4b 01 02 */
85 uint16_t version_made_by; /* 0-1 */
86 uint16_t version_needed; /* 2-3 */
Denys Vlasenko4e8ff732010-05-24 04:33:02 +020087 uint16_t cdf_flags; /* 4-5 */
Denis Vlasenko006e8622008-09-21 01:01:46 +000088 uint16_t method; /* 6-7 */
89 uint16_t mtime; /* 8-9 */
90 uint16_t mdate; /* 10-11 */
91 uint32_t crc32; /* 12-15 */
92 uint32_t cmpsize; /* 16-19 */
93 uint32_t ucmpsize; /* 20-23 */
94 uint16_t file_name_length; /* 24-25 */
95 uint16_t extra_field_length; /* 26-27 */
96 uint16_t file_comment_length; /* 28-29 */
97 uint16_t disk_number_start; /* 30-31 */
98 uint16_t internal_file_attributes; /* 32-33 */
99 uint32_t external_file_attributes PACKED; /* 34-37 */
100 uint32_t relative_offset_of_local_header PACKED; /* 38-41 */
101 } formatted PACKED;
Denys Vlasenko4e8ff732010-05-24 04:33:02 +0200102} cdf_header_t;
Denis Vlasenko006e8622008-09-21 01:01:46 +0000103
Denys Vlasenko4e8ff732010-05-24 04:33:02 +0200104struct 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];
Denis Vlasenko006e8622008-09-21 01:01:46 +0000108};
109
Denys Vlasenko4e8ff732010-05-24 04:33:02 +0200110#define FIX_ENDIANNESS_CDF(cdf_header) do { \
111 (cdf_header).formatted.crc32 = SWAP_LE32((cdf_header).formatted.crc32 ); \
112 (cdf_header).formatted.cmpsize = SWAP_LE32((cdf_header).formatted.cmpsize ); \
113 (cdf_header).formatted.ucmpsize = SWAP_LE32((cdf_header).formatted.ucmpsize ); \
114 (cdf_header).formatted.file_name_length = SWAP_LE16((cdf_header).formatted.file_name_length); \
115 (cdf_header).formatted.extra_field_length = SWAP_LE16((cdf_header).formatted.extra_field_length); \
116 (cdf_header).formatted.file_comment_length = SWAP_LE16((cdf_header).formatted.file_comment_length); \
117 IF_DESKTOP( \
118 (cdf_header).formatted.version_made_by = SWAP_LE16((cdf_header).formatted.version_made_by); \
119 (cdf_header).formatted.external_file_attributes = SWAP_LE32((cdf_header).formatted.external_file_attributes); \
120 ) \
Denis Vlasenko006e8622008-09-21 01:01:46 +0000121} while (0)
122
123#define CDE_HEADER_LEN 16
124
125typedef union {
126 uint8_t raw[CDE_HEADER_LEN];
127 struct {
128 /* uint32_t signature; 50 4b 05 06 */
129 uint16_t this_disk_no;
Denys Vlasenko4e8ff732010-05-24 04:33:02 +0200130 uint16_t disk_with_cdf_no;
131 uint16_t cdf_entries_on_this_disk;
132 uint16_t cdf_entries_total;
133 uint32_t cdf_size;
134 uint32_t cdf_offset;
Denis Vlasenko006e8622008-09-21 01:01:46 +0000135 /* uint16_t file_comment_length; */
136 /* .ZIP file comment (variable size) */
137 } formatted PACKED;
138} cde_header_t;
139
140struct BUG_cde_header_must_be_16_bytes {
141 char BUG_cde_header_must_be_16_bytes[
142 sizeof(cde_header_t) == CDE_HEADER_LEN ? 1 : -1];
143};
144
145#define FIX_ENDIANNESS_CDE(cde_header) do { \
Denys Vlasenko4e8ff732010-05-24 04:33:02 +0200146 (cde_header).formatted.cdf_offset = SWAP_LE32((cde_header).formatted.cdf_offset); \
Denis Vlasenko006e8622008-09-21 01:01:46 +0000147} while (0)
148
149enum { zip_fd = 3 };
150
151
152#if ENABLE_DESKTOP
153/* NB: does not preserve file position! */
Denys Vlasenko4e8ff732010-05-24 04:33:02 +0200154static uint32_t find_cdf_offset(void)
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000155{
Denis Vlasenko006e8622008-09-21 01:01:46 +0000156 unsigned char buf[1024];
157 cde_header_t cde_header;
158 unsigned char *p;
159 off_t end;
160
161 end = xlseek(zip_fd, 0, SEEK_END);
Denis Vlasenko006e8622008-09-21 01:01:46 +0000162 end -= 1024;
Denys Vlasenkofc2bb8f2010-05-24 13:07:55 +0200163 if (end < 0)
164 end = 0;
Denis Vlasenko006e8622008-09-21 01:01:46 +0000165 xlseek(zip_fd, end, SEEK_SET);
166 full_read(zip_fd, buf, 1024);
167
168 p = buf;
169 while (p <= buf + 1024 - CDE_HEADER_LEN - 4) {
170 if (*p != 'P') {
171 p++;
172 continue;
173 }
174 if (*++p != 'K')
175 continue;
176 if (*++p != 5)
177 continue;
178 if (*++p != 6)
179 continue;
180 /* we found CDE! */
181 memcpy(cde_header.raw, p + 1, CDE_HEADER_LEN);
182 FIX_ENDIANNESS_CDE(cde_header);
Denys Vlasenko4e8ff732010-05-24 04:33:02 +0200183 return cde_header.formatted.cdf_offset;
Denis Vlasenko006e8622008-09-21 01:01:46 +0000184 }
185 bb_error_msg_and_die("can't find file table");
186};
187
Denys Vlasenko4e8ff732010-05-24 04:33:02 +0200188static uint32_t read_next_cdf(uint32_t cdf_offset, cdf_header_t *cdf_ptr)
Denis Vlasenko006e8622008-09-21 01:01:46 +0000189{
190 off_t org;
191
192 org = xlseek(zip_fd, 0, SEEK_CUR);
193
Denys Vlasenko4e8ff732010-05-24 04:33:02 +0200194 if (!cdf_offset)
195 cdf_offset = find_cdf_offset();
Denis Vlasenko006e8622008-09-21 01:01:46 +0000196
Denys Vlasenko4e8ff732010-05-24 04:33:02 +0200197 xlseek(zip_fd, cdf_offset + 4, SEEK_SET);
198 xread(zip_fd, cdf_ptr->raw, CDF_HEADER_LEN);
199 FIX_ENDIANNESS_CDF(*cdf_ptr);
200 cdf_offset += 4 + CDF_HEADER_LEN
201 + cdf_ptr->formatted.file_name_length
202 + cdf_ptr->formatted.extra_field_length
203 + cdf_ptr->formatted.file_comment_length;
Denis Vlasenko006e8622008-09-21 01:01:46 +0000204
205 xlseek(zip_fd, org, SEEK_SET);
Denys Vlasenko4e8ff732010-05-24 04:33:02 +0200206 return cdf_offset;
Denis Vlasenko006e8622008-09-21 01:01:46 +0000207};
208#endif
209
210static void unzip_skip(off_t skip)
211{
Stefani Seiboldf3b56b42009-07-25 02:02:22 +0200212 if (lseek(zip_fd, skip, SEEK_CUR) == (off_t)-1)
213 bb_copyfd_exact_size(zip_fd, -1, skip);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000214}
215
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000216static void unzip_create_leading_dirs(const char *fn)
Paul Fox0840b762005-07-20 20:26:49 +0000217{
218 /* Create all leading directories */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000219 char *name = xstrdup(fn);
Paul Fox0840b762005-07-20 20:26:49 +0000220 if (bb_make_directory(dirname(name), 0777, FILEUTILS_RECUR)) {
Denis Vlasenko13858992006-10-08 12:49:22 +0000221 bb_error_msg_and_die("exiting"); /* bb_make_directory is noisy */
Paul Fox0840b762005-07-20 20:26:49 +0000222 }
223 free(name);
224}
225
Denis Vlasenko006e8622008-09-21 01:01:46 +0000226static void unzip_extract(zip_header_t *zip_header, int dst_fd)
Paul Fox0840b762005-07-20 20:26:49 +0000227{
"Robert P. J. Day"eea56182006-07-20 19:02:24 +0000228 if (zip_header->formatted.method == 0) {
Paul Fox0840b762005-07-20 20:26:49 +0000229 /* Method 0 - stored (not compressed) */
Denis Vlasenko7039a662006-10-08 17:54:47 +0000230 off_t size = zip_header->formatted.ucmpsize;
Denis Vlasenko714701c2006-12-22 00:21:07 +0000231 if (size)
Denis Vlasenko006e8622008-09-21 01:01:46 +0000232 bb_copyfd_exact_size(zip_fd, dst_fd, size);
Paul Fox0840b762005-07-20 20:26:49 +0000233 } else {
234 /* Method 8 - inflate */
Denis Vlasenkocd42cb82007-01-05 23:56:53 +0000235 inflate_unzip_result res;
Denis Vlasenko006e8622008-09-21 01:01:46 +0000236 if (inflate_unzip(&res, zip_header->formatted.cmpsize, zip_fd, dst_fd) < 0)
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000237 bb_error_msg_and_die("inflate error");
Paul Fox0840b762005-07-20 20:26:49 +0000238 /* Validate decompression - crc */
Denis Vlasenkocd42cb82007-01-05 23:56:53 +0000239 if (zip_header->formatted.crc32 != (res.crc ^ 0xffffffffL)) {
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000240 bb_error_msg_and_die("crc error");
Paul Fox0840b762005-07-20 20:26:49 +0000241 }
242 /* Validate decompression - size */
Denis Vlasenkocd42cb82007-01-05 23:56:53 +0000243 if (zip_header->formatted.ucmpsize != res.bytes_out) {
Denis Vlasenkofcc56962007-10-19 21:03:09 +0000244 /* Don't die. Who knows, maybe len calculation
245 * was botched somewhere. After all, crc matched! */
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000246 bb_error_msg("bad length");
Paul Fox0840b762005-07-20 20:26:49 +0000247 }
248 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000249}
250
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000251int unzip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landleydfba7412006-03-06 20:47:33 +0000252int unzip_main(int argc, char **argv)
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000253{
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000254 enum { O_PROMPT, O_NEVER, O_ALWAYS };
255
Paul Fox0840b762005-07-20 20:26:49 +0000256 zip_header_t zip_header;
Denys Vlasenko386bc9f2009-09-06 16:52:50 +0200257 smallint quiet = 0;
258 IF_NOT_DESKTOP(const) smallint verbose = 0;
Paul Fox9382b382007-09-07 20:28:25 +0000259 smallint listing = 0;
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000260 smallint overwrite = O_PROMPT;
Denis Vlasenko006e8622008-09-21 01:01:46 +0000261#if ENABLE_DESKTOP
Denys Vlasenko4e8ff732010-05-24 04:33:02 +0200262 uint32_t cdf_offset;
Denis Vlasenko006e8622008-09-21 01:01:46 +0000263#endif
Denys Vlasenko386bc9f2009-09-06 16:52:50 +0200264 unsigned long total_usize;
265 unsigned long total_size;
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000266 unsigned total_entries;
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000267 int dst_fd = -1;
268 char *src_fn = NULL;
269 char *dst_fn = NULL;
Mike Frysinger69024552005-07-30 07:30:26 +0000270 llist_t *zaccept = NULL;
271 llist_t *zreject = NULL;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000272 char *base_dir = NULL;
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000273 int i, opt;
274 int opt_range = 0;
275 char key_buf[80];
Paul Fox0840b762005-07-20 20:26:49 +0000276 struct stat stat_buf;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000277
Denys Vlasenko386bc9f2009-09-06 16:52:50 +0200278/* -q, -l and -v: UnZip 5.52 of 28 February 2005, by Info-ZIP:
279 *
280 * # /usr/bin/unzip -qq -v decompress_unlzma.i.zip
281 * 204372 Defl:N 35278 83% 09-06-09 14:23 0d056252 decompress_unlzma.i
282 * # /usr/bin/unzip -q -v decompress_unlzma.i.zip
283 * Length Method Size Ratio Date Time CRC-32 Name
284 * -------- ------ ------- ----- ---- ---- ------ ----
285 * 204372 Defl:N 35278 83% 09-06-09 14:23 0d056252 decompress_unlzma.i
286 * -------- ------- --- -------
287 * 204372 35278 83% 1 file
288 * # /usr/bin/unzip -v decompress_unlzma.i.zip
289 * Archive: decompress_unlzma.i.zip
290 * Length Method Size Ratio Date Time CRC-32 Name
291 * -------- ------ ------- ----- ---- ---- ------ ----
292 * 204372 Defl:N 35278 83% 09-06-09 14:23 0d056252 decompress_unlzma.i
293 * -------- ------- --- -------
294 * 204372 35278 83% 1 file
295 * # unzip -v decompress_unlzma.i.zip
296 * Archive: decompress_unlzma.i.zip
297 * Length Date Time Name
298 * -------- ---- ---- ----
299 * 204372 09-06-09 14:23 decompress_unlzma.i
300 * -------- -------
301 * 204372 1 files
302 * # /usr/bin/unzip -l -qq decompress_unlzma.i.zip
303 * 204372 09-06-09 14:23 decompress_unlzma.i
304 * # /usr/bin/unzip -l -q decompress_unlzma.i.zip
305 * Length Date Time Name
306 * -------- ---- ---- ----
307 * 204372 09-06-09 14:23 decompress_unlzma.i
308 * -------- -------
309 * 204372 1 file
310 * # /usr/bin/unzip -l decompress_unlzma.i.zip
311 * Archive: decompress_unlzma.i.zip
312 * Length Date Time Name
313 * -------- ---- ---- ----
314 * 204372 09-06-09 14:23 decompress_unlzma.i
315 * -------- -------
316 * 204372 1 file
317 */
318
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000319 /* '-' makes getopt return 1 for non-options */
Denys Vlasenko386bc9f2009-09-06 16:52:50 +0200320 while ((opt = getopt(argc, argv, "-d:lnopqxv")) != -1) {
Denis Vlasenkoc16bd212006-09-27 19:51:06 +0000321 switch (opt_range) {
Paul Fox0840b762005-07-20 20:26:49 +0000322 case 0: /* Options */
Denis Vlasenkoc16bd212006-09-27 19:51:06 +0000323 switch (opt) {
Paul Fox0840b762005-07-20 20:26:49 +0000324 case 'l': /* List */
Paul Fox9382b382007-09-07 20:28:25 +0000325 listing = 1;
Paul Fox0840b762005-07-20 20:26:49 +0000326 break;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000327
Paul Fox0840b762005-07-20 20:26:49 +0000328 case 'n': /* Never overwrite existing files */
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000329 overwrite = O_NEVER;
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000330 break;
Paul Fox0840b762005-07-20 20:26:49 +0000331
332 case 'o': /* Always overwrite existing files */
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000333 overwrite = O_ALWAYS;
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000334 break;
Paul Fox0840b762005-07-20 20:26:49 +0000335
336 case 'p': /* Extract files to stdout and fall through to set verbosity */
337 dst_fd = STDOUT_FILENO;
338
339 case 'q': /* Be quiet */
Denys Vlasenko386bc9f2009-09-06 16:52:50 +0200340 quiet++;
341 break;
342
343 case 'v': /* Verbose list */
344 IF_DESKTOP(verbose++;)
345 listing = 1;
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000346 break;
Paul Fox0840b762005-07-20 20:26:49 +0000347
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000348 case 1: /* The zip file */
349 /* +5: space for ".zip" and NUL */
350 src_fn = xmalloc(strlen(optarg) + 5);
Denis Vlasenko666c40c2007-03-31 10:17:24 +0000351 strcpy(src_fn, optarg);
Paul Fox0840b762005-07-20 20:26:49 +0000352 opt_range++;
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000353 break;
Paul Fox0840b762005-07-20 20:26:49 +0000354
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000355 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000356 bb_show_usage();
Paul Fox0840b762005-07-20 20:26:49 +0000357
358 }
359 break;
360
361 case 1: /* Include files */
362 if (opt == 1) {
Rob Landley8bb50782006-05-26 23:44:51 +0000363 llist_add_to(&zaccept, optarg);
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000364 break;
365 }
366 if (opt == 'd') {
Paul Fox0840b762005-07-20 20:26:49 +0000367 base_dir = optarg;
368 opt_range += 2;
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000369 break;
Paul Fox0840b762005-07-20 20:26:49 +0000370 }
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000371 if (opt == 'x') {
372 opt_range++;
373 break;
374 }
375 bb_show_usage();
Paul Fox0840b762005-07-20 20:26:49 +0000376
377 case 2 : /* Exclude files */
378 if (opt == 1) {
Rob Landley8bb50782006-05-26 23:44:51 +0000379 llist_add_to(&zreject, optarg);
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000380 break;
381 }
382 if (opt == 'd') { /* Extract to base directory */
Paul Fox0840b762005-07-20 20:26:49 +0000383 base_dir = optarg;
384 opt_range++;
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000385 break;
Paul Fox0840b762005-07-20 20:26:49 +0000386 }
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000387 /* fall through */
Paul Fox0840b762005-07-20 20:26:49 +0000388
389 default:
390 bb_show_usage();
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000391 }
392 }
393
Paul Fox0840b762005-07-20 20:26:49 +0000394 if (src_fn == NULL) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000395 bb_show_usage();
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000396 }
397
Paul Fox0840b762005-07-20 20:26:49 +0000398 /* Open input file */
Denis Vlasenko9f739442006-12-16 23:49:13 +0000399 if (LONE_DASH(src_fn)) {
Denis Vlasenko006e8622008-09-21 01:01:46 +0000400 xdup2(STDIN_FILENO, zip_fd);
Paul Fox0840b762005-07-20 20:26:49 +0000401 /* Cannot use prompt mode since zip data is arriving on STDIN */
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000402 if (overwrite == O_PROMPT)
403 overwrite = O_NEVER;
Glenn L McGrath237ae422002-11-03 14:05:15 +0000404 } else {
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000405 static const char extn[][5] = {"", ".zip", ".ZIP"};
Paul Fox0840b762005-07-20 20:26:49 +0000406 int orig_src_fn_len = strlen(src_fn);
Denis Vlasenko006e8622008-09-21 01:01:46 +0000407 int src_fd = -1;
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000408
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000409 for (i = 0; (i < 3) && (src_fd == -1); i++) {
Paul Fox0840b762005-07-20 20:26:49 +0000410 strcpy(src_fn + orig_src_fn_len, extn[i]);
411 src_fd = open(src_fn, O_RDONLY);
412 }
413 if (src_fd == -1) {
Denis Vlasenko666c40c2007-03-31 10:17:24 +0000414 src_fn[orig_src_fn_len] = '\0';
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000415 bb_error_msg_and_die("can't open %s, %s.zip, %s.ZIP", src_fn, src_fn, src_fn);
Paul Fox0840b762005-07-20 20:26:49 +0000416 }
Denis Vlasenko006e8622008-09-21 01:01:46 +0000417 xmove_fd(src_fd, zip_fd);
Glenn L McGrath237ae422002-11-03 14:05:15 +0000418 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000419
Paul Fox0840b762005-07-20 20:26:49 +0000420 /* Change dir if necessary */
Bernhard Reutner-Fischerd9cf7ac2006-04-12 18:39:58 +0000421 if (base_dir)
Rob Landleyd921b2e2006-08-03 15:41:12 +0000422 xchdir(base_dir);
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000423
Denys Vlasenko386bc9f2009-09-06 16:52:50 +0200424 if (quiet <= 1) { /* not -qq */
425 if (quiet == 0)
426 printf("Archive: %s\n", src_fn);
427 if (listing) {
428 puts(verbose ?
429 " Length Method Size Ratio Date Time CRC-32 Name\n"
430 "-------- ------ ------- ----- ---- ---- ------ ----"
431 :
432 " Length Date Time Name\n"
433 " -------- ---- ---- ----"
434 );
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000435 }
436 }
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000437
Denys Vlasenko4e8ff732010-05-24 04:33:02 +0200438/* Example of an archive with one 0-byte long file named 'z'
439 * created by Zip 2.31 on Unix:
440 * 0000 [50 4b]03 04 0a 00 00 00 00 00 42 1a b8 3c 00 00 |PK........B..<..|
441 * sig........ vneed flags compr mtime mdate crc32>
442 * 0010 00 00 00 00 00 00 00 00 00 00 01 00 15 00 7a 55 |..............zU|
443 * >..... csize...... usize...... fnlen exlen fn ex>
444 * 0020 54 09 00 03 cc d3 f9 4b cc d3 f9 4b 55 78 04 00 |T......K...KUx..|
445 * >tra_field......................................
446 * 0030 00 00 00 00[50 4b]01 02 17 03 0a 00 00 00 00 00 |....PK..........|
447 * ........... sig........ vmade vneed flags compr
448 * 0040 42 1a b8 3c 00 00 00 00 00 00 00 00 00 00 00 00 |B..<............|
449 * mtime mdate crc32...... csize...... usize......
450 * 0050 01 00 0d 00 00 00 00 00 00 00 00 00 a4 81 00 00 |................|
451 * fnlen exlen clen. dnum. iattr eattr...... relofs> (eattr = rw-r--r--)
452 * 0060 00 00 7a 55 54 05 00 03 cc d3 f9 4b 55 78 00 00 |..zUT......KUx..|
453 * >..... fn extra_field...........................
454 * 0070 [50 4b]05 06 00 00 00 00 01 00 01 00 3c 00 00 00 |PK..........<...|
455 * 0080 34 00 00 00 00 00 |4.....|
456 */
Denys Vlasenko386bc9f2009-09-06 16:52:50 +0200457 total_usize = 0;
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000458 total_size = 0;
459 total_entries = 0;
Denis Vlasenko006e8622008-09-21 01:01:46 +0000460#if ENABLE_DESKTOP
Denys Vlasenko4e8ff732010-05-24 04:33:02 +0200461 cdf_offset = 0;
Denis Vlasenko006e8622008-09-21 01:01:46 +0000462#endif
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000463 while (1) {
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000464 uint32_t magic;
Denys Vlasenko4e8ff732010-05-24 04:33:02 +0200465 mode_t dir_mode = 0777;
466#if ENABLE_DESKTOP
467 mode_t file_mode = 0666;
468#endif
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000469
Paul Fox0840b762005-07-20 20:26:49 +0000470 /* Check magic number */
Denis Vlasenko006e8622008-09-21 01:01:46 +0000471 xread(zip_fd, &magic, 4);
Denis Vlasenko48a99712008-07-26 17:32:41 +0000472 /* Central directory? It's at the end, so exit */
Denys Vlasenko4e8ff732010-05-24 04:33:02 +0200473 if (magic == ZIP_CDF_MAGIC)
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000474 break;
Denis Vlasenko006e8622008-09-21 01:01:46 +0000475#if ENABLE_DESKTOP
476 /* Data descriptor? It was a streaming file, go on */
477 if (magic == ZIP_DD_MAGIC) {
478 /* skip over duplicate crc32, cmpsize and ucmpsize */
479 unzip_skip(3 * 4);
480 continue;
481 }
482#endif
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000483 if (magic != ZIP_FILEHEADER_MAGIC)
Denis Vlasenko48a99712008-07-26 17:32:41 +0000484 bb_error_msg_and_die("invalid zip magic %08X", (int)magic);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000485
486 /* Read the file header */
Denis Vlasenko006e8622008-09-21 01:01:46 +0000487 xread(zip_fd, zip_header.raw, ZIP_HEADER_LEN);
488 FIX_ENDIANNESS_ZIP(zip_header);
"Robert P. J. Day"eea56182006-07-20 19:02:24 +0000489 if ((zip_header.formatted.method != 0) && (zip_header.formatted.method != 8)) {
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000490 bb_error_msg_and_die("unsupported method %d", zip_header.formatted.method);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000491 }
Denis Vlasenko006e8622008-09-21 01:01:46 +0000492#if !ENABLE_DESKTOP
Denys Vlasenkoe98884b2010-05-24 04:46:18 +0200493 if (zip_header.formatted.zip_flags & SWAP_LE16(0x0009)) {
Denis Vlasenko006e8622008-09-21 01:01:46 +0000494 bb_error_msg_and_die("zip flags 1 and 8 are not supported");
495 }
496#else
Denys Vlasenkoe98884b2010-05-24 04:46:18 +0200497 if (zip_header.formatted.zip_flags & SWAP_LE16(0x0001)) {
Denis Vlasenko48a99712008-07-26 17:32:41 +0000498 /* 0x0001 - encrypted */
Denis Vlasenko006e8622008-09-21 01:01:46 +0000499 bb_error_msg_and_die("zip flag 1 (encryption) is not supported");
500 }
Denys Vlasenko4e8ff732010-05-24 04:33:02 +0200501
502 {
503 cdf_header_t cdf_header;
504 cdf_offset = read_next_cdf(cdf_offset, &cdf_header);
Denys Vlasenkoe98884b2010-05-24 04:46:18 +0200505 if (zip_header.formatted.zip_flags & SWAP_LE16(0x0008)) {
Denys Vlasenko4e8ff732010-05-24 04:33:02 +0200506 /* 0x0008 - streaming. [u]cmpsize can be reliably gotten
507 * only from Central Directory. See unzip_doc.txt */
508 zip_header.formatted.crc32 = cdf_header.formatted.crc32;
509 zip_header.formatted.cmpsize = cdf_header.formatted.cmpsize;
510 zip_header.formatted.ucmpsize = cdf_header.formatted.ucmpsize;
511 }
512 if ((cdf_header.formatted.version_made_by >> 8) == 3) {
513 /* this archive is created on Unix */
514 dir_mode = file_mode = (cdf_header.formatted.external_file_attributes >> 16);
515 }
Denis Vlasenko48a99712008-07-26 17:32:41 +0000516 }
Denis Vlasenko006e8622008-09-21 01:01:46 +0000517#endif
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000518
519 /* Read filename */
Paul Fox0840b762005-07-20 20:26:49 +0000520 free(dst_fn);
"Robert P. J. Day"eea56182006-07-20 19:02:24 +0000521 dst_fn = xzalloc(zip_header.formatted.filename_len + 1);
Denis Vlasenko006e8622008-09-21 01:01:46 +0000522 xread(zip_fd, dst_fn, zip_header.formatted.filename_len);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000523
Paul Fox0840b762005-07-20 20:26:49 +0000524 /* Skip extra header bytes */
Denis Vlasenko006e8622008-09-21 01:01:46 +0000525 unzip_skip(zip_header.formatted.extra_len);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000526
Paul Fox0840b762005-07-20 20:26:49 +0000527 /* Filter zip entries */
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000528 if (find_list_entry(zreject, dst_fn)
529 || (zaccept && !find_list_entry(zaccept, dst_fn))
530 ) { /* Skip entry */
Paul Fox0840b762005-07-20 20:26:49 +0000531 i = 'n';
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000532
Paul Fox0840b762005-07-20 20:26:49 +0000533 } else { /* Extract entry */
Paul Fox9382b382007-09-07 20:28:25 +0000534 if (listing) { /* List entry */
Denys Vlasenko386bc9f2009-09-06 16:52:50 +0200535 unsigned dostime = zip_header.formatted.modtime | (zip_header.formatted.moddate << 16);
536 if (!verbose) {
537 // " Length Date Time Name\n"
538 // " -------- ---- ---- ----"
539 printf( "%9u %02u-%02u-%02u %02u:%02u %s\n",
540 (unsigned)zip_header.formatted.ucmpsize,
541 (dostime & 0x01e00000) >> 21,
542 (dostime & 0x001f0000) >> 16,
543 (((dostime & 0xfe000000) >> 25) + 1980) % 100,
544 (dostime & 0x0000f800) >> 11,
545 (dostime & 0x000007e0) >> 5,
546 dst_fn);
547 total_usize += zip_header.formatted.ucmpsize;
Denis Vlasenkofeb7ae72007-10-01 12:05:12 +0000548 } else {
Denys Vlasenko386bc9f2009-09-06 16:52:50 +0200549 unsigned long percents = zip_header.formatted.ucmpsize - zip_header.formatted.cmpsize;
550 percents = percents * 100;
551 if (zip_header.formatted.ucmpsize)
552 percents /= zip_header.formatted.ucmpsize;
553 // " Length Method Size Ratio Date Time CRC-32 Name\n"
554 // "-------- ------ ------- ----- ---- ---- ------ ----"
555 printf( "%8u Defl:N" "%9u%4u%% %02u-%02u-%02u %02u:%02u %08x %s\n",
556 (unsigned)zip_header.formatted.ucmpsize,
557 (unsigned)zip_header.formatted.cmpsize,
558 (unsigned)percents,
559 (dostime & 0x01e00000) >> 21,
560 (dostime & 0x001f0000) >> 16,
561 (((dostime & 0xfe000000) >> 25) + 1980) % 100,
562 (dostime & 0x0000f800) >> 11,
563 (dostime & 0x000007e0) >> 5,
564 zip_header.formatted.crc32,
565 dst_fn);
566 total_usize += zip_header.formatted.ucmpsize;
567 total_size += zip_header.formatted.cmpsize;
Denis Vlasenkofeb7ae72007-10-01 12:05:12 +0000568 }
569 i = 'n';
Paul Fox0840b762005-07-20 20:26:49 +0000570 } else if (dst_fd == STDOUT_FILENO) { /* Extracting to STDOUT */
571 i = -1;
Paul Fox0840b762005-07-20 20:26:49 +0000572 } else if (last_char_is(dst_fn, '/')) { /* Extract directory */
573 if (stat(dst_fn, &stat_buf) == -1) {
574 if (errno != ENOENT) {
Denis Vlasenko006e8622008-09-21 01:01:46 +0000575 bb_perror_msg_and_die("can't stat '%s'", dst_fn);
Paul Fox0840b762005-07-20 20:26:49 +0000576 }
Denys Vlasenko386bc9f2009-09-06 16:52:50 +0200577 if (!quiet) {
Paul Fox0840b762005-07-20 20:26:49 +0000578 printf(" creating: %s\n", dst_fn);
579 }
580 unzip_create_leading_dirs(dst_fn);
Denys Vlasenko4e8ff732010-05-24 04:33:02 +0200581 if (bb_make_directory(dst_fn, dir_mode, 0)) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000582 bb_error_msg_and_die("exiting");
Paul Fox0840b762005-07-20 20:26:49 +0000583 }
584 } else {
585 if (!S_ISDIR(stat_buf.st_mode)) {
586 bb_error_msg_and_die("'%s' exists but is not directory", dst_fn);
587 }
588 }
589 i = 'n';
590
591 } else { /* Extract file */
Denis Vlasenko48a99712008-07-26 17:32:41 +0000592 check_file:
Paul Fox0840b762005-07-20 20:26:49 +0000593 if (stat(dst_fn, &stat_buf) == -1) { /* File does not exist */
594 if (errno != ENOENT) {
Denis Vlasenko006e8622008-09-21 01:01:46 +0000595 bb_perror_msg_and_die("can't stat '%s'", dst_fn);
Paul Fox0840b762005-07-20 20:26:49 +0000596 }
597 i = 'y';
Paul Fox0840b762005-07-20 20:26:49 +0000598 } else { /* File already exists */
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000599 if (overwrite == O_NEVER) {
Paul Fox0840b762005-07-20 20:26:49 +0000600 i = 'n';
Paul Fox0840b762005-07-20 20:26:49 +0000601 } else if (S_ISREG(stat_buf.st_mode)) { /* File is regular file */
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000602 if (overwrite == O_ALWAYS) {
Paul Fox0840b762005-07-20 20:26:49 +0000603 i = 'y';
604 } else {
605 printf("replace %s? [y]es, [n]o, [A]ll, [N]one, [r]ename: ", dst_fn);
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000606 if (!fgets(key_buf, sizeof(key_buf), stdin)) {
Denis Vlasenko006e8622008-09-21 01:01:46 +0000607 bb_perror_msg_and_die("can't read input");
Paul Fox0840b762005-07-20 20:26:49 +0000608 }
609 i = key_buf[0];
610 }
Paul Fox0840b762005-07-20 20:26:49 +0000611 } else { /* File is not regular file */
Denis Vlasenkof5d8c902008-06-26 14:32:57 +0000612 bb_error_msg_and_die("'%s' exists but is not regular file", dst_fn);
Paul Fox0840b762005-07-20 20:26:49 +0000613 }
614 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000615 }
616 }
617
Paul Fox0840b762005-07-20 20:26:49 +0000618 switch (i) {
619 case 'A':
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000620 overwrite = O_ALWAYS;
Paul Fox0840b762005-07-20 20:26:49 +0000621 case 'y': /* Open file and fall into unzip */
622 unzip_create_leading_dirs(dst_fn);
Denys Vlasenko4e8ff732010-05-24 04:33:02 +0200623#if ENABLE_DESKTOP
624 dst_fd = xopen3(dst_fn, O_WRONLY | O_CREAT | O_TRUNC, file_mode);
625#else
Bernhard Reutner-Fischer64d7e932006-09-11 16:01:40 +0000626 dst_fd = xopen(dst_fn, O_WRONLY | O_CREAT | O_TRUNC);
Denys Vlasenko4e8ff732010-05-24 04:33:02 +0200627#endif
Paul Fox0840b762005-07-20 20:26:49 +0000628 case -1: /* Unzip */
Denys Vlasenko386bc9f2009-09-06 16:52:50 +0200629 if (!quiet) {
Paul Fox0840b762005-07-20 20:26:49 +0000630 printf(" inflating: %s\n", dst_fn);
631 }
Denis Vlasenko006e8622008-09-21 01:01:46 +0000632 unzip_extract(&zip_header, dst_fd);
Paul Fox0840b762005-07-20 20:26:49 +0000633 if (dst_fd != STDOUT_FILENO) {
634 /* closing STDOUT is potentially bad for future business */
635 close(dst_fd);
636 }
637 break;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000638
Paul Fox0840b762005-07-20 20:26:49 +0000639 case 'N':
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000640 overwrite = O_NEVER;
Paul Fox0840b762005-07-20 20:26:49 +0000641 case 'n':
642 /* Skip entry data */
Denis Vlasenko006e8622008-09-21 01:01:46 +0000643 unzip_skip(zip_header.formatted.cmpsize);
Paul Fox0840b762005-07-20 20:26:49 +0000644 break;
645
646 case 'r':
647 /* Prompt for new name */
648 printf("new name: ");
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000649 if (!fgets(key_buf, sizeof(key_buf), stdin)) {
Denis Vlasenko006e8622008-09-21 01:01:46 +0000650 bb_perror_msg_and_die("can't read input");
Paul Fox0840b762005-07-20 20:26:49 +0000651 }
652 free(dst_fn);
Rob Landleyd921b2e2006-08-03 15:41:12 +0000653 dst_fn = xstrdup(key_buf);
Paul Fox0840b762005-07-20 20:26:49 +0000654 chomp(dst_fn);
Denis Vlasenko48a99712008-07-26 17:32:41 +0000655 goto check_file;
Paul Fox0840b762005-07-20 20:26:49 +0000656
657 default:
Denys Vlasenko386bc9f2009-09-06 16:52:50 +0200658 printf("error: invalid response [%c]\n", (char)i);
Denis Vlasenko48a99712008-07-26 17:32:41 +0000659 goto check_file;
Paul Fox0840b762005-07-20 20:26:49 +0000660 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000661
Denis Vlasenko006e8622008-09-21 01:01:46 +0000662 total_entries++;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000663 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000664
Denys Vlasenko386bc9f2009-09-06 16:52:50 +0200665 if (listing && quiet <= 1) {
666 if (!verbose) {
667 // " Length Date Time Name\n"
668 // " -------- ---- ---- ----"
669 printf( " -------- -------\n"
670 "%9lu" " %u files\n",
671 total_usize, total_entries);
672 } else {
673 unsigned long percents = total_usize - total_size;
674 percents = percents * 100;
675 if (total_usize)
676 percents /= total_usize;
677 // " Length Method Size Ratio Date Time CRC-32 Name\n"
678 // "-------- ------ ------- ----- ---- ---- ------ ----"
679 printf( "-------- ------- --- -------\n"
680 "%8lu" "%17lu%4u%% %u files\n",
681 total_usize, total_size, (unsigned)percents,
682 total_entries);
683 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000684 }
685
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000686 return 0;
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000687}