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

📄 zlib.c

📁 经典的ppp程序
💻 C
📖 第 1 页 / 共 5 页
字号:
#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.     */    uInt blocks_in_packet;    /* Number of blocks produced since the last time Z_PACKET_FLUSH     * was used.     */} 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 */local void ct_init       OF((deflate_state *s));local int  ct_tally      OF((deflate_state *s, int dist, int lc));local ulg ct_flush_block OF((deflate_state *s, charf *buf, ulg stored_len,			     int flush));local void ct_align      OF((deflate_state *s));local void ct_stored_block OF((deflate_state *s, charf *buf, ulg stored_len,                          int eof));local void ct_stored_type_only OF((deflate_state *s));/*+++++*//* deflate.c -- compress data using the deflation algorithm * Copyright (C) 1995 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.uu.net:/pub/archiving/zip/doc/deflate-1.1.doc * *      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.8 1995/05/03 17:27:08 jloup Exp */local char zlib_copyright[] = " deflate Copyright 1995 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. */#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;} config;local config configuration_table[10] = {/*      good lazy nice chain *//* 0 */ {0,    0,  0,    0},  /* store only *//* 1 */ {4,    4,  8,    4},  /* maximum speed, no lazy matches *//* 2 */ {4,    5, 16,    8},/* 3 */ {4,    6, 32,   32},/* 4 */ {4,    4, 16,   16},  /* lazy matches *//* 5 */ {8,   16, 32,   32},/* 6 */ {8,   16, 128, 128},/* 7 */ {8,   32, 128, 256},/* 8 */ {32, 128, 258, 1024},/* 9 */ {32, 258, 258, 4096}}; /* 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 *//* =========================================================================== *  Prototypes for local functions. */local void fill_window   OF((deflate_state *s));local int  deflate_fast  OF((deflate_state *s, int flush));local int  deflate_slow  OF((deflate_state *s, int flush));local void lm_init       OF((deflate_state *s));local int longest_match  OF((deflate_state *s, IPos cur_match));local void putShortMSB   OF((deflate_state *s, uInt b));local void flush_pending OF((z_stream *strm));local int read_buf       OF((z_stream *strm, charf *buf, unsigned size));#ifdef ASMV      void match_init OF((void)); /* asm code initialization */#endif#ifdef DEBUG_ZLIBlocal  void check_match OF((deflate_state *s, IPos start, IPos match,                            int length));#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] = (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)    z_stream *strm;    int level;{    return deflateInit2 (strm, level, DEFLATED, MAX_WBITS, DEF_MEM_LEVEL,			 0, 0);    /* To do: ignore strm->next_in if we use it as window */}/* ========================================================================= */int deflateInit2 (strm, level, method, windowBits, memLevel,		  strategy, minCompression)    z_stream *strm;    int  level;    int  method;    int  windowBits;    int  memLevel;    int  strategy;    int  minCompression;{    deflate_state *s;    int noheader = 0;    if (strm == Z_NULL) return Z_STREAM_ERROR;    strm->msg = Z_NULL;/*    if (strm->zalloc == Z_NULL) strm->zalloc = zcalloc; *//*    if (strm->zfree == Z_NULL) strm->zfree = zcfree; */    if (level == Z_DEFAULT_COMPRESSION) level = 6;    if (windowBits < 0) { /* undocumented feature: suppress zlib header */        noheader = 1;        windowBits = -windowBits;    }    if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != DEFLATED ||        windowBits < 8 || windowBits > 15 || level < 1 || level > 9) {        return Z_STREAM_ERROR;    }    s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state));    if (s == Z_NULL) return Z_MEM_ERROR;    strm->state = (struct internal_state FAR *)s;    s->strm = strm;    s->noheader = noheader;    s->w_bits = windowBits;    s->w_size = 1 << s->w_bits;    s->w_mask = s->w_size - 1;    s->hash_bits = memLevel + 7;    s->hash_size = 1 << s->hash_bits;    s->hash_mask = s->hash_size - 1;    s->hash_shift =  ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH);    s->window = (Bytef *) ZALLOC(strm, s->w_size, 2*sizeof(Byte));    s->prev   = (Posf *)  ZALLOC(strm, s->w_size, sizeof(Pos));    s->head   = (Posf *)  ZALLOC(strm, s->hash_size, sizeof(Pos));    s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */    s->pending_buf = (uchf *) ZALLOC(strm, s->lit_bufsize, 2*sizeof(ush));    if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||        s->pending_buf == Z_NULL) {        strm->msg = z_errmsg[1-Z_MEM_ERROR];        deflateEnd (strm);        return Z_MEM_ERROR;    }    s->d_buf = (ushf *) &(s->pending_buf[s->lit_bufsize]);    s->l_buf = (uchf *) &(s->pending_buf[3*s->lit_bufsize]);    /* We overlay pending_buf and d_buf+l_buf. This works since the average     * output size for (length,distance) codes is <= 32 bits (worst case     * is 15+15+13=33).     */    s->level = level;    s->strategy = strategy;    s->method = (Byte)method;    s->minCompr = minCompression;    s->blocks_in_packet = 0;    return deflateReset(strm);}/* ========================================================================= */int deflateReset (strm)    z_stream *strm;{    deflate_state *s;        if (strm == Z_NULL || strm->state == Z_NULL ||        strm->zalloc == Z_NULL || strm->zfree == Z_NULL) return Z_STREAM_ERROR;    strm->total_in = strm->total_out = 0;    strm->msg = Z_NULL; /* use zfree if we ever allocate msg dynamically */    strm->data_type = Z_UNKNOWN;    s = (deflate_state *)strm->state;    s->pending = 0;    s->pending_out = s->pending_buf;    if (s->noheader < 0) {        s->noheader = 0; /* was set to -1 by deflate(..., Z_FINISH); */    }    s->status = s->noheader ? BUSY_STATE : INIT_STATE;    s->adler = 1;    ct_init(s);    lm_init(s);    return Z_OK;}/* ========================================================================= * Put a short in the pending buffer. The 16-bit value is put in MSB order. * IN assertion: the stream state is correct and there is enough room in * pending_buf. */local void putShortMSB (s, b)    deflate_state *s;    uInt b;{    put_byte(s, (Byte)(b >> 8));    put_byte(s, (Byte)(b & 0xff));}   /* ========================================================================= * Flush as much pending output as possible. */local void flush_pending(strm)    z_stream *strm;{    deflate_state *state = (deflate_state *) strm->state;    unsigned len = state->pending;    if (len > strm->avail_out) len = strm->avail_out;    if (len == 0) return;    if (strm->next_out != NULL) {	zmemcpy(strm->next_out, state->pending_out, len);	strm->next_out += len;    }    state->pending_out += len;    strm->total_out += len;    strm->avail_out -= len;    state->pending -= len;    if (state->pending == 0) {        state->pending_out = state->pending_buf;    }}/* ========================================================================= */int deflate (strm, flush)    z_stream *strm;    int flush;{    deflate_state *state = (deflate_state *) strm->state;    if (strm == Z_NULL || state == Z_NULL) return Z_STREAM_ERROR;    

⌨️ 快捷键说明

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