summaryrefslogtreecommitdiff
path: root/fitz/stm_read.c
diff options
context:
space:
mode:
authorTor Andersson <tor@ghostscript.com>2011-03-02 02:38:16 +0000
committerTor Andersson <tor@ghostscript.com>2011-03-02 02:38:16 +0000
commitdae1e4df9b6cdd4c04e9aecd13d7064ddbec88f1 (patch)
tree923fb555d6bc3368d15ea1dd96a03a95bb0c4526 /fitz/stm_read.c
parent5e59b61da17cf29ff3359c4806457aaf1e910439 (diff)
downloadmupdf-dae1e4df9b6cdd4c04e9aecd13d7064ddbec88f1.tar.xz
Use buffering for small reads.
Diffstat (limited to 'fitz/stm_read.c')
-rw-r--r--fitz/stm_read.c60
1 files changed, 44 insertions, 16 deletions
diff --git a/fitz/stm_read.c b/fitz/stm_read.c
index 51c9f590..af5afab6 100644
--- a/fitz/stm_read.c
+++ b/fitz/stm_read.c
@@ -3,35 +3,56 @@
int
fz_read(fz_stream *stm, unsigned char *buf, int len)
{
- int avail, count;
+ int count, n;
- avail = stm->wp - stm->rp;
- if (avail)
+ count = MIN(len, stm->wp - stm->rp);
+ if (count)
{
- count = MIN(len, avail);
memcpy(buf, stm->rp, count);
stm->rp += count;
}
- else
- {
- count = 0;
- }
- if (stm->dead)
+ if (count == len || stm->dead)
return count;
- while (len > count)
+ assert(stm->rp == stm->wp);
+
+ if (len - count < stm->ep - stm->bp)
{
- int n = stm->read(stm, buf + count, len - count);
+ n = stm->read(stm, stm->bp, stm->ep - stm->bp);
if (n < 0)
{
stm->dead = 1;
return fz_rethrow(n, "read error");
}
- if (n == 0)
- break;
- stm->pos += n;
- count += n;
+ else if (n > 0)
+ {
+ stm->rp = stm->bp;
+ stm->wp = stm->bp + n;
+ stm->pos += n;
+ }
+
+ n = MIN(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->dead = 1;
+ return fz_rethrow(n, "read error");
+ }
+ else if (n > 0)
+ {
+ stm->pos += n;
+ count += n;
+ }
}
return count;
@@ -44,13 +65,20 @@ fz_fillbuffer(fz_stream *stm)
assert(stm->rp == stm->wp);
- n = fz_read(stm, stm->bp, stm->ep - stm->bp);
+ if (stm->dead)
+ return;
+
+ n = stm->read(stm, stm->bp, stm->ep - stm->bp);
if (n < 0)
+ {
+ stm->dead = 1;
fz_catch(n, "read error; treating as end of file");
+ }
else if (n > 0)
{
stm->rp = stm->bp;
stm->wp = stm->bp + n;
+ stm->pos += n;
}
}