blob: 89aa874b4941b92ce5956e4f4613755ba006fde5 [file] [log] [blame]
The Android Open Source Projectd245d1d2008-10-21 07:00:00 -07001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17//
18// Access to Zip archives.
19//
20
21#define LOG_TAG "zip"
22
23#include "utils/ZipFile.h"
24#include "utils/ZipUtils.h"
25#include "utils/Log.h"
26
27#include <zlib.h>
28#define DEF_MEM_LEVEL 8 // normally in zutil.h?
29
30#include <memory.h>
31#include <sys/stat.h>
32#include <errno.h>
33#include <assert.h>
34
35using namespace android;
36
37/*
38 * Some environments require the "b", some choke on it.
39 */
40#define FILE_OPEN_RO "rb"
41#define FILE_OPEN_RW "r+b"
42#define FILE_OPEN_RW_CREATE "w+b"
43
44/* should live somewhere else? */
45static status_t errnoToStatus(int err)
46{
47 if (err == ENOENT)
48 return NAME_NOT_FOUND;
49 else if (err == EACCES)
50 return PERMISSION_DENIED;
51 else
52 return UNKNOWN_ERROR;
53}
54
55/*
56 * Open a file and parse its guts.
57 */
58status_t ZipFile::open(const char* zipFileName, int flags)
59{
60 bool newArchive = false;
61
62 assert(mZipFp == NULL); // no reopen
63
64 if ((flags & kOpenTruncate))
65 flags |= kOpenCreate; // trunc implies create
66
67 if ((flags & kOpenReadOnly) && (flags & kOpenReadWrite))
68 return INVALID_OPERATION; // not both
69 if (!((flags & kOpenReadOnly) || (flags & kOpenReadWrite)))
70 return INVALID_OPERATION; // not neither
71 if ((flags & kOpenCreate) && !(flags & kOpenReadWrite))
72 return INVALID_OPERATION; // create requires write
73
74 if (flags & kOpenTruncate) {
75 newArchive = true;
76 } else {
77 newArchive = (access(zipFileName, F_OK) != 0);
78 if (!(flags & kOpenCreate) && newArchive) {
79 /* not creating, must already exist */
80 LOGD("File %s does not exist", zipFileName);
81 return NAME_NOT_FOUND;
82 }
83 }
84
85 /* open the file */
86 const char* openflags;
87 if (flags & kOpenReadWrite) {
88 if (newArchive)
89 openflags = FILE_OPEN_RW_CREATE;
90 else
91 openflags = FILE_OPEN_RW;
92 } else {
93 openflags = FILE_OPEN_RO;
94 }
95 mZipFp = fopen(zipFileName, openflags);
96 if (mZipFp == NULL) {
97 int err = errno;
98 LOGD("fopen failed: %d\n", err);
99 return errnoToStatus(err);
100 }
101
102 status_t result;
103 if (!newArchive) {
104 /*
105 * Load the central directory. If that fails, then this probably
106 * isn't a Zip archive.
107 */
108 result = readCentralDir();
109 } else {
110 /*
111 * Newly-created. The EndOfCentralDir constructor actually
112 * sets everything to be the way we want it (all zeroes). We
113 * set mNeedCDRewrite so that we create *something* if the
114 * caller doesn't add any files. (We could also just unlink
115 * the file if it's brand new and nothing was added, but that's
116 * probably doing more than we really should -- the user might
117 * have a need for empty zip files.)
118 */
119 mNeedCDRewrite = true;
120 result = NO_ERROR;
121 }
122
123 if (flags & kOpenReadOnly)
124 mReadOnly = true;
125 else
126 assert(!mReadOnly);
127
128 return result;
129}
130
131/*
132 * Return the Nth entry in the archive.
133 */
134ZipEntry* ZipFile::getEntryByIndex(int idx) const
135{
136 if (idx < 0 || idx >= (int) mEntries.size())
137 return NULL;
138
139 return mEntries[idx];
140}
141
142/*
143 * Find an entry by name.
144 */
145ZipEntry* ZipFile::getEntryByName(const char* fileName) const
146{
147 /*
148 * Do a stupid linear string-compare search.
149 *
150 * There are various ways to speed this up, especially since it's rare
151 * to intermingle changes to the archive with "get by name" calls. We
152 * don't want to sort the mEntries vector itself, however, because
153 * it's used to recreate the Central Directory.
154 *
155 * (Hash table works, parallel list of pointers in sorted order is good.)
156 */
157 int idx;
158
159 for (idx = mEntries.size()-1; idx >= 0; idx--) {
160 ZipEntry* pEntry = mEntries[idx];
161 if (!pEntry->getDeleted() &&
162 strcmp(fileName, pEntry->getFileName()) == 0)
163 {
164 return pEntry;
165 }
166 }
167
168 return NULL;
169}
170
171/*
172 * Empty the mEntries vector.
173 */
174void ZipFile::discardEntries(void)
175{
176 int count = mEntries.size();
177
178 while (--count >= 0)
179 delete mEntries[count];
180
181 mEntries.clear();
182}
183
184
185/*
186 * Find the central directory and read the contents.
187 *
188 * The fun thing about ZIP archives is that they may or may not be
189 * readable from start to end. In some cases, notably for archives
190 * that were written to stdout, the only length information is in the
191 * central directory at the end of the file.
192 *
193 * Of course, the central directory can be followed by a variable-length
194 * comment field, so we have to scan through it backwards. The comment
195 * is at most 64K, plus we have 18 bytes for the end-of-central-dir stuff
196 * itself, plus apparently sometimes people throw random junk on the end
197 * just for the fun of it.
198 *
199 * This is all a little wobbly. If the wrong value ends up in the EOCD
200 * area, we're hosed. This appears to be the way that everbody handles
201 * it though, so we're in pretty good company if this fails.
202 */
203status_t ZipFile::readCentralDir(void)
204{
205 status_t result = NO_ERROR;
206 unsigned char* buf = NULL;
207 off_t fileLength, seekStart;
208 long readAmount;
209 int i;
210
211 fseek(mZipFp, 0, SEEK_END);
212 fileLength = ftell(mZipFp);
213 rewind(mZipFp);
214
215 /* too small to be a ZIP archive? */
216 if (fileLength < EndOfCentralDir::kEOCDLen) {
217 LOGD("Length is %ld -- too small\n", (long)fileLength);
218 result = INVALID_OPERATION;
219 goto bail;
220 }
221
222 buf = new unsigned char[EndOfCentralDir::kMaxEOCDSearch];
223 if (buf == NULL) {
224 LOGD("Failure allocating %d bytes for EOCD search",
225 EndOfCentralDir::kMaxEOCDSearch);
226 result = NO_MEMORY;
227 goto bail;
228 }
229
230 if (fileLength > EndOfCentralDir::kMaxEOCDSearch) {
231 seekStart = fileLength - EndOfCentralDir::kMaxEOCDSearch;
232 readAmount = EndOfCentralDir::kMaxEOCDSearch;
233 } else {
234 seekStart = 0;
235 readAmount = (long) fileLength;
236 }
237 if (fseek(mZipFp, seekStart, SEEK_SET) != 0) {
238 LOGD("Failure seeking to end of zip at %ld", (long) seekStart);
239 result = UNKNOWN_ERROR;
240 goto bail;
241 }
242
243 /* read the last part of the file into the buffer */
244 if (fread(buf, 1, readAmount, mZipFp) != (size_t) readAmount) {
245 LOGD("short file? wanted %ld\n", readAmount);
246 result = UNKNOWN_ERROR;
247 goto bail;
248 }
249
250 /* find the end-of-central-dir magic */
251 for (i = readAmount - 4; i >= 0; i--) {
252 if (buf[i] == 0x50 &&
253 ZipEntry::getLongLE(&buf[i]) == EndOfCentralDir::kSignature)
254 {
255 LOGV("+++ Found EOCD at buf+%d\n", i);
256 break;
257 }
258 }
259 if (i < 0) {
260 LOGD("EOCD not found, not Zip\n");
261 result = INVALID_OPERATION;
262 goto bail;
263 }
264
265 /* extract eocd values */
266 result = mEOCD.readBuf(buf + i, readAmount - i);
267 if (result != NO_ERROR) {
268 LOGD("Failure reading %ld bytes of EOCD values", readAmount - i);
269 goto bail;
270 }
271 //mEOCD.dump();
272
273 if (mEOCD.mDiskNumber != 0 || mEOCD.mDiskWithCentralDir != 0 ||
274 mEOCD.mNumEntries != mEOCD.mTotalNumEntries)
275 {
276 LOGD("Archive spanning not supported\n");
277 result = INVALID_OPERATION;
278 goto bail;
279 }
280
281 /*
282 * So far so good. "mCentralDirSize" is the size in bytes of the
283 * central directory, so we can just seek back that far to find it.
284 * We can also seek forward mCentralDirOffset bytes from the
285 * start of the file.
286 *
287 * We're not guaranteed to have the rest of the central dir in the
288 * buffer, nor are we guaranteed that the central dir will have any
289 * sort of convenient size. We need to skip to the start of it and
290 * read the header, then the other goodies.
291 *
292 * The only thing we really need right now is the file comment, which
293 * we're hoping to preserve.
294 */
295 if (fseek(mZipFp, mEOCD.mCentralDirOffset, SEEK_SET) != 0) {
296 LOGD("Failure seeking to central dir offset %ld\n",
297 mEOCD.mCentralDirOffset);
298 result = UNKNOWN_ERROR;
299 goto bail;
300 }
301
302 /*
303 * Loop through and read the central dir entries.
304 */
305 LOGV("Scanning %d entries...\n", mEOCD.mTotalNumEntries);
306 int entry;
307 for (entry = 0; entry < mEOCD.mTotalNumEntries; entry++) {
308 ZipEntry* pEntry = new ZipEntry;
309
310 result = pEntry->initFromCDE(mZipFp);
311 if (result != NO_ERROR) {
312 LOGD("initFromCDE failed\n");
313 delete pEntry;
314 goto bail;
315 }
316
317 mEntries.add(pEntry);
318 }
319
320
321 /*
322 * If all went well, we should now be back at the EOCD.
323 */
324 {
325 unsigned char checkBuf[4];
326 if (fread(checkBuf, 1, 4, mZipFp) != 4) {
327 LOGD("EOCD check read failed\n");
328 result = INVALID_OPERATION;
329 goto bail;
330 }
331 if (ZipEntry::getLongLE(checkBuf) != EndOfCentralDir::kSignature) {
332 LOGD("EOCD read check failed\n");
333 result = UNKNOWN_ERROR;
334 goto bail;
335 }
336 LOGV("+++ EOCD read check passed\n");
337 }
338
339bail:
340 delete[] buf;
341 return result;
342}
343
344
345/*
346 * Add a new file to the archive.
347 *
348 * This requires creating and populating a ZipEntry structure, and copying
349 * the data into the file at the appropriate position. The "appropriate
350 * position" is the current location of the central directory, which we
351 * casually overwrite (we can put it back later).
352 *
353 * If we were concerned about safety, we would want to make all changes
354 * in a temp file and then overwrite the original after everything was
355 * safely written. Not really a concern for us.
356 */
357status_t ZipFile::addCommon(const char* fileName, const void* data, size_t size,
358 const char* storageName, int sourceType, int compressionMethod,
359 ZipEntry** ppEntry)
360{
361 ZipEntry* pEntry = NULL;
362 status_t result = NO_ERROR;
363 long lfhPosn, startPosn, endPosn, uncompressedLen;
364 FILE* inputFp = NULL;
365 unsigned long crc;
366 time_t modWhen;
367
368 if (mReadOnly)
369 return INVALID_OPERATION;
370
371 assert(compressionMethod == ZipEntry::kCompressDeflated ||
372 compressionMethod == ZipEntry::kCompressStored);
373
374 /* make sure we're in a reasonable state */
375 assert(mZipFp != NULL);
376 assert(mEntries.size() == mEOCD.mTotalNumEntries);
377
378 /* make sure it doesn't already exist */
379 if (getEntryByName(storageName) != NULL)
380 return ALREADY_EXISTS;
381
382 if (!data) {
383 inputFp = fopen(fileName, FILE_OPEN_RO);
384 if (inputFp == NULL)
385 return errnoToStatus(errno);
386 }
387
388 if (fseek(mZipFp, mEOCD.mCentralDirOffset, SEEK_SET) != 0) {
389 result = UNKNOWN_ERROR;
390 goto bail;
391 }
392
393 pEntry = new ZipEntry;
394 pEntry->initNew(storageName, NULL);
395
396 /*
397 * From here on out, failures are more interesting.
398 */
399 mNeedCDRewrite = true;
400
401 /*
402 * Write the LFH, even though it's still mostly blank. We need it
403 * as a place-holder. In theory the LFH isn't necessary, but in
404 * practice some utilities demand it.
405 */
406 lfhPosn = ftell(mZipFp);
407 pEntry->mLFH.write(mZipFp);
408 startPosn = ftell(mZipFp);
409
410 /*
411 * Copy the data in, possibly compressing it as we go.
412 */
413 if (sourceType == ZipEntry::kCompressStored) {
414 if (compressionMethod == ZipEntry::kCompressDeflated) {
415 bool failed = false;
416 result = compressFpToFp(mZipFp, inputFp, data, size, &crc);
417 if (result != NO_ERROR) {
418 LOGD("compression failed, storing\n");
419 failed = true;
420 } else {
421 /*
422 * Make sure it has compressed "enough". This probably ought
423 * to be set through an API call, but I don't expect our
424 * criteria to change over time.
425 */
426 long src = inputFp ? ftell(inputFp) : size;
427 long dst = ftell(mZipFp) - startPosn;
428 if (dst + (dst / 10) > src) {
429 LOGD("insufficient compression (src=%ld dst=%ld), storing\n",
430 src, dst);
431 failed = true;
432 }
433 }
434
435 if (failed) {
436 compressionMethod = ZipEntry::kCompressStored;
437 if (inputFp) rewind(inputFp);
438 fseek(mZipFp, startPosn, SEEK_SET);
439 /* fall through to kCompressStored case */
440 }
441 }
442 /* handle "no compression" request, or failed compression from above */
443 if (compressionMethod == ZipEntry::kCompressStored) {
444 if (inputFp) {
445 result = copyFpToFp(mZipFp, inputFp, &crc);
446 } else {
447 result = copyDataToFp(mZipFp, data, size, &crc);
448 }
449 if (result != NO_ERROR) {
450 // don't need to truncate; happens in CDE rewrite
451 LOGD("failed copying data in\n");
452 goto bail;
453 }
454 }
455
456 // currently seeked to end of file
457 uncompressedLen = inputFp ? ftell(inputFp) : size;
458 } else if (sourceType == ZipEntry::kCompressDeflated) {
459 /* we should support uncompressed-from-compressed, but it's not
460 * important right now */
461 assert(compressionMethod == ZipEntry::kCompressDeflated);
462
463 bool scanResult;
464 int method;
465 long compressedLen;
466
467 scanResult = ZipUtils::examineGzip(inputFp, &method, &uncompressedLen,
468 &compressedLen, &crc);
469 if (!scanResult || method != ZipEntry::kCompressDeflated) {
470 LOGD("this isn't a deflated gzip file?");
471 result = UNKNOWN_ERROR;
472 goto bail;
473 }
474
475 result = copyPartialFpToFp(mZipFp, inputFp, compressedLen, NULL);
476 if (result != NO_ERROR) {
477 LOGD("failed copying gzip data in\n");
478 goto bail;
479 }
480 } else {
481 assert(false);
482 result = UNKNOWN_ERROR;
483 goto bail;
484 }
485
486 /*
487 * We could write the "Data Descriptor", but there doesn't seem to
488 * be any point since we're going to go back and write the LFH.
489 *
490 * Update file offsets.
491 */
492 endPosn = ftell(mZipFp); // seeked to end of compressed data
493
494 /*
495 * Success! Fill out new values.
496 */
497 pEntry->setDataInfo(uncompressedLen, endPosn - startPosn, crc,
498 compressionMethod);
499 modWhen = getModTime(inputFp ? fileno(inputFp) : fileno(mZipFp));
500 pEntry->setModWhen(modWhen);
501 pEntry->setLFHOffset(lfhPosn);
502 mEOCD.mNumEntries++;
503 mEOCD.mTotalNumEntries++;
504 mEOCD.mCentralDirSize = 0; // mark invalid; set by flush()
505 mEOCD.mCentralDirOffset = endPosn;
506
507 /*
508 * Go back and write the LFH.
509 */
510 if (fseek(mZipFp, lfhPosn, SEEK_SET) != 0) {
511 result = UNKNOWN_ERROR;
512 goto bail;
513 }
514 pEntry->mLFH.write(mZipFp);
515
516 /*
517 * Add pEntry to the list.
518 */
519 mEntries.add(pEntry);
520 if (ppEntry != NULL)
521 *ppEntry = pEntry;
522 pEntry = NULL;
523
524bail:
525 if (inputFp != NULL)
526 fclose(inputFp);
527 delete pEntry;
528 return result;
529}
530
531/*
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.
535 *
536 * If "ppEntry" is non-NULL, a pointer to the new entry will be returned.
537 */
538status_t ZipFile::add(const ZipFile* pSourceZip, const ZipEntry* pSourceEntry,
539 int padding, ZipEntry** ppEntry)
540{
541 ZipEntry* pEntry = NULL;
542 status_t result;
543 long lfhPosn, endPosn;
544
545 if (mReadOnly)
546 return INVALID_OPERATION;
547
548 /* make sure we're in a reasonable state */
549 assert(mZipFp != NULL);
550 assert(mEntries.size() == mEOCD.mTotalNumEntries);
551
552 if (fseek(mZipFp, mEOCD.mCentralDirOffset, SEEK_SET) != 0) {
553 result = UNKNOWN_ERROR;
554 goto bail;
555 }
556
557 pEntry = new ZipEntry;
558 if (pEntry == NULL) {
559 result = NO_MEMORY;
560 goto bail;
561 }
562
563 result = pEntry->initFromExternal(pSourceZip, pSourceEntry);
564 if (result != NO_ERROR)
565 goto bail;
566 if (padding != 0) {
567 result = pEntry->addPadding(padding);
568 if (result != NO_ERROR)
569 goto bail;
570 }
571
572 /*
573 * From here on out, failures are more interesting.
574 */
575 mNeedCDRewrite = true;
576
577 /*
578 * Write the LFH. Since we're not recompressing the data, we already
579 * have all of the fields filled out.
580 */
581 lfhPosn = ftell(mZipFp);
582 pEntry->mLFH.write(mZipFp);
583
584 /*
585 * Copy the data over.
586 *
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
589 * the data.
590 */
591 if (fseek(pSourceZip->mZipFp, pSourceEntry->getFileOffset(), SEEK_SET) != 0)
592 {
593 result = UNKNOWN_ERROR;
594 goto bail;
595 }
596
597 off_t copyLen;
598 copyLen = pSourceEntry->getCompressedLen();
599 if ((pSourceEntry->mLFH.mGPBitFlag & ZipEntry::kUsesDataDescr) != 0)
600 copyLen += ZipEntry::kDataDescriptorLen;
601
602 if (copyPartialFpToFp(mZipFp, pSourceZip->mZipFp, copyLen, NULL)
603 != NO_ERROR)
604 {
605 LOGW("copy of '%s' failed\n", pEntry->mCDE.mFileName);
606 result = UNKNOWN_ERROR;
607 goto bail;
608 }
609
610 /*
611 * Update file offsets.
612 */
613 endPosn = ftell(mZipFp);
614
615 /*
616 * Success! Fill out new values.
617 */
618 pEntry->setLFHOffset(lfhPosn); // sets mCDE.mLocalHeaderRelOffset
619 mEOCD.mNumEntries++;
620 mEOCD.mTotalNumEntries++;
621 mEOCD.mCentralDirSize = 0; // mark invalid; set by flush()
622 mEOCD.mCentralDirOffset = endPosn;
623
624 /*
625 * Add pEntry to the list.
626 */
627 mEntries.add(pEntry);
628 if (ppEntry != NULL)
629 *ppEntry = pEntry;
630 pEntry = NULL;
631
632 result = NO_ERROR;
633
634bail:
635 delete pEntry;
636 return result;
637}
638
639/*
640 * Copy all of the bytes in "src" to "dst".
641 *
642 * On exit, "srcFp" will be seeked to the end of the file, and "dstFp"
643 * will be seeked immediately past the data.
644 */
645status_t ZipFile::copyFpToFp(FILE* dstFp, FILE* srcFp, unsigned long* pCRC32)
646{
647 unsigned char tmpBuf[32768];
648 size_t count;
649
650 *pCRC32 = crc32(0L, Z_NULL, 0);
651
652 while (1) {
653 count = fread(tmpBuf, 1, sizeof(tmpBuf), srcFp);
654 if (ferror(srcFp) || ferror(dstFp))
655 return errnoToStatus(errno);
656 if (count == 0)
657 break;
658
659 *pCRC32 = crc32(*pCRC32, tmpBuf, count);
660
661 if (fwrite(tmpBuf, 1, count, dstFp) != count) {
662 LOGD("fwrite %d bytes failed\n", (int) count);
663 return UNKNOWN_ERROR;
664 }
665 }
666
667 return NO_ERROR;
668}
669
670/*
671 * Copy all of the bytes in "src" to "dst".
672 *
673 * On exit, "dstFp" will be seeked immediately past the data.
674 */
675status_t ZipFile::copyDataToFp(FILE* dstFp,
676 const void* data, size_t size, unsigned long* pCRC32)
677{
678 size_t count;
679
680 *pCRC32 = crc32(0L, Z_NULL, 0);
681 if (size > 0) {
682 *pCRC32 = crc32(*pCRC32, (const unsigned char*)data, size);
683 if (fwrite(data, 1, size, dstFp) != size) {
684 LOGD("fwrite %d bytes failed\n", (int) size);
685 return UNKNOWN_ERROR;
686 }
687 }
688
689 return NO_ERROR;
690}
691
692/*
693 * Copy some of the bytes in "src" to "dst".
694 *
695 * If "pCRC32" is NULL, the CRC will not be computed.
696 *
697 * On exit, "srcFp" will be seeked to the end of the file, and "dstFp"
698 * will be seeked immediately past the data just written.
699 */
700status_t ZipFile::copyPartialFpToFp(FILE* dstFp, FILE* srcFp, long length,
701 unsigned long* pCRC32)
702{
703 unsigned char tmpBuf[32768];
704 size_t count;
705
706 if (pCRC32 != NULL)
707 *pCRC32 = crc32(0L, Z_NULL, 0);
708
709 while (length) {
710 long readSize;
711
712 readSize = sizeof(tmpBuf);
713 if (readSize > length)
714 readSize = length;
715
716 count = fread(tmpBuf, 1, readSize, srcFp);
717 if ((long) count != readSize) { // error or unexpected EOF
718 LOGD("fread %d bytes failed\n", (int) readSize);
719 return UNKNOWN_ERROR;
720 }
721
722 if (pCRC32 != NULL)
723 *pCRC32 = crc32(*pCRC32, tmpBuf, count);
724
725 if (fwrite(tmpBuf, 1, count, dstFp) != count) {
726 LOGD("fwrite %d bytes failed\n", (int) count);
727 return UNKNOWN_ERROR;
728 }
729
730 length -= readSize;
731 }
732
733 return NO_ERROR;
734}
735
736/*
737 * Compress all of the data in "srcFp" and write it to "dstFp".
738 *
739 * On exit, "srcFp" will be seeked to the end of the file, and "dstFp"
740 * will be seeked immediately past the compressed data.
741 */
742status_t ZipFile::compressFpToFp(FILE* dstFp, FILE* srcFp,
743 const void* data, size_t size, unsigned long* pCRC32)
744{
745 status_t result = NO_ERROR;
746 const size_t kBufSize = 32768;
747 unsigned char* inBuf = NULL;
748 unsigned char* outBuf = NULL;
749 z_stream zstream;
750 bool atEof = false; // no feof() aviailable yet
751 unsigned long crc;
752 int zerr;
753
754 /*
755 * Create an input buffer and an output buffer.
756 */
757 inBuf = new unsigned char[kBufSize];
758 outBuf = new unsigned char[kBufSize];
759 if (inBuf == NULL || outBuf == NULL) {
760 result = NO_MEMORY;
761 goto bail;
762 }
763
764 /*
765 * Initialize the zlib stream.
766 */
767 memset(&zstream, 0, sizeof(zstream));
768 zstream.zalloc = Z_NULL;
769 zstream.zfree = Z_NULL;
770 zstream.opaque = Z_NULL;
771 zstream.next_in = NULL;
772 zstream.avail_in = 0;
773 zstream.next_out = outBuf;
774 zstream.avail_out = kBufSize;
775 zstream.data_type = Z_UNKNOWN;
776
777 zerr = deflateInit2(&zstream, Z_BEST_COMPRESSION,
778 Z_DEFLATED, -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
779 if (zerr != Z_OK) {
780 result = UNKNOWN_ERROR;
781 if (zerr == Z_VERSION_ERROR) {
782 LOGE("Installed zlib is not compatible with linked version (%s)\n",
783 ZLIB_VERSION);
784 } else {
785 LOGD("Call to deflateInit2 failed (zerr=%d)\n", zerr);
786 }
787 goto bail;
788 }
789
790 crc = crc32(0L, Z_NULL, 0);
791
792 /*
793 * Loop while we have data.
794 */
795 do {
796 size_t getSize;
797 int flush;
798
799 /* only read if the input buffer is empty */
800 if (zstream.avail_in == 0 && !atEof) {
801 LOGV("+++ reading %d bytes\n", (int)kBufSize);
802 if (data) {
803 getSize = size > kBufSize ? kBufSize : size;
804 memcpy(inBuf, data, getSize);
805 data = ((const char*)data) + getSize;
806 size -= getSize;
807 } else {
808 getSize = fread(inBuf, 1, kBufSize, srcFp);
809 if (ferror(srcFp)) {
810 LOGD("deflate read failed (errno=%d)\n", errno);
811 goto z_bail;
812 }
813 }
814 if (getSize < kBufSize) {
815 LOGV("+++ got %d bytes, EOF reached\n",
816 (int)getSize);
817 atEof = true;
818 }
819
820 crc = crc32(crc, inBuf, getSize);
821
822 zstream.next_in = inBuf;
823 zstream.avail_in = getSize;
824 }
825
826 if (atEof)
827 flush = Z_FINISH; /* tell zlib that we're done */
828 else
829 flush = Z_NO_FLUSH; /* more to come! */
830
831 zerr = deflate(&zstream, flush);
832 if (zerr != Z_OK && zerr != Z_STREAM_END) {
833 LOGD("zlib deflate call failed (zerr=%d)\n", zerr);
834 result = UNKNOWN_ERROR;
835 goto z_bail;
836 }
837
838 /* write when we're full or when we're done */
839 if (zstream.avail_out == 0 ||
840 (zerr == Z_STREAM_END && zstream.avail_out != (uInt) kBufSize))
841 {
842 LOGV("+++ writing %d bytes\n", (int) (zstream.next_out - outBuf));
843 if (fwrite(outBuf, 1, zstream.next_out - outBuf, dstFp) !=
844 (size_t)(zstream.next_out - outBuf))
845 {
846 LOGD("write %d failed in deflate\n",
847 (int) (zstream.next_out - outBuf));
848 goto z_bail;
849 }
850
851 zstream.next_out = outBuf;
852 zstream.avail_out = kBufSize;
853 }
854 } while (zerr == Z_OK);
855
856 assert(zerr == Z_STREAM_END); /* other errors should've been caught */
857
858 *pCRC32 = crc;
859
860z_bail:
861 deflateEnd(&zstream); /* free up any allocated structures */
862
863bail:
864 delete[] inBuf;
865 delete[] outBuf;
866
867 return result;
868}
869
870/*
871 * Mark an entry as deleted.
872 *
873 * We will eventually need to crunch the file down, but if several files
874 * are being removed (perhaps as part of an "update" process) we can make
875 * things considerably faster by deferring the removal to "flush" time.
876 */
877status_t ZipFile::remove(ZipEntry* pEntry)
878{
879 /*
880 * Should verify that pEntry is actually part of this archive, and
881 * not some stray ZipEntry from a different file.
882 */
883
884 /* mark entry as deleted, and mark archive as dirty */
885 pEntry->setDeleted();
886 mNeedCDRewrite = true;
887 return NO_ERROR;
888}
889
890/*
891 * Flush any pending writes.
892 *
893 * In particular, this will crunch out deleted entries, and write the
894 * Central Directory and EOCD if we have stomped on them.
895 */
896status_t ZipFile::flush(void)
897{
898 status_t result = NO_ERROR;
899 long eocdPosn;
900 int i, count;
901
902 if (mReadOnly)
903 return INVALID_OPERATION;
904 if (!mNeedCDRewrite)
905 return NO_ERROR;
906
907 assert(mZipFp != NULL);
908
909 result = crunchArchive();
910 if (result != NO_ERROR)
911 return result;
912
913 if (fseek(mZipFp, mEOCD.mCentralDirOffset, SEEK_SET) != 0)
914 return UNKNOWN_ERROR;
915
916 count = mEntries.size();
917 for (i = 0; i < count; i++) {
918 ZipEntry* pEntry = mEntries[i];
919 pEntry->mCDE.write(mZipFp);
920 }
921
922 eocdPosn = ftell(mZipFp);
923 mEOCD.mCentralDirSize = eocdPosn - mEOCD.mCentralDirOffset;
924
925 mEOCD.write(mZipFp);
926
927 /*
928 * If we had some stuff bloat up during compression and get replaced
929 * with plain files, or if we deleted some entries, there's a lot
930 * of wasted space at the end of the file. Remove it now.
931 */
932 if (ftruncate(fileno(mZipFp), ftell(mZipFp)) != 0) {
933 LOGW("ftruncate failed %ld: %s\n", ftell(mZipFp), strerror(errno));
934 // not fatal
935 }
936
937 /* should we clear the "newly added" flag in all entries now? */
938
939 mNeedCDRewrite = false;
940 return NO_ERROR;
941}
942
943/*
944 * Crunch deleted files out of an archive by shifting the later files down.
945 *
946 * Because we're not using a temp file, we do the operation inside the
947 * current file.
948 */
949status_t ZipFile::crunchArchive(void)
950{
951 status_t result = NO_ERROR;
952 int i, count;
953 long delCount, adjust;
954
955#if 0
956 printf("CONTENTS:\n");
957 for (i = 0; i < (int) mEntries.size(); i++) {
958 printf(" %d: lfhOff=%ld del=%d\n",
959 i, mEntries[i]->getLFHOffset(), mEntries[i]->getDeleted());
960 }
961 printf(" END is %ld\n", (long) mEOCD.mCentralDirOffset);
962#endif
963
964 /*
965 * Roll through the set of files, shifting them as appropriate. We
966 * could probably get a slight performance improvement by sliding
967 * multiple files down at once (because we could use larger reads
968 * when operating on batches of small files), but it's not that useful.
969 */
970 count = mEntries.size();
971 delCount = adjust = 0;
972 for (i = 0; i < count; i++) {
973 ZipEntry* pEntry = mEntries[i];
974 long span;
975
976 if (pEntry->getLFHOffset() != 0) {
977 long nextOffset;
978
979 /* Get the length of this entry by finding the offset
980 * of the next entry. Directory entries don't have
981 * file offsets, so we need to find the next non-directory
982 * entry.
983 */
984 nextOffset = 0;
985 for (int ii = i+1; nextOffset == 0 && ii < count; ii++)
986 nextOffset = mEntries[ii]->getLFHOffset();
987 if (nextOffset == 0)
988 nextOffset = mEOCD.mCentralDirOffset;
989 span = nextOffset - pEntry->getLFHOffset();
990
991 assert(span >= ZipEntry::LocalFileHeader::kLFHLen);
992 } else {
993 /* This is a directory entry. It doesn't have
994 * any actual file contents, so there's no need to
995 * move anything.
996 */
997 span = 0;
998 }
999
1000 //printf("+++ %d: off=%ld span=%ld del=%d [count=%d]\n",
1001 // i, pEntry->getLFHOffset(), span, pEntry->getDeleted(), count);
1002
1003 if (pEntry->getDeleted()) {
1004 adjust += span;
1005 delCount++;
1006
1007 delete pEntry;
1008 mEntries.removeAt(i);
1009
1010 /* adjust loop control */
1011 count--;
1012 i--;
1013 } else if (span != 0 && adjust > 0) {
1014 /* shuffle this entry back */
1015 //printf("+++ Shuffling '%s' back %ld\n",
1016 // pEntry->getFileName(), adjust);
1017 result = filemove(mZipFp, pEntry->getLFHOffset() - adjust,
1018 pEntry->getLFHOffset(), span);
1019 if (result != NO_ERROR) {
1020 /* this is why you use a temp file */
1021 LOGE("error during crunch - archive is toast\n");
1022 return result;
1023 }
1024
1025 pEntry->setLFHOffset(pEntry->getLFHOffset() - adjust);
1026 }
1027 }
1028
1029 /*
1030 * Fix EOCD info. We have to wait until the end to do some of this
1031 * because we use mCentralDirOffset to determine "span" for the
1032 * last entry.
1033 */
1034 mEOCD.mCentralDirOffset -= adjust;
1035 mEOCD.mNumEntries -= delCount;
1036 mEOCD.mTotalNumEntries -= delCount;
1037 mEOCD.mCentralDirSize = 0; // mark invalid; set by flush()
1038
1039 assert(mEOCD.mNumEntries == mEOCD.mTotalNumEntries);
1040 assert(mEOCD.mNumEntries == count);
1041
1042 return result;
1043}
1044
1045/*
1046 * Works like memmove(), but on pieces of a file.
1047 */
1048status_t ZipFile::filemove(FILE* fp, off_t dst, off_t src, size_t n)
1049{
1050 if (dst == src || n <= 0)
1051 return NO_ERROR;
1052
1053 unsigned char readBuf[32768];
1054
1055 if (dst < src) {
1056 /* shift stuff toward start of file; must read from start */
1057 while (n != 0) {
1058 size_t getSize = sizeof(readBuf);
1059 if (getSize > n)
1060 getSize = n;
1061
1062 if (fseek(fp, (long) src, SEEK_SET) != 0) {
1063 LOGD("filemove src seek %ld failed\n", (long) src);
1064 return UNKNOWN_ERROR;
1065 }
1066
1067 if (fread(readBuf, 1, getSize, fp) != getSize) {
1068 LOGD("filemove read %ld off=%ld failed\n",
1069 (long) getSize, (long) src);
1070 return UNKNOWN_ERROR;
1071 }
1072
1073 if (fseek(fp, (long) dst, SEEK_SET) != 0) {
1074 LOGD("filemove dst seek %ld failed\n", (long) dst);
1075 return UNKNOWN_ERROR;
1076 }
1077
1078 if (fwrite(readBuf, 1, getSize, fp) != getSize) {
1079 LOGD("filemove write %ld off=%ld failed\n",
1080 (long) getSize, (long) dst);
1081 return UNKNOWN_ERROR;
1082 }
1083
1084 src += getSize;
1085 dst += getSize;
1086 n -= getSize;
1087 }
1088 } else {
1089 /* shift stuff toward end of file; must read from end */
1090 assert(false); // write this someday, maybe
1091 return UNKNOWN_ERROR;
1092 }
1093
1094 return NO_ERROR;
1095}
1096
1097
1098/*
1099 * Get the modification time from a file descriptor.
1100 */
1101time_t ZipFile::getModTime(int fd)
1102{
1103 struct stat sb;
1104
1105 if (fstat(fd, &sb) < 0) {
1106 LOGD("HEY: fstat on fd %d failed\n", fd);
1107 return (time_t) -1;
1108 }
1109
1110 return sb.st_mtime;
1111}
1112
1113
1114#if 0 /* this is a bad idea */
1115/*
1116 * Get a copy of the Zip file descriptor.
1117 *
1118 * We don't allow this if the file was opened read-write because we tend
1119 * to leave the file contents in an uncertain state between calls to
1120 * flush(). The duplicated file descriptor should only be valid for reads.
1121 */
1122int ZipFile::getZipFd(void) const
1123{
1124 if (!mReadOnly)
1125 return INVALID_OPERATION;
1126 assert(mZipFp != NULL);
1127
1128 int fd;
1129 fd = dup(fileno(mZipFp));
1130 if (fd < 0) {
1131 LOGD("didn't work, errno=%d\n", errno);
1132 }
1133
1134 return fd;
1135}
1136#endif
1137
1138
1139#if 0
1140/*
1141 * Expand data.
1142 */
1143bool ZipFile::uncompress(const ZipEntry* pEntry, void* buf) const
1144{
1145 return false;
1146}
1147#endif
1148
1149// free the memory when you're done
1150void* ZipFile::uncompress(const ZipEntry* entry)
1151{
1152 size_t unlen = entry->getUncompressedLen();
1153 size_t clen = entry->getCompressedLen();
1154
1155 void* buf = malloc(unlen);
1156 if (buf == NULL) {
1157 return NULL;
1158 }
1159
1160 fseek(mZipFp, 0, SEEK_SET);
1161
1162 off_t offset = entry->getFileOffset();
1163 if (fseek(mZipFp, offset, SEEK_SET) != 0) {
1164 goto bail;
1165 }
1166
1167 switch (entry->getCompressionMethod())
1168 {
1169 case ZipEntry::kCompressStored: {
1170 ssize_t amt = fread(buf, 1, unlen, mZipFp);
1171 if (amt != (ssize_t)unlen) {
1172 goto bail;
1173 }
1174#if 0
1175 printf("data...\n");
1176 const unsigned char* p = (unsigned char*)buf;
1177 const unsigned char* end = p+unlen;
1178 for (int i=0; i<32 && p < end; i++) {
1179 printf("0x%08x ", (int)(offset+(i*0x10)));
1180 for (int j=0; j<0x10 && p < end; j++) {
1181 printf(" %02x", *p);
1182 p++;
1183 }
1184 printf("\n");
1185 }
1186#endif
1187
1188 }
1189 break;
1190 case ZipEntry::kCompressDeflated: {
1191 if (!ZipUtils::inflateToBuffer(mZipFp, buf, unlen, clen)) {
1192 goto bail;
1193 }
1194 }
1195 break;
1196 default:
1197 goto bail;
1198 }
1199 return buf;
1200
1201bail:
1202 free(buf);
1203 return NULL;
1204}
1205
1206
1207/*
1208 * ===========================================================================
1209 * ZipFile::EndOfCentralDir
1210 * ===========================================================================
1211 */
1212
1213/*
1214 * Read the end-of-central-dir fields.
1215 *
1216 * "buf" should be positioned at the EOCD signature, and should contain
1217 * the entire EOCD area including the comment.
1218 */
1219status_t ZipFile::EndOfCentralDir::readBuf(const unsigned char* buf, int len)
1220{
1221 /* don't allow re-use */
1222 assert(mComment == NULL);
1223
1224 if (len < kEOCDLen) {
1225 /* looks like ZIP file got truncated */
1226 LOGD(" Zip EOCD: expected >= %d bytes, found %d\n",
1227 kEOCDLen, len);
1228 return INVALID_OPERATION;
1229 }
1230
1231 /* this should probably be an assert() */
1232 if (ZipEntry::getLongLE(&buf[0x00]) != kSignature)
1233 return UNKNOWN_ERROR;
1234
1235 mDiskNumber = ZipEntry::getShortLE(&buf[0x04]);
1236 mDiskWithCentralDir = ZipEntry::getShortLE(&buf[0x06]);
1237 mNumEntries = ZipEntry::getShortLE(&buf[0x08]);
1238 mTotalNumEntries = ZipEntry::getShortLE(&buf[0x0a]);
1239 mCentralDirSize = ZipEntry::getLongLE(&buf[0x0c]);
1240 mCentralDirOffset = ZipEntry::getLongLE(&buf[0x10]);
1241 mCommentLen = ZipEntry::getShortLE(&buf[0x14]);
1242
1243 // TODO: validate mCentralDirOffset
1244
1245 if (mCommentLen > 0) {
1246 if (kEOCDLen + mCommentLen > len) {
1247 LOGD("EOCD(%d) + comment(%d) exceeds len (%d)\n",
1248 kEOCDLen, mCommentLen, len);
1249 return UNKNOWN_ERROR;
1250 }
1251 mComment = new unsigned char[mCommentLen];
1252 memcpy(mComment, buf + kEOCDLen, mCommentLen);
1253 }
1254
1255 return NO_ERROR;
1256}
1257
1258/*
1259 * Write an end-of-central-directory section.
1260 */
1261status_t ZipFile::EndOfCentralDir::write(FILE* fp)
1262{
1263 unsigned char buf[kEOCDLen];
1264
1265 ZipEntry::putLongLE(&buf[0x00], kSignature);
1266 ZipEntry::putShortLE(&buf[0x04], mDiskNumber);
1267 ZipEntry::putShortLE(&buf[0x06], mDiskWithCentralDir);
1268 ZipEntry::putShortLE(&buf[0x08], mNumEntries);
1269 ZipEntry::putShortLE(&buf[0x0a], mTotalNumEntries);
1270 ZipEntry::putLongLE(&buf[0x0c], mCentralDirSize);
1271 ZipEntry::putLongLE(&buf[0x10], mCentralDirOffset);
1272 ZipEntry::putShortLE(&buf[0x14], mCommentLen);
1273
1274 if (fwrite(buf, 1, kEOCDLen, fp) != kEOCDLen)
1275 return UNKNOWN_ERROR;
1276 if (mCommentLen > 0) {
1277 assert(mComment != NULL);
1278 if (fwrite(mComment, mCommentLen, 1, fp) != mCommentLen)
1279 return UNKNOWN_ERROR;
1280 }
1281
1282 return NO_ERROR;
1283}
1284
1285/*
1286 * Dump the contents of an EndOfCentralDir object.
1287 */
1288void ZipFile::EndOfCentralDir::dump(void) const
1289{
1290 LOGD(" EndOfCentralDir contents:\n");
1291 LOGD(" diskNum=%u diskWCD=%u numEnt=%u totalNumEnt=%u\n",
1292 mDiskNumber, mDiskWithCentralDir, mNumEntries, mTotalNumEntries);
1293 LOGD(" centDirSize=%lu centDirOff=%lu commentLen=%u\n",
1294 mCentralDirSize, mCentralDirOffset, mCommentLen);
1295}
1296