blob: 6f927b41fa6423f6fc9e8bf4165211d25376393d [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;
42 ZipEntryName name;
43 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));
Narayan Kamath5a7587f2015-05-11 15:45:36 +010071 CloseArchive(handle);
Mathias Agopian1f5762e2013-05-06 20:20:34 -070072 return NULL;
73 }
74
Narayan Kamathafd31e02013-12-03 13:16:03 +000075 return new ZipFileRO(handle, strdup(zipFileName));
Mathias Agopian1f5762e2013-05-06 20:20:34 -070076}
77
Narayan Kamathafd31e02013-12-03 13:16:03 +000078
79ZipEntryRO ZipFileRO::findEntryByName(const char* entryName) const
Mathias Agopian1f5762e2013-05-06 20:20:34 -070080{
Narayan Kamathafd31e02013-12-03 13:16:03 +000081 _ZipEntryRO* data = new _ZipEntryRO;
Piotr Jastrzebskie2134a42014-08-13 07:50:20 +010082
83 data->name = ZipEntryName(entryName);
84
85 const int32_t error = FindEntry(mHandle, data->name, &(data->entry));
Narayan Kamathafd31e02013-12-03 13:16:03 +000086 if (error) {
87 delete data;
Mathias Agopian1f5762e2013-05-06 20:20:34 -070088 return NULL;
89 }
90
Narayan Kamathafd31e02013-12-03 13:16:03 +000091 return (ZipEntryRO) data;
Mathias Agopian1f5762e2013-05-06 20:20:34 -070092}
93
94/*
95 * Get the useful fields from the zip entry.
96 *
97 * Returns "false" if the offsets to the fields or the contents of the fields
98 * appear to be bogus.
99 */
Narayan Kamath4600dd02015-06-16 12:02:57 +0100100bool ZipFileRO::getEntryInfo(ZipEntryRO entry, uint16_t* pMethod,
101 uint32_t* pUncompLen, uint32_t* pCompLen, off64_t* pOffset,
102 uint32_t* pModWhen, uint32_t* pCrc32) const
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700103{
Narayan Kamathafd31e02013-12-03 13:16:03 +0000104 const _ZipEntryRO* zipEntry = reinterpret_cast<_ZipEntryRO*>(entry);
105 const ZipEntry& ze = zipEntry->entry;
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700106
Narayan Kamathafd31e02013-12-03 13:16:03 +0000107 if (pMethod != NULL) {
108 *pMethod = ze.method;
Kenny Root0d6c2d72013-08-21 10:40:16 -0700109 }
Narayan Kamathafd31e02013-12-03 13:16:03 +0000110 if (pUncompLen != NULL) {
111 *pUncompLen = ze.uncompressed_length;
112 }
113 if (pCompLen != NULL) {
114 *pCompLen = ze.compressed_length;
115 }
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700116 if (pOffset != NULL) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000117 *pOffset = ze.offset;
118 }
119 if (pModWhen != NULL) {
120 *pModWhen = ze.mod_time;
121 }
122 if (pCrc32 != NULL) {
123 *pCrc32 = ze.crc32;
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700124 }
125
126 return true;
127}
128
Narayan Kamathafd31e02013-12-03 13:16:03 +0000129bool ZipFileRO::startIteration(void** cookie)
130{
131 _ZipEntryRO* ze = new _ZipEntryRO;
132 int32_t error = StartIteration(mHandle, &(ze->cookie), NULL /* prefix */);
133 if (error) {
134 ALOGW("Could not start iteration over %s: %s", mFileName, ErrorCodeString(error));
135 delete ze;
136 return false;
137 }
138
139 *cookie = ze;
140 return true;
141}
142
143ZipEntryRO ZipFileRO::nextEntry(void* cookie)
144{
145 _ZipEntryRO* ze = reinterpret_cast<_ZipEntryRO*>(cookie);
146 int32_t error = Next(ze->cookie, &(ze->entry), &(ze->name));
147 if (error) {
148 if (error != -1) {
149 ALOGW("Error iteration over %s: %s", mFileName, ErrorCodeString(error));
150 }
151 return NULL;
152 }
153
154 return &(ze->entry);
155}
156
157void ZipFileRO::endIteration(void* cookie)
158{
159 delete reinterpret_cast<_ZipEntryRO*>(cookie);
160}
161
162void ZipFileRO::releaseEntry(ZipEntryRO entry) const
163{
164 delete reinterpret_cast<_ZipEntryRO*>(entry);
165}
166
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700167/*
168 * Copy the entry's filename to the buffer.
169 */
Narayan Kamath4600dd02015-06-16 12:02:57 +0100170int ZipFileRO::getEntryFileName(ZipEntryRO entry, char* buffer, size_t bufLen)
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700171 const
172{
Narayan Kamathafd31e02013-12-03 13:16:03 +0000173 const _ZipEntryRO* zipEntry = reinterpret_cast<_ZipEntryRO*>(entry);
174 const uint16_t requiredSize = zipEntry->name.name_length + 1;
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700175
Narayan Kamathafd31e02013-12-03 13:16:03 +0000176 if (bufLen < requiredSize) {
177 ALOGW("Buffer too short, requires %d bytes for entry name", requiredSize);
178 return requiredSize;
179 }
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700180
Narayan Kamathafd31e02013-12-03 13:16:03 +0000181 memcpy(buffer, zipEntry->name.name, requiredSize - 1);
182 buffer[requiredSize - 1] = '\0';
183
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700184 return 0;
185}
186
187/*
188 * Create a new FileMap object that spans the data in "entry".
189 */
190FileMap* ZipFileRO::createEntryFileMap(ZipEntryRO entry) const
191{
Narayan Kamathafd31e02013-12-03 13:16:03 +0000192 const _ZipEntryRO *zipEntry = reinterpret_cast<_ZipEntryRO*>(entry);
193 const ZipEntry& ze = zipEntry->entry;
194 int fd = GetFileDescriptor(mHandle);
195 size_t actualLen = 0;
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700196
Narayan Kamathafd31e02013-12-03 13:16:03 +0000197 if (ze.method == kCompressStored) {
198 actualLen = ze.uncompressed_length;
Kenny Root0d6c2d72013-08-21 10:40:16 -0700199 } else {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000200 actualLen = ze.compressed_length;
Kenny Root0d6c2d72013-08-21 10:40:16 -0700201 }
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700202
Narayan Kamathafd31e02013-12-03 13:16:03 +0000203 FileMap* newMap = new FileMap();
204 if (!newMap->create(mFileName, fd, ze.offset, actualLen, true)) {
Narayan Kamath688ff4c2015-02-23 15:47:54 +0000205 delete newMap;
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700206 return NULL;
207 }
208
209 return newMap;
210}
211
212/*
213 * Uncompress an entry, in its entirety, into the provided output buffer.
214 *
215 * This doesn't verify the data's CRC, which might be useful for
216 * uncompressed data. The caller should be able to manage it.
217 */
Narayan Kamathafd31e02013-12-03 13:16:03 +0000218bool ZipFileRO::uncompressEntry(ZipEntryRO entry, void* buffer, size_t size) const
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700219{
Narayan Kamathafd31e02013-12-03 13:16:03 +0000220 _ZipEntryRO *zipEntry = reinterpret_cast<_ZipEntryRO*>(entry);
221 const int32_t error = ExtractToMemory(mHandle, &(zipEntry->entry),
222 (uint8_t*) buffer, size);
223 if (error) {
224 ALOGW("ExtractToMemory failed with %s", ErrorCodeString(error));
Kenny Root0d6c2d72013-08-21 10:40:16 -0700225 return false;
226 }
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700227
Narayan Kamathafd31e02013-12-03 13:16:03 +0000228 return true;
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700229}
230
231/*
232 * Uncompress an entry, in its entirety, to an open file descriptor.
233 *
234 * This doesn't verify the data's CRC, but probably should.
235 */
236bool ZipFileRO::uncompressEntry(ZipEntryRO entry, int fd) const
237{
Narayan Kamathafd31e02013-12-03 13:16:03 +0000238 _ZipEntryRO *zipEntry = reinterpret_cast<_ZipEntryRO*>(entry);
239 const int32_t error = ExtractEntryToFile(mHandle, &(zipEntry->entry), fd);
240 if (error) {
241 ALOGW("ExtractToMemory failed with %s", ErrorCodeString(error));
Kenny Root0d6c2d72013-08-21 10:40:16 -0700242 return false;
243 }
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700244
Narayan Kamathafd31e02013-12-03 13:16:03 +0000245 return true;
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700246}