flate

deflate implementation
git clone git://git.suckless.org/flate
Log | Files | Refs | README

commit f08500976663fe63aa81e0f6bd4f421e07b8d5c1
parent e2c36cb2b784ab7ded4dcd79925bb2291058108a
Author: nsz <nszabolcs@gmail.com>
Date:   Tue,  9 Jun 2009 12:09:55 +0200

less gotos
Diffstat:
inflate.c | 44++++++++++++++------------------------------
1 file changed, 14 insertions(+), 30 deletions(-)

diff --git a/inflate.c b/inflate.c @@ -337,26 +337,12 @@ static uint decode_symbol(State *s, Huff *huff) { static int decode_block(State *s, Huff *lhuff, Huff *dhuff) { uchar *win = s->win; uint pos = s->pos; - uint sym; - uint len; - uint dist; + uint sym = s->nclen; + uint len = s->lenpos; + uint dist = s->nclen; switch (s->state) { - case DecodeBlockLenBits: - sym = s->nclen; - goto decode_lenbits; - case DecodeBlockDist: - len = s->lenpos; - goto decode_dist; - case DecodeBlockDistBits: - sym = s->nclen; - len = s->lenpos; - goto decode_distbits; - case DecodeBlockCopy: - dist = s->nclen; - len = s->lenpos; - goto decode_copy; - } + case DecodeBlock: for (;;) { sym = decode_symbol(s, lhuff); @@ -376,7 +362,7 @@ static int decode_block(State *s, Huff *lhuff, Huff *dhuff) { return FlateNeedInput; return FlateError; } -decode_lenbits: + case DecodeBlockLenBits: if (!fillbits_fast(&s->src, s->srcend, &s->bits, &s->nbits, lenbits[sym])) { s->nclen = sym; /* using nclen to store sym */ s->pos = pos; @@ -384,7 +370,7 @@ decode_lenbits: return FlateNeedInput; } len = lenbase[sym] + getbits_fast(&s->bits, &s->nbits, lenbits[sym]); -decode_dist: + case DecodeBlockDist: sym = decode_symbol(s, dhuff); if (sym == (uint)FlateNeedInput) { s->pos = pos; @@ -394,7 +380,7 @@ decode_dist: } if (sym >= Ndist) return FlateError; -decode_distbits: + case DecodeBlockDistBits: if (!fillbits_fast(&s->src, s->srcend, &s->bits, &s->nbits, distbits[sym])) { s->nclen = sym; /* using nclen to store sym */ s->pos = pos; @@ -402,7 +388,6 @@ decode_distbits: s->state = DecodeBlockDistBits; return FlateNeedInput; } - /* TODO: s/dist/sym/ */ dist = distbase[sym] + getbits_fast(&s->bits, &s->nbits, distbits[sym]); /* copy match, loop unroll in common case */ if (pos + len < WinSize) { @@ -425,14 +410,14 @@ decode_distbits: } } } else { /* rare */ -decode_copy: + case DecodeBlockCopy: while (len--) { win[pos] = win[(pos - dist) & WinMask]; pos++; if (pos == WinSize) { s->pos = pos; s->lenpos = len; - s->nclen = dist; + s->nclen = dist; /* using nclen to store dist */ s->state = DecodeBlockCopy; return FlateHasOutput; } @@ -442,7 +427,9 @@ decode_copy: s->pos = pos; return FlateOk; } - } + } /* for (;;) */ + } /* switch () */ + return FlateError; } @@ -603,10 +590,7 @@ int inflate(State *s) { case DecodeBlockDist: case DecodeBlockDistBits: case DecodeBlockCopy: - if (s->fixed) - n = decode_block(s, &lhuff, &dhuff); - else - n = decode_block(s, &s->lhuff, &s->dhuff); + n = decode_block(s, s->fixed ? &lhuff : &s->lhuff, s->fixed ? &dhuff : &s->dhuff); if (n == FlateNeedInput) return FlateNeedInput; if (n == FlateHasOutput) @@ -651,7 +635,7 @@ int inflate_callback(int (*r)(void *, int, void *), void *rdata, int (*w)(void * free(src); return FlateError; } - s.pos = 0; /* ouch */ + s.pos = 0; /* TODO: ouch */ break; case FlateOk: free(src);