Coverage Report

Created: 2026-02-26 06:53

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
43.0M
static int emit_match(deflate_state *s, struct match match) {
24
43.0M
    int bflush = 0;
25
26
    /* matches that are not long enough we need to emit as literals */
27
43.0M
    if (match.match_length < WANT_MIN_MATCH) {
28
80.8M
        while (match.match_length) {
29
40.4M
            bflush += zng_tr_tally_lit(s, s->window[match.strstart]);
30
40.4M
            s->lookahead--;
31
40.4M
            match.strstart++;
32
40.4M
            match.match_length--;
33
40.4M
        }
34
40.4M
        return bflush;
35
40.4M
    }
36
37
2.59M
    check_match(s, match.strstart, match.match_start, match.match_length);
38
39
2.59M
    bflush += zng_tr_tally_dist(s, match.strstart - match.match_start, match.match_length - STD_MIN_MATCH);
40
41
2.59M
    s->lookahead -= match.match_length;
42
2.59M
    return bflush;
43
43.0M
}
44
45
43.0M
static void insert_match(deflate_state *s, struct match match) {
46
43.0M
    if (UNLIKELY(s->lookahead <= (unsigned int)(match.match_length + WANT_MIN_MATCH)))
47
4.63k
        return;
48
49
    /* matches that are not long enough we need to emit as literals */
50
43.0M
    if (LIKELY(match.match_length < WANT_MIN_MATCH)) {
51
40.4M
        match.strstart++;
52
40.4M
        match.match_length--;
53
40.4M
        if (UNLIKELY(match.match_length > 0)) {
54
0
            if (match.strstart >= match.orgstart) {
55
0
                if (match.strstart + match.match_length - 1 >= match.orgstart) {
56
0
                    insert_string(s, match.strstart, match.match_length);
57
0
                } else {
58
0
                    insert_string(s, match.strstart, match.orgstart - match.strstart + 1);
59
0
                }
60
0
                match.strstart += match.match_length;
61
0
                match.match_length = 0;
62
0
            }
63
0
        }
64
40.4M
        return;
65
40.4M
    }
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
2.61M
    if (match.match_length <= 16 * s->max_insert_length && s->lookahead >= WANT_MIN_MATCH) {
71
2.55M
        match.match_length--; /* string at strstart already in table */
72
2.55M
        match.strstart++;
73
74
2.55M
        if (LIKELY(match.strstart >= match.orgstart)) {
75
2.52M
            if (LIKELY(match.strstart + match.match_length - 1 >= match.orgstart)) {
76
2.52M
                insert_string(s, match.strstart, match.match_length);
77
2.52M
            } else {
78
0
                insert_string(s, match.strstart, match.orgstart - match.strstart + 1);
79
0
            }
80
2.52M
        } else if (match.orgstart < match.strstart + match.match_length) {
81
25.3k
            insert_string(s, match.orgstart, match.strstart + match.match_length - match.orgstart);
82
25.3k
        }
83
2.55M
        match.strstart += match.match_length;
84
2.55M
        match.match_length = 0;
85
2.55M
    } else {
86
61.2k
        match.strstart += match.match_length;
87
61.2k
        match.match_length = 0;
88
89
61.2k
        if (match.strstart >= (STD_MIN_MATCH - 2))
90
61.2k
            quick_insert_string(s, match.strstart + 2 - STD_MIN_MATCH);
91
92
        /* If lookahead < WANT_MIN_MATCH, ins_h is garbage, but it does not
93
         * matter since it will be recomputed at next deflate call.
94
         */
95
61.2k
    }
96
2.61M
}
97
98
801k
static void fizzle_matches(deflate_state *s, struct match *current, struct match *next) {
99
801k
    unsigned char *window;
100
801k
    unsigned char *match, *orig;
101
801k
    struct match c, n;
102
801k
    int changed = 0;
103
801k
    Pos limit;
104
    /* step zero: sanity checks */
105
106
801k
    if (current->match_length <= 1)
107
467k
        return;
108
109
334k
    if (UNLIKELY(current->match_length > 1 + next->match_start))
110
278
        return;
111
112
333k
    if (UNLIKELY(current->match_length > 1 + next->strstart))
113
0
        return;
114
115
333k
    window = s->window;
116
117
333k
    match = window - current->match_length + 1 + next->match_start;
118
333k
    orig  = window - current->match_length + 1 + next->strstart;
119
120
    /* quick exit check.. if this fails then don't bother with anything else */
121
333k
    if (LIKELY(*match != *orig))
122
185k
        return;
123
124
148k
    c = *current;
125
148k
    n = *next;
126
127
    /* step one: try to move the "next" match to the left as much as possible */
128
148k
    limit = next->strstart > MAX_DIST(s) ? next->strstart - (Pos)MAX_DIST(s) : 0;
129
130
148k
    match = window + n.match_start - 1;
131
148k
    orig = window + n.strstart - 1;
132
133
1.84M
    while (*match == *orig) {
134
1.73M
        if (UNLIKELY(c.match_length < 1))
135
3.85k
            break;
136
1.73M
        if (UNLIKELY(n.strstart <= limit))
137
0
            break;
138
1.73M
        if (UNLIKELY(n.match_length >= 256))
139
37.7k
            break;
140
1.69M
        if (UNLIKELY(n.match_start <= 1))
141
34
            break;
142
143
1.69M
        n.strstart--;
144
1.69M
        n.match_start--;
145
1.69M
        n.match_length++;
146
1.69M
        c.match_length--;
147
1.69M
        match--;
148
1.69M
        orig--;
149
1.69M
        changed++;
150
1.69M
    }
151
152
148k
    if (!changed)
153
86.3k
        return;
154
155
62.1k
    if (c.match_length <= 1 && n.match_length != 2) {
156
25.3k
        n.orgstart++;
157
25.3k
        *current = c;
158
25.3k
        *next = n;
159
36.7k
    } else {
160
36.7k
        return;
161
36.7k
    }
162
62.1k
}
163
164
1.96k
Z_INTERNAL block_state deflate_medium(deflate_state *s, int flush) {
165
    /* Align the first struct to start on a new cacheline, this allows us to fit both structs in one cacheline */
166
1.96k
    ALIGNED_(16) struct match current_match = {0};
167
1.96k
                 struct match next_match = {0};
168
169
    /* For levels below 5, don't check the next position for a better match */
170
1.96k
    int early_exit = s->level < 5;
171
172
43.0M
    for (;;) {
173
43.0M
        uint32_t hash_head = 0;    /* head of the hash chain */
174
43.0M
        int bflush = 0;       /* set if current block must be flushed */
175
43.0M
        int64_t dist;
176
177
        /* Make sure that we always have enough lookahead, except
178
         * at the end of the input file. We need STD_MAX_MATCH bytes
179
         * for the next match, plus WANT_MIN_MATCH bytes to insert the
180
         * string following the next current_match.
181
         */
182
43.0M
        if (s->lookahead < MIN_LOOKAHEAD) {
183
87.3k
            PREFIX(fill_window)(s);
184
87.3k
            if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
185
437
                return need_more;
186
437
            }
187
86.9k
            if (UNLIKELY(s->lookahead == 0))
188
1.31k
                break; /* flush the current block */
189
85.6k
            next_match.match_length = 0;
190
85.6k
        }
191
192
        /* Insert the string window[strstart .. strstart+2] in the
193
         * dictionary, and set hash_head to the head of the hash chain:
194
         */
195
196
        /* If we already have a future match from a previous round, just use that */
197
43.0M
        if (!early_exit && next_match.match_length > 0) {
198
18.4M
            current_match = next_match;
199
18.4M
            next_match.match_length = 0;
200
24.5M
        } else {
201
24.5M
            hash_head = 0;
202
24.5M
            if (s->lookahead >= WANT_MIN_MATCH) {
203
24.5M
                hash_head = quick_insert_string(s, s->strstart);
204
24.5M
            }
205
206
24.5M
            current_match.strstart = (uint16_t)s->strstart;
207
24.5M
            current_match.orgstart = current_match.strstart;
208
209
            /* Find the longest match, discarding those <= prev_length.
210
             * At this point we have always match_length < WANT_MIN_MATCH
211
             */
212
213
24.5M
            dist = (int64_t)s->strstart - hash_head;
214
24.5M
            if (dist <= MAX_DIST(s) && dist > 0 && hash_head != 0) {
215
                /* To simplify the code, we prevent matches with the string
216
                 * of window index 0 (in particular we have to avoid a match
217
                 * of the string with itself at the start of the input file).
218
                 */
219
9.14M
                current_match.match_length = (uint16_t)FUNCTABLE_CALL(longest_match)(s, hash_head);
220
9.14M
                current_match.match_start = (uint16_t)s->match_start;
221
9.14M
                if (UNLIKELY(current_match.match_length < WANT_MIN_MATCH))
222
7.33M
                    current_match.match_length = 1;
223
9.14M
                if (UNLIKELY(current_match.match_start >= current_match.strstart)) {
224
                    /* this can happen due to some restarts */
225
228
                    current_match.match_length = 1;
226
228
                }
227
15.4M
            } else {
228
                /* Set up the match to be a 1 byte literal */
229
15.4M
                current_match.match_start = 0;
230
15.4M
                current_match.match_length = 1;
231
15.4M
            }
232
24.5M
        }
233
234
43.0M
        insert_match(s, current_match);
235
236
        /* now, look ahead one */
237
43.0M
        if (LIKELY(!early_exit && s->lookahead > MIN_LOOKAHEAD && (uint32_t)(current_match.strstart + current_match.match_length) < (s->window_size - MIN_LOOKAHEAD))) {
238
18.4M
            s->strstart = current_match.strstart + current_match.match_length;
239
18.4M
            hash_head = quick_insert_string(s, s->strstart);
240
241
18.4M
            next_match.strstart = (uint16_t)s->strstart;
242
18.4M
            next_match.orgstart = next_match.strstart;
243
244
            /* Find the longest match, discarding those <= prev_length.
245
             * At this point we have always match_length < WANT_MIN_MATCH
246
             */
247
248
18.4M
            dist = (int64_t)s->strstart - hash_head;
249
18.4M
            if (dist <= MAX_DIST(s) && dist > 0 && hash_head != 0) {
250
                /* To simplify the code, we prevent matches with the string
251
                 * of window index 0 (in particular we have to avoid a match
252
                 * of the string with itself at the start of the input file).
253
                 */
254
5.82M
                next_match.match_length = (uint16_t)FUNCTABLE_CALL(longest_match)(s, hash_head);
255
5.82M
                next_match.match_start = (uint16_t)s->match_start;
256
5.82M
                if (UNLIKELY(next_match.match_start >= next_match.strstart)) {
257
                    /* this can happen due to some restarts */
258
195
                    next_match.match_length = 1;
259
195
                }
260
5.82M
                if (next_match.match_length < WANT_MIN_MATCH)
261
5.02M
                    next_match.match_length = 1;
262
801k
                else
263
801k
                    fizzle_matches(s, &current_match, &next_match);
264
12.6M
            } else {
265
                /* Set up the match to be a 1 byte literal */
266
12.6M
                next_match.match_start = 0;
267
12.6M
                next_match.match_length = 1;
268
12.6M
            }
269
270
18.4M
            s->strstart = current_match.strstart;
271
24.5M
        } else {
272
24.5M
            next_match.match_length = 0;
273
24.5M
        }
274
275
        /* now emit the current match */
276
43.0M
        bflush = emit_match(s, current_match);
277
278
        /* move the "cursor" forward */
279
43.0M
        s->strstart += current_match.match_length;
280
281
43.0M
        if (UNLIKELY(bflush))
282
43.0M
            FLUSH_BLOCK(s, 0);
283
43.0M
    }
284
1.31k
    s->insert = s->strstart < (STD_MIN_MATCH - 1) ? s->strstart : (STD_MIN_MATCH - 1);
285
1.31k
    if (flush == Z_FINISH) {
286
1.31k
        FLUSH_BLOCK(s, 1);
287
1.25k
        return finish_done;
288
1.31k
    }
289
0
    if (UNLIKELY(s->sym_next))
290
0
        FLUSH_BLOCK(s, 0);
291
292
0
    return block_done;
293
0
}
294
#endif