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

📄 inflate.c

📁 该代码包为INTEL IXP2400单板的BSP,同时经过修改,可以对MSN和ixf1104做自定义配置
💻 C
📖 第 1 页 / 共 5 页
字号:
        q += t;  m -= t;
        if ((s->sub.left -= t) != 0)
          break;
        Tracev((stderr, "inflate:       stored end, %lu total out\n",
                  z->total_out + (q >= s->read ? q - s->read :
                  (s->end - s->read) + (q - s->window))));
        s->mode = s->last ? DRY : TYPE;
        break;
     case TABLE:
        NEEDBITS(14)
        s->sub.trees.table = t = (UINT)b & 0x3fff;
        if ((t & 0x1f) > 29 || ((t >> 5) & 0x1f) > 29)
        {
          s->mode = BAD;
          z->msg = (char*)"too many length or distance symbols";
          r = Z_DATA_ERROR;
          ZLEAVE
        }
        t = 258 + (t & 0x1f) + ((t >> 5) & 0x1f);
        if (t < 19)
            t = 19;  /* inconsistent tongxj find */

        if ((s->sub.trees.blens = (UINT*)ZALLOC(z, t, sizeof(UINT))) == NULL)
        {
          r = Z_MEM_ERROR;
          ZLEAVE
        }
        DUMPBITS(14)
        s->sub.trees.index = 0;
        Tracev((stderr, "inflate:       table sizes ok\n"));
        s->mode = BTREE;
     case BTREE:
        while (s->sub.trees.index < 4 + (s->sub.trees.table >> 10))
        {
          NEEDBITS(3)
          s->sub.trees.blens[border[s->sub.trees.index++]] = (UINT)b & 7;
          DUMPBITS(3)
        }
        while (s->sub.trees.index < 19)
          s->sub.trees.blens[border[s->sub.trees.index++]] = 0;
        s->sub.trees.bb = 7;
        t = inflate_trees_bits(s->sub.trees.blens, &s->sub.trees.bb,
                                      &s->sub.trees.tb, z);
        if (t != Z_OK)
        {
          r = t;
          if (r == Z_DATA_ERROR)
             s->mode = BAD;
          ZLEAVE
        }
        s->sub.trees.index = 0;
        Tracev((stderr, "inflate:       bits tree ok\n"));
        s->mode = DTREE;
     case DTREE:
        while (t = s->sub.trees.table,
                 s->sub.trees.index < 258 + (t & 0x1f) + ((t >> 5) & 0x1f))
        {
          inflate_huft *h;
          UINT i, j, c;

          t = s->sub.trees.bb;
          NEEDBITS(t)
          h = s->sub.trees.tb + ((UINT)b & inflate_mask[t]);
          t = h->word.what.Bits;
          c = h->more.Base;
          if (c < 16)
          {
             DUMPBITS(t)
             s->sub.trees.blens[s->sub.trees.index++] = c;
          }
          else /* c == 16..18 */
          {
             i = c == 18 ? 7 : c - 14;
             j = c == 18 ? 11 : 3;
             NEEDBITS(t + i)
             DUMPBITS(t)
             j += (UINT)b & inflate_mask[i];
             DUMPBITS(i)
             i = s->sub.trees.index;
             t = s->sub.trees.table;
             if (i + j > 258 + (t & 0x1f) + ((t >> 5) & 0x1f) ||
                  (c == 16 && i < 1))
             {
                s->mode = BAD;
                z->msg = (char*)"invalid bit length repeat";
                r = Z_DATA_ERROR;
                ZLEAVE
             }
             c = c == 16 ? s->sub.trees.blens[i - 1] : 0;
             do {
                s->sub.trees.blens[i++] = c;
             } while (--j);
             s->sub.trees.index = i;
          }
        }
        inflate_trees_free(s->sub.trees.tb, z);
        s->sub.trees.tb = NULL;
        {
          UINT bl, bd;
          inflate_huft *tl, *td;
          inflate_codes_statef *c;

          bl = 9;         /* must be <= 9 for lookahead assumptions */
          bd = 6;         /* must be <= 9 for lookahead assumptions */
          t = s->sub.trees.table;
          t = inflate_trees_dynamic(257 + (t & 0x1f), 1 + ((t >> 5) & 0x1f),
                                             s->sub.trees.blens, &bl, &bd, &tl, &td, z);
          if (t != Z_OK)
          {
             if (t == (UINT)Z_DATA_ERROR)
                s->mode = BAD;
             r = t;
             ZLEAVE
          }
          /*  tongxj
          Tracev((stderr, "inflate:       trees ok, %d * %d bytes used\n",
              inflate_hufts, sizeof(inflate_huft))); */
          if ((c = inflate_codes_new(bl, bd, tl, td, z)) == NULL)
          {
             inflate_trees_free(td, z);
             inflate_trees_free(tl, z);
             r = Z_MEM_ERROR;
             ZLEAVE
          }
          ZFREE(z, s->sub.trees.blens);
          s->sub.decode.codes = c;
          s->sub.decode.tl = tl;
          s->sub.decode.td = td;
        }
        s->mode = CODES;
     case CODES:
        ZUPDATE
        if ((r = inflate_codes(s, z, r)) != Z_STREAM_END)
          return inflate_flush(s, z, r);
        r = Z_OK;
        inflate_codes_free(s->sub.decode.codes, z);
        inflate_trees_free(s->sub.decode.td, z);
        inflate_trees_free(s->sub.decode.tl, z);
        LOAD
        Tracev((stderr, "inflate:       codes end, %lu total out\n",
                  z->total_out + (q >= s->read ? q - s->read :
                  (s->end - s->read) + (q - s->window))));
        if (!s->last)
        {
          s->mode = TYPE;
          break;
        }
        if (k > 7)              /* return unused byte, if any */
        {
          Assert(k < 16, "inflate_codes grabbed too many bytes")
          k -= 8;
          n++;
          p--;                    /* can always return one */
        }
        s->mode = DRY;
     case DRY:
        FLUSH
        if (s->read != s->write)
          ZLEAVE
        s->mode = DONE;
     case DONE:
        r = Z_STREAM_END;
        ZLEAVE
     case BAD:
        r = Z_DATA_ERROR;
        ZLEAVE
     default:
        r = Z_STREAM_ERROR;
        ZLEAVE
  }
}

/******************************************************************************
*
* inflate_blocks_free -
*/

static int inflate_blocks_free(
        inflate_blocks_statef *s,
        ZAR_STREAM* z,
        ULONG *c)
{
  inflate_blocks_reset(s, z, c);
  ZFREE(z, s->window);
  ZFREE(z, s);
  return Z_OK;
}

/******************************************************************************
*
* inflateReset -
*/

static int inflateReset(ZAR_STREAM* z)
{
  ULONG c;

  if (z == NULL || z->state == NULL)
    return Z_STREAM_ERROR;

  z->total_in = z->total_out = 0;
  z->msg = NULL;
  z->state->mode = z->state->nowrap ? BLOCKS : METHOD;

  inflate_blocks_reset(z->state->blocks, z, &c);

  Trace((stderr, "inflate: reset\n"));

  return Z_OK;
}

/******************************************************************************
*
* inflateEnd -
*/

int inflateEnd( ZAR_STREAM* z)
{
    ULONG c;

    if (z == NULL || z->state == NULL || z->zfree == NULL)
        return Z_STREAM_ERROR;

    if (z->state->blocks != NULL)
        inflate_blocks_free(z->state->blocks, z, &c);

    ZFREE(z, z->state);   /* free state */
    z->state = NULL;

    if(z->z_mem_pool != NULL )
        free(z->z_mem_pool);

    Trace((stderr, "inflate: end\n"));
    return Z_OK;
}

/******************************************************************************
*
* inflateInit -
*/
int inflateInit( ZAR_STREAM* z)
{
    int w = DEF_WBITS;

    /* initialize state */
    if (z == NULL)
        return Z_STREAM_ERROR;

    bzero((char*)z,sizeof(ZAR_STREAM));

    /* allocate memory for this stream */
    if ( (z->z_mem_pool = (BYTE*)malloc(MEM_POOL_SIZE))== NULL)
        return Z_MEM_ERROR;

    z->z_next_block = (BYTE*)z->z_mem_pool + BLK_HDR_SIZE;
    BLK_PREV(z->z_next_block) = 0;      /* set first block's prev pointer with null */


    z->msg = NULL;
    z->zalloc = zcalloc;
    z->opaque = (VOIDP)0;
    z->zfree = zcfree;

    if ((z->state = (struct internal_state  *)
        ZALLOC(z,1,sizeof(struct internal_state))) == NULL)
        return Z_MEM_ERROR;

    /* 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 ? NULL : adler32, (UINT)1 << w))
            == NULL)
    {
        inflateEnd(z);
        return Z_MEM_ERROR;
    }

    /* >>> */

    z->z_fixed_built= 0;
    z->z_fixed_bl   = 0;
    z->z_fixed_bd   = 0;
    z->z_fixed_tl   = 0;
    z->z_fixed_td   = 0;

    /* <<< */

    Trace((stderr, "inflate: allocated\n"));
    /* reset state */
    if(inflateReset(z) != Z_OK )
        return Z_STREAM_ERROR;

    /* initialize */
    return Z_OK;
}

/******************************************************************************
*
* inflate -
*/
int zinflate (   ZAR_STREAM* z, int f )
{
  int   retVal;
  UINT  b;

  if (z == NULL || z->state == NULL || z->next_in == NULL || f < 0)
    return Z_STREAM_ERROR;
  retVal = Z_BUF_ERROR;
  while (1) switch (z->state->mode)
  {
    case METHOD:
        if(z->avail_in==0)
            return Z_BUF_ERROR;

        retVal = Z_OK;
        if (((z->state->sub.method = ZSTREAM_NEXT_BYTE(z)) & 0xf) != Z_DEFLATED)
        {
            z->state->mode = INF_BAD;
            z->msg = (char*)"unknown compression method";
            break;
        }
        if ((z->state->sub.method >> 4) + 8 > z->state->wbits)
        {
            z->state->mode = INF_BAD;
            z->msg = (char*)"invalid window size";
            break;
        }
        z->state->mode = FLAG;

    case FLAG:
        if(z->avail_in==0)
           return Z_BUF_ERROR;

        retVal = Z_OK;
        b = ZSTREAM_NEXT_BYTE(z);
      if (((z->state->sub.method << 8) + b) % 31)
      {
        z->state->mode = INF_BAD;
        z->msg = (char*)"incorrect header check";
        break;
      }
      Trace((stderr, "inflate: zlib header ok\n"));
      if (!(b & PRESET_DICT))
      {
        z->state->mode = BLOCKS;
    break;
        }
      z->state->mode = DICT4;
     case DICT4:
      if(z->avail_in==0)
          return Z_BUF_ERROR;

      retVal = Z_OK;
      z->state->sub.check.need = (ULONG)ZSTREAM_NEXT_BYTE(z) << 24;
      z->state->mode = DICT3;
    case DICT3:
        if(z->avail_in==0)
           return Z_BUF_ERROR;

        retVal = Z_OK;
      z->state->sub.check.need += (ULONG)ZSTREAM_NEXT_BYTE(z) << 16;
      z->state->mode = DICT2;
    case DICT2:
        if(z->avail_in==0)
           return Z_BUF_ERROR;

        retVal = Z_OK;
      z->state->sub.check.need += (ULONG)ZSTREAM_NEXT_BYTE(z) << 8;
      z->state->mode = DICT1;
    case DICT1:
        if(z->avail_in==0)
           return Z_BUF_ERROR;

        retVal = Z_OK;
      z->state->sub.check.need += (ULONG)ZSTREAM_NEXT_BYTE(z);
      z->adler = z->state->sub.check.need;
      z->state->mode = DICT0;
      return Z_NEED_DICT;

    case DICT0:
      z->state->mode = INF_BAD;
      z->msg = (char*)"need dictionary";
      return Z_STREAM_ERROR;

     case BLOCKS:
      retVal = inflate_blocks(z->state->blocks, z, retVal);
      if (retVal == Z_DATA_ERROR)
       {
        z->state->mode = INF_BAD;
          break;
      }
      if (retVal != Z_STREAM_END)
        return retVal;

      inflate_blocks_reset(z->state->blocks, z, &z->state->sub.check.was);
      if (z->state->nowrap)
      {
        z->state->mode = INF_DONE;
        break;
      }
      z->state->mode = CHECK4;

    case CHECK4:
        if(z->avail_in==0)
           return Z_BUF_ERROR;

        retVal = Z_OK;
      z->state->sub.check.need = (ULONG)ZSTREAM_NEXT_BYTE(z) << 24;
      z->state->mode = CHECK3;
    case CHECK3:
        if(z->avail_in==0)
           return Z_BUF_ERROR;

        retVal = Z_OK;
      z->state->sub.check.need += (ULONG)ZSTREAM_NEXT_BYTE(z) << 16;
        z->state->mode = CHECK2;
    case CHECK2:

⌨️ 快捷键说明

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