2 * Copyright (C) 2010 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 //#define LOG_NDEBUG 0
18 #define LOG_TAG "szipinf"
19 #include <utils/Log.h>
21 #include <androidfw/StreamingZipInflater.h>
22 #include <utils/FileMap.h>
30 * TEMP_FAILURE_RETRY is defined by some, but not all, versions of
31 * <unistd.h>. (Alas, it is not as standard as we'd hoped!) So, if it's
32 * not already defined, then define it here.
34 #ifndef TEMP_FAILURE_RETRY
35 /* Used to retry syscalls that can return EINTR. */
36 #define TEMP_FAILURE_RETRY(exp) ({ \
40 } while (_rc == -1 && errno == EINTR); \
44 static const bool kIsDebug = false;
46 static inline size_t min_of(size_t a, size_t b) { return (a < b) ? a : b; }
48 using namespace android;
51 * Streaming access to compressed asset data in an open fd
53 StreamingZipInflater::StreamingZipInflater(int fd, off64_t compDataStart,
54 size_t uncompSize, size_t compSize) {
57 mInFileStart = compDataStart;
58 mOutTotalSize = uncompSize;
59 mInTotalSize = compSize;
61 mInBufSize = StreamingZipInflater::INPUT_CHUNK_SIZE;
62 mInBuf = new uint8_t[mInBufSize];
64 mOutBufSize = StreamingZipInflater::OUTPUT_CHUNK_SIZE;
65 mOutBuf = new uint8_t[mOutBufSize];
71 * Streaming access to compressed data held in an mmapped region of memory
73 StreamingZipInflater::StreamingZipInflater(FileMap* dataMap, size_t uncompSize) {
76 mOutTotalSize = uncompSize;
77 mInTotalSize = dataMap->getDataLength();
79 mInBuf = (uint8_t*) dataMap->getDataPtr();
80 mInBufSize = mInTotalSize;
82 mOutBufSize = StreamingZipInflater::OUTPUT_CHUNK_SIZE;
83 mOutBuf = new uint8_t[mOutBufSize];
88 StreamingZipInflater::~StreamingZipInflater() {
89 // tear down the in-flight zip state just in case
90 ::inflateEnd(&mInflateState);
92 if (mDataMap == NULL) {
98 void StreamingZipInflater::initInflateState() {
99 ALOGV("Initializing inflate state");
101 memset(&mInflateState, 0, sizeof(mInflateState));
102 mInflateState.zalloc = Z_NULL;
103 mInflateState.zfree = Z_NULL;
104 mInflateState.opaque = Z_NULL;
105 mInflateState.next_in = (Bytef*)mInBuf;
106 mInflateState.next_out = (Bytef*) mOutBuf;
107 mInflateState.avail_out = mOutBufSize;
108 mInflateState.data_type = Z_UNKNOWN;
110 mOutLastDecoded = mOutDeliverable = mOutCurPosition = 0;
111 mInNextChunkOffset = 0;
112 mStreamNeedsInit = true;
114 if (mDataMap == NULL) {
115 ::lseek(mFd, mInFileStart, SEEK_SET);
116 mInflateState.avail_in = 0; // set when a chunk is read in
118 mInflateState.avail_in = mInBufSize;
125 * 1. If we have undelivered uncompressed data, send it. At this point
126 * either we've satisfied the request, or we've exhausted the available
127 * output data in mOutBuf.
129 * 2. While we haven't sent enough data to satisfy the request:
130 * 0. if the request is for more data than exists, bail.
131 * a. if there is no input data to decode, read some into the input buffer
132 * and readjust the z_stream input pointers
133 * b. point the output to the start of the output buffer and decode what we can
134 * c. deliver whatever output data we can
136 ssize_t StreamingZipInflater::read(void* outBuf, size_t count) {
137 uint8_t* dest = (uint8_t*) outBuf;
138 size_t bytesRead = 0;
139 size_t toRead = min_of(count, size_t(mOutTotalSize - mOutCurPosition));
141 // First, write from whatever we already have decoded and ready to go
142 size_t deliverable = min_of(toRead, mOutLastDecoded - mOutDeliverable);
143 if (deliverable > 0) {
144 if (outBuf != NULL) memcpy(dest, mOutBuf + mOutDeliverable, deliverable);
145 mOutDeliverable += deliverable;
146 mOutCurPosition += deliverable;
148 bytesRead += deliverable;
149 toRead -= deliverable;
152 // need more data? time to decode some.
154 // if we don't have any data to decode, read some in. If we're working
155 // from mmapped data this won't happen, because the clipping to total size
156 // will prevent reading off the end of the mapped input chunk.
157 if ((mInflateState.avail_in == 0) && (mDataMap == NULL)) {
158 int err = readNextChunk();
160 ALOGE("Unable to access asset data: %d", err);
161 if (!mStreamNeedsInit) {
162 ::inflateEnd(&mInflateState);
168 // we know we've drained whatever is in the out buffer now, so just
169 // start from scratch there, reading all the input we have at present.
170 mInflateState.next_out = (Bytef*) mOutBuf;
171 mInflateState.avail_out = mOutBufSize;
174 ALOGV("Inflating to outbuf: avail_in=%u avail_out=%u next_in=%p next_out=%p",
175 mInflateState.avail_in, mInflateState.avail_out,
176 mInflateState.next_in, mInflateState.next_out);
179 if (mStreamNeedsInit) {
180 ALOGV("Initializing zlib to inflate");
181 result = inflateInit2(&mInflateState, -MAX_WBITS);
182 mStreamNeedsInit = false;
184 if (result == Z_OK) result = ::inflate(&mInflateState, Z_SYNC_FLUSH);
186 // Whoops, inflation failed
187 ALOGE("Error inflating asset: %d", result);
188 ::inflateEnd(&mInflateState);
192 if (result == Z_STREAM_END) {
193 // we know we have to have reached the target size here and will
194 // not try to read any further, so just wind things up.
195 ::inflateEnd(&mInflateState);
198 // Note how much data we got, and off we go
200 mOutLastDecoded = mOutBufSize - mInflateState.avail_out;
207 int StreamingZipInflater::readNextChunk() {
208 assert(mDataMap == NULL);
210 if (mInNextChunkOffset < mInTotalSize) {
211 size_t toRead = min_of(mInBufSize, mInTotalSize - mInNextChunkOffset);
213 ssize_t didRead = TEMP_FAILURE_RETRY(::read(mFd, mInBuf, toRead));
215 ALOGV("Reading input chunk, size %08zx didread %08zx", toRead, didRead);
218 ALOGE("Error reading asset data: %s", strerror(errno));
221 mInNextChunkOffset += didRead;
222 mInflateState.next_in = (Bytef*) mInBuf;
223 mInflateState.avail_in = didRead;
230 // seeking backwards requires uncompressing fom the beginning, so is very
231 // expensive. seeking forwards only requires uncompressing from the current
232 // position to the destination.
233 off64_t StreamingZipInflater::seekAbsolute(off64_t absoluteInputPosition) {
234 if (absoluteInputPosition < mOutCurPosition) {
235 // rewind and reprocess the data from the beginning
236 if (!mStreamNeedsInit) {
237 ::inflateEnd(&mInflateState);
240 read(NULL, absoluteInputPosition);
241 } else if (absoluteInputPosition > mOutCurPosition) {
242 read(NULL, absoluteInputPosition - mOutCurPosition);
244 // else if the target position *is* our current position, do nothing
245 return absoluteInputPosition;