55 " deflate 1.2.7 Copyright 1995-2012 Jean-loup Gailly and Mark Adler ";
89 void match_init
OF((
void));
108 # define TOO_FAR 4096
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));
233 if (version ==
Z_NULL || version[0] != my_version[0] ||
240 if (strm->zalloc == (alloc_func)0) {
248 if (strm->zfree == (free_func)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;
362 strm->next_in = (
Bytef *)dictionary;
385 strm->next_in = next;
386 strm->avail_in = avail;
398 strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0) {
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) {
517 if (s->
level != level) {
569 uLong complen, wraplen;
573 complen = sourceLen +
574 ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 5;
587 wraplen = 6 + (s->
strstart ? 4 : 0);
593 wraplen += 2 + s->
gzhead->extra_len;
614 return complen + wraplen;
617 return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) +
618 (sourceLen >> 25) + 13 - 6 + wraplen;
648 if (len > strm->avail_out) len = strm->avail_out;
649 if (len == 0)
return;
652 strm->next_out += len;
654 strm->total_out += len;
655 strm->avail_out -= len;
671 flush >
Z_BLOCK || flush < 0) {
676 if (strm->next_out ==
Z_NULL ||
677 (strm->next_in ==
Z_NULL && strm->avail_in != 0) ||
709 (s->
gzhead->hcrc ? 2 : 0) +
741 else if (s->
level < 6)
743 else if (s->
level == 6)
747 header |= (level_flags << 6);
749 header += 31 - (header % 31);
870 if (strm->avail_out == 0) {
885 }
else if (strm->avail_in == 0 &&
RANK(flush) <=
RANK(old_flush) &&
897 if (strm->avail_in != 0 || s->
lookahead != 0 ||
903 (*(configuration_table[s->
level].func))(s, flush));
909 if (strm->avail_out == 0) {
939 if (strm->avail_out == 0) {
945 Assert(strm->avail_out > 0,
"bug2");
984 status = strm->state->status;
996 TRY_FREE(strm, strm->state->pending_buf);
999 TRY_FREE(strm, strm->state->window);
1001 ZFREE(strm, strm->state);
1040 ds->head = (
Posf *)
ZALLOC(dest, ds->hash_size,
sizeof(
Pos));
1041 overlay = (
ushf *)
ZALLOC(dest, ds->lit_bufsize,
sizeof(
ush)+2);
1042 ds->pending_buf = (
uchf *) overlay;
1045 ds->pending_buf ==
Z_NULL) {
1050 zmemcpy(ds->window, ss->window, ds->w_size * 2 *
sizeof(
Byte));
1053 zmemcpy(ds->pending_buf, ss->pending_buf, (
uInt)ds->pending_buf_size);
1055 ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf);
1056 ds->d_buf = overlay + ds->lit_bufsize/
sizeof(
ush);
1057 ds->l_buf = ds->pending_buf + (1+
sizeof(
ush))*ds->lit_bufsize;
1059 ds->l_desc.dyn_tree = ds->dyn_ltree;
1060 ds->d_desc.dyn_tree = ds->dyn_dtree;
1061 ds->bl_desc.dyn_tree = ds->bl_tree;
1079 unsigned len = strm->avail_in;
1081 if (len > size) len = size;
1082 if (len == 0)
return 0;
1084 strm->avail_in -= len;
1086 zmemcpy(buf, strm->next_in, len);
1087 if (strm->state->wrap == 1) {
1088 strm->adler =
adler32(strm->adler, buf, len);
1091 else if (strm->state->wrap == 2) {
1092 strm->adler =
crc32(strm->adler, buf, len);
1095 strm->next_in += len;
1096 strm->total_in += len;
1113 s->max_lazy_match = configuration_table[s->level].max_lazy;
1114 s->good_match = configuration_table[s->level].good_length;
1115 s->nice_match = configuration_table[s->level].nice_length;
1116 s->max_chain_length = configuration_table[s->level].max_chain;
1119 s->block_start = 0L;
1122 s->match_length = s->prev_length =
MIN_MATCH-1;
1123 s->match_available = 0;
1151 register Bytef *
scan = s->window + s->strstart;
1154 int best_len = s->prev_length;
1162 uInt wmask = s->w_mask;
1168 register Bytef *strend = s->window + s->strstart +
MAX_MATCH - 1;
1169 register ush scan_start = *(
ushf*)scan;
1170 register ush scan_end = *(
ushf*)(scan+best_len-1);
1173 register Byte scan_end1 = scan[best_len-1];
1174 register Byte scan_end = scan[best_len];
1183 if (s->prev_length >= s->good_match) {
1189 if ((
uInt)nice_match > s->lookahead) nice_match = s->lookahead;
1195 match = s->window + cur_match;
1205 #if (defined(UNALIGNED_OK) && MAX_MATCH == 258)
1209 if (*(
ushf*)(match+best_len-1) != scan_end ||
1210 *(
ushf*)match != scan_start)
continue;
1221 Assert(scan[2] == match[2],
"scan[2]?");
1224 }
while (*(
ushf*)(scan+=2) == *(
ushf*)(match+=2) &&
1225 *(
ushf*)(scan+=2) == *(
ushf*)(match+=2) &&
1226 *(
ushf*)(scan+=2) == *(
ushf*)(match+=2) &&
1227 *(
ushf*)(scan+=2) == *(
ushf*)(match+=2) &&
1232 Assert(scan <= s->
window+(
unsigned)(s->window_size-1),
"wild scan");
1233 if (*scan == *match) scan++;
1240 if (match[best_len] != scan_end ||
1241 match[best_len-1] != scan_end1 ||
1243 *++match != scan[1])
continue;
1252 Assert(*scan == *match,
"match[2]?");
1258 }
while (*++scan == *++match && *++scan == *++match &&
1259 *++scan == *++match && *++scan == *++match &&
1260 *++scan == *++match && *++scan == *++match &&
1261 *++scan == *++match && *++scan == *++match &&
1264 Assert(scan <= s->
window+(
unsigned)(s->window_size-1),
"wild scan");
1271 if (len > best_len) {
1272 s->match_start = cur_match;
1274 if (len >= nice_match)
break;
1276 scan_end = *(
ushf*)(scan+best_len-1);
1278 scan_end1 = scan[best_len-1];
1279 scan_end = scan[best_len];
1282 }
while ((cur_match = prev[cur_match & wmask]) > limit
1283 && --chain_length != 0);
1285 if ((
uInt)best_len <= s->lookahead)
return (
uInt)best_len;
1286 return s->lookahead;
1299 register Bytef *
scan = s->window + s->strstart;
1313 match = s->window + cur_match;
1317 if (match[0] != scan[0] || match[1] != scan[1])
return MIN_MATCH-1;
1325 scan += 2, match += 2;
1326 Assert(*scan == *match,
"match[2]?");
1332 }
while (*++scan == *++match && *++scan == *++match &&
1333 *++scan == *++match && *++scan == *++match &&
1334 *++scan == *++match && *++scan == *++match &&
1335 *++scan == *++match && *++scan == *++match &&
1338 Assert(scan <= s->
window+(
unsigned)(s->window_size-1),
"wild scan");
1344 s->match_start = cur_match;
1345 return (
uInt)len <= s->lookahead ? (
uInt)len : s->lookahead;
1360 if (
zmemcmp(s->window + match,
1361 s->window + start, length) !=
EQUAL) {
1362 fprintf(stderr,
" start %u, match %u, length %d\n",
1363 start, match, length);
1365 fprintf(stderr,
"%c%c", s->window[match++], s->window[start++]);
1366 }
while (--length != 0);
1367 z_error(
"invalid match");
1369 if (z_verbose > 1) {
1370 fprintf(stderr,
"\\[%d,%d]", start-match, length);
1371 do { putc(s->window[start++], stderr); }
while (--length != 0);
1375 # define check_match(s, start, match, length)
1391 register unsigned n, m;
1399 more = (unsigned)(s->window_size -(
ulg)s->lookahead -(
ulg)s->strstart);
1402 if (
sizeof(
int) <= 2) {
1403 if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
1406 }
else if (more == (
unsigned)(-1)) {
1417 if (s->strstart >= wsize+
MAX_DIST(s)) {
1419 zmemcpy(s->window, s->window+wsize, (
unsigned)wsize);
1420 s->match_start -= wsize;
1421 s->strstart -= wsize;
1422 s->block_start -= (long) wsize;
1434 *p = (
Pos)(m >= wsize ? m-wsize :
NIL);
1442 *p = (
Pos)(m >= wsize ? m-wsize :
NIL);
1450 if (s->strm->avail_in == 0)
break;
1463 Assert(more >= 2,
"more < 2");
1465 n =
read_buf(s->strm, s->window + s->strstart + s->lookahead, more);
1469 if (s->lookahead + s->insert >=
MIN_MATCH) {
1470 uInt str = s->strstart - s->insert;
1471 s->ins_h = s->window[str];
1479 s->prev[str & s->w_mask] = s->head[s->ins_h];
1481 s->head[s->ins_h] = (
Pos)str;
1484 if (s->lookahead + s->insert <
MIN_MATCH)
1492 }
while (s->lookahead <
MIN_LOOKAHEAD && s->strm->avail_in != 0);
1501 if (s->high_water < s->window_size) {
1502 ulg curr = s->strstart + (
ulg)(s->lookahead);
1505 if (s->high_water < curr) {
1509 init = s->window_size - curr;
1512 zmemzero(s->window + curr, (
unsigned)init);
1513 s->high_water = curr + init;
1521 if (init > s->window_size - s->high_water)
1522 init = s->window_size - s->high_water;
1523 zmemzero(s->window + s->high_water, (
unsigned)init);
1524 s->high_water += init;
1529 "not enough room for search");
1536 #define FLUSH_BLOCK_ONLY(s, last) { \
1537 _tr_flush_block(s, (s->block_start >= 0L ? \
1538 (charf *)&s->window[(unsigned)s->block_start] : \
1540 (ulg)((long)s->strstart - s->block_start), \
1542 s->block_start = s->strstart; \
1543 flush_pending(s->strm); \
1544 Tracev((stderr,"[FLUSH]")); \
1548 #define FLUSH_BLOCK(s, last) { \
1549 FLUSH_BLOCK_ONLY(s, last); \
1550 if (s->strm->avail_out == 0) return (last) ? finish_started : need_more; \
1569 ulg max_block_size = 0xffff;
1572 if (max_block_size > s->pending_buf_size - 5) {
1579 if (s->lookahead <= 1) {
1582 s->block_start >= (
long)s->w_size,
"slide too late");
1587 if (s->lookahead == 0)
break;
1589 Assert(s->block_start >= 0L,
"block gone");
1591 s->strstart += s->lookahead;
1595 max_start = s->block_start + max_block_size;
1596 if (s->strstart == 0 || (
ulg)s->strstart >= max_start) {
1598 s->lookahead = (
uInt)(s->strstart - max_start);
1599 s->strstart = (
uInt)max_start;
1605 if (s->strstart - (
uInt)s->block_start >=
MAX_DIST(s)) {
1614 if ((
long)s->strstart > s->block_start)
1644 if (s->lookahead == 0)
break;
1658 if (hash_head !=
NIL && s->strstart - hash_head <=
MAX_DIST(s)) {
1667 check_match(s, s->strstart, s->match_start, s->match_length);
1672 s->lookahead -= s->match_length;
1678 if (s->match_length <= s->max_insert_length &&
1687 }
while (--s->match_length != 0);
1692 s->strstart += s->match_length;
1693 s->match_length = 0;
1694 s->ins_h = s->window[s->strstart];
1695 UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
1705 Tracevv((stderr,
"%c", s->window[s->strstart]));
1747 if (s->lookahead == 0)
break;
1760 s->prev_length = s->match_length, s->prev_match = s->match_start;
1763 if (hash_head !=
NIL && s->prev_length < s->max_lazy_match &&
1764 s->strstart - hash_head <=
MAX_DIST(s)) {
1772 if (s->match_length <= 5 && (s->strategy ==
Z_FILTERED
1775 s->strstart - s->match_start >
TOO_FAR)
1788 if (s->prev_length >=
MIN_MATCH && s->match_length <= s->prev_length) {
1792 check_match(s, s->strstart-1, s->prev_match, s->prev_length);
1802 s->lookahead -= s->prev_length-1;
1803 s->prev_length -= 2;
1805 if (++s->strstart <= max_insert) {
1808 }
while (--s->prev_length != 0);
1809 s->match_available = 0;
1815 }
else if (s->match_available) {
1820 Tracevv((stderr,
"%c", s->window[s->strstart-1]));
1827 if (s->strm->avail_out == 0)
return need_more;
1832 s->match_available = 1;
1838 if (s->match_available) {
1839 Tracevv((stderr,
"%c", s->window[s->strstart-1]));
1841 s->match_available = 0;
1877 if (s->lookahead == 0)
break;
1881 s->match_length = 0;
1882 if (s->lookahead >=
MIN_MATCH && s->strstart > 0) {
1883 scan = s->window + s->strstart - 1;
1885 if (prev == *++scan && prev == *++scan && prev == *++scan) {
1886 strend = s->window + s->strstart +
MAX_MATCH;
1888 }
while (prev == *++scan && prev == *++scan &&
1889 prev == *++scan && prev == *++scan &&
1890 prev == *++scan && prev == *++scan &&
1891 prev == *++scan && prev == *++scan &&
1893 s->match_length =
MAX_MATCH - (int)(strend - scan);
1894 if (s->match_length > s->lookahead)
1895 s->match_length = s->lookahead;
1902 check_match(s, s->strstart, s->strstart - 1, s->match_length);
1906 s->lookahead -= s->match_length;
1907 s->strstart += s->match_length;
1908 s->match_length = 0;
1911 Tracevv((stderr,
"%c", s->window[s->strstart]));
1940 if (s->lookahead == 0) {
1942 if (s->lookahead == 0) {
1950 s->match_length = 0;
1951 Tracevv((stderr,
"%c", s->window[s->strstart]));