blob: 6e2ca60cc3d39de497ce80399bf40005b5f1d188 [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;
Yusuke Satof162faa2015-06-25 14:58:16 -070042 ZipString name;
Narayan Kamathafd31e02013-12-03 13:16:03 +000043 void *cookie;
Mathias Agopian1f5762e2013-05-06 20:20:34 -070044
Piotr Jastrzebskie2134a42014-08-13 07:50:20 +010045 _ZipEntryRO() : cookie(NULL) {}
Mathias Agopian1f5762e2013-05-06 20:20:34 -070046
Piotr Jastrzebski1a68b072014-08-08 12:52:39 +010047 ~_ZipEntryRO() {
48 EndIteration(cookie);
49 }
50
Narayan Kamathafd31e02013-12-03 13:16:03 +000051private:
52 _ZipEntryRO(const _ZipEntryRO& other);
53 _ZipEntryRO& operator=(const _ZipEntryRO& other);
54};
Mathias Agopian1f5762e2013-05-06 20:20:34 -070055
56ZipFileRO::~ZipFileRO() {
Narayan Kamathafd31e02013-12-03 13:16:03 +000057 CloseArchive(mHandle);
Dianne Hackbornca3872c2017-10-30 14:19:32 -070058 if (mFileName != NULL) {
59 free(mFileName);
60 }
Mathias Agopian1f5762e2013-05-06 20:20:34 -070061}
62
63/*
Mathias Agopian1f5762e2013-05-06 20:20:34 -070064 * Open the specified file read-only. We memory-map the entire thing and
65 * close the file before returning.
66 */
Narayan Kamathafd31e02013-12-03 13:16:03 +000067/* static */ ZipFileRO* ZipFileRO::open(const char* zipFileName)
Mathias Agopian1f5762e2013-05-06 20:20:34 -070068{
Narayan Kamathafd31e02013-12-03 13:16:03 +000069 ZipArchiveHandle handle;
70 const int32_t error = OpenArchive(zipFileName, &handle);
71 if (error) {
72 ALOGW("Error opening archive %s: %s", zipFileName, ErrorCodeString(error));
Narayan Kamath5a7587f2015-05-11 15:45:36 +010073 CloseArchive(handle);
Mathias Agopian1f5762e2013-05-06 20:20:34 -070074 return NULL;
75 }
76
Narayan Kamathafd31e02013-12-03 13:16:03 +000077 return new ZipFileRO(handle, strdup(zipFileName));
Mathias Agopian1f5762e2013-05-06 20:20:34 -070078}
79
Narayan Kamathafd31e02013-12-03 13:16:03 +000080
Dianne Hackbornca3872c2017-10-30 14:19:32 -070081/* static */ ZipFileRO* ZipFileRO::openFd(int fd, const char* debugFileName,
82 bool assume_ownership)
83{
84 ZipArchiveHandle handle;
85 const int32_t error = OpenArchiveFd(fd, debugFileName, &handle, assume_ownership);
86 if (error) {
87 ALOGW("Error opening archive fd %d %s: %s", fd, debugFileName, ErrorCodeString(error));
88 CloseArchive(handle);
89 return NULL;
90 }
91
92 return new ZipFileRO(handle, strdup(debugFileName));
93}
94
Narayan Kamathafd31e02013-12-03 13:16:03 +000095ZipEntryRO ZipFileRO::findEntryByName(const char* entryName) const
Mathias Agopian1f5762e2013-05-06 20:20:34 -070096{
Narayan Kamathafd31e02013-12-03 13:16:03 +000097 _ZipEntryRO* data = new _ZipEntryRO;
Piotr Jastrzebskie2134a42014-08-13 07:50:20 +010098
Yusuke Satof162faa2015-06-25 14:58:16 -070099 data->name = ZipString(entryName);
Piotr Jastrzebskie2134a42014-08-13 07:50:20 +0100100
101 const int32_t error = FindEntry(mHandle, data->name, &(data->entry));
Narayan Kamathafd31e02013-12-03 13:16:03 +0000102 if (error) {
103 delete data;
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700104 return NULL;
105 }
106
Narayan Kamathafd31e02013-12-03 13:16:03 +0000107 return (ZipEntryRO) data;
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700108}
109
110/*
111 * Get the useful fields from the zip entry.
112 *
113 * Returns "false" if the offsets to the fields or the contents of the fields
114 * appear to be bogus.
115 */
Narayan Kamath4600dd02015-06-16 12:02:57 +0100116bool ZipFileRO::getEntryInfo(ZipEntryRO entry, uint16_t* pMethod,
117 uint32_t* pUncompLen, uint32_t* pCompLen, off64_t* pOffset,
118 uint32_t* pModWhen, uint32_t* pCrc32) const
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700119{
Narayan Kamathafd31e02013-12-03 13:16:03 +0000120 const _ZipEntryRO* zipEntry = reinterpret_cast<_ZipEntryRO*>(entry);
121 const ZipEntry& ze = zipEntry->entry;
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700122
Narayan Kamathafd31e02013-12-03 13:16:03 +0000123 if (pMethod != NULL) {
124 *pMethod = ze.method;
Kenny Root0d6c2d72013-08-21 10:40:16 -0700125 }
Narayan Kamathafd31e02013-12-03 13:16:03 +0000126 if (pUncompLen != NULL) {
127 *pUncompLen = ze.uncompressed_length;
128 }
129 if (pCompLen != NULL) {
130 *pCompLen = ze.compressed_length;
131 }
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700132 if (pOffset != NULL) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000133 *pOffset = ze.offset;
134 }
135 if (pModWhen != NULL) {
136 *pModWhen = ze.mod_time;
137 }
138 if (pCrc32 != NULL) {
139 *pCrc32 = ze.crc32;
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700140 }
141
142 return true;
143}
144
Yusuke Sato34fe3df2015-06-19 17:18:07 -0700145bool ZipFileRO::startIteration(void** cookie) {
146 return startIteration(cookie, NULL, NULL);
147}
148
149bool ZipFileRO::startIteration(void** cookie, const char* prefix, const char* suffix)
Narayan Kamathafd31e02013-12-03 13:16:03 +0000150{
151 _ZipEntryRO* ze = new _ZipEntryRO;
Yusuke Sato53a19bb2015-06-29 13:09:51 -0700152 ZipString pe(prefix ? prefix : "");
153 ZipString se(suffix ? suffix : "");
Yusuke Sato34fe3df2015-06-19 17:18:07 -0700154 int32_t error = StartIteration(mHandle, &(ze->cookie),
155 prefix ? &pe : NULL,
156 suffix ? &se : NULL);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000157 if (error) {
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700158 ALOGW("Could not start iteration over %s: %s", mFileName != NULL ? mFileName : "<null>",
159 ErrorCodeString(error));
Narayan Kamathafd31e02013-12-03 13:16:03 +0000160 delete ze;
161 return false;
162 }
163
164 *cookie = ze;
165 return true;
166}
167
168ZipEntryRO ZipFileRO::nextEntry(void* cookie)
169{
170 _ZipEntryRO* ze = reinterpret_cast<_ZipEntryRO*>(cookie);
171 int32_t error = Next(ze->cookie, &(ze->entry), &(ze->name));
172 if (error) {
173 if (error != -1) {
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700174 ALOGW("Error iteration over %s: %s", mFileName != NULL ? mFileName : "<null>",
175 ErrorCodeString(error));
Narayan Kamathafd31e02013-12-03 13:16:03 +0000176 }
177 return NULL;
178 }
179
180 return &(ze->entry);
181}
182
183void ZipFileRO::endIteration(void* cookie)
184{
185 delete reinterpret_cast<_ZipEntryRO*>(cookie);
186}
187
188void ZipFileRO::releaseEntry(ZipEntryRO entry) const
189{
190 delete reinterpret_cast<_ZipEntryRO*>(entry);
191}
192
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700193/*
194 * Copy the entry's filename to the buffer.
195 */
Narayan Kamath4600dd02015-06-16 12:02:57 +0100196int ZipFileRO::getEntryFileName(ZipEntryRO entry, char* buffer, size_t bufLen)
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700197 const
198{
Narayan Kamathafd31e02013-12-03 13:16:03 +0000199 const _ZipEntryRO* zipEntry = reinterpret_cast<_ZipEntryRO*>(entry);
200 const uint16_t requiredSize = zipEntry->name.name_length + 1;
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700201
Narayan Kamathafd31e02013-12-03 13:16:03 +0000202 if (bufLen < requiredSize) {
203 ALOGW("Buffer too short, requires %d bytes for entry name", requiredSize);
204 return requiredSize;
205 }
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700206
Narayan Kamathafd31e02013-12-03 13:16:03 +0000207 memcpy(buffer, zipEntry->name.name, requiredSize - 1);
208 buffer[requiredSize - 1] = '\0';
209
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700210 return 0;
211}
212
213/*
214 * Create a new FileMap object that spans the data in "entry".
215 */
216FileMap* ZipFileRO::createEntryFileMap(ZipEntryRO entry) const
217{
Narayan Kamathafd31e02013-12-03 13:16:03 +0000218 const _ZipEntryRO *zipEntry = reinterpret_cast<_ZipEntryRO*>(entry);
219 const ZipEntry& ze = zipEntry->entry;
220 int fd = GetFileDescriptor(mHandle);
221 size_t actualLen = 0;
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700222
Narayan Kamathafd31e02013-12-03 13:16:03 +0000223 if (ze.method == kCompressStored) {
224 actualLen = ze.uncompressed_length;
Kenny Root0d6c2d72013-08-21 10:40:16 -0700225 } else {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000226 actualLen = ze.compressed_length;
Kenny Root0d6c2d72013-08-21 10:40:16 -0700227 }
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700228
Narayan Kamathafd31e02013-12-03 13:16:03 +0000229 FileMap* newMap = new FileMap();
230 if (!newMap->create(mFileName, fd, ze.offset, actualLen, true)) {
Narayan Kamath688ff4c2015-02-23 15:47:54 +0000231 delete newMap;
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700232 return NULL;
233 }
234
235 return newMap;
236}
237
238/*
239 * Uncompress an entry, in its entirety, into the provided output buffer.
240 *
241 * This doesn't verify the data's CRC, which might be useful for
242 * uncompressed data. The caller should be able to manage it.
243 */
Narayan Kamathafd31e02013-12-03 13:16:03 +0000244bool ZipFileRO::uncompressEntry(ZipEntryRO entry, void* buffer, size_t size) const
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700245{
Narayan Kamathafd31e02013-12-03 13:16:03 +0000246 _ZipEntryRO *zipEntry = reinterpret_cast<_ZipEntryRO*>(entry);
247 const int32_t error = ExtractToMemory(mHandle, &(zipEntry->entry),
248 (uint8_t*) buffer, size);
249 if (error) {
250 ALOGW("ExtractToMemory failed with %s", ErrorCodeString(error));
Kenny Root0d6c2d72013-08-21 10:40:16 -0700251 return false;
252 }
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700253
Narayan Kamathafd31e02013-12-03 13:16:03 +0000254 return true;
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700255}
256
257/*
258 * Uncompress an entry, in its entirety, to an open file descriptor.
259 *
260 * This doesn't verify the data's CRC, but probably should.
261 */
262bool ZipFileRO::uncompressEntry(ZipEntryRO entry, int fd) const
263{
Narayan Kamathafd31e02013-12-03 13:16:03 +0000264 _ZipEntryRO *zipEntry = reinterpret_cast<_ZipEntryRO*>(entry);
265 const int32_t error = ExtractEntryToFile(mHandle, &(zipEntry->entry), fd);
266 if (error) {
267 ALOGW("ExtractToMemory failed with %s", ErrorCodeString(error));
Kenny Root0d6c2d72013-08-21 10:40:16 -0700268 return false;
269 }
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700270
Narayan Kamathafd31e02013-12-03 13:16:03 +0000271 return true;
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700272}