home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / modules / zlib / src / inflate.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  9.1 KB  |  347 lines

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