Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games.
2 : *
3 : * Permission is hereby granted, free of charge, to any person obtaining
4 : * a copy of this software and associated documentation files (the
5 : * "Software"), to deal in the Software without restriction, including
6 : * without limitation the rights to use, copy, modify, merge, publish,
7 : * distribute, sublicense, and/or sell copies of the Software, and to
8 : * permit persons to whom the Software is furnished to do so, subject to
9 : * the following conditions:
10 : *
11 : * The above copyright notice and this permission notice shall be included
12 : * in all copies or substantial portions of the Software.
13 : *
14 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 : * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 : * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 : * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 : * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19 : * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20 : * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 : */
22 :
23 : #include "precompiled.h"
24 :
25 : #include "codec_zlib.h"
26 :
27 : #include "lib/alignment.h"
28 : #include "lib/debug.h"
29 : #include "lib/external_libraries/zlib.h"
30 : #include "lib/file/archive/codec.h"
31 : #include "lib/status.h"
32 :
33 : #include <algorithm>
34 : #include <cstring>
35 :
36 0 : class Codec_ZLib : public ICodec
37 : {
38 : public:
39 0 : std::uint32_t UpdateChecksum([[maybe_unused]] std::uint32_t checksum, [[maybe_unused]] const std::uint8_t* in,
40 : [[maybe_unused]] size_t inSize) const
41 : {
42 : #if CODEC_COMPUTE_CHECKSUM
43 0 : return static_cast<std::uint32_t>(crc32(checksum, in, (uInt)inSize));
44 : #else
45 : return 0;
46 : #endif
47 : }
48 :
49 : protected:
50 0 : std::uint32_t InitializeChecksum()
51 : {
52 : #if CODEC_COMPUTE_CHECKSUM
53 0 : return crc32(0, 0, 0);
54 : #else
55 : return 0;
56 : #endif
57 : }
58 : };
59 :
60 :
61 : //-----------------------------------------------------------------------------
62 :
63 : class Codec_ZLibNone : public Codec_ZLib
64 : {
65 : public:
66 0 : Codec_ZLibNone()
67 0 : {
68 0 : Reset();
69 0 : }
70 :
71 0 : virtual ~Codec_ZLibNone()
72 : {
73 0 : }
74 :
75 0 : virtual size_t MaxOutputSize(size_t inSize) const
76 : {
77 0 : return inSize;
78 : }
79 :
80 0 : virtual Status Reset()
81 : {
82 0 : m_checksum = InitializeChecksum();
83 0 : return INFO::OK;
84 : }
85 :
86 0 : virtual Status Process(const std::uint8_t* in, size_t inSize, std::uint8_t* out, size_t outSize,
87 : size_t& inConsumed, size_t& outProduced)
88 : {
89 0 : const size_t transferSize = std::min(inSize, outSize);
90 0 : memcpy(out, in, transferSize);
91 0 : inConsumed = outProduced = transferSize;
92 0 : m_checksum = UpdateChecksum(m_checksum, out, outProduced);
93 0 : return INFO::OK;
94 : }
95 :
96 0 : virtual Status Finish(std::uint32_t& checksum, size_t& outProduced)
97 : {
98 0 : outProduced = 0;
99 0 : checksum = m_checksum;
100 0 : return INFO::OK;
101 : }
102 :
103 : private:
104 : std::uint32_t m_checksum;
105 : };
106 :
107 :
108 : //-----------------------------------------------------------------------------
109 :
110 0 : class CodecZLibStream : public Codec_ZLib
111 : {
112 : protected:
113 0 : CodecZLibStream()
114 0 : {
115 0 : memset(&m_zs, 0, sizeof(m_zs));
116 0 : m_checksum = InitializeChecksum();
117 0 : }
118 :
119 0 : static Status LibError_from_zlib(int zlib_ret)
120 : {
121 0 : switch(zlib_ret)
122 : {
123 : case Z_OK:
124 : return INFO::OK;
125 0 : case Z_STREAM_END:
126 0 : WARN_RETURN(ERR::FAIL);
127 0 : case Z_MEM_ERROR:
128 0 : WARN_RETURN(ERR::NO_MEM);
129 0 : case Z_DATA_ERROR:
130 0 : WARN_RETURN(ERR::CORRUPTED);
131 0 : case Z_STREAM_ERROR:
132 0 : WARN_RETURN(ERR::INVALID_PARAM);
133 0 : default:
134 0 : WARN_RETURN(ERR::FAIL);
135 : }
136 : }
137 :
138 0 : static void WarnIfZLibError(int zlib_ret)
139 : {
140 0 : (void)LibError_from_zlib(zlib_ret);
141 : }
142 :
143 : typedef int ZEXPORT (*ZLibFunc)(z_streamp strm, int flush);
144 :
145 0 : Status CallStreamFunc(ZLibFunc func, int flush, const std::uint8_t* in, const size_t inSize,
146 : std::uint8_t* out, const size_t outSize, size_t& inConsumed, size_t& outProduced)
147 : {
148 0 : m_zs.next_in = (Byte*)in;
149 0 : m_zs.avail_in = (uInt)inSize;
150 0 : m_zs.next_out = (Byte*)out;
151 0 : m_zs.avail_out = (uInt)outSize;
152 :
153 0 : int ret = func(&m_zs, flush);
154 : // sanity check: if ZLib reports end of stream, all input data
155 : // must have been consumed.
156 0 : if(ret == Z_STREAM_END)
157 : {
158 0 : ENSURE(m_zs.avail_in == 0);
159 : ret = Z_OK;
160 : }
161 :
162 0 : ENSURE(inSize >= m_zs.avail_in && outSize >= m_zs.avail_out);
163 0 : inConsumed = inSize - m_zs.avail_in;
164 0 : outProduced = outSize - m_zs.avail_out;
165 :
166 0 : return LibError_from_zlib(ret);
167 : }
168 :
169 : mutable z_stream m_zs;
170 :
171 : // note: z_stream does contain an 'adler' checksum field, but that's
172 : // not updated in streams lacking a gzip header, so we'll have to
173 : // calculate a checksum ourselves.
174 : // adler32 is somewhat weaker than CRC32, but a more important argument
175 : // is that we should use the latter for compatibility with Zip archives.
176 : mutable std::uint32_t m_checksum;
177 : };
178 :
179 :
180 : //-----------------------------------------------------------------------------
181 :
182 : class Compressor_ZLib : public CodecZLibStream
183 : {
184 : public:
185 0 : Compressor_ZLib()
186 0 : {
187 : // note: with Z_BEST_COMPRESSION, 78% percent of
188 : // archive builder CPU time is spent in ZLib, even though
189 : // that is interleaved with IO; everything else is negligible.
190 : // we prefer faster speed at the cost of 1.5% larger archives.
191 0 : const int level = Z_BEST_SPEED;
192 0 : const int windowBits = -MAX_WBITS; // max window size; omit ZLib header
193 0 : const int memLevel = 9; // max speed; total mem ~= 384KiB
194 0 : const int strategy = Z_DEFAULT_STRATEGY; // normal data - not RLE
195 0 : const int ret = deflateInit2(&m_zs, level, Z_DEFLATED, windowBits, memLevel, strategy);
196 0 : ENSURE(ret == Z_OK);
197 0 : }
198 :
199 0 : virtual ~Compressor_ZLib()
200 0 : {
201 0 : const int ret = deflateEnd(&m_zs);
202 0 : WarnIfZLibError(ret);
203 0 : }
204 :
205 0 : virtual size_t MaxOutputSize(size_t inSize) const
206 : {
207 0 : return (size_t)deflateBound(&m_zs, (uLong)inSize);
208 : }
209 :
210 0 : virtual Status Reset()
211 : {
212 0 : m_checksum = InitializeChecksum();
213 0 : const int ret = deflateReset(&m_zs);
214 0 : return LibError_from_zlib(ret);
215 : }
216 :
217 0 : virtual Status Process(const std::uint8_t* in, size_t inSize, std::uint8_t* out, size_t outSize,
218 : size_t& inConsumed, size_t& outProduced)
219 : {
220 0 : m_checksum = UpdateChecksum(m_checksum, in, inSize);
221 0 : return CodecZLibStream::CallStreamFunc(deflate, 0, in, inSize, out, outSize, inConsumed, outProduced);
222 : }
223 :
224 0 : virtual Status Finish(std::uint32_t& checksum, size_t& outProduced)
225 : {
226 0 : const uInt availOut = m_zs.avail_out;
227 :
228 : // notify zlib that no more data is forthcoming and have it flush output.
229 : // our output buffer has enough space due to use of deflateBound;
230 : // therefore, deflate must return Z_STREAM_END.
231 0 : const int ret = deflate(&m_zs, Z_FINISH);
232 0 : ENSURE(ret == Z_STREAM_END);
233 :
234 0 : outProduced = size_t(availOut - m_zs.avail_out);
235 :
236 0 : checksum = m_checksum;
237 0 : return INFO::OK;
238 : }
239 : };
240 :
241 :
242 : //-----------------------------------------------------------------------------
243 :
244 : class Decompressor_ZLib : public CodecZLibStream
245 : {
246 : public:
247 0 : Decompressor_ZLib()
248 0 : {
249 0 : const int windowBits = -MAX_WBITS; // max window size; omit ZLib header
250 0 : const int ret = inflateInit2(&m_zs, windowBits);
251 0 : ENSURE(ret == Z_OK);
252 0 : }
253 :
254 0 : virtual ~Decompressor_ZLib()
255 0 : {
256 0 : const int ret = inflateEnd(&m_zs);
257 0 : WarnIfZLibError(ret);
258 0 : }
259 :
260 0 : virtual size_t MaxOutputSize(size_t inSize) const
261 : {
262 : // relying on an upper bound for the output is a really bad idea for
263 : // large files. archive formats store the uncompressed file sizes,
264 : // so callers should use that when allocating the output buffer.
265 0 : ENSURE(inSize < 1*MiB);
266 :
267 0 : return inSize*1032; // see http://www.zlib.org/zlib_tech.html
268 : }
269 :
270 0 : virtual Status Reset()
271 : {
272 0 : m_checksum = InitializeChecksum();
273 0 : const int ret = inflateReset(&m_zs);
274 0 : return LibError_from_zlib(ret);
275 : }
276 :
277 0 : virtual Status Process(const std::uint8_t* in, size_t inSize, std::uint8_t* out, size_t outSize,
278 : size_t& inConsumed, size_t& outProduced)
279 : {
280 0 : const Status ret = CodecZLibStream::CallStreamFunc(inflate, Z_SYNC_FLUSH, in, inSize, out, outSize, inConsumed, outProduced);
281 0 : m_checksum = UpdateChecksum(m_checksum, out, outProduced);
282 0 : return ret;
283 : }
284 :
285 0 : virtual Status Finish(std::uint32_t& checksum, size_t& outProduced)
286 : {
287 : // no action needed - decompression always flushes immediately.
288 0 : outProduced = 0;
289 :
290 0 : checksum = m_checksum;
291 0 : return INFO::OK;
292 : }
293 : };
294 :
295 :
296 : //-----------------------------------------------------------------------------
297 :
298 0 : PICodec CreateCodec_ZLibNone()
299 : {
300 0 : return PICodec(new Codec_ZLibNone);
301 : }
302 :
303 0 : PICodec CreateCompressor_ZLibDeflate()
304 : {
305 0 : return PICodec(new Compressor_ZLib);
306 : }
307 :
308 0 : PICodec CreateDecompressor_ZLibDeflate()
309 : {
310 0 : return PICodec (new Decompressor_ZLib);
311 : }
|