/[LeafOK_CVS]/pvpgn-1.7.4/src/zlib/pvpgn_deflate.c
ViewVC logotype

Annotation of /pvpgn-1.7.4/src/zlib/pvpgn_deflate.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (hide annotations)
Tue Jun 6 03:41:38 2006 UTC (19 years, 9 months ago) by sysadm
CVS Tags: pvpgn_1-7-4-0_MIL
Branch point for: GNU, MAIN
Content type: text/x-csrc
Initial revision

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

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1