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