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