A massively spiffy yet delicately unobtrusive compression library. (grpc依赖)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

248 lines
7.3 KiB

14 years ago
/* infcodes.c -- process literals and length/distance pairs
14 years ago
* Copyright (C) 1995-1996 Mark Adler
14 years ago
* For conditions of distribution and use, see copyright notice in zlib.h
*/
#include "zutil.h"
#include "inftrees.h"
14 years ago
#include "infblock.h"
#include "infcodes.h"
14 years ago
#include "infutil.h"
14 years ago
#include "inffast.h"
14 years ago
/* simplify the use of the inflate_huft type with some defines */
#define base more.Base
#define next more.Next
#define exop word.what.Exop
#define bits word.what.Bits
/* inflate codes private state */
struct inflate_codes_state {
/* mode */
14 years ago
enum { /* waiting for "i:"=input, "o:"=output, "x:"=nothing */
START, /* x: set up for LEN */
LEN, /* i: get length/literal/eob next */
LENEXT, /* i: getting length extra (have base) */
DIST, /* i: get distance next */
DISTEXT, /* i: getting distance extra */
COPY, /* o: copying bytes in window, waiting for space */
LIT, /* o: got literal, waiting for output space */
WASH, /* o: got eob, possibly still output waiting */
END, /* x: got eob and all data flushed */
BADCODE} /* x: got error */
mode; /* current inflate_codes mode */
14 years ago
/* mode dependent information */
uInt len;
union {
struct {
14 years ago
inflate_huft *tree; /* pointer into tree */
uInt need; /* bits needed */
} code; /* if LEN or DIST, where in tree */
uInt lit; /* if LIT, literal */
14 years ago
struct {
14 years ago
uInt get; /* bits to get for extra */
uInt dist; /* distance back to copy from */
} copy; /* if EXT or COPY, where and how much */
} sub; /* submode */
14 years ago
/* mode independent information */
14 years ago
Byte lbits; /* ltree bits decoded per branch */
Byte dbits; /* dtree bits decoder per branch */
inflate_huft *ltree; /* literal/length/eob tree */
inflate_huft *dtree; /* distance tree */
14 years ago
};
14 years ago
inflate_codes_statef *inflate_codes_new(bl, bd, tl, td, z)
14 years ago
uInt bl, bd;
14 years ago
inflate_huft *tl;
inflate_huft *td; /* need separate declaration for Borland C++ */
14 years ago
z_stream *z;
{
14 years ago
inflate_codes_statef *c;
14 years ago
14 years ago
if ((c = (inflate_codes_statef *)
14 years ago
ZALLOC(z,1,sizeof(struct inflate_codes_state))) != Z_NULL)
{
c->mode = START;
c->lbits = (Byte)bl;
c->dbits = (Byte)bd;
c->ltree = tl;
c->dtree = td;
14 years ago
Tracev((stderr, "inflate: codes new\n"));
14 years ago
}
return c;
}
int inflate_codes(s, z, r)
14 years ago
inflate_blocks_statef *s;
14 years ago
z_stream *z;
int r;
{
14 years ago
uInt j; /* temporary storage */
inflate_huft *t; /* temporary pointer */
14 years ago
uInt e; /* extra bits or operation */
14 years ago
uLong b; /* bit buffer */
uInt k; /* bits in bit buffer */
14 years ago
Bytef *p; /* input data pointer */
14 years ago
uInt n; /* bytes available there */
14 years ago
Bytef *q; /* output window write pointer */
14 years ago
uInt m; /* bytes to end of window or read pointer */
14 years ago
Bytef *f; /* pointer to copy strings from */
inflate_codes_statef *c = s->sub.decode.codes; /* codes state */
14 years ago
/* copy input/output information to locals (UPDATE macro restores) */
LOAD
/* process input and output based on current state */
while (1) switch (c->mode)
14 years ago
{ /* waiting for "i:"=input, "o:"=output, "x:"=nothing */
case START: /* x: set up for LEN */
14 years ago
#ifndef SLOW
if (m >= 258 && n >= 10)
{
UPDATE
14 years ago
r = inflate_fast(c->lbits, c->dbits, c->ltree, c->dtree, s, z);
LOAD
if (r != Z_OK)
{
c->mode = r == Z_STREAM_END ? WASH : BADCODE;
break;
}
14 years ago
}
#endif /* !SLOW */
14 years ago
c->sub.code.need = c->lbits;
c->sub.code.tree = c->ltree;
c->mode = LEN;
14 years ago
case LEN: /* i: get length/literal/eob next */
14 years ago
j = c->sub.code.need;
NEEDBITS(j)
t = c->sub.code.tree + ((uInt)b & inflate_mask[j]);
DUMPBITS(t->bits)
14 years ago
e = (uInt)(t->exop);
if (e == 0) /* literal */
14 years ago
{
14 years ago
c->sub.lit = t->base;
Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
"inflate: literal '%c'\n" :
"inflate: literal 0x%02x\n", t->base));
c->mode = LIT;
break;
14 years ago
}
14 years ago
if (e & 16) /* length */
{
c->sub.copy.get = e & 15;
c->len = t->base;
c->mode = LENEXT;
break;
}
if ((e & 64) == 0) /* next table */
{
c->sub.code.need = e;
c->sub.code.tree = t->next;
break;
}
if (e & 32) /* end of block */
{
Tracevv((stderr, "inflate: end of block\n"));
c->mode = WASH;
break;
}
c->mode = BADCODE; /* invalid code */
14 years ago
z->msg = (char*)"invalid literal/length code";
14 years ago
r = Z_DATA_ERROR;
LEAVE
14 years ago
case LENEXT: /* i: getting length extra (have base) */
14 years ago
j = c->sub.copy.get;
NEEDBITS(j)
c->len += (uInt)b & inflate_mask[j];
DUMPBITS(j)
c->sub.code.need = c->dbits;
c->sub.code.tree = c->dtree;
14 years ago
Tracevv((stderr, "inflate: length %u\n", c->len));
14 years ago
c->mode = DIST;
14 years ago
case DIST: /* i: get distance next */
14 years ago
j = c->sub.code.need;
NEEDBITS(j)
t = c->sub.code.tree + ((uInt)b & inflate_mask[j]);
DUMPBITS(t->bits)
14 years ago
e = (uInt)(t->exop);
if (e & 16) /* distance */
14 years ago
{
14 years ago
c->sub.copy.get = e & 15;
c->sub.copy.dist = t->base;
c->mode = DISTEXT;
break;
}
if ((e & 64) == 0) /* next table */
{
c->sub.code.need = e;
14 years ago
c->sub.code.tree = t->next;
break;
14 years ago
}
14 years ago
c->mode = BADCODE; /* invalid code */
14 years ago
z->msg = (char*)"invalid distance code";
14 years ago
r = Z_DATA_ERROR;
LEAVE
14 years ago
case DISTEXT: /* i: getting distance extra */
14 years ago
j = c->sub.copy.get;
NEEDBITS(j)
c->sub.copy.dist += (uInt)b & inflate_mask[j];
DUMPBITS(j)
14 years ago
Tracevv((stderr, "inflate: distance %u\n", c->sub.copy.dist));
14 years ago
c->mode = COPY;
14 years ago
case COPY: /* o: copying bytes in window, waiting for space */
14 years ago
#ifndef __TURBOC__ /* Turbo C bug for following expression */
14 years ago
f = (uInt)(q - s->window) < c->sub.copy.dist ?
14 years ago
s->end - (c->sub.copy.dist - (q - s->window)) :
q - c->sub.copy.dist;
14 years ago
#else
f = q - c->sub.copy.dist;
if ((uInt)(q - s->window) < c->sub.copy.dist)
14 years ago
f = s->end - (c->sub.copy.dist - (uInt)(q - s->window));
14 years ago
#endif
14 years ago
while (c->len)
{
14 years ago
NEEDOUT
OUTBYTE(*f++)
if (f == s->end)
f = s->window;
c->len--;
14 years ago
}
c->mode = START;
break;
14 years ago
case LIT: /* o: got literal, waiting for output space */
14 years ago
NEEDOUT
OUTBYTE(c->sub.lit)
c->mode = START;
break;
14 years ago
case WASH: /* o: got eob, possibly more output */
14 years ago
FLUSH
if (s->read != s->write)
LEAVE
c->mode = END;
case END:
r = Z_STREAM_END;
LEAVE
14 years ago
case BADCODE: /* x: got error */
14 years ago
r = Z_DATA_ERROR;
LEAVE
default:
r = Z_STREAM_ERROR;
LEAVE
}
}
void inflate_codes_free(c, z)
14 years ago
inflate_codes_statef *c;
14 years ago
z_stream *z;
{
ZFREE(z, c);
14 years ago
Tracev((stderr, "inflate: codes free\n"));
14 years ago
}