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