diff options
Diffstat (limited to 'fitz')
-rw-r--r-- | fitz/filt_basic.c | 2 | ||||
-rw-r--r-- | fitz/filt_lzwd.c | 2 | ||||
-rw-r--r-- | fitz/fitz.h | 24 | ||||
-rw-r--r-- | fitz/stm_open.c | 8 | ||||
-rw-r--r-- | fitz/stm_read.c | 24 |
5 files changed, 48 insertions, 12 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) { |