aboutsummaryrefslogtreecommitdiff
path: root/deflate.c
diff options
context:
space:
mode:
authorMark Adler <madler@alumni.caltech.edu>2023-04-14 01:42:03 -0700
committerMark Adler <madler@alumni.caltech.edu>2023-04-15 21:17:31 -0700
commite9d5486e6635141f589e110fd789648aa08e9544 (patch)
treea78b9ccd92b05af7cd5776b688d9c3eb3a81a40a /deflate.c
parent5799c14c8526bf1aaa130c021982f831d155b46d (diff)
downloadzlib-e9d5486e6635141f589e110fd789648aa08e9544.tar.gz
zlib-e9d5486e6635141f589e110fd789648aa08e9544.tar.bz2
zlib-e9d5486e6635141f589e110fd789648aa08e9544.zip
Remove K&R function definitions from zlib.
C2X has removed K&R definitions from the C function syntax. Though the standard has not yet been approved, some high-profile compilers are now issuing warnings when such definitions are encountered.
Diffstat (limited to 'deflate.c')
-rw-r--r--deflate.c557
1 files changed, 224 insertions, 333 deletions
diff --git a/deflate.c b/deflate.c
index 4a512e1..c271d01 100644
--- a/deflate.c
+++ b/deflate.c
@@ -60,9 +60,6 @@ const char deflate_copyright[] =
60 copyright string in the executable of your product. 60 copyright string in the executable of your product.
61 */ 61 */
62 62
63/* ===========================================================================
64 * Function prototypes.
65 */
66typedef enum { 63typedef enum {
67 need_more, /* block not completed, need more input or more output */ 64 need_more, /* block not completed, need more input or more output */
68 block_done, /* block flush performed */ 65 block_done, /* block flush performed */
@@ -70,29 +67,16 @@ typedef enum {
70 finish_done /* finish done, accept no more input or output */ 67 finish_done /* finish done, accept no more input or output */
71} block_state; 68} block_state;
72 69
73typedef block_state (*compress_func) OF((deflate_state *s, int flush)); 70typedef block_state (*compress_func)(deflate_state *s, int flush);
74/* Compression function. Returns the block state after the call. */ 71/* Compression function. Returns the block state after the call. */
75 72
76local int deflateStateCheck OF((z_streamp strm)); 73local block_state deflate_stored(deflate_state *s, int flush);
77local void slide_hash OF((deflate_state *s)); 74local block_state deflate_fast(deflate_state *s, int flush);
78local void fill_window OF((deflate_state *s));
79local block_state deflate_stored OF((deflate_state *s, int flush));
80local block_state deflate_fast OF((deflate_state *s, int flush));
81#ifndef FASTEST 75#ifndef FASTEST
82local block_state deflate_slow OF((deflate_state *s, int flush)); 76local block_state deflate_slow(deflate_state *s, int flush);
83#endif
84local block_state deflate_rle OF((deflate_state *s, int flush));
85local block_state deflate_huff OF((deflate_state *s, int flush));
86local void lm_init OF((deflate_state *s));
87local void putShortMSB OF((deflate_state *s, uInt b));
88local void flush_pending OF((z_streamp strm));
89local unsigned read_buf OF((z_streamp strm, Bytef *buf, unsigned size));
90local uInt longest_match OF((deflate_state *s, IPos cur_match));
91
92#ifdef ZLIB_DEBUG
93local void check_match OF((deflate_state *s, IPos start, IPos match,
94 int length));
95#endif 77#endif
78local block_state deflate_rle(deflate_state *s, int flush);
79local block_state deflate_huff(deflate_state *s, int flush);
96 80
97/* =========================================================================== 81/* ===========================================================================
98 * Local data 82 * Local data
@@ -195,9 +179,7 @@ local const config configuration_table[10] = {
195 * bit values at the expense of memory usage). We slide even when level == 0 to 179 * bit values at the expense of memory usage). We slide even when level == 0 to
196 * keep the hash table consistent if we switch back to level > 0 later. 180 * keep the hash table consistent if we switch back to level > 0 later.
197 */ 181 */
198local void slide_hash(s) 182local void slide_hash(deflate_state *s) {
199 deflate_state *s;
200{
201 unsigned n, m; 183 unsigned n, m;
202 Posf *p; 184 Posf *p;
203 uInt wsize = s->w_size; 185 uInt wsize = s->w_size;
@@ -221,30 +203,177 @@ local void slide_hash(s)
221#endif 203#endif
222} 204}
223 205
206/* ===========================================================================
207 * Read a new buffer from the current input stream, update the adler32
208 * and total number of bytes read. All deflate() input goes through
209 * this function so some applications may wish to modify it to avoid
210 * allocating a large strm->next_in buffer and copying from it.
211 * (See also flush_pending()).
212 */
213local unsigned read_buf(z_streamp strm, Bytef *buf, unsigned size) {
214 unsigned len = strm->avail_in;
215
216 if (len > size) len = size;
217 if (len == 0) return 0;
218
219 strm->avail_in -= len;
220
221 zmemcpy(buf, strm->next_in, len);
222 if (strm->state->wrap == 1) {
223 strm->adler = adler32(strm->adler, buf, len);
224 }
225#ifdef GZIP
226 else if (strm->state->wrap == 2) {
227 strm->adler = crc32(strm->adler, buf, len);
228 }
229#endif
230 strm->next_in += len;
231 strm->total_in += len;
232
233 return len;
234}
235
236/* ===========================================================================
237 * Fill the window when the lookahead becomes insufficient.
238 * Updates strstart and lookahead.
239 *
240 * IN assertion: lookahead < MIN_LOOKAHEAD
241 * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
242 * At least one byte has been read, or avail_in == 0; reads are
243 * performed for at least two bytes (required for the zip translate_eol
244 * option -- not supported here).
245 */
246local void fill_window(deflate_state *s) {
247 unsigned n;
248 unsigned more; /* Amount of free space at the end of the window. */
249 uInt wsize = s->w_size;
250
251 Assert(s->lookahead < MIN_LOOKAHEAD, "already enough lookahead");
252
253 do {
254 more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart);
255
256 /* Deal with !@#$% 64K limit: */
257 if (sizeof(int) <= 2) {
258 if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
259 more = wsize;
260
261 } else if (more == (unsigned)(-1)) {
262 /* Very unlikely, but possible on 16 bit machine if
263 * strstart == 0 && lookahead == 1 (input done a byte at time)
264 */
265 more--;
266 }
267 }
268
269 /* If the window is almost full and there is insufficient lookahead,
270 * move the upper half to the lower one to make room in the upper half.
271 */
272 if (s->strstart >= wsize + MAX_DIST(s)) {
273
274 zmemcpy(s->window, s->window + wsize, (unsigned)wsize - more);
275 s->match_start -= wsize;
276 s->strstart -= wsize; /* we now have strstart >= MAX_DIST */
277 s->block_start -= (long) wsize;
278 if (s->insert > s->strstart)
279 s->insert = s->strstart;
280 slide_hash(s);
281 more += wsize;
282 }
283 if (s->strm->avail_in == 0) break;
284
285 /* If there was no sliding:
286 * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
287 * more == window_size - lookahead - strstart
288 * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
289 * => more >= window_size - 2*WSIZE + 2
290 * In the BIG_MEM or MMAP case (not yet supported),
291 * window_size == input_size + MIN_LOOKAHEAD &&
292 * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
293 * Otherwise, window_size == 2*WSIZE so more >= 2.
294 * If there was sliding, more >= WSIZE. So in all cases, more >= 2.
295 */
296 Assert(more >= 2, "more < 2");
297
298 n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more);
299 s->lookahead += n;
300
301 /* Initialize the hash value now that we have some input: */
302 if (s->lookahead + s->insert >= MIN_MATCH) {
303 uInt str = s->strstart - s->insert;
304 s->ins_h = s->window[str];
305 UPDATE_HASH(s, s->ins_h, s->window[str + 1]);
306#if MIN_MATCH != 3
307 Call UPDATE_HASH() MIN_MATCH-3 more times
308#endif
309 while (s->insert) {
310 UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]);
311#ifndef FASTEST
312 s->prev[str & s->w_mask] = s->head[s->ins_h];
313#endif
314 s->head[s->ins_h] = (Pos)str;
315 str++;
316 s->insert--;
317 if (s->lookahead + s->insert < MIN_MATCH)
318 break;
319 }
320 }
321 /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
322 * but this is not important since only literal bytes will be emitted.
323 */
324
325 } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0);
326
327 /* If the WIN_INIT bytes after the end of the current data have never been
328 * written, then zero those bytes in order to avoid memory check reports of
329 * the use of uninitialized (or uninitialised as Julian writes) bytes by
330 * the longest match routines. Update the high water mark for the next
331 * time through here. WIN_INIT is set to MAX_MATCH since the longest match
332 * routines allow scanning to strstart + MAX_MATCH, ignoring lookahead.
333 */
334 if (s->high_water < s->window_size) {
335 ulg curr = s->strstart + (ulg)(s->lookahead);
336 ulg init;
337
338 if (s->high_water < curr) {
339 /* Previous high water mark below current data -- zero WIN_INIT
340 * bytes or up to end of window, whichever is less.
341 */
342 init = s->window_size - curr;
343 if (init > WIN_INIT)
344 init = WIN_INIT;
345 zmemzero(s->window + curr, (unsigned)init);
346 s->high_water = curr + init;
347 }
348 else if (s->high_water < (ulg)curr + WIN_INIT) {
349 /* High water mark at or above current data, but below current data
350 * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up
351 * to end of window, whichever is less.
352 */
353 init = (ulg)curr + WIN_INIT - s->high_water;
354 if (init > s->window_size - s->high_water)
355 init = s->window_size - s->high_water;
356 zmemzero(s->window + s->high_water, (unsigned)init);
357 s->high_water += init;
358 }
359 }
360
361 Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD,
362 "not enough room for search");
363}
364
224/* ========================================================================= */ 365/* ========================================================================= */
225int ZEXPORT deflateInit_(strm, level, version, stream_size) 366int ZEXPORT deflateInit_(z_streamp strm, int level, const char *version,
226 z_streamp strm; 367 int stream_size) {
227 int level;
228 const char *version;
229 int stream_size;
230{
231 return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL, 368 return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL,
232 Z_DEFAULT_STRATEGY, version, stream_size); 369 Z_DEFAULT_STRATEGY, version, stream_size);
233 /* To do: ignore strm->next_in if we use it as window */ 370 /* To do: ignore strm->next_in if we use it as window */
234} 371}
235 372
236/* ========================================================================= */ 373/* ========================================================================= */
237int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy, 374int ZEXPORT deflateInit2_(z_streamp strm, int level, int method,
238 version, stream_size) 375 int windowBits, int memLevel, int strategy,
239 z_streamp strm; 376 const char *version, int stream_size) {
240 int level;
241 int method;
242 int windowBits;
243 int memLevel;
244 int strategy;
245 const char *version;
246 int stream_size;
247{
248 deflate_state *s; 377 deflate_state *s;
249 int wrap = 1; 378 int wrap = 1;
250 static const char my_version[] = ZLIB_VERSION; 379 static const char my_version[] = ZLIB_VERSION;
@@ -386,9 +515,7 @@ int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy,
386/* ========================================================================= 515/* =========================================================================
387 * Check for a valid deflate stream state. Return 0 if ok, 1 if not. 516 * Check for a valid deflate stream state. Return 0 if ok, 1 if not.
388 */ 517 */
389local int deflateStateCheck(strm) 518local int deflateStateCheck(z_streamp strm) {
390 z_streamp strm;
391{
392 deflate_state *s; 519 deflate_state *s;
393 if (strm == Z_NULL || 520 if (strm == Z_NULL ||
394 strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0) 521 strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0)
@@ -409,11 +536,8 @@ local int deflateStateCheck(strm)
409} 536}
410 537
411/* ========================================================================= */ 538/* ========================================================================= */
412int ZEXPORT deflateSetDictionary(strm, dictionary, dictLength) 539int ZEXPORT deflateSetDictionary(z_streamp strm, const Bytef *dictionary,
413 z_streamp strm; 540 uInt dictLength) {
414 const Bytef *dictionary;
415 uInt dictLength;
416{
417 deflate_state *s; 541 deflate_state *s;
418 uInt str, n; 542 uInt str, n;
419 int wrap; 543 int wrap;
@@ -478,11 +602,8 @@ int ZEXPORT deflateSetDictionary(strm, dictionary, dictLength)
478} 602}
479 603
480/* ========================================================================= */ 604/* ========================================================================= */
481int ZEXPORT deflateGetDictionary(strm, dictionary, dictLength) 605int ZEXPORT deflateGetDictionary(z_streamp strm, Bytef *dictionary,
482 z_streamp strm; 606 uInt *dictLength) {
483 Bytef *dictionary;
484 uInt *dictLength;
485{
486 deflate_state *s; 607 deflate_state *s;
487 uInt len; 608 uInt len;
488 609
@@ -500,9 +621,7 @@ int ZEXPORT deflateGetDictionary(strm, dictionary, dictLength)
500} 621}
501 622
502/* ========================================================================= */ 623/* ========================================================================= */
503int ZEXPORT deflateResetKeep(strm) 624int ZEXPORT deflateResetKeep(z_streamp strm) {
504 z_streamp strm;
505{
506 deflate_state *s; 625 deflate_state *s;
507 626
508 if (deflateStateCheck(strm)) { 627 if (deflateStateCheck(strm)) {
@@ -537,10 +656,32 @@ int ZEXPORT deflateResetKeep(strm)
537 return Z_OK; 656 return Z_OK;
538} 657}
539 658
659/* ===========================================================================
660 * Initialize the "longest match" routines for a new zlib stream
661 */
662local void lm_init(deflate_state *s) {
663 s->window_size = (ulg)2L*s->w_size;
664
665 CLEAR_HASH(s);
666
667 /* Set the default configuration parameters:
668 */
669 s->max_lazy_match = configuration_table[s->level].max_lazy;
670 s->good_match = configuration_table[s->level].good_length;
671 s->nice_match = configuration_table[s->level].nice_length;
672 s->max_chain_length = configuration_table[s->level].max_chain;
673
674 s->strstart = 0;
675 s->block_start = 0L;
676 s->lookahead = 0;
677 s->insert = 0;
678 s->match_length = s->prev_length = MIN_MATCH-1;
679 s->match_available = 0;
680 s->ins_h = 0;
681}
682
540/* ========================================================================= */ 683/* ========================================================================= */
541int ZEXPORT deflateReset(strm) 684int ZEXPORT deflateReset(z_streamp strm) {
542 z_streamp strm;
543{
544 int ret; 685 int ret;
545 686
546 ret = deflateResetKeep(strm); 687 ret = deflateResetKeep(strm);
@@ -550,10 +691,7 @@ int ZEXPORT deflateReset(strm)
550} 691}
551 692
552/* ========================================================================= */ 693/* ========================================================================= */
553int ZEXPORT deflateSetHeader(strm, head) 694int ZEXPORT deflateSetHeader(z_streamp strm, gz_headerp head) {
554 z_streamp strm;
555 gz_headerp head;
556{
557 if (deflateStateCheck(strm) || strm->state->wrap != 2) 695 if (deflateStateCheck(strm) || strm->state->wrap != 2)
558 return Z_STREAM_ERROR; 696 return Z_STREAM_ERROR;
559 strm->state->gzhead = head; 697 strm->state->gzhead = head;
@@ -561,11 +699,7 @@ int ZEXPORT deflateSetHeader(strm, head)
561} 699}
562 700
563/* ========================================================================= */ 701/* ========================================================================= */
564int ZEXPORT deflatePending(strm, pending, bits) 702int ZEXPORT deflatePending(z_streamp strm, unsigned *pending, int *bits) {
565 unsigned *pending;
566 int *bits;
567 z_streamp strm;
568{
569 if (deflateStateCheck(strm)) return Z_STREAM_ERROR; 703 if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
570 if (pending != Z_NULL) 704 if (pending != Z_NULL)
571 *pending = strm->state->pending; 705 *pending = strm->state->pending;
@@ -575,11 +709,7 @@ int ZEXPORT deflatePending(strm, pending, bits)
575} 709}
576 710
577/* ========================================================================= */ 711/* ========================================================================= */
578int ZEXPORT deflatePrime(strm, bits, value) 712int ZEXPORT deflatePrime(z_streamp strm, int bits, int value) {
579 z_streamp strm;
580 int bits;
581 int value;
582{
583 deflate_state *s; 713 deflate_state *s;
584 int put; 714 int put;
585 715
@@ -602,11 +732,7 @@ int ZEXPORT deflatePrime(strm, bits, value)
602} 732}
603 733
604/* ========================================================================= */ 734/* ========================================================================= */
605int ZEXPORT deflateParams(strm, level, strategy) 735int ZEXPORT deflateParams(z_streamp strm, int level, int strategy) {
606 z_streamp strm;
607 int level;
608 int strategy;
609{
610 deflate_state *s; 736 deflate_state *s;
611 compress_func func; 737 compress_func func;
612 738
@@ -651,13 +777,8 @@ int ZEXPORT deflateParams(strm, level, strategy)
651} 777}
652 778
653/* ========================================================================= */ 779/* ========================================================================= */
654int ZEXPORT deflateTune(strm, good_length, max_lazy, nice_length, max_chain) 780int ZEXPORT deflateTune(z_streamp strm, int good_length, int max_lazy,
655 z_streamp strm; 781 int nice_length, int max_chain) {
656 int good_length;
657 int max_lazy;
658 int nice_length;
659 int max_chain;
660{
661 deflate_state *s; 782 deflate_state *s;
662 783
663 if (deflateStateCheck(strm)) return Z_STREAM_ERROR; 784 if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
@@ -693,10 +814,7 @@ int ZEXPORT deflateTune(strm, good_length, max_lazy, nice_length, max_chain)
693 * 814 *
694 * Shifts are used to approximate divisions, for speed. 815 * Shifts are used to approximate divisions, for speed.
695 */ 816 */
696uLong ZEXPORT deflateBound(strm, sourceLen) 817uLong ZEXPORT deflateBound(z_streamp strm, uLong sourceLen) {
697 z_streamp strm;
698 uLong sourceLen;
699{
700 deflate_state *s; 818 deflate_state *s;
701 uLong fixedlen, storelen, wraplen; 819 uLong fixedlen, storelen, wraplen;
702 820
@@ -766,10 +884,7 @@ uLong ZEXPORT deflateBound(strm, sourceLen)
766 * IN assertion: the stream state is correct and there is enough room in 884 * IN assertion: the stream state is correct and there is enough room in
767 * pending_buf. 885 * pending_buf.
768 */ 886 */
769local void putShortMSB(s, b) 887local void putShortMSB(deflate_state *s, uInt b) {
770 deflate_state *s;
771 uInt b;
772{
773 put_byte(s, (Byte)(b >> 8)); 888 put_byte(s, (Byte)(b >> 8));
774 put_byte(s, (Byte)(b & 0xff)); 889 put_byte(s, (Byte)(b & 0xff));
775} 890}
@@ -780,9 +895,7 @@ local void putShortMSB(s, b)
780 * applications may wish to modify it to avoid allocating a large 895 * applications may wish to modify it to avoid allocating a large
781 * strm->next_out buffer and copying into it. (See also read_buf()). 896 * strm->next_out buffer and copying into it. (See also read_buf()).
782 */ 897 */
783local void flush_pending(strm) 898local void flush_pending(z_streamp strm) {
784 z_streamp strm;
785{
786 unsigned len; 899 unsigned len;
787 deflate_state *s = strm->state; 900 deflate_state *s = strm->state;
788 901
@@ -813,10 +926,7 @@ local void flush_pending(strm)
813 } while (0) 926 } while (0)
814 927
815/* ========================================================================= */ 928/* ========================================================================= */
816int ZEXPORT deflate(strm, flush) 929int ZEXPORT deflate(z_streamp strm, int flush) {
817 z_streamp strm;
818 int flush;
819{
820 int old_flush; /* value of flush param for previous deflate call */ 930 int old_flush; /* value of flush param for previous deflate call */
821 deflate_state *s; 931 deflate_state *s;
822 932
@@ -1128,9 +1238,7 @@ int ZEXPORT deflate(strm, flush)
1128} 1238}
1129 1239
1130/* ========================================================================= */ 1240/* ========================================================================= */
1131int ZEXPORT deflateEnd(strm) 1241int ZEXPORT deflateEnd(z_streamp strm) {
1132 z_streamp strm;
1133{
1134 int status; 1242 int status;
1135 1243
1136 if (deflateStateCheck(strm)) return Z_STREAM_ERROR; 1244 if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
@@ -1154,11 +1262,10 @@ int ZEXPORT deflateEnd(strm)
1154 * To simplify the source, this is not supported for 16-bit MSDOS (which 1262 * To simplify the source, this is not supported for 16-bit MSDOS (which
1155 * doesn't have enough memory anyway to duplicate compression states). 1263 * doesn't have enough memory anyway to duplicate compression states).
1156 */ 1264 */
1157int ZEXPORT deflateCopy(dest, source) 1265int ZEXPORT deflateCopy(z_streamp dest, z_streamp source) {
1158 z_streamp dest;
1159 z_streamp source;
1160{
1161#ifdef MAXSEG_64K 1266#ifdef MAXSEG_64K
1267 (void)dest;
1268 (void)source;
1162 return Z_STREAM_ERROR; 1269 return Z_STREAM_ERROR;
1163#else 1270#else
1164 deflate_state *ds; 1271 deflate_state *ds;
@@ -1206,66 +1313,6 @@ int ZEXPORT deflateCopy(dest, source)
1206#endif /* MAXSEG_64K */ 1313#endif /* MAXSEG_64K */
1207} 1314}
1208 1315
1209/* ===========================================================================
1210 * Read a new buffer from the current input stream, update the adler32
1211 * and total number of bytes read. All deflate() input goes through
1212 * this function so some applications may wish to modify it to avoid
1213 * allocating a large strm->next_in buffer and copying from it.
1214 * (See also flush_pending()).
1215 */
1216local unsigned read_buf(strm, buf, size)
1217 z_streamp strm;
1218 Bytef *buf;
1219 unsigned size;
1220{
1221 unsigned len = strm->avail_in;
1222
1223 if (len > size) len = size;
1224 if (len == 0) return 0;
1225
1226 strm->avail_in -= len;
1227
1228 zmemcpy(buf, strm->next_in, len);
1229 if (strm->state->wrap == 1) {
1230 strm->adler = adler32(strm->adler, buf, len);
1231 }
1232#ifdef GZIP
1233 else if (strm->state->wrap == 2) {
1234 strm->adler = crc32(strm->adler, buf, len);
1235 }
1236#endif
1237 strm->next_in += len;
1238 strm->total_in += len;
1239
1240 return len;
1241}
1242
1243/* ===========================================================================
1244 * Initialize the "longest match" routines for a new zlib stream
1245 */
1246local void lm_init(s)
1247 deflate_state *s;
1248{
1249 s->window_size = (ulg)2L*s->w_size;
1250
1251 CLEAR_HASH(s);
1252
1253 /* Set the default configuration parameters:
1254 */
1255 s->max_lazy_match = configuration_table[s->level].max_lazy;
1256 s->good_match = configuration_table[s->level].good_length;
1257 s->nice_match = configuration_table[s->level].nice_length;
1258 s->max_chain_length = configuration_table[s->level].max_chain;
1259
1260 s->strstart = 0;
1261 s->block_start = 0L;
1262 s->lookahead = 0;
1263 s->insert = 0;
1264 s->match_length = s->prev_length = MIN_MATCH-1;
1265 s->match_available = 0;
1266 s->ins_h = 0;
1267}
1268
1269#ifndef FASTEST 1316#ifndef FASTEST
1270/* =========================================================================== 1317/* ===========================================================================
1271 * Set match_start to the longest match starting at the given string and 1318 * Set match_start to the longest match starting at the given string and
@@ -1276,10 +1323,7 @@ local void lm_init(s)
1276 * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1 1323 * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
1277 * OUT assertion: the match length is not greater than s->lookahead. 1324 * OUT assertion: the match length is not greater than s->lookahead.
1278 */ 1325 */
1279local uInt longest_match(s, cur_match) 1326local uInt longest_match(deflate_state *s, IPos cur_match) {
1280 deflate_state *s;
1281 IPos cur_match; /* current match */
1282{
1283 unsigned chain_length = s->max_chain_length;/* max hash chain length */ 1327 unsigned chain_length = s->max_chain_length;/* max hash chain length */
1284 register Bytef *scan = s->window + s->strstart; /* current string */ 1328 register Bytef *scan = s->window + s->strstart; /* current string */
1285 register Bytef *match; /* matched string */ 1329 register Bytef *match; /* matched string */
@@ -1427,10 +1471,7 @@ local uInt longest_match(s, cur_match)
1427/* --------------------------------------------------------------------------- 1471/* ---------------------------------------------------------------------------
1428 * Optimized version for FASTEST only 1472 * Optimized version for FASTEST only
1429 */ 1473 */
1430local uInt longest_match(s, cur_match) 1474local uInt longest_match(deflate_state *s, IPos cur_match) {
1431 deflate_state *s;
1432 IPos cur_match; /* current match */
1433{
1434 register Bytef *scan = s->window + s->strstart; /* current string */ 1475 register Bytef *scan = s->window + s->strstart; /* current string */
1435 register Bytef *match; /* matched string */ 1476 register Bytef *match; /* matched string */
1436 register int len; /* length of current match */ 1477 register int len; /* length of current match */
@@ -1491,11 +1532,7 @@ local uInt longest_match(s, cur_match)
1491/* =========================================================================== 1532/* ===========================================================================
1492 * Check that the match at match_start is indeed a match. 1533 * Check that the match at match_start is indeed a match.
1493 */ 1534 */
1494local void check_match(s, start, match, length) 1535local void check_match(deflate_state *s, IPos start, IPos match, int length) {
1495 deflate_state *s;
1496 IPos start, match;
1497 int length;
1498{
1499 /* check that the match is indeed a match */ 1536 /* check that the match is indeed a match */
1500 if (zmemcmp(s->window + match, 1537 if (zmemcmp(s->window + match,
1501 s->window + start, length) != EQUAL) { 1538 s->window + start, length) != EQUAL) {
@@ -1516,137 +1553,6 @@ local void check_match(s, start, match, length)
1516#endif /* ZLIB_DEBUG */ 1553#endif /* ZLIB_DEBUG */
1517 1554
1518/* =========================================================================== 1555/* ===========================================================================
1519 * Fill the window when the lookahead becomes insufficient.
1520 * Updates strstart and lookahead.
1521 *
1522 * IN assertion: lookahead < MIN_LOOKAHEAD
1523 * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
1524 * At least one byte has been read, or avail_in == 0; reads are
1525 * performed for at least two bytes (required for the zip translate_eol
1526 * option -- not supported here).
1527 */
1528local void fill_window(s)
1529 deflate_state *s;
1530{
1531 unsigned n;
1532 unsigned more; /* Amount of free space at the end of the window. */
1533 uInt wsize = s->w_size;
1534
1535 Assert(s->lookahead < MIN_LOOKAHEAD, "already enough lookahead");
1536
1537 do {
1538 more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart);
1539
1540 /* Deal with !@#$% 64K limit: */
1541 if (sizeof(int) <= 2) {
1542 if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
1543 more = wsize;
1544
1545 } else if (more == (unsigned)(-1)) {
1546 /* Very unlikely, but possible on 16 bit machine if
1547 * strstart == 0 && lookahead == 1 (input done a byte at time)
1548 */
1549 more--;
1550 }
1551 }
1552
1553 /* If the window is almost full and there is insufficient lookahead,
1554 * move the upper half to the lower one to make room in the upper half.
1555 */
1556 if (s->strstart >= wsize + MAX_DIST(s)) {
1557
1558 zmemcpy(s->window, s->window + wsize, (unsigned)wsize - more);
1559 s->match_start -= wsize;
1560 s->strstart -= wsize; /* we now have strstart >= MAX_DIST */
1561 s->block_start -= (long) wsize;
1562 if (s->insert > s->strstart)
1563 s->insert = s->strstart;
1564 slide_hash(s);
1565 more += wsize;
1566 }
1567 if (s->strm->avail_in == 0) break;
1568
1569 /* If there was no sliding:
1570 * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
1571 * more == window_size - lookahead - strstart
1572 * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
1573 * => more >= window_size - 2*WSIZE + 2
1574 * In the BIG_MEM or MMAP case (not yet supported),
1575 * window_size == input_size + MIN_LOOKAHEAD &&
1576 * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
1577 * Otherwise, window_size == 2*WSIZE so more >= 2.
1578 * If there was sliding, more >= WSIZE. So in all cases, more >= 2.
1579 */
1580 Assert(more >= 2, "more < 2");
1581
1582 n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more);
1583 s->lookahead += n;
1584
1585 /* Initialize the hash value now that we have some input: */
1586 if (s->lookahead + s->insert >= MIN_MATCH) {
1587 uInt str = s->strstart - s->insert;
1588 s->ins_h = s->window[str];
1589 UPDATE_HASH(s, s->ins_h, s->window[str + 1]);
1590#if MIN_MATCH != 3
1591 Call UPDATE_HASH() MIN_MATCH-3 more times
1592#endif
1593 while (s->insert) {
1594 UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]);
1595#ifndef FASTEST
1596 s->prev[str & s->w_mask] = s->head[s->ins_h];
1597#endif
1598 s->head[s->ins_h] = (Pos)str;
1599 str++;
1600 s->insert--;
1601 if (s->lookahead + s->insert < MIN_MATCH)
1602 break;
1603 }
1604 }
1605 /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
1606 * but this is not important since only literal bytes will be emitted.
1607 */
1608
1609 } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0);
1610
1611 /* If the WIN_INIT bytes after the end of the current data have never been
1612 * written, then zero those bytes in order to avoid memory check reports of
1613 * the use of uninitialized (or uninitialised as Julian writes) bytes by
1614 * the longest match routines. Update the high water mark for the next
1615 * time through here. WIN_INIT is set to MAX_MATCH since the longest match
1616 * routines allow scanning to strstart + MAX_MATCH, ignoring lookahead.
1617 */
1618 if (s->high_water < s->window_size) {
1619 ulg curr = s->strstart + (ulg)(s->lookahead);
1620 ulg init;
1621
1622 if (s->high_water < curr) {
1623 /* Previous high water mark below current data -- zero WIN_INIT
1624 * bytes or up to end of window, whichever is less.
1625 */
1626 init = s->window_size - curr;
1627 if (init > WIN_INIT)
1628 init = WIN_INIT;
1629 zmemzero(s->window + curr, (unsigned)init);
1630 s->high_water = curr + init;
1631 }
1632 else if (s->high_water < (ulg)curr + WIN_INIT) {
1633 /* High water mark at or above current data, but below current data
1634 * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up
1635 * to end of window, whichever is less.
1636 */
1637 init = (ulg)curr + WIN_INIT - s->high_water;
1638 if (init > s->window_size - s->high_water)
1639 init = s->window_size - s->high_water;
1640 zmemzero(s->window + s->high_water, (unsigned)init);
1641 s->high_water += init;
1642 }
1643 }
1644
1645 Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD,
1646 "not enough room for search");
1647}
1648
1649/* ===========================================================================
1650 * Flush the current block, with given end-of-file flag. 1556 * Flush the current block, with given end-of-file flag.
1651 * IN assertion: strstart is set to the end of the current match. 1557 * IN assertion: strstart is set to the end of the current match.
1652 */ 1558 */
@@ -1688,10 +1594,7 @@ local void fill_window(s)
1688 * copied. It is most efficient with large input and output buffers, which 1594 * copied. It is most efficient with large input and output buffers, which
1689 * maximizes the opportunities to have a single copy from next_in to next_out. 1595 * maximizes the opportunities to have a single copy from next_in to next_out.
1690 */ 1596 */
1691local block_state deflate_stored(s, flush) 1597local block_state deflate_stored(deflate_state *s, int flush) {
1692 deflate_state *s;
1693 int flush;
1694{
1695 /* Smallest worthy block size when not flushing or finishing. By default 1598 /* Smallest worthy block size when not flushing or finishing. By default
1696 * this is 32K. This can be as small as 507 bytes for memLevel == 1. For 1599 * this is 32K. This can be as small as 507 bytes for memLevel == 1. For
1697 * large input and output buffers, the stored block size will be larger. 1600 * large input and output buffers, the stored block size will be larger.
@@ -1875,10 +1778,7 @@ local block_state deflate_stored(s, flush)
1875 * new strings in the dictionary only for unmatched strings or for short 1778 * new strings in the dictionary only for unmatched strings or for short
1876 * matches. It is used only for the fast compression options. 1779 * matches. It is used only for the fast compression options.
1877 */ 1780 */
1878local block_state deflate_fast(s, flush) 1781local block_state deflate_fast(deflate_state *s, int flush) {
1879 deflate_state *s;
1880 int flush;
1881{
1882 IPos hash_head; /* head of the hash chain */ 1782 IPos hash_head; /* head of the hash chain */
1883 int bflush; /* set if current block must be flushed */ 1783 int bflush; /* set if current block must be flushed */
1884 1784
@@ -1977,10 +1877,7 @@ local block_state deflate_fast(s, flush)
1977 * evaluation for matches: a match is finally adopted only if there is 1877 * evaluation for matches: a match is finally adopted only if there is
1978 * no better match at the next window position. 1878 * no better match at the next window position.
1979 */ 1879 */
1980local block_state deflate_slow(s, flush) 1880local block_state deflate_slow(deflate_state *s, int flush) {
1981 deflate_state *s;
1982 int flush;
1983{
1984 IPos hash_head; /* head of hash chain */ 1881 IPos hash_head; /* head of hash chain */
1985 int bflush; /* set if current block must be flushed */ 1882 int bflush; /* set if current block must be flushed */
1986 1883
@@ -2108,10 +2005,7 @@ local block_state deflate_slow(s, flush)
2108 * one. Do not maintain a hash table. (It will be regenerated if this run of 2005 * one. Do not maintain a hash table. (It will be regenerated if this run of
2109 * deflate switches away from Z_RLE.) 2006 * deflate switches away from Z_RLE.)
2110 */ 2007 */
2111local block_state deflate_rle(s, flush) 2008local block_state deflate_rle(deflate_state *s, int flush) {
2112 deflate_state *s;
2113 int flush;
2114{
2115 int bflush; /* set if current block must be flushed */ 2009 int bflush; /* set if current block must be flushed */
2116 uInt prev; /* byte at distance one to match */ 2010 uInt prev; /* byte at distance one to match */
2117 Bytef *scan, *strend; /* scan goes up to strend for length of run */ 2011 Bytef *scan, *strend; /* scan goes up to strend for length of run */
@@ -2182,10 +2076,7 @@ local block_state deflate_rle(s, flush)
2182 * For Z_HUFFMAN_ONLY, do not look for matches. Do not maintain a hash table. 2076 * For Z_HUFFMAN_ONLY, do not look for matches. Do not maintain a hash table.
2183 * (It will be regenerated if this run of deflate switches away from Huffman.) 2077 * (It will be regenerated if this run of deflate switches away from Huffman.)
2184 */ 2078 */
2185local block_state deflate_huff(s, flush) 2079local block_state deflate_huff(deflate_state *s, int flush) {
2186 deflate_state *s;
2187 int flush;
2188{
2189 int bflush; /* set if current block must be flushed */ 2080 int bflush; /* set if current block must be flushed */
2190 2081
2191 for (;;) { 2082 for (;;) {