home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 March / CMCD0304.ISO / Software / Freeware / Programare / nullsoft / nsis20.exe / Source / zlib / INFBLOCK.C < prev    next >
C/C++ Source or Header  |  2003-08-03  |  22KB  |  698 lines

  1. #include "../exehead/config.h"
  2. #ifdef NSIS_COMPRESS_USE_ZLIB
  3.  
  4. #include "zutil.h"
  5.  
  6.  
  7. /* defines for inflate input/output */
  8. /*   update pointers and return */
  9. #define UPDBITS {s->bitb=b;s->bitk=k;}
  10. #define UPDIN {z->avail_in=n;z->next_in=p;}
  11. #define UPDOUT {s->write=q;}
  12. #define UPDATE {UPDBITS UPDIN UPDOUT}
  13. #define LEAVE(r) {UPDATE inflate_flush(z); return r;}
  14.  
  15. /*   get bytes and bits */
  16. #define LOADIN {p=z->next_in;n=z->avail_in;b=s->bitb;k=s->bitk;}
  17.  
  18.  
  19. #define NEEDBYTE {if(!n)LEAVE(Z_OK)}
  20. #define NEXTBYTE (n--,*p++)
  21. #define NEEDBITS(j) {while(k<(j)){NEEDBYTE;b|=((uLong)NEXTBYTE)<<k;k+=8;}}
  22.  
  23. #define DUMPBITS(j) {b>>=(j);k-=(j);}
  24. /*   output bytes */
  25. #define WAVAIL (uInt)(q<s->read?s->read-q-1:s->end-q)
  26. #define LOADOUT {q=s->write;m=(uInt)WAVAIL;}
  27. #define WRAP {if(q==s->end&&s->read!=s->window){q=s->window;m=(uInt)WAVAIL;}}
  28. #define FLUSH {UPDOUT inflate_flush(z); LOADOUT}
  29. #define NEEDOUT {if(m==0){WRAP if(m==0){FLUSH WRAP if(m==0) LEAVE(Z_OK)}}}
  30. #define OUTBYTE(a) {*q++=(Byte)(a);m--;}
  31. /*   load local pointers */
  32. #define LOAD {LOADIN LOADOUT}
  33.  
  34. #define LAST (s->last == DRY)
  35.  
  36. #define FIXEDH 544      /* number of hufts used by fixed tables */
  37.  
  38.  
  39.  
  40. typedef struct inflate_blocks_state FAR inflate_blocks_statef;
  41. #define exop word.what.Exop
  42. #define bits word.what.Bits
  43.  
  44. /* And'ing with mask[n] masks the lower n bits */
  45. local unsigned short inflate_mask[17] = {
  46.     0x0000,
  47.     0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
  48.     0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
  49. };
  50. local const char border[] = { /* Order of the bit length code lengths */
  51.         16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
  52.  
  53. /* Tables for deflate from PKZIP's appnote.txt. */
  54. local const unsigned short  cplens[31] = { /* Copy lengths for literal codes 257..285 */
  55.         3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
  56.         35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
  57.         /* see note #13 above about 258 */
  58. local const unsigned short  cplext[31] = { /* Extra bits for literal codes 257..285 */
  59.         0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
  60.         3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 112, 112}; /* 112==invalid */
  61. local const unsigned short  cpdist[30] = { /* Copy offsets for distance codes 0..29 */
  62.         1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
  63.         257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
  64.         8193, 12289, 16385, 24577};
  65. local const unsigned short  cpdext[30] = { /* Extra bits for distance codes */
  66.         0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
  67.         7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
  68.         12, 12, 13, 13};
  69.  
  70. /* build fixed tables only once--keep them here */
  71. local char fixed_built = 0;
  72. local inflate_huft fixed_mem[FIXEDH];
  73. local uInt fixed_bl=9;
  74. local uInt fixed_bd=5;
  75. local inflate_huft *fixed_tl;
  76. local inflate_huft *fixed_td;
  77.  
  78. /* copy as much as possible from the sliding window to the output area */
  79. local void inflate_flush(z)
  80. z_streamp z;
  81. {
  82.   inflate_blocks_statef *s = &z->blocks;
  83.   uInt n;
  84.   Bytef *q;
  85.  
  86.   /* local copies of source and destination pointers */
  87.   q = s->read;
  88.  
  89. again:
  90.   /* compute number of bytes to copy as far as end of window */
  91.   n = (uInt)((q <= s->write ? s->write : s->end) - q);
  92.   n = min(n, z->avail_out);
  93.  
  94.   /* update counters */
  95.   z->avail_out -= n;
  96.   //z->total_out += n;
  97.  
  98.   /* copy as far as end of window */
  99.   zmemcpy(z->next_out, q, n);
  100.   z->next_out += n;
  101.   q += n;
  102.  
  103.   /* see if more to copy at beginning of window */
  104.   if (q == s->end)
  105.   {
  106.     /* wrap pointers */
  107.     q = s->window;
  108.     if (s->write == s->end)
  109.       s->write = s->window;
  110.  
  111.     /* do the same for the beginning of the window */
  112.     goto again;
  113.   }
  114.  
  115.   /* update pointers */
  116.   s->read = q;
  117. }
  118.  
  119. #define BMAX 15         /* maximum bit length of any code */
  120.  
  121. local int huft_build(
  122. uIntf *b,               /* code lengths in bits (all assumed <= BMAX) */
  123. uInt n,                 /* number of codes (assumed <= 288) */
  124. uInt s,                 /* number of simple-valued codes (0..s-1) */
  125. const unsigned short *d,         /* list of base values for non-simple codes */
  126. const unsigned short *e,         /* list of extra bits for non-simple codes */
  127. inflate_huft * FAR *t,  /* result: starting table */
  128. uIntf *m,               /* maximum lookup bits, returns actual */
  129. inflate_huft *hp,       /* space for trees */
  130. uInt *hn)               /* working area: values in order of bit length */
  131. {
  132.   static uIntf v[288];             /* work area for huft_build */
  133.   uInt a;                       /* counter for codes of length k */
  134.   uInt c[BMAX+1];               /* bit length count table */
  135.   uInt f;                       /* i repeats in table every f entries */
  136.   int g;                        /* maximum code length */
  137.   int h;                        /* table level */
  138.   uInt i;              /* counter, current code */
  139.   uInt j;              /* counter */
  140.   int k;               /* number of bits in current code */
  141.   int l;                        /* bits per table (returned in m) */
  142.   uIntf *p;            /* pointer into c[], b[], or v[] */
  143.   inflate_huft *q;              /* points to current table */
  144.   struct inflate_huft_s r;      /* table entry for structure assignment */
  145.   inflate_huft *u[BMAX];        /* table stack */
  146.   int w;               /* bits before this table == (l * h) */
  147.   uInt x[BMAX+1];               /* bit offsets, then code stack */
  148.   uIntf *xp;                    /* pointer into x */
  149.   int y;                        /* number of dummy codes added */
  150.   uInt z;                       /* number of entries in current table */
  151.  
  152.  
  153.   /* Generate counts for each bit length */
  154.   p=c;
  155.   y=16; while (y--) *p++ = 0;
  156.   p = b;
  157.   i = n;
  158.   do {
  159.     c[*p++]++;                  /* assume all entries <= BMAX */
  160.   } while (--i);
  161.   if (c[0] == n)                /* null input--all zero length codes */
  162.   {
  163.     *t = (inflate_huft *)Z_NULL;
  164.     *m = 0;
  165.     return Z_OK;
  166.   }
  167.  
  168.  
  169.   /* Find minimum and maximum length, bound *m by those */
  170.   l = *m;
  171.   for (j = 1; j <= BMAX; j++)
  172.     if (c[j])
  173.       break;
  174.   k = j;                        /* minimum code length */
  175.   if ((uInt)l < j)
  176.     l = j;
  177.   for (i = BMAX; i; i--)
  178.     if (c[i])
  179.       break;
  180.   g = i;                        /* maximum code length */
  181.   if ((uInt)l > i)
  182.     l = i;
  183.   *m = l;
  184.  
  185.  
  186.   /* Adjust last length count to fill out codes, if needed */
  187.   for (y = 1 << j; j < i; j++, y <<= 1)
  188.     if ((y -= c[j]) < 0)
  189.       return Z_DATA_ERROR;
  190.   if ((y -= c[i]) < 0)
  191.     return Z_DATA_ERROR;
  192.   c[i] += y;
  193.  
  194.  
  195.   /* Generate starting offsets into the value table for each length */
  196.   x[1] = j = 0;
  197.   p = c + 1;  xp = x + 2;
  198.   while (--i) {                 /* note that i == g from above */
  199.     *xp++ = (j += *p++);
  200.   }
  201.  
  202.  
  203.   /* Make a table of values in order of bit lengths */
  204.   p = b;  i = 0;
  205.   do {
  206.     if ((j = *p++) != 0)
  207.       v[x[j]++] = i;
  208.   } while (++i < n);
  209.   n = x[g];                     /* set n to length of v */
  210.  
  211.  
  212.   /* Generate the Huffman codes and for each, make the table entries */
  213.   x[0] = i = 0;                 /* first Huffman code is zero */
  214.   p = v;                        /* grab values in bit order */
  215.   h = -1;                       /* no tables yet--level -1 */
  216.   w = -l;                       /* bits decoded == (l * h) */
  217.   u[0] = (inflate_huft *)Z_NULL;        /* just to keep compilers happy */
  218.   q = (inflate_huft *)Z_NULL;   /* ditto */
  219.   z = 0;                        /* ditto */
  220.  
  221.   /* go through the bit lengths (k already is bits in shortest code) */
  222.   for (; k <= g; k++)
  223.   {
  224.     a = c[k];
  225.     while (a--)
  226.     {
  227.       int nextw=w;
  228.       /* here i is the Huffman code of length k bits for value *p */
  229.       /* make tables up to required level */
  230.       while (k > (nextw=w + l))
  231.       {
  232.         h++;
  233.  
  234.         /* compute minimum size table less than or equal to l bits */
  235.         z = g - nextw;
  236.         z = z > (uInt)l ? l : z;        /* table size upper limit */
  237.         if ((f = 1 << (j = k - nextw)) > a + 1)     /* try a k-w bit table */
  238.         {                       /* too few codes for k-w bit table */
  239.           f -= a + 1;           /* deduct codes from patterns left */
  240.           xp = c + k;
  241.           if (j < z)
  242.             while (++j < z && (f <<= 1) > *++xp)     /* try smaller tables up to z bits */
  243.             {
  244.               f -= *xp;         /* else deduct codes from patterns */
  245.             }
  246.         }
  247.         z = 1 << j;             /* table entries for j-bit table */
  248.  
  249.         /* allocate new table */
  250.         if (*hn + z > MANY)     /* (note: doesn't matter for fixed) */
  251.           return Z_MEM_ERROR;   /* not enough memory */
  252.         u[h] = q = hp + *hn;
  253.         *hn += z;
  254.  
  255.         /* connect to last table, if there is one */
  256.         if (h)
  257.         {
  258.           x[h] = i;             /* save pattern for backing up */
  259.           r.bits = (Byte)l;     /* bits to dump before this table */
  260.           r.exop = (Byte)j;     /* bits in this table */
  261.           j = i >> w;
  262.           r.base = (uInt)(q - u[h-1] - j);   /* offset to this table */
  263.           u[h-1][j] = r;        /* connect to last table */
  264.         }
  265.         else
  266.           *t = q;               /* first table is returned result */
  267.         w=nextw;                 /* previous table always l bits */
  268.       }
  269.  
  270.       /* set up table entry in r */
  271.       r.bits = (Byte)(k - w);
  272.       if (p >= v + n)
  273.         r.exop = 128 + 64;      /* out of values--invalid code */
  274.       else if (*p < s)
  275.       {
  276.         r.exop = (Byte)(*p < 256 ? 0 : 32 + 64);     /* 256 is end-of-block */
  277.         r.base = *p++;          /* simple code is just the value */
  278.       }
  279.       else
  280.       {
  281.         r.exop = (Byte)(e[*p - s] + 16 + 64);/* non-simple--look up in lists */
  282.         r.base = d[*p++ - s];
  283.       }
  284.  
  285.       /* fill code-like entries with r */
  286.       f = 1 << (k - w);
  287.       for (j = i >> w; j < z; j += f)
  288.         q[j] = r;
  289.  
  290.       /* backwards increment the k-bit code i */
  291.       for (j = 1 << (k - 1); i & j; j >>= 1)
  292.         i ^= j;
  293.       i ^= j;
  294.  
  295.       /* backup over finished tables */
  296.       while ((i & ((1 << w) - 1)) != x[h])
  297.       {
  298.         h--;                    /* don't need to update q */
  299.         w -= l;
  300.       }
  301.     }
  302.   }
  303.  
  304.  
  305.   /* Return Z_BUF_ERROR if we were given an incomplete table */
  306.   return (y != 0 && g != 1) ? Z_BUF_ERROR : Z_OK;
  307. }
  308.  
  309. int inflate(z_streamp z)
  310. {
  311.   inflate_blocks_statef *s = &z->blocks;
  312.   inflate_codes_statef *c = &s->sub.decode.t_codes;  /* codes state */
  313.  
  314.   // lousy two bytes saved by doing this
  315.   struct
  316.   {
  317.     uInt t;               /* temporary storage */
  318.     uLong b;              /* bit buffer */
  319.     uInt k;               /* bits in bit buffer */
  320.     Bytef *p;             /* input data pointer */
  321.     uInt n;               /* bytes available there */
  322.     Bytef *q;             /* output window write pointer */
  323.     uInt m;               /* bytes to end of window or read pointer */
  324.  
  325.     /* CODES variables */
  326.  
  327.     inflate_huft *j;      /* temporary pointer */
  328.     uInt e;               /* extra bits or operation */
  329.     Bytef *f;             /* pointer to copy strings from */
  330.   } _state;
  331.  
  332. #define t _state.t
  333. #define b _state.b
  334. #define k _state.k
  335. #define p _state.p
  336. #define n _state.n
  337. #define q _state.q
  338. #define m _state.m
  339.  
  340.   /* copy input/output information to locals (UPDATE macro restores) */
  341.   LOAD
  342.  
  343.   /* process input based on current state */
  344.   for (;;) switch (s->mode)
  345.   {
  346.     case TYPE:
  347.       NEEDBITS(3)
  348.       t = (uInt)b & 7;
  349.       DUMPBITS(3)
  350.       s->last = (t & 1) ? DRY : TYPE;
  351.       switch (t >> 1)
  352.       {
  353.         case 0:                         /* stored */
  354.           Tracev((stderr, "inflate:     stored block%s\n",
  355.                  LAST ? " (last)" : ""));
  356.           DUMPBITS(k&7)
  357.           s->mode = LENS;               /* get length of stored block */
  358.           break;
  359.         case 1:                         /* fixed */
  360.           Tracev((stderr, "inflate:     fixed codes block%s\n",
  361.                  LAST ? " (last)" : ""));
  362.           {
  363.             if (!fixed_built)
  364.             {
  365.               int _k;              /* temporary variable */
  366.               uInt f = 0;         /* number of hufts used in fixed_mem */
  367.               static uIntf c[288];           /* length list for huft_build */
  368.  
  369.               /* literal table */
  370.               for (_k = 0; _k < 288; _k++) 
  371.               {
  372.                 char v=8;
  373.                 if (_k > 143)
  374.                 {
  375.                   if (_k < 256) v++;
  376.                   else if (_k < 280) v--;
  377.                 }
  378.                 c[_k] = v;
  379.               }
  380.  
  381.               huft_build(c, 288, 257, cplens, cplext, &fixed_tl, &fixed_bl, fixed_mem, &f);
  382.  
  383.               /* distance table */
  384.               for (_k = 0; _k < 30; _k++) c[_k] = 5;
  385.  
  386.               huft_build(c, 30, 0, cpdist, cpdext, &fixed_td, &fixed_bd, fixed_mem, &f);
  387.  
  388.               /* done */
  389.               fixed_built++;
  390.             }
  391.  
  392.             //s->sub.decode.t_codes.mode = CODES_START;
  393.             s->sub.decode.t_codes.lbits = (Byte)fixed_bl;
  394.             s->sub.decode.t_codes.dbits = (Byte)fixed_bd;
  395.             s->sub.decode.t_codes.ltree = fixed_tl;
  396.             s->sub.decode.t_codes.dtree = fixed_td;
  397.           }
  398.           s->mode = CODES_START;
  399.           break;
  400.         case 2:                         /* dynamic */
  401.           Tracev((stderr, "inflate:     dynamic codes block%s\n",
  402.                  LAST ? " (last)" : ""));
  403.           s->mode = TABLE;
  404.           break;
  405.         case 3:                         /* illegal */
  406.           /* the only illegal value possible is 3 because we check only 2 bits */
  407.           goto bad;
  408.       }
  409.       break;
  410.     case LENS:
  411.       NEEDBITS(16)
  412.       s->sub.left = (uInt)b & 0xffff;
  413.       b = k = 0;                      /* dump bits */
  414.       Tracev((stderr, "inflate:       stored length %u\n", s->sub.left));
  415.       s->mode = s->sub.left ? STORED : s->last;
  416.       break;
  417.     case STORED:
  418.     {
  419.       uInt mn;
  420.  
  421.       if (n == 0)
  422.         LEAVE(Z_OK)
  423.       NEEDOUT
  424.       mn = min(m, n);
  425.       t = min(s->sub.left, mn);
  426.       zmemcpy(q, p, t);
  427.       p += t;  n -= t;
  428.       q += t;  m -= t;
  429.       if (!(s->sub.left -= t))
  430.         s->mode = s->last;
  431.       break;
  432.     }
  433.     case TABLE:
  434.       NEEDBITS(14)
  435.       s->sub.trees.table = t = (uInt)b & 0x3fff;
  436.       if ((t & 0x1f) > 29 || ((t >> 5) & 0x1f) > 29)
  437.       {
  438.         s->mode = BAD;
  439.         LEAVE(Z_DATA_ERROR);
  440.       }
  441.       //t = 258 + (t & 0x1f) + ((t >> 5) & 0x1f);
  442.       DUMPBITS(14)
  443.       s->sub.trees.index = 0;
  444.       Tracev((stderr, "inflate:       table sizes ok\n"));
  445.       s->mode = BTREE;
  446.     case BTREE:
  447.       while (s->sub.trees.index < 4 + (s->sub.trees.table >> 10))
  448.       {
  449.         NEEDBITS(3)
  450.         s->sub.trees.t_blens[border[s->sub.trees.index++]] = (uInt)b & 7;
  451.         DUMPBITS(3)
  452.       }
  453.       while (s->sub.trees.index < 19)
  454.         s->sub.trees.t_blens[border[s->sub.trees.index++]] = 0;
  455.       s->sub.trees.bb = 7;
  456.  
  457.       {
  458.         uInt hn = 0;          /* hufts used in space */
  459.  
  460.         t = huft_build(s->sub.trees.t_blens, 19, 19, (short *)Z_NULL, (short*)Z_NULL,
  461.                  &s->sub.trees.tb, &s->sub.trees.bb, s->hufts, &hn);
  462.         if (t != Z_OK || !s->sub.trees.bb)
  463.         {
  464.           s->mode = BAD;
  465.           break;
  466.         }
  467.       }
  468.  
  469.       s->sub.trees.index = 0;
  470.       Tracev((stderr, "inflate:       bits tree ok\n"));
  471.       s->mode = DTREE;
  472.     case DTREE:
  473.       while (t = s->sub.trees.table,
  474.              s->sub.trees.index < 258 + (t & 0x1f) + ((t >> 5) & 0x1f))
  475.       {
  476.         inflate_huft *h;
  477.         uInt i, j, c;
  478.  
  479.         t = s->sub.trees.bb;
  480.         NEEDBITS(t)
  481.         h = s->sub.trees.tb + ((uInt)b & (uInt)inflate_mask[t]);
  482.         t = h->bits;
  483.         c = h->base;
  484.         if (c < 16)
  485.         {
  486.           DUMPBITS(t)
  487.           s->sub.trees.t_blens[s->sub.trees.index++] = c;
  488.         }
  489.         else /* c == 16..18 */
  490.         {
  491.           if (c == 18)
  492.           {
  493.             i=7;
  494.             j=11;          
  495.           }
  496.           else
  497.           {
  498.             i=c-14;
  499.             j=3;
  500.           }
  501.           NEEDBITS(t+i)
  502.           DUMPBITS(t)
  503.           j += (uInt)b & (uInt)inflate_mask[i];
  504.           DUMPBITS(i)
  505.           i = s->sub.trees.index;
  506.           t = s->sub.trees.table;
  507.           if (i + j > 258 + (t & 0x1f) + ((t >> 5) & 0x1f) ||
  508.               (c == 16 && i < 1))
  509.           {
  510.             s->mode = BAD;
  511.             LEAVE(Z_DATA_ERROR);
  512.           }
  513.           c = c == 16 ? s->sub.trees.t_blens[i - 1] : 0;
  514.           do {
  515.             s->sub.trees.t_blens[i++] = c;
  516.           } while (--j);
  517.           s->sub.trees.index = i;
  518.         }
  519.       }
  520.       s->sub.trees.tb = Z_NULL;
  521.       {
  522.         uInt hn = 0;          /* hufts used in space */
  523.         uInt bl, bd;
  524.         inflate_huft *tl, *td;
  525.         int nl,nd;
  526.         t = s->sub.trees.table;
  527.  
  528.         nl = 257 + (t & 0x1f);
  529.         nd = 1 + ((t >> 5) & 0x1f);
  530.         bl = 9;         /* must be <= 9 for lookahead assumptions */
  531.         bd = 6;         /* must be <= 9 for lookahead assumptions */
  532.  
  533.         t = huft_build(s->sub.trees.t_blens, nl, 257, cplens, cplext, &tl, &bl, s->hufts, &hn);
  534.         if (bl == 0) t = Z_DATA_ERROR;
  535.         if (t == Z_OK)
  536.         {
  537.           /* build distance tree */
  538.           t = huft_build(s->sub.trees.t_blens + nl, nd, 0, cpdist, cpdext, &td, &bd, s->hufts, &hn);
  539.         }
  540.         if (t != Z_OK || (bd == 0 && nl > 257))
  541.         {
  542.           s->mode = BAD;
  543.           LEAVE(Z_DATA_ERROR);
  544.         }
  545.         Tracev((stderr, "inflate:       trees ok\n"));
  546.  
  547.         //s->sub.decode.t_codes.mode = CODES_START;
  548.         s->sub.decode.t_codes.lbits = (Byte)bl;
  549.         s->sub.decode.t_codes.dbits = (Byte)bd;
  550.         s->sub.decode.t_codes.ltree = tl;
  551.         s->sub.decode.t_codes.dtree = td;
  552.       }
  553.       s->mode = CODES_START;
  554.  
  555. #define j (_state.j)
  556. #define e (_state.e)
  557. #define f (_state.f)
  558.  
  559.     /* waiting for "i:"=input, "o:"=output, "x:"=nothing */
  560.  
  561.     case CODES_START:         /* x: set up for LEN */
  562.       c->sub.code.need = c->lbits;
  563.       c->sub.code.tree = c->ltree;
  564.       s->mode = CODES_LEN;
  565.     case CODES_LEN:           /* i: get length/literal/eob next */
  566.       t = c->sub.code.need;
  567.       NEEDBITS(t)
  568.       j = c->sub.code.tree + ((uInt)b & (uInt)inflate_mask[t]);
  569.       DUMPBITS(j->bits)
  570.       e = (uInt)(j->exop);
  571.       if (e == 0)               /* literal */
  572.       {
  573.         c->sub.lit = j->base;
  574.         s->mode = CODES_LIT;
  575.         break;
  576.       }
  577.       if (e & 16)               /* length */
  578.       {
  579.         c->sub.copy.get = e & 15;
  580.         c->len = j->base;
  581.         s->mode = CODES_LENEXT;
  582.         break;
  583.       }
  584.       if ((e & 64) == 0)        /* next table */
  585.       {
  586.         c->sub.code.need = e;
  587.         c->sub.code.tree = j + j->base;
  588.         break;
  589.       }
  590.       if (e & 32)               /* end of block */
  591.       {
  592.         s->mode = CODES_WASH;
  593.         break;
  594.       }
  595.     goto bad;
  596.     case CODES_LENEXT:        /* i: getting length extra (have base) */
  597.       t = c->sub.copy.get;
  598.       NEEDBITS(t)
  599.       c->len += (uInt)b & (uInt)inflate_mask[t];
  600.       DUMPBITS(t)
  601.       c->sub.code.need = c->dbits;
  602.       c->sub.code.tree = c->dtree;
  603.       s->mode = CODES_DIST;
  604.     case CODES_DIST:          /* i: get distance next */
  605.       t = c->sub.code.need;
  606.       NEEDBITS(t)
  607.       j = c->sub.code.tree + ((uInt)b & (uInt)inflate_mask[t]);
  608.       DUMPBITS(j->bits)
  609.       e = (uInt)(j->exop);
  610.       if (e & 16)               /* distance */
  611.       {
  612.         c->sub.copy.get = e & 15;
  613.         c->sub.copy.dist = j->base;
  614.         s->mode = CODES_DISTEXT;
  615.         break;
  616.       }
  617.       if ((e & 64) == 0)        /* next table */
  618.       {
  619.         c->sub.code.need = e;
  620.         c->sub.code.tree = j + j->base;
  621.         break;
  622.       }
  623.       goto bad;        /* invalid code */
  624.     case CODES_DISTEXT:       /* i: getting distance extra */
  625.       t = c->sub.copy.get;
  626.       NEEDBITS(t)
  627.       c->sub.copy.dist += (uInt)b & (uInt)inflate_mask[t];
  628.       DUMPBITS(t)
  629.       s->mode = CODES_COPY;
  630.     case CODES_COPY:          /* o: copying bytes in window, waiting for space */
  631.       f = (uInt)(q - s->window) < c->sub.copy.dist ?
  632.           s->end - (c->sub.copy.dist - (q - s->window)) :
  633.           q - c->sub.copy.dist;
  634.  
  635.       while (c->len)
  636.       {
  637.         NEEDOUT
  638.         OUTBYTE(*f++)
  639.         if (f == s->end)
  640.           f = s->window;
  641.         c->len--;
  642.       }
  643.       s->mode = CODES_START;
  644.       break;
  645.     case CODES_LIT:           /* o: got literal, waiting for output space */
  646.       NEEDOUT
  647.       OUTBYTE(c->sub.lit)
  648.       s->mode = CODES_START;
  649.       break;
  650.     case CODES_WASH:          /* o: got eob, possibly more output */
  651.       if (k > 7)        /* return unused byte, if any */
  652.       {
  653.         k -= 8;
  654.         n++;
  655.         p--;            /* can always return one */
  656.       }
  657.       /* flushing will be done in DRY */
  658.  
  659. #undef j
  660. #undef e
  661. #undef f
  662.  
  663.     case DRY:
  664.       FLUSH
  665.       if (s->write != s->read)
  666.         LEAVE(Z_OK)
  667.       if (s->mode == CODES_WASH)
  668.       {
  669.         Tracev((stderr, "inflate:       codes end, %lu total out\n",
  670.                z->total_out + (q >= s->read ? q - s->read :
  671.                (s->end - s->read) + (q - s->window))));
  672.       }
  673.       /* DRY if last, TYPE if not */
  674.       s->mode = s->last;
  675.       if (s->mode == TYPE)
  676.         break;
  677.       LEAVE(Z_STREAM_END)
  678.     //case BAD:
  679.       //r = Z_DATA_ERROR;
  680.       //LEAVE
  681.     default: // we'll call Z_STREAM_ERROR if BAD anyway
  682.     bad:
  683.       s->mode = BAD;
  684.       LEAVE(Z_STREAM_ERROR)
  685.   }
  686. }
  687.  
  688. #undef t
  689. #undef b
  690. #undef k
  691. #undef p
  692. #undef n
  693. #undef q
  694. #undef m
  695.  
  696.  
  697. #endif
  698.