Coverage Report

Created: 2026-09-01 06:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/zlib-ng/deflate_fast.c
Line
Count
Source
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 "zmemory.h"
9
#include "deflate.h"
10
#include "deflate_p.h"
11
#include "functable.h"
12
#include "insert_string_p.h"
13
14
/* ===========================================================================
15
 * Compress as much as possible from the input stream, return the current
16
 * block state.
17
 * This function does not perform lazy evaluation of matches and inserts
18
 * new strings in the dictionary only for unmatched strings or for short
19
 * matches. It is used only for the fast compression options.
20
 */
21
1.40k
Z_INTERNAL block_state deflate_fast(deflate_state *s, int flush) {
22
1.40k
    unsigned char *window = s->window;
23
1.40k
    int bflush = 0;       /* set if current block must be flushed */
24
1.40k
    uint32_t match_len = 0;
25
26
19.7M
    for (;;) {
27
19.7M
        uint8_t lc;
28
29
        /* Make sure that we always have enough lookahead, except
30
         * at the end of the input file. We need STD_MAX_MATCH bytes
31
         * for the next match, plus WANT_MIN_MATCH bytes to insert the
32
         * string following the next match.
33
         */
34
19.7M
        if (UNLIKELY(s->lookahead < MIN_LOOKAHEAD)) {
35
94.4k
            PREFIX(fill_window)(s);
36
94.4k
            if (UNLIKELY(s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH)) {
37
148
                return need_more;
38
148
            }
39
94.2k
            if (UNLIKELY(s->lookahead == 0))
40
1.17k
                break; /* flush the current block */
41
94.2k
        }
42
43
        /* Insert the string window[strstart .. strstart+2] in the
44
         * dictionary, and set hash_head to the head of the hash chain:
45
         */
46
19.7M
        if (LIKELY(s->lookahead >= WANT_MIN_MATCH)) {
47
19.7M
            uint32_t str_val = Z_U32_FROM_LE(zng_memread_4(window + s->strstart));
48
19.7M
            uint32_t hash_head = insert_knuth_val(s, s->strstart, str_val);
49
19.7M
            int64_t dist = (int64_t)s->strstart - hash_head;
50
19.7M
            lc = (uint8_t)str_val;
51
52
            /* Find the longest match.
53
             * At this point we have always match length < WANT_MIN_MATCH
54
             */
55
19.7M
            if (dist <= MAX_DIST(s) && dist > 0 && hash_head != 0) {
56
                /* To simplify the code, we prevent matches with the string
57
                 * of window index 0 (in particular we have to avoid a match
58
                 * of the string with itself at the start of the input file).
59
                 */
60
6.62M
                match_len = FUNCTABLE_CALL(longest_match)(s, (uint32_t)hash_head);
61
                /* longest_match() sets match_start */
62
6.62M
            }
63
19.7M
        } else {
64
2.01k
            lc = window[s->strstart];
65
2.01k
        }
66
67
19.7M
        if (match_len >= WANT_MIN_MATCH) {
68
1.16M
            Assert(s->strstart <= UINT16_MAX, "strstart should fit in uint16_t");
69
1.16M
            Assert(s->match_start <= UINT16_MAX, "match_start should fit in uint16_t");
70
1.16M
            check_match(s, s->strstart, s->match_start, match_len);
71
72
1.16M
            bflush = zng_tr_tally_dist(s, s->strstart - s->match_start, match_len - STD_MIN_MATCH);
73
74
1.16M
            s->lookahead -= match_len;
75
76
            /* Insert new strings in the hash table only if the match length
77
             * is not too large. This saves time but degrades compression.
78
             */
79
1.16M
            if (match_len <= s->max_insert_length && s->lookahead >= WANT_MIN_MATCH) {
80
730k
                match_len--; /* string at strstart already in table */
81
730k
                s->strstart++;
82
83
730k
                insert_knuth_batch_static(s, window, s->strstart, match_len);
84
730k
                s->strstart += match_len;
85
730k
            } else {
86
436k
                s->strstart += match_len;
87
436k
                insert_knuth(s, window, s->strstart + 2 - STD_MIN_MATCH);
88
89
                /* If lookahead < STD_MIN_MATCH, ins_h is garbage, but it does not
90
                 * matter since it will be recomputed at next deflate call.
91
                 */
92
436k
            }
93
1.16M
            match_len = 0;
94
18.5M
        } else {
95
            /* No match, output a literal byte */
96
18.5M
            bflush = zng_tr_tally_lit(s, lc);
97
18.5M
            s->lookahead--;
98
18.5M
            s->strstart++;
99
18.5M
        }
100
19.7M
        if (UNLIKELY(bflush))
101
19.7M
            FLUSH_BLOCK(s, window, 0);
102
19.7M
    }
103
1.17k
    s->insert = s->strstart < (STD_MIN_MATCH - 1) ? s->strstart : (STD_MIN_MATCH - 1);
104
105
1.17k
    if (UNLIKELY(flush == Z_FINISH)) {
106
1.17k
        FLUSH_BLOCK(s, window, 1);
107
1.16k
        return finish_done;
108
1.17k
    }
109
0
    if (UNLIKELY(s->sym_next))
110
0
        FLUSH_BLOCK(s, window, 0);
111
0
    return block_done;
112
0
}