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):
48 mode = below.get_mode()&M_RDWR;
49 if(mode!=M_READ && mode!=M_WRITE)
50 throw invalid_access(mode);
56 int ret = deflateInit(&priv->stream, level);
58 throw zlib_error("deflateInit", ret);
62 int ret = inflateInit(&priv->stream);
64 throw zlib_error("inflateInit", ret);
67 in_buffer = new unsigned char[buffer_size];
68 out_buffer = new unsigned char[buffer_size];
70 priv->stream.next_in = in_buffer;
71 priv->stream.avail_in = 0;
72 priv->stream.next_out = out_buffer;
73 priv->stream.avail_out = buffer_size;
75 below.signal_flush_required.connect(sigc::mem_fun(this, &ZlibCompressed::flush));
78 throw zlib_error("unsupported", -1);
82 ZlibCompressed::~ZlibCompressed()
87 while(compress_data(Z_FINISH)) ;
88 deflateEnd(&priv->stream);
91 inflateEnd(&priv->stream);
99 void ZlibCompressed::flush()
106 if(!compress_data(Z_SYNC_FLUSH))
109 // The flush is done when all input data has been consumed
110 if(!priv->stream.avail_in)
117 unsigned ZlibCompressed::do_write(const char *data, unsigned size)
119 check_access(M_WRITE);
121 unsigned processed = 0;
123 while(processed<size)
125 unsigned free_in = (in_buffer+buffer_size-priv->stream.next_in);
126 if(free_in<size && priv->stream.next_in>in_buffer)
128 // Not all of the data fits in the buffer, so make some more room
129 copy(priv->stream.next_in, priv->stream.next_in+priv->stream.avail_in, in_buffer);
130 priv->stream.next_in = in_buffer;
131 free_in = buffer_size-priv->stream.avail_in;
136 // Copy as much data into the input buffer as possible
137 unsigned len = min(free_in, size-processed);
138 copy(data+processed, data+processed+len, priv->stream.next_in+priv->stream.avail_in);
139 priv->stream.avail_in += len;
143 bool stalled = false;
144 while(priv->stream.avail_in && !stalled)
145 stalled = !compress_data(Z_NO_FLUSH);
157 bool ZlibCompressed::compress_data(int flush_mode)
160 bool can_deflate = ((priv->stream.avail_in || flush_mode) && priv->stream.avail_out);
161 bool finished = false;
164 int ret = deflate(&priv->stream, flush_mode);
165 if(flush_mode==Z_FINISH && ret==Z_STREAM_END)
168 throw zlib_error("deflate", ret);
171 // Write compressed data into the underlying object
173 if(priv->stream.next_out>out_buffer)
174 len = below.write(reinterpret_cast<char *>(out_buffer), priv->stream.next_out-out_buffer);
177 if(len<static_cast<unsigned>(priv->stream.next_out-out_buffer))
178 copy(out_buffer+len, priv->stream.next_out, out_buffer);
179 priv->stream.avail_out += len;
180 priv->stream.next_out -= len;
182 else if(!can_deflate)
183 // We weren't able to do anything
193 unsigned ZlibCompressed::do_read(char *data, unsigned size)
195 check_access(M_READ);
197 unsigned processed = 0;
199 while(processed<size)
201 if(priv->stream.next_out>out_buffer)
203 // We have some pending output, give it out first
204 unsigned len = min<unsigned>(priv->stream.next_out-out_buffer, size-processed);
206 copy(out_buffer, out_buffer+len, data+processed);
209 if(len<static_cast<unsigned>(priv->stream.next_out-out_buffer))
210 copy(out_buffer+len, priv->stream.next_out, out_buffer);
211 priv->stream.next_out -= len;
212 priv->stream.avail_out += len;
217 bool need_more_input = !priv->stream.avail_in;
218 if(priv->stream.avail_in)
220 int ret = inflate(&priv->stream, Z_NO_FLUSH);
221 if(ret==Z_STREAM_END)
224 throw zlib_error("inflate", ret);
225 need_more_input = (priv->stream.next_out==out_buffer);
233 if(priv->stream.next_in>in_buffer)
234 copy(priv->stream.next_in, priv->stream.next_in+priv->stream.avail_in, in_buffer);
235 priv->stream.next_in = in_buffer;
237 unsigned len = below.read(reinterpret_cast<char *>(priv->stream.next_in), in_buffer+buffer_size-priv->stream.next_in);
238 priv->stream.avail_in += len;
239 if(!len && below.eof())