blob: bee86b2bbae050468e8d74a463589bfa8b847ed5 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
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
Mathias Agopianf446ba92009-06-04 13:53:57 -070022#include <utils/ZipFileRO.h>
23#include <utils/Log.h>
24#include <utils/misc.h>
Kenny Rootfa989202010-09-24 07:57:37 -070025#include <utils/threads.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026
27#include <zlib.h>
28
29#include <string.h>
30#include <fcntl.h>
31#include <errno.h>
32#include <assert.h>
Kenny Rootd4066a42010-04-22 18:28:29 -070033#include <unistd.h>
34
35/*
36 * TEMP_FAILURE_RETRY is defined by some, but not all, versions of
37 * <unistd.h>. (Alas, it is not as standard as we'd hoped!) So, if it's
38 * not already defined, then define it here.
39 */
40#ifndef TEMP_FAILURE_RETRY
41/* Used to retry syscalls that can return EINTR. */
42#define TEMP_FAILURE_RETRY(exp) ({ \
43 typeof (exp) _rc; \
44 do { \
45 _rc = (exp); \
46 } while (_rc == -1 && errno == EINTR); \
47 _rc; })
48#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049
50using namespace android;
51
52/*
53 * Zip file constants.
54 */
55#define kEOCDSignature 0x06054b50
56#define kEOCDLen 22
57#define kEOCDNumEntries 8 // offset to #of entries in file
Kenny Rootd4066a42010-04-22 18:28:29 -070058#define kEOCDSize 12 // size of the central directory
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059#define kEOCDFileOffset 16 // offset to central directory
60
61#define kMaxCommentLen 65535 // longest possible in ushort
62#define kMaxEOCDSearch (kMaxCommentLen + kEOCDLen)
63
64#define kLFHSignature 0x04034b50
65#define kLFHLen 30 // excluding variable-len fields
66#define kLFHNameLen 26 // offset to filename length
67#define kLFHExtraLen 28 // offset to extra length
68
69#define kCDESignature 0x02014b50
70#define kCDELen 46 // excluding variable-len fields
71#define kCDEMethod 10 // offset to compression method
72#define kCDEModWhen 12 // offset to modification timestamp
73#define kCDECRC 16 // offset to entry CRC
74#define kCDECompLen 20 // offset to compressed length
75#define kCDEUncompLen 24 // offset to uncompressed length
76#define kCDENameLen 28 // offset to filename length
77#define kCDEExtraLen 30 // offset to extra length
78#define kCDECommentLen 32 // offset to comment length
79#define kCDELocalOffset 42 // offset to local hdr
80
81/*
82 * The values we return for ZipEntryRO use 0 as an invalid value, so we
83 * want to adjust the hash table index by a fixed amount. Using a large
84 * value helps insure that people don't mix & match arguments, e.g. to
85 * findEntryByIndex().
86 */
87#define kZipEntryAdj 10000
88
Kenny Rootdbf6f272010-10-01 18:28:28 -070089ZipFileRO::~ZipFileRO() {
90 free(mHashTable);
91 if (mDirectoryMap)
92 mDirectoryMap->release();
93 if (mFd >= 0)
94 TEMP_FAILURE_RETRY(close(mFd));
95 if (mFileName)
96 free(mFileName);
97}
98
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080099/*
100 * Convert a ZipEntryRO to a hash table index, verifying that it's in a
101 * valid range.
102 */
103int ZipFileRO::entryToIndex(const ZipEntryRO entry) const
104{
105 long ent = ((long) entry) - kZipEntryAdj;
106 if (ent < 0 || ent >= mHashTableSize || mHashTable[ent].name == NULL) {
107 LOGW("Invalid ZipEntryRO %p (%ld)\n", entry, ent);
108 return -1;
109 }
110 return ent;
111}
112
113
114/*
115 * Open the specified file read-only. We memory-map the entire thing and
116 * close the file before returning.
117 */
118status_t ZipFileRO::open(const char* zipFileName)
119{
120 int fd = -1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800121
Kenny Rootd4066a42010-04-22 18:28:29 -0700122 assert(mDirectoryMap == NULL);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800123
124 /*
125 * Open and map the specified file.
126 */
127 fd = ::open(zipFileName, O_RDONLY);
128 if (fd < 0) {
129 LOGW("Unable to open zip '%s': %s\n", zipFileName, strerror(errno));
130 return NAME_NOT_FOUND;
131 }
132
Kenny Rootd4066a42010-04-22 18:28:29 -0700133 mFileLength = lseek(fd, 0, SEEK_END);
134 if (mFileLength < kEOCDLen) {
Kenny Rootdbf6f272010-10-01 18:28:28 -0700135 TEMP_FAILURE_RETRY(close(fd));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800136 return UNKNOWN_ERROR;
137 }
138
Kenny Rootd4066a42010-04-22 18:28:29 -0700139 if (mFileName != NULL) {
140 free(mFileName);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800141 }
Kenny Rootd4066a42010-04-22 18:28:29 -0700142 mFileName = strdup(zipFileName);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800143
144 mFd = fd;
145
146 /*
Kenny Rootd4066a42010-04-22 18:28:29 -0700147 * Find the Central Directory and store its size and number of entries.
148 */
149 if (!mapCentralDirectory()) {
150 goto bail;
151 }
152
153 /*
154 * Verify Central Directory and create data structures for fast access.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800155 */
156 if (!parseZipArchive()) {
Kenny Rootd4066a42010-04-22 18:28:29 -0700157 goto bail;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800158 }
159
160 return OK;
Kenny Rootd4066a42010-04-22 18:28:29 -0700161
162bail:
163 free(mFileName);
164 mFileName = NULL;
Kenny Rootdbf6f272010-10-01 18:28:28 -0700165 TEMP_FAILURE_RETRY(close(fd));
Kenny Rootd4066a42010-04-22 18:28:29 -0700166 return UNKNOWN_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800167}
168
169/*
170 * Parse the Zip archive, verifying its contents and initializing internal
171 * data structures.
172 */
Kenny Rootd4066a42010-04-22 18:28:29 -0700173bool ZipFileRO::mapCentralDirectory(void)
174{
175 size_t readAmount = kMaxEOCDSearch;
176 if (readAmount > (size_t) mFileLength)
177 readAmount = mFileLength;
178
179 unsigned char* scanBuf = (unsigned char*) malloc(readAmount);
180 if (scanBuf == NULL) {
181 LOGW("couldn't allocate scanBuf: %s", strerror(errno));
182 free(scanBuf);
183 return false;
184 }
185
186 /*
187 * Make sure this is a Zip archive.
188 */
189 if (lseek(mFd, 0, SEEK_SET) != 0) {
190 LOGW("seek to start failed: %s", strerror(errno));
191 free(scanBuf);
192 return false;
193 }
194
195 ssize_t actual = TEMP_FAILURE_RETRY(read(mFd, scanBuf, sizeof(int32_t)));
196 if (actual != (ssize_t) sizeof(int32_t)) {
197 LOGI("couldn't read first signature from zip archive: %s", strerror(errno));
198 free(scanBuf);
199 return false;
200 }
201
202 {
203 unsigned int header = get4LE(scanBuf);
204 if (header == kEOCDSignature) {
205 LOGI("Found Zip archive, but it looks empty\n");
206 free(scanBuf);
207 return false;
208 } else if (header != kLFHSignature) {
Kenny Rootfa989202010-09-24 07:57:37 -0700209 LOGV("Not a Zip archive (found 0x%08x)\n", header);
Kenny Rootd4066a42010-04-22 18:28:29 -0700210 free(scanBuf);
211 return false;
212 }
213 }
214
215 /*
216 * Perform the traditional EOCD snipe hunt.
217 *
218 * We're searching for the End of Central Directory magic number,
219 * which appears at the start of the EOCD block. It's followed by
220 * 18 bytes of EOCD stuff and up to 64KB of archive comment. We
221 * need to read the last part of the file into a buffer, dig through
222 * it to find the magic number, parse some values out, and use those
223 * to determine the extent of the CD.
224 *
225 * We start by pulling in the last part of the file.
226 */
227 off_t searchStart = mFileLength - readAmount;
228
229 if (lseek(mFd, searchStart, SEEK_SET) != searchStart) {
230 LOGW("seek %ld failed: %s\n", (long) searchStart, strerror(errno));
231 free(scanBuf);
232 return false;
233 }
234 actual = TEMP_FAILURE_RETRY(read(mFd, scanBuf, readAmount));
235 if (actual != (ssize_t) readAmount) {
236 LOGW("Zip: read %zd failed: %s\n", readAmount, strerror(errno));
237 free(scanBuf);
238 return false;
239 }
240
241 /*
242 * Scan backward for the EOCD magic. In an archive without a trailing
243 * comment, we'll find it on the first try. (We may want to consider
244 * doing an initial minimal read; if we don't find it, retry with a
245 * second read as above.)
246 */
247 int i;
248 for (i = readAmount - kEOCDLen; i >= 0; i--) {
249 if (scanBuf[i] == 0x50 && get4LE(&scanBuf[i]) == kEOCDSignature) {
250 LOGV("+++ Found EOCD at buf+%d\n", i);
251 break;
252 }
253 }
254 if (i < 0) {
255 LOGD("Zip: EOCD not found, %s is not zip\n", mFileName);
256 free(scanBuf);
257 return false;
258 }
259
260 off_t eocdOffset = searchStart + i;
261 const unsigned char* eocdPtr = scanBuf + i;
262
263 assert(eocdOffset < mFileLength);
264
265 /*
266 * Grab the CD offset and size, and the number of entries in the
Kenny Root8f20e5e2010-08-04 16:30:40 -0700267 * archive. After that, we can release our EOCD hunt buffer.
Kenny Rootd4066a42010-04-22 18:28:29 -0700268 */
269 unsigned int numEntries = get2LE(eocdPtr + kEOCDNumEntries);
270 unsigned int dirSize = get4LE(eocdPtr + kEOCDSize);
271 unsigned int dirOffset = get4LE(eocdPtr + kEOCDFileOffset);
Kenny Root8f20e5e2010-08-04 16:30:40 -0700272 free(scanBuf);
Kenny Rootd4066a42010-04-22 18:28:29 -0700273
Kenny Root8f20e5e2010-08-04 16:30:40 -0700274 // Verify that they look reasonable.
Kenny Rootd4066a42010-04-22 18:28:29 -0700275 if ((long long) dirOffset + (long long) dirSize > (long long) eocdOffset) {
276 LOGW("bad offsets (dir %ld, size %u, eocd %ld)\n",
277 (long) dirOffset, dirSize, (long) eocdOffset);
Kenny Rootd4066a42010-04-22 18:28:29 -0700278 return false;
279 }
280 if (numEntries == 0) {
281 LOGW("empty archive?\n");
Kenny Rootd4066a42010-04-22 18:28:29 -0700282 return false;
283 }
284
285 LOGV("+++ numEntries=%d dirSize=%d dirOffset=%d\n",
286 numEntries, dirSize, dirOffset);
287
288 mDirectoryMap = new FileMap();
289 if (mDirectoryMap == NULL) {
290 LOGW("Unable to create directory map: %s", strerror(errno));
Kenny Rootd4066a42010-04-22 18:28:29 -0700291 return false;
292 }
293
294 if (!mDirectoryMap->create(mFileName, mFd, dirOffset, dirSize, true)) {
295 LOGW("Unable to map '%s' (%zd to %zd): %s\n", mFileName,
296 dirOffset, dirOffset + dirSize, strerror(errno));
Kenny Rootd4066a42010-04-22 18:28:29 -0700297 return false;
298 }
299
300 mNumEntries = numEntries;
301 mDirectoryOffset = dirOffset;
302
303 return true;
304}
305
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800306bool ZipFileRO::parseZipArchive(void)
307{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800308 bool result = false;
Kenny Rootd4066a42010-04-22 18:28:29 -0700309 const unsigned char* cdPtr = (const unsigned char*) mDirectoryMap->getDataPtr();
310 size_t cdLength = mDirectoryMap->getDataLength();
311 int numEntries = mNumEntries;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800312
313 /*
314 * Create hash table. We have a minimum 75% load factor, possibly as
315 * low as 50% after we round off to a power of 2.
316 */
Kenny Rootd4066a42010-04-22 18:28:29 -0700317 mHashTableSize = roundUpPower2(1 + (numEntries * 4) / 3);
318 mHashTable = (HashEntry*) calloc(mHashTableSize, sizeof(HashEntry));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800319
320 /*
321 * Walk through the central directory, adding entries to the hash
322 * table.
323 */
Kenny Rootd4066a42010-04-22 18:28:29 -0700324 const unsigned char* ptr = cdPtr;
325 for (int i = 0; i < numEntries; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800326 if (get4LE(ptr) != kCDESignature) {
327 LOGW("Missed a central dir sig (at %d)\n", i);
328 goto bail;
329 }
Kenny Rootd4066a42010-04-22 18:28:29 -0700330 if (ptr + kCDELen > cdPtr + cdLength) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800331 LOGW("Ran off the end (at %d)\n", i);
332 goto bail;
333 }
334
Kenny Rootd4066a42010-04-22 18:28:29 -0700335 long localHdrOffset = (long) get4LE(ptr + kCDELocalOffset);
336 if (localHdrOffset >= mDirectoryOffset) {
337 LOGW("bad LFH offset %ld at entry %d\n", localHdrOffset, i);
338 goto bail;
339 }
340
341 unsigned int fileNameLen, extraLen, commentLen, hash;
342
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800343 fileNameLen = get2LE(ptr + kCDENameLen);
344 extraLen = get2LE(ptr + kCDEExtraLen);
345 commentLen = get2LE(ptr + kCDECommentLen);
346
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800347 /* add the CDE filename to the hash table */
348 hash = computeHash((const char*)ptr + kCDELen, fileNameLen);
349 addToHash((const char*)ptr + kCDELen, fileNameLen, hash);
350
Kenny Rootd4066a42010-04-22 18:28:29 -0700351 ptr += kCDELen + fileNameLen + extraLen + commentLen;
352 if ((size_t)(ptr - cdPtr) > cdLength) {
353 LOGW("bad CD advance (%d vs %zd) at entry %d\n",
354 (int) (ptr - cdPtr), cdLength, i);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800355 goto bail;
356 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800357 }
Kenny Rootd4066a42010-04-22 18:28:29 -0700358 LOGV("+++ zip good scan %d entries\n", numEntries);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800359 result = true;
360
361bail:
362 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800363}
364
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800365/*
366 * Simple string hash function for non-null-terminated strings.
367 */
368/*static*/ unsigned int ZipFileRO::computeHash(const char* str, int len)
369{
370 unsigned int hash = 0;
371
372 while (len--)
373 hash = hash * 31 + *str++;
374
375 return hash;
376}
377
378/*
379 * Add a new entry to the hash table.
380 */
381void ZipFileRO::addToHash(const char* str, int strLen, unsigned int hash)
382{
383 int ent = hash & (mHashTableSize-1);
384
385 /*
386 * We over-allocate the table, so we're guaranteed to find an empty slot.
387 */
388 while (mHashTable[ent].name != NULL)
389 ent = (ent + 1) & (mHashTableSize-1);
390
391 mHashTable[ent].name = str;
392 mHashTable[ent].nameLen = strLen;
393}
394
395/*
396 * Find a matching entry.
397 *
398 * Returns 0 if not found.
399 */
400ZipEntryRO ZipFileRO::findEntryByName(const char* fileName) const
401{
402 int nameLen = strlen(fileName);
403 unsigned int hash = computeHash(fileName, nameLen);
404 int ent = hash & (mHashTableSize-1);
405
406 while (mHashTable[ent].name != NULL) {
407 if (mHashTable[ent].nameLen == nameLen &&
408 memcmp(mHashTable[ent].name, fileName, nameLen) == 0)
409 {
410 /* match */
Kenny Rootd4066a42010-04-22 18:28:29 -0700411 return (ZipEntryRO)(long)(ent + kZipEntryAdj);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800412 }
413
414 ent = (ent + 1) & (mHashTableSize-1);
415 }
416
417 return NULL;
418}
419
420/*
421 * Find the Nth entry.
422 *
423 * This currently involves walking through the sparse hash table, counting
424 * non-empty entries. If we need to speed this up we can either allocate
425 * a parallel lookup table or (perhaps better) provide an iterator interface.
426 */
427ZipEntryRO ZipFileRO::findEntryByIndex(int idx) const
428{
429 if (idx < 0 || idx >= mNumEntries) {
430 LOGW("Invalid index %d\n", idx);
431 return NULL;
432 }
433
434 for (int ent = 0; ent < mHashTableSize; ent++) {
435 if (mHashTable[ent].name != NULL) {
436 if (idx-- == 0)
437 return (ZipEntryRO) (ent + kZipEntryAdj);
438 }
439 }
440
441 return NULL;
442}
443
444/*
445 * Get the useful fields from the zip entry.
446 *
447 * Returns "false" if the offsets to the fields or the contents of the fields
448 * appear to be bogus.
449 */
Kenny Rootd4066a42010-04-22 18:28:29 -0700450bool ZipFileRO::getEntryInfo(ZipEntryRO entry, int* pMethod, size_t* pUncompLen,
451 size_t* pCompLen, off_t* pOffset, long* pModWhen, long* pCrc32) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800452{
Kenny Rootd4066a42010-04-22 18:28:29 -0700453 bool ret = false;
454
455 const int ent = entryToIndex(entry);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800456 if (ent < 0)
457 return false;
458
Kenny Rootd4066a42010-04-22 18:28:29 -0700459 HashEntry hashEntry = mHashTable[ent];
460
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800461 /*
462 * Recover the start of the central directory entry from the filename
Kenny Rootd4066a42010-04-22 18:28:29 -0700463 * pointer. The filename is the first entry past the fixed-size data,
464 * so we can just subtract back from that.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800465 */
Kenny Rootd4066a42010-04-22 18:28:29 -0700466 const unsigned char* ptr = (const unsigned char*) hashEntry.name;
467 off_t cdOffset = mDirectoryOffset;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800468
469 ptr -= kCDELen;
470
471 int method = get2LE(ptr + kCDEMethod);
472 if (pMethod != NULL)
473 *pMethod = method;
474
475 if (pModWhen != NULL)
476 *pModWhen = get4LE(ptr + kCDEModWhen);
477 if (pCrc32 != NULL)
478 *pCrc32 = get4LE(ptr + kCDECRC);
479
Kenny Rootd4066a42010-04-22 18:28:29 -0700480 size_t compLen = get4LE(ptr + kCDECompLen);
481 if (pCompLen != NULL)
482 *pCompLen = compLen;
483 size_t uncompLen = get4LE(ptr + kCDEUncompLen);
484 if (pUncompLen != NULL)
485 *pUncompLen = uncompLen;
486
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800487 /*
Kenny Rootd4066a42010-04-22 18:28:29 -0700488 * If requested, determine the offset of the start of the data. All we
489 * have is the offset to the Local File Header, which is variable size,
490 * so we have to read the contents of the struct to figure out where
491 * the actual data starts.
492 *
493 * We also need to make sure that the lengths are not so large that
494 * somebody trying to map the compressed or uncompressed data runs
495 * off the end of the mapped region.
496 *
497 * Note we don't verify compLen/uncompLen if they don't request the
498 * dataOffset, because dataOffset is expensive to determine. However,
499 * if they don't have the file offset, they're not likely to be doing
500 * anything with the contents.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800501 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800502 if (pOffset != NULL) {
Kenny Rootd4066a42010-04-22 18:28:29 -0700503 long localHdrOffset = get4LE(ptr + kCDELocalOffset);
504 if (localHdrOffset + kLFHLen >= cdOffset) {
505 LOGE("ERROR: bad local hdr offset in zip\n");
506 return false;
507 }
508
509 unsigned char lfhBuf[kLFHLen];
Kenny Rootfa989202010-09-24 07:57:37 -0700510
511 {
512 AutoMutex _l(mFdLock);
513
514 if (lseek(mFd, localHdrOffset, SEEK_SET) != localHdrOffset) {
515 LOGW("failed seeking to lfh at offset %ld\n", localHdrOffset);
516 return false;
517 }
518
519 ssize_t actual =
520 TEMP_FAILURE_RETRY(read(mFd, lfhBuf, sizeof(lfhBuf)));
521 if (actual != sizeof(lfhBuf)) {
522 LOGW("failed reading lfh from offset %ld\n", localHdrOffset);
523 return false;
524 }
Kenny Rootd4066a42010-04-22 18:28:29 -0700525
Kenny Rootdbf6f272010-10-01 18:28:28 -0700526 if (get4LE(lfhBuf) != kLFHSignature) {
527 off_t actualOffset = lseek(mFd, 0, SEEK_CUR);
528 LOGW("didn't find signature at start of lfh; wanted: offset=%ld data=0x%08x; "
529 "got: offset=%zd data=0x%08lx\n",
530 localHdrOffset, kLFHSignature, (size_t)actualOffset, get4LE(lfhBuf));
531 return false;
532 }
Kenny Rootd4066a42010-04-22 18:28:29 -0700533 }
534
535 off_t dataOffset = localHdrOffset + kLFHLen
536 + get2LE(lfhBuf + kLFHNameLen) + get2LE(lfhBuf + kLFHExtraLen);
537 if (dataOffset >= cdOffset) {
538 LOGW("bad data offset %ld in zip\n", (long) dataOffset);
539 return false;
540 }
541
542 /* check lengths */
543 if ((off_t)(dataOffset + compLen) > cdOffset) {
544 LOGW("bad compressed length in zip (%ld + %zd > %ld)\n",
545 (long) dataOffset, compLen, (long) cdOffset);
546 return false;
547 }
548
549 if (method == kCompressStored &&
550 (off_t)(dataOffset + uncompLen) > cdOffset)
551 {
552 LOGE("ERROR: bad uncompressed length in zip (%ld + %zd > %ld)\n",
553 (long) dataOffset, uncompLen, (long) cdOffset);
554 return false;
555 }
556
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800557 *pOffset = dataOffset;
558 }
Kenny Rootd4066a42010-04-22 18:28:29 -0700559
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800560 return true;
561}
562
563/*
564 * Copy the entry's filename to the buffer.
565 */
566int ZipFileRO::getEntryFileName(ZipEntryRO entry, char* buffer, int bufLen)
567 const
568{
569 int ent = entryToIndex(entry);
570 if (ent < 0)
571 return -1;
572
573 int nameLen = mHashTable[ent].nameLen;
574 if (bufLen < nameLen+1)
575 return nameLen+1;
576
577 memcpy(buffer, mHashTable[ent].name, nameLen);
578 buffer[nameLen] = '\0';
579 return 0;
580}
581
582/*
583 * Create a new FileMap object that spans the data in "entry".
584 */
585FileMap* ZipFileRO::createEntryFileMap(ZipEntryRO entry) const
586{
587 /*
588 * TODO: the efficient way to do this is to modify FileMap to allow
589 * sub-regions of a file to be mapped. A reference-counting scheme
590 * can manage the base memory mapping. For now, we just create a brand
591 * new mapping off of the Zip archive file descriptor.
592 */
593
594 FileMap* newMap;
Kenny Rootd4066a42010-04-22 18:28:29 -0700595 size_t compLen;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800596 off_t offset;
597
598 if (!getEntryInfo(entry, NULL, NULL, &compLen, &offset, NULL, NULL))
599 return NULL;
600
601 newMap = new FileMap();
Kenny Rootd4066a42010-04-22 18:28:29 -0700602 if (!newMap->create(mFileName, mFd, offset, compLen, true)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800603 newMap->release();
604 return NULL;
605 }
606
607 return newMap;
608}
609
610/*
611 * Uncompress an entry, in its entirety, into the provided output buffer.
612 *
613 * This doesn't verify the data's CRC, which might be useful for
614 * uncompressed data. The caller should be able to manage it.
615 */
616bool ZipFileRO::uncompressEntry(ZipEntryRO entry, void* buffer) const
617{
Kenny Rootd4066a42010-04-22 18:28:29 -0700618 const size_t kSequentialMin = 32768;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800619 bool result = false;
620 int ent = entryToIndex(entry);
621 if (ent < 0)
622 return -1;
623
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800624 int method;
Kenny Rootd4066a42010-04-22 18:28:29 -0700625 size_t uncompLen, compLen;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800626 off_t offset;
Kenny Rootd4066a42010-04-22 18:28:29 -0700627 const unsigned char* ptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800628
629 getEntryInfo(entry, &method, &uncompLen, &compLen, &offset, NULL, NULL);
630
Kenny Rootd4066a42010-04-22 18:28:29 -0700631 FileMap* file = createEntryFileMap(entry);
632 if (file == NULL) {
633 goto bail;
634 }
635
636 ptr = (const unsigned char*) file->getDataPtr();
637
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800638 /*
639 * Experiment with madvise hint. When we want to uncompress a file,
640 * we pull some stuff out of the central dir entry and then hit a
641 * bunch of compressed or uncompressed data sequentially. The CDE
642 * visit will cause a limited amount of read-ahead because it's at
643 * the end of the file. We could end up doing lots of extra disk
644 * access if the file we're prying open is small. Bottom line is we
645 * probably don't want to turn MADV_SEQUENTIAL on and leave it on.
646 *
647 * So, if the compressed size of the file is above a certain minimum
648 * size, temporarily boost the read-ahead in the hope that the extra
649 * pair of system calls are negated by a reduction in page faults.
650 */
651 if (compLen > kSequentialMin)
Kenny Rootd4066a42010-04-22 18:28:29 -0700652 file->advise(FileMap::SEQUENTIAL);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800653
654 if (method == kCompressStored) {
Kenny Rootd4066a42010-04-22 18:28:29 -0700655 memcpy(buffer, ptr, uncompLen);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800656 } else {
Kenny Rootd4066a42010-04-22 18:28:29 -0700657 if (!inflateBuffer(buffer, ptr, uncompLen, compLen))
Kenny Rootb47eafa2010-09-24 09:11:28 -0700658 goto unmap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800659 }
660
661 if (compLen > kSequentialMin)
Kenny Rootd4066a42010-04-22 18:28:29 -0700662 file->advise(FileMap::NORMAL);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800663
664 result = true;
665
Kenny Rootb47eafa2010-09-24 09:11:28 -0700666unmap:
667 file->release();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800668bail:
669 return result;
670}
671
672/*
673 * Uncompress an entry, in its entirety, to an open file descriptor.
674 *
675 * This doesn't verify the data's CRC, but probably should.
676 */
677bool ZipFileRO::uncompressEntry(ZipEntryRO entry, int fd) const
678{
679 bool result = false;
680 int ent = entryToIndex(entry);
681 if (ent < 0)
682 return -1;
683
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800684 int method;
Kenny Rootd4066a42010-04-22 18:28:29 -0700685 size_t uncompLen, compLen;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800686 off_t offset;
Kenny Rootd4066a42010-04-22 18:28:29 -0700687 const unsigned char* ptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800688
689 getEntryInfo(entry, &method, &uncompLen, &compLen, &offset, NULL, NULL);
690
Kenny Rootb47eafa2010-09-24 09:11:28 -0700691 FileMap* file = createEntryFileMap(entry);
Kenny Rootd4066a42010-04-22 18:28:29 -0700692 if (file == NULL) {
693 goto bail;
694 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800695
Kenny Rootd4066a42010-04-22 18:28:29 -0700696 ptr = (const unsigned char*) file->getDataPtr();
697
698 if (method == kCompressStored) {
699 ssize_t actual = write(fd, ptr, uncompLen);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800700 if (actual < 0) {
701 LOGE("Write failed: %s\n", strerror(errno));
Kenny Rootb47eafa2010-09-24 09:11:28 -0700702 goto unmap;
Kenny Rootd4066a42010-04-22 18:28:29 -0700703 } else if ((size_t) actual != uncompLen) {
704 LOGE("Partial write during uncompress (%zd of %zd)\n",
Kenny Root8f20e5e2010-08-04 16:30:40 -0700705 (size_t)actual, (size_t)uncompLen);
Kenny Rootb47eafa2010-09-24 09:11:28 -0700706 goto unmap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800707 } else {
708 LOGI("+++ successful write\n");
709 }
710 } else {
Kenny Rootd4066a42010-04-22 18:28:29 -0700711 if (!inflateBuffer(fd, ptr, uncompLen, compLen))
Kenny Rootb47eafa2010-09-24 09:11:28 -0700712 goto unmap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800713 }
714
715 result = true;
716
Kenny Rootb47eafa2010-09-24 09:11:28 -0700717unmap:
718 file->release();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800719bail:
720 return result;
721}
722
723/*
724 * Uncompress "deflate" data from one buffer to another.
725 */
726/*static*/ bool ZipFileRO::inflateBuffer(void* outBuf, const void* inBuf,
Kenny Rootd4066a42010-04-22 18:28:29 -0700727 size_t uncompLen, size_t compLen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800728{
729 bool result = false;
730 z_stream zstream;
731 int zerr;
732
733 /*
734 * Initialize the zlib stream struct.
735 */
Kenny Rootd4066a42010-04-22 18:28:29 -0700736 memset(&zstream, 0, sizeof(zstream));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800737 zstream.zalloc = Z_NULL;
738 zstream.zfree = Z_NULL;
739 zstream.opaque = Z_NULL;
740 zstream.next_in = (Bytef*)inBuf;
741 zstream.avail_in = compLen;
742 zstream.next_out = (Bytef*) outBuf;
743 zstream.avail_out = uncompLen;
744 zstream.data_type = Z_UNKNOWN;
745
Kenny Rootd4066a42010-04-22 18:28:29 -0700746 /*
747 * Use the undocumented "negative window bits" feature to tell zlib
748 * that there's no zlib header waiting for it.
749 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800750 zerr = inflateInit2(&zstream, -MAX_WBITS);
751 if (zerr != Z_OK) {
752 if (zerr == Z_VERSION_ERROR) {
753 LOGE("Installed zlib is not compatible with linked version (%s)\n",
754 ZLIB_VERSION);
755 } else {
756 LOGE("Call to inflateInit2 failed (zerr=%d)\n", zerr);
757 }
758 goto bail;
759 }
760
761 /*
762 * Expand data.
763 */
764 zerr = inflate(&zstream, Z_FINISH);
765 if (zerr != Z_STREAM_END) {
766 LOGW("Zip inflate failed, zerr=%d (nIn=%p aIn=%u nOut=%p aOut=%u)\n",
767 zerr, zstream.next_in, zstream.avail_in,
768 zstream.next_out, zstream.avail_out);
769 goto z_bail;
770 }
771
772 /* paranoia */
Kenny Rootd4066a42010-04-22 18:28:29 -0700773 if (zstream.total_out != uncompLen) {
774 LOGW("Size mismatch on inflated file (%ld vs %zd)\n",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800775 zstream.total_out, uncompLen);
776 goto z_bail;
777 }
778
779 result = true;
780
781z_bail:
782 inflateEnd(&zstream); /* free up any allocated structures */
783
784bail:
785 return result;
786}
787
788/*
789 * Uncompress "deflate" data from one buffer to an open file descriptor.
790 */
791/*static*/ bool ZipFileRO::inflateBuffer(int fd, const void* inBuf,
Kenny Rootd4066a42010-04-22 18:28:29 -0700792 size_t uncompLen, size_t compLen)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800793{
794 bool result = false;
Kenny Rootd4066a42010-04-22 18:28:29 -0700795 const size_t kWriteBufSize = 32768;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800796 unsigned char writeBuf[kWriteBufSize];
797 z_stream zstream;
798 int zerr;
799
800 /*
801 * Initialize the zlib stream struct.
802 */
Kenny Rootd4066a42010-04-22 18:28:29 -0700803 memset(&zstream, 0, sizeof(zstream));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800804 zstream.zalloc = Z_NULL;
805 zstream.zfree = Z_NULL;
806 zstream.opaque = Z_NULL;
807 zstream.next_in = (Bytef*)inBuf;
808 zstream.avail_in = compLen;
809 zstream.next_out = (Bytef*) writeBuf;
810 zstream.avail_out = sizeof(writeBuf);
811 zstream.data_type = Z_UNKNOWN;
812
Kenny Rootd4066a42010-04-22 18:28:29 -0700813 /*
814 * Use the undocumented "negative window bits" feature to tell zlib
815 * that there's no zlib header waiting for it.
816 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800817 zerr = inflateInit2(&zstream, -MAX_WBITS);
818 if (zerr != Z_OK) {
819 if (zerr == Z_VERSION_ERROR) {
820 LOGE("Installed zlib is not compatible with linked version (%s)\n",
821 ZLIB_VERSION);
822 } else {
823 LOGE("Call to inflateInit2 failed (zerr=%d)\n", zerr);
824 }
825 goto bail;
826 }
827
828 /*
829 * Loop while we have more to do.
830 */
831 do {
832 /*
833 * Expand data.
834 */
835 zerr = inflate(&zstream, Z_NO_FLUSH);
836 if (zerr != Z_OK && zerr != Z_STREAM_END) {
837 LOGW("zlib inflate: zerr=%d (nIn=%p aIn=%u nOut=%p aOut=%u)\n",
838 zerr, zstream.next_in, zstream.avail_in,
839 zstream.next_out, zstream.avail_out);
840 goto z_bail;
841 }
842
843 /* write when we're full or when we're done */
844 if (zstream.avail_out == 0 ||
845 (zerr == Z_STREAM_END && zstream.avail_out != sizeof(writeBuf)))
846 {
847 long writeSize = zstream.next_out - writeBuf;
848 int cc = write(fd, writeBuf, writeSize);
849 if (cc != (int) writeSize) {
850 LOGW("write failed in inflate (%d vs %ld)\n", cc, writeSize);
851 goto z_bail;
852 }
853
854 zstream.next_out = writeBuf;
855 zstream.avail_out = sizeof(writeBuf);
856 }
857 } while (zerr == Z_OK);
858
859 assert(zerr == Z_STREAM_END); /* other errors should've been caught */
860
861 /* paranoia */
Kenny Rootd4066a42010-04-22 18:28:29 -0700862 if (zstream.total_out != uncompLen) {
863 LOGW("Size mismatch on inflated file (%ld vs %zd)\n",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800864 zstream.total_out, uncompLen);
865 goto z_bail;
866 }
867
868 result = true;
869
870z_bail:
871 inflateEnd(&zstream); /* free up any allocated structures */
872
873bail:
874 return result;
875}