55 " deflate 1.2.5 Copyright 1995-2010 Jean-loup Gailly and Mark Adler ";
89 void match_init
OF((
void));
108 # define TOO_FAR 4096 154 #ifndef NO_DUMMY_DECL 164 #define UPDATE_HASH(s,h,c) (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask) 178 #define INSERT_STRING(s, str, match_head) \ 179 (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \ 180 match_head = s->head[s->ins_h], \ 181 s->head[s->ins_h] = (Pos)(str)) 183 #define INSERT_STRING(s, str, match_head) \ 184 (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \ 185 match_head = s->prev[(str) & s->w_mask] = s->head[s->ins_h], \ 186 s->head[s->ins_h] = (Pos)(str)) 193 #define CLEAR_HASH(s) \ 194 s->head[s->hash_size-1] = NIL; \ 195 zmemzero((Bytef *)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head)); 230 if (version ==
Z_NULL || version[0] != my_version[0] ||
237 if (strm->zalloc == (alloc_func)0) {
241 if (strm->zfree == (free_func)0) strm->zfree =
zcfree;
244 if (level != 0) level = 1;
249 if (windowBits < 0) {
251 windowBits = -windowBits;
254 else if (windowBits > 15) {
260 windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
261 strategy < 0 || strategy >
Z_FIXED) {
264 if (windowBits == 8) windowBits = 9;
317 uInt length = dictLength;
322 strm->state->wrap == 2 ||
323 (strm->state->wrap == 1 && strm->state->status !=
INIT_STATE))
328 strm->adler =
adler32(strm->adler, dictionary, dictLength);
333 dictionary += dictLength - length;
345 for (n = 0; n <= length -
MIN_MATCH; n++) {
348 if (hash_head) hash_head = 0;
359 strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0) {
363 strm->total_in = strm->total_out = 0;
395 strm->state->gzhead =
head;
406 strm->state->bi_valid =
bits;
407 strm->state->bi_buf = (
ush)(value & ((1 << bits) - 1));
425 if (level != 0) level = 1;
429 if (level < 0 || level > 9 || strategy < 0 || strategy >
Z_FIXED) {
432 func = configuration_table[s->
level].func;
434 if ((strategy != s->
strategy || func != configuration_table[level].func) &&
435 strm->total_in != 0) {
439 if (s->
level != level) {
491 uLong complen, wraplen;
495 complen = sourceLen +
496 ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 5;
509 wraplen = 6 + (s->
strstart ? 4 : 0);
515 wraplen += 2 + s->
gzhead->extra_len;
536 return complen + wraplen;
539 return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) +
540 (sourceLen >> 25) + 13 - 6 + wraplen;
565 unsigned len = strm->state->pending;
567 if (len > strm->avail_out) len = strm->avail_out;
568 if (len == 0)
return;
570 zmemcpy(strm->next_out, strm->state->pending_out, len);
571 strm->next_out += len;
572 strm->state->pending_out += len;
573 strm->total_out += len;
574 strm->avail_out -= len;
575 strm->state->pending -= len;
576 if (strm->state->pending == 0) {
577 strm->state->pending_out = strm->state->pending_buf;
590 flush >
Z_BLOCK || flush < 0) {
595 if (strm->next_out ==
Z_NULL ||
596 (strm->next_in ==
Z_NULL && strm->avail_in != 0) ||
628 (s->
gzhead->hcrc ? 2 : 0) +
660 else if (s->
level < 6)
662 else if (s->
level == 6)
666 header |= (level_flags << 6);
668 header += 31 - (header % 31);
789 if (strm->avail_out == 0) {
804 }
else if (strm->avail_in == 0 && flush <= old_flush &&
816 if (strm->avail_in != 0 || s->
lookahead != 0 ||
822 (*(configuration_table[s->
level].func))(s, flush));
828 if (strm->avail_out == 0) {
857 if (strm->avail_out == 0) {
863 Assert(strm->avail_out > 0,
"bug2");
902 status = strm->state->status;
914 TRY_FREE(strm, strm->state->pending_buf);
917 TRY_FREE(strm, strm->state->window);
919 ZFREE(strm, strm->state);
997 unsigned len = strm->avail_in;
999 if (len > size) len = size;
1000 if (len == 0)
return 0;
1002 strm->avail_in -= len;
1004 if (strm->state->wrap == 1) {
1005 strm->adler =
adler32(strm->adler, strm->next_in, len);
1008 else if (strm->state->wrap == 2) {
1009 strm->adler =
crc32(strm->adler, strm->next_in, len);
1012 zmemcpy(buf, strm->next_in, len);
1013 strm->next_in += len;
1014 strm->total_in += len;
1031 s->max_lazy_match = configuration_table[s->level].max_lazy;
1032 s->good_match = configuration_table[s->level].good_length;
1033 s->nice_match = configuration_table[s->level].nice_length;
1034 s->max_chain_length = configuration_table[s->level].max_chain;
1037 s->block_start = 0L;
1039 s->match_length = s->prev_length =
MIN_MATCH-1;
1040 s->match_available = 0;
1068 register Bytef *scan = s->window + s->strstart;
1069 register Bytef *match;
1071 int best_len = s->prev_length;
1079 uInt wmask = s->w_mask;
1085 register Bytef *strend = s->window + s->strstart +
MAX_MATCH - 1;
1086 register ush scan_start = *(
ushf*)scan;
1087 register ush scan_end = *(
ushf*)(scan+best_len-1);
1090 register Byte scan_end1 = scan[best_len-1];
1091 register Byte scan_end = scan[best_len];
1100 if (s->prev_length >= s->good_match) {
1106 if ((
uInt)nice_match > s->lookahead) nice_match = s->lookahead;
1112 match = s->window + cur_match;
1122 #if (defined(UNALIGNED_OK) && MAX_MATCH == 258) 1126 if (*(
ushf*)(match+best_len-1) != scan_end ||
1127 *(
ushf*)match != scan_start)
continue;
1138 Assert(scan[2] == match[2],
"scan[2]?");
1141 }
while (*(
ushf*)(scan+=2) == *(
ushf*)(match+=2) &&
1142 *(
ushf*)(scan+=2) == *(
ushf*)(match+=2) &&
1143 *(
ushf*)(scan+=2) == *(
ushf*)(match+=2) &&
1144 *(
ushf*)(scan+=2) == *(
ushf*)(match+=2) &&
1149 Assert(scan <= s->
window+(
unsigned)(s->window_size-1),
"wild scan");
1150 if (*scan == *match) scan++;
1152 len = (
MAX_MATCH - 1) - (
int)(strend-scan);
1157 if (match[best_len] != scan_end ||
1158 match[best_len-1] != scan_end1 ||
1160 *++match != scan[1])
continue;
1169 Assert(*scan == *match,
"match[2]?");
1175 }
while (*++scan == *++match && *++scan == *++match &&
1176 *++scan == *++match && *++scan == *++match &&
1177 *++scan == *++match && *++scan == *++match &&
1178 *++scan == *++match && *++scan == *++match &&
1181 Assert(scan <= s->
window+(
unsigned)(s->window_size-1),
"wild scan");
1188 if (len > best_len) {
1189 s->match_start = cur_match;
1191 if (len >= nice_match)
break;
1193 scan_end = *(
ushf*)(scan+best_len-1);
1195 scan_end1 = scan[best_len-1];
1196 scan_end = scan[best_len];
1199 }
while ((cur_match = prev[cur_match & wmask]) > limit
1200 && --chain_length != 0);
1202 if ((
uInt)best_len <= s->lookahead)
return (
uInt)best_len;
1203 return s->lookahead;
1217 register Bytef *match;
1230 match = s->window + cur_match;
1234 if (match[0] != scan[0] || match[1] != scan[1])
return MIN_MATCH-1;
1242 scan += 2, match += 2;
1243 Assert(*scan == *match,
"match[2]?");
1249 }
while (*++scan == *++match && *++scan == *++match &&
1250 *++scan == *++match && *++scan == *++match &&
1251 *++scan == *++match && *++scan == *++match &&
1252 *++scan == *++match && *++scan == *++match &&
1255 Assert(scan <= s->
window+(
unsigned)(s->window_size-1),
"wild scan");
1261 s->match_start = cur_match;
1262 return (
uInt)len <= s->lookahead ? (
uInt)len : s->lookahead;
1277 if (
zmemcmp(s->window + match,
1278 s->window + start, length) !=
EQUAL) {
1279 fprintf(stderr,
" start %u, match %u, length %d\n",
1280 start, match, length);
1282 fprintf(stderr,
"%c%c", s->window[match++], s->window[start++]);
1283 }
while (--length != 0);
1284 z_error(
"invalid match");
1286 if (z_verbose > 1) {
1287 fprintf(stderr,
"\\[%d,%d]", start-match, length);
1288 do { putc(s->window[start++], stderr); }
while (--length != 0);
1292 # define check_match(s, start, match, length) 1308 register unsigned n, m;
1314 more = (unsigned)(s->window_size -(
ulg)s->lookahead -(
ulg)s->strstart);
1317 if (
sizeof(
int) <= 2) {
1318 if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
1321 }
else if (more == (
unsigned)(-1)) {
1332 if (s->strstart >= wsize+
MAX_DIST(s)) {
1334 zmemcpy(s->window, s->window+wsize, (
unsigned)wsize);
1335 s->match_start -= wsize;
1336 s->strstart -= wsize;
1337 s->block_start -= (long) wsize;
1349 *p = (
Pos)(m >= wsize ? m-wsize :
NIL);
1357 *p = (
Pos)(m >= wsize ? m-wsize :
NIL);
1365 if (s->strm->avail_in == 0)
return;
1378 Assert(more >= 2,
"more < 2");
1380 n =
read_buf(s->strm, s->window + s->strstart + s->lookahead, more);
1385 s->ins_h = s->window[s->strstart];
1386 UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
1395 }
while (s->lookahead <
MIN_LOOKAHEAD && s->strm->avail_in != 0);
1404 if (s->high_water < s->window_size) {
1405 ulg curr = s->strstart + (
ulg)(s->lookahead);
1408 if (s->high_water < curr) {
1412 init = s->window_size - curr;
1415 zmemzero(s->window + curr, (
unsigned)init);
1416 s->high_water = curr + init;
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;
1436 #define FLUSH_BLOCK_ONLY(s, last) { \ 1437 _tr_flush_block(s, (s->block_start >= 0L ? \ 1438 (charf *)&s->window[(unsigned)s->block_start] : \ 1440 (ulg)((long)s->strstart - s->block_start), \ 1442 s->block_start = s->strstart; \ 1443 flush_pending(s->strm); \ 1444 Tracev((stderr,"[FLUSH]")); \ 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; \ 1469 ulg max_block_size = 0xffff;
1472 if (max_block_size > s->pending_buf_size - 5) {
1479 if (s->lookahead <= 1) {
1482 s->block_start >= (
long)s->w_size,
"slide too late");
1487 if (s->lookahead == 0)
break;
1489 Assert(s->block_start >= 0L,
"block gone");
1491 s->strstart += s->lookahead;
1495 max_start = s->block_start + max_block_size;
1496 if (s->strstart == 0 || (
ulg)s->strstart >= max_start) {
1498 s->lookahead = (
uInt)(s->strstart - max_start);
1499 s->strstart = (
uInt)max_start;
1505 if (s->strstart - (
uInt)s->block_start >=
MAX_DIST(s)) {
1538 if (s->lookahead == 0)
break;
1552 if (hash_head !=
NIL && s->strstart - hash_head <=
MAX_DIST(s)) {
1561 check_match(s, s->strstart, s->match_start, s->match_length);
1566 s->lookahead -= s->match_length;
1572 if (s->match_length <= s->max_insert_length &&
1581 }
while (--s->match_length != 0);
1586 s->strstart += s->match_length;
1587 s->match_length = 0;
1588 s->ins_h = s->window[s->strstart];
1589 UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
1599 Tracevv((stderr,
"%c", s->window[s->strstart]));
1635 if (s->lookahead == 0)
break;
1648 s->prev_length = s->match_length, s->prev_match = s->match_start;
1651 if (hash_head !=
NIL && s->prev_length < s->max_lazy_match &&
1652 s->strstart - hash_head <=
MAX_DIST(s)) {
1660 if (s->match_length <= 5 && (s->strategy ==
Z_FILTERED 1663 s->strstart - s->match_start >
TOO_FAR)
1676 if (s->prev_length >=
MIN_MATCH && s->match_length <= s->prev_length) {
1680 check_match(s, s->strstart-1, s->prev_match, s->prev_length);
1690 s->lookahead -= s->prev_length-1;
1691 s->prev_length -= 2;
1693 if (++s->strstart <= max_insert) {
1696 }
while (--s->prev_length != 0);
1697 s->match_available = 0;
1703 }
else if (s->match_available) {
1708 Tracevv((stderr,
"%c", s->window[s->strstart-1]));
1715 if (s->strm->avail_out == 0)
return need_more;
1720 s->match_available = 1;
1726 if (s->match_available) {
1727 Tracevv((stderr,
"%c", s->window[s->strstart-1]));
1729 s->match_available = 0;
1747 Bytef *scan, *strend;
1759 if (s->lookahead == 0)
break;
1763 s->match_length = 0;
1764 if (s->lookahead >=
MIN_MATCH && s->strstart > 0) {
1765 scan = s->window + s->strstart - 1;
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;
1783 check_match(s, s->strstart, s->strstart - 1, s->match_length);
1787 s->lookahead -= s->match_length;
1788 s->strstart += s->match_length;
1789 s->match_length = 0;
1792 Tracevv((stderr,
"%c", s->window[s->strstart]));
1815 if (s->lookahead == 0) {
1817 if (s->lookahead == 0) {
1825 s->match_length = 0;
1826 Tracevv((stderr,
"%c", s->window[s->strstart]));
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)
int ZLIB_INTERNAL zmemcmp(Bytef *s1, const Bytef *s2, uInt len) const
void ZLIB_INTERNAL _tr_align(deflate_state *s)
#define Assert(cond, msg)
local uInt longest_match(deflate_state *s, IPos cur_match)
#define UPDATE_HASH(s, h, c)
struct ct_data_s dyn_ltree[HEAP_SIZE]
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)
int ZEXPORT deflatePrime(z_streamp strm, int bits, int value)
gz_header FAR * gz_headerp
const char deflate_copyright[]
local int read_buf(z_streamp strm, Bytef *buf, unsigned size)
#define _tr_tally_lit(s, c, flush)
#define ZALLOC(strm, items, size)
local block_state deflate_huff(deflate_state *s, int flush)
local block_state deflate_stored(deflate_state *s, int flush)
struct tree_desc_s bl_desc
struct tree_desc_s d_desc
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_init(deflate_state *s)
#define ZFREE(strm, addr)
local block_state deflate_fast(deflate_state *s, int flush)
unsigned long ZEXPORT crc32(unsigned long crc, const unsigned char FAR *buf, uInt len)
void ZLIB_INTERNAL zmemzero(Bytef *dest, uInt len)
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 ZEXPORT deflate(z_streamp strm, int flush)
void ZLIB_INTERNAL _tr_stored_block(deflate_state *s, charf *buf, ulg stored_len, int last)
struct ct_data_s dyn_dtree[2 *D_CODES+1]
local void fill_window(deflate_state *s)
struct tree_desc_s l_desc
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)
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)
voidpf ZLIB_INTERNAL zcalloc(voidpf opaque, unsigned items, unsigned size)
struct ct_data_s bl_tree[2 *BL_CODES+1]
block_state compress_func OF((deflate_state *s, int flush))
#define Z_DEFAULT_COMPRESSION
#define FLUSH_BLOCK_ONLY(s, last)
void ZLIB_INTERNAL zcfree(voidpf opaque, voidpf ptr)