blob: 37aae524d88966e67704005aa227e27c2bb78550 [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);
58 free(mFileName);
Mathias Agopian1f5762e2013-05-06 20:20:34 -070059}
60
61/*
Mathias Agopian1f5762e2013-05-06 20:20:34 -070062 * Open the specified file read-only. We memory-map the entire thing and
63 * close the file before returning.
64 */
Narayan Kamathafd31e02013-12-03 13:16:03 +000065/* static */ ZipFileRO* ZipFileRO::open(const char* zipFileName)
Mathias Agopian1f5762e2013-05-06 20:20:34 -070066{
Narayan Kamathafd31e02013-12-03 13:16:03 +000067 ZipArchiveHandle handle;
68 const int32_t error = OpenArchive(zipFileName, &handle);
69 if (error) {
70 ALOGW("Error opening archive %s: %s", zipFileName, ErrorCodeString(error));
Mathias Agopian1f5762e2013-05-06 20:20:34 -070071 return NULL;
72 }
73
Narayan Kamathafd31e02013-12-03 13:16:03 +000074 return new ZipFileRO(handle, strdup(zipFileName));
Mathias Agopian1f5762e2013-05-06 20:20:34 -070075}
76
Narayan Kamathafd31e02013-12-03 13:16:03 +000077
78ZipEntryRO ZipFileRO::findEntryByName(const char* entryName) const
Mathias Agopian1f5762e2013-05-06 20:20:34 -070079{
Narayan Kamathafd31e02013-12-03 13:16:03 +000080 _ZipEntryRO* data = new _ZipEntryRO;
Piotr Jastrzebskie2134a42014-08-13 07:50:20 +010081
Yusuke Satof162faa2015-06-25 14:58:16 -070082 data->name = ZipString(entryName);
Piotr Jastrzebskie2134a42014-08-13 07:50:20 +010083
84 const int32_t error = FindEntry(mHandle, data->name, &(data->entry));
Narayan Kamathafd31e02013-12-03 13:16:03 +000085 if (error) {
86 delete data;
Mathias Agopian1f5762e2013-05-06 20:20:34 -070087 return NULL;
88 }
89
Narayan Kamathafd31e02013-12-03 13:16:03 +000090 return (ZipEntryRO) data;
Mathias Agopian1f5762e2013-05-06 20:20:34 -070091}
92
93/*
94 * Get the useful fields from the zip entry.
95 *
96 * Returns "false" if the offsets to the fields or the contents of the fields
97 * appear to be bogus.
98 */
Narayan Kamath407753c2015-06-16 12:02:57 +010099bool ZipFileRO::getEntryInfo(ZipEntryRO entry, uint16_t* pMethod,
100 uint32_t* pUncompLen, uint32_t* pCompLen, off64_t* pOffset,
101 uint32_t* pModWhen, uint32_t* pCrc32) const
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700102{
Narayan Kamathafd31e02013-12-03 13:16:03 +0000103 const _ZipEntryRO* zipEntry = reinterpret_cast<_ZipEntryRO*>(entry);
104 const ZipEntry& ze = zipEntry->entry;
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700105
Narayan Kamathafd31e02013-12-03 13:16:03 +0000106 if (pMethod != NULL) {
107 *pMethod = ze.method;
Kenny Root0d6c2d72013-08-21 10:40:16 -0700108 }
Narayan Kamathafd31e02013-12-03 13:16:03 +0000109 if (pUncompLen != NULL) {
110 *pUncompLen = ze.uncompressed_length;
111 }
112 if (pCompLen != NULL) {
113 *pCompLen = ze.compressed_length;
114 }
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700115 if (pOffset != NULL) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000116 *pOffset = ze.offset;
117 }
118 if (pModWhen != NULL) {
119 *pModWhen = ze.mod_time;
120 }
121 if (pCrc32 != NULL) {
122 *pCrc32 = ze.crc32;
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700123 }
124
125 return true;
126}
127
Narayan Kamathafd31e02013-12-03 13:16:03 +0000128bool ZipFileRO::startIteration(void** cookie)
129{
Yusuke Sato957c2372015-08-03 16:17:16 -0700130 return startIteration(cookie, NULL, NULL);
131}
132
133bool ZipFileRO::startIteration(void** cookie, const char* prefix, const char* suffix)
134{
Narayan Kamathafd31e02013-12-03 13:16:03 +0000135 _ZipEntryRO* ze = new _ZipEntryRO;
Yusuke Sato957c2372015-08-03 16:17:16 -0700136 ZipString pe(prefix ? prefix : "");
137 ZipString se(suffix ? suffix : "");
Yusuke Satof162faa2015-06-25 14:58:16 -0700138 int32_t error = StartIteration(mHandle, &(ze->cookie),
Yusuke Sato957c2372015-08-03 16:17:16 -0700139 prefix ? &pe : NULL,
140 suffix ? &se : NULL);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000141 if (error) {
142 ALOGW("Could not start iteration over %s: %s", mFileName, ErrorCodeString(error));
143 delete ze;
144 return false;
145 }
146
147 *cookie = ze;
148 return true;
149}
150
151ZipEntryRO ZipFileRO::nextEntry(void* cookie)
152{
153 _ZipEntryRO* ze = reinterpret_cast<_ZipEntryRO*>(cookie);
154 int32_t error = Next(ze->cookie, &(ze->entry), &(ze->name));
155 if (error) {
156 if (error != -1) {
157 ALOGW("Error iteration over %s: %s", mFileName, ErrorCodeString(error));
158 }
159 return NULL;
160 }
161
162 return &(ze->entry);
163}
164
165void ZipFileRO::endIteration(void* cookie)
166{
167 delete reinterpret_cast<_ZipEntryRO*>(cookie);
168}
169
170void ZipFileRO::releaseEntry(ZipEntryRO entry) const
171{
172 delete reinterpret_cast<_ZipEntryRO*>(entry);
173}
174
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700175/*
176 * Copy the entry's filename to the buffer.
177 */
Narayan Kamath407753c2015-06-16 12:02:57 +0100178int ZipFileRO::getEntryFileName(ZipEntryRO entry, char* buffer, size_t bufLen)
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700179 const
180{
Narayan Kamathafd31e02013-12-03 13:16:03 +0000181 const _ZipEntryRO* zipEntry = reinterpret_cast<_ZipEntryRO*>(entry);
182 const uint16_t requiredSize = zipEntry->name.name_length + 1;
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700183
Narayan Kamathafd31e02013-12-03 13:16:03 +0000184 if (bufLen < requiredSize) {
185 ALOGW("Buffer too short, requires %d bytes for entry name", requiredSize);
186 return requiredSize;
187 }
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700188
Narayan Kamathafd31e02013-12-03 13:16:03 +0000189 memcpy(buffer, zipEntry->name.name, requiredSize - 1);
190 buffer[requiredSize - 1] = '\0';
191
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700192 return 0;
193}
194
195/*
196 * Create a new FileMap object that spans the data in "entry".
197 */
198FileMap* ZipFileRO::createEntryFileMap(ZipEntryRO entry) const
199{
Narayan Kamathafd31e02013-12-03 13:16:03 +0000200 const _ZipEntryRO *zipEntry = reinterpret_cast<_ZipEntryRO*>(entry);
201 const ZipEntry& ze = zipEntry->entry;
202 int fd = GetFileDescriptor(mHandle);
203 size_t actualLen = 0;
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700204
Narayan Kamathafd31e02013-12-03 13:16:03 +0000205 if (ze.method == kCompressStored) {
206 actualLen = ze.uncompressed_length;
Kenny Root0d6c2d72013-08-21 10:40:16 -0700207 } else {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000208 actualLen = ze.compressed_length;
Kenny Root0d6c2d72013-08-21 10:40:16 -0700209 }
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700210
Narayan Kamathafd31e02013-12-03 13:16:03 +0000211 FileMap* newMap = new FileMap();
212 if (!newMap->create(mFileName, fd, ze.offset, actualLen, true)) {
Narayan Kamath688ff4c2015-02-23 15:47:54 +0000213 delete newMap;
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700214 return NULL;
215 }
216
217 return newMap;
218}
219
220/*
221 * Uncompress an entry, in its entirety, into the provided output buffer.
222 *
223 * This doesn't verify the data's CRC, which might be useful for
224 * uncompressed data. The caller should be able to manage it.
225 */
Narayan Kamathafd31e02013-12-03 13:16:03 +0000226bool ZipFileRO::uncompressEntry(ZipEntryRO entry, void* buffer, size_t size) const
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700227{
Narayan Kamathafd31e02013-12-03 13:16:03 +0000228 _ZipEntryRO *zipEntry = reinterpret_cast<_ZipEntryRO*>(entry);
229 const int32_t error = ExtractToMemory(mHandle, &(zipEntry->entry),
230 (uint8_t*) buffer, size);
231 if (error) {
232 ALOGW("ExtractToMemory failed with %s", ErrorCodeString(error));
Kenny Root0d6c2d72013-08-21 10:40:16 -0700233 return false;
234 }
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700235
Narayan Kamathafd31e02013-12-03 13:16:03 +0000236 return true;
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700237}
238
239/*
240 * Uncompress an entry, in its entirety, to an open file descriptor.
241 *
242 * This doesn't verify the data's CRC, but probably should.
243 */
244bool ZipFileRO::uncompressEntry(ZipEntryRO entry, int fd) const
245{
Narayan Kamathafd31e02013-12-03 13:16:03 +0000246 _ZipEntryRO *zipEntry = reinterpret_cast<_ZipEntryRO*>(entry);
247 const int32_t error = ExtractEntryToFile(mHandle, &(zipEntry->entry), fd);
248 if (error) {
249 ALOGW("ExtractToMemory failed with %s", ErrorCodeString(error));
Kenny Root0d6c2d72013-08-21 10:40:16 -0700250 return false;
251 }
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700252
Narayan Kamathafd31e02013-12-03 13:16:03 +0000253 return true;
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700254}