flate

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

commit 4fcde3912027a729b762b199ba1f0b25209374b2
parent 966429e2a5d9e15ac6ee0a47eb715b8dfd04550f
Author: nsz <nszabolcs@gmail.com>
Date:   Tue, 28 Apr 2009 07:49:58 +0200

faster getbits
Diffstat:
inflate.c | 24+++++++++++++++++-------
1 file changed, 17 insertions(+), 7 deletions(-)

diff --git a/inflate.c b/inflate.c @@ -196,7 +196,7 @@ static void init_fixed_huffs(void) { build_huff(&dhuff, lens, Ndist, 5); } -/* get one bit from s->src stream */ +/* get one bit from stream */ static uint getbit(Stream *s) { uint bit; @@ -213,16 +213,26 @@ static uint getbit(Stream *s) { return bit; } -/* get n bits from s->src stream */ +/* get n bits from stream */ static uint getbits(Stream *s, int n) { - uint bits = 0; - int i; + uint bits; + uint nbits; if (n == 0) return 0; - for (i = 0; i < n; i++) - bits |= getbit(s) << i; - return bits; + bits = s->bits; + nbits = s->nbits; + while (nbits < n) { + if (s->src == s->srcend) { + s->error = FlateShortSrc; + return 0; + } + bits |= (*s->src++ << nbits); + nbits += 8; + } + s->bits = bits >> n; + s->nbits = nbits - n; + return bits & ((1 << n) - 1); } /* decode a symbol from stream with tree */