80
80
local block_state deflate_slow OF((deflate_state *s, int flush));
82
local block_state deflate_rle OF((deflate_state *s, int flush));
83
local block_state deflate_huff OF((deflate_state *s, int flush));
82
84
local void lm_init OF((deflate_state *s));
83
85
local void putShortMSB OF((deflate_state *s, uInt b));
84
86
local void flush_pending OF((z_streamp strm));
85
87
local int read_buf OF((z_streamp strm, Bytef *buf, unsigned size));
88
89
void match_init OF((void)); /* asm code initialization */
89
90
uInt longest_match OF((deflate_state *s, IPos cur_match));
91
92
local uInt longest_match OF((deflate_state *s, IPos cur_match));
94
local uInt longest_match_fast OF((deflate_state *s, IPos cur_match));
97
96
local void check_match OF((deflate_state *s, IPos start, IPos match,
111
110
/* Matches of length 3 are discarded if their distance exceeds TOO_FAR */
113
#define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
114
/* Minimum amount of lookahead, except at the end of the input file.
115
* See deflate.c for comments about the MIN_MATCH+1.
118
112
/* Values for max_lazy_match, good_match and max_chain_length, depending on
119
113
* the desired pack level (0..9). The values given below have been tuned to
120
114
* exclude worst case performance for pathological files. Better values may be
288
282
s->prev = (Posf *) ZALLOC(strm, s->w_size, sizeof(Pos));
289
283
s->head = (Posf *) ZALLOC(strm, s->hash_size, sizeof(Pos));
285
s->high_water = 0; /* nothing written to s->window yet */
291
287
s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
293
289
overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2);
332
328
strm->adler = adler32(strm->adler, dictionary, dictLength);
334
330
if (length < MIN_MATCH) return Z_OK;
335
if (length > MAX_DIST(s)) {
336
length = MAX_DIST(s);
331
if (length > s->w_size) {
337
333
dictionary += dictLength - length; /* use the tail of the dictionary */
339
335
zmemcpy(s->window, dictionary, length);
436
432
func = configuration_table[s->level].func;
438
if (func != configuration_table[level].func && strm->total_in != 0) {
434
if ((strategy != s->strategy || func != configuration_table[level].func) &&
435
strm->total_in != 0) {
439
436
/* Flush the last buffer: */
440
err = deflate(strm, Z_PARTIAL_FLUSH);
437
err = deflate(strm, Z_BLOCK);
442
439
if (s->level != level) {
443
440
s->level = level;
481
478
* resulting from using fixed blocks instead of stored blocks, which deflate
482
479
* can emit on compressed data for some combinations of the parameters.
484
* This function could be more sophisticated to provide closer upper bounds
485
* for every combination of windowBits and memLevel, as well as wrap.
486
* But even the conservative upper bound of about 14% expansion does not
487
* seem onerous for output buffer allocation.
481
* This function could be more sophisticated to provide closer upper bounds for
482
* every combination of windowBits and memLevel. But even the conservative
483
* upper bound of about 14% expansion does not seem onerous for output buffer
489
486
uLong ZEXPORT deflateBound(strm, sourceLen)
493
490
deflate_state *s;
496
/* conservative upper bound */
497
destLen = sourceLen +
498
((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 11;
500
/* if can't get parameters, return conservative bound */
491
uLong complen, wraplen;
494
/* conservative upper bound for compressed data */
495
complen = sourceLen +
496
((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 5;
498
/* if can't get parameters, return conservative bound plus zlib wrapper */
501
499
if (strm == Z_NULL || strm->state == Z_NULL)
502
/* compute wrapper length */
505
case 0: /* raw deflate */
508
case 1: /* zlib wrapper */
509
wraplen = 6 + (s->strstart ? 4 : 0);
511
case 2: /* gzip wrapper */
513
if (s->gzhead != Z_NULL) { /* user-supplied gzip header */
514
if (s->gzhead->extra != Z_NULL)
515
wraplen += 2 + s->gzhead->extra_len;
516
str = s->gzhead->name;
521
str = s->gzhead->comment;
530
default: /* for compiler happiness */
504
534
/* if not default parameters, return conservative bound */
506
535
if (s->w_bits != 15 || s->hash_bits != 8 + 7)
536
return complen + wraplen;
509
538
/* default settings: return tight bound for that case */
510
return compressBound(sourceLen);
539
return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) +
540
(sourceLen >> 25) + 13 - 6 + wraplen;
513
543
/* =========================================================================
608
638
(s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
610
640
put_byte(s, s->gzhead->os & 0xff);
611
if (s->gzhead->extra != NULL) {
641
if (s->gzhead->extra != Z_NULL) {
612
642
put_byte(s, s->gzhead->extra_len & 0xff);
613
643
put_byte(s, (s->gzhead->extra_len >> 8) & 0xff);
787
817
(flush != Z_NO_FLUSH && s->status != FINISH_STATE)) {
788
818
block_state bstate;
790
bstate = (*(configuration_table[s->level].func))(s, flush);
820
bstate = s->strategy == Z_HUFFMAN_ONLY ? deflate_huff(s, flush) :
821
(s->strategy == Z_RLE ? deflate_rle(s, flush) :
822
(*(configuration_table[s->level].func))(s, flush));
792
824
if (bstate == finish_started || bstate == finish_done) {
793
825
s->status = FINISH_STATE;
808
840
if (bstate == block_done) {
809
841
if (flush == Z_PARTIAL_FLUSH) {
811
} else { /* FULL_FLUSH or SYNC_FLUSH */
843
} else if (flush != Z_BLOCK) { /* FULL_FLUSH or SYNC_FLUSH */
812
844
_tr_stored_block(s, (char*)0, 0L, 0);
813
845
/* For a full flush, this empty block will be recognized
814
846
* as a special marker by inflate_sync().
816
848
if (flush == Z_FULL_FLUSH) {
817
849
CLEAR_HASH(s); /* forget history */
850
if (s->lookahead == 0) {
820
856
flush_pending(strm);
1167
1203
return s->lookahead;
1169
1205
#endif /* ASMV */
1170
#endif /* FASTEST */
1172
1209
/* ---------------------------------------------------------------------------
1173
* Optimized version for level == 1 or strategy == Z_RLE only
1210
* Optimized version for FASTEST only
1175
local uInt longest_match_fast(s, cur_match)
1212
local uInt longest_match(s, cur_match)
1176
1213
deflate_state *s;
1177
1214
IPos cur_match; /* current match */
1357
1395
} while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0);
1397
/* If the WIN_INIT bytes after the end of the current data have never been
1398
* written, then zero those bytes in order to avoid memory check reports of
1399
* the use of uninitialized (or uninitialised as Julian writes) bytes by
1400
* the longest match routines. Update the high water mark for the next
1401
* time through here. WIN_INIT is set to MAX_MATCH since the longest match
1402
* routines allow scanning to strstart + MAX_MATCH, ignoring lookahead.
1404
if (s->high_water < s->window_size) {
1405
ulg curr = s->strstart + (ulg)(s->lookahead);
1408
if (s->high_water < curr) {
1409
/* Previous high water mark below current data -- zero WIN_INIT
1410
* bytes or up to end of window, whichever is less.
1412
init = s->window_size - curr;
1413
if (init > WIN_INIT)
1415
zmemzero(s->window + curr, (unsigned)init);
1416
s->high_water = curr + init;
1418
else if (s->high_water < (ulg)curr + WIN_INIT) {
1419
/* High water mark at or above current data, but below current data
1420
* plus WIN_INIT -- zero out to current data plus WIN_INIT, or up
1421
* to end of window, whichever is less.
1423
init = (ulg)curr + WIN_INIT - s->high_water;
1424
if (init > s->window_size - s->high_water)
1425
init = s->window_size - s->high_water;
1426
zmemzero(s->window + s->high_water, (unsigned)init);
1427
s->high_water += init;
1360
1432
/* ===========================================================================
1361
1433
* Flush the current block, with given end-of-file flag.
1362
1434
* IN assertion: strstart is set to the end of the current match.
1364
#define FLUSH_BLOCK_ONLY(s, eof) { \
1436
#define FLUSH_BLOCK_ONLY(s, last) { \
1365
1437
_tr_flush_block(s, (s->block_start >= 0L ? \
1366
1438
(charf *)&s->window[(unsigned)s->block_start] : \
1367
1439
(charf *)Z_NULL), \
1368
1440
(ulg)((long)s->strstart - s->block_start), \
1370
1442
s->block_start = s->strstart; \
1371
1443
flush_pending(s->strm); \
1372
1444
Tracev((stderr,"[FLUSH]")); \
1375
1447
/* Same but force premature exit if necessary. */
1376
#define FLUSH_BLOCK(s, eof) { \
1377
FLUSH_BLOCK_ONLY(s, eof); \
1378
if (s->strm->avail_out == 0) return (eof) ? finish_started : need_more; \
1448
#define FLUSH_BLOCK(s, last) { \
1449
FLUSH_BLOCK_ONLY(s, last); \
1450
if (s->strm->avail_out == 0) return (last) ? finish_started : need_more; \
1381
1453
/* ===========================================================================
1481
1554
* of window index 0 (in particular we have to avoid a match
1482
1555
* of the string with itself at the start of the input file).
1485
if ((s->strategy != Z_HUFFMAN_ONLY && s->strategy != Z_RLE) ||
1486
(s->strategy == Z_RLE && s->strstart - hash_head == 1)) {
1487
s->match_length = longest_match_fast (s, hash_head);
1490
if (s->strategy != Z_HUFFMAN_ONLY && s->strategy != Z_RLE) {
1491
s->match_length = longest_match (s, hash_head);
1492
} else if (s->strategy == Z_RLE && s->strstart - hash_head == 1) {
1493
s->match_length = longest_match_fast (s, hash_head);
1496
/* longest_match() or longest_match_fast() sets match_start */
1557
s->match_length = longest_match (s, hash_head);
1558
/* longest_match() sets match_start */
1498
1560
if (s->match_length >= MIN_MATCH) {
1499
1561
check_match(s, s->strstart, s->match_start, s->match_length);
1591
1654
* of window index 0 (in particular we have to avoid a match
1592
1655
* of the string with itself at the start of the input file).
1594
if (s->strategy != Z_HUFFMAN_ONLY && s->strategy != Z_RLE) {
1595
s->match_length = longest_match (s, hash_head);
1596
} else if (s->strategy == Z_RLE && s->strstart - hash_head == 1) {
1597
s->match_length = longest_match_fast (s, hash_head);
1599
/* longest_match() or longest_match_fast() sets match_start */
1657
s->match_length = longest_match (s, hash_head);
1658
/* longest_match() sets match_start */
1601
1660
if (s->match_length <= 5 && (s->strategy == Z_FILTERED
1602
1661
#if TOO_FAR <= 32767
1684
1742
deflate_state *s;
1687
int bflush; /* set if current block must be flushed */
1688
uInt run; /* length of run */
1689
uInt max; /* maximum length of run */
1690
uInt prev; /* byte at distance one to match */
1691
Bytef *scan; /* scan for end of run */
1745
int bflush; /* set if current block must be flushed */
1746
uInt prev; /* byte at distance one to match */
1747
Bytef *scan, *strend; /* scan goes up to strend for length of run */
1694
1750
/* Make sure that we always have enough lookahead, except
1706
1762
/* See how many times the previous byte repeats */
1708
if (s->strstart > 0) { /* if there is a previous byte, that is */
1709
max = s->lookahead < MAX_MATCH ? s->lookahead : MAX_MATCH;
1763
s->match_length = 0;
1764
if (s->lookahead >= MIN_MATCH && s->strstart > 0) {
1710
1765
scan = s->window + s->strstart - 1;
1713
if (*scan++ != prev)
1715
} while (++run < max);
1767
if (prev == *++scan && prev == *++scan && prev == *++scan) {
1768
strend = s->window + s->strstart + MAX_MATCH;
1770
} while (prev == *++scan && prev == *++scan &&
1771
prev == *++scan && prev == *++scan &&
1772
prev == *++scan && prev == *++scan &&
1773
prev == *++scan && prev == *++scan &&
1775
s->match_length = MAX_MATCH - (int)(strend - scan);
1776
if (s->match_length > s->lookahead)
1777
s->match_length = s->lookahead;
1718
1781
/* Emit match if have run of MIN_MATCH or longer, else emit literal */
1719
if (run >= MIN_MATCH) {
1720
check_match(s, s->strstart, s->strstart - 1, run);
1721
_tr_tally_dist(s, 1, run - MIN_MATCH, bflush);
1722
s->lookahead -= run;
1782
if (s->match_length >= MIN_MATCH) {
1783
check_match(s, s->strstart, s->strstart - 1, s->match_length);
1785
_tr_tally_dist(s, 1, s->match_length - MIN_MATCH, bflush);
1787
s->lookahead -= s->match_length;
1788
s->strstart += s->match_length;
1789
s->match_length = 0;
1725
1791
/* No match, output a literal byte */
1726
1792
Tracevv((stderr,"%c", s->window[s->strstart]));
1733
1799
FLUSH_BLOCK(s, flush == Z_FINISH);
1734
1800
return flush == Z_FINISH ? finish_done : block_done;
1803
/* ===========================================================================
1804
* For Z_HUFFMAN_ONLY, do not look for matches. Do not maintain a hash table.
1805
* (It will be regenerated if this run of deflate switches away from Huffman.)
1807
local block_state deflate_huff(s, flush)
1811
int bflush; /* set if current block must be flushed */
1814
/* Make sure that we have a literal to write. */
1815
if (s->lookahead == 0) {
1817
if (s->lookahead == 0) {
1818
if (flush == Z_NO_FLUSH)
1820
break; /* flush the current block */
1824
/* Output a literal byte */
1825
s->match_length = 0;
1826
Tracevv((stderr,"%c", s->window[s->strstart]));
1827
_tr_tally_lit (s, s->window[s->strstart], bflush);
1830
if (bflush) FLUSH_BLOCK(s, 0);
1832
FLUSH_BLOCK(s, flush == Z_FINISH);
1833
return flush == Z_FINISH ? finish_done : block_done;