54 " deflate 1.2.12 Copyright 1995-2022 Jean-loup Gailly and Mark Adler ";
90# pragma message("Assembler code may have bugs -- use at your own risk")
91 void match_init
OF((
void));
154#define RANK(f) (((f) * 2) - ((f) > 4 ? 9 : 0))
162#define UPDATE_HASH(s,h,c) (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask)
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->head[s->ins_h], \
179 s->head[s->ins_h] = (Pos)(str))
181#define INSERT_STRING(s, str, match_head) \
182 (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
183 match_head = s->prev[(str) & s->w_mask] = s->head[s->ins_h], \
184 s->head[s->ins_h] = (Pos)(str))
191#define CLEAR_HASH(s) \
193 s->head[s->hash_size-1] = NIL; \
194 zmemzero((Bytef *)s->head, \
195 (unsigned)(s->hash_size-1)*sizeof(*s->head)); \
208 uInt wsize = s->w_size;
214 *p = (
Pos)(m >= wsize ? m - wsize :
NIL);
221 *p = (
Pos)(m >= wsize ? m - wsize :
NIL);
242int ZEXPORT
deflateInit2_(strm, level, method, windowBits, memLevel, strategy,
243 version, stream_size)
257 if (version ==
Z_NULL || version[0] != my_version[0] ||
264 if (strm->zalloc == (alloc_func)0) {
269 strm->opaque = (voidpf)0;
272 if (strm->zfree == (free_func)0)
280 if (level != 0) level = 1;
285 if (windowBits < 0) {
287 windowBits = -windowBits;
290 else if (windowBits > 15) {
295 if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method !=
Z_DEFLATED ||
296 windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
297 strategy < 0 || strategy >
Z_FIXED || (windowBits == 8 && wrap != 1)) {
300 if (windowBits == 8) windowBits = 9;
309 s->
w_bits = (uInt)windowBits;
401 strm->zalloc == (alloc_func)0 ||
strm->zfree == (free_func)0)
421 const Bytef *dictionary;
428 z_const
unsigned char *next;
443 if (dictLength >= s->
w_size) {
450 dictionary += dictLength - s->
w_size;
455 avail =
strm->avail_in;
456 next =
strm->next_in;
457 strm->avail_in = dictLength;
458 strm->next_in = (z_const Bytef *)dictionary;
481 strm->next_in = next;
482 strm->avail_in = avail;
502 if (dictionary !=
Z_NULL && len)
519 strm->total_in =
strm->total_out = 0;
580 *bits =
strm->state->bi_valid;
595 if (bits < 0 || bits > 16 ||
628 if (level < 0 || level > 9 || strategy < 0 || strategy >
Z_FIXED) {
701 uLong complen, wraplen;
704 complen = sourceLen +
705 ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 5;
718 wraplen = 6 + (s->
strstart ? 4 : 0);
726 wraplen += 2 + s->
gzhead->extra_len;
748 return complen + wraplen;
751 return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) +
752 (sourceLen >> 25) + 13 - 6 + wraplen;
782 if (len >
strm->avail_out) len =
strm->avail_out;
783 if (len == 0)
return;
786 strm->next_out += len;
788 strm->total_out += len;
789 strm->avail_out -= len;
799#define HCRC_UPDATE(beg) \
801 if (s->gzhead->hcrc && s->pending > (beg)) \
802 strm->adler = crc32(strm->adler, s->pending_buf + (beg), \
803 s->pending - (beg)); \
832 if (
strm->avail_out == 0) {
847 }
else if (
strm->avail_in == 0 &&
RANK(flush) <=
RANK(old_flush) &&
867 else if (s->
level < 6)
869 else if (s->
level == 6)
873 header |= (level_flags << 6);
875 header += 31 - (header % 31);
922 (s->
gzhead->hcrc ? 2 : 0) +
1055 if (
strm->avail_out == 0) {
1070 }
else if (flush !=
Z_BLOCK) {
1085 if (
strm->avail_out == 0) {
1212 unsigned len =
strm->avail_in;
1214 if (len > size) len = size;
1215 if (len == 0)
return 0;
1217 strm->avail_in -= len;
1220 if (
strm->state->wrap == 1) {
1224 else if (
strm->state->wrap == 2) {
1228 strm->next_in += len;
1229 strm->total_in += len;
1252 s->block_start = 0L;
1255 s->match_length = s->prev_length =
MIN_MATCH-1;
1256 s->match_available = 0;
1284 register Bytef *scan = s->window + s->strstart;
1285 register Bytef *match;
1287 int best_len = (int)s->prev_length;
1295 uInt wmask = s->w_mask;
1301 register Bytef *strend = s->window + s->strstart +
MAX_MATCH - 1;
1302 register ush scan_start = *(
ushf*)scan;
1303 register ush scan_end = *(
ushf*)(scan+best_len-1);
1305 register Bytef *strend = s->window + s->strstart +
MAX_MATCH;
1306 register Byte scan_end1 = scan[best_len-1];
1307 register Byte scan_end = scan[best_len];
1316 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 ||
1343 *(
ushf*)match != scan_start)
continue;
1354 Assert(scan[2] == match[2],
"scan[2]?");
1357 }
while (*(
ushf*)(scan+=2) == *(
ushf*)(match+=2) &&
1358 *(
ushf*)(scan+=2) == *(
ushf*)(match+=2) &&
1359 *(
ushf*)(scan+=2) == *(
ushf*)(match+=2) &&
1360 *(
ushf*)(scan+=2) == *(
ushf*)(match+=2) &&
1365 Assert(scan <= s->
window+(
unsigned)(s->window_size-1),
"wild scan");
1366 if (*scan == *match) scan++;
1368 len = (
MAX_MATCH - 1) - (
int)(strend-scan);
1373 if (match[best_len] != scan_end ||
1374 match[best_len-1] != scan_end1 ||
1376 *++match != scan[1])
continue;
1385 Assert(*scan == *match,
"match[2]?");
1391 }
while (*++scan == *++match && *++scan == *++match &&
1392 *++scan == *++match && *++scan == *++match &&
1393 *++scan == *++match && *++scan == *++match &&
1394 *++scan == *++match && *++scan == *++match &&
1397 Assert(scan <= s->
window+(
unsigned)(s->window_size-1),
"wild scan");
1404 if (len > best_len) {
1405 s->match_start = cur_match;
1409 scan_end = *(
ushf*)(scan+best_len-1);
1411 scan_end1 = scan[best_len-1];
1412 scan_end = scan[best_len];
1415 }
while ((cur_match =
prev[cur_match & wmask]) > limit
1416 && --chain_length != 0);
1418 if ((uInt)best_len <= s->lookahead)
return (uInt)best_len;
1419 return s->lookahead;
1432 register Bytef *scan = s->
window + s->strstart;
1433 register Bytef *match;
1435 register Bytef *strend = s->window + s->strstart +
MAX_MATCH;
1446 match = s->window + cur_match;
1450 if (match[0] != scan[0] || match[1] != scan[1])
return MIN_MATCH-1;
1458 scan += 2, match += 2;
1459 Assert(*scan == *match,
"match[2]?");
1465 }
while (*++scan == *++match && *++scan == *++match &&
1466 *++scan == *++match && *++scan == *++match &&
1467 *++scan == *++match && *++scan == *++match &&
1468 *++scan == *++match && *++scan == *++match &&
1471 Assert(scan <= s->
window+(
unsigned)(s->window_size-1),
"wild scan");
1477 s->match_start = cur_match;
1478 return (uInt)len <= s->lookahead ? (uInt)len : s->lookahead;
1497 if (
zmemcmp(s->window + match,
1498 s->window + start, length) != EQUAL) {
1499 fprintf(stderr,
" start %u, match %u, length %d\n",
1500 start, match, length);
1502 fprintf(stderr,
"%c%c", s->window[match++], s->window[start++]);
1503 }
while (--length != 0);
1504 z_error(
"invalid match");
1506 if (z_verbose > 1) {
1507 fprintf(stderr,
"\\[%d,%d]", start-match, length);
1508 do { putc(s->window[start++], stderr); }
while (--length != 0);
1512# define check_match(s, start, match, length)
1530 uInt wsize = s->w_size;
1535 more = (unsigned)(s->window_size -(
ulg)s->lookahead -(
ulg)s->strstart);
1538 if (
sizeof(
int) <= 2) {
1539 if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
1542 }
else if (more == (
unsigned)(-1)) {
1553 if (s->strstart >= wsize+
MAX_DIST(s)) {
1555 zmemcpy(s->window, s->window+wsize, (
unsigned)wsize - more);
1556 s->match_start -= wsize;
1557 s->strstart -= wsize;
1558 s->block_start -= (long) wsize;
1559 if (s->insert > s->strstart)
1560 s->insert = s->strstart;
1564 if (s->strm->avail_in == 0)
break;
1577 Assert(more >= 2,
"more < 2");
1579 n =
read_buf(s->strm, s->window + s->strstart + s->lookahead, more);
1583 if (s->lookahead + s->insert >=
MIN_MATCH) {
1584 uInt str = s->strstart - s->insert;
1585 s->ins_h = s->window[str];
1593 s->prev[str & s->w_mask] = s->head[s->ins_h];
1595 s->head[s->ins_h] = (
Pos)str;
1598 if (s->lookahead + s->insert <
MIN_MATCH)
1606 }
while (s->lookahead <
MIN_LOOKAHEAD && s->strm->avail_in != 0);
1615 if (s->high_water < s->window_size) {
1616 ulg curr = s->strstart + (
ulg)(s->lookahead);
1619 if (s->high_water < curr) {
1623 init = s->window_size - curr;
1626 zmemzero(s->window + curr, (
unsigned)init);
1627 s->high_water = curr + init;
1635 if (init > s->window_size - s->high_water)
1636 init = s->window_size - s->high_water;
1637 zmemzero(s->window + s->high_water, (
unsigned)init);
1638 s->high_water += init;
1643 "not enough room for search");
1650#define FLUSH_BLOCK_ONLY(s, last) { \
1651 _tr_flush_block(s, (s->block_start >= 0L ? \
1652 (charf *)&s->window[(unsigned)s->block_start] : \
1654 (ulg)((long)s->strstart - s->block_start), \
1656 s->block_start = s->strstart; \
1657 flush_pending(s->strm); \
1658 Tracev((stderr,"[FLUSH]")); \
1662#define FLUSH_BLOCK(s, last) { \
1663 FLUSH_BLOCK_ONLY(s, last); \
1664 if (s->strm->avail_out == 0) return (last) ? finish_started : need_more; \
1668#define MAX_STORED 65535
1671#define MIN(a, b) ((a) > (b) ? (b) : (a))
1696 unsigned min_block =
MIN(s->pending_buf_size - 5, s->w_size);
1702 unsigned len, left, have, last = 0;
1703 unsigned used = s->strm->avail_in;
1710 have = (s->bi_valid + 42) >> 3;
1711 if (s->strm->avail_out < have)
1714 have = s->strm->avail_out - have;
1715 left = s->strstart - s->block_start;
1716 if (len > (
ulg)left + s->strm->avail_in)
1717 len = left + s->strm->avail_in;
1726 if (len < min_block && ((len == 0 && flush !=
Z_FINISH) ||
1728 len != left + s->strm->avail_in))
1734 last = flush ==
Z_FINISH && len == left + s->strm->avail_in ? 1 : 0;
1738 s->pending_buf[s->pending - 4] = len;
1739 s->pending_buf[s->pending - 3] = len >> 8;
1740 s->pending_buf[s->pending - 2] = ~len;
1741 s->pending_buf[s->pending - 1] = ~len >> 8;
1748 s->compressed_len += len << 3;
1749 s->bits_sent += len << 3;
1756 zmemcpy(s->strm->next_out, s->window + s->block_start, left);
1757 s->strm->next_out += left;
1758 s->strm->avail_out -= left;
1759 s->strm->total_out += left;
1760 s->block_start += left;
1768 read_buf(s->strm, s->strm->next_out, len);
1769 s->strm->next_out += len;
1770 s->strm->avail_out -= len;
1771 s->strm->total_out += len;
1773 }
while (last == 0);
1781 used -= s->strm->avail_in;
1786 if (used >= s->w_size) {
1788 zmemcpy(s->window, s->strm->next_in - s->w_size, s->w_size);
1789 s->strstart = s->w_size;
1790 s->insert = s->strstart;
1793 if (s->window_size - s->strstart <= used) {
1795 s->strstart -= s->w_size;
1796 zmemcpy(s->window, s->window + s->w_size, s->strstart);
1799 if (s->insert > s->strstart)
1800 s->insert = s->strstart;
1802 zmemcpy(s->window + s->strstart, s->strm->next_in - used, used);
1803 s->strstart += used;
1804 s->insert +=
MIN(used, s->w_size - s->insert);
1806 s->block_start = s->strstart;
1808 if (s->high_water < s->strstart)
1809 s->high_water = s->strstart;
1817 s->strm->avail_in == 0 && (
long)s->strstart == s->block_start)
1821 have = s->window_size - s->strstart;
1822 if (s->strm->avail_in > have && s->block_start >= (
long)s->w_size) {
1824 s->block_start -= s->w_size;
1825 s->strstart -= s->w_size;
1826 zmemcpy(s->window, s->window + s->w_size, s->strstart);
1830 if (s->insert > s->strstart)
1831 s->insert = s->strstart;
1833 if (have > s->strm->avail_in)
1834 have = s->strm->avail_in;
1836 read_buf(s->strm, s->window + s->strstart, have);
1837 s->strstart += have;
1838 s->insert +=
MIN(have, s->w_size - s->insert);
1840 if (s->high_water < s->strstart)
1841 s->high_water = s->strstart;
1848 have = (s->bi_valid + 42) >> 3;
1851 min_block =
MIN(have, s->w_size);
1852 left = s->strstart - s->block_start;
1853 if (left >= min_block ||
1855 s->strm->avail_in == 0 && left <= have)) {
1856 len =
MIN(left, have);
1857 last = flush ==
Z_FINISH && s->strm->avail_in == 0 &&
1858 len == left ? 1 : 0;
1860 s->block_start += len;
1893 if (s->lookahead == 0)
break;
1907 if (hash_head !=
NIL && s->strstart - hash_head <=
MAX_DIST(s)) {
1916 check_match(s, s->strstart, s->match_start, s->match_length);
1921 s->lookahead -= s->match_length;
1927 if (s->match_length <= s->max_insert_length &&
1936 }
while (--s->match_length != 0);
1941 s->strstart += s->match_length;
1942 s->match_length = 0;
1943 s->ins_h = s->window[s->strstart];
1944 UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
1954 Tracevv((stderr,
"%c", s->window[s->strstart]));
1996 if (s->lookahead == 0)
break;
2009 s->prev_length = s->match_length, s->prev_match = s->match_start;
2012 if (hash_head !=
NIL && s->prev_length < s->max_lazy_match &&
2013 s->strstart - hash_head <=
MAX_DIST(s)) {
2021 if (s->match_length <= 5 && (s->strategy ==
Z_FILTERED
2024 s->strstart - s->match_start >
TOO_FAR)
2037 if (s->prev_length >=
MIN_MATCH && s->match_length <= s->prev_length) {
2038 uInt max_insert = s->strstart + s->lookahead -
MIN_MATCH;
2041 check_match(s, s->strstart-1, s->prev_match, s->prev_length);
2051 s->lookahead -= s->prev_length-1;
2052 s->prev_length -= 2;
2054 if (++s->strstart <= max_insert) {
2057 }
while (--s->prev_length != 0);
2058 s->match_available = 0;
2064 }
else if (s->match_available) {
2069 Tracevv((stderr,
"%c", s->window[s->strstart-1]));
2076 if (s->strm->avail_out == 0)
return need_more;
2081 s->match_available = 1;
2087 if (s->match_available) {
2088 Tracevv((stderr,
"%c", s->window[s->strstart-1]));
2090 s->match_available = 0;
2114 Bytef *scan, *strend;
2126 if (s->lookahead == 0)
break;
2130 s->match_length = 0;
2131 if (s->lookahead >=
MIN_MATCH && s->strstart > 0) {
2132 scan = s->window + s->strstart - 1;
2134 if (
prev == *++scan &&
prev == *++scan &&
prev == *++scan) {
2135 strend = s->window + s->strstart +
MAX_MATCH;
2137 }
while (
prev == *++scan &&
prev == *++scan &&
2138 prev == *++scan &&
prev == *++scan &&
2139 prev == *++scan &&
prev == *++scan &&
2140 prev == *++scan &&
prev == *++scan &&
2142 s->match_length =
MAX_MATCH - (uInt)(strend - scan);
2143 if (s->match_length > s->lookahead)
2144 s->match_length = s->lookahead;
2146 Assert(scan <= s->
window+(uInt)(s->window_size-1),
"wild scan");
2151 check_match(s, s->strstart, s->strstart - 1, s->match_length);
2155 s->lookahead -= s->match_length;
2156 s->strstart += s->match_length;
2157 s->match_length = 0;
2160 Tracevv((stderr,
"%c", s->window[s->strstart]));
2189 if (s->lookahead == 0) {
2191 if (s->lookahead == 0) {
2199 s->match_length = 0;
2200 Tracevv((stderr,
"%c", s->window[s->strstart]));
uLong ZEXPORT adler32(uLong adler, const Bytef *buf, uInt len)
unsigned long ZEXPORT crc32(unsigned long crc, const unsigned char FAR *buf, uInt len)
const config configuration_table[10]
int ZEXPORT deflateSetHeader(z_streamp strm, gz_headerp head)
int ZEXPORT deflateInit_(z_streamp strm, int level, const char *version, int stream_size)
#define FLUSH_BLOCK_ONLY(s, last)
int ZEXPORT deflatePending(z_streamp strm, unsigned *pending, int *bits)
block_state deflate_stored(deflate_state *s, int flush)
#define check_match(s, start, match, length)
void putShortMSB(deflate_state *s, uInt b)
#define UPDATE_HASH(s, h, c)
int ZEXPORT deflateCopy(z_streamp dest, z_streamp source)
int ZEXPORT deflateSetDictionary(z_streamp strm, const Bytef *dictionary, uInt dictLength)
void flush_pending(z_streamp strm)
int ZEXPORT deflateReset(z_streamp strm)
block_state compress_func OF((deflate_state *s, int flush))
#define INSERT_STRING(s, str, match_head)
int ZEXPORT deflateParams(z_streamp strm, int level, int strategy)
uLong ZEXPORT deflateBound(z_streamp strm, uLong sourceLen)
unsigned read_buf(z_streamp strm, Bytef *buf, unsigned size)
const char deflate_copyright[]
block_state deflate_fast(deflate_state *s, int flush)
block_state deflate_slow(deflate_state *s, int flush)
#define FLUSH_BLOCK(s, last)
int ZEXPORT deflateTune(z_streamp strm, int good_length, int max_lazy, int nice_length, int max_chain)
uInt longest_match(deflate_state *s, IPos cur_match)
int ZEXPORT deflatePrime(z_streamp strm, int bits, int value)
int ZEXPORT deflateGetDictionary(z_streamp strm, Bytef *dictionary, uInt *dictLength)
block_state deflate_huff(deflate_state *s, int flush)
int deflateStateCheck(z_streamp strm)
int ZEXPORT deflateResetKeep(z_streamp strm)
int ZEXPORT deflateEnd(z_streamp strm)
void fill_window(deflate_state *s)
int ZEXPORT deflateInit2_(z_streamp strm, int level, int method, int windowBits, int memLevel, int strategy, const char *version, int stream_size)
void lm_init(deflate_state *s)
int ZEXPORT deflate(z_streamp strm, int flush)
void slide_hash(deflate_state *s)
block_state deflate_rle(deflate_state *s, int flush)
#define _tr_tally_dist(s, distance, length, flush)
#define _tr_tally_lit(s, c, flush)
struct tree_desc_s l_desc
struct ct_data_s dyn_dtree[2 *D_CODES+1]
struct ct_data_s bl_tree[2 *BL_CODES+1]
struct tree_desc_s bl_desc
struct tree_desc_s d_desc
struct ct_data_s dyn_ltree[HEAP_SIZE]
void ZLIB_INTERNAL _tr_init(deflate_state *s)
void ZLIB_INTERNAL _tr_stored_block(deflate_state *s, charf *buf, ulg stored_len, int last)
void ZLIB_INTERNAL _tr_flush_bits(deflate_state *s)
void ZLIB_INTERNAL _tr_align(deflate_state *s)
gz_header FAR * gz_headerp
#define Z_DEFAULT_STRATEGY
#define Z_DEFAULT_COMPRESSION
void ZLIB_INTERNAL zcfree(voidpf opaque, voidpf ptr)
voidpf ZLIB_INTERNAL zcalloc(voidpf opaque, unsigned items, unsigned size)
void ZLIB_INTERNAL zmemzero(Bytef *dest, uInt len)
void ZLIB_INTERNAL zmemcpy(Bytef *dest, const Bytef *source, uInt len)
int ZLIB_INTERNAL zmemcmp(Bytef *s1, const Bytef *s2, uInt len) const
#define ERR_RETURN(strm, err)
#define ZALLOC(strm, items, size)
#define Assert(cond, msg)
#define ZFREE(strm, addr)