commit 7f256464f678d5bd8c3bd823882a80b5bb24054b
parent 29637fae613ae2ef326c64b4270ade27ea1ca61f
Author: nsz <nszabolcs@gmail.com>
Date: Wed, 22 Apr 2009 13:28:13 +0200
-dstbegin
Diffstat:
1 file changed, 9 insertions(+), 24 deletions(-)
diff --git a/inflate_simple.c b/inflate_simple.c
@@ -20,9 +20,7 @@ typedef struct {
typedef struct {
uchar *src;
-
uchar *dst;
- uchar *dstbegin;
uint bits;
uint nbits;
@@ -35,11 +33,11 @@ typedef struct {
static HuffTree ltree; /* fixed lit/len tree */
static HuffTree dtree; /* fixed distance tree */
-/* base offsets */
+/* base offset tables */
static ushort lenbase[Nlen];
static ushort distbase[Ndist];
-/* extra bits */
+/* extra bits tables */
static uchar lenbits[Nlen] = {
0, 0, 0, 0, 0, 0, 0, 0, 1, 1,
1, 1, 2, 2, 2, 2, 3, 3, 3, 3,
@@ -65,7 +63,6 @@ static void init_base_tables(void) {
base += 1 << lenbits[i];
}
lenbase[Nlen-1]--; /* deflate bug */
-
for (base = 1, i = 0; i < Ndist; i++) {
distbase[i] = base;
base += 1 << distbits[i];
@@ -86,7 +83,6 @@ static void init_fixed_trees(void) {
ltree.symbol[24 + 144 + i] = 280 + i;
for (i = 0; i < 112; i++)
ltree.symbol[24 + 144 + 8 + i] = 144 + i;
-
dtree.count[5] = Ndist;
for (i = 0; i < Ndist; i++)
dtree.symbol[i] = i;
@@ -97,19 +93,16 @@ static void build_tree(HuffTree *t, const uchar *lens, int n) {
int offs[HuffBits];
int i, sum;
+ /* count code lengths and calc first code (offs) for each length */
for (i = 0; i < HuffBits; i++)
t->count[i] = 0;
-
- /* count code lengths and calc first code for each length */
for (i = 0; i < n; i++)
t->count[lens[i]]++;
t->count[0] = 0;
-
for (sum = 0, i = 0; i < HuffBits; i++) {
offs[i] = sum;
sum += t->count[i];
}
-
/* sort symbols by code length */
for (i = 0; i < n; i++)
if (lens[i])
@@ -166,14 +159,12 @@ static void decode_trees(FlateStream *s, HuffTree *lt, HuffTree *dt) {
nlit = 257 + getbits(s, 5);
ndist = 1 + getbits(s, 5);
nclen = 4 + getbits(s, 4);
-
/* build code length tree */
for (i = 0; i < Nclen; i++)
lens[i] = 0;
for (i = 0; i < nclen; i++)
lens[clenorder[i]] = getbits(s, 3);
build_tree(&ctree, lens, Nclen);
-
/* decode code lengths for the dynamic trees */
for (i = 0; i < nlit + ndist; ) {
uint sym = decode_symbol(s, &ctree);
@@ -208,7 +199,6 @@ static void decode_block(FlateStream *s, HuffTree *lt, HuffTree *dt) {
for (;;) {
sym = decode_symbol(s, lt);
-
if (sym == 256)
return;
if (sym < 256)
@@ -234,10 +224,7 @@ static void inflate_uncompressed_block(FlateStream *s) {
len = (s->src[1] << 8) | s->src[0];
s->src += 4;
-
- /* start next block on a byte boundary */
- s->nbits = 0;
-
+ s->nbits = 0; /* start next block on a byte boundary */
while (len--)
*s->dst++ = *s->src++;
}
@@ -260,21 +247,19 @@ void inflate_init(void) {
init_base_tables();
}
-/* inflate stream from src to dst */
-uint inflate(void *dst, void *src) {
+/* inflate stream from src to dst, return end pointer */
+void *inflate(void *dst, void *src) {
FlateStream s;
uint final;
s.src = src;
- s.dst = s.dstbegin = dst;
+ s.dst = dst;
s.nbits = 0;
do {
uint blocktype;
final = getbit(&s);
blocktype = getbits(&s, 2);
-
- /* decompress block */
switch (blocktype) {
case 0:
inflate_uncompressed_block(&s);
@@ -287,7 +272,7 @@ uint inflate(void *dst, void *src) {
break;
}
} while (!final);
- return s.dst - s.dstbegin;
+ return s.dst;
}
@@ -313,7 +298,7 @@ int main(int argc, char **argv) {
inflate_init();
src = readall(argc < 2 ? stdin : fopen(argv[1], "r"));
dst = malloc(len);
- len = inflate(dst, src);
+ len = (uchar *)inflate(dst, src) - dst;
fprintf(stderr, "inflate_simple: uncompressed %u bytes\n", len);
fwrite(dst, 1, len, stdout);
free(dst);