Coverage Report

Created: 2026-06-10 06:34

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/zlib-ng/deflate_medium.c
Line
Count
Source
1
/* deflate_medium.c -- 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
 * For conditions of distribution and use, see copyright notice in zlib.h
8
 */
9
#ifndef NO_MEDIUM_STRATEGY
10
#include "zbuild.h"
11
#include "deflate.h"
12
#include "deflate_p.h"
13
#include "functable.h"
14
#include "insert_string_p.h"
15
16
struct match {
17
    uint16_t match_start;
18
    uint16_t match_length;
19
    uint16_t strstart;
20
    uint16_t orgstart;
21
};
22
23
225M
static int emit_match(deflate_state *s, unsigned char *window, struct match match) {
24
225M
    int bflush = 0;
25
225M
    uint32_t match_len = match.match_length;
26
27
    /* None of the below functions care about s->lookahead, so decrement it early */
28
225M
    s->lookahead -= match_len;
29
30
    /* matches that are not long enough we need to emit as literals */
31
225M
    if (match_len < WANT_MIN_MATCH) {
32
433M
        while (match_len) {
33
216M
            bflush += zng_tr_tally_lit(s, window[match.strstart]);
34
216M
            match_len--;
35
216M
            match.strstart++;
36
216M
        }
37
216M
        return bflush;
38
216M
    }
39
40
8.43M
    check_match(s, match.strstart, match.match_start, match_len);
41
42
8.43M
    bflush += zng_tr_tally_dist(s, match.strstart - match.match_start, match_len - STD_MIN_MATCH);
43
8.43M
    return bflush;
44
225M
}
45
46
/* insert_match assumes: s->lookahead > match.match_length + WANT_MIN_MATCH */
47
225M
static void insert_match(deflate_state *s, unsigned char *window, struct match match) {
48
225M
    uint32_t match_len = match.match_length;
49
225M
    uint32_t strstart = match.strstart;
50
51
    /* matches that are not long enough we need to emit as literals */
52
225M
    if (LIKELY(match_len < WANT_MIN_MATCH)) {
53
216M
        strstart++;
54
216M
        match_len--;
55
216M
        if (UNLIKELY(match_len > 0)) {
56
0
            if (strstart >= match.orgstart) {
57
0
                if (strstart + match_len - 1 >= match.orgstart) {
58
0
                    insert_string(s, window, strstart, match_len);
59
0
                } else {
60
0
                    insert_string(s, window, strstart, match.orgstart - strstart + 1);
61
0
                }
62
0
            }
63
0
        }
64
216M
        return;
65
216M
    }
66
67
    /* Insert new strings in the hash table only if the match length
68
     * is not too large. This saves time but degrades compression.
69
     */
70
8.50M
    if (match_len <= 16 * s->max_insert_length && s->lookahead >= WANT_MIN_MATCH) {
71
8.01M
        match_len--; /* string at strstart already in table */
72
8.01M
        strstart++;
73
74
8.01M
        if (LIKELY(strstart >= match.orgstart)) {
75
7.94M
            if (LIKELY(strstart + match_len - 1 >= match.orgstart)) {
76
7.94M
                insert_string(s, window, strstart, match_len);
77
7.94M
            } else {
78
0
                insert_string(s, window, strstart, match.orgstart - strstart + 1);
79
0
            }
80
7.94M
        } else if (match.orgstart < strstart + match_len) {
81
71.0k
            insert_string(s, window, match.orgstart, strstart + match_len - match.orgstart);
82
71.0k
        }
83
8.01M
    } else {
84
483k
        strstart += match_len;
85
483k
        quick_insert_string(s, window, strstart + 2 - STD_MIN_MATCH);
86
87
        /* If lookahead < WANT_MIN_MATCH, ins_h is garbage, but it does not
88
         * matter since it will be recomputed at next deflate call.
89
         */
90
483k
    }
91
8.50M
}
92
93
225M
Z_FORCEINLINE static struct match find_best_match(deflate_state *s, uint32_t hash_head) {
94
225M
    struct match m;
95
225M
    int64_t dist;
96
97
225M
    m.strstart = (uint16_t)s->strstart;
98
225M
    m.orgstart = m.strstart;
99
100
225M
    dist = (int64_t)s->strstart - hash_head;
101
225M
    if (dist <= MAX_DIST(s) && dist > 0 && hash_head != 0) {
102
        /* To simplify the code, we prevent matches with the string
103
         * of window index 0 (in particular we have to avoid a match
104
         * of the string with itself at the start of the input file).
105
         */
106
77.0M
        m.match_length = (uint16_t)FUNCTABLE_CALL(longest_match)(s, hash_head);
107
77.0M
        m.match_start = (uint16_t)s->match_start;
108
77.0M
        if (UNLIKELY(m.match_length < WANT_MIN_MATCH))
109
68.5M
            m.match_length = 1;
110
77.0M
        if (UNLIKELY(m.match_start >= m.strstart)) {
111
            /* this can happen due to some restarts */
112
0
            m.match_length = 1;
113
0
        }
114
148M
    } else {
115
        /* Set up the match to be a 1 byte literal */
116
148M
        m.match_start = 0;
117
148M
        m.match_length = 1;
118
148M
    }
119
120
225M
    return m;
121
225M
}
122
123
/* fizzle_matches assumes:
124
 * - current->match_length > 1
125
 * - (current->match_length - 1) <= next->match_start
126
 * - (current->match_length - 1) <= next->strstart
127
 */
128
2.35M
static void fizzle_matches(deflate_state *s, unsigned char *window, struct match *current, struct match *next) {
129
2.35M
    unsigned char *match, *orig;
130
2.35M
    struct match c, n;
131
2.35M
    int changed = 0;
132
2.35M
    Pos limit;
133
134
2.35M
    match = window - current->match_length + 1 + next->match_start;
135
2.35M
    orig  = window - current->match_length + 1 + next->strstart;
136
137
    /* quick exit check.. if this fails then don't bother with anything else */
138
2.35M
    if (LIKELY(*match != *orig))
139
972k
        return;
140
141
1.38M
    c = *current;
142
1.38M
    n = *next;
143
144
    /* step one: try to move the "next" match to the left as much as possible */
145
1.38M
    limit = next->strstart > MAX_DIST(s) ? next->strstart - (Pos)MAX_DIST(s) : 0;
146
147
1.38M
    match = window + n.match_start - 1;
148
1.38M
    orig = window + n.strstart - 1;
149
150
8.77M
    while (*match == *orig) {
151
7.60M
        if (UNLIKELY(c.match_length < 1))
152
6.95k
            break;
153
7.59M
        if (UNLIKELY(n.strstart <= limit))
154
0
            break;
155
7.59M
        if (UNLIKELY(n.match_length >= 256))
156
212k
            break;
157
7.38M
        if (UNLIKELY(n.match_start <= 1))
158
40
            break;
159
160
7.38M
        n.strstart--;
161
7.38M
        n.match_start--;
162
7.38M
        n.match_length++;
163
7.38M
        c.match_length--;
164
7.38M
        match--;
165
7.38M
        orig--;
166
7.38M
        changed++;
167
7.38M
    }
168
169
1.38M
    if (changed && c.match_length <= 1 && n.match_length != 2) {
170
71.0k
        n.orgstart++;
171
71.0k
        *current = c;
172
71.0k
        *next = n;
173
1.31M
    } else {
174
1.31M
        return;
175
1.31M
    }
176
1.38M
}
177
178
11.2k
Z_INTERNAL block_state deflate_medium(deflate_state *s, int flush) {
179
    /* Align the first struct to start on a new cacheline, this allows us to fit both structs in one cacheline */
180
11.2k
    ALIGNED_(16) struct match current_match = {0};
181
11.2k
                 struct match next_match = {0};
182
11.2k
    unsigned char *window = s->window;
183
184
    /* For levels below 5, don't check the next position for a better match */
185
11.2k
    int early_exit = s->level < 5;
186
187
225M
    for (;;) {
188
225M
        uint32_t hash_head = 0;    /* head of the hash chain */
189
225M
        int bflush = 0;       /* set if current block must be flushed */
190
191
        /* Make sure that we always have enough lookahead, except
192
         * at the end of the input file. We need STD_MAX_MATCH bytes
193
         * for the next match, plus WANT_MIN_MATCH bytes to insert the
194
         * string following the next current_match.
195
         */
196
225M
        if (s->lookahead < MIN_LOOKAHEAD) {
197
711k
            PREFIX(fill_window)(s);
198
711k
            if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
199
0
                return need_more;
200
0
            }
201
711k
            if (UNLIKELY(s->lookahead == 0))
202
11.2k
                break; /* flush the current block */
203
700k
            next_match.match_length = 0;
204
700k
        }
205
206
        /* Insert the string window[strstart .. strstart+2] in the
207
         * dictionary, and set hash_head to the head of the hash chain:
208
         */
209
210
        /* If we already have a future match from a previous round, just use that */
211
225M
        if (!early_exit && next_match.match_length > 0) {
212
111M
            current_match = next_match;
213
111M
            next_match.match_length = 0;
214
113M
        } else {
215
113M
            hash_head = 0;
216
113M
            if (s->lookahead >= WANT_MIN_MATCH) {
217
113M
                hash_head = quick_insert_string(s, window, s->strstart);
218
113M
            }
219
220
113M
            current_match = find_best_match(s, hash_head);
221
113M
        }
222
223
225M
        if (LIKELY(s->lookahead > (unsigned int)(current_match.match_length + WANT_MIN_MATCH)))
224
225M
            insert_match(s, window, current_match);
225
226
        /* now, look ahead one */
227
225M
        if (LIKELY(!early_exit && s->lookahead > MIN_LOOKAHEAD && (uint32_t)(current_match.strstart + current_match.match_length) < (s->window_size - MIN_LOOKAHEAD))) {
228
111M
            s->strstart = current_match.strstart + current_match.match_length;
229
111M
            hash_head = quick_insert_string(s, window, s->strstart);
230
231
111M
            next_match = find_best_match(s, hash_head);
232
233
111M
            uint32_t tmp_cmatch_len_sub = current_match.match_length - 1;
234
111M
            if (tmp_cmatch_len_sub
235
3.76M
                     && next_match.match_length >= WANT_MIN_MATCH
236
2.35M
                     && tmp_cmatch_len_sub <= next_match.match_start
237
2.35M
                     && tmp_cmatch_len_sub <= next_match.strstart) {
238
2.35M
                fizzle_matches(s, window, &current_match, &next_match);
239
2.35M
            }
240
241
111M
            s->strstart = current_match.strstart;
242
113M
        } else {
243
113M
            next_match.match_length = 0;
244
113M
        }
245
246
        /* now emit the current match */
247
225M
        bflush = emit_match(s, window, current_match);
248
249
        /* move the "cursor" forward */
250
225M
        s->strstart += current_match.match_length;
251
252
225M
        if (UNLIKELY(bflush))
253
225M
            FLUSH_BLOCK(s, window, 0);
254
225M
    }
255
11.2k
    s->insert = s->strstart < (STD_MIN_MATCH - 1) ? s->strstart : (STD_MIN_MATCH - 1);
256
11.2k
    if (flush == Z_FINISH) {
257
11.2k
        FLUSH_BLOCK(s, window, 1);
258
11.2k
        return finish_done;
259
11.2k
    }
260
0
    if (UNLIKELY(s->sym_next))
261
0
        FLUSH_BLOCK(s, window, 0);
262
263
0
    return block_done;
264
0
}
265
#endif