Coverage Report

Created: 2026-05-30 06:45

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
0
Z_INTERNAL block_state deflate_fast(deflate_state *s, int flush) {
22
0
    unsigned char *window = s->window;
23
0
    int bflush = 0;       /* set if current block must be flushed */
24
0
    uint32_t match_len = 0;
25
26
0
    for (;;) {
27
0
        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
0
        if (s->lookahead < MIN_LOOKAHEAD) {
35
0
            PREFIX(fill_window)(s);
36
0
            if (UNLIKELY(s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH)) {
37
0
                return need_more;
38
0
            }
39
0
            if (UNLIKELY(s->lookahead == 0))
40
0
                break; /* flush the current block */
41
0
        }
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
0
        if (s->lookahead >= WANT_MIN_MATCH) {
47
0
            uint32_t str_val = Z_U32_FROM_LE(zng_memread_4(window + s->strstart));
48
0
            uint32_t hash_head = quick_insert_value(s, s->strstart, str_val);
49
0
            int64_t dist = (int64_t)s->strstart - hash_head;
50
0
            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
0
            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
0
                match_len = FUNCTABLE_CALL(longest_match)(s, (uint32_t)hash_head);
61
                /* longest_match() sets match_start */
62
0
            }
63
0
        } else {
64
0
            lc = window[s->strstart];
65
0
        }
66
67
0
        if (match_len >= WANT_MIN_MATCH) {
68
0
            Assert(s->strstart <= UINT16_MAX, "strstart should fit in uint16_t");
69
0
            Assert(s->match_start <= UINT16_MAX, "match_start should fit in uint16_t");
70
0
            check_match(s, s->strstart, s->match_start, match_len);
71
72
0
            bflush = zng_tr_tally_dist(s, s->strstart - s->match_start, match_len - STD_MIN_MATCH);
73
74
0
            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
0
            if (match_len <= s->max_insert_length && s->lookahead >= WANT_MIN_MATCH) {
80
0
                match_len--; /* string at strstart already in table */
81
0
                s->strstart++;
82
83
0
                insert_string_static(s, window, s->strstart, match_len);
84
0
                s->strstart += match_len;
85
0
            } else {
86
0
                s->strstart += match_len;
87
0
                quick_insert_string(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
0
            }
93
0
            match_len = 0;
94
0
        } else {
95
            /* No match, output a literal byte */
96
0
            bflush = zng_tr_tally_lit(s, lc);
97
0
            s->lookahead--;
98
0
            s->strstart++;
99
0
        }
100
0
        if (UNLIKELY(bflush))
101
0
            FLUSH_BLOCK(s, window, 0);
102
0
    }
103
0
    s->insert = s->strstart < (STD_MIN_MATCH - 1) ? s->strstart : (STD_MIN_MATCH - 1);
104
105
0
    if (UNLIKELY(flush == Z_FINISH)) {
106
0
        FLUSH_BLOCK(s, window, 1);
107
0
        return finish_done;
108
0
    }
109
0
    if (UNLIKELY(s->sym_next))
110
0
        FLUSH_BLOCK(s, window, 0);
111
0
    return block_done;
112
0
}