xoreos  0.0.5
deflate.cpp
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #include <zlib.h>
26 
27 #include <boost/scope_exit.hpp>
28 
29 #include "src/common/deflate.h"
30 #include "src/common/error.h"
31 #include "src/common/scopedptr.h"
32 #include "src/common/ptrvector.h"
34 
35 namespace Common {
36 
37 static void setZStreamInput(z_stream &strm, size_t size, const byte *data) {
38  /* Set the input data for the zlib data stream.
39  *
40  * This ugly const cast is necessary because the zlib API wants a non-const
41  * next_in pointer by default. Unless we define ZLIB_CONST, but that only
42  * appeared in zlib 1.2.5.3. Not really worth bumping our required zlib
43  * version for, IMHO. */
44 
45  strm.avail_in = size;
46  strm.next_in = const_cast<byte *>(data);
47 }
48 
49 static void initZStream(z_stream &strm, int windowBits, size_t size, const byte *data) {
50  /* Initialize the zlib data stream for decompression with our input data. */
51 
52  strm.zalloc = Z_NULL;
53  strm.zfree = Z_NULL;
54  strm.opaque = Z_NULL;
55 
56  setZStreamInput(strm, size, data);
57 
58  int zResult = inflateInit2(&strm, windowBits);
59  if (zResult != Z_OK)
60  throw Exception("Could not initialize zlib inflate: %s (%d)", zError(zResult), zResult);
61 }
62 
63 byte *decompressDeflate(const byte *data, size_t inputSize,
64  size_t outputSize, int windowBits) {
65 
66  ScopedArray<byte> decompressedData(new byte[outputSize]);
67 
68  z_stream strm;
69  BOOST_SCOPE_EXIT( (&strm) ) {
70  inflateEnd(&strm);
71  } BOOST_SCOPE_EXIT_END
72 
73  initZStream(strm, windowBits, inputSize, data);
74 
75  // Set the output data pointer and size
76  strm.avail_out = outputSize;
77  strm.next_out = decompressedData.get();
78 
79  // Decompress. Z_FINISH, because we want to decompress the whole thing in one go.
80  int zResult = inflate(&strm, Z_FINISH);
81 
82  // Was the end of the input stream correctly reached?
83  if ((zResult != Z_STREAM_END) || (strm.avail_out != 0)) {
84  if (zResult == Z_OK)
85  throw Exception("Failed to inflate: premature end of output buffer");
86 
87  if (strm.avail_out != 0)
88  throw Exception("Failed to inflate: output buffer not completely filled");
89 
90  throw Exception("Failed to inflate: %s (%d)", zError(zResult), zResult);
91  }
92 
93  return decompressedData.release();
94 }
95 
96 byte *decompressDeflateWithoutOutputSize(const byte *data, size_t inputSize, size_t &outputSize,
97  int windowBits, unsigned int frameSize) {
98  z_stream strm;
99  BOOST_SCOPE_EXIT( (&strm) ) {
100  inflateEnd(&strm);
101  } BOOST_SCOPE_EXIT_END
102 
103  initZStream(strm, windowBits, inputSize, data);
104 
106 
107  int zResult = 0;
108  do {
109  buffers.push_back(new byte[frameSize]);
110 
111  // Set the output data pointer and size
112  strm.avail_out = frameSize;
113  strm.next_out = buffers.back();
114 
115  // Decompress. Z_SYNC_FLUSH, because we want to decompress partwise.
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);
119 
120  } while (zResult != Z_STREAM_END);
121 
122  ScopedArray<byte> decompressedData(new byte[strm.total_out]);
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));
125 
126  outputSize = strm.total_out;
127  return decompressedData.release();
128 }
129 
131  size_t outputSize, int windowBits) {
132 
133  ScopedArray<byte> compressedData(new byte[inputSize]);
134  if (input.read(compressedData.get(), inputSize) != inputSize)
135  throw Exception(kReadError);
136 
137  const byte *decompressedData = decompressDeflate(compressedData.get(), inputSize, outputSize, windowBits);
138 
139  return new MemoryReadStream(decompressedData, outputSize, true);
140 }
141 
143  int windowBits, unsigned int frameSize) {
144  ScopedArray<byte> compressedData(new byte[inputSize]);
145  if (input.read(compressedData.get(), inputSize) != inputSize)
146  throw Exception(kReadError);
147 
148  size_t size = 0;
149  byte *decompressedData = decompressDeflateWithoutOutputSize(compressedData.get(), inputSize, size, windowBits, frameSize);
150 
151  return new MemoryReadStream(decompressedData, size, true);
152 }
153 
154 size_t decompressDeflateChunk(SeekableReadStream &input, int windowBits,
155  byte *output, size_t outputSize, unsigned int frameSize) {
156 
157  z_stream strm;
158  BOOST_SCOPE_EXIT( (&strm) ) {
159  inflateEnd(&strm);
160  } BOOST_SCOPE_EXIT_END
161 
162  initZStream(strm, windowBits, 0, 0);
163 
164  strm.avail_out = outputSize;
165  strm.next_out = output;
166 
167  ScopedArray<byte> inputData(new byte[frameSize]);
168 
169  /* As long as the zlib stream has not ended, the chunk end was not reached.
170  * Read a frame from the input buffer and decompress. */
171 
172  int zResult = 0;
173  do {
174  if (strm.avail_in == 0) {
175  const size_t inputSize = MIN<size_t>(input.size() - input.pos(), frameSize);
176  if (inputSize == 0)
177  throw Exception("Failed to inflate: input buffer empty, stream not ended");
178 
179  if (input.read(inputData.get(), inputSize) != inputSize)
180  throw Exception(kReadError);
181 
182  setZStreamInput(strm, inputSize, inputData.get());
183  }
184 
185  // Decompress. Z_SYNC_FLUSH, because we want to decompress partwise.
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);
189 
190  } while (zResult != Z_STREAM_END);
191 
192  /* Since we don't know where the chunk ends beforehand, we probably have
193  * read past the chunk. Now that we know where the zlib stream ends, we
194  * know where the chunk ended, so we can seek back to that place. */
195  input.seek(- static_cast<ptrdiff_t>(strm.avail_in), SeekableReadStream::kOriginCurrent);
196 
197  return strm.total_out;
198 }
199 
200 } // End of namespace Common
Generic interface for a readable data stream.
Definition: readstream.h:64
Definition: 2dafile.h:39
static void setZStreamInput(z_stream &strm, size_t size, const byte *data)
Definition: deflate.cpp:37
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.
Definition: scopedptr.h:103
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&#39;s DEFLATE algorithm.
Definition: deflate.cpp:63
size_t decompressDeflateChunk(SeekableReadStream &input, int windowBits, byte *output, size_t outputSize, unsigned int frameSize)
Decompress (inflate) using zlib&#39;s DEFLATE algorithm, until a stream end marker was reached...
Definition: deflate.cpp:154
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 &#39;stream&#39;, which implements the ReadStream interface for a plain memory block...
Definition: memreadstream.h:66
StackException Exception
Definition: error.h:59
const Exception kReadError("Read error")
Exception when reading from a stream failed.
Definition: error.h:62
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&#39;s DEFLATE algorithm without knowing the output size.
Definition: deflate.cpp:96
Seek from the current position of the stream.
Definition: readstream.h:270
PointerType get() const
Returns the plain pointer value.
Definition: scopedptr.h:96
static void initZStream(z_stream &strm, int windowBits, size_t size, const byte *data)
Definition: deflate.cpp:49
Compress (deflate) and decompress (inflate) using zlib&#39;s DEFLATE algorithm.
Interface for a seekable & readable data stream.
Definition: readstream.h:265
uint8 byte
Definition: types.h:209