flate

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

commit 000a182ffddc221d01b79df9677037e9f0b00d60
parent b7c7df969c1390086c6dbe0160646e923cc636df
Author: nsz <nszabolcs@gmail.com>
Date:   Mon, 24 Aug 2009 10:40:22 +0200

-callback interface, +todo
Diffstat:
README | 5+++--
TODO | 16++++++++--------
deflate.c | 27+++++++++++++++------------
flate.h | 2--
inflate.c | 42------------------------------------------
sflate.c | 2+-
6 files changed, 27 insertions(+), 67 deletions(-)

diff --git a/README b/README @@ -9,6 +9,7 @@ based on: libflate from plan 9 deflate from halibut sshzlib from putty + deflateencoder from 7zip build: make @@ -26,9 +27,9 @@ usage: -p pkzip files: - adler.c: adler32 chechksum + adler.c: adler32 checksum crc.c: crc32 checksum deflate.c: raw deflate compression inflate.c: raw deflate decompression inflate_simple.c: minimal inflate implementation for reference - sflate.c: deflate, zlib, gzip stream compression + sflate.c: deflate, zlib, gzip, pkzip stream compression diff --git a/TODO b/TODO @@ -1,31 +1,31 @@ flate ----- -fill output entirely -man +fill output entirely (s->out, s->outend ?) +stream->err ? globals -error message +man inflate assumes Flate* < 0 _init _reset _free ? _zlib _gzip _zip interface +pkzip output is bugous to please unzip test, benchmark inflate ------- --callback interface return extra input -(rev lookup vs revinc) -(test/optimize uncompressed block) read less than 7 bits in clen decode bound checks in clen decode +(rev lookup vs revinc) +(test/optimize uncompressed block) deflate ------- -block split heuristics with lfreq, dfreq + cost +block split heuristics with lfreq, dfreq + cost (costbuf) +match selection (entropy based cost) special case: last block (better optimization for short input) check ushort vs int -lzbuf + costbuf minor mods: overlap rbuf/dstwin hufflen: overlap arrays (parent+heap only) diff --git a/deflate.c b/deflate.c @@ -655,18 +655,21 @@ static State *alloc_state(void) { memset(s->chain, 0, sizeof(s->chain)); memset(s->head, 0, sizeof(s->head)); s->bits = s->nbits = 0; - for (i = 0; i < 144; i++) - fixllen[i] = 8; - for (; i < 256; i++) - fixllen[i] = 9; - for (; i < 280; i++) - fixllen[i] = 7; - for (; i < Nlitlen; i++) - fixllen[i] = 8; - for (i = 0; i < Ndist; i++) - fixdlen[i] = 5; - huffcodes(fixlcode, fixllen, Nlitlen); - huffcodes(fixdcode, fixdlen, Ndist); + /* TODO: globals */ + if (fixllen[0] == 0) { + for (i = 0; i < 144; i++) + fixllen[i] = 8; + for (; i < 256; i++) + fixllen[i] = 9; + for (; i < 280; i++) + fixllen[i] = 7; + for (; i < Nlitlen; i++) + fixllen[i] = 8; + for (i = 0; i < Ndist; i++) + fixdlen[i] = 5; + huffcodes(fixlcode, fixllen, Nlitlen); + huffcodes(fixdcode, fixdlen, Ndist); + } s->state = FlateOut; s->in = s->inend = 0; s->dst = s->dstbegin = s->dstbuf; diff --git a/flate.h b/flate.h @@ -20,9 +20,7 @@ typedef struct { } FlateStream; int deflate(FlateStream *s); -int deflate_callback(int (*r)(void *, int, void *), void *rdata, int (*w)(void *, int, void *), void *wdata); int inflate(FlateStream *s); -int inflate_callback(int (*r)(void *, int, void *), void *rdata, int (*w)(void *, int, void *), void *wdata); uint adler32(uchar *p, int n, uint adler); void crc32init(void); diff --git a/inflate.c b/inflate.c @@ -642,45 +642,3 @@ int inflate(FlateStream *stream) { } return n; } - -int inflate_callback(int (*r)(void *, int, void *), void *rdata, int (*w)(void *, int, void *), void *wdata) { - State *s; - uchar *src; - int len, n; - enum {SrcSize = 4096}; - - s = alloc_state(); - if (!s) - return FlateErr; - s->src = s->srcend = src = malloc(SrcSize); - if (!s->src) { - free(s); - return FlateErr; - } - n = FlateIn; - for (;;) - switch (n) { - case FlateIn: - s->src = src; - len = r(s->src, SrcSize, rdata); - if (len > 0) { - s->srcend = s->src + len; - n = inflate_state(s); - } else - n = FlateErr; - break; - case FlateOut: - len = w(s->win, s->pos, wdata); - if (len == s->pos) { - s->pos = 0; - n = inflate_state(s); - } else - n = FlateErr; - break; - case FlateOk: - case FlateErr: - free(src); - free(s); - return n; - } -} diff --git a/sflate.c b/sflate.c @@ -152,7 +152,7 @@ enum { PKVersion = 20, PKFlag = 1 << 3, PKMethod = 8, - PKDate = (2009 - 1980) << 25, + PKDate = ((2009 - 1980) << 25) | (1 << 21) | (1 << 16), PKHeadSize = 30, PKDirSize = 46, PKNameLen = sizeof(pkname) - 1