- Notifications
You must be signed in to change notification settings - Fork 5.8k
/
Copy pathdeflate.h
401 lines (336 loc) · 14.8 KB
/
deflate.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* deflate.h -- internal compression state
* Copyright (C) 1995-2024 Jean-loup Gailly
* For conditions of distribution and use, see copyright notice in zlib.h
*/
/* WARNING: this file should *not* be used by applications. It is
part of the implementation of the compression library and is
subject to change. Applications should only use zlib.h.
*/
/* @(#) $Id$ */
#ifndefDEFLATE_H
#defineDEFLATE_H
#include"zutil.h"
/* define NO_GZIP when compiling if you want to disable gzip header and
trailer creation by deflate(). NO_GZIP would be used to avoid linking in
the crc code when it is not needed. For shared libraries, gzip encoding
should be left enabled. */
#ifndefNO_GZIP
# defineGZIP
#endif
/* define LIT_MEM to slightly increase the speed of deflate (order 1% to 2%) at
the cost of a larger memory footprint */
/* #define LIT_MEM */
/* ===========================================================================
* Internal compression state.
*/
#defineLENGTH_CODES 29
/* number of length codes, not counting the special END_BLOCK code */
#defineLITERALS 256
/* number of literal bytes 0..255 */
#defineL_CODES (LITERALS+1+LENGTH_CODES)
/* number of Literal or Length codes, including the END_BLOCK code */
#defineD_CODES 30
/* number of distance codes */
#defineBL_CODES 19
/* number of codes used to transfer the bit lengths */
#defineHEAP_SIZE (2*L_CODES+1)
/* maximum heap size */
#defineMAX_BITS 15
/* All codes must not exceed MAX_BITS bits */
#defineBuf_size 16
/* size of bit buffer in bi_buf */
#defineINIT_STATE 42 /* zlib header -> BUSY_STATE */
#ifdefGZIP
# defineGZIP_STATE 57 /* gzip header -> BUSY_STATE | EXTRA_STATE */
#endif
#defineEXTRA_STATE 69 /* gzip extra block -> NAME_STATE */
#defineNAME_STATE 73 /* gzip file name -> COMMENT_STATE */
#defineCOMMENT_STATE 91 /* gzip comment -> HCRC_STATE */
#defineHCRC_STATE 103 /* gzip header CRC -> BUSY_STATE */
#defineBUSY_STATE 113 /* deflate -> FINISH_STATE */
#defineFINISH_STATE 666 /* stream complete */
/* Stream status */
/* Data structure describing a single value and its code string. */
typedefstructct_data_s {
union {
ushfreq; /* frequency count */
ushcode; /* bit string */
} fc;
union {
ushdad; /* father node in Huffman tree */
ushlen; /* length of bit string */
} dl;
} FARct_data;
#defineFreq fc.freq
#defineCode fc.code
#defineDad dl.dad
#defineLen dl.len
typedefstructstatic_tree_desc_sstatic_tree_desc;
typedefstructtree_desc_s {
ct_data*dyn_tree; /* the dynamic tree */
intmax_code; /* largest code with non zero frequency */
conststatic_tree_desc*stat_desc; /* the corresponding static tree */
} FARtree_desc;
typedefushPos;
typedefPosFARPosf;
typedefunsignedIPos;
/* A Pos is an index in the character window. We use short instead of int to
* save space in the various tables. IPos is used only for parameter passing.
*/
typedefstructinternal_state {
z_streampstrm; /* pointer back to this zlib stream */
intstatus; /* as the name implies */
Bytef*pending_buf; /* output still pending */
ulgpending_buf_size; /* size of pending_buf */
Bytef*pending_out; /* next pending byte to output to the stream */
ulgpending; /* nb of bytes in the pending buffer */
intwrap; /* bit 0 true for zlib, bit 1 true for gzip */
gz_headerpgzhead; /* gzip header information to write */
ulggzindex; /* where in extra, name, or comment */
Bytemethod; /* can only be DEFLATED */
intlast_flush; /* value of flush param for previous deflate call */
/* used by deflate.c: */
uIntw_size; /* LZ77 window size (32K by default) */
uIntw_bits; /* log2(w_size) (8..16) */
uIntw_mask; /* w_size - 1 */
Bytef*window;
/* Sliding window. Input bytes are read into the second half of the window,
* and move to the first half later to keep a dictionary of at least wSize
* bytes. With this organization, matches are limited to a distance of
* wSize-MAX_MATCH bytes, but this ensures that IO is always
* performed with a length multiple of the block size. Also, it limits
* the window size to 64K, which is quite useful on MSDOS.
* To do: use the user input buffer as sliding window.
*/
ulgwindow_size;
/* Actual size of window: 2*wSize, except when the user input buffer
* is directly used as sliding window.
*/
Posf*prev;
/* Link to older string with same hash index. To limit the size of this
* array to 64K, this link is maintained only for the last 32K strings.
* An index in this array is thus a window index modulo 32K.
*/
Posf*head; /* Heads of the hash chains or NIL. */
uIntins_h; /* hash index of string to be inserted */
uInthash_size; /* number of elements in hash table */
uInthash_bits; /* log2(hash_size) */
uInthash_mask; /* hash_size-1 */
uInthash_shift;
/* Number of bits by which ins_h must be shifted at each input
* step. It must be such that after MIN_MATCH steps, the oldest
* byte no longer takes part in the hash key, that is:
* hash_shift * MIN_MATCH >= hash_bits
*/
longblock_start;
/* Window position at the beginning of the current output block. Gets
* negative when the window is moved backwards.
*/
uIntmatch_length; /* length of best match */
IPosprev_match; /* previous match */
intmatch_available; /* set if previous match exists */
uIntstrstart; /* start of string to insert */
uIntmatch_start; /* start of matching string */
uIntlookahead; /* number of valid bytes ahead in window */
uIntprev_length;
/* Length of the best match at previous step. Matches not greater than this
* are discarded. This is used in the lazy match evaluation.
*/
uIntmax_chain_length;
/* To speed up deflation, hash chains are never searched beyond this
* length. A higher limit improves compression ratio but degrades the
* speed.
*/
uIntmax_lazy_match;
/* Attempt to find a better match only when the current match is strictly
* smaller than this value. This mechanism is used only for compression
* levels >= 4.
*/
# definemax_insert_length max_lazy_match
/* Insert new strings in the hash table only if the match length is not
* greater than this length. This saves time but degrades compression.
* max_insert_length is used only for compression levels <= 3.
*/
intlevel; /* compression level (1..9) */
intstrategy; /* favor or force Huffman coding*/
uIntgood_match;
/* Use a faster search when the previous match is longer than this */
intnice_match; /* Stop searching when current match exceeds this */
/* used by trees.c: */
/* Didn't use ct_data typedef below to suppress compiler warning */
structct_data_sdyn_ltree[HEAP_SIZE]; /* literal and length tree */
structct_data_sdyn_dtree[2*D_CODES+1]; /* distance tree */
structct_data_sbl_tree[2*BL_CODES+1]; /* Huffman tree for bit lengths */
structtree_desc_sl_desc; /* desc. for literal tree */
structtree_desc_sd_desc; /* desc. for distance tree */
structtree_desc_sbl_desc; /* desc. for bit length tree */
ushbl_count[MAX_BITS+1];
/* number of codes at each bit length for an optimal tree */
intheap[2*L_CODES+1]; /* heap used to build the Huffman trees */
intheap_len; /* number of elements in the heap */
intheap_max; /* element of largest frequency */
/* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.
* The same heap array is used to build all trees.
*/
uchdepth[2*L_CODES+1];
/* Depth of each subtree used as tie breaker for trees of equal frequency
*/
#ifdefLIT_MEM
# defineLIT_BUFS 5
ushf*d_buf; /* buffer for distances */
uchf*l_buf; /* buffer for literals/lengths */
#else
# defineLIT_BUFS 4
uchf*sym_buf; /* buffer for distances and literals/lengths */
#endif
uIntlit_bufsize;
/* Size of match buffer for literals/lengths. There are 4 reasons for
* limiting lit_bufsize to 64K:
* - frequencies can be kept in 16 bit counters
* - if compression is not successful for the first block, all input
* data is still in the window so we can still emit a stored block even
* when input comes from standard input. (This can also be done for
* all blocks if lit_bufsize is not greater than 32K.)
* - if compression is not successful for a file smaller than 64K, we can
* even emit a stored file instead of a stored block (saving 5 bytes).
* This is applicable only for zip (not gzip or zlib).
* - creating new Huffman trees less frequently may not provide fast
* adaptation to changes in the input data statistics. (Take for
* example a binary file with poorly compressible code followed by
* a highly compressible string table.) Smaller buffer sizes give
* fast adaptation but have of course the overhead of transmitting
* trees more frequently.
* - I can't count above 4
*/
uIntsym_next; /* running index in symbol buffer */
uIntsym_end; /* symbol table full when sym_next reaches this */
ulgopt_len; /* bit length of current block with optimal trees */
ulgstatic_len; /* bit length of current block with static trees */
uIntmatches; /* number of string matches in current block */
uIntinsert; /* bytes at end of window left to insert */
#ifdefZLIB_DEBUG
ulgcompressed_len; /* total bit length of compressed file mod 2^32 */
ulgbits_sent; /* bit length of compressed data sent mod 2^32 */
#endif
ushbi_buf;
/* Output buffer. bits are inserted starting at the bottom (least
* significant bits).
*/
intbi_valid;
/* Number of valid bits in bi_buf. All bits above the last valid bit
* are always zero.
*/
ulghigh_water;
/* High water mark offset in window for initialized bytes -- bytes above
* this are set to zero in order to avoid memory check warnings when
* longest match routines access bytes past the input. This is then
* updated to the new high water mark.
*/
} FARdeflate_state;
/* Output a byte on the stream.
* IN assertion: there is enough room in pending_buf.
*/
#defineput_byte(s, c) {s->pending_buf[s->pending++] = (Bytef)(c);}
#defineMIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
/* Minimum amount of lookahead, except at the end of the input file.
* See deflate.c for comments about the MIN_MATCH+1.
*/
#defineMAX_DIST(s) ((s)->w_size-MIN_LOOKAHEAD)
/* In order to simplify the code, particularly on 16 bit machines, match
* distances are limited to MAX_DIST instead of WSIZE.
*/
#defineWIN_INIT MAX_MATCH
/* Number of bytes after end of data in window to initialize in order to avoid
memory checker errors from longest match routines */
/* in trees.c */
voidZLIB_INTERNAL_tr_init(deflate_state*s);
intZLIB_INTERNAL_tr_tally(deflate_state*s, unsigneddist, unsignedlc);
voidZLIB_INTERNAL_tr_flush_block(deflate_state*s, charf*buf,
ulgstored_len, intlast);
voidZLIB_INTERNAL_tr_flush_bits(deflate_state*s);
voidZLIB_INTERNAL_tr_align(deflate_state*s);
voidZLIB_INTERNAL_tr_stored_block(deflate_state*s, charf*buf,
ulgstored_len, intlast);
#defined_code(dist) \
((dist) < 256 ? _dist_code[dist] : _dist_code[256+((dist)>>7)])
/* Mapping from a distance to a distance code. dist is the distance - 1 and
* must not have side effects. _dist_code[256] and _dist_code[257] are never
* used.
*/
#ifndefZLIB_DEBUG
/* Inline versions of _tr_tally for speed: */
#if defined(GEN_TREES_H) || !defined(STDC)
externuchZLIB_INTERNAL_length_code[];
externuchZLIB_INTERNAL_dist_code[];
#else
externconstuchZLIB_INTERNAL_length_code[];
externconstuchZLIB_INTERNAL_dist_code[];
#endif
#ifdefLIT_MEM
# define_tr_tally_lit(s, c, flush) \
{ uch cc = (c); \
s->d_buf[s->sym_next] = 0; \
s->l_buf[s->sym_next++] = cc; \
s->dyn_ltree[cc].Freq++; \
flush = (s->sym_next == s->sym_end); \
}
# define_tr_tally_dist(s, distance, length, flush) \
{ uch len = (uch)(length); \
ush dist = (ush)(distance); \
s->d_buf[s->sym_next] = dist; \
s->l_buf[s->sym_next++] = len; \
dist--; \
s->dyn_ltree[_length_code[len]+LITERALS+1].Freq++; \
s->dyn_dtree[d_code(dist)].Freq++; \
flush = (s->sym_next == s->sym_end); \
}
#else
# define_tr_tally_lit(s, c, flush) \
{ uch cc = (c); \
s->sym_buf[s->sym_next++] = 0; \
s->sym_buf[s->sym_next++] = 0; \
s->sym_buf[s->sym_next++] = cc; \
s->dyn_ltree[cc].Freq++; \
flush = (s->sym_next == s->sym_end); \
}
# define_tr_tally_dist(s, distance, length, flush) \
{ uch len = (uch)(length); \
ush dist = (ush)(distance); \
s->sym_buf[s->sym_next++] = (uch)dist; \
s->sym_buf[s->sym_next++] = (uch)(dist >> 8); \
s->sym_buf[s->sym_next++] = len; \
dist--; \
s->dyn_ltree[_length_code[len]+LITERALS+1].Freq++; \
s->dyn_dtree[d_code(dist)].Freq++; \
flush = (s->sym_next == s->sym_end); \
}
#endif
#else
# define_tr_tally_lit(s, c, flush) flush = _tr_tally(s, 0, c)
# define_tr_tally_dist(s, distance, length, flush) \
flush = _tr_tally(s, distance, length)
#endif
#endif/* DEFLATE_H */