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