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.70k
Z_INTERNAL block_state deflate_slow(deflate_state *s, int flush) {
18
3.70k
    longest_match_func longest_match = s->longest_match;
19
3.70k
    insert_batch_func insert_batch = s->insert_batch;
20
3.70k
    unsigned char *window = s->window;
21
3.70k
    int bflush;              /* set if current block must be flushed */
22
3.70k
    int level = s->level;
23
24
    /* Process the input block. */
25
2.20M
    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
2.20M
        if (UNLIKELY(s->lookahead < MIN_LOOKAHEAD)) {
32
124k
            PREFIX(fill_window)(s);
33
124k
            if (UNLIKELY(s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH)) {
34
0
                return need_more;
35
0
            }
36
124k
            if (UNLIKELY(s->lookahead == 0))
37
3.70k
                break; /* flush the current block */
38
124k
        }
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
2.19M
        uint32_t hash_head = 0;
44
2.19M
        if (LIKELY(s->lookahead >= WANT_MIN_MATCH)) {
45
2.19M
            if (level >= 9)
46
1.66M
                hash_head = insert_roll(s, window, s->strstart);
47
526k
            else
48
526k
                hash_head = insert_knuth(s, window, s->strstart);
49
2.19M
        }
50
51
        /* Find the longest match, discarding those <= prev_length.
52
         */
53
2.19M
        s->prev_match = s->match_start;
54
2.19M
        uint32_t match_len = STD_MIN_MATCH - 1;
55
2.19M
        int64_t dist = (int64_t)s->strstart - hash_head;
56
57
2.19M
        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
557k
            match_len = longest_match(s, hash_head);
63
            /* longest_match() sets match_start */
64
65
557k
            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
44.8k
                match_len = STD_MIN_MATCH - 1;
70
44.8k
            }
71
557k
        }
72
        /* If there was a match at the previous step and the current
73
         * match is not better, output the previous match:
74
         */
75
2.19M
        if (s->prev_length >= STD_MIN_MATCH && match_len <= s->prev_length) {
76
250k
            unsigned int max_insert = s->strstart + s->lookahead - STD_MIN_MATCH;
77
            /* Do not insert strings in hash table beyond this. */
78
79
250k
            Assert((s->strstart-1) <= UINT16_MAX, "strstart-1 should fit in uint16_t");
80
250k
            check_match(s, s->strstart - 1, s->prev_match, s->prev_length);
81
82
250k
            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
250k
            s->prev_length -= 1;
90
250k
            s->lookahead -= s->prev_length;
91
92
250k
            unsigned int mov_fwd = s->prev_length - 1;
93
250k
            if (max_insert > s->strstart) {
94
250k
                unsigned int insert_cnt = mov_fwd;
95
250k
                if (UNLIKELY(insert_cnt > max_insert - s->strstart))
96
2.24k
                    insert_cnt = max_insert - s->strstart;
97
250k
                insert_batch(s, window, s->strstart + 1, insert_cnt);
98
250k
            }
99
250k
            s->prev_length = 0;
100
250k
            s->match_available = 0;
101
250k
            s->strstart += mov_fwd + 1;
102
103
250k
            if (UNLIKELY(bflush))
104
250k
                FLUSH_BLOCK(s, window, 0);
105
106
1.94M
        } 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
1.69M
            bflush = zng_tr_tally_lit(s, window[s->strstart-1]);
112
1.69M
            if (UNLIKELY(bflush))
113
4.72k
                FLUSH_BLOCK_ONLY(s, window, 0);
114
1.69M
            s->prev_length = match_len;
115
1.69M
            s->strstart++;
116
1.69M
            s->lookahead--;
117
1.69M
            if (UNLIKELY(s->strm->avail_out == 0))
118
0
                return need_more;
119
1.69M
        } else {
120
            /* There is no previous match to compare with, wait for
121
             * the next step to decide.
122
             */
123
252k
            s->prev_length = match_len;
124
252k
            s->match_available = 1;
125
252k
            s->strstart++;
126
252k
            s->lookahead--;
127
252k
        }
128
2.19M
    }
129
3.70k
    Assert(flush != Z_NO_FLUSH, "no flush?");
130
3.70k
    if (UNLIKELY(s->match_available)) {
131
1.64k
        Z_UNUSED(zng_tr_tally_lit(s, window[s->strstart-1]));
132
1.64k
        s->match_available = 0;
133
1.64k
    }
134
3.70k
    s->insert = s->strstart < (STD_MIN_MATCH - 1) ? s->strstart : (STD_MIN_MATCH - 1);
135
3.70k
    if (UNLIKELY(flush == Z_FINISH)) {
136
3.70k
        FLUSH_BLOCK(s, window, 1);
137
3.70k
        return finish_done;
138
3.70k
    }
139
0
    if (UNLIKELY(s->sym_next))
140
0
        FLUSH_BLOCK(s, window, 0);
141
0
    return block_done;
142
0
}