4
// $Id: //poco/1.2/Foundation/src/InflatingStream.cpp#1 $
10
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
13
// Permission is hereby granted, free of charge, to any person or organization
14
// obtaining a copy of the software and accompanying documentation covered by
15
// this license (the "Software") to use, reproduce, display, distribute,
16
// execute, and transmit the Software, and to prepare derivative works of the
17
// Software, and to permit third-parties to whom the Software is furnished to
18
// do so, all subject to the following:
20
// The copyright notices in the Software and this entire statement, including
21
// the above license grant, this restriction and the following disclaimer,
22
// must be included in all copies of the Software, in whole or in part, and
23
// all derivative works of the Software, unless such copies or derivative
24
// works are solely in the form of machine-executable object code generated by
25
// a source language processor.
27
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
30
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
31
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
32
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
33
// DEALINGS IN THE SOFTWARE.
37
#include "Poco/InflatingStream.h"
38
#include "Poco/Exception.h"
44
InflatingStreamBuf::InflatingStreamBuf(std::istream& istr, StreamType type):
45
BufferedStreamBuf(STREAM_BUFFER_SIZE, std::ios::in),
50
_zstr.zalloc = Z_NULL;
52
_zstr.opaque = Z_NULL;
58
int rc = inflateInit2(&_zstr, 15 + (type == STREAM_GZIP ? 16 : 0));
59
if (rc != Z_OK) throw IOException(zError(rc));
61
_buffer = new char[INFLATE_BUFFER_SIZE];
65
InflatingStreamBuf::InflatingStreamBuf(std::ostream& ostr, StreamType type):
66
BufferedStreamBuf(STREAM_BUFFER_SIZE, std::ios::out),
71
_zstr.zalloc = Z_NULL;
73
_zstr.opaque = Z_NULL;
79
int rc = inflateInit2(&_zstr, 15 + (type == STREAM_GZIP ? 16 : 0));
80
if (rc != Z_OK) throw IOException(zError(rc));
82
_buffer = new char[INFLATE_BUFFER_SIZE];
86
InflatingStreamBuf::~InflatingStreamBuf()
99
int InflatingStreamBuf::close()
102
if (_pIstr || _pOstr)
104
int rc = inflateEnd(&_zstr);
105
if (rc != Z_OK) throw IOException(zError(rc));
113
int InflatingStreamBuf::readFromDevice(char* buffer, std::streamsize length)
115
if (_eof || !_pIstr) return 0;
117
if (_zstr.avail_in == 0)
122
_pIstr->read(_buffer, INFLATE_BUFFER_SIZE);
123
n = _pIstr->gcount();
125
if (n == 0) return 0;
126
_zstr.next_in = (unsigned char*) _buffer;
129
_zstr.next_out = (unsigned char*) buffer;
130
_zstr.avail_out = length;
133
int rc = inflate(&_zstr, Z_NO_FLUSH);
134
if (rc == Z_STREAM_END)
137
return length - _zstr.avail_out;
139
if (rc != Z_OK) throw IOException(zError(rc));
140
if (_zstr.avail_out == 0)
142
if (_zstr.avail_in == 0)
147
_pIstr->read(_buffer, INFLATE_BUFFER_SIZE);
148
n = _pIstr->gcount();
152
_zstr.next_in = (unsigned char*) _buffer;
160
int InflatingStreamBuf::writeToDevice(const char* buffer, std::streamsize length)
162
if (length == 0 || !_pOstr) return 0;
164
_zstr.next_in = (unsigned char*) buffer;
165
_zstr.avail_in = length;
166
_zstr.next_out = (unsigned char*) _buffer;
167
_zstr.avail_out = INFLATE_BUFFER_SIZE;
170
int rc = inflate(&_zstr, Z_NO_FLUSH);
171
if (rc == Z_STREAM_END)
173
_pOstr->write(_buffer, INFLATE_BUFFER_SIZE - _zstr.avail_out);
174
if (!_pOstr->good()) throw IOException(zError(rc));
177
if (rc != Z_OK) throw IOException(zError(rc));
178
if (_zstr.avail_out == 0)
180
_pOstr->write(_buffer, INFLATE_BUFFER_SIZE);
181
if (!_pOstr->good()) throw IOException(zError(rc));
182
_zstr.next_out = (unsigned char*) _buffer;
183
_zstr.avail_out = INFLATE_BUFFER_SIZE;
185
if (_zstr.avail_in == 0)
187
_pOstr->write(_buffer, INFLATE_BUFFER_SIZE - _zstr.avail_out);
188
if (!_pOstr->good()) throw IOException(zError(rc));
189
_zstr.next_out = (unsigned char*) _buffer;
190
_zstr.avail_out = INFLATE_BUFFER_SIZE;
198
InflatingIOS::InflatingIOS(std::ostream& ostr, InflatingStreamBuf::StreamType type):
201
poco_ios_init(&_buf);
205
InflatingIOS::InflatingIOS(std::istream& istr, InflatingStreamBuf::StreamType type):
208
poco_ios_init(&_buf);
212
InflatingIOS::~InflatingIOS()
217
InflatingStreamBuf* InflatingIOS::rdbuf()
223
InflatingOutputStream::InflatingOutputStream(std::ostream& ostr, InflatingStreamBuf::StreamType type):
224
InflatingIOS(ostr, type),
230
InflatingOutputStream::~InflatingOutputStream()
235
int InflatingOutputStream::close()
241
InflatingInputStream::InflatingInputStream(std::istream& istr, InflatingStreamBuf::StreamType type):
242
InflatingIOS(istr, type),
248
InflatingInputStream::~InflatingInputStream()