rev |
line source |
alpar@9
|
1 /* deflate.c -- compress data using the deflation algorithm
|
alpar@9
|
2 * Copyright (C) 1995-2010 Jean-loup Gailly and Mark Adler
|
alpar@9
|
3 * For conditions of distribution and use, see copyright notice in zlib.h
|
alpar@9
|
4 */
|
alpar@9
|
5
|
alpar@9
|
6 /*
|
alpar@9
|
7 * ALGORITHM
|
alpar@9
|
8 *
|
alpar@9
|
9 * The "deflation" process depends on being able to identify portions
|
alpar@9
|
10 * of the input text which are identical to earlier input (within a
|
alpar@9
|
11 * sliding window trailing behind the input currently being processed).
|
alpar@9
|
12 *
|
alpar@9
|
13 * The most straightforward technique turns out to be the fastest for
|
alpar@9
|
14 * most input files: try all possible matches and select the longest.
|
alpar@9
|
15 * The key feature of this algorithm is that insertions into the string
|
alpar@9
|
16 * dictionary are very simple and thus fast, and deletions are avoided
|
alpar@9
|
17 * completely. Insertions are performed at each input character, whereas
|
alpar@9
|
18 * string matches are performed only when the previous match ends. So it
|
alpar@9
|
19 * is preferable to spend more time in matches to allow very fast string
|
alpar@9
|
20 * insertions and avoid deletions. The matching algorithm for small
|
alpar@9
|
21 * strings is inspired from that of Rabin & Karp. A brute force approach
|
alpar@9
|
22 * is used to find longer strings when a small match has been found.
|
alpar@9
|
23 * A similar algorithm is used in comic (by Jan-Mark Wams) and freeze
|
alpar@9
|
24 * (by Leonid Broukhis).
|
alpar@9
|
25 * A previous version of this file used a more sophisticated algorithm
|
alpar@9
|
26 * (by Fiala and Greene) which is guaranteed to run in linear amortized
|
alpar@9
|
27 * time, but has a larger average cost, uses more memory and is patented.
|
alpar@9
|
28 * However the F&G algorithm may be faster for some highly redundant
|
alpar@9
|
29 * files if the parameter max_chain_length (described below) is too large.
|
alpar@9
|
30 *
|
alpar@9
|
31 * ACKNOWLEDGEMENTS
|
alpar@9
|
32 *
|
alpar@9
|
33 * The idea of lazy evaluation of matches is due to Jan-Mark Wams, and
|
alpar@9
|
34 * I found it in 'freeze' written by Leonid Broukhis.
|
alpar@9
|
35 * Thanks to many people for bug reports and testing.
|
alpar@9
|
36 *
|
alpar@9
|
37 * REFERENCES
|
alpar@9
|
38 *
|
alpar@9
|
39 * Deutsch, L.P.,"DEFLATE Compressed Data Format Specification".
|
alpar@9
|
40 * Available in http://www.ietf.org/rfc/rfc1951.txt
|
alpar@9
|
41 *
|
alpar@9
|
42 * A description of the Rabin and Karp algorithm is given in the book
|
alpar@9
|
43 * "Algorithms" by R. Sedgewick, Addison-Wesley, p252.
|
alpar@9
|
44 *
|
alpar@9
|
45 * Fiala,E.R., and Greene,D.H.
|
alpar@9
|
46 * Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595
|
alpar@9
|
47 *
|
alpar@9
|
48 */
|
alpar@9
|
49
|
alpar@9
|
50 /* @(#) $Id$ */
|
alpar@9
|
51
|
alpar@9
|
52 #include "deflate.h"
|
alpar@9
|
53
|
alpar@9
|
54 const char deflate_copyright[] =
|
alpar@9
|
55 " deflate 1.2.5 Copyright 1995-2010 Jean-loup Gailly and Mark Adler ";
|
alpar@9
|
56 /*
|
alpar@9
|
57 If you use the zlib library in a product, an acknowledgment is welcome
|
alpar@9
|
58 in the documentation of your product. If for some reason you cannot
|
alpar@9
|
59 include such an acknowledgment, I would appreciate that you keep this
|
alpar@9
|
60 copyright string in the executable of your product.
|
alpar@9
|
61 */
|
alpar@9
|
62
|
alpar@9
|
63 /* ===========================================================================
|
alpar@9
|
64 * Function prototypes.
|
alpar@9
|
65 */
|
alpar@9
|
66 typedef enum {
|
alpar@9
|
67 need_more, /* block not completed, need more input or more output */
|
alpar@9
|
68 block_done, /* block flush performed */
|
alpar@9
|
69 finish_started, /* finish started, need only more output at next deflate */
|
alpar@9
|
70 finish_done /* finish done, accept no more input or output */
|
alpar@9
|
71 } block_state;
|
alpar@9
|
72
|
alpar@9
|
73 typedef block_state (*compress_func) OF((deflate_state *s, int flush));
|
alpar@9
|
74 /* Compression function. Returns the block state after the call. */
|
alpar@9
|
75
|
alpar@9
|
76 local void fill_window OF((deflate_state *s));
|
alpar@9
|
77 local block_state deflate_stored OF((deflate_state *s, int flush));
|
alpar@9
|
78 local block_state deflate_fast OF((deflate_state *s, int flush));
|
alpar@9
|
79 #ifndef FASTEST
|
alpar@9
|
80 local block_state deflate_slow OF((deflate_state *s, int flush));
|
alpar@9
|
81 #endif
|
alpar@9
|
82 local block_state deflate_rle OF((deflate_state *s, int flush));
|
alpar@9
|
83 local block_state deflate_huff OF((deflate_state *s, int flush));
|
alpar@9
|
84 local void lm_init OF((deflate_state *s));
|
alpar@9
|
85 local void putShortMSB OF((deflate_state *s, uInt b));
|
alpar@9
|
86 local void flush_pending OF((z_streamp strm));
|
alpar@9
|
87 local int read_buf OF((z_streamp strm, Bytef *buf, unsigned size));
|
alpar@9
|
88 #ifdef ASMV
|
alpar@9
|
89 void match_init OF((void)); /* asm code initialization */
|
alpar@9
|
90 uInt longest_match OF((deflate_state *s, IPos cur_match));
|
alpar@9
|
91 #else
|
alpar@9
|
92 local uInt longest_match OF((deflate_state *s, IPos cur_match));
|
alpar@9
|
93 #endif
|
alpar@9
|
94
|
alpar@9
|
95 #ifdef DEBUG
|
alpar@9
|
96 local void check_match OF((deflate_state *s, IPos start, IPos match,
|
alpar@9
|
97 int length));
|
alpar@9
|
98 #endif
|
alpar@9
|
99
|
alpar@9
|
100 /* ===========================================================================
|
alpar@9
|
101 * Local data
|
alpar@9
|
102 */
|
alpar@9
|
103
|
alpar@9
|
104 #define NIL 0
|
alpar@9
|
105 /* Tail of hash chains */
|
alpar@9
|
106
|
alpar@9
|
107 #ifndef TOO_FAR
|
alpar@9
|
108 # define TOO_FAR 4096
|
alpar@9
|
109 #endif
|
alpar@9
|
110 /* Matches of length 3 are discarded if their distance exceeds TOO_FAR */
|
alpar@9
|
111
|
alpar@9
|
112 /* Values for max_lazy_match, good_match and max_chain_length, depending on
|
alpar@9
|
113 * the desired pack level (0..9). The values given below have been tuned to
|
alpar@9
|
114 * exclude worst case performance for pathological files. Better values may be
|
alpar@9
|
115 * found for specific files.
|
alpar@9
|
116 */
|
alpar@9
|
117 typedef struct config_s {
|
alpar@9
|
118 ush good_length; /* reduce lazy search above this match length */
|
alpar@9
|
119 ush max_lazy; /* do not perform lazy search above this match length */
|
alpar@9
|
120 ush nice_length; /* quit search above this match length */
|
alpar@9
|
121 ush max_chain;
|
alpar@9
|
122 compress_func func;
|
alpar@9
|
123 } config;
|
alpar@9
|
124
|
alpar@9
|
125 #ifdef FASTEST
|
alpar@9
|
126 local const config configuration_table[2] = {
|
alpar@9
|
127 /* good lazy nice chain */
|
alpar@9
|
128 /* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */
|
alpar@9
|
129 /* 1 */ {4, 4, 8, 4, deflate_fast}}; /* max speed, no lazy matches */
|
alpar@9
|
130 #else
|
alpar@9
|
131 local const config configuration_table[10] = {
|
alpar@9
|
132 /* good lazy nice chain */
|
alpar@9
|
133 /* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */
|
alpar@9
|
134 /* 1 */ {4, 4, 8, 4, deflate_fast}, /* max speed, no lazy matches */
|
alpar@9
|
135 /* 2 */ {4, 5, 16, 8, deflate_fast},
|
alpar@9
|
136 /* 3 */ {4, 6, 32, 32, deflate_fast},
|
alpar@9
|
137
|
alpar@9
|
138 /* 4 */ {4, 4, 16, 16, deflate_slow}, /* lazy matches */
|
alpar@9
|
139 /* 5 */ {8, 16, 32, 32, deflate_slow},
|
alpar@9
|
140 /* 6 */ {8, 16, 128, 128, deflate_slow},
|
alpar@9
|
141 /* 7 */ {8, 32, 128, 256, deflate_slow},
|
alpar@9
|
142 /* 8 */ {32, 128, 258, 1024, deflate_slow},
|
alpar@9
|
143 /* 9 */ {32, 258, 258, 4096, deflate_slow}}; /* max compression */
|
alpar@9
|
144 #endif
|
alpar@9
|
145
|
alpar@9
|
146 /* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4
|
alpar@9
|
147 * For deflate_fast() (levels <= 3) good is ignored and lazy has a different
|
alpar@9
|
148 * meaning.
|
alpar@9
|
149 */
|
alpar@9
|
150
|
alpar@9
|
151 #define EQUAL 0
|
alpar@9
|
152 /* result of memcmp for equal strings */
|
alpar@9
|
153
|
alpar@9
|
154 #ifndef NO_DUMMY_DECL
|
alpar@9
|
155 struct static_tree_desc_s {int dummy;}; /* for buggy compilers */
|
alpar@9
|
156 #endif
|
alpar@9
|
157
|
alpar@9
|
158 /* ===========================================================================
|
alpar@9
|
159 * Update a hash value with the given input byte
|
alpar@9
|
160 * IN assertion: all calls to to UPDATE_HASH are made with consecutive
|
alpar@9
|
161 * input characters, so that a running hash key can be computed from the
|
alpar@9
|
162 * previous key instead of complete recalculation each time.
|
alpar@9
|
163 */
|
alpar@9
|
164 #define UPDATE_HASH(s,h,c) (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask)
|
alpar@9
|
165
|
alpar@9
|
166
|
alpar@9
|
167 /* ===========================================================================
|
alpar@9
|
168 * Insert string str in the dictionary and set match_head to the previous head
|
alpar@9
|
169 * of the hash chain (the most recent string with same hash key). Return
|
alpar@9
|
170 * the previous length of the hash chain.
|
alpar@9
|
171 * If this file is compiled with -DFASTEST, the compression level is forced
|
alpar@9
|
172 * to 1, and no hash chains are maintained.
|
alpar@9
|
173 * IN assertion: all calls to to INSERT_STRING are made with consecutive
|
alpar@9
|
174 * input characters and the first MIN_MATCH bytes of str are valid
|
alpar@9
|
175 * (except for the last MIN_MATCH-1 bytes of the input file).
|
alpar@9
|
176 */
|
alpar@9
|
177 #ifdef FASTEST
|
alpar@9
|
178 #define INSERT_STRING(s, str, match_head) \
|
alpar@9
|
179 (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
|
alpar@9
|
180 match_head = s->head[s->ins_h], \
|
alpar@9
|
181 s->head[s->ins_h] = (Pos)(str))
|
alpar@9
|
182 #else
|
alpar@9
|
183 #define INSERT_STRING(s, str, match_head) \
|
alpar@9
|
184 (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
|
alpar@9
|
185 match_head = s->prev[(str) & s->w_mask] = s->head[s->ins_h], \
|
alpar@9
|
186 s->head[s->ins_h] = (Pos)(str))
|
alpar@9
|
187 #endif
|
alpar@9
|
188
|
alpar@9
|
189 /* ===========================================================================
|
alpar@9
|
190 * Initialize the hash table (avoiding 64K overflow for 16 bit systems).
|
alpar@9
|
191 * prev[] will be initialized on the fly.
|
alpar@9
|
192 */
|
alpar@9
|
193 #define CLEAR_HASH(s) \
|
alpar@9
|
194 s->head[s->hash_size-1] = NIL; \
|
alpar@9
|
195 zmemzero((Bytef *)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head));
|
alpar@9
|
196
|
alpar@9
|
197 /* ========================================================================= */
|
alpar@9
|
198 int ZEXPORT deflateInit_(strm, level, version, stream_size)
|
alpar@9
|
199 z_streamp strm;
|
alpar@9
|
200 int level;
|
alpar@9
|
201 const char *version;
|
alpar@9
|
202 int stream_size;
|
alpar@9
|
203 {
|
alpar@9
|
204 return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL,
|
alpar@9
|
205 Z_DEFAULT_STRATEGY, version, stream_size);
|
alpar@9
|
206 /* To do: ignore strm->next_in if we use it as window */
|
alpar@9
|
207 }
|
alpar@9
|
208
|
alpar@9
|
209 /* ========================================================================= */
|
alpar@9
|
210 int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy,
|
alpar@9
|
211 version, stream_size)
|
alpar@9
|
212 z_streamp strm;
|
alpar@9
|
213 int level;
|
alpar@9
|
214 int method;
|
alpar@9
|
215 int windowBits;
|
alpar@9
|
216 int memLevel;
|
alpar@9
|
217 int strategy;
|
alpar@9
|
218 const char *version;
|
alpar@9
|
219 int stream_size;
|
alpar@9
|
220 {
|
alpar@9
|
221 deflate_state *s;
|
alpar@9
|
222 int wrap = 1;
|
alpar@9
|
223 static const char my_version[] = ZLIB_VERSION;
|
alpar@9
|
224
|
alpar@9
|
225 ushf *overlay;
|
alpar@9
|
226 /* We overlay pending_buf and d_buf+l_buf. This works since the average
|
alpar@9
|
227 * output size for (length,distance) codes is <= 24 bits.
|
alpar@9
|
228 */
|
alpar@9
|
229
|
alpar@9
|
230 if (version == Z_NULL || version[0] != my_version[0] ||
|
alpar@9
|
231 stream_size != sizeof(z_stream)) {
|
alpar@9
|
232 return Z_VERSION_ERROR;
|
alpar@9
|
233 }
|
alpar@9
|
234 if (strm == Z_NULL) return Z_STREAM_ERROR;
|
alpar@9
|
235
|
alpar@9
|
236 strm->msg = Z_NULL;
|
alpar@9
|
237 if (strm->zalloc == (alloc_func)0) {
|
alpar@9
|
238 strm->zalloc = zcalloc;
|
alpar@9
|
239 strm->opaque = (voidpf)0;
|
alpar@9
|
240 }
|
alpar@9
|
241 if (strm->zfree == (free_func)0) strm->zfree = zcfree;
|
alpar@9
|
242
|
alpar@9
|
243 #ifdef FASTEST
|
alpar@9
|
244 if (level != 0) level = 1;
|
alpar@9
|
245 #else
|
alpar@9
|
246 if (level == Z_DEFAULT_COMPRESSION) level = 6;
|
alpar@9
|
247 #endif
|
alpar@9
|
248
|
alpar@9
|
249 if (windowBits < 0) { /* suppress zlib wrapper */
|
alpar@9
|
250 wrap = 0;
|
alpar@9
|
251 windowBits = -windowBits;
|
alpar@9
|
252 }
|
alpar@9
|
253 #ifdef GZIP
|
alpar@9
|
254 else if (windowBits > 15) {
|
alpar@9
|
255 wrap = 2; /* write gzip wrapper instead */
|
alpar@9
|
256 windowBits -= 16;
|
alpar@9
|
257 }
|
alpar@9
|
258 #endif
|
alpar@9
|
259 if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED ||
|
alpar@9
|
260 windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
|
alpar@9
|
261 strategy < 0 || strategy > Z_FIXED) {
|
alpar@9
|
262 return Z_STREAM_ERROR;
|
alpar@9
|
263 }
|
alpar@9
|
264 if (windowBits == 8) windowBits = 9; /* until 256-byte window bug fixed */
|
alpar@9
|
265 s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state));
|
alpar@9
|
266 if (s == Z_NULL) return Z_MEM_ERROR;
|
alpar@9
|
267 strm->state = (struct internal_state FAR *)s;
|
alpar@9
|
268 s->strm = strm;
|
alpar@9
|
269
|
alpar@9
|
270 s->wrap = wrap;
|
alpar@9
|
271 s->gzhead = Z_NULL;
|
alpar@9
|
272 s->w_bits = windowBits;
|
alpar@9
|
273 s->w_size = 1 << s->w_bits;
|
alpar@9
|
274 s->w_mask = s->w_size - 1;
|
alpar@9
|
275
|
alpar@9
|
276 s->hash_bits = memLevel + 7;
|
alpar@9
|
277 s->hash_size = 1 << s->hash_bits;
|
alpar@9
|
278 s->hash_mask = s->hash_size - 1;
|
alpar@9
|
279 s->hash_shift = ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH);
|
alpar@9
|
280
|
alpar@9
|
281 s->window = (Bytef *) ZALLOC(strm, s->w_size, 2*sizeof(Byte));
|
alpar@9
|
282 s->prev = (Posf *) ZALLOC(strm, s->w_size, sizeof(Pos));
|
alpar@9
|
283 s->head = (Posf *) ZALLOC(strm, s->hash_size, sizeof(Pos));
|
alpar@9
|
284
|
alpar@9
|
285 s->high_water = 0; /* nothing written to s->window yet */
|
alpar@9
|
286
|
alpar@9
|
287 s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
|
alpar@9
|
288
|
alpar@9
|
289 overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2);
|
alpar@9
|
290 s->pending_buf = (uchf *) overlay;
|
alpar@9
|
291 s->pending_buf_size = (ulg)s->lit_bufsize * (sizeof(ush)+2L);
|
alpar@9
|
292
|
alpar@9
|
293 if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||
|
alpar@9
|
294 s->pending_buf == Z_NULL) {
|
alpar@9
|
295 s->status = FINISH_STATE;
|
alpar@9
|
296 strm->msg = (char*)ERR_MSG(Z_MEM_ERROR);
|
alpar@9
|
297 deflateEnd (strm);
|
alpar@9
|
298 return Z_MEM_ERROR;
|
alpar@9
|
299 }
|
alpar@9
|
300 s->d_buf = overlay + s->lit_bufsize/sizeof(ush);
|
alpar@9
|
301 s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize;
|
alpar@9
|
302
|
alpar@9
|
303 s->level = level;
|
alpar@9
|
304 s->strategy = strategy;
|
alpar@9
|
305 s->method = (Byte)method;
|
alpar@9
|
306
|
alpar@9
|
307 return deflateReset(strm);
|
alpar@9
|
308 }
|
alpar@9
|
309
|
alpar@9
|
310 /* ========================================================================= */
|
alpar@9
|
311 int ZEXPORT deflateSetDictionary (strm, dictionary, dictLength)
|
alpar@9
|
312 z_streamp strm;
|
alpar@9
|
313 const Bytef *dictionary;
|
alpar@9
|
314 uInt dictLength;
|
alpar@9
|
315 {
|
alpar@9
|
316 deflate_state *s;
|
alpar@9
|
317 uInt length = dictLength;
|
alpar@9
|
318 uInt n;
|
alpar@9
|
319 IPos hash_head = 0;
|
alpar@9
|
320
|
alpar@9
|
321 if (strm == Z_NULL || strm->state == Z_NULL || dictionary == Z_NULL ||
|
alpar@9
|
322 strm->state->wrap == 2 ||
|
alpar@9
|
323 (strm->state->wrap == 1 && strm->state->status != INIT_STATE))
|
alpar@9
|
324 return Z_STREAM_ERROR;
|
alpar@9
|
325
|
alpar@9
|
326 s = strm->state;
|
alpar@9
|
327 if (s->wrap)
|
alpar@9
|
328 strm->adler = adler32(strm->adler, dictionary, dictLength);
|
alpar@9
|
329
|
alpar@9
|
330 if (length < MIN_MATCH) return Z_OK;
|
alpar@9
|
331 if (length > s->w_size) {
|
alpar@9
|
332 length = s->w_size;
|
alpar@9
|
333 dictionary += dictLength - length; /* use the tail of the dictionary */
|
alpar@9
|
334 }
|
alpar@9
|
335 zmemcpy(s->window, dictionary, length);
|
alpar@9
|
336 s->strstart = length;
|
alpar@9
|
337 s->block_start = (long)length;
|
alpar@9
|
338
|
alpar@9
|
339 /* Insert all strings in the hash table (except for the last two bytes).
|
alpar@9
|
340 * s->lookahead stays null, so s->ins_h will be recomputed at the next
|
alpar@9
|
341 * call of fill_window.
|
alpar@9
|
342 */
|
alpar@9
|
343 s->ins_h = s->window[0];
|
alpar@9
|
344 UPDATE_HASH(s, s->ins_h, s->window[1]);
|
alpar@9
|
345 for (n = 0; n <= length - MIN_MATCH; n++) {
|
alpar@9
|
346 INSERT_STRING(s, n, hash_head);
|
alpar@9
|
347 }
|
alpar@9
|
348 if (hash_head) hash_head = 0; /* to make compiler happy */
|
alpar@9
|
349 return Z_OK;
|
alpar@9
|
350 }
|
alpar@9
|
351
|
alpar@9
|
352 /* ========================================================================= */
|
alpar@9
|
353 int ZEXPORT deflateReset (strm)
|
alpar@9
|
354 z_streamp strm;
|
alpar@9
|
355 {
|
alpar@9
|
356 deflate_state *s;
|
alpar@9
|
357
|
alpar@9
|
358 if (strm == Z_NULL || strm->state == Z_NULL ||
|
alpar@9
|
359 strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0) {
|
alpar@9
|
360 return Z_STREAM_ERROR;
|
alpar@9
|
361 }
|
alpar@9
|
362
|
alpar@9
|
363 strm->total_in = strm->total_out = 0;
|
alpar@9
|
364 strm->msg = Z_NULL; /* use zfree if we ever allocate msg dynamically */
|
alpar@9
|
365 strm->data_type = Z_UNKNOWN;
|
alpar@9
|
366
|
alpar@9
|
367 s = (deflate_state *)strm->state;
|
alpar@9
|
368 s->pending = 0;
|
alpar@9
|
369 s->pending_out = s->pending_buf;
|
alpar@9
|
370
|
alpar@9
|
371 if (s->wrap < 0) {
|
alpar@9
|
372 s->wrap = -s->wrap; /* was made negative by deflate(..., Z_FINISH); */
|
alpar@9
|
373 }
|
alpar@9
|
374 s->status = s->wrap ? INIT_STATE : BUSY_STATE;
|
alpar@9
|
375 strm->adler =
|
alpar@9
|
376 #ifdef GZIP
|
alpar@9
|
377 s->wrap == 2 ? crc32(0L, Z_NULL, 0) :
|
alpar@9
|
378 #endif
|
alpar@9
|
379 adler32(0L, Z_NULL, 0);
|
alpar@9
|
380 s->last_flush = Z_NO_FLUSH;
|
alpar@9
|
381
|
alpar@9
|
382 _tr_init(s);
|
alpar@9
|
383 lm_init(s);
|
alpar@9
|
384
|
alpar@9
|
385 return Z_OK;
|
alpar@9
|
386 }
|
alpar@9
|
387
|
alpar@9
|
388 /* ========================================================================= */
|
alpar@9
|
389 int ZEXPORT deflateSetHeader (strm, head)
|
alpar@9
|
390 z_streamp strm;
|
alpar@9
|
391 gz_headerp head;
|
alpar@9
|
392 {
|
alpar@9
|
393 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
|
alpar@9
|
394 if (strm->state->wrap != 2) return Z_STREAM_ERROR;
|
alpar@9
|
395 strm->state->gzhead = head;
|
alpar@9
|
396 return Z_OK;
|
alpar@9
|
397 }
|
alpar@9
|
398
|
alpar@9
|
399 /* ========================================================================= */
|
alpar@9
|
400 int ZEXPORT deflatePrime (strm, bits, value)
|
alpar@9
|
401 z_streamp strm;
|
alpar@9
|
402 int bits;
|
alpar@9
|
403 int value;
|
alpar@9
|
404 {
|
alpar@9
|
405 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
|
alpar@9
|
406 strm->state->bi_valid = bits;
|
alpar@9
|
407 strm->state->bi_buf = (ush)(value & ((1 << bits) - 1));
|
alpar@9
|
408 return Z_OK;
|
alpar@9
|
409 }
|
alpar@9
|
410
|
alpar@9
|
411 /* ========================================================================= */
|
alpar@9
|
412 int ZEXPORT deflateParams(strm, level, strategy)
|
alpar@9
|
413 z_streamp strm;
|
alpar@9
|
414 int level;
|
alpar@9
|
415 int strategy;
|
alpar@9
|
416 {
|
alpar@9
|
417 deflate_state *s;
|
alpar@9
|
418 compress_func func;
|
alpar@9
|
419 int err = Z_OK;
|
alpar@9
|
420
|
alpar@9
|
421 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
|
alpar@9
|
422 s = strm->state;
|
alpar@9
|
423
|
alpar@9
|
424 #ifdef FASTEST
|
alpar@9
|
425 if (level != 0) level = 1;
|
alpar@9
|
426 #else
|
alpar@9
|
427 if (level == Z_DEFAULT_COMPRESSION) level = 6;
|
alpar@9
|
428 #endif
|
alpar@9
|
429 if (level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED) {
|
alpar@9
|
430 return Z_STREAM_ERROR;
|
alpar@9
|
431 }
|
alpar@9
|
432 func = configuration_table[s->level].func;
|
alpar@9
|
433
|
alpar@9
|
434 if ((strategy != s->strategy || func != configuration_table[level].func) &&
|
alpar@9
|
435 strm->total_in != 0) {
|
alpar@9
|
436 /* Flush the last buffer: */
|
alpar@9
|
437 err = deflate(strm, Z_BLOCK);
|
alpar@9
|
438 }
|
alpar@9
|
439 if (s->level != level) {
|
alpar@9
|
440 s->level = level;
|
alpar@9
|
441 s->max_lazy_match = configuration_table[level].max_lazy;
|
alpar@9
|
442 s->good_match = configuration_table[level].good_length;
|
alpar@9
|
443 s->nice_match = configuration_table[level].nice_length;
|
alpar@9
|
444 s->max_chain_length = configuration_table[level].max_chain;
|
alpar@9
|
445 }
|
alpar@9
|
446 s->strategy = strategy;
|
alpar@9
|
447 return err;
|
alpar@9
|
448 }
|
alpar@9
|
449
|
alpar@9
|
450 /* ========================================================================= */
|
alpar@9
|
451 int ZEXPORT deflateTune(strm, good_length, max_lazy, nice_length, max_chain)
|
alpar@9
|
452 z_streamp strm;
|
alpar@9
|
453 int good_length;
|
alpar@9
|
454 int max_lazy;
|
alpar@9
|
455 int nice_length;
|
alpar@9
|
456 int max_chain;
|
alpar@9
|
457 {
|
alpar@9
|
458 deflate_state *s;
|
alpar@9
|
459
|
alpar@9
|
460 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
|
alpar@9
|
461 s = strm->state;
|
alpar@9
|
462 s->good_match = good_length;
|
alpar@9
|
463 s->max_lazy_match = max_lazy;
|
alpar@9
|
464 s->nice_match = nice_length;
|
alpar@9
|
465 s->max_chain_length = max_chain;
|
alpar@9
|
466 return Z_OK;
|
alpar@9
|
467 }
|
alpar@9
|
468
|
alpar@9
|
469 /* =========================================================================
|
alpar@9
|
470 * For the default windowBits of 15 and memLevel of 8, this function returns
|
alpar@9
|
471 * a close to exact, as well as small, upper bound on the compressed size.
|
alpar@9
|
472 * They are coded as constants here for a reason--if the #define's are
|
alpar@9
|
473 * changed, then this function needs to be changed as well. The return
|
alpar@9
|
474 * value for 15 and 8 only works for those exact settings.
|
alpar@9
|
475 *
|
alpar@9
|
476 * For any setting other than those defaults for windowBits and memLevel,
|
alpar@9
|
477 * the value returned is a conservative worst case for the maximum expansion
|
alpar@9
|
478 * resulting from using fixed blocks instead of stored blocks, which deflate
|
alpar@9
|
479 * can emit on compressed data for some combinations of the parameters.
|
alpar@9
|
480 *
|
alpar@9
|
481 * This function could be more sophisticated to provide closer upper bounds for
|
alpar@9
|
482 * every combination of windowBits and memLevel. But even the conservative
|
alpar@9
|
483 * upper bound of about 14% expansion does not seem onerous for output buffer
|
alpar@9
|
484 * allocation.
|
alpar@9
|
485 */
|
alpar@9
|
486 uLong ZEXPORT deflateBound(strm, sourceLen)
|
alpar@9
|
487 z_streamp strm;
|
alpar@9
|
488 uLong sourceLen;
|
alpar@9
|
489 {
|
alpar@9
|
490 deflate_state *s;
|
alpar@9
|
491 uLong complen, wraplen;
|
alpar@9
|
492 Bytef *str;
|
alpar@9
|
493
|
alpar@9
|
494 /* conservative upper bound for compressed data */
|
alpar@9
|
495 complen = sourceLen +
|
alpar@9
|
496 ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 5;
|
alpar@9
|
497
|
alpar@9
|
498 /* if can't get parameters, return conservative bound plus zlib wrapper */
|
alpar@9
|
499 if (strm == Z_NULL || strm->state == Z_NULL)
|
alpar@9
|
500 return complen + 6;
|
alpar@9
|
501
|
alpar@9
|
502 /* compute wrapper length */
|
alpar@9
|
503 s = strm->state;
|
alpar@9
|
504 switch (s->wrap) {
|
alpar@9
|
505 case 0: /* raw deflate */
|
alpar@9
|
506 wraplen = 0;
|
alpar@9
|
507 break;
|
alpar@9
|
508 case 1: /* zlib wrapper */
|
alpar@9
|
509 wraplen = 6 + (s->strstart ? 4 : 0);
|
alpar@9
|
510 break;
|
alpar@9
|
511 case 2: /* gzip wrapper */
|
alpar@9
|
512 wraplen = 18;
|
alpar@9
|
513 if (s->gzhead != Z_NULL) { /* user-supplied gzip header */
|
alpar@9
|
514 if (s->gzhead->extra != Z_NULL)
|
alpar@9
|
515 wraplen += 2 + s->gzhead->extra_len;
|
alpar@9
|
516 str = s->gzhead->name;
|
alpar@9
|
517 if (str != Z_NULL)
|
alpar@9
|
518 do {
|
alpar@9
|
519 wraplen++;
|
alpar@9
|
520 } while (*str++);
|
alpar@9
|
521 str = s->gzhead->comment;
|
alpar@9
|
522 if (str != Z_NULL)
|
alpar@9
|
523 do {
|
alpar@9
|
524 wraplen++;
|
alpar@9
|
525 } while (*str++);
|
alpar@9
|
526 if (s->gzhead->hcrc)
|
alpar@9
|
527 wraplen += 2;
|
alpar@9
|
528 }
|
alpar@9
|
529 break;
|
alpar@9
|
530 default: /* for compiler happiness */
|
alpar@9
|
531 wraplen = 6;
|
alpar@9
|
532 }
|
alpar@9
|
533
|
alpar@9
|
534 /* if not default parameters, return conservative bound */
|
alpar@9
|
535 if (s->w_bits != 15 || s->hash_bits != 8 + 7)
|
alpar@9
|
536 return complen + wraplen;
|
alpar@9
|
537
|
alpar@9
|
538 /* default settings: return tight bound for that case */
|
alpar@9
|
539 return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) +
|
alpar@9
|
540 (sourceLen >> 25) + 13 - 6 + wraplen;
|
alpar@9
|
541 }
|
alpar@9
|
542
|
alpar@9
|
543 /* =========================================================================
|
alpar@9
|
544 * Put a short in the pending buffer. The 16-bit value is put in MSB order.
|
alpar@9
|
545 * IN assertion: the stream state is correct and there is enough room in
|
alpar@9
|
546 * pending_buf.
|
alpar@9
|
547 */
|
alpar@9
|
548 local void putShortMSB (s, b)
|
alpar@9
|
549 deflate_state *s;
|
alpar@9
|
550 uInt b;
|
alpar@9
|
551 {
|
alpar@9
|
552 put_byte(s, (Byte)(b >> 8));
|
alpar@9
|
553 put_byte(s, (Byte)(b & 0xff));
|
alpar@9
|
554 }
|
alpar@9
|
555
|
alpar@9
|
556 /* =========================================================================
|
alpar@9
|
557 * Flush as much pending output as possible. All deflate() output goes
|
alpar@9
|
558 * through this function so some applications may wish to modify it
|
alpar@9
|
559 * to avoid allocating a large strm->next_out buffer and copying into it.
|
alpar@9
|
560 * (See also read_buf()).
|
alpar@9
|
561 */
|
alpar@9
|
562 local void flush_pending(strm)
|
alpar@9
|
563 z_streamp strm;
|
alpar@9
|
564 {
|
alpar@9
|
565 unsigned len = strm->state->pending;
|
alpar@9
|
566
|
alpar@9
|
567 if (len > strm->avail_out) len = strm->avail_out;
|
alpar@9
|
568 if (len == 0) return;
|
alpar@9
|
569
|
alpar@9
|
570 zmemcpy(strm->next_out, strm->state->pending_out, len);
|
alpar@9
|
571 strm->next_out += len;
|
alpar@9
|
572 strm->state->pending_out += len;
|
alpar@9
|
573 strm->total_out += len;
|
alpar@9
|
574 strm->avail_out -= len;
|
alpar@9
|
575 strm->state->pending -= len;
|
alpar@9
|
576 if (strm->state->pending == 0) {
|
alpar@9
|
577 strm->state->pending_out = strm->state->pending_buf;
|
alpar@9
|
578 }
|
alpar@9
|
579 }
|
alpar@9
|
580
|
alpar@9
|
581 /* ========================================================================= */
|
alpar@9
|
582 int ZEXPORT deflate (strm, flush)
|
alpar@9
|
583 z_streamp strm;
|
alpar@9
|
584 int flush;
|
alpar@9
|
585 {
|
alpar@9
|
586 int old_flush; /* value of flush param for previous deflate call */
|
alpar@9
|
587 deflate_state *s;
|
alpar@9
|
588
|
alpar@9
|
589 if (strm == Z_NULL || strm->state == Z_NULL ||
|
alpar@9
|
590 flush > Z_BLOCK || flush < 0) {
|
alpar@9
|
591 return Z_STREAM_ERROR;
|
alpar@9
|
592 }
|
alpar@9
|
593 s = strm->state;
|
alpar@9
|
594
|
alpar@9
|
595 if (strm->next_out == Z_NULL ||
|
alpar@9
|
596 (strm->next_in == Z_NULL && strm->avail_in != 0) ||
|
alpar@9
|
597 (s->status == FINISH_STATE && flush != Z_FINISH)) {
|
alpar@9
|
598 ERR_RETURN(strm, Z_STREAM_ERROR);
|
alpar@9
|
599 }
|
alpar@9
|
600 if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR);
|
alpar@9
|
601
|
alpar@9
|
602 s->strm = strm; /* just in case */
|
alpar@9
|
603 old_flush = s->last_flush;
|
alpar@9
|
604 s->last_flush = flush;
|
alpar@9
|
605
|
alpar@9
|
606 /* Write the header */
|
alpar@9
|
607 if (s->status == INIT_STATE) {
|
alpar@9
|
608 #ifdef GZIP
|
alpar@9
|
609 if (s->wrap == 2) {
|
alpar@9
|
610 strm->adler = crc32(0L, Z_NULL, 0);
|
alpar@9
|
611 put_byte(s, 31);
|
alpar@9
|
612 put_byte(s, 139);
|
alpar@9
|
613 put_byte(s, 8);
|
alpar@9
|
614 if (s->gzhead == Z_NULL) {
|
alpar@9
|
615 put_byte(s, 0);
|
alpar@9
|
616 put_byte(s, 0);
|
alpar@9
|
617 put_byte(s, 0);
|
alpar@9
|
618 put_byte(s, 0);
|
alpar@9
|
619 put_byte(s, 0);
|
alpar@9
|
620 put_byte(s, s->level == 9 ? 2 :
|
alpar@9
|
621 (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
|
alpar@9
|
622 4 : 0));
|
alpar@9
|
623 put_byte(s, OS_CODE);
|
alpar@9
|
624 s->status = BUSY_STATE;
|
alpar@9
|
625 }
|
alpar@9
|
626 else {
|
alpar@9
|
627 put_byte(s, (s->gzhead->text ? 1 : 0) +
|
alpar@9
|
628 (s->gzhead->hcrc ? 2 : 0) +
|
alpar@9
|
629 (s->gzhead->extra == Z_NULL ? 0 : 4) +
|
alpar@9
|
630 (s->gzhead->name == Z_NULL ? 0 : 8) +
|
alpar@9
|
631 (s->gzhead->comment == Z_NULL ? 0 : 16)
|
alpar@9
|
632 );
|
alpar@9
|
633 put_byte(s, (Byte)(s->gzhead->time & 0xff));
|
alpar@9
|
634 put_byte(s, (Byte)((s->gzhead->time >> 8) & 0xff));
|
alpar@9
|
635 put_byte(s, (Byte)((s->gzhead->time >> 16) & 0xff));
|
alpar@9
|
636 put_byte(s, (Byte)((s->gzhead->time >> 24) & 0xff));
|
alpar@9
|
637 put_byte(s, s->level == 9 ? 2 :
|
alpar@9
|
638 (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
|
alpar@9
|
639 4 : 0));
|
alpar@9
|
640 put_byte(s, s->gzhead->os & 0xff);
|
alpar@9
|
641 if (s->gzhead->extra != Z_NULL) {
|
alpar@9
|
642 put_byte(s, s->gzhead->extra_len & 0xff);
|
alpar@9
|
643 put_byte(s, (s->gzhead->extra_len >> 8) & 0xff);
|
alpar@9
|
644 }
|
alpar@9
|
645 if (s->gzhead->hcrc)
|
alpar@9
|
646 strm->adler = crc32(strm->adler, s->pending_buf,
|
alpar@9
|
647 s->pending);
|
alpar@9
|
648 s->gzindex = 0;
|
alpar@9
|
649 s->status = EXTRA_STATE;
|
alpar@9
|
650 }
|
alpar@9
|
651 }
|
alpar@9
|
652 else
|
alpar@9
|
653 #endif
|
alpar@9
|
654 {
|
alpar@9
|
655 uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8;
|
alpar@9
|
656 uInt level_flags;
|
alpar@9
|
657
|
alpar@9
|
658 if (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2)
|
alpar@9
|
659 level_flags = 0;
|
alpar@9
|
660 else if (s->level < 6)
|
alpar@9
|
661 level_flags = 1;
|
alpar@9
|
662 else if (s->level == 6)
|
alpar@9
|
663 level_flags = 2;
|
alpar@9
|
664 else
|
alpar@9
|
665 level_flags = 3;
|
alpar@9
|
666 header |= (level_flags << 6);
|
alpar@9
|
667 if (s->strstart != 0) header |= PRESET_DICT;
|
alpar@9
|
668 header += 31 - (header % 31);
|
alpar@9
|
669
|
alpar@9
|
670 s->status = BUSY_STATE;
|
alpar@9
|
671 putShortMSB(s, header);
|
alpar@9
|
672
|
alpar@9
|
673 /* Save the adler32 of the preset dictionary: */
|
alpar@9
|
674 if (s->strstart != 0) {
|
alpar@9
|
675 putShortMSB(s, (uInt)(strm->adler >> 16));
|
alpar@9
|
676 putShortMSB(s, (uInt)(strm->adler & 0xffff));
|
alpar@9
|
677 }
|
alpar@9
|
678 strm->adler = adler32(0L, Z_NULL, 0);
|
alpar@9
|
679 }
|
alpar@9
|
680 }
|
alpar@9
|
681 #ifdef GZIP
|
alpar@9
|
682 if (s->status == EXTRA_STATE) {
|
alpar@9
|
683 if (s->gzhead->extra != Z_NULL) {
|
alpar@9
|
684 uInt beg = s->pending; /* start of bytes to update crc */
|
alpar@9
|
685
|
alpar@9
|
686 while (s->gzindex < (s->gzhead->extra_len & 0xffff)) {
|
alpar@9
|
687 if (s->pending == s->pending_buf_size) {
|
alpar@9
|
688 if (s->gzhead->hcrc && s->pending > beg)
|
alpar@9
|
689 strm->adler = crc32(strm->adler, s->pending_buf + beg,
|
alpar@9
|
690 s->pending - beg);
|
alpar@9
|
691 flush_pending(strm);
|
alpar@9
|
692 beg = s->pending;
|
alpar@9
|
693 if (s->pending == s->pending_buf_size)
|
alpar@9
|
694 break;
|
alpar@9
|
695 }
|
alpar@9
|
696 put_byte(s, s->gzhead->extra[s->gzindex]);
|
alpar@9
|
697 s->gzindex++;
|
alpar@9
|
698 }
|
alpar@9
|
699 if (s->gzhead->hcrc && s->pending > beg)
|
alpar@9
|
700 strm->adler = crc32(strm->adler, s->pending_buf + beg,
|
alpar@9
|
701 s->pending - beg);
|
alpar@9
|
702 if (s->gzindex == s->gzhead->extra_len) {
|
alpar@9
|
703 s->gzindex = 0;
|
alpar@9
|
704 s->status = NAME_STATE;
|
alpar@9
|
705 }
|
alpar@9
|
706 }
|
alpar@9
|
707 else
|
alpar@9
|
708 s->status = NAME_STATE;
|
alpar@9
|
709 }
|
alpar@9
|
710 if (s->status == NAME_STATE) {
|
alpar@9
|
711 if (s->gzhead->name != Z_NULL) {
|
alpar@9
|
712 uInt beg = s->pending; /* start of bytes to update crc */
|
alpar@9
|
713 int val;
|
alpar@9
|
714
|
alpar@9
|
715 do {
|
alpar@9
|
716 if (s->pending == s->pending_buf_size) {
|
alpar@9
|
717 if (s->gzhead->hcrc && s->pending > beg)
|
alpar@9
|
718 strm->adler = crc32(strm->adler, s->pending_buf + beg,
|
alpar@9
|
719 s->pending - beg);
|
alpar@9
|
720 flush_pending(strm);
|
alpar@9
|
721 beg = s->pending;
|
alpar@9
|
722 if (s->pending == s->pending_buf_size) {
|
alpar@9
|
723 val = 1;
|
alpar@9
|
724 break;
|
alpar@9
|
725 }
|
alpar@9
|
726 }
|
alpar@9
|
727 val = s->gzhead->name[s->gzindex++];
|
alpar@9
|
728 put_byte(s, val);
|
alpar@9
|
729 } while (val != 0);
|
alpar@9
|
730 if (s->gzhead->hcrc && s->pending > beg)
|
alpar@9
|
731 strm->adler = crc32(strm->adler, s->pending_buf + beg,
|
alpar@9
|
732 s->pending - beg);
|
alpar@9
|
733 if (val == 0) {
|
alpar@9
|
734 s->gzindex = 0;
|
alpar@9
|
735 s->status = COMMENT_STATE;
|
alpar@9
|
736 }
|
alpar@9
|
737 }
|
alpar@9
|
738 else
|
alpar@9
|
739 s->status = COMMENT_STATE;
|
alpar@9
|
740 }
|
alpar@9
|
741 if (s->status == COMMENT_STATE) {
|
alpar@9
|
742 if (s->gzhead->comment != Z_NULL) {
|
alpar@9
|
743 uInt beg = s->pending; /* start of bytes to update crc */
|
alpar@9
|
744 int val;
|
alpar@9
|
745
|
alpar@9
|
746 do {
|
alpar@9
|
747 if (s->pending == s->pending_buf_size) {
|
alpar@9
|
748 if (s->gzhead->hcrc && s->pending > beg)
|
alpar@9
|
749 strm->adler = crc32(strm->adler, s->pending_buf + beg,
|
alpar@9
|
750 s->pending - beg);
|
alpar@9
|
751 flush_pending(strm);
|
alpar@9
|
752 beg = s->pending;
|
alpar@9
|
753 if (s->pending == s->pending_buf_size) {
|
alpar@9
|
754 val = 1;
|
alpar@9
|
755 break;
|
alpar@9
|
756 }
|
alpar@9
|
757 }
|
alpar@9
|
758 val = s->gzhead->comment[s->gzindex++];
|
alpar@9
|
759 put_byte(s, val);
|
alpar@9
|
760 } while (val != 0);
|
alpar@9
|
761 if (s->gzhead->hcrc && s->pending > beg)
|
alpar@9
|
762 strm->adler = crc32(strm->adler, s->pending_buf + beg,
|
alpar@9
|
763 s->pending - beg);
|
alpar@9
|
764 if (val == 0)
|
alpar@9
|
765 s->status = HCRC_STATE;
|
alpar@9
|
766 }
|
alpar@9
|
767 else
|
alpar@9
|
768 s->status = HCRC_STATE;
|
alpar@9
|
769 }
|
alpar@9
|
770 if (s->status == HCRC_STATE) {
|
alpar@9
|
771 if (s->gzhead->hcrc) {
|
alpar@9
|
772 if (s->pending + 2 > s->pending_buf_size)
|
alpar@9
|
773 flush_pending(strm);
|
alpar@9
|
774 if (s->pending + 2 <= s->pending_buf_size) {
|
alpar@9
|
775 put_byte(s, (Byte)(strm->adler & 0xff));
|
alpar@9
|
776 put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
|
alpar@9
|
777 strm->adler = crc32(0L, Z_NULL, 0);
|
alpar@9
|
778 s->status = BUSY_STATE;
|
alpar@9
|
779 }
|
alpar@9
|
780 }
|
alpar@9
|
781 else
|
alpar@9
|
782 s->status = BUSY_STATE;
|
alpar@9
|
783 }
|
alpar@9
|
784 #endif
|
alpar@9
|
785
|
alpar@9
|
786 /* Flush as much pending output as possible */
|
alpar@9
|
787 if (s->pending != 0) {
|
alpar@9
|
788 flush_pending(strm);
|
alpar@9
|
789 if (strm->avail_out == 0) {
|
alpar@9
|
790 /* Since avail_out is 0, deflate will be called again with
|
alpar@9
|
791 * more output space, but possibly with both pending and
|
alpar@9
|
792 * avail_in equal to zero. There won't be anything to do,
|
alpar@9
|
793 * but this is not an error situation so make sure we
|
alpar@9
|
794 * return OK instead of BUF_ERROR at next call of deflate:
|
alpar@9
|
795 */
|
alpar@9
|
796 s->last_flush = -1;
|
alpar@9
|
797 return Z_OK;
|
alpar@9
|
798 }
|
alpar@9
|
799
|
alpar@9
|
800 /* Make sure there is something to do and avoid duplicate consecutive
|
alpar@9
|
801 * flushes. For repeated and useless calls with Z_FINISH, we keep
|
alpar@9
|
802 * returning Z_STREAM_END instead of Z_BUF_ERROR.
|
alpar@9
|
803 */
|
alpar@9
|
804 } else if (strm->avail_in == 0 && flush <= old_flush &&
|
alpar@9
|
805 flush != Z_FINISH) {
|
alpar@9
|
806 ERR_RETURN(strm, Z_BUF_ERROR);
|
alpar@9
|
807 }
|
alpar@9
|
808
|
alpar@9
|
809 /* User must not provide more input after the first FINISH: */
|
alpar@9
|
810 if (s->status == FINISH_STATE && strm->avail_in != 0) {
|
alpar@9
|
811 ERR_RETURN(strm, Z_BUF_ERROR);
|
alpar@9
|
812 }
|
alpar@9
|
813
|
alpar@9
|
814 /* Start a new block or continue the current one.
|
alpar@9
|
815 */
|
alpar@9
|
816 if (strm->avail_in != 0 || s->lookahead != 0 ||
|
alpar@9
|
817 (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) {
|
alpar@9
|
818 block_state bstate;
|
alpar@9
|
819
|
alpar@9
|
820 bstate = s->strategy == Z_HUFFMAN_ONLY ? deflate_huff(s, flush) :
|
alpar@9
|
821 (s->strategy == Z_RLE ? deflate_rle(s, flush) :
|
alpar@9
|
822 (*(configuration_table[s->level].func))(s, flush));
|
alpar@9
|
823
|
alpar@9
|
824 if (bstate == finish_started || bstate == finish_done) {
|
alpar@9
|
825 s->status = FINISH_STATE;
|
alpar@9
|
826 }
|
alpar@9
|
827 if (bstate == need_more || bstate == finish_started) {
|
alpar@9
|
828 if (strm->avail_out == 0) {
|
alpar@9
|
829 s->last_flush = -1; /* avoid BUF_ERROR next call, see above */
|
alpar@9
|
830 }
|
alpar@9
|
831 return Z_OK;
|
alpar@9
|
832 /* If flush != Z_NO_FLUSH && avail_out == 0, the next call
|
alpar@9
|
833 * of deflate should use the same flush parameter to make sure
|
alpar@9
|
834 * that the flush is complete. So we don't have to output an
|
alpar@9
|
835 * empty block here, this will be done at next call. This also
|
alpar@9
|
836 * ensures that for a very small output buffer, we emit at most
|
alpar@9
|
837 * one empty block.
|
alpar@9
|
838 */
|
alpar@9
|
839 }
|
alpar@9
|
840 if (bstate == block_done) {
|
alpar@9
|
841 if (flush == Z_PARTIAL_FLUSH) {
|
alpar@9
|
842 _tr_align(s);
|
alpar@9
|
843 } else if (flush != Z_BLOCK) { /* FULL_FLUSH or SYNC_FLUSH */
|
alpar@9
|
844 _tr_stored_block(s, (char*)0, 0L, 0);
|
alpar@9
|
845 /* For a full flush, this empty block will be recognized
|
alpar@9
|
846 * as a special marker by inflate_sync().
|
alpar@9
|
847 */
|
alpar@9
|
848 if (flush == Z_FULL_FLUSH) {
|
alpar@9
|
849 CLEAR_HASH(s); /* forget history */
|
alpar@9
|
850 if (s->lookahead == 0) {
|
alpar@9
|
851 s->strstart = 0;
|
alpar@9
|
852 s->block_start = 0L;
|
alpar@9
|
853 }
|
alpar@9
|
854 }
|
alpar@9
|
855 }
|
alpar@9
|
856 flush_pending(strm);
|
alpar@9
|
857 if (strm->avail_out == 0) {
|
alpar@9
|
858 s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */
|
alpar@9
|
859 return Z_OK;
|
alpar@9
|
860 }
|
alpar@9
|
861 }
|
alpar@9
|
862 }
|
alpar@9
|
863 Assert(strm->avail_out > 0, "bug2");
|
alpar@9
|
864
|
alpar@9
|
865 if (flush != Z_FINISH) return Z_OK;
|
alpar@9
|
866 if (s->wrap <= 0) return Z_STREAM_END;
|
alpar@9
|
867
|
alpar@9
|
868 /* Write the trailer */
|
alpar@9
|
869 #ifdef GZIP
|
alpar@9
|
870 if (s->wrap == 2) {
|
alpar@9
|
871 put_byte(s, (Byte)(strm->adler & 0xff));
|
alpar@9
|
872 put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
|
alpar@9
|
873 put_byte(s, (Byte)((strm->adler >> 16) & 0xff));
|
alpar@9
|
874 put_byte(s, (Byte)((strm->adler >> 24) & 0xff));
|
alpar@9
|
875 put_byte(s, (Byte)(strm->total_in & 0xff));
|
alpar@9
|
876 put_byte(s, (Byte)((strm->total_in >> 8) & 0xff));
|
alpar@9
|
877 put_byte(s, (Byte)((strm->total_in >> 16) & 0xff));
|
alpar@9
|
878 put_byte(s, (Byte)((strm->total_in >> 24) & 0xff));
|
alpar@9
|
879 }
|
alpar@9
|
880 else
|
alpar@9
|
881 #endif
|
alpar@9
|
882 {
|
alpar@9
|
883 putShortMSB(s, (uInt)(strm->adler >> 16));
|
alpar@9
|
884 putShortMSB(s, (uInt)(strm->adler & 0xffff));
|
alpar@9
|
885 }
|
alpar@9
|
886 flush_pending(strm);
|
alpar@9
|
887 /* If avail_out is zero, the application will call deflate again
|
alpar@9
|
888 * to flush the rest.
|
alpar@9
|
889 */
|
alpar@9
|
890 if (s->wrap > 0) s->wrap = -s->wrap; /* write the trailer only once! */
|
alpar@9
|
891 return s->pending != 0 ? Z_OK : Z_STREAM_END;
|
alpar@9
|
892 }
|
alpar@9
|
893
|
alpar@9
|
894 /* ========================================================================= */
|
alpar@9
|
895 int ZEXPORT deflateEnd (strm)
|
alpar@9
|
896 z_streamp strm;
|
alpar@9
|
897 {
|
alpar@9
|
898 int status;
|
alpar@9
|
899
|
alpar@9
|
900 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
|
alpar@9
|
901
|
alpar@9
|
902 status = strm->state->status;
|
alpar@9
|
903 if (status != INIT_STATE &&
|
alpar@9
|
904 status != EXTRA_STATE &&
|
alpar@9
|
905 status != NAME_STATE &&
|
alpar@9
|
906 status != COMMENT_STATE &&
|
alpar@9
|
907 status != HCRC_STATE &&
|
alpar@9
|
908 status != BUSY_STATE &&
|
alpar@9
|
909 status != FINISH_STATE) {
|
alpar@9
|
910 return Z_STREAM_ERROR;
|
alpar@9
|
911 }
|
alpar@9
|
912
|
alpar@9
|
913 /* Deallocate in reverse order of allocations: */
|
alpar@9
|
914 TRY_FREE(strm, strm->state->pending_buf);
|
alpar@9
|
915 TRY_FREE(strm, strm->state->head);
|
alpar@9
|
916 TRY_FREE(strm, strm->state->prev);
|
alpar@9
|
917 TRY_FREE(strm, strm->state->window);
|
alpar@9
|
918
|
alpar@9
|
919 ZFREE(strm, strm->state);
|
alpar@9
|
920 strm->state = Z_NULL;
|
alpar@9
|
921
|
alpar@9
|
922 return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;
|
alpar@9
|
923 }
|
alpar@9
|
924
|
alpar@9
|
925 /* =========================================================================
|
alpar@9
|
926 * Copy the source state to the destination state.
|
alpar@9
|
927 * To simplify the source, this is not supported for 16-bit MSDOS (which
|
alpar@9
|
928 * doesn't have enough memory anyway to duplicate compression states).
|
alpar@9
|
929 */
|
alpar@9
|
930 int ZEXPORT deflateCopy (dest, source)
|
alpar@9
|
931 z_streamp dest;
|
alpar@9
|
932 z_streamp source;
|
alpar@9
|
933 {
|
alpar@9
|
934 #ifdef MAXSEG_64K
|
alpar@9
|
935 return Z_STREAM_ERROR;
|
alpar@9
|
936 #else
|
alpar@9
|
937 deflate_state *ds;
|
alpar@9
|
938 deflate_state *ss;
|
alpar@9
|
939 ushf *overlay;
|
alpar@9
|
940
|
alpar@9
|
941
|
alpar@9
|
942 if (source == Z_NULL || dest == Z_NULL || source->state == Z_NULL) {
|
alpar@9
|
943 return Z_STREAM_ERROR;
|
alpar@9
|
944 }
|
alpar@9
|
945
|
alpar@9
|
946 ss = source->state;
|
alpar@9
|
947
|
alpar@9
|
948 zmemcpy(dest, source, sizeof(z_stream));
|
alpar@9
|
949
|
alpar@9
|
950 ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state));
|
alpar@9
|
951 if (ds == Z_NULL) return Z_MEM_ERROR;
|
alpar@9
|
952 dest->state = (struct internal_state FAR *) ds;
|
alpar@9
|
953 zmemcpy(ds, ss, sizeof(deflate_state));
|
alpar@9
|
954 ds->strm = dest;
|
alpar@9
|
955
|
alpar@9
|
956 ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte));
|
alpar@9
|
957 ds->prev = (Posf *) ZALLOC(dest, ds->w_size, sizeof(Pos));
|
alpar@9
|
958 ds->head = (Posf *) ZALLOC(dest, ds->hash_size, sizeof(Pos));
|
alpar@9
|
959 overlay = (ushf *) ZALLOC(dest, ds->lit_bufsize, sizeof(ush)+2);
|
alpar@9
|
960 ds->pending_buf = (uchf *) overlay;
|
alpar@9
|
961
|
alpar@9
|
962 if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL ||
|
alpar@9
|
963 ds->pending_buf == Z_NULL) {
|
alpar@9
|
964 deflateEnd (dest);
|
alpar@9
|
965 return Z_MEM_ERROR;
|
alpar@9
|
966 }
|
alpar@9
|
967 /* following zmemcpy do not work for 16-bit MSDOS */
|
alpar@9
|
968 zmemcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(Byte));
|
alpar@9
|
969 zmemcpy(ds->prev, ss->prev, ds->w_size * sizeof(Pos));
|
alpar@9
|
970 zmemcpy(ds->head, ss->head, ds->hash_size * sizeof(Pos));
|
alpar@9
|
971 zmemcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size);
|
alpar@9
|
972
|
alpar@9
|
973 ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf);
|
alpar@9
|
974 ds->d_buf = overlay + ds->lit_bufsize/sizeof(ush);
|
alpar@9
|
975 ds->l_buf = ds->pending_buf + (1+sizeof(ush))*ds->lit_bufsize;
|
alpar@9
|
976
|
alpar@9
|
977 ds->l_desc.dyn_tree = ds->dyn_ltree;
|
alpar@9
|
978 ds->d_desc.dyn_tree = ds->dyn_dtree;
|
alpar@9
|
979 ds->bl_desc.dyn_tree = ds->bl_tree;
|
alpar@9
|
980
|
alpar@9
|
981 return Z_OK;
|
alpar@9
|
982 #endif /* MAXSEG_64K */
|
alpar@9
|
983 }
|
alpar@9
|
984
|
alpar@9
|
985 /* ===========================================================================
|
alpar@9
|
986 * Read a new buffer from the current input stream, update the adler32
|
alpar@9
|
987 * and total number of bytes read. All deflate() input goes through
|
alpar@9
|
988 * this function so some applications may wish to modify it to avoid
|
alpar@9
|
989 * allocating a large strm->next_in buffer and copying from it.
|
alpar@9
|
990 * (See also flush_pending()).
|
alpar@9
|
991 */
|
alpar@9
|
992 local int read_buf(strm, buf, size)
|
alpar@9
|
993 z_streamp strm;
|
alpar@9
|
994 Bytef *buf;
|
alpar@9
|
995 unsigned size;
|
alpar@9
|
996 {
|
alpar@9
|
997 unsigned len = strm->avail_in;
|
alpar@9
|
998
|
alpar@9
|
999 if (len > size) len = size;
|
alpar@9
|
1000 if (len == 0) return 0;
|
alpar@9
|
1001
|
alpar@9
|
1002 strm->avail_in -= len;
|
alpar@9
|
1003
|
alpar@9
|
1004 if (strm->state->wrap == 1) {
|
alpar@9
|
1005 strm->adler = adler32(strm->adler, strm->next_in, len);
|
alpar@9
|
1006 }
|
alpar@9
|
1007 #ifdef GZIP
|
alpar@9
|
1008 else if (strm->state->wrap == 2) {
|
alpar@9
|
1009 strm->adler = crc32(strm->adler, strm->next_in, len);
|
alpar@9
|
1010 }
|
alpar@9
|
1011 #endif
|
alpar@9
|
1012 zmemcpy(buf, strm->next_in, len);
|
alpar@9
|
1013 strm->next_in += len;
|
alpar@9
|
1014 strm->total_in += len;
|
alpar@9
|
1015
|
alpar@9
|
1016 return (int)len;
|
alpar@9
|
1017 }
|
alpar@9
|
1018
|
alpar@9
|
1019 /* ===========================================================================
|
alpar@9
|
1020 * Initialize the "longest match" routines for a new zlib stream
|
alpar@9
|
1021 */
|
alpar@9
|
1022 local void lm_init (s)
|
alpar@9
|
1023 deflate_state *s;
|
alpar@9
|
1024 {
|
alpar@9
|
1025 s->window_size = (ulg)2L*s->w_size;
|
alpar@9
|
1026
|
alpar@9
|
1027 CLEAR_HASH(s);
|
alpar@9
|
1028
|
alpar@9
|
1029 /* Set the default configuration parameters:
|
alpar@9
|
1030 */
|
alpar@9
|
1031 s->max_lazy_match = configuration_table[s->level].max_lazy;
|
alpar@9
|
1032 s->good_match = configuration_table[s->level].good_length;
|
alpar@9
|
1033 s->nice_match = configuration_table[s->level].nice_length;
|
alpar@9
|
1034 s->max_chain_length = configuration_table[s->level].max_chain;
|
alpar@9
|
1035
|
alpar@9
|
1036 s->strstart = 0;
|
alpar@9
|
1037 s->block_start = 0L;
|
alpar@9
|
1038 s->lookahead = 0;
|
alpar@9
|
1039 s->match_length = s->prev_length = MIN_MATCH-1;
|
alpar@9
|
1040 s->match_available = 0;
|
alpar@9
|
1041 s->ins_h = 0;
|
alpar@9
|
1042 #ifndef FASTEST
|
alpar@9
|
1043 #ifdef ASMV
|
alpar@9
|
1044 match_init(); /* initialize the asm code */
|
alpar@9
|
1045 #endif
|
alpar@9
|
1046 #endif
|
alpar@9
|
1047 }
|
alpar@9
|
1048
|
alpar@9
|
1049 #ifndef FASTEST
|
alpar@9
|
1050 /* ===========================================================================
|
alpar@9
|
1051 * Set match_start to the longest match starting at the given string and
|
alpar@9
|
1052 * return its length. Matches shorter or equal to prev_length are discarded,
|
alpar@9
|
1053 * in which case the result is equal to prev_length and match_start is
|
alpar@9
|
1054 * garbage.
|
alpar@9
|
1055 * IN assertions: cur_match is the head of the hash chain for the current
|
alpar@9
|
1056 * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
|
alpar@9
|
1057 * OUT assertion: the match length is not greater than s->lookahead.
|
alpar@9
|
1058 */
|
alpar@9
|
1059 #ifndef ASMV
|
alpar@9
|
1060 /* For 80x86 and 680x0, an optimized version will be provided in match.asm or
|
alpar@9
|
1061 * match.S. The code will be functionally equivalent.
|
alpar@9
|
1062 */
|
alpar@9
|
1063 local uInt longest_match(s, cur_match)
|
alpar@9
|
1064 deflate_state *s;
|
alpar@9
|
1065 IPos cur_match; /* current match */
|
alpar@9
|
1066 {
|
alpar@9
|
1067 unsigned chain_length = s->max_chain_length;/* max hash chain length */
|
alpar@9
|
1068 register Bytef *scan = s->window + s->strstart; /* current string */
|
alpar@9
|
1069 register Bytef *match; /* matched string */
|
alpar@9
|
1070 register int len; /* length of current match */
|
alpar@9
|
1071 int best_len = s->prev_length; /* best match length so far */
|
alpar@9
|
1072 int nice_match = s->nice_match; /* stop if match long enough */
|
alpar@9
|
1073 IPos limit = s->strstart > (IPos)MAX_DIST(s) ?
|
alpar@9
|
1074 s->strstart - (IPos)MAX_DIST(s) : NIL;
|
alpar@9
|
1075 /* Stop when cur_match becomes <= limit. To simplify the code,
|
alpar@9
|
1076 * we prevent matches with the string of window index 0.
|
alpar@9
|
1077 */
|
alpar@9
|
1078 Posf *prev = s->prev;
|
alpar@9
|
1079 uInt wmask = s->w_mask;
|
alpar@9
|
1080
|
alpar@9
|
1081 #ifdef UNALIGNED_OK
|
alpar@9
|
1082 /* Compare two bytes at a time. Note: this is not always beneficial.
|
alpar@9
|
1083 * Try with and without -DUNALIGNED_OK to check.
|
alpar@9
|
1084 */
|
alpar@9
|
1085 register Bytef *strend = s->window + s->strstart + MAX_MATCH - 1;
|
alpar@9
|
1086 register ush scan_start = *(ushf*)scan;
|
alpar@9
|
1087 register ush scan_end = *(ushf*)(scan+best_len-1);
|
alpar@9
|
1088 #else
|
alpar@9
|
1089 register Bytef *strend = s->window + s->strstart + MAX_MATCH;
|
alpar@9
|
1090 register Byte scan_end1 = scan[best_len-1];
|
alpar@9
|
1091 register Byte scan_end = scan[best_len];
|
alpar@9
|
1092 #endif
|
alpar@9
|
1093
|
alpar@9
|
1094 /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
|
alpar@9
|
1095 * It is easy to get rid of this optimization if necessary.
|
alpar@9
|
1096 */
|
alpar@9
|
1097 Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
|
alpar@9
|
1098
|
alpar@9
|
1099 /* Do not waste too much time if we already have a good match: */
|
alpar@9
|
1100 if (s->prev_length >= s->good_match) {
|
alpar@9
|
1101 chain_length >>= 2;
|
alpar@9
|
1102 }
|
alpar@9
|
1103 /* Do not look for matches beyond the end of the input. This is necessary
|
alpar@9
|
1104 * to make deflate deterministic.
|
alpar@9
|
1105 */
|
alpar@9
|
1106 if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead;
|
alpar@9
|
1107
|
alpar@9
|
1108 Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
|
alpar@9
|
1109
|
alpar@9
|
1110 do {
|
alpar@9
|
1111 Assert(cur_match < s->strstart, "no future");
|
alpar@9
|
1112 match = s->window + cur_match;
|
alpar@9
|
1113
|
alpar@9
|
1114 /* Skip to next match if the match length cannot increase
|
alpar@9
|
1115 * or if the match length is less than 2. Note that the checks below
|
alpar@9
|
1116 * for insufficient lookahead only occur occasionally for performance
|
alpar@9
|
1117 * reasons. Therefore uninitialized memory will be accessed, and
|
alpar@9
|
1118 * conditional jumps will be made that depend on those values.
|
alpar@9
|
1119 * However the length of the match is limited to the lookahead, so
|
alpar@9
|
1120 * the output of deflate is not affected by the uninitialized values.
|
alpar@9
|
1121 */
|
alpar@9
|
1122 #if (defined(UNALIGNED_OK) && MAX_MATCH == 258)
|
alpar@9
|
1123 /* This code assumes sizeof(unsigned short) == 2. Do not use
|
alpar@9
|
1124 * UNALIGNED_OK if your compiler uses a different size.
|
alpar@9
|
1125 */
|
alpar@9
|
1126 if (*(ushf*)(match+best_len-1) != scan_end ||
|
alpar@9
|
1127 *(ushf*)match != scan_start) continue;
|
alpar@9
|
1128
|
alpar@9
|
1129 /* It is not necessary to compare scan[2] and match[2] since they are
|
alpar@9
|
1130 * always equal when the other bytes match, given that the hash keys
|
alpar@9
|
1131 * are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at
|
alpar@9
|
1132 * strstart+3, +5, ... up to strstart+257. We check for insufficient
|
alpar@9
|
1133 * lookahead only every 4th comparison; the 128th check will be made
|
alpar@9
|
1134 * at strstart+257. If MAX_MATCH-2 is not a multiple of 8, it is
|
alpar@9
|
1135 * necessary to put more guard bytes at the end of the window, or
|
alpar@9
|
1136 * to check more often for insufficient lookahead.
|
alpar@9
|
1137 */
|
alpar@9
|
1138 Assert(scan[2] == match[2], "scan[2]?");
|
alpar@9
|
1139 scan++, match++;
|
alpar@9
|
1140 do {
|
alpar@9
|
1141 } while (*(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
|
alpar@9
|
1142 *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
|
alpar@9
|
1143 *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
|
alpar@9
|
1144 *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
|
alpar@9
|
1145 scan < strend);
|
alpar@9
|
1146 /* The funny "do {}" generates better code on most compilers */
|
alpar@9
|
1147
|
alpar@9
|
1148 /* Here, scan <= window+strstart+257 */
|
alpar@9
|
1149 Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
|
alpar@9
|
1150 if (*scan == *match) scan++;
|
alpar@9
|
1151
|
alpar@9
|
1152 len = (MAX_MATCH - 1) - (int)(strend-scan);
|
alpar@9
|
1153 scan = strend - (MAX_MATCH-1);
|
alpar@9
|
1154
|
alpar@9
|
1155 #else /* UNALIGNED_OK */
|
alpar@9
|
1156
|
alpar@9
|
1157 if (match[best_len] != scan_end ||
|
alpar@9
|
1158 match[best_len-1] != scan_end1 ||
|
alpar@9
|
1159 *match != *scan ||
|
alpar@9
|
1160 *++match != scan[1]) continue;
|
alpar@9
|
1161
|
alpar@9
|
1162 /* The check at best_len-1 can be removed because it will be made
|
alpar@9
|
1163 * again later. (This heuristic is not always a win.)
|
alpar@9
|
1164 * It is not necessary to compare scan[2] and match[2] since they
|
alpar@9
|
1165 * are always equal when the other bytes match, given that
|
alpar@9
|
1166 * the hash keys are equal and that HASH_BITS >= 8.
|
alpar@9
|
1167 */
|
alpar@9
|
1168 scan += 2, match++;
|
alpar@9
|
1169 Assert(*scan == *match, "match[2]?");
|
alpar@9
|
1170
|
alpar@9
|
1171 /* We check for insufficient lookahead only every 8th comparison;
|
alpar@9
|
1172 * the 256th check will be made at strstart+258.
|
alpar@9
|
1173 */
|
alpar@9
|
1174 do {
|
alpar@9
|
1175 } while (*++scan == *++match && *++scan == *++match &&
|
alpar@9
|
1176 *++scan == *++match && *++scan == *++match &&
|
alpar@9
|
1177 *++scan == *++match && *++scan == *++match &&
|
alpar@9
|
1178 *++scan == *++match && *++scan == *++match &&
|
alpar@9
|
1179 scan < strend);
|
alpar@9
|
1180
|
alpar@9
|
1181 Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
|
alpar@9
|
1182
|
alpar@9
|
1183 len = MAX_MATCH - (int)(strend - scan);
|
alpar@9
|
1184 scan = strend - MAX_MATCH;
|
alpar@9
|
1185
|
alpar@9
|
1186 #endif /* UNALIGNED_OK */
|
alpar@9
|
1187
|
alpar@9
|
1188 if (len > best_len) {
|
alpar@9
|
1189 s->match_start = cur_match;
|
alpar@9
|
1190 best_len = len;
|
alpar@9
|
1191 if (len >= nice_match) break;
|
alpar@9
|
1192 #ifdef UNALIGNED_OK
|
alpar@9
|
1193 scan_end = *(ushf*)(scan+best_len-1);
|
alpar@9
|
1194 #else
|
alpar@9
|
1195 scan_end1 = scan[best_len-1];
|
alpar@9
|
1196 scan_end = scan[best_len];
|
alpar@9
|
1197 #endif
|
alpar@9
|
1198 }
|
alpar@9
|
1199 } while ((cur_match = prev[cur_match & wmask]) > limit
|
alpar@9
|
1200 && --chain_length != 0);
|
alpar@9
|
1201
|
alpar@9
|
1202 if ((uInt)best_len <= s->lookahead) return (uInt)best_len;
|
alpar@9
|
1203 return s->lookahead;
|
alpar@9
|
1204 }
|
alpar@9
|
1205 #endif /* ASMV */
|
alpar@9
|
1206
|
alpar@9
|
1207 #else /* FASTEST */
|
alpar@9
|
1208
|
alpar@9
|
1209 /* ---------------------------------------------------------------------------
|
alpar@9
|
1210 * Optimized version for FASTEST only
|
alpar@9
|
1211 */
|
alpar@9
|
1212 local uInt longest_match(s, cur_match)
|
alpar@9
|
1213 deflate_state *s;
|
alpar@9
|
1214 IPos cur_match; /* current match */
|
alpar@9
|
1215 {
|
alpar@9
|
1216 register Bytef *scan = s->window + s->strstart; /* current string */
|
alpar@9
|
1217 register Bytef *match; /* matched string */
|
alpar@9
|
1218 register int len; /* length of current match */
|
alpar@9
|
1219 register Bytef *strend = s->window + s->strstart + MAX_MATCH;
|
alpar@9
|
1220
|
alpar@9
|
1221 /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
|
alpar@9
|
1222 * It is easy to get rid of this optimization if necessary.
|
alpar@9
|
1223 */
|
alpar@9
|
1224 Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
|
alpar@9
|
1225
|
alpar@9
|
1226 Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
|
alpar@9
|
1227
|
alpar@9
|
1228 Assert(cur_match < s->strstart, "no future");
|
alpar@9
|
1229
|
alpar@9
|
1230 match = s->window + cur_match;
|
alpar@9
|
1231
|
alpar@9
|
1232 /* Return failure if the match length is less than 2:
|
alpar@9
|
1233 */
|
alpar@9
|
1234 if (match[0] != scan[0] || match[1] != scan[1]) return MIN_MATCH-1;
|
alpar@9
|
1235
|
alpar@9
|
1236 /* The check at best_len-1 can be removed because it will be made
|
alpar@9
|
1237 * again later. (This heuristic is not always a win.)
|
alpar@9
|
1238 * It is not necessary to compare scan[2] and match[2] since they
|
alpar@9
|
1239 * are always equal when the other bytes match, given that
|
alpar@9
|
1240 * the hash keys are equal and that HASH_BITS >= 8.
|
alpar@9
|
1241 */
|
alpar@9
|
1242 scan += 2, match += 2;
|
alpar@9
|
1243 Assert(*scan == *match, "match[2]?");
|
alpar@9
|
1244
|
alpar@9
|
1245 /* We check for insufficient lookahead only every 8th comparison;
|
alpar@9
|
1246 * the 256th check will be made at strstart+258.
|
alpar@9
|
1247 */
|
alpar@9
|
1248 do {
|
alpar@9
|
1249 } while (*++scan == *++match && *++scan == *++match &&
|
alpar@9
|
1250 *++scan == *++match && *++scan == *++match &&
|
alpar@9
|
1251 *++scan == *++match && *++scan == *++match &&
|
alpar@9
|
1252 *++scan == *++match && *++scan == *++match &&
|
alpar@9
|
1253 scan < strend);
|
alpar@9
|
1254
|
alpar@9
|
1255 Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
|
alpar@9
|
1256
|
alpar@9
|
1257 len = MAX_MATCH - (int)(strend - scan);
|
alpar@9
|
1258
|
alpar@9
|
1259 if (len < MIN_MATCH) return MIN_MATCH - 1;
|
alpar@9
|
1260
|
alpar@9
|
1261 s->match_start = cur_match;
|
alpar@9
|
1262 return (uInt)len <= s->lookahead ? (uInt)len : s->lookahead;
|
alpar@9
|
1263 }
|
alpar@9
|
1264
|
alpar@9
|
1265 #endif /* FASTEST */
|
alpar@9
|
1266
|
alpar@9
|
1267 #ifdef DEBUG
|
alpar@9
|
1268 /* ===========================================================================
|
alpar@9
|
1269 * Check that the match at match_start is indeed a match.
|
alpar@9
|
1270 */
|
alpar@9
|
1271 local void check_match(s, start, match, length)
|
alpar@9
|
1272 deflate_state *s;
|
alpar@9
|
1273 IPos start, match;
|
alpar@9
|
1274 int length;
|
alpar@9
|
1275 {
|
alpar@9
|
1276 /* check that the match is indeed a match */
|
alpar@9
|
1277 if (zmemcmp(s->window + match,
|
alpar@9
|
1278 s->window + start, length) != EQUAL) {
|
alpar@9
|
1279 fprintf(stderr, " start %u, match %u, length %d\n",
|
alpar@9
|
1280 start, match, length);
|
alpar@9
|
1281 do {
|
alpar@9
|
1282 fprintf(stderr, "%c%c", s->window[match++], s->window[start++]);
|
alpar@9
|
1283 } while (--length != 0);
|
alpar@9
|
1284 z_error("invalid match");
|
alpar@9
|
1285 }
|
alpar@9
|
1286 if (z_verbose > 1) {
|
alpar@9
|
1287 fprintf(stderr,"\\[%d,%d]", start-match, length);
|
alpar@9
|
1288 do { putc(s->window[start++], stderr); } while (--length != 0);
|
alpar@9
|
1289 }
|
alpar@9
|
1290 }
|
alpar@9
|
1291 #else
|
alpar@9
|
1292 # define check_match(s, start, match, length)
|
alpar@9
|
1293 #endif /* DEBUG */
|
alpar@9
|
1294
|
alpar@9
|
1295 /* ===========================================================================
|
alpar@9
|
1296 * Fill the window when the lookahead becomes insufficient.
|
alpar@9
|
1297 * Updates strstart and lookahead.
|
alpar@9
|
1298 *
|
alpar@9
|
1299 * IN assertion: lookahead < MIN_LOOKAHEAD
|
alpar@9
|
1300 * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
|
alpar@9
|
1301 * At least one byte has been read, or avail_in == 0; reads are
|
alpar@9
|
1302 * performed for at least two bytes (required for the zip translate_eol
|
alpar@9
|
1303 * option -- not supported here).
|
alpar@9
|
1304 */
|
alpar@9
|
1305 local void fill_window(s)
|
alpar@9
|
1306 deflate_state *s;
|
alpar@9
|
1307 {
|
alpar@9
|
1308 register unsigned n, m;
|
alpar@9
|
1309 register Posf *p;
|
alpar@9
|
1310 unsigned more; /* Amount of free space at the end of the window. */
|
alpar@9
|
1311 uInt wsize = s->w_size;
|
alpar@9
|
1312
|
alpar@9
|
1313 do {
|
alpar@9
|
1314 more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart);
|
alpar@9
|
1315
|
alpar@9
|
1316 /* Deal with !@#$% 64K limit: */
|
alpar@9
|
1317 if (sizeof(int) <= 2) {
|
alpar@9
|
1318 if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
|
alpar@9
|
1319 more = wsize;
|
alpar@9
|
1320
|
alpar@9
|
1321 } else if (more == (unsigned)(-1)) {
|
alpar@9
|
1322 /* Very unlikely, but possible on 16 bit machine if
|
alpar@9
|
1323 * strstart == 0 && lookahead == 1 (input done a byte at time)
|
alpar@9
|
1324 */
|
alpar@9
|
1325 more--;
|
alpar@9
|
1326 }
|
alpar@9
|
1327 }
|
alpar@9
|
1328
|
alpar@9
|
1329 /* If the window is almost full and there is insufficient lookahead,
|
alpar@9
|
1330 * move the upper half to the lower one to make room in the upper half.
|
alpar@9
|
1331 */
|
alpar@9
|
1332 if (s->strstart >= wsize+MAX_DIST(s)) {
|
alpar@9
|
1333
|
alpar@9
|
1334 zmemcpy(s->window, s->window+wsize, (unsigned)wsize);
|
alpar@9
|
1335 s->match_start -= wsize;
|
alpar@9
|
1336 s->strstart -= wsize; /* we now have strstart >= MAX_DIST */
|
alpar@9
|
1337 s->block_start -= (long) wsize;
|
alpar@9
|
1338
|
alpar@9
|
1339 /* Slide the hash table (could be avoided with 32 bit values
|
alpar@9
|
1340 at the expense of memory usage). We slide even when level == 0
|
alpar@9
|
1341 to keep the hash table consistent if we switch back to level > 0
|
alpar@9
|
1342 later. (Using level 0 permanently is not an optimal usage of
|
alpar@9
|
1343 zlib, so we don't care about this pathological case.)
|
alpar@9
|
1344 */
|
alpar@9
|
1345 n = s->hash_size;
|
alpar@9
|
1346 p = &s->head[n];
|
alpar@9
|
1347 do {
|
alpar@9
|
1348 m = *--p;
|
alpar@9
|
1349 *p = (Pos)(m >= wsize ? m-wsize : NIL);
|
alpar@9
|
1350 } while (--n);
|
alpar@9
|
1351
|
alpar@9
|
1352 n = wsize;
|
alpar@9
|
1353 #ifndef FASTEST
|
alpar@9
|
1354 p = &s->prev[n];
|
alpar@9
|
1355 do {
|
alpar@9
|
1356 m = *--p;
|
alpar@9
|
1357 *p = (Pos)(m >= wsize ? m-wsize : NIL);
|
alpar@9
|
1358 /* If n is not on any hash chain, prev[n] is garbage but
|
alpar@9
|
1359 * its value will never be used.
|
alpar@9
|
1360 */
|
alpar@9
|
1361 } while (--n);
|
alpar@9
|
1362 #endif
|
alpar@9
|
1363 more += wsize;
|
alpar@9
|
1364 }
|
alpar@9
|
1365 if (s->strm->avail_in == 0) return;
|
alpar@9
|
1366
|
alpar@9
|
1367 /* If there was no sliding:
|
alpar@9
|
1368 * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
|
alpar@9
|
1369 * more == window_size - lookahead - strstart
|
alpar@9
|
1370 * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
|
alpar@9
|
1371 * => more >= window_size - 2*WSIZE + 2
|
alpar@9
|
1372 * In the BIG_MEM or MMAP case (not yet supported),
|
alpar@9
|
1373 * window_size == input_size + MIN_LOOKAHEAD &&
|
alpar@9
|
1374 * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
|
alpar@9
|
1375 * Otherwise, window_size == 2*WSIZE so more >= 2.
|
alpar@9
|
1376 * If there was sliding, more >= WSIZE. So in all cases, more >= 2.
|
alpar@9
|
1377 */
|
alpar@9
|
1378 Assert(more >= 2, "more < 2");
|
alpar@9
|
1379
|
alpar@9
|
1380 n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more);
|
alpar@9
|
1381 s->lookahead += n;
|
alpar@9
|
1382
|
alpar@9
|
1383 /* Initialize the hash value now that we have some input: */
|
alpar@9
|
1384 if (s->lookahead >= MIN_MATCH) {
|
alpar@9
|
1385 s->ins_h = s->window[s->strstart];
|
alpar@9
|
1386 UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
|
alpar@9
|
1387 #if MIN_MATCH != 3
|
alpar@9
|
1388 Call UPDATE_HASH() MIN_MATCH-3 more times
|
alpar@9
|
1389 #endif
|
alpar@9
|
1390 }
|
alpar@9
|
1391 /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
|
alpar@9
|
1392 * but this is not important since only literal bytes will be emitted.
|
alpar@9
|
1393 */
|
alpar@9
|
1394
|
alpar@9
|
1395 } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0);
|
alpar@9
|
1396
|
alpar@9
|
1397 /* If the WIN_INIT bytes after the end of the current data have never been
|
alpar@9
|
1398 * written, then zero those bytes in order to avoid memory check reports of
|
alpar@9
|
1399 * the use of uninitialized (or uninitialised as Julian writes) bytes by
|
alpar@9
|
1400 * the longest match routines. Update the high water mark for the next
|
alpar@9
|
1401 * time through here. WIN_INIT is set to MAX_MATCH since the longest match
|
alpar@9
|
1402 * routines allow scanning to strstart + MAX_MATCH, ignoring lookahead.
|
alpar@9
|
1403 */
|
alpar@9
|
1404 if (s->high_water < s->window_size) {
|
alpar@9
|
1405 ulg curr = s->strstart + (ulg)(s->lookahead);
|
alpar@9
|
1406 ulg init;
|
alpar@9
|
1407
|
alpar@9
|
1408 if (s->high_water < curr) {
|
alpar@9
|
1409 /* Previous high water mark below current data -- zero WIN_INIT
|
alpar@9
|
1410 * bytes or up to end of window, whichever is less.
|
alpar@9
|
1411 */
|
alpar@9
|
1412 init = s->window_size - curr;
|
alpar@9
|
1413 if (init > WIN_INIT)
|
alpar@9
|
1414 init = WIN_INIT;
|
alpar@9
|
1415 zmemzero(s->window + curr, (unsigned)init);
|
alpar@9
|
1416 s->high_water = curr + init;
|
alpar@9
|
1417 }
|
alpar@9
|
1418 else if (s->high_water < (ulg)curr + WIN_INIT) {
|
alpar@9
|
1419 /* High water mark at or above current data, but below current data
|
alpar@9
|
1420 * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up
|
alpar@9
|
1421 * to end of window, whichever is less.
|
alpar@9
|
1422 */
|
alpar@9
|
1423 init = (ulg)curr + WIN_INIT - s->high_water;
|
alpar@9
|
1424 if (init > s->window_size - s->high_water)
|
alpar@9
|
1425 init = s->window_size - s->high_water;
|
alpar@9
|
1426 zmemzero(s->window + s->high_water, (unsigned)init);
|
alpar@9
|
1427 s->high_water += init;
|
alpar@9
|
1428 }
|
alpar@9
|
1429 }
|
alpar@9
|
1430 }
|
alpar@9
|
1431
|
alpar@9
|
1432 /* ===========================================================================
|
alpar@9
|
1433 * Flush the current block, with given end-of-file flag.
|
alpar@9
|
1434 * IN assertion: strstart is set to the end of the current match.
|
alpar@9
|
1435 */
|
alpar@9
|
1436 #define FLUSH_BLOCK_ONLY(s, last) { \
|
alpar@9
|
1437 _tr_flush_block(s, (s->block_start >= 0L ? \
|
alpar@9
|
1438 (charf *)&s->window[(unsigned)s->block_start] : \
|
alpar@9
|
1439 (charf *)Z_NULL), \
|
alpar@9
|
1440 (ulg)((long)s->strstart - s->block_start), \
|
alpar@9
|
1441 (last)); \
|
alpar@9
|
1442 s->block_start = s->strstart; \
|
alpar@9
|
1443 flush_pending(s->strm); \
|
alpar@9
|
1444 Tracev((stderr,"[FLUSH]")); \
|
alpar@9
|
1445 }
|
alpar@9
|
1446
|
alpar@9
|
1447 /* Same but force premature exit if necessary. */
|
alpar@9
|
1448 #define FLUSH_BLOCK(s, last) { \
|
alpar@9
|
1449 FLUSH_BLOCK_ONLY(s, last); \
|
alpar@9
|
1450 if (s->strm->avail_out == 0) return (last) ? finish_started : need_more; \
|
alpar@9
|
1451 }
|
alpar@9
|
1452
|
alpar@9
|
1453 /* ===========================================================================
|
alpar@9
|
1454 * Copy without compression as much as possible from the input stream, return
|
alpar@9
|
1455 * the current block state.
|
alpar@9
|
1456 * This function does not insert new strings in the dictionary since
|
alpar@9
|
1457 * uncompressible data is probably not useful. This function is used
|
alpar@9
|
1458 * only for the level=0 compression option.
|
alpar@9
|
1459 * NOTE: this function should be optimized to avoid extra copying from
|
alpar@9
|
1460 * window to pending_buf.
|
alpar@9
|
1461 */
|
alpar@9
|
1462 local block_state deflate_stored(s, flush)
|
alpar@9
|
1463 deflate_state *s;
|
alpar@9
|
1464 int flush;
|
alpar@9
|
1465 {
|
alpar@9
|
1466 /* Stored blocks are limited to 0xffff bytes, pending_buf is limited
|
alpar@9
|
1467 * to pending_buf_size, and each stored block has a 5 byte header:
|
alpar@9
|
1468 */
|
alpar@9
|
1469 ulg max_block_size = 0xffff;
|
alpar@9
|
1470 ulg max_start;
|
alpar@9
|
1471
|
alpar@9
|
1472 if (max_block_size > s->pending_buf_size - 5) {
|
alpar@9
|
1473 max_block_size = s->pending_buf_size - 5;
|
alpar@9
|
1474 }
|
alpar@9
|
1475
|
alpar@9
|
1476 /* Copy as much as possible from input to output: */
|
alpar@9
|
1477 for (;;) {
|
alpar@9
|
1478 /* Fill the window as much as possible: */
|
alpar@9
|
1479 if (s->lookahead <= 1) {
|
alpar@9
|
1480
|
alpar@9
|
1481 Assert(s->strstart < s->w_size+MAX_DIST(s) ||
|
alpar@9
|
1482 s->block_start >= (long)s->w_size, "slide too late");
|
alpar@9
|
1483
|
alpar@9
|
1484 fill_window(s);
|
alpar@9
|
1485 if (s->lookahead == 0 && flush == Z_NO_FLUSH) return need_more;
|
alpar@9
|
1486
|
alpar@9
|
1487 if (s->lookahead == 0) break; /* flush the current block */
|
alpar@9
|
1488 }
|
alpar@9
|
1489 Assert(s->block_start >= 0L, "block gone");
|
alpar@9
|
1490
|
alpar@9
|
1491 s->strstart += s->lookahead;
|
alpar@9
|
1492 s->lookahead = 0;
|
alpar@9
|
1493
|
alpar@9
|
1494 /* Emit a stored block if pending_buf will be full: */
|
alpar@9
|
1495 max_start = s->block_start + max_block_size;
|
alpar@9
|
1496 if (s->strstart == 0 || (ulg)s->strstart >= max_start) {
|
alpar@9
|
1497 /* strstart == 0 is possible when wraparound on 16-bit machine */
|
alpar@9
|
1498 s->lookahead = (uInt)(s->strstart - max_start);
|
alpar@9
|
1499 s->strstart = (uInt)max_start;
|
alpar@9
|
1500 FLUSH_BLOCK(s, 0);
|
alpar@9
|
1501 }
|
alpar@9
|
1502 /* Flush if we may have to slide, otherwise block_start may become
|
alpar@9
|
1503 * negative and the data will be gone:
|
alpar@9
|
1504 */
|
alpar@9
|
1505 if (s->strstart - (uInt)s->block_start >= MAX_DIST(s)) {
|
alpar@9
|
1506 FLUSH_BLOCK(s, 0);
|
alpar@9
|
1507 }
|
alpar@9
|
1508 }
|
alpar@9
|
1509 FLUSH_BLOCK(s, flush == Z_FINISH);
|
alpar@9
|
1510 return flush == Z_FINISH ? finish_done : block_done;
|
alpar@9
|
1511 }
|
alpar@9
|
1512
|
alpar@9
|
1513 /* ===========================================================================
|
alpar@9
|
1514 * Compress as much as possible from the input stream, return the current
|
alpar@9
|
1515 * block state.
|
alpar@9
|
1516 * This function does not perform lazy evaluation of matches and inserts
|
alpar@9
|
1517 * new strings in the dictionary only for unmatched strings or for short
|
alpar@9
|
1518 * matches. It is used only for the fast compression options.
|
alpar@9
|
1519 */
|
alpar@9
|
1520 local block_state deflate_fast(s, flush)
|
alpar@9
|
1521 deflate_state *s;
|
alpar@9
|
1522 int flush;
|
alpar@9
|
1523 {
|
alpar@9
|
1524 IPos hash_head; /* head of the hash chain */
|
alpar@9
|
1525 int bflush; /* set if current block must be flushed */
|
alpar@9
|
1526
|
alpar@9
|
1527 for (;;) {
|
alpar@9
|
1528 /* Make sure that we always have enough lookahead, except
|
alpar@9
|
1529 * at the end of the input file. We need MAX_MATCH bytes
|
alpar@9
|
1530 * for the next match, plus MIN_MATCH bytes to insert the
|
alpar@9
|
1531 * string following the next match.
|
alpar@9
|
1532 */
|
alpar@9
|
1533 if (s->lookahead < MIN_LOOKAHEAD) {
|
alpar@9
|
1534 fill_window(s);
|
alpar@9
|
1535 if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
|
alpar@9
|
1536 return need_more;
|
alpar@9
|
1537 }
|
alpar@9
|
1538 if (s->lookahead == 0) break; /* flush the current block */
|
alpar@9
|
1539 }
|
alpar@9
|
1540
|
alpar@9
|
1541 /* Insert the string window[strstart .. strstart+2] in the
|
alpar@9
|
1542 * dictionary, and set hash_head to the head of the hash chain:
|
alpar@9
|
1543 */
|
alpar@9
|
1544 hash_head = NIL;
|
alpar@9
|
1545 if (s->lookahead >= MIN_MATCH) {
|
alpar@9
|
1546 INSERT_STRING(s, s->strstart, hash_head);
|
alpar@9
|
1547 }
|
alpar@9
|
1548
|
alpar@9
|
1549 /* Find the longest match, discarding those <= prev_length.
|
alpar@9
|
1550 * At this point we have always match_length < MIN_MATCH
|
alpar@9
|
1551 */
|
alpar@9
|
1552 if (hash_head != NIL && s->strstart - hash_head <= MAX_DIST(s)) {
|
alpar@9
|
1553 /* To simplify the code, we prevent matches with the string
|
alpar@9
|
1554 * of window index 0 (in particular we have to avoid a match
|
alpar@9
|
1555 * of the string with itself at the start of the input file).
|
alpar@9
|
1556 */
|
alpar@9
|
1557 s->match_length = longest_match (s, hash_head);
|
alpar@9
|
1558 /* longest_match() sets match_start */
|
alpar@9
|
1559 }
|
alpar@9
|
1560 if (s->match_length >= MIN_MATCH) {
|
alpar@9
|
1561 check_match(s, s->strstart, s->match_start, s->match_length);
|
alpar@9
|
1562
|
alpar@9
|
1563 _tr_tally_dist(s, s->strstart - s->match_start,
|
alpar@9
|
1564 s->match_length - MIN_MATCH, bflush);
|
alpar@9
|
1565
|
alpar@9
|
1566 s->lookahead -= s->match_length;
|
alpar@9
|
1567
|
alpar@9
|
1568 /* Insert new strings in the hash table only if the match length
|
alpar@9
|
1569 * is not too large. This saves time but degrades compression.
|
alpar@9
|
1570 */
|
alpar@9
|
1571 #ifndef FASTEST
|
alpar@9
|
1572 if (s->match_length <= s->max_insert_length &&
|
alpar@9
|
1573 s->lookahead >= MIN_MATCH) {
|
alpar@9
|
1574 s->match_length--; /* string at strstart already in table */
|
alpar@9
|
1575 do {
|
alpar@9
|
1576 s->strstart++;
|
alpar@9
|
1577 INSERT_STRING(s, s->strstart, hash_head);
|
alpar@9
|
1578 /* strstart never exceeds WSIZE-MAX_MATCH, so there are
|
alpar@9
|
1579 * always MIN_MATCH bytes ahead.
|
alpar@9
|
1580 */
|
alpar@9
|
1581 } while (--s->match_length != 0);
|
alpar@9
|
1582 s->strstart++;
|
alpar@9
|
1583 } else
|
alpar@9
|
1584 #endif
|
alpar@9
|
1585 {
|
alpar@9
|
1586 s->strstart += s->match_length;
|
alpar@9
|
1587 s->match_length = 0;
|
alpar@9
|
1588 s->ins_h = s->window[s->strstart];
|
alpar@9
|
1589 UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
|
alpar@9
|
1590 #if MIN_MATCH != 3
|
alpar@9
|
1591 Call UPDATE_HASH() MIN_MATCH-3 more times
|
alpar@9
|
1592 #endif
|
alpar@9
|
1593 /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not
|
alpar@9
|
1594 * matter since it will be recomputed at next deflate call.
|
alpar@9
|
1595 */
|
alpar@9
|
1596 }
|
alpar@9
|
1597 } else {
|
alpar@9
|
1598 /* No match, output a literal byte */
|
alpar@9
|
1599 Tracevv((stderr,"%c", s->window[s->strstart]));
|
alpar@9
|
1600 _tr_tally_lit (s, s->window[s->strstart], bflush);
|
alpar@9
|
1601 s->lookahead--;
|
alpar@9
|
1602 s->strstart++;
|
alpar@9
|
1603 }
|
alpar@9
|
1604 if (bflush) FLUSH_BLOCK(s, 0);
|
alpar@9
|
1605 }
|
alpar@9
|
1606 FLUSH_BLOCK(s, flush == Z_FINISH);
|
alpar@9
|
1607 return flush == Z_FINISH ? finish_done : block_done;
|
alpar@9
|
1608 }
|
alpar@9
|
1609
|
alpar@9
|
1610 #ifndef FASTEST
|
alpar@9
|
1611 /* ===========================================================================
|
alpar@9
|
1612 * Same as above, but achieves better compression. We use a lazy
|
alpar@9
|
1613 * evaluation for matches: a match is finally adopted only if there is
|
alpar@9
|
1614 * no better match at the next window position.
|
alpar@9
|
1615 */
|
alpar@9
|
1616 local block_state deflate_slow(s, flush)
|
alpar@9
|
1617 deflate_state *s;
|
alpar@9
|
1618 int flush;
|
alpar@9
|
1619 {
|
alpar@9
|
1620 IPos hash_head; /* head of hash chain */
|
alpar@9
|
1621 int bflush; /* set if current block must be flushed */
|
alpar@9
|
1622
|
alpar@9
|
1623 /* Process the input block. */
|
alpar@9
|
1624 for (;;) {
|
alpar@9
|
1625 /* Make sure that we always have enough lookahead, except
|
alpar@9
|
1626 * at the end of the input file. We need MAX_MATCH bytes
|
alpar@9
|
1627 * for the next match, plus MIN_MATCH bytes to insert the
|
alpar@9
|
1628 * string following the next match.
|
alpar@9
|
1629 */
|
alpar@9
|
1630 if (s->lookahead < MIN_LOOKAHEAD) {
|
alpar@9
|
1631 fill_window(s);
|
alpar@9
|
1632 if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
|
alpar@9
|
1633 return need_more;
|
alpar@9
|
1634 }
|
alpar@9
|
1635 if (s->lookahead == 0) break; /* flush the current block */
|
alpar@9
|
1636 }
|
alpar@9
|
1637
|
alpar@9
|
1638 /* Insert the string window[strstart .. strstart+2] in the
|
alpar@9
|
1639 * dictionary, and set hash_head to the head of the hash chain:
|
alpar@9
|
1640 */
|
alpar@9
|
1641 hash_head = NIL;
|
alpar@9
|
1642 if (s->lookahead >= MIN_MATCH) {
|
alpar@9
|
1643 INSERT_STRING(s, s->strstart, hash_head);
|
alpar@9
|
1644 }
|
alpar@9
|
1645
|
alpar@9
|
1646 /* Find the longest match, discarding those <= prev_length.
|
alpar@9
|
1647 */
|
alpar@9
|
1648 s->prev_length = s->match_length, s->prev_match = s->match_start;
|
alpar@9
|
1649 s->match_length = MIN_MATCH-1;
|
alpar@9
|
1650
|
alpar@9
|
1651 if (hash_head != NIL && s->prev_length < s->max_lazy_match &&
|
alpar@9
|
1652 s->strstart - hash_head <= MAX_DIST(s)) {
|
alpar@9
|
1653 /* To simplify the code, we prevent matches with the string
|
alpar@9
|
1654 * of window index 0 (in particular we have to avoid a match
|
alpar@9
|
1655 * of the string with itself at the start of the input file).
|
alpar@9
|
1656 */
|
alpar@9
|
1657 s->match_length = longest_match (s, hash_head);
|
alpar@9
|
1658 /* longest_match() sets match_start */
|
alpar@9
|
1659
|
alpar@9
|
1660 if (s->match_length <= 5 && (s->strategy == Z_FILTERED
|
alpar@9
|
1661 #if TOO_FAR <= 32767
|
alpar@9
|
1662 || (s->match_length == MIN_MATCH &&
|
alpar@9
|
1663 s->strstart - s->match_start > TOO_FAR)
|
alpar@9
|
1664 #endif
|
alpar@9
|
1665 )) {
|
alpar@9
|
1666
|
alpar@9
|
1667 /* If prev_match is also MIN_MATCH, match_start is garbage
|
alpar@9
|
1668 * but we will ignore the current match anyway.
|
alpar@9
|
1669 */
|
alpar@9
|
1670 s->match_length = MIN_MATCH-1;
|
alpar@9
|
1671 }
|
alpar@9
|
1672 }
|
alpar@9
|
1673 /* If there was a match at the previous step and the current
|
alpar@9
|
1674 * match is not better, output the previous match:
|
alpar@9
|
1675 */
|
alpar@9
|
1676 if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) {
|
alpar@9
|
1677 uInt max_insert = s->strstart + s->lookahead - MIN_MATCH;
|
alpar@9
|
1678 /* Do not insert strings in hash table beyond this. */
|
alpar@9
|
1679
|
alpar@9
|
1680 check_match(s, s->strstart-1, s->prev_match, s->prev_length);
|
alpar@9
|
1681
|
alpar@9
|
1682 _tr_tally_dist(s, s->strstart -1 - s->prev_match,
|
alpar@9
|
1683 s->prev_length - MIN_MATCH, bflush);
|
alpar@9
|
1684
|
alpar@9
|
1685 /* Insert in hash table all strings up to the end of the match.
|
alpar@9
|
1686 * strstart-1 and strstart are already inserted. If there is not
|
alpar@9
|
1687 * enough lookahead, the last two strings are not inserted in
|
alpar@9
|
1688 * the hash table.
|
alpar@9
|
1689 */
|
alpar@9
|
1690 s->lookahead -= s->prev_length-1;
|
alpar@9
|
1691 s->prev_length -= 2;
|
alpar@9
|
1692 do {
|
alpar@9
|
1693 if (++s->strstart <= max_insert) {
|
alpar@9
|
1694 INSERT_STRING(s, s->strstart, hash_head);
|
alpar@9
|
1695 }
|
alpar@9
|
1696 } while (--s->prev_length != 0);
|
alpar@9
|
1697 s->match_available = 0;
|
alpar@9
|
1698 s->match_length = MIN_MATCH-1;
|
alpar@9
|
1699 s->strstart++;
|
alpar@9
|
1700
|
alpar@9
|
1701 if (bflush) FLUSH_BLOCK(s, 0);
|
alpar@9
|
1702
|
alpar@9
|
1703 } else if (s->match_available) {
|
alpar@9
|
1704 /* If there was no match at the previous position, output a
|
alpar@9
|
1705 * single literal. If there was a match but the current match
|
alpar@9
|
1706 * is longer, truncate the previous match to a single literal.
|
alpar@9
|
1707 */
|
alpar@9
|
1708 Tracevv((stderr,"%c", s->window[s->strstart-1]));
|
alpar@9
|
1709 _tr_tally_lit(s, s->window[s->strstart-1], bflush);
|
alpar@9
|
1710 if (bflush) {
|
alpar@9
|
1711 FLUSH_BLOCK_ONLY(s, 0);
|
alpar@9
|
1712 }
|
alpar@9
|
1713 s->strstart++;
|
alpar@9
|
1714 s->lookahead--;
|
alpar@9
|
1715 if (s->strm->avail_out == 0) return need_more;
|
alpar@9
|
1716 } else {
|
alpar@9
|
1717 /* There is no previous match to compare with, wait for
|
alpar@9
|
1718 * the next step to decide.
|
alpar@9
|
1719 */
|
alpar@9
|
1720 s->match_available = 1;
|
alpar@9
|
1721 s->strstart++;
|
alpar@9
|
1722 s->lookahead--;
|
alpar@9
|
1723 }
|
alpar@9
|
1724 }
|
alpar@9
|
1725 Assert (flush != Z_NO_FLUSH, "no flush?");
|
alpar@9
|
1726 if (s->match_available) {
|
alpar@9
|
1727 Tracevv((stderr,"%c", s->window[s->strstart-1]));
|
alpar@9
|
1728 _tr_tally_lit(s, s->window[s->strstart-1], bflush);
|
alpar@9
|
1729 s->match_available = 0;
|
alpar@9
|
1730 }
|
alpar@9
|
1731 FLUSH_BLOCK(s, flush == Z_FINISH);
|
alpar@9
|
1732 return flush == Z_FINISH ? finish_done : block_done;
|
alpar@9
|
1733 }
|
alpar@9
|
1734 #endif /* FASTEST */
|
alpar@9
|
1735
|
alpar@9
|
1736 /* ===========================================================================
|
alpar@9
|
1737 * For Z_RLE, simply look for runs of bytes, generate matches only of distance
|
alpar@9
|
1738 * one. Do not maintain a hash table. (It will be regenerated if this run of
|
alpar@9
|
1739 * deflate switches away from Z_RLE.)
|
alpar@9
|
1740 */
|
alpar@9
|
1741 local block_state deflate_rle(s, flush)
|
alpar@9
|
1742 deflate_state *s;
|
alpar@9
|
1743 int flush;
|
alpar@9
|
1744 {
|
alpar@9
|
1745 int bflush; /* set if current block must be flushed */
|
alpar@9
|
1746 uInt prev; /* byte at distance one to match */
|
alpar@9
|
1747 Bytef *scan, *strend; /* scan goes up to strend for length of run */
|
alpar@9
|
1748
|
alpar@9
|
1749 for (;;) {
|
alpar@9
|
1750 /* Make sure that we always have enough lookahead, except
|
alpar@9
|
1751 * at the end of the input file. We need MAX_MATCH bytes
|
alpar@9
|
1752 * for the longest encodable run.
|
alpar@9
|
1753 */
|
alpar@9
|
1754 if (s->lookahead < MAX_MATCH) {
|
alpar@9
|
1755 fill_window(s);
|
alpar@9
|
1756 if (s->lookahead < MAX_MATCH && flush == Z_NO_FLUSH) {
|
alpar@9
|
1757 return need_more;
|
alpar@9
|
1758 }
|
alpar@9
|
1759 if (s->lookahead == 0) break; /* flush the current block */
|
alpar@9
|
1760 }
|
alpar@9
|
1761
|
alpar@9
|
1762 /* See how many times the previous byte repeats */
|
alpar@9
|
1763 s->match_length = 0;
|
alpar@9
|
1764 if (s->lookahead >= MIN_MATCH && s->strstart > 0) {
|
alpar@9
|
1765 scan = s->window + s->strstart - 1;
|
alpar@9
|
1766 prev = *scan;
|
alpar@9
|
1767 if (prev == *++scan && prev == *++scan && prev == *++scan) {
|
alpar@9
|
1768 strend = s->window + s->strstart + MAX_MATCH;
|
alpar@9
|
1769 do {
|
alpar@9
|
1770 } while (prev == *++scan && prev == *++scan &&
|
alpar@9
|
1771 prev == *++scan && prev == *++scan &&
|
alpar@9
|
1772 prev == *++scan && prev == *++scan &&
|
alpar@9
|
1773 prev == *++scan && prev == *++scan &&
|
alpar@9
|
1774 scan < strend);
|
alpar@9
|
1775 s->match_length = MAX_MATCH - (int)(strend - scan);
|
alpar@9
|
1776 if (s->match_length > s->lookahead)
|
alpar@9
|
1777 s->match_length = s->lookahead;
|
alpar@9
|
1778 }
|
alpar@9
|
1779 }
|
alpar@9
|
1780
|
alpar@9
|
1781 /* Emit match if have run of MIN_MATCH or longer, else emit literal */
|
alpar@9
|
1782 if (s->match_length >= MIN_MATCH) {
|
alpar@9
|
1783 check_match(s, s->strstart, s->strstart - 1, s->match_length);
|
alpar@9
|
1784
|
alpar@9
|
1785 _tr_tally_dist(s, 1, s->match_length - MIN_MATCH, bflush);
|
alpar@9
|
1786
|
alpar@9
|
1787 s->lookahead -= s->match_length;
|
alpar@9
|
1788 s->strstart += s->match_length;
|
alpar@9
|
1789 s->match_length = 0;
|
alpar@9
|
1790 } else {
|
alpar@9
|
1791 /* No match, output a literal byte */
|
alpar@9
|
1792 Tracevv((stderr,"%c", s->window[s->strstart]));
|
alpar@9
|
1793 _tr_tally_lit (s, s->window[s->strstart], bflush);
|
alpar@9
|
1794 s->lookahead--;
|
alpar@9
|
1795 s->strstart++;
|
alpar@9
|
1796 }
|
alpar@9
|
1797 if (bflush) FLUSH_BLOCK(s, 0);
|
alpar@9
|
1798 }
|
alpar@9
|
1799 FLUSH_BLOCK(s, flush == Z_FINISH);
|
alpar@9
|
1800 return flush == Z_FINISH ? finish_done : block_done;
|
alpar@9
|
1801 }
|
alpar@9
|
1802
|
alpar@9
|
1803 /* ===========================================================================
|
alpar@9
|
1804 * For Z_HUFFMAN_ONLY, do not look for matches. Do not maintain a hash table.
|
alpar@9
|
1805 * (It will be regenerated if this run of deflate switches away from Huffman.)
|
alpar@9
|
1806 */
|
alpar@9
|
1807 local block_state deflate_huff(s, flush)
|
alpar@9
|
1808 deflate_state *s;
|
alpar@9
|
1809 int flush;
|
alpar@9
|
1810 {
|
alpar@9
|
1811 int bflush; /* set if current block must be flushed */
|
alpar@9
|
1812
|
alpar@9
|
1813 for (;;) {
|
alpar@9
|
1814 /* Make sure that we have a literal to write. */
|
alpar@9
|
1815 if (s->lookahead == 0) {
|
alpar@9
|
1816 fill_window(s);
|
alpar@9
|
1817 if (s->lookahead == 0) {
|
alpar@9
|
1818 if (flush == Z_NO_FLUSH)
|
alpar@9
|
1819 return need_more;
|
alpar@9
|
1820 break; /* flush the current block */
|
alpar@9
|
1821 }
|
alpar@9
|
1822 }
|
alpar@9
|
1823
|
alpar@9
|
1824 /* Output a literal byte */
|
alpar@9
|
1825 s->match_length = 0;
|
alpar@9
|
1826 Tracevv((stderr,"%c", s->window[s->strstart]));
|
alpar@9
|
1827 _tr_tally_lit (s, s->window[s->strstart], bflush);
|
alpar@9
|
1828 s->lookahead--;
|
alpar@9
|
1829 s->strstart++;
|
alpar@9
|
1830 if (bflush) FLUSH_BLOCK(s, 0);
|
alpar@9
|
1831 }
|
alpar@9
|
1832 FLUSH_BLOCK(s, flush == Z_FINISH);
|
alpar@9
|
1833 return flush == Z_FINISH ? finish_done : block_done;
|
alpar@9
|
1834 }
|