16141cf33Sderaadt /* deflate.h -- internal compression state 2f0633749Stb * Copyright (C) 1995-2024 Jean-loup Gailly 36141cf33Sderaadt * For conditions of distribution and use, see copyright notice in zlib.h 46141cf33Sderaadt */ 56141cf33Sderaadt 66141cf33Sderaadt /* WARNING: this file should *not* be used by applications. It is 76141cf33Sderaadt part of the implementation of the compression library and is 86141cf33Sderaadt subject to change. Applications should only use zlib.h. 96141cf33Sderaadt */ 106141cf33Sderaadt 116141cf33Sderaadt #ifndef DEFLATE_H 126141cf33Sderaadt #define DEFLATE_H 136141cf33Sderaadt 146141cf33Sderaadt #include "zutil.h" 156141cf33Sderaadt 166141cf33Sderaadt /* define NO_GZIP when compiling if you want to disable gzip header and 176141cf33Sderaadt trailer creation by deflate(). NO_GZIP would be used to avoid linking in 186141cf33Sderaadt the crc code when it is not needed. For shared libraries, gzip encoding 196141cf33Sderaadt should be left enabled. */ 206141cf33Sderaadt #ifndef NO_GZIP 216141cf33Sderaadt # define GZIP 226141cf33Sderaadt #endif 236141cf33Sderaadt 2485d95931Stb /* define LIT_MEM to slightly increase the speed of deflate (order 1% to 2%) at 2585d95931Stb the cost of a larger memory footprint */ 2685d95931Stb /* #define LIT_MEM */ 2785d95931Stb 286141cf33Sderaadt /* =========================================================================== 296141cf33Sderaadt * Internal compression state. 306141cf33Sderaadt */ 316141cf33Sderaadt 326141cf33Sderaadt #define LENGTH_CODES 29 336141cf33Sderaadt /* number of length codes, not counting the special END_BLOCK code */ 346141cf33Sderaadt 356141cf33Sderaadt #define LITERALS 256 366141cf33Sderaadt /* number of literal bytes 0..255 */ 376141cf33Sderaadt 386141cf33Sderaadt #define L_CODES (LITERALS+1+LENGTH_CODES) 396141cf33Sderaadt /* number of Literal or Length codes, including the END_BLOCK code */ 406141cf33Sderaadt 416141cf33Sderaadt #define D_CODES 30 426141cf33Sderaadt /* number of distance codes */ 436141cf33Sderaadt 446141cf33Sderaadt #define BL_CODES 19 456141cf33Sderaadt /* number of codes used to transfer the bit lengths */ 466141cf33Sderaadt 476141cf33Sderaadt #define HEAP_SIZE (2*L_CODES+1) 486141cf33Sderaadt /* maximum heap size */ 496141cf33Sderaadt 506141cf33Sderaadt #define MAX_BITS 15 516141cf33Sderaadt /* All codes must not exceed MAX_BITS bits */ 526141cf33Sderaadt 5336f395ceStb #define Buf_size 16 5436f395ceStb /* size of bit buffer in bi_buf */ 5536f395ceStb 5636f395ceStb #define INIT_STATE 42 /* zlib header -> BUSY_STATE */ 5736f395ceStb #ifdef GZIP 5836f395ceStb # define GZIP_STATE 57 /* gzip header -> BUSY_STATE | EXTRA_STATE */ 5936f395ceStb #endif 6036f395ceStb #define EXTRA_STATE 69 /* gzip extra block -> NAME_STATE */ 6136f395ceStb #define NAME_STATE 73 /* gzip file name -> COMMENT_STATE */ 6236f395ceStb #define COMMENT_STATE 91 /* gzip comment -> HCRC_STATE */ 6336f395ceStb #define HCRC_STATE 103 /* gzip header CRC -> BUSY_STATE */ 6436f395ceStb #define BUSY_STATE 113 /* deflate -> FINISH_STATE */ 6536f395ceStb #define FINISH_STATE 666 /* stream complete */ 666141cf33Sderaadt /* Stream status */ 676141cf33Sderaadt 686141cf33Sderaadt 696141cf33Sderaadt /* Data structure describing a single value and its code string. */ 706141cf33Sderaadt typedef struct ct_data_s { 716141cf33Sderaadt union { 726141cf33Sderaadt ush freq; /* frequency count */ 736141cf33Sderaadt ush code; /* bit string */ 746141cf33Sderaadt } fc; 756141cf33Sderaadt union { 766141cf33Sderaadt ush dad; /* father node in Huffman tree */ 776141cf33Sderaadt ush len; /* length of bit string */ 786141cf33Sderaadt } dl; 796141cf33Sderaadt } FAR ct_data; 806141cf33Sderaadt 816141cf33Sderaadt #define Freq fc.freq 826141cf33Sderaadt #define Code fc.code 836141cf33Sderaadt #define Dad dl.dad 846141cf33Sderaadt #define Len dl.len 856141cf33Sderaadt 866141cf33Sderaadt typedef struct static_tree_desc_s static_tree_desc; 876141cf33Sderaadt 886141cf33Sderaadt typedef struct tree_desc_s { 896141cf33Sderaadt ct_data *dyn_tree; /* the dynamic tree */ 906141cf33Sderaadt int max_code; /* largest code with non zero frequency */ 9136f395ceStb const static_tree_desc *stat_desc; /* the corresponding static tree */ 926141cf33Sderaadt } FAR tree_desc; 936141cf33Sderaadt 946141cf33Sderaadt typedef ush Pos; 956141cf33Sderaadt typedef Pos FAR Posf; 966141cf33Sderaadt typedef unsigned IPos; 976141cf33Sderaadt 986141cf33Sderaadt /* A Pos is an index in the character window. We use short instead of int to 996141cf33Sderaadt * save space in the various tables. IPos is used only for parameter passing. 1006141cf33Sderaadt */ 1016141cf33Sderaadt 1026141cf33Sderaadt typedef struct internal_state { 1036141cf33Sderaadt z_streamp strm; /* pointer back to this zlib stream */ 1046141cf33Sderaadt int status; /* as the name implies */ 1056141cf33Sderaadt Bytef *pending_buf; /* output still pending */ 1066141cf33Sderaadt ulg pending_buf_size; /* size of pending_buf */ 1076141cf33Sderaadt Bytef *pending_out; /* next pending byte to output to the stream */ 10836f395ceStb ulg pending; /* nb of bytes in the pending buffer */ 1096141cf33Sderaadt int wrap; /* bit 0 true for zlib, bit 1 true for gzip */ 1106141cf33Sderaadt gz_headerp gzhead; /* gzip header information to write */ 11136f395ceStb ulg gzindex; /* where in extra, name, or comment */ 11236f395ceStb Byte method; /* can only be DEFLATED */ 1136141cf33Sderaadt int last_flush; /* value of flush param for previous deflate call */ 1146141cf33Sderaadt 1156141cf33Sderaadt /* used by deflate.c: */ 1166141cf33Sderaadt 1176141cf33Sderaadt uInt w_size; /* LZ77 window size (32K by default) */ 1186141cf33Sderaadt uInt w_bits; /* log2(w_size) (8..16) */ 1196141cf33Sderaadt uInt w_mask; /* w_size - 1 */ 1206141cf33Sderaadt 1216141cf33Sderaadt Bytef *window; 1226141cf33Sderaadt /* Sliding window. Input bytes are read into the second half of the window, 1236141cf33Sderaadt * and move to the first half later to keep a dictionary of at least wSize 1246141cf33Sderaadt * bytes. With this organization, matches are limited to a distance of 1256141cf33Sderaadt * wSize-MAX_MATCH bytes, but this ensures that IO is always 1266141cf33Sderaadt * performed with a length multiple of the block size. Also, it limits 1276141cf33Sderaadt * the window size to 64K, which is quite useful on MSDOS. 1286141cf33Sderaadt * To do: use the user input buffer as sliding window. 1296141cf33Sderaadt */ 1306141cf33Sderaadt 1316141cf33Sderaadt ulg window_size; 1326141cf33Sderaadt /* Actual size of window: 2*wSize, except when the user input buffer 1336141cf33Sderaadt * is directly used as sliding window. 1346141cf33Sderaadt */ 1356141cf33Sderaadt 1366141cf33Sderaadt Posf *prev; 1376141cf33Sderaadt /* Link to older string with same hash index. To limit the size of this 1386141cf33Sderaadt * array to 64K, this link is maintained only for the last 32K strings. 1396141cf33Sderaadt * An index in this array is thus a window index modulo 32K. 1406141cf33Sderaadt */ 1416141cf33Sderaadt 1426141cf33Sderaadt Posf *head; /* Heads of the hash chains or NIL. */ 1436141cf33Sderaadt 1446141cf33Sderaadt uInt ins_h; /* hash index of string to be inserted */ 1456141cf33Sderaadt uInt hash_size; /* number of elements in hash table */ 1466141cf33Sderaadt uInt hash_bits; /* log2(hash_size) */ 1476141cf33Sderaadt uInt hash_mask; /* hash_size-1 */ 1486141cf33Sderaadt 1496141cf33Sderaadt uInt hash_shift; 1506141cf33Sderaadt /* Number of bits by which ins_h must be shifted at each input 1516141cf33Sderaadt * step. It must be such that after MIN_MATCH steps, the oldest 1526141cf33Sderaadt * byte no longer takes part in the hash key, that is: 1536141cf33Sderaadt * hash_shift * MIN_MATCH >= hash_bits 1546141cf33Sderaadt */ 1556141cf33Sderaadt 1566141cf33Sderaadt long block_start; 1576141cf33Sderaadt /* Window position at the beginning of the current output block. Gets 1586141cf33Sderaadt * negative when the window is moved backwards. 1596141cf33Sderaadt */ 1606141cf33Sderaadt 1616141cf33Sderaadt uInt match_length; /* length of best match */ 1626141cf33Sderaadt IPos prev_match; /* previous match */ 1636141cf33Sderaadt int match_available; /* set if previous match exists */ 1646141cf33Sderaadt uInt strstart; /* start of string to insert */ 1656141cf33Sderaadt uInt match_start; /* start of matching string */ 1666141cf33Sderaadt uInt lookahead; /* number of valid bytes ahead in window */ 1676141cf33Sderaadt 1686141cf33Sderaadt uInt prev_length; 1696141cf33Sderaadt /* Length of the best match at previous step. Matches not greater than this 1706141cf33Sderaadt * are discarded. This is used in the lazy match evaluation. 1716141cf33Sderaadt */ 1726141cf33Sderaadt 1736141cf33Sderaadt uInt max_chain_length; 1746141cf33Sderaadt /* To speed up deflation, hash chains are never searched beyond this 1756141cf33Sderaadt * length. A higher limit improves compression ratio but degrades the 1766141cf33Sderaadt * speed. 1776141cf33Sderaadt */ 1786141cf33Sderaadt 1796141cf33Sderaadt uInt max_lazy_match; 1806141cf33Sderaadt /* Attempt to find a better match only when the current match is strictly 1816141cf33Sderaadt * smaller than this value. This mechanism is used only for compression 1826141cf33Sderaadt * levels >= 4. 1836141cf33Sderaadt */ 1846141cf33Sderaadt # define max_insert_length max_lazy_match 1856141cf33Sderaadt /* Insert new strings in the hash table only if the match length is not 1866141cf33Sderaadt * greater than this length. This saves time but degrades compression. 1876141cf33Sderaadt * max_insert_length is used only for compression levels <= 3. 1886141cf33Sderaadt */ 1896141cf33Sderaadt 1906141cf33Sderaadt int level; /* compression level (1..9) */ 1916141cf33Sderaadt int strategy; /* favor or force Huffman coding*/ 1926141cf33Sderaadt 1936141cf33Sderaadt uInt good_match; 1946141cf33Sderaadt /* Use a faster search when the previous match is longer than this */ 1956141cf33Sderaadt 1966141cf33Sderaadt int nice_match; /* Stop searching when current match exceeds this */ 1976141cf33Sderaadt 1986141cf33Sderaadt /* used by trees.c: */ 1996141cf33Sderaadt /* Didn't use ct_data typedef below to suppress compiler warning */ 2006141cf33Sderaadt struct ct_data_s dyn_ltree[HEAP_SIZE]; /* literal and length tree */ 2016141cf33Sderaadt struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */ 2026141cf33Sderaadt struct ct_data_s bl_tree[2*BL_CODES+1]; /* Huffman tree for bit lengths */ 2036141cf33Sderaadt 2046141cf33Sderaadt struct tree_desc_s l_desc; /* desc. for literal tree */ 2056141cf33Sderaadt struct tree_desc_s d_desc; /* desc. for distance tree */ 2066141cf33Sderaadt struct tree_desc_s bl_desc; /* desc. for bit length tree */ 2076141cf33Sderaadt 2086141cf33Sderaadt ush bl_count[MAX_BITS+1]; 2096141cf33Sderaadt /* number of codes at each bit length for an optimal tree */ 2106141cf33Sderaadt 2116141cf33Sderaadt int heap[2*L_CODES+1]; /* heap used to build the Huffman trees */ 2126141cf33Sderaadt int heap_len; /* number of elements in the heap */ 2136141cf33Sderaadt int heap_max; /* element of largest frequency */ 2146141cf33Sderaadt /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used. 2156141cf33Sderaadt * The same heap array is used to build all trees. 2166141cf33Sderaadt */ 2176141cf33Sderaadt 2186141cf33Sderaadt uch depth[2*L_CODES+1]; 2196141cf33Sderaadt /* Depth of each subtree used as tie breaker for trees of equal frequency 2206141cf33Sderaadt */ 2216141cf33Sderaadt 22285d95931Stb #ifdef LIT_MEM 223555b046eStb # define LIT_BUFS 5 22485d95931Stb ushf *d_buf; /* buffer for distances */ 22585d95931Stb uchf *l_buf; /* buffer for literals/lengths */ 22685d95931Stb #else 227555b046eStb # define LIT_BUFS 4 22856e03632Stb uchf *sym_buf; /* buffer for distances and literals/lengths */ 22985d95931Stb #endif 2306141cf33Sderaadt 2316141cf33Sderaadt uInt lit_bufsize; 2326141cf33Sderaadt /* Size of match buffer for literals/lengths. There are 4 reasons for 2336141cf33Sderaadt * limiting lit_bufsize to 64K: 2346141cf33Sderaadt * - frequencies can be kept in 16 bit counters 2356141cf33Sderaadt * - if compression is not successful for the first block, all input 2366141cf33Sderaadt * data is still in the window so we can still emit a stored block even 2376141cf33Sderaadt * when input comes from standard input. (This can also be done for 2386141cf33Sderaadt * all blocks if lit_bufsize is not greater than 32K.) 2396141cf33Sderaadt * - if compression is not successful for a file smaller than 64K, we can 2406141cf33Sderaadt * even emit a stored file instead of a stored block (saving 5 bytes). 2416141cf33Sderaadt * This is applicable only for zip (not gzip or zlib). 2426141cf33Sderaadt * - creating new Huffman trees less frequently may not provide fast 2436141cf33Sderaadt * adaptation to changes in the input data statistics. (Take for 2446141cf33Sderaadt * example a binary file with poorly compressible code followed by 2456141cf33Sderaadt * a highly compressible string table.) Smaller buffer sizes give 2466141cf33Sderaadt * fast adaptation but have of course the overhead of transmitting 2476141cf33Sderaadt * trees more frequently. 2486141cf33Sderaadt * - I can't count above 4 2496141cf33Sderaadt */ 2506141cf33Sderaadt 25185d95931Stb uInt sym_next; /* running index in symbol buffer */ 25256e03632Stb uInt sym_end; /* symbol table full when sym_next reaches this */ 2536141cf33Sderaadt 2546141cf33Sderaadt ulg opt_len; /* bit length of current block with optimal trees */ 2556141cf33Sderaadt ulg static_len; /* bit length of current block with static trees */ 2566141cf33Sderaadt uInt matches; /* number of string matches in current block */ 25736f395ceStb uInt insert; /* bytes at end of window left to insert */ 2586141cf33Sderaadt 25936f395ceStb #ifdef ZLIB_DEBUG 2606141cf33Sderaadt ulg compressed_len; /* total bit length of compressed file mod 2^32 */ 2616141cf33Sderaadt ulg bits_sent; /* bit length of compressed data sent mod 2^32 */ 2626141cf33Sderaadt #endif 2636141cf33Sderaadt 2646141cf33Sderaadt ush bi_buf; 2656141cf33Sderaadt /* Output buffer. bits are inserted starting at the bottom (least 2666141cf33Sderaadt * significant bits). 2676141cf33Sderaadt */ 2686141cf33Sderaadt int bi_valid; 2696141cf33Sderaadt /* Number of valid bits in bi_buf. All bits above the last valid bit 2706141cf33Sderaadt * are always zero. 2716141cf33Sderaadt */ 272*0a218225Stb int bi_used; 273*0a218225Stb /* Last number of used bits when going to a byte boundary. 274*0a218225Stb */ 2756141cf33Sderaadt 27636f395ceStb ulg high_water; 27736f395ceStb /* High water mark offset in window for initialized bytes -- bytes above 27836f395ceStb * this are set to zero in order to avoid memory check warnings when 27936f395ceStb * longest match routines access bytes past the input. This is then 28036f395ceStb * updated to the new high water mark. 28136f395ceStb */ 28236f395ceStb 2836141cf33Sderaadt } FAR deflate_state; 2846141cf33Sderaadt 2856141cf33Sderaadt /* Output a byte on the stream. 2866141cf33Sderaadt * IN assertion: there is enough room in pending_buf. 2876141cf33Sderaadt */ 28836f395ceStb #define put_byte(s, c) {s->pending_buf[s->pending++] = (Bytef)(c);} 2896141cf33Sderaadt 2906141cf33Sderaadt 2916141cf33Sderaadt #define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1) 2926141cf33Sderaadt /* Minimum amount of lookahead, except at the end of the input file. 2936141cf33Sderaadt * See deflate.c for comments about the MIN_MATCH+1. 2946141cf33Sderaadt */ 2956141cf33Sderaadt 2966141cf33Sderaadt #define MAX_DIST(s) ((s)->w_size-MIN_LOOKAHEAD) 2976141cf33Sderaadt /* In order to simplify the code, particularly on 16 bit machines, match 2986141cf33Sderaadt * distances are limited to MAX_DIST instead of WSIZE. 2996141cf33Sderaadt */ 3006141cf33Sderaadt 30136f395ceStb #define WIN_INIT MAX_MATCH 30236f395ceStb /* Number of bytes after end of data in window to initialize in order to avoid 30336f395ceStb memory checker errors from longest match routines */ 30436f395ceStb 3056141cf33Sderaadt /* in trees.c */ 306cfac609cStb void ZLIB_INTERNAL _tr_init(deflate_state *s); 307cfac609cStb int ZLIB_INTERNAL _tr_tally(deflate_state *s, unsigned dist, unsigned lc); 308cfac609cStb void ZLIB_INTERNAL _tr_flush_block(deflate_state *s, charf *buf, 309cfac609cStb ulg stored_len, int last); 310cfac609cStb void ZLIB_INTERNAL _tr_flush_bits(deflate_state *s); 311cfac609cStb void ZLIB_INTERNAL _tr_align(deflate_state *s); 312cfac609cStb void ZLIB_INTERNAL _tr_stored_block(deflate_state *s, charf *buf, 313cfac609cStb ulg stored_len, int last); 3146141cf33Sderaadt 3156141cf33Sderaadt #define d_code(dist) \ 3166141cf33Sderaadt ((dist) < 256 ? _dist_code[dist] : _dist_code[256+((dist)>>7)]) 3176141cf33Sderaadt /* Mapping from a distance to a distance code. dist is the distance - 1 and 3186141cf33Sderaadt * must not have side effects. _dist_code[256] and _dist_code[257] are never 3196141cf33Sderaadt * used. 3206141cf33Sderaadt */ 3216141cf33Sderaadt 32236f395ceStb #ifndef ZLIB_DEBUG 3236141cf33Sderaadt /* Inline versions of _tr_tally for speed: */ 3246141cf33Sderaadt 3256141cf33Sderaadt #if defined(GEN_TREES_H) || !defined(STDC) 32636f395ceStb extern uch ZLIB_INTERNAL _length_code[]; 32736f395ceStb extern uch ZLIB_INTERNAL _dist_code[]; 3286141cf33Sderaadt #else 32936f395ceStb extern const uch ZLIB_INTERNAL _length_code[]; 33036f395ceStb extern const uch ZLIB_INTERNAL _dist_code[]; 3316141cf33Sderaadt #endif 3326141cf33Sderaadt 33385d95931Stb #ifdef LIT_MEM 33485d95931Stb # define _tr_tally_lit(s, c, flush) \ 33585d95931Stb { uch cc = (c); \ 33685d95931Stb s->d_buf[s->sym_next] = 0; \ 33785d95931Stb s->l_buf[s->sym_next++] = cc; \ 33885d95931Stb s->dyn_ltree[cc].Freq++; \ 33985d95931Stb flush = (s->sym_next == s->sym_end); \ 34085d95931Stb } 34185d95931Stb # define _tr_tally_dist(s, distance, length, flush) \ 34285d95931Stb { uch len = (uch)(length); \ 34385d95931Stb ush dist = (ush)(distance); \ 34485d95931Stb s->d_buf[s->sym_next] = dist; \ 34585d95931Stb s->l_buf[s->sym_next++] = len; \ 34685d95931Stb dist--; \ 34785d95931Stb s->dyn_ltree[_length_code[len]+LITERALS+1].Freq++; \ 34885d95931Stb s->dyn_dtree[d_code(dist)].Freq++; \ 34985d95931Stb flush = (s->sym_next == s->sym_end); \ 35085d95931Stb } 35185d95931Stb #else 3526141cf33Sderaadt # define _tr_tally_lit(s, c, flush) \ 3536141cf33Sderaadt { uch cc = (c); \ 35456e03632Stb s->sym_buf[s->sym_next++] = 0; \ 35556e03632Stb s->sym_buf[s->sym_next++] = 0; \ 35656e03632Stb s->sym_buf[s->sym_next++] = cc; \ 3576141cf33Sderaadt s->dyn_ltree[cc].Freq++; \ 35856e03632Stb flush = (s->sym_next == s->sym_end); \ 3596141cf33Sderaadt } 3606141cf33Sderaadt # define _tr_tally_dist(s, distance, length, flush) \ 36136f395ceStb { uch len = (uch)(length); \ 36236f395ceStb ush dist = (ush)(distance); \ 363ddf65acdStb s->sym_buf[s->sym_next++] = (uch)dist; \ 364ddf65acdStb s->sym_buf[s->sym_next++] = (uch)(dist >> 8); \ 36556e03632Stb s->sym_buf[s->sym_next++] = len; \ 3666141cf33Sderaadt dist--; \ 3676141cf33Sderaadt s->dyn_ltree[_length_code[len]+LITERALS+1].Freq++; \ 3686141cf33Sderaadt s->dyn_dtree[d_code(dist)].Freq++; \ 36956e03632Stb flush = (s->sym_next == s->sym_end); \ 3706141cf33Sderaadt } 37185d95931Stb #endif 3726141cf33Sderaadt #else 3736141cf33Sderaadt # define _tr_tally_lit(s, c, flush) flush = _tr_tally(s, 0, c) 3746141cf33Sderaadt # define _tr_tally_dist(s, distance, length, flush) \ 3756141cf33Sderaadt flush = _tr_tally(s, distance, length) 3766141cf33Sderaadt #endif 3776141cf33Sderaadt 3786141cf33Sderaadt #endif /* DEFLATE_H */ 379