changeset: 97:fd3836ea7105
user: nsz <nszabolcs_AT_gmail.com>
date: Tue Aug 11 14:19:18 2009 +0200
files: deflate.c
description:
deflate += comments
diff -r f7385ae4153e -r fd3836ea7105 deflate.c
--- a/deflate.c Tue Aug 11 13:48:57 2009 +0200
+++ b/deflate.c Tue Aug 11 14:19:18 2009 +0200
@@ -79,7 +79,7 @@
ushort chain[MaxDist]; /* hash chain */
LzCode lzbuf[LzSize]; /* literal run length, match len, match dist */
LzCode *lz; /* current pos in lzbuf */
- int run; /* literal run length */
+ int nlit; /* literal run length */
ushort lfreq[Nlitlen];
ushort dfreq[Ndist];
uchar dstbuf[DstSize];
@@ -277,6 +277,7 @@
}
}
+/* run length encode literal and dist code lengths into codes and extra */
static int clencodes(uchar *codes, uchar *extra, uchar *llen, int nlit, uchar *dlen, int ndist) {
int i, c, r, rr;
int n = 0;
@@ -286,7 +287,6 @@
for (i = 0; i < ndist; i++)
codes[nlit + i] = dlen[i];
for (i = 0; i < nlit + ndist;) {
- /* run length encoding */
c = codes[i];
for (r = 1; i + r < nlit + ndist && codes[i + r] == c; r++);
i += r;
@@ -316,6 +316,7 @@
return n;
}
+/* compress block data into s->dstbuf using given codes */
static void putblock(State *s, ushort *lcode, uchar *llen, ushort *dcode, uchar *dlen) {
int n;
LzCode *lz;
@@ -336,6 +337,7 @@
putbits(s, lcode[EOB], llen[EOB]);
}
+/* build code trees and select dynamic/fixed/uncompressed block compression */
static void deflate_block(State *s) {
uchar codes[Nlitlen + Ndist], extra[Nlitlen + Ndist];
uchar llen[Nlitlen], dlen[Ndist], clen[Nclen];
@@ -365,7 +367,7 @@
huffcodes(ccode, clen, Nclen);
for (nclen = Nclen; nclen > 4 && clen[clenorder[nclen-1]] == 0; nclen--);
- /* calc size */
+ /* calc compressed size */
uncsize = 3 + 16 + 8 * blocklen + (16 - 3 - s->nbits) % 8; /* byte aligned */
fixsize = 3;
dynsize = 3 + 5 + 5 + 4 + 3 * nclen;
@@ -443,6 +445,7 @@
*/
}
+/* find n in base */
static int bisect(ushort *base, int len, int n) {
int lo = 0;
int hi = len;
@@ -458,15 +461,17 @@
return lo - 1;
}
+/* add literal run length to lzbuf */
static void flushlit(State *s) {
- if (s->run) {
+ if (s->nlit) {
s->lz->bits = LzLitFlag;
- s->lz->n = s->run;
+ s->lz->n = s->nlit;
s->lz++;
- s->run = 0;
+ s->nlit = 0;
}
}
+/* add match to lzbuf and update freq counts */
static void recordmatch(State *s, Match m) {
int n;
@@ -483,16 +488,18 @@
s->dfreq[n]++;
}
+/* update literal run length */
static void recordlit(State *s, int c) {
- s->run++;
+ s->nlit++;
s->lfreq[c]++;
}
-/* multiplicative hash */
+/* multiplicative hash (using a prime close to golden ratio * 2^32) */
static int gethash(uchar *p) {
return (0x9e3779b1 * ((p[0]<<16) + (p[1]<<8) + p[2]) >> (32 - HashBits)) % HashSize;
}
+/* update hash chain at the current position */
static int updatechain(State *s) {
int h, i;
@@ -507,6 +514,7 @@
return i;
}
+/* find longest match, mpos is the next in the hash chain */
static Match getmatch(State *s, int mpos) {
Match m = {0, MinMatch-1};
int len;
@@ -538,16 +546,18 @@
return m;
}
+/* start a new block */
static void newblock(State *s) {
s->startpos = s->pos;
s->dst = s->dstbegin = s->dstbuf;
s->lz = s->lzbuf;
- s->run = 0;
+ s->nlit = 0;
memset(s->lfreq, 0, sizeof(s->lfreq));
memset(s->dfreq, 0, sizeof(s->dfreq));
s->lfreq[EOB]++;
}
+/* shift and fill win from s->src */
static int fillwin(State *s) {
int n, k;
@@ -576,6 +586,7 @@
return 1;
}
+/* check if the current block should be ended */
static int endofblock(State *s) {
if (s->avail == 0)
return 1;
@@ -585,6 +596,7 @@
return 0;
}
+/* deflate from s->src into s->dstbuf */
static int deflate_state(State *s) {
Match m;
int head;
@@ -636,6 +648,7 @@
}
}
+/* alloc and init state */
static State *alloc_state(void) {
State *s = malloc(sizeof(State));
int i;
Received on Tue Aug 11 2009 - 13:07:17 UTC
This archive was generated by hypermail 2.2.0 : Sun Aug 16 2009 - 14:19:02 UTC