![]() |
|
|||
File indexing completed on 2025-05-11 08:24:10
0001 /* deflate.h -- internal compression state 0002 * Copyright (C) 1995-2018 Jean-loup Gailly 0003 * For conditions of distribution and use, see copyright notice in zlib.h 0004 */ 0005 0006 /* WARNING: this file should *not* be used by applications. It is 0007 part of the implementation of the compression library and is 0008 subject to change. Applications should only use zlib.h. 0009 */ 0010 0011 /* @(#) $Id$ */ 0012 0013 #ifndef DEFLATE_H 0014 #define DEFLATE_H 0015 0016 #include "zutil.h" 0017 0018 /* define NO_GZIP when compiling if you want to disable gzip header and 0019 trailer creation by deflate(). NO_GZIP would be used to avoid linking in 0020 the crc code when it is not needed. For shared libraries, gzip encoding 0021 should be left enabled. */ 0022 #ifndef NO_GZIP 0023 # define GZIP 0024 #endif 0025 0026 /* =========================================================================== 0027 * Internal compression state. 0028 */ 0029 0030 #define LENGTH_CODES 29 0031 /* number of length codes, not counting the special END_BLOCK code */ 0032 0033 #define LITERALS 256 0034 /* number of literal bytes 0..255 */ 0035 0036 #define L_CODES (LITERALS+1+LENGTH_CODES) 0037 /* number of Literal or Length codes, including the END_BLOCK code */ 0038 0039 #define D_CODES 30 0040 /* number of distance codes */ 0041 0042 #define BL_CODES 19 0043 /* number of codes used to transfer the bit lengths */ 0044 0045 #define HEAP_SIZE (2*L_CODES+1) 0046 /* maximum heap size */ 0047 0048 #define MAX_BITS 15 0049 /* All codes must not exceed MAX_BITS bits */ 0050 0051 #define Buf_size 16 0052 /* size of bit buffer in bi_buf */ 0053 0054 #define INIT_STATE 42 /* zlib header -> BUSY_STATE */ 0055 #ifdef GZIP 0056 # define GZIP_STATE 57 /* gzip header -> BUSY_STATE | EXTRA_STATE */ 0057 #endif 0058 #define EXTRA_STATE 69 /* gzip extra block -> NAME_STATE */ 0059 #define NAME_STATE 73 /* gzip file name -> COMMENT_STATE */ 0060 #define COMMENT_STATE 91 /* gzip comment -> HCRC_STATE */ 0061 #define HCRC_STATE 103 /* gzip header CRC -> BUSY_STATE */ 0062 #define BUSY_STATE 113 /* deflate -> FINISH_STATE */ 0063 #define FINISH_STATE 666 /* stream complete */ 0064 /* Stream status */ 0065 0066 0067 /* Data structure describing a single value and its code string. */ 0068 typedef struct ct_data_s { 0069 union { 0070 ush freq; /* frequency count */ 0071 ush code; /* bit string */ 0072 } fc; 0073 union { 0074 ush dad; /* father node in Huffman tree */ 0075 ush len; /* length of bit string */ 0076 } dl; 0077 } FAR ct_data; 0078 0079 #define Freq fc.freq 0080 #define Code fc.code 0081 #define Dad dl.dad 0082 #define Len dl.len 0083 0084 typedef struct static_tree_desc_s static_tree_desc; 0085 0086 typedef struct tree_desc_s { 0087 ct_data *dyn_tree; /* the dynamic tree */ 0088 int max_code; /* largest code with non zero frequency */ 0089 const static_tree_desc *stat_desc; /* the corresponding static tree */ 0090 } FAR tree_desc; 0091 0092 typedef ush Pos; 0093 typedef Pos FAR Posf; 0094 typedef unsigned IPos; 0095 0096 /* A Pos is an index in the character window. We use short instead of int to 0097 * save space in the various tables. IPos is used only for parameter passing. 0098 */ 0099 0100 typedef struct internal_state { 0101 z_streamp strm; /* pointer back to this zlib stream */ 0102 int status; /* as the name implies */ 0103 Bytef *pending_buf; /* output still pending */ 0104 ulg pending_buf_size; /* size of pending_buf */ 0105 Bytef *pending_out; /* next pending byte to output to the stream */ 0106 ulg pending; /* nb of bytes in the pending buffer */ 0107 int wrap; /* bit 0 true for zlib, bit 1 true for gzip */ 0108 gz_headerp gzhead; /* gzip header information to write */ 0109 ulg gzindex; /* where in extra, name, or comment */ 0110 Byte method; /* can only be DEFLATED */ 0111 int last_flush; /* value of flush param for previous deflate call */ 0112 0113 /* used by deflate.c: */ 0114 0115 uInt w_size; /* LZ77 window size (32K by default) */ 0116 uInt w_bits; /* log2(w_size) (8..16) */ 0117 uInt w_mask; /* w_size - 1 */ 0118 0119 Bytef *window; 0120 /* Sliding window. Input bytes are read into the second half of the window, 0121 * and move to the first half later to keep a dictionary of at least wSize 0122 * bytes. With this organization, matches are limited to a distance of 0123 * wSize-MAX_MATCH bytes, but this ensures that IO is always 0124 * performed with a length multiple of the block size. Also, it limits 0125 * the window size to 64K, which is quite useful on MSDOS. 0126 * To do: use the user input buffer as sliding window. 0127 */ 0128 0129 ulg window_size; 0130 /* Actual size of window: 2*wSize, except when the user input buffer 0131 * is directly used as sliding window. 0132 */ 0133 0134 Posf *prev; 0135 /* Link to older string with same hash index. To limit the size of this 0136 * array to 64K, this link is maintained only for the last 32K strings. 0137 * An index in this array is thus a window index modulo 32K. 0138 */ 0139 0140 Posf *head; /* Heads of the hash chains or NIL. */ 0141 0142 uInt ins_h; /* hash index of string to be inserted */ 0143 uInt hash_size; /* number of elements in hash table */ 0144 uInt hash_bits; /* log2(hash_size) */ 0145 uInt hash_mask; /* hash_size-1 */ 0146 0147 uInt hash_shift; 0148 /* Number of bits by which ins_h must be shifted at each input 0149 * step. It must be such that after MIN_MATCH steps, the oldest 0150 * byte no longer takes part in the hash key, that is: 0151 * hash_shift * MIN_MATCH >= hash_bits 0152 */ 0153 0154 long block_start; 0155 /* Window position at the beginning of the current output block. Gets 0156 * negative when the window is moved backwards. 0157 */ 0158 0159 uInt match_length; /* length of best match */ 0160 IPos prev_match; /* previous match */ 0161 int match_available; /* set if previous match exists */ 0162 uInt strstart; /* start of string to insert */ 0163 uInt match_start; /* start of matching string */ 0164 uInt lookahead; /* number of valid bytes ahead in window */ 0165 0166 uInt prev_length; 0167 /* Length of the best match at previous step. Matches not greater than this 0168 * are discarded. This is used in the lazy match evaluation. 0169 */ 0170 0171 uInt max_chain_length; 0172 /* To speed up deflation, hash chains are never searched beyond this 0173 * length. A higher limit improves compression ratio but degrades the 0174 * speed. 0175 */ 0176 0177 uInt max_lazy_match; 0178 /* Attempt to find a better match only when the current match is strictly 0179 * smaller than this value. This mechanism is used only for compression 0180 * levels >= 4. 0181 */ 0182 # define max_insert_length max_lazy_match 0183 /* Insert new strings in the hash table only if the match length is not 0184 * greater than this length. This saves time but degrades compression. 0185 * max_insert_length is used only for compression levels <= 3. 0186 */ 0187 0188 int level; /* compression level (1..9) */ 0189 int strategy; /* favor or force Huffman coding*/ 0190 0191 uInt good_match; 0192 /* Use a faster search when the previous match is longer than this */ 0193 0194 int nice_match; /* Stop searching when current match exceeds this */ 0195 0196 /* used by trees.c: */ 0197 /* Didn't use ct_data typedef below to suppress compiler warning */ 0198 struct ct_data_s dyn_ltree[HEAP_SIZE]; /* literal and length tree */ 0199 struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */ 0200 struct ct_data_s bl_tree[2*BL_CODES+1]; /* Huffman tree for bit lengths */ 0201 0202 struct tree_desc_s l_desc; /* desc. for literal tree */ 0203 struct tree_desc_s d_desc; /* desc. for distance tree */ 0204 struct tree_desc_s bl_desc; /* desc. for bit length tree */ 0205 0206 ush bl_count[MAX_BITS+1]; 0207 /* number of codes at each bit length for an optimal tree */ 0208 0209 int heap[2*L_CODES+1]; /* heap used to build the Huffman trees */ 0210 int heap_len; /* number of elements in the heap */ 0211 int heap_max; /* element of largest frequency */ 0212 /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used. 0213 * The same heap array is used to build all trees. 0214 */ 0215 0216 uch depth[2*L_CODES+1]; 0217 /* Depth of each subtree used as tie breaker for trees of equal frequency 0218 */ 0219 0220 uchf *sym_buf; /* buffer for distances and literals/lengths */ 0221 0222 uInt lit_bufsize; 0223 /* Size of match buffer for literals/lengths. There are 4 reasons for 0224 * limiting lit_bufsize to 64K: 0225 * - frequencies can be kept in 16 bit counters 0226 * - if compression is not successful for the first block, all input 0227 * data is still in the window so we can still emit a stored block even 0228 * when input comes from standard input. (This can also be done for 0229 * all blocks if lit_bufsize is not greater than 32K.) 0230 * - if compression is not successful for a file smaller than 64K, we can 0231 * even emit a stored file instead of a stored block (saving 5 bytes). 0232 * This is applicable only for zip (not gzip or zlib). 0233 * - creating new Huffman trees less frequently may not provide fast 0234 * adaptation to changes in the input data statistics. (Take for 0235 * example a binary file with poorly compressible code followed by 0236 * a highly compressible string table.) Smaller buffer sizes give 0237 * fast adaptation but have of course the overhead of transmitting 0238 * trees more frequently. 0239 * - I can't count above 4 0240 */ 0241 0242 uInt sym_next; /* running index in sym_buf */ 0243 uInt sym_end; /* symbol table full when sym_next reaches this */ 0244 0245 ulg opt_len; /* bit length of current block with optimal trees */ 0246 ulg static_len; /* bit length of current block with static trees */ 0247 uInt matches; /* number of string matches in current block */ 0248 uInt insert; /* bytes at end of window left to insert */ 0249 0250 #ifdef ZLIB_DEBUG 0251 ulg compressed_len; /* total bit length of compressed file mod 2^32 */ 0252 ulg bits_sent; /* bit length of compressed data sent mod 2^32 */ 0253 #endif 0254 0255 ush bi_buf; 0256 /* Output buffer. bits are inserted starting at the bottom (least 0257 * significant bits). 0258 */ 0259 int bi_valid; 0260 /* Number of valid bits in bi_buf. All bits above the last valid bit 0261 * are always zero. 0262 */ 0263 0264 ulg high_water; 0265 /* High water mark offset in window for initialized bytes -- bytes above 0266 * this are set to zero in order to avoid memory check warnings when 0267 * longest match routines access bytes past the input. This is then 0268 * updated to the new high water mark. 0269 */ 0270 0271 } FAR deflate_state; 0272 0273 /* Output a byte on the stream. 0274 * IN assertion: there is enough room in pending_buf. 0275 */ 0276 #define put_byte(s, c) {s->pending_buf[s->pending++] = (Bytef)(c);} 0277 0278 0279 #define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1) 0280 /* Minimum amount of lookahead, except at the end of the input file. 0281 * See deflate.c for comments about the MIN_MATCH+1. 0282 */ 0283 0284 #define MAX_DIST(s) ((s)->w_size-MIN_LOOKAHEAD) 0285 /* In order to simplify the code, particularly on 16 bit machines, match 0286 * distances are limited to MAX_DIST instead of WSIZE. 0287 */ 0288 0289 #define WIN_INIT MAX_MATCH 0290 /* Number of bytes after end of data in window to initialize in order to avoid 0291 memory checker errors from longest match routines */ 0292 0293 /* in trees.c */ 0294 void ZLIB_INTERNAL _tr_init OF((deflate_state *s)); 0295 int ZLIB_INTERNAL _tr_tally OF((deflate_state *s, unsigned dist, unsigned lc)); 0296 void ZLIB_INTERNAL _tr_flush_block OF((deflate_state *s, charf *buf, 0297 ulg stored_len, int last)); 0298 void ZLIB_INTERNAL _tr_flush_bits OF((deflate_state *s)); 0299 void ZLIB_INTERNAL _tr_align OF((deflate_state *s)); 0300 void ZLIB_INTERNAL _tr_stored_block OF((deflate_state *s, charf *buf, 0301 ulg stored_len, int last)); 0302 0303 #define d_code(dist) \ 0304 ((dist) < 256 ? _dist_code[dist] : _dist_code[256+((dist)>>7)]) 0305 /* Mapping from a distance to a distance code. dist is the distance - 1 and 0306 * must not have side effects. _dist_code[256] and _dist_code[257] are never 0307 * used. 0308 */ 0309 0310 #ifndef ZLIB_DEBUG 0311 /* Inline versions of _tr_tally for speed: */ 0312 0313 #if defined(GEN_TREES_H) || !defined(STDC) 0314 extern uch ZLIB_INTERNAL _length_code[]; 0315 extern uch ZLIB_INTERNAL _dist_code[]; 0316 #else 0317 extern const uch ZLIB_INTERNAL _length_code[]; 0318 extern const uch ZLIB_INTERNAL _dist_code[]; 0319 #endif 0320 0321 # define _tr_tally_lit(s, c, flush) \ 0322 { uch cc = (c); \ 0323 s->sym_buf[s->sym_next++] = 0; \ 0324 s->sym_buf[s->sym_next++] = 0; \ 0325 s->sym_buf[s->sym_next++] = cc; \ 0326 s->dyn_ltree[cc].Freq++; \ 0327 flush = (s->sym_next == s->sym_end); \ 0328 } 0329 # define _tr_tally_dist(s, distance, length, flush) \ 0330 { uch len = (uch)(length); \ 0331 ush dist = (ush)(distance); \ 0332 s->sym_buf[s->sym_next++] = (uch)dist; \ 0333 s->sym_buf[s->sym_next++] = (uch)(dist >> 8); \ 0334 s->sym_buf[s->sym_next++] = len; \ 0335 dist--; \ 0336 s->dyn_ltree[_length_code[len]+LITERALS+1].Freq++; \ 0337 s->dyn_dtree[d_code(dist)].Freq++; \ 0338 flush = (s->sym_next == s->sym_end); \ 0339 } 0340 #else 0341 # define _tr_tally_lit(s, c, flush) flush = _tr_tally(s, 0, c) 0342 # define _tr_tally_dist(s, distance, length, flush) \ 0343 flush = _tr_tally(s, distance, length) 0344 #endif 0345 0346 #endif /* DEFLATE_H */
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
![]() ![]() |