/src/poco/Foundation/src/InflatingStream.cpp
Line | Count | Source |
1 | | // |
2 | | // InflatingStream.cpp |
3 | | // |
4 | | // Library: Foundation |
5 | | // Package: Streams |
6 | | // Module: ZLibStream |
7 | | // |
8 | | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
9 | | // and Contributors. |
10 | | // |
11 | | // SPDX-License-Identifier: BSL-1.0 |
12 | | // |
13 | | |
14 | | |
15 | | #include "Poco/InflatingStream.h" |
16 | | #include "Poco/Exception.h" |
17 | | #include <cstring> |
18 | | #include <memory> |
19 | | #include <zlib.h> |
20 | | |
21 | | |
22 | | namespace |
23 | | { |
24 | | bool isTrailerError(const z_stream* pZstr) |
25 | | /// Returns true if inflate() failed because the checksum or length in the |
26 | | /// stream trailer did not match, i.e. after the compressed data was decoded. |
27 | | /// zlib reports these two errors only after reading the mismatching field. |
28 | 29.6k | { |
29 | 29.6k | return pZstr->msg && |
30 | 29.6k | (std::strcmp(pZstr->msg, "incorrect data check") == 0 || |
31 | 28.6k | std::strcmp(pZstr->msg, "incorrect length check") == 0); |
32 | 29.6k | } |
33 | | |
34 | | void watchHeader(z_stream* pZstr, gz_header* pHeader) |
35 | | /// Lets zlib record in pHeader->done whether the current stream |
36 | | /// has a gzip header (1) or not (-1). |
37 | 107k | { |
38 | 107k | pHeader->done = -1; |
39 | 107k | inflateGetHeader(pZstr, pHeader); // fails, leaving done at -1, if gzip is not accepted |
40 | 107k | } |
41 | | } |
42 | | |
43 | | |
44 | | namespace Poco { |
45 | | |
46 | | |
47 | | InflatingStreamBuf::InflatingStreamBuf(std::istream& istr, StreamType type): |
48 | 107k | BufferedStreamBuf(STREAM_BUFFER_SIZE, std::ios::in), |
49 | 107k | _pIstr(&istr), |
50 | 107k | _pOstr(nullptr), |
51 | 107k | _pHeader(nullptr), |
52 | 107k | _eof(false), |
53 | 107k | _check(type != STREAM_ZIP), |
54 | 107k | _failed(false) |
55 | 107k | { |
56 | 107k | std::unique_ptr<char[]> buffer(new char[INFLATE_BUFFER_SIZE]); |
57 | 107k | std::unique_ptr<gz_header> pHeader = std::make_unique<gz_header>(gz_header{}); |
58 | | |
59 | 107k | std::unique_ptr<z_stream> pZstr = std::make_unique<z_stream>(z_stream{}); |
60 | 107k | int rc = inflateInit2(pZstr.get(), 15 + (type == STREAM_GZIP ? 16 : 0)); |
61 | 107k | if (rc != Z_OK) |
62 | 0 | { |
63 | 0 | throw IOException(zError(rc)); |
64 | 0 | } |
65 | | |
66 | 107k | _pZstr = pZstr.release(); |
67 | 107k | _pHeader = pHeader.release(); |
68 | 107k | _buffer = buffer.release(); |
69 | 107k | watchHeader(_pZstr, _pHeader); |
70 | 107k | } |
71 | | |
72 | | |
73 | | InflatingStreamBuf::InflatingStreamBuf(std::istream& istr, int windowBits): |
74 | 0 | BufferedStreamBuf(STREAM_BUFFER_SIZE, std::ios::in), |
75 | 0 | _pIstr(&istr), |
76 | 0 | _pOstr(nullptr), |
77 | 0 | _pHeader(nullptr), |
78 | 0 | _eof(false), |
79 | 0 | _check(false), |
80 | 0 | _failed(false) |
81 | 0 | { |
82 | 0 | std::unique_ptr<char[]> buffer(new char[INFLATE_BUFFER_SIZE]); |
83 | 0 | std::unique_ptr<gz_header> pHeader = std::make_unique<gz_header>(gz_header{}); |
84 | |
|
85 | 0 | std::unique_ptr<z_stream> pZstr = std::make_unique<z_stream>(z_stream{}); |
86 | 0 | int rc = inflateInit2(pZstr.get(), windowBits); |
87 | 0 | if (rc != Z_OK) |
88 | 0 | { |
89 | 0 | throw IOException(zError(rc)); |
90 | 0 | } |
91 | | |
92 | 0 | _pZstr = pZstr.release(); |
93 | 0 | _pHeader = pHeader.release(); |
94 | 0 | _buffer = buffer.release(); |
95 | 0 | watchHeader(_pZstr, _pHeader); |
96 | 0 | } |
97 | | |
98 | | |
99 | | InflatingStreamBuf::InflatingStreamBuf(std::ostream& ostr, StreamType type): |
100 | 0 | BufferedStreamBuf(STREAM_BUFFER_SIZE, std::ios::out), |
101 | 0 | _pIstr(nullptr), |
102 | 0 | _pOstr(&ostr), |
103 | 0 | _pHeader(nullptr), |
104 | 0 | _eof(false), |
105 | 0 | _check(type != STREAM_ZIP), |
106 | 0 | _failed(false) |
107 | 0 | { |
108 | 0 | std::unique_ptr<char[]> buffer(new char[INFLATE_BUFFER_SIZE]); |
109 | |
|
110 | 0 | std::unique_ptr<z_stream> pZstr = std::make_unique<z_stream>(z_stream{}); |
111 | 0 | int rc = inflateInit2(pZstr.get(), 15 + (type == STREAM_GZIP ? 16 : 0)); |
112 | 0 | if (rc != Z_OK) |
113 | 0 | { |
114 | 0 | throw IOException(zError(rc)); |
115 | 0 | } |
116 | | |
117 | 0 | _pZstr = pZstr.release(); |
118 | 0 | _buffer = buffer.release(); |
119 | 0 | } |
120 | | |
121 | | |
122 | | InflatingStreamBuf::InflatingStreamBuf(std::ostream& ostr, int windowBits): |
123 | 0 | BufferedStreamBuf(STREAM_BUFFER_SIZE, std::ios::out), |
124 | 0 | _pIstr(nullptr), |
125 | 0 | _pOstr(&ostr), |
126 | 0 | _pHeader(nullptr), |
127 | 0 | _eof(false), |
128 | 0 | _check(false), |
129 | 0 | _failed(false) |
130 | 0 | { |
131 | 0 | std::unique_ptr<char[]> buffer(new char[INFLATE_BUFFER_SIZE]); |
132 | |
|
133 | 0 | std::unique_ptr<z_stream> pZstr = std::make_unique<z_stream>(z_stream{}); |
134 | 0 | int rc = inflateInit2(pZstr.get(), windowBits); |
135 | 0 | if (rc != Z_OK) |
136 | 0 | { |
137 | 0 | throw IOException(zError(rc)); |
138 | 0 | } |
139 | | |
140 | 0 | _pZstr = pZstr.release(); |
141 | 0 | _buffer = buffer.release(); |
142 | 0 | } |
143 | | |
144 | | |
145 | | InflatingStreamBuf::~InflatingStreamBuf() |
146 | 107k | { |
147 | 107k | try |
148 | 107k | { |
149 | 107k | close(); |
150 | 107k | } |
151 | 107k | catch (...) |
152 | 107k | { |
153 | 0 | } |
154 | 107k | delete [] _buffer; |
155 | 107k | inflateEnd(_pZstr); |
156 | 107k | delete _pZstr; |
157 | 107k | delete _pHeader; |
158 | 107k | } |
159 | | |
160 | | |
161 | | int InflatingStreamBuf::close() |
162 | 107k | { |
163 | 107k | sync(); |
164 | 107k | _pIstr = nullptr; |
165 | 107k | _pOstr = nullptr; |
166 | 107k | return 0; |
167 | 107k | } |
168 | | |
169 | | |
170 | | void InflatingStreamBuf::reset() |
171 | 0 | { |
172 | 0 | int rc = inflateReset(_pZstr); |
173 | 0 | if (rc == Z_OK) |
174 | 0 | { |
175 | 0 | _eof = false; |
176 | 0 | _failed = false; |
177 | 0 | if (_pHeader) watchHeader(_pZstr, _pHeader); |
178 | 0 | } |
179 | 0 | else |
180 | 0 | throw IOException(zError(rc)); |
181 | 0 | } |
182 | | |
183 | | |
184 | | std::streamsize InflatingStreamBuf::readFromDevice(char* buffer, std::streamsize length) |
185 | 333k | { |
186 | 333k | if (_eof || !_pIstr) return 0; |
187 | 297k | if (_failed) throw IOException("Cannot read after a decompression error, reset() must be called first"); |
188 | | |
189 | 297k | if (_pZstr->avail_in == 0) |
190 | 118k | { |
191 | 118k | int n = 0; |
192 | 118k | if (_pIstr->good()) |
193 | 107k | { |
194 | 107k | _pIstr->read(_buffer, INFLATE_BUFFER_SIZE); |
195 | 107k | n = static_cast<int>(_pIstr->gcount()); |
196 | 107k | } |
197 | 118k | _pZstr->next_in = (unsigned char*) _buffer; |
198 | 118k | _pZstr->avail_in = n; |
199 | 118k | } |
200 | 297k | _pZstr->next_out = (unsigned char*) buffer; |
201 | 297k | _pZstr->avail_out = static_cast<unsigned>(length); |
202 | 297k | while (true) |
203 | 297k | { |
204 | 297k | int rc = inflate(_pZstr, Z_NO_FLUSH); |
205 | 297k | if (rc == Z_DATA_ERROR && !_check) |
206 | 67.7k | { |
207 | 67.7k | if (_pZstr->avail_in == 0) |
208 | 38.1k | { |
209 | 38.1k | if (_pIstr->good()) |
210 | 1 | rc = Z_OK; |
211 | 38.1k | else |
212 | 38.1k | rc = Z_STREAM_END; |
213 | 38.1k | } |
214 | 67.7k | } |
215 | 297k | if (rc == Z_STREAM_END) |
216 | 38.3k | { |
217 | 38.3k | _eof = true; |
218 | 38.3k | return length - _pZstr->avail_out; |
219 | 38.3k | } |
220 | 258k | if (rc != Z_OK) |
221 | 41.1k | { |
222 | | // Z_BUF_ERROR means that the input ended early, which is not a decompression error. |
223 | 41.1k | if (rc != Z_BUF_ERROR) |
224 | 29.6k | { |
225 | 29.6k | _failed = true; |
226 | 29.6k | if (rc == Z_DATA_ERROR && isTrailerError(_pZstr)) |
227 | 966 | { |
228 | | // The failed stream ends at its trailer, so the buffered input that follows |
229 | | // belongs to the next stream and is kept. After a gzip CRC-32 mismatch, the |
230 | | // 4-byte length field of the trailer has not been read yet; skip it. |
231 | 966 | if (_pHeader && _pHeader->done == 1 && std::strcmp(_pZstr->msg, "incorrect data check") == 0) |
232 | 0 | { |
233 | 0 | unsigned n = _pZstr->avail_in < 4 ? _pZstr->avail_in : 4; |
234 | 0 | _pZstr->next_in += n; |
235 | 0 | _pZstr->avail_in -= n; |
236 | 0 | if (n < 4 && _pIstr->good()) _pIstr->ignore(4 - n); |
237 | 0 | } |
238 | 966 | } |
239 | 28.6k | else |
240 | 28.6k | { |
241 | | // The decoder cannot tell where the failed stream ends. Drop the buffered input, |
242 | | // so that reset() continues from the current position of the underlying stream. |
243 | 28.6k | _pZstr->avail_in = 0; |
244 | 28.6k | } |
245 | 29.6k | } |
246 | 41.1k | throw IOException(zError(rc)); |
247 | 41.1k | } |
248 | 217k | if (_pZstr->avail_out == 0) |
249 | 178k | return length; |
250 | 39.0k | if (_pZstr->avail_in == 0) |
251 | 39.0k | { |
252 | 39.0k | int n = 0; |
253 | 39.0k | if (_pIstr->good()) |
254 | 199 | { |
255 | 199 | _pIstr->read(_buffer, INFLATE_BUFFER_SIZE); |
256 | 199 | n = static_cast<int>(_pIstr->gcount()); |
257 | 199 | } |
258 | 39.0k | if (n > 0) |
259 | 180 | { |
260 | 180 | _pZstr->next_in = (unsigned char*) _buffer; |
261 | 180 | _pZstr->avail_in = n; |
262 | 180 | } |
263 | 38.8k | else return length - _pZstr->avail_out; |
264 | 39.0k | } |
265 | 39.0k | } |
266 | 297k | } |
267 | | |
268 | | |
269 | | std::streamsize InflatingStreamBuf::writeToDevice(const char* buffer, std::streamsize length) |
270 | 0 | { |
271 | 0 | if (length == 0 || !_pOstr) return 0; |
272 | | |
273 | 0 | _pZstr->next_in = (unsigned char*) buffer; |
274 | 0 | _pZstr->avail_in = static_cast<unsigned>(length); |
275 | 0 | _pZstr->next_out = (unsigned char*) _buffer; |
276 | 0 | _pZstr->avail_out = INFLATE_BUFFER_SIZE; |
277 | 0 | while (true) |
278 | 0 | { |
279 | 0 | int rc = inflate(_pZstr, Z_NO_FLUSH); |
280 | 0 | if (rc == Z_STREAM_END) |
281 | 0 | { |
282 | 0 | _pOstr->write(_buffer, INFLATE_BUFFER_SIZE - _pZstr->avail_out); |
283 | 0 | if (!_pOstr->good()) throw IOException("Failed writing inflated data to output stream"); |
284 | 0 | break; |
285 | 0 | } |
286 | 0 | if (rc != Z_OK) throw IOException(zError(rc)); |
287 | 0 | if (_pZstr->avail_out == 0) |
288 | 0 | { |
289 | 0 | _pOstr->write(_buffer, INFLATE_BUFFER_SIZE); |
290 | 0 | if (!_pOstr->good()) throw IOException("Failed writing inflated data to output stream"); |
291 | 0 | _pZstr->next_out = (unsigned char*) _buffer; |
292 | 0 | _pZstr->avail_out = INFLATE_BUFFER_SIZE; |
293 | 0 | } |
294 | 0 | if (_pZstr->avail_in == 0) |
295 | 0 | { |
296 | 0 | _pOstr->write(_buffer, INFLATE_BUFFER_SIZE - _pZstr->avail_out); |
297 | 0 | if (!_pOstr->good()) throw IOException("Failed writing inflated data to output stream"); |
298 | 0 | _pZstr->next_out = (unsigned char*) _buffer; |
299 | 0 | _pZstr->avail_out = INFLATE_BUFFER_SIZE; |
300 | 0 | break; |
301 | 0 | } |
302 | 0 | } |
303 | 0 | return length; |
304 | 0 | } |
305 | | |
306 | | |
307 | | int InflatingStreamBuf::sync() |
308 | 107k | { |
309 | 107k | int n = BufferedStreamBuf::sync(); |
310 | 107k | if (!n && _pOstr) _pOstr->flush(); |
311 | 107k | return n; |
312 | 107k | } |
313 | | |
314 | | |
315 | | InflatingIOS::InflatingIOS(std::ostream& ostr, InflatingStreamBuf::StreamType type): |
316 | 0 | _buf(ostr, type) |
317 | 0 | { |
318 | 0 | poco_ios_init(&_buf); |
319 | 0 | } Unexecuted instantiation: Poco::InflatingIOS::InflatingIOS(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, Poco::InflatingStreamBuf::StreamType) Unexecuted instantiation: Poco::InflatingIOS::InflatingIOS(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, Poco::InflatingStreamBuf::StreamType) |
320 | | |
321 | | |
322 | | InflatingIOS::InflatingIOS(std::ostream& ostr, int windowBits): |
323 | 0 | _buf(ostr, windowBits) |
324 | 0 | { |
325 | 0 | poco_ios_init(&_buf); |
326 | 0 | } Unexecuted instantiation: Poco::InflatingIOS::InflatingIOS(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, int) Unexecuted instantiation: Poco::InflatingIOS::InflatingIOS(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, int) |
327 | | |
328 | | |
329 | | InflatingIOS::InflatingIOS(std::istream& istr, InflatingStreamBuf::StreamType type): |
330 | 107k | _buf(istr, type) |
331 | 107k | { |
332 | 107k | poco_ios_init(&_buf); |
333 | 107k | } Poco::InflatingIOS::InflatingIOS(std::__1::basic_istream<char, std::__1::char_traits<char> >&, Poco::InflatingStreamBuf::StreamType) Line | Count | Source | 330 | 107k | _buf(istr, type) | 331 | 107k | { | 332 | 107k | poco_ios_init(&_buf); | 333 | 107k | } |
Unexecuted instantiation: Poco::InflatingIOS::InflatingIOS(std::__1::basic_istream<char, std::__1::char_traits<char> >&, Poco::InflatingStreamBuf::StreamType) |
334 | | |
335 | | |
336 | | InflatingIOS::InflatingIOS(std::istream& istr, int windowBits): |
337 | 0 | _buf(istr, windowBits) |
338 | 0 | { |
339 | 0 | poco_ios_init(&_buf); |
340 | 0 | } Unexecuted instantiation: Poco::InflatingIOS::InflatingIOS(std::__1::basic_istream<char, std::__1::char_traits<char> >&, int) Unexecuted instantiation: Poco::InflatingIOS::InflatingIOS(std::__1::basic_istream<char, std::__1::char_traits<char> >&, int) |
341 | | |
342 | | |
343 | | InflatingIOS::~InflatingIOS() |
344 | 107k | { |
345 | 107k | } |
346 | | |
347 | | |
348 | | InflatingStreamBuf* InflatingIOS::rdbuf() |
349 | 0 | { |
350 | 0 | return &_buf; |
351 | 0 | } |
352 | | |
353 | | |
354 | | InflatingOutputStream::InflatingOutputStream(std::ostream& ostr, InflatingStreamBuf::StreamType type): |
355 | 0 | std::ostream(&_buf), |
356 | 0 | InflatingIOS(ostr, type) |
357 | 0 | { |
358 | 0 | } Unexecuted instantiation: Poco::InflatingOutputStream::InflatingOutputStream(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, Poco::InflatingStreamBuf::StreamType) Unexecuted instantiation: Poco::InflatingOutputStream::InflatingOutputStream(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, Poco::InflatingStreamBuf::StreamType) |
359 | | |
360 | | |
361 | | InflatingOutputStream::InflatingOutputStream(std::ostream& ostr, int windowBits): |
362 | 0 | std::ostream(&_buf), |
363 | 0 | InflatingIOS(ostr, windowBits) |
364 | 0 | { |
365 | 0 | } Unexecuted instantiation: Poco::InflatingOutputStream::InflatingOutputStream(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, int) Unexecuted instantiation: Poco::InflatingOutputStream::InflatingOutputStream(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, int) |
366 | | |
367 | | |
368 | | InflatingOutputStream::~InflatingOutputStream() |
369 | 0 | { |
370 | 0 | } |
371 | | |
372 | | |
373 | | int InflatingOutputStream::close() |
374 | 0 | { |
375 | 0 | return _buf.close(); |
376 | 0 | } |
377 | | |
378 | | |
379 | | InflatingInputStream::InflatingInputStream(std::istream& istr, InflatingStreamBuf::StreamType type): |
380 | 107k | std::istream(&_buf), |
381 | 107k | InflatingIOS(istr, type) |
382 | 107k | { |
383 | 107k | } Unexecuted instantiation: Poco::InflatingInputStream::InflatingInputStream(std::__1::basic_istream<char, std::__1::char_traits<char> >&, Poco::InflatingStreamBuf::StreamType) Poco::InflatingInputStream::InflatingInputStream(std::__1::basic_istream<char, std::__1::char_traits<char> >&, Poco::InflatingStreamBuf::StreamType) Line | Count | Source | 380 | 107k | std::istream(&_buf), | 381 | 107k | InflatingIOS(istr, type) | 382 | 107k | { | 383 | 107k | } |
|
384 | | |
385 | | |
386 | | InflatingInputStream::InflatingInputStream(std::istream& istr, int windowBits): |
387 | 0 | std::istream(&_buf), |
388 | 0 | InflatingIOS(istr, windowBits) |
389 | 0 | { |
390 | 0 | } Unexecuted instantiation: Poco::InflatingInputStream::InflatingInputStream(std::__1::basic_istream<char, std::__1::char_traits<char> >&, int) Unexecuted instantiation: Poco::InflatingInputStream::InflatingInputStream(std::__1::basic_istream<char, std::__1::char_traits<char> >&, int) |
391 | | |
392 | | |
393 | | InflatingInputStream::~InflatingInputStream() |
394 | 107k | { |
395 | 107k | } |
396 | | |
397 | | |
398 | | void InflatingInputStream::reset() |
399 | 0 | { |
400 | 0 | _buf.reset(); |
401 | 0 | clear(); |
402 | 0 | } |
403 | | |
404 | | |
405 | | } // namespace Poco |