⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 inflate.c

📁 eCos/RedBoot for勤研ARM AnywhereII(4510) 含全部源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* inflate.c -- zlib interface to inflate modules
 * Copyright (C) 1995-2002 Mark Adler
 * For conditions of distribution and use, see copyright notice in zlib.h 
 */

#include "zutil.h"
#include "infblock.h"

struct inflate_blocks_state {int dummy;}; /* for buggy compilers */

typedef enum {
      METHOD,   /* waiting for method byte */
#ifdef __ECOS__
      GZ_HDR2,
      GZ_METHOD,
      GZ_FLAG,
      GZ_TIME,
      GZ_EXTRA,
      GZ_EXTRA_HI,
      GZ_EXTRA_ZAP,
      GZ_NAME,
      GZ_COMMENT,
      GZ_HEAD_CRC_LO,
      GZ_HEAD_CRC_HI,
      GZ_DONE,
#endif // __ECOS__
      FLAG,     /* waiting for flag byte */
      DICT4,    /* four dictionary check bytes to go */
      DICT3,    /* three dictionary check bytes to go */
      DICT2,    /* two dictionary check bytes to go */
      DICT1,    /* one dictionary check byte to go */
      DICT0,    /* waiting for inflateSetDictionary */
      BLOCKS,   /* decompressing blocks */
      CHECK4,   /* four check bytes to go */
      CHECK3,   /* three check bytes to go */
      CHECK2,   /* two check bytes to go */
      CHECK1,   /* one check byte to go */
      DONE,     /* finished check, done */
      BAD}      /* got an error--stay here */
inflate_mode;

/* inflate private state */
struct internal_state {

  /* mode */
  inflate_mode  mode;   /* current inflate mode */
#ifdef __ECOS__
    inflate_mode gz_mode;
    uInt gz_flag;
    int gz_cnt;
    unsigned char* gz_start;
    uLong gz_sum;
#endif

  /* mode dependent information */
  union {
    uInt method;        /* if FLAGS, method byte */
    struct {
      uLong was;                /* computed check value */
      uLong need;               /* stream check value */
    } check;            /* if CHECK, check values to compare */
    uInt marker;        /* if BAD, inflateSync's marker bytes count */
  } sub;        /* submode */

  /* mode independent information */
  int  nowrap;          /* flag for no wrapper */
  uInt wbits;           /* log2(window size)  (8..15, defaults to 15) */
  inflate_blocks_statef 
    *blocks;            /* current inflate_blocks state */

};

#ifdef __ECOS__
/* gzip flag byte */
#define _GZ_ASCII_FLAG   0x01 /* bit 0 set: file probably ascii text */
#define _GZ_HEAD_CRC     0x02 /* bit 1 set: header CRC present */
#define _GZ_EXTRA_FIELD  0x04 /* bit 2 set: extra field present */
#define _GZ_ORIG_NAME    0x08 /* bit 3 set: original file name present */
#define _GZ_COMMENT      0x10 /* bit 4 set: file comment present */
#define _GZ_RESERVED     0xE0 /* bits 5..7: reserved */
#endif // __ECOS__


int ZEXPORT inflateReset(z)
z_streamp z;
{
  if (z == Z_NULL || z->state == Z_NULL)
    return Z_STREAM_ERROR;
  z->total_in = z->total_out = 0;
  z->msg = Z_NULL;
  z->state->mode = z->state->nowrap ? BLOCKS : METHOD;
  inflate_blocks_reset(z->state->blocks, z, Z_NULL);
  Tracev((stderr, "inflate: reset\n"));
  return Z_OK;
}


int ZEXPORT inflateEnd(z)
z_streamp z;
{
  if (z == Z_NULL || z->state == Z_NULL || z->zfree == Z_NULL)
    return Z_STREAM_ERROR;
  if (z->state->blocks != Z_NULL)
    inflate_blocks_free(z->state->blocks, z);
  ZFREE(z, z->state);
  z->state = Z_NULL;
  Tracev((stderr, "inflate: end\n"));
  return Z_OK;
}


int ZEXPORT inflateInit2_(z, w, version, stream_size)
z_streamp z;
int w;
const char *version;
int stream_size;
{
  if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
      stream_size != sizeof(z_stream))
      return Z_VERSION_ERROR;

  /* initialize state */
  if (z == Z_NULL)
    return Z_STREAM_ERROR;
  z->msg = Z_NULL;
  if (z->zalloc == Z_NULL)
  {
    z->zalloc = zcalloc;
    z->opaque = (voidpf)0;
  }
  if (z->zfree == Z_NULL) z->zfree = zcfree;
  if ((z->state = (struct internal_state FAR *)
       ZALLOC(z,1,sizeof(struct internal_state))) == Z_NULL)
    return Z_MEM_ERROR;
  z->state->blocks = Z_NULL;

  /* handle undocumented nowrap option (no zlib header or check) */
  z->state->nowrap = 0;
  if (w < 0)
  {
    w = - w;
    z->state->nowrap = 1;
  }

  /* set window size */
  if (w < 8 || w > 15)
  {
    inflateEnd(z);
    return Z_STREAM_ERROR;
  }
  z->state->wbits = (uInt)w;

  /* create inflate_blocks state */
  if ((z->state->blocks =
      inflate_blocks_new(z, z->state->nowrap ? Z_NULL : adler32, (uInt)1 << w))
      == Z_NULL)
  {
    inflateEnd(z);
    return Z_MEM_ERROR;
  }
  Tracev((stderr, "inflate: allocated\n"));

  /* reset state */
  inflateReset(z);
  return Z_OK;
}


int ZEXPORT inflateInit_(z, version, stream_size)
z_streamp z;
const char *version;
int stream_size;
{
  return inflateInit2_(z, DEF_WBITS, version, stream_size);
}


#define NEEDBYTE {if(z->avail_in==0)return r;r=f;}
#define NEXTBYTE (z->avail_in--,z->total_in++,*z->next_in++)

int ZEXPORT inflate(z, f)
z_streamp z;
int f;
{
  int r;
  uInt b;

  if (z == Z_NULL || z->state == Z_NULL || z->next_in == Z_NULL)
    return Z_STREAM_ERROR;
  f = f == Z_FINISH ? Z_BUF_ERROR : Z_OK;
  r = Z_BUF_ERROR;
  while (1) switch (z->state->mode)
  {
    case METHOD:
#ifdef __ECOS__
        // "Clear" gz_mode - if DONE at exit, this was a zlib stream.
        z->state->gz_mode = DONE;
#endif
      NEEDBYTE
      if (((z->state->sub.method = NEXTBYTE) & 0xf) != Z_DEFLATED)
      {
#ifdef __ECOS__
          if (0x1f == z->state->sub.method) {
              z->state->mode = GZ_HDR2;
              break;
          }
          // This is a hack to get a reasonable error message in
          // RedBoot if the user tries to decompress raw data.
          z->state->mode = BAD;
          z->msg = (char*)"incorrect gzip header";
          z->state->sub.marker = 5;       /* can't try inflateSync */
#else
        z->state->mode = BAD;
        z->msg = (char*)"unknown compression method";
        z->state->sub.marker = 5;       /* can't try inflateSync */
#endif // __ECOS__
        break;
      }
      if ((z->state->sub.method >> 4) + 8 > z->state->wbits)
      {
        z->state->mode = BAD;
        z->msg = (char*)"invalid window size";
        z->state->sub.marker = 5;       /* can't try inflateSync */
        break;
      }
      z->state->mode = FLAG;
#ifdef __ECOS__
      break;
    case GZ_HDR2:
        NEEDBYTE;
        b = NEXTBYTE;
        if (0x8b != b) {
            z->state->mode = BAD;
            z->msg = (char*)"incorrect gzip header";
            z->state->sub.marker = 5;       /* can't try inflateSync */
            break;
        }
        z->state->mode = GZ_METHOD;
  case GZ_METHOD:
      NEEDBYTE
      if ((z->state->sub.method = NEXTBYTE) != Z_DEFLATED)
      {
        z->state->mode = BAD;
        z->msg = (char*)"unknown compression method";
        z->state->sub.marker = 5;       /* can't try inflateSync */
        break;
      }
      if ((z->state->sub.method >> 4) + 8 > z->state->wbits)
      {
        z->state->mode = BAD;
        z->msg = (char*)"invalid window size";
        z->state->sub.marker = 5;       /* can't try inflateSync */
        break;
      }
      z->state->mode = GZ_FLAG;
  case GZ_FLAG:
      NEEDBYTE
      z->state->gz_flag = NEXTBYTE;
      z->state->mode = GZ_TIME;
      z->state->gz_cnt = 6;             // for GZ_TIME
  case GZ_TIME:
      // Discard time, xflags and OS code
      while (z->state->gz_cnt-- > 0) {
          NEEDBYTE;
          b = NEXTBYTE;
      }
      z->state->mode = GZ_EXTRA;
  case GZ_EXTRA:
      if (!(z->state->gz_flag & _GZ_EXTRA_FIELD)) {
          z->state->mode = GZ_NAME;
          break;
      }

      NEEDBYTE;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -