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