1 /* vi: set sw=4 ts=4: */
3 * Mini unzip implementation for busybox
5 * Copyright (C) 2001 by Laurence Anderson
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.
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.
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
24 * http://www.pkware.com/products/enterprise/white_papers/appnote.txt
25 * http://www.info-zip.org/pub/infozip/doc/appnote-iz-latest.zip
28 /* TODO Endian issues, exclude, should we accept input from stdin ? */
35 #include "unarchive.h"
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
43 extern unsigned int gunzip_crc;
44 extern unsigned int gunzip_bytes_out;
46 static void header_list_unzip(const file_header_t *file_header)
48 printf(" inflating: %s\n", file_header->name);
51 static void header_verbose_list_unzip(const file_header_t *file_header)
53 unsigned int dostime = (unsigned int) file_header->mtime;
55 /* can printf arguments cut of the decade component ? */
56 unsigned short year = 1980 + ((dostime & 0xfe000000) >> 25);
61 printf("%9u %02u-%02u-%02u %02u:%02u %s\n",
62 (unsigned int) file_header->size,
63 (dostime & 0x01e00000) >> 21,
64 (dostime & 0x001f0000) >> 16,
66 (dostime & 0x0000f800) >> 11,
67 (dostime & 0x000007e0) >> 5,
71 extern int unzip_main(int argc, char **argv)
74 unsigned char raw[26];
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));
89 archive_handle_t *archive_handle;
90 unsigned int total_size = 0;
91 unsigned int total_entries = 0;
92 char *base_dir = NULL;
96 archive_handle = init_handle();
97 archive_handle->action_data = NULL;
98 archive_handle->action_header = header_list_unzip;
100 while ((opt = getopt(argc, argv, "lnopqd:")) != -1) {
103 archive_handle->action_header = header_verbose_list_unzip;
104 archive_handle->action_data = data_skip;
106 case 'n': /* never overwright existing files */
109 archive_handle->flags = ARCHIVE_EXTRACT_UNCONDITIONAL;
111 case 'p': /* extract files to stdout */
112 archive_handle->action_data = data_extract_to_stdout;
114 case 'q': /* Extract files quietly */
115 archive_handle->action_header = header_skip;
117 case 'd': /* Extract files to specified base directory*/
121 case 'x': /* Exclude the specified files */
122 archive_handle->filter = filter_accept_reject_list;
130 if (argc == optind) {
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");
140 if (*argv[optind] == '-') {
141 archive_handle->src_fd = STDIN_FILENO;
142 archive_handle->seek = seek_by_char;
144 archive_handle->src_fd = bb_xopen(argv[optind++], O_RDONLY);
147 if ((base_dir) && (chdir(base_dir))) {
148 bb_perror_msg_and_die("Couldnt chdir");
151 while (optind < argc) {
152 archive_handle->filter = filter_accept_list;
153 archive_handle->accept = llist_add_to(archive_handle->accept, argv[optind]);
161 /* TODO Endian issues */
162 archive_xread_all(archive_handle, &magic, 4);
163 archive_handle->offset += 4;
165 if (magic == ZIP_CDS_MAGIC) {
168 else if (magic != ZIP_FILEHEADER_MAGIC) {
169 bb_error_msg_and_die("Invlaide zip magic");
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;
177 if (zip_header.formated.method != 8) {
178 bb_error_msg_and_die("Unsupported compression method %d\n", zip_header.formated.method);
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';
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;
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;
200 archive_handle->file_header->size = zip_header.formated.cmpsize;
201 if (archive_handle->action_data) {
202 archive_handle->action_data(archive_handle);
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);
208 chmod(archive_handle->file_header->name, archive_handle->file_header->mode);
210 /* Validate decompression - crc */
211 if (zip_header.formated.crc32 != (gunzip_crc ^ 0xffffffffL)) {
212 bb_error_msg("Invalid compressed data--crc error");
215 /* Validate decompression - size */
216 if (gunzip_bytes_out != zip_header.formated.ucmpsize) {
217 bb_error_msg("Invalid compressed data--length error");
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;
229 archive_handle->action_header(archive_handle->file_header);
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;
239 /* Central directory section */
241 if (archive_handle->action_header == header_verbose_list_unzip) {
242 printf(" -------- -------\n");
243 printf("%9d %d files\n", total_size, total_entries);
246 return(EXIT_SUCCESS);