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

📄 zlib.c

📁 这是著名的jffs2嵌入式日志文件系统的源代码
💻 C
📖 第 1 页 / 共 5 页
字号:
    Posf *head; /* Heads of the hash chains or NIL. */    uInt  ins_h;          /* hash index of string to be inserted */    uInt  hash_size;      /* number of elements in hash table */    uInt  hash_bits;      /* log2(hash_size) */    uInt  hash_mask;      /* hash_size-1 */    uInt  hash_shift;    /* Number of bits by which ins_h must be shifted at each input     * step. It must be such that after MIN_MATCH steps, the oldest     * byte no longer takes part in the hash key, that is:     *   hash_shift * MIN_MATCH >= hash_bits     */    long block_start;    /* Window position at the beginning of the current output block. Gets     * negative when the window is moved backwards.     */    uInt match_length;           /* length of best match */    IPos prev_match;             /* previous match */    int match_available;         /* set if previous match exists */    uInt strstart;               /* start of string to insert */    uInt match_start;            /* start of matching string */    uInt lookahead;              /* number of valid bytes ahead in window */    uInt prev_length;    /* Length of the best match at previous step. Matches not greater than this     * are discarded. This is used in the lazy match evaluation.     */    uInt max_chain_length;    /* To speed up deflation, hash chains are never searched beyond this     * length.  A higher limit improves compression ratio but degrades the     * speed.     */    uInt max_lazy_match;    /* Attempt to find a better match only when the current match is strictly     * smaller than this value. This mechanism is used only for compression     * levels >= 4.     */#   define max_insert_length  max_lazy_match    /* Insert new strings in the hash table only if the match length is not     * greater than this length. This saves time but degrades compression.     * max_insert_length is used only for compression levels <= 3.     */    int level;    /* compression level (1..9) */    int strategy; /* favor or force Huffman coding*/    uInt good_match;    /* Use a faster search when the previous match is longer than this */    int nice_match; /* Stop searching when current match exceeds this */                /* used by trees.c: */    /* Didn't use ct_data typedef below to supress compiler warning */    struct ct_data_s dyn_ltree[HEAP_SIZE];   /* literal and length tree */    struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */    struct ct_data_s bl_tree[2*BL_CODES+1];  /* Huffman tree for bit lengths */    struct tree_desc_s l_desc;               /* desc. for literal tree */    struct tree_desc_s d_desc;               /* desc. for distance tree */    struct tree_desc_s bl_desc;              /* desc. for bit length tree */    ush bl_count[MAX_BITS+1];    /* number of codes at each bit length for an optimal tree */    int heap[2*L_CODES+1];      /* heap used to build the Huffman trees */    int heap_len;               /* number of elements in the heap */    int heap_max;               /* element of largest frequency */    /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.     * The same heap array is used to build all trees.     */    uch depth[2*L_CODES+1];    /* Depth of each subtree used as tie breaker for trees of equal frequency     */    uchf *l_buf;          /* buffer for literals or lengths */    uInt  lit_bufsize;    /* Size of match buffer for literals/lengths.  There are 4 reasons for     * limiting lit_bufsize to 64K:     *   - frequencies can be kept in 16 bit counters     *   - if compression is not successful for the first block, all input     *     data is still in the window so we can still emit a stored block even     *     when input comes from standard input.  (This can also be done for     *     all blocks if lit_bufsize is not greater than 32K.)     *   - if compression is not successful for a file smaller than 64K, we can     *     even emit a stored file instead of a stored block (saving 5 bytes).     *     This is applicable only for zip (not gzip or zlib).     *   - creating new Huffman trees less frequently may not provide fast     *     adaptation to changes in the input data statistics. (Take for     *     example a binary file with poorly compressible code followed by     *     a highly compressible string table.) Smaller buffer sizes give     *     fast adaptation but have of course the overhead of transmitting     *     trees more frequently.     *   - I can't count above 4     */    uInt last_lit;      /* running index in l_buf */    ushf *d_buf;    /* Buffer for distances. To simplify the code, d_buf and l_buf have     * the same number of elements. To use different lengths, an extra flag     * array would be necessary.     */    ulg opt_len;        /* bit length of current block with optimal trees */    ulg static_len;     /* bit length of current block with static trees */    ulg compressed_len; /* total bit length of compressed file */    uInt matches;       /* number of string matches in current block */    int last_eob_len;   /* bit length of EOB code for last block */#ifdef DEBUG_ZLIB    ulg bits_sent;      /* bit length of the compressed data */#endif    ush bi_buf;    /* Output buffer. bits are inserted starting at the bottom (least     * significant bits).     */    int bi_valid;    /* Number of valid bits in bi_buf.  All bits above the last valid bit     * are always zero.     */} FAR deflate_state;/* Output a byte on the stream. * IN assertion: there is enough room in pending_buf. */#define put_byte(s, c) {s->pending_buf[s->pending++] = (c);}#define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)/* Minimum amount of lookahead, except at the end of the input file. * See deflate.c for comments about the MIN_MATCH+1. */#define MAX_DIST(s)  ((s)->w_size-MIN_LOOKAHEAD)/* In order to simplify the code, particularly on 16 bit machines, match * distances are limited to MAX_DIST instead of WSIZE. */        /* in trees.c */void _tr_init         OF((deflate_state *s));int  _tr_tally        OF((deflate_state *s, unsigned dist, unsigned lc));ulg  _tr_flush_block  OF((deflate_state *s, charf *buf, ulg stored_len,			  int eof));void _tr_align        OF((deflate_state *s));void _tr_stored_block OF((deflate_state *s, charf *buf, ulg stored_len,                          int eof));void _tr_stored_type_only OF((deflate_state *));#endif/* --- deflate.h *//* +++ deflate.c *//* deflate.c -- compress data using the deflation algorithm * Copyright (C) 1995-1996 Jean-loup Gailly. * For conditions of distribution and use, see copyright notice in zlib.h  *//* *  ALGORITHM * *      The "deflation" process depends on being able to identify portions *      of the input text which are identical to earlier input (within a *      sliding window trailing behind the input currently being processed). * *      The most straightforward technique turns out to be the fastest for *      most input files: try all possible matches and select the longest. *      The key feature of this algorithm is that insertions into the string *      dictionary are very simple and thus fast, and deletions are avoided *      completely. Insertions are performed at each input character, whereas *      string matches are performed only when the previous match ends. So it *      is preferable to spend more time in matches to allow very fast string *      insertions and avoid deletions. The matching algorithm for small *      strings is inspired from that of Rabin & Karp. A brute force approach *      is used to find longer strings when a small match has been found. *      A similar algorithm is used in comic (by Jan-Mark Wams) and freeze *      (by Leonid Broukhis). *         A previous version of this file used a more sophisticated algorithm *      (by Fiala and Greene) which is guaranteed to run in linear amortized *      time, but has a larger average cost, uses more memory and is patented. *      However the F&G algorithm may be faster for some highly redundant *      files if the parameter max_chain_length (described below) is too large. * *  ACKNOWLEDGEMENTS * *      The idea of lazy evaluation of matches is due to Jan-Mark Wams, and *      I found it in 'freeze' written by Leonid Broukhis. *      Thanks to many people for bug reports and testing. * *  REFERENCES * *      Deutsch, L.P.,"DEFLATE Compressed Data Format Specification". *      Available in ftp://ds.internic.net/rfc/rfc1951.txt * *      A description of the Rabin and Karp algorithm is given in the book *         "Algorithms" by R. Sedgewick, Addison-Wesley, p252. * *      Fiala,E.R., and Greene,D.H. *         Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595 * *//* From: deflate.c,v 1.15 1996/07/24 13:40:58 me Exp $ *//* #include "deflate.h" */char deflate_copyright[] = " deflate 1.0.4 Copyright 1995-1996 Jean-loup Gailly ";/*  If you use the zlib library in a product, an acknowledgment is welcome  in the documentation of your product. If for some reason you cannot  include such an acknowledgment, I would appreciate that you keep this  copyright string in the executable of your product. *//* =========================================================================== *  Function prototypes. */typedef enum {    need_more,      /* block not completed, need more input or more output */    block_done,     /* block flush performed */    finish_started, /* finish started, need only more output at next deflate */    finish_done     /* finish done, accept no more input or output */} block_state;typedef block_state (*compress_func) OF((deflate_state *s, int flush));/* Compression function. Returns the block state after the call. */local void fill_window    OF((deflate_state *s));local block_state deflate_stored OF((deflate_state *s, int flush));local block_state deflate_fast   OF((deflate_state *s, int flush));local block_state deflate_slow   OF((deflate_state *s, int flush));local void lm_init        OF((deflate_state *s));local void putShortMSB    OF((deflate_state *s, uInt b));local void flush_pending  OF((z_streamp strm));local int read_buf        OF((z_streamp strm, charf *buf, unsigned size));#ifdef ASMV      void match_init OF((void)); /* asm code initialization */      uInt longest_match  OF((deflate_state *s, IPos cur_match));#elselocal uInt longest_match  OF((deflate_state *s, IPos cur_match));#endif#ifdef DEBUG_ZLIBlocal  void check_match OF((deflate_state *s, IPos start, IPos match,                            int length));#endif/* =========================================================================== * Local data */#define NIL 0/* Tail of hash chains */#ifndef TOO_FAR#  define TOO_FAR 4096#endif/* Matches of length 3 are discarded if their distance exceeds TOO_FAR */#define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)/* Minimum amount of lookahead, except at the end of the input file. * See deflate.c for comments about the MIN_MATCH+1. *//* Values for max_lazy_match, good_match and max_chain_length, depending on * the desired pack level (0..9). The values given below have been tuned to * exclude worst case performance for pathological files. Better values may be * found for specific files. */typedef struct config_s {   ush good_length; /* reduce lazy search above this match length */   ush max_lazy;    /* do not perform lazy search above this match length */   ush nice_length; /* quit search above this match length */   ush max_chain;   compress_func func;} config;local config configuration_table[10] = {/*      good lazy nice chain *//* 0 */ {0,    0,  0,    0, deflate_stored},  /* store only *//* 1 */ {4,    4,  8,    4, deflate_fast}, /* maximum speed, no lazy matches *//* 2 */ {4,    5, 16,    8, deflate_fast},/* 3 */ {4,    6, 32,   32, deflate_fast},/* 4 */ {4,    4, 16,   16, deflate_slow},  /* lazy matches *//* 5 */ {8,   16, 32,   32, deflate_slow},/* 6 */ {8,   16, 128, 128, deflate_slow},/* 7 */ {8,   32, 128, 256, deflate_slow},/* 8 */ {32, 128, 258, 1024, deflate_slow},/* 9 */ {32, 258, 258, 4096, deflate_slow}}; /* maximum compression *//* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4 * For deflate_fast() (levels <= 3) good is ignored and lazy has a different * meaning. */#define EQUAL 0/* result of memcmp for equal strings */#ifndef NO_DUMMY_DECLstruct static_tree_desc_s {int dummy;}; /* for buggy compilers */#endif/* =========================================================================== * Update a hash value with the given input byte * IN  assertion: all calls to to UPDATE_HASH are made with consecutive *    input characters, so that a running hash key can be computed from the *    previous key instead of complete recalculation each time. */#define UPDATE_HASH(s,h,c) (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask)/* =========================================================================== * Insert string str in the dictionary and set match_head to the previous head * of the hash chain (the most recent string with same hash key). Return * the previous length of the hash chain. * IN  assertion: all calls to to INSERT_STRING are made with consecutive *    input characters and the first MIN_MATCH bytes of str are valid *    (except for the last MIN_MATCH-1 bytes of the input file). */#define INSERT_STRING(s, str, match_head) \   (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \    s->prev[(str) & s->w_mask] = match_head = s->head[s->ins_h], \    s->head[s->ins_h] = (Pos)(str))/* =========================================================================== * Initialize the hash table (avoiding 64K overflow for 16 bit systems). * prev[] will be initialized on the fly. */#define CLEAR_HASH(s) \    s->head[s->hash_size-1] = NIL; \    zmemzero((charf *)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head));/* ========================================================================= */int deflateInit_(strm, level, version, stream_size)    z_streamp 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);    /* To do: ignore strm->next_in if we use it as window */}/* ========================================================================= */int deflateInit2_(strm, level, method, windowBits, memLevel, strategy,		  version, stream_size)    z_streamp strm;    int  level;    int  method;    int  windowBits;    int  memLevel;    int  strategy;    const char *version;    int stream_size;{    deflate_state *s;    int noheader = 0;    static char* my_version = ZLIB_VERSION;    ushf *overlay;    /* We overlay pending_buf and d_buf+l_buf. This works since the average     * output size for (length,distance) codes is <= 24 bits.     */    if (version == Z_NULL || version[0] != my_version[0] ||        stream_size != sizeof(z_stream)) {

⌨️ 快捷键说明

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