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