55 " deflate 1.3.1 Copyright 1995-2024 Jean-loup Gailly and Mark Adler ";
133 #define RANK(f) (((f) * 2) - ((f) > 4 ? 9 : 0))
141 #define UPDATE_HASH(s,h,c) (h = (((h) << s->hash_shift) ^ (c)) & s->hash_mask)
155 #define INSERT_STRING(s, str, match_head) \
156 (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
157 match_head = s->head[s->ins_h], \
158 s->head[s->ins_h] = (Pos)(str))
160 #define INSERT_STRING(s, str, match_head) \
161 (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
162 match_head = s->prev[(str) & s->w_mask] = s->head[s->ins_h], \
163 s->head[s->ins_h] = (Pos)(str))
170 #define CLEAR_HASH(s) \
172 s->head[s->hash_size - 1] = NIL; \
173 zmemzero((Bytef *)s->head, \
174 (unsigned)(s->hash_size - 1)*sizeof(*s->head)); \
182 #if defined(__has_feature)
183 # if __has_feature(memory_sanitizer)
184 __attribute__((no_sanitize(
"memory")))
196 *p = (
Pos)(m >= wsize ? m - wsize :
NIL);
203 *p = (
Pos)(m >= wsize ? m - wsize :
NIL);
219 unsigned len = strm->avail_in;
221 if (len > size) len =
size;
222 if (len == 0)
return 0;
224 strm->avail_in -= len;
226 zmemcpy(buf, strm->next_in, len);
227 if (strm->state->wrap == 1) {
228 strm->adler =
adler32(strm->adler, buf, len);
231 else if (strm->state->wrap == 2) {
232 strm->adler =
crc32(strm->adler, buf, len);
235 strm->next_in += len;
236 strm->total_in += len;
262 if (
sizeof(
int) <= 2) {
266 }
else if (more == (
unsigned)(-1)) {
288 if (s->
strm->avail_in == 0)
break;
301 Assert(more >= 2,
"more < 2");
367 "not enough room for search");
380 int windowBits,
int memLevel,
int strategy,
381 const char *version,
int stream_size) {
386 if (version ==
Z_NULL || version[0] != my_version[0] ||
409 if (level != 0) level = 1;
414 if (windowBits < 0) {
416 if (windowBits < -15)
418 windowBits = -windowBits;
421 else if (windowBits > 15) {
427 windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
428 strategy < 0 || strategy >
Z_FIXED || (windowBits == 8 && wrap != 1)) {
431 if (windowBits == 8) windowBits = 9;
567 strm->adler =
adler32(strm->adler, dictionary, dictLength);
571 if (dictLength >= s->
w_size) {
578 dictionary += dictLength - s->
w_size;
583 avail = strm->avail_in;
584 next = strm->next_in;
585 strm->avail_in = dictLength;
609 strm->next_in = next;
610 strm->avail_in = avail;
627 if (dictionary !=
Z_NULL && len)
642 strm->total_in = strm->total_out = 0;
708 strm->state->gzhead =
head;
716 *pending = strm->state->pending;
718 *bits = strm->state->bi_valid;
730 if (bits < 0 || bits > 16 ||
734 if (bits < 0 || bits > 16 ||
760 if (level != 0) level = 1;
764 if (level < 0 || level > 9 || strategy < 0 || strategy >
Z_FIXED) {
778 if (s->
level != level) {
798 int nice_length,
int max_chain) {
836 uLong fixedlen, storelen, wraplen;
841 fixedlen = sourceLen + (sourceLen >> 3) + (sourceLen >> 8) +
842 (sourceLen >> 9) + 4;
846 storelen = sourceLen + (sourceLen >> 5) + (sourceLen >> 7) +
847 (sourceLen >> 11) + 7;
851 return (fixedlen > storelen ? fixedlen : storelen) + 6;
860 wraplen = 6 + (s->
strstart ? 4 : 0);
868 wraplen += 2 + s->
gzhead->extra_len;
895 return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) +
896 (sourceLen >> 25) + 13 - 6 + wraplen;
921 if (len > strm->avail_out) len = strm->avail_out;
922 if (len == 0)
return;
925 strm->next_out += len;
927 strm->total_out += len;
928 strm->avail_out -= len;
938 #define HCRC_UPDATE(beg) \
940 if (s->gzhead->hcrc && s->pending > (beg)) \
941 strm->adler = crc32(strm->adler, s->pending_buf + (beg), \
942 s->pending - (beg)); \
955 if (strm->next_out ==
Z_NULL ||
956 (strm->avail_in != 0 && strm->next_in ==
Z_NULL) ||
968 if (strm->avail_out == 0) {
983 }
else if (strm->avail_in == 0 &&
RANK(flush) <=
RANK(old_flush) &&
1003 else if (s->
level < 6)
1005 else if (s->
level == 6)
1009 header |= (level_flags << 6);
1011 header += 31 - (header % 31);
1058 (s->
gzhead->hcrc ? 2 : 0) +
1178 if (strm->avail_in != 0 || s->
lookahead != 0 ||
1191 if (strm->avail_out == 0) {
1206 }
else if (flush !=
Z_BLOCK) {
1221 if (strm->avail_out == 0) {
1263 status = strm->state->status;
1266 TRY_FREE(strm, strm->state->pending_buf);
1269 TRY_FREE(strm, strm->state->window);
1271 ZFREE(strm, strm->state);
1308 ds->head = (
Posf *)
ZALLOC(dest, ds->hash_size,
sizeof(
Pos));
1312 ds->pending_buf ==
Z_NULL) {
1317 zmemcpy(ds->window, ss->window, ds->w_size * 2 *
sizeof(
Byte));
1320 zmemcpy(ds->pending_buf, ss->pending_buf, ds->lit_bufsize *
LIT_BUFS);
1322 ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf);
1324 ds->d_buf = (
ushf *)(ds->pending_buf + (ds->lit_bufsize << 1));
1325 ds->l_buf = ds->pending_buf + (ds->lit_bufsize << 2);
1327 ds->sym_buf = ds->pending_buf + ds->lit_bufsize;
1330 ds->l_desc.dyn_tree = ds->dyn_ltree;
1331 ds->d_desc.dyn_tree = ds->dyn_dtree;
1332 ds->bl_desc.dyn_tree = ds->bl_tree;
1351 register Bytef *match;
1368 register ush scan_start = *(
ushf*)scan;
1369 register ush scan_end = *(
ushf*)(scan + best_len - 1);
1372 register Byte scan_end1 = scan[best_len - 1];
1373 register Byte scan_end = scan[best_len];
1395 match = s->
window + cur_match;
1405 #if (defined(UNALIGNED_OK) && MAX_MATCH == 258)
1409 if (*(
ushf*)(match + best_len - 1) != scan_end ||
1410 *(
ushf*)match != scan_start)
continue;
1421 Assert(scan[2] == match[2],
"scan[2]?");
1424 }
while (*(
ushf*)(scan += 2) == *(
ushf*)(match += 2) &&
1425 *(
ushf*)(scan += 2) == *(
ushf*)(match += 2) &&
1426 *(
ushf*)(scan += 2) == *(
ushf*)(match += 2) &&
1427 *(
ushf*)(scan += 2) == *(
ushf*)(match += 2) &&
1434 if (*scan == *match) scan++;
1436 len = (
MAX_MATCH - 1) - (
int)(strend - scan);
1441 if (match[best_len] != scan_end ||
1442 match[best_len - 1] != scan_end1 ||
1444 *++match != scan[1])
continue;
1453 Assert(*scan == *match,
"match[2]?");
1459 }
while (*++scan == *++match && *++scan == *++match &&
1460 *++scan == *++match && *++scan == *++match &&
1461 *++scan == *++match && *++scan == *++match &&
1462 *++scan == *++match && *++scan == *++match &&
1473 if (len > best_len) {
1478 scan_end = *(
ushf*)(scan + best_len - 1);
1480 scan_end1 = scan[best_len - 1];
1481 scan_end = scan[best_len];
1484 }
while ((cur_match = prev[cur_match & wmask]) > limit
1485 && --chain_length != 0);
1498 register Bytef *match;
1512 match = s->
window + cur_match;
1516 if (match[0] != scan[0] || match[1] != scan[1])
return MIN_MATCH-1;
1524 scan += 2, match += 2;
1525 Assert(*scan == *match,
"match[2]?");
1531 }
while (*++scan == *++match && *++scan == *++match &&
1532 *++scan == *++match && *++scan == *++match &&
1533 *++scan == *++match && *++scan == *++match &&
1534 *++scan == *++match && *++scan == *++match &&
1561 if (match == (
IPos)-1) {
1568 if (
zmemcmp(back, here, len) != EQUAL) {
1569 fprintf(stderr,
" start %u, match %d, length %d\n",
1570 start, (
int)match, length);
1572 fprintf(stderr,
"(%02x %02x)", *back++, *here++);
1573 }
while (--len != 0);
1574 z_error(
"invalid match");
1576 if (z_verbose > 1) {
1577 fprintf(stderr,
"\\[%d,%d]", start - match, length);
1578 do { putc(s->
window[start++], stderr); }
while (--length != 0);
1582 # define check_match(s, start, match, length)
1589 #define FLUSH_BLOCK_ONLY(s, last) { \
1590 _tr_flush_block(s, (s->block_start >= 0L ? \
1591 (charf *)&s->window[(unsigned)s->block_start] : \
1593 (ulg)((long)s->strstart - s->block_start), \
1595 s->block_start = s->strstart; \
1596 flush_pending(s->strm); \
1597 Tracev((stderr,"[FLUSH]")); \
1601 #define FLUSH_BLOCK(s, last) { \
1602 FLUSH_BLOCK_ONLY(s, last); \
1603 if (s->strm->avail_out == 0) return (last) ? finish_started : need_more; \
1607 #define MAX_STORED 65535
1610 #define MIN(a, b) ((a) > (b) ? (b) : (a))
1638 unsigned len, left, have, last = 0;
1639 unsigned used = s->
strm->avail_in;
1647 if (s->
strm->avail_out < have)
1650 have = s->
strm->avail_out - have;
1652 if (len > (
ulg)left + s->
strm->avail_in)
1653 len = left + s->
strm->avail_in;
1662 if (len < min_block && ((len == 0 && flush !=
Z_FINISH) ||
1664 len != left + s->
strm->avail_in))
1670 last = flush ==
Z_FINISH && len == left + s->
strm->avail_in ? 1 : 0;
1684 s->compressed_len += len << 3;
1685 s->bits_sent += len << 3;
1693 s->
strm->next_out += left;
1694 s->
strm->avail_out -= left;
1695 s->
strm->total_out += left;
1705 s->
strm->next_out += len;
1706 s->
strm->avail_out -= len;
1707 s->
strm->total_out += len;
1709 }
while (last == 0);
1717 used -= s->
strm->avail_in;
1769 if (have > s->
strm->avail_in)
1770 have = s->
strm->avail_in;
1789 if (left >= min_block ||
1791 s->
strm->avail_in == 0 && left <= have)) {
1792 len =
MIN(left, have);
1794 len == left ? 1 : 0;
2041 Bytef *scan, *strend;
2061 if (prev == *++scan && prev == *++scan && prev == *++scan) {
2064 }
while (prev == *++scan && prev == *++scan &&
2065 prev == *++scan && prev == *++scan &&
2066 prev == *++scan && prev == *++scan &&
2067 prev == *++scan && prev == *++scan &&
int ZEXPORT deflateGetDictionary(z_streamp strm, Bytef *dictionary, uInt *dictLength)
int ZEXPORT deflateParams(z_streamp strm, int level, int strategy)
#define INSERT_STRING(s, str, match_head)
#define check_match(s, start, match, length)
local void flush_pending(z_streamp strm)
#define Assert(cond, msg)
local uInt longest_match(deflate_state *s, IPos cur_match)
voidpf(* alloc_func)(voidpf opaque, uInt items, uInt size)
#define UPDATE_HASH(s, h, c)
typedef int(ZCALLBACK *close_file_func) OF((voidpf opaque
int ZEXPORT deflateInit2_(z_streamp strm, int level, int method, int windowBits, int memLevel, int strategy, const char *version, int stream_size)
uLong ZEXPORT deflateBound(z_streamp strm, uLong sourceLen)
int ZEXPORT deflateEnd(z_streamp strm)
void ZLIB_INTERNAL _tr_init(deflate_state *s)
int ZEXPORT deflatePrime(z_streamp strm, int bits, int value)
gz_header FAR * gz_headerp
const char deflate_copyright[]
void ZLIB_INTERNAL _tr_align(deflate_state *s)
#define _tr_tally_lit(s, c, flush)
#define ZALLOC(strm, items, size)
local int deflateStateCheck(z_streamp strm)
local block_state deflate_huff(deflate_state *s, int flush)
local block_state deflate_stored(deflate_state *s, int flush)
int ZEXPORT deflateCopy(z_streamp dest, z_streamp source)
int ZEXPORT deflateTune(z_streamp strm, int good_length, int max_lazy, int nice_length, int max_chain)
void ZLIB_INTERNAL _tr_flush_bits(deflate_state *s)
#define ZFREE(strm, addr)
local block_state deflate_fast(deflate_state *s, int flush)
typedef long(ZCALLBACK *tell_file_func) OF((voidpf opaque
unsigned long ZEXPORT crc32(unsigned long crc, const unsigned char FAR *buf, uInt len)
void ZLIB_INTERNAL zmemzero(Bytef *dest, uInt len)
local void slide_hash(deflate_state *s)
void ZLIB_INTERNAL zmemcpy(Bytef *dest, const Bytef *source, uInt len)
local const config configuration_table[10]
#define _tr_tally_dist(s, distance, length, flush)
int ZLIB_INTERNAL zmemcmp(const Bytef *s1, const Bytef *s2, uInt len)
int ZEXPORT deflate(z_streamp strm, int flush)
local void fill_window(deflate_state *s)
local block_state deflate_rle(deflate_state *s, int flush)
uLong ZEXPORT adler32(uLong adler, const Bytef *buf, uInt len)
local void putShortMSB(deflate_state *s, uInt b)
local unsigned read_buf(z_streamp strm, Bytef *buf, unsigned size)
int ZEXPORT deflateResetKeep(z_streamp strm)
int ZEXPORT deflateSetDictionary(z_streamp strm, const Bytef *dictionary, uInt dictLength)
#define ERR_RETURN(strm, err)
int ZEXPORT deflateSetHeader(z_streamp strm, gz_headerp head)
#define FLUSH_BLOCK(s, last)
local void lm_init(deflate_state *s)
int ZEXPORT deflateInit_(z_streamp strm, int level, const char *version, int stream_size)
local block_state deflate_slow(deflate_state *s, int flush)
#define Z_DEFAULT_STRATEGY
int ZEXPORT deflateReset(z_streamp strm)
int ZEXPORT deflatePending(z_streamp strm, unsigned *pending, int *bits)
void ZLIB_INTERNAL _tr_stored_block(deflate_state *s, charf *buf, ulg stored_len, int last)
void(* free_func)(voidpf opaque, voidpf address)
block_state(* compress_func)(deflate_state *s, int flush)
voidpf ZLIB_INTERNAL zcalloc(voidpf opaque, unsigned items, unsigned size)
#define Z_DEFAULT_COMPRESSION
#define FLUSH_BLOCK_ONLY(s, last)
void ZLIB_INTERNAL zcfree(voidpf opaque, voidpf ptr)