Merge "zipalign/ziptime: use ftello()/fseeko()."

This commit is contained in:
Elliott Hughes
2023-01-13 14:52:05 +00:00
committed by Gerrit Code Review
4 changed files with 115 additions and 169 deletions

View File

@@ -40,14 +40,10 @@ namespace android {
*/ */
status_t ZipEntry::initFromCDE(FILE* fp) status_t ZipEntry::initFromCDE(FILE* fp)
{ {
status_t result;
long posn; // NOLINT(google-runtime-int), for ftell/fseek
bool hasDD;
//ALOGV("initFromCDE ---\n"); //ALOGV("initFromCDE ---\n");
/* read the CDE */ /* read the CDE */
result = mCDE.read(fp); status_t result = mCDE.read(fp);
if (result != OK) { if (result != OK) {
ALOGD("mCDE.read failed\n"); ALOGD("mCDE.read failed\n");
return result; return result;
@@ -56,8 +52,8 @@ status_t ZipEntry::initFromCDE(FILE* fp)
//mCDE.dump(); //mCDE.dump();
/* using the info in the CDE, go load up the LFH */ /* using the info in the CDE, go load up the LFH */
posn = ftell(fp); off_t posn = ftello(fp);
if (fseek(fp, mCDE.mLocalHeaderRelOffset, SEEK_SET) != 0) { if (fseeko(fp, mCDE.mLocalHeaderRelOffset, SEEK_SET) != 0) {
ALOGD("local header seek failed (%" PRIu32 ")\n", ALOGD("local header seek failed (%" PRIu32 ")\n",
mCDE.mLocalHeaderRelOffset); mCDE.mLocalHeaderRelOffset);
return UNKNOWN_ERROR; return UNKNOWN_ERROR;
@@ -69,7 +65,7 @@ status_t ZipEntry::initFromCDE(FILE* fp)
return result; return result;
} }
if (fseek(fp, posn, SEEK_SET) != 0) if (fseeko(fp, posn, SEEK_SET) != 0)
return UNKNOWN_ERROR; return UNKNOWN_ERROR;
//mLFH.dump(); //mLFH.dump();
@@ -80,7 +76,7 @@ status_t ZipEntry::initFromCDE(FILE* fp)
* compressed size, and uncompressed size will be zero. In practice * compressed size, and uncompressed size will be zero. In practice
* these seem to be rare. * these seem to be rare.
*/ */
hasDD = (mLFH.mGPBitFlag & kUsesDataDescr) != 0; bool hasDD = (mLFH.mGPBitFlag & kUsesDataDescr) != 0;
if (hasDD) { if (hasDD) {
// do something clever // do something clever
//ALOGD("+++ has data descriptor\n"); //ALOGD("+++ has data descriptor\n");

View File

@@ -35,6 +35,8 @@
#include <assert.h> #include <assert.h>
#include <inttypes.h> #include <inttypes.h>
_Static_assert(sizeof(off_t) == 8, "off_t too small");
namespace android { namespace android {
/* /*
@@ -205,56 +207,43 @@ void ZipFile::discardEntries(void)
*/ */
status_t ZipFile::readCentralDir(void) status_t ZipFile::readCentralDir(void)
{ {
status_t result = OK; fseeko(mZipFp, 0, SEEK_END);
uint8_t* buf = NULL; off_t fileLength = ftello(mZipFp);
off_t fileLength, seekStart;
long readAmount;
int i;
fseek(mZipFp, 0, SEEK_END);
fileLength = ftell(mZipFp);
rewind(mZipFp); rewind(mZipFp);
/* too small to be a ZIP archive? */ /* too small to be a ZIP archive? */
if (fileLength < EndOfCentralDir::kEOCDLen) { if (fileLength < EndOfCentralDir::kEOCDLen) {
ALOGD("Length is %ld -- too small\n", (long)fileLength); ALOGD("Length is %lld -- too small\n", (long long) fileLength);
result = INVALID_OPERATION; return INVALID_OPERATION;
goto bail;
}
buf = new uint8_t[EndOfCentralDir::kMaxEOCDSearch];
if (buf == NULL) {
ALOGD("Failure allocating %d bytes for EOCD search",
EndOfCentralDir::kMaxEOCDSearch);
result = NO_MEMORY;
goto bail;
} }
off_t seekStart;
size_t readAmount;
if (fileLength > EndOfCentralDir::kMaxEOCDSearch) { if (fileLength > EndOfCentralDir::kMaxEOCDSearch) {
seekStart = fileLength - EndOfCentralDir::kMaxEOCDSearch; seekStart = fileLength - EndOfCentralDir::kMaxEOCDSearch;
readAmount = EndOfCentralDir::kMaxEOCDSearch; readAmount = EndOfCentralDir::kMaxEOCDSearch;
} else { } else {
seekStart = 0; seekStart = 0;
readAmount = (long) fileLength; readAmount = fileLength;
} }
if (fseek(mZipFp, seekStart, SEEK_SET) != 0) { if (fseeko(mZipFp, seekStart, SEEK_SET) != 0) {
ALOGD("Failure seeking to end of zip at %ld", (long) seekStart); ALOGD("Failure seeking to end of zip at %lld", (long long) seekStart);
result = UNKNOWN_ERROR; return UNKNOWN_ERROR;
goto bail;
} }
/* read the last part of the file into the buffer */ /* read the last part of the file into the buffer */
if (fread(buf, 1, readAmount, mZipFp) != (size_t) readAmount) { uint8_t buf[EndOfCentralDir::kMaxEOCDSearch];
if (fread(buf, 1, readAmount, mZipFp) != readAmount) {
if (feof(mZipFp)) { if (feof(mZipFp)) {
ALOGW("fread %ld bytes failed, unexpected EOF", readAmount); ALOGW("fread %zu bytes failed, unexpected EOF", readAmount);
} else { } else {
ALOGW("fread %ld bytes failed, %s", readAmount, strerror(errno)); ALOGW("fread %zu bytes failed, %s", readAmount, strerror(errno));
} }
result = UNKNOWN_ERROR; return UNKNOWN_ERROR;
goto bail;
} }
/* find the end-of-central-dir magic */ /* find the end-of-central-dir magic */
int i;
for (i = readAmount - 4; i >= 0; i--) { for (i = readAmount - 4; i >= 0; i--) {
if (buf[i] == 0x50 && if (buf[i] == 0x50 &&
ZipEntry::getLongLE(&buf[i]) == EndOfCentralDir::kSignature) ZipEntry::getLongLE(&buf[i]) == EndOfCentralDir::kSignature)
@@ -265,15 +254,14 @@ status_t ZipFile::readCentralDir(void)
} }
if (i < 0) { if (i < 0) {
ALOGD("EOCD not found, not Zip\n"); ALOGD("EOCD not found, not Zip\n");
result = INVALID_OPERATION; return INVALID_OPERATION;
goto bail;
} }
/* extract eocd values */ /* extract eocd values */
result = mEOCD.readBuf(buf + i, readAmount - i); status_t result = mEOCD.readBuf(buf + i, readAmount - i);
if (result != OK) { if (result != OK) {
ALOGD("Failure reading %ld bytes of EOCD values", readAmount - i); ALOGD("Failure reading %zu bytes of EOCD values", readAmount - i);
goto bail; return result;
} }
//mEOCD.dump(); //mEOCD.dump();
@@ -281,8 +269,7 @@ status_t ZipFile::readCentralDir(void)
mEOCD.mNumEntries != mEOCD.mTotalNumEntries) mEOCD.mNumEntries != mEOCD.mTotalNumEntries)
{ {
ALOGD("Archive spanning not supported\n"); ALOGD("Archive spanning not supported\n");
result = INVALID_OPERATION; return INVALID_OPERATION;
goto bail;
} }
/* /*
@@ -299,11 +286,10 @@ status_t ZipFile::readCentralDir(void)
* The only thing we really need right now is the file comment, which * The only thing we really need right now is the file comment, which
* we're hoping to preserve. * we're hoping to preserve.
*/ */
if (fseek(mZipFp, mEOCD.mCentralDirOffset, SEEK_SET) != 0) { if (fseeko(mZipFp, mEOCD.mCentralDirOffset, SEEK_SET) != 0) {
ALOGD("Failure seeking to central dir offset %" PRIu32 "\n", ALOGD("Failure seeking to central dir offset %" PRIu32 "\n",
mEOCD.mCentralDirOffset); mEOCD.mCentralDirOffset);
result = UNKNOWN_ERROR; return UNKNOWN_ERROR;
goto bail;
} }
/* /*
@@ -318,7 +304,7 @@ status_t ZipFile::readCentralDir(void)
if (result != OK) { if (result != OK) {
ALOGD("initFromCDE failed\n"); ALOGD("initFromCDE failed\n");
delete pEntry; delete pEntry;
goto bail; return result;
} }
mEntries.add(pEntry); mEntries.add(pEntry);
@@ -336,20 +322,16 @@ status_t ZipFile::readCentralDir(void)
} else { } else {
ALOGW("fread EOCD failed, %s", strerror(errno)); ALOGW("fread EOCD failed, %s", strerror(errno));
} }
result = INVALID_OPERATION; return INVALID_OPERATION;
goto bail;
} }
if (ZipEntry::getLongLE(checkBuf) != EndOfCentralDir::kSignature) { if (ZipEntry::getLongLE(checkBuf) != EndOfCentralDir::kSignature) {
ALOGD("EOCD read check failed\n"); ALOGD("EOCD read check failed\n");
result = UNKNOWN_ERROR; return UNKNOWN_ERROR;
goto bail;
} }
ALOGV("+++ EOCD read check passed\n"); ALOGV("+++ EOCD read check passed\n");
} }
bail: return OK;
delete[] buf;
return result;
} }
@@ -370,8 +352,7 @@ status_t ZipFile::addCommon(const char* fileName, const void* data, size_t size,
{ {
ZipEntry* pEntry = NULL; ZipEntry* pEntry = NULL;
status_t result = OK; status_t result = OK;
long lfhPosn, startPosn, endPosn, uncompressedLen; off_t lfhPosn, startPosn, endPosn, uncompressedLen;
FILE* inputFp = NULL;
uint32_t crc = 0; uint32_t crc = 0;
time_t modWhen; time_t modWhen;
@@ -389,13 +370,14 @@ status_t ZipFile::addCommon(const char* fileName, const void* data, size_t size,
if (getEntryByName(storageName) != NULL) if (getEntryByName(storageName) != NULL)
return ALREADY_EXISTS; return ALREADY_EXISTS;
FILE* inputFp = NULL;
if (!data) { if (!data) {
inputFp = fopen(fileName, FILE_OPEN_RO); inputFp = fopen(fileName, FILE_OPEN_RO);
if (inputFp == NULL) if (inputFp == NULL)
return errnoToStatus(errno); return errnoToStatus(errno);
} }
if (fseek(mZipFp, mEOCD.mCentralDirOffset, SEEK_SET) != 0) { if (fseeko(mZipFp, mEOCD.mCentralDirOffset, SEEK_SET) != 0) {
result = UNKNOWN_ERROR; result = UNKNOWN_ERROR;
goto bail; goto bail;
} }
@@ -413,9 +395,9 @@ status_t ZipFile::addCommon(const char* fileName, const void* data, size_t size,
* as a place-holder. In theory the LFH isn't necessary, but in * as a place-holder. In theory the LFH isn't necessary, but in
* practice some utilities demand it. * practice some utilities demand it.
*/ */
lfhPosn = ftell(mZipFp); lfhPosn = ftello(mZipFp);
pEntry->mLFH.write(mZipFp); pEntry->mLFH.write(mZipFp);
startPosn = ftell(mZipFp); startPosn = ftello(mZipFp);
/* /*
* Copy the data in, possibly compressing it as we go. * Copy the data in, possibly compressing it as we go.
@@ -432,11 +414,11 @@ status_t ZipFile::addCommon(const char* fileName, const void* data, size_t size,
* to be set through an API call, but I don't expect our * to be set through an API call, but I don't expect our
* criteria to change over time. * criteria to change over time.
*/ */
long src = inputFp ? ftell(inputFp) : size; off_t src = inputFp ? ftello(inputFp) : size;
long dst = ftell(mZipFp) - startPosn; off_t dst = ftello(mZipFp) - startPosn;
if (dst + (dst / 10) > src) { if (dst + (dst / 10) > src) {
ALOGD("insufficient compression (src=%ld dst=%ld), storing\n", ALOGD("insufficient compression (src=%lld dst=%lld), storing\n",
src, dst); (long long) src, (long long) dst);
failed = true; failed = true;
} }
} }
@@ -444,7 +426,7 @@ status_t ZipFile::addCommon(const char* fileName, const void* data, size_t size,
if (failed) { if (failed) {
compressionMethod = ZipEntry::kCompressStored; compressionMethod = ZipEntry::kCompressStored;
if (inputFp) rewind(inputFp); if (inputFp) rewind(inputFp);
fseek(mZipFp, startPosn, SEEK_SET); fseeko(mZipFp, startPosn, SEEK_SET);
/* fall through to kCompressStored case */ /* fall through to kCompressStored case */
} }
} }
@@ -463,7 +445,7 @@ status_t ZipFile::addCommon(const char* fileName, const void* data, size_t size,
} }
// currently seeked to end of file // currently seeked to end of file
uncompressedLen = inputFp ? ftell(inputFp) : size; uncompressedLen = inputFp ? ftello(inputFp) : size;
/* /*
* We could write the "Data Descriptor", but there doesn't seem to * We could write the "Data Descriptor", but there doesn't seem to
@@ -471,7 +453,7 @@ status_t ZipFile::addCommon(const char* fileName, const void* data, size_t size,
* *
* Update file offsets. * Update file offsets.
*/ */
endPosn = ftell(mZipFp); // seeked to end of compressed data endPosn = ftello(mZipFp); // seeked to end of compressed data
/* /*
* Success! Fill out new values. * Success! Fill out new values.
@@ -489,7 +471,7 @@ status_t ZipFile::addCommon(const char* fileName, const void* data, size_t size,
/* /*
* Go back and write the LFH. * Go back and write the LFH.
*/ */
if (fseek(mZipFp, lfhPosn, SEEK_SET) != 0) { if (fseeko(mZipFp, lfhPosn, SEEK_SET) != 0) {
result = UNKNOWN_ERROR; result = UNKNOWN_ERROR;
goto bail; goto bail;
} }
@@ -522,7 +504,7 @@ status_t ZipFile::alignEntry(android::ZipEntry* pEntry, uint32_t alignTo){
// Calculate where the entry payload offset will end up if we were to write // Calculate where the entry payload offset will end up if we were to write
// it as-is. // it as-is.
uint64_t expectedPayloadOffset = ftell(mZipFp) + uint64_t expectedPayloadOffset = ftello(mZipFp) +
android::ZipEntry::LocalFileHeader::kLFHLen + android::ZipEntry::LocalFileHeader::kLFHLen +
pEntry->mLFH.mFileNameLength + pEntry->mLFH.mFileNameLength +
pEntry->mLFH.mExtraFieldLength; pEntry->mLFH.mExtraFieldLength;
@@ -548,7 +530,7 @@ status_t ZipFile::add(const ZipFile* pSourceZip, const ZipEntry* pSourceEntry,
{ {
ZipEntry* pEntry = NULL; ZipEntry* pEntry = NULL;
status_t result; status_t result;
long lfhPosn, endPosn; off_t lfhPosn, endPosn;
if (mReadOnly) if (mReadOnly)
return INVALID_OPERATION; return INVALID_OPERATION;
@@ -557,7 +539,7 @@ status_t ZipFile::add(const ZipFile* pSourceZip, const ZipEntry* pSourceEntry,
assert(mZipFp != NULL); assert(mZipFp != NULL);
assert(mEntries.size() == mEOCD.mTotalNumEntries); assert(mEntries.size() == mEOCD.mTotalNumEntries);
if (fseek(mZipFp, mEOCD.mCentralDirOffset, SEEK_SET) != 0) { if (fseeko(mZipFp, mEOCD.mCentralDirOffset, SEEK_SET) != 0) {
result = UNKNOWN_ERROR; result = UNKNOWN_ERROR;
goto bail; goto bail;
} }
@@ -585,7 +567,7 @@ status_t ZipFile::add(const ZipFile* pSourceZip, const ZipEntry* pSourceEntry,
* Write the LFH. Since we're not recompressing the data, we already * Write the LFH. Since we're not recompressing the data, we already
* have all of the fields filled out. * have all of the fields filled out.
*/ */
lfhPosn = ftell(mZipFp); lfhPosn = ftello(mZipFp);
pEntry->mLFH.write(mZipFp); pEntry->mLFH.write(mZipFp);
/* /*
@@ -595,8 +577,7 @@ status_t ZipFile::add(const ZipFile* pSourceZip, const ZipEntry* pSourceEntry,
* fields as well. This is a fixed-size area immediately following * fields as well. This is a fixed-size area immediately following
* the data. * the data.
*/ */
if (fseek(pSourceZip->mZipFp, pSourceEntry->getFileOffset(), SEEK_SET) != 0) if (fseeko(pSourceZip->mZipFp, pSourceEntry->getFileOffset(), SEEK_SET) != 0) {
{
result = UNKNOWN_ERROR; result = UNKNOWN_ERROR;
goto bail; goto bail;
} }
@@ -617,7 +598,7 @@ status_t ZipFile::add(const ZipFile* pSourceZip, const ZipEntry* pSourceEntry,
/* /*
* Update file offsets. * Update file offsets.
*/ */
endPosn = ftell(mZipFp); endPosn = ftello(mZipFp);
/* /*
* Success! Fill out new values. * Success! Fill out new values.
@@ -654,7 +635,7 @@ status_t ZipFile::addRecompress(const ZipFile* pSourceZip, const ZipEntry* pSour
{ {
ZipEntry* pEntry = NULL; ZipEntry* pEntry = NULL;
status_t result; status_t result;
long lfhPosn, uncompressedLen; off_t lfhPosn, uncompressedLen;
if (mReadOnly) if (mReadOnly)
return INVALID_OPERATION; return INVALID_OPERATION;
@@ -663,7 +644,7 @@ status_t ZipFile::addRecompress(const ZipFile* pSourceZip, const ZipEntry* pSour
assert(mZipFp != NULL); assert(mZipFp != NULL);
assert(mEntries.size() == mEOCD.mTotalNumEntries); assert(mEntries.size() == mEOCD.mTotalNumEntries);
if (fseek(mZipFp, mEOCD.mCentralDirOffset, SEEK_SET) != 0) { if (fseeko(mZipFp, mEOCD.mCentralDirOffset, SEEK_SET) != 0) {
result = UNKNOWN_ERROR; result = UNKNOWN_ERROR;
goto bail; goto bail;
} }
@@ -688,7 +669,7 @@ status_t ZipFile::addRecompress(const ZipFile* pSourceZip, const ZipEntry* pSour
* as a place-holder. In theory the LFH isn't necessary, but in * as a place-holder. In theory the LFH isn't necessary, but in
* practice some utilities demand it. * practice some utilities demand it.
*/ */
lfhPosn = ftell(mZipFp); lfhPosn = ftello(mZipFp);
pEntry->mLFH.write(mZipFp); pEntry->mLFH.write(mZipFp);
/* /*
@@ -698,8 +679,7 @@ status_t ZipFile::addRecompress(const ZipFile* pSourceZip, const ZipEntry* pSour
* fields as well. This is a fixed-size area immediately following * fields as well. This is a fixed-size area immediately following
* the data. * the data.
*/ */
if (fseek(pSourceZip->mZipFp, pSourceEntry->getFileOffset(), SEEK_SET) != 0) if (fseeko(pSourceZip->mZipFp, pSourceEntry->getFileOffset(), SEEK_SET) != 0) {
{
result = UNKNOWN_ERROR; result = UNKNOWN_ERROR;
goto bail; goto bail;
} }
@@ -712,7 +692,7 @@ status_t ZipFile::addRecompress(const ZipFile* pSourceZip, const ZipEntry* pSour
result = NO_MEMORY; result = NO_MEMORY;
goto bail; goto bail;
} }
long startPosn = ftell(mZipFp); off_t startPosn = ftello(mZipFp);
uint32_t crc; uint32_t crc;
if (compressFpToFp(mZipFp, NULL, buf, uncompressedLen, &crc) != OK) { if (compressFpToFp(mZipFp, NULL, buf, uncompressedLen, &crc) != OK) {
ALOGW("recompress of '%s' failed\n", pEntry->mCDE.mFileName); ALOGW("recompress of '%s' failed\n", pEntry->mCDE.mFileName);
@@ -720,13 +700,12 @@ status_t ZipFile::addRecompress(const ZipFile* pSourceZip, const ZipEntry* pSour
free(buf); free(buf);
goto bail; goto bail;
} }
long endPosn = ftell(mZipFp); off_t endPosn = ftello(mZipFp);
pEntry->setDataInfo(uncompressedLen, endPosn - startPosn, pEntry->setDataInfo(uncompressedLen, endPosn - startPosn,
pSourceEntry->getCRC32(), ZipEntry::kCompressDeflated); pSourceEntry->getCRC32(), ZipEntry::kCompressDeflated);
free(buf); free(buf);
} else { } else {
off_t copyLen; off_t copyLen = pSourceEntry->getCompressedLen();
copyLen = pSourceEntry->getCompressedLen();
if ((pSourceEntry->mLFH.mGPBitFlag & ZipEntry::kUsesDataDescr) != 0) if ((pSourceEntry->mLFH.mGPBitFlag & ZipEntry::kUsesDataDescr) != 0)
copyLen += ZipEntry::kDataDescriptorLen; copyLen += ZipEntry::kDataDescriptorLen;
@@ -746,12 +725,12 @@ status_t ZipFile::addRecompress(const ZipFile* pSourceZip, const ZipEntry* pSour
mEOCD.mNumEntries++; mEOCD.mNumEntries++;
mEOCD.mTotalNumEntries++; mEOCD.mTotalNumEntries++;
mEOCD.mCentralDirSize = 0; // mark invalid; set by flush() mEOCD.mCentralDirSize = 0; // mark invalid; set by flush()
mEOCD.mCentralDirOffset = ftell(mZipFp); mEOCD.mCentralDirOffset = ftello(mZipFp);
/* /*
* Go back and write the LFH. * Go back and write the LFH.
*/ */
if (fseek(mZipFp, lfhPosn, SEEK_SET) != 0) { if (fseeko(mZipFp, lfhPosn, SEEK_SET) != 0) {
result = UNKNOWN_ERROR; result = UNKNOWN_ERROR;
goto bail; goto bail;
} }
@@ -978,7 +957,7 @@ status_t ZipFile::remove(ZipEntry* pEntry)
status_t ZipFile::flush(void) status_t ZipFile::flush(void)
{ {
status_t result = OK; status_t result = OK;
long eocdPosn; off_t eocdPosn;
int i, count; int i, count;
if (mReadOnly) if (mReadOnly)
@@ -992,8 +971,7 @@ status_t ZipFile::flush(void)
if (result != OK) if (result != OK)
return result; return result;
if (fseek(mZipFp, mEOCD.mCentralDirOffset, SEEK_SET) != 0) if (fseeko(mZipFp, mEOCD.mCentralDirOffset, SEEK_SET) != 0) return UNKNOWN_ERROR;
return UNKNOWN_ERROR;
count = mEntries.size(); count = mEntries.size();
for (i = 0; i < count; i++) { for (i = 0; i < count; i++) {
@@ -1001,7 +979,7 @@ status_t ZipFile::flush(void)
pEntry->mCDE.write(mZipFp); pEntry->mCDE.write(mZipFp);
} }
eocdPosn = ftell(mZipFp); eocdPosn = ftello(mZipFp);
mEOCD.mCentralDirSize = eocdPosn - mEOCD.mCentralDirOffset; mEOCD.mCentralDirSize = eocdPosn - mEOCD.mCentralDirOffset;
mEOCD.write(mZipFp); mEOCD.write(mZipFp);
@@ -1011,8 +989,8 @@ status_t ZipFile::flush(void)
* with plain files, or if we deleted some entries, there's a lot * with plain files, or if we deleted some entries, there's a lot
* of wasted space at the end of the file. Remove it now. * of wasted space at the end of the file. Remove it now.
*/ */
if (ftruncate(fileno(mZipFp), ftell(mZipFp)) != 0) { if (ftruncate(fileno(mZipFp), ftello(mZipFp)) != 0) {
ALOGW("ftruncate failed %ld: %s\n", ftell(mZipFp), strerror(errno)); ALOGW("ftruncate failed %lld: %s\n", (long long) ftello(mZipFp), strerror(errno));
// not fatal // not fatal
} }
@@ -1141,32 +1119,32 @@ status_t ZipFile::filemove(FILE* fp, off_t dst, off_t src, size_t n)
if (getSize > n) if (getSize > n)
getSize = n; getSize = n;
if (fseek(fp, (long) src, SEEK_SET) != 0) { if (fseeko(fp, src, SEEK_SET) != 0) {
ALOGW("filemove src seek %ld failed, %s", ALOGW("filemove src seek %lld failed, %s",
(long) src, strerror(errno)); (long long) src, strerror(errno));
return UNKNOWN_ERROR; return UNKNOWN_ERROR;
} }
if (fread(readBuf, 1, getSize, fp) != getSize) { if (fread(readBuf, 1, getSize, fp) != getSize) {
if (feof(fp)) { if (feof(fp)) {
ALOGW("fread %zu bytes off=%ld failed, unexpected EOF", ALOGW("fread %zu bytes off=%lld failed, unexpected EOF",
getSize, (long) src); getSize, (long long) src);
} else { } else {
ALOGW("fread %zu bytes off=%ld failed, %s", ALOGW("fread %zu bytes off=%lld failed, %s",
getSize, (long) src, strerror(errno)); getSize, (long long) src, strerror(errno));
} }
return UNKNOWN_ERROR; return UNKNOWN_ERROR;
} }
if (fseek(fp, (long) dst, SEEK_SET) != 0) { if (fseeko(fp, dst, SEEK_SET) != 0) {
ALOGW("filemove dst seek %ld failed, %s", ALOGW("filemove dst seek %lld failed, %s",
(long) dst, strerror(errno)); (long long) dst, strerror(errno));
return UNKNOWN_ERROR; return UNKNOWN_ERROR;
} }
if (fwrite(readBuf, 1, getSize, fp) != getSize) { if (fwrite(readBuf, 1, getSize, fp) != getSize) {
ALOGW("filemove write %zu off=%ld failed, %s", ALOGW("filemove write %zu off=%lld failed, %s",
getSize, (long) dst, strerror(errno)); getSize, (long long) dst, strerror(errno));
return UNKNOWN_ERROR; return UNKNOWN_ERROR;
} }
@@ -1263,10 +1241,10 @@ class FileReader : public zip_archive::Reader {
bool ReadAtOffset(uint8_t* buf, size_t len, off64_t offset) const { bool ReadAtOffset(uint8_t* buf, size_t len, off64_t offset) const {
// Data is usually requested sequentially, so this helps avoid pointless // Data is usually requested sequentially, so this helps avoid pointless
// fseeks every time we perform a read. There's an impedence mismatch // seeks every time we perform a read. There's an impedence mismatch
// here because the original API was designed around pread and pwrite. // here because the original API was designed around pread and pwrite.
if (offset != current_offset_) { if (offset != current_offset_) {
if (fseek(fp_, offset, SEEK_SET) != 0) { if (fseeko(fp_, offset, SEEK_SET) != 0) {
return false; return false;
} }
@@ -1298,10 +1276,10 @@ void* ZipFile::uncompress(const ZipEntry* entry) const
return NULL; return NULL;
} }
fseek(mZipFp, 0, SEEK_SET); fseeko(mZipFp, 0, SEEK_SET);
off_t offset = entry->getFileOffset(); off_t offset = entry->getFileOffset();
if (fseek(mZipFp, offset, SEEK_SET) != 0) { if (fseeko(mZipFp, offset, SEEK_SET) != 0) {
goto bail; goto bail;
} }

View File

@@ -43,19 +43,16 @@ using namespace android;
*/ */
status_t ZipEntry::initAndRewriteFromCDE(FILE* fp) status_t ZipEntry::initAndRewriteFromCDE(FILE* fp)
{ {
status_t result;
long posn;
/* read the CDE */ /* read the CDE */
result = mCDE.rewrite(fp); status_t result = mCDE.rewrite(fp);
if (result != 0) { if (result != 0) {
LOG("mCDE.rewrite failed\n"); LOG("mCDE.rewrite failed\n");
return result; return result;
} }
/* using the info in the CDE, go load up the LFH */ /* using the info in the CDE, go load up the LFH */
posn = ftell(fp); off_t posn = ftello(fp);
if (fseek(fp, mCDE.mLocalHeaderRelOffset, SEEK_SET) != 0) { if (fseeko(fp, mCDE.mLocalHeaderRelOffset, SEEK_SET) != 0) {
LOG("local header seek failed (%" PRIu32 ")\n", LOG("local header seek failed (%" PRIu32 ")\n",
mCDE.mLocalHeaderRelOffset); mCDE.mLocalHeaderRelOffset);
return -1; return -1;
@@ -67,7 +64,7 @@ status_t ZipEntry::initAndRewriteFromCDE(FILE* fp)
return result; return result;
} }
if (fseek(fp, posn, SEEK_SET) != 0) if (fseeko(fp, posn, SEEK_SET) != 0)
return -1; return -1;
return 0; return 0;

View File

@@ -40,8 +40,7 @@ status_t ZipFile::rewrite(const char* zipFileName)
/* open the file */ /* open the file */
mZipFp = fopen(zipFileName, "r+b"); mZipFp = fopen(zipFileName, "r+b");
if (mZipFp == NULL) { if (mZipFp == NULL) {
int err = errno; LOG("fopen \"%s\" failed: %s\n", zipFileName, strerror(errno));
LOG("fopen failed: %d\n", err);
return -1; return -1;
} }
@@ -72,52 +71,39 @@ status_t ZipFile::rewrite(const char* zipFileName)
*/ */
status_t ZipFile::rewriteCentralDir(void) status_t ZipFile::rewriteCentralDir(void)
{ {
status_t result = 0; fseeko(mZipFp, 0, SEEK_END);
uint8_t* buf = NULL; off_t fileLength = ftello(mZipFp);
off_t fileLength, seekStart;
long readAmount;
int i;
fseek(mZipFp, 0, SEEK_END);
fileLength = ftell(mZipFp);
rewind(mZipFp); rewind(mZipFp);
/* too small to be a ZIP archive? */ /* too small to be a ZIP archive? */
if (fileLength < EndOfCentralDir::kEOCDLen) { if (fileLength < EndOfCentralDir::kEOCDLen) {
LOG("Length is %ld -- too small\n", (long)fileLength); LOG("Length is %lld -- too small\n", (long long) fileLength);
result = -1; return -1;
goto bail;
}
buf = new uint8_t[EndOfCentralDir::kMaxEOCDSearch];
if (buf == NULL) {
LOG("Failure allocating %d bytes for EOCD search",
EndOfCentralDir::kMaxEOCDSearch);
result = -1;
goto bail;
} }
off_t seekStart;
size_t readAmount;
if (fileLength > EndOfCentralDir::kMaxEOCDSearch) { if (fileLength > EndOfCentralDir::kMaxEOCDSearch) {
seekStart = fileLength - EndOfCentralDir::kMaxEOCDSearch; seekStart = fileLength - EndOfCentralDir::kMaxEOCDSearch;
readAmount = EndOfCentralDir::kMaxEOCDSearch; readAmount = EndOfCentralDir::kMaxEOCDSearch;
} else { } else {
seekStart = 0; seekStart = 0;
readAmount = (long) fileLength; readAmount = fileLength;
} }
if (fseek(mZipFp, seekStart, SEEK_SET) != 0) { if (fseeko(mZipFp, seekStart, SEEK_SET) != 0) {
LOG("Failure seeking to end of zip at %ld", (long) seekStart); LOG("Failure seeking to end of zip at %lld", (long long) seekStart);
result = -1; return -1;
goto bail;
} }
/* read the last part of the file into the buffer */ /* read the last part of the file into the buffer */
if (fread(buf, 1, readAmount, mZipFp) != (size_t) readAmount) { uint8_t buf[EndOfCentralDir::kMaxEOCDSearch];
LOG("short file? wanted %ld\n", readAmount); if (fread(buf, 1, readAmount, mZipFp) != readAmount) {
result = -1; LOG("short file? wanted %zu\n", readAmount);
goto bail; return -1;
} }
/* find the end-of-central-dir magic */ /* find the end-of-central-dir magic */
int i;
for (i = readAmount - 4; i >= 0; i--) { for (i = readAmount - 4; i >= 0; i--) {
if (buf[i] == 0x50 && if (buf[i] == 0x50 &&
ZipEntry::getLongLE(&buf[i]) == EndOfCentralDir::kSignature) ZipEntry::getLongLE(&buf[i]) == EndOfCentralDir::kSignature)
@@ -127,15 +113,14 @@ status_t ZipFile::rewriteCentralDir(void)
} }
if (i < 0) { if (i < 0) {
LOG("EOCD not found, not Zip\n"); LOG("EOCD not found, not Zip\n");
result = -1; return -1;
goto bail;
} }
/* extract eocd values */ /* extract eocd values */
result = mEOCD.readBuf(buf + i, readAmount - i); status_t result = mEOCD.readBuf(buf + i, readAmount - i);
if (result != 0) { if (result != 0) {
LOG("Failure reading %ld bytes of EOCD values", readAmount - i); LOG("Failure reading %zu bytes of EOCD values", readAmount - i);
goto bail; return result;
} }
/* /*
@@ -152,49 +137,39 @@ status_t ZipFile::rewriteCentralDir(void)
* The only thing we really need right now is the file comment, which * The only thing we really need right now is the file comment, which
* we're hoping to preserve. * we're hoping to preserve.
*/ */
if (fseek(mZipFp, mEOCD.mCentralDirOffset, SEEK_SET) != 0) { if (fseeko(mZipFp, mEOCD.mCentralDirOffset, SEEK_SET) != 0) {
LOG("Failure seeking to central dir offset %" PRIu32 "\n", LOG("Failure seeking to central dir offset %" PRIu32 "\n",
mEOCD.mCentralDirOffset); mEOCD.mCentralDirOffset);
result = -1; return -1;
goto bail;
} }
/* /*
* Loop through and read the central dir entries. * Loop through and read the central dir entries.
*/ */
int entry; for (int entry = 0; entry < mEOCD.mTotalNumEntries; entry++) {
for (entry = 0; entry < mEOCD.mTotalNumEntries; entry++) {
ZipEntry* pEntry = new ZipEntry; ZipEntry* pEntry = new ZipEntry;
result = pEntry->initAndRewriteFromCDE(mZipFp); result = pEntry->initAndRewriteFromCDE(mZipFp);
delete pEntry;
if (result != 0) { if (result != 0) {
LOG("initFromCDE failed\n"); LOG("initFromCDE failed\n");
delete pEntry; return -1;
goto bail;
} }
delete pEntry;
} }
/* /*
* If all went well, we should now be back at the EOCD. * If all went well, we should now be back at the EOCD.
*/ */
uint8_t checkBuf[4]; uint8_t checkBuf[4];
if (fread(checkBuf, 1, 4, mZipFp) != 4) { if (fread(checkBuf, 1, 4, mZipFp) != 4) {
LOG("EOCD check read failed\n"); LOG("EOCD check read failed\n");
result = -1; return -1;
goto bail;
} }
if (ZipEntry::getLongLE(checkBuf) != EndOfCentralDir::kSignature) { if (ZipEntry::getLongLE(checkBuf) != EndOfCentralDir::kSignature) {
LOG("EOCD read check failed\n"); LOG("EOCD read check failed\n");
result = -1; return -1;
goto bail;
} }
bail: return 0;
delete[] buf;
return result;
} }
/* /*