frameworks/native: move Zip* from libandroidfw to libutils

ZipUtils is needed by build/tools, move it from libandroidfw
(frameworks/base) to libutils (frameworks/native).

Change-Id: Ib8c41134ccdec6d6aa8d49860f8dddef49a24783
This commit is contained in:
Colin Cross 2012-03-22 18:46:44 -07:00 committed by Alex Ray
parent cd19987605
commit 3abdea92ef
9 changed files with 1790 additions and 2 deletions

View File

@ -0,0 +1,61 @@
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//
// C API for ead-only access to Zip archives, with minimal heap allocation.
//
#ifndef __LIBS_ZIPFILECRO_H
#define __LIBS_ZIPFILECRO_H
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <utils/Compat.h>
#ifdef __cplusplus
extern "C" {
#endif
/*
* Trivial typedef to ensure that ZipFileCRO is not treated as a simple integer.
*/
typedef void* ZipFileCRO;
/*
* Trivial typedef to ensure that ZipEntryCRO is not treated as a simple
* integer. We use NULL to indicate an invalid value.
*/
typedef void* ZipEntryCRO;
extern ZipFileCRO ZipFileXRO_open(const char* path);
extern void ZipFileCRO_destroy(ZipFileCRO zip);
extern ZipEntryCRO ZipFileCRO_findEntryByName(ZipFileCRO zip,
const char* fileName);
extern bool ZipFileCRO_getEntryInfo(ZipFileCRO zip, ZipEntryCRO entry,
int* pMethod, size_t* pUncompLen,
size_t* pCompLen, off64_t* pOffset, long* pModWhen, long* pCrc32);
extern bool ZipFileCRO_uncompressEntry(ZipFileCRO zip, ZipEntryCRO entry, int fd);
#ifdef __cplusplus
}
#endif
#endif /*__LIBS_ZIPFILECRO_H*/

262
include/utils/ZipFileRO.h Normal file
View File

@ -0,0 +1,262 @@
/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Read-only access to Zip archives, with minimal heap allocation.
*
* This is similar to the more-complete ZipFile class, but no attempt
* has been made to make them interchangeable. This class operates under
* a very different set of assumptions and constraints.
*
* One such assumption is that if you're getting file descriptors for
* use with this class as a child of a fork() operation, you must be on
* a pread() to guarantee correct operation. This is because pread() can
* atomically read at a file offset without worrying about a lock around an
* lseek() + read() pair.
*/
#ifndef __LIBS_ZIPFILERO_H
#define __LIBS_ZIPFILERO_H
#include <utils/Compat.h>
#include <utils/Errors.h>
#include <utils/FileMap.h>
#include <utils/threads.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
namespace android {
/*
* Trivial typedef to ensure that ZipEntryRO is not treated as a simple
* integer. We use NULL to indicate an invalid value.
*/
typedef void* ZipEntryRO;
/*
* Open a Zip archive for reading.
*
* We want "open" and "find entry by name" to be fast operations, and we
* want to use as little memory as possible. We memory-map the file,
* and load a hash table with pointers to the filenames (which aren't
* null-terminated). The other fields are at a fixed offset from the
* filename, so we don't need to extract those (but we do need to byte-read
* and endian-swap them every time we want them).
*
* To speed comparisons when doing a lookup by name, we could make the mapping
* "private" (copy-on-write) and null-terminate the filenames after verifying
* the record structure. However, this requires a private mapping of
* every page that the Central Directory touches. Easier to tuck a copy
* of the string length into the hash table entry.
*
* NOTE: If this is used on file descriptors inherited from a fork() operation,
* you must be on a platform that implements pread() to guarantee correctness
* on the shared file descriptors.
*/
class ZipFileRO {
public:
ZipFileRO()
: mFd(-1), mFileName(NULL), mFileLength(-1),
mDirectoryMap(NULL),
mNumEntries(-1), mDirectoryOffset(-1),
mHashTableSize(-1), mHashTable(NULL)
{}
~ZipFileRO();
/*
* Open an archive.
*/
status_t open(const char* zipFileName);
/*
* Find an entry, by name. Returns the entry identifier, or NULL if
* not found.
*
* If two entries have the same name, one will be chosen at semi-random.
*/
ZipEntryRO findEntryByName(const char* fileName) const;
/*
* Return the #of entries in the Zip archive.
*/
int getNumEntries(void) const {
return mNumEntries;
}
/*
* Return the Nth entry. Zip file entries are not stored in sorted
* order, and updated entries may appear at the end, so anyone walking
* the archive needs to avoid making ordering assumptions. We take
* that further by returning the Nth non-empty entry in the hash table
* rather than the Nth entry in the archive.
*
* Valid values are [0..numEntries).
*
* [This is currently O(n). If it needs to be fast we can allocate an
* additional data structure or provide an iterator interface.]
*/
ZipEntryRO findEntryByIndex(int idx) const;
/*
* Copy the filename into the supplied buffer. Returns 0 on success,
* -1 if "entry" is invalid, or the filename length if it didn't fit. The
* length, and the returned string, include the null-termination.
*/
int getEntryFileName(ZipEntryRO entry, char* buffer, int bufLen) const;
/*
* Get the vital stats for an entry. Pass in NULL pointers for anything
* you don't need.
*
* "*pOffset" holds the Zip file offset of the entry's data.
*
* Returns "false" if "entry" is bogus or if the data in the Zip file
* appears to be bad.
*/
bool getEntryInfo(ZipEntryRO entry, int* pMethod, size_t* pUncompLen,
size_t* pCompLen, off64_t* pOffset, long* pModWhen, long* pCrc32) const;
/*
* Create a new FileMap object that maps a subset of the archive. For
* an uncompressed entry this effectively provides a pointer to the
* actual data, for a compressed entry this provides the input buffer
* for inflate().
*/
FileMap* createEntryFileMap(ZipEntryRO entry) const;
/*
* Uncompress the data into a buffer. Depending on the compression
* format, this is either an "inflate" operation or a memcpy.
*
* Use "uncompLen" from getEntryInfo() to determine the required
* buffer size.
*
* Returns "true" on success.
*/
bool uncompressEntry(ZipEntryRO entry, void* buffer) const;
/*
* Uncompress the data to an open file descriptor.
*/
bool uncompressEntry(ZipEntryRO entry, int fd) const;
/* Zip compression methods we support */
enum {
kCompressStored = 0, // no compression
kCompressDeflated = 8, // standard deflate
};
/*
* Utility function: uncompress deflated data, buffer to buffer.
*/
static bool inflateBuffer(void* outBuf, const void* inBuf,
size_t uncompLen, size_t compLen);
/*
* Utility function: uncompress deflated data, buffer to fd.
*/
static bool inflateBuffer(int fd, const void* inBuf,
size_t uncompLen, size_t compLen);
/*
* Utility function to convert ZIP's time format to a timespec struct.
*/
static inline void zipTimeToTimespec(long when, struct tm* timespec) {
const long date = when >> 16;
timespec->tm_year = ((date >> 9) & 0x7F) + 80; // Zip is years since 1980
timespec->tm_mon = (date >> 5) & 0x0F;
timespec->tm_mday = date & 0x1F;
timespec->tm_hour = (when >> 11) & 0x1F;
timespec->tm_min = (when >> 5) & 0x3F;
timespec->tm_sec = (when & 0x1F) << 1;
}
/*
* Some basic functions for raw data manipulation. "LE" means
* Little Endian.
*/
static inline unsigned short get2LE(const unsigned char* buf) {
return buf[0] | (buf[1] << 8);
}
static inline unsigned long get4LE(const unsigned char* buf) {
return buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
}
private:
/* these are private and not defined */
ZipFileRO(const ZipFileRO& src);
ZipFileRO& operator=(const ZipFileRO& src);
/* locate and parse the central directory */
bool mapCentralDirectory(void);
/* parse the archive, prepping internal structures */
bool parseZipArchive(void);
/* add a new entry to the hash table */
void addToHash(const char* str, int strLen, unsigned int hash);
/* compute string hash code */
static unsigned int computeHash(const char* str, int len);
/* convert a ZipEntryRO back to a hash table index */
int entryToIndex(const ZipEntryRO entry) const;
/*
* One entry in the hash table.
*/
typedef struct HashEntry {
const char* name;
unsigned short nameLen;
//unsigned int hash;
} HashEntry;
/* open Zip archive */
int mFd;
/* Lock for handling the file descriptor (seeks, etc) */
mutable Mutex mFdLock;
/* zip file name */
char* mFileName;
/* length of file */
size_t mFileLength;
/* mapped file */
FileMap* mDirectoryMap;
/* number of entries in the Zip archive */
int mNumEntries;
/* CD directory offset in the Zip archive */
off64_t mDirectoryOffset;
/*
* We know how many entries are in the Zip archive, so we have a
* fixed-size hash table. We probe for an empty slot.
*/
int mHashTableSize;
HashEntry* mHashTable;
};
}; // namespace android
#endif /*__LIBS_ZIPFILERO_H*/

67
include/utils/ZipUtils.h Normal file
View File

@ -0,0 +1,67 @@
/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//
// Miscellaneous zip/gzip utility functions.
//
#ifndef __LIBS_ZIPUTILS_H
#define __LIBS_ZIPUTILS_H
#include <stdio.h>
namespace android {
/*
* Container class for utility functions, primarily for namespace reasons.
*/
class ZipUtils {
public:
/*
* General utility function for uncompressing "deflate" data from a file
* to a buffer.
*/
static bool inflateToBuffer(int fd, void* buf, long uncompressedLen,
long compressedLen);
static bool inflateToBuffer(FILE* fp, void* buf, long uncompressedLen,
long compressedLen);
/*
* Someday we might want to make this generic and handle bzip2 ".bz2"
* files too.
*
* We could declare gzip to be a sub-class of zip that has exactly
* one always-compressed entry, but we currently want to treat Zip
* and gzip as distinct, so there's no value.
*
* The zlib library has some gzip utilities, but it has no interface
* for extracting the uncompressed length of the file (you do *not*
* want to gzseek to the end).
*
* Pass in a seeked file pointer for the gzip file. If this is a gzip
* file, we set our return values appropriately and return "true" with
* the file seeked to the start of the compressed data.
*/
static bool examineGzip(FILE* fp, int* pCompressionMethod,
long* pUncompressedLen, long* pCompressedLen, unsigned long* pCRC32);
private:
ZipUtils() {}
~ZipUtils() {}
};
}; // namespace android
#endif /*__LIBS_ZIPUTILS_H*/

View File

@ -42,6 +42,9 @@ commonSources:= \
Unicode.cpp \
VectorImpl.cpp \
WorkQueue.cpp \
ZipFileCRO.cpp \
ZipFileRO.cpp \
ZipUtils.cpp \
misc.cpp
host_commonCflags := -DLIBUTILS_NATIVE=1 $(TOOL_CFLAGS)
@ -96,7 +99,8 @@ LOCAL_LDLIBS += -lrt -ldl
endif
LOCAL_C_INCLUDES += \
bionic/libc/private
bionic/libc/private \
external/zlib
LOCAL_LDLIBS += -lpthread
@ -104,7 +108,8 @@ LOCAL_SHARED_LIBRARIES := \
liblog \
libcutils \
libdl \
libcorkscrew
libcorkscrew \
libz
LOCAL_MODULE:= libutils
include $(BUILD_SHARED_LIBRARY)

54
libs/utils/ZipFileCRO.cpp Normal file
View File

@ -0,0 +1,54 @@
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <utils/ZipFileCRO.h>
#include <utils/ZipFileRO.h>
using namespace android;
ZipFileCRO ZipFileXRO_open(const char* path) {
ZipFileRO* zip = new ZipFileRO();
if (zip->open(path) == NO_ERROR) {
return (ZipFileCRO)zip;
}
return NULL;
}
void ZipFileCRO_destroy(ZipFileCRO zipToken) {
ZipFileRO* zip = (ZipFileRO*)zipToken;
delete zip;
}
ZipEntryCRO ZipFileCRO_findEntryByName(ZipFileCRO zipToken,
const char* fileName) {
ZipFileRO* zip = (ZipFileRO*)zipToken;
return (ZipEntryCRO)zip->findEntryByName(fileName);
}
bool ZipFileCRO_getEntryInfo(ZipFileCRO zipToken, ZipEntryRO entryToken,
int* pMethod, size_t* pUncompLen,
size_t* pCompLen, off64_t* pOffset, long* pModWhen, long* pCrc32) {
ZipFileRO* zip = (ZipFileRO*)zipToken;
ZipEntryRO entry = (ZipEntryRO)entryToken;
return zip->getEntryInfo(entry, pMethod, pUncompLen, pCompLen, pOffset,
pModWhen, pCrc32);
}
bool ZipFileCRO_uncompressEntry(ZipFileCRO zipToken, ZipEntryRO entryToken, int fd) {
ZipFileRO* zip = (ZipFileRO*)zipToken;
ZipEntryRO entry = (ZipEntryRO)entryToken;
return zip->uncompressEntry(entry, fd);
}

931
libs/utils/ZipFileRO.cpp Normal file
View File

@ -0,0 +1,931 @@
/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//
// Read-only access to Zip archives, with minimal heap allocation.
//
#define LOG_TAG "zipro"
//#define LOG_NDEBUG 0
#include <utils/Log.h>
#include <utils/ZipFileRO.h>
#include <utils/misc.h>
#include <utils/threads.h>
#include <zlib.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <assert.h>
#include <unistd.h>
#if HAVE_PRINTF_ZD
# define ZD "%zd"
# define ZD_TYPE ssize_t
#else
# define ZD "%ld"
# define ZD_TYPE long
#endif
/*
* We must open binary files using open(path, ... | O_BINARY) under Windows.
* Otherwise strange read errors will happen.
*/
#ifndef O_BINARY
# define O_BINARY 0
#endif
/*
* 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;
/*
* Zip file constants.
*/
#define kEOCDSignature 0x06054b50
#define kEOCDLen 22
#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 kMaxCommentLen 65535 // longest possible in ushort
#define kMaxEOCDSearch (kMaxCommentLen + kEOCDLen)
#define kLFHSignature 0x04034b50
#define kLFHLen 30 // excluding variable-len fields
#define kLFHNameLen 26 // offset to filename length
#define kLFHExtraLen 28 // offset to extra length
#define kCDESignature 0x02014b50
#define kCDELen 46 // excluding variable-len fields
#define kCDEMethod 10 // offset to compression method
#define kCDEModWhen 12 // offset to modification timestamp
#define kCDECRC 16 // offset to entry CRC
#define kCDECompLen 20 // offset to compressed length
#define kCDEUncompLen 24 // offset to uncompressed length
#define kCDENameLen 28 // offset to filename length
#define kCDEExtraLen 30 // offset to extra length
#define kCDECommentLen 32 // offset to comment length
#define kCDELocalOffset 42 // offset to local hdr
/*
* The values we return for ZipEntryRO use 0 as an invalid value, so we
* want to adjust the hash table index by a fixed amount. Using a large
* value helps insure that people don't mix & match arguments, e.g. to
* findEntryByIndex().
*/
#define kZipEntryAdj 10000
ZipFileRO::~ZipFileRO() {
free(mHashTable);
if (mDirectoryMap)
mDirectoryMap->release();
if (mFd >= 0)
TEMP_FAILURE_RETRY(close(mFd));
if (mFileName)
free(mFileName);
}
/*
* Convert a ZipEntryRO to a hash table index, verifying that it's in a
* valid range.
*/
int ZipFileRO::entryToIndex(const ZipEntryRO entry) const
{
long ent = ((long) entry) - kZipEntryAdj;
if (ent < 0 || ent >= mHashTableSize || mHashTable[ent].name == NULL) {
ALOGW("Invalid ZipEntryRO %p (%ld)\n", entry, ent);
return -1;
}
return ent;
}
/*
* Open the specified file read-only. We memory-map the entire thing and
* close the file before returning.
*/
status_t ZipFileRO::open(const char* zipFileName)
{
int fd = -1;
assert(mDirectoryMap == NULL);
/*
* Open and map the specified file.
*/
fd = ::open(zipFileName, O_RDONLY | O_BINARY);
if (fd < 0) {
ALOGW("Unable to open zip '%s': %s\n", zipFileName, strerror(errno));
return NAME_NOT_FOUND;
}
mFileLength = lseek64(fd, 0, SEEK_END);
if (mFileLength < kEOCDLen) {
TEMP_FAILURE_RETRY(close(fd));
return UNKNOWN_ERROR;
}
if (mFileName != NULL) {
free(mFileName);
}
mFileName = strdup(zipFileName);
mFd = fd;
/*
* 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()) {
goto bail;
}
return OK;
bail:
free(mFileName);
mFileName = NULL;
TEMP_FAILURE_RETRY(close(fd));
return UNKNOWN_ERROR;
}
/*
* Parse the Zip archive, verifying its contents and initializing internal
* data structures.
*/
bool ZipFileRO::mapCentralDirectory(void)
{
ssize_t readAmount = kMaxEOCDSearch;
if (readAmount > (ssize_t) mFileLength)
readAmount = mFileLength;
unsigned char* scanBuf = (unsigned char*) malloc(readAmount);
if (scanBuf == NULL) {
ALOGW("couldn't allocate scanBuf: %s", strerror(errno));
free(scanBuf);
return false;
}
/*
* Make sure this is a Zip archive.
*/
if (lseek64(mFd, 0, SEEK_SET) != 0) {
ALOGW("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)) {
ALOGI("couldn't read first signature from zip archive: %s", strerror(errno));
free(scanBuf);
return false;
}
{
unsigned int header = get4LE(scanBuf);
if (header == kEOCDSignature) {
ALOGI("Found Zip archive, but it looks empty\n");
free(scanBuf);
return false;
} else if (header != kLFHSignature) {
ALOGV("Not a Zip archive (found 0x%08x)\n", header);
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.
*/
off64_t searchStart = mFileLength - readAmount;
if (lseek64(mFd, searchStart, SEEK_SET) != searchStart) {
ALOGW("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) {
ALOGW("Zip: read " ZD ", expected " ZD ". Failed: %s\n",
(ZD_TYPE) actual, (ZD_TYPE) 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) {
ALOGV("+++ Found EOCD at buf+%d\n", i);
break;
}
}
if (i < 0) {
ALOGD("Zip: EOCD not found, %s is not zip\n", mFileName);
free(scanBuf);
return false;
}
off64_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. After that, we can release our EOCD hunt buffer.
*/
unsigned int numEntries = get2LE(eocdPtr + kEOCDNumEntries);
unsigned int dirSize = get4LE(eocdPtr + kEOCDSize);
unsigned int dirOffset = get4LE(eocdPtr + kEOCDFileOffset);
free(scanBuf);
// Verify that they look reasonable.
if ((long long) dirOffset + (long long) dirSize > (long long) eocdOffset) {
ALOGW("bad offsets (dir %ld, size %u, eocd %ld)\n",
(long) dirOffset, dirSize, (long) eocdOffset);
return false;
}
if (numEntries == 0) {
ALOGW("empty archive?\n");
return false;
}
ALOGV("+++ numEntries=%d dirSize=%d dirOffset=%d\n",
numEntries, dirSize, dirOffset);
mDirectoryMap = new FileMap();
if (mDirectoryMap == NULL) {
ALOGW("Unable to create directory map: %s", strerror(errno));
return false;
}
if (!mDirectoryMap->create(mFileName, mFd, dirOffset, dirSize, true)) {
ALOGW("Unable to map '%s' (" ZD " to " ZD "): %s\n", mFileName,
(ZD_TYPE) dirOffset, (ZD_TYPE) (dirOffset + dirSize), strerror(errno));
return false;
}
mNumEntries = numEntries;
mDirectoryOffset = dirOffset;
return true;
}
bool ZipFileRO::parseZipArchive(void)
{
bool result = false;
const unsigned char* cdPtr = (const unsigned char*) mDirectoryMap->getDataPtr();
size_t cdLength = mDirectoryMap->getDataLength();
int numEntries = mNumEntries;
/*
* Create hash table. We have a minimum 75% load factor, possibly as
* low as 50% after we round off to a power of 2.
*/
mHashTableSize = roundUpPower2(1 + (numEntries * 4) / 3);
mHashTable = (HashEntry*) calloc(mHashTableSize, sizeof(HashEntry));
/*
* Walk through the central directory, adding entries to the hash
* table.
*/
const unsigned char* ptr = cdPtr;
for (int i = 0; i < numEntries; i++) {
if (get4LE(ptr) != kCDESignature) {
ALOGW("Missed a central dir sig (at %d)\n", i);
goto bail;
}
if (ptr + kCDELen > cdPtr + cdLength) {
ALOGW("Ran off the end (at %d)\n", i);
goto bail;
}
long localHdrOffset = (long) get4LE(ptr + kCDELocalOffset);
if (localHdrOffset >= mDirectoryOffset) {
ALOGW("bad LFH offset %ld at entry %d\n", localHdrOffset, i);
goto bail;
}
unsigned int fileNameLen, extraLen, commentLen, hash;
fileNameLen = get2LE(ptr + kCDENameLen);
extraLen = get2LE(ptr + kCDEExtraLen);
commentLen = get2LE(ptr + kCDECommentLen);
/* add the CDE filename to the hash table */
hash = computeHash((const char*)ptr + kCDELen, fileNameLen);
addToHash((const char*)ptr + kCDELen, fileNameLen, hash);
ptr += kCDELen + fileNameLen + extraLen + commentLen;
if ((size_t)(ptr - cdPtr) > cdLength) {
ALOGW("bad CD advance (%d vs " ZD ") at entry %d\n",
(int) (ptr - cdPtr), (ZD_TYPE) cdLength, i);
goto bail;
}
}
ALOGV("+++ zip good scan %d entries\n", numEntries);
result = true;
bail:
return result;
}
/*
* Simple string hash function for non-null-terminated strings.
*/
/*static*/ unsigned int ZipFileRO::computeHash(const char* str, int len)
{
unsigned int hash = 0;
while (len--)
hash = hash * 31 + *str++;
return hash;
}
/*
* Add a new entry to the hash table.
*/
void ZipFileRO::addToHash(const char* str, int strLen, unsigned int hash)
{
int ent = hash & (mHashTableSize-1);
/*
* We over-allocate the table, so we're guaranteed to find an empty slot.
*/
while (mHashTable[ent].name != NULL)
ent = (ent + 1) & (mHashTableSize-1);
mHashTable[ent].name = str;
mHashTable[ent].nameLen = strLen;
}
/*
* Find a matching entry.
*
* Returns NULL if not found.
*/
ZipEntryRO ZipFileRO::findEntryByName(const char* fileName) const
{
/*
* If the ZipFileRO instance is not initialized, the entry number will
* end up being garbage since mHashTableSize is -1.
*/
if (mHashTableSize <= 0) {
return NULL;
}
int nameLen = strlen(fileName);
unsigned int hash = computeHash(fileName, nameLen);
int ent = hash & (mHashTableSize-1);
while (mHashTable[ent].name != NULL) {
if (mHashTable[ent].nameLen == nameLen &&
memcmp(mHashTable[ent].name, fileName, nameLen) == 0)
{
/* match */
return (ZipEntryRO)(long)(ent + kZipEntryAdj);
}
ent = (ent + 1) & (mHashTableSize-1);
}
return NULL;
}
/*
* Find the Nth entry.
*
* This currently involves walking through the sparse hash table, counting
* non-empty entries. If we need to speed this up we can either allocate
* a parallel lookup table or (perhaps better) provide an iterator interface.
*/
ZipEntryRO ZipFileRO::findEntryByIndex(int idx) const
{
if (idx < 0 || idx >= mNumEntries) {
ALOGW("Invalid index %d\n", idx);
return NULL;
}
for (int ent = 0; ent < mHashTableSize; ent++) {
if (mHashTable[ent].name != NULL) {
if (idx-- == 0)
return (ZipEntryRO) (ent + kZipEntryAdj);
}
}
return NULL;
}
/*
* Get the useful fields from the zip entry.
*
* Returns "false" if the offsets to the fields or the contents of the fields
* appear to be bogus.
*/
bool ZipFileRO::getEntryInfo(ZipEntryRO entry, int* pMethod, size_t* pUncompLen,
size_t* pCompLen, off64_t* pOffset, long* pModWhen, long* pCrc32) const
{
bool ret = false;
const int ent = entryToIndex(entry);
if (ent < 0)
return false;
HashEntry hashEntry = mHashTable[ent];
/*
* Recover the start of the central directory entry from the filename
* pointer. The filename is the first entry past the fixed-size data,
* so we can just subtract back from that.
*/
const unsigned char* ptr = (const unsigned char*) hashEntry.name;
off64_t cdOffset = mDirectoryOffset;
ptr -= kCDELen;
int method = get2LE(ptr + kCDEMethod);
if (pMethod != NULL)
*pMethod = method;
if (pModWhen != NULL)
*pModWhen = get4LE(ptr + kCDEModWhen);
if (pCrc32 != NULL)
*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;
/*
* If requested, determine the offset of the start of the data. All we
* have is the offset to the Local File Header, which is variable size,
* 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.
*/
if (pOffset != NULL) {
long localHdrOffset = get4LE(ptr + kCDELocalOffset);
if (localHdrOffset + kLFHLen >= cdOffset) {
ALOGE("ERROR: bad local hdr offset in zip\n");
return false;
}
unsigned char lfhBuf[kLFHLen];
#ifdef HAVE_PREAD
/*
* This file descriptor might be from zygote's preloaded assets,
* so we need to do an pread64() instead of a lseek64() + read() to
* guarantee atomicity across the processes with the shared file
* descriptors.
*/
ssize_t actual =
TEMP_FAILURE_RETRY(pread64(mFd, lfhBuf, sizeof(lfhBuf), localHdrOffset));
if (actual != sizeof(lfhBuf)) {
ALOGW("failed reading lfh from offset %ld\n", localHdrOffset);
return false;
}
if (get4LE(lfhBuf) != kLFHSignature) {
ALOGW("didn't find signature at start of lfh; wanted: offset=%ld data=0x%08x; "
"got: data=0x%08lx\n",
localHdrOffset, kLFHSignature, get4LE(lfhBuf));
return false;
}
#else /* HAVE_PREAD */
/*
* For hosts don't have pread64() we cannot guarantee atomic reads from
* an offset in a file. Android should never run on those platforms.
* File descriptors inherited from a fork() share file offsets and
* there would be nothing to protect from two different processes
* calling lseek64() concurrently.
*/
{
AutoMutex _l(mFdLock);
if (lseek64(mFd, localHdrOffset, SEEK_SET) != localHdrOffset) {
ALOGW("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)) {
ALOGW("failed reading lfh from offset %ld\n", localHdrOffset);
return false;
}
if (get4LE(lfhBuf) != kLFHSignature) {
off64_t actualOffset = lseek64(mFd, 0, SEEK_CUR);
ALOGW("didn't find signature at start of lfh; wanted: offset=%ld data=0x%08x; "
"got: offset=" ZD " data=0x%08lx\n",
localHdrOffset, kLFHSignature, (ZD_TYPE) actualOffset, get4LE(lfhBuf));
return false;
}
}
#endif /* HAVE_PREAD */
off64_t dataOffset = localHdrOffset + kLFHLen
+ get2LE(lfhBuf + kLFHNameLen) + get2LE(lfhBuf + kLFHExtraLen);
if (dataOffset >= cdOffset) {
ALOGW("bad data offset %ld in zip\n", (long) dataOffset);
return false;
}
/* check lengths */
if ((off64_t)(dataOffset + compLen) > cdOffset) {
ALOGW("bad compressed length in zip (%ld + " ZD " > %ld)\n",
(long) dataOffset, (ZD_TYPE) compLen, (long) cdOffset);
return false;
}
if (method == kCompressStored &&
(off64_t)(dataOffset + uncompLen) > cdOffset)
{
ALOGE("ERROR: bad uncompressed length in zip (%ld + " ZD " > %ld)\n",
(long) dataOffset, (ZD_TYPE) uncompLen, (long) cdOffset);
return false;
}
*pOffset = dataOffset;
}
return true;
}
/*
* Copy the entry's filename to the buffer.
*/
int ZipFileRO::getEntryFileName(ZipEntryRO entry, char* buffer, int bufLen)
const
{
int ent = entryToIndex(entry);
if (ent < 0)
return -1;
int nameLen = mHashTable[ent].nameLen;
if (bufLen < nameLen+1)
return nameLen+1;
memcpy(buffer, mHashTable[ent].name, nameLen);
buffer[nameLen] = '\0';
return 0;
}
/*
* Create a new FileMap object that spans the data in "entry".
*/
FileMap* ZipFileRO::createEntryFileMap(ZipEntryRO entry) const
{
/*
* TODO: the efficient way to do this is to modify FileMap to allow
* sub-regions of a file to be mapped. A reference-counting scheme
* can manage the base memory mapping. For now, we just create a brand
* new mapping off of the Zip archive file descriptor.
*/
FileMap* newMap;
size_t compLen;
off64_t offset;
if (!getEntryInfo(entry, NULL, NULL, &compLen, &offset, NULL, NULL))
return NULL;
newMap = new FileMap();
if (!newMap->create(mFileName, mFd, offset, compLen, true)) {
newMap->release();
return NULL;
}
return newMap;
}
/*
* Uncompress an entry, in its entirety, into the provided output buffer.
*
* This doesn't verify the data's CRC, which might be useful for
* uncompressed data. The caller should be able to manage it.
*/
bool ZipFileRO::uncompressEntry(ZipEntryRO entry, void* buffer) const
{
const size_t kSequentialMin = 32768;
bool result = false;
int ent = entryToIndex(entry);
if (ent < 0)
return -1;
int method;
size_t uncompLen, compLen;
off64_t offset;
const unsigned char* ptr;
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,
* we pull some stuff out of the central dir entry and then hit a
* bunch of compressed or uncompressed data sequentially. The CDE
* visit will cause a limited amount of read-ahead because it's at
* the end of the file. We could end up doing lots of extra disk
* access if the file we're prying open is small. Bottom line is we
* probably don't want to turn MADV_SEQUENTIAL on and leave it on.
*
* So, if the compressed size of the file is above a certain minimum
* size, temporarily boost the read-ahead in the hope that the extra
* pair of system calls are negated by a reduction in page faults.
*/
if (compLen > kSequentialMin)
file->advise(FileMap::SEQUENTIAL);
if (method == kCompressStored) {
memcpy(buffer, ptr, uncompLen);
} else {
if (!inflateBuffer(buffer, ptr, uncompLen, compLen))
goto unmap;
}
if (compLen > kSequentialMin)
file->advise(FileMap::NORMAL);
result = true;
unmap:
file->release();
bail:
return result;
}
/*
* Uncompress an entry, in its entirety, to an open file descriptor.
*
* This doesn't verify the data's CRC, but probably should.
*/
bool ZipFileRO::uncompressEntry(ZipEntryRO entry, int fd) const
{
bool result = false;
int ent = entryToIndex(entry);
if (ent < 0)
return -1;
int method;
size_t uncompLen, compLen;
off64_t offset;
const unsigned char* ptr;
getEntryInfo(entry, &method, &uncompLen, &compLen, &offset, NULL, NULL);
FileMap* file = createEntryFileMap(entry);
if (file == NULL) {
goto bail;
}
ptr = (const unsigned char*) file->getDataPtr();
if (method == kCompressStored) {
ssize_t actual = write(fd, ptr, uncompLen);
if (actual < 0) {
ALOGE("Write failed: %s\n", strerror(errno));
goto unmap;
} else if ((size_t) actual != uncompLen) {
ALOGE("Partial write during uncompress (" ZD " of " ZD ")\n",
(ZD_TYPE) actual, (ZD_TYPE) uncompLen);
goto unmap;
} else {
ALOGI("+++ successful write\n");
}
} else {
if (!inflateBuffer(fd, ptr, uncompLen, compLen))
goto unmap;
}
result = true;
unmap:
file->release();
bail:
return result;
}
/*
* Uncompress "deflate" data from one buffer to another.
*/
/*static*/ bool ZipFileRO::inflateBuffer(void* outBuf, const void* inBuf,
size_t uncompLen, size_t compLen)
{
bool result = false;
z_stream zstream;
int zerr;
/*
* Initialize the zlib stream struct.
*/
memset(&zstream, 0, sizeof(zstream));
zstream.zalloc = Z_NULL;
zstream.zfree = Z_NULL;
zstream.opaque = Z_NULL;
zstream.next_in = (Bytef*)inBuf;
zstream.avail_in = compLen;
zstream.next_out = (Bytef*) outBuf;
zstream.avail_out = uncompLen;
zstream.data_type = Z_UNKNOWN;
/*
* Use the undocumented "negative window bits" feature to tell zlib
* that there's no zlib header waiting for it.
*/
zerr = inflateInit2(&zstream, -MAX_WBITS);
if (zerr != Z_OK) {
if (zerr == Z_VERSION_ERROR) {
ALOGE("Installed zlib is not compatible with linked version (%s)\n",
ZLIB_VERSION);
} else {
ALOGE("Call to inflateInit2 failed (zerr=%d)\n", zerr);
}
goto bail;
}
/*
* Expand data.
*/
zerr = inflate(&zstream, Z_FINISH);
if (zerr != Z_STREAM_END) {
ALOGW("Zip inflate failed, zerr=%d (nIn=%p aIn=%u nOut=%p aOut=%u)\n",
zerr, zstream.next_in, zstream.avail_in,
zstream.next_out, zstream.avail_out);
goto z_bail;
}
/* paranoia */
if (zstream.total_out != uncompLen) {
ALOGW("Size mismatch on inflated file (%ld vs " ZD ")\n",
zstream.total_out, (ZD_TYPE) uncompLen);
goto z_bail;
}
result = true;
z_bail:
inflateEnd(&zstream); /* free up any allocated structures */
bail:
return result;
}
/*
* Uncompress "deflate" data from one buffer to an open file descriptor.
*/
/*static*/ bool ZipFileRO::inflateBuffer(int fd, const void* inBuf,
size_t uncompLen, size_t compLen)
{
bool result = false;
const size_t kWriteBufSize = 32768;
unsigned char writeBuf[kWriteBufSize];
z_stream zstream;
int zerr;
/*
* Initialize the zlib stream struct.
*/
memset(&zstream, 0, sizeof(zstream));
zstream.zalloc = Z_NULL;
zstream.zfree = Z_NULL;
zstream.opaque = Z_NULL;
zstream.next_in = (Bytef*)inBuf;
zstream.avail_in = compLen;
zstream.next_out = (Bytef*) writeBuf;
zstream.avail_out = sizeof(writeBuf);
zstream.data_type = Z_UNKNOWN;
/*
* Use the undocumented "negative window bits" feature to tell zlib
* that there's no zlib header waiting for it.
*/
zerr = inflateInit2(&zstream, -MAX_WBITS);
if (zerr != Z_OK) {
if (zerr == Z_VERSION_ERROR) {
ALOGE("Installed zlib is not compatible with linked version (%s)\n",
ZLIB_VERSION);
} else {
ALOGE("Call to inflateInit2 failed (zerr=%d)\n", zerr);
}
goto bail;
}
/*
* Loop while we have more to do.
*/
do {
/*
* Expand data.
*/
zerr = inflate(&zstream, Z_NO_FLUSH);
if (zerr != Z_OK && zerr != Z_STREAM_END) {
ALOGW("zlib inflate: zerr=%d (nIn=%p aIn=%u nOut=%p aOut=%u)\n",
zerr, zstream.next_in, zstream.avail_in,
zstream.next_out, zstream.avail_out);
goto z_bail;
}
/* write when we're full or when we're done */
if (zstream.avail_out == 0 ||
(zerr == Z_STREAM_END && zstream.avail_out != sizeof(writeBuf)))
{
long writeSize = zstream.next_out - writeBuf;
int cc = write(fd, writeBuf, writeSize);
if (cc != (int) writeSize) {
ALOGW("write failed in inflate (%d vs %ld)\n", cc, writeSize);
goto z_bail;
}
zstream.next_out = writeBuf;
zstream.avail_out = sizeof(writeBuf);
}
} while (zerr == Z_OK);
assert(zerr == Z_STREAM_END); /* other errors should've been caught */
/* paranoia */
if (zstream.total_out != uncompLen) {
ALOGW("Size mismatch on inflated file (%ld vs " ZD ")\n",
zstream.total_out, (ZD_TYPE) uncompLen);
goto z_bail;
}
result = true;
z_bail:
inflateEnd(&zstream); /* free up any allocated structures */
bail:
return result;
}

343
libs/utils/ZipUtils.cpp Normal file
View File

@ -0,0 +1,343 @@
/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//
// Misc zip/gzip utility functions.
//
#define LOG_TAG "ziputil"
#include <utils/Log.h>
#include <utils/ZipUtils.h>
#include <utils/ZipFileRO.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <zlib.h>
using namespace android;
/*
* Utility function that expands zip/gzip "deflate" compressed data
* into a buffer.
*
* "fd" is an open file positioned at the start of the "deflate" data
* "buf" must hold at least "uncompressedLen" bytes.
*/
/*static*/ bool ZipUtils::inflateToBuffer(int fd, void* buf,
long uncompressedLen, long compressedLen)
{
bool result = false;
const unsigned long kReadBufSize = 32768;
unsigned char* readBuf = NULL;
z_stream zstream;
int zerr;
unsigned long compRemaining;
assert(uncompressedLen >= 0);
assert(compressedLen >= 0);
readBuf = new unsigned char[kReadBufSize];
if (readBuf == NULL)
goto bail;
compRemaining = compressedLen;
/*
* Initialize the zlib stream.
*/
memset(&zstream, 0, sizeof(zstream));
zstream.zalloc = Z_NULL;
zstream.zfree = Z_NULL;
zstream.opaque = Z_NULL;
zstream.next_in = NULL;
zstream.avail_in = 0;
zstream.next_out = (Bytef*) buf;
zstream.avail_out = uncompressedLen;
zstream.data_type = Z_UNKNOWN;
/*
* Use the undocumented "negative window bits" feature to tell zlib
* that there's no zlib header waiting for it.
*/
zerr = inflateInit2(&zstream, -MAX_WBITS);
if (zerr != Z_OK) {
if (zerr == Z_VERSION_ERROR) {
ALOGE("Installed zlib is not compatible with linked version (%s)\n",
ZLIB_VERSION);
} else {
ALOGE("Call to inflateInit2 failed (zerr=%d)\n", zerr);
}
goto bail;
}
/*
* Loop while we have data.
*/
do {
unsigned long getSize;
/* read as much as we can */
if (zstream.avail_in == 0) {
getSize = (compRemaining > kReadBufSize) ?
kReadBufSize : compRemaining;
ALOGV("+++ reading %ld bytes (%ld left)\n",
getSize, compRemaining);
int cc = read(fd, readBuf, getSize);
if (cc != (int) getSize) {
ALOGD("inflate read failed (%d vs %ld)\n",
cc, getSize);
goto z_bail;
}
compRemaining -= getSize;
zstream.next_in = readBuf;
zstream.avail_in = getSize;
}
/* uncompress the data */
zerr = inflate(&zstream, Z_NO_FLUSH);
if (zerr != Z_OK && zerr != Z_STREAM_END) {
ALOGD("zlib inflate call failed (zerr=%d)\n", zerr);
goto z_bail;
}
/* output buffer holds all, so no need to write the output */
} while (zerr == Z_OK);
assert(zerr == Z_STREAM_END); /* other errors should've been caught */
if ((long) zstream.total_out != uncompressedLen) {
ALOGW("Size mismatch on inflated file (%ld vs %ld)\n",
zstream.total_out, uncompressedLen);
goto z_bail;
}
// success!
result = true;
z_bail:
inflateEnd(&zstream); /* free up any allocated structures */
bail:
delete[] readBuf;
return result;
}
/*
* Utility function that expands zip/gzip "deflate" compressed data
* into a buffer.
*
* (This is a clone of the previous function, but it takes a FILE* instead
* of an fd. We could pass fileno(fd) to the above, but we can run into
* trouble when "fp" has a different notion of what fd's file position is.)
*
* "fp" is an open file positioned at the start of the "deflate" data
* "buf" must hold at least "uncompressedLen" bytes.
*/
/*static*/ bool ZipUtils::inflateToBuffer(FILE* fp, void* buf,
long uncompressedLen, long compressedLen)
{
bool result = false;
const unsigned long kReadBufSize = 32768;
unsigned char* readBuf = NULL;
z_stream zstream;
int zerr;
unsigned long compRemaining;
assert(uncompressedLen >= 0);
assert(compressedLen >= 0);
readBuf = new unsigned char[kReadBufSize];
if (readBuf == NULL)
goto bail;
compRemaining = compressedLen;
/*
* Initialize the zlib stream.
*/
memset(&zstream, 0, sizeof(zstream));
zstream.zalloc = Z_NULL;
zstream.zfree = Z_NULL;
zstream.opaque = Z_NULL;
zstream.next_in = NULL;
zstream.avail_in = 0;
zstream.next_out = (Bytef*) buf;
zstream.avail_out = uncompressedLen;
zstream.data_type = Z_UNKNOWN;
/*
* Use the undocumented "negative window bits" feature to tell zlib
* that there's no zlib header waiting for it.
*/
zerr = inflateInit2(&zstream, -MAX_WBITS);
if (zerr != Z_OK) {
if (zerr == Z_VERSION_ERROR) {
ALOGE("Installed zlib is not compatible with linked version (%s)\n",
ZLIB_VERSION);
} else {
ALOGE("Call to inflateInit2 failed (zerr=%d)\n", zerr);
}
goto bail;
}
/*
* Loop while we have data.
*/
do {
unsigned long getSize;
/* read as much as we can */
if (zstream.avail_in == 0) {
getSize = (compRemaining > kReadBufSize) ?
kReadBufSize : compRemaining;
ALOGV("+++ reading %ld bytes (%ld left)\n",
getSize, compRemaining);
int cc = fread(readBuf, 1, getSize, fp);
if (cc != (int) getSize) {
ALOGD("inflate read failed (%d vs %ld)\n",
cc, getSize);
goto z_bail;
}
compRemaining -= getSize;
zstream.next_in = readBuf;
zstream.avail_in = getSize;
}
/* uncompress the data */
zerr = inflate(&zstream, Z_NO_FLUSH);
if (zerr != Z_OK && zerr != Z_STREAM_END) {
ALOGD("zlib inflate call failed (zerr=%d)\n", zerr);
goto z_bail;
}
/* output buffer holds all, so no need to write the output */
} while (zerr == Z_OK);
assert(zerr == Z_STREAM_END); /* other errors should've been caught */
if ((long) zstream.total_out != uncompressedLen) {
ALOGW("Size mismatch on inflated file (%ld vs %ld)\n",
zstream.total_out, uncompressedLen);
goto z_bail;
}
// success!
result = true;
z_bail:
inflateEnd(&zstream); /* free up any allocated structures */
bail:
delete[] readBuf;
return result;
}
/*
* Look at the contents of a gzip archive. We want to know where the
* data starts, and how long it will be after it is uncompressed.
*
* We expect to find the CRC and length as the last 8 bytes on the file.
* This is a pretty reasonable thing to expect for locally-compressed
* files, but there's a small chance that some extra padding got thrown
* on (the man page talks about compressed data written to tape). We
* don't currently deal with that here. If "gzip -l" whines, we're going
* to fail too.
*
* On exit, "fp" is pointing at the start of the compressed data.
*/
/*static*/ bool ZipUtils::examineGzip(FILE* fp, int* pCompressionMethod,
long* pUncompressedLen, long* pCompressedLen, unsigned long* pCRC32)
{
enum { // flags
FTEXT = 0x01,
FHCRC = 0x02,
FEXTRA = 0x04,
FNAME = 0x08,
FCOMMENT = 0x10,
};
int ic;
int method, flags;
int i;
ic = getc(fp);
if (ic != 0x1f || getc(fp) != 0x8b)
return false; // not gzip
method = getc(fp);
flags = getc(fp);
/* quick sanity checks */
if (method == EOF || flags == EOF)
return false;
if (method != ZipFileRO::kCompressDeflated)
return false;
/* skip over 4 bytes of mod time, 1 byte XFL, 1 byte OS */
for (i = 0; i < 6; i++)
(void) getc(fp);
/* consume "extra" field, if present */
if ((flags & FEXTRA) != 0) {
int len;
len = getc(fp);
len |= getc(fp) << 8;
while (len-- && getc(fp) != EOF)
;
}
/* consume filename, if present */
if ((flags & FNAME) != 0) {
do {
ic = getc(fp);
} while (ic != 0 && ic != EOF);
}
/* consume comment, if present */
if ((flags & FCOMMENT) != 0) {
do {
ic = getc(fp);
} while (ic != 0 && ic != EOF);
}
/* consume 16-bit header CRC, if present */
if ((flags & FHCRC) != 0) {
(void) getc(fp);
(void) getc(fp);
}
if (feof(fp) || ferror(fp))
return false;
/* seek to the end; CRC and length are in the last 8 bytes */
long curPosn = ftell(fp);
unsigned char buf[8];
fseek(fp, -8, SEEK_END);
*pCompressedLen = ftell(fp) - curPosn;
if (fread(buf, 1, 8, fp) != 8)
return false;
/* seek back to start of compressed data */
fseek(fp, curPosn, SEEK_SET);
*pCompressionMethod = method;
*pCRC32 = ZipFileRO::get4LE(&buf[0]);
*pUncompressedLen = ZipFileRO::get4LE(&buf[4]);
return true;
}

View File

@ -9,6 +9,7 @@ test_src_files := \
Looper_test.cpp \
String8_test.cpp \
Unicode_test.cpp \
ZipFileRO_test.cpp
shared_libraries := \
libz \

View File

@ -0,0 +1,64 @@
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define LOG_TAG "ZipFileRO_test"
#include <utils/Log.h>
#include <utils/ZipFileRO.h>
#include <gtest/gtest.h>
#include <fcntl.h>
#include <string.h>
namespace android {
class ZipFileROTest : public testing::Test {
protected:
virtual void SetUp() {
}
virtual void TearDown() {
}
};
TEST_F(ZipFileROTest, ZipTimeConvertSuccess) {
struct tm t;
// 2011-06-29 14:40:40
long when = 0x3EDD7514;
ZipFileRO::zipTimeToTimespec(when, &t);
EXPECT_EQ(2011, t.tm_year + 1900)
<< "Year was improperly converted.";
EXPECT_EQ(6, t.tm_mon)
<< "Month was improperly converted.";
EXPECT_EQ(29, t.tm_mday)
<< "Day was improperly converted.";
EXPECT_EQ(14, t.tm_hour)
<< "Hour was improperly converted.";
EXPECT_EQ(40, t.tm_min)
<< "Minute was improperly converted.";
EXPECT_EQ(40, t.tm_sec)
<< "Second was improperly converted.";
}
}