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