27 #include <boost/scope_exit.hpp> 46 strm.next_in =
const_cast<byte *
>(data);
49 static void initZStream(z_stream &strm,
int windowBits,
size_t size,
const byte *data) {
58 int zResult = inflateInit2(&strm, windowBits);
60 throw Exception(
"Could not initialize zlib inflate: %s (%d)", zError(zResult), zResult);
64 size_t outputSize,
int windowBits) {
69 BOOST_SCOPE_EXIT( (&strm) ) {
71 } BOOST_SCOPE_EXIT_END
76 strm.avail_out = outputSize;
77 strm.next_out = decompressedData.
get();
80 int zResult = inflate(&strm, Z_FINISH);
83 if ((zResult != Z_STREAM_END) || (strm.avail_out != 0)) {
85 throw Exception(
"Failed to inflate: premature end of output buffer");
87 if (strm.avail_out != 0)
88 throw Exception(
"Failed to inflate: output buffer not completely filled");
90 throw Exception(
"Failed to inflate: %s (%d)", zError(zResult), zResult);
93 return decompressedData.
release();
97 int windowBits,
unsigned int frameSize) {
99 BOOST_SCOPE_EXIT( (&strm) ) {
101 } BOOST_SCOPE_EXIT_END
109 buffers.push_back(
new byte[frameSize]);
112 strm.avail_out = frameSize;
113 strm.next_out = buffers.back();
116 zResult = inflate(&strm, Z_SYNC_FLUSH);
117 if (zResult != Z_STREAM_END && zResult != Z_OK)
118 throw Exception(
"Failed to inflate: %s (%d)", zError(zResult), zResult);
120 }
while (zResult != Z_STREAM_END);
123 for (
size_t i = 0, size = strm.total_out; i < buffers.size(); ++i, size -= frameSize)
124 std::memcpy(decompressedData.
get() + i * frameSize, buffers[i], MIN<size_t>(size, frameSize));
126 outputSize = strm.total_out;
127 return decompressedData.
release();
131 size_t outputSize,
int windowBits) {
134 if (input.
read(compressedData.
get(), inputSize) != inputSize)
143 int windowBits,
unsigned int frameSize) {
145 if (input.
read(compressedData.
get(), inputSize) != inputSize)
155 byte *output,
size_t outputSize,
unsigned int frameSize) {
158 BOOST_SCOPE_EXIT( (&strm) ) {
160 } BOOST_SCOPE_EXIT_END
164 strm.avail_out = outputSize;
165 strm.next_out = output;
174 if (strm.avail_in == 0) {
175 const size_t inputSize = MIN<size_t>(input.
size() - input.
pos(), frameSize);
177 throw Exception(
"Failed to inflate: input buffer empty, stream not ended");
179 if (input.
read(inputData.
get(), inputSize) != inputSize)
186 zResult = inflate(&strm, Z_SYNC_FLUSH);
187 if (zResult != Z_STREAM_END && zResult != Z_OK)
188 throw Exception(
"Failed to inflate: %s (%d)", zError(zResult), zResult);
190 }
while (zResult != Z_STREAM_END);
197 return strm.total_out;
Generic interface for a readable data stream.
static void setZStreamInput(z_stream &strm, size_t size, const byte *data)
virtual size_t seek(ptrdiff_t offset, Origin whence=kOriginBegin)=0
Sets the stream position indicator for the stream.
PointerType release()
Returns the plain pointer value and releases ScopedPtr.
Implementing the reading stream interfaces for plain memory blocks.
byte * decompressDeflate(const byte *data, size_t inputSize, size_t outputSize, int windowBits)
Decompress (inflate) using zlib's DEFLATE algorithm.
size_t decompressDeflateChunk(SeekableReadStream &input, int windowBits, byte *output, size_t outputSize, unsigned int frameSize)
Decompress (inflate) using zlib's DEFLATE algorithm, until a stream end marker was reached...
A simple scoped smart pointer template.
Basic exceptions to throw.
virtual size_t read(void *dataPtr, size_t dataSize)=0
Read data from the stream.
Simple memory based 'stream', which implements the ReadStream interface for a plain memory block...
const Exception kReadError("Read error")
Exception when reading from a stream failed.
A vector storing pointer to objects, with automatic deletion.
virtual size_t size() const =0
Obtains the total size of the stream, measured in bytes.
virtual size_t pos() const =0
Obtains the current value of the stream position indicator of the stream.
byte * decompressDeflateWithoutOutputSize(const byte *data, size_t inputSize, size_t &outputSize, int windowBits, unsigned int frameSize)
Decompress (inflate) using zlib's DEFLATE algorithm without knowing the output size.
Seek from the current position of the stream.
PointerType get() const
Returns the plain pointer value.
static void initZStream(z_stream &strm, int windowBits, size_t size, const byte *data)
Compress (deflate) and decompress (inflate) using zlib's DEFLATE algorithm.
Interface for a seekable & readable data stream.