compressionlibs/ziplib/test/oldezlib/EZLib/inflate.cpp
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     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 EXPORT_C int ZEXPORT inflateReset(
       
    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 EXPORT_C int ZEXPORT inflateEnd(
       
    68 z_streamp z)
       
    69 {
       
    70   if (z == Z_NULL || z->state == Z_NULL || z->zfree == 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 EXPORT_C int ZEXPORT inflateInit2_(
       
    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->zalloc == Z_NULL)
       
    96   {
       
    97     z->zalloc = zcalloc;
       
    98     z->opaque = (voidpf)0;
       
    99   }
       
   100   if (z->zfree == Z_NULL) z->zfree = zcfree;
       
   101   if ((z->state = (struct internal_state FAR *)
       
   102        ZALLOC(z,1,sizeof(struct internal_state))) == Z_NULL)
       
   103     return Z_MEM_ERROR;
       
   104   z->state->blocks = Z_NULL;
       
   105 
       
   106   /* handle undocumented nowrap option (no zlib header or check) */
       
   107   z->state->nowrap = 0;
       
   108   if (w < 0)
       
   109   {
       
   110     w = - w;
       
   111     z->state->nowrap = 1;
       
   112   }
       
   113 
       
   114   /* set window size */
       
   115   if (w < 8 || w > 15)
       
   116   {
       
   117     inflateEnd(z);
       
   118     return Z_STREAM_ERROR;
       
   119   }
       
   120   z->state->wbits = (uInt)w;
       
   121 
       
   122   /* create inflate_blocks state */
       
   123   if ((z->state->blocks =
       
   124       inflate_blocks_new(z, z->state->nowrap ? Z_NULL : adler32, (uInt)1 << w))
       
   125       == Z_NULL)
       
   126   {
       
   127     inflateEnd(z);
       
   128     return Z_MEM_ERROR;
       
   129   }
       
   130   Tracev((stderr, "inflate: allocated\n"));
       
   131 
       
   132   /* reset state */
       
   133   inflateReset(z);
       
   134   return Z_OK;
       
   135 }
       
   136 
       
   137 
       
   138 EXPORT_C int ZEXPORT inflateInit_(
       
   139 z_streamp z,
       
   140 const char *version,
       
   141 int stream_size)
       
   142 {
       
   143   return inflateInit2_(z, DEF_WBITS, version, stream_size);
       
   144 }
       
   145 
       
   146 
       
   147 #define NEEDBYTE {if(z->avail_in==0)return r;r=f;}
       
   148 #define NEXTBYTE (z->avail_in--,z->total_in++,*z->next_in++)
       
   149 
       
   150 EXPORT_C int ZEXPORT inflate(
       
   151 z_streamp z,
       
   152 int f)
       
   153 {
       
   154   int r;
       
   155   uInt b;
       
   156 
       
   157   if (z == Z_NULL || z->state == Z_NULL || z->next_in == Z_NULL)
       
   158     return Z_STREAM_ERROR;
       
   159   f = f == Z_FINISH ? Z_BUF_ERROR : Z_OK;
       
   160   r = Z_BUF_ERROR;
       
   161 
       
   162   for (;;) switch (z->state->mode)
       
   163   {
       
   164     case METHOD:
       
   165       NEEDBYTE
       
   166       if (((z->state->sub.method = NEXTBYTE) & 0xf) != Z_DEFLATED)
       
   167       {
       
   168         z->state->mode = BAD;
       
   169         z->msg = (char*)"unknown compression method";
       
   170         z->state->sub.marker = 5;       /* can't try inflateSync */
       
   171         break;
       
   172       }
       
   173       if ((z->state->sub.method >> 4) + 8 > z->state->wbits)
       
   174       {
       
   175         z->state->mode = BAD;
       
   176         z->msg = (char*)"invalid window size";
       
   177         z->state->sub.marker = 5;       /* can't try inflateSync */
       
   178         break;
       
   179       }
       
   180       z->state->mode = FLAG;
       
   181     case FLAG:
       
   182       NEEDBYTE
       
   183       b = NEXTBYTE;
       
   184       if (((z->state->sub.method << 8) + b) % 31)
       
   185       {
       
   186         z->state->mode = BAD;
       
   187         z->msg = (char*)"incorrect header check";
       
   188         z->state->sub.marker = 5;       /* can't try inflateSync */
       
   189         break;
       
   190       }
       
   191       Tracev((stderr, "inflate: zlib header ok\n"));
       
   192       if (!(b & PRESET_DICT))
       
   193       {
       
   194         z->state->mode = BLOCKS;
       
   195         break;
       
   196       }
       
   197       z->state->mode = DICT4;
       
   198     case DICT4:
       
   199       NEEDBYTE
       
   200       z->state->sub.check.need = (uLong)NEXTBYTE << 24;
       
   201       z->state->mode = DICT3;
       
   202     case DICT3:
       
   203       NEEDBYTE
       
   204       z->state->sub.check.need += (uLong)NEXTBYTE << 16;
       
   205       z->state->mode = DICT2;
       
   206     case DICT2:
       
   207       NEEDBYTE
       
   208       z->state->sub.check.need += (uLong)NEXTBYTE << 8;
       
   209       z->state->mode = DICT1;
       
   210     case DICT1:
       
   211       NEEDBYTE
       
   212       z->state->sub.check.need += (uLong)NEXTBYTE;
       
   213       z->adler = z->state->sub.check.need;
       
   214       z->state->mode = DICT0;
       
   215       return Z_NEED_DICT;
       
   216     case DICT0:
       
   217       z->state->mode = BAD;
       
   218       z->msg = (char*)"need dictionary";
       
   219       z->state->sub.marker = 0;       /* can try inflateSync */
       
   220       return Z_STREAM_ERROR;
       
   221     case BLOCKS:
       
   222       r = inflate_blocks(z->state->blocks, z, r);
       
   223       if (r == Z_DATA_ERROR)
       
   224       {
       
   225         z->state->mode = BAD;
       
   226         z->state->sub.marker = 0;       /* can try inflateSync */
       
   227         break;
       
   228       }
       
   229       if (r == Z_OK)
       
   230         r = f;
       
   231       if (r != Z_STREAM_END)
       
   232         return r;
       
   233       r = f;
       
   234       inflate_blocks_reset(z->state->blocks, z, &z->state->sub.check.was);
       
   235       if (z->state->nowrap)
       
   236       {
       
   237         z->state->mode = DONE;
       
   238         break;
       
   239       }
       
   240       z->state->mode = CHECK4;
       
   241     case CHECK4:
       
   242       NEEDBYTE
       
   243       z->state->sub.check.need = (uLong)NEXTBYTE << 24;
       
   244       z->state->mode = CHECK3;
       
   245     case CHECK3:
       
   246       NEEDBYTE
       
   247       z->state->sub.check.need += (uLong)NEXTBYTE << 16;
       
   248       z->state->mode = CHECK2;
       
   249     case CHECK2:
       
   250       NEEDBYTE
       
   251       z->state->sub.check.need += (uLong)NEXTBYTE << 8;
       
   252       z->state->mode = CHECK1;
       
   253     case CHECK1:
       
   254       NEEDBYTE
       
   255       z->state->sub.check.need += (uLong)NEXTBYTE;
       
   256 
       
   257       if (z->state->sub.check.was != z->state->sub.check.need)
       
   258       {
       
   259         z->state->mode = BAD;
       
   260         z->msg = (char*)"incorrect data check";
       
   261         z->state->sub.marker = 5;       /* can't try inflateSync */
       
   262         break;
       
   263       }
       
   264       Tracev((stderr, "inflate: zlib check ok\n"));
       
   265       z->state->mode = DONE;
       
   266     case DONE:
       
   267       return Z_STREAM_END;
       
   268     case BAD:
       
   269       return Z_DATA_ERROR;
       
   270     default:
       
   271       return Z_STREAM_ERROR;
       
   272   }
       
   273 #ifdef NEED_DUMMY_RETURN
       
   274   return Z_STREAM_ERROR;  /* Some dumb compilers complain without this */
       
   275 #endif
       
   276 }
       
   277 
       
   278 
       
   279 EXPORT_C int ZEXPORT inflateSetDictionary(
       
   280 z_streamp z,
       
   281 const Bytef *dictionary,
       
   282 uInt  dictLength)
       
   283 {
       
   284   uInt length = dictLength;
       
   285 
       
   286   if (z == Z_NULL || z->state == Z_NULL || z->state->mode != DICT0)
       
   287     return Z_STREAM_ERROR;
       
   288 
       
   289   if (adler32(1L, dictionary, dictLength) != z->adler) return Z_DATA_ERROR;
       
   290   z->adler = 1L;
       
   291 
       
   292   if (length >= ((uInt)1<<z->state->wbits))
       
   293   {
       
   294     length = (1<<z->state->wbits)-1;
       
   295     dictionary += dictLength - length;
       
   296   }
       
   297   inflate_set_dictionary(z->state->blocks, dictionary, length);
       
   298   z->state->mode = BLOCKS;
       
   299   return Z_OK;
       
   300 }
       
   301 
       
   302 
       
   303 EXPORT_C int ZEXPORT inflateSync(
       
   304 z_streamp z)
       
   305 {
       
   306   uInt n;       /* number of bytes to look at */
       
   307   Bytef *p;     /* pointer to bytes */
       
   308   uInt m;       /* number of marker bytes found in a row */
       
   309   uLong r, w;   /* temporaries to save total_in and total_out */
       
   310 
       
   311   /* set up */
       
   312   if (z == Z_NULL || z->state == Z_NULL)
       
   313     return Z_STREAM_ERROR;
       
   314   if (z->state->mode != BAD)
       
   315   {
       
   316     z->state->mode = BAD;
       
   317     z->state->sub.marker = 0;
       
   318   }
       
   319   if ((n = z->avail_in) == 0)
       
   320     return Z_BUF_ERROR;
       
   321   p = z->next_in;
       
   322   m = z->state->sub.marker;
       
   323 
       
   324   /* search */
       
   325   while (n && m < 4)
       
   326   {
       
   327     static const Byte mark[4] = {0, 0, 0xff, 0xff};
       
   328     if (*p == mark[m])
       
   329       m++;
       
   330     else if (*p)
       
   331       m = 0;
       
   332     else
       
   333       m = 4 - m;
       
   334     p++, n--;
       
   335   }
       
   336 
       
   337   /* restore */
       
   338   z->total_in += p - z->next_in;
       
   339   z->next_in = p;
       
   340   z->avail_in = n;
       
   341   z->state->sub.marker = m;
       
   342 
       
   343   /* return no joy or set up to restart on a new block */
       
   344   if (m != 4)
       
   345     return Z_DATA_ERROR;
       
   346   r = z->total_in;  w = z->total_out;
       
   347   inflateReset(z);
       
   348   z->total_in = r;  z->total_out = w;
       
   349   z->state->mode = BLOCKS;
       
   350   return Z_OK;
       
   351 }
       
   352 
       
   353 
       
   354 /* Returns true if inflate is currently at the end of a block generated
       
   355  * by Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
       
   356  * implementation to provide an additional safety check. PPP uses Z_SYNC_FLUSH
       
   357  * but removes the length bytes of the resulting empty stored block. When
       
   358  * decompressing, PPP checks that at the end of input packet, inflate is
       
   359  * waiting for these length bytes.
       
   360  */
       
   361 EXPORT_C int ZEXPORT inflateSyncPoint(
       
   362 z_streamp z)
       
   363 {
       
   364   if (z == Z_NULL || z->state == Z_NULL || z->state->blocks == Z_NULL)
       
   365     return Z_STREAM_ERROR;
       
   366   return inflate_blocks_sync_point(z->state->blocks);
       
   367 }