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