blob: a485d2bf59d2ea355febf6911eee6a9c132a42bd [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
Narayan Kamathafd31e02013-12-03 13:16:03 +000053 _ZipEntryRO() : cookie(NULL) {
54 }
Mathias Agopian1f5762e2013-05-06 20:20:34 -070055
Piotr Jastrzebski1a68b072014-08-08 12:52:39 +010056 ~_ZipEntryRO() {
57 EndIteration(cookie);
58 }
59
Narayan Kamathafd31e02013-12-03 13:16:03 +000060private:
61 _ZipEntryRO(const _ZipEntryRO& other);
62 _ZipEntryRO& operator=(const _ZipEntryRO& other);
63};
Mathias Agopian1f5762e2013-05-06 20:20:34 -070064
65ZipFileRO::~ZipFileRO() {
Narayan Kamathafd31e02013-12-03 13:16:03 +000066 CloseArchive(mHandle);
67 free(mFileName);
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));
Mathias Agopian1f5762e2013-05-06 20:20:34 -070080 return NULL;
81 }
82
Narayan Kamathafd31e02013-12-03 13:16:03 +000083 return new ZipFileRO(handle, strdup(zipFileName));
Mathias Agopian1f5762e2013-05-06 20:20:34 -070084}
85
Narayan Kamathafd31e02013-12-03 13:16:03 +000086
87ZipEntryRO ZipFileRO::findEntryByName(const char* entryName) const
Mathias Agopian1f5762e2013-05-06 20:20:34 -070088{
Narayan Kamathafd31e02013-12-03 13:16:03 +000089 _ZipEntryRO* data = new _ZipEntryRO;
90 const int32_t error = FindEntry(mHandle, entryName, &(data->entry));
91 if (error) {
92 delete data;
Mathias Agopian1f5762e2013-05-06 20:20:34 -070093 return NULL;
94 }
95
Narayan Kamathafd31e02013-12-03 13:16:03 +000096 data->name.name = entryName;
97 data->name.name_length = strlen(entryName);
Mathias Agopian1f5762e2013-05-06 20:20:34 -070098
Narayan Kamathafd31e02013-12-03 13:16:03 +000099 return (ZipEntryRO) data;
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700100}
101
102/*
103 * Get the useful fields from the zip entry.
104 *
105 * Returns "false" if the offsets to the fields or the contents of the fields
106 * appear to be bogus.
107 */
108bool ZipFileRO::getEntryInfo(ZipEntryRO entry, int* pMethod, size_t* pUncompLen,
109 size_t* pCompLen, off64_t* pOffset, long* pModWhen, long* pCrc32) const
110{
Narayan Kamathafd31e02013-12-03 13:16:03 +0000111 const _ZipEntryRO* zipEntry = reinterpret_cast<_ZipEntryRO*>(entry);
112 const ZipEntry& ze = zipEntry->entry;
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700113
Narayan Kamathafd31e02013-12-03 13:16:03 +0000114 if (pMethod != NULL) {
115 *pMethod = ze.method;
Kenny Root0d6c2d72013-08-21 10:40:16 -0700116 }
Narayan Kamathafd31e02013-12-03 13:16:03 +0000117 if (pUncompLen != NULL) {
118 *pUncompLen = ze.uncompressed_length;
119 }
120 if (pCompLen != NULL) {
121 *pCompLen = ze.compressed_length;
122 }
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700123 if (pOffset != NULL) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000124 *pOffset = ze.offset;
125 }
126 if (pModWhen != NULL) {
127 *pModWhen = ze.mod_time;
128 }
129 if (pCrc32 != NULL) {
130 *pCrc32 = ze.crc32;
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700131 }
132
133 return true;
134}
135
Narayan Kamathafd31e02013-12-03 13:16:03 +0000136bool ZipFileRO::startIteration(void** cookie)
137{
138 _ZipEntryRO* ze = new _ZipEntryRO;
139 int32_t error = StartIteration(mHandle, &(ze->cookie), NULL /* prefix */);
140 if (error) {
141 ALOGW("Could not start iteration over %s: %s", mFileName, ErrorCodeString(error));
142 delete ze;
143 return false;
144 }
145
146 *cookie = ze;
147 return true;
148}
149
150ZipEntryRO ZipFileRO::nextEntry(void* cookie)
151{
152 _ZipEntryRO* ze = reinterpret_cast<_ZipEntryRO*>(cookie);
153 int32_t error = Next(ze->cookie, &(ze->entry), &(ze->name));
154 if (error) {
155 if (error != -1) {
156 ALOGW("Error iteration over %s: %s", mFileName, ErrorCodeString(error));
157 }
158 return NULL;
159 }
160
161 return &(ze->entry);
162}
163
164void ZipFileRO::endIteration(void* cookie)
165{
166 delete reinterpret_cast<_ZipEntryRO*>(cookie);
167}
168
169void ZipFileRO::releaseEntry(ZipEntryRO entry) const
170{
171 delete reinterpret_cast<_ZipEntryRO*>(entry);
172}
173
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700174/*
175 * Copy the entry's filename to the buffer.
176 */
177int ZipFileRO::getEntryFileName(ZipEntryRO entry, char* buffer, int bufLen)
178 const
179{
Narayan Kamathafd31e02013-12-03 13:16:03 +0000180 const _ZipEntryRO* zipEntry = reinterpret_cast<_ZipEntryRO*>(entry);
181 const uint16_t requiredSize = zipEntry->name.name_length + 1;
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700182
Narayan Kamathafd31e02013-12-03 13:16:03 +0000183 if (bufLen < requiredSize) {
184 ALOGW("Buffer too short, requires %d bytes for entry name", requiredSize);
185 return requiredSize;
186 }
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700187
Narayan Kamathafd31e02013-12-03 13:16:03 +0000188 memcpy(buffer, zipEntry->name.name, requiredSize - 1);
189 buffer[requiredSize - 1] = '\0';
190
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700191 return 0;
192}
193
194/*
195 * Create a new FileMap object that spans the data in "entry".
196 */
197FileMap* ZipFileRO::createEntryFileMap(ZipEntryRO entry) const
198{
Narayan Kamathafd31e02013-12-03 13:16:03 +0000199 const _ZipEntryRO *zipEntry = reinterpret_cast<_ZipEntryRO*>(entry);
200 const ZipEntry& ze = zipEntry->entry;
201 int fd = GetFileDescriptor(mHandle);
202 size_t actualLen = 0;
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700203
Narayan Kamathafd31e02013-12-03 13:16:03 +0000204 if (ze.method == kCompressStored) {
205 actualLen = ze.uncompressed_length;
Kenny Root0d6c2d72013-08-21 10:40:16 -0700206 } else {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000207 actualLen = ze.compressed_length;
Kenny Root0d6c2d72013-08-21 10:40:16 -0700208 }
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700209
Narayan Kamathafd31e02013-12-03 13:16:03 +0000210 FileMap* newMap = new FileMap();
211 if (!newMap->create(mFileName, fd, ze.offset, actualLen, true)) {
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700212 newMap->release();
213 return NULL;
214 }
215
216 return newMap;
217}
218
219/*
220 * Uncompress an entry, in its entirety, into the provided output buffer.
221 *
222 * This doesn't verify the data's CRC, which might be useful for
223 * uncompressed data. The caller should be able to manage it.
224 */
Narayan Kamathafd31e02013-12-03 13:16:03 +0000225bool ZipFileRO::uncompressEntry(ZipEntryRO entry, void* buffer, size_t size) const
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700226{
Narayan Kamathafd31e02013-12-03 13:16:03 +0000227 _ZipEntryRO *zipEntry = reinterpret_cast<_ZipEntryRO*>(entry);
228 const int32_t error = ExtractToMemory(mHandle, &(zipEntry->entry),
229 (uint8_t*) buffer, size);
230 if (error) {
231 ALOGW("ExtractToMemory failed with %s", ErrorCodeString(error));
Kenny Root0d6c2d72013-08-21 10:40:16 -0700232 return false;
233 }
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700234
Narayan Kamathafd31e02013-12-03 13:16:03 +0000235 return true;
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700236}
237
238/*
239 * Uncompress an entry, in its entirety, to an open file descriptor.
240 *
241 * This doesn't verify the data's CRC, but probably should.
242 */
243bool ZipFileRO::uncompressEntry(ZipEntryRO entry, int fd) const
244{
Narayan Kamathafd31e02013-12-03 13:16:03 +0000245 _ZipEntryRO *zipEntry = reinterpret_cast<_ZipEntryRO*>(entry);
246 const int32_t error = ExtractEntryToFile(mHandle, &(zipEntry->entry), fd);
247 if (error) {
248 ALOGW("ExtractToMemory failed with %s", ErrorCodeString(error));
Kenny Root0d6c2d72013-08-21 10:40:16 -0700249 return false;
250 }
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700251
Narayan Kamathafd31e02013-12-03 13:16:03 +0000252 return true;
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700253}