ZLIB
Loading...
Searching...
No Matches
deflate.h
Go to the documentation of this file.
1
12/* @(#) $Id$ */
13
14#ifndef DEFLATE_H
15#define DEFLATE_H
16
17#include "zutil.h"
18
19/* define NO_GZIP when compiling if you want to disable gzip header and
20 trailer creation by deflate(). NO_GZIP would be used to avoid linking in
21 the crc code when it is not needed. For shared libraries, gzip encoding
22 should be left enabled. */
23#ifndef NO_GZIP
24# define GZIP
25#endif
26
27/* define LIT_MEM to slightly increase the speed of deflate (order 1% to 2%) at
28 the cost of a larger memory footprint */
29/* #define LIT_MEM */
30
31/* ===========================================================================
32 * Internal compression state.
33 */
34
35#define LENGTH_CODES 29
36/* number of length codes, not counting the special END_BLOCK code */
37
38#define LITERALS 256
39/* number of literal bytes 0..255 */
40
41#define L_CODES (LITERALS+1+LENGTH_CODES)
42/* number of Literal or Length codes, including the END_BLOCK code */
43
44#define D_CODES 30
45/* number of distance codes */
46
47#define BL_CODES 19
48/* number of codes used to transfer the bit lengths */
49
50#define HEAP_SIZE (2*L_CODES+1)
51/* maximum heap size */
52
53#define MAX_BITS 15
54/* All codes must not exceed MAX_BITS bits */
55
56#define Buf_size 16
57/* size of bit buffer in bi_buf */
58
59#define INIT_STATE 42 /* zlib header -> BUSY_STATE */
60#ifdef GZIP
61# define GZIP_STATE 57 /* gzip header -> BUSY_STATE | EXTRA_STATE */
62#endif
63#define EXTRA_STATE 69 /* gzip extra block -> NAME_STATE */
64#define NAME_STATE 73 /* gzip file name -> COMMENT_STATE */
65#define COMMENT_STATE 91 /* gzip comment -> HCRC_STATE */
66#define HCRC_STATE 103 /* gzip header CRC -> BUSY_STATE */
67#define BUSY_STATE 113 /* deflate -> FINISH_STATE */
68#define FINISH_STATE 666 /* stream complete */
69/* Stream status */
70
71
73typedef struct ct_data_s {
74 union {
75 ush freq;
76 ush code;
77 } fc;
78 union {
79 ush dad;
80 ush len;
81 } dl;
82} FAR ct_data;
83
84#define Freq fc.freq
85#define Code fc.code
86#define Dad dl.dad
87#define Len dl.len
88
89typedef struct static_tree_desc_s static_tree_desc;
90
91typedef struct tree_desc_s {
92 ct_data *dyn_tree; /* the dynamic tree */
93 int max_code; /* largest code with non zero frequency */
94 const static_tree_desc *stat_desc; /* the corresponding static tree */
95} FAR tree_desc;
96
97typedef ush Pos;
98typedef Pos FAR Posf;
99typedef unsigned IPos;
100
101/* A Pos is an index in the character window. We use short instead of int to
102 * save space in the various tables. IPos is used only for parameter passing.
103 */
104
106typedef struct internal_state {
108 int status;
109 Bytef *pending_buf;
111 Bytef *pending_out;
113 int wrap;
116 Byte method;
121 uInt w_size;
122 uInt w_bits;
123 uInt w_mask;
125 Bytef *window;
139 Posf *prev;
144 Posf *head;
146 uInt ins_h;
163 uInt strstart;
183# define max_insert_length max_lazy_match
189 int level;
197
199 /* Didn't use ct_data typedef below to suppress compiler warning */
200 struct ct_data_s dyn_ltree[HEAP_SIZE];
201 struct ct_data_s dyn_dtree[2*D_CODES+1];
202 struct ct_data_s bl_tree[2*BL_CODES+1];
204 struct tree_desc_s l_desc;
205 struct tree_desc_s d_desc;
206 struct tree_desc_s bl_desc;
208 ush bl_count[MAX_BITS+1];
211 int heap[2*L_CODES+1];
214 /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.
215 * The same heap array is used to build all trees.
216 */
217
218 uch depth[2*L_CODES+1];
222#ifdef LIT_MEM
223 ushf *d_buf;
224 uchf *l_buf;
225#else
226 uchf *sym_buf;
227#endif
228
250 uInt sym_next;
251 uInt sym_end;
255 uInt matches;
256 uInt insert;
258#ifdef ZLIB_DEBUG
259 ulg compressed_len;
260 ulg bits_sent;
261#endif
262
279} FAR deflate_state;
280
281/* Output a byte on the stream.
282 * IN assertion: there is enough room in pending_buf.
283 */
284#define put_byte(s, c) {s->pending_buf[s->pending++] = (Bytef)(c);}
285
286
287#define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
288/* Minimum amount of lookahead, except at the end of the input file.
289 * See deflate.c for comments about the MIN_MATCH+1.
290 */
291
292#define MAX_DIST(s) ((s)->w_size-MIN_LOOKAHEAD)
293/* In order to simplify the code, particularly on 16 bit machines, match
294 * distances are limited to MAX_DIST instead of WSIZE.
295 */
296
297#define WIN_INIT MAX_MATCH
298/* Number of bytes after end of data in window to initialize in order to avoid
299 memory checker errors from longest match routines */
300
301 /* in trees.c */
302void ZLIB_INTERNAL _tr_init(deflate_state *s);
303int ZLIB_INTERNAL _tr_tally(deflate_state *s, unsigned dist, unsigned lc);
304void ZLIB_INTERNAL _tr_flush_block(deflate_state *s, charf *buf,
305 ulg stored_len, int last);
306void ZLIB_INTERNAL _tr_flush_bits(deflate_state *s);
307void ZLIB_INTERNAL _tr_align(deflate_state *s);
308void ZLIB_INTERNAL _tr_stored_block(deflate_state *s, charf *buf,
309 ulg stored_len, int last);
310
311#define d_code(dist) \
312 ((dist) < 256 ? _dist_code[dist] : _dist_code[256+((dist)>>7)])
313/* Mapping from a distance to a distance code. dist is the distance - 1 and
314 * must not have side effects. _dist_code[256] and _dist_code[257] are never
315 * used.
316 */
317
318#ifndef ZLIB_DEBUG
319/* Inline versions of _tr_tally for speed: */
320
321#if defined(GEN_TREES_H) || !defined(STDC)
322 extern uch ZLIB_INTERNAL _length_code[];
323 extern uch ZLIB_INTERNAL _dist_code[];
324#else
325 extern const uch ZLIB_INTERNAL _length_code[];
326 extern const uch ZLIB_INTERNAL _dist_code[];
327#endif
328
329#ifdef LIT_MEM
330# define _tr_tally_lit(s, c, flush) \
331 { uch cc = (c); \
332 s->d_buf[s->sym_next] = 0; \
333 s->l_buf[s->sym_next++] = cc; \
334 s->dyn_ltree[cc].Freq++; \
335 flush = (s->sym_next == s->sym_end); \
336 }
337# define _tr_tally_dist(s, distance, length, flush) \
338 { uch len = (uch)(length); \
339 ush dist = (ush)(distance); \
340 s->d_buf[s->sym_next] = dist; \
341 s->l_buf[s->sym_next++] = len; \
342 dist--; \
343 s->dyn_ltree[_length_code[len]+LITERALS+1].Freq++; \
344 s->dyn_dtree[d_code(dist)].Freq++; \
345 flush = (s->sym_next == s->sym_end); \
346 }
347#else
348# define _tr_tally_lit(s, c, flush) \
349 { uch cc = (c); \
350 s->sym_buf[s->sym_next++] = 0; \
351 s->sym_buf[s->sym_next++] = 0; \
352 s->sym_buf[s->sym_next++] = cc; \
353 s->dyn_ltree[cc].Freq++; \
354 flush = (s->sym_next == s->sym_end); \
355 }
356# define _tr_tally_dist(s, distance, length, flush) \
357 { uch len = (uch)(length); \
358 ush dist = (ush)(distance); \
359 s->sym_buf[s->sym_next++] = (uch)dist; \
360 s->sym_buf[s->sym_next++] = (uch)(dist >> 8); \
361 s->sym_buf[s->sym_next++] = len; \
362 dist--; \
363 s->dyn_ltree[_length_code[len]+LITERALS+1].Freq++; \
364 s->dyn_dtree[d_code(dist)].Freq++; \
365 flush = (s->sym_next == s->sym_end); \
366 }
367#endif
368#else
369# define _tr_tally_lit(s, c, flush) flush = _tr_tally(s, 0, c)
370# define _tr_tally_dist(s, distance, length, flush) \
371 flush = _tr_tally(s, distance, length)
372#endif
373
374#endif /* DEFLATE_H */
Data structure describing a single value and its code string.
Definition deflate.h:73
ush len
length of bit string
Definition deflate.h:80
ush freq
frequency count
Definition deflate.h:75
ush dad
father node in Huffman tree
Definition deflate.h:79
ush code
bit string
Definition deflate.h:76
Deflate internal state.
Definition deflate.h:106
int last_flush
value of flush param for previous deflate call
Definition deflate.h:117
Posf * head
Heads of the hash chains or NIL.
Definition deflate.h:144
uInt prev_length
Length of the best match at previous step.
Definition deflate.h:167
int heap_len
number of elements in the heap
Definition deflate.h:212
uInt sym_end
symbol table full when sym_next reaches this
Definition deflate.h:251
Bytef * pending_buf
output still pending
Definition deflate.h:109
uInt w_size
LZ77 window size (32K by default)
Definition deflate.h:121
int level
compression level (1..9)
Definition deflate.h:189
uInt w_bits
log2(w_size) (8..16)
Definition deflate.h:122
uInt hash_mask
hash_size-1
Definition deflate.h:149
Bytef * window
Sliding window.
Definition deflate.h:125
ulg pending
nb of bytes in the pending buffer
Definition deflate.h:112
uInt match_length
length of best match
Definition deflate.h:160
uInt max_lazy_match
Attempt to find a better match only when the current match is strictly smaller than this value.
Definition deflate.h:178
uInt lookahead
number of valid bytes ahead in window
Definition deflate.h:165
IPos prev_match
previous match
Definition deflate.h:161
uInt insert
bytes at end of window left to insert
Definition deflate.h:256
uInt strstart
start of string to insert
Definition deflate.h:163
uInt matches
number of string matches in current block
Definition deflate.h:255
Bytef * pending_out
next pending byte to output to the stream
Definition deflate.h:111
int status
as the name implies
Definition deflate.h:108
z_streamp strm
pointer back to this zlib stream
Definition deflate.h:107
uInt sym_next
running index in symbol buffer
Definition deflate.h:250
ulg opt_len
bit length of current block with optimal trees
Definition deflate.h:253
Posf * prev
Link to older string with same hash index.
Definition deflate.h:139
uInt hash_bits
log2(hash_size)
Definition deflate.h:148
uInt match_start
start of matching string
Definition deflate.h:164
uchf * sym_buf
buffer for distances and literals/lengths
Definition deflate.h:226
int bi_valid
Number of valid bits in bi_buf.
Definition deflate.h:267
int match_available
set if previous match exists
Definition deflate.h:162
ulg window_size
Actual size of window: 2*wSize, except when the user input buffer is directly used as sliding window.
Definition deflate.h:136
uInt w_mask
w_size - 1
Definition deflate.h:123
uInt hash_size
number of elements in hash table
Definition deflate.h:147
int heap_max
element of largest frequency
Definition deflate.h:213
ulg static_len
bit length of current block with static trees
Definition deflate.h:254
int wrap
bit 0 true for zlib, bit 1 true for gzip
Definition deflate.h:113
ulg gzindex
where in extra, name, or comment
Definition deflate.h:115
ush bi_buf
Output buffer.
Definition deflate.h:263
uInt max_chain_length
To speed up deflation, hash chains are never searched beyond this length.
Definition deflate.h:172
uInt hash_shift
Number of bits by which ins_h must be shifted at each input step.
Definition deflate.h:151
ulg pending_buf_size
size of pending_buf
Definition deflate.h:110
uInt ins_h
hash index of string to be inserted
Definition deflate.h:146
Byte method
can only be DEFLATED
Definition deflate.h:116
int nice_match
Stop searching when current match exceeds this.
Definition deflate.h:195
long block_start
Window position at the beginning of the current output block.
Definition deflate.h:156
uInt lit_bufsize
Size of match buffer for literals/lengths.
Definition deflate.h:229
gz_headerp gzhead
gzip header information to write
Definition deflate.h:114
ulg high_water
High water mark offset in window for initialized bytes – bytes above this are set to zero in order to...
Definition deflate.h:272
int strategy
favor or force Huffman coding
Definition deflate.h:190
uInt good_match
Use a faster search when the previous match is longer than this.
Definition deflate.h:192
gzip header information passed to and from zlib routines.
Definition zlib.h:164
Definition trees.c:119
Definition deflate.h:91
Compressed stream state information.
Definition zlib.h:136
Internal interface and configuration of the compression library.