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