9
#ifndef NO_MEDIUM_STRATEGY
17
uint16_t match_length;
22
static int emit_match(deflate_state *s, struct match match) {
26
if (match.match_length < WANT_MIN_MATCH) {
27
while (match.match_length) {
28
bflush += zng_tr_tally_lit(s, s->window[match.strstart]);
36
check_match(s, match.strstart, match.match_start, match.match_length);
38
bflush += zng_tr_tally_dist(s, match.strstart - match.match_start, match.match_length - STD_MIN_MATCH);
40
s->lookahead -= match.match_length;
44
static void insert_match(deflate_state *s, struct match match) {
45
if (UNLIKELY(s->lookahead <= (unsigned int)(match.match_length + WANT_MIN_MATCH)))
49
if (LIKELY(match.match_length < WANT_MIN_MATCH)) {
52
if (UNLIKELY(match.match_length > 0)) {
53
if (match.strstart >= match.orgstart) {
54
if (match.strstart + match.match_length - 1 >= match.orgstart) {
55
functable.insert_string(s, match.strstart, match.match_length);
57
functable.insert_string(s, match.strstart, match.orgstart - match.strstart + 1);
59
match.strstart += match.match_length;
60
match.match_length = 0;
69
if (match.match_length <= 16 * s->max_insert_length && s->lookahead >= WANT_MIN_MATCH) {
73
if (LIKELY(match.strstart >= match.orgstart)) {
74
if (LIKELY(match.strstart + match.match_length - 1 >= match.orgstart)) {
75
functable.insert_string(s, match.strstart, match.match_length);
77
functable.insert_string(s, match.strstart, match.orgstart - match.strstart + 1);
79
} else if (match.orgstart < match.strstart + match.match_length) {
80
functable.insert_string(s, match.orgstart, match.strstart + match.match_length - match.orgstart);
82
match.strstart += match.match_length;
83
match.match_length = 0;
85
match.strstart += match.match_length;
86
match.match_length = 0;
88
if (match.strstart >= (STD_MIN_MATCH - 2))
89
functable.quick_insert_string(s, match.strstart + 2 - STD_MIN_MATCH);
97
static void fizzle_matches(deflate_state *s, struct match *current, struct match *next) {
99
unsigned char *match, *orig;
104
if (current->match_length <= 1)
107
if (UNLIKELY(current->match_length > 1 + next->match_start))
110
if (UNLIKELY(current->match_length > 1 + next->strstart))
113
match = s->window - current->match_length + 1 + next->match_start;
114
orig = s->window - current->match_length + 1 + next->strstart;
117
if (LIKELY(*match != *orig))
124
limit = next->strstart > MAX_DIST(s) ? next->strstart - (Pos)MAX_DIST(s) : 0;
126
match = s->window + n.match_start - 1;
127
orig = s->window + n.strstart - 1;
129
while (*match == *orig) {
130
if (UNLIKELY(c.match_length < 1))
132
if (UNLIKELY(n.strstart <= limit))
134
if (UNLIKELY(n.match_length >= 256))
136
if (UNLIKELY(n.match_start <= 1))
151
if (c.match_length <= 1 && n.match_length != 2) {
160
Z_INTERNAL block_state deflate_medium(deflate_state *s, int flush) {
162
ALIGNED_(16) struct match current_match;
163
struct match next_match;
166
int early_exit = s->level < 5;
168
memset(¤t_match, 0, sizeof(struct match));
169
memset(&next_match, 0, sizeof(struct match));
181
if (s->lookahead < MIN_LOOKAHEAD) {
182
PREFIX(fill_window)(s);
183
if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
186
if (UNLIKELY(s->lookahead == 0))
188
next_match.match_length = 0;
196
if (!early_exit && next_match.match_length > 0) {
197
current_match = next_match;
198
next_match.match_length = 0;
201
if (s->lookahead >= WANT_MIN_MATCH) {
202
hash_head = functable.quick_insert_string(s, s->strstart);
205
current_match.strstart = (uint16_t)s->strstart;
206
current_match.orgstart = current_match.strstart;
212
dist = (int64_t)s->strstart - hash_head;
213
if (dist <= MAX_DIST(s) && dist > 0 && hash_head != 0) {
218
current_match.match_length = (uint16_t)functable.longest_match(s, hash_head);
219
current_match.match_start = (uint16_t)s->match_start;
220
if (UNLIKELY(current_match.match_length < WANT_MIN_MATCH))
221
current_match.match_length = 1;
222
if (UNLIKELY(current_match.match_start >= current_match.strstart)) {
224
current_match.match_length = 1;
228
current_match.match_start = 0;
229
current_match.match_length = 1;
233
insert_match(s, current_match);
236
if (LIKELY(!early_exit && s->lookahead > MIN_LOOKAHEAD && (uint32_t)(current_match.strstart + current_match.match_length) < (s->window_size - MIN_LOOKAHEAD))) {
237
s->strstart = current_match.strstart + current_match.match_length;
238
hash_head = functable.quick_insert_string(s, s->strstart);
240
next_match.strstart = (uint16_t)s->strstart;
241
next_match.orgstart = next_match.strstart;
247
dist = (int64_t)s->strstart - hash_head;
248
if (dist <= MAX_DIST(s) && dist > 0 && hash_head != 0) {
253
next_match.match_length = (uint16_t)functable.longest_match(s, hash_head);
254
next_match.match_start = (uint16_t)s->match_start;
255
if (UNLIKELY(next_match.match_start >= next_match.strstart)) {
257
next_match.match_length = 1;
259
if (next_match.match_length < WANT_MIN_MATCH)
260
next_match.match_length = 1;
262
fizzle_matches(s, ¤t_match, &next_match);
265
next_match.match_start = 0;
266
next_match.match_length = 1;
269
s->strstart = current_match.strstart;
271
next_match.match_length = 0;
275
bflush = emit_match(s, current_match);
278
s->strstart += current_match.match_length;
280
if (UNLIKELY(bflush))
283
s->insert = s->strstart < (STD_MIN_MATCH - 1) ? s->strstart : (STD_MIN_MATCH - 1);
284
if (flush == Z_FINISH) {
288
if (UNLIKELY(s->sym_next))