blob: 031d43a4c64ee8787cd57c27f201d0edf722ba45 [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.
62static const bool kCrcChecksEnabled = false;
63
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
Tianjie6ab29122020-03-18 17:44:30 -0700140static ZipError FindCentralDirectoryInfoForZip64(const char* debugFileName, ZipArchive* archive,
141 off64_t eocdOffset, CentralDirectoryInfo* cdInfo) {
142 if (eocdOffset <= sizeof(Zip64EocdLocator)) {
143 ALOGW("Zip: %s: Not enough space for zip64 eocd locator", debugFileName);
144 return kInvalidFile;
145 }
146 // We expect to find the zip64 eocd locator immediately before the zip eocd.
147 const int64_t locatorOffset = eocdOffset - sizeof(Zip64EocdLocator);
148 Zip64EocdLocator zip64EocdLocator{};
149 if (!archive->mapped_zip.ReadAtOffset(reinterpret_cast<uint8_t*>((&zip64EocdLocator)),
150 sizeof(Zip64EocdLocator), locatorOffset)) {
151 ALOGW("Zip: %s: Read %zu from offset %" PRId64 " failed %s", debugFileName,
152 sizeof(Zip64EocdLocator), locatorOffset, debugFileName);
153 return kIoError;
154 }
155
156 if (zip64EocdLocator.locator_signature != Zip64EocdLocator::kSignature) {
157 ALOGW("Zip: %s: Zip64 eocd locator signature not found at offset %" PRId64, debugFileName,
158 locatorOffset);
159 return kInvalidFile;
160 }
161
162 const int64_t zip64EocdOffset = zip64EocdLocator.zip64_eocd_offset;
Tianjie173aba02020-03-28 18:28:43 -0700163 if (locatorOffset <= sizeof(Zip64EocdRecord) ||
164 zip64EocdOffset > locatorOffset - sizeof(Zip64EocdRecord)) {
165 ALOGW("Zip: %s: Bad zip64 eocd offset %" PRId64 ", eocd locator offset %" PRId64, debugFileName,
166 zip64EocdOffset, locatorOffset);
Tianjie6ab29122020-03-18 17:44:30 -0700167 return kInvalidOffset;
168 }
169
170 Zip64EocdRecord zip64EocdRecord{};
171 if (!archive->mapped_zip.ReadAtOffset(reinterpret_cast<uint8_t*>(&zip64EocdRecord),
172 sizeof(Zip64EocdRecord), zip64EocdOffset)) {
173 ALOGW("Zip: %s: read %zu from offset %" PRId64 " failed %s", debugFileName,
Tianjie173aba02020-03-28 18:28:43 -0700174 sizeof(Zip64EocdLocator), zip64EocdOffset, debugFileName);
Tianjie6ab29122020-03-18 17:44:30 -0700175 return kIoError;
176 }
177
178 if (zip64EocdRecord.record_signature != Zip64EocdRecord::kSignature) {
179 ALOGW("Zip: %s: Zip64 eocd record signature not found at offset %" PRId64, debugFileName,
180 zip64EocdOffset);
181 return kInvalidFile;
182 }
183
Tianjie173aba02020-03-28 18:28:43 -0700184 if (zip64EocdOffset <= zip64EocdRecord.cd_size ||
185 zip64EocdRecord.cd_start_offset > zip64EocdOffset - zip64EocdRecord.cd_size) {
Tianjie6ab29122020-03-18 17:44:30 -0700186 ALOGW("Zip: %s: Bad offset for zip64 central directory. cd offset %" PRIu64 ", cd size %" PRIu64
187 ", zip64 eocd offset %" PRIu64,
188 debugFileName, zip64EocdRecord.cd_start_offset, zip64EocdRecord.cd_size, zip64EocdOffset);
189 return kInvalidOffset;
190 }
191
192 *cdInfo = {.num_records = zip64EocdRecord.num_records,
193 .cd_size = zip64EocdRecord.cd_size,
194 .cd_start_offset = zip64EocdRecord.cd_start_offset};
195
196 return kSuccess;
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700197}
198
199static ZipError FindCentralDirectoryInfo(const char* debug_file_name, ZipArchive* archive,
200 off64_t file_length, uint32_t read_amount,
201 CentralDirectoryInfo* cdInfo) {
202 std::vector<uint8_t> scan_buffer(read_amount);
Narayan Kamath7462f022013-11-21 13:05:04 +0000203 const off64_t search_start = file_length - read_amount;
204
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700205 if (!archive->mapped_zip.ReadAtOffset(scan_buffer.data(), read_amount, search_start)) {
Jiyong Parkcd997e62017-06-30 17:23:33 +0900206 ALOGE("Zip: read %" PRId64 " from offset %" PRId64 " failed", static_cast<int64_t>(read_amount),
207 static_cast<int64_t>(search_start));
Narayan Kamath7462f022013-11-21 13:05:04 +0000208 return kIoError;
209 }
210
211 /*
212 * Scan backward for the EOCD magic. In an archive without a trailing
213 * comment, we'll find it on the first try. (We may want to consider
214 * doing an initial minimal read; if we don't find it, retry with a
215 * second read as above.)
216 */
Andreas Gampe964b95c2019-04-05 13:48:02 -0700217 CHECK_LE(read_amount, std::numeric_limits<int32_t>::max());
218 int32_t i = read_amount - sizeof(EocdRecord);
Narayan Kamath926973e2014-06-09 14:18:14 +0100219 for (; i >= 0; i--) {
Dan Albert1ae07642015-04-09 14:11:18 -0700220 if (scan_buffer[i] == 0x50) {
221 uint32_t* sig_addr = reinterpret_cast<uint32_t*>(&scan_buffer[i]);
Tianjie0ec0eaa2020-03-26 12:34:44 -0700222 if (android::base::get_unaligned<uint32_t>(sig_addr) == EocdRecord::kSignature) {
Dan Albert1ae07642015-04-09 14:11:18 -0700223 ALOGV("+++ Found EOCD at buf+%d", i);
224 break;
225 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000226 }
227 }
228 if (i < 0) {
229 ALOGD("Zip: EOCD not found, %s is not zip", debug_file_name);
230 return kInvalidFile;
231 }
232
233 const off64_t eocd_offset = search_start + i;
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700234 auto eocd = reinterpret_cast<const EocdRecord*>(scan_buffer.data() + i);
Narayan Kamath7462f022013-11-21 13:05:04 +0000235 /*
Narayan Kamath926973e2014-06-09 14:18:14 +0100236 * Verify that there's no trailing space at the end of the central directory
237 * and its comment.
Narayan Kamath7462f022013-11-21 13:05:04 +0000238 */
Jiyong Parkcd997e62017-06-30 17:23:33 +0900239 const off64_t calculated_length = eocd_offset + sizeof(EocdRecord) + eocd->comment_length;
Narayan Kamath926973e2014-06-09 14:18:14 +0100240 if (calculated_length != file_length) {
Narayan Kamath4f6b4992014-06-03 13:59:23 +0100241 ALOGW("Zip: %" PRId64 " extraneous bytes at the end of the central directory",
Narayan Kamath926973e2014-06-09 14:18:14 +0100242 static_cast<int64_t>(file_length - calculated_length));
Narayan Kamath4f6b4992014-06-03 13:59:23 +0100243 return kInvalidFile;
244 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000245
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700246 // One of the field is 0xFFFFFFFF, look for the zip64 EOCD instead.
247 if (eocd->cd_size == UINT32_MAX || eocd->cd_start_offset == UINT32_MAX) {
248 ALOGV("Looking for the zip64 EOCD, cd_size: %" PRIu32 "cd_start_offset: %" PRId32,
249 eocd->cd_size, eocd->cd_start_offset);
Tianjie6ab29122020-03-18 17:44:30 -0700250 return FindCentralDirectoryInfoForZip64(debug_file_name, archive, eocd_offset, cdInfo);
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700251 }
252
Narayan Kamath926973e2014-06-09 14:18:14 +0100253 /*
254 * Grab the CD offset and size, and the number of entries in the
255 * archive and verify that they look reasonable.
256 */
Tianjie Xu1ee48922016-09-21 14:58:11 -0700257 if (static_cast<off64_t>(eocd->cd_start_offset) + eocd->cd_size > eocd_offset) {
Narayan Kamath926973e2014-06-09 14:18:14 +0100258 ALOGW("Zip: bad offsets (dir %" PRIu32 ", size %" PRIu32 ", eocd %" PRId64 ")",
Jiyong Parkcd997e62017-06-30 17:23:33 +0900259 eocd->cd_start_offset, eocd->cd_size, static_cast<int64_t>(eocd_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000260 return kInvalidOffset;
261 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000262
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700263 *cdInfo = {.num_records = eocd->num_records,
264 .cd_size = eocd->cd_size,
265 .cd_start_offset = eocd->cd_start_offset};
266 return kSuccess;
Narayan Kamath7462f022013-11-21 13:05:04 +0000267}
268
269/*
270 * Find the zip Central Directory and memory-map it.
271 *
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700272 * On success, returns kSuccess after populating fields from the EOCD area:
Narayan Kamath7462f022013-11-21 13:05:04 +0000273 * directory_offset
Tianjie Xu18c25922016-09-29 15:27:41 -0700274 * directory_ptr
Narayan Kamath7462f022013-11-21 13:05:04 +0000275 * num_entries
276 */
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700277static ZipError MapCentralDirectory(const char* debug_file_name, ZipArchive* archive) {
278 // 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 -0700279 off64_t file_length = archive->mapped_zip.GetFileLength();
Narayan Kamath7462f022013-11-21 13:05:04 +0000280 if (file_length == -1) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000281 return kInvalidFile;
282 }
283
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700284 if (file_length > kMaxFileLength) {
Narayan Kamath926973e2014-06-09 14:18:14 +0100285 ALOGV("Zip: zip file too long %" PRId64, static_cast<int64_t>(file_length));
Narayan Kamath7462f022013-11-21 13:05:04 +0000286 return kInvalidFile;
287 }
288
Narayan Kamath926973e2014-06-09 14:18:14 +0100289 if (file_length < static_cast<off64_t>(sizeof(EocdRecord))) {
290 ALOGV("Zip: length %" PRId64 " is too small to be zip", static_cast<int64_t>(file_length));
Narayan Kamath7462f022013-11-21 13:05:04 +0000291 return kInvalidFile;
292 }
293
294 /*
295 * Perform the traditional EOCD snipe hunt.
296 *
297 * We're searching for the End of Central Directory magic number,
298 * which appears at the start of the EOCD block. It's followed by
299 * 18 bytes of EOCD stuff and up to 64KB of archive comment. We
300 * need to read the last part of the file into a buffer, dig through
301 * it to find the magic number, parse some values out, and use those
302 * to determine the extent of the CD.
303 *
304 * We start by pulling in the last part of the file.
305 */
Andreas Gampe964b95c2019-04-05 13:48:02 -0700306 uint32_t read_amount = kMaxEOCDSearch;
Narayan Kamath926973e2014-06-09 14:18:14 +0100307 if (file_length < read_amount) {
Andreas Gampe964b95c2019-04-05 13:48:02 -0700308 read_amount = static_cast<uint32_t>(file_length);
Narayan Kamath7462f022013-11-21 13:05:04 +0000309 }
310
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700311 CentralDirectoryInfo cdInfo = {};
312 if (auto result =
313 FindCentralDirectoryInfo(debug_file_name, archive, file_length, read_amount, &cdInfo);
314 result != kSuccess) {
315 return result;
316 }
317
318 if (cdInfo.num_records == 0) {
319#if defined(__ANDROID__)
320 ALOGW("Zip: empty archive?");
321#endif
322 return kEmptyArchive;
323 }
324
325 if (cdInfo.cd_size >= SIZE_MAX) {
326 ALOGW("Zip: The size of central directory doesn't fit in range of size_t: %" PRIu64,
327 cdInfo.cd_size);
328 return kInvalidFile;
329 }
330
331 ALOGV("+++ num_entries=%" PRIu64 " dir_size=%" PRIu64 " dir_offset=%" PRIu64, cdInfo.num_records,
332 cdInfo.cd_size, cdInfo.cd_start_offset);
333
334 // It all looks good. Create a mapping for the CD, and set the fields in archive.
335 if (!archive->InitializeCentralDirectory(static_cast<off64_t>(cdInfo.cd_start_offset),
336 static_cast<size_t>(cdInfo.cd_size))) {
337 return kMmapFailed;
338 }
339
340 archive->num_entries = cdInfo.num_records;
341 archive->directory_offset = cdInfo.cd_start_offset;
342
343 return kSuccess;
Narayan Kamath7462f022013-11-21 13:05:04 +0000344}
345
Tianjie6ab29122020-03-18 17:44:30 -0700346static ZipError ParseZip64ExtendedInfoInExtraField(
347 const uint8_t* extraFieldStart, uint16_t extraFieldLength, uint32_t zip32UncompressedSize,
348 uint32_t zip32CompressedSize, std::optional<uint32_t> zip32LocalFileHeaderOffset,
349 Zip64ExtendedInfo* zip64Info) {
350 if (extraFieldLength <= 4) {
351 ALOGW("Zip: Extra field isn't large enough to hold zip64 info, size %" PRIu16,
352 extraFieldLength);
353 return kInvalidFile;
354 }
355
356 // Each header MUST consist of:
357 // Header ID - 2 bytes
358 // Data Size - 2 bytes
359 uint16_t offset = 0;
360 while (offset < extraFieldLength - 4) {
Tianjie0ec0eaa2020-03-26 12:34:44 -0700361 auto readPtr = const_cast<uint8_t*>(extraFieldStart + offset);
362 auto headerId = ConsumeUnaligned<uint16_t>(&readPtr);
363 auto dataSize = ConsumeUnaligned<uint16_t>(&readPtr);
Tianjie6ab29122020-03-18 17:44:30 -0700364
365 offset += 4;
366 if (dataSize > extraFieldLength - offset) {
367 ALOGW("Zip: Data size exceeds the boundary of extra field, data size %" PRIu16, dataSize);
368 return kInvalidOffset;
369 }
370
371 // Skip the other types of extensible data fields. Details in
372 // https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT section 4.5
373 if (headerId != Zip64ExtendedInfo::kHeaderId) {
374 offset += dataSize;
375 continue;
376 }
377
Tianjie0ec0eaa2020-03-26 12:34:44 -0700378 std::optional<uint64_t> uncompressedFileSize;
379 std::optional<uint64_t> compressedFileSize;
380 std::optional<uint64_t> localHeaderOffset;
381 if (zip32UncompressedSize == UINT32_MAX) {
382 uncompressedFileSize = ConsumeUnaligned<uint64_t>(&readPtr);
383 }
384 if (zip32CompressedSize == UINT32_MAX) {
385 compressedFileSize = ConsumeUnaligned<uint64_t>(&readPtr);
Tianjie6ab29122020-03-18 17:44:30 -0700386 }
387 if (zip32LocalFileHeaderOffset == UINT32_MAX) {
Tianjie0ec0eaa2020-03-26 12:34:44 -0700388 localHeaderOffset = ConsumeUnaligned<uint64_t>(&readPtr);
Tianjie6ab29122020-03-18 17:44:30 -0700389 }
390
Tianjie0ec0eaa2020-03-26 12:34:44 -0700391 // calculate how many bytes we read after the data size field.
392 size_t bytesRead = readPtr - (extraFieldStart + offset);
393 if (bytesRead == 0) {
Tianjie6ab29122020-03-18 17:44:30 -0700394 ALOGW("Zip: Data size should not be 0 in zip64 extended field");
395 return kInvalidFile;
396 }
397
Tianjie0ec0eaa2020-03-26 12:34:44 -0700398 if (dataSize != bytesRead) {
Tianjie6ab29122020-03-18 17:44:30 -0700399 auto localOffsetString = zip32LocalFileHeaderOffset.has_value()
400 ? std::to_string(zip32LocalFileHeaderOffset.value())
401 : "missing";
Tianjie0ec0eaa2020-03-26 12:34:44 -0700402 ALOGW("Zip: Invalid data size in zip64 extended field, expect %zu , get %" PRIu16
Tianjie6ab29122020-03-18 17:44:30 -0700403 ", uncompressed size %" PRIu32 ", compressed size %" PRIu32 ", local header offset %s",
Tianjie0ec0eaa2020-03-26 12:34:44 -0700404 bytesRead, dataSize, zip32UncompressedSize, zip32CompressedSize,
Tianjie6ab29122020-03-18 17:44:30 -0700405 localOffsetString.c_str());
406 return kInvalidFile;
407 }
408
Tianjie0ec0eaa2020-03-26 12:34:44 -0700409 // TODO(xunchang) Support handling file large than UINT32_MAX. It's theoretically possible
410 // for libz to (de)compressing file larger than UINT32_MAX. But we should use our own
411 // bytes counter to replace stream.total_out.
412 if ((uncompressedFileSize.has_value() && uncompressedFileSize.value() > UINT32_MAX) ||
413 (compressedFileSize.has_value() && compressedFileSize.value() > UINT32_MAX)) {
414 ALOGW("Zip: File size larger than UINT32_MAX isn't supported yet");
415 return kInvalidFile;
Tianjie6ab29122020-03-18 17:44:30 -0700416 }
417
418 zip64Info->uncompressed_file_size = uncompressedFileSize;
419 zip64Info->compressed_file_size = compressedFileSize;
420 zip64Info->local_header_offset = localHeaderOffset;
421 return kSuccess;
422 }
423
424 ALOGW("Zip: zip64 extended info isn't found in the extra field.");
425 return kInvalidFile;
426}
427
Narayan Kamath7462f022013-11-21 13:05:04 +0000428/*
429 * Parses the Zip archive's Central Directory. Allocates and populates the
430 * hash table.
431 *
432 * Returns 0 on success.
433 */
Tianjie6ab29122020-03-18 17:44:30 -0700434static ZipError ParseZipArchive(ZipArchive* archive) {
Tianjie Xu18c25922016-09-29 15:27:41 -0700435 const uint8_t* const cd_ptr = archive->central_directory.GetBasePtr();
436 const size_t cd_length = archive->central_directory.GetMapLength();
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700437 const uint64_t num_entries = archive->num_entries;
Narayan Kamath7462f022013-11-21 13:05:04 +0000438
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700439 if (num_entries <= UINT16_MAX) {
440 archive->cd_entry_map = CdEntryMapZip32::Create(static_cast<uint16_t>(num_entries));
Tianjie Xu0ef97832020-03-15 21:23:24 -0700441 } else {
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700442 archive->cd_entry_map = CdEntryMapZip64::Create();
Tianjie Xu0ef97832020-03-15 21:23:24 -0700443 }
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800444 if (archive->cd_entry_map == nullptr) {
Elliott Hughesfba2a1a2019-12-16 16:16:16 -0800445 return kAllocationFailed;
Tianjie Xu9e020e22016-10-10 12:11:30 -0700446 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000447
448 /*
449 * Walk through the central directory, adding entries to the hash
450 * table and verifying values.
451 */
Narayan Kamath926973e2014-06-09 14:18:14 +0100452 const uint8_t* const cd_end = cd_ptr + cd_length;
Narayan Kamath7462f022013-11-21 13:05:04 +0000453 const uint8_t* ptr = cd_ptr;
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700454 for (uint64_t i = 0; i < num_entries; i++) {
Tianjie Xu0fda1cf2017-04-05 14:46:27 -0700455 if (ptr > cd_end - sizeof(CentralDirectoryRecord)) {
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700456 ALOGW("Zip: ran off the end (item #%" PRIu64 ", %zu bytes of central directory)", i,
Elliott Hughesfba2a1a2019-12-16 16:16:16 -0800457 cd_length);
Tianjie Xu0fda1cf2017-04-05 14:46:27 -0700458#if defined(__ANDROID__)
459 android_errorWriteLog(0x534e4554, "36392138");
460#endif
Elliott Hughesfba2a1a2019-12-16 16:16:16 -0800461 return kInvalidFile;
Tianjie Xu0fda1cf2017-04-05 14:46:27 -0700462 }
463
Tianjie6ab29122020-03-18 17:44:30 -0700464 auto cdr = reinterpret_cast<const CentralDirectoryRecord*>(ptr);
Narayan Kamath926973e2014-06-09 14:18:14 +0100465 if (cdr->record_signature != CentralDirectoryRecord::kSignature) {
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700466 ALOGW("Zip: missed a central dir sig (at %" PRIu64 ")", i);
Elliott Hughesfba2a1a2019-12-16 16:16:16 -0800467 return kInvalidFile;
Narayan Kamath7462f022013-11-21 13:05:04 +0000468 }
469
Narayan Kamath926973e2014-06-09 14:18:14 +0100470 const uint16_t file_name_length = cdr->file_name_length;
471 const uint16_t extra_length = cdr->extra_field_length;
472 const uint16_t comment_length = cdr->comment_length;
Piotr Jastrzebski78271ba2014-08-15 12:53:00 +0100473 const uint8_t* file_name = ptr + sizeof(CentralDirectoryRecord);
474
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700475 if (file_name_length >= cd_length || file_name > cd_end - file_name_length) {
476 ALOGW("Zip: file name for entry %" PRIu64
Elliott Hughes50ef29a2019-06-18 18:23:59 -0700477 " exceeds the central directory range, file_name_length: %" PRIu16 ", cd_length: %zu",
478 i, file_name_length, cd_length);
Elliott Hughesfba2a1a2019-12-16 16:16:16 -0800479 return kInvalidEntryName;
Tianjie Xu9e020e22016-10-10 12:11:30 -0700480 }
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700481
482 const uint8_t* extra_field = file_name + file_name_length;
483 if (extra_length >= cd_length || extra_field > cd_end - extra_length) {
484 ALOGW("Zip: extra field for entry %" PRIu64
485 " exceeds the central directory range, file_name_length: %" PRIu16 ", cd_length: %zu",
486 i, extra_length, cd_length);
487 return kInvalidFile;
488 }
489
490 off64_t local_header_offset = cdr->local_file_header_offset;
491 if (local_header_offset == UINT32_MAX) {
Tianjie6ab29122020-03-18 17:44:30 -0700492 Zip64ExtendedInfo zip64_info{};
493 if (auto status = ParseZip64ExtendedInfoInExtraField(
494 extra_field, extra_length, cdr->uncompressed_size, cdr->compressed_size,
495 cdr->local_file_header_offset, &zip64_info);
496 status != kSuccess) {
497 return status;
498 }
499 CHECK(zip64_info.local_header_offset.has_value());
500 local_header_offset = zip64_info.local_header_offset.value();
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700501 }
502
503 if (local_header_offset >= archive->directory_offset) {
504 ALOGW("Zip: bad LFH offset %" PRId64 " at entry %" PRIu64,
505 static_cast<int64_t>(local_header_offset), i);
506 return kInvalidFile;
507 }
508
Elliott Hughes50ef29a2019-06-18 18:23:59 -0700509 // Check that file name is valid UTF-8 and doesn't contain NUL (U+0000) characters.
Narayan Kamath044bc8e2014-12-03 18:22:53 +0000510 if (!IsValidEntryName(file_name, file_name_length)) {
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700511 ALOGW("Zip: invalid file name at entry %" PRIu64, i);
Elliott Hughesfba2a1a2019-12-16 16:16:16 -0800512 return kInvalidEntryName;
Piotr Jastrzebski78271ba2014-08-15 12:53:00 +0100513 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000514
Elliott Hughes50ef29a2019-06-18 18:23:59 -0700515 // Add the CDE filename to the hash table.
516 std::string_view entry_name{reinterpret_cast<const char*>(file_name), file_name_length};
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800517 if (auto add_result =
518 archive->cd_entry_map->AddToMap(entry_name, archive->central_directory.GetBasePtr());
519 add_result != 0) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000520 ALOGW("Zip: Error adding entry to hash table %d", add_result);
Dmitriy Ivanov3ea93da2015-03-06 11:48:47 -0800521 return add_result;
Narayan Kamath7462f022013-11-21 13:05:04 +0000522 }
523
Narayan Kamath926973e2014-06-09 14:18:14 +0100524 ptr += sizeof(CentralDirectoryRecord) + file_name_length + extra_length + comment_length;
525 if ((ptr - cd_ptr) > static_cast<int64_t>(cd_length)) {
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700526 ALOGW("Zip: bad CD advance (%tu vs %zu) at entry %" PRIu64, ptr - cd_ptr, cd_length, i);
Elliott Hughesfba2a1a2019-12-16 16:16:16 -0800527 return kInvalidFile;
Narayan Kamath7462f022013-11-21 13:05:04 +0000528 }
529 }
Narayan Kamathc1a56dc2017-08-09 18:32:09 +0100530
531 uint32_t lfh_start_bytes;
532 if (!archive->mapped_zip.ReadAtOffset(reinterpret_cast<uint8_t*>(&lfh_start_bytes),
533 sizeof(uint32_t), 0)) {
534 ALOGW("Zip: Unable to read header for entry at offset == 0.");
Elliott Hughesfba2a1a2019-12-16 16:16:16 -0800535 return kInvalidFile;
Narayan Kamathc1a56dc2017-08-09 18:32:09 +0100536 }
537
538 if (lfh_start_bytes != LocalFileHeader::kSignature) {
539 ALOGW("Zip: Entry at offset zero has invalid LFH signature %" PRIx32, lfh_start_bytes);
540#if defined(__ANDROID__)
541 android_errorWriteLog(0x534e4554, "64211847");
542#endif
Elliott Hughesfba2a1a2019-12-16 16:16:16 -0800543 return kInvalidFile;
Narayan Kamathc1a56dc2017-08-09 18:32:09 +0100544 }
545
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700546 ALOGV("+++ zip good scan %" PRIu64 " entries", num_entries);
Narayan Kamath7462f022013-11-21 13:05:04 +0000547
Tianjie6ab29122020-03-18 17:44:30 -0700548 return kSuccess;
Narayan Kamath7462f022013-11-21 13:05:04 +0000549}
550
Jiyong Parkcd997e62017-06-30 17:23:33 +0900551static int32_t OpenArchiveInternal(ZipArchive* archive, const char* debug_file_name) {
Elliott Hughesfba2a1a2019-12-16 16:16:16 -0800552 int32_t result = MapCentralDirectory(debug_file_name, archive);
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700553 return result != kSuccess ? result : ParseZipArchive(archive);
Narayan Kamath7462f022013-11-21 13:05:04 +0000554}
555
Jiyong Parkcd997e62017-06-30 17:23:33 +0900556int32_t OpenArchiveFd(int fd, const char* debug_file_name, ZipArchiveHandle* handle,
557 bool assume_ownership) {
Ryan Mitchell23150e42020-03-09 09:33:46 -0700558 ZipArchive* archive = new ZipArchive(MappedZipFile(fd), assume_ownership);
Narayan Kamath7462f022013-11-21 13:05:04 +0000559 *handle = archive;
Narayan Kamath7462f022013-11-21 13:05:04 +0000560 return OpenArchiveInternal(archive, debug_file_name);
561}
562
Ryan Mitchell23150e42020-03-09 09:33:46 -0700563int32_t OpenArchiveFdRange(int fd, const char* debug_file_name, ZipArchiveHandle* handle,
564 off64_t length, off64_t offset, bool assume_ownership) {
565 ZipArchive* archive = new ZipArchive(MappedZipFile(fd, length, offset), assume_ownership);
566 *handle = archive;
567
568 if (length < 0) {
569 ALOGW("Invalid zip length %" PRId64, length);
570 return kIoError;
571 }
572
573 if (offset < 0) {
574 ALOGW("Invalid zip offset %" PRId64, offset);
575 return kIoError;
576 }
577
578 return OpenArchiveInternal(archive, debug_file_name);
579}
580
Narayan Kamath7462f022013-11-21 13:05:04 +0000581int32_t OpenArchive(const char* fileName, ZipArchiveHandle* handle) {
Nick Kralevich3bdf7442018-12-18 12:48:06 -0800582 const int fd = ::android::base::utf8::open(fileName, O_RDONLY | O_BINARY | O_CLOEXEC, 0);
Ryan Mitchell23150e42020-03-09 09:33:46 -0700583 ZipArchive* archive = new ZipArchive(MappedZipFile(fd), true);
Narayan Kamath7462f022013-11-21 13:05:04 +0000584 *handle = archive;
585
Narayan Kamath7462f022013-11-21 13:05:04 +0000586 if (fd < 0) {
587 ALOGW("Unable to open '%s': %s", fileName, strerror(errno));
588 return kIoError;
Narayan Kamath7462f022013-11-21 13:05:04 +0000589 }
Dmitriy Ivanov40b52b22014-07-15 19:33:00 -0700590
Narayan Kamath7462f022013-11-21 13:05:04 +0000591 return OpenArchiveInternal(archive, fileName);
592}
593
Elliott Hughesf66460b2019-10-22 11:44:50 -0700594int32_t OpenArchiveFromMemory(const void* address, size_t length, const char* debug_file_name,
Jiyong Parkcd997e62017-06-30 17:23:33 +0900595 ZipArchiveHandle* handle) {
Tianjie Xu18c25922016-09-29 15:27:41 -0700596 ZipArchive* archive = new ZipArchive(address, length);
597 *handle = archive;
598 return OpenArchiveInternal(archive, debug_file_name);
599}
600
Elliott Hughes26724132019-10-25 09:57:58 -0700601ZipArchiveInfo GetArchiveInfo(ZipArchiveHandle archive) {
602 ZipArchiveInfo result;
603 result.archive_size = archive->mapped_zip.GetFileLength();
604 result.entry_count = archive->num_entries;
605 return result;
606}
607
Narayan Kamath7462f022013-11-21 13:05:04 +0000608/*
609 * Close a ZipArchive, closing the file and freeing the contents.
610 */
Ryan Prichard3673f992018-10-10 22:41:14 -0700611void CloseArchive(ZipArchiveHandle archive) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000612 ALOGV("Closing archive %p", archive);
Neil Fullerb1a113f2014-07-25 14:43:04 +0100613 delete archive;
Narayan Kamath7462f022013-11-21 13:05:04 +0000614}
615
Narayan Kamath162b7052017-06-05 13:21:12 +0100616static int32_t ValidateDataDescriptor(MappedZipFile& mapped_zip, ZipEntry* entry) {
Tianjie0ec0eaa2020-03-26 12:34:44 -0700617 // Maximum possible size for data descriptor: 2 * 4 + 2 * 8 = 24 bytes
618 uint8_t ddBuf[24];
Adam Lesinskide117e42017-06-19 10:27:38 -0700619 off64_t offset = entry->offset;
620 if (entry->method != kCompressStored) {
621 offset += entry->compressed_length;
622 } else {
623 offset += entry->uncompressed_length;
624 }
625
626 if (!mapped_zip.ReadAtOffset(ddBuf, sizeof(ddBuf), offset)) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000627 return kIoError;
628 }
629
Narayan Kamath926973e2014-06-09 14:18:14 +0100630 const uint32_t ddSignature = *(reinterpret_cast<const uint32_t*>(ddBuf));
Tianjie0ec0eaa2020-03-26 12:34:44 -0700631 uint8_t* ddReadPtr = (ddSignature == DataDescriptor::kOptSignature) ? ddBuf + 4 : ddBuf;
632 DataDescriptor descriptor{};
633 descriptor.crc32 = ConsumeUnaligned<uint32_t>(&ddReadPtr);
634 if (entry->zip64_format_size) {
635 descriptor.compressed_size = ConsumeUnaligned<uint64_t>(&ddReadPtr);
636 descriptor.uncompressed_size = ConsumeUnaligned<uint64_t>(&ddReadPtr);
637 } else {
638 descriptor.compressed_size = ConsumeUnaligned<uint32_t>(&ddReadPtr);
639 descriptor.uncompressed_size = ConsumeUnaligned<uint32_t>(&ddReadPtr);
640 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000641
Narayan Kamath162b7052017-06-05 13:21:12 +0100642 // Validate that the values in the data descriptor match those in the central
643 // directory.
Tianjie0ec0eaa2020-03-26 12:34:44 -0700644 if (entry->compressed_length != descriptor.compressed_size ||
645 entry->uncompressed_length != descriptor.uncompressed_size ||
646 entry->crc32 != descriptor.crc32) {
Narayan Kamath162b7052017-06-05 13:21:12 +0100647 ALOGW("Zip: size/crc32 mismatch. expected {%" PRIu32 ", %" PRIu32 ", %" PRIx32
Tianjie0ec0eaa2020-03-26 12:34:44 -0700648 "}, was {%" PRIu64 ", %" PRIu64 ", %" PRIx32 "}",
Narayan Kamath162b7052017-06-05 13:21:12 +0100649 entry->compressed_length, entry->uncompressed_length, entry->crc32,
Tianjie0ec0eaa2020-03-26 12:34:44 -0700650 descriptor.compressed_size, descriptor.uncompressed_size, descriptor.crc32);
Narayan Kamath162b7052017-06-05 13:21:12 +0100651 return kInconsistentInformation;
652 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000653
654 return 0;
655}
656
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800657static int32_t FindEntry(const ZipArchive* archive, std::string_view entryName,
658 const uint64_t nameOffset, ZipEntry* data) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000659 // Recover the start of the central directory entry from the filename
660 // pointer. The filename is the first entry past the fixed-size data,
661 // so we can just subtract back from that.
Elliott Hughes50ef29a2019-06-18 18:23:59 -0700662 const uint8_t* base_ptr = archive->central_directory.GetBasePtr();
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800663 const uint8_t* ptr = base_ptr + nameOffset;
Narayan Kamath926973e2014-06-09 14:18:14 +0100664 ptr -= sizeof(CentralDirectoryRecord);
Narayan Kamath7462f022013-11-21 13:05:04 +0000665
666 // This is the base of our mmapped region, we have to sanity check that
667 // the name that's in the hash table is a pointer to a location within
668 // this mapped region.
Tianjie Xu18c25922016-09-29 15:27:41 -0700669 if (ptr < base_ptr || ptr > base_ptr + archive->central_directory.GetMapLength()) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000670 ALOGW("Zip: Invalid entry pointer");
671 return kInvalidOffset;
672 }
673
Tianjie6ab29122020-03-18 17:44:30 -0700674 auto cdr = reinterpret_cast<const CentralDirectoryRecord*>(ptr);
Narayan Kamath926973e2014-06-09 14:18:14 +0100675
Narayan Kamath7462f022013-11-21 13:05:04 +0000676 // The offset of the start of the central directory in the zipfile.
677 // We keep this lying around so that we can sanity check all our lengths
678 // and our per-file structures.
679 const off64_t cd_offset = archive->directory_offset;
680
681 // Fill out the compression method, modification time, crc32
682 // and other interesting attributes from the central directory. These
683 // will later be compared against values from the local file header.
Narayan Kamath926973e2014-06-09 14:18:14 +0100684 data->method = cdr->compression_method;
beonit0e99a2f2015-07-18 02:08:16 +0900685 data->mod_time = cdr->last_mod_date << 16 | cdr->last_mod_time;
Narayan Kamath926973e2014-06-09 14:18:14 +0100686 data->crc32 = cdr->crc32;
687 data->compressed_length = cdr->compressed_size;
688 data->uncompressed_length = cdr->uncompressed_size;
Narayan Kamath7462f022013-11-21 13:05:04 +0000689
690 // Figure out the local header offset from the central directory. The
691 // actual file data will begin after the local header and the name /
692 // extra comments.
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700693 off64_t local_header_offset = cdr->local_file_header_offset;
694 // One of the info field is UINT32_MAX, try to parse the real value in the zip64 extended info in
695 // the extra field.
696 if (cdr->uncompressed_size == UINT32_MAX || cdr->compressed_size == UINT32_MAX ||
697 cdr->local_file_header_offset == UINT32_MAX) {
Tianjie6ab29122020-03-18 17:44:30 -0700698 const uint8_t* extra_field = ptr + sizeof(CentralDirectoryRecord) + cdr->file_name_length;
699 Zip64ExtendedInfo zip64_info{};
700 if (auto status = ParseZip64ExtendedInfoInExtraField(
701 extra_field, cdr->extra_field_length, cdr->uncompressed_size, cdr->compressed_size,
702 cdr->local_file_header_offset, &zip64_info);
703 status != kSuccess) {
704 return status;
705 }
706
Tianjie0ec0eaa2020-03-26 12:34:44 -0700707 // TODO(xunchang) remove the size limit and support entry length > UINT32_MAX.
708 data->uncompressed_length =
709 static_cast<uint32_t>(zip64_info.uncompressed_file_size.value_or(cdr->uncompressed_size));
710 data->compressed_length =
711 static_cast<uint32_t>(zip64_info.compressed_file_size.value_or(cdr->compressed_size));
712 local_header_offset = zip64_info.local_header_offset.value_or(local_header_offset);
713 data->zip64_format_size =
714 cdr->uncompressed_size == UINT32_MAX || cdr->compressed_size == UINT32_MAX;
Tianjie Xu69ee4b72020-03-11 11:59:10 -0700715 }
716
Narayan Kamath926973e2014-06-09 14:18:14 +0100717 if (local_header_offset + static_cast<off64_t>(sizeof(LocalFileHeader)) >= cd_offset) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000718 ALOGW("Zip: bad local hdr offset in zip");
719 return kInvalidOffset;
720 }
721
Narayan Kamath926973e2014-06-09 14:18:14 +0100722 uint8_t lfh_buf[sizeof(LocalFileHeader)];
Tianjie Xu18c25922016-09-29 15:27:41 -0700723 if (!archive->mapped_zip.ReadAtOffset(lfh_buf, sizeof(lfh_buf), local_header_offset)) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800724 ALOGW("Zip: failed reading lfh name from offset %" PRId64,
Jiyong Parkcd997e62017-06-30 17:23:33 +0900725 static_cast<int64_t>(local_header_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000726 return kIoError;
727 }
728
Tianjie6ab29122020-03-18 17:44:30 -0700729 auto lfh = reinterpret_cast<const LocalFileHeader*>(lfh_buf);
Narayan Kamath926973e2014-06-09 14:18:14 +0100730 if (lfh->lfh_signature != LocalFileHeader::kSignature) {
Mark Salyzyn99ef9912014-03-14 14:26:22 -0700731 ALOGW("Zip: didn't find signature at start of lfh, offset=%" PRId64,
Jiyong Parkcd997e62017-06-30 17:23:33 +0900732 static_cast<int64_t>(local_header_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000733 return kInvalidOffset;
734 }
735
Tianjie6ab29122020-03-18 17:44:30 -0700736 // Check that the local file header name matches the declared name in the central directory.
737 CHECK_LE(entryName.size(), UINT16_MAX);
738 auto nameLen = static_cast<uint16_t>(entryName.size());
739 if (lfh->file_name_length != nameLen) {
740 ALOGW("Zip: lfh name length did not match central directory for %s: %" PRIu16 " %" PRIu16,
741 std::string(entryName).c_str(), lfh->file_name_length, nameLen);
742 return kInconsistentInformation;
743 }
744 const off64_t name_offset = local_header_offset + sizeof(LocalFileHeader);
745 if (name_offset > cd_offset - lfh->file_name_length) {
746 ALOGW("Zip: lfh name has invalid declared length");
747 return kInvalidOffset;
748 }
749
750 std::vector<uint8_t> name_buf(nameLen);
751 if (!archive->mapped_zip.ReadAtOffset(name_buf.data(), nameLen, name_offset)) {
752 ALOGW("Zip: failed reading lfh name from offset %" PRId64, static_cast<int64_t>(name_offset));
753 return kIoError;
754 }
755 if (memcmp(entryName.data(), name_buf.data(), nameLen) != 0) {
756 ALOGW("Zip: lfh name did not match central directory");
757 return kInconsistentInformation;
758 }
759
760 uint64_t lfh_uncompressed_size = lfh->uncompressed_size;
761 uint64_t lfh_compressed_size = lfh->compressed_size;
762 if (lfh_uncompressed_size == UINT32_MAX || lfh_compressed_size == UINT32_MAX) {
Tianjie0ec0eaa2020-03-26 12:34:44 -0700763 if (lfh_uncompressed_size != UINT32_MAX || lfh_compressed_size != UINT32_MAX) {
764 ALOGW(
765 "Zip: The zip64 extended field in the local header MUST include BOTH original and "
766 "compressed file size fields.");
767 return kInvalidFile;
768 }
769
Tianjie6ab29122020-03-18 17:44:30 -0700770 const off64_t lfh_extra_field_offset = name_offset + lfh->file_name_length;
771 const uint16_t lfh_extra_field_size = lfh->extra_field_length;
772 if (lfh_extra_field_offset > cd_offset - lfh_extra_field_size) {
773 ALOGW("Zip: extra field has a bad size for entry %s", std::string(entryName).c_str());
774 return kInvalidOffset;
775 }
776
777 std::vector<uint8_t> local_extra_field(lfh_extra_field_size);
778 if (!archive->mapped_zip.ReadAtOffset(local_extra_field.data(), lfh_extra_field_size,
779 lfh_extra_field_offset)) {
780 ALOGW("Zip: failed reading lfh extra field from offset %" PRId64, lfh_extra_field_offset);
781 return kIoError;
782 }
783
784 Zip64ExtendedInfo zip64_info{};
785 if (auto status = ParseZip64ExtendedInfoInExtraField(
786 local_extra_field.data(), lfh_extra_field_size, lfh->uncompressed_size,
787 lfh->compressed_size, std::nullopt, &zip64_info);
788 status != kSuccess) {
789 return status;
790 }
791
792 CHECK(zip64_info.uncompressed_file_size.has_value());
793 CHECK(zip64_info.compressed_file_size.has_value());
794 lfh_uncompressed_size = zip64_info.uncompressed_file_size.value();
795 lfh_compressed_size = zip64_info.compressed_file_size.value();
796 }
797
Narayan Kamath7462f022013-11-21 13:05:04 +0000798 // Paranoia: Match the values specified in the local file header
799 // to those specified in the central directory.
Adam Lesinskid987c9d2017-04-06 18:55:47 -0700800
Narayan Kamath162b7052017-06-05 13:21:12 +0100801 // Warn if central directory and local file header don't agree on the use
802 // of a trailing Data Descriptor. The reference implementation is inconsistent
803 // and appears to use the LFH value during extraction (unzip) but the CD value
804 // while displayng information about archives (zipinfo). The spec remains
805 // silent on this inconsistency as well.
806 //
807 // For now, always use the version from the LFH but make sure that the values
808 // specified in the central directory match those in the data descriptor.
809 //
810 // NOTE: It's also worth noting that unzip *does* warn about inconsistencies in
811 // bit 11 (EFS: The language encoding flag, marking that filename and comment are
812 // encoded using UTF-8). This implementation does not check for the presence of
813 // that flag and always enforces that entry names are valid UTF-8.
814 if ((lfh->gpb_flags & kGPBDDFlagMask) != (cdr->gpb_flags & kGPBDDFlagMask)) {
815 ALOGW("Zip: gpb flag mismatch at bit 3. expected {%04" PRIx16 "}, was {%04" PRIx16 "}",
Adam Lesinskid987c9d2017-04-06 18:55:47 -0700816 cdr->gpb_flags, lfh->gpb_flags);
Adam Lesinskid987c9d2017-04-06 18:55:47 -0700817 }
818
819 // If there is no trailing data descriptor, verify that the central directory and local file
820 // header agree on the crc, compressed, and uncompressed sizes of the entry.
Narayan Kamath926973e2014-06-09 14:18:14 +0100821 if ((lfh->gpb_flags & kGPBDDFlagMask) == 0) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000822 data->has_data_descriptor = 0;
Tianjie6ab29122020-03-18 17:44:30 -0700823 if (data->compressed_length != lfh_compressed_size ||
824 data->uncompressed_length != lfh_uncompressed_size || data->crc32 != lfh->crc32) {
Jiyong Parkcd997e62017-06-30 17:23:33 +0900825 ALOGW("Zip: size/crc32 mismatch. expected {%" PRIu32 ", %" PRIu32 ", %" PRIx32
Tianjie6ab29122020-03-18 17:44:30 -0700826 "}, was {%" PRIu64 ", %" PRIu64 ", %" PRIx32 "}",
827 data->compressed_length, data->uncompressed_length, data->crc32, lfh_compressed_size,
828 lfh_uncompressed_size, lfh->crc32);
Narayan Kamath7462f022013-11-21 13:05:04 +0000829 return kInconsistentInformation;
830 }
831 } else {
832 data->has_data_descriptor = 1;
833 }
834
Elliott Hughes55fd2932017-05-28 22:59:04 -0700835 // 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 -0700836 data->version_made_by = cdr->version_made_by;
Elliott Hughesd5095252019-10-28 21:35:52 -0700837 data->external_file_attributes = cdr->external_file_attributes;
Elliott Hughes26724132019-10-25 09:57:58 -0700838 if ((data->version_made_by >> 8) == 3) {
Elliott Hughes55fd2932017-05-28 22:59:04 -0700839 data->unix_mode = (cdr->external_file_attributes >> 16) & 0xffff;
840 } else {
841 data->unix_mode = 0777;
842 }
843
Elliott Hughesd5095252019-10-28 21:35:52 -0700844 // 4.4.4: general purpose bit flags.
845 data->gpbf = lfh->gpb_flags;
846
Elliott Hughes26724132019-10-25 09:57:58 -0700847 // 4.4.14: the lowest bit of the internal file attributes field indicates text.
848 // Currently only needed to implement zipinfo.
849 data->is_text = (cdr->internal_file_attributes & 1);
850
Jiyong Parkcd997e62017-06-30 17:23:33 +0900851 const off64_t data_offset = local_header_offset + sizeof(LocalFileHeader) +
852 lfh->file_name_length + lfh->extra_field_length;
Narayan Kamath48953a12014-01-24 12:32:39 +0000853 if (data_offset > cd_offset) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800854 ALOGW("Zip: bad data offset %" PRId64 " in zip", static_cast<int64_t>(data_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000855 return kInvalidOffset;
856 }
857
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800858 if (static_cast<off64_t>(data_offset + data->compressed_length) > cd_offset) {
Mark Salyzyn088bf902014-05-08 16:02:20 -0700859 ALOGW("Zip: bad compressed length in zip (%" PRId64 " + %" PRIu32 " > %" PRId64 ")",
Jiyong Parkcd997e62017-06-30 17:23:33 +0900860 static_cast<int64_t>(data_offset), data->compressed_length,
861 static_cast<int64_t>(cd_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000862 return kInvalidOffset;
863 }
864
865 if (data->method == kCompressStored &&
Jiyong Parkcd997e62017-06-30 17:23:33 +0900866 static_cast<off64_t>(data_offset + data->uncompressed_length) > cd_offset) {
867 ALOGW("Zip: bad uncompressed length in zip (%" PRId64 " + %" PRIu32 " > %" PRId64 ")",
868 static_cast<int64_t>(data_offset), data->uncompressed_length,
869 static_cast<int64_t>(cd_offset));
870 return kInvalidOffset;
Narayan Kamath7462f022013-11-21 13:05:04 +0000871 }
872
873 data->offset = data_offset;
874 return 0;
875}
876
877struct IterationHandle {
Narayan Kamath7462f022013-11-21 13:05:04 +0000878 ZipArchive* archive;
Piotr Jastrzebski8e085362014-08-18 11:37:45 +0100879
Songchun Fanc33f5262020-03-24 09:15:51 -0700880 std::function<bool(std::string_view)> matcher;
Elliott Hughesa22ac0f2019-05-08 10:44:06 -0700881
882 uint32_t position = 0;
883
Songchun Fanc33f5262020-03-24 09:15:51 -0700884 IterationHandle(ZipArchive* archive, std::function<bool(std::string_view)> in_matcher)
885 : archive(archive), matcher(std::move(in_matcher)) {}
886
887 bool Match(std::string_view entry_name) const { return matcher(entry_name); }
Narayan Kamath7462f022013-11-21 13:05:04 +0000888};
889
Ryan Prichard3673f992018-10-10 22:41:14 -0700890int32_t StartIteration(ZipArchiveHandle archive, void** cookie_ptr,
Elliott Hughesa22ac0f2019-05-08 10:44:06 -0700891 const std::string_view optional_prefix,
892 const std::string_view optional_suffix) {
Elliott Hughesa22ac0f2019-05-08 10:44:06 -0700893 if (optional_prefix.size() > static_cast<size_t>(UINT16_MAX) ||
894 optional_suffix.size() > static_cast<size_t>(UINT16_MAX)) {
895 ALOGW("Zip: prefix/suffix too long");
896 return kInvalidEntryName;
897 }
Songchun Fanc33f5262020-03-24 09:15:51 -0700898 auto matcher = [prefix = std::string(optional_prefix),
899 suffix = std::string(optional_suffix)](std::string_view name) mutable {
900 return android::base::StartsWith(name, prefix) && android::base::EndsWith(name, suffix);
901 };
902 return StartIteration(archive, cookie_ptr, std::move(matcher));
903}
904
905int32_t StartIteration(ZipArchiveHandle archive, void** cookie_ptr,
906 std::function<bool(std::string_view)> matcher) {
907 if (archive == nullptr || archive->cd_entry_map == nullptr) {
908 ALOGW("Zip: Invalid ZipArchiveHandle");
909 return kInvalidHandle;
910 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000911
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800912 archive->cd_entry_map->ResetIteration();
Songchun Fanc33f5262020-03-24 09:15:51 -0700913 *cookie_ptr = new IterationHandle(archive, matcher);
Narayan Kamath7462f022013-11-21 13:05:04 +0000914 return 0;
915}
916
Piotr Jastrzebski79c8b342014-08-08 14:02:17 +0100917void EndIteration(void* cookie) {
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100918 delete reinterpret_cast<IterationHandle*>(cookie);
Piotr Jastrzebski79c8b342014-08-08 14:02:17 +0100919}
920
Elliott Hughesb17bf522019-05-03 22:38:44 -0700921int32_t FindEntry(const ZipArchiveHandle archive, const std::string_view entryName,
922 ZipEntry* data) {
923 if (entryName.empty() || entryName.size() > static_cast<size_t>(UINT16_MAX)) {
924 ALOGW("Zip: Invalid filename of length %zu", entryName.size());
925 return kInvalidEntryName;
926 }
927
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800928 const auto [result, offset] =
929 archive->cd_entry_map->GetCdEntryOffset(entryName, archive->central_directory.GetBasePtr());
930 if (result != 0) {
Elliott Hughesb17bf522019-05-03 22:38:44 -0700931 ALOGV("Zip: Could not find entry %.*s", static_cast<int>(entryName.size()), entryName.data());
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800932 return static_cast<int32_t>(result); // kEntryNotFound is safe to truncate.
Elliott Hughesb17bf522019-05-03 22:38:44 -0700933 }
Elliott Hughesa5ff19e2019-05-07 09:27:59 -0700934 // We know there are at most hash_table_size entries, safe to truncate.
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800935 return FindEntry(archive, entryName, offset, data);
Elliott Hughesb17bf522019-05-03 22:38:44 -0700936}
937
Elliott Hughese06a8082019-05-22 18:56:41 -0700938int32_t Next(void* cookie, ZipEntry* data, std::string* name) {
Elliott Hughes1e40c302019-06-12 12:12:47 -0700939 std::string_view sv;
940 int32_t result = Next(cookie, data, &sv);
941 if (result == 0 && name) {
942 *name = std::string(sv);
943 }
944 return result;
945}
946
947int32_t Next(void* cookie, ZipEntry* data, std::string_view* name) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800948 IterationHandle* handle = reinterpret_cast<IterationHandle*>(cookie);
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800949 if (handle == nullptr) {
Zimuzo5a503ef2018-09-17 19:49:55 +0100950 ALOGW("Zip: Null ZipArchiveHandle");
Narayan Kamath7462f022013-11-21 13:05:04 +0000951 return kInvalidHandle;
952 }
953
954 ZipArchive* archive = handle->archive;
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800955 if (archive == nullptr || archive->cd_entry_map == nullptr) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000956 ALOGW("Zip: Invalid ZipArchiveHandle");
957 return kInvalidHandle;
958 }
959
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800960 auto entry = archive->cd_entry_map->Next(archive->central_directory.GetBasePtr());
961 while (entry != std::pair<std::string_view, uint64_t>()) {
962 const auto [entry_name, offset] = entry;
Songchun Fanc33f5262020-03-24 09:15:51 -0700963 if (handle->Match(entry_name)) {
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800964 const int error = FindEntry(archive, entry_name, offset, data);
Elliott Hughes50ef29a2019-06-18 18:23:59 -0700965 if (!error && name) {
966 *name = entry_name;
Narayan Kamath7462f022013-11-21 13:05:04 +0000967 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000968 return error;
969 }
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800970 entry = archive->cd_entry_map->Next(archive->central_directory.GetBasePtr());
Narayan Kamath7462f022013-11-21 13:05:04 +0000971 }
972
Tianjie Xu28f8eae2020-03-05 16:31:23 -0800973 archive->cd_entry_map->ResetIteration();
Narayan Kamath7462f022013-11-21 13:05:04 +0000974 return kIterationEnd;
975}
976
Narayan Kamathf899bd52015-04-17 11:53:14 +0100977// A Writer that writes data to a fixed size memory region.
978// The size of the memory region must be equal to the total size of
979// the data appended to it.
Narayan Kamath485b3642017-10-26 14:42:39 +0100980class MemoryWriter : public zip_archive::Writer {
Narayan Kamathf899bd52015-04-17 11:53:14 +0100981 public:
Jiyong Parkcd997e62017-06-30 17:23:33 +0900982 MemoryWriter(uint8_t* buf, size_t size) : Writer(), buf_(buf), size_(size), bytes_written_(0) {}
Narayan Kamathf899bd52015-04-17 11:53:14 +0100983
984 virtual bool Append(uint8_t* buf, size_t buf_size) override {
985 if (bytes_written_ + buf_size > size_) {
Elliott Hughese8f4b142018-10-19 16:09:39 -0700986 ALOGW("Zip: Unexpected size %zu (declared) vs %zu (actual)", size_,
Jiyong Parkcd997e62017-06-30 17:23:33 +0900987 bytes_written_ + buf_size);
Narayan Kamathf899bd52015-04-17 11:53:14 +0100988 return false;
989 }
990
991 memcpy(buf_ + bytes_written_, buf, buf_size);
992 bytes_written_ += buf_size;
993 return true;
994 }
995
996 private:
997 uint8_t* const buf_;
998 const size_t size_;
999 size_t bytes_written_;
1000};
1001
1002// A Writer that appends data to a file |fd| at its current position.
1003// The file will be truncated to the end of the written data.
Narayan Kamath485b3642017-10-26 14:42:39 +01001004class FileWriter : public zip_archive::Writer {
Narayan Kamathf899bd52015-04-17 11:53:14 +01001005 public:
Narayan Kamathf899bd52015-04-17 11:53:14 +01001006 // Creates a FileWriter for |fd| and prepare to write |entry| to it,
1007 // guaranteeing that the file descriptor is valid and that there's enough
1008 // space on the volume to write out the entry completely and that the file
Tao Baoa456c212016-11-15 10:08:07 -08001009 // is truncated to the correct length (no truncation if |fd| references a
1010 // block device).
Narayan Kamathf899bd52015-04-17 11:53:14 +01001011 //
1012 // Returns a valid FileWriter on success, |nullptr| if an error occurred.
Yurii Zubrytskyi834326c2017-12-20 01:01:01 -08001013 static FileWriter Create(int fd, const ZipEntry* entry) {
Narayan Kamathf899bd52015-04-17 11:53:14 +01001014 const uint32_t declared_length = entry->uncompressed_length;
1015 const off64_t current_offset = lseek64(fd, 0, SEEK_CUR);
1016 if (current_offset == -1) {
1017 ALOGW("Zip: unable to seek to current location on fd %d: %s", fd, strerror(errno));
Yurii Zubrytskyi834326c2017-12-20 01:01:01 -08001018 return FileWriter{};
Narayan Kamathf899bd52015-04-17 11:53:14 +01001019 }
1020
Narayan Kamathf899bd52015-04-17 11:53:14 +01001021#if defined(__linux__)
1022 if (declared_length > 0) {
1023 // Make sure we have enough space on the volume to extract the compressed
1024 // entry. Note that the call to ftruncate below will change the file size but
1025 // will not allocate space on disk and this call to fallocate will not
1026 // change the file size.
Badhri Jagan Sridharana68d0d12015-06-02 14:47:57 -07001027 // Note: fallocate is only supported by the following filesystems -
1028 // btrfs, ext4, ocfs2, and xfs. Therefore fallocate might fail with
1029 // EOPNOTSUPP error when issued in other filesystems.
1030 // Hence, check for the return error code before concluding that the
1031 // disk does not have enough space.
Andreas Gampe964b95c2019-04-05 13:48:02 -07001032 long result = TEMP_FAILURE_RETRY(fallocate(fd, 0, current_offset, declared_length));
Badhri Jagan Sridharana68d0d12015-06-02 14:47:57 -07001033 if (result == -1 && errno == ENOSPC) {
Elliott Hughes4089d342017-10-27 14:21:12 -07001034 ALOGW("Zip: unable to allocate %" PRId64 " bytes at offset %" PRId64 ": %s",
Narayan Kamathd5d7abe2016-08-10 12:24:05 +01001035 static_cast<int64_t>(declared_length), static_cast<int64_t>(current_offset),
1036 strerror(errno));
Yurii Zubrytskyi834326c2017-12-20 01:01:01 -08001037 return FileWriter{};
Narayan Kamathf899bd52015-04-17 11:53:14 +01001038 }
1039 }
1040#endif // __linux__
1041
Tao Baoa456c212016-11-15 10:08:07 -08001042 struct stat sb;
1043 if (fstat(fd, &sb) == -1) {
1044 ALOGW("Zip: unable to fstat file: %s", strerror(errno));
Yurii Zubrytskyi834326c2017-12-20 01:01:01 -08001045 return FileWriter{};
Narayan Kamathf899bd52015-04-17 11:53:14 +01001046 }
1047
Tao Baoa456c212016-11-15 10:08:07 -08001048 // Block device doesn't support ftruncate(2).
1049 if (!S_ISBLK(sb.st_mode)) {
Andreas Gampe964b95c2019-04-05 13:48:02 -07001050 long result = TEMP_FAILURE_RETRY(ftruncate(fd, declared_length + current_offset));
Tao Baoa456c212016-11-15 10:08:07 -08001051 if (result == -1) {
1052 ALOGW("Zip: unable to truncate file to %" PRId64 ": %s",
1053 static_cast<int64_t>(declared_length + current_offset), strerror(errno));
Yurii Zubrytskyi834326c2017-12-20 01:01:01 -08001054 return FileWriter{};
Tao Baoa456c212016-11-15 10:08:07 -08001055 }
1056 }
1057
Yurii Zubrytskyi834326c2017-12-20 01:01:01 -08001058 return FileWriter(fd, declared_length);
Narayan Kamathf899bd52015-04-17 11:53:14 +01001059 }
1060
Chih-Hung Hsieh747eb142018-09-25 11:16:22 -07001061 FileWriter(FileWriter&& other) noexcept
Yurii Zubrytskyi834326c2017-12-20 01:01:01 -08001062 : fd_(other.fd_),
1063 declared_length_(other.declared_length_),
1064 total_bytes_written_(other.total_bytes_written_) {
1065 other.fd_ = -1;
1066 }
1067
1068 bool IsValid() const { return fd_ != -1; }
1069
Narayan Kamathf899bd52015-04-17 11:53:14 +01001070 virtual bool Append(uint8_t* buf, size_t buf_size) override {
1071 if (total_bytes_written_ + buf_size > declared_length_) {
Elliott Hughese8f4b142018-10-19 16:09:39 -07001072 ALOGW("Zip: Unexpected size %zu (declared) vs %zu (actual)", declared_length_,
Jiyong Parkcd997e62017-06-30 17:23:33 +09001073 total_bytes_written_ + buf_size);
Narayan Kamathf899bd52015-04-17 11:53:14 +01001074 return false;
1075 }
1076
Narayan Kamathe97e66e2015-04-27 16:25:53 +01001077 const bool result = android::base::WriteFully(fd_, buf, buf_size);
1078 if (result) {
1079 total_bytes_written_ += buf_size;
1080 } else {
Elliott Hughese8f4b142018-10-19 16:09:39 -07001081 ALOGW("Zip: unable to write %zu bytes to file; %s", buf_size, strerror(errno));
Narayan Kamathf899bd52015-04-17 11:53:14 +01001082 }
1083
Narayan Kamathe97e66e2015-04-27 16:25:53 +01001084 return result;
Narayan Kamathf899bd52015-04-17 11:53:14 +01001085 }
Jiyong Parkcd997e62017-06-30 17:23:33 +09001086
Narayan Kamathf899bd52015-04-17 11:53:14 +01001087 private:
Yurii Zubrytskyi834326c2017-12-20 01:01:01 -08001088 explicit FileWriter(const int fd = -1, const size_t declared_length = 0)
Jiyong Parkcd997e62017-06-30 17:23:33 +09001089 : Writer(), fd_(fd), declared_length_(declared_length), total_bytes_written_(0) {}
Narayan Kamathf899bd52015-04-17 11:53:14 +01001090
Yurii Zubrytskyi834326c2017-12-20 01:01:01 -08001091 int fd_;
Narayan Kamathf899bd52015-04-17 11:53:14 +01001092 const size_t declared_length_;
1093 size_t total_bytes_written_;
1094};
1095
Narayan Kamath485b3642017-10-26 14:42:39 +01001096class EntryReader : public zip_archive::Reader {
Narayan Kamath8b8faed2017-10-26 14:08:38 +01001097 public:
1098 EntryReader(const MappedZipFile& zip_file, const ZipEntry* entry)
1099 : Reader(), zip_file_(zip_file), entry_(entry) {}
1100
1101 virtual bool ReadAtOffset(uint8_t* buf, size_t len, uint32_t offset) const {
1102 return zip_file_.ReadAtOffset(buf, len, entry_->offset + offset);
1103 }
1104
1105 virtual ~EntryReader() {}
1106
1107 private:
1108 const MappedZipFile& zip_file_;
1109 const ZipEntry* entry_;
1110};
1111
Dmitriy Ivanovf94e1592015-03-06 13:27:59 -08001112// This method is using libz macros with old-style-casts
1113#pragma GCC diagnostic push
1114#pragma GCC diagnostic ignored "-Wold-style-cast"
1115static inline int zlib_inflateInit2(z_stream* stream, int window_bits) {
1116 return inflateInit2(stream, window_bits);
1117}
1118#pragma GCC diagnostic pop
1119
Narayan Kamath485b3642017-10-26 14:42:39 +01001120namespace zip_archive {
1121
1122// Moved out of line to avoid -Wweak-vtables.
1123Reader::~Reader() {}
1124Writer::~Writer() {}
1125
1126int32_t Inflate(const Reader& reader, const uint32_t compressed_length,
1127 const uint32_t uncompressed_length, Writer* writer, uint64_t* crc_out) {
Dmitriy Ivanovedbabfe2015-03-12 09:58:15 -07001128 const size_t kBufSize = 32768;
1129 std::vector<uint8_t> read_buf(kBufSize);
1130 std::vector<uint8_t> write_buf(kBufSize);
Narayan Kamath7462f022013-11-21 13:05:04 +00001131 z_stream zstream;
1132 int zerr;
1133
1134 /*
1135 * Initialize the zlib stream struct.
1136 */
1137 memset(&zstream, 0, sizeof(zstream));
1138 zstream.zalloc = Z_NULL;
1139 zstream.zfree = Z_NULL;
1140 zstream.opaque = Z_NULL;
1141 zstream.next_in = NULL;
1142 zstream.avail_in = 0;
Dmitriy Ivanovedbabfe2015-03-12 09:58:15 -07001143 zstream.next_out = &write_buf[0];
Narayan Kamath7462f022013-11-21 13:05:04 +00001144 zstream.avail_out = kBufSize;
1145 zstream.data_type = Z_UNKNOWN;
1146
1147 /*
1148 * Use the undocumented "negative window bits" feature to tell zlib
1149 * that there's no zlib header waiting for it.
1150 */
Dmitriy Ivanovf94e1592015-03-06 13:27:59 -08001151 zerr = zlib_inflateInit2(&zstream, -MAX_WBITS);
Narayan Kamath7462f022013-11-21 13:05:04 +00001152 if (zerr != Z_OK) {
1153 if (zerr == Z_VERSION_ERROR) {
Jiyong Parkcd997e62017-06-30 17:23:33 +09001154 ALOGE("Installed zlib is not compatible with linked version (%s)", ZLIB_VERSION);
Narayan Kamath7462f022013-11-21 13:05:04 +00001155 } else {
1156 ALOGW("Call to inflateInit2 failed (zerr=%d)", zerr);
1157 }
1158
1159 return kZlibError;
1160 }
1161
Dmitriy Ivanov1f741e52015-03-06 14:26:37 -08001162 auto zstream_deleter = [](z_stream* stream) {
Jiyong Parkcd997e62017-06-30 17:23:33 +09001163 inflateEnd(stream); /* free up any allocated structures */
Dmitriy Ivanov1f741e52015-03-06 14:26:37 -08001164 };
1165
1166 std::unique_ptr<z_stream, decltype(zstream_deleter)> zstream_guard(&zstream, zstream_deleter);
1167
Narayan Kamath2d1e23f2017-10-30 11:17:28 +00001168 const bool compute_crc = (crc_out != nullptr);
Andreas Gampe964b95c2019-04-05 13:48:02 -07001169 uLong crc = 0;
Narayan Kamath8b8faed2017-10-26 14:08:38 +01001170 uint32_t remaining_bytes = compressed_length;
Narayan Kamath7462f022013-11-21 13:05:04 +00001171 do {
1172 /* read as much as we can */
1173 if (zstream.avail_in == 0) {
Andreas Gampe964b95c2019-04-05 13:48:02 -07001174 const uint32_t read_size = (remaining_bytes > kBufSize) ? kBufSize : remaining_bytes;
Narayan Kamath8b8faed2017-10-26 14:08:38 +01001175 const uint32_t offset = (compressed_length - remaining_bytes);
Adam Lesinskide117e42017-06-19 10:27:38 -07001176 // Make sure to read at offset to ensure concurrent access to the fd.
Narayan Kamath8b8faed2017-10-26 14:08:38 +01001177 if (!reader.ReadAtOffset(read_buf.data(), read_size, offset)) {
Andreas Gampe964b95c2019-04-05 13:48:02 -07001178 ALOGW("Zip: inflate read failed, getSize = %u: %s", read_size, strerror(errno));
Dmitriy Ivanov1f741e52015-03-06 14:26:37 -08001179 return kIoError;
Narayan Kamath7462f022013-11-21 13:05:04 +00001180 }
1181
Narayan Kamath8b8faed2017-10-26 14:08:38 +01001182 remaining_bytes -= read_size;
Narayan Kamath7462f022013-11-21 13:05:04 +00001183
Dmitriy Ivanovedbabfe2015-03-12 09:58:15 -07001184 zstream.next_in = &read_buf[0];
Narayan Kamath8b8faed2017-10-26 14:08:38 +01001185 zstream.avail_in = read_size;
Narayan Kamath7462f022013-11-21 13:05:04 +00001186 }
1187
1188 /* uncompress the data */
1189 zerr = inflate(&zstream, Z_NO_FLUSH);
1190 if (zerr != Z_OK && zerr != Z_STREAM_END) {
Jiyong Parkcd997e62017-06-30 17:23:33 +09001191 ALOGW("Zip: inflate zerr=%d (nIn=%p aIn=%u nOut=%p aOut=%u)", zerr, zstream.next_in,
1192 zstream.avail_in, zstream.next_out, zstream.avail_out);
Dmitriy Ivanov1f741e52015-03-06 14:26:37 -08001193 return kZlibError;
Narayan Kamath7462f022013-11-21 13:05:04 +00001194 }
1195
1196 /* write when we're full or when we're done */
Jiyong Parkcd997e62017-06-30 17:23:33 +09001197 if (zstream.avail_out == 0 || (zerr == Z_STREAM_END && zstream.avail_out != kBufSize)) {
Dmitriy Ivanovedbabfe2015-03-12 09:58:15 -07001198 const size_t write_size = zstream.next_out - &write_buf[0];
Narayan Kamathf899bd52015-04-17 11:53:14 +01001199 if (!writer->Append(&write_buf[0], write_size)) {
Narayan Kamath2d1e23f2017-10-30 11:17:28 +00001200 return kIoError;
1201 } else if (compute_crc) {
Andreas Gampe964b95c2019-04-05 13:48:02 -07001202 DCHECK_LE(write_size, kBufSize);
1203 crc = crc32(crc, &write_buf[0], static_cast<uint32_t>(write_size));
Narayan Kamath7462f022013-11-21 13:05:04 +00001204 }
Narayan Kamath7462f022013-11-21 13:05:04 +00001205
Dmitriy Ivanovedbabfe2015-03-12 09:58:15 -07001206 zstream.next_out = &write_buf[0];
Narayan Kamath7462f022013-11-21 13:05:04 +00001207 zstream.avail_out = kBufSize;
1208 }
1209 } while (zerr == Z_OK);
1210
Elliott Hughese8f4b142018-10-19 16:09:39 -07001211 CHECK_EQ(zerr, Z_STREAM_END); /* other errors should've been caught */
Narayan Kamath7462f022013-11-21 13:05:04 +00001212
Narayan Kamath162b7052017-06-05 13:21:12 +01001213 // NOTE: zstream.adler is always set to 0, because we're using the -MAX_WBITS
1214 // "feature" of zlib to tell it there won't be a zlib file header. zlib
1215 // doesn't bother calculating the checksum in that scenario. We just do
1216 // it ourselves above because there are no additional gains to be made by
1217 // having zlib calculate it for us, since they do it by calling crc32 in
1218 // the same manner that we have above.
Narayan Kamath2d1e23f2017-10-30 11:17:28 +00001219 if (compute_crc) {
1220 *crc_out = crc;
1221 }
Narayan Kamath7462f022013-11-21 13:05:04 +00001222
Narayan Kamath8b8faed2017-10-26 14:08:38 +01001223 if (zstream.total_out != uncompressed_length || remaining_bytes != 0) {
Jiyong Parkcd997e62017-06-30 17:23:33 +09001224 ALOGW("Zip: size mismatch on inflated file (%lu vs %" PRIu32 ")", zstream.total_out,
1225 uncompressed_length);
Dmitriy Ivanov1f741e52015-03-06 14:26:37 -08001226 return kInconsistentInformation;
Narayan Kamath7462f022013-11-21 13:05:04 +00001227 }
1228
Dmitriy Ivanov1f741e52015-03-06 14:26:37 -08001229 return 0;
Narayan Kamath7462f022013-11-21 13:05:04 +00001230}
Narayan Kamath485b3642017-10-26 14:42:39 +01001231} // namespace zip_archive
Narayan Kamath7462f022013-11-21 13:05:04 +00001232
Narayan Kamath8b8faed2017-10-26 14:08:38 +01001233static int32_t InflateEntryToWriter(MappedZipFile& mapped_zip, const ZipEntry* entry,
Narayan Kamath485b3642017-10-26 14:42:39 +01001234 zip_archive::Writer* writer, uint64_t* crc_out) {
Narayan Kamath8b8faed2017-10-26 14:08:38 +01001235 const EntryReader reader(mapped_zip, entry);
1236
Narayan Kamath485b3642017-10-26 14:42:39 +01001237 return zip_archive::Inflate(reader, entry->compressed_length, entry->uncompressed_length, writer,
1238 crc_out);
Narayan Kamath8b8faed2017-10-26 14:08:38 +01001239}
1240
Narayan Kamath485b3642017-10-26 14:42:39 +01001241static int32_t CopyEntryToWriter(MappedZipFile& mapped_zip, const ZipEntry* entry,
1242 zip_archive::Writer* writer, uint64_t* crc_out) {
Narayan Kamathf899bd52015-04-17 11:53:14 +01001243 static const uint32_t kBufSize = 32768;
1244 std::vector<uint8_t> buf(kBufSize);
1245
1246 const uint32_t length = entry->uncompressed_length;
1247 uint32_t count = 0;
Andreas Gampe964b95c2019-04-05 13:48:02 -07001248 uLong crc = 0;
Narayan Kamathf899bd52015-04-17 11:53:14 +01001249 while (count < length) {
1250 uint32_t remaining = length - count;
Adam Lesinskide117e42017-06-19 10:27:38 -07001251 off64_t offset = entry->offset + count;
Narayan Kamathf899bd52015-04-17 11:53:14 +01001252
Adam Lesinskide117e42017-06-19 10:27:38 -07001253 // Safe conversion because kBufSize is narrow enough for a 32 bit signed value.
Andreas Gampe964b95c2019-04-05 13:48:02 -07001254 const uint32_t block_size = (remaining > kBufSize) ? kBufSize : remaining;
Adam Lesinskide117e42017-06-19 10:27:38 -07001255
1256 // Make sure to read at offset to ensure concurrent access to the fd.
1257 if (!mapped_zip.ReadAtOffset(buf.data(), block_size, offset)) {
Andreas Gampe964b95c2019-04-05 13:48:02 -07001258 ALOGW("CopyFileToFile: copy read failed, block_size = %u, offset = %" PRId64 ": %s",
Adam Lesinskide117e42017-06-19 10:27:38 -07001259 block_size, static_cast<int64_t>(offset), strerror(errno));
Narayan Kamathf899bd52015-04-17 11:53:14 +01001260 return kIoError;
1261 }
1262
1263 if (!writer->Append(&buf[0], block_size)) {
1264 return kIoError;
1265 }
1266 crc = crc32(crc, &buf[0], block_size);
1267 count += block_size;
1268 }
1269
1270 *crc_out = crc;
1271
1272 return 0;
1273}
1274
Ryan Prichard3673f992018-10-10 22:41:14 -07001275int32_t ExtractToWriter(ZipArchiveHandle archive, ZipEntry* entry, zip_archive::Writer* writer) {
Narayan Kamath7462f022013-11-21 13:05:04 +00001276 const uint16_t method = entry->method;
Narayan Kamath7462f022013-11-21 13:05:04 +00001277
1278 // this should default to kUnknownCompressionMethod.
1279 int32_t return_value = -1;
1280 uint64_t crc = 0;
1281 if (method == kCompressStored) {
Tianjie Xu18c25922016-09-29 15:27:41 -07001282 return_value = CopyEntryToWriter(archive->mapped_zip, entry, writer, &crc);
Narayan Kamath7462f022013-11-21 13:05:04 +00001283 } else if (method == kCompressDeflated) {
Tianjie Xu18c25922016-09-29 15:27:41 -07001284 return_value = InflateEntryToWriter(archive->mapped_zip, entry, writer, &crc);
Narayan Kamath7462f022013-11-21 13:05:04 +00001285 }
1286
1287 if (!return_value && entry->has_data_descriptor) {
Narayan Kamath162b7052017-06-05 13:21:12 +01001288 return_value = ValidateDataDescriptor(archive->mapped_zip, entry);
Narayan Kamath7462f022013-11-21 13:05:04 +00001289 if (return_value) {
1290 return return_value;
1291 }
1292 }
1293
Narayan Kamath162b7052017-06-05 13:21:12 +01001294 // Validate that the CRC matches the calculated value.
1295 if (kCrcChecksEnabled && (entry->crc32 != static_cast<uint32_t>(crc))) {
Mark Salyzyn088bf902014-05-08 16:02:20 -07001296 ALOGW("Zip: crc mismatch: expected %" PRIu32 ", was %" PRIu64, entry->crc32, crc);
Narayan Kamath7462f022013-11-21 13:05:04 +00001297 return kInconsistentInformation;
1298 }
1299
1300 return return_value;
1301}
1302
Ryan Prichard3673f992018-10-10 22:41:14 -07001303int32_t ExtractToMemory(ZipArchiveHandle archive, ZipEntry* entry, uint8_t* begin, uint32_t size) {
Yurii Zubrytskyi834326c2017-12-20 01:01:01 -08001304 MemoryWriter writer(begin, size);
Ryan Prichard3673f992018-10-10 22:41:14 -07001305 return ExtractToWriter(archive, entry, &writer);
Narayan Kamathf899bd52015-04-17 11:53:14 +01001306}
1307
Ryan Prichard3673f992018-10-10 22:41:14 -07001308int32_t ExtractEntryToFile(ZipArchiveHandle archive, ZipEntry* entry, int fd) {
Yurii Zubrytskyi834326c2017-12-20 01:01:01 -08001309 auto writer = FileWriter::Create(fd, entry);
1310 if (!writer.IsValid()) {
Narayan Kamath7462f022013-11-21 13:05:04 +00001311 return kIoError;
1312 }
1313
Ryan Prichard3673f992018-10-10 22:41:14 -07001314 return ExtractToWriter(archive, entry, &writer);
Narayan Kamath7462f022013-11-21 13:05:04 +00001315}
1316
Ryan Prichard3673f992018-10-10 22:41:14 -07001317int GetFileDescriptor(const ZipArchiveHandle archive) {
1318 return archive->mapped_zip.GetFileDescriptor();
Narayan Kamath7462f022013-11-21 13:05:04 +00001319}
Colin Cross7c6c7f02016-09-16 10:15:51 -07001320
Ryan Mitchell23150e42020-03-09 09:33:46 -07001321off64_t GetFileDescriptorOffset(const ZipArchiveHandle archive) {
1322 return archive->mapped_zip.GetFileOffset();
1323}
1324
Tianjie Xu18c25922016-09-29 15:27:41 -07001325#if !defined(_WIN32)
Narayan Kamath485b3642017-10-26 14:42:39 +01001326class ProcessWriter : public zip_archive::Writer {
Tianjie Xu18c25922016-09-29 15:27:41 -07001327 public:
Jiyong Parkcd997e62017-06-30 17:23:33 +09001328 ProcessWriter(ProcessZipEntryFunction func, void* cookie)
1329 : Writer(), proc_function_(func), cookie_(cookie) {}
Tianjie Xu18c25922016-09-29 15:27:41 -07001330
1331 virtual bool Append(uint8_t* buf, size_t buf_size) override {
1332 return proc_function_(buf, buf_size, cookie_);
1333 }
1334
1335 private:
1336 ProcessZipEntryFunction proc_function_;
1337 void* cookie_;
1338};
1339
Ryan Prichard3673f992018-10-10 22:41:14 -07001340int32_t ProcessZipEntryContents(ZipArchiveHandle archive, ZipEntry* entry,
Tianjie Xu18c25922016-09-29 15:27:41 -07001341 ProcessZipEntryFunction func, void* cookie) {
1342 ProcessWriter writer(func, cookie);
Ryan Prichard3673f992018-10-10 22:41:14 -07001343 return ExtractToWriter(archive, entry, &writer);
Tianjie Xu18c25922016-09-29 15:27:41 -07001344}
1345
Jiyong Parkcd997e62017-06-30 17:23:33 +09001346#endif //! defined(_WIN32)
Tianjie Xu18c25922016-09-29 15:27:41 -07001347
1348int MappedZipFile::GetFileDescriptor() const {
1349 if (!has_fd_) {
1350 ALOGW("Zip: MappedZipFile doesn't have a file descriptor.");
1351 return -1;
1352 }
1353 return fd_;
1354}
1355
Elliott Hughesf66460b2019-10-22 11:44:50 -07001356const void* MappedZipFile::GetBasePtr() const {
Tianjie Xu18c25922016-09-29 15:27:41 -07001357 if (has_fd_) {
1358 ALOGW("Zip: MappedZipFile doesn't have a base pointer.");
1359 return nullptr;
1360 }
1361 return base_ptr_;
1362}
1363
Ryan Mitchell23150e42020-03-09 09:33:46 -07001364off64_t MappedZipFile::GetFileOffset() const {
1365 return fd_offset_;
1366}
1367
Tianjie Xu18c25922016-09-29 15:27:41 -07001368off64_t MappedZipFile::GetFileLength() const {
1369 if (has_fd_) {
Ryan Mitchell23150e42020-03-09 09:33:46 -07001370 if (data_length_ != -1) {
1371 return data_length_;
1372 }
1373 data_length_ = lseek64(fd_, 0, SEEK_END);
1374 if (data_length_ == -1) {
Tianjie Xu18c25922016-09-29 15:27:41 -07001375 ALOGE("Zip: lseek on fd %d failed: %s", fd_, strerror(errno));
1376 }
Ryan Mitchell23150e42020-03-09 09:33:46 -07001377 return data_length_;
Tianjie Xu18c25922016-09-29 15:27:41 -07001378 } else {
1379 if (base_ptr_ == nullptr) {
Elliott Hughesfba2a1a2019-12-16 16:16:16 -08001380 ALOGE("Zip: invalid file map");
Tianjie Xu18c25922016-09-29 15:27:41 -07001381 return -1;
1382 }
Ryan Mitchell23150e42020-03-09 09:33:46 -07001383 return data_length_;
Tianjie Xu18c25922016-09-29 15:27:41 -07001384 }
1385}
1386
Tianjie Xu18c25922016-09-29 15:27:41 -07001387// Attempts to read |len| bytes into |buf| at offset |off|.
Narayan Kamath8b8faed2017-10-26 14:08:38 +01001388bool MappedZipFile::ReadAtOffset(uint8_t* buf, size_t len, off64_t off) const {
Tianjie Xu18c25922016-09-29 15:27:41 -07001389 if (has_fd_) {
Ryan Mitchell23150e42020-03-09 09:33:46 -07001390 if (off < 0) {
1391 ALOGE("Zip: invalid offset %" PRId64, off);
1392 return false;
1393 }
1394
1395 off64_t read_offset;
1396 if (__builtin_add_overflow(fd_offset_, off, &read_offset)) {
1397 ALOGE("Zip: invalid read offset %" PRId64 " overflows, fd offset %" PRId64, off, fd_offset_);
1398 return false;
1399 }
1400
1401 if (data_length_ != -1) {
1402 off64_t read_end;
1403 if (len > std::numeric_limits<off64_t>::max() ||
1404 __builtin_add_overflow(off, static_cast<off64_t>(len), &read_end)) {
1405 ALOGE("Zip: invalid read length %" PRId64 " overflows, offset %" PRId64,
1406 static_cast<off64_t>(len), off);
1407 return false;
1408 }
1409
1410 if (read_end > data_length_) {
1411 ALOGE("Zip: invalid read length %" PRId64 " exceeds data length %" PRId64 ", offset %"
1412 PRId64, static_cast<off64_t>(len), data_length_, off);
1413 return false;
1414 }
1415 }
1416
1417 if (!android::base::ReadFullyAtOffset(fd_, buf, len, read_offset)) {
Elliott Hughesfba2a1a2019-12-16 16:16:16 -08001418 ALOGE("Zip: failed to read at offset %" PRId64, off);
Tianjie Xu18c25922016-09-29 15:27:41 -07001419 return false;
1420 }
Adam Lesinskide117e42017-06-19 10:27:38 -07001421 } else {
Ryan Mitchell23150e42020-03-09 09:33:46 -07001422 if (off < 0 || off > data_length_) {
Elliott Hughesfba2a1a2019-12-16 16:16:16 -08001423 ALOGE("Zip: invalid offset: %" PRId64 ", data length: %" PRId64, off, data_length_);
Adam Lesinskide117e42017-06-19 10:27:38 -07001424 return false;
1425 }
Elliott Hughesf66460b2019-10-22 11:44:50 -07001426 memcpy(buf, static_cast<const uint8_t*>(base_ptr_) + off, len);
Tianjie Xu18c25922016-09-29 15:27:41 -07001427 }
Adam Lesinskide117e42017-06-19 10:27:38 -07001428 return true;
Tianjie Xu18c25922016-09-29 15:27:41 -07001429}
1430
Elliott Hughesf66460b2019-10-22 11:44:50 -07001431void CentralDirectory::Initialize(const void* map_base_ptr, off64_t cd_start_offset,
1432 size_t cd_size) {
1433 base_ptr_ = static_cast<const uint8_t*>(map_base_ptr) + cd_start_offset;
Tianjie Xu18c25922016-09-29 15:27:41 -07001434 length_ = cd_size;
1435}
1436
Elliott Hughese8f4b142018-10-19 16:09:39 -07001437bool ZipArchive::InitializeCentralDirectory(off64_t cd_start_offset, size_t cd_size) {
Tianjie Xu18c25922016-09-29 15:27:41 -07001438 if (mapped_zip.HasFd()) {
Elliott Hughese8f4b142018-10-19 16:09:39 -07001439 directory_map = android::base::MappedFile::FromFd(mapped_zip.GetFileDescriptor(),
Ryan Mitchell23150e42020-03-09 09:33:46 -07001440 mapped_zip.GetFileOffset() + cd_start_offset,
1441 cd_size, PROT_READ);
Elliott Hughesfba2a1a2019-12-16 16:16:16 -08001442 if (!directory_map) {
1443 ALOGE("Zip: failed to map central directory (offset %" PRId64 ", size %zu): %s",
1444 cd_start_offset, cd_size, strerror(errno));
1445 return false;
1446 }
Tianjie Xu18c25922016-09-29 15:27:41 -07001447
Elliott Hughese8f4b142018-10-19 16:09:39 -07001448 CHECK_EQ(directory_map->size(), cd_size);
1449 central_directory.Initialize(directory_map->data(), 0 /*offset*/, cd_size);
Tianjie Xu18c25922016-09-29 15:27:41 -07001450 } else {
1451 if (mapped_zip.GetBasePtr() == nullptr) {
Elliott Hughesfba2a1a2019-12-16 16:16:16 -08001452 ALOGE("Zip: Failed to map central directory, bad mapped_zip base pointer");
Tianjie Xu18c25922016-09-29 15:27:41 -07001453 return false;
1454 }
1455 if (static_cast<off64_t>(cd_start_offset) + static_cast<off64_t>(cd_size) >
1456 mapped_zip.GetFileLength()) {
Jiyong Parkcd997e62017-06-30 17:23:33 +09001457 ALOGE(
1458 "Zip: Failed to map central directory, offset exceeds mapped memory region ("
1459 "start_offset %" PRId64 ", cd_size %zu, mapped_region_size %" PRId64 ")",
1460 static_cast<int64_t>(cd_start_offset), cd_size, mapped_zip.GetFileLength());
Tianjie Xu18c25922016-09-29 15:27:41 -07001461 return false;
1462 }
1463
1464 central_directory.Initialize(mapped_zip.GetBasePtr(), cd_start_offset, cd_size);
1465 }
1466 return true;
1467}
Elliott Hughes55fd2932017-05-28 22:59:04 -07001468
1469tm ZipEntry::GetModificationTime() const {
1470 tm t = {};
1471
1472 t.tm_hour = (mod_time >> 11) & 0x1f;
1473 t.tm_min = (mod_time >> 5) & 0x3f;
1474 t.tm_sec = (mod_time & 0x1f) << 1;
1475
1476 t.tm_year = ((mod_time >> 25) & 0x7f) + 80;
1477 t.tm_mon = ((mod_time >> 21) & 0xf) - 1;
1478 t.tm_mday = (mod_time >> 16) & 0x1f;
1479
1480 return t;
1481}