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