Narayan Kamath | e1e8f71 | 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 | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 20 | |
Mark Salyzyn | 43344b1 | 2016-10-17 14:28:00 -0700 | [diff] [blame] | 21 | #define LOG_TAG "ziparchive" |
| 22 | |
Elliott Hughes | 51cbbaa | 2018-10-19 16:09:39 -0700 | [diff] [blame] | 23 | #include "ziparchive/zip_archive.h" |
| 24 | |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 25 | #include <errno.h> |
Mark Salyzyn | 285fe0b | 2014-03-14 14:26:22 -0700 | [diff] [blame] | 26 | #include <fcntl.h> |
| 27 | #include <inttypes.h> |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 28 | #include <limits.h> |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 29 | #include <stdlib.h> |
| 30 | #include <string.h> |
Elliott Hughes | 6702cef | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 31 | #include <time.h> |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 32 | #include <unistd.h> |
| 33 | |
Dan Albert | eb75d68 | 2015-04-09 14:11:18 -0700 | [diff] [blame] | 34 | #include <memory> |
Songchun Fan | 2884066 | 2020-03-24 09:15:51 -0700 | [diff] [blame] | 35 | #include <optional> |
Yurii Zubrytskyi | 3c793c4 | 2021-02-22 16:22:01 -0800 | [diff] [blame] | 36 | #include <span> |
Dan Albert | eb75d68 | 2015-04-09 14:11:18 -0700 | [diff] [blame] | 37 | #include <vector> |
| 38 | |
Elliott Hughes | 821e732 | 2018-10-26 16:14:21 -0700 | [diff] [blame] | 39 | #if defined(__APPLE__) |
| 40 | #define lseek64 lseek |
| 41 | #endif |
| 42 | |
Josh Gao | cdd8082 | 2018-07-17 11:08:48 -0700 | [diff] [blame] | 43 | #if defined(__BIONIC__) |
| 44 | #include <android/fdsan.h> |
| 45 | #endif |
| 46 | |
Mark Salyzyn | e101ab9 | 2016-09-28 15:54:45 -0700 | [diff] [blame] | 47 | #include <android-base/file.h> |
| 48 | #include <android-base/logging.h> |
| 49 | #include <android-base/macros.h> // TEMP_FAILURE_RETRY may or may not be in unistd |
Elliott Hughes | 51cbbaa | 2018-10-19 16:09:39 -0700 | [diff] [blame] | 50 | #include <android-base/mapped_file.h> |
Mark Salyzyn | e101ab9 | 2016-09-28 15:54:45 -0700 | [diff] [blame] | 51 | #include <android-base/memory.h> |
Elliott Hughes | b92a93d | 2019-06-18 18:23:59 -0700 | [diff] [blame] | 52 | #include <android-base/strings.h> |
Ryan Mitchell | 58fe33b | 2018-08-25 14:06:29 -0700 | [diff] [blame] | 53 | #include <android-base/utf8.h> |
Mark Salyzyn | 43344b1 | 2016-10-17 14:28:00 -0700 | [diff] [blame] | 54 | #include <log/log.h> |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 55 | |
Narayan Kamath | 1470064 | 2014-12-03 18:22:53 +0000 | [diff] [blame] | 56 | #include "entry_name_utils-inl.h" |
Yurii Zubrytskyi | 3c793c4 | 2021-02-22 16:22:01 -0800 | [diff] [blame] | 57 | #include "incfs_support/signal_handling.h" |
| 58 | #include "incfs_support/util.h" |
Adam Lesinski | 622f304 | 2015-10-05 18:16:18 -0700 | [diff] [blame] | 59 | #include "zip_archive_common.h" |
Christopher Ferris | 2b7daba | 2015-11-10 14:55:12 -0800 | [diff] [blame] | 60 | #include "zip_archive_private.h" |
Yurii Zubrytskyi | 3c793c4 | 2021-02-22 16:22:01 -0800 | [diff] [blame] | 61 | #include "zlib.h" |
Mark Salyzyn | 285fe0b | 2014-03-14 14:26:22 -0700 | [diff] [blame] | 62 | |
Narayan Kamath | 9775435 | 2017-06-05 13:21:12 +0100 | [diff] [blame] | 63 | // Used to turn on crc checks - verify that the content CRC matches the values |
| 64 | // specified in the local file header and the central directory. |
Yurii Zubrytskyi | e75a3e1 | 2020-04-06 19:35:33 -0700 | [diff] [blame] | 65 | static constexpr bool kCrcChecksEnabled = false; |
Narayan Kamath | 9775435 | 2017-06-05 13:21:12 +0100 | [diff] [blame] | 66 | |
Narayan Kamath | 38fa647 | 2014-06-09 14:18:14 +0100 | [diff] [blame] | 67 | // The maximum number of bytes to scan backwards for the EOCD start. |
| 68 | static const uint32_t kMaxEOCDSearch = kMaxCommentLen + sizeof(EocdRecord); |
| 69 | |
Tianjie Xu | 53a7ca0 | 2020-03-11 11:59:10 -0700 | [diff] [blame] | 70 | // Set a reasonable cap (256 GiB) for the zip file size. So the data is always valid when |
| 71 | // we parse the fields in cd or local headers as 64 bits signed integers. |
| 72 | static constexpr uint64_t kMaxFileLength = 256 * static_cast<uint64_t>(1u << 30u); |
| 73 | |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 74 | /* |
| 75 | * A Read-only Zip archive. |
| 76 | * |
| 77 | * We want "open" and "find entry by name" to be fast operations, and |
| 78 | * we want to use as little memory as possible. We memory-map the zip |
| 79 | * central directory, and load a hash table with pointers to the filenames |
| 80 | * (which aren't null-terminated). The other fields are at a fixed offset |
| 81 | * from the filename, so we don't need to extract those (but we do need |
| 82 | * to byte-read and endian-swap them every time we want them). |
| 83 | * |
| 84 | * It's possible that somebody has handed us a massive (~1GB) zip archive, |
| 85 | * so we can't expect to mmap the entire file. |
| 86 | * |
| 87 | * To speed comparisons when doing a lookup by name, we could make the mapping |
| 88 | * "private" (copy-on-write) and null-terminate the filenames after verifying |
| 89 | * the record structure. However, this requires a private mapping of |
| 90 | * every page that the Central Directory touches. Easier to tuck a copy |
| 91 | * of the string length into the hash table entry. |
| 92 | */ |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 93 | |
Josh Gao | 6946b3c | 2018-09-07 12:44:40 -0700 | [diff] [blame] | 94 | #if defined(__BIONIC__) |
| 95 | uint64_t GetOwnerTag(const ZipArchive* archive) { |
| 96 | return android_fdsan_create_owner_tag(ANDROID_FDSAN_OWNER_TYPE_ZIPARCHIVE, |
| 97 | reinterpret_cast<uint64_t>(archive)); |
| 98 | } |
| 99 | #endif |
| 100 | |
Ryan Mitchell | 0edeea9 | 2020-03-09 09:33:46 -0700 | [diff] [blame] | 101 | ZipArchive::ZipArchive(MappedZipFile&& map, bool assume_ownership) |
| 102 | : mapped_zip(map), |
Josh Gao | cdd8082 | 2018-07-17 11:08:48 -0700 | [diff] [blame] | 103 | close_file(assume_ownership), |
| 104 | directory_offset(0), |
| 105 | central_directory(), |
Elliott Hughes | 51cbbaa | 2018-10-19 16:09:39 -0700 | [diff] [blame] | 106 | directory_map(), |
Tianjie Xu | a5e894b | 2020-03-05 16:31:23 -0800 | [diff] [blame] | 107 | num_entries(0) { |
Josh Gao | cdd8082 | 2018-07-17 11:08:48 -0700 | [diff] [blame] | 108 | #if defined(__BIONIC__) |
| 109 | if (assume_ownership) { |
Ryan Mitchell | 0edeea9 | 2020-03-09 09:33:46 -0700 | [diff] [blame] | 110 | CHECK(mapped_zip.HasFd()); |
| 111 | android_fdsan_exchange_owner_tag(mapped_zip.GetFileDescriptor(), 0, GetOwnerTag(this)); |
Josh Gao | cdd8082 | 2018-07-17 11:08:48 -0700 | [diff] [blame] | 112 | } |
| 113 | #endif |
| 114 | } |
| 115 | |
Elliott Hughes | 62c2d53 | 2019-10-22 11:44:50 -0700 | [diff] [blame] | 116 | ZipArchive::ZipArchive(const void* address, size_t length) |
Josh Gao | cdd8082 | 2018-07-17 11:08:48 -0700 | [diff] [blame] | 117 | : mapped_zip(address, length), |
| 118 | close_file(false), |
| 119 | directory_offset(0), |
| 120 | central_directory(), |
Elliott Hughes | 51cbbaa | 2018-10-19 16:09:39 -0700 | [diff] [blame] | 121 | directory_map(), |
Tianjie Xu | a5e894b | 2020-03-05 16:31:23 -0800 | [diff] [blame] | 122 | num_entries(0) {} |
Josh Gao | cdd8082 | 2018-07-17 11:08:48 -0700 | [diff] [blame] | 123 | |
| 124 | ZipArchive::~ZipArchive() { |
| 125 | if (close_file && mapped_zip.GetFileDescriptor() >= 0) { |
| 126 | #if defined(__BIONIC__) |
Josh Gao | 6946b3c | 2018-09-07 12:44:40 -0700 | [diff] [blame] | 127 | android_fdsan_close_with_tag(mapped_zip.GetFileDescriptor(), GetOwnerTag(this)); |
Josh Gao | cdd8082 | 2018-07-17 11:08:48 -0700 | [diff] [blame] | 128 | #else |
| 129 | close(mapped_zip.GetFileDescriptor()); |
| 130 | #endif |
| 131 | } |
Josh Gao | cdd8082 | 2018-07-17 11:08:48 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Tianjie Xu | 53a7ca0 | 2020-03-11 11:59:10 -0700 | [diff] [blame] | 134 | struct CentralDirectoryInfo { |
| 135 | uint64_t num_records; |
| 136 | // The size of the central directory (in bytes). |
| 137 | uint64_t cd_size; |
| 138 | // The offset of the start of the central directory, relative |
| 139 | // to the start of the file. |
| 140 | uint64_t cd_start_offset; |
| 141 | }; |
| 142 | |
Tianjie | 41cf173 | 2020-04-13 16:29:22 -0700 | [diff] [blame] | 143 | // Reads |T| at |readPtr| and increments |readPtr|. Returns std::nullopt if the boundary check |
| 144 | // fails. |
| 145 | template <typename T> |
| 146 | static std::optional<T> TryConsumeUnaligned(uint8_t** readPtr, const uint8_t* bufStart, |
| 147 | size_t bufSize) { |
| 148 | if (bufSize < sizeof(T) || *readPtr - bufStart > bufSize - sizeof(T)) { |
| 149 | ALOGW("Zip: %zu byte read exceeds the boundary of allocated buf, offset %zu, bufSize %zu", |
| 150 | sizeof(T), *readPtr - bufStart, bufSize); |
| 151 | return std::nullopt; |
| 152 | } |
| 153 | return ConsumeUnaligned<T>(readPtr); |
| 154 | } |
| 155 | |
Tianjie | 439a46f | 2020-03-18 17:44:30 -0700 | [diff] [blame] | 156 | static ZipError FindCentralDirectoryInfoForZip64(const char* debugFileName, ZipArchive* archive, |
| 157 | off64_t eocdOffset, CentralDirectoryInfo* cdInfo) { |
| 158 | if (eocdOffset <= sizeof(Zip64EocdLocator)) { |
| 159 | ALOGW("Zip: %s: Not enough space for zip64 eocd locator", debugFileName); |
| 160 | return kInvalidFile; |
| 161 | } |
| 162 | // We expect to find the zip64 eocd locator immediately before the zip eocd. |
| 163 | const int64_t locatorOffset = eocdOffset - sizeof(Zip64EocdLocator); |
| 164 | Zip64EocdLocator zip64EocdLocator{}; |
| 165 | if (!archive->mapped_zip.ReadAtOffset(reinterpret_cast<uint8_t*>((&zip64EocdLocator)), |
| 166 | sizeof(Zip64EocdLocator), locatorOffset)) { |
| 167 | ALOGW("Zip: %s: Read %zu from offset %" PRId64 " failed %s", debugFileName, |
| 168 | sizeof(Zip64EocdLocator), locatorOffset, debugFileName); |
| 169 | return kIoError; |
| 170 | } |
| 171 | |
| 172 | if (zip64EocdLocator.locator_signature != Zip64EocdLocator::kSignature) { |
| 173 | ALOGW("Zip: %s: Zip64 eocd locator signature not found at offset %" PRId64, debugFileName, |
| 174 | locatorOffset); |
| 175 | return kInvalidFile; |
| 176 | } |
| 177 | |
| 178 | const int64_t zip64EocdOffset = zip64EocdLocator.zip64_eocd_offset; |
Tianjie | 0eaec6c | 2020-03-28 18:28:43 -0700 | [diff] [blame] | 179 | if (locatorOffset <= sizeof(Zip64EocdRecord) || |
| 180 | zip64EocdOffset > locatorOffset - sizeof(Zip64EocdRecord)) { |
| 181 | ALOGW("Zip: %s: Bad zip64 eocd offset %" PRId64 ", eocd locator offset %" PRId64, debugFileName, |
| 182 | zip64EocdOffset, locatorOffset); |
Tianjie | 439a46f | 2020-03-18 17:44:30 -0700 | [diff] [blame] | 183 | return kInvalidOffset; |
| 184 | } |
| 185 | |
| 186 | Zip64EocdRecord zip64EocdRecord{}; |
| 187 | if (!archive->mapped_zip.ReadAtOffset(reinterpret_cast<uint8_t*>(&zip64EocdRecord), |
| 188 | sizeof(Zip64EocdRecord), zip64EocdOffset)) { |
| 189 | ALOGW("Zip: %s: read %zu from offset %" PRId64 " failed %s", debugFileName, |
Tianjie | 0eaec6c | 2020-03-28 18:28:43 -0700 | [diff] [blame] | 190 | sizeof(Zip64EocdLocator), zip64EocdOffset, debugFileName); |
Tianjie | 439a46f | 2020-03-18 17:44:30 -0700 | [diff] [blame] | 191 | return kIoError; |
| 192 | } |
| 193 | |
| 194 | if (zip64EocdRecord.record_signature != Zip64EocdRecord::kSignature) { |
| 195 | ALOGW("Zip: %s: Zip64 eocd record signature not found at offset %" PRId64, debugFileName, |
| 196 | zip64EocdOffset); |
| 197 | return kInvalidFile; |
| 198 | } |
| 199 | |
Tianjie | 0eaec6c | 2020-03-28 18:28:43 -0700 | [diff] [blame] | 200 | if (zip64EocdOffset <= zip64EocdRecord.cd_size || |
| 201 | zip64EocdRecord.cd_start_offset > zip64EocdOffset - zip64EocdRecord.cd_size) { |
Tianjie | 439a46f | 2020-03-18 17:44:30 -0700 | [diff] [blame] | 202 | ALOGW("Zip: %s: Bad offset for zip64 central directory. cd offset %" PRIu64 ", cd size %" PRIu64 |
| 203 | ", zip64 eocd offset %" PRIu64, |
| 204 | debugFileName, zip64EocdRecord.cd_start_offset, zip64EocdRecord.cd_size, zip64EocdOffset); |
| 205 | return kInvalidOffset; |
| 206 | } |
| 207 | |
| 208 | *cdInfo = {.num_records = zip64EocdRecord.num_records, |
| 209 | .cd_size = zip64EocdRecord.cd_size, |
| 210 | .cd_start_offset = zip64EocdRecord.cd_start_offset}; |
| 211 | |
| 212 | return kSuccess; |
Tianjie Xu | 53a7ca0 | 2020-03-11 11:59:10 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Yurii Zubrytskyi | 3c793c4 | 2021-02-22 16:22:01 -0800 | [diff] [blame] | 215 | static ZipError FindCentralDirectoryInfo(const char* debug_file_name, |
| 216 | ZipArchive* archive, |
| 217 | off64_t file_length, |
| 218 | std::span<uint8_t> scan_buffer, |
Tianjie Xu | 53a7ca0 | 2020-03-11 11:59:10 -0700 | [diff] [blame] | 219 | CentralDirectoryInfo* cdInfo) { |
Yurii Zubrytskyi | 3c793c4 | 2021-02-22 16:22:01 -0800 | [diff] [blame] | 220 | const auto read_amount = static_cast<uint32_t>(scan_buffer.size()); |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 221 | const off64_t search_start = file_length - read_amount; |
| 222 | |
Tianjie Xu | 53a7ca0 | 2020-03-11 11:59:10 -0700 | [diff] [blame] | 223 | if (!archive->mapped_zip.ReadAtOffset(scan_buffer.data(), read_amount, search_start)) { |
Jiyong Park | 6821cc8 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 224 | ALOGE("Zip: read %" PRId64 " from offset %" PRId64 " failed", static_cast<int64_t>(read_amount), |
| 225 | static_cast<int64_t>(search_start)); |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 226 | return kIoError; |
| 227 | } |
| 228 | |
| 229 | /* |
| 230 | * Scan backward for the EOCD magic. In an archive without a trailing |
| 231 | * comment, we'll find it on the first try. (We may want to consider |
| 232 | * doing an initial minimal read; if we don't find it, retry with a |
| 233 | * second read as above.) |
| 234 | */ |
Andreas Gampe | 5a5ffb5 | 2019-04-05 13:48:02 -0700 | [diff] [blame] | 235 | CHECK_LE(read_amount, std::numeric_limits<int32_t>::max()); |
| 236 | int32_t i = read_amount - sizeof(EocdRecord); |
Narayan Kamath | 38fa647 | 2014-06-09 14:18:14 +0100 | [diff] [blame] | 237 | for (; i >= 0; i--) { |
Dan Albert | eb75d68 | 2015-04-09 14:11:18 -0700 | [diff] [blame] | 238 | if (scan_buffer[i] == 0x50) { |
| 239 | uint32_t* sig_addr = reinterpret_cast<uint32_t*>(&scan_buffer[i]); |
Tianjie | cc92463 | 2020-03-26 12:34:44 -0700 | [diff] [blame] | 240 | if (android::base::get_unaligned<uint32_t>(sig_addr) == EocdRecord::kSignature) { |
Dan Albert | eb75d68 | 2015-04-09 14:11:18 -0700 | [diff] [blame] | 241 | ALOGV("+++ Found EOCD at buf+%d", i); |
| 242 | break; |
| 243 | } |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 244 | } |
| 245 | } |
| 246 | if (i < 0) { |
| 247 | ALOGD("Zip: EOCD not found, %s is not zip", debug_file_name); |
| 248 | return kInvalidFile; |
| 249 | } |
| 250 | |
| 251 | const off64_t eocd_offset = search_start + i; |
Tianjie Xu | 53a7ca0 | 2020-03-11 11:59:10 -0700 | [diff] [blame] | 252 | auto eocd = reinterpret_cast<const EocdRecord*>(scan_buffer.data() + i); |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 253 | /* |
Narayan Kamath | 38fa647 | 2014-06-09 14:18:14 +0100 | [diff] [blame] | 254 | * Verify that there's no trailing space at the end of the central directory |
| 255 | * and its comment. |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 256 | */ |
Jiyong Park | 6821cc8 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 257 | const off64_t calculated_length = eocd_offset + sizeof(EocdRecord) + eocd->comment_length; |
Narayan Kamath | 38fa647 | 2014-06-09 14:18:14 +0100 | [diff] [blame] | 258 | if (calculated_length != file_length) { |
Narayan Kamath | 1685af4 | 2014-06-03 13:59:23 +0100 | [diff] [blame] | 259 | ALOGW("Zip: %" PRId64 " extraneous bytes at the end of the central directory", |
Narayan Kamath | 38fa647 | 2014-06-09 14:18:14 +0100 | [diff] [blame] | 260 | static_cast<int64_t>(file_length - calculated_length)); |
Narayan Kamath | 1685af4 | 2014-06-03 13:59:23 +0100 | [diff] [blame] | 261 | return kInvalidFile; |
| 262 | } |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 263 | |
Tianjie Xu | 53a7ca0 | 2020-03-11 11:59:10 -0700 | [diff] [blame] | 264 | // One of the field is 0xFFFFFFFF, look for the zip64 EOCD instead. |
| 265 | if (eocd->cd_size == UINT32_MAX || eocd->cd_start_offset == UINT32_MAX) { |
| 266 | ALOGV("Looking for the zip64 EOCD, cd_size: %" PRIu32 "cd_start_offset: %" PRId32, |
| 267 | eocd->cd_size, eocd->cd_start_offset); |
Tianjie | 439a46f | 2020-03-18 17:44:30 -0700 | [diff] [blame] | 268 | return FindCentralDirectoryInfoForZip64(debug_file_name, archive, eocd_offset, cdInfo); |
Tianjie Xu | 53a7ca0 | 2020-03-11 11:59:10 -0700 | [diff] [blame] | 269 | } |
| 270 | |
Narayan Kamath | 38fa647 | 2014-06-09 14:18:14 +0100 | [diff] [blame] | 271 | /* |
| 272 | * Grab the CD offset and size, and the number of entries in the |
| 273 | * archive and verify that they look reasonable. |
| 274 | */ |
Tianjie Xu | 3bca82b | 2016-09-21 14:58:11 -0700 | [diff] [blame] | 275 | if (static_cast<off64_t>(eocd->cd_start_offset) + eocd->cd_size > eocd_offset) { |
Narayan Kamath | 38fa647 | 2014-06-09 14:18:14 +0100 | [diff] [blame] | 276 | ALOGW("Zip: bad offsets (dir %" PRIu32 ", size %" PRIu32 ", eocd %" PRId64 ")", |
Jiyong Park | 6821cc8 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 277 | eocd->cd_start_offset, eocd->cd_size, static_cast<int64_t>(eocd_offset)); |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 278 | return kInvalidOffset; |
| 279 | } |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 280 | |
Tianjie Xu | 53a7ca0 | 2020-03-11 11:59:10 -0700 | [diff] [blame] | 281 | *cdInfo = {.num_records = eocd->num_records, |
| 282 | .cd_size = eocd->cd_size, |
| 283 | .cd_start_offset = eocd->cd_start_offset}; |
| 284 | return kSuccess; |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | /* |
| 288 | * Find the zip Central Directory and memory-map it. |
| 289 | * |
Tianjie Xu | 53a7ca0 | 2020-03-11 11:59:10 -0700 | [diff] [blame] | 290 | * On success, returns kSuccess after populating fields from the EOCD area: |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 291 | * directory_offset |
Tianjie Xu | 484de54 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 292 | * directory_ptr |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 293 | * num_entries |
| 294 | */ |
Tianjie Xu | 53a7ca0 | 2020-03-11 11:59:10 -0700 | [diff] [blame] | 295 | static ZipError MapCentralDirectory(const char* debug_file_name, ZipArchive* archive) { |
| 296 | // Test file length. We use lseek64 to make sure the file is small enough to be a zip file. |
Tianjie Xu | 484de54 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 297 | off64_t file_length = archive->mapped_zip.GetFileLength(); |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 298 | if (file_length == -1) { |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 299 | return kInvalidFile; |
| 300 | } |
| 301 | |
Tianjie Xu | 53a7ca0 | 2020-03-11 11:59:10 -0700 | [diff] [blame] | 302 | if (file_length > kMaxFileLength) { |
Narayan Kamath | 38fa647 | 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 | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 304 | return kInvalidFile; |
| 305 | } |
| 306 | |
Narayan Kamath | 38fa647 | 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 | e1e8f71 | 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 | 5a5ffb5 | 2019-04-05 13:48:02 -0700 | [diff] [blame] | 324 | uint32_t read_amount = kMaxEOCDSearch; |
Narayan Kamath | 38fa647 | 2014-06-09 14:18:14 +0100 | [diff] [blame] | 325 | if (file_length < read_amount) { |
Andreas Gampe | 5a5ffb5 | 2019-04-05 13:48:02 -0700 | [diff] [blame] | 326 | read_amount = static_cast<uint32_t>(file_length); |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Tianjie Xu | 53a7ca0 | 2020-03-11 11:59:10 -0700 | [diff] [blame] | 329 | CentralDirectoryInfo cdInfo = {}; |
Yurii Zubrytskyi | 3c793c4 | 2021-02-22 16:22:01 -0800 | [diff] [blame] | 330 | std::vector<uint8_t> scan_buffer(read_amount); |
| 331 | |
| 332 | SCOPED_SIGBUS_HANDLER({ |
| 333 | incfs::util::clearAndFree(scan_buffer); |
| 334 | return kIoError; |
| 335 | }); |
| 336 | |
| 337 | if (auto result = FindCentralDirectoryInfo(debug_file_name, archive, |
| 338 | file_length, scan_buffer, &cdInfo); |
Tianjie Xu | 53a7ca0 | 2020-03-11 11:59:10 -0700 | [diff] [blame] | 339 | result != kSuccess) { |
| 340 | return result; |
| 341 | } |
| 342 | |
Yurii Zubrytskyi | 3c793c4 | 2021-02-22 16:22:01 -0800 | [diff] [blame] | 343 | scan_buffer.clear(); |
| 344 | |
Tianjie Xu | 53a7ca0 | 2020-03-11 11:59:10 -0700 | [diff] [blame] | 345 | if (cdInfo.num_records == 0) { |
| 346 | #if defined(__ANDROID__) |
| 347 | ALOGW("Zip: empty archive?"); |
| 348 | #endif |
| 349 | return kEmptyArchive; |
| 350 | } |
| 351 | |
| 352 | if (cdInfo.cd_size >= SIZE_MAX) { |
| 353 | ALOGW("Zip: The size of central directory doesn't fit in range of size_t: %" PRIu64, |
| 354 | cdInfo.cd_size); |
| 355 | return kInvalidFile; |
| 356 | } |
| 357 | |
| 358 | ALOGV("+++ num_entries=%" PRIu64 " dir_size=%" PRIu64 " dir_offset=%" PRIu64, cdInfo.num_records, |
| 359 | cdInfo.cd_size, cdInfo.cd_start_offset); |
| 360 | |
| 361 | // It all looks good. Create a mapping for the CD, and set the fields in archive. |
| 362 | if (!archive->InitializeCentralDirectory(static_cast<off64_t>(cdInfo.cd_start_offset), |
| 363 | static_cast<size_t>(cdInfo.cd_size))) { |
| 364 | return kMmapFailed; |
| 365 | } |
| 366 | |
| 367 | archive->num_entries = cdInfo.num_records; |
| 368 | archive->directory_offset = cdInfo.cd_start_offset; |
| 369 | |
| 370 | return kSuccess; |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Tianjie | 439a46f | 2020-03-18 17:44:30 -0700 | [diff] [blame] | 373 | static ZipError ParseZip64ExtendedInfoInExtraField( |
| 374 | const uint8_t* extraFieldStart, uint16_t extraFieldLength, uint32_t zip32UncompressedSize, |
| 375 | uint32_t zip32CompressedSize, std::optional<uint32_t> zip32LocalFileHeaderOffset, |
| 376 | Zip64ExtendedInfo* zip64Info) { |
| 377 | if (extraFieldLength <= 4) { |
| 378 | ALOGW("Zip: Extra field isn't large enough to hold zip64 info, size %" PRIu16, |
| 379 | extraFieldLength); |
| 380 | return kInvalidFile; |
| 381 | } |
| 382 | |
| 383 | // Each header MUST consist of: |
| 384 | // Header ID - 2 bytes |
| 385 | // Data Size - 2 bytes |
| 386 | uint16_t offset = 0; |
| 387 | while (offset < extraFieldLength - 4) { |
Tianjie | cc92463 | 2020-03-26 12:34:44 -0700 | [diff] [blame] | 388 | auto readPtr = const_cast<uint8_t*>(extraFieldStart + offset); |
| 389 | auto headerId = ConsumeUnaligned<uint16_t>(&readPtr); |
| 390 | auto dataSize = ConsumeUnaligned<uint16_t>(&readPtr); |
Tianjie | 439a46f | 2020-03-18 17:44:30 -0700 | [diff] [blame] | 391 | |
| 392 | offset += 4; |
| 393 | if (dataSize > extraFieldLength - offset) { |
| 394 | ALOGW("Zip: Data size exceeds the boundary of extra field, data size %" PRIu16, dataSize); |
| 395 | return kInvalidOffset; |
| 396 | } |
| 397 | |
| 398 | // Skip the other types of extensible data fields. Details in |
| 399 | // https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT section 4.5 |
| 400 | if (headerId != Zip64ExtendedInfo::kHeaderId) { |
| 401 | offset += dataSize; |
| 402 | continue; |
| 403 | } |
Kelvin Zhang | 77495e5 | 2020-09-14 15:24:03 -0400 | [diff] [blame] | 404 | // Layout for Zip64 extended info (not include first 4 bytes of header) |
| 405 | // Original |
| 406 | // Size 8 bytes Original uncompressed file size |
| 407 | |
| 408 | // Compressed |
| 409 | // Size 8 bytes Size of compressed data |
| 410 | |
| 411 | // Relative Header |
| 412 | // Offset 8 bytes Offset of local header record |
| 413 | |
| 414 | // Disk Start |
| 415 | // Number 4 bytes Number of the disk on which |
| 416 | // this file starts |
| 417 | if (dataSize == 8 * 3 + 4) { |
| 418 | ALOGW( |
| 419 | "Zip: Found `Disk Start Number` field in extra block. Ignoring it."); |
| 420 | dataSize -= 4; |
| 421 | } |
| 422 | // Sometimes, only a subset of {uncompressed size, compressed size, relative |
| 423 | // header offset} is presents. but golang's zip writer will write out all |
| 424 | // 3 even if only 1 is necessary. We should parse all 3 fields if they are |
| 425 | // there. |
| 426 | const bool completeField = dataSize == 8 * 3; |
Tianjie | 439a46f | 2020-03-18 17:44:30 -0700 | [diff] [blame] | 427 | |
Tianjie | cc92463 | 2020-03-26 12:34:44 -0700 | [diff] [blame] | 428 | std::optional<uint64_t> uncompressedFileSize; |
| 429 | std::optional<uint64_t> compressedFileSize; |
| 430 | std::optional<uint64_t> localHeaderOffset; |
Kelvin Zhang | 77495e5 | 2020-09-14 15:24:03 -0400 | [diff] [blame] | 431 | if (zip32UncompressedSize == UINT32_MAX || completeField) { |
| 432 | uncompressedFileSize = TryConsumeUnaligned<uint64_t>( |
| 433 | &readPtr, extraFieldStart, extraFieldLength); |
Tianjie | 41cf173 | 2020-04-13 16:29:22 -0700 | [diff] [blame] | 434 | if (!uncompressedFileSize.has_value()) return kInvalidOffset; |
Tianjie | cc92463 | 2020-03-26 12:34:44 -0700 | [diff] [blame] | 435 | } |
Kelvin Zhang | 77495e5 | 2020-09-14 15:24:03 -0400 | [diff] [blame] | 436 | if (zip32CompressedSize == UINT32_MAX || completeField) { |
| 437 | compressedFileSize = TryConsumeUnaligned<uint64_t>( |
| 438 | &readPtr, extraFieldStart, extraFieldLength); |
Tianjie | 41cf173 | 2020-04-13 16:29:22 -0700 | [diff] [blame] | 439 | if (!compressedFileSize.has_value()) return kInvalidOffset; |
Tianjie | 439a46f | 2020-03-18 17:44:30 -0700 | [diff] [blame] | 440 | } |
Kelvin Zhang | 77495e5 | 2020-09-14 15:24:03 -0400 | [diff] [blame] | 441 | if (zip32LocalFileHeaderOffset == UINT32_MAX || completeField) { |
| 442 | localHeaderOffset = TryConsumeUnaligned<uint64_t>( |
| 443 | &readPtr, extraFieldStart, extraFieldLength); |
Tianjie | 41cf173 | 2020-04-13 16:29:22 -0700 | [diff] [blame] | 444 | if (!localHeaderOffset.has_value()) return kInvalidOffset; |
Tianjie | 439a46f | 2020-03-18 17:44:30 -0700 | [diff] [blame] | 445 | } |
| 446 | |
Tianjie | cc92463 | 2020-03-26 12:34:44 -0700 | [diff] [blame] | 447 | // calculate how many bytes we read after the data size field. |
| 448 | size_t bytesRead = readPtr - (extraFieldStart + offset); |
| 449 | if (bytesRead == 0) { |
Tianjie | 439a46f | 2020-03-18 17:44:30 -0700 | [diff] [blame] | 450 | ALOGW("Zip: Data size should not be 0 in zip64 extended field"); |
| 451 | return kInvalidFile; |
| 452 | } |
| 453 | |
Tianjie | cc92463 | 2020-03-26 12:34:44 -0700 | [diff] [blame] | 454 | if (dataSize != bytesRead) { |
Tianjie | 439a46f | 2020-03-18 17:44:30 -0700 | [diff] [blame] | 455 | auto localOffsetString = zip32LocalFileHeaderOffset.has_value() |
| 456 | ? std::to_string(zip32LocalFileHeaderOffset.value()) |
| 457 | : "missing"; |
Tianjie | cc92463 | 2020-03-26 12:34:44 -0700 | [diff] [blame] | 458 | ALOGW("Zip: Invalid data size in zip64 extended field, expect %zu , get %" PRIu16 |
Tianjie | 439a46f | 2020-03-18 17:44:30 -0700 | [diff] [blame] | 459 | ", uncompressed size %" PRIu32 ", compressed size %" PRIu32 ", local header offset %s", |
Tianjie | cc92463 | 2020-03-26 12:34:44 -0700 | [diff] [blame] | 460 | bytesRead, dataSize, zip32UncompressedSize, zip32CompressedSize, |
Tianjie | 439a46f | 2020-03-18 17:44:30 -0700 | [diff] [blame] | 461 | localOffsetString.c_str()); |
| 462 | return kInvalidFile; |
| 463 | } |
| 464 | |
Tianjie | 439a46f | 2020-03-18 17:44:30 -0700 | [diff] [blame] | 465 | zip64Info->uncompressed_file_size = uncompressedFileSize; |
| 466 | zip64Info->compressed_file_size = compressedFileSize; |
| 467 | zip64Info->local_header_offset = localHeaderOffset; |
| 468 | return kSuccess; |
| 469 | } |
| 470 | |
| 471 | ALOGW("Zip: zip64 extended info isn't found in the extra field."); |
| 472 | return kInvalidFile; |
| 473 | } |
| 474 | |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 475 | /* |
| 476 | * Parses the Zip archive's Central Directory. Allocates and populates the |
| 477 | * hash table. |
| 478 | * |
| 479 | * Returns 0 on success. |
| 480 | */ |
Tianjie | 439a46f | 2020-03-18 17:44:30 -0700 | [diff] [blame] | 481 | static ZipError ParseZipArchive(ZipArchive* archive) { |
Yurii Zubrytskyi | 3c793c4 | 2021-02-22 16:22:01 -0800 | [diff] [blame] | 482 | SCOPED_SIGBUS_HANDLER(return kIoError); |
| 483 | |
Tianjie Xu | 484de54 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 484 | const uint8_t* const cd_ptr = archive->central_directory.GetBasePtr(); |
| 485 | const size_t cd_length = archive->central_directory.GetMapLength(); |
Tianjie Xu | 53a7ca0 | 2020-03-11 11:59:10 -0700 | [diff] [blame] | 486 | const uint64_t num_entries = archive->num_entries; |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 487 | |
Tianjie Xu | 53a7ca0 | 2020-03-11 11:59:10 -0700 | [diff] [blame] | 488 | if (num_entries <= UINT16_MAX) { |
| 489 | archive->cd_entry_map = CdEntryMapZip32::Create(static_cast<uint16_t>(num_entries)); |
Tianjie Xu | 89d7973 | 2020-03-15 21:23:24 -0700 | [diff] [blame] | 490 | } else { |
Tianjie Xu | 53a7ca0 | 2020-03-11 11:59:10 -0700 | [diff] [blame] | 491 | archive->cd_entry_map = CdEntryMapZip64::Create(); |
Tianjie Xu | 89d7973 | 2020-03-15 21:23:24 -0700 | [diff] [blame] | 492 | } |
Tianjie Xu | a5e894b | 2020-03-05 16:31:23 -0800 | [diff] [blame] | 493 | if (archive->cd_entry_map == nullptr) { |
Elliott Hughes | b59cc23 | 2019-12-16 16:16:16 -0800 | [diff] [blame] | 494 | return kAllocationFailed; |
Tianjie Xu | 597bbea | 2016-10-10 12:11:30 -0700 | [diff] [blame] | 495 | } |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 496 | |
| 497 | /* |
| 498 | * Walk through the central directory, adding entries to the hash |
| 499 | * table and verifying values. |
| 500 | */ |
Narayan Kamath | 38fa647 | 2014-06-09 14:18:14 +0100 | [diff] [blame] | 501 | const uint8_t* const cd_end = cd_ptr + cd_length; |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 502 | const uint8_t* ptr = cd_ptr; |
Tianjie Xu | 53a7ca0 | 2020-03-11 11:59:10 -0700 | [diff] [blame] | 503 | for (uint64_t i = 0; i < num_entries; i++) { |
Tianjie Xu | 91810b5 | 2017-04-05 14:46:27 -0700 | [diff] [blame] | 504 | if (ptr > cd_end - sizeof(CentralDirectoryRecord)) { |
Tianjie Xu | 53a7ca0 | 2020-03-11 11:59:10 -0700 | [diff] [blame] | 505 | ALOGW("Zip: ran off the end (item #%" PRIu64 ", %zu bytes of central directory)", i, |
Elliott Hughes | b59cc23 | 2019-12-16 16:16:16 -0800 | [diff] [blame] | 506 | cd_length); |
Tianjie Xu | 91810b5 | 2017-04-05 14:46:27 -0700 | [diff] [blame] | 507 | #if defined(__ANDROID__) |
| 508 | android_errorWriteLog(0x534e4554, "36392138"); |
| 509 | #endif |
Elliott Hughes | b59cc23 | 2019-12-16 16:16:16 -0800 | [diff] [blame] | 510 | return kInvalidFile; |
Tianjie Xu | 91810b5 | 2017-04-05 14:46:27 -0700 | [diff] [blame] | 511 | } |
| 512 | |
Tianjie | 439a46f | 2020-03-18 17:44:30 -0700 | [diff] [blame] | 513 | auto cdr = reinterpret_cast<const CentralDirectoryRecord*>(ptr); |
Narayan Kamath | 38fa647 | 2014-06-09 14:18:14 +0100 | [diff] [blame] | 514 | if (cdr->record_signature != CentralDirectoryRecord::kSignature) { |
Tianjie Xu | 53a7ca0 | 2020-03-11 11:59:10 -0700 | [diff] [blame] | 515 | ALOGW("Zip: missed a central dir sig (at %" PRIu64 ")", i); |
Elliott Hughes | b59cc23 | 2019-12-16 16:16:16 -0800 | [diff] [blame] | 516 | return kInvalidFile; |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 517 | } |
| 518 | |
Narayan Kamath | 38fa647 | 2014-06-09 14:18:14 +0100 | [diff] [blame] | 519 | const uint16_t file_name_length = cdr->file_name_length; |
| 520 | const uint16_t extra_length = cdr->extra_field_length; |
| 521 | const uint16_t comment_length = cdr->comment_length; |
Piotr Jastrzebski | 22979d0 | 2014-08-15 12:53:00 +0100 | [diff] [blame] | 522 | const uint8_t* file_name = ptr + sizeof(CentralDirectoryRecord); |
| 523 | |
Tianjie Xu | 53a7ca0 | 2020-03-11 11:59:10 -0700 | [diff] [blame] | 524 | if (file_name_length >= cd_length || file_name > cd_end - file_name_length) { |
| 525 | ALOGW("Zip: file name for entry %" PRIu64 |
Elliott Hughes | b92a93d | 2019-06-18 18:23:59 -0700 | [diff] [blame] | 526 | " exceeds the central directory range, file_name_length: %" PRIu16 ", cd_length: %zu", |
| 527 | i, file_name_length, cd_length); |
Elliott Hughes | b59cc23 | 2019-12-16 16:16:16 -0800 | [diff] [blame] | 528 | return kInvalidEntryName; |
Tianjie Xu | 597bbea | 2016-10-10 12:11:30 -0700 | [diff] [blame] | 529 | } |
Tianjie Xu | 53a7ca0 | 2020-03-11 11:59:10 -0700 | [diff] [blame] | 530 | |
| 531 | const uint8_t* extra_field = file_name + file_name_length; |
| 532 | if (extra_length >= cd_length || extra_field > cd_end - extra_length) { |
| 533 | ALOGW("Zip: extra field for entry %" PRIu64 |
| 534 | " exceeds the central directory range, file_name_length: %" PRIu16 ", cd_length: %zu", |
| 535 | i, extra_length, cd_length); |
| 536 | return kInvalidFile; |
| 537 | } |
| 538 | |
| 539 | off64_t local_header_offset = cdr->local_file_header_offset; |
| 540 | if (local_header_offset == UINT32_MAX) { |
Tianjie | 439a46f | 2020-03-18 17:44:30 -0700 | [diff] [blame] | 541 | Zip64ExtendedInfo zip64_info{}; |
| 542 | if (auto status = ParseZip64ExtendedInfoInExtraField( |
| 543 | extra_field, extra_length, cdr->uncompressed_size, cdr->compressed_size, |
| 544 | cdr->local_file_header_offset, &zip64_info); |
| 545 | status != kSuccess) { |
| 546 | return status; |
| 547 | } |
| 548 | CHECK(zip64_info.local_header_offset.has_value()); |
| 549 | local_header_offset = zip64_info.local_header_offset.value(); |
Tianjie Xu | 53a7ca0 | 2020-03-11 11:59:10 -0700 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | if (local_header_offset >= archive->directory_offset) { |
| 553 | ALOGW("Zip: bad LFH offset %" PRId64 " at entry %" PRIu64, |
| 554 | static_cast<int64_t>(local_header_offset), i); |
| 555 | return kInvalidFile; |
| 556 | } |
| 557 | |
Elliott Hughes | b92a93d | 2019-06-18 18:23:59 -0700 | [diff] [blame] | 558 | // Check that file name is valid UTF-8 and doesn't contain NUL (U+0000) characters. |
Narayan Kamath | 1470064 | 2014-12-03 18:22:53 +0000 | [diff] [blame] | 559 | if (!IsValidEntryName(file_name, file_name_length)) { |
Tianjie Xu | 53a7ca0 | 2020-03-11 11:59:10 -0700 | [diff] [blame] | 560 | ALOGW("Zip: invalid file name at entry %" PRIu64, i); |
Elliott Hughes | b59cc23 | 2019-12-16 16:16:16 -0800 | [diff] [blame] | 561 | return kInvalidEntryName; |
Piotr Jastrzebski | 22979d0 | 2014-08-15 12:53:00 +0100 | [diff] [blame] | 562 | } |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 563 | |
Elliott Hughes | b92a93d | 2019-06-18 18:23:59 -0700 | [diff] [blame] | 564 | // Add the CDE filename to the hash table. |
| 565 | std::string_view entry_name{reinterpret_cast<const char*>(file_name), file_name_length}; |
Tianjie Xu | a5e894b | 2020-03-05 16:31:23 -0800 | [diff] [blame] | 566 | if (auto add_result = |
| 567 | archive->cd_entry_map->AddToMap(entry_name, archive->central_directory.GetBasePtr()); |
| 568 | add_result != 0) { |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 569 | ALOGW("Zip: Error adding entry to hash table %d", add_result); |
Dmitriy Ivanov | 4ad3ec7 | 2015-03-06 11:48:47 -0800 | [diff] [blame] | 570 | return add_result; |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 571 | } |
| 572 | |
Narayan Kamath | 38fa647 | 2014-06-09 14:18:14 +0100 | [diff] [blame] | 573 | ptr += sizeof(CentralDirectoryRecord) + file_name_length + extra_length + comment_length; |
| 574 | if ((ptr - cd_ptr) > static_cast<int64_t>(cd_length)) { |
Tianjie Xu | 53a7ca0 | 2020-03-11 11:59:10 -0700 | [diff] [blame] | 575 | ALOGW("Zip: bad CD advance (%tu vs %zu) at entry %" PRIu64, ptr - cd_ptr, cd_length, i); |
Elliott Hughes | b59cc23 | 2019-12-16 16:16:16 -0800 | [diff] [blame] | 576 | return kInvalidFile; |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 577 | } |
| 578 | } |
Narayan Kamath | 33a086d | 2017-08-09 18:32:09 +0100 | [diff] [blame] | 579 | |
| 580 | uint32_t lfh_start_bytes; |
| 581 | if (!archive->mapped_zip.ReadAtOffset(reinterpret_cast<uint8_t*>(&lfh_start_bytes), |
| 582 | sizeof(uint32_t), 0)) { |
| 583 | ALOGW("Zip: Unable to read header for entry at offset == 0."); |
Elliott Hughes | b59cc23 | 2019-12-16 16:16:16 -0800 | [diff] [blame] | 584 | return kInvalidFile; |
Narayan Kamath | 33a086d | 2017-08-09 18:32:09 +0100 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | if (lfh_start_bytes != LocalFileHeader::kSignature) { |
| 588 | ALOGW("Zip: Entry at offset zero has invalid LFH signature %" PRIx32, lfh_start_bytes); |
| 589 | #if defined(__ANDROID__) |
| 590 | android_errorWriteLog(0x534e4554, "64211847"); |
| 591 | #endif |
Elliott Hughes | b59cc23 | 2019-12-16 16:16:16 -0800 | [diff] [blame] | 592 | return kInvalidFile; |
Narayan Kamath | 33a086d | 2017-08-09 18:32:09 +0100 | [diff] [blame] | 593 | } |
| 594 | |
Tianjie Xu | 53a7ca0 | 2020-03-11 11:59:10 -0700 | [diff] [blame] | 595 | ALOGV("+++ zip good scan %" PRIu64 " entries", num_entries); |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 596 | |
Tianjie | 439a46f | 2020-03-18 17:44:30 -0700 | [diff] [blame] | 597 | return kSuccess; |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 598 | } |
| 599 | |
Jiyong Park | 6821cc8 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 600 | static int32_t OpenArchiveInternal(ZipArchive* archive, const char* debug_file_name) { |
Elliott Hughes | b59cc23 | 2019-12-16 16:16:16 -0800 | [diff] [blame] | 601 | int32_t result = MapCentralDirectory(debug_file_name, archive); |
Tianjie Xu | 53a7ca0 | 2020-03-11 11:59:10 -0700 | [diff] [blame] | 602 | return result != kSuccess ? result : ParseZipArchive(archive); |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 603 | } |
| 604 | |
Jiyong Park | 6821cc8 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 605 | int32_t OpenArchiveFd(int fd, const char* debug_file_name, ZipArchiveHandle* handle, |
| 606 | bool assume_ownership) { |
Ryan Mitchell | 0edeea9 | 2020-03-09 09:33:46 -0700 | [diff] [blame] | 607 | ZipArchive* archive = new ZipArchive(MappedZipFile(fd), assume_ownership); |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 608 | *handle = archive; |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 609 | return OpenArchiveInternal(archive, debug_file_name); |
| 610 | } |
| 611 | |
Ryan Mitchell | 0edeea9 | 2020-03-09 09:33:46 -0700 | [diff] [blame] | 612 | int32_t OpenArchiveFdRange(int fd, const char* debug_file_name, ZipArchiveHandle* handle, |
| 613 | off64_t length, off64_t offset, bool assume_ownership) { |
| 614 | ZipArchive* archive = new ZipArchive(MappedZipFile(fd, length, offset), assume_ownership); |
| 615 | *handle = archive; |
| 616 | |
| 617 | if (length < 0) { |
| 618 | ALOGW("Invalid zip length %" PRId64, length); |
| 619 | return kIoError; |
| 620 | } |
| 621 | |
| 622 | if (offset < 0) { |
| 623 | ALOGW("Invalid zip offset %" PRId64, offset); |
| 624 | return kIoError; |
| 625 | } |
| 626 | |
| 627 | return OpenArchiveInternal(archive, debug_file_name); |
| 628 | } |
| 629 | |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 630 | int32_t OpenArchive(const char* fileName, ZipArchiveHandle* handle) { |
Nick Kralevich | f769747 | 2018-12-18 12:48:06 -0800 | [diff] [blame] | 631 | const int fd = ::android::base::utf8::open(fileName, O_RDONLY | O_BINARY | O_CLOEXEC, 0); |
Ryan Mitchell | 0edeea9 | 2020-03-09 09:33:46 -0700 | [diff] [blame] | 632 | ZipArchive* archive = new ZipArchive(MappedZipFile(fd), true); |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 633 | *handle = archive; |
| 634 | |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 635 | if (fd < 0) { |
| 636 | ALOGW("Unable to open '%s': %s", fileName, strerror(errno)); |
| 637 | return kIoError; |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 638 | } |
Dmitriy Ivanov | 84485cf | 2014-07-15 19:33:00 -0700 | [diff] [blame] | 639 | |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 640 | return OpenArchiveInternal(archive, fileName); |
| 641 | } |
| 642 | |
Elliott Hughes | 62c2d53 | 2019-10-22 11:44:50 -0700 | [diff] [blame] | 643 | int32_t OpenArchiveFromMemory(const void* address, size_t length, const char* debug_file_name, |
Jiyong Park | 6821cc8 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 644 | ZipArchiveHandle* handle) { |
Tianjie Xu | 484de54 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 645 | ZipArchive* archive = new ZipArchive(address, length); |
| 646 | *handle = archive; |
| 647 | return OpenArchiveInternal(archive, debug_file_name); |
| 648 | } |
| 649 | |
Elliott Hughes | dcd3f92 | 2019-10-25 09:57:58 -0700 | [diff] [blame] | 650 | ZipArchiveInfo GetArchiveInfo(ZipArchiveHandle archive) { |
| 651 | ZipArchiveInfo result; |
| 652 | result.archive_size = archive->mapped_zip.GetFileLength(); |
| 653 | result.entry_count = archive->num_entries; |
| 654 | return result; |
| 655 | } |
| 656 | |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 657 | /* |
| 658 | * Close a ZipArchive, closing the file and freeing the contents. |
| 659 | */ |
Ryan Prichard | 30236a1 | 2018-10-10 22:41:14 -0700 | [diff] [blame] | 660 | void CloseArchive(ZipArchiveHandle archive) { |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 661 | ALOGV("Closing archive %p", archive); |
Neil Fuller | 29a4302 | 2014-07-25 14:43:04 +0100 | [diff] [blame] | 662 | delete archive; |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 663 | } |
| 664 | |
Tianjie | 26ee1db | 2020-04-01 23:08:34 -0700 | [diff] [blame] | 665 | static int32_t ValidateDataDescriptor(MappedZipFile& mapped_zip, const ZipEntry64* entry) { |
Yurii Zubrytskyi | 3c793c4 | 2021-02-22 16:22:01 -0800 | [diff] [blame] | 666 | SCOPED_SIGBUS_HANDLER(return kIoError); |
| 667 | |
Tianjie | cc92463 | 2020-03-26 12:34:44 -0700 | [diff] [blame] | 668 | // Maximum possible size for data descriptor: 2 * 4 + 2 * 8 = 24 bytes |
Tianjie | 41cf173 | 2020-04-13 16:29:22 -0700 | [diff] [blame] | 669 | // The zip format doesn't specify the size of data descriptor. But we won't read OOB here even |
| 670 | // if the descriptor isn't present. Because the size cd + eocd in the end of the zipfile is |
| 671 | // larger than 24 bytes. And if the descriptor contains invalid data, we'll abort due to |
| 672 | // kInconsistentInformation. |
Tianjie | cc92463 | 2020-03-26 12:34:44 -0700 | [diff] [blame] | 673 | uint8_t ddBuf[24]; |
Adam Lesinski | 367ccf6 | 2017-06-19 10:27:38 -0700 | [diff] [blame] | 674 | off64_t offset = entry->offset; |
| 675 | if (entry->method != kCompressStored) { |
| 676 | offset += entry->compressed_length; |
| 677 | } else { |
| 678 | offset += entry->uncompressed_length; |
| 679 | } |
| 680 | |
| 681 | if (!mapped_zip.ReadAtOffset(ddBuf, sizeof(ddBuf), offset)) { |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 682 | return kIoError; |
| 683 | } |
| 684 | |
Narayan Kamath | 38fa647 | 2014-06-09 14:18:14 +0100 | [diff] [blame] | 685 | const uint32_t ddSignature = *(reinterpret_cast<const uint32_t*>(ddBuf)); |
Tianjie | cc92463 | 2020-03-26 12:34:44 -0700 | [diff] [blame] | 686 | uint8_t* ddReadPtr = (ddSignature == DataDescriptor::kOptSignature) ? ddBuf + 4 : ddBuf; |
| 687 | DataDescriptor descriptor{}; |
| 688 | descriptor.crc32 = ConsumeUnaligned<uint32_t>(&ddReadPtr); |
Kelvin Zhang | da618a3 | 2020-09-14 14:59:21 -0400 | [diff] [blame] | 689 | // Don't use entry->zip64_format_size, because that is set to true even if |
| 690 | // both compressed/uncompressed size are < 0xFFFFFFFF. |
| 691 | constexpr auto u32max = std::numeric_limits<uint32_t>::max(); |
| 692 | if (entry->compressed_length >= u32max || |
| 693 | entry->uncompressed_length >= u32max) { |
Tianjie | cc92463 | 2020-03-26 12:34:44 -0700 | [diff] [blame] | 694 | descriptor.compressed_size = ConsumeUnaligned<uint64_t>(&ddReadPtr); |
| 695 | descriptor.uncompressed_size = ConsumeUnaligned<uint64_t>(&ddReadPtr); |
| 696 | } else { |
| 697 | descriptor.compressed_size = ConsumeUnaligned<uint32_t>(&ddReadPtr); |
| 698 | descriptor.uncompressed_size = ConsumeUnaligned<uint32_t>(&ddReadPtr); |
| 699 | } |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 700 | |
Narayan Kamath | 9775435 | 2017-06-05 13:21:12 +0100 | [diff] [blame] | 701 | // Validate that the values in the data descriptor match those in the central |
| 702 | // directory. |
Tianjie | cc92463 | 2020-03-26 12:34:44 -0700 | [diff] [blame] | 703 | if (entry->compressed_length != descriptor.compressed_size || |
| 704 | entry->uncompressed_length != descriptor.uncompressed_size || |
| 705 | entry->crc32 != descriptor.crc32) { |
Tianjie | 26ee1db | 2020-04-01 23:08:34 -0700 | [diff] [blame] | 706 | ALOGW("Zip: size/crc32 mismatch. expected {%" PRIu64 ", %" PRIu64 ", %" PRIx32 |
Tianjie | cc92463 | 2020-03-26 12:34:44 -0700 | [diff] [blame] | 707 | "}, was {%" PRIu64 ", %" PRIu64 ", %" PRIx32 "}", |
Narayan Kamath | 9775435 | 2017-06-05 13:21:12 +0100 | [diff] [blame] | 708 | entry->compressed_length, entry->uncompressed_length, entry->crc32, |
Tianjie | cc92463 | 2020-03-26 12:34:44 -0700 | [diff] [blame] | 709 | descriptor.compressed_size, descriptor.uncompressed_size, descriptor.crc32); |
Narayan Kamath | 9775435 | 2017-06-05 13:21:12 +0100 | [diff] [blame] | 710 | return kInconsistentInformation; |
| 711 | } |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 712 | |
| 713 | return 0; |
| 714 | } |
| 715 | |
Tianjie Xu | a5e894b | 2020-03-05 16:31:23 -0800 | [diff] [blame] | 716 | static int32_t FindEntry(const ZipArchive* archive, std::string_view entryName, |
Tianjie | 26ee1db | 2020-04-01 23:08:34 -0700 | [diff] [blame] | 717 | const uint64_t nameOffset, ZipEntry64* data) { |
Yurii Zubrytskyi | 3c793c4 | 2021-02-22 16:22:01 -0800 | [diff] [blame] | 718 | std::vector<uint8_t> name_buf; |
| 719 | std::vector<uint8_t> local_extra_field; |
| 720 | SCOPED_SIGBUS_HANDLER({ |
| 721 | incfs::util::clearAndFree(name_buf); |
| 722 | incfs::util::clearAndFree(local_extra_field); |
| 723 | return kIoError; |
| 724 | }); |
| 725 | |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 726 | // Recover the start of the central directory entry from the filename |
| 727 | // pointer. The filename is the first entry past the fixed-size data, |
| 728 | // so we can just subtract back from that. |
Elliott Hughes | b92a93d | 2019-06-18 18:23:59 -0700 | [diff] [blame] | 729 | const uint8_t* base_ptr = archive->central_directory.GetBasePtr(); |
Tianjie Xu | a5e894b | 2020-03-05 16:31:23 -0800 | [diff] [blame] | 730 | const uint8_t* ptr = base_ptr + nameOffset; |
Narayan Kamath | 38fa647 | 2014-06-09 14:18:14 +0100 | [diff] [blame] | 731 | ptr -= sizeof(CentralDirectoryRecord); |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 732 | |
Elliott Hughes | 27d0eaa | 2020-07-21 16:43:46 -0700 | [diff] [blame] | 733 | // This is the base of our mmapped region, we have to check that |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 734 | // the name that's in the hash table is a pointer to a location within |
| 735 | // this mapped region. |
Tianjie Xu | 484de54 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 736 | if (ptr < base_ptr || ptr > base_ptr + archive->central_directory.GetMapLength()) { |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 737 | ALOGW("Zip: Invalid entry pointer"); |
| 738 | return kInvalidOffset; |
| 739 | } |
| 740 | |
Tianjie | 439a46f | 2020-03-18 17:44:30 -0700 | [diff] [blame] | 741 | auto cdr = reinterpret_cast<const CentralDirectoryRecord*>(ptr); |
Narayan Kamath | 38fa647 | 2014-06-09 14:18:14 +0100 | [diff] [blame] | 742 | |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 743 | // The offset of the start of the central directory in the zipfile. |
Elliott Hughes | 27d0eaa | 2020-07-21 16:43:46 -0700 | [diff] [blame] | 744 | // We keep this lying around so that we can check all our lengths |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 745 | // and our per-file structures. |
| 746 | const off64_t cd_offset = archive->directory_offset; |
| 747 | |
| 748 | // Fill out the compression method, modification time, crc32 |
| 749 | // and other interesting attributes from the central directory. These |
| 750 | // will later be compared against values from the local file header. |
Narayan Kamath | 38fa647 | 2014-06-09 14:18:14 +0100 | [diff] [blame] | 751 | data->method = cdr->compression_method; |
beonit | 4063b24 | 2015-07-18 02:08:16 +0900 | [diff] [blame] | 752 | data->mod_time = cdr->last_mod_date << 16 | cdr->last_mod_time; |
Narayan Kamath | 38fa647 | 2014-06-09 14:18:14 +0100 | [diff] [blame] | 753 | data->crc32 = cdr->crc32; |
| 754 | data->compressed_length = cdr->compressed_size; |
| 755 | data->uncompressed_length = cdr->uncompressed_size; |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 756 | |
| 757 | // Figure out the local header offset from the central directory. The |
| 758 | // actual file data will begin after the local header and the name / |
| 759 | // extra comments. |
Tianjie Xu | 53a7ca0 | 2020-03-11 11:59:10 -0700 | [diff] [blame] | 760 | off64_t local_header_offset = cdr->local_file_header_offset; |
| 761 | // One of the info field is UINT32_MAX, try to parse the real value in the zip64 extended info in |
| 762 | // the extra field. |
| 763 | if (cdr->uncompressed_size == UINT32_MAX || cdr->compressed_size == UINT32_MAX || |
| 764 | cdr->local_file_header_offset == UINT32_MAX) { |
Tianjie | 439a46f | 2020-03-18 17:44:30 -0700 | [diff] [blame] | 765 | const uint8_t* extra_field = ptr + sizeof(CentralDirectoryRecord) + cdr->file_name_length; |
| 766 | Zip64ExtendedInfo zip64_info{}; |
| 767 | if (auto status = ParseZip64ExtendedInfoInExtraField( |
| 768 | extra_field, cdr->extra_field_length, cdr->uncompressed_size, cdr->compressed_size, |
| 769 | cdr->local_file_header_offset, &zip64_info); |
| 770 | status != kSuccess) { |
| 771 | return status; |
| 772 | } |
| 773 | |
Tianjie | 26ee1db | 2020-04-01 23:08:34 -0700 | [diff] [blame] | 774 | data->uncompressed_length = zip64_info.uncompressed_file_size.value_or(cdr->uncompressed_size); |
| 775 | data->compressed_length = zip64_info.compressed_file_size.value_or(cdr->compressed_size); |
Tianjie | cc92463 | 2020-03-26 12:34:44 -0700 | [diff] [blame] | 776 | local_header_offset = zip64_info.local_header_offset.value_or(local_header_offset); |
| 777 | data->zip64_format_size = |
| 778 | cdr->uncompressed_size == UINT32_MAX || cdr->compressed_size == UINT32_MAX; |
Tianjie Xu | 53a7ca0 | 2020-03-11 11:59:10 -0700 | [diff] [blame] | 779 | } |
| 780 | |
Elliott Hughes | 88d1cbf | 2021-02-08 16:02:20 -0800 | [diff] [blame] | 781 | off64_t local_header_end; |
| 782 | if (__builtin_add_overflow(local_header_offset, sizeof(LocalFileHeader), &local_header_end) || |
| 783 | local_header_end >= cd_offset) { |
| 784 | // We tested >= because the name that follows can't be zero length. |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 785 | ALOGW("Zip: bad local hdr offset in zip"); |
| 786 | return kInvalidOffset; |
| 787 | } |
| 788 | |
Narayan Kamath | 38fa647 | 2014-06-09 14:18:14 +0100 | [diff] [blame] | 789 | uint8_t lfh_buf[sizeof(LocalFileHeader)]; |
Tianjie Xu | 484de54 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 790 | if (!archive->mapped_zip.ReadAtOffset(lfh_buf, sizeof(lfh_buf), local_header_offset)) { |
Dmitriy Ivanov | d47a85e | 2015-03-06 10:50:56 -0800 | [diff] [blame] | 791 | ALOGW("Zip: failed reading lfh name from offset %" PRId64, |
Jiyong Park | 6821cc8 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 792 | static_cast<int64_t>(local_header_offset)); |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 793 | return kIoError; |
| 794 | } |
| 795 | |
Tianjie | 439a46f | 2020-03-18 17:44:30 -0700 | [diff] [blame] | 796 | auto lfh = reinterpret_cast<const LocalFileHeader*>(lfh_buf); |
Narayan Kamath | 38fa647 | 2014-06-09 14:18:14 +0100 | [diff] [blame] | 797 | if (lfh->lfh_signature != LocalFileHeader::kSignature) { |
Mark Salyzyn | 285fe0b | 2014-03-14 14:26:22 -0700 | [diff] [blame] | 798 | ALOGW("Zip: didn't find signature at start of lfh, offset=%" PRId64, |
Jiyong Park | 6821cc8 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 799 | static_cast<int64_t>(local_header_offset)); |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 800 | return kInvalidOffset; |
| 801 | } |
| 802 | |
Tianjie | 439a46f | 2020-03-18 17:44:30 -0700 | [diff] [blame] | 803 | // Check that the local file header name matches the declared name in the central directory. |
| 804 | CHECK_LE(entryName.size(), UINT16_MAX); |
Elliott Hughes | 88d1cbf | 2021-02-08 16:02:20 -0800 | [diff] [blame] | 805 | auto name_length = static_cast<uint16_t>(entryName.size()); |
| 806 | if (lfh->file_name_length != name_length) { |
Tianjie | 439a46f | 2020-03-18 17:44:30 -0700 | [diff] [blame] | 807 | ALOGW("Zip: lfh name length did not match central directory for %s: %" PRIu16 " %" PRIu16, |
Elliott Hughes | 88d1cbf | 2021-02-08 16:02:20 -0800 | [diff] [blame] | 808 | std::string(entryName).c_str(), lfh->file_name_length, name_length); |
Tianjie | 439a46f | 2020-03-18 17:44:30 -0700 | [diff] [blame] | 809 | return kInconsistentInformation; |
| 810 | } |
Elliott Hughes | 88d1cbf | 2021-02-08 16:02:20 -0800 | [diff] [blame] | 811 | off64_t name_offset; |
| 812 | if (__builtin_add_overflow(local_header_offset, sizeof(LocalFileHeader), &name_offset)) { |
| 813 | ALOGW("Zip: lfh name offset invalid"); |
| 814 | return kInvalidOffset; |
| 815 | } |
| 816 | off64_t name_end; |
| 817 | if (__builtin_add_overflow(name_offset, name_length, &name_end) || name_end > cd_offset) { |
| 818 | // We tested > cd_offset here because the file data that follows can be zero length. |
| 819 | ALOGW("Zip: lfh name length invalid"); |
Tianjie | 439a46f | 2020-03-18 17:44:30 -0700 | [diff] [blame] | 820 | return kInvalidOffset; |
| 821 | } |
| 822 | |
Yurii Zubrytskyi | 3c793c4 | 2021-02-22 16:22:01 -0800 | [diff] [blame] | 823 | name_buf.resize(name_length); |
Elliott Hughes | 88d1cbf | 2021-02-08 16:02:20 -0800 | [diff] [blame] | 824 | if (!archive->mapped_zip.ReadAtOffset(name_buf.data(), name_buf.size(), name_offset)) { |
Tianjie | 439a46f | 2020-03-18 17:44:30 -0700 | [diff] [blame] | 825 | ALOGW("Zip: failed reading lfh name from offset %" PRId64, static_cast<int64_t>(name_offset)); |
| 826 | return kIoError; |
| 827 | } |
Elliott Hughes | 88d1cbf | 2021-02-08 16:02:20 -0800 | [diff] [blame] | 828 | if (memcmp(entryName.data(), name_buf.data(), name_buf.size()) != 0) { |
Tianjie | 439a46f | 2020-03-18 17:44:30 -0700 | [diff] [blame] | 829 | ALOGW("Zip: lfh name did not match central directory"); |
| 830 | return kInconsistentInformation; |
| 831 | } |
| 832 | |
| 833 | uint64_t lfh_uncompressed_size = lfh->uncompressed_size; |
| 834 | uint64_t lfh_compressed_size = lfh->compressed_size; |
| 835 | if (lfh_uncompressed_size == UINT32_MAX || lfh_compressed_size == UINT32_MAX) { |
Tianjie | cc92463 | 2020-03-26 12:34:44 -0700 | [diff] [blame] | 836 | if (lfh_uncompressed_size != UINT32_MAX || lfh_compressed_size != UINT32_MAX) { |
| 837 | ALOGW( |
| 838 | "Zip: The zip64 extended field in the local header MUST include BOTH original and " |
| 839 | "compressed file size fields."); |
| 840 | return kInvalidFile; |
| 841 | } |
| 842 | |
Tianjie | 439a46f | 2020-03-18 17:44:30 -0700 | [diff] [blame] | 843 | const off64_t lfh_extra_field_offset = name_offset + lfh->file_name_length; |
| 844 | const uint16_t lfh_extra_field_size = lfh->extra_field_length; |
| 845 | if (lfh_extra_field_offset > cd_offset - lfh_extra_field_size) { |
| 846 | ALOGW("Zip: extra field has a bad size for entry %s", std::string(entryName).c_str()); |
| 847 | return kInvalidOffset; |
| 848 | } |
| 849 | |
Yurii Zubrytskyi | 3c793c4 | 2021-02-22 16:22:01 -0800 | [diff] [blame] | 850 | local_extra_field.resize(lfh_extra_field_size); |
Tianjie | 439a46f | 2020-03-18 17:44:30 -0700 | [diff] [blame] | 851 | if (!archive->mapped_zip.ReadAtOffset(local_extra_field.data(), lfh_extra_field_size, |
| 852 | lfh_extra_field_offset)) { |
| 853 | ALOGW("Zip: failed reading lfh extra field from offset %" PRId64, lfh_extra_field_offset); |
| 854 | return kIoError; |
| 855 | } |
| 856 | |
| 857 | Zip64ExtendedInfo zip64_info{}; |
| 858 | if (auto status = ParseZip64ExtendedInfoInExtraField( |
| 859 | local_extra_field.data(), lfh_extra_field_size, lfh->uncompressed_size, |
| 860 | lfh->compressed_size, std::nullopt, &zip64_info); |
| 861 | status != kSuccess) { |
| 862 | return status; |
| 863 | } |
| 864 | |
| 865 | CHECK(zip64_info.uncompressed_file_size.has_value()); |
| 866 | CHECK(zip64_info.compressed_file_size.has_value()); |
| 867 | lfh_uncompressed_size = zip64_info.uncompressed_file_size.value(); |
| 868 | lfh_compressed_size = zip64_info.compressed_file_size.value(); |
| 869 | } |
| 870 | |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 871 | // Paranoia: Match the values specified in the local file header |
| 872 | // to those specified in the central directory. |
Adam Lesinski | 253cc06 | 2017-04-06 18:55:47 -0700 | [diff] [blame] | 873 | |
Narayan Kamath | 9775435 | 2017-06-05 13:21:12 +0100 | [diff] [blame] | 874 | // Warn if central directory and local file header don't agree on the use |
| 875 | // of a trailing Data Descriptor. The reference implementation is inconsistent |
| 876 | // and appears to use the LFH value during extraction (unzip) but the CD value |
| 877 | // while displayng information about archives (zipinfo). The spec remains |
| 878 | // silent on this inconsistency as well. |
| 879 | // |
| 880 | // For now, always use the version from the LFH but make sure that the values |
| 881 | // specified in the central directory match those in the data descriptor. |
| 882 | // |
| 883 | // NOTE: It's also worth noting that unzip *does* warn about inconsistencies in |
| 884 | // bit 11 (EFS: The language encoding flag, marking that filename and comment are |
| 885 | // encoded using UTF-8). This implementation does not check for the presence of |
| 886 | // that flag and always enforces that entry names are valid UTF-8. |
| 887 | if ((lfh->gpb_flags & kGPBDDFlagMask) != (cdr->gpb_flags & kGPBDDFlagMask)) { |
| 888 | ALOGW("Zip: gpb flag mismatch at bit 3. expected {%04" PRIx16 "}, was {%04" PRIx16 "}", |
Adam Lesinski | 253cc06 | 2017-04-06 18:55:47 -0700 | [diff] [blame] | 889 | cdr->gpb_flags, lfh->gpb_flags); |
Adam Lesinski | 253cc06 | 2017-04-06 18:55:47 -0700 | [diff] [blame] | 890 | } |
| 891 | |
| 892 | // If there is no trailing data descriptor, verify that the central directory and local file |
| 893 | // header agree on the crc, compressed, and uncompressed sizes of the entry. |
Narayan Kamath | 38fa647 | 2014-06-09 14:18:14 +0100 | [diff] [blame] | 894 | if ((lfh->gpb_flags & kGPBDDFlagMask) == 0) { |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 895 | data->has_data_descriptor = 0; |
Tianjie | 439a46f | 2020-03-18 17:44:30 -0700 | [diff] [blame] | 896 | if (data->compressed_length != lfh_compressed_size || |
| 897 | data->uncompressed_length != lfh_uncompressed_size || data->crc32 != lfh->crc32) { |
Tianjie | 26ee1db | 2020-04-01 23:08:34 -0700 | [diff] [blame] | 898 | ALOGW("Zip: size/crc32 mismatch. expected {%" PRIu64 ", %" PRIu64 ", %" PRIx32 |
Tianjie | 439a46f | 2020-03-18 17:44:30 -0700 | [diff] [blame] | 899 | "}, was {%" PRIu64 ", %" PRIu64 ", %" PRIx32 "}", |
| 900 | data->compressed_length, data->uncompressed_length, data->crc32, lfh_compressed_size, |
| 901 | lfh_uncompressed_size, lfh->crc32); |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 902 | return kInconsistentInformation; |
| 903 | } |
| 904 | } else { |
| 905 | data->has_data_descriptor = 1; |
| 906 | } |
| 907 | |
Elliott Hughes | 6702cef | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 908 | // 4.4.2.1: the upper byte of `version_made_by` gives the source OS. Unix is 3. |
Elliott Hughes | dcd3f92 | 2019-10-25 09:57:58 -0700 | [diff] [blame] | 909 | data->version_made_by = cdr->version_made_by; |
Elliott Hughes | 344113b | 2019-10-28 21:35:52 -0700 | [diff] [blame] | 910 | data->external_file_attributes = cdr->external_file_attributes; |
Elliott Hughes | dcd3f92 | 2019-10-25 09:57:58 -0700 | [diff] [blame] | 911 | if ((data->version_made_by >> 8) == 3) { |
Elliott Hughes | 6702cef | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 912 | data->unix_mode = (cdr->external_file_attributes >> 16) & 0xffff; |
| 913 | } else { |
| 914 | data->unix_mode = 0777; |
| 915 | } |
| 916 | |
Elliott Hughes | 344113b | 2019-10-28 21:35:52 -0700 | [diff] [blame] | 917 | // 4.4.4: general purpose bit flags. |
| 918 | data->gpbf = lfh->gpb_flags; |
| 919 | |
Elliott Hughes | dcd3f92 | 2019-10-25 09:57:58 -0700 | [diff] [blame] | 920 | // 4.4.14: the lowest bit of the internal file attributes field indicates text. |
| 921 | // Currently only needed to implement zipinfo. |
| 922 | data->is_text = (cdr->internal_file_attributes & 1); |
| 923 | |
Jiyong Park | 6821cc8 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 924 | const off64_t data_offset = local_header_offset + sizeof(LocalFileHeader) + |
| 925 | lfh->file_name_length + lfh->extra_field_length; |
Narayan Kamath | 1ebe794 | 2014-01-24 12:32:39 +0000 | [diff] [blame] | 926 | if (data_offset > cd_offset) { |
Dmitriy Ivanov | d47a85e | 2015-03-06 10:50:56 -0800 | [diff] [blame] | 927 | ALOGW("Zip: bad data offset %" PRId64 " in zip", static_cast<int64_t>(data_offset)); |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 928 | return kInvalidOffset; |
| 929 | } |
| 930 | |
Tianjie | 26ee1db | 2020-04-01 23:08:34 -0700 | [diff] [blame] | 931 | if (data->compressed_length > cd_offset - data_offset) { |
| 932 | ALOGW("Zip: bad compressed length in zip (%" PRId64 " + %" PRIu64 " > %" PRId64 ")", |
Jiyong Park | 6821cc8 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 933 | static_cast<int64_t>(data_offset), data->compressed_length, |
| 934 | static_cast<int64_t>(cd_offset)); |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 935 | return kInvalidOffset; |
| 936 | } |
| 937 | |
Tianjie | 26ee1db | 2020-04-01 23:08:34 -0700 | [diff] [blame] | 938 | if (data->method == kCompressStored && data->uncompressed_length > cd_offset - data_offset) { |
| 939 | ALOGW("Zip: bad uncompressed length in zip (%" PRId64 " + %" PRIu64 " > %" PRId64 ")", |
Jiyong Park | 6821cc8 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 940 | static_cast<int64_t>(data_offset), data->uncompressed_length, |
| 941 | static_cast<int64_t>(cd_offset)); |
| 942 | return kInvalidOffset; |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 943 | } |
| 944 | |
| 945 | data->offset = data_offset; |
| 946 | return 0; |
| 947 | } |
| 948 | |
| 949 | struct IterationHandle { |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 950 | ZipArchive* archive; |
Piotr Jastrzebski | dd82cf4 | 2014-08-18 11:37:45 +0100 | [diff] [blame] | 951 | |
Songchun Fan | 2884066 | 2020-03-24 09:15:51 -0700 | [diff] [blame] | 952 | std::function<bool(std::string_view)> matcher; |
Elliott Hughes | 4e78d05 | 2019-05-08 10:44:06 -0700 | [diff] [blame] | 953 | |
| 954 | uint32_t position = 0; |
| 955 | |
Songchun Fan | 2884066 | 2020-03-24 09:15:51 -0700 | [diff] [blame] | 956 | IterationHandle(ZipArchive* archive, std::function<bool(std::string_view)> in_matcher) |
| 957 | : archive(archive), matcher(std::move(in_matcher)) {} |
| 958 | |
| 959 | bool Match(std::string_view entry_name) const { return matcher(entry_name); } |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 960 | }; |
| 961 | |
Ryan Prichard | 30236a1 | 2018-10-10 22:41:14 -0700 | [diff] [blame] | 962 | int32_t StartIteration(ZipArchiveHandle archive, void** cookie_ptr, |
Elliott Hughes | 4e78d05 | 2019-05-08 10:44:06 -0700 | [diff] [blame] | 963 | const std::string_view optional_prefix, |
| 964 | const std::string_view optional_suffix) { |
Elliott Hughes | 4e78d05 | 2019-05-08 10:44:06 -0700 | [diff] [blame] | 965 | if (optional_prefix.size() > static_cast<size_t>(UINT16_MAX) || |
| 966 | optional_suffix.size() > static_cast<size_t>(UINT16_MAX)) { |
| 967 | ALOGW("Zip: prefix/suffix too long"); |
| 968 | return kInvalidEntryName; |
| 969 | } |
Songchun Fan | 2884066 | 2020-03-24 09:15:51 -0700 | [diff] [blame] | 970 | auto matcher = [prefix = std::string(optional_prefix), |
| 971 | suffix = std::string(optional_suffix)](std::string_view name) mutable { |
| 972 | return android::base::StartsWith(name, prefix) && android::base::EndsWith(name, suffix); |
| 973 | }; |
| 974 | return StartIteration(archive, cookie_ptr, std::move(matcher)); |
| 975 | } |
| 976 | |
| 977 | int32_t StartIteration(ZipArchiveHandle archive, void** cookie_ptr, |
| 978 | std::function<bool(std::string_view)> matcher) { |
| 979 | if (archive == nullptr || archive->cd_entry_map == nullptr) { |
| 980 | ALOGW("Zip: Invalid ZipArchiveHandle"); |
| 981 | return kInvalidHandle; |
| 982 | } |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 983 | |
Tianjie Xu | a5e894b | 2020-03-05 16:31:23 -0800 | [diff] [blame] | 984 | archive->cd_entry_map->ResetIteration(); |
Yurii Zubrytskyi | 3c793c4 | 2021-02-22 16:22:01 -0800 | [diff] [blame] | 985 | *cookie_ptr = new IterationHandle(archive, std::move(matcher)); |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 986 | return 0; |
| 987 | } |
| 988 | |
Piotr Jastrzebski | 183e314 | 2014-08-08 14:02:17 +0100 | [diff] [blame] | 989 | void EndIteration(void* cookie) { |
Piotr Jastrzebski | c7a3ce4 | 2014-08-11 16:35:11 +0100 | [diff] [blame] | 990 | delete reinterpret_cast<IterationHandle*>(cookie); |
Piotr Jastrzebski | 183e314 | 2014-08-08 14:02:17 +0100 | [diff] [blame] | 991 | } |
| 992 | |
Tianjie | 26ee1db | 2020-04-01 23:08:34 -0700 | [diff] [blame] | 993 | int32_t ZipEntry::CopyFromZipEntry64(ZipEntry* dst, const ZipEntry64* src) { |
| 994 | if (src->compressed_length > UINT32_MAX || src->uncompressed_length > UINT32_MAX) { |
| 995 | ALOGW( |
| 996 | "Zip: the entry size is too large to fit into the 32 bits ZipEntry, uncompressed " |
| 997 | "length %" PRIu64 ", compressed length %" PRIu64, |
| 998 | src->uncompressed_length, src->compressed_length); |
| 999 | return kUnsupportedEntrySize; |
| 1000 | } |
| 1001 | |
| 1002 | *dst = *src; |
| 1003 | dst->uncompressed_length = static_cast<uint32_t>(src->uncompressed_length); |
| 1004 | dst->compressed_length = static_cast<uint32_t>(src->compressed_length); |
| 1005 | return kSuccess; |
| 1006 | } |
| 1007 | |
Elliott Hughes | 0b4bf2f | 2019-05-03 22:38:44 -0700 | [diff] [blame] | 1008 | int32_t FindEntry(const ZipArchiveHandle archive, const std::string_view entryName, |
| 1009 | ZipEntry* data) { |
Tianjie | 26ee1db | 2020-04-01 23:08:34 -0700 | [diff] [blame] | 1010 | ZipEntry64 entry64; |
| 1011 | if (auto status = FindEntry(archive, entryName, &entry64); status != kSuccess) { |
| 1012 | return status; |
| 1013 | } |
| 1014 | |
| 1015 | return ZipEntry::CopyFromZipEntry64(data, &entry64); |
| 1016 | } |
| 1017 | |
| 1018 | int32_t FindEntry(const ZipArchiveHandle archive, const std::string_view entryName, |
| 1019 | ZipEntry64* data) { |
Elliott Hughes | 0b4bf2f | 2019-05-03 22:38:44 -0700 | [diff] [blame] | 1020 | if (entryName.empty() || entryName.size() > static_cast<size_t>(UINT16_MAX)) { |
| 1021 | ALOGW("Zip: Invalid filename of length %zu", entryName.size()); |
| 1022 | return kInvalidEntryName; |
| 1023 | } |
| 1024 | |
Tianjie Xu | a5e894b | 2020-03-05 16:31:23 -0800 | [diff] [blame] | 1025 | const auto [result, offset] = |
| 1026 | archive->cd_entry_map->GetCdEntryOffset(entryName, archive->central_directory.GetBasePtr()); |
| 1027 | if (result != 0) { |
Elliott Hughes | 0b4bf2f | 2019-05-03 22:38:44 -0700 | [diff] [blame] | 1028 | ALOGV("Zip: Could not find entry %.*s", static_cast<int>(entryName.size()), entryName.data()); |
Tianjie Xu | a5e894b | 2020-03-05 16:31:23 -0800 | [diff] [blame] | 1029 | return static_cast<int32_t>(result); // kEntryNotFound is safe to truncate. |
Elliott Hughes | 0b4bf2f | 2019-05-03 22:38:44 -0700 | [diff] [blame] | 1030 | } |
Elliott Hughes | c640ea9 | 2019-05-07 09:27:59 -0700 | [diff] [blame] | 1031 | // We know there are at most hash_table_size entries, safe to truncate. |
Tianjie Xu | a5e894b | 2020-03-05 16:31:23 -0800 | [diff] [blame] | 1032 | return FindEntry(archive, entryName, offset, data); |
Elliott Hughes | 0b4bf2f | 2019-05-03 22:38:44 -0700 | [diff] [blame] | 1033 | } |
| 1034 | |
Elliott Hughes | 21f9ab0 | 2019-05-22 18:56:41 -0700 | [diff] [blame] | 1035 | int32_t Next(void* cookie, ZipEntry* data, std::string* name) { |
Tianjie | 26ee1db | 2020-04-01 23:08:34 -0700 | [diff] [blame] | 1036 | ZipEntry64 entry64; |
| 1037 | if (auto status = Next(cookie, &entry64, name); status != kSuccess) { |
| 1038 | return status; |
| 1039 | } |
| 1040 | |
| 1041 | return ZipEntry::CopyFromZipEntry64(data, &entry64); |
| 1042 | } |
| 1043 | |
| 1044 | int32_t Next(void* cookie, ZipEntry* data, std::string_view* name) { |
| 1045 | ZipEntry64 entry64; |
| 1046 | if (auto status = Next(cookie, &entry64, name); status != kSuccess) { |
| 1047 | return status; |
| 1048 | } |
| 1049 | |
| 1050 | return ZipEntry::CopyFromZipEntry64(data, &entry64); |
| 1051 | } |
| 1052 | |
| 1053 | int32_t Next(void* cookie, ZipEntry64* data, std::string* name) { |
Elliott Hughes | 47390fe | 2019-06-12 12:12:47 -0700 | [diff] [blame] | 1054 | std::string_view sv; |
| 1055 | int32_t result = Next(cookie, data, &sv); |
| 1056 | if (result == 0 && name) { |
| 1057 | *name = std::string(sv); |
| 1058 | } |
| 1059 | return result; |
| 1060 | } |
| 1061 | |
Tianjie | 26ee1db | 2020-04-01 23:08:34 -0700 | [diff] [blame] | 1062 | int32_t Next(void* cookie, ZipEntry64* data, std::string_view* name) { |
Dmitriy Ivanov | d47a85e | 2015-03-06 10:50:56 -0800 | [diff] [blame] | 1063 | IterationHandle* handle = reinterpret_cast<IterationHandle*>(cookie); |
Tianjie Xu | a5e894b | 2020-03-05 16:31:23 -0800 | [diff] [blame] | 1064 | if (handle == nullptr) { |
Zimuzo | a22210d | 2018-09-17 19:49:55 +0100 | [diff] [blame] | 1065 | ALOGW("Zip: Null ZipArchiveHandle"); |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 1066 | return kInvalidHandle; |
| 1067 | } |
| 1068 | |
| 1069 | ZipArchive* archive = handle->archive; |
Tianjie Xu | a5e894b | 2020-03-05 16:31:23 -0800 | [diff] [blame] | 1070 | if (archive == nullptr || archive->cd_entry_map == nullptr) { |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 1071 | ALOGW("Zip: Invalid ZipArchiveHandle"); |
| 1072 | return kInvalidHandle; |
| 1073 | } |
| 1074 | |
Yurii Zubrytskyi | 3c793c4 | 2021-02-22 16:22:01 -0800 | [diff] [blame] | 1075 | SCOPED_SIGBUS_HANDLER(return kIoError); |
| 1076 | |
Tianjie Xu | a5e894b | 2020-03-05 16:31:23 -0800 | [diff] [blame] | 1077 | auto entry = archive->cd_entry_map->Next(archive->central_directory.GetBasePtr()); |
| 1078 | while (entry != std::pair<std::string_view, uint64_t>()) { |
| 1079 | const auto [entry_name, offset] = entry; |
Songchun Fan | 2884066 | 2020-03-24 09:15:51 -0700 | [diff] [blame] | 1080 | if (handle->Match(entry_name)) { |
Tianjie Xu | a5e894b | 2020-03-05 16:31:23 -0800 | [diff] [blame] | 1081 | const int error = FindEntry(archive, entry_name, offset, data); |
Elliott Hughes | b92a93d | 2019-06-18 18:23:59 -0700 | [diff] [blame] | 1082 | if (!error && name) { |
| 1083 | *name = entry_name; |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 1084 | } |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 1085 | return error; |
| 1086 | } |
Tianjie Xu | a5e894b | 2020-03-05 16:31:23 -0800 | [diff] [blame] | 1087 | entry = archive->cd_entry_map->Next(archive->central_directory.GetBasePtr()); |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 1088 | } |
| 1089 | |
Tianjie Xu | a5e894b | 2020-03-05 16:31:23 -0800 | [diff] [blame] | 1090 | archive->cd_entry_map->ResetIteration(); |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 1091 | return kIterationEnd; |
| 1092 | } |
| 1093 | |
Narayan Kamath | 7e80ad0 | 2015-04-17 11:53:14 +0100 | [diff] [blame] | 1094 | // A Writer that writes data to a fixed size memory region. |
| 1095 | // The size of the memory region must be equal to the total size of |
| 1096 | // the data appended to it. |
Narayan Kamath | b73a5a8 | 2017-10-26 14:42:39 +0100 | [diff] [blame] | 1097 | class MemoryWriter : public zip_archive::Writer { |
Narayan Kamath | 7e80ad0 | 2015-04-17 11:53:14 +0100 | [diff] [blame] | 1098 | public: |
Yurii Zubrytskyi | 3c793c4 | 2021-02-22 16:22:01 -0800 | [diff] [blame] | 1099 | static std::optional<MemoryWriter> Create(uint8_t* buf, size_t size, |
| 1100 | const ZipEntry64* entry) { |
Tianjie | 26ee1db | 2020-04-01 23:08:34 -0700 | [diff] [blame] | 1101 | const uint64_t declared_length = entry->uncompressed_length; |
| 1102 | if (declared_length > size) { |
| 1103 | ALOGW("Zip: file size %" PRIu64 " is larger than the buffer size %zu.", declared_length, |
| 1104 | size); |
Yurii Zubrytskyi | 3c793c4 | 2021-02-22 16:22:01 -0800 | [diff] [blame] | 1105 | return {}; |
Tianjie | 26ee1db | 2020-04-01 23:08:34 -0700 | [diff] [blame] | 1106 | } |
| 1107 | |
Yurii Zubrytskyi | 3c793c4 | 2021-02-22 16:22:01 -0800 | [diff] [blame] | 1108 | return std::make_optional<MemoryWriter>(buf, size); |
Tianjie | 26ee1db | 2020-04-01 23:08:34 -0700 | [diff] [blame] | 1109 | } |
| 1110 | |
Narayan Kamath | 7e80ad0 | 2015-04-17 11:53:14 +0100 | [diff] [blame] | 1111 | virtual bool Append(uint8_t* buf, size_t buf_size) override { |
Tianjie | 26ee1db | 2020-04-01 23:08:34 -0700 | [diff] [blame] | 1112 | if (size_ < buf_size || bytes_written_ > size_ - buf_size) { |
Elliott Hughes | 51cbbaa | 2018-10-19 16:09:39 -0700 | [diff] [blame] | 1113 | ALOGW("Zip: Unexpected size %zu (declared) vs %zu (actual)", size_, |
Jiyong Park | 6821cc8 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 1114 | bytes_written_ + buf_size); |
Narayan Kamath | 7e80ad0 | 2015-04-17 11:53:14 +0100 | [diff] [blame] | 1115 | return false; |
| 1116 | } |
| 1117 | |
| 1118 | memcpy(buf_ + bytes_written_, buf, buf_size); |
| 1119 | bytes_written_ += buf_size; |
| 1120 | return true; |
| 1121 | } |
| 1122 | |
Tianjie | 26ee1db | 2020-04-01 23:08:34 -0700 | [diff] [blame] | 1123 | MemoryWriter(uint8_t* buf, size_t size) : Writer(), buf_(buf), size_(size), bytes_written_(0) {} |
| 1124 | |
Yurii Zubrytskyi | 3c793c4 | 2021-02-22 16:22:01 -0800 | [diff] [blame] | 1125 | private: |
Tianjie | 26ee1db | 2020-04-01 23:08:34 -0700 | [diff] [blame] | 1126 | uint8_t* const buf_{nullptr}; |
Narayan Kamath | 7e80ad0 | 2015-04-17 11:53:14 +0100 | [diff] [blame] | 1127 | const size_t size_; |
| 1128 | size_t bytes_written_; |
| 1129 | }; |
| 1130 | |
| 1131 | // A Writer that appends data to a file |fd| at its current position. |
| 1132 | // The file will be truncated to the end of the written data. |
Narayan Kamath | b73a5a8 | 2017-10-26 14:42:39 +0100 | [diff] [blame] | 1133 | class FileWriter : public zip_archive::Writer { |
Narayan Kamath | 7e80ad0 | 2015-04-17 11:53:14 +0100 | [diff] [blame] | 1134 | public: |
Narayan Kamath | 7e80ad0 | 2015-04-17 11:53:14 +0100 | [diff] [blame] | 1135 | // Creates a FileWriter for |fd| and prepare to write |entry| to it, |
| 1136 | // guaranteeing that the file descriptor is valid and that there's enough |
| 1137 | // space on the volume to write out the entry completely and that the file |
Tao Bao | dfb8816 | 2016-11-15 10:08:07 -0800 | [diff] [blame] | 1138 | // is truncated to the correct length (no truncation if |fd| references a |
| 1139 | // block device). |
Narayan Kamath | 7e80ad0 | 2015-04-17 11:53:14 +0100 | [diff] [blame] | 1140 | // |
Yurii Zubrytskyi | 3c793c4 | 2021-02-22 16:22:01 -0800 | [diff] [blame] | 1141 | // Returns a valid FileWriter on success, |nullopt| if an error occurred. |
| 1142 | static std::optional<FileWriter> Create(int fd, const ZipEntry64* entry) { |
Tianjie | 26ee1db | 2020-04-01 23:08:34 -0700 | [diff] [blame] | 1143 | const uint64_t declared_length = entry->uncompressed_length; |
Narayan Kamath | 7e80ad0 | 2015-04-17 11:53:14 +0100 | [diff] [blame] | 1144 | const off64_t current_offset = lseek64(fd, 0, SEEK_CUR); |
| 1145 | if (current_offset == -1) { |
| 1146 | ALOGW("Zip: unable to seek to current location on fd %d: %s", fd, strerror(errno)); |
Yurii Zubrytskyi | 3c793c4 | 2021-02-22 16:22:01 -0800 | [diff] [blame] | 1147 | return {}; |
Narayan Kamath | 7e80ad0 | 2015-04-17 11:53:14 +0100 | [diff] [blame] | 1148 | } |
| 1149 | |
Tianjie | 26ee1db | 2020-04-01 23:08:34 -0700 | [diff] [blame] | 1150 | if (declared_length > SIZE_MAX || declared_length > INT64_MAX) { |
| 1151 | ALOGW("Zip: file size %" PRIu64 " is too large to extract.", declared_length); |
Yurii Zubrytskyi | 3c793c4 | 2021-02-22 16:22:01 -0800 | [diff] [blame] | 1152 | return {}; |
Tianjie | 26ee1db | 2020-04-01 23:08:34 -0700 | [diff] [blame] | 1153 | } |
| 1154 | |
Narayan Kamath | 7e80ad0 | 2015-04-17 11:53:14 +0100 | [diff] [blame] | 1155 | #if defined(__linux__) |
| 1156 | if (declared_length > 0) { |
| 1157 | // Make sure we have enough space on the volume to extract the compressed |
| 1158 | // entry. Note that the call to ftruncate below will change the file size but |
| 1159 | // will not allocate space on disk and this call to fallocate will not |
| 1160 | // change the file size. |
Badhri Jagan Sridharan | fd59cf5 | 2015-06-02 14:47:57 -0700 | [diff] [blame] | 1161 | // Note: fallocate is only supported by the following filesystems - |
| 1162 | // btrfs, ext4, ocfs2, and xfs. Therefore fallocate might fail with |
| 1163 | // EOPNOTSUPP error when issued in other filesystems. |
| 1164 | // Hence, check for the return error code before concluding that the |
| 1165 | // disk does not have enough space. |
Andreas Gampe | 5a5ffb5 | 2019-04-05 13:48:02 -0700 | [diff] [blame] | 1166 | long result = TEMP_FAILURE_RETRY(fallocate(fd, 0, current_offset, declared_length)); |
Badhri Jagan Sridharan | fd59cf5 | 2015-06-02 14:47:57 -0700 | [diff] [blame] | 1167 | if (result == -1 && errno == ENOSPC) { |
Tianjie | 26ee1db | 2020-04-01 23:08:34 -0700 | [diff] [blame] | 1168 | ALOGW("Zip: unable to allocate %" PRIu64 " bytes at offset %" PRId64 ": %s", |
| 1169 | declared_length, static_cast<int64_t>(current_offset), strerror(errno)); |
Yurii Zubrytskyi | 3c793c4 | 2021-02-22 16:22:01 -0800 | [diff] [blame] | 1170 | return {}; |
Narayan Kamath | 7e80ad0 | 2015-04-17 11:53:14 +0100 | [diff] [blame] | 1171 | } |
| 1172 | } |
| 1173 | #endif // __linux__ |
| 1174 | |
Tao Bao | dfb8816 | 2016-11-15 10:08:07 -0800 | [diff] [blame] | 1175 | struct stat sb; |
| 1176 | if (fstat(fd, &sb) == -1) { |
| 1177 | ALOGW("Zip: unable to fstat file: %s", strerror(errno)); |
Yurii Zubrytskyi | 3c793c4 | 2021-02-22 16:22:01 -0800 | [diff] [blame] | 1178 | return {}; |
Narayan Kamath | 7e80ad0 | 2015-04-17 11:53:14 +0100 | [diff] [blame] | 1179 | } |
| 1180 | |
Tao Bao | dfb8816 | 2016-11-15 10:08:07 -0800 | [diff] [blame] | 1181 | // Block device doesn't support ftruncate(2). |
| 1182 | if (!S_ISBLK(sb.st_mode)) { |
Andreas Gampe | 5a5ffb5 | 2019-04-05 13:48:02 -0700 | [diff] [blame] | 1183 | long result = TEMP_FAILURE_RETRY(ftruncate(fd, declared_length + current_offset)); |
Tao Bao | dfb8816 | 2016-11-15 10:08:07 -0800 | [diff] [blame] | 1184 | if (result == -1) { |
| 1185 | ALOGW("Zip: unable to truncate file to %" PRId64 ": %s", |
| 1186 | static_cast<int64_t>(declared_length + current_offset), strerror(errno)); |
Yurii Zubrytskyi | 3c793c4 | 2021-02-22 16:22:01 -0800 | [diff] [blame] | 1187 | return {}; |
Tao Bao | dfb8816 | 2016-11-15 10:08:07 -0800 | [diff] [blame] | 1188 | } |
| 1189 | } |
| 1190 | |
Yurii Zubrytskyi | 3c793c4 | 2021-02-22 16:22:01 -0800 | [diff] [blame] | 1191 | return std::make_optional<FileWriter>(fd, declared_length); |
Yurii Zubrytskyi | a09d7fc | 2017-12-20 01:01:01 -0800 | [diff] [blame] | 1192 | } |
| 1193 | |
Narayan Kamath | 7e80ad0 | 2015-04-17 11:53:14 +0100 | [diff] [blame] | 1194 | virtual bool Append(uint8_t* buf, size_t buf_size) override { |
Tianjie | 26ee1db | 2020-04-01 23:08:34 -0700 | [diff] [blame] | 1195 | if (declared_length_ < buf_size || total_bytes_written_ > declared_length_ - buf_size) { |
| 1196 | ALOGW("Zip: Unexpected size %zu (declared) vs %zu (actual)", declared_length_, |
Jiyong Park | 6821cc8 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 1197 | total_bytes_written_ + buf_size); |
Narayan Kamath | 7e80ad0 | 2015-04-17 11:53:14 +0100 | [diff] [blame] | 1198 | return false; |
| 1199 | } |
| 1200 | |
Narayan Kamath | a221593 | 2015-04-27 16:25:53 +0100 | [diff] [blame] | 1201 | const bool result = android::base::WriteFully(fd_, buf, buf_size); |
| 1202 | if (result) { |
| 1203 | total_bytes_written_ += buf_size; |
| 1204 | } else { |
Elliott Hughes | 51cbbaa | 2018-10-19 16:09:39 -0700 | [diff] [blame] | 1205 | ALOGW("Zip: unable to write %zu bytes to file; %s", buf_size, strerror(errno)); |
Narayan Kamath | 7e80ad0 | 2015-04-17 11:53:14 +0100 | [diff] [blame] | 1206 | } |
| 1207 | |
Narayan Kamath | a221593 | 2015-04-27 16:25:53 +0100 | [diff] [blame] | 1208 | return result; |
Narayan Kamath | 7e80ad0 | 2015-04-17 11:53:14 +0100 | [diff] [blame] | 1209 | } |
Jiyong Park | 6821cc8 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 1210 | |
Tianjie | 26ee1db | 2020-04-01 23:08:34 -0700 | [diff] [blame] | 1211 | explicit FileWriter(const int fd = -1, const uint64_t declared_length = 0) |
| 1212 | : Writer(), |
| 1213 | fd_(fd), |
| 1214 | declared_length_(static_cast<size_t>(declared_length)), |
| 1215 | total_bytes_written_(0) { |
| 1216 | CHECK_LE(declared_length, SIZE_MAX); |
| 1217 | } |
Narayan Kamath | 7e80ad0 | 2015-04-17 11:53:14 +0100 | [diff] [blame] | 1218 | |
Yurii Zubrytskyi | 3c793c4 | 2021-02-22 16:22:01 -0800 | [diff] [blame] | 1219 | private: |
Yurii Zubrytskyi | a09d7fc | 2017-12-20 01:01:01 -0800 | [diff] [blame] | 1220 | int fd_; |
Narayan Kamath | 7e80ad0 | 2015-04-17 11:53:14 +0100 | [diff] [blame] | 1221 | const size_t declared_length_; |
| 1222 | size_t total_bytes_written_; |
| 1223 | }; |
| 1224 | |
Narayan Kamath | b73a5a8 | 2017-10-26 14:42:39 +0100 | [diff] [blame] | 1225 | class EntryReader : public zip_archive::Reader { |
Narayan Kamath | 0a5df0d | 2017-10-26 14:08:38 +0100 | [diff] [blame] | 1226 | public: |
Tianjie | 26ee1db | 2020-04-01 23:08:34 -0700 | [diff] [blame] | 1227 | EntryReader(const MappedZipFile& zip_file, const ZipEntry64* entry) |
Narayan Kamath | 0a5df0d | 2017-10-26 14:08:38 +0100 | [diff] [blame] | 1228 | : Reader(), zip_file_(zip_file), entry_(entry) {} |
| 1229 | |
Tianjie | 26ee1db | 2020-04-01 23:08:34 -0700 | [diff] [blame] | 1230 | virtual bool ReadAtOffset(uint8_t* buf, size_t len, off64_t offset) const { |
Narayan Kamath | 0a5df0d | 2017-10-26 14:08:38 +0100 | [diff] [blame] | 1231 | return zip_file_.ReadAtOffset(buf, len, entry_->offset + offset); |
| 1232 | } |
| 1233 | |
| 1234 | virtual ~EntryReader() {} |
| 1235 | |
| 1236 | private: |
| 1237 | const MappedZipFile& zip_file_; |
Tianjie | 26ee1db | 2020-04-01 23:08:34 -0700 | [diff] [blame] | 1238 | const ZipEntry64* entry_; |
Narayan Kamath | 0a5df0d | 2017-10-26 14:08:38 +0100 | [diff] [blame] | 1239 | }; |
| 1240 | |
Dmitriy Ivanov | 7286ca2 | 2015-03-06 13:27:59 -0800 | [diff] [blame] | 1241 | // This method is using libz macros with old-style-casts |
| 1242 | #pragma GCC diagnostic push |
| 1243 | #pragma GCC diagnostic ignored "-Wold-style-cast" |
| 1244 | static inline int zlib_inflateInit2(z_stream* stream, int window_bits) { |
| 1245 | return inflateInit2(stream, window_bits); |
| 1246 | } |
| 1247 | #pragma GCC diagnostic pop |
| 1248 | |
Narayan Kamath | b73a5a8 | 2017-10-26 14:42:39 +0100 | [diff] [blame] | 1249 | namespace zip_archive { |
| 1250 | |
| 1251 | // Moved out of line to avoid -Wweak-vtables. |
| 1252 | Reader::~Reader() {} |
| 1253 | Writer::~Writer() {} |
| 1254 | |
Yurii Zubrytskyi | 3c793c4 | 2021-02-22 16:22:01 -0800 | [diff] [blame] | 1255 | } // namespace zip_archive |
| 1256 | |
| 1257 | template <bool OnIncfs> |
| 1258 | static int32_t inflateImpl(const zip_archive::Reader& reader, |
| 1259 | const uint64_t compressed_length, |
| 1260 | const uint64_t uncompressed_length, |
| 1261 | zip_archive::Writer* writer, uint64_t* crc_out) { |
Dmitriy Ivanov | 0828c83 | 2015-03-12 09:58:15 -0700 | [diff] [blame] | 1262 | const size_t kBufSize = 32768; |
| 1263 | std::vector<uint8_t> read_buf(kBufSize); |
| 1264 | std::vector<uint8_t> write_buf(kBufSize); |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 1265 | z_stream zstream; |
| 1266 | int zerr; |
| 1267 | |
| 1268 | /* |
| 1269 | * Initialize the zlib stream struct. |
| 1270 | */ |
| 1271 | memset(&zstream, 0, sizeof(zstream)); |
| 1272 | zstream.zalloc = Z_NULL; |
| 1273 | zstream.zfree = Z_NULL; |
| 1274 | zstream.opaque = Z_NULL; |
| 1275 | zstream.next_in = NULL; |
| 1276 | zstream.avail_in = 0; |
Dmitriy Ivanov | 0828c83 | 2015-03-12 09:58:15 -0700 | [diff] [blame] | 1277 | zstream.next_out = &write_buf[0]; |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 1278 | zstream.avail_out = kBufSize; |
| 1279 | zstream.data_type = Z_UNKNOWN; |
| 1280 | |
| 1281 | /* |
| 1282 | * Use the undocumented "negative window bits" feature to tell zlib |
| 1283 | * that there's no zlib header waiting for it. |
| 1284 | */ |
Dmitriy Ivanov | 7286ca2 | 2015-03-06 13:27:59 -0800 | [diff] [blame] | 1285 | zerr = zlib_inflateInit2(&zstream, -MAX_WBITS); |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 1286 | if (zerr != Z_OK) { |
| 1287 | if (zerr == Z_VERSION_ERROR) { |
Jiyong Park | 6821cc8 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 1288 | ALOGE("Installed zlib is not compatible with linked version (%s)", ZLIB_VERSION); |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 1289 | } else { |
| 1290 | ALOGW("Call to inflateInit2 failed (zerr=%d)", zerr); |
| 1291 | } |
| 1292 | |
| 1293 | return kZlibError; |
| 1294 | } |
| 1295 | |
Dmitriy Ivanov | 89c3bab | 2015-03-06 14:26:37 -0800 | [diff] [blame] | 1296 | auto zstream_deleter = [](z_stream* stream) { |
Jiyong Park | 6821cc8 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 1297 | inflateEnd(stream); /* free up any allocated structures */ |
Dmitriy Ivanov | 89c3bab | 2015-03-06 14:26:37 -0800 | [diff] [blame] | 1298 | }; |
| 1299 | |
| 1300 | std::unique_ptr<z_stream, decltype(zstream_deleter)> zstream_guard(&zstream, zstream_deleter); |
| 1301 | |
Yurii Zubrytskyi | 3c793c4 | 2021-02-22 16:22:01 -0800 | [diff] [blame] | 1302 | SCOPED_SIGBUS_HANDLER_CONDITIONAL(OnIncfs, { |
| 1303 | zstream_guard.reset(); |
| 1304 | incfs::util::clearAndFree(read_buf); |
| 1305 | incfs::util::clearAndFree(write_buf); |
| 1306 | return kIoError; |
| 1307 | }); |
| 1308 | |
Narayan Kamath | 976e32f | 2017-10-30 11:17:28 +0000 | [diff] [blame] | 1309 | const bool compute_crc = (crc_out != nullptr); |
Andreas Gampe | 5a5ffb5 | 2019-04-05 13:48:02 -0700 | [diff] [blame] | 1310 | uLong crc = 0; |
Tianjie | 26ee1db | 2020-04-01 23:08:34 -0700 | [diff] [blame] | 1311 | uint64_t remaining_bytes = compressed_length; |
| 1312 | uint64_t total_output = 0; |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 1313 | do { |
| 1314 | /* read as much as we can */ |
| 1315 | if (zstream.avail_in == 0) { |
Tianjie | 26ee1db | 2020-04-01 23:08:34 -0700 | [diff] [blame] | 1316 | const uint32_t read_size = |
| 1317 | (remaining_bytes > kBufSize) ? kBufSize : static_cast<uint32_t>(remaining_bytes); |
| 1318 | const off64_t offset = (compressed_length - remaining_bytes); |
Adam Lesinski | 367ccf6 | 2017-06-19 10:27:38 -0700 | [diff] [blame] | 1319 | // Make sure to read at offset to ensure concurrent access to the fd. |
Narayan Kamath | 0a5df0d | 2017-10-26 14:08:38 +0100 | [diff] [blame] | 1320 | if (!reader.ReadAtOffset(read_buf.data(), read_size, offset)) { |
Andreas Gampe | 5a5ffb5 | 2019-04-05 13:48:02 -0700 | [diff] [blame] | 1321 | ALOGW("Zip: inflate read failed, getSize = %u: %s", read_size, strerror(errno)); |
Dmitriy Ivanov | 89c3bab | 2015-03-06 14:26:37 -0800 | [diff] [blame] | 1322 | return kIoError; |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 1323 | } |
| 1324 | |
Narayan Kamath | 0a5df0d | 2017-10-26 14:08:38 +0100 | [diff] [blame] | 1325 | remaining_bytes -= read_size; |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 1326 | |
Dmitriy Ivanov | 0828c83 | 2015-03-12 09:58:15 -0700 | [diff] [blame] | 1327 | zstream.next_in = &read_buf[0]; |
Narayan Kamath | 0a5df0d | 2017-10-26 14:08:38 +0100 | [diff] [blame] | 1328 | zstream.avail_in = read_size; |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 1329 | } |
| 1330 | |
| 1331 | /* uncompress the data */ |
| 1332 | zerr = inflate(&zstream, Z_NO_FLUSH); |
| 1333 | if (zerr != Z_OK && zerr != Z_STREAM_END) { |
Jiyong Park | 6821cc8 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 1334 | ALOGW("Zip: inflate zerr=%d (nIn=%p aIn=%u nOut=%p aOut=%u)", zerr, zstream.next_in, |
| 1335 | zstream.avail_in, zstream.next_out, zstream.avail_out); |
Dmitriy Ivanov | 89c3bab | 2015-03-06 14:26:37 -0800 | [diff] [blame] | 1336 | return kZlibError; |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 1337 | } |
| 1338 | |
| 1339 | /* write when we're full or when we're done */ |
Jiyong Park | 6821cc8 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 1340 | if (zstream.avail_out == 0 || (zerr == Z_STREAM_END && zstream.avail_out != kBufSize)) { |
Dmitriy Ivanov | 0828c83 | 2015-03-12 09:58:15 -0700 | [diff] [blame] | 1341 | const size_t write_size = zstream.next_out - &write_buf[0]; |
Narayan Kamath | 7e80ad0 | 2015-04-17 11:53:14 +0100 | [diff] [blame] | 1342 | if (!writer->Append(&write_buf[0], write_size)) { |
Narayan Kamath | 976e32f | 2017-10-30 11:17:28 +0000 | [diff] [blame] | 1343 | return kIoError; |
| 1344 | } else if (compute_crc) { |
Andreas Gampe | 5a5ffb5 | 2019-04-05 13:48:02 -0700 | [diff] [blame] | 1345 | DCHECK_LE(write_size, kBufSize); |
| 1346 | crc = crc32(crc, &write_buf[0], static_cast<uint32_t>(write_size)); |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 1347 | } |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 1348 | |
Tianjie | 26ee1db | 2020-04-01 23:08:34 -0700 | [diff] [blame] | 1349 | total_output += kBufSize - zstream.avail_out; |
Dmitriy Ivanov | 0828c83 | 2015-03-12 09:58:15 -0700 | [diff] [blame] | 1350 | zstream.next_out = &write_buf[0]; |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 1351 | zstream.avail_out = kBufSize; |
| 1352 | } |
| 1353 | } while (zerr == Z_OK); |
| 1354 | |
Elliott Hughes | 51cbbaa | 2018-10-19 16:09:39 -0700 | [diff] [blame] | 1355 | CHECK_EQ(zerr, Z_STREAM_END); /* other errors should've been caught */ |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 1356 | |
Narayan Kamath | 9775435 | 2017-06-05 13:21:12 +0100 | [diff] [blame] | 1357 | // NOTE: zstream.adler is always set to 0, because we're using the -MAX_WBITS |
| 1358 | // "feature" of zlib to tell it there won't be a zlib file header. zlib |
| 1359 | // doesn't bother calculating the checksum in that scenario. We just do |
| 1360 | // it ourselves above because there are no additional gains to be made by |
| 1361 | // having zlib calculate it for us, since they do it by calling crc32 in |
| 1362 | // the same manner that we have above. |
Narayan Kamath | 976e32f | 2017-10-30 11:17:28 +0000 | [diff] [blame] | 1363 | if (compute_crc) { |
| 1364 | *crc_out = crc; |
| 1365 | } |
Tianjie | 26ee1db | 2020-04-01 23:08:34 -0700 | [diff] [blame] | 1366 | if (total_output != uncompressed_length || remaining_bytes != 0) { |
| 1367 | ALOGW("Zip: size mismatch on inflated file (%lu vs %" PRIu64 ")", zstream.total_out, |
Jiyong Park | 6821cc8 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 1368 | uncompressed_length); |
Dmitriy Ivanov | 89c3bab | 2015-03-06 14:26:37 -0800 | [diff] [blame] | 1369 | return kInconsistentInformation; |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 1370 | } |
| 1371 | |
Dmitriy Ivanov | 89c3bab | 2015-03-06 14:26:37 -0800 | [diff] [blame] | 1372 | return 0; |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 1373 | } |
| 1374 | |
Tianjie | 26ee1db | 2020-04-01 23:08:34 -0700 | [diff] [blame] | 1375 | static int32_t InflateEntryToWriter(MappedZipFile& mapped_zip, const ZipEntry64* entry, |
Narayan Kamath | b73a5a8 | 2017-10-26 14:42:39 +0100 | [diff] [blame] | 1376 | zip_archive::Writer* writer, uint64_t* crc_out) { |
Narayan Kamath | 0a5df0d | 2017-10-26 14:08:38 +0100 | [diff] [blame] | 1377 | const EntryReader reader(mapped_zip, entry); |
| 1378 | |
Yurii Zubrytskyi | 3c793c4 | 2021-02-22 16:22:01 -0800 | [diff] [blame] | 1379 | return inflateImpl<true>(reader, entry->compressed_length, |
| 1380 | entry->uncompressed_length, writer, crc_out); |
Narayan Kamath | 0a5df0d | 2017-10-26 14:08:38 +0100 | [diff] [blame] | 1381 | } |
| 1382 | |
Tianjie | 26ee1db | 2020-04-01 23:08:34 -0700 | [diff] [blame] | 1383 | static int32_t CopyEntryToWriter(MappedZipFile& mapped_zip, const ZipEntry64* entry, |
Narayan Kamath | b73a5a8 | 2017-10-26 14:42:39 +0100 | [diff] [blame] | 1384 | zip_archive::Writer* writer, uint64_t* crc_out) { |
Narayan Kamath | 7e80ad0 | 2015-04-17 11:53:14 +0100 | [diff] [blame] | 1385 | static const uint32_t kBufSize = 32768; |
| 1386 | std::vector<uint8_t> buf(kBufSize); |
| 1387 | |
Yurii Zubrytskyi | 3c793c4 | 2021-02-22 16:22:01 -0800 | [diff] [blame] | 1388 | SCOPED_SIGBUS_HANDLER({ |
| 1389 | incfs::util::clearAndFree(buf); |
| 1390 | return kIoError; |
| 1391 | }); |
| 1392 | |
Tianjie | 26ee1db | 2020-04-01 23:08:34 -0700 | [diff] [blame] | 1393 | const uint64_t length = entry->uncompressed_length; |
| 1394 | uint64_t count = 0; |
Andreas Gampe | 5a5ffb5 | 2019-04-05 13:48:02 -0700 | [diff] [blame] | 1395 | uLong crc = 0; |
Narayan Kamath | 7e80ad0 | 2015-04-17 11:53:14 +0100 | [diff] [blame] | 1396 | while (count < length) { |
Tianjie | 26ee1db | 2020-04-01 23:08:34 -0700 | [diff] [blame] | 1397 | uint64_t remaining = length - count; |
Adam Lesinski | 367ccf6 | 2017-06-19 10:27:38 -0700 | [diff] [blame] | 1398 | off64_t offset = entry->offset + count; |
Narayan Kamath | 7e80ad0 | 2015-04-17 11:53:14 +0100 | [diff] [blame] | 1399 | |
Adam Lesinski | 367ccf6 | 2017-06-19 10:27:38 -0700 | [diff] [blame] | 1400 | // Safe conversion because kBufSize is narrow enough for a 32 bit signed value. |
Tianjie | 26ee1db | 2020-04-01 23:08:34 -0700 | [diff] [blame] | 1401 | const uint32_t block_size = |
| 1402 | (remaining > kBufSize) ? kBufSize : static_cast<uint32_t>(remaining); |
Adam Lesinski | 367ccf6 | 2017-06-19 10:27:38 -0700 | [diff] [blame] | 1403 | |
| 1404 | // Make sure to read at offset to ensure concurrent access to the fd. |
| 1405 | if (!mapped_zip.ReadAtOffset(buf.data(), block_size, offset)) { |
Andreas Gampe | 5a5ffb5 | 2019-04-05 13:48:02 -0700 | [diff] [blame] | 1406 | ALOGW("CopyFileToFile: copy read failed, block_size = %u, offset = %" PRId64 ": %s", |
Adam Lesinski | 367ccf6 | 2017-06-19 10:27:38 -0700 | [diff] [blame] | 1407 | block_size, static_cast<int64_t>(offset), strerror(errno)); |
Narayan Kamath | 7e80ad0 | 2015-04-17 11:53:14 +0100 | [diff] [blame] | 1408 | return kIoError; |
| 1409 | } |
| 1410 | |
| 1411 | if (!writer->Append(&buf[0], block_size)) { |
| 1412 | return kIoError; |
| 1413 | } |
Yurii Zubrytskyi | e75a3e1 | 2020-04-06 19:35:33 -0700 | [diff] [blame] | 1414 | if (crc_out) { |
| 1415 | crc = crc32(crc, &buf[0], block_size); |
| 1416 | } |
Narayan Kamath | 7e80ad0 | 2015-04-17 11:53:14 +0100 | [diff] [blame] | 1417 | count += block_size; |
| 1418 | } |
| 1419 | |
Yurii Zubrytskyi | e75a3e1 | 2020-04-06 19:35:33 -0700 | [diff] [blame] | 1420 | if (crc_out) { |
| 1421 | *crc_out = crc; |
| 1422 | } |
Narayan Kamath | 7e80ad0 | 2015-04-17 11:53:14 +0100 | [diff] [blame] | 1423 | |
| 1424 | return 0; |
| 1425 | } |
| 1426 | |
Yurii Zubrytskyi | 3c793c4 | 2021-02-22 16:22:01 -0800 | [diff] [blame] | 1427 | static int32_t extractToWriter(ZipArchiveHandle handle, const ZipEntry64* entry, |
| 1428 | zip_archive::Writer* writer) { |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 1429 | const uint16_t method = entry->method; |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 1430 | |
| 1431 | // this should default to kUnknownCompressionMethod. |
| 1432 | int32_t return_value = -1; |
| 1433 | uint64_t crc = 0; |
| 1434 | if (method == kCompressStored) { |
Yurii Zubrytskyi | e75a3e1 | 2020-04-06 19:35:33 -0700 | [diff] [blame] | 1435 | return_value = |
| 1436 | CopyEntryToWriter(handle->mapped_zip, entry, writer, kCrcChecksEnabled ? &crc : nullptr); |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 1437 | } else if (method == kCompressDeflated) { |
Yurii Zubrytskyi | e75a3e1 | 2020-04-06 19:35:33 -0700 | [diff] [blame] | 1438 | return_value = |
| 1439 | InflateEntryToWriter(handle->mapped_zip, entry, writer, kCrcChecksEnabled ? &crc : nullptr); |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 1440 | } |
| 1441 | |
| 1442 | if (!return_value && entry->has_data_descriptor) { |
Tianjie | 26ee1db | 2020-04-01 23:08:34 -0700 | [diff] [blame] | 1443 | return_value = ValidateDataDescriptor(handle->mapped_zip, entry); |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 1444 | if (return_value) { |
| 1445 | return return_value; |
| 1446 | } |
| 1447 | } |
| 1448 | |
Narayan Kamath | 9775435 | 2017-06-05 13:21:12 +0100 | [diff] [blame] | 1449 | // Validate that the CRC matches the calculated value. |
| 1450 | if (kCrcChecksEnabled && (entry->crc32 != static_cast<uint32_t>(crc))) { |
Mark Salyzyn | 8fa540e | 2014-05-08 16:02:20 -0700 | [diff] [blame] | 1451 | ALOGW("Zip: crc mismatch: expected %" PRIu32 ", was %" PRIu64, entry->crc32, crc); |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 1452 | return kInconsistentInformation; |
| 1453 | } |
| 1454 | |
| 1455 | return return_value; |
| 1456 | } |
| 1457 | |
Tianjie | 26ee1db | 2020-04-01 23:08:34 -0700 | [diff] [blame] | 1458 | int32_t ExtractToMemory(ZipArchiveHandle archive, const ZipEntry* entry, uint8_t* begin, |
| 1459 | size_t size) { |
| 1460 | ZipEntry64 entry64(*entry); |
| 1461 | return ExtractToMemory(archive, &entry64, begin, size); |
| 1462 | } |
| 1463 | |
| 1464 | int32_t ExtractToMemory(ZipArchiveHandle archive, const ZipEntry64* entry, uint8_t* begin, |
| 1465 | size_t size) { |
| 1466 | auto writer = MemoryWriter::Create(begin, size, entry); |
Tianjie | e3977ec | 2020-04-07 00:12:54 -0700 | [diff] [blame] | 1467 | if (!writer) { |
Tianjie | 26ee1db | 2020-04-01 23:08:34 -0700 | [diff] [blame] | 1468 | return kIoError; |
| 1469 | } |
| 1470 | |
Yurii Zubrytskyi | 3c793c4 | 2021-02-22 16:22:01 -0800 | [diff] [blame] | 1471 | return extractToWriter(archive, entry, &writer.value()); |
Narayan Kamath | 7e80ad0 | 2015-04-17 11:53:14 +0100 | [diff] [blame] | 1472 | } |
| 1473 | |
Tianjie | 26ee1db | 2020-04-01 23:08:34 -0700 | [diff] [blame] | 1474 | int32_t ExtractEntryToFile(ZipArchiveHandle archive, const ZipEntry* entry, int fd) { |
| 1475 | ZipEntry64 entry64(*entry); |
| 1476 | return ExtractEntryToFile(archive, &entry64, fd); |
| 1477 | } |
| 1478 | |
| 1479 | int32_t ExtractEntryToFile(ZipArchiveHandle archive, const ZipEntry64* entry, int fd) { |
Yurii Zubrytskyi | a09d7fc | 2017-12-20 01:01:01 -0800 | [diff] [blame] | 1480 | auto writer = FileWriter::Create(fd, entry); |
Tianjie | e3977ec | 2020-04-07 00:12:54 -0700 | [diff] [blame] | 1481 | if (!writer) { |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 1482 | return kIoError; |
| 1483 | } |
| 1484 | |
Yurii Zubrytskyi | 3c793c4 | 2021-02-22 16:22:01 -0800 | [diff] [blame] | 1485 | return extractToWriter(archive, entry, &writer.value()); |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 1486 | } |
| 1487 | |
Ryan Prichard | 30236a1 | 2018-10-10 22:41:14 -0700 | [diff] [blame] | 1488 | int GetFileDescriptor(const ZipArchiveHandle archive) { |
| 1489 | return archive->mapped_zip.GetFileDescriptor(); |
Narayan Kamath | e1e8f71 | 2013-11-21 13:05:04 +0000 | [diff] [blame] | 1490 | } |
Colin Cross | df22776 | 2016-09-16 10:15:51 -0700 | [diff] [blame] | 1491 | |
Ryan Mitchell | 0edeea9 | 2020-03-09 09:33:46 -0700 | [diff] [blame] | 1492 | off64_t GetFileDescriptorOffset(const ZipArchiveHandle archive) { |
| 1493 | return archive->mapped_zip.GetFileOffset(); |
| 1494 | } |
| 1495 | |
Yurii Zubrytskyi | 3c793c4 | 2021-02-22 16:22:01 -0800 | [diff] [blame] | 1496 | // |
| 1497 | // ZIPARCHIVE_DISABLE_CALLBACK_API disables all APIs that accept user callbacks. |
| 1498 | // It gets defined for the incfs-supporting version of libziparchive, where one |
| 1499 | // has to control all the code accessing the archive. See more at |
| 1500 | // incfs_support/signal_handling.h |
| 1501 | // |
| 1502 | #if !ZIPARCHIVE_DISABLE_CALLBACK_API && !defined(_WIN32) |
Narayan Kamath | b73a5a8 | 2017-10-26 14:42:39 +0100 | [diff] [blame] | 1503 | class ProcessWriter : public zip_archive::Writer { |
Tianjie Xu | 484de54 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 1504 | public: |
Jiyong Park | 6821cc8 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 1505 | ProcessWriter(ProcessZipEntryFunction func, void* cookie) |
| 1506 | : Writer(), proc_function_(func), cookie_(cookie) {} |
Tianjie Xu | 484de54 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 1507 | |
| 1508 | virtual bool Append(uint8_t* buf, size_t buf_size) override { |
| 1509 | return proc_function_(buf, buf_size, cookie_); |
| 1510 | } |
| 1511 | |
| 1512 | private: |
| 1513 | ProcessZipEntryFunction proc_function_; |
| 1514 | void* cookie_; |
| 1515 | }; |
| 1516 | |
Tianjie | 26ee1db | 2020-04-01 23:08:34 -0700 | [diff] [blame] | 1517 | int32_t ProcessZipEntryContents(ZipArchiveHandle archive, const ZipEntry* entry, |
| 1518 | ProcessZipEntryFunction func, void* cookie) { |
| 1519 | ZipEntry64 entry64(*entry); |
| 1520 | return ProcessZipEntryContents(archive, &entry64, func, cookie); |
| 1521 | } |
| 1522 | |
| 1523 | int32_t ProcessZipEntryContents(ZipArchiveHandle archive, const ZipEntry64* entry, |
Tianjie Xu | 484de54 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 1524 | ProcessZipEntryFunction func, void* cookie) { |
| 1525 | ProcessWriter writer(func, cookie); |
Yurii Zubrytskyi | 3c793c4 | 2021-02-22 16:22:01 -0800 | [diff] [blame] | 1526 | return extractToWriter(archive, entry, &writer); |
Tianjie Xu | 484de54 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 1527 | } |
| 1528 | |
Yurii Zubrytskyi | 3c793c4 | 2021-02-22 16:22:01 -0800 | [diff] [blame] | 1529 | #endif // !ZIPARCHIVE_DISABLE_CALLBACK_API && !defined(_WIN32) |
Tianjie Xu | 484de54 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 1530 | |
| 1531 | int MappedZipFile::GetFileDescriptor() const { |
| 1532 | if (!has_fd_) { |
| 1533 | ALOGW("Zip: MappedZipFile doesn't have a file descriptor."); |
| 1534 | return -1; |
| 1535 | } |
| 1536 | return fd_; |
| 1537 | } |
| 1538 | |
Elliott Hughes | 62c2d53 | 2019-10-22 11:44:50 -0700 | [diff] [blame] | 1539 | const void* MappedZipFile::GetBasePtr() const { |
Tianjie Xu | 484de54 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 1540 | if (has_fd_) { |
| 1541 | ALOGW("Zip: MappedZipFile doesn't have a base pointer."); |
| 1542 | return nullptr; |
| 1543 | } |
| 1544 | return base_ptr_; |
| 1545 | } |
| 1546 | |
Ryan Mitchell | 0edeea9 | 2020-03-09 09:33:46 -0700 | [diff] [blame] | 1547 | off64_t MappedZipFile::GetFileOffset() const { |
| 1548 | return fd_offset_; |
| 1549 | } |
| 1550 | |
Tianjie Xu | 484de54 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 1551 | off64_t MappedZipFile::GetFileLength() const { |
| 1552 | if (has_fd_) { |
Ryan Mitchell | 0edeea9 | 2020-03-09 09:33:46 -0700 | [diff] [blame] | 1553 | if (data_length_ != -1) { |
| 1554 | return data_length_; |
| 1555 | } |
| 1556 | data_length_ = lseek64(fd_, 0, SEEK_END); |
| 1557 | if (data_length_ == -1) { |
Tianjie Xu | 484de54 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 1558 | ALOGE("Zip: lseek on fd %d failed: %s", fd_, strerror(errno)); |
| 1559 | } |
Ryan Mitchell | 0edeea9 | 2020-03-09 09:33:46 -0700 | [diff] [blame] | 1560 | return data_length_; |
Tianjie Xu | 484de54 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 1561 | } else { |
| 1562 | if (base_ptr_ == nullptr) { |
Elliott Hughes | b59cc23 | 2019-12-16 16:16:16 -0800 | [diff] [blame] | 1563 | ALOGE("Zip: invalid file map"); |
Tianjie Xu | 484de54 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 1564 | return -1; |
| 1565 | } |
Ryan Mitchell | 0edeea9 | 2020-03-09 09:33:46 -0700 | [diff] [blame] | 1566 | return data_length_; |
Tianjie Xu | 484de54 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 1567 | } |
| 1568 | } |
| 1569 | |
Tianjie Xu | 484de54 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 1570 | // Attempts to read |len| bytes into |buf| at offset |off|. |
Narayan Kamath | 0a5df0d | 2017-10-26 14:08:38 +0100 | [diff] [blame] | 1571 | bool MappedZipFile::ReadAtOffset(uint8_t* buf, size_t len, off64_t off) const { |
Tianjie Xu | 484de54 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 1572 | if (has_fd_) { |
Ryan Mitchell | 0edeea9 | 2020-03-09 09:33:46 -0700 | [diff] [blame] | 1573 | if (off < 0) { |
| 1574 | ALOGE("Zip: invalid offset %" PRId64, off); |
| 1575 | return false; |
| 1576 | } |
| 1577 | |
| 1578 | off64_t read_offset; |
| 1579 | if (__builtin_add_overflow(fd_offset_, off, &read_offset)) { |
| 1580 | ALOGE("Zip: invalid read offset %" PRId64 " overflows, fd offset %" PRId64, off, fd_offset_); |
| 1581 | return false; |
| 1582 | } |
| 1583 | |
| 1584 | if (data_length_ != -1) { |
| 1585 | off64_t read_end; |
| 1586 | if (len > std::numeric_limits<off64_t>::max() || |
| 1587 | __builtin_add_overflow(off, static_cast<off64_t>(len), &read_end)) { |
| 1588 | ALOGE("Zip: invalid read length %" PRId64 " overflows, offset %" PRId64, |
| 1589 | static_cast<off64_t>(len), off); |
| 1590 | return false; |
| 1591 | } |
| 1592 | |
| 1593 | if (read_end > data_length_) { |
| 1594 | ALOGE("Zip: invalid read length %" PRId64 " exceeds data length %" PRId64 ", offset %" |
| 1595 | PRId64, static_cast<off64_t>(len), data_length_, off); |
| 1596 | return false; |
| 1597 | } |
| 1598 | } |
| 1599 | |
| 1600 | if (!android::base::ReadFullyAtOffset(fd_, buf, len, read_offset)) { |
Elliott Hughes | b59cc23 | 2019-12-16 16:16:16 -0800 | [diff] [blame] | 1601 | ALOGE("Zip: failed to read at offset %" PRId64, off); |
Tianjie Xu | 484de54 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 1602 | return false; |
| 1603 | } |
Adam Lesinski | 367ccf6 | 2017-06-19 10:27:38 -0700 | [diff] [blame] | 1604 | } else { |
Tianjie | 41cf173 | 2020-04-13 16:29:22 -0700 | [diff] [blame] | 1605 | if (off < 0 || data_length_ < len || off > data_length_ - len) { |
| 1606 | ALOGE("Zip: invalid offset: %" PRId64 ", read length: %zu, data length: %" PRId64, off, len, |
| 1607 | data_length_); |
Adam Lesinski | 367ccf6 | 2017-06-19 10:27:38 -0700 | [diff] [blame] | 1608 | return false; |
| 1609 | } |
Elliott Hughes | 62c2d53 | 2019-10-22 11:44:50 -0700 | [diff] [blame] | 1610 | memcpy(buf, static_cast<const uint8_t*>(base_ptr_) + off, len); |
Tianjie Xu | 484de54 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 1611 | } |
Adam Lesinski | 367ccf6 | 2017-06-19 10:27:38 -0700 | [diff] [blame] | 1612 | return true; |
Tianjie Xu | 484de54 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 1613 | } |
| 1614 | |
Elliott Hughes | 62c2d53 | 2019-10-22 11:44:50 -0700 | [diff] [blame] | 1615 | void CentralDirectory::Initialize(const void* map_base_ptr, off64_t cd_start_offset, |
| 1616 | size_t cd_size) { |
| 1617 | base_ptr_ = static_cast<const uint8_t*>(map_base_ptr) + cd_start_offset; |
Tianjie Xu | 484de54 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 1618 | length_ = cd_size; |
| 1619 | } |
| 1620 | |
Elliott Hughes | 51cbbaa | 2018-10-19 16:09:39 -0700 | [diff] [blame] | 1621 | bool ZipArchive::InitializeCentralDirectory(off64_t cd_start_offset, size_t cd_size) { |
Tianjie Xu | 484de54 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 1622 | if (mapped_zip.HasFd()) { |
Elliott Hughes | 51cbbaa | 2018-10-19 16:09:39 -0700 | [diff] [blame] | 1623 | directory_map = android::base::MappedFile::FromFd(mapped_zip.GetFileDescriptor(), |
Ryan Mitchell | 0edeea9 | 2020-03-09 09:33:46 -0700 | [diff] [blame] | 1624 | mapped_zip.GetFileOffset() + cd_start_offset, |
| 1625 | cd_size, PROT_READ); |
Elliott Hughes | b59cc23 | 2019-12-16 16:16:16 -0800 | [diff] [blame] | 1626 | if (!directory_map) { |
| 1627 | ALOGE("Zip: failed to map central directory (offset %" PRId64 ", size %zu): %s", |
| 1628 | cd_start_offset, cd_size, strerror(errno)); |
| 1629 | return false; |
| 1630 | } |
Tianjie Xu | 484de54 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 1631 | |
Elliott Hughes | 51cbbaa | 2018-10-19 16:09:39 -0700 | [diff] [blame] | 1632 | CHECK_EQ(directory_map->size(), cd_size); |
| 1633 | central_directory.Initialize(directory_map->data(), 0 /*offset*/, cd_size); |
Tianjie Xu | 484de54 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 1634 | } else { |
| 1635 | if (mapped_zip.GetBasePtr() == nullptr) { |
Elliott Hughes | b59cc23 | 2019-12-16 16:16:16 -0800 | [diff] [blame] | 1636 | ALOGE("Zip: Failed to map central directory, bad mapped_zip base pointer"); |
Tianjie Xu | 484de54 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 1637 | return false; |
| 1638 | } |
| 1639 | if (static_cast<off64_t>(cd_start_offset) + static_cast<off64_t>(cd_size) > |
| 1640 | mapped_zip.GetFileLength()) { |
Jiyong Park | 6821cc8 | 2017-06-30 17:23:33 +0900 | [diff] [blame] | 1641 | ALOGE( |
| 1642 | "Zip: Failed to map central directory, offset exceeds mapped memory region (" |
| 1643 | "start_offset %" PRId64 ", cd_size %zu, mapped_region_size %" PRId64 ")", |
| 1644 | static_cast<int64_t>(cd_start_offset), cd_size, mapped_zip.GetFileLength()); |
Tianjie Xu | 484de54 | 2016-09-29 15:27:41 -0700 | [diff] [blame] | 1645 | return false; |
| 1646 | } |
| 1647 | |
| 1648 | central_directory.Initialize(mapped_zip.GetBasePtr(), cd_start_offset, cd_size); |
| 1649 | } |
| 1650 | return true; |
| 1651 | } |
Elliott Hughes | 6702cef | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 1652 | |
Elliott Hughes | b75c9f3 | 2020-09-25 13:30:37 -0700 | [diff] [blame] | 1653 | // This function returns the embedded timestamp as is and doesn't perform validation. |
Tianjie | 26ee1db | 2020-04-01 23:08:34 -0700 | [diff] [blame] | 1654 | tm ZipEntryCommon::GetModificationTime() const { |
Elliott Hughes | 6702cef | 2017-05-28 22:59:04 -0700 | [diff] [blame] | 1655 | tm t = {}; |
| 1656 | |
| 1657 | t.tm_hour = (mod_time >> 11) & 0x1f; |
| 1658 | t.tm_min = (mod_time >> 5) & 0x3f; |
| 1659 | t.tm_sec = (mod_time & 0x1f) << 1; |
| 1660 | |
| 1661 | t.tm_year = ((mod_time >> 25) & 0x7f) + 80; |
| 1662 | t.tm_mon = ((mod_time >> 21) & 0xf) - 1; |
| 1663 | t.tm_mday = (mod_time >> 16) & 0x1f; |
| 1664 | |
| 1665 | return t; |
| 1666 | } |
Yurii Zubrytskyi | 3c793c4 | 2021-02-22 16:22:01 -0800 | [diff] [blame] | 1667 | |
| 1668 | namespace zip_archive { |
| 1669 | |
| 1670 | int32_t Inflate(const Reader& reader, const uint64_t compressed_length, |
| 1671 | const uint64_t uncompressed_length, Writer* writer, |
| 1672 | uint64_t* crc_out) { |
| 1673 | return inflateImpl<false>(reader, compressed_length, uncompressed_length, |
| 1674 | writer, crc_out); |
| 1675 | } |
| 1676 | |
| 1677 | // |
| 1678 | // ZIPARCHIVE_DISABLE_CALLBACK_API disables all APIs that accept user callbacks. |
| 1679 | // It gets defined for the incfs-supporting version of libziparchive, where one |
| 1680 | // has to control all the code accessing the archive. See more at |
| 1681 | // incfs_support/signal_handling.h |
| 1682 | // |
| 1683 | #if !ZIPARCHIVE_DISABLE_CALLBACK_API |
| 1684 | |
| 1685 | int32_t ExtractToWriter(ZipArchiveHandle handle, const ZipEntry64* entry, |
| 1686 | zip_archive::Writer* writer) { |
| 1687 | return extractToWriter(handle, entry, writer); |
| 1688 | } |
| 1689 | |
| 1690 | #endif // !ZIPARCHIVE_DISABLE_CALLBACK_API |
| 1691 | |
| 1692 | } // namespace zip_archive |