1b39c5158Smillert /* deflate.h -- internal compression state 2*3d61058aSafresh1 * Copyright (C) 1995-2024 Jean-loup Gailly 3b39c5158Smillert * For conditions of distribution and use, see copyright notice in zlib.h 4b39c5158Smillert */ 5b39c5158Smillert 6b39c5158Smillert /* WARNING: this file should *not* be used by applications. It is 7b39c5158Smillert part of the implementation of the compression library and is 8b39c5158Smillert subject to change. Applications should only use zlib.h. 9b39c5158Smillert */ 10b39c5158Smillert 11b39c5158Smillert /* @(#) $Id$ */ 12b39c5158Smillert 13b39c5158Smillert #ifndef DEFLATE_H 14b39c5158Smillert #define DEFLATE_H 15b39c5158Smillert 16b39c5158Smillert #include "zutil.h" 17b39c5158Smillert 18b39c5158Smillert /* define NO_GZIP when compiling if you want to disable gzip header and 19b39c5158Smillert trailer creation by deflate(). NO_GZIP would be used to avoid linking in 20b39c5158Smillert the crc code when it is not needed. For shared libraries, gzip encoding 21b39c5158Smillert should be left enabled. */ 22b39c5158Smillert #ifndef NO_GZIP 23b39c5158Smillert # define GZIP 24b39c5158Smillert #endif 25b39c5158Smillert 26*3d61058aSafresh1 /* define LIT_MEM to slightly increase the speed of deflate (order 1% to 2%) at 27*3d61058aSafresh1 the cost of a larger memory footprint */ 28*3d61058aSafresh1 /* #define LIT_MEM */ 29*3d61058aSafresh1 30b39c5158Smillert /* =========================================================================== 31b39c5158Smillert * Internal compression state. 32b39c5158Smillert */ 33b39c5158Smillert 34b39c5158Smillert #define LENGTH_CODES 29 35b39c5158Smillert /* number of length codes, not counting the special END_BLOCK code */ 36b39c5158Smillert 37b39c5158Smillert #define LITERALS 256 38b39c5158Smillert /* number of literal bytes 0..255 */ 39b39c5158Smillert 40b39c5158Smillert #define L_CODES (LITERALS+1+LENGTH_CODES) 41b39c5158Smillert /* number of Literal or Length codes, including the END_BLOCK code */ 42b39c5158Smillert 43b39c5158Smillert #define D_CODES 30 44b39c5158Smillert /* number of distance codes */ 45b39c5158Smillert 46b39c5158Smillert #define BL_CODES 19 47b39c5158Smillert /* number of codes used to transfer the bit lengths */ 48b39c5158Smillert 49b39c5158Smillert #define HEAP_SIZE (2*L_CODES+1) 50b39c5158Smillert /* maximum heap size */ 51b39c5158Smillert 52b39c5158Smillert #define MAX_BITS 15 53b39c5158Smillert /* All codes must not exceed MAX_BITS bits */ 54b39c5158Smillert 55e9ce3842Safresh1 #define Buf_size 16 56e9ce3842Safresh1 /* size of bit buffer in bi_buf */ 57e9ce3842Safresh1 589f11ffb7Safresh1 #define INIT_STATE 42 /* zlib header -> BUSY_STATE */ 599f11ffb7Safresh1 #ifdef GZIP 609f11ffb7Safresh1 # define GZIP_STATE 57 /* gzip header -> BUSY_STATE | EXTRA_STATE */ 619f11ffb7Safresh1 #endif 629f11ffb7Safresh1 #define EXTRA_STATE 69 /* gzip extra block -> NAME_STATE */ 639f11ffb7Safresh1 #define NAME_STATE 73 /* gzip file name -> COMMENT_STATE */ 649f11ffb7Safresh1 #define COMMENT_STATE 91 /* gzip comment -> HCRC_STATE */ 659f11ffb7Safresh1 #define HCRC_STATE 103 /* gzip header CRC -> BUSY_STATE */ 669f11ffb7Safresh1 #define BUSY_STATE 113 /* deflate -> FINISH_STATE */ 679f11ffb7Safresh1 #define FINISH_STATE 666 /* stream complete */ 68b39c5158Smillert /* Stream status */ 69b39c5158Smillert 70b39c5158Smillert 71b39c5158Smillert /* Data structure describing a single value and its code string. */ 72b39c5158Smillert typedef struct ct_data_s { 73b39c5158Smillert union { 74b39c5158Smillert ush freq; /* frequency count */ 75b39c5158Smillert ush code; /* bit string */ 76b39c5158Smillert } fc; 77b39c5158Smillert union { 78b39c5158Smillert ush dad; /* father node in Huffman tree */ 79b39c5158Smillert ush len; /* length of bit string */ 80b39c5158Smillert } dl; 81b39c5158Smillert } FAR ct_data; 82b39c5158Smillert 83b39c5158Smillert #define Freq fc.freq 84b39c5158Smillert #define Code fc.code 85b39c5158Smillert #define Dad dl.dad 86b39c5158Smillert #define Len dl.len 87b39c5158Smillert 88b39c5158Smillert typedef struct static_tree_desc_s static_tree_desc; 89b39c5158Smillert 90b39c5158Smillert typedef struct tree_desc_s { 91b39c5158Smillert ct_data *dyn_tree; /* the dynamic tree */ 92b39c5158Smillert int max_code; /* largest code with non zero frequency */ 93b8851fccSafresh1 const static_tree_desc *stat_desc; /* the corresponding static tree */ 94b39c5158Smillert } FAR tree_desc; 95b39c5158Smillert 96b39c5158Smillert typedef ush Pos; 97b39c5158Smillert typedef Pos FAR Posf; 98b39c5158Smillert typedef unsigned IPos; 99b39c5158Smillert 100b39c5158Smillert /* A Pos is an index in the character window. We use short instead of int to 101b39c5158Smillert * save space in the various tables. IPos is used only for parameter passing. 102b39c5158Smillert */ 103b39c5158Smillert 104b39c5158Smillert typedef struct internal_state { 105b39c5158Smillert z_streamp strm; /* pointer back to this zlib stream */ 106b39c5158Smillert int status; /* as the name implies */ 107b39c5158Smillert Bytef *pending_buf; /* output still pending */ 108b39c5158Smillert ulg pending_buf_size; /* size of pending_buf */ 109b39c5158Smillert Bytef *pending_out; /* next pending byte to output to the stream */ 1109f11ffb7Safresh1 ulg pending; /* nb of bytes in the pending buffer */ 111b39c5158Smillert int wrap; /* bit 0 true for zlib, bit 1 true for gzip */ 112b39c5158Smillert gz_headerp gzhead; /* gzip header information to write */ 1139f11ffb7Safresh1 ulg gzindex; /* where in extra, name, or comment */ 114e5157e49Safresh1 Byte method; /* can only be DEFLATED */ 115b39c5158Smillert int last_flush; /* value of flush param for previous deflate call */ 116b39c5158Smillert 117b39c5158Smillert /* used by deflate.c: */ 118b39c5158Smillert 119b39c5158Smillert uInt w_size; /* LZ77 window size (32K by default) */ 120b39c5158Smillert uInt w_bits; /* log2(w_size) (8..16) */ 121b39c5158Smillert uInt w_mask; /* w_size - 1 */ 122b39c5158Smillert 123b39c5158Smillert Bytef *window; 124b39c5158Smillert /* Sliding window. Input bytes are read into the second half of the window, 125b39c5158Smillert * and move to the first half later to keep a dictionary of at least wSize 126b39c5158Smillert * bytes. With this organization, matches are limited to a distance of 127b39c5158Smillert * wSize-MAX_MATCH bytes, but this ensures that IO is always 128b39c5158Smillert * performed with a length multiple of the block size. Also, it limits 129b39c5158Smillert * the window size to 64K, which is quite useful on MSDOS. 130b39c5158Smillert * To do: use the user input buffer as sliding window. 131b39c5158Smillert */ 132b39c5158Smillert 133b39c5158Smillert ulg window_size; 134b39c5158Smillert /* Actual size of window: 2*wSize, except when the user input buffer 135b39c5158Smillert * is directly used as sliding window. 136b39c5158Smillert */ 137b39c5158Smillert 138b39c5158Smillert Posf *prev; 139b39c5158Smillert /* Link to older string with same hash index. To limit the size of this 140b39c5158Smillert * array to 64K, this link is maintained only for the last 32K strings. 141b39c5158Smillert * An index in this array is thus a window index modulo 32K. 142b39c5158Smillert */ 143b39c5158Smillert 144b39c5158Smillert Posf *head; /* Heads of the hash chains or NIL. */ 145b39c5158Smillert 146b39c5158Smillert uInt ins_h; /* hash index of string to be inserted */ 147b39c5158Smillert uInt hash_size; /* number of elements in hash table */ 148b39c5158Smillert uInt hash_bits; /* log2(hash_size) */ 149b39c5158Smillert uInt hash_mask; /* hash_size-1 */ 150b39c5158Smillert 151b39c5158Smillert uInt hash_shift; 152b39c5158Smillert /* Number of bits by which ins_h must be shifted at each input 153b39c5158Smillert * step. It must be such that after MIN_MATCH steps, the oldest 154b39c5158Smillert * byte no longer takes part in the hash key, that is: 155b39c5158Smillert * hash_shift * MIN_MATCH >= hash_bits 156b39c5158Smillert */ 157b39c5158Smillert 158b39c5158Smillert long block_start; 159b39c5158Smillert /* Window position at the beginning of the current output block. Gets 160b39c5158Smillert * negative when the window is moved backwards. 161b39c5158Smillert */ 162b39c5158Smillert 163b39c5158Smillert uInt match_length; /* length of best match */ 164b39c5158Smillert IPos prev_match; /* previous match */ 165b39c5158Smillert int match_available; /* set if previous match exists */ 166b39c5158Smillert uInt strstart; /* start of string to insert */ 167b39c5158Smillert uInt match_start; /* start of matching string */ 168b39c5158Smillert uInt lookahead; /* number of valid bytes ahead in window */ 169b39c5158Smillert 170b39c5158Smillert uInt prev_length; 171b39c5158Smillert /* Length of the best match at previous step. Matches not greater than this 172b39c5158Smillert * are discarded. This is used in the lazy match evaluation. 173b39c5158Smillert */ 174b39c5158Smillert 175b39c5158Smillert uInt max_chain_length; 176b39c5158Smillert /* To speed up deflation, hash chains are never searched beyond this 177b39c5158Smillert * length. A higher limit improves compression ratio but degrades the 178b39c5158Smillert * speed. 179b39c5158Smillert */ 180b39c5158Smillert 181b39c5158Smillert uInt max_lazy_match; 182b39c5158Smillert /* Attempt to find a better match only when the current match is strictly 183b39c5158Smillert * smaller than this value. This mechanism is used only for compression 184b39c5158Smillert * levels >= 4. 185b39c5158Smillert */ 186b39c5158Smillert # define max_insert_length max_lazy_match 187b39c5158Smillert /* Insert new strings in the hash table only if the match length is not 188b39c5158Smillert * greater than this length. This saves time but degrades compression. 189b39c5158Smillert * max_insert_length is used only for compression levels <= 3. 190b39c5158Smillert */ 191b39c5158Smillert 192b39c5158Smillert int level; /* compression level (1..9) */ 193b39c5158Smillert int strategy; /* favor or force Huffman coding*/ 194b39c5158Smillert 195b39c5158Smillert uInt good_match; 196b39c5158Smillert /* Use a faster search when the previous match is longer than this */ 197b39c5158Smillert 198b39c5158Smillert int nice_match; /* Stop searching when current match exceeds this */ 199b39c5158Smillert 200b39c5158Smillert /* used by trees.c: */ 201e9ce3842Safresh1 /* Didn't use ct_data typedef below to suppress compiler warning */ 202b39c5158Smillert struct ct_data_s dyn_ltree[HEAP_SIZE]; /* literal and length tree */ 203b39c5158Smillert struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */ 204b39c5158Smillert struct ct_data_s bl_tree[2*BL_CODES+1]; /* Huffman tree for bit lengths */ 205b39c5158Smillert 206b39c5158Smillert struct tree_desc_s l_desc; /* desc. for literal tree */ 207b39c5158Smillert struct tree_desc_s d_desc; /* desc. for distance tree */ 208b39c5158Smillert struct tree_desc_s bl_desc; /* desc. for bit length tree */ 209b39c5158Smillert 210b39c5158Smillert ush bl_count[MAX_BITS+1]; 211b39c5158Smillert /* number of codes at each bit length for an optimal tree */ 212b39c5158Smillert 213b39c5158Smillert int heap[2*L_CODES+1]; /* heap used to build the Huffman trees */ 214b39c5158Smillert int heap_len; /* number of elements in the heap */ 215b39c5158Smillert int heap_max; /* element of largest frequency */ 216b39c5158Smillert /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used. 217b39c5158Smillert * The same heap array is used to build all trees. 218b39c5158Smillert */ 219b39c5158Smillert 220b39c5158Smillert uch depth[2*L_CODES+1]; 221b39c5158Smillert /* Depth of each subtree used as tie breaker for trees of equal frequency 222b39c5158Smillert */ 223b39c5158Smillert 224*3d61058aSafresh1 #ifdef LIT_MEM 225*3d61058aSafresh1 # define LIT_BUFS 5 226*3d61058aSafresh1 ushf *d_buf; /* buffer for distances */ 227*3d61058aSafresh1 uchf *l_buf; /* buffer for literals/lengths */ 228*3d61058aSafresh1 #else 229*3d61058aSafresh1 # define LIT_BUFS 4 230fc2b99ebSbluhm uchf *sym_buf; /* buffer for distances and literals/lengths */ 231*3d61058aSafresh1 #endif 232b39c5158Smillert 233b39c5158Smillert uInt lit_bufsize; 234b39c5158Smillert /* Size of match buffer for literals/lengths. There are 4 reasons for 235b39c5158Smillert * limiting lit_bufsize to 64K: 236b39c5158Smillert * - frequencies can be kept in 16 bit counters 237b39c5158Smillert * - if compression is not successful for the first block, all input 238b39c5158Smillert * data is still in the window so we can still emit a stored block even 239b39c5158Smillert * when input comes from standard input. (This can also be done for 240b39c5158Smillert * all blocks if lit_bufsize is not greater than 32K.) 241b39c5158Smillert * - if compression is not successful for a file smaller than 64K, we can 242b39c5158Smillert * even emit a stored file instead of a stored block (saving 5 bytes). 243b39c5158Smillert * This is applicable only for zip (not gzip or zlib). 244b39c5158Smillert * - creating new Huffman trees less frequently may not provide fast 245b39c5158Smillert * adaptation to changes in the input data statistics. (Take for 246b39c5158Smillert * example a binary file with poorly compressible code followed by 247b39c5158Smillert * a highly compressible string table.) Smaller buffer sizes give 248b39c5158Smillert * fast adaptation but have of course the overhead of transmitting 249b39c5158Smillert * trees more frequently. 250b39c5158Smillert * - I can't count above 4 251b39c5158Smillert */ 252b39c5158Smillert 253*3d61058aSafresh1 uInt sym_next; /* running index in symbol buffer */ 254fc2b99ebSbluhm uInt sym_end; /* symbol table full when sym_next reaches this */ 255b39c5158Smillert 256b39c5158Smillert ulg opt_len; /* bit length of current block with optimal trees */ 257b39c5158Smillert ulg static_len; /* bit length of current block with static trees */ 258b39c5158Smillert uInt matches; /* number of string matches in current block */ 259e9ce3842Safresh1 uInt insert; /* bytes at end of window left to insert */ 260b39c5158Smillert 2619f11ffb7Safresh1 #ifdef ZLIB_DEBUG 262b39c5158Smillert ulg compressed_len; /* total bit length of compressed file mod 2^32 */ 263b39c5158Smillert ulg bits_sent; /* bit length of compressed data sent mod 2^32 */ 264b39c5158Smillert #endif 265b39c5158Smillert 266b39c5158Smillert ush bi_buf; 267b39c5158Smillert /* Output buffer. bits are inserted starting at the bottom (least 268b39c5158Smillert * significant bits). 269b39c5158Smillert */ 270b39c5158Smillert int bi_valid; 271b39c5158Smillert /* Number of valid bits in bi_buf. All bits above the last valid bit 272b39c5158Smillert * are always zero. 273b39c5158Smillert */ 274b39c5158Smillert 27548950c12Ssthen ulg high_water; 27648950c12Ssthen /* High water mark offset in window for initialized bytes -- bytes above 27748950c12Ssthen * this are set to zero in order to avoid memory check warnings when 27848950c12Ssthen * longest match routines access bytes past the input. This is then 27948950c12Ssthen * updated to the new high water mark. 28048950c12Ssthen */ 28148950c12Ssthen 282b39c5158Smillert } FAR deflate_state; 283b39c5158Smillert 284b39c5158Smillert /* Output a byte on the stream. 285b39c5158Smillert * IN assertion: there is enough room in pending_buf. 286b39c5158Smillert */ 2879f11ffb7Safresh1 #define put_byte(s, c) {s->pending_buf[s->pending++] = (Bytef)(c);} 288b39c5158Smillert 289b39c5158Smillert 290b39c5158Smillert #define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1) 291b39c5158Smillert /* Minimum amount of lookahead, except at the end of the input file. 292b39c5158Smillert * See deflate.c for comments about the MIN_MATCH+1. 293b39c5158Smillert */ 294b39c5158Smillert 295b39c5158Smillert #define MAX_DIST(s) ((s)->w_size-MIN_LOOKAHEAD) 296b39c5158Smillert /* In order to simplify the code, particularly on 16 bit machines, match 297b39c5158Smillert * distances are limited to MAX_DIST instead of WSIZE. 298b39c5158Smillert */ 299b39c5158Smillert 30048950c12Ssthen #define WIN_INIT MAX_MATCH 30148950c12Ssthen /* Number of bytes after end of data in window to initialize in order to avoid 30248950c12Ssthen memory checker errors from longest match routines */ 30348950c12Ssthen 304b39c5158Smillert /* in trees.c */ 305*3d61058aSafresh1 void ZLIB_INTERNAL _tr_init(deflate_state *s); 306*3d61058aSafresh1 int ZLIB_INTERNAL _tr_tally(deflate_state *s, unsigned dist, unsigned lc); 307*3d61058aSafresh1 void ZLIB_INTERNAL _tr_flush_block(deflate_state *s, charf *buf, 308*3d61058aSafresh1 ulg stored_len, int last); 309*3d61058aSafresh1 void ZLIB_INTERNAL _tr_flush_bits(deflate_state *s); 310*3d61058aSafresh1 void ZLIB_INTERNAL _tr_align(deflate_state *s); 311*3d61058aSafresh1 void ZLIB_INTERNAL _tr_stored_block(deflate_state *s, charf *buf, 312*3d61058aSafresh1 ulg stored_len, int last); 313b39c5158Smillert 314b39c5158Smillert #define d_code(dist) \ 315b39c5158Smillert ((dist) < 256 ? _dist_code[dist] : _dist_code[256+((dist)>>7)]) 316b39c5158Smillert /* Mapping from a distance to a distance code. dist is the distance - 1 and 317b39c5158Smillert * must not have side effects. _dist_code[256] and _dist_code[257] are never 318b39c5158Smillert * used. 319b39c5158Smillert */ 320b39c5158Smillert 3219f11ffb7Safresh1 #ifndef ZLIB_DEBUG 322b39c5158Smillert /* Inline versions of _tr_tally for speed: */ 323b39c5158Smillert 324b39c5158Smillert #if defined(GEN_TREES_H) || !defined(STDC) 32548950c12Ssthen extern uch ZLIB_INTERNAL _length_code[]; 32648950c12Ssthen extern uch ZLIB_INTERNAL _dist_code[]; 327b39c5158Smillert #else 32848950c12Ssthen extern const uch ZLIB_INTERNAL _length_code[]; 32948950c12Ssthen extern const uch ZLIB_INTERNAL _dist_code[]; 330b39c5158Smillert #endif 331b39c5158Smillert 332*3d61058aSafresh1 #ifdef LIT_MEM 333*3d61058aSafresh1 # define _tr_tally_lit(s, c, flush) \ 334*3d61058aSafresh1 { uch cc = (c); \ 335*3d61058aSafresh1 s->d_buf[s->sym_next] = 0; \ 336*3d61058aSafresh1 s->l_buf[s->sym_next++] = cc; \ 337*3d61058aSafresh1 s->dyn_ltree[cc].Freq++; \ 338*3d61058aSafresh1 flush = (s->sym_next == s->sym_end); \ 339*3d61058aSafresh1 } 340*3d61058aSafresh1 # define _tr_tally_dist(s, distance, length, flush) \ 341*3d61058aSafresh1 { uch len = (uch)(length); \ 342*3d61058aSafresh1 ush dist = (ush)(distance); \ 343*3d61058aSafresh1 s->d_buf[s->sym_next] = dist; \ 344*3d61058aSafresh1 s->l_buf[s->sym_next++] = len; \ 345*3d61058aSafresh1 dist--; \ 346*3d61058aSafresh1 s->dyn_ltree[_length_code[len]+LITERALS+1].Freq++; \ 347*3d61058aSafresh1 s->dyn_dtree[d_code(dist)].Freq++; \ 348*3d61058aSafresh1 flush = (s->sym_next == s->sym_end); \ 349*3d61058aSafresh1 } 350*3d61058aSafresh1 #else 351b39c5158Smillert # define _tr_tally_lit(s, c, flush) \ 352b39c5158Smillert { uch cc = (c); \ 353fc2b99ebSbluhm s->sym_buf[s->sym_next++] = 0; \ 354fc2b99ebSbluhm s->sym_buf[s->sym_next++] = 0; \ 355fc2b99ebSbluhm s->sym_buf[s->sym_next++] = cc; \ 356b39c5158Smillert s->dyn_ltree[cc].Freq++; \ 357fc2b99ebSbluhm flush = (s->sym_next == s->sym_end); \ 358b39c5158Smillert } 359b39c5158Smillert # define _tr_tally_dist(s, distance, length, flush) \ 3609f11ffb7Safresh1 { uch len = (uch)(length); \ 3619f11ffb7Safresh1 ush dist = (ush)(distance); \ 362e0680481Safresh1 s->sym_buf[s->sym_next++] = (uch)dist; \ 363e0680481Safresh1 s->sym_buf[s->sym_next++] = (uch)(dist >> 8); \ 364fc2b99ebSbluhm s->sym_buf[s->sym_next++] = len; \ 365b39c5158Smillert dist--; \ 366b39c5158Smillert s->dyn_ltree[_length_code[len]+LITERALS+1].Freq++; \ 367b39c5158Smillert s->dyn_dtree[d_code(dist)].Freq++; \ 368fc2b99ebSbluhm flush = (s->sym_next == s->sym_end); \ 369b39c5158Smillert } 370*3d61058aSafresh1 #endif 371b39c5158Smillert #else 372b39c5158Smillert # define _tr_tally_lit(s, c, flush) flush = _tr_tally(s, 0, c) 373b39c5158Smillert # define _tr_tally_dist(s, distance, length, flush) \ 374b39c5158Smillert flush = _tr_tally(s, distance, length) 375b39c5158Smillert #endif 376b39c5158Smillert 377b39c5158Smillert #endif /* DEFLATE_H */ 378