blob: d0bbd7220ebcc580b5481bc6a0f3ff1f29dd708a [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
Mark Salyzyncfd5b082016-10-17 14:28:00 -070021#define LOG_TAG "ziparchive"
22
Narayan Kamath7462f022013-11-21 13:05:04 +000023#include <assert.h>
24#include <errno.h>
Mark Salyzyn99ef9912014-03-14 14:26:22 -070025#include <fcntl.h>
26#include <inttypes.h>
Narayan Kamath7462f022013-11-21 13:05:04 +000027#include <limits.h>
Narayan Kamath7462f022013-11-21 13:05:04 +000028#include <stdlib.h>
29#include <string.h>
Narayan Kamath7462f022013-11-21 13:05:04 +000030#include <unistd.h>
31
Dan Albert1ae07642015-04-09 14:11:18 -070032#include <memory>
33#include <vector>
34
Mark Salyzynff2dcd92016-09-28 15:54:45 -070035#include <android-base/file.h>
36#include <android-base/logging.h>
37#include <android-base/macros.h> // TEMP_FAILURE_RETRY may or may not be in unistd
38#include <android-base/memory.h>
Mark Salyzyncfd5b082016-10-17 14:28:00 -070039#include <log/log.h>
Mark Salyzynff2dcd92016-09-28 15:54:45 -070040#include <utils/Compat.h>
41#include <utils/FileMap.h>
Christopher Ferrise6884ce2015-11-10 14:55:12 -080042#include "ziparchive/zip_archive.h"
Dan Albert1ae07642015-04-09 14:11:18 -070043#include "zlib.h"
Narayan Kamath7462f022013-11-21 13:05:04 +000044
Narayan Kamath044bc8e2014-12-03 18:22:53 +000045#include "entry_name_utils-inl.h"
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -070046#include "zip_archive_common.h"
Christopher Ferrise6884ce2015-11-10 14:55:12 -080047#include "zip_archive_private.h"
Mark Salyzyn99ef9912014-03-14 14:26:22 -070048
Dan Albert1ae07642015-04-09 14:11:18 -070049using android::base::get_unaligned;
Narayan Kamath044bc8e2014-12-03 18:22:53 +000050
Narayan Kamath926973e2014-06-09 14:18:14 +010051// This is for windows. If we don't open a file in binary mode, weird
Narayan Kamath7462f022013-11-21 13:05:04 +000052// things will happen.
53#ifndef O_BINARY
54#define O_BINARY 0
55#endif
56
Narayan Kamath926973e2014-06-09 14:18:14 +010057// The maximum number of bytes to scan backwards for the EOCD start.
58static const uint32_t kMaxEOCDSearch = kMaxCommentLen + sizeof(EocdRecord);
59
Narayan Kamath7462f022013-11-21 13:05:04 +000060static const char* kErrorMessages[] = {
61 "Unknown return code.",
Narayan Kamatheb41ad22013-12-09 16:26:36 +000062 "Iteration ended",
Narayan Kamath7462f022013-11-21 13:05:04 +000063 "Zlib error",
64 "Invalid file",
65 "Invalid handle",
66 "Duplicate entries in archive",
67 "Empty archive",
68 "Entry not found",
69 "Invalid offset",
70 "Inconsistent information",
71 "Invalid entry name",
Narayan Kamatheb41ad22013-12-09 16:26:36 +000072 "I/O Error",
Narayan Kamatheaf98852013-12-11 14:51:51 +000073 "File mapping failed"
Narayan Kamath7462f022013-11-21 13:05:04 +000074};
75
76static const int32_t kErrorMessageUpperBound = 0;
77
Narayan Kamatheb41ad22013-12-09 16:26:36 +000078static const int32_t kIterationEnd = -1;
Narayan Kamath7462f022013-11-21 13:05:04 +000079
80// We encountered a Zlib error when inflating a stream from this file.
81// Usually indicates file corruption.
82static const int32_t kZlibError = -2;
83
84// The input file cannot be processed as a zip archive. Usually because
85// it's too small, too large or does not have a valid signature.
86static const int32_t kInvalidFile = -3;
87
88// An invalid iteration / ziparchive handle was passed in as an input
89// argument.
90static const int32_t kInvalidHandle = -4;
91
92// The zip archive contained two (or possibly more) entries with the same
93// name.
94static const int32_t kDuplicateEntry = -5;
95
96// The zip archive contains no entries.
97static const int32_t kEmptyArchive = -6;
98
99// The specified entry was not found in the archive.
100static const int32_t kEntryNotFound = -7;
101
102// The zip archive contained an invalid local file header pointer.
103static const int32_t kInvalidOffset = -8;
104
105// The zip archive contained inconsistent entry information. This could
106// be because the central directory & local file header did not agree, or
107// if the actual uncompressed length or crc32 do not match their declared
108// values.
109static const int32_t kInconsistentInformation = -9;
110
111// An invalid entry name was encountered.
112static const int32_t kInvalidEntryName = -10;
113
Narayan Kamatheb41ad22013-12-09 16:26:36 +0000114// An I/O related system call (read, lseek, ftruncate, map) failed.
115static const int32_t kIoError = -11;
Narayan Kamath7462f022013-11-21 13:05:04 +0000116
Narayan Kamatheaf98852013-12-11 14:51:51 +0000117// We were not able to mmap the central directory or entry contents.
118static const int32_t kMmapFailed = -12;
Narayan Kamath7462f022013-11-21 13:05:04 +0000119
Narayan Kamatheaf98852013-12-11 14:51:51 +0000120static const int32_t kErrorMessageLowerBound = -13;
Narayan Kamath7462f022013-11-21 13:05:04 +0000121
Narayan Kamath7462f022013-11-21 13:05:04 +0000122/*
123 * A Read-only Zip archive.
124 *
125 * We want "open" and "find entry by name" to be fast operations, and
126 * we want to use as little memory as possible. We memory-map the zip
127 * central directory, and load a hash table with pointers to the filenames
128 * (which aren't null-terminated). The other fields are at a fixed offset
129 * from the filename, so we don't need to extract those (but we do need
130 * to byte-read and endian-swap them every time we want them).
131 *
132 * It's possible that somebody has handed us a massive (~1GB) zip archive,
133 * so we can't expect to mmap the entire file.
134 *
135 * To speed comparisons when doing a lookup by name, we could make the mapping
136 * "private" (copy-on-write) and null-terminate the filenames after verifying
137 * the record structure. However, this requires a private mapping of
138 * every page that the Central Directory touches. Easier to tuck a copy
139 * of the string length into the hash table entry.
140 */
Narayan Kamath7462f022013-11-21 13:05:04 +0000141
Narayan Kamath7462f022013-11-21 13:05:04 +0000142/*
143 * Round up to the next highest power of 2.
144 *
145 * Found on http://graphics.stanford.edu/~seander/bithacks.html.
146 */
147static uint32_t RoundUpPower2(uint32_t val) {
148 val--;
149 val |= val >> 1;
150 val |= val >> 2;
151 val |= val >> 4;
152 val |= val >> 8;
153 val |= val >> 16;
154 val++;
155
156 return val;
157}
158
Yusuke Sato07447542015-06-25 14:39:19 -0700159static uint32_t ComputeHash(const ZipString& name) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000160 uint32_t hash = 0;
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100161 uint16_t len = name.name_length;
162 const uint8_t* str = name.name;
Narayan Kamath7462f022013-11-21 13:05:04 +0000163
164 while (len--) {
165 hash = hash * 31 + *str++;
166 }
167
168 return hash;
169}
170
171/*
172 * Convert a ZipEntry to a hash table index, verifying that it's in a
173 * valid range.
174 */
Yusuke Sato07447542015-06-25 14:39:19 -0700175static int64_t EntryToIndex(const ZipString* hash_table,
Narayan Kamath7462f022013-11-21 13:05:04 +0000176 const uint32_t hash_table_size,
Yusuke Sato07447542015-06-25 14:39:19 -0700177 const ZipString& name) {
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100178 const uint32_t hash = ComputeHash(name);
Narayan Kamath7462f022013-11-21 13:05:04 +0000179
180 // NOTE: (hash_table_size - 1) is guaranteed to be non-negative.
181 uint32_t ent = hash & (hash_table_size - 1);
182 while (hash_table[ent].name != NULL) {
Yusuke Sato07447542015-06-25 14:39:19 -0700183 if (hash_table[ent] == name) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000184 return ent;
185 }
186
187 ent = (ent + 1) & (hash_table_size - 1);
188 }
189
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100190 ALOGV("Zip: Unable to find entry %.*s", name.name_length, name.name);
Narayan Kamath7462f022013-11-21 13:05:04 +0000191 return kEntryNotFound;
192}
193
194/*
195 * Add a new entry to the hash table.
196 */
Yusuke Sato07447542015-06-25 14:39:19 -0700197static int32_t AddToHash(ZipString *hash_table, const uint64_t hash_table_size,
198 const ZipString& name) {
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100199 const uint64_t hash = ComputeHash(name);
Narayan Kamath7462f022013-11-21 13:05:04 +0000200 uint32_t ent = hash & (hash_table_size - 1);
201
202 /*
203 * We over-allocated the table, so we're guaranteed to find an empty slot.
204 * Further, we guarantee that the hashtable size is not 0.
205 */
206 while (hash_table[ent].name != NULL) {
Yusuke Sato07447542015-06-25 14:39:19 -0700207 if (hash_table[ent] == name) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000208 // We've found a duplicate entry. We don't accept it
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100209 ALOGW("Zip: Found duplicate entry %.*s", name.name_length, name.name);
Narayan Kamath7462f022013-11-21 13:05:04 +0000210 return kDuplicateEntry;
211 }
212 ent = (ent + 1) & (hash_table_size - 1);
213 }
214
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100215 hash_table[ent].name = name.name;
216 hash_table[ent].name_length = name.name_length;
Narayan Kamath7462f022013-11-21 13:05:04 +0000217 return 0;
218}
219
Tianjie Xu18c25922016-09-29 15:27:41 -0700220static int32_t MapCentralDirectory0(const char* debug_file_name, ZipArchive* archive,
221 off64_t file_length, off64_t read_amount,
222 uint8_t* scan_buffer) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000223 const off64_t search_start = file_length - read_amount;
224
Tianjie Xu18c25922016-09-29 15:27:41 -0700225 if(!archive->mapped_zip.ReadAtOffset(scan_buffer, read_amount, search_start)) {
226 ALOGE("Zip: read %" PRId64 " from offset %" PRId64 " failed",
227 static_cast<int64_t>(read_amount), static_cast<int64_t>(search_start));
Narayan Kamath7462f022013-11-21 13:05:04 +0000228 return kIoError;
229 }
230
231 /*
232 * Scan backward for the EOCD magic. In an archive without a trailing
233 * comment, we'll find it on the first try. (We may want to consider
234 * doing an initial minimal read; if we don't find it, retry with a
235 * second read as above.)
236 */
Narayan Kamath926973e2014-06-09 14:18:14 +0100237 int i = read_amount - sizeof(EocdRecord);
238 for (; i >= 0; i--) {
Dan Albert1ae07642015-04-09 14:11:18 -0700239 if (scan_buffer[i] == 0x50) {
240 uint32_t* sig_addr = reinterpret_cast<uint32_t*>(&scan_buffer[i]);
241 if (get_unaligned<uint32_t>(sig_addr) == EocdRecord::kSignature) {
242 ALOGV("+++ Found EOCD at buf+%d", i);
243 break;
244 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000245 }
246 }
247 if (i < 0) {
248 ALOGD("Zip: EOCD not found, %s is not zip", debug_file_name);
249 return kInvalidFile;
250 }
251
252 const off64_t eocd_offset = search_start + i;
Narayan Kamath926973e2014-06-09 14:18:14 +0100253 const EocdRecord* eocd = reinterpret_cast<const EocdRecord*>(scan_buffer + i);
Narayan Kamath7462f022013-11-21 13:05:04 +0000254 /*
Narayan Kamath926973e2014-06-09 14:18:14 +0100255 * Verify that there's no trailing space at the end of the central directory
256 * and its comment.
Narayan Kamath7462f022013-11-21 13:05:04 +0000257 */
Narayan Kamath926973e2014-06-09 14:18:14 +0100258 const off64_t calculated_length = eocd_offset + sizeof(EocdRecord)
259 + eocd->comment_length;
260 if (calculated_length != file_length) {
Narayan Kamath4f6b4992014-06-03 13:59:23 +0100261 ALOGW("Zip: %" PRId64 " extraneous bytes at the end of the central directory",
Narayan Kamath926973e2014-06-09 14:18:14 +0100262 static_cast<int64_t>(file_length - calculated_length));
Narayan Kamath4f6b4992014-06-03 13:59:23 +0100263 return kInvalidFile;
264 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000265
Narayan Kamath926973e2014-06-09 14:18:14 +0100266 /*
267 * Grab the CD offset and size, and the number of entries in the
268 * archive and verify that they look reasonable.
269 */
Tianjie Xuae8180c2016-09-21 14:58:11 -0700270 if (static_cast<off64_t>(eocd->cd_start_offset) + eocd->cd_size > eocd_offset) {
Narayan Kamath926973e2014-06-09 14:18:14 +0100271 ALOGW("Zip: bad offsets (dir %" PRIu32 ", size %" PRIu32 ", eocd %" PRId64 ")",
272 eocd->cd_start_offset, eocd->cd_size, static_cast<int64_t>(eocd_offset));
Tianjie Xuae8180c2016-09-21 14:58:11 -0700273#if defined(__ANDROID__)
274 if (eocd->cd_start_offset + eocd->cd_size <= eocd_offset) {
275 android_errorWriteLog(0x534e4554, "31251826");
276 }
277#endif
Narayan Kamath7462f022013-11-21 13:05:04 +0000278 return kInvalidOffset;
279 }
Narayan Kamath926973e2014-06-09 14:18:14 +0100280 if (eocd->num_records == 0) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000281 ALOGW("Zip: empty archive?");
282 return kEmptyArchive;
283 }
284
Elliott Hughese49236b2015-06-04 15:21:59 -0700285 ALOGV("+++ num_entries=%" PRIu32 " dir_size=%" PRIu32 " dir_offset=%" PRIu32,
Narayan Kamath926973e2014-06-09 14:18:14 +0100286 eocd->num_records, eocd->cd_size, eocd->cd_start_offset);
Narayan Kamath7462f022013-11-21 13:05:04 +0000287
288 /*
289 * It all looks good. Create a mapping for the CD, and set the fields
290 * in archive.
291 */
Tianjie Xu18c25922016-09-29 15:27:41 -0700292
293 if (!archive->InitializeCentralDirectory(debug_file_name,
294 static_cast<off64_t>(eocd->cd_start_offset),
295 static_cast<size_t>(eocd->cd_size))) {
296 ALOGE("Zip: failed to intialize central directory.\n");
Narayan Kamatheaf98852013-12-11 14:51:51 +0000297 return kMmapFailed;
Narayan Kamath7462f022013-11-21 13:05:04 +0000298 }
299
Narayan Kamath926973e2014-06-09 14:18:14 +0100300 archive->num_entries = eocd->num_records;
301 archive->directory_offset = eocd->cd_start_offset;
Narayan Kamath7462f022013-11-21 13:05:04 +0000302
303 return 0;
304}
305
306/*
307 * Find the zip Central Directory and memory-map it.
308 *
309 * On success, returns 0 after populating fields from the EOCD area:
310 * directory_offset
Tianjie Xu18c25922016-09-29 15:27:41 -0700311 * directory_ptr
Narayan Kamath7462f022013-11-21 13:05:04 +0000312 * num_entries
313 */
Tianjie Xu18c25922016-09-29 15:27:41 -0700314static int32_t MapCentralDirectory(const char* debug_file_name, ZipArchive* archive) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000315
316 // Test file length. We use lseek64 to make sure the file
317 // is small enough to be a zip file (Its size must be less than
318 // 0xffffffff bytes).
Tianjie Xu18c25922016-09-29 15:27:41 -0700319 off64_t file_length = archive->mapped_zip.GetFileLength();
Narayan Kamath7462f022013-11-21 13:05:04 +0000320 if (file_length == -1) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000321 return kInvalidFile;
322 }
323
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800324 if (file_length > static_cast<off64_t>(0xffffffff)) {
Narayan Kamath926973e2014-06-09 14:18:14 +0100325 ALOGV("Zip: zip file too long %" PRId64, static_cast<int64_t>(file_length));
Narayan Kamath7462f022013-11-21 13:05:04 +0000326 return kInvalidFile;
327 }
328
Narayan Kamath926973e2014-06-09 14:18:14 +0100329 if (file_length < static_cast<off64_t>(sizeof(EocdRecord))) {
330 ALOGV("Zip: length %" PRId64 " is too small to be zip", static_cast<int64_t>(file_length));
Narayan Kamath7462f022013-11-21 13:05:04 +0000331 return kInvalidFile;
332 }
333
334 /*
335 * Perform the traditional EOCD snipe hunt.
336 *
337 * We're searching for the End of Central Directory magic number,
338 * which appears at the start of the EOCD block. It's followed by
339 * 18 bytes of EOCD stuff and up to 64KB of archive comment. We
340 * need to read the last part of the file into a buffer, dig through
341 * it to find the magic number, parse some values out, and use those
342 * to determine the extent of the CD.
343 *
344 * We start by pulling in the last part of the file.
345 */
Narayan Kamath926973e2014-06-09 14:18:14 +0100346 off64_t read_amount = kMaxEOCDSearch;
347 if (file_length < read_amount) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000348 read_amount = file_length;
349 }
350
Tianjie Xu18c25922016-09-29 15:27:41 -0700351 std::vector<uint8_t> scan_buffer(read_amount);
352 int32_t result = MapCentralDirectory0(debug_file_name, archive, file_length, read_amount,
353 scan_buffer.data());
Narayan Kamath7462f022013-11-21 13:05:04 +0000354 return result;
355}
356
357/*
358 * Parses the Zip archive's Central Directory. Allocates and populates the
359 * hash table.
360 *
361 * Returns 0 on success.
362 */
363static int32_t ParseZipArchive(ZipArchive* archive) {
Tianjie Xu18c25922016-09-29 15:27:41 -0700364 const uint8_t* const cd_ptr = archive->central_directory.GetBasePtr();
365 const size_t cd_length = archive->central_directory.GetMapLength();
Narayan Kamath926973e2014-06-09 14:18:14 +0100366 const uint16_t num_entries = archive->num_entries;
Narayan Kamath7462f022013-11-21 13:05:04 +0000367
368 /*
369 * Create hash table. We have a minimum 75% load factor, possibly as
370 * low as 50% after we round off to a power of 2. There must be at
371 * least one unused entry to avoid an infinite loop during creation.
372 */
373 archive->hash_table_size = RoundUpPower2(1 + (num_entries * 4) / 3);
Yusuke Sato07447542015-06-25 14:39:19 -0700374 archive->hash_table = reinterpret_cast<ZipString*>(calloc(archive->hash_table_size,
375 sizeof(ZipString)));
Tianjie Xubcc44312016-10-10 12:11:30 -0700376 if (archive->hash_table == nullptr) {
377 ALOGW("Zip: unable to allocate the %u-entry hash_table, entry size: %zu",
378 archive->hash_table_size, sizeof(ZipString));
379 return -1;
380 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000381
382 /*
383 * Walk through the central directory, adding entries to the hash
384 * table and verifying values.
385 */
Narayan Kamath926973e2014-06-09 14:18:14 +0100386 const uint8_t* const cd_end = cd_ptr + cd_length;
Narayan Kamath7462f022013-11-21 13:05:04 +0000387 const uint8_t* ptr = cd_ptr;
388 for (uint16_t i = 0; i < num_entries; i++) {
Tianjie Xud9fd1862017-04-05 14:46:27 -0700389 if (ptr > cd_end - sizeof(CentralDirectoryRecord)) {
390 ALOGW("Zip: ran off the end (at %" PRIu16 ")", i);
391#if defined(__ANDROID__)
392 android_errorWriteLog(0x534e4554, "36392138");
393#endif
394 return -1;
395 }
396
Narayan Kamath926973e2014-06-09 14:18:14 +0100397 const CentralDirectoryRecord* cdr =
398 reinterpret_cast<const CentralDirectoryRecord*>(ptr);
399 if (cdr->record_signature != CentralDirectoryRecord::kSignature) {
Mark Salyzyn088bf902014-05-08 16:02:20 -0700400 ALOGW("Zip: missed a central dir sig (at %" PRIu16 ")", i);
Dmitriy Ivanov3ea93da2015-03-06 11:48:47 -0800401 return -1;
Narayan Kamath7462f022013-11-21 13:05:04 +0000402 }
403
Narayan Kamath926973e2014-06-09 14:18:14 +0100404 const off64_t local_header_offset = cdr->local_file_header_offset;
Narayan Kamath7462f022013-11-21 13:05:04 +0000405 if (local_header_offset >= archive->directory_offset) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800406 ALOGW("Zip: bad LFH offset %" PRId64 " at entry %" PRIu16,
407 static_cast<int64_t>(local_header_offset), i);
Dmitriy Ivanov3ea93da2015-03-06 11:48:47 -0800408 return -1;
Narayan Kamath7462f022013-11-21 13:05:04 +0000409 }
410
Narayan Kamath926973e2014-06-09 14:18:14 +0100411 const uint16_t file_name_length = cdr->file_name_length;
412 const uint16_t extra_length = cdr->extra_field_length;
413 const uint16_t comment_length = cdr->comment_length;
Piotr Jastrzebski78271ba2014-08-15 12:53:00 +0100414 const uint8_t* file_name = ptr + sizeof(CentralDirectoryRecord);
415
Tianjie Xubcc44312016-10-10 12:11:30 -0700416 if (file_name + file_name_length > cd_end) {
417 ALOGW("Zip: file name boundary exceeds the central directory range, file_name_length: "
418 "%" PRIx16 ", cd_length: %zu", file_name_length, cd_length);
419 return -1;
420 }
Narayan Kamath044bc8e2014-12-03 18:22:53 +0000421 /* check that file name is valid UTF-8 and doesn't contain NUL (U+0000) characters */
422 if (!IsValidEntryName(file_name, file_name_length)) {
Dmitriy Ivanov3ea93da2015-03-06 11:48:47 -0800423 return -1;
Piotr Jastrzebski78271ba2014-08-15 12:53:00 +0100424 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000425
426 /* add the CDE filename to the hash table */
Yusuke Sato07447542015-06-25 14:39:19 -0700427 ZipString entry_name;
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100428 entry_name.name = file_name;
429 entry_name.name_length = file_name_length;
Narayan Kamath7462f022013-11-21 13:05:04 +0000430 const int add_result = AddToHash(archive->hash_table,
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100431 archive->hash_table_size, entry_name);
Dmitriy Ivanov3ea93da2015-03-06 11:48:47 -0800432 if (add_result != 0) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000433 ALOGW("Zip: Error adding entry to hash table %d", add_result);
Dmitriy Ivanov3ea93da2015-03-06 11:48:47 -0800434 return add_result;
Narayan Kamath7462f022013-11-21 13:05:04 +0000435 }
436
Narayan Kamath926973e2014-06-09 14:18:14 +0100437 ptr += sizeof(CentralDirectoryRecord) + file_name_length + extra_length + comment_length;
438 if ((ptr - cd_ptr) > static_cast<int64_t>(cd_length)) {
Mark Salyzyn088bf902014-05-08 16:02:20 -0700439 ALOGW("Zip: bad CD advance (%tu vs %zu) at entry %" PRIu16,
440 ptr - cd_ptr, cd_length, i);
Dmitriy Ivanov3ea93da2015-03-06 11:48:47 -0800441 return -1;
Narayan Kamath7462f022013-11-21 13:05:04 +0000442 }
443 }
Narayan Kamath9dced162017-08-09 18:32:09 +0100444
445 uint32_t lfh_start_bytes;
446 if (!archive->mapped_zip.ReadAtOffset(reinterpret_cast<uint8_t*>(&lfh_start_bytes),
447 sizeof(uint32_t), 0)) {
448 ALOGW("Zip: Unable to read header for entry at offset == 0.");
449 return -1;
450 }
451
452 if (lfh_start_bytes != LocalFileHeader::kSignature) {
453 ALOGW("Zip: Entry at offset zero has invalid LFH signature %" PRIx32, lfh_start_bytes);
454#if defined(__ANDROID__)
455 android_errorWriteLog(0x534e4554, "64211847");
456#endif
457 return -1;
458 }
459
Mark Salyzyn088bf902014-05-08 16:02:20 -0700460 ALOGV("+++ zip good scan %" PRIu16 " entries", num_entries);
Narayan Kamath7462f022013-11-21 13:05:04 +0000461
Dmitriy Ivanov3ea93da2015-03-06 11:48:47 -0800462 return 0;
Narayan Kamath7462f022013-11-21 13:05:04 +0000463}
464
465static int32_t OpenArchiveInternal(ZipArchive* archive,
466 const char* debug_file_name) {
467 int32_t result = -1;
Tianjie Xu18c25922016-09-29 15:27:41 -0700468 if ((result = MapCentralDirectory(debug_file_name, archive)) != 0) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000469 return result;
470 }
471
472 if ((result = ParseZipArchive(archive))) {
473 return result;
474 }
475
476 return 0;
477}
478
479int32_t OpenArchiveFd(int fd, const char* debug_file_name,
Dmitriy Ivanov40b52b22014-07-15 19:33:00 -0700480 ZipArchiveHandle* handle, bool assume_ownership) {
481 ZipArchive* archive = new ZipArchive(fd, assume_ownership);
Narayan Kamath7462f022013-11-21 13:05:04 +0000482 *handle = archive;
Narayan Kamath7462f022013-11-21 13:05:04 +0000483 return OpenArchiveInternal(archive, debug_file_name);
484}
485
486int32_t OpenArchive(const char* fileName, ZipArchiveHandle* handle) {
Neil Fullerb1a113f2014-07-25 14:43:04 +0100487 const int fd = open(fileName, O_RDONLY | O_BINARY, 0);
Dmitriy Ivanov40b52b22014-07-15 19:33:00 -0700488 ZipArchive* archive = new ZipArchive(fd, true);
Narayan Kamath7462f022013-11-21 13:05:04 +0000489 *handle = archive;
490
Narayan Kamath7462f022013-11-21 13:05:04 +0000491 if (fd < 0) {
492 ALOGW("Unable to open '%s': %s", fileName, strerror(errno));
493 return kIoError;
Narayan Kamath7462f022013-11-21 13:05:04 +0000494 }
Dmitriy Ivanov40b52b22014-07-15 19:33:00 -0700495
Narayan Kamath7462f022013-11-21 13:05:04 +0000496 return OpenArchiveInternal(archive, fileName);
497}
498
Tianjie Xu18c25922016-09-29 15:27:41 -0700499int32_t OpenArchiveFromMemory(void* address, size_t length, const char* debug_file_name,
500 ZipArchiveHandle *handle) {
501 ZipArchive* archive = new ZipArchive(address, length);
502 *handle = archive;
503 return OpenArchiveInternal(archive, debug_file_name);
504}
505
Narayan Kamath7462f022013-11-21 13:05:04 +0000506/*
507 * Close a ZipArchive, closing the file and freeing the contents.
508 */
509void CloseArchive(ZipArchiveHandle handle) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800510 ZipArchive* archive = reinterpret_cast<ZipArchive*>(handle);
Narayan Kamath7462f022013-11-21 13:05:04 +0000511 ALOGV("Closing archive %p", archive);
Neil Fullerb1a113f2014-07-25 14:43:04 +0100512 delete archive;
Narayan Kamath7462f022013-11-21 13:05:04 +0000513}
514
Tianjie Xu18c25922016-09-29 15:27:41 -0700515static int32_t UpdateEntryFromDataDescriptor(MappedZipFile& mapped_zip,
Narayan Kamath7462f022013-11-21 13:05:04 +0000516 ZipEntry *entry) {
Narayan Kamath926973e2014-06-09 14:18:14 +0100517 uint8_t ddBuf[sizeof(DataDescriptor) + sizeof(DataDescriptor::kOptSignature)];
Tianjie Xu18c25922016-09-29 15:27:41 -0700518 if (!mapped_zip.ReadData(ddBuf, sizeof(ddBuf))) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000519 return kIoError;
520 }
521
Narayan Kamath926973e2014-06-09 14:18:14 +0100522 const uint32_t ddSignature = *(reinterpret_cast<const uint32_t*>(ddBuf));
523 const uint16_t offset = (ddSignature == DataDescriptor::kOptSignature) ? 4 : 0;
524 const DataDescriptor* descriptor = reinterpret_cast<const DataDescriptor*>(ddBuf + offset);
Narayan Kamath7462f022013-11-21 13:05:04 +0000525
Narayan Kamath926973e2014-06-09 14:18:14 +0100526 entry->crc32 = descriptor->crc32;
527 entry->compressed_length = descriptor->compressed_size;
528 entry->uncompressed_length = descriptor->uncompressed_size;
Narayan Kamath7462f022013-11-21 13:05:04 +0000529
530 return 0;
531}
532
Narayan Kamath7462f022013-11-21 13:05:04 +0000533static int32_t FindEntry(const ZipArchive* archive, const int ent,
534 ZipEntry* data) {
535 const uint16_t nameLen = archive->hash_table[ent].name_length;
Narayan Kamath7462f022013-11-21 13:05:04 +0000536
537 // Recover the start of the central directory entry from the filename
538 // pointer. The filename is the first entry past the fixed-size data,
539 // so we can just subtract back from that.
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100540 const uint8_t* ptr = archive->hash_table[ent].name;
Narayan Kamath926973e2014-06-09 14:18:14 +0100541 ptr -= sizeof(CentralDirectoryRecord);
Narayan Kamath7462f022013-11-21 13:05:04 +0000542
543 // This is the base of our mmapped region, we have to sanity check that
544 // the name that's in the hash table is a pointer to a location within
545 // this mapped region.
Tianjie Xu18c25922016-09-29 15:27:41 -0700546 const uint8_t* base_ptr = archive->central_directory.GetBasePtr();
547 if (ptr < base_ptr || ptr > base_ptr + archive->central_directory.GetMapLength()) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000548 ALOGW("Zip: Invalid entry pointer");
549 return kInvalidOffset;
550 }
551
Narayan Kamath926973e2014-06-09 14:18:14 +0100552 const CentralDirectoryRecord *cdr =
553 reinterpret_cast<const CentralDirectoryRecord*>(ptr);
554
Narayan Kamath7462f022013-11-21 13:05:04 +0000555 // The offset of the start of the central directory in the zipfile.
556 // We keep this lying around so that we can sanity check all our lengths
557 // and our per-file structures.
558 const off64_t cd_offset = archive->directory_offset;
559
560 // Fill out the compression method, modification time, crc32
561 // and other interesting attributes from the central directory. These
562 // will later be compared against values from the local file header.
Narayan Kamath926973e2014-06-09 14:18:14 +0100563 data->method = cdr->compression_method;
beonit0e99a2f2015-07-18 02:08:16 +0900564 data->mod_time = cdr->last_mod_date << 16 | cdr->last_mod_time;
Narayan Kamath926973e2014-06-09 14:18:14 +0100565 data->crc32 = cdr->crc32;
566 data->compressed_length = cdr->compressed_size;
567 data->uncompressed_length = cdr->uncompressed_size;
Narayan Kamath7462f022013-11-21 13:05:04 +0000568
569 // Figure out the local header offset from the central directory. The
570 // actual file data will begin after the local header and the name /
571 // extra comments.
Narayan Kamath926973e2014-06-09 14:18:14 +0100572 const off64_t local_header_offset = cdr->local_file_header_offset;
573 if (local_header_offset + static_cast<off64_t>(sizeof(LocalFileHeader)) >= cd_offset) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000574 ALOGW("Zip: bad local hdr offset in zip");
575 return kInvalidOffset;
576 }
577
Narayan Kamath926973e2014-06-09 14:18:14 +0100578 uint8_t lfh_buf[sizeof(LocalFileHeader)];
Tianjie Xu18c25922016-09-29 15:27:41 -0700579 if (!archive->mapped_zip.ReadAtOffset(lfh_buf, sizeof(lfh_buf), local_header_offset)) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800580 ALOGW("Zip: failed reading lfh name from offset %" PRId64,
581 static_cast<int64_t>(local_header_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000582 return kIoError;
583 }
584
Narayan Kamath926973e2014-06-09 14:18:14 +0100585 const LocalFileHeader *lfh = reinterpret_cast<const LocalFileHeader*>(lfh_buf);
586
587 if (lfh->lfh_signature != LocalFileHeader::kSignature) {
Mark Salyzyn99ef9912014-03-14 14:26:22 -0700588 ALOGW("Zip: didn't find signature at start of lfh, offset=%" PRId64,
Narayan Kamath926973e2014-06-09 14:18:14 +0100589 static_cast<int64_t>(local_header_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000590 return kInvalidOffset;
591 }
592
593 // Paranoia: Match the values specified in the local file header
594 // to those specified in the central directory.
Adam Lesinskie0eca552017-04-06 18:55:47 -0700595
Adam Lesinski73b3aa52017-04-10 12:08:22 -0700596 // Verify that the central directory and local file header have the same general purpose bit
597 // flags set.
598 if (lfh->gpb_flags != cdr->gpb_flags) {
Adam Lesinskie0eca552017-04-06 18:55:47 -0700599 ALOGW("Zip: gpb flag mismatch. expected {%04" PRIx16 "}, was {%04" PRIx16 "}",
600 cdr->gpb_flags, lfh->gpb_flags);
601 return kInconsistentInformation;
602 }
603
604 // If there is no trailing data descriptor, verify that the central directory and local file
605 // header agree on the crc, compressed, and uncompressed sizes of the entry.
Narayan Kamath926973e2014-06-09 14:18:14 +0100606 if ((lfh->gpb_flags & kGPBDDFlagMask) == 0) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000607 data->has_data_descriptor = 0;
Narayan Kamath926973e2014-06-09 14:18:14 +0100608 if (data->compressed_length != lfh->compressed_size
609 || data->uncompressed_length != lfh->uncompressed_size
610 || data->crc32 != lfh->crc32) {
Mark Salyzyn088bf902014-05-08 16:02:20 -0700611 ALOGW("Zip: size/crc32 mismatch. expected {%" PRIu32 ", %" PRIu32
612 ", %" PRIx32 "}, was {%" PRIu32 ", %" PRIu32 ", %" PRIx32 "}",
Narayan Kamath7462f022013-11-21 13:05:04 +0000613 data->compressed_length, data->uncompressed_length, data->crc32,
Narayan Kamath926973e2014-06-09 14:18:14 +0100614 lfh->compressed_size, lfh->uncompressed_size, lfh->crc32);
Narayan Kamath7462f022013-11-21 13:05:04 +0000615 return kInconsistentInformation;
616 }
617 } else {
618 data->has_data_descriptor = 1;
619 }
620
621 // Check that the local file header name matches the declared
622 // name in the central directory.
Narayan Kamath926973e2014-06-09 14:18:14 +0100623 if (lfh->file_name_length == nameLen) {
624 const off64_t name_offset = local_header_offset + sizeof(LocalFileHeader);
Mykola Kondratenko50afc152014-09-08 12:46:37 +0200625 if (name_offset + lfh->file_name_length > cd_offset) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000626 ALOGW("Zip: Invalid declared length");
627 return kInvalidOffset;
628 }
629
Tianjie Xu18c25922016-09-29 15:27:41 -0700630 std::vector<uint8_t> name_buf(nameLen);
631 if (!archive->mapped_zip.ReadAtOffset(name_buf.data(), nameLen, name_offset)) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800632 ALOGW("Zip: failed reading lfh name from offset %" PRId64, static_cast<int64_t>(name_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000633 return kIoError;
634 }
635
Tianjie Xu18c25922016-09-29 15:27:41 -0700636 if (memcmp(archive->hash_table[ent].name, name_buf.data(), nameLen)) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000637 return kInconsistentInformation;
638 }
639
Narayan Kamath7462f022013-11-21 13:05:04 +0000640 } else {
641 ALOGW("Zip: lfh name did not match central directory.");
642 return kInconsistentInformation;
643 }
644
Narayan Kamath926973e2014-06-09 14:18:14 +0100645 const off64_t data_offset = local_header_offset + sizeof(LocalFileHeader)
646 + lfh->file_name_length + lfh->extra_field_length;
Narayan Kamath48953a12014-01-24 12:32:39 +0000647 if (data_offset > cd_offset) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800648 ALOGW("Zip: bad data offset %" PRId64 " in zip", static_cast<int64_t>(data_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000649 return kInvalidOffset;
650 }
651
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800652 if (static_cast<off64_t>(data_offset + data->compressed_length) > cd_offset) {
Mark Salyzyn088bf902014-05-08 16:02:20 -0700653 ALOGW("Zip: bad compressed length in zip (%" PRId64 " + %" PRIu32 " > %" PRId64 ")",
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800654 static_cast<int64_t>(data_offset), data->compressed_length, static_cast<int64_t>(cd_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000655 return kInvalidOffset;
656 }
657
658 if (data->method == kCompressStored &&
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800659 static_cast<off64_t>(data_offset + data->uncompressed_length) > cd_offset) {
Mark Salyzyn088bf902014-05-08 16:02:20 -0700660 ALOGW("Zip: bad uncompressed length in zip (%" PRId64 " + %" PRIu32 " > %" PRId64 ")",
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800661 static_cast<int64_t>(data_offset), data->uncompressed_length,
662 static_cast<int64_t>(cd_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000663 return kInvalidOffset;
664 }
665
666 data->offset = data_offset;
667 return 0;
668}
669
670struct IterationHandle {
671 uint32_t position;
Piotr Jastrzebski10aa9a02014-08-19 09:01:20 +0100672 // We're not using vector here because this code is used in the Windows SDK
673 // where the STL is not available.
Yusuke Sato07447542015-06-25 14:39:19 -0700674 ZipString prefix;
675 ZipString suffix;
Narayan Kamath7462f022013-11-21 13:05:04 +0000676 ZipArchive* archive;
Piotr Jastrzebski8e085362014-08-18 11:37:45 +0100677
Yusuke Sato07447542015-06-25 14:39:19 -0700678 IterationHandle(const ZipString* in_prefix,
679 const ZipString* in_suffix) {
680 if (in_prefix) {
681 uint8_t* name_copy = new uint8_t[in_prefix->name_length];
682 memcpy(name_copy, in_prefix->name, in_prefix->name_length);
683 prefix.name = name_copy;
684 prefix.name_length = in_prefix->name_length;
685 } else {
686 prefix.name = NULL;
687 prefix.name_length = 0;
Yusuke Satof1d3d3b2015-06-25 14:09:00 -0700688 }
Yusuke Sato07447542015-06-25 14:39:19 -0700689 if (in_suffix) {
690 uint8_t* name_copy = new uint8_t[in_suffix->name_length];
691 memcpy(name_copy, in_suffix->name, in_suffix->name_length);
692 suffix.name = name_copy;
693 suffix.name_length = in_suffix->name_length;
694 } else {
695 suffix.name = NULL;
696 suffix.name_length = 0;
Yusuke Satof1d3d3b2015-06-25 14:09:00 -0700697 }
Piotr Jastrzebski8e085362014-08-18 11:37:45 +0100698 }
699
700 ~IterationHandle() {
Yusuke Sato07447542015-06-25 14:39:19 -0700701 delete[] prefix.name;
702 delete[] suffix.name;
Piotr Jastrzebski8e085362014-08-18 11:37:45 +0100703 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000704};
705
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100706int32_t StartIteration(ZipArchiveHandle handle, void** cookie_ptr,
Yusuke Sato07447542015-06-25 14:39:19 -0700707 const ZipString* optional_prefix,
708 const ZipString* optional_suffix) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800709 ZipArchive* archive = reinterpret_cast<ZipArchive*>(handle);
Narayan Kamath7462f022013-11-21 13:05:04 +0000710
711 if (archive == NULL || archive->hash_table == NULL) {
712 ALOGW("Zip: Invalid ZipArchiveHandle");
713 return kInvalidHandle;
714 }
715
Yusuke Satof1d3d3b2015-06-25 14:09:00 -0700716 IterationHandle* cookie = new IterationHandle(optional_prefix, optional_suffix);
Narayan Kamath7462f022013-11-21 13:05:04 +0000717 cookie->position = 0;
Narayan Kamath7462f022013-11-21 13:05:04 +0000718 cookie->archive = archive;
Narayan Kamath7462f022013-11-21 13:05:04 +0000719
720 *cookie_ptr = cookie ;
721 return 0;
722}
723
Piotr Jastrzebski79c8b342014-08-08 14:02:17 +0100724void EndIteration(void* cookie) {
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100725 delete reinterpret_cast<IterationHandle*>(cookie);
Piotr Jastrzebski79c8b342014-08-08 14:02:17 +0100726}
727
Yusuke Sato07447542015-06-25 14:39:19 -0700728int32_t FindEntry(const ZipArchiveHandle handle, const ZipString& entryName,
Narayan Kamath7462f022013-11-21 13:05:04 +0000729 ZipEntry* data) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800730 const ZipArchive* archive = reinterpret_cast<ZipArchive*>(handle);
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100731 if (entryName.name_length == 0) {
732 ALOGW("Zip: Invalid filename %.*s", entryName.name_length, entryName.name);
Narayan Kamath7462f022013-11-21 13:05:04 +0000733 return kInvalidEntryName;
734 }
735
736 const int64_t ent = EntryToIndex(archive->hash_table,
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100737 archive->hash_table_size, entryName);
Narayan Kamath7462f022013-11-21 13:05:04 +0000738
739 if (ent < 0) {
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100740 ALOGV("Zip: Could not find entry %.*s", entryName.name_length, entryName.name);
Narayan Kamath7462f022013-11-21 13:05:04 +0000741 return ent;
742 }
743
744 return FindEntry(archive, ent, data);
745}
746
Yusuke Sato07447542015-06-25 14:39:19 -0700747int32_t Next(void* cookie, ZipEntry* data, ZipString* name) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800748 IterationHandle* handle = reinterpret_cast<IterationHandle*>(cookie);
Narayan Kamath7462f022013-11-21 13:05:04 +0000749 if (handle == NULL) {
750 return kInvalidHandle;
751 }
752
753 ZipArchive* archive = handle->archive;
754 if (archive == NULL || archive->hash_table == NULL) {
755 ALOGW("Zip: Invalid ZipArchiveHandle");
756 return kInvalidHandle;
757 }
758
759 const uint32_t currentOffset = handle->position;
760 const uint32_t hash_table_length = archive->hash_table_size;
Yusuke Sato07447542015-06-25 14:39:19 -0700761 const ZipString* hash_table = archive->hash_table;
Narayan Kamath7462f022013-11-21 13:05:04 +0000762
763 for (uint32_t i = currentOffset; i < hash_table_length; ++i) {
764 if (hash_table[i].name != NULL &&
Yusuke Sato07447542015-06-25 14:39:19 -0700765 (handle->prefix.name_length == 0 ||
766 hash_table[i].StartsWith(handle->prefix)) &&
767 (handle->suffix.name_length == 0 ||
768 hash_table[i].EndsWith(handle->suffix))) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000769 handle->position = (i + 1);
770 const int error = FindEntry(archive, i, data);
771 if (!error) {
772 name->name = hash_table[i].name;
773 name->name_length = hash_table[i].name_length;
774 }
775
776 return error;
777 }
778 }
779
780 handle->position = 0;
781 return kIterationEnd;
782}
783
Narayan Kamathf899bd52015-04-17 11:53:14 +0100784class Writer {
785 public:
786 virtual bool Append(uint8_t* buf, size_t buf_size) = 0;
787 virtual ~Writer() {}
788 protected:
789 Writer() = default;
790 private:
791 DISALLOW_COPY_AND_ASSIGN(Writer);
792};
793
794// A Writer that writes data to a fixed size memory region.
795// The size of the memory region must be equal to the total size of
796// the data appended to it.
797class MemoryWriter : public Writer {
798 public:
799 MemoryWriter(uint8_t* buf, size_t size) : Writer(),
800 buf_(buf), size_(size), bytes_written_(0) {
801 }
802
803 virtual bool Append(uint8_t* buf, size_t buf_size) override {
804 if (bytes_written_ + buf_size > size_) {
805 ALOGW("Zip: Unexpected size " ZD " (declared) vs " ZD " (actual)",
806 size_, bytes_written_ + buf_size);
807 return false;
808 }
809
810 memcpy(buf_ + bytes_written_, buf, buf_size);
811 bytes_written_ += buf_size;
812 return true;
813 }
814
815 private:
816 uint8_t* const buf_;
817 const size_t size_;
818 size_t bytes_written_;
819};
820
821// A Writer that appends data to a file |fd| at its current position.
822// The file will be truncated to the end of the written data.
823class FileWriter : public Writer {
824 public:
825
826 // Creates a FileWriter for |fd| and prepare to write |entry| to it,
827 // guaranteeing that the file descriptor is valid and that there's enough
828 // space on the volume to write out the entry completely and that the file
Tao Baoa456c212016-11-15 10:08:07 -0800829 // is truncated to the correct length (no truncation if |fd| references a
830 // block device).
Narayan Kamathf899bd52015-04-17 11:53:14 +0100831 //
832 // Returns a valid FileWriter on success, |nullptr| if an error occurred.
833 static std::unique_ptr<FileWriter> Create(int fd, const ZipEntry* entry) {
834 const uint32_t declared_length = entry->uncompressed_length;
835 const off64_t current_offset = lseek64(fd, 0, SEEK_CUR);
836 if (current_offset == -1) {
837 ALOGW("Zip: unable to seek to current location on fd %d: %s", fd, strerror(errno));
838 return nullptr;
839 }
840
841 int result = 0;
842#if defined(__linux__)
843 if (declared_length > 0) {
844 // Make sure we have enough space on the volume to extract the compressed
845 // entry. Note that the call to ftruncate below will change the file size but
846 // will not allocate space on disk and this call to fallocate will not
847 // change the file size.
Badhri Jagan Sridharana68d0d12015-06-02 14:47:57 -0700848 // Note: fallocate is only supported by the following filesystems -
849 // btrfs, ext4, ocfs2, and xfs. Therefore fallocate might fail with
850 // EOPNOTSUPP error when issued in other filesystems.
851 // Hence, check for the return error code before concluding that the
852 // disk does not have enough space.
Narayan Kamathf899bd52015-04-17 11:53:14 +0100853 result = TEMP_FAILURE_RETRY(fallocate(fd, 0, current_offset, declared_length));
Badhri Jagan Sridharana68d0d12015-06-02 14:47:57 -0700854 if (result == -1 && errno == ENOSPC) {
Narayan Kamathd5d7abe2016-08-10 12:24:05 +0100855 ALOGW("Zip: unable to allocate %" PRId64 " bytes at offset %" PRId64 " : %s",
856 static_cast<int64_t>(declared_length), static_cast<int64_t>(current_offset),
857 strerror(errno));
Narayan Kamathf899bd52015-04-17 11:53:14 +0100858 return std::unique_ptr<FileWriter>(nullptr);
859 }
860 }
861#endif // __linux__
862
Tao Baoa456c212016-11-15 10:08:07 -0800863 struct stat sb;
864 if (fstat(fd, &sb) == -1) {
865 ALOGW("Zip: unable to fstat file: %s", strerror(errno));
Narayan Kamathf899bd52015-04-17 11:53:14 +0100866 return std::unique_ptr<FileWriter>(nullptr);
867 }
868
Tao Baoa456c212016-11-15 10:08:07 -0800869 // Block device doesn't support ftruncate(2).
870 if (!S_ISBLK(sb.st_mode)) {
871 result = TEMP_FAILURE_RETRY(ftruncate(fd, declared_length + current_offset));
872 if (result == -1) {
873 ALOGW("Zip: unable to truncate file to %" PRId64 ": %s",
874 static_cast<int64_t>(declared_length + current_offset), strerror(errno));
875 return std::unique_ptr<FileWriter>(nullptr);
876 }
877 }
878
Narayan Kamathf899bd52015-04-17 11:53:14 +0100879 return std::unique_ptr<FileWriter>(new FileWriter(fd, declared_length));
880 }
881
882 virtual bool Append(uint8_t* buf, size_t buf_size) override {
883 if (total_bytes_written_ + buf_size > declared_length_) {
884 ALOGW("Zip: Unexpected size " ZD " (declared) vs " ZD " (actual)",
885 declared_length_, total_bytes_written_ + buf_size);
886 return false;
887 }
888
Narayan Kamathe97e66e2015-04-27 16:25:53 +0100889 const bool result = android::base::WriteFully(fd_, buf, buf_size);
890 if (result) {
891 total_bytes_written_ += buf_size;
892 } else {
893 ALOGW("Zip: unable to write " ZD " bytes to file; %s", buf_size, strerror(errno));
Narayan Kamathf899bd52015-04-17 11:53:14 +0100894 }
895
Narayan Kamathe97e66e2015-04-27 16:25:53 +0100896 return result;
Narayan Kamathf899bd52015-04-17 11:53:14 +0100897 }
898 private:
899 FileWriter(const int fd, const size_t declared_length) :
900 Writer(),
901 fd_(fd),
902 declared_length_(declared_length),
903 total_bytes_written_(0) {
904 }
905
906 const int fd_;
907 const size_t declared_length_;
908 size_t total_bytes_written_;
909};
910
Dmitriy Ivanovf94e1592015-03-06 13:27:59 -0800911// This method is using libz macros with old-style-casts
912#pragma GCC diagnostic push
913#pragma GCC diagnostic ignored "-Wold-style-cast"
914static inline int zlib_inflateInit2(z_stream* stream, int window_bits) {
915 return inflateInit2(stream, window_bits);
916}
917#pragma GCC diagnostic pop
918
Tianjie Xu18c25922016-09-29 15:27:41 -0700919static int32_t InflateEntryToWriter(MappedZipFile& mapped_zip, const ZipEntry* entry,
Narayan Kamathf899bd52015-04-17 11:53:14 +0100920 Writer* writer, uint64_t* crc_out) {
Dmitriy Ivanovedbabfe2015-03-12 09:58:15 -0700921 const size_t kBufSize = 32768;
922 std::vector<uint8_t> read_buf(kBufSize);
923 std::vector<uint8_t> write_buf(kBufSize);
Narayan Kamath7462f022013-11-21 13:05:04 +0000924 z_stream zstream;
925 int zerr;
926
927 /*
928 * Initialize the zlib stream struct.
929 */
930 memset(&zstream, 0, sizeof(zstream));
931 zstream.zalloc = Z_NULL;
932 zstream.zfree = Z_NULL;
933 zstream.opaque = Z_NULL;
934 zstream.next_in = NULL;
935 zstream.avail_in = 0;
Dmitriy Ivanovedbabfe2015-03-12 09:58:15 -0700936 zstream.next_out = &write_buf[0];
Narayan Kamath7462f022013-11-21 13:05:04 +0000937 zstream.avail_out = kBufSize;
938 zstream.data_type = Z_UNKNOWN;
939
940 /*
941 * Use the undocumented "negative window bits" feature to tell zlib
942 * that there's no zlib header waiting for it.
943 */
Dmitriy Ivanovf94e1592015-03-06 13:27:59 -0800944 zerr = zlib_inflateInit2(&zstream, -MAX_WBITS);
Narayan Kamath7462f022013-11-21 13:05:04 +0000945 if (zerr != Z_OK) {
946 if (zerr == Z_VERSION_ERROR) {
947 ALOGE("Installed zlib is not compatible with linked version (%s)",
948 ZLIB_VERSION);
949 } else {
950 ALOGW("Call to inflateInit2 failed (zerr=%d)", zerr);
951 }
952
953 return kZlibError;
954 }
955
Dmitriy Ivanov1f741e52015-03-06 14:26:37 -0800956 auto zstream_deleter = [](z_stream* stream) {
957 inflateEnd(stream); /* free up any allocated structures */
958 };
959
960 std::unique_ptr<z_stream, decltype(zstream_deleter)> zstream_guard(&zstream, zstream_deleter);
961
Narayan Kamath7462f022013-11-21 13:05:04 +0000962 const uint32_t uncompressed_length = entry->uncompressed_length;
963
964 uint32_t compressed_length = entry->compressed_length;
Narayan Kamath7462f022013-11-21 13:05:04 +0000965 do {
966 /* read as much as we can */
967 if (zstream.avail_in == 0) {
Yabin Cuib2a77002016-02-08 16:26:33 -0800968 const size_t getSize = (compressed_length > kBufSize) ? kBufSize : compressed_length;
Tianjie Xu18c25922016-09-29 15:27:41 -0700969 if (!mapped_zip.ReadData(read_buf.data(), getSize)) {
Yabin Cuib2a77002016-02-08 16:26:33 -0800970 ALOGW("Zip: inflate read failed, getSize = %zu: %s", getSize, strerror(errno));
Dmitriy Ivanov1f741e52015-03-06 14:26:37 -0800971 return kIoError;
Narayan Kamath7462f022013-11-21 13:05:04 +0000972 }
973
974 compressed_length -= getSize;
975
Dmitriy Ivanovedbabfe2015-03-12 09:58:15 -0700976 zstream.next_in = &read_buf[0];
Narayan Kamath7462f022013-11-21 13:05:04 +0000977 zstream.avail_in = getSize;
978 }
979
980 /* uncompress the data */
981 zerr = inflate(&zstream, Z_NO_FLUSH);
982 if (zerr != Z_OK && zerr != Z_STREAM_END) {
983 ALOGW("Zip: inflate zerr=%d (nIn=%p aIn=%u nOut=%p aOut=%u)",
984 zerr, zstream.next_in, zstream.avail_in,
985 zstream.next_out, zstream.avail_out);
Dmitriy Ivanov1f741e52015-03-06 14:26:37 -0800986 return kZlibError;
Narayan Kamath7462f022013-11-21 13:05:04 +0000987 }
988
989 /* write when we're full or when we're done */
990 if (zstream.avail_out == 0 ||
991 (zerr == Z_STREAM_END && zstream.avail_out != kBufSize)) {
Dmitriy Ivanovedbabfe2015-03-12 09:58:15 -0700992 const size_t write_size = zstream.next_out - &write_buf[0];
Narayan Kamathf899bd52015-04-17 11:53:14 +0100993 if (!writer->Append(&write_buf[0], write_size)) {
994 // The file might have declared a bogus length.
995 return kInconsistentInformation;
Narayan Kamath7462f022013-11-21 13:05:04 +0000996 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000997
Dmitriy Ivanovedbabfe2015-03-12 09:58:15 -0700998 zstream.next_out = &write_buf[0];
Narayan Kamath7462f022013-11-21 13:05:04 +0000999 zstream.avail_out = kBufSize;
1000 }
1001 } while (zerr == Z_OK);
1002
1003 assert(zerr == Z_STREAM_END); /* other errors should've been caught */
1004
1005 // stream.adler holds the crc32 value for such streams.
1006 *crc_out = zstream.adler;
1007
1008 if (zstream.total_out != uncompressed_length || compressed_length != 0) {
Mark Salyzyn088bf902014-05-08 16:02:20 -07001009 ALOGW("Zip: size mismatch on inflated file (%lu vs %" PRIu32 ")",
Narayan Kamath7462f022013-11-21 13:05:04 +00001010 zstream.total_out, uncompressed_length);
Dmitriy Ivanov1f741e52015-03-06 14:26:37 -08001011 return kInconsistentInformation;
Narayan Kamath7462f022013-11-21 13:05:04 +00001012 }
1013
Dmitriy Ivanov1f741e52015-03-06 14:26:37 -08001014 return 0;
Narayan Kamath7462f022013-11-21 13:05:04 +00001015}
1016
Tianjie Xu18c25922016-09-29 15:27:41 -07001017static int32_t CopyEntryToWriter(MappedZipFile& mapped_zip, const ZipEntry* entry, Writer* writer,
Narayan Kamathf899bd52015-04-17 11:53:14 +01001018 uint64_t *crc_out) {
1019 static const uint32_t kBufSize = 32768;
1020 std::vector<uint8_t> buf(kBufSize);
1021
1022 const uint32_t length = entry->uncompressed_length;
1023 uint32_t count = 0;
1024 uint64_t crc = 0;
1025 while (count < length) {
1026 uint32_t remaining = length - count;
1027
1028 // Safe conversion because kBufSize is narrow enough for a 32 bit signed
1029 // value.
Yabin Cuib2a77002016-02-08 16:26:33 -08001030 const size_t block_size = (remaining > kBufSize) ? kBufSize : remaining;
Tianjie Xu18c25922016-09-29 15:27:41 -07001031 if (!mapped_zip.ReadData(buf.data(), block_size)) {
Yabin Cuib2a77002016-02-08 16:26:33 -08001032 ALOGW("CopyFileToFile: copy read failed, block_size = %zu: %s", block_size, strerror(errno));
Narayan Kamathf899bd52015-04-17 11:53:14 +01001033 return kIoError;
1034 }
1035
1036 if (!writer->Append(&buf[0], block_size)) {
1037 return kIoError;
1038 }
1039 crc = crc32(crc, &buf[0], block_size);
1040 count += block_size;
1041 }
1042
1043 *crc_out = crc;
1044
1045 return 0;
1046}
1047
1048int32_t ExtractToWriter(ZipArchiveHandle handle,
1049 ZipEntry* entry, Writer* writer) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -08001050 ZipArchive* archive = reinterpret_cast<ZipArchive*>(handle);
Narayan Kamath7462f022013-11-21 13:05:04 +00001051 const uint16_t method = entry->method;
1052 off64_t data_offset = entry->offset;
1053
Tianjie Xu18c25922016-09-29 15:27:41 -07001054 if (!archive->mapped_zip.SeekToOffset(data_offset)) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -08001055 ALOGW("Zip: lseek to data at %" PRId64 " failed", static_cast<int64_t>(data_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +00001056 return kIoError;
1057 }
1058
1059 // this should default to kUnknownCompressionMethod.
1060 int32_t return_value = -1;
1061 uint64_t crc = 0;
1062 if (method == kCompressStored) {
Tianjie Xu18c25922016-09-29 15:27:41 -07001063 return_value = CopyEntryToWriter(archive->mapped_zip, entry, writer, &crc);
Narayan Kamath7462f022013-11-21 13:05:04 +00001064 } else if (method == kCompressDeflated) {
Tianjie Xu18c25922016-09-29 15:27:41 -07001065 return_value = InflateEntryToWriter(archive->mapped_zip, entry, writer, &crc);
Narayan Kamath7462f022013-11-21 13:05:04 +00001066 }
1067
1068 if (!return_value && entry->has_data_descriptor) {
Tianjie Xu18c25922016-09-29 15:27:41 -07001069 return_value = UpdateEntryFromDataDescriptor(archive->mapped_zip, entry);
Narayan Kamath7462f022013-11-21 13:05:04 +00001070 if (return_value) {
1071 return return_value;
1072 }
1073 }
1074
1075 // TODO: Fix this check by passing the right flags to inflate2 so that
1076 // it calculates the CRC for us.
1077 if (entry->crc32 != crc && false) {
Mark Salyzyn088bf902014-05-08 16:02:20 -07001078 ALOGW("Zip: crc mismatch: expected %" PRIu32 ", was %" PRIu64, entry->crc32, crc);
Narayan Kamath7462f022013-11-21 13:05:04 +00001079 return kInconsistentInformation;
1080 }
1081
1082 return return_value;
1083}
1084
Narayan Kamathf899bd52015-04-17 11:53:14 +01001085int32_t ExtractToMemory(ZipArchiveHandle handle, ZipEntry* entry,
1086 uint8_t* begin, uint32_t size) {
1087 std::unique_ptr<Writer> writer(new MemoryWriter(begin, size));
1088 return ExtractToWriter(handle, entry, writer.get());
1089}
1090
Narayan Kamath7462f022013-11-21 13:05:04 +00001091int32_t ExtractEntryToFile(ZipArchiveHandle handle,
1092 ZipEntry* entry, int fd) {
Narayan Kamathf899bd52015-04-17 11:53:14 +01001093 std::unique_ptr<Writer> writer(FileWriter::Create(fd, entry));
1094 if (writer.get() == nullptr) {
Narayan Kamath7462f022013-11-21 13:05:04 +00001095 return kIoError;
1096 }
1097
Narayan Kamathf899bd52015-04-17 11:53:14 +01001098 return ExtractToWriter(handle, entry, writer.get());
Narayan Kamath7462f022013-11-21 13:05:04 +00001099}
1100
1101const char* ErrorCodeString(int32_t error_code) {
1102 if (error_code > kErrorMessageLowerBound && error_code < kErrorMessageUpperBound) {
1103 return kErrorMessages[error_code * -1];
1104 }
1105
1106 return kErrorMessages[0];
1107}
1108
1109int GetFileDescriptor(const ZipArchiveHandle handle) {
Tianjie Xu18c25922016-09-29 15:27:41 -07001110 return reinterpret_cast<ZipArchive*>(handle)->mapped_zip.GetFileDescriptor();
Narayan Kamath7462f022013-11-21 13:05:04 +00001111}
Colin Cross7c6c7f02016-09-16 10:15:51 -07001112
1113ZipString::ZipString(const char* entry_name)
1114 : name(reinterpret_cast<const uint8_t*>(entry_name)) {
1115 size_t len = strlen(entry_name);
1116 CHECK_LE(len, static_cast<size_t>(UINT16_MAX));
1117 name_length = static_cast<uint16_t>(len);
1118}
Tianjie Xu18c25922016-09-29 15:27:41 -07001119
1120#if !defined(_WIN32)
1121class ProcessWriter : public Writer {
1122 public:
1123 ProcessWriter(ProcessZipEntryFunction func, void* cookie) : Writer(),
1124 proc_function_(func),
1125 cookie_(cookie) {
1126 }
1127
1128 virtual bool Append(uint8_t* buf, size_t buf_size) override {
1129 return proc_function_(buf, buf_size, cookie_);
1130 }
1131
1132 private:
1133 ProcessZipEntryFunction proc_function_;
1134 void* cookie_;
1135};
1136
1137int32_t ProcessZipEntryContents(ZipArchiveHandle handle, ZipEntry* entry,
1138 ProcessZipEntryFunction func, void* cookie) {
1139 ProcessWriter writer(func, cookie);
1140 return ExtractToWriter(handle, entry, &writer);
1141}
1142
1143#endif //!defined(_WIN32)
1144
1145int MappedZipFile::GetFileDescriptor() const {
1146 if (!has_fd_) {
1147 ALOGW("Zip: MappedZipFile doesn't have a file descriptor.");
1148 return -1;
1149 }
1150 return fd_;
1151}
1152
1153void* MappedZipFile::GetBasePtr() const {
1154 if (has_fd_) {
1155 ALOGW("Zip: MappedZipFile doesn't have a base pointer.");
1156 return nullptr;
1157 }
1158 return base_ptr_;
1159}
1160
1161off64_t MappedZipFile::GetFileLength() const {
1162 if (has_fd_) {
1163 off64_t result = lseek64(fd_, 0, SEEK_END);
1164 if (result == -1) {
1165 ALOGE("Zip: lseek on fd %d failed: %s", fd_, strerror(errno));
1166 }
1167 return result;
1168 } else {
1169 if (base_ptr_ == nullptr) {
1170 ALOGE("Zip: invalid file map\n");
1171 return -1;
1172 }
1173 return static_cast<off64_t>(data_length_);
1174 }
1175}
1176
1177bool MappedZipFile::SeekToOffset(off64_t offset) {
1178 if (has_fd_) {
1179 if (lseek64(fd_, offset, SEEK_SET) != offset) {
1180 ALOGE("Zip: lseek to %" PRId64 " failed: %s\n", offset, strerror(errno));
1181 return false;
1182 }
1183 return true;
1184 } else {
1185 if (offset < 0 || offset > static_cast<off64_t>(data_length_)) {
1186 ALOGE("Zip: invalid offset: %" PRId64 ", data length: %" PRId64 "\n" , offset,
1187 data_length_);
1188 return false;
1189 }
1190
1191 read_pos_ = offset;
1192 return true;
1193 }
1194}
1195
1196bool MappedZipFile::ReadData(uint8_t* buffer, size_t read_amount) {
1197 if (has_fd_) {
1198 if(!android::base::ReadFully(fd_, buffer, read_amount)) {
1199 ALOGE("Zip: read from %d failed\n", fd_);
1200 return false;
1201 }
1202 } else {
1203 memcpy(buffer, static_cast<uint8_t*>(base_ptr_) + read_pos_, read_amount);
1204 read_pos_ += read_amount;
1205 }
1206 return true;
1207}
1208
1209// Attempts to read |len| bytes into |buf| at offset |off|.
1210bool MappedZipFile::ReadAtOffset(uint8_t* buf, size_t len, off64_t off) {
1211#if !defined(_WIN32)
1212 if (has_fd_) {
1213 if (static_cast<size_t>(TEMP_FAILURE_RETRY(pread64(fd_, buf, len, off))) != len) {
1214 ALOGE("Zip: failed to read at offset %" PRId64 "\n", off);
1215 return false;
1216 }
1217 return true;
1218 }
1219#endif
1220 if (!SeekToOffset(off)) {
1221 return false;
1222 }
1223 return ReadData(buf, len);
1224
1225}
1226
1227void CentralDirectory::Initialize(void* map_base_ptr, off64_t cd_start_offset, size_t cd_size) {
1228 base_ptr_ = static_cast<uint8_t*>(map_base_ptr) + cd_start_offset;
1229 length_ = cd_size;
1230}
1231
1232bool ZipArchive::InitializeCentralDirectory(const char* debug_file_name, off64_t cd_start_offset,
1233 size_t cd_size) {
1234 if (mapped_zip.HasFd()) {
1235 if (!directory_map->create(debug_file_name, mapped_zip.GetFileDescriptor(),
1236 cd_start_offset, cd_size, true /* read only */)) {
1237 return false;
1238 }
1239
1240 CHECK_EQ(directory_map->getDataLength(), cd_size);
1241 central_directory.Initialize(directory_map->getDataPtr(), 0/*offset*/, cd_size);
1242 } else {
1243 if (mapped_zip.GetBasePtr() == nullptr) {
1244 ALOGE("Zip: Failed to map central directory, bad mapped_zip base pointer\n");
1245 return false;
1246 }
1247 if (static_cast<off64_t>(cd_start_offset) + static_cast<off64_t>(cd_size) >
1248 mapped_zip.GetFileLength()) {
1249 ALOGE("Zip: Failed to map central directory, offset exceeds mapped memory region ("
1250 "start_offset %" PRId64 ", cd_size %zu, mapped_region_size %" PRId64 ")",
1251 static_cast<int64_t>(cd_start_offset), cd_size, mapped_zip.GetFileLength());
1252 return false;
1253 }
1254
1255 central_directory.Initialize(mapped_zip.GetBasePtr(), cd_start_offset, cd_size);
1256 }
1257 return true;
1258}