Coverage Report

Created: 2025-08-28 06:40

/src/zlib-ng/deflate_fast.c
Line
Count
Source (jump to first uncovered line)
1
/* deflate_fast.c -- compress data using the fast 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
 * Compress as much as possible from the input stream, return the current
14
 * block state.
15
 * This function does not perform lazy evaluation of matches and inserts
16
 * new strings in the dictionary only for unmatched strings or for short
17
 * matches. It is used only for the fast compression options.
18
 */
19
589
Z_INTERNAL block_state deflate_fast(deflate_state *s, int flush) {
20
589
    Pos hash_head;        /* head of the hash chain */
21
589
    int bflush = 0;       /* set if current block must be flushed */
22
589
    int64_t dist;
23
589
    uint32_t match_len = 0;
24
25
11.8M
    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
11.8M
        if (s->lookahead < MIN_LOOKAHEAD) {
32
32.2k
            PREFIX(fill_window)(s);
33
32.2k
            if (UNLIKELY(s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH)) {
34
173
                return need_more;
35
173
            }
36
32.1k
            if (UNLIKELY(s->lookahead == 0))
37
365
                break; /* flush the current block */
38
32.1k
        }
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
11.8M
        if (s->lookahead >= WANT_MIN_MATCH) {
44
11.8M
            hash_head = quick_insert_string(s, s->strstart);
45
11.8M
            dist = (int64_t)s->strstart - hash_head;
46
47
            /* Find the longest match, discarding those <= prev_length.
48
             * At this point we have always match length < WANT_MIN_MATCH
49
             */
50
11.8M
            if (dist <= MAX_DIST(s) && dist > 0 && hash_head != 0) {
51
                /* To simplify the code, we prevent matches with the string
52
                 * of window index 0 (in particular we have to avoid a match
53
                 * of the string with itself at the start of the input file).
54
                 */
55
4.01M
                match_len = FUNCTABLE_CALL(longest_match)(s, hash_head);
56
                /* longest_match() sets match_start */
57
4.01M
            }
58
11.8M
        }
59
60
11.8M
        if (match_len >= WANT_MIN_MATCH) {
61
1.34M
            Assert(s->strstart <= UINT16_MAX, "strstart should fit in uint16_t");
62
1.34M
            Assert(s->match_start <= UINT16_MAX, "match_start should fit in uint16_t");
63
1.34M
            check_match(s, (Pos)s->strstart, (Pos)s->match_start, match_len);
64
65
1.34M
            bflush = zng_tr_tally_dist(s, s->strstart - s->match_start, match_len - STD_MIN_MATCH);
66
67
1.34M
            s->lookahead -= match_len;
68
69
            /* Insert new strings in the hash table only if the match length
70
             * is not too large. This saves time but degrades compression.
71
             */
72
1.34M
            if (match_len <= s->max_insert_length && s->lookahead >= WANT_MIN_MATCH) {
73
474k
                match_len--; /* string at strstart already in table */
74
474k
                s->strstart++;
75
76
474k
                insert_string(s, s->strstart, match_len);
77
474k
                s->strstart += match_len;
78
869k
            } else {
79
869k
                s->strstart += match_len;
80
869k
                quick_insert_string(s, s->strstart + 2 - STD_MIN_MATCH);
81
82
                /* If lookahead < STD_MIN_MATCH, ins_h is garbage, but it does not
83
                 * matter since it will be recomputed at next deflate call.
84
                 */
85
869k
            }
86
1.34M
            match_len = 0;
87
10.5M
        } else {
88
            /* No match, output a literal byte */
89
10.5M
            bflush = zng_tr_tally_lit(s, s->window[s->strstart]);
90
10.5M
            s->lookahead--;
91
10.5M
            s->strstart++;
92
10.5M
        }
93
11.8M
        if (UNLIKELY(bflush))
94
11.8M
            FLUSH_BLOCK(s, 0);
95
11.8M
    }
96
365
    s->insert = s->strstart < (STD_MIN_MATCH - 1) ? s->strstart : (STD_MIN_MATCH - 1);
97
365
    if (UNLIKELY(flush == Z_FINISH)) {
98
365
        FLUSH_BLOCK(s, 1);
99
353
        return finish_done;
100
365
    }
101
0
    if (UNLIKELY(s->sym_next))
102
0
        FLUSH_BLOCK(s, 0);
103
0
    return block_done;
104
0
}