blob: eea2f5438b67563a7b8371835dc78c70e89df3fb [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 *
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
Glenn L McGrathf34b0e92004-06-06 10:22:43 +000023/* 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 */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000027
28/* TODO Endian issues, exclude, should we accept input from stdin ? */
29
Glenn L McGrath87ac7022002-01-02 13:52:26 +000030#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
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000038#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
43extern unsigned int gunzip_crc;
44extern unsigned int gunzip_bytes_out;
45
46static void header_list_unzip(const file_header_t *file_header)
47{
48 printf(" inflating: %s\n", file_header->name);
49}
50
51static 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
Glenn L McGrath87ac7022002-01-02 13:52:26 +000071extern int unzip_main(int argc, char **argv)
72{
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000073 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;
Glenn L McGrath87ac7022002-01-02 13:52:26 +000088
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000089 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) {
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000101 switch (opt) {
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000102 case 'l': /* list */
103 archive_handle->action_header = header_verbose_list_unzip;
104 archive_handle->action_data = data_skip;
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000105 break;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000106 case 'n': /* never overwright existing files */
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000107 break;
108 case 'o':
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000109 archive_handle->flags = ARCHIVE_EXTRACT_UNCONDITIONAL;
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000110 break;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000111 case 'p': /* extract files to stdout */
112 archive_handle->action_data = data_extract_to_stdout;
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000113 break;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000114 case 'q': /* Extract files quietly */
115 archive_handle->action_header = header_skip;
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000116 break;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000117 case 'd': /* Extract files to specified base directory*/
118 base_dir = optarg;
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000119 break;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000120#if 0
121 case 'x': /* Exclude the specified files */
122 archive_handle->filter = filter_accept_reject_list;
123 break;
124#endif
125 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000126 bb_show_usage();
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000127 }
128 }
129
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000130 if (argc == optind) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000131 bb_show_usage();
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000132 }
133
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000134 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] == '-') {
Eric Andersen70060d22004-03-27 10:02:48 +0000141 archive_handle->src_fd = STDIN_FILENO;
Glenn L McGrath237ae422002-11-03 14:05:15 +0000142 archive_handle->seek = seek_by_char;
143 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000144 archive_handle->src_fd = bb_xopen(argv[optind++], O_RDONLY);
Glenn L McGrath237ae422002-11-03 14:05:15 +0000145 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000146
147 if ((base_dir) && (chdir(base_dir))) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000148 bb_perror_msg_and_die("Couldnt chdir");
Glenn L McGrath237ae422002-11-03 14:05:15 +0000149 }
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000150
151 while (optind < argc) {
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000152 archive_handle->filter = filter_accept_list;
Glenn L McGrath66125c82002-12-08 00:54:33 +0000153 archive_handle->accept = llist_add_to(archive_handle->accept, argv[optind]);
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000154 optind++;
155 }
156
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000157 while (1) {
158 unsigned int magic;
159 int dst_fd;
160
161 /* TODO Endian issues */
Glenn L McGrath237ae422002-11-03 14:05:15 +0000162 archive_xread_all(archive_handle, &magic, 4);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000163 archive_handle->offset += 4;
164
165 if (magic == ZIP_CDS_MAGIC) {
166 break;
167 }
168 else if (magic != ZIP_FILEHEADER_MAGIC) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000169 bb_error_msg_and_die("Invlaide zip magic");
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000170 }
171
172 /* Read the file header */
Glenn L McGrath237ae422002-11-03 14:05:15 +0000173 archive_xread_all(archive_handle, zip_header.raw, 26);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000174 archive_handle->offset += 26;
175 archive_handle->file_header->mode = S_IFREG | 0777;
176
177 if (zip_header.formated.method != 8) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000178 bb_error_msg_and_die("Unsupported compression method %d\n", zip_header.formated.method);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000179 }
180
181 /* Read filename */
182 archive_handle->file_header->name = xmalloc(zip_header.formated.filename_len + 1);
Glenn L McGrath237ae422002-11-03 14:05:15 +0000183 archive_xread_all(archive_handle, archive_handle->file_header->name, zip_header.formated.filename_len);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000184 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 {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000204 dst_fd = bb_xopen(archive_handle->file_header->name, O_WRONLY | O_CREAT);
Glenn L McGrath5699b852003-11-15 23:19:05 +0000205 inflate_init(zip_header.formated.cmpsize);
206 inflate_unzip(archive_handle->src_fd, dst_fd);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000207 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)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000212 bb_error_msg("Invalid compressed data--crc error");
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000213 }
214
215 /* Validate decompression - size */
216 if (gunzip_bytes_out != zip_header.formated.ucmpsize) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000217 bb_error_msg("Invalid compressed data--length error");
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000218 }
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 */
Glenn L McGrath5699b852003-11-15 23:19:05 +0000234 unsigned char data_description[12];
235 archive_xread_all(archive_handle, data_description, 12);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000236 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);
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000247}