Coverage Report

Created: 2025-12-14 06:55

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
559
Z_INTERNAL block_state deflate_fast(deflate_state *s, int flush) {
22
559
    int bflush = 0;       /* set if current block must be flushed */
23
559
    uint32_t match_len = 0;
24
25
556k
    for (;;) {
26
556k
        uint8_t lc;
27
28
        /* Make sure that we always have enough lookahead, except
29
         * at the end of the input file. We need STD_MAX_MATCH bytes
30
         * for the next match, plus WANT_MIN_MATCH bytes to insert the
31
         * string following the next match.
32
         */
33
556k
        if (s->lookahead < MIN_LOOKAHEAD) {
34
36.4k
            PREFIX(fill_window)(s);
35
36.4k
            if (UNLIKELY(s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH)) {
36
0
                return need_more;
37
0
            }
38
36.4k
            if (UNLIKELY(s->lookahead == 0))
39
559
                break; /* flush the current block */
40
36.4k
        }
41
42
        /* Insert the string window[strstart .. strstart+2] in the
43
         * dictionary, and set hash_head to the head of the hash chain:
44
         */
45
555k
        if (s->lookahead >= WANT_MIN_MATCH) {
46
554k
#if BYTE_ORDER == LITTLE_ENDIAN
47
554k
            uint32_t str_val = zng_memread_4(&s->window[s->strstart]);
48
#else
49
            uint32_t str_val = ZSWAP32(zng_memread_4(&s->window[s->strstart]));
50
#endif
51
554k
            Pos hash_head = quick_insert_value(s, s->strstart, str_val);
52
554k
            int64_t dist = (int64_t)s->strstart - hash_head;
53
554k
            lc = (uint8_t)str_val;
54
55
            /* Find the longest match, discarding those <= prev_length.
56
             * At this point we have always match length < WANT_MIN_MATCH
57
             */
58
554k
            if (dist <= MAX_DIST(s) && dist > 0 && hash_head != 0) {
59
                /* To simplify the code, we prevent matches with the string
60
                 * of window index 0 (in particular we have to avoid a match
61
                 * of the string with itself at the start of the input file).
62
                 */
63
97.5k
                match_len = FUNCTABLE_CALL(longest_match)(s, hash_head);
64
                /* longest_match() sets match_start */
65
97.5k
            }
66
554k
        } else {
67
821
            lc = s->window[s->strstart];
68
821
        }
69
70
555k
        if (match_len >= WANT_MIN_MATCH) {
71
90.9k
            Assert(s->strstart <= UINT16_MAX, "strstart should fit in uint16_t");
72
90.9k
            Assert(s->match_start <= UINT16_MAX, "match_start should fit in uint16_t");
73
90.9k
            check_match(s, (Pos)s->strstart, (Pos)s->match_start, match_len);
74
75
90.9k
            bflush = zng_tr_tally_dist(s, s->strstart - s->match_start, match_len - STD_MIN_MATCH);
76
77
90.9k
            s->lookahead -= match_len;
78
79
            /* Insert new strings in the hash table only if the match length
80
             * is not too large. This saves time but degrades compression.
81
             */
82
90.9k
            if (match_len <= s->max_insert_length && s->lookahead >= WANT_MIN_MATCH) {
83
18.1k
                match_len--; /* string at strstart already in table */
84
18.1k
                s->strstart++;
85
86
18.1k
                insert_string_static(s, s->strstart, match_len);
87
18.1k
                s->strstart += match_len;
88
72.8k
            } else {
89
72.8k
                s->strstart += match_len;
90
72.8k
                quick_insert_string(s, s->strstart + 2 - STD_MIN_MATCH);
91
92
                /* If lookahead < STD_MIN_MATCH, ins_h is garbage, but it does not
93
                 * matter since it will be recomputed at next deflate call.
94
                 */
95
72.8k
            }
96
90.9k
            match_len = 0;
97
464k
        } else {
98
            /* No match, output a literal byte */
99
464k
            bflush = zng_tr_tally_lit(s, lc);
100
464k
            s->lookahead--;
101
464k
            s->strstart++;
102
464k
        }
103
555k
        if (UNLIKELY(bflush))
104
555k
            FLUSH_BLOCK(s, 0);
105
555k
    }
106
559
    s->insert = s->strstart < (STD_MIN_MATCH - 1) ? s->strstart : (STD_MIN_MATCH - 1);
107
108
559
    if (UNLIKELY(flush == Z_FINISH)) {
109
559
        FLUSH_BLOCK(s, 1);
110
559
        return finish_done;
111
559
    }
112
0
    if (UNLIKELY(s->sym_next))
113
0
        FLUSH_BLOCK(s, 0);
114
0
    return block_done;
115
0
}