blob: e9662958ffff710500b33eecf89cbae46b13bf8c [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>
Ryan Mitchellc77f9d32018-08-25 14:06:29 -070050#include <android-base/utf8.h>
Mark Salyzyncfd5b082016-10-17 14:28:00 -070051#include <log/log.h>
Dan Albert1ae07642015-04-09 14:11:18 -070052#include "zlib.h"
Narayan Kamath7462f022013-11-21 13:05:04 +000053
Narayan Kamath044bc8e2014-12-03 18:22:53 +000054#include "entry_name_utils-inl.h"
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -070055#include "zip_archive_common.h"
Christopher Ferrise6884ce2015-11-10 14:55:12 -080056#include "zip_archive_private.h"
Mark Salyzyn99ef9912014-03-14 14:26:22 -070057
Dan Albert1ae07642015-04-09 14:11:18 -070058using android::base::get_unaligned;
Narayan Kamath044bc8e2014-12-03 18:22:53 +000059
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
Narayan Kamath7462f022013-11-21 13:05:04 +000067/*
68 * A Read-only Zip archive.
69 *
70 * We want "open" and "find entry by name" to be fast operations, and
71 * we want to use as little memory as possible. We memory-map the zip
72 * central directory, and load a hash table with pointers to the filenames
73 * (which aren't null-terminated). The other fields are at a fixed offset
74 * from the filename, so we don't need to extract those (but we do need
75 * to byte-read and endian-swap them every time we want them).
76 *
77 * It's possible that somebody has handed us a massive (~1GB) zip archive,
78 * so we can't expect to mmap the entire file.
79 *
80 * To speed comparisons when doing a lookup by name, we could make the mapping
81 * "private" (copy-on-write) and null-terminate the filenames after verifying
82 * the record structure. However, this requires a private mapping of
83 * every page that the Central Directory touches. Easier to tuck a copy
84 * of the string length into the hash table entry.
85 */
Narayan Kamath7462f022013-11-21 13:05:04 +000086
Narayan Kamath7462f022013-11-21 13:05:04 +000087/*
88 * Round up to the next highest power of 2.
89 *
90 * Found on http://graphics.stanford.edu/~seander/bithacks.html.
91 */
92static uint32_t RoundUpPower2(uint32_t val) {
93 val--;
94 val |= val >> 1;
95 val |= val >> 2;
96 val |= val >> 4;
97 val |= val >> 8;
98 val |= val >> 16;
99 val++;
100
101 return val;
102}
103
Yusuke Sato07447542015-06-25 14:39:19 -0700104static uint32_t ComputeHash(const ZipString& name) {
Nick Kralevichc0bf3662019-04-05 09:10:34 -0700105 return static_cast<uint32_t>(std::hash<std::string_view>{}(
106 std::string_view(reinterpret_cast<const char*>(name.name), name.name_length)));
Narayan Kamath7462f022013-11-21 13:05:04 +0000107}
108
Zimuzo5a503ef2018-09-17 19:49:55 +0100109static bool isZipStringEqual(const uint8_t* start, const ZipString& zip_string,
110 const ZipStringOffset& zip_string_offset) {
111 const ZipString from_offset = zip_string_offset.GetZipString(start);
112 return from_offset == zip_string;
113}
114
115/**
116 * Returns offset of ZipString#name from the start of the central directory in the memory map.
117 * For valid ZipStrings contained in the zip archive mmap, 0 < offset < 0xffffff.
118 */
119static inline uint32_t GetOffset(const uint8_t* name, const uint8_t* start) {
120 CHECK_GT(name, start);
121 CHECK_LT(name, start + 0xffffff);
122 return static_cast<uint32_t>(name - start);
123}
124
Narayan Kamath7462f022013-11-21 13:05:04 +0000125/*
126 * Convert a ZipEntry to a hash table index, verifying that it's in a
127 * valid range.
128 */
Zimuzo5a503ef2018-09-17 19:49:55 +0100129static int64_t EntryToIndex(const ZipStringOffset* hash_table, const uint32_t hash_table_size,
130 const ZipString& name, const uint8_t* start) {
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100131 const uint32_t hash = ComputeHash(name);
Narayan Kamath7462f022013-11-21 13:05:04 +0000132
133 // NOTE: (hash_table_size - 1) is guaranteed to be non-negative.
134 uint32_t ent = hash & (hash_table_size - 1);
Zimuzo5a503ef2018-09-17 19:49:55 +0100135 while (hash_table[ent].name_offset != 0) {
136 if (isZipStringEqual(start, name, hash_table[ent])) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000137 return ent;
138 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000139 ent = (ent + 1) & (hash_table_size - 1);
140 }
141
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100142 ALOGV("Zip: Unable to find entry %.*s", name.name_length, name.name);
Narayan Kamath7462f022013-11-21 13:05:04 +0000143 return kEntryNotFound;
144}
145
146/*
147 * Add a new entry to the hash table.
148 */
Andreas Gampe964b95c2019-04-05 13:48:02 -0700149static int32_t AddToHash(ZipStringOffset* hash_table, const uint32_t hash_table_size,
Zimuzo5a503ef2018-09-17 19:49:55 +0100150 const ZipString& name, const uint8_t* start) {
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100151 const uint64_t hash = ComputeHash(name);
Narayan Kamath7462f022013-11-21 13:05:04 +0000152 uint32_t ent = hash & (hash_table_size - 1);
153
154 /*
155 * We over-allocated the table, so we're guaranteed to find an empty slot.
156 * Further, we guarantee that the hashtable size is not 0.
157 */
Zimuzo5a503ef2018-09-17 19:49:55 +0100158 while (hash_table[ent].name_offset != 0) {
159 if (isZipStringEqual(start, name, hash_table[ent])) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000160 // We've found a duplicate entry. We don't accept it
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100161 ALOGW("Zip: Found duplicate entry %.*s", name.name_length, name.name);
Narayan Kamath7462f022013-11-21 13:05:04 +0000162 return kDuplicateEntry;
163 }
164 ent = (ent + 1) & (hash_table_size - 1);
165 }
Zimuzo5a503ef2018-09-17 19:49:55 +0100166 hash_table[ent].name_offset = GetOffset(name.name, start);
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100167 hash_table[ent].name_length = name.name_length;
Narayan Kamath7462f022013-11-21 13:05:04 +0000168 return 0;
169}
170
Josh Gaoabdfc242018-09-07 12:44:40 -0700171#if defined(__BIONIC__)
172uint64_t GetOwnerTag(const ZipArchive* archive) {
173 return android_fdsan_create_owner_tag(ANDROID_FDSAN_OWNER_TYPE_ZIPARCHIVE,
174 reinterpret_cast<uint64_t>(archive));
175}
176#endif
177
Josh Gao1b496342018-07-17 11:08:48 -0700178ZipArchive::ZipArchive(const int fd, bool assume_ownership)
179 : mapped_zip(fd),
180 close_file(assume_ownership),
181 directory_offset(0),
182 central_directory(),
Elliott Hughese8f4b142018-10-19 16:09:39 -0700183 directory_map(),
Josh Gao1b496342018-07-17 11:08:48 -0700184 num_entries(0),
185 hash_table_size(0),
186 hash_table(nullptr) {
187#if defined(__BIONIC__)
188 if (assume_ownership) {
Josh Gaoabdfc242018-09-07 12:44:40 -0700189 android_fdsan_exchange_owner_tag(fd, 0, GetOwnerTag(this));
Josh Gao1b496342018-07-17 11:08:48 -0700190 }
191#endif
192}
193
194ZipArchive::ZipArchive(void* address, size_t length)
195 : mapped_zip(address, length),
196 close_file(false),
197 directory_offset(0),
198 central_directory(),
Elliott Hughese8f4b142018-10-19 16:09:39 -0700199 directory_map(),
Josh Gao1b496342018-07-17 11:08:48 -0700200 num_entries(0),
201 hash_table_size(0),
202 hash_table(nullptr) {}
203
204ZipArchive::~ZipArchive() {
205 if (close_file && mapped_zip.GetFileDescriptor() >= 0) {
206#if defined(__BIONIC__)
Josh Gaoabdfc242018-09-07 12:44:40 -0700207 android_fdsan_close_with_tag(mapped_zip.GetFileDescriptor(), GetOwnerTag(this));
Josh Gao1b496342018-07-17 11:08:48 -0700208#else
209 close(mapped_zip.GetFileDescriptor());
210#endif
211 }
212
213 free(hash_table);
214}
215
Tianjie Xu18c25922016-09-29 15:27:41 -0700216static int32_t MapCentralDirectory0(const char* debug_file_name, ZipArchive* archive,
Andreas Gampe964b95c2019-04-05 13:48:02 -0700217 off64_t file_length, uint32_t read_amount,
Zimuzo5a503ef2018-09-17 19:49:55 +0100218 uint8_t* scan_buffer) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000219 const off64_t search_start = file_length - read_amount;
220
Jiyong Parkcd997e62017-06-30 17:23:33 +0900221 if (!archive->mapped_zip.ReadAtOffset(scan_buffer, read_amount, search_start)) {
222 ALOGE("Zip: read %" PRId64 " from offset %" PRId64 " failed", static_cast<int64_t>(read_amount),
223 static_cast<int64_t>(search_start));
Narayan Kamath7462f022013-11-21 13:05:04 +0000224 return kIoError;
225 }
226
227 /*
228 * Scan backward for the EOCD magic. In an archive without a trailing
229 * comment, we'll find it on the first try. (We may want to consider
230 * doing an initial minimal read; if we don't find it, retry with a
231 * second read as above.)
232 */
Andreas Gampe964b95c2019-04-05 13:48:02 -0700233 CHECK_LE(read_amount, std::numeric_limits<int32_t>::max());
234 int32_t i = read_amount - sizeof(EocdRecord);
Narayan Kamath926973e2014-06-09 14:18:14 +0100235 for (; i >= 0; i--) {
Dan Albert1ae07642015-04-09 14:11:18 -0700236 if (scan_buffer[i] == 0x50) {
237 uint32_t* sig_addr = reinterpret_cast<uint32_t*>(&scan_buffer[i]);
238 if (get_unaligned<uint32_t>(sig_addr) == EocdRecord::kSignature) {
239 ALOGV("+++ Found EOCD at buf+%d", i);
240 break;
241 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000242 }
243 }
244 if (i < 0) {
245 ALOGD("Zip: EOCD not found, %s is not zip", debug_file_name);
246 return kInvalidFile;
247 }
248
249 const off64_t eocd_offset = search_start + i;
Narayan Kamath926973e2014-06-09 14:18:14 +0100250 const EocdRecord* eocd = reinterpret_cast<const EocdRecord*>(scan_buffer + i);
Narayan Kamath7462f022013-11-21 13:05:04 +0000251 /*
Narayan Kamath926973e2014-06-09 14:18:14 +0100252 * Verify that there's no trailing space at the end of the central directory
253 * and its comment.
Narayan Kamath7462f022013-11-21 13:05:04 +0000254 */
Jiyong Parkcd997e62017-06-30 17:23:33 +0900255 const off64_t calculated_length = eocd_offset + sizeof(EocdRecord) + eocd->comment_length;
Narayan Kamath926973e2014-06-09 14:18:14 +0100256 if (calculated_length != file_length) {
Narayan Kamath4f6b4992014-06-03 13:59:23 +0100257 ALOGW("Zip: %" PRId64 " extraneous bytes at the end of the central directory",
Narayan Kamath926973e2014-06-09 14:18:14 +0100258 static_cast<int64_t>(file_length - calculated_length));
Narayan Kamath4f6b4992014-06-03 13:59:23 +0100259 return kInvalidFile;
260 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000261
Narayan Kamath926973e2014-06-09 14:18:14 +0100262 /*
263 * Grab the CD offset and size, and the number of entries in the
264 * archive and verify that they look reasonable.
265 */
Tianjie Xu1ee48922016-09-21 14:58:11 -0700266 if (static_cast<off64_t>(eocd->cd_start_offset) + eocd->cd_size > eocd_offset) {
Narayan Kamath926973e2014-06-09 14:18:14 +0100267 ALOGW("Zip: bad offsets (dir %" PRIu32 ", size %" PRIu32 ", eocd %" PRId64 ")",
Jiyong Parkcd997e62017-06-30 17:23:33 +0900268 eocd->cd_start_offset, eocd->cd_size, static_cast<int64_t>(eocd_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000269 return kInvalidOffset;
270 }
Narayan Kamath926973e2014-06-09 14:18:14 +0100271 if (eocd->num_records == 0) {
Adam Lesinskib354dce2018-03-01 21:32:13 +0000272#if defined(__ANDROID__)
Narayan Kamath7462f022013-11-21 13:05:04 +0000273 ALOGW("Zip: empty archive?");
Adam Lesinskib354dce2018-03-01 21:32:13 +0000274#endif
Narayan Kamath7462f022013-11-21 13:05:04 +0000275 return kEmptyArchive;
276 }
277
Jiyong Parkcd997e62017-06-30 17:23:33 +0900278 ALOGV("+++ num_entries=%" PRIu32 " dir_size=%" PRIu32 " dir_offset=%" PRIu32, eocd->num_records,
279 eocd->cd_size, eocd->cd_start_offset);
Narayan Kamath7462f022013-11-21 13:05:04 +0000280
281 /*
282 * It all looks good. Create a mapping for the CD, and set the fields
283 * in archive.
284 */
Tianjie Xu18c25922016-09-29 15:27:41 -0700285
Elliott Hughese8f4b142018-10-19 16:09:39 -0700286 if (!archive->InitializeCentralDirectory(static_cast<off64_t>(eocd->cd_start_offset),
Tianjie Xu18c25922016-09-29 15:27:41 -0700287 static_cast<size_t>(eocd->cd_size))) {
288 ALOGE("Zip: failed to intialize central directory.\n");
Narayan Kamatheaf98852013-12-11 14:51:51 +0000289 return kMmapFailed;
Narayan Kamath7462f022013-11-21 13:05:04 +0000290 }
291
Narayan Kamath926973e2014-06-09 14:18:14 +0100292 archive->num_entries = eocd->num_records;
293 archive->directory_offset = eocd->cd_start_offset;
Narayan Kamath7462f022013-11-21 13:05:04 +0000294
295 return 0;
296}
297
298/*
299 * Find the zip Central Directory and memory-map it.
300 *
301 * On success, returns 0 after populating fields from the EOCD area:
302 * directory_offset
Tianjie Xu18c25922016-09-29 15:27:41 -0700303 * directory_ptr
Narayan Kamath7462f022013-11-21 13:05:04 +0000304 * num_entries
305 */
Tianjie Xu18c25922016-09-29 15:27:41 -0700306static int32_t MapCentralDirectory(const char* debug_file_name, ZipArchive* archive) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000307 // Test file length. We use lseek64 to make sure the file
308 // is small enough to be a zip file (Its size must be less than
309 // 0xffffffff bytes).
Tianjie Xu18c25922016-09-29 15:27:41 -0700310 off64_t file_length = archive->mapped_zip.GetFileLength();
Narayan Kamath7462f022013-11-21 13:05:04 +0000311 if (file_length == -1) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000312 return kInvalidFile;
313 }
314
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800315 if (file_length > static_cast<off64_t>(0xffffffff)) {
Narayan Kamath926973e2014-06-09 14:18:14 +0100316 ALOGV("Zip: zip file too long %" PRId64, static_cast<int64_t>(file_length));
Narayan Kamath7462f022013-11-21 13:05:04 +0000317 return kInvalidFile;
318 }
319
Narayan Kamath926973e2014-06-09 14:18:14 +0100320 if (file_length < static_cast<off64_t>(sizeof(EocdRecord))) {
321 ALOGV("Zip: length %" PRId64 " is too small to be zip", static_cast<int64_t>(file_length));
Narayan Kamath7462f022013-11-21 13:05:04 +0000322 return kInvalidFile;
323 }
324
325 /*
326 * Perform the traditional EOCD snipe hunt.
327 *
328 * We're searching for the End of Central Directory magic number,
329 * which appears at the start of the EOCD block. It's followed by
330 * 18 bytes of EOCD stuff and up to 64KB of archive comment. We
331 * need to read the last part of the file into a buffer, dig through
332 * it to find the magic number, parse some values out, and use those
333 * to determine the extent of the CD.
334 *
335 * We start by pulling in the last part of the file.
336 */
Andreas Gampe964b95c2019-04-05 13:48:02 -0700337 uint32_t read_amount = kMaxEOCDSearch;
Narayan Kamath926973e2014-06-09 14:18:14 +0100338 if (file_length < read_amount) {
Andreas Gampe964b95c2019-04-05 13:48:02 -0700339 read_amount = static_cast<uint32_t>(file_length);
Narayan Kamath7462f022013-11-21 13:05:04 +0000340 }
341
Tianjie Xu18c25922016-09-29 15:27:41 -0700342 std::vector<uint8_t> scan_buffer(read_amount);
Jiyong Parkcd997e62017-06-30 17:23:33 +0900343 int32_t result =
344 MapCentralDirectory0(debug_file_name, archive, file_length, read_amount, scan_buffer.data());
Narayan Kamath7462f022013-11-21 13:05:04 +0000345 return result;
346}
347
348/*
349 * Parses the Zip archive's Central Directory. Allocates and populates the
350 * hash table.
351 *
352 * Returns 0 on success.
353 */
354static int32_t ParseZipArchive(ZipArchive* archive) {
Tianjie Xu18c25922016-09-29 15:27:41 -0700355 const uint8_t* const cd_ptr = archive->central_directory.GetBasePtr();
356 const size_t cd_length = archive->central_directory.GetMapLength();
Narayan Kamath926973e2014-06-09 14:18:14 +0100357 const uint16_t num_entries = archive->num_entries;
Narayan Kamath7462f022013-11-21 13:05:04 +0000358
359 /*
360 * Create hash table. We have a minimum 75% load factor, possibly as
361 * low as 50% after we round off to a power of 2. There must be at
362 * least one unused entry to avoid an infinite loop during creation.
363 */
364 archive->hash_table_size = RoundUpPower2(1 + (num_entries * 4) / 3);
Jiyong Parkcd997e62017-06-30 17:23:33 +0900365 archive->hash_table =
Zimuzo5a503ef2018-09-17 19:49:55 +0100366 reinterpret_cast<ZipStringOffset*>(calloc(archive->hash_table_size, sizeof(ZipStringOffset)));
Tianjie Xu9e020e22016-10-10 12:11:30 -0700367 if (archive->hash_table == nullptr) {
368 ALOGW("Zip: unable to allocate the %u-entry hash_table, entry size: %zu",
369 archive->hash_table_size, sizeof(ZipString));
370 return -1;
371 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000372
373 /*
374 * Walk through the central directory, adding entries to the hash
375 * table and verifying values.
376 */
Narayan Kamath926973e2014-06-09 14:18:14 +0100377 const uint8_t* const cd_end = cd_ptr + cd_length;
Narayan Kamath7462f022013-11-21 13:05:04 +0000378 const uint8_t* ptr = cd_ptr;
379 for (uint16_t i = 0; i < num_entries; i++) {
Tianjie Xu0fda1cf2017-04-05 14:46:27 -0700380 if (ptr > cd_end - sizeof(CentralDirectoryRecord)) {
381 ALOGW("Zip: ran off the end (at %" PRIu16 ")", i);
382#if defined(__ANDROID__)
383 android_errorWriteLog(0x534e4554, "36392138");
384#endif
385 return -1;
386 }
387
Jiyong Parkcd997e62017-06-30 17:23:33 +0900388 const CentralDirectoryRecord* cdr = reinterpret_cast<const CentralDirectoryRecord*>(ptr);
Narayan Kamath926973e2014-06-09 14:18:14 +0100389 if (cdr->record_signature != CentralDirectoryRecord::kSignature) {
Mark Salyzyn088bf902014-05-08 16:02:20 -0700390 ALOGW("Zip: missed a central dir sig (at %" PRIu16 ")", i);
Dmitriy Ivanov3ea93da2015-03-06 11:48:47 -0800391 return -1;
Narayan Kamath7462f022013-11-21 13:05:04 +0000392 }
393
Narayan Kamath926973e2014-06-09 14:18:14 +0100394 const off64_t local_header_offset = cdr->local_file_header_offset;
Narayan Kamath7462f022013-11-21 13:05:04 +0000395 if (local_header_offset >= archive->directory_offset) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800396 ALOGW("Zip: bad LFH offset %" PRId64 " at entry %" PRIu16,
Jiyong Parkcd997e62017-06-30 17:23:33 +0900397 static_cast<int64_t>(local_header_offset), i);
Dmitriy Ivanov3ea93da2015-03-06 11:48:47 -0800398 return -1;
Narayan Kamath7462f022013-11-21 13:05:04 +0000399 }
400
Narayan Kamath926973e2014-06-09 14:18:14 +0100401 const uint16_t file_name_length = cdr->file_name_length;
402 const uint16_t extra_length = cdr->extra_field_length;
403 const uint16_t comment_length = cdr->comment_length;
Piotr Jastrzebski78271ba2014-08-15 12:53:00 +0100404 const uint8_t* file_name = ptr + sizeof(CentralDirectoryRecord);
405
Tianjie Xu9e020e22016-10-10 12:11:30 -0700406 if (file_name + file_name_length > cd_end) {
Jiyong Parkcd997e62017-06-30 17:23:33 +0900407 ALOGW(
408 "Zip: file name boundary exceeds the central directory range, file_name_length: "
409 "%" PRIx16 ", cd_length: %zu",
410 file_name_length, cd_length);
Tianjie Xu9e020e22016-10-10 12:11:30 -0700411 return -1;
412 }
Narayan Kamath044bc8e2014-12-03 18:22:53 +0000413 /* check that file name is valid UTF-8 and doesn't contain NUL (U+0000) characters */
414 if (!IsValidEntryName(file_name, file_name_length)) {
Dmitriy Ivanov3ea93da2015-03-06 11:48:47 -0800415 return -1;
Piotr Jastrzebski78271ba2014-08-15 12:53:00 +0100416 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000417
418 /* add the CDE filename to the hash table */
Yusuke Sato07447542015-06-25 14:39:19 -0700419 ZipString entry_name;
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100420 entry_name.name = file_name;
421 entry_name.name_length = file_name_length;
Zimuzo5a503ef2018-09-17 19:49:55 +0100422 const int add_result = AddToHash(archive->hash_table, archive->hash_table_size, entry_name,
423 archive->central_directory.GetBasePtr());
Dmitriy Ivanov3ea93da2015-03-06 11:48:47 -0800424 if (add_result != 0) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000425 ALOGW("Zip: Error adding entry to hash table %d", add_result);
Dmitriy Ivanov3ea93da2015-03-06 11:48:47 -0800426 return add_result;
Narayan Kamath7462f022013-11-21 13:05:04 +0000427 }
428
Narayan Kamath926973e2014-06-09 14:18:14 +0100429 ptr += sizeof(CentralDirectoryRecord) + file_name_length + extra_length + comment_length;
430 if ((ptr - cd_ptr) > static_cast<int64_t>(cd_length)) {
Jiyong Parkcd997e62017-06-30 17:23:33 +0900431 ALOGW("Zip: bad CD advance (%tu vs %zu) at entry %" PRIu16, ptr - cd_ptr, cd_length, i);
Dmitriy Ivanov3ea93da2015-03-06 11:48:47 -0800432 return -1;
Narayan Kamath7462f022013-11-21 13:05:04 +0000433 }
434 }
Narayan Kamathc1a56dc2017-08-09 18:32:09 +0100435
436 uint32_t lfh_start_bytes;
437 if (!archive->mapped_zip.ReadAtOffset(reinterpret_cast<uint8_t*>(&lfh_start_bytes),
438 sizeof(uint32_t), 0)) {
439 ALOGW("Zip: Unable to read header for entry at offset == 0.");
440 return -1;
441 }
442
443 if (lfh_start_bytes != LocalFileHeader::kSignature) {
444 ALOGW("Zip: Entry at offset zero has invalid LFH signature %" PRIx32, lfh_start_bytes);
445#if defined(__ANDROID__)
446 android_errorWriteLog(0x534e4554, "64211847");
447#endif
448 return -1;
449 }
450
Mark Salyzyn088bf902014-05-08 16:02:20 -0700451 ALOGV("+++ zip good scan %" PRIu16 " entries", num_entries);
Narayan Kamath7462f022013-11-21 13:05:04 +0000452
Dmitriy Ivanov3ea93da2015-03-06 11:48:47 -0800453 return 0;
Narayan Kamath7462f022013-11-21 13:05:04 +0000454}
455
Jiyong Parkcd997e62017-06-30 17:23:33 +0900456static int32_t OpenArchiveInternal(ZipArchive* archive, const char* debug_file_name) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000457 int32_t result = -1;
Tianjie Xu18c25922016-09-29 15:27:41 -0700458 if ((result = MapCentralDirectory(debug_file_name, archive)) != 0) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000459 return result;
460 }
461
462 if ((result = ParseZipArchive(archive))) {
463 return result;
464 }
465
466 return 0;
467}
468
Jiyong Parkcd997e62017-06-30 17:23:33 +0900469int32_t OpenArchiveFd(int fd, const char* debug_file_name, ZipArchiveHandle* handle,
470 bool assume_ownership) {
Dmitriy Ivanov40b52b22014-07-15 19:33:00 -0700471 ZipArchive* archive = new ZipArchive(fd, assume_ownership);
Narayan Kamath7462f022013-11-21 13:05:04 +0000472 *handle = archive;
Narayan Kamath7462f022013-11-21 13:05:04 +0000473 return OpenArchiveInternal(archive, debug_file_name);
474}
475
476int32_t OpenArchive(const char* fileName, ZipArchiveHandle* handle) {
Nick Kralevich3bdf7442018-12-18 12:48:06 -0800477 const int fd = ::android::base::utf8::open(fileName, O_RDONLY | O_BINARY | O_CLOEXEC, 0);
Dmitriy Ivanov40b52b22014-07-15 19:33:00 -0700478 ZipArchive* archive = new ZipArchive(fd, true);
Narayan Kamath7462f022013-11-21 13:05:04 +0000479 *handle = archive;
480
Narayan Kamath7462f022013-11-21 13:05:04 +0000481 if (fd < 0) {
482 ALOGW("Unable to open '%s': %s", fileName, strerror(errno));
483 return kIoError;
Narayan Kamath7462f022013-11-21 13:05:04 +0000484 }
Dmitriy Ivanov40b52b22014-07-15 19:33:00 -0700485
Narayan Kamath7462f022013-11-21 13:05:04 +0000486 return OpenArchiveInternal(archive, fileName);
487}
488
Tianjie Xu18c25922016-09-29 15:27:41 -0700489int32_t OpenArchiveFromMemory(void* address, size_t length, const char* debug_file_name,
Jiyong Parkcd997e62017-06-30 17:23:33 +0900490 ZipArchiveHandle* handle) {
Tianjie Xu18c25922016-09-29 15:27:41 -0700491 ZipArchive* archive = new ZipArchive(address, length);
492 *handle = archive;
493 return OpenArchiveInternal(archive, debug_file_name);
494}
495
Narayan Kamath7462f022013-11-21 13:05:04 +0000496/*
497 * Close a ZipArchive, closing the file and freeing the contents.
498 */
Ryan Prichard3673f992018-10-10 22:41:14 -0700499void CloseArchive(ZipArchiveHandle archive) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000500 ALOGV("Closing archive %p", archive);
Neil Fullerb1a113f2014-07-25 14:43:04 +0100501 delete archive;
Narayan Kamath7462f022013-11-21 13:05:04 +0000502}
503
Narayan Kamath162b7052017-06-05 13:21:12 +0100504static int32_t ValidateDataDescriptor(MappedZipFile& mapped_zip, ZipEntry* entry) {
Narayan Kamath926973e2014-06-09 14:18:14 +0100505 uint8_t ddBuf[sizeof(DataDescriptor) + sizeof(DataDescriptor::kOptSignature)];
Adam Lesinskide117e42017-06-19 10:27:38 -0700506 off64_t offset = entry->offset;
507 if (entry->method != kCompressStored) {
508 offset += entry->compressed_length;
509 } else {
510 offset += entry->uncompressed_length;
511 }
512
513 if (!mapped_zip.ReadAtOffset(ddBuf, sizeof(ddBuf), offset)) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000514 return kIoError;
515 }
516
Narayan Kamath926973e2014-06-09 14:18:14 +0100517 const uint32_t ddSignature = *(reinterpret_cast<const uint32_t*>(ddBuf));
Adam Lesinskide117e42017-06-19 10:27:38 -0700518 const uint16_t ddOffset = (ddSignature == DataDescriptor::kOptSignature) ? 4 : 0;
519 const DataDescriptor* descriptor = reinterpret_cast<const DataDescriptor*>(ddBuf + ddOffset);
Narayan Kamath7462f022013-11-21 13:05:04 +0000520
Narayan Kamath162b7052017-06-05 13:21:12 +0100521 // Validate that the values in the data descriptor match those in the central
522 // directory.
523 if (entry->compressed_length != descriptor->compressed_size ||
524 entry->uncompressed_length != descriptor->uncompressed_size ||
525 entry->crc32 != descriptor->crc32) {
526 ALOGW("Zip: size/crc32 mismatch. expected {%" PRIu32 ", %" PRIu32 ", %" PRIx32
527 "}, was {%" PRIu32 ", %" PRIu32 ", %" PRIx32 "}",
528 entry->compressed_length, entry->uncompressed_length, entry->crc32,
529 descriptor->compressed_size, descriptor->uncompressed_size, descriptor->crc32);
530 return kInconsistentInformation;
531 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000532
533 return 0;
534}
535
Andreas Gampe964b95c2019-04-05 13:48:02 -0700536static int32_t FindEntry(const ZipArchive* archive, const int32_t ent, ZipEntry* data) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000537 const uint16_t nameLen = archive->hash_table[ent].name_length;
Narayan Kamath7462f022013-11-21 13:05:04 +0000538
539 // Recover the start of the central directory entry from the filename
540 // pointer. The filename is the first entry past the fixed-size data,
541 // so we can just subtract back from that.
Zimuzo5a503ef2018-09-17 19:49:55 +0100542 const ZipString from_offset =
543 archive->hash_table[ent].GetZipString(archive->central_directory.GetBasePtr());
544 const uint8_t* ptr = from_offset.name;
Narayan Kamath926973e2014-06-09 14:18:14 +0100545 ptr -= sizeof(CentralDirectoryRecord);
Narayan Kamath7462f022013-11-21 13:05:04 +0000546
547 // This is the base of our mmapped region, we have to sanity check that
548 // the name that's in the hash table is a pointer to a location within
549 // this mapped region.
Tianjie Xu18c25922016-09-29 15:27:41 -0700550 const uint8_t* base_ptr = archive->central_directory.GetBasePtr();
551 if (ptr < base_ptr || ptr > base_ptr + archive->central_directory.GetMapLength()) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000552 ALOGW("Zip: Invalid entry pointer");
553 return kInvalidOffset;
554 }
555
Jiyong Parkcd997e62017-06-30 17:23:33 +0900556 const CentralDirectoryRecord* cdr = reinterpret_cast<const CentralDirectoryRecord*>(ptr);
Narayan Kamath926973e2014-06-09 14:18:14 +0100557
Narayan Kamath7462f022013-11-21 13:05:04 +0000558 // The offset of the start of the central directory in the zipfile.
559 // We keep this lying around so that we can sanity check all our lengths
560 // and our per-file structures.
561 const off64_t cd_offset = archive->directory_offset;
562
563 // Fill out the compression method, modification time, crc32
564 // and other interesting attributes from the central directory. These
565 // will later be compared against values from the local file header.
Narayan Kamath926973e2014-06-09 14:18:14 +0100566 data->method = cdr->compression_method;
beonit0e99a2f2015-07-18 02:08:16 +0900567 data->mod_time = cdr->last_mod_date << 16 | cdr->last_mod_time;
Narayan Kamath926973e2014-06-09 14:18:14 +0100568 data->crc32 = cdr->crc32;
569 data->compressed_length = cdr->compressed_size;
570 data->uncompressed_length = cdr->uncompressed_size;
Narayan Kamath7462f022013-11-21 13:05:04 +0000571
572 // Figure out the local header offset from the central directory. The
573 // actual file data will begin after the local header and the name /
574 // extra comments.
Narayan Kamath926973e2014-06-09 14:18:14 +0100575 const off64_t local_header_offset = cdr->local_file_header_offset;
576 if (local_header_offset + static_cast<off64_t>(sizeof(LocalFileHeader)) >= cd_offset) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000577 ALOGW("Zip: bad local hdr offset in zip");
578 return kInvalidOffset;
579 }
580
Narayan Kamath926973e2014-06-09 14:18:14 +0100581 uint8_t lfh_buf[sizeof(LocalFileHeader)];
Tianjie Xu18c25922016-09-29 15:27:41 -0700582 if (!archive->mapped_zip.ReadAtOffset(lfh_buf, sizeof(lfh_buf), local_header_offset)) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800583 ALOGW("Zip: failed reading lfh name from offset %" PRId64,
Jiyong Parkcd997e62017-06-30 17:23:33 +0900584 static_cast<int64_t>(local_header_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000585 return kIoError;
586 }
587
Jiyong Parkcd997e62017-06-30 17:23:33 +0900588 const LocalFileHeader* lfh = reinterpret_cast<const LocalFileHeader*>(lfh_buf);
Narayan Kamath926973e2014-06-09 14:18:14 +0100589
590 if (lfh->lfh_signature != LocalFileHeader::kSignature) {
Mark Salyzyn99ef9912014-03-14 14:26:22 -0700591 ALOGW("Zip: didn't find signature at start of lfh, offset=%" PRId64,
Jiyong Parkcd997e62017-06-30 17:23:33 +0900592 static_cast<int64_t>(local_header_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000593 return kInvalidOffset;
594 }
595
596 // Paranoia: Match the values specified in the local file header
597 // to those specified in the central directory.
Adam Lesinskid987c9d2017-04-06 18:55:47 -0700598
Narayan Kamath162b7052017-06-05 13:21:12 +0100599 // Warn if central directory and local file header don't agree on the use
600 // of a trailing Data Descriptor. The reference implementation is inconsistent
601 // and appears to use the LFH value during extraction (unzip) but the CD value
602 // while displayng information about archives (zipinfo). The spec remains
603 // silent on this inconsistency as well.
604 //
605 // For now, always use the version from the LFH but make sure that the values
606 // specified in the central directory match those in the data descriptor.
607 //
608 // NOTE: It's also worth noting that unzip *does* warn about inconsistencies in
609 // bit 11 (EFS: The language encoding flag, marking that filename and comment are
610 // encoded using UTF-8). This implementation does not check for the presence of
611 // that flag and always enforces that entry names are valid UTF-8.
612 if ((lfh->gpb_flags & kGPBDDFlagMask) != (cdr->gpb_flags & kGPBDDFlagMask)) {
613 ALOGW("Zip: gpb flag mismatch at bit 3. expected {%04" PRIx16 "}, was {%04" PRIx16 "}",
Adam Lesinskid987c9d2017-04-06 18:55:47 -0700614 cdr->gpb_flags, lfh->gpb_flags);
Adam Lesinskid987c9d2017-04-06 18:55:47 -0700615 }
616
617 // If there is no trailing data descriptor, verify that the central directory and local file
618 // header agree on the crc, compressed, and uncompressed sizes of the entry.
Narayan Kamath926973e2014-06-09 14:18:14 +0100619 if ((lfh->gpb_flags & kGPBDDFlagMask) == 0) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000620 data->has_data_descriptor = 0;
Jiyong Parkcd997e62017-06-30 17:23:33 +0900621 if (data->compressed_length != lfh->compressed_size ||
622 data->uncompressed_length != lfh->uncompressed_size || data->crc32 != lfh->crc32) {
623 ALOGW("Zip: size/crc32 mismatch. expected {%" PRIu32 ", %" PRIu32 ", %" PRIx32
624 "}, was {%" PRIu32 ", %" PRIu32 ", %" PRIx32 "}",
625 data->compressed_length, data->uncompressed_length, data->crc32, lfh->compressed_size,
626 lfh->uncompressed_size, lfh->crc32);
Narayan Kamath7462f022013-11-21 13:05:04 +0000627 return kInconsistentInformation;
628 }
629 } else {
630 data->has_data_descriptor = 1;
631 }
632
Elliott Hughes55fd2932017-05-28 22:59:04 -0700633 // 4.4.2.1: the upper byte of `version_made_by` gives the source OS. Unix is 3.
634 if ((cdr->version_made_by >> 8) == 3) {
635 data->unix_mode = (cdr->external_file_attributes >> 16) & 0xffff;
636 } else {
637 data->unix_mode = 0777;
638 }
639
Narayan Kamath7462f022013-11-21 13:05:04 +0000640 // Check that the local file header name matches the declared
641 // name in the central directory.
Narayan Kamath926973e2014-06-09 14:18:14 +0100642 if (lfh->file_name_length == nameLen) {
643 const off64_t name_offset = local_header_offset + sizeof(LocalFileHeader);
Mykola Kondratenko50afc152014-09-08 12:46:37 +0200644 if (name_offset + lfh->file_name_length > cd_offset) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000645 ALOGW("Zip: Invalid declared length");
646 return kInvalidOffset;
647 }
648
Tianjie Xu18c25922016-09-29 15:27:41 -0700649 std::vector<uint8_t> name_buf(nameLen);
650 if (!archive->mapped_zip.ReadAtOffset(name_buf.data(), nameLen, name_offset)) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800651 ALOGW("Zip: failed reading lfh name from offset %" PRId64, static_cast<int64_t>(name_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000652 return kIoError;
653 }
Zimuzo5a503ef2018-09-17 19:49:55 +0100654 const ZipString from_offset =
655 archive->hash_table[ent].GetZipString(archive->central_directory.GetBasePtr());
656 if (memcmp(from_offset.name, name_buf.data(), nameLen)) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000657 return kInconsistentInformation;
658 }
659
Narayan Kamath7462f022013-11-21 13:05:04 +0000660 } else {
661 ALOGW("Zip: lfh name did not match central directory.");
662 return kInconsistentInformation;
663 }
664
Jiyong Parkcd997e62017-06-30 17:23:33 +0900665 const off64_t data_offset = local_header_offset + sizeof(LocalFileHeader) +
666 lfh->file_name_length + lfh->extra_field_length;
Narayan Kamath48953a12014-01-24 12:32:39 +0000667 if (data_offset > cd_offset) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800668 ALOGW("Zip: bad data offset %" PRId64 " in zip", static_cast<int64_t>(data_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000669 return kInvalidOffset;
670 }
671
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800672 if (static_cast<off64_t>(data_offset + data->compressed_length) > cd_offset) {
Mark Salyzyn088bf902014-05-08 16:02:20 -0700673 ALOGW("Zip: bad compressed length in zip (%" PRId64 " + %" PRIu32 " > %" PRId64 ")",
Jiyong Parkcd997e62017-06-30 17:23:33 +0900674 static_cast<int64_t>(data_offset), data->compressed_length,
675 static_cast<int64_t>(cd_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000676 return kInvalidOffset;
677 }
678
679 if (data->method == kCompressStored &&
Jiyong Parkcd997e62017-06-30 17:23:33 +0900680 static_cast<off64_t>(data_offset + data->uncompressed_length) > cd_offset) {
681 ALOGW("Zip: bad uncompressed length in zip (%" PRId64 " + %" PRIu32 " > %" PRId64 ")",
682 static_cast<int64_t>(data_offset), data->uncompressed_length,
683 static_cast<int64_t>(cd_offset));
684 return kInvalidOffset;
Narayan Kamath7462f022013-11-21 13:05:04 +0000685 }
686
687 data->offset = data_offset;
688 return 0;
689}
690
691struct IterationHandle {
Narayan Kamath7462f022013-11-21 13:05:04 +0000692 ZipArchive* archive;
Piotr Jastrzebski8e085362014-08-18 11:37:45 +0100693
Elliott Hughesa22ac0f2019-05-08 10:44:06 -0700694 std::string prefix_holder;
695 ZipString prefix;
Piotr Jastrzebski8e085362014-08-18 11:37:45 +0100696
Elliott Hughesa22ac0f2019-05-08 10:44:06 -0700697 std::string suffix_holder;
698 ZipString suffix;
699
700 uint32_t position = 0;
701
702 IterationHandle(ZipArchive* archive, const std::string_view in_prefix,
703 const std::string_view in_suffix)
704 : archive(archive),
705 prefix_holder(in_prefix),
706 prefix(prefix_holder),
707 suffix_holder(in_suffix),
708 suffix(suffix_holder) {}
Narayan Kamath7462f022013-11-21 13:05:04 +0000709};
710
Ryan Prichard3673f992018-10-10 22:41:14 -0700711int32_t StartIteration(ZipArchiveHandle archive, void** cookie_ptr,
Elliott Hughesa22ac0f2019-05-08 10:44:06 -0700712 const std::string_view optional_prefix,
713 const std::string_view optional_suffix) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000714 if (archive == NULL || archive->hash_table == NULL) {
715 ALOGW("Zip: Invalid ZipArchiveHandle");
716 return kInvalidHandle;
717 }
718
Elliott Hughesa22ac0f2019-05-08 10:44:06 -0700719 if (optional_prefix.size() > static_cast<size_t>(UINT16_MAX) ||
720 optional_suffix.size() > static_cast<size_t>(UINT16_MAX)) {
721 ALOGW("Zip: prefix/suffix too long");
722 return kInvalidEntryName;
723 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000724
Elliott Hughesa22ac0f2019-05-08 10:44:06 -0700725 *cookie_ptr = new IterationHandle(archive, optional_prefix, optional_suffix);
Narayan Kamath7462f022013-11-21 13:05:04 +0000726 return 0;
727}
728
Piotr Jastrzebski79c8b342014-08-08 14:02:17 +0100729void EndIteration(void* cookie) {
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100730 delete reinterpret_cast<IterationHandle*>(cookie);
Piotr Jastrzebski79c8b342014-08-08 14:02:17 +0100731}
732
Elliott Hughesb17bf522019-05-03 22:38:44 -0700733int32_t FindEntry(const ZipArchiveHandle archive, const std::string_view entryName,
734 ZipEntry* data) {
735 if (entryName.empty() || entryName.size() > static_cast<size_t>(UINT16_MAX)) {
736 ALOGW("Zip: Invalid filename of length %zu", entryName.size());
737 return kInvalidEntryName;
738 }
739
740 const int64_t ent = EntryToIndex(archive->hash_table, archive->hash_table_size,
741 ZipString(entryName), archive->central_directory.GetBasePtr());
742 if (ent < 0) {
743 ALOGV("Zip: Could not find entry %.*s", static_cast<int>(entryName.size()), entryName.data());
744 return static_cast<int32_t>(ent); // kEntryNotFound is safe to truncate.
745 }
Elliott Hughesa5ff19e2019-05-07 09:27:59 -0700746 // We know there are at most hash_table_size entries, safe to truncate.
Elliott Hughesb17bf522019-05-03 22:38:44 -0700747 return FindEntry(archive, static_cast<uint32_t>(ent), data);
748}
749
Elliott Hughese06a8082019-05-22 18:56:41 -0700750int32_t Next(void* cookie, ZipEntry* data, std::string* name) {
Elliott Hughes1e40c302019-06-12 12:12:47 -0700751 std::string_view sv;
752 int32_t result = Next(cookie, data, &sv);
753 if (result == 0 && name) {
754 *name = std::string(sv);
755 }
756 return result;
757}
758
759int32_t Next(void* cookie, ZipEntry* data, std::string_view* name) {
Elliott Hughese06a8082019-05-22 18:56:41 -0700760 ZipString zs;
761 int32_t result = Next(cookie, data, &zs);
Elliott Hughes1e40c302019-06-12 12:12:47 -0700762 if (result == 0 && name) {
763 *name = std::string_view(reinterpret_cast<const char*>(zs.name), zs.name_length);
Elliott Hughese06a8082019-05-22 18:56:41 -0700764 }
765 return result;
766}
767
Yusuke Sato07447542015-06-25 14:39:19 -0700768int32_t Next(void* cookie, ZipEntry* data, ZipString* name) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800769 IterationHandle* handle = reinterpret_cast<IterationHandle*>(cookie);
Narayan Kamath7462f022013-11-21 13:05:04 +0000770 if (handle == NULL) {
Zimuzo5a503ef2018-09-17 19:49:55 +0100771 ALOGW("Zip: Null ZipArchiveHandle");
Narayan Kamath7462f022013-11-21 13:05:04 +0000772 return kInvalidHandle;
773 }
774
775 ZipArchive* archive = handle->archive;
776 if (archive == NULL || archive->hash_table == NULL) {
777 ALOGW("Zip: Invalid ZipArchiveHandle");
778 return kInvalidHandle;
779 }
780
781 const uint32_t currentOffset = handle->position;
782 const uint32_t hash_table_length = archive->hash_table_size;
Zimuzo5a503ef2018-09-17 19:49:55 +0100783 const ZipStringOffset* hash_table = archive->hash_table;
Narayan Kamath7462f022013-11-21 13:05:04 +0000784 for (uint32_t i = currentOffset; i < hash_table_length; ++i) {
Zimuzo5a503ef2018-09-17 19:49:55 +0100785 const ZipString from_offset =
786 hash_table[i].GetZipString(archive->central_directory.GetBasePtr());
787 if (hash_table[i].name_offset != 0 &&
788 (handle->prefix.name_length == 0 || from_offset.StartsWith(handle->prefix)) &&
789 (handle->suffix.name_length == 0 || from_offset.EndsWith(handle->suffix))) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000790 handle->position = (i + 1);
791 const int error = FindEntry(archive, i, data);
792 if (!error) {
Zimuzo5a503ef2018-09-17 19:49:55 +0100793 name->name = from_offset.name;
Narayan Kamath7462f022013-11-21 13:05:04 +0000794 name->name_length = hash_table[i].name_length;
795 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000796 return error;
797 }
798 }
799
800 handle->position = 0;
801 return kIterationEnd;
802}
803
Narayan Kamathf899bd52015-04-17 11:53:14 +0100804// A Writer that writes data to a fixed size memory region.
805// The size of the memory region must be equal to the total size of
806// the data appended to it.
Narayan Kamath485b3642017-10-26 14:42:39 +0100807class MemoryWriter : public zip_archive::Writer {
Narayan Kamathf899bd52015-04-17 11:53:14 +0100808 public:
Jiyong Parkcd997e62017-06-30 17:23:33 +0900809 MemoryWriter(uint8_t* buf, size_t size) : Writer(), buf_(buf), size_(size), bytes_written_(0) {}
Narayan Kamathf899bd52015-04-17 11:53:14 +0100810
811 virtual bool Append(uint8_t* buf, size_t buf_size) override {
812 if (bytes_written_ + buf_size > size_) {
Elliott Hughese8f4b142018-10-19 16:09:39 -0700813 ALOGW("Zip: Unexpected size %zu (declared) vs %zu (actual)", size_,
Jiyong Parkcd997e62017-06-30 17:23:33 +0900814 bytes_written_ + buf_size);
Narayan Kamathf899bd52015-04-17 11:53:14 +0100815 return false;
816 }
817
818 memcpy(buf_ + bytes_written_, buf, buf_size);
819 bytes_written_ += buf_size;
820 return true;
821 }
822
823 private:
824 uint8_t* const buf_;
825 const size_t size_;
826 size_t bytes_written_;
827};
828
829// A Writer that appends data to a file |fd| at its current position.
830// The file will be truncated to the end of the written data.
Narayan Kamath485b3642017-10-26 14:42:39 +0100831class FileWriter : public zip_archive::Writer {
Narayan Kamathf899bd52015-04-17 11:53:14 +0100832 public:
Narayan Kamathf899bd52015-04-17 11:53:14 +0100833 // Creates a FileWriter for |fd| and prepare to write |entry| to it,
834 // guaranteeing that the file descriptor is valid and that there's enough
835 // space on the volume to write out the entry completely and that the file
Tao Baoa456c212016-11-15 10:08:07 -0800836 // is truncated to the correct length (no truncation if |fd| references a
837 // block device).
Narayan Kamathf899bd52015-04-17 11:53:14 +0100838 //
839 // Returns a valid FileWriter on success, |nullptr| if an error occurred.
Yurii Zubrytskyi834326c2017-12-20 01:01:01 -0800840 static FileWriter Create(int fd, const ZipEntry* entry) {
Narayan Kamathf899bd52015-04-17 11:53:14 +0100841 const uint32_t declared_length = entry->uncompressed_length;
842 const off64_t current_offset = lseek64(fd, 0, SEEK_CUR);
843 if (current_offset == -1) {
844 ALOGW("Zip: unable to seek to current location on fd %d: %s", fd, strerror(errno));
Yurii Zubrytskyi834326c2017-12-20 01:01:01 -0800845 return FileWriter{};
Narayan Kamathf899bd52015-04-17 11:53:14 +0100846 }
847
Narayan Kamathf899bd52015-04-17 11:53:14 +0100848#if defined(__linux__)
849 if (declared_length > 0) {
850 // Make sure we have enough space on the volume to extract the compressed
851 // entry. Note that the call to ftruncate below will change the file size but
852 // will not allocate space on disk and this call to fallocate will not
853 // change the file size.
Badhri Jagan Sridharana68d0d12015-06-02 14:47:57 -0700854 // Note: fallocate is only supported by the following filesystems -
855 // btrfs, ext4, ocfs2, and xfs. Therefore fallocate might fail with
856 // EOPNOTSUPP error when issued in other filesystems.
857 // Hence, check for the return error code before concluding that the
858 // disk does not have enough space.
Andreas Gampe964b95c2019-04-05 13:48:02 -0700859 long result = TEMP_FAILURE_RETRY(fallocate(fd, 0, current_offset, declared_length));
Badhri Jagan Sridharana68d0d12015-06-02 14:47:57 -0700860 if (result == -1 && errno == ENOSPC) {
Elliott Hughes4089d342017-10-27 14:21:12 -0700861 ALOGW("Zip: unable to allocate %" PRId64 " bytes at offset %" PRId64 ": %s",
Narayan Kamathd5d7abe2016-08-10 12:24:05 +0100862 static_cast<int64_t>(declared_length), static_cast<int64_t>(current_offset),
863 strerror(errno));
Yurii Zubrytskyi834326c2017-12-20 01:01:01 -0800864 return FileWriter{};
Narayan Kamathf899bd52015-04-17 11:53:14 +0100865 }
866 }
867#endif // __linux__
868
Tao Baoa456c212016-11-15 10:08:07 -0800869 struct stat sb;
870 if (fstat(fd, &sb) == -1) {
871 ALOGW("Zip: unable to fstat file: %s", strerror(errno));
Yurii Zubrytskyi834326c2017-12-20 01:01:01 -0800872 return FileWriter{};
Narayan Kamathf899bd52015-04-17 11:53:14 +0100873 }
874
Tao Baoa456c212016-11-15 10:08:07 -0800875 // Block device doesn't support ftruncate(2).
876 if (!S_ISBLK(sb.st_mode)) {
Andreas Gampe964b95c2019-04-05 13:48:02 -0700877 long result = TEMP_FAILURE_RETRY(ftruncate(fd, declared_length + current_offset));
Tao Baoa456c212016-11-15 10:08:07 -0800878 if (result == -1) {
879 ALOGW("Zip: unable to truncate file to %" PRId64 ": %s",
880 static_cast<int64_t>(declared_length + current_offset), strerror(errno));
Yurii Zubrytskyi834326c2017-12-20 01:01:01 -0800881 return FileWriter{};
Tao Baoa456c212016-11-15 10:08:07 -0800882 }
883 }
884
Yurii Zubrytskyi834326c2017-12-20 01:01:01 -0800885 return FileWriter(fd, declared_length);
Narayan Kamathf899bd52015-04-17 11:53:14 +0100886 }
887
Chih-Hung Hsieh747eb142018-09-25 11:16:22 -0700888 FileWriter(FileWriter&& other) noexcept
Yurii Zubrytskyi834326c2017-12-20 01:01:01 -0800889 : fd_(other.fd_),
890 declared_length_(other.declared_length_),
891 total_bytes_written_(other.total_bytes_written_) {
892 other.fd_ = -1;
893 }
894
895 bool IsValid() const { return fd_ != -1; }
896
Narayan Kamathf899bd52015-04-17 11:53:14 +0100897 virtual bool Append(uint8_t* buf, size_t buf_size) override {
898 if (total_bytes_written_ + buf_size > declared_length_) {
Elliott Hughese8f4b142018-10-19 16:09:39 -0700899 ALOGW("Zip: Unexpected size %zu (declared) vs %zu (actual)", declared_length_,
Jiyong Parkcd997e62017-06-30 17:23:33 +0900900 total_bytes_written_ + buf_size);
Narayan Kamathf899bd52015-04-17 11:53:14 +0100901 return false;
902 }
903
Narayan Kamathe97e66e2015-04-27 16:25:53 +0100904 const bool result = android::base::WriteFully(fd_, buf, buf_size);
905 if (result) {
906 total_bytes_written_ += buf_size;
907 } else {
Elliott Hughese8f4b142018-10-19 16:09:39 -0700908 ALOGW("Zip: unable to write %zu bytes to file; %s", buf_size, strerror(errno));
Narayan Kamathf899bd52015-04-17 11:53:14 +0100909 }
910
Narayan Kamathe97e66e2015-04-27 16:25:53 +0100911 return result;
Narayan Kamathf899bd52015-04-17 11:53:14 +0100912 }
Jiyong Parkcd997e62017-06-30 17:23:33 +0900913
Narayan Kamathf899bd52015-04-17 11:53:14 +0100914 private:
Yurii Zubrytskyi834326c2017-12-20 01:01:01 -0800915 explicit FileWriter(const int fd = -1, const size_t declared_length = 0)
Jiyong Parkcd997e62017-06-30 17:23:33 +0900916 : Writer(), fd_(fd), declared_length_(declared_length), total_bytes_written_(0) {}
Narayan Kamathf899bd52015-04-17 11:53:14 +0100917
Yurii Zubrytskyi834326c2017-12-20 01:01:01 -0800918 int fd_;
Narayan Kamathf899bd52015-04-17 11:53:14 +0100919 const size_t declared_length_;
920 size_t total_bytes_written_;
921};
922
Narayan Kamath485b3642017-10-26 14:42:39 +0100923class EntryReader : public zip_archive::Reader {
Narayan Kamath8b8faed2017-10-26 14:08:38 +0100924 public:
925 EntryReader(const MappedZipFile& zip_file, const ZipEntry* entry)
926 : Reader(), zip_file_(zip_file), entry_(entry) {}
927
928 virtual bool ReadAtOffset(uint8_t* buf, size_t len, uint32_t offset) const {
929 return zip_file_.ReadAtOffset(buf, len, entry_->offset + offset);
930 }
931
932 virtual ~EntryReader() {}
933
934 private:
935 const MappedZipFile& zip_file_;
936 const ZipEntry* entry_;
937};
938
Dmitriy Ivanovf94e1592015-03-06 13:27:59 -0800939// This method is using libz macros with old-style-casts
940#pragma GCC diagnostic push
941#pragma GCC diagnostic ignored "-Wold-style-cast"
942static inline int zlib_inflateInit2(z_stream* stream, int window_bits) {
943 return inflateInit2(stream, window_bits);
944}
945#pragma GCC diagnostic pop
946
Narayan Kamath485b3642017-10-26 14:42:39 +0100947namespace zip_archive {
948
949// Moved out of line to avoid -Wweak-vtables.
950Reader::~Reader() {}
951Writer::~Writer() {}
952
953int32_t Inflate(const Reader& reader, const uint32_t compressed_length,
954 const uint32_t uncompressed_length, Writer* writer, uint64_t* crc_out) {
Dmitriy Ivanovedbabfe2015-03-12 09:58:15 -0700955 const size_t kBufSize = 32768;
956 std::vector<uint8_t> read_buf(kBufSize);
957 std::vector<uint8_t> write_buf(kBufSize);
Narayan Kamath7462f022013-11-21 13:05:04 +0000958 z_stream zstream;
959 int zerr;
960
961 /*
962 * Initialize the zlib stream struct.
963 */
964 memset(&zstream, 0, sizeof(zstream));
965 zstream.zalloc = Z_NULL;
966 zstream.zfree = Z_NULL;
967 zstream.opaque = Z_NULL;
968 zstream.next_in = NULL;
969 zstream.avail_in = 0;
Dmitriy Ivanovedbabfe2015-03-12 09:58:15 -0700970 zstream.next_out = &write_buf[0];
Narayan Kamath7462f022013-11-21 13:05:04 +0000971 zstream.avail_out = kBufSize;
972 zstream.data_type = Z_UNKNOWN;
973
974 /*
975 * Use the undocumented "negative window bits" feature to tell zlib
976 * that there's no zlib header waiting for it.
977 */
Dmitriy Ivanovf94e1592015-03-06 13:27:59 -0800978 zerr = zlib_inflateInit2(&zstream, -MAX_WBITS);
Narayan Kamath7462f022013-11-21 13:05:04 +0000979 if (zerr != Z_OK) {
980 if (zerr == Z_VERSION_ERROR) {
Jiyong Parkcd997e62017-06-30 17:23:33 +0900981 ALOGE("Installed zlib is not compatible with linked version (%s)", ZLIB_VERSION);
Narayan Kamath7462f022013-11-21 13:05:04 +0000982 } else {
983 ALOGW("Call to inflateInit2 failed (zerr=%d)", zerr);
984 }
985
986 return kZlibError;
987 }
988
Dmitriy Ivanov1f741e52015-03-06 14:26:37 -0800989 auto zstream_deleter = [](z_stream* stream) {
Jiyong Parkcd997e62017-06-30 17:23:33 +0900990 inflateEnd(stream); /* free up any allocated structures */
Dmitriy Ivanov1f741e52015-03-06 14:26:37 -0800991 };
992
993 std::unique_ptr<z_stream, decltype(zstream_deleter)> zstream_guard(&zstream, zstream_deleter);
994
Narayan Kamath2d1e23f2017-10-30 11:17:28 +0000995 const bool compute_crc = (crc_out != nullptr);
Andreas Gampe964b95c2019-04-05 13:48:02 -0700996 uLong crc = 0;
Narayan Kamath8b8faed2017-10-26 14:08:38 +0100997 uint32_t remaining_bytes = compressed_length;
Narayan Kamath7462f022013-11-21 13:05:04 +0000998 do {
999 /* read as much as we can */
1000 if (zstream.avail_in == 0) {
Andreas Gampe964b95c2019-04-05 13:48:02 -07001001 const uint32_t read_size = (remaining_bytes > kBufSize) ? kBufSize : remaining_bytes;
Narayan Kamath8b8faed2017-10-26 14:08:38 +01001002 const uint32_t offset = (compressed_length - remaining_bytes);
Adam Lesinskide117e42017-06-19 10:27:38 -07001003 // Make sure to read at offset to ensure concurrent access to the fd.
Narayan Kamath8b8faed2017-10-26 14:08:38 +01001004 if (!reader.ReadAtOffset(read_buf.data(), read_size, offset)) {
Andreas Gampe964b95c2019-04-05 13:48:02 -07001005 ALOGW("Zip: inflate read failed, getSize = %u: %s", read_size, strerror(errno));
Dmitriy Ivanov1f741e52015-03-06 14:26:37 -08001006 return kIoError;
Narayan Kamath7462f022013-11-21 13:05:04 +00001007 }
1008
Narayan Kamath8b8faed2017-10-26 14:08:38 +01001009 remaining_bytes -= read_size;
Narayan Kamath7462f022013-11-21 13:05:04 +00001010
Dmitriy Ivanovedbabfe2015-03-12 09:58:15 -07001011 zstream.next_in = &read_buf[0];
Narayan Kamath8b8faed2017-10-26 14:08:38 +01001012 zstream.avail_in = read_size;
Narayan Kamath7462f022013-11-21 13:05:04 +00001013 }
1014
1015 /* uncompress the data */
1016 zerr = inflate(&zstream, Z_NO_FLUSH);
1017 if (zerr != Z_OK && zerr != Z_STREAM_END) {
Jiyong Parkcd997e62017-06-30 17:23:33 +09001018 ALOGW("Zip: inflate zerr=%d (nIn=%p aIn=%u nOut=%p aOut=%u)", zerr, zstream.next_in,
1019 zstream.avail_in, zstream.next_out, zstream.avail_out);
Dmitriy Ivanov1f741e52015-03-06 14:26:37 -08001020 return kZlibError;
Narayan Kamath7462f022013-11-21 13:05:04 +00001021 }
1022
1023 /* write when we're full or when we're done */
Jiyong Parkcd997e62017-06-30 17:23:33 +09001024 if (zstream.avail_out == 0 || (zerr == Z_STREAM_END && zstream.avail_out != kBufSize)) {
Dmitriy Ivanovedbabfe2015-03-12 09:58:15 -07001025 const size_t write_size = zstream.next_out - &write_buf[0];
Narayan Kamathf899bd52015-04-17 11:53:14 +01001026 if (!writer->Append(&write_buf[0], write_size)) {
Narayan Kamath2d1e23f2017-10-30 11:17:28 +00001027 return kIoError;
1028 } else if (compute_crc) {
Andreas Gampe964b95c2019-04-05 13:48:02 -07001029 DCHECK_LE(write_size, kBufSize);
1030 crc = crc32(crc, &write_buf[0], static_cast<uint32_t>(write_size));
Narayan Kamath7462f022013-11-21 13:05:04 +00001031 }
Narayan Kamath7462f022013-11-21 13:05:04 +00001032
Dmitriy Ivanovedbabfe2015-03-12 09:58:15 -07001033 zstream.next_out = &write_buf[0];
Narayan Kamath7462f022013-11-21 13:05:04 +00001034 zstream.avail_out = kBufSize;
1035 }
1036 } while (zerr == Z_OK);
1037
Elliott Hughese8f4b142018-10-19 16:09:39 -07001038 CHECK_EQ(zerr, Z_STREAM_END); /* other errors should've been caught */
Narayan Kamath7462f022013-11-21 13:05:04 +00001039
Narayan Kamath162b7052017-06-05 13:21:12 +01001040 // NOTE: zstream.adler is always set to 0, because we're using the -MAX_WBITS
1041 // "feature" of zlib to tell it there won't be a zlib file header. zlib
1042 // doesn't bother calculating the checksum in that scenario. We just do
1043 // it ourselves above because there are no additional gains to be made by
1044 // having zlib calculate it for us, since they do it by calling crc32 in
1045 // the same manner that we have above.
Narayan Kamath2d1e23f2017-10-30 11:17:28 +00001046 if (compute_crc) {
1047 *crc_out = crc;
1048 }
Narayan Kamath7462f022013-11-21 13:05:04 +00001049
Narayan Kamath8b8faed2017-10-26 14:08:38 +01001050 if (zstream.total_out != uncompressed_length || remaining_bytes != 0) {
Jiyong Parkcd997e62017-06-30 17:23:33 +09001051 ALOGW("Zip: size mismatch on inflated file (%lu vs %" PRIu32 ")", zstream.total_out,
1052 uncompressed_length);
Dmitriy Ivanov1f741e52015-03-06 14:26:37 -08001053 return kInconsistentInformation;
Narayan Kamath7462f022013-11-21 13:05:04 +00001054 }
1055
Dmitriy Ivanov1f741e52015-03-06 14:26:37 -08001056 return 0;
Narayan Kamath7462f022013-11-21 13:05:04 +00001057}
Narayan Kamath485b3642017-10-26 14:42:39 +01001058} // namespace zip_archive
Narayan Kamath7462f022013-11-21 13:05:04 +00001059
Narayan Kamath8b8faed2017-10-26 14:08:38 +01001060static int32_t InflateEntryToWriter(MappedZipFile& mapped_zip, const ZipEntry* entry,
Narayan Kamath485b3642017-10-26 14:42:39 +01001061 zip_archive::Writer* writer, uint64_t* crc_out) {
Narayan Kamath8b8faed2017-10-26 14:08:38 +01001062 const EntryReader reader(mapped_zip, entry);
1063
Narayan Kamath485b3642017-10-26 14:42:39 +01001064 return zip_archive::Inflate(reader, entry->compressed_length, entry->uncompressed_length, writer,
1065 crc_out);
Narayan Kamath8b8faed2017-10-26 14:08:38 +01001066}
1067
Narayan Kamath485b3642017-10-26 14:42:39 +01001068static int32_t CopyEntryToWriter(MappedZipFile& mapped_zip, const ZipEntry* entry,
1069 zip_archive::Writer* writer, uint64_t* crc_out) {
Narayan Kamathf899bd52015-04-17 11:53:14 +01001070 static const uint32_t kBufSize = 32768;
1071 std::vector<uint8_t> buf(kBufSize);
1072
1073 const uint32_t length = entry->uncompressed_length;
1074 uint32_t count = 0;
Andreas Gampe964b95c2019-04-05 13:48:02 -07001075 uLong crc = 0;
Narayan Kamathf899bd52015-04-17 11:53:14 +01001076 while (count < length) {
1077 uint32_t remaining = length - count;
Adam Lesinskide117e42017-06-19 10:27:38 -07001078 off64_t offset = entry->offset + count;
Narayan Kamathf899bd52015-04-17 11:53:14 +01001079
Adam Lesinskide117e42017-06-19 10:27:38 -07001080 // Safe conversion because kBufSize is narrow enough for a 32 bit signed value.
Andreas Gampe964b95c2019-04-05 13:48:02 -07001081 const uint32_t block_size = (remaining > kBufSize) ? kBufSize : remaining;
Adam Lesinskide117e42017-06-19 10:27:38 -07001082
1083 // Make sure to read at offset to ensure concurrent access to the fd.
1084 if (!mapped_zip.ReadAtOffset(buf.data(), block_size, offset)) {
Andreas Gampe964b95c2019-04-05 13:48:02 -07001085 ALOGW("CopyFileToFile: copy read failed, block_size = %u, offset = %" PRId64 ": %s",
Adam Lesinskide117e42017-06-19 10:27:38 -07001086 block_size, static_cast<int64_t>(offset), strerror(errno));
Narayan Kamathf899bd52015-04-17 11:53:14 +01001087 return kIoError;
1088 }
1089
1090 if (!writer->Append(&buf[0], block_size)) {
1091 return kIoError;
1092 }
1093 crc = crc32(crc, &buf[0], block_size);
1094 count += block_size;
1095 }
1096
1097 *crc_out = crc;
1098
1099 return 0;
1100}
1101
Ryan Prichard3673f992018-10-10 22:41:14 -07001102int32_t ExtractToWriter(ZipArchiveHandle archive, ZipEntry* entry, zip_archive::Writer* writer) {
Narayan Kamath7462f022013-11-21 13:05:04 +00001103 const uint16_t method = entry->method;
Narayan Kamath7462f022013-11-21 13:05:04 +00001104
1105 // this should default to kUnknownCompressionMethod.
1106 int32_t return_value = -1;
1107 uint64_t crc = 0;
1108 if (method == kCompressStored) {
Tianjie Xu18c25922016-09-29 15:27:41 -07001109 return_value = CopyEntryToWriter(archive->mapped_zip, entry, writer, &crc);
Narayan Kamath7462f022013-11-21 13:05:04 +00001110 } else if (method == kCompressDeflated) {
Tianjie Xu18c25922016-09-29 15:27:41 -07001111 return_value = InflateEntryToWriter(archive->mapped_zip, entry, writer, &crc);
Narayan Kamath7462f022013-11-21 13:05:04 +00001112 }
1113
1114 if (!return_value && entry->has_data_descriptor) {
Narayan Kamath162b7052017-06-05 13:21:12 +01001115 return_value = ValidateDataDescriptor(archive->mapped_zip, entry);
Narayan Kamath7462f022013-11-21 13:05:04 +00001116 if (return_value) {
1117 return return_value;
1118 }
1119 }
1120
Narayan Kamath162b7052017-06-05 13:21:12 +01001121 // Validate that the CRC matches the calculated value.
1122 if (kCrcChecksEnabled && (entry->crc32 != static_cast<uint32_t>(crc))) {
Mark Salyzyn088bf902014-05-08 16:02:20 -07001123 ALOGW("Zip: crc mismatch: expected %" PRIu32 ", was %" PRIu64, entry->crc32, crc);
Narayan Kamath7462f022013-11-21 13:05:04 +00001124 return kInconsistentInformation;
1125 }
1126
1127 return return_value;
1128}
1129
Ryan Prichard3673f992018-10-10 22:41:14 -07001130int32_t ExtractToMemory(ZipArchiveHandle archive, ZipEntry* entry, uint8_t* begin, uint32_t size) {
Yurii Zubrytskyi834326c2017-12-20 01:01:01 -08001131 MemoryWriter writer(begin, size);
Ryan Prichard3673f992018-10-10 22:41:14 -07001132 return ExtractToWriter(archive, entry, &writer);
Narayan Kamathf899bd52015-04-17 11:53:14 +01001133}
1134
Ryan Prichard3673f992018-10-10 22:41:14 -07001135int32_t ExtractEntryToFile(ZipArchiveHandle archive, ZipEntry* entry, int fd) {
Yurii Zubrytskyi834326c2017-12-20 01:01:01 -08001136 auto writer = FileWriter::Create(fd, entry);
1137 if (!writer.IsValid()) {
Narayan Kamath7462f022013-11-21 13:05:04 +00001138 return kIoError;
1139 }
1140
Ryan Prichard3673f992018-10-10 22:41:14 -07001141 return ExtractToWriter(archive, entry, &writer);
Narayan Kamath7462f022013-11-21 13:05:04 +00001142}
1143
1144const char* ErrorCodeString(int32_t error_code) {
Narayan Kamath1ef9d2d2017-06-15 13:58:25 +01001145 // Make sure that the number of entries in kErrorMessages and ErrorCodes
1146 // match.
1147 static_assert((-kLastErrorCode + 1) == arraysize(kErrorMessages),
1148 "(-kLastErrorCode + 1) != arraysize(kErrorMessages)");
1149
1150 const uint32_t idx = -error_code;
1151 if (idx < arraysize(kErrorMessages)) {
1152 return kErrorMessages[idx];
Narayan Kamath7462f022013-11-21 13:05:04 +00001153 }
1154
Narayan Kamath1ef9d2d2017-06-15 13:58:25 +01001155 return "Unknown return code";
Narayan Kamath7462f022013-11-21 13:05:04 +00001156}
1157
Ryan Prichard3673f992018-10-10 22:41:14 -07001158int GetFileDescriptor(const ZipArchiveHandle archive) {
1159 return archive->mapped_zip.GetFileDescriptor();
Narayan Kamath7462f022013-11-21 13:05:04 +00001160}
Colin Cross7c6c7f02016-09-16 10:15:51 -07001161
Elliott Hughesb17bf522019-05-03 22:38:44 -07001162ZipString::ZipString(std::string_view entry_name)
1163 : name(reinterpret_cast<const uint8_t*>(entry_name.data())) {
1164 size_t len = entry_name.size();
Colin Cross7c6c7f02016-09-16 10:15:51 -07001165 CHECK_LE(len, static_cast<size_t>(UINT16_MAX));
1166 name_length = static_cast<uint16_t>(len);
1167}
Tianjie Xu18c25922016-09-29 15:27:41 -07001168
1169#if !defined(_WIN32)
Narayan Kamath485b3642017-10-26 14:42:39 +01001170class ProcessWriter : public zip_archive::Writer {
Tianjie Xu18c25922016-09-29 15:27:41 -07001171 public:
Jiyong Parkcd997e62017-06-30 17:23:33 +09001172 ProcessWriter(ProcessZipEntryFunction func, void* cookie)
1173 : Writer(), proc_function_(func), cookie_(cookie) {}
Tianjie Xu18c25922016-09-29 15:27:41 -07001174
1175 virtual bool Append(uint8_t* buf, size_t buf_size) override {
1176 return proc_function_(buf, buf_size, cookie_);
1177 }
1178
1179 private:
1180 ProcessZipEntryFunction proc_function_;
1181 void* cookie_;
1182};
1183
Ryan Prichard3673f992018-10-10 22:41:14 -07001184int32_t ProcessZipEntryContents(ZipArchiveHandle archive, ZipEntry* entry,
Tianjie Xu18c25922016-09-29 15:27:41 -07001185 ProcessZipEntryFunction func, void* cookie) {
1186 ProcessWriter writer(func, cookie);
Ryan Prichard3673f992018-10-10 22:41:14 -07001187 return ExtractToWriter(archive, entry, &writer);
Tianjie Xu18c25922016-09-29 15:27:41 -07001188}
1189
Jiyong Parkcd997e62017-06-30 17:23:33 +09001190#endif //! defined(_WIN32)
Tianjie Xu18c25922016-09-29 15:27:41 -07001191
1192int MappedZipFile::GetFileDescriptor() const {
1193 if (!has_fd_) {
1194 ALOGW("Zip: MappedZipFile doesn't have a file descriptor.");
1195 return -1;
1196 }
1197 return fd_;
1198}
1199
1200void* MappedZipFile::GetBasePtr() const {
1201 if (has_fd_) {
1202 ALOGW("Zip: MappedZipFile doesn't have a base pointer.");
1203 return nullptr;
1204 }
1205 return base_ptr_;
1206}
1207
1208off64_t MappedZipFile::GetFileLength() const {
1209 if (has_fd_) {
1210 off64_t result = lseek64(fd_, 0, SEEK_END);
1211 if (result == -1) {
1212 ALOGE("Zip: lseek on fd %d failed: %s", fd_, strerror(errno));
1213 }
1214 return result;
1215 } else {
1216 if (base_ptr_ == nullptr) {
1217 ALOGE("Zip: invalid file map\n");
1218 return -1;
1219 }
1220 return static_cast<off64_t>(data_length_);
1221 }
1222}
1223
Tianjie Xu18c25922016-09-29 15:27:41 -07001224// Attempts to read |len| bytes into |buf| at offset |off|.
Narayan Kamath8b8faed2017-10-26 14:08:38 +01001225bool MappedZipFile::ReadAtOffset(uint8_t* buf, size_t len, off64_t off) const {
Tianjie Xu18c25922016-09-29 15:27:41 -07001226 if (has_fd_) {
Adam Lesinskide117e42017-06-19 10:27:38 -07001227 if (!android::base::ReadFullyAtOffset(fd_, buf, len, off)) {
Tianjie Xu18c25922016-09-29 15:27:41 -07001228 ALOGE("Zip: failed to read at offset %" PRId64 "\n", off);
1229 return false;
1230 }
Adam Lesinskide117e42017-06-19 10:27:38 -07001231 } else {
1232 if (off < 0 || off > static_cast<off64_t>(data_length_)) {
1233 ALOGE("Zip: invalid offset: %" PRId64 ", data length: %" PRId64 "\n", off, data_length_);
1234 return false;
1235 }
1236 memcpy(buf, static_cast<uint8_t*>(base_ptr_) + off, len);
Tianjie Xu18c25922016-09-29 15:27:41 -07001237 }
Adam Lesinskide117e42017-06-19 10:27:38 -07001238 return true;
Tianjie Xu18c25922016-09-29 15:27:41 -07001239}
1240
1241void CentralDirectory::Initialize(void* map_base_ptr, off64_t cd_start_offset, size_t cd_size) {
1242 base_ptr_ = static_cast<uint8_t*>(map_base_ptr) + cd_start_offset;
1243 length_ = cd_size;
1244}
1245
Elliott Hughese8f4b142018-10-19 16:09:39 -07001246bool ZipArchive::InitializeCentralDirectory(off64_t cd_start_offset, size_t cd_size) {
Tianjie Xu18c25922016-09-29 15:27:41 -07001247 if (mapped_zip.HasFd()) {
Elliott Hughese8f4b142018-10-19 16:09:39 -07001248 directory_map = android::base::MappedFile::FromFd(mapped_zip.GetFileDescriptor(),
1249 cd_start_offset, cd_size, PROT_READ);
1250 if (!directory_map) return false;
Tianjie Xu18c25922016-09-29 15:27:41 -07001251
Elliott Hughese8f4b142018-10-19 16:09:39 -07001252 CHECK_EQ(directory_map->size(), cd_size);
1253 central_directory.Initialize(directory_map->data(), 0 /*offset*/, cd_size);
Tianjie Xu18c25922016-09-29 15:27:41 -07001254 } else {
1255 if (mapped_zip.GetBasePtr() == nullptr) {
1256 ALOGE("Zip: Failed to map central directory, bad mapped_zip base pointer\n");
1257 return false;
1258 }
1259 if (static_cast<off64_t>(cd_start_offset) + static_cast<off64_t>(cd_size) >
1260 mapped_zip.GetFileLength()) {
Jiyong Parkcd997e62017-06-30 17:23:33 +09001261 ALOGE(
1262 "Zip: Failed to map central directory, offset exceeds mapped memory region ("
1263 "start_offset %" PRId64 ", cd_size %zu, mapped_region_size %" PRId64 ")",
1264 static_cast<int64_t>(cd_start_offset), cd_size, mapped_zip.GetFileLength());
Tianjie Xu18c25922016-09-29 15:27:41 -07001265 return false;
1266 }
1267
1268 central_directory.Initialize(mapped_zip.GetBasePtr(), cd_start_offset, cd_size);
1269 }
1270 return true;
1271}
Elliott Hughes55fd2932017-05-28 22:59:04 -07001272
1273tm ZipEntry::GetModificationTime() const {
1274 tm t = {};
1275
1276 t.tm_hour = (mod_time >> 11) & 0x1f;
1277 t.tm_min = (mod_time >> 5) & 0x3f;
1278 t.tm_sec = (mod_time & 0x1f) << 1;
1279
1280 t.tm_year = ((mod_time >> 25) & 0x7f) + 80;
1281 t.tm_mon = ((mod_time >> 21) & 0xf) - 1;
1282 t.tm_mday = (mod_time >> 16) & 0x1f;
1283
1284 return t;
1285}