blob: 632cc8551cea2ea28ffeb3eb540857e5e850fb78 [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
19 * Endian issues
20 * Zip64 + other methods
21 * Improve handling of zip format, ie.
22 * - deferred CRC, comp. & uncomp. lengths (zip header flags bit 3)
23 * - unix file permissions, etc.
24 * - central directory
25 */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000026
Glenn L McGrath87ac7022002-01-02 13:52:26 +000027#include <fcntl.h>
Glenn L McGrath87ac7022002-01-02 13:52:26 +000028#include <stdlib.h>
29#include <string.h>
30#include <unistd.h>
Paul Fox0840b762005-07-20 20:26:49 +000031#include <errno.h>
Glenn L McGrath87ac7022002-01-02 13:52:26 +000032#include "unarchive.h"
33#include "busybox.h"
34
Rob Landley53437472006-07-16 08:14:35 +000035#define ZIP_FILEHEADER_MAGIC SWAP_LE32(0x04034b50)
36#define ZIP_CDS_MAGIC SWAP_LE32(0x02014b50)
37#define ZIP_CDS_END_MAGIC SWAP_LE32(0x06054b50)
38#define ZIP_DD_MAGIC SWAP_LE32(0x08074b50)
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000039
40extern unsigned int gunzip_crc;
41extern unsigned int gunzip_bytes_out;
42
Paul Fox0840b762005-07-20 20:26:49 +000043typedef union {
44 unsigned char raw[26];
45 struct {
46 unsigned short version; /* 0-1 */
47 unsigned short flags; /* 2-3 */
48 unsigned short method; /* 4-5 */
49 unsigned short modtime; /* 6-7 */
50 unsigned short moddate; /* 8-9 */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000051 unsigned int crc32 ATTRIBUTE_PACKED; /* 10-13 */
52 unsigned int cmpsize ATTRIBUTE_PACKED; /* 14-17 */
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +000053 unsigned int ucmpsize ATTRIBUTE_PACKED; /* 18-21 */
Paul Fox0840b762005-07-20 20:26:49 +000054 unsigned short filename_len; /* 22-23 */
55 unsigned short extra_len; /* 24-25 */
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +000056 } formated ATTRIBUTE_PACKED;
Paul Fox0840b762005-07-20 20:26:49 +000057} zip_header_t;
58
59static void unzip_skip(int fd, off_t skip)
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000060{
Paul Fox0840b762005-07-20 20:26:49 +000061 if (lseek(fd, skip, SEEK_CUR) == (off_t)-1) {
62 if ((errno != ESPIPE) || (bb_copyfd_size(fd, -1, skip) != skip)) {
63 bb_error_msg_and_die("Seek failure");
64 }
65 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000066}
67
Paul Fox0840b762005-07-20 20:26:49 +000068static void unzip_create_leading_dirs(char *fn)
69{
70 /* Create all leading directories */
71 char *name = bb_xstrdup(fn);
72 if (bb_make_directory(dirname(name), 0777, FILEUTILS_RECUR)) {
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +000073 bb_error_msg_and_die("Exiting"); /* bb_make_directory is noisy */
Paul Fox0840b762005-07-20 20:26:49 +000074 }
75 free(name);
76}
77
Paul Fox986ab522006-03-27 23:09:14 +000078static int unzip_extract(zip_header_t *zip_header, int src_fd, int dst_fd)
Paul Fox0840b762005-07-20 20:26:49 +000079{
80 if (zip_header->formated.method == 0) {
81 /* Method 0 - stored (not compressed) */
82 int size = zip_header->formated.ucmpsize;
83 if (size && (bb_copyfd_size(src_fd, dst_fd, size) != size)) {
84 bb_error_msg_and_die("Cannot complete extraction");
85 }
86
87 } else {
88 /* Method 8 - inflate */
89 inflate_init(zip_header->formated.cmpsize);
90 inflate_unzip(src_fd, dst_fd);
91 inflate_cleanup();
92 /* Validate decompression - crc */
93 if (zip_header->formated.crc32 != (gunzip_crc ^ 0xffffffffL)) {
94 bb_error_msg("Invalid compressed data--crc error");
Paul Fox986ab522006-03-27 23:09:14 +000095 return 1;
Paul Fox0840b762005-07-20 20:26:49 +000096 }
97 /* Validate decompression - size */
98 if (zip_header->formated.ucmpsize != gunzip_bytes_out) {
99 bb_error_msg("Invalid compressed data--length error");
Paul Fox986ab522006-03-27 23:09:14 +0000100 return 1;
Paul Fox0840b762005-07-20 20:26:49 +0000101 }
102 }
Paul Fox986ab522006-03-27 23:09:14 +0000103 return 0;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000104}
105
Rob Landleydfba7412006-03-06 20:47:33 +0000106int unzip_main(int argc, char **argv)
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000107{
Paul Fox0840b762005-07-20 20:26:49 +0000108 zip_header_t zip_header;
109 enum {v_silent, v_normal, v_list} verbosity = v_normal;
110 enum {o_prompt, o_never, o_always} overwrite = o_prompt;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000111 unsigned int total_size = 0;
112 unsigned int total_entries = 0;
Paul Fox0840b762005-07-20 20:26:49 +0000113 int src_fd = -1, dst_fd = -1;
114 char *src_fn = NULL, *dst_fn = NULL;
Mike Frysinger69024552005-07-30 07:30:26 +0000115 llist_t *zaccept = NULL;
116 llist_t *zreject = NULL;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000117 char *base_dir = NULL;
Paul Fox986ab522006-03-27 23:09:14 +0000118 int failed, i, opt, opt_range = 0, list_header_done = 0;
Paul Fox0840b762005-07-20 20:26:49 +0000119 char key_buf[512];
120 struct stat stat_buf;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000121
Paul Fox0840b762005-07-20 20:26:49 +0000122 while((opt = getopt(argc, argv, "-d:lnopqx")) != -1) {
123 switch(opt_range) {
124 case 0: /* Options */
125 switch(opt) {
126 case 'l': /* List */
127 verbosity = v_list;
128 break;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000129
Paul Fox0840b762005-07-20 20:26:49 +0000130 case 'n': /* Never overwrite existing files */
131 overwrite = o_never;
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000132 break;
Paul Fox0840b762005-07-20 20:26:49 +0000133
134 case 'o': /* Always overwrite existing files */
135 overwrite = o_always;
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000136 break;
Paul Fox0840b762005-07-20 20:26:49 +0000137
138 case 'p': /* Extract files to stdout and fall through to set verbosity */
139 dst_fd = STDOUT_FILENO;
140
141 case 'q': /* Be quiet */
142 verbosity = (verbosity == v_normal) ? v_silent : verbosity;
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000143 break;
Paul Fox0840b762005-07-20 20:26:49 +0000144
145 case 1 : /* The zip file */
146 src_fn = bb_xstrndup(optarg, strlen(optarg)+4);
147 opt_range++;
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000148 break;
Paul Fox0840b762005-07-20 20:26:49 +0000149
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000150 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000151 bb_show_usage();
Paul Fox0840b762005-07-20 20:26:49 +0000152
153 }
154 break;
155
156 case 1: /* Include files */
157 if (opt == 1) {
Rob Landley8bb50782006-05-26 23:44:51 +0000158 llist_add_to(&zaccept, optarg);
Paul Fox0840b762005-07-20 20:26:49 +0000159
160 } else if (opt == 'd') {
161 base_dir = optarg;
162 opt_range += 2;
163
164 } else if (opt == 'x') {
165 opt_range++;
166
167 } else {
168 bb_show_usage();
169 }
170 break;
171
172 case 2 : /* Exclude files */
173 if (opt == 1) {
Rob Landley8bb50782006-05-26 23:44:51 +0000174 llist_add_to(&zreject, optarg);
Paul Fox0840b762005-07-20 20:26:49 +0000175
176 } else if (opt == 'd') { /* Extract to base directory */
177 base_dir = optarg;
178 opt_range++;
179
180 } else {
181 bb_show_usage();
182 }
183 break;
184
185 default:
186 bb_show_usage();
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000187 }
188 }
189
Paul Fox0840b762005-07-20 20:26:49 +0000190 if (src_fn == NULL) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000191 bb_show_usage();
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000192 }
193
Paul Fox0840b762005-07-20 20:26:49 +0000194 /* Open input file */
195 if (strcmp("-", src_fn) == 0) {
196 src_fd = STDIN_FILENO;
197 /* Cannot use prompt mode since zip data is arriving on STDIN */
198 overwrite = (overwrite == o_prompt) ? o_never : overwrite;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000199
Glenn L McGrath237ae422002-11-03 14:05:15 +0000200 } else {
Rob Landley0a7c8ef2006-02-22 17:01:00 +0000201 static const char *const extn[] = {"", ".zip", ".ZIP"};
Paul Fox0840b762005-07-20 20:26:49 +0000202 int orig_src_fn_len = strlen(src_fn);
203 for(i = 0; (i < 3) && (src_fd == -1); i++) {
204 strcpy(src_fn + orig_src_fn_len, extn[i]);
205 src_fd = open(src_fn, O_RDONLY);
206 }
207 if (src_fd == -1) {
208 src_fn[orig_src_fn_len] = 0;
209 bb_error_msg_and_die("Cannot open %s, %s.zip, %s.ZIP", src_fn, src_fn, src_fn);
210 }
Glenn L McGrath237ae422002-11-03 14:05:15 +0000211 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000212
Paul Fox0840b762005-07-20 20:26:49 +0000213 /* Change dir if necessary */
Bernhard Reutner-Fischerd9cf7ac2006-04-12 18:39:58 +0000214 if (base_dir)
215 bb_xchdir(base_dir);
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000216
Paul Fox0840b762005-07-20 20:26:49 +0000217 if (verbosity != v_silent)
218 printf("Archive: %s\n", src_fn);
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000219
Paul Fox986ab522006-03-27 23:09:14 +0000220 failed = 0;
221
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000222 while (1) {
223 unsigned int magic;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000224
Paul Fox0840b762005-07-20 20:26:49 +0000225 /* Check magic number */
Rob Landley53437472006-07-16 08:14:35 +0000226 xread(src_fd, &magic, 4);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000227 if (magic == ZIP_CDS_MAGIC) {
228 break;
Paul Fox0840b762005-07-20 20:26:49 +0000229 } else if (magic != ZIP_FILEHEADER_MAGIC) {
230 bb_error_msg_and_die("Invalid zip magic %08X", magic);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000231 }
232
233 /* Read the file header */
Rob Landley53437472006-07-16 08:14:35 +0000234 xread(src_fd, zip_header.raw, 26);
235 zip_header.formated.version = SWAP_LE32(zip_header.formated.version);
236 zip_header.formated.flags = SWAP_LE32(zip_header.formated.flags);
237 zip_header.formated.method = SWAP_LE32(zip_header.formated.method);
238 zip_header.formated.modtime = SWAP_LE32(zip_header.formated.modtime);
239 zip_header.formated.moddate = SWAP_LE32(zip_header.formated.moddate);
240 zip_header.formated.crc32 = SWAP_LE32(zip_header.formated.crc32);
241 zip_header.formated.cmpsize = SWAP_LE32(zip_header.formated.cmpsize);
242 zip_header.formated.ucmpsize = SWAP_LE32(zip_header.formated.ucmpsize);
243 zip_header.formated.filename_len = SWAP_LE32(zip_header.formated.filename_len);
244 zip_header.formated.extra_len = SWAP_LE32(zip_header.formated.extra_len);
Paul Fox0840b762005-07-20 20:26:49 +0000245 if ((zip_header.formated.method != 0) && (zip_header.formated.method != 8)) {
246 bb_error_msg_and_die("Unsupported compression method %d", zip_header.formated.method);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000247 }
248
249 /* Read filename */
Paul Fox0840b762005-07-20 20:26:49 +0000250 free(dst_fn);
Rob Landley1ec5b292006-05-29 07:42:02 +0000251 dst_fn = xzalloc(zip_header.formated.filename_len + 1);
Rob Landley53437472006-07-16 08:14:35 +0000252 xread(src_fd, dst_fn, zip_header.formated.filename_len);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000253
Paul Fox0840b762005-07-20 20:26:49 +0000254 /* Skip extra header bytes */
255 unzip_skip(src_fd, zip_header.formated.extra_len);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000256
Paul Fox0840b762005-07-20 20:26:49 +0000257 if ((verbosity == v_list) && !list_header_done){
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000258 printf(" Length Date Time Name\n"
259 " -------- ---- ---- ----\n");
Paul Fox0840b762005-07-20 20:26:49 +0000260 list_header_done = 1;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000261 }
262
Paul Fox0840b762005-07-20 20:26:49 +0000263 /* Filter zip entries */
Mike Frysinger69024552005-07-30 07:30:26 +0000264 if (find_list_entry(zreject, dst_fn) ||
265 (zaccept && !find_list_entry(zaccept, dst_fn))) { /* Skip entry */
Paul Fox0840b762005-07-20 20:26:49 +0000266 i = 'n';
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000267
Paul Fox0840b762005-07-20 20:26:49 +0000268 } else { /* Extract entry */
269 total_size += zip_header.formated.ucmpsize;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000270
Paul Fox0840b762005-07-20 20:26:49 +0000271 if (verbosity == v_list) { /* List entry */
272 unsigned int dostime = zip_header.formated.modtime | (zip_header.formated.moddate << 16);
273 printf("%9u %02u-%02u-%02u %02u:%02u %s\n",
274 zip_header.formated.ucmpsize,
275 (dostime & 0x01e00000) >> 21,
276 (dostime & 0x001f0000) >> 16,
277 (((dostime & 0xfe000000) >> 25) + 1980) % 100,
278 (dostime & 0x0000f800) >> 11,
279 (dostime & 0x000007e0) >> 5,
280 dst_fn);
281 total_entries++;
282 i = 'n';
283
284 } else if (dst_fd == STDOUT_FILENO) { /* Extracting to STDOUT */
285 i = -1;
286
287 } else if (last_char_is(dst_fn, '/')) { /* Extract directory */
288 if (stat(dst_fn, &stat_buf) == -1) {
289 if (errno != ENOENT) {
290 bb_perror_msg_and_die("Cannot stat '%s'",dst_fn);
291 }
292 if (verbosity == v_normal) {
293 printf(" creating: %s\n", dst_fn);
294 }
295 unzip_create_leading_dirs(dst_fn);
296 if (bb_make_directory(dst_fn, 0777, 0)) {
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000297 bb_error_msg_and_die("Exiting");
Paul Fox0840b762005-07-20 20:26:49 +0000298 }
299 } else {
300 if (!S_ISDIR(stat_buf.st_mode)) {
301 bb_error_msg_and_die("'%s' exists but is not directory", dst_fn);
302 }
303 }
304 i = 'n';
305
306 } else { /* Extract file */
307 _check_file:
308 if (stat(dst_fn, &stat_buf) == -1) { /* File does not exist */
309 if (errno != ENOENT) {
310 bb_perror_msg_and_die("Cannot stat '%s'",dst_fn);
311 }
312 i = 'y';
313
314 } else { /* File already exists */
315 if (overwrite == o_never) {
316 i = 'n';
317
318 } else if (S_ISREG(stat_buf.st_mode)) { /* File is regular file */
319 if (overwrite == o_always) {
320 i = 'y';
321 } else {
322 printf("replace %s? [y]es, [n]o, [A]ll, [N]one, [r]ename: ", dst_fn);
323 if (!fgets(key_buf, 512, stdin)) {
324 bb_perror_msg_and_die("Cannot read input");
325 }
326 i = key_buf[0];
327 }
328
329 } else { /* File is not regular file */
330 bb_error_msg_and_die("'%s' exists but is not regular file",dst_fn);
331 }
332 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000333 }
334 }
335
Paul Fox0840b762005-07-20 20:26:49 +0000336 switch (i) {
337 case 'A':
338 overwrite = o_always;
339 case 'y': /* Open file and fall into unzip */
340 unzip_create_leading_dirs(dst_fn);
341 dst_fd = bb_xopen(dst_fn, O_WRONLY | O_CREAT);
342 case -1: /* Unzip */
343 if (verbosity == v_normal) {
344 printf(" inflating: %s\n", dst_fn);
345 }
Paul Fox986ab522006-03-27 23:09:14 +0000346 if (unzip_extract(&zip_header, src_fd, dst_fd)) {
347 failed = 1;
348 }
Paul Fox0840b762005-07-20 20:26:49 +0000349 if (dst_fd != STDOUT_FILENO) {
350 /* closing STDOUT is potentially bad for future business */
351 close(dst_fd);
352 }
353 break;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000354
Paul Fox0840b762005-07-20 20:26:49 +0000355 case 'N':
356 overwrite = o_never;
357 case 'n':
358 /* Skip entry data */
359 unzip_skip(src_fd, zip_header.formated.cmpsize);
360 break;
361
362 case 'r':
363 /* Prompt for new name */
364 printf("new name: ");
365 if (!fgets(key_buf, 512, stdin)) {
366 bb_perror_msg_and_die("Cannot read input");
367 }
368 free(dst_fn);
369 dst_fn = bb_xstrdup(key_buf);
370 chomp(dst_fn);
371 goto _check_file;
372
373 default:
Rob Landleye7c43b62006-03-01 16:39:45 +0000374 printf("error: invalid response [%c]\n",(char)i);
Paul Fox0840b762005-07-20 20:26:49 +0000375 goto _check_file;
376 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000377
378 /* Data descriptor section */
379 if (zip_header.formated.flags & 4) {
380 /* skip over duplicate crc, compressed size and uncompressed size */
Paul Fox0840b762005-07-20 20:26:49 +0000381 unzip_skip(src_fd, 12);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000382 }
383 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000384
Paul Fox0840b762005-07-20 20:26:49 +0000385 if (verbosity == v_list) {
Rob Landleye7c43b62006-03-01 16:39:45 +0000386 printf(" -------- -------\n"
387 "%9d %d files\n", total_size, total_entries);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000388 }
389
Paul Fox986ab522006-03-27 23:09:14 +0000390 return failed;
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000391}