blob: 7ae9af8589fced373048b33440035ac85666ade3 [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
Narayan Kamath044bc8e2014-12-03 18:22:53 +000036#include "entry_name_utils-inl.h"
Mark Salyzyn99ef9912014-03-14 14:26:22 -070037#include "ziparchive/zip_archive.h"
38
Narayan Kamath044bc8e2014-12-03 18:22:53 +000039
Narayan Kamath926973e2014-06-09 14:18:14 +010040// This is for windows. If we don't open a file in binary mode, weird
Narayan Kamath7462f022013-11-21 13:05:04 +000041// things will happen.
42#ifndef O_BINARY
43#define O_BINARY 0
44#endif
45
Narayan Kamath926973e2014-06-09 14:18:14 +010046#define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
47 TypeName(); \
48 TypeName(const TypeName&); \
49 void operator=(const TypeName&)
Narayan Kamath7462f022013-11-21 13:05:04 +000050
Narayan Kamath926973e2014-06-09 14:18:14 +010051// The "end of central directory" (EOCD) record. Each archive
52// contains exactly once such record which appears at the end of
53// the archive. It contains archive wide information like the
54// number of entries in the archive and the offset to the central
55// directory of the offset.
56struct EocdRecord {
57 static const uint32_t kSignature = 0x06054b50;
Narayan Kamath7462f022013-11-21 13:05:04 +000058
Narayan Kamath926973e2014-06-09 14:18:14 +010059 // End of central directory signature, should always be
60 // |kSignature|.
61 uint32_t eocd_signature;
62 // The number of the current "disk", i.e, the "disk" that this
63 // central directory is on.
64 //
65 // This implementation assumes that each archive spans a single
66 // disk only. i.e, that disk_num == 1.
67 uint16_t disk_num;
68 // The disk where the central directory starts.
69 //
70 // This implementation assumes that each archive spans a single
71 // disk only. i.e, that cd_start_disk == 1.
72 uint16_t cd_start_disk;
73 // The number of central directory records on this disk.
74 //
75 // This implementation assumes that each archive spans a single
76 // disk only. i.e, that num_records_on_disk == num_records.
77 uint16_t num_records_on_disk;
78 // The total number of central directory records.
79 uint16_t num_records;
80 // The size of the central directory (in bytes).
81 uint32_t cd_size;
82 // The offset of the start of the central directory, relative
83 // to the start of the file.
84 uint32_t cd_start_offset;
85 // Length of the central directory comment.
86 uint16_t comment_length;
87 private:
88 DISALLOW_IMPLICIT_CONSTRUCTORS(EocdRecord);
89} __attribute__((packed));
Narayan Kamath7462f022013-11-21 13:05:04 +000090
Narayan Kamath926973e2014-06-09 14:18:14 +010091// A structure representing the fixed length fields for a single
92// record in the central directory of the archive. In addition to
93// the fixed length fields listed here, each central directory
94// record contains a variable length "file_name" and "extra_field"
95// whose lengths are given by |file_name_length| and |extra_field_length|
96// respectively.
97struct CentralDirectoryRecord {
98 static const uint32_t kSignature = 0x02014b50;
Narayan Kamath7462f022013-11-21 13:05:04 +000099
Narayan Kamath926973e2014-06-09 14:18:14 +0100100 // The start of record signature. Must be |kSignature|.
101 uint32_t record_signature;
102 // Tool version. Ignored by this implementation.
103 uint16_t version_made_by;
104 // Tool version. Ignored by this implementation.
105 uint16_t version_needed;
106 // The "general purpose bit flags" for this entry. The only
107 // flag value that we currently check for is the "data descriptor"
108 // flag.
109 uint16_t gpb_flags;
110 // The compression method for this entry, one of |kCompressStored|
111 // and |kCompressDeflated|.
112 uint16_t compression_method;
113 // The file modification time and date for this entry.
114 uint16_t last_mod_time;
115 uint16_t last_mod_date;
116 // The CRC-32 checksum for this entry.
117 uint32_t crc32;
118 // The compressed size (in bytes) of this entry.
119 uint32_t compressed_size;
120 // The uncompressed size (in bytes) of this entry.
121 uint32_t uncompressed_size;
122 // The length of the entry file name in bytes. The file name
123 // will appear immediately after this record.
124 uint16_t file_name_length;
125 // The length of the extra field info (in bytes). This data
126 // will appear immediately after the entry file name.
127 uint16_t extra_field_length;
128 // The length of the entry comment (in bytes). This data will
129 // appear immediately after the extra field.
130 uint16_t comment_length;
131 // The start disk for this entry. Ignored by this implementation).
132 uint16_t file_start_disk;
133 // File attributes. Ignored by this implementation.
134 uint16_t internal_file_attributes;
135 // File attributes. Ignored by this implementation.
136 uint32_t external_file_attributes;
137 // The offset to the local file header for this entry, from the
138 // beginning of this archive.
139 uint32_t local_file_header_offset;
140 private:
141 DISALLOW_IMPLICIT_CONSTRUCTORS(CentralDirectoryRecord);
142} __attribute__((packed));
Narayan Kamath7462f022013-11-21 13:05:04 +0000143
Narayan Kamath926973e2014-06-09 14:18:14 +0100144// The local file header for a given entry. This duplicates information
145// present in the central directory of the archive. It is an error for
146// the information here to be different from the central directory
147// information for a given entry.
148struct LocalFileHeader {
149 static const uint32_t kSignature = 0x04034b50;
Narayan Kamath7462f022013-11-21 13:05:04 +0000150
Narayan Kamath926973e2014-06-09 14:18:14 +0100151 // The local file header signature, must be |kSignature|.
152 uint32_t lfh_signature;
153 // Tool version. Ignored by this implementation.
154 uint16_t version_needed;
155 // The "general purpose bit flags" for this entry. The only
156 // flag value that we currently check for is the "data descriptor"
157 // flag.
158 uint16_t gpb_flags;
159 // The compression method for this entry, one of |kCompressStored|
160 // and |kCompressDeflated|.
161 uint16_t compression_method;
162 // The file modification time and date for this entry.
163 uint16_t last_mod_time;
164 uint16_t last_mod_date;
165 // The CRC-32 checksum for this entry.
166 uint32_t crc32;
167 // The compressed size (in bytes) of this entry.
168 uint32_t compressed_size;
169 // The uncompressed size (in bytes) of this entry.
170 uint32_t uncompressed_size;
171 // The length of the entry file name in bytes. The file name
172 // will appear immediately after this record.
173 uint16_t file_name_length;
174 // The length of the extra field info (in bytes). This data
175 // will appear immediately after the entry file name.
176 uint16_t extra_field_length;
177 private:
178 DISALLOW_IMPLICIT_CONSTRUCTORS(LocalFileHeader);
179} __attribute__((packed));
180
181struct DataDescriptor {
182 // The *optional* data descriptor start signature.
183 static const uint32_t kOptSignature = 0x08074b50;
184
185 // CRC-32 checksum of the entry.
186 uint32_t crc32;
187 // Compressed size of the entry.
188 uint32_t compressed_size;
189 // Uncompressed size of the entry.
190 uint32_t uncompressed_size;
191 private:
192 DISALLOW_IMPLICIT_CONSTRUCTORS(DataDescriptor);
193} __attribute__((packed));
194
195#undef DISALLOW_IMPLICIT_CONSTRUCTORS
196
Piotr Jastrzebskibd0a7482014-08-13 09:49:25 +0000197static const uint32_t kGPBDDFlagMask = 0x0008; // mask value that signifies that the entry has a DD
Narayan Kamath7462f022013-11-21 13:05:04 +0000198
Narayan Kamath926973e2014-06-09 14:18:14 +0100199// The maximum size of a central directory or a file
200// comment in bytes.
201static const uint32_t kMaxCommentLen = 65535;
202
203// The maximum number of bytes to scan backwards for the EOCD start.
204static const uint32_t kMaxEOCDSearch = kMaxCommentLen + sizeof(EocdRecord);
205
Narayan Kamath7462f022013-11-21 13:05:04 +0000206static const char* kErrorMessages[] = {
207 "Unknown return code.",
Narayan Kamatheb41ad22013-12-09 16:26:36 +0000208 "Iteration ended",
Narayan Kamath7462f022013-11-21 13:05:04 +0000209 "Zlib error",
210 "Invalid file",
211 "Invalid handle",
212 "Duplicate entries in archive",
213 "Empty archive",
214 "Entry not found",
215 "Invalid offset",
216 "Inconsistent information",
217 "Invalid entry name",
Narayan Kamatheb41ad22013-12-09 16:26:36 +0000218 "I/O Error",
Narayan Kamatheaf98852013-12-11 14:51:51 +0000219 "File mapping failed"
Narayan Kamath7462f022013-11-21 13:05:04 +0000220};
221
222static const int32_t kErrorMessageUpperBound = 0;
223
Narayan Kamatheb41ad22013-12-09 16:26:36 +0000224static const int32_t kIterationEnd = -1;
Narayan Kamath7462f022013-11-21 13:05:04 +0000225
226// We encountered a Zlib error when inflating a stream from this file.
227// Usually indicates file corruption.
228static const int32_t kZlibError = -2;
229
230// The input file cannot be processed as a zip archive. Usually because
231// it's too small, too large or does not have a valid signature.
232static const int32_t kInvalidFile = -3;
233
234// An invalid iteration / ziparchive handle was passed in as an input
235// argument.
236static const int32_t kInvalidHandle = -4;
237
238// The zip archive contained two (or possibly more) entries with the same
239// name.
240static const int32_t kDuplicateEntry = -5;
241
242// The zip archive contains no entries.
243static const int32_t kEmptyArchive = -6;
244
245// The specified entry was not found in the archive.
246static const int32_t kEntryNotFound = -7;
247
248// The zip archive contained an invalid local file header pointer.
249static const int32_t kInvalidOffset = -8;
250
251// The zip archive contained inconsistent entry information. This could
252// be because the central directory & local file header did not agree, or
253// if the actual uncompressed length or crc32 do not match their declared
254// values.
255static const int32_t kInconsistentInformation = -9;
256
257// An invalid entry name was encountered.
258static const int32_t kInvalidEntryName = -10;
259
Narayan Kamatheb41ad22013-12-09 16:26:36 +0000260// An I/O related system call (read, lseek, ftruncate, map) failed.
261static const int32_t kIoError = -11;
Narayan Kamath7462f022013-11-21 13:05:04 +0000262
Narayan Kamatheaf98852013-12-11 14:51:51 +0000263// We were not able to mmap the central directory or entry contents.
264static const int32_t kMmapFailed = -12;
Narayan Kamath7462f022013-11-21 13:05:04 +0000265
Narayan Kamatheaf98852013-12-11 14:51:51 +0000266static const int32_t kErrorMessageLowerBound = -13;
Narayan Kamath7462f022013-11-21 13:05:04 +0000267
Narayan Kamatheaf98852013-12-11 14:51:51 +0000268static const char kTempMappingFileName[] = "zip: ExtractFileToFile";
Narayan Kamath7462f022013-11-21 13:05:04 +0000269
270/*
271 * A Read-only Zip archive.
272 *
273 * We want "open" and "find entry by name" to be fast operations, and
274 * we want to use as little memory as possible. We memory-map the zip
275 * central directory, and load a hash table with pointers to the filenames
276 * (which aren't null-terminated). The other fields are at a fixed offset
277 * from the filename, so we don't need to extract those (but we do need
278 * to byte-read and endian-swap them every time we want them).
279 *
280 * It's possible that somebody has handed us a massive (~1GB) zip archive,
281 * so we can't expect to mmap the entire file.
282 *
283 * To speed comparisons when doing a lookup by name, we could make the mapping
284 * "private" (copy-on-write) and null-terminate the filenames after verifying
285 * the record structure. However, this requires a private mapping of
286 * every page that the Central Directory touches. Easier to tuck a copy
287 * of the string length into the hash table entry.
288 */
289struct ZipArchive {
290 /* open Zip archive */
Neil Fullerb1a113f2014-07-25 14:43:04 +0100291 const int fd;
Dmitriy Ivanov40b52b22014-07-15 19:33:00 -0700292 const bool close_file;
Narayan Kamath7462f022013-11-21 13:05:04 +0000293
294 /* mapped central directory area */
295 off64_t directory_offset;
Dmitriy Ivanov4b67f832015-03-06 10:22:34 -0800296 android::FileMap directory_map;
Narayan Kamath7462f022013-11-21 13:05:04 +0000297
298 /* number of entries in the Zip archive */
299 uint16_t num_entries;
300
301 /*
302 * We know how many entries are in the Zip archive, so we can have a
303 * fixed-size hash table. We define a load factor of 0.75 and overallocat
304 * so the maximum number entries can never be higher than
305 * ((4 * UINT16_MAX) / 3 + 1) which can safely fit into a uint32_t.
306 */
307 uint32_t hash_table_size;
308 ZipEntryName* hash_table;
Neil Fullerb1a113f2014-07-25 14:43:04 +0100309
Dmitriy Ivanov40b52b22014-07-15 19:33:00 -0700310 ZipArchive(const int fd, bool assume_ownership) :
Neil Fullerb1a113f2014-07-25 14:43:04 +0100311 fd(fd),
Dmitriy Ivanov40b52b22014-07-15 19:33:00 -0700312 close_file(assume_ownership),
Neil Fullerb1a113f2014-07-25 14:43:04 +0100313 directory_offset(0),
Neil Fullerb1a113f2014-07-25 14:43:04 +0100314 num_entries(0),
315 hash_table_size(0),
316 hash_table(NULL) {}
317
318 ~ZipArchive() {
Dmitriy Ivanov40b52b22014-07-15 19:33:00 -0700319 if (close_file && fd >= 0) {
Neil Fullerb1a113f2014-07-25 14:43:04 +0100320 close(fd);
321 }
322
Neil Fullerb1a113f2014-07-25 14:43:04 +0100323 free(hash_table);
324 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000325};
326
Narayan Kamath7462f022013-11-21 13:05:04 +0000327static int32_t CopyFileToFile(int fd, uint8_t* begin, const uint32_t length, uint64_t *crc_out) {
328 static const uint32_t kBufSize = 32768;
329 uint8_t buf[kBufSize];
330
331 uint32_t count = 0;
332 uint64_t crc = 0;
Narayan Kamath58aaf462013-12-10 16:47:14 +0000333 while (count < length) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000334 uint32_t remaining = length - count;
335
336 // Safe conversion because kBufSize is narrow enough for a 32 bit signed
337 // value.
338 ssize_t get_size = (remaining > kBufSize) ? kBufSize : remaining;
339 ssize_t actual = TEMP_FAILURE_RETRY(read(fd, buf, get_size));
340
341 if (actual != get_size) {
Mark Salyzyn51d562d2014-05-05 14:38:05 -0700342 ALOGW("CopyFileToFile: copy read failed (" ZD " vs " ZD ")", actual, get_size);
Narayan Kamath7462f022013-11-21 13:05:04 +0000343 return kIoError;
344 }
345
346 memcpy(begin + count, buf, get_size);
347 crc = crc32(crc, buf, get_size);
348 count += get_size;
349 }
350
351 *crc_out = crc;
352
353 return 0;
354}
355
356/*
357 * Round up to the next highest power of 2.
358 *
359 * Found on http://graphics.stanford.edu/~seander/bithacks.html.
360 */
361static uint32_t RoundUpPower2(uint32_t val) {
362 val--;
363 val |= val >> 1;
364 val |= val >> 2;
365 val |= val >> 4;
366 val |= val >> 8;
367 val |= val >> 16;
368 val++;
369
370 return val;
371}
372
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100373static uint32_t ComputeHash(const ZipEntryName& name) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000374 uint32_t hash = 0;
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100375 uint16_t len = name.name_length;
376 const uint8_t* str = name.name;
Narayan Kamath7462f022013-11-21 13:05:04 +0000377
378 while (len--) {
379 hash = hash * 31 + *str++;
380 }
381
382 return hash;
383}
384
385/*
386 * Convert a ZipEntry to a hash table index, verifying that it's in a
387 * valid range.
388 */
389static int64_t EntryToIndex(const ZipEntryName* hash_table,
390 const uint32_t hash_table_size,
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100391 const ZipEntryName& name) {
392 const uint32_t hash = ComputeHash(name);
Narayan Kamath7462f022013-11-21 13:05:04 +0000393
394 // NOTE: (hash_table_size - 1) is guaranteed to be non-negative.
395 uint32_t ent = hash & (hash_table_size - 1);
396 while (hash_table[ent].name != NULL) {
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100397 if (hash_table[ent].name_length == name.name_length &&
398 memcmp(hash_table[ent].name, name.name, name.name_length) == 0) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000399 return ent;
400 }
401
402 ent = (ent + 1) & (hash_table_size - 1);
403 }
404
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100405 ALOGV("Zip: Unable to find entry %.*s", name.name_length, name.name);
Narayan Kamath7462f022013-11-21 13:05:04 +0000406 return kEntryNotFound;
407}
408
409/*
410 * Add a new entry to the hash table.
411 */
412static int32_t AddToHash(ZipEntryName *hash_table, const uint64_t hash_table_size,
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100413 const ZipEntryName& name) {
414 const uint64_t hash = ComputeHash(name);
Narayan Kamath7462f022013-11-21 13:05:04 +0000415 uint32_t ent = hash & (hash_table_size - 1);
416
417 /*
418 * We over-allocated the table, so we're guaranteed to find an empty slot.
419 * Further, we guarantee that the hashtable size is not 0.
420 */
421 while (hash_table[ent].name != NULL) {
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100422 if (hash_table[ent].name_length == name.name_length &&
423 memcmp(hash_table[ent].name, name.name, name.name_length) == 0) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000424 // We've found a duplicate entry. We don't accept it
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100425 ALOGW("Zip: Found duplicate entry %.*s", name.name_length, name.name);
Narayan Kamath7462f022013-11-21 13:05:04 +0000426 return kDuplicateEntry;
427 }
428 ent = (ent + 1) & (hash_table_size - 1);
429 }
430
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100431 hash_table[ent].name = name.name;
432 hash_table[ent].name_length = name.name_length;
Narayan Kamath7462f022013-11-21 13:05:04 +0000433 return 0;
434}
435
Narayan Kamath7462f022013-11-21 13:05:04 +0000436static int32_t MapCentralDirectory0(int fd, const char* debug_file_name,
437 ZipArchive* archive, off64_t file_length,
Narayan Kamath926973e2014-06-09 14:18:14 +0100438 off64_t read_amount, uint8_t* scan_buffer) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000439 const off64_t search_start = file_length - read_amount;
440
441 if (lseek64(fd, search_start, SEEK_SET) != search_start) {
Narayan Kamath926973e2014-06-09 14:18:14 +0100442 ALOGW("Zip: seek %" PRId64 " failed: %s", static_cast<int64_t>(search_start),
443 strerror(errno));
Narayan Kamath7462f022013-11-21 13:05:04 +0000444 return kIoError;
445 }
Narayan Kamath926973e2014-06-09 14:18:14 +0100446 ssize_t actual = TEMP_FAILURE_RETRY(
447 read(fd, scan_buffer, static_cast<size_t>(read_amount)));
448 if (actual != static_cast<ssize_t>(read_amount)) {
449 ALOGW("Zip: read %" PRId64 " failed: %s", static_cast<int64_t>(read_amount),
450 strerror(errno));
Narayan Kamath7462f022013-11-21 13:05:04 +0000451 return kIoError;
452 }
453
454 /*
455 * Scan backward for the EOCD magic. In an archive without a trailing
456 * comment, we'll find it on the first try. (We may want to consider
457 * doing an initial minimal read; if we don't find it, retry with a
458 * second read as above.)
459 */
Narayan Kamath926973e2014-06-09 14:18:14 +0100460 int i = read_amount - sizeof(EocdRecord);
461 for (; i >= 0; i--) {
462 if (scan_buffer[i] == 0x50 &&
463 ((*reinterpret_cast<uint32_t*>(&scan_buffer[i])) == EocdRecord::kSignature)) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000464 ALOGV("+++ Found EOCD at buf+%d", i);
465 break;
466 }
467 }
468 if (i < 0) {
469 ALOGD("Zip: EOCD not found, %s is not zip", debug_file_name);
470 return kInvalidFile;
471 }
472
473 const off64_t eocd_offset = search_start + i;
Narayan Kamath926973e2014-06-09 14:18:14 +0100474 const EocdRecord* eocd = reinterpret_cast<const EocdRecord*>(scan_buffer + i);
Narayan Kamath7462f022013-11-21 13:05:04 +0000475 /*
Narayan Kamath926973e2014-06-09 14:18:14 +0100476 * Verify that there's no trailing space at the end of the central directory
477 * and its comment.
Narayan Kamath7462f022013-11-21 13:05:04 +0000478 */
Narayan Kamath926973e2014-06-09 14:18:14 +0100479 const off64_t calculated_length = eocd_offset + sizeof(EocdRecord)
480 + eocd->comment_length;
481 if (calculated_length != file_length) {
Narayan Kamath4f6b4992014-06-03 13:59:23 +0100482 ALOGW("Zip: %" PRId64 " extraneous bytes at the end of the central directory",
Narayan Kamath926973e2014-06-09 14:18:14 +0100483 static_cast<int64_t>(file_length - calculated_length));
Narayan Kamath4f6b4992014-06-03 13:59:23 +0100484 return kInvalidFile;
485 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000486
Narayan Kamath926973e2014-06-09 14:18:14 +0100487 /*
488 * Grab the CD offset and size, and the number of entries in the
489 * archive and verify that they look reasonable.
490 */
491 if (eocd->cd_start_offset + eocd->cd_size > eocd_offset) {
492 ALOGW("Zip: bad offsets (dir %" PRIu32 ", size %" PRIu32 ", eocd %" PRId64 ")",
493 eocd->cd_start_offset, eocd->cd_size, static_cast<int64_t>(eocd_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000494 return kInvalidOffset;
495 }
Narayan Kamath926973e2014-06-09 14:18:14 +0100496 if (eocd->num_records == 0) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000497 ALOGW("Zip: empty archive?");
498 return kEmptyArchive;
499 }
500
Narayan Kamath926973e2014-06-09 14:18:14 +0100501 ALOGV("+++ num_entries=%" PRIu32 "dir_size=%" PRIu32 " dir_offset=%" PRIu32,
502 eocd->num_records, eocd->cd_size, eocd->cd_start_offset);
Narayan Kamath7462f022013-11-21 13:05:04 +0000503
504 /*
505 * It all looks good. Create a mapping for the CD, and set the fields
506 * in archive.
507 */
Dmitriy Ivanov4b67f832015-03-06 10:22:34 -0800508 if (!archive->directory_map.create(debug_file_name, fd,
509 static_cast<off64_t>(eocd->cd_start_offset),
510 static_cast<size_t>(eocd->cd_size), true /* read only */) ) {
Narayan Kamatheaf98852013-12-11 14:51:51 +0000511 return kMmapFailed;
Narayan Kamath7462f022013-11-21 13:05:04 +0000512 }
513
Narayan Kamath926973e2014-06-09 14:18:14 +0100514 archive->num_entries = eocd->num_records;
515 archive->directory_offset = eocd->cd_start_offset;
Narayan Kamath7462f022013-11-21 13:05:04 +0000516
517 return 0;
518}
519
520/*
521 * Find the zip Central Directory and memory-map it.
522 *
523 * On success, returns 0 after populating fields from the EOCD area:
524 * directory_offset
525 * directory_map
526 * num_entries
527 */
528static int32_t MapCentralDirectory(int fd, const char* debug_file_name,
529 ZipArchive* archive) {
530
531 // Test file length. We use lseek64 to make sure the file
532 // is small enough to be a zip file (Its size must be less than
533 // 0xffffffff bytes).
534 off64_t file_length = lseek64(fd, 0, SEEK_END);
535 if (file_length == -1) {
536 ALOGV("Zip: lseek on fd %d failed", fd);
537 return kInvalidFile;
538 }
539
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800540 if (file_length > static_cast<off64_t>(0xffffffff)) {
Narayan Kamath926973e2014-06-09 14:18:14 +0100541 ALOGV("Zip: zip file too long %" PRId64, static_cast<int64_t>(file_length));
Narayan Kamath7462f022013-11-21 13:05:04 +0000542 return kInvalidFile;
543 }
544
Narayan Kamath926973e2014-06-09 14:18:14 +0100545 if (file_length < static_cast<off64_t>(sizeof(EocdRecord))) {
546 ALOGV("Zip: length %" PRId64 " is too small to be zip", static_cast<int64_t>(file_length));
Narayan Kamath7462f022013-11-21 13:05:04 +0000547 return kInvalidFile;
548 }
549
550 /*
551 * Perform the traditional EOCD snipe hunt.
552 *
553 * We're searching for the End of Central Directory magic number,
554 * which appears at the start of the EOCD block. It's followed by
555 * 18 bytes of EOCD stuff and up to 64KB of archive comment. We
556 * need to read the last part of the file into a buffer, dig through
557 * it to find the magic number, parse some values out, and use those
558 * to determine the extent of the CD.
559 *
560 * We start by pulling in the last part of the file.
561 */
Narayan Kamath926973e2014-06-09 14:18:14 +0100562 off64_t read_amount = kMaxEOCDSearch;
563 if (file_length < read_amount) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000564 read_amount = file_length;
565 }
566
Narayan Kamath926973e2014-06-09 14:18:14 +0100567 uint8_t* scan_buffer = reinterpret_cast<uint8_t*>(malloc(read_amount));
Narayan Kamath7462f022013-11-21 13:05:04 +0000568 int32_t result = MapCentralDirectory0(fd, debug_file_name, archive,
569 file_length, read_amount, scan_buffer);
570
571 free(scan_buffer);
572 return result;
573}
574
575/*
576 * Parses the Zip archive's Central Directory. Allocates and populates the
577 * hash table.
578 *
579 * Returns 0 on success.
580 */
581static int32_t ParseZipArchive(ZipArchive* archive) {
582 int32_t result = -1;
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800583 const uint8_t* const cd_ptr =
584 reinterpret_cast<const uint8_t*>(archive->directory_map.getDataPtr());
Dmitriy Ivanov4b67f832015-03-06 10:22:34 -0800585 const size_t cd_length = archive->directory_map.getDataLength();
Narayan Kamath926973e2014-06-09 14:18:14 +0100586 const uint16_t num_entries = archive->num_entries;
Narayan Kamath7462f022013-11-21 13:05:04 +0000587
588 /*
589 * Create hash table. We have a minimum 75% load factor, possibly as
590 * low as 50% after we round off to a power of 2. There must be at
591 * least one unused entry to avoid an infinite loop during creation.
592 */
593 archive->hash_table_size = RoundUpPower2(1 + (num_entries * 4) / 3);
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800594 archive->hash_table = reinterpret_cast<ZipEntryName*>(calloc(archive->hash_table_size,
595 sizeof(ZipEntryName)));
Narayan Kamath7462f022013-11-21 13:05:04 +0000596
597 /*
598 * Walk through the central directory, adding entries to the hash
599 * table and verifying values.
600 */
Narayan Kamath926973e2014-06-09 14:18:14 +0100601 const uint8_t* const cd_end = cd_ptr + cd_length;
Narayan Kamath7462f022013-11-21 13:05:04 +0000602 const uint8_t* ptr = cd_ptr;
603 for (uint16_t i = 0; i < num_entries; i++) {
Narayan Kamath926973e2014-06-09 14:18:14 +0100604 const CentralDirectoryRecord* cdr =
605 reinterpret_cast<const CentralDirectoryRecord*>(ptr);
606 if (cdr->record_signature != CentralDirectoryRecord::kSignature) {
Mark Salyzyn088bf902014-05-08 16:02:20 -0700607 ALOGW("Zip: missed a central dir sig (at %" PRIu16 ")", i);
Narayan Kamath7462f022013-11-21 13:05:04 +0000608 goto bail;
609 }
610
Narayan Kamath926973e2014-06-09 14:18:14 +0100611 if (ptr + sizeof(CentralDirectoryRecord) > cd_end) {
Mark Salyzyn088bf902014-05-08 16:02:20 -0700612 ALOGW("Zip: ran off the end (at %" PRIu16 ")", i);
Narayan Kamath7462f022013-11-21 13:05:04 +0000613 goto bail;
614 }
615
Narayan Kamath926973e2014-06-09 14:18:14 +0100616 const off64_t local_header_offset = cdr->local_file_header_offset;
Narayan Kamath7462f022013-11-21 13:05:04 +0000617 if (local_header_offset >= archive->directory_offset) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800618 ALOGW("Zip: bad LFH offset %" PRId64 " at entry %" PRIu16,
619 static_cast<int64_t>(local_header_offset), i);
Narayan Kamath7462f022013-11-21 13:05:04 +0000620 goto bail;
621 }
622
Narayan Kamath926973e2014-06-09 14:18:14 +0100623 const uint16_t file_name_length = cdr->file_name_length;
624 const uint16_t extra_length = cdr->extra_field_length;
625 const uint16_t comment_length = cdr->comment_length;
Piotr Jastrzebski78271ba2014-08-15 12:53:00 +0100626 const uint8_t* file_name = ptr + sizeof(CentralDirectoryRecord);
627
Narayan Kamath044bc8e2014-12-03 18:22:53 +0000628 /* check that file name is valid UTF-8 and doesn't contain NUL (U+0000) characters */
629 if (!IsValidEntryName(file_name, file_name_length)) {
Piotr Jastrzebski78271ba2014-08-15 12:53:00 +0100630 goto bail;
631 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000632
633 /* add the CDE filename to the hash table */
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100634 ZipEntryName entry_name;
635 entry_name.name = file_name;
636 entry_name.name_length = file_name_length;
Narayan Kamath7462f022013-11-21 13:05:04 +0000637 const int add_result = AddToHash(archive->hash_table,
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100638 archive->hash_table_size, entry_name);
Narayan Kamath7462f022013-11-21 13:05:04 +0000639 if (add_result) {
640 ALOGW("Zip: Error adding entry to hash table %d", add_result);
641 result = add_result;
642 goto bail;
643 }
644
Narayan Kamath926973e2014-06-09 14:18:14 +0100645 ptr += sizeof(CentralDirectoryRecord) + file_name_length + extra_length + comment_length;
646 if ((ptr - cd_ptr) > static_cast<int64_t>(cd_length)) {
Mark Salyzyn088bf902014-05-08 16:02:20 -0700647 ALOGW("Zip: bad CD advance (%tu vs %zu) at entry %" PRIu16,
648 ptr - cd_ptr, cd_length, i);
Narayan Kamath7462f022013-11-21 13:05:04 +0000649 goto bail;
650 }
651 }
Mark Salyzyn088bf902014-05-08 16:02:20 -0700652 ALOGV("+++ zip good scan %" PRIu16 " entries", num_entries);
Narayan Kamath7462f022013-11-21 13:05:04 +0000653
654 result = 0;
655
656bail:
657 return result;
658}
659
660static int32_t OpenArchiveInternal(ZipArchive* archive,
661 const char* debug_file_name) {
662 int32_t result = -1;
663 if ((result = MapCentralDirectory(archive->fd, debug_file_name, archive))) {
664 return result;
665 }
666
667 if ((result = ParseZipArchive(archive))) {
668 return result;
669 }
670
671 return 0;
672}
673
674int32_t OpenArchiveFd(int fd, const char* debug_file_name,
Dmitriy Ivanov40b52b22014-07-15 19:33:00 -0700675 ZipArchiveHandle* handle, bool assume_ownership) {
676 ZipArchive* archive = new ZipArchive(fd, assume_ownership);
Narayan Kamath7462f022013-11-21 13:05:04 +0000677 *handle = archive;
Narayan Kamath7462f022013-11-21 13:05:04 +0000678 return OpenArchiveInternal(archive, debug_file_name);
679}
680
681int32_t OpenArchive(const char* fileName, ZipArchiveHandle* handle) {
Neil Fullerb1a113f2014-07-25 14:43:04 +0100682 const int fd = open(fileName, O_RDONLY | O_BINARY, 0);
Dmitriy Ivanov40b52b22014-07-15 19:33:00 -0700683 ZipArchive* archive = new ZipArchive(fd, true);
Narayan Kamath7462f022013-11-21 13:05:04 +0000684 *handle = archive;
685
Narayan Kamath7462f022013-11-21 13:05:04 +0000686 if (fd < 0) {
687 ALOGW("Unable to open '%s': %s", fileName, strerror(errno));
688 return kIoError;
Narayan Kamath7462f022013-11-21 13:05:04 +0000689 }
Dmitriy Ivanov40b52b22014-07-15 19:33:00 -0700690
Narayan Kamath7462f022013-11-21 13:05:04 +0000691 return OpenArchiveInternal(archive, fileName);
692}
693
694/*
695 * Close a ZipArchive, closing the file and freeing the contents.
696 */
697void CloseArchive(ZipArchiveHandle handle) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800698 ZipArchive* archive = reinterpret_cast<ZipArchive*>(handle);
Narayan Kamath7462f022013-11-21 13:05:04 +0000699 ALOGV("Closing archive %p", archive);
Neil Fullerb1a113f2014-07-25 14:43:04 +0100700 delete archive;
Narayan Kamath7462f022013-11-21 13:05:04 +0000701}
702
703static int32_t UpdateEntryFromDataDescriptor(int fd,
704 ZipEntry *entry) {
Narayan Kamath926973e2014-06-09 14:18:14 +0100705 uint8_t ddBuf[sizeof(DataDescriptor) + sizeof(DataDescriptor::kOptSignature)];
Narayan Kamath7462f022013-11-21 13:05:04 +0000706 ssize_t actual = TEMP_FAILURE_RETRY(read(fd, ddBuf, sizeof(ddBuf)));
707 if (actual != sizeof(ddBuf)) {
708 return kIoError;
709 }
710
Narayan Kamath926973e2014-06-09 14:18:14 +0100711 const uint32_t ddSignature = *(reinterpret_cast<const uint32_t*>(ddBuf));
712 const uint16_t offset = (ddSignature == DataDescriptor::kOptSignature) ? 4 : 0;
713 const DataDescriptor* descriptor = reinterpret_cast<const DataDescriptor*>(ddBuf + offset);
Narayan Kamath7462f022013-11-21 13:05:04 +0000714
Narayan Kamath926973e2014-06-09 14:18:14 +0100715 entry->crc32 = descriptor->crc32;
716 entry->compressed_length = descriptor->compressed_size;
717 entry->uncompressed_length = descriptor->uncompressed_size;
Narayan Kamath7462f022013-11-21 13:05:04 +0000718
719 return 0;
720}
721
722// Attempts to read |len| bytes into |buf| at offset |off|.
723//
724// This method uses pread64 on platforms that support it and
725// lseek64 + read on platforms that don't. This implies that
726// callers should not rely on the |fd| offset being incremented
727// as a side effect of this call.
728static inline ssize_t ReadAtOffset(int fd, uint8_t* buf, size_t len,
729 off64_t off) {
Yabin Cui70160f42014-11-19 20:47:18 -0800730#if !defined(_WIN32)
Narayan Kamath7462f022013-11-21 13:05:04 +0000731 return TEMP_FAILURE_RETRY(pread64(fd, buf, len, off));
732#else
733 // The only supported platform that doesn't support pread at the moment
734 // is Windows. Only recent versions of windows support unix like forks,
735 // and even there the semantics are quite different.
736 if (lseek64(fd, off, SEEK_SET) != off) {
Mark Salyzyn99ef9912014-03-14 14:26:22 -0700737 ALOGW("Zip: failed seek to offset %" PRId64, off);
Narayan Kamath7462f022013-11-21 13:05:04 +0000738 return kIoError;
739 }
740
741 return TEMP_FAILURE_RETRY(read(fd, buf, len));
Yabin Cui70160f42014-11-19 20:47:18 -0800742#endif
Narayan Kamath7462f022013-11-21 13:05:04 +0000743}
744
745static int32_t FindEntry(const ZipArchive* archive, const int ent,
746 ZipEntry* data) {
747 const uint16_t nameLen = archive->hash_table[ent].name_length;
Narayan Kamath7462f022013-11-21 13:05:04 +0000748
749 // Recover the start of the central directory entry from the filename
750 // pointer. The filename is the first entry past the fixed-size data,
751 // so we can just subtract back from that.
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100752 const uint8_t* ptr = archive->hash_table[ent].name;
Narayan Kamath926973e2014-06-09 14:18:14 +0100753 ptr -= sizeof(CentralDirectoryRecord);
Narayan Kamath7462f022013-11-21 13:05:04 +0000754
755 // This is the base of our mmapped region, we have to sanity check that
756 // the name that's in the hash table is a pointer to a location within
757 // this mapped region.
Narayan Kamath926973e2014-06-09 14:18:14 +0100758 const uint8_t* base_ptr = reinterpret_cast<const uint8_t*>(
Dmitriy Ivanov4b67f832015-03-06 10:22:34 -0800759 archive->directory_map.getDataPtr());
760 if (ptr < base_ptr || ptr > base_ptr + archive->directory_map.getDataLength()) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000761 ALOGW("Zip: Invalid entry pointer");
762 return kInvalidOffset;
763 }
764
Narayan Kamath926973e2014-06-09 14:18:14 +0100765 const CentralDirectoryRecord *cdr =
766 reinterpret_cast<const CentralDirectoryRecord*>(ptr);
767
Narayan Kamath7462f022013-11-21 13:05:04 +0000768 // The offset of the start of the central directory in the zipfile.
769 // We keep this lying around so that we can sanity check all our lengths
770 // and our per-file structures.
771 const off64_t cd_offset = archive->directory_offset;
772
773 // Fill out the compression method, modification time, crc32
774 // and other interesting attributes from the central directory. These
775 // will later be compared against values from the local file header.
Narayan Kamath926973e2014-06-09 14:18:14 +0100776 data->method = cdr->compression_method;
777 data->mod_time = cdr->last_mod_time;
778 data->crc32 = cdr->crc32;
779 data->compressed_length = cdr->compressed_size;
780 data->uncompressed_length = cdr->uncompressed_size;
Narayan Kamath7462f022013-11-21 13:05:04 +0000781
782 // Figure out the local header offset from the central directory. The
783 // actual file data will begin after the local header and the name /
784 // extra comments.
Narayan Kamath926973e2014-06-09 14:18:14 +0100785 const off64_t local_header_offset = cdr->local_file_header_offset;
786 if (local_header_offset + static_cast<off64_t>(sizeof(LocalFileHeader)) >= cd_offset) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000787 ALOGW("Zip: bad local hdr offset in zip");
788 return kInvalidOffset;
789 }
790
Narayan Kamath926973e2014-06-09 14:18:14 +0100791 uint8_t lfh_buf[sizeof(LocalFileHeader)];
Narayan Kamath7462f022013-11-21 13:05:04 +0000792 ssize_t actual = ReadAtOffset(archive->fd, lfh_buf, sizeof(lfh_buf),
793 local_header_offset);
794 if (actual != sizeof(lfh_buf)) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800795 ALOGW("Zip: failed reading lfh name from offset %" PRId64,
796 static_cast<int64_t>(local_header_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000797 return kIoError;
798 }
799
Narayan Kamath926973e2014-06-09 14:18:14 +0100800 const LocalFileHeader *lfh = reinterpret_cast<const LocalFileHeader*>(lfh_buf);
801
802 if (lfh->lfh_signature != LocalFileHeader::kSignature) {
Mark Salyzyn99ef9912014-03-14 14:26:22 -0700803 ALOGW("Zip: didn't find signature at start of lfh, offset=%" PRId64,
Narayan Kamath926973e2014-06-09 14:18:14 +0100804 static_cast<int64_t>(local_header_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000805 return kInvalidOffset;
806 }
807
808 // Paranoia: Match the values specified in the local file header
809 // to those specified in the central directory.
Narayan Kamath926973e2014-06-09 14:18:14 +0100810 if ((lfh->gpb_flags & kGPBDDFlagMask) == 0) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000811 data->has_data_descriptor = 0;
Narayan Kamath926973e2014-06-09 14:18:14 +0100812 if (data->compressed_length != lfh->compressed_size
813 || data->uncompressed_length != lfh->uncompressed_size
814 || data->crc32 != lfh->crc32) {
Mark Salyzyn088bf902014-05-08 16:02:20 -0700815 ALOGW("Zip: size/crc32 mismatch. expected {%" PRIu32 ", %" PRIu32
816 ", %" PRIx32 "}, was {%" PRIu32 ", %" PRIu32 ", %" PRIx32 "}",
Narayan Kamath7462f022013-11-21 13:05:04 +0000817 data->compressed_length, data->uncompressed_length, data->crc32,
Narayan Kamath926973e2014-06-09 14:18:14 +0100818 lfh->compressed_size, lfh->uncompressed_size, lfh->crc32);
Narayan Kamath7462f022013-11-21 13:05:04 +0000819 return kInconsistentInformation;
820 }
821 } else {
822 data->has_data_descriptor = 1;
823 }
824
825 // Check that the local file header name matches the declared
826 // name in the central directory.
Narayan Kamath926973e2014-06-09 14:18:14 +0100827 if (lfh->file_name_length == nameLen) {
828 const off64_t name_offset = local_header_offset + sizeof(LocalFileHeader);
829 if (name_offset + lfh->file_name_length >= cd_offset) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000830 ALOGW("Zip: Invalid declared length");
831 return kInvalidOffset;
832 }
833
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800834 uint8_t* name_buf = reinterpret_cast<uint8_t*>(malloc(nameLen));
Narayan Kamath7462f022013-11-21 13:05:04 +0000835 ssize_t actual = ReadAtOffset(archive->fd, name_buf, nameLen,
836 name_offset);
837
838 if (actual != nameLen) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800839 ALOGW("Zip: failed reading lfh name from offset %" PRId64, static_cast<int64_t>(name_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000840 free(name_buf);
841 return kIoError;
842 }
843
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100844 if (memcmp(archive->hash_table[ent].name, name_buf, nameLen)) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000845 free(name_buf);
846 return kInconsistentInformation;
847 }
848
849 free(name_buf);
850 } else {
851 ALOGW("Zip: lfh name did not match central directory.");
852 return kInconsistentInformation;
853 }
854
Narayan Kamath926973e2014-06-09 14:18:14 +0100855 const off64_t data_offset = local_header_offset + sizeof(LocalFileHeader)
856 + lfh->file_name_length + lfh->extra_field_length;
Narayan Kamath48953a12014-01-24 12:32:39 +0000857 if (data_offset > cd_offset) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800858 ALOGW("Zip: bad data offset %" PRId64 " in zip", static_cast<int64_t>(data_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000859 return kInvalidOffset;
860 }
861
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800862 if (static_cast<off64_t>(data_offset + data->compressed_length) > cd_offset) {
Mark Salyzyn088bf902014-05-08 16:02:20 -0700863 ALOGW("Zip: bad compressed length in zip (%" PRId64 " + %" PRIu32 " > %" PRId64 ")",
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800864 static_cast<int64_t>(data_offset), data->compressed_length, static_cast<int64_t>(cd_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000865 return kInvalidOffset;
866 }
867
868 if (data->method == kCompressStored &&
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800869 static_cast<off64_t>(data_offset + data->uncompressed_length) > cd_offset) {
Mark Salyzyn088bf902014-05-08 16:02:20 -0700870 ALOGW("Zip: bad uncompressed length in zip (%" PRId64 " + %" PRIu32 " > %" PRId64 ")",
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800871 static_cast<int64_t>(data_offset), data->uncompressed_length,
872 static_cast<int64_t>(cd_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000873 return kInvalidOffset;
874 }
875
876 data->offset = data_offset;
877 return 0;
878}
879
880struct IterationHandle {
881 uint32_t position;
Piotr Jastrzebski10aa9a02014-08-19 09:01:20 +0100882 // We're not using vector here because this code is used in the Windows SDK
883 // where the STL is not available.
Piotr Jastrzebski8e085362014-08-18 11:37:45 +0100884 const uint8_t* prefix;
885 uint16_t prefix_len;
Narayan Kamath7462f022013-11-21 13:05:04 +0000886 ZipArchive* archive;
Piotr Jastrzebski8e085362014-08-18 11:37:45 +0100887
888 IterationHandle() : prefix(NULL), prefix_len(0) {}
889
890 IterationHandle(const ZipEntryName& prefix_name)
891 : prefix_len(prefix_name.name_length) {
892 uint8_t* prefix_copy = new uint8_t[prefix_len];
Piotr Jastrzebski10aa9a02014-08-19 09:01:20 +0100893 memcpy(prefix_copy, prefix_name.name, prefix_len);
Piotr Jastrzebski8e085362014-08-18 11:37:45 +0100894 prefix = prefix_copy;
895 }
896
897 ~IterationHandle() {
Piotr Jastrzebski10aa9a02014-08-19 09:01:20 +0100898 delete[] prefix;
Piotr Jastrzebski8e085362014-08-18 11:37:45 +0100899 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000900};
901
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100902int32_t StartIteration(ZipArchiveHandle handle, void** cookie_ptr,
903 const ZipEntryName* optional_prefix) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800904 ZipArchive* archive = reinterpret_cast<ZipArchive*>(handle);
Narayan Kamath7462f022013-11-21 13:05:04 +0000905
906 if (archive == NULL || archive->hash_table == NULL) {
907 ALOGW("Zip: Invalid ZipArchiveHandle");
908 return kInvalidHandle;
909 }
910
Piotr Jastrzebski8e085362014-08-18 11:37:45 +0100911 IterationHandle* cookie =
912 optional_prefix != NULL ? new IterationHandle(*optional_prefix) : new IterationHandle();
Narayan Kamath7462f022013-11-21 13:05:04 +0000913 cookie->position = 0;
Narayan Kamath7462f022013-11-21 13:05:04 +0000914 cookie->archive = archive;
Narayan Kamath7462f022013-11-21 13:05:04 +0000915
916 *cookie_ptr = cookie ;
917 return 0;
918}
919
Piotr Jastrzebski79c8b342014-08-08 14:02:17 +0100920void EndIteration(void* cookie) {
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100921 delete reinterpret_cast<IterationHandle*>(cookie);
Piotr Jastrzebski79c8b342014-08-08 14:02:17 +0100922}
923
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100924int32_t FindEntry(const ZipArchiveHandle handle, const ZipEntryName& entryName,
Narayan Kamath7462f022013-11-21 13:05:04 +0000925 ZipEntry* data) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800926 const ZipArchive* archive = reinterpret_cast<ZipArchive*>(handle);
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100927 if (entryName.name_length == 0) {
928 ALOGW("Zip: Invalid filename %.*s", entryName.name_length, entryName.name);
Narayan Kamath7462f022013-11-21 13:05:04 +0000929 return kInvalidEntryName;
930 }
931
932 const int64_t ent = EntryToIndex(archive->hash_table,
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100933 archive->hash_table_size, entryName);
Narayan Kamath7462f022013-11-21 13:05:04 +0000934
935 if (ent < 0) {
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100936 ALOGV("Zip: Could not find entry %.*s", entryName.name_length, entryName.name);
Narayan Kamath7462f022013-11-21 13:05:04 +0000937 return ent;
938 }
939
940 return FindEntry(archive, ent, data);
941}
942
943int32_t Next(void* cookie, ZipEntry* data, ZipEntryName* name) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800944 IterationHandle* handle = reinterpret_cast<IterationHandle*>(cookie);
Narayan Kamath7462f022013-11-21 13:05:04 +0000945 if (handle == NULL) {
946 return kInvalidHandle;
947 }
948
949 ZipArchive* archive = handle->archive;
950 if (archive == NULL || archive->hash_table == NULL) {
951 ALOGW("Zip: Invalid ZipArchiveHandle");
952 return kInvalidHandle;
953 }
954
955 const uint32_t currentOffset = handle->position;
956 const uint32_t hash_table_length = archive->hash_table_size;
957 const ZipEntryName *hash_table = archive->hash_table;
958
959 for (uint32_t i = currentOffset; i < hash_table_length; ++i) {
960 if (hash_table[i].name != NULL &&
Piotr Jastrzebski8e085362014-08-18 11:37:45 +0100961 (handle->prefix_len == 0 ||
962 (memcmp(handle->prefix, hash_table[i].name, handle->prefix_len) == 0))) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000963 handle->position = (i + 1);
964 const int error = FindEntry(archive, i, data);
965 if (!error) {
966 name->name = hash_table[i].name;
967 name->name_length = hash_table[i].name_length;
968 }
969
970 return error;
971 }
972 }
973
974 handle->position = 0;
975 return kIterationEnd;
976}
977
978static int32_t InflateToFile(int fd, const ZipEntry* entry,
979 uint8_t* begin, uint32_t length,
980 uint64_t* crc_out) {
981 int32_t result = -1;
982 const uint32_t kBufSize = 32768;
983 uint8_t read_buf[kBufSize];
984 uint8_t write_buf[kBufSize];
985 z_stream zstream;
986 int zerr;
987
988 /*
989 * Initialize the zlib stream struct.
990 */
991 memset(&zstream, 0, sizeof(zstream));
992 zstream.zalloc = Z_NULL;
993 zstream.zfree = Z_NULL;
994 zstream.opaque = Z_NULL;
995 zstream.next_in = NULL;
996 zstream.avail_in = 0;
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800997 zstream.next_out = reinterpret_cast<Bytef*>(write_buf);
Narayan Kamath7462f022013-11-21 13:05:04 +0000998 zstream.avail_out = kBufSize;
999 zstream.data_type = Z_UNKNOWN;
1000
1001 /*
1002 * Use the undocumented "negative window bits" feature to tell zlib
1003 * that there's no zlib header waiting for it.
1004 */
1005 zerr = inflateInit2(&zstream, -MAX_WBITS);
1006 if (zerr != Z_OK) {
1007 if (zerr == Z_VERSION_ERROR) {
1008 ALOGE("Installed zlib is not compatible with linked version (%s)",
1009 ZLIB_VERSION);
1010 } else {
1011 ALOGW("Call to inflateInit2 failed (zerr=%d)", zerr);
1012 }
1013
1014 return kZlibError;
1015 }
1016
1017 const uint32_t uncompressed_length = entry->uncompressed_length;
1018
1019 uint32_t compressed_length = entry->compressed_length;
1020 uint32_t write_count = 0;
1021 do {
1022 /* read as much as we can */
1023 if (zstream.avail_in == 0) {
Mark Salyzyn51d562d2014-05-05 14:38:05 -07001024 const ZD_TYPE getSize = (compressed_length > kBufSize) ? kBufSize : compressed_length;
1025 const ZD_TYPE actual = TEMP_FAILURE_RETRY(read(fd, read_buf, getSize));
Narayan Kamath7462f022013-11-21 13:05:04 +00001026 if (actual != getSize) {
Mark Salyzyn51d562d2014-05-05 14:38:05 -07001027 ALOGW("Zip: inflate read failed (" ZD " vs " ZD ")", actual, getSize);
Narayan Kamath7462f022013-11-21 13:05:04 +00001028 result = kIoError;
1029 goto z_bail;
1030 }
1031
1032 compressed_length -= getSize;
1033
1034 zstream.next_in = read_buf;
1035 zstream.avail_in = getSize;
1036 }
1037
1038 /* uncompress the data */
1039 zerr = inflate(&zstream, Z_NO_FLUSH);
1040 if (zerr != Z_OK && zerr != Z_STREAM_END) {
1041 ALOGW("Zip: inflate zerr=%d (nIn=%p aIn=%u nOut=%p aOut=%u)",
1042 zerr, zstream.next_in, zstream.avail_in,
1043 zstream.next_out, zstream.avail_out);
1044 result = kZlibError;
1045 goto z_bail;
1046 }
1047
1048 /* write when we're full or when we're done */
1049 if (zstream.avail_out == 0 ||
1050 (zerr == Z_STREAM_END && zstream.avail_out != kBufSize)) {
1051 const size_t write_size = zstream.next_out - write_buf;
1052 // The file might have declared a bogus length.
1053 if (write_size + write_count > length) {
1054 goto z_bail;
1055 }
1056 memcpy(begin + write_count, write_buf, write_size);
1057 write_count += write_size;
1058
1059 zstream.next_out = write_buf;
1060 zstream.avail_out = kBufSize;
1061 }
1062 } while (zerr == Z_OK);
1063
1064 assert(zerr == Z_STREAM_END); /* other errors should've been caught */
1065
1066 // stream.adler holds the crc32 value for such streams.
1067 *crc_out = zstream.adler;
1068
1069 if (zstream.total_out != uncompressed_length || compressed_length != 0) {
Mark Salyzyn088bf902014-05-08 16:02:20 -07001070 ALOGW("Zip: size mismatch on inflated file (%lu vs %" PRIu32 ")",
Narayan Kamath7462f022013-11-21 13:05:04 +00001071 zstream.total_out, uncompressed_length);
1072 result = kInconsistentInformation;
1073 goto z_bail;
1074 }
1075
1076 result = 0;
1077
1078z_bail:
1079 inflateEnd(&zstream); /* free up any allocated structures */
1080
1081 return result;
1082}
1083
1084int32_t ExtractToMemory(ZipArchiveHandle handle,
1085 ZipEntry* entry, uint8_t* begin, uint32_t size) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -08001086 ZipArchive* archive = reinterpret_cast<ZipArchive*>(handle);
Narayan Kamath7462f022013-11-21 13:05:04 +00001087 const uint16_t method = entry->method;
1088 off64_t data_offset = entry->offset;
1089
1090 if (lseek64(archive->fd, data_offset, SEEK_SET) != data_offset) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -08001091 ALOGW("Zip: lseek to data at %" PRId64 " failed", static_cast<int64_t>(data_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +00001092 return kIoError;
1093 }
1094
1095 // this should default to kUnknownCompressionMethod.
1096 int32_t return_value = -1;
1097 uint64_t crc = 0;
1098 if (method == kCompressStored) {
1099 return_value = CopyFileToFile(archive->fd, begin, size, &crc);
1100 } else if (method == kCompressDeflated) {
1101 return_value = InflateToFile(archive->fd, entry, begin, size, &crc);
1102 }
1103
1104 if (!return_value && entry->has_data_descriptor) {
1105 return_value = UpdateEntryFromDataDescriptor(archive->fd, entry);
1106 if (return_value) {
1107 return return_value;
1108 }
1109 }
1110
1111 // TODO: Fix this check by passing the right flags to inflate2 so that
1112 // it calculates the CRC for us.
1113 if (entry->crc32 != crc && false) {
Mark Salyzyn088bf902014-05-08 16:02:20 -07001114 ALOGW("Zip: crc mismatch: expected %" PRIu32 ", was %" PRIu64, entry->crc32, crc);
Narayan Kamath7462f022013-11-21 13:05:04 +00001115 return kInconsistentInformation;
1116 }
1117
1118 return return_value;
1119}
1120
1121int32_t ExtractEntryToFile(ZipArchiveHandle handle,
1122 ZipEntry* entry, int fd) {
1123 const int32_t declared_length = entry->uncompressed_length;
1124
Narayan Kamath00a258c2013-12-13 16:06:19 +00001125 const off64_t current_offset = lseek64(fd, 0, SEEK_CUR);
1126 if (current_offset == -1) {
1127 ALOGW("Zip: unable to seek to current location on fd %d: %s", fd,
1128 strerror(errno));
Narayan Kamath7462f022013-11-21 13:05:04 +00001129 return kIoError;
1130 }
1131
Narayan Kamath00a258c2013-12-13 16:06:19 +00001132 int result = TEMP_FAILURE_RETRY(ftruncate(fd, declared_length + current_offset));
1133 if (result == -1) {
Mark Salyzyn99ef9912014-03-14 14:26:22 -07001134 ALOGW("Zip: unable to truncate file to %" PRId64 ": %s",
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -08001135 static_cast<int64_t>(declared_length + current_offset), strerror(errno));
Narayan Kamath00a258c2013-12-13 16:06:19 +00001136 return kIoError;
1137 }
1138
Narayan Kamath48953a12014-01-24 12:32:39 +00001139 // Don't attempt to map a region of length 0. We still need the
1140 // ftruncate() though, since the API guarantees that we will truncate
1141 // the file to the end of the uncompressed output.
1142 if (declared_length == 0) {
1143 return 0;
1144 }
1145
Dmitriy Ivanov4b67f832015-03-06 10:22:34 -08001146 android::FileMap map;
1147 if (!map.create(kTempMappingFileName, fd, current_offset, declared_length, false)) {
Narayan Kamatheaf98852013-12-11 14:51:51 +00001148 return kMmapFailed;
Narayan Kamath7462f022013-11-21 13:05:04 +00001149 }
1150
Narayan Kamatheaf98852013-12-11 14:51:51 +00001151 const int32_t error = ExtractToMemory(handle, entry,
Dmitriy Ivanov4b67f832015-03-06 10:22:34 -08001152 reinterpret_cast<uint8_t*>(map.getDataPtr()),
1153 map.getDataLength());
Narayan Kamath7462f022013-11-21 13:05:04 +00001154 return error;
1155}
1156
1157const char* ErrorCodeString(int32_t error_code) {
1158 if (error_code > kErrorMessageLowerBound && error_code < kErrorMessageUpperBound) {
1159 return kErrorMessages[error_code * -1];
1160 }
1161
1162 return kErrorMessages[0];
1163}
1164
1165int GetFileDescriptor(const ZipArchiveHandle handle) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -08001166 return reinterpret_cast<ZipArchive*>(handle)->fd;
Narayan Kamath7462f022013-11-21 13:05:04 +00001167}
1168