blob: 34a9c545faf653b992849ff22194381dcaed0f68 [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
Elliott Hughese8f4b142018-10-19 16:09:39 -070023#include "ziparchive/zip_archive.h"
24
Narayan Kamath7462f022013-11-21 13:05:04 +000025#include <errno.h>
Mark Salyzyn99ef9912014-03-14 14:26:22 -070026#include <fcntl.h>
27#include <inttypes.h>
Narayan Kamath7462f022013-11-21 13:05:04 +000028#include <limits.h>
Narayan Kamath7462f022013-11-21 13:05:04 +000029#include <stdlib.h>
30#include <string.h>
Elliott Hughes55fd2932017-05-28 22:59:04 -070031#include <time.h>
Narayan Kamath7462f022013-11-21 13:05:04 +000032#include <unistd.h>
33
Dan Albert1ae07642015-04-09 14:11:18 -070034#include <memory>
35#include <vector>
36
Elliott Hughes9c8bd662018-10-26 16:14:21 -070037#if defined(__APPLE__)
38#define lseek64 lseek
39#endif
40
Josh Gao1b496342018-07-17 11:08:48 -070041#if defined(__BIONIC__)
42#include <android/fdsan.h>
43#endif
44
Mark Salyzynff2dcd92016-09-28 15:54:45 -070045#include <android-base/file.h>
46#include <android-base/logging.h>
47#include <android-base/macros.h> // TEMP_FAILURE_RETRY may or may not be in unistd
Elliott Hughese8f4b142018-10-19 16:09:39 -070048#include <android-base/mapped_file.h>
Mark Salyzynff2dcd92016-09-28 15:54:45 -070049#include <android-base/memory.h>
Elliott Hughes50ef29a2019-06-18 18:23:59 -070050#include <android-base/strings.h>
Ryan Mitchellc77f9d32018-08-25 14:06:29 -070051#include <android-base/utf8.h>
Mark Salyzyncfd5b082016-10-17 14:28:00 -070052#include <log/log.h>
Dan Albert1ae07642015-04-09 14:11:18 -070053#include "zlib.h"
Narayan Kamath7462f022013-11-21 13:05:04 +000054
Narayan Kamath044bc8e2014-12-03 18:22:53 +000055#include "entry_name_utils-inl.h"
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -070056#include "zip_archive_common.h"
Christopher Ferrise6884ce2015-11-10 14:55:12 -080057#include "zip_archive_private.h"
Mark Salyzyn99ef9912014-03-14 14:26:22 -070058
Dan Albert1ae07642015-04-09 14:11:18 -070059using android::base::get_unaligned;
Narayan Kamath044bc8e2014-12-03 18:22:53 +000060
Narayan Kamath162b7052017-06-05 13:21:12 +010061// Used to turn on crc checks - verify that the content CRC matches the values
62// specified in the local file header and the central directory.
63static const bool kCrcChecksEnabled = false;
64
Narayan Kamath926973e2014-06-09 14:18:14 +010065// The maximum number of bytes to scan backwards for the EOCD start.
66static const uint32_t kMaxEOCDSearch = kMaxCommentLen + sizeof(EocdRecord);
67
Narayan Kamath7462f022013-11-21 13:05:04 +000068/*
69 * A Read-only Zip archive.
70 *
71 * We want "open" and "find entry by name" to be fast operations, and
72 * we want to use as little memory as possible. We memory-map the zip
73 * central directory, and load a hash table with pointers to the filenames
74 * (which aren't null-terminated). The other fields are at a fixed offset
75 * from the filename, so we don't need to extract those (but we do need
76 * to byte-read and endian-swap them every time we want them).
77 *
78 * It's possible that somebody has handed us a massive (~1GB) zip archive,
79 * so we can't expect to mmap the entire file.
80 *
81 * To speed comparisons when doing a lookup by name, we could make the mapping
82 * "private" (copy-on-write) and null-terminate the filenames after verifying
83 * the record structure. However, this requires a private mapping of
84 * every page that the Central Directory touches. Easier to tuck a copy
85 * of the string length into the hash table entry.
86 */
Narayan Kamath7462f022013-11-21 13:05:04 +000087
Narayan Kamath7462f022013-11-21 13:05:04 +000088/*
89 * Round up to the next highest power of 2.
90 *
91 * Found on http://graphics.stanford.edu/~seander/bithacks.html.
92 */
93static uint32_t RoundUpPower2(uint32_t val) {
94 val--;
95 val |= val >> 1;
96 val |= val >> 2;
97 val |= val >> 4;
98 val |= val >> 8;
99 val |= val >> 16;
100 val++;
101
102 return val;
103}
104
Elliott Hughes50ef29a2019-06-18 18:23:59 -0700105static uint32_t ComputeHash(std::string_view name) {
106 return static_cast<uint32_t>(std::hash<std::string_view>{}(name));
Zimuzo5a503ef2018-09-17 19:49:55 +0100107}
108
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800109// Convert a ZipEntry to a hash table index, verifying that it's in a valid range.
Tianjie Xu0ef97832020-03-15 21:23:24 -0700110std::pair<ZipError, uint64_t> CdEntryMapZip32::GetCdEntryOffset(std::string_view name,
111 const uint8_t* start) const {
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100112 const uint32_t hash = ComputeHash(name);
Narayan Kamath7462f022013-11-21 13:05:04 +0000113
114 // NOTE: (hash_table_size - 1) is guaranteed to be non-negative.
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800115 uint32_t ent = hash & (hash_table_size_ - 1);
116 while (hash_table_[ent].name_offset != 0) {
117 if (hash_table_[ent].ToStringView(start) == name) {
Tianjie Xu0ef97832020-03-15 21:23:24 -0700118 return {kSuccess, hash_table_[ent].name_offset};
Narayan Kamath7462f022013-11-21 13:05:04 +0000119 }
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800120 ent = (ent + 1) & (hash_table_size_ - 1);
Narayan Kamath7462f022013-11-21 13:05:04 +0000121 }
122
Elliott Hughes50ef29a2019-06-18 18:23:59 -0700123 ALOGV("Zip: Unable to find entry %.*s", static_cast<int>(name.size()), name.data());
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800124 return {kEntryNotFound, 0};
Narayan Kamath7462f022013-11-21 13:05:04 +0000125}
126
Tianjie Xu0ef97832020-03-15 21:23:24 -0700127ZipError CdEntryMapZip32::AddToMap(std::string_view name, const uint8_t* start) {
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100128 const uint64_t hash = ComputeHash(name);
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800129 uint32_t ent = hash & (hash_table_size_ - 1);
Narayan Kamath7462f022013-11-21 13:05:04 +0000130
131 /*
132 * We over-allocated the table, so we're guaranteed to find an empty slot.
133 * Further, we guarantee that the hashtable size is not 0.
134 */
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800135 while (hash_table_[ent].name_offset != 0) {
136 if (hash_table_[ent].ToStringView(start) == name) {
Elliott Hughes50ef29a2019-06-18 18:23:59 -0700137 // We've found a duplicate entry. We don't accept duplicates.
138 ALOGW("Zip: Found duplicate entry %.*s", static_cast<int>(name.size()), name.data());
Narayan Kamath7462f022013-11-21 13:05:04 +0000139 return kDuplicateEntry;
140 }
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800141 ent = (ent + 1) & (hash_table_size_ - 1);
Narayan Kamath7462f022013-11-21 13:05:04 +0000142 }
Elliott Hughes50ef29a2019-06-18 18:23:59 -0700143
144 // `name` has already been validated before entry.
145 const char* start_char = reinterpret_cast<const char*>(start);
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800146 hash_table_[ent].name_offset = static_cast<uint32_t>(name.data() - start_char);
147 hash_table_[ent].name_length = static_cast<uint16_t>(name.size());
Tianjie Xu0ef97832020-03-15 21:23:24 -0700148 return kSuccess;
Narayan Kamath7462f022013-11-21 13:05:04 +0000149}
150
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800151void CdEntryMapZip32::ResetIteration() {
152 current_position_ = 0;
153}
154
155std::pair<std::string_view, uint64_t> CdEntryMapZip32::Next(const uint8_t* cd_start) {
156 while (current_position_ < hash_table_size_) {
157 const auto& entry = hash_table_[current_position_];
158 current_position_ += 1;
159
160 if (entry.name_offset != 0) {
161 return {entry.ToStringView(cd_start), entry.name_offset};
162 }
163 }
164 // We have reached the end of the hash table.
165 return {};
166}
167
168CdEntryMapZip32::CdEntryMapZip32(uint16_t num_entries) {
Tianjie Xu0ef97832020-03-15 21:23:24 -0700169 /*
170 * Create hash table. We have a minimum 75% load factor, possibly as
171 * low as 50% after we round off to a power of 2. There must be at
172 * least one unused entry to avoid an infinite loop during creation.
173 */
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800174 hash_table_size_ = RoundUpPower2(1 + (num_entries * 4) / 3);
175 hash_table_ = {
176 reinterpret_cast<ZipStringOffset*>(calloc(hash_table_size_, sizeof(ZipStringOffset))), free};
177}
178
179std::unique_ptr<CdEntryMapInterface> CdEntryMapZip32::Create(uint16_t num_entries) {
180 auto entry_map = new CdEntryMapZip32(num_entries);
181 CHECK(entry_map->hash_table_ != nullptr)
182 << "Zip: unable to allocate the " << entry_map->hash_table_size_
183 << " entry hash_table, entry size: " << sizeof(ZipStringOffset);
184 return std::unique_ptr<CdEntryMapInterface>(entry_map);
185}
186
Tianjie Xu0ef97832020-03-15 21:23:24 -0700187std::unique_ptr<CdEntryMapInterface> CdEntryMapZip64::Create() {
188 return std::unique_ptr<CdEntryMapInterface>(new CdEntryMapZip64());
189}
190
191ZipError CdEntryMapZip64::AddToMap(std::string_view name, const uint8_t* start) {
192 const auto [it, added] =
193 entry_table_.insert({name, name.data() - reinterpret_cast<const char*>(start)});
194 if (!added) {
195 ALOGW("Zip: Found duplicate entry %.*s", static_cast<int>(name.size()), name.data());
196 return kDuplicateEntry;
197 }
198 return kSuccess;
199}
200
201std::pair<ZipError, uint64_t> CdEntryMapZip64::GetCdEntryOffset(std::string_view name,
202 const uint8_t* /*cd_start*/) const {
203 const auto it = entry_table_.find(name);
204 if (it == entry_table_.end()) {
205 ALOGV("Zip: Could not find entry %.*s", static_cast<int>(name.size()), name.data());
206 return {kEntryNotFound, 0};
207 }
208
209 return {kSuccess, it->second};
210}
211
212void CdEntryMapZip64::ResetIteration() {
213 iterator_ = entry_table_.begin();
214}
215
216std::pair<std::string_view, uint64_t> CdEntryMapZip64::Next(const uint8_t* /*cd_start*/) {
217 if (iterator_ == entry_table_.end()) {
218 return {};
219 }
220
221 return *iterator_++;
222}
223
Josh Gaoabdfc242018-09-07 12:44:40 -0700224#if defined(__BIONIC__)
225uint64_t GetOwnerTag(const ZipArchive* archive) {
226 return android_fdsan_create_owner_tag(ANDROID_FDSAN_OWNER_TYPE_ZIPARCHIVE,
227 reinterpret_cast<uint64_t>(archive));
228}
229#endif
230
Josh Gao1b496342018-07-17 11:08:48 -0700231ZipArchive::ZipArchive(const int fd, bool assume_ownership)
232 : mapped_zip(fd),
233 close_file(assume_ownership),
234 directory_offset(0),
235 central_directory(),
Elliott Hughese8f4b142018-10-19 16:09:39 -0700236 directory_map(),
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800237 num_entries(0) {
Josh Gao1b496342018-07-17 11:08:48 -0700238#if defined(__BIONIC__)
239 if (assume_ownership) {
Josh Gaoabdfc242018-09-07 12:44:40 -0700240 android_fdsan_exchange_owner_tag(fd, 0, GetOwnerTag(this));
Josh Gao1b496342018-07-17 11:08:48 -0700241 }
242#endif
243}
244
Elliott Hughesf66460b2019-10-22 11:44:50 -0700245ZipArchive::ZipArchive(const void* address, size_t length)
Josh Gao1b496342018-07-17 11:08:48 -0700246 : mapped_zip(address, length),
247 close_file(false),
248 directory_offset(0),
249 central_directory(),
Elliott Hughese8f4b142018-10-19 16:09:39 -0700250 directory_map(),
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800251 num_entries(0) {}
Josh Gao1b496342018-07-17 11:08:48 -0700252
253ZipArchive::~ZipArchive() {
254 if (close_file && mapped_zip.GetFileDescriptor() >= 0) {
255#if defined(__BIONIC__)
Josh Gaoabdfc242018-09-07 12:44:40 -0700256 android_fdsan_close_with_tag(mapped_zip.GetFileDescriptor(), GetOwnerTag(this));
Josh Gao1b496342018-07-17 11:08:48 -0700257#else
258 close(mapped_zip.GetFileDescriptor());
259#endif
260 }
Josh Gao1b496342018-07-17 11:08:48 -0700261}
262
Tianjie Xu18c25922016-09-29 15:27:41 -0700263static int32_t MapCentralDirectory0(const char* debug_file_name, ZipArchive* archive,
Andreas Gampe964b95c2019-04-05 13:48:02 -0700264 off64_t file_length, uint32_t read_amount,
Zimuzo5a503ef2018-09-17 19:49:55 +0100265 uint8_t* scan_buffer) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000266 const off64_t search_start = file_length - read_amount;
267
Jiyong Parkcd997e62017-06-30 17:23:33 +0900268 if (!archive->mapped_zip.ReadAtOffset(scan_buffer, read_amount, search_start)) {
269 ALOGE("Zip: read %" PRId64 " from offset %" PRId64 " failed", static_cast<int64_t>(read_amount),
270 static_cast<int64_t>(search_start));
Narayan Kamath7462f022013-11-21 13:05:04 +0000271 return kIoError;
272 }
273
274 /*
275 * Scan backward for the EOCD magic. In an archive without a trailing
276 * comment, we'll find it on the first try. (We may want to consider
277 * doing an initial minimal read; if we don't find it, retry with a
278 * second read as above.)
279 */
Andreas Gampe964b95c2019-04-05 13:48:02 -0700280 CHECK_LE(read_amount, std::numeric_limits<int32_t>::max());
281 int32_t i = read_amount - sizeof(EocdRecord);
Narayan Kamath926973e2014-06-09 14:18:14 +0100282 for (; i >= 0; i--) {
Dan Albert1ae07642015-04-09 14:11:18 -0700283 if (scan_buffer[i] == 0x50) {
284 uint32_t* sig_addr = reinterpret_cast<uint32_t*>(&scan_buffer[i]);
285 if (get_unaligned<uint32_t>(sig_addr) == EocdRecord::kSignature) {
286 ALOGV("+++ Found EOCD at buf+%d", i);
287 break;
288 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000289 }
290 }
291 if (i < 0) {
292 ALOGD("Zip: EOCD not found, %s is not zip", debug_file_name);
293 return kInvalidFile;
294 }
295
296 const off64_t eocd_offset = search_start + i;
Narayan Kamath926973e2014-06-09 14:18:14 +0100297 const EocdRecord* eocd = reinterpret_cast<const EocdRecord*>(scan_buffer + i);
Narayan Kamath7462f022013-11-21 13:05:04 +0000298 /*
Narayan Kamath926973e2014-06-09 14:18:14 +0100299 * Verify that there's no trailing space at the end of the central directory
300 * and its comment.
Narayan Kamath7462f022013-11-21 13:05:04 +0000301 */
Jiyong Parkcd997e62017-06-30 17:23:33 +0900302 const off64_t calculated_length = eocd_offset + sizeof(EocdRecord) + eocd->comment_length;
Narayan Kamath926973e2014-06-09 14:18:14 +0100303 if (calculated_length != file_length) {
Narayan Kamath4f6b4992014-06-03 13:59:23 +0100304 ALOGW("Zip: %" PRId64 " extraneous bytes at the end of the central directory",
Narayan Kamath926973e2014-06-09 14:18:14 +0100305 static_cast<int64_t>(file_length - calculated_length));
Narayan Kamath4f6b4992014-06-03 13:59:23 +0100306 return kInvalidFile;
307 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000308
Narayan Kamath926973e2014-06-09 14:18:14 +0100309 /*
310 * Grab the CD offset and size, and the number of entries in the
311 * archive and verify that they look reasonable.
312 */
Tianjie Xu1ee48922016-09-21 14:58:11 -0700313 if (static_cast<off64_t>(eocd->cd_start_offset) + eocd->cd_size > eocd_offset) {
Narayan Kamath926973e2014-06-09 14:18:14 +0100314 ALOGW("Zip: bad offsets (dir %" PRIu32 ", size %" PRIu32 ", eocd %" PRId64 ")",
Jiyong Parkcd997e62017-06-30 17:23:33 +0900315 eocd->cd_start_offset, eocd->cd_size, static_cast<int64_t>(eocd_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000316 return kInvalidOffset;
317 }
Narayan Kamath926973e2014-06-09 14:18:14 +0100318 if (eocd->num_records == 0) {
Adam Lesinskib354dce2018-03-01 21:32:13 +0000319#if defined(__ANDROID__)
Narayan Kamath7462f022013-11-21 13:05:04 +0000320 ALOGW("Zip: empty archive?");
Adam Lesinskib354dce2018-03-01 21:32:13 +0000321#endif
Narayan Kamath7462f022013-11-21 13:05:04 +0000322 return kEmptyArchive;
323 }
324
Jiyong Parkcd997e62017-06-30 17:23:33 +0900325 ALOGV("+++ num_entries=%" PRIu32 " dir_size=%" PRIu32 " dir_offset=%" PRIu32, eocd->num_records,
326 eocd->cd_size, eocd->cd_start_offset);
Narayan Kamath7462f022013-11-21 13:05:04 +0000327
Elliott Hughesfba2a1a2019-12-16 16:16:16 -0800328 // It all looks good. Create a mapping for the CD, and set the fields
329 // in archive.
Elliott Hughese8f4b142018-10-19 16:09:39 -0700330 if (!archive->InitializeCentralDirectory(static_cast<off64_t>(eocd->cd_start_offset),
Tianjie Xu18c25922016-09-29 15:27:41 -0700331 static_cast<size_t>(eocd->cd_size))) {
Narayan Kamatheaf98852013-12-11 14:51:51 +0000332 return kMmapFailed;
Narayan Kamath7462f022013-11-21 13:05:04 +0000333 }
334
Narayan Kamath926973e2014-06-09 14:18:14 +0100335 archive->num_entries = eocd->num_records;
336 archive->directory_offset = eocd->cd_start_offset;
Narayan Kamath7462f022013-11-21 13:05:04 +0000337
338 return 0;
339}
340
341/*
342 * Find the zip Central Directory and memory-map it.
343 *
344 * On success, returns 0 after populating fields from the EOCD area:
345 * directory_offset
Tianjie Xu18c25922016-09-29 15:27:41 -0700346 * directory_ptr
Narayan Kamath7462f022013-11-21 13:05:04 +0000347 * num_entries
348 */
Tianjie Xu18c25922016-09-29 15:27:41 -0700349static int32_t MapCentralDirectory(const char* debug_file_name, ZipArchive* archive) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000350 // 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).
Tianjie Xu18c25922016-09-29 15:27:41 -0700353 off64_t file_length = archive->mapped_zip.GetFileLength();
Narayan Kamath7462f022013-11-21 13:05:04 +0000354 if (file_length == -1) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000355 return kInvalidFile;
356 }
357
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800358 if (file_length > static_cast<off64_t>(0xffffffff)) {
Narayan Kamath926973e2014-06-09 14:18:14 +0100359 ALOGV("Zip: zip file too long %" PRId64, static_cast<int64_t>(file_length));
Narayan Kamath7462f022013-11-21 13:05:04 +0000360 return kInvalidFile;
361 }
362
Narayan Kamath926973e2014-06-09 14:18:14 +0100363 if (file_length < static_cast<off64_t>(sizeof(EocdRecord))) {
364 ALOGV("Zip: length %" PRId64 " is too small to be zip", static_cast<int64_t>(file_length));
Narayan Kamath7462f022013-11-21 13:05:04 +0000365 return kInvalidFile;
366 }
367
368 /*
369 * Perform the traditional EOCD snipe hunt.
370 *
371 * We're searching for the End of Central Directory magic number,
372 * which appears at the start of the EOCD block. It's followed by
373 * 18 bytes of EOCD stuff and up to 64KB of archive comment. We
374 * need to read the last part of the file into a buffer, dig through
375 * it to find the magic number, parse some values out, and use those
376 * to determine the extent of the CD.
377 *
378 * We start by pulling in the last part of the file.
379 */
Andreas Gampe964b95c2019-04-05 13:48:02 -0700380 uint32_t read_amount = kMaxEOCDSearch;
Narayan Kamath926973e2014-06-09 14:18:14 +0100381 if (file_length < read_amount) {
Andreas Gampe964b95c2019-04-05 13:48:02 -0700382 read_amount = static_cast<uint32_t>(file_length);
Narayan Kamath7462f022013-11-21 13:05:04 +0000383 }
384
Tianjie Xu18c25922016-09-29 15:27:41 -0700385 std::vector<uint8_t> scan_buffer(read_amount);
Jiyong Parkcd997e62017-06-30 17:23:33 +0900386 int32_t result =
387 MapCentralDirectory0(debug_file_name, archive, file_length, read_amount, scan_buffer.data());
Narayan Kamath7462f022013-11-21 13:05:04 +0000388 return result;
389}
390
391/*
392 * Parses the Zip archive's Central Directory. Allocates and populates the
393 * hash table.
394 *
395 * Returns 0 on success.
396 */
397static int32_t ParseZipArchive(ZipArchive* archive) {
Tianjie Xu18c25922016-09-29 15:27:41 -0700398 const uint8_t* const cd_ptr = archive->central_directory.GetBasePtr();
399 const size_t cd_length = archive->central_directory.GetMapLength();
Narayan Kamath926973e2014-06-09 14:18:14 +0100400 const uint16_t num_entries = archive->num_entries;
Narayan Kamath7462f022013-11-21 13:05:04 +0000401
Tianjie Xu0ef97832020-03-15 21:23:24 -0700402 // TODO(xunchang) parse the zip64 Eocd
403 if (num_entries > UINT16_MAX) {
404 archive->cd_entry_map = CdEntryMapZip64::Create();
405 } else {
406 archive->cd_entry_map = CdEntryMapZip32::Create(num_entries);
407 }
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800408 if (archive->cd_entry_map == nullptr) {
Elliott Hughesfba2a1a2019-12-16 16:16:16 -0800409 return kAllocationFailed;
Tianjie Xu9e020e22016-10-10 12:11:30 -0700410 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000411
412 /*
413 * Walk through the central directory, adding entries to the hash
414 * table and verifying values.
415 */
Narayan Kamath926973e2014-06-09 14:18:14 +0100416 const uint8_t* const cd_end = cd_ptr + cd_length;
Narayan Kamath7462f022013-11-21 13:05:04 +0000417 const uint8_t* ptr = cd_ptr;
418 for (uint16_t i = 0; i < num_entries; i++) {
Tianjie Xu0fda1cf2017-04-05 14:46:27 -0700419 if (ptr > cd_end - sizeof(CentralDirectoryRecord)) {
Elliott Hughesfba2a1a2019-12-16 16:16:16 -0800420 ALOGW("Zip: ran off the end (item #%" PRIu16 ", %zu bytes of central directory)", i,
421 cd_length);
Tianjie Xu0fda1cf2017-04-05 14:46:27 -0700422#if defined(__ANDROID__)
423 android_errorWriteLog(0x534e4554, "36392138");
424#endif
Elliott Hughesfba2a1a2019-12-16 16:16:16 -0800425 return kInvalidFile;
Tianjie Xu0fda1cf2017-04-05 14:46:27 -0700426 }
427
Jiyong Parkcd997e62017-06-30 17:23:33 +0900428 const CentralDirectoryRecord* cdr = reinterpret_cast<const CentralDirectoryRecord*>(ptr);
Narayan Kamath926973e2014-06-09 14:18:14 +0100429 if (cdr->record_signature != CentralDirectoryRecord::kSignature) {
Mark Salyzyn088bf902014-05-08 16:02:20 -0700430 ALOGW("Zip: missed a central dir sig (at %" PRIu16 ")", i);
Elliott Hughesfba2a1a2019-12-16 16:16:16 -0800431 return kInvalidFile;
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,
Jiyong Parkcd997e62017-06-30 17:23:33 +0900437 static_cast<int64_t>(local_header_offset), i);
Elliott Hughesfba2a1a2019-12-16 16:16:16 -0800438 return kInvalidFile;
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
Tianjie Xu9e020e22016-10-10 12:11:30 -0700446 if (file_name + file_name_length > cd_end) {
Elliott Hughes50ef29a2019-06-18 18:23:59 -0700447 ALOGW("Zip: file name for entry %" PRIu16
448 " exceeds the central directory range, file_name_length: %" PRIu16 ", cd_length: %zu",
449 i, file_name_length, cd_length);
Elliott Hughesfba2a1a2019-12-16 16:16:16 -0800450 return kInvalidEntryName;
Tianjie Xu9e020e22016-10-10 12:11:30 -0700451 }
Elliott Hughes50ef29a2019-06-18 18:23:59 -0700452 // Check that file name is valid UTF-8 and doesn't contain NUL (U+0000) characters.
Narayan Kamath044bc8e2014-12-03 18:22:53 +0000453 if (!IsValidEntryName(file_name, file_name_length)) {
Elliott Hughes50ef29a2019-06-18 18:23:59 -0700454 ALOGW("Zip: invalid file name at entry %" PRIu16, i);
Elliott Hughesfba2a1a2019-12-16 16:16:16 -0800455 return kInvalidEntryName;
Piotr Jastrzebski78271ba2014-08-15 12:53:00 +0100456 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000457
Elliott Hughes50ef29a2019-06-18 18:23:59 -0700458 // Add the CDE filename to the hash table.
459 std::string_view entry_name{reinterpret_cast<const char*>(file_name), file_name_length};
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800460 if (auto add_result =
461 archive->cd_entry_map->AddToMap(entry_name, archive->central_directory.GetBasePtr());
462 add_result != 0) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000463 ALOGW("Zip: Error adding entry to hash table %d", add_result);
Dmitriy Ivanov3ea93da2015-03-06 11:48:47 -0800464 return add_result;
Narayan Kamath7462f022013-11-21 13:05:04 +0000465 }
466
Narayan Kamath926973e2014-06-09 14:18:14 +0100467 ptr += sizeof(CentralDirectoryRecord) + file_name_length + extra_length + comment_length;
468 if ((ptr - cd_ptr) > static_cast<int64_t>(cd_length)) {
Jiyong Parkcd997e62017-06-30 17:23:33 +0900469 ALOGW("Zip: bad CD advance (%tu vs %zu) at entry %" PRIu16, ptr - cd_ptr, cd_length, i);
Elliott Hughesfba2a1a2019-12-16 16:16:16 -0800470 return kInvalidFile;
Narayan Kamath7462f022013-11-21 13:05:04 +0000471 }
472 }
Narayan Kamathc1a56dc2017-08-09 18:32:09 +0100473
474 uint32_t lfh_start_bytes;
475 if (!archive->mapped_zip.ReadAtOffset(reinterpret_cast<uint8_t*>(&lfh_start_bytes),
476 sizeof(uint32_t), 0)) {
477 ALOGW("Zip: Unable to read header for entry at offset == 0.");
Elliott Hughesfba2a1a2019-12-16 16:16:16 -0800478 return kInvalidFile;
Narayan Kamathc1a56dc2017-08-09 18:32:09 +0100479 }
480
481 if (lfh_start_bytes != LocalFileHeader::kSignature) {
482 ALOGW("Zip: Entry at offset zero has invalid LFH signature %" PRIx32, lfh_start_bytes);
483#if defined(__ANDROID__)
484 android_errorWriteLog(0x534e4554, "64211847");
485#endif
Elliott Hughesfba2a1a2019-12-16 16:16:16 -0800486 return kInvalidFile;
Narayan Kamathc1a56dc2017-08-09 18:32:09 +0100487 }
488
Mark Salyzyn088bf902014-05-08 16:02:20 -0700489 ALOGV("+++ zip good scan %" PRIu16 " entries", num_entries);
Narayan Kamath7462f022013-11-21 13:05:04 +0000490
Dmitriy Ivanov3ea93da2015-03-06 11:48:47 -0800491 return 0;
Narayan Kamath7462f022013-11-21 13:05:04 +0000492}
493
Jiyong Parkcd997e62017-06-30 17:23:33 +0900494static int32_t OpenArchiveInternal(ZipArchive* archive, const char* debug_file_name) {
Elliott Hughesfba2a1a2019-12-16 16:16:16 -0800495 int32_t result = MapCentralDirectory(debug_file_name, archive);
496 return result != 0 ? result : ParseZipArchive(archive);
Narayan Kamath7462f022013-11-21 13:05:04 +0000497}
498
Jiyong Parkcd997e62017-06-30 17:23:33 +0900499int32_t OpenArchiveFd(int fd, const char* debug_file_name, ZipArchiveHandle* handle,
500 bool assume_ownership) {
Dmitriy Ivanov40b52b22014-07-15 19:33:00 -0700501 ZipArchive* archive = new ZipArchive(fd, assume_ownership);
Narayan Kamath7462f022013-11-21 13:05:04 +0000502 *handle = archive;
Narayan Kamath7462f022013-11-21 13:05:04 +0000503 return OpenArchiveInternal(archive, debug_file_name);
504}
505
506int32_t OpenArchive(const char* fileName, ZipArchiveHandle* handle) {
Nick Kralevich3bdf7442018-12-18 12:48:06 -0800507 const int fd = ::android::base::utf8::open(fileName, O_RDONLY | O_BINARY | O_CLOEXEC, 0);
Dmitriy Ivanov40b52b22014-07-15 19:33:00 -0700508 ZipArchive* archive = new ZipArchive(fd, true);
Narayan Kamath7462f022013-11-21 13:05:04 +0000509 *handle = archive;
510
Narayan Kamath7462f022013-11-21 13:05:04 +0000511 if (fd < 0) {
512 ALOGW("Unable to open '%s': %s", fileName, strerror(errno));
513 return kIoError;
Narayan Kamath7462f022013-11-21 13:05:04 +0000514 }
Dmitriy Ivanov40b52b22014-07-15 19:33:00 -0700515
Narayan Kamath7462f022013-11-21 13:05:04 +0000516 return OpenArchiveInternal(archive, fileName);
517}
518
Elliott Hughesf66460b2019-10-22 11:44:50 -0700519int32_t OpenArchiveFromMemory(const void* address, size_t length, const char* debug_file_name,
Jiyong Parkcd997e62017-06-30 17:23:33 +0900520 ZipArchiveHandle* handle) {
Tianjie Xu18c25922016-09-29 15:27:41 -0700521 ZipArchive* archive = new ZipArchive(address, length);
522 *handle = archive;
523 return OpenArchiveInternal(archive, debug_file_name);
524}
525
Elliott Hughes26724132019-10-25 09:57:58 -0700526ZipArchiveInfo GetArchiveInfo(ZipArchiveHandle archive) {
527 ZipArchiveInfo result;
528 result.archive_size = archive->mapped_zip.GetFileLength();
529 result.entry_count = archive->num_entries;
530 return result;
531}
532
Narayan Kamath7462f022013-11-21 13:05:04 +0000533/*
534 * Close a ZipArchive, closing the file and freeing the contents.
535 */
Ryan Prichard3673f992018-10-10 22:41:14 -0700536void CloseArchive(ZipArchiveHandle archive) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000537 ALOGV("Closing archive %p", archive);
Neil Fullerb1a113f2014-07-25 14:43:04 +0100538 delete archive;
Narayan Kamath7462f022013-11-21 13:05:04 +0000539}
540
Narayan Kamath162b7052017-06-05 13:21:12 +0100541static int32_t ValidateDataDescriptor(MappedZipFile& mapped_zip, ZipEntry* entry) {
Narayan Kamath926973e2014-06-09 14:18:14 +0100542 uint8_t ddBuf[sizeof(DataDescriptor) + sizeof(DataDescriptor::kOptSignature)];
Adam Lesinskide117e42017-06-19 10:27:38 -0700543 off64_t offset = entry->offset;
544 if (entry->method != kCompressStored) {
545 offset += entry->compressed_length;
546 } else {
547 offset += entry->uncompressed_length;
548 }
549
550 if (!mapped_zip.ReadAtOffset(ddBuf, sizeof(ddBuf), offset)) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000551 return kIoError;
552 }
553
Narayan Kamath926973e2014-06-09 14:18:14 +0100554 const uint32_t ddSignature = *(reinterpret_cast<const uint32_t*>(ddBuf));
Adam Lesinskide117e42017-06-19 10:27:38 -0700555 const uint16_t ddOffset = (ddSignature == DataDescriptor::kOptSignature) ? 4 : 0;
556 const DataDescriptor* descriptor = reinterpret_cast<const DataDescriptor*>(ddBuf + ddOffset);
Narayan Kamath7462f022013-11-21 13:05:04 +0000557
Narayan Kamath162b7052017-06-05 13:21:12 +0100558 // Validate that the values in the data descriptor match those in the central
559 // directory.
560 if (entry->compressed_length != descriptor->compressed_size ||
561 entry->uncompressed_length != descriptor->uncompressed_size ||
562 entry->crc32 != descriptor->crc32) {
563 ALOGW("Zip: size/crc32 mismatch. expected {%" PRIu32 ", %" PRIu32 ", %" PRIx32
564 "}, was {%" PRIu32 ", %" PRIu32 ", %" PRIx32 "}",
565 entry->compressed_length, entry->uncompressed_length, entry->crc32,
566 descriptor->compressed_size, descriptor->uncompressed_size, descriptor->crc32);
567 return kInconsistentInformation;
568 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000569
570 return 0;
571}
572
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800573static int32_t FindEntry(const ZipArchive* archive, std::string_view entryName,
574 const uint64_t nameOffset, ZipEntry* data) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000575 // Recover the start of the central directory entry from the filename
576 // pointer. The filename is the first entry past the fixed-size data,
577 // so we can just subtract back from that.
Elliott Hughes50ef29a2019-06-18 18:23:59 -0700578 const uint8_t* base_ptr = archive->central_directory.GetBasePtr();
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800579 const uint8_t* ptr = base_ptr + nameOffset;
Narayan Kamath926973e2014-06-09 14:18:14 +0100580 ptr -= sizeof(CentralDirectoryRecord);
Narayan Kamath7462f022013-11-21 13:05:04 +0000581
582 // This is the base of our mmapped region, we have to sanity check that
583 // the name that's in the hash table is a pointer to a location within
584 // this mapped region.
Tianjie Xu18c25922016-09-29 15:27:41 -0700585 if (ptr < base_ptr || ptr > base_ptr + archive->central_directory.GetMapLength()) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000586 ALOGW("Zip: Invalid entry pointer");
587 return kInvalidOffset;
588 }
589
Jiyong Parkcd997e62017-06-30 17:23:33 +0900590 const CentralDirectoryRecord* cdr = reinterpret_cast<const CentralDirectoryRecord*>(ptr);
Narayan Kamath926973e2014-06-09 14:18:14 +0100591
Narayan Kamath7462f022013-11-21 13:05:04 +0000592 // The offset of the start of the central directory in the zipfile.
593 // We keep this lying around so that we can sanity check all our lengths
594 // and our per-file structures.
595 const off64_t cd_offset = archive->directory_offset;
596
597 // Fill out the compression method, modification time, crc32
598 // and other interesting attributes from the central directory. These
599 // will later be compared against values from the local file header.
Narayan Kamath926973e2014-06-09 14:18:14 +0100600 data->method = cdr->compression_method;
beonit0e99a2f2015-07-18 02:08:16 +0900601 data->mod_time = cdr->last_mod_date << 16 | cdr->last_mod_time;
Narayan Kamath926973e2014-06-09 14:18:14 +0100602 data->crc32 = cdr->crc32;
603 data->compressed_length = cdr->compressed_size;
604 data->uncompressed_length = cdr->uncompressed_size;
Narayan Kamath7462f022013-11-21 13:05:04 +0000605
606 // Figure out the local header offset from the central directory. The
607 // actual file data will begin after the local header and the name /
608 // extra comments.
Narayan Kamath926973e2014-06-09 14:18:14 +0100609 const off64_t local_header_offset = cdr->local_file_header_offset;
610 if (local_header_offset + static_cast<off64_t>(sizeof(LocalFileHeader)) >= cd_offset) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000611 ALOGW("Zip: bad local hdr offset in zip");
612 return kInvalidOffset;
613 }
614
Narayan Kamath926973e2014-06-09 14:18:14 +0100615 uint8_t lfh_buf[sizeof(LocalFileHeader)];
Tianjie Xu18c25922016-09-29 15:27:41 -0700616 if (!archive->mapped_zip.ReadAtOffset(lfh_buf, sizeof(lfh_buf), local_header_offset)) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800617 ALOGW("Zip: failed reading lfh name from offset %" PRId64,
Jiyong Parkcd997e62017-06-30 17:23:33 +0900618 static_cast<int64_t>(local_header_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000619 return kIoError;
620 }
621
Jiyong Parkcd997e62017-06-30 17:23:33 +0900622 const LocalFileHeader* lfh = reinterpret_cast<const LocalFileHeader*>(lfh_buf);
Narayan Kamath926973e2014-06-09 14:18:14 +0100623
624 if (lfh->lfh_signature != LocalFileHeader::kSignature) {
Mark Salyzyn99ef9912014-03-14 14:26:22 -0700625 ALOGW("Zip: didn't find signature at start of lfh, offset=%" PRId64,
Jiyong Parkcd997e62017-06-30 17:23:33 +0900626 static_cast<int64_t>(local_header_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000627 return kInvalidOffset;
628 }
629
630 // Paranoia: Match the values specified in the local file header
631 // to those specified in the central directory.
Adam Lesinskid987c9d2017-04-06 18:55:47 -0700632
Narayan Kamath162b7052017-06-05 13:21:12 +0100633 // Warn if central directory and local file header don't agree on the use
634 // of a trailing Data Descriptor. The reference implementation is inconsistent
635 // and appears to use the LFH value during extraction (unzip) but the CD value
636 // while displayng information about archives (zipinfo). The spec remains
637 // silent on this inconsistency as well.
638 //
639 // For now, always use the version from the LFH but make sure that the values
640 // specified in the central directory match those in the data descriptor.
641 //
642 // NOTE: It's also worth noting that unzip *does* warn about inconsistencies in
643 // bit 11 (EFS: The language encoding flag, marking that filename and comment are
644 // encoded using UTF-8). This implementation does not check for the presence of
645 // that flag and always enforces that entry names are valid UTF-8.
646 if ((lfh->gpb_flags & kGPBDDFlagMask) != (cdr->gpb_flags & kGPBDDFlagMask)) {
647 ALOGW("Zip: gpb flag mismatch at bit 3. expected {%04" PRIx16 "}, was {%04" PRIx16 "}",
Adam Lesinskid987c9d2017-04-06 18:55:47 -0700648 cdr->gpb_flags, lfh->gpb_flags);
Adam Lesinskid987c9d2017-04-06 18:55:47 -0700649 }
650
651 // If there is no trailing data descriptor, verify that the central directory and local file
652 // header agree on the crc, compressed, and uncompressed sizes of the entry.
Narayan Kamath926973e2014-06-09 14:18:14 +0100653 if ((lfh->gpb_flags & kGPBDDFlagMask) == 0) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000654 data->has_data_descriptor = 0;
Jiyong Parkcd997e62017-06-30 17:23:33 +0900655 if (data->compressed_length != lfh->compressed_size ||
656 data->uncompressed_length != lfh->uncompressed_size || data->crc32 != lfh->crc32) {
657 ALOGW("Zip: size/crc32 mismatch. expected {%" PRIu32 ", %" PRIu32 ", %" PRIx32
658 "}, was {%" PRIu32 ", %" PRIu32 ", %" PRIx32 "}",
659 data->compressed_length, data->uncompressed_length, data->crc32, lfh->compressed_size,
660 lfh->uncompressed_size, lfh->crc32);
Narayan Kamath7462f022013-11-21 13:05:04 +0000661 return kInconsistentInformation;
662 }
663 } else {
664 data->has_data_descriptor = 1;
665 }
666
Elliott Hughes55fd2932017-05-28 22:59:04 -0700667 // 4.4.2.1: the upper byte of `version_made_by` gives the source OS. Unix is 3.
Elliott Hughes26724132019-10-25 09:57:58 -0700668 data->version_made_by = cdr->version_made_by;
Elliott Hughesd5095252019-10-28 21:35:52 -0700669 data->external_file_attributes = cdr->external_file_attributes;
Elliott Hughes26724132019-10-25 09:57:58 -0700670 if ((data->version_made_by >> 8) == 3) {
Elliott Hughes55fd2932017-05-28 22:59:04 -0700671 data->unix_mode = (cdr->external_file_attributes >> 16) & 0xffff;
672 } else {
673 data->unix_mode = 0777;
674 }
675
Elliott Hughesd5095252019-10-28 21:35:52 -0700676 // 4.4.4: general purpose bit flags.
677 data->gpbf = lfh->gpb_flags;
678
Elliott Hughes26724132019-10-25 09:57:58 -0700679 // 4.4.14: the lowest bit of the internal file attributes field indicates text.
680 // Currently only needed to implement zipinfo.
681 data->is_text = (cdr->internal_file_attributes & 1);
682
Narayan Kamath7462f022013-11-21 13:05:04 +0000683 // Check that the local file header name matches the declared
684 // name in the central directory.
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800685 CHECK_LE(entryName.size(), UINT16_MAX);
686 auto nameLen = static_cast<uint16_t>(entryName.size());
Elliott Hughes50ef29a2019-06-18 18:23:59 -0700687 if (lfh->file_name_length != nameLen) {
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800688 ALOGW("Zip: lfh name length did not match central directory for %s: %" PRIu16 " %" PRIu16,
689 std::string(entryName).c_str(), lfh->file_name_length, nameLen);
Elliott Hughes50ef29a2019-06-18 18:23:59 -0700690 return kInconsistentInformation;
691 }
692 const off64_t name_offset = local_header_offset + sizeof(LocalFileHeader);
693 if (name_offset + lfh->file_name_length > cd_offset) {
694 ALOGW("Zip: lfh name has invalid declared length");
695 return kInvalidOffset;
696 }
697 std::vector<uint8_t> name_buf(nameLen);
698 if (!archive->mapped_zip.ReadAtOffset(name_buf.data(), nameLen, name_offset)) {
699 ALOGW("Zip: failed reading lfh name from offset %" PRId64, static_cast<int64_t>(name_offset));
700 return kIoError;
701 }
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800702 if (memcmp(entryName.data(), name_buf.data(), nameLen) != 0) {
Elliott Hughes50ef29a2019-06-18 18:23:59 -0700703 ALOGW("Zip: lfh name did not match central directory");
Narayan Kamath7462f022013-11-21 13:05:04 +0000704 return kInconsistentInformation;
705 }
706
Jiyong Parkcd997e62017-06-30 17:23:33 +0900707 const off64_t data_offset = local_header_offset + sizeof(LocalFileHeader) +
708 lfh->file_name_length + lfh->extra_field_length;
Narayan Kamath48953a12014-01-24 12:32:39 +0000709 if (data_offset > cd_offset) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800710 ALOGW("Zip: bad data offset %" PRId64 " in zip", static_cast<int64_t>(data_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000711 return kInvalidOffset;
712 }
713
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800714 if (static_cast<off64_t>(data_offset + data->compressed_length) > cd_offset) {
Mark Salyzyn088bf902014-05-08 16:02:20 -0700715 ALOGW("Zip: bad compressed length in zip (%" PRId64 " + %" PRIu32 " > %" PRId64 ")",
Jiyong Parkcd997e62017-06-30 17:23:33 +0900716 static_cast<int64_t>(data_offset), data->compressed_length,
717 static_cast<int64_t>(cd_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000718 return kInvalidOffset;
719 }
720
721 if (data->method == kCompressStored &&
Jiyong Parkcd997e62017-06-30 17:23:33 +0900722 static_cast<off64_t>(data_offset + data->uncompressed_length) > cd_offset) {
723 ALOGW("Zip: bad uncompressed length in zip (%" PRId64 " + %" PRIu32 " > %" PRId64 ")",
724 static_cast<int64_t>(data_offset), data->uncompressed_length,
725 static_cast<int64_t>(cd_offset));
726 return kInvalidOffset;
Narayan Kamath7462f022013-11-21 13:05:04 +0000727 }
728
729 data->offset = data_offset;
730 return 0;
731}
732
733struct IterationHandle {
Narayan Kamath7462f022013-11-21 13:05:04 +0000734 ZipArchive* archive;
Piotr Jastrzebski8e085362014-08-18 11:37:45 +0100735
Elliott Hughes50ef29a2019-06-18 18:23:59 -0700736 std::string prefix;
737 std::string suffix;
Elliott Hughesa22ac0f2019-05-08 10:44:06 -0700738
739 uint32_t position = 0;
740
Elliott Hughes50ef29a2019-06-18 18:23:59 -0700741 IterationHandle(ZipArchive* archive, std::string_view in_prefix, std::string_view in_suffix)
742 : archive(archive), prefix(in_prefix), suffix(in_suffix) {}
Narayan Kamath7462f022013-11-21 13:05:04 +0000743};
744
Ryan Prichard3673f992018-10-10 22:41:14 -0700745int32_t StartIteration(ZipArchiveHandle archive, void** cookie_ptr,
Elliott Hughesa22ac0f2019-05-08 10:44:06 -0700746 const std::string_view optional_prefix,
747 const std::string_view optional_suffix) {
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800748 if (archive == nullptr || archive->cd_entry_map == nullptr) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000749 ALOGW("Zip: Invalid ZipArchiveHandle");
750 return kInvalidHandle;
751 }
752
Elliott Hughesa22ac0f2019-05-08 10:44:06 -0700753 if (optional_prefix.size() > static_cast<size_t>(UINT16_MAX) ||
754 optional_suffix.size() > static_cast<size_t>(UINT16_MAX)) {
755 ALOGW("Zip: prefix/suffix too long");
756 return kInvalidEntryName;
757 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000758
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800759 archive->cd_entry_map->ResetIteration();
Elliott Hughesa22ac0f2019-05-08 10:44:06 -0700760 *cookie_ptr = new IterationHandle(archive, optional_prefix, optional_suffix);
Narayan Kamath7462f022013-11-21 13:05:04 +0000761 return 0;
762}
763
Piotr Jastrzebski79c8b342014-08-08 14:02:17 +0100764void EndIteration(void* cookie) {
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100765 delete reinterpret_cast<IterationHandle*>(cookie);
Piotr Jastrzebski79c8b342014-08-08 14:02:17 +0100766}
767
Elliott Hughesb17bf522019-05-03 22:38:44 -0700768int32_t FindEntry(const ZipArchiveHandle archive, const std::string_view entryName,
769 ZipEntry* data) {
770 if (entryName.empty() || entryName.size() > static_cast<size_t>(UINT16_MAX)) {
771 ALOGW("Zip: Invalid filename of length %zu", entryName.size());
772 return kInvalidEntryName;
773 }
774
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800775 const auto [result, offset] =
776 archive->cd_entry_map->GetCdEntryOffset(entryName, archive->central_directory.GetBasePtr());
777 if (result != 0) {
Elliott Hughesb17bf522019-05-03 22:38:44 -0700778 ALOGV("Zip: Could not find entry %.*s", static_cast<int>(entryName.size()), entryName.data());
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800779 return static_cast<int32_t>(result); // kEntryNotFound is safe to truncate.
Elliott Hughesb17bf522019-05-03 22:38:44 -0700780 }
Elliott Hughesa5ff19e2019-05-07 09:27:59 -0700781 // We know there are at most hash_table_size entries, safe to truncate.
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800782 return FindEntry(archive, entryName, offset, data);
Elliott Hughesb17bf522019-05-03 22:38:44 -0700783}
784
Elliott Hughese06a8082019-05-22 18:56:41 -0700785int32_t Next(void* cookie, ZipEntry* data, std::string* name) {
Elliott Hughes1e40c302019-06-12 12:12:47 -0700786 std::string_view sv;
787 int32_t result = Next(cookie, data, &sv);
788 if (result == 0 && name) {
789 *name = std::string(sv);
790 }
791 return result;
792}
793
794int32_t Next(void* cookie, ZipEntry* data, std::string_view* name) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800795 IterationHandle* handle = reinterpret_cast<IterationHandle*>(cookie);
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800796 if (handle == nullptr) {
Zimuzo5a503ef2018-09-17 19:49:55 +0100797 ALOGW("Zip: Null ZipArchiveHandle");
Narayan Kamath7462f022013-11-21 13:05:04 +0000798 return kInvalidHandle;
799 }
800
801 ZipArchive* archive = handle->archive;
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800802 if (archive == nullptr || archive->cd_entry_map == nullptr) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000803 ALOGW("Zip: Invalid ZipArchiveHandle");
804 return kInvalidHandle;
805 }
806
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800807 auto entry = archive->cd_entry_map->Next(archive->central_directory.GetBasePtr());
808 while (entry != std::pair<std::string_view, uint64_t>()) {
809 const auto [entry_name, offset] = entry;
810 if (android::base::StartsWith(entry_name, handle->prefix) &&
811 android::base::EndsWith(entry_name, handle->suffix)) {
812 const int error = FindEntry(archive, entry_name, offset, data);
Elliott Hughes50ef29a2019-06-18 18:23:59 -0700813 if (!error && name) {
814 *name = entry_name;
Narayan Kamath7462f022013-11-21 13:05:04 +0000815 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000816 return error;
817 }
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800818 entry = archive->cd_entry_map->Next(archive->central_directory.GetBasePtr());
Narayan Kamath7462f022013-11-21 13:05:04 +0000819 }
820
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800821 archive->cd_entry_map->ResetIteration();
Narayan Kamath7462f022013-11-21 13:05:04 +0000822 return kIterationEnd;
823}
824
Narayan Kamathf899bd52015-04-17 11:53:14 +0100825// A Writer that writes data to a fixed size memory region.
826// The size of the memory region must be equal to the total size of
827// the data appended to it.
Narayan Kamath485b3642017-10-26 14:42:39 +0100828class MemoryWriter : public zip_archive::Writer {
Narayan Kamathf899bd52015-04-17 11:53:14 +0100829 public:
Jiyong Parkcd997e62017-06-30 17:23:33 +0900830 MemoryWriter(uint8_t* buf, size_t size) : Writer(), buf_(buf), size_(size), bytes_written_(0) {}
Narayan Kamathf899bd52015-04-17 11:53:14 +0100831
832 virtual bool Append(uint8_t* buf, size_t buf_size) override {
833 if (bytes_written_ + buf_size > size_) {
Elliott Hughese8f4b142018-10-19 16:09:39 -0700834 ALOGW("Zip: Unexpected size %zu (declared) vs %zu (actual)", size_,
Jiyong Parkcd997e62017-06-30 17:23:33 +0900835 bytes_written_ + buf_size);
Narayan Kamathf899bd52015-04-17 11:53:14 +0100836 return false;
837 }
838
839 memcpy(buf_ + bytes_written_, buf, buf_size);
840 bytes_written_ += buf_size;
841 return true;
842 }
843
844 private:
845 uint8_t* const buf_;
846 const size_t size_;
847 size_t bytes_written_;
848};
849
850// A Writer that appends data to a file |fd| at its current position.
851// The file will be truncated to the end of the written data.
Narayan Kamath485b3642017-10-26 14:42:39 +0100852class FileWriter : public zip_archive::Writer {
Narayan Kamathf899bd52015-04-17 11:53:14 +0100853 public:
Narayan Kamathf899bd52015-04-17 11:53:14 +0100854 // Creates a FileWriter for |fd| and prepare to write |entry| to it,
855 // guaranteeing that the file descriptor is valid and that there's enough
856 // space on the volume to write out the entry completely and that the file
Tao Baoa456c212016-11-15 10:08:07 -0800857 // is truncated to the correct length (no truncation if |fd| references a
858 // block device).
Narayan Kamathf899bd52015-04-17 11:53:14 +0100859 //
860 // Returns a valid FileWriter on success, |nullptr| if an error occurred.
Yurii Zubrytskyi834326c2017-12-20 01:01:01 -0800861 static FileWriter Create(int fd, const ZipEntry* entry) {
Narayan Kamathf899bd52015-04-17 11:53:14 +0100862 const uint32_t declared_length = entry->uncompressed_length;
863 const off64_t current_offset = lseek64(fd, 0, SEEK_CUR);
864 if (current_offset == -1) {
865 ALOGW("Zip: unable to seek to current location on fd %d: %s", fd, strerror(errno));
Yurii Zubrytskyi834326c2017-12-20 01:01:01 -0800866 return FileWriter{};
Narayan Kamathf899bd52015-04-17 11:53:14 +0100867 }
868
Narayan Kamathf899bd52015-04-17 11:53:14 +0100869#if defined(__linux__)
870 if (declared_length > 0) {
871 // Make sure we have enough space on the volume to extract the compressed
872 // entry. Note that the call to ftruncate below will change the file size but
873 // will not allocate space on disk and this call to fallocate will not
874 // change the file size.
Badhri Jagan Sridharana68d0d12015-06-02 14:47:57 -0700875 // Note: fallocate is only supported by the following filesystems -
876 // btrfs, ext4, ocfs2, and xfs. Therefore fallocate might fail with
877 // EOPNOTSUPP error when issued in other filesystems.
878 // Hence, check for the return error code before concluding that the
879 // disk does not have enough space.
Andreas Gampe964b95c2019-04-05 13:48:02 -0700880 long result = TEMP_FAILURE_RETRY(fallocate(fd, 0, current_offset, declared_length));
Badhri Jagan Sridharana68d0d12015-06-02 14:47:57 -0700881 if (result == -1 && errno == ENOSPC) {
Elliott Hughes4089d342017-10-27 14:21:12 -0700882 ALOGW("Zip: unable to allocate %" PRId64 " bytes at offset %" PRId64 ": %s",
Narayan Kamathd5d7abe2016-08-10 12:24:05 +0100883 static_cast<int64_t>(declared_length), static_cast<int64_t>(current_offset),
884 strerror(errno));
Yurii Zubrytskyi834326c2017-12-20 01:01:01 -0800885 return FileWriter{};
Narayan Kamathf899bd52015-04-17 11:53:14 +0100886 }
887 }
888#endif // __linux__
889
Tao Baoa456c212016-11-15 10:08:07 -0800890 struct stat sb;
891 if (fstat(fd, &sb) == -1) {
892 ALOGW("Zip: unable to fstat file: %s", strerror(errno));
Yurii Zubrytskyi834326c2017-12-20 01:01:01 -0800893 return FileWriter{};
Narayan Kamathf899bd52015-04-17 11:53:14 +0100894 }
895
Tao Baoa456c212016-11-15 10:08:07 -0800896 // Block device doesn't support ftruncate(2).
897 if (!S_ISBLK(sb.st_mode)) {
Andreas Gampe964b95c2019-04-05 13:48:02 -0700898 long result = TEMP_FAILURE_RETRY(ftruncate(fd, declared_length + current_offset));
Tao Baoa456c212016-11-15 10:08:07 -0800899 if (result == -1) {
900 ALOGW("Zip: unable to truncate file to %" PRId64 ": %s",
901 static_cast<int64_t>(declared_length + current_offset), strerror(errno));
Yurii Zubrytskyi834326c2017-12-20 01:01:01 -0800902 return FileWriter{};
Tao Baoa456c212016-11-15 10:08:07 -0800903 }
904 }
905
Yurii Zubrytskyi834326c2017-12-20 01:01:01 -0800906 return FileWriter(fd, declared_length);
Narayan Kamathf899bd52015-04-17 11:53:14 +0100907 }
908
Chih-Hung Hsieh747eb142018-09-25 11:16:22 -0700909 FileWriter(FileWriter&& other) noexcept
Yurii Zubrytskyi834326c2017-12-20 01:01:01 -0800910 : fd_(other.fd_),
911 declared_length_(other.declared_length_),
912 total_bytes_written_(other.total_bytes_written_) {
913 other.fd_ = -1;
914 }
915
916 bool IsValid() const { return fd_ != -1; }
917
Narayan Kamathf899bd52015-04-17 11:53:14 +0100918 virtual bool Append(uint8_t* buf, size_t buf_size) override {
919 if (total_bytes_written_ + buf_size > declared_length_) {
Elliott Hughese8f4b142018-10-19 16:09:39 -0700920 ALOGW("Zip: Unexpected size %zu (declared) vs %zu (actual)", declared_length_,
Jiyong Parkcd997e62017-06-30 17:23:33 +0900921 total_bytes_written_ + buf_size);
Narayan Kamathf899bd52015-04-17 11:53:14 +0100922 return false;
923 }
924
Narayan Kamathe97e66e2015-04-27 16:25:53 +0100925 const bool result = android::base::WriteFully(fd_, buf, buf_size);
926 if (result) {
927 total_bytes_written_ += buf_size;
928 } else {
Elliott Hughese8f4b142018-10-19 16:09:39 -0700929 ALOGW("Zip: unable to write %zu bytes to file; %s", buf_size, strerror(errno));
Narayan Kamathf899bd52015-04-17 11:53:14 +0100930 }
931
Narayan Kamathe97e66e2015-04-27 16:25:53 +0100932 return result;
Narayan Kamathf899bd52015-04-17 11:53:14 +0100933 }
Jiyong Parkcd997e62017-06-30 17:23:33 +0900934
Narayan Kamathf899bd52015-04-17 11:53:14 +0100935 private:
Yurii Zubrytskyi834326c2017-12-20 01:01:01 -0800936 explicit FileWriter(const int fd = -1, const size_t declared_length = 0)
Jiyong Parkcd997e62017-06-30 17:23:33 +0900937 : Writer(), fd_(fd), declared_length_(declared_length), total_bytes_written_(0) {}
Narayan Kamathf899bd52015-04-17 11:53:14 +0100938
Yurii Zubrytskyi834326c2017-12-20 01:01:01 -0800939 int fd_;
Narayan Kamathf899bd52015-04-17 11:53:14 +0100940 const size_t declared_length_;
941 size_t total_bytes_written_;
942};
943
Narayan Kamath485b3642017-10-26 14:42:39 +0100944class EntryReader : public zip_archive::Reader {
Narayan Kamath8b8faed2017-10-26 14:08:38 +0100945 public:
946 EntryReader(const MappedZipFile& zip_file, const ZipEntry* entry)
947 : Reader(), zip_file_(zip_file), entry_(entry) {}
948
949 virtual bool ReadAtOffset(uint8_t* buf, size_t len, uint32_t offset) const {
950 return zip_file_.ReadAtOffset(buf, len, entry_->offset + offset);
951 }
952
953 virtual ~EntryReader() {}
954
955 private:
956 const MappedZipFile& zip_file_;
957 const ZipEntry* entry_;
958};
959
Dmitriy Ivanovf94e1592015-03-06 13:27:59 -0800960// This method is using libz macros with old-style-casts
961#pragma GCC diagnostic push
962#pragma GCC diagnostic ignored "-Wold-style-cast"
963static inline int zlib_inflateInit2(z_stream* stream, int window_bits) {
964 return inflateInit2(stream, window_bits);
965}
966#pragma GCC diagnostic pop
967
Narayan Kamath485b3642017-10-26 14:42:39 +0100968namespace zip_archive {
969
970// Moved out of line to avoid -Wweak-vtables.
971Reader::~Reader() {}
972Writer::~Writer() {}
973
974int32_t Inflate(const Reader& reader, const uint32_t compressed_length,
975 const uint32_t uncompressed_length, Writer* writer, uint64_t* crc_out) {
Dmitriy Ivanovedbabfe2015-03-12 09:58:15 -0700976 const size_t kBufSize = 32768;
977 std::vector<uint8_t> read_buf(kBufSize);
978 std::vector<uint8_t> write_buf(kBufSize);
Narayan Kamath7462f022013-11-21 13:05:04 +0000979 z_stream zstream;
980 int zerr;
981
982 /*
983 * Initialize the zlib stream struct.
984 */
985 memset(&zstream, 0, sizeof(zstream));
986 zstream.zalloc = Z_NULL;
987 zstream.zfree = Z_NULL;
988 zstream.opaque = Z_NULL;
989 zstream.next_in = NULL;
990 zstream.avail_in = 0;
Dmitriy Ivanovedbabfe2015-03-12 09:58:15 -0700991 zstream.next_out = &write_buf[0];
Narayan Kamath7462f022013-11-21 13:05:04 +0000992 zstream.avail_out = kBufSize;
993 zstream.data_type = Z_UNKNOWN;
994
995 /*
996 * Use the undocumented "negative window bits" feature to tell zlib
997 * that there's no zlib header waiting for it.
998 */
Dmitriy Ivanovf94e1592015-03-06 13:27:59 -0800999 zerr = zlib_inflateInit2(&zstream, -MAX_WBITS);
Narayan Kamath7462f022013-11-21 13:05:04 +00001000 if (zerr != Z_OK) {
1001 if (zerr == Z_VERSION_ERROR) {
Jiyong Parkcd997e62017-06-30 17:23:33 +09001002 ALOGE("Installed zlib is not compatible with linked version (%s)", ZLIB_VERSION);
Narayan Kamath7462f022013-11-21 13:05:04 +00001003 } else {
1004 ALOGW("Call to inflateInit2 failed (zerr=%d)", zerr);
1005 }
1006
1007 return kZlibError;
1008 }
1009
Dmitriy Ivanov1f741e52015-03-06 14:26:37 -08001010 auto zstream_deleter = [](z_stream* stream) {
Jiyong Parkcd997e62017-06-30 17:23:33 +09001011 inflateEnd(stream); /* free up any allocated structures */
Dmitriy Ivanov1f741e52015-03-06 14:26:37 -08001012 };
1013
1014 std::unique_ptr<z_stream, decltype(zstream_deleter)> zstream_guard(&zstream, zstream_deleter);
1015
Narayan Kamath2d1e23f2017-10-30 11:17:28 +00001016 const bool compute_crc = (crc_out != nullptr);
Andreas Gampe964b95c2019-04-05 13:48:02 -07001017 uLong crc = 0;
Narayan Kamath8b8faed2017-10-26 14:08:38 +01001018 uint32_t remaining_bytes = compressed_length;
Narayan Kamath7462f022013-11-21 13:05:04 +00001019 do {
1020 /* read as much as we can */
1021 if (zstream.avail_in == 0) {
Andreas Gampe964b95c2019-04-05 13:48:02 -07001022 const uint32_t read_size = (remaining_bytes > kBufSize) ? kBufSize : remaining_bytes;
Narayan Kamath8b8faed2017-10-26 14:08:38 +01001023 const uint32_t offset = (compressed_length - remaining_bytes);
Adam Lesinskide117e42017-06-19 10:27:38 -07001024 // Make sure to read at offset to ensure concurrent access to the fd.
Narayan Kamath8b8faed2017-10-26 14:08:38 +01001025 if (!reader.ReadAtOffset(read_buf.data(), read_size, offset)) {
Andreas Gampe964b95c2019-04-05 13:48:02 -07001026 ALOGW("Zip: inflate read failed, getSize = %u: %s", read_size, strerror(errno));
Dmitriy Ivanov1f741e52015-03-06 14:26:37 -08001027 return kIoError;
Narayan Kamath7462f022013-11-21 13:05:04 +00001028 }
1029
Narayan Kamath8b8faed2017-10-26 14:08:38 +01001030 remaining_bytes -= read_size;
Narayan Kamath7462f022013-11-21 13:05:04 +00001031
Dmitriy Ivanovedbabfe2015-03-12 09:58:15 -07001032 zstream.next_in = &read_buf[0];
Narayan Kamath8b8faed2017-10-26 14:08:38 +01001033 zstream.avail_in = read_size;
Narayan Kamath7462f022013-11-21 13:05:04 +00001034 }
1035
1036 /* uncompress the data */
1037 zerr = inflate(&zstream, Z_NO_FLUSH);
1038 if (zerr != Z_OK && zerr != Z_STREAM_END) {
Jiyong Parkcd997e62017-06-30 17:23:33 +09001039 ALOGW("Zip: inflate zerr=%d (nIn=%p aIn=%u nOut=%p aOut=%u)", zerr, zstream.next_in,
1040 zstream.avail_in, zstream.next_out, zstream.avail_out);
Dmitriy Ivanov1f741e52015-03-06 14:26:37 -08001041 return kZlibError;
Narayan Kamath7462f022013-11-21 13:05:04 +00001042 }
1043
1044 /* write when we're full or when we're done */
Jiyong Parkcd997e62017-06-30 17:23:33 +09001045 if (zstream.avail_out == 0 || (zerr == Z_STREAM_END && zstream.avail_out != kBufSize)) {
Dmitriy Ivanovedbabfe2015-03-12 09:58:15 -07001046 const size_t write_size = zstream.next_out - &write_buf[0];
Narayan Kamathf899bd52015-04-17 11:53:14 +01001047 if (!writer->Append(&write_buf[0], write_size)) {
Narayan Kamath2d1e23f2017-10-30 11:17:28 +00001048 return kIoError;
1049 } else if (compute_crc) {
Andreas Gampe964b95c2019-04-05 13:48:02 -07001050 DCHECK_LE(write_size, kBufSize);
1051 crc = crc32(crc, &write_buf[0], static_cast<uint32_t>(write_size));
Narayan Kamath7462f022013-11-21 13:05:04 +00001052 }
Narayan Kamath7462f022013-11-21 13:05:04 +00001053
Dmitriy Ivanovedbabfe2015-03-12 09:58:15 -07001054 zstream.next_out = &write_buf[0];
Narayan Kamath7462f022013-11-21 13:05:04 +00001055 zstream.avail_out = kBufSize;
1056 }
1057 } while (zerr == Z_OK);
1058
Elliott Hughese8f4b142018-10-19 16:09:39 -07001059 CHECK_EQ(zerr, Z_STREAM_END); /* other errors should've been caught */
Narayan Kamath7462f022013-11-21 13:05:04 +00001060
Narayan Kamath162b7052017-06-05 13:21:12 +01001061 // NOTE: zstream.adler is always set to 0, because we're using the -MAX_WBITS
1062 // "feature" of zlib to tell it there won't be a zlib file header. zlib
1063 // doesn't bother calculating the checksum in that scenario. We just do
1064 // it ourselves above because there are no additional gains to be made by
1065 // having zlib calculate it for us, since they do it by calling crc32 in
1066 // the same manner that we have above.
Narayan Kamath2d1e23f2017-10-30 11:17:28 +00001067 if (compute_crc) {
1068 *crc_out = crc;
1069 }
Narayan Kamath7462f022013-11-21 13:05:04 +00001070
Narayan Kamath8b8faed2017-10-26 14:08:38 +01001071 if (zstream.total_out != uncompressed_length || remaining_bytes != 0) {
Jiyong Parkcd997e62017-06-30 17:23:33 +09001072 ALOGW("Zip: size mismatch on inflated file (%lu vs %" PRIu32 ")", zstream.total_out,
1073 uncompressed_length);
Dmitriy Ivanov1f741e52015-03-06 14:26:37 -08001074 return kInconsistentInformation;
Narayan Kamath7462f022013-11-21 13:05:04 +00001075 }
1076
Dmitriy Ivanov1f741e52015-03-06 14:26:37 -08001077 return 0;
Narayan Kamath7462f022013-11-21 13:05:04 +00001078}
Narayan Kamath485b3642017-10-26 14:42:39 +01001079} // namespace zip_archive
Narayan Kamath7462f022013-11-21 13:05:04 +00001080
Narayan Kamath8b8faed2017-10-26 14:08:38 +01001081static int32_t InflateEntryToWriter(MappedZipFile& mapped_zip, const ZipEntry* entry,
Narayan Kamath485b3642017-10-26 14:42:39 +01001082 zip_archive::Writer* writer, uint64_t* crc_out) {
Narayan Kamath8b8faed2017-10-26 14:08:38 +01001083 const EntryReader reader(mapped_zip, entry);
1084
Narayan Kamath485b3642017-10-26 14:42:39 +01001085 return zip_archive::Inflate(reader, entry->compressed_length, entry->uncompressed_length, writer,
1086 crc_out);
Narayan Kamath8b8faed2017-10-26 14:08:38 +01001087}
1088
Narayan Kamath485b3642017-10-26 14:42:39 +01001089static int32_t CopyEntryToWriter(MappedZipFile& mapped_zip, const ZipEntry* entry,
1090 zip_archive::Writer* writer, uint64_t* crc_out) {
Narayan Kamathf899bd52015-04-17 11:53:14 +01001091 static const uint32_t kBufSize = 32768;
1092 std::vector<uint8_t> buf(kBufSize);
1093
1094 const uint32_t length = entry->uncompressed_length;
1095 uint32_t count = 0;
Andreas Gampe964b95c2019-04-05 13:48:02 -07001096 uLong crc = 0;
Narayan Kamathf899bd52015-04-17 11:53:14 +01001097 while (count < length) {
1098 uint32_t remaining = length - count;
Adam Lesinskide117e42017-06-19 10:27:38 -07001099 off64_t offset = entry->offset + count;
Narayan Kamathf899bd52015-04-17 11:53:14 +01001100
Adam Lesinskide117e42017-06-19 10:27:38 -07001101 // Safe conversion because kBufSize is narrow enough for a 32 bit signed value.
Andreas Gampe964b95c2019-04-05 13:48:02 -07001102 const uint32_t block_size = (remaining > kBufSize) ? kBufSize : remaining;
Adam Lesinskide117e42017-06-19 10:27:38 -07001103
1104 // Make sure to read at offset to ensure concurrent access to the fd.
1105 if (!mapped_zip.ReadAtOffset(buf.data(), block_size, offset)) {
Andreas Gampe964b95c2019-04-05 13:48:02 -07001106 ALOGW("CopyFileToFile: copy read failed, block_size = %u, offset = %" PRId64 ": %s",
Adam Lesinskide117e42017-06-19 10:27:38 -07001107 block_size, static_cast<int64_t>(offset), strerror(errno));
Narayan Kamathf899bd52015-04-17 11:53:14 +01001108 return kIoError;
1109 }
1110
1111 if (!writer->Append(&buf[0], block_size)) {
1112 return kIoError;
1113 }
1114 crc = crc32(crc, &buf[0], block_size);
1115 count += block_size;
1116 }
1117
1118 *crc_out = crc;
1119
1120 return 0;
1121}
1122
Ryan Prichard3673f992018-10-10 22:41:14 -07001123int32_t ExtractToWriter(ZipArchiveHandle archive, ZipEntry* entry, zip_archive::Writer* writer) {
Narayan Kamath7462f022013-11-21 13:05:04 +00001124 const uint16_t method = entry->method;
Narayan Kamath7462f022013-11-21 13:05:04 +00001125
1126 // this should default to kUnknownCompressionMethod.
1127 int32_t return_value = -1;
1128 uint64_t crc = 0;
1129 if (method == kCompressStored) {
Tianjie Xu18c25922016-09-29 15:27:41 -07001130 return_value = CopyEntryToWriter(archive->mapped_zip, entry, writer, &crc);
Narayan Kamath7462f022013-11-21 13:05:04 +00001131 } else if (method == kCompressDeflated) {
Tianjie Xu18c25922016-09-29 15:27:41 -07001132 return_value = InflateEntryToWriter(archive->mapped_zip, entry, writer, &crc);
Narayan Kamath7462f022013-11-21 13:05:04 +00001133 }
1134
1135 if (!return_value && entry->has_data_descriptor) {
Narayan Kamath162b7052017-06-05 13:21:12 +01001136 return_value = ValidateDataDescriptor(archive->mapped_zip, entry);
Narayan Kamath7462f022013-11-21 13:05:04 +00001137 if (return_value) {
1138 return return_value;
1139 }
1140 }
1141
Narayan Kamath162b7052017-06-05 13:21:12 +01001142 // Validate that the CRC matches the calculated value.
1143 if (kCrcChecksEnabled && (entry->crc32 != static_cast<uint32_t>(crc))) {
Mark Salyzyn088bf902014-05-08 16:02:20 -07001144 ALOGW("Zip: crc mismatch: expected %" PRIu32 ", was %" PRIu64, entry->crc32, crc);
Narayan Kamath7462f022013-11-21 13:05:04 +00001145 return kInconsistentInformation;
1146 }
1147
1148 return return_value;
1149}
1150
Ryan Prichard3673f992018-10-10 22:41:14 -07001151int32_t ExtractToMemory(ZipArchiveHandle archive, ZipEntry* entry, uint8_t* begin, uint32_t size) {
Yurii Zubrytskyi834326c2017-12-20 01:01:01 -08001152 MemoryWriter writer(begin, size);
Ryan Prichard3673f992018-10-10 22:41:14 -07001153 return ExtractToWriter(archive, entry, &writer);
Narayan Kamathf899bd52015-04-17 11:53:14 +01001154}
1155
Ryan Prichard3673f992018-10-10 22:41:14 -07001156int32_t ExtractEntryToFile(ZipArchiveHandle archive, ZipEntry* entry, int fd) {
Yurii Zubrytskyi834326c2017-12-20 01:01:01 -08001157 auto writer = FileWriter::Create(fd, entry);
1158 if (!writer.IsValid()) {
Narayan Kamath7462f022013-11-21 13:05:04 +00001159 return kIoError;
1160 }
1161
Ryan Prichard3673f992018-10-10 22:41:14 -07001162 return ExtractToWriter(archive, entry, &writer);
Narayan Kamath7462f022013-11-21 13:05:04 +00001163}
1164
1165const char* ErrorCodeString(int32_t error_code) {
Narayan Kamath1ef9d2d2017-06-15 13:58:25 +01001166 // Make sure that the number of entries in kErrorMessages and ErrorCodes
1167 // match.
1168 static_assert((-kLastErrorCode + 1) == arraysize(kErrorMessages),
1169 "(-kLastErrorCode + 1) != arraysize(kErrorMessages)");
1170
1171 const uint32_t idx = -error_code;
1172 if (idx < arraysize(kErrorMessages)) {
1173 return kErrorMessages[idx];
Narayan Kamath7462f022013-11-21 13:05:04 +00001174 }
1175
Narayan Kamath1ef9d2d2017-06-15 13:58:25 +01001176 return "Unknown return code";
Narayan Kamath7462f022013-11-21 13:05:04 +00001177}
1178
Ryan Prichard3673f992018-10-10 22:41:14 -07001179int GetFileDescriptor(const ZipArchiveHandle archive) {
1180 return archive->mapped_zip.GetFileDescriptor();
Narayan Kamath7462f022013-11-21 13:05:04 +00001181}
Colin Cross7c6c7f02016-09-16 10:15:51 -07001182
Tianjie Xu18c25922016-09-29 15:27:41 -07001183#if !defined(_WIN32)
Narayan Kamath485b3642017-10-26 14:42:39 +01001184class ProcessWriter : public zip_archive::Writer {
Tianjie Xu18c25922016-09-29 15:27:41 -07001185 public:
Jiyong Parkcd997e62017-06-30 17:23:33 +09001186 ProcessWriter(ProcessZipEntryFunction func, void* cookie)
1187 : Writer(), proc_function_(func), cookie_(cookie) {}
Tianjie Xu18c25922016-09-29 15:27:41 -07001188
1189 virtual bool Append(uint8_t* buf, size_t buf_size) override {
1190 return proc_function_(buf, buf_size, cookie_);
1191 }
1192
1193 private:
1194 ProcessZipEntryFunction proc_function_;
1195 void* cookie_;
1196};
1197
Ryan Prichard3673f992018-10-10 22:41:14 -07001198int32_t ProcessZipEntryContents(ZipArchiveHandle archive, ZipEntry* entry,
Tianjie Xu18c25922016-09-29 15:27:41 -07001199 ProcessZipEntryFunction func, void* cookie) {
1200 ProcessWriter writer(func, cookie);
Ryan Prichard3673f992018-10-10 22:41:14 -07001201 return ExtractToWriter(archive, entry, &writer);
Tianjie Xu18c25922016-09-29 15:27:41 -07001202}
1203
Jiyong Parkcd997e62017-06-30 17:23:33 +09001204#endif //! defined(_WIN32)
Tianjie Xu18c25922016-09-29 15:27:41 -07001205
1206int MappedZipFile::GetFileDescriptor() const {
1207 if (!has_fd_) {
1208 ALOGW("Zip: MappedZipFile doesn't have a file descriptor.");
1209 return -1;
1210 }
1211 return fd_;
1212}
1213
Elliott Hughesf66460b2019-10-22 11:44:50 -07001214const void* MappedZipFile::GetBasePtr() const {
Tianjie Xu18c25922016-09-29 15:27:41 -07001215 if (has_fd_) {
1216 ALOGW("Zip: MappedZipFile doesn't have a base pointer.");
1217 return nullptr;
1218 }
1219 return base_ptr_;
1220}
1221
1222off64_t MappedZipFile::GetFileLength() const {
1223 if (has_fd_) {
1224 off64_t result = lseek64(fd_, 0, SEEK_END);
1225 if (result == -1) {
1226 ALOGE("Zip: lseek on fd %d failed: %s", fd_, strerror(errno));
1227 }
1228 return result;
1229 } else {
1230 if (base_ptr_ == nullptr) {
Elliott Hughesfba2a1a2019-12-16 16:16:16 -08001231 ALOGE("Zip: invalid file map");
Tianjie Xu18c25922016-09-29 15:27:41 -07001232 return -1;
1233 }
1234 return static_cast<off64_t>(data_length_);
1235 }
1236}
1237
Tianjie Xu18c25922016-09-29 15:27:41 -07001238// Attempts to read |len| bytes into |buf| at offset |off|.
Narayan Kamath8b8faed2017-10-26 14:08:38 +01001239bool MappedZipFile::ReadAtOffset(uint8_t* buf, size_t len, off64_t off) const {
Tianjie Xu18c25922016-09-29 15:27:41 -07001240 if (has_fd_) {
Adam Lesinskide117e42017-06-19 10:27:38 -07001241 if (!android::base::ReadFullyAtOffset(fd_, buf, len, off)) {
Elliott Hughesfba2a1a2019-12-16 16:16:16 -08001242 ALOGE("Zip: failed to read at offset %" PRId64, off);
Tianjie Xu18c25922016-09-29 15:27:41 -07001243 return false;
1244 }
Adam Lesinskide117e42017-06-19 10:27:38 -07001245 } else {
1246 if (off < 0 || off > static_cast<off64_t>(data_length_)) {
Elliott Hughesfba2a1a2019-12-16 16:16:16 -08001247 ALOGE("Zip: invalid offset: %" PRId64 ", data length: %" PRId64, off, data_length_);
Adam Lesinskide117e42017-06-19 10:27:38 -07001248 return false;
1249 }
Elliott Hughesf66460b2019-10-22 11:44:50 -07001250 memcpy(buf, static_cast<const uint8_t*>(base_ptr_) + off, len);
Tianjie Xu18c25922016-09-29 15:27:41 -07001251 }
Adam Lesinskide117e42017-06-19 10:27:38 -07001252 return true;
Tianjie Xu18c25922016-09-29 15:27:41 -07001253}
1254
Elliott Hughesf66460b2019-10-22 11:44:50 -07001255void CentralDirectory::Initialize(const void* map_base_ptr, off64_t cd_start_offset,
1256 size_t cd_size) {
1257 base_ptr_ = static_cast<const uint8_t*>(map_base_ptr) + cd_start_offset;
Tianjie Xu18c25922016-09-29 15:27:41 -07001258 length_ = cd_size;
1259}
1260
Elliott Hughese8f4b142018-10-19 16:09:39 -07001261bool ZipArchive::InitializeCentralDirectory(off64_t cd_start_offset, size_t cd_size) {
Tianjie Xu18c25922016-09-29 15:27:41 -07001262 if (mapped_zip.HasFd()) {
Elliott Hughese8f4b142018-10-19 16:09:39 -07001263 directory_map = android::base::MappedFile::FromFd(mapped_zip.GetFileDescriptor(),
1264 cd_start_offset, cd_size, PROT_READ);
Elliott Hughesfba2a1a2019-12-16 16:16:16 -08001265 if (!directory_map) {
1266 ALOGE("Zip: failed to map central directory (offset %" PRId64 ", size %zu): %s",
1267 cd_start_offset, cd_size, strerror(errno));
1268 return false;
1269 }
Tianjie Xu18c25922016-09-29 15:27:41 -07001270
Elliott Hughese8f4b142018-10-19 16:09:39 -07001271 CHECK_EQ(directory_map->size(), cd_size);
1272 central_directory.Initialize(directory_map->data(), 0 /*offset*/, cd_size);
Tianjie Xu18c25922016-09-29 15:27:41 -07001273 } else {
1274 if (mapped_zip.GetBasePtr() == nullptr) {
Elliott Hughesfba2a1a2019-12-16 16:16:16 -08001275 ALOGE("Zip: Failed to map central directory, bad mapped_zip base pointer");
Tianjie Xu18c25922016-09-29 15:27:41 -07001276 return false;
1277 }
1278 if (static_cast<off64_t>(cd_start_offset) + static_cast<off64_t>(cd_size) >
1279 mapped_zip.GetFileLength()) {
Jiyong Parkcd997e62017-06-30 17:23:33 +09001280 ALOGE(
1281 "Zip: Failed to map central directory, offset exceeds mapped memory region ("
1282 "start_offset %" PRId64 ", cd_size %zu, mapped_region_size %" PRId64 ")",
1283 static_cast<int64_t>(cd_start_offset), cd_size, mapped_zip.GetFileLength());
Tianjie Xu18c25922016-09-29 15:27:41 -07001284 return false;
1285 }
1286
1287 central_directory.Initialize(mapped_zip.GetBasePtr(), cd_start_offset, cd_size);
1288 }
1289 return true;
1290}
Elliott Hughes55fd2932017-05-28 22:59:04 -07001291
1292tm ZipEntry::GetModificationTime() const {
1293 tm t = {};
1294
1295 t.tm_hour = (mod_time >> 11) & 0x1f;
1296 t.tm_min = (mod_time >> 5) & 0x3f;
1297 t.tm_sec = (mod_time & 0x1f) << 1;
1298
1299 t.tm_year = ((mod_time >> 25) & 0x7f) + 80;
1300 t.tm_mon = ((mod_time >> 21) & 0xf) - 1;
1301 t.tm_mday = (mod_time >> 16) & 0x1f;
1302
1303 return t;
1304}