55 " deflate 1.2.3 Copyright 1995-2005 Jean-loup Gailly ";
88 void match_init
OF((
void));
109 # define TOO_FAR 4096
113 #define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
160 #ifndef NO_DUMMY_DECL
170 #define UPDATE_HASH(s,h,c) (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask)
184 #define INSERT_STRING(s, str, match_head) \
185 (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
186 match_head = s->head[s->ins_h], \
187 s->head[s->ins_h] = (Pos)(str))
189 #define INSERT_STRING(s, str, match_head) \
190 (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
191 match_head = s->prev[(str) & s->w_mask] = s->head[s->ins_h], \
192 s->head[s->ins_h] = (Pos)(str))
199 #define CLEAR_HASH(s) \
200 s->head[s->hash_size-1] = NIL; \
201 zmemzero((Bytef *)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head));
243 if (strm->zalloc == (alloc_func)0) {
247 if (strm->zfree == (free_func)0) strm->zfree =
zcfree;
255 if (windowBits < 0) {
257 windowBits = -windowBits;
260 else if (windowBits > 15) {
266 windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
267 strategy < 0 || strategy >
Z_FIXED) {
270 if (windowBits == 8) windowBits = 9;
326 strm->state->wrap == 2 ||
352 if (hash_head) hash_head = 0;
363 strm->zalloc == (alloc_func)0 ||
strm->zfree == (free_func)0) {
367 strm->total_in =
strm->total_out = 0;
410 strm->state->bi_valid = bits;
433 if (level < 0 || level > 9 || strategy < 0 || strategy >
Z_FIXED) {
497 destLen = sourceLen +
498 ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 11;
535 unsigned len =
strm->state->pending;
537 if (len >
strm->avail_out) len =
strm->avail_out;
538 if (len == 0)
return;
541 strm->next_out += len;
542 strm->state->pending_out += len;
543 strm->total_out += len;
544 strm->avail_out -= len;
545 strm->state->pending -= len;
546 if (
strm->state->pending == 0) {
547 strm->state->pending_out =
strm->state->pending_buf;
598 (s->
gzhead->hcrc ? 2 : 0) +
611 if (s->
gzhead->extra != NULL) {
630 else if (s->
level < 6)
632 else if (s->
level == 6)
636 header |= (level_flags << 6);
638 header += 31 - (header % 31);
653 if (s->
gzhead->extra != NULL) {
681 if (s->
gzhead->name != NULL) {
712 if (s->
gzhead->comment != NULL) {
759 if (
strm->avail_out == 0) {
774 }
else if (
strm->avail_in == 0 && flush <= old_flush &&
796 if (
strm->avail_out == 0) {
821 if (
strm->avail_out == 0) {
961 unsigned len =
strm->avail_in;
964 if (len == 0)
return 0;
966 strm->avail_in -= len;
968 if (
strm->state->wrap == 1) {
972 else if (
strm->state->wrap == 2) {
977 strm->next_in += len;
978 strm->total_in += len;
1001 s->block_start = 0L;
1003 s->match_length = s->prev_length =
MIN_MATCH-1;
1004 s->match_available = 0;
1032 register Bytef *scan = s->window + s->strstart;
1033 register Bytef *match;
1035 int best_len = s->prev_length;
1043 uInt wmask = s->w_mask;
1049 register Bytef *strend = s->window + s->strstart +
MAX_MATCH - 1;
1050 register ush scan_start = *(
ushf*)scan;
1051 register ush scan_end = *(
ushf*)(scan+best_len-1);
1054 register Byte scan_end1 = scan[best_len-1];
1055 register Byte scan_end = scan[best_len];
1064 if (s->prev_length >= s->good_match) {
1076 match = s->window + cur_match;
1086 #if (defined(UNALIGNED_OK) && MAX_MATCH == 258)
1090 if (*(
ushf*)(match+best_len-1) != scan_end ||
1091 *(
ushf*)match != scan_start)
continue;
1102 Assert(scan[2] == match[2],
"scan[2]?");
1105 }
while (*(
ushf*)(scan+=2) == *(
ushf*)(match+=2) &&
1106 *(
ushf*)(scan+=2) == *(
ushf*)(match+=2) &&
1107 *(
ushf*)(scan+=2) == *(
ushf*)(match+=2) &&
1108 *(
ushf*)(scan+=2) == *(
ushf*)(match+=2) &&
1113 Assert(scan <= s->
window+(
unsigned)(s->window_size-1),
"wild scan");
1114 if (*scan == *match) scan++;
1116 len = (
MAX_MATCH - 1) - (
int)(strend-scan);
1121 if (match[best_len] != scan_end ||
1122 match[best_len-1] != scan_end1 ||
1124 *++match != scan[1])
continue;
1133 Assert(*scan == *match,
"match[2]?");
1139 }
while (*++scan == *++match && *++scan == *++match &&
1140 *++scan == *++match && *++scan == *++match &&
1141 *++scan == *++match && *++scan == *++match &&
1142 *++scan == *++match && *++scan == *++match &&
1145 Assert(scan <= s->
window+(
unsigned)(s->window_size-1),
"wild scan");
1152 if (len > best_len) {
1153 s->match_start = cur_match;
1157 scan_end = *(
ushf*)(scan+best_len-1);
1159 scan_end1 = scan[best_len-1];
1160 scan_end = scan[best_len];
1163 }
while ((cur_match =
prev[cur_match & wmask]) > limit
1164 && --chain_length != 0);
1166 if ((
uInt)best_len <= s->lookahead)
return (
uInt)best_len;
1167 return s->lookahead;
1180 register Bytef *match;
1193 match = s->window + cur_match;
1197 if (match[0] != scan[0] || match[1] != scan[1])
return MIN_MATCH-1;
1205 scan += 2, match += 2;
1206 Assert(*scan == *match,
"match[2]?");
1212 }
while (*++scan == *++match && *++scan == *++match &&
1213 *++scan == *++match && *++scan == *++match &&
1214 *++scan == *++match && *++scan == *++match &&
1215 *++scan == *++match && *++scan == *++match &&
1218 Assert(scan <= s->
window+(
unsigned)(s->window_size-1),
"wild scan");
1224 s->match_start = cur_match;
1225 return (
uInt)len <= s->lookahead ? (
uInt)len : s->lookahead;
1238 if (
zmemcmp(s->window + match,
1240 fprintf(stderr,
" start %u, match %u, length %d\n",
1243 fprintf(stderr,
"%c%c", s->window[match++], s->window[
start++]);
1245 z_error(
"invalid match");
1247 if (z_verbose > 1) {
1249 do { putc(s->window[
start++], stderr); }
while (--
length != 0);
1253 # define check_match(s, start, match, length)
1269 register unsigned n, m;
1272 uInt wsize = s->w_size;
1275 more = (unsigned)(s->window_size -(
ulg)s->lookahead -(
ulg)s->strstart);
1278 if (
sizeof(
int) <= 2) {
1279 if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
1282 }
else if (more == (
unsigned)(-1)) {
1293 if (s->strstart >= wsize+
MAX_DIST(s)) {
1295 zmemcpy(s->window, s->window+wsize, (
unsigned)wsize);
1296 s->match_start -= wsize;
1297 s->strstart -= wsize;
1298 s->block_start -= (long) wsize;
1311 *p = (
Pos)(m >= wsize ? m-wsize :
NIL);
1319 *p = (
Pos)(m >= wsize ? m-wsize :
NIL);
1327 if (s->strm->avail_in == 0)
return;
1340 Assert(more >= 2,
"more < 2");
1342 n =
read_buf(s->strm, s->window + s->strstart + s->lookahead, more);
1347 s->ins_h = s->window[s->strstart];
1348 UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
1357 }
while (s->lookahead <
MIN_LOOKAHEAD && s->strm->avail_in != 0);
1364 #define FLUSH_BLOCK_ONLY(s, eof) { \
1365 _tr_flush_block(s, (s->block_start >= 0L ? \
1366 (charf *)&s->window[(unsigned)s->block_start] : \
1368 (ulg)((long)s->strstart - s->block_start), \
1370 s->block_start = s->strstart; \
1371 flush_pending(s->strm); \
1372 Tracev((stderr,"[FLUSH]")); \
1376 #define FLUSH_BLOCK(s, eof) { \
1377 FLUSH_BLOCK_ONLY(s, eof); \
1378 if (s->strm->avail_out == 0) return (eof) ? finish_started : need_more; \
1397 ulg max_block_size = 0xffff;
1400 if (max_block_size > s->pending_buf_size - 5) {
1407 if (s->lookahead <= 1) {
1410 s->block_start >= (
long)s->w_size,
"slide too late");
1415 if (s->lookahead == 0)
break;
1417 Assert(s->block_start >= 0L,
"block gone");
1419 s->strstart += s->lookahead;
1423 max_start = s->block_start + max_block_size;
1424 if (s->strstart == 0 || (
ulg)s->strstart >= max_start) {
1426 s->lookahead = (
uInt)(s->strstart - max_start);
1427 s->strstart = (
uInt)max_start;
1433 if (s->strstart - (
uInt)s->block_start >=
MAX_DIST(s)) {
1466 if (s->lookahead == 0)
break;
1479 if (hash_head !=
NIL && s->strstart - hash_head <=
MAX_DIST(s)) {
1486 (s->strategy ==
Z_RLE && s->strstart - hash_head == 1)) {
1492 }
else if (s->strategy ==
Z_RLE && s->strstart - hash_head == 1) {
1499 check_match(s, s->strstart, s->match_start, s->match_length);
1504 s->lookahead -= s->match_length;
1510 if (s->match_length <= s->max_insert_length &&
1519 }
while (--s->match_length != 0);
1524 s->strstart += s->match_length;
1525 s->match_length = 0;
1526 s->ins_h = s->window[s->strstart];
1527 UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
1537 Tracevv((stderr,
"%c", s->window[s->strstart]));
1573 if (s->lookahead == 0)
break;
1585 s->prev_length = s->match_length, s->prev_match = s->match_start;
1588 if (hash_head !=
NIL && s->prev_length < s->max_lazy_match &&
1589 s->strstart - hash_head <=
MAX_DIST(s)) {
1596 }
else if (s->strategy ==
Z_RLE && s->strstart - hash_head == 1) {
1601 if (s->match_length <= 5 && (s->strategy ==
Z_FILTERED
1604 s->strstart - s->match_start >
TOO_FAR)
1617 if (s->prev_length >=
MIN_MATCH && s->match_length <= s->prev_length) {
1621 check_match(s, s->strstart-1, s->prev_match, s->prev_length);
1631 s->lookahead -= s->prev_length-1;
1632 s->prev_length -= 2;
1634 if (++s->strstart <= max_insert) {
1637 }
while (--s->prev_length != 0);
1638 s->match_available = 0;
1644 }
else if (s->match_available) {
1649 Tracevv((stderr,
"%c", s->window[s->strstart-1]));
1656 if (s->strm->avail_out == 0)
return need_more;
1661 s->match_available = 1;
1667 if (s->match_available) {
1668 Tracevv((stderr,
"%c", s->window[s->strstart-1]));
1670 s->match_available = 0;
1703 if (s->lookahead == 0)
break;
1708 if (s->strstart > 0) {
1710 scan = s->window + s->strstart - 1;
1713 if (*scan++ !=
prev)
1715 }
while (++run <
max);
1720 check_match(s, s->strstart, s->strstart - 1, run);
1722 s->lookahead -= run;
1726 Tracevv((stderr,
"%c", s->window[s->strstart]));