From 51790988f8409bd636714316fd4bccac63252d3f Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Mon, 2 Aug 2010 12:31:39 +0000 Subject: Use 'while (rp < wp)' idiom for filter output buffering state. --- fitz/filt_basic.c | 120 +++++++++++++++++++++++++++------------------------- fitz/filt_dctd.c | 50 ++++++++++------------ fitz/filt_faxd.c | 19 +++++---- fitz/filt_flate.c | 5 +-- fitz/filt_jbig2d.c | 3 +- fitz/filt_lzwd.c | 39 +++++++++-------- fitz/filt_predict.c | 21 ++++----- 7 files changed, 133 insertions(+), 124 deletions(-) (limited to 'fitz') diff --git a/fitz/filt_basic.c b/fitz/filt_basic.c index e5c54518..a0369282 100644 --- a/fitz/filt_basic.c +++ b/fitz/filt_basic.c @@ -91,11 +91,12 @@ readahxd(fz_stream *stm, unsigned char *buf, int len) { fz_ahxd *state = stm->state; unsigned char *p = buf; + unsigned char *ep = buf + len; int a, b, c, odd; odd = 0; - while (p < buf + len) + while (p < ep) { if (state->eod) return p - buf; @@ -160,8 +161,8 @@ typedef struct fz_a85d_s fz_a85d; struct fz_a85d_s { fz_stream *chain; - unsigned char buf[4]; - int remain; + unsigned char bp[4]; + unsigned char *rp, *wp; int eod; }; @@ -170,14 +171,15 @@ reada85d(fz_stream *stm, unsigned char *buf, int len) { fz_a85d *state = stm->state; unsigned char *p = buf; + unsigned char *ep = buf + len; int count = 0; int word = 0; int c; - while (state->remain > 0 && p < buf + len) - *p++ = state->buf[4 - state->remain--]; + while (state->rp < state->wp && p < ep) + *p++ = *state->rp++; - while (p < buf + len) + while (p < ep) { if (state->eod) return p - buf; @@ -192,11 +194,12 @@ reada85d(fz_stream *stm, unsigned char *buf, int len) { word = word * 85 + (c - '!'); - state->buf[0] = (word >> 24) & 0xff; - state->buf[1] = (word >> 16) & 0xff; - state->buf[2] = (word >> 8) & 0xff; - state->buf[3] = (word) & 0xff; - state->remain = 4; + state->bp[0] = (word >> 24) & 0xff; + state->bp[1] = (word >> 16) & 0xff; + state->bp[2] = (word >> 8) & 0xff; + state->bp[3] = (word) & 0xff; + state->rp = state->bp; + state->wp = state->bp + 4; word = 0; count = 0; @@ -210,11 +213,12 @@ reada85d(fz_stream *stm, unsigned char *buf, int len) else if (c == 'z' && count == 0) { - state->buf[0] = 0; - state->buf[1] = 0; - state->buf[2] = 0; - state->buf[3] = 0; - state->remain = 4; + state->bp[0] = 0; + state->bp[1] = 0; + state->bp[2] = 0; + state->bp[3] = 0; + state->rp = state->bp; + state->wp = state->bp + 4; } else if (c == '~') @@ -230,21 +234,24 @@ reada85d(fz_stream *stm, unsigned char *buf, int len) return fz_throw("partial final byte in a85d"); case 2: word = word * (85 * 85 * 85) + 0xffffff; - state->buf[3] = word >> 24; - state->remain = 1; + state->bp[0] = word >> 24; + state->rp = state->bp; + state->wp = state->bp + 1; break; case 3: word = word * (85 * 85) + 0xffff; - state->buf[2] = word >> 24; - state->buf[3] = word >> 16; - state->remain = 2; + state->bp[0] = word >> 24; + state->bp[1] = word >> 16; + state->rp = state->bp; + state->wp = state->bp + 2; break; case 4: word = word * 85 + 0xff; - state->buf[1] = word >> 24; - state->buf[2] = word >> 16; - state->buf[3] = word >> 8; - state->remain = 3; + state->bp[0] = word >> 24; + state->bp[1] = word >> 16; + state->bp[2] = word >> 8; + state->rp = state->bp; + state->wp = state->bp + 3; break; } state->eod = 1; @@ -255,8 +262,8 @@ reada85d(fz_stream *stm, unsigned char *buf, int len) return fz_throw("bad data in a85d: '%c'", c); } - while (state->remain > 0 && p < buf + len) - *p++ = state->buf[4 - state->remain--]; + while (state->rp < state->wp && p < ep) + *p++ = *state->rp++; } return p - buf; @@ -277,7 +284,8 @@ fz_opena85d(fz_stream *chain) state = fz_malloc(sizeof(fz_a85d)); state->chain = chain; - state->remain = 0; + state->rp = state->bp; + state->wp = state->bp; state->eod = 0; return fz_newstream(state, reada85d, closea85d); @@ -298,8 +306,9 @@ readrld(fz_stream *stm, unsigned char *buf, int len) { fz_rld *state = stm->state; unsigned char *p = buf; + unsigned char *ep = buf + len; - while (p < buf + len) + while (p < ep) { if (state->run == 128) return p - buf; @@ -322,7 +331,7 @@ readrld(fz_stream *stm, unsigned char *buf, int len) if (state->run < 128) { - while (p < buf + len && state->n--) + while (p < ep && state->n--) { int c = fz_readbyte(state->chain); if (c < 0) @@ -333,7 +342,7 @@ readrld(fz_stream *stm, unsigned char *buf, int len) if (state->run > 128) { - while (p < buf + len && state->n--) + while (p < ep && state->n--) *p++ = state->c; } } @@ -417,8 +426,8 @@ struct fz_aesd_s fz_aes aes; unsigned char iv[16]; int ivcount; - unsigned char buf[16]; - int remain; + unsigned char bp[16]; + unsigned char *rp, *wp; }; static int @@ -426,48 +435,44 @@ readaesd(fz_stream *stm, unsigned char *buf, int len) { fz_aesd *state = stm->state; unsigned char *p = buf; + unsigned char *ep = buf + len; while (state->ivcount < 16) { int c = fz_readbyte(state->chain); if (c < 0) - return fz_throw("premature end in AES filter"); + return fz_throw("premature end in aes filter"); state->iv[state->ivcount++] = c; } - while (state->remain > 0 && p < buf + len) - *p++ = state->buf[16 - state->remain--]; + while (state->rp < state->wp && p < ep) + *p++ = *state->rp++; - while (p < buf + len) + while (p < ep) { - while (state->remain < 16) - { - int c = fz_readbyte(state->chain); - if (c < 0) - { - if (state->remain > 0) - return fz_throw("premature end in AES filter"); - return p - buf; - } - state->buf[state->remain++] = c; - } + int n = fz_read(state->chain, state->bp, 16); + if (n < 0) + return fz_rethrow(n, "read error in aes filter"); + else if (n == 0) + return p - buf; + else if (n < 16) + return fz_throw("partial block in aes filter"); - aes_crypt_cbc(&state->aes, AES_DECRYPT, 16, state->iv, state->buf, state->buf); + aes_crypt_cbc(&state->aes, AES_DECRYPT, 16, state->iv, state->bp, state->bp); + state->rp = state->bp; + state->wp = state->bp + 16; /* strip padding at end of file */ if (fz_peekbyte(state->chain) == EOF) { - int pad = state->buf[15]; + int pad = state->bp[15]; if (pad < 1 || pad > 16) return fz_throw("aes padding out of range: %d", pad); - state->remain -= pad; - memmove(&state->buf[16 - state->remain], - &state->buf[0], - state->remain); + state->wp -= pad; } - while (state->remain > 0 && p < buf + len) - *p++ = state->buf[16 - state->remain--]; + while (state->rp < state->wp && p < ep) + *p++ = *state->rp++; } return p - buf; @@ -490,7 +495,8 @@ fz_openaesd(fz_stream *chain, unsigned char *key, unsigned keylen) state->chain = chain; aes_setkey_dec(&state->aes, key, keylen * 8); state->ivcount = 0; - state->remain = 0; + state->rp = state->bp; + state->wp = state->bp; return fz_newstream(state, readaesd, closeaesd); } diff --git a/fitz/filt_dctd.c b/fitz/filt_dctd.c index daa66902..9c2b3fe4 100644 --- a/fitz/filt_dctd.c +++ b/fitz/filt_dctd.c @@ -9,10 +9,10 @@ struct fz_dctd_s { fz_stream *chain; int colortransform; - int init, done; - unsigned char *scanline; + int init; int stride; - int remain; + unsigned char *scanline; + unsigned char *rp, *wp; struct jpeg_decompress_struct cinfo; struct jpeg_source_mgr srcmgr; struct jpeg_error_mgr errmgr; @@ -24,6 +24,8 @@ static void error_exit(j_common_ptr cinfo) { fz_dctd *state = cinfo->client_data; cinfo->err->format_message(cinfo, state->msg); + if (state->cinfo.src) + state->chain->rp = state->chain->wp - state->cinfo.src->bytes_in_buffer; longjmp(state->jb, 1); } @@ -82,12 +84,10 @@ readdctd(fz_stream *stm, unsigned char *buf, int len) fz_dctd *state = stm->state; j_decompress_ptr cinfo = &state->cinfo; unsigned char *p = buf; + unsigned char *ep = buf + len; if (setjmp(state->jb)) - { - state->done = 1; return fz_throw("jpeg error: %s", state->msg); - } if (!state->init) { @@ -144,27 +144,22 @@ readdctd(fz_stream *stm, unsigned char *buf, int len) jpeg_start_decompress(cinfo); state->stride = cinfo->output_width * cinfo->output_components; - state->remain = 0; state->scanline = fz_malloc(state->stride); + state->rp = state->scanline; + state->wp = state->scanline; state->init = 1; } - while (state->remain > 0 && p < buf + len) - *p++ = state->scanline[state->stride - state->remain--]; + while (state->rp < state->wp && p < ep) + *p++ = *state->rp++; - while (p < buf + len) + while (p < ep) { - if (state->done) - return p - buf; - if (cinfo->output_scanline == cinfo->output_height) - { - state->done = 1; - return p - buf; - } + break; - if (p + state->stride <= buf + len) + if (p + state->stride <= ep) { jpeg_read_scanlines(cinfo, &p, 1); p += state->stride; @@ -172,13 +167,15 @@ readdctd(fz_stream *stm, unsigned char *buf, int len) else { jpeg_read_scanlines(cinfo, &state->scanline, 1); - state->remain = state->stride; + state->rp = state->scanline; + state->wp = state->scanline + state->stride; } - while (state->remain > 0 && p < buf + len) - *p++ = state->scanline[state->stride - state->remain--]; + while (state->rp < state->wp && p < ep) + *p++ = *state->rp++; } + state->chain->rp = state->chain->wp - cinfo->src->bytes_in_buffer; return p - buf; } @@ -186,9 +183,12 @@ static void closedctd(fz_stream *stm) { fz_dctd *state = stm->state; - jpeg_finish_decompress(&state->cinfo); + if (state->init) + { + jpeg_finish_decompress(&state->cinfo); + state->chain->rp = state->chain->wp - state->cinfo.src->bytes_in_buffer; + } jpeg_destroy_decompress(&state->cinfo); - state->chain->rp = state->chain->wp - state->cinfo.src->bytes_in_buffer; fz_free(state->scanline); fz_close(state->chain); fz_free(state); @@ -204,10 +204,6 @@ fz_opendctd(fz_stream *chain, fz_obj *params) state->chain = chain; state->colortransform = -1; /* unset */ state->init = 0; - state->done = 0; - state->scanline = nil; - state->stride = 0; - state->remain = 0; obj = fz_dictgets(params, "ColorTransform"); if (obj) diff --git a/fitz/filt_faxd.c b/fitz/filt_faxd.c index e925a612..66410660 100644 --- a/fitz/filt_faxd.c +++ b/fitz/filt_faxd.c @@ -329,7 +329,7 @@ struct fz_faxd_s int a, c, dim, eolc; unsigned char *ref; unsigned char *dst; - int remain; + unsigned char *rp, *wp; }; static inline void @@ -546,6 +546,7 @@ readfaxd(fz_stream *stm, unsigned char *buf, int len) { fz_faxd *fax = stm->state; unsigned char *p = buf; + unsigned char *ep = buf + len; unsigned char *tmp; fz_error error; int i; @@ -634,19 +635,20 @@ eol: if (fax->blackis1) { - while (fax->remain > 0 && p < buf + len) - *p++ = fax->dst[fax->stride - fax->remain--]; + while (fax->rp < fax->wp && p < ep) + *p++ = *fax->rp++; } else { - while (fax->remain > 0 && p < buf + len) - *p++ = fax->dst[fax->stride - fax->remain--] ^ 0xff; + while (fax->rp < fax->wp && p < ep) + *p++ = *fax->rp++ ^ 0xff; } - if (fax->remain > 0) + if (fax->rp < fax->wp) return p - buf; - fax->remain = fax->stride; + fax->rp = fax->dst; + fax->wp = fax->dst + fax->stride; tmp = fax->ref; fax->ref = fax->dst; @@ -759,7 +761,8 @@ fz_openfaxd(fz_stream *chain, fz_obj *params) fax->ref = fz_malloc(fax->stride); fax->dst = fz_malloc(fax->stride); - fax->remain = fax->stride; + fax->rp = fax->dst; + fax->wp = fax->dst + fax->stride; memset(fax->ref, 0, fax->stride); memset(fax->dst, 0, fax->stride); diff --git a/fitz/filt_flate.c b/fitz/filt_flate.c index 87b1c907..1e22d02b 100644 --- a/fitz/filt_flate.c +++ b/fitz/filt_flate.c @@ -26,14 +26,13 @@ readflated(fz_stream *stm, unsigned char *outbuf, int outlen) fz_flate *state = stm->state; fz_stream *chain = state->chain; z_streamp zp = &state->z; - int inlen, code; + int code; if (chain->rp == chain->wp) fz_fillbuffer(chain); - inlen = chain->wp - chain->rp; zp->next_in = chain->rp; - zp->avail_in = inlen; + zp->avail_in = chain->wp - chain->rp; zp->next_out = outbuf; zp->avail_out = outlen; diff --git a/fitz/filt_jbig2d.c b/fitz/filt_jbig2d.c index 37d976ea..8804ef29 100644 --- a/fitz/filt_jbig2d.c +++ b/fitz/filt_jbig2d.c @@ -47,6 +47,7 @@ readjbig2d(fz_stream *stm, unsigned char *buf, int len) fz_jbig2d *state = stm->state; unsigned char tmp[4096]; unsigned char *p = buf; + unsigned char *ep = buf + len; unsigned char *s; int x, w, n; @@ -72,7 +73,7 @@ readjbig2d(fz_stream *stm, unsigned char *buf, int len) s = state->page->data; w = state->page->height * state->page->stride; x = state->idx; - while (p < buf + len && x < w) + while (p < ep && x < w) *p++ = s[x++] ^ 0xff; state->idx = x; diff --git a/fitz/filt_lzwd.c b/fitz/filt_lzwd.c index 5ec8093f..e55a8c28 100644 --- a/fitz/filt_lzwd.c +++ b/fitz/filt_lzwd.c @@ -41,9 +41,8 @@ struct fz_lzwd_s int nextcode; /* next free entry */ lzw_code table[NUMCODES]; - unsigned char output[MAXLENGTH]; - int outlen; - int remain; + unsigned char bp[MAXLENGTH]; + unsigned char *rp, *wp; }; static inline void eatbits(fz_lzwd *lzw, int nbits) @@ -77,12 +76,14 @@ readlzwd(fz_stream *stm, unsigned char *buf, int len) { fz_lzwd *lzw = stm->state; unsigned char *p = buf; + unsigned char *ep = buf + len; unsigned char *s; + int codelen; - while (lzw->remain > 0 && p < buf + len) - *p++ = lzw->output[lzw->outlen - lzw->remain--]; + while (lzw->rp < lzw->wp && p < ep) + *p++ = *lzw->rp++; - while (p < buf + len) + while (p < ep) { if (lzw->eod) return 0; @@ -150,28 +151,30 @@ output: /* code maps to a string, copy to output (in reverse...) */ if (lzw->code > 255) { - assert(lzw->table[lzw->code].length < MAXLENGTH); + codelen = lzw->table[lzw->code].length; + lzw->rp = lzw->bp; + lzw->wp = lzw->bp + codelen; - lzw->outlen = lzw->table[lzw->code].length; - lzw->remain = lzw->outlen; - s = lzw->output + lzw->remain; + assert(codelen < MAXLENGTH); + + s = lzw->wp; do { *(--s) = lzw->table[lzw->code].value; lzw->code = lzw->table[lzw->code].prev; - } while (lzw->code >= 0 && s > lzw->output); + } while (lzw->code >= 0 && s > lzw->bp); } /* ... or just a single character */ else { - lzw->output[0] = lzw->code; - lzw->outlen = 1; - lzw->remain = 1; + lzw->bp[0] = lzw->code; + lzw->rp = lzw->bp; + lzw->wp = lzw->bp + 1; } /* copy to output */ - while (lzw->remain > 0 && p < buf + len) - *p++ = lzw->output[lzw->outlen - lzw->remain--]; + while (lzw->rp < lzw->wp && p < ep) + *p++ = *lzw->rp++; } return p - buf; @@ -224,8 +227,8 @@ fz_openlzwd(fz_stream *chain, fz_obj *params) lzw->code = -1; lzw->nextcode = LZW_FIRST; lzw->oldcode = -1; - lzw->remain = 0; - lzw->outlen = 0; + lzw->rp = lzw->bp; + lzw->wp = lzw->bp; return fz_newstream(lzw, readlzwd, closelzwd); } diff --git a/fitz/filt_predict.c b/fitz/filt_predict.c index b0ef8d3c..ae2d1aaf 100644 --- a/fitz/filt_predict.c +++ b/fitz/filt_predict.c @@ -20,7 +20,7 @@ struct fz_predict_s unsigned char *in; unsigned char *out; unsigned char *ref; - int outlen, outidx; + unsigned char *rp, *wp; }; static inline int @@ -145,13 +145,14 @@ readpredict(fz_stream *stm, unsigned char *buf, int len) { fz_predict *state = stm->state; unsigned char *p = buf; + unsigned char *ep = buf + len; int ispng = state->predictor >= 10; int n; - while (state->outidx < state->outlen && p < buf + len) - *p++ = state->out[state->outidx++]; + while (state->rp < state->wp && p < ep) + *p++ = *state->rp++; - while (p < buf + len) + while (p < ep) { n = fz_read(state->chain, state->in, state->stride + ispng); if (n < 0) @@ -169,11 +170,11 @@ readpredict(fz_stream *stm, unsigned char *buf, int len) memcpy(state->ref, state->out, state->stride); } - state->outlen = n - ispng; - state->outidx = 0; + state->rp = state->out; + state->wp = state->out + n - ispng; - while (state->outidx < state->outlen && p < buf + len) - *p++ = state->out[state->outidx++]; + while (state->rp < state->wp && p < ep) + *p++ = *state->rp++; } return p - buf; @@ -235,8 +236,8 @@ fz_openpredict(fz_stream *chain, fz_obj *params) state->in = fz_malloc(state->stride + 1); state->out = fz_malloc(state->stride); state->ref = fz_malloc(state->stride); - state->outlen = 0; - state->outidx = 0; + state->rp = state->out; + state->wp = state->out; memset(state->ref, 0, state->stride); -- cgit v1.2.3