diff options
author | Tor Andersson <tor@ghostscript.com> | 2011-03-02 15:00:03 +0000 |
---|---|---|
committer | Tor Andersson <tor@ghostscript.com> | 2011-03-02 15:00:03 +0000 |
commit | d01eb80ff0bf5b61949fb346f3348c95ec98863f (patch) | |
tree | 47a5a6725153eb7291e639403df29932606b288c /fitz/filt_flate.c | |
parent | dae1e4df9b6cdd4c04e9aecd13d7064ddbec88f1 (diff) | |
download | mupdf-d01eb80ff0bf5b61949fb346f3348c95ec98863f.tar.xz |
Comply with the stream read protocol in flate decoder: always fill the output buffer with all available data.
Diffstat (limited to 'fitz/filt_flate.c')
-rw-r--r-- | fitz/filt_flate.c | 60 |
1 files changed, 33 insertions, 27 deletions
diff --git a/fitz/filt_flate.c b/fitz/filt_flate.c index acda8c49..0d14a75c 100644 --- a/fitz/filt_flate.c +++ b/fitz/filt_flate.c @@ -10,7 +10,7 @@ struct fz_flate_s z_stream z; }; -static void *zmalloc(void *opaque, unsigned int items, unsigned int size) +static void *zalloc(void *opaque, unsigned int items, unsigned int size) { return fz_calloc(items, size); } @@ -28,36 +28,42 @@ readflated(fz_stream *stm, unsigned char *outbuf, int outlen) z_streamp zp = &state->z; int code; - if (chain->rp == chain->wp) - fz_fillbuffer(chain); - - zp->next_in = chain->rp; - zp->avail_in = chain->wp - chain->rp; zp->next_out = outbuf; zp->avail_out = outlen; - code = inflate(zp, Z_SYNC_FLUSH); - - chain->rp = chain->wp - zp->avail_in; - - if (code == Z_STREAM_END || code == Z_OK) - { - return outlen - zp->avail_out; - } - else if (code == Z_BUF_ERROR) + while (zp->avail_out > 0) { - fz_warn("premature end of data in flate filter"); - return outlen - zp->avail_out; - } - else if (code == Z_DATA_ERROR && zp->avail_in == 0) - { - fz_warn("ignoring zlib error: %s", zp->msg); - return outlen - zp->avail_out; - } - else - { - return fz_throw("zlib error: %s", zp->msg); + if (chain->rp == chain->wp) + fz_fillbuffer(chain); + + zp->next_in = chain->rp; + zp->avail_in = chain->wp - chain->rp; + + code = inflate(zp, Z_SYNC_FLUSH); + + chain->rp = chain->wp - zp->avail_in; + + if (code == Z_STREAM_END) + { + return outlen - zp->avail_out; + } + else if (code == Z_BUF_ERROR) + { + fz_warn("premature end of data in flate filter"); + return outlen - zp->avail_out; + } + else if (code == Z_DATA_ERROR && zp->avail_in == 0) + { + fz_warn("ignoring zlib error: %s", zp->msg); + return outlen - zp->avail_out; + } + else if (code != Z_OK) + { + return fz_throw("zlib error: %s", zp->msg); + } } + + return outlen - zp->avail_out; } static void @@ -83,7 +89,7 @@ fz_openflated(fz_stream *chain) state = fz_malloc(sizeof(fz_flate)); state->chain = chain; - state->z.zalloc = zmalloc; + state->z.zalloc = zalloc; state->z.zfree = zfree; state->z.opaque = nil; state->z.next_in = nil; |