blob: 014f881312794099f5db7f52a57bd0346abcd6c7 [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>
Songchun Fanc33f5262020-03-24 09:15:51 -070035#include <optional>
Dan Albert1ae07642015-04-09 14:11:18 -070036#include <vector>
37
Elliott Hughes9c8bd662018-10-26 16:14:21 -070038#if defined(__APPLE__)
39#define lseek64 lseek
40#endif
41
Josh Gao1b496342018-07-17 11:08:48 -070042#if defined(__BIONIC__)
43#include <android/fdsan.h>
44#endif
45
Mark Salyzynff2dcd92016-09-28 15:54:45 -070046#include <android-base/file.h>
47#include <android-base/logging.h>
48#include <android-base/macros.h> // TEMP_FAILURE_RETRY may or may not be in unistd
Elliott Hughese8f4b142018-10-19 16:09:39 -070049#include <android-base/mapped_file.h>
Mark Salyzynff2dcd92016-09-28 15:54:45 -070050#include <android-base/memory.h>
Elliott Hughes50ef29a2019-06-18 18:23:59 -070051#include <android-base/strings.h>
Ryan Mitchellc77f9d32018-08-25 14:06:29 -070052#include <android-base/utf8.h>
Mark Salyzyncfd5b082016-10-17 14:28:00 -070053#include <log/log.h>
Dan Albert1ae07642015-04-09 14:11:18 -070054#include "zlib.h"
Narayan Kamath7462f022013-11-21 13:05:04 +000055
Narayan Kamath044bc8e2014-12-03 18:22:53 +000056#include "entry_name_utils-inl.h"
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -070057#include "zip_archive_common.h"
Christopher Ferrise6884ce2015-11-10 14:55:12 -080058#include "zip_archive_private.h"
Mark Salyzyn99ef9912014-03-14 14:26:22 -070059
Narayan Kamath162b7052017-06-05 13:21:12 +010060// Used to turn on crc checks - verify that the content CRC matches the values
61// specified in the local file header and the central directory.
Yurii Zubrytskyi8d8f6372020-04-06 19:35:33 -070062static constexpr bool kCrcChecksEnabled = false;
Narayan Kamath162b7052017-06-05 13:21:12 +010063
Narayan Kamath926973e2014-06-09 14:18:14 +010064// The maximum number of bytes to scan backwards for the EOCD start.
65static const uint32_t kMaxEOCDSearch = kMaxCommentLen + sizeof(EocdRecord);
66
Tianjie Xu69ee4b72020-03-11 11:59:10 -070067// Set a reasonable cap (256 GiB) for the zip file size. So the data is always valid when
68// we parse the fields in cd or local headers as 64 bits signed integers.
69static constexpr uint64_t kMaxFileLength = 256 * static_cast<uint64_t>(1u << 30u);
70
Narayan Kamath7462f022013-11-21 13:05:04 +000071/*
72 * A Read-only Zip archive.
73 *
74 * We want "open" and "find entry by name" to be fast operations, and
75 * we want to use as little memory as possible. We memory-map the zip
76 * central directory, and load a hash table with pointers to the filenames
77 * (which aren't null-terminated). The other fields are at a fixed offset
78 * from the filename, so we don't need to extract those (but we do need
79 * to byte-read and endian-swap them every time we want them).
80 *
81 * It's possible that somebody has handed us a massive (~1GB) zip archive,
82 * so we can't expect to mmap the entire file.
83 *
84 * To speed comparisons when doing a lookup by name, we could make the mapping
85 * "private" (copy-on-write) and null-terminate the filenames after verifying
86 * the record structure. However, this requires a private mapping of
87 * every page that the Central Directory touches. Easier to tuck a copy
88 * of the string length into the hash table entry.
89 */
Narayan Kamath7462f022013-11-21 13:05:04 +000090
Josh Gaoabdfc242018-09-07 12:44:40 -070091#if defined(__BIONIC__)
92uint64_t GetOwnerTag(const ZipArchive* archive) {
93 return android_fdsan_create_owner_tag(ANDROID_FDSAN_OWNER_TYPE_ZIPARCHIVE,
94 reinterpret_cast<uint64_t>(archive));
95}
96#endif
97
Ryan Mitchell23150e42020-03-09 09:33:46 -070098ZipArchive::ZipArchive(MappedZipFile&& map, bool assume_ownership)
99 : mapped_zip(map),
Josh Gao1b496342018-07-17 11:08:48 -0700100 close_file(assume_ownership),
101 directory_offset(0),
102 central_directory(),
Elliott Hughese8f4b142018-10-19 16:09:39 -0700103 directory_map(),
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800104 num_entries(0) {
Josh Gao1b496342018-07-17 11:08:48 -0700105#if defined(__BIONIC__)
106 if (assume_ownership) {
Ryan Mitchell23150e42020-03-09 09:33:46 -0700107 CHECK(mapped_zip.HasFd());
108 android_fdsan_exchange_owner_tag(mapped_zip.GetFileDescriptor(), 0, GetOwnerTag(this));
Josh Gao1b496342018-07-17 11:08:48 -0700109 }
110#endif
111}
112
Elliott Hughesf66460b2019-10-22 11:44:50 -0700113ZipArchive::ZipArchive(const void* address, size_t length)
Josh Gao1b496342018-07-17 11:08:48 -0700114 : mapped_zip(address, length),
115 close_file(false),
116 directory_offset(0),
117 central_directory(),
Elliott Hughese8f4b142018-10-19 16:09:39 -0700118 directory_map(),
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800119 num_entries(0) {}
Josh Gao1b496342018-07-17 11:08:48 -0700120
121ZipArchive::~ZipArchive() {
122 if (close_file && mapped_zip.GetFileDescriptor() >= 0) {
123#if defined(__BIONIC__)
Josh Gaoabdfc242018-09-07 12:44:40 -0700124 android_fdsan_close_with_tag(mapped_zip.GetFileDescriptor(), GetOwnerTag(this));
Josh Gao1b496342018-07-17 11:08:48 -0700125#else
126 close(mapped_zip.GetFileDescriptor());
127#endif
128 }
Josh Gao1b496342018-07-17 11:08:48 -0700129}
130
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700131struct CentralDirectoryInfo {
132 uint64_t num_records;
133 // The size of the central directory (in bytes).
134 uint64_t cd_size;
135 // The offset of the start of the central directory, relative
136 // to the start of the file.
137 uint64_t cd_start_offset;
138};
139
Tianjied9bc8fd2020-04-13 16:29:22 -0700140// Reads |T| at |readPtr| and increments |readPtr|. Returns std::nullopt if the boundary check
141// fails.
142template <typename T>
143static std::optional<T> TryConsumeUnaligned(uint8_t** readPtr, const uint8_t* bufStart,
144 size_t bufSize) {
145 if (bufSize < sizeof(T) || *readPtr - bufStart > bufSize - sizeof(T)) {
146 ALOGW("Zip: %zu byte read exceeds the boundary of allocated buf, offset %zu, bufSize %zu",
147 sizeof(T), *readPtr - bufStart, bufSize);
148 return std::nullopt;
149 }
150 return ConsumeUnaligned<T>(readPtr);
151}
152
Tianjie6ab29122020-03-18 17:44:30 -0700153static ZipError FindCentralDirectoryInfoForZip64(const char* debugFileName, ZipArchive* archive,
154 off64_t eocdOffset, CentralDirectoryInfo* cdInfo) {
155 if (eocdOffset <= sizeof(Zip64EocdLocator)) {
156 ALOGW("Zip: %s: Not enough space for zip64 eocd locator", debugFileName);
157 return kInvalidFile;
158 }
159 // We expect to find the zip64 eocd locator immediately before the zip eocd.
160 const int64_t locatorOffset = eocdOffset - sizeof(Zip64EocdLocator);
161 Zip64EocdLocator zip64EocdLocator{};
162 if (!archive->mapped_zip.ReadAtOffset(reinterpret_cast<uint8_t*>((&zip64EocdLocator)),
163 sizeof(Zip64EocdLocator), locatorOffset)) {
164 ALOGW("Zip: %s: Read %zu from offset %" PRId64 " failed %s", debugFileName,
165 sizeof(Zip64EocdLocator), locatorOffset, debugFileName);
166 return kIoError;
167 }
168
169 if (zip64EocdLocator.locator_signature != Zip64EocdLocator::kSignature) {
170 ALOGW("Zip: %s: Zip64 eocd locator signature not found at offset %" PRId64, debugFileName,
171 locatorOffset);
172 return kInvalidFile;
173 }
174
175 const int64_t zip64EocdOffset = zip64EocdLocator.zip64_eocd_offset;
Tianjie173aba02020-03-28 18:28:43 -0700176 if (locatorOffset <= sizeof(Zip64EocdRecord) ||
177 zip64EocdOffset > locatorOffset - sizeof(Zip64EocdRecord)) {
178 ALOGW("Zip: %s: Bad zip64 eocd offset %" PRId64 ", eocd locator offset %" PRId64, debugFileName,
179 zip64EocdOffset, locatorOffset);
Tianjie6ab29122020-03-18 17:44:30 -0700180 return kInvalidOffset;
181 }
182
183 Zip64EocdRecord zip64EocdRecord{};
184 if (!archive->mapped_zip.ReadAtOffset(reinterpret_cast<uint8_t*>(&zip64EocdRecord),
185 sizeof(Zip64EocdRecord), zip64EocdOffset)) {
186 ALOGW("Zip: %s: read %zu from offset %" PRId64 " failed %s", debugFileName,
Tianjie173aba02020-03-28 18:28:43 -0700187 sizeof(Zip64EocdLocator), zip64EocdOffset, debugFileName);
Tianjie6ab29122020-03-18 17:44:30 -0700188 return kIoError;
189 }
190
191 if (zip64EocdRecord.record_signature != Zip64EocdRecord::kSignature) {
192 ALOGW("Zip: %s: Zip64 eocd record signature not found at offset %" PRId64, debugFileName,
193 zip64EocdOffset);
194 return kInvalidFile;
195 }
196
Tianjie173aba02020-03-28 18:28:43 -0700197 if (zip64EocdOffset <= zip64EocdRecord.cd_size ||
198 zip64EocdRecord.cd_start_offset > zip64EocdOffset - zip64EocdRecord.cd_size) {
Tianjie6ab29122020-03-18 17:44:30 -0700199 ALOGW("Zip: %s: Bad offset for zip64 central directory. cd offset %" PRIu64 ", cd size %" PRIu64
200 ", zip64 eocd offset %" PRIu64,
201 debugFileName, zip64EocdRecord.cd_start_offset, zip64EocdRecord.cd_size, zip64EocdOffset);
202 return kInvalidOffset;
203 }
204
205 *cdInfo = {.num_records = zip64EocdRecord.num_records,
206 .cd_size = zip64EocdRecord.cd_size,
207 .cd_start_offset = zip64EocdRecord.cd_start_offset};
208
209 return kSuccess;
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700210}
211
212static ZipError FindCentralDirectoryInfo(const char* debug_file_name, ZipArchive* archive,
213 off64_t file_length, uint32_t read_amount,
214 CentralDirectoryInfo* cdInfo) {
215 std::vector<uint8_t> scan_buffer(read_amount);
Narayan Kamath7462f022013-11-21 13:05:04 +0000216 const off64_t search_start = file_length - read_amount;
217
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700218 if (!archive->mapped_zip.ReadAtOffset(scan_buffer.data(), read_amount, search_start)) {
Jiyong Parkcd997e62017-06-30 17:23:33 +0900219 ALOGE("Zip: read %" PRId64 " from offset %" PRId64 " failed", static_cast<int64_t>(read_amount),
220 static_cast<int64_t>(search_start));
Narayan Kamath7462f022013-11-21 13:05:04 +0000221 return kIoError;
222 }
223
224 /*
225 * Scan backward for the EOCD magic. In an archive without a trailing
226 * comment, we'll find it on the first try. (We may want to consider
227 * doing an initial minimal read; if we don't find it, retry with a
228 * second read as above.)
229 */
Andreas Gampe964b95c2019-04-05 13:48:02 -0700230 CHECK_LE(read_amount, std::numeric_limits<int32_t>::max());
231 int32_t i = read_amount - sizeof(EocdRecord);
Narayan Kamath926973e2014-06-09 14:18:14 +0100232 for (; i >= 0; i--) {
Dan Albert1ae07642015-04-09 14:11:18 -0700233 if (scan_buffer[i] == 0x50) {
234 uint32_t* sig_addr = reinterpret_cast<uint32_t*>(&scan_buffer[i]);
Tianjie0ec0eaa2020-03-26 12:34:44 -0700235 if (android::base::get_unaligned<uint32_t>(sig_addr) == EocdRecord::kSignature) {
Dan Albert1ae07642015-04-09 14:11:18 -0700236 ALOGV("+++ Found EOCD at buf+%d", i);
237 break;
238 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000239 }
240 }
241 if (i < 0) {
242 ALOGD("Zip: EOCD not found, %s is not zip", debug_file_name);
243 return kInvalidFile;
244 }
245
246 const off64_t eocd_offset = search_start + i;
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700247 auto eocd = reinterpret_cast<const EocdRecord*>(scan_buffer.data() + i);
Narayan Kamath7462f022013-11-21 13:05:04 +0000248 /*
Narayan Kamath926973e2014-06-09 14:18:14 +0100249 * Verify that there's no trailing space at the end of the central directory
250 * and its comment.
Narayan Kamath7462f022013-11-21 13:05:04 +0000251 */
Jiyong Parkcd997e62017-06-30 17:23:33 +0900252 const off64_t calculated_length = eocd_offset + sizeof(EocdRecord) + eocd->comment_length;
Narayan Kamath926973e2014-06-09 14:18:14 +0100253 if (calculated_length != file_length) {
Narayan Kamath4f6b4992014-06-03 13:59:23 +0100254 ALOGW("Zip: %" PRId64 " extraneous bytes at the end of the central directory",
Narayan Kamath926973e2014-06-09 14:18:14 +0100255 static_cast<int64_t>(file_length - calculated_length));
Narayan Kamath4f6b4992014-06-03 13:59:23 +0100256 return kInvalidFile;
257 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000258
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700259 // One of the field is 0xFFFFFFFF, look for the zip64 EOCD instead.
260 if (eocd->cd_size == UINT32_MAX || eocd->cd_start_offset == UINT32_MAX) {
261 ALOGV("Looking for the zip64 EOCD, cd_size: %" PRIu32 "cd_start_offset: %" PRId32,
262 eocd->cd_size, eocd->cd_start_offset);
Tianjie6ab29122020-03-18 17:44:30 -0700263 return FindCentralDirectoryInfoForZip64(debug_file_name, archive, eocd_offset, cdInfo);
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700264 }
265
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 Xu1ee48922016-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 ")",
Jiyong Parkcd997e62017-06-30 17:23:33 +0900272 eocd->cd_start_offset, eocd->cd_size, static_cast<int64_t>(eocd_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000273 return kInvalidOffset;
274 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000275
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700276 *cdInfo = {.num_records = eocd->num_records,
277 .cd_size = eocd->cd_size,
278 .cd_start_offset = eocd->cd_start_offset};
279 return kSuccess;
Narayan Kamath7462f022013-11-21 13:05:04 +0000280}
281
282/*
283 * Find the zip Central Directory and memory-map it.
284 *
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700285 * On success, returns kSuccess after populating fields from the EOCD area:
Narayan Kamath7462f022013-11-21 13:05:04 +0000286 * directory_offset
Tianjie Xu18c25922016-09-29 15:27:41 -0700287 * directory_ptr
Narayan Kamath7462f022013-11-21 13:05:04 +0000288 * num_entries
289 */
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700290static ZipError MapCentralDirectory(const char* debug_file_name, ZipArchive* archive) {
291 // Test file length. We use lseek64 to make sure the file is small enough to be a zip file.
Tianjie Xu18c25922016-09-29 15:27:41 -0700292 off64_t file_length = archive->mapped_zip.GetFileLength();
Narayan Kamath7462f022013-11-21 13:05:04 +0000293 if (file_length == -1) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000294 return kInvalidFile;
295 }
296
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700297 if (file_length > kMaxFileLength) {
Narayan Kamath926973e2014-06-09 14:18:14 +0100298 ALOGV("Zip: zip file too long %" PRId64, static_cast<int64_t>(file_length));
Narayan Kamath7462f022013-11-21 13:05:04 +0000299 return kInvalidFile;
300 }
301
Narayan Kamath926973e2014-06-09 14:18:14 +0100302 if (file_length < static_cast<off64_t>(sizeof(EocdRecord))) {
303 ALOGV("Zip: length %" PRId64 " is too small to be zip", static_cast<int64_t>(file_length));
Narayan Kamath7462f022013-11-21 13:05:04 +0000304 return kInvalidFile;
305 }
306
307 /*
308 * Perform the traditional EOCD snipe hunt.
309 *
310 * We're searching for the End of Central Directory magic number,
311 * which appears at the start of the EOCD block. It's followed by
312 * 18 bytes of EOCD stuff and up to 64KB of archive comment. We
313 * need to read the last part of the file into a buffer, dig through
314 * it to find the magic number, parse some values out, and use those
315 * to determine the extent of the CD.
316 *
317 * We start by pulling in the last part of the file.
318 */
Andreas Gampe964b95c2019-04-05 13:48:02 -0700319 uint32_t read_amount = kMaxEOCDSearch;
Narayan Kamath926973e2014-06-09 14:18:14 +0100320 if (file_length < read_amount) {
Andreas Gampe964b95c2019-04-05 13:48:02 -0700321 read_amount = static_cast<uint32_t>(file_length);
Narayan Kamath7462f022013-11-21 13:05:04 +0000322 }
323
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700324 CentralDirectoryInfo cdInfo = {};
325 if (auto result =
326 FindCentralDirectoryInfo(debug_file_name, archive, file_length, read_amount, &cdInfo);
327 result != kSuccess) {
328 return result;
329 }
330
331 if (cdInfo.num_records == 0) {
332#if defined(__ANDROID__)
333 ALOGW("Zip: empty archive?");
334#endif
335 return kEmptyArchive;
336 }
337
338 if (cdInfo.cd_size >= SIZE_MAX) {
339 ALOGW("Zip: The size of central directory doesn't fit in range of size_t: %" PRIu64,
340 cdInfo.cd_size);
341 return kInvalidFile;
342 }
343
344 ALOGV("+++ num_entries=%" PRIu64 " dir_size=%" PRIu64 " dir_offset=%" PRIu64, cdInfo.num_records,
345 cdInfo.cd_size, cdInfo.cd_start_offset);
346
347 // It all looks good. Create a mapping for the CD, and set the fields in archive.
348 if (!archive->InitializeCentralDirectory(static_cast<off64_t>(cdInfo.cd_start_offset),
349 static_cast<size_t>(cdInfo.cd_size))) {
350 return kMmapFailed;
351 }
352
353 archive->num_entries = cdInfo.num_records;
354 archive->directory_offset = cdInfo.cd_start_offset;
355
356 return kSuccess;
Narayan Kamath7462f022013-11-21 13:05:04 +0000357}
358
Tianjie6ab29122020-03-18 17:44:30 -0700359static ZipError ParseZip64ExtendedInfoInExtraField(
360 const uint8_t* extraFieldStart, uint16_t extraFieldLength, uint32_t zip32UncompressedSize,
361 uint32_t zip32CompressedSize, std::optional<uint32_t> zip32LocalFileHeaderOffset,
362 Zip64ExtendedInfo* zip64Info) {
363 if (extraFieldLength <= 4) {
364 ALOGW("Zip: Extra field isn't large enough to hold zip64 info, size %" PRIu16,
365 extraFieldLength);
366 return kInvalidFile;
367 }
368
369 // Each header MUST consist of:
370 // Header ID - 2 bytes
371 // Data Size - 2 bytes
372 uint16_t offset = 0;
373 while (offset < extraFieldLength - 4) {
Tianjie0ec0eaa2020-03-26 12:34:44 -0700374 auto readPtr = const_cast<uint8_t*>(extraFieldStart + offset);
375 auto headerId = ConsumeUnaligned<uint16_t>(&readPtr);
376 auto dataSize = ConsumeUnaligned<uint16_t>(&readPtr);
Tianjie6ab29122020-03-18 17:44:30 -0700377
378 offset += 4;
379 if (dataSize > extraFieldLength - offset) {
380 ALOGW("Zip: Data size exceeds the boundary of extra field, data size %" PRIu16, dataSize);
381 return kInvalidOffset;
382 }
383
384 // Skip the other types of extensible data fields. Details in
385 // https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT section 4.5
386 if (headerId != Zip64ExtendedInfo::kHeaderId) {
387 offset += dataSize;
388 continue;
389 }
390
Tianjie0ec0eaa2020-03-26 12:34:44 -0700391 std::optional<uint64_t> uncompressedFileSize;
392 std::optional<uint64_t> compressedFileSize;
393 std::optional<uint64_t> localHeaderOffset;
394 if (zip32UncompressedSize == UINT32_MAX) {
Tianjied9bc8fd2020-04-13 16:29:22 -0700395 uncompressedFileSize =
396 TryConsumeUnaligned<uint64_t>(&readPtr, extraFieldStart, extraFieldLength);
397 if (!uncompressedFileSize.has_value()) return kInvalidOffset;
Tianjie0ec0eaa2020-03-26 12:34:44 -0700398 }
399 if (zip32CompressedSize == UINT32_MAX) {
Tianjied9bc8fd2020-04-13 16:29:22 -0700400 compressedFileSize =
401 TryConsumeUnaligned<uint64_t>(&readPtr, extraFieldStart, extraFieldLength);
402 if (!compressedFileSize.has_value()) return kInvalidOffset;
Tianjie6ab29122020-03-18 17:44:30 -0700403 }
404 if (zip32LocalFileHeaderOffset == UINT32_MAX) {
Tianjied9bc8fd2020-04-13 16:29:22 -0700405 localHeaderOffset =
406 TryConsumeUnaligned<uint64_t>(&readPtr, extraFieldStart, extraFieldLength);
407 if (!localHeaderOffset.has_value()) return kInvalidOffset;
Tianjie6ab29122020-03-18 17:44:30 -0700408 }
409
Tianjie0ec0eaa2020-03-26 12:34:44 -0700410 // calculate how many bytes we read after the data size field.
411 size_t bytesRead = readPtr - (extraFieldStart + offset);
412 if (bytesRead == 0) {
Tianjie6ab29122020-03-18 17:44:30 -0700413 ALOGW("Zip: Data size should not be 0 in zip64 extended field");
414 return kInvalidFile;
415 }
416
Tianjie0ec0eaa2020-03-26 12:34:44 -0700417 if (dataSize != bytesRead) {
Tianjie6ab29122020-03-18 17:44:30 -0700418 auto localOffsetString = zip32LocalFileHeaderOffset.has_value()
419 ? std::to_string(zip32LocalFileHeaderOffset.value())
420 : "missing";
Tianjie0ec0eaa2020-03-26 12:34:44 -0700421 ALOGW("Zip: Invalid data size in zip64 extended field, expect %zu , get %" PRIu16
Tianjie6ab29122020-03-18 17:44:30 -0700422 ", uncompressed size %" PRIu32 ", compressed size %" PRIu32 ", local header offset %s",
Tianjie0ec0eaa2020-03-26 12:34:44 -0700423 bytesRead, dataSize, zip32UncompressedSize, zip32CompressedSize,
Tianjie6ab29122020-03-18 17:44:30 -0700424 localOffsetString.c_str());
425 return kInvalidFile;
426 }
427
Tianjie6ab29122020-03-18 17:44:30 -0700428 zip64Info->uncompressed_file_size = uncompressedFileSize;
429 zip64Info->compressed_file_size = compressedFileSize;
430 zip64Info->local_header_offset = localHeaderOffset;
431 return kSuccess;
432 }
433
434 ALOGW("Zip: zip64 extended info isn't found in the extra field.");
435 return kInvalidFile;
436}
437
Narayan Kamath7462f022013-11-21 13:05:04 +0000438/*
439 * Parses the Zip archive's Central Directory. Allocates and populates the
440 * hash table.
441 *
442 * Returns 0 on success.
443 */
Tianjie6ab29122020-03-18 17:44:30 -0700444static ZipError ParseZipArchive(ZipArchive* archive) {
Tianjie Xu18c25922016-09-29 15:27:41 -0700445 const uint8_t* const cd_ptr = archive->central_directory.GetBasePtr();
446 const size_t cd_length = archive->central_directory.GetMapLength();
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700447 const uint64_t num_entries = archive->num_entries;
Narayan Kamath7462f022013-11-21 13:05:04 +0000448
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700449 if (num_entries <= UINT16_MAX) {
450 archive->cd_entry_map = CdEntryMapZip32::Create(static_cast<uint16_t>(num_entries));
Tianjie Xu0ef97832020-03-15 21:23:24 -0700451 } else {
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700452 archive->cd_entry_map = CdEntryMapZip64::Create();
Tianjie Xu0ef97832020-03-15 21:23:24 -0700453 }
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800454 if (archive->cd_entry_map == nullptr) {
Elliott Hughesfba2a1a2019-12-16 16:16:16 -0800455 return kAllocationFailed;
Tianjie Xu9e020e22016-10-10 12:11:30 -0700456 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000457
458 /*
459 * Walk through the central directory, adding entries to the hash
460 * table and verifying values.
461 */
Narayan Kamath926973e2014-06-09 14:18:14 +0100462 const uint8_t* const cd_end = cd_ptr + cd_length;
Narayan Kamath7462f022013-11-21 13:05:04 +0000463 const uint8_t* ptr = cd_ptr;
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700464 for (uint64_t i = 0; i < num_entries; i++) {
Tianjie Xu0fda1cf2017-04-05 14:46:27 -0700465 if (ptr > cd_end - sizeof(CentralDirectoryRecord)) {
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700466 ALOGW("Zip: ran off the end (item #%" PRIu64 ", %zu bytes of central directory)", i,
Elliott Hughesfba2a1a2019-12-16 16:16:16 -0800467 cd_length);
Tianjie Xu0fda1cf2017-04-05 14:46:27 -0700468#if defined(__ANDROID__)
469 android_errorWriteLog(0x534e4554, "36392138");
470#endif
Elliott Hughesfba2a1a2019-12-16 16:16:16 -0800471 return kInvalidFile;
Tianjie Xu0fda1cf2017-04-05 14:46:27 -0700472 }
473
Tianjie6ab29122020-03-18 17:44:30 -0700474 auto cdr = reinterpret_cast<const CentralDirectoryRecord*>(ptr);
Narayan Kamath926973e2014-06-09 14:18:14 +0100475 if (cdr->record_signature != CentralDirectoryRecord::kSignature) {
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700476 ALOGW("Zip: missed a central dir sig (at %" PRIu64 ")", i);
Elliott Hughesfba2a1a2019-12-16 16:16:16 -0800477 return kInvalidFile;
Narayan Kamath7462f022013-11-21 13:05:04 +0000478 }
479
Narayan Kamath926973e2014-06-09 14:18:14 +0100480 const uint16_t file_name_length = cdr->file_name_length;
481 const uint16_t extra_length = cdr->extra_field_length;
482 const uint16_t comment_length = cdr->comment_length;
Piotr Jastrzebski78271ba2014-08-15 12:53:00 +0100483 const uint8_t* file_name = ptr + sizeof(CentralDirectoryRecord);
484
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700485 if (file_name_length >= cd_length || file_name > cd_end - file_name_length) {
486 ALOGW("Zip: file name for entry %" PRIu64
Elliott Hughes50ef29a2019-06-18 18:23:59 -0700487 " exceeds the central directory range, file_name_length: %" PRIu16 ", cd_length: %zu",
488 i, file_name_length, cd_length);
Elliott Hughesfba2a1a2019-12-16 16:16:16 -0800489 return kInvalidEntryName;
Tianjie Xu9e020e22016-10-10 12:11:30 -0700490 }
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700491
492 const uint8_t* extra_field = file_name + file_name_length;
493 if (extra_length >= cd_length || extra_field > cd_end - extra_length) {
494 ALOGW("Zip: extra field for entry %" PRIu64
495 " exceeds the central directory range, file_name_length: %" PRIu16 ", cd_length: %zu",
496 i, extra_length, cd_length);
497 return kInvalidFile;
498 }
499
500 off64_t local_header_offset = cdr->local_file_header_offset;
501 if (local_header_offset == UINT32_MAX) {
Tianjie6ab29122020-03-18 17:44:30 -0700502 Zip64ExtendedInfo zip64_info{};
503 if (auto status = ParseZip64ExtendedInfoInExtraField(
504 extra_field, extra_length, cdr->uncompressed_size, cdr->compressed_size,
505 cdr->local_file_header_offset, &zip64_info);
506 status != kSuccess) {
507 return status;
508 }
509 CHECK(zip64_info.local_header_offset.has_value());
510 local_header_offset = zip64_info.local_header_offset.value();
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700511 }
512
513 if (local_header_offset >= archive->directory_offset) {
514 ALOGW("Zip: bad LFH offset %" PRId64 " at entry %" PRIu64,
515 static_cast<int64_t>(local_header_offset), i);
516 return kInvalidFile;
517 }
518
Elliott Hughes50ef29a2019-06-18 18:23:59 -0700519 // Check that file name is valid UTF-8 and doesn't contain NUL (U+0000) characters.
Narayan Kamath044bc8e2014-12-03 18:22:53 +0000520 if (!IsValidEntryName(file_name, file_name_length)) {
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700521 ALOGW("Zip: invalid file name at entry %" PRIu64, i);
Elliott Hughesfba2a1a2019-12-16 16:16:16 -0800522 return kInvalidEntryName;
Piotr Jastrzebski78271ba2014-08-15 12:53:00 +0100523 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000524
Elliott Hughes50ef29a2019-06-18 18:23:59 -0700525 // Add the CDE filename to the hash table.
526 std::string_view entry_name{reinterpret_cast<const char*>(file_name), file_name_length};
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800527 if (auto add_result =
528 archive->cd_entry_map->AddToMap(entry_name, archive->central_directory.GetBasePtr());
529 add_result != 0) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000530 ALOGW("Zip: Error adding entry to hash table %d", add_result);
Dmitriy Ivanov3ea93da2015-03-06 11:48:47 -0800531 return add_result;
Narayan Kamath7462f022013-11-21 13:05:04 +0000532 }
533
Narayan Kamath926973e2014-06-09 14:18:14 +0100534 ptr += sizeof(CentralDirectoryRecord) + file_name_length + extra_length + comment_length;
535 if ((ptr - cd_ptr) > static_cast<int64_t>(cd_length)) {
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700536 ALOGW("Zip: bad CD advance (%tu vs %zu) at entry %" PRIu64, ptr - cd_ptr, cd_length, i);
Elliott Hughesfba2a1a2019-12-16 16:16:16 -0800537 return kInvalidFile;
Narayan Kamath7462f022013-11-21 13:05:04 +0000538 }
539 }
Narayan Kamathc1a56dc2017-08-09 18:32:09 +0100540
541 uint32_t lfh_start_bytes;
542 if (!archive->mapped_zip.ReadAtOffset(reinterpret_cast<uint8_t*>(&lfh_start_bytes),
543 sizeof(uint32_t), 0)) {
544 ALOGW("Zip: Unable to read header for entry at offset == 0.");
Elliott Hughesfba2a1a2019-12-16 16:16:16 -0800545 return kInvalidFile;
Narayan Kamathc1a56dc2017-08-09 18:32:09 +0100546 }
547
548 if (lfh_start_bytes != LocalFileHeader::kSignature) {
549 ALOGW("Zip: Entry at offset zero has invalid LFH signature %" PRIx32, lfh_start_bytes);
550#if defined(__ANDROID__)
551 android_errorWriteLog(0x534e4554, "64211847");
552#endif
Elliott Hughesfba2a1a2019-12-16 16:16:16 -0800553 return kInvalidFile;
Narayan Kamathc1a56dc2017-08-09 18:32:09 +0100554 }
555
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700556 ALOGV("+++ zip good scan %" PRIu64 " entries", num_entries);
Narayan Kamath7462f022013-11-21 13:05:04 +0000557
Tianjie6ab29122020-03-18 17:44:30 -0700558 return kSuccess;
Narayan Kamath7462f022013-11-21 13:05:04 +0000559}
560
Jiyong Parkcd997e62017-06-30 17:23:33 +0900561static int32_t OpenArchiveInternal(ZipArchive* archive, const char* debug_file_name) {
Elliott Hughesfba2a1a2019-12-16 16:16:16 -0800562 int32_t result = MapCentralDirectory(debug_file_name, archive);
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700563 return result != kSuccess ? result : ParseZipArchive(archive);
Narayan Kamath7462f022013-11-21 13:05:04 +0000564}
565
Jiyong Parkcd997e62017-06-30 17:23:33 +0900566int32_t OpenArchiveFd(int fd, const char* debug_file_name, ZipArchiveHandle* handle,
567 bool assume_ownership) {
Ryan Mitchell23150e42020-03-09 09:33:46 -0700568 ZipArchive* archive = new ZipArchive(MappedZipFile(fd), assume_ownership);
Narayan Kamath7462f022013-11-21 13:05:04 +0000569 *handle = archive;
Narayan Kamath7462f022013-11-21 13:05:04 +0000570 return OpenArchiveInternal(archive, debug_file_name);
571}
572
Ryan Mitchell23150e42020-03-09 09:33:46 -0700573int32_t OpenArchiveFdRange(int fd, const char* debug_file_name, ZipArchiveHandle* handle,
574 off64_t length, off64_t offset, bool assume_ownership) {
575 ZipArchive* archive = new ZipArchive(MappedZipFile(fd, length, offset), assume_ownership);
576 *handle = archive;
577
578 if (length < 0) {
579 ALOGW("Invalid zip length %" PRId64, length);
580 return kIoError;
581 }
582
583 if (offset < 0) {
584 ALOGW("Invalid zip offset %" PRId64, offset);
585 return kIoError;
586 }
587
588 return OpenArchiveInternal(archive, debug_file_name);
589}
590
Narayan Kamath7462f022013-11-21 13:05:04 +0000591int32_t OpenArchive(const char* fileName, ZipArchiveHandle* handle) {
Nick Kralevich3bdf7442018-12-18 12:48:06 -0800592 const int fd = ::android::base::utf8::open(fileName, O_RDONLY | O_BINARY | O_CLOEXEC, 0);
Ryan Mitchell23150e42020-03-09 09:33:46 -0700593 ZipArchive* archive = new ZipArchive(MappedZipFile(fd), true);
Narayan Kamath7462f022013-11-21 13:05:04 +0000594 *handle = archive;
595
Narayan Kamath7462f022013-11-21 13:05:04 +0000596 if (fd < 0) {
597 ALOGW("Unable to open '%s': %s", fileName, strerror(errno));
598 return kIoError;
Narayan Kamath7462f022013-11-21 13:05:04 +0000599 }
Dmitriy Ivanov40b52b22014-07-15 19:33:00 -0700600
Narayan Kamath7462f022013-11-21 13:05:04 +0000601 return OpenArchiveInternal(archive, fileName);
602}
603
Elliott Hughesf66460b2019-10-22 11:44:50 -0700604int32_t OpenArchiveFromMemory(const void* address, size_t length, const char* debug_file_name,
Jiyong Parkcd997e62017-06-30 17:23:33 +0900605 ZipArchiveHandle* handle) {
Tianjie Xu18c25922016-09-29 15:27:41 -0700606 ZipArchive* archive = new ZipArchive(address, length);
607 *handle = archive;
608 return OpenArchiveInternal(archive, debug_file_name);
609}
610
Elliott Hughes26724132019-10-25 09:57:58 -0700611ZipArchiveInfo GetArchiveInfo(ZipArchiveHandle archive) {
612 ZipArchiveInfo result;
613 result.archive_size = archive->mapped_zip.GetFileLength();
614 result.entry_count = archive->num_entries;
615 return result;
616}
617
Narayan Kamath7462f022013-11-21 13:05:04 +0000618/*
619 * Close a ZipArchive, closing the file and freeing the contents.
620 */
Ryan Prichard3673f992018-10-10 22:41:14 -0700621void CloseArchive(ZipArchiveHandle archive) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000622 ALOGV("Closing archive %p", archive);
Neil Fullerb1a113f2014-07-25 14:43:04 +0100623 delete archive;
Narayan Kamath7462f022013-11-21 13:05:04 +0000624}
625
Tianjie85c5d232020-04-01 23:08:34 -0700626static int32_t ValidateDataDescriptor(MappedZipFile& mapped_zip, const ZipEntry64* entry) {
Tianjie0ec0eaa2020-03-26 12:34:44 -0700627 // Maximum possible size for data descriptor: 2 * 4 + 2 * 8 = 24 bytes
Tianjied9bc8fd2020-04-13 16:29:22 -0700628 // The zip format doesn't specify the size of data descriptor. But we won't read OOB here even
629 // if the descriptor isn't present. Because the size cd + eocd in the end of the zipfile is
630 // larger than 24 bytes. And if the descriptor contains invalid data, we'll abort due to
631 // kInconsistentInformation.
Tianjie0ec0eaa2020-03-26 12:34:44 -0700632 uint8_t ddBuf[24];
Adam Lesinskide117e42017-06-19 10:27:38 -0700633 off64_t offset = entry->offset;
634 if (entry->method != kCompressStored) {
635 offset += entry->compressed_length;
636 } else {
637 offset += entry->uncompressed_length;
638 }
639
640 if (!mapped_zip.ReadAtOffset(ddBuf, sizeof(ddBuf), offset)) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000641 return kIoError;
642 }
643
Narayan Kamath926973e2014-06-09 14:18:14 +0100644 const uint32_t ddSignature = *(reinterpret_cast<const uint32_t*>(ddBuf));
Tianjie0ec0eaa2020-03-26 12:34:44 -0700645 uint8_t* ddReadPtr = (ddSignature == DataDescriptor::kOptSignature) ? ddBuf + 4 : ddBuf;
646 DataDescriptor descriptor{};
647 descriptor.crc32 = ConsumeUnaligned<uint32_t>(&ddReadPtr);
648 if (entry->zip64_format_size) {
649 descriptor.compressed_size = ConsumeUnaligned<uint64_t>(&ddReadPtr);
650 descriptor.uncompressed_size = ConsumeUnaligned<uint64_t>(&ddReadPtr);
651 } else {
652 descriptor.compressed_size = ConsumeUnaligned<uint32_t>(&ddReadPtr);
653 descriptor.uncompressed_size = ConsumeUnaligned<uint32_t>(&ddReadPtr);
654 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000655
Narayan Kamath162b7052017-06-05 13:21:12 +0100656 // Validate that the values in the data descriptor match those in the central
657 // directory.
Tianjie0ec0eaa2020-03-26 12:34:44 -0700658 if (entry->compressed_length != descriptor.compressed_size ||
659 entry->uncompressed_length != descriptor.uncompressed_size ||
660 entry->crc32 != descriptor.crc32) {
Tianjie85c5d232020-04-01 23:08:34 -0700661 ALOGW("Zip: size/crc32 mismatch. expected {%" PRIu64 ", %" PRIu64 ", %" PRIx32
Tianjie0ec0eaa2020-03-26 12:34:44 -0700662 "}, was {%" PRIu64 ", %" PRIu64 ", %" PRIx32 "}",
Narayan Kamath162b7052017-06-05 13:21:12 +0100663 entry->compressed_length, entry->uncompressed_length, entry->crc32,
Tianjie0ec0eaa2020-03-26 12:34:44 -0700664 descriptor.compressed_size, descriptor.uncompressed_size, descriptor.crc32);
Narayan Kamath162b7052017-06-05 13:21:12 +0100665 return kInconsistentInformation;
666 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000667
668 return 0;
669}
670
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800671static int32_t FindEntry(const ZipArchive* archive, std::string_view entryName,
Tianjie85c5d232020-04-01 23:08:34 -0700672 const uint64_t nameOffset, ZipEntry64* data) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000673 // Recover the start of the central directory entry from the filename
674 // pointer. The filename is the first entry past the fixed-size data,
675 // so we can just subtract back from that.
Elliott Hughes50ef29a2019-06-18 18:23:59 -0700676 const uint8_t* base_ptr = archive->central_directory.GetBasePtr();
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800677 const uint8_t* ptr = base_ptr + nameOffset;
Narayan Kamath926973e2014-06-09 14:18:14 +0100678 ptr -= sizeof(CentralDirectoryRecord);
Narayan Kamath7462f022013-11-21 13:05:04 +0000679
680 // This is the base of our mmapped region, we have to sanity check that
681 // the name that's in the hash table is a pointer to a location within
682 // this mapped region.
Tianjie Xu18c25922016-09-29 15:27:41 -0700683 if (ptr < base_ptr || ptr > base_ptr + archive->central_directory.GetMapLength()) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000684 ALOGW("Zip: Invalid entry pointer");
685 return kInvalidOffset;
686 }
687
Tianjie6ab29122020-03-18 17:44:30 -0700688 auto cdr = reinterpret_cast<const CentralDirectoryRecord*>(ptr);
Narayan Kamath926973e2014-06-09 14:18:14 +0100689
Narayan Kamath7462f022013-11-21 13:05:04 +0000690 // The offset of the start of the central directory in the zipfile.
691 // We keep this lying around so that we can sanity check all our lengths
692 // and our per-file structures.
693 const off64_t cd_offset = archive->directory_offset;
694
695 // Fill out the compression method, modification time, crc32
696 // and other interesting attributes from the central directory. These
697 // will later be compared against values from the local file header.
Narayan Kamath926973e2014-06-09 14:18:14 +0100698 data->method = cdr->compression_method;
beonit0e99a2f2015-07-18 02:08:16 +0900699 data->mod_time = cdr->last_mod_date << 16 | cdr->last_mod_time;
Narayan Kamath926973e2014-06-09 14:18:14 +0100700 data->crc32 = cdr->crc32;
701 data->compressed_length = cdr->compressed_size;
702 data->uncompressed_length = cdr->uncompressed_size;
Narayan Kamath7462f022013-11-21 13:05:04 +0000703
704 // Figure out the local header offset from the central directory. The
705 // actual file data will begin after the local header and the name /
706 // extra comments.
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700707 off64_t local_header_offset = cdr->local_file_header_offset;
708 // One of the info field is UINT32_MAX, try to parse the real value in the zip64 extended info in
709 // the extra field.
710 if (cdr->uncompressed_size == UINT32_MAX || cdr->compressed_size == UINT32_MAX ||
711 cdr->local_file_header_offset == UINT32_MAX) {
Tianjie6ab29122020-03-18 17:44:30 -0700712 const uint8_t* extra_field = ptr + sizeof(CentralDirectoryRecord) + cdr->file_name_length;
713 Zip64ExtendedInfo zip64_info{};
714 if (auto status = ParseZip64ExtendedInfoInExtraField(
715 extra_field, cdr->extra_field_length, cdr->uncompressed_size, cdr->compressed_size,
716 cdr->local_file_header_offset, &zip64_info);
717 status != kSuccess) {
718 return status;
719 }
720
Tianjie85c5d232020-04-01 23:08:34 -0700721 data->uncompressed_length = zip64_info.uncompressed_file_size.value_or(cdr->uncompressed_size);
722 data->compressed_length = zip64_info.compressed_file_size.value_or(cdr->compressed_size);
Tianjie0ec0eaa2020-03-26 12:34:44 -0700723 local_header_offset = zip64_info.local_header_offset.value_or(local_header_offset);
724 data->zip64_format_size =
725 cdr->uncompressed_size == UINT32_MAX || cdr->compressed_size == UINT32_MAX;
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700726 }
727
Narayan Kamath926973e2014-06-09 14:18:14 +0100728 if (local_header_offset + static_cast<off64_t>(sizeof(LocalFileHeader)) >= cd_offset) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000729 ALOGW("Zip: bad local hdr offset in zip");
730 return kInvalidOffset;
731 }
732
Narayan Kamath926973e2014-06-09 14:18:14 +0100733 uint8_t lfh_buf[sizeof(LocalFileHeader)];
Tianjie Xu18c25922016-09-29 15:27:41 -0700734 if (!archive->mapped_zip.ReadAtOffset(lfh_buf, sizeof(lfh_buf), local_header_offset)) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800735 ALOGW("Zip: failed reading lfh name from offset %" PRId64,
Jiyong Parkcd997e62017-06-30 17:23:33 +0900736 static_cast<int64_t>(local_header_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000737 return kIoError;
738 }
739
Tianjie6ab29122020-03-18 17:44:30 -0700740 auto lfh = reinterpret_cast<const LocalFileHeader*>(lfh_buf);
Narayan Kamath926973e2014-06-09 14:18:14 +0100741 if (lfh->lfh_signature != LocalFileHeader::kSignature) {
Mark Salyzyn99ef9912014-03-14 14:26:22 -0700742 ALOGW("Zip: didn't find signature at start of lfh, offset=%" PRId64,
Jiyong Parkcd997e62017-06-30 17:23:33 +0900743 static_cast<int64_t>(local_header_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000744 return kInvalidOffset;
745 }
746
Tianjie6ab29122020-03-18 17:44:30 -0700747 // Check that the local file header name matches the declared name in the central directory.
748 CHECK_LE(entryName.size(), UINT16_MAX);
749 auto nameLen = static_cast<uint16_t>(entryName.size());
750 if (lfh->file_name_length != nameLen) {
751 ALOGW("Zip: lfh name length did not match central directory for %s: %" PRIu16 " %" PRIu16,
752 std::string(entryName).c_str(), lfh->file_name_length, nameLen);
753 return kInconsistentInformation;
754 }
755 const off64_t name_offset = local_header_offset + sizeof(LocalFileHeader);
756 if (name_offset > cd_offset - lfh->file_name_length) {
757 ALOGW("Zip: lfh name has invalid declared length");
758 return kInvalidOffset;
759 }
760
761 std::vector<uint8_t> name_buf(nameLen);
762 if (!archive->mapped_zip.ReadAtOffset(name_buf.data(), nameLen, name_offset)) {
763 ALOGW("Zip: failed reading lfh name from offset %" PRId64, static_cast<int64_t>(name_offset));
764 return kIoError;
765 }
766 if (memcmp(entryName.data(), name_buf.data(), nameLen) != 0) {
767 ALOGW("Zip: lfh name did not match central directory");
768 return kInconsistentInformation;
769 }
770
771 uint64_t lfh_uncompressed_size = lfh->uncompressed_size;
772 uint64_t lfh_compressed_size = lfh->compressed_size;
773 if (lfh_uncompressed_size == UINT32_MAX || lfh_compressed_size == UINT32_MAX) {
Tianjie0ec0eaa2020-03-26 12:34:44 -0700774 if (lfh_uncompressed_size != UINT32_MAX || lfh_compressed_size != UINT32_MAX) {
775 ALOGW(
776 "Zip: The zip64 extended field in the local header MUST include BOTH original and "
777 "compressed file size fields.");
778 return kInvalidFile;
779 }
780
Tianjie6ab29122020-03-18 17:44:30 -0700781 const off64_t lfh_extra_field_offset = name_offset + lfh->file_name_length;
782 const uint16_t lfh_extra_field_size = lfh->extra_field_length;
783 if (lfh_extra_field_offset > cd_offset - lfh_extra_field_size) {
784 ALOGW("Zip: extra field has a bad size for entry %s", std::string(entryName).c_str());
785 return kInvalidOffset;
786 }
787
788 std::vector<uint8_t> local_extra_field(lfh_extra_field_size);
789 if (!archive->mapped_zip.ReadAtOffset(local_extra_field.data(), lfh_extra_field_size,
790 lfh_extra_field_offset)) {
791 ALOGW("Zip: failed reading lfh extra field from offset %" PRId64, lfh_extra_field_offset);
792 return kIoError;
793 }
794
795 Zip64ExtendedInfo zip64_info{};
796 if (auto status = ParseZip64ExtendedInfoInExtraField(
797 local_extra_field.data(), lfh_extra_field_size, lfh->uncompressed_size,
798 lfh->compressed_size, std::nullopt, &zip64_info);
799 status != kSuccess) {
800 return status;
801 }
802
803 CHECK(zip64_info.uncompressed_file_size.has_value());
804 CHECK(zip64_info.compressed_file_size.has_value());
805 lfh_uncompressed_size = zip64_info.uncompressed_file_size.value();
806 lfh_compressed_size = zip64_info.compressed_file_size.value();
807 }
808
Narayan Kamath7462f022013-11-21 13:05:04 +0000809 // Paranoia: Match the values specified in the local file header
810 // to those specified in the central directory.
Adam Lesinskid987c9d2017-04-06 18:55:47 -0700811
Narayan Kamath162b7052017-06-05 13:21:12 +0100812 // Warn if central directory and local file header don't agree on the use
813 // of a trailing Data Descriptor. The reference implementation is inconsistent
814 // and appears to use the LFH value during extraction (unzip) but the CD value
815 // while displayng information about archives (zipinfo). The spec remains
816 // silent on this inconsistency as well.
817 //
818 // For now, always use the version from the LFH but make sure that the values
819 // specified in the central directory match those in the data descriptor.
820 //
821 // NOTE: It's also worth noting that unzip *does* warn about inconsistencies in
822 // bit 11 (EFS: The language encoding flag, marking that filename and comment are
823 // encoded using UTF-8). This implementation does not check for the presence of
824 // that flag and always enforces that entry names are valid UTF-8.
825 if ((lfh->gpb_flags & kGPBDDFlagMask) != (cdr->gpb_flags & kGPBDDFlagMask)) {
826 ALOGW("Zip: gpb flag mismatch at bit 3. expected {%04" PRIx16 "}, was {%04" PRIx16 "}",
Adam Lesinskid987c9d2017-04-06 18:55:47 -0700827 cdr->gpb_flags, lfh->gpb_flags);
Adam Lesinskid987c9d2017-04-06 18:55:47 -0700828 }
829
830 // If there is no trailing data descriptor, verify that the central directory and local file
831 // header agree on the crc, compressed, and uncompressed sizes of the entry.
Narayan Kamath926973e2014-06-09 14:18:14 +0100832 if ((lfh->gpb_flags & kGPBDDFlagMask) == 0) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000833 data->has_data_descriptor = 0;
Tianjie6ab29122020-03-18 17:44:30 -0700834 if (data->compressed_length != lfh_compressed_size ||
835 data->uncompressed_length != lfh_uncompressed_size || data->crc32 != lfh->crc32) {
Tianjie85c5d232020-04-01 23:08:34 -0700836 ALOGW("Zip: size/crc32 mismatch. expected {%" PRIu64 ", %" PRIu64 ", %" PRIx32
Tianjie6ab29122020-03-18 17:44:30 -0700837 "}, was {%" PRIu64 ", %" PRIu64 ", %" PRIx32 "}",
838 data->compressed_length, data->uncompressed_length, data->crc32, lfh_compressed_size,
839 lfh_uncompressed_size, lfh->crc32);
Narayan Kamath7462f022013-11-21 13:05:04 +0000840 return kInconsistentInformation;
841 }
842 } else {
843 data->has_data_descriptor = 1;
844 }
845
Elliott Hughes55fd2932017-05-28 22:59:04 -0700846 // 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 -0700847 data->version_made_by = cdr->version_made_by;
Elliott Hughesd5095252019-10-28 21:35:52 -0700848 data->external_file_attributes = cdr->external_file_attributes;
Elliott Hughes26724132019-10-25 09:57:58 -0700849 if ((data->version_made_by >> 8) == 3) {
Elliott Hughes55fd2932017-05-28 22:59:04 -0700850 data->unix_mode = (cdr->external_file_attributes >> 16) & 0xffff;
851 } else {
852 data->unix_mode = 0777;
853 }
854
Elliott Hughesd5095252019-10-28 21:35:52 -0700855 // 4.4.4: general purpose bit flags.
856 data->gpbf = lfh->gpb_flags;
857
Elliott Hughes26724132019-10-25 09:57:58 -0700858 // 4.4.14: the lowest bit of the internal file attributes field indicates text.
859 // Currently only needed to implement zipinfo.
860 data->is_text = (cdr->internal_file_attributes & 1);
861
Jiyong Parkcd997e62017-06-30 17:23:33 +0900862 const off64_t data_offset = local_header_offset + sizeof(LocalFileHeader) +
863 lfh->file_name_length + lfh->extra_field_length;
Narayan Kamath48953a12014-01-24 12:32:39 +0000864 if (data_offset > cd_offset) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800865 ALOGW("Zip: bad data offset %" PRId64 " in zip", static_cast<int64_t>(data_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000866 return kInvalidOffset;
867 }
868
Tianjie85c5d232020-04-01 23:08:34 -0700869 if (data->compressed_length > cd_offset - data_offset) {
870 ALOGW("Zip: bad compressed length in zip (%" PRId64 " + %" PRIu64 " > %" PRId64 ")",
Jiyong Parkcd997e62017-06-30 17:23:33 +0900871 static_cast<int64_t>(data_offset), data->compressed_length,
872 static_cast<int64_t>(cd_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000873 return kInvalidOffset;
874 }
875
Tianjie85c5d232020-04-01 23:08:34 -0700876 if (data->method == kCompressStored && data->uncompressed_length > cd_offset - data_offset) {
877 ALOGW("Zip: bad uncompressed length in zip (%" PRId64 " + %" PRIu64 " > %" PRId64 ")",
Jiyong Parkcd997e62017-06-30 17:23:33 +0900878 static_cast<int64_t>(data_offset), data->uncompressed_length,
879 static_cast<int64_t>(cd_offset));
880 return kInvalidOffset;
Narayan Kamath7462f022013-11-21 13:05:04 +0000881 }
882
883 data->offset = data_offset;
884 return 0;
885}
886
887struct IterationHandle {
Narayan Kamath7462f022013-11-21 13:05:04 +0000888 ZipArchive* archive;
Piotr Jastrzebski8e085362014-08-18 11:37:45 +0100889
Songchun Fanc33f5262020-03-24 09:15:51 -0700890 std::function<bool(std::string_view)> matcher;
Elliott Hughesa22ac0f2019-05-08 10:44:06 -0700891
892 uint32_t position = 0;
893
Songchun Fanc33f5262020-03-24 09:15:51 -0700894 IterationHandle(ZipArchive* archive, std::function<bool(std::string_view)> in_matcher)
895 : archive(archive), matcher(std::move(in_matcher)) {}
896
897 bool Match(std::string_view entry_name) const { return matcher(entry_name); }
Narayan Kamath7462f022013-11-21 13:05:04 +0000898};
899
Ryan Prichard3673f992018-10-10 22:41:14 -0700900int32_t StartIteration(ZipArchiveHandle archive, void** cookie_ptr,
Elliott Hughesa22ac0f2019-05-08 10:44:06 -0700901 const std::string_view optional_prefix,
902 const std::string_view optional_suffix) {
Elliott Hughesa22ac0f2019-05-08 10:44:06 -0700903 if (optional_prefix.size() > static_cast<size_t>(UINT16_MAX) ||
904 optional_suffix.size() > static_cast<size_t>(UINT16_MAX)) {
905 ALOGW("Zip: prefix/suffix too long");
906 return kInvalidEntryName;
907 }
Songchun Fanc33f5262020-03-24 09:15:51 -0700908 auto matcher = [prefix = std::string(optional_prefix),
909 suffix = std::string(optional_suffix)](std::string_view name) mutable {
910 return android::base::StartsWith(name, prefix) && android::base::EndsWith(name, suffix);
911 };
912 return StartIteration(archive, cookie_ptr, std::move(matcher));
913}
914
915int32_t StartIteration(ZipArchiveHandle archive, void** cookie_ptr,
916 std::function<bool(std::string_view)> matcher) {
917 if (archive == nullptr || archive->cd_entry_map == nullptr) {
918 ALOGW("Zip: Invalid ZipArchiveHandle");
919 return kInvalidHandle;
920 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000921
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800922 archive->cd_entry_map->ResetIteration();
Songchun Fanc33f5262020-03-24 09:15:51 -0700923 *cookie_ptr = new IterationHandle(archive, matcher);
Narayan Kamath7462f022013-11-21 13:05:04 +0000924 return 0;
925}
926
Piotr Jastrzebski79c8b342014-08-08 14:02:17 +0100927void EndIteration(void* cookie) {
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100928 delete reinterpret_cast<IterationHandle*>(cookie);
Piotr Jastrzebski79c8b342014-08-08 14:02:17 +0100929}
930
Tianjie85c5d232020-04-01 23:08:34 -0700931int32_t ZipEntry::CopyFromZipEntry64(ZipEntry* dst, const ZipEntry64* src) {
932 if (src->compressed_length > UINT32_MAX || src->uncompressed_length > UINT32_MAX) {
933 ALOGW(
934 "Zip: the entry size is too large to fit into the 32 bits ZipEntry, uncompressed "
935 "length %" PRIu64 ", compressed length %" PRIu64,
936 src->uncompressed_length, src->compressed_length);
937 return kUnsupportedEntrySize;
938 }
939
940 *dst = *src;
941 dst->uncompressed_length = static_cast<uint32_t>(src->uncompressed_length);
942 dst->compressed_length = static_cast<uint32_t>(src->compressed_length);
943 return kSuccess;
944}
945
Elliott Hughesb17bf522019-05-03 22:38:44 -0700946int32_t FindEntry(const ZipArchiveHandle archive, const std::string_view entryName,
947 ZipEntry* data) {
Tianjie85c5d232020-04-01 23:08:34 -0700948 ZipEntry64 entry64;
949 if (auto status = FindEntry(archive, entryName, &entry64); status != kSuccess) {
950 return status;
951 }
952
953 return ZipEntry::CopyFromZipEntry64(data, &entry64);
954}
955
956int32_t FindEntry(const ZipArchiveHandle archive, const std::string_view entryName,
957 ZipEntry64* data) {
Elliott Hughesb17bf522019-05-03 22:38:44 -0700958 if (entryName.empty() || entryName.size() > static_cast<size_t>(UINT16_MAX)) {
959 ALOGW("Zip: Invalid filename of length %zu", entryName.size());
960 return kInvalidEntryName;
961 }
962
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800963 const auto [result, offset] =
964 archive->cd_entry_map->GetCdEntryOffset(entryName, archive->central_directory.GetBasePtr());
965 if (result != 0) {
Elliott Hughesb17bf522019-05-03 22:38:44 -0700966 ALOGV("Zip: Could not find entry %.*s", static_cast<int>(entryName.size()), entryName.data());
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800967 return static_cast<int32_t>(result); // kEntryNotFound is safe to truncate.
Elliott Hughesb17bf522019-05-03 22:38:44 -0700968 }
Elliott Hughesa5ff19e2019-05-07 09:27:59 -0700969 // We know there are at most hash_table_size entries, safe to truncate.
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800970 return FindEntry(archive, entryName, offset, data);
Elliott Hughesb17bf522019-05-03 22:38:44 -0700971}
972
Elliott Hughese06a8082019-05-22 18:56:41 -0700973int32_t Next(void* cookie, ZipEntry* data, std::string* name) {
Tianjie85c5d232020-04-01 23:08:34 -0700974 ZipEntry64 entry64;
975 if (auto status = Next(cookie, &entry64, name); status != kSuccess) {
976 return status;
977 }
978
979 return ZipEntry::CopyFromZipEntry64(data, &entry64);
980}
981
982int32_t Next(void* cookie, ZipEntry* data, std::string_view* name) {
983 ZipEntry64 entry64;
984 if (auto status = Next(cookie, &entry64, name); status != kSuccess) {
985 return status;
986 }
987
988 return ZipEntry::CopyFromZipEntry64(data, &entry64);
989}
990
991int32_t Next(void* cookie, ZipEntry64* data, std::string* name) {
Elliott Hughes1e40c302019-06-12 12:12:47 -0700992 std::string_view sv;
993 int32_t result = Next(cookie, data, &sv);
994 if (result == 0 && name) {
995 *name = std::string(sv);
996 }
997 return result;
998}
999
Tianjie85c5d232020-04-01 23:08:34 -07001000int32_t Next(void* cookie, ZipEntry64* data, std::string_view* name) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -08001001 IterationHandle* handle = reinterpret_cast<IterationHandle*>(cookie);
Tianjie Xu28f8eae2020-03-05 16:31:23 -08001002 if (handle == nullptr) {
Zimuzo5a503ef2018-09-17 19:49:55 +01001003 ALOGW("Zip: Null ZipArchiveHandle");
Narayan Kamath7462f022013-11-21 13:05:04 +00001004 return kInvalidHandle;
1005 }
1006
1007 ZipArchive* archive = handle->archive;
Tianjie Xu28f8eae2020-03-05 16:31:23 -08001008 if (archive == nullptr || archive->cd_entry_map == nullptr) {
Narayan Kamath7462f022013-11-21 13:05:04 +00001009 ALOGW("Zip: Invalid ZipArchiveHandle");
1010 return kInvalidHandle;
1011 }
1012
Tianjie Xu28f8eae2020-03-05 16:31:23 -08001013 auto entry = archive->cd_entry_map->Next(archive->central_directory.GetBasePtr());
1014 while (entry != std::pair<std::string_view, uint64_t>()) {
1015 const auto [entry_name, offset] = entry;
Songchun Fanc33f5262020-03-24 09:15:51 -07001016 if (handle->Match(entry_name)) {
Tianjie Xu28f8eae2020-03-05 16:31:23 -08001017 const int error = FindEntry(archive, entry_name, offset, data);
Elliott Hughes50ef29a2019-06-18 18:23:59 -07001018 if (!error && name) {
1019 *name = entry_name;
Narayan Kamath7462f022013-11-21 13:05:04 +00001020 }
Narayan Kamath7462f022013-11-21 13:05:04 +00001021 return error;
1022 }
Tianjie Xu28f8eae2020-03-05 16:31:23 -08001023 entry = archive->cd_entry_map->Next(archive->central_directory.GetBasePtr());
Narayan Kamath7462f022013-11-21 13:05:04 +00001024 }
1025
Tianjie Xu28f8eae2020-03-05 16:31:23 -08001026 archive->cd_entry_map->ResetIteration();
Narayan Kamath7462f022013-11-21 13:05:04 +00001027 return kIterationEnd;
1028}
1029
Narayan Kamathf899bd52015-04-17 11:53:14 +01001030// A Writer that writes data to a fixed size memory region.
1031// The size of the memory region must be equal to the total size of
1032// the data appended to it.
Narayan Kamath485b3642017-10-26 14:42:39 +01001033class MemoryWriter : public zip_archive::Writer {
Narayan Kamathf899bd52015-04-17 11:53:14 +01001034 public:
Tianjie088c4032020-04-07 00:12:54 -07001035 static std::unique_ptr<MemoryWriter> Create(uint8_t* buf, size_t size, const ZipEntry64* entry) {
Tianjie85c5d232020-04-01 23:08:34 -07001036 const uint64_t declared_length = entry->uncompressed_length;
1037 if (declared_length > size) {
1038 ALOGW("Zip: file size %" PRIu64 " is larger than the buffer size %zu.", declared_length,
1039 size);
Tianjie088c4032020-04-07 00:12:54 -07001040 return nullptr;
Tianjie85c5d232020-04-01 23:08:34 -07001041 }
1042
Tianjie088c4032020-04-07 00:12:54 -07001043 return std::unique_ptr<MemoryWriter>(new MemoryWriter(buf, size));
Tianjie85c5d232020-04-01 23:08:34 -07001044 }
1045
Narayan Kamathf899bd52015-04-17 11:53:14 +01001046 virtual bool Append(uint8_t* buf, size_t buf_size) override {
Tianjie85c5d232020-04-01 23:08:34 -07001047 if (size_ < buf_size || bytes_written_ > size_ - buf_size) {
Elliott Hughese8f4b142018-10-19 16:09:39 -07001048 ALOGW("Zip: Unexpected size %zu (declared) vs %zu (actual)", size_,
Jiyong Parkcd997e62017-06-30 17:23:33 +09001049 bytes_written_ + buf_size);
Narayan Kamathf899bd52015-04-17 11:53:14 +01001050 return false;
1051 }
1052
1053 memcpy(buf_ + bytes_written_, buf, buf_size);
1054 bytes_written_ += buf_size;
1055 return true;
1056 }
1057
1058 private:
Tianjie85c5d232020-04-01 23:08:34 -07001059 MemoryWriter(uint8_t* buf, size_t size) : Writer(), buf_(buf), size_(size), bytes_written_(0) {}
1060
1061 uint8_t* const buf_{nullptr};
Narayan Kamathf899bd52015-04-17 11:53:14 +01001062 const size_t size_;
1063 size_t bytes_written_;
1064};
1065
1066// A Writer that appends data to a file |fd| at its current position.
1067// The file will be truncated to the end of the written data.
Narayan Kamath485b3642017-10-26 14:42:39 +01001068class FileWriter : public zip_archive::Writer {
Narayan Kamathf899bd52015-04-17 11:53:14 +01001069 public:
Narayan Kamathf899bd52015-04-17 11:53:14 +01001070 // Creates a FileWriter for |fd| and prepare to write |entry| to it,
1071 // guaranteeing that the file descriptor is valid and that there's enough
1072 // space on the volume to write out the entry completely and that the file
Tao Baoa456c212016-11-15 10:08:07 -08001073 // is truncated to the correct length (no truncation if |fd| references a
1074 // block device).
Narayan Kamathf899bd52015-04-17 11:53:14 +01001075 //
1076 // Returns a valid FileWriter on success, |nullptr| if an error occurred.
Tianjie088c4032020-04-07 00:12:54 -07001077 static std::unique_ptr<FileWriter> Create(int fd, const ZipEntry64* entry) {
Tianjie85c5d232020-04-01 23:08:34 -07001078 const uint64_t declared_length = entry->uncompressed_length;
Narayan Kamathf899bd52015-04-17 11:53:14 +01001079 const off64_t current_offset = lseek64(fd, 0, SEEK_CUR);
1080 if (current_offset == -1) {
1081 ALOGW("Zip: unable to seek to current location on fd %d: %s", fd, strerror(errno));
Tianjie088c4032020-04-07 00:12:54 -07001082 return nullptr;
Narayan Kamathf899bd52015-04-17 11:53:14 +01001083 }
1084
Tianjie85c5d232020-04-01 23:08:34 -07001085 if (declared_length > SIZE_MAX || declared_length > INT64_MAX) {
1086 ALOGW("Zip: file size %" PRIu64 " is too large to extract.", declared_length);
Tianjie088c4032020-04-07 00:12:54 -07001087 return nullptr;
Tianjie85c5d232020-04-01 23:08:34 -07001088 }
1089
Narayan Kamathf899bd52015-04-17 11:53:14 +01001090#if defined(__linux__)
1091 if (declared_length > 0) {
1092 // Make sure we have enough space on the volume to extract the compressed
1093 // entry. Note that the call to ftruncate below will change the file size but
1094 // will not allocate space on disk and this call to fallocate will not
1095 // change the file size.
Badhri Jagan Sridharana68d0d12015-06-02 14:47:57 -07001096 // Note: fallocate is only supported by the following filesystems -
1097 // btrfs, ext4, ocfs2, and xfs. Therefore fallocate might fail with
1098 // EOPNOTSUPP error when issued in other filesystems.
1099 // Hence, check for the return error code before concluding that the
1100 // disk does not have enough space.
Andreas Gampe964b95c2019-04-05 13:48:02 -07001101 long result = TEMP_FAILURE_RETRY(fallocate(fd, 0, current_offset, declared_length));
Badhri Jagan Sridharana68d0d12015-06-02 14:47:57 -07001102 if (result == -1 && errno == ENOSPC) {
Tianjie85c5d232020-04-01 23:08:34 -07001103 ALOGW("Zip: unable to allocate %" PRIu64 " bytes at offset %" PRId64 ": %s",
1104 declared_length, static_cast<int64_t>(current_offset), strerror(errno));
Tianjie088c4032020-04-07 00:12:54 -07001105 return nullptr;
Narayan Kamathf899bd52015-04-17 11:53:14 +01001106 }
1107 }
1108#endif // __linux__
1109
Tao Baoa456c212016-11-15 10:08:07 -08001110 struct stat sb;
1111 if (fstat(fd, &sb) == -1) {
1112 ALOGW("Zip: unable to fstat file: %s", strerror(errno));
Tianjie088c4032020-04-07 00:12:54 -07001113 return nullptr;
Narayan Kamathf899bd52015-04-17 11:53:14 +01001114 }
1115
Tao Baoa456c212016-11-15 10:08:07 -08001116 // Block device doesn't support ftruncate(2).
1117 if (!S_ISBLK(sb.st_mode)) {
Andreas Gampe964b95c2019-04-05 13:48:02 -07001118 long result = TEMP_FAILURE_RETRY(ftruncate(fd, declared_length + current_offset));
Tao Baoa456c212016-11-15 10:08:07 -08001119 if (result == -1) {
1120 ALOGW("Zip: unable to truncate file to %" PRId64 ": %s",
1121 static_cast<int64_t>(declared_length + current_offset), strerror(errno));
Tianjie088c4032020-04-07 00:12:54 -07001122 return nullptr;
Tao Baoa456c212016-11-15 10:08:07 -08001123 }
1124 }
1125
Tianjie088c4032020-04-07 00:12:54 -07001126 return std::unique_ptr<FileWriter>(new FileWriter(fd, declared_length));
Narayan Kamathf899bd52015-04-17 11:53:14 +01001127 }
1128
Chih-Hung Hsieh747eb142018-09-25 11:16:22 -07001129 FileWriter(FileWriter&& other) noexcept
Yurii Zubrytskyi834326c2017-12-20 01:01:01 -08001130 : fd_(other.fd_),
1131 declared_length_(other.declared_length_),
1132 total_bytes_written_(other.total_bytes_written_) {
1133 other.fd_ = -1;
1134 }
1135
Narayan Kamathf899bd52015-04-17 11:53:14 +01001136 virtual bool Append(uint8_t* buf, size_t buf_size) override {
Tianjie85c5d232020-04-01 23:08:34 -07001137 if (declared_length_ < buf_size || total_bytes_written_ > declared_length_ - buf_size) {
1138 ALOGW("Zip: Unexpected size %zu (declared) vs %zu (actual)", declared_length_,
Jiyong Parkcd997e62017-06-30 17:23:33 +09001139 total_bytes_written_ + buf_size);
Narayan Kamathf899bd52015-04-17 11:53:14 +01001140 return false;
1141 }
1142
Narayan Kamathe97e66e2015-04-27 16:25:53 +01001143 const bool result = android::base::WriteFully(fd_, buf, buf_size);
1144 if (result) {
1145 total_bytes_written_ += buf_size;
1146 } else {
Elliott Hughese8f4b142018-10-19 16:09:39 -07001147 ALOGW("Zip: unable to write %zu bytes to file; %s", buf_size, strerror(errno));
Narayan Kamathf899bd52015-04-17 11:53:14 +01001148 }
1149
Narayan Kamathe97e66e2015-04-27 16:25:53 +01001150 return result;
Narayan Kamathf899bd52015-04-17 11:53:14 +01001151 }
Jiyong Parkcd997e62017-06-30 17:23:33 +09001152
Narayan Kamathf899bd52015-04-17 11:53:14 +01001153 private:
Tianjie85c5d232020-04-01 23:08:34 -07001154 explicit FileWriter(const int fd = -1, const uint64_t declared_length = 0)
1155 : Writer(),
1156 fd_(fd),
1157 declared_length_(static_cast<size_t>(declared_length)),
1158 total_bytes_written_(0) {
1159 CHECK_LE(declared_length, SIZE_MAX);
1160 }
Narayan Kamathf899bd52015-04-17 11:53:14 +01001161
Yurii Zubrytskyi834326c2017-12-20 01:01:01 -08001162 int fd_;
Narayan Kamathf899bd52015-04-17 11:53:14 +01001163 const size_t declared_length_;
1164 size_t total_bytes_written_;
1165};
1166
Narayan Kamath485b3642017-10-26 14:42:39 +01001167class EntryReader : public zip_archive::Reader {
Narayan Kamath8b8faed2017-10-26 14:08:38 +01001168 public:
Tianjie85c5d232020-04-01 23:08:34 -07001169 EntryReader(const MappedZipFile& zip_file, const ZipEntry64* entry)
Narayan Kamath8b8faed2017-10-26 14:08:38 +01001170 : Reader(), zip_file_(zip_file), entry_(entry) {}
1171
Tianjie85c5d232020-04-01 23:08:34 -07001172 virtual bool ReadAtOffset(uint8_t* buf, size_t len, off64_t offset) const {
Narayan Kamath8b8faed2017-10-26 14:08:38 +01001173 return zip_file_.ReadAtOffset(buf, len, entry_->offset + offset);
1174 }
1175
1176 virtual ~EntryReader() {}
1177
1178 private:
1179 const MappedZipFile& zip_file_;
Tianjie85c5d232020-04-01 23:08:34 -07001180 const ZipEntry64* entry_;
Narayan Kamath8b8faed2017-10-26 14:08:38 +01001181};
1182
Dmitriy Ivanovf94e1592015-03-06 13:27:59 -08001183// This method is using libz macros with old-style-casts
1184#pragma GCC diagnostic push
1185#pragma GCC diagnostic ignored "-Wold-style-cast"
1186static inline int zlib_inflateInit2(z_stream* stream, int window_bits) {
1187 return inflateInit2(stream, window_bits);
1188}
1189#pragma GCC diagnostic pop
1190
Narayan Kamath485b3642017-10-26 14:42:39 +01001191namespace zip_archive {
1192
1193// Moved out of line to avoid -Wweak-vtables.
1194Reader::~Reader() {}
1195Writer::~Writer() {}
1196
Tianjie85c5d232020-04-01 23:08:34 -07001197int32_t Inflate(const Reader& reader, const uint64_t compressed_length,
1198 const uint64_t uncompressed_length, Writer* writer, uint64_t* crc_out) {
Dmitriy Ivanovedbabfe2015-03-12 09:58:15 -07001199 const size_t kBufSize = 32768;
1200 std::vector<uint8_t> read_buf(kBufSize);
1201 std::vector<uint8_t> write_buf(kBufSize);
Narayan Kamath7462f022013-11-21 13:05:04 +00001202 z_stream zstream;
1203 int zerr;
1204
1205 /*
1206 * Initialize the zlib stream struct.
1207 */
1208 memset(&zstream, 0, sizeof(zstream));
1209 zstream.zalloc = Z_NULL;
1210 zstream.zfree = Z_NULL;
1211 zstream.opaque = Z_NULL;
1212 zstream.next_in = NULL;
1213 zstream.avail_in = 0;
Dmitriy Ivanovedbabfe2015-03-12 09:58:15 -07001214 zstream.next_out = &write_buf[0];
Narayan Kamath7462f022013-11-21 13:05:04 +00001215 zstream.avail_out = kBufSize;
1216 zstream.data_type = Z_UNKNOWN;
1217
1218 /*
1219 * Use the undocumented "negative window bits" feature to tell zlib
1220 * that there's no zlib header waiting for it.
1221 */
Dmitriy Ivanovf94e1592015-03-06 13:27:59 -08001222 zerr = zlib_inflateInit2(&zstream, -MAX_WBITS);
Narayan Kamath7462f022013-11-21 13:05:04 +00001223 if (zerr != Z_OK) {
1224 if (zerr == Z_VERSION_ERROR) {
Jiyong Parkcd997e62017-06-30 17:23:33 +09001225 ALOGE("Installed zlib is not compatible with linked version (%s)", ZLIB_VERSION);
Narayan Kamath7462f022013-11-21 13:05:04 +00001226 } else {
1227 ALOGW("Call to inflateInit2 failed (zerr=%d)", zerr);
1228 }
1229
1230 return kZlibError;
1231 }
1232
Dmitriy Ivanov1f741e52015-03-06 14:26:37 -08001233 auto zstream_deleter = [](z_stream* stream) {
Jiyong Parkcd997e62017-06-30 17:23:33 +09001234 inflateEnd(stream); /* free up any allocated structures */
Dmitriy Ivanov1f741e52015-03-06 14:26:37 -08001235 };
1236
1237 std::unique_ptr<z_stream, decltype(zstream_deleter)> zstream_guard(&zstream, zstream_deleter);
1238
Narayan Kamath2d1e23f2017-10-30 11:17:28 +00001239 const bool compute_crc = (crc_out != nullptr);
Andreas Gampe964b95c2019-04-05 13:48:02 -07001240 uLong crc = 0;
Tianjie85c5d232020-04-01 23:08:34 -07001241 uint64_t remaining_bytes = compressed_length;
1242 uint64_t total_output = 0;
Narayan Kamath7462f022013-11-21 13:05:04 +00001243 do {
1244 /* read as much as we can */
1245 if (zstream.avail_in == 0) {
Tianjie85c5d232020-04-01 23:08:34 -07001246 const uint32_t read_size =
1247 (remaining_bytes > kBufSize) ? kBufSize : static_cast<uint32_t>(remaining_bytes);
1248 const off64_t offset = (compressed_length - remaining_bytes);
Adam Lesinskide117e42017-06-19 10:27:38 -07001249 // Make sure to read at offset to ensure concurrent access to the fd.
Narayan Kamath8b8faed2017-10-26 14:08:38 +01001250 if (!reader.ReadAtOffset(read_buf.data(), read_size, offset)) {
Andreas Gampe964b95c2019-04-05 13:48:02 -07001251 ALOGW("Zip: inflate read failed, getSize = %u: %s", read_size, strerror(errno));
Dmitriy Ivanov1f741e52015-03-06 14:26:37 -08001252 return kIoError;
Narayan Kamath7462f022013-11-21 13:05:04 +00001253 }
1254
Narayan Kamath8b8faed2017-10-26 14:08:38 +01001255 remaining_bytes -= read_size;
Narayan Kamath7462f022013-11-21 13:05:04 +00001256
Dmitriy Ivanovedbabfe2015-03-12 09:58:15 -07001257 zstream.next_in = &read_buf[0];
Narayan Kamath8b8faed2017-10-26 14:08:38 +01001258 zstream.avail_in = read_size;
Narayan Kamath7462f022013-11-21 13:05:04 +00001259 }
1260
1261 /* uncompress the data */
1262 zerr = inflate(&zstream, Z_NO_FLUSH);
1263 if (zerr != Z_OK && zerr != Z_STREAM_END) {
Jiyong Parkcd997e62017-06-30 17:23:33 +09001264 ALOGW("Zip: inflate zerr=%d (nIn=%p aIn=%u nOut=%p aOut=%u)", zerr, zstream.next_in,
1265 zstream.avail_in, zstream.next_out, zstream.avail_out);
Dmitriy Ivanov1f741e52015-03-06 14:26:37 -08001266 return kZlibError;
Narayan Kamath7462f022013-11-21 13:05:04 +00001267 }
1268
1269 /* write when we're full or when we're done */
Jiyong Parkcd997e62017-06-30 17:23:33 +09001270 if (zstream.avail_out == 0 || (zerr == Z_STREAM_END && zstream.avail_out != kBufSize)) {
Dmitriy Ivanovedbabfe2015-03-12 09:58:15 -07001271 const size_t write_size = zstream.next_out - &write_buf[0];
Narayan Kamathf899bd52015-04-17 11:53:14 +01001272 if (!writer->Append(&write_buf[0], write_size)) {
Narayan Kamath2d1e23f2017-10-30 11:17:28 +00001273 return kIoError;
1274 } else if (compute_crc) {
Andreas Gampe964b95c2019-04-05 13:48:02 -07001275 DCHECK_LE(write_size, kBufSize);
1276 crc = crc32(crc, &write_buf[0], static_cast<uint32_t>(write_size));
Narayan Kamath7462f022013-11-21 13:05:04 +00001277 }
Narayan Kamath7462f022013-11-21 13:05:04 +00001278
Tianjie85c5d232020-04-01 23:08:34 -07001279 total_output += kBufSize - zstream.avail_out;
Dmitriy Ivanovedbabfe2015-03-12 09:58:15 -07001280 zstream.next_out = &write_buf[0];
Narayan Kamath7462f022013-11-21 13:05:04 +00001281 zstream.avail_out = kBufSize;
1282 }
1283 } while (zerr == Z_OK);
1284
Elliott Hughese8f4b142018-10-19 16:09:39 -07001285 CHECK_EQ(zerr, Z_STREAM_END); /* other errors should've been caught */
Narayan Kamath7462f022013-11-21 13:05:04 +00001286
Narayan Kamath162b7052017-06-05 13:21:12 +01001287 // NOTE: zstream.adler is always set to 0, because we're using the -MAX_WBITS
1288 // "feature" of zlib to tell it there won't be a zlib file header. zlib
1289 // doesn't bother calculating the checksum in that scenario. We just do
1290 // it ourselves above because there are no additional gains to be made by
1291 // having zlib calculate it for us, since they do it by calling crc32 in
1292 // the same manner that we have above.
Narayan Kamath2d1e23f2017-10-30 11:17:28 +00001293 if (compute_crc) {
1294 *crc_out = crc;
1295 }
Tianjie85c5d232020-04-01 23:08:34 -07001296 if (total_output != uncompressed_length || remaining_bytes != 0) {
1297 ALOGW("Zip: size mismatch on inflated file (%lu vs %" PRIu64 ")", zstream.total_out,
Jiyong Parkcd997e62017-06-30 17:23:33 +09001298 uncompressed_length);
Dmitriy Ivanov1f741e52015-03-06 14:26:37 -08001299 return kInconsistentInformation;
Narayan Kamath7462f022013-11-21 13:05:04 +00001300 }
1301
Dmitriy Ivanov1f741e52015-03-06 14:26:37 -08001302 return 0;
Narayan Kamath7462f022013-11-21 13:05:04 +00001303}
Narayan Kamath485b3642017-10-26 14:42:39 +01001304} // namespace zip_archive
Narayan Kamath7462f022013-11-21 13:05:04 +00001305
Tianjie85c5d232020-04-01 23:08:34 -07001306static int32_t InflateEntryToWriter(MappedZipFile& mapped_zip, const ZipEntry64* entry,
Narayan Kamath485b3642017-10-26 14:42:39 +01001307 zip_archive::Writer* writer, uint64_t* crc_out) {
Narayan Kamath8b8faed2017-10-26 14:08:38 +01001308 const EntryReader reader(mapped_zip, entry);
1309
Narayan Kamath485b3642017-10-26 14:42:39 +01001310 return zip_archive::Inflate(reader, entry->compressed_length, entry->uncompressed_length, writer,
1311 crc_out);
Narayan Kamath8b8faed2017-10-26 14:08:38 +01001312}
1313
Tianjie85c5d232020-04-01 23:08:34 -07001314static int32_t CopyEntryToWriter(MappedZipFile& mapped_zip, const ZipEntry64* entry,
Narayan Kamath485b3642017-10-26 14:42:39 +01001315 zip_archive::Writer* writer, uint64_t* crc_out) {
Narayan Kamathf899bd52015-04-17 11:53:14 +01001316 static const uint32_t kBufSize = 32768;
1317 std::vector<uint8_t> buf(kBufSize);
1318
Tianjie85c5d232020-04-01 23:08:34 -07001319 const uint64_t length = entry->uncompressed_length;
1320 uint64_t count = 0;
Andreas Gampe964b95c2019-04-05 13:48:02 -07001321 uLong crc = 0;
Narayan Kamathf899bd52015-04-17 11:53:14 +01001322 while (count < length) {
Tianjie85c5d232020-04-01 23:08:34 -07001323 uint64_t remaining = length - count;
Adam Lesinskide117e42017-06-19 10:27:38 -07001324 off64_t offset = entry->offset + count;
Narayan Kamathf899bd52015-04-17 11:53:14 +01001325
Adam Lesinskide117e42017-06-19 10:27:38 -07001326 // Safe conversion because kBufSize is narrow enough for a 32 bit signed value.
Tianjie85c5d232020-04-01 23:08:34 -07001327 const uint32_t block_size =
1328 (remaining > kBufSize) ? kBufSize : static_cast<uint32_t>(remaining);
Adam Lesinskide117e42017-06-19 10:27:38 -07001329
1330 // Make sure to read at offset to ensure concurrent access to the fd.
1331 if (!mapped_zip.ReadAtOffset(buf.data(), block_size, offset)) {
Andreas Gampe964b95c2019-04-05 13:48:02 -07001332 ALOGW("CopyFileToFile: copy read failed, block_size = %u, offset = %" PRId64 ": %s",
Adam Lesinskide117e42017-06-19 10:27:38 -07001333 block_size, static_cast<int64_t>(offset), strerror(errno));
Narayan Kamathf899bd52015-04-17 11:53:14 +01001334 return kIoError;
1335 }
1336
1337 if (!writer->Append(&buf[0], block_size)) {
1338 return kIoError;
1339 }
Yurii Zubrytskyi8d8f6372020-04-06 19:35:33 -07001340 if (crc_out) {
1341 crc = crc32(crc, &buf[0], block_size);
1342 }
Narayan Kamathf899bd52015-04-17 11:53:14 +01001343 count += block_size;
1344 }
1345
Yurii Zubrytskyi8d8f6372020-04-06 19:35:33 -07001346 if (crc_out) {
1347 *crc_out = crc;
1348 }
Narayan Kamathf899bd52015-04-17 11:53:14 +01001349
1350 return 0;
1351}
1352
Tianjie85c5d232020-04-01 23:08:34 -07001353int32_t ExtractToWriter(ZipArchiveHandle handle, const ZipEntry64* entry,
1354 zip_archive::Writer* writer) {
Narayan Kamath7462f022013-11-21 13:05:04 +00001355 const uint16_t method = entry->method;
Narayan Kamath7462f022013-11-21 13:05:04 +00001356
1357 // this should default to kUnknownCompressionMethod.
1358 int32_t return_value = -1;
1359 uint64_t crc = 0;
1360 if (method == kCompressStored) {
Yurii Zubrytskyi8d8f6372020-04-06 19:35:33 -07001361 return_value =
1362 CopyEntryToWriter(handle->mapped_zip, entry, writer, kCrcChecksEnabled ? &crc : nullptr);
Narayan Kamath7462f022013-11-21 13:05:04 +00001363 } else if (method == kCompressDeflated) {
Yurii Zubrytskyi8d8f6372020-04-06 19:35:33 -07001364 return_value =
1365 InflateEntryToWriter(handle->mapped_zip, entry, writer, kCrcChecksEnabled ? &crc : nullptr);
Narayan Kamath7462f022013-11-21 13:05:04 +00001366 }
1367
1368 if (!return_value && entry->has_data_descriptor) {
Tianjie85c5d232020-04-01 23:08:34 -07001369 return_value = ValidateDataDescriptor(handle->mapped_zip, entry);
Narayan Kamath7462f022013-11-21 13:05:04 +00001370 if (return_value) {
1371 return return_value;
1372 }
1373 }
1374
Narayan Kamath162b7052017-06-05 13:21:12 +01001375 // Validate that the CRC matches the calculated value.
1376 if (kCrcChecksEnabled && (entry->crc32 != static_cast<uint32_t>(crc))) {
Mark Salyzyn088bf902014-05-08 16:02:20 -07001377 ALOGW("Zip: crc mismatch: expected %" PRIu32 ", was %" PRIu64, entry->crc32, crc);
Narayan Kamath7462f022013-11-21 13:05:04 +00001378 return kInconsistentInformation;
1379 }
1380
1381 return return_value;
1382}
1383
Tianjie85c5d232020-04-01 23:08:34 -07001384int32_t ExtractToMemory(ZipArchiveHandle archive, const ZipEntry* entry, uint8_t* begin,
1385 size_t size) {
1386 ZipEntry64 entry64(*entry);
1387 return ExtractToMemory(archive, &entry64, begin, size);
1388}
1389
1390int32_t ExtractToMemory(ZipArchiveHandle archive, const ZipEntry64* entry, uint8_t* begin,
1391 size_t size) {
1392 auto writer = MemoryWriter::Create(begin, size, entry);
Tianjie088c4032020-04-07 00:12:54 -07001393 if (!writer) {
Tianjie85c5d232020-04-01 23:08:34 -07001394 return kIoError;
1395 }
1396
Tianjie088c4032020-04-07 00:12:54 -07001397 return ExtractToWriter(archive, entry, writer.get());
Narayan Kamathf899bd52015-04-17 11:53:14 +01001398}
1399
Tianjie85c5d232020-04-01 23:08:34 -07001400int32_t ExtractEntryToFile(ZipArchiveHandle archive, const ZipEntry* entry, int fd) {
1401 ZipEntry64 entry64(*entry);
1402 return ExtractEntryToFile(archive, &entry64, fd);
1403}
1404
1405int32_t ExtractEntryToFile(ZipArchiveHandle archive, const ZipEntry64* entry, int fd) {
Yurii Zubrytskyi834326c2017-12-20 01:01:01 -08001406 auto writer = FileWriter::Create(fd, entry);
Tianjie088c4032020-04-07 00:12:54 -07001407 if (!writer) {
Narayan Kamath7462f022013-11-21 13:05:04 +00001408 return kIoError;
1409 }
1410
Tianjie088c4032020-04-07 00:12:54 -07001411 return ExtractToWriter(archive, entry, writer.get());
Narayan Kamath7462f022013-11-21 13:05:04 +00001412}
1413
Ryan Prichard3673f992018-10-10 22:41:14 -07001414int GetFileDescriptor(const ZipArchiveHandle archive) {
1415 return archive->mapped_zip.GetFileDescriptor();
Narayan Kamath7462f022013-11-21 13:05:04 +00001416}
Colin Cross7c6c7f02016-09-16 10:15:51 -07001417
Ryan Mitchell23150e42020-03-09 09:33:46 -07001418off64_t GetFileDescriptorOffset(const ZipArchiveHandle archive) {
1419 return archive->mapped_zip.GetFileOffset();
1420}
1421
Tianjie Xu18c25922016-09-29 15:27:41 -07001422#if !defined(_WIN32)
Narayan Kamath485b3642017-10-26 14:42:39 +01001423class ProcessWriter : public zip_archive::Writer {
Tianjie Xu18c25922016-09-29 15:27:41 -07001424 public:
Jiyong Parkcd997e62017-06-30 17:23:33 +09001425 ProcessWriter(ProcessZipEntryFunction func, void* cookie)
1426 : Writer(), proc_function_(func), cookie_(cookie) {}
Tianjie Xu18c25922016-09-29 15:27:41 -07001427
1428 virtual bool Append(uint8_t* buf, size_t buf_size) override {
1429 return proc_function_(buf, buf_size, cookie_);
1430 }
1431
1432 private:
1433 ProcessZipEntryFunction proc_function_;
1434 void* cookie_;
1435};
1436
Tianjie85c5d232020-04-01 23:08:34 -07001437int32_t ProcessZipEntryContents(ZipArchiveHandle archive, const ZipEntry* entry,
1438 ProcessZipEntryFunction func, void* cookie) {
1439 ZipEntry64 entry64(*entry);
1440 return ProcessZipEntryContents(archive, &entry64, func, cookie);
1441}
1442
1443int32_t ProcessZipEntryContents(ZipArchiveHandle archive, const ZipEntry64* entry,
Tianjie Xu18c25922016-09-29 15:27:41 -07001444 ProcessZipEntryFunction func, void* cookie) {
1445 ProcessWriter writer(func, cookie);
Ryan Prichard3673f992018-10-10 22:41:14 -07001446 return ExtractToWriter(archive, entry, &writer);
Tianjie Xu18c25922016-09-29 15:27:41 -07001447}
1448
Jiyong Parkcd997e62017-06-30 17:23:33 +09001449#endif //! defined(_WIN32)
Tianjie Xu18c25922016-09-29 15:27:41 -07001450
1451int MappedZipFile::GetFileDescriptor() const {
1452 if (!has_fd_) {
1453 ALOGW("Zip: MappedZipFile doesn't have a file descriptor.");
1454 return -1;
1455 }
1456 return fd_;
1457}
1458
Elliott Hughesf66460b2019-10-22 11:44:50 -07001459const void* MappedZipFile::GetBasePtr() const {
Tianjie Xu18c25922016-09-29 15:27:41 -07001460 if (has_fd_) {
1461 ALOGW("Zip: MappedZipFile doesn't have a base pointer.");
1462 return nullptr;
1463 }
1464 return base_ptr_;
1465}
1466
Ryan Mitchell23150e42020-03-09 09:33:46 -07001467off64_t MappedZipFile::GetFileOffset() const {
1468 return fd_offset_;
1469}
1470
Tianjie Xu18c25922016-09-29 15:27:41 -07001471off64_t MappedZipFile::GetFileLength() const {
1472 if (has_fd_) {
Ryan Mitchell23150e42020-03-09 09:33:46 -07001473 if (data_length_ != -1) {
1474 return data_length_;
1475 }
1476 data_length_ = lseek64(fd_, 0, SEEK_END);
1477 if (data_length_ == -1) {
Tianjie Xu18c25922016-09-29 15:27:41 -07001478 ALOGE("Zip: lseek on fd %d failed: %s", fd_, strerror(errno));
1479 }
Ryan Mitchell23150e42020-03-09 09:33:46 -07001480 return data_length_;
Tianjie Xu18c25922016-09-29 15:27:41 -07001481 } else {
1482 if (base_ptr_ == nullptr) {
Elliott Hughesfba2a1a2019-12-16 16:16:16 -08001483 ALOGE("Zip: invalid file map");
Tianjie Xu18c25922016-09-29 15:27:41 -07001484 return -1;
1485 }
Ryan Mitchell23150e42020-03-09 09:33:46 -07001486 return data_length_;
Tianjie Xu18c25922016-09-29 15:27:41 -07001487 }
1488}
1489
Tianjie Xu18c25922016-09-29 15:27:41 -07001490// Attempts to read |len| bytes into |buf| at offset |off|.
Narayan Kamath8b8faed2017-10-26 14:08:38 +01001491bool MappedZipFile::ReadAtOffset(uint8_t* buf, size_t len, off64_t off) const {
Tianjie Xu18c25922016-09-29 15:27:41 -07001492 if (has_fd_) {
Ryan Mitchell23150e42020-03-09 09:33:46 -07001493 if (off < 0) {
1494 ALOGE("Zip: invalid offset %" PRId64, off);
1495 return false;
1496 }
1497
1498 off64_t read_offset;
1499 if (__builtin_add_overflow(fd_offset_, off, &read_offset)) {
1500 ALOGE("Zip: invalid read offset %" PRId64 " overflows, fd offset %" PRId64, off, fd_offset_);
1501 return false;
1502 }
1503
1504 if (data_length_ != -1) {
1505 off64_t read_end;
1506 if (len > std::numeric_limits<off64_t>::max() ||
1507 __builtin_add_overflow(off, static_cast<off64_t>(len), &read_end)) {
1508 ALOGE("Zip: invalid read length %" PRId64 " overflows, offset %" PRId64,
1509 static_cast<off64_t>(len), off);
1510 return false;
1511 }
1512
1513 if (read_end > data_length_) {
1514 ALOGE("Zip: invalid read length %" PRId64 " exceeds data length %" PRId64 ", offset %"
1515 PRId64, static_cast<off64_t>(len), data_length_, off);
1516 return false;
1517 }
1518 }
1519
1520 if (!android::base::ReadFullyAtOffset(fd_, buf, len, read_offset)) {
Elliott Hughesfba2a1a2019-12-16 16:16:16 -08001521 ALOGE("Zip: failed to read at offset %" PRId64, off);
Tianjie Xu18c25922016-09-29 15:27:41 -07001522 return false;
1523 }
Adam Lesinskide117e42017-06-19 10:27:38 -07001524 } else {
Tianjied9bc8fd2020-04-13 16:29:22 -07001525 if (off < 0 || data_length_ < len || off > data_length_ - len) {
1526 ALOGE("Zip: invalid offset: %" PRId64 ", read length: %zu, data length: %" PRId64, off, len,
1527 data_length_);
Adam Lesinskide117e42017-06-19 10:27:38 -07001528 return false;
1529 }
Elliott Hughesf66460b2019-10-22 11:44:50 -07001530 memcpy(buf, static_cast<const uint8_t*>(base_ptr_) + off, len);
Tianjie Xu18c25922016-09-29 15:27:41 -07001531 }
Adam Lesinskide117e42017-06-19 10:27:38 -07001532 return true;
Tianjie Xu18c25922016-09-29 15:27:41 -07001533}
1534
Elliott Hughesf66460b2019-10-22 11:44:50 -07001535void CentralDirectory::Initialize(const void* map_base_ptr, off64_t cd_start_offset,
1536 size_t cd_size) {
1537 base_ptr_ = static_cast<const uint8_t*>(map_base_ptr) + cd_start_offset;
Tianjie Xu18c25922016-09-29 15:27:41 -07001538 length_ = cd_size;
1539}
1540
Elliott Hughese8f4b142018-10-19 16:09:39 -07001541bool ZipArchive::InitializeCentralDirectory(off64_t cd_start_offset, size_t cd_size) {
Tianjie Xu18c25922016-09-29 15:27:41 -07001542 if (mapped_zip.HasFd()) {
Elliott Hughese8f4b142018-10-19 16:09:39 -07001543 directory_map = android::base::MappedFile::FromFd(mapped_zip.GetFileDescriptor(),
Ryan Mitchell23150e42020-03-09 09:33:46 -07001544 mapped_zip.GetFileOffset() + cd_start_offset,
1545 cd_size, PROT_READ);
Elliott Hughesfba2a1a2019-12-16 16:16:16 -08001546 if (!directory_map) {
1547 ALOGE("Zip: failed to map central directory (offset %" PRId64 ", size %zu): %s",
1548 cd_start_offset, cd_size, strerror(errno));
1549 return false;
1550 }
Tianjie Xu18c25922016-09-29 15:27:41 -07001551
Elliott Hughese8f4b142018-10-19 16:09:39 -07001552 CHECK_EQ(directory_map->size(), cd_size);
1553 central_directory.Initialize(directory_map->data(), 0 /*offset*/, cd_size);
Tianjie Xu18c25922016-09-29 15:27:41 -07001554 } else {
1555 if (mapped_zip.GetBasePtr() == nullptr) {
Elliott Hughesfba2a1a2019-12-16 16:16:16 -08001556 ALOGE("Zip: Failed to map central directory, bad mapped_zip base pointer");
Tianjie Xu18c25922016-09-29 15:27:41 -07001557 return false;
1558 }
1559 if (static_cast<off64_t>(cd_start_offset) + static_cast<off64_t>(cd_size) >
1560 mapped_zip.GetFileLength()) {
Jiyong Parkcd997e62017-06-30 17:23:33 +09001561 ALOGE(
1562 "Zip: Failed to map central directory, offset exceeds mapped memory region ("
1563 "start_offset %" PRId64 ", cd_size %zu, mapped_region_size %" PRId64 ")",
1564 static_cast<int64_t>(cd_start_offset), cd_size, mapped_zip.GetFileLength());
Tianjie Xu18c25922016-09-29 15:27:41 -07001565 return false;
1566 }
1567
1568 central_directory.Initialize(mapped_zip.GetBasePtr(), cd_start_offset, cd_size);
1569 }
1570 return true;
1571}
Elliott Hughes55fd2932017-05-28 22:59:04 -07001572
Tianjie426bf3a2020-04-15 16:30:39 -07001573// This function returns the embedded timestamp as is; and doesn't perform validations.
Tianjie85c5d232020-04-01 23:08:34 -07001574tm ZipEntryCommon::GetModificationTime() const {
Elliott Hughes55fd2932017-05-28 22:59:04 -07001575 tm t = {};
1576
1577 t.tm_hour = (mod_time >> 11) & 0x1f;
1578 t.tm_min = (mod_time >> 5) & 0x3f;
1579 t.tm_sec = (mod_time & 0x1f) << 1;
1580
1581 t.tm_year = ((mod_time >> 25) & 0x7f) + 80;
1582 t.tm_mon = ((mod_time >> 21) & 0xf) - 1;
1583 t.tm_mday = (mod_time >> 16) & 0x1f;
1584
1585 return t;
1586}