blob: d7b5914130ee07e966adc306e164fcca3b283689 [file] [log] [blame]
Mathias Agopian1f5762e2013-05-06 20:20:34 -07001/*
2 * Copyright (C) 2007 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//
20#define LOG_TAG "zipro"
21//#define LOG_NDEBUG 0
22#include <androidfw/ZipFileRO.h>
23#include <utils/Log.h>
24#include <utils/Compat.h>
25#include <utils/misc.h>
26#include <utils/threads.h>
Narayan Kamathafd31e02013-12-03 13:16:03 +000027#include <ziparchive/zip_archive.h>
Mathias Agopian1f5762e2013-05-06 20:20:34 -070028
29#include <zlib.h>
30
31#include <string.h>
32#include <fcntl.h>
33#include <errno.h>
34#include <assert.h>
35#include <unistd.h>
36
Mathias Agopian1f5762e2013-05-06 20:20:34 -070037using namespace android;
38
Narayan Kamathafd31e02013-12-03 13:16:03 +000039class _ZipEntryRO {
40public:
41 ZipEntry entry;
Elliott Hughes78de4f92019-06-14 15:28:38 -070042 std::string_view name;
Alex Buynytskyyb92c16e2023-07-05 21:48:59 -070043 void *cookie = nullptr;
Mathias Agopian1f5762e2013-05-06 20:20:34 -070044
Alex Buynytskyyb92c16e2023-07-05 21:48:59 -070045 _ZipEntryRO() = default;
Mathias Agopian1f5762e2013-05-06 20:20:34 -070046
Piotr Jastrzebski1a68b072014-08-08 12:52:39 +010047 ~_ZipEntryRO() {
Alex Buynytskyyb92c16e2023-07-05 21:48:59 -070048 EndIteration(cookie);
49 }
50
51 android::ZipEntryRO convertToPtr() {
52 _ZipEntryRO* result = new _ZipEntryRO;
53 result->entry = std::move(this->entry);
54 result->name = std::move(this->name);
55 result->cookie = std::exchange(this->cookie, nullptr);
56 return result;
Piotr Jastrzebski1a68b072014-08-08 12:52:39 +010057 }
58
Narayan Kamathafd31e02013-12-03 13:16:03 +000059private:
Alex Buynytskyyb92c16e2023-07-05 21:48:59 -070060 DISALLOW_COPY_AND_ASSIGN(_ZipEntryRO);
Narayan Kamathafd31e02013-12-03 13:16:03 +000061};
Mathias Agopian1f5762e2013-05-06 20:20:34 -070062
63ZipFileRO::~ZipFileRO() {
Narayan Kamathafd31e02013-12-03 13:16:03 +000064 CloseArchive(mHandle);
Dianne Hackbornca3872c2017-10-30 14:19:32 -070065 if (mFileName != NULL) {
66 free(mFileName);
67 }
Mathias Agopian1f5762e2013-05-06 20:20:34 -070068}
69
70/*
Mathias Agopian1f5762e2013-05-06 20:20:34 -070071 * Open the specified file read-only. We memory-map the entire thing and
72 * close the file before returning.
73 */
Narayan Kamathafd31e02013-12-03 13:16:03 +000074/* static */ ZipFileRO* ZipFileRO::open(const char* zipFileName)
Mathias Agopian1f5762e2013-05-06 20:20:34 -070075{
Narayan Kamathafd31e02013-12-03 13:16:03 +000076 ZipArchiveHandle handle;
77 const int32_t error = OpenArchive(zipFileName, &handle);
78 if (error) {
79 ALOGW("Error opening archive %s: %s", zipFileName, ErrorCodeString(error));
Narayan Kamath5a7587f2015-05-11 15:45:36 +010080 CloseArchive(handle);
Mathias Agopian1f5762e2013-05-06 20:20:34 -070081 return NULL;
82 }
83
Narayan Kamathafd31e02013-12-03 13:16:03 +000084 return new ZipFileRO(handle, strdup(zipFileName));
Mathias Agopian1f5762e2013-05-06 20:20:34 -070085}
86
Narayan Kamathafd31e02013-12-03 13:16:03 +000087
Dianne Hackbornca3872c2017-10-30 14:19:32 -070088/* static */ ZipFileRO* ZipFileRO::openFd(int fd, const char* debugFileName,
89 bool assume_ownership)
90{
91 ZipArchiveHandle handle;
92 const int32_t error = OpenArchiveFd(fd, debugFileName, &handle, assume_ownership);
93 if (error) {
94 ALOGW("Error opening archive fd %d %s: %s", fd, debugFileName, ErrorCodeString(error));
95 CloseArchive(handle);
96 return NULL;
97 }
98
99 return new ZipFileRO(handle, strdup(debugFileName));
100}
101
Narayan Kamathafd31e02013-12-03 13:16:03 +0000102ZipEntryRO ZipFileRO::findEntryByName(const char* entryName) const
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700103{
Alex Buynytskyyb92c16e2023-07-05 21:48:59 -0700104 _ZipEntryRO data;
105 data.name = entryName;
Piotr Jastrzebskie2134a42014-08-13 07:50:20 +0100106
Alex Buynytskyyb92c16e2023-07-05 21:48:59 -0700107 const int32_t error = FindEntry(mHandle, entryName, &(data.entry));
Narayan Kamathafd31e02013-12-03 13:16:03 +0000108 if (error) {
Alex Buynytskyyb92c16e2023-07-05 21:48:59 -0700109 return nullptr;
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700110 }
111
Alex Buynytskyyb92c16e2023-07-05 21:48:59 -0700112 return data.convertToPtr();
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700113}
114
115/*
116 * Get the useful fields from the zip entry.
117 *
118 * Returns "false" if the offsets to the fields or the contents of the fields
119 * appear to be bogus.
120 */
Narayan Kamath4600dd02015-06-16 12:02:57 +0100121bool ZipFileRO::getEntryInfo(ZipEntryRO entry, uint16_t* pMethod,
122 uint32_t* pUncompLen, uint32_t* pCompLen, off64_t* pOffset,
123 uint32_t* pModWhen, uint32_t* pCrc32) const
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700124{
Narayan Kamathafd31e02013-12-03 13:16:03 +0000125 const _ZipEntryRO* zipEntry = reinterpret_cast<_ZipEntryRO*>(entry);
126 const ZipEntry& ze = zipEntry->entry;
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700127
Narayan Kamathafd31e02013-12-03 13:16:03 +0000128 if (pMethod != NULL) {
129 *pMethod = ze.method;
Kenny Root0d6c2d72013-08-21 10:40:16 -0700130 }
Narayan Kamathafd31e02013-12-03 13:16:03 +0000131 if (pUncompLen != NULL) {
132 *pUncompLen = ze.uncompressed_length;
133 }
134 if (pCompLen != NULL) {
135 *pCompLen = ze.compressed_length;
136 }
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700137 if (pOffset != NULL) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000138 *pOffset = ze.offset;
139 }
140 if (pModWhen != NULL) {
141 *pModWhen = ze.mod_time;
142 }
143 if (pCrc32 != NULL) {
144 *pCrc32 = ze.crc32;
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700145 }
146
147 return true;
148}
149
Yusuke Sato34fe3df2015-06-19 17:18:07 -0700150bool ZipFileRO::startIteration(void** cookie) {
Alex Buynytskyyb92c16e2023-07-05 21:48:59 -0700151 return startIteration(cookie, nullptr, nullptr);
Yusuke Sato34fe3df2015-06-19 17:18:07 -0700152}
153
Alex Buynytskyyb92c16e2023-07-05 21:48:59 -0700154bool ZipFileRO::startIteration(void** cookie, const char* prefix, const char* suffix) {
155 auto result = startIterationOrError(prefix, suffix);
156 if (!result.ok()) {
157 return false;
158 }
159 *cookie = result.value();
160 return true;
161}
162
163base::expected<void*, int32_t>
164ZipFileRO::startIterationOrError(const char* prefix, const char* suffix) {
165 _ZipEntryRO ze;
166 int32_t error = StartIteration(mHandle, &(ze.cookie),
Elliott Hughes7a6cc0c2019-05-08 12:12:39 -0700167 prefix ? prefix : "", suffix ? suffix : "");
Narayan Kamathafd31e02013-12-03 13:16:03 +0000168 if (error) {
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700169 ALOGW("Could not start iteration over %s: %s", mFileName != NULL ? mFileName : "<null>",
170 ErrorCodeString(error));
Alex Buynytskyyb92c16e2023-07-05 21:48:59 -0700171 return base::unexpected(error);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000172 }
173
Alex Buynytskyyb92c16e2023-07-05 21:48:59 -0700174 return ze.convertToPtr();
Narayan Kamathafd31e02013-12-03 13:16:03 +0000175}
176
Alex Buynytskyyb92c16e2023-07-05 21:48:59 -0700177ZipEntryRO ZipFileRO::nextEntry(void* cookie) {
178 auto result = nextEntryOrError(cookie);
179 if (!result.ok()) {
180 return nullptr;
181 }
182 return result.value();
183}
184
185base::expected<ZipEntryRO, int32_t> ZipFileRO::nextEntryOrError(void* cookie) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000186 _ZipEntryRO* ze = reinterpret_cast<_ZipEntryRO*>(cookie);
187 int32_t error = Next(ze->cookie, &(ze->entry), &(ze->name));
188 if (error) {
189 if (error != -1) {
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700190 ALOGW("Error iteration over %s: %s", mFileName != NULL ? mFileName : "<null>",
191 ErrorCodeString(error));
Alex Buynytskyyb92c16e2023-07-05 21:48:59 -0700192 return base::unexpected(error);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000193 }
Alex Buynytskyyb92c16e2023-07-05 21:48:59 -0700194 return nullptr;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000195 }
196
197 return &(ze->entry);
198}
199
200void ZipFileRO::endIteration(void* cookie)
201{
202 delete reinterpret_cast<_ZipEntryRO*>(cookie);
203}
204
205void ZipFileRO::releaseEntry(ZipEntryRO entry) const
206{
207 delete reinterpret_cast<_ZipEntryRO*>(entry);
208}
209
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700210/*
211 * Copy the entry's filename to the buffer.
212 */
Narayan Kamath4600dd02015-06-16 12:02:57 +0100213int ZipFileRO::getEntryFileName(ZipEntryRO entry, char* buffer, size_t bufLen)
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700214 const
215{
Narayan Kamathafd31e02013-12-03 13:16:03 +0000216 const _ZipEntryRO* zipEntry = reinterpret_cast<_ZipEntryRO*>(entry);
Elliott Hughes78de4f92019-06-14 15:28:38 -0700217 const uint16_t requiredSize = zipEntry->name.length() + 1;
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700218
Narayan Kamathafd31e02013-12-03 13:16:03 +0000219 if (bufLen < requiredSize) {
220 ALOGW("Buffer too short, requires %d bytes for entry name", requiredSize);
221 return requiredSize;
222 }
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700223
Elliott Hughes78de4f92019-06-14 15:28:38 -0700224 memcpy(buffer, zipEntry->name.data(), requiredSize - 1);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000225 buffer[requiredSize - 1] = '\0';
226
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700227 return 0;
228}
229
230/*
231 * Create a new FileMap object that spans the data in "entry".
232 */
233FileMap* ZipFileRO::createEntryFileMap(ZipEntryRO entry) const
234{
Narayan Kamathafd31e02013-12-03 13:16:03 +0000235 const _ZipEntryRO *zipEntry = reinterpret_cast<_ZipEntryRO*>(entry);
236 const ZipEntry& ze = zipEntry->entry;
237 int fd = GetFileDescriptor(mHandle);
238 size_t actualLen = 0;
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700239
Narayan Kamathafd31e02013-12-03 13:16:03 +0000240 if (ze.method == kCompressStored) {
241 actualLen = ze.uncompressed_length;
Kenny Root0d6c2d72013-08-21 10:40:16 -0700242 } else {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000243 actualLen = ze.compressed_length;
Kenny Root0d6c2d72013-08-21 10:40:16 -0700244 }
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700245
Narayan Kamathafd31e02013-12-03 13:16:03 +0000246 FileMap* newMap = new FileMap();
247 if (!newMap->create(mFileName, fd, ze.offset, actualLen, true)) {
Narayan Kamath688ff4c2015-02-23 15:47:54 +0000248 delete newMap;
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700249 return NULL;
250 }
251
252 return newMap;
253}
254
255/*
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000256 * Create a new incfs::IncFsFileMap object that spans the data in "entry".
257 */
258std::optional<incfs::IncFsFileMap> ZipFileRO::createEntryIncFsFileMap(ZipEntryRO entry) const
259{
260 const _ZipEntryRO *zipEntry = reinterpret_cast<_ZipEntryRO*>(entry);
261 const ZipEntry& ze = zipEntry->entry;
262 int fd = GetFileDescriptor(mHandle);
263 size_t actualLen = 0;
264
265 if (ze.method == kCompressStored) {
266 actualLen = ze.uncompressed_length;
267 } else {
268 actualLen = ze.compressed_length;
269 }
270
271 incfs::IncFsFileMap newMap;
272 if (!newMap.Create(fd, ze.offset, actualLen, mFileName)) {
273 return std::nullopt;
274 }
275 return std::move(newMap);
276}
277
278/*
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700279 * Uncompress an entry, in its entirety, into the provided output buffer.
280 *
281 * This doesn't verify the data's CRC, which might be useful for
282 * uncompressed data. The caller should be able to manage it.
283 */
Narayan Kamathafd31e02013-12-03 13:16:03 +0000284bool ZipFileRO::uncompressEntry(ZipEntryRO entry, void* buffer, size_t size) const
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700285{
Narayan Kamathafd31e02013-12-03 13:16:03 +0000286 _ZipEntryRO *zipEntry = reinterpret_cast<_ZipEntryRO*>(entry);
287 const int32_t error = ExtractToMemory(mHandle, &(zipEntry->entry),
288 (uint8_t*) buffer, size);
289 if (error) {
290 ALOGW("ExtractToMemory failed with %s", ErrorCodeString(error));
Kenny Root0d6c2d72013-08-21 10:40:16 -0700291 return false;
292 }
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700293
Narayan Kamathafd31e02013-12-03 13:16:03 +0000294 return true;
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700295}
296
297/*
298 * Uncompress an entry, in its entirety, to an open file descriptor.
299 *
300 * This doesn't verify the data's CRC, but probably should.
301 */
302bool ZipFileRO::uncompressEntry(ZipEntryRO entry, int fd) const
303{
Narayan Kamathafd31e02013-12-03 13:16:03 +0000304 _ZipEntryRO *zipEntry = reinterpret_cast<_ZipEntryRO*>(entry);
305 const int32_t error = ExtractEntryToFile(mHandle, &(zipEntry->entry), fd);
306 if (error) {
307 ALOGW("ExtractToMemory failed with %s", ErrorCodeString(error));
Kenny Root0d6c2d72013-08-21 10:40:16 -0700308 return false;
309 }
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700310
Narayan Kamathafd31e02013-12-03 13:16:03 +0000311 return true;
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700312}