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