4 #include "zlibcompressed.h"
11 zlib_error::zlib_error(const string &w, int c):
13 runtime_error(w+": "+zError(c)),
21 struct ZlibCompressed::Private
30 ZlibCompressed::Private::Private()
40 ZlibCompressed::ZlibCompressed(Base &b, Mode m, unsigned level):
43 mode = m&below.get_mode()&M_RDWR;
44 if(mode!=M_READ && mode!=M_WRITE)
45 throw invalid_access(m);
52 int ret = deflateInit(&priv->stream, level);
54 throw zlib_error("deflateInit", ret);
58 int ret = inflateInit(&priv->stream);
60 throw zlib_error("inflateInit", ret);
63 in_buffer = new unsigned char[buffer_size];
64 out_buffer = new unsigned char[buffer_size];
66 priv->stream.next_in = in_buffer;
67 priv->stream.avail_in = 0;
68 priv->stream.next_out = out_buffer;
69 priv->stream.avail_out = buffer_size;
71 below.signal_flush_required.connect(sigc::mem_fun(this, &ZlibCompressed::flush));
76 throw zlib_error("unsupported", -1);
80 ZlibCompressed::~ZlibCompressed()
85 while(compress_data(Z_FINISH)) ;
86 deflateEnd(&priv->stream);
89 inflateEnd(&priv->stream);
97 void ZlibCompressed::set_block(bool)
99 throw logic_error("ZlibCompressed::set_block");
102 void ZlibCompressed::set_inherit(bool)
104 throw logic_error("ZlibCompressed::set_inherit");
107 void ZlibCompressed::flush()
114 if(!compress_data(Z_SYNC_FLUSH))
117 // The flush is done when all input data has been consumed
118 if(!priv->stream.avail_in)
125 size_t ZlibCompressed::do_write(const char *data, size_t size)
127 check_access(M_WRITE);
129 size_t processed = 0;
131 while(processed<size)
133 size_t free_in = (in_buffer+buffer_size-priv->stream.next_in);
134 if(free_in<size && priv->stream.next_in>in_buffer)
136 // Not all of the data fits in the buffer, so make some more room
137 copy(priv->stream.next_in, priv->stream.next_in+priv->stream.avail_in, in_buffer);
138 priv->stream.next_in = in_buffer;
139 free_in = buffer_size-priv->stream.avail_in;
144 // Copy as much data into the input buffer as possible
145 size_t len = min(free_in, size-processed);
146 copy(data+processed, data+processed+len, priv->stream.next_in+priv->stream.avail_in);
147 priv->stream.avail_in += len;
151 bool stalled = false;
152 while(priv->stream.avail_in && !stalled)
153 stalled = !compress_data(Z_NO_FLUSH);
165 bool ZlibCompressed::compress_data(int flush_mode)
168 bool can_deflate = ((priv->stream.avail_in || flush_mode) && priv->stream.avail_out);
169 bool finished = false;
172 int ret = deflate(&priv->stream, flush_mode);
173 if(flush_mode==Z_FINISH && ret==Z_STREAM_END)
176 throw zlib_error("deflate", ret);
179 // Write compressed data into the underlying object
181 if(priv->stream.next_out>out_buffer)
182 len = below.write(reinterpret_cast<char *>(out_buffer), priv->stream.next_out-out_buffer);
185 if(len<static_cast<size_t>(priv->stream.next_out-out_buffer))
186 copy(out_buffer+len, priv->stream.next_out, out_buffer);
187 priv->stream.avail_out += len;
188 priv->stream.next_out -= len;
190 else if(!can_deflate)
191 // We weren't able to do anything
201 size_t ZlibCompressed::do_read(char *data, size_t size)
203 check_access(M_READ);
205 size_t processed = 0;
207 while(processed<size)
209 if(priv->stream.next_out>out_buffer)
211 // We have some pending output, give it out first
212 size_t len = min<size_t>(priv->stream.next_out-out_buffer, size-processed);
214 copy(out_buffer, out_buffer+len, data+processed);
217 if(len<static_cast<size_t>(priv->stream.next_out-out_buffer))
218 copy(out_buffer+len, priv->stream.next_out, out_buffer);
219 priv->stream.next_out -= len;
220 priv->stream.avail_out += len;
225 bool need_more_input = !priv->stream.avail_in;
226 if(priv->stream.avail_in)
228 int ret = inflate(&priv->stream, Z_NO_FLUSH);
229 if(ret==Z_STREAM_END)
232 throw zlib_error("inflate", ret);
233 need_more_input = (priv->stream.next_out==out_buffer);
241 if(priv->stream.next_in>in_buffer)
242 copy(priv->stream.next_in, priv->stream.next_in+priv->stream.avail_in, in_buffer);
243 priv->stream.next_in = in_buffer;
245 size_t len = below.read(reinterpret_cast<char *>(priv->stream.next_in), in_buffer+buffer_size-priv->stream.next_in);
246 priv->stream.avail_in += len;
247 if(!len && below.eof())
252 if(size>0 && processed==0 && stream_end)
262 const Handle &ZlibCompressed::get_handle(Mode)
264 throw logic_error("ZlibCompressed::get_handle");