flate

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

commit e2c36cb2b784ab7ded4dcd79925bb2291058108a
parent 8fba42f347be7bb505324c2d57847807a7ba26a3
Author: nsz <nszabolcs@gmail.com>
Date:   Tue,  9 Jun 2009 11:41:36 +0200

cleanup
Diffstat:
inflate.c | 45+++++++++++++++++++--------------------------
1 file changed, 19 insertions(+), 26 deletions(-)

diff --git a/inflate.c b/inflate.c @@ -37,6 +37,7 @@ enum { DynamicHuff, DynamicHuffClen, DynamicHuffLitlenDist, + DynamicHuffContinue, DecodeBlock, DecodeBlockLenBits, DecodeBlockDist, @@ -423,7 +424,7 @@ decode_distbits: pos++; } } - } else { + } else { /* rare */ decode_copy: while (len--) { win[pos] = win[(pos - dist) & WinMask]; @@ -472,12 +473,14 @@ int inflate(State *s) { case BlockHead: if (s->final) { if (s->pos) - goto hasoutput; - else - goto finish; + return FlateHasOutput; + else { + free(s->win); + return FlateOk; + } } if (!fillbits(s, 3)) - goto needinput; + return FlateNeedInput; s->final = getbits(s, 1); n = getbits(s, 2); if (n == 0) @@ -494,7 +497,7 @@ int inflate(State *s) { s->bits >>= s->nbits & 7; s->nbits &= ~7; if (!fillbits(s, 32)) - goto needinput; + return FlateNeedInput; s->lenpos = getbits(s, 16); n = getbits(s, 16); if (s->lenpos != (~n & 0xffff)) @@ -505,11 +508,11 @@ int inflate(State *s) { /* s->nbits should be 0 here */ while (s->lenpos) { if (s->src == s->srcend) - goto needinput; + return FlateNeedInput; s->lenpos--; s->win[s->pos++] = *s->src++; if (s->pos == WinSize) - goto hasoutput; + return FlateHasOutput; } s->state = BlockHead; break; @@ -522,7 +525,7 @@ int inflate(State *s) { case DynamicHuff: /* decode dynamic huffman code trees */ if (!fillbits(s, 14)) - goto needinput; + return FlateNeedInput; s->nlit = 257 + getbits(s, 5); s->ndist = 1 + getbits(s, 5); s->nclen = 4 + getbits(s, 4); @@ -540,17 +543,14 @@ int inflate(State *s) { s->lens[clenorder[n]] = getbits(s, 3); } else { s->lenpos = n; - goto needinput; + return FlateNeedInput; } /* using lhuff for code length huff code */ if (build_huff(&s->lhuff, s->lens, Nclen, ClenTableBits) < 0) goto error; s->state = DynamicHuffLitlenDist; s->lenpos = 0; - s->nclen = -1; /* decoded symbol is stored in clen or -1 */ case DynamicHuffLitlenDist: - if (s->nclen >= 0) - goto dynhuff_continue; /* decode code lengths for the dynamic trees */ for (n = s->lenpos; n < s->nlit + s->ndist; ) { uint sym = decode_symbol(s, &s->lhuff); @@ -561,10 +561,9 @@ int inflate(State *s) { s->lens[n++] = sym; continue; } else if (sym == (uint)FlateNeedInput) { - s->nclen = -1; s->lenpos = n; - goto needinput; -dynhuff_continue: + return FlateNeedInput; + case DynamicHuffContinue: n = s->lenpos; sym = s->nclen; } @@ -574,7 +573,8 @@ dynhuff_continue: goto error; s->nclen = sym; s->lenpos = n; - goto needinput; + s->state = DynamicHuffContinue; + return FlateNeedInput; } if (sym == 16) { /* copy previous code length 3-6 times */ @@ -608,9 +608,9 @@ dynhuff_continue: else n = decode_block(s, &s->lhuff, &s->dhuff); if (n == FlateNeedInput) - goto needinput; + return FlateNeedInput; if (n == FlateHasOutput) - goto hasoutput; + return FlateHasOutput; if (n == FlateError) goto error; s->state = BlockHead; @@ -619,16 +619,9 @@ dynhuff_continue: goto error; } } -needinput: - return FlateNeedInput; -hasoutput: - return FlateHasOutput; error: free(s->win); return FlateError; -finish: - free(s->win); - return FlateOk; } int inflate_callback(int (*r)(void *, int, void *), void *rdata, int (*w)(void *, int, void *), void *wdata) {