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
107M
static int emit_match(deflate_state *s, struct match match) {
23
107M
    int bflush = 0;
24
25
    /* matches that are not long enough we need to emit as literals */
26
107M
    if (match.match_length < WANT_MIN_MATCH) {
27
209M
        while (match.match_length) {
28
104M
            bflush += zng_tr_tally_lit(s, s->window[match.strstart]);
29
104M
            s->lookahead--;
30
104M
            match.strstart++;
31
104M
            match.match_length--;
32
104M
        }
33
104M
        return bflush;
34
104M
    }
35
36
2.63M
    check_match(s, match.strstart, match.match_start, match.match_length);
37
38
2.63M
    bflush += zng_tr_tally_dist(s, match.strstart - match.match_start, match.match_length - STD_MIN_MATCH);
39
40
2.63M
    s->lookahead -= match.match_length;
41
2.63M
    return bflush;
42
107M
}
43
44
107M
static void insert_match(deflate_state *s, struct match match) {
45
107M
    if (UNLIKELY(s->lookahead <= (unsigned int)(match.match_length + WANT_MIN_MATCH)))
46
80.9k
        return;
47
48
    /* string at strstart already in table */
49
107M
    match.strstart++;
50
107M
    match.match_length--;
51
52
    /* matches that are not long enough we need to emit as literals */
53
107M
    if (LIKELY(match.match_length < WANT_MIN_MATCH - 1)) {
54
104M
        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
104M
        return;
66
104M
    }
67
68
    /* Insert into hash table. */
69
2.56M
    if (LIKELY(match.strstart >= match.orgstart)) {
70
2.56M
        if (LIKELY(match.strstart + match.match_length - 1 >= match.orgstart)) {
71
2.56M
            insert_string(s, match.strstart, match.match_length);
72
2.56M
        } else {
73
0
            insert_string(s, match.strstart, match.orgstart - match.strstart + 1);
74
0
        }
75
2.56M
    } else if (match.orgstart < match.strstart + match.match_length) {
76
0
        insert_string(s, match.orgstart, match.strstart + match.match_length - match.orgstart);
77
0
    }
78
2.56M
    match.strstart += match.match_length;
79
2.56M
    match.match_length = 0;
80
2.56M
}
81
82
0
static void fizzle_matches(deflate_state *s, struct match *current, struct match *next) {
83
0
    Pos limit;
84
0
    unsigned char *match, *orig;
85
0
    int changed = 0;
86
0
    struct match c, n;
87
    /* step zero: sanity checks */
88
89
0
    if (current->match_length <= 1)
90
0
        return;
91
92
0
    if (UNLIKELY(current->match_length > 1 + next->match_start))
93
0
        return;
94
95
0
    if (UNLIKELY(current->match_length > 1 + next->strstart))
96
0
        return;
97
98
0
    match = s->window - current->match_length + 1 + next->match_start;
99
0
    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
0
    if (LIKELY(*match != *orig))
103
0
        return;
104
105
0
    c = *current;
106
0
    n = *next;
107
108
    /* step one: try to move the "next" match to the left as much as possible */
109
0
    limit = next->strstart > MAX_DIST(s) ? next->strstart - (Pos)MAX_DIST(s) : 0;
110
111
0
    match = s->window + n.match_start - 1;
112
0
    orig = s->window + n.strstart - 1;
113
114
0
    while (*match == *orig) {
115
0
        if (UNLIKELY(c.match_length < 1))
116
0
            break;
117
0
        if (UNLIKELY(n.strstart <= limit))
118
0
            break;
119
0
        if (UNLIKELY(n.match_length >= 256))
120
0
            break;
121
0
        if (UNLIKELY(n.match_start <= 1))
122
0
            break;
123
124
0
        n.strstart--;
125
0
        n.match_start--;
126
0
        n.match_length++;
127
0
        c.match_length--;
128
0
        match--;
129
0
        orig--;
130
0
        changed++;
131
0
    }
132
133
0
    if (!changed)
134
0
        return;
135
136
0
    if (c.match_length <= 1 && n.match_length != 2) {
137
0
        n.orgstart++;
138
0
        *current = c;
139
0
        *next = n;
140
0
    } else {
141
0
        return;
142
0
    }
143
0
}
144
145
150M
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
150M
    ALIGNED_(16) struct match current_match;
148
150M
                 struct match next_match;
149
150
    /* For levels below 5, don't check the next position for a better match */
151
150M
    int early_exit = s->level < 5;
152
153
150M
    memset(&current_match, 0, sizeof(struct match));
154
150M
    memset(&next_match, 0, sizeof(struct match));
155
156
257M
    for (;;) {
157
257M
        Pos hash_head = 0;    /* head of the hash chain */
158
257M
        int bflush = 0;       /* set if current block must be flushed */
159
257M
        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
257M
        if (s->lookahead < MIN_LOOKAHEAD) {
167
257M
            PREFIX(fill_window)(s);
168
257M
            if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
169
150M
                return need_more;
170
150M
            }
171
107M
            if (UNLIKELY(s->lookahead == 0))
172
2.78k
                break; /* flush the current block */
173
107M
            next_match.match_length = 0;
174
107M
        }
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
107M
        if (!early_exit && next_match.match_length > 0) {
182
0
            current_match = next_match;
183
0
            next_match.match_length = 0;
184
107M
        } else {
185
107M
            hash_head = 0;
186
107M
            if (s->lookahead >= WANT_MIN_MATCH) {
187
107M
                hash_head = quick_insert_string(s, s->strstart);
188
107M
            }
189
190
107M
            current_match.strstart = (uint16_t)s->strstart;
191
107M
            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
107M
            dist = (int64_t)s->strstart - hash_head;
198
107M
            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
37.5M
                current_match.match_length = (uint16_t)FUNCTABLE_CALL(longest_match)(s, hash_head);
204
37.5M
                current_match.match_start = (uint16_t)s->match_start;
205
37.5M
                if (UNLIKELY(current_match.match_length < WANT_MIN_MATCH))
206
34.9M
                    current_match.match_length = 1;
207
37.5M
                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
69.6M
            } else {
212
                /* Set up the match to be a 1 byte literal */
213
69.6M
                current_match.match_start = 0;
214
69.6M
                current_match.match_length = 1;
215
69.6M
            }
216
107M
        }
217
218
107M
        insert_match(s, current_match);
219
220
        /* now, look ahead one */
221
107M
        if (LIKELY(!early_exit && s->lookahead > MIN_LOOKAHEAD && (uint32_t)(current_match.strstart + current_match.match_length) < (s->window_size - MIN_LOOKAHEAD))) {
222
0
            s->strstart = current_match.strstart + current_match.match_length;
223
0
            hash_head = quick_insert_string(s, s->strstart);
224
225
0
            next_match.strstart = (uint16_t)s->strstart;
226
0
            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
0
            dist = (int64_t)s->strstart - hash_head;
233
0
            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
0
                next_match.match_length = (uint16_t)FUNCTABLE_CALL(longest_match)(s, hash_head);
239
0
                next_match.match_start = (uint16_t)s->match_start;
240
0
                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
0
                if (next_match.match_length < WANT_MIN_MATCH)
245
0
                    next_match.match_length = 1;
246
0
                else
247
0
                    fizzle_matches(s, &current_match, &next_match);
248
0
            } else {
249
                /* Set up the match to be a 1 byte literal */
250
0
                next_match.match_start = 0;
251
0
                next_match.match_length = 1;
252
0
            }
253
254
0
            s->strstart = current_match.strstart;
255
107M
        } else {
256
107M
            next_match.match_length = 0;
257
107M
        }
258
259
        /* now emit the current match */
260
107M
        bflush = emit_match(s, current_match);
261
262
        /* move the "cursor" forward */
263
107M
        s->strstart += current_match.match_length;
264
265
107M
        if (UNLIKELY(bflush))
266
107M
            FLUSH_BLOCK(s, 0);
267
107M
    }
268
2.78k
    s->insert = s->strstart < (STD_MIN_MATCH - 1) ? s->strstart : (STD_MIN_MATCH - 1);
269
2.78k
    if (flush == Z_FINISH) {
270
2.78k
        FLUSH_BLOCK(s, 1);
271
0
        return finish_done;
272
2.78k
    }
273
0
    if (UNLIKELY(s->sym_next))
274
0
        FLUSH_BLOCK(s, 0);
275
276
0
    return block_done;
277
0
}
278
#endif