blob: 4bc2669a92de72a6ba6dc033bdd97324022153b9 [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>
Narayan Kamath7462f022013-11-21 13:05:04 +000030#include <unistd.h>
31
Dan Albert1ae07642015-04-09 14:11:18 -070032#include <memory>
33#include <vector>
34
Mark Salyzynff2dcd92016-09-28 15:54:45 -070035#include <android-base/file.h>
36#include <android-base/logging.h>
37#include <android-base/macros.h> // TEMP_FAILURE_RETRY may or may not be in unistd
38#include <android-base/memory.h>
Mark Salyzyncfd5b082016-10-17 14:28:00 -070039#include <log/log.h>
Mark Salyzynff2dcd92016-09-28 15:54:45 -070040#include <utils/Compat.h>
41#include <utils/FileMap.h>
Christopher Ferrise6884ce2015-11-10 14:55:12 -080042#include "ziparchive/zip_archive.h"
Dan Albert1ae07642015-04-09 14:11:18 -070043#include "zlib.h"
Narayan Kamath7462f022013-11-21 13:05:04 +000044
Narayan Kamath044bc8e2014-12-03 18:22:53 +000045#include "entry_name_utils-inl.h"
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -070046#include "zip_archive_common.h"
Christopher Ferrise6884ce2015-11-10 14:55:12 -080047#include "zip_archive_private.h"
Mark Salyzyn99ef9912014-03-14 14:26:22 -070048
Dan Albert1ae07642015-04-09 14:11:18 -070049using android::base::get_unaligned;
Narayan Kamath044bc8e2014-12-03 18:22:53 +000050
Narayan Kamath162b7052017-06-05 13:21:12 +010051// Used to turn on crc checks - verify that the content CRC matches the values
52// specified in the local file header and the central directory.
53static const bool kCrcChecksEnabled = false;
54
Narayan Kamath926973e2014-06-09 14:18:14 +010055// This is for windows. If we don't open a file in binary mode, weird
Narayan Kamath7462f022013-11-21 13:05:04 +000056// things will happen.
57#ifndef O_BINARY
58#define O_BINARY 0
59#endif
60
Narayan Kamath926973e2014-06-09 14:18:14 +010061// The maximum number of bytes to scan backwards for the EOCD start.
62static const uint32_t kMaxEOCDSearch = kMaxCommentLen + sizeof(EocdRecord);
63
Narayan Kamath7462f022013-11-21 13:05:04 +000064/*
65 * A Read-only Zip archive.
66 *
67 * We want "open" and "find entry by name" to be fast operations, and
68 * we want to use as little memory as possible. We memory-map the zip
69 * central directory, and load a hash table with pointers to the filenames
70 * (which aren't null-terminated). The other fields are at a fixed offset
71 * from the filename, so we don't need to extract those (but we do need
72 * to byte-read and endian-swap them every time we want them).
73 *
74 * It's possible that somebody has handed us a massive (~1GB) zip archive,
75 * so we can't expect to mmap the entire file.
76 *
77 * To speed comparisons when doing a lookup by name, we could make the mapping
78 * "private" (copy-on-write) and null-terminate the filenames after verifying
79 * the record structure. However, this requires a private mapping of
80 * every page that the Central Directory touches. Easier to tuck a copy
81 * of the string length into the hash table entry.
82 */
Narayan Kamath7462f022013-11-21 13:05:04 +000083
Narayan Kamath7462f022013-11-21 13:05:04 +000084/*
85 * Round up to the next highest power of 2.
86 *
87 * Found on http://graphics.stanford.edu/~seander/bithacks.html.
88 */
89static uint32_t RoundUpPower2(uint32_t val) {
90 val--;
91 val |= val >> 1;
92 val |= val >> 2;
93 val |= val >> 4;
94 val |= val >> 8;
95 val |= val >> 16;
96 val++;
97
98 return val;
99}
100
Yusuke Sato07447542015-06-25 14:39:19 -0700101static uint32_t ComputeHash(const ZipString& name) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000102 uint32_t hash = 0;
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100103 uint16_t len = name.name_length;
104 const uint8_t* str = name.name;
Narayan Kamath7462f022013-11-21 13:05:04 +0000105
106 while (len--) {
107 hash = hash * 31 + *str++;
108 }
109
110 return hash;
111}
112
113/*
114 * Convert a ZipEntry to a hash table index, verifying that it's in a
115 * valid range.
116 */
Yusuke Sato07447542015-06-25 14:39:19 -0700117static int64_t EntryToIndex(const ZipString* hash_table,
Narayan Kamath7462f022013-11-21 13:05:04 +0000118 const uint32_t hash_table_size,
Yusuke Sato07447542015-06-25 14:39:19 -0700119 const ZipString& name) {
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100120 const uint32_t hash = ComputeHash(name);
Narayan Kamath7462f022013-11-21 13:05:04 +0000121
122 // NOTE: (hash_table_size - 1) is guaranteed to be non-negative.
123 uint32_t ent = hash & (hash_table_size - 1);
124 while (hash_table[ent].name != NULL) {
Yusuke Sato07447542015-06-25 14:39:19 -0700125 if (hash_table[ent] == name) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000126 return ent;
127 }
128
129 ent = (ent + 1) & (hash_table_size - 1);
130 }
131
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100132 ALOGV("Zip: Unable to find entry %.*s", name.name_length, name.name);
Narayan Kamath7462f022013-11-21 13:05:04 +0000133 return kEntryNotFound;
134}
135
136/*
137 * Add a new entry to the hash table.
138 */
Yusuke Sato07447542015-06-25 14:39:19 -0700139static int32_t AddToHash(ZipString *hash_table, const uint64_t hash_table_size,
140 const ZipString& name) {
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100141 const uint64_t hash = ComputeHash(name);
Narayan Kamath7462f022013-11-21 13:05:04 +0000142 uint32_t ent = hash & (hash_table_size - 1);
143
144 /*
145 * We over-allocated the table, so we're guaranteed to find an empty slot.
146 * Further, we guarantee that the hashtable size is not 0.
147 */
148 while (hash_table[ent].name != NULL) {
Yusuke Sato07447542015-06-25 14:39:19 -0700149 if (hash_table[ent] == name) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000150 // We've found a duplicate entry. We don't accept it
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100151 ALOGW("Zip: Found duplicate entry %.*s", name.name_length, name.name);
Narayan Kamath7462f022013-11-21 13:05:04 +0000152 return kDuplicateEntry;
153 }
154 ent = (ent + 1) & (hash_table_size - 1);
155 }
156
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100157 hash_table[ent].name = name.name;
158 hash_table[ent].name_length = name.name_length;
Narayan Kamath7462f022013-11-21 13:05:04 +0000159 return 0;
160}
161
Tianjie Xu18c25922016-09-29 15:27:41 -0700162static int32_t MapCentralDirectory0(const char* debug_file_name, ZipArchive* archive,
163 off64_t file_length, off64_t read_amount,
164 uint8_t* scan_buffer) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000165 const off64_t search_start = file_length - read_amount;
166
Tianjie Xu18c25922016-09-29 15:27:41 -0700167 if(!archive->mapped_zip.ReadAtOffset(scan_buffer, read_amount, search_start)) {
168 ALOGE("Zip: read %" PRId64 " from offset %" PRId64 " failed",
169 static_cast<int64_t>(read_amount), static_cast<int64_t>(search_start));
Narayan Kamath7462f022013-11-21 13:05:04 +0000170 return kIoError;
171 }
172
173 /*
174 * Scan backward for the EOCD magic. In an archive without a trailing
175 * comment, we'll find it on the first try. (We may want to consider
176 * doing an initial minimal read; if we don't find it, retry with a
177 * second read as above.)
178 */
Narayan Kamath926973e2014-06-09 14:18:14 +0100179 int i = read_amount - sizeof(EocdRecord);
180 for (; i >= 0; i--) {
Dan Albert1ae07642015-04-09 14:11:18 -0700181 if (scan_buffer[i] == 0x50) {
182 uint32_t* sig_addr = reinterpret_cast<uint32_t*>(&scan_buffer[i]);
183 if (get_unaligned<uint32_t>(sig_addr) == EocdRecord::kSignature) {
184 ALOGV("+++ Found EOCD at buf+%d", i);
185 break;
186 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000187 }
188 }
189 if (i < 0) {
190 ALOGD("Zip: EOCD not found, %s is not zip", debug_file_name);
191 return kInvalidFile;
192 }
193
194 const off64_t eocd_offset = search_start + i;
Narayan Kamath926973e2014-06-09 14:18:14 +0100195 const EocdRecord* eocd = reinterpret_cast<const EocdRecord*>(scan_buffer + i);
Narayan Kamath7462f022013-11-21 13:05:04 +0000196 /*
Narayan Kamath926973e2014-06-09 14:18:14 +0100197 * Verify that there's no trailing space at the end of the central directory
198 * and its comment.
Narayan Kamath7462f022013-11-21 13:05:04 +0000199 */
Narayan Kamath926973e2014-06-09 14:18:14 +0100200 const off64_t calculated_length = eocd_offset + sizeof(EocdRecord)
201 + eocd->comment_length;
202 if (calculated_length != file_length) {
Narayan Kamath4f6b4992014-06-03 13:59:23 +0100203 ALOGW("Zip: %" PRId64 " extraneous bytes at the end of the central directory",
Narayan Kamath926973e2014-06-09 14:18:14 +0100204 static_cast<int64_t>(file_length - calculated_length));
Narayan Kamath4f6b4992014-06-03 13:59:23 +0100205 return kInvalidFile;
206 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000207
Narayan Kamath926973e2014-06-09 14:18:14 +0100208 /*
209 * Grab the CD offset and size, and the number of entries in the
210 * archive and verify that they look reasonable.
211 */
Tianjie Xu1ee48922016-09-21 14:58:11 -0700212 if (static_cast<off64_t>(eocd->cd_start_offset) + eocd->cd_size > eocd_offset) {
Narayan Kamath926973e2014-06-09 14:18:14 +0100213 ALOGW("Zip: bad offsets (dir %" PRIu32 ", size %" PRIu32 ", eocd %" PRId64 ")",
214 eocd->cd_start_offset, eocd->cd_size, static_cast<int64_t>(eocd_offset));
Tianjie Xu1ee48922016-09-21 14:58:11 -0700215#if defined(__ANDROID__)
216 if (eocd->cd_start_offset + eocd->cd_size <= eocd_offset) {
217 android_errorWriteLog(0x534e4554, "31251826");
218 }
219#endif
Narayan Kamath7462f022013-11-21 13:05:04 +0000220 return kInvalidOffset;
221 }
Narayan Kamath926973e2014-06-09 14:18:14 +0100222 if (eocd->num_records == 0) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000223 ALOGW("Zip: empty archive?");
224 return kEmptyArchive;
225 }
226
Elliott Hughese49236b2015-06-04 15:21:59 -0700227 ALOGV("+++ num_entries=%" PRIu32 " dir_size=%" PRIu32 " dir_offset=%" PRIu32,
Narayan Kamath926973e2014-06-09 14:18:14 +0100228 eocd->num_records, eocd->cd_size, eocd->cd_start_offset);
Narayan Kamath7462f022013-11-21 13:05:04 +0000229
230 /*
231 * It all looks good. Create a mapping for the CD, and set the fields
232 * in archive.
233 */
Tianjie Xu18c25922016-09-29 15:27:41 -0700234
235 if (!archive->InitializeCentralDirectory(debug_file_name,
236 static_cast<off64_t>(eocd->cd_start_offset),
237 static_cast<size_t>(eocd->cd_size))) {
238 ALOGE("Zip: failed to intialize central directory.\n");
Narayan Kamatheaf98852013-12-11 14:51:51 +0000239 return kMmapFailed;
Narayan Kamath7462f022013-11-21 13:05:04 +0000240 }
241
Narayan Kamath926973e2014-06-09 14:18:14 +0100242 archive->num_entries = eocd->num_records;
243 archive->directory_offset = eocd->cd_start_offset;
Narayan Kamath7462f022013-11-21 13:05:04 +0000244
245 return 0;
246}
247
248/*
249 * Find the zip Central Directory and memory-map it.
250 *
251 * On success, returns 0 after populating fields from the EOCD area:
252 * directory_offset
Tianjie Xu18c25922016-09-29 15:27:41 -0700253 * directory_ptr
Narayan Kamath7462f022013-11-21 13:05:04 +0000254 * num_entries
255 */
Tianjie Xu18c25922016-09-29 15:27:41 -0700256static int32_t MapCentralDirectory(const char* debug_file_name, ZipArchive* archive) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000257
258 // Test file length. We use lseek64 to make sure the file
259 // is small enough to be a zip file (Its size must be less than
260 // 0xffffffff bytes).
Tianjie Xu18c25922016-09-29 15:27:41 -0700261 off64_t file_length = archive->mapped_zip.GetFileLength();
Narayan Kamath7462f022013-11-21 13:05:04 +0000262 if (file_length == -1) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000263 return kInvalidFile;
264 }
265
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800266 if (file_length > static_cast<off64_t>(0xffffffff)) {
Narayan Kamath926973e2014-06-09 14:18:14 +0100267 ALOGV("Zip: zip file too long %" PRId64, static_cast<int64_t>(file_length));
Narayan Kamath7462f022013-11-21 13:05:04 +0000268 return kInvalidFile;
269 }
270
Narayan Kamath926973e2014-06-09 14:18:14 +0100271 if (file_length < static_cast<off64_t>(sizeof(EocdRecord))) {
272 ALOGV("Zip: length %" PRId64 " is too small to be zip", static_cast<int64_t>(file_length));
Narayan Kamath7462f022013-11-21 13:05:04 +0000273 return kInvalidFile;
274 }
275
276 /*
277 * Perform the traditional EOCD snipe hunt.
278 *
279 * We're searching for the End of Central Directory magic number,
280 * which appears at the start of the EOCD block. It's followed by
281 * 18 bytes of EOCD stuff and up to 64KB of archive comment. We
282 * need to read the last part of the file into a buffer, dig through
283 * it to find the magic number, parse some values out, and use those
284 * to determine the extent of the CD.
285 *
286 * We start by pulling in the last part of the file.
287 */
Narayan Kamath926973e2014-06-09 14:18:14 +0100288 off64_t read_amount = kMaxEOCDSearch;
289 if (file_length < read_amount) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000290 read_amount = file_length;
291 }
292
Tianjie Xu18c25922016-09-29 15:27:41 -0700293 std::vector<uint8_t> scan_buffer(read_amount);
294 int32_t result = MapCentralDirectory0(debug_file_name, archive, file_length, read_amount,
295 scan_buffer.data());
Narayan Kamath7462f022013-11-21 13:05:04 +0000296 return result;
297}
298
299/*
300 * Parses the Zip archive's Central Directory. Allocates and populates the
301 * hash table.
302 *
303 * Returns 0 on success.
304 */
305static int32_t ParseZipArchive(ZipArchive* archive) {
Tianjie Xu18c25922016-09-29 15:27:41 -0700306 const uint8_t* const cd_ptr = archive->central_directory.GetBasePtr();
307 const size_t cd_length = archive->central_directory.GetMapLength();
Narayan Kamath926973e2014-06-09 14:18:14 +0100308 const uint16_t num_entries = archive->num_entries;
Narayan Kamath7462f022013-11-21 13:05:04 +0000309
310 /*
311 * Create hash table. We have a minimum 75% load factor, possibly as
312 * low as 50% after we round off to a power of 2. There must be at
313 * least one unused entry to avoid an infinite loop during creation.
314 */
315 archive->hash_table_size = RoundUpPower2(1 + (num_entries * 4) / 3);
Yusuke Sato07447542015-06-25 14:39:19 -0700316 archive->hash_table = reinterpret_cast<ZipString*>(calloc(archive->hash_table_size,
317 sizeof(ZipString)));
Narayan Kamath7462f022013-11-21 13:05:04 +0000318
319 /*
320 * Walk through the central directory, adding entries to the hash
321 * table and verifying values.
322 */
Narayan Kamath926973e2014-06-09 14:18:14 +0100323 const uint8_t* const cd_end = cd_ptr + cd_length;
Narayan Kamath7462f022013-11-21 13:05:04 +0000324 const uint8_t* ptr = cd_ptr;
325 for (uint16_t i = 0; i < num_entries; i++) {
Narayan Kamath926973e2014-06-09 14:18:14 +0100326 const CentralDirectoryRecord* cdr =
327 reinterpret_cast<const CentralDirectoryRecord*>(ptr);
328 if (cdr->record_signature != CentralDirectoryRecord::kSignature) {
Mark Salyzyn088bf902014-05-08 16:02:20 -0700329 ALOGW("Zip: missed a central dir sig (at %" PRIu16 ")", i);
Dmitriy Ivanov3ea93da2015-03-06 11:48:47 -0800330 return -1;
Narayan Kamath7462f022013-11-21 13:05:04 +0000331 }
332
Narayan Kamath926973e2014-06-09 14:18:14 +0100333 if (ptr + sizeof(CentralDirectoryRecord) > cd_end) {
Mark Salyzyn088bf902014-05-08 16:02:20 -0700334 ALOGW("Zip: ran off the end (at %" PRIu16 ")", i);
Dmitriy Ivanov3ea93da2015-03-06 11:48:47 -0800335 return -1;
Narayan Kamath7462f022013-11-21 13:05:04 +0000336 }
337
Narayan Kamath926973e2014-06-09 14:18:14 +0100338 const off64_t local_header_offset = cdr->local_file_header_offset;
Narayan Kamath7462f022013-11-21 13:05:04 +0000339 if (local_header_offset >= archive->directory_offset) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800340 ALOGW("Zip: bad LFH offset %" PRId64 " at entry %" PRIu16,
341 static_cast<int64_t>(local_header_offset), i);
Dmitriy Ivanov3ea93da2015-03-06 11:48:47 -0800342 return -1;
Narayan Kamath7462f022013-11-21 13:05:04 +0000343 }
344
Narayan Kamath926973e2014-06-09 14:18:14 +0100345 const uint16_t file_name_length = cdr->file_name_length;
346 const uint16_t extra_length = cdr->extra_field_length;
347 const uint16_t comment_length = cdr->comment_length;
Piotr Jastrzebski78271ba2014-08-15 12:53:00 +0100348 const uint8_t* file_name = ptr + sizeof(CentralDirectoryRecord);
349
Narayan Kamath044bc8e2014-12-03 18:22:53 +0000350 /* check that file name is valid UTF-8 and doesn't contain NUL (U+0000) characters */
351 if (!IsValidEntryName(file_name, file_name_length)) {
Dmitriy Ivanov3ea93da2015-03-06 11:48:47 -0800352 return -1;
Piotr Jastrzebski78271ba2014-08-15 12:53:00 +0100353 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000354
355 /* add the CDE filename to the hash table */
Yusuke Sato07447542015-06-25 14:39:19 -0700356 ZipString entry_name;
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100357 entry_name.name = file_name;
358 entry_name.name_length = file_name_length;
Narayan Kamath7462f022013-11-21 13:05:04 +0000359 const int add_result = AddToHash(archive->hash_table,
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100360 archive->hash_table_size, entry_name);
Dmitriy Ivanov3ea93da2015-03-06 11:48:47 -0800361 if (add_result != 0) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000362 ALOGW("Zip: Error adding entry to hash table %d", add_result);
Dmitriy Ivanov3ea93da2015-03-06 11:48:47 -0800363 return add_result;
Narayan Kamath7462f022013-11-21 13:05:04 +0000364 }
365
Narayan Kamath926973e2014-06-09 14:18:14 +0100366 ptr += sizeof(CentralDirectoryRecord) + file_name_length + extra_length + comment_length;
367 if ((ptr - cd_ptr) > static_cast<int64_t>(cd_length)) {
Mark Salyzyn088bf902014-05-08 16:02:20 -0700368 ALOGW("Zip: bad CD advance (%tu vs %zu) at entry %" PRIu16,
369 ptr - cd_ptr, cd_length, i);
Dmitriy Ivanov3ea93da2015-03-06 11:48:47 -0800370 return -1;
Narayan Kamath7462f022013-11-21 13:05:04 +0000371 }
372 }
Mark Salyzyn088bf902014-05-08 16:02:20 -0700373 ALOGV("+++ zip good scan %" PRIu16 " entries", num_entries);
Narayan Kamath7462f022013-11-21 13:05:04 +0000374
Dmitriy Ivanov3ea93da2015-03-06 11:48:47 -0800375 return 0;
Narayan Kamath7462f022013-11-21 13:05:04 +0000376}
377
378static int32_t OpenArchiveInternal(ZipArchive* archive,
379 const char* debug_file_name) {
380 int32_t result = -1;
Tianjie Xu18c25922016-09-29 15:27:41 -0700381 if ((result = MapCentralDirectory(debug_file_name, archive)) != 0) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000382 return result;
383 }
384
385 if ((result = ParseZipArchive(archive))) {
386 return result;
387 }
388
389 return 0;
390}
391
392int32_t OpenArchiveFd(int fd, const char* debug_file_name,
Dmitriy Ivanov40b52b22014-07-15 19:33:00 -0700393 ZipArchiveHandle* handle, bool assume_ownership) {
394 ZipArchive* archive = new ZipArchive(fd, assume_ownership);
Narayan Kamath7462f022013-11-21 13:05:04 +0000395 *handle = archive;
Narayan Kamath7462f022013-11-21 13:05:04 +0000396 return OpenArchiveInternal(archive, debug_file_name);
397}
398
399int32_t OpenArchive(const char* fileName, ZipArchiveHandle* handle) {
Neil Fullerb1a113f2014-07-25 14:43:04 +0100400 const int fd = open(fileName, O_RDONLY | O_BINARY, 0);
Dmitriy Ivanov40b52b22014-07-15 19:33:00 -0700401 ZipArchive* archive = new ZipArchive(fd, true);
Narayan Kamath7462f022013-11-21 13:05:04 +0000402 *handle = archive;
403
Narayan Kamath7462f022013-11-21 13:05:04 +0000404 if (fd < 0) {
405 ALOGW("Unable to open '%s': %s", fileName, strerror(errno));
406 return kIoError;
Narayan Kamath7462f022013-11-21 13:05:04 +0000407 }
Dmitriy Ivanov40b52b22014-07-15 19:33:00 -0700408
Narayan Kamath7462f022013-11-21 13:05:04 +0000409 return OpenArchiveInternal(archive, fileName);
410}
411
Tianjie Xu18c25922016-09-29 15:27:41 -0700412int32_t OpenArchiveFromMemory(void* address, size_t length, const char* debug_file_name,
413 ZipArchiveHandle *handle) {
414 ZipArchive* archive = new ZipArchive(address, length);
415 *handle = archive;
416 return OpenArchiveInternal(archive, debug_file_name);
417}
418
Narayan Kamath7462f022013-11-21 13:05:04 +0000419/*
420 * Close a ZipArchive, closing the file and freeing the contents.
421 */
422void CloseArchive(ZipArchiveHandle handle) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800423 ZipArchive* archive = reinterpret_cast<ZipArchive*>(handle);
Narayan Kamath7462f022013-11-21 13:05:04 +0000424 ALOGV("Closing archive %p", archive);
Neil Fullerb1a113f2014-07-25 14:43:04 +0100425 delete archive;
Narayan Kamath7462f022013-11-21 13:05:04 +0000426}
427
Narayan Kamath162b7052017-06-05 13:21:12 +0100428static int32_t ValidateDataDescriptor(MappedZipFile& mapped_zip, ZipEntry* entry) {
Narayan Kamath926973e2014-06-09 14:18:14 +0100429 uint8_t ddBuf[sizeof(DataDescriptor) + sizeof(DataDescriptor::kOptSignature)];
Tianjie Xu18c25922016-09-29 15:27:41 -0700430 if (!mapped_zip.ReadData(ddBuf, sizeof(ddBuf))) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000431 return kIoError;
432 }
433
Narayan Kamath926973e2014-06-09 14:18:14 +0100434 const uint32_t ddSignature = *(reinterpret_cast<const uint32_t*>(ddBuf));
435 const uint16_t offset = (ddSignature == DataDescriptor::kOptSignature) ? 4 : 0;
436 const DataDescriptor* descriptor = reinterpret_cast<const DataDescriptor*>(ddBuf + offset);
Narayan Kamath7462f022013-11-21 13:05:04 +0000437
Narayan Kamath162b7052017-06-05 13:21:12 +0100438 // Validate that the values in the data descriptor match those in the central
439 // directory.
440 if (entry->compressed_length != descriptor->compressed_size ||
441 entry->uncompressed_length != descriptor->uncompressed_size ||
442 entry->crc32 != descriptor->crc32) {
443 ALOGW("Zip: size/crc32 mismatch. expected {%" PRIu32 ", %" PRIu32 ", %" PRIx32
444 "}, was {%" PRIu32 ", %" PRIu32 ", %" PRIx32 "}",
445 entry->compressed_length, entry->uncompressed_length, entry->crc32,
446 descriptor->compressed_size, descriptor->uncompressed_size, descriptor->crc32);
447 return kInconsistentInformation;
448 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000449
450 return 0;
451}
452
Narayan Kamath7462f022013-11-21 13:05:04 +0000453static int32_t FindEntry(const ZipArchive* archive, const int ent,
454 ZipEntry* data) {
455 const uint16_t nameLen = archive->hash_table[ent].name_length;
Narayan Kamath7462f022013-11-21 13:05:04 +0000456
457 // Recover the start of the central directory entry from the filename
458 // pointer. The filename is the first entry past the fixed-size data,
459 // so we can just subtract back from that.
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100460 const uint8_t* ptr = archive->hash_table[ent].name;
Narayan Kamath926973e2014-06-09 14:18:14 +0100461 ptr -= sizeof(CentralDirectoryRecord);
Narayan Kamath7462f022013-11-21 13:05:04 +0000462
463 // This is the base of our mmapped region, we have to sanity check that
464 // the name that's in the hash table is a pointer to a location within
465 // this mapped region.
Tianjie Xu18c25922016-09-29 15:27:41 -0700466 const uint8_t* base_ptr = archive->central_directory.GetBasePtr();
467 if (ptr < base_ptr || ptr > base_ptr + archive->central_directory.GetMapLength()) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000468 ALOGW("Zip: Invalid entry pointer");
469 return kInvalidOffset;
470 }
471
Narayan Kamath926973e2014-06-09 14:18:14 +0100472 const CentralDirectoryRecord *cdr =
473 reinterpret_cast<const CentralDirectoryRecord*>(ptr);
474
Narayan Kamath7462f022013-11-21 13:05:04 +0000475 // The offset of the start of the central directory in the zipfile.
476 // We keep this lying around so that we can sanity check all our lengths
477 // and our per-file structures.
478 const off64_t cd_offset = archive->directory_offset;
479
480 // Fill out the compression method, modification time, crc32
481 // and other interesting attributes from the central directory. These
482 // will later be compared against values from the local file header.
Narayan Kamath926973e2014-06-09 14:18:14 +0100483 data->method = cdr->compression_method;
beonit0e99a2f2015-07-18 02:08:16 +0900484 data->mod_time = cdr->last_mod_date << 16 | cdr->last_mod_time;
Narayan Kamath926973e2014-06-09 14:18:14 +0100485 data->crc32 = cdr->crc32;
486 data->compressed_length = cdr->compressed_size;
487 data->uncompressed_length = cdr->uncompressed_size;
Narayan Kamath7462f022013-11-21 13:05:04 +0000488
489 // Figure out the local header offset from the central directory. The
490 // actual file data will begin after the local header and the name /
491 // extra comments.
Narayan Kamath926973e2014-06-09 14:18:14 +0100492 const off64_t local_header_offset = cdr->local_file_header_offset;
493 if (local_header_offset + static_cast<off64_t>(sizeof(LocalFileHeader)) >= cd_offset) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000494 ALOGW("Zip: bad local hdr offset in zip");
495 return kInvalidOffset;
496 }
497
Narayan Kamath926973e2014-06-09 14:18:14 +0100498 uint8_t lfh_buf[sizeof(LocalFileHeader)];
Tianjie Xu18c25922016-09-29 15:27:41 -0700499 if (!archive->mapped_zip.ReadAtOffset(lfh_buf, sizeof(lfh_buf), local_header_offset)) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800500 ALOGW("Zip: failed reading lfh name from offset %" PRId64,
501 static_cast<int64_t>(local_header_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000502 return kIoError;
503 }
504
Narayan Kamath926973e2014-06-09 14:18:14 +0100505 const LocalFileHeader *lfh = reinterpret_cast<const LocalFileHeader*>(lfh_buf);
506
507 if (lfh->lfh_signature != LocalFileHeader::kSignature) {
Mark Salyzyn99ef9912014-03-14 14:26:22 -0700508 ALOGW("Zip: didn't find signature at start of lfh, offset=%" PRId64,
Narayan Kamath926973e2014-06-09 14:18:14 +0100509 static_cast<int64_t>(local_header_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000510 return kInvalidOffset;
511 }
512
513 // Paranoia: Match the values specified in the local file header
514 // to those specified in the central directory.
Adam Lesinskid987c9d2017-04-06 18:55:47 -0700515
Narayan Kamath162b7052017-06-05 13:21:12 +0100516 // Warn if central directory and local file header don't agree on the use
517 // of a trailing Data Descriptor. The reference implementation is inconsistent
518 // and appears to use the LFH value during extraction (unzip) but the CD value
519 // while displayng information about archives (zipinfo). The spec remains
520 // silent on this inconsistency as well.
521 //
522 // For now, always use the version from the LFH but make sure that the values
523 // specified in the central directory match those in the data descriptor.
524 //
525 // NOTE: It's also worth noting that unzip *does* warn about inconsistencies in
526 // bit 11 (EFS: The language encoding flag, marking that filename and comment are
527 // encoded using UTF-8). This implementation does not check for the presence of
528 // that flag and always enforces that entry names are valid UTF-8.
529 if ((lfh->gpb_flags & kGPBDDFlagMask) != (cdr->gpb_flags & kGPBDDFlagMask)) {
530 ALOGW("Zip: gpb flag mismatch at bit 3. expected {%04" PRIx16 "}, was {%04" PRIx16 "}",
Adam Lesinskid987c9d2017-04-06 18:55:47 -0700531 cdr->gpb_flags, lfh->gpb_flags);
Adam Lesinskid987c9d2017-04-06 18:55:47 -0700532 }
533
534 // If there is no trailing data descriptor, verify that the central directory and local file
535 // header agree on the crc, compressed, and uncompressed sizes of the entry.
Narayan Kamath926973e2014-06-09 14:18:14 +0100536 if ((lfh->gpb_flags & kGPBDDFlagMask) == 0) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000537 data->has_data_descriptor = 0;
Narayan Kamath926973e2014-06-09 14:18:14 +0100538 if (data->compressed_length != lfh->compressed_size
539 || data->uncompressed_length != lfh->uncompressed_size
540 || data->crc32 != lfh->crc32) {
Mark Salyzyn088bf902014-05-08 16:02:20 -0700541 ALOGW("Zip: size/crc32 mismatch. expected {%" PRIu32 ", %" PRIu32
542 ", %" PRIx32 "}, was {%" PRIu32 ", %" PRIu32 ", %" PRIx32 "}",
Narayan Kamath7462f022013-11-21 13:05:04 +0000543 data->compressed_length, data->uncompressed_length, data->crc32,
Narayan Kamath926973e2014-06-09 14:18:14 +0100544 lfh->compressed_size, lfh->uncompressed_size, lfh->crc32);
Narayan Kamath7462f022013-11-21 13:05:04 +0000545 return kInconsistentInformation;
546 }
547 } else {
548 data->has_data_descriptor = 1;
549 }
550
551 // Check that the local file header name matches the declared
552 // name in the central directory.
Narayan Kamath926973e2014-06-09 14:18:14 +0100553 if (lfh->file_name_length == nameLen) {
554 const off64_t name_offset = local_header_offset + sizeof(LocalFileHeader);
Mykola Kondratenko50afc152014-09-08 12:46:37 +0200555 if (name_offset + lfh->file_name_length > cd_offset) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000556 ALOGW("Zip: Invalid declared length");
557 return kInvalidOffset;
558 }
559
Tianjie Xu18c25922016-09-29 15:27:41 -0700560 std::vector<uint8_t> name_buf(nameLen);
561 if (!archive->mapped_zip.ReadAtOffset(name_buf.data(), nameLen, name_offset)) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800562 ALOGW("Zip: failed reading lfh name from offset %" PRId64, static_cast<int64_t>(name_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000563 return kIoError;
564 }
565
Tianjie Xu18c25922016-09-29 15:27:41 -0700566 if (memcmp(archive->hash_table[ent].name, name_buf.data(), nameLen)) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000567 return kInconsistentInformation;
568 }
569
Narayan Kamath7462f022013-11-21 13:05:04 +0000570 } else {
571 ALOGW("Zip: lfh name did not match central directory.");
572 return kInconsistentInformation;
573 }
574
Narayan Kamath926973e2014-06-09 14:18:14 +0100575 const off64_t data_offset = local_header_offset + sizeof(LocalFileHeader)
576 + lfh->file_name_length + lfh->extra_field_length;
Narayan Kamath48953a12014-01-24 12:32:39 +0000577 if (data_offset > cd_offset) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800578 ALOGW("Zip: bad data offset %" PRId64 " in zip", static_cast<int64_t>(data_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000579 return kInvalidOffset;
580 }
581
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800582 if (static_cast<off64_t>(data_offset + data->compressed_length) > cd_offset) {
Mark Salyzyn088bf902014-05-08 16:02:20 -0700583 ALOGW("Zip: bad compressed length in zip (%" PRId64 " + %" PRIu32 " > %" PRId64 ")",
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800584 static_cast<int64_t>(data_offset), data->compressed_length, static_cast<int64_t>(cd_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000585 return kInvalidOffset;
586 }
587
588 if (data->method == kCompressStored &&
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800589 static_cast<off64_t>(data_offset + data->uncompressed_length) > cd_offset) {
Mark Salyzyn088bf902014-05-08 16:02:20 -0700590 ALOGW("Zip: bad uncompressed length in zip (%" PRId64 " + %" PRIu32 " > %" PRId64 ")",
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800591 static_cast<int64_t>(data_offset), data->uncompressed_length,
592 static_cast<int64_t>(cd_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000593 return kInvalidOffset;
594 }
595
596 data->offset = data_offset;
597 return 0;
598}
599
600struct IterationHandle {
601 uint32_t position;
Piotr Jastrzebski10aa9a02014-08-19 09:01:20 +0100602 // We're not using vector here because this code is used in the Windows SDK
603 // where the STL is not available.
Yusuke Sato07447542015-06-25 14:39:19 -0700604 ZipString prefix;
605 ZipString suffix;
Narayan Kamath7462f022013-11-21 13:05:04 +0000606 ZipArchive* archive;
Piotr Jastrzebski8e085362014-08-18 11:37:45 +0100607
Yusuke Sato07447542015-06-25 14:39:19 -0700608 IterationHandle(const ZipString* in_prefix,
609 const ZipString* in_suffix) {
610 if (in_prefix) {
611 uint8_t* name_copy = new uint8_t[in_prefix->name_length];
612 memcpy(name_copy, in_prefix->name, in_prefix->name_length);
613 prefix.name = name_copy;
614 prefix.name_length = in_prefix->name_length;
615 } else {
616 prefix.name = NULL;
617 prefix.name_length = 0;
Yusuke Satof1d3d3b2015-06-25 14:09:00 -0700618 }
Yusuke Sato07447542015-06-25 14:39:19 -0700619 if (in_suffix) {
620 uint8_t* name_copy = new uint8_t[in_suffix->name_length];
621 memcpy(name_copy, in_suffix->name, in_suffix->name_length);
622 suffix.name = name_copy;
623 suffix.name_length = in_suffix->name_length;
624 } else {
625 suffix.name = NULL;
626 suffix.name_length = 0;
Yusuke Satof1d3d3b2015-06-25 14:09:00 -0700627 }
Piotr Jastrzebski8e085362014-08-18 11:37:45 +0100628 }
629
630 ~IterationHandle() {
Yusuke Sato07447542015-06-25 14:39:19 -0700631 delete[] prefix.name;
632 delete[] suffix.name;
Piotr Jastrzebski8e085362014-08-18 11:37:45 +0100633 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000634};
635
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100636int32_t StartIteration(ZipArchiveHandle handle, void** cookie_ptr,
Yusuke Sato07447542015-06-25 14:39:19 -0700637 const ZipString* optional_prefix,
638 const ZipString* optional_suffix) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800639 ZipArchive* archive = reinterpret_cast<ZipArchive*>(handle);
Narayan Kamath7462f022013-11-21 13:05:04 +0000640
641 if (archive == NULL || archive->hash_table == NULL) {
642 ALOGW("Zip: Invalid ZipArchiveHandle");
643 return kInvalidHandle;
644 }
645
Yusuke Satof1d3d3b2015-06-25 14:09:00 -0700646 IterationHandle* cookie = new IterationHandle(optional_prefix, optional_suffix);
Narayan Kamath7462f022013-11-21 13:05:04 +0000647 cookie->position = 0;
Narayan Kamath7462f022013-11-21 13:05:04 +0000648 cookie->archive = archive;
Narayan Kamath7462f022013-11-21 13:05:04 +0000649
650 *cookie_ptr = cookie ;
651 return 0;
652}
653
Piotr Jastrzebski79c8b342014-08-08 14:02:17 +0100654void EndIteration(void* cookie) {
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100655 delete reinterpret_cast<IterationHandle*>(cookie);
Piotr Jastrzebski79c8b342014-08-08 14:02:17 +0100656}
657
Yusuke Sato07447542015-06-25 14:39:19 -0700658int32_t FindEntry(const ZipArchiveHandle handle, const ZipString& entryName,
Narayan Kamath7462f022013-11-21 13:05:04 +0000659 ZipEntry* data) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800660 const ZipArchive* archive = reinterpret_cast<ZipArchive*>(handle);
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100661 if (entryName.name_length == 0) {
662 ALOGW("Zip: Invalid filename %.*s", entryName.name_length, entryName.name);
Narayan Kamath7462f022013-11-21 13:05:04 +0000663 return kInvalidEntryName;
664 }
665
666 const int64_t ent = EntryToIndex(archive->hash_table,
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100667 archive->hash_table_size, entryName);
Narayan Kamath7462f022013-11-21 13:05:04 +0000668
669 if (ent < 0) {
Piotr Jastrzebskiecccc5a2014-08-11 16:35:11 +0100670 ALOGV("Zip: Could not find entry %.*s", entryName.name_length, entryName.name);
Narayan Kamath7462f022013-11-21 13:05:04 +0000671 return ent;
672 }
673
674 return FindEntry(archive, ent, data);
675}
676
Yusuke Sato07447542015-06-25 14:39:19 -0700677int32_t Next(void* cookie, ZipEntry* data, ZipString* name) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800678 IterationHandle* handle = reinterpret_cast<IterationHandle*>(cookie);
Narayan Kamath7462f022013-11-21 13:05:04 +0000679 if (handle == NULL) {
680 return kInvalidHandle;
681 }
682
683 ZipArchive* archive = handle->archive;
684 if (archive == NULL || archive->hash_table == NULL) {
685 ALOGW("Zip: Invalid ZipArchiveHandle");
686 return kInvalidHandle;
687 }
688
689 const uint32_t currentOffset = handle->position;
690 const uint32_t hash_table_length = archive->hash_table_size;
Yusuke Sato07447542015-06-25 14:39:19 -0700691 const ZipString* hash_table = archive->hash_table;
Narayan Kamath7462f022013-11-21 13:05:04 +0000692
693 for (uint32_t i = currentOffset; i < hash_table_length; ++i) {
694 if (hash_table[i].name != NULL &&
Yusuke Sato07447542015-06-25 14:39:19 -0700695 (handle->prefix.name_length == 0 ||
696 hash_table[i].StartsWith(handle->prefix)) &&
697 (handle->suffix.name_length == 0 ||
698 hash_table[i].EndsWith(handle->suffix))) {
Narayan Kamath7462f022013-11-21 13:05:04 +0000699 handle->position = (i + 1);
700 const int error = FindEntry(archive, i, data);
701 if (!error) {
702 name->name = hash_table[i].name;
703 name->name_length = hash_table[i].name_length;
704 }
705
706 return error;
707 }
708 }
709
710 handle->position = 0;
711 return kIterationEnd;
712}
713
Narayan Kamathf899bd52015-04-17 11:53:14 +0100714class Writer {
715 public:
716 virtual bool Append(uint8_t* buf, size_t buf_size) = 0;
717 virtual ~Writer() {}
718 protected:
719 Writer() = default;
720 private:
721 DISALLOW_COPY_AND_ASSIGN(Writer);
722};
723
724// A Writer that writes data to a fixed size memory region.
725// The size of the memory region must be equal to the total size of
726// the data appended to it.
727class MemoryWriter : public Writer {
728 public:
729 MemoryWriter(uint8_t* buf, size_t size) : Writer(),
730 buf_(buf), size_(size), bytes_written_(0) {
731 }
732
733 virtual bool Append(uint8_t* buf, size_t buf_size) override {
734 if (bytes_written_ + buf_size > size_) {
735 ALOGW("Zip: Unexpected size " ZD " (declared) vs " ZD " (actual)",
736 size_, bytes_written_ + buf_size);
737 return false;
738 }
739
740 memcpy(buf_ + bytes_written_, buf, buf_size);
741 bytes_written_ += buf_size;
742 return true;
743 }
744
745 private:
746 uint8_t* const buf_;
747 const size_t size_;
748 size_t bytes_written_;
749};
750
751// A Writer that appends data to a file |fd| at its current position.
752// The file will be truncated to the end of the written data.
753class FileWriter : public Writer {
754 public:
755
756 // Creates a FileWriter for |fd| and prepare to write |entry| to it,
757 // guaranteeing that the file descriptor is valid and that there's enough
758 // space on the volume to write out the entry completely and that the file
Tao Baoa456c212016-11-15 10:08:07 -0800759 // is truncated to the correct length (no truncation if |fd| references a
760 // block device).
Narayan Kamathf899bd52015-04-17 11:53:14 +0100761 //
762 // Returns a valid FileWriter on success, |nullptr| if an error occurred.
763 static std::unique_ptr<FileWriter> Create(int fd, const ZipEntry* entry) {
764 const uint32_t declared_length = entry->uncompressed_length;
765 const off64_t current_offset = lseek64(fd, 0, SEEK_CUR);
766 if (current_offset == -1) {
767 ALOGW("Zip: unable to seek to current location on fd %d: %s", fd, strerror(errno));
768 return nullptr;
769 }
770
771 int result = 0;
772#if defined(__linux__)
773 if (declared_length > 0) {
774 // Make sure we have enough space on the volume to extract the compressed
775 // entry. Note that the call to ftruncate below will change the file size but
776 // will not allocate space on disk and this call to fallocate will not
777 // change the file size.
Badhri Jagan Sridharana68d0d12015-06-02 14:47:57 -0700778 // Note: fallocate is only supported by the following filesystems -
779 // btrfs, ext4, ocfs2, and xfs. Therefore fallocate might fail with
780 // EOPNOTSUPP error when issued in other filesystems.
781 // Hence, check for the return error code before concluding that the
782 // disk does not have enough space.
Narayan Kamathf899bd52015-04-17 11:53:14 +0100783 result = TEMP_FAILURE_RETRY(fallocate(fd, 0, current_offset, declared_length));
Badhri Jagan Sridharana68d0d12015-06-02 14:47:57 -0700784 if (result == -1 && errno == ENOSPC) {
Narayan Kamathd5d7abe2016-08-10 12:24:05 +0100785 ALOGW("Zip: unable to allocate %" PRId64 " bytes at offset %" PRId64 " : %s",
786 static_cast<int64_t>(declared_length), static_cast<int64_t>(current_offset),
787 strerror(errno));
Narayan Kamathf899bd52015-04-17 11:53:14 +0100788 return std::unique_ptr<FileWriter>(nullptr);
789 }
790 }
791#endif // __linux__
792
Tao Baoa456c212016-11-15 10:08:07 -0800793 struct stat sb;
794 if (fstat(fd, &sb) == -1) {
795 ALOGW("Zip: unable to fstat file: %s", strerror(errno));
Narayan Kamathf899bd52015-04-17 11:53:14 +0100796 return std::unique_ptr<FileWriter>(nullptr);
797 }
798
Tao Baoa456c212016-11-15 10:08:07 -0800799 // Block device doesn't support ftruncate(2).
800 if (!S_ISBLK(sb.st_mode)) {
801 result = TEMP_FAILURE_RETRY(ftruncate(fd, declared_length + current_offset));
802 if (result == -1) {
803 ALOGW("Zip: unable to truncate file to %" PRId64 ": %s",
804 static_cast<int64_t>(declared_length + current_offset), strerror(errno));
805 return std::unique_ptr<FileWriter>(nullptr);
806 }
807 }
808
Narayan Kamathf899bd52015-04-17 11:53:14 +0100809 return std::unique_ptr<FileWriter>(new FileWriter(fd, declared_length));
810 }
811
812 virtual bool Append(uint8_t* buf, size_t buf_size) override {
813 if (total_bytes_written_ + buf_size > declared_length_) {
814 ALOGW("Zip: Unexpected size " ZD " (declared) vs " ZD " (actual)",
815 declared_length_, total_bytes_written_ + buf_size);
816 return false;
817 }
818
Narayan Kamathe97e66e2015-04-27 16:25:53 +0100819 const bool result = android::base::WriteFully(fd_, buf, buf_size);
820 if (result) {
821 total_bytes_written_ += buf_size;
822 } else {
823 ALOGW("Zip: unable to write " ZD " bytes to file; %s", buf_size, strerror(errno));
Narayan Kamathf899bd52015-04-17 11:53:14 +0100824 }
825
Narayan Kamathe97e66e2015-04-27 16:25:53 +0100826 return result;
Narayan Kamathf899bd52015-04-17 11:53:14 +0100827 }
828 private:
829 FileWriter(const int fd, const size_t declared_length) :
830 Writer(),
831 fd_(fd),
832 declared_length_(declared_length),
833 total_bytes_written_(0) {
834 }
835
836 const int fd_;
837 const size_t declared_length_;
838 size_t total_bytes_written_;
839};
840
Dmitriy Ivanovf94e1592015-03-06 13:27:59 -0800841// This method is using libz macros with old-style-casts
842#pragma GCC diagnostic push
843#pragma GCC diagnostic ignored "-Wold-style-cast"
844static inline int zlib_inflateInit2(z_stream* stream, int window_bits) {
845 return inflateInit2(stream, window_bits);
846}
847#pragma GCC diagnostic pop
848
Tianjie Xu18c25922016-09-29 15:27:41 -0700849static int32_t InflateEntryToWriter(MappedZipFile& mapped_zip, const ZipEntry* entry,
Narayan Kamathf899bd52015-04-17 11:53:14 +0100850 Writer* writer, uint64_t* crc_out) {
Dmitriy Ivanovedbabfe2015-03-12 09:58:15 -0700851 const size_t kBufSize = 32768;
852 std::vector<uint8_t> read_buf(kBufSize);
853 std::vector<uint8_t> write_buf(kBufSize);
Narayan Kamath7462f022013-11-21 13:05:04 +0000854 z_stream zstream;
855 int zerr;
856
857 /*
858 * Initialize the zlib stream struct.
859 */
860 memset(&zstream, 0, sizeof(zstream));
861 zstream.zalloc = Z_NULL;
862 zstream.zfree = Z_NULL;
863 zstream.opaque = Z_NULL;
864 zstream.next_in = NULL;
865 zstream.avail_in = 0;
Dmitriy Ivanovedbabfe2015-03-12 09:58:15 -0700866 zstream.next_out = &write_buf[0];
Narayan Kamath7462f022013-11-21 13:05:04 +0000867 zstream.avail_out = kBufSize;
868 zstream.data_type = Z_UNKNOWN;
869
870 /*
871 * Use the undocumented "negative window bits" feature to tell zlib
872 * that there's no zlib header waiting for it.
873 */
Dmitriy Ivanovf94e1592015-03-06 13:27:59 -0800874 zerr = zlib_inflateInit2(&zstream, -MAX_WBITS);
Narayan Kamath7462f022013-11-21 13:05:04 +0000875 if (zerr != Z_OK) {
876 if (zerr == Z_VERSION_ERROR) {
877 ALOGE("Installed zlib is not compatible with linked version (%s)",
878 ZLIB_VERSION);
879 } else {
880 ALOGW("Call to inflateInit2 failed (zerr=%d)", zerr);
881 }
882
883 return kZlibError;
884 }
885
Dmitriy Ivanov1f741e52015-03-06 14:26:37 -0800886 auto zstream_deleter = [](z_stream* stream) {
887 inflateEnd(stream); /* free up any allocated structures */
888 };
889
890 std::unique_ptr<z_stream, decltype(zstream_deleter)> zstream_guard(&zstream, zstream_deleter);
891
Narayan Kamath7462f022013-11-21 13:05:04 +0000892 const uint32_t uncompressed_length = entry->uncompressed_length;
893
Narayan Kamath162b7052017-06-05 13:21:12 +0100894 uint64_t crc = 0;
Narayan Kamath7462f022013-11-21 13:05:04 +0000895 uint32_t compressed_length = entry->compressed_length;
Narayan Kamath7462f022013-11-21 13:05:04 +0000896 do {
897 /* read as much as we can */
898 if (zstream.avail_in == 0) {
Yabin Cuib2a77002016-02-08 16:26:33 -0800899 const size_t getSize = (compressed_length > kBufSize) ? kBufSize : compressed_length;
Tianjie Xu18c25922016-09-29 15:27:41 -0700900 if (!mapped_zip.ReadData(read_buf.data(), getSize)) {
Yabin Cuib2a77002016-02-08 16:26:33 -0800901 ALOGW("Zip: inflate read failed, getSize = %zu: %s", getSize, strerror(errno));
Dmitriy Ivanov1f741e52015-03-06 14:26:37 -0800902 return kIoError;
Narayan Kamath7462f022013-11-21 13:05:04 +0000903 }
904
905 compressed_length -= getSize;
906
Dmitriy Ivanovedbabfe2015-03-12 09:58:15 -0700907 zstream.next_in = &read_buf[0];
Narayan Kamath7462f022013-11-21 13:05:04 +0000908 zstream.avail_in = getSize;
909 }
910
911 /* uncompress the data */
912 zerr = inflate(&zstream, Z_NO_FLUSH);
913 if (zerr != Z_OK && zerr != Z_STREAM_END) {
914 ALOGW("Zip: inflate zerr=%d (nIn=%p aIn=%u nOut=%p aOut=%u)",
915 zerr, zstream.next_in, zstream.avail_in,
916 zstream.next_out, zstream.avail_out);
Dmitriy Ivanov1f741e52015-03-06 14:26:37 -0800917 return kZlibError;
Narayan Kamath7462f022013-11-21 13:05:04 +0000918 }
919
920 /* write when we're full or when we're done */
921 if (zstream.avail_out == 0 ||
922 (zerr == Z_STREAM_END && zstream.avail_out != kBufSize)) {
Dmitriy Ivanovedbabfe2015-03-12 09:58:15 -0700923 const size_t write_size = zstream.next_out - &write_buf[0];
Narayan Kamathf899bd52015-04-17 11:53:14 +0100924 if (!writer->Append(&write_buf[0], write_size)) {
925 // The file might have declared a bogus length.
926 return kInconsistentInformation;
Narayan Kamath162b7052017-06-05 13:21:12 +0100927 } else {
928 crc = crc32(crc, &write_buf[0], write_size);
Narayan Kamath7462f022013-11-21 13:05:04 +0000929 }
Narayan Kamath7462f022013-11-21 13:05:04 +0000930
Dmitriy Ivanovedbabfe2015-03-12 09:58:15 -0700931 zstream.next_out = &write_buf[0];
Narayan Kamath7462f022013-11-21 13:05:04 +0000932 zstream.avail_out = kBufSize;
933 }
934 } while (zerr == Z_OK);
935
936 assert(zerr == Z_STREAM_END); /* other errors should've been caught */
937
Narayan Kamath162b7052017-06-05 13:21:12 +0100938 // NOTE: zstream.adler is always set to 0, because we're using the -MAX_WBITS
939 // "feature" of zlib to tell it there won't be a zlib file header. zlib
940 // doesn't bother calculating the checksum in that scenario. We just do
941 // it ourselves above because there are no additional gains to be made by
942 // having zlib calculate it for us, since they do it by calling crc32 in
943 // the same manner that we have above.
944 *crc_out = crc;
Narayan Kamath7462f022013-11-21 13:05:04 +0000945
946 if (zstream.total_out != uncompressed_length || compressed_length != 0) {
Mark Salyzyn088bf902014-05-08 16:02:20 -0700947 ALOGW("Zip: size mismatch on inflated file (%lu vs %" PRIu32 ")",
Narayan Kamath7462f022013-11-21 13:05:04 +0000948 zstream.total_out, uncompressed_length);
Dmitriy Ivanov1f741e52015-03-06 14:26:37 -0800949 return kInconsistentInformation;
Narayan Kamath7462f022013-11-21 13:05:04 +0000950 }
951
Dmitriy Ivanov1f741e52015-03-06 14:26:37 -0800952 return 0;
Narayan Kamath7462f022013-11-21 13:05:04 +0000953}
954
Tianjie Xu18c25922016-09-29 15:27:41 -0700955static int32_t CopyEntryToWriter(MappedZipFile& mapped_zip, const ZipEntry* entry, Writer* writer,
Narayan Kamathf899bd52015-04-17 11:53:14 +0100956 uint64_t *crc_out) {
957 static const uint32_t kBufSize = 32768;
958 std::vector<uint8_t> buf(kBufSize);
959
960 const uint32_t length = entry->uncompressed_length;
961 uint32_t count = 0;
962 uint64_t crc = 0;
963 while (count < length) {
964 uint32_t remaining = length - count;
965
966 // Safe conversion because kBufSize is narrow enough for a 32 bit signed
967 // value.
Yabin Cuib2a77002016-02-08 16:26:33 -0800968 const size_t block_size = (remaining > kBufSize) ? kBufSize : remaining;
Tianjie Xu18c25922016-09-29 15:27:41 -0700969 if (!mapped_zip.ReadData(buf.data(), block_size)) {
Yabin Cuib2a77002016-02-08 16:26:33 -0800970 ALOGW("CopyFileToFile: copy read failed, block_size = %zu: %s", block_size, strerror(errno));
Narayan Kamathf899bd52015-04-17 11:53:14 +0100971 return kIoError;
972 }
973
974 if (!writer->Append(&buf[0], block_size)) {
975 return kIoError;
976 }
977 crc = crc32(crc, &buf[0], block_size);
978 count += block_size;
979 }
980
981 *crc_out = crc;
982
983 return 0;
984}
985
986int32_t ExtractToWriter(ZipArchiveHandle handle,
987 ZipEntry* entry, Writer* writer) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800988 ZipArchive* archive = reinterpret_cast<ZipArchive*>(handle);
Narayan Kamath7462f022013-11-21 13:05:04 +0000989 const uint16_t method = entry->method;
990 off64_t data_offset = entry->offset;
991
Tianjie Xu18c25922016-09-29 15:27:41 -0700992 if (!archive->mapped_zip.SeekToOffset(data_offset)) {
Dmitriy Ivanovf4cb8e22015-03-06 10:50:56 -0800993 ALOGW("Zip: lseek to data at %" PRId64 " failed", static_cast<int64_t>(data_offset));
Narayan Kamath7462f022013-11-21 13:05:04 +0000994 return kIoError;
995 }
996
997 // this should default to kUnknownCompressionMethod.
998 int32_t return_value = -1;
999 uint64_t crc = 0;
1000 if (method == kCompressStored) {
Tianjie Xu18c25922016-09-29 15:27:41 -07001001 return_value = CopyEntryToWriter(archive->mapped_zip, entry, writer, &crc);
Narayan Kamath7462f022013-11-21 13:05:04 +00001002 } else if (method == kCompressDeflated) {
Tianjie Xu18c25922016-09-29 15:27:41 -07001003 return_value = InflateEntryToWriter(archive->mapped_zip, entry, writer, &crc);
Narayan Kamath7462f022013-11-21 13:05:04 +00001004 }
1005
1006 if (!return_value && entry->has_data_descriptor) {
Narayan Kamath162b7052017-06-05 13:21:12 +01001007 return_value = ValidateDataDescriptor(archive->mapped_zip, entry);
Narayan Kamath7462f022013-11-21 13:05:04 +00001008 if (return_value) {
1009 return return_value;
1010 }
1011 }
1012
Narayan Kamath162b7052017-06-05 13:21:12 +01001013 // Validate that the CRC matches the calculated value.
1014 if (kCrcChecksEnabled && (entry->crc32 != static_cast<uint32_t>(crc))) {
Mark Salyzyn088bf902014-05-08 16:02:20 -07001015 ALOGW("Zip: crc mismatch: expected %" PRIu32 ", was %" PRIu64, entry->crc32, crc);
Narayan Kamath7462f022013-11-21 13:05:04 +00001016 return kInconsistentInformation;
1017 }
1018
1019 return return_value;
1020}
1021
Narayan Kamathf899bd52015-04-17 11:53:14 +01001022int32_t ExtractToMemory(ZipArchiveHandle handle, ZipEntry* entry,
1023 uint8_t* begin, uint32_t size) {
1024 std::unique_ptr<Writer> writer(new MemoryWriter(begin, size));
1025 return ExtractToWriter(handle, entry, writer.get());
1026}
1027
Narayan Kamath7462f022013-11-21 13:05:04 +00001028int32_t ExtractEntryToFile(ZipArchiveHandle handle,
1029 ZipEntry* entry, int fd) {
Narayan Kamathf899bd52015-04-17 11:53:14 +01001030 std::unique_ptr<Writer> writer(FileWriter::Create(fd, entry));
1031 if (writer.get() == nullptr) {
Narayan Kamath7462f022013-11-21 13:05:04 +00001032 return kIoError;
1033 }
1034
Narayan Kamathf899bd52015-04-17 11:53:14 +01001035 return ExtractToWriter(handle, entry, writer.get());
Narayan Kamath7462f022013-11-21 13:05:04 +00001036}
1037
1038const char* ErrorCodeString(int32_t error_code) {
Narayan Kamath1ef9d2d2017-06-15 13:58:25 +01001039 // Make sure that the number of entries in kErrorMessages and ErrorCodes
1040 // match.
1041 static_assert((-kLastErrorCode + 1) == arraysize(kErrorMessages),
1042 "(-kLastErrorCode + 1) != arraysize(kErrorMessages)");
1043
1044 const uint32_t idx = -error_code;
1045 if (idx < arraysize(kErrorMessages)) {
1046 return kErrorMessages[idx];
Narayan Kamath7462f022013-11-21 13:05:04 +00001047 }
1048
Narayan Kamath1ef9d2d2017-06-15 13:58:25 +01001049 return "Unknown return code";
Narayan Kamath7462f022013-11-21 13:05:04 +00001050}
1051
1052int GetFileDescriptor(const ZipArchiveHandle handle) {
Tianjie Xu18c25922016-09-29 15:27:41 -07001053 return reinterpret_cast<ZipArchive*>(handle)->mapped_zip.GetFileDescriptor();
Narayan Kamath7462f022013-11-21 13:05:04 +00001054}
Colin Cross7c6c7f02016-09-16 10:15:51 -07001055
1056ZipString::ZipString(const char* entry_name)
1057 : name(reinterpret_cast<const uint8_t*>(entry_name)) {
1058 size_t len = strlen(entry_name);
1059 CHECK_LE(len, static_cast<size_t>(UINT16_MAX));
1060 name_length = static_cast<uint16_t>(len);
1061}
Tianjie Xu18c25922016-09-29 15:27:41 -07001062
1063#if !defined(_WIN32)
1064class ProcessWriter : public Writer {
1065 public:
1066 ProcessWriter(ProcessZipEntryFunction func, void* cookie) : Writer(),
1067 proc_function_(func),
1068 cookie_(cookie) {
1069 }
1070
1071 virtual bool Append(uint8_t* buf, size_t buf_size) override {
1072 return proc_function_(buf, buf_size, cookie_);
1073 }
1074
1075 private:
1076 ProcessZipEntryFunction proc_function_;
1077 void* cookie_;
1078};
1079
1080int32_t ProcessZipEntryContents(ZipArchiveHandle handle, ZipEntry* entry,
1081 ProcessZipEntryFunction func, void* cookie) {
1082 ProcessWriter writer(func, cookie);
1083 return ExtractToWriter(handle, entry, &writer);
1084}
1085
1086#endif //!defined(_WIN32)
1087
1088int MappedZipFile::GetFileDescriptor() const {
1089 if (!has_fd_) {
1090 ALOGW("Zip: MappedZipFile doesn't have a file descriptor.");
1091 return -1;
1092 }
1093 return fd_;
1094}
1095
1096void* MappedZipFile::GetBasePtr() const {
1097 if (has_fd_) {
1098 ALOGW("Zip: MappedZipFile doesn't have a base pointer.");
1099 return nullptr;
1100 }
1101 return base_ptr_;
1102}
1103
1104off64_t MappedZipFile::GetFileLength() const {
1105 if (has_fd_) {
1106 off64_t result = lseek64(fd_, 0, SEEK_END);
1107 if (result == -1) {
1108 ALOGE("Zip: lseek on fd %d failed: %s", fd_, strerror(errno));
1109 }
1110 return result;
1111 } else {
1112 if (base_ptr_ == nullptr) {
1113 ALOGE("Zip: invalid file map\n");
1114 return -1;
1115 }
1116 return static_cast<off64_t>(data_length_);
1117 }
1118}
1119
1120bool MappedZipFile::SeekToOffset(off64_t offset) {
1121 if (has_fd_) {
1122 if (lseek64(fd_, offset, SEEK_SET) != offset) {
1123 ALOGE("Zip: lseek to %" PRId64 " failed: %s\n", offset, strerror(errno));
1124 return false;
1125 }
1126 return true;
1127 } else {
1128 if (offset < 0 || offset > static_cast<off64_t>(data_length_)) {
1129 ALOGE("Zip: invalid offset: %" PRId64 ", data length: %" PRId64 "\n" , offset,
1130 data_length_);
1131 return false;
1132 }
1133
1134 read_pos_ = offset;
1135 return true;
1136 }
1137}
1138
1139bool MappedZipFile::ReadData(uint8_t* buffer, size_t read_amount) {
1140 if (has_fd_) {
1141 if(!android::base::ReadFully(fd_, buffer, read_amount)) {
1142 ALOGE("Zip: read from %d failed\n", fd_);
1143 return false;
1144 }
1145 } else {
1146 memcpy(buffer, static_cast<uint8_t*>(base_ptr_) + read_pos_, read_amount);
1147 read_pos_ += read_amount;
1148 }
1149 return true;
1150}
1151
1152// Attempts to read |len| bytes into |buf| at offset |off|.
1153bool MappedZipFile::ReadAtOffset(uint8_t* buf, size_t len, off64_t off) {
1154#if !defined(_WIN32)
1155 if (has_fd_) {
1156 if (static_cast<size_t>(TEMP_FAILURE_RETRY(pread64(fd_, buf, len, off))) != len) {
1157 ALOGE("Zip: failed to read at offset %" PRId64 "\n", off);
1158 return false;
1159 }
1160 return true;
1161 }
1162#endif
1163 if (!SeekToOffset(off)) {
1164 return false;
1165 }
1166 return ReadData(buf, len);
1167
1168}
1169
1170void CentralDirectory::Initialize(void* map_base_ptr, off64_t cd_start_offset, size_t cd_size) {
1171 base_ptr_ = static_cast<uint8_t*>(map_base_ptr) + cd_start_offset;
1172 length_ = cd_size;
1173}
1174
1175bool ZipArchive::InitializeCentralDirectory(const char* debug_file_name, off64_t cd_start_offset,
1176 size_t cd_size) {
1177 if (mapped_zip.HasFd()) {
1178 if (!directory_map->create(debug_file_name, mapped_zip.GetFileDescriptor(),
1179 cd_start_offset, cd_size, true /* read only */)) {
1180 return false;
1181 }
1182
1183 CHECK_EQ(directory_map->getDataLength(), cd_size);
1184 central_directory.Initialize(directory_map->getDataPtr(), 0/*offset*/, cd_size);
1185 } else {
1186 if (mapped_zip.GetBasePtr() == nullptr) {
1187 ALOGE("Zip: Failed to map central directory, bad mapped_zip base pointer\n");
1188 return false;
1189 }
1190 if (static_cast<off64_t>(cd_start_offset) + static_cast<off64_t>(cd_size) >
1191 mapped_zip.GetFileLength()) {
1192 ALOGE("Zip: Failed to map central directory, offset exceeds mapped memory region ("
1193 "start_offset %" PRId64 ", cd_size %zu, mapped_region_size %" PRId64 ")",
1194 static_cast<int64_t>(cd_start_offset), cd_size, mapped_zip.GetFileLength());
1195 return false;
1196 }
1197
1198 central_directory.Initialize(mapped_zip.GetBasePtr(), cd_start_offset, cd_size);
1199 }
1200 return true;
1201}