summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTor Andersson <tor@ghostscript.com>2011-03-06 13:30:01 +0000
committerTor Andersson <tor@ghostscript.com>2011-03-06 13:30:01 +0000
commit2d919ffba311dde73dc957f325ee55c7ce4f66fa (patch)
tree4bc02aa664c92a1002cec047a7ccece22eee77e2
parent9dee36047ab01f929582fa7b715af392f2de7d24 (diff)
downloadmupdf-2d919ffba311dde73dc957f325ee55c7ce4f66fa.tar.xz
Add explicit EOF testing functions.
-rw-r--r--fitz/filt_basic.c2
-rw-r--r--fitz/filt_lzwd.c2
-rw-r--r--fitz/fitz.h24
-rw-r--r--fitz/stm_open.c8
-rw-r--r--fitz/stm_read.c24
-rw-r--r--mupdf/pdf_function.c2
-rw-r--r--mupdf/pdf_shade.c8
-rw-r--r--mupdf/pdf_xref.c2
8 files changed, 54 insertions, 18 deletions
diff --git a/fitz/filt_basic.c b/fitz/filt_basic.c
index 14eefb19..91af02d4 100644
--- a/fitz/filt_basic.c
+++ b/fitz/filt_basic.c
@@ -468,7 +468,7 @@ readaesd(fz_stream *stm, unsigned char *buf, int len)
state->wp = state->bp + 16;
/* strip padding at end of file */
- if (fz_peekbyte(state->chain) == EOF)
+ if (fz_iseof(state->chain))
{
int pad = state->bp[15];
if (pad < 1 || pad > 16)
diff --git a/fitz/filt_lzwd.c b/fitz/filt_lzwd.c
index 5d2bd663..23cb7520 100644
--- a/fitz/filt_lzwd.c
+++ b/fitz/filt_lzwd.c
@@ -68,7 +68,7 @@ readlzwd(fz_stream *stm, unsigned char *buf, int len)
code = fz_readbits(lzw->chain, codebits);
- if (fz_peekbyte(lzw->chain) == EOF)
+ if (fz_iseofbits(lzw->chain))
{
lzw->eod = 1;
break;
diff --git a/fitz/fitz.h b/fitz/fitz.h
index bea851fa..13e285e6 100644
--- a/fitz/fitz.h
+++ b/fitz/fitz.h
@@ -497,7 +497,8 @@ typedef struct fz_stream_s fz_stream;
struct fz_stream_s
{
int refs;
- int dead;
+ int error;
+ int eof;
int pos;
int avail;
int bits;
@@ -550,6 +551,17 @@ static inline void fz_unreadbyte(fz_stream *stm)
stm->rp--;
}
+static inline int fz_iseof(fz_stream *stm)
+{
+ if (stm->rp == stm->wp)
+ {
+ if (stm->eof)
+ return 1;
+ return fz_peekbyte(stm) == EOF;
+ }
+ return 0;
+}
+
static inline unsigned int fz_readbits(fz_stream *stm, int n)
{
unsigned int x;
@@ -582,6 +594,16 @@ static inline unsigned int fz_readbits(fz_stream *stm, int n)
return x;
}
+static inline void fz_syncbits(fz_stream *stm)
+{
+ stm->avail = 0;
+}
+
+static inline int fz_iseofbits(fz_stream *stm)
+{
+ return fz_iseof(stm) && (stm->avail == 0 || stm->bits == EOF);
+}
+
/*
* Data filters.
*/
diff --git a/fitz/stm_open.c b/fitz/stm_open.c
index b81d9176..6f1160d8 100644
--- a/fitz/stm_open.c
+++ b/fitz/stm_open.c
@@ -10,7 +10,8 @@ fz_newstream(void *state,
stm = fz_malloc(sizeof(fz_stream));
stm->refs = 1;
- stm->dead = 0;
+ stm->error = 0;
+ stm->eof = 0;
stm->pos = 0;
stm->bits = 0;
@@ -105,8 +106,9 @@ static void seekbuffer(fz_stream *stm, int offset, int whence)
if (whence == 1)
stm->rp += offset;
if (whence == 2)
- stm->rp = stm->wp - offset;
- stm->rp = CLAMP(stm->rp, stm->bp, stm->wp);
+ stm->rp = stm->ep - offset;
+ stm->rp = CLAMP(stm->rp, stm->bp, stm->ep);
+ stm->wp = stm->ep;
}
static void closebuffer(fz_stream *stm)
diff --git a/fitz/stm_read.c b/fitz/stm_read.c
index af5afab6..e5ead914 100644
--- a/fitz/stm_read.c
+++ b/fitz/stm_read.c
@@ -12,7 +12,7 @@ fz_read(fz_stream *stm, unsigned char *buf, int len)
stm->rp += count;
}
- if (count == len || stm->dead)
+ if (count == len || stm->error || stm->eof)
return count;
assert(stm->rp == stm->wp);
@@ -22,9 +22,13 @@ fz_read(fz_stream *stm, unsigned char *buf, int len)
n = stm->read(stm, stm->bp, stm->ep - stm->bp);
if (n < 0)
{
- stm->dead = 1;
+ stm->error = 1;
return fz_rethrow(n, "read error");
}
+ else if (n == 0)
+ {
+ stm->eof = 1;
+ }
else if (n > 0)
{
stm->rp = stm->bp;
@@ -45,9 +49,13 @@ fz_read(fz_stream *stm, unsigned char *buf, int len)
n = stm->read(stm, buf + count, len - count);
if (n < 0)
{
- stm->dead = 1;
+ stm->error = 1;
return fz_rethrow(n, "read error");
}
+ else if (n == 0)
+ {
+ stm->eof = 1;
+ }
else if (n > 0)
{
stm->pos += n;
@@ -65,15 +73,19 @@ fz_fillbuffer(fz_stream *stm)
assert(stm->rp == stm->wp);
- if (stm->dead)
+ if (stm->error || stm->eof)
return;
n = stm->read(stm, stm->bp, stm->ep - stm->bp);
if (n < 0)
{
- stm->dead = 1;
+ stm->error = 1;
fz_catch(n, "read error; treating as end of file");
}
+ else if (n == 0)
+ {
+ stm->eof = 1;
+ }
else if (n > 0)
{
stm->rp = stm->bp;
@@ -161,8 +173,8 @@ fz_seek(fz_stream *stm, int offset, int whence)
offset = fz_tell(stm) + offset;
whence = 0;
}
-
stm->seek(stm, offset, whence);
+ stm->eof = 0;
}
else if (whence != 2)
{
diff --git a/mupdf/pdf_function.c b/mupdf/pdf_function.c
index 97b9e2e0..228d1dfc 100644
--- a/mupdf/pdf_function.c
+++ b/mupdf/pdf_function.c
@@ -1058,7 +1058,7 @@ loadsamplefunc(pdf_function *func, pdf_xref *xref, fz_obj *dict, int num, int ge
unsigned int x;
float s;
- if (fz_peekbyte(stream) == EOF)
+ if (fz_iseofbits(stream))
{
fz_close(stream);
return fz_throw("truncated sample stream");
diff --git a/mupdf/pdf_shade.c b/mupdf/pdf_shade.c
index 0fda414e..8fd0eb78 100644
--- a/mupdf/pdf_shade.c
+++ b/mupdf/pdf_shade.c
@@ -670,7 +670,7 @@ pdf_loadtype4shade(fz_shade *shade, pdf_xref *xref, fz_obj *dict,
else
ncomp = shade->cs->n;
- while (fz_peekbyte(stream) != EOF)
+ while (!fz_iseofbits(stream))
{
flag = fz_readbits(stream, p.bpflag);
vd.x = readsample(stream, p.bpcoord, p.x0, p.x1);
@@ -745,7 +745,7 @@ pdf_loadtype5shade(fz_shade *shade, pdf_xref *xref, fz_obj *dict,
buf = fz_calloc(p.vprow, sizeof(struct vertex));
first = 1;
- while (fz_peekbyte(stream) != EOF)
+ while (!fz_iseofbits(stream))
{
for (i = 0; i < p.vprow; i++)
{
@@ -800,7 +800,7 @@ pdf_loadtype6shade(fz_shade *shade, pdf_xref *xref, fz_obj *dict,
hasprevpatch = 0;
- while (fz_peekbyte(stream) != EOF)
+ while (!fz_iseofbits(stream))
{
float c[4][FZ_MAXCOLORS];
fz_point v[12];
@@ -925,7 +925,7 @@ pdf_loadtype7shade(fz_shade *shade, pdf_xref *xref, fz_obj *dict,
hasprevpatch = 0;
- while (fz_peekbyte(stream) != EOF)
+ while (!fz_iseofbits(stream))
{
float c[4][FZ_MAXCOLORS];
fz_point v[16];
diff --git a/mupdf/pdf_xref.c b/mupdf/pdf_xref.c
index 799b2f18..7174cfc5 100644
--- a/mupdf/pdf_xref.c
+++ b/mupdf/pdf_xref.c
@@ -288,7 +288,7 @@ pdf_readnewxrefsection(pdf_xref *xref, fz_stream *stm, int i0, int i1, int w0, i
int b = 0;
int c = 0;
- if (fz_peekbyte(stm) == EOF)
+ if (fz_iseof(stm))
return fz_throw("truncated xref stream");
for (n = 0; n < w0; n++)