summaryrefslogtreecommitdiff
path: root/source/fitz/filter-lzw.c
diff options
context:
space:
mode:
authorRobin Watts <robin.watts@artifex.com>2014-03-11 19:04:17 +0000
committerRobin Watts <robin.watts@artifex.com>2014-03-17 19:38:40 +0000
commit551de42088c58dc69fba06fb53e36c2ddb12367f (patch)
treef073b5ac7d3dee2de2f5609a13fd9c47e605d3d8 /source/fitz/filter-lzw.c
parent8fdcb140930c3027841af28a4632ce6d23aa44b6 (diff)
downloadmupdf-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-lzw.c')
-rw-r--r--source/fitz/filter-lzw.c23
1 files changed, 18 insertions, 5 deletions
diff --git a/source/fitz/filter-lzw.c b/source/fitz/filter-lzw.c
index c78c8c3f..e13b0ef0 100644
--- a/source/fitz/filter-lzw.c
+++ b/source/fitz/filter-lzw.c
@@ -41,15 +41,18 @@ struct fz_lzwd_s
unsigned char bp[MAX_LENGTH];
unsigned char *rp, *wp;
+
+ unsigned char buffer[4096];
};
static int
-read_lzwd(fz_stream *stm, unsigned char *buf, int len)
+next_lzwd(fz_stream *stm, int len)
{
fz_lzwd *lzw = stm->state;
lzw_code *table = lzw->table;
+ unsigned char *buf = lzw->buffer;
unsigned char *p = buf;
- unsigned char *ep = buf + len;
+ unsigned char *ep;
unsigned char *s;
int codelen;
@@ -58,13 +61,17 @@ read_lzwd(fz_stream *stm, unsigned char *buf, int len)
int old_code = lzw->old_code;
int next_code = lzw->next_code;
+ if (len > sizeof(lzw->buffer))
+ len = sizeof(lzw->buffer);
+ ep = buf + len;
+
while (lzw->rp < lzw->wp && p < ep)
*p++ = *lzw->rp++;
while (p < ep)
{
if (lzw->eod)
- return 0;
+ return EOF;
code = fz_read_bits(lzw->chain, code_bits);
@@ -162,7 +169,13 @@ read_lzwd(fz_stream *stm, unsigned char *buf, int len)
lzw->old_code = old_code;
lzw->next_code = next_code;
- return p - buf;
+ stm->rp = buf;
+ stm->wp = p;
+ if (buf == p)
+ return EOF;
+ stm->pos += p - buf;
+
+ return *stm->rp++;
}
static void
@@ -228,5 +241,5 @@ fz_open_lzwd(fz_stream *chain, int early_change)
fz_rethrow(ctx);
}
- return fz_new_stream(ctx, lzw, read_lzwd, close_lzwd, rebind_lzwd);
+ return fz_new_stream(ctx, lzw, next_lzwd, close_lzwd, rebind_lzwd);
}