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 | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 23 | /* For reference to format see http://www.pkware.com/support/appnote.html */ |
| 24 | |
| 25 | /* TODO Endian issues, exclude, should we accept input from stdin ? */ |
| 26 | |
Glenn L McGrath | 87ac702 | 2002-01-02 13:52:26 +0000 | [diff] [blame] | 27 | #include <fcntl.h> |
| 28 | #include <getopt.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <string.h> |
| 31 | #include <unistd.h> |
| 32 | #include "unarchive.h" |
| 33 | #include "busybox.h" |
| 34 | |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 35 | #define ZIP_FILEHEADER_MAGIC 0x04034b50 |
| 36 | #define ZIP_CDS_MAGIC 0x02014b50 |
| 37 | #define ZIP_CDS_END_MAGIC 0x06054b50 |
| 38 | #define ZIP_DD_MAGIC 0x08074b50 |
| 39 | |
| 40 | extern unsigned int gunzip_crc; |
| 41 | extern unsigned int gunzip_bytes_out; |
| 42 | |
| 43 | static void header_list_unzip(const file_header_t *file_header) |
| 44 | { |
| 45 | printf(" inflating: %s\n", file_header->name); |
| 46 | } |
| 47 | |
| 48 | static void header_verbose_list_unzip(const file_header_t *file_header) |
| 49 | { |
| 50 | unsigned int dostime = (unsigned int) file_header->mtime; |
| 51 | |
| 52 | /* can printf arguments cut of the decade component ? */ |
| 53 | unsigned short year = 1980 + ((dostime & 0xfe000000) >> 25); |
| 54 | while (year >= 100) { |
| 55 | year -= 100; |
| 56 | } |
| 57 | |
| 58 | printf("%9u %02u-%02u-%02u %02u:%02u %s\n", |
| 59 | (unsigned int) file_header->size, |
| 60 | (dostime & 0x01e00000) >> 21, |
| 61 | (dostime & 0x001f0000) >> 16, |
| 62 | year, |
| 63 | (dostime & 0x0000f800) >> 11, |
| 64 | (dostime & 0x000007e0) >> 5, |
| 65 | file_header->name); |
| 66 | } |
| 67 | |
Glenn L McGrath | 87ac702 | 2002-01-02 13:52:26 +0000 | [diff] [blame] | 68 | extern int unzip_main(int argc, char **argv) |
| 69 | { |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 70 | union { |
| 71 | unsigned char raw[26]; |
| 72 | struct { |
| 73 | unsigned short version; /* 0-1 */ |
| 74 | unsigned short flags; /* 2-3 */ |
| 75 | unsigned short method; /* 4-5 */ |
| 76 | unsigned short modtime; /* 6-7 */ |
| 77 | unsigned short moddate; /* 8-9 */ |
| 78 | unsigned int crc32 __attribute__ ((packed)); /* 10-13 */ |
| 79 | unsigned int cmpsize __attribute__ ((packed));; /* 14-17 */ |
| 80 | unsigned int ucmpsize __attribute__ ((packed));; /* 18-21 */ |
| 81 | unsigned short filename_len; /* 22-23 */ |
| 82 | unsigned short extra_len; /* 24-25 */ |
| 83 | } formated __attribute__ ((packed)); |
| 84 | } zip_header; |
Glenn L McGrath | 87ac702 | 2002-01-02 13:52:26 +0000 | [diff] [blame] | 85 | |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 86 | archive_handle_t *archive_handle; |
| 87 | unsigned int total_size = 0; |
| 88 | unsigned int total_entries = 0; |
| 89 | char *base_dir = NULL; |
| 90 | int opt = 0; |
| 91 | |
| 92 | /* Initialise */ |
| 93 | archive_handle = init_handle(); |
| 94 | archive_handle->action_data = NULL; |
| 95 | archive_handle->action_header = header_list_unzip; |
| 96 | |
| 97 | while ((opt = getopt(argc, argv, "lnopqd:")) != -1) { |
Glenn L McGrath | 87ac702 | 2002-01-02 13:52:26 +0000 | [diff] [blame] | 98 | switch (opt) { |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 99 | case 'l': /* list */ |
| 100 | archive_handle->action_header = header_verbose_list_unzip; |
| 101 | archive_handle->action_data = data_skip; |
Glenn L McGrath | 87ac702 | 2002-01-02 13:52:26 +0000 | [diff] [blame] | 102 | break; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 103 | case 'n': /* never overwright existing files */ |
Glenn L McGrath | 87ac702 | 2002-01-02 13:52:26 +0000 | [diff] [blame] | 104 | break; |
| 105 | case 'o': |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 106 | archive_handle->flags = ARCHIVE_EXTRACT_UNCONDITIONAL; |
Glenn L McGrath | 87ac702 | 2002-01-02 13:52:26 +0000 | [diff] [blame] | 107 | break; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 108 | case 'p': /* extract files to stdout */ |
| 109 | archive_handle->action_data = data_extract_to_stdout; |
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 'q': /* Extract files quietly */ |
| 112 | archive_handle->action_header = header_skip; |
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 'd': /* Extract files to specified base directory*/ |
| 115 | base_dir = optarg; |
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 | #if 0 |
| 118 | case 'x': /* Exclude the specified files */ |
| 119 | archive_handle->filter = filter_accept_reject_list; |
| 120 | break; |
| 121 | #endif |
| 122 | default: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 123 | bb_show_usage(); |
Glenn L McGrath | 87ac702 | 2002-01-02 13:52:26 +0000 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 127 | if (argc == optind) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 128 | bb_show_usage(); |
Glenn L McGrath | 87ac702 | 2002-01-02 13:52:26 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 131 | printf("Archive: %s\n", argv[optind]); |
| 132 | if (archive_handle->action_header == header_verbose_list_unzip) { |
| 133 | printf(" Length Date Time Name\n"); |
| 134 | printf(" -------- ---- ---- ----\n"); |
| 135 | } |
| 136 | |
| 137 | if (*argv[optind] == '-') { |
| 138 | archive_handle->src_fd = fileno(stdin); |
Glenn L McGrath | 237ae42 | 2002-11-03 14:05:15 +0000 | [diff] [blame] | 139 | archive_handle->seek = seek_by_char; |
| 140 | } else { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 141 | archive_handle->src_fd = bb_xopen(argv[optind++], O_RDONLY); |
Glenn L McGrath | 237ae42 | 2002-11-03 14:05:15 +0000 | [diff] [blame] | 142 | } |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 143 | |
| 144 | if ((base_dir) && (chdir(base_dir))) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 145 | bb_perror_msg_and_die("Couldnt chdir"); |
Glenn L McGrath | 237ae42 | 2002-11-03 14:05:15 +0000 | [diff] [blame] | 146 | } |
Glenn L McGrath | 87ac702 | 2002-01-02 13:52:26 +0000 | [diff] [blame] | 147 | |
| 148 | while (optind < argc) { |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 149 | archive_handle->filter = filter_accept_list; |
Glenn L McGrath | 66125c8 | 2002-12-08 00:54:33 +0000 | [diff] [blame] | 150 | archive_handle->accept = llist_add_to(archive_handle->accept, argv[optind]); |
Glenn L McGrath | 87ac702 | 2002-01-02 13:52:26 +0000 | [diff] [blame] | 151 | optind++; |
| 152 | } |
| 153 | |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 154 | while (1) { |
| 155 | unsigned int magic; |
| 156 | int dst_fd; |
| 157 | |
| 158 | /* TODO Endian issues */ |
Glenn L McGrath | 237ae42 | 2002-11-03 14:05:15 +0000 | [diff] [blame] | 159 | archive_xread_all(archive_handle, &magic, 4); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 160 | archive_handle->offset += 4; |
| 161 | |
| 162 | if (magic == ZIP_CDS_MAGIC) { |
| 163 | break; |
| 164 | } |
| 165 | else if (magic != ZIP_FILEHEADER_MAGIC) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 166 | bb_error_msg_and_die("Invlaide zip magic"); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | /* Read the file header */ |
Glenn L McGrath | 237ae42 | 2002-11-03 14:05:15 +0000 | [diff] [blame] | 170 | archive_xread_all(archive_handle, zip_header.raw, 26); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 171 | archive_handle->offset += 26; |
| 172 | archive_handle->file_header->mode = S_IFREG | 0777; |
| 173 | |
| 174 | if (zip_header.formated.method != 8) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 175 | 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] | 176 | } |
| 177 | |
| 178 | /* Read filename */ |
| 179 | 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] | 180 | 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] | 181 | archive_handle->offset += zip_header.formated.filename_len; |
| 182 | archive_handle->file_header->name[zip_header.formated.filename_len] = '\0'; |
| 183 | |
| 184 | /* Skip extra header bits */ |
| 185 | archive_handle->file_header->size = zip_header.formated.extra_len; |
| 186 | data_skip(archive_handle); |
| 187 | archive_handle->offset += zip_header.formated.extra_len; |
| 188 | |
| 189 | /* Handle directories */ |
| 190 | archive_handle->file_header->mode = S_IFREG | 0777; |
| 191 | if (last_char_is(archive_handle->file_header->name, '/')) { |
| 192 | archive_handle->file_header->mode ^= S_IFREG; |
| 193 | archive_handle->file_header->mode |= S_IFDIR; |
| 194 | } |
| 195 | |
| 196 | /* Data section */ |
| 197 | archive_handle->file_header->size = zip_header.formated.cmpsize; |
| 198 | if (archive_handle->action_data) { |
| 199 | archive_handle->action_data(archive_handle); |
| 200 | } else { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 201 | dst_fd = bb_xopen(archive_handle->file_header->name, O_WRONLY | O_CREAT); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 202 | inflate(archive_handle->src_fd, dst_fd); |
| 203 | close(dst_fd); |
| 204 | chmod(archive_handle->file_header->name, archive_handle->file_header->mode); |
| 205 | |
| 206 | /* Validate decompression - crc */ |
| 207 | if (zip_header.formated.crc32 != (gunzip_crc ^ 0xffffffffL)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 208 | bb_error_msg("Invalid compressed data--crc error"); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | /* Validate decompression - size */ |
| 212 | if (gunzip_bytes_out != zip_header.formated.ucmpsize) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 213 | bb_error_msg("Invalid compressed data--length error"); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | |
| 217 | /* local file descriptor section */ |
| 218 | archive_handle->offset += zip_header.formated.cmpsize; |
| 219 | /* This ISNT unix time */ |
| 220 | archive_handle->file_header->mtime = zip_header.formated.modtime | (zip_header.formated.moddate << 16); |
| 221 | archive_handle->file_header->size = zip_header.formated.ucmpsize; |
| 222 | total_size += archive_handle->file_header->size; |
| 223 | total_entries++; |
| 224 | |
| 225 | archive_handle->action_header(archive_handle->file_header); |
| 226 | |
| 227 | /* Data descriptor section */ |
| 228 | if (zip_header.formated.flags & 4) { |
| 229 | /* skip over duplicate crc, compressed size and uncompressed size */ |
| 230 | unsigned short i; |
| 231 | for (i = 0; i != 12; i++) { |
Glenn L McGrath | 237ae42 | 2002-11-03 14:05:15 +0000 | [diff] [blame] | 232 | archive_xread_char(archive_handle); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 233 | } |
| 234 | archive_handle->offset += 12; |
| 235 | } |
| 236 | } |
| 237 | /* Central directory section */ |
| 238 | |
| 239 | if (archive_handle->action_header == header_verbose_list_unzip) { |
| 240 | printf(" -------- -------\n"); |
| 241 | printf("%9d %d files\n", total_size, total_entries); |
| 242 | } |
| 243 | |
| 244 | return(EXIT_SUCCESS); |
Glenn L McGrath | 87ac702 | 2002-01-02 13:52:26 +0000 | [diff] [blame] | 245 | } |