This commit was manufactured by cvs2svn to create tag 'busybox_1_00'.
[oweals/busybox.git] / busybox / archival / unzip.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini unzip implementation for busybox
4  *
5  * Copyright (C) 2001 by Laurence Anderson
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  *
21  */
22
23 /* For reference see
24  * http://www.pkware.com/products/enterprise/white_papers/appnote.txt
25  * http://www.info-zip.org/pub/infozip/doc/appnote-iz-latest.zip
26  */
27
28 /* TODO Endian issues, exclude, should we accept input from stdin ? */
29
30 #include <fcntl.h>
31 #include <getopt.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include "unarchive.h"
36 #include "busybox.h"
37
38 #define ZIP_FILEHEADER_MAGIC    0x04034b50
39 #define ZIP_CDS_MAGIC                   0x02014b50
40 #define ZIP_CDS_END_MAGIC               0x06054b50
41 #define ZIP_DD_MAGIC                    0x08074b50
42
43 extern unsigned int gunzip_crc;
44 extern unsigned int gunzip_bytes_out;
45
46 static void header_list_unzip(const file_header_t *file_header)
47 {
48         printf("  inflating: %s\n", file_header->name);
49 }
50
51 static void header_verbose_list_unzip(const file_header_t *file_header)
52 {
53         unsigned int dostime = (unsigned int) file_header->mtime;
54
55         /* can printf arguments cut of the decade component ? */
56         unsigned short year = 1980 + ((dostime & 0xfe000000) >> 25);
57         while (year >= 100) {
58                 year -= 100;
59         }
60
61         printf("%9u  %02u-%02u-%02u %02u:%02u   %s\n",
62                 (unsigned int) file_header->size,
63                 (dostime & 0x01e00000) >> 21,
64                 (dostime & 0x001f0000) >> 16,
65                 year,
66                 (dostime & 0x0000f800) >> 11,
67                 (dostime & 0x000007e0) >> 5,
68                 file_header->name);
69 }
70
71 extern int unzip_main(int argc, char **argv)
72 {
73         union {
74                 unsigned char raw[26];
75                 struct {
76                         unsigned short version; /* 0-1 */
77                         unsigned short flags;   /* 2-3 */
78                         unsigned short method;  /* 4-5 */
79                         unsigned short modtime; /* 6-7 */
80                         unsigned short moddate; /* 8-9 */
81                         unsigned int crc32 __attribute__ ((packed));            /* 10-13 */
82                         unsigned int cmpsize __attribute__ ((packed));; /* 14-17 */
83                         unsigned int ucmpsize __attribute__ ((packed));;        /* 18-21 */
84                         unsigned short filename_len;            /* 22-23 */
85                         unsigned short extra_len;               /* 24-25 */
86                 } formated __attribute__ ((packed));
87         } zip_header;
88
89         archive_handle_t *archive_handle;
90         unsigned int total_size = 0;
91         unsigned int total_entries = 0;
92         char *base_dir = NULL;
93         int opt = 0;
94
95         /* Initialise */
96         archive_handle = init_handle();
97         archive_handle->action_data = NULL;
98         archive_handle->action_header = header_list_unzip;
99
100         while ((opt = getopt(argc, argv, "lnopqd:")) != -1) {
101                 switch (opt) {
102                         case 'l':       /* list */
103                                 archive_handle->action_header = header_verbose_list_unzip;
104                                 archive_handle->action_data = data_skip;
105                                 break;
106                         case 'n':       /* never overwright existing files */
107                                 break;
108                         case 'o':
109                                 archive_handle->flags = ARCHIVE_EXTRACT_UNCONDITIONAL;
110                                 break;
111                         case 'p':       /* extract files to stdout */
112                                 archive_handle->action_data = data_extract_to_stdout;
113                                 break;
114                         case 'q':       /* Extract files quietly */
115                                 archive_handle->action_header = header_skip;
116                                 break;
117                         case 'd':       /* Extract files to specified base directory*/
118                                 base_dir = optarg;
119                                 break;
120 #if 0
121                         case 'x':       /* Exclude the specified files */
122                                 archive_handle->filter = filter_accept_reject_list;
123                                 break;
124 #endif
125                         default:
126                                 bb_show_usage();
127                 }
128         }
129
130         if (argc == optind) {
131                 bb_show_usage();
132         }
133
134         printf("Archive:  %s\n", argv[optind]);
135         if (archive_handle->action_header == header_verbose_list_unzip) {
136                 printf("  Length     Date   Time    Name\n");
137                 printf(" --------    ----   ----    ----\n");
138         }
139
140         if (*argv[optind] == '-') {
141                 archive_handle->src_fd = STDIN_FILENO;
142                 archive_handle->seek = seek_by_char;
143         } else {
144                 archive_handle->src_fd = bb_xopen(argv[optind++], O_RDONLY);
145         }
146
147         if ((base_dir) && (chdir(base_dir))) {
148                 bb_perror_msg_and_die("Couldnt chdir");
149         }
150
151         while (optind < argc) {
152                 archive_handle->filter = filter_accept_list;
153                 archive_handle->accept = llist_add_to(archive_handle->accept, argv[optind]);
154                 optind++;
155         }
156
157         while (1) {
158                 unsigned int magic;
159                 int dst_fd;
160
161                 /* TODO Endian issues */
162                 archive_xread_all(archive_handle, &magic, 4);
163                 archive_handle->offset += 4;
164
165                 if (magic == ZIP_CDS_MAGIC) {
166                         break;
167                 }
168                 else if (magic != ZIP_FILEHEADER_MAGIC) {
169                         bb_error_msg_and_die("Invlaide zip magic");
170                 }
171
172                 /* Read the file header */
173                 archive_xread_all(archive_handle, zip_header.raw, 26);
174                 archive_handle->offset += 26;
175                 archive_handle->file_header->mode = S_IFREG | 0777;
176
177                 if (zip_header.formated.method != 8) {
178                         bb_error_msg_and_die("Unsupported compression method %d\n", zip_header.formated.method);
179                 }
180
181                 /* Read filename */
182                 archive_handle->file_header->name = xmalloc(zip_header.formated.filename_len + 1);
183                 archive_xread_all(archive_handle, archive_handle->file_header->name, zip_header.formated.filename_len);
184                 archive_handle->offset += zip_header.formated.filename_len;
185                 archive_handle->file_header->name[zip_header.formated.filename_len] = '\0';
186
187                 /* Skip extra header bits */
188                 archive_handle->file_header->size = zip_header.formated.extra_len;
189                 data_skip(archive_handle);
190                 archive_handle->offset += zip_header.formated.extra_len;
191
192                 /* Handle directories */
193                 archive_handle->file_header->mode = S_IFREG | 0777;
194                 if (last_char_is(archive_handle->file_header->name, '/')) {
195                         archive_handle->file_header->mode ^= S_IFREG;
196                         archive_handle->file_header->mode |= S_IFDIR;
197                 }
198
199                 /* Data section */
200                 archive_handle->file_header->size = zip_header.formated.cmpsize;
201                 if (archive_handle->action_data) {
202                         archive_handle->action_data(archive_handle);
203                 } else {
204                         dst_fd = bb_xopen(archive_handle->file_header->name, O_WRONLY | O_CREAT);
205                         inflate_init(zip_header.formated.cmpsize);
206                         inflate_unzip(archive_handle->src_fd, dst_fd);
207                         close(dst_fd);
208                         chmod(archive_handle->file_header->name, archive_handle->file_header->mode);
209
210                         /* Validate decompression - crc */
211                         if (zip_header.formated.crc32 != (gunzip_crc ^ 0xffffffffL)) {
212                                 bb_error_msg("Invalid compressed data--crc error");
213                         }
214
215                         /* Validate decompression - size */
216                         if (gunzip_bytes_out != zip_header.formated.ucmpsize) {
217                                 bb_error_msg("Invalid compressed data--length error");
218                         }
219                 }
220
221                 /* local file descriptor section */
222                 archive_handle->offset += zip_header.formated.cmpsize;
223                 /* This ISNT unix time */
224                 archive_handle->file_header->mtime = zip_header.formated.modtime | (zip_header.formated.moddate << 16);
225                 archive_handle->file_header->size = zip_header.formated.ucmpsize;
226                 total_size += archive_handle->file_header->size;
227                 total_entries++;
228
229                 archive_handle->action_header(archive_handle->file_header);
230
231                 /* Data descriptor section */
232                 if (zip_header.formated.flags & 4) {
233                         /* skip over duplicate crc, compressed size and uncompressed size */
234                         unsigned char data_description[12];
235                         archive_xread_all(archive_handle, data_description, 12);
236                         archive_handle->offset += 12;
237                 }
238         }
239         /* Central directory section */
240
241         if (archive_handle->action_header == header_verbose_list_unzip) {
242                 printf(" --------                   -------\n");
243                 printf("%9d                   %d files\n", total_size, total_entries);
244         }
245
246         return(EXIT_SUCCESS);
247 }