Merge "ZipUtils: Rewrite in terms of zip_archive::Inflate." am: f941215d56
am: 1bed4d4880
Change-Id: I61cee0ba3cd640e914583daae64d522cc250603e
This commit is contained in:
@@ -20,10 +20,11 @@
|
|||||||
|
|
||||||
#define LOG_TAG "ziputil"
|
#define LOG_TAG "ziputil"
|
||||||
|
|
||||||
|
#include "android-base/file.h"
|
||||||
#include <androidfw/ZipUtils.h>
|
#include <androidfw/ZipUtils.h>
|
||||||
#include <androidfw/ZipFileRO.h>
|
|
||||||
#include <utils/Log.h>
|
#include <utils/Log.h>
|
||||||
#include <utils/Compat.h>
|
#include <utils/Compat.h>
|
||||||
|
#include <ziparchive/zip_archive.h>
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@@ -33,211 +34,121 @@
|
|||||||
|
|
||||||
using namespace android;
|
using namespace android;
|
||||||
|
|
||||||
static inline unsigned long get4LE(const unsigned char* buf) {
|
// TODO: This can go away once the only remaining usage in aapt goes away.
|
||||||
return buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
|
class FileReader : public zip_archive::Reader {
|
||||||
}
|
public:
|
||||||
|
FileReader(FILE* fp) : Reader(), mFp(fp), mCurrentOffset(0) {
|
||||||
|
|
||||||
static const unsigned long kReadBufSize = 32768;
|
|
||||||
|
|
||||||
/*
|
|
||||||
* 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*/ template<typename T> bool inflateToBuffer(T& reader, void* buf,
|
|
||||||
long uncompressedLen, long compressedLen)
|
|
||||||
{
|
|
||||||
bool result = false;
|
|
||||||
|
|
||||||
z_stream zstream;
|
|
||||||
int zerr;
|
|
||||||
unsigned long compRemaining;
|
|
||||||
|
|
||||||
assert(uncompressedLen >= 0);
|
|
||||||
assert(compressedLen >= 0);
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
bool ReadAtOffset(uint8_t* buf, size_t len, uint32_t offset) const {
|
||||||
* Loop while we have data.
|
// Data is usually requested sequentially, so this helps avoid pointless
|
||||||
*/
|
// fseeks every time we perform a read. There's an impedence mismatch
|
||||||
do {
|
// here because the original API was designed around pread and pwrite.
|
||||||
unsigned long getSize;
|
if (offset != mCurrentOffset) {
|
||||||
|
if (fseek(mFp, offset, SEEK_SET) != 0) {
|
||||||
/* read as much as we can */
|
return false;
|
||||||
if (zstream.avail_in == 0) {
|
|
||||||
getSize = (compRemaining > kReadBufSize) ?
|
|
||||||
kReadBufSize : compRemaining;
|
|
||||||
ALOGV("+++ reading %ld bytes (%ld left)\n",
|
|
||||||
getSize, compRemaining);
|
|
||||||
|
|
||||||
unsigned char* nextBuffer = NULL;
|
|
||||||
const unsigned long nextSize = reader.read(&nextBuffer, getSize);
|
|
||||||
|
|
||||||
if (nextSize < getSize || nextBuffer == NULL) {
|
|
||||||
ALOGD("inflate read failed (%ld vs %ld)\n", nextSize, getSize);
|
|
||||||
goto z_bail;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
compRemaining -= nextSize;
|
mCurrentOffset = offset;
|
||||||
|
|
||||||
zstream.next_in = nextBuffer;
|
|
||||||
zstream.avail_in = nextSize;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* uncompress the data */
|
size_t read = fread(buf, 1, len, mFp);
|
||||||
zerr = inflate(&zstream, Z_NO_FLUSH);
|
if (read != len) {
|
||||||
if (zerr != Z_OK && zerr != Z_STREAM_END) {
|
return false;
|
||||||
ALOGD("zlib inflate call failed (zerr=%d)\n", zerr);
|
|
||||||
goto z_bail;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* output buffer holds all, so no need to write the output */
|
mCurrentOffset += read;
|
||||||
} while (zerr == Z_OK);
|
return true;
|
||||||
|
|
||||||
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!
|
private:
|
||||||
result = true;
|
FILE* mFp;
|
||||||
|
mutable uint32_t mCurrentOffset;
|
||||||
z_bail:
|
|
||||||
inflateEnd(&zstream); /* free up any allocated structures */
|
|
||||||
|
|
||||||
bail:
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
class FileReader {
|
|
||||||
public:
|
|
||||||
explicit FileReader(FILE* fp) :
|
|
||||||
mFp(fp), mReadBuf(new unsigned char[kReadBufSize])
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
~FileReader() {
|
|
||||||
delete[] mReadBuf;
|
|
||||||
}
|
|
||||||
|
|
||||||
long read(unsigned char** nextBuffer, long readSize) const {
|
|
||||||
*nextBuffer = mReadBuf;
|
|
||||||
return fread(mReadBuf, 1, readSize, mFp);
|
|
||||||
}
|
|
||||||
|
|
||||||
FILE* mFp;
|
|
||||||
unsigned char* mReadBuf;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class FdReader {
|
class FdReader : public zip_archive::Reader {
|
||||||
public:
|
public:
|
||||||
explicit FdReader(int fd) :
|
explicit FdReader(int fd) : mFd(fd) {
|
||||||
mFd(fd), mReadBuf(new unsigned char[kReadBufSize])
|
}
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
~FdReader() {
|
bool ReadAtOffset(uint8_t* buf, size_t len, uint32_t offset) const {
|
||||||
delete[] mReadBuf;
|
return android::base::ReadFullyAtOffset(mFd, buf, len, static_cast<off_t>(offset));
|
||||||
}
|
}
|
||||||
|
|
||||||
long read(unsigned char** nextBuffer, long readSize) const {
|
private:
|
||||||
*nextBuffer = mReadBuf;
|
const int mFd;
|
||||||
return TEMP_FAILURE_RETRY(::read(mFd, mReadBuf, readSize));
|
|
||||||
}
|
|
||||||
|
|
||||||
int mFd;
|
|
||||||
unsigned char* mReadBuf;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class BufferReader {
|
class BufferReader : public zip_archive::Reader {
|
||||||
public:
|
public:
|
||||||
BufferReader(void* input, size_t inputSize) :
|
BufferReader(const void* input, size_t inputSize) : Reader(),
|
||||||
mInput(reinterpret_cast<unsigned char*>(input)),
|
mInput(reinterpret_cast<const uint8_t*>(input)),
|
||||||
mInputSize(inputSize),
|
mInputSize(inputSize) {
|
||||||
mBufferReturned(false)
|
|
||||||
{
|
|
||||||
}
|
}
|
||||||
|
|
||||||
long read(unsigned char** nextBuffer, long /*readSize*/) {
|
bool ReadAtOffset(uint8_t* buf, size_t len, uint32_t offset) const {
|
||||||
if (!mBufferReturned) {
|
if (offset + len > mInputSize) {
|
||||||
mBufferReturned = true;
|
return false;
|
||||||
*nextBuffer = mInput;
|
|
||||||
return mInputSize;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
*nextBuffer = NULL;
|
memcpy(buf, mInput + offset, len);
|
||||||
return 0;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char* mInput;
|
private:
|
||||||
|
const uint8_t* mInput;
|
||||||
const size_t mInputSize;
|
const size_t mInputSize;
|
||||||
bool mBufferReturned;
|
};
|
||||||
|
|
||||||
|
class BufferWriter : public zip_archive::Writer {
|
||||||
|
public:
|
||||||
|
BufferWriter(void* output, size_t outputSize) : Writer(),
|
||||||
|
mOutput(reinterpret_cast<uint8_t*>(output)), mOutputSize(outputSize), mBytesWritten(0) {
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Append(uint8_t* buf, size_t bufSize) override {
|
||||||
|
if (mBytesWritten + bufSize > mOutputSize) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
memcpy(mOutput + mBytesWritten, buf, bufSize);
|
||||||
|
mBytesWritten += bufSize;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
uint8_t* const mOutput;
|
||||||
|
const size_t mOutputSize;
|
||||||
|
size_t mBytesWritten;
|
||||||
};
|
};
|
||||||
|
|
||||||
/*static*/ bool ZipUtils::inflateToBuffer(FILE* fp, void* buf,
|
/*static*/ bool ZipUtils::inflateToBuffer(FILE* fp, void* buf,
|
||||||
long uncompressedLen, long compressedLen)
|
long uncompressedLen, long compressedLen)
|
||||||
{
|
{
|
||||||
FileReader reader(fp);
|
FileReader reader(fp);
|
||||||
return ::inflateToBuffer<FileReader>(reader, buf,
|
BufferWriter writer(buf, uncompressedLen);
|
||||||
uncompressedLen, compressedLen);
|
return (zip_archive::Inflate(reader, compressedLen, uncompressedLen, &writer, nullptr) == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*static*/ bool ZipUtils::inflateToBuffer(int fd, void* buf,
|
/*static*/ bool ZipUtils::inflateToBuffer(int fd, void* buf,
|
||||||
long uncompressedLen, long compressedLen)
|
long uncompressedLen, long compressedLen)
|
||||||
{
|
{
|
||||||
FdReader reader(fd);
|
FdReader reader(fd);
|
||||||
return ::inflateToBuffer<FdReader>(reader, buf,
|
BufferWriter writer(buf, uncompressedLen);
|
||||||
uncompressedLen, compressedLen);
|
return (zip_archive::Inflate(reader, compressedLen, uncompressedLen, &writer, nullptr) == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*static*/ bool ZipUtils::inflateToBuffer(void* in, void* buf,
|
/*static*/ bool ZipUtils::inflateToBuffer(const void* in, void* buf,
|
||||||
long uncompressedLen, long compressedLen)
|
long uncompressedLen, long compressedLen)
|
||||||
{
|
{
|
||||||
BufferReader reader(in, compressedLen);
|
BufferReader reader(in, compressedLen);
|
||||||
return ::inflateToBuffer<BufferReader>(reader, buf,
|
BufferWriter writer(buf, uncompressedLen);
|
||||||
uncompressedLen, compressedLen);
|
return (zip_archive::Inflate(reader, compressedLen, uncompressedLen, &writer, nullptr) == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static inline unsigned long get4LE(const unsigned char* buf) {
|
||||||
|
return buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Look at the contents of a gzip archive. We want to know where the
|
* Look at the contents of a gzip archive. We want to know where the
|
||||||
@@ -275,7 +186,7 @@ public:
|
|||||||
/* quick sanity checks */
|
/* quick sanity checks */
|
||||||
if (method == EOF || flags == EOF)
|
if (method == EOF || flags == EOF)
|
||||||
return false;
|
return false;
|
||||||
if (method != ZipFileRO::kCompressDeflated)
|
if (method != kCompressDeflated)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
/* skip over 4 bytes of mod time, 1 byte XFL, 1 byte OS */
|
/* skip over 4 bytes of mod time, 1 byte XFL, 1 byte OS */
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ public:
|
|||||||
long compressedLen);
|
long compressedLen);
|
||||||
static bool inflateToBuffer(int fd, void* buf, long uncompressedLen,
|
static bool inflateToBuffer(int fd, void* buf, long uncompressedLen,
|
||||||
long compressedLen);
|
long compressedLen);
|
||||||
static bool inflateToBuffer(void *in, void* buf, long uncompressedLen,
|
static bool inflateToBuffer(const void *in, void* buf, long uncompressedLen,
|
||||||
long compressedLen);
|
long compressedLen);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
Reference in New Issue
Block a user