flate

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

commit 0a6feb60f9b8d1f591f15692fee108f043ed2fdf
parent 3c040f8374e0155983ffe86633b6050a16b13499
Author: nsz <nszabolcs@gmail.com>
Date:   Tue, 12 May 2009 00:59:26 +0200

fix
Diffstat:
inflate.c | 30++++++++++++++----------------
1 file changed, 14 insertions(+), 16 deletions(-)

diff --git a/inflate.c b/inflate.c @@ -17,7 +17,8 @@ enum { Ndist = 30, /* number of distance codes */ Nclen = 19, /* number of code length codes */ SrcSize = 1 << 12, /* input buffer size */ - WinSize = 1 << 15 /* output window size */ + WinSize = 1 << 15, /* output window size */ + WinMask = WinSize - 1 /* window pos (index) mask */ }; enum { @@ -213,11 +214,11 @@ static int checksrc(Stream *s) { /* check window position before writing to it */ static int checkpos(Stream *s) { if (s->pos == WinSize) { + s->pos = 0; if (s->w(s->win, WinSize, s->wdata) != WinSize) { s->error = FlateOutError; return 0; } - s->pos = 0; } return 1; } @@ -389,7 +390,7 @@ static void decode_block(Stream *s, Huff *lhuff, Huff *dhuff) { if (!checkpos(s)) return; } else { - uint i, len, dist; + uint len, dist; sym -= 257; if (sym >= Nlen) { @@ -407,32 +408,31 @@ static void decode_block(Stream *s, Huff *lhuff, Huff *dhuff) { dist = distbase[sym] + getbits(s, distbits[sym]); if (s->error != FlateOk) return; - /* copy match, loop unroll for common case */ + /* copy match, loop unroll in common case */ if (s->pos + len <= WinSize) { uint pos = s->pos; - while (len > 4) { - s->win[pos] = s->win[(pos - dist) & 0x7ff]; + while (len >= 4) { + s->win[pos] = s->win[(pos - dist) & WinMask]; pos++; - s->win[pos] = s->win[(pos - dist) & 0x7ff]; + s->win[pos] = s->win[(pos - dist) & WinMask]; pos++; - s->win[pos] = s->win[(pos - dist) & 0x7ff]; + s->win[pos] = s->win[(pos - dist) & WinMask]; pos++; - s->win[pos] = s->win[(pos - dist) & 0x7ff]; + s->win[pos] = s->win[(pos - dist) & WinMask]; pos++; len -= 4; } - while (len > 0) { - s->win[pos] = s->win[(pos - dist) & 0x7ff]; + while (len--) { + s->win[pos] = s->win[(pos - dist) & WinMask]; pos++; - len--; } s->pos = pos; if (!checkpos(s)) return; } else { - for (i = 0; i < len; i++) { - s->win[s->pos] = s->win[(s->pos - dist) & 0x7ff]; + while (len--) { + s->win[s->pos] = s->win[(s->pos - dist) & WinMask]; s->pos++; if (!checkpos(s)) return; @@ -542,7 +542,6 @@ int r(void *p, int siz, void *data) { struct data *d = data; n = fread(p, 1, siz, d->f); -/*fprintf(stderr, "r: %u\n", n);*/ d->n += n; return n; } @@ -552,7 +551,6 @@ int w(void *p, int siz, void *data) { struct data *d = data; n = fwrite(p, 1, siz, d->f); -/*fprintf(stderr, "w: %u\n", n);*/ d->n += n; return n; }