blob: 5f6e831a02003a7ae02833ff3631f13f186c02b8 [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
37/*
38 * We must open binary files using open(path, ... | O_BINARY) under Windows.
39 * Otherwise strange read errors will happen.
40 */
41#ifndef O_BINARY
42# define O_BINARY 0
43#endif
44
45using namespace android;
46
Narayan Kamathafd31e02013-12-03 13:16:03 +000047class _ZipEntryRO {
48public:
49 ZipEntry entry;
50 ZipEntryName name;
51 void *cookie;
Mathias Agopian1f5762e2013-05-06 20:20:34 -070052
Piotr Jastrzebskie2134a42014-08-13 07:50:20 +010053 _ZipEntryRO() : cookie(NULL) {}
Mathias Agopian1f5762e2013-05-06 20:20:34 -070054
Piotr Jastrzebski1a68b072014-08-08 12:52:39 +010055 ~_ZipEntryRO() {
56 EndIteration(cookie);
57 }
58
Narayan Kamathafd31e02013-12-03 13:16:03 +000059private:
60 _ZipEntryRO(const _ZipEntryRO& other);
61 _ZipEntryRO& operator=(const _ZipEntryRO& other);
62};
Mathias Agopian1f5762e2013-05-06 20:20:34 -070063
64ZipFileRO::~ZipFileRO() {
Narayan Kamathafd31e02013-12-03 13:16:03 +000065 CloseArchive(mHandle);
66 free(mFileName);
Mathias Agopian1f5762e2013-05-06 20:20:34 -070067}
68
69/*
Mathias Agopian1f5762e2013-05-06 20:20:34 -070070 * Open the specified file read-only. We memory-map the entire thing and
71 * close the file before returning.
72 */
Narayan Kamathafd31e02013-12-03 13:16:03 +000073/* static */ ZipFileRO* ZipFileRO::open(const char* zipFileName)
Mathias Agopian1f5762e2013-05-06 20:20:34 -070074{
Narayan Kamathafd31e02013-12-03 13:16:03 +000075 ZipArchiveHandle handle;
76 const int32_t error = OpenArchive(zipFileName, &handle);
77 if (error) {
78 ALOGW("Error opening archive %s: %s", zipFileName, ErrorCodeString(error));
Mathias Agopian1f5762e2013-05-06 20:20:34 -070079 return NULL;
80 }
81
Narayan Kamathafd31e02013-12-03 13:16:03 +000082 return new ZipFileRO(handle, strdup(zipFileName));
Mathias Agopian1f5762e2013-05-06 20:20:34 -070083}
84
Narayan Kamathafd31e02013-12-03 13:16:03 +000085
86ZipEntryRO ZipFileRO::findEntryByName(const char* entryName) const
Mathias Agopian1f5762e2013-05-06 20:20:34 -070087{
Narayan Kamathafd31e02013-12-03 13:16:03 +000088 _ZipEntryRO* data = new _ZipEntryRO;
Piotr Jastrzebskie2134a42014-08-13 07:50:20 +010089
90 data->name = ZipEntryName(entryName);
91
92 const int32_t error = FindEntry(mHandle, data->name, &(data->entry));
Narayan Kamathafd31e02013-12-03 13:16:03 +000093 if (error) {
94 delete data;
Mathias Agopian1f5762e2013-05-06 20:20:34 -070095 return NULL;
96 }
97
Narayan Kamathafd31e02013-12-03 13:16:03 +000098 return (ZipEntryRO) data;
Mathias Agopian1f5762e2013-05-06 20:20:34 -070099}
100
101/*
102 * Get the useful fields from the zip entry.
103 *
104 * Returns "false" if the offsets to the fields or the contents of the fields
105 * appear to be bogus.
106 */
107bool ZipFileRO::getEntryInfo(ZipEntryRO entry, int* pMethod, size_t* pUncompLen,
108 size_t* pCompLen, off64_t* pOffset, long* pModWhen, long* pCrc32) const
109{
Narayan Kamathafd31e02013-12-03 13:16:03 +0000110 const _ZipEntryRO* zipEntry = reinterpret_cast<_ZipEntryRO*>(entry);
111 const ZipEntry& ze = zipEntry->entry;
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700112
Narayan Kamathafd31e02013-12-03 13:16:03 +0000113 if (pMethod != NULL) {
114 *pMethod = ze.method;
Kenny Root0d6c2d72013-08-21 10:40:16 -0700115 }
Narayan Kamathafd31e02013-12-03 13:16:03 +0000116 if (pUncompLen != NULL) {
117 *pUncompLen = ze.uncompressed_length;
118 }
119 if (pCompLen != NULL) {
120 *pCompLen = ze.compressed_length;
121 }
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700122 if (pOffset != NULL) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000123 *pOffset = ze.offset;
124 }
125 if (pModWhen != NULL) {
126 *pModWhen = ze.mod_time;
127 }
128 if (pCrc32 != NULL) {
129 *pCrc32 = ze.crc32;
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700130 }
131
132 return true;
133}
134
Narayan Kamathafd31e02013-12-03 13:16:03 +0000135bool ZipFileRO::startIteration(void** cookie)
136{
137 _ZipEntryRO* ze = new _ZipEntryRO;
138 int32_t error = StartIteration(mHandle, &(ze->cookie), NULL /* prefix */);
139 if (error) {
140 ALOGW("Could not start iteration over %s: %s", mFileName, ErrorCodeString(error));
141 delete ze;
142 return false;
143 }
144
145 *cookie = ze;
146 return true;
147}
148
149ZipEntryRO ZipFileRO::nextEntry(void* cookie)
150{
151 _ZipEntryRO* ze = reinterpret_cast<_ZipEntryRO*>(cookie);
152 int32_t error = Next(ze->cookie, &(ze->entry), &(ze->name));
153 if (error) {
154 if (error != -1) {
155 ALOGW("Error iteration over %s: %s", mFileName, ErrorCodeString(error));
156 }
157 return NULL;
158 }
159
160 return &(ze->entry);
161}
162
163void ZipFileRO::endIteration(void* cookie)
164{
165 delete reinterpret_cast<_ZipEntryRO*>(cookie);
166}
167
168void ZipFileRO::releaseEntry(ZipEntryRO entry) const
169{
170 delete reinterpret_cast<_ZipEntryRO*>(entry);
171}
172
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700173/*
174 * Copy the entry's filename to the buffer.
175 */
176int ZipFileRO::getEntryFileName(ZipEntryRO entry, char* buffer, int bufLen)
177 const
178{
Narayan Kamathafd31e02013-12-03 13:16:03 +0000179 const _ZipEntryRO* zipEntry = reinterpret_cast<_ZipEntryRO*>(entry);
180 const uint16_t requiredSize = zipEntry->name.name_length + 1;
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700181
Narayan Kamathafd31e02013-12-03 13:16:03 +0000182 if (bufLen < requiredSize) {
183 ALOGW("Buffer too short, requires %d bytes for entry name", requiredSize);
184 return requiredSize;
185 }
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700186
Narayan Kamathafd31e02013-12-03 13:16:03 +0000187 memcpy(buffer, zipEntry->name.name, requiredSize - 1);
188 buffer[requiredSize - 1] = '\0';
189
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700190 return 0;
191}
192
193/*
194 * Create a new FileMap object that spans the data in "entry".
195 */
196FileMap* ZipFileRO::createEntryFileMap(ZipEntryRO entry) const
197{
Narayan Kamathafd31e02013-12-03 13:16:03 +0000198 const _ZipEntryRO *zipEntry = reinterpret_cast<_ZipEntryRO*>(entry);
199 const ZipEntry& ze = zipEntry->entry;
200 int fd = GetFileDescriptor(mHandle);
201 size_t actualLen = 0;
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700202
Narayan Kamathafd31e02013-12-03 13:16:03 +0000203 if (ze.method == kCompressStored) {
204 actualLen = ze.uncompressed_length;
Kenny Root0d6c2d72013-08-21 10:40:16 -0700205 } else {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000206 actualLen = ze.compressed_length;
Kenny Root0d6c2d72013-08-21 10:40:16 -0700207 }
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700208
Narayan Kamathafd31e02013-12-03 13:16:03 +0000209 FileMap* newMap = new FileMap();
210 if (!newMap->create(mFileName, fd, ze.offset, actualLen, true)) {
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700211 newMap->release();
212 return NULL;
213 }
214
215 return newMap;
216}
217
218/*
219 * Uncompress an entry, in its entirety, into the provided output buffer.
220 *
221 * This doesn't verify the data's CRC, which might be useful for
222 * uncompressed data. The caller should be able to manage it.
223 */
Narayan Kamathafd31e02013-12-03 13:16:03 +0000224bool ZipFileRO::uncompressEntry(ZipEntryRO entry, void* buffer, size_t size) const
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700225{
Narayan Kamathafd31e02013-12-03 13:16:03 +0000226 _ZipEntryRO *zipEntry = reinterpret_cast<_ZipEntryRO*>(entry);
227 const int32_t error = ExtractToMemory(mHandle, &(zipEntry->entry),
228 (uint8_t*) buffer, size);
229 if (error) {
230 ALOGW("ExtractToMemory failed with %s", ErrorCodeString(error));
Kenny Root0d6c2d72013-08-21 10:40:16 -0700231 return false;
232 }
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700233
Narayan Kamathafd31e02013-12-03 13:16:03 +0000234 return true;
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700235}
236
237/*
238 * Uncompress an entry, in its entirety, to an open file descriptor.
239 *
240 * This doesn't verify the data's CRC, but probably should.
241 */
242bool ZipFileRO::uncompressEntry(ZipEntryRO entry, int fd) const
243{
Narayan Kamathafd31e02013-12-03 13:16:03 +0000244 _ZipEntryRO *zipEntry = reinterpret_cast<_ZipEntryRO*>(entry);
245 const int32_t error = ExtractEntryToFile(mHandle, &(zipEntry->entry), fd);
246 if (error) {
247 ALOGW("ExtractToMemory failed with %s", ErrorCodeString(error));
Kenny Root0d6c2d72013-08-21 10:40:16 -0700248 return false;
249 }
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700250
Narayan Kamathafd31e02013-12-03 13:16:03 +0000251 return true;
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700252}