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