📄 zlib.c
字号:
/* * This file is derived from various .h and .c files from the zlib-0.95 * distribution by Jean-loup Gailly and Mark Adler, with some additions * by Paul Mackerras to aid in implementing Deflate compression and * decompression for PPP packets. See zlib.h for conditions of * distribution and use. * * Changes that have been made include: * - changed functions not used outside this file to "local" * - added minCompression parameter to deflateInit2 * - added Z_PACKET_FLUSH (see zlib.h for details) * - added inflateIncomp * * $Id: zlib.c,v 1.2 1999/04/01 07:26:30 paulus Exp $ *//*+++++*//* zutil.h -- internal interface and configuration of the compression library * Copyright (C) 1995 Jean-loup Gailly. * For conditions of distribution and use, see copyright notice in zlib.h *//* WARNING: this file should *not* be used by applications. It is part of the implementation of the compression library and is subject to change. Applications should only use zlib.h. *//* From: zutil.h,v 1.9 1995/05/03 17:27:12 jloup Exp */#define _Z_UTIL_H#include "zlib.h"#ifdef STDC# include <string.h>#endif#ifndef local# define local static#endif/* compile with -Dlocal if your debugger can't find static symbols */#define FARtypedef unsigned char uch;typedef uch FAR uchf;typedef unsigned short ush;typedef ush FAR ushf;typedef unsigned long ulg;extern char *z_errmsg[]; /* indexed by 1-zlib_error */#define ERR_RETURN(strm,err) return (strm->msg=z_errmsg[1-err], err)/* To be used only when the state is known to be valid */#ifndef NULL#define NULL ((void *) 0)#endif /* common constants */#define DEFLATED 8#ifndef DEF_WBITS# define DEF_WBITS MAX_WBITS#endif/* default windowBits for decompression. MAX_WBITS is for compression only */#if MAX_MEM_LEVEL >= 8# define DEF_MEM_LEVEL 8#else# define DEF_MEM_LEVEL MAX_MEM_LEVEL#endif/* default memLevel */#define STORED_BLOCK 0#define STATIC_TREES 1#define DYN_TREES 2/* The three kinds of block type */#define MIN_MATCH 3#define MAX_MATCH 258/* The minimum and maximum match lengths */ /* functions */#if defined(STDC) && !defined(HAVE_MEMCPY) && !defined(NO_MEMCPY)# define HAVE_MEMCPY#endif#ifdef HAVE_MEMCPY# define zmemcpy memcpy# define zmemzero(dest, len) memset(dest, 0, len)#else# define zmemcpy(d, s, n) bcopy((s), (d), (n))# define zmemzero bzero#endif/* Diagnostic functions */#ifdef DEBUG_ZLIB# include <stdio.h># ifndef verbose# define verbose 0# endif# define Assert(cond,msg) {if(!(cond)) z_error(msg);}# define Trace(x) fprintf x# define Tracev(x) {if (verbose) fprintf x ;}# define Tracevv(x) {if (verbose>1) fprintf x ;}# define Tracec(c,x) {if (verbose && (c)) fprintf x ;}# define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}#else# define Assert(cond,msg)# define Trace(x)# define Tracev(x)# define Tracevv(x)# define Tracec(c,x)# define Tracecv(c,x)#endiftypedef uLong (*check_func) OF((uLong check, Bytef *buf, uInt len));/* voidpf zcalloc OF((voidpf opaque, unsigned items, unsigned size)); *//* void zcfree OF((voidpf opaque, voidpf ptr)); */#define ZALLOC(strm, items, size) \ (*((strm)->zalloc))((strm)->opaque, (items), (size))#define ZFREE(strm, addr, size) \ (*((strm)->zfree))((strm)->opaque, (voidpf)(addr), (size))#define TRY_FREE(s, p, n) {if (p) ZFREE(s, p, n);}/* deflate.h -- internal compression state * Copyright (C) 1995 Jean-loup Gailly * For conditions of distribution and use, see copyright notice in zlib.h *//* WARNING: this file should *not* be used by applications. It is part of the implementation of the compression library and is subject to change. Applications should only use zlib.h. *//*+++++*//* From: deflate.h,v 1.5 1995/05/03 17:27:09 jloup Exp *//* =========================================================================== * Internal compression state. *//* Data type */#define BINARY 0#define ASCII 1#define UNKNOWN 2#define LENGTH_CODES 29/* number of length codes, not counting the special END_BLOCK code */#define LITERALS 256/* number of literal bytes 0..255 */#define L_CODES (LITERALS+1+LENGTH_CODES)/* number of Literal or Length codes, including the END_BLOCK code */#define D_CODES 30/* number of distance codes */#define BL_CODES 19/* number of codes used to transfer the bit lengths */#define HEAP_SIZE (2*L_CODES+1)/* maximum heap size */#define MAX_BITS 15/* All codes must not exceed MAX_BITS bits */#define INIT_STATE 42#define BUSY_STATE 113#define FLUSH_STATE 124#define FINISH_STATE 666/* Stream status *//* Data structure describing a single value and its code string. */typedef struct ct_data_s { union { ush freq; /* frequency count */ ush code; /* bit string */ } fc; union { ush dad; /* father node in Huffman tree */ ush len; /* length of bit string */ } dl;} FAR ct_data;#define Freq fc.freq#define Code fc.code#define Dad dl.dad#define Len dl.lentypedef struct static_tree_desc_s static_tree_desc;typedef struct tree_desc_s { ct_data *dyn_tree; /* the dynamic tree */ int max_code; /* largest code with non zero frequency */ static_tree_desc *stat_desc; /* the corresponding static tree */} FAR tree_desc;typedef ush Pos;typedef Pos FAR Posf;typedef unsigned IPos;/* A Pos is an index in the character window. We use short instead of int to * save space in the various tables. IPos is used only for parameter passing. */typedef struct deflate_state { z_stream *strm; /* pointer back to this zlib stream */ int status; /* as the name implies */ Bytef *pending_buf; /* output still pending */ Bytef *pending_out; /* next pending byte to output to the stream */ int pending; /* nb of bytes in the pending buffer */ uLong adler; /* adler32 of uncompressed data */ int noheader; /* suppress zlib header and adler32 */ Byte data_type; /* UNKNOWN, BINARY or ASCII */ Byte method; /* STORED (for zip only) or DEFLATED */ int minCompr; /* min size decrease for Z_FLUSH_NOSTORE */ /* used by deflate.c: */ uInt w_size; /* LZ77 window size (32K by default) */ uInt w_bits; /* log2(w_size) (8..16) */ uInt w_mask; /* w_size - 1 */ Bytef *window; /* Sliding window. Input bytes are read into the second half of the window, * and move to the first half later to keep a dictionary of at least wSize * bytes. With this organization, matches are limited to a distance of * wSize-MAX_MATCH bytes, but this ensures that IO is always * performed with a length multiple of the block size. Also, it limits * the window size to 64K, which is quite useful on MSDOS. * To do: use the user input buffer as sliding window. */ ulg window_size; /* Actual size of window: 2*wSize, except when the user input buffer * is directly used as sliding window. */ Posf *prev; /* Link to older string with same hash index. To limit the size of this * array to 64K, this link is maintained only for the last 32K strings. * An index in this array is thus a window index modulo 32K. */ 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 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -