| 1 |
|
| 2 |
#include <zlib.h> |
| 3 |
|
| 4 |
#include "utilmem.h" |
| 5 |
#include "utillog.h" |
| 6 |
#include "utilflate.h" |
| 7 |
|
| 8 |
/* flate codec */ |
| 9 |
|
| 10 |
/* |
| 11 |
Flate codec example provided at http://www.zlib.net/zpipe.c (http://www.zlib.net/zlib_how.html) uses the following scheme: |
| 12 |
- provide input data buffer |
| 13 |
- keep providing output until codec function uses it |
| 14 |
|
| 15 |
For encoder: |
| 16 |
|
| 17 |
z->zalloc = z->zfree = z->zopaque = NULL; |
| 18 |
deflateInit(z, compression_level); |
| 19 |
do { |
| 20 |
z->next_in = <input buffer> |
| 21 |
z->avail_in = <input buffer bytes> |
| 22 |
do { |
| 23 |
z->next_out = <output buffer> |
| 24 |
z->avail_out = <output buffer bytes> |
| 25 |
deflate(z, flush); |
| 26 |
// write obtained output from deflate |
| 27 |
} while (z->avail_out == 0); |
| 28 |
assert(z->avail_in == 0); |
| 29 |
} while (flush != Z_FINISH); |
| 30 |
deflateEnd(z); |
| 31 |
|
| 32 |
'z' is an internal codec state of type z_stream, 'flush' is either Z_NO_FLUSH or Z_FINISH at the end of data. |
| 33 |
deflate() ensures to consume the entire input if there are no obstackles to write an output. The inner loop |
| 34 |
provides an output space as long as it is used by deflate(). When deflate() wrote everything it could, |
| 35 |
it leaves z->avail_out > 0, which breaks the inner loop. At this point z->avail_in should also be zero. |
| 36 |
The example documentation claims that the return codes from deflate() doesn't really need to be checked, |
| 37 |
as checking z->avail_out for zero is enough. |
| 38 |
|
| 39 |
The scheme for decoder is pretty similar, but with substantial differences: |
| 40 |
- the end of stream is automatically found by decoder, so using Z_FINISH flag to indicate an end of stream |
| 41 |
is not necessary, but if provided, it MUST be given only if the EOF marker actually occurs in the input chunk, |
| 42 |
and subsequent calls to inflate() must consequently use Z_FINISH |
| 43 |
- calling inflate() as long as it uses the output buffer provided still works for decoder, but inflate() |
| 44 |
does not ensure to consume the entire input, as it will read until end of stream marker |
| 45 |
- the return code from inflate() must be checked to ensure the proper reaction on invalid data stream and |
| 46 |
end of stream signals |
| 47 |
- initialization must set an input buffer to NULL or to some existing chunk (the later helps zlib to perform |
| 48 |
better on inflate(), but inflate() does the research on the first call anyway) |
| 49 |
|
| 50 |
z->zalloc = z->zfree = z->zopaque = NULL; |
| 51 |
z->next_in = NULL, z->avail_in = 0; |
| 52 |
inflateInit(z); |
| 53 |
do { |
| 54 |
z->next_in = <input buffer> |
| 55 |
z->avail_in = <input buffer bytes> |
| 56 |
do { |
| 57 |
z->next_out = <output buffer> |
| 58 |
z->avail_out = <output buffer bytes> |
| 59 |
status = inflate(z, flush); |
| 60 |
// check return status |
| 61 |
// write obtained output from inflate |
| 62 |
} while (z->avail_out == 0); |
| 63 |
} while (status != Z_STREAM_END); |
| 64 |
inflateEnd(z); |
| 65 |
|
| 66 |
Our wrapper generally follows "prepare input, keep pomping output" scheme, but we need to support handler function |
| 67 |
breaks on IOFEMPTY and IOFFULL. For a consistent come back from those on subsequent calls to the handler function, |
| 68 |
we use 3 states: |
| 69 |
- FLATE_IN - get input, when got something then goto FALTE_OUT |
| 70 |
- FLATE_OUT - set z_stream buffers and keep writing output until enything to write, then goto FLATE_IN or FLATE_DONE |
| 71 |
- FLATE_DONE - we are done, no return from that state |
| 72 |
Distinction of FLATE_IN and FLATE_OUT states guarantees that we will not get more input until zlib consumes the stuff |
| 73 |
from the previous feed, possibly interrupted by IOFFULL return on filling the output buffer. This distinction is not |
| 74 |
critical, but makes the filter running according to the scheme described above. Note that we set zlib input buffer |
| 75 |
(z->next_in, z->avail_in) at the beginning of FLATE_OUT state. Also note that we always update our buffers according |
| 76 |
to updated avail_in / avail_out values, just after a call to inflate() / deflate(). So no matter what have happens |
| 77 |
between handler calls, zlib input buffer is in sync with ours. |
| 78 |
*/ |
| 79 |
|
| 80 |
struct flate_state { |
| 81 |
z_stream z; |
| 82 |
int flush; |
| 83 |
int status; |
| 84 |
int level; /* encoder compression level -1..9 */ |
| 85 |
}; |
| 86 |
|
| 87 |
typedef union { flate_state *flatestate; void *voidstate; } flate_state_pointer; // to avoid 'dereferencing type-puned ...' warnings |
| 88 |
|
| 89 |
enum { |
| 90 |
FLATE_IN, |
| 91 |
FLATE_OUT, |
| 92 |
FLATE_DONE |
| 93 |
}; |
| 94 |
|
| 95 |
flate_state * flate_decoder_init (flate_state *state) |
| 96 |
{ /* initialize zlib */ |
| 97 |
z_stream *z = &state->z; |
| 98 |
z->zalloc = Z_NULL; |
| 99 |
z->zfree = Z_NULL; |
| 100 |
z->opaque = Z_NULL; |
| 101 |
z->avail_in = 0; /* must be initialized before inflateInit() */ |
| 102 |
z->next_in = Z_NULL; /* ditto */ |
| 103 |
if (inflateInit(z) != Z_OK) |
| 104 |
return NULL; |
| 105 |
state->status = FLATE_IN; |
| 106 |
return state; |
| 107 |
} |
| 108 |
|
| 109 |
flate_state * flate_encoder_init (flate_state *state) |
| 110 |
{ |
| 111 |
z_stream *z = &state->z; |
| 112 |
z->zalloc = Z_NULL; |
| 113 |
z->zfree = Z_NULL; |
| 114 |
z->opaque = Z_NULL; |
| 115 |
z->avail_in = 0; |
| 116 |
z->next_in = Z_NULL; |
| 117 |
state->level = Z_DEFAULT_COMPRESSION; // will probably be moved upward |
| 118 |
if (deflateInit(z, state->level) != Z_OK) |
| 119 |
return NULL; |
| 120 |
state->status = FLATE_IN; |
| 121 |
return state; |
| 122 |
} |
| 123 |
|
| 124 |
static const char * zmess (int zstatus) |
| 125 |
{ |
| 126 |
switch (zstatus) |
| 127 |
{ |
| 128 |
case Z_OK: return "ok"; |
| 129 |
case Z_STREAM_END: return "end of stream"; |
| 130 |
case Z_BUF_ERROR: return "buffer error"; |
| 131 |
case Z_STREAM_ERROR: return "stream error"; |
| 132 |
case Z_NEED_DICT: return "need dict"; |
| 133 |
case Z_DATA_ERROR: return "data error"; |
| 134 |
case Z_MEM_ERROR: return "memory error"; |
| 135 |
case Z_VERSION_ERROR: return "version error"; |
| 136 |
case Z_ERRNO: return "io error"; |
| 137 |
default: |
| 138 |
break; |
| 139 |
} |
| 140 |
return "unknown error"; |
| 141 |
} |
| 142 |
|
| 143 |
iof_status flate_decode_state (iof *I, iof *O, flate_state *state) |
| 144 |
{ |
| 145 |
z_stream *z; |
| 146 |
int zstatus = Z_OK; |
| 147 |
z = &state->z; |
| 148 |
while (state->status != FLATE_DONE) |
| 149 |
{ |
| 150 |
if (state->status == FLATE_IN) |
| 151 |
{ |
| 152 |
if (!iof_readable(I)) |
| 153 |
return state->flush ? IOFERR : IOFEMPTY; |
| 154 |
state->status = FLATE_OUT; |
| 155 |
} |
| 156 |
z->next_in = (Bytef *)I->pos; |
| 157 |
z->avail_in = (uInt)iof_left(I); |
| 158 |
do { |
| 159 |
if (!iof_writable(O)) |
| 160 |
return IOFFULL; |
| 161 |
z->next_out = (Bytef *)O->pos; |
| 162 |
z->avail_out = (uInt)iof_left(O); |
| 163 |
zstatus = inflate(z, Z_NO_FLUSH); |
| 164 |
I->pos += iof_left(I) - z->avail_in; |
| 165 |
O->pos += iof_left(O) - z->avail_out; |
| 166 |
switch (zstatus) |
| 167 |
{ |
| 168 |
case Z_OK: |
| 169 |
case Z_STREAM_END: |
| 170 |
break; |
| 171 |
default: |
| 172 |
loggerf("flate decoder %s (%d)", zmess(zstatus), zstatus); |
| 173 |
return IOFERR; |
| 174 |
} |
| 175 |
} while (z->avail_out == 0); |
| 176 |
state->status = zstatus == Z_STREAM_END ? FLATE_DONE : FLATE_IN; |
| 177 |
} |
| 178 |
return IOFEOF; |
| 179 |
} |
| 180 |
|
| 181 |
iof_status flate_encode_state (iof *I, iof *O, flate_state *state) |
| 182 |
{ |
| 183 |
z_stream *z; |
| 184 |
int zstatus; |
| 185 |
z = &state->z; |
| 186 |
while (state->status != FLATE_DONE) |
| 187 |
{ |
| 188 |
if (state->status == FLATE_IN) |
| 189 |
{ |
| 190 |
if (!iof_readable(I)) |
| 191 |
if (!state->flush) |
| 192 |
return IOFEMPTY; |
| 193 |
state->status = FLATE_OUT; |
| 194 |
} |
| 195 |
z->next_in = (Bytef *)I->pos; |
| 196 |
z->avail_in = (uInt)iof_left(I); |
| 197 |
do { |
| 198 |
if (!iof_writable(O)) |
| 199 |
return IOFFULL; |
| 200 |
z->next_out = (Bytef *)O->pos; |
| 201 |
z->avail_out = (uInt)iof_left(O); |
| 202 |
zstatus = deflate(z, state->flush ? Z_FINISH : Z_NO_FLUSH); |
| 203 |
I->pos += iof_left(I) - z->avail_in; |
| 204 |
O->pos += iof_left(O) - z->avail_out; |
| 205 |
switch (zstatus) |
| 206 |
{ |
| 207 |
case Z_OK: |
| 208 |
case Z_STREAM_END: |
| 209 |
break; |
| 210 |
default: |
| 211 |
loggerf("flate encoder %s (%d)", zmess(zstatus), zstatus); |
| 212 |
return IOFERR; |
| 213 |
} |
| 214 |
} while (z->avail_out == 0); |
| 215 |
state->status = state->flush ? FLATE_DONE : FLATE_IN; |
| 216 |
} |
| 217 |
return IOFEOF; |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
void flate_decoder_close (flate_state *state) |
| 222 |
{ |
| 223 |
inflateEnd(&state->z); |
| 224 |
} |
| 225 |
|
| 226 |
void flate_encoder_close (flate_state *state) |
| 227 |
{ |
| 228 |
deflateEnd(&state->z); |
| 229 |
} |
| 230 |
|
| 231 |
/* filter */ |
| 232 |
|
| 233 |
// flate decoder function |
| 234 |
|
| 235 |
static size_t flate_decoder (iof *F, iof_mode mode) |
| 236 |
{ |
| 237 |
flate_state *state; |
| 238 |
iof_status status; |
| 239 |
size_t tail; |
| 240 |
|
| 241 |
state = iof_filter_state(flate_state *, F); |
| 242 |
switch(mode) |
| 243 |
{ |
| 244 |
case IOFLOAD: |
| 245 |
case IOFREAD: |
| 246 |
if (F->flags & IOF_STOPPED) |
| 247 |
return 0; |
| 248 |
tail = iof_tail(F); |
| 249 |
F->pos = F->buf + tail; |
| 250 |
F->end = F->buf + F->space; |
| 251 |
do { |
| 252 |
status = flate_decode_state(F->next, F, state); |
| 253 |
} while (mode == IOFLOAD && status == IOFFULL && iof_resize_buffer(F)); |
| 254 |
return iof_decoder_retval(F, "flate", status); |
| 255 |
case IOFCLOSE: |
| 256 |
flate_decoder_close(state); |
| 257 |
iof_free(F); |
| 258 |
return 0; |
| 259 |
default: |
| 260 |
break; |
| 261 |
} |
| 262 |
return 0; |
| 263 |
} |
| 264 |
|
| 265 |
// flate encoder function |
| 266 |
|
| 267 |
static size_t flate_encoder (iof *F, iof_mode mode) |
| 268 |
{ |
| 269 |
flate_state *state; |
| 270 |
iof_status status; |
| 271 |
|
| 272 |
state = iof_filter_state(flate_state *, F); |
| 273 |
switch (mode) |
| 274 |
{ |
| 275 |
case IOFFLUSH: |
| 276 |
state->flush = 1; |
| 277 |
FALLTHRU // fall through |
| 278 |
case IOFWRITE: |
| 279 |
F->end = F->pos; |
| 280 |
F->pos = F->buf; |
| 281 |
status = flate_encode_state(F, F->next, state); |
| 282 |
return iof_encoder_retval(F, "flate", status); |
| 283 |
case IOFCLOSE: |
| 284 |
if (!state->flush) |
| 285 |
flate_encoder(F, IOFFLUSH); |
| 286 |
flate_encoder_close(state); |
| 287 |
iof_free(F); |
| 288 |
return 0; |
| 289 |
default: |
| 290 |
break; |
| 291 |
} |
| 292 |
return 0; |
| 293 |
} |
| 294 |
|
| 295 |
iof * iof_filter_flate_decoder (iof *N) |
| 296 |
{ |
| 297 |
iof *I; |
| 298 |
flate_state_pointer P; |
| 299 |
I = iof_filter_reader(flate_decoder, sizeof(flate_state), &P.voidstate); |
| 300 |
iof_setup_next(I, N); |
| 301 |
if (flate_decoder_init(P.flatestate) == NULL) |
| 302 |
{ |
| 303 |
iof_discard(I); |
| 304 |
return NULL; |
| 305 |
} |
| 306 |
P.flatestate->flush = 1; |
| 307 |
return I; |
| 308 |
} |
| 309 |
|
| 310 |
iof * iof_filter_flate_encoder (iof *N) |
| 311 |
{ |
| 312 |
iof *O; |
| 313 |
flate_state_pointer P; |
| 314 |
O = iof_filter_writer(flate_encoder, sizeof(flate_state), &P.voidstate); |
| 315 |
iof_setup_next(O, N); |
| 316 |
if (flate_encoder_init(P.flatestate) == NULL) |
| 317 |
{ |
| 318 |
iof_discard(O); |
| 319 |
return NULL; |
| 320 |
} |
| 321 |
return O; |
| 322 |
} |