blob: f3d4d7721408cdb0aa4395212ca14bc5ab8ace82 [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
17#include "zip_archive.h"
18
19#include <fcntl.h>
Ian Rogers8d31bbd2013-10-13 10:44:14 -070020#include <stdio.h>
Andreas Gampe0dfc3152017-04-24 07:58:06 -070021#include <sys/mman.h> // For the PROT_* and MAP_* constants.
Brian Carlstromb0460ea2011-07-29 10:08:05 -070022#include <sys/stat.h>
23#include <sys/types.h>
24#include <unistd.h>
Ian Rogers700a4022014-05-19 16:49:03 -070025#include <vector>
Brian Carlstromb0460ea2011-07-29 10:08:05 -070026
Igor Murashkin271a0f82017-02-14 21:14:17 +000027#include "android-base/stringprintf.h"
Andreas Gampe0c2d3e52017-07-03 12:50:44 -070028#include "ziparchive/zip_archive.h"
29
Andreas Gampe3b7dc352017-06-06 20:02:03 -070030#include "base/bit_utils.h"
Elliott Hughes76160052012-12-12 16:31:20 -080031#include "base/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
52bool ZipEntry::IsAlignedTo(size_t alignment) {
53 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());
63 if (error) {
64 *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
Brian Carlstrom0aa504b2014-05-23 02:47:28 -070071MemMap* ZipEntry::ExtractToMemMap(const char* zip_filename, const char* entry_filename,
72 std::string* error_msg) {
Brian Carlstrom4922e9d2013-07-09 17:18:47 -070073 std::string name(entry_filename);
74 name += " extracted in memory from ";
Brian Carlstrom0aa504b2014-05-23 02:47:28 -070075 name += zip_filename;
Ian Rogers700a4022014-05-19 16:49:03 -070076 std::unique_ptr<MemMap> map(MemMap::MapAnonymous(name.c_str(),
Mathieu Chartier2cebb242015-04-21 16:50:40 -070077 nullptr, GetUncompressedLength(),
Vladimir Marko5c42c292015-02-25 12:02:49 +000078 PROT_READ | PROT_WRITE, false, false,
79 error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -070080 if (map.get() == nullptr) {
81 DCHECK(!error_msg->empty());
Narayan Kamath92572be2013-11-28 14:06:24 +000082 return nullptr;
Brian Carlstrom4922e9d2013-07-09 17:18:47 -070083 }
84
Narayan Kamath92572be2013-11-28 14:06:24 +000085 const int32_t error = ExtractToMemory(handle_, zip_entry_,
86 map->Begin(), map->Size());
87 if (error) {
88 *error_msg = std::string(ErrorCodeString(error));
89 return nullptr;
Brian Carlstrom4922e9d2013-07-09 17:18:47 -070090 }
91
92 return map.release();
93}
94
Igor Murashkin271a0f82017-02-14 21:14:17 +000095MemMap* ZipEntry::MapDirectlyFromFile(const char* zip_filename, std::string* error_msg) {
96 const int zip_fd = GetFileDescriptor(handle_);
97 const char* entry_filename = entry_name_.c_str();
98
99 // Should not happen since we don't have a memory ZipArchive constructor.
100 // However the underlying ZipArchive isn't required to have an FD,
101 // so check to be sure.
102 CHECK_GE(zip_fd, 0) <<
103 StringPrintf("Cannot map '%s' (in zip '%s') directly because the zip archive "
104 "is not file backed.",
105 entry_filename,
106 zip_filename);
107
108 if (!IsUncompressed()) {
109 *error_msg = StringPrintf("Cannot map '%s' (in zip '%s') directly because it is compressed.",
110 entry_filename,
111 zip_filename);
112 return nullptr;
113 } else if (zip_entry_->uncompressed_length != zip_entry_->compressed_length) {
114 *error_msg = StringPrintf("Cannot map '%s' (in zip '%s') directly because "
115 "entry has bad size (%u != %u).",
116 entry_filename,
117 zip_filename,
118 zip_entry_->uncompressed_length,
119 zip_entry_->compressed_length);
120 return nullptr;
121 }
122
123 std::string name(entry_filename);
124 name += " mapped directly in memory from ";
125 name += zip_filename;
126
127 const off_t offset = zip_entry_->offset;
128
129 if (kDebugZipMapDirectly) {
130 LOG(INFO) << "zip_archive: " << "make mmap of " << name << " @ offset = " << offset;
131 }
132
133 std::unique_ptr<MemMap> map(
134 MemMap::MapFileAtAddress(nullptr, // Expected pointer address
135 GetUncompressedLength(), // Byte count
136 PROT_READ | PROT_WRITE,
137 MAP_PRIVATE,
138 zip_fd,
139 offset,
140 false, // Don't restrict allocation to lower4GB
141 false, // Doesn't overlap existing map (reuse=false)
142 name.c_str(),
143 /*out*/error_msg));
144
145 if (map == nullptr) {
146 DCHECK(!error_msg->empty());
147 }
148
149 if (kDebugZipMapDirectly) {
150 // Dump contents of file, same format as using this shell command:
151 // $> od -j <offset> -t x1 <zip_filename>
152 static constexpr const int kMaxDumpChars = 15;
153 lseek(zip_fd, 0, SEEK_SET);
154
155 int count = offset + kMaxDumpChars;
156
157 std::string tmp;
158 char buf;
159
160 // Dump file contents.
161 int i = 0;
162 while (read(zip_fd, &buf, 1) > 0 && i < count) {
163 tmp += StringPrintf("%3d ", (unsigned int)buf);
164 ++i;
165 }
166
167 LOG(INFO) << "map_fd raw bytes starting at 0";
168 LOG(INFO) << "" << tmp;
169 LOG(INFO) << "---------------------------";
170
171 // Dump map contents.
172 if (map != nullptr) {
173 tmp = "";
174
175 count = kMaxDumpChars;
176
177 uint8_t* begin = map->Begin();
178 for (i = 0; i < count; ++i) {
179 tmp += StringPrintf("%3d ", (unsigned int)begin[i]);
180 }
181
182 LOG(INFO) << "map address " << StringPrintf("%p", begin);
183 LOG(INFO) << "map first " << kMaxDumpChars << " chars:";
184 LOG(INFO) << tmp;
185 }
186 }
187
188 return map.release();
189}
190
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800191static void SetCloseOnExec(int fd) {
192 // This dance is more portable than Linux's O_CLOEXEC open(2) flag.
193 int flags = fcntl(fd, F_GETFD);
194 if (flags == -1) {
195 PLOG(WARNING) << "fcntl(" << fd << ", F_GETFD) failed";
196 return;
197 }
198 int rc = fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
199 if (rc == -1) {
200 PLOG(WARNING) << "fcntl(" << fd << ", F_SETFD, " << flags << ") failed";
201 return;
202 }
203}
204
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700205ZipArchive* ZipArchive::Open(const char* filename, std::string* error_msg) {
206 DCHECK(filename != nullptr);
Narayan Kamath92572be2013-11-28 14:06:24 +0000207
208 ZipArchiveHandle handle;
209 const int32_t error = OpenArchive(filename, &handle);
210 if (error) {
211 *error_msg = std::string(ErrorCodeString(error));
212 CloseArchive(handle);
213 return nullptr;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700214 }
Narayan Kamath92572be2013-11-28 14:06:24 +0000215
216 SetCloseOnExec(GetFileDescriptor(handle));
217 return new ZipArchive(handle);
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700218}
219
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700220ZipArchive* ZipArchive::OpenFromFd(int fd, const char* filename, std::string* error_msg) {
Narayan Kamath92572be2013-11-28 14:06:24 +0000221 DCHECK(filename != nullptr);
222 DCHECK_GT(fd, 0);
223
224 ZipArchiveHandle handle;
225 const int32_t error = OpenArchiveFd(fd, filename, &handle);
226 if (error) {
227 *error_msg = std::string(ErrorCodeString(error));
228 CloseArchive(handle);
229 return nullptr;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700230 }
Narayan Kamath92572be2013-11-28 14:06:24 +0000231
232 SetCloseOnExec(GetFileDescriptor(handle));
233 return new ZipArchive(handle);
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700234}
235
Narayan Kamath92572be2013-11-28 14:06:24 +0000236ZipEntry* ZipArchive::Find(const char* name, std::string* error_msg) const {
237 DCHECK(name != nullptr);
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700238
Narayan Kamath92572be2013-11-28 14:06:24 +0000239 // Resist the urge to delete the space. <: is a bigraph sequence.
Ian Rogers700a4022014-05-19 16:49:03 -0700240 std::unique_ptr< ::ZipEntry> zip_entry(new ::ZipEntry);
Yusuke Sato64db62d2015-06-25 14:56:49 -0700241 const int32_t error = FindEntry(handle_, ZipString(name), zip_entry.get());
Narayan Kamath92572be2013-11-28 14:06:24 +0000242 if (error) {
243 *error_msg = std::string(ErrorCodeString(error));
244 return nullptr;
Kenny Root72fcca22013-09-19 09:25:34 -0700245 }
246
Igor Murashkin271a0f82017-02-14 21:14:17 +0000247 return new ZipEntry(handle_, zip_entry.release(), name);
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700248}
249
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000250ZipArchive::~ZipArchive() {
251 CloseArchive(handle_);
252}
253
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700254} // namespace art