Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | * Read-only access to Zip archives, with minimal heap allocation. |
| 19 | */ |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 20 | |
Mark Salyzyn | cfd5b08 | 2016-10-17 14:28:00 -0700 | [diff] [blame] | 21 | #define LOG_TAG "ziparchive" |
| 22 | |
Elliott Hughes | e8f4b14 | 2018-10-19 16:09:39 -0700 | [diff] [blame] | 23 | #include "ziparchive/zip_archive.h" |
| 24 | |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 25 | #include <errno.h> |
Mark Salyzyn | 99ef991 | 2014-03-14 14:26:22 -0700 | [diff] [blame] | 26 | #include <fcntl.h> |
| 27 | #include <inttypes.h> |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 28 | #include <limits.h> |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 29 | #include <stdlib.h> |
| 30 | #include <string.h> |
Elliott Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 31 | #include <time.h> |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 32 | #include <unistd.h> |
| 33 | |
Dan Albert | 1ae0764 | 2015-04-09 14:11:18 -0700 | [diff] [blame] | 34 | #include <memory> |
| 35 | #include <vector> |
| 36 | |
Elliott Hughes | 9c8bd66 | 2018-10-26 16:14:21 -0700 | [diff] [blame] | 37 | #if defined(__APPLE__) |
| 38 | #define lseek64 lseek |
| 39 | #endif |
| 40 | |
Josh Gao | 1b49634 | 2018-07-17 11:08:48 -0700 | [diff] [blame] | 41 | #if defined(__BIONIC__) |
| 42 | #include <android/fdsan.h> |
| 43 | #endif |
| 44 | |
Mark Salyzyn | ff2dcd9 | 2016-09-28 15:54:45 -0700 | [diff] [blame] | 45 | #include <android-base/file.h> |
| 46 | #include <android-base/logging.h> |
| 47 | #include <android-base/macros.h> // TEMP_FAILURE_RETRY may or may not be in unistd |
Elliott Hughes | e8f4b14 | 2018-10-19 16:09:39 -0700 | [diff] [blame] | 48 | #include <android-base/mapped_file.h> |
Mark Salyzyn | ff2dcd9 | 2016-09-28 15:54:45 -0700 | [diff] [blame] | 49 | #include <android-base/memory.h> |
Elliott Hughes | 50ef29a | 2019-06-18 18:23:59 -0700 | [diff] [blame] | 50 | #include <android-base/strings.h> |
Ryan Mitchell | c77f9d3 | 2018-08-25 14:06:29 -0700 | [diff] [blame] | 51 | #include <android-base/utf8.h> |
Mark Salyzyn | cfd5b08 | 2016-10-17 14:28:00 -0700 | [diff] [blame] | 52 | #include <log/log.h> |
Dan Albert | 1ae0764 | 2015-04-09 14:11:18 -0700 | [diff] [blame] | 53 | #include "zlib.h" |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 54 | |
Narayan Kamath | 044bc8e | 2014-12-03 18:22:53 +0000 | [diff] [blame] | 55 | #include "entry_name_utils-inl.h" |
Adam Lesinski | ad4ad8c | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 56 | #include "zip_archive_common.h" |
Christopher Ferris | e6884ce | 2015-11-10 14:55:12 -0800 | [diff] [blame] | 57 | #include "zip_archive_private.h" |
Mark Salyzyn | 99ef991 | 2014-03-14 14:26:22 -0700 | [diff] [blame] | 58 | |
Dan Albert | 1ae0764 | 2015-04-09 14:11:18 -0700 | [diff] [blame] | 59 | using android::base::get_unaligned; |
Narayan Kamath | 044bc8e | 2014-12-03 18:22:53 +0000 | [diff] [blame] | 60 | |
Narayan Kamath | 162b705 | 2017-06-05 13:21:12 +0100 | [diff] [blame] | 61 | // Used to turn on crc checks - verify that the content CRC matches the values |
| 62 | // specified in the local file header and the central directory. |
| 63 | static const bool kCrcChecksEnabled = false; |
| 64 | |
Narayan Kamath | 926973e | 2014-06-09 14:18:14 +0100 | [diff] [blame] | 65 | // The maximum number of bytes to scan backwards for the EOCD start. |
| 66 | static const uint32_t kMaxEOCDSearch = kMaxCommentLen + sizeof(EocdRecord); |
| 67 | |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 68 | /* |
| 69 | * A Read-only Zip archive. |
| 70 | * |
| 71 | * We want "open" and "find entry by name" to be fast operations, and |
| 72 | * we want to use as little memory as possible. We memory-map the zip |
| 73 | * central directory, and load a hash table with pointers to the filenames |
| 74 | * (which aren't null-terminated). The other fields are at a fixed offset |
| 75 | * from the filename, so we don't need to extract those (but we do need |
| 76 | * to byte-read and endian-swap them every time we want them). |
| 77 | * |
| 78 | * It's possible that somebody has handed us a massive (~1GB) zip archive, |
| 79 | * so we can't expect to mmap the entire file. |
| 80 | * |
| 81 | * To speed comparisons when doing a lookup by name, we could make the mapping |
| 82 | * "private" (copy-on-write) and null-terminate the filenames after verifying |
| 83 | * the record structure. However, this requires a private mapping of |
| 84 | * every page that the Central Directory touches. Easier to tuck a copy |
| 85 | * of the string length into the hash table entry. |
| 86 | */ |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 87 | |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 88 | /* |
| 89 | * Round up to the next highest power of 2. |
| 90 | * |
| 91 | * Found on http://graphics.stanford.edu/~seander/bithacks.html. |
| 92 | */ |
| 93 | static uint32_t RoundUpPower2(uint32_t val) { |
| 94 | val--; |
| 95 | val |= val >> 1; |
| 96 | val |= val >> 2; |
| 97 | val |= val >> 4; |
| 98 | val |= val >> 8; |
| 99 | val |= val >> 16; |
| 100 | val++; |
| 101 | |
| 102 | return val; |
| 103 | } |
| 104 | |
Elliott Hughes | 50ef29a | 2019-06-18 18:23:59 -0700 | [diff] [blame] | 105 | static uint32_t ComputeHash(std::string_view name) { |
| 106 | return static_cast<uint32_t>(std::hash<std::string_view>{}(name)); |
Zimuzo | 5a503ef | 2018-09-17 19:49:55 +0100 | [diff] [blame] | 107 | } |
| 108 | |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 109 | /* |
| 110 | * Convert a ZipEntry to a hash table index, verifying that it's in a |
| 111 | * valid range. |
| 112 | */ |
Zimuzo | 5a503ef | 2018-09-17 19:49:55 +0100 | [diff] [blame] | 113 | static int64_t EntryToIndex(const ZipStringOffset* hash_table, const uint32_t hash_table_size, |
Elliott Hughes | 50ef29a | 2019-06-18 18:23:59 -0700 | [diff] [blame] | 114 | std::string_view name, const uint8_t* start) { |
Piotr Jastrzebski | ecccc5a | 2014-08-11 16:35:11 +0100 | [diff] [blame] | 115 | const uint32_t hash = ComputeHash(name); |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 116 | |
| 117 | // NOTE: (hash_table_size - 1) is guaranteed to be non-negative. |
| 118 | uint32_t ent = hash & (hash_table_size - 1); |
Zimuzo | 5a503ef | 2018-09-17 19:49:55 +0100 | [diff] [blame] | 119 | while (hash_table[ent].name_offset != 0) { |
Elliott Hughes | 50ef29a | 2019-06-18 18:23:59 -0700 | [diff] [blame] | 120 | if (hash_table[ent].ToStringView(start) == name) { |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 121 | return ent; |
| 122 | } |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 123 | ent = (ent + 1) & (hash_table_size - 1); |
| 124 | } |
| 125 | |
Elliott Hughes | 50ef29a | 2019-06-18 18:23:59 -0700 | [diff] [blame] | 126 | ALOGV("Zip: Unable to find entry %.*s", static_cast<int>(name.size()), name.data()); |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 127 | return kEntryNotFound; |
| 128 | } |
| 129 | |
| 130 | /* |
| 131 | * Add a new entry to the hash table. |
| 132 | */ |
Andreas Gampe | 964b95c | 2019-04-05 13:48:02 -0700 | [diff] [blame] | 133 | static int32_t AddToHash(ZipStringOffset* hash_table, const uint32_t hash_table_size, |
Elliott Hughes | 50ef29a | 2019-06-18 18:23:59 -0700 | [diff] [blame] | 134 | std::string_view name, const uint8_t* start) { |
Piotr Jastrzebski | ecccc5a | 2014-08-11 16:35:11 +0100 | [diff] [blame] | 135 | const uint64_t hash = ComputeHash(name); |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 136 | uint32_t ent = hash & (hash_table_size - 1); |
| 137 | |
| 138 | /* |
| 139 | * We over-allocated the table, so we're guaranteed to find an empty slot. |
| 140 | * Further, we guarantee that the hashtable size is not 0. |
| 141 | */ |
Zimuzo | 5a503ef | 2018-09-17 19:49:55 +0100 | [diff] [blame] | 142 | while (hash_table[ent].name_offset != 0) { |
Elliott Hughes | 50ef29a | 2019-06-18 18:23:59 -0700 | [diff] [blame] | 143 | if (hash_table[ent].ToStringView(start) == name) { |
| 144 | // We've found a duplicate entry. We don't accept duplicates. |
| 145 | ALOGW("Zip: Found duplicate entry %.*s", static_cast<int>(name.size()), name.data()); |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 146 | return kDuplicateEntry; |
| 147 | } |
| 148 | ent = (ent + 1) & (hash_table_size - 1); |
| 149 | } |
Elliott Hughes | 50ef29a | 2019-06-18 18:23:59 -0700 | [diff] [blame] | 150 | |
| 151 | // `name` has already been validated before entry. |
| 152 | const char* start_char = reinterpret_cast<const char*>(start); |
| 153 | hash_table[ent].name_offset = static_cast<uint32_t>(name.data() - start_char); |
| 154 | hash_table[ent].name_length = static_cast<uint16_t>(name.size()); |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 155 | return 0; |
| 156 | } |
| 157 | |
Josh Gao | abdfc24 | 2018-09-07 12:44:40 -0700 | [diff] [blame] | 158 | #if defined(__BIONIC__) |
| 159 | uint64_t GetOwnerTag(const ZipArchive* archive) { |
| 160 | return android_fdsan_create_owner_tag(ANDROID_FDSAN_OWNER_TYPE_ZIPARCHIVE, |
| 161 | reinterpret_cast<uint64_t>(archive)); |
| 162 | } |
| 163 | #endif |
| 164 | |
Josh Gao | 1b49634 | 2018-07-17 11:08:48 -0700 | [diff] [blame] | 165 | ZipArchive::ZipArchive(const int fd, bool assume_ownership) |
| 166 | : mapped_zip(fd), |
| 167 | close_file(assume_ownership), |
| 168 | directory_offset(0), |
| 169 | central_directory(), |
Elliott Hughes | e8f4b14 | 2018-10-19 16:09:39 -0700 | [diff] [blame] | 170 | directory_map(), |
Josh Gao | 1b49634 | 2018-07-17 11:08:48 -0700 | [diff] [blame] | 171 | num_entries(0), |
| 172 | hash_table_size(0), |
| 173 | hash_table(nullptr) { |
| 174 | #if defined(__BIONIC__) |
| 175 | if (assume_ownership) { |
Josh Gao | abdfc24 | 2018-09-07 12:44:40 -0700 | [diff] [blame] | 176 | android_fdsan_exchange_owner_tag(fd, 0, GetOwnerTag(this)); |
Josh Gao | 1b49634 | 2018-07-17 11:08:48 -0700 | [diff] [blame] | 177 | } |
| 178 | #endif |
| 179 | } |
| 180 | |
Elliott Hughes | f66460b | 2019-10-22 11:44:50 -0700 | [diff] [blame] | 181 | ZipArchive::ZipArchive(const void* address, size_t length) |
Josh Gao | 1b49634 | 2018-07-17 11:08:48 -0700 | [diff] [blame] | 182 | : mapped_zip(address, length), |
| 183 | close_file(false), |
| 184 | directory_offset(0), |
| 185 | central_directory(), |
Elliott Hughes | e8f4b14 | 2018-10-19 16:09:39 -0700 | [diff] [blame] | 186 | directory_map(), |
Josh Gao | 1b49634 | 2018-07-17 11:08:48 -0700 | [diff] [blame] | 187 | num_entries(0), |
| 188 | hash_table_size(0), |
| 189 | hash_table(nullptr) {} |
| 190 | |
| 191 | ZipArchive::~ZipArchive() { |
| 192 | if (close_file && mapped_zip.GetFileDescriptor() >= 0) { |
| 193 | #if defined(__BIONIC__) |
Josh Gao | abdfc24 | 2018-09-07 12:44:40 -0700 | [diff] [blame] | 194 | android_fdsan_close_with_tag(mapped_zip.GetFileDescriptor(), GetOwnerTag(this)); |
Josh Gao | 1b49634 | 2018-07-17 11:08:48 -0700 | [diff] [blame] | 195 | #else |
| 196 | close(mapped_zip.GetFileDescriptor()); |
| 197 | #endif |
| 198 | } |
| 199 | |
| 200 | free(hash_table); |
| 201 | } |
| 202 | |
Tianjie Xu | 18c2592 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 203 | static int32_t MapCentralDirectory0(const char* debug_file_name, ZipArchive* archive, |
Andreas Gampe | 964b95c | 2019-04-05 13:48:02 -0700 | [diff] [blame] | 204 | off64_t file_length, uint32_t read_amount, |
Zimuzo | 5a503ef | 2018-09-17 19:49:55 +0100 | [diff] [blame] | 205 | uint8_t* scan_buffer) { |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 206 | const off64_t search_start = file_length - read_amount; |
| 207 | |
Jiyong Park | cd997e6 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 208 | if (!archive->mapped_zip.ReadAtOffset(scan_buffer, read_amount, search_start)) { |
| 209 | ALOGE("Zip: read %" PRId64 " from offset %" PRId64 " failed", static_cast<int64_t>(read_amount), |
| 210 | static_cast<int64_t>(search_start)); |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 211 | return kIoError; |
| 212 | } |
| 213 | |
| 214 | /* |
| 215 | * Scan backward for the EOCD magic. In an archive without a trailing |
| 216 | * comment, we'll find it on the first try. (We may want to consider |
| 217 | * doing an initial minimal read; if we don't find it, retry with a |
| 218 | * second read as above.) |
| 219 | */ |
Andreas Gampe | 964b95c | 2019-04-05 13:48:02 -0700 | [diff] [blame] | 220 | CHECK_LE(read_amount, std::numeric_limits<int32_t>::max()); |
| 221 | int32_t i = read_amount - sizeof(EocdRecord); |
Narayan Kamath | 926973e | 2014-06-09 14:18:14 +0100 | [diff] [blame] | 222 | for (; i >= 0; i--) { |
Dan Albert | 1ae0764 | 2015-04-09 14:11:18 -0700 | [diff] [blame] | 223 | if (scan_buffer[i] == 0x50) { |
| 224 | uint32_t* sig_addr = reinterpret_cast<uint32_t*>(&scan_buffer[i]); |
| 225 | if (get_unaligned<uint32_t>(sig_addr) == EocdRecord::kSignature) { |
| 226 | ALOGV("+++ Found EOCD at buf+%d", i); |
| 227 | break; |
| 228 | } |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | if (i < 0) { |
| 232 | ALOGD("Zip: EOCD not found, %s is not zip", debug_file_name); |
| 233 | return kInvalidFile; |
| 234 | } |
| 235 | |
| 236 | const off64_t eocd_offset = search_start + i; |
Narayan Kamath | 926973e | 2014-06-09 14:18:14 +0100 | [diff] [blame] | 237 | const EocdRecord* eocd = reinterpret_cast<const EocdRecord*>(scan_buffer + i); |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 238 | /* |
Narayan Kamath | 926973e | 2014-06-09 14:18:14 +0100 | [diff] [blame] | 239 | * Verify that there's no trailing space at the end of the central directory |
| 240 | * and its comment. |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 241 | */ |
Jiyong Park | cd997e6 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 242 | const off64_t calculated_length = eocd_offset + sizeof(EocdRecord) + eocd->comment_length; |
Narayan Kamath | 926973e | 2014-06-09 14:18:14 +0100 | [diff] [blame] | 243 | if (calculated_length != file_length) { |
Narayan Kamath | 4f6b499 | 2014-06-03 13:59:23 +0100 | [diff] [blame] | 244 | ALOGW("Zip: %" PRId64 " extraneous bytes at the end of the central directory", |
Narayan Kamath | 926973e | 2014-06-09 14:18:14 +0100 | [diff] [blame] | 245 | static_cast<int64_t>(file_length - calculated_length)); |
Narayan Kamath | 4f6b499 | 2014-06-03 13:59:23 +0100 | [diff] [blame] | 246 | return kInvalidFile; |
| 247 | } |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 248 | |
Narayan Kamath | 926973e | 2014-06-09 14:18:14 +0100 | [diff] [blame] | 249 | /* |
| 250 | * Grab the CD offset and size, and the number of entries in the |
| 251 | * archive and verify that they look reasonable. |
| 252 | */ |
Tianjie Xu | 1ee4892 | 2016-09-21 14:58:11 -0700 | [diff] [blame] | 253 | if (static_cast<off64_t>(eocd->cd_start_offset) + eocd->cd_size > eocd_offset) { |
Narayan Kamath | 926973e | 2014-06-09 14:18:14 +0100 | [diff] [blame] | 254 | ALOGW("Zip: bad offsets (dir %" PRIu32 ", size %" PRIu32 ", eocd %" PRId64 ")", |
Jiyong Park | cd997e6 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 255 | eocd->cd_start_offset, eocd->cd_size, static_cast<int64_t>(eocd_offset)); |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 256 | return kInvalidOffset; |
| 257 | } |
Narayan Kamath | 926973e | 2014-06-09 14:18:14 +0100 | [diff] [blame] | 258 | if (eocd->num_records == 0) { |
Adam Lesinski | b354dce | 2018-03-01 21:32:13 +0000 | [diff] [blame] | 259 | #if defined(__ANDROID__) |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 260 | ALOGW("Zip: empty archive?"); |
Adam Lesinski | b354dce | 2018-03-01 21:32:13 +0000 | [diff] [blame] | 261 | #endif |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 262 | return kEmptyArchive; |
| 263 | } |
| 264 | |
Jiyong Park | cd997e6 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 265 | ALOGV("+++ num_entries=%" PRIu32 " dir_size=%" PRIu32 " dir_offset=%" PRIu32, eocd->num_records, |
| 266 | eocd->cd_size, eocd->cd_start_offset); |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 267 | |
| 268 | /* |
| 269 | * It all looks good. Create a mapping for the CD, and set the fields |
| 270 | * in archive. |
| 271 | */ |
Tianjie Xu | 18c2592 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 272 | |
Elliott Hughes | e8f4b14 | 2018-10-19 16:09:39 -0700 | [diff] [blame] | 273 | if (!archive->InitializeCentralDirectory(static_cast<off64_t>(eocd->cd_start_offset), |
Tianjie Xu | 18c2592 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 274 | static_cast<size_t>(eocd->cd_size))) { |
| 275 | ALOGE("Zip: failed to intialize central directory.\n"); |
Narayan Kamath | eaf9885 | 2013-12-11 14:51:51 +0000 | [diff] [blame] | 276 | return kMmapFailed; |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 277 | } |
| 278 | |
Narayan Kamath | 926973e | 2014-06-09 14:18:14 +0100 | [diff] [blame] | 279 | archive->num_entries = eocd->num_records; |
| 280 | archive->directory_offset = eocd->cd_start_offset; |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 281 | |
| 282 | return 0; |
| 283 | } |
| 284 | |
| 285 | /* |
| 286 | * Find the zip Central Directory and memory-map it. |
| 287 | * |
| 288 | * On success, returns 0 after populating fields from the EOCD area: |
| 289 | * directory_offset |
Tianjie Xu | 18c2592 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 290 | * directory_ptr |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 291 | * num_entries |
| 292 | */ |
Tianjie Xu | 18c2592 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 293 | static int32_t MapCentralDirectory(const char* debug_file_name, ZipArchive* archive) { |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 294 | // Test file length. We use lseek64 to make sure the file |
| 295 | // is small enough to be a zip file (Its size must be less than |
| 296 | // 0xffffffff bytes). |
Tianjie Xu | 18c2592 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 297 | off64_t file_length = archive->mapped_zip.GetFileLength(); |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 298 | if (file_length == -1) { |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 299 | return kInvalidFile; |
| 300 | } |
| 301 | |
Dmitriy Ivanov | f4cb8e2 | 2015-03-06 10:50:56 -0800 | [diff] [blame] | 302 | if (file_length > static_cast<off64_t>(0xffffffff)) { |
Narayan Kamath | 926973e | 2014-06-09 14:18:14 +0100 | [diff] [blame] | 303 | ALOGV("Zip: zip file too long %" PRId64, static_cast<int64_t>(file_length)); |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 304 | return kInvalidFile; |
| 305 | } |
| 306 | |
Narayan Kamath | 926973e | 2014-06-09 14:18:14 +0100 | [diff] [blame] | 307 | if (file_length < static_cast<off64_t>(sizeof(EocdRecord))) { |
| 308 | ALOGV("Zip: length %" PRId64 " is too small to be zip", static_cast<int64_t>(file_length)); |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 309 | return kInvalidFile; |
| 310 | } |
| 311 | |
| 312 | /* |
| 313 | * Perform the traditional EOCD snipe hunt. |
| 314 | * |
| 315 | * We're searching for the End of Central Directory magic number, |
| 316 | * which appears at the start of the EOCD block. It's followed by |
| 317 | * 18 bytes of EOCD stuff and up to 64KB of archive comment. We |
| 318 | * need to read the last part of the file into a buffer, dig through |
| 319 | * it to find the magic number, parse some values out, and use those |
| 320 | * to determine the extent of the CD. |
| 321 | * |
| 322 | * We start by pulling in the last part of the file. |
| 323 | */ |
Andreas Gampe | 964b95c | 2019-04-05 13:48:02 -0700 | [diff] [blame] | 324 | uint32_t read_amount = kMaxEOCDSearch; |
Narayan Kamath | 926973e | 2014-06-09 14:18:14 +0100 | [diff] [blame] | 325 | if (file_length < read_amount) { |
Andreas Gampe | 964b95c | 2019-04-05 13:48:02 -0700 | [diff] [blame] | 326 | read_amount = static_cast<uint32_t>(file_length); |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Tianjie Xu | 18c2592 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 329 | std::vector<uint8_t> scan_buffer(read_amount); |
Jiyong Park | cd997e6 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 330 | int32_t result = |
| 331 | MapCentralDirectory0(debug_file_name, archive, file_length, read_amount, scan_buffer.data()); |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 332 | return result; |
| 333 | } |
| 334 | |
| 335 | /* |
| 336 | * Parses the Zip archive's Central Directory. Allocates and populates the |
| 337 | * hash table. |
| 338 | * |
| 339 | * Returns 0 on success. |
| 340 | */ |
| 341 | static int32_t ParseZipArchive(ZipArchive* archive) { |
Tianjie Xu | 18c2592 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 342 | const uint8_t* const cd_ptr = archive->central_directory.GetBasePtr(); |
| 343 | const size_t cd_length = archive->central_directory.GetMapLength(); |
Narayan Kamath | 926973e | 2014-06-09 14:18:14 +0100 | [diff] [blame] | 344 | const uint16_t num_entries = archive->num_entries; |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 345 | |
| 346 | /* |
| 347 | * Create hash table. We have a minimum 75% load factor, possibly as |
| 348 | * low as 50% after we round off to a power of 2. There must be at |
| 349 | * least one unused entry to avoid an infinite loop during creation. |
| 350 | */ |
| 351 | archive->hash_table_size = RoundUpPower2(1 + (num_entries * 4) / 3); |
Jiyong Park | cd997e6 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 352 | archive->hash_table = |
Zimuzo | 5a503ef | 2018-09-17 19:49:55 +0100 | [diff] [blame] | 353 | reinterpret_cast<ZipStringOffset*>(calloc(archive->hash_table_size, sizeof(ZipStringOffset))); |
Tianjie Xu | 9e020e2 | 2016-10-10 12:11:30 -0700 | [diff] [blame] | 354 | if (archive->hash_table == nullptr) { |
| 355 | ALOGW("Zip: unable to allocate the %u-entry hash_table, entry size: %zu", |
Elliott Hughes | 50ef29a | 2019-06-18 18:23:59 -0700 | [diff] [blame] | 356 | archive->hash_table_size, sizeof(ZipStringOffset)); |
Tianjie Xu | 9e020e2 | 2016-10-10 12:11:30 -0700 | [diff] [blame] | 357 | return -1; |
| 358 | } |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 359 | |
| 360 | /* |
| 361 | * Walk through the central directory, adding entries to the hash |
| 362 | * table and verifying values. |
| 363 | */ |
Narayan Kamath | 926973e | 2014-06-09 14:18:14 +0100 | [diff] [blame] | 364 | const uint8_t* const cd_end = cd_ptr + cd_length; |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 365 | const uint8_t* ptr = cd_ptr; |
| 366 | for (uint16_t i = 0; i < num_entries; i++) { |
Tianjie Xu | 0fda1cf | 2017-04-05 14:46:27 -0700 | [diff] [blame] | 367 | if (ptr > cd_end - sizeof(CentralDirectoryRecord)) { |
| 368 | ALOGW("Zip: ran off the end (at %" PRIu16 ")", i); |
| 369 | #if defined(__ANDROID__) |
| 370 | android_errorWriteLog(0x534e4554, "36392138"); |
| 371 | #endif |
| 372 | return -1; |
| 373 | } |
| 374 | |
Jiyong Park | cd997e6 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 375 | const CentralDirectoryRecord* cdr = reinterpret_cast<const CentralDirectoryRecord*>(ptr); |
Narayan Kamath | 926973e | 2014-06-09 14:18:14 +0100 | [diff] [blame] | 376 | if (cdr->record_signature != CentralDirectoryRecord::kSignature) { |
Mark Salyzyn | 088bf90 | 2014-05-08 16:02:20 -0700 | [diff] [blame] | 377 | ALOGW("Zip: missed a central dir sig (at %" PRIu16 ")", i); |
Dmitriy Ivanov | 3ea93da | 2015-03-06 11:48:47 -0800 | [diff] [blame] | 378 | return -1; |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 379 | } |
| 380 | |
Narayan Kamath | 926973e | 2014-06-09 14:18:14 +0100 | [diff] [blame] | 381 | const off64_t local_header_offset = cdr->local_file_header_offset; |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 382 | if (local_header_offset >= archive->directory_offset) { |
Dmitriy Ivanov | f4cb8e2 | 2015-03-06 10:50:56 -0800 | [diff] [blame] | 383 | ALOGW("Zip: bad LFH offset %" PRId64 " at entry %" PRIu16, |
Jiyong Park | cd997e6 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 384 | static_cast<int64_t>(local_header_offset), i); |
Dmitriy Ivanov | 3ea93da | 2015-03-06 11:48:47 -0800 | [diff] [blame] | 385 | return -1; |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 386 | } |
| 387 | |
Narayan Kamath | 926973e | 2014-06-09 14:18:14 +0100 | [diff] [blame] | 388 | const uint16_t file_name_length = cdr->file_name_length; |
| 389 | const uint16_t extra_length = cdr->extra_field_length; |
| 390 | const uint16_t comment_length = cdr->comment_length; |
Piotr Jastrzebski | 78271ba | 2014-08-15 12:53:00 +0100 | [diff] [blame] | 391 | const uint8_t* file_name = ptr + sizeof(CentralDirectoryRecord); |
| 392 | |
Tianjie Xu | 9e020e2 | 2016-10-10 12:11:30 -0700 | [diff] [blame] | 393 | if (file_name + file_name_length > cd_end) { |
Elliott Hughes | 50ef29a | 2019-06-18 18:23:59 -0700 | [diff] [blame] | 394 | ALOGW("Zip: file name for entry %" PRIu16 |
| 395 | " exceeds the central directory range, file_name_length: %" PRIu16 ", cd_length: %zu", |
| 396 | i, file_name_length, cd_length); |
Tianjie Xu | 9e020e2 | 2016-10-10 12:11:30 -0700 | [diff] [blame] | 397 | return -1; |
| 398 | } |
Elliott Hughes | 50ef29a | 2019-06-18 18:23:59 -0700 | [diff] [blame] | 399 | // Check that file name is valid UTF-8 and doesn't contain NUL (U+0000) characters. |
Narayan Kamath | 044bc8e | 2014-12-03 18:22:53 +0000 | [diff] [blame] | 400 | if (!IsValidEntryName(file_name, file_name_length)) { |
Elliott Hughes | 50ef29a | 2019-06-18 18:23:59 -0700 | [diff] [blame] | 401 | ALOGW("Zip: invalid file name at entry %" PRIu16, i); |
Dmitriy Ivanov | 3ea93da | 2015-03-06 11:48:47 -0800 | [diff] [blame] | 402 | return -1; |
Piotr Jastrzebski | 78271ba | 2014-08-15 12:53:00 +0100 | [diff] [blame] | 403 | } |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 404 | |
Elliott Hughes | 50ef29a | 2019-06-18 18:23:59 -0700 | [diff] [blame] | 405 | // Add the CDE filename to the hash table. |
| 406 | std::string_view entry_name{reinterpret_cast<const char*>(file_name), file_name_length}; |
Zimuzo | 5a503ef | 2018-09-17 19:49:55 +0100 | [diff] [blame] | 407 | const int add_result = AddToHash(archive->hash_table, archive->hash_table_size, entry_name, |
| 408 | archive->central_directory.GetBasePtr()); |
Dmitriy Ivanov | 3ea93da | 2015-03-06 11:48:47 -0800 | [diff] [blame] | 409 | if (add_result != 0) { |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 410 | ALOGW("Zip: Error adding entry to hash table %d", add_result); |
Dmitriy Ivanov | 3ea93da | 2015-03-06 11:48:47 -0800 | [diff] [blame] | 411 | return add_result; |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 412 | } |
| 413 | |
Narayan Kamath | 926973e | 2014-06-09 14:18:14 +0100 | [diff] [blame] | 414 | ptr += sizeof(CentralDirectoryRecord) + file_name_length + extra_length + comment_length; |
| 415 | if ((ptr - cd_ptr) > static_cast<int64_t>(cd_length)) { |
Jiyong Park | cd997e6 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 416 | ALOGW("Zip: bad CD advance (%tu vs %zu) at entry %" PRIu16, ptr - cd_ptr, cd_length, i); |
Dmitriy Ivanov | 3ea93da | 2015-03-06 11:48:47 -0800 | [diff] [blame] | 417 | return -1; |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 418 | } |
| 419 | } |
Narayan Kamath | c1a56dc | 2017-08-09 18:32:09 +0100 | [diff] [blame] | 420 | |
| 421 | uint32_t lfh_start_bytes; |
| 422 | if (!archive->mapped_zip.ReadAtOffset(reinterpret_cast<uint8_t*>(&lfh_start_bytes), |
| 423 | sizeof(uint32_t), 0)) { |
| 424 | ALOGW("Zip: Unable to read header for entry at offset == 0."); |
| 425 | return -1; |
| 426 | } |
| 427 | |
| 428 | if (lfh_start_bytes != LocalFileHeader::kSignature) { |
| 429 | ALOGW("Zip: Entry at offset zero has invalid LFH signature %" PRIx32, lfh_start_bytes); |
| 430 | #if defined(__ANDROID__) |
| 431 | android_errorWriteLog(0x534e4554, "64211847"); |
| 432 | #endif |
| 433 | return -1; |
| 434 | } |
| 435 | |
Mark Salyzyn | 088bf90 | 2014-05-08 16:02:20 -0700 | [diff] [blame] | 436 | ALOGV("+++ zip good scan %" PRIu16 " entries", num_entries); |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 437 | |
Dmitriy Ivanov | 3ea93da | 2015-03-06 11:48:47 -0800 | [diff] [blame] | 438 | return 0; |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 439 | } |
| 440 | |
Jiyong Park | cd997e6 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 441 | static int32_t OpenArchiveInternal(ZipArchive* archive, const char* debug_file_name) { |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 442 | int32_t result = -1; |
Tianjie Xu | 18c2592 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 443 | if ((result = MapCentralDirectory(debug_file_name, archive)) != 0) { |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 444 | return result; |
| 445 | } |
| 446 | |
| 447 | if ((result = ParseZipArchive(archive))) { |
| 448 | return result; |
| 449 | } |
| 450 | |
| 451 | return 0; |
| 452 | } |
| 453 | |
Jiyong Park | cd997e6 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 454 | int32_t OpenArchiveFd(int fd, const char* debug_file_name, ZipArchiveHandle* handle, |
| 455 | bool assume_ownership) { |
Dmitriy Ivanov | 40b52b2 | 2014-07-15 19:33:00 -0700 | [diff] [blame] | 456 | ZipArchive* archive = new ZipArchive(fd, assume_ownership); |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 457 | *handle = archive; |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 458 | return OpenArchiveInternal(archive, debug_file_name); |
| 459 | } |
| 460 | |
| 461 | int32_t OpenArchive(const char* fileName, ZipArchiveHandle* handle) { |
Nick Kralevich | 3bdf744 | 2018-12-18 12:48:06 -0800 | [diff] [blame] | 462 | const int fd = ::android::base::utf8::open(fileName, O_RDONLY | O_BINARY | O_CLOEXEC, 0); |
Dmitriy Ivanov | 40b52b2 | 2014-07-15 19:33:00 -0700 | [diff] [blame] | 463 | ZipArchive* archive = new ZipArchive(fd, true); |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 464 | *handle = archive; |
| 465 | |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 466 | if (fd < 0) { |
| 467 | ALOGW("Unable to open '%s': %s", fileName, strerror(errno)); |
| 468 | return kIoError; |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 469 | } |
Dmitriy Ivanov | 40b52b2 | 2014-07-15 19:33:00 -0700 | [diff] [blame] | 470 | |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 471 | return OpenArchiveInternal(archive, fileName); |
| 472 | } |
| 473 | |
Elliott Hughes | f66460b | 2019-10-22 11:44:50 -0700 | [diff] [blame] | 474 | int32_t OpenArchiveFromMemory(const void* address, size_t length, const char* debug_file_name, |
Jiyong Park | cd997e6 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 475 | ZipArchiveHandle* handle) { |
Tianjie Xu | 18c2592 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 476 | ZipArchive* archive = new ZipArchive(address, length); |
| 477 | *handle = archive; |
| 478 | return OpenArchiveInternal(archive, debug_file_name); |
| 479 | } |
| 480 | |
Elliott Hughes | 2672413 | 2019-10-25 09:57:58 -0700 | [diff] [blame^] | 481 | ZipArchiveInfo GetArchiveInfo(ZipArchiveHandle archive) { |
| 482 | ZipArchiveInfo result; |
| 483 | result.archive_size = archive->mapped_zip.GetFileLength(); |
| 484 | result.entry_count = archive->num_entries; |
| 485 | return result; |
| 486 | } |
| 487 | |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 488 | /* |
| 489 | * Close a ZipArchive, closing the file and freeing the contents. |
| 490 | */ |
Ryan Prichard | 3673f99 | 2018-10-10 22:41:14 -0700 | [diff] [blame] | 491 | void CloseArchive(ZipArchiveHandle archive) { |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 492 | ALOGV("Closing archive %p", archive); |
Neil Fuller | b1a113f | 2014-07-25 14:43:04 +0100 | [diff] [blame] | 493 | delete archive; |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 494 | } |
| 495 | |
Narayan Kamath | 162b705 | 2017-06-05 13:21:12 +0100 | [diff] [blame] | 496 | static int32_t ValidateDataDescriptor(MappedZipFile& mapped_zip, ZipEntry* entry) { |
Narayan Kamath | 926973e | 2014-06-09 14:18:14 +0100 | [diff] [blame] | 497 | uint8_t ddBuf[sizeof(DataDescriptor) + sizeof(DataDescriptor::kOptSignature)]; |
Adam Lesinski | de117e4 | 2017-06-19 10:27:38 -0700 | [diff] [blame] | 498 | off64_t offset = entry->offset; |
| 499 | if (entry->method != kCompressStored) { |
| 500 | offset += entry->compressed_length; |
| 501 | } else { |
| 502 | offset += entry->uncompressed_length; |
| 503 | } |
| 504 | |
| 505 | if (!mapped_zip.ReadAtOffset(ddBuf, sizeof(ddBuf), offset)) { |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 506 | return kIoError; |
| 507 | } |
| 508 | |
Narayan Kamath | 926973e | 2014-06-09 14:18:14 +0100 | [diff] [blame] | 509 | const uint32_t ddSignature = *(reinterpret_cast<const uint32_t*>(ddBuf)); |
Adam Lesinski | de117e4 | 2017-06-19 10:27:38 -0700 | [diff] [blame] | 510 | const uint16_t ddOffset = (ddSignature == DataDescriptor::kOptSignature) ? 4 : 0; |
| 511 | const DataDescriptor* descriptor = reinterpret_cast<const DataDescriptor*>(ddBuf + ddOffset); |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 512 | |
Narayan Kamath | 162b705 | 2017-06-05 13:21:12 +0100 | [diff] [blame] | 513 | // Validate that the values in the data descriptor match those in the central |
| 514 | // directory. |
| 515 | if (entry->compressed_length != descriptor->compressed_size || |
| 516 | entry->uncompressed_length != descriptor->uncompressed_size || |
| 517 | entry->crc32 != descriptor->crc32) { |
| 518 | ALOGW("Zip: size/crc32 mismatch. expected {%" PRIu32 ", %" PRIu32 ", %" PRIx32 |
| 519 | "}, was {%" PRIu32 ", %" PRIu32 ", %" PRIx32 "}", |
| 520 | entry->compressed_length, entry->uncompressed_length, entry->crc32, |
| 521 | descriptor->compressed_size, descriptor->uncompressed_size, descriptor->crc32); |
| 522 | return kInconsistentInformation; |
| 523 | } |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 524 | |
| 525 | return 0; |
| 526 | } |
| 527 | |
Andreas Gampe | 964b95c | 2019-04-05 13:48:02 -0700 | [diff] [blame] | 528 | static int32_t FindEntry(const ZipArchive* archive, const int32_t ent, ZipEntry* data) { |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 529 | const uint16_t nameLen = archive->hash_table[ent].name_length; |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 530 | |
| 531 | // Recover the start of the central directory entry from the filename |
| 532 | // pointer. The filename is the first entry past the fixed-size data, |
| 533 | // so we can just subtract back from that. |
Elliott Hughes | 50ef29a | 2019-06-18 18:23:59 -0700 | [diff] [blame] | 534 | const uint8_t* base_ptr = archive->central_directory.GetBasePtr(); |
| 535 | const uint8_t* ptr = base_ptr + archive->hash_table[ent].name_offset; |
Narayan Kamath | 926973e | 2014-06-09 14:18:14 +0100 | [diff] [blame] | 536 | ptr -= sizeof(CentralDirectoryRecord); |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 537 | |
| 538 | // This is the base of our mmapped region, we have to sanity check that |
| 539 | // the name that's in the hash table is a pointer to a location within |
| 540 | // this mapped region. |
Tianjie Xu | 18c2592 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 541 | if (ptr < base_ptr || ptr > base_ptr + archive->central_directory.GetMapLength()) { |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 542 | ALOGW("Zip: Invalid entry pointer"); |
| 543 | return kInvalidOffset; |
| 544 | } |
| 545 | |
Jiyong Park | cd997e6 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 546 | const CentralDirectoryRecord* cdr = reinterpret_cast<const CentralDirectoryRecord*>(ptr); |
Narayan Kamath | 926973e | 2014-06-09 14:18:14 +0100 | [diff] [blame] | 547 | |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 548 | // The offset of the start of the central directory in the zipfile. |
| 549 | // We keep this lying around so that we can sanity check all our lengths |
| 550 | // and our per-file structures. |
| 551 | const off64_t cd_offset = archive->directory_offset; |
| 552 | |
| 553 | // Fill out the compression method, modification time, crc32 |
| 554 | // and other interesting attributes from the central directory. These |
| 555 | // will later be compared against values from the local file header. |
Narayan Kamath | 926973e | 2014-06-09 14:18:14 +0100 | [diff] [blame] | 556 | data->method = cdr->compression_method; |
beonit | 0e99a2f | 2015-07-18 02:08:16 +0900 | [diff] [blame] | 557 | data->mod_time = cdr->last_mod_date << 16 | cdr->last_mod_time; |
Narayan Kamath | 926973e | 2014-06-09 14:18:14 +0100 | [diff] [blame] | 558 | data->crc32 = cdr->crc32; |
| 559 | data->compressed_length = cdr->compressed_size; |
| 560 | data->uncompressed_length = cdr->uncompressed_size; |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 561 | |
| 562 | // Figure out the local header offset from the central directory. The |
| 563 | // actual file data will begin after the local header and the name / |
| 564 | // extra comments. |
Narayan Kamath | 926973e | 2014-06-09 14:18:14 +0100 | [diff] [blame] | 565 | const off64_t local_header_offset = cdr->local_file_header_offset; |
| 566 | if (local_header_offset + static_cast<off64_t>(sizeof(LocalFileHeader)) >= cd_offset) { |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 567 | ALOGW("Zip: bad local hdr offset in zip"); |
| 568 | return kInvalidOffset; |
| 569 | } |
| 570 | |
Narayan Kamath | 926973e | 2014-06-09 14:18:14 +0100 | [diff] [blame] | 571 | uint8_t lfh_buf[sizeof(LocalFileHeader)]; |
Tianjie Xu | 18c2592 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 572 | if (!archive->mapped_zip.ReadAtOffset(lfh_buf, sizeof(lfh_buf), local_header_offset)) { |
Dmitriy Ivanov | f4cb8e2 | 2015-03-06 10:50:56 -0800 | [diff] [blame] | 573 | ALOGW("Zip: failed reading lfh name from offset %" PRId64, |
Jiyong Park | cd997e6 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 574 | static_cast<int64_t>(local_header_offset)); |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 575 | return kIoError; |
| 576 | } |
| 577 | |
Jiyong Park | cd997e6 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 578 | const LocalFileHeader* lfh = reinterpret_cast<const LocalFileHeader*>(lfh_buf); |
Narayan Kamath | 926973e | 2014-06-09 14:18:14 +0100 | [diff] [blame] | 579 | |
| 580 | if (lfh->lfh_signature != LocalFileHeader::kSignature) { |
Mark Salyzyn | 99ef991 | 2014-03-14 14:26:22 -0700 | [diff] [blame] | 581 | ALOGW("Zip: didn't find signature at start of lfh, offset=%" PRId64, |
Jiyong Park | cd997e6 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 582 | static_cast<int64_t>(local_header_offset)); |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 583 | return kInvalidOffset; |
| 584 | } |
| 585 | |
| 586 | // Paranoia: Match the values specified in the local file header |
| 587 | // to those specified in the central directory. |
Adam Lesinski | d987c9d | 2017-04-06 18:55:47 -0700 | [diff] [blame] | 588 | |
Narayan Kamath | 162b705 | 2017-06-05 13:21:12 +0100 | [diff] [blame] | 589 | // Warn if central directory and local file header don't agree on the use |
| 590 | // of a trailing Data Descriptor. The reference implementation is inconsistent |
| 591 | // and appears to use the LFH value during extraction (unzip) but the CD value |
| 592 | // while displayng information about archives (zipinfo). The spec remains |
| 593 | // silent on this inconsistency as well. |
| 594 | // |
| 595 | // For now, always use the version from the LFH but make sure that the values |
| 596 | // specified in the central directory match those in the data descriptor. |
| 597 | // |
| 598 | // NOTE: It's also worth noting that unzip *does* warn about inconsistencies in |
| 599 | // bit 11 (EFS: The language encoding flag, marking that filename and comment are |
| 600 | // encoded using UTF-8). This implementation does not check for the presence of |
| 601 | // that flag and always enforces that entry names are valid UTF-8. |
| 602 | if ((lfh->gpb_flags & kGPBDDFlagMask) != (cdr->gpb_flags & kGPBDDFlagMask)) { |
| 603 | ALOGW("Zip: gpb flag mismatch at bit 3. expected {%04" PRIx16 "}, was {%04" PRIx16 "}", |
Adam Lesinski | d987c9d | 2017-04-06 18:55:47 -0700 | [diff] [blame] | 604 | cdr->gpb_flags, lfh->gpb_flags); |
Adam Lesinski | d987c9d | 2017-04-06 18:55:47 -0700 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | // If there is no trailing data descriptor, verify that the central directory and local file |
| 608 | // header agree on the crc, compressed, and uncompressed sizes of the entry. |
Narayan Kamath | 926973e | 2014-06-09 14:18:14 +0100 | [diff] [blame] | 609 | if ((lfh->gpb_flags & kGPBDDFlagMask) == 0) { |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 610 | data->has_data_descriptor = 0; |
Jiyong Park | cd997e6 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 611 | if (data->compressed_length != lfh->compressed_size || |
| 612 | data->uncompressed_length != lfh->uncompressed_size || data->crc32 != lfh->crc32) { |
| 613 | ALOGW("Zip: size/crc32 mismatch. expected {%" PRIu32 ", %" PRIu32 ", %" PRIx32 |
| 614 | "}, was {%" PRIu32 ", %" PRIu32 ", %" PRIx32 "}", |
| 615 | data->compressed_length, data->uncompressed_length, data->crc32, lfh->compressed_size, |
| 616 | lfh->uncompressed_size, lfh->crc32); |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 617 | return kInconsistentInformation; |
| 618 | } |
| 619 | } else { |
| 620 | data->has_data_descriptor = 1; |
| 621 | } |
| 622 | |
Elliott Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 623 | // 4.4.2.1: the upper byte of `version_made_by` gives the source OS. Unix is 3. |
Elliott Hughes | 2672413 | 2019-10-25 09:57:58 -0700 | [diff] [blame^] | 624 | data->version_made_by = cdr->version_made_by; |
| 625 | if ((data->version_made_by >> 8) == 3) { |
Elliott Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 626 | data->unix_mode = (cdr->external_file_attributes >> 16) & 0xffff; |
| 627 | } else { |
| 628 | data->unix_mode = 0777; |
| 629 | } |
| 630 | |
Elliott Hughes | 2672413 | 2019-10-25 09:57:58 -0700 | [diff] [blame^] | 631 | // 4.4.14: the lowest bit of the internal file attributes field indicates text. |
| 632 | // Currently only needed to implement zipinfo. |
| 633 | data->is_text = (cdr->internal_file_attributes & 1); |
| 634 | |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 635 | // Check that the local file header name matches the declared |
| 636 | // name in the central directory. |
Elliott Hughes | 50ef29a | 2019-06-18 18:23:59 -0700 | [diff] [blame] | 637 | if (lfh->file_name_length != nameLen) { |
| 638 | ALOGW("Zip: lfh name length did not match central directory"); |
| 639 | return kInconsistentInformation; |
| 640 | } |
| 641 | const off64_t name_offset = local_header_offset + sizeof(LocalFileHeader); |
| 642 | if (name_offset + lfh->file_name_length > cd_offset) { |
| 643 | ALOGW("Zip: lfh name has invalid declared length"); |
| 644 | return kInvalidOffset; |
| 645 | } |
| 646 | std::vector<uint8_t> name_buf(nameLen); |
| 647 | if (!archive->mapped_zip.ReadAtOffset(name_buf.data(), nameLen, name_offset)) { |
| 648 | ALOGW("Zip: failed reading lfh name from offset %" PRId64, static_cast<int64_t>(name_offset)); |
| 649 | return kIoError; |
| 650 | } |
| 651 | const std::string_view entry_name = |
| 652 | archive->hash_table[ent].ToStringView(archive->central_directory.GetBasePtr()); |
| 653 | if (memcmp(entry_name.data(), name_buf.data(), nameLen) != 0) { |
| 654 | ALOGW("Zip: lfh name did not match central directory"); |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 655 | return kInconsistentInformation; |
| 656 | } |
| 657 | |
Jiyong Park | cd997e6 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 658 | const off64_t data_offset = local_header_offset + sizeof(LocalFileHeader) + |
| 659 | lfh->file_name_length + lfh->extra_field_length; |
Narayan Kamath | 48953a1 | 2014-01-24 12:32:39 +0000 | [diff] [blame] | 660 | if (data_offset > cd_offset) { |
Dmitriy Ivanov | f4cb8e2 | 2015-03-06 10:50:56 -0800 | [diff] [blame] | 661 | ALOGW("Zip: bad data offset %" PRId64 " in zip", static_cast<int64_t>(data_offset)); |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 662 | return kInvalidOffset; |
| 663 | } |
| 664 | |
Dmitriy Ivanov | f4cb8e2 | 2015-03-06 10:50:56 -0800 | [diff] [blame] | 665 | if (static_cast<off64_t>(data_offset + data->compressed_length) > cd_offset) { |
Mark Salyzyn | 088bf90 | 2014-05-08 16:02:20 -0700 | [diff] [blame] | 666 | ALOGW("Zip: bad compressed length in zip (%" PRId64 " + %" PRIu32 " > %" PRId64 ")", |
Jiyong Park | cd997e6 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 667 | static_cast<int64_t>(data_offset), data->compressed_length, |
| 668 | static_cast<int64_t>(cd_offset)); |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 669 | return kInvalidOffset; |
| 670 | } |
| 671 | |
| 672 | if (data->method == kCompressStored && |
Jiyong Park | cd997e6 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 673 | static_cast<off64_t>(data_offset + data->uncompressed_length) > cd_offset) { |
| 674 | ALOGW("Zip: bad uncompressed length in zip (%" PRId64 " + %" PRIu32 " > %" PRId64 ")", |
| 675 | static_cast<int64_t>(data_offset), data->uncompressed_length, |
| 676 | static_cast<int64_t>(cd_offset)); |
| 677 | return kInvalidOffset; |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | data->offset = data_offset; |
| 681 | return 0; |
| 682 | } |
| 683 | |
| 684 | struct IterationHandle { |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 685 | ZipArchive* archive; |
Piotr Jastrzebski | 8e08536 | 2014-08-18 11:37:45 +0100 | [diff] [blame] | 686 | |
Elliott Hughes | 50ef29a | 2019-06-18 18:23:59 -0700 | [diff] [blame] | 687 | std::string prefix; |
| 688 | std::string suffix; |
Elliott Hughes | a22ac0f | 2019-05-08 10:44:06 -0700 | [diff] [blame] | 689 | |
| 690 | uint32_t position = 0; |
| 691 | |
Elliott Hughes | 50ef29a | 2019-06-18 18:23:59 -0700 | [diff] [blame] | 692 | IterationHandle(ZipArchive* archive, std::string_view in_prefix, std::string_view in_suffix) |
| 693 | : archive(archive), prefix(in_prefix), suffix(in_suffix) {} |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 694 | }; |
| 695 | |
Ryan Prichard | 3673f99 | 2018-10-10 22:41:14 -0700 | [diff] [blame] | 696 | int32_t StartIteration(ZipArchiveHandle archive, void** cookie_ptr, |
Elliott Hughes | a22ac0f | 2019-05-08 10:44:06 -0700 | [diff] [blame] | 697 | const std::string_view optional_prefix, |
| 698 | const std::string_view optional_suffix) { |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 699 | if (archive == NULL || archive->hash_table == NULL) { |
| 700 | ALOGW("Zip: Invalid ZipArchiveHandle"); |
| 701 | return kInvalidHandle; |
| 702 | } |
| 703 | |
Elliott Hughes | a22ac0f | 2019-05-08 10:44:06 -0700 | [diff] [blame] | 704 | if (optional_prefix.size() > static_cast<size_t>(UINT16_MAX) || |
| 705 | optional_suffix.size() > static_cast<size_t>(UINT16_MAX)) { |
| 706 | ALOGW("Zip: prefix/suffix too long"); |
| 707 | return kInvalidEntryName; |
| 708 | } |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 709 | |
Elliott Hughes | a22ac0f | 2019-05-08 10:44:06 -0700 | [diff] [blame] | 710 | *cookie_ptr = new IterationHandle(archive, optional_prefix, optional_suffix); |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 711 | return 0; |
| 712 | } |
| 713 | |
Piotr Jastrzebski | 79c8b34 | 2014-08-08 14:02:17 +0100 | [diff] [blame] | 714 | void EndIteration(void* cookie) { |
Piotr Jastrzebski | ecccc5a | 2014-08-11 16:35:11 +0100 | [diff] [blame] | 715 | delete reinterpret_cast<IterationHandle*>(cookie); |
Piotr Jastrzebski | 79c8b34 | 2014-08-08 14:02:17 +0100 | [diff] [blame] | 716 | } |
| 717 | |
Elliott Hughes | b17bf52 | 2019-05-03 22:38:44 -0700 | [diff] [blame] | 718 | int32_t FindEntry(const ZipArchiveHandle archive, const std::string_view entryName, |
| 719 | ZipEntry* data) { |
| 720 | if (entryName.empty() || entryName.size() > static_cast<size_t>(UINT16_MAX)) { |
| 721 | ALOGW("Zip: Invalid filename of length %zu", entryName.size()); |
| 722 | return kInvalidEntryName; |
| 723 | } |
| 724 | |
Elliott Hughes | 50ef29a | 2019-06-18 18:23:59 -0700 | [diff] [blame] | 725 | const int64_t ent = EntryToIndex(archive->hash_table, archive->hash_table_size, entryName, |
| 726 | archive->central_directory.GetBasePtr()); |
Elliott Hughes | b17bf52 | 2019-05-03 22:38:44 -0700 | [diff] [blame] | 727 | if (ent < 0) { |
| 728 | ALOGV("Zip: Could not find entry %.*s", static_cast<int>(entryName.size()), entryName.data()); |
| 729 | return static_cast<int32_t>(ent); // kEntryNotFound is safe to truncate. |
| 730 | } |
Elliott Hughes | a5ff19e | 2019-05-07 09:27:59 -0700 | [diff] [blame] | 731 | // We know there are at most hash_table_size entries, safe to truncate. |
Elliott Hughes | b17bf52 | 2019-05-03 22:38:44 -0700 | [diff] [blame] | 732 | return FindEntry(archive, static_cast<uint32_t>(ent), data); |
| 733 | } |
| 734 | |
Elliott Hughes | e06a808 | 2019-05-22 18:56:41 -0700 | [diff] [blame] | 735 | int32_t Next(void* cookie, ZipEntry* data, std::string* name) { |
Elliott Hughes | 1e40c30 | 2019-06-12 12:12:47 -0700 | [diff] [blame] | 736 | std::string_view sv; |
| 737 | int32_t result = Next(cookie, data, &sv); |
| 738 | if (result == 0 && name) { |
| 739 | *name = std::string(sv); |
| 740 | } |
| 741 | return result; |
| 742 | } |
| 743 | |
| 744 | int32_t Next(void* cookie, ZipEntry* data, std::string_view* name) { |
Dmitriy Ivanov | f4cb8e2 | 2015-03-06 10:50:56 -0800 | [diff] [blame] | 745 | IterationHandle* handle = reinterpret_cast<IterationHandle*>(cookie); |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 746 | if (handle == NULL) { |
Zimuzo | 5a503ef | 2018-09-17 19:49:55 +0100 | [diff] [blame] | 747 | ALOGW("Zip: Null ZipArchiveHandle"); |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 748 | return kInvalidHandle; |
| 749 | } |
| 750 | |
| 751 | ZipArchive* archive = handle->archive; |
| 752 | if (archive == NULL || archive->hash_table == NULL) { |
| 753 | ALOGW("Zip: Invalid ZipArchiveHandle"); |
| 754 | return kInvalidHandle; |
| 755 | } |
| 756 | |
| 757 | const uint32_t currentOffset = handle->position; |
| 758 | const uint32_t hash_table_length = archive->hash_table_size; |
Zimuzo | 5a503ef | 2018-09-17 19:49:55 +0100 | [diff] [blame] | 759 | const ZipStringOffset* hash_table = archive->hash_table; |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 760 | for (uint32_t i = currentOffset; i < hash_table_length; ++i) { |
Elliott Hughes | 50ef29a | 2019-06-18 18:23:59 -0700 | [diff] [blame] | 761 | const std::string_view entry_name = |
| 762 | hash_table[i].ToStringView(archive->central_directory.GetBasePtr()); |
| 763 | if (hash_table[i].name_offset != 0 && (android::base::StartsWith(entry_name, handle->prefix) && |
| 764 | android::base::EndsWith(entry_name, handle->suffix))) { |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 765 | handle->position = (i + 1); |
| 766 | const int error = FindEntry(archive, i, data); |
Elliott Hughes | 50ef29a | 2019-06-18 18:23:59 -0700 | [diff] [blame] | 767 | if (!error && name) { |
| 768 | *name = entry_name; |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 769 | } |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 770 | return error; |
| 771 | } |
| 772 | } |
| 773 | |
| 774 | handle->position = 0; |
| 775 | return kIterationEnd; |
| 776 | } |
| 777 | |
Narayan Kamath | f899bd5 | 2015-04-17 11:53:14 +0100 | [diff] [blame] | 778 | // A Writer that writes data to a fixed size memory region. |
| 779 | // The size of the memory region must be equal to the total size of |
| 780 | // the data appended to it. |
Narayan Kamath | 485b364 | 2017-10-26 14:42:39 +0100 | [diff] [blame] | 781 | class MemoryWriter : public zip_archive::Writer { |
Narayan Kamath | f899bd5 | 2015-04-17 11:53:14 +0100 | [diff] [blame] | 782 | public: |
Jiyong Park | cd997e6 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 783 | MemoryWriter(uint8_t* buf, size_t size) : Writer(), buf_(buf), size_(size), bytes_written_(0) {} |
Narayan Kamath | f899bd5 | 2015-04-17 11:53:14 +0100 | [diff] [blame] | 784 | |
| 785 | virtual bool Append(uint8_t* buf, size_t buf_size) override { |
| 786 | if (bytes_written_ + buf_size > size_) { |
Elliott Hughes | e8f4b14 | 2018-10-19 16:09:39 -0700 | [diff] [blame] | 787 | ALOGW("Zip: Unexpected size %zu (declared) vs %zu (actual)", size_, |
Jiyong Park | cd997e6 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 788 | bytes_written_ + buf_size); |
Narayan Kamath | f899bd5 | 2015-04-17 11:53:14 +0100 | [diff] [blame] | 789 | return false; |
| 790 | } |
| 791 | |
| 792 | memcpy(buf_ + bytes_written_, buf, buf_size); |
| 793 | bytes_written_ += buf_size; |
| 794 | return true; |
| 795 | } |
| 796 | |
| 797 | private: |
| 798 | uint8_t* const buf_; |
| 799 | const size_t size_; |
| 800 | size_t bytes_written_; |
| 801 | }; |
| 802 | |
| 803 | // A Writer that appends data to a file |fd| at its current position. |
| 804 | // The file will be truncated to the end of the written data. |
Narayan Kamath | 485b364 | 2017-10-26 14:42:39 +0100 | [diff] [blame] | 805 | class FileWriter : public zip_archive::Writer { |
Narayan Kamath | f899bd5 | 2015-04-17 11:53:14 +0100 | [diff] [blame] | 806 | public: |
Narayan Kamath | f899bd5 | 2015-04-17 11:53:14 +0100 | [diff] [blame] | 807 | // Creates a FileWriter for |fd| and prepare to write |entry| to it, |
| 808 | // guaranteeing that the file descriptor is valid and that there's enough |
| 809 | // space on the volume to write out the entry completely and that the file |
Tao Bao | a456c21 | 2016-11-15 10:08:07 -0800 | [diff] [blame] | 810 | // is truncated to the correct length (no truncation if |fd| references a |
| 811 | // block device). |
Narayan Kamath | f899bd5 | 2015-04-17 11:53:14 +0100 | [diff] [blame] | 812 | // |
| 813 | // Returns a valid FileWriter on success, |nullptr| if an error occurred. |
Yurii Zubrytskyi | 834326c | 2017-12-20 01:01:01 -0800 | [diff] [blame] | 814 | static FileWriter Create(int fd, const ZipEntry* entry) { |
Narayan Kamath | f899bd5 | 2015-04-17 11:53:14 +0100 | [diff] [blame] | 815 | const uint32_t declared_length = entry->uncompressed_length; |
| 816 | const off64_t current_offset = lseek64(fd, 0, SEEK_CUR); |
| 817 | if (current_offset == -1) { |
| 818 | ALOGW("Zip: unable to seek to current location on fd %d: %s", fd, strerror(errno)); |
Yurii Zubrytskyi | 834326c | 2017-12-20 01:01:01 -0800 | [diff] [blame] | 819 | return FileWriter{}; |
Narayan Kamath | f899bd5 | 2015-04-17 11:53:14 +0100 | [diff] [blame] | 820 | } |
| 821 | |
Narayan Kamath | f899bd5 | 2015-04-17 11:53:14 +0100 | [diff] [blame] | 822 | #if defined(__linux__) |
| 823 | if (declared_length > 0) { |
| 824 | // Make sure we have enough space on the volume to extract the compressed |
| 825 | // entry. Note that the call to ftruncate below will change the file size but |
| 826 | // will not allocate space on disk and this call to fallocate will not |
| 827 | // change the file size. |
Badhri Jagan Sridharan | a68d0d1 | 2015-06-02 14:47:57 -0700 | [diff] [blame] | 828 | // Note: fallocate is only supported by the following filesystems - |
| 829 | // btrfs, ext4, ocfs2, and xfs. Therefore fallocate might fail with |
| 830 | // EOPNOTSUPP error when issued in other filesystems. |
| 831 | // Hence, check for the return error code before concluding that the |
| 832 | // disk does not have enough space. |
Andreas Gampe | 964b95c | 2019-04-05 13:48:02 -0700 | [diff] [blame] | 833 | long result = TEMP_FAILURE_RETRY(fallocate(fd, 0, current_offset, declared_length)); |
Badhri Jagan Sridharan | a68d0d1 | 2015-06-02 14:47:57 -0700 | [diff] [blame] | 834 | if (result == -1 && errno == ENOSPC) { |
Elliott Hughes | 4089d34 | 2017-10-27 14:21:12 -0700 | [diff] [blame] | 835 | ALOGW("Zip: unable to allocate %" PRId64 " bytes at offset %" PRId64 ": %s", |
Narayan Kamath | d5d7abe | 2016-08-10 12:24:05 +0100 | [diff] [blame] | 836 | static_cast<int64_t>(declared_length), static_cast<int64_t>(current_offset), |
| 837 | strerror(errno)); |
Yurii Zubrytskyi | 834326c | 2017-12-20 01:01:01 -0800 | [diff] [blame] | 838 | return FileWriter{}; |
Narayan Kamath | f899bd5 | 2015-04-17 11:53:14 +0100 | [diff] [blame] | 839 | } |
| 840 | } |
| 841 | #endif // __linux__ |
| 842 | |
Tao Bao | a456c21 | 2016-11-15 10:08:07 -0800 | [diff] [blame] | 843 | struct stat sb; |
| 844 | if (fstat(fd, &sb) == -1) { |
| 845 | ALOGW("Zip: unable to fstat file: %s", strerror(errno)); |
Yurii Zubrytskyi | 834326c | 2017-12-20 01:01:01 -0800 | [diff] [blame] | 846 | return FileWriter{}; |
Narayan Kamath | f899bd5 | 2015-04-17 11:53:14 +0100 | [diff] [blame] | 847 | } |
| 848 | |
Tao Bao | a456c21 | 2016-11-15 10:08:07 -0800 | [diff] [blame] | 849 | // Block device doesn't support ftruncate(2). |
| 850 | if (!S_ISBLK(sb.st_mode)) { |
Andreas Gampe | 964b95c | 2019-04-05 13:48:02 -0700 | [diff] [blame] | 851 | long result = TEMP_FAILURE_RETRY(ftruncate(fd, declared_length + current_offset)); |
Tao Bao | a456c21 | 2016-11-15 10:08:07 -0800 | [diff] [blame] | 852 | if (result == -1) { |
| 853 | ALOGW("Zip: unable to truncate file to %" PRId64 ": %s", |
| 854 | static_cast<int64_t>(declared_length + current_offset), strerror(errno)); |
Yurii Zubrytskyi | 834326c | 2017-12-20 01:01:01 -0800 | [diff] [blame] | 855 | return FileWriter{}; |
Tao Bao | a456c21 | 2016-11-15 10:08:07 -0800 | [diff] [blame] | 856 | } |
| 857 | } |
| 858 | |
Yurii Zubrytskyi | 834326c | 2017-12-20 01:01:01 -0800 | [diff] [blame] | 859 | return FileWriter(fd, declared_length); |
Narayan Kamath | f899bd5 | 2015-04-17 11:53:14 +0100 | [diff] [blame] | 860 | } |
| 861 | |
Chih-Hung Hsieh | 747eb14 | 2018-09-25 11:16:22 -0700 | [diff] [blame] | 862 | FileWriter(FileWriter&& other) noexcept |
Yurii Zubrytskyi | 834326c | 2017-12-20 01:01:01 -0800 | [diff] [blame] | 863 | : fd_(other.fd_), |
| 864 | declared_length_(other.declared_length_), |
| 865 | total_bytes_written_(other.total_bytes_written_) { |
| 866 | other.fd_ = -1; |
| 867 | } |
| 868 | |
| 869 | bool IsValid() const { return fd_ != -1; } |
| 870 | |
Narayan Kamath | f899bd5 | 2015-04-17 11:53:14 +0100 | [diff] [blame] | 871 | virtual bool Append(uint8_t* buf, size_t buf_size) override { |
| 872 | if (total_bytes_written_ + buf_size > declared_length_) { |
Elliott Hughes | e8f4b14 | 2018-10-19 16:09:39 -0700 | [diff] [blame] | 873 | ALOGW("Zip: Unexpected size %zu (declared) vs %zu (actual)", declared_length_, |
Jiyong Park | cd997e6 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 874 | total_bytes_written_ + buf_size); |
Narayan Kamath | f899bd5 | 2015-04-17 11:53:14 +0100 | [diff] [blame] | 875 | return false; |
| 876 | } |
| 877 | |
Narayan Kamath | e97e66e | 2015-04-27 16:25:53 +0100 | [diff] [blame] | 878 | const bool result = android::base::WriteFully(fd_, buf, buf_size); |
| 879 | if (result) { |
| 880 | total_bytes_written_ += buf_size; |
| 881 | } else { |
Elliott Hughes | e8f4b14 | 2018-10-19 16:09:39 -0700 | [diff] [blame] | 882 | ALOGW("Zip: unable to write %zu bytes to file; %s", buf_size, strerror(errno)); |
Narayan Kamath | f899bd5 | 2015-04-17 11:53:14 +0100 | [diff] [blame] | 883 | } |
| 884 | |
Narayan Kamath | e97e66e | 2015-04-27 16:25:53 +0100 | [diff] [blame] | 885 | return result; |
Narayan Kamath | f899bd5 | 2015-04-17 11:53:14 +0100 | [diff] [blame] | 886 | } |
Jiyong Park | cd997e6 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 887 | |
Narayan Kamath | f899bd5 | 2015-04-17 11:53:14 +0100 | [diff] [blame] | 888 | private: |
Yurii Zubrytskyi | 834326c | 2017-12-20 01:01:01 -0800 | [diff] [blame] | 889 | explicit FileWriter(const int fd = -1, const size_t declared_length = 0) |
Jiyong Park | cd997e6 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 890 | : Writer(), fd_(fd), declared_length_(declared_length), total_bytes_written_(0) {} |
Narayan Kamath | f899bd5 | 2015-04-17 11:53:14 +0100 | [diff] [blame] | 891 | |
Yurii Zubrytskyi | 834326c | 2017-12-20 01:01:01 -0800 | [diff] [blame] | 892 | int fd_; |
Narayan Kamath | f899bd5 | 2015-04-17 11:53:14 +0100 | [diff] [blame] | 893 | const size_t declared_length_; |
| 894 | size_t total_bytes_written_; |
| 895 | }; |
| 896 | |
Narayan Kamath | 485b364 | 2017-10-26 14:42:39 +0100 | [diff] [blame] | 897 | class EntryReader : public zip_archive::Reader { |
Narayan Kamath | 8b8faed | 2017-10-26 14:08:38 +0100 | [diff] [blame] | 898 | public: |
| 899 | EntryReader(const MappedZipFile& zip_file, const ZipEntry* entry) |
| 900 | : Reader(), zip_file_(zip_file), entry_(entry) {} |
| 901 | |
| 902 | virtual bool ReadAtOffset(uint8_t* buf, size_t len, uint32_t offset) const { |
| 903 | return zip_file_.ReadAtOffset(buf, len, entry_->offset + offset); |
| 904 | } |
| 905 | |
| 906 | virtual ~EntryReader() {} |
| 907 | |
| 908 | private: |
| 909 | const MappedZipFile& zip_file_; |
| 910 | const ZipEntry* entry_; |
| 911 | }; |
| 912 | |
Dmitriy Ivanov | f94e159 | 2015-03-06 13:27:59 -0800 | [diff] [blame] | 913 | // This method is using libz macros with old-style-casts |
| 914 | #pragma GCC diagnostic push |
| 915 | #pragma GCC diagnostic ignored "-Wold-style-cast" |
| 916 | static inline int zlib_inflateInit2(z_stream* stream, int window_bits) { |
| 917 | return inflateInit2(stream, window_bits); |
| 918 | } |
| 919 | #pragma GCC diagnostic pop |
| 920 | |
Narayan Kamath | 485b364 | 2017-10-26 14:42:39 +0100 | [diff] [blame] | 921 | namespace zip_archive { |
| 922 | |
| 923 | // Moved out of line to avoid -Wweak-vtables. |
| 924 | Reader::~Reader() {} |
| 925 | Writer::~Writer() {} |
| 926 | |
| 927 | int32_t Inflate(const Reader& reader, const uint32_t compressed_length, |
| 928 | const uint32_t uncompressed_length, Writer* writer, uint64_t* crc_out) { |
Dmitriy Ivanov | edbabfe | 2015-03-12 09:58:15 -0700 | [diff] [blame] | 929 | const size_t kBufSize = 32768; |
| 930 | std::vector<uint8_t> read_buf(kBufSize); |
| 931 | std::vector<uint8_t> write_buf(kBufSize); |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 932 | z_stream zstream; |
| 933 | int zerr; |
| 934 | |
| 935 | /* |
| 936 | * Initialize the zlib stream struct. |
| 937 | */ |
| 938 | memset(&zstream, 0, sizeof(zstream)); |
| 939 | zstream.zalloc = Z_NULL; |
| 940 | zstream.zfree = Z_NULL; |
| 941 | zstream.opaque = Z_NULL; |
| 942 | zstream.next_in = NULL; |
| 943 | zstream.avail_in = 0; |
Dmitriy Ivanov | edbabfe | 2015-03-12 09:58:15 -0700 | [diff] [blame] | 944 | zstream.next_out = &write_buf[0]; |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 945 | zstream.avail_out = kBufSize; |
| 946 | zstream.data_type = Z_UNKNOWN; |
| 947 | |
| 948 | /* |
| 949 | * Use the undocumented "negative window bits" feature to tell zlib |
| 950 | * that there's no zlib header waiting for it. |
| 951 | */ |
Dmitriy Ivanov | f94e159 | 2015-03-06 13:27:59 -0800 | [diff] [blame] | 952 | zerr = zlib_inflateInit2(&zstream, -MAX_WBITS); |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 953 | if (zerr != Z_OK) { |
| 954 | if (zerr == Z_VERSION_ERROR) { |
Jiyong Park | cd997e6 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 955 | ALOGE("Installed zlib is not compatible with linked version (%s)", ZLIB_VERSION); |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 956 | } else { |
| 957 | ALOGW("Call to inflateInit2 failed (zerr=%d)", zerr); |
| 958 | } |
| 959 | |
| 960 | return kZlibError; |
| 961 | } |
| 962 | |
Dmitriy Ivanov | 1f741e5 | 2015-03-06 14:26:37 -0800 | [diff] [blame] | 963 | auto zstream_deleter = [](z_stream* stream) { |
Jiyong Park | cd997e6 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 964 | inflateEnd(stream); /* free up any allocated structures */ |
Dmitriy Ivanov | 1f741e5 | 2015-03-06 14:26:37 -0800 | [diff] [blame] | 965 | }; |
| 966 | |
| 967 | std::unique_ptr<z_stream, decltype(zstream_deleter)> zstream_guard(&zstream, zstream_deleter); |
| 968 | |
Narayan Kamath | 2d1e23f | 2017-10-30 11:17:28 +0000 | [diff] [blame] | 969 | const bool compute_crc = (crc_out != nullptr); |
Andreas Gampe | 964b95c | 2019-04-05 13:48:02 -0700 | [diff] [blame] | 970 | uLong crc = 0; |
Narayan Kamath | 8b8faed | 2017-10-26 14:08:38 +0100 | [diff] [blame] | 971 | uint32_t remaining_bytes = compressed_length; |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 972 | do { |
| 973 | /* read as much as we can */ |
| 974 | if (zstream.avail_in == 0) { |
Andreas Gampe | 964b95c | 2019-04-05 13:48:02 -0700 | [diff] [blame] | 975 | const uint32_t read_size = (remaining_bytes > kBufSize) ? kBufSize : remaining_bytes; |
Narayan Kamath | 8b8faed | 2017-10-26 14:08:38 +0100 | [diff] [blame] | 976 | const uint32_t offset = (compressed_length - remaining_bytes); |
Adam Lesinski | de117e4 | 2017-06-19 10:27:38 -0700 | [diff] [blame] | 977 | // Make sure to read at offset to ensure concurrent access to the fd. |
Narayan Kamath | 8b8faed | 2017-10-26 14:08:38 +0100 | [diff] [blame] | 978 | if (!reader.ReadAtOffset(read_buf.data(), read_size, offset)) { |
Andreas Gampe | 964b95c | 2019-04-05 13:48:02 -0700 | [diff] [blame] | 979 | ALOGW("Zip: inflate read failed, getSize = %u: %s", read_size, strerror(errno)); |
Dmitriy Ivanov | 1f741e5 | 2015-03-06 14:26:37 -0800 | [diff] [blame] | 980 | return kIoError; |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 981 | } |
| 982 | |
Narayan Kamath | 8b8faed | 2017-10-26 14:08:38 +0100 | [diff] [blame] | 983 | remaining_bytes -= read_size; |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 984 | |
Dmitriy Ivanov | edbabfe | 2015-03-12 09:58:15 -0700 | [diff] [blame] | 985 | zstream.next_in = &read_buf[0]; |
Narayan Kamath | 8b8faed | 2017-10-26 14:08:38 +0100 | [diff] [blame] | 986 | zstream.avail_in = read_size; |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 987 | } |
| 988 | |
| 989 | /* uncompress the data */ |
| 990 | zerr = inflate(&zstream, Z_NO_FLUSH); |
| 991 | if (zerr != Z_OK && zerr != Z_STREAM_END) { |
Jiyong Park | cd997e6 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 992 | ALOGW("Zip: inflate zerr=%d (nIn=%p aIn=%u nOut=%p aOut=%u)", zerr, zstream.next_in, |
| 993 | zstream.avail_in, zstream.next_out, zstream.avail_out); |
Dmitriy Ivanov | 1f741e5 | 2015-03-06 14:26:37 -0800 | [diff] [blame] | 994 | return kZlibError; |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 995 | } |
| 996 | |
| 997 | /* write when we're full or when we're done */ |
Jiyong Park | cd997e6 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 998 | if (zstream.avail_out == 0 || (zerr == Z_STREAM_END && zstream.avail_out != kBufSize)) { |
Dmitriy Ivanov | edbabfe | 2015-03-12 09:58:15 -0700 | [diff] [blame] | 999 | const size_t write_size = zstream.next_out - &write_buf[0]; |
Narayan Kamath | f899bd5 | 2015-04-17 11:53:14 +0100 | [diff] [blame] | 1000 | if (!writer->Append(&write_buf[0], write_size)) { |
Narayan Kamath | 2d1e23f | 2017-10-30 11:17:28 +0000 | [diff] [blame] | 1001 | return kIoError; |
| 1002 | } else if (compute_crc) { |
Andreas Gampe | 964b95c | 2019-04-05 13:48:02 -0700 | [diff] [blame] | 1003 | DCHECK_LE(write_size, kBufSize); |
| 1004 | crc = crc32(crc, &write_buf[0], static_cast<uint32_t>(write_size)); |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 1005 | } |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 1006 | |
Dmitriy Ivanov | edbabfe | 2015-03-12 09:58:15 -0700 | [diff] [blame] | 1007 | zstream.next_out = &write_buf[0]; |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 1008 | zstream.avail_out = kBufSize; |
| 1009 | } |
| 1010 | } while (zerr == Z_OK); |
| 1011 | |
Elliott Hughes | e8f4b14 | 2018-10-19 16:09:39 -0700 | [diff] [blame] | 1012 | CHECK_EQ(zerr, Z_STREAM_END); /* other errors should've been caught */ |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 1013 | |
Narayan Kamath | 162b705 | 2017-06-05 13:21:12 +0100 | [diff] [blame] | 1014 | // NOTE: zstream.adler is always set to 0, because we're using the -MAX_WBITS |
| 1015 | // "feature" of zlib to tell it there won't be a zlib file header. zlib |
| 1016 | // doesn't bother calculating the checksum in that scenario. We just do |
| 1017 | // it ourselves above because there are no additional gains to be made by |
| 1018 | // having zlib calculate it for us, since they do it by calling crc32 in |
| 1019 | // the same manner that we have above. |
Narayan Kamath | 2d1e23f | 2017-10-30 11:17:28 +0000 | [diff] [blame] | 1020 | if (compute_crc) { |
| 1021 | *crc_out = crc; |
| 1022 | } |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 1023 | |
Narayan Kamath | 8b8faed | 2017-10-26 14:08:38 +0100 | [diff] [blame] | 1024 | if (zstream.total_out != uncompressed_length || remaining_bytes != 0) { |
Jiyong Park | cd997e6 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 1025 | ALOGW("Zip: size mismatch on inflated file (%lu vs %" PRIu32 ")", zstream.total_out, |
| 1026 | uncompressed_length); |
Dmitriy Ivanov | 1f741e5 | 2015-03-06 14:26:37 -0800 | [diff] [blame] | 1027 | return kInconsistentInformation; |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 1028 | } |
| 1029 | |
Dmitriy Ivanov | 1f741e5 | 2015-03-06 14:26:37 -0800 | [diff] [blame] | 1030 | return 0; |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 1031 | } |
Narayan Kamath | 485b364 | 2017-10-26 14:42:39 +0100 | [diff] [blame] | 1032 | } // namespace zip_archive |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 1033 | |
Narayan Kamath | 8b8faed | 2017-10-26 14:08:38 +0100 | [diff] [blame] | 1034 | static int32_t InflateEntryToWriter(MappedZipFile& mapped_zip, const ZipEntry* entry, |
Narayan Kamath | 485b364 | 2017-10-26 14:42:39 +0100 | [diff] [blame] | 1035 | zip_archive::Writer* writer, uint64_t* crc_out) { |
Narayan Kamath | 8b8faed | 2017-10-26 14:08:38 +0100 | [diff] [blame] | 1036 | const EntryReader reader(mapped_zip, entry); |
| 1037 | |
Narayan Kamath | 485b364 | 2017-10-26 14:42:39 +0100 | [diff] [blame] | 1038 | return zip_archive::Inflate(reader, entry->compressed_length, entry->uncompressed_length, writer, |
| 1039 | crc_out); |
Narayan Kamath | 8b8faed | 2017-10-26 14:08:38 +0100 | [diff] [blame] | 1040 | } |
| 1041 | |
Narayan Kamath | 485b364 | 2017-10-26 14:42:39 +0100 | [diff] [blame] | 1042 | static int32_t CopyEntryToWriter(MappedZipFile& mapped_zip, const ZipEntry* entry, |
| 1043 | zip_archive::Writer* writer, uint64_t* crc_out) { |
Narayan Kamath | f899bd5 | 2015-04-17 11:53:14 +0100 | [diff] [blame] | 1044 | static const uint32_t kBufSize = 32768; |
| 1045 | std::vector<uint8_t> buf(kBufSize); |
| 1046 | |
| 1047 | const uint32_t length = entry->uncompressed_length; |
| 1048 | uint32_t count = 0; |
Andreas Gampe | 964b95c | 2019-04-05 13:48:02 -0700 | [diff] [blame] | 1049 | uLong crc = 0; |
Narayan Kamath | f899bd5 | 2015-04-17 11:53:14 +0100 | [diff] [blame] | 1050 | while (count < length) { |
| 1051 | uint32_t remaining = length - count; |
Adam Lesinski | de117e4 | 2017-06-19 10:27:38 -0700 | [diff] [blame] | 1052 | off64_t offset = entry->offset + count; |
Narayan Kamath | f899bd5 | 2015-04-17 11:53:14 +0100 | [diff] [blame] | 1053 | |
Adam Lesinski | de117e4 | 2017-06-19 10:27:38 -0700 | [diff] [blame] | 1054 | // Safe conversion because kBufSize is narrow enough for a 32 bit signed value. |
Andreas Gampe | 964b95c | 2019-04-05 13:48:02 -0700 | [diff] [blame] | 1055 | const uint32_t block_size = (remaining > kBufSize) ? kBufSize : remaining; |
Adam Lesinski | de117e4 | 2017-06-19 10:27:38 -0700 | [diff] [blame] | 1056 | |
| 1057 | // Make sure to read at offset to ensure concurrent access to the fd. |
| 1058 | if (!mapped_zip.ReadAtOffset(buf.data(), block_size, offset)) { |
Andreas Gampe | 964b95c | 2019-04-05 13:48:02 -0700 | [diff] [blame] | 1059 | ALOGW("CopyFileToFile: copy read failed, block_size = %u, offset = %" PRId64 ": %s", |
Adam Lesinski | de117e4 | 2017-06-19 10:27:38 -0700 | [diff] [blame] | 1060 | block_size, static_cast<int64_t>(offset), strerror(errno)); |
Narayan Kamath | f899bd5 | 2015-04-17 11:53:14 +0100 | [diff] [blame] | 1061 | return kIoError; |
| 1062 | } |
| 1063 | |
| 1064 | if (!writer->Append(&buf[0], block_size)) { |
| 1065 | return kIoError; |
| 1066 | } |
| 1067 | crc = crc32(crc, &buf[0], block_size); |
| 1068 | count += block_size; |
| 1069 | } |
| 1070 | |
| 1071 | *crc_out = crc; |
| 1072 | |
| 1073 | return 0; |
| 1074 | } |
| 1075 | |
Ryan Prichard | 3673f99 | 2018-10-10 22:41:14 -0700 | [diff] [blame] | 1076 | int32_t ExtractToWriter(ZipArchiveHandle archive, ZipEntry* entry, zip_archive::Writer* writer) { |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 1077 | const uint16_t method = entry->method; |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 1078 | |
| 1079 | // this should default to kUnknownCompressionMethod. |
| 1080 | int32_t return_value = -1; |
| 1081 | uint64_t crc = 0; |
| 1082 | if (method == kCompressStored) { |
Tianjie Xu | 18c2592 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 1083 | return_value = CopyEntryToWriter(archive->mapped_zip, entry, writer, &crc); |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 1084 | } else if (method == kCompressDeflated) { |
Tianjie Xu | 18c2592 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 1085 | return_value = InflateEntryToWriter(archive->mapped_zip, entry, writer, &crc); |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 1086 | } |
| 1087 | |
| 1088 | if (!return_value && entry->has_data_descriptor) { |
Narayan Kamath | 162b705 | 2017-06-05 13:21:12 +0100 | [diff] [blame] | 1089 | return_value = ValidateDataDescriptor(archive->mapped_zip, entry); |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 1090 | if (return_value) { |
| 1091 | return return_value; |
| 1092 | } |
| 1093 | } |
| 1094 | |
Narayan Kamath | 162b705 | 2017-06-05 13:21:12 +0100 | [diff] [blame] | 1095 | // Validate that the CRC matches the calculated value. |
| 1096 | if (kCrcChecksEnabled && (entry->crc32 != static_cast<uint32_t>(crc))) { |
Mark Salyzyn | 088bf90 | 2014-05-08 16:02:20 -0700 | [diff] [blame] | 1097 | ALOGW("Zip: crc mismatch: expected %" PRIu32 ", was %" PRIu64, entry->crc32, crc); |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 1098 | return kInconsistentInformation; |
| 1099 | } |
| 1100 | |
| 1101 | return return_value; |
| 1102 | } |
| 1103 | |
Ryan Prichard | 3673f99 | 2018-10-10 22:41:14 -0700 | [diff] [blame] | 1104 | int32_t ExtractToMemory(ZipArchiveHandle archive, ZipEntry* entry, uint8_t* begin, uint32_t size) { |
Yurii Zubrytskyi | 834326c | 2017-12-20 01:01:01 -0800 | [diff] [blame] | 1105 | MemoryWriter writer(begin, size); |
Ryan Prichard | 3673f99 | 2018-10-10 22:41:14 -0700 | [diff] [blame] | 1106 | return ExtractToWriter(archive, entry, &writer); |
Narayan Kamath | f899bd5 | 2015-04-17 11:53:14 +0100 | [diff] [blame] | 1107 | } |
| 1108 | |
Ryan Prichard | 3673f99 | 2018-10-10 22:41:14 -0700 | [diff] [blame] | 1109 | int32_t ExtractEntryToFile(ZipArchiveHandle archive, ZipEntry* entry, int fd) { |
Yurii Zubrytskyi | 834326c | 2017-12-20 01:01:01 -0800 | [diff] [blame] | 1110 | auto writer = FileWriter::Create(fd, entry); |
| 1111 | if (!writer.IsValid()) { |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 1112 | return kIoError; |
| 1113 | } |
| 1114 | |
Ryan Prichard | 3673f99 | 2018-10-10 22:41:14 -0700 | [diff] [blame] | 1115 | return ExtractToWriter(archive, entry, &writer); |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 1116 | } |
| 1117 | |
| 1118 | const char* ErrorCodeString(int32_t error_code) { |
Narayan Kamath | 1ef9d2d | 2017-06-15 13:58:25 +0100 | [diff] [blame] | 1119 | // Make sure that the number of entries in kErrorMessages and ErrorCodes |
| 1120 | // match. |
| 1121 | static_assert((-kLastErrorCode + 1) == arraysize(kErrorMessages), |
| 1122 | "(-kLastErrorCode + 1) != arraysize(kErrorMessages)"); |
| 1123 | |
| 1124 | const uint32_t idx = -error_code; |
| 1125 | if (idx < arraysize(kErrorMessages)) { |
| 1126 | return kErrorMessages[idx]; |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 1127 | } |
| 1128 | |
Narayan Kamath | 1ef9d2d | 2017-06-15 13:58:25 +0100 | [diff] [blame] | 1129 | return "Unknown return code"; |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 1130 | } |
| 1131 | |
Ryan Prichard | 3673f99 | 2018-10-10 22:41:14 -0700 | [diff] [blame] | 1132 | int GetFileDescriptor(const ZipArchiveHandle archive) { |
| 1133 | return archive->mapped_zip.GetFileDescriptor(); |
Narayan Kamath | 7462f02 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 1134 | } |
Colin Cross | 7c6c7f0 | 2016-09-16 10:15:51 -0700 | [diff] [blame] | 1135 | |
Tianjie Xu | 18c2592 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 1136 | #if !defined(_WIN32) |
Narayan Kamath | 485b364 | 2017-10-26 14:42:39 +0100 | [diff] [blame] | 1137 | class ProcessWriter : public zip_archive::Writer { |
Tianjie Xu | 18c2592 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 1138 | public: |
Jiyong Park | cd997e6 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 1139 | ProcessWriter(ProcessZipEntryFunction func, void* cookie) |
| 1140 | : Writer(), proc_function_(func), cookie_(cookie) {} |
Tianjie Xu | 18c2592 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 1141 | |
| 1142 | virtual bool Append(uint8_t* buf, size_t buf_size) override { |
| 1143 | return proc_function_(buf, buf_size, cookie_); |
| 1144 | } |
| 1145 | |
| 1146 | private: |
| 1147 | ProcessZipEntryFunction proc_function_; |
| 1148 | void* cookie_; |
| 1149 | }; |
| 1150 | |
Ryan Prichard | 3673f99 | 2018-10-10 22:41:14 -0700 | [diff] [blame] | 1151 | int32_t ProcessZipEntryContents(ZipArchiveHandle archive, ZipEntry* entry, |
Tianjie Xu | 18c2592 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 1152 | ProcessZipEntryFunction func, void* cookie) { |
| 1153 | ProcessWriter writer(func, cookie); |
Ryan Prichard | 3673f99 | 2018-10-10 22:41:14 -0700 | [diff] [blame] | 1154 | return ExtractToWriter(archive, entry, &writer); |
Tianjie Xu | 18c2592 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 1155 | } |
| 1156 | |
Jiyong Park | cd997e6 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 1157 | #endif //! defined(_WIN32) |
Tianjie Xu | 18c2592 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 1158 | |
| 1159 | int MappedZipFile::GetFileDescriptor() const { |
| 1160 | if (!has_fd_) { |
| 1161 | ALOGW("Zip: MappedZipFile doesn't have a file descriptor."); |
| 1162 | return -1; |
| 1163 | } |
| 1164 | return fd_; |
| 1165 | } |
| 1166 | |
Elliott Hughes | f66460b | 2019-10-22 11:44:50 -0700 | [diff] [blame] | 1167 | const void* MappedZipFile::GetBasePtr() const { |
Tianjie Xu | 18c2592 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 1168 | if (has_fd_) { |
| 1169 | ALOGW("Zip: MappedZipFile doesn't have a base pointer."); |
| 1170 | return nullptr; |
| 1171 | } |
| 1172 | return base_ptr_; |
| 1173 | } |
| 1174 | |
| 1175 | off64_t MappedZipFile::GetFileLength() const { |
| 1176 | if (has_fd_) { |
| 1177 | off64_t result = lseek64(fd_, 0, SEEK_END); |
| 1178 | if (result == -1) { |
| 1179 | ALOGE("Zip: lseek on fd %d failed: %s", fd_, strerror(errno)); |
| 1180 | } |
| 1181 | return result; |
| 1182 | } else { |
| 1183 | if (base_ptr_ == nullptr) { |
| 1184 | ALOGE("Zip: invalid file map\n"); |
| 1185 | return -1; |
| 1186 | } |
| 1187 | return static_cast<off64_t>(data_length_); |
| 1188 | } |
| 1189 | } |
| 1190 | |
Tianjie Xu | 18c2592 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 1191 | // Attempts to read |len| bytes into |buf| at offset |off|. |
Narayan Kamath | 8b8faed | 2017-10-26 14:08:38 +0100 | [diff] [blame] | 1192 | bool MappedZipFile::ReadAtOffset(uint8_t* buf, size_t len, off64_t off) const { |
Tianjie Xu | 18c2592 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 1193 | if (has_fd_) { |
Adam Lesinski | de117e4 | 2017-06-19 10:27:38 -0700 | [diff] [blame] | 1194 | if (!android::base::ReadFullyAtOffset(fd_, buf, len, off)) { |
Tianjie Xu | 18c2592 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 1195 | ALOGE("Zip: failed to read at offset %" PRId64 "\n", off); |
| 1196 | return false; |
| 1197 | } |
Adam Lesinski | de117e4 | 2017-06-19 10:27:38 -0700 | [diff] [blame] | 1198 | } else { |
| 1199 | if (off < 0 || off > static_cast<off64_t>(data_length_)) { |
| 1200 | ALOGE("Zip: invalid offset: %" PRId64 ", data length: %" PRId64 "\n", off, data_length_); |
| 1201 | return false; |
| 1202 | } |
Elliott Hughes | f66460b | 2019-10-22 11:44:50 -0700 | [diff] [blame] | 1203 | memcpy(buf, static_cast<const uint8_t*>(base_ptr_) + off, len); |
Tianjie Xu | 18c2592 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 1204 | } |
Adam Lesinski | de117e4 | 2017-06-19 10:27:38 -0700 | [diff] [blame] | 1205 | return true; |
Tianjie Xu | 18c2592 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 1206 | } |
| 1207 | |
Elliott Hughes | f66460b | 2019-10-22 11:44:50 -0700 | [diff] [blame] | 1208 | void CentralDirectory::Initialize(const void* map_base_ptr, off64_t cd_start_offset, |
| 1209 | size_t cd_size) { |
| 1210 | base_ptr_ = static_cast<const uint8_t*>(map_base_ptr) + cd_start_offset; |
Tianjie Xu | 18c2592 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 1211 | length_ = cd_size; |
| 1212 | } |
| 1213 | |
Elliott Hughes | e8f4b14 | 2018-10-19 16:09:39 -0700 | [diff] [blame] | 1214 | bool ZipArchive::InitializeCentralDirectory(off64_t cd_start_offset, size_t cd_size) { |
Tianjie Xu | 18c2592 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 1215 | if (mapped_zip.HasFd()) { |
Elliott Hughes | e8f4b14 | 2018-10-19 16:09:39 -0700 | [diff] [blame] | 1216 | directory_map = android::base::MappedFile::FromFd(mapped_zip.GetFileDescriptor(), |
| 1217 | cd_start_offset, cd_size, PROT_READ); |
| 1218 | if (!directory_map) return false; |
Tianjie Xu | 18c2592 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 1219 | |
Elliott Hughes | e8f4b14 | 2018-10-19 16:09:39 -0700 | [diff] [blame] | 1220 | CHECK_EQ(directory_map->size(), cd_size); |
| 1221 | central_directory.Initialize(directory_map->data(), 0 /*offset*/, cd_size); |
Tianjie Xu | 18c2592 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 1222 | } else { |
| 1223 | if (mapped_zip.GetBasePtr() == nullptr) { |
| 1224 | ALOGE("Zip: Failed to map central directory, bad mapped_zip base pointer\n"); |
| 1225 | return false; |
| 1226 | } |
| 1227 | if (static_cast<off64_t>(cd_start_offset) + static_cast<off64_t>(cd_size) > |
| 1228 | mapped_zip.GetFileLength()) { |
Jiyong Park | cd997e6 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 1229 | ALOGE( |
| 1230 | "Zip: Failed to map central directory, offset exceeds mapped memory region (" |
| 1231 | "start_offset %" PRId64 ", cd_size %zu, mapped_region_size %" PRId64 ")", |
| 1232 | static_cast<int64_t>(cd_start_offset), cd_size, mapped_zip.GetFileLength()); |
Tianjie Xu | 18c2592 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 1233 | return false; |
| 1234 | } |
| 1235 | |
| 1236 | central_directory.Initialize(mapped_zip.GetBasePtr(), cd_start_offset, cd_size); |
| 1237 | } |
| 1238 | return true; |
| 1239 | } |
Elliott Hughes | 55fd293 | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 1240 | |
| 1241 | tm ZipEntry::GetModificationTime() const { |
| 1242 | tm t = {}; |
| 1243 | |
| 1244 | t.tm_hour = (mod_time >> 11) & 0x1f; |
| 1245 | t.tm_min = (mod_time >> 5) & 0x3f; |
| 1246 | t.tm_sec = (mod_time & 0x1f) << 1; |
| 1247 | |
| 1248 | t.tm_year = ((mod_time >> 25) & 0x7f) + 80; |
| 1249 | t.tm_mon = ((mod_time >> 21) & 0xf) - 1; |
| 1250 | t.tm_mday = (mod_time >> 16) & 0x1f; |
| 1251 | |
| 1252 | return t; |
| 1253 | } |