blob: afc122dc47b84f75ae4668e32aa1785282fb03ac [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;
Narayan Kamatheaf98852013-12-11 14:51:51 +0000296 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),
314 directory_map(NULL),
315 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
324 if (directory_map != NULL) {
325 directory_map->release();
326 }
327 free(hash_table);
328 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000329};
330
331// Returns 0 on success and negative values on failure.
Narayan Kamatheaf98852013-12-11 14:51:51 +0000332static android::FileMap* MapFileSegment(const int fd, const off64_t start,
333 const size_t length, const bool read_only,
334 const char* debug_file_name) {
335 android::FileMap* file_map = new android::FileMap;
336 const bool success = file_map->create(debug_file_name, fd, start, length, read_only);
337 if (!success) {
338 file_map->release();
339 return NULL;
Narayan Kamath7462f022013-11-21 13:05:04 +0000340 }
341
Narayan Kamatheaf98852013-12-11 14:51:51 +0000342 return file_map;
Narayan Kamath7462f022013-11-21 13:05:04 +0000343}
344
345static int32_t CopyFileToFile(int fd, uint8_t* begin, const uint32_t length, uint64_t *crc_out) {
346 static const uint32_t kBufSize = 32768;
347 uint8_t buf[kBufSize];
348
349 uint32_t count = 0;
350 uint64_t crc = 0;
Narayan Kamath58aaf462013-12-10 16:47:14 +0000351 while (count < length) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000352 uint32_t remaining = length - count;
353
354 // Safe conversion because kBufSize is narrow enough for a 32 bit signed
355 // value.
356 ssize_t get_size = (remaining > kBufSize) ? kBufSize : remaining;
357 ssize_t actual = TEMP_FAILURE_RETRY(read(fd, buf, get_size));
358
359 if (actual != get_size) {
Mark Salyzyn51d562d2014-05-05 14:38:05 -0700360 ALOGW("CopyFileToFile: copy read failed (" ZD " vs " ZD ")", actual, get_size);
Narayan Kamath7462f022013-11-21 13:05:04 +0000361 return kIoError;
362 }
363
364 memcpy(begin + count, buf, get_size);
365 crc = crc32(crc, buf, get_size);
366 count += get_size;
367 }
368
369 *crc_out = crc;
370
371 return 0;
372}
373
374/*
375 * Round up to the next highest power of 2.
376 *
377 * Found on http://graphics.stanford.edu/~seander/bithacks.html.
378 */
379static uint32_t RoundUpPower2(uint32_t val) {
380 val--;
381 val |= val >> 1;
382 val |= val >> 2;
383 val |= val >> 4;
384 val |= val >> 8;
385 val |= val >> 16;
386 val++;
387
388 return val;
389}
390
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100391static uint32_t ComputeHash(const ZipEntryName& name) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000392 uint32_t hash = 0;
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100393 uint16_t len = name.name_length;
394 const uint8_t* str = name.name;
Narayan Kamath7462f022013-11-21 13:05:04 +0000395
396 while (len--) {
397 hash = hash * 31 + *str++;
398 }
399
400 return hash;
401}
402
403/*
404 * Convert a ZipEntry to a hash table index, verifying that it's in a
405 * valid range.
406 */
407static int64_t EntryToIndex(const ZipEntryName* hash_table,
408 const uint32_t hash_table_size,
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100409 const ZipEntryName& name) {
410 const uint32_t hash = ComputeHash(name);
Narayan Kamath7462f022013-11-21 13:05:04 +0000411
412 // NOTE: (hash_table_size - 1) is guaranteed to be non-negative.
413 uint32_t ent = hash & (hash_table_size - 1);
414 while (hash_table[ent].name != NULL) {
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100415 if (hash_table[ent].name_length == name.name_length &&
416 memcmp(hash_table[ent].name, name.name, name.name_length) == 0) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000417 return ent;
418 }
419
420 ent = (ent + 1) & (hash_table_size - 1);
421 }
422
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100423 ALOGV("Zip: Unable to find entry %.*s", name.name_length, name.name);
Narayan Kamath7462f022013-11-21 13:05:04 +0000424 return kEntryNotFound;
425}
426
427/*
428 * Add a new entry to the hash table.
429 */
430static int32_t AddToHash(ZipEntryName *hash_table, const uint64_t hash_table_size,
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100431 const ZipEntryName& name) {
432 const uint64_t hash = ComputeHash(name);
Narayan Kamath7462f022013-11-21 13:05:04 +0000433 uint32_t ent = hash & (hash_table_size - 1);
434
435 /*
436 * We over-allocated the table, so we're guaranteed to find an empty slot.
437 * Further, we guarantee that the hashtable size is not 0.
438 */
439 while (hash_table[ent].name != NULL) {
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100440 if (hash_table[ent].name_length == name.name_length &&
441 memcmp(hash_table[ent].name, name.name, name.name_length) == 0) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000442 // We've found a duplicate entry. We don't accept it
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100443 ALOGW("Zip: Found duplicate entry %.*s", name.name_length, name.name);
Narayan Kamath7462f022013-11-21 13:05:04 +0000444 return kDuplicateEntry;
445 }
446 ent = (ent + 1) & (hash_table_size - 1);
447 }
448
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100449 hash_table[ent].name = name.name;
450 hash_table[ent].name_length = name.name_length;
Narayan Kamath7462f022013-11-21 13:05:04 +0000451 return 0;
452}
453
Narayan Kamath7462f022013-11-21 13:05:04 +0000454static int32_t MapCentralDirectory0(int fd, const char* debug_file_name,
455 ZipArchive* archive, off64_t file_length,
Narayan Kamath926973e2014-06-09 14:18:14 +0100456 off64_t read_amount, uint8_t* scan_buffer) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000457 const off64_t search_start = file_length - read_amount;
458
459 if (lseek64(fd, search_start, SEEK_SET) != search_start) {
Narayan Kamath926973e2014-06-09 14:18:14 +0100460 ALOGW("Zip: seek %" PRId64 " failed: %s", static_cast<int64_t>(search_start),
461 strerror(errno));
Narayan Kamath7462f022013-11-21 13:05:04 +0000462 return kIoError;
463 }
Narayan Kamath926973e2014-06-09 14:18:14 +0100464 ssize_t actual = TEMP_FAILURE_RETRY(
465 read(fd, scan_buffer, static_cast<size_t>(read_amount)));
466 if (actual != static_cast<ssize_t>(read_amount)) {
467 ALOGW("Zip: read %" PRId64 " failed: %s", static_cast<int64_t>(read_amount),
468 strerror(errno));
Narayan Kamath7462f022013-11-21 13:05:04 +0000469 return kIoError;
470 }
471
472 /*
473 * Scan backward for the EOCD magic. In an archive without a trailing
474 * comment, we'll find it on the first try. (We may want to consider
475 * doing an initial minimal read; if we don't find it, retry with a
476 * second read as above.)
477 */
Narayan Kamath926973e2014-06-09 14:18:14 +0100478 int i = read_amount - sizeof(EocdRecord);
479 for (; i >= 0; i--) {
480 if (scan_buffer[i] == 0x50 &&
481 ((*reinterpret_cast<uint32_t*>(&scan_buffer[i])) == EocdRecord::kSignature)) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000482 ALOGV("+++ Found EOCD at buf+%d", i);
483 break;
484 }
485 }
486 if (i < 0) {
487 ALOGD("Zip: EOCD not found, %s is not zip", debug_file_name);
488 return kInvalidFile;
489 }
490
491 const off64_t eocd_offset = search_start + i;
Narayan Kamath926973e2014-06-09 14:18:14 +0100492 const EocdRecord* eocd = reinterpret_cast<const EocdRecord*>(scan_buffer + i);
Narayan Kamath7462f022013-11-21 13:05:04 +0000493 /*
Narayan Kamath926973e2014-06-09 14:18:14 +0100494 * Verify that there's no trailing space at the end of the central directory
495 * and its comment.
Narayan Kamath7462f022013-11-21 13:05:04 +0000496 */
Narayan Kamath926973e2014-06-09 14:18:14 +0100497 const off64_t calculated_length = eocd_offset + sizeof(EocdRecord)
498 + eocd->comment_length;
499 if (calculated_length != file_length) {
Narayan Kamath4f6b4992014-06-03 13:59:23 +0100500 ALOGW("Zip: %" PRId64 " extraneous bytes at the end of the central directory",
Narayan Kamath926973e2014-06-09 14:18:14 +0100501 static_cast<int64_t>(file_length - calculated_length));
Narayan Kamath4f6b4992014-06-03 13:59:23 +0100502 return kInvalidFile;
503 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000504
Narayan Kamath926973e2014-06-09 14:18:14 +0100505 /*
506 * Grab the CD offset and size, and the number of entries in the
507 * archive and verify that they look reasonable.
508 */
509 if (eocd->cd_start_offset + eocd->cd_size > eocd_offset) {
510 ALOGW("Zip: bad offsets (dir %" PRIu32 ", size %" PRIu32 ", eocd %" PRId64 ")",
511 eocd->cd_start_offset, eocd->cd_size, static_cast<int64_t>(eocd_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000512 return kInvalidOffset;
513 }
Narayan Kamath926973e2014-06-09 14:18:14 +0100514 if (eocd->num_records == 0) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000515 ALOGW("Zip: empty archive?");
516 return kEmptyArchive;
517 }
518
Narayan Kamath926973e2014-06-09 14:18:14 +0100519 ALOGV("+++ num_entries=%" PRIu32 "dir_size=%" PRIu32 " dir_offset=%" PRIu32,
520 eocd->num_records, eocd->cd_size, eocd->cd_start_offset);
Narayan Kamath7462f022013-11-21 13:05:04 +0000521
522 /*
523 * It all looks good. Create a mapping for the CD, and set the fields
524 * in archive.
525 */
Narayan Kamath926973e2014-06-09 14:18:14 +0100526 android::FileMap* map = MapFileSegment(fd,
527 static_cast<off64_t>(eocd->cd_start_offset),
528 static_cast<size_t>(eocd->cd_size),
529 true /* read only */, debug_file_name);
Narayan Kamatheaf98852013-12-11 14:51:51 +0000530 if (map == NULL) {
531 archive->directory_map = NULL;
532 return kMmapFailed;
Narayan Kamath7462f022013-11-21 13:05:04 +0000533 }
534
Narayan Kamatheaf98852013-12-11 14:51:51 +0000535 archive->directory_map = map;
Narayan Kamath926973e2014-06-09 14:18:14 +0100536 archive->num_entries = eocd->num_records;
537 archive->directory_offset = eocd->cd_start_offset;
Narayan Kamath7462f022013-11-21 13:05:04 +0000538
539 return 0;
540}
541
542/*
543 * Find the zip Central Directory and memory-map it.
544 *
545 * On success, returns 0 after populating fields from the EOCD area:
546 * directory_offset
547 * directory_map
548 * num_entries
549 */
550static int32_t MapCentralDirectory(int fd, const char* debug_file_name,
551 ZipArchive* archive) {
552
553 // Test file length. We use lseek64 to make sure the file
554 // is small enough to be a zip file (Its size must be less than
555 // 0xffffffff bytes).
556 off64_t file_length = lseek64(fd, 0, SEEK_END);
557 if (file_length == -1) {
558 ALOGV("Zip: lseek on fd %d failed", fd);
559 return kInvalidFile;
560 }
561
562 if (file_length > (off64_t) 0xffffffff) {
Narayan Kamath926973e2014-06-09 14:18:14 +0100563 ALOGV("Zip: zip file too long %" PRId64, static_cast<int64_t>(file_length));
Narayan Kamath7462f022013-11-21 13:05:04 +0000564 return kInvalidFile;
565 }
566
Narayan Kamath926973e2014-06-09 14:18:14 +0100567 if (file_length < static_cast<off64_t>(sizeof(EocdRecord))) {
568 ALOGV("Zip: length %" PRId64 " is too small to be zip", static_cast<int64_t>(file_length));
Narayan Kamath7462f022013-11-21 13:05:04 +0000569 return kInvalidFile;
570 }
571
572 /*
573 * Perform the traditional EOCD snipe hunt.
574 *
575 * We're searching for the End of Central Directory magic number,
576 * which appears at the start of the EOCD block. It's followed by
577 * 18 bytes of EOCD stuff and up to 64KB of archive comment. We
578 * need to read the last part of the file into a buffer, dig through
579 * it to find the magic number, parse some values out, and use those
580 * to determine the extent of the CD.
581 *
582 * We start by pulling in the last part of the file.
583 */
Narayan Kamath926973e2014-06-09 14:18:14 +0100584 off64_t read_amount = kMaxEOCDSearch;
585 if (file_length < read_amount) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000586 read_amount = file_length;
587 }
588
Narayan Kamath926973e2014-06-09 14:18:14 +0100589 uint8_t* scan_buffer = reinterpret_cast<uint8_t*>(malloc(read_amount));
Narayan Kamath7462f022013-11-21 13:05:04 +0000590 int32_t result = MapCentralDirectory0(fd, debug_file_name, archive,
591 file_length, read_amount, scan_buffer);
592
593 free(scan_buffer);
594 return result;
595}
596
597/*
598 * Parses the Zip archive's Central Directory. Allocates and populates the
599 * hash table.
600 *
601 * Returns 0 on success.
602 */
603static int32_t ParseZipArchive(ZipArchive* archive) {
604 int32_t result = -1;
Narayan Kamath926973e2014-06-09 14:18:14 +0100605 const uint8_t* const cd_ptr = (const uint8_t*) archive->directory_map->getDataPtr();
606 const size_t cd_length = archive->directory_map->getDataLength();
607 const uint16_t num_entries = archive->num_entries;
Narayan Kamath7462f022013-11-21 13:05:04 +0000608
609 /*
610 * Create hash table. We have a minimum 75% load factor, possibly as
611 * low as 50% after we round off to a power of 2. There must be at
612 * least one unused entry to avoid an infinite loop during creation.
613 */
614 archive->hash_table_size = RoundUpPower2(1 + (num_entries * 4) / 3);
615 archive->hash_table = (ZipEntryName*) calloc(archive->hash_table_size,
616 sizeof(ZipEntryName));
617
618 /*
619 * Walk through the central directory, adding entries to the hash
620 * table and verifying values.
621 */
Narayan Kamath926973e2014-06-09 14:18:14 +0100622 const uint8_t* const cd_end = cd_ptr + cd_length;
Narayan Kamath7462f022013-11-21 13:05:04 +0000623 const uint8_t* ptr = cd_ptr;
624 for (uint16_t i = 0; i < num_entries; i++) {
Narayan Kamath926973e2014-06-09 14:18:14 +0100625 const CentralDirectoryRecord* cdr =
626 reinterpret_cast<const CentralDirectoryRecord*>(ptr);
627 if (cdr->record_signature != CentralDirectoryRecord::kSignature) {
Mark Salyzyn088bf902014-05-08 16:02:20 -0700628 ALOGW("Zip: missed a central dir sig (at %" PRIu16 ")", i);
Narayan Kamath7462f022013-11-21 13:05:04 +0000629 goto bail;
630 }
631
Narayan Kamath926973e2014-06-09 14:18:14 +0100632 if (ptr + sizeof(CentralDirectoryRecord) > cd_end) {
Mark Salyzyn088bf902014-05-08 16:02:20 -0700633 ALOGW("Zip: ran off the end (at %" PRIu16 ")", i);
Narayan Kamath7462f022013-11-21 13:05:04 +0000634 goto bail;
635 }
636
Narayan Kamath926973e2014-06-09 14:18:14 +0100637 const off64_t local_header_offset = cdr->local_file_header_offset;
Narayan Kamath7462f022013-11-21 13:05:04 +0000638 if (local_header_offset >= archive->directory_offset) {
Mark Salyzyn56a90a02014-05-08 17:20:55 -0700639 ALOGW("Zip: bad LFH offset %" PRId64 " at entry %" PRIu16, (int64_t)local_header_offset, i);
Narayan Kamath7462f022013-11-21 13:05:04 +0000640 goto bail;
641 }
642
Narayan Kamath926973e2014-06-09 14:18:14 +0100643 const uint16_t file_name_length = cdr->file_name_length;
644 const uint16_t extra_length = cdr->extra_field_length;
645 const uint16_t comment_length = cdr->comment_length;
Piotr Jastrzebski78271ba2014-08-15 12:53:00 +0100646 const uint8_t* file_name = ptr + sizeof(CentralDirectoryRecord);
647
Narayan Kamath044bc8e2014-12-03 18:22:53 +0000648 /* check that file name is valid UTF-8 and doesn't contain NUL (U+0000) characters */
649 if (!IsValidEntryName(file_name, file_name_length)) {
Piotr Jastrzebski78271ba2014-08-15 12:53:00 +0100650 goto bail;
651 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000652
653 /* add the CDE filename to the hash table */
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100654 ZipEntryName entry_name;
655 entry_name.name = file_name;
656 entry_name.name_length = file_name_length;
Narayan Kamath7462f022013-11-21 13:05:04 +0000657 const int add_result = AddToHash(archive->hash_table,
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100658 archive->hash_table_size, entry_name);
Narayan Kamath7462f022013-11-21 13:05:04 +0000659 if (add_result) {
660 ALOGW("Zip: Error adding entry to hash table %d", add_result);
661 result = add_result;
662 goto bail;
663 }
664
Narayan Kamath926973e2014-06-09 14:18:14 +0100665 ptr += sizeof(CentralDirectoryRecord) + file_name_length + extra_length + comment_length;
666 if ((ptr - cd_ptr) > static_cast<int64_t>(cd_length)) {
Mark Salyzyn088bf902014-05-08 16:02:20 -0700667 ALOGW("Zip: bad CD advance (%tu vs %zu) at entry %" PRIu16,
668 ptr - cd_ptr, cd_length, i);
Narayan Kamath7462f022013-11-21 13:05:04 +0000669 goto bail;
670 }
671 }
Mark Salyzyn088bf902014-05-08 16:02:20 -0700672 ALOGV("+++ zip good scan %" PRIu16 " entries", num_entries);
Narayan Kamath7462f022013-11-21 13:05:04 +0000673
674 result = 0;
675
676bail:
677 return result;
678}
679
680static int32_t OpenArchiveInternal(ZipArchive* archive,
681 const char* debug_file_name) {
682 int32_t result = -1;
683 if ((result = MapCentralDirectory(archive->fd, debug_file_name, archive))) {
684 return result;
685 }
686
687 if ((result = ParseZipArchive(archive))) {
688 return result;
689 }
690
691 return 0;
692}
693
694int32_t OpenArchiveFd(int fd, const char* debug_file_name,
Dmitriy Ivanov40b52b22014-07-15 19:33:00 -0700695 ZipArchiveHandle* handle, bool assume_ownership) {
696 ZipArchive* archive = new ZipArchive(fd, assume_ownership);
Narayan Kamath7462f022013-11-21 13:05:04 +0000697 *handle = archive;
Narayan Kamath7462f022013-11-21 13:05:04 +0000698 return OpenArchiveInternal(archive, debug_file_name);
699}
700
701int32_t OpenArchive(const char* fileName, ZipArchiveHandle* handle) {
Neil Fullerb1a113f2014-07-25 14:43:04 +0100702 const int fd = open(fileName, O_RDONLY | O_BINARY, 0);
Dmitriy Ivanov40b52b22014-07-15 19:33:00 -0700703 ZipArchive* archive = new ZipArchive(fd, true);
Narayan Kamath7462f022013-11-21 13:05:04 +0000704 *handle = archive;
705
Narayan Kamath7462f022013-11-21 13:05:04 +0000706 if (fd < 0) {
707 ALOGW("Unable to open '%s': %s", fileName, strerror(errno));
708 return kIoError;
Narayan Kamath7462f022013-11-21 13:05:04 +0000709 }
Dmitriy Ivanov40b52b22014-07-15 19:33:00 -0700710
Narayan Kamath7462f022013-11-21 13:05:04 +0000711 return OpenArchiveInternal(archive, fileName);
712}
713
714/*
715 * Close a ZipArchive, closing the file and freeing the contents.
716 */
717void CloseArchive(ZipArchiveHandle handle) {
718 ZipArchive* archive = (ZipArchive*) handle;
719 ALOGV("Closing archive %p", archive);
Neil Fullerb1a113f2014-07-25 14:43:04 +0100720 delete archive;
Narayan Kamath7462f022013-11-21 13:05:04 +0000721}
722
723static int32_t UpdateEntryFromDataDescriptor(int fd,
724 ZipEntry *entry) {
Narayan Kamath926973e2014-06-09 14:18:14 +0100725 uint8_t ddBuf[sizeof(DataDescriptor) + sizeof(DataDescriptor::kOptSignature)];
Narayan Kamath7462f022013-11-21 13:05:04 +0000726 ssize_t actual = TEMP_FAILURE_RETRY(read(fd, ddBuf, sizeof(ddBuf)));
727 if (actual != sizeof(ddBuf)) {
728 return kIoError;
729 }
730
Narayan Kamath926973e2014-06-09 14:18:14 +0100731 const uint32_t ddSignature = *(reinterpret_cast<const uint32_t*>(ddBuf));
732 const uint16_t offset = (ddSignature == DataDescriptor::kOptSignature) ? 4 : 0;
733 const DataDescriptor* descriptor = reinterpret_cast<const DataDescriptor*>(ddBuf + offset);
Narayan Kamath7462f022013-11-21 13:05:04 +0000734
Narayan Kamath926973e2014-06-09 14:18:14 +0100735 entry->crc32 = descriptor->crc32;
736 entry->compressed_length = descriptor->compressed_size;
737 entry->uncompressed_length = descriptor->uncompressed_size;
Narayan Kamath7462f022013-11-21 13:05:04 +0000738
739 return 0;
740}
741
742// Attempts to read |len| bytes into |buf| at offset |off|.
743//
744// This method uses pread64 on platforms that support it and
745// lseek64 + read on platforms that don't. This implies that
746// callers should not rely on the |fd| offset being incremented
747// as a side effect of this call.
748static inline ssize_t ReadAtOffset(int fd, uint8_t* buf, size_t len,
749 off64_t off) {
Yabin Cui70160f42014-11-19 20:47:18 -0800750#if !defined(_WIN32)
Narayan Kamath7462f022013-11-21 13:05:04 +0000751 return TEMP_FAILURE_RETRY(pread64(fd, buf, len, off));
752#else
753 // The only supported platform that doesn't support pread at the moment
754 // is Windows. Only recent versions of windows support unix like forks,
755 // and even there the semantics are quite different.
756 if (lseek64(fd, off, SEEK_SET) != off) {
Mark Salyzyn99ef9912014-03-14 14:26:22 -0700757 ALOGW("Zip: failed seek to offset %" PRId64, off);
Narayan Kamath7462f022013-11-21 13:05:04 +0000758 return kIoError;
759 }
760
761 return TEMP_FAILURE_RETRY(read(fd, buf, len));
Yabin Cui70160f42014-11-19 20:47:18 -0800762#endif
Narayan Kamath7462f022013-11-21 13:05:04 +0000763}
764
765static int32_t FindEntry(const ZipArchive* archive, const int ent,
766 ZipEntry* data) {
767 const uint16_t nameLen = archive->hash_table[ent].name_length;
Narayan Kamath7462f022013-11-21 13:05:04 +0000768
769 // Recover the start of the central directory entry from the filename
770 // pointer. The filename is the first entry past the fixed-size data,
771 // so we can just subtract back from that.
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100772 const uint8_t* ptr = archive->hash_table[ent].name;
Narayan Kamath926973e2014-06-09 14:18:14 +0100773 ptr -= sizeof(CentralDirectoryRecord);
Narayan Kamath7462f022013-11-21 13:05:04 +0000774
775 // This is the base of our mmapped region, we have to sanity check that
776 // the name that's in the hash table is a pointer to a location within
777 // this mapped region.
Narayan Kamath926973e2014-06-09 14:18:14 +0100778 const uint8_t* base_ptr = reinterpret_cast<const uint8_t*>(
779 archive->directory_map->getDataPtr());
Narayan Kamatheaf98852013-12-11 14:51:51 +0000780 if (ptr < base_ptr || ptr > base_ptr + archive->directory_map->getDataLength()) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000781 ALOGW("Zip: Invalid entry pointer");
782 return kInvalidOffset;
783 }
784
Narayan Kamath926973e2014-06-09 14:18:14 +0100785 const CentralDirectoryRecord *cdr =
786 reinterpret_cast<const CentralDirectoryRecord*>(ptr);
787
Narayan Kamath7462f022013-11-21 13:05:04 +0000788 // The offset of the start of the central directory in the zipfile.
789 // We keep this lying around so that we can sanity check all our lengths
790 // and our per-file structures.
791 const off64_t cd_offset = archive->directory_offset;
792
793 // Fill out the compression method, modification time, crc32
794 // and other interesting attributes from the central directory. These
795 // will later be compared against values from the local file header.
Narayan Kamath926973e2014-06-09 14:18:14 +0100796 data->method = cdr->compression_method;
797 data->mod_time = cdr->last_mod_time;
798 data->crc32 = cdr->crc32;
799 data->compressed_length = cdr->compressed_size;
800 data->uncompressed_length = cdr->uncompressed_size;
Narayan Kamath7462f022013-11-21 13:05:04 +0000801
802 // Figure out the local header offset from the central directory. The
803 // actual file data will begin after the local header and the name /
804 // extra comments.
Narayan Kamath926973e2014-06-09 14:18:14 +0100805 const off64_t local_header_offset = cdr->local_file_header_offset;
806 if (local_header_offset + static_cast<off64_t>(sizeof(LocalFileHeader)) >= cd_offset) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000807 ALOGW("Zip: bad local hdr offset in zip");
808 return kInvalidOffset;
809 }
810
Narayan Kamath926973e2014-06-09 14:18:14 +0100811 uint8_t lfh_buf[sizeof(LocalFileHeader)];
Narayan Kamath7462f022013-11-21 13:05:04 +0000812 ssize_t actual = ReadAtOffset(archive->fd, lfh_buf, sizeof(lfh_buf),
813 local_header_offset);
814 if (actual != sizeof(lfh_buf)) {
Mark Salyzyn56a90a02014-05-08 17:20:55 -0700815 ALOGW("Zip: failed reading lfh name from offset %" PRId64, (int64_t)local_header_offset);
Narayan Kamath7462f022013-11-21 13:05:04 +0000816 return kIoError;
817 }
818
Narayan Kamath926973e2014-06-09 14:18:14 +0100819 const LocalFileHeader *lfh = reinterpret_cast<const LocalFileHeader*>(lfh_buf);
820
821 if (lfh->lfh_signature != LocalFileHeader::kSignature) {
Mark Salyzyn99ef9912014-03-14 14:26:22 -0700822 ALOGW("Zip: didn't find signature at start of lfh, offset=%" PRId64,
Narayan Kamath926973e2014-06-09 14:18:14 +0100823 static_cast<int64_t>(local_header_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000824 return kInvalidOffset;
825 }
826
827 // Paranoia: Match the values specified in the local file header
828 // to those specified in the central directory.
Narayan Kamath926973e2014-06-09 14:18:14 +0100829 if ((lfh->gpb_flags & kGPBDDFlagMask) == 0) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000830 data->has_data_descriptor = 0;
Narayan Kamath926973e2014-06-09 14:18:14 +0100831 if (data->compressed_length != lfh->compressed_size
832 || data->uncompressed_length != lfh->uncompressed_size
833 || data->crc32 != lfh->crc32) {
Mark Salyzyn088bf902014-05-08 16:02:20 -0700834 ALOGW("Zip: size/crc32 mismatch. expected {%" PRIu32 ", %" PRIu32
835 ", %" PRIx32 "}, was {%" PRIu32 ", %" PRIu32 ", %" PRIx32 "}",
Narayan Kamath7462f022013-11-21 13:05:04 +0000836 data->compressed_length, data->uncompressed_length, data->crc32,
Narayan Kamath926973e2014-06-09 14:18:14 +0100837 lfh->compressed_size, lfh->uncompressed_size, lfh->crc32);
Narayan Kamath7462f022013-11-21 13:05:04 +0000838 return kInconsistentInformation;
839 }
840 } else {
841 data->has_data_descriptor = 1;
842 }
843
844 // Check that the local file header name matches the declared
845 // name in the central directory.
Narayan Kamath926973e2014-06-09 14:18:14 +0100846 if (lfh->file_name_length == nameLen) {
847 const off64_t name_offset = local_header_offset + sizeof(LocalFileHeader);
848 if (name_offset + lfh->file_name_length >= cd_offset) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000849 ALOGW("Zip: Invalid declared length");
850 return kInvalidOffset;
851 }
852
853 uint8_t* name_buf = (uint8_t*) malloc(nameLen);
854 ssize_t actual = ReadAtOffset(archive->fd, name_buf, nameLen,
855 name_offset);
856
857 if (actual != nameLen) {
Mark Salyzyn56a90a02014-05-08 17:20:55 -0700858 ALOGW("Zip: failed reading lfh name from offset %" PRId64, (int64_t)name_offset);
Narayan Kamath7462f022013-11-21 13:05:04 +0000859 free(name_buf);
860 return kIoError;
861 }
862
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100863 if (memcmp(archive->hash_table[ent].name, name_buf, nameLen)) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000864 free(name_buf);
865 return kInconsistentInformation;
866 }
867
868 free(name_buf);
869 } else {
870 ALOGW("Zip: lfh name did not match central directory.");
871 return kInconsistentInformation;
872 }
873
Narayan Kamath926973e2014-06-09 14:18:14 +0100874 const off64_t data_offset = local_header_offset + sizeof(LocalFileHeader)
875 + lfh->file_name_length + lfh->extra_field_length;
Narayan Kamath48953a12014-01-24 12:32:39 +0000876 if (data_offset > cd_offset) {
Mark Salyzyn56a90a02014-05-08 17:20:55 -0700877 ALOGW("Zip: bad data offset %" PRId64 " in zip", (int64_t)data_offset);
Narayan Kamath7462f022013-11-21 13:05:04 +0000878 return kInvalidOffset;
879 }
880
881 if ((off64_t)(data_offset + data->compressed_length) > cd_offset) {
Mark Salyzyn088bf902014-05-08 16:02:20 -0700882 ALOGW("Zip: bad compressed length in zip (%" PRId64 " + %" PRIu32 " > %" PRId64 ")",
Mark Salyzyn56a90a02014-05-08 17:20:55 -0700883 (int64_t)data_offset, data->compressed_length, (int64_t)cd_offset);
Narayan Kamath7462f022013-11-21 13:05:04 +0000884 return kInvalidOffset;
885 }
886
887 if (data->method == kCompressStored &&
888 (off64_t)(data_offset + data->uncompressed_length) > cd_offset) {
Mark Salyzyn088bf902014-05-08 16:02:20 -0700889 ALOGW("Zip: bad uncompressed length in zip (%" PRId64 " + %" PRIu32 " > %" PRId64 ")",
Mark Salyzyn96c5c992014-05-08 19:16:40 -0700890 (int64_t)data_offset, data->uncompressed_length, (int64_t)cd_offset);
Narayan Kamath7462f022013-11-21 13:05:04 +0000891 return kInvalidOffset;
892 }
893
894 data->offset = data_offset;
895 return 0;
896}
897
898struct IterationHandle {
899 uint32_t position;
Piotr Jastrzebski10aa9a02014-08-19 09:01:20 +0100900 // We're not using vector here because this code is used in the Windows SDK
901 // where the STL is not available.
Piotr Jastrzebski8e085362014-08-18 11:37:45 +0100902 const uint8_t* prefix;
903 uint16_t prefix_len;
Narayan Kamath7462f022013-11-21 13:05:04 +0000904 ZipArchive* archive;
Piotr Jastrzebski8e085362014-08-18 11:37:45 +0100905
906 IterationHandle() : prefix(NULL), prefix_len(0) {}
907
908 IterationHandle(const ZipEntryName& prefix_name)
909 : prefix_len(prefix_name.name_length) {
910 uint8_t* prefix_copy = new uint8_t[prefix_len];
Piotr Jastrzebski10aa9a02014-08-19 09:01:20 +0100911 memcpy(prefix_copy, prefix_name.name, prefix_len);
Piotr Jastrzebski8e085362014-08-18 11:37:45 +0100912 prefix = prefix_copy;
913 }
914
915 ~IterationHandle() {
Piotr Jastrzebski10aa9a02014-08-19 09:01:20 +0100916 delete[] prefix;
Piotr Jastrzebski8e085362014-08-18 11:37:45 +0100917 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000918};
919
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100920int32_t StartIteration(ZipArchiveHandle handle, void** cookie_ptr,
921 const ZipEntryName* optional_prefix) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000922 ZipArchive* archive = (ZipArchive *) handle;
923
924 if (archive == NULL || archive->hash_table == NULL) {
925 ALOGW("Zip: Invalid ZipArchiveHandle");
926 return kInvalidHandle;
927 }
928
Piotr Jastrzebski8e085362014-08-18 11:37:45 +0100929 IterationHandle* cookie =
930 optional_prefix != NULL ? new IterationHandle(*optional_prefix) : new IterationHandle();
Narayan Kamath7462f022013-11-21 13:05:04 +0000931 cookie->position = 0;
Narayan Kamath7462f022013-11-21 13:05:04 +0000932 cookie->archive = archive;
Narayan Kamath7462f022013-11-21 13:05:04 +0000933
934 *cookie_ptr = cookie ;
935 return 0;
936}
937
Piotr Jastrzebski79c8b342014-08-08 14:02:17 +0100938void EndIteration(void* cookie) {
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100939 delete reinterpret_cast<IterationHandle*>(cookie);
Piotr Jastrzebski79c8b342014-08-08 14:02:17 +0100940}
941
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100942int32_t FindEntry(const ZipArchiveHandle handle, const ZipEntryName& entryName,
Narayan Kamath7462f022013-11-21 13:05:04 +0000943 ZipEntry* data) {
944 const ZipArchive* archive = (ZipArchive*) handle;
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100945 if (entryName.name_length == 0) {
946 ALOGW("Zip: Invalid filename %.*s", entryName.name_length, entryName.name);
Narayan Kamath7462f022013-11-21 13:05:04 +0000947 return kInvalidEntryName;
948 }
949
950 const int64_t ent = EntryToIndex(archive->hash_table,
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100951 archive->hash_table_size, entryName);
Narayan Kamath7462f022013-11-21 13:05:04 +0000952
953 if (ent < 0) {
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100954 ALOGV("Zip: Could not find entry %.*s", entryName.name_length, entryName.name);
Narayan Kamath7462f022013-11-21 13:05:04 +0000955 return ent;
956 }
957
958 return FindEntry(archive, ent, data);
959}
960
961int32_t Next(void* cookie, ZipEntry* data, ZipEntryName* name) {
962 IterationHandle* handle = (IterationHandle *) cookie;
963 if (handle == NULL) {
964 return kInvalidHandle;
965 }
966
967 ZipArchive* archive = handle->archive;
968 if (archive == NULL || archive->hash_table == NULL) {
969 ALOGW("Zip: Invalid ZipArchiveHandle");
970 return kInvalidHandle;
971 }
972
973 const uint32_t currentOffset = handle->position;
974 const uint32_t hash_table_length = archive->hash_table_size;
975 const ZipEntryName *hash_table = archive->hash_table;
976
977 for (uint32_t i = currentOffset; i < hash_table_length; ++i) {
978 if (hash_table[i].name != NULL &&
Piotr Jastrzebski8e085362014-08-18 11:37:45 +0100979 (handle->prefix_len == 0 ||
980 (memcmp(handle->prefix, hash_table[i].name, handle->prefix_len) == 0))) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000981 handle->position = (i + 1);
982 const int error = FindEntry(archive, i, data);
983 if (!error) {
984 name->name = hash_table[i].name;
985 name->name_length = hash_table[i].name_length;
986 }
987
988 return error;
989 }
990 }
991
992 handle->position = 0;
993 return kIterationEnd;
994}
995
996static int32_t InflateToFile(int fd, const ZipEntry* entry,
997 uint8_t* begin, uint32_t length,
998 uint64_t* crc_out) {
999 int32_t result = -1;
1000 const uint32_t kBufSize = 32768;
1001 uint8_t read_buf[kBufSize];
1002 uint8_t write_buf[kBufSize];
1003 z_stream zstream;
1004 int zerr;
1005
1006 /*
1007 * Initialize the zlib stream struct.
1008 */
1009 memset(&zstream, 0, sizeof(zstream));
1010 zstream.zalloc = Z_NULL;
1011 zstream.zfree = Z_NULL;
1012 zstream.opaque = Z_NULL;
1013 zstream.next_in = NULL;
1014 zstream.avail_in = 0;
1015 zstream.next_out = (Bytef*) write_buf;
1016 zstream.avail_out = kBufSize;
1017 zstream.data_type = Z_UNKNOWN;
1018
1019 /*
1020 * Use the undocumented "negative window bits" feature to tell zlib
1021 * that there's no zlib header waiting for it.
1022 */
1023 zerr = inflateInit2(&zstream, -MAX_WBITS);
1024 if (zerr != Z_OK) {
1025 if (zerr == Z_VERSION_ERROR) {
1026 ALOGE("Installed zlib is not compatible with linked version (%s)",
1027 ZLIB_VERSION);
1028 } else {
1029 ALOGW("Call to inflateInit2 failed (zerr=%d)", zerr);
1030 }
1031
1032 return kZlibError;
1033 }
1034
1035 const uint32_t uncompressed_length = entry->uncompressed_length;
1036
1037 uint32_t compressed_length = entry->compressed_length;
1038 uint32_t write_count = 0;
1039 do {
1040 /* read as much as we can */
1041 if (zstream.avail_in == 0) {
Mark Salyzyn51d562d2014-05-05 14:38:05 -07001042 const ZD_TYPE getSize = (compressed_length > kBufSize) ? kBufSize : compressed_length;
1043 const ZD_TYPE actual = TEMP_FAILURE_RETRY(read(fd, read_buf, getSize));
Narayan Kamath7462f022013-11-21 13:05:04 +00001044 if (actual != getSize) {
Mark Salyzyn51d562d2014-05-05 14:38:05 -07001045 ALOGW("Zip: inflate read failed (" ZD " vs " ZD ")", actual, getSize);
Narayan Kamath7462f022013-11-21 13:05:04 +00001046 result = kIoError;
1047 goto z_bail;
1048 }
1049
1050 compressed_length -= getSize;
1051
1052 zstream.next_in = read_buf;
1053 zstream.avail_in = getSize;
1054 }
1055
1056 /* uncompress the data */
1057 zerr = inflate(&zstream, Z_NO_FLUSH);
1058 if (zerr != Z_OK && zerr != Z_STREAM_END) {
1059 ALOGW("Zip: inflate zerr=%d (nIn=%p aIn=%u nOut=%p aOut=%u)",
1060 zerr, zstream.next_in, zstream.avail_in,
1061 zstream.next_out, zstream.avail_out);
1062 result = kZlibError;
1063 goto z_bail;
1064 }
1065
1066 /* write when we're full or when we're done */
1067 if (zstream.avail_out == 0 ||
1068 (zerr == Z_STREAM_END && zstream.avail_out != kBufSize)) {
1069 const size_t write_size = zstream.next_out - write_buf;
1070 // The file might have declared a bogus length.
1071 if (write_size + write_count > length) {
1072 goto z_bail;
1073 }
1074 memcpy(begin + write_count, write_buf, write_size);
1075 write_count += write_size;
1076
1077 zstream.next_out = write_buf;
1078 zstream.avail_out = kBufSize;
1079 }
1080 } while (zerr == Z_OK);
1081
1082 assert(zerr == Z_STREAM_END); /* other errors should've been caught */
1083
1084 // stream.adler holds the crc32 value for such streams.
1085 *crc_out = zstream.adler;
1086
1087 if (zstream.total_out != uncompressed_length || compressed_length != 0) {
Mark Salyzyn088bf902014-05-08 16:02:20 -07001088 ALOGW("Zip: size mismatch on inflated file (%lu vs %" PRIu32 ")",
Narayan Kamath7462f022013-11-21 13:05:04 +00001089 zstream.total_out, uncompressed_length);
1090 result = kInconsistentInformation;
1091 goto z_bail;
1092 }
1093
1094 result = 0;
1095
1096z_bail:
1097 inflateEnd(&zstream); /* free up any allocated structures */
1098
1099 return result;
1100}
1101
1102int32_t ExtractToMemory(ZipArchiveHandle handle,
1103 ZipEntry* entry, uint8_t* begin, uint32_t size) {
1104 ZipArchive* archive = (ZipArchive*) handle;
1105 const uint16_t method = entry->method;
1106 off64_t data_offset = entry->offset;
1107
1108 if (lseek64(archive->fd, data_offset, SEEK_SET) != data_offset) {
Mark Salyzyn56a90a02014-05-08 17:20:55 -07001109 ALOGW("Zip: lseek to data at %" PRId64 " failed", (int64_t)data_offset);
Narayan Kamath7462f022013-11-21 13:05:04 +00001110 return kIoError;
1111 }
1112
1113 // this should default to kUnknownCompressionMethod.
1114 int32_t return_value = -1;
1115 uint64_t crc = 0;
1116 if (method == kCompressStored) {
1117 return_value = CopyFileToFile(archive->fd, begin, size, &crc);
1118 } else if (method == kCompressDeflated) {
1119 return_value = InflateToFile(archive->fd, entry, begin, size, &crc);
1120 }
1121
1122 if (!return_value && entry->has_data_descriptor) {
1123 return_value = UpdateEntryFromDataDescriptor(archive->fd, entry);
1124 if (return_value) {
1125 return return_value;
1126 }
1127 }
1128
1129 // TODO: Fix this check by passing the right flags to inflate2 so that
1130 // it calculates the CRC for us.
1131 if (entry->crc32 != crc && false) {
Mark Salyzyn088bf902014-05-08 16:02:20 -07001132 ALOGW("Zip: crc mismatch: expected %" PRIu32 ", was %" PRIu64, entry->crc32, crc);
Narayan Kamath7462f022013-11-21 13:05:04 +00001133 return kInconsistentInformation;
1134 }
1135
1136 return return_value;
1137}
1138
1139int32_t ExtractEntryToFile(ZipArchiveHandle handle,
1140 ZipEntry* entry, int fd) {
1141 const int32_t declared_length = entry->uncompressed_length;
1142
Narayan Kamath00a258c2013-12-13 16:06:19 +00001143 const off64_t current_offset = lseek64(fd, 0, SEEK_CUR);
1144 if (current_offset == -1) {
1145 ALOGW("Zip: unable to seek to current location on fd %d: %s", fd,
1146 strerror(errno));
Narayan Kamath7462f022013-11-21 13:05:04 +00001147 return kIoError;
1148 }
1149
Narayan Kamath00a258c2013-12-13 16:06:19 +00001150 int result = TEMP_FAILURE_RETRY(ftruncate(fd, declared_length + current_offset));
1151 if (result == -1) {
Mark Salyzyn99ef9912014-03-14 14:26:22 -07001152 ALOGW("Zip: unable to truncate file to %" PRId64 ": %s",
Mark Salyzyn56a90a02014-05-08 17:20:55 -07001153 (int64_t)(declared_length + current_offset), strerror(errno));
Narayan Kamath00a258c2013-12-13 16:06:19 +00001154 return kIoError;
1155 }
1156
Narayan Kamath48953a12014-01-24 12:32:39 +00001157 // Don't attempt to map a region of length 0. We still need the
1158 // ftruncate() though, since the API guarantees that we will truncate
1159 // the file to the end of the uncompressed output.
1160 if (declared_length == 0) {
1161 return 0;
1162 }
1163
Narayan Kamath00a258c2013-12-13 16:06:19 +00001164 android::FileMap* map = MapFileSegment(fd, current_offset, declared_length,
Narayan Kamatheaf98852013-12-11 14:51:51 +00001165 false, kTempMappingFileName);
1166 if (map == NULL) {
1167 return kMmapFailed;
Narayan Kamath7462f022013-11-21 13:05:04 +00001168 }
1169
Narayan Kamatheaf98852013-12-11 14:51:51 +00001170 const int32_t error = ExtractToMemory(handle, entry,
1171 reinterpret_cast<uint8_t*>(map->getDataPtr()),
1172 map->getDataLength());
1173 map->release();
Narayan Kamath7462f022013-11-21 13:05:04 +00001174 return error;
1175}
1176
1177const char* ErrorCodeString(int32_t error_code) {
1178 if (error_code > kErrorMessageLowerBound && error_code < kErrorMessageUpperBound) {
1179 return kErrorMessages[error_code * -1];
1180 }
1181
1182 return kErrorMessages[0];
1183}
1184
1185int GetFileDescriptor(const ZipArchiveHandle handle) {
1186 return ((ZipArchive*) handle)->fd;
1187}
1188