home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 October / Chip_2001-10_cd1.bin / zkuste / delphi / nastroje / d23456 / NSIS.EXE / Source / zlib / INFCODES.C < prev    next >
C/C++ Source or Header  |  2001-05-19  |  8KB  |  245 lines

  1. /* infcodes.c -- process literals and length/distance pairs
  2.  * Copyright (C) 1995-1998 Mark Adler
  3.  * For conditions of distribution and use, see copyright notice in zlib.h 
  4.  */
  5.  
  6. #include "zutil.h"
  7. #include "inftrees.h"
  8. #include "infblock.h"
  9. #include "infcodes.h"
  10. #include "infutil.h"
  11.  
  12.  
  13. /* simplify the use of the inflate_huft type with some defines */
  14. #define exop word.what.Exop
  15. #define bits word.what.Bits
  16.  
  17. typedef enum {        /* waiting for "i:"=input, "o:"=output, "x:"=nothing */
  18.       START,    /* x: set up for LEN */
  19.       LEN,      /* i: get length/literal/eob next */
  20.       LENEXT,   /* i: getting length extra (have base) */
  21.       DIST,     /* i: get distance next */
  22.       DISTEXT,  /* i: getting distance extra */
  23.       COPY,     /* o: copying bytes in window, waiting for space */
  24.       LIT,      /* o: got literal, waiting for output space */
  25.       WASH,     /* o: got eob, possibly still output waiting */
  26.       END,      /* x: got eob and all data flushed */
  27.       BADCODE}  /* x: got error */
  28. inflate_codes_mode;
  29.  
  30. /* inflate codes private state */
  31. struct inflate_codes_state {
  32.  
  33.   /* mode */
  34.   inflate_codes_mode mode;      /* current inflate_codes mode */
  35.  
  36.   /* mode dependent information */
  37.   uInt len;
  38.   union {
  39.     struct {
  40.       inflate_huft *tree;       /* pointer into tree */
  41.       uInt need;                /* bits needed */
  42.     } code;             /* if LEN or DIST, where in tree */
  43.     uInt lit;           /* if LIT, literal */
  44.     struct {
  45.       uInt get;                 /* bits to get for extra */
  46.       uInt dist;                /* distance back to copy from */
  47.     } copy;             /* if EXT or COPY, where and how much */
  48.   } sub;                /* submode */
  49.  
  50.   /* mode independent information */
  51.   Byte lbits;           /* ltree bits decoded per branch */
  52.   Byte dbits;           /* dtree bits decoder per branch */
  53.   inflate_huft *ltree;          /* literal/length/eob tree */
  54.   inflate_huft *dtree;          /* distance tree */
  55.  
  56. };
  57.  
  58.  
  59. inflate_codes_statef *inflate_codes_new(bl, bd, tl, td, z)
  60. uInt bl, bd;
  61. inflate_huft *tl;
  62. inflate_huft *td; /* need separate declaration for Borland C++ */
  63. z_streamp z;
  64. {
  65.   inflate_codes_statef *c;
  66.  
  67.   if ((c = (inflate_codes_statef *)
  68.        ZALLOC(z,1,sizeof(struct inflate_codes_state))) != Z_NULL)
  69.   {
  70.     c->mode = START;
  71.     c->lbits = (Byte)bl;
  72.     c->dbits = (Byte)bd;
  73.     c->ltree = tl;
  74.     c->dtree = td;
  75.     Tracev((stderr, "inflate:       codes new\n"));
  76.   }
  77.   return c;
  78. }
  79.  
  80.  
  81. int inflate_codes(s, z, r)
  82. inflate_blocks_statef *s;
  83. z_streamp z;
  84. int r;
  85. {
  86.   uInt j;               /* temporary storage */
  87.   inflate_huft *t;      /* temporary pointer */
  88.   uInt e;               /* extra bits or operation */
  89.   uLong b;              /* bit buffer */
  90.   uInt k;               /* bits in bit buffer */
  91.   Bytef *p;             /* input data pointer */
  92.   uInt n;               /* bytes available there */
  93.   Bytef *q;             /* output window write pointer */
  94.   uInt m;               /* bytes to end of window or read pointer */
  95.   Bytef *f;             /* pointer to copy strings from */
  96.   inflate_codes_statef *c = s->sub.decode.codes;  /* codes state */
  97.  
  98.   /* copy input/output information to locals (UPDATE macro restores) */
  99.   LOAD
  100.  
  101.   /* process input and output based on current state */
  102.   while (1) switch (c->mode)
  103.   {             /* waiting for "i:"=input, "o:"=output, "x:"=nothing */
  104.     case START:         /* x: set up for LEN */
  105.       c->sub.code.need = c->lbits;
  106.       c->sub.code.tree = c->ltree;
  107.       c->mode = LEN;
  108.     case LEN:           /* i: get length/literal/eob next */
  109.       j = c->sub.code.need;
  110.       NEEDBITS(j)
  111.       t = c->sub.code.tree + ((uInt)b & (uInt)inflate_mask[j]);
  112.       DUMPBITS(t->bits)
  113.       e = (uInt)(t->exop);
  114.       if (e == 0)               /* literal */
  115.       {
  116.         c->sub.lit = t->base;
  117.         Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
  118.                  "inflate:         literal '%c'\n" :
  119.                  "inflate:         literal 0x%02x\n", t->base));
  120.         c->mode = LIT;
  121.         break;
  122.       }
  123.       if (e & 16)               /* length */
  124.       {
  125.         c->sub.copy.get = e & 15;
  126.         c->len = t->base;
  127.         c->mode = LENEXT;
  128.         break;
  129.       }
  130.       if ((e & 64) == 0)        /* next table */
  131.       {
  132.         c->sub.code.need = e;
  133.         c->sub.code.tree = t + t->base;
  134.         break;
  135.       }
  136.       if (e & 32)               /* end of block */
  137.       {
  138.         Tracevv((stderr, "inflate:         end of block\n"));
  139.         c->mode = WASH;
  140.         break;
  141.       }
  142.       c->mode = BADCODE;        /* invalid code */
  143. //      z->msg = (char*)"err";//invalid literal/length code";
  144.       r = Z_DATA_ERROR;
  145.       LEAVE
  146.     case LENEXT:        /* i: getting length extra (have base) */
  147.       j = c->sub.copy.get;
  148.       NEEDBITS(j)
  149.       c->len += (uInt)b & (uInt)inflate_mask[j];
  150.       DUMPBITS(j)
  151.       c->sub.code.need = c->dbits;
  152.       c->sub.code.tree = c->dtree;
  153.       Tracevv((stderr, "inflate:         length %u\n", c->len));
  154.       c->mode = DIST;
  155.     case DIST:          /* i: get distance next */
  156.       j = c->sub.code.need;
  157.       NEEDBITS(j)
  158.       t = c->sub.code.tree + ((uInt)b & (uInt)inflate_mask[j]);
  159.       DUMPBITS(t->bits)
  160.       e = (uInt)(t->exop);
  161.       if (e & 16)               /* distance */
  162.       {
  163.         c->sub.copy.get = e & 15;
  164.         c->sub.copy.dist = t->base;
  165.         c->mode = DISTEXT;
  166.         break;
  167.       }
  168.       if ((e & 64) == 0)        /* next table */
  169.       {
  170.         c->sub.code.need = e;
  171.         c->sub.code.tree = t + t->base;
  172.         break;
  173.       }
  174.       c->mode = BADCODE;        /* invalid code */
  175. //      z->msg = (char*)"err";//invalid distance code";
  176.       r = Z_DATA_ERROR;
  177.       LEAVE
  178.     case DISTEXT:       /* i: getting distance extra */
  179.       j = c->sub.copy.get;
  180.       NEEDBITS(j)
  181.       c->sub.copy.dist += (uInt)b & (uInt)inflate_mask[j];
  182.       DUMPBITS(j)
  183.       Tracevv((stderr, "inflate:         distance %u\n", c->sub.copy.dist));
  184.       c->mode = COPY;
  185.     case COPY:          /* o: copying bytes in window, waiting for space */
  186. #ifndef __TURBOC__ /* Turbo C bug for following expression */
  187.       f = (uInt)(q - s->window) < c->sub.copy.dist ?
  188.           s->end - (c->sub.copy.dist - (q - s->window)) :
  189.           q - c->sub.copy.dist;
  190. #else
  191.       f = q - c->sub.copy.dist;
  192.       if ((uInt)(q - s->window) < c->sub.copy.dist)
  193.         f = s->end - (c->sub.copy.dist - (uInt)(q - s->window));
  194. #endif
  195.       while (c->len)
  196.       {
  197.         NEEDOUT
  198.         OUTBYTE(*f++)
  199.         if (f == s->end)
  200.           f = s->window;
  201.         c->len--;
  202.       }
  203.       c->mode = START;
  204.       break;
  205.     case LIT:           /* o: got literal, waiting for output space */
  206.       NEEDOUT
  207.       OUTBYTE(c->sub.lit)
  208.       c->mode = START;
  209.       break;
  210.     case WASH:          /* o: got eob, possibly more output */
  211.       if (k > 7)        /* return unused byte, if any */
  212.       {
  213.         Assert(k < 16, "inflate_codes grabbed too many bytes")
  214.         k -= 8;
  215.         n++;
  216.         p--;            /* can always return one */
  217.       }
  218.       FLUSH
  219.       if (s->read != s->write)
  220.         LEAVE
  221.       c->mode = END;
  222.     case END:
  223.       r = Z_STREAM_END;
  224.       LEAVE
  225.     case BADCODE:       /* x: got error */
  226.       r = Z_DATA_ERROR;
  227.       LEAVE
  228.     default:
  229.       r = Z_STREAM_ERROR;
  230.       LEAVE
  231.   }
  232. #ifdef NEED_DUMMY_RETURN
  233.   return Z_STREAM_ERROR;  /* Some dumb compilers complain without this */
  234. #endif
  235. }
  236.  
  237.  
  238. void inflate_codes_free(c, z)
  239. inflate_codes_statef *c;
  240. z_streamp z;
  241. {
  242.   ZFREE(z, c);
  243.   Tracev((stderr, "inflate:       codes free\n"));
  244. }
  245.