xref: /openbmc/linux/lib/zlib_deflate/deflate.c (revision 8730046c)
1 /* +++ deflate.c */
2 /* deflate.c -- compress data using the deflation algorithm
3  * Copyright (C) 1995-1996 Jean-loup Gailly.
4  * For conditions of distribution and use, see copyright notice in zlib.h
5  */
6 
7 /*
8  *  ALGORITHM
9  *
10  *      The "deflation" process depends on being able to identify portions
11  *      of the input text which are identical to earlier input (within a
12  *      sliding window trailing behind the input currently being processed).
13  *
14  *      The most straightforward technique turns out to be the fastest for
15  *      most input files: try all possible matches and select the longest.
16  *      The key feature of this algorithm is that insertions into the string
17  *      dictionary are very simple and thus fast, and deletions are avoided
18  *      completely. Insertions are performed at each input character, whereas
19  *      string matches are performed only when the previous match ends. So it
20  *      is preferable to spend more time in matches to allow very fast string
21  *      insertions and avoid deletions. The matching algorithm for small
22  *      strings is inspired from that of Rabin & Karp. A brute force approach
23  *      is used to find longer strings when a small match has been found.
24  *      A similar algorithm is used in comic (by Jan-Mark Wams) and freeze
25  *      (by Leonid Broukhis).
26  *         A previous version of this file used a more sophisticated algorithm
27  *      (by Fiala and Greene) which is guaranteed to run in linear amortized
28  *      time, but has a larger average cost, uses more memory and is patented.
29  *      However the F&G algorithm may be faster for some highly redundant
30  *      files if the parameter max_chain_length (described below) is too large.
31  *
32  *  ACKNOWLEDGEMENTS
33  *
34  *      The idea of lazy evaluation of matches is due to Jan-Mark Wams, and
35  *      I found it in 'freeze' written by Leonid Broukhis.
36  *      Thanks to many people for bug reports and testing.
37  *
38  *  REFERENCES
39  *
40  *      Deutsch, L.P.,"DEFLATE Compressed Data Format Specification".
41  *      Available in ftp://ds.internic.net/rfc/rfc1951.txt
42  *
43  *      A description of the Rabin and Karp algorithm is given in the book
44  *         "Algorithms" by R. Sedgewick, Addison-Wesley, p252.
45  *
46  *      Fiala,E.R., and Greene,D.H.
47  *         Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595
48  *
49  */
50 
51 #include <linux/module.h>
52 #include <linux/zutil.h>
53 #include "defutil.h"
54 
55 
56 /* ===========================================================================
57  *  Function prototypes.
58  */
59 typedef enum {
60     need_more,      /* block not completed, need more input or more output */
61     block_done,     /* block flush performed */
62     finish_started, /* finish started, need only more output at next deflate */
63     finish_done     /* finish done, accept no more input or output */
64 } block_state;
65 
66 typedef block_state (*compress_func) (deflate_state *s, int flush);
67 /* Compression function. Returns the block state after the call. */
68 
69 static void fill_window    (deflate_state *s);
70 static block_state deflate_stored (deflate_state *s, int flush);
71 static block_state deflate_fast   (deflate_state *s, int flush);
72 static block_state deflate_slow   (deflate_state *s, int flush);
73 static void lm_init        (deflate_state *s);
74 static void putShortMSB    (deflate_state *s, uInt b);
75 static void flush_pending  (z_streamp strm);
76 static int read_buf        (z_streamp strm, Byte *buf, unsigned size);
77 static uInt longest_match  (deflate_state *s, IPos cur_match);
78 
79 #ifdef DEBUG_ZLIB
80 static  void check_match (deflate_state *s, IPos start, IPos match,
81                          int length);
82 #endif
83 
84 /* ===========================================================================
85  * Local data
86  */
87 
88 #define NIL 0
89 /* Tail of hash chains */
90 
91 #ifndef TOO_FAR
92 #  define TOO_FAR 4096
93 #endif
94 /* Matches of length 3 are discarded if their distance exceeds TOO_FAR */
95 
96 #define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
97 /* Minimum amount of lookahead, except at the end of the input file.
98  * See deflate.c for comments about the MIN_MATCH+1.
99  */
100 
101 /* Values for max_lazy_match, good_match and max_chain_length, depending on
102  * the desired pack level (0..9). The values given below have been tuned to
103  * exclude worst case performance for pathological files. Better values may be
104  * found for specific files.
105  */
106 typedef struct config_s {
107    ush good_length; /* reduce lazy search above this match length */
108    ush max_lazy;    /* do not perform lazy search above this match length */
109    ush nice_length; /* quit search above this match length */
110    ush max_chain;
111    compress_func func;
112 } config;
113 
114 static const config configuration_table[10] = {
115 /*      good lazy nice chain */
116 /* 0 */ {0,    0,  0,    0, deflate_stored},  /* store only */
117 /* 1 */ {4,    4,  8,    4, deflate_fast}, /* maximum speed, no lazy matches */
118 /* 2 */ {4,    5, 16,    8, deflate_fast},
119 /* 3 */ {4,    6, 32,   32, deflate_fast},
120 
121 /* 4 */ {4,    4, 16,   16, deflate_slow},  /* lazy matches */
122 /* 5 */ {8,   16, 32,   32, deflate_slow},
123 /* 6 */ {8,   16, 128, 128, deflate_slow},
124 /* 7 */ {8,   32, 128, 256, deflate_slow},
125 /* 8 */ {32, 128, 258, 1024, deflate_slow},
126 /* 9 */ {32, 258, 258, 4096, deflate_slow}}; /* maximum compression */
127 
128 /* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4
129  * For deflate_fast() (levels <= 3) good is ignored and lazy has a different
130  * meaning.
131  */
132 
133 #define EQUAL 0
134 /* result of memcmp for equal strings */
135 
136 /* ===========================================================================
137  * Update a hash value with the given input byte
138  * IN  assertion: all calls to UPDATE_HASH are made with consecutive
139  *    input characters, so that a running hash key can be computed from the
140  *    previous key instead of complete recalculation each time.
141  */
142 #define UPDATE_HASH(s,h,c) (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask)
143 
144 
145 /* ===========================================================================
146  * Insert string str in the dictionary and set match_head to the previous head
147  * of the hash chain (the most recent string with same hash key). Return
148  * the previous length of the hash chain.
149  * IN  assertion: all calls to INSERT_STRING are made with consecutive
150  *    input characters and the first MIN_MATCH bytes of str are valid
151  *    (except for the last MIN_MATCH-1 bytes of the input file).
152  */
153 #define INSERT_STRING(s, str, match_head) \
154    (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
155     s->prev[(str) & s->w_mask] = match_head = s->head[s->ins_h], \
156     s->head[s->ins_h] = (Pos)(str))
157 
158 /* ===========================================================================
159  * Initialize the hash table (avoiding 64K overflow for 16 bit systems).
160  * prev[] will be initialized on the fly.
161  */
162 #define CLEAR_HASH(s) \
163     s->head[s->hash_size-1] = NIL; \
164     memset((char *)s->head, 0, (unsigned)(s->hash_size-1)*sizeof(*s->head));
165 
166 /* ========================================================================= */
167 int zlib_deflateInit2(
168 	z_streamp strm,
169 	int  level,
170 	int  method,
171 	int  windowBits,
172 	int  memLevel,
173 	int  strategy
174 )
175 {
176     deflate_state *s;
177     int noheader = 0;
178     deflate_workspace *mem;
179     char *next;
180 
181     ush *overlay;
182     /* We overlay pending_buf and d_buf+l_buf. This works since the average
183      * output size for (length,distance) codes is <= 24 bits.
184      */
185 
186     if (strm == NULL) return Z_STREAM_ERROR;
187 
188     strm->msg = NULL;
189 
190     if (level == Z_DEFAULT_COMPRESSION) level = 6;
191 
192     mem = (deflate_workspace *) strm->workspace;
193 
194     if (windowBits < 0) { /* undocumented feature: suppress zlib header */
195         noheader = 1;
196         windowBits = -windowBits;
197     }
198     if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED ||
199         windowBits < 9 || windowBits > 15 || level < 0 || level > 9 ||
200 	strategy < 0 || strategy > Z_HUFFMAN_ONLY) {
201         return Z_STREAM_ERROR;
202     }
203 
204     /*
205      * Direct the workspace's pointers to the chunks that were allocated
206      * along with the deflate_workspace struct.
207      */
208     next = (char *) mem;
209     next += sizeof(*mem);
210     mem->window_memory = (Byte *) next;
211     next += zlib_deflate_window_memsize(windowBits);
212     mem->prev_memory = (Pos *) next;
213     next += zlib_deflate_prev_memsize(windowBits);
214     mem->head_memory = (Pos *) next;
215     next += zlib_deflate_head_memsize(memLevel);
216     mem->overlay_memory = next;
217 
218     s = (deflate_state *) &(mem->deflate_memory);
219     strm->state = (struct internal_state *)s;
220     s->strm = strm;
221 
222     s->noheader = noheader;
223     s->w_bits = windowBits;
224     s->w_size = 1 << s->w_bits;
225     s->w_mask = s->w_size - 1;
226 
227     s->hash_bits = memLevel + 7;
228     s->hash_size = 1 << s->hash_bits;
229     s->hash_mask = s->hash_size - 1;
230     s->hash_shift =  ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH);
231 
232     s->window = (Byte *) mem->window_memory;
233     s->prev   = (Pos *)  mem->prev_memory;
234     s->head   = (Pos *)  mem->head_memory;
235 
236     s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
237 
238     overlay = (ush *) mem->overlay_memory;
239     s->pending_buf = (uch *) overlay;
240     s->pending_buf_size = (ulg)s->lit_bufsize * (sizeof(ush)+2L);
241 
242     s->d_buf = overlay + s->lit_bufsize/sizeof(ush);
243     s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize;
244 
245     s->level = level;
246     s->strategy = strategy;
247     s->method = (Byte)method;
248 
249     return zlib_deflateReset(strm);
250 }
251 
252 /* ========================================================================= */
253 int zlib_deflateReset(
254 	z_streamp strm
255 )
256 {
257     deflate_state *s;
258 
259     if (strm == NULL || strm->state == NULL)
260         return Z_STREAM_ERROR;
261 
262     strm->total_in = strm->total_out = 0;
263     strm->msg = NULL;
264     strm->data_type = Z_UNKNOWN;
265 
266     s = (deflate_state *)strm->state;
267     s->pending = 0;
268     s->pending_out = s->pending_buf;
269 
270     if (s->noheader < 0) {
271         s->noheader = 0; /* was set to -1 by deflate(..., Z_FINISH); */
272     }
273     s->status = s->noheader ? BUSY_STATE : INIT_STATE;
274     strm->adler = 1;
275     s->last_flush = Z_NO_FLUSH;
276 
277     zlib_tr_init(s);
278     lm_init(s);
279 
280     return Z_OK;
281 }
282 
283 /* =========================================================================
284  * Put a short in the pending buffer. The 16-bit value is put in MSB order.
285  * IN assertion: the stream state is correct and there is enough room in
286  * pending_buf.
287  */
288 static void putShortMSB(
289 	deflate_state *s,
290 	uInt b
291 )
292 {
293     put_byte(s, (Byte)(b >> 8));
294     put_byte(s, (Byte)(b & 0xff));
295 }
296 
297 /* =========================================================================
298  * Flush as much pending output as possible. All deflate() output goes
299  * through this function so some applications may wish to modify it
300  * to avoid allocating a large strm->next_out buffer and copying into it.
301  * (See also read_buf()).
302  */
303 static void flush_pending(
304 	z_streamp strm
305 )
306 {
307     deflate_state *s = (deflate_state *) strm->state;
308     unsigned len = s->pending;
309 
310     if (len > strm->avail_out) len = strm->avail_out;
311     if (len == 0) return;
312 
313     if (strm->next_out != NULL) {
314 	memcpy(strm->next_out, s->pending_out, len);
315 	strm->next_out += len;
316     }
317     s->pending_out += len;
318     strm->total_out += len;
319     strm->avail_out  -= len;
320     s->pending -= len;
321     if (s->pending == 0) {
322         s->pending_out = s->pending_buf;
323     }
324 }
325 
326 /* ========================================================================= */
327 int zlib_deflate(
328 	z_streamp strm,
329 	int flush
330 )
331 {
332     int old_flush; /* value of flush param for previous deflate call */
333     deflate_state *s;
334 
335     if (strm == NULL || strm->state == NULL ||
336 	flush > Z_FINISH || flush < 0) {
337         return Z_STREAM_ERROR;
338     }
339     s = (deflate_state *) strm->state;
340 
341     if ((strm->next_in == NULL && strm->avail_in != 0) ||
342 	(s->status == FINISH_STATE && flush != Z_FINISH)) {
343         return Z_STREAM_ERROR;
344     }
345     if (strm->avail_out == 0) return Z_BUF_ERROR;
346 
347     s->strm = strm; /* just in case */
348     old_flush = s->last_flush;
349     s->last_flush = flush;
350 
351     /* Write the zlib header */
352     if (s->status == INIT_STATE) {
353 
354         uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8;
355         uInt level_flags = (s->level-1) >> 1;
356 
357         if (level_flags > 3) level_flags = 3;
358         header |= (level_flags << 6);
359 	if (s->strstart != 0) header |= PRESET_DICT;
360         header += 31 - (header % 31);
361 
362         s->status = BUSY_STATE;
363         putShortMSB(s, header);
364 
365 	/* Save the adler32 of the preset dictionary: */
366 	if (s->strstart != 0) {
367 	    putShortMSB(s, (uInt)(strm->adler >> 16));
368 	    putShortMSB(s, (uInt)(strm->adler & 0xffff));
369 	}
370 	strm->adler = 1L;
371     }
372 
373     /* Flush as much pending output as possible */
374     if (s->pending != 0) {
375         flush_pending(strm);
376         if (strm->avail_out == 0) {
377 	    /* Since avail_out is 0, deflate will be called again with
378 	     * more output space, but possibly with both pending and
379 	     * avail_in equal to zero. There won't be anything to do,
380 	     * but this is not an error situation so make sure we
381 	     * return OK instead of BUF_ERROR at next call of deflate:
382              */
383 	    s->last_flush = -1;
384 	    return Z_OK;
385 	}
386 
387     /* Make sure there is something to do and avoid duplicate consecutive
388      * flushes. For repeated and useless calls with Z_FINISH, we keep
389      * returning Z_STREAM_END instead of Z_BUFF_ERROR.
390      */
391     } else if (strm->avail_in == 0 && flush <= old_flush &&
392 	       flush != Z_FINISH) {
393         return Z_BUF_ERROR;
394     }
395 
396     /* User must not provide more input after the first FINISH: */
397     if (s->status == FINISH_STATE && strm->avail_in != 0) {
398         return Z_BUF_ERROR;
399     }
400 
401     /* Start a new block or continue the current one.
402      */
403     if (strm->avail_in != 0 || s->lookahead != 0 ||
404         (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) {
405         block_state bstate;
406 
407 	bstate = (*(configuration_table[s->level].func))(s, flush);
408 
409         if (bstate == finish_started || bstate == finish_done) {
410             s->status = FINISH_STATE;
411         }
412         if (bstate == need_more || bstate == finish_started) {
413 	    if (strm->avail_out == 0) {
414 	        s->last_flush = -1; /* avoid BUF_ERROR next call, see above */
415 	    }
416 	    return Z_OK;
417 	    /* If flush != Z_NO_FLUSH && avail_out == 0, the next call
418 	     * of deflate should use the same flush parameter to make sure
419 	     * that the flush is complete. So we don't have to output an
420 	     * empty block here, this will be done at next call. This also
421 	     * ensures that for a very small output buffer, we emit at most
422 	     * one empty block.
423 	     */
424 	}
425         if (bstate == block_done) {
426             if (flush == Z_PARTIAL_FLUSH) {
427                 zlib_tr_align(s);
428 	    } else if (flush == Z_PACKET_FLUSH) {
429 		/* Output just the 3-bit `stored' block type value,
430 		   but not a zero length. */
431 		zlib_tr_stored_type_only(s);
432             } else { /* FULL_FLUSH or SYNC_FLUSH */
433                 zlib_tr_stored_block(s, (char*)0, 0L, 0);
434                 /* For a full flush, this empty block will be recognized
435                  * as a special marker by inflate_sync().
436                  */
437                 if (flush == Z_FULL_FLUSH) {
438                     CLEAR_HASH(s);             /* forget history */
439                 }
440             }
441             flush_pending(strm);
442 	    if (strm->avail_out == 0) {
443 	      s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */
444 	      return Z_OK;
445 	    }
446         }
447     }
448     Assert(strm->avail_out > 0, "bug2");
449 
450     if (flush != Z_FINISH) return Z_OK;
451     if (s->noheader) return Z_STREAM_END;
452 
453     /* Write the zlib trailer (adler32) */
454     putShortMSB(s, (uInt)(strm->adler >> 16));
455     putShortMSB(s, (uInt)(strm->adler & 0xffff));
456     flush_pending(strm);
457     /* If avail_out is zero, the application will call deflate again
458      * to flush the rest.
459      */
460     s->noheader = -1; /* write the trailer only once! */
461     return s->pending != 0 ? Z_OK : Z_STREAM_END;
462 }
463 
464 /* ========================================================================= */
465 int zlib_deflateEnd(
466 	z_streamp strm
467 )
468 {
469     int status;
470     deflate_state *s;
471 
472     if (strm == NULL || strm->state == NULL) return Z_STREAM_ERROR;
473     s = (deflate_state *) strm->state;
474 
475     status = s->status;
476     if (status != INIT_STATE && status != BUSY_STATE &&
477 	status != FINISH_STATE) {
478       return Z_STREAM_ERROR;
479     }
480 
481     strm->state = NULL;
482 
483     return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;
484 }
485 
486 /* ===========================================================================
487  * Read a new buffer from the current input stream, update the adler32
488  * and total number of bytes read.  All deflate() input goes through
489  * this function so some applications may wish to modify it to avoid
490  * allocating a large strm->next_in buffer and copying from it.
491  * (See also flush_pending()).
492  */
493 static int read_buf(
494 	z_streamp strm,
495 	Byte *buf,
496 	unsigned size
497 )
498 {
499     unsigned len = strm->avail_in;
500 
501     if (len > size) len = size;
502     if (len == 0) return 0;
503 
504     strm->avail_in  -= len;
505 
506     if (!((deflate_state *)(strm->state))->noheader) {
507         strm->adler = zlib_adler32(strm->adler, strm->next_in, len);
508     }
509     memcpy(buf, strm->next_in, len);
510     strm->next_in  += len;
511     strm->total_in += len;
512 
513     return (int)len;
514 }
515 
516 /* ===========================================================================
517  * Initialize the "longest match" routines for a new zlib stream
518  */
519 static void lm_init(
520 	deflate_state *s
521 )
522 {
523     s->window_size = (ulg)2L*s->w_size;
524 
525     CLEAR_HASH(s);
526 
527     /* Set the default configuration parameters:
528      */
529     s->max_lazy_match   = configuration_table[s->level].max_lazy;
530     s->good_match       = configuration_table[s->level].good_length;
531     s->nice_match       = configuration_table[s->level].nice_length;
532     s->max_chain_length = configuration_table[s->level].max_chain;
533 
534     s->strstart = 0;
535     s->block_start = 0L;
536     s->lookahead = 0;
537     s->match_length = s->prev_length = MIN_MATCH-1;
538     s->match_available = 0;
539     s->ins_h = 0;
540 }
541 
542 /* ===========================================================================
543  * Set match_start to the longest match starting at the given string and
544  * return its length. Matches shorter or equal to prev_length are discarded,
545  * in which case the result is equal to prev_length and match_start is
546  * garbage.
547  * IN assertions: cur_match is the head of the hash chain for the current
548  *   string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
549  * OUT assertion: the match length is not greater than s->lookahead.
550  */
551 /* For 80x86 and 680x0, an optimized version will be provided in match.asm or
552  * match.S. The code will be functionally equivalent.
553  */
554 static uInt longest_match(
555 	deflate_state *s,
556 	IPos cur_match			/* current match */
557 )
558 {
559     unsigned chain_length = s->max_chain_length;/* max hash chain length */
560     register Byte *scan = s->window + s->strstart; /* current string */
561     register Byte *match;                       /* matched string */
562     register int len;                           /* length of current match */
563     int best_len = s->prev_length;              /* best match length so far */
564     int nice_match = s->nice_match;             /* stop if match long enough */
565     IPos limit = s->strstart > (IPos)MAX_DIST(s) ?
566         s->strstart - (IPos)MAX_DIST(s) : NIL;
567     /* Stop when cur_match becomes <= limit. To simplify the code,
568      * we prevent matches with the string of window index 0.
569      */
570     Pos *prev = s->prev;
571     uInt wmask = s->w_mask;
572 
573 #ifdef UNALIGNED_OK
574     /* Compare two bytes at a time. Note: this is not always beneficial.
575      * Try with and without -DUNALIGNED_OK to check.
576      */
577     register Byte *strend = s->window + s->strstart + MAX_MATCH - 1;
578     register ush scan_start = *(ush*)scan;
579     register ush scan_end   = *(ush*)(scan+best_len-1);
580 #else
581     register Byte *strend = s->window + s->strstart + MAX_MATCH;
582     register Byte scan_end1  = scan[best_len-1];
583     register Byte scan_end   = scan[best_len];
584 #endif
585 
586     /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
587      * It is easy to get rid of this optimization if necessary.
588      */
589     Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
590 
591     /* Do not waste too much time if we already have a good match: */
592     if (s->prev_length >= s->good_match) {
593         chain_length >>= 2;
594     }
595     /* Do not look for matches beyond the end of the input. This is necessary
596      * to make deflate deterministic.
597      */
598     if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead;
599 
600     Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
601 
602     do {
603         Assert(cur_match < s->strstart, "no future");
604         match = s->window + cur_match;
605 
606         /* Skip to next match if the match length cannot increase
607          * or if the match length is less than 2:
608          */
609 #if (defined(UNALIGNED_OK) && MAX_MATCH == 258)
610         /* This code assumes sizeof(unsigned short) == 2. Do not use
611          * UNALIGNED_OK if your compiler uses a different size.
612          */
613         if (*(ush*)(match+best_len-1) != scan_end ||
614             *(ush*)match != scan_start) continue;
615 
616         /* It is not necessary to compare scan[2] and match[2] since they are
617          * always equal when the other bytes match, given that the hash keys
618          * are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at
619          * strstart+3, +5, ... up to strstart+257. We check for insufficient
620          * lookahead only every 4th comparison; the 128th check will be made
621          * at strstart+257. If MAX_MATCH-2 is not a multiple of 8, it is
622          * necessary to put more guard bytes at the end of the window, or
623          * to check more often for insufficient lookahead.
624          */
625         Assert(scan[2] == match[2], "scan[2]?");
626         scan++, match++;
627         do {
628         } while (*(ush*)(scan+=2) == *(ush*)(match+=2) &&
629                  *(ush*)(scan+=2) == *(ush*)(match+=2) &&
630                  *(ush*)(scan+=2) == *(ush*)(match+=2) &&
631                  *(ush*)(scan+=2) == *(ush*)(match+=2) &&
632                  scan < strend);
633         /* The funny "do {}" generates better code on most compilers */
634 
635         /* Here, scan <= window+strstart+257 */
636         Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
637         if (*scan == *match) scan++;
638 
639         len = (MAX_MATCH - 1) - (int)(strend-scan);
640         scan = strend - (MAX_MATCH-1);
641 
642 #else /* UNALIGNED_OK */
643 
644         if (match[best_len]   != scan_end  ||
645             match[best_len-1] != scan_end1 ||
646             *match            != *scan     ||
647             *++match          != scan[1])      continue;
648 
649         /* The check at best_len-1 can be removed because it will be made
650          * again later. (This heuristic is not always a win.)
651          * It is not necessary to compare scan[2] and match[2] since they
652          * are always equal when the other bytes match, given that
653          * the hash keys are equal and that HASH_BITS >= 8.
654          */
655         scan += 2, match++;
656         Assert(*scan == *match, "match[2]?");
657 
658         /* We check for insufficient lookahead only every 8th comparison;
659          * the 256th check will be made at strstart+258.
660          */
661         do {
662         } while (*++scan == *++match && *++scan == *++match &&
663                  *++scan == *++match && *++scan == *++match &&
664                  *++scan == *++match && *++scan == *++match &&
665                  *++scan == *++match && *++scan == *++match &&
666                  scan < strend);
667 
668         Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
669 
670         len = MAX_MATCH - (int)(strend - scan);
671         scan = strend - MAX_MATCH;
672 
673 #endif /* UNALIGNED_OK */
674 
675         if (len > best_len) {
676             s->match_start = cur_match;
677             best_len = len;
678             if (len >= nice_match) break;
679 #ifdef UNALIGNED_OK
680             scan_end = *(ush*)(scan+best_len-1);
681 #else
682             scan_end1  = scan[best_len-1];
683             scan_end   = scan[best_len];
684 #endif
685         }
686     } while ((cur_match = prev[cur_match & wmask]) > limit
687              && --chain_length != 0);
688 
689     if ((uInt)best_len <= s->lookahead) return best_len;
690     return s->lookahead;
691 }
692 
693 #ifdef DEBUG_ZLIB
694 /* ===========================================================================
695  * Check that the match at match_start is indeed a match.
696  */
697 static void check_match(
698 	deflate_state *s,
699 	IPos start,
700 	IPos match,
701 	int length
702 )
703 {
704     /* check that the match is indeed a match */
705     if (memcmp((char *)s->window + match,
706                 (char *)s->window + start, length) != EQUAL) {
707         fprintf(stderr, " start %u, match %u, length %d\n",
708 		start, match, length);
709         do {
710 	    fprintf(stderr, "%c%c", s->window[match++], s->window[start++]);
711 	} while (--length != 0);
712         z_error("invalid match");
713     }
714     if (z_verbose > 1) {
715         fprintf(stderr,"\\[%d,%d]", start-match, length);
716         do { putc(s->window[start++], stderr); } while (--length != 0);
717     }
718 }
719 #else
720 #  define check_match(s, start, match, length)
721 #endif
722 
723 /* ===========================================================================
724  * Fill the window when the lookahead becomes insufficient.
725  * Updates strstart and lookahead.
726  *
727  * IN assertion: lookahead < MIN_LOOKAHEAD
728  * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
729  *    At least one byte has been read, or avail_in == 0; reads are
730  *    performed for at least two bytes (required for the zip translate_eol
731  *    option -- not supported here).
732  */
733 static void fill_window(
734 	deflate_state *s
735 )
736 {
737     register unsigned n, m;
738     register Pos *p;
739     unsigned more;    /* Amount of free space at the end of the window. */
740     uInt wsize = s->w_size;
741 
742     do {
743         more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart);
744 
745         /* Deal with !@#$% 64K limit: */
746         if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
747             more = wsize;
748 
749         } else if (more == (unsigned)(-1)) {
750             /* Very unlikely, but possible on 16 bit machine if strstart == 0
751              * and lookahead == 1 (input done one byte at time)
752              */
753             more--;
754 
755         /* If the window is almost full and there is insufficient lookahead,
756          * move the upper half to the lower one to make room in the upper half.
757          */
758         } else if (s->strstart >= wsize+MAX_DIST(s)) {
759 
760             memcpy((char *)s->window, (char *)s->window+wsize,
761                    (unsigned)wsize);
762             s->match_start -= wsize;
763             s->strstart    -= wsize; /* we now have strstart >= MAX_DIST */
764             s->block_start -= (long) wsize;
765 
766             /* Slide the hash table (could be avoided with 32 bit values
767                at the expense of memory usage). We slide even when level == 0
768                to keep the hash table consistent if we switch back to level > 0
769                later. (Using level 0 permanently is not an optimal usage of
770                zlib, so we don't care about this pathological case.)
771              */
772             n = s->hash_size;
773             p = &s->head[n];
774             do {
775                 m = *--p;
776                 *p = (Pos)(m >= wsize ? m-wsize : NIL);
777             } while (--n);
778 
779             n = wsize;
780             p = &s->prev[n];
781             do {
782                 m = *--p;
783                 *p = (Pos)(m >= wsize ? m-wsize : NIL);
784                 /* If n is not on any hash chain, prev[n] is garbage but
785                  * its value will never be used.
786                  */
787             } while (--n);
788             more += wsize;
789         }
790         if (s->strm->avail_in == 0) return;
791 
792         /* If there was no sliding:
793          *    strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
794          *    more == window_size - lookahead - strstart
795          * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
796          * => more >= window_size - 2*WSIZE + 2
797          * In the BIG_MEM or MMAP case (not yet supported),
798          *   window_size == input_size + MIN_LOOKAHEAD  &&
799          *   strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
800          * Otherwise, window_size == 2*WSIZE so more >= 2.
801          * If there was sliding, more >= WSIZE. So in all cases, more >= 2.
802          */
803         Assert(more >= 2, "more < 2");
804 
805         n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more);
806         s->lookahead += n;
807 
808         /* Initialize the hash value now that we have some input: */
809         if (s->lookahead >= MIN_MATCH) {
810             s->ins_h = s->window[s->strstart];
811             UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
812 #if MIN_MATCH != 3
813             Call UPDATE_HASH() MIN_MATCH-3 more times
814 #endif
815         }
816         /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
817          * but this is not important since only literal bytes will be emitted.
818          */
819 
820     } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0);
821 }
822 
823 /* ===========================================================================
824  * Flush the current block, with given end-of-file flag.
825  * IN assertion: strstart is set to the end of the current match.
826  */
827 #define FLUSH_BLOCK_ONLY(s, eof) { \
828    zlib_tr_flush_block(s, (s->block_start >= 0L ? \
829                    (char *)&s->window[(unsigned)s->block_start] : \
830                    NULL), \
831 		(ulg)((long)s->strstart - s->block_start), \
832 		(eof)); \
833    s->block_start = s->strstart; \
834    flush_pending(s->strm); \
835    Tracev((stderr,"[FLUSH]")); \
836 }
837 
838 /* Same but force premature exit if necessary. */
839 #define FLUSH_BLOCK(s, eof) { \
840    FLUSH_BLOCK_ONLY(s, eof); \
841    if (s->strm->avail_out == 0) return (eof) ? finish_started : need_more; \
842 }
843 
844 /* ===========================================================================
845  * Copy without compression as much as possible from the input stream, return
846  * the current block state.
847  * This function does not insert new strings in the dictionary since
848  * uncompressible data is probably not useful. This function is used
849  * only for the level=0 compression option.
850  * NOTE: this function should be optimized to avoid extra copying from
851  * window to pending_buf.
852  */
853 static block_state deflate_stored(
854 	deflate_state *s,
855 	int flush
856 )
857 {
858     /* Stored blocks are limited to 0xffff bytes, pending_buf is limited
859      * to pending_buf_size, and each stored block has a 5 byte header:
860      */
861     ulg max_block_size = 0xffff;
862     ulg max_start;
863 
864     if (max_block_size > s->pending_buf_size - 5) {
865         max_block_size = s->pending_buf_size - 5;
866     }
867 
868     /* Copy as much as possible from input to output: */
869     for (;;) {
870         /* Fill the window as much as possible: */
871         if (s->lookahead <= 1) {
872 
873             Assert(s->strstart < s->w_size+MAX_DIST(s) ||
874 		   s->block_start >= (long)s->w_size, "slide too late");
875 
876             fill_window(s);
877             if (s->lookahead == 0 && flush == Z_NO_FLUSH) return need_more;
878 
879             if (s->lookahead == 0) break; /* flush the current block */
880         }
881 	Assert(s->block_start >= 0L, "block gone");
882 
883 	s->strstart += s->lookahead;
884 	s->lookahead = 0;
885 
886 	/* Emit a stored block if pending_buf will be full: */
887  	max_start = s->block_start + max_block_size;
888         if (s->strstart == 0 || (ulg)s->strstart >= max_start) {
889 	    /* strstart == 0 is possible when wraparound on 16-bit machine */
890 	    s->lookahead = (uInt)(s->strstart - max_start);
891 	    s->strstart = (uInt)max_start;
892             FLUSH_BLOCK(s, 0);
893 	}
894 	/* Flush if we may have to slide, otherwise block_start may become
895          * negative and the data will be gone:
896          */
897         if (s->strstart - (uInt)s->block_start >= MAX_DIST(s)) {
898             FLUSH_BLOCK(s, 0);
899 	}
900     }
901     FLUSH_BLOCK(s, flush == Z_FINISH);
902     return flush == Z_FINISH ? finish_done : block_done;
903 }
904 
905 /* ===========================================================================
906  * Compress as much as possible from the input stream, return the current
907  * block state.
908  * This function does not perform lazy evaluation of matches and inserts
909  * new strings in the dictionary only for unmatched strings or for short
910  * matches. It is used only for the fast compression options.
911  */
912 static block_state deflate_fast(
913 	deflate_state *s,
914 	int flush
915 )
916 {
917     IPos hash_head = NIL; /* head of the hash chain */
918     int bflush;           /* set if current block must be flushed */
919 
920     for (;;) {
921         /* Make sure that we always have enough lookahead, except
922          * at the end of the input file. We need MAX_MATCH bytes
923          * for the next match, plus MIN_MATCH bytes to insert the
924          * string following the next match.
925          */
926         if (s->lookahead < MIN_LOOKAHEAD) {
927             fill_window(s);
928             if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
929 	        return need_more;
930 	    }
931             if (s->lookahead == 0) break; /* flush the current block */
932         }
933 
934         /* Insert the string window[strstart .. strstart+2] in the
935          * dictionary, and set hash_head to the head of the hash chain:
936          */
937         if (s->lookahead >= MIN_MATCH) {
938             INSERT_STRING(s, s->strstart, hash_head);
939         }
940 
941         /* Find the longest match, discarding those <= prev_length.
942          * At this point we have always match_length < MIN_MATCH
943          */
944         if (hash_head != NIL && s->strstart - hash_head <= MAX_DIST(s)) {
945             /* To simplify the code, we prevent matches with the string
946              * of window index 0 (in particular we have to avoid a match
947              * of the string with itself at the start of the input file).
948              */
949             if (s->strategy != Z_HUFFMAN_ONLY) {
950                 s->match_length = longest_match (s, hash_head);
951             }
952             /* longest_match() sets match_start */
953         }
954         if (s->match_length >= MIN_MATCH) {
955             check_match(s, s->strstart, s->match_start, s->match_length);
956 
957             bflush = zlib_tr_tally(s, s->strstart - s->match_start,
958                                s->match_length - MIN_MATCH);
959 
960             s->lookahead -= s->match_length;
961 
962             /* Insert new strings in the hash table only if the match length
963              * is not too large. This saves time but degrades compression.
964              */
965             if (s->match_length <= s->max_insert_length &&
966                 s->lookahead >= MIN_MATCH) {
967                 s->match_length--; /* string at strstart already in hash table */
968                 do {
969                     s->strstart++;
970                     INSERT_STRING(s, s->strstart, hash_head);
971                     /* strstart never exceeds WSIZE-MAX_MATCH, so there are
972                      * always MIN_MATCH bytes ahead.
973                      */
974                 } while (--s->match_length != 0);
975                 s->strstart++;
976             } else {
977                 s->strstart += s->match_length;
978                 s->match_length = 0;
979                 s->ins_h = s->window[s->strstart];
980                 UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
981 #if MIN_MATCH != 3
982                 Call UPDATE_HASH() MIN_MATCH-3 more times
983 #endif
984                 /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not
985                  * matter since it will be recomputed at next deflate call.
986                  */
987             }
988         } else {
989             /* No match, output a literal byte */
990             Tracevv((stderr,"%c", s->window[s->strstart]));
991             bflush = zlib_tr_tally (s, 0, s->window[s->strstart]);
992             s->lookahead--;
993             s->strstart++;
994         }
995         if (bflush) FLUSH_BLOCK(s, 0);
996     }
997     FLUSH_BLOCK(s, flush == Z_FINISH);
998     return flush == Z_FINISH ? finish_done : block_done;
999 }
1000 
1001 /* ===========================================================================
1002  * Same as above, but achieves better compression. We use a lazy
1003  * evaluation for matches: a match is finally adopted only if there is
1004  * no better match at the next window position.
1005  */
1006 static block_state deflate_slow(
1007 	deflate_state *s,
1008 	int flush
1009 )
1010 {
1011     IPos hash_head = NIL;    /* head of hash chain */
1012     int bflush;              /* set if current block must be flushed */
1013 
1014     /* Process the input block. */
1015     for (;;) {
1016         /* Make sure that we always have enough lookahead, except
1017          * at the end of the input file. We need MAX_MATCH bytes
1018          * for the next match, plus MIN_MATCH bytes to insert the
1019          * string following the next match.
1020          */
1021         if (s->lookahead < MIN_LOOKAHEAD) {
1022             fill_window(s);
1023             if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
1024 	        return need_more;
1025 	    }
1026             if (s->lookahead == 0) break; /* flush the current block */
1027         }
1028 
1029         /* Insert the string window[strstart .. strstart+2] in the
1030          * dictionary, and set hash_head to the head of the hash chain:
1031          */
1032         if (s->lookahead >= MIN_MATCH) {
1033             INSERT_STRING(s, s->strstart, hash_head);
1034         }
1035 
1036         /* Find the longest match, discarding those <= prev_length.
1037          */
1038         s->prev_length = s->match_length, s->prev_match = s->match_start;
1039         s->match_length = MIN_MATCH-1;
1040 
1041         if (hash_head != NIL && s->prev_length < s->max_lazy_match &&
1042             s->strstart - hash_head <= MAX_DIST(s)) {
1043             /* To simplify the code, we prevent matches with the string
1044              * of window index 0 (in particular we have to avoid a match
1045              * of the string with itself at the start of the input file).
1046              */
1047             if (s->strategy != Z_HUFFMAN_ONLY) {
1048                 s->match_length = longest_match (s, hash_head);
1049             }
1050             /* longest_match() sets match_start */
1051 
1052             if (s->match_length <= 5 && (s->strategy == Z_FILTERED ||
1053                  (s->match_length == MIN_MATCH &&
1054                   s->strstart - s->match_start > TOO_FAR))) {
1055 
1056                 /* If prev_match is also MIN_MATCH, match_start is garbage
1057                  * but we will ignore the current match anyway.
1058                  */
1059                 s->match_length = MIN_MATCH-1;
1060             }
1061         }
1062         /* If there was a match at the previous step and the current
1063          * match is not better, output the previous match:
1064          */
1065         if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) {
1066             uInt max_insert = s->strstart + s->lookahead - MIN_MATCH;
1067             /* Do not insert strings in hash table beyond this. */
1068 
1069             check_match(s, s->strstart-1, s->prev_match, s->prev_length);
1070 
1071             bflush = zlib_tr_tally(s, s->strstart -1 - s->prev_match,
1072 				   s->prev_length - MIN_MATCH);
1073 
1074             /* Insert in hash table all strings up to the end of the match.
1075              * strstart-1 and strstart are already inserted. If there is not
1076              * enough lookahead, the last two strings are not inserted in
1077              * the hash table.
1078              */
1079             s->lookahead -= s->prev_length-1;
1080             s->prev_length -= 2;
1081             do {
1082                 if (++s->strstart <= max_insert) {
1083                     INSERT_STRING(s, s->strstart, hash_head);
1084                 }
1085             } while (--s->prev_length != 0);
1086             s->match_available = 0;
1087             s->match_length = MIN_MATCH-1;
1088             s->strstart++;
1089 
1090             if (bflush) FLUSH_BLOCK(s, 0);
1091 
1092         } else if (s->match_available) {
1093             /* If there was no match at the previous position, output a
1094              * single literal. If there was a match but the current match
1095              * is longer, truncate the previous match to a single literal.
1096              */
1097             Tracevv((stderr,"%c", s->window[s->strstart-1]));
1098             if (zlib_tr_tally (s, 0, s->window[s->strstart-1])) {
1099                 FLUSH_BLOCK_ONLY(s, 0);
1100             }
1101             s->strstart++;
1102             s->lookahead--;
1103             if (s->strm->avail_out == 0) return need_more;
1104         } else {
1105             /* There is no previous match to compare with, wait for
1106              * the next step to decide.
1107              */
1108             s->match_available = 1;
1109             s->strstart++;
1110             s->lookahead--;
1111         }
1112     }
1113     Assert (flush != Z_NO_FLUSH, "no flush?");
1114     if (s->match_available) {
1115         Tracevv((stderr,"%c", s->window[s->strstart-1]));
1116         zlib_tr_tally (s, 0, s->window[s->strstart-1]);
1117         s->match_available = 0;
1118     }
1119     FLUSH_BLOCK(s, flush == Z_FINISH);
1120     return flush == Z_FINISH ? finish_done : block_done;
1121 }
1122 
1123 int zlib_deflate_workspacesize(int windowBits, int memLevel)
1124 {
1125     if (windowBits < 0) /* undocumented feature: suppress zlib header */
1126         windowBits = -windowBits;
1127 
1128     /* Since the return value is typically passed to vmalloc() unchecked... */
1129     BUG_ON(memLevel < 1 || memLevel > MAX_MEM_LEVEL || windowBits < 9 ||
1130 							windowBits > 15);
1131 
1132     return sizeof(deflate_workspace)
1133         + zlib_deflate_window_memsize(windowBits)
1134         + zlib_deflate_prev_memsize(windowBits)
1135         + zlib_deflate_head_memsize(memLevel)
1136         + zlib_deflate_overlay_memsize(memLevel);
1137 }
1138