summaryrefslogtreecommitdiff
path: root/deflate.c
diff options
context:
space:
mode:
Diffstat (limited to 'deflate.c')
-rw-r--r--deflate.c399
1 files changed, 287 insertions, 112 deletions
diff --git a/deflate.c b/deflate.c
index c531856..31b090e 100644
--- a/deflate.c
+++ b/deflate.c
@@ -1,5 +1,5 @@
1/* deflate.c -- compress data using the deflation algorithm 1/* deflate.c -- compress data using the deflation algorithm
2 * Copyright (C) 1995 Jean-loup Gailly. 2 * Copyright (C) 1995-1996 Jean-loup Gailly.
3 * For conditions of distribution and use, see copyright notice in zlib.h 3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */ 4 */
5 5
@@ -51,7 +51,7 @@
51 51
52#include "deflate.h" 52#include "deflate.h"
53 53
54char copyright[] = " deflate Copyright 1995 Jean-loup Gailly "; 54char deflate_copyright[] = " deflate 1.0 Copyright 1995-1996 Jean-loup Gailly ";
55/* 55/*
56 If you use the zlib library in a product, an acknowledgment is welcome 56 If you use the zlib library in a product, an acknowledgment is welcome
57 in the documentation of your product. If for some reason you cannot 57 in the documentation of your product. If for some reason you cannot
@@ -59,6 +59,31 @@ char copyright[] = " deflate Copyright 1995 Jean-loup Gailly ";
59 copyright string in the executable of your product. 59 copyright string in the executable of your product.
60 */ 60 */
61 61
62/* ===========================================================================
63 * Function prototypes.
64 */
65local void fill_window OF((deflate_state *s));
66local int deflate_stored OF((deflate_state *s, int flush));
67local int deflate_fast OF((deflate_state *s, int flush));
68local int deflate_slow OF((deflate_state *s, int flush));
69local void lm_init OF((deflate_state *s));
70local int longest_match OF((deflate_state *s, IPos cur_match));
71local void putShortMSB OF((deflate_state *s, uInt b));
72local void flush_pending OF((z_stream *strm));
73local int read_buf OF((z_stream *strm, charf *buf, unsigned size));
74#ifdef ASMV
75 void match_init OF((void)); /* asm code initialization */
76#endif
77
78#ifdef DEBUG
79local void check_match OF((deflate_state *s, IPos start, IPos match,
80 int length));
81#endif
82
83/* ===========================================================================
84 * Local data
85 */
86
62#define NIL 0 87#define NIL 0
63/* Tail of hash chains */ 88/* Tail of hash chains */
64 89
@@ -72,32 +97,35 @@ char copyright[] = " deflate Copyright 1995 Jean-loup Gailly ";
72 * See deflate.c for comments about the MIN_MATCH+1. 97 * See deflate.c for comments about the MIN_MATCH+1.
73 */ 98 */
74 99
100typedef int (*compress_func) OF((deflate_state *s, int flush));
101/* Compressing function */
102
75/* Values for max_lazy_match, good_match and max_chain_length, depending on 103/* Values for max_lazy_match, good_match and max_chain_length, depending on
76 * the desired pack level (0..9). The values given below have been tuned to 104 * the desired pack level (0..9). The values given below have been tuned to
77 * exclude worst case performance for pathological files. Better values may be 105 * exclude worst case performance for pathological files. Better values may be
78 * found for specific files. 106 * found for specific files.
79 */ 107 */
80
81typedef struct config_s { 108typedef struct config_s {
82 ush good_length; /* reduce lazy search above this match length */ 109 ush good_length; /* reduce lazy search above this match length */
83 ush max_lazy; /* do not perform lazy search above this match length */ 110 ush max_lazy; /* do not perform lazy search above this match length */
84 ush nice_length; /* quit search above this match length */ 111 ush nice_length; /* quit search above this match length */
85 ush max_chain; 112 ush max_chain;
113 compress_func func;
86} config; 114} config;
87 115
88local config configuration_table[10] = { 116local config configuration_table[10] = {
89/* good lazy nice chain */ 117/* good lazy nice chain */
90/* 0 */ {0, 0, 0, 0}, /* store only */ 118/* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */
91/* 1 */ {4, 4, 8, 4}, /* maximum speed, no lazy matches */ 119/* 1 */ {4, 4, 8, 4, deflate_fast}, /* maximum speed, no lazy matches */
92/* 2 */ {4, 5, 16, 8}, 120/* 2 */ {4, 5, 16, 8, deflate_fast},
93/* 3 */ {4, 6, 32, 32}, 121/* 3 */ {4, 6, 32, 32, deflate_fast},
94 122
95/* 4 */ {4, 4, 16, 16}, /* lazy matches */ 123/* 4 */ {4, 4, 16, 16, deflate_slow}, /* lazy matches */
96/* 5 */ {8, 16, 32, 32}, 124/* 5 */ {8, 16, 32, 32, deflate_slow},
97/* 6 */ {8, 16, 128, 128}, 125/* 6 */ {8, 16, 128, 128, deflate_slow},
98/* 7 */ {8, 32, 128, 256}, 126/* 7 */ {8, 32, 128, 256, deflate_slow},
99/* 8 */ {32, 128, 258, 1024}, 127/* 8 */ {32, 128, 258, 1024, deflate_slow},
100/* 9 */ {32, 258, 258, 4096}}; /* maximum compression */ 128/* 9 */ {32, 258, 258, 4096, deflate_slow}}; /* maximum compression */
101 129
102/* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4 130/* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4
103 * For deflate_fast() (levels <= 3) good is ignored and lazy has a different 131 * For deflate_fast() (levels <= 3) good is ignored and lazy has a different
@@ -110,28 +138,6 @@ local config configuration_table[10] = {
110struct static_tree_desc_s {int dummy;}; /* for buggy compilers */ 138struct static_tree_desc_s {int dummy;}; /* for buggy compilers */
111 139
112/* =========================================================================== 140/* ===========================================================================
113 * Prototypes for local functions.
114 */
115
116local void fill_window OF((deflate_state *s));
117local int deflate_fast OF((deflate_state *s, int flush));
118local int deflate_slow OF((deflate_state *s, int flush));
119local void lm_init OF((deflate_state *s));
120local int longest_match OF((deflate_state *s, IPos cur_match));
121local void putShortMSB OF((deflate_state *s, uInt b));
122local void flush_pending OF((z_stream *strm));
123local int read_buf OF((z_stream *strm, charf *buf, unsigned size));
124#ifdef ASMV
125 void match_init OF((void)); /* asm code initialization */
126#endif
127
128#ifdef DEBUG
129local void check_match OF((deflate_state *s, IPos start, IPos match,
130 int length));
131#endif
132
133
134/* ===========================================================================
135 * Update a hash value with the given input byte 141 * Update a hash value with the given input byte
136 * IN assertion: all calls to to UPDATE_HASH are made with consecutive 142 * IN assertion: all calls to to UPDATE_HASH are made with consecutive
137 * input characters, so that a running hash key can be computed from the 143 * input characters, so that a running hash key can be computed from the
@@ -162,30 +168,43 @@ local void check_match OF((deflate_state *s, IPos start, IPos match,
162 zmemzero((charf *)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head)); 168 zmemzero((charf *)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head));
163 169
164/* ========================================================================= */ 170/* ========================================================================= */
165int deflateInit (strm, level) 171int deflateInit_(strm, level, version, stream_size)
166 z_stream *strm; 172 z_stream *strm;
167 int level; 173 int level;
174 const char *version;
175 int stream_size;
168{ 176{
169 return deflateInit2 (strm, level, DEFLATED, MAX_WBITS, DEF_MEM_LEVEL, 0); 177 return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL,
178 Z_DEFAULT_STRATEGY, version, stream_size);
170 /* To do: ignore strm->next_in if we use it as window */ 179 /* To do: ignore strm->next_in if we use it as window */
171} 180}
172 181
173/* ========================================================================= */ 182/* ========================================================================= */
174int deflateInit2 (strm, level, method, windowBits, memLevel, strategy) 183int deflateInit2_(strm, level, method, windowBits, memLevel, strategy,
184 version, stream_size)
175 z_stream *strm; 185 z_stream *strm;
176 int level; 186 int level;
177 int method; 187 int method;
178 int windowBits; 188 int windowBits;
179 int memLevel; 189 int memLevel;
180 int strategy; 190 int strategy;
191 const char *version;
192 int stream_size;
181{ 193{
182 deflate_state *s; 194 deflate_state *s;
183 int noheader = 0; 195 int noheader = 0;
184 196
197 if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
198 stream_size != sizeof(z_stream)) {
199 return Z_VERSION_ERROR;
200 }
185 if (strm == Z_NULL) return Z_STREAM_ERROR; 201 if (strm == Z_NULL) return Z_STREAM_ERROR;
186 202
187 strm->msg = Z_NULL; 203 strm->msg = Z_NULL;
188 if (strm->zalloc == Z_NULL) strm->zalloc = zcalloc; 204 if (strm->zalloc == Z_NULL) {
205 strm->zalloc = zcalloc;
206 strm->opaque = (voidpf)0;
207 }
189 if (strm->zfree == Z_NULL) strm->zfree = zcfree; 208 if (strm->zfree == Z_NULL) strm->zfree = zcfree;
190 209
191 if (level == Z_DEFAULT_COMPRESSION) level = 6; 210 if (level == Z_DEFAULT_COMPRESSION) level = 6;
@@ -194,8 +213,9 @@ int deflateInit2 (strm, level, method, windowBits, memLevel, strategy)
194 noheader = 1; 213 noheader = 1;
195 windowBits = -windowBits; 214 windowBits = -windowBits;
196 } 215 }
197 if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != DEFLATED || 216 if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED ||
198 windowBits < 8 || windowBits > 15 || level < 1 || level > 9) { 217 windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
218 strategy < 0 || strategy > Z_HUFFMAN_ONLY) {
199 return Z_STREAM_ERROR; 219 return Z_STREAM_ERROR;
200 } 220 }
201 s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state)); 221 s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state));
@@ -223,15 +243,15 @@ int deflateInit2 (strm, level, method, windowBits, memLevel, strategy)
223 243
224 if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL || 244 if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||
225 s->pending_buf == Z_NULL) { 245 s->pending_buf == Z_NULL) {
226 strm->msg = z_errmsg[1-Z_MEM_ERROR]; 246 strm->msg = ERR_MSG(Z_MEM_ERROR);
227 deflateEnd (strm); 247 deflateEnd (strm);
228 return Z_MEM_ERROR; 248 return Z_MEM_ERROR;
229 } 249 }
230 s->d_buf = (ushf *) &(s->pending_buf[s->lit_bufsize]); 250 s->l_buf = (uchf *) &(s->pending_buf[s->lit_bufsize]);
231 s->l_buf = (uchf *) &(s->pending_buf[3*s->lit_bufsize]); 251 s->d_buf = (ushf *) &(s->pending_buf[2*s->lit_bufsize]);
232 /* We overlay pending_buf and d_buf+l_buf. This works since the average 252 /* We overlay pending_buf and d_buf+l_buf. This works since the average
233 * output size for (length,distance) codes is <= 32 bits (worst case 253 * output size for (length,distance) codes is <= 32 bits (worst case
234 * is 15+15+13=33). 254 * is 15+15+13=33). d_buf is put last in case sizeof(short)>2.
235 */ 255 */
236 256
237 s->level = level; 257 s->level = level;
@@ -242,6 +262,44 @@ int deflateInit2 (strm, level, method, windowBits, memLevel, strategy)
242} 262}
243 263
244/* ========================================================================= */ 264/* ========================================================================= */
265int deflateSetDictionary (strm, dictionary, dictLength)
266 z_stream *strm;
267 const Bytef *dictionary;
268 uInt dictLength;
269{
270 deflate_state *s;
271 uInt length = dictLength;
272 uInt n;
273 IPos hash_head;
274
275 if (strm == Z_NULL || strm->state == Z_NULL || dictionary == Z_NULL ||
276 strm->state->status != INIT_STATE) return Z_STREAM_ERROR;
277
278 s = strm->state;
279 strm->adler = adler32(strm->adler, dictionary, dictLength);
280
281 if (length < MIN_MATCH) return Z_OK;
282 if (length > MAX_DIST(s)) {
283 length = MAX_DIST(s);
284 dictionary += dictLength - length;
285 }
286 zmemcpy((charf *)s->window, dictionary, length);
287 s->strstart = length;
288 s->block_start = (long)length;
289
290 /* Insert all strings in the hash table (except for the last two bytes).
291 * s->lookahead stays null, so s->ins_h will be recomputed at the next
292 * call of fill_window.
293 */
294 s->ins_h = s->window[0];
295 UPDATE_HASH(s, s->ins_h, s->window[1]);
296 for (n = 0; n <= length - MIN_MATCH; n++) {
297 INSERT_STRING(s, n, hash_head);
298 }
299 return Z_OK;
300}
301
302/* ========================================================================= */
245int deflateReset (strm) 303int deflateReset (strm)
246 z_stream *strm; 304 z_stream *strm;
247{ 305{
@@ -262,14 +320,52 @@ int deflateReset (strm)
262 s->noheader = 0; /* was set to -1 by deflate(..., Z_FINISH); */ 320 s->noheader = 0; /* was set to -1 by deflate(..., Z_FINISH); */
263 } 321 }
264 s->status = s->noheader ? BUSY_STATE : INIT_STATE; 322 s->status = s->noheader ? BUSY_STATE : INIT_STATE;
265 s->adler = 1; 323 strm->adler = 1;
324 s->last_flush = Z_NO_FLUSH;
266 325
267 ct_init(s); 326 _tr_init(s);
268 lm_init(s); 327 lm_init(s);
269 328
270 return Z_OK; 329 return Z_OK;
271} 330}
272 331
332/* ========================================================================= */
333int deflateParams(strm, level, strategy)
334 z_stream *strm;
335 int level;
336 int strategy;
337{
338 deflate_state *s;
339 compress_func func;
340
341 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
342 s = strm->state;
343
344 if (level == Z_DEFAULT_COMPRESSION) {
345 level = 6;
346 }
347 if (level < 0 || level > 9 || strategy < 0 || strategy > Z_HUFFMAN_ONLY) {
348 return Z_STREAM_ERROR;
349 }
350 func = configuration_table[s->level].func;
351
352 if (func != configuration_table[level].func
353 && strm->state->lookahead != 0) {
354
355 /* Flush the last buffer: */
356 (void)(*func)(strm->state, Z_PARTIAL_FLUSH);
357 }
358 if (s->level != level) {
359 s->level = level;
360 s->max_lazy_match = configuration_table[level].max_lazy;
361 s->good_match = configuration_table[level].good_length;
362 s->nice_match = configuration_table[level].nice_length;
363 s->max_chain_length = configuration_table[level].max_chain;
364 }
365 s->strategy = strategy;
366 return Z_OK;
367}
368
273/* ========================================================================= 369/* =========================================================================
274 * Put a short in the pending buffer. The 16-bit value is put in MSB order. 370 * Put a short in the pending buffer. The 16-bit value is put in MSB order.
275 * IN assertion: the stream state is correct and there is enough room in 371 * IN assertion: the stream state is correct and there is enough room in
@@ -284,7 +380,10 @@ local void putShortMSB (s, b)
284} 380}
285 381
286/* ========================================================================= 382/* =========================================================================
287 * Flush as much pending output as possible. 383 * Flush as much pending output as possible. All deflate() output goes
384 * through this function so some applications may wish to modify it
385 * to avoid allocating a large strm->next_out buffer and copying into it.
386 * (See also read_buf()).
288 */ 387 */
289local void flush_pending(strm) 388local void flush_pending(strm)
290 z_stream *strm; 389 z_stream *strm;
@@ -310,55 +409,76 @@ int deflate (strm, flush)
310 z_stream *strm; 409 z_stream *strm;
311 int flush; 410 int flush;
312{ 411{
412 int old_flush; /* value of flush param for previous deflate call */
413 deflate_state *s;
414
313 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; 415 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
314 416
417 s = strm->state;
418
315 if (strm->next_out == Z_NULL || 419 if (strm->next_out == Z_NULL ||
316 (strm->next_in == Z_NULL && strm->avail_in != 0)) { 420 (strm->next_in == Z_NULL && strm->avail_in != 0) ||
421 (s->status == FINISH_STATE && flush != Z_FINISH)) {
317 ERR_RETURN(strm, Z_STREAM_ERROR); 422 ERR_RETURN(strm, Z_STREAM_ERROR);
318 } 423 }
319 if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR); 424 if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR);
320 425
321 strm->state->strm = strm; /* just in case */ 426 s->strm = strm; /* just in case */
427 old_flush = s->last_flush;
428 s->last_flush = flush;
322 429
323 /* Write the zlib header */ 430 /* Write the zlib header */
324 if (strm->state->status == INIT_STATE) { 431 if (s->status == INIT_STATE) {
325 432
326 uInt header = (DEFLATED + ((strm->state->w_bits-8)<<4)) << 8; 433 uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8;
327 uInt level_flags = (strm->state->level-1) >> 1; 434 uInt level_flags = (s->level-1) >> 1;
328 435
329 if (level_flags > 3) level_flags = 3; 436 if (level_flags > 3) level_flags = 3;
330 header |= (level_flags << 6); 437 header |= (level_flags << 6);
438 if (s->strstart != 0) header |= PRESET_DICT;
331 header += 31 - (header % 31); 439 header += 31 - (header % 31);
332 440
333 strm->state->status = BUSY_STATE; 441 s->status = BUSY_STATE;
334 putShortMSB(strm->state, header); 442 putShortMSB(s, header);
443
444 /* Save the adler32 of the preset dictionary: */
445 if (s->strstart != 0) {
446 putShortMSB(s, (uInt)(strm->adler >> 16));
447 putShortMSB(s, (uInt)(strm->adler & 0xffff));
448 strm->adler = 1L;
449 }
335 } 450 }
336 451
337 /* Flush as much pending output as possible */ 452 /* Flush as much pending output as possible */
338 if (strm->state->pending != 0) { 453 if (s->pending != 0) {
339 flush_pending(strm); 454 flush_pending(strm);
340 if (strm->avail_out == 0) return Z_OK; 455 if (strm->avail_out == 0) return Z_OK;
456
457 /* Make sure there is something to do and avoid duplicate consecutive
458 * flushes. For repeated and useless calls with Z_FINISH, we keep
459 * returning Z_STREAM_END instead of Z_BUFF_ERROR.
460 */
461 } else if (strm->avail_in == 0 && flush <= old_flush &&
462 flush != Z_FINISH) {
463 ERR_RETURN(strm, Z_BUF_ERROR);
341 } 464 }
342 465
343 /* User must not provide more input after the first FINISH: */ 466 /* User must not provide more input after the first FINISH: */
344 if (strm->state->status == FINISH_STATE && strm->avail_in != 0) { 467 if (s->status == FINISH_STATE && strm->avail_in != 0) {
345 ERR_RETURN(strm, Z_BUF_ERROR); 468 ERR_RETURN(strm, Z_BUF_ERROR);
346 } 469 }
347 470
348 /* Start a new block or continue the current one. 471 /* Start a new block or continue the current one.
349 */ 472 */
350 if (strm->avail_in != 0 || strm->state->lookahead != 0 || 473 if (strm->avail_in != 0 || s->lookahead != 0 ||
351 (flush != Z_NO_FLUSH && strm->state->status != FINISH_STATE)) { 474 (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) {
352 int quit; 475 int quit;
353 476
354 if (flush == Z_FINISH) { 477 if (flush == Z_FINISH) {
355 strm->state->status = FINISH_STATE; 478 s->status = FINISH_STATE;
356 }
357 if (strm->state->level <= 3) {
358 quit = deflate_fast(strm->state, flush);
359 } else {
360 quit = deflate_slow(strm->state, flush);
361 } 479 }
480 quit = (*(configuration_table[s->level].func))(s, flush);
481
362 if (quit || strm->avail_out == 0) return Z_OK; 482 if (quit || strm->avail_out == 0) return Z_OK;
363 /* If flush != Z_NO_FLUSH && avail_out == 0, the next call 483 /* If flush != Z_NO_FLUSH && avail_out == 0, the next call
364 * of deflate should use the same flush parameter to make sure 484 * of deflate should use the same flush parameter to make sure
@@ -367,16 +487,16 @@ int deflate (strm, flush)
367 * ensures that for a very small output buffer, we emit at most 487 * ensures that for a very small output buffer, we emit at most
368 * one empty block. 488 * one empty block.
369 */ 489 */
370 if (flush != Z_OK && flush != Z_FINISH) { 490 if (flush != Z_NO_FLUSH && flush != Z_FINISH) {
371 if (flush == Z_PARTIAL_FLUSH) { 491 if (flush == Z_PARTIAL_FLUSH) {
372 ct_align(strm->state); 492 _tr_align(s);
373 } else { /* FULL_FLUSH or SYNC_FLUSH */ 493 } else { /* FULL_FLUSH or SYNC_FLUSH */
374 ct_stored_block(strm->state, (char*)0, 0L, 0); 494 _tr_stored_block(s, (char*)0, 0L, 0);
375 /* For a full flush, this empty block will be recognized 495 /* For a full flush, this empty block will be recognized
376 * as a special marker by inflate_sync(). 496 * as a special marker by inflate_sync().
377 */ 497 */
378 if (flush == Z_FULL_FLUSH) { 498 if (flush == Z_FULL_FLUSH) {
379 CLEAR_HASH(strm->state); /* forget history */ 499 CLEAR_HASH(s); /* forget history */
380 } 500 }
381 } 501 }
382 flush_pending(strm); 502 flush_pending(strm);
@@ -386,34 +506,38 @@ int deflate (strm, flush)
386 Assert(strm->avail_out > 0, "bug2"); 506 Assert(strm->avail_out > 0, "bug2");
387 507
388 if (flush != Z_FINISH) return Z_OK; 508 if (flush != Z_FINISH) return Z_OK;
389 if (strm->state->noheader) return Z_STREAM_END; 509 if (s->noheader) return Z_STREAM_END;
390 510
391 /* Write the zlib trailer (adler32) */ 511 /* Write the zlib trailer (adler32) */
392 putShortMSB(strm->state, (uInt)(strm->state->adler >> 16)); 512 putShortMSB(s, (uInt)(strm->adler >> 16));
393 putShortMSB(strm->state, (uInt)(strm->state->adler & 0xffff)); 513 putShortMSB(s, (uInt)(strm->adler & 0xffff));
394 flush_pending(strm); 514 flush_pending(strm);
395 /* If avail_out is zero, the application will call deflate again 515 /* If avail_out is zero, the application will call deflate again
396 * to flush the rest. 516 * to flush the rest.
397 */ 517 */
398 strm->state->noheader = -1; /* write the trailer only once! */ 518 s->noheader = -1; /* write the trailer only once! */
399 return strm->state->pending != 0 ? Z_OK : Z_STREAM_END; 519 return s->pending != 0 ? Z_OK : Z_STREAM_END;
400} 520}
401 521
402/* ========================================================================= */ 522/* ========================================================================= */
403int deflateEnd (strm) 523int deflateEnd (strm)
404 z_stream *strm; 524 z_stream *strm;
405{ 525{
526 int status;
527
406 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; 528 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
407 529
408 TRY_FREE(strm, strm->state->window); 530 /* Deallocate in reverse order of allocations: */
409 TRY_FREE(strm, strm->state->prev);
410 TRY_FREE(strm, strm->state->head);
411 TRY_FREE(strm, strm->state->pending_buf); 531 TRY_FREE(strm, strm->state->pending_buf);
532 TRY_FREE(strm, strm->state->head);
533 TRY_FREE(strm, strm->state->prev);
534 TRY_FREE(strm, strm->state->window);
412 535
536 status = strm->state->status;
413 ZFREE(strm, strm->state); 537 ZFREE(strm, strm->state);
414 strm->state = Z_NULL; 538 strm->state = Z_NULL;
415 539
416 return Z_OK; 540 return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;
417} 541}
418 542
419/* ========================================================================= */ 543/* ========================================================================= */
@@ -438,7 +562,10 @@ int deflateCopy (dest, source)
438 562
439/* =========================================================================== 563/* ===========================================================================
440 * Read a new buffer from the current input stream, update the adler32 564 * Read a new buffer from the current input stream, update the adler32
441 * and total number of bytes read. 565 * and total number of bytes read. All deflate() input goes through
566 * this function so some applications may wish to modify it to avoid
567 * allocating a large strm->next_in buffer and copying from it.
568 * (See also flush_pending()).
442 */ 569 */
443local int read_buf(strm, buf, size) 570local int read_buf(strm, buf, size)
444 z_stream *strm; 571 z_stream *strm;
@@ -453,7 +580,7 @@ local int read_buf(strm, buf, size)
453 strm->avail_in -= len; 580 strm->avail_in -= len;
454 581
455 if (!strm->state->noheader) { 582 if (!strm->state->noheader) {
456 strm->state->adler = adler32(strm->state->adler, strm->next_in, len); 583 strm->adler = adler32(strm->adler, strm->next_in, len);
457 } 584 }
458 zmemcpy(buf, strm->next_in, len); 585 zmemcpy(buf, strm->next_in, len);
459 strm->next_in += len; 586 strm->next_in += len;
@@ -482,7 +609,7 @@ local void lm_init (s)
482 s->strstart = 0; 609 s->strstart = 0;
483 s->block_start = 0L; 610 s->block_start = 0L;
484 s->lookahead = 0; 611 s->lookahead = 0;
485 s->match_length = MIN_MATCH-1; 612 s->match_length = s->prev_length = MIN_MATCH-1;
486 s->match_available = 0; 613 s->match_available = 0;
487 s->ins_h = 0; 614 s->ins_h = 0;
488#ifdef ASMV 615#ifdef ASMV
@@ -497,6 +624,7 @@ local void lm_init (s)
497 * garbage. 624 * garbage.
498 * IN assertions: cur_match is the head of the hash chain for the current 625 * IN assertions: cur_match is the head of the hash chain for the current
499 * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1 626 * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
627 * OUT assertion: the match length is not greater than s->lookahead.
500 */ 628 */
501#ifndef ASMV 629#ifndef ASMV
502/* For 80x86 and 680x0, an optimized version will be provided in match.asm or 630/* For 80x86 and 680x0, an optimized version will be provided in match.asm or
@@ -511,6 +639,7 @@ local int longest_match(s, cur_match)
511 register Bytef *match; /* matched string */ 639 register Bytef *match; /* matched string */
512 register int len; /* length of current match */ 640 register int len; /* length of current match */
513 int best_len = s->prev_length; /* best match length so far */ 641 int best_len = s->prev_length; /* best match length so far */
642 int nice_match = s->nice_match; /* stop if match long enough */
514 IPos limit = s->strstart > (IPos)MAX_DIST(s) ? 643 IPos limit = s->strstart > (IPos)MAX_DIST(s) ?
515 s->strstart - (IPos)MAX_DIST(s) : NIL; 644 s->strstart - (IPos)MAX_DIST(s) : NIL;
516 /* Stop when cur_match becomes <= limit. To simplify the code, 645 /* Stop when cur_match becomes <= limit. To simplify the code,
@@ -541,6 +670,11 @@ local int longest_match(s, cur_match)
541 if (s->prev_length >= s->good_match) { 670 if (s->prev_length >= s->good_match) {
542 chain_length >>= 2; 671 chain_length >>= 2;
543 } 672 }
673 /* Do not look for matches beyond the end of the input. This is necessary
674 * to make deflate deterministic.
675 */
676 if (nice_match > s->lookahead) nice_match = s->lookahead;
677
544 Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead"); 678 Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
545 679
546 do { 680 do {
@@ -619,7 +753,7 @@ local int longest_match(s, cur_match)
619 if (len > best_len) { 753 if (len > best_len) {
620 s->match_start = cur_match; 754 s->match_start = cur_match;
621 best_len = len; 755 best_len = len;
622 if (len >= s->nice_match) break; 756 if (len >= nice_match) break;
623#ifdef UNALIGNED_OK 757#ifdef UNALIGNED_OK
624 scan_end = *(ushf*)(scan+best_len-1); 758 scan_end = *(ushf*)(scan+best_len-1);
625#else 759#else
@@ -630,7 +764,8 @@ local int longest_match(s, cur_match)
630 } while ((cur_match = prev[cur_match & wmask]) > limit 764 } while ((cur_match = prev[cur_match & wmask]) > limit
631 && --chain_length != 0); 765 && --chain_length != 0);
632 766
633 return best_len; 767 if (best_len <= s->lookahead) return best_len;
768 return s->lookahead;
634} 769}
635#endif /* ASMV */ 770#endif /* ASMV */
636 771
@@ -644,13 +779,13 @@ local void check_match(s, start, match, length)
644 int length; 779 int length;
645{ 780{
646 /* check that the match is indeed a match */ 781 /* check that the match is indeed a match */
647 if (memcmp((charf *)s->window + match, 782 if (zmemcmp((charf *)s->window + match,
648 (charf *)s->window + start, length) != EQUAL) { 783 (charf *)s->window + start, length) != EQUAL) {
649 fprintf(stderr, 784 fprintf(stderr, " start %u, match %u, length %d\n",
650 " start %u, match %u, length %d\n", 785 start, match, length);
651 start, match, length); 786 do {
652 do { fprintf(stderr, "%c%c", s->window[match++], 787 fprintf(stderr, "%c%c", s->window[match++], s->window[start++]);
653 s->window[start++]); } while (--length != 0); 788 } while (--length != 0);
654 z_error("invalid match"); 789 z_error("invalid match");
655 } 790 }
656 if (verbose > 1) { 791 if (verbose > 1) {
@@ -686,6 +821,7 @@ local void fill_window(s)
686 /* Deal with !@#$% 64K limit: */ 821 /* Deal with !@#$% 64K limit: */
687 if (more == 0 && s->strstart == 0 && s->lookahead == 0) { 822 if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
688 more = wsize; 823 more = wsize;
824
689 } else if (more == (unsigned)(-1)) { 825 } else if (more == (unsigned)(-1)) {
690 /* Very unlikely, but possible on 16 bit machine if strstart == 0 826 /* Very unlikely, but possible on 16 bit machine if strstart == 0
691 * and lookahead == 1 (input done one byte at time) 827 * and lookahead == 1 (input done one byte at time)
@@ -697,9 +833,6 @@ local void fill_window(s)
697 */ 833 */
698 } else if (s->strstart >= wsize+MAX_DIST(s)) { 834 } else if (s->strstart >= wsize+MAX_DIST(s)) {
699 835
700 /* By the IN assertion, the window is not empty so we can't confuse
701 * more == 0 with more == 64K on a 16 bit machine.
702 */
703 zmemcpy((charf *)s->window, (charf *)s->window+wsize, 836 zmemcpy((charf *)s->window, (charf *)s->window+wsize,
704 (unsigned)wsize); 837 (unsigned)wsize);
705 s->match_start -= wsize; 838 s->match_start -= wsize;
@@ -768,9 +901,11 @@ local void fill_window(s)
768 * IN assertion: strstart is set to the end of the current match. 901 * IN assertion: strstart is set to the end of the current match.
769 */ 902 */
770#define FLUSH_BLOCK_ONLY(s, eof) { \ 903#define FLUSH_BLOCK_ONLY(s, eof) { \
771 ct_flush_block(s, (s->block_start >= 0L ? \ 904 _tr_flush_block(s, (s->block_start >= 0L ? \
772 (charf *)&s->window[(unsigned)s->block_start] : \ 905 (charf *)&s->window[(unsigned)s->block_start] : \
773 (charf *)Z_NULL), (long)s->strstart - s->block_start, (eof)); \ 906 (charf *)Z_NULL), \
907 (ulg)((long)s->strstart - s->block_start), \
908 (eof)); \
774 s->block_start = s->strstart; \ 909 s->block_start = s->strstart; \
775 flush_pending(s->strm); \ 910 flush_pending(s->strm); \
776 Tracev((stderr,"[FLUSH]")); \ 911 Tracev((stderr,"[FLUSH]")); \
@@ -783,9 +918,54 @@ local void fill_window(s)
783} 918}
784 919
785/* =========================================================================== 920/* ===========================================================================
921 * Copy without compression as much as possible from the input stream, return
922 * true if processing was terminated prematurely (no more input or output
923 * space). This function does not insert new strings in the dictionary
924 * since uncompressible data is probably not useful. This function is used
925 * only for the level=0 compression option.
926 * NOTE: this function should be optimized to avoid extra copying.
927 */
928local int deflate_stored(s, flush)
929 deflate_state *s;
930 int flush;
931{
932 for (;;) {
933 /* Fill the window as much as possible: */
934 if (s->lookahead <= 1) {
935
936 Assert(s->strstart < s->w_size+MAX_DIST(s) ||
937 s->block_start >= (long)s->w_size, "slide too late");
938
939 fill_window(s);
940 if (s->lookahead == 0 && flush == Z_NO_FLUSH) return 1;
941
942 if (s->lookahead == 0) break; /* flush the current block */
943 }
944 Assert(s->block_start >= 0L, "block gone");
945
946 s->strstart += s->lookahead;
947 s->lookahead = 0;
948
949 /* Stored blocks are limited to 0xffff bytes: */
950 if (s->strstart == 0 || s->strstart > 0xffff) {
951 /* strstart == 0 is possible when wraparound on 16-bit machine */
952 s->lookahead = s->strstart - 0xffff;
953 s->strstart = 0xffff;
954 }
955
956 /* Emit a stored block if it is large enough: */
957 if (s->strstart - (uInt)s->block_start >= MAX_DIST(s)) {
958 FLUSH_BLOCK(s, 0);
959 }
960 }
961 FLUSH_BLOCK(s, flush == Z_FINISH);
962 return 0; /* normal exit */
963}
964
965/* ===========================================================================
786 * Compress as much as possible from the input stream, return true if 966 * Compress as much as possible from the input stream, return true if
787 * processing was terminated prematurely (no more input or output space). 967 * processing was terminated prematurely (no more input or output space).
788 * This function does not perform lazy evaluationof matches and inserts 968 * This function does not perform lazy evaluation of matches and inserts
789 * new strings in the dictionary only for unmatched strings or for short 969 * new strings in the dictionary only for unmatched strings or for short
790 * matches. It is used only for the fast compression options. 970 * matches. It is used only for the fast compression options.
791 */ 971 */
@@ -793,10 +973,8 @@ local int deflate_fast(s, flush)
793 deflate_state *s; 973 deflate_state *s;
794 int flush; 974 int flush;
795{ 975{
796 IPos hash_head; /* head of the hash chain */ 976 IPos hash_head = NIL; /* head of the hash chain */
797 int bflush; /* set if current block must be flushed */ 977 int bflush; /* set if current block must be flushed */
798
799 s->prev_length = MIN_MATCH-1;
800 978
801 for (;;) { 979 for (;;) {
802 /* Make sure that we always have enough lookahead, except 980 /* Make sure that we always have enough lookahead, except
@@ -830,14 +1008,12 @@ local int deflate_fast(s, flush)
830 s->match_length = longest_match (s, hash_head); 1008 s->match_length = longest_match (s, hash_head);
831 } 1009 }
832 /* longest_match() sets match_start */ 1010 /* longest_match() sets match_start */
833
834 if (s->match_length > s->lookahead) s->match_length = s->lookahead;
835 } 1011 }
836 if (s->match_length >= MIN_MATCH) { 1012 if (s->match_length >= MIN_MATCH) {
837 check_match(s, s->strstart, s->match_start, s->match_length); 1013 check_match(s, s->strstart, s->match_start, s->match_length);
838 1014
839 bflush = ct_tally(s, s->strstart - s->match_start, 1015 bflush = _tr_tally(s, s->strstart - s->match_start,
840 s->match_length - MIN_MATCH); 1016 s->match_length - MIN_MATCH);
841 1017
842 s->lookahead -= s->match_length; 1018 s->lookahead -= s->match_length;
843 1019
@@ -870,7 +1046,7 @@ local int deflate_fast(s, flush)
870 } else { 1046 } else {
871 /* No match, output a literal byte */ 1047 /* No match, output a literal byte */
872 Tracevv((stderr,"%c", s->window[s->strstart])); 1048 Tracevv((stderr,"%c", s->window[s->strstart]));
873 bflush = ct_tally (s, 0, s->window[s->strstart]); 1049 bflush = _tr_tally (s, 0, s->window[s->strstart]);
874 s->lookahead--; 1050 s->lookahead--;
875 s->strstart++; 1051 s->strstart++;
876 } 1052 }
@@ -889,7 +1065,7 @@ local int deflate_slow(s, flush)
889 deflate_state *s; 1065 deflate_state *s;
890 int flush; 1066 int flush;
891{ 1067{
892 IPos hash_head; /* head of hash chain */ 1068 IPos hash_head = NIL; /* head of hash chain */
893 int bflush; /* set if current block must be flushed */ 1069 int bflush; /* set if current block must be flushed */
894 1070
895 /* Process the input block. */ 1071 /* Process the input block. */
@@ -928,7 +1104,6 @@ local int deflate_slow(s, flush)
928 s->match_length = longest_match (s, hash_head); 1104 s->match_length = longest_match (s, hash_head);
929 } 1105 }
930 /* longest_match() sets match_start */ 1106 /* longest_match() sets match_start */
931 if (s->match_length > s->lookahead) s->match_length = s->lookahead;
932 1107
933 if (s->match_length <= 5 && (s->strategy == Z_FILTERED || 1108 if (s->match_length <= 5 && (s->strategy == Z_FILTERED ||
934 (s->match_length == MIN_MATCH && 1109 (s->match_length == MIN_MATCH &&
@@ -949,8 +1124,8 @@ local int deflate_slow(s, flush)
949 1124
950 check_match(s, s->strstart-1, s->prev_match, s->prev_length); 1125 check_match(s, s->strstart-1, s->prev_match, s->prev_length);
951 1126
952 bflush = ct_tally(s, s->strstart -1 - s->prev_match, 1127 bflush = _tr_tally(s, s->strstart -1 - s->prev_match,
953 s->prev_length - MIN_MATCH); 1128 s->prev_length - MIN_MATCH);
954 1129
955 /* Insert in hash table all strings up to the end of the match. 1130 /* Insert in hash table all strings up to the end of the match.
956 * strstart-1 and strstart are already inserted. If there is not 1131 * strstart-1 and strstart are already inserted. If there is not
@@ -976,7 +1151,7 @@ local int deflate_slow(s, flush)
976 * is longer, truncate the previous match to a single literal. 1151 * is longer, truncate the previous match to a single literal.
977 */ 1152 */
978 Tracevv((stderr,"%c", s->window[s->strstart-1])); 1153 Tracevv((stderr,"%c", s->window[s->strstart-1]));
979 if (ct_tally (s, 0, s->window[s->strstart-1])) { 1154 if (_tr_tally (s, 0, s->window[s->strstart-1])) {
980 FLUSH_BLOCK_ONLY(s, 0); 1155 FLUSH_BLOCK_ONLY(s, 0);
981 } 1156 }
982 s->strstart++; 1157 s->strstart++;
@@ -994,7 +1169,7 @@ local int deflate_slow(s, flush)
994 Assert (flush != Z_NO_FLUSH, "no flush?"); 1169 Assert (flush != Z_NO_FLUSH, "no flush?");
995 if (s->match_available) { 1170 if (s->match_available) {
996 Tracevv((stderr,"%c", s->window[s->strstart-1])); 1171 Tracevv((stderr,"%c", s->window[s->strstart-1]));
997 ct_tally (s, 0, s->window[s->strstart-1]); 1172 _tr_tally (s, 0, s->window[s->strstart-1]);
998 s->match_available = 0; 1173 s->match_available = 0;
999 } 1174 }
1000 FLUSH_BLOCK(s, flush == Z_FINISH); 1175 FLUSH_BLOCK(s, flush == Z_FINISH);