55 " deflate 1.2.8 Copyright 1995-2013 Jean-loup Gailly and Mark Adler ";
89 void match_init
OF((
void));
108 # define TOO_FAR 4096 117 typedef struct config_s {
154 #ifndef NO_DUMMY_DECL 159 #define RANK(f) (((f) << 1) - ((f) > 4 ? 9 : 0)) 167 #define UPDATE_HASH(s,h,c) (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask) 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->head[s->ins_h], \ 184 s->head[s->ins_h] = (Pos)(str)) 186 #define INSERT_STRING(s, str, match_head) \ 187 (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \ 188 match_head = s->prev[(str) & s->w_mask] = s->head[s->ins_h], \ 189 s->head[s->ins_h] = (Pos)(str)) 196 #define CLEAR_HASH(s) \ 197 s->head[s->hash_size-1] = NIL; \ 198 zmemzero((Bytef *)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head)); 214 version, stream_size)
233 if (version ==
Z_NULL || version[0] != my_version[0] ||
256 if (level != 0) level = 1;
261 if (windowBits < 0) {
263 windowBits = -windowBits;
266 else if (windowBits > 15) {
272 windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
273 strategy < 0 || strategy >
Z_FIXED) {
276 if (windowBits == 8) windowBits = 9;
343 strm->adler =
adler32(strm->adler, dictionary, dictLength);
347 if (dictLength >= s->
w_size) {
354 dictionary += dictLength - s->
w_size;
359 avail = strm->avail_in;
360 next = strm->next_in;
361 strm->avail_in = dictLength;
385 strm->next_in = next;
386 strm->avail_in = avail;
402 strm->total_in = strm->total_out = 0;
445 strm->state->gzhead = head;
457 *pending = strm->
state->pending;
459 *bits = strm->
state->bi_valid;
503 if (level != 0) level = 1;
507 if (level < 0 || level > 9 || strategy < 0 || strategy >
Z_FIXED) {
510 func = configuration_table[s->
level].func;
512 if ((strategy != s->
strategy || func != configuration_table[level].func) &&
513 strm->total_in != 0) {
519 if (s->
level != level) {
522 s->
good_match = configuration_table[level].good_length;
523 s->
nice_match = configuration_table[level].nice_length;
571 uLong complen, wraplen;
575 complen = sourceLen +
576 ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 5;
589 wraplen = 6 + (s->
strstart ? 4 : 0);
616 return complen + wraplen;
619 return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) +
620 (sourceLen >> 25) + 13 - 6 + wraplen;
650 if (len > strm->avail_out) len = strm->avail_out;
651 if (len == 0)
return;
654 strm->next_out += len;
656 strm->total_out += len;
657 strm->avail_out -= len;
673 flush >
Z_BLOCK || flush < 0) {
678 if (strm->next_out ==
Z_NULL ||
679 (strm->next_in ==
Z_NULL && strm->avail_in != 0) ||
743 else if (s->
level < 6)
745 else if (s->
level == 6)
749 header |= (level_flags << 6);
751 header += 31 - (header % 31);
872 if (strm->avail_out == 0) {
887 }
else if (strm->avail_in == 0 &&
RANK(flush) <=
RANK(old_flush) &&
899 if (strm->avail_in != 0 || s->
lookahead != 0 ||
905 (*(configuration_table[s->
level].func))(s, flush));
911 if (strm->avail_out == 0) {
941 if (strm->avail_out == 0) {
947 Assert(strm->avail_out > 0,
"bug2");
986 status = strm->state->status;
998 TRY_FREE(strm, strm->state->pending_buf);
1001 TRY_FREE(strm, strm->state->window);
1003 ZFREE(strm, strm->state);
1042 ds->head = (
Posf *)
ZALLOC(dest, ds->hash_size,
sizeof(
Pos));
1043 overlay = (
ushf *)
ZALLOC(dest, ds->lit_bufsize,
sizeof(
ush)+2);
1044 ds->pending_buf = (
uchf *) overlay;
1047 ds->pending_buf ==
Z_NULL) {
1052 zmemcpy(ds->window, ss->window, ds->w_size * 2 *
sizeof(
Byte));
1055 zmemcpy(ds->pending_buf, ss->pending_buf, (
uInt)ds->pending_buf_size);
1057 ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf);
1058 ds->d_buf = overlay + ds->lit_bufsize/
sizeof(
ush);
1059 ds->l_buf = ds->pending_buf + (1+
sizeof(
ush))*ds->lit_bufsize;
1061 ds->l_desc.dyn_tree = ds->dyn_ltree;
1062 ds->d_desc.dyn_tree = ds->dyn_dtree;
1063 ds->bl_desc.dyn_tree = ds->bl_tree;
1081 unsigned len = strm->avail_in;
1083 if (len > size) len =
size;
1084 if (len == 0)
return 0;
1086 strm->avail_in -= len;
1088 zmemcpy(buf, strm->next_in, len);
1089 if (strm->state->wrap == 1) {
1090 strm->adler =
adler32(strm->adler, buf, len);
1093 else if (strm->state->wrap == 2) {
1094 strm->adler =
crc32(strm->adler, buf, len);
1097 strm->next_in += len;
1098 strm->total_in += len;
1115 s->max_lazy_match = configuration_table[s->level].max_lazy;
1116 s->good_match = configuration_table[s->level].good_length;
1117 s->nice_match = configuration_table[s->level].nice_length;
1118 s->max_chain_length = configuration_table[s->level].max_chain;
1121 s->block_start = 0L;
1124 s->match_length = s->prev_length =
MIN_MATCH-1;
1125 s->match_available = 0;
1153 register Bytef *scan = s->window + s->strstart;
1156 int best_len = s->prev_length;
1157 int nice_match = s->nice_match;
1163 Posf *prev = s->prev;
1164 uInt wmask = s->w_mask;
1170 register Bytef *strend = s->window + s->strstart +
MAX_MATCH - 1;
1171 register ush scan_start = *(
ushf*)scan;
1172 register ush scan_end = *(
ushf*)(scan+best_len-1);
1175 register Byte scan_end1 = scan[best_len-1];
1176 register Byte scan_end = scan[best_len];
1185 if (s->prev_length >= s->good_match) {
1191 if ((
uInt)nice_match > s->lookahead) nice_match = s->lookahead;
1196 Assert(cur_match < s->strstart,
"no future");
1197 match = s->window + cur_match;
1207 #if (defined(UNALIGNED_OK) && MAX_MATCH == 258) 1211 if (*(
ushf*)(match+best_len-1) != scan_end ||
1212 *(
ushf*)match != scan_start)
continue;
1223 Assert(scan[2] == match[2],
"scan[2]?");
1226 }
while (*(
ushf*)(scan+=2) == *(
ushf*)(match+=2) &&
1227 *(
ushf*)(scan+=2) == *(
ushf*)(match+=2) &&
1228 *(
ushf*)(scan+=2) == *(
ushf*)(match+=2) &&
1229 *(
ushf*)(scan+=2) == *(
ushf*)(match+=2) &&
1234 Assert(scan <= s->window+(
unsigned)(s->window_size-1),
"wild scan");
1235 if (*scan == *match) scan++;
1237 len = (
MAX_MATCH - 1) - (
int)(strend-scan);
1242 if (match[best_len] != scan_end ||
1243 match[best_len-1] != scan_end1 ||
1245 *++match != scan[1])
continue;
1254 Assert(*scan == *match,
"match[2]?");
1260 }
while (*++scan == *++match && *++scan == *++match &&
1261 *++scan == *++match && *++scan == *++match &&
1262 *++scan == *++match && *++scan == *++match &&
1263 *++scan == *++match && *++scan == *++match &&
1266 Assert(scan <= s->window+(
unsigned)(s->window_size-1),
"wild scan");
1273 if (len > best_len) {
1274 s->match_start = cur_match;
1276 if (len >= nice_match)
break;
1278 scan_end = *(
ushf*)(scan+best_len-1);
1280 scan_end1 = scan[best_len-1];
1281 scan_end = scan[best_len];
1284 }
while ((cur_match = prev[cur_match & wmask]) > limit
1285 && --chain_length != 0);
1287 if ((
uInt)best_len <= s->lookahead)
return (
uInt)best_len;
1288 return s->lookahead;
1313 Assert(cur_match < s->strstart,
"no future");
1315 match = s->window + cur_match;
1319 if (match[0] != scan[0] || match[1] != scan[1])
return MIN_MATCH-1;
1327 scan += 2, match += 2;
1328 Assert(*scan == *match,
"match[2]?");
1334 }
while (*++scan == *++match && *++scan == *++match &&
1335 *++scan == *++match && *++scan == *++match &&
1336 *++scan == *++match && *++scan == *++match &&
1337 *++scan == *++match && *++scan == *++match &&
1340 Assert(scan <= s->window+(
unsigned)(s->window_size-1),
"wild scan");
1346 s->match_start = cur_match;
1347 return (
uInt)len <= s->lookahead ? (
uInt)len : s->lookahead;
1362 if (
zmemcmp(s->window + match,
1363 s->window + start, length) !=
EQUAL) {
1364 fprintf(stderr,
" start %u, match %u, length %d\n",
1365 start, match, length);
1367 fprintf(stderr,
"%c%c", s->window[match++], s->window[start++]);
1368 }
while (--length != 0);
1369 z_error(
"invalid match");
1371 if (z_verbose > 1) {
1372 fprintf(stderr,
"\\[%d,%d]", start-match, length);
1373 do { putc(s->window[start++], stderr); }
while (--length != 0);
1377 # define check_match(s, start, match, length) 1393 register unsigned n, m;
1401 more = (unsigned)(s->window_size -(
ulg)s->lookahead -(
ulg)s->strstart);
1404 if (
sizeof(
int) <= 2) {
1405 if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
1408 }
else if (more == (
unsigned)(-1)) {
1419 if (s->strstart >= wsize+
MAX_DIST(s)) {
1421 zmemcpy(s->window, s->window+wsize, (
unsigned)wsize);
1422 s->match_start -= wsize;
1423 s->strstart -= wsize;
1424 s->block_start -= (long) wsize;
1436 *p = (
Pos)(m >= wsize ? m-wsize :
NIL);
1444 *p = (
Pos)(m >= wsize ? m-wsize :
NIL);
1452 if (s->strm->avail_in == 0)
break;
1465 Assert(more >= 2,
"more < 2");
1467 n =
read_buf(s->strm, s->window + s->strstart + s->lookahead, more);
1471 if (s->lookahead + s->insert >=
MIN_MATCH) {
1472 uInt str = s->strstart - s->insert;
1473 s->ins_h = s->window[str];
1481 s->prev[str & s->w_mask] = s->head[s->ins_h];
1483 s->head[s->ins_h] = (
Pos)str;
1486 if (s->lookahead + s->insert <
MIN_MATCH)
1494 }
while (s->lookahead <
MIN_LOOKAHEAD && s->strm->avail_in != 0);
1503 if (s->high_water < s->window_size) {
1504 ulg curr = s->strstart + (
ulg)(s->lookahead);
1507 if (s->high_water < curr) {
1511 init = s->window_size - curr;
1514 zmemzero(s->window + curr, (
unsigned)init);
1515 s->high_water = curr + init;
1523 if (init > s->window_size - s->high_water)
1524 init = s->window_size - s->high_water;
1525 zmemzero(s->window + s->high_water, (
unsigned)init);
1526 s->high_water += init;
1531 "not enough room for search");
1538 #define FLUSH_BLOCK_ONLY(s, last) { \ 1539 _tr_flush_block(s, (s->block_start >= 0L ? \ 1540 (charf *)&s->window[(unsigned)s->block_start] : \ 1542 (ulg)((long)s->strstart - s->block_start), \ 1544 s->block_start = s->strstart; \ 1545 flush_pending(s->strm); \ 1546 Tracev((stderr,"[FLUSH]")); \ 1550 #define FLUSH_BLOCK(s, last) { \ 1551 FLUSH_BLOCK_ONLY(s, last); \ 1552 if (s->strm->avail_out == 0) return (last) ? finish_started : need_more; \ 1571 ulg max_block_size = 0xffff;
1574 if (max_block_size > s->pending_buf_size - 5) {
1581 if (s->lookahead <= 1) {
1584 s->block_start >= (
long)s->w_size,
"slide too late");
1589 if (s->lookahead == 0)
break;
1591 Assert(s->block_start >= 0L,
"block gone");
1593 s->strstart += s->lookahead;
1597 max_start = s->block_start + max_block_size;
1598 if (s->strstart == 0 || (
ulg)s->strstart >= max_start) {
1600 s->lookahead = (
uInt)(s->strstart - max_start);
1601 s->strstart = (
uInt)max_start;
1607 if (s->strstart - (
uInt)s->block_start >=
MAX_DIST(s)) {
1616 if ((
long)s->strstart > s->block_start)
1646 if (s->lookahead == 0)
break;
1660 if (hash_head !=
NIL && s->strstart - hash_head <=
MAX_DIST(s)) {
1669 check_match(s, s->strstart, s->match_start, s->match_length);
1674 s->lookahead -= s->match_length;
1680 if (s->match_length <= s->max_insert_length &&
1689 }
while (--s->match_length != 0);
1694 s->strstart += s->match_length;
1695 s->match_length = 0;
1696 s->ins_h = s->window[s->strstart];
1697 UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
1707 Tracevv((stderr,
"%c", s->window[s->strstart]));
1749 if (s->lookahead == 0)
break;
1762 s->prev_length = s->match_length, s->prev_match = s->match_start;
1765 if (hash_head !=
NIL && s->prev_length < s->max_lazy_match &&
1766 s->strstart - hash_head <=
MAX_DIST(s)) {
1774 if (s->match_length <= 5 && (s->strategy ==
Z_FILTERED 1777 s->strstart - s->match_start >
TOO_FAR)
1790 if (s->prev_length >=
MIN_MATCH && s->match_length <= s->prev_length) {
1794 check_match(s, s->strstart-1, s->prev_match, s->prev_length);
1804 s->lookahead -= s->prev_length-1;
1805 s->prev_length -= 2;
1807 if (++s->strstart <= max_insert) {
1810 }
while (--s->prev_length != 0);
1811 s->match_available = 0;
1817 }
else if (s->match_available) {
1822 Tracevv((stderr,
"%c", s->window[s->strstart-1]));
1829 if (s->strm->avail_out == 0)
return need_more;
1834 s->match_available = 1;
1840 if (s->match_available) {
1841 Tracevv((stderr,
"%c", s->window[s->strstart-1]));
1843 s->match_available = 0;
1867 Bytef *scan, *strend;
1879 if (s->lookahead == 0)
break;
1883 s->match_length = 0;
1884 if (s->lookahead >=
MIN_MATCH && s->strstart > 0) {
1885 scan = s->window + s->strstart - 1;
1887 if (prev == *++scan && prev == *++scan && prev == *++scan) {
1888 strend = s->window + s->strstart +
MAX_MATCH;
1890 }
while (prev == *++scan && prev == *++scan &&
1891 prev == *++scan && prev == *++scan &&
1892 prev == *++scan && prev == *++scan &&
1893 prev == *++scan && prev == *++scan &&
1895 s->match_length =
MAX_MATCH - (int)(strend - scan);
1896 if (s->match_length > s->lookahead)
1897 s->match_length = s->lookahead;
1899 Assert(scan <= s->window+(
uInt)(s->window_size-1),
"wild scan");
1904 check_match(s, s->strstart, s->strstart - 1, s->match_length);
1908 s->lookahead -= s->match_length;
1909 s->strstart += s->match_length;
1910 s->match_length = 0;
1913 Tracevv((stderr,
"%c", s->window[s->strstart]));
1942 if (s->lookahead == 0) {
1944 if (s->lookahead == 0) {
1952 s->match_length = 0;
1953 Tracevv((stderr,
"%c", s->window[s->strstart]));
unsigned char match[65280+2]
static block_state deflate_slow()
int deflateSetDictionary(z_streamp strm, Bytef *dictionary, uInt dictLength)
static void flush_pending()
static block_state deflate_rle()
void zcfree(voidpf opaque, voidpf ptr)
#define INSERT_STRING(s, str, match_head)
static config configuration_table[10]
#define check_match(s, start, match, length)
int deflateResetKeep(z_streamp strm)
int deflateEnd(z_streamp strm)
#define Assert(cond, msg)
#define UPDATE_HASH(s, h, c)
int deflateSetHeader(z_streamp strm, gz_headerp head)
unsigned long crc32(unsigned long crc, unsigned char *buf, uInt len)
static uInt longest_match()
#define _tr_tally_lit(s, c, flush)
#define ZALLOC(strm, items, size)
void zmemcpy(Bytef *dest, Bytef *source, uInt len)
static void putShortMSB()
int zmemcmp(Bytef *s1, Bytef *s2, uInt len)
int deflatePrime(z_streamp strm, int bits, int value)
voidpf zcalloc(voidpf opaque, unsigned items, unsigned size)
static block_state deflate_huff()
int deflateReset(z_streamp strm)
#define ZFREE(strm, addr)
int deflateTune(z_streamp strm, int good_length, int max_lazy, int nice_length, int max_chain)
uLong adler32(uLong adler, Bytef *buf, uInt len)
int deflateInit2_(z_streamp strm, int level, int method, int windowBits, int memLevel, int strategy, char *version, int stream_size)
static block_state deflate_fast()
#define _tr_tally_dist(s, distance, length, flush)
int deflateCopy(z_streamp dest, z_streamp source)
static void fill_window()
uLong deflateBound(z_streamp strm, uLong sourceLen)
#define ERR_RETURN(strm, err)
#define FLUSH_BLOCK(s, last)
static int bits(struct state *s, int need)
int deflateParams(z_streamp strm, int level, int strategy)
int deflateInit_(z_streamp strm, int level, char *version, int stream_size)
#define Z_DEFAULT_STRATEGY
struct internal_state * state
block_state(* compress_func)()
static block_state deflate_stored()
void zmemzero(Bytef *dest, uInt len)
int deflate(z_streamp strm, int flush)
#define Z_DEFAULT_COMPRESSION
int deflatePending(z_streamp strm, unsigned *pending, int *bits)
#define FLUSH_BLOCK_ONLY(s, last)