wearefair-grpc 1.3.1.pre.a → 1.3.1.pre.c

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 750f2731da4bf9b38882eed5d41469c4745f5e6e
4
- data.tar.gz: 7dadbb16715a38b4e4826612288b2d4d5a7f8218
3
+ metadata.gz: 5df81987fb128110c0500bdc85e9e819e91b7ca6
4
+ data.tar.gz: 7eaff10dd76a6b724b206a3f33767e0272a372b4
5
5
  SHA512:
6
- metadata.gz: c4ec6d5bb2c4f977028fd4f7c78c8346bd25490ff26648563c75500a0037a208f07001d53ed596ca7f6f932afdc0a8b545defcaeb33f82cc8490788e3b58f219
7
- data.tar.gz: ffaed82f5eccc53290377f7382c6e71d28a113490e4ea1f504297f0926e715ab50626a12870d009e23f6ba912a1fea8a19c0c7c4e3a94e4726eebfec7cbbbec4
6
+ metadata.gz: 6bb64d67ccd868f1f43464d42effa4134b4d1bdde0f6a4534de3073fabcb8a9c70562756a6e1dd179c8748b61b4b9730e0a417317e6f69349579d1436cc94ed8
7
+ data.tar.gz: c8b17e4f88612c6cbec52215ed8b4b88f08e7bebea1a72155aac4aafa75911ce01893984031f02c412a3b7e4acb341fe14dc5209c06ccd8f51fe08414c966e04
Binary file
Binary file
@@ -1,5 +1,5 @@
1
1
  /* adler32.c -- compute the Adler-32 checksum of a data stream
2
- * Copyright (C) 1995-2011 Mark Adler
2
+ * Copyright (C) 1995-2011, 2016 Mark Adler
3
3
  * For conditions of distribution and use, see copyright notice in zlib.h
4
4
  */
5
5
 
@@ -7,11 +7,9 @@
7
7
 
8
8
  #include "zutil.h"
9
9
 
10
- #define local static
11
-
12
10
  local uLong adler32_combine_ OF((uLong adler1, uLong adler2, z_off64_t len2));
13
11
 
14
- #define BASE 65521 /* largest prime smaller than 65536 */
12
+ #define BASE 65521U /* largest prime smaller than 65536 */
15
13
  #define NMAX 5552
16
14
  /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
17
15
 
@@ -62,10 +60,10 @@ local uLong adler32_combine_ OF((uLong adler1, uLong adler2, z_off64_t len2));
62
60
  #endif
63
61
 
64
62
  /* ========================================================================= */
65
- uLong ZEXPORT adler32(adler, buf, len)
63
+ uLong ZEXPORT adler32_z(adler, buf, len)
66
64
  uLong adler;
67
65
  const Bytef *buf;
68
- uInt len;
66
+ z_size_t len;
69
67
  {
70
68
  unsigned long sum2;
71
69
  unsigned n;
@@ -132,6 +130,15 @@ uLong ZEXPORT adler32(adler, buf, len)
132
130
  return adler | (sum2 << 16);
133
131
  }
134
132
 
133
+ /* ========================================================================= */
134
+ uLong ZEXPORT adler32(adler, buf, len)
135
+ uLong adler;
136
+ const Bytef *buf;
137
+ uInt len;
138
+ {
139
+ return adler32_z(adler, buf, len);
140
+ }
141
+
135
142
  /* ========================================================================= */
136
143
  local uLong adler32_combine_(adler1, adler2, len2)
137
144
  uLong adler1;
@@ -156,7 +163,7 @@ local uLong adler32_combine_(adler1, adler2, len2)
156
163
  sum2 += ((adler1 >> 16) & 0xffff) + ((adler2 >> 16) & 0xffff) + BASE - rem;
157
164
  if (sum1 >= BASE) sum1 -= BASE;
158
165
  if (sum1 >= BASE) sum1 -= BASE;
159
- if (sum2 >= (BASE << 1)) sum2 -= (BASE << 1);
166
+ if (sum2 >= ((unsigned long)BASE << 1)) sum2 -= ((unsigned long)BASE << 1);
160
167
  if (sum2 >= BASE) sum2 -= BASE;
161
168
  return sum1 | (sum2 << 16);
162
169
  }
@@ -1,5 +1,5 @@
1
1
  /* compress.c -- compress a memory buffer
2
- * Copyright (C) 1995-2005 Jean-loup Gailly.
2
+ * Copyright (C) 1995-2005, 2014, 2016 Jean-loup Gailly, Mark Adler
3
3
  * For conditions of distribution and use, see copyright notice in zlib.h
4
4
  */
5
5
 
@@ -28,16 +28,11 @@ int ZEXPORT compress2 (dest, destLen, source, sourceLen, level)
28
28
  {
29
29
  z_stream stream;
30
30
  int err;
31
+ const uInt max = (uInt)-1;
32
+ uLong left;
31
33
 
32
- stream.next_in = (z_const Bytef *)source;
33
- stream.avail_in = (uInt)sourceLen;
34
- #ifdef MAXSEG_64K
35
- /* Check for source > 64K on 16-bit machine: */
36
- if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
37
- #endif
38
- stream.next_out = dest;
39
- stream.avail_out = (uInt)*destLen;
40
- if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
34
+ left = *destLen;
35
+ *destLen = 0;
41
36
 
42
37
  stream.zalloc = (alloc_func)0;
43
38
  stream.zfree = (free_func)0;
@@ -46,15 +41,26 @@ int ZEXPORT compress2 (dest, destLen, source, sourceLen, level)
46
41
  err = deflateInit(&stream, level);
47
42
  if (err != Z_OK) return err;
48
43
 
49
- err = deflate(&stream, Z_FINISH);
50
- if (err != Z_STREAM_END) {
51
- deflateEnd(&stream);
52
- return err == Z_OK ? Z_BUF_ERROR : err;
53
- }
54
- *destLen = stream.total_out;
44
+ stream.next_out = dest;
45
+ stream.avail_out = 0;
46
+ stream.next_in = (z_const Bytef *)source;
47
+ stream.avail_in = 0;
48
+
49
+ do {
50
+ if (stream.avail_out == 0) {
51
+ stream.avail_out = left > (uLong)max ? max : (uInt)left;
52
+ left -= stream.avail_out;
53
+ }
54
+ if (stream.avail_in == 0) {
55
+ stream.avail_in = sourceLen > (uLong)max ? max : (uInt)sourceLen;
56
+ sourceLen -= stream.avail_in;
57
+ }
58
+ err = deflate(&stream, sourceLen ? Z_NO_FLUSH : Z_FINISH);
59
+ } while (err == Z_OK);
55
60
 
56
- err = deflateEnd(&stream);
57
- return err;
61
+ *destLen = stream.total_out;
62
+ deflateEnd(&stream);
63
+ return err == Z_STREAM_END ? Z_OK : err;
58
64
  }
59
65
 
60
66
  /* ===========================================================================
@@ -1,5 +1,5 @@
1
1
  /* crc32.c -- compute the CRC-32 of a data stream
2
- * Copyright (C) 1995-2006, 2010, 2011, 2012 Mark Adler
2
+ * Copyright (C) 1995-2006, 2010, 2011, 2012, 2016 Mark Adler
3
3
  * For conditions of distribution and use, see copyright notice in zlib.h
4
4
  *
5
5
  * Thanks to Rodney Brown <rbrown64@csc.com.au> for his contribution of faster
@@ -30,17 +30,15 @@
30
30
 
31
31
  #include "zutil.h" /* for STDC and FAR definitions */
32
32
 
33
- #define local static
34
-
35
33
  /* Definitions for doing the crc four data bytes at a time. */
36
34
  #if !defined(NOBYFOUR) && defined(Z_U4)
37
35
  # define BYFOUR
38
36
  #endif
39
37
  #ifdef BYFOUR
40
38
  local unsigned long crc32_little OF((unsigned long,
41
- const unsigned char FAR *, unsigned));
39
+ const unsigned char FAR *, z_size_t));
42
40
  local unsigned long crc32_big OF((unsigned long,
43
- const unsigned char FAR *, unsigned));
41
+ const unsigned char FAR *, z_size_t));
44
42
  # define TBLS 8
45
43
  #else
46
44
  # define TBLS 1
@@ -201,10 +199,10 @@ const z_crc_t FAR * ZEXPORT get_crc_table()
201
199
  #define DO8 DO1; DO1; DO1; DO1; DO1; DO1; DO1; DO1
202
200
 
203
201
  /* ========================================================================= */
204
- unsigned long ZEXPORT crc32(crc, buf, len)
202
+ unsigned long ZEXPORT crc32_z(crc, buf, len)
205
203
  unsigned long crc;
206
204
  const unsigned char FAR *buf;
207
- uInt len;
205
+ z_size_t len;
208
206
  {
209
207
  if (buf == Z_NULL) return 0UL;
210
208
 
@@ -235,8 +233,29 @@ unsigned long ZEXPORT crc32(crc, buf, len)
235
233
  return crc ^ 0xffffffffUL;
236
234
  }
237
235
 
236
+ /* ========================================================================= */
237
+ unsigned long ZEXPORT crc32(crc, buf, len)
238
+ unsigned long crc;
239
+ const unsigned char FAR *buf;
240
+ uInt len;
241
+ {
242
+ return crc32_z(crc, buf, len);
243
+ }
244
+
238
245
  #ifdef BYFOUR
239
246
 
247
+ /*
248
+ This BYFOUR code accesses the passed unsigned char * buffer with a 32-bit
249
+ integer pointer type. This violates the strict aliasing rule, where a
250
+ compiler can assume, for optimization purposes, that two pointers to
251
+ fundamentally different types won't ever point to the same memory. This can
252
+ manifest as a problem only if one of the pointers is written to. This code
253
+ only reads from those pointers. So long as this code remains isolated in
254
+ this compilation unit, there won't be a problem. For this reason, this code
255
+ should not be copied and pasted into a compilation unit in which other code
256
+ writes to the buffer that is passed to these routines.
257
+ */
258
+
240
259
  /* ========================================================================= */
241
260
  #define DOLIT4 c ^= *buf4++; \
242
261
  c = crc_table[3][c & 0xff] ^ crc_table[2][(c >> 8) & 0xff] ^ \
@@ -247,7 +266,7 @@ unsigned long ZEXPORT crc32(crc, buf, len)
247
266
  local unsigned long crc32_little(crc, buf, len)
248
267
  unsigned long crc;
249
268
  const unsigned char FAR *buf;
250
- unsigned len;
269
+ z_size_t len;
251
270
  {
252
271
  register z_crc_t c;
253
272
  register const z_crc_t FAR *buf4;
@@ -278,7 +297,7 @@ local unsigned long crc32_little(crc, buf, len)
278
297
  }
279
298
 
280
299
  /* ========================================================================= */
281
- #define DOBIG4 c ^= *++buf4; \
300
+ #define DOBIG4 c ^= *buf4++; \
282
301
  c = crc_table[4][c & 0xff] ^ crc_table[5][(c >> 8) & 0xff] ^ \
283
302
  crc_table[6][(c >> 16) & 0xff] ^ crc_table[7][c >> 24]
284
303
  #define DOBIG32 DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4
@@ -287,7 +306,7 @@ local unsigned long crc32_little(crc, buf, len)
287
306
  local unsigned long crc32_big(crc, buf, len)
288
307
  unsigned long crc;
289
308
  const unsigned char FAR *buf;
290
- unsigned len;
309
+ z_size_t len;
291
310
  {
292
311
  register z_crc_t c;
293
312
  register const z_crc_t FAR *buf4;
@@ -300,7 +319,6 @@ local unsigned long crc32_big(crc, buf, len)
300
319
  }
301
320
 
302
321
  buf4 = (const z_crc_t FAR *)(const void FAR *)buf;
303
- buf4--;
304
322
  while (len >= 32) {
305
323
  DOBIG32;
306
324
  len -= 32;
@@ -309,7 +327,6 @@ local unsigned long crc32_big(crc, buf, len)
309
327
  DOBIG4;
310
328
  len -= 4;
311
329
  }
312
- buf4++;
313
330
  buf = (const unsigned char FAR *)buf4;
314
331
 
315
332
  if (len) do {
@@ -1,5 +1,5 @@
1
1
  /* deflate.c -- compress data using the deflation algorithm
2
- * Copyright (C) 1995-2013 Jean-loup Gailly and Mark Adler
2
+ * Copyright (C) 1995-2017 Jean-loup Gailly and Mark Adler
3
3
  * For conditions of distribution and use, see copyright notice in zlib.h
4
4
  */
5
5
 
@@ -52,7 +52,7 @@
52
52
  #include "deflate.h"
53
53
 
54
54
  const char deflate_copyright[] =
55
- " deflate 1.2.8 Copyright 1995-2013 Jean-loup Gailly and Mark Adler ";
55
+ " deflate 1.2.11 Copyright 1995-2017 Jean-loup Gailly and Mark Adler ";
56
56
  /*
57
57
  If you use the zlib library in a product, an acknowledgment is welcome
58
58
  in the documentation of your product. If for some reason you cannot
@@ -73,6 +73,8 @@ typedef enum {
73
73
  typedef block_state (*compress_func) OF((deflate_state *s, int flush));
74
74
  /* Compression function. Returns the block state after the call. */
75
75
 
76
+ local int deflateStateCheck OF((z_streamp strm));
77
+ local void slide_hash OF((deflate_state *s));
76
78
  local void fill_window OF((deflate_state *s));
77
79
  local block_state deflate_stored OF((deflate_state *s, int flush));
78
80
  local block_state deflate_fast OF((deflate_state *s, int flush));
@@ -84,15 +86,16 @@ local block_state deflate_huff OF((deflate_state *s, int flush));
84
86
  local void lm_init OF((deflate_state *s));
85
87
  local void putShortMSB OF((deflate_state *s, uInt b));
86
88
  local void flush_pending OF((z_streamp strm));
87
- local int read_buf OF((z_streamp strm, Bytef *buf, unsigned size));
89
+ local unsigned read_buf OF((z_streamp strm, Bytef *buf, unsigned size));
88
90
  #ifdef ASMV
91
+ # pragma message("Assembler code may have bugs -- use at your own risk")
89
92
  void match_init OF((void)); /* asm code initialization */
90
93
  uInt longest_match OF((deflate_state *s, IPos cur_match));
91
94
  #else
92
95
  local uInt longest_match OF((deflate_state *s, IPos cur_match));
93
96
  #endif
94
97
 
95
- #ifdef DEBUG
98
+ #ifdef ZLIB_DEBUG
96
99
  local void check_match OF((deflate_state *s, IPos start, IPos match,
97
100
  int length));
98
101
  #endif
@@ -148,21 +151,14 @@ local const config configuration_table[10] = {
148
151
  * meaning.
149
152
  */
150
153
 
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
154
  /* rank Z_BLOCK between Z_NO_FLUSH and Z_PARTIAL_FLUSH */
159
- #define RANK(f) (((f) << 1) - ((f) > 4 ? 9 : 0))
155
+ #define RANK(f) (((f) * 2) - ((f) > 4 ? 9 : 0))
160
156
 
161
157
  /* ===========================================================================
162
158
  * 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.
159
+ * IN assertion: all calls to UPDATE_HASH are made with consecutive input
160
+ * characters, so that a running hash key can be computed from the previous
161
+ * key instead of complete recalculation each time.
166
162
  */
167
163
  #define UPDATE_HASH(s,h,c) (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask)
168
164
 
@@ -173,9 +169,9 @@ struct static_tree_desc_s {int dummy;}; /* for buggy compilers */
173
169
  * the previous length of the hash chain.
174
170
  * If this file is compiled with -DFASTEST, the compression level is forced
175
171
  * 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).
172
+ * IN assertion: all calls to INSERT_STRING are made with consecutive input
173
+ * characters and the first MIN_MATCH bytes of str are valid (except for
174
+ * the last MIN_MATCH-1 bytes of the input file).
179
175
  */
180
176
  #ifdef FASTEST
181
177
  #define INSERT_STRING(s, str, match_head) \
@@ -197,6 +193,37 @@ struct static_tree_desc_s {int dummy;}; /* for buggy compilers */
197
193
  s->head[s->hash_size-1] = NIL; \
198
194
  zmemzero((Bytef *)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head));
199
195
 
196
+ /* ===========================================================================
197
+ * Slide the hash table when sliding the window down (could be avoided with 32
198
+ * bit values at the expense of memory usage). We slide even when level == 0 to
199
+ * keep the hash table consistent if we switch back to level > 0 later.
200
+ */
201
+ local void slide_hash(s)
202
+ deflate_state *s;
203
+ {
204
+ unsigned n, m;
205
+ Posf *p;
206
+ uInt wsize = s->w_size;
207
+
208
+ n = s->hash_size;
209
+ p = &s->head[n];
210
+ do {
211
+ m = *--p;
212
+ *p = (Pos)(m >= wsize ? m - wsize : NIL);
213
+ } while (--n);
214
+ n = wsize;
215
+ #ifndef FASTEST
216
+ p = &s->prev[n];
217
+ do {
218
+ m = *--p;
219
+ *p = (Pos)(m >= wsize ? m - wsize : NIL);
220
+ /* If n is not on any hash chain, prev[n] is garbage but
221
+ * its value will never be used.
222
+ */
223
+ } while (--n);
224
+ #endif
225
+ }
226
+
200
227
  /* ========================================================================= */
201
228
  int ZEXPORT deflateInit_(strm, level, version, stream_size)
202
229
  z_streamp strm;
@@ -270,7 +297,7 @@ int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy,
270
297
  #endif
271
298
  if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED ||
272
299
  windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
273
- strategy < 0 || strategy > Z_FIXED) {
300
+ strategy < 0 || strategy > Z_FIXED || (windowBits == 8 && wrap != 1)) {
274
301
  return Z_STREAM_ERROR;
275
302
  }
276
303
  if (windowBits == 8) windowBits = 9; /* until 256-byte window bug fixed */
@@ -278,14 +305,15 @@ int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy,
278
305
  if (s == Z_NULL) return Z_MEM_ERROR;
279
306
  strm->state = (struct internal_state FAR *)s;
280
307
  s->strm = strm;
308
+ s->status = INIT_STATE; /* to pass state test in deflateReset() */
281
309
 
282
310
  s->wrap = wrap;
283
311
  s->gzhead = Z_NULL;
284
- s->w_bits = windowBits;
312
+ s->w_bits = (uInt)windowBits;
285
313
  s->w_size = 1 << s->w_bits;
286
314
  s->w_mask = s->w_size - 1;
287
315
 
288
- s->hash_bits = memLevel + 7;
316
+ s->hash_bits = (uInt)memLevel + 7;
289
317
  s->hash_size = 1 << s->hash_bits;
290
318
  s->hash_mask = s->hash_size - 1;
291
319
  s->hash_shift = ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH);
@@ -319,6 +347,31 @@ int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy,
319
347
  return deflateReset(strm);
320
348
  }
321
349
 
350
+ /* =========================================================================
351
+ * Check for a valid deflate stream state. Return 0 if ok, 1 if not.
352
+ */
353
+ local int deflateStateCheck (strm)
354
+ z_streamp strm;
355
+ {
356
+ deflate_state *s;
357
+ if (strm == Z_NULL ||
358
+ strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0)
359
+ return 1;
360
+ s = strm->state;
361
+ if (s == Z_NULL || s->strm != strm || (s->status != INIT_STATE &&
362
+ #ifdef GZIP
363
+ s->status != GZIP_STATE &&
364
+ #endif
365
+ s->status != EXTRA_STATE &&
366
+ s->status != NAME_STATE &&
367
+ s->status != COMMENT_STATE &&
368
+ s->status != HCRC_STATE &&
369
+ s->status != BUSY_STATE &&
370
+ s->status != FINISH_STATE))
371
+ return 1;
372
+ return 0;
373
+ }
374
+
322
375
  /* ========================================================================= */
323
376
  int ZEXPORT deflateSetDictionary (strm, dictionary, dictLength)
324
377
  z_streamp strm;
@@ -331,7 +384,7 @@ int ZEXPORT deflateSetDictionary (strm, dictionary, dictLength)
331
384
  unsigned avail;
332
385
  z_const unsigned char *next;
333
386
 
334
- if (strm == Z_NULL || strm->state == Z_NULL || dictionary == Z_NULL)
387
+ if (deflateStateCheck(strm) || dictionary == Z_NULL)
335
388
  return Z_STREAM_ERROR;
336
389
  s = strm->state;
337
390
  wrap = s->wrap;
@@ -388,14 +441,35 @@ int ZEXPORT deflateSetDictionary (strm, dictionary, dictLength)
388
441
  return Z_OK;
389
442
  }
390
443
 
444
+ /* ========================================================================= */
445
+ int ZEXPORT deflateGetDictionary (strm, dictionary, dictLength)
446
+ z_streamp strm;
447
+ Bytef *dictionary;
448
+ uInt *dictLength;
449
+ {
450
+ deflate_state *s;
451
+ uInt len;
452
+
453
+ if (deflateStateCheck(strm))
454
+ return Z_STREAM_ERROR;
455
+ s = strm->state;
456
+ len = s->strstart + s->lookahead;
457
+ if (len > s->w_size)
458
+ len = s->w_size;
459
+ if (dictionary != Z_NULL && len)
460
+ zmemcpy(dictionary, s->window + s->strstart + s->lookahead - len, len);
461
+ if (dictLength != Z_NULL)
462
+ *dictLength = len;
463
+ return Z_OK;
464
+ }
465
+
391
466
  /* ========================================================================= */
392
467
  int ZEXPORT deflateResetKeep (strm)
393
468
  z_streamp strm;
394
469
  {
395
470
  deflate_state *s;
396
471
 
397
- if (strm == Z_NULL || strm->state == Z_NULL ||
398
- strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0) {
472
+ if (deflateStateCheck(strm)) {
399
473
  return Z_STREAM_ERROR;
400
474
  }
401
475
 
@@ -410,7 +484,11 @@ int ZEXPORT deflateResetKeep (strm)
410
484
  if (s->wrap < 0) {
411
485
  s->wrap = -s->wrap; /* was made negative by deflate(..., Z_FINISH); */
412
486
  }
413
- s->status = s->wrap ? INIT_STATE : BUSY_STATE;
487
+ s->status =
488
+ #ifdef GZIP
489
+ s->wrap == 2 ? GZIP_STATE :
490
+ #endif
491
+ s->wrap ? INIT_STATE : BUSY_STATE;
414
492
  strm->adler =
415
493
  #ifdef GZIP
416
494
  s->wrap == 2 ? crc32(0L, Z_NULL, 0) :
@@ -440,8 +518,8 @@ int ZEXPORT deflateSetHeader (strm, head)
440
518
  z_streamp strm;
441
519
  gz_headerp head;
442
520
  {
443
- if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
444
- if (strm->state->wrap != 2) return Z_STREAM_ERROR;
521
+ if (deflateStateCheck(strm) || strm->state->wrap != 2)
522
+ return Z_STREAM_ERROR;
445
523
  strm->state->gzhead = head;
446
524
  return Z_OK;
447
525
  }
@@ -452,7 +530,7 @@ int ZEXPORT deflatePending (strm, pending, bits)
452
530
  int *bits;
453
531
  z_streamp strm;
454
532
  {
455
- if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
533
+ if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
456
534
  if (pending != Z_NULL)
457
535
  *pending = strm->state->pending;
458
536
  if (bits != Z_NULL)
@@ -469,7 +547,7 @@ int ZEXPORT deflatePrime (strm, bits, value)
469
547
  deflate_state *s;
470
548
  int put;
471
549
 
472
- if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
550
+ if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
473
551
  s = strm->state;
474
552
  if ((Bytef *)(s->d_buf) < s->pending_out + ((Buf_size + 7) >> 3))
475
553
  return Z_BUF_ERROR;
@@ -494,9 +572,8 @@ int ZEXPORT deflateParams(strm, level, strategy)
494
572
  {
495
573
  deflate_state *s;
496
574
  compress_func func;
497
- int err = Z_OK;
498
575
 
499
- if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
576
+ if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
500
577
  s = strm->state;
501
578
 
502
579
  #ifdef FASTEST
@@ -510,13 +587,22 @@ int ZEXPORT deflateParams(strm, level, strategy)
510
587
  func = configuration_table[s->level].func;
511
588
 
512
589
  if ((strategy != s->strategy || func != configuration_table[level].func) &&
513
- strm->total_in != 0) {
590
+ s->high_water) {
514
591
  /* Flush the last buffer: */
515
- err = deflate(strm, Z_BLOCK);
516
- if (err == Z_BUF_ERROR && s->pending == 0)
517
- err = Z_OK;
592
+ int err = deflate(strm, Z_BLOCK);
593
+ if (err == Z_STREAM_ERROR)
594
+ return err;
595
+ if (strm->avail_out == 0)
596
+ return Z_BUF_ERROR;
518
597
  }
519
598
  if (s->level != level) {
599
+ if (s->level == 0 && s->matches != 0) {
600
+ if (s->matches == 1)
601
+ slide_hash(s);
602
+ else
603
+ CLEAR_HASH(s);
604
+ s->matches = 0;
605
+ }
520
606
  s->level = level;
521
607
  s->max_lazy_match = configuration_table[level].max_lazy;
522
608
  s->good_match = configuration_table[level].good_length;
@@ -524,7 +610,7 @@ int ZEXPORT deflateParams(strm, level, strategy)
524
610
  s->max_chain_length = configuration_table[level].max_chain;
525
611
  }
526
612
  s->strategy = strategy;
527
- return err;
613
+ return Z_OK;
528
614
  }
529
615
 
530
616
  /* ========================================================================= */
@@ -537,12 +623,12 @@ int ZEXPORT deflateTune(strm, good_length, max_lazy, nice_length, max_chain)
537
623
  {
538
624
  deflate_state *s;
539
625
 
540
- if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
626
+ if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
541
627
  s = strm->state;
542
- s->good_match = good_length;
543
- s->max_lazy_match = max_lazy;
628
+ s->good_match = (uInt)good_length;
629
+ s->max_lazy_match = (uInt)max_lazy;
544
630
  s->nice_match = nice_length;
545
- s->max_chain_length = max_chain;
631
+ s->max_chain_length = (uInt)max_chain;
546
632
  return Z_OK;
547
633
  }
548
634
 
@@ -569,14 +655,13 @@ uLong ZEXPORT deflateBound(strm, sourceLen)
569
655
  {
570
656
  deflate_state *s;
571
657
  uLong complen, wraplen;
572
- Bytef *str;
573
658
 
574
659
  /* conservative upper bound for compressed data */
575
660
  complen = sourceLen +
576
661
  ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 5;
577
662
 
578
663
  /* if can't get parameters, return conservative bound plus zlib wrapper */
579
- if (strm == Z_NULL || strm->state == Z_NULL)
664
+ if (deflateStateCheck(strm))
580
665
  return complen + 6;
581
666
 
582
667
  /* compute wrapper length */
@@ -588,9 +673,11 @@ uLong ZEXPORT deflateBound(strm, sourceLen)
588
673
  case 1: /* zlib wrapper */
589
674
  wraplen = 6 + (s->strstart ? 4 : 0);
590
675
  break;
676
+ #ifdef GZIP
591
677
  case 2: /* gzip wrapper */
592
678
  wraplen = 18;
593
679
  if (s->gzhead != Z_NULL) { /* user-supplied gzip header */
680
+ Bytef *str;
594
681
  if (s->gzhead->extra != Z_NULL)
595
682
  wraplen += 2 + s->gzhead->extra_len;
596
683
  str = s->gzhead->name;
@@ -607,6 +694,7 @@ uLong ZEXPORT deflateBound(strm, sourceLen)
607
694
  wraplen += 2;
608
695
  }
609
696
  break;
697
+ #endif
610
698
  default: /* for compiler happiness */
611
699
  wraplen = 6;
612
700
  }
@@ -634,10 +722,10 @@ local void putShortMSB (s, b)
634
722
  }
635
723
 
636
724
  /* =========================================================================
637
- * Flush as much pending output as possible. All deflate() output goes
638
- * through this function so some applications may wish to modify it
639
- * to avoid allocating a large strm->next_out buffer and copying into it.
640
- * (See also read_buf()).
725
+ * Flush as much pending output as possible. All deflate() output, except for
726
+ * some deflate_stored() output, goes through this function so some
727
+ * applications may wish to modify it to avoid allocating a large
728
+ * strm->next_out buffer and copying into it. (See also read_buf()).
641
729
  */
642
730
  local void flush_pending(strm)
643
731
  z_streamp strm;
@@ -654,13 +742,23 @@ local void flush_pending(strm)
654
742
  strm->next_out += len;
655
743
  s->pending_out += len;
656
744
  strm->total_out += len;
657
- strm->avail_out -= len;
658
- s->pending -= len;
745
+ strm->avail_out -= len;
746
+ s->pending -= len;
659
747
  if (s->pending == 0) {
660
748
  s->pending_out = s->pending_buf;
661
749
  }
662
750
  }
663
751
 
752
+ /* ===========================================================================
753
+ * Update the header CRC with the bytes s->pending_buf[beg..s->pending - 1].
754
+ */
755
+ #define HCRC_UPDATE(beg) \
756
+ do { \
757
+ if (s->gzhead->hcrc && s->pending > (beg)) \
758
+ strm->adler = crc32(strm->adler, s->pending_buf + (beg), \
759
+ s->pending - (beg)); \
760
+ } while (0)
761
+
664
762
  /* ========================================================================= */
665
763
  int ZEXPORT deflate (strm, flush)
666
764
  z_streamp strm;
@@ -669,230 +767,229 @@ int ZEXPORT deflate (strm, flush)
669
767
  int old_flush; /* value of flush param for previous deflate call */
670
768
  deflate_state *s;
671
769
 
672
- if (strm == Z_NULL || strm->state == Z_NULL ||
673
- flush > Z_BLOCK || flush < 0) {
770
+ if (deflateStateCheck(strm) || flush > Z_BLOCK || flush < 0) {
674
771
  return Z_STREAM_ERROR;
675
772
  }
676
773
  s = strm->state;
677
774
 
678
775
  if (strm->next_out == Z_NULL ||
679
- (strm->next_in == Z_NULL && strm->avail_in != 0) ||
776
+ (strm->avail_in != 0 && strm->next_in == Z_NULL) ||
680
777
  (s->status == FINISH_STATE && flush != Z_FINISH)) {
681
778
  ERR_RETURN(strm, Z_STREAM_ERROR);
682
779
  }
683
780
  if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR);
684
781
 
685
- s->strm = strm; /* just in case */
686
782
  old_flush = s->last_flush;
687
783
  s->last_flush = flush;
688
784
 
785
+ /* Flush as much pending output as possible */
786
+ if (s->pending != 0) {
787
+ flush_pending(strm);
788
+ if (strm->avail_out == 0) {
789
+ /* Since avail_out is 0, deflate will be called again with
790
+ * more output space, but possibly with both pending and
791
+ * avail_in equal to zero. There won't be anything to do,
792
+ * but this is not an error situation so make sure we
793
+ * return OK instead of BUF_ERROR at next call of deflate:
794
+ */
795
+ s->last_flush = -1;
796
+ return Z_OK;
797
+ }
798
+
799
+ /* Make sure there is something to do and avoid duplicate consecutive
800
+ * flushes. For repeated and useless calls with Z_FINISH, we keep
801
+ * returning Z_STREAM_END instead of Z_BUF_ERROR.
802
+ */
803
+ } else if (strm->avail_in == 0 && RANK(flush) <= RANK(old_flush) &&
804
+ flush != Z_FINISH) {
805
+ ERR_RETURN(strm, Z_BUF_ERROR);
806
+ }
807
+
808
+ /* User must not provide more input after the first FINISH: */
809
+ if (s->status == FINISH_STATE && strm->avail_in != 0) {
810
+ ERR_RETURN(strm, Z_BUF_ERROR);
811
+ }
812
+
689
813
  /* Write the header */
690
814
  if (s->status == INIT_STATE) {
691
- #ifdef GZIP
692
- if (s->wrap == 2) {
693
- strm->adler = crc32(0L, Z_NULL, 0);
694
- put_byte(s, 31);
695
- put_byte(s, 139);
696
- put_byte(s, 8);
697
- if (s->gzhead == Z_NULL) {
698
- put_byte(s, 0);
699
- put_byte(s, 0);
700
- put_byte(s, 0);
701
- put_byte(s, 0);
702
- put_byte(s, 0);
703
- put_byte(s, s->level == 9 ? 2 :
704
- (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
705
- 4 : 0));
706
- put_byte(s, OS_CODE);
707
- s->status = BUSY_STATE;
708
- }
709
- else {
710
- put_byte(s, (s->gzhead->text ? 1 : 0) +
711
- (s->gzhead->hcrc ? 2 : 0) +
712
- (s->gzhead->extra == Z_NULL ? 0 : 4) +
713
- (s->gzhead->name == Z_NULL ? 0 : 8) +
714
- (s->gzhead->comment == Z_NULL ? 0 : 16)
715
- );
716
- put_byte(s, (Byte)(s->gzhead->time & 0xff));
717
- put_byte(s, (Byte)((s->gzhead->time >> 8) & 0xff));
718
- put_byte(s, (Byte)((s->gzhead->time >> 16) & 0xff));
719
- put_byte(s, (Byte)((s->gzhead->time >> 24) & 0xff));
720
- put_byte(s, s->level == 9 ? 2 :
721
- (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
722
- 4 : 0));
723
- put_byte(s, s->gzhead->os & 0xff);
724
- if (s->gzhead->extra != Z_NULL) {
725
- put_byte(s, s->gzhead->extra_len & 0xff);
726
- put_byte(s, (s->gzhead->extra_len >> 8) & 0xff);
727
- }
728
- if (s->gzhead->hcrc)
729
- strm->adler = crc32(strm->adler, s->pending_buf,
730
- s->pending);
731
- s->gzindex = 0;
732
- s->status = EXTRA_STATE;
733
- }
734
- }
815
+ /* zlib header */
816
+ uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8;
817
+ uInt level_flags;
818
+
819
+ if (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2)
820
+ level_flags = 0;
821
+ else if (s->level < 6)
822
+ level_flags = 1;
823
+ else if (s->level == 6)
824
+ level_flags = 2;
735
825
  else
736
- #endif
737
- {
738
- uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8;
739
- uInt level_flags;
740
-
741
- if (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2)
742
- level_flags = 0;
743
- else if (s->level < 6)
744
- level_flags = 1;
745
- else if (s->level == 6)
746
- level_flags = 2;
747
- else
748
- level_flags = 3;
749
- header |= (level_flags << 6);
750
- if (s->strstart != 0) header |= PRESET_DICT;
751
- header += 31 - (header % 31);
826
+ level_flags = 3;
827
+ header |= (level_flags << 6);
828
+ if (s->strstart != 0) header |= PRESET_DICT;
829
+ header += 31 - (header % 31);
830
+
831
+ putShortMSB(s, header);
752
832
 
833
+ /* Save the adler32 of the preset dictionary: */
834
+ if (s->strstart != 0) {
835
+ putShortMSB(s, (uInt)(strm->adler >> 16));
836
+ putShortMSB(s, (uInt)(strm->adler & 0xffff));
837
+ }
838
+ strm->adler = adler32(0L, Z_NULL, 0);
839
+ s->status = BUSY_STATE;
840
+
841
+ /* Compression must start with an empty pending buffer */
842
+ flush_pending(strm);
843
+ if (s->pending != 0) {
844
+ s->last_flush = -1;
845
+ return Z_OK;
846
+ }
847
+ }
848
+ #ifdef GZIP
849
+ if (s->status == GZIP_STATE) {
850
+ /* gzip header */
851
+ strm->adler = crc32(0L, Z_NULL, 0);
852
+ put_byte(s, 31);
853
+ put_byte(s, 139);
854
+ put_byte(s, 8);
855
+ if (s->gzhead == Z_NULL) {
856
+ put_byte(s, 0);
857
+ put_byte(s, 0);
858
+ put_byte(s, 0);
859
+ put_byte(s, 0);
860
+ put_byte(s, 0);
861
+ put_byte(s, s->level == 9 ? 2 :
862
+ (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
863
+ 4 : 0));
864
+ put_byte(s, OS_CODE);
753
865
  s->status = BUSY_STATE;
754
- putShortMSB(s, header);
755
866
 
756
- /* Save the adler32 of the preset dictionary: */
757
- if (s->strstart != 0) {
758
- putShortMSB(s, (uInt)(strm->adler >> 16));
759
- putShortMSB(s, (uInt)(strm->adler & 0xffff));
867
+ /* Compression must start with an empty pending buffer */
868
+ flush_pending(strm);
869
+ if (s->pending != 0) {
870
+ s->last_flush = -1;
871
+ return Z_OK;
872
+ }
873
+ }
874
+ else {
875
+ put_byte(s, (s->gzhead->text ? 1 : 0) +
876
+ (s->gzhead->hcrc ? 2 : 0) +
877
+ (s->gzhead->extra == Z_NULL ? 0 : 4) +
878
+ (s->gzhead->name == Z_NULL ? 0 : 8) +
879
+ (s->gzhead->comment == Z_NULL ? 0 : 16)
880
+ );
881
+ put_byte(s, (Byte)(s->gzhead->time & 0xff));
882
+ put_byte(s, (Byte)((s->gzhead->time >> 8) & 0xff));
883
+ put_byte(s, (Byte)((s->gzhead->time >> 16) & 0xff));
884
+ put_byte(s, (Byte)((s->gzhead->time >> 24) & 0xff));
885
+ put_byte(s, s->level == 9 ? 2 :
886
+ (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
887
+ 4 : 0));
888
+ put_byte(s, s->gzhead->os & 0xff);
889
+ if (s->gzhead->extra != Z_NULL) {
890
+ put_byte(s, s->gzhead->extra_len & 0xff);
891
+ put_byte(s, (s->gzhead->extra_len >> 8) & 0xff);
760
892
  }
761
- strm->adler = adler32(0L, Z_NULL, 0);
893
+ if (s->gzhead->hcrc)
894
+ strm->adler = crc32(strm->adler, s->pending_buf,
895
+ s->pending);
896
+ s->gzindex = 0;
897
+ s->status = EXTRA_STATE;
762
898
  }
763
899
  }
764
- #ifdef GZIP
765
900
  if (s->status == EXTRA_STATE) {
766
901
  if (s->gzhead->extra != Z_NULL) {
767
- uInt beg = s->pending; /* start of bytes to update crc */
768
-
769
- while (s->gzindex < (s->gzhead->extra_len & 0xffff)) {
770
- if (s->pending == s->pending_buf_size) {
771
- if (s->gzhead->hcrc && s->pending > beg)
772
- strm->adler = crc32(strm->adler, s->pending_buf + beg,
773
- s->pending - beg);
774
- flush_pending(strm);
775
- beg = s->pending;
776
- if (s->pending == s->pending_buf_size)
777
- break;
902
+ ulg beg = s->pending; /* start of bytes to update crc */
903
+ uInt left = (s->gzhead->extra_len & 0xffff) - s->gzindex;
904
+ while (s->pending + left > s->pending_buf_size) {
905
+ uInt copy = s->pending_buf_size - s->pending;
906
+ zmemcpy(s->pending_buf + s->pending,
907
+ s->gzhead->extra + s->gzindex, copy);
908
+ s->pending = s->pending_buf_size;
909
+ HCRC_UPDATE(beg);
910
+ s->gzindex += copy;
911
+ flush_pending(strm);
912
+ if (s->pending != 0) {
913
+ s->last_flush = -1;
914
+ return Z_OK;
778
915
  }
779
- put_byte(s, s->gzhead->extra[s->gzindex]);
780
- s->gzindex++;
781
- }
782
- if (s->gzhead->hcrc && s->pending > beg)
783
- strm->adler = crc32(strm->adler, s->pending_buf + beg,
784
- s->pending - beg);
785
- if (s->gzindex == s->gzhead->extra_len) {
786
- s->gzindex = 0;
787
- s->status = NAME_STATE;
916
+ beg = 0;
917
+ left -= copy;
788
918
  }
919
+ zmemcpy(s->pending_buf + s->pending,
920
+ s->gzhead->extra + s->gzindex, left);
921
+ s->pending += left;
922
+ HCRC_UPDATE(beg);
923
+ s->gzindex = 0;
789
924
  }
790
- else
791
- s->status = NAME_STATE;
925
+ s->status = NAME_STATE;
792
926
  }
793
927
  if (s->status == NAME_STATE) {
794
928
  if (s->gzhead->name != Z_NULL) {
795
- uInt beg = s->pending; /* start of bytes to update crc */
929
+ ulg beg = s->pending; /* start of bytes to update crc */
796
930
  int val;
797
-
798
931
  do {
799
932
  if (s->pending == s->pending_buf_size) {
800
- if (s->gzhead->hcrc && s->pending > beg)
801
- strm->adler = crc32(strm->adler, s->pending_buf + beg,
802
- s->pending - beg);
933
+ HCRC_UPDATE(beg);
803
934
  flush_pending(strm);
804
- beg = s->pending;
805
- if (s->pending == s->pending_buf_size) {
806
- val = 1;
807
- break;
935
+ if (s->pending != 0) {
936
+ s->last_flush = -1;
937
+ return Z_OK;
808
938
  }
939
+ beg = 0;
809
940
  }
810
941
  val = s->gzhead->name[s->gzindex++];
811
942
  put_byte(s, val);
812
943
  } while (val != 0);
813
- if (s->gzhead->hcrc && s->pending > beg)
814
- strm->adler = crc32(strm->adler, s->pending_buf + beg,
815
- s->pending - beg);
816
- if (val == 0) {
817
- s->gzindex = 0;
818
- s->status = COMMENT_STATE;
819
- }
944
+ HCRC_UPDATE(beg);
945
+ s->gzindex = 0;
820
946
  }
821
- else
822
- s->status = COMMENT_STATE;
947
+ s->status = COMMENT_STATE;
823
948
  }
824
949
  if (s->status == COMMENT_STATE) {
825
950
  if (s->gzhead->comment != Z_NULL) {
826
- uInt beg = s->pending; /* start of bytes to update crc */
951
+ ulg beg = s->pending; /* start of bytes to update crc */
827
952
  int val;
828
-
829
953
  do {
830
954
  if (s->pending == s->pending_buf_size) {
831
- if (s->gzhead->hcrc && s->pending > beg)
832
- strm->adler = crc32(strm->adler, s->pending_buf + beg,
833
- s->pending - beg);
955
+ HCRC_UPDATE(beg);
834
956
  flush_pending(strm);
835
- beg = s->pending;
836
- if (s->pending == s->pending_buf_size) {
837
- val = 1;
838
- break;
957
+ if (s->pending != 0) {
958
+ s->last_flush = -1;
959
+ return Z_OK;
839
960
  }
961
+ beg = 0;
840
962
  }
841
963
  val = s->gzhead->comment[s->gzindex++];
842
964
  put_byte(s, val);
843
965
  } while (val != 0);
844
- if (s->gzhead->hcrc && s->pending > beg)
845
- strm->adler = crc32(strm->adler, s->pending_buf + beg,
846
- s->pending - beg);
847
- if (val == 0)
848
- s->status = HCRC_STATE;
966
+ HCRC_UPDATE(beg);
849
967
  }
850
- else
851
- s->status = HCRC_STATE;
968
+ s->status = HCRC_STATE;
852
969
  }
853
970
  if (s->status == HCRC_STATE) {
854
971
  if (s->gzhead->hcrc) {
855
- if (s->pending + 2 > s->pending_buf_size)
972
+ if (s->pending + 2 > s->pending_buf_size) {
856
973
  flush_pending(strm);
857
- if (s->pending + 2 <= s->pending_buf_size) {
858
- put_byte(s, (Byte)(strm->adler & 0xff));
859
- put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
860
- strm->adler = crc32(0L, Z_NULL, 0);
861
- s->status = BUSY_STATE;
974
+ if (s->pending != 0) {
975
+ s->last_flush = -1;
976
+ return Z_OK;
977
+ }
862
978
  }
979
+ put_byte(s, (Byte)(strm->adler & 0xff));
980
+ put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
981
+ strm->adler = crc32(0L, Z_NULL, 0);
863
982
  }
864
- else
865
- s->status = BUSY_STATE;
866
- }
867
- #endif
983
+ s->status = BUSY_STATE;
868
984
 
869
- /* Flush as much pending output as possible */
870
- if (s->pending != 0) {
985
+ /* Compression must start with an empty pending buffer */
871
986
  flush_pending(strm);
872
- if (strm->avail_out == 0) {
873
- /* Since avail_out is 0, deflate will be called again with
874
- * more output space, but possibly with both pending and
875
- * avail_in equal to zero. There won't be anything to do,
876
- * but this is not an error situation so make sure we
877
- * return OK instead of BUF_ERROR at next call of deflate:
878
- */
987
+ if (s->pending != 0) {
879
988
  s->last_flush = -1;
880
989
  return Z_OK;
881
990
  }
882
-
883
- /* Make sure there is something to do and avoid duplicate consecutive
884
- * flushes. For repeated and useless calls with Z_FINISH, we keep
885
- * returning Z_STREAM_END instead of Z_BUF_ERROR.
886
- */
887
- } else if (strm->avail_in == 0 && RANK(flush) <= RANK(old_flush) &&
888
- flush != Z_FINISH) {
889
- ERR_RETURN(strm, Z_BUF_ERROR);
890
- }
891
-
892
- /* User must not provide more input after the first FINISH: */
893
- if (s->status == FINISH_STATE && strm->avail_in != 0) {
894
- ERR_RETURN(strm, Z_BUF_ERROR);
895
991
  }
992
+ #endif
896
993
 
897
994
  /* Start a new block or continue the current one.
898
995
  */
@@ -900,9 +997,10 @@ int ZEXPORT deflate (strm, flush)
900
997
  (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) {
901
998
  block_state bstate;
902
999
 
903
- bstate = s->strategy == Z_HUFFMAN_ONLY ? deflate_huff(s, flush) :
904
- (s->strategy == Z_RLE ? deflate_rle(s, flush) :
905
- (*(configuration_table[s->level].func))(s, flush));
1000
+ bstate = s->level == 0 ? deflate_stored(s, flush) :
1001
+ s->strategy == Z_HUFFMAN_ONLY ? deflate_huff(s, flush) :
1002
+ s->strategy == Z_RLE ? deflate_rle(s, flush) :
1003
+ (*(configuration_table[s->level].func))(s, flush);
906
1004
 
907
1005
  if (bstate == finish_started || bstate == finish_done) {
908
1006
  s->status = FINISH_STATE;
@@ -944,7 +1042,6 @@ int ZEXPORT deflate (strm, flush)
944
1042
  }
945
1043
  }
946
1044
  }
947
- Assert(strm->avail_out > 0, "bug2");
948
1045
 
949
1046
  if (flush != Z_FINISH) return Z_OK;
950
1047
  if (s->wrap <= 0) return Z_STREAM_END;
@@ -981,18 +1078,9 @@ int ZEXPORT deflateEnd (strm)
981
1078
  {
982
1079
  int status;
983
1080
 
984
- if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
1081
+ if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
985
1082
 
986
1083
  status = strm->state->status;
987
- if (status != INIT_STATE &&
988
- status != EXTRA_STATE &&
989
- status != NAME_STATE &&
990
- status != COMMENT_STATE &&
991
- status != HCRC_STATE &&
992
- status != BUSY_STATE &&
993
- status != FINISH_STATE) {
994
- return Z_STREAM_ERROR;
995
- }
996
1084
 
997
1085
  /* Deallocate in reverse order of allocations: */
998
1086
  TRY_FREE(strm, strm->state->pending_buf);
@@ -1023,7 +1111,7 @@ int ZEXPORT deflateCopy (dest, source)
1023
1111
  ushf *overlay;
1024
1112
 
1025
1113
 
1026
- if (source == Z_NULL || dest == Z_NULL || source->state == Z_NULL) {
1114
+ if (deflateStateCheck(source) || dest == Z_NULL) {
1027
1115
  return Z_STREAM_ERROR;
1028
1116
  }
1029
1117
 
@@ -1073,7 +1161,7 @@ int ZEXPORT deflateCopy (dest, source)
1073
1161
  * allocating a large strm->next_in buffer and copying from it.
1074
1162
  * (See also flush_pending()).
1075
1163
  */
1076
- local int read_buf(strm, buf, size)
1164
+ local unsigned read_buf(strm, buf, size)
1077
1165
  z_streamp strm;
1078
1166
  Bytef *buf;
1079
1167
  unsigned size;
@@ -1097,7 +1185,7 @@ local int read_buf(strm, buf, size)
1097
1185
  strm->next_in += len;
1098
1186
  strm->total_in += len;
1099
1187
 
1100
- return (int)len;
1188
+ return len;
1101
1189
  }
1102
1190
 
1103
1191
  /* ===========================================================================
@@ -1151,9 +1239,9 @@ local uInt longest_match(s, cur_match)
1151
1239
  {
1152
1240
  unsigned chain_length = s->max_chain_length;/* max hash chain length */
1153
1241
  register Bytef *scan = s->window + s->strstart; /* current string */
1154
- register Bytef *match; /* matched string */
1242
+ register Bytef *match; /* matched string */
1155
1243
  register int len; /* length of current match */
1156
- int best_len = s->prev_length; /* best match length so far */
1244
+ int best_len = (int)s->prev_length; /* best match length so far */
1157
1245
  int nice_match = s->nice_match; /* stop if match long enough */
1158
1246
  IPos limit = s->strstart > (IPos)MAX_DIST(s) ?
1159
1247
  s->strstart - (IPos)MAX_DIST(s) : NIL;
@@ -1188,7 +1276,7 @@ local uInt longest_match(s, cur_match)
1188
1276
  /* Do not look for matches beyond the end of the input. This is necessary
1189
1277
  * to make deflate deterministic.
1190
1278
  */
1191
- if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead;
1279
+ if ((uInt)nice_match > s->lookahead) nice_match = (int)s->lookahead;
1192
1280
 
1193
1281
  Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
1194
1282
 
@@ -1349,7 +1437,11 @@ local uInt longest_match(s, cur_match)
1349
1437
 
1350
1438
  #endif /* FASTEST */
1351
1439
 
1352
- #ifdef DEBUG
1440
+ #ifdef ZLIB_DEBUG
1441
+
1442
+ #define EQUAL 0
1443
+ /* result of memcmp for equal strings */
1444
+
1353
1445
  /* ===========================================================================
1354
1446
  * Check that the match at match_start is indeed a match.
1355
1447
  */
@@ -1375,7 +1467,7 @@ local void check_match(s, start, match, length)
1375
1467
  }
1376
1468
  #else
1377
1469
  # define check_match(s, start, match, length)
1378
- #endif /* DEBUG */
1470
+ #endif /* ZLIB_DEBUG */
1379
1471
 
1380
1472
  /* ===========================================================================
1381
1473
  * Fill the window when the lookahead becomes insufficient.
@@ -1390,8 +1482,7 @@ local void check_match(s, start, match, length)
1390
1482
  local void fill_window(s)
1391
1483
  deflate_state *s;
1392
1484
  {
1393
- register unsigned n, m;
1394
- register Posf *p;
1485
+ unsigned n;
1395
1486
  unsigned more; /* Amount of free space at the end of the window. */
1396
1487
  uInt wsize = s->w_size;
1397
1488
 
@@ -1418,35 +1509,11 @@ local void fill_window(s)
1418
1509
  */
1419
1510
  if (s->strstart >= wsize+MAX_DIST(s)) {
1420
1511
 
1421
- zmemcpy(s->window, s->window+wsize, (unsigned)wsize);
1512
+ zmemcpy(s->window, s->window+wsize, (unsigned)wsize - more);
1422
1513
  s->match_start -= wsize;
1423
1514
  s->strstart -= wsize; /* we now have strstart >= MAX_DIST */
1424
1515
  s->block_start -= (long) wsize;
1425
-
1426
- /* Slide the hash table (could be avoided with 32 bit values
1427
- at the expense of memory usage). We slide even when level == 0
1428
- to keep the hash table consistent if we switch back to level > 0
1429
- later. (Using level 0 permanently is not an optimal usage of
1430
- zlib, so we don't care about this pathological case.)
1431
- */
1432
- n = s->hash_size;
1433
- p = &s->head[n];
1434
- do {
1435
- m = *--p;
1436
- *p = (Pos)(m >= wsize ? m-wsize : NIL);
1437
- } while (--n);
1438
-
1439
- n = wsize;
1440
- #ifndef FASTEST
1441
- p = &s->prev[n];
1442
- do {
1443
- m = *--p;
1444
- *p = (Pos)(m >= wsize ? m-wsize : NIL);
1445
- /* If n is not on any hash chain, prev[n] is garbage but
1446
- * its value will never be used.
1447
- */
1448
- } while (--n);
1449
- #endif
1516
+ slide_hash(s);
1450
1517
  more += wsize;
1451
1518
  }
1452
1519
  if (s->strm->avail_in == 0) break;
@@ -1552,70 +1619,199 @@ local void fill_window(s)
1552
1619
  if (s->strm->avail_out == 0) return (last) ? finish_started : need_more; \
1553
1620
  }
1554
1621
 
1622
+ /* Maximum stored block length in deflate format (not including header). */
1623
+ #define MAX_STORED 65535
1624
+
1625
+ /* Minimum of a and b. */
1626
+ #define MIN(a, b) ((a) > (b) ? (b) : (a))
1627
+
1555
1628
  /* ===========================================================================
1556
1629
  * Copy without compression as much as possible from the input stream, return
1557
1630
  * the current block state.
1558
- * This function does not insert new strings in the dictionary since
1559
- * uncompressible data is probably not useful. This function is used
1560
- * only for the level=0 compression option.
1561
- * NOTE: this function should be optimized to avoid extra copying from
1562
- * window to pending_buf.
1631
+ *
1632
+ * In case deflateParams() is used to later switch to a non-zero compression
1633
+ * level, s->matches (otherwise unused when storing) keeps track of the number
1634
+ * of hash table slides to perform. If s->matches is 1, then one hash table
1635
+ * slide will be done when switching. If s->matches is 2, the maximum value
1636
+ * allowed here, then the hash table will be cleared, since two or more slides
1637
+ * is the same as a clear.
1638
+ *
1639
+ * deflate_stored() is written to minimize the number of times an input byte is
1640
+ * copied. It is most efficient with large input and output buffers, which
1641
+ * maximizes the opportunites to have a single copy from next_in to next_out.
1563
1642
  */
1564
1643
  local block_state deflate_stored(s, flush)
1565
1644
  deflate_state *s;
1566
1645
  int flush;
1567
1646
  {
1568
- /* Stored blocks are limited to 0xffff bytes, pending_buf is limited
1569
- * to pending_buf_size, and each stored block has a 5 byte header:
1647
+ /* Smallest worthy block size when not flushing or finishing. By default
1648
+ * this is 32K. This can be as small as 507 bytes for memLevel == 1. For
1649
+ * large input and output buffers, the stored block size will be larger.
1570
1650
  */
1571
- ulg max_block_size = 0xffff;
1572
- ulg max_start;
1573
-
1574
- if (max_block_size > s->pending_buf_size - 5) {
1575
- max_block_size = s->pending_buf_size - 5;
1576
- }
1651
+ unsigned min_block = MIN(s->pending_buf_size - 5, s->w_size);
1577
1652
 
1578
- /* Copy as much as possible from input to output: */
1579
- for (;;) {
1580
- /* Fill the window as much as possible: */
1581
- if (s->lookahead <= 1) {
1582
-
1583
- Assert(s->strstart < s->w_size+MAX_DIST(s) ||
1584
- s->block_start >= (long)s->w_size, "slide too late");
1653
+ /* Copy as many min_block or larger stored blocks directly to next_out as
1654
+ * possible. If flushing, copy the remaining available input to next_out as
1655
+ * stored blocks, if there is enough space.
1656
+ */
1657
+ unsigned len, left, have, last = 0;
1658
+ unsigned used = s->strm->avail_in;
1659
+ do {
1660
+ /* Set len to the maximum size block that we can copy directly with the
1661
+ * available input data and output space. Set left to how much of that
1662
+ * would be copied from what's left in the window.
1663
+ */
1664
+ len = MAX_STORED; /* maximum deflate stored block length */
1665
+ have = (s->bi_valid + 42) >> 3; /* number of header bytes */
1666
+ if (s->strm->avail_out < have) /* need room for header */
1667
+ break;
1668
+ /* maximum stored block length that will fit in avail_out: */
1669
+ have = s->strm->avail_out - have;
1670
+ left = s->strstart - s->block_start; /* bytes left in window */
1671
+ if (len > (ulg)left + s->strm->avail_in)
1672
+ len = left + s->strm->avail_in; /* limit len to the input */
1673
+ if (len > have)
1674
+ len = have; /* limit len to the output */
1675
+
1676
+ /* If the stored block would be less than min_block in length, or if
1677
+ * unable to copy all of the available input when flushing, then try
1678
+ * copying to the window and the pending buffer instead. Also don't
1679
+ * write an empty block when flushing -- deflate() does that.
1680
+ */
1681
+ if (len < min_block && ((len == 0 && flush != Z_FINISH) ||
1682
+ flush == Z_NO_FLUSH ||
1683
+ len != left + s->strm->avail_in))
1684
+ break;
1585
1685
 
1586
- fill_window(s);
1587
- if (s->lookahead == 0 && flush == Z_NO_FLUSH) return need_more;
1686
+ /* Make a dummy stored block in pending to get the header bytes,
1687
+ * including any pending bits. This also updates the debugging counts.
1688
+ */
1689
+ last = flush == Z_FINISH && len == left + s->strm->avail_in ? 1 : 0;
1690
+ _tr_stored_block(s, (char *)0, 0L, last);
1691
+
1692
+ /* Replace the lengths in the dummy stored block with len. */
1693
+ s->pending_buf[s->pending - 4] = len;
1694
+ s->pending_buf[s->pending - 3] = len >> 8;
1695
+ s->pending_buf[s->pending - 2] = ~len;
1696
+ s->pending_buf[s->pending - 1] = ~len >> 8;
1697
+
1698
+ /* Write the stored block header bytes. */
1699
+ flush_pending(s->strm);
1700
+
1701
+ #ifdef ZLIB_DEBUG
1702
+ /* Update debugging counts for the data about to be copied. */
1703
+ s->compressed_len += len << 3;
1704
+ s->bits_sent += len << 3;
1705
+ #endif
1588
1706
 
1589
- if (s->lookahead == 0) break; /* flush the current block */
1707
+ /* Copy uncompressed bytes from the window to next_out. */
1708
+ if (left) {
1709
+ if (left > len)
1710
+ left = len;
1711
+ zmemcpy(s->strm->next_out, s->window + s->block_start, left);
1712
+ s->strm->next_out += left;
1713
+ s->strm->avail_out -= left;
1714
+ s->strm->total_out += left;
1715
+ s->block_start += left;
1716
+ len -= left;
1590
1717
  }
1591
- Assert(s->block_start >= 0L, "block gone");
1592
-
1593
- s->strstart += s->lookahead;
1594
- s->lookahead = 0;
1595
-
1596
- /* Emit a stored block if pending_buf will be full: */
1597
- max_start = s->block_start + max_block_size;
1598
- if (s->strstart == 0 || (ulg)s->strstart >= max_start) {
1599
- /* strstart == 0 is possible when wraparound on 16-bit machine */
1600
- s->lookahead = (uInt)(s->strstart - max_start);
1601
- s->strstart = (uInt)max_start;
1602
- FLUSH_BLOCK(s, 0);
1718
+
1719
+ /* Copy uncompressed bytes directly from next_in to next_out, updating
1720
+ * the check value.
1721
+ */
1722
+ if (len) {
1723
+ read_buf(s->strm, s->strm->next_out, len);
1724
+ s->strm->next_out += len;
1725
+ s->strm->avail_out -= len;
1726
+ s->strm->total_out += len;
1603
1727
  }
1604
- /* Flush if we may have to slide, otherwise block_start may become
1605
- * negative and the data will be gone:
1728
+ } while (last == 0);
1729
+
1730
+ /* Update the sliding window with the last s->w_size bytes of the copied
1731
+ * data, or append all of the copied data to the existing window if less
1732
+ * than s->w_size bytes were copied. Also update the number of bytes to
1733
+ * insert in the hash tables, in the event that deflateParams() switches to
1734
+ * a non-zero compression level.
1735
+ */
1736
+ used -= s->strm->avail_in; /* number of input bytes directly copied */
1737
+ if (used) {
1738
+ /* If any input was used, then no unused input remains in the window,
1739
+ * therefore s->block_start == s->strstart.
1606
1740
  */
1607
- if (s->strstart - (uInt)s->block_start >= MAX_DIST(s)) {
1608
- FLUSH_BLOCK(s, 0);
1741
+ if (used >= s->w_size) { /* supplant the previous history */
1742
+ s->matches = 2; /* clear hash */
1743
+ zmemcpy(s->window, s->strm->next_in - s->w_size, s->w_size);
1744
+ s->strstart = s->w_size;
1609
1745
  }
1746
+ else {
1747
+ if (s->window_size - s->strstart <= used) {
1748
+ /* Slide the window down. */
1749
+ s->strstart -= s->w_size;
1750
+ zmemcpy(s->window, s->window + s->w_size, s->strstart);
1751
+ if (s->matches < 2)
1752
+ s->matches++; /* add a pending slide_hash() */
1753
+ }
1754
+ zmemcpy(s->window + s->strstart, s->strm->next_in - used, used);
1755
+ s->strstart += used;
1756
+ }
1757
+ s->block_start = s->strstart;
1758
+ s->insert += MIN(used, s->w_size - s->insert);
1610
1759
  }
1611
- s->insert = 0;
1612
- if (flush == Z_FINISH) {
1613
- FLUSH_BLOCK(s, 1);
1760
+ if (s->high_water < s->strstart)
1761
+ s->high_water = s->strstart;
1762
+
1763
+ /* If the last block was written to next_out, then done. */
1764
+ if (last)
1614
1765
  return finish_done;
1766
+
1767
+ /* If flushing and all input has been consumed, then done. */
1768
+ if (flush != Z_NO_FLUSH && flush != Z_FINISH &&
1769
+ s->strm->avail_in == 0 && (long)s->strstart == s->block_start)
1770
+ return block_done;
1771
+
1772
+ /* Fill the window with any remaining input. */
1773
+ have = s->window_size - s->strstart - 1;
1774
+ if (s->strm->avail_in > have && s->block_start >= (long)s->w_size) {
1775
+ /* Slide the window down. */
1776
+ s->block_start -= s->w_size;
1777
+ s->strstart -= s->w_size;
1778
+ zmemcpy(s->window, s->window + s->w_size, s->strstart);
1779
+ if (s->matches < 2)
1780
+ s->matches++; /* add a pending slide_hash() */
1781
+ have += s->w_size; /* more space now */
1615
1782
  }
1616
- if ((long)s->strstart > s->block_start)
1617
- FLUSH_BLOCK(s, 0);
1618
- return block_done;
1783
+ if (have > s->strm->avail_in)
1784
+ have = s->strm->avail_in;
1785
+ if (have) {
1786
+ read_buf(s->strm, s->window + s->strstart, have);
1787
+ s->strstart += have;
1788
+ }
1789
+ if (s->high_water < s->strstart)
1790
+ s->high_water = s->strstart;
1791
+
1792
+ /* There was not enough avail_out to write a complete worthy or flushed
1793
+ * stored block to next_out. Write a stored block to pending instead, if we
1794
+ * have enough input for a worthy block, or if flushing and there is enough
1795
+ * room for the remaining input as a stored block in the pending buffer.
1796
+ */
1797
+ have = (s->bi_valid + 42) >> 3; /* number of header bytes */
1798
+ /* maximum stored block length that will fit in pending: */
1799
+ have = MIN(s->pending_buf_size - have, MAX_STORED);
1800
+ min_block = MIN(have, s->w_size);
1801
+ left = s->strstart - s->block_start;
1802
+ if (left >= min_block ||
1803
+ ((left || flush == Z_FINISH) && flush != Z_NO_FLUSH &&
1804
+ s->strm->avail_in == 0 && left <= have)) {
1805
+ len = MIN(left, have);
1806
+ last = flush == Z_FINISH && s->strm->avail_in == 0 &&
1807
+ len == left ? 1 : 0;
1808
+ _tr_stored_block(s, (charf *)s->window + s->block_start, len, last);
1809
+ s->block_start += len;
1810
+ flush_pending(s->strm);
1811
+ }
1812
+
1813
+ /* We've done all we can with the available input and output. */
1814
+ return last ? finish_started : need_more;
1619
1815
  }
1620
1816
 
1621
1817
  /* ===========================================================================
@@ -1892,7 +2088,7 @@ local block_state deflate_rle(s, flush)
1892
2088
  prev == *++scan && prev == *++scan &&
1893
2089
  prev == *++scan && prev == *++scan &&
1894
2090
  scan < strend);
1895
- s->match_length = MAX_MATCH - (int)(strend - scan);
2091
+ s->match_length = MAX_MATCH - (uInt)(strend - scan);
1896
2092
  if (s->match_length > s->lookahead)
1897
2093
  s->match_length = s->lookahead;
1898
2094
  }