blob: 0e8536df786113289e0327d2270db4fd3ed9acdc [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
Narayan Kamath7462f022013-11-21 13:05:04 +000023#include <assert.h>
24#include <errno.h>
Mark Salyzyn99ef9912014-03-14 14:26:22 -070025#include <fcntl.h>
26#include <inttypes.h>
Narayan Kamath7462f022013-11-21 13:05:04 +000027#include <limits.h>
Narayan Kamath7462f022013-11-21 13:05:04 +000028#include <stdlib.h>
29#include <string.h>
Elliott Hughes55fd2932017-05-28 22:59:04 -070030#include <time.h>
Narayan Kamath7462f022013-11-21 13:05:04 +000031#include <unistd.h>
32
Dan Albert1ae07642015-04-09 14:11:18 -070033#include <memory>
34#include <vector>
35
Mark Salyzynff2dcd92016-09-28 15:54:45 -070036#include <android-base/file.h>
37#include <android-base/logging.h>
38#include <android-base/macros.h> // TEMP_FAILURE_RETRY may or may not be in unistd
39#include <android-base/memory.h>
Mark Salyzyncfd5b082016-10-17 14:28:00 -070040#include <log/log.h>
Mark Salyzynff2dcd92016-09-28 15:54:45 -070041#include <utils/Compat.h>
42#include <utils/FileMap.h>
Christopher Ferrise6884ce2015-11-10 14:55:12 -080043#include "ziparchive/zip_archive.h"
Dan Albert1ae07642015-04-09 14:11:18 -070044#include "zlib.h"
Narayan Kamath7462f022013-11-21 13:05:04 +000045
Narayan Kamath044bc8e2014-12-03 18:22:53 +000046#include "entry_name_utils-inl.h"
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -070047#include "zip_archive_common.h"
Christopher Ferrise6884ce2015-11-10 14:55:12 -080048#include "zip_archive_private.h"
Mark Salyzyn99ef9912014-03-14 14:26:22 -070049
Dan Albert1ae07642015-04-09 14:11:18 -070050using android::base::get_unaligned;
Narayan Kamath044bc8e2014-12-03 18:22:53 +000051
Narayan Kamath162b7052017-06-05 13:21:12 +010052// Used to turn on crc checks - verify that the content CRC matches the values
53// specified in the local file header and the central directory.
54static const bool kCrcChecksEnabled = false;
55
Narayan Kamath926973e2014-06-09 14:18:14 +010056// This is for windows. If we don't open a file in binary mode, weird
Narayan Kamath7462f022013-11-21 13:05:04 +000057// things will happen.
58#ifndef O_BINARY
59#define O_BINARY 0
60#endif
61
Narayan Kamath926973e2014-06-09 14:18:14 +010062// The maximum number of bytes to scan backwards for the EOCD start.
63static const uint32_t kMaxEOCDSearch = kMaxCommentLen + sizeof(EocdRecord);
64
Narayan Kamath7462f022013-11-21 13:05:04 +000065/*
66 * A Read-only Zip archive.
67 *
68 * We want "open" and "find entry by name" to be fast operations, and
69 * we want to use as little memory as possible. We memory-map the zip
70 * central directory, and load a hash table with pointers to the filenames
71 * (which aren't null-terminated). The other fields are at a fixed offset
72 * from the filename, so we don't need to extract those (but we do need
73 * to byte-read and endian-swap them every time we want them).
74 *
75 * It's possible that somebody has handed us a massive (~1GB) zip archive,
76 * so we can't expect to mmap the entire file.
77 *
78 * To speed comparisons when doing a lookup by name, we could make the mapping
79 * "private" (copy-on-write) and null-terminate the filenames after verifying
80 * the record structure. However, this requires a private mapping of
81 * every page that the Central Directory touches. Easier to tuck a copy
82 * of the string length into the hash table entry.
83 */
Narayan Kamath7462f022013-11-21 13:05:04 +000084
Narayan Kamath7462f022013-11-21 13:05:04 +000085/*
86 * Round up to the next highest power of 2.
87 *
88 * Found on http://graphics.stanford.edu/~seander/bithacks.html.
89 */
90static uint32_t RoundUpPower2(uint32_t val) {
91 val--;
92 val |= val >> 1;
93 val |= val >> 2;
94 val |= val >> 4;
95 val |= val >> 8;
96 val |= val >> 16;
97 val++;
98
99 return val;
100}
101
Yusuke Sato07447542015-06-25 14:39:19 -0700102static uint32_t ComputeHash(const ZipString& name) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000103 uint32_t hash = 0;
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100104 uint16_t len = name.name_length;
105 const uint8_t* str = name.name;
Narayan Kamath7462f022013-11-21 13:05:04 +0000106
107 while (len--) {
108 hash = hash * 31 + *str++;
109 }
110
111 return hash;
112}
113
114/*
115 * Convert a ZipEntry to a hash table index, verifying that it's in a
116 * valid range.
117 */
Yusuke Sato07447542015-06-25 14:39:19 -0700118static int64_t EntryToIndex(const ZipString* hash_table,
Narayan Kamath7462f022013-11-21 13:05:04 +0000119 const uint32_t hash_table_size,
Yusuke Sato07447542015-06-25 14:39:19 -0700120 const ZipString& name) {
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100121 const uint32_t hash = ComputeHash(name);
Narayan Kamath7462f022013-11-21 13:05:04 +0000122
123 // NOTE: (hash_table_size - 1) is guaranteed to be non-negative.
124 uint32_t ent = hash & (hash_table_size - 1);
125 while (hash_table[ent].name != NULL) {
Yusuke Sato07447542015-06-25 14:39:19 -0700126 if (hash_table[ent] == name) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000127 return ent;
128 }
129
130 ent = (ent + 1) & (hash_table_size - 1);
131 }
132
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100133 ALOGV("Zip: Unable to find entry %.*s", name.name_length, name.name);
Narayan Kamath7462f022013-11-21 13:05:04 +0000134 return kEntryNotFound;
135}
136
137/*
138 * Add a new entry to the hash table.
139 */
Yusuke Sato07447542015-06-25 14:39:19 -0700140static int32_t AddToHash(ZipString *hash_table, const uint64_t hash_table_size,
141 const ZipString& name) {
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100142 const uint64_t hash = ComputeHash(name);
Narayan Kamath7462f022013-11-21 13:05:04 +0000143 uint32_t ent = hash & (hash_table_size - 1);
144
145 /*
146 * We over-allocated the table, so we're guaranteed to find an empty slot.
147 * Further, we guarantee that the hashtable size is not 0.
148 */
149 while (hash_table[ent].name != NULL) {
Yusuke Sato07447542015-06-25 14:39:19 -0700150 if (hash_table[ent] == name) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000151 // We've found a duplicate entry. We don't accept it
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100152 ALOGW("Zip: Found duplicate entry %.*s", name.name_length, name.name);
Narayan Kamath7462f022013-11-21 13:05:04 +0000153 return kDuplicateEntry;
154 }
155 ent = (ent + 1) & (hash_table_size - 1);
156 }
157
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100158 hash_table[ent].name = name.name;
159 hash_table[ent].name_length = name.name_length;
Narayan Kamath7462f022013-11-21 13:05:04 +0000160 return 0;
161}
162
Tianjie Xu18c25922016-09-29 15:27:41 -0700163static int32_t MapCentralDirectory0(const char* debug_file_name, ZipArchive* archive,
164 off64_t file_length, off64_t read_amount,
165 uint8_t* scan_buffer) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000166 const off64_t search_start = file_length - read_amount;
167
Tianjie Xu18c25922016-09-29 15:27:41 -0700168 if(!archive->mapped_zip.ReadAtOffset(scan_buffer, read_amount, search_start)) {
169 ALOGE("Zip: read %" PRId64 " from offset %" PRId64 " failed",
170 static_cast<int64_t>(read_amount), static_cast<int64_t>(search_start));
Narayan Kamath7462f022013-11-21 13:05:04 +0000171 return kIoError;
172 }
173
174 /*
175 * Scan backward for the EOCD magic. In an archive without a trailing
176 * comment, we'll find it on the first try. (We may want to consider
177 * doing an initial minimal read; if we don't find it, retry with a
178 * second read as above.)
179 */
Narayan Kamath926973e2014-06-09 14:18:14 +0100180 int i = read_amount - sizeof(EocdRecord);
181 for (; i >= 0; i--) {
Dan Albert1ae07642015-04-09 14:11:18 -0700182 if (scan_buffer[i] == 0x50) {
183 uint32_t* sig_addr = reinterpret_cast<uint32_t*>(&scan_buffer[i]);
184 if (get_unaligned<uint32_t>(sig_addr) == EocdRecord::kSignature) {
185 ALOGV("+++ Found EOCD at buf+%d", i);
186 break;
187 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000188 }
189 }
190 if (i < 0) {
191 ALOGD("Zip: EOCD not found, %s is not zip", debug_file_name);
192 return kInvalidFile;
193 }
194
195 const off64_t eocd_offset = search_start + i;
Narayan Kamath926973e2014-06-09 14:18:14 +0100196 const EocdRecord* eocd = reinterpret_cast<const EocdRecord*>(scan_buffer + i);
Narayan Kamath7462f022013-11-21 13:05:04 +0000197 /*
Narayan Kamath926973e2014-06-09 14:18:14 +0100198 * Verify that there's no trailing space at the end of the central directory
199 * and its comment.
Narayan Kamath7462f022013-11-21 13:05:04 +0000200 */
Narayan Kamath926973e2014-06-09 14:18:14 +0100201 const off64_t calculated_length = eocd_offset + sizeof(EocdRecord)
202 + eocd->comment_length;
203 if (calculated_length != file_length) {
Narayan Kamath4f6b4992014-06-03 13:59:23 +0100204 ALOGW("Zip: %" PRId64 " extraneous bytes at the end of the central directory",
Narayan Kamath926973e2014-06-09 14:18:14 +0100205 static_cast<int64_t>(file_length - calculated_length));
Narayan Kamath4f6b4992014-06-03 13:59:23 +0100206 return kInvalidFile;
207 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000208
Narayan Kamath926973e2014-06-09 14:18:14 +0100209 /*
210 * Grab the CD offset and size, and the number of entries in the
211 * archive and verify that they look reasonable.
212 */
Tianjie Xu1ee48922016-09-21 14:58:11 -0700213 if (static_cast<off64_t>(eocd->cd_start_offset) + eocd->cd_size > eocd_offset) {
Narayan Kamath926973e2014-06-09 14:18:14 +0100214 ALOGW("Zip: bad offsets (dir %" PRIu32 ", size %" PRIu32 ", eocd %" PRId64 ")",
215 eocd->cd_start_offset, eocd->cd_size, static_cast<int64_t>(eocd_offset));
Tianjie Xu1ee48922016-09-21 14:58:11 -0700216#if defined(__ANDROID__)
217 if (eocd->cd_start_offset + eocd->cd_size <= eocd_offset) {
218 android_errorWriteLog(0x534e4554, "31251826");
219 }
220#endif
Narayan Kamath7462f022013-11-21 13:05:04 +0000221 return kInvalidOffset;
222 }
Narayan Kamath926973e2014-06-09 14:18:14 +0100223 if (eocd->num_records == 0) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000224 ALOGW("Zip: empty archive?");
225 return kEmptyArchive;
226 }
227
Elliott Hughese49236b2015-06-04 15:21:59 -0700228 ALOGV("+++ num_entries=%" PRIu32 " dir_size=%" PRIu32 " dir_offset=%" PRIu32,
Narayan Kamath926973e2014-06-09 14:18:14 +0100229 eocd->num_records, eocd->cd_size, eocd->cd_start_offset);
Narayan Kamath7462f022013-11-21 13:05:04 +0000230
231 /*
232 * It all looks good. Create a mapping for the CD, and set the fields
233 * in archive.
234 */
Tianjie Xu18c25922016-09-29 15:27:41 -0700235
236 if (!archive->InitializeCentralDirectory(debug_file_name,
237 static_cast<off64_t>(eocd->cd_start_offset),
238 static_cast<size_t>(eocd->cd_size))) {
239 ALOGE("Zip: failed to intialize central directory.\n");
Narayan Kamatheaf98852013-12-11 14:51:51 +0000240 return kMmapFailed;
Narayan Kamath7462f022013-11-21 13:05:04 +0000241 }
242
Narayan Kamath926973e2014-06-09 14:18:14 +0100243 archive->num_entries = eocd->num_records;
244 archive->directory_offset = eocd->cd_start_offset;
Narayan Kamath7462f022013-11-21 13:05:04 +0000245
246 return 0;
247}
248
249/*
250 * Find the zip Central Directory and memory-map it.
251 *
252 * On success, returns 0 after populating fields from the EOCD area:
253 * directory_offset
Tianjie Xu18c25922016-09-29 15:27:41 -0700254 * directory_ptr
Narayan Kamath7462f022013-11-21 13:05:04 +0000255 * num_entries
256 */
Tianjie Xu18c25922016-09-29 15:27:41 -0700257static int32_t MapCentralDirectory(const char* debug_file_name, ZipArchive* archive) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000258
259 // Test file length. We use lseek64 to make sure the file
260 // is small enough to be a zip file (Its size must be less than
261 // 0xffffffff bytes).
Tianjie Xu18c25922016-09-29 15:27:41 -0700262 off64_t file_length = archive->mapped_zip.GetFileLength();
Narayan Kamath7462f022013-11-21 13:05:04 +0000263 if (file_length == -1) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000264 return kInvalidFile;
265 }
266
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800267 if (file_length > static_cast<off64_t>(0xffffffff)) {
Narayan Kamath926973e2014-06-09 14:18:14 +0100268 ALOGV("Zip: zip file too long %" PRId64, static_cast<int64_t>(file_length));
Narayan Kamath7462f022013-11-21 13:05:04 +0000269 return kInvalidFile;
270 }
271
Narayan Kamath926973e2014-06-09 14:18:14 +0100272 if (file_length < static_cast<off64_t>(sizeof(EocdRecord))) {
273 ALOGV("Zip: length %" PRId64 " is too small to be zip", static_cast<int64_t>(file_length));
Narayan Kamath7462f022013-11-21 13:05:04 +0000274 return kInvalidFile;
275 }
276
277 /*
278 * Perform the traditional EOCD snipe hunt.
279 *
280 * We're searching for the End of Central Directory magic number,
281 * which appears at the start of the EOCD block. It's followed by
282 * 18 bytes of EOCD stuff and up to 64KB of archive comment. We
283 * need to read the last part of the file into a buffer, dig through
284 * it to find the magic number, parse some values out, and use those
285 * to determine the extent of the CD.
286 *
287 * We start by pulling in the last part of the file.
288 */
Narayan Kamath926973e2014-06-09 14:18:14 +0100289 off64_t read_amount = kMaxEOCDSearch;
290 if (file_length < read_amount) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000291 read_amount = file_length;
292 }
293
Tianjie Xu18c25922016-09-29 15:27:41 -0700294 std::vector<uint8_t> scan_buffer(read_amount);
295 int32_t result = MapCentralDirectory0(debug_file_name, archive, file_length, read_amount,
296 scan_buffer.data());
Narayan Kamath7462f022013-11-21 13:05:04 +0000297 return result;
298}
299
300/*
301 * Parses the Zip archive's Central Directory. Allocates and populates the
302 * hash table.
303 *
304 * Returns 0 on success.
305 */
306static int32_t ParseZipArchive(ZipArchive* archive) {
Tianjie Xu18c25922016-09-29 15:27:41 -0700307 const uint8_t* const cd_ptr = archive->central_directory.GetBasePtr();
308 const size_t cd_length = archive->central_directory.GetMapLength();
Narayan Kamath926973e2014-06-09 14:18:14 +0100309 const uint16_t num_entries = archive->num_entries;
Narayan Kamath7462f022013-11-21 13:05:04 +0000310
311 /*
312 * Create hash table. We have a minimum 75% load factor, possibly as
313 * low as 50% after we round off to a power of 2. There must be at
314 * least one unused entry to avoid an infinite loop during creation.
315 */
316 archive->hash_table_size = RoundUpPower2(1 + (num_entries * 4) / 3);
Yusuke Sato07447542015-06-25 14:39:19 -0700317 archive->hash_table = reinterpret_cast<ZipString*>(calloc(archive->hash_table_size,
318 sizeof(ZipString)));
Tianjie Xu9e020e22016-10-10 12:11:30 -0700319 if (archive->hash_table == nullptr) {
320 ALOGW("Zip: unable to allocate the %u-entry hash_table, entry size: %zu",
321 archive->hash_table_size, sizeof(ZipString));
322 return -1;
323 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000324
325 /*
326 * Walk through the central directory, adding entries to the hash
327 * table and verifying values.
328 */
Narayan Kamath926973e2014-06-09 14:18:14 +0100329 const uint8_t* const cd_end = cd_ptr + cd_length;
Narayan Kamath7462f022013-11-21 13:05:04 +0000330 const uint8_t* ptr = cd_ptr;
331 for (uint16_t i = 0; i < num_entries; i++) {
Narayan Kamath926973e2014-06-09 14:18:14 +0100332 const CentralDirectoryRecord* cdr =
333 reinterpret_cast<const CentralDirectoryRecord*>(ptr);
334 if (cdr->record_signature != CentralDirectoryRecord::kSignature) {
Mark Salyzyn088bf902014-05-08 16:02:20 -0700335 ALOGW("Zip: missed a central dir sig (at %" PRIu16 ")", i);
Dmitriy Ivanov3ea93da2015-03-06 11:48:47 -0800336 return -1;
Narayan Kamath7462f022013-11-21 13:05:04 +0000337 }
338
Narayan Kamath926973e2014-06-09 14:18:14 +0100339 if (ptr + sizeof(CentralDirectoryRecord) > cd_end) {
Mark Salyzyn088bf902014-05-08 16:02:20 -0700340 ALOGW("Zip: ran off the end (at %" PRIu16 ")", i);
Dmitriy Ivanov3ea93da2015-03-06 11:48:47 -0800341 return -1;
Narayan Kamath7462f022013-11-21 13:05:04 +0000342 }
343
Narayan Kamath926973e2014-06-09 14:18:14 +0100344 const off64_t local_header_offset = cdr->local_file_header_offset;
Narayan Kamath7462f022013-11-21 13:05:04 +0000345 if (local_header_offset >= archive->directory_offset) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800346 ALOGW("Zip: bad LFH offset %" PRId64 " at entry %" PRIu16,
347 static_cast<int64_t>(local_header_offset), i);
Dmitriy Ivanov3ea93da2015-03-06 11:48:47 -0800348 return -1;
Narayan Kamath7462f022013-11-21 13:05:04 +0000349 }
350
Narayan Kamath926973e2014-06-09 14:18:14 +0100351 const uint16_t file_name_length = cdr->file_name_length;
352 const uint16_t extra_length = cdr->extra_field_length;
353 const uint16_t comment_length = cdr->comment_length;
Piotr Jastrzebski78271ba2014-08-15 12:53:00 +0100354 const uint8_t* file_name = ptr + sizeof(CentralDirectoryRecord);
355
Tianjie Xu9e020e22016-10-10 12:11:30 -0700356 if (file_name + file_name_length > cd_end) {
357 ALOGW("Zip: file name boundary exceeds the central directory range, file_name_length: "
358 "%" PRIx16 ", cd_length: %zu", file_name_length, cd_length);
359 return -1;
360 }
Narayan Kamath044bc8e2014-12-03 18:22:53 +0000361 /* check that file name is valid UTF-8 and doesn't contain NUL (U+0000) characters */
362 if (!IsValidEntryName(file_name, file_name_length)) {
Dmitriy Ivanov3ea93da2015-03-06 11:48:47 -0800363 return -1;
Piotr Jastrzebski78271ba2014-08-15 12:53:00 +0100364 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000365
366 /* add the CDE filename to the hash table */
Yusuke Sato07447542015-06-25 14:39:19 -0700367 ZipString entry_name;
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100368 entry_name.name = file_name;
369 entry_name.name_length = file_name_length;
Narayan Kamath7462f022013-11-21 13:05:04 +0000370 const int add_result = AddToHash(archive->hash_table,
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100371 archive->hash_table_size, entry_name);
Dmitriy Ivanov3ea93da2015-03-06 11:48:47 -0800372 if (add_result != 0) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000373 ALOGW("Zip: Error adding entry to hash table %d", add_result);
Dmitriy Ivanov3ea93da2015-03-06 11:48:47 -0800374 return add_result;
Narayan Kamath7462f022013-11-21 13:05:04 +0000375 }
376
Narayan Kamath926973e2014-06-09 14:18:14 +0100377 ptr += sizeof(CentralDirectoryRecord) + file_name_length + extra_length + comment_length;
378 if ((ptr - cd_ptr) > static_cast<int64_t>(cd_length)) {
Mark Salyzyn088bf902014-05-08 16:02:20 -0700379 ALOGW("Zip: bad CD advance (%tu vs %zu) at entry %" PRIu16,
380 ptr - cd_ptr, cd_length, i);
Dmitriy Ivanov3ea93da2015-03-06 11:48:47 -0800381 return -1;
Narayan Kamath7462f022013-11-21 13:05:04 +0000382 }
383 }
Mark Salyzyn088bf902014-05-08 16:02:20 -0700384 ALOGV("+++ zip good scan %" PRIu16 " entries", num_entries);
Narayan Kamath7462f022013-11-21 13:05:04 +0000385
Dmitriy Ivanov3ea93da2015-03-06 11:48:47 -0800386 return 0;
Narayan Kamath7462f022013-11-21 13:05:04 +0000387}
388
389static int32_t OpenArchiveInternal(ZipArchive* archive,
390 const char* debug_file_name) {
391 int32_t result = -1;
Tianjie Xu18c25922016-09-29 15:27:41 -0700392 if ((result = MapCentralDirectory(debug_file_name, archive)) != 0) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000393 return result;
394 }
395
396 if ((result = ParseZipArchive(archive))) {
397 return result;
398 }
399
400 return 0;
401}
402
403int32_t OpenArchiveFd(int fd, const char* debug_file_name,
Dmitriy Ivanov40b52b22014-07-15 19:33:00 -0700404 ZipArchiveHandle* handle, bool assume_ownership) {
405 ZipArchive* archive = new ZipArchive(fd, assume_ownership);
Narayan Kamath7462f022013-11-21 13:05:04 +0000406 *handle = archive;
Narayan Kamath7462f022013-11-21 13:05:04 +0000407 return OpenArchiveInternal(archive, debug_file_name);
408}
409
410int32_t OpenArchive(const char* fileName, ZipArchiveHandle* handle) {
Neil Fullerb1a113f2014-07-25 14:43:04 +0100411 const int fd = open(fileName, O_RDONLY | O_BINARY, 0);
Dmitriy Ivanov40b52b22014-07-15 19:33:00 -0700412 ZipArchive* archive = new ZipArchive(fd, true);
Narayan Kamath7462f022013-11-21 13:05:04 +0000413 *handle = archive;
414
Narayan Kamath7462f022013-11-21 13:05:04 +0000415 if (fd < 0) {
416 ALOGW("Unable to open '%s': %s", fileName, strerror(errno));
417 return kIoError;
Narayan Kamath7462f022013-11-21 13:05:04 +0000418 }
Dmitriy Ivanov40b52b22014-07-15 19:33:00 -0700419
Narayan Kamath7462f022013-11-21 13:05:04 +0000420 return OpenArchiveInternal(archive, fileName);
421}
422
Tianjie Xu18c25922016-09-29 15:27:41 -0700423int32_t OpenArchiveFromMemory(void* address, size_t length, const char* debug_file_name,
424 ZipArchiveHandle *handle) {
425 ZipArchive* archive = new ZipArchive(address, length);
426 *handle = archive;
427 return OpenArchiveInternal(archive, debug_file_name);
428}
429
Narayan Kamath7462f022013-11-21 13:05:04 +0000430/*
431 * Close a ZipArchive, closing the file and freeing the contents.
432 */
433void CloseArchive(ZipArchiveHandle handle) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800434 ZipArchive* archive = reinterpret_cast<ZipArchive*>(handle);
Narayan Kamath7462f022013-11-21 13:05:04 +0000435 ALOGV("Closing archive %p", archive);
Neil Fullerb1a113f2014-07-25 14:43:04 +0100436 delete archive;
Narayan Kamath7462f022013-11-21 13:05:04 +0000437}
438
Narayan Kamath162b7052017-06-05 13:21:12 +0100439static int32_t ValidateDataDescriptor(MappedZipFile& mapped_zip, ZipEntry* entry) {
Narayan Kamath926973e2014-06-09 14:18:14 +0100440 uint8_t ddBuf[sizeof(DataDescriptor) + sizeof(DataDescriptor::kOptSignature)];
Tianjie Xu18c25922016-09-29 15:27:41 -0700441 if (!mapped_zip.ReadData(ddBuf, sizeof(ddBuf))) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000442 return kIoError;
443 }
444
Narayan Kamath926973e2014-06-09 14:18:14 +0100445 const uint32_t ddSignature = *(reinterpret_cast<const uint32_t*>(ddBuf));
446 const uint16_t offset = (ddSignature == DataDescriptor::kOptSignature) ? 4 : 0;
447 const DataDescriptor* descriptor = reinterpret_cast<const DataDescriptor*>(ddBuf + offset);
Narayan Kamath7462f022013-11-21 13:05:04 +0000448
Narayan Kamath162b7052017-06-05 13:21:12 +0100449 // Validate that the values in the data descriptor match those in the central
450 // directory.
451 if (entry->compressed_length != descriptor->compressed_size ||
452 entry->uncompressed_length != descriptor->uncompressed_size ||
453 entry->crc32 != descriptor->crc32) {
454 ALOGW("Zip: size/crc32 mismatch. expected {%" PRIu32 ", %" PRIu32 ", %" PRIx32
455 "}, was {%" PRIu32 ", %" PRIu32 ", %" PRIx32 "}",
456 entry->compressed_length, entry->uncompressed_length, entry->crc32,
457 descriptor->compressed_size, descriptor->uncompressed_size, descriptor->crc32);
458 return kInconsistentInformation;
459 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000460
461 return 0;
462}
463
Narayan Kamath7462f022013-11-21 13:05:04 +0000464static int32_t FindEntry(const ZipArchive* archive, const int ent,
465 ZipEntry* data) {
466 const uint16_t nameLen = archive->hash_table[ent].name_length;
Narayan Kamath7462f022013-11-21 13:05:04 +0000467
468 // Recover the start of the central directory entry from the filename
469 // pointer. The filename is the first entry past the fixed-size data,
470 // so we can just subtract back from that.
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100471 const uint8_t* ptr = archive->hash_table[ent].name;
Narayan Kamath926973e2014-06-09 14:18:14 +0100472 ptr -= sizeof(CentralDirectoryRecord);
Narayan Kamath7462f022013-11-21 13:05:04 +0000473
474 // This is the base of our mmapped region, we have to sanity check that
475 // the name that's in the hash table is a pointer to a location within
476 // this mapped region.
Tianjie Xu18c25922016-09-29 15:27:41 -0700477 const uint8_t* base_ptr = archive->central_directory.GetBasePtr();
478 if (ptr < base_ptr || ptr > base_ptr + archive->central_directory.GetMapLength()) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000479 ALOGW("Zip: Invalid entry pointer");
480 return kInvalidOffset;
481 }
482
Narayan Kamath926973e2014-06-09 14:18:14 +0100483 const CentralDirectoryRecord *cdr =
484 reinterpret_cast<const CentralDirectoryRecord*>(ptr);
485
Narayan Kamath7462f022013-11-21 13:05:04 +0000486 // The offset of the start of the central directory in the zipfile.
487 // We keep this lying around so that we can sanity check all our lengths
488 // and our per-file structures.
489 const off64_t cd_offset = archive->directory_offset;
490
491 // Fill out the compression method, modification time, crc32
492 // and other interesting attributes from the central directory. These
493 // will later be compared against values from the local file header.
Narayan Kamath926973e2014-06-09 14:18:14 +0100494 data->method = cdr->compression_method;
beonit0e99a2f2015-07-18 02:08:16 +0900495 data->mod_time = cdr->last_mod_date << 16 | cdr->last_mod_time;
Narayan Kamath926973e2014-06-09 14:18:14 +0100496 data->crc32 = cdr->crc32;
497 data->compressed_length = cdr->compressed_size;
498 data->uncompressed_length = cdr->uncompressed_size;
Narayan Kamath7462f022013-11-21 13:05:04 +0000499
500 // Figure out the local header offset from the central directory. The
501 // actual file data will begin after the local header and the name /
502 // extra comments.
Narayan Kamath926973e2014-06-09 14:18:14 +0100503 const off64_t local_header_offset = cdr->local_file_header_offset;
504 if (local_header_offset + static_cast<off64_t>(sizeof(LocalFileHeader)) >= cd_offset) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000505 ALOGW("Zip: bad local hdr offset in zip");
506 return kInvalidOffset;
507 }
508
Narayan Kamath926973e2014-06-09 14:18:14 +0100509 uint8_t lfh_buf[sizeof(LocalFileHeader)];
Tianjie Xu18c25922016-09-29 15:27:41 -0700510 if (!archive->mapped_zip.ReadAtOffset(lfh_buf, sizeof(lfh_buf), local_header_offset)) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800511 ALOGW("Zip: failed reading lfh name from offset %" PRId64,
512 static_cast<int64_t>(local_header_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000513 return kIoError;
514 }
515
Narayan Kamath926973e2014-06-09 14:18:14 +0100516 const LocalFileHeader *lfh = reinterpret_cast<const LocalFileHeader*>(lfh_buf);
517
518 if (lfh->lfh_signature != LocalFileHeader::kSignature) {
Mark Salyzyn99ef9912014-03-14 14:26:22 -0700519 ALOGW("Zip: didn't find signature at start of lfh, offset=%" PRId64,
Narayan Kamath926973e2014-06-09 14:18:14 +0100520 static_cast<int64_t>(local_header_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000521 return kInvalidOffset;
522 }
523
524 // Paranoia: Match the values specified in the local file header
525 // to those specified in the central directory.
Adam Lesinskid987c9d2017-04-06 18:55:47 -0700526
Narayan Kamath162b7052017-06-05 13:21:12 +0100527 // Warn if central directory and local file header don't agree on the use
528 // of a trailing Data Descriptor. The reference implementation is inconsistent
529 // and appears to use the LFH value during extraction (unzip) but the CD value
530 // while displayng information about archives (zipinfo). The spec remains
531 // silent on this inconsistency as well.
532 //
533 // For now, always use the version from the LFH but make sure that the values
534 // specified in the central directory match those in the data descriptor.
535 //
536 // NOTE: It's also worth noting that unzip *does* warn about inconsistencies in
537 // bit 11 (EFS: The language encoding flag, marking that filename and comment are
538 // encoded using UTF-8). This implementation does not check for the presence of
539 // that flag and always enforces that entry names are valid UTF-8.
540 if ((lfh->gpb_flags & kGPBDDFlagMask) != (cdr->gpb_flags & kGPBDDFlagMask)) {
541 ALOGW("Zip: gpb flag mismatch at bit 3. expected {%04" PRIx16 "}, was {%04" PRIx16 "}",
Adam Lesinskid987c9d2017-04-06 18:55:47 -0700542 cdr->gpb_flags, lfh->gpb_flags);
Adam Lesinskid987c9d2017-04-06 18:55:47 -0700543 }
544
545 // If there is no trailing data descriptor, verify that the central directory and local file
546 // header agree on the crc, compressed, and uncompressed sizes of the entry.
Narayan Kamath926973e2014-06-09 14:18:14 +0100547 if ((lfh->gpb_flags & kGPBDDFlagMask) == 0) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000548 data->has_data_descriptor = 0;
Narayan Kamath926973e2014-06-09 14:18:14 +0100549 if (data->compressed_length != lfh->compressed_size
550 || data->uncompressed_length != lfh->uncompressed_size
551 || data->crc32 != lfh->crc32) {
Mark Salyzyn088bf902014-05-08 16:02:20 -0700552 ALOGW("Zip: size/crc32 mismatch. expected {%" PRIu32 ", %" PRIu32
553 ", %" PRIx32 "}, was {%" PRIu32 ", %" PRIu32 ", %" PRIx32 "}",
Narayan Kamath7462f022013-11-21 13:05:04 +0000554 data->compressed_length, data->uncompressed_length, data->crc32,
Narayan Kamath926973e2014-06-09 14:18:14 +0100555 lfh->compressed_size, lfh->uncompressed_size, lfh->crc32);
Narayan Kamath7462f022013-11-21 13:05:04 +0000556 return kInconsistentInformation;
557 }
558 } else {
559 data->has_data_descriptor = 1;
560 }
561
Elliott Hughes55fd2932017-05-28 22:59:04 -0700562 // 4.4.2.1: the upper byte of `version_made_by` gives the source OS. Unix is 3.
563 if ((cdr->version_made_by >> 8) == 3) {
564 data->unix_mode = (cdr->external_file_attributes >> 16) & 0xffff;
565 } else {
566 data->unix_mode = 0777;
567 }
568
Narayan Kamath7462f022013-11-21 13:05:04 +0000569 // Check that the local file header name matches the declared
570 // name in the central directory.
Narayan Kamath926973e2014-06-09 14:18:14 +0100571 if (lfh->file_name_length == nameLen) {
572 const off64_t name_offset = local_header_offset + sizeof(LocalFileHeader);
Mykola Kondratenko50afc152014-09-08 12:46:37 +0200573 if (name_offset + lfh->file_name_length > cd_offset) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000574 ALOGW("Zip: Invalid declared length");
575 return kInvalidOffset;
576 }
577
Tianjie Xu18c25922016-09-29 15:27:41 -0700578 std::vector<uint8_t> name_buf(nameLen);
579 if (!archive->mapped_zip.ReadAtOffset(name_buf.data(), nameLen, name_offset)) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800580 ALOGW("Zip: failed reading lfh name from offset %" PRId64, static_cast<int64_t>(name_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000581 return kIoError;
582 }
583
Tianjie Xu18c25922016-09-29 15:27:41 -0700584 if (memcmp(archive->hash_table[ent].name, name_buf.data(), nameLen)) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000585 return kInconsistentInformation;
586 }
587
Narayan Kamath7462f022013-11-21 13:05:04 +0000588 } else {
589 ALOGW("Zip: lfh name did not match central directory.");
590 return kInconsistentInformation;
591 }
592
Narayan Kamath926973e2014-06-09 14:18:14 +0100593 const off64_t data_offset = local_header_offset + sizeof(LocalFileHeader)
594 + lfh->file_name_length + lfh->extra_field_length;
Narayan Kamath48953a12014-01-24 12:32:39 +0000595 if (data_offset > cd_offset) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800596 ALOGW("Zip: bad data offset %" PRId64 " in zip", static_cast<int64_t>(data_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000597 return kInvalidOffset;
598 }
599
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800600 if (static_cast<off64_t>(data_offset + data->compressed_length) > cd_offset) {
Mark Salyzyn088bf902014-05-08 16:02:20 -0700601 ALOGW("Zip: bad compressed length in zip (%" PRId64 " + %" PRIu32 " > %" PRId64 ")",
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800602 static_cast<int64_t>(data_offset), data->compressed_length, static_cast<int64_t>(cd_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000603 return kInvalidOffset;
604 }
605
606 if (data->method == kCompressStored &&
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800607 static_cast<off64_t>(data_offset + data->uncompressed_length) > cd_offset) {
Mark Salyzyn088bf902014-05-08 16:02:20 -0700608 ALOGW("Zip: bad uncompressed length in zip (%" PRId64 " + %" PRIu32 " > %" PRId64 ")",
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800609 static_cast<int64_t>(data_offset), data->uncompressed_length,
610 static_cast<int64_t>(cd_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000611 return kInvalidOffset;
612 }
613
614 data->offset = data_offset;
615 return 0;
616}
617
618struct IterationHandle {
619 uint32_t position;
Piotr Jastrzebski10aa9a02014-08-19 09:01:20 +0100620 // We're not using vector here because this code is used in the Windows SDK
621 // where the STL is not available.
Yusuke Sato07447542015-06-25 14:39:19 -0700622 ZipString prefix;
623 ZipString suffix;
Narayan Kamath7462f022013-11-21 13:05:04 +0000624 ZipArchive* archive;
Piotr Jastrzebski8e085362014-08-18 11:37:45 +0100625
Yusuke Sato07447542015-06-25 14:39:19 -0700626 IterationHandle(const ZipString* in_prefix,
627 const ZipString* in_suffix) {
628 if (in_prefix) {
629 uint8_t* name_copy = new uint8_t[in_prefix->name_length];
630 memcpy(name_copy, in_prefix->name, in_prefix->name_length);
631 prefix.name = name_copy;
632 prefix.name_length = in_prefix->name_length;
633 } else {
634 prefix.name = NULL;
635 prefix.name_length = 0;
Yusuke Satof1d3d3b2015-06-25 14:09:00 -0700636 }
Yusuke Sato07447542015-06-25 14:39:19 -0700637 if (in_suffix) {
638 uint8_t* name_copy = new uint8_t[in_suffix->name_length];
639 memcpy(name_copy, in_suffix->name, in_suffix->name_length);
640 suffix.name = name_copy;
641 suffix.name_length = in_suffix->name_length;
642 } else {
643 suffix.name = NULL;
644 suffix.name_length = 0;
Yusuke Satof1d3d3b2015-06-25 14:09:00 -0700645 }
Piotr Jastrzebski8e085362014-08-18 11:37:45 +0100646 }
647
648 ~IterationHandle() {
Yusuke Sato07447542015-06-25 14:39:19 -0700649 delete[] prefix.name;
650 delete[] suffix.name;
Piotr Jastrzebski8e085362014-08-18 11:37:45 +0100651 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000652};
653
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100654int32_t StartIteration(ZipArchiveHandle handle, void** cookie_ptr,
Yusuke Sato07447542015-06-25 14:39:19 -0700655 const ZipString* optional_prefix,
656 const ZipString* optional_suffix) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800657 ZipArchive* archive = reinterpret_cast<ZipArchive*>(handle);
Narayan Kamath7462f022013-11-21 13:05:04 +0000658
659 if (archive == NULL || archive->hash_table == NULL) {
660 ALOGW("Zip: Invalid ZipArchiveHandle");
661 return kInvalidHandle;
662 }
663
Yusuke Satof1d3d3b2015-06-25 14:09:00 -0700664 IterationHandle* cookie = new IterationHandle(optional_prefix, optional_suffix);
Narayan Kamath7462f022013-11-21 13:05:04 +0000665 cookie->position = 0;
Narayan Kamath7462f022013-11-21 13:05:04 +0000666 cookie->archive = archive;
Narayan Kamath7462f022013-11-21 13:05:04 +0000667
668 *cookie_ptr = cookie ;
669 return 0;
670}
671
Piotr Jastrzebski79c8b342014-08-08 14:02:17 +0100672void EndIteration(void* cookie) {
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100673 delete reinterpret_cast<IterationHandle*>(cookie);
Piotr Jastrzebski79c8b342014-08-08 14:02:17 +0100674}
675
Yusuke Sato07447542015-06-25 14:39:19 -0700676int32_t FindEntry(const ZipArchiveHandle handle, const ZipString& entryName,
Narayan Kamath7462f022013-11-21 13:05:04 +0000677 ZipEntry* data) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800678 const ZipArchive* archive = reinterpret_cast<ZipArchive*>(handle);
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100679 if (entryName.name_length == 0) {
680 ALOGW("Zip: Invalid filename %.*s", entryName.name_length, entryName.name);
Narayan Kamath7462f022013-11-21 13:05:04 +0000681 return kInvalidEntryName;
682 }
683
684 const int64_t ent = EntryToIndex(archive->hash_table,
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100685 archive->hash_table_size, entryName);
Narayan Kamath7462f022013-11-21 13:05:04 +0000686
687 if (ent < 0) {
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100688 ALOGV("Zip: Could not find entry %.*s", entryName.name_length, entryName.name);
Narayan Kamath7462f022013-11-21 13:05:04 +0000689 return ent;
690 }
691
692 return FindEntry(archive, ent, data);
693}
694
Yusuke Sato07447542015-06-25 14:39:19 -0700695int32_t Next(void* cookie, ZipEntry* data, ZipString* name) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800696 IterationHandle* handle = reinterpret_cast<IterationHandle*>(cookie);
Narayan Kamath7462f022013-11-21 13:05:04 +0000697 if (handle == NULL) {
698 return kInvalidHandle;
699 }
700
701 ZipArchive* archive = handle->archive;
702 if (archive == NULL || archive->hash_table == NULL) {
703 ALOGW("Zip: Invalid ZipArchiveHandle");
704 return kInvalidHandle;
705 }
706
707 const uint32_t currentOffset = handle->position;
708 const uint32_t hash_table_length = archive->hash_table_size;
Yusuke Sato07447542015-06-25 14:39:19 -0700709 const ZipString* hash_table = archive->hash_table;
Narayan Kamath7462f022013-11-21 13:05:04 +0000710
711 for (uint32_t i = currentOffset; i < hash_table_length; ++i) {
712 if (hash_table[i].name != NULL &&
Yusuke Sato07447542015-06-25 14:39:19 -0700713 (handle->prefix.name_length == 0 ||
714 hash_table[i].StartsWith(handle->prefix)) &&
715 (handle->suffix.name_length == 0 ||
716 hash_table[i].EndsWith(handle->suffix))) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000717 handle->position = (i + 1);
718 const int error = FindEntry(archive, i, data);
719 if (!error) {
720 name->name = hash_table[i].name;
721 name->name_length = hash_table[i].name_length;
722 }
723
724 return error;
725 }
726 }
727
728 handle->position = 0;
729 return kIterationEnd;
730}
731
Narayan Kamathf899bd52015-04-17 11:53:14 +0100732class Writer {
733 public:
734 virtual bool Append(uint8_t* buf, size_t buf_size) = 0;
735 virtual ~Writer() {}
736 protected:
737 Writer() = default;
738 private:
739 DISALLOW_COPY_AND_ASSIGN(Writer);
740};
741
742// A Writer that writes data to a fixed size memory region.
743// The size of the memory region must be equal to the total size of
744// the data appended to it.
745class MemoryWriter : public Writer {
746 public:
747 MemoryWriter(uint8_t* buf, size_t size) : Writer(),
748 buf_(buf), size_(size), bytes_written_(0) {
749 }
750
751 virtual bool Append(uint8_t* buf, size_t buf_size) override {
752 if (bytes_written_ + buf_size > size_) {
753 ALOGW("Zip: Unexpected size " ZD " (declared) vs " ZD " (actual)",
754 size_, bytes_written_ + buf_size);
755 return false;
756 }
757
758 memcpy(buf_ + bytes_written_, buf, buf_size);
759 bytes_written_ += buf_size;
760 return true;
761 }
762
763 private:
764 uint8_t* const buf_;
765 const size_t size_;
766 size_t bytes_written_;
767};
768
769// A Writer that appends data to a file |fd| at its current position.
770// The file will be truncated to the end of the written data.
771class FileWriter : public Writer {
772 public:
773
774 // Creates a FileWriter for |fd| and prepare to write |entry| to it,
775 // guaranteeing that the file descriptor is valid and that there's enough
776 // space on the volume to write out the entry completely and that the file
Tao Baoa456c212016-11-15 10:08:07 -0800777 // is truncated to the correct length (no truncation if |fd| references a
778 // block device).
Narayan Kamathf899bd52015-04-17 11:53:14 +0100779 //
780 // Returns a valid FileWriter on success, |nullptr| if an error occurred.
781 static std::unique_ptr<FileWriter> Create(int fd, const ZipEntry* entry) {
782 const uint32_t declared_length = entry->uncompressed_length;
783 const off64_t current_offset = lseek64(fd, 0, SEEK_CUR);
784 if (current_offset == -1) {
785 ALOGW("Zip: unable to seek to current location on fd %d: %s", fd, strerror(errno));
786 return nullptr;
787 }
788
789 int result = 0;
790#if defined(__linux__)
791 if (declared_length > 0) {
792 // Make sure we have enough space on the volume to extract the compressed
793 // entry. Note that the call to ftruncate below will change the file size but
794 // will not allocate space on disk and this call to fallocate will not
795 // change the file size.
Badhri Jagan Sridharana68d0d12015-06-02 14:47:57 -0700796 // Note: fallocate is only supported by the following filesystems -
797 // btrfs, ext4, ocfs2, and xfs. Therefore fallocate might fail with
798 // EOPNOTSUPP error when issued in other filesystems.
799 // Hence, check for the return error code before concluding that the
800 // disk does not have enough space.
Narayan Kamathf899bd52015-04-17 11:53:14 +0100801 result = TEMP_FAILURE_RETRY(fallocate(fd, 0, current_offset, declared_length));
Badhri Jagan Sridharana68d0d12015-06-02 14:47:57 -0700802 if (result == -1 && errno == ENOSPC) {
Narayan Kamathd5d7abe2016-08-10 12:24:05 +0100803 ALOGW("Zip: unable to allocate %" PRId64 " bytes at offset %" PRId64 " : %s",
804 static_cast<int64_t>(declared_length), static_cast<int64_t>(current_offset),
805 strerror(errno));
Narayan Kamathf899bd52015-04-17 11:53:14 +0100806 return std::unique_ptr<FileWriter>(nullptr);
807 }
808 }
809#endif // __linux__
810
Tao Baoa456c212016-11-15 10:08:07 -0800811 struct stat sb;
812 if (fstat(fd, &sb) == -1) {
813 ALOGW("Zip: unable to fstat file: %s", strerror(errno));
Narayan Kamathf899bd52015-04-17 11:53:14 +0100814 return std::unique_ptr<FileWriter>(nullptr);
815 }
816
Tao Baoa456c212016-11-15 10:08:07 -0800817 // Block device doesn't support ftruncate(2).
818 if (!S_ISBLK(sb.st_mode)) {
819 result = TEMP_FAILURE_RETRY(ftruncate(fd, declared_length + current_offset));
820 if (result == -1) {
821 ALOGW("Zip: unable to truncate file to %" PRId64 ": %s",
822 static_cast<int64_t>(declared_length + current_offset), strerror(errno));
823 return std::unique_ptr<FileWriter>(nullptr);
824 }
825 }
826
Narayan Kamathf899bd52015-04-17 11:53:14 +0100827 return std::unique_ptr<FileWriter>(new FileWriter(fd, declared_length));
828 }
829
830 virtual bool Append(uint8_t* buf, size_t buf_size) override {
831 if (total_bytes_written_ + buf_size > declared_length_) {
832 ALOGW("Zip: Unexpected size " ZD " (declared) vs " ZD " (actual)",
833 declared_length_, total_bytes_written_ + buf_size);
834 return false;
835 }
836
Narayan Kamathe97e66e2015-04-27 16:25:53 +0100837 const bool result = android::base::WriteFully(fd_, buf, buf_size);
838 if (result) {
839 total_bytes_written_ += buf_size;
840 } else {
841 ALOGW("Zip: unable to write " ZD " bytes to file; %s", buf_size, strerror(errno));
Narayan Kamathf899bd52015-04-17 11:53:14 +0100842 }
843
Narayan Kamathe97e66e2015-04-27 16:25:53 +0100844 return result;
Narayan Kamathf899bd52015-04-17 11:53:14 +0100845 }
846 private:
847 FileWriter(const int fd, const size_t declared_length) :
848 Writer(),
849 fd_(fd),
850 declared_length_(declared_length),
851 total_bytes_written_(0) {
852 }
853
854 const int fd_;
855 const size_t declared_length_;
856 size_t total_bytes_written_;
857};
858
Dmitriy Ivanovf94e1592015-03-06 13:27:59 -0800859// This method is using libz macros with old-style-casts
860#pragma GCC diagnostic push
861#pragma GCC diagnostic ignored "-Wold-style-cast"
862static inline int zlib_inflateInit2(z_stream* stream, int window_bits) {
863 return inflateInit2(stream, window_bits);
864}
865#pragma GCC diagnostic pop
866
Tianjie Xu18c25922016-09-29 15:27:41 -0700867static int32_t InflateEntryToWriter(MappedZipFile& mapped_zip, const ZipEntry* entry,
Narayan Kamathf899bd52015-04-17 11:53:14 +0100868 Writer* writer, uint64_t* crc_out) {
Dmitriy Ivanovedbabfe2015-03-12 09:58:15 -0700869 const size_t kBufSize = 32768;
870 std::vector<uint8_t> read_buf(kBufSize);
871 std::vector<uint8_t> write_buf(kBufSize);
Narayan Kamath7462f022013-11-21 13:05:04 +0000872 z_stream zstream;
873 int zerr;
874
875 /*
876 * Initialize the zlib stream struct.
877 */
878 memset(&zstream, 0, sizeof(zstream));
879 zstream.zalloc = Z_NULL;
880 zstream.zfree = Z_NULL;
881 zstream.opaque = Z_NULL;
882 zstream.next_in = NULL;
883 zstream.avail_in = 0;
Dmitriy Ivanovedbabfe2015-03-12 09:58:15 -0700884 zstream.next_out = &write_buf[0];
Narayan Kamath7462f022013-11-21 13:05:04 +0000885 zstream.avail_out = kBufSize;
886 zstream.data_type = Z_UNKNOWN;
887
888 /*
889 * Use the undocumented "negative window bits" feature to tell zlib
890 * that there's no zlib header waiting for it.
891 */
Dmitriy Ivanovf94e1592015-03-06 13:27:59 -0800892 zerr = zlib_inflateInit2(&zstream, -MAX_WBITS);
Narayan Kamath7462f022013-11-21 13:05:04 +0000893 if (zerr != Z_OK) {
894 if (zerr == Z_VERSION_ERROR) {
895 ALOGE("Installed zlib is not compatible with linked version (%s)",
896 ZLIB_VERSION);
897 } else {
898 ALOGW("Call to inflateInit2 failed (zerr=%d)", zerr);
899 }
900
901 return kZlibError;
902 }
903
Dmitriy Ivanov1f741e52015-03-06 14:26:37 -0800904 auto zstream_deleter = [](z_stream* stream) {
905 inflateEnd(stream); /* free up any allocated structures */
906 };
907
908 std::unique_ptr<z_stream, decltype(zstream_deleter)> zstream_guard(&zstream, zstream_deleter);
909
Narayan Kamath7462f022013-11-21 13:05:04 +0000910 const uint32_t uncompressed_length = entry->uncompressed_length;
911
Narayan Kamath162b7052017-06-05 13:21:12 +0100912 uint64_t crc = 0;
Narayan Kamath7462f022013-11-21 13:05:04 +0000913 uint32_t compressed_length = entry->compressed_length;
Narayan Kamath7462f022013-11-21 13:05:04 +0000914 do {
915 /* read as much as we can */
916 if (zstream.avail_in == 0) {
Yabin Cuib2a77002016-02-08 16:26:33 -0800917 const size_t getSize = (compressed_length > kBufSize) ? kBufSize : compressed_length;
Tianjie Xu18c25922016-09-29 15:27:41 -0700918 if (!mapped_zip.ReadData(read_buf.data(), getSize)) {
Yabin Cuib2a77002016-02-08 16:26:33 -0800919 ALOGW("Zip: inflate read failed, getSize = %zu: %s", getSize, strerror(errno));
Dmitriy Ivanov1f741e52015-03-06 14:26:37 -0800920 return kIoError;
Narayan Kamath7462f022013-11-21 13:05:04 +0000921 }
922
923 compressed_length -= getSize;
924
Dmitriy Ivanovedbabfe2015-03-12 09:58:15 -0700925 zstream.next_in = &read_buf[0];
Narayan Kamath7462f022013-11-21 13:05:04 +0000926 zstream.avail_in = getSize;
927 }
928
929 /* uncompress the data */
930 zerr = inflate(&zstream, Z_NO_FLUSH);
931 if (zerr != Z_OK && zerr != Z_STREAM_END) {
932 ALOGW("Zip: inflate zerr=%d (nIn=%p aIn=%u nOut=%p aOut=%u)",
933 zerr, zstream.next_in, zstream.avail_in,
934 zstream.next_out, zstream.avail_out);
Dmitriy Ivanov1f741e52015-03-06 14:26:37 -0800935 return kZlibError;
Narayan Kamath7462f022013-11-21 13:05:04 +0000936 }
937
938 /* write when we're full or when we're done */
939 if (zstream.avail_out == 0 ||
940 (zerr == Z_STREAM_END && zstream.avail_out != kBufSize)) {
Dmitriy Ivanovedbabfe2015-03-12 09:58:15 -0700941 const size_t write_size = zstream.next_out - &write_buf[0];
Narayan Kamathf899bd52015-04-17 11:53:14 +0100942 if (!writer->Append(&write_buf[0], write_size)) {
943 // The file might have declared a bogus length.
944 return kInconsistentInformation;
Narayan Kamath162b7052017-06-05 13:21:12 +0100945 } else {
946 crc = crc32(crc, &write_buf[0], write_size);
Narayan Kamath7462f022013-11-21 13:05:04 +0000947 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000948
Dmitriy Ivanovedbabfe2015-03-12 09:58:15 -0700949 zstream.next_out = &write_buf[0];
Narayan Kamath7462f022013-11-21 13:05:04 +0000950 zstream.avail_out = kBufSize;
951 }
952 } while (zerr == Z_OK);
953
954 assert(zerr == Z_STREAM_END); /* other errors should've been caught */
955
Narayan Kamath162b7052017-06-05 13:21:12 +0100956 // NOTE: zstream.adler is always set to 0, because we're using the -MAX_WBITS
957 // "feature" of zlib to tell it there won't be a zlib file header. zlib
958 // doesn't bother calculating the checksum in that scenario. We just do
959 // it ourselves above because there are no additional gains to be made by
960 // having zlib calculate it for us, since they do it by calling crc32 in
961 // the same manner that we have above.
962 *crc_out = crc;
Narayan Kamath7462f022013-11-21 13:05:04 +0000963
964 if (zstream.total_out != uncompressed_length || compressed_length != 0) {
Mark Salyzyn088bf902014-05-08 16:02:20 -0700965 ALOGW("Zip: size mismatch on inflated file (%lu vs %" PRIu32 ")",
Narayan Kamath7462f022013-11-21 13:05:04 +0000966 zstream.total_out, uncompressed_length);
Dmitriy Ivanov1f741e52015-03-06 14:26:37 -0800967 return kInconsistentInformation;
Narayan Kamath7462f022013-11-21 13:05:04 +0000968 }
969
Dmitriy Ivanov1f741e52015-03-06 14:26:37 -0800970 return 0;
Narayan Kamath7462f022013-11-21 13:05:04 +0000971}
972
Tianjie Xu18c25922016-09-29 15:27:41 -0700973static int32_t CopyEntryToWriter(MappedZipFile& mapped_zip, const ZipEntry* entry, Writer* writer,
Narayan Kamathf899bd52015-04-17 11:53:14 +0100974 uint64_t *crc_out) {
975 static const uint32_t kBufSize = 32768;
976 std::vector<uint8_t> buf(kBufSize);
977
978 const uint32_t length = entry->uncompressed_length;
979 uint32_t count = 0;
980 uint64_t crc = 0;
981 while (count < length) {
982 uint32_t remaining = length - count;
983
984 // Safe conversion because kBufSize is narrow enough for a 32 bit signed
985 // value.
Yabin Cuib2a77002016-02-08 16:26:33 -0800986 const size_t block_size = (remaining > kBufSize) ? kBufSize : remaining;
Tianjie Xu18c25922016-09-29 15:27:41 -0700987 if (!mapped_zip.ReadData(buf.data(), block_size)) {
Yabin Cuib2a77002016-02-08 16:26:33 -0800988 ALOGW("CopyFileToFile: copy read failed, block_size = %zu: %s", block_size, strerror(errno));
Narayan Kamathf899bd52015-04-17 11:53:14 +0100989 return kIoError;
990 }
991
992 if (!writer->Append(&buf[0], block_size)) {
993 return kIoError;
994 }
995 crc = crc32(crc, &buf[0], block_size);
996 count += block_size;
997 }
998
999 *crc_out = crc;
1000
1001 return 0;
1002}
1003
1004int32_t ExtractToWriter(ZipArchiveHandle handle,
1005 ZipEntry* entry, Writer* writer) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -08001006 ZipArchive* archive = reinterpret_cast<ZipArchive*>(handle);
Narayan Kamath7462f022013-11-21 13:05:04 +00001007 const uint16_t method = entry->method;
1008 off64_t data_offset = entry->offset;
1009
Tianjie Xu18c25922016-09-29 15:27:41 -07001010 if (!archive->mapped_zip.SeekToOffset(data_offset)) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -08001011 ALOGW("Zip: lseek to data at %" PRId64 " failed", static_cast<int64_t>(data_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +00001012 return kIoError;
1013 }
1014
1015 // this should default to kUnknownCompressionMethod.
1016 int32_t return_value = -1;
1017 uint64_t crc = 0;
1018 if (method == kCompressStored) {
Tianjie Xu18c25922016-09-29 15:27:41 -07001019 return_value = CopyEntryToWriter(archive->mapped_zip, entry, writer, &crc);
Narayan Kamath7462f022013-11-21 13:05:04 +00001020 } else if (method == kCompressDeflated) {
Tianjie Xu18c25922016-09-29 15:27:41 -07001021 return_value = InflateEntryToWriter(archive->mapped_zip, entry, writer, &crc);
Narayan Kamath7462f022013-11-21 13:05:04 +00001022 }
1023
1024 if (!return_value && entry->has_data_descriptor) {
Narayan Kamath162b7052017-06-05 13:21:12 +01001025 return_value = ValidateDataDescriptor(archive->mapped_zip, entry);
Narayan Kamath7462f022013-11-21 13:05:04 +00001026 if (return_value) {
1027 return return_value;
1028 }
1029 }
1030
Narayan Kamath162b7052017-06-05 13:21:12 +01001031 // Validate that the CRC matches the calculated value.
1032 if (kCrcChecksEnabled && (entry->crc32 != static_cast<uint32_t>(crc))) {
Mark Salyzyn088bf902014-05-08 16:02:20 -07001033 ALOGW("Zip: crc mismatch: expected %" PRIu32 ", was %" PRIu64, entry->crc32, crc);
Narayan Kamath7462f022013-11-21 13:05:04 +00001034 return kInconsistentInformation;
1035 }
1036
1037 return return_value;
1038}
1039
Narayan Kamathf899bd52015-04-17 11:53:14 +01001040int32_t ExtractToMemory(ZipArchiveHandle handle, ZipEntry* entry,
1041 uint8_t* begin, uint32_t size) {
1042 std::unique_ptr<Writer> writer(new MemoryWriter(begin, size));
1043 return ExtractToWriter(handle, entry, writer.get());
1044}
1045
Narayan Kamath7462f022013-11-21 13:05:04 +00001046int32_t ExtractEntryToFile(ZipArchiveHandle handle,
1047 ZipEntry* entry, int fd) {
Narayan Kamathf899bd52015-04-17 11:53:14 +01001048 std::unique_ptr<Writer> writer(FileWriter::Create(fd, entry));
1049 if (writer.get() == nullptr) {
Narayan Kamath7462f022013-11-21 13:05:04 +00001050 return kIoError;
1051 }
1052
Narayan Kamathf899bd52015-04-17 11:53:14 +01001053 return ExtractToWriter(handle, entry, writer.get());
Narayan Kamath7462f022013-11-21 13:05:04 +00001054}
1055
1056const char* ErrorCodeString(int32_t error_code) {
Narayan Kamath1ef9d2d2017-06-15 13:58:25 +01001057 // Make sure that the number of entries in kErrorMessages and ErrorCodes
1058 // match.
1059 static_assert((-kLastErrorCode + 1) == arraysize(kErrorMessages),
1060 "(-kLastErrorCode + 1) != arraysize(kErrorMessages)");
1061
1062 const uint32_t idx = -error_code;
1063 if (idx < arraysize(kErrorMessages)) {
1064 return kErrorMessages[idx];
Narayan Kamath7462f022013-11-21 13:05:04 +00001065 }
1066
Narayan Kamath1ef9d2d2017-06-15 13:58:25 +01001067 return "Unknown return code";
Narayan Kamath7462f022013-11-21 13:05:04 +00001068}
1069
1070int GetFileDescriptor(const ZipArchiveHandle handle) {
Tianjie Xu18c25922016-09-29 15:27:41 -07001071 return reinterpret_cast<ZipArchive*>(handle)->mapped_zip.GetFileDescriptor();
Narayan Kamath7462f022013-11-21 13:05:04 +00001072}
Colin Cross7c6c7f02016-09-16 10:15:51 -07001073
1074ZipString::ZipString(const char* entry_name)
1075 : name(reinterpret_cast<const uint8_t*>(entry_name)) {
1076 size_t len = strlen(entry_name);
1077 CHECK_LE(len, static_cast<size_t>(UINT16_MAX));
1078 name_length = static_cast<uint16_t>(len);
1079}
Tianjie Xu18c25922016-09-29 15:27:41 -07001080
1081#if !defined(_WIN32)
1082class ProcessWriter : public Writer {
1083 public:
1084 ProcessWriter(ProcessZipEntryFunction func, void* cookie) : Writer(),
1085 proc_function_(func),
1086 cookie_(cookie) {
1087 }
1088
1089 virtual bool Append(uint8_t* buf, size_t buf_size) override {
1090 return proc_function_(buf, buf_size, cookie_);
1091 }
1092
1093 private:
1094 ProcessZipEntryFunction proc_function_;
1095 void* cookie_;
1096};
1097
1098int32_t ProcessZipEntryContents(ZipArchiveHandle handle, ZipEntry* entry,
1099 ProcessZipEntryFunction func, void* cookie) {
1100 ProcessWriter writer(func, cookie);
1101 return ExtractToWriter(handle, entry, &writer);
1102}
1103
1104#endif //!defined(_WIN32)
1105
1106int MappedZipFile::GetFileDescriptor() const {
1107 if (!has_fd_) {
1108 ALOGW("Zip: MappedZipFile doesn't have a file descriptor.");
1109 return -1;
1110 }
1111 return fd_;
1112}
1113
1114void* MappedZipFile::GetBasePtr() const {
1115 if (has_fd_) {
1116 ALOGW("Zip: MappedZipFile doesn't have a base pointer.");
1117 return nullptr;
1118 }
1119 return base_ptr_;
1120}
1121
1122off64_t MappedZipFile::GetFileLength() const {
1123 if (has_fd_) {
1124 off64_t result = lseek64(fd_, 0, SEEK_END);
1125 if (result == -1) {
1126 ALOGE("Zip: lseek on fd %d failed: %s", fd_, strerror(errno));
1127 }
1128 return result;
1129 } else {
1130 if (base_ptr_ == nullptr) {
1131 ALOGE("Zip: invalid file map\n");
1132 return -1;
1133 }
1134 return static_cast<off64_t>(data_length_);
1135 }
1136}
1137
1138bool MappedZipFile::SeekToOffset(off64_t offset) {
1139 if (has_fd_) {
1140 if (lseek64(fd_, offset, SEEK_SET) != offset) {
1141 ALOGE("Zip: lseek to %" PRId64 " failed: %s\n", offset, strerror(errno));
1142 return false;
1143 }
1144 return true;
1145 } else {
1146 if (offset < 0 || offset > static_cast<off64_t>(data_length_)) {
1147 ALOGE("Zip: invalid offset: %" PRId64 ", data length: %" PRId64 "\n" , offset,
1148 data_length_);
1149 return false;
1150 }
1151
1152 read_pos_ = offset;
1153 return true;
1154 }
1155}
1156
1157bool MappedZipFile::ReadData(uint8_t* buffer, size_t read_amount) {
1158 if (has_fd_) {
1159 if(!android::base::ReadFully(fd_, buffer, read_amount)) {
1160 ALOGE("Zip: read from %d failed\n", fd_);
1161 return false;
1162 }
1163 } else {
1164 memcpy(buffer, static_cast<uint8_t*>(base_ptr_) + read_pos_, read_amount);
1165 read_pos_ += read_amount;
1166 }
1167 return true;
1168}
1169
1170// Attempts to read |len| bytes into |buf| at offset |off|.
1171bool MappedZipFile::ReadAtOffset(uint8_t* buf, size_t len, off64_t off) {
1172#if !defined(_WIN32)
1173 if (has_fd_) {
1174 if (static_cast<size_t>(TEMP_FAILURE_RETRY(pread64(fd_, buf, len, off))) != len) {
1175 ALOGE("Zip: failed to read at offset %" PRId64 "\n", off);
1176 return false;
1177 }
1178 return true;
1179 }
1180#endif
1181 if (!SeekToOffset(off)) {
1182 return false;
1183 }
1184 return ReadData(buf, len);
1185
1186}
1187
1188void CentralDirectory::Initialize(void* map_base_ptr, off64_t cd_start_offset, size_t cd_size) {
1189 base_ptr_ = static_cast<uint8_t*>(map_base_ptr) + cd_start_offset;
1190 length_ = cd_size;
1191}
1192
1193bool ZipArchive::InitializeCentralDirectory(const char* debug_file_name, off64_t cd_start_offset,
1194 size_t cd_size) {
1195 if (mapped_zip.HasFd()) {
1196 if (!directory_map->create(debug_file_name, mapped_zip.GetFileDescriptor(),
1197 cd_start_offset, cd_size, true /* read only */)) {
1198 return false;
1199 }
1200
1201 CHECK_EQ(directory_map->getDataLength(), cd_size);
1202 central_directory.Initialize(directory_map->getDataPtr(), 0/*offset*/, cd_size);
1203 } else {
1204 if (mapped_zip.GetBasePtr() == nullptr) {
1205 ALOGE("Zip: Failed to map central directory, bad mapped_zip base pointer\n");
1206 return false;
1207 }
1208 if (static_cast<off64_t>(cd_start_offset) + static_cast<off64_t>(cd_size) >
1209 mapped_zip.GetFileLength()) {
1210 ALOGE("Zip: Failed to map central directory, offset exceeds mapped memory region ("
1211 "start_offset %" PRId64 ", cd_size %zu, mapped_region_size %" PRId64 ")",
1212 static_cast<int64_t>(cd_start_offset), cd_size, mapped_zip.GetFileLength());
1213 return false;
1214 }
1215
1216 central_directory.Initialize(mapped_zip.GetBasePtr(), cd_start_offset, cd_size);
1217 }
1218 return true;
1219}
Elliott Hughes55fd2932017-05-28 22:59:04 -07001220
1221tm ZipEntry::GetModificationTime() const {
1222 tm t = {};
1223
1224 t.tm_hour = (mod_time >> 11) & 0x1f;
1225 t.tm_min = (mod_time >> 5) & 0x3f;
1226 t.tm_sec = (mod_time & 0x1f) << 1;
1227
1228 t.tm_year = ((mod_time >> 25) & 0x7f) + 80;
1229 t.tm_mon = ((mod_time >> 21) & 0xf) - 1;
1230 t.tm_mday = (mod_time >> 16) & 0x1f;
1231
1232 return t;
1233}