1/*
2 * Copyright (C) 2007 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// Read-only access to Zip archives, with minimal heap allocation.
19//
20#define LOG_TAG "zipro"
21//#define LOG_NDEBUG 0
22#include <utils/Log.h>
23#include <utils/Compat.h>
24#include <utils/ZipFileRO.h>
25#include <utils/misc.h>
26#include <utils/threads.h>
27
28#include <zlib.h>
29
30#include <string.h>
31#include <fcntl.h>
32#include <errno.h>
33#include <assert.h>
34#include <unistd.h>
35
36/*
37 * We must open binary files using open(path, ... | O_BINARY) under Windows.
38 * Otherwise strange read errors will happen.
39 */
40#ifndef O_BINARY
41#  define O_BINARY  0
42#endif
43
44using namespace android;
45
46/*
47 * Zip file constants.
48 */
49#define kEOCDSignature      0x06054b50
50#define kEOCDLen            22
51#define kEOCDNumEntries     8               // offset to #of entries in file
52#define kEOCDSize           12              // size of the central directory
53#define kEOCDFileOffset     16              // offset to central directory
54
55#define kMaxCommentLen      65535           // longest possible in ushort
56#define kMaxEOCDSearch      (kMaxCommentLen + kEOCDLen)
57
58#define kLFHSignature       0x04034b50
59#define kLFHLen             30              // excluding variable-len fields
60#define kLFHNameLen         26              // offset to filename length
61#define kLFHExtraLen        28              // offset to extra length
62
63#define kCDESignature       0x02014b50
64#define kCDELen             46              // excluding variable-len fields
65#define kCDEMethod          10              // offset to compression method
66#define kCDEModWhen         12              // offset to modification timestamp
67#define kCDECRC             16              // offset to entry CRC
68#define kCDECompLen         20              // offset to compressed length
69#define kCDEUncompLen       24              // offset to uncompressed length
70#define kCDENameLen         28              // offset to filename length
71#define kCDEExtraLen        30              // offset to extra length
72#define kCDECommentLen      32              // offset to comment length
73#define kCDELocalOffset     42              // offset to local hdr
74
75/*
76 * The values we return for ZipEntryRO use 0 as an invalid value, so we
77 * want to adjust the hash table index by a fixed amount.  Using a large
78 * value helps insure that people don't mix & match arguments, e.g. to
79 * findEntryByIndex().
80 */
81#define kZipEntryAdj        10000
82
83ZipFileRO::~ZipFileRO() {
84    free(mHashTable);
85    if (mDirectoryMap)
86        mDirectoryMap->release();
87    if (mFd >= 0)
88        TEMP_FAILURE_RETRY(close(mFd));
89    if (mFileName)
90        free(mFileName);
91}
92
93/*
94 * Convert a ZipEntryRO to a hash table index, verifying that it's in a
95 * valid range.
96 */
97int ZipFileRO::entryToIndex(const ZipEntryRO entry) const
98{
99    long ent = ((intptr_t) entry) - kZipEntryAdj;
100    if (ent < 0 || ent >= mHashTableSize || mHashTable[ent].name == NULL) {
101        ALOGW("Invalid ZipEntryRO %p (%ld)\n", entry, ent);
102        return -1;
103    }
104    return ent;
105}
106
107
108/*
109 * Open the specified file read-only.  We memory-map the entire thing and
110 * close the file before returning.
111 */
112status_t ZipFileRO::open(const char* zipFileName)
113{
114    int fd = -1;
115
116    assert(mDirectoryMap == NULL);
117
118    /*
119     * Open and map the specified file.
120     */
121    fd = TEMP_FAILURE_RETRY(::open(zipFileName, O_RDONLY | O_BINARY));
122    if (fd < 0) {
123        ALOGW("Unable to open zip '%s': %s\n", zipFileName, strerror(errno));
124        return NAME_NOT_FOUND;
125    }
126
127    mFileLength = lseek64(fd, 0, SEEK_END);
128    if (mFileLength < kEOCDLen) {
129        TEMP_FAILURE_RETRY(close(fd));
130        return UNKNOWN_ERROR;
131    }
132
133    if (mFileName != NULL) {
134        free(mFileName);
135    }
136    mFileName = strdup(zipFileName);
137
138    mFd = fd;
139
140    /*
141     * Find the Central Directory and store its size and number of entries.
142     */
143    if (!mapCentralDirectory()) {
144        goto bail;
145    }
146
147    /*
148     * Verify Central Directory and create data structures for fast access.
149     */
150    if (!parseZipArchive()) {
151        goto bail;
152    }
153
154    return OK;
155
156bail:
157    free(mFileName);
158    mFileName = NULL;
159    TEMP_FAILURE_RETRY(close(fd));
160    return UNKNOWN_ERROR;
161}
162
163/*
164 * Parse the Zip archive, verifying its contents and initializing internal
165 * data structures.
166 */
167bool ZipFileRO::mapCentralDirectory(void)
168{
169    ssize_t readAmount = kMaxEOCDSearch;
170    if (readAmount > (ssize_t) mFileLength)
171        readAmount = mFileLength;
172
173    unsigned char* scanBuf = (unsigned char*) malloc(readAmount);
174    if (scanBuf == NULL) {
175        ALOGW("couldn't allocate scanBuf: %s", strerror(errno));
176        free(scanBuf);
177        return false;
178    }
179
180    /*
181     * Make sure this is a Zip archive.
182     */
183    if (lseek64(mFd, 0, SEEK_SET) != 0) {
184        ALOGW("seek to start failed: %s", strerror(errno));
185        free(scanBuf);
186        return false;
187    }
188
189    ssize_t actual = TEMP_FAILURE_RETRY(read(mFd, scanBuf, sizeof(int32_t)));
190    if (actual != (ssize_t) sizeof(int32_t)) {
191        ALOGI("couldn't read first signature from zip archive: %s", strerror(errno));
192        free(scanBuf);
193        return false;
194    }
195
196    {
197        unsigned int header = get4LE(scanBuf);
198        if (header == kEOCDSignature) {
199            ALOGI("Found Zip archive, but it looks empty\n");
200            free(scanBuf);
201            return false;
202        } else if (header != kLFHSignature) {
203            ALOGV("Not a Zip archive (found 0x%08x)\n", header);
204            free(scanBuf);
205            return false;
206        }
207    }
208
209    /*
210     * Perform the traditional EOCD snipe hunt.
211     *
212     * We're searching for the End of Central Directory magic number,
213     * which appears at the start of the EOCD block.  It's followed by
214     * 18 bytes of EOCD stuff and up to 64KB of archive comment.  We
215     * need to read the last part of the file into a buffer, dig through
216     * it to find the magic number, parse some values out, and use those
217     * to determine the extent of the CD.
218     *
219     * We start by pulling in the last part of the file.
220     */
221    off64_t searchStart = mFileLength - readAmount;
222
223    if (lseek64(mFd, searchStart, SEEK_SET) != searchStart) {
224        ALOGW("seek %ld failed: %s\n",  (long) searchStart, strerror(errno));
225        free(scanBuf);
226        return false;
227    }
228    actual = TEMP_FAILURE_RETRY(read(mFd, scanBuf, readAmount));
229    if (actual != (ssize_t) readAmount) {
230        ALOGW("Zip: read " ZD ", expected " ZD ". Failed: %s\n",
231            (ZD_TYPE) actual, (ZD_TYPE) readAmount, strerror(errno));
232        free(scanBuf);
233        return false;
234    }
235
236    /*
237     * Scan backward for the EOCD magic.  In an archive without a trailing
238     * comment, we'll find it on the first try.  (We may want to consider
239     * doing an initial minimal read; if we don't find it, retry with a
240     * second read as above.)
241     */
242    int i;
243    for (i = readAmount - kEOCDLen; i >= 0; i--) {
244        if (scanBuf[i] == 0x50 && get4LE(&scanBuf[i]) == kEOCDSignature) {
245            ALOGV("+++ Found EOCD at buf+%d\n", i);
246            break;
247        }
248    }
249    if (i < 0) {
250        ALOGD("Zip: EOCD not found, %s is not zip\n", mFileName);
251        free(scanBuf);
252        return false;
253    }
254
255    off64_t eocdOffset = searchStart + i;
256    const unsigned char* eocdPtr = scanBuf + i;
257
258    assert(eocdOffset < mFileLength);
259
260    /*
261     * Grab the CD offset and size, and the number of entries in the
262     * archive. After that, we can release our EOCD hunt buffer.
263     */
264    unsigned int numEntries = get2LE(eocdPtr + kEOCDNumEntries);
265    unsigned int dirSize = get4LE(eocdPtr + kEOCDSize);
266    unsigned int dirOffset = get4LE(eocdPtr + kEOCDFileOffset);
267    free(scanBuf);
268
269    // Verify that they look reasonable.
270    if ((long long) dirOffset + (long long) dirSize > (long long) eocdOffset) {
271        ALOGW("bad offsets (dir %ld, size %u, eocd %ld)\n",
272            (long) dirOffset, dirSize, (long) eocdOffset);
273        return false;
274    }
275    if (numEntries == 0) {
276        ALOGW("empty archive?\n");
277        return false;
278    }
279
280    ALOGV("+++ numEntries=%d dirSize=%d dirOffset=%d\n",
281        numEntries, dirSize, dirOffset);
282
283    mDirectoryMap = new FileMap();
284    if (mDirectoryMap == NULL) {
285        ALOGW("Unable to create directory map: %s", strerror(errno));
286        return false;
287    }
288
289    if (!mDirectoryMap->create(mFileName, mFd, dirOffset, dirSize, true)) {
290        ALOGW("Unable to map '%s' (" ZD " to " ZD "): %s\n", mFileName,
291                (ZD_TYPE) dirOffset, (ZD_TYPE) (dirOffset + dirSize), strerror(errno));
292        return false;
293    }
294
295    mNumEntries = numEntries;
296    mDirectoryOffset = dirOffset;
297
298    return true;
299}
300
301
302/*
303 * Round up to the next highest power of 2.
304 *
305 * Found on http://graphics.stanford.edu/~seander/bithacks.html.
306 */
307static unsigned int roundUpPower2(unsigned int val)
308{
309    val--;
310    val |= val >> 1;
311    val |= val >> 2;
312    val |= val >> 4;
313    val |= val >> 8;
314    val |= val >> 16;
315    val++;
316
317    return val;
318}
319
320bool ZipFileRO::parseZipArchive(void)
321{
322    bool result = false;
323    const unsigned char* cdPtr = (const unsigned char*) mDirectoryMap->getDataPtr();
324    size_t cdLength = mDirectoryMap->getDataLength();
325    int numEntries = mNumEntries;
326
327    /*
328     * Create hash table.  We have a minimum 75% load factor, possibly as
329     * low as 50% after we round off to a power of 2.
330     */
331    mHashTableSize = roundUpPower2(1 + (numEntries * 4) / 3);
332    mHashTable = (HashEntry*) calloc(mHashTableSize, sizeof(HashEntry));
333
334    /*
335     * Walk through the central directory, adding entries to the hash
336     * table.
337     */
338    const unsigned char* ptr = cdPtr;
339    for (int i = 0; i < numEntries; i++) {
340        if (get4LE(ptr) != kCDESignature) {
341            ALOGW("Missed a central dir sig (at %d)\n", i);
342            goto bail;
343        }
344        if (ptr + kCDELen > cdPtr + cdLength) {
345            ALOGW("Ran off the end (at %d)\n", i);
346            goto bail;
347        }
348
349        long localHdrOffset = (long) get4LE(ptr + kCDELocalOffset);
350        if (localHdrOffset >= mDirectoryOffset) {
351            ALOGW("bad LFH offset %ld at entry %d\n", localHdrOffset, i);
352            goto bail;
353        }
354
355        unsigned int fileNameLen, extraLen, commentLen, hash;
356
357        fileNameLen = get2LE(ptr + kCDENameLen);
358        extraLen = get2LE(ptr + kCDEExtraLen);
359        commentLen = get2LE(ptr + kCDECommentLen);
360
361        /* add the CDE filename to the hash table */
362        hash = computeHash((const char*)ptr + kCDELen, fileNameLen);
363        addToHash((const char*)ptr + kCDELen, fileNameLen, hash);
364
365        ptr += kCDELen + fileNameLen + extraLen + commentLen;
366        if ((size_t)(ptr - cdPtr) > cdLength) {
367            ALOGW("bad CD advance (%d vs " ZD ") at entry %d\n",
368                (int) (ptr - cdPtr), (ZD_TYPE) cdLength, i);
369            goto bail;
370        }
371    }
372    ALOGV("+++ zip good scan %d entries\n", numEntries);
373    result = true;
374
375bail:
376    return result;
377}
378
379/*
380 * Simple string hash function for non-null-terminated strings.
381 */
382/*static*/ unsigned int ZipFileRO::computeHash(const char* str, int len)
383{
384    unsigned int hash = 0;
385
386    while (len--)
387        hash = hash * 31 + *str++;
388
389    return hash;
390}
391
392/*
393 * Add a new entry to the hash table.
394 */
395void ZipFileRO::addToHash(const char* str, int strLen, unsigned int hash)
396{
397    int ent = hash & (mHashTableSize-1);
398
399    /*
400     * We over-allocate the table, so we're guaranteed to find an empty slot.
401     */
402    while (mHashTable[ent].name != NULL)
403        ent = (ent + 1) & (mHashTableSize-1);
404
405    mHashTable[ent].name = str;
406    mHashTable[ent].nameLen = strLen;
407}
408
409/*
410 * Find a matching entry.
411 *
412 * Returns NULL if not found.
413 */
414ZipEntryRO ZipFileRO::findEntryByName(const char* fileName) const
415{
416    /*
417     * If the ZipFileRO instance is not initialized, the entry number will
418     * end up being garbage since mHashTableSize is -1.
419     */
420    if (mHashTableSize <= 0) {
421        return NULL;
422    }
423
424    int nameLen = strlen(fileName);
425    unsigned int hash = computeHash(fileName, nameLen);
426    int ent = hash & (mHashTableSize-1);
427
428    while (mHashTable[ent].name != NULL) {
429        if (mHashTable[ent].nameLen == nameLen &&
430            memcmp(mHashTable[ent].name, fileName, nameLen) == 0)
431        {
432            /* match */
433            return (ZipEntryRO)(long)(ent + kZipEntryAdj);
434        }
435
436        ent = (ent + 1) & (mHashTableSize-1);
437    }
438
439    return NULL;
440}
441
442/*
443 * Find the Nth entry.
444 *
445 * This currently involves walking through the sparse hash table, counting
446 * non-empty entries.  If we need to speed this up we can either allocate
447 * a parallel lookup table or (perhaps better) provide an iterator interface.
448 */
449ZipEntryRO ZipFileRO::findEntryByIndex(int idx) const
450{
451    if (idx < 0 || idx >= mNumEntries) {
452        ALOGW("Invalid index %d\n", idx);
453        return NULL;
454    }
455
456    for (int ent = 0; ent < mHashTableSize; ent++) {
457        if (mHashTable[ent].name != NULL) {
458            if (idx-- == 0)
459                return (ZipEntryRO) (intptr_t)(ent + kZipEntryAdj);
460        }
461    }
462
463    return NULL;
464}
465
466/*
467 * Get the useful fields from the zip entry.
468 *
469 * Returns "false" if the offsets to the fields or the contents of the fields
470 * appear to be bogus.
471 */
472bool ZipFileRO::getEntryInfo(ZipEntryRO entry, int* pMethod, size_t* pUncompLen,
473    size_t* pCompLen, off64_t* pOffset, long* pModWhen, long* pCrc32) const
474{
475    bool ret = false;
476
477    const int ent = entryToIndex(entry);
478    if (ent < 0)
479        return false;
480
481    HashEntry hashEntry = mHashTable[ent];
482
483    /*
484     * Recover the start of the central directory entry from the filename
485     * pointer.  The filename is the first entry past the fixed-size data,
486     * so we can just subtract back from that.
487     */
488    const unsigned char* ptr = (const unsigned char*) hashEntry.name;
489    off64_t cdOffset = mDirectoryOffset;
490
491    ptr -= kCDELen;
492
493    int method = get2LE(ptr + kCDEMethod);
494    if (pMethod != NULL)
495        *pMethod = method;
496
497    if (pModWhen != NULL)
498        *pModWhen = get4LE(ptr + kCDEModWhen);
499    if (pCrc32 != NULL)
500        *pCrc32 = get4LE(ptr + kCDECRC);
501
502    size_t compLen = get4LE(ptr + kCDECompLen);
503    if (pCompLen != NULL)
504        *pCompLen = compLen;
505    size_t uncompLen = get4LE(ptr + kCDEUncompLen);
506    if (pUncompLen != NULL)
507        *pUncompLen = uncompLen;
508
509    /*
510     * If requested, determine the offset of the start of the data.  All we
511     * have is the offset to the Local File Header, which is variable size,
512     * so we have to read the contents of the struct to figure out where
513     * the actual data starts.
514     *
515     * We also need to make sure that the lengths are not so large that
516     * somebody trying to map the compressed or uncompressed data runs
517     * off the end of the mapped region.
518     *
519     * Note we don't verify compLen/uncompLen if they don't request the
520     * dataOffset, because dataOffset is expensive to determine.  However,
521     * if they don't have the file offset, they're not likely to be doing
522     * anything with the contents.
523     */
524    if (pOffset != NULL) {
525        long localHdrOffset = get4LE(ptr + kCDELocalOffset);
526        if (localHdrOffset + kLFHLen >= cdOffset) {
527            ALOGE("ERROR: bad local hdr offset in zip\n");
528            return false;
529        }
530
531        unsigned char lfhBuf[kLFHLen];
532
533#ifdef HAVE_PREAD
534        /*
535         * This file descriptor might be from zygote's preloaded assets,
536         * so we need to do an pread64() instead of a lseek64() + read() to
537         * guarantee atomicity across the processes with the shared file
538         * descriptors.
539         */
540        ssize_t actual =
541                TEMP_FAILURE_RETRY(pread64(mFd, lfhBuf, sizeof(lfhBuf), localHdrOffset));
542
543        if (actual != sizeof(lfhBuf)) {
544            ALOGW("failed reading lfh from offset %ld\n", localHdrOffset);
545            return false;
546        }
547
548        if (get4LE(lfhBuf) != kLFHSignature) {
549            ALOGW("didn't find signature at start of lfh; wanted: offset=%ld data=0x%08x; "
550                    "got: data=0x%08lx\n",
551                    localHdrOffset, kLFHSignature, get4LE(lfhBuf));
552            return false;
553        }
554#else /* HAVE_PREAD */
555        /*
556         * For hosts don't have pread64() we cannot guarantee atomic reads from
557         * an offset in a file. Android should never run on those platforms.
558         * File descriptors inherited from a fork() share file offsets and
559         * there would be nothing to protect from two different processes
560         * calling lseek64() concurrently.
561         */
562
563        {
564            AutoMutex _l(mFdLock);
565
566            if (lseek64(mFd, localHdrOffset, SEEK_SET) != localHdrOffset) {
567                ALOGW("failed seeking to lfh at offset %ld\n", localHdrOffset);
568                return false;
569            }
570
571            ssize_t actual =
572                    TEMP_FAILURE_RETRY(read(mFd, lfhBuf, sizeof(lfhBuf)));
573            if (actual != sizeof(lfhBuf)) {
574                ALOGW("failed reading lfh from offset %ld\n", localHdrOffset);
575                return false;
576            }
577
578            if (get4LE(lfhBuf) != kLFHSignature) {
579                off64_t actualOffset = lseek64(mFd, 0, SEEK_CUR);
580                ALOGW("didn't find signature at start of lfh; wanted: offset=%ld data=0x%08x; "
581                        "got: offset=" ZD " data=0x%08lx\n",
582                        localHdrOffset, kLFHSignature, (ZD_TYPE) actualOffset, get4LE(lfhBuf));
583                return false;
584            }
585        }
586#endif /* HAVE_PREAD */
587
588        off64_t dataOffset = localHdrOffset + kLFHLen
589            + get2LE(lfhBuf + kLFHNameLen) + get2LE(lfhBuf + kLFHExtraLen);
590        if (dataOffset >= cdOffset) {
591            ALOGW("bad data offset %ld in zip\n", (long) dataOffset);
592            return false;
593        }
594
595        /* check lengths */
596        if ((off64_t)(dataOffset + compLen) > cdOffset) {
597            ALOGW("bad compressed length in zip (%ld + " ZD " > %ld)\n",
598                (long) dataOffset, (ZD_TYPE) compLen, (long) cdOffset);
599            return false;
600        }
601
602        if (method == kCompressStored &&
603            (off64_t)(dataOffset + uncompLen) > cdOffset)
604        {
605            ALOGE("ERROR: bad uncompressed length in zip (%ld + " ZD " > %ld)\n",
606                (long) dataOffset, (ZD_TYPE) uncompLen, (long) cdOffset);
607            return false;
608        }
609
610        *pOffset = dataOffset;
611    }
612
613    return true;
614}
615
616/*
617 * Copy the entry's filename to the buffer.
618 */
619int ZipFileRO::getEntryFileName(ZipEntryRO entry, char* buffer, int bufLen)
620    const
621{
622    int ent = entryToIndex(entry);
623    if (ent < 0)
624        return -1;
625
626    int nameLen = mHashTable[ent].nameLen;
627    if (bufLen < nameLen+1)
628        return nameLen+1;
629
630    memcpy(buffer, mHashTable[ent].name, nameLen);
631    buffer[nameLen] = '\0';
632    return 0;
633}
634
635/*
636 * Create a new FileMap object that spans the data in "entry".
637 */
638FileMap* ZipFileRO::createEntryFileMap(ZipEntryRO entry) const
639{
640    /*
641     * TODO: the efficient way to do this is to modify FileMap to allow
642     * sub-regions of a file to be mapped.  A reference-counting scheme
643     * can manage the base memory mapping.  For now, we just create a brand
644     * new mapping off of the Zip archive file descriptor.
645     */
646
647    FileMap* newMap;
648    size_t compLen;
649    off64_t offset;
650
651    if (!getEntryInfo(entry, NULL, NULL, &compLen, &offset, NULL, NULL))
652        return NULL;
653
654    newMap = new FileMap();
655    if (!newMap->create(mFileName, mFd, offset, compLen, true)) {
656        newMap->release();
657        return NULL;
658    }
659
660    return newMap;
661}
662
663/*
664 * Uncompress an entry, in its entirety, into the provided output buffer.
665 *
666 * This doesn't verify the data's CRC, which might be useful for
667 * uncompressed data.  The caller should be able to manage it.
668 */
669bool ZipFileRO::uncompressEntry(ZipEntryRO entry, void* buffer) const
670{
671    const size_t kSequentialMin = 32768;
672    bool result = false;
673    int ent = entryToIndex(entry);
674    if (ent < 0)
675        return -1;
676
677    int method;
678    size_t uncompLen, compLen;
679    off64_t offset;
680    const unsigned char* ptr;
681
682    getEntryInfo(entry, &method, &uncompLen, &compLen, &offset, NULL, NULL);
683
684    FileMap* file = createEntryFileMap(entry);
685    if (file == NULL) {
686        goto bail;
687    }
688
689    ptr = (const unsigned char*) file->getDataPtr();
690
691    /*
692     * Experiment with madvise hint.  When we want to uncompress a file,
693     * we pull some stuff out of the central dir entry and then hit a
694     * bunch of compressed or uncompressed data sequentially.  The CDE
695     * visit will cause a limited amount of read-ahead because it's at
696     * the end of the file.  We could end up doing lots of extra disk
697     * access if the file we're prying open is small.  Bottom line is we
698     * probably don't want to turn MADV_SEQUENTIAL on and leave it on.
699     *
700     * So, if the compressed size of the file is above a certain minimum
701     * size, temporarily boost the read-ahead in the hope that the extra
702     * pair of system calls are negated by a reduction in page faults.
703     */
704    if (compLen > kSequentialMin)
705        file->advise(FileMap::SEQUENTIAL);
706
707    if (method == kCompressStored) {
708        memcpy(buffer, ptr, uncompLen);
709    } else {
710        if (!inflateBuffer(buffer, ptr, uncompLen, compLen))
711            goto unmap;
712    }
713
714    if (compLen > kSequentialMin)
715        file->advise(FileMap::NORMAL);
716
717    result = true;
718
719unmap:
720    file->release();
721bail:
722    return result;
723}
724
725/*
726 * Uncompress an entry, in its entirety, to an open file descriptor.
727 *
728 * This doesn't verify the data's CRC, but probably should.
729 */
730bool ZipFileRO::uncompressEntry(ZipEntryRO entry, int fd) const
731{
732    bool result = false;
733    int ent = entryToIndex(entry);
734    if (ent < 0)
735        return -1;
736
737    int method;
738    size_t uncompLen, compLen;
739    off64_t offset;
740    const unsigned char* ptr;
741
742    getEntryInfo(entry, &method, &uncompLen, &compLen, &offset, NULL, NULL);
743
744    FileMap* file = createEntryFileMap(entry);
745    if (file == NULL) {
746        goto bail;
747    }
748
749    ptr = (const unsigned char*) file->getDataPtr();
750
751    if (method == kCompressStored) {
752        ssize_t actual = TEMP_FAILURE_RETRY(write(fd, ptr, uncompLen));
753        if (actual < 0) {
754            ALOGE("Write failed: %s\n", strerror(errno));
755            goto unmap;
756        } else if ((size_t) actual != uncompLen) {
757            ALOGE("Partial write during uncompress (" ZD " of " ZD ")\n",
758                (ZD_TYPE) actual, (ZD_TYPE) uncompLen);
759            goto unmap;
760        } else {
761            ALOGI("+++ successful write\n");
762        }
763    } else {
764        if (!inflateBuffer(fd, ptr, uncompLen, compLen))
765            goto unmap;
766    }
767
768    result = true;
769
770unmap:
771    file->release();
772bail:
773    return result;
774}
775
776/*
777 * Uncompress "deflate" data from one buffer to another.
778 */
779/*static*/ bool ZipFileRO::inflateBuffer(void* outBuf, const void* inBuf,
780    size_t uncompLen, size_t compLen)
781{
782    bool result = false;
783    z_stream zstream;
784    int zerr;
785
786    /*
787     * Initialize the zlib stream struct.
788     */
789    memset(&zstream, 0, sizeof(zstream));
790    zstream.zalloc = Z_NULL;
791    zstream.zfree = Z_NULL;
792    zstream.opaque = Z_NULL;
793    zstream.next_in = (Bytef*)inBuf;
794    zstream.avail_in = compLen;
795    zstream.next_out = (Bytef*) outBuf;
796    zstream.avail_out = uncompLen;
797    zstream.data_type = Z_UNKNOWN;
798
799    /*
800     * Use the undocumented "negative window bits" feature to tell zlib
801     * that there's no zlib header waiting for it.
802     */
803    zerr = inflateInit2(&zstream, -MAX_WBITS);
804    if (zerr != Z_OK) {
805        if (zerr == Z_VERSION_ERROR) {
806            ALOGE("Installed zlib is not compatible with linked version (%s)\n",
807                ZLIB_VERSION);
808        } else {
809            ALOGE("Call to inflateInit2 failed (zerr=%d)\n", zerr);
810        }
811        goto bail;
812    }
813
814    /*
815     * Expand data.
816     */
817    zerr = inflate(&zstream, Z_FINISH);
818    if (zerr != Z_STREAM_END) {
819        ALOGW("Zip inflate failed, zerr=%d (nIn=%p aIn=%u nOut=%p aOut=%u)\n",
820            zerr, zstream.next_in, zstream.avail_in,
821            zstream.next_out, zstream.avail_out);
822        goto z_bail;
823    }
824
825    /* paranoia */
826    if (zstream.total_out != uncompLen) {
827        ALOGW("Size mismatch on inflated file (%ld vs " ZD ")\n",
828            zstream.total_out, (ZD_TYPE) uncompLen);
829        goto z_bail;
830    }
831
832    result = true;
833
834z_bail:
835    inflateEnd(&zstream);        /* free up any allocated structures */
836
837bail:
838    return result;
839}
840
841/*
842 * Uncompress "deflate" data from one buffer to an open file descriptor.
843 */
844/*static*/ bool ZipFileRO::inflateBuffer(int fd, const void* inBuf,
845    size_t uncompLen, size_t compLen)
846{
847    bool result = false;
848    const size_t kWriteBufSize = 32768;
849    unsigned char writeBuf[kWriteBufSize];
850    z_stream zstream;
851    int zerr;
852
853    /*
854     * Initialize the zlib stream struct.
855     */
856    memset(&zstream, 0, sizeof(zstream));
857    zstream.zalloc = Z_NULL;
858    zstream.zfree = Z_NULL;
859    zstream.opaque = Z_NULL;
860    zstream.next_in = (Bytef*)inBuf;
861    zstream.avail_in = compLen;
862    zstream.next_out = (Bytef*) writeBuf;
863    zstream.avail_out = sizeof(writeBuf);
864    zstream.data_type = Z_UNKNOWN;
865
866    /*
867     * Use the undocumented "negative window bits" feature to tell zlib
868     * that there's no zlib header waiting for it.
869     */
870    zerr = inflateInit2(&zstream, -MAX_WBITS);
871    if (zerr != Z_OK) {
872        if (zerr == Z_VERSION_ERROR) {
873            ALOGE("Installed zlib is not compatible with linked version (%s)\n",
874                ZLIB_VERSION);
875        } else {
876            ALOGE("Call to inflateInit2 failed (zerr=%d)\n", zerr);
877        }
878        goto bail;
879    }
880
881    /*
882     * Loop while we have more to do.
883     */
884    do {
885        /*
886         * Expand data.
887         */
888        zerr = inflate(&zstream, Z_NO_FLUSH);
889        if (zerr != Z_OK && zerr != Z_STREAM_END) {
890            ALOGW("zlib inflate: zerr=%d (nIn=%p aIn=%u nOut=%p aOut=%u)\n",
891                zerr, zstream.next_in, zstream.avail_in,
892                zstream.next_out, zstream.avail_out);
893            goto z_bail;
894        }
895
896        /* write when we're full or when we're done */
897        if (zstream.avail_out == 0 ||
898            (zerr == Z_STREAM_END && zstream.avail_out != sizeof(writeBuf)))
899        {
900            long writeSize = zstream.next_out - writeBuf;
901            int cc = TEMP_FAILURE_RETRY(write(fd, writeBuf, writeSize));
902            if (cc < 0) {
903                ALOGW("write failed in inflate: %s", strerror(errno));
904                goto z_bail;
905            } else if (cc != (int) writeSize) {
906                ALOGW("write failed in inflate (%d vs %ld)", cc, writeSize);
907                goto z_bail;
908            }
909
910            zstream.next_out = writeBuf;
911            zstream.avail_out = sizeof(writeBuf);
912        }
913    } while (zerr == Z_OK);
914
915    assert(zerr == Z_STREAM_END);       /* other errors should've been caught */
916
917    /* paranoia */
918    if (zstream.total_out != uncompLen) {
919        ALOGW("Size mismatch on inflated file (%ld vs " ZD ")\n",
920            zstream.total_out, (ZD_TYPE) uncompLen);
921        goto z_bail;
922    }
923
924    result = true;
925
926z_bail:
927    inflateEnd(&zstream);        /* free up any allocated structures */
928
929bail:
930    return result;
931}
932