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