blob: 70778e7f7656bb4766d8e1ba86fd26b4be869762 [file] [log] [blame]
Brian Carlstromb0460ea2011-07-29 10:08:05 -07001/*
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
David Sehr1979c642018-04-26 14:41:18 -070017#include "zip_archive.h"
Brian Carlstromb0460ea2011-07-29 10:08:05 -070018
19#include <fcntl.h>
Ian Rogers8d31bbd2013-10-13 10:44:14 -070020#include <stdio.h>
Brian Carlstromb0460ea2011-07-29 10:08:05 -070021#include <sys/stat.h>
22#include <sys/types.h>
23#include <unistd.h>
Ian Rogers700a4022014-05-19 16:49:03 -070024#include <vector>
Brian Carlstromb0460ea2011-07-29 10:08:05 -070025
Igor Murashkin271a0f82017-02-14 21:14:17 +000026#include "android-base/stringprintf.h"
Andreas Gampe0c2d3e52017-07-03 12:50:44 -070027#include "ziparchive/zip_archive.h"
28
David Sehr10db8fe2018-07-18 11:01:20 -070029#include "base/mman.h"
David Sehr1979c642018-04-26 14:41:18 -070030#include "bit_utils.h"
31#include "unix_file/fd_file.h"
Elliott Hughes90a33692011-08-30 13:27:07 -070032
Brian Carlstromb0460ea2011-07-29 10:08:05 -070033namespace art {
34
Igor Murashkin271a0f82017-02-14 21:14:17 +000035// Log file contents and mmap info when mapping entries directly.
36static constexpr const bool kDebugZipMapDirectly = false;
37
38using android::base::StringPrintf;
39
Brian Carlstromb0460ea2011-07-29 10:08:05 -070040uint32_t ZipEntry::GetUncompressedLength() {
Narayan Kamath92572be2013-11-28 14:06:24 +000041 return zip_entry_->uncompressed_length;
Brian Carlstromb0460ea2011-07-29 10:08:05 -070042}
43
44uint32_t ZipEntry::GetCrc32() {
Narayan Kamath92572be2013-11-28 14:06:24 +000045 return zip_entry_->crc32;
Brian Carlstromb0460ea2011-07-29 10:08:05 -070046}
47
Igor Murashkin271a0f82017-02-14 21:14:17 +000048bool ZipEntry::IsUncompressed() {
49 return zip_entry_->method == kCompressStored;
50}
51
Nicolas Geoffrayf3075272018-01-08 12:41:19 +000052bool ZipEntry::IsAlignedTo(size_t alignment) const {
Igor Murashkin271a0f82017-02-14 21:14:17 +000053 DCHECK(IsPowerOfTwo(alignment)) << alignment;
54 return IsAlignedParam(zip_entry_->offset, static_cast<int>(alignment));
55}
56
Mathieu Chartier661974a2014-01-09 11:23:53 -080057ZipEntry::~ZipEntry() {
58 delete zip_entry_;
59}
Brian Carlstromb0460ea2011-07-29 10:08:05 -070060
Ian Rogers8d31bbd2013-10-13 10:44:14 -070061bool ZipEntry::ExtractToFile(File& file, std::string* error_msg) {
Narayan Kamath92572be2013-11-28 14:06:24 +000062 const int32_t error = ExtractEntryToFile(handle_, zip_entry_, file.Fd());
Vladimir Marko9e4b42a2020-04-28 12:28:21 +010063 if (error != 0) {
Narayan Kamath92572be2013-11-28 14:06:24 +000064 *error_msg = std::string(ErrorCodeString(error));
Brian Carlstrom89521892011-12-07 22:05:07 -080065 return false;
66 }
67
Narayan Kamath92572be2013-11-28 14:06:24 +000068 return true;
Brian Carlstromb0460ea2011-07-29 10:08:05 -070069}
70
Vladimir Markoc34bebf2018-08-16 16:12:49 +010071MemMap ZipEntry::ExtractToMemMap(const char* zip_filename,
72 const char* entry_filename,
73 std::string* error_msg) {
Brian Carlstrom4922e9d2013-07-09 17:18:47 -070074 std::string name(entry_filename);
75 name += " extracted in memory from ";
Brian Carlstrom0aa504b2014-05-23 02:47:28 -070076 name += zip_filename;
Vladimir Markoc34bebf2018-08-16 16:12:49 +010077 MemMap map = MemMap::MapAnonymous(name.c_str(),
Vladimir Markoc34bebf2018-08-16 16:12:49 +010078 GetUncompressedLength(),
79 PROT_READ | PROT_WRITE,
Vladimir Marko11306592018-10-26 14:22:59 +010080 /*low_4gb=*/ false,
Vladimir Markoc34bebf2018-08-16 16:12:49 +010081 error_msg);
82 if (!map.IsValid()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070083 DCHECK(!error_msg->empty());
Vladimir Markoc34bebf2018-08-16 16:12:49 +010084 return MemMap::Invalid();
Brian Carlstrom4922e9d2013-07-09 17:18:47 -070085 }
86
Vladimir Marko9e4b42a2020-04-28 12:28:21 +010087 DCHECK_EQ(map.Size(), GetUncompressedLength());
88 if (!ExtractToMemory(map.Begin(), error_msg)) {
Vladimir Markoc34bebf2018-08-16 16:12:49 +010089 return MemMap::Invalid();
Brian Carlstrom4922e9d2013-07-09 17:18:47 -070090 }
91
Vladimir Markoc34bebf2018-08-16 16:12:49 +010092 return map;
Brian Carlstrom4922e9d2013-07-09 17:18:47 -070093}
94
Vladimir Marko9e4b42a2020-04-28 12:28:21 +010095bool ZipEntry::ExtractToMemory(/*out*/uint8_t* buffer, /*out*/std::string* error_msg) {
96 const int32_t error = ::ExtractToMemory(handle_, zip_entry_, buffer, GetUncompressedLength());
97 if (error != 0) {
98 *error_msg = std::string(ErrorCodeString(error));
99 return false;
100 }
101 return true;
102}
103
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100104MemMap ZipEntry::MapDirectlyFromFile(const char* zip_filename, std::string* error_msg) {
Igor Murashkin271a0f82017-02-14 21:14:17 +0000105 const int zip_fd = GetFileDescriptor(handle_);
106 const char* entry_filename = entry_name_.c_str();
107
108 // Should not happen since we don't have a memory ZipArchive constructor.
109 // However the underlying ZipArchive isn't required to have an FD,
110 // so check to be sure.
111 CHECK_GE(zip_fd, 0) <<
112 StringPrintf("Cannot map '%s' (in zip '%s') directly because the zip archive "
113 "is not file backed.",
114 entry_filename,
115 zip_filename);
116
117 if (!IsUncompressed()) {
118 *error_msg = StringPrintf("Cannot map '%s' (in zip '%s') directly because it is compressed.",
119 entry_filename,
120 zip_filename);
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100121 return MemMap::Invalid();
Igor Murashkin271a0f82017-02-14 21:14:17 +0000122 } else if (zip_entry_->uncompressed_length != zip_entry_->compressed_length) {
123 *error_msg = StringPrintf("Cannot map '%s' (in zip '%s') directly because "
124 "entry has bad size (%u != %u).",
125 entry_filename,
126 zip_filename,
127 zip_entry_->uncompressed_length,
128 zip_entry_->compressed_length);
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100129 return MemMap::Invalid();
Igor Murashkin271a0f82017-02-14 21:14:17 +0000130 }
131
132 std::string name(entry_filename);
133 name += " mapped directly in memory from ";
134 name += zip_filename;
135
136 const off_t offset = zip_entry_->offset;
137
138 if (kDebugZipMapDirectly) {
139 LOG(INFO) << "zip_archive: " << "make mmap of " << name << " @ offset = " << offset;
140 }
141
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100142 MemMap map =
Vladimir Markoc09cd052018-08-23 16:36:36 +0100143 MemMap::MapFile(GetUncompressedLength(), // Byte count
144 PROT_READ | PROT_WRITE,
145 MAP_PRIVATE,
146 zip_fd,
147 offset,
Vladimir Marko11306592018-10-26 14:22:59 +0100148 /*low_4gb=*/ false,
Vladimir Markoc09cd052018-08-23 16:36:36 +0100149 name.c_str(),
150 error_msg);
Igor Murashkin271a0f82017-02-14 21:14:17 +0000151
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100152 if (!map.IsValid()) {
Igor Murashkin271a0f82017-02-14 21:14:17 +0000153 DCHECK(!error_msg->empty());
154 }
155
156 if (kDebugZipMapDirectly) {
157 // Dump contents of file, same format as using this shell command:
158 // $> od -j <offset> -t x1 <zip_filename>
159 static constexpr const int kMaxDumpChars = 15;
160 lseek(zip_fd, 0, SEEK_SET);
161
162 int count = offset + kMaxDumpChars;
163
164 std::string tmp;
165 char buf;
166
167 // Dump file contents.
168 int i = 0;
169 while (read(zip_fd, &buf, 1) > 0 && i < count) {
170 tmp += StringPrintf("%3d ", (unsigned int)buf);
171 ++i;
172 }
173
174 LOG(INFO) << "map_fd raw bytes starting at 0";
175 LOG(INFO) << "" << tmp;
176 LOG(INFO) << "---------------------------";
177
178 // Dump map contents.
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100179 if (map.IsValid()) {
Igor Murashkin271a0f82017-02-14 21:14:17 +0000180 tmp = "";
181
182 count = kMaxDumpChars;
183
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100184 uint8_t* begin = map.Begin();
Igor Murashkin271a0f82017-02-14 21:14:17 +0000185 for (i = 0; i < count; ++i) {
186 tmp += StringPrintf("%3d ", (unsigned int)begin[i]);
187 }
188
189 LOG(INFO) << "map address " << StringPrintf("%p", begin);
190 LOG(INFO) << "map first " << kMaxDumpChars << " chars:";
191 LOG(INFO) << tmp;
192 }
193 }
194
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100195 return map;
Igor Murashkin271a0f82017-02-14 21:14:17 +0000196}
197
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100198MemMap ZipEntry::MapDirectlyOrExtract(const char* zip_filename,
199 const char* entry_filename,
Colin Cross2b41cca2018-11-16 22:43:41 -0800200 std::string* error_msg,
201 size_t alignment) {
202 if (IsUncompressed() && IsAlignedTo(alignment) && GetFileDescriptor(handle_) >= 0) {
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100203 std::string local_error_msg;
204 MemMap ret = MapDirectlyFromFile(zip_filename, &local_error_msg);
205 if (ret.IsValid()) {
Mathieu Chartier792111c2018-02-15 13:02:15 -0800206 return ret;
207 }
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100208 // Fall back to extraction for the failure case.
Mathieu Chartier792111c2018-02-15 13:02:15 -0800209 }
Mathieu Chartier792111c2018-02-15 13:02:15 -0800210 return ExtractToMemMap(zip_filename, entry_filename, error_msg);
211}
212
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800213static void SetCloseOnExec(int fd) {
David Sehr10db8fe2018-07-18 11:01:20 -0700214#ifdef _WIN32
215 // Exec is not supported on Windows.
216 UNUSED(fd);
217 PLOG(ERROR) << "SetCloseOnExec is not supported on Windows.";
218#else
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800219 // This dance is more portable than Linux's O_CLOEXEC open(2) flag.
220 int flags = fcntl(fd, F_GETFD);
221 if (flags == -1) {
222 PLOG(WARNING) << "fcntl(" << fd << ", F_GETFD) failed";
223 return;
224 }
225 int rc = fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
226 if (rc == -1) {
227 PLOG(WARNING) << "fcntl(" << fd << ", F_SETFD, " << flags << ") failed";
228 return;
229 }
David Sehr10db8fe2018-07-18 11:01:20 -0700230#endif
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800231}
232
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700233ZipArchive* ZipArchive::Open(const char* filename, std::string* error_msg) {
234 DCHECK(filename != nullptr);
Narayan Kamath92572be2013-11-28 14:06:24 +0000235
236 ZipArchiveHandle handle;
237 const int32_t error = OpenArchive(filename, &handle);
Vladimir Marko9e4b42a2020-04-28 12:28:21 +0100238 if (error != 0) {
Narayan Kamath92572be2013-11-28 14:06:24 +0000239 *error_msg = std::string(ErrorCodeString(error));
240 CloseArchive(handle);
241 return nullptr;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700242 }
Narayan Kamath92572be2013-11-28 14:06:24 +0000243
244 SetCloseOnExec(GetFileDescriptor(handle));
245 return new ZipArchive(handle);
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700246}
247
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700248ZipArchive* ZipArchive::OpenFromFd(int fd, const char* filename, std::string* error_msg) {
Victor Hsiehecaf7d12021-06-14 11:09:21 -0700249 return OpenFromFdInternal(fd, /*assume_ownership=*/true, filename, error_msg);
250}
251
252ZipArchive* ZipArchive::OpenFromOwnedFd(int fd, const char* filename, std::string* error_msg) {
253 return OpenFromFdInternal(fd, /*assume_ownership=*/false, filename, error_msg);
254}
255
256ZipArchive* ZipArchive::OpenFromFdInternal(int fd,
257 bool assume_ownership,
258 const char* filename,
259 std::string* error_msg) {
Narayan Kamath92572be2013-11-28 14:06:24 +0000260 DCHECK(filename != nullptr);
261 DCHECK_GT(fd, 0);
262
263 ZipArchiveHandle handle;
Victor Hsiehecaf7d12021-06-14 11:09:21 -0700264 const int32_t error = OpenArchiveFd(fd, filename, &handle, assume_ownership);
Vladimir Marko9e4b42a2020-04-28 12:28:21 +0100265 if (error != 0) {
Narayan Kamath92572be2013-11-28 14:06:24 +0000266 *error_msg = std::string(ErrorCodeString(error));
267 CloseArchive(handle);
268 return nullptr;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700269 }
Narayan Kamath92572be2013-11-28 14:06:24 +0000270
271 SetCloseOnExec(GetFileDescriptor(handle));
272 return new ZipArchive(handle);
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700273}
274
Narayan Kamath92572be2013-11-28 14:06:24 +0000275ZipEntry* ZipArchive::Find(const char* name, std::string* error_msg) const {
276 DCHECK(name != nullptr);
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700277
Narayan Kamath92572be2013-11-28 14:06:24 +0000278 // Resist the urge to delete the space. <: is a bigraph sequence.
Ian Rogers700a4022014-05-19 16:49:03 -0700279 std::unique_ptr< ::ZipEntry> zip_entry(new ::ZipEntry);
Elliott Hughes1389dd42019-05-03 22:43:12 -0700280 const int32_t error = FindEntry(handle_, name, zip_entry.get());
Vladimir Marko9e4b42a2020-04-28 12:28:21 +0100281 if (error != 0) {
Narayan Kamath92572be2013-11-28 14:06:24 +0000282 *error_msg = std::string(ErrorCodeString(error));
283 return nullptr;
Kenny Root72fcca22013-09-19 09:25:34 -0700284 }
285
Igor Murashkin271a0f82017-02-14 21:14:17 +0000286 return new ZipEntry(handle_, zip_entry.release(), name);
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700287}
288
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000289ZipArchive::~ZipArchive() {
290 CloseArchive(handle_);
291}
292
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700293} // namespace art