55 " deflate 1.2.13 Copyright 1995-2022 Jean-loup Gailly and Mark Adler ";
105 # define TOO_FAR 4096
149 #define RANK(f) (((f) * 2) - ((f) > 4 ? 9 : 0))
157 #define UPDATE_HASH(s,h,c) (h = (((h) << s->hash_shift) ^ (c)) & s->hash_mask)
171 #define INSERT_STRING(s, str, match_head) \
172 (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
173 match_head = s->head[s->ins_h], \
174 s->head[s->ins_h] = (Pos)(str))
176 #define INSERT_STRING(s, str, match_head) \
177 (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
178 match_head = s->prev[(str) & s->w_mask] = s->head[s->ins_h], \
179 s->head[s->ins_h] = (Pos)(str))
186 #define CLEAR_HASH(s) \
188 s->head[s->hash_size - 1] = NIL; \
189 zmemzero((Bytef *)s->head, \
190 (unsigned)(s->hash_size - 1)*sizeof(*s->head)); \
203 uInt wsize = s->w_size;
209 *
p = (
Pos)(
m >= wsize ?
m - wsize :
NIL);
216 *
p = (
Pos)(
m >= wsize ?
m - wsize :
NIL);
259 if (strm->zalloc == (alloc_func)0) {
267 if (strm->zfree == (free_func)0)
280 if (windowBits < 0) {
282 if (windowBits < -15)
284 windowBits = -windowBits;
287 else if (windowBits > 15) {
293 windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
294 strategy < 0 || strategy >
Z_FIXED || (windowBits == 8 && wrap != 1)) {
297 if (windowBits == 8) windowBits = 9;
306 s->w_bits = (
uInt)windowBits;
307 s->w_size = 1 << s->w_bits;
308 s->w_mask = s->w_size - 1;
310 s->hash_bits = (
uInt)memLevel + 7;
311 s->hash_size = 1 << s->hash_bits;
312 s->hash_mask = s->hash_size - 1;
321 s->lit_bufsize = 1 << (memLevel + 6);
363 s->pending_buf_size = (
ulg)s->lit_bufsize * 4;
366 s->pending_buf ==
Z_NULL) {
372 s->sym_buf = s->pending_buf + s->lit_bufsize;
373 s->sym_end = (s->lit_bufsize - 1) * 3;
394 strm->zalloc == (alloc_func)0 ||
strm->zfree == (free_func)0)
436 if (dictLength >= s->w_size) {
444 dictLength = s->w_size;
448 avail =
strm->avail_in;
450 strm->avail_in = dictLength;
459 s->prev[
str & s->w_mask] = s->head[s->ins_h];
461 s->head[s->ins_h] = (
Pos)
str;
468 s->strstart += s->lookahead;
469 s->block_start = (
long)s->strstart;
470 s->insert = s->lookahead;
472 s->match_length = s->prev_length =
MIN_MATCH-1;
473 s->match_available = 0;
475 strm->avail_in = avail;
492 len = s->strstart + s->lookahead;
512 strm->total_in =
strm->total_out = 0;
518 s->pending_out = s->pending_buf;
588 if (bits < 0 || bits > 16 ||
589 s->sym_buf < s->pending_out + ((
Buf_size + 7) >> 3))
595 s->bi_buf |= (
ush)((
value & ((1 << put) - 1)) << s->bi_valid);
621 if (level < 0 || level > 9 || strategy < 0 || strategy >
Z_FIXED) {
627 s->last_flush != -2) {
632 if (
strm->avail_in || (s->strstart - s->block_start) + s->lookahead)
635 if (s->level !=
level) {
636 if (s->level == 0 && s->matches != 0) {
665 s->good_match = (
uInt)good_length;
666 s->max_lazy_match = (
uInt)max_lazy;
667 s->nice_match = nice_length;
668 s->max_chain_length = (
uInt)max_chain;
701 uLong fixedlen, storelen, wraplen;
706 fixedlen = sourceLen + (sourceLen >> 3) + (sourceLen >> 8) +
707 (sourceLen >> 9) + 4;
711 storelen = sourceLen + (sourceLen >> 5) + (sourceLen >> 7) +
712 (sourceLen >> 11) + 7;
716 return (fixedlen > storelen ? fixedlen : storelen) + 6;
725 wraplen = 6 + (s->strstart ? 4 : 0);
730 if (s->gzhead !=
Z_NULL) {
732 if (s->gzhead->extra !=
Z_NULL)
733 wraplen += 2 + s->gzhead->extra_len;
734 str = s->gzhead->name;
739 str = s->gzhead->comment;
754 if (s->w_bits != 15 || s->hash_bits != 8 + 7)
755 return (s->w_bits <= s->hash_bits ? fixedlen : storelen) + wraplen;
759 return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) +
760 (sourceLen >> 25) + 13 - 6 + wraplen;
791 if (
len == 0)
return;
795 s->pending_out +=
len;
799 if (s->pending == 0) {
800 s->pending_out = s->pending_buf;
807 #define HCRC_UPDATE(beg) \
809 if (s->gzhead->hcrc && s->pending > (beg)) \
810 strm->adler = crc32(strm->adler, s->pending_buf + (beg), \
811 s->pending - (beg)); \
834 old_flush = s->last_flush;
835 s->last_flush = flush;
838 if (s->pending != 0) {
840 if (
strm->avail_out == 0) {
855 }
else if (
strm->avail_in == 0 &&
RANK(flush) <=
RANK(old_flush) &&
875 else if (s->level < 6)
877 else if (s->level == 6)
881 header |= (level_flags << 6);
888 if (s->strstart != 0) {
897 if (s->pending != 0) {
909 if (s->gzhead ==
Z_NULL) {
923 if (s->pending != 0) {
929 put_byte(s, (s->gzhead->text ? 1 : 0) +
930 (s->gzhead->hcrc ? 2 : 0) +
931 (s->gzhead->extra ==
Z_NULL ? 0 : 4) +
932 (s->gzhead->name ==
Z_NULL ? 0 : 8) +
933 (s->gzhead->comment ==
Z_NULL ? 0 : 16)
943 if (s->gzhead->extra !=
Z_NULL) {
944 put_byte(s, s->gzhead->extra_len & 0xff);
945 put_byte(s, (s->gzhead->extra_len >> 8) & 0xff);
955 if (s->gzhead->extra !=
Z_NULL) {
956 ulg beg = s->pending;
957 uInt left = (s->gzhead->extra_len & 0xffff) - s->gzindex;
958 while (s->pending + left > s->pending_buf_size) {
959 uInt copy = s->pending_buf_size - s->pending;
960 zmemcpy(s->pending_buf + s->pending,
961 s->gzhead->extra + s->gzindex,
copy);
962 s->pending = s->pending_buf_size;
966 if (s->pending != 0) {
973 zmemcpy(s->pending_buf + s->pending,
974 s->gzhead->extra + s->gzindex, left);
982 if (s->gzhead->name !=
Z_NULL) {
983 ulg beg = s->pending;
986 if (s->pending == s->pending_buf_size) {
989 if (s->pending != 0) {
995 val = s->gzhead->name[s->gzindex++];
1004 if (s->gzhead->comment !=
Z_NULL) {
1005 ulg beg = s->pending;
1008 if (s->pending == s->pending_buf_size) {
1011 if (s->pending != 0) {
1017 val = s->gzhead->comment[s->gzindex++];
1025 if (s->gzhead->hcrc) {
1026 if (s->pending + 2 > s->pending_buf_size) {
1028 if (s->pending != 0) {
1041 if (s->pending != 0) {
1050 if (
strm->avail_in != 0 || s->lookahead != 0 ||
1063 if (
strm->avail_out == 0) {
1078 }
else if (flush !=
Z_BLOCK) {
1085 if (s->lookahead == 0) {
1087 s->block_start = 0
L;
1093 if (
strm->avail_out == 0) {
1125 if (s->wrap > 0) s->wrap = -s->wrap;
1187 ds->pending_buf ==
Z_NULL) {
1192 zmemcpy(ds->window, ss->window, ds->w_size * 2 *
sizeof(
Byte));
1195 zmemcpy(ds->pending_buf, ss->pending_buf, (
uInt)ds->pending_buf_size);
1197 ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf);
1198 ds->sym_buf = ds->pending_buf + ds->lit_bufsize;
1200 ds->l_desc.dyn_tree = ds->dyn_ltree;
1201 ds->d_desc.dyn_tree = ds->dyn_dtree;
1202 ds->bl_desc.dyn_tree = ds->bl_tree;
1220 unsigned len =
strm->avail_in;
1223 if (
len == 0)
return 0;
1228 if (
strm->state->wrap == 1) {
1232 else if (
strm->state->wrap == 2) {
1248 s->window_size = (
ulg)2
L*s->w_size;
1260 s->block_start = 0
L;
1263 s->match_length = s->prev_length =
MIN_MATCH-1;
1264 s->match_available = 0;
1282 unsigned chain_length = s->max_chain_length;
1283 register Bytef *scan = s->window + s->strstart;
1286 int best_len = (
int)s->prev_length;
1294 uInt wmask = s->w_mask;
1300 register Bytef *strend = s->window + s->strstart +
MAX_MATCH - 1;
1301 register ush scan_start = *(
ushf*)scan;
1302 register ush scan_end = *(
ushf*)(scan + best_len - 1);
1305 register Byte scan_end1 = scan[best_len - 1];
1306 register Byte scan_end = scan[best_len];
1315 if (s->prev_length >= s->good_match) {
1328 match = s->window + cur_match;
1338 #if (defined(UNALIGNED_OK) && MAX_MATCH == 258)
1342 if (*(
ushf*)(
match + best_len - 1) != scan_end ||
1365 Assert(scan <= s->
window + (
unsigned)(s->window_size - 1),
1367 if (*scan == *
match) scan++;
1374 if (
match[best_len] != scan_end ||
1375 match[best_len - 1] != scan_end1 ||
1377 *++
match != scan[1])
continue;
1392 }
while (*++scan == *++
match && *++scan == *++
match &&
1393 *++scan == *++
match && *++scan == *++
match &&
1394 *++scan == *++
match && *++scan == *++
match &&
1395 *++scan == *++
match && *++scan == *++
match &&
1398 Assert(scan <= s->
window + (
unsigned)(s->window_size - 1),
1406 if (
len > best_len) {
1407 s->match_start = cur_match;
1411 scan_end = *(
ushf*)(scan + best_len - 1);
1413 scan_end1 = scan[best_len - 1];
1414 scan_end = scan[best_len];
1417 }
while ((cur_match =
prev[cur_match & wmask]) > limit
1418 && --chain_length != 0);
1420 if ((
uInt)best_len <= s->lookahead)
return (
uInt)best_len;
1421 return s->lookahead;
1433 register Bytef *scan =
s->window +
s->strstart;
1448 match =
s->window + cur_match;
1460 scan += 2,
match += 2;
1467 }
while (*++scan == *++
match && *++scan == *++
match &&
1468 *++scan == *++
match && *++scan == *++
match &&
1469 *++scan == *++
match && *++scan == *++
match &&
1470 *++scan == *++
match && *++scan == *++
match &&
1473 Assert(scan <= s->
window + (
unsigned)(
s->window_size - 1),
"wild scan");
1479 s->match_start = cur_match;
1501 fprintf(
stderr,
" start %u, match %u, length %d\n",
1506 z_error(
"invalid match");
1508 if (z_verbose > 1) {
1514 # define check_match(s, start, match, length)
1532 uInt wsize = s->w_size;
1537 more = (unsigned)(s->window_size -(
ulg)s->lookahead -(
ulg)s->strstart);
1540 if (
sizeof(
int) <= 2) {
1541 if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
1544 }
else if (more == (
unsigned)(-1)) {
1555 if (s->strstart >= wsize +
MAX_DIST(s)) {
1557 zmemcpy(s->window, s->window + wsize, (
unsigned)wsize - more);
1558 s->match_start -= wsize;
1559 s->strstart -= wsize;
1560 s->block_start -= (
long) wsize;
1561 if (s->insert > s->strstart)
1562 s->insert = s->strstart;
1566 if (s->strm->avail_in == 0)
break;
1579 Assert(more >= 2,
"more < 2");
1581 n =
read_buf(s->strm, s->window + s->strstart + s->lookahead, more);
1585 if (s->lookahead + s->insert >=
MIN_MATCH) {
1586 uInt str = s->strstart - s->insert;
1587 s->ins_h = s->window[
str];
1595 s->prev[
str & s->w_mask] = s->head[s->ins_h];
1597 s->head[s->ins_h] = (
Pos)
str;
1600 if (s->lookahead + s->insert <
MIN_MATCH)
1608 }
while (s->lookahead <
MIN_LOOKAHEAD && s->strm->avail_in != 0);
1617 if (s->high_water < s->window_size) {
1618 ulg curr = s->strstart + (
ulg)(s->lookahead);
1621 if (s->high_water < curr) {
1625 init = s->window_size - curr;
1629 s->high_water = curr +
init;
1637 if (
init > s->window_size - s->high_water)
1638 init = s->window_size - s->high_water;
1640 s->high_water +=
init;
1645 "not enough room for search");
1652 #define FLUSH_BLOCK_ONLY(s, last) { \
1653 _tr_flush_block(s, (s->block_start >= 0L ? \
1654 (charf *)&s->window[(unsigned)s->block_start] : \
1656 (ulg)((long)s->strstart - s->block_start), \
1658 s->block_start = s->strstart; \
1659 flush_pending(s->strm); \
1660 Tracev((stderr,"[FLUSH]")); \
1664 #define FLUSH_BLOCK(s, last) { \
1665 FLUSH_BLOCK_ONLY(s, last); \
1666 if (s->strm->avail_out == 0) return (last) ? finish_started : need_more; \
1670 #define MAX_STORED 65535
1673 #define MIN(a, b) ((a) > (b) ? (b) : (a))
1698 unsigned min_block =
MIN(s->pending_buf_size - 5, s->w_size);
1704 unsigned len, left, have, last = 0;
1705 unsigned used = s->strm->avail_in;
1712 have = (s->bi_valid + 42) >> 3;
1713 if (s->strm->avail_out < have)
1716 have = s->strm->avail_out - have;
1717 left = s->strstart - s->block_start;
1718 if (
len > (
ulg)left + s->strm->avail_in)
1719 len = left + s->strm->avail_in;
1730 len != left + s->strm->avail_in))
1736 last = flush ==
Z_FINISH &&
len == left + s->strm->avail_in ? 1 : 0;
1740 s->pending_buf[s->pending - 4] =
len;
1741 s->pending_buf[s->pending - 3] =
len >> 8;
1742 s->pending_buf[s->pending - 2] = ~
len;
1743 s->pending_buf[s->pending - 1] = ~
len >> 8;
1750 s->compressed_len +=
len << 3;
1751 s->bits_sent +=
len << 3;
1758 zmemcpy(s->strm->next_out, s->window + s->block_start, left);
1759 s->strm->next_out += left;
1760 s->strm->avail_out -= left;
1761 s->strm->total_out += left;
1762 s->block_start += left;
1771 s->strm->next_out +=
len;
1772 s->strm->avail_out -=
len;
1773 s->strm->total_out +=
len;
1775 }
while (last == 0);
1783 used -= s->strm->avail_in;
1788 if (used >= s->w_size) {
1790 zmemcpy(s->window, s->strm->next_in - s->w_size, s->w_size);
1791 s->strstart = s->w_size;
1792 s->insert = s->strstart;
1795 if (s->window_size - s->strstart <= used) {
1797 s->strstart -= s->w_size;
1798 zmemcpy(s->window, s->window + s->w_size, s->strstart);
1801 if (s->insert > s->strstart)
1802 s->insert = s->strstart;
1804 zmemcpy(s->window + s->strstart, s->strm->next_in - used, used);
1805 s->strstart += used;
1806 s->insert +=
MIN(used, s->w_size - s->insert);
1808 s->block_start = s->strstart;
1810 if (s->high_water < s->strstart)
1811 s->high_water = s->strstart;
1819 s->strm->avail_in == 0 && (
long)s->strstart == s->block_start)
1823 have = s->window_size - s->strstart;
1824 if (s->strm->avail_in > have && s->block_start >= (
long)s->w_size) {
1826 s->block_start -= s->w_size;
1827 s->strstart -= s->w_size;
1828 zmemcpy(s->window, s->window + s->w_size, s->strstart);
1832 if (s->insert > s->strstart)
1833 s->insert = s->strstart;
1835 if (have > s->strm->avail_in)
1836 have = s->strm->avail_in;
1838 read_buf(s->strm, s->window + s->strstart, have);
1839 s->strstart += have;
1840 s->insert +=
MIN(have, s->w_size - s->insert);
1842 if (s->high_water < s->strstart)
1843 s->high_water = s->strstart;
1850 have = (s->bi_valid + 42) >> 3;
1853 min_block =
MIN(have, s->w_size);
1854 left = s->strstart - s->block_start;
1855 if (left >= min_block ||
1857 s->strm->avail_in == 0 && left <= have)) {
1859 last = flush ==
Z_FINISH && s->strm->avail_in == 0 &&
1860 len == left ? 1 : 0;
1862 s->block_start +=
len;
1895 if (s->lookahead == 0)
break;
1909 if (hash_head !=
NIL && s->strstart - hash_head <=
MAX_DIST(s)) {
1918 check_match(s, s->strstart, s->match_start, s->match_length);
1923 s->lookahead -= s->match_length;
1929 if (s->match_length <= s->max_insert_length &&
1938 }
while (--s->match_length != 0);
1943 s->strstart += s->match_length;
1944 s->match_length = 0;
1945 s->ins_h = s->window[s->strstart];
1946 UPDATE_HASH(s, s->ins_h, s->window[s->strstart + 1]);
1998 if (s->lookahead == 0)
break;
2011 s->prev_length = s->match_length, s->prev_match = s->match_start;
2014 if (hash_head !=
NIL && s->prev_length < s->max_lazy_match &&
2015 s->strstart - hash_head <=
MAX_DIST(s)) {
2023 if (s->match_length <= 5 && (s->strategy ==
Z_FILTERED
2026 s->strstart - s->match_start >
TOO_FAR)
2039 if (s->prev_length >=
MIN_MATCH && s->match_length <= s->prev_length) {
2043 check_match(s, s->strstart - 1, s->prev_match, s->prev_length);
2053 s->lookahead -= s->prev_length - 1;
2054 s->prev_length -= 2;
2056 if (++s->strstart <= max_insert) {
2059 }
while (--s->prev_length != 0);
2060 s->match_available = 0;
2066 }
else if (s->match_available) {
2078 if (s->strm->avail_out == 0)
return need_more;
2083 s->match_available = 1;
2089 if (s->match_available) {
2092 s->match_available = 0;
2116 Bytef *scan, *strend;
2128 if (s->lookahead == 0)
break;
2132 s->match_length = 0;
2133 if (s->lookahead >=
MIN_MATCH && s->strstart > 0) {
2134 scan = s->window + s->strstart - 1;
2136 if (
prev == *++scan &&
prev == *++scan &&
prev == *++scan) {
2137 strend = s->window + s->strstart +
MAX_MATCH;
2139 }
while (
prev == *++scan &&
prev == *++scan &&
2140 prev == *++scan &&
prev == *++scan &&
2141 prev == *++scan &&
prev == *++scan &&
2142 prev == *++scan &&
prev == *++scan &&
2145 if (s->match_length > s->lookahead)
2146 s->match_length = s->lookahead;
2154 check_match(s, s->strstart, s->strstart - 1, s->match_length);
2158 s->lookahead -= s->match_length;
2159 s->strstart += s->match_length;
2160 s->match_length = 0;
2192 if (s->lookahead == 0) {
2194 if (s->lookahead == 0) {
2202 s->match_length = 0;