cannam@89: /* deflate.h -- internal compression state cannam@89: * Copyright (C) 1995-2012 Jean-loup Gailly cannam@89: * For conditions of distribution and use, see copyright notice in zlib.h cannam@89: */ cannam@89: cannam@89: /* WARNING: this file should *not* be used by applications. It is cannam@89: part of the implementation of the compression library and is cannam@89: subject to change. Applications should only use zlib.h. cannam@89: */ cannam@89: cannam@89: /* @(#) $Id$ */ cannam@89: cannam@89: #ifndef DEFLATE_H cannam@89: #define DEFLATE_H cannam@89: cannam@89: #include "zutil.h" cannam@89: cannam@89: /* define NO_GZIP when compiling if you want to disable gzip header and cannam@89: trailer creation by deflate(). NO_GZIP would be used to avoid linking in cannam@89: the crc code when it is not needed. For shared libraries, gzip encoding cannam@89: should be left enabled. */ cannam@89: #ifndef NO_GZIP cannam@89: # define GZIP cannam@89: #endif cannam@89: cannam@89: /* =========================================================================== cannam@89: * Internal compression state. cannam@89: */ cannam@89: cannam@89: #define LENGTH_CODES 29 cannam@89: /* number of length codes, not counting the special END_BLOCK code */ cannam@89: cannam@89: #define LITERALS 256 cannam@89: /* number of literal bytes 0..255 */ cannam@89: cannam@89: #define L_CODES (LITERALS+1+LENGTH_CODES) cannam@89: /* number of Literal or Length codes, including the END_BLOCK code */ cannam@89: cannam@89: #define D_CODES 30 cannam@89: /* number of distance codes */ cannam@89: cannam@89: #define BL_CODES 19 cannam@89: /* number of codes used to transfer the bit lengths */ cannam@89: cannam@89: #define HEAP_SIZE (2*L_CODES+1) cannam@89: /* maximum heap size */ cannam@89: cannam@89: #define MAX_BITS 15 cannam@89: /* All codes must not exceed MAX_BITS bits */ cannam@89: cannam@89: #define Buf_size 16 cannam@89: /* size of bit buffer in bi_buf */ cannam@89: cannam@89: #define INIT_STATE 42 cannam@89: #define EXTRA_STATE 69 cannam@89: #define NAME_STATE 73 cannam@89: #define COMMENT_STATE 91 cannam@89: #define HCRC_STATE 103 cannam@89: #define BUSY_STATE 113 cannam@89: #define FINISH_STATE 666 cannam@89: /* Stream status */ cannam@89: cannam@89: cannam@89: /* Data structure describing a single value and its code string. */ cannam@89: typedef struct ct_data_s { cannam@89: union { cannam@89: ush freq; /* frequency count */ cannam@89: ush code; /* bit string */ cannam@89: } fc; cannam@89: union { cannam@89: ush dad; /* father node in Huffman tree */ cannam@89: ush len; /* length of bit string */ cannam@89: } dl; cannam@89: } FAR ct_data; cannam@89: cannam@89: #define Freq fc.freq cannam@89: #define Code fc.code cannam@89: #define Dad dl.dad cannam@89: #define Len dl.len cannam@89: cannam@89: typedef struct static_tree_desc_s static_tree_desc; cannam@89: cannam@89: typedef struct tree_desc_s { cannam@89: ct_data *dyn_tree; /* the dynamic tree */ cannam@89: int max_code; /* largest code with non zero frequency */ cannam@89: static_tree_desc *stat_desc; /* the corresponding static tree */ cannam@89: } FAR tree_desc; cannam@89: cannam@89: typedef ush Pos; cannam@89: typedef Pos FAR Posf; cannam@89: typedef unsigned IPos; cannam@89: cannam@89: /* A Pos is an index in the character window. We use short instead of int to cannam@89: * save space in the various tables. IPos is used only for parameter passing. cannam@89: */ cannam@89: cannam@89: typedef struct internal_state { cannam@89: z_streamp strm; /* pointer back to this zlib stream */ cannam@89: int status; /* as the name implies */ cannam@89: Bytef *pending_buf; /* output still pending */ cannam@89: ulg pending_buf_size; /* size of pending_buf */ cannam@89: Bytef *pending_out; /* next pending byte to output to the stream */ cannam@89: uInt pending; /* nb of bytes in the pending buffer */ cannam@89: int wrap; /* bit 0 true for zlib, bit 1 true for gzip */ cannam@89: gz_headerp gzhead; /* gzip header information to write */ cannam@89: uInt gzindex; /* where in extra, name, or comment */ cannam@89: Byte method; /* STORED (for zip only) or DEFLATED */ cannam@89: int last_flush; /* value of flush param for previous deflate call */ cannam@89: cannam@89: /* used by deflate.c: */ cannam@89: cannam@89: uInt w_size; /* LZ77 window size (32K by default) */ cannam@89: uInt w_bits; /* log2(w_size) (8..16) */ cannam@89: uInt w_mask; /* w_size - 1 */ cannam@89: cannam@89: Bytef *window; cannam@89: /* Sliding window. Input bytes are read into the second half of the window, cannam@89: * and move to the first half later to keep a dictionary of at least wSize cannam@89: * bytes. With this organization, matches are limited to a distance of cannam@89: * wSize-MAX_MATCH bytes, but this ensures that IO is always cannam@89: * performed with a length multiple of the block size. Also, it limits cannam@89: * the window size to 64K, which is quite useful on MSDOS. cannam@89: * To do: use the user input buffer as sliding window. cannam@89: */ cannam@89: cannam@89: ulg window_size; cannam@89: /* Actual size of window: 2*wSize, except when the user input buffer cannam@89: * is directly used as sliding window. cannam@89: */ cannam@89: cannam@89: Posf *prev; cannam@89: /* Link to older string with same hash index. To limit the size of this cannam@89: * array to 64K, this link is maintained only for the last 32K strings. cannam@89: * An index in this array is thus a window index modulo 32K. cannam@89: */ cannam@89: cannam@89: Posf *head; /* Heads of the hash chains or NIL. */ cannam@89: cannam@89: uInt ins_h; /* hash index of string to be inserted */ cannam@89: uInt hash_size; /* number of elements in hash table */ cannam@89: uInt hash_bits; /* log2(hash_size) */ cannam@89: uInt hash_mask; /* hash_size-1 */ cannam@89: cannam@89: uInt hash_shift; cannam@89: /* Number of bits by which ins_h must be shifted at each input cannam@89: * step. It must be such that after MIN_MATCH steps, the oldest cannam@89: * byte no longer takes part in the hash key, that is: cannam@89: * hash_shift * MIN_MATCH >= hash_bits cannam@89: */ cannam@89: cannam@89: long block_start; cannam@89: /* Window position at the beginning of the current output block. Gets cannam@89: * negative when the window is moved backwards. cannam@89: */ cannam@89: cannam@89: uInt match_length; /* length of best match */ cannam@89: IPos prev_match; /* previous match */ cannam@89: int match_available; /* set if previous match exists */ cannam@89: uInt strstart; /* start of string to insert */ cannam@89: uInt match_start; /* start of matching string */ cannam@89: uInt lookahead; /* number of valid bytes ahead in window */ cannam@89: cannam@89: uInt prev_length; cannam@89: /* Length of the best match at previous step. Matches not greater than this cannam@89: * are discarded. This is used in the lazy match evaluation. cannam@89: */ cannam@89: cannam@89: uInt max_chain_length; cannam@89: /* To speed up deflation, hash chains are never searched beyond this cannam@89: * length. A higher limit improves compression ratio but degrades the cannam@89: * speed. cannam@89: */ cannam@89: cannam@89: uInt max_lazy_match; cannam@89: /* Attempt to find a better match only when the current match is strictly cannam@89: * smaller than this value. This mechanism is used only for compression cannam@89: * levels >= 4. cannam@89: */ cannam@89: # define max_insert_length max_lazy_match cannam@89: /* Insert new strings in the hash table only if the match length is not cannam@89: * greater than this length. This saves time but degrades compression. cannam@89: * max_insert_length is used only for compression levels <= 3. cannam@89: */ cannam@89: cannam@89: int level; /* compression level (1..9) */ cannam@89: int strategy; /* favor or force Huffman coding*/ cannam@89: cannam@89: uInt good_match; cannam@89: /* Use a faster search when the previous match is longer than this */ cannam@89: cannam@89: int nice_match; /* Stop searching when current match exceeds this */ cannam@89: cannam@89: /* used by trees.c: */ cannam@89: /* Didn't use ct_data typedef below to suppress compiler warning */ cannam@89: struct ct_data_s dyn_ltree[HEAP_SIZE]; /* literal and length tree */ cannam@89: struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */ cannam@89: struct ct_data_s bl_tree[2*BL_CODES+1]; /* Huffman tree for bit lengths */ cannam@89: cannam@89: struct tree_desc_s l_desc; /* desc. for literal tree */ cannam@89: struct tree_desc_s d_desc; /* desc. for distance tree */ cannam@89: struct tree_desc_s bl_desc; /* desc. for bit length tree */ cannam@89: cannam@89: ush bl_count[MAX_BITS+1]; cannam@89: /* number of codes at each bit length for an optimal tree */ cannam@89: cannam@89: int heap[2*L_CODES+1]; /* heap used to build the Huffman trees */ cannam@89: int heap_len; /* number of elements in the heap */ cannam@89: int heap_max; /* element of largest frequency */ cannam@89: /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used. cannam@89: * The same heap array is used to build all trees. cannam@89: */ cannam@89: cannam@89: uch depth[2*L_CODES+1]; cannam@89: /* Depth of each subtree used as tie breaker for trees of equal frequency cannam@89: */ cannam@89: cannam@89: uchf *l_buf; /* buffer for literals or lengths */ cannam@89: cannam@89: uInt lit_bufsize; cannam@89: /* Size of match buffer for literals/lengths. There are 4 reasons for cannam@89: * limiting lit_bufsize to 64K: cannam@89: * - frequencies can be kept in 16 bit counters cannam@89: * - if compression is not successful for the first block, all input cannam@89: * data is still in the window so we can still emit a stored block even cannam@89: * when input comes from standard input. (This can also be done for cannam@89: * all blocks if lit_bufsize is not greater than 32K.) cannam@89: * - if compression is not successful for a file smaller than 64K, we can cannam@89: * even emit a stored file instead of a stored block (saving 5 bytes). cannam@89: * This is applicable only for zip (not gzip or zlib). cannam@89: * - creating new Huffman trees less frequently may not provide fast cannam@89: * adaptation to changes in the input data statistics. (Take for cannam@89: * example a binary file with poorly compressible code followed by cannam@89: * a highly compressible string table.) Smaller buffer sizes give cannam@89: * fast adaptation but have of course the overhead of transmitting cannam@89: * trees more frequently. cannam@89: * - I can't count above 4 cannam@89: */ cannam@89: cannam@89: uInt last_lit; /* running index in l_buf */ cannam@89: cannam@89: ushf *d_buf; cannam@89: /* Buffer for distances. To simplify the code, d_buf and l_buf have cannam@89: * the same number of elements. To use different lengths, an extra flag cannam@89: * array would be necessary. cannam@89: */ cannam@89: cannam@89: ulg opt_len; /* bit length of current block with optimal trees */ cannam@89: ulg static_len; /* bit length of current block with static trees */ cannam@89: uInt matches; /* number of string matches in current block */ cannam@89: uInt insert; /* bytes at end of window left to insert */ cannam@89: cannam@89: #ifdef DEBUG cannam@89: ulg compressed_len; /* total bit length of compressed file mod 2^32 */ cannam@89: ulg bits_sent; /* bit length of compressed data sent mod 2^32 */ cannam@89: #endif cannam@89: cannam@89: ush bi_buf; cannam@89: /* Output buffer. bits are inserted starting at the bottom (least cannam@89: * significant bits). cannam@89: */ cannam@89: int bi_valid; cannam@89: /* Number of valid bits in bi_buf. All bits above the last valid bit cannam@89: * are always zero. cannam@89: */ cannam@89: cannam@89: ulg high_water; cannam@89: /* High water mark offset in window for initialized bytes -- bytes above cannam@89: * this are set to zero in order to avoid memory check warnings when cannam@89: * longest match routines access bytes past the input. This is then cannam@89: * updated to the new high water mark. cannam@89: */ cannam@89: cannam@89: } FAR deflate_state; cannam@89: cannam@89: /* Output a byte on the stream. cannam@89: * IN assertion: there is enough room in pending_buf. cannam@89: */ cannam@89: #define put_byte(s, c) {s->pending_buf[s->pending++] = (c);} cannam@89: cannam@89: cannam@89: #define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1) cannam@89: /* Minimum amount of lookahead, except at the end of the input file. cannam@89: * See deflate.c for comments about the MIN_MATCH+1. cannam@89: */ cannam@89: cannam@89: #define MAX_DIST(s) ((s)->w_size-MIN_LOOKAHEAD) cannam@89: /* In order to simplify the code, particularly on 16 bit machines, match cannam@89: * distances are limited to MAX_DIST instead of WSIZE. cannam@89: */ cannam@89: cannam@89: #define WIN_INIT MAX_MATCH cannam@89: /* Number of bytes after end of data in window to initialize in order to avoid cannam@89: memory checker errors from longest match routines */ cannam@89: cannam@89: /* in trees.c */ cannam@89: void ZLIB_INTERNAL _tr_init OF((deflate_state *s)); cannam@89: int ZLIB_INTERNAL _tr_tally OF((deflate_state *s, unsigned dist, unsigned lc)); cannam@89: void ZLIB_INTERNAL _tr_flush_block OF((deflate_state *s, charf *buf, cannam@89: ulg stored_len, int last)); cannam@89: void ZLIB_INTERNAL _tr_flush_bits OF((deflate_state *s)); cannam@89: void ZLIB_INTERNAL _tr_align OF((deflate_state *s)); cannam@89: void ZLIB_INTERNAL _tr_stored_block OF((deflate_state *s, charf *buf, cannam@89: ulg stored_len, int last)); cannam@89: cannam@89: #define d_code(dist) \ cannam@89: ((dist) < 256 ? _dist_code[dist] : _dist_code[256+((dist)>>7)]) cannam@89: /* Mapping from a distance to a distance code. dist is the distance - 1 and cannam@89: * must not have side effects. _dist_code[256] and _dist_code[257] are never cannam@89: * used. cannam@89: */ cannam@89: cannam@89: #ifndef DEBUG cannam@89: /* Inline versions of _tr_tally for speed: */ cannam@89: cannam@89: #if defined(GEN_TREES_H) || !defined(STDC) cannam@89: extern uch ZLIB_INTERNAL _length_code[]; cannam@89: extern uch ZLIB_INTERNAL _dist_code[]; cannam@89: #else cannam@89: extern const uch ZLIB_INTERNAL _length_code[]; cannam@89: extern const uch ZLIB_INTERNAL _dist_code[]; cannam@89: #endif cannam@89: cannam@89: # define _tr_tally_lit(s, c, flush) \ cannam@89: { uch cc = (c); \ cannam@89: s->d_buf[s->last_lit] = 0; \ cannam@89: s->l_buf[s->last_lit++] = cc; \ cannam@89: s->dyn_ltree[cc].Freq++; \ cannam@89: flush = (s->last_lit == s->lit_bufsize-1); \ cannam@89: } cannam@89: # define _tr_tally_dist(s, distance, length, flush) \ cannam@89: { uch len = (length); \ cannam@89: ush dist = (distance); \ cannam@89: s->d_buf[s->last_lit] = dist; \ cannam@89: s->l_buf[s->last_lit++] = len; \ cannam@89: dist--; \ cannam@89: s->dyn_ltree[_length_code[len]+LITERALS+1].Freq++; \ cannam@89: s->dyn_dtree[d_code(dist)].Freq++; \ cannam@89: flush = (s->last_lit == s->lit_bufsize-1); \ cannam@89: } cannam@89: #else cannam@89: # define _tr_tally_lit(s, c, flush) flush = _tr_tally(s, 0, c) cannam@89: # define _tr_tally_dist(s, distance, length, flush) \ cannam@89: flush = _tr_tally(s, distance, length) cannam@89: #endif cannam@89: cannam@89: #endif /* DEFLATE_H */