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