Coverage Report

Created: 2026-09-13 06:38

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/zlib-ng/deflate_slow.c
Line
Count
Source
1
/* deflate_slow.c -- compress data using the slow strategy of deflation algorithm
2
 *
3
 * Copyright (C) 1995-2024 Jean-loup Gailly and Mark Adler
4
 * For conditions of distribution and use, see copyright notice in zlib.h
5
 */
6
7
#include "zbuild.h"
8
#include "deflate.h"
9
#include "deflate_p.h"
10
#include "insert_string_p.h"
11
12
/* ===========================================================================
13
 * Same as deflate_medium, but achieves better compression. We use a lazy
14
 * evaluation for matches: a match is finally adopted only if there is
15
 * no better match at the next window position.
16
 */
17
3.36k
Z_INTERNAL block_state deflate_slow(deflate_state *s, int flush) {
18
3.36k
    longest_match_func longest_match = s->longest_match;
19
3.36k
    insert_batch_func insert_batch = s->insert_batch;
20
3.36k
    unsigned char *window = s->window;
21
3.36k
    int bflush;              /* set if current block must be flushed */
22
3.36k
    int level = s->level;
23
24
    /* Process the input block. */
25
69.9M
    for (;;) {
26
        /* Make sure that we always have enough lookahead, except
27
         * at the end of the input file. We need STD_MAX_MATCH bytes
28
         * for the next match, plus WANT_MIN_MATCH bytes to insert the
29
         * string following the next match.
30
         */
31
69.9M
        if (UNLIKELY(s->lookahead < MIN_LOOKAHEAD)) {
32
157k
            PREFIX(fill_window)(s);
33
157k
            if (UNLIKELY(s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH)) {
34
666
                return need_more;
35
666
            }
36
156k
            if (UNLIKELY(s->lookahead == 0))
37
2.37k
                break; /* flush the current block */
38
156k
        }
39
40
        /* Insert the string window[strstart .. strstart+2] in the
41
         * dictionary, and set hash_head to the head of the hash chain:
42
         */
43
69.9M
        uint32_t hash_head = 0;
44
69.9M
        if (LIKELY(s->lookahead >= WANT_MIN_MATCH)) {
45
69.9M
            if (level >= 9)
46
55.3M
                hash_head = insert_roll(s, window, s->strstart);
47
14.6M
            else
48
14.6M
                hash_head = insert_knuth(s, window, s->strstart);
49
69.9M
        }
50
51
        /* Find the longest match, discarding those <= prev_length.
52
         */
53
69.9M
        s->prev_match = s->match_start;
54
69.9M
        uint32_t match_len = STD_MIN_MATCH - 1;
55
69.9M
        int64_t dist = (int64_t)s->strstart - hash_head;
56
57
69.9M
        if (dist <= MAX_DIST(s) && dist > 0 && s->prev_length < s->max_lazy_match && hash_head != 0) {
58
            /* To simplify the code, we prevent matches with the string
59
             * of window index 0 (in particular we have to avoid a match
60
             * of the string with itself at the start of the input file).
61
             */
62
39.7M
            match_len = longest_match(s, hash_head);
63
            /* longest_match() sets match_start */
64
65
39.7M
            if (match_len <= 5 && (s->strategy == Z_FILTERED)) {
66
                /* If prev_match is also WANT_MIN_MATCH, match_start is garbage
67
                 * but we will ignore the current match anyway.
68
                 */
69
7.28M
                match_len = STD_MIN_MATCH - 1;
70
7.28M
            }
71
39.7M
        }
72
        /* If there was a match at the previous step and the current
73
         * match is not better, output the previous match:
74
         */
75
69.9M
        if (s->prev_length >= STD_MIN_MATCH && match_len <= s->prev_length) {
76
6.61M
            unsigned int max_insert = s->strstart + s->lookahead - STD_MIN_MATCH;
77
            /* Do not insert strings in hash table beyond this. */
78
79
6.61M
            Assert((s->strstart-1) <= UINT16_MAX, "strstart-1 should fit in uint16_t");
80
6.61M
            check_match(s, s->strstart - 1, s->prev_match, s->prev_length);
81
82
6.61M
            bflush = zng_tr_tally_dist(s, s->strstart -1 - s->prev_match, s->prev_length - STD_MIN_MATCH);
83
84
            /* Insert in hash table all strings up to the end of the match.
85
             * strstart-1 and strstart are already inserted. If there is not
86
             * enough lookahead, the last two strings are not inserted in
87
             * the hash table.
88
             */
89
6.61M
            s->prev_length -= 1;
90
6.61M
            s->lookahead -= s->prev_length;
91
92
6.61M
            unsigned int mov_fwd = s->prev_length - 1;
93
6.61M
            if (max_insert > s->strstart) {
94
6.61M
                unsigned int insert_cnt = mov_fwd;
95
6.61M
                if (UNLIKELY(insert_cnt > max_insert - s->strstart))
96
665
                    insert_cnt = max_insert - s->strstart;
97
6.61M
                insert_batch(s, window, s->strstart + 1, insert_cnt);
98
6.61M
            }
99
6.61M
            s->prev_length = 0;
100
6.61M
            s->match_available = 0;
101
6.61M
            s->strstart += mov_fwd + 1;
102
103
6.61M
            if (UNLIKELY(bflush))
104
6.61M
                FLUSH_BLOCK(s, window, 0);
105
106
63.3M
        } else if (s->match_available) {
107
            /* If there was no match at the previous position, output a
108
             * single literal. If there was a match but the current match
109
             * is longer, truncate the previous match to a single literal.
110
             */
111
56.7M
            bflush = zng_tr_tally_lit(s, window[s->strstart-1]);
112
56.7M
            if (UNLIKELY(bflush))
113
3.11k
                FLUSH_BLOCK_ONLY(s, window, 0);
114
56.7M
            s->prev_length = match_len;
115
56.7M
            s->strstart++;
116
56.7M
            s->lookahead--;
117
56.7M
            if (UNLIKELY(s->strm->avail_out == 0))
118
280
                return need_more;
119
56.7M
        } else {
120
            /* There is no previous match to compare with, wait for
121
             * the next step to decide.
122
             */
123
6.61M
            s->prev_length = match_len;
124
6.61M
            s->match_available = 1;
125
6.61M
            s->strstart++;
126
6.61M
            s->lookahead--;
127
6.61M
        }
128
69.9M
    }
129
2.37k
    Assert(flush != Z_NO_FLUSH, "no flush?");
130
2.37k
    if (UNLIKELY(s->match_available)) {
131
1.85k
        Z_UNUSED(zng_tr_tally_lit(s, window[s->strstart-1]));
132
1.85k
        s->match_available = 0;
133
1.85k
    }
134
2.37k
    s->insert = s->strstart < (STD_MIN_MATCH - 1) ? s->strstart : (STD_MIN_MATCH - 1);
135
2.37k
    if (UNLIKELY(flush == Z_FINISH)) {
136
2.37k
        FLUSH_BLOCK(s, window, 1);
137
2.26k
        return finish_done;
138
2.37k
    }
139
0
    if (UNLIKELY(s->sym_next))
140
0
        FLUSH_BLOCK(s, window, 0);
141
0
    return block_done;
142
0
}