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