blob: 839c7b6fef37ab3462f9be5078a628db0f71f623 [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,
Pawan Waghcb47bda2024-04-01 22:21:52 +0000122 uint32_t* pUncompLen, uint32_t* pCompLen, off64_t* pOffset,
123 uint32_t* pModWhen, uint32_t* pCrc32) const
124{
125 return getEntryInfo(entry, pMethod, pUncompLen, pCompLen, pOffset, pModWhen,
126 pCrc32, nullptr);
127}
128
129bool ZipFileRO::getEntryInfo(ZipEntryRO entry, uint16_t* pMethod,
Narayan Kamath4600dd02015-06-16 12:02:57 +0100130 uint32_t* pUncompLen, uint32_t* pCompLen, off64_t* pOffset,
Pawan Waghcb47bda2024-04-01 22:21:52 +0000131 uint32_t* pModWhen, uint32_t* pCrc32, uint16_t* pExtraFieldSize) const
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700132{
Narayan Kamathafd31e02013-12-03 13:16:03 +0000133 const _ZipEntryRO* zipEntry = reinterpret_cast<_ZipEntryRO*>(entry);
134 const ZipEntry& ze = zipEntry->entry;
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700135
Pawan Waghcb47bda2024-04-01 22:21:52 +0000136 if (pMethod != nullptr) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000137 *pMethod = ze.method;
Kenny Root0d6c2d72013-08-21 10:40:16 -0700138 }
Pawan Waghcb47bda2024-04-01 22:21:52 +0000139 if (pUncompLen != nullptr) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000140 *pUncompLen = ze.uncompressed_length;
141 }
Pawan Waghcb47bda2024-04-01 22:21:52 +0000142 if (pCompLen != nullptr) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000143 *pCompLen = ze.compressed_length;
144 }
Pawan Waghcb47bda2024-04-01 22:21:52 +0000145 if (pOffset != nullptr) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000146 *pOffset = ze.offset;
147 }
Pawan Waghcb47bda2024-04-01 22:21:52 +0000148 if (pModWhen != nullptr) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000149 *pModWhen = ze.mod_time;
150 }
Pawan Waghcb47bda2024-04-01 22:21:52 +0000151 if (pCrc32 != nullptr) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000152 *pCrc32 = ze.crc32;
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700153 }
Pawan Waghcb47bda2024-04-01 22:21:52 +0000154 if (pExtraFieldSize != nullptr) {
155 *pExtraFieldSize = ze.extra_field_size;
156 }
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700157
158 return true;
159}
160
Yusuke Sato34fe3df2015-06-19 17:18:07 -0700161bool ZipFileRO::startIteration(void** cookie) {
Alex Buynytskyyb92c16e2023-07-05 21:48:59 -0700162 return startIteration(cookie, nullptr, nullptr);
Yusuke Sato34fe3df2015-06-19 17:18:07 -0700163}
164
Alex Buynytskyyb92c16e2023-07-05 21:48:59 -0700165bool ZipFileRO::startIteration(void** cookie, const char* prefix, const char* suffix) {
166 auto result = startIterationOrError(prefix, suffix);
167 if (!result.ok()) {
168 return false;
169 }
170 *cookie = result.value();
171 return true;
172}
173
174base::expected<void*, int32_t>
175ZipFileRO::startIterationOrError(const char* prefix, const char* suffix) {
176 _ZipEntryRO ze;
177 int32_t error = StartIteration(mHandle, &(ze.cookie),
Elliott Hughes7a6cc0c2019-05-08 12:12:39 -0700178 prefix ? prefix : "", suffix ? suffix : "");
Narayan Kamathafd31e02013-12-03 13:16:03 +0000179 if (error) {
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700180 ALOGW("Could not start iteration over %s: %s", mFileName != NULL ? mFileName : "<null>",
181 ErrorCodeString(error));
Alex Buynytskyyb92c16e2023-07-05 21:48:59 -0700182 return base::unexpected(error);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000183 }
184
Alex Buynytskyyb92c16e2023-07-05 21:48:59 -0700185 return ze.convertToPtr();
Narayan Kamathafd31e02013-12-03 13:16:03 +0000186}
187
Alex Buynytskyyb92c16e2023-07-05 21:48:59 -0700188ZipEntryRO ZipFileRO::nextEntry(void* cookie) {
189 auto result = nextEntryOrError(cookie);
190 if (!result.ok()) {
191 return nullptr;
192 }
193 return result.value();
194}
195
196base::expected<ZipEntryRO, int32_t> ZipFileRO::nextEntryOrError(void* cookie) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000197 _ZipEntryRO* ze = reinterpret_cast<_ZipEntryRO*>(cookie);
198 int32_t error = Next(ze->cookie, &(ze->entry), &(ze->name));
199 if (error) {
200 if (error != -1) {
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700201 ALOGW("Error iteration over %s: %s", mFileName != NULL ? mFileName : "<null>",
202 ErrorCodeString(error));
Alex Buynytskyyb92c16e2023-07-05 21:48:59 -0700203 return base::unexpected(error);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000204 }
Alex Buynytskyyb92c16e2023-07-05 21:48:59 -0700205 return nullptr;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000206 }
207
208 return &(ze->entry);
209}
210
211void ZipFileRO::endIteration(void* cookie)
212{
213 delete reinterpret_cast<_ZipEntryRO*>(cookie);
214}
215
216void ZipFileRO::releaseEntry(ZipEntryRO entry) const
217{
218 delete reinterpret_cast<_ZipEntryRO*>(entry);
219}
220
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700221/*
222 * Copy the entry's filename to the buffer.
223 */
Narayan Kamath4600dd02015-06-16 12:02:57 +0100224int ZipFileRO::getEntryFileName(ZipEntryRO entry, char* buffer, size_t bufLen)
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700225 const
226{
Narayan Kamathafd31e02013-12-03 13:16:03 +0000227 const _ZipEntryRO* zipEntry = reinterpret_cast<_ZipEntryRO*>(entry);
Elliott Hughes78de4f92019-06-14 15:28:38 -0700228 const uint16_t requiredSize = zipEntry->name.length() + 1;
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700229
Narayan Kamathafd31e02013-12-03 13:16:03 +0000230 if (bufLen < requiredSize) {
231 ALOGW("Buffer too short, requires %d bytes for entry name", requiredSize);
232 return requiredSize;
233 }
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700234
Elliott Hughes78de4f92019-06-14 15:28:38 -0700235 memcpy(buffer, zipEntry->name.data(), requiredSize - 1);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000236 buffer[requiredSize - 1] = '\0';
237
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700238 return 0;
239}
240
241/*
242 * Create a new FileMap object that spans the data in "entry".
243 */
244FileMap* ZipFileRO::createEntryFileMap(ZipEntryRO entry) const
245{
Narayan Kamathafd31e02013-12-03 13:16:03 +0000246 const _ZipEntryRO *zipEntry = reinterpret_cast<_ZipEntryRO*>(entry);
247 const ZipEntry& ze = zipEntry->entry;
248 int fd = GetFileDescriptor(mHandle);
249 size_t actualLen = 0;
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700250
Narayan Kamathafd31e02013-12-03 13:16:03 +0000251 if (ze.method == kCompressStored) {
252 actualLen = ze.uncompressed_length;
Kenny Root0d6c2d72013-08-21 10:40:16 -0700253 } else {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000254 actualLen = ze.compressed_length;
Kenny Root0d6c2d72013-08-21 10:40:16 -0700255 }
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700256
Narayan Kamathafd31e02013-12-03 13:16:03 +0000257 FileMap* newMap = new FileMap();
258 if (!newMap->create(mFileName, fd, ze.offset, actualLen, true)) {
Narayan Kamath688ff4c2015-02-23 15:47:54 +0000259 delete newMap;
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700260 return NULL;
261 }
262
263 return newMap;
264}
265
266/*
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000267 * Create a new incfs::IncFsFileMap object that spans the data in "entry".
268 */
269std::optional<incfs::IncFsFileMap> ZipFileRO::createEntryIncFsFileMap(ZipEntryRO entry) const
270{
271 const _ZipEntryRO *zipEntry = reinterpret_cast<_ZipEntryRO*>(entry);
272 const ZipEntry& ze = zipEntry->entry;
273 int fd = GetFileDescriptor(mHandle);
274 size_t actualLen = 0;
275
276 if (ze.method == kCompressStored) {
277 actualLen = ze.uncompressed_length;
278 } else {
279 actualLen = ze.compressed_length;
280 }
281
282 incfs::IncFsFileMap newMap;
283 if (!newMap.Create(fd, ze.offset, actualLen, mFileName)) {
284 return std::nullopt;
285 }
286 return std::move(newMap);
287}
288
289/*
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700290 * Uncompress an entry, in its entirety, into the provided output buffer.
291 *
292 * This doesn't verify the data's CRC, which might be useful for
293 * uncompressed data. The caller should be able to manage it.
294 */
Narayan Kamathafd31e02013-12-03 13:16:03 +0000295bool ZipFileRO::uncompressEntry(ZipEntryRO entry, void* buffer, size_t size) const
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700296{
Narayan Kamathafd31e02013-12-03 13:16:03 +0000297 _ZipEntryRO *zipEntry = reinterpret_cast<_ZipEntryRO*>(entry);
298 const int32_t error = ExtractToMemory(mHandle, &(zipEntry->entry),
299 (uint8_t*) buffer, size);
300 if (error) {
301 ALOGW("ExtractToMemory failed with %s", ErrorCodeString(error));
Kenny Root0d6c2d72013-08-21 10:40:16 -0700302 return false;
303 }
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700304
Narayan Kamathafd31e02013-12-03 13:16:03 +0000305 return true;
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700306}
307
308/*
309 * Uncompress an entry, in its entirety, to an open file descriptor.
310 *
311 * This doesn't verify the data's CRC, but probably should.
312 */
313bool ZipFileRO::uncompressEntry(ZipEntryRO entry, int fd) const
314{
Narayan Kamathafd31e02013-12-03 13:16:03 +0000315 _ZipEntryRO *zipEntry = reinterpret_cast<_ZipEntryRO*>(entry);
316 const int32_t error = ExtractEntryToFile(mHandle, &(zipEntry->entry), fd);
317 if (error) {
Elliott Hughesc9fc6652024-04-03 01:43:35 +0000318 ALOGW("ExtractToFile failed with %s", ErrorCodeString(error));
Kenny Root0d6c2d72013-08-21 10:40:16 -0700319 return false;
320 }
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700321
Narayan Kamathafd31e02013-12-03 13:16:03 +0000322 return true;
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700323}
Pawan Wagh38d8f722024-03-28 16:06:21 +0000324
325const char* ZipFileRO::getZipFileName() {
326 return mFileName;
327}