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

📄 zlib.c

📁 这是著名的jffs2嵌入式日志文件系统的源代码
💻 C
📖 第 1 页 / 共 5 页
字号:
    return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;}/* ========================================================================= * Copy the source state to the destination state. */int deflateCopy (dest, source)    z_streamp dest;    z_streamp source;{    deflate_state *ds;    deflate_state *ss;    ushf *overlay;    if (source == Z_NULL || dest == Z_NULL || source->state == Z_NULL)        return Z_STREAM_ERROR;    ss = (deflate_state *) source->state;    *dest = *source;    ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state));    if (ds == Z_NULL) return Z_MEM_ERROR;    dest->state = (struct internal_state FAR *) ds;    *ds = *ss;    ds->strm = dest;    ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte));    ds->prev   = (Posf *)  ZALLOC(dest, ds->w_size, sizeof(Pos));    ds->head   = (Posf *)  ZALLOC(dest, ds->hash_size, sizeof(Pos));    overlay = (ushf *) ZALLOC(dest, ds->lit_bufsize, sizeof(ush)+2);    ds->pending_buf = (uchf *) overlay;    if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL ||        ds->pending_buf == Z_NULL) {        deflateEnd (dest);        return Z_MEM_ERROR;    }    /* ??? following zmemcpy doesn't work for 16-bit MSDOS */    zmemcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(Byte));    zmemcpy(ds->prev, ss->prev, ds->w_size * sizeof(Pos));    zmemcpy(ds->head, ss->head, ds->hash_size * sizeof(Pos));    zmemcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size);    ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf);    ds->d_buf = overlay + ds->lit_bufsize/sizeof(ush);    ds->l_buf = ds->pending_buf + (1+sizeof(ush))*ds->lit_bufsize;    ds->l_desc.dyn_tree = ds->dyn_ltree;    ds->d_desc.dyn_tree = ds->dyn_dtree;    ds->bl_desc.dyn_tree = ds->bl_tree;    return Z_OK;}/* =========================================================================== * Return the number of bytes of output which are immediately available * for output from the decompressor. */int deflateOutputPending (strm)    z_streamp strm;{    if (strm == Z_NULL || strm->state == Z_NULL) return 0;        return ((deflate_state *)(strm->state))->pending;}/* =========================================================================== * Read a new buffer from the current input stream, update the adler32 * and total number of bytes read.  All deflate() input goes through * this function so some applications may wish to modify it to avoid * allocating a large strm->next_in buffer and copying from it. * (See also flush_pending()). */local int read_buf(strm, buf, size)    z_streamp strm;    charf *buf;    unsigned size;{    unsigned len = strm->avail_in;    if (len > size) len = size;    if (len == 0) return 0;    strm->avail_in  -= len;    if (!((deflate_state *)(strm->state))->noheader) {        strm->adler = adler32(strm->adler, strm->next_in, len);    }    zmemcpy(buf, strm->next_in, len);    strm->next_in  += len;    strm->total_in += len;    return (int)len;}/* =========================================================================== * Initialize the "longest match" routines for a new zlib stream */local void lm_init (s)    deflate_state *s;{    s->window_size = (ulg)2L*s->w_size;    CLEAR_HASH(s);    /* Set the default configuration parameters:     */    s->max_lazy_match   = configuration_table[s->level].max_lazy;    s->good_match       = configuration_table[s->level].good_length;    s->nice_match       = configuration_table[s->level].nice_length;    s->max_chain_length = configuration_table[s->level].max_chain;    s->strstart = 0;    s->block_start = 0L;    s->lookahead = 0;    s->match_length = s->prev_length = MIN_MATCH-1;    s->match_available = 0;    s->ins_h = 0;#ifdef ASMV    match_init(); /* initialize the asm code */#endif}/* =========================================================================== * Set match_start to the longest match starting at the given string and * return its length. Matches shorter or equal to prev_length are discarded, * in which case the result is equal to prev_length and match_start is * garbage. * IN assertions: cur_match is the head of the hash chain for the current *   string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1 * OUT assertion: the match length is not greater than s->lookahead. */#ifndef ASMV/* For 80x86 and 680x0, an optimized version will be provided in match.asm or * match.S. The code will be functionally equivalent. */local uInt longest_match(s, cur_match)    deflate_state *s;    IPos cur_match;                             /* current match */{    unsigned chain_length = s->max_chain_length;/* max hash chain length */    register Bytef *scan = s->window + s->strstart; /* current string */    register Bytef *match;                       /* matched string */    register int len;                           /* length of current match */    int best_len = s->prev_length;              /* best match length so far */    int nice_match = s->nice_match;             /* stop if match long enough */    IPos limit = s->strstart > (IPos)MAX_DIST(s) ?        s->strstart - (IPos)MAX_DIST(s) : NIL;    /* Stop when cur_match becomes <= limit. To simplify the code,     * we prevent matches with the string of window index 0.     */    Posf *prev = s->prev;    uInt wmask = s->w_mask;#ifdef UNALIGNED_OK    /* Compare two bytes at a time. Note: this is not always beneficial.     * Try with and without -DUNALIGNED_OK to check.     */    register Bytef *strend = s->window + s->strstart + MAX_MATCH - 1;    register ush scan_start = *(ushf*)scan;    register ush scan_end   = *(ushf*)(scan+best_len-1);#else    register Bytef *strend = s->window + s->strstart + MAX_MATCH;    register Byte scan_end1  = scan[best_len-1];    register Byte scan_end   = scan[best_len];#endif    /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.     * It is easy to get rid of this optimization if necessary.     */    Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");    /* Do not waste too much time if we already have a good match: */    if (s->prev_length >= s->good_match) {        chain_length >>= 2;    }    /* Do not look for matches beyond the end of the input. This is necessary     * to make deflate deterministic.     */    if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead;    Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");    do {        Assert(cur_match < s->strstart, "no future");        match = s->window + cur_match;        /* Skip to next match if the match length cannot increase         * or if the match length is less than 2:         */#if (defined(UNALIGNED_OK) && MAX_MATCH == 258)        /* This code assumes sizeof(unsigned short) == 2. Do not use         * UNALIGNED_OK if your compiler uses a different size.         */        if (*(ushf*)(match+best_len-1) != scan_end ||            *(ushf*)match != scan_start) continue;        /* It is not necessary to compare scan[2] and match[2] since they are         * always equal when the other bytes match, given that the hash keys         * are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at         * strstart+3, +5, ... up to strstart+257. We check for insufficient         * lookahead only every 4th comparison; the 128th check will be made         * at strstart+257. If MAX_MATCH-2 is not a multiple of 8, it is         * necessary to put more guard bytes at the end of the window, or         * to check more often for insufficient lookahead.         */        Assert(scan[2] == match[2], "scan[2]?");        scan++, match++;        do {        } while (*(ushf*)(scan+=2) == *(ushf*)(match+=2) &&                 *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&                 *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&                 *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&                 scan < strend);        /* The funny "do {}" generates better code on most compilers */        /* Here, scan <= window+strstart+257 */        Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");        if (*scan == *match) scan++;        len = (MAX_MATCH - 1) - (int)(strend-scan);        scan = strend - (MAX_MATCH-1);#else /* UNALIGNED_OK */        if (match[best_len]   != scan_end  ||            match[best_len-1] != scan_end1 ||            *match            != *scan     ||            *++match          != scan[1])      continue;        /* The check at best_len-1 can be removed because it will be made         * again later. (This heuristic is not always a win.)         * It is not necessary to compare scan[2] and match[2] since they         * are always equal when the other bytes match, given that         * the hash keys are equal and that HASH_BITS >= 8.         */        scan += 2, match++;        Assert(*scan == *match, "match[2]?");        /* We check for insufficient lookahead only every 8th comparison;         * the 256th check will be made at strstart+258.         */        do {        } while (*++scan == *++match && *++scan == *++match &&                 *++scan == *++match && *++scan == *++match &&                 *++scan == *++match && *++scan == *++match &&                 *++scan == *++match && *++scan == *++match &&                 scan < strend);        Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");        len = MAX_MATCH - (int)(strend - scan);        scan = strend - MAX_MATCH;#endif /* UNALIGNED_OK */        if (len > best_len) {            s->match_start = cur_match;            best_len = len;            if (len >= nice_match) break;#ifdef UNALIGNED_OK            scan_end = *(ushf*)(scan+best_len-1);#else            scan_end1  = scan[best_len-1];            scan_end   = scan[best_len];#endif        }    } while ((cur_match = prev[cur_match & wmask]) > limit             && --chain_length != 0);    if ((uInt)best_len <= s->lookahead) return best_len;    return s->lookahead;}#endif /* ASMV */#ifdef DEBUG_ZLIB/* =========================================================================== * Check that the match at match_start is indeed a match. */local void check_match(s, start, match, length)    deflate_state *s;    IPos start, match;    int length;{    /* check that the match is indeed a match */    if (zmemcmp((charf *)s->window + match,                (charf *)s->window + start, length) != EQUAL) {        fprintf(stderr, " start %u, match %u, length %d\n",		start, match, length);        do {	    fprintf(stderr, "%c%c", s->window[match++], s->window[start++]);	} while (--length != 0);        z_error("invalid match");    }    if (z_verbose > 1) {        fprintf(stderr,"\\[%d,%d]", start-match, length);        do { putc(s->window[start++], stderr); } while (--length != 0);    }}#else#  define check_match(s, start, match, length)#endif/* =========================================================================== * Fill the window when the lookahead becomes insufficient. * Updates strstart and lookahead. * * IN assertion: lookahead < MIN_LOOKAHEAD * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD *    At least one byte has been read, or avail_in == 0; reads are *    performed for at least two bytes (required for the zip translate_eol *    option -- not supported here). */local void fill_window(s)    deflate_state *s;{    register unsigned n, m;    register Posf *p;    unsigned more;    /* Amount of free space at the end of the window. */    uInt wsize = s->w_size;    do {        more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart);        /* Deal with !@#$% 64K limit: */        if (more == 0 && s->strstart == 0 && s->lookahead == 0) {            more = wsize;        } else if (more == (unsigned)(-1)) {            /* Very unlikely, but possible on 16 bit machine if strstart == 0             * and lookahead == 1 (input done one byte at time)             */            more--;        /* If the window is almost full and there is insufficient lookahead,         * move the upper half to the lower one to make room in the upper half.         */        } else if (s->strstart >= wsize+MAX_DIST(s)) {            zmemcpy((charf *)s->window, (charf *)s->window+wsize,                   (unsigned)wsize);            s->match_start -= wsize;            s->strstart    -= wsize; /* we now have strstart >= MAX_DIST */            s->block_start -= (long) wsize;            /* Slide the hash table (could be avoided with 32 bit values               at the expense of memory usage). We slide even when level == 0               to keep the hash table consistent if we switch back to level > 0               later. (Using level 0 permanently is not an optimal usage of               zlib, so we don't care about this pathological case.)             */            n = s->hash_size;            p = &s->head[n];            do {                m = *--p;                *p = (Pos)(m >= wsize ? m-wsize : NIL);            } while (--n);            n = wsize;            p = &s->prev[n];            do {                m = *--p;                *p = (Pos)(m >= wsize ? m-wsize : NIL);                /* If n is not on any hash chain, prev[n] is garbage but                 * its value will never be used.                 */            } while (--n);            more += wsize;        }        if (s->strm->avail_in == 0) return;        /* If there was no sliding:         *    strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&         *    more == window_size - lookahead - strstart         * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)         * => more >= window_size - 2*WSIZE + 2         * In the BIG_MEM or MMAP case (not yet supported),

⌨️ 快捷键说明

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