Coverage Report

Created: 2025-12-14 06:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/c-blosc2/internal-complibs/zlib-ng-2.0.7/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 <stdint.h>
11
#include "zbuild.h"
12
#include "deflate.h"
13
#include "deflate_p.h"
14
#include "functable.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
12.7M
static int emit_match(deflate_state *s, struct match match) {
24
12.7M
    int bflush = 0;
25
26
    /* matches that are not long enough we need to emit as literals */
27
12.7M
    if (match.match_length < MIN_MATCH) {
28
24.3M
        while (match.match_length) {
29
12.1M
            bflush += zng_tr_tally_lit(s, s->window[match.strstart]);
30
12.1M
            s->lookahead--;
31
12.1M
            match.strstart++;
32
12.1M
            match.match_length--;
33
12.1M
        }
34
12.1M
        return bflush;
35
12.1M
    }
36
37
570k
    check_match(s, match.strstart, match.match_start, match.match_length);
38
39
570k
    bflush += zng_tr_tally_dist(s, match.strstart - match.match_start, match.match_length - MIN_MATCH);
40
41
570k
    s->lookahead -= match.match_length;
42
570k
    return bflush;
43
12.7M
}
44
45
12.7M
static void insert_match(deflate_state *s, struct match match) {
46
12.7M
    if (UNLIKELY(s->lookahead <= (unsigned int)(match.match_length + MIN_MATCH)))
47
18.1k
        return;
48
49
    /* matches that are not long enough we need to emit as literals */
50
12.7M
    if (LIKELY(match.match_length < MIN_MATCH)) {
51
12.1M
        match.strstart++;
52
12.1M
        match.match_length--;
53
12.1M
        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
                    functable.insert_string(s, match.strstart, match.match_length);
57
0
                } else {
58
0
                    functable.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
12.1M
        return;
65
12.1M
    }
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
599k
    if (match.match_length <= 16* s->max_insert_length && s->lookahead >= MIN_MATCH) {
71
538k
        match.match_length--; /* string at strstart already in table */
72
538k
        match.strstart++;
73
74
538k
        if (LIKELY(match.strstart >= match.orgstart)) {
75
504k
            if (LIKELY(match.strstart + match.match_length - 1 >= match.orgstart)) {
76
504k
                functable.insert_string(s, match.strstart, match.match_length);
77
504k
            } else {
78
0
                functable.insert_string(s, match.strstart, match.orgstart - match.strstart + 1);
79
0
            }
80
504k
        } else if (match.orgstart < match.strstart + match.match_length) {
81
33.7k
            functable.insert_string(s, match.orgstart, match.strstart + match.match_length - match.orgstart);
82
33.7k
        }
83
538k
        match.strstart += match.match_length;
84
538k
        match.match_length = 0;
85
538k
    } else {
86
61.3k
        match.strstart += match.match_length;
87
61.3k
        match.match_length = 0;
88
61.3k
        if (match.strstart >= (MIN_MATCH - 2))
89
#if MIN_MATCH != 3
90
            functable.insert_string(s, match.strstart + 2 - MIN_MATCH, MIN_MATCH - 2);
91
#else
92
61.3k
            functable.quick_insert_string(s, match.strstart + 2 - MIN_MATCH);
93
61.3k
#endif
94
        /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not
95
         * matter since it will be recomputed at next deflate call.
96
         */
97
61.3k
    }
98
599k
}
99
100
574k
static void fizzle_matches(deflate_state *s, struct match *current, struct match *next) {
101
574k
    Pos limit;
102
574k
    unsigned char *match, *orig;
103
574k
    int changed = 0;
104
574k
    struct match c, n;
105
    /* step zero: sanity checks */
106
107
574k
    if (current->match_length <= 1)
108
259k
        return;
109
110
315k
    if (UNLIKELY(current->match_length > 1 + next->match_start))
111
15.9k
        return;
112
113
299k
    if (UNLIKELY(current->match_length > 1 + next->strstart))
114
0
        return;
115
116
299k
    match = s->window - current->match_length + 1 + next->match_start;
117
299k
    orig  = s->window - current->match_length + 1 + next->strstart;
118
119
    /* quick exit check.. if this fails then don't bother with anything else */
120
299k
    if (LIKELY(*match != *orig))
121
145k
        return;
122
123
153k
    c = *current;
124
153k
    n = *next;
125
126
    /* step one: try to move the "next" match to the left as much as possible */
127
153k
    limit = next->strstart > MAX_DIST(s) ? next->strstart - (Pos)MAX_DIST(s) : 0;
128
129
153k
    match = s->window + n.match_start - 1;
130
153k
    orig = s->window + n.strstart - 1;
131
132
2.49M
    while (*match == *orig) {
133
2.39M
        if (UNLIKELY(c.match_length < 1))
134
9.10k
            break;
135
2.38M
        if (UNLIKELY(n.strstart <= limit))
136
0
            break;
137
2.38M
        if (UNLIKELY(n.match_length >= 256))
138
43.2k
            break;
139
2.34M
        if (UNLIKELY(n.match_start <= 1))
140
474
            break;
141
142
2.33M
        n.strstart--;
143
2.33M
        n.match_start--;
144
2.33M
        n.match_length++;
145
2.33M
        c.match_length--;
146
2.33M
        match--;
147
2.33M
        orig--;
148
2.33M
        changed++;
149
2.33M
    }
150
151
153k
    if (!changed)
152
58.8k
        return;
153
154
95.1k
    if (c.match_length <= 1 && n.match_length != 2) {
155
36.4k
        n.orgstart++;
156
36.4k
        *current = c;
157
36.4k
        *next = n;
158
58.6k
    } else {
159
58.6k
        return;
160
58.6k
    }
161
95.1k
}
162
163
9.94k
Z_INTERNAL block_state deflate_medium(deflate_state *s, int flush) {
164
    /* Align the first struct to start on a new cacheline, this allows us to fit both structs in one cacheline */
165
9.94k
    ALIGNED_(16) struct match current_match;
166
9.94k
                 struct match next_match;
167
168
9.94k
    memset(&current_match, 0, sizeof(struct match));
169
9.94k
    memset(&next_match, 0, sizeof(struct match));
170
171
12.7M
    for (;;) {
172
12.7M
        Pos hash_head = 0;    /* head of the hash chain */
173
12.7M
        int bflush = 0;       /* set if current block must be flushed */
174
12.7M
        int64_t dist;
175
176
        /* Make sure that we always have enough lookahead, except
177
         * at the end of the input file. We need MAX_MATCH bytes
178
         * for the next match, plus MIN_MATCH bytes to insert the
179
         * string following the next current_match.
180
         */
181
12.7M
        if (s->lookahead < MIN_LOOKAHEAD) {
182
715k
            fill_window(s);
183
715k
            if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
184
0
                return need_more;
185
0
            }
186
715k
            if (UNLIKELY(s->lookahead == 0))
187
9.94k
                break; /* flush the current block */
188
705k
            next_match.match_length = 0;
189
705k
        }
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
12.7M
        if (next_match.match_length > 0) {
197
12.0M
            current_match = next_match;
198
12.0M
            next_match.match_length = 0;
199
12.0M
        } else {
200
705k
            hash_head = 0;
201
705k
            if (s->lookahead >= MIN_MATCH) {
202
699k
                hash_head = functable.quick_insert_string(s, s->strstart);
203
699k
            }
204
205
705k
            current_match.strstart = (uint16_t)s->strstart;
206
705k
            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 < MIN_MATCH
210
             */
211
212
705k
            dist = (int64_t)s->strstart - hash_head;
213
705k
            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
70.0k
                current_match.match_length = (uint16_t)functable.longest_match(s, hash_head);
219
70.0k
                current_match.match_start = (uint16_t)s->match_start;
220
70.0k
                if (UNLIKELY(current_match.match_length < MIN_MATCH))
221
30.9k
                    current_match.match_length = 1;
222
70.0k
                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
635k
            } else {
227
                /* Set up the match to be a 1 byte literal */
228
635k
                current_match.match_start = 0;
229
635k
                current_match.match_length = 1;
230
635k
            }
231
705k
        }
232
233
12.7M
        insert_match(s, current_match);
234
235
        /* now, look ahead one */
236
12.7M
        if (LIKELY(s->lookahead > MIN_LOOKAHEAD && (uint32_t)(current_match.strstart + current_match.match_length) < (s->window_size - MIN_LOOKAHEAD))) {
237
12.0M
            s->strstart = current_match.strstart + current_match.match_length;
238
12.0M
            hash_head = functable.quick_insert_string(s, s->strstart);
239
240
12.0M
            next_match.strstart = (uint16_t)s->strstart;
241
12.0M
            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 < MIN_MATCH
245
             */
246
247
12.0M
            dist = (int64_t)s->strstart - hash_head;
248
12.0M
            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
837k
                next_match.match_length = (uint16_t)functable.longest_match(s, hash_head);
254
837k
                next_match.match_start = (uint16_t)s->match_start;
255
837k
                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
837k
                if (next_match.match_length < MIN_MATCH)
260
263k
                    next_match.match_length = 1;
261
574k
                else
262
574k
                    fizzle_matches(s, &current_match, &next_match);
263
11.1M
            } else {
264
                /* Set up the match to be a 1 byte literal */
265
11.1M
                next_match.match_start = 0;
266
11.1M
                next_match.match_length = 1;
267
11.1M
            }
268
269
12.0M
            s->strstart = current_match.strstart;
270
12.0M
        } else {
271
698k
            next_match.match_length = 0;
272
698k
        }
273
274
        /* now emit the current match */
275
12.7M
        bflush = emit_match(s, current_match);
276
277
        /* move the "cursor" forward */
278
12.7M
        s->strstart += current_match.match_length;
279
280
12.7M
        if (UNLIKELY(bflush))
281
12.7M
            FLUSH_BLOCK(s, 0);
282
12.7M
    }
283
9.94k
    s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1;
284
9.94k
    if (flush == Z_FINISH) {
285
9.94k
        FLUSH_BLOCK(s, 1);
286
9.05k
        return finish_done;
287
9.94k
    }
288
0
    if (UNLIKELY(s->sym_next))
289
0
        FLUSH_BLOCK(s, 0);
290
291
0
    return block_done;
292
0
}
293
#endif