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