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