4 #include "zlibcompressed.h"
11 zlib_error::zlib_error(const std::string &w, int c):
13 runtime_error(w+": "+zError(c)),
21 struct ZlibCompressed::Private
30 ZlibCompressed::Private::Private()
40 ZlibCompressed::ZlibCompressed(Base &b, unsigned level):
43 mode = below.get_mode()&M_RDWR;
44 if(mode!=M_READ && mode!=M_WRITE)
45 throw invalid_access(mode);
50 ZlibCompressed::ZlibCompressed(Base &b, Mode m, unsigned level):
53 mode = m&below.get_mode()&M_RDWR;
54 if(mode!=M_READ && mode!=M_WRITE)
55 throw invalid_access(m);
60 void ZlibCompressed::init(unsigned level)
71 int ret = deflateInit(&priv->stream, level);
73 throw zlib_error("deflateInit", ret);
77 int ret = inflateInit(&priv->stream);
79 throw zlib_error("inflateInit", ret);
82 in_buffer = new unsigned char[buffer_size];
83 out_buffer = new unsigned char[buffer_size];
85 priv->stream.next_in = in_buffer;
86 priv->stream.avail_in = 0;
87 priv->stream.next_out = out_buffer;
88 priv->stream.avail_out = buffer_size;
90 below.signal_flush_required.connect(sigc::mem_fun(this, &ZlibCompressed::flush));
95 throw zlib_error("unsupported", -1);
99 ZlibCompressed::~ZlibCompressed()
104 while(compress_data(Z_FINISH)) ;
105 deflateEnd(&priv->stream);
108 inflateEnd(&priv->stream);
116 void ZlibCompressed::set_block(bool)
118 throw logic_error("ZlibCompressed::set_block");
121 void ZlibCompressed::flush()
128 if(!compress_data(Z_SYNC_FLUSH))
131 // The flush is done when all input data has been consumed
132 if(!priv->stream.avail_in)
139 unsigned ZlibCompressed::do_write(const char *data, unsigned size)
141 check_access(M_WRITE);
143 unsigned processed = 0;
145 while(processed<size)
147 unsigned free_in = (in_buffer+buffer_size-priv->stream.next_in);
148 if(free_in<size && priv->stream.next_in>in_buffer)
150 // Not all of the data fits in the buffer, so make some more room
151 copy(priv->stream.next_in, priv->stream.next_in+priv->stream.avail_in, in_buffer);
152 priv->stream.next_in = in_buffer;
153 free_in = buffer_size-priv->stream.avail_in;
158 // Copy as much data into the input buffer as possible
159 unsigned len = min(free_in, size-processed);
160 copy(data+processed, data+processed+len, priv->stream.next_in+priv->stream.avail_in);
161 priv->stream.avail_in += len;
165 bool stalled = false;
166 while(priv->stream.avail_in && !stalled)
167 stalled = !compress_data(Z_NO_FLUSH);
179 bool ZlibCompressed::compress_data(int flush_mode)
182 bool can_deflate = ((priv->stream.avail_in || flush_mode) && priv->stream.avail_out);
183 bool finished = false;
186 int ret = deflate(&priv->stream, flush_mode);
187 if(flush_mode==Z_FINISH && ret==Z_STREAM_END)
190 throw zlib_error("deflate", ret);
193 // Write compressed data into the underlying object
195 if(priv->stream.next_out>out_buffer)
196 len = below.write(reinterpret_cast<char *>(out_buffer), priv->stream.next_out-out_buffer);
199 if(len<static_cast<unsigned>(priv->stream.next_out-out_buffer))
200 copy(out_buffer+len, priv->stream.next_out, out_buffer);
201 priv->stream.avail_out += len;
202 priv->stream.next_out -= len;
204 else if(!can_deflate)
205 // We weren't able to do anything
215 unsigned ZlibCompressed::do_read(char *data, unsigned size)
217 check_access(M_READ);
219 unsigned processed = 0;
221 while(processed<size)
223 if(priv->stream.next_out>out_buffer)
225 // We have some pending output, give it out first
226 unsigned len = min<unsigned>(priv->stream.next_out-out_buffer, size-processed);
228 copy(out_buffer, out_buffer+len, data+processed);
231 if(len<static_cast<unsigned>(priv->stream.next_out-out_buffer))
232 copy(out_buffer+len, priv->stream.next_out, out_buffer);
233 priv->stream.next_out -= len;
234 priv->stream.avail_out += len;
239 bool need_more_input = !priv->stream.avail_in;
240 if(priv->stream.avail_in)
242 int ret = inflate(&priv->stream, Z_NO_FLUSH);
243 if(ret==Z_STREAM_END)
246 throw zlib_error("inflate", ret);
247 need_more_input = (priv->stream.next_out==out_buffer);
255 if(priv->stream.next_in>in_buffer)
256 copy(priv->stream.next_in, priv->stream.next_in+priv->stream.avail_in, in_buffer);
257 priv->stream.next_in = in_buffer;
259 unsigned len = below.read(reinterpret_cast<char *>(priv->stream.next_in), in_buffer+buffer_size-priv->stream.next_in);
260 priv->stream.avail_in += len;
261 if(!len && below.eof())
266 if(size>0 && processed==0 && stream_end)
276 const Handle &ZlibCompressed::get_handle(Mode)
278 throw logic_error("ZlibCompressed::get_handle");