blob: afbc5d8910918bd5c68bdb97b61fac0f80fa37eb [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
Tianjie Xu69ee4b72020-03-11 11:59:10 -070068// Set a reasonable cap (256 GiB) for the zip file size. So the data is always valid when
69// we parse the fields in cd or local headers as 64 bits signed integers.
70static constexpr uint64_t kMaxFileLength = 256 * static_cast<uint64_t>(1u << 30u);
71
Narayan Kamath7462f022013-11-21 13:05:04 +000072/*
73 * A Read-only Zip archive.
74 *
75 * We want "open" and "find entry by name" to be fast operations, and
76 * we want to use as little memory as possible. We memory-map the zip
77 * central directory, and load a hash table with pointers to the filenames
78 * (which aren't null-terminated). The other fields are at a fixed offset
79 * from the filename, so we don't need to extract those (but we do need
80 * to byte-read and endian-swap them every time we want them).
81 *
82 * It's possible that somebody has handed us a massive (~1GB) zip archive,
83 * so we can't expect to mmap the entire file.
84 *
85 * To speed comparisons when doing a lookup by name, we could make the mapping
86 * "private" (copy-on-write) and null-terminate the filenames after verifying
87 * the record structure. However, this requires a private mapping of
88 * every page that the Central Directory touches. Easier to tuck a copy
89 * of the string length into the hash table entry.
90 */
Narayan Kamath7462f022013-11-21 13:05:04 +000091
Josh Gaoabdfc242018-09-07 12:44:40 -070092#if defined(__BIONIC__)
93uint64_t GetOwnerTag(const ZipArchive* archive) {
94 return android_fdsan_create_owner_tag(ANDROID_FDSAN_OWNER_TYPE_ZIPARCHIVE,
95 reinterpret_cast<uint64_t>(archive));
96}
97#endif
98
Ryan Mitchell23150e42020-03-09 09:33:46 -070099ZipArchive::ZipArchive(MappedZipFile&& map, bool assume_ownership)
100 : mapped_zip(map),
Josh Gao1b496342018-07-17 11:08:48 -0700101 close_file(assume_ownership),
102 directory_offset(0),
103 central_directory(),
Elliott Hughese8f4b142018-10-19 16:09:39 -0700104 directory_map(),
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800105 num_entries(0) {
Josh Gao1b496342018-07-17 11:08:48 -0700106#if defined(__BIONIC__)
107 if (assume_ownership) {
Ryan Mitchell23150e42020-03-09 09:33:46 -0700108 CHECK(mapped_zip.HasFd());
109 android_fdsan_exchange_owner_tag(mapped_zip.GetFileDescriptor(), 0, GetOwnerTag(this));
Josh Gao1b496342018-07-17 11:08:48 -0700110 }
111#endif
112}
113
Elliott Hughesf66460b2019-10-22 11:44:50 -0700114ZipArchive::ZipArchive(const void* address, size_t length)
Josh Gao1b496342018-07-17 11:08:48 -0700115 : mapped_zip(address, length),
116 close_file(false),
117 directory_offset(0),
118 central_directory(),
Elliott Hughese8f4b142018-10-19 16:09:39 -0700119 directory_map(),
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800120 num_entries(0) {}
Josh Gao1b496342018-07-17 11:08:48 -0700121
122ZipArchive::~ZipArchive() {
123 if (close_file && mapped_zip.GetFileDescriptor() >= 0) {
124#if defined(__BIONIC__)
Josh Gaoabdfc242018-09-07 12:44:40 -0700125 android_fdsan_close_with_tag(mapped_zip.GetFileDescriptor(), GetOwnerTag(this));
Josh Gao1b496342018-07-17 11:08:48 -0700126#else
127 close(mapped_zip.GetFileDescriptor());
128#endif
129 }
Josh Gao1b496342018-07-17 11:08:48 -0700130}
131
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700132struct CentralDirectoryInfo {
133 uint64_t num_records;
134 // The size of the central directory (in bytes).
135 uint64_t cd_size;
136 // The offset of the start of the central directory, relative
137 // to the start of the file.
138 uint64_t cd_start_offset;
139};
140
141static ZipError FindCentralDirectoryInfoForZip64(CentralDirectoryInfo* /* cdInfo */) {
142 ALOGW("Zip: Parsing zip64 EOCD isn't supported yet.");
143 return kInvalidFile;
144}
145
146static ZipError FindCentralDirectoryInfo(const char* debug_file_name, ZipArchive* archive,
147 off64_t file_length, uint32_t read_amount,
148 CentralDirectoryInfo* cdInfo) {
149 std::vector<uint8_t> scan_buffer(read_amount);
Narayan Kamath7462f022013-11-21 13:05:04 +0000150 const off64_t search_start = file_length - read_amount;
151
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700152 if (!archive->mapped_zip.ReadAtOffset(scan_buffer.data(), read_amount, search_start)) {
Jiyong Parkcd997e62017-06-30 17:23:33 +0900153 ALOGE("Zip: read %" PRId64 " from offset %" PRId64 " failed", static_cast<int64_t>(read_amount),
154 static_cast<int64_t>(search_start));
Narayan Kamath7462f022013-11-21 13:05:04 +0000155 return kIoError;
156 }
157
158 /*
159 * Scan backward for the EOCD magic. In an archive without a trailing
160 * comment, we'll find it on the first try. (We may want to consider
161 * doing an initial minimal read; if we don't find it, retry with a
162 * second read as above.)
163 */
Andreas Gampe964b95c2019-04-05 13:48:02 -0700164 CHECK_LE(read_amount, std::numeric_limits<int32_t>::max());
165 int32_t i = read_amount - sizeof(EocdRecord);
Narayan Kamath926973e2014-06-09 14:18:14 +0100166 for (; i >= 0; i--) {
Dan Albert1ae07642015-04-09 14:11:18 -0700167 if (scan_buffer[i] == 0x50) {
168 uint32_t* sig_addr = reinterpret_cast<uint32_t*>(&scan_buffer[i]);
169 if (get_unaligned<uint32_t>(sig_addr) == EocdRecord::kSignature) {
170 ALOGV("+++ Found EOCD at buf+%d", i);
171 break;
172 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000173 }
174 }
175 if (i < 0) {
176 ALOGD("Zip: EOCD not found, %s is not zip", debug_file_name);
177 return kInvalidFile;
178 }
179
180 const off64_t eocd_offset = search_start + i;
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700181 auto eocd = reinterpret_cast<const EocdRecord*>(scan_buffer.data() + i);
Narayan Kamath7462f022013-11-21 13:05:04 +0000182 /*
Narayan Kamath926973e2014-06-09 14:18:14 +0100183 * Verify that there's no trailing space at the end of the central directory
184 * and its comment.
Narayan Kamath7462f022013-11-21 13:05:04 +0000185 */
Jiyong Parkcd997e62017-06-30 17:23:33 +0900186 const off64_t calculated_length = eocd_offset + sizeof(EocdRecord) + eocd->comment_length;
Narayan Kamath926973e2014-06-09 14:18:14 +0100187 if (calculated_length != file_length) {
Narayan Kamath4f6b4992014-06-03 13:59:23 +0100188 ALOGW("Zip: %" PRId64 " extraneous bytes at the end of the central directory",
Narayan Kamath926973e2014-06-09 14:18:14 +0100189 static_cast<int64_t>(file_length - calculated_length));
Narayan Kamath4f6b4992014-06-03 13:59:23 +0100190 return kInvalidFile;
191 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000192
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700193 // One of the field is 0xFFFFFFFF, look for the zip64 EOCD instead.
194 if (eocd->cd_size == UINT32_MAX || eocd->cd_start_offset == UINT32_MAX) {
195 ALOGV("Looking for the zip64 EOCD, cd_size: %" PRIu32 "cd_start_offset: %" PRId32,
196 eocd->cd_size, eocd->cd_start_offset);
197 return FindCentralDirectoryInfoForZip64(cdInfo);
198 }
199
Narayan Kamath926973e2014-06-09 14:18:14 +0100200 /*
201 * Grab the CD offset and size, and the number of entries in the
202 * archive and verify that they look reasonable.
203 */
Tianjie Xu1ee48922016-09-21 14:58:11 -0700204 if (static_cast<off64_t>(eocd->cd_start_offset) + eocd->cd_size > eocd_offset) {
Narayan Kamath926973e2014-06-09 14:18:14 +0100205 ALOGW("Zip: bad offsets (dir %" PRIu32 ", size %" PRIu32 ", eocd %" PRId64 ")",
Jiyong Parkcd997e62017-06-30 17:23:33 +0900206 eocd->cd_start_offset, eocd->cd_size, static_cast<int64_t>(eocd_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000207 return kInvalidOffset;
208 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000209
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700210 *cdInfo = {.num_records = eocd->num_records,
211 .cd_size = eocd->cd_size,
212 .cd_start_offset = eocd->cd_start_offset};
213 return kSuccess;
Narayan Kamath7462f022013-11-21 13:05:04 +0000214}
215
216/*
217 * Find the zip Central Directory and memory-map it.
218 *
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700219 * On success, returns kSuccess after populating fields from the EOCD area:
Narayan Kamath7462f022013-11-21 13:05:04 +0000220 * directory_offset
Tianjie Xu18c25922016-09-29 15:27:41 -0700221 * directory_ptr
Narayan Kamath7462f022013-11-21 13:05:04 +0000222 * num_entries
223 */
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700224static ZipError MapCentralDirectory(const char* debug_file_name, ZipArchive* archive) {
225 // 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 -0700226 off64_t file_length = archive->mapped_zip.GetFileLength();
Narayan Kamath7462f022013-11-21 13:05:04 +0000227 if (file_length == -1) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000228 return kInvalidFile;
229 }
230
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700231 if (file_length > kMaxFileLength) {
Narayan Kamath926973e2014-06-09 14:18:14 +0100232 ALOGV("Zip: zip file too long %" PRId64, static_cast<int64_t>(file_length));
Narayan Kamath7462f022013-11-21 13:05:04 +0000233 return kInvalidFile;
234 }
235
Narayan Kamath926973e2014-06-09 14:18:14 +0100236 if (file_length < static_cast<off64_t>(sizeof(EocdRecord))) {
237 ALOGV("Zip: length %" PRId64 " is too small to be zip", static_cast<int64_t>(file_length));
Narayan Kamath7462f022013-11-21 13:05:04 +0000238 return kInvalidFile;
239 }
240
241 /*
242 * Perform the traditional EOCD snipe hunt.
243 *
244 * We're searching for the End of Central Directory magic number,
245 * which appears at the start of the EOCD block. It's followed by
246 * 18 bytes of EOCD stuff and up to 64KB of archive comment. We
247 * need to read the last part of the file into a buffer, dig through
248 * it to find the magic number, parse some values out, and use those
249 * to determine the extent of the CD.
250 *
251 * We start by pulling in the last part of the file.
252 */
Andreas Gampe964b95c2019-04-05 13:48:02 -0700253 uint32_t read_amount = kMaxEOCDSearch;
Narayan Kamath926973e2014-06-09 14:18:14 +0100254 if (file_length < read_amount) {
Andreas Gampe964b95c2019-04-05 13:48:02 -0700255 read_amount = static_cast<uint32_t>(file_length);
Narayan Kamath7462f022013-11-21 13:05:04 +0000256 }
257
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700258 CentralDirectoryInfo cdInfo = {};
259 if (auto result =
260 FindCentralDirectoryInfo(debug_file_name, archive, file_length, read_amount, &cdInfo);
261 result != kSuccess) {
262 return result;
263 }
264
265 if (cdInfo.num_records == 0) {
266#if defined(__ANDROID__)
267 ALOGW("Zip: empty archive?");
268#endif
269 return kEmptyArchive;
270 }
271
272 if (cdInfo.cd_size >= SIZE_MAX) {
273 ALOGW("Zip: The size of central directory doesn't fit in range of size_t: %" PRIu64,
274 cdInfo.cd_size);
275 return kInvalidFile;
276 }
277
278 ALOGV("+++ num_entries=%" PRIu64 " dir_size=%" PRIu64 " dir_offset=%" PRIu64, cdInfo.num_records,
279 cdInfo.cd_size, cdInfo.cd_start_offset);
280
281 // It all looks good. Create a mapping for the CD, and set the fields in archive.
282 if (!archive->InitializeCentralDirectory(static_cast<off64_t>(cdInfo.cd_start_offset),
283 static_cast<size_t>(cdInfo.cd_size))) {
284 return kMmapFailed;
285 }
286
287 archive->num_entries = cdInfo.num_records;
288 archive->directory_offset = cdInfo.cd_start_offset;
289
290 return kSuccess;
Narayan Kamath7462f022013-11-21 13:05:04 +0000291}
292
293/*
294 * Parses the Zip archive's Central Directory. Allocates and populates the
295 * hash table.
296 *
297 * Returns 0 on success.
298 */
299static int32_t ParseZipArchive(ZipArchive* archive) {
Tianjie Xu18c25922016-09-29 15:27:41 -0700300 const uint8_t* const cd_ptr = archive->central_directory.GetBasePtr();
301 const size_t cd_length = archive->central_directory.GetMapLength();
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700302 const uint64_t num_entries = archive->num_entries;
Narayan Kamath7462f022013-11-21 13:05:04 +0000303
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700304 if (num_entries <= UINT16_MAX) {
305 archive->cd_entry_map = CdEntryMapZip32::Create(static_cast<uint16_t>(num_entries));
Tianjie Xu0ef97832020-03-15 21:23:24 -0700306 } else {
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700307 archive->cd_entry_map = CdEntryMapZip64::Create();
Tianjie Xu0ef97832020-03-15 21:23:24 -0700308 }
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800309 if (archive->cd_entry_map == nullptr) {
Elliott Hughesfba2a1a2019-12-16 16:16:16 -0800310 return kAllocationFailed;
Tianjie Xu9e020e22016-10-10 12:11:30 -0700311 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000312
313 /*
314 * Walk through the central directory, adding entries to the hash
315 * table and verifying values.
316 */
Narayan Kamath926973e2014-06-09 14:18:14 +0100317 const uint8_t* const cd_end = cd_ptr + cd_length;
Narayan Kamath7462f022013-11-21 13:05:04 +0000318 const uint8_t* ptr = cd_ptr;
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700319 for (uint64_t i = 0; i < num_entries; i++) {
Tianjie Xu0fda1cf2017-04-05 14:46:27 -0700320 if (ptr > cd_end - sizeof(CentralDirectoryRecord)) {
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700321 ALOGW("Zip: ran off the end (item #%" PRIu64 ", %zu bytes of central directory)", i,
Elliott Hughesfba2a1a2019-12-16 16:16:16 -0800322 cd_length);
Tianjie Xu0fda1cf2017-04-05 14:46:27 -0700323#if defined(__ANDROID__)
324 android_errorWriteLog(0x534e4554, "36392138");
325#endif
Elliott Hughesfba2a1a2019-12-16 16:16:16 -0800326 return kInvalidFile;
Tianjie Xu0fda1cf2017-04-05 14:46:27 -0700327 }
328
Jiyong Parkcd997e62017-06-30 17:23:33 +0900329 const CentralDirectoryRecord* cdr = reinterpret_cast<const CentralDirectoryRecord*>(ptr);
Narayan Kamath926973e2014-06-09 14:18:14 +0100330 if (cdr->record_signature != CentralDirectoryRecord::kSignature) {
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700331 ALOGW("Zip: missed a central dir sig (at %" PRIu64 ")", i);
Elliott Hughesfba2a1a2019-12-16 16:16:16 -0800332 return kInvalidFile;
Narayan Kamath7462f022013-11-21 13:05:04 +0000333 }
334
Narayan Kamath926973e2014-06-09 14:18:14 +0100335 const uint16_t file_name_length = cdr->file_name_length;
336 const uint16_t extra_length = cdr->extra_field_length;
337 const uint16_t comment_length = cdr->comment_length;
Piotr Jastrzebski78271ba2014-08-15 12:53:00 +0100338 const uint8_t* file_name = ptr + sizeof(CentralDirectoryRecord);
339
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700340 if (file_name_length >= cd_length || file_name > cd_end - file_name_length) {
341 ALOGW("Zip: file name for entry %" PRIu64
Elliott Hughes50ef29a2019-06-18 18:23:59 -0700342 " exceeds the central directory range, file_name_length: %" PRIu16 ", cd_length: %zu",
343 i, file_name_length, cd_length);
Elliott Hughesfba2a1a2019-12-16 16:16:16 -0800344 return kInvalidEntryName;
Tianjie Xu9e020e22016-10-10 12:11:30 -0700345 }
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700346
347 const uint8_t* extra_field = file_name + file_name_length;
348 if (extra_length >= cd_length || extra_field > cd_end - extra_length) {
349 ALOGW("Zip: extra field for entry %" PRIu64
350 " exceeds the central directory range, file_name_length: %" PRIu16 ", cd_length: %zu",
351 i, extra_length, cd_length);
352 return kInvalidFile;
353 }
354
355 off64_t local_header_offset = cdr->local_file_header_offset;
356 if (local_header_offset == UINT32_MAX) {
357 // TODO(xunchang) parse the zip64 eocd
358 ALOGW("Zip: Parsing zip64 cd entry isn't supported yet");
359 return kInvalidFile;
360 }
361
362 if (local_header_offset >= archive->directory_offset) {
363 ALOGW("Zip: bad LFH offset %" PRId64 " at entry %" PRIu64,
364 static_cast<int64_t>(local_header_offset), i);
365 return kInvalidFile;
366 }
367
Elliott Hughes50ef29a2019-06-18 18:23:59 -0700368 // Check that file name is valid UTF-8 and doesn't contain NUL (U+0000) characters.
Narayan Kamath044bc8e2014-12-03 18:22:53 +0000369 if (!IsValidEntryName(file_name, file_name_length)) {
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700370 ALOGW("Zip: invalid file name at entry %" PRIu64, i);
Elliott Hughesfba2a1a2019-12-16 16:16:16 -0800371 return kInvalidEntryName;
Piotr Jastrzebski78271ba2014-08-15 12:53:00 +0100372 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000373
Elliott Hughes50ef29a2019-06-18 18:23:59 -0700374 // Add the CDE filename to the hash table.
375 std::string_view entry_name{reinterpret_cast<const char*>(file_name), file_name_length};
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800376 if (auto add_result =
377 archive->cd_entry_map->AddToMap(entry_name, archive->central_directory.GetBasePtr());
378 add_result != 0) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000379 ALOGW("Zip: Error adding entry to hash table %d", add_result);
Dmitriy Ivanov3ea93da2015-03-06 11:48:47 -0800380 return add_result;
Narayan Kamath7462f022013-11-21 13:05:04 +0000381 }
382
Narayan Kamath926973e2014-06-09 14:18:14 +0100383 ptr += sizeof(CentralDirectoryRecord) + file_name_length + extra_length + comment_length;
384 if ((ptr - cd_ptr) > static_cast<int64_t>(cd_length)) {
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700385 ALOGW("Zip: bad CD advance (%tu vs %zu) at entry %" PRIu64, ptr - cd_ptr, cd_length, i);
Elliott Hughesfba2a1a2019-12-16 16:16:16 -0800386 return kInvalidFile;
Narayan Kamath7462f022013-11-21 13:05:04 +0000387 }
388 }
Narayan Kamathc1a56dc2017-08-09 18:32:09 +0100389
390 uint32_t lfh_start_bytes;
391 if (!archive->mapped_zip.ReadAtOffset(reinterpret_cast<uint8_t*>(&lfh_start_bytes),
392 sizeof(uint32_t), 0)) {
393 ALOGW("Zip: Unable to read header for entry at offset == 0.");
Elliott Hughesfba2a1a2019-12-16 16:16:16 -0800394 return kInvalidFile;
Narayan Kamathc1a56dc2017-08-09 18:32:09 +0100395 }
396
397 if (lfh_start_bytes != LocalFileHeader::kSignature) {
398 ALOGW("Zip: Entry at offset zero has invalid LFH signature %" PRIx32, lfh_start_bytes);
399#if defined(__ANDROID__)
400 android_errorWriteLog(0x534e4554, "64211847");
401#endif
Elliott Hughesfba2a1a2019-12-16 16:16:16 -0800402 return kInvalidFile;
Narayan Kamathc1a56dc2017-08-09 18:32:09 +0100403 }
404
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700405 ALOGV("+++ zip good scan %" PRIu64 " entries", num_entries);
Narayan Kamath7462f022013-11-21 13:05:04 +0000406
Dmitriy Ivanov3ea93da2015-03-06 11:48:47 -0800407 return 0;
Narayan Kamath7462f022013-11-21 13:05:04 +0000408}
409
Jiyong Parkcd997e62017-06-30 17:23:33 +0900410static int32_t OpenArchiveInternal(ZipArchive* archive, const char* debug_file_name) {
Elliott Hughesfba2a1a2019-12-16 16:16:16 -0800411 int32_t result = MapCentralDirectory(debug_file_name, archive);
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700412 return result != kSuccess ? result : ParseZipArchive(archive);
Narayan Kamath7462f022013-11-21 13:05:04 +0000413}
414
Jiyong Parkcd997e62017-06-30 17:23:33 +0900415int32_t OpenArchiveFd(int fd, const char* debug_file_name, ZipArchiveHandle* handle,
416 bool assume_ownership) {
Ryan Mitchell23150e42020-03-09 09:33:46 -0700417 ZipArchive* archive = new ZipArchive(MappedZipFile(fd), assume_ownership);
Narayan Kamath7462f022013-11-21 13:05:04 +0000418 *handle = archive;
Narayan Kamath7462f022013-11-21 13:05:04 +0000419 return OpenArchiveInternal(archive, debug_file_name);
420}
421
Ryan Mitchell23150e42020-03-09 09:33:46 -0700422int32_t OpenArchiveFdRange(int fd, const char* debug_file_name, ZipArchiveHandle* handle,
423 off64_t length, off64_t offset, bool assume_ownership) {
424 ZipArchive* archive = new ZipArchive(MappedZipFile(fd, length, offset), assume_ownership);
425 *handle = archive;
426
427 if (length < 0) {
428 ALOGW("Invalid zip length %" PRId64, length);
429 return kIoError;
430 }
431
432 if (offset < 0) {
433 ALOGW("Invalid zip offset %" PRId64, offset);
434 return kIoError;
435 }
436
437 return OpenArchiveInternal(archive, debug_file_name);
438}
439
Narayan Kamath7462f022013-11-21 13:05:04 +0000440int32_t OpenArchive(const char* fileName, ZipArchiveHandle* handle) {
Nick Kralevich3bdf7442018-12-18 12:48:06 -0800441 const int fd = ::android::base::utf8::open(fileName, O_RDONLY | O_BINARY | O_CLOEXEC, 0);
Ryan Mitchell23150e42020-03-09 09:33:46 -0700442 ZipArchive* archive = new ZipArchive(MappedZipFile(fd), true);
Narayan Kamath7462f022013-11-21 13:05:04 +0000443 *handle = archive;
444
Narayan Kamath7462f022013-11-21 13:05:04 +0000445 if (fd < 0) {
446 ALOGW("Unable to open '%s': %s", fileName, strerror(errno));
447 return kIoError;
Narayan Kamath7462f022013-11-21 13:05:04 +0000448 }
Dmitriy Ivanov40b52b22014-07-15 19:33:00 -0700449
Narayan Kamath7462f022013-11-21 13:05:04 +0000450 return OpenArchiveInternal(archive, fileName);
451}
452
Elliott Hughesf66460b2019-10-22 11:44:50 -0700453int32_t OpenArchiveFromMemory(const void* address, size_t length, const char* debug_file_name,
Jiyong Parkcd997e62017-06-30 17:23:33 +0900454 ZipArchiveHandle* handle) {
Tianjie Xu18c25922016-09-29 15:27:41 -0700455 ZipArchive* archive = new ZipArchive(address, length);
456 *handle = archive;
457 return OpenArchiveInternal(archive, debug_file_name);
458}
459
Elliott Hughes26724132019-10-25 09:57:58 -0700460ZipArchiveInfo GetArchiveInfo(ZipArchiveHandle archive) {
461 ZipArchiveInfo result;
462 result.archive_size = archive->mapped_zip.GetFileLength();
463 result.entry_count = archive->num_entries;
464 return result;
465}
466
Narayan Kamath7462f022013-11-21 13:05:04 +0000467/*
468 * Close a ZipArchive, closing the file and freeing the contents.
469 */
Ryan Prichard3673f992018-10-10 22:41:14 -0700470void CloseArchive(ZipArchiveHandle archive) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000471 ALOGV("Closing archive %p", archive);
Neil Fullerb1a113f2014-07-25 14:43:04 +0100472 delete archive;
Narayan Kamath7462f022013-11-21 13:05:04 +0000473}
474
Narayan Kamath162b7052017-06-05 13:21:12 +0100475static int32_t ValidateDataDescriptor(MappedZipFile& mapped_zip, ZipEntry* entry) {
Narayan Kamath926973e2014-06-09 14:18:14 +0100476 uint8_t ddBuf[sizeof(DataDescriptor) + sizeof(DataDescriptor::kOptSignature)];
Adam Lesinskide117e42017-06-19 10:27:38 -0700477 off64_t offset = entry->offset;
478 if (entry->method != kCompressStored) {
479 offset += entry->compressed_length;
480 } else {
481 offset += entry->uncompressed_length;
482 }
483
484 if (!mapped_zip.ReadAtOffset(ddBuf, sizeof(ddBuf), offset)) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000485 return kIoError;
486 }
487
Narayan Kamath926973e2014-06-09 14:18:14 +0100488 const uint32_t ddSignature = *(reinterpret_cast<const uint32_t*>(ddBuf));
Adam Lesinskide117e42017-06-19 10:27:38 -0700489 const uint16_t ddOffset = (ddSignature == DataDescriptor::kOptSignature) ? 4 : 0;
490 const DataDescriptor* descriptor = reinterpret_cast<const DataDescriptor*>(ddBuf + ddOffset);
Narayan Kamath7462f022013-11-21 13:05:04 +0000491
Narayan Kamath162b7052017-06-05 13:21:12 +0100492 // Validate that the values in the data descriptor match those in the central
493 // directory.
494 if (entry->compressed_length != descriptor->compressed_size ||
495 entry->uncompressed_length != descriptor->uncompressed_size ||
496 entry->crc32 != descriptor->crc32) {
497 ALOGW("Zip: size/crc32 mismatch. expected {%" PRIu32 ", %" PRIu32 ", %" PRIx32
498 "}, was {%" PRIu32 ", %" PRIu32 ", %" PRIx32 "}",
499 entry->compressed_length, entry->uncompressed_length, entry->crc32,
500 descriptor->compressed_size, descriptor->uncompressed_size, descriptor->crc32);
501 return kInconsistentInformation;
502 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000503
504 return 0;
505}
506
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800507static int32_t FindEntry(const ZipArchive* archive, std::string_view entryName,
508 const uint64_t nameOffset, ZipEntry* data) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000509 // Recover the start of the central directory entry from the filename
510 // pointer. The filename is the first entry past the fixed-size data,
511 // so we can just subtract back from that.
Elliott Hughes50ef29a2019-06-18 18:23:59 -0700512 const uint8_t* base_ptr = archive->central_directory.GetBasePtr();
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800513 const uint8_t* ptr = base_ptr + nameOffset;
Narayan Kamath926973e2014-06-09 14:18:14 +0100514 ptr -= sizeof(CentralDirectoryRecord);
Narayan Kamath7462f022013-11-21 13:05:04 +0000515
516 // This is the base of our mmapped region, we have to sanity check that
517 // the name that's in the hash table is a pointer to a location within
518 // this mapped region.
Tianjie Xu18c25922016-09-29 15:27:41 -0700519 if (ptr < base_ptr || ptr > base_ptr + archive->central_directory.GetMapLength()) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000520 ALOGW("Zip: Invalid entry pointer");
521 return kInvalidOffset;
522 }
523
Jiyong Parkcd997e62017-06-30 17:23:33 +0900524 const CentralDirectoryRecord* cdr = reinterpret_cast<const CentralDirectoryRecord*>(ptr);
Narayan Kamath926973e2014-06-09 14:18:14 +0100525
Narayan Kamath7462f022013-11-21 13:05:04 +0000526 // The offset of the start of the central directory in the zipfile.
527 // We keep this lying around so that we can sanity check all our lengths
528 // and our per-file structures.
529 const off64_t cd_offset = archive->directory_offset;
530
531 // Fill out the compression method, modification time, crc32
532 // and other interesting attributes from the central directory. These
533 // will later be compared against values from the local file header.
Narayan Kamath926973e2014-06-09 14:18:14 +0100534 data->method = cdr->compression_method;
beonit0e99a2f2015-07-18 02:08:16 +0900535 data->mod_time = cdr->last_mod_date << 16 | cdr->last_mod_time;
Narayan Kamath926973e2014-06-09 14:18:14 +0100536 data->crc32 = cdr->crc32;
537 data->compressed_length = cdr->compressed_size;
538 data->uncompressed_length = cdr->uncompressed_size;
Narayan Kamath7462f022013-11-21 13:05:04 +0000539
540 // Figure out the local header offset from the central directory. The
541 // actual file data will begin after the local header and the name /
542 // extra comments.
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700543 off64_t local_header_offset = cdr->local_file_header_offset;
544 // One of the info field is UINT32_MAX, try to parse the real value in the zip64 extended info in
545 // the extra field.
546 if (cdr->uncompressed_size == UINT32_MAX || cdr->compressed_size == UINT32_MAX ||
547 cdr->local_file_header_offset == UINT32_MAX) {
548 ALOGW("Zip: Parsing zip64 local file header isn't supported yet");
549 return kInvalidFile;
550 }
551
Narayan Kamath926973e2014-06-09 14:18:14 +0100552 if (local_header_offset + static_cast<off64_t>(sizeof(LocalFileHeader)) >= cd_offset) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000553 ALOGW("Zip: bad local hdr offset in zip");
554 return kInvalidOffset;
555 }
556
Narayan Kamath926973e2014-06-09 14:18:14 +0100557 uint8_t lfh_buf[sizeof(LocalFileHeader)];
Tianjie Xu18c25922016-09-29 15:27:41 -0700558 if (!archive->mapped_zip.ReadAtOffset(lfh_buf, sizeof(lfh_buf), local_header_offset)) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800559 ALOGW("Zip: failed reading lfh name from offset %" PRId64,
Jiyong Parkcd997e62017-06-30 17:23:33 +0900560 static_cast<int64_t>(local_header_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000561 return kIoError;
562 }
563
Jiyong Parkcd997e62017-06-30 17:23:33 +0900564 const LocalFileHeader* lfh = reinterpret_cast<const LocalFileHeader*>(lfh_buf);
Narayan Kamath926973e2014-06-09 14:18:14 +0100565
566 if (lfh->lfh_signature != LocalFileHeader::kSignature) {
Mark Salyzyn99ef9912014-03-14 14:26:22 -0700567 ALOGW("Zip: didn't find signature at start of lfh, offset=%" PRId64,
Jiyong Parkcd997e62017-06-30 17:23:33 +0900568 static_cast<int64_t>(local_header_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000569 return kInvalidOffset;
570 }
571
572 // Paranoia: Match the values specified in the local file header
573 // to those specified in the central directory.
Adam Lesinskid987c9d2017-04-06 18:55:47 -0700574
Narayan Kamath162b7052017-06-05 13:21:12 +0100575 // Warn if central directory and local file header don't agree on the use
576 // of a trailing Data Descriptor. The reference implementation is inconsistent
577 // and appears to use the LFH value during extraction (unzip) but the CD value
578 // while displayng information about archives (zipinfo). The spec remains
579 // silent on this inconsistency as well.
580 //
581 // For now, always use the version from the LFH but make sure that the values
582 // specified in the central directory match those in the data descriptor.
583 //
584 // NOTE: It's also worth noting that unzip *does* warn about inconsistencies in
585 // bit 11 (EFS: The language encoding flag, marking that filename and comment are
586 // encoded using UTF-8). This implementation does not check for the presence of
587 // that flag and always enforces that entry names are valid UTF-8.
588 if ((lfh->gpb_flags & kGPBDDFlagMask) != (cdr->gpb_flags & kGPBDDFlagMask)) {
589 ALOGW("Zip: gpb flag mismatch at bit 3. expected {%04" PRIx16 "}, was {%04" PRIx16 "}",
Adam Lesinskid987c9d2017-04-06 18:55:47 -0700590 cdr->gpb_flags, lfh->gpb_flags);
Adam Lesinskid987c9d2017-04-06 18:55:47 -0700591 }
592
593 // If there is no trailing data descriptor, verify that the central directory and local file
594 // header agree on the crc, compressed, and uncompressed sizes of the entry.
Narayan Kamath926973e2014-06-09 14:18:14 +0100595 if ((lfh->gpb_flags & kGPBDDFlagMask) == 0) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000596 data->has_data_descriptor = 0;
Jiyong Parkcd997e62017-06-30 17:23:33 +0900597 if (data->compressed_length != lfh->compressed_size ||
598 data->uncompressed_length != lfh->uncompressed_size || data->crc32 != lfh->crc32) {
599 ALOGW("Zip: size/crc32 mismatch. expected {%" PRIu32 ", %" PRIu32 ", %" PRIx32
600 "}, was {%" PRIu32 ", %" PRIu32 ", %" PRIx32 "}",
601 data->compressed_length, data->uncompressed_length, data->crc32, lfh->compressed_size,
602 lfh->uncompressed_size, lfh->crc32);
Narayan Kamath7462f022013-11-21 13:05:04 +0000603 return kInconsistentInformation;
604 }
605 } else {
606 data->has_data_descriptor = 1;
607 }
608
Elliott Hughes55fd2932017-05-28 22:59:04 -0700609 // 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 -0700610 data->version_made_by = cdr->version_made_by;
Elliott Hughesd5095252019-10-28 21:35:52 -0700611 data->external_file_attributes = cdr->external_file_attributes;
Elliott Hughes26724132019-10-25 09:57:58 -0700612 if ((data->version_made_by >> 8) == 3) {
Elliott Hughes55fd2932017-05-28 22:59:04 -0700613 data->unix_mode = (cdr->external_file_attributes >> 16) & 0xffff;
614 } else {
615 data->unix_mode = 0777;
616 }
617
Elliott Hughesd5095252019-10-28 21:35:52 -0700618 // 4.4.4: general purpose bit flags.
619 data->gpbf = lfh->gpb_flags;
620
Elliott Hughes26724132019-10-25 09:57:58 -0700621 // 4.4.14: the lowest bit of the internal file attributes field indicates text.
622 // Currently only needed to implement zipinfo.
623 data->is_text = (cdr->internal_file_attributes & 1);
624
Narayan Kamath7462f022013-11-21 13:05:04 +0000625 // Check that the local file header name matches the declared
626 // name in the central directory.
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800627 CHECK_LE(entryName.size(), UINT16_MAX);
628 auto nameLen = static_cast<uint16_t>(entryName.size());
Elliott Hughes50ef29a2019-06-18 18:23:59 -0700629 if (lfh->file_name_length != nameLen) {
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800630 ALOGW("Zip: lfh name length did not match central directory for %s: %" PRIu16 " %" PRIu16,
631 std::string(entryName).c_str(), lfh->file_name_length, nameLen);
Elliott Hughes50ef29a2019-06-18 18:23:59 -0700632 return kInconsistentInformation;
633 }
634 const off64_t name_offset = local_header_offset + sizeof(LocalFileHeader);
635 if (name_offset + lfh->file_name_length > cd_offset) {
636 ALOGW("Zip: lfh name has invalid declared length");
637 return kInvalidOffset;
638 }
639 std::vector<uint8_t> name_buf(nameLen);
640 if (!archive->mapped_zip.ReadAtOffset(name_buf.data(), nameLen, name_offset)) {
641 ALOGW("Zip: failed reading lfh name from offset %" PRId64, static_cast<int64_t>(name_offset));
642 return kIoError;
643 }
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800644 if (memcmp(entryName.data(), name_buf.data(), nameLen) != 0) {
Elliott Hughes50ef29a2019-06-18 18:23:59 -0700645 ALOGW("Zip: lfh name did not match central directory");
Narayan Kamath7462f022013-11-21 13:05:04 +0000646 return kInconsistentInformation;
647 }
648
Jiyong Parkcd997e62017-06-30 17:23:33 +0900649 const off64_t data_offset = local_header_offset + sizeof(LocalFileHeader) +
650 lfh->file_name_length + lfh->extra_field_length;
Narayan Kamath48953a12014-01-24 12:32:39 +0000651 if (data_offset > cd_offset) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800652 ALOGW("Zip: bad data offset %" PRId64 " in zip", static_cast<int64_t>(data_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000653 return kInvalidOffset;
654 }
655
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800656 if (static_cast<off64_t>(data_offset + data->compressed_length) > cd_offset) {
Mark Salyzyn088bf902014-05-08 16:02:20 -0700657 ALOGW("Zip: bad compressed length in zip (%" PRId64 " + %" PRIu32 " > %" PRId64 ")",
Jiyong Parkcd997e62017-06-30 17:23:33 +0900658 static_cast<int64_t>(data_offset), data->compressed_length,
659 static_cast<int64_t>(cd_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000660 return kInvalidOffset;
661 }
662
663 if (data->method == kCompressStored &&
Jiyong Parkcd997e62017-06-30 17:23:33 +0900664 static_cast<off64_t>(data_offset + data->uncompressed_length) > cd_offset) {
665 ALOGW("Zip: bad uncompressed length in zip (%" PRId64 " + %" PRIu32 " > %" PRId64 ")",
666 static_cast<int64_t>(data_offset), data->uncompressed_length,
667 static_cast<int64_t>(cd_offset));
668 return kInvalidOffset;
Narayan Kamath7462f022013-11-21 13:05:04 +0000669 }
670
671 data->offset = data_offset;
672 return 0;
673}
674
675struct IterationHandle {
Narayan Kamath7462f022013-11-21 13:05:04 +0000676 ZipArchive* archive;
Piotr Jastrzebski8e085362014-08-18 11:37:45 +0100677
Elliott Hughes50ef29a2019-06-18 18:23:59 -0700678 std::string prefix;
679 std::string suffix;
Elliott Hughesa22ac0f2019-05-08 10:44:06 -0700680
681 uint32_t position = 0;
682
Elliott Hughes50ef29a2019-06-18 18:23:59 -0700683 IterationHandle(ZipArchive* archive, std::string_view in_prefix, std::string_view in_suffix)
684 : archive(archive), prefix(in_prefix), suffix(in_suffix) {}
Narayan Kamath7462f022013-11-21 13:05:04 +0000685};
686
Ryan Prichard3673f992018-10-10 22:41:14 -0700687int32_t StartIteration(ZipArchiveHandle archive, void** cookie_ptr,
Elliott Hughesa22ac0f2019-05-08 10:44:06 -0700688 const std::string_view optional_prefix,
689 const std::string_view optional_suffix) {
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800690 if (archive == nullptr || archive->cd_entry_map == nullptr) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000691 ALOGW("Zip: Invalid ZipArchiveHandle");
692 return kInvalidHandle;
693 }
694
Elliott Hughesa22ac0f2019-05-08 10:44:06 -0700695 if (optional_prefix.size() > static_cast<size_t>(UINT16_MAX) ||
696 optional_suffix.size() > static_cast<size_t>(UINT16_MAX)) {
697 ALOGW("Zip: prefix/suffix too long");
698 return kInvalidEntryName;
699 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000700
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800701 archive->cd_entry_map->ResetIteration();
Elliott Hughesa22ac0f2019-05-08 10:44:06 -0700702 *cookie_ptr = new IterationHandle(archive, optional_prefix, optional_suffix);
Narayan Kamath7462f022013-11-21 13:05:04 +0000703 return 0;
704}
705
Piotr Jastrzebski79c8b342014-08-08 14:02:17 +0100706void EndIteration(void* cookie) {
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100707 delete reinterpret_cast<IterationHandle*>(cookie);
Piotr Jastrzebski79c8b342014-08-08 14:02:17 +0100708}
709
Elliott Hughesb17bf522019-05-03 22:38:44 -0700710int32_t FindEntry(const ZipArchiveHandle archive, const std::string_view entryName,
711 ZipEntry* data) {
712 if (entryName.empty() || entryName.size() > static_cast<size_t>(UINT16_MAX)) {
713 ALOGW("Zip: Invalid filename of length %zu", entryName.size());
714 return kInvalidEntryName;
715 }
716
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800717 const auto [result, offset] =
718 archive->cd_entry_map->GetCdEntryOffset(entryName, archive->central_directory.GetBasePtr());
719 if (result != 0) {
Elliott Hughesb17bf522019-05-03 22:38:44 -0700720 ALOGV("Zip: Could not find entry %.*s", static_cast<int>(entryName.size()), entryName.data());
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800721 return static_cast<int32_t>(result); // kEntryNotFound is safe to truncate.
Elliott Hughesb17bf522019-05-03 22:38:44 -0700722 }
Elliott Hughesa5ff19e2019-05-07 09:27:59 -0700723 // We know there are at most hash_table_size entries, safe to truncate.
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800724 return FindEntry(archive, entryName, offset, data);
Elliott Hughesb17bf522019-05-03 22:38:44 -0700725}
726
Elliott Hughese06a8082019-05-22 18:56:41 -0700727int32_t Next(void* cookie, ZipEntry* data, std::string* name) {
Elliott Hughes1e40c302019-06-12 12:12:47 -0700728 std::string_view sv;
729 int32_t result = Next(cookie, data, &sv);
730 if (result == 0 && name) {
731 *name = std::string(sv);
732 }
733 return result;
734}
735
736int32_t Next(void* cookie, ZipEntry* data, std::string_view* name) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800737 IterationHandle* handle = reinterpret_cast<IterationHandle*>(cookie);
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800738 if (handle == nullptr) {
Zimuzo5a503ef2018-09-17 19:49:55 +0100739 ALOGW("Zip: Null ZipArchiveHandle");
Narayan Kamath7462f022013-11-21 13:05:04 +0000740 return kInvalidHandle;
741 }
742
743 ZipArchive* archive = handle->archive;
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800744 if (archive == nullptr || archive->cd_entry_map == nullptr) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000745 ALOGW("Zip: Invalid ZipArchiveHandle");
746 return kInvalidHandle;
747 }
748
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800749 auto entry = archive->cd_entry_map->Next(archive->central_directory.GetBasePtr());
750 while (entry != std::pair<std::string_view, uint64_t>()) {
751 const auto [entry_name, offset] = entry;
752 if (android::base::StartsWith(entry_name, handle->prefix) &&
753 android::base::EndsWith(entry_name, handle->suffix)) {
754 const int error = FindEntry(archive, entry_name, offset, data);
Elliott Hughes50ef29a2019-06-18 18:23:59 -0700755 if (!error && name) {
756 *name = entry_name;
Narayan Kamath7462f022013-11-21 13:05:04 +0000757 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000758 return error;
759 }
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800760 entry = archive->cd_entry_map->Next(archive->central_directory.GetBasePtr());
Narayan Kamath7462f022013-11-21 13:05:04 +0000761 }
762
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800763 archive->cd_entry_map->ResetIteration();
Narayan Kamath7462f022013-11-21 13:05:04 +0000764 return kIterationEnd;
765}
766
Narayan Kamathf899bd52015-04-17 11:53:14 +0100767// A Writer that writes data to a fixed size memory region.
768// The size of the memory region must be equal to the total size of
769// the data appended to it.
Narayan Kamath485b3642017-10-26 14:42:39 +0100770class MemoryWriter : public zip_archive::Writer {
Narayan Kamathf899bd52015-04-17 11:53:14 +0100771 public:
Jiyong Parkcd997e62017-06-30 17:23:33 +0900772 MemoryWriter(uint8_t* buf, size_t size) : Writer(), buf_(buf), size_(size), bytes_written_(0) {}
Narayan Kamathf899bd52015-04-17 11:53:14 +0100773
774 virtual bool Append(uint8_t* buf, size_t buf_size) override {
775 if (bytes_written_ + buf_size > size_) {
Elliott Hughese8f4b142018-10-19 16:09:39 -0700776 ALOGW("Zip: Unexpected size %zu (declared) vs %zu (actual)", size_,
Jiyong Parkcd997e62017-06-30 17:23:33 +0900777 bytes_written_ + buf_size);
Narayan Kamathf899bd52015-04-17 11:53:14 +0100778 return false;
779 }
780
781 memcpy(buf_ + bytes_written_, buf, buf_size);
782 bytes_written_ += buf_size;
783 return true;
784 }
785
786 private:
787 uint8_t* const buf_;
788 const size_t size_;
789 size_t bytes_written_;
790};
791
792// A Writer that appends data to a file |fd| at its current position.
793// The file will be truncated to the end of the written data.
Narayan Kamath485b3642017-10-26 14:42:39 +0100794class FileWriter : public zip_archive::Writer {
Narayan Kamathf899bd52015-04-17 11:53:14 +0100795 public:
Narayan Kamathf899bd52015-04-17 11:53:14 +0100796 // Creates a FileWriter for |fd| and prepare to write |entry| to it,
797 // guaranteeing that the file descriptor is valid and that there's enough
798 // space on the volume to write out the entry completely and that the file
Tao Baoa456c212016-11-15 10:08:07 -0800799 // is truncated to the correct length (no truncation if |fd| references a
800 // block device).
Narayan Kamathf899bd52015-04-17 11:53:14 +0100801 //
802 // Returns a valid FileWriter on success, |nullptr| if an error occurred.
Yurii Zubrytskyi834326c2017-12-20 01:01:01 -0800803 static FileWriter Create(int fd, const ZipEntry* entry) {
Narayan Kamathf899bd52015-04-17 11:53:14 +0100804 const uint32_t declared_length = entry->uncompressed_length;
805 const off64_t current_offset = lseek64(fd, 0, SEEK_CUR);
806 if (current_offset == -1) {
807 ALOGW("Zip: unable to seek to current location on fd %d: %s", fd, strerror(errno));
Yurii Zubrytskyi834326c2017-12-20 01:01:01 -0800808 return FileWriter{};
Narayan Kamathf899bd52015-04-17 11:53:14 +0100809 }
810
Narayan Kamathf899bd52015-04-17 11:53:14 +0100811#if defined(__linux__)
812 if (declared_length > 0) {
813 // Make sure we have enough space on the volume to extract the compressed
814 // entry. Note that the call to ftruncate below will change the file size but
815 // will not allocate space on disk and this call to fallocate will not
816 // change the file size.
Badhri Jagan Sridharana68d0d12015-06-02 14:47:57 -0700817 // Note: fallocate is only supported by the following filesystems -
818 // btrfs, ext4, ocfs2, and xfs. Therefore fallocate might fail with
819 // EOPNOTSUPP error when issued in other filesystems.
820 // Hence, check for the return error code before concluding that the
821 // disk does not have enough space.
Andreas Gampe964b95c2019-04-05 13:48:02 -0700822 long result = TEMP_FAILURE_RETRY(fallocate(fd, 0, current_offset, declared_length));
Badhri Jagan Sridharana68d0d12015-06-02 14:47:57 -0700823 if (result == -1 && errno == ENOSPC) {
Elliott Hughes4089d342017-10-27 14:21:12 -0700824 ALOGW("Zip: unable to allocate %" PRId64 " bytes at offset %" PRId64 ": %s",
Narayan Kamathd5d7abe2016-08-10 12:24:05 +0100825 static_cast<int64_t>(declared_length), static_cast<int64_t>(current_offset),
826 strerror(errno));
Yurii Zubrytskyi834326c2017-12-20 01:01:01 -0800827 return FileWriter{};
Narayan Kamathf899bd52015-04-17 11:53:14 +0100828 }
829 }
830#endif // __linux__
831
Tao Baoa456c212016-11-15 10:08:07 -0800832 struct stat sb;
833 if (fstat(fd, &sb) == -1) {
834 ALOGW("Zip: unable to fstat file: %s", strerror(errno));
Yurii Zubrytskyi834326c2017-12-20 01:01:01 -0800835 return FileWriter{};
Narayan Kamathf899bd52015-04-17 11:53:14 +0100836 }
837
Tao Baoa456c212016-11-15 10:08:07 -0800838 // Block device doesn't support ftruncate(2).
839 if (!S_ISBLK(sb.st_mode)) {
Andreas Gampe964b95c2019-04-05 13:48:02 -0700840 long result = TEMP_FAILURE_RETRY(ftruncate(fd, declared_length + current_offset));
Tao Baoa456c212016-11-15 10:08:07 -0800841 if (result == -1) {
842 ALOGW("Zip: unable to truncate file to %" PRId64 ": %s",
843 static_cast<int64_t>(declared_length + current_offset), strerror(errno));
Yurii Zubrytskyi834326c2017-12-20 01:01:01 -0800844 return FileWriter{};
Tao Baoa456c212016-11-15 10:08:07 -0800845 }
846 }
847
Yurii Zubrytskyi834326c2017-12-20 01:01:01 -0800848 return FileWriter(fd, declared_length);
Narayan Kamathf899bd52015-04-17 11:53:14 +0100849 }
850
Chih-Hung Hsieh747eb142018-09-25 11:16:22 -0700851 FileWriter(FileWriter&& other) noexcept
Yurii Zubrytskyi834326c2017-12-20 01:01:01 -0800852 : fd_(other.fd_),
853 declared_length_(other.declared_length_),
854 total_bytes_written_(other.total_bytes_written_) {
855 other.fd_ = -1;
856 }
857
858 bool IsValid() const { return fd_ != -1; }
859
Narayan Kamathf899bd52015-04-17 11:53:14 +0100860 virtual bool Append(uint8_t* buf, size_t buf_size) override {
861 if (total_bytes_written_ + buf_size > declared_length_) {
Elliott Hughese8f4b142018-10-19 16:09:39 -0700862 ALOGW("Zip: Unexpected size %zu (declared) vs %zu (actual)", declared_length_,
Jiyong Parkcd997e62017-06-30 17:23:33 +0900863 total_bytes_written_ + buf_size);
Narayan Kamathf899bd52015-04-17 11:53:14 +0100864 return false;
865 }
866
Narayan Kamathe97e66e2015-04-27 16:25:53 +0100867 const bool result = android::base::WriteFully(fd_, buf, buf_size);
868 if (result) {
869 total_bytes_written_ += buf_size;
870 } else {
Elliott Hughese8f4b142018-10-19 16:09:39 -0700871 ALOGW("Zip: unable to write %zu bytes to file; %s", buf_size, strerror(errno));
Narayan Kamathf899bd52015-04-17 11:53:14 +0100872 }
873
Narayan Kamathe97e66e2015-04-27 16:25:53 +0100874 return result;
Narayan Kamathf899bd52015-04-17 11:53:14 +0100875 }
Jiyong Parkcd997e62017-06-30 17:23:33 +0900876
Narayan Kamathf899bd52015-04-17 11:53:14 +0100877 private:
Yurii Zubrytskyi834326c2017-12-20 01:01:01 -0800878 explicit FileWriter(const int fd = -1, const size_t declared_length = 0)
Jiyong Parkcd997e62017-06-30 17:23:33 +0900879 : Writer(), fd_(fd), declared_length_(declared_length), total_bytes_written_(0) {}
Narayan Kamathf899bd52015-04-17 11:53:14 +0100880
Yurii Zubrytskyi834326c2017-12-20 01:01:01 -0800881 int fd_;
Narayan Kamathf899bd52015-04-17 11:53:14 +0100882 const size_t declared_length_;
883 size_t total_bytes_written_;
884};
885
Narayan Kamath485b3642017-10-26 14:42:39 +0100886class EntryReader : public zip_archive::Reader {
Narayan Kamath8b8faed2017-10-26 14:08:38 +0100887 public:
888 EntryReader(const MappedZipFile& zip_file, const ZipEntry* entry)
889 : Reader(), zip_file_(zip_file), entry_(entry) {}
890
891 virtual bool ReadAtOffset(uint8_t* buf, size_t len, uint32_t offset) const {
892 return zip_file_.ReadAtOffset(buf, len, entry_->offset + offset);
893 }
894
895 virtual ~EntryReader() {}
896
897 private:
898 const MappedZipFile& zip_file_;
899 const ZipEntry* entry_;
900};
901
Dmitriy Ivanovf94e1592015-03-06 13:27:59 -0800902// This method is using libz macros with old-style-casts
903#pragma GCC diagnostic push
904#pragma GCC diagnostic ignored "-Wold-style-cast"
905static inline int zlib_inflateInit2(z_stream* stream, int window_bits) {
906 return inflateInit2(stream, window_bits);
907}
908#pragma GCC diagnostic pop
909
Narayan Kamath485b3642017-10-26 14:42:39 +0100910namespace zip_archive {
911
912// Moved out of line to avoid -Wweak-vtables.
913Reader::~Reader() {}
914Writer::~Writer() {}
915
916int32_t Inflate(const Reader& reader, const uint32_t compressed_length,
917 const uint32_t uncompressed_length, Writer* writer, uint64_t* crc_out) {
Dmitriy Ivanovedbabfe2015-03-12 09:58:15 -0700918 const size_t kBufSize = 32768;
919 std::vector<uint8_t> read_buf(kBufSize);
920 std::vector<uint8_t> write_buf(kBufSize);
Narayan Kamath7462f022013-11-21 13:05:04 +0000921 z_stream zstream;
922 int zerr;
923
924 /*
925 * Initialize the zlib stream struct.
926 */
927 memset(&zstream, 0, sizeof(zstream));
928 zstream.zalloc = Z_NULL;
929 zstream.zfree = Z_NULL;
930 zstream.opaque = Z_NULL;
931 zstream.next_in = NULL;
932 zstream.avail_in = 0;
Dmitriy Ivanovedbabfe2015-03-12 09:58:15 -0700933 zstream.next_out = &write_buf[0];
Narayan Kamath7462f022013-11-21 13:05:04 +0000934 zstream.avail_out = kBufSize;
935 zstream.data_type = Z_UNKNOWN;
936
937 /*
938 * Use the undocumented "negative window bits" feature to tell zlib
939 * that there's no zlib header waiting for it.
940 */
Dmitriy Ivanovf94e1592015-03-06 13:27:59 -0800941 zerr = zlib_inflateInit2(&zstream, -MAX_WBITS);
Narayan Kamath7462f022013-11-21 13:05:04 +0000942 if (zerr != Z_OK) {
943 if (zerr == Z_VERSION_ERROR) {
Jiyong Parkcd997e62017-06-30 17:23:33 +0900944 ALOGE("Installed zlib is not compatible with linked version (%s)", ZLIB_VERSION);
Narayan Kamath7462f022013-11-21 13:05:04 +0000945 } else {
946 ALOGW("Call to inflateInit2 failed (zerr=%d)", zerr);
947 }
948
949 return kZlibError;
950 }
951
Dmitriy Ivanov1f741e52015-03-06 14:26:37 -0800952 auto zstream_deleter = [](z_stream* stream) {
Jiyong Parkcd997e62017-06-30 17:23:33 +0900953 inflateEnd(stream); /* free up any allocated structures */
Dmitriy Ivanov1f741e52015-03-06 14:26:37 -0800954 };
955
956 std::unique_ptr<z_stream, decltype(zstream_deleter)> zstream_guard(&zstream, zstream_deleter);
957
Narayan Kamath2d1e23f2017-10-30 11:17:28 +0000958 const bool compute_crc = (crc_out != nullptr);
Andreas Gampe964b95c2019-04-05 13:48:02 -0700959 uLong crc = 0;
Narayan Kamath8b8faed2017-10-26 14:08:38 +0100960 uint32_t remaining_bytes = compressed_length;
Narayan Kamath7462f022013-11-21 13:05:04 +0000961 do {
962 /* read as much as we can */
963 if (zstream.avail_in == 0) {
Andreas Gampe964b95c2019-04-05 13:48:02 -0700964 const uint32_t read_size = (remaining_bytes > kBufSize) ? kBufSize : remaining_bytes;
Narayan Kamath8b8faed2017-10-26 14:08:38 +0100965 const uint32_t offset = (compressed_length - remaining_bytes);
Adam Lesinskide117e42017-06-19 10:27:38 -0700966 // Make sure to read at offset to ensure concurrent access to the fd.
Narayan Kamath8b8faed2017-10-26 14:08:38 +0100967 if (!reader.ReadAtOffset(read_buf.data(), read_size, offset)) {
Andreas Gampe964b95c2019-04-05 13:48:02 -0700968 ALOGW("Zip: inflate read failed, getSize = %u: %s", read_size, strerror(errno));
Dmitriy Ivanov1f741e52015-03-06 14:26:37 -0800969 return kIoError;
Narayan Kamath7462f022013-11-21 13:05:04 +0000970 }
971
Narayan Kamath8b8faed2017-10-26 14:08:38 +0100972 remaining_bytes -= read_size;
Narayan Kamath7462f022013-11-21 13:05:04 +0000973
Dmitriy Ivanovedbabfe2015-03-12 09:58:15 -0700974 zstream.next_in = &read_buf[0];
Narayan Kamath8b8faed2017-10-26 14:08:38 +0100975 zstream.avail_in = read_size;
Narayan Kamath7462f022013-11-21 13:05:04 +0000976 }
977
978 /* uncompress the data */
979 zerr = inflate(&zstream, Z_NO_FLUSH);
980 if (zerr != Z_OK && zerr != Z_STREAM_END) {
Jiyong Parkcd997e62017-06-30 17:23:33 +0900981 ALOGW("Zip: inflate zerr=%d (nIn=%p aIn=%u nOut=%p aOut=%u)", zerr, zstream.next_in,
982 zstream.avail_in, zstream.next_out, zstream.avail_out);
Dmitriy Ivanov1f741e52015-03-06 14:26:37 -0800983 return kZlibError;
Narayan Kamath7462f022013-11-21 13:05:04 +0000984 }
985
986 /* write when we're full or when we're done */
Jiyong Parkcd997e62017-06-30 17:23:33 +0900987 if (zstream.avail_out == 0 || (zerr == Z_STREAM_END && zstream.avail_out != kBufSize)) {
Dmitriy Ivanovedbabfe2015-03-12 09:58:15 -0700988 const size_t write_size = zstream.next_out - &write_buf[0];
Narayan Kamathf899bd52015-04-17 11:53:14 +0100989 if (!writer->Append(&write_buf[0], write_size)) {
Narayan Kamath2d1e23f2017-10-30 11:17:28 +0000990 return kIoError;
991 } else if (compute_crc) {
Andreas Gampe964b95c2019-04-05 13:48:02 -0700992 DCHECK_LE(write_size, kBufSize);
993 crc = crc32(crc, &write_buf[0], static_cast<uint32_t>(write_size));
Narayan Kamath7462f022013-11-21 13:05:04 +0000994 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000995
Dmitriy Ivanovedbabfe2015-03-12 09:58:15 -0700996 zstream.next_out = &write_buf[0];
Narayan Kamath7462f022013-11-21 13:05:04 +0000997 zstream.avail_out = kBufSize;
998 }
999 } while (zerr == Z_OK);
1000
Elliott Hughese8f4b142018-10-19 16:09:39 -07001001 CHECK_EQ(zerr, Z_STREAM_END); /* other errors should've been caught */
Narayan Kamath7462f022013-11-21 13:05:04 +00001002
Narayan Kamath162b7052017-06-05 13:21:12 +01001003 // NOTE: zstream.adler is always set to 0, because we're using the -MAX_WBITS
1004 // "feature" of zlib to tell it there won't be a zlib file header. zlib
1005 // doesn't bother calculating the checksum in that scenario. We just do
1006 // it ourselves above because there are no additional gains to be made by
1007 // having zlib calculate it for us, since they do it by calling crc32 in
1008 // the same manner that we have above.
Narayan Kamath2d1e23f2017-10-30 11:17:28 +00001009 if (compute_crc) {
1010 *crc_out = crc;
1011 }
Narayan Kamath7462f022013-11-21 13:05:04 +00001012
Narayan Kamath8b8faed2017-10-26 14:08:38 +01001013 if (zstream.total_out != uncompressed_length || remaining_bytes != 0) {
Jiyong Parkcd997e62017-06-30 17:23:33 +09001014 ALOGW("Zip: size mismatch on inflated file (%lu vs %" PRIu32 ")", zstream.total_out,
1015 uncompressed_length);
Dmitriy Ivanov1f741e52015-03-06 14:26:37 -08001016 return kInconsistentInformation;
Narayan Kamath7462f022013-11-21 13:05:04 +00001017 }
1018
Dmitriy Ivanov1f741e52015-03-06 14:26:37 -08001019 return 0;
Narayan Kamath7462f022013-11-21 13:05:04 +00001020}
Narayan Kamath485b3642017-10-26 14:42:39 +01001021} // namespace zip_archive
Narayan Kamath7462f022013-11-21 13:05:04 +00001022
Narayan Kamath8b8faed2017-10-26 14:08:38 +01001023static int32_t InflateEntryToWriter(MappedZipFile& mapped_zip, const ZipEntry* entry,
Narayan Kamath485b3642017-10-26 14:42:39 +01001024 zip_archive::Writer* writer, uint64_t* crc_out) {
Narayan Kamath8b8faed2017-10-26 14:08:38 +01001025 const EntryReader reader(mapped_zip, entry);
1026
Narayan Kamath485b3642017-10-26 14:42:39 +01001027 return zip_archive::Inflate(reader, entry->compressed_length, entry->uncompressed_length, writer,
1028 crc_out);
Narayan Kamath8b8faed2017-10-26 14:08:38 +01001029}
1030
Narayan Kamath485b3642017-10-26 14:42:39 +01001031static int32_t CopyEntryToWriter(MappedZipFile& mapped_zip, const ZipEntry* entry,
1032 zip_archive::Writer* writer, uint64_t* crc_out) {
Narayan Kamathf899bd52015-04-17 11:53:14 +01001033 static const uint32_t kBufSize = 32768;
1034 std::vector<uint8_t> buf(kBufSize);
1035
1036 const uint32_t length = entry->uncompressed_length;
1037 uint32_t count = 0;
Andreas Gampe964b95c2019-04-05 13:48:02 -07001038 uLong crc = 0;
Narayan Kamathf899bd52015-04-17 11:53:14 +01001039 while (count < length) {
1040 uint32_t remaining = length - count;
Adam Lesinskide117e42017-06-19 10:27:38 -07001041 off64_t offset = entry->offset + count;
Narayan Kamathf899bd52015-04-17 11:53:14 +01001042
Adam Lesinskide117e42017-06-19 10:27:38 -07001043 // Safe conversion because kBufSize is narrow enough for a 32 bit signed value.
Andreas Gampe964b95c2019-04-05 13:48:02 -07001044 const uint32_t block_size = (remaining > kBufSize) ? kBufSize : remaining;
Adam Lesinskide117e42017-06-19 10:27:38 -07001045
1046 // Make sure to read at offset to ensure concurrent access to the fd.
1047 if (!mapped_zip.ReadAtOffset(buf.data(), block_size, offset)) {
Andreas Gampe964b95c2019-04-05 13:48:02 -07001048 ALOGW("CopyFileToFile: copy read failed, block_size = %u, offset = %" PRId64 ": %s",
Adam Lesinskide117e42017-06-19 10:27:38 -07001049 block_size, static_cast<int64_t>(offset), strerror(errno));
Narayan Kamathf899bd52015-04-17 11:53:14 +01001050 return kIoError;
1051 }
1052
1053 if (!writer->Append(&buf[0], block_size)) {
1054 return kIoError;
1055 }
1056 crc = crc32(crc, &buf[0], block_size);
1057 count += block_size;
1058 }
1059
1060 *crc_out = crc;
1061
1062 return 0;
1063}
1064
Ryan Prichard3673f992018-10-10 22:41:14 -07001065int32_t ExtractToWriter(ZipArchiveHandle archive, ZipEntry* entry, zip_archive::Writer* writer) {
Narayan Kamath7462f022013-11-21 13:05:04 +00001066 const uint16_t method = entry->method;
Narayan Kamath7462f022013-11-21 13:05:04 +00001067
1068 // this should default to kUnknownCompressionMethod.
1069 int32_t return_value = -1;
1070 uint64_t crc = 0;
1071 if (method == kCompressStored) {
Tianjie Xu18c25922016-09-29 15:27:41 -07001072 return_value = CopyEntryToWriter(archive->mapped_zip, entry, writer, &crc);
Narayan Kamath7462f022013-11-21 13:05:04 +00001073 } else if (method == kCompressDeflated) {
Tianjie Xu18c25922016-09-29 15:27:41 -07001074 return_value = InflateEntryToWriter(archive->mapped_zip, entry, writer, &crc);
Narayan Kamath7462f022013-11-21 13:05:04 +00001075 }
1076
1077 if (!return_value && entry->has_data_descriptor) {
Narayan Kamath162b7052017-06-05 13:21:12 +01001078 return_value = ValidateDataDescriptor(archive->mapped_zip, entry);
Narayan Kamath7462f022013-11-21 13:05:04 +00001079 if (return_value) {
1080 return return_value;
1081 }
1082 }
1083
Narayan Kamath162b7052017-06-05 13:21:12 +01001084 // Validate that the CRC matches the calculated value.
1085 if (kCrcChecksEnabled && (entry->crc32 != static_cast<uint32_t>(crc))) {
Mark Salyzyn088bf902014-05-08 16:02:20 -07001086 ALOGW("Zip: crc mismatch: expected %" PRIu32 ", was %" PRIu64, entry->crc32, crc);
Narayan Kamath7462f022013-11-21 13:05:04 +00001087 return kInconsistentInformation;
1088 }
1089
1090 return return_value;
1091}
1092
Ryan Prichard3673f992018-10-10 22:41:14 -07001093int32_t ExtractToMemory(ZipArchiveHandle archive, ZipEntry* entry, uint8_t* begin, uint32_t size) {
Yurii Zubrytskyi834326c2017-12-20 01:01:01 -08001094 MemoryWriter writer(begin, size);
Ryan Prichard3673f992018-10-10 22:41:14 -07001095 return ExtractToWriter(archive, entry, &writer);
Narayan Kamathf899bd52015-04-17 11:53:14 +01001096}
1097
Ryan Prichard3673f992018-10-10 22:41:14 -07001098int32_t ExtractEntryToFile(ZipArchiveHandle archive, ZipEntry* entry, int fd) {
Yurii Zubrytskyi834326c2017-12-20 01:01:01 -08001099 auto writer = FileWriter::Create(fd, entry);
1100 if (!writer.IsValid()) {
Narayan Kamath7462f022013-11-21 13:05:04 +00001101 return kIoError;
1102 }
1103
Ryan Prichard3673f992018-10-10 22:41:14 -07001104 return ExtractToWriter(archive, entry, &writer);
Narayan Kamath7462f022013-11-21 13:05:04 +00001105}
1106
Ryan Prichard3673f992018-10-10 22:41:14 -07001107int GetFileDescriptor(const ZipArchiveHandle archive) {
1108 return archive->mapped_zip.GetFileDescriptor();
Narayan Kamath7462f022013-11-21 13:05:04 +00001109}
Colin Cross7c6c7f02016-09-16 10:15:51 -07001110
Ryan Mitchell23150e42020-03-09 09:33:46 -07001111off64_t GetFileDescriptorOffset(const ZipArchiveHandle archive) {
1112 return archive->mapped_zip.GetFileOffset();
1113}
1114
Tianjie Xu18c25922016-09-29 15:27:41 -07001115#if !defined(_WIN32)
Narayan Kamath485b3642017-10-26 14:42:39 +01001116class ProcessWriter : public zip_archive::Writer {
Tianjie Xu18c25922016-09-29 15:27:41 -07001117 public:
Jiyong Parkcd997e62017-06-30 17:23:33 +09001118 ProcessWriter(ProcessZipEntryFunction func, void* cookie)
1119 : Writer(), proc_function_(func), cookie_(cookie) {}
Tianjie Xu18c25922016-09-29 15:27:41 -07001120
1121 virtual bool Append(uint8_t* buf, size_t buf_size) override {
1122 return proc_function_(buf, buf_size, cookie_);
1123 }
1124
1125 private:
1126 ProcessZipEntryFunction proc_function_;
1127 void* cookie_;
1128};
1129
Ryan Prichard3673f992018-10-10 22:41:14 -07001130int32_t ProcessZipEntryContents(ZipArchiveHandle archive, ZipEntry* entry,
Tianjie Xu18c25922016-09-29 15:27:41 -07001131 ProcessZipEntryFunction func, void* cookie) {
1132 ProcessWriter writer(func, cookie);
Ryan Prichard3673f992018-10-10 22:41:14 -07001133 return ExtractToWriter(archive, entry, &writer);
Tianjie Xu18c25922016-09-29 15:27:41 -07001134}
1135
Jiyong Parkcd997e62017-06-30 17:23:33 +09001136#endif //! defined(_WIN32)
Tianjie Xu18c25922016-09-29 15:27:41 -07001137
1138int MappedZipFile::GetFileDescriptor() const {
1139 if (!has_fd_) {
1140 ALOGW("Zip: MappedZipFile doesn't have a file descriptor.");
1141 return -1;
1142 }
1143 return fd_;
1144}
1145
Elliott Hughesf66460b2019-10-22 11:44:50 -07001146const void* MappedZipFile::GetBasePtr() const {
Tianjie Xu18c25922016-09-29 15:27:41 -07001147 if (has_fd_) {
1148 ALOGW("Zip: MappedZipFile doesn't have a base pointer.");
1149 return nullptr;
1150 }
1151 return base_ptr_;
1152}
1153
Ryan Mitchell23150e42020-03-09 09:33:46 -07001154off64_t MappedZipFile::GetFileOffset() const {
1155 return fd_offset_;
1156}
1157
Tianjie Xu18c25922016-09-29 15:27:41 -07001158off64_t MappedZipFile::GetFileLength() const {
1159 if (has_fd_) {
Ryan Mitchell23150e42020-03-09 09:33:46 -07001160 if (data_length_ != -1) {
1161 return data_length_;
1162 }
1163 data_length_ = lseek64(fd_, 0, SEEK_END);
1164 if (data_length_ == -1) {
Tianjie Xu18c25922016-09-29 15:27:41 -07001165 ALOGE("Zip: lseek on fd %d failed: %s", fd_, strerror(errno));
1166 }
Ryan Mitchell23150e42020-03-09 09:33:46 -07001167 return data_length_;
Tianjie Xu18c25922016-09-29 15:27:41 -07001168 } else {
1169 if (base_ptr_ == nullptr) {
Elliott Hughesfba2a1a2019-12-16 16:16:16 -08001170 ALOGE("Zip: invalid file map");
Tianjie Xu18c25922016-09-29 15:27:41 -07001171 return -1;
1172 }
Ryan Mitchell23150e42020-03-09 09:33:46 -07001173 return data_length_;
Tianjie Xu18c25922016-09-29 15:27:41 -07001174 }
1175}
1176
Tianjie Xu18c25922016-09-29 15:27:41 -07001177// Attempts to read |len| bytes into |buf| at offset |off|.
Narayan Kamath8b8faed2017-10-26 14:08:38 +01001178bool MappedZipFile::ReadAtOffset(uint8_t* buf, size_t len, off64_t off) const {
Tianjie Xu18c25922016-09-29 15:27:41 -07001179 if (has_fd_) {
Ryan Mitchell23150e42020-03-09 09:33:46 -07001180 if (off < 0) {
1181 ALOGE("Zip: invalid offset %" PRId64, off);
1182 return false;
1183 }
1184
1185 off64_t read_offset;
1186 if (__builtin_add_overflow(fd_offset_, off, &read_offset)) {
1187 ALOGE("Zip: invalid read offset %" PRId64 " overflows, fd offset %" PRId64, off, fd_offset_);
1188 return false;
1189 }
1190
1191 if (data_length_ != -1) {
1192 off64_t read_end;
1193 if (len > std::numeric_limits<off64_t>::max() ||
1194 __builtin_add_overflow(off, static_cast<off64_t>(len), &read_end)) {
1195 ALOGE("Zip: invalid read length %" PRId64 " overflows, offset %" PRId64,
1196 static_cast<off64_t>(len), off);
1197 return false;
1198 }
1199
1200 if (read_end > data_length_) {
1201 ALOGE("Zip: invalid read length %" PRId64 " exceeds data length %" PRId64 ", offset %"
1202 PRId64, static_cast<off64_t>(len), data_length_, off);
1203 return false;
1204 }
1205 }
1206
1207 if (!android::base::ReadFullyAtOffset(fd_, buf, len, read_offset)) {
Elliott Hughesfba2a1a2019-12-16 16:16:16 -08001208 ALOGE("Zip: failed to read at offset %" PRId64, off);
Tianjie Xu18c25922016-09-29 15:27:41 -07001209 return false;
1210 }
Adam Lesinskide117e42017-06-19 10:27:38 -07001211 } else {
Ryan Mitchell23150e42020-03-09 09:33:46 -07001212 if (off < 0 || off > data_length_) {
Elliott Hughesfba2a1a2019-12-16 16:16:16 -08001213 ALOGE("Zip: invalid offset: %" PRId64 ", data length: %" PRId64, off, data_length_);
Adam Lesinskide117e42017-06-19 10:27:38 -07001214 return false;
1215 }
Elliott Hughesf66460b2019-10-22 11:44:50 -07001216 memcpy(buf, static_cast<const uint8_t*>(base_ptr_) + off, len);
Tianjie Xu18c25922016-09-29 15:27:41 -07001217 }
Adam Lesinskide117e42017-06-19 10:27:38 -07001218 return true;
Tianjie Xu18c25922016-09-29 15:27:41 -07001219}
1220
Elliott Hughesf66460b2019-10-22 11:44:50 -07001221void CentralDirectory::Initialize(const void* map_base_ptr, off64_t cd_start_offset,
1222 size_t cd_size) {
1223 base_ptr_ = static_cast<const uint8_t*>(map_base_ptr) + cd_start_offset;
Tianjie Xu18c25922016-09-29 15:27:41 -07001224 length_ = cd_size;
1225}
1226
Elliott Hughese8f4b142018-10-19 16:09:39 -07001227bool ZipArchive::InitializeCentralDirectory(off64_t cd_start_offset, size_t cd_size) {
Tianjie Xu18c25922016-09-29 15:27:41 -07001228 if (mapped_zip.HasFd()) {
Elliott Hughese8f4b142018-10-19 16:09:39 -07001229 directory_map = android::base::MappedFile::FromFd(mapped_zip.GetFileDescriptor(),
Ryan Mitchell23150e42020-03-09 09:33:46 -07001230 mapped_zip.GetFileOffset() + cd_start_offset,
1231 cd_size, PROT_READ);
Elliott Hughesfba2a1a2019-12-16 16:16:16 -08001232 if (!directory_map) {
1233 ALOGE("Zip: failed to map central directory (offset %" PRId64 ", size %zu): %s",
1234 cd_start_offset, cd_size, strerror(errno));
1235 return false;
1236 }
Tianjie Xu18c25922016-09-29 15:27:41 -07001237
Elliott Hughese8f4b142018-10-19 16:09:39 -07001238 CHECK_EQ(directory_map->size(), cd_size);
1239 central_directory.Initialize(directory_map->data(), 0 /*offset*/, cd_size);
Tianjie Xu18c25922016-09-29 15:27:41 -07001240 } else {
1241 if (mapped_zip.GetBasePtr() == nullptr) {
Elliott Hughesfba2a1a2019-12-16 16:16:16 -08001242 ALOGE("Zip: Failed to map central directory, bad mapped_zip base pointer");
Tianjie Xu18c25922016-09-29 15:27:41 -07001243 return false;
1244 }
1245 if (static_cast<off64_t>(cd_start_offset) + static_cast<off64_t>(cd_size) >
1246 mapped_zip.GetFileLength()) {
Jiyong Parkcd997e62017-06-30 17:23:33 +09001247 ALOGE(
1248 "Zip: Failed to map central directory, offset exceeds mapped memory region ("
1249 "start_offset %" PRId64 ", cd_size %zu, mapped_region_size %" PRId64 ")",
1250 static_cast<int64_t>(cd_start_offset), cd_size, mapped_zip.GetFileLength());
Tianjie Xu18c25922016-09-29 15:27:41 -07001251 return false;
1252 }
1253
1254 central_directory.Initialize(mapped_zip.GetBasePtr(), cd_start_offset, cd_size);
1255 }
1256 return true;
1257}
Elliott Hughes55fd2932017-05-28 22:59:04 -07001258
1259tm ZipEntry::GetModificationTime() const {
1260 tm t = {};
1261
1262 t.tm_hour = (mod_time >> 11) & 0x1f;
1263 t.tm_min = (mod_time >> 5) & 0x3f;
1264 t.tm_sec = (mod_time & 0x1f) << 1;
1265
1266 t.tm_year = ((mod_time >> 25) & 0x7f) + 80;
1267 t.tm_mon = ((mod_time >> 21) & 0xf) - 1;
1268 t.tm_mday = (mod_time >> 16) & 0x1f;
1269
1270 return t;
1271}