blob: f1e13a7bc2e84b243acc688e4023207efb5dfcaf [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
Narayan Kamathe97e66e2015-04-27 16:25:53 +010033#include "base/file.h"
Dan Albert1ae07642015-04-09 14:11:18 -070034#include "base/macros.h" // TEMP_FAILURE_RETRY may or may not be in unistd
35#include "base/memory.h"
36#include "log/log.h"
37#include "utils/Compat.h"
38#include "utils/FileMap.h"
39#include "zlib.h"
Narayan Kamath7462f022013-11-21 13:05:04 +000040
Narayan Kamath044bc8e2014-12-03 18:22:53 +000041#include "entry_name_utils-inl.h"
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -070042#include "zip_archive_common.h"
Mark Salyzyn99ef9912014-03-14 14:26:22 -070043#include "ziparchive/zip_archive.h"
44
Dan Albert1ae07642015-04-09 14:11:18 -070045using android::base::get_unaligned;
Narayan Kamath044bc8e2014-12-03 18:22:53 +000046
Narayan Kamath926973e2014-06-09 14:18:14 +010047// This is for windows. If we don't open a file in binary mode, weird
Narayan Kamath7462f022013-11-21 13:05:04 +000048// things will happen.
49#ifndef O_BINARY
50#define O_BINARY 0
51#endif
52
Narayan Kamath926973e2014-06-09 14:18:14 +010053// The maximum number of bytes to scan backwards for the EOCD start.
54static const uint32_t kMaxEOCDSearch = kMaxCommentLen + sizeof(EocdRecord);
55
Narayan Kamath7462f022013-11-21 13:05:04 +000056static const char* kErrorMessages[] = {
57 "Unknown return code.",
Narayan Kamatheb41ad22013-12-09 16:26:36 +000058 "Iteration ended",
Narayan Kamath7462f022013-11-21 13:05:04 +000059 "Zlib error",
60 "Invalid file",
61 "Invalid handle",
62 "Duplicate entries in archive",
63 "Empty archive",
64 "Entry not found",
65 "Invalid offset",
66 "Inconsistent information",
67 "Invalid entry name",
Narayan Kamatheb41ad22013-12-09 16:26:36 +000068 "I/O Error",
Narayan Kamatheaf98852013-12-11 14:51:51 +000069 "File mapping failed"
Narayan Kamath7462f022013-11-21 13:05:04 +000070};
71
72static const int32_t kErrorMessageUpperBound = 0;
73
Narayan Kamatheb41ad22013-12-09 16:26:36 +000074static const int32_t kIterationEnd = -1;
Narayan Kamath7462f022013-11-21 13:05:04 +000075
76// We encountered a Zlib error when inflating a stream from this file.
77// Usually indicates file corruption.
78static const int32_t kZlibError = -2;
79
80// The input file cannot be processed as a zip archive. Usually because
81// it's too small, too large or does not have a valid signature.
82static const int32_t kInvalidFile = -3;
83
84// An invalid iteration / ziparchive handle was passed in as an input
85// argument.
86static const int32_t kInvalidHandle = -4;
87
88// The zip archive contained two (or possibly more) entries with the same
89// name.
90static const int32_t kDuplicateEntry = -5;
91
92// The zip archive contains no entries.
93static const int32_t kEmptyArchive = -6;
94
95// The specified entry was not found in the archive.
96static const int32_t kEntryNotFound = -7;
97
98// The zip archive contained an invalid local file header pointer.
99static const int32_t kInvalidOffset = -8;
100
101// The zip archive contained inconsistent entry information. This could
102// be because the central directory & local file header did not agree, or
103// if the actual uncompressed length or crc32 do not match their declared
104// values.
105static const int32_t kInconsistentInformation = -9;
106
107// An invalid entry name was encountered.
108static const int32_t kInvalidEntryName = -10;
109
Narayan Kamatheb41ad22013-12-09 16:26:36 +0000110// An I/O related system call (read, lseek, ftruncate, map) failed.
111static const int32_t kIoError = -11;
Narayan Kamath7462f022013-11-21 13:05:04 +0000112
Narayan Kamatheaf98852013-12-11 14:51:51 +0000113// We were not able to mmap the central directory or entry contents.
114static const int32_t kMmapFailed = -12;
Narayan Kamath7462f022013-11-21 13:05:04 +0000115
Narayan Kamatheaf98852013-12-11 14:51:51 +0000116static const int32_t kErrorMessageLowerBound = -13;
Narayan Kamath7462f022013-11-21 13:05:04 +0000117
Narayan Kamath7462f022013-11-21 13:05:04 +0000118/*
119 * A Read-only Zip archive.
120 *
121 * We want "open" and "find entry by name" to be fast operations, and
122 * we want to use as little memory as possible. We memory-map the zip
123 * central directory, and load a hash table with pointers to the filenames
124 * (which aren't null-terminated). The other fields are at a fixed offset
125 * from the filename, so we don't need to extract those (but we do need
126 * to byte-read and endian-swap them every time we want them).
127 *
128 * It's possible that somebody has handed us a massive (~1GB) zip archive,
129 * so we can't expect to mmap the entire file.
130 *
131 * To speed comparisons when doing a lookup by name, we could make the mapping
132 * "private" (copy-on-write) and null-terminate the filenames after verifying
133 * the record structure. However, this requires a private mapping of
134 * every page that the Central Directory touches. Easier to tuck a copy
135 * of the string length into the hash table entry.
136 */
137struct ZipArchive {
138 /* open Zip archive */
Neil Fullerb1a113f2014-07-25 14:43:04 +0100139 const int fd;
Dmitriy Ivanov40b52b22014-07-15 19:33:00 -0700140 const bool close_file;
Narayan Kamath7462f022013-11-21 13:05:04 +0000141
142 /* mapped central directory area */
143 off64_t directory_offset;
Dmitriy Ivanov4b67f832015-03-06 10:22:34 -0800144 android::FileMap directory_map;
Narayan Kamath7462f022013-11-21 13:05:04 +0000145
146 /* number of entries in the Zip archive */
147 uint16_t num_entries;
148
149 /*
150 * We know how many entries are in the Zip archive, so we can have a
151 * fixed-size hash table. We define a load factor of 0.75 and overallocat
152 * so the maximum number entries can never be higher than
153 * ((4 * UINT16_MAX) / 3 + 1) which can safely fit into a uint32_t.
154 */
155 uint32_t hash_table_size;
Yusuke Sato07447542015-06-25 14:39:19 -0700156 ZipString* hash_table;
Neil Fullerb1a113f2014-07-25 14:43:04 +0100157
Dmitriy Ivanov40b52b22014-07-15 19:33:00 -0700158 ZipArchive(const int fd, bool assume_ownership) :
Neil Fullerb1a113f2014-07-25 14:43:04 +0100159 fd(fd),
Dmitriy Ivanov40b52b22014-07-15 19:33:00 -0700160 close_file(assume_ownership),
Neil Fullerb1a113f2014-07-25 14:43:04 +0100161 directory_offset(0),
Neil Fullerb1a113f2014-07-25 14:43:04 +0100162 num_entries(0),
163 hash_table_size(0),
164 hash_table(NULL) {}
165
166 ~ZipArchive() {
Dmitriy Ivanov40b52b22014-07-15 19:33:00 -0700167 if (close_file && fd >= 0) {
Neil Fullerb1a113f2014-07-25 14:43:04 +0100168 close(fd);
169 }
170
Neil Fullerb1a113f2014-07-25 14:43:04 +0100171 free(hash_table);
172 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000173};
174
Narayan Kamath7462f022013-11-21 13:05:04 +0000175/*
176 * Round up to the next highest power of 2.
177 *
178 * Found on http://graphics.stanford.edu/~seander/bithacks.html.
179 */
180static uint32_t RoundUpPower2(uint32_t val) {
181 val--;
182 val |= val >> 1;
183 val |= val >> 2;
184 val |= val >> 4;
185 val |= val >> 8;
186 val |= val >> 16;
187 val++;
188
189 return val;
190}
191
Yusuke Sato07447542015-06-25 14:39:19 -0700192static uint32_t ComputeHash(const ZipString& name) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000193 uint32_t hash = 0;
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100194 uint16_t len = name.name_length;
195 const uint8_t* str = name.name;
Narayan Kamath7462f022013-11-21 13:05:04 +0000196
197 while (len--) {
198 hash = hash * 31 + *str++;
199 }
200
201 return hash;
202}
203
204/*
205 * Convert a ZipEntry to a hash table index, verifying that it's in a
206 * valid range.
207 */
Yusuke Sato07447542015-06-25 14:39:19 -0700208static int64_t EntryToIndex(const ZipString* hash_table,
Narayan Kamath7462f022013-11-21 13:05:04 +0000209 const uint32_t hash_table_size,
Yusuke Sato07447542015-06-25 14:39:19 -0700210 const ZipString& name) {
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100211 const uint32_t hash = ComputeHash(name);
Narayan Kamath7462f022013-11-21 13:05:04 +0000212
213 // NOTE: (hash_table_size - 1) is guaranteed to be non-negative.
214 uint32_t ent = hash & (hash_table_size - 1);
215 while (hash_table[ent].name != NULL) {
Yusuke Sato07447542015-06-25 14:39:19 -0700216 if (hash_table[ent] == name) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000217 return ent;
218 }
219
220 ent = (ent + 1) & (hash_table_size - 1);
221 }
222
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100223 ALOGV("Zip: Unable to find entry %.*s", name.name_length, name.name);
Narayan Kamath7462f022013-11-21 13:05:04 +0000224 return kEntryNotFound;
225}
226
227/*
228 * Add a new entry to the hash table.
229 */
Yusuke Sato07447542015-06-25 14:39:19 -0700230static int32_t AddToHash(ZipString *hash_table, const uint64_t hash_table_size,
231 const ZipString& name) {
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100232 const uint64_t hash = ComputeHash(name);
Narayan Kamath7462f022013-11-21 13:05:04 +0000233 uint32_t ent = hash & (hash_table_size - 1);
234
235 /*
236 * We over-allocated the table, so we're guaranteed to find an empty slot.
237 * Further, we guarantee that the hashtable size is not 0.
238 */
239 while (hash_table[ent].name != NULL) {
Yusuke Sato07447542015-06-25 14:39:19 -0700240 if (hash_table[ent] == name) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000241 // We've found a duplicate entry. We don't accept it
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100242 ALOGW("Zip: Found duplicate entry %.*s", name.name_length, name.name);
Narayan Kamath7462f022013-11-21 13:05:04 +0000243 return kDuplicateEntry;
244 }
245 ent = (ent + 1) & (hash_table_size - 1);
246 }
247
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100248 hash_table[ent].name = name.name;
249 hash_table[ent].name_length = name.name_length;
Narayan Kamath7462f022013-11-21 13:05:04 +0000250 return 0;
251}
252
Narayan Kamath7462f022013-11-21 13:05:04 +0000253static int32_t MapCentralDirectory0(int fd, const char* debug_file_name,
254 ZipArchive* archive, off64_t file_length,
Narayan Kamath926973e2014-06-09 14:18:14 +0100255 off64_t read_amount, uint8_t* scan_buffer) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000256 const off64_t search_start = file_length - read_amount;
257
258 if (lseek64(fd, search_start, SEEK_SET) != search_start) {
Narayan Kamath926973e2014-06-09 14:18:14 +0100259 ALOGW("Zip: seek %" PRId64 " failed: %s", static_cast<int64_t>(search_start),
260 strerror(errno));
Narayan Kamath7462f022013-11-21 13:05:04 +0000261 return kIoError;
262 }
Narayan Kamath926973e2014-06-09 14:18:14 +0100263 ssize_t actual = TEMP_FAILURE_RETRY(
264 read(fd, scan_buffer, static_cast<size_t>(read_amount)));
265 if (actual != static_cast<ssize_t>(read_amount)) {
266 ALOGW("Zip: read %" PRId64 " failed: %s", static_cast<int64_t>(read_amount),
267 strerror(errno));
Narayan Kamath7462f022013-11-21 13:05:04 +0000268 return kIoError;
269 }
270
271 /*
272 * Scan backward for the EOCD magic. In an archive without a trailing
273 * comment, we'll find it on the first try. (We may want to consider
274 * doing an initial minimal read; if we don't find it, retry with a
275 * second read as above.)
276 */
Narayan Kamath926973e2014-06-09 14:18:14 +0100277 int i = read_amount - sizeof(EocdRecord);
278 for (; i >= 0; i--) {
Dan Albert1ae07642015-04-09 14:11:18 -0700279 if (scan_buffer[i] == 0x50) {
280 uint32_t* sig_addr = reinterpret_cast<uint32_t*>(&scan_buffer[i]);
281 if (get_unaligned<uint32_t>(sig_addr) == EocdRecord::kSignature) {
282 ALOGV("+++ Found EOCD at buf+%d", i);
283 break;
284 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000285 }
286 }
287 if (i < 0) {
288 ALOGD("Zip: EOCD not found, %s is not zip", debug_file_name);
289 return kInvalidFile;
290 }
291
292 const off64_t eocd_offset = search_start + i;
Narayan Kamath926973e2014-06-09 14:18:14 +0100293 const EocdRecord* eocd = reinterpret_cast<const EocdRecord*>(scan_buffer + i);
Narayan Kamath7462f022013-11-21 13:05:04 +0000294 /*
Narayan Kamath926973e2014-06-09 14:18:14 +0100295 * Verify that there's no trailing space at the end of the central directory
296 * and its comment.
Narayan Kamath7462f022013-11-21 13:05:04 +0000297 */
Narayan Kamath926973e2014-06-09 14:18:14 +0100298 const off64_t calculated_length = eocd_offset + sizeof(EocdRecord)
299 + eocd->comment_length;
300 if (calculated_length != file_length) {
Narayan Kamath4f6b4992014-06-03 13:59:23 +0100301 ALOGW("Zip: %" PRId64 " extraneous bytes at the end of the central directory",
Narayan Kamath926973e2014-06-09 14:18:14 +0100302 static_cast<int64_t>(file_length - calculated_length));
Narayan Kamath4f6b4992014-06-03 13:59:23 +0100303 return kInvalidFile;
304 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000305
Narayan Kamath926973e2014-06-09 14:18:14 +0100306 /*
307 * Grab the CD offset and size, and the number of entries in the
308 * archive and verify that they look reasonable.
309 */
310 if (eocd->cd_start_offset + eocd->cd_size > eocd_offset) {
311 ALOGW("Zip: bad offsets (dir %" PRIu32 ", size %" PRIu32 ", eocd %" PRId64 ")",
312 eocd->cd_start_offset, eocd->cd_size, static_cast<int64_t>(eocd_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000313 return kInvalidOffset;
314 }
Narayan Kamath926973e2014-06-09 14:18:14 +0100315 if (eocd->num_records == 0) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000316 ALOGW("Zip: empty archive?");
317 return kEmptyArchive;
318 }
319
Elliott Hughese49236b2015-06-04 15:21:59 -0700320 ALOGV("+++ num_entries=%" PRIu32 " dir_size=%" PRIu32 " dir_offset=%" PRIu32,
Narayan Kamath926973e2014-06-09 14:18:14 +0100321 eocd->num_records, eocd->cd_size, eocd->cd_start_offset);
Narayan Kamath7462f022013-11-21 13:05:04 +0000322
323 /*
324 * It all looks good. Create a mapping for the CD, and set the fields
325 * in archive.
326 */
Dmitriy Ivanov4b67f832015-03-06 10:22:34 -0800327 if (!archive->directory_map.create(debug_file_name, fd,
328 static_cast<off64_t>(eocd->cd_start_offset),
329 static_cast<size_t>(eocd->cd_size), true /* read only */) ) {
Narayan Kamatheaf98852013-12-11 14:51:51 +0000330 return kMmapFailed;
Narayan Kamath7462f022013-11-21 13:05:04 +0000331 }
332
Narayan Kamath926973e2014-06-09 14:18:14 +0100333 archive->num_entries = eocd->num_records;
334 archive->directory_offset = eocd->cd_start_offset;
Narayan Kamath7462f022013-11-21 13:05:04 +0000335
336 return 0;
337}
338
339/*
340 * Find the zip Central Directory and memory-map it.
341 *
342 * On success, returns 0 after populating fields from the EOCD area:
343 * directory_offset
344 * directory_map
345 * num_entries
346 */
347static int32_t MapCentralDirectory(int fd, const char* debug_file_name,
348 ZipArchive* archive) {
349
350 // Test file length. We use lseek64 to make sure the file
351 // is small enough to be a zip file (Its size must be less than
352 // 0xffffffff bytes).
353 off64_t file_length = lseek64(fd, 0, SEEK_END);
354 if (file_length == -1) {
355 ALOGV("Zip: lseek on fd %d failed", fd);
356 return kInvalidFile;
357 }
358
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800359 if (file_length > static_cast<off64_t>(0xffffffff)) {
Narayan Kamath926973e2014-06-09 14:18:14 +0100360 ALOGV("Zip: zip file too long %" PRId64, static_cast<int64_t>(file_length));
Narayan Kamath7462f022013-11-21 13:05:04 +0000361 return kInvalidFile;
362 }
363
Narayan Kamath926973e2014-06-09 14:18:14 +0100364 if (file_length < static_cast<off64_t>(sizeof(EocdRecord))) {
365 ALOGV("Zip: length %" PRId64 " is too small to be zip", static_cast<int64_t>(file_length));
Narayan Kamath7462f022013-11-21 13:05:04 +0000366 return kInvalidFile;
367 }
368
369 /*
370 * Perform the traditional EOCD snipe hunt.
371 *
372 * We're searching for the End of Central Directory magic number,
373 * which appears at the start of the EOCD block. It's followed by
374 * 18 bytes of EOCD stuff and up to 64KB of archive comment. We
375 * need to read the last part of the file into a buffer, dig through
376 * it to find the magic number, parse some values out, and use those
377 * to determine the extent of the CD.
378 *
379 * We start by pulling in the last part of the file.
380 */
Narayan Kamath926973e2014-06-09 14:18:14 +0100381 off64_t read_amount = kMaxEOCDSearch;
382 if (file_length < read_amount) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000383 read_amount = file_length;
384 }
385
Narayan Kamath926973e2014-06-09 14:18:14 +0100386 uint8_t* scan_buffer = reinterpret_cast<uint8_t*>(malloc(read_amount));
Narayan Kamath7462f022013-11-21 13:05:04 +0000387 int32_t result = MapCentralDirectory0(fd, debug_file_name, archive,
388 file_length, read_amount, scan_buffer);
389
390 free(scan_buffer);
391 return result;
392}
393
394/*
395 * Parses the Zip archive's Central Directory. Allocates and populates the
396 * hash table.
397 *
398 * Returns 0 on success.
399 */
400static int32_t ParseZipArchive(ZipArchive* archive) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800401 const uint8_t* const cd_ptr =
402 reinterpret_cast<const uint8_t*>(archive->directory_map.getDataPtr());
Dmitriy Ivanov4b67f832015-03-06 10:22:34 -0800403 const size_t cd_length = archive->directory_map.getDataLength();
Narayan Kamath926973e2014-06-09 14:18:14 +0100404 const uint16_t num_entries = archive->num_entries;
Narayan Kamath7462f022013-11-21 13:05:04 +0000405
406 /*
407 * Create hash table. We have a minimum 75% load factor, possibly as
408 * low as 50% after we round off to a power of 2. There must be at
409 * least one unused entry to avoid an infinite loop during creation.
410 */
411 archive->hash_table_size = RoundUpPower2(1 + (num_entries * 4) / 3);
Yusuke Sato07447542015-06-25 14:39:19 -0700412 archive->hash_table = reinterpret_cast<ZipString*>(calloc(archive->hash_table_size,
413 sizeof(ZipString)));
Narayan Kamath7462f022013-11-21 13:05:04 +0000414
415 /*
416 * Walk through the central directory, adding entries to the hash
417 * table and verifying values.
418 */
Narayan Kamath926973e2014-06-09 14:18:14 +0100419 const uint8_t* const cd_end = cd_ptr + cd_length;
Narayan Kamath7462f022013-11-21 13:05:04 +0000420 const uint8_t* ptr = cd_ptr;
421 for (uint16_t i = 0; i < num_entries; i++) {
Narayan Kamath926973e2014-06-09 14:18:14 +0100422 const CentralDirectoryRecord* cdr =
423 reinterpret_cast<const CentralDirectoryRecord*>(ptr);
424 if (cdr->record_signature != CentralDirectoryRecord::kSignature) {
Mark Salyzyn088bf902014-05-08 16:02:20 -0700425 ALOGW("Zip: missed a central dir sig (at %" PRIu16 ")", i);
Dmitriy Ivanov3ea93da2015-03-06 11:48:47 -0800426 return -1;
Narayan Kamath7462f022013-11-21 13:05:04 +0000427 }
428
Narayan Kamath926973e2014-06-09 14:18:14 +0100429 if (ptr + sizeof(CentralDirectoryRecord) > cd_end) {
Mark Salyzyn088bf902014-05-08 16:02:20 -0700430 ALOGW("Zip: ran off the end (at %" PRIu16 ")", i);
Dmitriy Ivanov3ea93da2015-03-06 11:48:47 -0800431 return -1;
Narayan Kamath7462f022013-11-21 13:05:04 +0000432 }
433
Narayan Kamath926973e2014-06-09 14:18:14 +0100434 const off64_t local_header_offset = cdr->local_file_header_offset;
Narayan Kamath7462f022013-11-21 13:05:04 +0000435 if (local_header_offset >= archive->directory_offset) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800436 ALOGW("Zip: bad LFH offset %" PRId64 " at entry %" PRIu16,
437 static_cast<int64_t>(local_header_offset), i);
Dmitriy Ivanov3ea93da2015-03-06 11:48:47 -0800438 return -1;
Narayan Kamath7462f022013-11-21 13:05:04 +0000439 }
440
Narayan Kamath926973e2014-06-09 14:18:14 +0100441 const uint16_t file_name_length = cdr->file_name_length;
442 const uint16_t extra_length = cdr->extra_field_length;
443 const uint16_t comment_length = cdr->comment_length;
Piotr Jastrzebski78271ba2014-08-15 12:53:00 +0100444 const uint8_t* file_name = ptr + sizeof(CentralDirectoryRecord);
445
Narayan Kamath044bc8e2014-12-03 18:22:53 +0000446 /* check that file name is valid UTF-8 and doesn't contain NUL (U+0000) characters */
447 if (!IsValidEntryName(file_name, file_name_length)) {
Dmitriy Ivanov3ea93da2015-03-06 11:48:47 -0800448 return -1;
Piotr Jastrzebski78271ba2014-08-15 12:53:00 +0100449 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000450
451 /* add the CDE filename to the hash table */
Yusuke Sato07447542015-06-25 14:39:19 -0700452 ZipString entry_name;
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100453 entry_name.name = file_name;
454 entry_name.name_length = file_name_length;
Narayan Kamath7462f022013-11-21 13:05:04 +0000455 const int add_result = AddToHash(archive->hash_table,
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100456 archive->hash_table_size, entry_name);
Dmitriy Ivanov3ea93da2015-03-06 11:48:47 -0800457 if (add_result != 0) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000458 ALOGW("Zip: Error adding entry to hash table %d", add_result);
Dmitriy Ivanov3ea93da2015-03-06 11:48:47 -0800459 return add_result;
Narayan Kamath7462f022013-11-21 13:05:04 +0000460 }
461
Narayan Kamath926973e2014-06-09 14:18:14 +0100462 ptr += sizeof(CentralDirectoryRecord) + file_name_length + extra_length + comment_length;
463 if ((ptr - cd_ptr) > static_cast<int64_t>(cd_length)) {
Mark Salyzyn088bf902014-05-08 16:02:20 -0700464 ALOGW("Zip: bad CD advance (%tu vs %zu) at entry %" PRIu16,
465 ptr - cd_ptr, cd_length, i);
Dmitriy Ivanov3ea93da2015-03-06 11:48:47 -0800466 return -1;
Narayan Kamath7462f022013-11-21 13:05:04 +0000467 }
468 }
Mark Salyzyn088bf902014-05-08 16:02:20 -0700469 ALOGV("+++ zip good scan %" PRIu16 " entries", num_entries);
Narayan Kamath7462f022013-11-21 13:05:04 +0000470
Dmitriy Ivanov3ea93da2015-03-06 11:48:47 -0800471 return 0;
Narayan Kamath7462f022013-11-21 13:05:04 +0000472}
473
474static int32_t OpenArchiveInternal(ZipArchive* archive,
475 const char* debug_file_name) {
476 int32_t result = -1;
477 if ((result = MapCentralDirectory(archive->fd, debug_file_name, archive))) {
478 return result;
479 }
480
481 if ((result = ParseZipArchive(archive))) {
482 return result;
483 }
484
485 return 0;
486}
487
488int32_t OpenArchiveFd(int fd, const char* debug_file_name,
Dmitriy Ivanov40b52b22014-07-15 19:33:00 -0700489 ZipArchiveHandle* handle, bool assume_ownership) {
490 ZipArchive* archive = new ZipArchive(fd, assume_ownership);
Narayan Kamath7462f022013-11-21 13:05:04 +0000491 *handle = archive;
Narayan Kamath7462f022013-11-21 13:05:04 +0000492 return OpenArchiveInternal(archive, debug_file_name);
493}
494
495int32_t OpenArchive(const char* fileName, ZipArchiveHandle* handle) {
Neil Fullerb1a113f2014-07-25 14:43:04 +0100496 const int fd = open(fileName, O_RDONLY | O_BINARY, 0);
Dmitriy Ivanov40b52b22014-07-15 19:33:00 -0700497 ZipArchive* archive = new ZipArchive(fd, true);
Narayan Kamath7462f022013-11-21 13:05:04 +0000498 *handle = archive;
499
Narayan Kamath7462f022013-11-21 13:05:04 +0000500 if (fd < 0) {
501 ALOGW("Unable to open '%s': %s", fileName, strerror(errno));
502 return kIoError;
Narayan Kamath7462f022013-11-21 13:05:04 +0000503 }
Dmitriy Ivanov40b52b22014-07-15 19:33:00 -0700504
Narayan Kamath7462f022013-11-21 13:05:04 +0000505 return OpenArchiveInternal(archive, fileName);
506}
507
508/*
509 * Close a ZipArchive, closing the file and freeing the contents.
510 */
511void CloseArchive(ZipArchiveHandle handle) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800512 ZipArchive* archive = reinterpret_cast<ZipArchive*>(handle);
Narayan Kamath7462f022013-11-21 13:05:04 +0000513 ALOGV("Closing archive %p", archive);
Neil Fullerb1a113f2014-07-25 14:43:04 +0100514 delete archive;
Narayan Kamath7462f022013-11-21 13:05:04 +0000515}
516
517static int32_t UpdateEntryFromDataDescriptor(int fd,
518 ZipEntry *entry) {
Narayan Kamath926973e2014-06-09 14:18:14 +0100519 uint8_t ddBuf[sizeof(DataDescriptor) + sizeof(DataDescriptor::kOptSignature)];
Narayan Kamath7462f022013-11-21 13:05:04 +0000520 ssize_t actual = TEMP_FAILURE_RETRY(read(fd, ddBuf, sizeof(ddBuf)));
521 if (actual != sizeof(ddBuf)) {
522 return kIoError;
523 }
524
Narayan Kamath926973e2014-06-09 14:18:14 +0100525 const uint32_t ddSignature = *(reinterpret_cast<const uint32_t*>(ddBuf));
526 const uint16_t offset = (ddSignature == DataDescriptor::kOptSignature) ? 4 : 0;
527 const DataDescriptor* descriptor = reinterpret_cast<const DataDescriptor*>(ddBuf + offset);
Narayan Kamath7462f022013-11-21 13:05:04 +0000528
Narayan Kamath926973e2014-06-09 14:18:14 +0100529 entry->crc32 = descriptor->crc32;
530 entry->compressed_length = descriptor->compressed_size;
531 entry->uncompressed_length = descriptor->uncompressed_size;
Narayan Kamath7462f022013-11-21 13:05:04 +0000532
533 return 0;
534}
535
536// Attempts to read |len| bytes into |buf| at offset |off|.
537//
538// This method uses pread64 on platforms that support it and
539// lseek64 + read on platforms that don't. This implies that
540// callers should not rely on the |fd| offset being incremented
541// as a side effect of this call.
542static inline ssize_t ReadAtOffset(int fd, uint8_t* buf, size_t len,
543 off64_t off) {
Yabin Cui70160f42014-11-19 20:47:18 -0800544#if !defined(_WIN32)
Narayan Kamath7462f022013-11-21 13:05:04 +0000545 return TEMP_FAILURE_RETRY(pread64(fd, buf, len, off));
546#else
547 // The only supported platform that doesn't support pread at the moment
548 // is Windows. Only recent versions of windows support unix like forks,
549 // and even there the semantics are quite different.
550 if (lseek64(fd, off, SEEK_SET) != off) {
Mark Salyzyn99ef9912014-03-14 14:26:22 -0700551 ALOGW("Zip: failed seek to offset %" PRId64, off);
Narayan Kamath7462f022013-11-21 13:05:04 +0000552 return kIoError;
553 }
554
555 return TEMP_FAILURE_RETRY(read(fd, buf, len));
Yabin Cui70160f42014-11-19 20:47:18 -0800556#endif
Narayan Kamath7462f022013-11-21 13:05:04 +0000557}
558
559static int32_t FindEntry(const ZipArchive* archive, const int ent,
560 ZipEntry* data) {
561 const uint16_t nameLen = archive->hash_table[ent].name_length;
Narayan Kamath7462f022013-11-21 13:05:04 +0000562
563 // Recover the start of the central directory entry from the filename
564 // pointer. The filename is the first entry past the fixed-size data,
565 // so we can just subtract back from that.
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100566 const uint8_t* ptr = archive->hash_table[ent].name;
Narayan Kamath926973e2014-06-09 14:18:14 +0100567 ptr -= sizeof(CentralDirectoryRecord);
Narayan Kamath7462f022013-11-21 13:05:04 +0000568
569 // This is the base of our mmapped region, we have to sanity check that
570 // the name that's in the hash table is a pointer to a location within
571 // this mapped region.
Narayan Kamath926973e2014-06-09 14:18:14 +0100572 const uint8_t* base_ptr = reinterpret_cast<const uint8_t*>(
Dmitriy Ivanov4b67f832015-03-06 10:22:34 -0800573 archive->directory_map.getDataPtr());
574 if (ptr < base_ptr || ptr > base_ptr + archive->directory_map.getDataLength()) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000575 ALOGW("Zip: Invalid entry pointer");
576 return kInvalidOffset;
577 }
578
Narayan Kamath926973e2014-06-09 14:18:14 +0100579 const CentralDirectoryRecord *cdr =
580 reinterpret_cast<const CentralDirectoryRecord*>(ptr);
581
Narayan Kamath7462f022013-11-21 13:05:04 +0000582 // The offset of the start of the central directory in the zipfile.
583 // We keep this lying around so that we can sanity check all our lengths
584 // and our per-file structures.
585 const off64_t cd_offset = archive->directory_offset;
586
587 // Fill out the compression method, modification time, crc32
588 // and other interesting attributes from the central directory. These
589 // will later be compared against values from the local file header.
Narayan Kamath926973e2014-06-09 14:18:14 +0100590 data->method = cdr->compression_method;
591 data->mod_time = cdr->last_mod_time;
592 data->crc32 = cdr->crc32;
593 data->compressed_length = cdr->compressed_size;
594 data->uncompressed_length = cdr->uncompressed_size;
Narayan Kamath7462f022013-11-21 13:05:04 +0000595
596 // Figure out the local header offset from the central directory. The
597 // actual file data will begin after the local header and the name /
598 // extra comments.
Narayan Kamath926973e2014-06-09 14:18:14 +0100599 const off64_t local_header_offset = cdr->local_file_header_offset;
600 if (local_header_offset + static_cast<off64_t>(sizeof(LocalFileHeader)) >= cd_offset) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000601 ALOGW("Zip: bad local hdr offset in zip");
602 return kInvalidOffset;
603 }
604
Narayan Kamath926973e2014-06-09 14:18:14 +0100605 uint8_t lfh_buf[sizeof(LocalFileHeader)];
Narayan Kamath7462f022013-11-21 13:05:04 +0000606 ssize_t actual = ReadAtOffset(archive->fd, lfh_buf, sizeof(lfh_buf),
607 local_header_offset);
608 if (actual != sizeof(lfh_buf)) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800609 ALOGW("Zip: failed reading lfh name from offset %" PRId64,
610 static_cast<int64_t>(local_header_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000611 return kIoError;
612 }
613
Narayan Kamath926973e2014-06-09 14:18:14 +0100614 const LocalFileHeader *lfh = reinterpret_cast<const LocalFileHeader*>(lfh_buf);
615
616 if (lfh->lfh_signature != LocalFileHeader::kSignature) {
Mark Salyzyn99ef9912014-03-14 14:26:22 -0700617 ALOGW("Zip: didn't find signature at start of lfh, offset=%" PRId64,
Narayan Kamath926973e2014-06-09 14:18:14 +0100618 static_cast<int64_t>(local_header_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000619 return kInvalidOffset;
620 }
621
622 // Paranoia: Match the values specified in the local file header
623 // to those specified in the central directory.
Narayan Kamath926973e2014-06-09 14:18:14 +0100624 if ((lfh->gpb_flags & kGPBDDFlagMask) == 0) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000625 data->has_data_descriptor = 0;
Narayan Kamath926973e2014-06-09 14:18:14 +0100626 if (data->compressed_length != lfh->compressed_size
627 || data->uncompressed_length != lfh->uncompressed_size
628 || data->crc32 != lfh->crc32) {
Mark Salyzyn088bf902014-05-08 16:02:20 -0700629 ALOGW("Zip: size/crc32 mismatch. expected {%" PRIu32 ", %" PRIu32
630 ", %" PRIx32 "}, was {%" PRIu32 ", %" PRIu32 ", %" PRIx32 "}",
Narayan Kamath7462f022013-11-21 13:05:04 +0000631 data->compressed_length, data->uncompressed_length, data->crc32,
Narayan Kamath926973e2014-06-09 14:18:14 +0100632 lfh->compressed_size, lfh->uncompressed_size, lfh->crc32);
Narayan Kamath7462f022013-11-21 13:05:04 +0000633 return kInconsistentInformation;
634 }
635 } else {
636 data->has_data_descriptor = 1;
637 }
638
639 // Check that the local file header name matches the declared
640 // name in the central directory.
Narayan Kamath926973e2014-06-09 14:18:14 +0100641 if (lfh->file_name_length == nameLen) {
642 const off64_t name_offset = local_header_offset + sizeof(LocalFileHeader);
Mykola Kondratenko50afc152014-09-08 12:46:37 +0200643 if (name_offset + lfh->file_name_length > cd_offset) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000644 ALOGW("Zip: Invalid declared length");
645 return kInvalidOffset;
646 }
647
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800648 uint8_t* name_buf = reinterpret_cast<uint8_t*>(malloc(nameLen));
Narayan Kamath7462f022013-11-21 13:05:04 +0000649 ssize_t actual = ReadAtOffset(archive->fd, name_buf, nameLen,
650 name_offset);
651
652 if (actual != nameLen) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800653 ALOGW("Zip: failed reading lfh name from offset %" PRId64, static_cast<int64_t>(name_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000654 free(name_buf);
655 return kIoError;
656 }
657
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100658 if (memcmp(archive->hash_table[ent].name, name_buf, nameLen)) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000659 free(name_buf);
660 return kInconsistentInformation;
661 }
662
663 free(name_buf);
664 } else {
665 ALOGW("Zip: lfh name did not match central directory.");
666 return kInconsistentInformation;
667 }
668
Narayan Kamath926973e2014-06-09 14:18:14 +0100669 const off64_t data_offset = local_header_offset + sizeof(LocalFileHeader)
670 + lfh->file_name_length + lfh->extra_field_length;
Narayan Kamath48953a12014-01-24 12:32:39 +0000671 if (data_offset > cd_offset) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800672 ALOGW("Zip: bad data offset %" PRId64 " in zip", static_cast<int64_t>(data_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000673 return kInvalidOffset;
674 }
675
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800676 if (static_cast<off64_t>(data_offset + data->compressed_length) > cd_offset) {
Mark Salyzyn088bf902014-05-08 16:02:20 -0700677 ALOGW("Zip: bad compressed length in zip (%" PRId64 " + %" PRIu32 " > %" PRId64 ")",
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800678 static_cast<int64_t>(data_offset), data->compressed_length, static_cast<int64_t>(cd_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000679 return kInvalidOffset;
680 }
681
682 if (data->method == kCompressStored &&
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800683 static_cast<off64_t>(data_offset + data->uncompressed_length) > cd_offset) {
Mark Salyzyn088bf902014-05-08 16:02:20 -0700684 ALOGW("Zip: bad uncompressed length in zip (%" PRId64 " + %" PRIu32 " > %" PRId64 ")",
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800685 static_cast<int64_t>(data_offset), data->uncompressed_length,
686 static_cast<int64_t>(cd_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000687 return kInvalidOffset;
688 }
689
690 data->offset = data_offset;
691 return 0;
692}
693
694struct IterationHandle {
695 uint32_t position;
Piotr Jastrzebski10aa9a02014-08-19 09:01:20 +0100696 // We're not using vector here because this code is used in the Windows SDK
697 // where the STL is not available.
Yusuke Sato07447542015-06-25 14:39:19 -0700698 ZipString prefix;
699 ZipString suffix;
Narayan Kamath7462f022013-11-21 13:05:04 +0000700 ZipArchive* archive;
Piotr Jastrzebski8e085362014-08-18 11:37:45 +0100701
Yusuke Sato07447542015-06-25 14:39:19 -0700702 IterationHandle(const ZipString* in_prefix,
703 const ZipString* in_suffix) {
704 if (in_prefix) {
705 uint8_t* name_copy = new uint8_t[in_prefix->name_length];
706 memcpy(name_copy, in_prefix->name, in_prefix->name_length);
707 prefix.name = name_copy;
708 prefix.name_length = in_prefix->name_length;
709 } else {
710 prefix.name = NULL;
711 prefix.name_length = 0;
Yusuke Satof1d3d3b2015-06-25 14:09:00 -0700712 }
Yusuke Sato07447542015-06-25 14:39:19 -0700713 if (in_suffix) {
714 uint8_t* name_copy = new uint8_t[in_suffix->name_length];
715 memcpy(name_copy, in_suffix->name, in_suffix->name_length);
716 suffix.name = name_copy;
717 suffix.name_length = in_suffix->name_length;
718 } else {
719 suffix.name = NULL;
720 suffix.name_length = 0;
Yusuke Satof1d3d3b2015-06-25 14:09:00 -0700721 }
Piotr Jastrzebski8e085362014-08-18 11:37:45 +0100722 }
723
724 ~IterationHandle() {
Yusuke Sato07447542015-06-25 14:39:19 -0700725 delete[] prefix.name;
726 delete[] suffix.name;
Piotr Jastrzebski8e085362014-08-18 11:37:45 +0100727 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000728};
729
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100730int32_t StartIteration(ZipArchiveHandle handle, void** cookie_ptr,
Yusuke Sato07447542015-06-25 14:39:19 -0700731 const ZipString* optional_prefix,
732 const ZipString* optional_suffix) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800733 ZipArchive* archive = reinterpret_cast<ZipArchive*>(handle);
Narayan Kamath7462f022013-11-21 13:05:04 +0000734
735 if (archive == NULL || archive->hash_table == NULL) {
736 ALOGW("Zip: Invalid ZipArchiveHandle");
737 return kInvalidHandle;
738 }
739
Yusuke Satof1d3d3b2015-06-25 14:09:00 -0700740 IterationHandle* cookie = new IterationHandle(optional_prefix, optional_suffix);
Narayan Kamath7462f022013-11-21 13:05:04 +0000741 cookie->position = 0;
Narayan Kamath7462f022013-11-21 13:05:04 +0000742 cookie->archive = archive;
Narayan Kamath7462f022013-11-21 13:05:04 +0000743
744 *cookie_ptr = cookie ;
745 return 0;
746}
747
Piotr Jastrzebski79c8b342014-08-08 14:02:17 +0100748void EndIteration(void* cookie) {
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100749 delete reinterpret_cast<IterationHandle*>(cookie);
Piotr Jastrzebski79c8b342014-08-08 14:02:17 +0100750}
751
Yusuke Sato07447542015-06-25 14:39:19 -0700752int32_t FindEntry(const ZipArchiveHandle handle, const ZipString& entryName,
Narayan Kamath7462f022013-11-21 13:05:04 +0000753 ZipEntry* data) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800754 const ZipArchive* archive = reinterpret_cast<ZipArchive*>(handle);
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100755 if (entryName.name_length == 0) {
756 ALOGW("Zip: Invalid filename %.*s", entryName.name_length, entryName.name);
Narayan Kamath7462f022013-11-21 13:05:04 +0000757 return kInvalidEntryName;
758 }
759
760 const int64_t ent = EntryToIndex(archive->hash_table,
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100761 archive->hash_table_size, entryName);
Narayan Kamath7462f022013-11-21 13:05:04 +0000762
763 if (ent < 0) {
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100764 ALOGV("Zip: Could not find entry %.*s", entryName.name_length, entryName.name);
Narayan Kamath7462f022013-11-21 13:05:04 +0000765 return ent;
766 }
767
768 return FindEntry(archive, ent, data);
769}
770
Yusuke Sato07447542015-06-25 14:39:19 -0700771int32_t Next(void* cookie, ZipEntry* data, ZipString* name) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800772 IterationHandle* handle = reinterpret_cast<IterationHandle*>(cookie);
Narayan Kamath7462f022013-11-21 13:05:04 +0000773 if (handle == NULL) {
774 return kInvalidHandle;
775 }
776
777 ZipArchive* archive = handle->archive;
778 if (archive == NULL || archive->hash_table == NULL) {
779 ALOGW("Zip: Invalid ZipArchiveHandle");
780 return kInvalidHandle;
781 }
782
783 const uint32_t currentOffset = handle->position;
784 const uint32_t hash_table_length = archive->hash_table_size;
Yusuke Sato07447542015-06-25 14:39:19 -0700785 const ZipString* hash_table = archive->hash_table;
Narayan Kamath7462f022013-11-21 13:05:04 +0000786
787 for (uint32_t i = currentOffset; i < hash_table_length; ++i) {
788 if (hash_table[i].name != NULL &&
Yusuke Sato07447542015-06-25 14:39:19 -0700789 (handle->prefix.name_length == 0 ||
790 hash_table[i].StartsWith(handle->prefix)) &&
791 (handle->suffix.name_length == 0 ||
792 hash_table[i].EndsWith(handle->suffix))) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000793 handle->position = (i + 1);
794 const int error = FindEntry(archive, i, data);
795 if (!error) {
796 name->name = hash_table[i].name;
797 name->name_length = hash_table[i].name_length;
798 }
799
800 return error;
801 }
802 }
803
804 handle->position = 0;
805 return kIterationEnd;
806}
807
Narayan Kamathf899bd52015-04-17 11:53:14 +0100808class Writer {
809 public:
810 virtual bool Append(uint8_t* buf, size_t buf_size) = 0;
811 virtual ~Writer() {}
812 protected:
813 Writer() = default;
814 private:
815 DISALLOW_COPY_AND_ASSIGN(Writer);
816};
817
818// A Writer that writes data to a fixed size memory region.
819// The size of the memory region must be equal to the total size of
820// the data appended to it.
821class MemoryWriter : public Writer {
822 public:
823 MemoryWriter(uint8_t* buf, size_t size) : Writer(),
824 buf_(buf), size_(size), bytes_written_(0) {
825 }
826
827 virtual bool Append(uint8_t* buf, size_t buf_size) override {
828 if (bytes_written_ + buf_size > size_) {
829 ALOGW("Zip: Unexpected size " ZD " (declared) vs " ZD " (actual)",
830 size_, bytes_written_ + buf_size);
831 return false;
832 }
833
834 memcpy(buf_ + bytes_written_, buf, buf_size);
835 bytes_written_ += buf_size;
836 return true;
837 }
838
839 private:
840 uint8_t* const buf_;
841 const size_t size_;
842 size_t bytes_written_;
843};
844
845// A Writer that appends data to a file |fd| at its current position.
846// The file will be truncated to the end of the written data.
847class FileWriter : public Writer {
848 public:
849
850 // Creates a FileWriter for |fd| and prepare to write |entry| to it,
851 // guaranteeing that the file descriptor is valid and that there's enough
852 // space on the volume to write out the entry completely and that the file
853 // is truncated to the correct length.
854 //
855 // Returns a valid FileWriter on success, |nullptr| if an error occurred.
856 static std::unique_ptr<FileWriter> Create(int fd, const ZipEntry* entry) {
857 const uint32_t declared_length = entry->uncompressed_length;
858 const off64_t current_offset = lseek64(fd, 0, SEEK_CUR);
859 if (current_offset == -1) {
860 ALOGW("Zip: unable to seek to current location on fd %d: %s", fd, strerror(errno));
861 return nullptr;
862 }
863
864 int result = 0;
865#if defined(__linux__)
866 if (declared_length > 0) {
867 // Make sure we have enough space on the volume to extract the compressed
868 // entry. Note that the call to ftruncate below will change the file size but
869 // will not allocate space on disk and this call to fallocate will not
870 // change the file size.
Badhri Jagan Sridharana68d0d12015-06-02 14:47:57 -0700871 // Note: fallocate is only supported by the following filesystems -
872 // btrfs, ext4, ocfs2, and xfs. Therefore fallocate might fail with
873 // EOPNOTSUPP error when issued in other filesystems.
874 // Hence, check for the return error code before concluding that the
875 // disk does not have enough space.
Narayan Kamathf899bd52015-04-17 11:53:14 +0100876 result = TEMP_FAILURE_RETRY(fallocate(fd, 0, current_offset, declared_length));
Badhri Jagan Sridharana68d0d12015-06-02 14:47:57 -0700877 if (result == -1 && errno == ENOSPC) {
Narayan Kamathf899bd52015-04-17 11:53:14 +0100878 ALOGW("Zip: unable to allocate space for file to %" PRId64 ": %s",
879 static_cast<int64_t>(declared_length + current_offset), strerror(errno));
880 return std::unique_ptr<FileWriter>(nullptr);
881 }
882 }
883#endif // __linux__
884
885 result = TEMP_FAILURE_RETRY(ftruncate(fd, declared_length + current_offset));
886 if (result == -1) {
887 ALOGW("Zip: unable to truncate file to %" PRId64 ": %s",
888 static_cast<int64_t>(declared_length + current_offset), strerror(errno));
889 return std::unique_ptr<FileWriter>(nullptr);
890 }
891
892 return std::unique_ptr<FileWriter>(new FileWriter(fd, declared_length));
893 }
894
895 virtual bool Append(uint8_t* buf, size_t buf_size) override {
896 if (total_bytes_written_ + buf_size > declared_length_) {
897 ALOGW("Zip: Unexpected size " ZD " (declared) vs " ZD " (actual)",
898 declared_length_, total_bytes_written_ + buf_size);
899 return false;
900 }
901
Narayan Kamathe97e66e2015-04-27 16:25:53 +0100902 const bool result = android::base::WriteFully(fd_, buf, buf_size);
903 if (result) {
904 total_bytes_written_ += buf_size;
905 } else {
906 ALOGW("Zip: unable to write " ZD " bytes to file; %s", buf_size, strerror(errno));
Narayan Kamathf899bd52015-04-17 11:53:14 +0100907 }
908
Narayan Kamathe97e66e2015-04-27 16:25:53 +0100909 return result;
Narayan Kamathf899bd52015-04-17 11:53:14 +0100910 }
911 private:
912 FileWriter(const int fd, const size_t declared_length) :
913 Writer(),
914 fd_(fd),
915 declared_length_(declared_length),
916 total_bytes_written_(0) {
917 }
918
919 const int fd_;
920 const size_t declared_length_;
921 size_t total_bytes_written_;
922};
923
Dmitriy Ivanovf94e1592015-03-06 13:27:59 -0800924// This method is using libz macros with old-style-casts
925#pragma GCC diagnostic push
926#pragma GCC diagnostic ignored "-Wold-style-cast"
927static inline int zlib_inflateInit2(z_stream* stream, int window_bits) {
928 return inflateInit2(stream, window_bits);
929}
930#pragma GCC diagnostic pop
931
Narayan Kamathf899bd52015-04-17 11:53:14 +0100932static int32_t InflateEntryToWriter(int fd, const ZipEntry* entry,
933 Writer* writer, uint64_t* crc_out) {
Dmitriy Ivanovedbabfe2015-03-12 09:58:15 -0700934 const size_t kBufSize = 32768;
935 std::vector<uint8_t> read_buf(kBufSize);
936 std::vector<uint8_t> write_buf(kBufSize);
Narayan Kamath7462f022013-11-21 13:05:04 +0000937 z_stream zstream;
938 int zerr;
939
940 /*
941 * Initialize the zlib stream struct.
942 */
943 memset(&zstream, 0, sizeof(zstream));
944 zstream.zalloc = Z_NULL;
945 zstream.zfree = Z_NULL;
946 zstream.opaque = Z_NULL;
947 zstream.next_in = NULL;
948 zstream.avail_in = 0;
Dmitriy Ivanovedbabfe2015-03-12 09:58:15 -0700949 zstream.next_out = &write_buf[0];
Narayan Kamath7462f022013-11-21 13:05:04 +0000950 zstream.avail_out = kBufSize;
951 zstream.data_type = Z_UNKNOWN;
952
953 /*
954 * Use the undocumented "negative window bits" feature to tell zlib
955 * that there's no zlib header waiting for it.
956 */
Dmitriy Ivanovf94e1592015-03-06 13:27:59 -0800957 zerr = zlib_inflateInit2(&zstream, -MAX_WBITS);
Narayan Kamath7462f022013-11-21 13:05:04 +0000958 if (zerr != Z_OK) {
959 if (zerr == Z_VERSION_ERROR) {
960 ALOGE("Installed zlib is not compatible with linked version (%s)",
961 ZLIB_VERSION);
962 } else {
963 ALOGW("Call to inflateInit2 failed (zerr=%d)", zerr);
964 }
965
966 return kZlibError;
967 }
968
Dmitriy Ivanov1f741e52015-03-06 14:26:37 -0800969 auto zstream_deleter = [](z_stream* stream) {
970 inflateEnd(stream); /* free up any allocated structures */
971 };
972
973 std::unique_ptr<z_stream, decltype(zstream_deleter)> zstream_guard(&zstream, zstream_deleter);
974
Narayan Kamath7462f022013-11-21 13:05:04 +0000975 const uint32_t uncompressed_length = entry->uncompressed_length;
976
977 uint32_t compressed_length = entry->compressed_length;
Narayan Kamath7462f022013-11-21 13:05:04 +0000978 do {
979 /* read as much as we can */
980 if (zstream.avail_in == 0) {
Mark Salyzyn51d562d2014-05-05 14:38:05 -0700981 const ZD_TYPE getSize = (compressed_length > kBufSize) ? kBufSize : compressed_length;
Dmitriy Ivanovedbabfe2015-03-12 09:58:15 -0700982 const ZD_TYPE actual = TEMP_FAILURE_RETRY(read(fd, &read_buf[0], getSize));
Narayan Kamath7462f022013-11-21 13:05:04 +0000983 if (actual != getSize) {
Mark Salyzyn51d562d2014-05-05 14:38:05 -0700984 ALOGW("Zip: inflate read failed (" ZD " vs " ZD ")", actual, getSize);
Dmitriy Ivanov1f741e52015-03-06 14:26:37 -0800985 return kIoError;
Narayan Kamath7462f022013-11-21 13:05:04 +0000986 }
987
988 compressed_length -= getSize;
989
Dmitriy Ivanovedbabfe2015-03-12 09:58:15 -0700990 zstream.next_in = &read_buf[0];
Narayan Kamath7462f022013-11-21 13:05:04 +0000991 zstream.avail_in = getSize;
992 }
993
994 /* uncompress the data */
995 zerr = inflate(&zstream, Z_NO_FLUSH);
996 if (zerr != Z_OK && zerr != Z_STREAM_END) {
997 ALOGW("Zip: inflate zerr=%d (nIn=%p aIn=%u nOut=%p aOut=%u)",
998 zerr, zstream.next_in, zstream.avail_in,
999 zstream.next_out, zstream.avail_out);
Dmitriy Ivanov1f741e52015-03-06 14:26:37 -08001000 return kZlibError;
Narayan Kamath7462f022013-11-21 13:05:04 +00001001 }
1002
1003 /* write when we're full or when we're done */
1004 if (zstream.avail_out == 0 ||
1005 (zerr == Z_STREAM_END && zstream.avail_out != kBufSize)) {
Dmitriy Ivanovedbabfe2015-03-12 09:58:15 -07001006 const size_t write_size = zstream.next_out - &write_buf[0];
Narayan Kamathf899bd52015-04-17 11:53:14 +01001007 if (!writer->Append(&write_buf[0], write_size)) {
1008 // The file might have declared a bogus length.
1009 return kInconsistentInformation;
Narayan Kamath7462f022013-11-21 13:05:04 +00001010 }
Narayan Kamath7462f022013-11-21 13:05:04 +00001011
Dmitriy Ivanovedbabfe2015-03-12 09:58:15 -07001012 zstream.next_out = &write_buf[0];
Narayan Kamath7462f022013-11-21 13:05:04 +00001013 zstream.avail_out = kBufSize;
1014 }
1015 } while (zerr == Z_OK);
1016
1017 assert(zerr == Z_STREAM_END); /* other errors should've been caught */
1018
1019 // stream.adler holds the crc32 value for such streams.
1020 *crc_out = zstream.adler;
1021
1022 if (zstream.total_out != uncompressed_length || compressed_length != 0) {
Mark Salyzyn088bf902014-05-08 16:02:20 -07001023 ALOGW("Zip: size mismatch on inflated file (%lu vs %" PRIu32 ")",
Narayan Kamath7462f022013-11-21 13:05:04 +00001024 zstream.total_out, uncompressed_length);
Dmitriy Ivanov1f741e52015-03-06 14:26:37 -08001025 return kInconsistentInformation;
Narayan Kamath7462f022013-11-21 13:05:04 +00001026 }
1027
Dmitriy Ivanov1f741e52015-03-06 14:26:37 -08001028 return 0;
Narayan Kamath7462f022013-11-21 13:05:04 +00001029}
1030
Narayan Kamathf899bd52015-04-17 11:53:14 +01001031static int32_t CopyEntryToWriter(int fd, const ZipEntry* entry, Writer* writer,
1032 uint64_t *crc_out) {
1033 static const uint32_t kBufSize = 32768;
1034 std::vector<uint8_t> buf(kBufSize);
1035
1036 const uint32_t length = entry->uncompressed_length;
1037 uint32_t count = 0;
1038 uint64_t crc = 0;
1039 while (count < length) {
1040 uint32_t remaining = length - count;
1041
1042 // Safe conversion because kBufSize is narrow enough for a 32 bit signed
1043 // value.
1044 const ssize_t block_size = (remaining > kBufSize) ? kBufSize : remaining;
1045 const ssize_t actual = TEMP_FAILURE_RETRY(read(fd, &buf[0], block_size));
1046
1047 if (actual != block_size) {
1048 ALOGW("CopyFileToFile: copy read failed (" ZD " vs " ZD ")", actual, block_size);
1049 return kIoError;
1050 }
1051
1052 if (!writer->Append(&buf[0], block_size)) {
1053 return kIoError;
1054 }
1055 crc = crc32(crc, &buf[0], block_size);
1056 count += block_size;
1057 }
1058
1059 *crc_out = crc;
1060
1061 return 0;
1062}
1063
1064int32_t ExtractToWriter(ZipArchiveHandle handle,
1065 ZipEntry* entry, Writer* writer) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -08001066 ZipArchive* archive = reinterpret_cast<ZipArchive*>(handle);
Narayan Kamath7462f022013-11-21 13:05:04 +00001067 const uint16_t method = entry->method;
1068 off64_t data_offset = entry->offset;
1069
1070 if (lseek64(archive->fd, data_offset, SEEK_SET) != data_offset) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -08001071 ALOGW("Zip: lseek to data at %" PRId64 " failed", static_cast<int64_t>(data_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +00001072 return kIoError;
1073 }
1074
1075 // this should default to kUnknownCompressionMethod.
1076 int32_t return_value = -1;
1077 uint64_t crc = 0;
1078 if (method == kCompressStored) {
Narayan Kamathf899bd52015-04-17 11:53:14 +01001079 return_value = CopyEntryToWriter(archive->fd, entry, writer, &crc);
Narayan Kamath7462f022013-11-21 13:05:04 +00001080 } else if (method == kCompressDeflated) {
Narayan Kamathf899bd52015-04-17 11:53:14 +01001081 return_value = InflateEntryToWriter(archive->fd, entry, writer, &crc);
Narayan Kamath7462f022013-11-21 13:05:04 +00001082 }
1083
1084 if (!return_value && entry->has_data_descriptor) {
1085 return_value = UpdateEntryFromDataDescriptor(archive->fd, entry);
1086 if (return_value) {
1087 return return_value;
1088 }
1089 }
1090
1091 // TODO: Fix this check by passing the right flags to inflate2 so that
1092 // it calculates the CRC for us.
1093 if (entry->crc32 != crc && false) {
Mark Salyzyn088bf902014-05-08 16:02:20 -07001094 ALOGW("Zip: crc mismatch: expected %" PRIu32 ", was %" PRIu64, entry->crc32, crc);
Narayan Kamath7462f022013-11-21 13:05:04 +00001095 return kInconsistentInformation;
1096 }
1097
1098 return return_value;
1099}
1100
Narayan Kamathf899bd52015-04-17 11:53:14 +01001101int32_t ExtractToMemory(ZipArchiveHandle handle, ZipEntry* entry,
1102 uint8_t* begin, uint32_t size) {
1103 std::unique_ptr<Writer> writer(new MemoryWriter(begin, size));
1104 return ExtractToWriter(handle, entry, writer.get());
1105}
1106
Narayan Kamath7462f022013-11-21 13:05:04 +00001107int32_t ExtractEntryToFile(ZipArchiveHandle handle,
1108 ZipEntry* entry, int fd) {
Narayan Kamathf899bd52015-04-17 11:53:14 +01001109 std::unique_ptr<Writer> writer(FileWriter::Create(fd, entry));
1110 if (writer.get() == nullptr) {
Narayan Kamath7462f022013-11-21 13:05:04 +00001111 return kIoError;
1112 }
1113
Narayan Kamathf899bd52015-04-17 11:53:14 +01001114 return ExtractToWriter(handle, entry, writer.get());
Narayan Kamath7462f022013-11-21 13:05:04 +00001115}
1116
1117const char* ErrorCodeString(int32_t error_code) {
1118 if (error_code > kErrorMessageLowerBound && error_code < kErrorMessageUpperBound) {
1119 return kErrorMessages[error_code * -1];
1120 }
1121
1122 return kErrorMessages[0];
1123}
1124
1125int GetFileDescriptor(const ZipArchiveHandle handle) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -08001126 return reinterpret_cast<ZipArchive*>(handle)->fd;
Narayan Kamath7462f022013-11-21 13:05:04 +00001127}