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