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