11 #define ZBUFSIZE 10240
15 out_limit(_out_limit), tmpbuf(ZBUFSIZE), mode(_mode)
23 WvGzipEncoder::~WvGzipEncoder()
29 void WvGzipEncoder::init()
32 memset(zstr, 0,
sizeof(*zstr));
33 zstr->zalloc = Z_NULL;
40 retval = deflateInit(zstr, Z_BEST_SPEED);
42 retval = inflateInit(zstr);
46 seterror(
"error %s initializing gzip %s: %s", retval,
47 mode ==
Deflate ?
"compressor" :
"decompressor",
48 zstr->msg ? zstr->msg :
"unknown");
51 zstr->next_in = zstr->next_out = NULL;
52 zstr->avail_in = zstr->avail_out = 0;
55 void WvGzipEncoder::close()
72 size_t starting_size = inbuf.
used();
74 bool alldata = inbuf.
used() == 0;
75 success = process(outbuf,
flush && alldata,
false);
76 if (zstr->avail_in != 0)
79 inbuf.
unget(zstr->avail_in);
84 if (alldata || (starting_size == inbuf.
used()) ||
94 return process(outbuf,
false,
true);
106 void WvGzipEncoder::prepare(
WvBuf *inbuf)
108 assert(zstr->avail_in == 0);
109 if (inbuf && inbuf->
used() != 0)
112 zstr->avail_in = avail;
113 zstr->next_in =
const_cast<Bytef*
>(
114 (
const Bytef*)inbuf->
get(avail));
119 zstr->next_in = (Bytef*)
"";
124 bool WvGzipEncoder::process(
WvBuf &outbuf,
bool flush,
bool finish)
126 int flushmode =
finish ? Z_FINISH :
133 size_t avail_out = tmpbuf.
free();
138 zstr->avail_out = avail_out;
139 zstr->next_out = tmpbuf.
alloc(avail_out);
141 retval = deflate(zstr, flushmode);
143 retval = inflate(zstr, flushmode);
144 tmpbuf.
unalloc(zstr->avail_out);
146 output += avail_out - zstr->avail_out;
149 outbuf.
merge(tmpbuf);
151 if (retval == Z_DATA_ERROR && mode ==
Inflate
153 retval = inflateSync(zstr);
156 if (retval == Z_STREAM_END)
158 else if (retval != Z_OK && retval != Z_BUF_ERROR &&
159 !(retval == Z_DATA_ERROR && mode ==
Inflate
162 seterror(
"error %s during gzip %s: %s", retval,
163 mode ==
Deflate ?
"compression" :
"decompression",
164 zstr->msg ? zstr->msg :
"unknown");
const T * get(size_t count)
Reads exactly the specified number of elements and returns a pointer to a storage location owned by t...
void unget(size_t count)
Ungets exactly the specified number of elements by returning them to the buffer for subsequent reads.
T * alloc(size_t count)
Allocates exactly the specified number of elements and returns a pointer to an UNINITIALIZED storage ...
void unalloc(size_t count)
Unallocates exactly the specified number of elements by removing them from the buffer and releasing t...
size_t free() const
Returns the number of elements that the buffer can currently accept for writing.
virtual bool _finish(WvBuf &outbuf)
Template method implementation of finish().
size_t optgettable() const
Returns the optimal maximum number of elements in the buffer currently available for reading without ...
bool full_flush
Do full flushes.
virtual bool _encode(WvBuf &inbuf, WvBuf &outbuf, bool flush)
Template method implementation of encode().
virtual bool _reset()
Template method implementation of reset().
Specialization of WvBufBase for unsigned char type buffers intended for use with raw memory buffers.
bool ignore_decompression_errors
Continue decompression if errors are found.
void zap()
Clears the buffer.
size_t out_limit
Limit the amount of output produced in one call to encode().
bool finish(WvBuf &outbuf)
Tells the encoder that NO MORE DATA will ever be encoded.
bool flush(WvBuf &inbuf, WvBuf &outbuf, bool finish=false)
Flushes the encoder and optionally finishes it.
void setfinished()
Sets 'finished' to true explicitly.
size_t used() const
Returns the number of elements in the buffer currently available for reading.
void merge(Buffer &inbuf, size_t count)
Efficiently moves count bytes from the specified buffer into this one.
void seterror(WvStringParm message)
Sets an error condition, then setnotok().
WvGzipEncoder(Mode mode, size_t _out_limit=0)
Creates a Gzip encoder.