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 | * |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 5 | * Copyright (C) 2004 by Ed Clark |
| 6 | * |
| 7 | * Loosely based on original busybox unzip applet by Laurence Anderson. |
| 8 | * All options and features should work in this version. |
Glenn L McGrath | 87ac702 | 2002-01-02 13:52:26 +0000 | [diff] [blame] | 9 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 10 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Glenn L McGrath | 87ac702 | 2002-01-02 13:52:26 +0000 | [diff] [blame] | 11 | */ |
| 12 | |
Glenn L McGrath | f34b0e9 | 2004-06-06 10:22:43 +0000 | [diff] [blame] | 13 | /* For reference see |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 14 | * http://www.pkware.com/company/standards/appnote/ |
Glenn L McGrath | f34b0e9 | 2004-06-06 10:22:43 +0000 | [diff] [blame] | 15 | * http://www.info-zip.org/pub/infozip/doc/appnote-iz-latest.zip |
| 16 | */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 17 | |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 18 | /* TODO |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 19 | * Zip64 + other methods |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 20 | */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 21 | |
Pere Orga | 1f4447b | 2011-03-27 22:40:30 +0200 | [diff] [blame] | 22 | //usage:#define unzip_trivial_usage |
| 23 | //usage: "[-opts[modifiers]] FILE[.zip] [LIST] [-x XLIST] [-d DIR]" |
| 24 | //usage:#define unzip_full_usage "\n\n" |
| 25 | //usage: "Extract files from ZIP archives\n" |
Pere Orga | 1f4447b | 2011-03-27 22:40:30 +0200 | [diff] [blame] | 26 | //usage: "\n -l List archive contents (with -q for short form)" |
| 27 | //usage: "\n -n Never overwrite files (default)" |
| 28 | //usage: "\n -o Overwrite" |
| 29 | //usage: "\n -p Send output to stdout" |
| 30 | //usage: "\n -q Quiet" |
| 31 | //usage: "\n -x XLST Exclude these files" |
| 32 | //usage: "\n -d DIR Extract files into DIR" |
| 33 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 34 | #include "libbb.h" |
Denys Vlasenko | d184a72 | 2011-09-22 12:45:14 +0200 | [diff] [blame] | 35 | #include "bb_archive.h" |
Glenn L McGrath | 87ac702 | 2002-01-02 13:52:26 +0000 | [diff] [blame] | 36 | |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 37 | enum { |
| 38 | #if BB_BIG_ENDIAN |
| 39 | ZIP_FILEHEADER_MAGIC = 0x504b0304, |
Denys Vlasenko | 4e8ff73 | 2010-05-24 04:33:02 +0200 | [diff] [blame] | 40 | ZIP_CDF_MAGIC = 0x504b0102, /* central directory's file header */ |
| 41 | ZIP_CDE_MAGIC = 0x504b0506, /* "end of central directory" record */ |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 42 | ZIP_DD_MAGIC = 0x504b0708, |
| 43 | #else |
| 44 | ZIP_FILEHEADER_MAGIC = 0x04034b50, |
Denys Vlasenko | 4e8ff73 | 2010-05-24 04:33:02 +0200 | [diff] [blame] | 45 | ZIP_CDF_MAGIC = 0x02014b50, |
Denis Vlasenko | 006e862 | 2008-09-21 01:01:46 +0000 | [diff] [blame] | 46 | ZIP_CDE_MAGIC = 0x06054b50, |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 47 | ZIP_DD_MAGIC = 0x08074b50, |
| 48 | #endif |
| 49 | }; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 50 | |
Paul Fox | cb98163 | 2007-11-05 23:09:03 +0000 | [diff] [blame] | 51 | #define ZIP_HEADER_LEN 26 |
| 52 | |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 53 | typedef union { |
Paul Fox | cb98163 | 2007-11-05 23:09:03 +0000 | [diff] [blame] | 54 | uint8_t raw[ZIP_HEADER_LEN]; |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 55 | struct { |
Denis Vlasenko | 006e862 | 2008-09-21 01:01:46 +0000 | [diff] [blame] | 56 | uint16_t version; /* 0-1 */ |
Denys Vlasenko | e98884b | 2010-05-24 04:46:18 +0200 | [diff] [blame] | 57 | uint16_t zip_flags; /* 2-3 */ |
Denis Vlasenko | 006e862 | 2008-09-21 01:01:46 +0000 | [diff] [blame] | 58 | uint16_t method; /* 4-5 */ |
| 59 | uint16_t modtime; /* 6-7 */ |
| 60 | uint16_t moddate; /* 8-9 */ |
| 61 | uint32_t crc32 PACKED; /* 10-13 */ |
| 62 | uint32_t cmpsize PACKED; /* 14-17 */ |
| 63 | uint32_t ucmpsize PACKED; /* 18-21 */ |
| 64 | uint16_t filename_len; /* 22-23 */ |
| 65 | uint16_t extra_len; /* 24-25 */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 66 | } formatted PACKED; |
| 67 | } zip_header_t; /* PACKED - gcc 4.2.1 doesn't like it (spews warning) */ |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 68 | |
Paul Fox | cb98163 | 2007-11-05 23:09:03 +0000 | [diff] [blame] | 69 | /* Check the offset of the last element, not the length. This leniency |
| 70 | * allows for poor packing, whereby the overall struct may be too long, |
| 71 | * even though the elements are all in the right place. |
| 72 | */ |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 73 | struct BUG_zip_header_must_be_26_bytes { |
Paul Fox | cb98163 | 2007-11-05 23:09:03 +0000 | [diff] [blame] | 74 | char BUG_zip_header_must_be_26_bytes[ |
Denis Vlasenko | 006e862 | 2008-09-21 01:01:46 +0000 | [diff] [blame] | 75 | offsetof(zip_header_t, formatted.extra_len) + 2 |
| 76 | == ZIP_HEADER_LEN ? 1 : -1]; |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 77 | }; |
| 78 | |
Denis Vlasenko | 006e862 | 2008-09-21 01:01:46 +0000 | [diff] [blame] | 79 | #define FIX_ENDIANNESS_ZIP(zip_header) do { \ |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 80 | (zip_header).formatted.version = SWAP_LE16((zip_header).formatted.version ); \ |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 81 | (zip_header).formatted.method = SWAP_LE16((zip_header).formatted.method ); \ |
| 82 | (zip_header).formatted.modtime = SWAP_LE16((zip_header).formatted.modtime ); \ |
| 83 | (zip_header).formatted.moddate = SWAP_LE16((zip_header).formatted.moddate ); \ |
| 84 | (zip_header).formatted.crc32 = SWAP_LE32((zip_header).formatted.crc32 ); \ |
| 85 | (zip_header).formatted.cmpsize = SWAP_LE32((zip_header).formatted.cmpsize ); \ |
| 86 | (zip_header).formatted.ucmpsize = SWAP_LE32((zip_header).formatted.ucmpsize ); \ |
| 87 | (zip_header).formatted.filename_len = SWAP_LE16((zip_header).formatted.filename_len); \ |
| 88 | (zip_header).formatted.extra_len = SWAP_LE16((zip_header).formatted.extra_len ); \ |
| 89 | } while (0) |
| 90 | |
Denys Vlasenko | 4e8ff73 | 2010-05-24 04:33:02 +0200 | [diff] [blame] | 91 | #define CDF_HEADER_LEN 42 |
Denis Vlasenko | 006e862 | 2008-09-21 01:01:46 +0000 | [diff] [blame] | 92 | |
| 93 | typedef union { |
Denys Vlasenko | 4e8ff73 | 2010-05-24 04:33:02 +0200 | [diff] [blame] | 94 | uint8_t raw[CDF_HEADER_LEN]; |
Denis Vlasenko | 006e862 | 2008-09-21 01:01:46 +0000 | [diff] [blame] | 95 | struct { |
| 96 | /* uint32_t signature; 50 4b 01 02 */ |
| 97 | uint16_t version_made_by; /* 0-1 */ |
| 98 | uint16_t version_needed; /* 2-3 */ |
Denys Vlasenko | 4e8ff73 | 2010-05-24 04:33:02 +0200 | [diff] [blame] | 99 | uint16_t cdf_flags; /* 4-5 */ |
Denis Vlasenko | 006e862 | 2008-09-21 01:01:46 +0000 | [diff] [blame] | 100 | uint16_t method; /* 6-7 */ |
| 101 | uint16_t mtime; /* 8-9 */ |
| 102 | uint16_t mdate; /* 10-11 */ |
| 103 | uint32_t crc32; /* 12-15 */ |
| 104 | uint32_t cmpsize; /* 16-19 */ |
| 105 | uint32_t ucmpsize; /* 20-23 */ |
| 106 | uint16_t file_name_length; /* 24-25 */ |
| 107 | uint16_t extra_field_length; /* 26-27 */ |
| 108 | uint16_t file_comment_length; /* 28-29 */ |
| 109 | uint16_t disk_number_start; /* 30-31 */ |
| 110 | uint16_t internal_file_attributes; /* 32-33 */ |
| 111 | uint32_t external_file_attributes PACKED; /* 34-37 */ |
| 112 | uint32_t relative_offset_of_local_header PACKED; /* 38-41 */ |
| 113 | } formatted PACKED; |
Denys Vlasenko | 4e8ff73 | 2010-05-24 04:33:02 +0200 | [diff] [blame] | 114 | } cdf_header_t; |
Denis Vlasenko | 006e862 | 2008-09-21 01:01:46 +0000 | [diff] [blame] | 115 | |
Denys Vlasenko | 4e8ff73 | 2010-05-24 04:33:02 +0200 | [diff] [blame] | 116 | struct BUG_cdf_header_must_be_42_bytes { |
| 117 | char BUG_cdf_header_must_be_42_bytes[ |
| 118 | offsetof(cdf_header_t, formatted.relative_offset_of_local_header) + 4 |
| 119 | == CDF_HEADER_LEN ? 1 : -1]; |
Denis Vlasenko | 006e862 | 2008-09-21 01:01:46 +0000 | [diff] [blame] | 120 | }; |
| 121 | |
Denys Vlasenko | 4e8ff73 | 2010-05-24 04:33:02 +0200 | [diff] [blame] | 122 | #define FIX_ENDIANNESS_CDF(cdf_header) do { \ |
| 123 | (cdf_header).formatted.crc32 = SWAP_LE32((cdf_header).formatted.crc32 ); \ |
| 124 | (cdf_header).formatted.cmpsize = SWAP_LE32((cdf_header).formatted.cmpsize ); \ |
| 125 | (cdf_header).formatted.ucmpsize = SWAP_LE32((cdf_header).formatted.ucmpsize ); \ |
| 126 | (cdf_header).formatted.file_name_length = SWAP_LE16((cdf_header).formatted.file_name_length); \ |
| 127 | (cdf_header).formatted.extra_field_length = SWAP_LE16((cdf_header).formatted.extra_field_length); \ |
| 128 | (cdf_header).formatted.file_comment_length = SWAP_LE16((cdf_header).formatted.file_comment_length); \ |
| 129 | IF_DESKTOP( \ |
| 130 | (cdf_header).formatted.version_made_by = SWAP_LE16((cdf_header).formatted.version_made_by); \ |
| 131 | (cdf_header).formatted.external_file_attributes = SWAP_LE32((cdf_header).formatted.external_file_attributes); \ |
| 132 | ) \ |
Denis Vlasenko | 006e862 | 2008-09-21 01:01:46 +0000 | [diff] [blame] | 133 | } while (0) |
| 134 | |
| 135 | #define CDE_HEADER_LEN 16 |
| 136 | |
| 137 | typedef union { |
| 138 | uint8_t raw[CDE_HEADER_LEN]; |
| 139 | struct { |
| 140 | /* uint32_t signature; 50 4b 05 06 */ |
| 141 | uint16_t this_disk_no; |
Denys Vlasenko | 4e8ff73 | 2010-05-24 04:33:02 +0200 | [diff] [blame] | 142 | uint16_t disk_with_cdf_no; |
| 143 | uint16_t cdf_entries_on_this_disk; |
| 144 | uint16_t cdf_entries_total; |
| 145 | uint32_t cdf_size; |
| 146 | uint32_t cdf_offset; |
Denis Vlasenko | 006e862 | 2008-09-21 01:01:46 +0000 | [diff] [blame] | 147 | /* uint16_t file_comment_length; */ |
| 148 | /* .ZIP file comment (variable size) */ |
| 149 | } formatted PACKED; |
| 150 | } cde_header_t; |
| 151 | |
| 152 | struct BUG_cde_header_must_be_16_bytes { |
| 153 | char BUG_cde_header_must_be_16_bytes[ |
| 154 | sizeof(cde_header_t) == CDE_HEADER_LEN ? 1 : -1]; |
| 155 | }; |
| 156 | |
| 157 | #define FIX_ENDIANNESS_CDE(cde_header) do { \ |
Denys Vlasenko | 4e8ff73 | 2010-05-24 04:33:02 +0200 | [diff] [blame] | 158 | (cde_header).formatted.cdf_offset = SWAP_LE32((cde_header).formatted.cdf_offset); \ |
Denis Vlasenko | 006e862 | 2008-09-21 01:01:46 +0000 | [diff] [blame] | 159 | } while (0) |
| 160 | |
| 161 | enum { zip_fd = 3 }; |
| 162 | |
| 163 | |
| 164 | #if ENABLE_DESKTOP |
Dan Fandrich | b76f18d | 2010-06-17 21:39:44 -0700 | [diff] [blame] | 165 | |
| 166 | #define PEEK_FROM_END 16384 |
| 167 | |
Denis Vlasenko | 006e862 | 2008-09-21 01:01:46 +0000 | [diff] [blame] | 168 | /* NB: does not preserve file position! */ |
Denys Vlasenko | 4e8ff73 | 2010-05-24 04:33:02 +0200 | [diff] [blame] | 169 | static uint32_t find_cdf_offset(void) |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 170 | { |
Denis Vlasenko | 006e862 | 2008-09-21 01:01:46 +0000 | [diff] [blame] | 171 | cde_header_t cde_header; |
| 172 | unsigned char *p; |
| 173 | off_t end; |
Dan Fandrich | b76f18d | 2010-06-17 21:39:44 -0700 | [diff] [blame] | 174 | unsigned char *buf = xzalloc(PEEK_FROM_END); |
Denis Vlasenko | 006e862 | 2008-09-21 01:01:46 +0000 | [diff] [blame] | 175 | |
| 176 | end = xlseek(zip_fd, 0, SEEK_END); |
Dan Fandrich | b76f18d | 2010-06-17 21:39:44 -0700 | [diff] [blame] | 177 | end -= PEEK_FROM_END; |
Denys Vlasenko | fc2bb8f | 2010-05-24 13:07:55 +0200 | [diff] [blame] | 178 | if (end < 0) |
| 179 | end = 0; |
Denis Vlasenko | 006e862 | 2008-09-21 01:01:46 +0000 | [diff] [blame] | 180 | xlseek(zip_fd, end, SEEK_SET); |
Dan Fandrich | b76f18d | 2010-06-17 21:39:44 -0700 | [diff] [blame] | 181 | full_read(zip_fd, buf, PEEK_FROM_END); |
Denis Vlasenko | 006e862 | 2008-09-21 01:01:46 +0000 | [diff] [blame] | 182 | |
| 183 | p = buf; |
Dan Fandrich | b76f18d | 2010-06-17 21:39:44 -0700 | [diff] [blame] | 184 | while (p <= buf + PEEK_FROM_END - CDE_HEADER_LEN - 4) { |
Denis Vlasenko | 006e862 | 2008-09-21 01:01:46 +0000 | [diff] [blame] | 185 | if (*p != 'P') { |
| 186 | p++; |
| 187 | continue; |
| 188 | } |
| 189 | if (*++p != 'K') |
| 190 | continue; |
| 191 | if (*++p != 5) |
| 192 | continue; |
| 193 | if (*++p != 6) |
| 194 | continue; |
| 195 | /* we found CDE! */ |
| 196 | memcpy(cde_header.raw, p + 1, CDE_HEADER_LEN); |
| 197 | FIX_ENDIANNESS_CDE(cde_header); |
Dan Fandrich | b76f18d | 2010-06-17 21:39:44 -0700 | [diff] [blame] | 198 | free(buf); |
Denys Vlasenko | 4e8ff73 | 2010-05-24 04:33:02 +0200 | [diff] [blame] | 199 | return cde_header.formatted.cdf_offset; |
Denis Vlasenko | 006e862 | 2008-09-21 01:01:46 +0000 | [diff] [blame] | 200 | } |
Dan Fandrich | b76f18d | 2010-06-17 21:39:44 -0700 | [diff] [blame] | 201 | //free(buf); |
Denis Vlasenko | 006e862 | 2008-09-21 01:01:46 +0000 | [diff] [blame] | 202 | bb_error_msg_and_die("can't find file table"); |
| 203 | }; |
| 204 | |
Denys Vlasenko | 4e8ff73 | 2010-05-24 04:33:02 +0200 | [diff] [blame] | 205 | static uint32_t read_next_cdf(uint32_t cdf_offset, cdf_header_t *cdf_ptr) |
Denis Vlasenko | 006e862 | 2008-09-21 01:01:46 +0000 | [diff] [blame] | 206 | { |
| 207 | off_t org; |
| 208 | |
| 209 | org = xlseek(zip_fd, 0, SEEK_CUR); |
| 210 | |
Denys Vlasenko | 4e8ff73 | 2010-05-24 04:33:02 +0200 | [diff] [blame] | 211 | if (!cdf_offset) |
| 212 | cdf_offset = find_cdf_offset(); |
Denis Vlasenko | 006e862 | 2008-09-21 01:01:46 +0000 | [diff] [blame] | 213 | |
Denys Vlasenko | 4e8ff73 | 2010-05-24 04:33:02 +0200 | [diff] [blame] | 214 | xlseek(zip_fd, cdf_offset + 4, SEEK_SET); |
| 215 | xread(zip_fd, cdf_ptr->raw, CDF_HEADER_LEN); |
| 216 | FIX_ENDIANNESS_CDF(*cdf_ptr); |
| 217 | cdf_offset += 4 + CDF_HEADER_LEN |
| 218 | + cdf_ptr->formatted.file_name_length |
| 219 | + cdf_ptr->formatted.extra_field_length |
| 220 | + cdf_ptr->formatted.file_comment_length; |
Denis Vlasenko | 006e862 | 2008-09-21 01:01:46 +0000 | [diff] [blame] | 221 | |
| 222 | xlseek(zip_fd, org, SEEK_SET); |
Denys Vlasenko | 4e8ff73 | 2010-05-24 04:33:02 +0200 | [diff] [blame] | 223 | return cdf_offset; |
Denis Vlasenko | 006e862 | 2008-09-21 01:01:46 +0000 | [diff] [blame] | 224 | }; |
| 225 | #endif |
| 226 | |
| 227 | static void unzip_skip(off_t skip) |
| 228 | { |
Stefani Seibold | f3b56b4 | 2009-07-25 02:02:22 +0200 | [diff] [blame] | 229 | if (lseek(zip_fd, skip, SEEK_CUR) == (off_t)-1) |
| 230 | bb_copyfd_exact_size(zip_fd, -1, skip); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 233 | static void unzip_create_leading_dirs(const char *fn) |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 234 | { |
| 235 | /* Create all leading directories */ |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 236 | char *name = xstrdup(fn); |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 237 | if (bb_make_directory(dirname(name), 0777, FILEUTILS_RECUR)) { |
Denys Vlasenko | 831756b | 2011-09-09 17:30:55 +0200 | [diff] [blame] | 238 | xfunc_die(); /* bb_make_directory is noisy */ |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 239 | } |
| 240 | free(name); |
| 241 | } |
| 242 | |
Denis Vlasenko | 006e862 | 2008-09-21 01:01:46 +0000 | [diff] [blame] | 243 | static void unzip_extract(zip_header_t *zip_header, int dst_fd) |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 244 | { |
"Robert P. J. Day" | eea5618 | 2006-07-20 19:02:24 +0000 | [diff] [blame] | 245 | if (zip_header->formatted.method == 0) { |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 246 | /* Method 0 - stored (not compressed) */ |
Denis Vlasenko | 7039a66 | 2006-10-08 17:54:47 +0000 | [diff] [blame] | 247 | off_t size = zip_header->formatted.ucmpsize; |
Denis Vlasenko | 714701c | 2006-12-22 00:21:07 +0000 | [diff] [blame] | 248 | if (size) |
Denis Vlasenko | 006e862 | 2008-09-21 01:01:46 +0000 | [diff] [blame] | 249 | bb_copyfd_exact_size(zip_fd, dst_fd, size); |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 250 | } else { |
| 251 | /* Method 8 - inflate */ |
Denys Vlasenko | 8a6a2f9 | 2012-03-06 16:27:48 +0100 | [diff] [blame] | 252 | transformer_aux_data_t aux; |
| 253 | init_transformer_aux_data(&aux); |
| 254 | aux.bytes_in = zip_header->formatted.cmpsize; |
| 255 | if (inflate_unzip(&aux, zip_fd, dst_fd) < 0) |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 256 | bb_error_msg_and_die("inflate error"); |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 257 | /* Validate decompression - crc */ |
Denys Vlasenko | 8a6a2f9 | 2012-03-06 16:27:48 +0100 | [diff] [blame] | 258 | if (zip_header->formatted.crc32 != (aux.crc32 ^ 0xffffffffL)) { |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 259 | bb_error_msg_and_die("crc error"); |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 260 | } |
| 261 | /* Validate decompression - size */ |
Denys Vlasenko | 8a6a2f9 | 2012-03-06 16:27:48 +0100 | [diff] [blame] | 262 | if (zip_header->formatted.ucmpsize != aux.bytes_out) { |
Denis Vlasenko | fcc5696 | 2007-10-19 21:03:09 +0000 | [diff] [blame] | 263 | /* Don't die. Who knows, maybe len calculation |
| 264 | * was botched somewhere. After all, crc matched! */ |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 265 | bb_error_msg("bad length"); |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 266 | } |
| 267 | } |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 270 | int unzip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 271 | int unzip_main(int argc, char **argv) |
Glenn L McGrath | 87ac702 | 2002-01-02 13:52:26 +0000 | [diff] [blame] | 272 | { |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 273 | enum { O_PROMPT, O_NEVER, O_ALWAYS }; |
| 274 | |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 275 | zip_header_t zip_header; |
Denys Vlasenko | 386bc9f | 2009-09-06 16:52:50 +0200 | [diff] [blame] | 276 | smallint quiet = 0; |
| 277 | IF_NOT_DESKTOP(const) smallint verbose = 0; |
Paul Fox | 9382b38 | 2007-09-07 20:28:25 +0000 | [diff] [blame] | 278 | smallint listing = 0; |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 279 | smallint overwrite = O_PROMPT; |
Denis Vlasenko | 006e862 | 2008-09-21 01:01:46 +0000 | [diff] [blame] | 280 | #if ENABLE_DESKTOP |
Denys Vlasenko | 4e8ff73 | 2010-05-24 04:33:02 +0200 | [diff] [blame] | 281 | uint32_t cdf_offset; |
Denis Vlasenko | 006e862 | 2008-09-21 01:01:46 +0000 | [diff] [blame] | 282 | #endif |
Denys Vlasenko | 386bc9f | 2009-09-06 16:52:50 +0200 | [diff] [blame] | 283 | unsigned long total_usize; |
| 284 | unsigned long total_size; |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 285 | unsigned total_entries; |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 286 | int dst_fd = -1; |
| 287 | char *src_fn = NULL; |
| 288 | char *dst_fn = NULL; |
Mike Frysinger | 6902455 | 2005-07-30 07:30:26 +0000 | [diff] [blame] | 289 | llist_t *zaccept = NULL; |
| 290 | llist_t *zreject = NULL; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 291 | char *base_dir = NULL; |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 292 | int i, opt; |
| 293 | int opt_range = 0; |
| 294 | char key_buf[80]; |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 295 | struct stat stat_buf; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 296 | |
Denys Vlasenko | 386bc9f | 2009-09-06 16:52:50 +0200 | [diff] [blame] | 297 | /* -q, -l and -v: UnZip 5.52 of 28 February 2005, by Info-ZIP: |
| 298 | * |
| 299 | * # /usr/bin/unzip -qq -v decompress_unlzma.i.zip |
| 300 | * 204372 Defl:N 35278 83% 09-06-09 14:23 0d056252 decompress_unlzma.i |
| 301 | * # /usr/bin/unzip -q -v decompress_unlzma.i.zip |
| 302 | * Length Method Size Ratio Date Time CRC-32 Name |
| 303 | * -------- ------ ------- ----- ---- ---- ------ ---- |
| 304 | * 204372 Defl:N 35278 83% 09-06-09 14:23 0d056252 decompress_unlzma.i |
| 305 | * -------- ------- --- ------- |
| 306 | * 204372 35278 83% 1 file |
| 307 | * # /usr/bin/unzip -v decompress_unlzma.i.zip |
| 308 | * Archive: decompress_unlzma.i.zip |
| 309 | * Length Method Size Ratio Date Time CRC-32 Name |
| 310 | * -------- ------ ------- ----- ---- ---- ------ ---- |
| 311 | * 204372 Defl:N 35278 83% 09-06-09 14:23 0d056252 decompress_unlzma.i |
| 312 | * -------- ------- --- ------- |
| 313 | * 204372 35278 83% 1 file |
| 314 | * # unzip -v decompress_unlzma.i.zip |
| 315 | * Archive: decompress_unlzma.i.zip |
| 316 | * Length Date Time Name |
| 317 | * -------- ---- ---- ---- |
| 318 | * 204372 09-06-09 14:23 decompress_unlzma.i |
| 319 | * -------- ------- |
| 320 | * 204372 1 files |
| 321 | * # /usr/bin/unzip -l -qq decompress_unlzma.i.zip |
| 322 | * 204372 09-06-09 14:23 decompress_unlzma.i |
| 323 | * # /usr/bin/unzip -l -q decompress_unlzma.i.zip |
| 324 | * Length Date Time Name |
| 325 | * -------- ---- ---- ---- |
| 326 | * 204372 09-06-09 14:23 decompress_unlzma.i |
| 327 | * -------- ------- |
| 328 | * 204372 1 file |
| 329 | * # /usr/bin/unzip -l decompress_unlzma.i.zip |
| 330 | * Archive: decompress_unlzma.i.zip |
| 331 | * Length Date Time Name |
| 332 | * -------- ---- ---- ---- |
| 333 | * 204372 09-06-09 14:23 decompress_unlzma.i |
| 334 | * -------- ------- |
| 335 | * 204372 1 file |
| 336 | */ |
| 337 | |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 338 | /* '-' makes getopt return 1 for non-options */ |
Denys Vlasenko | 386bc9f | 2009-09-06 16:52:50 +0200 | [diff] [blame] | 339 | while ((opt = getopt(argc, argv, "-d:lnopqxv")) != -1) { |
Denis Vlasenko | c16bd21 | 2006-09-27 19:51:06 +0000 | [diff] [blame] | 340 | switch (opt_range) { |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 341 | case 0: /* Options */ |
Denis Vlasenko | c16bd21 | 2006-09-27 19:51:06 +0000 | [diff] [blame] | 342 | switch (opt) { |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 343 | case 'l': /* List */ |
Paul Fox | 9382b38 | 2007-09-07 20:28:25 +0000 | [diff] [blame] | 344 | listing = 1; |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 345 | break; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 346 | |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 347 | case 'n': /* Never overwrite existing files */ |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 348 | overwrite = O_NEVER; |
Glenn L McGrath | 87ac702 | 2002-01-02 13:52:26 +0000 | [diff] [blame] | 349 | break; |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 350 | |
| 351 | case 'o': /* Always overwrite existing files */ |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 352 | overwrite = O_ALWAYS; |
Glenn L McGrath | 87ac702 | 2002-01-02 13:52:26 +0000 | [diff] [blame] | 353 | break; |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 354 | |
| 355 | case 'p': /* Extract files to stdout and fall through to set verbosity */ |
| 356 | dst_fd = STDOUT_FILENO; |
| 357 | |
| 358 | case 'q': /* Be quiet */ |
Denys Vlasenko | 386bc9f | 2009-09-06 16:52:50 +0200 | [diff] [blame] | 359 | quiet++; |
| 360 | break; |
| 361 | |
| 362 | case 'v': /* Verbose list */ |
| 363 | IF_DESKTOP(verbose++;) |
| 364 | listing = 1; |
Glenn L McGrath | 87ac702 | 2002-01-02 13:52:26 +0000 | [diff] [blame] | 365 | break; |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 366 | |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 367 | case 1: /* The zip file */ |
| 368 | /* +5: space for ".zip" and NUL */ |
| 369 | src_fn = xmalloc(strlen(optarg) + 5); |
Denis Vlasenko | 666c40c | 2007-03-31 10:17:24 +0000 | [diff] [blame] | 370 | strcpy(src_fn, optarg); |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 371 | opt_range++; |
Glenn L McGrath | 87ac702 | 2002-01-02 13:52:26 +0000 | [diff] [blame] | 372 | break; |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 373 | |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 374 | default: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 375 | bb_show_usage(); |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 376 | } |
| 377 | break; |
| 378 | |
| 379 | case 1: /* Include files */ |
| 380 | if (opt == 1) { |
Rob Landley | 8bb5078 | 2006-05-26 23:44:51 +0000 | [diff] [blame] | 381 | llist_add_to(&zaccept, optarg); |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 382 | break; |
| 383 | } |
| 384 | if (opt == 'd') { |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 385 | base_dir = optarg; |
| 386 | opt_range += 2; |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 387 | break; |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 388 | } |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 389 | if (opt == 'x') { |
| 390 | opt_range++; |
| 391 | break; |
| 392 | } |
| 393 | bb_show_usage(); |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 394 | |
| 395 | case 2 : /* Exclude files */ |
| 396 | if (opt == 1) { |
Rob Landley | 8bb5078 | 2006-05-26 23:44:51 +0000 | [diff] [blame] | 397 | llist_add_to(&zreject, optarg); |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 398 | break; |
| 399 | } |
| 400 | if (opt == 'd') { /* Extract to base directory */ |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 401 | base_dir = optarg; |
| 402 | opt_range++; |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 403 | break; |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 404 | } |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 405 | /* fall through */ |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 406 | |
| 407 | default: |
| 408 | bb_show_usage(); |
Glenn L McGrath | 87ac702 | 2002-01-02 13:52:26 +0000 | [diff] [blame] | 409 | } |
| 410 | } |
| 411 | |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 412 | if (src_fn == NULL) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 413 | bb_show_usage(); |
Glenn L McGrath | 87ac702 | 2002-01-02 13:52:26 +0000 | [diff] [blame] | 414 | } |
| 415 | |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 416 | /* Open input file */ |
Denis Vlasenko | 9f73944 | 2006-12-16 23:49:13 +0000 | [diff] [blame] | 417 | if (LONE_DASH(src_fn)) { |
Denis Vlasenko | 006e862 | 2008-09-21 01:01:46 +0000 | [diff] [blame] | 418 | xdup2(STDIN_FILENO, zip_fd); |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 419 | /* Cannot use prompt mode since zip data is arriving on STDIN */ |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 420 | if (overwrite == O_PROMPT) |
| 421 | overwrite = O_NEVER; |
Glenn L McGrath | 237ae42 | 2002-11-03 14:05:15 +0000 | [diff] [blame] | 422 | } else { |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 423 | static const char extn[][5] = {"", ".zip", ".ZIP"}; |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 424 | int orig_src_fn_len = strlen(src_fn); |
Denis Vlasenko | 006e862 | 2008-09-21 01:01:46 +0000 | [diff] [blame] | 425 | int src_fd = -1; |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 426 | |
Denis Vlasenko | bf0a201 | 2006-12-26 10:42:51 +0000 | [diff] [blame] | 427 | for (i = 0; (i < 3) && (src_fd == -1); i++) { |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 428 | strcpy(src_fn + orig_src_fn_len, extn[i]); |
| 429 | src_fd = open(src_fn, O_RDONLY); |
| 430 | } |
| 431 | if (src_fd == -1) { |
Denis Vlasenko | 666c40c | 2007-03-31 10:17:24 +0000 | [diff] [blame] | 432 | src_fn[orig_src_fn_len] = '\0'; |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 433 | bb_error_msg_and_die("can't open %s, %s.zip, %s.ZIP", src_fn, src_fn, src_fn); |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 434 | } |
Denis Vlasenko | 006e862 | 2008-09-21 01:01:46 +0000 | [diff] [blame] | 435 | xmove_fd(src_fd, zip_fd); |
Glenn L McGrath | 237ae42 | 2002-11-03 14:05:15 +0000 | [diff] [blame] | 436 | } |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 437 | |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 438 | /* Change dir if necessary */ |
Bernhard Reutner-Fischer | d9cf7ac | 2006-04-12 18:39:58 +0000 | [diff] [blame] | 439 | if (base_dir) |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 440 | xchdir(base_dir); |
Glenn L McGrath | 87ac702 | 2002-01-02 13:52:26 +0000 | [diff] [blame] | 441 | |
Denys Vlasenko | 386bc9f | 2009-09-06 16:52:50 +0200 | [diff] [blame] | 442 | if (quiet <= 1) { /* not -qq */ |
| 443 | if (quiet == 0) |
| 444 | printf("Archive: %s\n", src_fn); |
| 445 | if (listing) { |
| 446 | puts(verbose ? |
| 447 | " Length Method Size Ratio Date Time CRC-32 Name\n" |
| 448 | "-------- ------ ------- ----- ---- ---- ------ ----" |
| 449 | : |
| 450 | " Length Date Time Name\n" |
| 451 | " -------- ---- ---- ----" |
| 452 | ); |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 453 | } |
| 454 | } |
Glenn L McGrath | 87ac702 | 2002-01-02 13:52:26 +0000 | [diff] [blame] | 455 | |
Denys Vlasenko | 4e8ff73 | 2010-05-24 04:33:02 +0200 | [diff] [blame] | 456 | /* Example of an archive with one 0-byte long file named 'z' |
| 457 | * created by Zip 2.31 on Unix: |
| 458 | * 0000 [50 4b]03 04 0a 00 00 00 00 00 42 1a b8 3c 00 00 |PK........B..<..| |
| 459 | * sig........ vneed flags compr mtime mdate crc32> |
| 460 | * 0010 00 00 00 00 00 00 00 00 00 00 01 00 15 00 7a 55 |..............zU| |
| 461 | * >..... csize...... usize...... fnlen exlen fn ex> |
| 462 | * 0020 54 09 00 03 cc d3 f9 4b cc d3 f9 4b 55 78 04 00 |T......K...KUx..| |
| 463 | * >tra_field...................................... |
| 464 | * 0030 00 00 00 00[50 4b]01 02 17 03 0a 00 00 00 00 00 |....PK..........| |
| 465 | * ........... sig........ vmade vneed flags compr |
| 466 | * 0040 42 1a b8 3c 00 00 00 00 00 00 00 00 00 00 00 00 |B..<............| |
| 467 | * mtime mdate crc32...... csize...... usize...... |
| 468 | * 0050 01 00 0d 00 00 00 00 00 00 00 00 00 a4 81 00 00 |................| |
| 469 | * fnlen exlen clen. dnum. iattr eattr...... relofs> (eattr = rw-r--r--) |
| 470 | * 0060 00 00 7a 55 54 05 00 03 cc d3 f9 4b 55 78 00 00 |..zUT......KUx..| |
| 471 | * >..... fn extra_field........................... |
| 472 | * 0070 [50 4b]05 06 00 00 00 00 01 00 01 00 3c 00 00 00 |PK..........<...| |
| 473 | * 0080 34 00 00 00 00 00 |4.....| |
| 474 | */ |
Denys Vlasenko | 386bc9f | 2009-09-06 16:52:50 +0200 | [diff] [blame] | 475 | total_usize = 0; |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 476 | total_size = 0; |
| 477 | total_entries = 0; |
Denis Vlasenko | 006e862 | 2008-09-21 01:01:46 +0000 | [diff] [blame] | 478 | #if ENABLE_DESKTOP |
Denys Vlasenko | 4e8ff73 | 2010-05-24 04:33:02 +0200 | [diff] [blame] | 479 | cdf_offset = 0; |
Denis Vlasenko | 006e862 | 2008-09-21 01:01:46 +0000 | [diff] [blame] | 480 | #endif |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 481 | while (1) { |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 482 | uint32_t magic; |
Denys Vlasenko | 4e8ff73 | 2010-05-24 04:33:02 +0200 | [diff] [blame] | 483 | mode_t dir_mode = 0777; |
| 484 | #if ENABLE_DESKTOP |
| 485 | mode_t file_mode = 0666; |
| 486 | #endif |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 487 | |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 488 | /* Check magic number */ |
Denis Vlasenko | 006e862 | 2008-09-21 01:01:46 +0000 | [diff] [blame] | 489 | xread(zip_fd, &magic, 4); |
Denis Vlasenko | 48a9971 | 2008-07-26 17:32:41 +0000 | [diff] [blame] | 490 | /* Central directory? It's at the end, so exit */ |
Denys Vlasenko | 4e8ff73 | 2010-05-24 04:33:02 +0200 | [diff] [blame] | 491 | if (magic == ZIP_CDF_MAGIC) |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 492 | break; |
Denis Vlasenko | 006e862 | 2008-09-21 01:01:46 +0000 | [diff] [blame] | 493 | #if ENABLE_DESKTOP |
| 494 | /* Data descriptor? It was a streaming file, go on */ |
| 495 | if (magic == ZIP_DD_MAGIC) { |
| 496 | /* skip over duplicate crc32, cmpsize and ucmpsize */ |
| 497 | unzip_skip(3 * 4); |
| 498 | continue; |
| 499 | } |
| 500 | #endif |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 501 | if (magic != ZIP_FILEHEADER_MAGIC) |
Denis Vlasenko | 48a9971 | 2008-07-26 17:32:41 +0000 | [diff] [blame] | 502 | bb_error_msg_and_die("invalid zip magic %08X", (int)magic); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 503 | |
| 504 | /* Read the file header */ |
Denis Vlasenko | 006e862 | 2008-09-21 01:01:46 +0000 | [diff] [blame] | 505 | xread(zip_fd, zip_header.raw, ZIP_HEADER_LEN); |
| 506 | FIX_ENDIANNESS_ZIP(zip_header); |
"Robert P. J. Day" | eea5618 | 2006-07-20 19:02:24 +0000 | [diff] [blame] | 507 | if ((zip_header.formatted.method != 0) && (zip_header.formatted.method != 8)) { |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 508 | bb_error_msg_and_die("unsupported method %d", zip_header.formatted.method); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 509 | } |
Denis Vlasenko | 006e862 | 2008-09-21 01:01:46 +0000 | [diff] [blame] | 510 | #if !ENABLE_DESKTOP |
Denys Vlasenko | e98884b | 2010-05-24 04:46:18 +0200 | [diff] [blame] | 511 | if (zip_header.formatted.zip_flags & SWAP_LE16(0x0009)) { |
Denis Vlasenko | 006e862 | 2008-09-21 01:01:46 +0000 | [diff] [blame] | 512 | bb_error_msg_and_die("zip flags 1 and 8 are not supported"); |
| 513 | } |
| 514 | #else |
Denys Vlasenko | e98884b | 2010-05-24 04:46:18 +0200 | [diff] [blame] | 515 | if (zip_header.formatted.zip_flags & SWAP_LE16(0x0001)) { |
Denis Vlasenko | 48a9971 | 2008-07-26 17:32:41 +0000 | [diff] [blame] | 516 | /* 0x0001 - encrypted */ |
Denis Vlasenko | 006e862 | 2008-09-21 01:01:46 +0000 | [diff] [blame] | 517 | bb_error_msg_and_die("zip flag 1 (encryption) is not supported"); |
| 518 | } |
Denys Vlasenko | 4e8ff73 | 2010-05-24 04:33:02 +0200 | [diff] [blame] | 519 | |
| 520 | { |
| 521 | cdf_header_t cdf_header; |
| 522 | cdf_offset = read_next_cdf(cdf_offset, &cdf_header); |
Denys Vlasenko | e98884b | 2010-05-24 04:46:18 +0200 | [diff] [blame] | 523 | if (zip_header.formatted.zip_flags & SWAP_LE16(0x0008)) { |
Denys Vlasenko | 4e8ff73 | 2010-05-24 04:33:02 +0200 | [diff] [blame] | 524 | /* 0x0008 - streaming. [u]cmpsize can be reliably gotten |
| 525 | * only from Central Directory. See unzip_doc.txt */ |
| 526 | zip_header.formatted.crc32 = cdf_header.formatted.crc32; |
| 527 | zip_header.formatted.cmpsize = cdf_header.formatted.cmpsize; |
| 528 | zip_header.formatted.ucmpsize = cdf_header.formatted.ucmpsize; |
| 529 | } |
| 530 | if ((cdf_header.formatted.version_made_by >> 8) == 3) { |
| 531 | /* this archive is created on Unix */ |
| 532 | dir_mode = file_mode = (cdf_header.formatted.external_file_attributes >> 16); |
| 533 | } |
Denis Vlasenko | 48a9971 | 2008-07-26 17:32:41 +0000 | [diff] [blame] | 534 | } |
Denis Vlasenko | 006e862 | 2008-09-21 01:01:46 +0000 | [diff] [blame] | 535 | #endif |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 536 | |
| 537 | /* Read filename */ |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 538 | free(dst_fn); |
"Robert P. J. Day" | eea5618 | 2006-07-20 19:02:24 +0000 | [diff] [blame] | 539 | dst_fn = xzalloc(zip_header.formatted.filename_len + 1); |
Denis Vlasenko | 006e862 | 2008-09-21 01:01:46 +0000 | [diff] [blame] | 540 | xread(zip_fd, dst_fn, zip_header.formatted.filename_len); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 541 | |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 542 | /* Skip extra header bytes */ |
Denis Vlasenko | 006e862 | 2008-09-21 01:01:46 +0000 | [diff] [blame] | 543 | unzip_skip(zip_header.formatted.extra_len); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 544 | |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 545 | /* Filter zip entries */ |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 546 | if (find_list_entry(zreject, dst_fn) |
| 547 | || (zaccept && !find_list_entry(zaccept, dst_fn)) |
| 548 | ) { /* Skip entry */ |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 549 | i = 'n'; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 550 | |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 551 | } else { /* Extract entry */ |
Paul Fox | 9382b38 | 2007-09-07 20:28:25 +0000 | [diff] [blame] | 552 | if (listing) { /* List entry */ |
Denys Vlasenko | 386bc9f | 2009-09-06 16:52:50 +0200 | [diff] [blame] | 553 | unsigned dostime = zip_header.formatted.modtime | (zip_header.formatted.moddate << 16); |
| 554 | if (!verbose) { |
| 555 | // " Length Date Time Name\n" |
| 556 | // " -------- ---- ---- ----" |
| 557 | printf( "%9u %02u-%02u-%02u %02u:%02u %s\n", |
| 558 | (unsigned)zip_header.formatted.ucmpsize, |
| 559 | (dostime & 0x01e00000) >> 21, |
| 560 | (dostime & 0x001f0000) >> 16, |
| 561 | (((dostime & 0xfe000000) >> 25) + 1980) % 100, |
| 562 | (dostime & 0x0000f800) >> 11, |
| 563 | (dostime & 0x000007e0) >> 5, |
| 564 | dst_fn); |
| 565 | total_usize += zip_header.formatted.ucmpsize; |
Denis Vlasenko | feb7ae7 | 2007-10-01 12:05:12 +0000 | [diff] [blame] | 566 | } else { |
Denys Vlasenko | 386bc9f | 2009-09-06 16:52:50 +0200 | [diff] [blame] | 567 | unsigned long percents = zip_header.formatted.ucmpsize - zip_header.formatted.cmpsize; |
| 568 | percents = percents * 100; |
| 569 | if (zip_header.formatted.ucmpsize) |
| 570 | percents /= zip_header.formatted.ucmpsize; |
| 571 | // " Length Method Size Ratio Date Time CRC-32 Name\n" |
| 572 | // "-------- ------ ------- ----- ---- ---- ------ ----" |
| 573 | printf( "%8u Defl:N" "%9u%4u%% %02u-%02u-%02u %02u:%02u %08x %s\n", |
| 574 | (unsigned)zip_header.formatted.ucmpsize, |
| 575 | (unsigned)zip_header.formatted.cmpsize, |
| 576 | (unsigned)percents, |
| 577 | (dostime & 0x01e00000) >> 21, |
| 578 | (dostime & 0x001f0000) >> 16, |
| 579 | (((dostime & 0xfe000000) >> 25) + 1980) % 100, |
| 580 | (dostime & 0x0000f800) >> 11, |
| 581 | (dostime & 0x000007e0) >> 5, |
| 582 | zip_header.formatted.crc32, |
| 583 | dst_fn); |
| 584 | total_usize += zip_header.formatted.ucmpsize; |
| 585 | total_size += zip_header.formatted.cmpsize; |
Denis Vlasenko | feb7ae7 | 2007-10-01 12:05:12 +0000 | [diff] [blame] | 586 | } |
| 587 | i = 'n'; |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 588 | } else if (dst_fd == STDOUT_FILENO) { /* Extracting to STDOUT */ |
| 589 | i = -1; |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 590 | } else if (last_char_is(dst_fn, '/')) { /* Extract directory */ |
| 591 | if (stat(dst_fn, &stat_buf) == -1) { |
| 592 | if (errno != ENOENT) { |
Denis Vlasenko | 006e862 | 2008-09-21 01:01:46 +0000 | [diff] [blame] | 593 | bb_perror_msg_and_die("can't stat '%s'", dst_fn); |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 594 | } |
Denys Vlasenko | 386bc9f | 2009-09-06 16:52:50 +0200 | [diff] [blame] | 595 | if (!quiet) { |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 596 | printf(" creating: %s\n", dst_fn); |
| 597 | } |
| 598 | unzip_create_leading_dirs(dst_fn); |
Denys Vlasenko | 4e8ff73 | 2010-05-24 04:33:02 +0200 | [diff] [blame] | 599 | if (bb_make_directory(dst_fn, dir_mode, 0)) { |
Denys Vlasenko | 831756b | 2011-09-09 17:30:55 +0200 | [diff] [blame] | 600 | xfunc_die(); |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 601 | } |
| 602 | } else { |
| 603 | if (!S_ISDIR(stat_buf.st_mode)) { |
| 604 | bb_error_msg_and_die("'%s' exists but is not directory", dst_fn); |
| 605 | } |
| 606 | } |
| 607 | i = 'n'; |
| 608 | |
| 609 | } else { /* Extract file */ |
Denis Vlasenko | 48a9971 | 2008-07-26 17:32:41 +0000 | [diff] [blame] | 610 | check_file: |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 611 | if (stat(dst_fn, &stat_buf) == -1) { /* File does not exist */ |
| 612 | if (errno != ENOENT) { |
Denis Vlasenko | 006e862 | 2008-09-21 01:01:46 +0000 | [diff] [blame] | 613 | bb_perror_msg_and_die("can't stat '%s'", dst_fn); |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 614 | } |
| 615 | i = 'y'; |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 616 | } else { /* File already exists */ |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 617 | if (overwrite == O_NEVER) { |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 618 | i = 'n'; |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 619 | } else if (S_ISREG(stat_buf.st_mode)) { /* File is regular file */ |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 620 | if (overwrite == O_ALWAYS) { |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 621 | i = 'y'; |
| 622 | } else { |
| 623 | printf("replace %s? [y]es, [n]o, [A]ll, [N]one, [r]ename: ", dst_fn); |
Denys Vlasenko | 831756b | 2011-09-09 17:30:55 +0200 | [diff] [blame] | 624 | fflush_all(); |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 625 | if (!fgets(key_buf, sizeof(key_buf), stdin)) { |
Denis Vlasenko | 006e862 | 2008-09-21 01:01:46 +0000 | [diff] [blame] | 626 | bb_perror_msg_and_die("can't read input"); |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 627 | } |
| 628 | i = key_buf[0]; |
| 629 | } |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 630 | } else { /* File is not regular file */ |
Denis Vlasenko | f5d8c90 | 2008-06-26 14:32:57 +0000 | [diff] [blame] | 631 | bb_error_msg_and_die("'%s' exists but is not regular file", dst_fn); |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 632 | } |
| 633 | } |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 634 | } |
| 635 | } |
| 636 | |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 637 | switch (i) { |
| 638 | case 'A': |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 639 | overwrite = O_ALWAYS; |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 640 | case 'y': /* Open file and fall into unzip */ |
| 641 | unzip_create_leading_dirs(dst_fn); |
Denys Vlasenko | 4e8ff73 | 2010-05-24 04:33:02 +0200 | [diff] [blame] | 642 | #if ENABLE_DESKTOP |
| 643 | dst_fd = xopen3(dst_fn, O_WRONLY | O_CREAT | O_TRUNC, file_mode); |
| 644 | #else |
Bernhard Reutner-Fischer | 64d7e93 | 2006-09-11 16:01:40 +0000 | [diff] [blame] | 645 | dst_fd = xopen(dst_fn, O_WRONLY | O_CREAT | O_TRUNC); |
Denys Vlasenko | 4e8ff73 | 2010-05-24 04:33:02 +0200 | [diff] [blame] | 646 | #endif |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 647 | case -1: /* Unzip */ |
Denys Vlasenko | 386bc9f | 2009-09-06 16:52:50 +0200 | [diff] [blame] | 648 | if (!quiet) { |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 649 | printf(" inflating: %s\n", dst_fn); |
| 650 | } |
Denis Vlasenko | 006e862 | 2008-09-21 01:01:46 +0000 | [diff] [blame] | 651 | unzip_extract(&zip_header, dst_fd); |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 652 | if (dst_fd != STDOUT_FILENO) { |
| 653 | /* closing STDOUT is potentially bad for future business */ |
| 654 | close(dst_fd); |
| 655 | } |
| 656 | break; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 657 | |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 658 | case 'N': |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 659 | overwrite = O_NEVER; |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 660 | case 'n': |
| 661 | /* Skip entry data */ |
Denis Vlasenko | 006e862 | 2008-09-21 01:01:46 +0000 | [diff] [blame] | 662 | unzip_skip(zip_header.formatted.cmpsize); |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 663 | break; |
| 664 | |
| 665 | case 'r': |
| 666 | /* Prompt for new name */ |
| 667 | printf("new name: "); |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 668 | if (!fgets(key_buf, sizeof(key_buf), stdin)) { |
Denis Vlasenko | 006e862 | 2008-09-21 01:01:46 +0000 | [diff] [blame] | 669 | bb_perror_msg_and_die("can't read input"); |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 670 | } |
| 671 | free(dst_fn); |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 672 | dst_fn = xstrdup(key_buf); |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 673 | chomp(dst_fn); |
Denis Vlasenko | 48a9971 | 2008-07-26 17:32:41 +0000 | [diff] [blame] | 674 | goto check_file; |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 675 | |
| 676 | default: |
Denys Vlasenko | 386bc9f | 2009-09-06 16:52:50 +0200 | [diff] [blame] | 677 | printf("error: invalid response [%c]\n", (char)i); |
Denis Vlasenko | 48a9971 | 2008-07-26 17:32:41 +0000 | [diff] [blame] | 678 | goto check_file; |
Paul Fox | 0840b76 | 2005-07-20 20:26:49 +0000 | [diff] [blame] | 679 | } |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 680 | |
Denis Vlasenko | 006e862 | 2008-09-21 01:01:46 +0000 | [diff] [blame] | 681 | total_entries++; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 682 | } |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 683 | |
Denys Vlasenko | 386bc9f | 2009-09-06 16:52:50 +0200 | [diff] [blame] | 684 | if (listing && quiet <= 1) { |
| 685 | if (!verbose) { |
| 686 | // " Length Date Time Name\n" |
| 687 | // " -------- ---- ---- ----" |
| 688 | printf( " -------- -------\n" |
| 689 | "%9lu" " %u files\n", |
| 690 | total_usize, total_entries); |
| 691 | } else { |
| 692 | unsigned long percents = total_usize - total_size; |
| 693 | percents = percents * 100; |
| 694 | if (total_usize) |
| 695 | percents /= total_usize; |
| 696 | // " Length Method Size Ratio Date Time CRC-32 Name\n" |
| 697 | // "-------- ------ ------- ----- ---- ---- ------ ----" |
| 698 | printf( "-------- ------- --- -------\n" |
| 699 | "%8lu" "%17lu%4u%% %u files\n", |
| 700 | total_usize, total_size, (unsigned)percents, |
| 701 | total_entries); |
| 702 | } |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 703 | } |
| 704 | |
Denis Vlasenko | bc7c5d0 | 2007-10-18 23:27:46 +0000 | [diff] [blame] | 705 | return 0; |
Glenn L McGrath | 87ac702 | 2002-01-02 13:52:26 +0000 | [diff] [blame] | 706 | } |