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