| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
#include "zbuild.h" |
| 8 |
#include "deflate.h" |
| 9 |
#include "deflate_p.h" |
| 10 |
#include "functable.h" |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
Z_INTERNAL block_state deflate_slow(deflate_state *s, int flush) { |
| 18 |
Pos hash_head; |
| 19 |
int bflush; |
| 20 |
int64_t dist; |
| 21 |
uint32_t match_len; |
| 22 |
match_func *longest_match; |
| 23 |
|
| 24 |
if (s->max_chain_length <= 1024) |
| 25 |
longest_match = &functable.longest_match; |
| 26 |
else |
| 27 |
longest_match = &functable.longest_match_slow; |
| 28 |
|
| 29 |
|
| 30 |
for (;;) { |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
if (s->lookahead < MIN_LOOKAHEAD) { |
| 37 |
fill_window(s); |
| 38 |
if (UNLIKELY(s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH)) { |
| 39 |
return need_more; |
| 40 |
} |
| 41 |
if (UNLIKELY(s->lookahead == 0)) |
| 42 |
break; |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
hash_head = 0; |
| 49 |
if (LIKELY(s->lookahead >= WANT_MIN_MATCH)) { |
| 50 |
hash_head = s->quick_insert_string(s, s->strstart); |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
s->prev_match = (Pos)s->match_start; |
| 56 |
match_len = STD_MIN_MATCH - 1; |
| 57 |
dist = (int64_t)s->strstart - hash_head; |
| 58 |
|
| 59 |
if (dist <= MAX_DIST(s) && dist > 0 && s->prev_length < s->max_lazy_match && hash_head != 0) { |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
match_len = (*longest_match)(s, hash_head); |
| 65 |
|
| 66 |
|
| 67 |
if (match_len <= 5 && (s->strategy == Z_FILTERED)) { |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
match_len = STD_MIN_MATCH - 1; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
if (s->prev_length >= STD_MIN_MATCH && match_len <= s->prev_length) { |
| 78 |
unsigned int max_insert = s->strstart + s->lookahead - STD_MIN_MATCH; |
| 79 |
|
| 80 |
|
| 81 |
check_match(s, s->strstart-1, s->prev_match, s->prev_length); |
| 82 |
|
| 83 |
bflush = zng_tr_tally_dist(s, s->strstart -1 - s->prev_match, s->prev_length - STD_MIN_MATCH); |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
s->prev_length -= 1; |
| 91 |
s->lookahead -= s->prev_length; |
| 92 |
|
| 93 |
unsigned int mov_fwd = s->prev_length - 1; |
| 94 |
if (max_insert > s->strstart) { |
| 95 |
unsigned int insert_cnt = mov_fwd; |
| 96 |
if (UNLIKELY(insert_cnt > max_insert - s->strstart)) |
| 97 |
insert_cnt = max_insert - s->strstart; |
| 98 |
s->insert_string(s, s->strstart + 1, insert_cnt); |
| 99 |
} |
| 100 |
s->prev_length = 0; |
| 101 |
s->match_available = 0; |
| 102 |
s->strstart += mov_fwd + 1; |
| 103 |
|
| 104 |
if (UNLIKELY(bflush)) |
| 105 |
FLUSH_BLOCK(s, 0); |
| 106 |
|
| 107 |
} else if (s->match_available) { |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
bflush = zng_tr_tally_lit(s, s->window[s->strstart-1]); |
| 113 |
if (UNLIKELY(bflush)) |
| 114 |
FLUSH_BLOCK_ONLY(s, 0); |
| 115 |
s->prev_length = match_len; |
| 116 |
s->strstart++; |
| 117 |
s->lookahead--; |
| 118 |
if (UNLIKELY(s->strm->avail_out == 0)) |
| 119 |
return need_more; |
| 120 |
} else { |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
s->prev_length = match_len; |
| 125 |
s->match_available = 1; |
| 126 |
s->strstart++; |
| 127 |
s->lookahead--; |
| 128 |
} |
| 129 |
} |
| 130 |
Assert(flush != Z_NO_FLUSH, "no flush?"); |
| 131 |
if (UNLIKELY(s->match_available)) { |
| 132 |
(void) zng_tr_tally_lit(s, s->window[s->strstart-1]); |
| 133 |
s->match_available = 0; |
| 134 |
} |
| 135 |
s->insert = s->strstart < (STD_MIN_MATCH - 1) ? s->strstart : (STD_MIN_MATCH - 1); |
| 136 |
if (UNLIKELY(flush == Z_FINISH)) { |
| 137 |
FLUSH_BLOCK(s, 1); |
| 138 |
return finish_done; |
| 139 |
} |
| 140 |
if (UNLIKELY(s->sym_next)) |
| 141 |
FLUSH_BLOCK(s, 0); |
| 142 |
return block_done; |
| 143 |
} |