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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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;
|
... | ... | |
389 |
442 |
}
|
390 |
443 |
|
391 |
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 |
|
|
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
694 |
wraplen += 2;
|
608 |
695 |
}
|
609 |
696 |
break;
|
|
697 |
#endif
|
610 |
698 |
default: /* for compiler happiness */
|
611 |
699 |
wraplen = 6;
|
612 |
700 |
}
|
... | ... | |
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 |
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 |
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;
|
|
826 |
level_flags = 3;
|
|
827 |
header |= (level_flags << 6);
|
|
828 |
if (s->strstart != 0) header |= PRESET_DICT;
|
|
829 |
header += 31 - (header % 31);
|
740 |
830 |
|
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);
|
|
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;
|
760 |
872 |
}
|
761 |
|
strm->adler = adler32(0L, Z_NULL, 0);
|
|
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);
|
|
892 |
}
|
|
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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;
|
|
1651 |
unsigned min_block = MIN(s->pending_buf_size - 5, s->w_size);
|
1573 |
1652 |
|
1574 |
|
if (max_block_size > s->pending_buf_size - 5) {
|
1575 |
|
max_block_size = s->pending_buf_size - 5;
|
1576 |
|
}
|
|
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 */
|
1577 |
1675 |
|
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) {
|
|
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;
|
1582 |
1685 |
|
1583 |
|
Assert(s->strstart < s->w_size+MAX_DIST(s) ||
|
1584 |
|
s->block_start >= (long)s->w_size, "slide too late");
|
|
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);
|
1585 |
1691 |
|
1586 |
|
fill_window(s);
|
1587 |
|
if (s->lookahead == 0 && flush == Z_NO_FLUSH) return need_more;
|
|
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;
|
1588 |
1697 |
|
1589 |
|
if (s->lookahead == 0) break; /* flush the current block */
|
1590 |
|
}
|
1591 |
|
Assert(s->block_start >= 0L, "block gone");
|
|
1698 |
/* Write the stored block header bytes. */
|
|
1699 |
flush_pending(s->strm);
|
1592 |
1700 |
|
1593 |
|
s->strstart += s->lookahead;
|
1594 |
|
s->lookahead = 0;
|
|
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
|
1595 |
1706 |
|
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);
|
|
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;
|
1603 |
1717 |
}
|
1604 |
|
/* Flush if we may have to slide, otherwise block_start may become
|
1605 |
|
* negative and the data will be gone:
|
|
1718 |
|
|
1719 |
/* Copy uncompressed bytes directly from next_in to next_out, updating
|
|
1720 |
* the check value.
|
1606 |
1721 |
*/
|
1607 |
|
if (s->strstart - (uInt)s->block_start >= MAX_DIST(s)) {
|
1608 |
|
FLUSH_BLOCK(s, 0);
|
|
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;
|
|
1727 |
}
|
|
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 |
*/
|