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