1 /* 2 * Copyright (C) 2010 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 #ifndef __LIBS_STREAMINGZIPINFLATER_H 18 #define __LIBS_STREAMINGZIPINFLATER_H 19 20 #include <unistd.h> 21 #include <inttypes.h> 22 23 #include <util/map_ptr.h> 24 #include <zlib.h> 25 26 #include <utils/Compat.h> 27 28 namespace android { 29 30 class StreamingZipInflater { 31 public: 32 static const size_t INPUT_CHUNK_SIZE = 64 * 1024; 33 static const size_t OUTPUT_CHUNK_SIZE = 64 * 1024; 34 35 // Flavor that pages in the compressed data from a fd 36 StreamingZipInflater(int fd, off64_t compDataStart, size_t uncompSize, size_t compSize); 37 38 // Flavor that gets the compressed data from an in-memory buffer 39 StreamingZipInflater(const incfs::IncFsFileMap* dataMap, size_t uncompSize); 40 41 ~StreamingZipInflater(); 42 43 // read 'count' bytes of uncompressed data from the current position. outBuf may 44 // be NULL, in which case the data is consumed and discarded. 45 ssize_t read(void* outBuf, size_t count); 46 47 // seeking backwards requires uncompressing fom the beginning, so is very 48 // expensive. seeking forwards only requires uncompressing from the current 49 // position to the destination. 50 off64_t seekAbsolute(off64_t absoluteInputPosition); 51 52 private: 53 void initInflateState(); 54 int readNextChunk(); 55 56 // where to find the uncompressed data 57 int mFd; 58 off64_t mInFileStart; // where the compressed data lives in the file 59 const incfs::IncFsFileMap* mDataMap; 60 61 z_stream mInflateState; 62 bool mStreamNeedsInit; 63 64 // output invariants for this asset 65 uint8_t* mOutBuf; // output buf for decompressed bytes 66 size_t mOutBufSize; // allocated size of mOutBuf 67 size_t mOutTotalSize; // total uncompressed size of the blob 68 69 // current output state bookkeeping 70 off64_t mOutCurPosition; // current position in total offset 71 size_t mOutLastDecoded; // last decoded byte + 1 in mOutbuf 72 size_t mOutDeliverable; // next undelivered byte of decoded output in mOutBuf 73 74 // input invariants 75 uint8_t* mInBuf; 76 size_t mInBufSize; // allocated size of mInBuf; 77 size_t mInTotalSize; // total size of compressed data for this blob 78 79 // input state bookkeeping 80 size_t mInNextChunkOffset; // offset from start of blob at which the next input chunk lies 81 // the z_stream contains state about input block consumption 82 }; 83 84 } 85 86 #endif 87