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