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

📄 inflate.c

📁 Intel 5.2 版本的zlib 库实现
💻 C
📖 第 1 页 / 共 5 页
字号:
/* inflate.c -- zlib decompression
 * Copyright (C) 1995-2003 Mark Adler
 * For conditions of distribution and use, see copyright notice in zlib.h
 */

/*
 * Change history:
 *
 * 1.2.beta0    24 Nov 2002
 * - First version -- complete rewrite of inflate to simplify code, avoid
 *   creation of window when not needed, minimize use of window when it is
 *   needed, make inffast.c even faster, implement gzip decoding, and to
 *   improve code readability and style over the previous zlib inflate code
 *
 * 1.2.beta1    25 Nov 2002
 * - Use pointers for available input and output checking in inffast.c
 * - Remove input and output counters in inffast.c
 * - Change inffast.c entry and loop from avail_in >= 7 to >= 6
 * - Remove unnecessary second byte pull from length extra in inffast.c
 * - Unroll direct copy to three copies per loop in inffast.c
 *
 * 1.2.beta2    4 Dec 2002
 * - Change external routine names to reduce potential conflicts
 * - Correct filename to inffixed.h for fixed tables in inflate.c
 * - Make hbuf[] unsigned char to match parameter type in inflate.c
 * - Change strm->next_out[-state->offset] to *(strm->next_out - state->offset)
 *   to avoid negation problem on Alphas (64 bit) in inflate.c
 *
 * 1.2.beta3    22 Dec 2002
 * - Add comments on state->bits assertion in inffast.c
 * - Add comments on op field in inftrees.h
 * - Fix bug in reuse of allocated window after inflateReset()
 * - Remove bit fields--back to byte structure for speed
 * - Remove distance extra == 0 check in inflate_fast()--only helps for lengths
 * - Change post-increments to pre-increments in inflate_fast(), PPC biased?
 * - Add compile time option, POSTINC, to use post-increments instead (Intel?)
 * - Make MATCH copy in inflate() much faster for when inflate_fast() not used
 * - Use local copies of stream next and avail values, as well as local bit
 *   buffer and bit count in inflate()--for speed when inflate_fast() not used
 *
 * 1.2.beta4    1 Jan 2003
 * - Split ptr - 257 statements in inflate_table() to avoid compiler warnings
 * - Move a comment on output buffer sizes from inffast.c to inflate.c
 * - Add comments in inffast.c to introduce the inflate_fast() routine
 * - Rearrange window copies in inflate_fast() for speed and simplification
 * - Unroll last copy for window match in inflate_fast()
 * - Use local copies of window variables in inflate_fast() for speed
 * - Pull out common write == 0 case for speed in inflate_fast()
 * - Make op and len in inflate_fast() unsigned for consistency
 * - Add FAR to lcode and dcode declarations in inflate_fast()
 * - Simplified bad distance check in inflate_fast()
 * - Added inflateBackInit(), inflateBack(), and inflateBackEnd() in new
 *   source file infback.c to provide a call-back interface to inflate for
 *   programs like gzip and unzip -- uses window as output buffer to avoid
 *   window copying
 *
 * 1.2.beta5    1 Jan 2003
 * - Improved inflateBack() interface to allow the caller to provide initial
 *   input in strm.
 * - Fixed stored blocks bug in inflateBack()
 *
 * 1.2.beta6    4 Jan 2003
 * - Added comments in inffast.c on effectiveness of POSTINC
 * - Typecasting all around to reduce compiler warnings
 * - Changed loops from while (1) or do {} while (1) to for (;;), again to
 *   make compilers happy
 * - Changed type of window in inflateBackInit() to unsigned char *
 *
 * 1.2.beta7    27 Jan 2003
 * - Changed many types to unsigned or unsigned short to avoid warnings
 * - Added inflateCopy() function
 *
 * 1.2.0        9 Mar 2003
 * - Changed inflateBack() interface to provide separate opaque descriptors
 *   for the in() and out() functions
 * - Changed inflateBack() argument and in_func typedef to swap the length
 *   and buffer address return values for the input function
 * - Check next_in and next_out for Z_NULL on entry to inflate()
 *
 * The history for versions after 1.2.0 are in ChangeLog in zlib distribution.
 */

#include "zutil.h"
#include "inftrees.h"
#include "inflate.h"
#include "inffast.h"

#ifdef MAKEFIXED
#  ifndef BUILDFIXED
#    define BUILDFIXED
#  endif
#endif

/* function prototypes */
local void fixedtables OF((struct inflate_state FAR *state));
local int updatewindow OF((z_streamp strm, unsigned out));
#ifdef BUILDFIXED
   void makefixed OF((void));
#endif
local unsigned syncsearch OF((unsigned FAR *have, unsigned char FAR *buf,
                              unsigned len));

int ZEXPORT inflateReset(strm)
z_streamp strm;
{
    struct inflate_state FAR *state;
    IppLZ77State_8u* pLZ77State;

#ifdef INFLATE_OMP
 #if defined(_OPENMP)
    IppLZ77State_8u* pLZ77StateMT;
    IppLZ77Pair*     pPairConstMT;
    int              pairsLenConstMT;
    int              pairsIndMT;
 #endif    
#endif
  
    if (strm == Z_NULL || strm->state == Z_NULL ) return Z_STREAM_ERROR;
    
	state = (struct inflate_state FAR *)strm->state;
	pLZ77State = (IppLZ77State_8u*) state->ipp_state;
    if(pLZ77State == 0) return Z_STREAM_ERROR;

#ifdef INFLATE_OMP
 #if defined(_OPENMP)
    pLZ77StateMT = (IppLZ77State_8u*) state->ipp_stateMT;
    if(pLZ77StateMT == 0) return Z_STREAM_ERROR;  
#endif    
#endif

    strm->total_in = strm->total_out = state->total = 0;
    strm->msg = Z_NULL;
    strm->adler = 1;        /* to support ill-conceived Java test suite */
    state->mode = HEAD;
    state->last = 0;
    state->havedict = 0;
    state->wsize = 0;
    state->whave = 0;
    state->hold = 0;
    state->bits = 0;
    state->lencode = state->distcode = state->next = state->codes;

    state->headerFlags = 0;
    state->headerBuf = 0;
    state->headerBits = 0;
    state->headerLength = 0;
    state->blockEnd = 0;
    state->headerMode = infhead;
    ippsDecodeLZ77Reset_8u(pLZ77State);

#ifdef INFLATE_OMP
 #if defined(_OPENMP)
    ippsDecodeLZ77Reset_8u(pLZ77StateMT);
    ippsDecodeLZ77GetPairs_8u(&pPairConstMT, &pairsIndMT, &pairsLenConstMT, pLZ77StateMT);
    pairsIndMT = pairsLenConstMT;
    ippsDecodeLZ77SetPairs_8u(pPairConstMT, pairsIndMT, pairsLenConstMT, pLZ77StateMT);
 #endif    
#endif

    Tracev((stderr, "inflate: reset\n"));
    return Z_OK;
}

int ZEXPORT inflateInit2_(strm, windowBits, version, stream_size)
z_streamp strm;
int windowBits;
const char *version;
int stream_size;
{
    struct inflate_state FAR *state;
    IppLZ77State_8u* ipp_state;
    IppStatus        status;

#ifdef INFLATE_OMP
 #if defined (_OPENMP) 
    IppLZ77State_8u* ipp_stateMT;
    IppLZ77Pair*     pPairConstMT;
    int              pairsLenConstMT;
    int              pairsIndMT;
 #endif
#endif

    if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
        stream_size != (int)(sizeof(z_stream)))
        return Z_VERSION_ERROR;
    if (strm == Z_NULL) return Z_STREAM_ERROR;
    strm->msg = Z_NULL;                 /* in case we return an error */

    if (strm->zalloc == (alloc_func)0) {
        strm->zalloc = zcalloc;
        strm->opaque = (voidpf)0;
    }
    if (strm->zfree == (free_func)0) strm->zfree = zcfree;
    state = (struct inflate_state FAR *)
            ZALLOC(strm, 1, sizeof(struct inflate_state));
    if (state == Z_NULL) return Z_MEM_ERROR;
	Tracev((stderr, "inflate: allocated\n"));
    strm->state = (voidpf)state;

    state->headerMode = infhead;
    state->headerFlags = 0;
    state->headerBuf = 0;
    state->headerBits = 0;
    state->headerLength = 0;
    state->blockEnd = 0;

    if (windowBits < 0) {
        state->wrap = 0;

        if ( ( status  = ippsDecodeLZ77InitAlloc_8u( IppLZ77CRC32, &ipp_state )) != ippStsNoErr ) 
           return Z_MEM_ERROR;

#ifdef INFLATE_OMP 
 #if defined (_OPENMP)
        if ( ( status  = ippsDecodeLZ77InitAlloc_8u( IppLZ77CRC32, &ipp_stateMT )) != ippStsNoErr ) 
           return Z_MEM_ERROR;

        ippsDecodeLZ77GetPairs_8u(&pPairConstMT, &pairsIndMT, &pairsLenConstMT, ipp_stateMT);
        pairsIndMT = pairsLenConstMT;
        ippsDecodeLZ77SetPairs_8u(pPairConstMT, pairsIndMT, pairsLenConstMT, ipp_stateMT);
 #endif
#endif
        windowBits = -windowBits;
    }
    else {
        state->wrap = (windowBits >> 4) + 1;
        if ( ( status  = ippsDecodeLZ77InitAlloc_8u( IppLZ77Adler32, &ipp_state )) != ippStsNoErr ) 
           return Z_MEM_ERROR;
#ifdef INFLATE_OMP 
 #if defined (_OPENMP)
        if ( ( status  = ippsDecodeLZ77InitAlloc_8u( IppLZ77Adler32, &ipp_stateMT )) != ippStsNoErr ) 
           return Z_MEM_ERROR;

        ippsDecodeLZ77GetPairs_8u(&pPairConstMT, &pairsIndMT, &pairsLenConstMT, ipp_stateMT);
        pairsIndMT = pairsLenConstMT;
        ippsDecodeLZ77SetPairs_8u(pPairConstMT, pairsIndMT, pairsLenConstMT, ipp_stateMT);
 #endif
#endif

#ifdef GUNZIP
        if (windowBits < 48) windowBits &= 15;
#endif
    }
    state->ipp_state = ipp_state;
#ifdef INFLATE_OMP 
 #if defined (_OPENMP)
    state->ipp_stateMT = ipp_stateMT;
    state->decodeHuffStatus = ippStsNoErr;
    state->decodeLZ77Status = ippStsNoErr;
    state->lastBlockHuff    = 0;
    state->lastBlockLZ77    = 0;
    state->firstSwapDone    = 0;
 #endif
#endif

    if (windowBits < 8 || windowBits > 15) 
	{
       ippsLZ77Free_8u(ipp_state);
#ifdef INFLATE_OMP 
 #if defined (_OPENMP)
        ippsLZ77Free_8u(ipp_stateMT);
 #endif
#endif
		ZFREE(strm, state);
        strm->state = Z_NULL;
        return Z_STREAM_ERROR;
    }
    state->wbits = (unsigned)windowBits;
    state->window = Z_NULL;
    return inflateReset(strm);
}

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

/*
   Return state with length and distance decoding tables and index sizes set to
   fixed code decoding.  Normally this returns fixed tables from inffixed.h.
   If BUILDFIXED is defined, then instead this routine builds the tables the
   first time it's called, and returns those tables the first time and
   thereafter.  This reduces the size of the code by about 2K bytes, in
   exchange for a little execution time.  However, BUILDFIXED should not be
   used for threaded applications, since the rewriting of the tables and virgin
   may not be thread-safe.
 */
local void fixedtables(state)
struct inflate_state FAR *state;
{
#ifdef BUILDFIXED
    static int virgin = 1;
    static code *lenfix, *distfix;
    static code fixed[544];

    /* build fixed huffman tables if first call (may not be thread safe) */
    if (virgin) {
        unsigned sym, bits;
        static code *next;

        /* literal/length table */
        sym = 0;
        while (sym < 144) state->lens[sym++] = 8;
        while (sym < 256) state->lens[sym++] = 9;
        while (sym < 280) state->lens[sym++] = 7;
        while (sym < 288) state->lens[sym++] = 8;
        next = fixed;
        lenfix = next;
        bits = 9;
        inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work);

        /* distance table */
        sym = 0;
        while (sym < 32) state->lens[sym++] = 5;
        distfix = next;
        bits = 5;
        inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work);

        /* do this just once */
        virgin = 0;
    }
#else /* !BUILDFIXED */
#   include "inffixed.h"
#endif /* BUILDFIXED */
    state->lencode = lenfix;
    state->lenbits = 9;
    state->distcode = distfix;
    state->distbits = 5;
}

#ifdef MAKEFIXED
#include <stdio.h>

/*
   Write out the inffixed.h that is #include'd above.  Defining MAKEFIXED also
   defines BUILDFIXED, so the tables are built on the fly.  makefixed() writes
   those tables to stdout, which would be piped to inffixed.h.  A small program
   can simply call makefixed to do this:

    void makefixed(void);

    int main(void)
    {
        makefixed();
        return 0;
    }

   Then that can be linked with zlib built with MAKEFIXED defined and run:

    a.out > inffixed.h
 */
void makefixed()
{
    unsigned low, size;
    struct inflate_state state;

    fixedtables(&state);
    puts("    /* inffixed.h -- table for decoding fixed codes");
    puts("     * Generated automatically by makefixed().");
    puts("     */");
    puts("");
    puts("    /* WARNING: this file should *not* be used by applications.");
    puts("       It is part of the implementation of this library and is");
    puts("       subject to change. Applications should only use zlib.h.");
    puts("     */");
    puts("");
    size = 1U << 9;
    printf("    static const code lenfix[%u] = {", size);
    low = 0;
    for (;;) {
        if ((low % 7) == 0) printf("\n        ");
        printf("{%u,%u,%d}", state.lencode[low].op, state.lencode[low].bits,
               state.lencode[low].val);
        if (++low == size) break;
        putchar(',');
    }
    puts("\n    };");
    size = 1U << 5;
    printf("\n    static const code distfix[%u] = {", size);
    low = 0;
    for (;;) {
        if ((low % 6) == 0) printf("\n        ");

⌨️ 快捷键说明

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