os/ossrv/compressionlibs/ziplib/test/oldezlib/EZLib/inflate.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/compressionlibs/ziplib/test/oldezlib/EZLib/inflate.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,367 @@
     1.4 +/* inflate.c -- zlib interface to inflate modules
     1.5 + * Copyright (C) 1995-1998 Mark Adler
     1.6 + * For conditions of distribution and use, see copyright notice in zlib.h 
     1.7 + */
     1.8 +
     1.9 +#include "zutil.h"
    1.10 +#include "infblock.h"
    1.11 +
    1.12 +struct inflate_blocks_state {int dummy;}; /* for buggy compilers */
    1.13 +
    1.14 +typedef enum {
    1.15 +      METHOD,   /* waiting for method byte */
    1.16 +      FLAG,     /* waiting for flag byte */
    1.17 +      DICT4,    /* four dictionary check bytes to go */
    1.18 +      DICT3,    /* three dictionary check bytes to go */
    1.19 +      DICT2,    /* two dictionary check bytes to go */
    1.20 +      DICT1,    /* one dictionary check byte to go */
    1.21 +      DICT0,    /* waiting for inflateSetDictionary */
    1.22 +      BLOCKS,   /* decompressing blocks */
    1.23 +      CHECK4,   /* four check bytes to go */
    1.24 +      CHECK3,   /* three check bytes to go */
    1.25 +      CHECK2,   /* two check bytes to go */
    1.26 +      CHECK1,   /* one check byte to go */
    1.27 +      DONE,     /* finished check, done */
    1.28 +      BAD}      /* got an error--stay here */
    1.29 +inflate_mode;
    1.30 +
    1.31 +/* inflate private state */
    1.32 +struct internal_state {
    1.33 +
    1.34 +  /* mode */
    1.35 +  inflate_mode  mode;   /* current inflate mode */
    1.36 +	
    1.37 +  /* mode dependent information */
    1.38 +  union {
    1.39 +    uInt method;        /* if FLAGS, method byte */
    1.40 +    struct {
    1.41 +      uLong was;                /* computed check value */
    1.42 +      uLong need;               /* stream check value */
    1.43 +    } check;            /* if CHECK, check values to compare */
    1.44 +    uInt marker;        /* if BAD, inflateSync's marker bytes count */
    1.45 +  } sub;        /* submode */
    1.46 +
    1.47 +  /* mode independent information */
    1.48 +  int  nowrap;          /* flag for no wrapper */
    1.49 +  uInt wbits;           /* log2(window size)  (8..15, defaults to 15) */
    1.50 +  inflate_blocks_statef 
    1.51 +    *blocks;            /* current inflate_blocks state */
    1.52 +
    1.53 +};
    1.54 +
    1.55 +
    1.56 +EXPORT_C int ZEXPORT inflateReset(
    1.57 +z_streamp z)
    1.58 +{
    1.59 +  if (z == Z_NULL || z->state == Z_NULL)
    1.60 +    return Z_STREAM_ERROR;
    1.61 +  z->total_in = z->total_out = 0;
    1.62 +  z->msg = Z_NULL;
    1.63 +  z->state->mode = z->state->nowrap ? BLOCKS : METHOD;
    1.64 +  inflate_blocks_reset(z->state->blocks, z, Z_NULL);
    1.65 +  Tracev((stderr, "inflate: reset\n"));
    1.66 +  return Z_OK;
    1.67 +}
    1.68 +
    1.69 +
    1.70 +EXPORT_C int ZEXPORT inflateEnd(
    1.71 +z_streamp z)
    1.72 +{
    1.73 +  if (z == Z_NULL || z->state == Z_NULL || z->zfree == Z_NULL)
    1.74 +    return Z_STREAM_ERROR;
    1.75 +  if (z->state->blocks != Z_NULL)
    1.76 +    inflate_blocks_free(z->state->blocks, z);
    1.77 +  ZFREE(z, z->state);
    1.78 +  z->state = Z_NULL;
    1.79 +  Tracev((stderr, "inflate: end\n"));
    1.80 +  return Z_OK;
    1.81 +}
    1.82 +
    1.83 +
    1.84 +EXPORT_C int ZEXPORT inflateInit2_(
    1.85 +z_streamp z,
    1.86 +int w,
    1.87 +const char *version,
    1.88 +int stream_size)
    1.89 +{
    1.90 +  if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
    1.91 +      stream_size != sizeof(z_stream))
    1.92 +      return Z_VERSION_ERROR;
    1.93 +
    1.94 +  /* initialize state */
    1.95 +  if (z == Z_NULL)
    1.96 +    return Z_STREAM_ERROR;
    1.97 +  z->msg = Z_NULL;
    1.98 +  if (z->zalloc == Z_NULL)
    1.99 +  {
   1.100 +    z->zalloc = zcalloc;
   1.101 +    z->opaque = (voidpf)0;
   1.102 +  }
   1.103 +  if (z->zfree == Z_NULL) z->zfree = zcfree;
   1.104 +  if ((z->state = (struct internal_state FAR *)
   1.105 +       ZALLOC(z,1,sizeof(struct internal_state))) == Z_NULL)
   1.106 +    return Z_MEM_ERROR;
   1.107 +  z->state->blocks = Z_NULL;
   1.108 +
   1.109 +  /* handle undocumented nowrap option (no zlib header or check) */
   1.110 +  z->state->nowrap = 0;
   1.111 +  if (w < 0)
   1.112 +  {
   1.113 +    w = - w;
   1.114 +    z->state->nowrap = 1;
   1.115 +  }
   1.116 +
   1.117 +  /* set window size */
   1.118 +  if (w < 8 || w > 15)
   1.119 +  {
   1.120 +    inflateEnd(z);
   1.121 +    return Z_STREAM_ERROR;
   1.122 +  }
   1.123 +  z->state->wbits = (uInt)w;
   1.124 +
   1.125 +  /* create inflate_blocks state */
   1.126 +  if ((z->state->blocks =
   1.127 +      inflate_blocks_new(z, z->state->nowrap ? Z_NULL : adler32, (uInt)1 << w))
   1.128 +      == Z_NULL)
   1.129 +  {
   1.130 +    inflateEnd(z);
   1.131 +    return Z_MEM_ERROR;
   1.132 +  }
   1.133 +  Tracev((stderr, "inflate: allocated\n"));
   1.134 +
   1.135 +  /* reset state */
   1.136 +  inflateReset(z);
   1.137 +  return Z_OK;
   1.138 +}
   1.139 +
   1.140 +
   1.141 +EXPORT_C int ZEXPORT inflateInit_(
   1.142 +z_streamp z,
   1.143 +const char *version,
   1.144 +int stream_size)
   1.145 +{
   1.146 +  return inflateInit2_(z, DEF_WBITS, version, stream_size);
   1.147 +}
   1.148 +
   1.149 +
   1.150 +#define NEEDBYTE {if(z->avail_in==0)return r;r=f;}
   1.151 +#define NEXTBYTE (z->avail_in--,z->total_in++,*z->next_in++)
   1.152 +
   1.153 +EXPORT_C int ZEXPORT inflate(
   1.154 +z_streamp z,
   1.155 +int f)
   1.156 +{
   1.157 +  int r;
   1.158 +  uInt b;
   1.159 +
   1.160 +  if (z == Z_NULL || z->state == Z_NULL || z->next_in == Z_NULL)
   1.161 +    return Z_STREAM_ERROR;
   1.162 +  f = f == Z_FINISH ? Z_BUF_ERROR : Z_OK;
   1.163 +  r = Z_BUF_ERROR;
   1.164 +
   1.165 +  for (;;) switch (z->state->mode)
   1.166 +  {
   1.167 +    case METHOD:
   1.168 +      NEEDBYTE
   1.169 +      if (((z->state->sub.method = NEXTBYTE) & 0xf) != Z_DEFLATED)
   1.170 +      {
   1.171 +        z->state->mode = BAD;
   1.172 +        z->msg = (char*)"unknown compression method";
   1.173 +        z->state->sub.marker = 5;       /* can't try inflateSync */
   1.174 +        break;
   1.175 +      }
   1.176 +      if ((z->state->sub.method >> 4) + 8 > z->state->wbits)
   1.177 +      {
   1.178 +        z->state->mode = BAD;
   1.179 +        z->msg = (char*)"invalid window size";
   1.180 +        z->state->sub.marker = 5;       /* can't try inflateSync */
   1.181 +        break;
   1.182 +      }
   1.183 +      z->state->mode = FLAG;
   1.184 +    case FLAG:
   1.185 +      NEEDBYTE
   1.186 +      b = NEXTBYTE;
   1.187 +      if (((z->state->sub.method << 8) + b) % 31)
   1.188 +      {
   1.189 +        z->state->mode = BAD;
   1.190 +        z->msg = (char*)"incorrect header check";
   1.191 +        z->state->sub.marker = 5;       /* can't try inflateSync */
   1.192 +        break;
   1.193 +      }
   1.194 +      Tracev((stderr, "inflate: zlib header ok\n"));
   1.195 +      if (!(b & PRESET_DICT))
   1.196 +      {
   1.197 +        z->state->mode = BLOCKS;
   1.198 +        break;
   1.199 +      }
   1.200 +      z->state->mode = DICT4;
   1.201 +    case DICT4:
   1.202 +      NEEDBYTE
   1.203 +      z->state->sub.check.need = (uLong)NEXTBYTE << 24;
   1.204 +      z->state->mode = DICT3;
   1.205 +    case DICT3:
   1.206 +      NEEDBYTE
   1.207 +      z->state->sub.check.need += (uLong)NEXTBYTE << 16;
   1.208 +      z->state->mode = DICT2;
   1.209 +    case DICT2:
   1.210 +      NEEDBYTE
   1.211 +      z->state->sub.check.need += (uLong)NEXTBYTE << 8;
   1.212 +      z->state->mode = DICT1;
   1.213 +    case DICT1:
   1.214 +      NEEDBYTE
   1.215 +      z->state->sub.check.need += (uLong)NEXTBYTE;
   1.216 +      z->adler = z->state->sub.check.need;
   1.217 +      z->state->mode = DICT0;
   1.218 +      return Z_NEED_DICT;
   1.219 +    case DICT0:
   1.220 +      z->state->mode = BAD;
   1.221 +      z->msg = (char*)"need dictionary";
   1.222 +      z->state->sub.marker = 0;       /* can try inflateSync */
   1.223 +      return Z_STREAM_ERROR;
   1.224 +    case BLOCKS:
   1.225 +      r = inflate_blocks(z->state->blocks, z, r);
   1.226 +      if (r == Z_DATA_ERROR)
   1.227 +      {
   1.228 +        z->state->mode = BAD;
   1.229 +        z->state->sub.marker = 0;       /* can try inflateSync */
   1.230 +        break;
   1.231 +      }
   1.232 +      if (r == Z_OK)
   1.233 +        r = f;
   1.234 +      if (r != Z_STREAM_END)
   1.235 +        return r;
   1.236 +      r = f;
   1.237 +      inflate_blocks_reset(z->state->blocks, z, &z->state->sub.check.was);
   1.238 +      if (z->state->nowrap)
   1.239 +      {
   1.240 +        z->state->mode = DONE;
   1.241 +        break;
   1.242 +      }
   1.243 +      z->state->mode = CHECK4;
   1.244 +    case CHECK4:
   1.245 +      NEEDBYTE
   1.246 +      z->state->sub.check.need = (uLong)NEXTBYTE << 24;
   1.247 +      z->state->mode = CHECK3;
   1.248 +    case CHECK3:
   1.249 +      NEEDBYTE
   1.250 +      z->state->sub.check.need += (uLong)NEXTBYTE << 16;
   1.251 +      z->state->mode = CHECK2;
   1.252 +    case CHECK2:
   1.253 +      NEEDBYTE
   1.254 +      z->state->sub.check.need += (uLong)NEXTBYTE << 8;
   1.255 +      z->state->mode = CHECK1;
   1.256 +    case CHECK1:
   1.257 +      NEEDBYTE
   1.258 +      z->state->sub.check.need += (uLong)NEXTBYTE;
   1.259 +
   1.260 +      if (z->state->sub.check.was != z->state->sub.check.need)
   1.261 +      {
   1.262 +        z->state->mode = BAD;
   1.263 +        z->msg = (char*)"incorrect data check";
   1.264 +        z->state->sub.marker = 5;       /* can't try inflateSync */
   1.265 +        break;
   1.266 +      }
   1.267 +      Tracev((stderr, "inflate: zlib check ok\n"));
   1.268 +      z->state->mode = DONE;
   1.269 +    case DONE:
   1.270 +      return Z_STREAM_END;
   1.271 +    case BAD:
   1.272 +      return Z_DATA_ERROR;
   1.273 +    default:
   1.274 +      return Z_STREAM_ERROR;
   1.275 +  }
   1.276 +#ifdef NEED_DUMMY_RETURN
   1.277 +  return Z_STREAM_ERROR;  /* Some dumb compilers complain without this */
   1.278 +#endif
   1.279 +}
   1.280 +
   1.281 +
   1.282 +EXPORT_C int ZEXPORT inflateSetDictionary(
   1.283 +z_streamp z,
   1.284 +const Bytef *dictionary,
   1.285 +uInt  dictLength)
   1.286 +{
   1.287 +  uInt length = dictLength;
   1.288 +
   1.289 +  if (z == Z_NULL || z->state == Z_NULL || z->state->mode != DICT0)
   1.290 +    return Z_STREAM_ERROR;
   1.291 +
   1.292 +  if (adler32(1L, dictionary, dictLength) != z->adler) return Z_DATA_ERROR;
   1.293 +  z->adler = 1L;
   1.294 +
   1.295 +  if (length >= ((uInt)1<<z->state->wbits))
   1.296 +  {
   1.297 +    length = (1<<z->state->wbits)-1;
   1.298 +    dictionary += dictLength - length;
   1.299 +  }
   1.300 +  inflate_set_dictionary(z->state->blocks, dictionary, length);
   1.301 +  z->state->mode = BLOCKS;
   1.302 +  return Z_OK;
   1.303 +}
   1.304 +
   1.305 +
   1.306 +EXPORT_C int ZEXPORT inflateSync(
   1.307 +z_streamp z)
   1.308 +{
   1.309 +  uInt n;       /* number of bytes to look at */
   1.310 +  Bytef *p;     /* pointer to bytes */
   1.311 +  uInt m;       /* number of marker bytes found in a row */
   1.312 +  uLong r, w;   /* temporaries to save total_in and total_out */
   1.313 +
   1.314 +  /* set up */
   1.315 +  if (z == Z_NULL || z->state == Z_NULL)
   1.316 +    return Z_STREAM_ERROR;
   1.317 +  if (z->state->mode != BAD)
   1.318 +  {
   1.319 +    z->state->mode = BAD;
   1.320 +    z->state->sub.marker = 0;
   1.321 +  }
   1.322 +  if ((n = z->avail_in) == 0)
   1.323 +    return Z_BUF_ERROR;
   1.324 +  p = z->next_in;
   1.325 +  m = z->state->sub.marker;
   1.326 +
   1.327 +  /* search */
   1.328 +  while (n && m < 4)
   1.329 +  {
   1.330 +    static const Byte mark[4] = {0, 0, 0xff, 0xff};
   1.331 +    if (*p == mark[m])
   1.332 +      m++;
   1.333 +    else if (*p)
   1.334 +      m = 0;
   1.335 +    else
   1.336 +      m = 4 - m;
   1.337 +    p++, n--;
   1.338 +  }
   1.339 +
   1.340 +  /* restore */
   1.341 +  z->total_in += p - z->next_in;
   1.342 +  z->next_in = p;
   1.343 +  z->avail_in = n;
   1.344 +  z->state->sub.marker = m;
   1.345 +
   1.346 +  /* return no joy or set up to restart on a new block */
   1.347 +  if (m != 4)
   1.348 +    return Z_DATA_ERROR;
   1.349 +  r = z->total_in;  w = z->total_out;
   1.350 +  inflateReset(z);
   1.351 +  z->total_in = r;  z->total_out = w;
   1.352 +  z->state->mode = BLOCKS;
   1.353 +  return Z_OK;
   1.354 +}
   1.355 +
   1.356 +
   1.357 +/* Returns true if inflate is currently at the end of a block generated
   1.358 + * by Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
   1.359 + * implementation to provide an additional safety check. PPP uses Z_SYNC_FLUSH
   1.360 + * but removes the length bytes of the resulting empty stored block. When
   1.361 + * decompressing, PPP checks that at the end of input packet, inflate is
   1.362 + * waiting for these length bytes.
   1.363 + */
   1.364 +EXPORT_C int ZEXPORT inflateSyncPoint(
   1.365 +z_streamp z)
   1.366 +{
   1.367 +  if (z == Z_NULL || z->state == Z_NULL || z->state->blocks == Z_NULL)
   1.368 +    return Z_STREAM_ERROR;
   1.369 +  return inflate_blocks_sync_point(z->state->blocks);
   1.370 +}