blob: e468ff451db4dca764600812bcf156ab1f1ccbbf [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,
28 ZIP_CDS_MAGIC = 0x504b0102,
Denis Vlasenko006e8622008-09-21 01:01:46 +000029 ZIP_CDE_MAGIC = 0x504b0506,
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +000030 ZIP_DD_MAGIC = 0x504b0708,
31#else
32 ZIP_FILEHEADER_MAGIC = 0x04034b50,
33 ZIP_CDS_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 */
45 uint16_t flags; /* 2-3 */
46 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 ); \
69 (zip_header).formatted.flags = SWAP_LE16((zip_header).formatted.flags ); \
70 (zip_header).formatted.method = SWAP_LE16((zip_header).formatted.method ); \
71 (zip_header).formatted.modtime = SWAP_LE16((zip_header).formatted.modtime ); \
72 (zip_header).formatted.moddate = SWAP_LE16((zip_header).formatted.moddate ); \
73 (zip_header).formatted.crc32 = SWAP_LE32((zip_header).formatted.crc32 ); \
74 (zip_header).formatted.cmpsize = SWAP_LE32((zip_header).formatted.cmpsize ); \
75 (zip_header).formatted.ucmpsize = SWAP_LE32((zip_header).formatted.ucmpsize ); \
76 (zip_header).formatted.filename_len = SWAP_LE16((zip_header).formatted.filename_len); \
77 (zip_header).formatted.extra_len = SWAP_LE16((zip_header).formatted.extra_len ); \
78} while (0)
79
Denis Vlasenko006e8622008-09-21 01:01:46 +000080#define CDS_HEADER_LEN 42
81
82typedef union {
83 uint8_t raw[CDS_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 cds_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} cds_header_t;
104
105struct BUG_cds_header_must_be_42_bytes {
106 char BUG_cds_header_must_be_42_bytes[
107 offsetof(cds_header_t, formatted.relative_offset_of_local_header) + 4
108 == CDS_HEADER_LEN ? 1 : -1];
109};
110
111#define FIX_ENDIANNESS_CDS(cds_header) do { \
112 (cds_header).formatted.crc32 = SWAP_LE32((cds_header).formatted.crc32 ); \
113 (cds_header).formatted.cmpsize = SWAP_LE32((cds_header).formatted.cmpsize ); \
114 (cds_header).formatted.ucmpsize = SWAP_LE32((cds_header).formatted.ucmpsize ); \
115 (cds_header).formatted.file_name_length = SWAP_LE16((cds_header).formatted.file_name_length); \
116 (cds_header).formatted.extra_field_length = SWAP_LE16((cds_header).formatted.extra_field_length); \
117 (cds_header).formatted.file_comment_length = SWAP_LE16((cds_header).formatted.file_comment_length); \
118} while (0)
119
120#define CDE_HEADER_LEN 16
121
122typedef 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_cds_no;
128 uint16_t cds_entries_on_this_disk;
129 uint16_t cds_entries_total;
130 uint32_t cds_size;
131 uint32_t cds_offset;
132 /* uint16_t file_comment_length; */
133 /* .ZIP file comment (variable size) */
134 } formatted PACKED;
135} cde_header_t;
136
137struct BUG_cde_header_must_be_16_bytes {
138 char BUG_cde_header_must_be_16_bytes[
139 sizeof(cde_header_t) == CDE_HEADER_LEN ? 1 : -1];
140};
141
142#define FIX_ENDIANNESS_CDE(cde_header) do { \
143 (cde_header).formatted.cds_offset = SWAP_LE16((cde_header).formatted.cds_offset); \
144} while (0)
145
146enum { zip_fd = 3 };
147
148
149#if ENABLE_DESKTOP
150/* NB: does not preserve file position! */
151static uint32_t find_cds_offset(void)
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000152{
Denis Vlasenko006e8622008-09-21 01:01:46 +0000153 unsigned char buf[1024];
154 cde_header_t cde_header;
155 unsigned char *p;
156 off_t end;
157
158 end = xlseek(zip_fd, 0, SEEK_END);
159 if (end < 1024)
160 end = 1024;
161 end -= 1024;
162 xlseek(zip_fd, end, SEEK_SET);
163 full_read(zip_fd, buf, 1024);
164
165 p = buf;
166 while (p <= buf + 1024 - CDE_HEADER_LEN - 4) {
167 if (*p != 'P') {
168 p++;
169 continue;
170 }
171 if (*++p != 'K')
172 continue;
173 if (*++p != 5)
174 continue;
175 if (*++p != 6)
176 continue;
177 /* we found CDE! */
178 memcpy(cde_header.raw, p + 1, CDE_HEADER_LEN);
179 FIX_ENDIANNESS_CDE(cde_header);
180 return cde_header.formatted.cds_offset;
181 }
182 bb_error_msg_and_die("can't find file table");
183};
184
185static uint32_t read_next_cds(int count_m1, uint32_t cds_offset, cds_header_t *cds_ptr)
186{
187 off_t org;
188
189 org = xlseek(zip_fd, 0, SEEK_CUR);
190
191 if (!cds_offset)
192 cds_offset = find_cds_offset();
193
194 while (count_m1-- >= 0) {
195 xlseek(zip_fd, cds_offset + 4, SEEK_SET);
196 xread(zip_fd, cds_ptr->raw, CDS_HEADER_LEN);
197 FIX_ENDIANNESS_CDS(*cds_ptr);
198 cds_offset += 4 + CDS_HEADER_LEN
199 + cds_ptr->formatted.file_name_length
200 + cds_ptr->formatted.extra_field_length
201 + cds_ptr->formatted.file_comment_length;
202 }
203
204 xlseek(zip_fd, org, SEEK_SET);
205 return cds_offset;
206};
207#endif
208
209static void unzip_skip(off_t skip)
210{
211 bb_copyfd_exact_size(zip_fd, -1, skip);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000212}
213
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000214static void unzip_create_leading_dirs(const char *fn)
Paul Fox0840b762005-07-20 20:26:49 +0000215{
216 /* Create all leading directories */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000217 char *name = xstrdup(fn);
Paul Fox0840b762005-07-20 20:26:49 +0000218 if (bb_make_directory(dirname(name), 0777, FILEUTILS_RECUR)) {
Denis Vlasenko13858992006-10-08 12:49:22 +0000219 bb_error_msg_and_die("exiting"); /* bb_make_directory is noisy */
Paul Fox0840b762005-07-20 20:26:49 +0000220 }
221 free(name);
222}
223
Denis Vlasenko006e8622008-09-21 01:01:46 +0000224static void unzip_extract(zip_header_t *zip_header, int dst_fd)
Paul Fox0840b762005-07-20 20:26:49 +0000225{
"Robert P. J. Day"eea56182006-07-20 19:02:24 +0000226 if (zip_header->formatted.method == 0) {
Paul Fox0840b762005-07-20 20:26:49 +0000227 /* Method 0 - stored (not compressed) */
Denis Vlasenko7039a662006-10-08 17:54:47 +0000228 off_t size = zip_header->formatted.ucmpsize;
Denis Vlasenko714701c2006-12-22 00:21:07 +0000229 if (size)
Denis Vlasenko006e8622008-09-21 01:01:46 +0000230 bb_copyfd_exact_size(zip_fd, dst_fd, size);
Paul Fox0840b762005-07-20 20:26:49 +0000231 } else {
232 /* Method 8 - inflate */
Denis Vlasenkocd42cb82007-01-05 23:56:53 +0000233 inflate_unzip_result res;
Denis Vlasenko006e8622008-09-21 01:01:46 +0000234 if (inflate_unzip(&res, zip_header->formatted.cmpsize, zip_fd, dst_fd) < 0)
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000235 bb_error_msg_and_die("inflate error");
Paul Fox0840b762005-07-20 20:26:49 +0000236 /* Validate decompression - crc */
Denis Vlasenkocd42cb82007-01-05 23:56:53 +0000237 if (zip_header->formatted.crc32 != (res.crc ^ 0xffffffffL)) {
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000238 bb_error_msg_and_die("crc error");
Paul Fox0840b762005-07-20 20:26:49 +0000239 }
240 /* Validate decompression - size */
Denis Vlasenkocd42cb82007-01-05 23:56:53 +0000241 if (zip_header->formatted.ucmpsize != res.bytes_out) {
Denis Vlasenkofcc56962007-10-19 21:03:09 +0000242 /* Don't die. Who knows, maybe len calculation
243 * was botched somewhere. After all, crc matched! */
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000244 bb_error_msg("bad length");
Paul Fox0840b762005-07-20 20:26:49 +0000245 }
246 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000247}
248
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000249int unzip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landleydfba7412006-03-06 20:47:33 +0000250int unzip_main(int argc, char **argv)
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000251{
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000252 enum { O_PROMPT, O_NEVER, O_ALWAYS };
253
Paul Fox0840b762005-07-20 20:26:49 +0000254 zip_header_t zip_header;
Paul Fox9382b382007-09-07 20:28:25 +0000255 smallint verbose = 1;
256 smallint listing = 0;
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000257 smallint overwrite = O_PROMPT;
Denis Vlasenko006e8622008-09-21 01:01:46 +0000258#if ENABLE_DESKTOP
259 uint32_t cds_offset;
260 unsigned cds_entries;
261#endif
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000262 unsigned total_size;
263 unsigned total_entries;
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000264 int dst_fd = -1;
265 char *src_fn = NULL;
266 char *dst_fn = NULL;
Mike Frysinger69024552005-07-30 07:30:26 +0000267 llist_t *zaccept = NULL;
268 llist_t *zreject = NULL;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000269 char *base_dir = NULL;
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000270 int i, opt;
271 int opt_range = 0;
272 char key_buf[80];
Paul Fox0840b762005-07-20 20:26:49 +0000273 struct stat stat_buf;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000274
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000275 /* '-' makes getopt return 1 for non-options */
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000276 while ((opt = getopt(argc, argv, "-d:lnopqx")) != -1) {
Denis Vlasenkoc16bd212006-09-27 19:51:06 +0000277 switch (opt_range) {
Paul Fox0840b762005-07-20 20:26:49 +0000278 case 0: /* Options */
Denis Vlasenkoc16bd212006-09-27 19:51:06 +0000279 switch (opt) {
Paul Fox0840b762005-07-20 20:26:49 +0000280 case 'l': /* List */
Paul Fox9382b382007-09-07 20:28:25 +0000281 listing = 1;
Paul Fox0840b762005-07-20 20:26:49 +0000282 break;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000283
Paul Fox0840b762005-07-20 20:26:49 +0000284 case 'n': /* Never overwrite existing files */
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000285 overwrite = O_NEVER;
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000286 break;
Paul Fox0840b762005-07-20 20:26:49 +0000287
288 case 'o': /* Always overwrite existing files */
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000289 overwrite = O_ALWAYS;
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000290 break;
Paul Fox0840b762005-07-20 20:26:49 +0000291
292 case 'p': /* Extract files to stdout and fall through to set verbosity */
293 dst_fd = STDOUT_FILENO;
294
295 case 'q': /* Be quiet */
Paul Fox9382b382007-09-07 20:28:25 +0000296 verbose = 0;
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000297 break;
Paul Fox0840b762005-07-20 20:26:49 +0000298
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000299 case 1: /* The zip file */
300 /* +5: space for ".zip" and NUL */
301 src_fn = xmalloc(strlen(optarg) + 5);
Denis Vlasenko666c40c2007-03-31 10:17:24 +0000302 strcpy(src_fn, optarg);
Paul Fox0840b762005-07-20 20:26:49 +0000303 opt_range++;
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000304 break;
Paul Fox0840b762005-07-20 20:26:49 +0000305
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000306 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000307 bb_show_usage();
Paul Fox0840b762005-07-20 20:26:49 +0000308
309 }
310 break;
311
312 case 1: /* Include files */
313 if (opt == 1) {
Rob Landley8bb50782006-05-26 23:44:51 +0000314 llist_add_to(&zaccept, optarg);
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000315 break;
316 }
317 if (opt == 'd') {
Paul Fox0840b762005-07-20 20:26:49 +0000318 base_dir = optarg;
319 opt_range += 2;
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000320 break;
Paul Fox0840b762005-07-20 20:26:49 +0000321 }
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000322 if (opt == 'x') {
323 opt_range++;
324 break;
325 }
326 bb_show_usage();
Paul Fox0840b762005-07-20 20:26:49 +0000327
328 case 2 : /* Exclude files */
329 if (opt == 1) {
Rob Landley8bb50782006-05-26 23:44:51 +0000330 llist_add_to(&zreject, optarg);
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000331 break;
332 }
333 if (opt == 'd') { /* Extract to base directory */
Paul Fox0840b762005-07-20 20:26:49 +0000334 base_dir = optarg;
335 opt_range++;
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000336 break;
Paul Fox0840b762005-07-20 20:26:49 +0000337 }
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000338 /* fall through */
Paul Fox0840b762005-07-20 20:26:49 +0000339
340 default:
341 bb_show_usage();
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000342 }
343 }
344
Paul Fox0840b762005-07-20 20:26:49 +0000345 if (src_fn == NULL) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000346 bb_show_usage();
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000347 }
348
Paul Fox0840b762005-07-20 20:26:49 +0000349 /* Open input file */
Denis Vlasenko9f739442006-12-16 23:49:13 +0000350 if (LONE_DASH(src_fn)) {
Denis Vlasenko006e8622008-09-21 01:01:46 +0000351 xdup2(STDIN_FILENO, zip_fd);
Paul Fox0840b762005-07-20 20:26:49 +0000352 /* Cannot use prompt mode since zip data is arriving on STDIN */
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000353 if (overwrite == O_PROMPT)
354 overwrite = O_NEVER;
Glenn L McGrath237ae422002-11-03 14:05:15 +0000355 } else {
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000356 static const char extn[][5] = {"", ".zip", ".ZIP"};
Paul Fox0840b762005-07-20 20:26:49 +0000357 int orig_src_fn_len = strlen(src_fn);
Denis Vlasenko006e8622008-09-21 01:01:46 +0000358 int src_fd = -1;
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000359
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000360 for (i = 0; (i < 3) && (src_fd == -1); i++) {
Paul Fox0840b762005-07-20 20:26:49 +0000361 strcpy(src_fn + orig_src_fn_len, extn[i]);
362 src_fd = open(src_fn, O_RDONLY);
363 }
364 if (src_fd == -1) {
Denis Vlasenko666c40c2007-03-31 10:17:24 +0000365 src_fn[orig_src_fn_len] = '\0';
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000366 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 +0000367 }
Denis Vlasenko006e8622008-09-21 01:01:46 +0000368 xmove_fd(src_fd, zip_fd);
Glenn L McGrath237ae422002-11-03 14:05:15 +0000369 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000370
Paul Fox0840b762005-07-20 20:26:49 +0000371 /* Change dir if necessary */
Bernhard Reutner-Fischerd9cf7ac2006-04-12 18:39:58 +0000372 if (base_dir)
Rob Landleyd921b2e2006-08-03 15:41:12 +0000373 xchdir(base_dir);
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000374
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000375 if (verbose) {
Paul Fox0840b762005-07-20 20:26:49 +0000376 printf("Archive: %s\n", src_fn);
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000377 if (listing){
378 puts(" Length Date Time Name\n"
379 " -------- ---- ---- ----");
380 }
381 }
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000382
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000383 total_size = 0;
384 total_entries = 0;
Denis Vlasenko006e8622008-09-21 01:01:46 +0000385#if ENABLE_DESKTOP
386 cds_entries = 0;
387 cds_offset = 0;
388#endif
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000389 while (1) {
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000390 uint32_t magic;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000391
Paul Fox0840b762005-07-20 20:26:49 +0000392 /* Check magic number */
Denis Vlasenko006e8622008-09-21 01:01:46 +0000393 xread(zip_fd, &magic, 4);
Denis Vlasenko48a99712008-07-26 17:32:41 +0000394 /* Central directory? It's at the end, so exit */
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000395 if (magic == ZIP_CDS_MAGIC)
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000396 break;
Denis Vlasenko006e8622008-09-21 01:01:46 +0000397#if ENABLE_DESKTOP
398 /* Data descriptor? It was a streaming file, go on */
399 if (magic == ZIP_DD_MAGIC) {
400 /* skip over duplicate crc32, cmpsize and ucmpsize */
401 unzip_skip(3 * 4);
402 continue;
403 }
404#endif
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000405 if (magic != ZIP_FILEHEADER_MAGIC)
Denis Vlasenko48a99712008-07-26 17:32:41 +0000406 bb_error_msg_and_die("invalid zip magic %08X", (int)magic);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000407
408 /* Read the file header */
Denis Vlasenko006e8622008-09-21 01:01:46 +0000409 xread(zip_fd, zip_header.raw, ZIP_HEADER_LEN);
410 FIX_ENDIANNESS_ZIP(zip_header);
"Robert P. J. Day"eea56182006-07-20 19:02:24 +0000411 if ((zip_header.formatted.method != 0) && (zip_header.formatted.method != 8)) {
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000412 bb_error_msg_and_die("unsupported method %d", zip_header.formatted.method);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000413 }
Denis Vlasenko006e8622008-09-21 01:01:46 +0000414#if !ENABLE_DESKTOP
415 if (zip_header.formatted.flags & 0x0009) {
416 bb_error_msg_and_die("zip flags 1 and 8 are not supported");
417 }
418#else
419 if (zip_header.formatted.flags & 0x0001) {
Denis Vlasenko48a99712008-07-26 17:32:41 +0000420 /* 0x0001 - encrypted */
Denis Vlasenko006e8622008-09-21 01:01:46 +0000421 bb_error_msg_and_die("zip flag 1 (encryption) is not supported");
422 }
423 if (zip_header.formatted.flags & 0x0008) {
424 cds_header_t cds_header;
Denis Vlasenko48a99712008-07-26 17:32:41 +0000425 /* 0x0008 - streaming. [u]cmpsize can be reliably gotten
426 * only from Central Directory. See unzip_doc.txt */
Denis Vlasenko006e8622008-09-21 01:01:46 +0000427 cds_offset = read_next_cds(total_entries - cds_entries, cds_offset, &cds_header);
428 cds_entries = total_entries + 1;
429 zip_header.formatted.crc32 = cds_header.formatted.crc32;
430 zip_header.formatted.cmpsize = cds_header.formatted.cmpsize;
431 zip_header.formatted.ucmpsize = cds_header.formatted.ucmpsize;
Denis Vlasenko48a99712008-07-26 17:32:41 +0000432 }
Denis Vlasenko006e8622008-09-21 01:01:46 +0000433#endif
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000434
435 /* Read filename */
Paul Fox0840b762005-07-20 20:26:49 +0000436 free(dst_fn);
"Robert P. J. Day"eea56182006-07-20 19:02:24 +0000437 dst_fn = xzalloc(zip_header.formatted.filename_len + 1);
Denis Vlasenko006e8622008-09-21 01:01:46 +0000438 xread(zip_fd, dst_fn, zip_header.formatted.filename_len);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000439
Paul Fox0840b762005-07-20 20:26:49 +0000440 /* Skip extra header bytes */
Denis Vlasenko006e8622008-09-21 01:01:46 +0000441 unzip_skip(zip_header.formatted.extra_len);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000442
Paul Fox0840b762005-07-20 20:26:49 +0000443 /* Filter zip entries */
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000444 if (find_list_entry(zreject, dst_fn)
445 || (zaccept && !find_list_entry(zaccept, dst_fn))
446 ) { /* Skip entry */
Paul Fox0840b762005-07-20 20:26:49 +0000447 i = 'n';
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000448
Paul Fox0840b762005-07-20 20:26:49 +0000449 } else { /* Extract entry */
Paul Fox9382b382007-09-07 20:28:25 +0000450 if (listing) { /* List entry */
Denis Vlasenkofeb7ae72007-10-01 12:05:12 +0000451 if (verbose) {
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000452 unsigned dostime = zip_header.formatted.modtime | (zip_header.formatted.moddate << 16);
Denis Vlasenkofeb7ae72007-10-01 12:05:12 +0000453 printf("%9u %02u-%02u-%02u %02u:%02u %s\n",
"Robert P. J. Day"eea56182006-07-20 19:02:24 +0000454 zip_header.formatted.ucmpsize,
Paul Fox0840b762005-07-20 20:26:49 +0000455 (dostime & 0x01e00000) >> 21,
456 (dostime & 0x001f0000) >> 16,
457 (((dostime & 0xfe000000) >> 25) + 1980) % 100,
458 (dostime & 0x0000f800) >> 11,
459 (dostime & 0x000007e0) >> 5,
460 dst_fn);
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000461 total_size += zip_header.formatted.ucmpsize;
Denis Vlasenkofeb7ae72007-10-01 12:05:12 +0000462 } else {
463 /* short listing -- filenames only */
464 puts(dst_fn);
465 }
466 i = 'n';
Paul Fox0840b762005-07-20 20:26:49 +0000467 } else if (dst_fd == STDOUT_FILENO) { /* Extracting to STDOUT */
468 i = -1;
Paul Fox0840b762005-07-20 20:26:49 +0000469 } else if (last_char_is(dst_fn, '/')) { /* Extract directory */
470 if (stat(dst_fn, &stat_buf) == -1) {
471 if (errno != ENOENT) {
Denis Vlasenko006e8622008-09-21 01:01:46 +0000472 bb_perror_msg_and_die("can't stat '%s'", dst_fn);
Paul Fox0840b762005-07-20 20:26:49 +0000473 }
Paul Fox9382b382007-09-07 20:28:25 +0000474 if (verbose) {
Paul Fox0840b762005-07-20 20:26:49 +0000475 printf(" creating: %s\n", dst_fn);
476 }
477 unzip_create_leading_dirs(dst_fn);
478 if (bb_make_directory(dst_fn, 0777, 0)) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000479 bb_error_msg_and_die("exiting");
Paul Fox0840b762005-07-20 20:26:49 +0000480 }
481 } else {
482 if (!S_ISDIR(stat_buf.st_mode)) {
483 bb_error_msg_and_die("'%s' exists but is not directory", dst_fn);
484 }
485 }
486 i = 'n';
487
488 } else { /* Extract file */
Denis Vlasenko48a99712008-07-26 17:32:41 +0000489 check_file:
Paul Fox0840b762005-07-20 20:26:49 +0000490 if (stat(dst_fn, &stat_buf) == -1) { /* File does not exist */
491 if (errno != ENOENT) {
Denis Vlasenko006e8622008-09-21 01:01:46 +0000492 bb_perror_msg_and_die("can't stat '%s'", dst_fn);
Paul Fox0840b762005-07-20 20:26:49 +0000493 }
494 i = 'y';
Paul Fox0840b762005-07-20 20:26:49 +0000495 } else { /* File already exists */
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000496 if (overwrite == O_NEVER) {
Paul Fox0840b762005-07-20 20:26:49 +0000497 i = 'n';
Paul Fox0840b762005-07-20 20:26:49 +0000498 } else if (S_ISREG(stat_buf.st_mode)) { /* File is regular file */
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000499 if (overwrite == O_ALWAYS) {
Paul Fox0840b762005-07-20 20:26:49 +0000500 i = 'y';
501 } else {
502 printf("replace %s? [y]es, [n]o, [A]ll, [N]one, [r]ename: ", dst_fn);
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000503 if (!fgets(key_buf, sizeof(key_buf), stdin)) {
Denis Vlasenko006e8622008-09-21 01:01:46 +0000504 bb_perror_msg_and_die("can't read input");
Paul Fox0840b762005-07-20 20:26:49 +0000505 }
506 i = key_buf[0];
507 }
Paul Fox0840b762005-07-20 20:26:49 +0000508 } else { /* File is not regular file */
Denis Vlasenkof5d8c902008-06-26 14:32:57 +0000509 bb_error_msg_and_die("'%s' exists but is not regular file", dst_fn);
Paul Fox0840b762005-07-20 20:26:49 +0000510 }
511 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000512 }
513 }
514
Paul Fox0840b762005-07-20 20:26:49 +0000515 switch (i) {
516 case 'A':
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000517 overwrite = O_ALWAYS;
Paul Fox0840b762005-07-20 20:26:49 +0000518 case 'y': /* Open file and fall into unzip */
519 unzip_create_leading_dirs(dst_fn);
Bernhard Reutner-Fischer64d7e932006-09-11 16:01:40 +0000520 dst_fd = xopen(dst_fn, O_WRONLY | O_CREAT | O_TRUNC);
Paul Fox0840b762005-07-20 20:26:49 +0000521 case -1: /* Unzip */
Paul Fox9382b382007-09-07 20:28:25 +0000522 if (verbose) {
Paul Fox0840b762005-07-20 20:26:49 +0000523 printf(" inflating: %s\n", dst_fn);
524 }
Denis Vlasenko006e8622008-09-21 01:01:46 +0000525 unzip_extract(&zip_header, dst_fd);
Paul Fox0840b762005-07-20 20:26:49 +0000526 if (dst_fd != STDOUT_FILENO) {
527 /* closing STDOUT is potentially bad for future business */
528 close(dst_fd);
529 }
530 break;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000531
Paul Fox0840b762005-07-20 20:26:49 +0000532 case 'N':
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000533 overwrite = O_NEVER;
Paul Fox0840b762005-07-20 20:26:49 +0000534 case 'n':
535 /* Skip entry data */
Denis Vlasenko006e8622008-09-21 01:01:46 +0000536 unzip_skip(zip_header.formatted.cmpsize);
Paul Fox0840b762005-07-20 20:26:49 +0000537 break;
538
539 case 'r':
540 /* Prompt for new name */
541 printf("new name: ");
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000542 if (!fgets(key_buf, sizeof(key_buf), stdin)) {
Denis Vlasenko006e8622008-09-21 01:01:46 +0000543 bb_perror_msg_and_die("can't read input");
Paul Fox0840b762005-07-20 20:26:49 +0000544 }
545 free(dst_fn);
Rob Landleyd921b2e2006-08-03 15:41:12 +0000546 dst_fn = xstrdup(key_buf);
Paul Fox0840b762005-07-20 20:26:49 +0000547 chomp(dst_fn);
Denis Vlasenko48a99712008-07-26 17:32:41 +0000548 goto check_file;
Paul Fox0840b762005-07-20 20:26:49 +0000549
550 default:
Rob Landleye7c43b62006-03-01 16:39:45 +0000551 printf("error: invalid response [%c]\n",(char)i);
Denis Vlasenko48a99712008-07-26 17:32:41 +0000552 goto check_file;
Paul Fox0840b762005-07-20 20:26:49 +0000553 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000554
Denis Vlasenko006e8622008-09-21 01:01:46 +0000555 total_entries++;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000556 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000557
Paul Fox9382b382007-09-07 20:28:25 +0000558 if (listing && verbose) {
Rob Landleye7c43b62006-03-01 16:39:45 +0000559 printf(" -------- -------\n"
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000560 "%9d %d files\n",
561 total_size, total_entries);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000562 }
563
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000564 return 0;
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000565}