zlib/infcodes.c

248 lines
7.3 KiB
C
Raw Normal View History

2011-09-10 01:36:31 -04:00
/* infcodes.c -- process literals and length/distance pairs
2011-09-10 02:14:39 -04:00
* Copyright (C) 1995-1996 Mark Adler
2011-09-10 01:36:31 -04:00
* For conditions of distribution and use, see copyright notice in zlib.h
*/
#include "zutil.h"
#include "inftrees.h"
2011-09-10 02:09:18 -04:00
#include "infblock.h"
#include "infcodes.h"
2011-09-10 01:36:31 -04:00
#include "infutil.h"
2011-09-10 01:52:17 -04:00
#include "inffast.h"
2011-09-10 01:36:31 -04:00
/* 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 */
2011-09-10 02:08:07 -04:00
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 */
2011-09-10 01:36:31 -04:00
/* mode dependent information */
uInt len;
union {
struct {
2011-09-10 02:08:07 -04:00
inflate_huft *tree; /* pointer into tree */
uInt need; /* bits needed */
} code; /* if LEN or DIST, where in tree */
uInt lit; /* if LIT, literal */
2011-09-10 01:36:31 -04:00
struct {
2011-09-10 02:08:07 -04:00
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 */
2011-09-10 01:36:31 -04:00
/* mode independent information */
2011-09-10 02:08:07 -04:00
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 */
2011-09-10 01:36:31 -04:00
};
2011-09-10 02:09:18 -04:00
inflate_codes_statef *inflate_codes_new(bl, bd, tl, td, z)
2011-09-10 01:36:31 -04:00
uInt bl, bd;
2011-09-10 02:14:39 -04:00
inflate_huft *tl;
inflate_huft *td; /* need separate declaration for Borland C++ */
2011-09-10 02:17:02 -04:00
z_streamp z;
2011-09-10 01:36:31 -04:00
{
2011-09-10 02:09:18 -04:00
inflate_codes_statef *c;
2011-09-10 01:36:31 -04:00
2011-09-10 02:09:18 -04:00
if ((c = (inflate_codes_statef *)
2011-09-10 01:36:31 -04:00
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;
2011-09-10 02:06:52 -04:00
Tracev((stderr, "inflate: codes new\n"));
2011-09-10 01:36:31 -04:00
}
return c;
}
int inflate_codes(s, z, r)
2011-09-10 02:09:18 -04:00
inflate_blocks_statef *s;
2011-09-10 02:17:02 -04:00
z_streamp z;
2011-09-10 01:36:31 -04:00
int r;
{
2011-09-10 02:08:07 -04:00
uInt j; /* temporary storage */
inflate_huft *t; /* temporary pointer */
2011-09-10 02:08:28 -04:00
uInt e; /* extra bits or operation */
2011-09-10 02:08:07 -04:00
uLong b; /* bit buffer */
uInt k; /* bits in bit buffer */
2011-09-10 02:09:18 -04:00
Bytef *p; /* input data pointer */
2011-09-10 02:08:07 -04:00
uInt n; /* bytes available there */
2011-09-10 02:09:18 -04:00
Bytef *q; /* output window write pointer */
2011-09-10 02:08:07 -04:00
uInt m; /* bytes to end of window or read pointer */
2011-09-10 02:09:18 -04:00
Bytef *f; /* pointer to copy strings from */
inflate_codes_statef *c = s->sub.decode.codes; /* codes state */
2011-09-10 01:36:31 -04:00
/* copy input/output information to locals (UPDATE macro restores) */
LOAD
/* process input and output based on current state */
while (1) switch (c->mode)
2011-09-10 02:08:07 -04:00
{ /* waiting for "i:"=input, "o:"=output, "x:"=nothing */
case START: /* x: set up for LEN */
2011-09-10 01:52:17 -04:00
#ifndef SLOW
if (m >= 258 && n >= 10)
{
UPDATE
2011-09-10 02:08:07 -04:00
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;
}
2011-09-10 01:52:17 -04:00
}
#endif /* !SLOW */
2011-09-10 01:36:31 -04:00
c->sub.code.need = c->lbits;
c->sub.code.tree = c->ltree;
c->mode = LEN;
2011-09-10 02:08:07 -04:00
case LEN: /* i: get length/literal/eob next */
2011-09-10 01:36:31 -04:00
j = c->sub.code.need;
NEEDBITS(j)
t = c->sub.code.tree + ((uInt)b & inflate_mask[j]);
DUMPBITS(t->bits)
2011-09-10 02:08:28 -04:00
e = (uInt)(t->exop);
if (e == 0) /* literal */
2011-09-10 01:36:31 -04:00
{
2011-09-10 02:08:07 -04:00
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;
2011-09-10 01:36:31 -04:00
}
2011-09-10 02:08:28 -04:00
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 */
2011-09-10 02:14:39 -04:00
z->msg = (char*)"invalid literal/length code";
2011-09-10 02:08:28 -04:00
r = Z_DATA_ERROR;
LEAVE
2011-09-10 02:08:07 -04:00
case LENEXT: /* i: getting length extra (have base) */
2011-09-10 01:36:31 -04:00
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;
2011-09-10 02:06:52 -04:00
Tracevv((stderr, "inflate: length %u\n", c->len));
2011-09-10 01:36:31 -04:00
c->mode = DIST;
2011-09-10 02:08:07 -04:00
case DIST: /* i: get distance next */
2011-09-10 01:36:31 -04:00
j = c->sub.code.need;
NEEDBITS(j)
t = c->sub.code.tree + ((uInt)b & inflate_mask[j]);
DUMPBITS(t->bits)
2011-09-10 02:08:28 -04:00
e = (uInt)(t->exop);
if (e & 16) /* distance */
2011-09-10 01:36:31 -04:00
{
2011-09-10 02:08:28 -04:00
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;
2011-09-10 02:08:07 -04:00
c->sub.code.tree = t->next;
break;
2011-09-10 01:36:31 -04:00
}
2011-09-10 02:08:28 -04:00
c->mode = BADCODE; /* invalid code */
2011-09-10 02:14:39 -04:00
z->msg = (char*)"invalid distance code";
2011-09-10 02:08:28 -04:00
r = Z_DATA_ERROR;
LEAVE
2011-09-10 02:08:07 -04:00
case DISTEXT: /* i: getting distance extra */
2011-09-10 01:36:31 -04:00
j = c->sub.copy.get;
NEEDBITS(j)
c->sub.copy.dist += (uInt)b & inflate_mask[j];
DUMPBITS(j)
2011-09-10 02:06:52 -04:00
Tracevv((stderr, "inflate: distance %u\n", c->sub.copy.dist));
2011-09-10 01:36:31 -04:00
c->mode = COPY;
2011-09-10 02:08:07 -04:00
case COPY: /* o: copying bytes in window, waiting for space */
2011-09-10 02:09:18 -04:00
#ifndef __TURBOC__ /* Turbo C bug for following expression */
2011-09-10 01:52:17 -04:00
f = (uInt)(q - s->window) < c->sub.copy.dist ?
2011-09-10 02:08:07 -04:00
s->end - (c->sub.copy.dist - (q - s->window)) :
q - c->sub.copy.dist;
2011-09-10 02:09:18 -04:00
#else
f = q - c->sub.copy.dist;
if ((uInt)(q - s->window) < c->sub.copy.dist)
2011-09-10 02:11:37 -04:00
f = s->end - (c->sub.copy.dist - (uInt)(q - s->window));
2011-09-10 02:09:18 -04:00
#endif
2011-09-10 01:36:31 -04:00
while (c->len)
{
2011-09-10 02:08:07 -04:00
NEEDOUT
OUTBYTE(*f++)
if (f == s->end)
f = s->window;
c->len--;
2011-09-10 01:36:31 -04:00
}
c->mode = START;
break;
2011-09-10 02:08:07 -04:00
case LIT: /* o: got literal, waiting for output space */
2011-09-10 01:36:31 -04:00
NEEDOUT
OUTBYTE(c->sub.lit)
c->mode = START;
break;
2011-09-10 02:08:07 -04:00
case WASH: /* o: got eob, possibly more output */
2011-09-10 01:36:31 -04:00
FLUSH
if (s->read != s->write)
LEAVE
c->mode = END;
case END:
r = Z_STREAM_END;
LEAVE
2011-09-10 02:08:07 -04:00
case BADCODE: /* x: got error */
2011-09-10 01:36:31 -04:00
r = Z_DATA_ERROR;
LEAVE
default:
r = Z_STREAM_ERROR;
LEAVE
}
}
void inflate_codes_free(c, z)
2011-09-10 02:09:18 -04:00
inflate_codes_statef *c;
2011-09-10 02:17:02 -04:00
z_streamp z;
2011-09-10 01:36:31 -04:00
{
ZFREE(z, c);
2011-09-10 02:06:52 -04:00
Tracev((stderr, "inflate: codes free\n"));
2011-09-10 01:36:31 -04:00
}