Debian Codegraph @

src/lib/deflate.h

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