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

📄 inflate.c

📁 许多压缩算法都用到了ZLIP算法
💻 C
📖 第 1 页 / 共 3 页
字号:
        while (bits < (unsigned)(n)) \            PULLBYTE(); \    } while (0)/* Return the low n bits of the bit accumulator (n < 16) */#define BITS(n) \    ((unsigned)hold & ((1U << (n)) - 1))/* Remove n bits from the bit accumulator */#define DROPBITS(n) \    do { \        hold >>= (n); \        bits -= (unsigned)(n); \    } while (0)/* Remove zero to seven bits as needed to go to a byte boundary */#define BYTEBITS() \    do { \        hold >>= bits & 7; \        bits -= bits & 7; \    } while (0)/* Reverse the bytes in a 32-bit value */#define REVERSE(q) \    ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \     (((q) & 0xff00) << 8) + (((q) & 0xff) << 24))/*   inflate() uses a state machine to process as much input data and generate as   much output data as possible before returning.  The state machine is   structured roughly as follows:    for (;;) switch (state) {    ...    case STATEn:        if (not enough input data or output space to make progress)            return;        ... make progress ...        state = STATEm;        break;    ...    }   so when inflate() is called again, the same case is attempted again, and   if the appropriate resources are provided, the machine proceeds to the   next state.  The NEEDBITS() macro is usually the way the state evaluates   whether it can proceed or should return.  NEEDBITS() does the return if   the requested bits are not available.  The typical use of the BITS macros   is:        NEEDBITS(n);        ... do something with BITS(n) ...        DROPBITS(n);   where NEEDBITS(n) either returns from inflate() if there isn't enough   input left to load n bits into the accumulator, or it continues.  BITS(n)   gives the low n bits in the accumulator.  When done, DROPBITS(n) drops   the low n bits off the accumulator.  INITBITS() clears the accumulator   and sets the number of available bits to zero.  BYTEBITS() discards just   enough bits to put the accumulator on a byte boundary.  After BYTEBITS()   and a NEEDBITS(8), then BITS(8) would return the next byte in the stream.   NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return   if there is no input available.  The decoding of variable length codes uses   PULLBYTE() directly in order to pull just enough bytes to decode the next   code, and no more.   Some states loop until they get enough input, making sure that enough   state information is maintained to continue the loop where it left off   if NEEDBITS() returns in the loop.  For example, want, need, and keep   would all have to actually be part of the saved state in case NEEDBITS()   returns:    case STATEw:        while (want < need) {            NEEDBITS(n);            keep[want++] = BITS(n);            DROPBITS(n);        }        state = STATEx;    case STATEx:   As shown above, if the next state is also the next case, then the break   is omitted.   A state may also return if there is not enough output space available to   complete that state.  Those states are copying stored data, writing a   literal byte, and copying a matching string.   When returning, a "goto inf_leave" is used to update the total counters,   update the check value, and determine whether any progress has been made   during that inflate() call in order to return the proper return code.   Progress is defined as a change in either strm->avail_in or strm->avail_out.   When there is a window, goto inf_leave will update the window with the last   output written.  If a goto inf_leave occurs in the middle of decompression   and there is no window currently, goto inf_leave will create one and copy   output to the window for the next call of inflate().   In this implementation, the flush parameter of inflate() only affects the   return code (per zlib.h).  inflate() always writes as much as possible to   strm->next_out, given the space available and the provided input--the effect   documented in zlib.h of Z_SYNC_FLUSH.  Furthermore, inflate() always defers   the allocation of and copying into a sliding window until necessary, which   provides the effect documented in zlib.h for Z_FINISH when the entire input   stream available.  So the only thing the flush parameter actually does is:   when flush is set to Z_FINISH, inflate() cannot return Z_OK.  Instead it   will return Z_BUF_ERROR if it has not reached the end of the stream. */int ZEXPORT inflate(strm, flush)z_streamp strm;int flush;{    struct inflate_state FAR *state;    unsigned char FAR *next;    /* next input */    unsigned char FAR *put;     /* next output */    unsigned have, left;        /* available input and output */    unsigned long hold;         /* bit buffer */    unsigned bits;              /* bits in bit buffer */    unsigned in, out;           /* save starting available input and output */    unsigned copy;              /* number of stored or match bytes to copy */    unsigned char FAR *from;    /* where to copy match bytes from */    code this;                  /* current decoding table entry */    code last;                  /* parent table entry */    unsigned len;               /* length to copy for repeats, bits to drop */    int ret;                    /* return code */#ifdef GUNZIP    unsigned char hbuf[4];      /* buffer for gzip header crc calculation */#endif    static const unsigned short order[19] = /* permutation of code lengths */        {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};    if (strm == Z_NULL || strm->state == Z_NULL || strm->next_out == Z_NULL ||        (strm->next_in == Z_NULL && strm->avail_in != 0))        return Z_STREAM_ERROR;    state = (struct inflate_state FAR *)strm->state;    if (state->mode == TYPE) state->mode = TYPEDO;      /* skip check */    LOAD();    in = have;    out = left;    ret = Z_OK;    for (;;)        switch (state->mode) {        case HEAD:            if (state->wrap == 0) {                state->mode = TYPEDO;                break;            }            NEEDBITS(16);#ifdef GUNZIP            if ((state->wrap & 2) && hold == 0x8b1f) {  /* gzip header */                state->check = crc32(0L, Z_NULL, 0);                CRC2(state->check, hold);                INITBITS();                state->mode = FLAGS;                break;            }            state->flags = 0;           /* expect zlib header */            if (!(state->wrap & 1) ||   /* check if zlib header allowed */#else            if (#endif                ((BITS(8) << 8) + (hold >> 8)) % 31) {                strm->msg = (char *)"incorrect header check";                state->mode = BAD;                break;            }            if (BITS(4) != Z_DEFLATED) {                strm->msg = (char *)"unknown compression method";                state->mode = BAD;                break;            }            DROPBITS(4);            if (BITS(4) + 8 > state->wbits) {                strm->msg = (char *)"invalid window size";                state->mode = BAD;                break;            }            Tracev((stderr, "inflate:   zlib header ok\n"));            strm->adler = state->check = adler32(0L, Z_NULL, 0);            state->mode = hold & 0x200 ? DICTID : TYPE;            INITBITS();            break;#ifdef GUNZIP        case FLAGS:            NEEDBITS(16);            state->flags = (int)(hold);            if ((state->flags & 0xff) != Z_DEFLATED) {                strm->msg = (char *)"unknown compression method";                state->mode = BAD;                break;            }            if (state->flags & 0xe000) {                strm->msg = (char *)"unknown header flags set";                state->mode = BAD;                break;            }            if (state->flags & 0x0200) CRC2(state->check, hold);            INITBITS();            state->mode = TIME;        case TIME:            NEEDBITS(32);            if (state->flags & 0x0200) CRC4(state->check, hold);            INITBITS();            state->mode = OS;        case OS:            NEEDBITS(16);            if (state->flags & 0x0200) CRC2(state->check, hold);            INITBITS();            state->mode = EXLEN;        case EXLEN:            if (state->flags & 0x0400) {                NEEDBITS(16);                state->length = (unsigned)(hold);                if (state->flags & 0x0200) CRC2(state->check, hold);                INITBITS();            }            state->mode = EXTRA;        case EXTRA:            if (state->flags & 0x0400) {                copy = state->length;                if (copy > have) copy = have;                if (copy) {                    if (state->flags & 0x0200)                        state->check = crc32(state->check, next, copy);                    have -= copy;                    next += copy;                    state->length -= copy;                }                if (state->length) goto inf_leave;            }            state->mode = NAME;        case NAME:            if (state->flags & 0x0800) {                if (have == 0) goto inf_leave;                copy = 0;                do {                    len = (unsigned)(next[copy++]);                } while (len && copy < have);                if (state->flags & 0x02000)                    state->check = crc32(state->check, next, copy);                have -= copy;                next += copy;                if (len) goto inf_leave;            }            state->mode = COMMENT;        case COMMENT:            if (state->flags & 0x1000) {                if (have == 0) goto inf_leave;                copy = 0;                do {                    len = (unsigned)(next[copy++]);                } while (len && copy < have);                if (state->flags & 0x02000)                    state->check = crc32(state->check, next, copy);                have -= copy;                next += copy;                if (len) goto inf_leave;            }            state->mode = HCRC;        case HCRC:            if (state->flags & 0x0200) {                NEEDBITS(16);                if (hold != (state->check & 0xffff)) {                    strm->msg = (char *)"header crc mismatch";                    state->mode = BAD;                    break;                }                INITBITS();            }            strm->adler = state->check = crc32(0L, Z_NULL, 0);            state->mode = TYPE;            break;#endif        case DICTID:            NEEDBITS(32);            strm->adler = state->check = REVERSE(hold);            INITBITS();            state->mode = DICT;        case DICT:            if (state->havedict == 0) {                RESTORE();                return Z_NEED_DICT;            }            strm->adler = state->check = adler32(0L, Z_NULL, 0);            state->mode = TYPE;        case TYPE:            if (flush == Z_BLOCK) goto inf_leave;        case TYPEDO:            if (state->last) {                BYTEBITS();                state->mode = CHECK;                break;            }            NEEDBITS(3);            state->last = BITS(1);            DROPBITS(1);            switch (BITS(2)) {            case 0:                             /* stored block */                Tracev((stderr, "inflate:     stored block%s\n",                        state->last ? " (last)" : ""));                state->mode = STORED;                break;            case 1:                             /* fixed block */                fixedtables(state);                Tracev((stderr, "inflate:     fixed codes block%s\n",                        state->last ? " (last)" : ""));                state->mode = LEN;              /* decode codes */                break;            case 2:                             /* dynamic block */                Tracev((stderr, "inflate:     dynamic codes block%s\n",                        state->last ? " (last)" : ""));                state->mode = TABLE;                break;            case 3:                strm->msg = (char *)"invalid block type";                state->mode = BAD;            }            DROPBITS(2);            break;        case STORED:            BYTEBITS();                         /* go to byte boundary */            NEEDBITS(32);            if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {                strm->msg = (char *)"invalid stored block lengths";                state->mode = BAD;                break;            }            state->length = (unsigned)hold & 0xffff;            Tracev((stderr, "inflate:       stored length %u\n",                    state->length));            INITBITS();            state->mode = COPY;        case COPY:            copy = state->length;            if (copy) {                if (copy > have) copy = have;                if (copy > left) copy = left;                if (copy == 0) goto inf_leave;                zmemcpy(put, next, copy);                have -= copy;                next += copy;                left -= copy;                put += copy;                state->length -= copy;                break;            }            Tracev((stderr, "inflate:       stored end\n"));            state->mode = TYPE;            break;        case TABLE:            NEEDBITS(14);            state->nlen = BITS(5) + 257;            DROPBITS(5);            state->ndist = BITS(5) + 1;            DROPBITS(5);            state->ncode = BITS(4) + 4;            DROPBITS(4);#ifndef PKZIP_BUG_WORKAROUND            if (state->nlen > 286 || state->ndist > 30) {                strm->msg = (char *)"too many length or distance symbols";                state->mode = BAD;                break;            }#endif            Tracev((stderr, "inflate:       table sizes ok\n"));            state->have = 0;            state->mode = LENLENS;        case LENLENS:            while (state->have < state->ncode) {                NEEDBITS(3);                state->lens[order[state->have++]] = (unsigned short)BITS(3);                DROPBITS(3);            }            while (state->have < 19)                state->lens[order[state->have++]] = 0;            state->next = state->codes;            state->lencode = (code const FAR *)(state->next);            state->lenbits = 7;            ret = inflate_table(CODES, state->lens, 19, &(state->next),                                &(state->lenbits), state->work);            if (ret) {                strm->msg = (char *)"invalid code lengths set";                state->mode = BAD;                break;            }            Tracev((stderr, "inflate:       code lengths ok\n"));            state->have = 0;            state->mode = CODELENS;        case CODELENS:            while (state->have < state->nlen + state->ndist) {                for (;;) {                    this = state->lencode[BITS(state->lenbits)];                    if ((unsigned)(this.bits) <= bits) break;                    PULLBYTE();                }                if (this.val < 16) {                    NEEDBITS(this.bits);                    DROPBITS(this.bits);                    state->lens[state->have++] = this.val;                }                else {                    if (this.val == 16) {                        NEEDBITS(this.bits + 2);                        DROPBITS(this.bits);                        if (state->have == 0) {                            strm->msg = (char *)"invalid bit length repeat";                            state->mode = BAD;                            break;                        }                        len = state->lens[state->have - 1];                        copy = 3 + BITS(2);                        DROPBITS(2);                    }                    else if (this.val == 17) {                        NEEDBITS(this.bits + 3);                        DROPBITS(this.bits);                        len = 0;                        copy = 3 + BITS(3);                        DROPBITS(3);                    }                    else {                        NEEDBITS(this.bits + 7);

⌨️ 快捷键说明

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