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

📄 ipp_zlib.c

📁 intel的ipp性能库的示例代码
💻 C
📖 第 1 页 / 共 5 页
字号:
                        PUP(out) = PUP(from);
                        PUP(out) = PUP(from);
                        len -= 3;
                    } while (len > 2);
                    if (len) {
                        PUP(out) = PUP(from);
                        if (len > 1)
                            PUP(out) = PUP(from);
                    }
                }
            }
            else if ((op & 64) == 0) {          /* 2nd level distance code */
                this = dcode[this.val + (hold & ((1U << op) - 1))];
                goto dodist;
            }
            else {
                strm->msg = (char *)"invalid distance code";
                state->mode = BAD;
                break;
            }
        }
        else if ((op & 64) == 0) {              /* 2nd level length code */
            this = lcode[this.val + (hold & ((1U << op) - 1))];
            goto dolen;
        }
        else if (op & 32) {                     /* end-of-block */
            state->mode = TYPE;
            break;
        }
        else {
            strm->msg = (char *)"invalid literal/length code";
            state->mode = BAD;
            break;
        }
    } while (in < last && out < end);

    /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
    len = bits >> 3;
    in -= len;
    bits -= len << 3;
    hold &= (1U << bits) - 1;

    /* update state and return */
    strm->next_in = in + OFF;
    strm->next_out = out + OFF;
    strm->avail_in = (unsigned)(in < last ? 5 + (last - in) : 5 - (in - last));
    strm->avail_out = (unsigned)(out < end ?
                                 257 + (end - out) : 257 - (out - end));
    state->hold = hold;
    state->bits = bits;
    return;
} /* inflate_fast() */

/**********************************************************************************/
/*                             Macros for inflate()                               */
/**********************************************************************************/

#ifdef GUNZIP
#  define UPDATE(check, buf, len) \
    (state->flags ? crc32(check, buf, len) : adler32(check, buf, len))
#else
#define UPDATE(check, buf, len) adler32(check, buf, len)
#endif

#ifdef GUNZIP
#  define CRC2(check, word) \
    do { \
        hbuf[0] = (unsigned char)(word); \
        hbuf[1] = (unsigned char)((word) >> 8); \
        check = crc32(check, hbuf, 2); \
    } while (0)

#  define CRC4(check, word) \
    do { \
        hbuf[0] = (unsigned char)(word); \
        hbuf[1] = (unsigned char)((word) >> 8); \
        hbuf[2] = (unsigned char)((word) >> 16); \
        hbuf[3] = (unsigned char)((word) >> 24); \
        check = crc32(check, hbuf, 4); \
    } while (0)
#endif

#define LOAD() \
    do { \
        put = strm->next_out; \
        left = strm->avail_out; \
        next = strm->next_in; \
        have = strm->avail_in; \
        hold = state->hold; \
        bits = state->bits; \
    } while (0)

#define RESTORE() \
    do { \
        strm->next_out = put; \
        strm->avail_out = left; \
        strm->next_in = next; \
        strm->avail_in = have; \
        state->hold = hold; \
        state->bits = bits; \
    } while (0)

#define INITBITS() \
    do { \
        hold = 0; \
        bits = 0; \
    } while (0)

#define PULLBYTE() \
    do { \
        if (have == 0) goto inf_leave; \
        have--; \
        hold += (unsigned long)(*next++) << bits; \
        bits += 8; \
    } while (0)

#define NEEDBITS(n) \
    do { \
        while (bits < (unsigned)(n)) \
            PULLBYTE(); \
    } while (0)

#define BITS(n) \
    ((unsigned)hold & ((1U << (n)) - 1))

#define DROPBITS(n) \
    do { \
        hold >>= (n); \
        bits -= (unsigned)(n); \
    } while (0)

#define BYTEBITS() \
    do { \
        hold >>= bits & 7; \
        bits -= bits & 7; \
    } while (0)

#define REVERSE(q) \
    ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \
     (((q) & 0xff00) << 8) + (((q) & 0xff) << 24))

/**********************************************************************************/
/*                     Implementation of ZLIB interfaces                          */
/**********************************************************************************/

int
deflate(z_stream* strm, int flush)
{
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;

 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 ||
    flush > Z_FINISH || flush < 0)
 {
   return Z_STREAM_ERROR;
 }
 pLZ77State = strm->state;

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

 ippsEncodeLZ77GetStatus_8u(&deflateStatus, pLZ77State);
 if(deflateStatus == IppLZ77StatusInit)
 {
#ifdef GZIP
   put_byte(strm, 31);
   put_byte(strm, 139);
   put_byte(strm, 8);
   put_byte(strm, 0);
   put_byte(strm, 0);
   put_byte(strm, 0);
   put_byte(strm, 0);
   put_byte(strm, 0);
   put_byte(strm, 4);
   put_byte(strm, 255);
   deflateStatus = IppLZ77StatusLZ77Process;
   ippsEncodeLZ77SetStatus_8u(deflateStatus, pLZ77State);

#endif
#ifndef GZIP
           uInt header = (Z_DEFLATED + (7<<4)) << 8;
           uInt level_flags;
           level_flags = 0;
           header |= (level_flags << 6);
           header += 31 - (header % 31);
           ippsEncodeLZ77SetStatus_8u(IppLZ77StatusLZ77Process, pLZ77State);
           putShortMSB(strm, header);
           strm->adler = 0;
#endif
 } /* 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;

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

    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;

    status = ippsEncodeLZ77FixedHuff_8u(&pPairs, &psrcLen, &pDst, &dstLen, ippflush, pLZ77State);

    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);
      return Z_OK;
    } /* if else */

    deflateStatus = IppLZ77StatusFinal;
    ippsEncodeLZ77SetStatus_8u(deflateStatus, pLZ77State);

 } /* IppLZ77StatusHuffProcess */

 ippsEncodeLZ77GetStatus_8u(&deflateStatus, pLZ77State);
 if(deflateStatus == IppLZ77StatusFinal)
 {
   pDst = strm->next_out;
   dstLen = strm->avail_out;

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

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

   if(status < 0 )
     return Z_STREAM_ERROR;

   if(status == ippStsDstSizeLessExpected)
     return Z_OK;

   return Z_STREAM_END;
 } /* IppLZ77StatusFinal */

 return Z_STREAM_END;
} /* deflate() */

/**********************************************************************************/

int
deflateInit_(z_stream* strm, int level, const char *version, int stream_size)
{
    return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL,
                         Z_DEFAULT_STRATEGY, version, stream_size);
} /* deflateInit_() */

/**********************************************************************************/

int
deflateInit2_(z_stream* strm, int  level, int  method,
              int windowBits, int memLevel, int strategy,
              const char *version, int stream_size)
{
    IppLZ77State_8u* pLZ77State;
    IppStatus status;
    static const char my_version[] = ZLIB_VERSION;

    if ( version == Z_NULL || (stream_size != sizeof(z_stream)) ) {
        return Z_VERSION_ERROR;
    }
    if (strm == Z_NULL) return Z_STREAM_ERROR;
    strm->msg = Z_NULL;
    level = 1;

    if ( memLevel != DEF_MEM_LEVEL || method != Z_DEFLATED ||
        windowBits != 15 || level != 1 || strategy != 0)
    {
        return Z_STREAM_ERROR;
    }

#ifdef GZIP
    status = ippsEncodeLZ77InitAlloc_8u(IppLZ77FastCompr, IppLZ77CRC32, &pLZ77State);
#endif
#ifndef GZIP
    status = ippsEncodeLZ77InitAlloc_8u(IppLZ77FastCompr, IppLZ77Adler32, &pLZ77State);
#endif

    if (status != 0)
    {
      return Z_MEM_ERROR;
    }
    if (pLZ77State == Z_NULL) return Z_MEM_ERROR;

    strm->state = pLZ77State;
    strm->total_in = strm->total_out = 0;
    strm->adler = 0;

    return Z_OK;
} /* deflateInit2_() */

/**********************************************************************************/

int
deflateEnd (z_stream* strm)
{
    IppLZ77DeflateStatus deflateStatus;

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

    ippsEncodeLZ77GetStatus_8u(&deflateStatus, strm->state);

    if(deflateStatus != IppLZ77StatusInit && deflateStatus != IppLZ77StatusLZ77Process &&
       deflateStatus != IppLZ77StatusHuffProcess && deflateStatus != IppLZ77StatusFinal) {
      return Z_STREAM_ERROR;
    }

    ippsLZ77Free_8u(strm->state);
    return deflateStatus == IppLZ77StatusLZ77Process ? Z_DATA_ERROR : Z_OK;
} /* deflateEnd() */

/**********************************************************************************/

int
deflateReset (z_stream* strm)
{
    if (strm == Z_NULL || strm->state == Z_NULL)
      return Z_STREAM_ERROR;

⌨️ 快捷键说明

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