flate

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

commit 6c48e7f91f57a70b1b4ff6ee6c6aeb4f71bcfd14
parent 65e244b895a0227983d92b1bf8ed98795fa67845
Author: nsz@tpx <unknown>
Date:   Thu,  6 Aug 2009 09:42:43 +0200

deflate cleaning
Diffstat:
deflate.c | 78++++++++++++++++++++++--------------------------------------------------------
1 file changed, 22 insertions(+), 56 deletions(-)

diff --git a/deflate.c b/deflate.c @@ -22,6 +22,7 @@ TODO: alloc bufs on heap? overlap freq/code, rbuf/dstwin ? fix match (loopunroll near end) + dstsize spec case tests: empty block @@ -53,12 +54,6 @@ enum { Nclen = 19 /* number of code length codes */ }; -/* state */ -enum { - FillWin, - Block -}; - typedef struct { ushort dist; ushort len; @@ -69,18 +64,17 @@ typedef struct { ushort bits; } Runlen; -/* TODO: alloc buffers on heap */ typedef struct { int state; int pos; int avail; int blockstart; int lastblock; - Match m; + Match prevm; uchar *src; /* input (block start) */ uchar *srcend; uchar *dst; /* compressed output */ - uchar *dstpos; + uchar *dstbegin; uint bits; int nbits; int lfreq[Nlitlen]; @@ -91,7 +85,6 @@ typedef struct { Runlen rbuf[RbufSize]; /* literal run length, match len, match dist */ Runlen *rend; /* current pos in rbuf */ int run; /* literal count */ - uchar dstbuf[DstSize]; } State; @@ -552,7 +545,7 @@ static Match getmatch(State *s, ushort pos, ushort mpos) { } static void newblock(State *s) { - s->dst = s->dstpos = s->dstbuf; + s->dst = s->dstbegin = s->dstbuf; s->rend = s->rbuf; s->run = 0; memset(s->lfreq, 0, sizeof(s->lfreq)); @@ -592,15 +585,15 @@ static int deflate_state(State *s) { int pos = s->pos; int head; - - if (s->state == Block) { - if (s->lastblock && s->dstpos == s->dstbuf) /* todo.. */ + switch (s->state) { + case FlateHasOutput: + if (s->lastblock && s->dstbegin == s->dst) /* todo.. */ return FlateOk; else goto block; - } - if (s->state == FillWin) + case FlateNeedInput: goto filled; + } do { /* messy.. */ if (s->avail < MaxMatch || s->rend - s->rbuf >= RbufSize - 2) { @@ -612,22 +605,22 @@ static int deflate_state(State *s) { prevm.len = 0; } fprintf(stderr, "avail %d pos %d lastpos %d srcavail %d\n", s->avail, pos, s->pos, s->srcend - s->src); - s->state = Block; s->pos = pos; - s->m = prevm; + s->prevm = prevm; + s->state = FlateHasOutput; deflate_block(s, pos - s->blockstart); return FlateHasOutput; block: - if (s->dstpos != s->dstbuf) + if (s->dstbegin < s->dst) return FlateHasOutput; newblock(s); pos = s->pos; - prevm = s->m; + prevm = s->prevm; s->blockstart = pos; } s->pos = pos; - s->m = prevm; - s->state = FillWin; + s->prevm = prevm; + s->state = FlateNeedInput; if (fillwin(s)) return FlateNeedInput; /* TODO: .. */ @@ -636,7 +629,7 @@ filled: /* tralala */; fprintf(stderr, "avail %d pos %d srcavail %d\n", s->avail, s->pos, s->srcend - s->src); pos = s->pos; - prevm = s->m; + prevm = s->prevm; } head = addtochain(s, pos); if (head >= pos || pos - head >= MaxDist) @@ -662,7 +655,7 @@ fprintf(stderr, "avail %d pos %d srcavail %d\n", s->avail, s->pos, s->srcend - s } while (s->avail); flushlit(s); s->lastblock = 1; - s->state = Block; + s->state = FlateHasOutput; deflate_block(s, pos - s->blockstart); putbits(s, 0, 7); return FlateHasOutput; @@ -690,7 +683,7 @@ static State *alloc_state(void) { huffcodes(fixlcode, fixllen, Nlitlen); huffcodes(fixdcode, fixdlen, Ndist); - s->dstpos = s->dstbuf; + s->dst = s->dstbegin = s->dstbuf; s->pos = s->blockstart = WinSize; s->avail = 0; return s; @@ -709,7 +702,7 @@ int deflate(FlateStream *stream) { s = stream->state = alloc_state(); if (!s) return stream->err = "no mem.", FlateError; - s->state = Block; + s->state = FlateHasOutput; } if (stream->nin) { s->src = stream->in; @@ -718,13 +711,11 @@ int deflate(FlateStream *stream) { } n = deflate_state(s); if (n == FlateHasOutput) { - k = s->dst - s->dstpos; + k = s->dst - s->dstbegin; if (k < stream->nout) stream->nout = k; - memcpy(stream->out, s->dstpos, stream->nout); - s->dstpos += stream->nout; - if (s->dstpos == s->dst) - s->dstpos = s->dstbuf; + memcpy(stream->out, s->dstbegin, stream->nout); + s->dstbegin += stream->nout; } if (n == FlateOk) { free(s); @@ -732,28 +723,3 @@ int deflate(FlateStream *stream) { } return n; } - -/* -#include <stdlib.h> -#include <stdio.h> - -enum {Siz = 1<<24}; - -int main() { - FlateStream s; - int n; - - s.in = malloc(Siz); - s.nin = fread(s.in, 1, Siz, stdin); - s.out = malloc(Siz); - s.nout = Siz; - s.err = 0; - s.state = 0; - n = deflate(&s); - fwrite(s.out, 1, s.nout, stdout); - fprintf(stderr, "compressed: %d return: %d\n", s.nout, n); - free(s.in); - free(s.out); - return 0; -} -*/