blob: 24088bb934d08abcc7cb850ee9d92b909e8b1005 [file] [log] [blame]
Narayan Kamath7462f022013-11-21 13:05:04 +00001/*
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 Kamath7462f022013-11-21 13:05:04 +000020
21#include <assert.h>
22#include <errno.h>
Mark Salyzyn99ef9912014-03-14 14:26:22 -070023#include <fcntl.h>
24#include <inttypes.h>
Narayan Kamath7462f022013-11-21 13:05:04 +000025#include <limits.h>
26#include <log/log.h>
Narayan Kamath7462f022013-11-21 13:05:04 +000027#include <stdlib.h>
28#include <string.h>
Narayan Kamath7462f022013-11-21 13:05:04 +000029#include <unistd.h>
Mark Salyzyn51d562d2014-05-05 14:38:05 -070030#include <utils/Compat.h>
Narayan Kamatheaf98852013-12-11 14:51:51 +000031#include <utils/FileMap.h>
Mark Salyzyn99ef9912014-03-14 14:26:22 -070032#include <zlib.h>
Narayan Kamath7462f022013-11-21 13:05:04 +000033
34#include <JNIHelp.h> // TEMP_FAILURE_RETRY may or may not be in unistd
35
Mark Salyzyn99ef9912014-03-14 14:26:22 -070036#include "ziparchive/zip_archive.h"
37
Narayan Kamath926973e2014-06-09 14:18:14 +010038// This is for windows. If we don't open a file in binary mode, weird
Narayan Kamath7462f022013-11-21 13:05:04 +000039// things will happen.
40#ifndef O_BINARY
41#define O_BINARY 0
42#endif
43
Narayan Kamath926973e2014-06-09 14:18:14 +010044#define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
45 TypeName(); \
46 TypeName(const TypeName&); \
47 void operator=(const TypeName&)
Narayan Kamath7462f022013-11-21 13:05:04 +000048
Narayan Kamath926973e2014-06-09 14:18:14 +010049// 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.
54struct EocdRecord {
55 static const uint32_t kSignature = 0x06054b50;
Narayan Kamath7462f022013-11-21 13:05:04 +000056
Narayan Kamath926973e2014-06-09 14:18:14 +010057 // 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 Kamath7462f022013-11-21 13:05:04 +000088
Narayan Kamath926973e2014-06-09 14:18:14 +010089// 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.
95struct CentralDirectoryRecord {
96 static const uint32_t kSignature = 0x02014b50;
Narayan Kamath7462f022013-11-21 13:05:04 +000097
Narayan Kamath926973e2014-06-09 14:18:14 +010098 // 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 Kamath7462f022013-11-21 13:05:04 +0000141
Narayan Kamath926973e2014-06-09 14:18:14 +0100142// 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.
146struct LocalFileHeader {
147 static const uint32_t kSignature = 0x04034b50;
Narayan Kamath7462f022013-11-21 13:05:04 +0000148
Narayan Kamath926973e2014-06-09 14:18:14 +0100149 // 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
179struct 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
Piotr Jastrzebskibd0a7482014-08-13 09:49:25 +0000195static const uint32_t kGPBDDFlagMask = 0x0008; // mask value that signifies that the entry has a DD
Narayan Kamath7462f022013-11-21 13:05:04 +0000196static const uint32_t kMaxErrorLen = 1024;
197
Narayan Kamath926973e2014-06-09 14:18:14 +0100198// The maximum size of a central directory or a file
199// comment in bytes.
200static const uint32_t kMaxCommentLen = 65535;
201
202// The maximum number of bytes to scan backwards for the EOCD start.
203static const uint32_t kMaxEOCDSearch = kMaxCommentLen + sizeof(EocdRecord);
204
Narayan Kamath7462f022013-11-21 13:05:04 +0000205static const char* kErrorMessages[] = {
206 "Unknown return code.",
Narayan Kamatheb41ad22013-12-09 16:26:36 +0000207 "Iteration ended",
Narayan Kamath7462f022013-11-21 13:05:04 +0000208 "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 Kamatheb41ad22013-12-09 16:26:36 +0000217 "I/O Error",
Narayan Kamatheaf98852013-12-11 14:51:51 +0000218 "File mapping failed"
Narayan Kamath7462f022013-11-21 13:05:04 +0000219};
220
221static const int32_t kErrorMessageUpperBound = 0;
222
Narayan Kamatheb41ad22013-12-09 16:26:36 +0000223static const int32_t kIterationEnd = -1;
Narayan Kamath7462f022013-11-21 13:05:04 +0000224
225// We encountered a Zlib error when inflating a stream from this file.
226// Usually indicates file corruption.
227static 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.
231static const int32_t kInvalidFile = -3;
232
233// An invalid iteration / ziparchive handle was passed in as an input
234// argument.
235static const int32_t kInvalidHandle = -4;
236
237// The zip archive contained two (or possibly more) entries with the same
238// name.
239static const int32_t kDuplicateEntry = -5;
240
241// The zip archive contains no entries.
242static const int32_t kEmptyArchive = -6;
243
244// The specified entry was not found in the archive.
245static const int32_t kEntryNotFound = -7;
246
247// The zip archive contained an invalid local file header pointer.
248static 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.
254static const int32_t kInconsistentInformation = -9;
255
256// An invalid entry name was encountered.
257static const int32_t kInvalidEntryName = -10;
258
Narayan Kamatheb41ad22013-12-09 16:26:36 +0000259// An I/O related system call (read, lseek, ftruncate, map) failed.
260static const int32_t kIoError = -11;
Narayan Kamath7462f022013-11-21 13:05:04 +0000261
Narayan Kamatheaf98852013-12-11 14:51:51 +0000262// We were not able to mmap the central directory or entry contents.
263static const int32_t kMmapFailed = -12;
Narayan Kamath7462f022013-11-21 13:05:04 +0000264
Narayan Kamatheaf98852013-12-11 14:51:51 +0000265static const int32_t kErrorMessageLowerBound = -13;
Narayan Kamath7462f022013-11-21 13:05:04 +0000266
Narayan Kamatheaf98852013-12-11 14:51:51 +0000267static const char kTempMappingFileName[] = "zip: ExtractFileToFile";
Narayan Kamath7462f022013-11-21 13:05:04 +0000268
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 */
288struct ZipArchive {
289 /* open Zip archive */
Neil Fullerb1a113f2014-07-25 14:43:04 +0100290 const int fd;
Narayan Kamath7462f022013-11-21 13:05:04 +0000291
292 /* mapped central directory area */
293 off64_t directory_offset;
Narayan Kamatheaf98852013-12-11 14:51:51 +0000294 android::FileMap* directory_map;
Narayan Kamath7462f022013-11-21 13:05:04 +0000295
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;
Neil Fullerb1a113f2014-07-25 14:43:04 +0100307
308 ZipArchive(const int fd) :
309 fd(fd),
310 directory_offset(0),
311 directory_map(NULL),
312 num_entries(0),
313 hash_table_size(0),
314 hash_table(NULL) {}
315
316 ~ZipArchive() {
317 if (fd >= 0) {
318 close(fd);
319 }
320
321 if (directory_map != NULL) {
322 directory_map->release();
323 }
324 free(hash_table);
325 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000326};
327
328// Returns 0 on success and negative values on failure.
Narayan Kamatheaf98852013-12-11 14:51:51 +0000329static android::FileMap* MapFileSegment(const int fd, const off64_t start,
330 const size_t length, const bool read_only,
331 const char* debug_file_name) {
332 android::FileMap* file_map = new android::FileMap;
333 const bool success = file_map->create(debug_file_name, fd, start, length, read_only);
334 if (!success) {
335 file_map->release();
336 return NULL;
Narayan Kamath7462f022013-11-21 13:05:04 +0000337 }
338
Narayan Kamatheaf98852013-12-11 14:51:51 +0000339 return file_map;
Narayan Kamath7462f022013-11-21 13:05:04 +0000340}
341
342static int32_t CopyFileToFile(int fd, uint8_t* begin, const uint32_t length, uint64_t *crc_out) {
343 static const uint32_t kBufSize = 32768;
344 uint8_t buf[kBufSize];
345
346 uint32_t count = 0;
347 uint64_t crc = 0;
Narayan Kamath58aaf462013-12-10 16:47:14 +0000348 while (count < length) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000349 uint32_t remaining = length - count;
350
351 // Safe conversion because kBufSize is narrow enough for a 32 bit signed
352 // value.
353 ssize_t get_size = (remaining > kBufSize) ? kBufSize : remaining;
354 ssize_t actual = TEMP_FAILURE_RETRY(read(fd, buf, get_size));
355
356 if (actual != get_size) {
Mark Salyzyn51d562d2014-05-05 14:38:05 -0700357 ALOGW("CopyFileToFile: copy read failed (" ZD " vs " ZD ")", actual, get_size);
Narayan Kamath7462f022013-11-21 13:05:04 +0000358 return kIoError;
359 }
360
361 memcpy(begin + count, buf, get_size);
362 crc = crc32(crc, buf, get_size);
363 count += get_size;
364 }
365
366 *crc_out = crc;
367
368 return 0;
369}
370
371/*
372 * Round up to the next highest power of 2.
373 *
374 * Found on http://graphics.stanford.edu/~seander/bithacks.html.
375 */
376static uint32_t RoundUpPower2(uint32_t val) {
377 val--;
378 val |= val >> 1;
379 val |= val >> 2;
380 val |= val >> 4;
381 val |= val >> 8;
382 val |= val >> 16;
383 val++;
384
385 return val;
386}
387
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100388static uint32_t ComputeHash(const ZipEntryName& name) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000389 uint32_t hash = 0;
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100390 uint16_t len = name.name_length;
391 const uint8_t* str = name.name;
Narayan Kamath7462f022013-11-21 13:05:04 +0000392
393 while (len--) {
394 hash = hash * 31 + *str++;
395 }
396
397 return hash;
398}
399
400/*
401 * Convert a ZipEntry to a hash table index, verifying that it's in a
402 * valid range.
403 */
404static int64_t EntryToIndex(const ZipEntryName* hash_table,
405 const uint32_t hash_table_size,
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100406 const ZipEntryName& name) {
407 const uint32_t hash = ComputeHash(name);
Narayan Kamath7462f022013-11-21 13:05:04 +0000408
409 // NOTE: (hash_table_size - 1) is guaranteed to be non-negative.
410 uint32_t ent = hash & (hash_table_size - 1);
411 while (hash_table[ent].name != NULL) {
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100412 if (hash_table[ent].name_length == name.name_length &&
413 memcmp(hash_table[ent].name, name.name, name.name_length) == 0) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000414 return ent;
415 }
416
417 ent = (ent + 1) & (hash_table_size - 1);
418 }
419
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100420 ALOGV("Zip: Unable to find entry %.*s", name.name_length, name.name);
Narayan Kamath7462f022013-11-21 13:05:04 +0000421 return kEntryNotFound;
422}
423
424/*
425 * Add a new entry to the hash table.
426 */
427static int32_t AddToHash(ZipEntryName *hash_table, const uint64_t hash_table_size,
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100428 const ZipEntryName& name) {
429 const uint64_t hash = ComputeHash(name);
Narayan Kamath7462f022013-11-21 13:05:04 +0000430 uint32_t ent = hash & (hash_table_size - 1);
431
432 /*
433 * We over-allocated the table, so we're guaranteed to find an empty slot.
434 * Further, we guarantee that the hashtable size is not 0.
435 */
436 while (hash_table[ent].name != NULL) {
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100437 if (hash_table[ent].name_length == name.name_length &&
438 memcmp(hash_table[ent].name, name.name, name.name_length) == 0) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000439 // We've found a duplicate entry. We don't accept it
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100440 ALOGW("Zip: Found duplicate entry %.*s", name.name_length, name.name);
Narayan Kamath7462f022013-11-21 13:05:04 +0000441 return kDuplicateEntry;
442 }
443 ent = (ent + 1) & (hash_table_size - 1);
444 }
445
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100446 hash_table[ent].name = name.name;
447 hash_table[ent].name_length = name.name_length;
Narayan Kamath7462f022013-11-21 13:05:04 +0000448 return 0;
449}
450
Narayan Kamath7462f022013-11-21 13:05:04 +0000451static int32_t MapCentralDirectory0(int fd, const char* debug_file_name,
452 ZipArchive* archive, off64_t file_length,
Narayan Kamath926973e2014-06-09 14:18:14 +0100453 off64_t read_amount, uint8_t* scan_buffer) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000454 const off64_t search_start = file_length - read_amount;
455
456 if (lseek64(fd, search_start, SEEK_SET) != search_start) {
Narayan Kamath926973e2014-06-09 14:18:14 +0100457 ALOGW("Zip: seek %" PRId64 " failed: %s", static_cast<int64_t>(search_start),
458 strerror(errno));
Narayan Kamath7462f022013-11-21 13:05:04 +0000459 return kIoError;
460 }
Narayan Kamath926973e2014-06-09 14:18:14 +0100461 ssize_t actual = TEMP_FAILURE_RETRY(
462 read(fd, scan_buffer, static_cast<size_t>(read_amount)));
463 if (actual != static_cast<ssize_t>(read_amount)) {
464 ALOGW("Zip: read %" PRId64 " failed: %s", static_cast<int64_t>(read_amount),
465 strerror(errno));
Narayan Kamath7462f022013-11-21 13:05:04 +0000466 return kIoError;
467 }
468
469 /*
470 * Scan backward for the EOCD magic. In an archive without a trailing
471 * comment, we'll find it on the first try. (We may want to consider
472 * doing an initial minimal read; if we don't find it, retry with a
473 * second read as above.)
474 */
Narayan Kamath926973e2014-06-09 14:18:14 +0100475 int i = read_amount - sizeof(EocdRecord);
476 for (; i >= 0; i--) {
477 if (scan_buffer[i] == 0x50 &&
478 ((*reinterpret_cast<uint32_t*>(&scan_buffer[i])) == EocdRecord::kSignature)) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000479 ALOGV("+++ Found EOCD at buf+%d", i);
480 break;
481 }
482 }
483 if (i < 0) {
484 ALOGD("Zip: EOCD not found, %s is not zip", debug_file_name);
485 return kInvalidFile;
486 }
487
488 const off64_t eocd_offset = search_start + i;
Narayan Kamath926973e2014-06-09 14:18:14 +0100489 const EocdRecord* eocd = reinterpret_cast<const EocdRecord*>(scan_buffer + i);
Narayan Kamath7462f022013-11-21 13:05:04 +0000490 /*
Narayan Kamath926973e2014-06-09 14:18:14 +0100491 * Verify that there's no trailing space at the end of the central directory
492 * and its comment.
Narayan Kamath7462f022013-11-21 13:05:04 +0000493 */
Narayan Kamath926973e2014-06-09 14:18:14 +0100494 const off64_t calculated_length = eocd_offset + sizeof(EocdRecord)
495 + eocd->comment_length;
496 if (calculated_length != file_length) {
Narayan Kamath4f6b4992014-06-03 13:59:23 +0100497 ALOGW("Zip: %" PRId64 " extraneous bytes at the end of the central directory",
Narayan Kamath926973e2014-06-09 14:18:14 +0100498 static_cast<int64_t>(file_length - calculated_length));
Narayan Kamath4f6b4992014-06-03 13:59:23 +0100499 return kInvalidFile;
500 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000501
Narayan Kamath926973e2014-06-09 14:18:14 +0100502 /*
503 * Grab the CD offset and size, and the number of entries in the
504 * archive and verify that they look reasonable.
505 */
506 if (eocd->cd_start_offset + eocd->cd_size > eocd_offset) {
507 ALOGW("Zip: bad offsets (dir %" PRIu32 ", size %" PRIu32 ", eocd %" PRId64 ")",
508 eocd->cd_start_offset, eocd->cd_size, static_cast<int64_t>(eocd_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000509 return kInvalidOffset;
510 }
Narayan Kamath926973e2014-06-09 14:18:14 +0100511 if (eocd->num_records == 0) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000512 ALOGW("Zip: empty archive?");
513 return kEmptyArchive;
514 }
515
Narayan Kamath926973e2014-06-09 14:18:14 +0100516 ALOGV("+++ num_entries=%" PRIu32 "dir_size=%" PRIu32 " dir_offset=%" PRIu32,
517 eocd->num_records, eocd->cd_size, eocd->cd_start_offset);
Narayan Kamath7462f022013-11-21 13:05:04 +0000518
519 /*
520 * It all looks good. Create a mapping for the CD, and set the fields
521 * in archive.
522 */
Narayan Kamath926973e2014-06-09 14:18:14 +0100523 android::FileMap* map = MapFileSegment(fd,
524 static_cast<off64_t>(eocd->cd_start_offset),
525 static_cast<size_t>(eocd->cd_size),
526 true /* read only */, debug_file_name);
Narayan Kamatheaf98852013-12-11 14:51:51 +0000527 if (map == NULL) {
528 archive->directory_map = NULL;
529 return kMmapFailed;
Narayan Kamath7462f022013-11-21 13:05:04 +0000530 }
531
Narayan Kamatheaf98852013-12-11 14:51:51 +0000532 archive->directory_map = map;
Narayan Kamath926973e2014-06-09 14:18:14 +0100533 archive->num_entries = eocd->num_records;
534 archive->directory_offset = eocd->cd_start_offset;
Narayan Kamath7462f022013-11-21 13:05:04 +0000535
536 return 0;
537}
538
539/*
540 * Find the zip Central Directory and memory-map it.
541 *
542 * On success, returns 0 after populating fields from the EOCD area:
543 * directory_offset
544 * directory_map
545 * num_entries
546 */
547static int32_t MapCentralDirectory(int fd, const char* debug_file_name,
548 ZipArchive* archive) {
549
550 // Test file length. We use lseek64 to make sure the file
551 // is small enough to be a zip file (Its size must be less than
552 // 0xffffffff bytes).
553 off64_t file_length = lseek64(fd, 0, SEEK_END);
554 if (file_length == -1) {
555 ALOGV("Zip: lseek on fd %d failed", fd);
556 return kInvalidFile;
557 }
558
559 if (file_length > (off64_t) 0xffffffff) {
Narayan Kamath926973e2014-06-09 14:18:14 +0100560 ALOGV("Zip: zip file too long %" PRId64, static_cast<int64_t>(file_length));
Narayan Kamath7462f022013-11-21 13:05:04 +0000561 return kInvalidFile;
562 }
563
Narayan Kamath926973e2014-06-09 14:18:14 +0100564 if (file_length < static_cast<off64_t>(sizeof(EocdRecord))) {
565 ALOGV("Zip: length %" PRId64 " is too small to be zip", static_cast<int64_t>(file_length));
Narayan Kamath7462f022013-11-21 13:05:04 +0000566 return kInvalidFile;
567 }
568
569 /*
570 * Perform the traditional EOCD snipe hunt.
571 *
572 * We're searching for the End of Central Directory magic number,
573 * which appears at the start of the EOCD block. It's followed by
574 * 18 bytes of EOCD stuff and up to 64KB of archive comment. We
575 * need to read the last part of the file into a buffer, dig through
576 * it to find the magic number, parse some values out, and use those
577 * to determine the extent of the CD.
578 *
579 * We start by pulling in the last part of the file.
580 */
Narayan Kamath926973e2014-06-09 14:18:14 +0100581 off64_t read_amount = kMaxEOCDSearch;
582 if (file_length < read_amount) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000583 read_amount = file_length;
584 }
585
Narayan Kamath926973e2014-06-09 14:18:14 +0100586 uint8_t* scan_buffer = reinterpret_cast<uint8_t*>(malloc(read_amount));
Narayan Kamath7462f022013-11-21 13:05:04 +0000587 int32_t result = MapCentralDirectory0(fd, debug_file_name, archive,
588 file_length, read_amount, scan_buffer);
589
590 free(scan_buffer);
591 return result;
592}
593
594/*
595 * Parses the Zip archive's Central Directory. Allocates and populates the
596 * hash table.
597 *
598 * Returns 0 on success.
599 */
600static int32_t ParseZipArchive(ZipArchive* archive) {
601 int32_t result = -1;
Narayan Kamath926973e2014-06-09 14:18:14 +0100602 const uint8_t* const cd_ptr = (const uint8_t*) archive->directory_map->getDataPtr();
603 const size_t cd_length = archive->directory_map->getDataLength();
604 const uint16_t num_entries = archive->num_entries;
Narayan Kamath7462f022013-11-21 13:05:04 +0000605
606 /*
607 * Create hash table. We have a minimum 75% load factor, possibly as
608 * low as 50% after we round off to a power of 2. There must be at
609 * least one unused entry to avoid an infinite loop during creation.
610 */
611 archive->hash_table_size = RoundUpPower2(1 + (num_entries * 4) / 3);
612 archive->hash_table = (ZipEntryName*) calloc(archive->hash_table_size,
613 sizeof(ZipEntryName));
614
615 /*
616 * Walk through the central directory, adding entries to the hash
617 * table and verifying values.
618 */
Narayan Kamath926973e2014-06-09 14:18:14 +0100619 const uint8_t* const cd_end = cd_ptr + cd_length;
Narayan Kamath7462f022013-11-21 13:05:04 +0000620 const uint8_t* ptr = cd_ptr;
621 for (uint16_t i = 0; i < num_entries; i++) {
Narayan Kamath926973e2014-06-09 14:18:14 +0100622 const CentralDirectoryRecord* cdr =
623 reinterpret_cast<const CentralDirectoryRecord*>(ptr);
624 if (cdr->record_signature != CentralDirectoryRecord::kSignature) {
Mark Salyzyn088bf902014-05-08 16:02:20 -0700625 ALOGW("Zip: missed a central dir sig (at %" PRIu16 ")", i);
Narayan Kamath7462f022013-11-21 13:05:04 +0000626 goto bail;
627 }
628
Narayan Kamath926973e2014-06-09 14:18:14 +0100629 if (ptr + sizeof(CentralDirectoryRecord) > cd_end) {
Mark Salyzyn088bf902014-05-08 16:02:20 -0700630 ALOGW("Zip: ran off the end (at %" PRIu16 ")", i);
Narayan Kamath7462f022013-11-21 13:05:04 +0000631 goto bail;
632 }
633
Narayan Kamath926973e2014-06-09 14:18:14 +0100634 const off64_t local_header_offset = cdr->local_file_header_offset;
Narayan Kamath7462f022013-11-21 13:05:04 +0000635 if (local_header_offset >= archive->directory_offset) {
Mark Salyzyn56a90a02014-05-08 17:20:55 -0700636 ALOGW("Zip: bad LFH offset %" PRId64 " at entry %" PRIu16, (int64_t)local_header_offset, i);
Narayan Kamath7462f022013-11-21 13:05:04 +0000637 goto bail;
638 }
639
Narayan Kamath926973e2014-06-09 14:18:14 +0100640 const uint16_t file_name_length = cdr->file_name_length;
641 const uint16_t extra_length = cdr->extra_field_length;
642 const uint16_t comment_length = cdr->comment_length;
Narayan Kamath7462f022013-11-21 13:05:04 +0000643
644 /* add the CDE filename to the hash table */
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100645 const uint8_t* file_name = ptr + sizeof(CentralDirectoryRecord);
646 ZipEntryName entry_name;
647 entry_name.name = file_name;
648 entry_name.name_length = file_name_length;
Narayan Kamath7462f022013-11-21 13:05:04 +0000649 const int add_result = AddToHash(archive->hash_table,
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100650 archive->hash_table_size, entry_name);
Narayan Kamath7462f022013-11-21 13:05:04 +0000651 if (add_result) {
652 ALOGW("Zip: Error adding entry to hash table %d", add_result);
653 result = add_result;
654 goto bail;
655 }
656
Narayan Kamath926973e2014-06-09 14:18:14 +0100657 ptr += sizeof(CentralDirectoryRecord) + file_name_length + extra_length + comment_length;
658 if ((ptr - cd_ptr) > static_cast<int64_t>(cd_length)) {
Mark Salyzyn088bf902014-05-08 16:02:20 -0700659 ALOGW("Zip: bad CD advance (%tu vs %zu) at entry %" PRIu16,
660 ptr - cd_ptr, cd_length, i);
Narayan Kamath7462f022013-11-21 13:05:04 +0000661 goto bail;
662 }
663 }
Mark Salyzyn088bf902014-05-08 16:02:20 -0700664 ALOGV("+++ zip good scan %" PRIu16 " entries", num_entries);
Narayan Kamath7462f022013-11-21 13:05:04 +0000665
666 result = 0;
667
668bail:
669 return result;
670}
671
672static int32_t OpenArchiveInternal(ZipArchive* archive,
673 const char* debug_file_name) {
674 int32_t result = -1;
675 if ((result = MapCentralDirectory(archive->fd, debug_file_name, archive))) {
676 return result;
677 }
678
679 if ((result = ParseZipArchive(archive))) {
680 return result;
681 }
682
683 return 0;
684}
685
686int32_t OpenArchiveFd(int fd, const char* debug_file_name,
687 ZipArchiveHandle* handle) {
Neil Fullerb1a113f2014-07-25 14:43:04 +0100688 ZipArchive* archive = new ZipArchive(fd);
Narayan Kamath7462f022013-11-21 13:05:04 +0000689 *handle = archive;
Narayan Kamath7462f022013-11-21 13:05:04 +0000690 return OpenArchiveInternal(archive, debug_file_name);
691}
692
693int32_t OpenArchive(const char* fileName, ZipArchiveHandle* handle) {
Neil Fullerb1a113f2014-07-25 14:43:04 +0100694 const int fd = open(fileName, O_RDONLY | O_BINARY, 0);
695 ZipArchive* archive = new ZipArchive(fd);
Narayan Kamath7462f022013-11-21 13:05:04 +0000696 *handle = archive;
697
Narayan Kamath7462f022013-11-21 13:05:04 +0000698 if (fd < 0) {
699 ALOGW("Unable to open '%s': %s", fileName, strerror(errno));
700 return kIoError;
Narayan Kamath7462f022013-11-21 13:05:04 +0000701 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000702 return OpenArchiveInternal(archive, fileName);
703}
704
705/*
706 * Close a ZipArchive, closing the file and freeing the contents.
707 */
708void CloseArchive(ZipArchiveHandle handle) {
709 ZipArchive* archive = (ZipArchive*) handle;
710 ALOGV("Closing archive %p", archive);
Neil Fullerb1a113f2014-07-25 14:43:04 +0100711 delete archive;
Narayan Kamath7462f022013-11-21 13:05:04 +0000712}
713
714static int32_t UpdateEntryFromDataDescriptor(int fd,
715 ZipEntry *entry) {
Narayan Kamath926973e2014-06-09 14:18:14 +0100716 uint8_t ddBuf[sizeof(DataDescriptor) + sizeof(DataDescriptor::kOptSignature)];
Narayan Kamath7462f022013-11-21 13:05:04 +0000717 ssize_t actual = TEMP_FAILURE_RETRY(read(fd, ddBuf, sizeof(ddBuf)));
718 if (actual != sizeof(ddBuf)) {
719 return kIoError;
720 }
721
Narayan Kamath926973e2014-06-09 14:18:14 +0100722 const uint32_t ddSignature = *(reinterpret_cast<const uint32_t*>(ddBuf));
723 const uint16_t offset = (ddSignature == DataDescriptor::kOptSignature) ? 4 : 0;
724 const DataDescriptor* descriptor = reinterpret_cast<const DataDescriptor*>(ddBuf + offset);
Narayan Kamath7462f022013-11-21 13:05:04 +0000725
Narayan Kamath926973e2014-06-09 14:18:14 +0100726 entry->crc32 = descriptor->crc32;
727 entry->compressed_length = descriptor->compressed_size;
728 entry->uncompressed_length = descriptor->uncompressed_size;
Narayan Kamath7462f022013-11-21 13:05:04 +0000729
730 return 0;
731}
732
733// Attempts to read |len| bytes into |buf| at offset |off|.
734//
735// This method uses pread64 on platforms that support it and
736// lseek64 + read on platforms that don't. This implies that
737// callers should not rely on the |fd| offset being incremented
738// as a side effect of this call.
739static inline ssize_t ReadAtOffset(int fd, uint8_t* buf, size_t len,
740 off64_t off) {
741#ifdef HAVE_PREAD
742 return TEMP_FAILURE_RETRY(pread64(fd, buf, len, off));
743#else
744 // The only supported platform that doesn't support pread at the moment
745 // is Windows. Only recent versions of windows support unix like forks,
746 // and even there the semantics are quite different.
747 if (lseek64(fd, off, SEEK_SET) != off) {
Mark Salyzyn99ef9912014-03-14 14:26:22 -0700748 ALOGW("Zip: failed seek to offset %" PRId64, off);
Narayan Kamath7462f022013-11-21 13:05:04 +0000749 return kIoError;
750 }
751
752 return TEMP_FAILURE_RETRY(read(fd, buf, len));
753#endif // HAVE_PREAD
754}
755
756static int32_t FindEntry(const ZipArchive* archive, const int ent,
757 ZipEntry* data) {
758 const uint16_t nameLen = archive->hash_table[ent].name_length;
Narayan Kamath7462f022013-11-21 13:05:04 +0000759
760 // Recover the start of the central directory entry from the filename
761 // pointer. The filename is the first entry past the fixed-size data,
762 // so we can just subtract back from that.
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100763 const uint8_t* ptr = archive->hash_table[ent].name;
Narayan Kamath926973e2014-06-09 14:18:14 +0100764 ptr -= sizeof(CentralDirectoryRecord);
Narayan Kamath7462f022013-11-21 13:05:04 +0000765
766 // This is the base of our mmapped region, we have to sanity check that
767 // the name that's in the hash table is a pointer to a location within
768 // this mapped region.
Narayan Kamath926973e2014-06-09 14:18:14 +0100769 const uint8_t* base_ptr = reinterpret_cast<const uint8_t*>(
770 archive->directory_map->getDataPtr());
Narayan Kamatheaf98852013-12-11 14:51:51 +0000771 if (ptr < base_ptr || ptr > base_ptr + archive->directory_map->getDataLength()) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000772 ALOGW("Zip: Invalid entry pointer");
773 return kInvalidOffset;
774 }
775
Narayan Kamath926973e2014-06-09 14:18:14 +0100776 const CentralDirectoryRecord *cdr =
777 reinterpret_cast<const CentralDirectoryRecord*>(ptr);
778
Narayan Kamath7462f022013-11-21 13:05:04 +0000779 // The offset of the start of the central directory in the zipfile.
780 // We keep this lying around so that we can sanity check all our lengths
781 // and our per-file structures.
782 const off64_t cd_offset = archive->directory_offset;
783
784 // Fill out the compression method, modification time, crc32
785 // and other interesting attributes from the central directory. These
786 // will later be compared against values from the local file header.
Narayan Kamath926973e2014-06-09 14:18:14 +0100787 data->method = cdr->compression_method;
788 data->mod_time = cdr->last_mod_time;
789 data->crc32 = cdr->crc32;
790 data->compressed_length = cdr->compressed_size;
791 data->uncompressed_length = cdr->uncompressed_size;
Narayan Kamath7462f022013-11-21 13:05:04 +0000792
793 // Figure out the local header offset from the central directory. The
794 // actual file data will begin after the local header and the name /
795 // extra comments.
Narayan Kamath926973e2014-06-09 14:18:14 +0100796 const off64_t local_header_offset = cdr->local_file_header_offset;
797 if (local_header_offset + static_cast<off64_t>(sizeof(LocalFileHeader)) >= cd_offset) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000798 ALOGW("Zip: bad local hdr offset in zip");
799 return kInvalidOffset;
800 }
801
Narayan Kamath926973e2014-06-09 14:18:14 +0100802 uint8_t lfh_buf[sizeof(LocalFileHeader)];
Narayan Kamath7462f022013-11-21 13:05:04 +0000803 ssize_t actual = ReadAtOffset(archive->fd, lfh_buf, sizeof(lfh_buf),
804 local_header_offset);
805 if (actual != sizeof(lfh_buf)) {
Mark Salyzyn56a90a02014-05-08 17:20:55 -0700806 ALOGW("Zip: failed reading lfh name from offset %" PRId64, (int64_t)local_header_offset);
Narayan Kamath7462f022013-11-21 13:05:04 +0000807 return kIoError;
808 }
809
Narayan Kamath926973e2014-06-09 14:18:14 +0100810 const LocalFileHeader *lfh = reinterpret_cast<const LocalFileHeader*>(lfh_buf);
811
812 if (lfh->lfh_signature != LocalFileHeader::kSignature) {
Mark Salyzyn99ef9912014-03-14 14:26:22 -0700813 ALOGW("Zip: didn't find signature at start of lfh, offset=%" PRId64,
Narayan Kamath926973e2014-06-09 14:18:14 +0100814 static_cast<int64_t>(local_header_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000815 return kInvalidOffset;
816 }
817
818 // Paranoia: Match the values specified in the local file header
819 // to those specified in the central directory.
Narayan Kamath926973e2014-06-09 14:18:14 +0100820 if ((lfh->gpb_flags & kGPBDDFlagMask) == 0) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000821 data->has_data_descriptor = 0;
Narayan Kamath926973e2014-06-09 14:18:14 +0100822 if (data->compressed_length != lfh->compressed_size
823 || data->uncompressed_length != lfh->uncompressed_size
824 || data->crc32 != lfh->crc32) {
Mark Salyzyn088bf902014-05-08 16:02:20 -0700825 ALOGW("Zip: size/crc32 mismatch. expected {%" PRIu32 ", %" PRIu32
826 ", %" PRIx32 "}, was {%" PRIu32 ", %" PRIu32 ", %" PRIx32 "}",
Narayan Kamath7462f022013-11-21 13:05:04 +0000827 data->compressed_length, data->uncompressed_length, data->crc32,
Narayan Kamath926973e2014-06-09 14:18:14 +0100828 lfh->compressed_size, lfh->uncompressed_size, lfh->crc32);
Narayan Kamath7462f022013-11-21 13:05:04 +0000829 return kInconsistentInformation;
830 }
831 } else {
832 data->has_data_descriptor = 1;
833 }
834
835 // Check that the local file header name matches the declared
836 // name in the central directory.
Narayan Kamath926973e2014-06-09 14:18:14 +0100837 if (lfh->file_name_length == nameLen) {
838 const off64_t name_offset = local_header_offset + sizeof(LocalFileHeader);
839 if (name_offset + lfh->file_name_length >= cd_offset) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000840 ALOGW("Zip: Invalid declared length");
841 return kInvalidOffset;
842 }
843
844 uint8_t* name_buf = (uint8_t*) malloc(nameLen);
845 ssize_t actual = ReadAtOffset(archive->fd, name_buf, nameLen,
846 name_offset);
847
848 if (actual != nameLen) {
Mark Salyzyn56a90a02014-05-08 17:20:55 -0700849 ALOGW("Zip: failed reading lfh name from offset %" PRId64, (int64_t)name_offset);
Narayan Kamath7462f022013-11-21 13:05:04 +0000850 free(name_buf);
851 return kIoError;
852 }
853
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100854 if (memcmp(archive->hash_table[ent].name, name_buf, nameLen)) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000855 free(name_buf);
856 return kInconsistentInformation;
857 }
858
859 free(name_buf);
860 } else {
861 ALOGW("Zip: lfh name did not match central directory.");
862 return kInconsistentInformation;
863 }
864
Narayan Kamath926973e2014-06-09 14:18:14 +0100865 const off64_t data_offset = local_header_offset + sizeof(LocalFileHeader)
866 + lfh->file_name_length + lfh->extra_field_length;
Narayan Kamath48953a12014-01-24 12:32:39 +0000867 if (data_offset > cd_offset) {
Mark Salyzyn56a90a02014-05-08 17:20:55 -0700868 ALOGW("Zip: bad data offset %" PRId64 " in zip", (int64_t)data_offset);
Narayan Kamath7462f022013-11-21 13:05:04 +0000869 return kInvalidOffset;
870 }
871
872 if ((off64_t)(data_offset + data->compressed_length) > cd_offset) {
Mark Salyzyn088bf902014-05-08 16:02:20 -0700873 ALOGW("Zip: bad compressed length in zip (%" PRId64 " + %" PRIu32 " > %" PRId64 ")",
Mark Salyzyn56a90a02014-05-08 17:20:55 -0700874 (int64_t)data_offset, data->compressed_length, (int64_t)cd_offset);
Narayan Kamath7462f022013-11-21 13:05:04 +0000875 return kInvalidOffset;
876 }
877
878 if (data->method == kCompressStored &&
879 (off64_t)(data_offset + data->uncompressed_length) > cd_offset) {
Mark Salyzyn088bf902014-05-08 16:02:20 -0700880 ALOGW("Zip: bad uncompressed length in zip (%" PRId64 " + %" PRIu32 " > %" PRId64 ")",
Mark Salyzyn96c5c992014-05-08 19:16:40 -0700881 (int64_t)data_offset, data->uncompressed_length, (int64_t)cd_offset);
Narayan Kamath7462f022013-11-21 13:05:04 +0000882 return kInvalidOffset;
883 }
884
885 data->offset = data_offset;
886 return 0;
887}
888
889struct IterationHandle {
890 uint32_t position;
Piotr Jastrzebski8e085362014-08-18 11:37:45 +0100891 const uint8_t* prefix;
892 uint16_t prefix_len;
Narayan Kamath7462f022013-11-21 13:05:04 +0000893 ZipArchive* archive;
Piotr Jastrzebski8e085362014-08-18 11:37:45 +0100894
895 IterationHandle() : prefix(NULL), prefix_len(0) {}
896
897 IterationHandle(const ZipEntryName& prefix_name)
898 : prefix_len(prefix_name.name_length) {
899 uint8_t* prefix_copy = new uint8_t[prefix_len];
900 memcpy(reinterpret_cast<void*>(prefix_copy), prefix_name.name,
901 prefix_len * sizeof(uint8_t));
902 prefix = prefix_copy;
903 }
904
905 ~IterationHandle() {
906 delete [] prefix;
907 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000908};
909
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100910int32_t StartIteration(ZipArchiveHandle handle, void** cookie_ptr,
911 const ZipEntryName* optional_prefix) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000912 ZipArchive* archive = (ZipArchive *) handle;
913
914 if (archive == NULL || archive->hash_table == NULL) {
915 ALOGW("Zip: Invalid ZipArchiveHandle");
916 return kInvalidHandle;
917 }
918
Piotr Jastrzebski8e085362014-08-18 11:37:45 +0100919 IterationHandle* cookie =
920 optional_prefix != NULL ? new IterationHandle(*optional_prefix) : new IterationHandle();
Narayan Kamath7462f022013-11-21 13:05:04 +0000921 cookie->position = 0;
Narayan Kamath7462f022013-11-21 13:05:04 +0000922 cookie->archive = archive;
Narayan Kamath7462f022013-11-21 13:05:04 +0000923
924 *cookie_ptr = cookie ;
925 return 0;
926}
927
Piotr Jastrzebski79c8b342014-08-08 14:02:17 +0100928void EndIteration(void* cookie) {
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100929 delete reinterpret_cast<IterationHandle*>(cookie);
Piotr Jastrzebski79c8b342014-08-08 14:02:17 +0100930}
931
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100932int32_t FindEntry(const ZipArchiveHandle handle, const ZipEntryName& entryName,
Narayan Kamath7462f022013-11-21 13:05:04 +0000933 ZipEntry* data) {
934 const ZipArchive* archive = (ZipArchive*) handle;
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100935 if (entryName.name_length == 0) {
936 ALOGW("Zip: Invalid filename %.*s", entryName.name_length, entryName.name);
Narayan Kamath7462f022013-11-21 13:05:04 +0000937 return kInvalidEntryName;
938 }
939
940 const int64_t ent = EntryToIndex(archive->hash_table,
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100941 archive->hash_table_size, entryName);
Narayan Kamath7462f022013-11-21 13:05:04 +0000942
943 if (ent < 0) {
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100944 ALOGV("Zip: Could not find entry %.*s", entryName.name_length, entryName.name);
Narayan Kamath7462f022013-11-21 13:05:04 +0000945 return ent;
946 }
947
948 return FindEntry(archive, ent, data);
949}
950
951int32_t Next(void* cookie, ZipEntry* data, ZipEntryName* name) {
952 IterationHandle* handle = (IterationHandle *) cookie;
953 if (handle == NULL) {
954 return kInvalidHandle;
955 }
956
957 ZipArchive* archive = handle->archive;
958 if (archive == NULL || archive->hash_table == NULL) {
959 ALOGW("Zip: Invalid ZipArchiveHandle");
960 return kInvalidHandle;
961 }
962
963 const uint32_t currentOffset = handle->position;
964 const uint32_t hash_table_length = archive->hash_table_size;
965 const ZipEntryName *hash_table = archive->hash_table;
966
967 for (uint32_t i = currentOffset; i < hash_table_length; ++i) {
968 if (hash_table[i].name != NULL &&
Piotr Jastrzebski8e085362014-08-18 11:37:45 +0100969 (handle->prefix_len == 0 ||
970 (memcmp(handle->prefix, hash_table[i].name, handle->prefix_len) == 0))) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000971 handle->position = (i + 1);
972 const int error = FindEntry(archive, i, data);
973 if (!error) {
974 name->name = hash_table[i].name;
975 name->name_length = hash_table[i].name_length;
976 }
977
978 return error;
979 }
980 }
981
982 handle->position = 0;
983 return kIterationEnd;
984}
985
986static int32_t InflateToFile(int fd, const ZipEntry* entry,
987 uint8_t* begin, uint32_t length,
988 uint64_t* crc_out) {
989 int32_t result = -1;
990 const uint32_t kBufSize = 32768;
991 uint8_t read_buf[kBufSize];
992 uint8_t write_buf[kBufSize];
993 z_stream zstream;
994 int zerr;
995
996 /*
997 * Initialize the zlib stream struct.
998 */
999 memset(&zstream, 0, sizeof(zstream));
1000 zstream.zalloc = Z_NULL;
1001 zstream.zfree = Z_NULL;
1002 zstream.opaque = Z_NULL;
1003 zstream.next_in = NULL;
1004 zstream.avail_in = 0;
1005 zstream.next_out = (Bytef*) write_buf;
1006 zstream.avail_out = kBufSize;
1007 zstream.data_type = Z_UNKNOWN;
1008
1009 /*
1010 * Use the undocumented "negative window bits" feature to tell zlib
1011 * that there's no zlib header waiting for it.
1012 */
1013 zerr = inflateInit2(&zstream, -MAX_WBITS);
1014 if (zerr != Z_OK) {
1015 if (zerr == Z_VERSION_ERROR) {
1016 ALOGE("Installed zlib is not compatible with linked version (%s)",
1017 ZLIB_VERSION);
1018 } else {
1019 ALOGW("Call to inflateInit2 failed (zerr=%d)", zerr);
1020 }
1021
1022 return kZlibError;
1023 }
1024
1025 const uint32_t uncompressed_length = entry->uncompressed_length;
1026
1027 uint32_t compressed_length = entry->compressed_length;
1028 uint32_t write_count = 0;
1029 do {
1030 /* read as much as we can */
1031 if (zstream.avail_in == 0) {
Mark Salyzyn51d562d2014-05-05 14:38:05 -07001032 const ZD_TYPE getSize = (compressed_length > kBufSize) ? kBufSize : compressed_length;
1033 const ZD_TYPE actual = TEMP_FAILURE_RETRY(read(fd, read_buf, getSize));
Narayan Kamath7462f022013-11-21 13:05:04 +00001034 if (actual != getSize) {
Mark Salyzyn51d562d2014-05-05 14:38:05 -07001035 ALOGW("Zip: inflate read failed (" ZD " vs " ZD ")", actual, getSize);
Narayan Kamath7462f022013-11-21 13:05:04 +00001036 result = kIoError;
1037 goto z_bail;
1038 }
1039
1040 compressed_length -= getSize;
1041
1042 zstream.next_in = read_buf;
1043 zstream.avail_in = getSize;
1044 }
1045
1046 /* uncompress the data */
1047 zerr = inflate(&zstream, Z_NO_FLUSH);
1048 if (zerr != Z_OK && zerr != Z_STREAM_END) {
1049 ALOGW("Zip: inflate zerr=%d (nIn=%p aIn=%u nOut=%p aOut=%u)",
1050 zerr, zstream.next_in, zstream.avail_in,
1051 zstream.next_out, zstream.avail_out);
1052 result = kZlibError;
1053 goto z_bail;
1054 }
1055
1056 /* write when we're full or when we're done */
1057 if (zstream.avail_out == 0 ||
1058 (zerr == Z_STREAM_END && zstream.avail_out != kBufSize)) {
1059 const size_t write_size = zstream.next_out - write_buf;
1060 // The file might have declared a bogus length.
1061 if (write_size + write_count > length) {
1062 goto z_bail;
1063 }
1064 memcpy(begin + write_count, write_buf, write_size);
1065 write_count += write_size;
1066
1067 zstream.next_out = write_buf;
1068 zstream.avail_out = kBufSize;
1069 }
1070 } while (zerr == Z_OK);
1071
1072 assert(zerr == Z_STREAM_END); /* other errors should've been caught */
1073
1074 // stream.adler holds the crc32 value for such streams.
1075 *crc_out = zstream.adler;
1076
1077 if (zstream.total_out != uncompressed_length || compressed_length != 0) {
Mark Salyzyn088bf902014-05-08 16:02:20 -07001078 ALOGW("Zip: size mismatch on inflated file (%lu vs %" PRIu32 ")",
Narayan Kamath7462f022013-11-21 13:05:04 +00001079 zstream.total_out, uncompressed_length);
1080 result = kInconsistentInformation;
1081 goto z_bail;
1082 }
1083
1084 result = 0;
1085
1086z_bail:
1087 inflateEnd(&zstream); /* free up any allocated structures */
1088
1089 return result;
1090}
1091
1092int32_t ExtractToMemory(ZipArchiveHandle handle,
1093 ZipEntry* entry, uint8_t* begin, uint32_t size) {
1094 ZipArchive* archive = (ZipArchive*) handle;
1095 const uint16_t method = entry->method;
1096 off64_t data_offset = entry->offset;
1097
1098 if (lseek64(archive->fd, data_offset, SEEK_SET) != data_offset) {
Mark Salyzyn56a90a02014-05-08 17:20:55 -07001099 ALOGW("Zip: lseek to data at %" PRId64 " failed", (int64_t)data_offset);
Narayan Kamath7462f022013-11-21 13:05:04 +00001100 return kIoError;
1101 }
1102
1103 // this should default to kUnknownCompressionMethod.
1104 int32_t return_value = -1;
1105 uint64_t crc = 0;
1106 if (method == kCompressStored) {
1107 return_value = CopyFileToFile(archive->fd, begin, size, &crc);
1108 } else if (method == kCompressDeflated) {
1109 return_value = InflateToFile(archive->fd, entry, begin, size, &crc);
1110 }
1111
1112 if (!return_value && entry->has_data_descriptor) {
1113 return_value = UpdateEntryFromDataDescriptor(archive->fd, entry);
1114 if (return_value) {
1115 return return_value;
1116 }
1117 }
1118
1119 // TODO: Fix this check by passing the right flags to inflate2 so that
1120 // it calculates the CRC for us.
1121 if (entry->crc32 != crc && false) {
Mark Salyzyn088bf902014-05-08 16:02:20 -07001122 ALOGW("Zip: crc mismatch: expected %" PRIu32 ", was %" PRIu64, entry->crc32, crc);
Narayan Kamath7462f022013-11-21 13:05:04 +00001123 return kInconsistentInformation;
1124 }
1125
1126 return return_value;
1127}
1128
1129int32_t ExtractEntryToFile(ZipArchiveHandle handle,
1130 ZipEntry* entry, int fd) {
1131 const int32_t declared_length = entry->uncompressed_length;
1132
Narayan Kamath00a258c2013-12-13 16:06:19 +00001133 const off64_t current_offset = lseek64(fd, 0, SEEK_CUR);
1134 if (current_offset == -1) {
1135 ALOGW("Zip: unable to seek to current location on fd %d: %s", fd,
1136 strerror(errno));
Narayan Kamath7462f022013-11-21 13:05:04 +00001137 return kIoError;
1138 }
1139
Narayan Kamath00a258c2013-12-13 16:06:19 +00001140 int result = TEMP_FAILURE_RETRY(ftruncate(fd, declared_length + current_offset));
1141 if (result == -1) {
Mark Salyzyn99ef9912014-03-14 14:26:22 -07001142 ALOGW("Zip: unable to truncate file to %" PRId64 ": %s",
Mark Salyzyn56a90a02014-05-08 17:20:55 -07001143 (int64_t)(declared_length + current_offset), strerror(errno));
Narayan Kamath00a258c2013-12-13 16:06:19 +00001144 return kIoError;
1145 }
1146
Narayan Kamath48953a12014-01-24 12:32:39 +00001147 // Don't attempt to map a region of length 0. We still need the
1148 // ftruncate() though, since the API guarantees that we will truncate
1149 // the file to the end of the uncompressed output.
1150 if (declared_length == 0) {
1151 return 0;
1152 }
1153
Narayan Kamath00a258c2013-12-13 16:06:19 +00001154 android::FileMap* map = MapFileSegment(fd, current_offset, declared_length,
Narayan Kamatheaf98852013-12-11 14:51:51 +00001155 false, kTempMappingFileName);
1156 if (map == NULL) {
1157 return kMmapFailed;
Narayan Kamath7462f022013-11-21 13:05:04 +00001158 }
1159
Narayan Kamatheaf98852013-12-11 14:51:51 +00001160 const int32_t error = ExtractToMemory(handle, entry,
1161 reinterpret_cast<uint8_t*>(map->getDataPtr()),
1162 map->getDataLength());
1163 map->release();
Narayan Kamath7462f022013-11-21 13:05:04 +00001164 return error;
1165}
1166
1167const char* ErrorCodeString(int32_t error_code) {
1168 if (error_code > kErrorMessageLowerBound && error_code < kErrorMessageUpperBound) {
1169 return kErrorMessages[error_code * -1];
1170 }
1171
1172 return kErrorMessages[0];
1173}
1174
1175int GetFileDescriptor(const ZipArchiveHandle handle) {
1176 return ((ZipArchive*) handle)->fd;
1177}
1178