Back to index

plt-scheme  4.2.1
Classes | Defines | Functions | Variables
inftrees.c File Reference
#include "zutil.h"
#include "inftrees.h"

Go to the source code of this file.

Classes

struct  internal_state

Defines

#define BUILDFIXED   /* non ANSI compilers may not accept inffixed.h */
#define exop   word.what.Exop
#define bits   word.what.Bits
#define BMAX   15 /* maximum bit length of any code */
#define C0   *p++ = 0;
#define C2   C0 C0 C0 C0
#define C4   C2 C2 C2 C2
#define FIXEDH   544 /* number of hufts used by fixed tables */

Functions

local int huft_build OF ((uIntf *, uInt, uInt, const uIntf *, const uIntf *, inflate_huft *FAR *, uIntf *, inflate_huft *, uInt *, uIntf *))
local int huft_build (uIntf *b, uInt n, uInt s, const uIntf *d, const uIntf *e, inflate_huft *FAR *t, uIntf *m, inflate_huft *hp, uInt *hn, uIntf *v)
int inflate_trees_bits (uIntf *c, uIntf *bb, inflate_huft *FAR *tb, inflate_huft *hp, z_streamp z)
int inflate_trees_dynamic (uInt nl, uInt nd, uIntf *c, uIntf *bl, uIntf *bd, inflate_huft *FAR *tl, inflate_huft *FAR *td, inflate_huft *hp, z_streamp z)
int inflate_trees_fixed (uIntf *bl, uIntf *bd, inflate_huft *FAR *tl, inflate_huft *FAR *td, z_streamp z)

Variables

const char inflate_copyright [] = " inflate 1.1.4 Copyright 1995-2002 Mark Adler "
local const uInt cplens [31]
local const uInt cplext [31]
local const uInt cpdist [30]
local const uInt cpdext [30]
local int fixed_built = 0
local inflate_huft fixed_mem [FIXEDH]
local uInt fixed_bl
local uInt fixed_bd
local inflate_huft * fixed_tl
local inflate_huft * fixed_td

Class Documentation

struct internal_state

Definition at line 82 of file deflate.h.

Collaboration diagram for internal_state:
Class Members
ush bi_buf
int bi_valid
ush bl_count
long block_start
inflate_blocks_statef * blocks
ushf * d_buf
Byte data_type
uch depth
int dummy
uInt good_match
uInt hash_bits
uInt hash_mask
uInt hash_shift
uInt hash_size
Posf * head
int heap
int heap_len
int heap_max
uInt ins_h
uchf * l_buf
int last_eob_len
int last_flush
uInt last_lit
int level
uInt lit_bufsize
uInt lookahead
int match_available
uInt match_length
uInt match_start
uInt matches
uInt max_chain_length
uInt max_lazy_match
Byte method
inflate_mode mode
int nice_match
int noheader
int nowrap
ulg opt_len
int pending
Bytef * pending_buf
ulg pending_buf_size
Bytef * pending_out
Posf * prev
uInt prev_length
IPos prev_match
ulg static_len
int status
int strategy
z_streamp strm
uInt strstart
union internal_state sub
union internal_state sub
uInt w_bits
uInt w_mask
uInt w_size
uInt wbits
Bytef * window
ulg window_size

Define Documentation

#define bits   word.what.Bits

Definition at line 25 of file inftrees.c.

#define BMAX   15 /* maximum bit length of any code */

Definition at line 91 of file inftrees.c.

#define BUILDFIXED   /* non ANSI compilers may not accept inffixed.h */

Definition at line 10 of file inftrees.c.

#define C0   *p++ = 0;
#define C2   C0 C0 C0 C0
#define C4   C2 C2 C2 C2
#define exop   word.what.Exop

Definition at line 24 of file inftrees.c.

#define FIXEDH   544 /* number of hufts used by fixed tables */

Definition at line 387 of file inftrees.c.


Function Documentation

local int huft_build ( uIntf b,
uInt  n,
uInt  s,
const uIntf d,
const uIntf e,
inflate_huft * FAR t,
uIntf m,
inflate_huft *  hp,
uInt hn,
uIntf v 
)

Definition at line 93 of file inftrees.c.

{

  uInt a;                       /* counter for codes of length k */
  uInt c[BMAX+1];               /* bit length count table */
  uInt f;                       /* i repeats in table every f entries */
  int g;                        /* maximum code length */
  int h;                        /* table level */
  register uInt i;              /* counter, current code */
  register uInt j;              /* counter */
  register int k;               /* number of bits in current code */
  int l;                        /* bits per table (returned in m) */
  uInt mask;                    /* (1 << w) - 1, to avoid cc -O bug on HP */
  register uIntf *p;            /* pointer into c[], b[], or v[] */
  inflate_huft *q;              /* points to current table */
  struct inflate_huft_s r;      /* table entry for structure assignment */
  inflate_huft *u[BMAX];        /* table stack */
  register int w;               /* bits before this table == (l * h) */
  uInt x[BMAX+1];               /* bit offsets, then code stack */
  uIntf *xp;                    /* pointer into x */
  int y;                        /* number of dummy codes added */
  uInt z;                       /* number of entries in current table */


  /* Generate counts for each bit length */
  p = c;
#define C0 *p++ = 0;
#define C2 C0 C0 C0 C0
#define C4 C2 C2 C2 C2
  C4                            /* clear c[]--assume BMAX+1 is 16 */
  p = b;  i = n;
  do {
    c[*p++]++;                  /* assume all entries <= BMAX */
  } while (--i);
  if (c[0] == n)                /* null input--all zero length codes */
  {
    *t = (inflate_huft *)Z_NULL;
    *m = 0;
    return Z_OK;
  }


  /* Find minimum and maximum length, bound *m by those */
  l = *m;
  for (j = 1; j <= BMAX; j++)
    if (c[j])
      break;
  k = j;                        /* minimum code length */
  if ((uInt)l < j)
    l = j;
  for (i = BMAX; i; i--)
    if (c[i])
      break;
  g = i;                        /* maximum code length */
  if ((uInt)l > i)
    l = i;
  *m = l;


  /* Adjust last length count to fill out codes, if needed */
  for (y = 1 << j; j < i; j++, y <<= 1)
    if ((y -= c[j]) < 0)
      return Z_DATA_ERROR;
  if ((y -= c[i]) < 0)
    return Z_DATA_ERROR;
  c[i] += y;


  /* Generate starting offsets into the value table for each length */
  x[1] = j = 0;
  p = c + 1;  xp = x + 2;
  while (--i) {                 /* note that i == g from above */
    *xp++ = (j += *p++);
  }


  /* Make a table of values in order of bit lengths */
  p = b;  i = 0;
  do {
    if ((j = *p++) != 0)
      v[x[j]++] = i;
  } while (++i < n);
  n = x[g];                     /* set n to length of v */


  /* Generate the Huffman codes and for each, make the table entries */
  x[0] = i = 0;                 /* first Huffman code is zero */
  p = v;                        /* grab values in bit order */
  h = -1;                       /* no tables yet--level -1 */
  w = -l;                       /* bits decoded == (l * h) */
  u[0] = (inflate_huft *)Z_NULL;        /* just to keep compilers happy */
  q = (inflate_huft *)Z_NULL;   /* ditto */
  z = 0;                        /* ditto */

  /* go through the bit lengths (k already is bits in shortest code) */
  for (; k <= g; k++)
  {
    a = c[k];
    while (a--)
    {
      /* here i is the Huffman code of length k bits for value *p */
      /* make tables up to required level */
      while (k > w + l)
      {
        h++;
        w += l;                 /* previous table always l bits */

        /* compute minimum size table less than or equal to l bits */
        z = g - w;
        z = z > (uInt)l ? l : z;        /* table size upper limit */
        if ((f = 1 << (j = k - w)) > a + 1)     /* try a k-w bit table */
        {                       /* too few codes for k-w bit table */
          f -= a + 1;           /* deduct codes from patterns left */
          xp = c + k;
          if (j < z)
            while (++j < z)     /* try smaller tables up to z bits */
            {
              if ((f <<= 1) <= *++xp)
                break;          /* enough codes to use up j bits */
              f -= *xp;         /* else deduct codes from patterns */
            }
        }
        z = 1 << j;             /* table entries for j-bit table */

        /* allocate new table */
        if (*hn + z > MANY)     /* (note: doesn't matter for fixed) */
          return Z_DATA_ERROR;  /* overflow of MANY */
        u[h] = q = hp + *hn;
        *hn += z;

        /* connect to last table, if there is one */
        if (h)
        {
          x[h] = i;             /* save pattern for backing up */
          r.bits = (Byte)l;     /* bits to dump before this table */
          r.exop = (Byte)j;     /* bits in this table */
          j = i >> (w - l);
          r.base = (uInt)(q - u[h-1] - j);   /* offset to this table */
          u[h-1][j] = r;        /* connect to last table */
        }
        else
          *t = q;               /* first table is returned result */
      }

      /* set up table entry in r */
      r.bits = (Byte)(k - w);
      if (p >= v + n)
        r.exop = 128 + 64;      /* out of values--invalid code */
      else if (*p < s)
      {
        r.exop = (Byte)(*p < 256 ? 0 : 32 + 64);     /* 256 is end-of-block */
        r.base = *p++;          /* simple code is just the value */
      }
      else
      {
        r.exop = (Byte)(e[*p - s] + 16 + 64);/* non-simple--look up in lists */
        r.base = d[*p++ - s];
      }

      /* fill code-like entries with r */
      f = 1 << (k - w);
      for (j = i >> w; j < z; j += f)
        q[j] = r;

      /* backwards increment the k-bit code i */
      for (j = 1 << (k - 1); i & j; j >>= 1)
        i ^= j;
      i ^= j;

      /* backup over finished tables */
      mask = (1 << w) - 1;      /* needed on HP, cc -O bug */
      while ((i & mask) != x[h])
      {
        h--;                    /* don't need to update q */
        w -= l;
        mask = (1 << w) - 1;
      }
    }
  }


  /* Return Z_BUF_ERROR if we were given an incomplete table */
  return y != 0 && g != 1 ? Z_BUF_ERROR : Z_OK;
}

Here is the call graph for this function:

int inflate_trees_bits ( uIntf c,
uIntf bb,
inflate_huft * FAR tb,
inflate_huft *  hp,
z_streamp  z 
)

Definition at line 293 of file inftrees.c.

{
  int r;
  uInt hn = 0;          /* hufts used in space */
  uIntf *v;             /* work area for huft_build */

  if ((v = (uIntf*)ZALLOC(z, 19, sizeof(uInt))) == Z_NULL)
    return Z_MEM_ERROR;
  r = huft_build(c, 19, 19, (uIntf*)Z_NULL, (uIntf*)Z_NULL,
                 tb, bb, hp, &hn, v);
  if (r == Z_DATA_ERROR)
    z->msg = (char*)"oversubscribed dynamic bit lengths tree";
  else if (r == Z_BUF_ERROR || *bb == 0)
  {
    z->msg = (char*)"incomplete dynamic bit lengths tree";
    r = Z_DATA_ERROR;
  }
  ZFREE(z, v);
  return r;
}

Here is the call graph for this function:

int inflate_trees_dynamic ( uInt  nl,
uInt  nd,
uIntf c,
uIntf bl,
uIntf bd,
inflate_huft * FAR tl,
inflate_huft * FAR td,
inflate_huft *  hp,
z_streamp  z 
)

Definition at line 320 of file inftrees.c.

{
  int r;
  uInt hn = 0;          /* hufts used in space */
  uIntf *v;             /* work area for huft_build */

  /* allocate work area */
  if ((v = (uIntf*)ZALLOC(z, 288, sizeof(uInt))) == Z_NULL)
    return Z_MEM_ERROR;

  /* build literal/length tree */
  r = huft_build(c, nl, 257, cplens, cplext, tl, bl, hp, &hn, v);
  if (r != Z_OK || *bl == 0)
  {
    if (r == Z_DATA_ERROR)
      z->msg = (char*)"oversubscribed literal/length tree";
    else if (r != Z_MEM_ERROR)
    {
      z->msg = (char*)"incomplete literal/length tree";
      r = Z_DATA_ERROR;
    }
    ZFREE(z, v);
    return r;
  }

  /* build distance tree */
  r = huft_build(c + nl, nd, 0, cpdist, cpdext, td, bd, hp, &hn, v);
  if (r != Z_OK || (*bd == 0 && nl > 257))
  {
    if (r == Z_DATA_ERROR)
      z->msg = (char*)"oversubscribed distance tree";
    else if (r == Z_BUF_ERROR) {
#ifdef PKZIP_BUG_WORKAROUND
      r = Z_OK;
    }
#else
      z->msg = (char*)"incomplete distance tree";
      r = Z_DATA_ERROR;
    }
    else if (r != Z_MEM_ERROR)
    {
      z->msg = (char*)"empty distance tree with lengths";
      r = Z_DATA_ERROR;
    }
    ZFREE(z, v);
    return r;
#endif
  }

  /* done */
  ZFREE(z, v);
  return Z_OK;
}

Here is the call graph for this function:

int inflate_trees_fixed ( uIntf bl,
uIntf bd,
inflate_huft * FAR tl,
inflate_huft * FAR td,
z_streamp  z 
)

Definition at line 398 of file inftrees.c.

{
#ifdef BUILDFIXED
  /* build fixed tables if not already */
  if (!fixed_built)
  {
    int k;              /* temporary variable */
    uInt f = 0;         /* number of hufts used in fixed_mem */
    uIntf *c;           /* length list for huft_build */
    uIntf *v;           /* work area for huft_build */

    /* allocate memory */
    if ((c = (uIntf*)ZALLOC(z, 288, sizeof(uInt))) == Z_NULL)
      return Z_MEM_ERROR;
    if ((v = (uIntf*)ZALLOC(z, 288, sizeof(uInt))) == Z_NULL)
    {
      ZFREE(z, c);
      return Z_MEM_ERROR;
    }

    /* literal table */
    for (k = 0; k < 144; k++)
      c[k] = 8;
    for (; k < 256; k++)
      c[k] = 9;
    for (; k < 280; k++)
      c[k] = 7;
    for (; k < 288; k++)
      c[k] = 8;
    fixed_bl = 9;
    huft_build(c, 288, 257, cplens, cplext, &fixed_tl, &fixed_bl,
               fixed_mem, &f, v);

    /* distance table */
    for (k = 0; k < 30; k++)
      c[k] = 5;
    fixed_bd = 5;
    huft_build(c, 30, 0, cpdist, cpdext, &fixed_td, &fixed_bd,
               fixed_mem, &f, v);

    /* done */
    ZFREE(z, v);
    ZFREE(z, c);
    fixed_built = 1;
  }
#endif
  *bl = fixed_bl;
  *bd = fixed_bd;
  *tl = fixed_tl;
  *td = fixed_td;
  return Z_OK;
}

Here is the call graph for this function:

local int huft_build OF ( (uIntf *, uInt, uInt, const uIntf *, const uIntf *, inflate_huft *FAR *, uIntf *, inflate_huft *, uInt *, uIntf *)  )

Variable Documentation

Initial value:
 { 
        0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
        7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
        12, 12, 13, 13}

Definition at line 52 of file inftrees.c.

Initial value:
 { 
        1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
        257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
        8193, 12289, 16385, 24577}

Definition at line 48 of file inftrees.c.

Initial value:
 { 
        3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
        35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0}

Definition at line 41 of file inftrees.c.

Initial value:
 { 
        0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
        3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 112, 112}

Definition at line 45 of file inftrees.c.

Definition at line 390 of file inftrees.c.

Definition at line 389 of file inftrees.c.

Definition at line 386 of file inftrees.c.

local inflate_huft fixed_mem[FIXEDH]

Definition at line 388 of file inftrees.c.

local inflate_huft* fixed_td

Definition at line 392 of file inftrees.c.

local inflate_huft* fixed_tl

Definition at line 391 of file inftrees.c.

const char inflate_copyright[] = " inflate 1.1.4 Copyright 1995-2002 Mark Adler "

Definition at line 13 of file inftrees.c.