diff options
author | Robin Watts <robin.watts@artifex.com> | 2014-03-11 19:04:17 +0000 |
---|---|---|
committer | Robin Watts <robin.watts@artifex.com> | 2014-03-17 19:38:40 +0000 |
commit | 551de42088c58dc69fba06fb53e36c2ddb12367f (patch) | |
tree | f073b5ac7d3dee2de2f5609a13fd9c47e605d3d8 /source/fitz/filter-flate.c | |
parent | 8fdcb140930c3027841af28a4632ce6d23aa44b6 (diff) | |
download | mupdf-551de42088c58dc69fba06fb53e36c2ddb12367f.tar.xz |
Rework fz_streams.
Currently fz_streams have a 4K buffer within their header. The call
to read from a stream fills this buffer, resulting in more data being
pulled from any underlying stream than we might like. This causes
problems with the forthcoming 'leech' filter.
Here we simplify the fields available in the public stream header.
No specific buffer is given; simply the read and write pointers.
The underlying 'read' function is replaced by a 'next' function
that makes the next block of data available and returns the first
character of it (or EOF).
A caller to the 'next' function should supply the maximum number of
bytes that it knows it will need (possibly not now, but eventually).
This enables the underlying stream to efficiently decode just enough.
The underlying stream is free to return fewer, or a greater number
if it wants to.
The exact size of the 'block' of data returned will depend on the
filter in use and (possibly) the data therein.
Callers can get the currently available amount of data by calling
fz_available (but again should pass the maximum amount of data they know
they will need). The only time this will ever return 0 is if we have
hit EOF.
Diffstat (limited to 'source/fitz/filter-flate.c')
-rw-r--r-- | source/fitz/filter-flate.c | 35 |
1 files changed, 24 insertions, 11 deletions
diff --git a/source/fitz/filter-flate.c b/source/fitz/filter-flate.c index 022cdb12..b306c9c3 100644 --- a/source/fitz/filter-flate.c +++ b/source/fitz/filter-flate.c @@ -8,6 +8,7 @@ struct fz_flate_s { fz_stream *chain; z_stream z; + unsigned char buffer[4096]; }; static void *zalloc(void *opaque, unsigned int items, unsigned int size) @@ -21,23 +22,27 @@ static void zfree(void *opaque, void *ptr) } static int -read_flated(fz_stream *stm, unsigned char *outbuf, int outlen) +next_flated(fz_stream *stm, int outlen) { fz_flate *state = stm->state; fz_stream *chain = state->chain; z_streamp zp = &state->z; int code; + unsigned char *outbuf = state->buffer; + + if (outlen > sizeof(state->buffer)) + outlen = sizeof(state->buffer); + + if (stm->eof) + return EOF; zp->next_out = outbuf; zp->avail_out = outlen; while (zp->avail_out > 0) { - if (chain->rp == chain->wp) - fz_fill_buffer(chain); - + zp->avail_in = fz_available(chain, 1); zp->next_in = chain->rp; - zp->avail_in = chain->wp - chain->rp; code = inflate(zp, Z_SYNC_FLUSH); @@ -45,23 +50,23 @@ read_flated(fz_stream *stm, unsigned char *outbuf, int outlen) if (code == Z_STREAM_END) { - return outlen - zp->avail_out; + break; } else if (code == Z_BUF_ERROR) { fz_warn(stm->ctx, "premature end of data in flate filter"); - return outlen - zp->avail_out; + break; } else if (code == Z_DATA_ERROR && zp->avail_in == 0) { fz_warn(stm->ctx, "ignoring zlib error: %s", zp->msg); - return outlen - zp->avail_out; + break; } else if (code == Z_DATA_ERROR && !strcmp(zp->msg, "incorrect data check")) { fz_warn(stm->ctx, "ignoring zlib error: %s", zp->msg); chain->rp = chain->wp; - return outlen - zp->avail_out; + break; } else if (code != Z_OK) { @@ -69,7 +74,15 @@ read_flated(fz_stream *stm, unsigned char *outbuf, int outlen) } } - return outlen - zp->avail_out; + stm->rp = state->buffer; + stm->wp = state->buffer + outlen - zp->avail_out; + stm->pos += outlen - zp->avail_out; + if (stm->rp == stm->wp) + { + stm->eof = 1; + return EOF; + } + return *stm->rp++; } static void @@ -126,5 +139,5 @@ fz_open_flated(fz_stream *chain) fz_close(chain); fz_rethrow(ctx); } - return fz_new_stream(ctx, state, read_flated, close_flated, rebind_flated); + return fz_new_stream(ctx, state, next_flated, close_flated, rebind_flated); } |