blob: 8ba39e9af936440d576cd36b9cae0ffd835b6a77 [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 Landley688ed0d2006-03-04 22:40:25 +000035#if BB_BIG_ENDIAN
Paul Fox0840b762005-07-20 20:26:49 +000036static inline unsigned short
37__swap16(unsigned short x) {
38 return (((uint16_t)(x) & 0xFF) << 8) | (((uint16_t)(x) & 0xFF00) >> 8);
39}
40
41static inline uint32_t
42__swap32(uint32_t x) {
43 return (((x & 0xFF) << 24) |
44 ((x & 0xFF00) << 8) |
45 ((x & 0xFF0000) >> 8) |
46 ((x & 0xFF000000) >> 24));
47}
Rob Landley688ed0d2006-03-04 22:40:25 +000048#else /* it's little-endian */
49# define __swap16(x) (x)
50# define __swap32(x) (x)
51#endif /* BB_BIG_ENDIAN */
Paul Fox0840b762005-07-20 20:26:49 +000052
53#define ZIP_FILEHEADER_MAGIC __swap32(0x04034b50)
54#define ZIP_CDS_MAGIC __swap32(0x02014b50)
55#define ZIP_CDS_END_MAGIC __swap32(0x06054b50)
56#define ZIP_DD_MAGIC __swap32(0x08074b50)
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000057
58extern unsigned int gunzip_crc;
59extern unsigned int gunzip_bytes_out;
60
Paul Fox0840b762005-07-20 20:26:49 +000061typedef union {
62 unsigned char raw[26];
63 struct {
64 unsigned short version; /* 0-1 */
65 unsigned short flags; /* 2-3 */
66 unsigned short method; /* 4-5 */
67 unsigned short modtime; /* 6-7 */
68 unsigned short moddate; /* 8-9 */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000069 unsigned int crc32 ATTRIBUTE_PACKED; /* 10-13 */
70 unsigned int cmpsize ATTRIBUTE_PACKED; /* 14-17 */
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +000071 unsigned int ucmpsize ATTRIBUTE_PACKED; /* 18-21 */
Paul Fox0840b762005-07-20 20:26:49 +000072 unsigned short filename_len; /* 22-23 */
73 unsigned short extra_len; /* 24-25 */
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +000074 } formated ATTRIBUTE_PACKED;
Paul Fox0840b762005-07-20 20:26:49 +000075} zip_header_t;
76
77static void unzip_skip(int fd, off_t skip)
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000078{
Paul Fox0840b762005-07-20 20:26:49 +000079 if (lseek(fd, skip, SEEK_CUR) == (off_t)-1) {
80 if ((errno != ESPIPE) || (bb_copyfd_size(fd, -1, skip) != skip)) {
81 bb_error_msg_and_die("Seek failure");
82 }
83 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000084}
85
Paul Fox0840b762005-07-20 20:26:49 +000086static void unzip_read(int fd, void *buf, size_t count)
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000087{
Paul Fox0840b762005-07-20 20:26:49 +000088 if (bb_xread(fd, buf, count) != count) {
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +000089 bb_error_msg_and_die(bb_msg_read_error);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000090 }
Paul Fox0840b762005-07-20 20:26:49 +000091}
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000092
Paul Fox0840b762005-07-20 20:26:49 +000093static void unzip_create_leading_dirs(char *fn)
94{
95 /* Create all leading directories */
96 char *name = bb_xstrdup(fn);
97 if (bb_make_directory(dirname(name), 0777, FILEUTILS_RECUR)) {
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +000098 bb_error_msg_and_die("Exiting"); /* bb_make_directory is noisy */
Paul Fox0840b762005-07-20 20:26:49 +000099 }
100 free(name);
101}
102
Paul Fox986ab522006-03-27 23:09:14 +0000103static int unzip_extract(zip_header_t *zip_header, int src_fd, int dst_fd)
Paul Fox0840b762005-07-20 20:26:49 +0000104{
105 if (zip_header->formated.method == 0) {
106 /* Method 0 - stored (not compressed) */
107 int size = zip_header->formated.ucmpsize;
108 if (size && (bb_copyfd_size(src_fd, dst_fd, size) != size)) {
109 bb_error_msg_and_die("Cannot complete extraction");
110 }
111
112 } else {
113 /* Method 8 - inflate */
114 inflate_init(zip_header->formated.cmpsize);
115 inflate_unzip(src_fd, dst_fd);
116 inflate_cleanup();
117 /* Validate decompression - crc */
118 if (zip_header->formated.crc32 != (gunzip_crc ^ 0xffffffffL)) {
119 bb_error_msg("Invalid compressed data--crc error");
Paul Fox986ab522006-03-27 23:09:14 +0000120 return 1;
Paul Fox0840b762005-07-20 20:26:49 +0000121 }
122 /* Validate decompression - size */
123 if (zip_header->formated.ucmpsize != gunzip_bytes_out) {
124 bb_error_msg("Invalid compressed data--length error");
Paul Fox986ab522006-03-27 23:09:14 +0000125 return 1;
Paul Fox0840b762005-07-20 20:26:49 +0000126 }
127 }
Paul Fox986ab522006-03-27 23:09:14 +0000128 return 0;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000129}
130
Rob Landleydfba7412006-03-06 20:47:33 +0000131int unzip_main(int argc, char **argv)
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000132{
Paul Fox0840b762005-07-20 20:26:49 +0000133 zip_header_t zip_header;
134 enum {v_silent, v_normal, v_list} verbosity = v_normal;
135 enum {o_prompt, o_never, o_always} overwrite = o_prompt;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000136 unsigned int total_size = 0;
137 unsigned int total_entries = 0;
Paul Fox0840b762005-07-20 20:26:49 +0000138 int src_fd = -1, dst_fd = -1;
139 char *src_fn = NULL, *dst_fn = NULL;
Mike Frysinger69024552005-07-30 07:30:26 +0000140 llist_t *zaccept = NULL;
141 llist_t *zreject = NULL;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000142 char *base_dir = NULL;
Paul Fox986ab522006-03-27 23:09:14 +0000143 int failed, i, opt, opt_range = 0, list_header_done = 0;
Paul Fox0840b762005-07-20 20:26:49 +0000144 char key_buf[512];
145 struct stat stat_buf;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000146
Paul Fox0840b762005-07-20 20:26:49 +0000147 while((opt = getopt(argc, argv, "-d:lnopqx")) != -1) {
148 switch(opt_range) {
149 case 0: /* Options */
150 switch(opt) {
151 case 'l': /* List */
152 verbosity = v_list;
153 break;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000154
Paul Fox0840b762005-07-20 20:26:49 +0000155 case 'n': /* Never overwrite existing files */
156 overwrite = o_never;
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000157 break;
Paul Fox0840b762005-07-20 20:26:49 +0000158
159 case 'o': /* Always overwrite existing files */
160 overwrite = o_always;
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000161 break;
Paul Fox0840b762005-07-20 20:26:49 +0000162
163 case 'p': /* Extract files to stdout and fall through to set verbosity */
164 dst_fd = STDOUT_FILENO;
165
166 case 'q': /* Be quiet */
167 verbosity = (verbosity == v_normal) ? v_silent : verbosity;
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000168 break;
Paul Fox0840b762005-07-20 20:26:49 +0000169
170 case 1 : /* The zip file */
171 src_fn = bb_xstrndup(optarg, strlen(optarg)+4);
172 opt_range++;
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000173 break;
Paul Fox0840b762005-07-20 20:26:49 +0000174
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000175 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000176 bb_show_usage();
Paul Fox0840b762005-07-20 20:26:49 +0000177
178 }
179 break;
180
181 case 1: /* Include files */
182 if (opt == 1) {
Rob Landley8bb50782006-05-26 23:44:51 +0000183 llist_add_to(&zaccept, optarg);
Paul Fox0840b762005-07-20 20:26:49 +0000184
185 } else if (opt == 'd') {
186 base_dir = optarg;
187 opt_range += 2;
188
189 } else if (opt == 'x') {
190 opt_range++;
191
192 } else {
193 bb_show_usage();
194 }
195 break;
196
197 case 2 : /* Exclude files */
198 if (opt == 1) {
Rob Landley8bb50782006-05-26 23:44:51 +0000199 llist_add_to(&zreject, optarg);
Paul Fox0840b762005-07-20 20:26:49 +0000200
201 } else if (opt == 'd') { /* Extract to base directory */
202 base_dir = optarg;
203 opt_range++;
204
205 } else {
206 bb_show_usage();
207 }
208 break;
209
210 default:
211 bb_show_usage();
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000212 }
213 }
214
Paul Fox0840b762005-07-20 20:26:49 +0000215 if (src_fn == NULL) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000216 bb_show_usage();
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000217 }
218
Paul Fox0840b762005-07-20 20:26:49 +0000219 /* Open input file */
220 if (strcmp("-", src_fn) == 0) {
221 src_fd = STDIN_FILENO;
222 /* Cannot use prompt mode since zip data is arriving on STDIN */
223 overwrite = (overwrite == o_prompt) ? o_never : overwrite;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000224
Glenn L McGrath237ae422002-11-03 14:05:15 +0000225 } else {
Rob Landley0a7c8ef2006-02-22 17:01:00 +0000226 static const char *const extn[] = {"", ".zip", ".ZIP"};
Paul Fox0840b762005-07-20 20:26:49 +0000227 int orig_src_fn_len = strlen(src_fn);
228 for(i = 0; (i < 3) && (src_fd == -1); i++) {
229 strcpy(src_fn + orig_src_fn_len, extn[i]);
230 src_fd = open(src_fn, O_RDONLY);
231 }
232 if (src_fd == -1) {
233 src_fn[orig_src_fn_len] = 0;
234 bb_error_msg_and_die("Cannot open %s, %s.zip, %s.ZIP", src_fn, src_fn, src_fn);
235 }
Glenn L McGrath237ae422002-11-03 14:05:15 +0000236 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000237
Paul Fox0840b762005-07-20 20:26:49 +0000238 /* Change dir if necessary */
Bernhard Reutner-Fischerd9cf7ac2006-04-12 18:39:58 +0000239 if (base_dir)
240 bb_xchdir(base_dir);
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000241
Paul Fox0840b762005-07-20 20:26:49 +0000242 if (verbosity != v_silent)
243 printf("Archive: %s\n", src_fn);
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000244
Paul Fox986ab522006-03-27 23:09:14 +0000245 failed = 0;
246
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000247 while (1) {
248 unsigned int magic;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000249
Paul Fox0840b762005-07-20 20:26:49 +0000250 /* Check magic number */
251 unzip_read(src_fd, &magic, 4);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000252 if (magic == ZIP_CDS_MAGIC) {
253 break;
Paul Fox0840b762005-07-20 20:26:49 +0000254 } else if (magic != ZIP_FILEHEADER_MAGIC) {
255 bb_error_msg_and_die("Invalid zip magic %08X", magic);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000256 }
257
258 /* Read the file header */
Paul Fox0840b762005-07-20 20:26:49 +0000259 unzip_read(src_fd, zip_header.raw, 26);
Rob Landley688ed0d2006-03-04 22:40:25 +0000260#if BB_BIG_ENDIAN
Paul Fox0840b762005-07-20 20:26:49 +0000261 zip_header.formated.version = __swap16(zip_header.formated.version);
262 zip_header.formated.flags = __swap16(zip_header.formated.flags);
263 zip_header.formated.method = __swap16(zip_header.formated.method);
264 zip_header.formated.modtime = __swap16(zip_header.formated.modtime);
265 zip_header.formated.moddate = __swap16(zip_header.formated.moddate);
266 zip_header.formated.crc32 = __swap32(zip_header.formated.crc32);
267 zip_header.formated.cmpsize = __swap32(zip_header.formated.cmpsize);
268 zip_header.formated.ucmpsize = __swap32(zip_header.formated.ucmpsize);
269 zip_header.formated.filename_len = __swap16(zip_header.formated.filename_len);
270 zip_header.formated.extra_len = __swap16(zip_header.formated.extra_len);
Rob Landley688ed0d2006-03-04 22:40:25 +0000271#endif /* BB_BIG_ENDIAN */
Paul Fox0840b762005-07-20 20:26:49 +0000272 if ((zip_header.formated.method != 0) && (zip_header.formated.method != 8)) {
273 bb_error_msg_and_die("Unsupported compression method %d", zip_header.formated.method);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000274 }
275
276 /* Read filename */
Paul Fox0840b762005-07-20 20:26:49 +0000277 free(dst_fn);
Rob Landley1ec5b292006-05-29 07:42:02 +0000278 dst_fn = xzalloc(zip_header.formated.filename_len + 1);
Paul Fox0840b762005-07-20 20:26:49 +0000279 unzip_read(src_fd, dst_fn, zip_header.formated.filename_len);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000280
Paul Fox0840b762005-07-20 20:26:49 +0000281 /* Skip extra header bytes */
282 unzip_skip(src_fd, zip_header.formated.extra_len);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000283
Paul Fox0840b762005-07-20 20:26:49 +0000284 if ((verbosity == v_list) && !list_header_done){
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000285 printf(" Length Date Time Name\n"
286 " -------- ---- ---- ----\n");
Paul Fox0840b762005-07-20 20:26:49 +0000287 list_header_done = 1;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000288 }
289
Paul Fox0840b762005-07-20 20:26:49 +0000290 /* Filter zip entries */
Mike Frysinger69024552005-07-30 07:30:26 +0000291 if (find_list_entry(zreject, dst_fn) ||
292 (zaccept && !find_list_entry(zaccept, dst_fn))) { /* Skip entry */
Paul Fox0840b762005-07-20 20:26:49 +0000293 i = 'n';
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000294
Paul Fox0840b762005-07-20 20:26:49 +0000295 } else { /* Extract entry */
296 total_size += zip_header.formated.ucmpsize;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000297
Paul Fox0840b762005-07-20 20:26:49 +0000298 if (verbosity == v_list) { /* List entry */
299 unsigned int dostime = zip_header.formated.modtime | (zip_header.formated.moddate << 16);
300 printf("%9u %02u-%02u-%02u %02u:%02u %s\n",
301 zip_header.formated.ucmpsize,
302 (dostime & 0x01e00000) >> 21,
303 (dostime & 0x001f0000) >> 16,
304 (((dostime & 0xfe000000) >> 25) + 1980) % 100,
305 (dostime & 0x0000f800) >> 11,
306 (dostime & 0x000007e0) >> 5,
307 dst_fn);
308 total_entries++;
309 i = 'n';
310
311 } else if (dst_fd == STDOUT_FILENO) { /* Extracting to STDOUT */
312 i = -1;
313
314 } else if (last_char_is(dst_fn, '/')) { /* Extract directory */
315 if (stat(dst_fn, &stat_buf) == -1) {
316 if (errno != ENOENT) {
317 bb_perror_msg_and_die("Cannot stat '%s'",dst_fn);
318 }
319 if (verbosity == v_normal) {
320 printf(" creating: %s\n", dst_fn);
321 }
322 unzip_create_leading_dirs(dst_fn);
323 if (bb_make_directory(dst_fn, 0777, 0)) {
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000324 bb_error_msg_and_die("Exiting");
Paul Fox0840b762005-07-20 20:26:49 +0000325 }
326 } else {
327 if (!S_ISDIR(stat_buf.st_mode)) {
328 bb_error_msg_and_die("'%s' exists but is not directory", dst_fn);
329 }
330 }
331 i = 'n';
332
333 } else { /* Extract file */
334 _check_file:
335 if (stat(dst_fn, &stat_buf) == -1) { /* File does not exist */
336 if (errno != ENOENT) {
337 bb_perror_msg_and_die("Cannot stat '%s'",dst_fn);
338 }
339 i = 'y';
340
341 } else { /* File already exists */
342 if (overwrite == o_never) {
343 i = 'n';
344
345 } else if (S_ISREG(stat_buf.st_mode)) { /* File is regular file */
346 if (overwrite == o_always) {
347 i = 'y';
348 } else {
349 printf("replace %s? [y]es, [n]o, [A]ll, [N]one, [r]ename: ", dst_fn);
350 if (!fgets(key_buf, 512, stdin)) {
351 bb_perror_msg_and_die("Cannot read input");
352 }
353 i = key_buf[0];
354 }
355
356 } else { /* File is not regular file */
357 bb_error_msg_and_die("'%s' exists but is not regular file",dst_fn);
358 }
359 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000360 }
361 }
362
Paul Fox0840b762005-07-20 20:26:49 +0000363 switch (i) {
364 case 'A':
365 overwrite = o_always;
366 case 'y': /* Open file and fall into unzip */
367 unzip_create_leading_dirs(dst_fn);
368 dst_fd = bb_xopen(dst_fn, O_WRONLY | O_CREAT);
369 case -1: /* Unzip */
370 if (verbosity == v_normal) {
371 printf(" inflating: %s\n", dst_fn);
372 }
Paul Fox986ab522006-03-27 23:09:14 +0000373 if (unzip_extract(&zip_header, src_fd, dst_fd)) {
374 failed = 1;
375 }
Paul Fox0840b762005-07-20 20:26:49 +0000376 if (dst_fd != STDOUT_FILENO) {
377 /* closing STDOUT is potentially bad for future business */
378 close(dst_fd);
379 }
380 break;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000381
Paul Fox0840b762005-07-20 20:26:49 +0000382 case 'N':
383 overwrite = o_never;
384 case 'n':
385 /* Skip entry data */
386 unzip_skip(src_fd, zip_header.formated.cmpsize);
387 break;
388
389 case 'r':
390 /* Prompt for new name */
391 printf("new name: ");
392 if (!fgets(key_buf, 512, stdin)) {
393 bb_perror_msg_and_die("Cannot read input");
394 }
395 free(dst_fn);
396 dst_fn = bb_xstrdup(key_buf);
397 chomp(dst_fn);
398 goto _check_file;
399
400 default:
Rob Landleye7c43b62006-03-01 16:39:45 +0000401 printf("error: invalid response [%c]\n",(char)i);
Paul Fox0840b762005-07-20 20:26:49 +0000402 goto _check_file;
403 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000404
405 /* Data descriptor section */
406 if (zip_header.formated.flags & 4) {
407 /* skip over duplicate crc, compressed size and uncompressed size */
Paul Fox0840b762005-07-20 20:26:49 +0000408 unzip_skip(src_fd, 12);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000409 }
410 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000411
Paul Fox0840b762005-07-20 20:26:49 +0000412 if (verbosity == v_list) {
Rob Landleye7c43b62006-03-01 16:39:45 +0000413 printf(" -------- -------\n"
414 "%9d %d files\n", total_size, total_entries);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000415 }
416
Paul Fox986ab522006-03-27 23:09:14 +0000417 return failed;
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000418}