blob: 34131f1be367a87bd16e408d8171b4290b240120 [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>
Narayan Kamath7462f022013-11-21 13:05:04 +000026#include <stdlib.h>
27#include <string.h>
Narayan Kamath7462f022013-11-21 13:05:04 +000028#include <unistd.h>
29
Dan Albert1ae07642015-04-09 14:11:18 -070030#include <memory>
31#include <vector>
32
33#include "base/macros.h" // TEMP_FAILURE_RETRY may or may not be in unistd
34#include "base/memory.h"
35#include "log/log.h"
36#include "utils/Compat.h"
37#include "utils/FileMap.h"
38#include "zlib.h"
Narayan Kamath7462f022013-11-21 13:05:04 +000039
Narayan Kamath044bc8e2014-12-03 18:22:53 +000040#include "entry_name_utils-inl.h"
Mark Salyzyn99ef9912014-03-14 14:26:22 -070041#include "ziparchive/zip_archive.h"
42
Dan Albert1ae07642015-04-09 14:11:18 -070043using android::base::get_unaligned;
Narayan Kamath044bc8e2014-12-03 18:22:53 +000044
Narayan Kamath926973e2014-06-09 14:18:14 +010045// This is for windows. If we don't open a file in binary mode, weird
Narayan Kamath7462f022013-11-21 13:05:04 +000046// things will happen.
47#ifndef O_BINARY
48#define O_BINARY 0
49#endif
50
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:
Narayan Kamathf899bd52015-04-17 11:53:14 +010088 EocdRecord() = default;
89 DISALLOW_COPY_AND_ASSIGN(EocdRecord);
Narayan Kamath926973e2014-06-09 14:18:14 +010090} __attribute__((packed));
Narayan Kamath7462f022013-11-21 13:05:04 +000091
Narayan Kamath926973e2014-06-09 14:18:14 +010092// A structure representing the fixed length fields for a single
93// record in the central directory of the archive. In addition to
94// the fixed length fields listed here, each central directory
95// record contains a variable length "file_name" and "extra_field"
96// whose lengths are given by |file_name_length| and |extra_field_length|
97// respectively.
98struct CentralDirectoryRecord {
99 static const uint32_t kSignature = 0x02014b50;
Narayan Kamath7462f022013-11-21 13:05:04 +0000100
Narayan Kamath926973e2014-06-09 14:18:14 +0100101 // The start of record signature. Must be |kSignature|.
102 uint32_t record_signature;
103 // Tool version. Ignored by this implementation.
104 uint16_t version_made_by;
105 // Tool version. Ignored by this implementation.
106 uint16_t version_needed;
107 // The "general purpose bit flags" for this entry. The only
108 // flag value that we currently check for is the "data descriptor"
109 // flag.
110 uint16_t gpb_flags;
111 // The compression method for this entry, one of |kCompressStored|
112 // and |kCompressDeflated|.
113 uint16_t compression_method;
114 // The file modification time and date for this entry.
115 uint16_t last_mod_time;
116 uint16_t last_mod_date;
117 // The CRC-32 checksum for this entry.
118 uint32_t crc32;
119 // The compressed size (in bytes) of this entry.
120 uint32_t compressed_size;
121 // The uncompressed size (in bytes) of this entry.
122 uint32_t uncompressed_size;
123 // The length of the entry file name in bytes. The file name
124 // will appear immediately after this record.
125 uint16_t file_name_length;
126 // The length of the extra field info (in bytes). This data
127 // will appear immediately after the entry file name.
128 uint16_t extra_field_length;
129 // The length of the entry comment (in bytes). This data will
130 // appear immediately after the extra field.
131 uint16_t comment_length;
132 // The start disk for this entry. Ignored by this implementation).
133 uint16_t file_start_disk;
134 // File attributes. Ignored by this implementation.
135 uint16_t internal_file_attributes;
136 // File attributes. Ignored by this implementation.
137 uint32_t external_file_attributes;
138 // The offset to the local file header for this entry, from the
139 // beginning of this archive.
140 uint32_t local_file_header_offset;
141 private:
Narayan Kamathf899bd52015-04-17 11:53:14 +0100142 CentralDirectoryRecord() = default;
143 DISALLOW_COPY_AND_ASSIGN(CentralDirectoryRecord);
Narayan Kamath926973e2014-06-09 14:18:14 +0100144} __attribute__((packed));
Narayan Kamath7462f022013-11-21 13:05:04 +0000145
Narayan Kamath926973e2014-06-09 14:18:14 +0100146// The local file header for a given entry. This duplicates information
147// present in the central directory of the archive. It is an error for
148// the information here to be different from the central directory
149// information for a given entry.
150struct LocalFileHeader {
151 static const uint32_t kSignature = 0x04034b50;
Narayan Kamath7462f022013-11-21 13:05:04 +0000152
Narayan Kamath926973e2014-06-09 14:18:14 +0100153 // The local file header signature, must be |kSignature|.
154 uint32_t lfh_signature;
155 // Tool version. Ignored by this implementation.
156 uint16_t version_needed;
157 // The "general purpose bit flags" for this entry. The only
158 // flag value that we currently check for is the "data descriptor"
159 // flag.
160 uint16_t gpb_flags;
161 // The compression method for this entry, one of |kCompressStored|
162 // and |kCompressDeflated|.
163 uint16_t compression_method;
164 // The file modification time and date for this entry.
165 uint16_t last_mod_time;
166 uint16_t last_mod_date;
167 // The CRC-32 checksum for this entry.
168 uint32_t crc32;
169 // The compressed size (in bytes) of this entry.
170 uint32_t compressed_size;
171 // The uncompressed size (in bytes) of this entry.
172 uint32_t uncompressed_size;
173 // The length of the entry file name in bytes. The file name
174 // will appear immediately after this record.
175 uint16_t file_name_length;
176 // The length of the extra field info (in bytes). This data
177 // will appear immediately after the entry file name.
178 uint16_t extra_field_length;
179 private:
Narayan Kamathf899bd52015-04-17 11:53:14 +0100180 LocalFileHeader() = default;
181 DISALLOW_COPY_AND_ASSIGN(LocalFileHeader);
Narayan Kamath926973e2014-06-09 14:18:14 +0100182} __attribute__((packed));
183
184struct DataDescriptor {
185 // The *optional* data descriptor start signature.
186 static const uint32_t kOptSignature = 0x08074b50;
187
188 // CRC-32 checksum of the entry.
189 uint32_t crc32;
190 // Compressed size of the entry.
191 uint32_t compressed_size;
192 // Uncompressed size of the entry.
193 uint32_t uncompressed_size;
194 private:
Narayan Kamathf899bd52015-04-17 11:53:14 +0100195 DataDescriptor() = default;
196 DISALLOW_COPY_AND_ASSIGN(DataDescriptor);
Narayan Kamath926973e2014-06-09 14:18:14 +0100197} __attribute__((packed));
198
Narayan Kamath926973e2014-06-09 14:18:14 +0100199
Piotr Jastrzebskibd0a7482014-08-13 09:49:25 +0000200static const uint32_t kGPBDDFlagMask = 0x0008; // mask value that signifies that the entry has a DD
Narayan Kamath7462f022013-11-21 13:05:04 +0000201
Narayan Kamath926973e2014-06-09 14:18:14 +0100202// The maximum size of a central directory or a file
203// comment in bytes.
204static const uint32_t kMaxCommentLen = 65535;
205
206// The maximum number of bytes to scan backwards for the EOCD start.
207static const uint32_t kMaxEOCDSearch = kMaxCommentLen + sizeof(EocdRecord);
208
Narayan Kamath7462f022013-11-21 13:05:04 +0000209static const char* kErrorMessages[] = {
210 "Unknown return code.",
Narayan Kamatheb41ad22013-12-09 16:26:36 +0000211 "Iteration ended",
Narayan Kamath7462f022013-11-21 13:05:04 +0000212 "Zlib error",
213 "Invalid file",
214 "Invalid handle",
215 "Duplicate entries in archive",
216 "Empty archive",
217 "Entry not found",
218 "Invalid offset",
219 "Inconsistent information",
220 "Invalid entry name",
Narayan Kamatheb41ad22013-12-09 16:26:36 +0000221 "I/O Error",
Narayan Kamatheaf98852013-12-11 14:51:51 +0000222 "File mapping failed"
Narayan Kamath7462f022013-11-21 13:05:04 +0000223};
224
225static const int32_t kErrorMessageUpperBound = 0;
226
Narayan Kamatheb41ad22013-12-09 16:26:36 +0000227static const int32_t kIterationEnd = -1;
Narayan Kamath7462f022013-11-21 13:05:04 +0000228
229// We encountered a Zlib error when inflating a stream from this file.
230// Usually indicates file corruption.
231static const int32_t kZlibError = -2;
232
233// The input file cannot be processed as a zip archive. Usually because
234// it's too small, too large or does not have a valid signature.
235static const int32_t kInvalidFile = -3;
236
237// An invalid iteration / ziparchive handle was passed in as an input
238// argument.
239static const int32_t kInvalidHandle = -4;
240
241// The zip archive contained two (or possibly more) entries with the same
242// name.
243static const int32_t kDuplicateEntry = -5;
244
245// The zip archive contains no entries.
246static const int32_t kEmptyArchive = -6;
247
248// The specified entry was not found in the archive.
249static const int32_t kEntryNotFound = -7;
250
251// The zip archive contained an invalid local file header pointer.
252static const int32_t kInvalidOffset = -8;
253
254// The zip archive contained inconsistent entry information. This could
255// be because the central directory & local file header did not agree, or
256// if the actual uncompressed length or crc32 do not match their declared
257// values.
258static const int32_t kInconsistentInformation = -9;
259
260// An invalid entry name was encountered.
261static const int32_t kInvalidEntryName = -10;
262
Narayan Kamatheb41ad22013-12-09 16:26:36 +0000263// An I/O related system call (read, lseek, ftruncate, map) failed.
264static const int32_t kIoError = -11;
Narayan Kamath7462f022013-11-21 13:05:04 +0000265
Narayan Kamatheaf98852013-12-11 14:51:51 +0000266// We were not able to mmap the central directory or entry contents.
267static const int32_t kMmapFailed = -12;
Narayan Kamath7462f022013-11-21 13:05:04 +0000268
Narayan Kamatheaf98852013-12-11 14:51:51 +0000269static const int32_t kErrorMessageLowerBound = -13;
Narayan Kamath7462f022013-11-21 13:05:04 +0000270
Narayan Kamath7462f022013-11-21 13:05:04 +0000271/*
272 * A Read-only Zip archive.
273 *
274 * We want "open" and "find entry by name" to be fast operations, and
275 * we want to use as little memory as possible. We memory-map the zip
276 * central directory, and load a hash table with pointers to the filenames
277 * (which aren't null-terminated). The other fields are at a fixed offset
278 * from the filename, so we don't need to extract those (but we do need
279 * to byte-read and endian-swap them every time we want them).
280 *
281 * It's possible that somebody has handed us a massive (~1GB) zip archive,
282 * so we can't expect to mmap the entire file.
283 *
284 * To speed comparisons when doing a lookup by name, we could make the mapping
285 * "private" (copy-on-write) and null-terminate the filenames after verifying
286 * the record structure. However, this requires a private mapping of
287 * every page that the Central Directory touches. Easier to tuck a copy
288 * of the string length into the hash table entry.
289 */
290struct ZipArchive {
291 /* open Zip archive */
Neil Fullerb1a113f2014-07-25 14:43:04 +0100292 const int fd;
Dmitriy Ivanov40b52b22014-07-15 19:33:00 -0700293 const bool close_file;
Narayan Kamath7462f022013-11-21 13:05:04 +0000294
295 /* mapped central directory area */
296 off64_t directory_offset;
Dmitriy Ivanov4b67f832015-03-06 10:22:34 -0800297 android::FileMap directory_map;
Narayan Kamath7462f022013-11-21 13:05:04 +0000298
299 /* number of entries in the Zip archive */
300 uint16_t num_entries;
301
302 /*
303 * We know how many entries are in the Zip archive, so we can have a
304 * fixed-size hash table. We define a load factor of 0.75 and overallocat
305 * so the maximum number entries can never be higher than
306 * ((4 * UINT16_MAX) / 3 + 1) which can safely fit into a uint32_t.
307 */
308 uint32_t hash_table_size;
309 ZipEntryName* hash_table;
Neil Fullerb1a113f2014-07-25 14:43:04 +0100310
Dmitriy Ivanov40b52b22014-07-15 19:33:00 -0700311 ZipArchive(const int fd, bool assume_ownership) :
Neil Fullerb1a113f2014-07-25 14:43:04 +0100312 fd(fd),
Dmitriy Ivanov40b52b22014-07-15 19:33:00 -0700313 close_file(assume_ownership),
Neil Fullerb1a113f2014-07-25 14:43:04 +0100314 directory_offset(0),
Neil Fullerb1a113f2014-07-25 14:43:04 +0100315 num_entries(0),
316 hash_table_size(0),
317 hash_table(NULL) {}
318
319 ~ZipArchive() {
Dmitriy Ivanov40b52b22014-07-15 19:33:00 -0700320 if (close_file && fd >= 0) {
Neil Fullerb1a113f2014-07-25 14:43:04 +0100321 close(fd);
322 }
323
Neil Fullerb1a113f2014-07-25 14:43:04 +0100324 free(hash_table);
325 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000326};
327
Narayan Kamath7462f022013-11-21 13:05:04 +0000328/*
329 * Round up to the next highest power of 2.
330 *
331 * Found on http://graphics.stanford.edu/~seander/bithacks.html.
332 */
333static uint32_t RoundUpPower2(uint32_t val) {
334 val--;
335 val |= val >> 1;
336 val |= val >> 2;
337 val |= val >> 4;
338 val |= val >> 8;
339 val |= val >> 16;
340 val++;
341
342 return val;
343}
344
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100345static uint32_t ComputeHash(const ZipEntryName& name) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000346 uint32_t hash = 0;
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100347 uint16_t len = name.name_length;
348 const uint8_t* str = name.name;
Narayan Kamath7462f022013-11-21 13:05:04 +0000349
350 while (len--) {
351 hash = hash * 31 + *str++;
352 }
353
354 return hash;
355}
356
357/*
358 * Convert a ZipEntry to a hash table index, verifying that it's in a
359 * valid range.
360 */
361static int64_t EntryToIndex(const ZipEntryName* hash_table,
362 const uint32_t hash_table_size,
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100363 const ZipEntryName& name) {
364 const uint32_t hash = ComputeHash(name);
Narayan Kamath7462f022013-11-21 13:05:04 +0000365
366 // NOTE: (hash_table_size - 1) is guaranteed to be non-negative.
367 uint32_t ent = hash & (hash_table_size - 1);
368 while (hash_table[ent].name != NULL) {
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100369 if (hash_table[ent].name_length == name.name_length &&
370 memcmp(hash_table[ent].name, name.name, name.name_length) == 0) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000371 return ent;
372 }
373
374 ent = (ent + 1) & (hash_table_size - 1);
375 }
376
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100377 ALOGV("Zip: Unable to find entry %.*s", name.name_length, name.name);
Narayan Kamath7462f022013-11-21 13:05:04 +0000378 return kEntryNotFound;
379}
380
381/*
382 * Add a new entry to the hash table.
383 */
384static int32_t AddToHash(ZipEntryName *hash_table, const uint64_t hash_table_size,
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100385 const ZipEntryName& name) {
386 const uint64_t hash = ComputeHash(name);
Narayan Kamath7462f022013-11-21 13:05:04 +0000387 uint32_t ent = hash & (hash_table_size - 1);
388
389 /*
390 * We over-allocated the table, so we're guaranteed to find an empty slot.
391 * Further, we guarantee that the hashtable size is not 0.
392 */
393 while (hash_table[ent].name != NULL) {
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100394 if (hash_table[ent].name_length == name.name_length &&
395 memcmp(hash_table[ent].name, name.name, name.name_length) == 0) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000396 // We've found a duplicate entry. We don't accept it
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100397 ALOGW("Zip: Found duplicate entry %.*s", name.name_length, name.name);
Narayan Kamath7462f022013-11-21 13:05:04 +0000398 return kDuplicateEntry;
399 }
400 ent = (ent + 1) & (hash_table_size - 1);
401 }
402
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100403 hash_table[ent].name = name.name;
404 hash_table[ent].name_length = name.name_length;
Narayan Kamath7462f022013-11-21 13:05:04 +0000405 return 0;
406}
407
Narayan Kamath7462f022013-11-21 13:05:04 +0000408static int32_t MapCentralDirectory0(int fd, const char* debug_file_name,
409 ZipArchive* archive, off64_t file_length,
Narayan Kamath926973e2014-06-09 14:18:14 +0100410 off64_t read_amount, uint8_t* scan_buffer) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000411 const off64_t search_start = file_length - read_amount;
412
413 if (lseek64(fd, search_start, SEEK_SET) != search_start) {
Narayan Kamath926973e2014-06-09 14:18:14 +0100414 ALOGW("Zip: seek %" PRId64 " failed: %s", static_cast<int64_t>(search_start),
415 strerror(errno));
Narayan Kamath7462f022013-11-21 13:05:04 +0000416 return kIoError;
417 }
Narayan Kamath926973e2014-06-09 14:18:14 +0100418 ssize_t actual = TEMP_FAILURE_RETRY(
419 read(fd, scan_buffer, static_cast<size_t>(read_amount)));
420 if (actual != static_cast<ssize_t>(read_amount)) {
421 ALOGW("Zip: read %" PRId64 " failed: %s", static_cast<int64_t>(read_amount),
422 strerror(errno));
Narayan Kamath7462f022013-11-21 13:05:04 +0000423 return kIoError;
424 }
425
426 /*
427 * Scan backward for the EOCD magic. In an archive without a trailing
428 * comment, we'll find it on the first try. (We may want to consider
429 * doing an initial minimal read; if we don't find it, retry with a
430 * second read as above.)
431 */
Narayan Kamath926973e2014-06-09 14:18:14 +0100432 int i = read_amount - sizeof(EocdRecord);
433 for (; i >= 0; i--) {
Dan Albert1ae07642015-04-09 14:11:18 -0700434 if (scan_buffer[i] == 0x50) {
435 uint32_t* sig_addr = reinterpret_cast<uint32_t*>(&scan_buffer[i]);
436 if (get_unaligned<uint32_t>(sig_addr) == EocdRecord::kSignature) {
437 ALOGV("+++ Found EOCD at buf+%d", i);
438 break;
439 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000440 }
441 }
442 if (i < 0) {
443 ALOGD("Zip: EOCD not found, %s is not zip", debug_file_name);
444 return kInvalidFile;
445 }
446
447 const off64_t eocd_offset = search_start + i;
Narayan Kamath926973e2014-06-09 14:18:14 +0100448 const EocdRecord* eocd = reinterpret_cast<const EocdRecord*>(scan_buffer + i);
Narayan Kamath7462f022013-11-21 13:05:04 +0000449 /*
Narayan Kamath926973e2014-06-09 14:18:14 +0100450 * Verify that there's no trailing space at the end of the central directory
451 * and its comment.
Narayan Kamath7462f022013-11-21 13:05:04 +0000452 */
Narayan Kamath926973e2014-06-09 14:18:14 +0100453 const off64_t calculated_length = eocd_offset + sizeof(EocdRecord)
454 + eocd->comment_length;
455 if (calculated_length != file_length) {
Narayan Kamath4f6b4992014-06-03 13:59:23 +0100456 ALOGW("Zip: %" PRId64 " extraneous bytes at the end of the central directory",
Narayan Kamath926973e2014-06-09 14:18:14 +0100457 static_cast<int64_t>(file_length - calculated_length));
Narayan Kamath4f6b4992014-06-03 13:59:23 +0100458 return kInvalidFile;
459 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000460
Narayan Kamath926973e2014-06-09 14:18:14 +0100461 /*
462 * Grab the CD offset and size, and the number of entries in the
463 * archive and verify that they look reasonable.
464 */
465 if (eocd->cd_start_offset + eocd->cd_size > eocd_offset) {
466 ALOGW("Zip: bad offsets (dir %" PRIu32 ", size %" PRIu32 ", eocd %" PRId64 ")",
467 eocd->cd_start_offset, eocd->cd_size, static_cast<int64_t>(eocd_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000468 return kInvalidOffset;
469 }
Narayan Kamath926973e2014-06-09 14:18:14 +0100470 if (eocd->num_records == 0) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000471 ALOGW("Zip: empty archive?");
472 return kEmptyArchive;
473 }
474
Narayan Kamath926973e2014-06-09 14:18:14 +0100475 ALOGV("+++ num_entries=%" PRIu32 "dir_size=%" PRIu32 " dir_offset=%" PRIu32,
476 eocd->num_records, eocd->cd_size, eocd->cd_start_offset);
Narayan Kamath7462f022013-11-21 13:05:04 +0000477
478 /*
479 * It all looks good. Create a mapping for the CD, and set the fields
480 * in archive.
481 */
Dmitriy Ivanov4b67f832015-03-06 10:22:34 -0800482 if (!archive->directory_map.create(debug_file_name, fd,
483 static_cast<off64_t>(eocd->cd_start_offset),
484 static_cast<size_t>(eocd->cd_size), true /* read only */) ) {
Narayan Kamatheaf98852013-12-11 14:51:51 +0000485 return kMmapFailed;
Narayan Kamath7462f022013-11-21 13:05:04 +0000486 }
487
Narayan Kamath926973e2014-06-09 14:18:14 +0100488 archive->num_entries = eocd->num_records;
489 archive->directory_offset = eocd->cd_start_offset;
Narayan Kamath7462f022013-11-21 13:05:04 +0000490
491 return 0;
492}
493
494/*
495 * Find the zip Central Directory and memory-map it.
496 *
497 * On success, returns 0 after populating fields from the EOCD area:
498 * directory_offset
499 * directory_map
500 * num_entries
501 */
502static int32_t MapCentralDirectory(int fd, const char* debug_file_name,
503 ZipArchive* archive) {
504
505 // Test file length. We use lseek64 to make sure the file
506 // is small enough to be a zip file (Its size must be less than
507 // 0xffffffff bytes).
508 off64_t file_length = lseek64(fd, 0, SEEK_END);
509 if (file_length == -1) {
510 ALOGV("Zip: lseek on fd %d failed", fd);
511 return kInvalidFile;
512 }
513
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800514 if (file_length > static_cast<off64_t>(0xffffffff)) {
Narayan Kamath926973e2014-06-09 14:18:14 +0100515 ALOGV("Zip: zip file too long %" PRId64, static_cast<int64_t>(file_length));
Narayan Kamath7462f022013-11-21 13:05:04 +0000516 return kInvalidFile;
517 }
518
Narayan Kamath926973e2014-06-09 14:18:14 +0100519 if (file_length < static_cast<off64_t>(sizeof(EocdRecord))) {
520 ALOGV("Zip: length %" PRId64 " is too small to be zip", static_cast<int64_t>(file_length));
Narayan Kamath7462f022013-11-21 13:05:04 +0000521 return kInvalidFile;
522 }
523
524 /*
525 * Perform the traditional EOCD snipe hunt.
526 *
527 * We're searching for the End of Central Directory magic number,
528 * which appears at the start of the EOCD block. It's followed by
529 * 18 bytes of EOCD stuff and up to 64KB of archive comment. We
530 * need to read the last part of the file into a buffer, dig through
531 * it to find the magic number, parse some values out, and use those
532 * to determine the extent of the CD.
533 *
534 * We start by pulling in the last part of the file.
535 */
Narayan Kamath926973e2014-06-09 14:18:14 +0100536 off64_t read_amount = kMaxEOCDSearch;
537 if (file_length < read_amount) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000538 read_amount = file_length;
539 }
540
Narayan Kamath926973e2014-06-09 14:18:14 +0100541 uint8_t* scan_buffer = reinterpret_cast<uint8_t*>(malloc(read_amount));
Narayan Kamath7462f022013-11-21 13:05:04 +0000542 int32_t result = MapCentralDirectory0(fd, debug_file_name, archive,
543 file_length, read_amount, scan_buffer);
544
545 free(scan_buffer);
546 return result;
547}
548
549/*
550 * Parses the Zip archive's Central Directory. Allocates and populates the
551 * hash table.
552 *
553 * Returns 0 on success.
554 */
555static int32_t ParseZipArchive(ZipArchive* archive) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800556 const uint8_t* const cd_ptr =
557 reinterpret_cast<const uint8_t*>(archive->directory_map.getDataPtr());
Dmitriy Ivanov4b67f832015-03-06 10:22:34 -0800558 const size_t cd_length = archive->directory_map.getDataLength();
Narayan Kamath926973e2014-06-09 14:18:14 +0100559 const uint16_t num_entries = archive->num_entries;
Narayan Kamath7462f022013-11-21 13:05:04 +0000560
561 /*
562 * Create hash table. We have a minimum 75% load factor, possibly as
563 * low as 50% after we round off to a power of 2. There must be at
564 * least one unused entry to avoid an infinite loop during creation.
565 */
566 archive->hash_table_size = RoundUpPower2(1 + (num_entries * 4) / 3);
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800567 archive->hash_table = reinterpret_cast<ZipEntryName*>(calloc(archive->hash_table_size,
568 sizeof(ZipEntryName)));
Narayan Kamath7462f022013-11-21 13:05:04 +0000569
570 /*
571 * Walk through the central directory, adding entries to the hash
572 * table and verifying values.
573 */
Narayan Kamath926973e2014-06-09 14:18:14 +0100574 const uint8_t* const cd_end = cd_ptr + cd_length;
Narayan Kamath7462f022013-11-21 13:05:04 +0000575 const uint8_t* ptr = cd_ptr;
576 for (uint16_t i = 0; i < num_entries; i++) {
Narayan Kamath926973e2014-06-09 14:18:14 +0100577 const CentralDirectoryRecord* cdr =
578 reinterpret_cast<const CentralDirectoryRecord*>(ptr);
579 if (cdr->record_signature != CentralDirectoryRecord::kSignature) {
Mark Salyzyn088bf902014-05-08 16:02:20 -0700580 ALOGW("Zip: missed a central dir sig (at %" PRIu16 ")", i);
Dmitriy Ivanov3ea93da2015-03-06 11:48:47 -0800581 return -1;
Narayan Kamath7462f022013-11-21 13:05:04 +0000582 }
583
Narayan Kamath926973e2014-06-09 14:18:14 +0100584 if (ptr + sizeof(CentralDirectoryRecord) > cd_end) {
Mark Salyzyn088bf902014-05-08 16:02:20 -0700585 ALOGW("Zip: ran off the end (at %" PRIu16 ")", i);
Dmitriy Ivanov3ea93da2015-03-06 11:48:47 -0800586 return -1;
Narayan Kamath7462f022013-11-21 13:05:04 +0000587 }
588
Narayan Kamath926973e2014-06-09 14:18:14 +0100589 const off64_t local_header_offset = cdr->local_file_header_offset;
Narayan Kamath7462f022013-11-21 13:05:04 +0000590 if (local_header_offset >= archive->directory_offset) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800591 ALOGW("Zip: bad LFH offset %" PRId64 " at entry %" PRIu16,
592 static_cast<int64_t>(local_header_offset), i);
Dmitriy Ivanov3ea93da2015-03-06 11:48:47 -0800593 return -1;
Narayan Kamath7462f022013-11-21 13:05:04 +0000594 }
595
Narayan Kamath926973e2014-06-09 14:18:14 +0100596 const uint16_t file_name_length = cdr->file_name_length;
597 const uint16_t extra_length = cdr->extra_field_length;
598 const uint16_t comment_length = cdr->comment_length;
Piotr Jastrzebski78271ba2014-08-15 12:53:00 +0100599 const uint8_t* file_name = ptr + sizeof(CentralDirectoryRecord);
600
Narayan Kamath044bc8e2014-12-03 18:22:53 +0000601 /* check that file name is valid UTF-8 and doesn't contain NUL (U+0000) characters */
602 if (!IsValidEntryName(file_name, file_name_length)) {
Dmitriy Ivanov3ea93da2015-03-06 11:48:47 -0800603 return -1;
Piotr Jastrzebski78271ba2014-08-15 12:53:00 +0100604 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000605
606 /* add the CDE filename to the hash table */
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100607 ZipEntryName entry_name;
608 entry_name.name = file_name;
609 entry_name.name_length = file_name_length;
Narayan Kamath7462f022013-11-21 13:05:04 +0000610 const int add_result = AddToHash(archive->hash_table,
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100611 archive->hash_table_size, entry_name);
Dmitriy Ivanov3ea93da2015-03-06 11:48:47 -0800612 if (add_result != 0) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000613 ALOGW("Zip: Error adding entry to hash table %d", add_result);
Dmitriy Ivanov3ea93da2015-03-06 11:48:47 -0800614 return add_result;
Narayan Kamath7462f022013-11-21 13:05:04 +0000615 }
616
Narayan Kamath926973e2014-06-09 14:18:14 +0100617 ptr += sizeof(CentralDirectoryRecord) + file_name_length + extra_length + comment_length;
618 if ((ptr - cd_ptr) > static_cast<int64_t>(cd_length)) {
Mark Salyzyn088bf902014-05-08 16:02:20 -0700619 ALOGW("Zip: bad CD advance (%tu vs %zu) at entry %" PRIu16,
620 ptr - cd_ptr, cd_length, i);
Dmitriy Ivanov3ea93da2015-03-06 11:48:47 -0800621 return -1;
Narayan Kamath7462f022013-11-21 13:05:04 +0000622 }
623 }
Mark Salyzyn088bf902014-05-08 16:02:20 -0700624 ALOGV("+++ zip good scan %" PRIu16 " entries", num_entries);
Narayan Kamath7462f022013-11-21 13:05:04 +0000625
Dmitriy Ivanov3ea93da2015-03-06 11:48:47 -0800626 return 0;
Narayan Kamath7462f022013-11-21 13:05:04 +0000627}
628
629static int32_t OpenArchiveInternal(ZipArchive* archive,
630 const char* debug_file_name) {
631 int32_t result = -1;
632 if ((result = MapCentralDirectory(archive->fd, debug_file_name, archive))) {
633 return result;
634 }
635
636 if ((result = ParseZipArchive(archive))) {
637 return result;
638 }
639
640 return 0;
641}
642
643int32_t OpenArchiveFd(int fd, const char* debug_file_name,
Dmitriy Ivanov40b52b22014-07-15 19:33:00 -0700644 ZipArchiveHandle* handle, bool assume_ownership) {
645 ZipArchive* archive = new ZipArchive(fd, assume_ownership);
Narayan Kamath7462f022013-11-21 13:05:04 +0000646 *handle = archive;
Narayan Kamath7462f022013-11-21 13:05:04 +0000647 return OpenArchiveInternal(archive, debug_file_name);
648}
649
650int32_t OpenArchive(const char* fileName, ZipArchiveHandle* handle) {
Neil Fullerb1a113f2014-07-25 14:43:04 +0100651 const int fd = open(fileName, O_RDONLY | O_BINARY, 0);
Dmitriy Ivanov40b52b22014-07-15 19:33:00 -0700652 ZipArchive* archive = new ZipArchive(fd, true);
Narayan Kamath7462f022013-11-21 13:05:04 +0000653 *handle = archive;
654
Narayan Kamath7462f022013-11-21 13:05:04 +0000655 if (fd < 0) {
656 ALOGW("Unable to open '%s': %s", fileName, strerror(errno));
657 return kIoError;
Narayan Kamath7462f022013-11-21 13:05:04 +0000658 }
Dmitriy Ivanov40b52b22014-07-15 19:33:00 -0700659
Narayan Kamath7462f022013-11-21 13:05:04 +0000660 return OpenArchiveInternal(archive, fileName);
661}
662
663/*
664 * Close a ZipArchive, closing the file and freeing the contents.
665 */
666void CloseArchive(ZipArchiveHandle handle) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800667 ZipArchive* archive = reinterpret_cast<ZipArchive*>(handle);
Narayan Kamath7462f022013-11-21 13:05:04 +0000668 ALOGV("Closing archive %p", archive);
Neil Fullerb1a113f2014-07-25 14:43:04 +0100669 delete archive;
Narayan Kamath7462f022013-11-21 13:05:04 +0000670}
671
672static int32_t UpdateEntryFromDataDescriptor(int fd,
673 ZipEntry *entry) {
Narayan Kamath926973e2014-06-09 14:18:14 +0100674 uint8_t ddBuf[sizeof(DataDescriptor) + sizeof(DataDescriptor::kOptSignature)];
Narayan Kamath7462f022013-11-21 13:05:04 +0000675 ssize_t actual = TEMP_FAILURE_RETRY(read(fd, ddBuf, sizeof(ddBuf)));
676 if (actual != sizeof(ddBuf)) {
677 return kIoError;
678 }
679
Narayan Kamath926973e2014-06-09 14:18:14 +0100680 const uint32_t ddSignature = *(reinterpret_cast<const uint32_t*>(ddBuf));
681 const uint16_t offset = (ddSignature == DataDescriptor::kOptSignature) ? 4 : 0;
682 const DataDescriptor* descriptor = reinterpret_cast<const DataDescriptor*>(ddBuf + offset);
Narayan Kamath7462f022013-11-21 13:05:04 +0000683
Narayan Kamath926973e2014-06-09 14:18:14 +0100684 entry->crc32 = descriptor->crc32;
685 entry->compressed_length = descriptor->compressed_size;
686 entry->uncompressed_length = descriptor->uncompressed_size;
Narayan Kamath7462f022013-11-21 13:05:04 +0000687
688 return 0;
689}
690
691// Attempts to read |len| bytes into |buf| at offset |off|.
692//
693// This method uses pread64 on platforms that support it and
694// lseek64 + read on platforms that don't. This implies that
695// callers should not rely on the |fd| offset being incremented
696// as a side effect of this call.
697static inline ssize_t ReadAtOffset(int fd, uint8_t* buf, size_t len,
698 off64_t off) {
Yabin Cui70160f42014-11-19 20:47:18 -0800699#if !defined(_WIN32)
Narayan Kamath7462f022013-11-21 13:05:04 +0000700 return TEMP_FAILURE_RETRY(pread64(fd, buf, len, off));
701#else
702 // The only supported platform that doesn't support pread at the moment
703 // is Windows. Only recent versions of windows support unix like forks,
704 // and even there the semantics are quite different.
705 if (lseek64(fd, off, SEEK_SET) != off) {
Mark Salyzyn99ef9912014-03-14 14:26:22 -0700706 ALOGW("Zip: failed seek to offset %" PRId64, off);
Narayan Kamath7462f022013-11-21 13:05:04 +0000707 return kIoError;
708 }
709
710 return TEMP_FAILURE_RETRY(read(fd, buf, len));
Yabin Cui70160f42014-11-19 20:47:18 -0800711#endif
Narayan Kamath7462f022013-11-21 13:05:04 +0000712}
713
714static int32_t FindEntry(const ZipArchive* archive, const int ent,
715 ZipEntry* data) {
716 const uint16_t nameLen = archive->hash_table[ent].name_length;
Narayan Kamath7462f022013-11-21 13:05:04 +0000717
718 // Recover the start of the central directory entry from the filename
719 // pointer. The filename is the first entry past the fixed-size data,
720 // so we can just subtract back from that.
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100721 const uint8_t* ptr = archive->hash_table[ent].name;
Narayan Kamath926973e2014-06-09 14:18:14 +0100722 ptr -= sizeof(CentralDirectoryRecord);
Narayan Kamath7462f022013-11-21 13:05:04 +0000723
724 // This is the base of our mmapped region, we have to sanity check that
725 // the name that's in the hash table is a pointer to a location within
726 // this mapped region.
Narayan Kamath926973e2014-06-09 14:18:14 +0100727 const uint8_t* base_ptr = reinterpret_cast<const uint8_t*>(
Dmitriy Ivanov4b67f832015-03-06 10:22:34 -0800728 archive->directory_map.getDataPtr());
729 if (ptr < base_ptr || ptr > base_ptr + archive->directory_map.getDataLength()) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000730 ALOGW("Zip: Invalid entry pointer");
731 return kInvalidOffset;
732 }
733
Narayan Kamath926973e2014-06-09 14:18:14 +0100734 const CentralDirectoryRecord *cdr =
735 reinterpret_cast<const CentralDirectoryRecord*>(ptr);
736
Narayan Kamath7462f022013-11-21 13:05:04 +0000737 // The offset of the start of the central directory in the zipfile.
738 // We keep this lying around so that we can sanity check all our lengths
739 // and our per-file structures.
740 const off64_t cd_offset = archive->directory_offset;
741
742 // Fill out the compression method, modification time, crc32
743 // and other interesting attributes from the central directory. These
744 // will later be compared against values from the local file header.
Narayan Kamath926973e2014-06-09 14:18:14 +0100745 data->method = cdr->compression_method;
746 data->mod_time = cdr->last_mod_time;
747 data->crc32 = cdr->crc32;
748 data->compressed_length = cdr->compressed_size;
749 data->uncompressed_length = cdr->uncompressed_size;
Narayan Kamath7462f022013-11-21 13:05:04 +0000750
751 // Figure out the local header offset from the central directory. The
752 // actual file data will begin after the local header and the name /
753 // extra comments.
Narayan Kamath926973e2014-06-09 14:18:14 +0100754 const off64_t local_header_offset = cdr->local_file_header_offset;
755 if (local_header_offset + static_cast<off64_t>(sizeof(LocalFileHeader)) >= cd_offset) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000756 ALOGW("Zip: bad local hdr offset in zip");
757 return kInvalidOffset;
758 }
759
Narayan Kamath926973e2014-06-09 14:18:14 +0100760 uint8_t lfh_buf[sizeof(LocalFileHeader)];
Narayan Kamath7462f022013-11-21 13:05:04 +0000761 ssize_t actual = ReadAtOffset(archive->fd, lfh_buf, sizeof(lfh_buf),
762 local_header_offset);
763 if (actual != sizeof(lfh_buf)) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800764 ALOGW("Zip: failed reading lfh name from offset %" PRId64,
765 static_cast<int64_t>(local_header_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000766 return kIoError;
767 }
768
Narayan Kamath926973e2014-06-09 14:18:14 +0100769 const LocalFileHeader *lfh = reinterpret_cast<const LocalFileHeader*>(lfh_buf);
770
771 if (lfh->lfh_signature != LocalFileHeader::kSignature) {
Mark Salyzyn99ef9912014-03-14 14:26:22 -0700772 ALOGW("Zip: didn't find signature at start of lfh, offset=%" PRId64,
Narayan Kamath926973e2014-06-09 14:18:14 +0100773 static_cast<int64_t>(local_header_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000774 return kInvalidOffset;
775 }
776
777 // Paranoia: Match the values specified in the local file header
778 // to those specified in the central directory.
Narayan Kamath926973e2014-06-09 14:18:14 +0100779 if ((lfh->gpb_flags & kGPBDDFlagMask) == 0) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000780 data->has_data_descriptor = 0;
Narayan Kamath926973e2014-06-09 14:18:14 +0100781 if (data->compressed_length != lfh->compressed_size
782 || data->uncompressed_length != lfh->uncompressed_size
783 || data->crc32 != lfh->crc32) {
Mark Salyzyn088bf902014-05-08 16:02:20 -0700784 ALOGW("Zip: size/crc32 mismatch. expected {%" PRIu32 ", %" PRIu32
785 ", %" PRIx32 "}, was {%" PRIu32 ", %" PRIu32 ", %" PRIx32 "}",
Narayan Kamath7462f022013-11-21 13:05:04 +0000786 data->compressed_length, data->uncompressed_length, data->crc32,
Narayan Kamath926973e2014-06-09 14:18:14 +0100787 lfh->compressed_size, lfh->uncompressed_size, lfh->crc32);
Narayan Kamath7462f022013-11-21 13:05:04 +0000788 return kInconsistentInformation;
789 }
790 } else {
791 data->has_data_descriptor = 1;
792 }
793
794 // Check that the local file header name matches the declared
795 // name in the central directory.
Narayan Kamath926973e2014-06-09 14:18:14 +0100796 if (lfh->file_name_length == nameLen) {
797 const off64_t name_offset = local_header_offset + sizeof(LocalFileHeader);
Mykola Kondratenko50afc152014-09-08 12:46:37 +0200798 if (name_offset + lfh->file_name_length > cd_offset) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000799 ALOGW("Zip: Invalid declared length");
800 return kInvalidOffset;
801 }
802
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800803 uint8_t* name_buf = reinterpret_cast<uint8_t*>(malloc(nameLen));
Narayan Kamath7462f022013-11-21 13:05:04 +0000804 ssize_t actual = ReadAtOffset(archive->fd, name_buf, nameLen,
805 name_offset);
806
807 if (actual != nameLen) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800808 ALOGW("Zip: failed reading lfh name from offset %" PRId64, static_cast<int64_t>(name_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000809 free(name_buf);
810 return kIoError;
811 }
812
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100813 if (memcmp(archive->hash_table[ent].name, name_buf, nameLen)) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000814 free(name_buf);
815 return kInconsistentInformation;
816 }
817
818 free(name_buf);
819 } else {
820 ALOGW("Zip: lfh name did not match central directory.");
821 return kInconsistentInformation;
822 }
823
Narayan Kamath926973e2014-06-09 14:18:14 +0100824 const off64_t data_offset = local_header_offset + sizeof(LocalFileHeader)
825 + lfh->file_name_length + lfh->extra_field_length;
Narayan Kamath48953a12014-01-24 12:32:39 +0000826 if (data_offset > cd_offset) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800827 ALOGW("Zip: bad data offset %" PRId64 " in zip", static_cast<int64_t>(data_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000828 return kInvalidOffset;
829 }
830
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800831 if (static_cast<off64_t>(data_offset + data->compressed_length) > cd_offset) {
Mark Salyzyn088bf902014-05-08 16:02:20 -0700832 ALOGW("Zip: bad compressed length in zip (%" PRId64 " + %" PRIu32 " > %" PRId64 ")",
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800833 static_cast<int64_t>(data_offset), data->compressed_length, static_cast<int64_t>(cd_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000834 return kInvalidOffset;
835 }
836
837 if (data->method == kCompressStored &&
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800838 static_cast<off64_t>(data_offset + data->uncompressed_length) > cd_offset) {
Mark Salyzyn088bf902014-05-08 16:02:20 -0700839 ALOGW("Zip: bad uncompressed length in zip (%" PRId64 " + %" PRIu32 " > %" PRId64 ")",
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800840 static_cast<int64_t>(data_offset), data->uncompressed_length,
841 static_cast<int64_t>(cd_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000842 return kInvalidOffset;
843 }
844
845 data->offset = data_offset;
846 return 0;
847}
848
849struct IterationHandle {
850 uint32_t position;
Piotr Jastrzebski10aa9a02014-08-19 09:01:20 +0100851 // We're not using vector here because this code is used in the Windows SDK
852 // where the STL is not available.
Piotr Jastrzebski8e085362014-08-18 11:37:45 +0100853 const uint8_t* prefix;
854 uint16_t prefix_len;
Narayan Kamath7462f022013-11-21 13:05:04 +0000855 ZipArchive* archive;
Piotr Jastrzebski8e085362014-08-18 11:37:45 +0100856
857 IterationHandle() : prefix(NULL), prefix_len(0) {}
858
859 IterationHandle(const ZipEntryName& prefix_name)
860 : prefix_len(prefix_name.name_length) {
861 uint8_t* prefix_copy = new uint8_t[prefix_len];
Piotr Jastrzebski10aa9a02014-08-19 09:01:20 +0100862 memcpy(prefix_copy, prefix_name.name, prefix_len);
Piotr Jastrzebski8e085362014-08-18 11:37:45 +0100863 prefix = prefix_copy;
864 }
865
866 ~IterationHandle() {
Piotr Jastrzebski10aa9a02014-08-19 09:01:20 +0100867 delete[] prefix;
Piotr Jastrzebski8e085362014-08-18 11:37:45 +0100868 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000869};
870
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100871int32_t StartIteration(ZipArchiveHandle handle, void** cookie_ptr,
872 const ZipEntryName* optional_prefix) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800873 ZipArchive* archive = reinterpret_cast<ZipArchive*>(handle);
Narayan Kamath7462f022013-11-21 13:05:04 +0000874
875 if (archive == NULL || archive->hash_table == NULL) {
876 ALOGW("Zip: Invalid ZipArchiveHandle");
877 return kInvalidHandle;
878 }
879
Piotr Jastrzebski8e085362014-08-18 11:37:45 +0100880 IterationHandle* cookie =
881 optional_prefix != NULL ? new IterationHandle(*optional_prefix) : new IterationHandle();
Narayan Kamath7462f022013-11-21 13:05:04 +0000882 cookie->position = 0;
Narayan Kamath7462f022013-11-21 13:05:04 +0000883 cookie->archive = archive;
Narayan Kamath7462f022013-11-21 13:05:04 +0000884
885 *cookie_ptr = cookie ;
886 return 0;
887}
888
Piotr Jastrzebski79c8b342014-08-08 14:02:17 +0100889void EndIteration(void* cookie) {
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100890 delete reinterpret_cast<IterationHandle*>(cookie);
Piotr Jastrzebski79c8b342014-08-08 14:02:17 +0100891}
892
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100893int32_t FindEntry(const ZipArchiveHandle handle, const ZipEntryName& entryName,
Narayan Kamath7462f022013-11-21 13:05:04 +0000894 ZipEntry* data) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800895 const ZipArchive* archive = reinterpret_cast<ZipArchive*>(handle);
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100896 if (entryName.name_length == 0) {
897 ALOGW("Zip: Invalid filename %.*s", entryName.name_length, entryName.name);
Narayan Kamath7462f022013-11-21 13:05:04 +0000898 return kInvalidEntryName;
899 }
900
901 const int64_t ent = EntryToIndex(archive->hash_table,
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100902 archive->hash_table_size, entryName);
Narayan Kamath7462f022013-11-21 13:05:04 +0000903
904 if (ent < 0) {
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100905 ALOGV("Zip: Could not find entry %.*s", entryName.name_length, entryName.name);
Narayan Kamath7462f022013-11-21 13:05:04 +0000906 return ent;
907 }
908
909 return FindEntry(archive, ent, data);
910}
911
912int32_t Next(void* cookie, ZipEntry* data, ZipEntryName* name) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800913 IterationHandle* handle = reinterpret_cast<IterationHandle*>(cookie);
Narayan Kamath7462f022013-11-21 13:05:04 +0000914 if (handle == NULL) {
915 return kInvalidHandle;
916 }
917
918 ZipArchive* archive = handle->archive;
919 if (archive == NULL || archive->hash_table == NULL) {
920 ALOGW("Zip: Invalid ZipArchiveHandle");
921 return kInvalidHandle;
922 }
923
924 const uint32_t currentOffset = handle->position;
925 const uint32_t hash_table_length = archive->hash_table_size;
926 const ZipEntryName *hash_table = archive->hash_table;
927
928 for (uint32_t i = currentOffset; i < hash_table_length; ++i) {
929 if (hash_table[i].name != NULL &&
Piotr Jastrzebski8e085362014-08-18 11:37:45 +0100930 (handle->prefix_len == 0 ||
931 (memcmp(handle->prefix, hash_table[i].name, handle->prefix_len) == 0))) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000932 handle->position = (i + 1);
933 const int error = FindEntry(archive, i, data);
934 if (!error) {
935 name->name = hash_table[i].name;
936 name->name_length = hash_table[i].name_length;
937 }
938
939 return error;
940 }
941 }
942
943 handle->position = 0;
944 return kIterationEnd;
945}
946
Narayan Kamathf899bd52015-04-17 11:53:14 +0100947class Writer {
948 public:
949 virtual bool Append(uint8_t* buf, size_t buf_size) = 0;
950 virtual ~Writer() {}
951 protected:
952 Writer() = default;
953 private:
954 DISALLOW_COPY_AND_ASSIGN(Writer);
955};
956
957// A Writer that writes data to a fixed size memory region.
958// The size of the memory region must be equal to the total size of
959// the data appended to it.
960class MemoryWriter : public Writer {
961 public:
962 MemoryWriter(uint8_t* buf, size_t size) : Writer(),
963 buf_(buf), size_(size), bytes_written_(0) {
964 }
965
966 virtual bool Append(uint8_t* buf, size_t buf_size) override {
967 if (bytes_written_ + buf_size > size_) {
968 ALOGW("Zip: Unexpected size " ZD " (declared) vs " ZD " (actual)",
969 size_, bytes_written_ + buf_size);
970 return false;
971 }
972
973 memcpy(buf_ + bytes_written_, buf, buf_size);
974 bytes_written_ += buf_size;
975 return true;
976 }
977
978 private:
979 uint8_t* const buf_;
980 const size_t size_;
981 size_t bytes_written_;
982};
983
984// A Writer that appends data to a file |fd| at its current position.
985// The file will be truncated to the end of the written data.
986class FileWriter : public Writer {
987 public:
988
989 // Creates a FileWriter for |fd| and prepare to write |entry| to it,
990 // guaranteeing that the file descriptor is valid and that there's enough
991 // space on the volume to write out the entry completely and that the file
992 // is truncated to the correct length.
993 //
994 // Returns a valid FileWriter on success, |nullptr| if an error occurred.
995 static std::unique_ptr<FileWriter> Create(int fd, const ZipEntry* entry) {
996 const uint32_t declared_length = entry->uncompressed_length;
997 const off64_t current_offset = lseek64(fd, 0, SEEK_CUR);
998 if (current_offset == -1) {
999 ALOGW("Zip: unable to seek to current location on fd %d: %s", fd, strerror(errno));
1000 return nullptr;
1001 }
1002
1003 int result = 0;
1004#if defined(__linux__)
1005 if (declared_length > 0) {
1006 // Make sure we have enough space on the volume to extract the compressed
1007 // entry. Note that the call to ftruncate below will change the file size but
1008 // will not allocate space on disk and this call to fallocate will not
1009 // change the file size.
1010 result = TEMP_FAILURE_RETRY(fallocate(fd, 0, current_offset, declared_length));
1011 if (result == -1) {
1012 ALOGW("Zip: unable to allocate space for file to %" PRId64 ": %s",
1013 static_cast<int64_t>(declared_length + current_offset), strerror(errno));
1014 return std::unique_ptr<FileWriter>(nullptr);
1015 }
1016 }
1017#endif // __linux__
1018
1019 result = TEMP_FAILURE_RETRY(ftruncate(fd, declared_length + current_offset));
1020 if (result == -1) {
1021 ALOGW("Zip: unable to truncate file to %" PRId64 ": %s",
1022 static_cast<int64_t>(declared_length + current_offset), strerror(errno));
1023 return std::unique_ptr<FileWriter>(nullptr);
1024 }
1025
1026 return std::unique_ptr<FileWriter>(new FileWriter(fd, declared_length));
1027 }
1028
1029 virtual bool Append(uint8_t* buf, size_t buf_size) override {
1030 if (total_bytes_written_ + buf_size > declared_length_) {
1031 ALOGW("Zip: Unexpected size " ZD " (declared) vs " ZD " (actual)",
1032 declared_length_, total_bytes_written_ + buf_size);
1033 return false;
1034 }
1035
1036 // Keep track of the start position so we can calculate the
1037 // total number of bytes written.
1038 const uint8_t* const start = buf;
Narayan Kamathf899bd52015-04-17 11:53:14 +01001039 while (buf_size > 0) {
1040 ssize_t bytes_written = TEMP_FAILURE_RETRY(write(fd_, buf, buf_size));
1041 if (bytes_written == -1) {
1042 ALOGW("Zip: unable to write " ZD " bytes to file; %s", buf_size, strerror(errno));
1043 return false;
1044 }
1045
1046 buf_size -= bytes_written;
1047 buf += bytes_written;
1048 }
1049
1050 total_bytes_written_ += static_cast<size_t>(
1051 reinterpret_cast<uintptr_t>(buf) - reinterpret_cast<uintptr_t>(start));
1052
1053 return true;
1054 }
1055 private:
1056 FileWriter(const int fd, const size_t declared_length) :
1057 Writer(),
1058 fd_(fd),
1059 declared_length_(declared_length),
1060 total_bytes_written_(0) {
1061 }
1062
1063 const int fd_;
1064 const size_t declared_length_;
1065 size_t total_bytes_written_;
1066};
1067
Dmitriy Ivanovf94e1592015-03-06 13:27:59 -08001068// This method is using libz macros with old-style-casts
1069#pragma GCC diagnostic push
1070#pragma GCC diagnostic ignored "-Wold-style-cast"
1071static inline int zlib_inflateInit2(z_stream* stream, int window_bits) {
1072 return inflateInit2(stream, window_bits);
1073}
1074#pragma GCC diagnostic pop
1075
Narayan Kamathf899bd52015-04-17 11:53:14 +01001076static int32_t InflateEntryToWriter(int fd, const ZipEntry* entry,
1077 Writer* writer, uint64_t* crc_out) {
Dmitriy Ivanovedbabfe2015-03-12 09:58:15 -07001078 const size_t kBufSize = 32768;
1079 std::vector<uint8_t> read_buf(kBufSize);
1080 std::vector<uint8_t> write_buf(kBufSize);
Narayan Kamath7462f022013-11-21 13:05:04 +00001081 z_stream zstream;
1082 int zerr;
1083
1084 /*
1085 * Initialize the zlib stream struct.
1086 */
1087 memset(&zstream, 0, sizeof(zstream));
1088 zstream.zalloc = Z_NULL;
1089 zstream.zfree = Z_NULL;
1090 zstream.opaque = Z_NULL;
1091 zstream.next_in = NULL;
1092 zstream.avail_in = 0;
Dmitriy Ivanovedbabfe2015-03-12 09:58:15 -07001093 zstream.next_out = &write_buf[0];
Narayan Kamath7462f022013-11-21 13:05:04 +00001094 zstream.avail_out = kBufSize;
1095 zstream.data_type = Z_UNKNOWN;
1096
1097 /*
1098 * Use the undocumented "negative window bits" feature to tell zlib
1099 * that there's no zlib header waiting for it.
1100 */
Dmitriy Ivanovf94e1592015-03-06 13:27:59 -08001101 zerr = zlib_inflateInit2(&zstream, -MAX_WBITS);
Narayan Kamath7462f022013-11-21 13:05:04 +00001102 if (zerr != Z_OK) {
1103 if (zerr == Z_VERSION_ERROR) {
1104 ALOGE("Installed zlib is not compatible with linked version (%s)",
1105 ZLIB_VERSION);
1106 } else {
1107 ALOGW("Call to inflateInit2 failed (zerr=%d)", zerr);
1108 }
1109
1110 return kZlibError;
1111 }
1112
Dmitriy Ivanov1f741e52015-03-06 14:26:37 -08001113 auto zstream_deleter = [](z_stream* stream) {
1114 inflateEnd(stream); /* free up any allocated structures */
1115 };
1116
1117 std::unique_ptr<z_stream, decltype(zstream_deleter)> zstream_guard(&zstream, zstream_deleter);
1118
Narayan Kamath7462f022013-11-21 13:05:04 +00001119 const uint32_t uncompressed_length = entry->uncompressed_length;
1120
1121 uint32_t compressed_length = entry->compressed_length;
Narayan Kamath7462f022013-11-21 13:05:04 +00001122 do {
1123 /* read as much as we can */
1124 if (zstream.avail_in == 0) {
Mark Salyzyn51d562d2014-05-05 14:38:05 -07001125 const ZD_TYPE getSize = (compressed_length > kBufSize) ? kBufSize : compressed_length;
Dmitriy Ivanovedbabfe2015-03-12 09:58:15 -07001126 const ZD_TYPE actual = TEMP_FAILURE_RETRY(read(fd, &read_buf[0], getSize));
Narayan Kamath7462f022013-11-21 13:05:04 +00001127 if (actual != getSize) {
Mark Salyzyn51d562d2014-05-05 14:38:05 -07001128 ALOGW("Zip: inflate read failed (" ZD " vs " ZD ")", actual, getSize);
Dmitriy Ivanov1f741e52015-03-06 14:26:37 -08001129 return kIoError;
Narayan Kamath7462f022013-11-21 13:05:04 +00001130 }
1131
1132 compressed_length -= getSize;
1133
Dmitriy Ivanovedbabfe2015-03-12 09:58:15 -07001134 zstream.next_in = &read_buf[0];
Narayan Kamath7462f022013-11-21 13:05:04 +00001135 zstream.avail_in = getSize;
1136 }
1137
1138 /* uncompress the data */
1139 zerr = inflate(&zstream, Z_NO_FLUSH);
1140 if (zerr != Z_OK && zerr != Z_STREAM_END) {
1141 ALOGW("Zip: inflate zerr=%d (nIn=%p aIn=%u nOut=%p aOut=%u)",
1142 zerr, zstream.next_in, zstream.avail_in,
1143 zstream.next_out, zstream.avail_out);
Dmitriy Ivanov1f741e52015-03-06 14:26:37 -08001144 return kZlibError;
Narayan Kamath7462f022013-11-21 13:05:04 +00001145 }
1146
1147 /* write when we're full or when we're done */
1148 if (zstream.avail_out == 0 ||
1149 (zerr == Z_STREAM_END && zstream.avail_out != kBufSize)) {
Dmitriy Ivanovedbabfe2015-03-12 09:58:15 -07001150 const size_t write_size = zstream.next_out - &write_buf[0];
Narayan Kamathf899bd52015-04-17 11:53:14 +01001151 if (!writer->Append(&write_buf[0], write_size)) {
1152 // The file might have declared a bogus length.
1153 return kInconsistentInformation;
Narayan Kamath7462f022013-11-21 13:05:04 +00001154 }
Narayan Kamath7462f022013-11-21 13:05:04 +00001155
Dmitriy Ivanovedbabfe2015-03-12 09:58:15 -07001156 zstream.next_out = &write_buf[0];
Narayan Kamath7462f022013-11-21 13:05:04 +00001157 zstream.avail_out = kBufSize;
1158 }
1159 } while (zerr == Z_OK);
1160
1161 assert(zerr == Z_STREAM_END); /* other errors should've been caught */
1162
1163 // stream.adler holds the crc32 value for such streams.
1164 *crc_out = zstream.adler;
1165
1166 if (zstream.total_out != uncompressed_length || compressed_length != 0) {
Mark Salyzyn088bf902014-05-08 16:02:20 -07001167 ALOGW("Zip: size mismatch on inflated file (%lu vs %" PRIu32 ")",
Narayan Kamath7462f022013-11-21 13:05:04 +00001168 zstream.total_out, uncompressed_length);
Dmitriy Ivanov1f741e52015-03-06 14:26:37 -08001169 return kInconsistentInformation;
Narayan Kamath7462f022013-11-21 13:05:04 +00001170 }
1171
Dmitriy Ivanov1f741e52015-03-06 14:26:37 -08001172 return 0;
Narayan Kamath7462f022013-11-21 13:05:04 +00001173}
1174
Narayan Kamathf899bd52015-04-17 11:53:14 +01001175static int32_t CopyEntryToWriter(int fd, const ZipEntry* entry, Writer* writer,
1176 uint64_t *crc_out) {
1177 static const uint32_t kBufSize = 32768;
1178 std::vector<uint8_t> buf(kBufSize);
1179
1180 const uint32_t length = entry->uncompressed_length;
1181 uint32_t count = 0;
1182 uint64_t crc = 0;
1183 while (count < length) {
1184 uint32_t remaining = length - count;
1185
1186 // Safe conversion because kBufSize is narrow enough for a 32 bit signed
1187 // value.
1188 const ssize_t block_size = (remaining > kBufSize) ? kBufSize : remaining;
1189 const ssize_t actual = TEMP_FAILURE_RETRY(read(fd, &buf[0], block_size));
1190
1191 if (actual != block_size) {
1192 ALOGW("CopyFileToFile: copy read failed (" ZD " vs " ZD ")", actual, block_size);
1193 return kIoError;
1194 }
1195
1196 if (!writer->Append(&buf[0], block_size)) {
1197 return kIoError;
1198 }
1199 crc = crc32(crc, &buf[0], block_size);
1200 count += block_size;
1201 }
1202
1203 *crc_out = crc;
1204
1205 return 0;
1206}
1207
1208int32_t ExtractToWriter(ZipArchiveHandle handle,
1209 ZipEntry* entry, Writer* writer) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -08001210 ZipArchive* archive = reinterpret_cast<ZipArchive*>(handle);
Narayan Kamath7462f022013-11-21 13:05:04 +00001211 const uint16_t method = entry->method;
1212 off64_t data_offset = entry->offset;
1213
1214 if (lseek64(archive->fd, data_offset, SEEK_SET) != data_offset) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -08001215 ALOGW("Zip: lseek to data at %" PRId64 " failed", static_cast<int64_t>(data_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +00001216 return kIoError;
1217 }
1218
1219 // this should default to kUnknownCompressionMethod.
1220 int32_t return_value = -1;
1221 uint64_t crc = 0;
1222 if (method == kCompressStored) {
Narayan Kamathf899bd52015-04-17 11:53:14 +01001223 return_value = CopyEntryToWriter(archive->fd, entry, writer, &crc);
Narayan Kamath7462f022013-11-21 13:05:04 +00001224 } else if (method == kCompressDeflated) {
Narayan Kamathf899bd52015-04-17 11:53:14 +01001225 return_value = InflateEntryToWriter(archive->fd, entry, writer, &crc);
Narayan Kamath7462f022013-11-21 13:05:04 +00001226 }
1227
1228 if (!return_value && entry->has_data_descriptor) {
1229 return_value = UpdateEntryFromDataDescriptor(archive->fd, entry);
1230 if (return_value) {
1231 return return_value;
1232 }
1233 }
1234
1235 // TODO: Fix this check by passing the right flags to inflate2 so that
1236 // it calculates the CRC for us.
1237 if (entry->crc32 != crc && false) {
Mark Salyzyn088bf902014-05-08 16:02:20 -07001238 ALOGW("Zip: crc mismatch: expected %" PRIu32 ", was %" PRIu64, entry->crc32, crc);
Narayan Kamath7462f022013-11-21 13:05:04 +00001239 return kInconsistentInformation;
1240 }
1241
1242 return return_value;
1243}
1244
Narayan Kamathf899bd52015-04-17 11:53:14 +01001245int32_t ExtractToMemory(ZipArchiveHandle handle, ZipEntry* entry,
1246 uint8_t* begin, uint32_t size) {
1247 std::unique_ptr<Writer> writer(new MemoryWriter(begin, size));
1248 return ExtractToWriter(handle, entry, writer.get());
1249}
1250
Narayan Kamath7462f022013-11-21 13:05:04 +00001251int32_t ExtractEntryToFile(ZipArchiveHandle handle,
1252 ZipEntry* entry, int fd) {
Narayan Kamathf899bd52015-04-17 11:53:14 +01001253 std::unique_ptr<Writer> writer(FileWriter::Create(fd, entry));
1254 if (writer.get() == nullptr) {
Narayan Kamath7462f022013-11-21 13:05:04 +00001255 return kIoError;
1256 }
1257
Narayan Kamathf899bd52015-04-17 11:53:14 +01001258 return ExtractToWriter(handle, entry, writer.get());
Narayan Kamath7462f022013-11-21 13:05:04 +00001259}
1260
1261const char* ErrorCodeString(int32_t error_code) {
1262 if (error_code > kErrorMessageLowerBound && error_code < kErrorMessageUpperBound) {
1263 return kErrorMessages[error_code * -1];
1264 }
1265
1266 return kErrorMessages[0];
1267}
1268
1269int GetFileDescriptor(const ZipArchiveHandle handle) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -08001270 return reinterpret_cast<ZipArchive*>(handle)->fd;
Narayan Kamath7462f022013-11-21 13:05:04 +00001271}
1272