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