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