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