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

📄 inflate.c

📁 Lib files of linux kernel
💻 C
📖 第 1 页 / 共 3 页
字号:
/* inflate.c -- zlib decompression * Copyright (C) 1995-2005 Mark Adler * For conditions of distribution and use, see copyright notice in zlib.h * * Based on zlib 1.2.3 but modified for the Linux Kernel by * Richard Purdie <richard@openedhand.com> * * Changes mainly for static instead of dynamic memory allocation * */#include <linux/zutil.h>#include "inftrees.h"#include "inflate.h"#include "inffast.h"#include "infutil.h"int zlib_inflate_workspacesize(void){    return sizeof(struct inflate_workspace);}int zlib_inflateReset(z_streamp strm){    struct inflate_state *state;    if (strm == NULL || strm->state == NULL) return Z_STREAM_ERROR;    state = (struct inflate_state *)strm->state;    strm->total_in = strm->total_out = state->total = 0;    strm->msg = NULL;    strm->adler = 1;        /* to support ill-conceived Java test suite */    state->mode = HEAD;    state->last = 0;    state->havedict = 0;    state->dmax = 32768U;    state->hold = 0;    state->bits = 0;    state->lencode = state->distcode = state->next = state->codes;    /* Initialise Window */    state->wsize = 1U << state->wbits;    state->write = 0;    state->whave = 0;    return Z_OK;}#if 0int zlib_inflatePrime(z_streamp strm, int bits, int value){    struct inflate_state *state;    if (strm == NULL || strm->state == NULL) return Z_STREAM_ERROR;    state = (struct inflate_state *)strm->state;    if (bits > 16 || state->bits + bits > 32) return Z_STREAM_ERROR;    value &= (1L << bits) - 1;    state->hold += value << state->bits;    state->bits += bits;    return Z_OK;}#endifint zlib_inflateInit2(z_streamp strm, int windowBits){    struct inflate_state *state;    if (strm == NULL) return Z_STREAM_ERROR;    strm->msg = NULL;                 /* in case we return an error */    state = &WS(strm)->inflate_state;    strm->state = (struct internal_state *)state;    if (windowBits < 0) {        state->wrap = 0;        windowBits = -windowBits;    }    else {        state->wrap = (windowBits >> 4) + 1;    }    if (windowBits < 8 || windowBits > 15) {        return Z_STREAM_ERROR;    }    state->wbits = (unsigned)windowBits;    state->window = &WS(strm)->working_window[0];    return zlib_inflateReset(strm);}/*   Return state with length and distance decoding tables and index sizes set to   fixed code decoding.  This returns fixed tables from inffixed.h. */static void zlib_fixedtables(struct inflate_state *state){#   include "inffixed.h"    state->lencode = lenfix;    state->lenbits = 9;    state->distcode = distfix;    state->distbits = 5;}/*   Update the window with the last wsize (normally 32K) bytes written before   returning. This is only called when a window is already in use, or when   output has been written during this inflate call, but the end of the deflate   stream has not been reached yet. It is also called to window dictionary data   when a dictionary is loaded.   Providing output buffers larger than 32K to inflate() should provide a speed   advantage, since only the last 32K of output is copied to the sliding window   upon return from inflate(), and since all distances after the first 32K of   output will fall in the output data, making match copies simpler and faster.   The advantage may be dependent on the size of the processor's data caches. */static void zlib_updatewindow(z_streamp strm, unsigned out){    struct inflate_state *state;    unsigned copy, dist;    state = (struct inflate_state *)strm->state;    /* copy state->wsize or less output bytes into the circular window */    copy = out - strm->avail_out;    if (copy >= state->wsize) {        memcpy(state->window, strm->next_out - state->wsize, state->wsize);        state->write = 0;        state->whave = state->wsize;    }    else {        dist = state->wsize - state->write;        if (dist > copy) dist = copy;        memcpy(state->window + state->write, strm->next_out - copy, dist);        copy -= dist;        if (copy) {            memcpy(state->window, strm->next_out - copy, copy);            state->write = copy;            state->whave = state->wsize;        }        else {            state->write += dist;            if (state->write == state->wsize) state->write = 0;            if (state->whave < state->wsize) state->whave += dist;        }    }}/* * At the end of a Deflate-compressed PPP packet, we expect to have seen * a `stored' block type value but not the (zero) length bytes. *//*   Returns true if inflate is currently at the end of a block generated by   Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP   implementation to provide an additional safety check. PPP uses   Z_SYNC_FLUSH but removes the length bytes of the resulting empty stored   block. When decompressing, PPP checks that at the end of input packet,   inflate is waiting for these length bytes. */static int zlib_inflateSyncPacket(z_streamp strm){    struct inflate_state *state;    if (strm == NULL || strm->state == NULL) return Z_STREAM_ERROR;    state = (struct inflate_state *)strm->state;    if (state->mode == STORED && state->bits == 0) {	state->mode = TYPE;        return Z_OK;    }    return Z_DATA_ERROR;}/* Macros for inflate(): *//* check function to use adler32() for zlib or crc32() for gzip */#define UPDATE(check, buf, len) zlib_adler32(check, buf, len)/* Load registers with state in inflate() for speed */#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)/* Restore state from registers in inflate() */#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)/* Clear the input bit accumulator */#define INITBITS() \    do { \        hold = 0; \        bits = 0; \    } while (0)/* Get a byte of input into the bit accumulator, or return from inflate()   if there is no input available. */#define PULLBYTE() \    do { \        if (have == 0) goto inf_leave; \        have--; \        hold += (unsigned long)(*next++) << bits; \        bits += 8; \    } while (0)/* Assure that there are at least n bits in the bit accumulator.  If there is   not enough available input to do that, then return from inflate(). */#define NEEDBITS(n) \    do { \        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.

⌨️ 快捷键说明

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