2 * Copyright (C) 2006 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 // Access to Zip archives.
23 #include <androidfw/ZipUtils.h>
24 #include <utils/Log.h>
29 #define DEF_MEM_LEVEL 8 // normally in zutil.h?
36 using namespace android;
39 * Some environments require the "b", some choke on it.
41 #define FILE_OPEN_RO "rb"
42 #define FILE_OPEN_RW "r+b"
43 #define FILE_OPEN_RW_CREATE "w+b"
45 /* should live somewhere else? */
46 static status_t errnoToStatus(int err)
49 return NAME_NOT_FOUND;
50 else if (err == EACCES)
51 return PERMISSION_DENIED;
57 * Open a file and parse its guts.
59 status_t ZipFile::open(const char* zipFileName, int flags)
61 bool newArchive = false;
63 assert(mZipFp == NULL); // no reopen
65 if ((flags & kOpenTruncate))
66 flags |= kOpenCreate; // trunc implies create
68 if ((flags & kOpenReadOnly) && (flags & kOpenReadWrite))
69 return INVALID_OPERATION; // not both
70 if (!((flags & kOpenReadOnly) || (flags & kOpenReadWrite)))
71 return INVALID_OPERATION; // not neither
72 if ((flags & kOpenCreate) && !(flags & kOpenReadWrite))
73 return INVALID_OPERATION; // create requires write
75 if (flags & kOpenTruncate) {
78 newArchive = (access(zipFileName, F_OK) != 0);
79 if (!(flags & kOpenCreate) && newArchive) {
80 /* not creating, must already exist */
81 ALOGD("File %s does not exist", zipFileName);
82 return NAME_NOT_FOUND;
87 const char* openflags;
88 if (flags & kOpenReadWrite) {
90 openflags = FILE_OPEN_RW_CREATE;
92 openflags = FILE_OPEN_RW;
94 openflags = FILE_OPEN_RO;
96 mZipFp = fopen(zipFileName, openflags);
99 ALOGD("fopen failed: %d\n", err);
100 return errnoToStatus(err);
106 * Load the central directory. If that fails, then this probably
107 * isn't a Zip archive.
109 result = readCentralDir();
112 * Newly-created. The EndOfCentralDir constructor actually
113 * sets everything to be the way we want it (all zeroes). We
114 * set mNeedCDRewrite so that we create *something* if the
115 * caller doesn't add any files. (We could also just unlink
116 * the file if it's brand new and nothing was added, but that's
117 * probably doing more than we really should -- the user might
118 * have a need for empty zip files.)
120 mNeedCDRewrite = true;
124 if (flags & kOpenReadOnly)
133 * Return the Nth entry in the archive.
135 ZipEntry* ZipFile::getEntryByIndex(int idx) const
137 if (idx < 0 || idx >= (int) mEntries.size())
140 return mEntries[idx];
144 * Find an entry by name.
146 ZipEntry* ZipFile::getEntryByName(const char* fileName) const
149 * Do a stupid linear string-compare search.
151 * There are various ways to speed this up, especially since it's rare
152 * to intermingle changes to the archive with "get by name" calls. We
153 * don't want to sort the mEntries vector itself, however, because
154 * it's used to recreate the Central Directory.
156 * (Hash table works, parallel list of pointers in sorted order is good.)
160 for (idx = mEntries.size()-1; idx >= 0; idx--) {
161 ZipEntry* pEntry = mEntries[idx];
162 if (!pEntry->getDeleted() &&
163 strcmp(fileName, pEntry->getFileName()) == 0)
173 * Empty the mEntries vector.
175 void ZipFile::discardEntries(void)
177 int count = mEntries.size();
180 delete mEntries[count];
187 * Find the central directory and read the contents.
189 * The fun thing about ZIP archives is that they may or may not be
190 * readable from start to end. In some cases, notably for archives
191 * that were written to stdout, the only length information is in the
192 * central directory at the end of the file.
194 * Of course, the central directory can be followed by a variable-length
195 * comment field, so we have to scan through it backwards. The comment
196 * is at most 64K, plus we have 18 bytes for the end-of-central-dir stuff
197 * itself, plus apparently sometimes people throw random junk on the end
198 * just for the fun of it.
200 * This is all a little wobbly. If the wrong value ends up in the EOCD
201 * area, we're hosed. This appears to be the way that everbody handles
202 * it though, so we're in pretty good company if this fails.
204 status_t ZipFile::readCentralDir(void)
206 status_t result = NO_ERROR;
207 unsigned char* buf = NULL;
208 off_t fileLength, seekStart;
212 fseek(mZipFp, 0, SEEK_END);
213 fileLength = ftell(mZipFp);
216 /* too small to be a ZIP archive? */
217 if (fileLength < EndOfCentralDir::kEOCDLen) {
218 ALOGD("Length is %ld -- too small\n", (long)fileLength);
219 result = INVALID_OPERATION;
223 buf = new unsigned char[EndOfCentralDir::kMaxEOCDSearch];
225 ALOGD("Failure allocating %d bytes for EOCD search",
226 EndOfCentralDir::kMaxEOCDSearch);
231 if (fileLength > EndOfCentralDir::kMaxEOCDSearch) {
232 seekStart = fileLength - EndOfCentralDir::kMaxEOCDSearch;
233 readAmount = EndOfCentralDir::kMaxEOCDSearch;
236 readAmount = (long) fileLength;
238 if (fseek(mZipFp, seekStart, SEEK_SET) != 0) {
239 ALOGD("Failure seeking to end of zip at %ld", (long) seekStart);
240 result = UNKNOWN_ERROR;
244 /* read the last part of the file into the buffer */
245 if (fread(buf, 1, readAmount, mZipFp) != (size_t) readAmount) {
246 ALOGD("short file? wanted %ld\n", readAmount);
247 result = UNKNOWN_ERROR;
251 /* find the end-of-central-dir magic */
252 for (i = readAmount - 4; i >= 0; i--) {
253 if (buf[i] == 0x50 &&
254 ZipEntry::getLongLE(&buf[i]) == EndOfCentralDir::kSignature)
256 ALOGV("+++ Found EOCD at buf+%d\n", i);
261 ALOGD("EOCD not found, not Zip\n");
262 result = INVALID_OPERATION;
266 /* extract eocd values */
267 result = mEOCD.readBuf(buf + i, readAmount - i);
268 if (result != NO_ERROR) {
269 ALOGD("Failure reading %ld bytes of EOCD values", readAmount - i);
274 if (mEOCD.mDiskNumber != 0 || mEOCD.mDiskWithCentralDir != 0 ||
275 mEOCD.mNumEntries != mEOCD.mTotalNumEntries)
277 ALOGD("Archive spanning not supported\n");
278 result = INVALID_OPERATION;
283 * So far so good. "mCentralDirSize" is the size in bytes of the
284 * central directory, so we can just seek back that far to find it.
285 * We can also seek forward mCentralDirOffset bytes from the
288 * We're not guaranteed to have the rest of the central dir in the
289 * buffer, nor are we guaranteed that the central dir will have any
290 * sort of convenient size. We need to skip to the start of it and
291 * read the header, then the other goodies.
293 * The only thing we really need right now is the file comment, which
294 * we're hoping to preserve.
296 if (fseek(mZipFp, mEOCD.mCentralDirOffset, SEEK_SET) != 0) {
297 ALOGD("Failure seeking to central dir offset %ld\n",
298 mEOCD.mCentralDirOffset);
299 result = UNKNOWN_ERROR;
304 * Loop through and read the central dir entries.
306 ALOGV("Scanning %d entries...\n", mEOCD.mTotalNumEntries);
308 for (entry = 0; entry < mEOCD.mTotalNumEntries; entry++) {
309 ZipEntry* pEntry = new ZipEntry;
311 result = pEntry->initFromCDE(mZipFp);
312 if (result != NO_ERROR) {
313 ALOGD("initFromCDE failed\n");
318 mEntries.add(pEntry);
323 * If all went well, we should now be back at the EOCD.
326 unsigned char checkBuf[4];
327 if (fread(checkBuf, 1, 4, mZipFp) != 4) {
328 ALOGD("EOCD check read failed\n");
329 result = INVALID_OPERATION;
332 if (ZipEntry::getLongLE(checkBuf) != EndOfCentralDir::kSignature) {
333 ALOGD("EOCD read check failed\n");
334 result = UNKNOWN_ERROR;
337 ALOGV("+++ EOCD read check passed\n");
347 * Add a new file to the archive.
349 * This requires creating and populating a ZipEntry structure, and copying
350 * the data into the file at the appropriate position. The "appropriate
351 * position" is the current location of the central directory, which we
352 * casually overwrite (we can put it back later).
354 * If we were concerned about safety, we would want to make all changes
355 * in a temp file and then overwrite the original after everything was
356 * safely written. Not really a concern for us.
358 status_t ZipFile::addCommon(const char* fileName, const void* data, size_t size,
359 const char* storageName, int sourceType, int compressionMethod,
362 ZipEntry* pEntry = NULL;
363 status_t result = NO_ERROR;
364 long lfhPosn, startPosn, endPosn, uncompressedLen;
365 FILE* inputFp = NULL;
370 return INVALID_OPERATION;
372 assert(compressionMethod == ZipEntry::kCompressDeflated ||
373 compressionMethod == ZipEntry::kCompressStored);
375 /* make sure we're in a reasonable state */
376 assert(mZipFp != NULL);
377 assert(mEntries.size() == mEOCD.mTotalNumEntries);
379 /* make sure it doesn't already exist */
380 if (getEntryByName(storageName) != NULL)
381 return ALREADY_EXISTS;
384 inputFp = fopen(fileName, FILE_OPEN_RO);
386 return errnoToStatus(errno);
389 if (fseek(mZipFp, mEOCD.mCentralDirOffset, SEEK_SET) != 0) {
390 result = UNKNOWN_ERROR;
394 pEntry = new ZipEntry;
395 pEntry->initNew(storageName, NULL);
398 * From here on out, failures are more interesting.
400 mNeedCDRewrite = true;
403 * Write the LFH, even though it's still mostly blank. We need it
404 * as a place-holder. In theory the LFH isn't necessary, but in
405 * practice some utilities demand it.
407 lfhPosn = ftell(mZipFp);
408 pEntry->mLFH.write(mZipFp);
409 startPosn = ftell(mZipFp);
412 * Copy the data in, possibly compressing it as we go.
414 if (sourceType == ZipEntry::kCompressStored) {
415 if (compressionMethod == ZipEntry::kCompressDeflated) {
417 result = compressFpToFp(mZipFp, inputFp, data, size, &crc);
418 if (result != NO_ERROR) {
419 ALOGD("compression failed, storing\n");
423 * Make sure it has compressed "enough". This probably ought
424 * to be set through an API call, but I don't expect our
425 * criteria to change over time.
427 long src = inputFp ? ftell(inputFp) : size;
428 long dst = ftell(mZipFp) - startPosn;
429 if (dst + (dst / 10) > src) {
430 ALOGD("insufficient compression (src=%ld dst=%ld), storing\n",
437 compressionMethod = ZipEntry::kCompressStored;
438 if (inputFp) rewind(inputFp);
439 fseek(mZipFp, startPosn, SEEK_SET);
440 /* fall through to kCompressStored case */
443 /* handle "no compression" request, or failed compression from above */
444 if (compressionMethod == ZipEntry::kCompressStored) {
446 result = copyFpToFp(mZipFp, inputFp, &crc);
448 result = copyDataToFp(mZipFp, data, size, &crc);
450 if (result != NO_ERROR) {
451 // don't need to truncate; happens in CDE rewrite
452 ALOGD("failed copying data in\n");
457 // currently seeked to end of file
458 uncompressedLen = inputFp ? ftell(inputFp) : size;
459 } else if (sourceType == ZipEntry::kCompressDeflated) {
460 /* we should support uncompressed-from-compressed, but it's not
461 * important right now */
462 assert(compressionMethod == ZipEntry::kCompressDeflated);
468 scanResult = ZipUtils::examineGzip(inputFp, &method, &uncompressedLen,
469 &compressedLen, &crc);
470 if (!scanResult || method != ZipEntry::kCompressDeflated) {
471 ALOGD("this isn't a deflated gzip file?");
472 result = UNKNOWN_ERROR;
476 result = copyPartialFpToFp(mZipFp, inputFp, compressedLen, NULL);
477 if (result != NO_ERROR) {
478 ALOGD("failed copying gzip data in\n");
483 result = UNKNOWN_ERROR;
488 * We could write the "Data Descriptor", but there doesn't seem to
489 * be any point since we're going to go back and write the LFH.
491 * Update file offsets.
493 endPosn = ftell(mZipFp); // seeked to end of compressed data
496 * Success! Fill out new values.
498 pEntry->setDataInfo(uncompressedLen, endPosn - startPosn, crc,
500 pEntry->setModWhen(modWhen);
501 pEntry->setLFHOffset(lfhPosn);
503 mEOCD.mTotalNumEntries++;
504 mEOCD.mCentralDirSize = 0; // mark invalid; set by flush()
505 mEOCD.mCentralDirOffset = endPosn;
508 * Go back and write the LFH.
510 if (fseek(mZipFp, lfhPosn, SEEK_SET) != 0) {
511 result = UNKNOWN_ERROR;
514 pEntry->mLFH.write(mZipFp);
517 * Add pEntry to the list.
519 mEntries.add(pEntry);
532 * Add an entry by copying it from another zip file. If "padding" is
533 * nonzero, the specified number of bytes will be added to the "extra"
534 * field in the header.
536 * If "ppEntry" is non-NULL, a pointer to the new entry will be returned.
538 status_t ZipFile::add(const ZipFile* pSourceZip, const ZipEntry* pSourceEntry,
539 int padding, ZipEntry** ppEntry)
541 ZipEntry* pEntry = NULL;
543 long lfhPosn, endPosn;
546 return INVALID_OPERATION;
548 /* make sure we're in a reasonable state */
549 assert(mZipFp != NULL);
550 assert(mEntries.size() == mEOCD.mTotalNumEntries);
552 if (fseek(mZipFp, mEOCD.mCentralDirOffset, SEEK_SET) != 0) {
553 result = UNKNOWN_ERROR;
557 pEntry = new ZipEntry;
558 if (pEntry == NULL) {
563 result = pEntry->initFromExternal(pSourceZip, pSourceEntry);
564 if (result != NO_ERROR)
567 result = pEntry->addPadding(padding);
568 if (result != NO_ERROR)
573 * From here on out, failures are more interesting.
575 mNeedCDRewrite = true;
578 * Write the LFH. Since we're not recompressing the data, we already
579 * have all of the fields filled out.
581 lfhPosn = ftell(mZipFp);
582 pEntry->mLFH.write(mZipFp);
585 * Copy the data over.
587 * If the "has data descriptor" flag is set, we want to copy the DD
588 * fields as well. This is a fixed-size area immediately following
591 if (fseek(pSourceZip->mZipFp, pSourceEntry->getFileOffset(), SEEK_SET) != 0)
593 result = UNKNOWN_ERROR;
598 copyLen = pSourceEntry->getCompressedLen();
599 if ((pSourceEntry->mLFH.mGPBitFlag & ZipEntry::kUsesDataDescr) != 0)
600 copyLen += ZipEntry::kDataDescriptorLen;
602 if (copyPartialFpToFp(mZipFp, pSourceZip->mZipFp, copyLen, NULL)
605 ALOGW("copy of '%s' failed\n", pEntry->mCDE.mFileName);
606 result = UNKNOWN_ERROR;
611 * Update file offsets.
613 endPosn = ftell(mZipFp);
616 * Success! Fill out new values.
618 pEntry->setLFHOffset(lfhPosn); // sets mCDE.mLocalHeaderRelOffset
620 mEOCD.mTotalNumEntries++;
621 mEOCD.mCentralDirSize = 0; // mark invalid; set by flush()
622 mEOCD.mCentralDirOffset = endPosn;
625 * Add pEntry to the list.
627 mEntries.add(pEntry);
640 * Copy all of the bytes in "src" to "dst".
642 * On exit, "srcFp" will be seeked to the end of the file, and "dstFp"
643 * will be seeked immediately past the data.
645 status_t ZipFile::copyFpToFp(FILE* dstFp, FILE* srcFp, unsigned long* pCRC32)
647 unsigned char tmpBuf[32768];
650 *pCRC32 = crc32(0L, Z_NULL, 0);
653 count = fread(tmpBuf, 1, sizeof(tmpBuf), srcFp);
654 if (ferror(srcFp) || ferror(dstFp))
655 return errnoToStatus(errno);
659 *pCRC32 = crc32(*pCRC32, tmpBuf, count);
661 if (fwrite(tmpBuf, 1, count, dstFp) != count) {
662 ALOGD("fwrite %d bytes failed\n", (int) count);
663 return UNKNOWN_ERROR;
671 * Copy all of the bytes in "src" to "dst".
673 * On exit, "dstFp" will be seeked immediately past the data.
675 status_t ZipFile::copyDataToFp(FILE* dstFp,
676 const void* data, size_t size, unsigned long* pCRC32)
678 *pCRC32 = crc32(0L, Z_NULL, 0);
680 *pCRC32 = crc32(*pCRC32, (const unsigned char*)data, size);
681 if (fwrite(data, 1, size, dstFp) != size) {
682 ALOGD("fwrite %d bytes failed\n", (int) size);
683 return UNKNOWN_ERROR;
691 * Copy some of the bytes in "src" to "dst".
693 * If "pCRC32" is NULL, the CRC will not be computed.
695 * On exit, "srcFp" will be seeked to the end of the file, and "dstFp"
696 * will be seeked immediately past the data just written.
698 status_t ZipFile::copyPartialFpToFp(FILE* dstFp, FILE* srcFp, long length,
699 unsigned long* pCRC32)
701 unsigned char tmpBuf[32768];
705 *pCRC32 = crc32(0L, Z_NULL, 0);
710 readSize = sizeof(tmpBuf);
711 if (readSize > length)
714 count = fread(tmpBuf, 1, readSize, srcFp);
715 if ((long) count != readSize) { // error or unexpected EOF
716 ALOGD("fread %d bytes failed\n", (int) readSize);
717 return UNKNOWN_ERROR;
721 *pCRC32 = crc32(*pCRC32, tmpBuf, count);
723 if (fwrite(tmpBuf, 1, count, dstFp) != count) {
724 ALOGD("fwrite %d bytes failed\n", (int) count);
725 return UNKNOWN_ERROR;
735 * Compress all of the data in "srcFp" and write it to "dstFp".
737 * On exit, "srcFp" will be seeked to the end of the file, and "dstFp"
738 * will be seeked immediately past the compressed data.
740 status_t ZipFile::compressFpToFp(FILE* dstFp, FILE* srcFp,
741 const void* data, size_t size, unsigned long* pCRC32)
743 status_t result = NO_ERROR;
744 const size_t kBufSize = 32768;
745 unsigned char* inBuf = NULL;
746 unsigned char* outBuf = NULL;
748 bool atEof = false; // no feof() aviailable yet
753 * Create an input buffer and an output buffer.
755 inBuf = new unsigned char[kBufSize];
756 outBuf = new unsigned char[kBufSize];
757 if (inBuf == NULL || outBuf == NULL) {
763 * Initialize the zlib stream.
765 memset(&zstream, 0, sizeof(zstream));
766 zstream.zalloc = Z_NULL;
767 zstream.zfree = Z_NULL;
768 zstream.opaque = Z_NULL;
769 zstream.next_in = NULL;
770 zstream.avail_in = 0;
771 zstream.next_out = outBuf;
772 zstream.avail_out = kBufSize;
773 zstream.data_type = Z_UNKNOWN;
775 zerr = deflateInit2(&zstream, Z_BEST_COMPRESSION,
776 Z_DEFLATED, -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
778 result = UNKNOWN_ERROR;
779 if (zerr == Z_VERSION_ERROR) {
780 ALOGE("Installed zlib is not compatible with linked version (%s)\n",
783 ALOGD("Call to deflateInit2 failed (zerr=%d)\n", zerr);
788 crc = crc32(0L, Z_NULL, 0);
791 * Loop while we have data.
797 /* only read if the input buffer is empty */
798 if (zstream.avail_in == 0 && !atEof) {
799 ALOGV("+++ reading %d bytes\n", (int)kBufSize);
801 getSize = size > kBufSize ? kBufSize : size;
802 memcpy(inBuf, data, getSize);
803 data = ((const char*)data) + getSize;
806 getSize = fread(inBuf, 1, kBufSize, srcFp);
808 ALOGD("deflate read failed (errno=%d)\n", errno);
812 if (getSize < kBufSize) {
813 ALOGV("+++ got %d bytes, EOF reached\n",
818 crc = crc32(crc, inBuf, getSize);
820 zstream.next_in = inBuf;
821 zstream.avail_in = getSize;
825 flush = Z_FINISH; /* tell zlib that we're done */
827 flush = Z_NO_FLUSH; /* more to come! */
829 zerr = deflate(&zstream, flush);
830 if (zerr != Z_OK && zerr != Z_STREAM_END) {
831 ALOGD("zlib deflate call failed (zerr=%d)\n", zerr);
832 result = UNKNOWN_ERROR;
836 /* write when we're full or when we're done */
837 if (zstream.avail_out == 0 ||
838 (zerr == Z_STREAM_END && zstream.avail_out != (uInt) kBufSize))
840 ALOGV("+++ writing %d bytes\n", (int) (zstream.next_out - outBuf));
841 if (fwrite(outBuf, 1, zstream.next_out - outBuf, dstFp) !=
842 (size_t)(zstream.next_out - outBuf))
844 ALOGD("write %d failed in deflate\n",
845 (int) (zstream.next_out - outBuf));
849 zstream.next_out = outBuf;
850 zstream.avail_out = kBufSize;
852 } while (zerr == Z_OK);
854 assert(zerr == Z_STREAM_END); /* other errors should've been caught */
859 deflateEnd(&zstream); /* free up any allocated structures */
869 * Mark an entry as deleted.
871 * We will eventually need to crunch the file down, but if several files
872 * are being removed (perhaps as part of an "update" process) we can make
873 * things considerably faster by deferring the removal to "flush" time.
875 status_t ZipFile::remove(ZipEntry* pEntry)
878 * Should verify that pEntry is actually part of this archive, and
879 * not some stray ZipEntry from a different file.
882 /* mark entry as deleted, and mark archive as dirty */
883 pEntry->setDeleted();
884 mNeedCDRewrite = true;
889 * Flush any pending writes.
891 * In particular, this will crunch out deleted entries, and write the
892 * Central Directory and EOCD if we have stomped on them.
894 status_t ZipFile::flush(void)
896 status_t result = NO_ERROR;
901 return INVALID_OPERATION;
905 assert(mZipFp != NULL);
907 result = crunchArchive();
908 if (result != NO_ERROR)
911 if (fseek(mZipFp, mEOCD.mCentralDirOffset, SEEK_SET) != 0)
912 return UNKNOWN_ERROR;
914 count = mEntries.size();
915 for (i = 0; i < count; i++) {
916 ZipEntry* pEntry = mEntries[i];
917 pEntry->mCDE.write(mZipFp);
920 eocdPosn = ftell(mZipFp);
921 mEOCD.mCentralDirSize = eocdPosn - mEOCD.mCentralDirOffset;
926 * If we had some stuff bloat up during compression and get replaced
927 * with plain files, or if we deleted some entries, there's a lot
928 * of wasted space at the end of the file. Remove it now.
930 if (ftruncate(fileno(mZipFp), ftell(mZipFp)) != 0) {
931 ALOGW("ftruncate failed %ld: %s\n", ftell(mZipFp), strerror(errno));
935 /* should we clear the "newly added" flag in all entries now? */
937 mNeedCDRewrite = false;
942 * Crunch deleted files out of an archive by shifting the later files down.
944 * Because we're not using a temp file, we do the operation inside the
947 status_t ZipFile::crunchArchive(void)
949 status_t result = NO_ERROR;
951 long delCount, adjust;
954 printf("CONTENTS:\n");
955 for (i = 0; i < (int) mEntries.size(); i++) {
956 printf(" %d: lfhOff=%ld del=%d\n",
957 i, mEntries[i]->getLFHOffset(), mEntries[i]->getDeleted());
959 printf(" END is %ld\n", (long) mEOCD.mCentralDirOffset);
963 * Roll through the set of files, shifting them as appropriate. We
964 * could probably get a slight performance improvement by sliding
965 * multiple files down at once (because we could use larger reads
966 * when operating on batches of small files), but it's not that useful.
968 count = mEntries.size();
969 delCount = adjust = 0;
970 for (i = 0; i < count; i++) {
971 ZipEntry* pEntry = mEntries[i];
974 if (pEntry->getLFHOffset() != 0) {
977 /* Get the length of this entry by finding the offset
978 * of the next entry. Directory entries don't have
979 * file offsets, so we need to find the next non-directory
983 for (int ii = i+1; nextOffset == 0 && ii < count; ii++)
984 nextOffset = mEntries[ii]->getLFHOffset();
986 nextOffset = mEOCD.mCentralDirOffset;
987 span = nextOffset - pEntry->getLFHOffset();
989 assert(span >= ZipEntry::LocalFileHeader::kLFHLen);
991 /* This is a directory entry. It doesn't have
992 * any actual file contents, so there's no need to
998 //printf("+++ %d: off=%ld span=%ld del=%d [count=%d]\n",
999 // i, pEntry->getLFHOffset(), span, pEntry->getDeleted(), count);
1001 if (pEntry->getDeleted()) {
1006 mEntries.removeAt(i);
1008 /* adjust loop control */
1011 } else if (span != 0 && adjust > 0) {
1012 /* shuffle this entry back */
1013 //printf("+++ Shuffling '%s' back %ld\n",
1014 // pEntry->getFileName(), adjust);
1015 result = filemove(mZipFp, pEntry->getLFHOffset() - adjust,
1016 pEntry->getLFHOffset(), span);
1017 if (result != NO_ERROR) {
1018 /* this is why you use a temp file */
1019 ALOGE("error during crunch - archive is toast\n");
1023 pEntry->setLFHOffset(pEntry->getLFHOffset() - adjust);
1028 * Fix EOCD info. We have to wait until the end to do some of this
1029 * because we use mCentralDirOffset to determine "span" for the
1032 mEOCD.mCentralDirOffset -= adjust;
1033 mEOCD.mNumEntries -= delCount;
1034 mEOCD.mTotalNumEntries -= delCount;
1035 mEOCD.mCentralDirSize = 0; // mark invalid; set by flush()
1037 assert(mEOCD.mNumEntries == mEOCD.mTotalNumEntries);
1038 assert(mEOCD.mNumEntries == count);
1044 * Works like memmove(), but on pieces of a file.
1046 status_t ZipFile::filemove(FILE* fp, off_t dst, off_t src, size_t n)
1048 if (dst == src || n <= 0)
1051 unsigned char readBuf[32768];
1054 /* shift stuff toward start of file; must read from start */
1056 size_t getSize = sizeof(readBuf);
1060 if (fseek(fp, (long) src, SEEK_SET) != 0) {
1061 ALOGD("filemove src seek %ld failed\n", (long) src);
1062 return UNKNOWN_ERROR;
1065 if (fread(readBuf, 1, getSize, fp) != getSize) {
1066 ALOGD("filemove read %ld off=%ld failed\n",
1067 (long) getSize, (long) src);
1068 return UNKNOWN_ERROR;
1071 if (fseek(fp, (long) dst, SEEK_SET) != 0) {
1072 ALOGD("filemove dst seek %ld failed\n", (long) dst);
1073 return UNKNOWN_ERROR;
1076 if (fwrite(readBuf, 1, getSize, fp) != getSize) {
1077 ALOGD("filemove write %ld off=%ld failed\n",
1078 (long) getSize, (long) dst);
1079 return UNKNOWN_ERROR;
1087 /* shift stuff toward end of file; must read from end */
1088 assert(false); // write this someday, maybe
1089 return UNKNOWN_ERROR;
1097 * Get the modification time from a file descriptor.
1099 time_t ZipFile::getModTime(int fd)
1103 if (fstat(fd, &sb) < 0) {
1104 ALOGD("HEY: fstat on fd %d failed\n", fd);
1112 #if 0 /* this is a bad idea */
1114 * Get a copy of the Zip file descriptor.
1116 * We don't allow this if the file was opened read-write because we tend
1117 * to leave the file contents in an uncertain state between calls to
1118 * flush(). The duplicated file descriptor should only be valid for reads.
1120 int ZipFile::getZipFd(void) const
1123 return INVALID_OPERATION;
1124 assert(mZipFp != NULL);
1127 fd = dup(fileno(mZipFp));
1129 ALOGD("didn't work, errno=%d\n", errno);
1141 bool ZipFile::uncompress(const ZipEntry* pEntry, void* buf) const
1147 // free the memory when you're done
1148 void* ZipFile::uncompress(const ZipEntry* entry)
1150 size_t unlen = entry->getUncompressedLen();
1151 size_t clen = entry->getCompressedLen();
1153 void* buf = malloc(unlen);
1158 fseek(mZipFp, 0, SEEK_SET);
1160 off_t offset = entry->getFileOffset();
1161 if (fseek(mZipFp, offset, SEEK_SET) != 0) {
1165 switch (entry->getCompressionMethod())
1167 case ZipEntry::kCompressStored: {
1168 ssize_t amt = fread(buf, 1, unlen, mZipFp);
1169 if (amt != (ssize_t)unlen) {
1173 printf("data...\n");
1174 const unsigned char* p = (unsigned char*)buf;
1175 const unsigned char* end = p+unlen;
1176 for (int i=0; i<32 && p < end; i++) {
1177 printf("0x%08x ", (int)(offset+(i*0x10)));
1178 for (int j=0; j<0x10 && p < end; j++) {
1179 printf(" %02x", *p);
1188 case ZipEntry::kCompressDeflated: {
1189 if (!ZipUtils::inflateToBuffer(mZipFp, buf, unlen, clen)) {
1206 * ===========================================================================
1207 * ZipFile::EndOfCentralDir
1208 * ===========================================================================
1212 * Read the end-of-central-dir fields.
1214 * "buf" should be positioned at the EOCD signature, and should contain
1215 * the entire EOCD area including the comment.
1217 status_t ZipFile::EndOfCentralDir::readBuf(const unsigned char* buf, int len)
1219 /* don't allow re-use */
1220 assert(mComment == NULL);
1222 if (len < kEOCDLen) {
1223 /* looks like ZIP file got truncated */
1224 ALOGD(" Zip EOCD: expected >= %d bytes, found %d\n",
1226 return INVALID_OPERATION;
1229 /* this should probably be an assert() */
1230 if (ZipEntry::getLongLE(&buf[0x00]) != kSignature)
1231 return UNKNOWN_ERROR;
1233 mDiskNumber = ZipEntry::getShortLE(&buf[0x04]);
1234 mDiskWithCentralDir = ZipEntry::getShortLE(&buf[0x06]);
1235 mNumEntries = ZipEntry::getShortLE(&buf[0x08]);
1236 mTotalNumEntries = ZipEntry::getShortLE(&buf[0x0a]);
1237 mCentralDirSize = ZipEntry::getLongLE(&buf[0x0c]);
1238 mCentralDirOffset = ZipEntry::getLongLE(&buf[0x10]);
1239 mCommentLen = ZipEntry::getShortLE(&buf[0x14]);
1241 // TODO: validate mCentralDirOffset
1243 if (mCommentLen > 0) {
1244 if (kEOCDLen + mCommentLen > len) {
1245 ALOGD("EOCD(%d) + comment(%d) exceeds len (%d)\n",
1246 kEOCDLen, mCommentLen, len);
1247 return UNKNOWN_ERROR;
1249 mComment = new unsigned char[mCommentLen];
1250 memcpy(mComment, buf + kEOCDLen, mCommentLen);
1257 * Write an end-of-central-directory section.
1259 status_t ZipFile::EndOfCentralDir::write(FILE* fp)
1261 unsigned char buf[kEOCDLen];
1263 ZipEntry::putLongLE(&buf[0x00], kSignature);
1264 ZipEntry::putShortLE(&buf[0x04], mDiskNumber);
1265 ZipEntry::putShortLE(&buf[0x06], mDiskWithCentralDir);
1266 ZipEntry::putShortLE(&buf[0x08], mNumEntries);
1267 ZipEntry::putShortLE(&buf[0x0a], mTotalNumEntries);
1268 ZipEntry::putLongLE(&buf[0x0c], mCentralDirSize);
1269 ZipEntry::putLongLE(&buf[0x10], mCentralDirOffset);
1270 ZipEntry::putShortLE(&buf[0x14], mCommentLen);
1272 if (fwrite(buf, 1, kEOCDLen, fp) != kEOCDLen)
1273 return UNKNOWN_ERROR;
1274 if (mCommentLen > 0) {
1275 assert(mComment != NULL);
1276 if (fwrite(mComment, mCommentLen, 1, fp) != mCommentLen)
1277 return UNKNOWN_ERROR;
1284 * Dump the contents of an EndOfCentralDir object.
1286 void ZipFile::EndOfCentralDir::dump(void) const
1288 ALOGD(" EndOfCentralDir contents:\n");
1289 ALOGD(" diskNum=%u diskWCD=%u numEnt=%u totalNumEnt=%u\n",
1290 mDiskNumber, mDiskWithCentralDir, mNumEntries, mTotalNumEntries);
1291 ALOGD(" centDirSize=%lu centDirOff=%lu commentLen=%u\n",
1292 mCentralDirSize, mCentralDirOffset, mCommentLen);