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