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

📄 deflate.c

📁 Intel 5.2 版本的zlib 库实现
💻 C
📖 第 1 页 / 共 4 页
字号:

    if(s->wrap == 0)
      deflateStatus = IppLZ77StatusLZ77Process;
    else
      deflateStatus = IppLZ77StatusInit;

    ippsEncodeLZ77SetStatus_8u(deflateStatus, pLZ77State);

    strm->adler =
#ifdef GZIP
        s->wrap == 2 ? crc32(0L, Z_NULL, 0) :
#endif
        adler32(0L, Z_NULL, 0);
    s->last_flush = Z_NO_FLUSH;

    _tr_init(s);
    lm_init(s);

    if(ippsEncodeLZ77Reset_8u(pLZ77State) < 0)
      return Z_STREAM_ERROR;
    return Z_OK;
}

/* ========================================================================= */
int ZEXPORT deflatePrime (strm, bits, value)
    z_streamp strm;
    int bits;
    int value;
{
    if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
    strm->state->bi_valid = bits;
    strm->state->bi_buf = (ush)(value & ((1 << bits) - 1));
    return Z_OK;
}

/* ========================================================================= */
int ZEXPORT deflateParams(strm, level, strategy)
    z_streamp strm;
    int level;
    int strategy;
{
    deflate_state *s;
    compress_func func;
    int err = Z_OK;

    if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
    s = strm->state;

#ifdef FASTEST
    if (level != 0) level = 1;
#else
    if (level == Z_DEFAULT_COMPRESSION) level = 6;
#endif
    if (level < 0 || level > 9 || strategy < 0 || strategy > Z_RLE) {
        return Z_STREAM_ERROR;
    }
    func = configuration_table[s->level].func;

    if (func != configuration_table[level].func && strm->total_in != 0) {
        /* Flush the last buffer: */
        err = deflate(strm, Z_PARTIAL_FLUSH);
    }
    if (s->level != level) {
        s->level = level;
        s->max_lazy_match   = configuration_table[level].max_lazy;
        s->good_match       = configuration_table[level].good_length;
        s->nice_match       = configuration_table[level].nice_length;
        s->max_chain_length = configuration_table[level].max_chain;
    }
    s->strategy = strategy;
    return err;
}

/* =========================================================================
 * For the default windowBits of 15 and memLevel of 8, this function returns
 * a close to exact, as well as small, upper bound on the compressed size.
 * They are coded as constants here for a reason--if the #define's are
 * changed, then this function needs to be changed as well.  The return
 * value for 15 and 8 only works for those exact settings.
 *
 * For any setting other than those defaults for windowBits and memLevel,
 * the value returned is a conservative worst case for the maximum expansion
 * resulting from using fixed blocks instead of stored blocks, which deflate
 * can emit on compressed data for some combinations of the parameters.
 *
 * This function could be more sophisticated to provide closer upper bounds
 * for every combination of windowBits and memLevel, as well as wrap.
 * But even the conservative upper bound of about 14% expansion does not
 * seem onerous for output buffer allocation.
 */
uLong ZEXPORT deflateBound(strm, sourceLen)
    z_streamp strm;
    uLong sourceLen;
{
    deflate_state *s;
    uLong destLen;

    /* conservative upper bound */
    destLen = sourceLen +
              ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 11;

    /* if can't get parameters, return conservative bound */
    if (strm == Z_NULL || strm->state == Z_NULL)
        return destLen;

    /* if not default parameters, return conservative bound */
    s = strm->state;
    if (s->w_bits != 15 || s->hash_bits != 8 + 7)
        return destLen;

    /* default settings: return tight bound for that case */
    return compressBound(sourceLen);
}

/* =========================================================================
 * Put a short in the pending buffer. The 16-bit value is put in MSB order.
 * IN assertion: the stream state is correct and there is enough room in
 * pending_buf.
 */
local void putShortMSB (s, b)
    deflate_state *s;
    uInt b;
{
    put_byte(s, (Byte)(b >> 8));
    put_byte(s, (Byte)(b & 0xff));
}

local void putShortMSB_ipp (s, b)
    deflate_state *s;
    uInt b;
{
    put_byte_ipp(s, (Byte)(b >> 8));
    put_byte_ipp(s, (Byte)(b & 0xff));
}

/* =========================================================================
 * Flush as much pending output as possible. All deflate() output goes
 * through this function so some applications may wish to modify it
 * to avoid allocating a large strm->next_out buffer and copying into it.
 * (See also read_buf()).
 */
local void flush_pending(strm)
    z_streamp strm;
{
    unsigned len = strm->state->pending;

    if (len > strm->avail_out) len = strm->avail_out;
    if (len == 0) return;

    zmemcpy(strm->next_out, strm->state->pending_out, len);
    strm->next_out  += len;
    strm->state->pending_out  += len;
    strm->total_out += len;
    strm->avail_out  -= len;
    strm->state->pending -= len;
    if (strm->state->pending == 0) {
        strm->state->pending_out = strm->state->pending_buf;
    }
}

local void zmemcpy_ipp(Bytef* dest, const Bytef* source, uInt len)
{
    int i;
    if( len <= 0 ) return;
    for ( i = 0; i < len; i++ )
    {
       dest[i] = source[i];
    }
    return;
}

local int flush_header_trailer(strm)
    z_streamp strm;
{
    unsigned len = strm->state->header_trailer_bytes_valid;

    if(len == 0) return 0;

    if (len > strm->avail_out)
    {
       zmemcpy_ipp(strm->next_out, strm->state->header_trailer_buff, strm->avail_out);
       zmemcpy_ipp(strm->state->header_trailer_buff, &(strm->state->header_trailer_buff[strm->avail_out]), (len - strm->avail_out) );
       strm->state->header_trailer_bytes_valid -= strm->avail_out;
       strm->total_out += strm->avail_out;
       strm->next_out += strm->avail_out;
       strm->avail_out = 0;

       return (-1);
    }
    else
    {
       zmemcpy_ipp(strm->next_out, strm->state->header_trailer_buff, len);
       strm->state->header_trailer_bytes_valid = 0;
       strm->total_out += len;
       strm->next_out += len;
       strm->avail_out -= len;

       return 0;
    } /* if else */

} /* flush_header_trailer() */

/* ========================================================================= */

int ZEXPORT deflate (strm, flush)
    z_streamp strm;
    int flush;
{
  deflate_state *s;
  IppLZ77State_8u* pLZ77State;
  IppLZ77DeflateStatus deflateStatus;
  IppLZ77Flush ippflush;
  IppLZ77Pair* pPairs;
  IppLZ77Pair* cpPairs;
  IppStatus status;
  int pairsInd;
  int pairsLen;
  int psrcLen;
  int pdstLen;
  int srcLen;
  Ipp8u* pSrc;
  int dstLen;
  Ipp8u* pDst;
  int chslen;

  unsigned int header;
  unsigned int level_flags;

  if( flush == Z_NO_FLUSH )
    ippflush = IppLZ77NoFlush;
  else if( flush == Z_SYNC_FLUSH )
    ippflush = IppLZ77SyncFlush;
  else if( flush == Z_FULL_FLUSH )
    ippflush = IppLZ77FullFlush;
  else if( flush == Z_FINISH)
    ippflush = IppLZ77FinishFlush;

  if(strm == Z_NULL || strm->state == Z_NULL || strm->state->ipp_state == Z_NULL || flush < 0)
    return Z_STREAM_ERROR;
    s = strm->state;
  pLZ77State = strm->state->ipp_state;

  if(strm->next_out == Z_NULL || (strm->next_in == Z_NULL && strm->avail_in != 0) )
    return Z_STREAM_ERROR;

  for( ; ; )
  {

    if(flush_header_trailer(strm) < 0) return Z_OK;

    ippsEncodeLZ77GetStatus_8u(&deflateStatus, pLZ77State);
    if(deflateStatus == IppLZ77StatusInit)
    {

#ifdef GZIP
      if (s->wrap == 2)
      {
         put_byte_ipp(s, 31);
         put_byte_ipp(s, 139);
         put_byte_ipp(s, 8);
         put_byte_ipp(s, 0);
         put_byte_ipp(s, 0);
         put_byte_ipp(s, 0);
         put_byte_ipp(s, 0);
         put_byte_ipp(s, 0);
         put_byte_ipp(s, 4);
         put_byte_ipp(s, 255);
      }
      else
#endif
      {
         header = (Z_DEFLATED + (7<<4)) << 8;
         level_flags = 2;
         header |= (level_flags << 6);
         header += 31 - (header % 31);
         putShortMSB_ipp(s, header);
//         strm->adler = 0;
      }

      deflateStatus = IppLZ77StatusLZ77Process;
      ippsEncodeLZ77SetStatus_8u(deflateStatus, pLZ77State);

      if(flush_header_trailer(strm) < 0)
      {
        return Z_OK;
      } /* if */
    } /* IppLZ77StatusInit */

    ippsEncodeLZ77GetStatus_8u(&deflateStatus, pLZ77State);
    if(deflateStatus == IppLZ77StatusLZ77Process)
    {
      ippsEncodeLZ77GetPairs_8u(&cpPairs, &pairsInd, &pairsLen, pLZ77State);
      pPairs = cpPairs + pairsInd;
      pdstLen = pairsLen - pairsInd;

      pSrc = strm->next_in;
      srcLen = strm->avail_in;
      strm->total_in += srcLen;
      chslen = srcLen;

      status = ippsEncodeLZ77_8u(&pSrc, &srcLen, &pPairs, &pdstLen, ippflush, pLZ77State);

      strm->total_in -= srcLen;
      chslen -= srcLen;

      if(s->wrap == 1) strm->adler = adler32(strm->adler, strm->next_in, chslen);

      strm->next_in = pSrc;
      strm->avail_in = srcLen;

      pairsInd = pairsLen - pdstLen;
      ippsEncodeLZ77SetPairs_8u(cpPairs, pairsInd, pairsLen, pLZ77State);

      if(status < 0 )
        return Z_STREAM_ERROR;
      else if(status == ippStsNoErr && ippflush == IppLZ77NoFlush)
        return Z_OK;
      else
      {
        deflateStatus = IppLZ77StatusHuffProcess;
        ippsEncodeLZ77SetStatus_8u(deflateStatus, pLZ77State);
      }
    } /* IppLZ77StatusLZ77Process */

    ippsEncodeLZ77GetStatus_8u(&deflateStatus, pLZ77State);
    if(deflateStatus == IppLZ77StatusHuffProcess)
    {
      ippsEncodeLZ77GetPairs_8u(&cpPairs, &pairsInd, &pairsLen, pLZ77State);
      psrcLen = pairsLen - pairsInd;
      pPairs = cpPairs + pairsInd;

      pDst = strm->next_out;
      dstLen = strm->avail_out;
      strm->total_out += dstLen;

#ifdef FORCE_STATIC
      status = ippsEncodeLZ77FixedHuff_8u(&pPairs, &psrcLen, &pDst, &dstLen, ippflush, pLZ77State);
#else
      status = ippsEncodeLZ77DynamicHuff_8u(&pPairs, &psrcLen, &pDst, &dstLen, ippflush, pLZ77State);
#endif

      strm->total_out -= dstLen;
      strm->next_out = pDst;
      strm->avail_out = dstLen;
      pairsInd = pairsLen - psrcLen;

      if(status < 0)
        return Z_STREAM_ERROR;

      if(status == ippStsDstSizeLessExpected)
      {
        ippsEncodeLZ77SetPairs_8u(cpPairs, pairsInd, pairsLen, pLZ77State);
        return Z_OK;
      }
      else if(status == ippStsNoErr)
      {
        deflateStatus = IppLZ77StatusLZ77Process;
        ippsEncodeLZ77SetStatus_8u(deflateStatus, pLZ77State);

        if(ippflush != IppLZ77FinishFlush)
          return Z_OK;
      }
      else if(status == ippStsStreamEnd)
      {
        deflateStatus = IppLZ77StatusFinal;
        ippsEncodeLZ77SetStatus_8u(deflateStatus, pLZ77State);
      } /* if else */

    } /* IppLZ77StatusHuffProcess */

    ippsEncodeLZ77GetStatus_8u(&deflateStatus, pLZ77State);
    if(deflateStatus == IppLZ77StatusFinal)
    {

      if(s->wrap < 0) return Z_STREAM_END;

      if(s->wrap == 2)
      {
        pDst = strm->next_out;
        dstLen = strm->avail_out;
        strm->total_out += dstLen;

        status = ippsEncodeLZ77Flush_8u(&pDst, &dstLen, pLZ77State);

        strm->total_out -= dstLen;
        strm->next_out = pDst;
        strm->avail_out = dstLen;

        if(status < 0 )
          return Z_STREAM_ERROR;

        if(status == ippStsDstSizeLessExpected)
          return Z_OK;
      } /* if */
      else if(s->wrap == 1)
      {
        if(s->trailer_empty == 1)
        {
          putShortMSB_ipp(s, (uInt)(strm->adler >> 16));
          putShortMSB_ipp(s, (uInt)(strm->adler & 0xffff));
        }

        if(flush_header_trailer(strm) < 0)
        {
          s->trailer_empty = 0;
          return Z_OK;
        } /* if */
      } /* else */

      if(s->wrap > 0) s->wrap = -s->wrap; /* write the trailer only once! */

⌨️ 快捷键说明

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