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

  1. /* inflate.c -- zlib interface to inflate modules
  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 "infblock.h"
  8.  
  9. struct inflate_blocks_state {int dummy;}; /* for buggy compilers */
  10.  
  11. typedef enum {
  12.       METHOD,   /* waiting for method byte */
  13.       FLAG,     /* waiting for flag byte */
  14.       DICT4,    /* four dictionary check bytes to go */
  15.       DICT3,    /* three dictionary check bytes to go */
  16.       DICT2,    /* two dictionary check bytes to go */
  17.       DICT1,    /* one dictionary check byte to go */
  18.       DICT0,    /* waiting for inflateSetDictionary */
  19.       BLOCKS,   /* decompressing blocks */
  20.       CHECK4,   /* four check bytes to go */
  21.       CHECK3,   /* three check bytes to go */
  22.       CHECK2,   /* two check bytes to go */
  23.       CHECK1,   /* one check byte to go */
  24.       DONE,     /* finished check, done */
  25.       BAD}      /* got an error--stay here */
  26. inflate_mode;
  27.  
  28. /* inflate private state */
  29. struct internal_state {
  30.  
  31.   /* mode */
  32.   inflate_mode  mode;   /* current inflate mode */
  33.  
  34.   /* mode dependent information */
  35.   union {
  36.     uInt method;        /* if FLAGS, method byte */
  37.     struct {
  38.       uLong was;                /* computed check value */
  39.       uLong need;               /* stream check value */
  40.     } check;            /* if CHECK, check values to compare */
  41.     uInt marker;        /* if BAD, inflateSync's marker bytes count */
  42.   } sub;        /* submode */
  43.  
  44.   /* mode independent information */
  45.   int  nowrap;          /* flag for no wrapper */
  46.   uInt wbits;           /* log2(window size)  (8..15, defaults to 15) */
  47.   inflate_blocks_statef 
  48.     *blocks;            /* current inflate_blocks state */
  49.  
  50. };
  51.  
  52.  
  53. int ZEXPORT inflateReset(z)
  54. z_streamp z;
  55. {
  56.   if (z == Z_NULL || z->state == Z_NULL)
  57.     return Z_STREAM_ERROR;
  58.   z->total_in = z->total_out = 0;
  59. //  z->msg = Z_NULL;
  60.   z->state->mode = z->state->nowrap ? BLOCKS : METHOD;
  61.   inflate_blocks_reset(z->state->blocks, z, Z_NULL);
  62.   Tracev((stderr, "inflate: reset\n"));
  63.   return Z_OK;
  64. }
  65.  
  66.  
  67. int ZEXPORT inflateEnd(z)
  68. z_streamp z;
  69. {
  70.   if (z == Z_NULL || z->state == Z_NULL)
  71.     return Z_STREAM_ERROR;
  72.   if (z->state->blocks != Z_NULL)
  73.     inflate_blocks_free(z->state->blocks, z);
  74.   ZFREE(z, z->state);
  75.   z->state = Z_NULL;
  76.   Tracev((stderr, "inflate: end\n"));
  77.   return Z_OK;
  78. }
  79.  
  80.  
  81. int ZEXPORT inflateInit2_(z, w, version, stream_size)
  82. z_streamp z;
  83. int w;
  84. const char *version;
  85. int stream_size;
  86. {
  87.   if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
  88.       stream_size != sizeof(z_stream))
  89.       return Z_VERSION_ERROR;
  90.  
  91.   /* initialize state */
  92.   if (z == Z_NULL)
  93.     return Z_STREAM_ERROR;
  94. //  z->msg = Z_NULL;
  95.   if ((z->state = (struct internal_state FAR *)
  96.        ZALLOC(z,1,sizeof(struct internal_state))) == Z_NULL)
  97.     return Z_MEM_ERROR;
  98.   z->state->blocks = Z_NULL;
  99.  
  100.   /* handle undocumented nowrap option (no zlib header or check) */
  101.   z->state->nowrap = 0;
  102.   if (w < 0)
  103.   {
  104.     w = - w;
  105.     z->state->nowrap = 1;
  106.   }
  107.  
  108.   /* set window size */
  109.   if (w < 8 || w > 15)
  110.   {
  111.     inflateEnd(z);
  112.     return Z_STREAM_ERROR;
  113.   }
  114.   z->state->wbits = (uInt)w;
  115.  
  116.   /* create inflate_blocks state */
  117.   if ((z->state->blocks =
  118.       inflate_blocks_new(z, z->state->nowrap ? Z_NULL : adler32, (uInt)1 << w))
  119.       == Z_NULL)
  120.   {
  121.     inflateEnd(z);
  122.     return Z_MEM_ERROR;
  123.   }
  124.   Tracev((stderr, "inflate: allocated\n"));
  125.  
  126.   /* reset state */
  127.   inflateReset(z);
  128.   return Z_OK;
  129. }
  130.  
  131.  
  132. int ZEXPORT inflateInit_(z, version, stream_size)
  133. z_streamp z;
  134. const char *version;
  135. int stream_size;
  136. {
  137.   return inflateInit2_(z, DEF_WBITS, version, stream_size);
  138. }
  139.  
  140.  
  141. #define NEEDBYTE {if(z->avail_in==0)return r;r=f;}
  142. #define NEXTBYTE (z->avail_in--,z->total_in++,*z->next_in++)
  143.  
  144. int ZEXPORT inflate(z, f)
  145. z_streamp z;
  146. int f;
  147. {
  148.   int r;
  149.   uInt b;
  150.  
  151.   if (z == Z_NULL || z->state == Z_NULL || z->next_in == Z_NULL)
  152.     return Z_STREAM_ERROR;
  153.   f = f == Z_FINISH ? Z_BUF_ERROR : Z_OK;
  154.   r = Z_BUF_ERROR;
  155.   while (1) switch (z->state->mode)
  156.   {
  157.     case METHOD:
  158.       NEEDBYTE
  159.       if (((z->state->sub.method = NEXTBYTE) & 0xf) != Z_DEFLATED)
  160.       {
  161.         z->state->mode = BAD;
  162. //        z->msg = (char*)"unknown compression method";
  163.         z->state->sub.marker = 5;       /* can't try inflateSync */
  164.         break;
  165.       }
  166.       if ((z->state->sub.method >> 4) + 8 > z->state->wbits)
  167.       {
  168.         z->state->mode = BAD;
  169. //        z->msg = (char*)"invalid window size";
  170.         z->state->sub.marker = 5;       /* can't try inflateSync */
  171.         break;
  172.       }
  173.       z->state->mode = FLAG;
  174.     case FLAG:
  175.       NEEDBYTE
  176.       b = NEXTBYTE;
  177.       if (((z->state->sub.method << 8) + b) % 31)
  178.       {
  179.         z->state->mode = BAD;
  180. //        z->msg = (char*)"incorrect header check";
  181.         z->state->sub.marker = 5;       /* can't try inflateSync */
  182.         break;
  183.       }
  184.       Tracev((stderr, "inflate: zlib header ok\n"));
  185.       if (!(b & PRESET_DICT))
  186.       {
  187.         z->state->mode = BLOCKS;
  188.         break;
  189.       }
  190.       z->state->mode = DICT4;
  191.     case DICT4:
  192.       NEEDBYTE
  193.       z->state->sub.check.need = (uLong)NEXTBYTE << 24;
  194.       z->state->mode = DICT3;
  195.     case DICT3:
  196.       NEEDBYTE
  197.       z->state->sub.check.need += (uLong)NEXTBYTE << 16;
  198.       z->state->mode = DICT2;
  199.     case DICT2:
  200.       NEEDBYTE
  201.       z->state->sub.check.need += (uLong)NEXTBYTE << 8;
  202.       z->state->mode = DICT1;
  203.     case DICT1:
  204.       NEEDBYTE
  205.       z->state->sub.check.need += (uLong)NEXTBYTE;
  206.       z->adler = z->state->sub.check.need;
  207.       z->state->mode = DICT0;
  208.       return Z_NEED_DICT;
  209.     case DICT0:
  210.       z->state->mode = BAD;
  211. //      z->msg = (char*)"need dictionary";
  212.       z->state->sub.marker = 0;       /* can try inflateSync */
  213.       return Z_STREAM_ERROR;
  214.     case BLOCKS:
  215.       r = inflate_blocks(z->state->blocks, z, r);
  216.       if (r == Z_DATA_ERROR)
  217.       {
  218.         z->state->mode = BAD;
  219.         z->state->sub.marker = 0;       /* can try inflateSync */
  220.         break;
  221.       }
  222.       if (r == Z_OK)
  223.         r = f;
  224.       if (r != Z_STREAM_END)
  225.         return r;
  226.       r = f;
  227.       inflate_blocks_reset(z->state->blocks, z, &z->state->sub.check.was);
  228.       if (z->state->nowrap)
  229.       {
  230.         z->state->mode = DONE;
  231.         break;
  232.       }
  233.       z->state->mode = CHECK4;
  234.     case CHECK4:
  235.       NEEDBYTE
  236.       z->state->sub.check.need = (uLong)NEXTBYTE << 24;
  237.       z->state->mode = CHECK3;
  238.     case CHECK3:
  239.       NEEDBYTE
  240.       z->state->sub.check.need += (uLong)NEXTBYTE << 16;
  241.       z->state->mode = CHECK2;
  242.     case CHECK2:
  243.       NEEDBYTE
  244.       z->state->sub.check.need += (uLong)NEXTBYTE << 8;
  245.       z->state->mode = CHECK1;
  246.     case CHECK1:
  247.       NEEDBYTE
  248.       z->state->sub.check.need += (uLong)NEXTBYTE;
  249.  
  250.       if (z->state->sub.check.was != z->state->sub.check.need)
  251.       {
  252.         z->state->mode = BAD;
  253. //        z->msg = (char*)"incorrect data check";
  254.         z->state->sub.marker = 5;       /* can't try inflateSync */
  255.         break;
  256.       }
  257.       Tracev((stderr, "inflate: zlib check ok\n"));
  258.       z->state->mode = DONE;
  259.     case DONE:
  260.       return Z_STREAM_END;
  261.     case BAD:
  262.       return Z_DATA_ERROR;
  263.     default:
  264.       return Z_STREAM_ERROR;
  265.   }
  266. #ifdef NEED_DUMMY_RETURN
  267.   return Z_STREAM_ERROR;  /* Some dumb compilers complain without this */
  268. #endif
  269. }
  270.  
  271.  
  272. int ZEXPORT inflateSetDictionary(z, dictionary, dictLength)
  273. z_streamp z;
  274. const Bytef *dictionary;
  275. uInt  dictLength;
  276. {
  277.   uInt length = dictLength;
  278.  
  279.   if (z == Z_NULL || z->state == Z_NULL || z->state->mode != DICT0)
  280.     return Z_STREAM_ERROR;
  281.  
  282.   if (adler32(1L, dictionary, dictLength) != z->adler) return Z_DATA_ERROR;
  283.   z->adler = 1L;
  284.  
  285.   if (length >= ((uInt)1<<z->state->wbits))
  286.   {
  287.     length = (1<<z->state->wbits)-1;
  288.     dictionary += dictLength - length;
  289.   }
  290.   inflate_set_dictionary(z->state->blocks, dictionary, length);
  291.   z->state->mode = BLOCKS;
  292.   return Z_OK;
  293. }
  294.  
  295.  
  296. int ZEXPORT inflateSync(z)
  297. z_streamp z;
  298. {
  299.   uInt n;       /* number of bytes to look at */
  300.   Bytef *p;     /* pointer to bytes */
  301.   uInt m;       /* number of marker bytes found in a row */
  302.   uLong r, w;   /* temporaries to save total_in and total_out */
  303.  
  304.   /* set up */
  305.   if (z == Z_NULL || z->state == Z_NULL)
  306.     return Z_STREAM_ERROR;
  307.   if (z->state->mode != BAD)
  308.   {
  309.     z->state->mode = BAD;
  310.     z->state->sub.marker = 0;
  311.   }
  312.   if ((n = z->avail_in) == 0)
  313.     return Z_BUF_ERROR;
  314.   p = z->next_in;
  315.   m = z->state->sub.marker;
  316.  
  317.   /* search */
  318.   while (n && m < 4)
  319.   {
  320.     static const Byte mark[4] = {0, 0, 0xff, 0xff};
  321.     if (*p == mark[m])
  322.       m++;
  323.     else if (*p)
  324.       m = 0;
  325.     else
  326.       m = 4 - m;
  327.     p++, n--;
  328.   }
  329.  
  330.   /* restore */
  331.   z->total_in += p - z->next_in;
  332.   z->next_in = p;
  333.   z->avail_in = n;
  334.   z->state->sub.marker = m;
  335.  
  336.   /* return no joy or set up to restart on a new block */
  337.   if (m != 4)
  338.     return Z_DATA_ERROR;
  339.   r = z->total_in;  w = z->total_out;
  340.   inflateReset(z);
  341.   z->total_in = r;  z->total_out = w;
  342.   z->state->mode = BLOCKS;
  343.   return Z_OK;
  344. }
  345.  
  346.  
  347. /* Returns true if inflate is currently at the end of a block generated
  348.  * by Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
  349.  * implementation to provide an additional safety check. PPP uses Z_SYNC_FLUSH
  350.  * but removes the length bytes of the resulting empty stored block. When
  351.  * decompressing, PPP checks that at the end of input packet, inflate is
  352.  * waiting for these length bytes.
  353.  */
  354. int ZEXPORT inflateSyncPoint(z)
  355. z_streamp z;
  356. {
  357.   if (z == Z_NULL || z->state == Z_NULL || z->state->blocks == Z_NULL)
  358.     return Z_STREAM_ERROR;
  359.   return inflate_blocks_sync_point(z->state->blocks);
  360. }
  361.