[gs-commits] mupdf/master - 0_8-2-g36c99dd - Add explicit EOF testing functions.

Tor Andersson tor at ghostscript.com
Sun Mar 6 17:10:38 UTC 2011


commit 36c99dd8e92abfdb56e9ddcce3f9a8fe24d9ebfc
Author: Tor Andersson <tor at ghostscript.com>
Date:   Sun Mar 6 13:30:01 2011 +0000

    Add explicit EOF testing functions.
    
    Ignore-this: 8ab57caacf332d7f95cae0e5b0a5d763
    
    darcs-hash:20110306133001-f546f-08565abb93ad219b43afec623b77d94b07aa4bd2.gz

 8 files changed, 54 insertions(+), 18 deletions(-)

diff --git a/fitz/filt_basic.c b/fitz/filt_basic.c
index 14eefb1..91af02d 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 5d2bd66..23cb752 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 bea851f..13e285e 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 b81d917..6f1160d 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 af5afab..e5ead91 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 97b9e2e..228d1df 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 0fda414..8fd0eb7 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 799b2f1..7174cfc 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++)

--
git/hooks/post-receive


More information about the gs-commits mailing list