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