blob: 472b794b911c2ed755ffdab6f8fbbf2fbaabb201 [file] [log] [blame]
Adam Lesinski16c4d152014-01-24 13:27:13 -08001/*
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>
Ryan Mitchell80094e32020-11-16 23:08:18 +000022
23#include <util/map_ptr.h>
Adam Lesinski16c4d152014-01-24 13:27:13 -080024#include <zlib.h>
25
26#include <utils/Compat.h>
27
28namespace android {
29
30class StreamingZipInflater {
31public:
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
Ryan Mitchell80094e32020-11-16 23:08:18 +000039 StreamingZipInflater(const incfs::IncFsFileMap* dataMap, size_t uncompSize);
Adam Lesinski16c4d152014-01-24 13:27:13 -080040
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
52private:
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
Ryan Mitchell80094e32020-11-16 23:08:18 +000059 const incfs::IncFsFileMap* mDataMap;
Adam Lesinski16c4d152014-01-24 13:27:13 -080060
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