diff options
author | Tor Andersson <tor@ghostscript.com> | 2011-03-02 02:38:16 +0000 |
---|---|---|
committer | Tor Andersson <tor@ghostscript.com> | 2011-03-02 02:38:16 +0000 |
commit | dae1e4df9b6cdd4c04e9aecd13d7064ddbec88f1 (patch) | |
tree | 923fb555d6bc3368d15ea1dd96a03a95bb0c4526 | |
parent | 5e59b61da17cf29ff3359c4806457aaf1e910439 (diff) | |
download | mupdf-dae1e4df9b6cdd4c04e9aecd13d7064ddbec88f1.tar.xz |
Use buffering for small reads.
-rw-r--r-- | fitz/stm_read.c | 60 | ||||
-rw-r--r-- | mupdf/pdf_colorspace.c | 1 |
2 files changed, 44 insertions, 17 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; } } diff --git a/mupdf/pdf_colorspace.c b/mupdf/pdf_colorspace.c index 28774fe1..50d93a69 100644 --- a/mupdf/pdf_colorspace.c +++ b/mupdf/pdf_colorspace.c @@ -281,7 +281,6 @@ loadindexed(fz_colorspace **csp, pdf_xref *xref, fz_obj *array) pdf_logrsrc("stream lookup\n"); - /* TODO: openstream, read, close instead */ error = pdf_openstream(&file, xref, fz_tonum(lookup), fz_togen(lookup)); if (error) { |