Coverage Report

Created: 2026-06-07 06:54

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
222M
static int emit_match(deflate_state *s, unsigned char *window, struct match match) {
24
222M
    int bflush = 0;
25
222M
    uint32_t match_len = match.match_length;
26
27
    /* None of the below functions care about s->lookahead, so decrement it early */
28
222M
    s->lookahead -= match_len;
29
30
    /* matches that are not long enough we need to emit as literals */
31
222M
    if (match_len < WANT_MIN_MATCH) {
32
428M
        while (match_len) {
33
214M
            bflush += zng_tr_tally_lit(s, window[match.strstart]);
34
214M
            match_len--;
35
214M
            match.strstart++;
36
214M
        }
37
214M
        return bflush;
38
214M
    }
39
40
8.18M
    check_match(s, match.strstart, match.match_start, match_len);
41
42
8.18M
    bflush += zng_tr_tally_dist(s, match.strstart - match.match_start, match_len - STD_MIN_MATCH);
43
8.18M
    return bflush;
44
222M
}
45
46
/* insert_match assumes: s->lookahead > match.match_length + WANT_MIN_MATCH */
47
222M
static void insert_match(deflate_state *s, unsigned char *window, struct match match) {
48
222M
    uint32_t match_len = match.match_length;
49
222M
    uint32_t strstart = match.strstart;
50
51
    /* matches that are not long enough we need to emit as literals */
52
222M
    if (LIKELY(match_len < WANT_MIN_MATCH)) {
53
214M
        strstart++;
54
214M
        match_len--;
55
214M
        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
214M
        return;
65
214M
    }
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.25M
    if (match_len <= 16 * s->max_insert_length && s->lookahead >= WANT_MIN_MATCH) {
71
7.73M
        match_len--; /* string at strstart already in table */
72
7.73M
        strstart++;
73
74
7.73M
        if (LIKELY(strstart >= match.orgstart)) {
75
7.66M
            if (LIKELY(strstart + match_len - 1 >= match.orgstart)) {
76
7.66M
                insert_string(s, window, strstart, match_len);
77
7.66M
            } else {
78
0
                insert_string(s, window, strstart, match.orgstart - strstart + 1);
79
0
            }
80
7.66M
        } else if (match.orgstart < strstart + match_len) {
81
72.7k
            insert_string(s, window, match.orgstart, strstart + match_len - match.orgstart);
82
72.7k
        }
83
7.73M
    } else {
84
519k
        strstart += match_len;
85
519k
        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
519k
    }
91
8.25M
}
92
93
222M
Z_FORCEINLINE static struct match find_best_match(deflate_state *s, uint32_t hash_head) {
94
222M
    struct match m;
95
222M
    int64_t dist;
96
97
222M
    m.strstart = (uint16_t)s->strstart;
98
222M
    m.orgstart = m.strstart;
99
100
222M
    dist = (int64_t)s->strstart - hash_head;
101
222M
    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
75.4M
        m.match_length = (uint16_t)FUNCTABLE_CALL(longest_match)(s, hash_head);
107
75.4M
        m.match_start = (uint16_t)s->match_start;
108
75.4M
        if (UNLIKELY(m.match_length < WANT_MIN_MATCH))
109
67.1M
            m.match_length = 1;
110
75.4M
        if (UNLIKELY(m.match_start >= m.strstart)) {
111
            /* this can happen due to some restarts */
112
0
            m.match_length = 1;
113
0
        }
114
146M
    } else {
115
        /* Set up the match to be a 1 byte literal */
116
146M
        m.match_start = 0;
117
146M
        m.match_length = 1;
118
146M
    }
119
120
222M
    return m;
121
222M
}
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.29M
static void fizzle_matches(deflate_state *s, unsigned char *window, struct match *current, struct match *next) {
129
2.29M
    unsigned char *match, *orig;
130
2.29M
    struct match c, n;
131
2.29M
    int changed = 0;
132
2.29M
    Pos limit;
133
134
2.29M
    match = window - current->match_length + 1 + next->match_start;
135
2.29M
    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.29M
    if (LIKELY(*match != *orig))
139
937k
        return;
140
141
1.35M
    c = *current;
142
1.35M
    n = *next;
143
144
    /* step one: try to move the "next" match to the left as much as possible */
145
1.35M
    limit = next->strstart > MAX_DIST(s) ? next->strstart - (Pos)MAX_DIST(s) : 0;
146
147
1.35M
    match = window + n.match_start - 1;
148
1.35M
    orig = window + n.strstart - 1;
149
150
9.13M
    while (*match == *orig) {
151
8.01M
        if (UNLIKELY(c.match_length < 1))
152
7.54k
            break;
153
8.00M
        if (UNLIKELY(n.strstart <= limit))
154
0
            break;
155
8.00M
        if (UNLIKELY(n.match_length >= 256))
156
227k
            break;
157
7.77M
        if (UNLIKELY(n.match_start <= 1))
158
39
            break;
159
160
7.77M
        n.strstart--;
161
7.77M
        n.match_start--;
162
7.77M
        n.match_length++;
163
7.77M
        c.match_length--;
164
7.77M
        match--;
165
7.77M
        orig--;
166
7.77M
        changed++;
167
7.77M
    }
168
169
1.35M
    if (changed && c.match_length <= 1 && n.match_length != 2) {
170
72.7k
        n.orgstart++;
171
72.7k
        *current = c;
172
72.7k
        *next = n;
173
1.28M
    } else {
174
1.28M
        return;
175
1.28M
    }
176
1.35M
}
177
178
11.1k
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.1k
    ALIGNED_(16) struct match current_match = {0};
181
11.1k
                 struct match next_match = {0};
182
11.1k
    unsigned char *window = s->window;
183
184
    /* For levels below 5, don't check the next position for a better match */
185
11.1k
    int early_exit = s->level < 5;
186
187
222M
    for (;;) {
188
222M
        uint32_t hash_head = 0;    /* head of the hash chain */
189
222M
        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
222M
        if (s->lookahead < MIN_LOOKAHEAD) {
197
712k
            PREFIX(fill_window)(s);
198
712k
            if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
199
0
                return need_more;
200
0
            }
201
712k
            if (UNLIKELY(s->lookahead == 0))
202
11.1k
                break; /* flush the current block */
203
701k
            next_match.match_length = 0;
204
701k
        }
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
222M
        if (!early_exit && next_match.match_length > 0) {
212
110M
            current_match = next_match;
213
110M
            next_match.match_length = 0;
214
112M
        } else {
215
112M
            hash_head = 0;
216
112M
            if (s->lookahead >= WANT_MIN_MATCH) {
217
112M
                hash_head = quick_insert_string(s, window, s->strstart);
218
112M
            }
219
220
112M
            current_match = find_best_match(s, hash_head);
221
112M
        }
222
223
222M
        if (LIKELY(s->lookahead > (unsigned int)(current_match.match_length + WANT_MIN_MATCH)))
224
222M
            insert_match(s, window, current_match);
225
226
        /* now, look ahead one */
227
222M
        if (LIKELY(!early_exit && s->lookahead > MIN_LOOKAHEAD && (uint32_t)(current_match.strstart + current_match.match_length) < (s->window_size - MIN_LOOKAHEAD))) {
228
110M
            s->strstart = current_match.strstart + current_match.match_length;
229
110M
            hash_head = quick_insert_string(s, window, s->strstart);
230
231
110M
            next_match = find_best_match(s, hash_head);
232
233
110M
            uint32_t tmp_cmatch_len_sub = current_match.match_length - 1;
234
110M
            if (tmp_cmatch_len_sub
235
3.61M
                     && next_match.match_length >= WANT_MIN_MATCH
236
2.29M
                     && tmp_cmatch_len_sub <= next_match.match_start
237
2.29M
                     && tmp_cmatch_len_sub <= next_match.strstart) {
238
2.29M
                fizzle_matches(s, window, &current_match, &next_match);
239
2.29M
            }
240
241
110M
            s->strstart = current_match.strstart;
242
112M
        } else {
243
112M
            next_match.match_length = 0;
244
112M
        }
245
246
        /* now emit the current match */
247
222M
        bflush = emit_match(s, window, current_match);
248
249
        /* move the "cursor" forward */
250
222M
        s->strstart += current_match.match_length;
251
252
222M
        if (UNLIKELY(bflush))
253
222M
            FLUSH_BLOCK(s, window, 0);
254
222M
    }
255
11.1k
    s->insert = s->strstart < (STD_MIN_MATCH - 1) ? s->strstart : (STD_MIN_MATCH - 1);
256
11.1k
    if (flush == Z_FINISH) {
257
11.1k
        FLUSH_BLOCK(s, window, 1);
258
11.1k
        return finish_done;
259
11.1k
    }
260
0
    if (UNLIKELY(s->sym_next))
261
0
        FLUSH_BLOCK(s, window, 0);
262
263
0
    return block_done;
264
0
}
265
#endif