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