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