Coverage Report

Created: 2025-07-11 06:15

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