Coverage Report

Created: 2026-11-23 06:53

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/zlib-ng/deflate_medium_tpl.h
Line
Count
Source
1
/* deflate_medium_tpl.h -- The deflate_medium deflate strategy
2
 *
3
 * Copyright (C) 2013 Intel Corporation. All rights reserved.
4
 * Authors:
5
 *  Arjan van de Ven    <arjan@linux.intel.com>
6
 *
7
 * Copyright (C) 2026 Hans Kristian Rosbach
8
 *
9
 * For conditions of distribution and use, see copyright notice in zlib.h
10
 */
11
12
/* insert_match assumes:
13
 * - s->lookahead > match.match_length + WANT_MIN_MATCH
14
 * - match_len >= WANT_MIN_MATCH
15
 */
16
2.49M
Z_FORCEINLINE static void SUFFIX(insert_match)(deflate_state *s, unsigned char *Z_RESTRICT window, struct match match, uint32_t max_insert_count) {
17
2.49M
    uint32_t strstart = match.strstart + 1;        // string at strstart already in table
18
2.49M
    uint32_t match_len = match.match_length - 1;
19
2.49M
    uint32_t max_len = 2 * max_insert_count;       // Normally 32, 56, 96 and 112 for levels 3-6
20
21
    /* Don't re-hash positions hashed as literals (see fizzle_matches).
22
     * Also don't hash overlapping positions (see find_best_match). */
23
2.49M
    if (UNLIKELY(strstart < match.orgstart)) {
24
144k
        uint32_t end = strstart + match_len;
25
26
144k
        strstart = match.orgstart;
27
144k
        match_len = end - strstart;
28
144k
        if (match_len == 0) {
29
            /* Hash only the tail position */
30
6.10k
            insert_knuth(s, window, end - 1);
31
6.10k
            return;
32
6.10k
        }
33
144k
    }
34
35
    /* Shorter matches insert every position, which needs no step and no division. */
36
2.48M
    if (LIKELY(match_len <= max_len)) {
37
2.37M
        insert_knuth_batch(s, window, strstart, match_len);
38
2.37M
        return;
39
2.37M
    }
40
109k
    insert_knuth_stepped(s, window, strstart, match_len, max_insert_count);
41
109k
}
deflate_medium.c:insert_match
Line
Count
Source
16
1.38M
Z_FORCEINLINE static void SUFFIX(insert_match)(deflate_state *s, unsigned char *Z_RESTRICT window, struct match match, uint32_t max_insert_count) {
17
1.38M
    uint32_t strstart = match.strstart + 1;        // string at strstart already in table
18
1.38M
    uint32_t match_len = match.match_length - 1;
19
1.38M
    uint32_t max_len = 2 * max_insert_count;       // Normally 32, 56, 96 and 112 for levels 3-6
20
21
    /* Don't re-hash positions hashed as literals (see fizzle_matches).
22
     * Also don't hash overlapping positions (see find_best_match). */
23
1.38M
    if (UNLIKELY(strstart < match.orgstart)) {
24
61.9k
        uint32_t end = strstart + match_len;
25
26
61.9k
        strstart = match.orgstart;
27
61.9k
        match_len = end - strstart;
28
61.9k
        if (match_len == 0) {
29
            /* Hash only the tail position */
30
0
            insert_knuth(s, window, end - 1);
31
0
            return;
32
0
        }
33
61.9k
    }
34
35
    /* Shorter matches insert every position, which needs no step and no division. */
36
1.38M
    if (LIKELY(match_len <= max_len)) {
37
1.33M
        insert_knuth_batch(s, window, strstart, match_len);
38
1.33M
        return;
39
1.33M
    }
40
56.9k
    insert_knuth_stepped(s, window, strstart, match_len, max_insert_count);
41
56.9k
}
deflate_medium.c:insert_match_fizzle
Line
Count
Source
16
1.10M
Z_FORCEINLINE static void SUFFIX(insert_match)(deflate_state *s, unsigned char *Z_RESTRICT window, struct match match, uint32_t max_insert_count) {
17
1.10M
    uint32_t strstart = match.strstart + 1;        // string at strstart already in table
18
1.10M
    uint32_t match_len = match.match_length - 1;
19
1.10M
    uint32_t max_len = 2 * max_insert_count;       // Normally 32, 56, 96 and 112 for levels 3-6
20
21
    /* Don't re-hash positions hashed as literals (see fizzle_matches).
22
     * Also don't hash overlapping positions (see find_best_match). */
23
1.10M
    if (UNLIKELY(strstart < match.orgstart)) {
24
82.1k
        uint32_t end = strstart + match_len;
25
26
82.1k
        strstart = match.orgstart;
27
82.1k
        match_len = end - strstart;
28
82.1k
        if (match_len == 0) {
29
            /* Hash only the tail position */
30
6.10k
            insert_knuth(s, window, end - 1);
31
6.10k
            return;
32
6.10k
        }
33
82.1k
    }
34
35
    /* Shorter matches insert every position, which needs no step and no division. */
36
1.10M
    if (LIKELY(match_len <= max_len)) {
37
1.04M
        insert_knuth_batch(s, window, strstart, match_len);
38
1.04M
        return;
39
1.04M
    }
40
52.8k
    insert_knuth_stepped(s, window, strstart, match_len, max_insert_count);
41
52.8k
}
42
43
46.3M
Z_FORCEINLINE static struct match SUFFIX(find_best_match)(deflate_state *s, uint32_t hash_head, int32_t max_dist) {
44
46.3M
    struct match m;
45
46.3M
    int32_t dist;
46
47
    /* Initialize the match to be a 1 byte literal */
48
46.3M
    m.match_start = 0;
49
46.3M
    m.match_length = 1;
50
46.3M
    m.strstart = s->strstart;
51
46.3M
    m.orgstart = m.strstart;
52
53
46.3M
    dist = (int32_t)s->strstart - (int32_t)hash_head;
54
46.3M
    if (dist <= (int32_t)max_dist && dist > 0 && hash_head != 0) {
55
        /* To simplify the code, we prevent matches with the string
56
         * of window index 0 (in particular we have to avoid a match
57
         * of the string with itself at the start of the input file).
58
         */
59
14.9M
        uint32_t match_len = FUNCTABLE_CALL(longest_match)(s, hash_head);
60
14.9M
        m.match_start = s->match_start;
61
14.9M
        if (UNLIKELY(match_len < WANT_MIN_MATCH))
62
12.5M
            return m;
63
2.40M
        if (UNLIKELY(m.match_start >= m.strstart)) {
64
            /* this can happen due to some restarts */
65
223
            return m;
66
223
        }
67
2.40M
        m.match_length = match_len;
68
69
        /* We want to skip hashing overlapping positions when dist < match_length,
70
         * encode this into orgstart, fizzle_matches might re-introduce some
71
         * overlapping positions, but the simplification is worth it.
72
         *
73
         * If dist < match_length, the first (match_length - dist) positions
74
         * are byte-identical to data `dist` bytes earlier (already in table).
75
         */
76
2.40M
        uint32_t match_dist = m.strstart - m.match_start;
77
2.40M
        if (match_dist < match_len) {
78
96.5k
            m.orgstart += match_len - match_dist;
79
96.5k
        }
80
2.40M
    }
81
33.7M
    return m;
82
46.3M
}
deflate_medium.c:find_best_match
Line
Count
Source
43
20.0M
Z_FORCEINLINE static struct match SUFFIX(find_best_match)(deflate_state *s, uint32_t hash_head, int32_t max_dist) {
44
20.0M
    struct match m;
45
20.0M
    int32_t dist;
46
47
    /* Initialize the match to be a 1 byte literal */
48
20.0M
    m.match_start = 0;
49
20.0M
    m.match_length = 1;
50
20.0M
    m.strstart = s->strstart;
51
20.0M
    m.orgstart = m.strstart;
52
53
20.0M
    dist = (int32_t)s->strstart - (int32_t)hash_head;
54
20.0M
    if (dist <= (int32_t)max_dist && dist > 0 && hash_head != 0) {
55
        /* To simplify the code, we prevent matches with the string
56
         * of window index 0 (in particular we have to avoid a match
57
         * of the string with itself at the start of the input file).
58
         */
59
6.71M
        uint32_t match_len = FUNCTABLE_CALL(longest_match)(s, hash_head);
60
6.71M
        m.match_start = s->match_start;
61
6.71M
        if (UNLIKELY(match_len < WANT_MIN_MATCH))
62
5.37M
            return m;
63
1.34M
        if (UNLIKELY(m.match_start >= m.strstart)) {
64
            /* this can happen due to some restarts */
65
0
            return m;
66
0
        }
67
1.34M
        m.match_length = match_len;
68
69
        /* We want to skip hashing overlapping positions when dist < match_length,
70
         * encode this into orgstart, fizzle_matches might re-introduce some
71
         * overlapping positions, but the simplification is worth it.
72
         *
73
         * If dist < match_length, the first (match_length - dist) positions
74
         * are byte-identical to data `dist` bytes earlier (already in table).
75
         */
76
1.34M
        uint32_t match_dist = m.strstart - m.match_start;
77
1.34M
        if (match_dist < match_len) {
78
51.7k
            m.orgstart += match_len - match_dist;
79
51.7k
        }
80
1.34M
    }
81
14.6M
    return m;
82
20.0M
}
deflate_medium.c:find_best_match_fizzle
Line
Count
Source
43
26.3M
Z_FORCEINLINE static struct match SUFFIX(find_best_match)(deflate_state *s, uint32_t hash_head, int32_t max_dist) {
44
26.3M
    struct match m;
45
26.3M
    int32_t dist;
46
47
    /* Initialize the match to be a 1 byte literal */
48
26.3M
    m.match_start = 0;
49
26.3M
    m.match_length = 1;
50
26.3M
    m.strstart = s->strstart;
51
26.3M
    m.orgstart = m.strstart;
52
53
26.3M
    dist = (int32_t)s->strstart - (int32_t)hash_head;
54
26.3M
    if (dist <= (int32_t)max_dist && dist > 0 && hash_head != 0) {
55
        /* To simplify the code, we prevent matches with the string
56
         * of window index 0 (in particular we have to avoid a match
57
         * of the string with itself at the start of the input file).
58
         */
59
8.26M
        uint32_t match_len = FUNCTABLE_CALL(longest_match)(s, hash_head);
60
8.26M
        m.match_start = s->match_start;
61
8.26M
        if (UNLIKELY(match_len < WANT_MIN_MATCH))
62
7.20M
            return m;
63
1.05M
        if (UNLIKELY(m.match_start >= m.strstart)) {
64
            /* this can happen due to some restarts */
65
223
            return m;
66
223
        }
67
1.05M
        m.match_length = match_len;
68
69
        /* We want to skip hashing overlapping positions when dist < match_length,
70
         * encode this into orgstart, fizzle_matches might re-introduce some
71
         * overlapping positions, but the simplification is worth it.
72
         *
73
         * If dist < match_length, the first (match_length - dist) positions
74
         * are byte-identical to data `dist` bytes earlier (already in table).
75
         */
76
1.05M
        uint32_t match_dist = m.strstart - m.match_start;
77
1.05M
        if (match_dist < match_len) {
78
44.8k
            m.orgstart += match_len - match_dist;
79
44.8k
        }
80
1.05M
    }
81
19.1M
    return m;
82
26.3M
}
83
84
/* fizzle_matches investigates whether next_match (which starts after current_match) can grow backwards
85
 * to absorb current_match entirely, or reduce it to a single literal.
86
 * This occurs because next_match points to a different historical dictionary position, allowing it to discover
87
 * a better matching alignment that current_match bypassed due to the medium-strategy skipping positions.
88
 *
89
 * fizzle_matches assumes:
90
 * - current_match.match_length > 1
91
 * - current_match.match_length - 1 <= next->match_start
92
 * - current_match.match_length - 1 <= next->strstart
93
 * - next_match.match_length >= WANT_MIN_MATCH
94
 *
95
 * fizzle_matches returns:
96
 * - If successful, current and next are returned modified.
97
 *   - current_match.match_length is then either 0, 1
98
 */
99
#ifdef USE_FIZZLE
100
static void fizzle_matches(unsigned char *Z_RESTRICT window, struct match *Z_RESTRICT current,
101
426k
                           struct match *Z_RESTRICT next, int32_t max_dist) {
102
426k
    unsigned char *match = window + next->match_start + 1 - current->match_length;
103
426k
    unsigned char *orig  = window + next->strstart + 1 - current->match_length;
104
105
    /* quick exit check.. if this fails then don't bother with anything else */
106
426k
    if (LIKELY(*match != *orig))
107
216k
        return;
108
109
209k
    int32_t limit = (int32_t)next->strstart > max_dist ? (int32_t)next->strstart - max_dist : 0;
110
111
    // Steps needed to successfully fizzle match
112
209k
    uint32_t need = current->match_length - 1;
113
114
    // Protect next->strstart from moving past maximum distance
115
209k
    int32_t max_steps_to_limit = (int32_t)next->strstart - limit;
116
117
    // Protect next->match_length from exceeding STD_MAX_MATCH
118
209k
    int32_t max_growth_allowed = STD_MAX_MATCH - (int32_t)next->match_length;
119
120
    // Protect next->match_start from going too far back
121
209k
    int32_t max_steps_to_history = (int32_t)next->match_start - 1;
122
123
    // steps is the max number of backward steps allowed for each limitation
124
209k
    int32_t steps1 = MIN((int32_t)current->match_length, max_steps_to_limit);
125
209k
    int32_t steps2 = MIN(max_growth_allowed, max_steps_to_history);
126
209k
    int32_t steps = MIN(steps1, steps2);
127
128
    // If we can't possibly fizzle the current match out, return early
129
209k
    if (LIKELY(steps < (int32_t)need))
130
9.34k
        return;
131
132
    // The quick exit above already checked the first byte of the range.
133
    // Compare the whole range here; small ranges inline as a single masked
134
    // 8-byte XOR to avoid the memcmp call.
135
199k
    if (LIKELY(need <= sizeof(uint64_t))) {
136
166k
        uint64_t diff = zng_memread_8(match) ^ zng_memread_8(orig);
137
166k
        if (LIKELY(diff & zng_first_bytes_mask64(need)))
138
148k
            return;
139
166k
    } else if (LIKELY(memcmp(match, orig, (size_t)need) != 0)) {
140
20.7k
         return;
141
20.7k
    }
142
143
    // Check whether the final extra backward byte is also possible.
144
    // This decides whether the current match becomes length 1 or 0.
145
30.5k
    int extra_byte_ok = (steps == (int32_t)current->match_length);
146
147
    // Update variables, reduces current->match_length to 1.
148
30.5k
    next->match_start  = next->match_start - need;
149
30.5k
    next->strstart     = next->strstart - need;
150
30.5k
    next->match_length = next->match_length + need;
151
30.5k
    next->orgstart++;
152
30.5k
    current->match_length = 1;
153
154
    // If every constraint allowed one more backward byte, test it.
155
    // If it matches, the current match is fully absorbed and becomes length 0.
156
30.5k
    if (extra_byte_ok) {
157
30.3k
        match = window + next->match_start - 1;
158
30.3k
        orig  = window + next->strstart - 1;
159
160
30.3k
        if (*match == *orig) {
161
8.68k
            next->match_start--;
162
8.68k
            next->strstart--;
163
8.68k
            next->match_length++;
164
8.68k
            current->match_length = 0;
165
8.68k
        }
166
30.3k
    }
167
30.5k
}
168
#endif
169
170
3.50k
Z_INTERNAL block_state SUFFIX(deflate_medium)(deflate_state *s, int flush) {
171
3.50k
    ALIGNED_(16) struct match current_match = {0};
172
#ifdef USE_FIZZLE
173
                 struct match next_match = {0};
174
#endif
175
3.50k
    unsigned char *window = s->window;
176
3.50k
    uint32_t max_insert_count = MAX(2, s->max_insert_count); // stepped insert requires min 2
177
3.50k
    int32_t max_dist = MAX_DIST(s);
178
179
46.3M
    for (;;) {
180
46.3M
        int bflush = 0;       /* set if current block must be flushed */
181
46.3M
        uint32_t curr_match_len;
182
183
        /* Make sure that we always have enough lookahead, except
184
         * at the end of the input file. We need STD_MAX_MATCH bytes
185
         * for the next match, plus WANT_MIN_MATCH bytes to insert the
186
         * string following the next current_match.
187
         */
188
46.3M
        if (UNLIKELY(s->lookahead < MIN_LOOKAHEAD)) {
189
160k
            PREFIX(fill_window)(s);
190
160k
            if (UNLIKELY(s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH)) {
191
502
                return need_more;
192
502
            }
193
160k
            if (UNLIKELY(s->lookahead == 0))
194
2.80k
                break; /* flush the current block */
195
#ifdef USE_FIZZLE
196
102k
            next_match.match_length = 0;
197
102k
#endif
198
102k
        }
199
200
        /* Insert the string window[strstart .. strstart+2] in the
201
         * dictionary, and set hash_head to the head of the hash chain:
202
         */
203
204
#ifdef USE_FIZZLE
205
        /* If we already have a future match from a previous round, just use that */
206
26.3M
        if (next_match.match_length > 0) {
207
26.2M
            current_match = next_match;
208
26.2M
            next_match.match_length = 0;
209
26.2M
        } else {
210
119k
            uint32_t hash_head = 0;   /* head of the hash chain */
211
119k
            if (LIKELY(s->lookahead >= WANT_MIN_MATCH)) {
212
116k
                hash_head = insert_knuth(s, window, s->strstart);
213
116k
            }
214
215
119k
            current_match = SUFFIX(find_best_match)(s, hash_head, max_dist);
216
119k
        }
217
        curr_match_len = current_match.match_length;
218
219
26.3M
        if (curr_match_len >= WANT_MIN_MATCH && s->lookahead > (unsigned int)(curr_match_len + WANT_MIN_MATCH )) {
220
1.05M
            SUFFIX(insert_match)(s, window, current_match, max_insert_count);
221
1.05M
        }
222
223
26.3M
        if (UNLIKELY(curr_match_len == STD_MAX_MATCH)) {
224
            /* A max-length match cannot be extended by fizzle without truncating it,
225
             * skip fizzle and instead check for continuation after emitting current match. */
226
15.8k
            next_match.match_length = 0;
227
26.3M
        } else if (LIKELY((s->lookahead > MIN_LOOKAHEAD) &
228
26.3M
                          ((current_match.strstart + curr_match_len) < s->fill_end))) {
229
            /* now, look ahead one */
230
            /* Spelled without short-circuit so both loads issue before the compare, which keeps
231
             * the two tests fused into one branch. */
232
26.2M
            s->strstart = current_match.strstart + curr_match_len;
233
26.2M
            uint32_t hash_head = insert_knuth(s, window, s->strstart);
234
235
26.2M
            next_match = SUFFIX(find_best_match)(s, hash_head, max_dist);
236
237
26.2M
            uint32_t tmp_cmatch_len_sub = curr_match_len - 1;
238
26.2M
            if (tmp_cmatch_len_sub
239
1.02M
                     && next_match.match_length >= WANT_MIN_MATCH
240
426k
                     && tmp_cmatch_len_sub <= next_match.match_start) {
241
426k
                fizzle_matches(window, &current_match, &next_match, max_dist);
242
426k
                curr_match_len = current_match.match_length;
243
426k
            }
244
245
26.2M
            s->strstart = current_match.strstart;
246
26.2M
            if (curr_match_len == 0) {
247
                /* If current match fizzled out, jump to next loop iteration */
248
8.68k
                continue;
249
8.68k
            }
250
26.2M
        } else {
251
102k
            next_match.match_length = 0;
252
102k
        }
253
#else
254
        // WITHOUT_FIZZLE: We don't look for future matches, so code is much simplified
255
20.0M
        uint32_t hash_head = 0;   /* head of the hash chain */
256
20.0M
        if (LIKELY(s->lookahead >= WANT_MIN_MATCH)) {
257
20.0M
            hash_head = insert_knuth(s, window, s->strstart);
258
20.0M
        }
259
260
20.0M
        current_match = SUFFIX(find_best_match)(s, hash_head, max_dist);
261
        curr_match_len = current_match.match_length;
262
263
20.0M
        if (curr_match_len >= WANT_MIN_MATCH && s->lookahead > (unsigned int)(curr_match_len + WANT_MIN_MATCH )) {
264
1.34M
            SUFFIX(insert_match)(s, window, current_match, max_insert_count);
265
1.34M
        }
266
20.0M
#endif
267
268
        /* now emit the current match */
269
26.3M
        s->lookahead -= curr_match_len;
270
46.3M
        if (LIKELY(curr_match_len == 1)) {
271
            /* matches shorter than WANT_MIN_MATCH are set to 1, we need to emit these as literals */
272
43.9M
            bflush = zng_tr_tally_lit(s, window[current_match.strstart]);
273
43.9M
        } else {
274
2.37M
            check_match(s, current_match.strstart, current_match.match_start, curr_match_len);
275
2.37M
            bflush = zng_tr_tally_dist(s, current_match.strstart - current_match.match_start, curr_match_len - STD_MIN_MATCH);
276
2.37M
        }
277
278
        /* move the "cursor" forward */
279
26.3M
        s->strstart += curr_match_len;
280
281
        /* A max-length match might continue: keep emitting continuations at the same
282
         * distance without a full search as long as possible. */
283
46.3M
        if (UNLIKELY(curr_match_len == STD_MAX_MATCH && s->lookahead >= MIN_LOOKAHEAD)) {
284
27.6k
            uint32_t cont_dist = current_match.strstart - current_match.match_start;
285
118k
            for (;;) {
286
118k
                struct match cont_match;
287
288
                // Check for continuing match
289
118k
                uint32_t cont_len = max_match_continues(window, s->strstart, cont_dist);
290
118k
                if (UNLIKELY(cont_len < 8))
291
26.9k
                    break;
292
293
                // Set up continuation match
294
91.9k
                cont_match.match_start  = s->strstart - cont_dist;
295
91.9k
                cont_match.match_length = cont_len;
296
91.9k
                cont_match.strstart     = s->strstart;
297
298
91.9k
                check_match(s, cont_match.strstart, cont_match.match_start, cont_len);
299
300
                // Skip hashing positions that overlap the match (see find_best_match).
301
91.9k
                cont_match.orgstart = (cont_dist < cont_len) ? cont_match.strstart + cont_len - cont_dist : cont_match.strstart;
302
303
                // Insert match
304
91.9k
                insert_knuth(s, window, s->strstart);
305
91.9k
                SUFFIX(insert_match)(s, window, cont_match, max_insert_count);
306
307
                // Emit match
308
91.9k
                s->lookahead -= cont_len;
309
91.9k
                bflush |= zng_tr_tally_dist(s, cont_dist, cont_len - STD_MIN_MATCH);
310
91.9k
                s->strstart += cont_len;
311
312
91.9k
                if (UNLIKELY(bflush)) {
313
108
                    FLUSH_BLOCK(s, window, 0);
314
99
                    bflush = 0;
315
99
                }
316
91.9k
                if (UNLIKELY(s->lookahead < MIN_LOOKAHEAD)) {
317
641
                    break; /* not enough lookahead, return to the main loop */
318
641
                }
319
91.9k
            }
320
27.6k
        }
321
322
46.3M
        if (UNLIKELY(bflush))
323
46.3M
            FLUSH_BLOCK(s, window, 0);
324
46.3M
    }
325
326
2.80k
    s->insert = s->strstart < (STD_MIN_MATCH - 1) ? s->strstart : (STD_MIN_MATCH - 1);
327
2.80k
    if (flush == Z_FINISH) {
328
2.80k
        FLUSH_BLOCK(s, window, 1);
329
2.77k
        return finish_done;
330
2.80k
    }
331
0
    if (UNLIKELY(s->sym_next))
332
0
        FLUSH_BLOCK(s, window, 0);
333
334
0
    return block_done;
335
0
}
deflate_medium
Line
Count
Source
170
1.21k
Z_INTERNAL block_state SUFFIX(deflate_medium)(deflate_state *s, int flush) {
171
1.21k
    ALIGNED_(16) struct match current_match = {0};
172
#ifdef USE_FIZZLE
173
                 struct match next_match = {0};
174
#endif
175
1.21k
    unsigned char *window = s->window;
176
1.21k
    uint32_t max_insert_count = MAX(2, s->max_insert_count); // stepped insert requires min 2
177
1.21k
    int32_t max_dist = MAX_DIST(s);
178
179
20.0M
    for (;;) {
180
20.0M
        int bflush = 0;       /* set if current block must be flushed */
181
20.0M
        uint32_t curr_match_len;
182
183
        /* Make sure that we always have enough lookahead, except
184
         * at the end of the input file. We need STD_MAX_MATCH bytes
185
         * for the next match, plus WANT_MIN_MATCH bytes to insert the
186
         * string following the next current_match.
187
         */
188
20.0M
        if (UNLIKELY(s->lookahead < MIN_LOOKAHEAD)) {
189
55.6k
            PREFIX(fill_window)(s);
190
55.6k
            if (UNLIKELY(s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH)) {
191
217
                return need_more;
192
217
            }
193
55.4k
            if (UNLIKELY(s->lookahead == 0))
194
911
                break; /* flush the current block */
195
#ifdef USE_FIZZLE
196
            next_match.match_length = 0;
197
#endif
198
55.4k
        }
199
200
        /* Insert the string window[strstart .. strstart+2] in the
201
         * dictionary, and set hash_head to the head of the hash chain:
202
         */
203
204
#ifdef USE_FIZZLE
205
        /* If we already have a future match from a previous round, just use that */
206
        if (next_match.match_length > 0) {
207
            current_match = next_match;
208
            next_match.match_length = 0;
209
        } else {
210
            uint32_t hash_head = 0;   /* head of the hash chain */
211
            if (LIKELY(s->lookahead >= WANT_MIN_MATCH)) {
212
                hash_head = insert_knuth(s, window, s->strstart);
213
            }
214
215
            current_match = SUFFIX(find_best_match)(s, hash_head, max_dist);
216
        }
217
        curr_match_len = current_match.match_length;
218
219
        if (curr_match_len >= WANT_MIN_MATCH && s->lookahead > (unsigned int)(curr_match_len + WANT_MIN_MATCH )) {
220
            SUFFIX(insert_match)(s, window, current_match, max_insert_count);
221
        }
222
223
        if (UNLIKELY(curr_match_len == STD_MAX_MATCH)) {
224
            /* A max-length match cannot be extended by fizzle without truncating it,
225
             * skip fizzle and instead check for continuation after emitting current match. */
226
            next_match.match_length = 0;
227
        } else if (LIKELY((s->lookahead > MIN_LOOKAHEAD) &
228
                          ((current_match.strstart + curr_match_len) < s->fill_end))) {
229
            /* now, look ahead one */
230
            /* Spelled without short-circuit so both loads issue before the compare, which keeps
231
             * the two tests fused into one branch. */
232
            s->strstart = current_match.strstart + curr_match_len;
233
            uint32_t hash_head = insert_knuth(s, window, s->strstart);
234
235
            next_match = SUFFIX(find_best_match)(s, hash_head, max_dist);
236
237
            uint32_t tmp_cmatch_len_sub = curr_match_len - 1;
238
            if (tmp_cmatch_len_sub
239
                     && next_match.match_length >= WANT_MIN_MATCH
240
                     && tmp_cmatch_len_sub <= next_match.match_start) {
241
                fizzle_matches(window, &current_match, &next_match, max_dist);
242
                curr_match_len = current_match.match_length;
243
            }
244
245
            s->strstart = current_match.strstart;
246
            if (curr_match_len == 0) {
247
                /* If current match fizzled out, jump to next loop iteration */
248
                continue;
249
            }
250
        } else {
251
            next_match.match_length = 0;
252
        }
253
#else
254
        // WITHOUT_FIZZLE: We don't look for future matches, so code is much simplified
255
20.0M
        uint32_t hash_head = 0;   /* head of the hash chain */
256
20.0M
        if (LIKELY(s->lookahead >= WANT_MIN_MATCH)) {
257
20.0M
            hash_head = insert_knuth(s, window, s->strstart);
258
20.0M
        }
259
260
20.0M
        current_match = SUFFIX(find_best_match)(s, hash_head, max_dist);
261
20.0M
        curr_match_len = current_match.match_length;
262
263
20.0M
        if (curr_match_len >= WANT_MIN_MATCH && s->lookahead > (unsigned int)(curr_match_len + WANT_MIN_MATCH )) {
264
1.34M
            SUFFIX(insert_match)(s, window, current_match, max_insert_count);
265
1.34M
        }
266
20.0M
#endif
267
268
        /* now emit the current match */
269
20.0M
        s->lookahead -= curr_match_len;
270
20.0M
        if (LIKELY(curr_match_len == 1)) {
271
            /* matches shorter than WANT_MIN_MATCH are set to 1, we need to emit these as literals */
272
18.6M
            bflush = zng_tr_tally_lit(s, window[current_match.strstart]);
273
18.6M
        } else {
274
1.34M
            check_match(s, current_match.strstart, current_match.match_start, curr_match_len);
275
1.34M
            bflush = zng_tr_tally_dist(s, current_match.strstart - current_match.match_start, curr_match_len - STD_MIN_MATCH);
276
1.34M
        }
277
278
        /* move the "cursor" forward */
279
20.0M
        s->strstart += curr_match_len;
280
281
        /* A max-length match might continue: keep emitting continuations at the same
282
         * distance without a full search as long as possible. */
283
20.0M
        if (UNLIKELY(curr_match_len == STD_MAX_MATCH && s->lookahead >= MIN_LOOKAHEAD)) {
284
11.8k
            uint32_t cont_dist = current_match.strstart - current_match.match_start;
285
53.2k
            for (;;) {
286
53.2k
                struct match cont_match;
287
288
                // Check for continuing match
289
53.2k
                uint32_t cont_len = max_match_continues(window, s->strstart, cont_dist);
290
53.2k
                if (UNLIKELY(cont_len < 8))
291
11.5k
                    break;
292
293
                // Set up continuation match
294
41.7k
                cont_match.match_start  = s->strstart - cont_dist;
295
41.7k
                cont_match.match_length = cont_len;
296
41.7k
                cont_match.strstart     = s->strstart;
297
298
41.7k
                check_match(s, cont_match.strstart, cont_match.match_start, cont_len);
299
300
                // Skip hashing positions that overlap the match (see find_best_match).
301
41.7k
                cont_match.orgstart = (cont_dist < cont_len) ? cont_match.strstart + cont_len - cont_dist : cont_match.strstart;
302
303
                // Insert match
304
41.7k
                insert_knuth(s, window, s->strstart);
305
41.7k
                SUFFIX(insert_match)(s, window, cont_match, max_insert_count);
306
307
                // Emit match
308
41.7k
                s->lookahead -= cont_len;
309
41.7k
                bflush |= zng_tr_tally_dist(s, cont_dist, cont_len - STD_MIN_MATCH);
310
41.7k
                s->strstart += cont_len;
311
312
41.7k
                if (UNLIKELY(bflush)) {
313
62
                    FLUSH_BLOCK(s, window, 0);
314
59
                    bflush = 0;
315
59
                }
316
41.7k
                if (UNLIKELY(s->lookahead < MIN_LOOKAHEAD)) {
317
298
                    break; /* not enough lookahead, return to the main loop */
318
298
                }
319
41.7k
            }
320
11.8k
        }
321
322
20.0M
        if (UNLIKELY(bflush))
323
20.0M
            FLUSH_BLOCK(s, window, 0);
324
20.0M
    }
325
326
911
    s->insert = s->strstart < (STD_MIN_MATCH - 1) ? s->strstart : (STD_MIN_MATCH - 1);
327
911
    if (flush == Z_FINISH) {
328
911
        FLUSH_BLOCK(s, window, 1);
329
893
        return finish_done;
330
911
    }
331
0
    if (UNLIKELY(s->sym_next))
332
0
        FLUSH_BLOCK(s, window, 0);
333
334
0
    return block_done;
335
0
}
deflate_medium_fizzle
Line
Count
Source
170
2.29k
Z_INTERNAL block_state SUFFIX(deflate_medium)(deflate_state *s, int flush) {
171
2.29k
    ALIGNED_(16) struct match current_match = {0};
172
2.29k
#ifdef USE_FIZZLE
173
2.29k
                 struct match next_match = {0};
174
2.29k
#endif
175
2.29k
    unsigned char *window = s->window;
176
2.29k
    uint32_t max_insert_count = MAX(2, s->max_insert_count); // stepped insert requires min 2
177
2.29k
    int32_t max_dist = MAX_DIST(s);
178
179
26.3M
    for (;;) {
180
26.3M
        int bflush = 0;       /* set if current block must be flushed */
181
26.3M
        uint32_t curr_match_len;
182
183
        /* Make sure that we always have enough lookahead, except
184
         * at the end of the input file. We need STD_MAX_MATCH bytes
185
         * for the next match, plus WANT_MIN_MATCH bytes to insert the
186
         * string following the next current_match.
187
         */
188
26.3M
        if (UNLIKELY(s->lookahead < MIN_LOOKAHEAD)) {
189
105k
            PREFIX(fill_window)(s);
190
105k
            if (UNLIKELY(s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH)) {
191
285
                return need_more;
192
285
            }
193
104k
            if (UNLIKELY(s->lookahead == 0))
194
1.88k
                break; /* flush the current block */
195
102k
#ifdef USE_FIZZLE
196
102k
            next_match.match_length = 0;
197
102k
#endif
198
102k
        }
199
200
        /* Insert the string window[strstart .. strstart+2] in the
201
         * dictionary, and set hash_head to the head of the hash chain:
202
         */
203
204
26.3M
#ifdef USE_FIZZLE
205
        /* If we already have a future match from a previous round, just use that */
206
26.3M
        if (next_match.match_length > 0) {
207
26.2M
            current_match = next_match;
208
26.2M
            next_match.match_length = 0;
209
26.2M
        } else {
210
119k
            uint32_t hash_head = 0;   /* head of the hash chain */
211
119k
            if (LIKELY(s->lookahead >= WANT_MIN_MATCH)) {
212
116k
                hash_head = insert_knuth(s, window, s->strstart);
213
116k
            }
214
215
119k
            current_match = SUFFIX(find_best_match)(s, hash_head, max_dist);
216
119k
        }
217
26.3M
        curr_match_len = current_match.match_length;
218
219
26.3M
        if (curr_match_len >= WANT_MIN_MATCH && s->lookahead > (unsigned int)(curr_match_len + WANT_MIN_MATCH )) {
220
1.05M
            SUFFIX(insert_match)(s, window, current_match, max_insert_count);
221
1.05M
        }
222
223
26.3M
        if (UNLIKELY(curr_match_len == STD_MAX_MATCH)) {
224
            /* A max-length match cannot be extended by fizzle without truncating it,
225
             * skip fizzle and instead check for continuation after emitting current match. */
226
15.8k
            next_match.match_length = 0;
227
26.3M
        } else if (LIKELY((s->lookahead > MIN_LOOKAHEAD) &
228
26.3M
                          ((current_match.strstart + curr_match_len) < s->fill_end))) {
229
            /* now, look ahead one */
230
            /* Spelled without short-circuit so both loads issue before the compare, which keeps
231
             * the two tests fused into one branch. */
232
26.2M
            s->strstart = current_match.strstart + curr_match_len;
233
26.2M
            uint32_t hash_head = insert_knuth(s, window, s->strstart);
234
235
26.2M
            next_match = SUFFIX(find_best_match)(s, hash_head, max_dist);
236
237
26.2M
            uint32_t tmp_cmatch_len_sub = curr_match_len - 1;
238
26.2M
            if (tmp_cmatch_len_sub
239
1.02M
                     && next_match.match_length >= WANT_MIN_MATCH
240
426k
                     && tmp_cmatch_len_sub <= next_match.match_start) {
241
426k
                fizzle_matches(window, &current_match, &next_match, max_dist);
242
426k
                curr_match_len = current_match.match_length;
243
426k
            }
244
245
26.2M
            s->strstart = current_match.strstart;
246
26.2M
            if (curr_match_len == 0) {
247
                /* If current match fizzled out, jump to next loop iteration */
248
8.68k
                continue;
249
8.68k
            }
250
26.2M
        } else {
251
102k
            next_match.match_length = 0;
252
102k
        }
253
#else
254
        // WITHOUT_FIZZLE: We don't look for future matches, so code is much simplified
255
        uint32_t hash_head = 0;   /* head of the hash chain */
256
        if (LIKELY(s->lookahead >= WANT_MIN_MATCH)) {
257
            hash_head = insert_knuth(s, window, s->strstart);
258
        }
259
260
        current_match = SUFFIX(find_best_match)(s, hash_head, max_dist);
261
        curr_match_len = current_match.match_length;
262
263
        if (curr_match_len >= WANT_MIN_MATCH && s->lookahead > (unsigned int)(curr_match_len + WANT_MIN_MATCH )) {
264
            SUFFIX(insert_match)(s, window, current_match, max_insert_count);
265
        }
266
#endif
267
268
        /* now emit the current match */
269
26.3M
        s->lookahead -= curr_match_len;
270
26.3M
        if (LIKELY(curr_match_len == 1)) {
271
            /* matches shorter than WANT_MIN_MATCH are set to 1, we need to emit these as literals */
272
25.3M
            bflush = zng_tr_tally_lit(s, window[current_match.strstart]);
273
25.3M
        } else {
274
1.02M
            check_match(s, current_match.strstart, current_match.match_start, curr_match_len);
275
1.02M
            bflush = zng_tr_tally_dist(s, current_match.strstart - current_match.match_start, curr_match_len - STD_MIN_MATCH);
276
1.02M
        }
277
278
        /* move the "cursor" forward */
279
26.3M
        s->strstart += curr_match_len;
280
281
        /* A max-length match might continue: keep emitting continuations at the same
282
         * distance without a full search as long as possible. */
283
26.3M
        if (UNLIKELY(curr_match_len == STD_MAX_MATCH && s->lookahead >= MIN_LOOKAHEAD)) {
284
15.7k
            uint32_t cont_dist = current_match.strstart - current_match.match_start;
285
65.5k
            for (;;) {
286
65.5k
                struct match cont_match;
287
288
                // Check for continuing match
289
65.5k
                uint32_t cont_len = max_match_continues(window, s->strstart, cont_dist);
290
65.5k
                if (UNLIKELY(cont_len < 8))
291
15.3k
                    break;
292
293
                // Set up continuation match
294
50.1k
                cont_match.match_start  = s->strstart - cont_dist;
295
50.1k
                cont_match.match_length = cont_len;
296
50.1k
                cont_match.strstart     = s->strstart;
297
298
50.1k
                check_match(s, cont_match.strstart, cont_match.match_start, cont_len);
299
300
                // Skip hashing positions that overlap the match (see find_best_match).
301
50.1k
                cont_match.orgstart = (cont_dist < cont_len) ? cont_match.strstart + cont_len - cont_dist : cont_match.strstart;
302
303
                // Insert match
304
50.1k
                insert_knuth(s, window, s->strstart);
305
50.1k
                SUFFIX(insert_match)(s, window, cont_match, max_insert_count);
306
307
                // Emit match
308
50.1k
                s->lookahead -= cont_len;
309
50.1k
                bflush |= zng_tr_tally_dist(s, cont_dist, cont_len - STD_MIN_MATCH);
310
50.1k
                s->strstart += cont_len;
311
312
50.1k
                if (UNLIKELY(bflush)) {
313
46
                    FLUSH_BLOCK(s, window, 0);
314
40
                    bflush = 0;
315
40
                }
316
50.1k
                if (UNLIKELY(s->lookahead < MIN_LOOKAHEAD)) {
317
343
                    break; /* not enough lookahead, return to the main loop */
318
343
                }
319
50.1k
            }
320
15.7k
        }
321
322
26.3M
        if (UNLIKELY(bflush))
323
26.3M
            FLUSH_BLOCK(s, window, 0);
324
26.3M
    }
325
326
1.88k
    s->insert = s->strstart < (STD_MIN_MATCH - 1) ? s->strstart : (STD_MIN_MATCH - 1);
327
1.88k
    if (flush == Z_FINISH) {
328
1.88k
        FLUSH_BLOCK(s, window, 1);
329
1.87k
        return finish_done;
330
1.88k
    }
331
0
    if (UNLIKELY(s->sym_next))
332
0
        FLUSH_BLOCK(s, window, 0);
333
334
0
    return block_done;
335
0
}