am e8621534: Merge "ZipUtilsRO rewrite based on Dalvik Zip rewrite" into kraken

This commit is contained in:
Kenny Root
2010-05-12 14:55:56 -07:00
committed by Android Git Automerger
5 changed files with 350 additions and 203 deletions

View File

@@ -47,8 +47,8 @@ extern ZipEntryCRO ZipFileCRO_findEntryByName(ZipFileCRO zip,
const char* fileName); const char* fileName);
extern bool ZipFileCRO_getEntryInfo(ZipFileCRO zip, ZipEntryCRO entry, extern bool ZipFileCRO_getEntryInfo(ZipFileCRO zip, ZipEntryCRO entry,
int* pMethod, long* pUncompLen, int* pMethod, size_t* pUncompLen,
long* pCompLen, off_t* pOffset, long* pModWhen, long* pCrc32); size_t* pCompLen, off_t* pOffset, long* pModWhen, long* pCrc32);
extern bool ZipFileCRO_uncompressEntry(ZipFileCRO zip, ZipEntryCRO entry, int fd); extern bool ZipFileCRO_uncompressEntry(ZipFileCRO zip, ZipEntryCRO entry, int fd);

View File

@@ -58,14 +58,19 @@ typedef void* ZipEntryRO;
class ZipFileRO { class ZipFileRO {
public: public:
ZipFileRO() ZipFileRO()
: mFd(-1), mFileMap(NULL), mHashTableSize(-1), mHashTable(NULL) : mFd(-1), mFileName(NULL), mFileLength(-1),
mDirectoryMap(NULL),
mNumEntries(-1), mDirectoryOffset(-1),
mHashTableSize(-1), mHashTable(NULL)
{} {}
~ZipFileRO() { ~ZipFileRO() {
free(mHashTable); free(mHashTable);
if (mFileMap) if (mDirectoryMap)
mFileMap->release(); mDirectoryMap->release();
if (mFd >= 0) if (mFd >= 0)
close(mFd); close(mFd);
if (mFileName)
free(mFileName);
} }
/* /*
@@ -118,8 +123,8 @@ public:
* Returns "false" if "entry" is bogus or if the data in the Zip file * Returns "false" if "entry" is bogus or if the data in the Zip file
* appears to be bad. * appears to be bad.
*/ */
bool getEntryInfo(ZipEntryRO entry, int* pMethod, long* pUncompLen, bool getEntryInfo(ZipEntryRO entry, int* pMethod, size_t* pUncompLen,
long* pCompLen, off_t* pOffset, long* pModWhen, long* pCrc32) const; size_t* pCompLen, off_t* pOffset, long* pModWhen, long* pCrc32) const;
/* /*
* Create a new FileMap object that maps a subset of the archive. For * Create a new FileMap object that maps a subset of the archive. For
@@ -155,13 +160,13 @@ public:
* Utility function: uncompress deflated data, buffer to buffer. * Utility function: uncompress deflated data, buffer to buffer.
*/ */
static bool inflateBuffer(void* outBuf, const void* inBuf, static bool inflateBuffer(void* outBuf, const void* inBuf,
long uncompLen, long compLen); size_t uncompLen, size_t compLen);
/* /*
* Utility function: uncompress deflated data, buffer to fd. * Utility function: uncompress deflated data, buffer to fd.
*/ */
static bool inflateBuffer(int fd, const void* inBuf, static bool inflateBuffer(int fd, const void* inBuf,
long uncompLen, long compLen); size_t uncompLen, size_t compLen);
/* /*
* Some basic functions for raw data manipulation. "LE" means * Some basic functions for raw data manipulation. "LE" means
@@ -179,6 +184,9 @@ private:
ZipFileRO(const ZipFileRO& src); ZipFileRO(const ZipFileRO& src);
ZipFileRO& operator=(const ZipFileRO& src); ZipFileRO& operator=(const ZipFileRO& src);
/* locate and parse the central directory */
bool mapCentralDirectory(void);
/* parse the archive, prepping internal structures */ /* parse the archive, prepping internal structures */
bool parseZipArchive(void); bool parseZipArchive(void);
@@ -203,12 +211,21 @@ private:
/* open Zip archive */ /* open Zip archive */
int mFd; int mFd;
/* zip file name */
char* mFileName;
/* length of file */
size_t mFileLength;
/* mapped file */ /* mapped file */
FileMap* mFileMap; FileMap* mDirectoryMap;
/* number of entries in the Zip archive */ /* number of entries in the Zip archive */
int mNumEntries; int mNumEntries;
/* CD directory offset in the Zip archive */
off_t mDirectoryOffset;
/* /*
* We know how many entries are in the Zip archive, so we have a * We know how many entries are in the Zip archive, so we have a
* fixed-size hash table. We probe for an empty slot. * fixed-size hash table. We probe for an empty slot.

View File

@@ -824,7 +824,7 @@ Asset* AssetManager::openAssetFromZipLocked(const ZipFileRO* pZipFile,
// TODO: look for previously-created shared memory slice? // TODO: look for previously-created shared memory slice?
int method; int method;
long uncompressedLen; size_t uncompressedLen;
//printf("USING Zip '%s'\n", pEntry->getFileName()); //printf("USING Zip '%s'\n", pEntry->getFileName());

View File

@@ -39,8 +39,8 @@ ZipEntryCRO ZipFileCRO_findEntryByName(ZipFileCRO zipToken,
} }
bool ZipFileCRO_getEntryInfo(ZipFileCRO zipToken, ZipEntryRO entryToken, bool ZipFileCRO_getEntryInfo(ZipFileCRO zipToken, ZipEntryRO entryToken,
int* pMethod, long* pUncompLen, int* pMethod, size_t* pUncompLen,
long* pCompLen, off_t* pOffset, long* pModWhen, long* pCrc32) { size_t* pCompLen, off_t* pOffset, long* pModWhen, long* pCrc32) {
ZipFileRO* zip = (ZipFileRO*)zipToken; ZipFileRO* zip = (ZipFileRO*)zipToken;
ZipEntryRO entry = (ZipEntryRO)entryToken; ZipEntryRO entry = (ZipEntryRO)entryToken;
return zip->getEntryInfo(entry, pMethod, pUncompLen, pCompLen, pOffset, return zip->getEntryInfo(entry, pMethod, pUncompLen, pCompLen, pOffset,

View File

@@ -29,6 +29,22 @@
#include <fcntl.h> #include <fcntl.h>
#include <errno.h> #include <errno.h>
#include <assert.h> #include <assert.h>
#include <unistd.h>
/*
* TEMP_FAILURE_RETRY is defined by some, but not all, versions of
* <unistd.h>. (Alas, it is not as standard as we'd hoped!) So, if it's
* not already defined, then define it here.
*/
#ifndef TEMP_FAILURE_RETRY
/* Used to retry syscalls that can return EINTR. */
#define TEMP_FAILURE_RETRY(exp) ({ \
typeof (exp) _rc; \
do { \
_rc = (exp); \
} while (_rc == -1 && errno == EINTR); \
_rc; })
#endif
using namespace android; using namespace android;
@@ -38,6 +54,7 @@ using namespace android;
#define kEOCDSignature 0x06054b50 #define kEOCDSignature 0x06054b50
#define kEOCDLen 22 #define kEOCDLen 22
#define kEOCDNumEntries 8 // offset to #of entries in file #define kEOCDNumEntries 8 // offset to #of entries in file
#define kEOCDSize 12 // size of the central directory
#define kEOCDFileOffset 16 // offset to central directory #define kEOCDFileOffset 16 // offset to central directory
#define kMaxCommentLen 65535 // longest possible in ushort #define kMaxCommentLen 65535 // longest possible in ushort
@@ -90,9 +107,8 @@ int ZipFileRO::entryToIndex(const ZipEntryRO entry) const
status_t ZipFileRO::open(const char* zipFileName) status_t ZipFileRO::open(const char* zipFileName)
{ {
int fd = -1; int fd = -1;
off_t length;
assert(mFileMap == NULL); assert(mDirectoryMap == NULL);
/* /*
* Open and map the specified file. * Open and map the specified file.
@@ -103,172 +119,240 @@ status_t ZipFileRO::open(const char* zipFileName)
return NAME_NOT_FOUND; return NAME_NOT_FOUND;
} }
length = lseek(fd, 0, SEEK_END); mFileLength = lseek(fd, 0, SEEK_END);
if (length < 0) { if (mFileLength < kEOCDLen) {
close(fd); close(fd);
return UNKNOWN_ERROR; return UNKNOWN_ERROR;
} }
mFileMap = new FileMap(); if (mFileName != NULL) {
if (mFileMap == NULL) { free(mFileName);
close(fd);
return NO_MEMORY;
}
if (!mFileMap->create(zipFileName, fd, 0, length, true)) {
LOGW("Unable to map '%s': %s\n", zipFileName, strerror(errno));
close(fd);
return UNKNOWN_ERROR;
} }
mFileName = strdup(zipFileName);
mFd = fd; mFd = fd;
/* /*
* Got it mapped, verify it and create data structures for fast access. * Find the Central Directory and store its size and number of entries.
*/
if (!mapCentralDirectory()) {
goto bail;
}
/*
* Verify Central Directory and create data structures for fast access.
*/ */
if (!parseZipArchive()) { if (!parseZipArchive()) {
mFileMap->release(); goto bail;
mFileMap = NULL;
return UNKNOWN_ERROR;
} }
return OK; return OK;
bail:
free(mFileName);
mFileName = NULL;
close(fd);
return UNKNOWN_ERROR;
} }
/* /*
* Parse the Zip archive, verifying its contents and initializing internal * Parse the Zip archive, verifying its contents and initializing internal
* data structures. * data structures.
*/ */
bool ZipFileRO::mapCentralDirectory(void)
{
size_t readAmount = kMaxEOCDSearch;
if (readAmount > (size_t) mFileLength)
readAmount = mFileLength;
unsigned char* scanBuf = (unsigned char*) malloc(readAmount);
if (scanBuf == NULL) {
LOGW("couldn't allocate scanBuf: %s", strerror(errno));
free(scanBuf);
return false;
}
/*
* Make sure this is a Zip archive.
*/
if (lseek(mFd, 0, SEEK_SET) != 0) {
LOGW("seek to start failed: %s", strerror(errno));
free(scanBuf);
return false;
}
ssize_t actual = TEMP_FAILURE_RETRY(read(mFd, scanBuf, sizeof(int32_t)));
if (actual != (ssize_t) sizeof(int32_t)) {
LOGI("couldn't read first signature from zip archive: %s", strerror(errno));
free(scanBuf);
return false;
}
{
unsigned int header = get4LE(scanBuf);
if (header == kEOCDSignature) {
LOGI("Found Zip archive, but it looks empty\n");
free(scanBuf);
return false;
} else if (header != kLFHSignature) {
LOGV("Not a Zip archive (found 0x%08x)\n", val);
free(scanBuf);
return false;
}
}
/*
* Perform the traditional EOCD snipe hunt.
*
* We're searching for the End of Central Directory magic number,
* which appears at the start of the EOCD block. It's followed by
* 18 bytes of EOCD stuff and up to 64KB of archive comment. We
* need to read the last part of the file into a buffer, dig through
* it to find the magic number, parse some values out, and use those
* to determine the extent of the CD.
*
* We start by pulling in the last part of the file.
*/
off_t searchStart = mFileLength - readAmount;
if (lseek(mFd, searchStart, SEEK_SET) != searchStart) {
LOGW("seek %ld failed: %s\n", (long) searchStart, strerror(errno));
free(scanBuf);
return false;
}
actual = TEMP_FAILURE_RETRY(read(mFd, scanBuf, readAmount));
if (actual != (ssize_t) readAmount) {
LOGW("Zip: read %zd failed: %s\n", readAmount, strerror(errno));
free(scanBuf);
return false;
}
/*
* Scan backward for the EOCD magic. In an archive without a trailing
* comment, we'll find it on the first try. (We may want to consider
* doing an initial minimal read; if we don't find it, retry with a
* second read as above.)
*/
int i;
for (i = readAmount - kEOCDLen; i >= 0; i--) {
if (scanBuf[i] == 0x50 && get4LE(&scanBuf[i]) == kEOCDSignature) {
LOGV("+++ Found EOCD at buf+%d\n", i);
break;
}
}
if (i < 0) {
LOGD("Zip: EOCD not found, %s is not zip\n", mFileName);
free(scanBuf);
return false;
}
off_t eocdOffset = searchStart + i;
const unsigned char* eocdPtr = scanBuf + i;
assert(eocdOffset < mFileLength);
/*
* Grab the CD offset and size, and the number of entries in the
* archive. Verify that they look reasonable.
*/
unsigned int numEntries = get2LE(eocdPtr + kEOCDNumEntries);
unsigned int dirSize = get4LE(eocdPtr + kEOCDSize);
unsigned int dirOffset = get4LE(eocdPtr + kEOCDFileOffset);
if ((long long) dirOffset + (long long) dirSize > (long long) eocdOffset) {
LOGW("bad offsets (dir %ld, size %u, eocd %ld)\n",
(long) dirOffset, dirSize, (long) eocdOffset);
free(scanBuf);
return false;
}
if (numEntries == 0) {
LOGW("empty archive?\n");
free(scanBuf);
return false;
}
LOGV("+++ numEntries=%d dirSize=%d dirOffset=%d\n",
numEntries, dirSize, dirOffset);
mDirectoryMap = new FileMap();
if (mDirectoryMap == NULL) {
LOGW("Unable to create directory map: %s", strerror(errno));
free(scanBuf);
return false;
}
if (!mDirectoryMap->create(mFileName, mFd, dirOffset, dirSize, true)) {
LOGW("Unable to map '%s' (%zd to %zd): %s\n", mFileName,
dirOffset, dirOffset + dirSize, strerror(errno));
free(scanBuf);
return false;
}
mNumEntries = numEntries;
mDirectoryOffset = dirOffset;
return true;
}
bool ZipFileRO::parseZipArchive(void) bool ZipFileRO::parseZipArchive(void)
{ {
#define CHECK_OFFSET(_off) { \
if ((unsigned int) (_off) >= maxOffset) { \
LOGE("ERROR: bad offset %u (max %d): %s\n", \
(unsigned int) (_off), maxOffset, #_off); \
goto bail; \
} \
}
const unsigned char* basePtr = (const unsigned char*)mFileMap->getDataPtr();
const unsigned char* ptr;
size_t length = mFileMap->getDataLength();
bool result = false; bool result = false;
unsigned int i, numEntries, cdOffset; const unsigned char* cdPtr = (const unsigned char*) mDirectoryMap->getDataPtr();
unsigned int val; size_t cdLength = mDirectoryMap->getDataLength();
int numEntries = mNumEntries;
/*
* The first 4 bytes of the file will either be the local header
* signature for the first file (kLFHSignature) or, if the archive doesn't
* have any files in it, the end-of-central-directory signature
* (kEOCDSignature).
*/
val = get4LE(basePtr);
if (val == kEOCDSignature) {
LOGI("Found Zip archive, but it looks empty\n");
goto bail;
} else if (val != kLFHSignature) {
LOGV("Not a Zip archive (found 0x%08x)\n", val);
goto bail;
}
/*
* Find the EOCD. We'll find it immediately unless they have a file
* comment.
*/
ptr = basePtr + length - kEOCDLen;
while (ptr >= basePtr) {
if (*ptr == (kEOCDSignature & 0xff) && get4LE(ptr) == kEOCDSignature)
break;
ptr--;
}
if (ptr < basePtr) {
LOGI("Could not find end-of-central-directory in Zip\n");
goto bail;
}
/*
* There are two interesting items in the EOCD block: the number of
* entries in the file, and the file offset of the start of the
* central directory.
*
* (There's actually a count of the #of entries in this file, and for
* all files which comprise a spanned archive, but for our purposes
* we're only interested in the current file. Besides, we expect the
* two to be equivalent for our stuff.)
*/
numEntries = get2LE(ptr + kEOCDNumEntries);
cdOffset = get4LE(ptr + kEOCDFileOffset);
/* valid offsets are [0,EOCD] */
unsigned int maxOffset;
maxOffset = (ptr - basePtr) +1;
LOGV("+++ numEntries=%d cdOffset=%d\n", numEntries, cdOffset);
if (numEntries == 0 || cdOffset >= length) {
LOGW("Invalid entries=%d offset=%d (len=%zd)\n",
numEntries, cdOffset, length);
goto bail;
}
/* /*
* Create hash table. We have a minimum 75% load factor, possibly as * Create hash table. We have a minimum 75% load factor, possibly as
* low as 50% after we round off to a power of 2. * low as 50% after we round off to a power of 2.
*/ */
mNumEntries = numEntries; mHashTableSize = roundUpPower2(1 + (numEntries * 4) / 3);
mHashTableSize = roundUpPower2(1 + ((numEntries * 4) / 3)); mHashTable = (HashEntry*) calloc(mHashTableSize, sizeof(HashEntry));
mHashTable = (HashEntry*) calloc(1, sizeof(HashEntry) * mHashTableSize);
/* /*
* Walk through the central directory, adding entries to the hash * Walk through the central directory, adding entries to the hash
* table. * table.
*/ */
ptr = basePtr + cdOffset; const unsigned char* ptr = cdPtr;
for (i = 0; i < numEntries; i++) { for (int i = 0; i < numEntries; i++) {
unsigned int fileNameLen, extraLen, commentLen, localHdrOffset;
const unsigned char* localHdr;
unsigned int hash;
if (get4LE(ptr) != kCDESignature) { if (get4LE(ptr) != kCDESignature) {
LOGW("Missed a central dir sig (at %d)\n", i); LOGW("Missed a central dir sig (at %d)\n", i);
goto bail; goto bail;
} }
if (ptr + kCDELen > basePtr + length) { if (ptr + kCDELen > cdPtr + cdLength) {
LOGW("Ran off the end (at %d)\n", i); LOGW("Ran off the end (at %d)\n", i);
goto bail; goto bail;
} }
localHdrOffset = get4LE(ptr + kCDELocalOffset); long localHdrOffset = (long) get4LE(ptr + kCDELocalOffset);
CHECK_OFFSET(localHdrOffset); if (localHdrOffset >= mDirectoryOffset) {
LOGW("bad LFH offset %ld at entry %d\n", localHdrOffset, i);
goto bail;
}
unsigned int fileNameLen, extraLen, commentLen, hash;
fileNameLen = get2LE(ptr + kCDENameLen); fileNameLen = get2LE(ptr + kCDENameLen);
extraLen = get2LE(ptr + kCDEExtraLen); extraLen = get2LE(ptr + kCDEExtraLen);
commentLen = get2LE(ptr + kCDECommentLen); commentLen = get2LE(ptr + kCDECommentLen);
//LOGV("+++ %d: localHdr=%d fnl=%d el=%d cl=%d\n",
// i, localHdrOffset, fileNameLen, extraLen, commentLen);
//LOGV(" '%.*s'\n", fileNameLen, ptr + kCDELen);
/* add the CDE filename to the hash table */ /* add the CDE filename to the hash table */
hash = computeHash((const char*)ptr + kCDELen, fileNameLen); hash = computeHash((const char*)ptr + kCDELen, fileNameLen);
addToHash((const char*)ptr + kCDELen, fileNameLen, hash); addToHash((const char*)ptr + kCDELen, fileNameLen, hash);
localHdr = basePtr + localHdrOffset; ptr += kCDELen + fileNameLen + extraLen + commentLen;
if (get4LE(localHdr) != kLFHSignature) { if ((size_t)(ptr - cdPtr) > cdLength) {
LOGW("Bad offset to local header: %d (at %d)\n", LOGW("bad CD advance (%d vs %zd) at entry %d\n",
localHdrOffset, i); (int) (ptr - cdPtr), cdLength, i);
goto bail; goto bail;
} }
ptr += kCDELen + fileNameLen + extraLen + commentLen;
CHECK_OFFSET(ptr - basePtr);
} }
LOGV("+++ zip good scan %d entries\n", numEntries);
result = true; result = true;
bail: bail:
return result; return result;
#undef CHECK_OFFSET
} }
/* /*
* Simple string hash function for non-null-terminated strings. * Simple string hash function for non-null-terminated strings.
*/ */
@@ -315,7 +399,7 @@ ZipEntryRO ZipFileRO::findEntryByName(const char* fileName) const
memcmp(mHashTable[ent].name, fileName, nameLen) == 0) memcmp(mHashTable[ent].name, fileName, nameLen) == 0)
{ {
/* match */ /* match */
return (ZipEntryRO) (ent + kZipEntryAdj); return (ZipEntryRO)(long)(ent + kZipEntryAdj);
} }
ent = (ent + 1) & (mHashTableSize-1); ent = (ent + 1) & (mHashTableSize-1);
@@ -354,20 +438,24 @@ ZipEntryRO ZipFileRO::findEntryByIndex(int idx) const
* Returns "false" if the offsets to the fields or the contents of the fields * Returns "false" if the offsets to the fields or the contents of the fields
* appear to be bogus. * appear to be bogus.
*/ */
bool ZipFileRO::getEntryInfo(ZipEntryRO entry, int* pMethod, long* pUncompLen, bool ZipFileRO::getEntryInfo(ZipEntryRO entry, int* pMethod, size_t* pUncompLen,
long* pCompLen, off_t* pOffset, long* pModWhen, long* pCrc32) const size_t* pCompLen, off_t* pOffset, long* pModWhen, long* pCrc32) const
{ {
int ent = entryToIndex(entry); bool ret = false;
const int ent = entryToIndex(entry);
if (ent < 0) if (ent < 0)
return false; return false;
HashEntry hashEntry = mHashTable[ent];
/* /*
* Recover the start of the central directory entry from the filename * Recover the start of the central directory entry from the filename
* pointer. * pointer. The filename is the first entry past the fixed-size data,
* so we can just subtract back from that.
*/ */
const unsigned char* basePtr = (const unsigned char*)mFileMap->getDataPtr(); const unsigned char* ptr = (const unsigned char*) hashEntry.name;
const unsigned char* ptr = (const unsigned char*) mHashTable[ent].name; off_t cdOffset = mDirectoryOffset;
size_t zipLength = mFileMap->getDataLength();
ptr -= kCDELen; ptr -= kCDELen;
@@ -380,48 +468,78 @@ bool ZipFileRO::getEntryInfo(ZipEntryRO entry, int* pMethod, long* pUncompLen,
if (pCrc32 != NULL) if (pCrc32 != NULL)
*pCrc32 = get4LE(ptr + kCDECRC); *pCrc32 = get4LE(ptr + kCDECRC);
size_t compLen = get4LE(ptr + kCDECompLen);
if (pCompLen != NULL)
*pCompLen = compLen;
size_t uncompLen = get4LE(ptr + kCDEUncompLen);
if (pUncompLen != NULL)
*pUncompLen = uncompLen;
/* /*
* We need to make sure that the lengths are not so large that somebody * If requested, determine the offset of the start of the data. All we
* trying to map the compressed or uncompressed data runs off the end * have is the offset to the Local File Header, which is variable size,
* of the mapped region. * so we have to read the contents of the struct to figure out where
* the actual data starts.
*
* We also need to make sure that the lengths are not so large that
* somebody trying to map the compressed or uncompressed data runs
* off the end of the mapped region.
*
* Note we don't verify compLen/uncompLen if they don't request the
* dataOffset, because dataOffset is expensive to determine. However,
* if they don't have the file offset, they're not likely to be doing
* anything with the contents.
*/ */
unsigned long localHdrOffset = get4LE(ptr + kCDELocalOffset);
if (localHdrOffset + kLFHLen >= zipLength) {
LOGE("ERROR: bad local hdr offset in zip\n");
return false;
}
const unsigned char* localHdr = basePtr + localHdrOffset;
off_t dataOffset = localHdrOffset + kLFHLen
+ get2LE(localHdr + kLFHNameLen) + get2LE(localHdr + kLFHExtraLen);
if ((unsigned long) dataOffset >= zipLength) {
LOGE("ERROR: bad data offset in zip\n");
return false;
}
if (pCompLen != NULL) {
*pCompLen = get4LE(ptr + kCDECompLen);
if (*pCompLen < 0 || (size_t)(dataOffset + *pCompLen) >= zipLength) {
LOGE("ERROR: bad compressed length in zip\n");
return false;
}
}
if (pUncompLen != NULL) {
*pUncompLen = get4LE(ptr + kCDEUncompLen);
if (*pUncompLen < 0) {
LOGE("ERROR: negative uncompressed length in zip\n");
return false;
}
if (method == kCompressStored &&
(size_t)(dataOffset + *pUncompLen) >= zipLength)
{
LOGE("ERROR: bad uncompressed length in zip\n");
return false;
}
}
if (pOffset != NULL) { if (pOffset != NULL) {
long localHdrOffset = get4LE(ptr + kCDELocalOffset);
if (localHdrOffset + kLFHLen >= cdOffset) {
LOGE("ERROR: bad local hdr offset in zip\n");
return false;
}
unsigned char lfhBuf[kLFHLen];
if (lseek(mFd, localHdrOffset, SEEK_SET) != localHdrOffset) {
LOGW("failed seeking to lfh at offset %ld\n", localHdrOffset);
return false;
}
ssize_t actual =
TEMP_FAILURE_RETRY(read(mFd, lfhBuf, sizeof(lfhBuf)));
if (actual != sizeof(lfhBuf)) {
LOGW("failed reading lfh from offset %ld\n", localHdrOffset);
return false;
}
if (get4LE(lfhBuf) != kLFHSignature) {
LOGW("didn't find signature at start of lfh, offset=%ld\n",
localHdrOffset);
return false;
}
off_t dataOffset = localHdrOffset + kLFHLen
+ get2LE(lfhBuf + kLFHNameLen) + get2LE(lfhBuf + kLFHExtraLen);
if (dataOffset >= cdOffset) {
LOGW("bad data offset %ld in zip\n", (long) dataOffset);
return false;
}
/* check lengths */
if ((off_t)(dataOffset + compLen) > cdOffset) {
LOGW("bad compressed length in zip (%ld + %zd > %ld)\n",
(long) dataOffset, compLen, (long) cdOffset);
return false;
}
if (method == kCompressStored &&
(off_t)(dataOffset + uncompLen) > cdOffset)
{
LOGE("ERROR: bad uncompressed length in zip (%ld + %zd > %ld)\n",
(long) dataOffset, uncompLen, (long) cdOffset);
return false;
}
*pOffset = dataOffset; *pOffset = dataOffset;
} }
return true; return true;
} }
@@ -457,14 +575,14 @@ FileMap* ZipFileRO::createEntryFileMap(ZipEntryRO entry) const
*/ */
FileMap* newMap; FileMap* newMap;
long compLen; size_t compLen;
off_t offset; off_t offset;
if (!getEntryInfo(entry, NULL, NULL, &compLen, &offset, NULL, NULL)) if (!getEntryInfo(entry, NULL, NULL, &compLen, &offset, NULL, NULL))
return NULL; return NULL;
newMap = new FileMap(); newMap = new FileMap();
if (!newMap->create(mFileMap->getFileName(), mFd, offset, compLen, true)) { if (!newMap->create(mFileName, mFd, offset, compLen, true)) {
newMap->release(); newMap->release();
return NULL; return NULL;
} }
@@ -480,19 +598,26 @@ FileMap* ZipFileRO::createEntryFileMap(ZipEntryRO entry) const
*/ */
bool ZipFileRO::uncompressEntry(ZipEntryRO entry, void* buffer) const bool ZipFileRO::uncompressEntry(ZipEntryRO entry, void* buffer) const
{ {
const int kSequentialMin = 32768; const size_t kSequentialMin = 32768;
bool result = false; bool result = false;
int ent = entryToIndex(entry); int ent = entryToIndex(entry);
if (ent < 0) if (ent < 0)
return -1; return -1;
const unsigned char* basePtr = (const unsigned char*)mFileMap->getDataPtr();
int method; int method;
long uncompLen, compLen; size_t uncompLen, compLen;
off_t offset; off_t offset;
const unsigned char* ptr;
getEntryInfo(entry, &method, &uncompLen, &compLen, &offset, NULL, NULL); getEntryInfo(entry, &method, &uncompLen, &compLen, &offset, NULL, NULL);
FileMap* file = createEntryFileMap(entry);
if (file == NULL) {
goto bail;
}
ptr = (const unsigned char*) file->getDataPtr();
/* /*
* Experiment with madvise hint. When we want to uncompress a file, * Experiment with madvise hint. When we want to uncompress a file,
* we pull some stuff out of the central dir entry and then hit a * we pull some stuff out of the central dir entry and then hit a
@@ -507,17 +632,17 @@ bool ZipFileRO::uncompressEntry(ZipEntryRO entry, void* buffer) const
* pair of system calls are negated by a reduction in page faults. * pair of system calls are negated by a reduction in page faults.
*/ */
if (compLen > kSequentialMin) if (compLen > kSequentialMin)
mFileMap->advise(FileMap::SEQUENTIAL); file->advise(FileMap::SEQUENTIAL);
if (method == kCompressStored) { if (method == kCompressStored) {
memcpy(buffer, basePtr + offset, uncompLen); memcpy(buffer, ptr, uncompLen);
} else { } else {
if (!inflateBuffer(buffer, basePtr + offset, uncompLen, compLen)) if (!inflateBuffer(buffer, ptr, uncompLen, compLen))
goto bail; goto bail;
} }
if (compLen > kSequentialMin) if (compLen > kSequentialMin)
mFileMap->advise(FileMap::NORMAL); file->advise(FileMap::NORMAL);
result = true; result = true;
@@ -537,29 +662,34 @@ bool ZipFileRO::uncompressEntry(ZipEntryRO entry, int fd) const
if (ent < 0) if (ent < 0)
return -1; return -1;
const unsigned char* basePtr = (const unsigned char*)mFileMap->getDataPtr();
int method; int method;
long uncompLen, compLen; size_t uncompLen, compLen;
off_t offset; off_t offset;
const unsigned char* ptr;
getEntryInfo(entry, &method, &uncompLen, &compLen, &offset, NULL, NULL); getEntryInfo(entry, &method, &uncompLen, &compLen, &offset, NULL, NULL);
if (method == kCompressStored) { const FileMap* file = createEntryFileMap(entry);
ssize_t actual; if (file == NULL) {
goto bail;
}
actual = write(fd, basePtr + offset, uncompLen); ptr = (const unsigned char*) file->getDataPtr();
if (method == kCompressStored) {
ssize_t actual = write(fd, ptr, uncompLen);
if (actual < 0) { if (actual < 0) {
LOGE("Write failed: %s\n", strerror(errno)); LOGE("Write failed: %s\n", strerror(errno));
goto bail; goto bail;
} else if (actual != uncompLen) { } else if ((size_t) actual != uncompLen) {
LOGE("Partial write during uncompress (%d of %ld)\n", LOGE("Partial write during uncompress (%zd of %zd)\n",
(int)actual, uncompLen); actual, uncompLen);
goto bail; goto bail;
} else { } else {
LOGI("+++ successful write\n"); LOGI("+++ successful write\n");
} }
} else { } else {
if (!inflateBuffer(fd, basePtr+offset, uncompLen, compLen)) if (!inflateBuffer(fd, ptr, uncompLen, compLen))
goto bail; goto bail;
} }
@@ -573,7 +703,7 @@ bail:
* Uncompress "deflate" data from one buffer to another. * Uncompress "deflate" data from one buffer to another.
*/ */
/*static*/ bool ZipFileRO::inflateBuffer(void* outBuf, const void* inBuf, /*static*/ bool ZipFileRO::inflateBuffer(void* outBuf, const void* inBuf,
long uncompLen, long compLen) size_t uncompLen, size_t compLen)
{ {
bool result = false; bool result = false;
z_stream zstream; z_stream zstream;
@@ -582,7 +712,7 @@ bail:
/* /*
* Initialize the zlib stream struct. * Initialize the zlib stream struct.
*/ */
memset(&zstream, 0, sizeof(zstream)); memset(&zstream, 0, sizeof(zstream));
zstream.zalloc = Z_NULL; zstream.zalloc = Z_NULL;
zstream.zfree = Z_NULL; zstream.zfree = Z_NULL;
zstream.opaque = Z_NULL; zstream.opaque = Z_NULL;
@@ -592,10 +722,10 @@ bail:
zstream.avail_out = uncompLen; zstream.avail_out = uncompLen;
zstream.data_type = Z_UNKNOWN; zstream.data_type = Z_UNKNOWN;
/* /*
* Use the undocumented "negative window bits" feature to tell zlib * Use the undocumented "negative window bits" feature to tell zlib
* that there's no zlib header waiting for it. * that there's no zlib header waiting for it.
*/ */
zerr = inflateInit2(&zstream, -MAX_WBITS); zerr = inflateInit2(&zstream, -MAX_WBITS);
if (zerr != Z_OK) { if (zerr != Z_OK) {
if (zerr == Z_VERSION_ERROR) { if (zerr == Z_VERSION_ERROR) {
@@ -619,8 +749,8 @@ bail:
} }
/* paranoia */ /* paranoia */
if ((long) zstream.total_out != uncompLen) { if (zstream.total_out != uncompLen) {
LOGW("Size mismatch on inflated file (%ld vs %ld)\n", LOGW("Size mismatch on inflated file (%ld vs %zd)\n",
zstream.total_out, uncompLen); zstream.total_out, uncompLen);
goto z_bail; goto z_bail;
} }
@@ -638,10 +768,10 @@ bail:
* Uncompress "deflate" data from one buffer to an open file descriptor. * Uncompress "deflate" data from one buffer to an open file descriptor.
*/ */
/*static*/ bool ZipFileRO::inflateBuffer(int fd, const void* inBuf, /*static*/ bool ZipFileRO::inflateBuffer(int fd, const void* inBuf,
long uncompLen, long compLen) size_t uncompLen, size_t compLen)
{ {
bool result = false; bool result = false;
const int kWriteBufSize = 32768; const size_t kWriteBufSize = 32768;
unsigned char writeBuf[kWriteBufSize]; unsigned char writeBuf[kWriteBufSize];
z_stream zstream; z_stream zstream;
int zerr; int zerr;
@@ -649,7 +779,7 @@ bail:
/* /*
* Initialize the zlib stream struct. * Initialize the zlib stream struct.
*/ */
memset(&zstream, 0, sizeof(zstream)); memset(&zstream, 0, sizeof(zstream));
zstream.zalloc = Z_NULL; zstream.zalloc = Z_NULL;
zstream.zfree = Z_NULL; zstream.zfree = Z_NULL;
zstream.opaque = Z_NULL; zstream.opaque = Z_NULL;
@@ -659,10 +789,10 @@ bail:
zstream.avail_out = sizeof(writeBuf); zstream.avail_out = sizeof(writeBuf);
zstream.data_type = Z_UNKNOWN; zstream.data_type = Z_UNKNOWN;
/* /*
* Use the undocumented "negative window bits" feature to tell zlib * Use the undocumented "negative window bits" feature to tell zlib
* that there's no zlib header waiting for it. * that there's no zlib header waiting for it.
*/ */
zerr = inflateInit2(&zstream, -MAX_WBITS); zerr = inflateInit2(&zstream, -MAX_WBITS);
if (zerr != Z_OK) { if (zerr != Z_OK) {
if (zerr == Z_VERSION_ERROR) { if (zerr == Z_VERSION_ERROR) {
@@ -708,8 +838,8 @@ bail:
assert(zerr == Z_STREAM_END); /* other errors should've been caught */ assert(zerr == Z_STREAM_END); /* other errors should've been caught */
/* paranoia */ /* paranoia */
if ((long) zstream.total_out != uncompLen) { if (zstream.total_out != uncompLen) {
LOGW("Size mismatch on inflated file (%ld vs %ld)\n", LOGW("Size mismatch on inflated file (%ld vs %zd)\n",
zstream.total_out, uncompLen); zstream.total_out, uncompLen);
goto z_bail; goto z_bail;
} }