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