Coverage Report

Created: 2025-08-03 06:21

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