frameworks/base refactoring
create the new libandroidfw from parts of libui and libutils Change-Id: I1584995616fff5d527a2aba63921b682a6194d58
This commit is contained in:
parent
6c865b58aa
commit
eb63cf29db
15 changed files with 0 additions and 12610 deletions
|
|
@ -18,9 +18,6 @@ LOCAL_PATH:= $(call my-dir)
|
|||
# and once for the device.
|
||||
|
||||
commonSources:= \
|
||||
Asset.cpp \
|
||||
AssetDir.cpp \
|
||||
AssetManager.cpp \
|
||||
BasicHashtable.cpp \
|
||||
BlobCache.cpp \
|
||||
BufferedTextOutput.cpp \
|
||||
|
|
@ -29,14 +26,11 @@ commonSources:= \
|
|||
FileMap.cpp \
|
||||
Flattenable.cpp \
|
||||
LinearTransform.cpp \
|
||||
ObbFile.cpp \
|
||||
PropertyMap.cpp \
|
||||
RefBase.cpp \
|
||||
ResourceTypes.cpp \
|
||||
SharedBuffer.cpp \
|
||||
Static.cpp \
|
||||
StopWatch.cpp \
|
||||
StreamingZipInflater.cpp \
|
||||
String8.cpp \
|
||||
String16.cpp \
|
||||
StringArray.cpp \
|
||||
|
|
@ -47,9 +41,6 @@ commonSources:= \
|
|||
Tokenizer.cpp \
|
||||
Unicode.cpp \
|
||||
VectorImpl.cpp \
|
||||
ZipFileCRO.cpp \
|
||||
ZipFileRO.cpp \
|
||||
ZipUtils.cpp \
|
||||
misc.cpp
|
||||
|
||||
|
||||
|
|
@ -63,7 +54,6 @@ LOCAL_SRC_FILES:= $(commonSources)
|
|||
LOCAL_MODULE:= libutils
|
||||
|
||||
LOCAL_CFLAGS += -DLIBUTILS_NATIVE=1 $(TOOL_CFLAGS)
|
||||
LOCAL_C_INCLUDES += external/zlib
|
||||
|
||||
ifeq ($(HOST_OS),windows)
|
||||
ifeq ($(strip $(USE_CYGWIN),),)
|
||||
|
|
@ -79,7 +69,6 @@ endif
|
|||
include $(BUILD_HOST_STATIC_LIBRARY)
|
||||
|
||||
|
||||
|
||||
# For the device
|
||||
# =====================================================
|
||||
include $(CLEAR_VARS)
|
||||
|
|
@ -88,8 +77,6 @@ include $(CLEAR_VARS)
|
|||
# we have the common sources, plus some device-specific stuff
|
||||
LOCAL_SRC_FILES:= \
|
||||
$(commonSources) \
|
||||
BackupData.cpp \
|
||||
BackupHelpers.cpp \
|
||||
Looper.cpp
|
||||
|
||||
ifeq ($(TARGET_OS),linux)
|
||||
|
|
@ -97,14 +84,11 @@ LOCAL_LDLIBS += -lrt -ldl
|
|||
endif
|
||||
|
||||
LOCAL_C_INCLUDES += \
|
||||
external/zlib \
|
||||
external/icu4c/common \
|
||||
bionic/libc/private
|
||||
|
||||
LOCAL_LDLIBS += -lpthread
|
||||
|
||||
LOCAL_SHARED_LIBRARIES := \
|
||||
libz \
|
||||
liblog \
|
||||
libcutils \
|
||||
libdl \
|
||||
|
|
@ -113,19 +97,6 @@ LOCAL_SHARED_LIBRARIES := \
|
|||
LOCAL_MODULE:= libutils
|
||||
include $(BUILD_SHARED_LIBRARY)
|
||||
|
||||
ifeq ($(TARGET_OS),linux)
|
||||
include $(CLEAR_VARS)
|
||||
LOCAL_C_INCLUDES += \
|
||||
external/zlib \
|
||||
external/icu4c/common \
|
||||
bionic/libc/private
|
||||
LOCAL_LDLIBS := -lrt -ldl -lpthread
|
||||
LOCAL_MODULE := libutils
|
||||
LOCAL_SRC_FILES := $(commonSources) BackupData.cpp BackupHelpers.cpp
|
||||
include $(BUILD_STATIC_LIBRARY)
|
||||
endif
|
||||
|
||||
|
||||
# Include subdirectory makefiles
|
||||
# ============================================================
|
||||
|
||||
|
|
|
|||
|
|
@ -1,897 +0,0 @@
|
|||
/*
|
||||
* Copyright (C) 2006 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.
|
||||
*/
|
||||
|
||||
//
|
||||
// Provide access to a read-only asset.
|
||||
//
|
||||
|
||||
#define LOG_TAG "asset"
|
||||
//#define NDEBUG 0
|
||||
|
||||
#include <androidfw/Asset.h>
|
||||
#include <androidfw/StreamingZipInflater.h>
|
||||
#include <androidfw/ZipFileRO.h>
|
||||
#include <androidfw/ZipUtils.h>
|
||||
#include <utils/Atomic.h>
|
||||
#include <utils/FileMap.h>
|
||||
#include <utils/Log.h>
|
||||
#include <utils/threads.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <memory.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
using namespace android;
|
||||
|
||||
#ifndef O_BINARY
|
||||
# define O_BINARY 0
|
||||
#endif
|
||||
|
||||
static Mutex gAssetLock;
|
||||
static int32_t gCount = 0;
|
||||
static Asset* gHead = NULL;
|
||||
static Asset* gTail = NULL;
|
||||
|
||||
int32_t Asset::getGlobalCount()
|
||||
{
|
||||
AutoMutex _l(gAssetLock);
|
||||
return gCount;
|
||||
}
|
||||
|
||||
String8 Asset::getAssetAllocations()
|
||||
{
|
||||
AutoMutex _l(gAssetLock);
|
||||
String8 res;
|
||||
Asset* cur = gHead;
|
||||
while (cur != NULL) {
|
||||
if (cur->isAllocated()) {
|
||||
res.append(" ");
|
||||
res.append(cur->getAssetSource());
|
||||
off64_t size = (cur->getLength()+512)/1024;
|
||||
char buf[64];
|
||||
sprintf(buf, ": %dK\n", (int)size);
|
||||
res.append(buf);
|
||||
}
|
||||
cur = cur->mNext;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
Asset::Asset(void)
|
||||
: mAccessMode(ACCESS_UNKNOWN)
|
||||
{
|
||||
AutoMutex _l(gAssetLock);
|
||||
gCount++;
|
||||
mNext = mPrev = NULL;
|
||||
if (gTail == NULL) {
|
||||
gHead = gTail = this;
|
||||
} else {
|
||||
mPrev = gTail;
|
||||
gTail->mNext = this;
|
||||
gTail = this;
|
||||
}
|
||||
//ALOGI("Creating Asset %p #%d\n", this, gCount);
|
||||
}
|
||||
|
||||
Asset::~Asset(void)
|
||||
{
|
||||
AutoMutex _l(gAssetLock);
|
||||
gCount--;
|
||||
if (gHead == this) {
|
||||
gHead = mNext;
|
||||
}
|
||||
if (gTail == this) {
|
||||
gTail = mPrev;
|
||||
}
|
||||
if (mNext != NULL) {
|
||||
mNext->mPrev = mPrev;
|
||||
}
|
||||
if (mPrev != NULL) {
|
||||
mPrev->mNext = mNext;
|
||||
}
|
||||
mNext = mPrev = NULL;
|
||||
//ALOGI("Destroying Asset in %p #%d\n", this, gCount);
|
||||
}
|
||||
|
||||
/*
|
||||
* Create a new Asset from a file on disk. There is a fair chance that
|
||||
* the file doesn't actually exist.
|
||||
*
|
||||
* We can use "mode" to decide how we want to go about it.
|
||||
*/
|
||||
/*static*/ Asset* Asset::createFromFile(const char* fileName, AccessMode mode)
|
||||
{
|
||||
_FileAsset* pAsset;
|
||||
status_t result;
|
||||
off64_t length;
|
||||
int fd;
|
||||
|
||||
fd = open(fileName, O_RDONLY | O_BINARY);
|
||||
if (fd < 0)
|
||||
return NULL;
|
||||
|
||||
/*
|
||||
* Under Linux, the lseek fails if we actually opened a directory. To
|
||||
* be correct we should test the file type explicitly, but since we
|
||||
* always open things read-only it doesn't really matter, so there's
|
||||
* no value in incurring the extra overhead of an fstat() call.
|
||||
*/
|
||||
// TODO(kroot): replace this with fstat despite the plea above.
|
||||
#if 1
|
||||
length = lseek64(fd, 0, SEEK_END);
|
||||
if (length < 0) {
|
||||
::close(fd);
|
||||
return NULL;
|
||||
}
|
||||
(void) lseek64(fd, 0, SEEK_SET);
|
||||
#else
|
||||
struct stat st;
|
||||
if (fstat(fd, &st) < 0) {
|
||||
::close(fd);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!S_ISREG(st.st_mode)) {
|
||||
::close(fd);
|
||||
return NULL;
|
||||
}
|
||||
#endif
|
||||
|
||||
pAsset = new _FileAsset;
|
||||
result = pAsset->openChunk(fileName, fd, 0, length);
|
||||
if (result != NO_ERROR) {
|
||||
delete pAsset;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
pAsset->mAccessMode = mode;
|
||||
return pAsset;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Create a new Asset from a compressed file on disk. There is a fair chance
|
||||
* that the file doesn't actually exist.
|
||||
*
|
||||
* We currently support gzip files. We might want to handle .bz2 someday.
|
||||
*/
|
||||
/*static*/ Asset* Asset::createFromCompressedFile(const char* fileName,
|
||||
AccessMode mode)
|
||||
{
|
||||
_CompressedAsset* pAsset;
|
||||
status_t result;
|
||||
off64_t fileLen;
|
||||
bool scanResult;
|
||||
long offset;
|
||||
int method;
|
||||
long uncompressedLen, compressedLen;
|
||||
int fd;
|
||||
|
||||
fd = open(fileName, O_RDONLY | O_BINARY);
|
||||
if (fd < 0)
|
||||
return NULL;
|
||||
|
||||
fileLen = lseek(fd, 0, SEEK_END);
|
||||
if (fileLen < 0) {
|
||||
::close(fd);
|
||||
return NULL;
|
||||
}
|
||||
(void) lseek(fd, 0, SEEK_SET);
|
||||
|
||||
/* want buffered I/O for the file scan; must dup so fclose() is safe */
|
||||
FILE* fp = fdopen(dup(fd), "rb");
|
||||
if (fp == NULL) {
|
||||
::close(fd);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
unsigned long crc32;
|
||||
scanResult = ZipUtils::examineGzip(fp, &method, &uncompressedLen,
|
||||
&compressedLen, &crc32);
|
||||
offset = ftell(fp);
|
||||
fclose(fp);
|
||||
if (!scanResult) {
|
||||
ALOGD("File '%s' is not in gzip format\n", fileName);
|
||||
::close(fd);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
pAsset = new _CompressedAsset;
|
||||
result = pAsset->openChunk(fd, offset, method, uncompressedLen,
|
||||
compressedLen);
|
||||
if (result != NO_ERROR) {
|
||||
delete pAsset;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
pAsset->mAccessMode = mode;
|
||||
return pAsset;
|
||||
}
|
||||
|
||||
|
||||
#if 0
|
||||
/*
|
||||
* Create a new Asset from part of an open file.
|
||||
*/
|
||||
/*static*/ Asset* Asset::createFromFileSegment(int fd, off64_t offset,
|
||||
size_t length, AccessMode mode)
|
||||
{
|
||||
_FileAsset* pAsset;
|
||||
status_t result;
|
||||
|
||||
pAsset = new _FileAsset;
|
||||
result = pAsset->openChunk(NULL, fd, offset, length);
|
||||
if (result != NO_ERROR)
|
||||
return NULL;
|
||||
|
||||
pAsset->mAccessMode = mode;
|
||||
return pAsset;
|
||||
}
|
||||
|
||||
/*
|
||||
* Create a new Asset from compressed data in an open file.
|
||||
*/
|
||||
/*static*/ Asset* Asset::createFromCompressedData(int fd, off64_t offset,
|
||||
int compressionMethod, size_t uncompressedLen, size_t compressedLen,
|
||||
AccessMode mode)
|
||||
{
|
||||
_CompressedAsset* pAsset;
|
||||
status_t result;
|
||||
|
||||
pAsset = new _CompressedAsset;
|
||||
result = pAsset->openChunk(fd, offset, compressionMethod,
|
||||
uncompressedLen, compressedLen);
|
||||
if (result != NO_ERROR)
|
||||
return NULL;
|
||||
|
||||
pAsset->mAccessMode = mode;
|
||||
return pAsset;
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Create a new Asset from a memory mapping.
|
||||
*/
|
||||
/*static*/ Asset* Asset::createFromUncompressedMap(FileMap* dataMap,
|
||||
AccessMode mode)
|
||||
{
|
||||
_FileAsset* pAsset;
|
||||
status_t result;
|
||||
|
||||
pAsset = new _FileAsset;
|
||||
result = pAsset->openChunk(dataMap);
|
||||
if (result != NO_ERROR)
|
||||
return NULL;
|
||||
|
||||
pAsset->mAccessMode = mode;
|
||||
return pAsset;
|
||||
}
|
||||
|
||||
/*
|
||||
* Create a new Asset from compressed data in a memory mapping.
|
||||
*/
|
||||
/*static*/ Asset* Asset::createFromCompressedMap(FileMap* dataMap,
|
||||
int method, size_t uncompressedLen, AccessMode mode)
|
||||
{
|
||||
_CompressedAsset* pAsset;
|
||||
status_t result;
|
||||
|
||||
pAsset = new _CompressedAsset;
|
||||
result = pAsset->openChunk(dataMap, method, uncompressedLen);
|
||||
if (result != NO_ERROR)
|
||||
return NULL;
|
||||
|
||||
pAsset->mAccessMode = mode;
|
||||
return pAsset;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Do generic seek() housekeeping. Pass in the offset/whence values from
|
||||
* the seek request, along with the current chunk offset and the chunk
|
||||
* length.
|
||||
*
|
||||
* Returns the new chunk offset, or -1 if the seek is illegal.
|
||||
*/
|
||||
off64_t Asset::handleSeek(off64_t offset, int whence, off64_t curPosn, off64_t maxPosn)
|
||||
{
|
||||
off64_t newOffset;
|
||||
|
||||
switch (whence) {
|
||||
case SEEK_SET:
|
||||
newOffset = offset;
|
||||
break;
|
||||
case SEEK_CUR:
|
||||
newOffset = curPosn + offset;
|
||||
break;
|
||||
case SEEK_END:
|
||||
newOffset = maxPosn + offset;
|
||||
break;
|
||||
default:
|
||||
ALOGW("unexpected whence %d\n", whence);
|
||||
// this was happening due to an off64_t size mismatch
|
||||
assert(false);
|
||||
return (off64_t) -1;
|
||||
}
|
||||
|
||||
if (newOffset < 0 || newOffset > maxPosn) {
|
||||
ALOGW("seek out of range: want %ld, end=%ld\n",
|
||||
(long) newOffset, (long) maxPosn);
|
||||
return (off64_t) -1;
|
||||
}
|
||||
|
||||
return newOffset;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* ===========================================================================
|
||||
* _FileAsset
|
||||
* ===========================================================================
|
||||
*/
|
||||
|
||||
/*
|
||||
* Constructor.
|
||||
*/
|
||||
_FileAsset::_FileAsset(void)
|
||||
: mStart(0), mLength(0), mOffset(0), mFp(NULL), mFileName(NULL), mMap(NULL), mBuf(NULL)
|
||||
{
|
||||
}
|
||||
|
||||
/*
|
||||
* Destructor. Release resources.
|
||||
*/
|
||||
_FileAsset::~_FileAsset(void)
|
||||
{
|
||||
close();
|
||||
}
|
||||
|
||||
/*
|
||||
* Operate on a chunk of an uncompressed file.
|
||||
*
|
||||
* Zero-length chunks are allowed.
|
||||
*/
|
||||
status_t _FileAsset::openChunk(const char* fileName, int fd, off64_t offset, size_t length)
|
||||
{
|
||||
assert(mFp == NULL); // no reopen
|
||||
assert(mMap == NULL);
|
||||
assert(fd >= 0);
|
||||
assert(offset >= 0);
|
||||
|
||||
/*
|
||||
* Seek to end to get file length.
|
||||
*/
|
||||
off64_t fileLength;
|
||||
fileLength = lseek64(fd, 0, SEEK_END);
|
||||
if (fileLength == (off64_t) -1) {
|
||||
// probably a bad file descriptor
|
||||
ALOGD("failed lseek (errno=%d)\n", errno);
|
||||
return UNKNOWN_ERROR;
|
||||
}
|
||||
|
||||
if ((off64_t) (offset + length) > fileLength) {
|
||||
ALOGD("start (%ld) + len (%ld) > end (%ld)\n",
|
||||
(long) offset, (long) length, (long) fileLength);
|
||||
return BAD_INDEX;
|
||||
}
|
||||
|
||||
/* after fdopen, the fd will be closed on fclose() */
|
||||
mFp = fdopen(fd, "rb");
|
||||
if (mFp == NULL)
|
||||
return UNKNOWN_ERROR;
|
||||
|
||||
mStart = offset;
|
||||
mLength = length;
|
||||
assert(mOffset == 0);
|
||||
|
||||
/* seek the FILE* to the start of chunk */
|
||||
if (fseek(mFp, mStart, SEEK_SET) != 0) {
|
||||
assert(false);
|
||||
}
|
||||
|
||||
mFileName = fileName != NULL ? strdup(fileName) : NULL;
|
||||
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
/*
|
||||
* Create the chunk from the map.
|
||||
*/
|
||||
status_t _FileAsset::openChunk(FileMap* dataMap)
|
||||
{
|
||||
assert(mFp == NULL); // no reopen
|
||||
assert(mMap == NULL);
|
||||
assert(dataMap != NULL);
|
||||
|
||||
mMap = dataMap;
|
||||
mStart = -1; // not used
|
||||
mLength = dataMap->getDataLength();
|
||||
assert(mOffset == 0);
|
||||
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
/*
|
||||
* Read a chunk of data.
|
||||
*/
|
||||
ssize_t _FileAsset::read(void* buf, size_t count)
|
||||
{
|
||||
size_t maxLen;
|
||||
size_t actual;
|
||||
|
||||
assert(mOffset >= 0 && mOffset <= mLength);
|
||||
|
||||
if (getAccessMode() == ACCESS_BUFFER) {
|
||||
/*
|
||||
* On first access, read or map the entire file. The caller has
|
||||
* requested buffer access, either because they're going to be
|
||||
* using the buffer or because what they're doing has appropriate
|
||||
* performance needs and access patterns.
|
||||
*/
|
||||
if (mBuf == NULL)
|
||||
getBuffer(false);
|
||||
}
|
||||
|
||||
/* adjust count if we're near EOF */
|
||||
maxLen = mLength - mOffset;
|
||||
if (count > maxLen)
|
||||
count = maxLen;
|
||||
|
||||
if (!count)
|
||||
return 0;
|
||||
|
||||
if (mMap != NULL) {
|
||||
/* copy from mapped area */
|
||||
//printf("map read\n");
|
||||
memcpy(buf, (char*)mMap->getDataPtr() + mOffset, count);
|
||||
actual = count;
|
||||
} else if (mBuf != NULL) {
|
||||
/* copy from buffer */
|
||||
//printf("buf read\n");
|
||||
memcpy(buf, (char*)mBuf + mOffset, count);
|
||||
actual = count;
|
||||
} else {
|
||||
/* read from the file */
|
||||
//printf("file read\n");
|
||||
if (ftell(mFp) != mStart + mOffset) {
|
||||
ALOGE("Hosed: %ld != %ld+%ld\n",
|
||||
ftell(mFp), (long) mStart, (long) mOffset);
|
||||
assert(false);
|
||||
}
|
||||
|
||||
/*
|
||||
* This returns 0 on error or eof. We need to use ferror() or feof()
|
||||
* to tell the difference, but we don't currently have those on the
|
||||
* device. However, we know how much data is *supposed* to be in the
|
||||
* file, so if we don't read the full amount we know something is
|
||||
* hosed.
|
||||
*/
|
||||
actual = fread(buf, 1, count, mFp);
|
||||
if (actual == 0) // something failed -- I/O error?
|
||||
return -1;
|
||||
|
||||
assert(actual == count);
|
||||
}
|
||||
|
||||
mOffset += actual;
|
||||
return actual;
|
||||
}
|
||||
|
||||
/*
|
||||
* Seek to a new position.
|
||||
*/
|
||||
off64_t _FileAsset::seek(off64_t offset, int whence)
|
||||
{
|
||||
off64_t newPosn;
|
||||
off64_t actualOffset;
|
||||
|
||||
// compute new position within chunk
|
||||
newPosn = handleSeek(offset, whence, mOffset, mLength);
|
||||
if (newPosn == (off64_t) -1)
|
||||
return newPosn;
|
||||
|
||||
actualOffset = mStart + newPosn;
|
||||
|
||||
if (mFp != NULL) {
|
||||
if (fseek(mFp, (long) actualOffset, SEEK_SET) != 0)
|
||||
return (off64_t) -1;
|
||||
}
|
||||
|
||||
mOffset = actualOffset - mStart;
|
||||
return mOffset;
|
||||
}
|
||||
|
||||
/*
|
||||
* Close the asset.
|
||||
*/
|
||||
void _FileAsset::close(void)
|
||||
{
|
||||
if (mMap != NULL) {
|
||||
mMap->release();
|
||||
mMap = NULL;
|
||||
}
|
||||
if (mBuf != NULL) {
|
||||
delete[] mBuf;
|
||||
mBuf = NULL;
|
||||
}
|
||||
|
||||
if (mFileName != NULL) {
|
||||
free(mFileName);
|
||||
mFileName = NULL;
|
||||
}
|
||||
|
||||
if (mFp != NULL) {
|
||||
// can only be NULL when called from destructor
|
||||
// (otherwise we would never return this object)
|
||||
fclose(mFp);
|
||||
mFp = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Return a read-only pointer to a buffer.
|
||||
*
|
||||
* We can either read the whole thing in or map the relevant piece of
|
||||
* the source file. Ideally a map would be established at a higher
|
||||
* level and we'd be using a different object, but we didn't, so we
|
||||
* deal with it here.
|
||||
*/
|
||||
const void* _FileAsset::getBuffer(bool wordAligned)
|
||||
{
|
||||
/* subsequent requests just use what we did previously */
|
||||
if (mBuf != NULL)
|
||||
return mBuf;
|
||||
if (mMap != NULL) {
|
||||
if (!wordAligned) {
|
||||
return mMap->getDataPtr();
|
||||
}
|
||||
return ensureAlignment(mMap);
|
||||
}
|
||||
|
||||
assert(mFp != NULL);
|
||||
|
||||
if (mLength < kReadVsMapThreshold) {
|
||||
unsigned char* buf;
|
||||
long allocLen;
|
||||
|
||||
/* zero-length files are allowed; not sure about zero-len allocs */
|
||||
/* (works fine with gcc + x86linux) */
|
||||
allocLen = mLength;
|
||||
if (mLength == 0)
|
||||
allocLen = 1;
|
||||
|
||||
buf = new unsigned char[allocLen];
|
||||
if (buf == NULL) {
|
||||
ALOGE("alloc of %ld bytes failed\n", (long) allocLen);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ALOGV("Asset %p allocating buffer size %d (smaller than threshold)", this, (int)allocLen);
|
||||
if (mLength > 0) {
|
||||
long oldPosn = ftell(mFp);
|
||||
fseek(mFp, mStart, SEEK_SET);
|
||||
if (fread(buf, 1, mLength, mFp) != (size_t) mLength) {
|
||||
ALOGE("failed reading %ld bytes\n", (long) mLength);
|
||||
delete[] buf;
|
||||
return NULL;
|
||||
}
|
||||
fseek(mFp, oldPosn, SEEK_SET);
|
||||
}
|
||||
|
||||
ALOGV(" getBuffer: loaded into buffer\n");
|
||||
|
||||
mBuf = buf;
|
||||
return mBuf;
|
||||
} else {
|
||||
FileMap* map;
|
||||
|
||||
map = new FileMap;
|
||||
if (!map->create(NULL, fileno(mFp), mStart, mLength, true)) {
|
||||
map->release();
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ALOGV(" getBuffer: mapped\n");
|
||||
|
||||
mMap = map;
|
||||
if (!wordAligned) {
|
||||
return mMap->getDataPtr();
|
||||
}
|
||||
return ensureAlignment(mMap);
|
||||
}
|
||||
}
|
||||
|
||||
int _FileAsset::openFileDescriptor(off64_t* outStart, off64_t* outLength) const
|
||||
{
|
||||
if (mMap != NULL) {
|
||||
const char* fname = mMap->getFileName();
|
||||
if (fname == NULL) {
|
||||
fname = mFileName;
|
||||
}
|
||||
if (fname == NULL) {
|
||||
return -1;
|
||||
}
|
||||
*outStart = mMap->getDataOffset();
|
||||
*outLength = mMap->getDataLength();
|
||||
return open(fname, O_RDONLY | O_BINARY);
|
||||
}
|
||||
if (mFileName == NULL) {
|
||||
return -1;
|
||||
}
|
||||
*outStart = mStart;
|
||||
*outLength = mLength;
|
||||
return open(mFileName, O_RDONLY | O_BINARY);
|
||||
}
|
||||
|
||||
const void* _FileAsset::ensureAlignment(FileMap* map)
|
||||
{
|
||||
void* data = map->getDataPtr();
|
||||
if ((((size_t)data)&0x3) == 0) {
|
||||
// We can return this directly if it is aligned on a word
|
||||
// boundary.
|
||||
ALOGV("Returning aligned FileAsset %p (%s).", this,
|
||||
getAssetSource());
|
||||
return data;
|
||||
}
|
||||
// If not aligned on a word boundary, then we need to copy it into
|
||||
// our own buffer.
|
||||
ALOGV("Copying FileAsset %p (%s) to buffer size %d to make it aligned.", this,
|
||||
getAssetSource(), (int)mLength);
|
||||
unsigned char* buf = new unsigned char[mLength];
|
||||
if (buf == NULL) {
|
||||
ALOGE("alloc of %ld bytes failed\n", (long) mLength);
|
||||
return NULL;
|
||||
}
|
||||
memcpy(buf, data, mLength);
|
||||
mBuf = buf;
|
||||
return buf;
|
||||
}
|
||||
|
||||
/*
|
||||
* ===========================================================================
|
||||
* _CompressedAsset
|
||||
* ===========================================================================
|
||||
*/
|
||||
|
||||
/*
|
||||
* Constructor.
|
||||
*/
|
||||
_CompressedAsset::_CompressedAsset(void)
|
||||
: mStart(0), mCompressedLen(0), mUncompressedLen(0), mOffset(0),
|
||||
mMap(NULL), mFd(-1), mZipInflater(NULL), mBuf(NULL)
|
||||
{
|
||||
}
|
||||
|
||||
/*
|
||||
* Destructor. Release resources.
|
||||
*/
|
||||
_CompressedAsset::~_CompressedAsset(void)
|
||||
{
|
||||
close();
|
||||
}
|
||||
|
||||
/*
|
||||
* Open a chunk of compressed data inside a file.
|
||||
*
|
||||
* This currently just sets up some values and returns. On the first
|
||||
* read, we expand the entire file into a buffer and return data from it.
|
||||
*/
|
||||
status_t _CompressedAsset::openChunk(int fd, off64_t offset,
|
||||
int compressionMethod, size_t uncompressedLen, size_t compressedLen)
|
||||
{
|
||||
assert(mFd < 0); // no re-open
|
||||
assert(mMap == NULL);
|
||||
assert(fd >= 0);
|
||||
assert(offset >= 0);
|
||||
assert(compressedLen > 0);
|
||||
|
||||
if (compressionMethod != ZipFileRO::kCompressDeflated) {
|
||||
assert(false);
|
||||
return UNKNOWN_ERROR;
|
||||
}
|
||||
|
||||
mStart = offset;
|
||||
mCompressedLen = compressedLen;
|
||||
mUncompressedLen = uncompressedLen;
|
||||
assert(mOffset == 0);
|
||||
mFd = fd;
|
||||
assert(mBuf == NULL);
|
||||
|
||||
if (uncompressedLen > StreamingZipInflater::OUTPUT_CHUNK_SIZE) {
|
||||
mZipInflater = new StreamingZipInflater(mFd, offset, uncompressedLen, compressedLen);
|
||||
}
|
||||
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
/*
|
||||
* Open a chunk of compressed data in a mapped region.
|
||||
*
|
||||
* Nothing is expanded until the first read call.
|
||||
*/
|
||||
status_t _CompressedAsset::openChunk(FileMap* dataMap, int compressionMethod,
|
||||
size_t uncompressedLen)
|
||||
{
|
||||
assert(mFd < 0); // no re-open
|
||||
assert(mMap == NULL);
|
||||
assert(dataMap != NULL);
|
||||
|
||||
if (compressionMethod != ZipFileRO::kCompressDeflated) {
|
||||
assert(false);
|
||||
return UNKNOWN_ERROR;
|
||||
}
|
||||
|
||||
mMap = dataMap;
|
||||
mStart = -1; // not used
|
||||
mCompressedLen = dataMap->getDataLength();
|
||||
mUncompressedLen = uncompressedLen;
|
||||
assert(mOffset == 0);
|
||||
|
||||
if (uncompressedLen > StreamingZipInflater::OUTPUT_CHUNK_SIZE) {
|
||||
mZipInflater = new StreamingZipInflater(dataMap, uncompressedLen);
|
||||
}
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
/*
|
||||
* Read data from a chunk of compressed data.
|
||||
*
|
||||
* [For now, that's just copying data out of a buffer.]
|
||||
*/
|
||||
ssize_t _CompressedAsset::read(void* buf, size_t count)
|
||||
{
|
||||
size_t maxLen;
|
||||
size_t actual;
|
||||
|
||||
assert(mOffset >= 0 && mOffset <= mUncompressedLen);
|
||||
|
||||
/* If we're relying on a streaming inflater, go through that */
|
||||
if (mZipInflater) {
|
||||
actual = mZipInflater->read(buf, count);
|
||||
} else {
|
||||
if (mBuf == NULL) {
|
||||
if (getBuffer(false) == NULL)
|
||||
return -1;
|
||||
}
|
||||
assert(mBuf != NULL);
|
||||
|
||||
/* adjust count if we're near EOF */
|
||||
maxLen = mUncompressedLen - mOffset;
|
||||
if (count > maxLen)
|
||||
count = maxLen;
|
||||
|
||||
if (!count)
|
||||
return 0;
|
||||
|
||||
/* copy from buffer */
|
||||
//printf("comp buf read\n");
|
||||
memcpy(buf, (char*)mBuf + mOffset, count);
|
||||
actual = count;
|
||||
}
|
||||
|
||||
mOffset += actual;
|
||||
return actual;
|
||||
}
|
||||
|
||||
/*
|
||||
* Handle a seek request.
|
||||
*
|
||||
* If we're working in a streaming mode, this is going to be fairly
|
||||
* expensive, because it requires plowing through a bunch of compressed
|
||||
* data.
|
||||
*/
|
||||
off64_t _CompressedAsset::seek(off64_t offset, int whence)
|
||||
{
|
||||
off64_t newPosn;
|
||||
|
||||
// compute new position within chunk
|
||||
newPosn = handleSeek(offset, whence, mOffset, mUncompressedLen);
|
||||
if (newPosn == (off64_t) -1)
|
||||
return newPosn;
|
||||
|
||||
if (mZipInflater) {
|
||||
mZipInflater->seekAbsolute(newPosn);
|
||||
}
|
||||
mOffset = newPosn;
|
||||
return mOffset;
|
||||
}
|
||||
|
||||
/*
|
||||
* Close the asset.
|
||||
*/
|
||||
void _CompressedAsset::close(void)
|
||||
{
|
||||
if (mMap != NULL) {
|
||||
mMap->release();
|
||||
mMap = NULL;
|
||||
}
|
||||
|
||||
delete[] mBuf;
|
||||
mBuf = NULL;
|
||||
|
||||
delete mZipInflater;
|
||||
mZipInflater = NULL;
|
||||
|
||||
if (mFd > 0) {
|
||||
::close(mFd);
|
||||
mFd = -1;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Get a pointer to a read-only buffer of data.
|
||||
*
|
||||
* The first time this is called, we expand the compressed data into a
|
||||
* buffer.
|
||||
*/
|
||||
const void* _CompressedAsset::getBuffer(bool wordAligned)
|
||||
{
|
||||
unsigned char* buf = NULL;
|
||||
|
||||
if (mBuf != NULL)
|
||||
return mBuf;
|
||||
|
||||
/*
|
||||
* Allocate a buffer and read the file into it.
|
||||
*/
|
||||
buf = new unsigned char[mUncompressedLen];
|
||||
if (buf == NULL) {
|
||||
ALOGW("alloc %ld bytes failed\n", (long) mUncompressedLen);
|
||||
goto bail;
|
||||
}
|
||||
|
||||
if (mMap != NULL) {
|
||||
if (!ZipFileRO::inflateBuffer(buf, mMap->getDataPtr(),
|
||||
mUncompressedLen, mCompressedLen))
|
||||
goto bail;
|
||||
} else {
|
||||
assert(mFd >= 0);
|
||||
|
||||
/*
|
||||
* Seek to the start of the compressed data.
|
||||
*/
|
||||
if (lseek(mFd, mStart, SEEK_SET) != mStart)
|
||||
goto bail;
|
||||
|
||||
/*
|
||||
* Expand the data into it.
|
||||
*/
|
||||
if (!ZipUtils::inflateToBuffer(mFd, buf, mUncompressedLen,
|
||||
mCompressedLen))
|
||||
goto bail;
|
||||
}
|
||||
|
||||
/*
|
||||
* Success - now that we have the full asset in RAM we
|
||||
* no longer need the streaming inflater
|
||||
*/
|
||||
delete mZipInflater;
|
||||
mZipInflater = NULL;
|
||||
|
||||
mBuf = buf;
|
||||
buf = NULL;
|
||||
|
||||
bail:
|
||||
delete[] buf;
|
||||
return mBuf;
|
||||
}
|
||||
|
||||
|
|
@ -1,66 +0,0 @@
|
|||
/*
|
||||
* Copyright (C) 2006 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.
|
||||
*/
|
||||
|
||||
//
|
||||
// Provide access to a virtual directory in "asset space". Most of the
|
||||
// implementation is in the header file or in friend functions in
|
||||
// AssetManager.
|
||||
//
|
||||
#include <androidfw/AssetDir.h>
|
||||
|
||||
using namespace android;
|
||||
|
||||
|
||||
/*
|
||||
* Find a matching entry in a vector of FileInfo. Because it's sorted, we
|
||||
* can use a binary search.
|
||||
*
|
||||
* Assumes the vector is sorted in ascending order.
|
||||
*/
|
||||
/*static*/ int AssetDir::FileInfo::findEntry(const SortedVector<FileInfo>* pVector,
|
||||
const String8& fileName)
|
||||
{
|
||||
FileInfo tmpInfo;
|
||||
|
||||
tmpInfo.setFileName(fileName);
|
||||
return pVector->indexOf(tmpInfo);
|
||||
|
||||
#if 0 // don't need this after all (uses 1/2 compares of SortedVector though)
|
||||
int lo, hi, cur;
|
||||
|
||||
lo = 0;
|
||||
hi = pVector->size() -1;
|
||||
while (lo <= hi) {
|
||||
int cmp;
|
||||
|
||||
cur = (hi + lo) / 2;
|
||||
cmp = strcmp(pVector->itemAt(cur).getFileName(), fileName);
|
||||
if (cmp == 0) {
|
||||
/* match, bail */
|
||||
return cur;
|
||||
} else if (cmp < 0) {
|
||||
/* too low */
|
||||
lo = cur + 1;
|
||||
} else {
|
||||
/* too high */
|
||||
hi = cur -1;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load diff
|
|
@ -1,381 +0,0 @@
|
|||
/*
|
||||
* Copyright (C) 2009 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 "backup_data"
|
||||
|
||||
#include <androidfw/BackupHelpers.h>
|
||||
#include <utils/ByteOrder.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <cutils/log.h>
|
||||
|
||||
namespace android {
|
||||
|
||||
static const bool DEBUG = false;
|
||||
|
||||
/*
|
||||
* File Format (v1):
|
||||
*
|
||||
* All ints are stored little-endian.
|
||||
*
|
||||
* - An app_header_v1 struct.
|
||||
* - The name of the package, utf-8, null terminated, padded to 4-byte boundary.
|
||||
* - A sequence of zero or more key/value paires (entities), each with
|
||||
* - A entity_header_v1 struct
|
||||
* - The key, utf-8, null terminated, padded to 4-byte boundary.
|
||||
* - The value, padded to 4 byte boundary
|
||||
*/
|
||||
|
||||
const static int ROUND_UP[4] = { 0, 3, 2, 1 };
|
||||
|
||||
static inline size_t
|
||||
round_up(size_t n)
|
||||
{
|
||||
return n + ROUND_UP[n % 4];
|
||||
}
|
||||
|
||||
static inline size_t
|
||||
padding_extra(size_t n)
|
||||
{
|
||||
return ROUND_UP[n % 4];
|
||||
}
|
||||
|
||||
BackupDataWriter::BackupDataWriter(int fd)
|
||||
:m_fd(fd),
|
||||
m_status(NO_ERROR),
|
||||
m_pos(0),
|
||||
m_entityCount(0)
|
||||
{
|
||||
}
|
||||
|
||||
BackupDataWriter::~BackupDataWriter()
|
||||
{
|
||||
}
|
||||
|
||||
// Pad out anything they've previously written to the next 4 byte boundary.
|
||||
status_t
|
||||
BackupDataWriter::write_padding_for(int n)
|
||||
{
|
||||
ssize_t amt;
|
||||
ssize_t paddingSize;
|
||||
|
||||
paddingSize = padding_extra(n);
|
||||
if (paddingSize > 0) {
|
||||
uint32_t padding = 0xbcbcbcbc;
|
||||
if (DEBUG) ALOGI("writing %d padding bytes for %d", paddingSize, n);
|
||||
amt = write(m_fd, &padding, paddingSize);
|
||||
if (amt != paddingSize) {
|
||||
m_status = errno;
|
||||
return m_status;
|
||||
}
|
||||
m_pos += amt;
|
||||
}
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
status_t
|
||||
BackupDataWriter::WriteEntityHeader(const String8& key, size_t dataSize)
|
||||
{
|
||||
if (m_status != NO_ERROR) {
|
||||
return m_status;
|
||||
}
|
||||
|
||||
ssize_t amt;
|
||||
|
||||
amt = write_padding_for(m_pos);
|
||||
if (amt != 0) {
|
||||
return amt;
|
||||
}
|
||||
|
||||
String8 k;
|
||||
if (m_keyPrefix.length() > 0) {
|
||||
k = m_keyPrefix;
|
||||
k += ":";
|
||||
k += key;
|
||||
} else {
|
||||
k = key;
|
||||
}
|
||||
if (DEBUG) {
|
||||
ALOGD("Writing header: prefix='%s' key='%s' dataSize=%d", m_keyPrefix.string(),
|
||||
key.string(), dataSize);
|
||||
}
|
||||
|
||||
entity_header_v1 header;
|
||||
ssize_t keyLen;
|
||||
|
||||
keyLen = k.length();
|
||||
|
||||
header.type = tolel(BACKUP_HEADER_ENTITY_V1);
|
||||
header.keyLen = tolel(keyLen);
|
||||
header.dataSize = tolel(dataSize);
|
||||
|
||||
if (DEBUG) ALOGI("writing entity header, %d bytes", sizeof(entity_header_v1));
|
||||
amt = write(m_fd, &header, sizeof(entity_header_v1));
|
||||
if (amt != sizeof(entity_header_v1)) {
|
||||
m_status = errno;
|
||||
return m_status;
|
||||
}
|
||||
m_pos += amt;
|
||||
|
||||
if (DEBUG) ALOGI("writing entity header key, %d bytes", keyLen+1);
|
||||
amt = write(m_fd, k.string(), keyLen+1);
|
||||
if (amt != keyLen+1) {
|
||||
m_status = errno;
|
||||
return m_status;
|
||||
}
|
||||
m_pos += amt;
|
||||
|
||||
amt = write_padding_for(keyLen+1);
|
||||
|
||||
m_entityCount++;
|
||||
|
||||
return amt;
|
||||
}
|
||||
|
||||
status_t
|
||||
BackupDataWriter::WriteEntityData(const void* data, size_t size)
|
||||
{
|
||||
if (DEBUG) ALOGD("Writing data: size=%lu", (unsigned long) size);
|
||||
|
||||
if (m_status != NO_ERROR) {
|
||||
if (DEBUG) {
|
||||
ALOGD("Not writing data - stream in error state %d (%s)", m_status, strerror(m_status));
|
||||
}
|
||||
return m_status;
|
||||
}
|
||||
|
||||
// We don't write padding here, because they're allowed to call this several
|
||||
// times with smaller buffers. We write it at the end of WriteEntityHeader
|
||||
// instead.
|
||||
ssize_t amt = write(m_fd, data, size);
|
||||
if (amt != (ssize_t)size) {
|
||||
m_status = errno;
|
||||
if (DEBUG) ALOGD("write returned error %d (%s)", m_status, strerror(m_status));
|
||||
return m_status;
|
||||
}
|
||||
m_pos += amt;
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
void
|
||||
BackupDataWriter::SetKeyPrefix(const String8& keyPrefix)
|
||||
{
|
||||
m_keyPrefix = keyPrefix;
|
||||
}
|
||||
|
||||
|
||||
BackupDataReader::BackupDataReader(int fd)
|
||||
:m_fd(fd),
|
||||
m_done(false),
|
||||
m_status(NO_ERROR),
|
||||
m_pos(0),
|
||||
m_entityCount(0)
|
||||
{
|
||||
memset(&m_header, 0, sizeof(m_header));
|
||||
}
|
||||
|
||||
BackupDataReader::~BackupDataReader()
|
||||
{
|
||||
}
|
||||
|
||||
status_t
|
||||
BackupDataReader::Status()
|
||||
{
|
||||
return m_status;
|
||||
}
|
||||
|
||||
#define CHECK_SIZE(actual, expected) \
|
||||
do { \
|
||||
if ((actual) != (expected)) { \
|
||||
if ((actual) == 0) { \
|
||||
m_status = EIO; \
|
||||
m_done = true; \
|
||||
} else { \
|
||||
m_status = errno; \
|
||||
ALOGD("CHECK_SIZE(a=%ld e=%ld) failed at line %d m_status='%s'", \
|
||||
long(actual), long(expected), __LINE__, strerror(m_status)); \
|
||||
} \
|
||||
return m_status; \
|
||||
} \
|
||||
} while(0)
|
||||
#define SKIP_PADDING() \
|
||||
do { \
|
||||
status_t err = skip_padding(); \
|
||||
if (err != NO_ERROR) { \
|
||||
ALOGD("SKIP_PADDING FAILED at line %d", __LINE__); \
|
||||
m_status = err; \
|
||||
return err; \
|
||||
} \
|
||||
} while(0)
|
||||
|
||||
status_t
|
||||
BackupDataReader::ReadNextHeader(bool* done, int* type)
|
||||
{
|
||||
*done = m_done;
|
||||
if (m_status != NO_ERROR) {
|
||||
return m_status;
|
||||
}
|
||||
|
||||
int amt;
|
||||
|
||||
amt = skip_padding();
|
||||
if (amt == EIO) {
|
||||
*done = m_done = true;
|
||||
return NO_ERROR;
|
||||
}
|
||||
else if (amt != NO_ERROR) {
|
||||
return amt;
|
||||
}
|
||||
amt = read(m_fd, &m_header, sizeof(m_header));
|
||||
*done = m_done = (amt == 0);
|
||||
if (*done) {
|
||||
return NO_ERROR;
|
||||
}
|
||||
CHECK_SIZE(amt, sizeof(m_header));
|
||||
m_pos += sizeof(m_header);
|
||||
if (type) {
|
||||
*type = m_header.type;
|
||||
}
|
||||
|
||||
// validate and fix up the fields.
|
||||
m_header.type = fromlel(m_header.type);
|
||||
switch (m_header.type)
|
||||
{
|
||||
case BACKUP_HEADER_ENTITY_V1:
|
||||
{
|
||||
m_header.entity.keyLen = fromlel(m_header.entity.keyLen);
|
||||
if (m_header.entity.keyLen <= 0) {
|
||||
ALOGD("Entity header at %d has keyLen<=0: 0x%08x\n", (int)m_pos,
|
||||
(int)m_header.entity.keyLen);
|
||||
m_status = EINVAL;
|
||||
}
|
||||
m_header.entity.dataSize = fromlel(m_header.entity.dataSize);
|
||||
m_entityCount++;
|
||||
|
||||
// read the rest of the header (filename)
|
||||
size_t size = m_header.entity.keyLen;
|
||||
char* buf = m_key.lockBuffer(size);
|
||||
if (buf == NULL) {
|
||||
m_status = ENOMEM;
|
||||
return m_status;
|
||||
}
|
||||
int amt = read(m_fd, buf, size+1);
|
||||
CHECK_SIZE(amt, (int)size+1);
|
||||
m_key.unlockBuffer(size);
|
||||
m_pos += size+1;
|
||||
SKIP_PADDING();
|
||||
m_dataEndPos = m_pos + m_header.entity.dataSize;
|
||||
|
||||
break;
|
||||
}
|
||||
default:
|
||||
ALOGD("Chunk header at %d has invalid type: 0x%08x",
|
||||
(int)(m_pos - sizeof(m_header)), (int)m_header.type);
|
||||
m_status = EINVAL;
|
||||
}
|
||||
|
||||
return m_status;
|
||||
}
|
||||
|
||||
bool
|
||||
BackupDataReader::HasEntities()
|
||||
{
|
||||
return m_status == NO_ERROR && m_header.type == BACKUP_HEADER_ENTITY_V1;
|
||||
}
|
||||
|
||||
status_t
|
||||
BackupDataReader::ReadEntityHeader(String8* key, size_t* dataSize)
|
||||
{
|
||||
if (m_status != NO_ERROR) {
|
||||
return m_status;
|
||||
}
|
||||
if (m_header.type != BACKUP_HEADER_ENTITY_V1) {
|
||||
return EINVAL;
|
||||
}
|
||||
*key = m_key;
|
||||
*dataSize = m_header.entity.dataSize;
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
status_t
|
||||
BackupDataReader::SkipEntityData()
|
||||
{
|
||||
if (m_status != NO_ERROR) {
|
||||
return m_status;
|
||||
}
|
||||
if (m_header.type != BACKUP_HEADER_ENTITY_V1) {
|
||||
return EINVAL;
|
||||
}
|
||||
if (m_header.entity.dataSize > 0) {
|
||||
int pos = lseek(m_fd, m_dataEndPos, SEEK_SET);
|
||||
if (pos == -1) {
|
||||
return errno;
|
||||
}
|
||||
}
|
||||
SKIP_PADDING();
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
ssize_t
|
||||
BackupDataReader::ReadEntityData(void* data, size_t size)
|
||||
{
|
||||
if (m_status != NO_ERROR) {
|
||||
return -1;
|
||||
}
|
||||
int remaining = m_dataEndPos - m_pos;
|
||||
//ALOGD("ReadEntityData size=%d m_pos=0x%x m_dataEndPos=0x%x remaining=%d\n",
|
||||
// size, m_pos, m_dataEndPos, remaining);
|
||||
if (remaining <= 0) {
|
||||
return 0;
|
||||
}
|
||||
if (((int)size) > remaining) {
|
||||
size = remaining;
|
||||
}
|
||||
//ALOGD(" reading %d bytes", size);
|
||||
int amt = read(m_fd, data, size);
|
||||
if (amt < 0) {
|
||||
m_status = errno;
|
||||
return -1;
|
||||
}
|
||||
if (amt == 0) {
|
||||
m_status = EIO;
|
||||
m_done = true;
|
||||
}
|
||||
m_pos += amt;
|
||||
return amt;
|
||||
}
|
||||
|
||||
status_t
|
||||
BackupDataReader::skip_padding()
|
||||
{
|
||||
ssize_t amt;
|
||||
ssize_t paddingSize;
|
||||
|
||||
paddingSize = padding_extra(m_pos);
|
||||
if (paddingSize > 0) {
|
||||
uint32_t padding;
|
||||
amt = read(m_fd, &padding, paddingSize);
|
||||
CHECK_SIZE(amt, paddingSize);
|
||||
m_pos += amt;
|
||||
}
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
|
||||
} // namespace android
|
||||
File diff suppressed because it is too large
Load diff
|
|
@ -1,345 +0,0 @@
|
|||
/*
|
||||
* Copyright (C) 2010 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 <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define LOG_TAG "ObbFile"
|
||||
|
||||
#include <androidfw/ObbFile.h>
|
||||
#include <utils/Compat.h>
|
||||
#include <utils/Log.h>
|
||||
|
||||
//#define DEBUG 1
|
||||
|
||||
#define kFooterTagSize 8 /* last two 32-bit integers */
|
||||
|
||||
#define kFooterMinSize 33 /* 32-bit signature version (4 bytes)
|
||||
* 32-bit package version (4 bytes)
|
||||
* 32-bit flags (4 bytes)
|
||||
* 64-bit salt (8 bytes)
|
||||
* 32-bit package name size (4 bytes)
|
||||
* >=1-character package name (1 byte)
|
||||
* 32-bit footer size (4 bytes)
|
||||
* 32-bit footer marker (4 bytes)
|
||||
*/
|
||||
|
||||
#define kMaxBufSize 32768 /* Maximum file read buffer */
|
||||
|
||||
#define kSignature 0x01059983U /* ObbFile signature */
|
||||
|
||||
#define kSigVersion 1 /* We only know about signature version 1 */
|
||||
|
||||
/* offsets in version 1 of the header */
|
||||
#define kPackageVersionOffset 4
|
||||
#define kFlagsOffset 8
|
||||
#define kSaltOffset 12
|
||||
#define kPackageNameLenOffset 20
|
||||
#define kPackageNameOffset 24
|
||||
|
||||
/*
|
||||
* 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
|
||||
|
||||
|
||||
namespace android {
|
||||
|
||||
ObbFile::ObbFile()
|
||||
: mPackageName("")
|
||||
, mVersion(-1)
|
||||
, mFlags(0)
|
||||
{
|
||||
memset(mSalt, 0, sizeof(mSalt));
|
||||
}
|
||||
|
||||
ObbFile::~ObbFile() {
|
||||
}
|
||||
|
||||
bool ObbFile::readFrom(const char* filename)
|
||||
{
|
||||
int fd;
|
||||
bool success = false;
|
||||
|
||||
fd = ::open(filename, O_RDONLY);
|
||||
if (fd < 0) {
|
||||
ALOGW("couldn't open file %s: %s", filename, strerror(errno));
|
||||
goto out;
|
||||
}
|
||||
success = readFrom(fd);
|
||||
close(fd);
|
||||
|
||||
if (!success) {
|
||||
ALOGW("failed to read from %s (fd=%d)\n", filename, fd);
|
||||
}
|
||||
|
||||
out:
|
||||
return success;
|
||||
}
|
||||
|
||||
bool ObbFile::readFrom(int fd)
|
||||
{
|
||||
if (fd < 0) {
|
||||
ALOGW("attempt to read from invalid fd\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
return parseObbFile(fd);
|
||||
}
|
||||
|
||||
bool ObbFile::parseObbFile(int fd)
|
||||
{
|
||||
off64_t fileLength = lseek64(fd, 0, SEEK_END);
|
||||
|
||||
if (fileLength < kFooterMinSize) {
|
||||
if (fileLength < 0) {
|
||||
ALOGW("error seeking in ObbFile: %s\n", strerror(errno));
|
||||
} else {
|
||||
ALOGW("file is only %lld (less than %d minimum)\n", fileLength, kFooterMinSize);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
ssize_t actual;
|
||||
size_t footerSize;
|
||||
|
||||
{
|
||||
lseek64(fd, fileLength - kFooterTagSize, SEEK_SET);
|
||||
|
||||
char *footer = new char[kFooterTagSize];
|
||||
actual = TEMP_FAILURE_RETRY(read(fd, footer, kFooterTagSize));
|
||||
if (actual != kFooterTagSize) {
|
||||
ALOGW("couldn't read footer signature: %s\n", strerror(errno));
|
||||
return false;
|
||||
}
|
||||
|
||||
unsigned int fileSig = get4LE((unsigned char*)footer + sizeof(int32_t));
|
||||
if (fileSig != kSignature) {
|
||||
ALOGW("footer didn't match magic string (expected 0x%08x; got 0x%08x)\n",
|
||||
kSignature, fileSig);
|
||||
return false;
|
||||
}
|
||||
|
||||
footerSize = get4LE((unsigned char*)footer);
|
||||
if (footerSize > (size_t)fileLength - kFooterTagSize
|
||||
|| footerSize > kMaxBufSize) {
|
||||
ALOGW("claimed footer size is too large (0x%08zx; file size is 0x%08llx)\n",
|
||||
footerSize, fileLength);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (footerSize < (kFooterMinSize - kFooterTagSize)) {
|
||||
ALOGW("claimed footer size is too small (0x%zx; minimum size is 0x%x)\n",
|
||||
footerSize, kFooterMinSize - kFooterTagSize);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
off64_t fileOffset = fileLength - footerSize - kFooterTagSize;
|
||||
if (lseek64(fd, fileOffset, SEEK_SET) != fileOffset) {
|
||||
ALOGW("seek %lld failed: %s\n", fileOffset, strerror(errno));
|
||||
return false;
|
||||
}
|
||||
|
||||
mFooterStart = fileOffset;
|
||||
|
||||
char* scanBuf = (char*)malloc(footerSize);
|
||||
if (scanBuf == NULL) {
|
||||
ALOGW("couldn't allocate scanBuf: %s\n", strerror(errno));
|
||||
return false;
|
||||
}
|
||||
|
||||
actual = TEMP_FAILURE_RETRY(read(fd, scanBuf, footerSize));
|
||||
// readAmount is guaranteed to be less than kMaxBufSize
|
||||
if (actual != (ssize_t)footerSize) {
|
||||
ALOGI("couldn't read ObbFile footer: %s\n", strerror(errno));
|
||||
free(scanBuf);
|
||||
return false;
|
||||
}
|
||||
|
||||
#ifdef DEBUG
|
||||
for (int i = 0; i < footerSize; ++i) {
|
||||
ALOGI("char: 0x%02x\n", scanBuf[i]);
|
||||
}
|
||||
#endif
|
||||
|
||||
uint32_t sigVersion = get4LE((unsigned char*)scanBuf);
|
||||
if (sigVersion != kSigVersion) {
|
||||
ALOGW("Unsupported ObbFile version %d\n", sigVersion);
|
||||
free(scanBuf);
|
||||
return false;
|
||||
}
|
||||
|
||||
mVersion = (int32_t) get4LE((unsigned char*)scanBuf + kPackageVersionOffset);
|
||||
mFlags = (int32_t) get4LE((unsigned char*)scanBuf + kFlagsOffset);
|
||||
|
||||
memcpy(&mSalt, (unsigned char*)scanBuf + kSaltOffset, sizeof(mSalt));
|
||||
|
||||
size_t packageNameLen = get4LE((unsigned char*)scanBuf + kPackageNameLenOffset);
|
||||
if (packageNameLen == 0
|
||||
|| packageNameLen > (footerSize - kPackageNameOffset)) {
|
||||
ALOGW("bad ObbFile package name length (0x%04zx; 0x%04zx possible)\n",
|
||||
packageNameLen, footerSize - kPackageNameOffset);
|
||||
free(scanBuf);
|
||||
return false;
|
||||
}
|
||||
|
||||
char* packageName = reinterpret_cast<char*>(scanBuf + kPackageNameOffset);
|
||||
mPackageName = String8(const_cast<char*>(packageName), packageNameLen);
|
||||
|
||||
free(scanBuf);
|
||||
|
||||
#ifdef DEBUG
|
||||
ALOGI("Obb scan succeeded: packageName=%s, version=%d\n", mPackageName.string(), mVersion);
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ObbFile::writeTo(const char* filename)
|
||||
{
|
||||
int fd;
|
||||
bool success = false;
|
||||
|
||||
fd = ::open(filename, O_WRONLY);
|
||||
if (fd < 0) {
|
||||
goto out;
|
||||
}
|
||||
success = writeTo(fd);
|
||||
close(fd);
|
||||
|
||||
out:
|
||||
if (!success) {
|
||||
ALOGW("failed to write to %s: %s\n", filename, strerror(errno));
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
bool ObbFile::writeTo(int fd)
|
||||
{
|
||||
if (fd < 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
lseek64(fd, 0, SEEK_END);
|
||||
|
||||
if (mPackageName.size() == 0 || mVersion == -1) {
|
||||
ALOGW("tried to write uninitialized ObbFile data\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
unsigned char intBuf[sizeof(uint32_t)+1];
|
||||
memset(&intBuf, 0, sizeof(intBuf));
|
||||
|
||||
put4LE(intBuf, kSigVersion);
|
||||
if (write(fd, &intBuf, sizeof(uint32_t)) != (ssize_t)sizeof(uint32_t)) {
|
||||
ALOGW("couldn't write signature version: %s\n", strerror(errno));
|
||||
return false;
|
||||
}
|
||||
|
||||
put4LE(intBuf, mVersion);
|
||||
if (write(fd, &intBuf, sizeof(uint32_t)) != (ssize_t)sizeof(uint32_t)) {
|
||||
ALOGW("couldn't write package version\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
put4LE(intBuf, mFlags);
|
||||
if (write(fd, &intBuf, sizeof(uint32_t)) != (ssize_t)sizeof(uint32_t)) {
|
||||
ALOGW("couldn't write package version\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (write(fd, mSalt, sizeof(mSalt)) != (ssize_t)sizeof(mSalt)) {
|
||||
ALOGW("couldn't write salt: %s\n", strerror(errno));
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t packageNameLen = mPackageName.size();
|
||||
put4LE(intBuf, packageNameLen);
|
||||
if (write(fd, &intBuf, sizeof(uint32_t)) != (ssize_t)sizeof(uint32_t)) {
|
||||
ALOGW("couldn't write package name length: %s\n", strerror(errno));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (write(fd, mPackageName.string(), packageNameLen) != (ssize_t)packageNameLen) {
|
||||
ALOGW("couldn't write package name: %s\n", strerror(errno));
|
||||
return false;
|
||||
}
|
||||
|
||||
put4LE(intBuf, kPackageNameOffset + packageNameLen);
|
||||
if (write(fd, &intBuf, sizeof(uint32_t)) != (ssize_t)sizeof(uint32_t)) {
|
||||
ALOGW("couldn't write footer size: %s\n", strerror(errno));
|
||||
return false;
|
||||
}
|
||||
|
||||
put4LE(intBuf, kSignature);
|
||||
if (write(fd, &intBuf, sizeof(uint32_t)) != (ssize_t)sizeof(uint32_t)) {
|
||||
ALOGW("couldn't write footer magic signature: %s\n", strerror(errno));
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ObbFile::removeFrom(const char* filename)
|
||||
{
|
||||
int fd;
|
||||
bool success = false;
|
||||
|
||||
fd = ::open(filename, O_RDWR);
|
||||
if (fd < 0) {
|
||||
goto out;
|
||||
}
|
||||
success = removeFrom(fd);
|
||||
close(fd);
|
||||
|
||||
out:
|
||||
if (!success) {
|
||||
ALOGW("failed to remove signature from %s: %s\n", filename, strerror(errno));
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
bool ObbFile::removeFrom(int fd)
|
||||
{
|
||||
if (fd < 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!readFrom(fd)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
ftruncate(fd, mFooterStart);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
File diff suppressed because it is too large
Load diff
|
|
@ -1,226 +0,0 @@
|
|||
/*
|
||||
* Copyright (C) 2010 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_NDEBUG 0
|
||||
#define LOG_TAG "szipinf"
|
||||
#include <utils/Log.h>
|
||||
|
||||
#include <androidfw/StreamingZipInflater.h>
|
||||
#include <utils/FileMap.h>
|
||||
#include <string.h>
|
||||
#include <stddef.h>
|
||||
#include <assert.h>
|
||||
|
||||
static inline size_t min_of(size_t a, size_t b) { return (a < b) ? a : b; }
|
||||
|
||||
using namespace android;
|
||||
|
||||
/*
|
||||
* Streaming access to compressed asset data in an open fd
|
||||
*/
|
||||
StreamingZipInflater::StreamingZipInflater(int fd, off64_t compDataStart,
|
||||
size_t uncompSize, size_t compSize) {
|
||||
mFd = fd;
|
||||
mDataMap = NULL;
|
||||
mInFileStart = compDataStart;
|
||||
mOutTotalSize = uncompSize;
|
||||
mInTotalSize = compSize;
|
||||
|
||||
mInBufSize = StreamingZipInflater::INPUT_CHUNK_SIZE;
|
||||
mInBuf = new uint8_t[mInBufSize];
|
||||
|
||||
mOutBufSize = StreamingZipInflater::OUTPUT_CHUNK_SIZE;
|
||||
mOutBuf = new uint8_t[mOutBufSize];
|
||||
|
||||
initInflateState();
|
||||
}
|
||||
|
||||
/*
|
||||
* Streaming access to compressed data held in an mmapped region of memory
|
||||
*/
|
||||
StreamingZipInflater::StreamingZipInflater(FileMap* dataMap, size_t uncompSize) {
|
||||
mFd = -1;
|
||||
mDataMap = dataMap;
|
||||
mOutTotalSize = uncompSize;
|
||||
mInTotalSize = dataMap->getDataLength();
|
||||
|
||||
mInBuf = (uint8_t*) dataMap->getDataPtr();
|
||||
mInBufSize = mInTotalSize;
|
||||
|
||||
mOutBufSize = StreamingZipInflater::OUTPUT_CHUNK_SIZE;
|
||||
mOutBuf = new uint8_t[mOutBufSize];
|
||||
|
||||
initInflateState();
|
||||
}
|
||||
|
||||
StreamingZipInflater::~StreamingZipInflater() {
|
||||
// tear down the in-flight zip state just in case
|
||||
::inflateEnd(&mInflateState);
|
||||
|
||||
if (mDataMap == NULL) {
|
||||
delete [] mInBuf;
|
||||
}
|
||||
delete [] mOutBuf;
|
||||
}
|
||||
|
||||
void StreamingZipInflater::initInflateState() {
|
||||
ALOGV("Initializing inflate state");
|
||||
|
||||
memset(&mInflateState, 0, sizeof(mInflateState));
|
||||
mInflateState.zalloc = Z_NULL;
|
||||
mInflateState.zfree = Z_NULL;
|
||||
mInflateState.opaque = Z_NULL;
|
||||
mInflateState.next_in = (Bytef*)mInBuf;
|
||||
mInflateState.next_out = (Bytef*) mOutBuf;
|
||||
mInflateState.avail_out = mOutBufSize;
|
||||
mInflateState.data_type = Z_UNKNOWN;
|
||||
|
||||
mOutLastDecoded = mOutDeliverable = mOutCurPosition = 0;
|
||||
mInNextChunkOffset = 0;
|
||||
mStreamNeedsInit = true;
|
||||
|
||||
if (mDataMap == NULL) {
|
||||
::lseek(mFd, mInFileStart, SEEK_SET);
|
||||
mInflateState.avail_in = 0; // set when a chunk is read in
|
||||
} else {
|
||||
mInflateState.avail_in = mInBufSize;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Basic approach:
|
||||
*
|
||||
* 1. If we have undelivered uncompressed data, send it. At this point
|
||||
* either we've satisfied the request, or we've exhausted the available
|
||||
* output data in mOutBuf.
|
||||
*
|
||||
* 2. While we haven't sent enough data to satisfy the request:
|
||||
* 0. if the request is for more data than exists, bail.
|
||||
* a. if there is no input data to decode, read some into the input buffer
|
||||
* and readjust the z_stream input pointers
|
||||
* b. point the output to the start of the output buffer and decode what we can
|
||||
* c. deliver whatever output data we can
|
||||
*/
|
||||
ssize_t StreamingZipInflater::read(void* outBuf, size_t count) {
|
||||
uint8_t* dest = (uint8_t*) outBuf;
|
||||
size_t bytesRead = 0;
|
||||
size_t toRead = min_of(count, size_t(mOutTotalSize - mOutCurPosition));
|
||||
while (toRead > 0) {
|
||||
// First, write from whatever we already have decoded and ready to go
|
||||
size_t deliverable = min_of(toRead, mOutLastDecoded - mOutDeliverable);
|
||||
if (deliverable > 0) {
|
||||
if (outBuf != NULL) memcpy(dest, mOutBuf + mOutDeliverable, deliverable);
|
||||
mOutDeliverable += deliverable;
|
||||
mOutCurPosition += deliverable;
|
||||
dest += deliverable;
|
||||
bytesRead += deliverable;
|
||||
toRead -= deliverable;
|
||||
}
|
||||
|
||||
// need more data? time to decode some.
|
||||
if (toRead > 0) {
|
||||
// if we don't have any data to decode, read some in. If we're working
|
||||
// from mmapped data this won't happen, because the clipping to total size
|
||||
// will prevent reading off the end of the mapped input chunk.
|
||||
if (mInflateState.avail_in == 0) {
|
||||
int err = readNextChunk();
|
||||
if (err < 0) {
|
||||
ALOGE("Unable to access asset data: %d", err);
|
||||
if (!mStreamNeedsInit) {
|
||||
::inflateEnd(&mInflateState);
|
||||
initInflateState();
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
// we know we've drained whatever is in the out buffer now, so just
|
||||
// start from scratch there, reading all the input we have at present.
|
||||
mInflateState.next_out = (Bytef*) mOutBuf;
|
||||
mInflateState.avail_out = mOutBufSize;
|
||||
|
||||
/*
|
||||
ALOGV("Inflating to outbuf: avail_in=%u avail_out=%u next_in=%p next_out=%p",
|
||||
mInflateState.avail_in, mInflateState.avail_out,
|
||||
mInflateState.next_in, mInflateState.next_out);
|
||||
*/
|
||||
int result = Z_OK;
|
||||
if (mStreamNeedsInit) {
|
||||
ALOGV("Initializing zlib to inflate");
|
||||
result = inflateInit2(&mInflateState, -MAX_WBITS);
|
||||
mStreamNeedsInit = false;
|
||||
}
|
||||
if (result == Z_OK) result = ::inflate(&mInflateState, Z_SYNC_FLUSH);
|
||||
if (result < 0) {
|
||||
// Whoops, inflation failed
|
||||
ALOGE("Error inflating asset: %d", result);
|
||||
::inflateEnd(&mInflateState);
|
||||
initInflateState();
|
||||
return -1;
|
||||
} else {
|
||||
if (result == Z_STREAM_END) {
|
||||
// we know we have to have reached the target size here and will
|
||||
// not try to read any further, so just wind things up.
|
||||
::inflateEnd(&mInflateState);
|
||||
}
|
||||
|
||||
// Note how much data we got, and off we go
|
||||
mOutDeliverable = 0;
|
||||
mOutLastDecoded = mOutBufSize - mInflateState.avail_out;
|
||||
}
|
||||
}
|
||||
}
|
||||
return bytesRead;
|
||||
}
|
||||
|
||||
int StreamingZipInflater::readNextChunk() {
|
||||
assert(mDataMap == NULL);
|
||||
|
||||
if (mInNextChunkOffset < mInTotalSize) {
|
||||
size_t toRead = min_of(mInBufSize, mInTotalSize - mInNextChunkOffset);
|
||||
if (toRead > 0) {
|
||||
ssize_t didRead = ::read(mFd, mInBuf, toRead);
|
||||
//ALOGV("Reading input chunk, size %08x didread %08x", toRead, didRead);
|
||||
if (didRead < 0) {
|
||||
// TODO: error
|
||||
ALOGE("Error reading asset data");
|
||||
return didRead;
|
||||
} else {
|
||||
mInNextChunkOffset += didRead;
|
||||
mInflateState.next_in = (Bytef*) mInBuf;
|
||||
mInflateState.avail_in = didRead;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// seeking backwards requires uncompressing fom the beginning, so is very
|
||||
// expensive. seeking forwards only requires uncompressing from the current
|
||||
// position to the destination.
|
||||
off64_t StreamingZipInflater::seekAbsolute(off64_t absoluteInputPosition) {
|
||||
if (absoluteInputPosition < mOutCurPosition) {
|
||||
// rewind and reprocess the data from the beginning
|
||||
if (!mStreamNeedsInit) {
|
||||
::inflateEnd(&mInflateState);
|
||||
}
|
||||
initInflateState();
|
||||
read(NULL, absoluteInputPosition);
|
||||
} else if (absoluteInputPosition > mOutCurPosition) {
|
||||
read(NULL, absoluteInputPosition - mOutCurPosition);
|
||||
}
|
||||
// else if the target position *is* our current position, do nothing
|
||||
return absoluteInputPosition;
|
||||
}
|
||||
|
|
@ -1,54 +0,0 @@
|
|||
/*
|
||||
* 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 <androidfw/ZipFileCRO.h>
|
||||
#include <androidfw/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);
|
||||
}
|
||||
|
|
@ -1,931 +0,0 @@
|
|||
/*
|
||||
* 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 <androidfw/ZipFileRO.h>
|
||||
#include <utils/Log.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;
|
||||
}
|
||||
|
|
@ -1,343 +0,0 @@
|
|||
/*
|
||||
* 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 <androidfw/ZipUtils.h>
|
||||
#include <androidfw/ZipFileRO.h>
|
||||
#include <utils/Log.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;
|
||||
}
|
||||
|
|
@ -7,10 +7,8 @@ test_src_files := \
|
|||
BasicHashtable_test.cpp \
|
||||
BlobCache_test.cpp \
|
||||
Looper_test.cpp \
|
||||
ObbFile_test.cpp \
|
||||
String8_test.cpp \
|
||||
Unicode_test.cpp \
|
||||
ZipFileRO_test.cpp \
|
||||
|
||||
shared_libraries := \
|
||||
libz \
|
||||
|
|
|
|||
|
|
@ -1,100 +0,0 @@
|
|||
/*
|
||||
* Copyright (C) 2010 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 "ObbFile_test"
|
||||
#include <androidfw/ObbFile.h>
|
||||
#include <utils/Log.h>
|
||||
#include <utils/RefBase.h>
|
||||
#include <utils/String8.h>
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
|
||||
namespace android {
|
||||
|
||||
#define TEST_FILENAME "/test.obb"
|
||||
|
||||
class ObbFileTest : public testing::Test {
|
||||
protected:
|
||||
sp<ObbFile> mObbFile;
|
||||
char* mExternalStorage;
|
||||
char* mFileName;
|
||||
|
||||
virtual void SetUp() {
|
||||
mObbFile = new ObbFile();
|
||||
mExternalStorage = getenv("EXTERNAL_STORAGE");
|
||||
|
||||
const int totalLen = strlen(mExternalStorage) + strlen(TEST_FILENAME) + 1;
|
||||
mFileName = new char[totalLen];
|
||||
snprintf(mFileName, totalLen, "%s%s", mExternalStorage, TEST_FILENAME);
|
||||
|
||||
int fd = ::open(mFileName, O_CREAT | O_TRUNC);
|
||||
if (fd < 0) {
|
||||
FAIL() << "Couldn't create " << mFileName << " for tests";
|
||||
}
|
||||
}
|
||||
|
||||
virtual void TearDown() {
|
||||
}
|
||||
};
|
||||
|
||||
TEST_F(ObbFileTest, ReadFailure) {
|
||||
EXPECT_FALSE(mObbFile->readFrom(-1))
|
||||
<< "No failure on invalid file descriptor";
|
||||
}
|
||||
|
||||
TEST_F(ObbFileTest, WriteThenRead) {
|
||||
const char* packageName = "com.example.obbfile";
|
||||
const int32_t versionNum = 1;
|
||||
|
||||
mObbFile->setPackageName(String8(packageName));
|
||||
mObbFile->setVersion(versionNum);
|
||||
#define SALT_SIZE 8
|
||||
unsigned char salt[SALT_SIZE] = {0x01, 0x10, 0x55, 0xAA, 0xFF, 0x00, 0x5A, 0xA5};
|
||||
EXPECT_TRUE(mObbFile->setSalt(salt, SALT_SIZE))
|
||||
<< "Salt should be successfully set";
|
||||
|
||||
EXPECT_TRUE(mObbFile->writeTo(mFileName))
|
||||
<< "couldn't write to fake .obb file";
|
||||
|
||||
mObbFile = new ObbFile();
|
||||
|
||||
EXPECT_TRUE(mObbFile->readFrom(mFileName))
|
||||
<< "couldn't read from fake .obb file";
|
||||
|
||||
EXPECT_EQ(versionNum, mObbFile->getVersion())
|
||||
<< "version didn't come out the same as it went in";
|
||||
const char* currentPackageName = mObbFile->getPackageName().string();
|
||||
EXPECT_STREQ(packageName, currentPackageName)
|
||||
<< "package name didn't come out the same as it went in";
|
||||
|
||||
size_t saltLen;
|
||||
const unsigned char* newSalt = mObbFile->getSalt(&saltLen);
|
||||
|
||||
EXPECT_EQ(sizeof(salt), saltLen)
|
||||
<< "salt sizes were not the same";
|
||||
|
||||
for (int i = 0; i < sizeof(salt); i++) {
|
||||
EXPECT_EQ(salt[i], newSalt[i])
|
||||
<< "salt character " << i << " should be equal";
|
||||
}
|
||||
EXPECT_TRUE(memcmp(newSalt, salt, sizeof(salt)) == 0)
|
||||
<< "salts should be the same";
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,64 +0,0 @@
|
|||
/*
|
||||
* 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 <androidfw/ZipFileRO.h>
|
||||
#include <utils/Log.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.";
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue