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