summaryrefslogtreecommitdiff
path: root/source/fitz/stream-read.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/stream-read.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/stream-read.c')
-rw-r--r--source/fitz/stream-read.c97
1 files changed, 12 insertions, 85 deletions
diff --git a/source/fitz/stream-read.c b/source/fitz/stream-read.c
index 71ae7666..02f186a0 100644
--- a/source/fitz/stream-read.c
+++ b/source/fitz/stream-read.c
@@ -7,89 +7,26 @@ fz_read(fz_stream *stm, unsigned char *buf, int len)
{
int count, n;
- count = fz_mini(len, stm->wp - stm->rp);
- if (count)
+ count = 0;
+ do
{
- memcpy(buf, stm->rp, count);
- stm->rp += count;
- }
-
- if (count == len || stm->error || stm->eof)
- return count;
-
- assert(stm->rp == stm->wp);
-
- if (len - count < stm->ep - stm->bp)
- {
- n = stm->read(stm, stm->bp, stm->ep - stm->bp);
+ n = fz_available(stm, len);
+ if (n > len)
+ n = len;
if (n == 0)
- {
- stm->eof = 1;
- }
- else if (n > 0)
- {
- stm->rp = stm->bp;
- stm->wp = stm->bp + n;
- stm->pos += n;
- }
+ break;
- n = fz_mini(len - count, stm->wp - stm->rp);
- if (n)
- {
- memcpy(buf + count, stm->rp, n);
- stm->rp += n;
- count += n;
- }
- }
- else
- {
- n = stm->read(stm, buf + count, len - count);
- if (n == 0)
- {
- stm->eof = 1;
- }
- else if (n > 0)
- {
- stm->pos += n;
- count += n;
- }
+ memcpy(buf, stm->rp, n);
+ stm->rp += n;
+ buf += n;
+ count += n;
+ len -= n;
}
+ while (len > 0);
return count;
}
-void
-fz_fill_buffer(fz_stream *stm)
-{
- int n;
-
- assert(stm->rp == stm->wp);
-
- if (stm->error || stm->eof)
- return;
-
- fz_try(stm->ctx)
- {
- n = stm->read(stm, stm->bp, stm->ep - stm->bp);
- if (n == 0)
- {
- stm->eof = 1;
- }
- else if (n > 0)
- {
- stm->rp = stm->bp;
- stm->wp = stm->bp + n;
- stm->pos += n;
- }
- }
- fz_catch(stm->ctx)
- {
- fz_rethrow_if(stm->ctx, FZ_ERROR_TRYLATER);
- fz_warn(stm->ctx, "read error; treating as end of file");
- stm->error = 1;
- }
-}
-
fz_buffer *
fz_read_all(fz_stream *stm, int initial)
{
@@ -196,16 +133,6 @@ fz_seek(fz_stream *stm, int offset, int whence)
offset = fz_tell(stm) + offset;
whence = 0;
}
- if (whence == 0)
- {
- int dist = stm->pos - offset;
- if (dist >= 0 && dist <= stm->wp - stm->bp)
- {
- stm->rp = stm->wp - dist;
- stm->eof = 0;
- return;
- }
- }
stm->seek(stm, offset, whence);
stm->eof = 0;
}