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