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