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