Coverage Report

Created: 2025-08-28 06:40

/src/zlib-ng/deflate_slow.c
Line
Count
Source (jump to first uncovered line)
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 "functable.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
2.43k
Z_INTERNAL block_state deflate_slow(deflate_state *s, int flush) {
18
2.43k
    Pos hash_head;           /* head of hash chain */
19
2.43k
    int bflush;              /* set if current block must be flushed */
20
2.43k
    int64_t dist;
21
2.43k
    uint32_t match_len;
22
2.43k
    match_func longest_match;
23
24
2.43k
    if (s->max_chain_length <= 1024)
25
867
        longest_match = FUNCTABLE_FPTR(longest_match);
26
1.57k
    else
27
1.57k
        longest_match = FUNCTABLE_FPTR(longest_match_slow);
28
29
    /* Process the input block. */
30
37.5M
    for (;;) {
31
        /* Make sure that we always have enough lookahead, except
32
         * at the end of the input file. We need STD_MAX_MATCH bytes
33
         * for the next match, plus WANT_MIN_MATCH bytes to insert the
34
         * string following the next match.
35
         */
36
37.5M
        if (s->lookahead < MIN_LOOKAHEAD) {
37
89.2k
            PREFIX(fill_window)(s);
38
89.2k
            if (UNLIKELY(s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH)) {
39
426
                return need_more;
40
426
            }
41
88.7k
            if (UNLIKELY(s->lookahead == 0))
42
1.83k
                break; /* flush the current block */
43
88.7k
        }
44
45
        /* Insert the string window[strstart .. strstart+2] in the
46
         * dictionary, and set hash_head to the head of the hash chain:
47
         */
48
37.5M
        hash_head = 0;
49
37.5M
        if (LIKELY(s->lookahead >= WANT_MIN_MATCH)) {
50
37.5M
            hash_head = s->quick_insert_string(s, s->strstart);
51
37.5M
        }
52
53
        /* Find the longest match, discarding those <= prev_length.
54
         */
55
37.5M
        s->prev_match = (Pos)s->match_start;
56
37.5M
        match_len = STD_MIN_MATCH - 1;
57
37.5M
        dist = (int64_t)s->strstart - hash_head;
58
59
37.5M
        if (dist <= MAX_DIST(s) && dist > 0 && s->prev_length < s->max_lazy_match && hash_head != 0) {
60
            /* To simplify the code, we prevent matches with the string
61
             * of window index 0 (in particular we have to avoid a match
62
             * of the string with itself at the start of the input file).
63
             */
64
21.2M
            match_len = longest_match(s, hash_head);
65
            /* longest_match() sets match_start */
66
67
21.2M
            if (match_len <= 5 && (s->strategy == Z_FILTERED)) {
68
                /* If prev_match is also WANT_MIN_MATCH, match_start is garbage
69
                 * but we will ignore the current match anyway.
70
                 */
71
3.82M
                match_len = STD_MIN_MATCH - 1;
72
3.82M
            }
73
21.2M
        }
74
        /* If there was a match at the previous step and the current
75
         * match is not better, output the previous match:
76
         */
77
37.5M
        if (s->prev_length >= STD_MIN_MATCH && match_len <= s->prev_length) {
78
4.22M
            unsigned int max_insert = s->strstart + s->lookahead - STD_MIN_MATCH;
79
            /* Do not insert strings in hash table beyond this. */
80
81
4.22M
            Assert((s->strstart-1) <= UINT16_MAX, "strstart-1 should fit in uint16_t");
82
4.22M
            check_match(s, (Pos)(s->strstart - 1), s->prev_match, s->prev_length);
83
84
4.22M
            bflush = zng_tr_tally_dist(s, s->strstart -1 - s->prev_match, s->prev_length - STD_MIN_MATCH);
85
86
            /* Insert in hash table all strings up to the end of the match.
87
             * strstart-1 and strstart are already inserted. If there is not
88
             * enough lookahead, the last two strings are not inserted in
89
             * the hash table.
90
             */
91
4.22M
            s->prev_length -= 1;
92
4.22M
            s->lookahead -= s->prev_length;
93
94
4.22M
            unsigned int mov_fwd = s->prev_length - 1;
95
4.22M
            if (max_insert > s->strstart) {
96
4.22M
                unsigned int insert_cnt = mov_fwd;
97
4.22M
                if (UNLIKELY(insert_cnt > max_insert - s->strstart))
98
871
                    insert_cnt = max_insert - s->strstart;
99
4.22M
                s->insert_string(s, s->strstart + 1, insert_cnt);
100
4.22M
            }
101
4.22M
            s->prev_length = 0;
102
4.22M
            s->match_available = 0;
103
4.22M
            s->strstart += mov_fwd + 1;
104
105
4.22M
            if (UNLIKELY(bflush))
106
4.22M
                FLUSH_BLOCK(s, 0);
107
108
33.2M
        } else if (s->match_available) {
109
            /* If there was no match at the previous position, output a
110
             * single literal. If there was a match but the current match
111
             * is longer, truncate the previous match to a single literal.
112
             */
113
29.0M
            bflush = zng_tr_tally_lit(s, s->window[s->strstart-1]);
114
29.0M
            if (UNLIKELY(bflush))
115
1.60k
                FLUSH_BLOCK_ONLY(s, 0);
116
29.0M
            s->prev_length = match_len;
117
29.0M
            s->strstart++;
118
29.0M
            s->lookahead--;
119
29.0M
            if (UNLIKELY(s->strm->avail_out == 0))
120
151
                return need_more;
121
29.0M
        } else {
122
            /* There is no previous match to compare with, wait for
123
             * the next step to decide.
124
             */
125
4.22M
            s->prev_length = match_len;
126
4.22M
            s->match_available = 1;
127
4.22M
            s->strstart++;
128
4.22M
            s->lookahead--;
129
4.22M
        }
130
37.5M
    }
131
1.83k
    Assert(flush != Z_NO_FLUSH, "no flush?");
132
1.83k
    if (UNLIKELY(s->match_available)) {
133
1.05k
        Z_UNUSED(zng_tr_tally_lit(s, s->window[s->strstart-1]));
134
1.05k
        s->match_available = 0;
135
1.05k
    }
136
1.83k
    s->insert = s->strstart < (STD_MIN_MATCH - 1) ? s->strstart : (STD_MIN_MATCH - 1);
137
1.83k
    if (UNLIKELY(flush == Z_FINISH)) {
138
1.83k
        FLUSH_BLOCK(s, 1);
139
1.79k
        return finish_done;
140
1.83k
    }
141
0
    if (UNLIKELY(s->sym_next))
142
0
        FLUSH_BLOCK(s, 0);
143
0
    return block_done;
144
0
}