Glenn L McGrath | 87ac702 | 2002-01-02 13:52:26 +0000 | [diff] [blame] | 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 | |
Glenn L McGrath | f34b0e9 | 2004-06-06 10:22:43 +0000 | [diff] [blame] | 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 | */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 27 | |
| 28 | /* TODO Endian issues, exclude, should we accept input from stdin ? */ |
| 29 | |
Glenn L McGrath | 87ac702 | 2002-01-02 13:52:26 +0000 | [diff] [blame] | 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 | |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 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 | |
Glenn L McGrath | 87ac702 | 2002-01-02 13:52:26 +0000 | [diff] [blame] | 71 | extern int unzip_main(int argc, char **argv) |
| 72 | { |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 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; |
Glenn L McGrath | 87ac702 | 2002-01-02 13:52:26 +0000 | [diff] [blame] | 88 | |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 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) { |
Glenn L McGrath | 87ac702 | 2002-01-02 13:52:26 +0000 | [diff] [blame] | 101 | switch (opt) { |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 102 | case 'l': /* list */ |
| 103 | archive_handle->action_header = header_verbose_list_unzip; |
| 104 | archive_handle->action_data = data_skip; |
Glenn L McGrath | 87ac702 | 2002-01-02 13:52:26 +0000 | [diff] [blame] | 105 | break; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 106 | case 'n': /* never overwright existing files */ |
Glenn L McGrath | 87ac702 | 2002-01-02 13:52:26 +0000 | [diff] [blame] | 107 | break; |
| 108 | case 'o': |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 109 | archive_handle->flags = ARCHIVE_EXTRACT_UNCONDITIONAL; |
Glenn L McGrath | 87ac702 | 2002-01-02 13:52:26 +0000 | [diff] [blame] | 110 | break; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 111 | case 'p': /* extract files to stdout */ |
| 112 | archive_handle->action_data = data_extract_to_stdout; |
Glenn L McGrath | 87ac702 | 2002-01-02 13:52:26 +0000 | [diff] [blame] | 113 | break; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 114 | case 'q': /* Extract files quietly */ |
| 115 | archive_handle->action_header = header_skip; |
Glenn L McGrath | 87ac702 | 2002-01-02 13:52:26 +0000 | [diff] [blame] | 116 | break; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 117 | case 'd': /* Extract files to specified base directory*/ |
| 118 | base_dir = optarg; |
Glenn L McGrath | 87ac702 | 2002-01-02 13:52:26 +0000 | [diff] [blame] | 119 | break; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 120 | #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 | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 126 | bb_show_usage(); |
Glenn L McGrath | 87ac702 | 2002-01-02 13:52:26 +0000 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 130 | if (argc == optind) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 131 | bb_show_usage(); |
Glenn L McGrath | 87ac702 | 2002-01-02 13:52:26 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 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] == '-') { |
Eric Andersen | 70060d2 | 2004-03-27 10:02:48 +0000 | [diff] [blame] | 141 | archive_handle->src_fd = STDIN_FILENO; |
Glenn L McGrath | 237ae42 | 2002-11-03 14:05:15 +0000 | [diff] [blame] | 142 | archive_handle->seek = seek_by_char; |
| 143 | } else { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 144 | archive_handle->src_fd = bb_xopen(argv[optind++], O_RDONLY); |
Glenn L McGrath | 237ae42 | 2002-11-03 14:05:15 +0000 | [diff] [blame] | 145 | } |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 146 | |
| 147 | if ((base_dir) && (chdir(base_dir))) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 148 | bb_perror_msg_and_die("Couldnt chdir"); |
Glenn L McGrath | 237ae42 | 2002-11-03 14:05:15 +0000 | [diff] [blame] | 149 | } |
Glenn L McGrath | 87ac702 | 2002-01-02 13:52:26 +0000 | [diff] [blame] | 150 | |
| 151 | while (optind < argc) { |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 152 | archive_handle->filter = filter_accept_list; |
Glenn L McGrath | 66125c8 | 2002-12-08 00:54:33 +0000 | [diff] [blame] | 153 | archive_handle->accept = llist_add_to(archive_handle->accept, argv[optind]); |
Glenn L McGrath | 87ac702 | 2002-01-02 13:52:26 +0000 | [diff] [blame] | 154 | optind++; |
| 155 | } |
| 156 | |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 157 | while (1) { |
| 158 | unsigned int magic; |
| 159 | int dst_fd; |
| 160 | |
| 161 | /* TODO Endian issues */ |
Glenn L McGrath | 237ae42 | 2002-11-03 14:05:15 +0000 | [diff] [blame] | 162 | archive_xread_all(archive_handle, &magic, 4); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 163 | archive_handle->offset += 4; |
| 164 | |
| 165 | if (magic == ZIP_CDS_MAGIC) { |
| 166 | break; |
| 167 | } |
| 168 | else if (magic != ZIP_FILEHEADER_MAGIC) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 169 | bb_error_msg_and_die("Invlaide zip magic"); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | /* Read the file header */ |
Glenn L McGrath | 237ae42 | 2002-11-03 14:05:15 +0000 | [diff] [blame] | 173 | archive_xread_all(archive_handle, zip_header.raw, 26); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 174 | archive_handle->offset += 26; |
| 175 | archive_handle->file_header->mode = S_IFREG | 0777; |
| 176 | |
| 177 | if (zip_header.formated.method != 8) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 178 | bb_error_msg_and_die("Unsupported compression method %d\n", zip_header.formated.method); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | /* Read filename */ |
| 182 | archive_handle->file_header->name = xmalloc(zip_header.formated.filename_len + 1); |
Glenn L McGrath | 237ae42 | 2002-11-03 14:05:15 +0000 | [diff] [blame] | 183 | archive_xread_all(archive_handle, archive_handle->file_header->name, zip_header.formated.filename_len); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 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 { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 204 | dst_fd = bb_xopen(archive_handle->file_header->name, O_WRONLY | O_CREAT); |
Glenn L McGrath | 5699b85 | 2003-11-15 23:19:05 +0000 | [diff] [blame] | 205 | inflate_init(zip_header.formated.cmpsize); |
| 206 | inflate_unzip(archive_handle->src_fd, dst_fd); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 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)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 212 | bb_error_msg("Invalid compressed data--crc error"); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | /* Validate decompression - size */ |
| 216 | if (gunzip_bytes_out != zip_header.formated.ucmpsize) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 217 | bb_error_msg("Invalid compressed data--length error"); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 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 */ |
Glenn L McGrath | 5699b85 | 2003-11-15 23:19:05 +0000 | [diff] [blame] | 234 | unsigned char data_description[12]; |
| 235 | archive_xread_all(archive_handle, data_description, 12); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 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); |
Glenn L McGrath | 87ac702 | 2002-01-02 13:52:26 +0000 | [diff] [blame] | 247 | } |