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