Coverage Report

Created: 2026-01-10 06:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/zlib-ng/deflate_p.h
Line
Count
Source
1
/* deflate_p.h -- Private inline functions and macros shared with more than
2
 *                one deflate method
3
 *
4
 * Copyright (C) 1995-2024 Jean-loup Gailly and Mark Adler
5
 * For conditions of distribution and use, see copyright notice in zlib.h
6
 *
7
 */
8
9
#ifndef DEFLATE_P_H
10
#define DEFLATE_P_H
11
12
#include "functable.h"
13
#include "fallback_builtins.h"
14
15
/* Forward declare common non-inlined functions declared in deflate.c */
16
17
#ifdef ZLIB_DEBUG
18
/* ===========================================================================
19
 * Check that the match at match_start is indeed a match.
20
 */
21
static inline void check_match(deflate_state *s, uint32_t start, uint32_t match, int length) {
22
    /* check that the match length is valid*/
23
    if (length < STD_MIN_MATCH || length > STD_MAX_MATCH) {
24
        fprintf(stderr, " start %u, match %u, length %d\n", start, match, length);
25
        z_error("invalid match length");
26
    }
27
    /* check that the match isn't at the same position as the start string */
28
    if (match == start) {
29
        fprintf(stderr, " start %u, match %u, length %d\n", start, match, length);
30
        z_error("invalid match position");
31
    }
32
    /* check that the match is indeed a match */
33
    if (memcmp(s->window + match, s->window + start, length) != 0) {
34
        int32_t i = 0;
35
        fprintf(stderr, " start %u, match %u, length %d\n", start, match, length);
36
        do {
37
            fprintf(stderr, "  %03d: match [%02x] start [%02x]\n", i++,
38
                s->window[match++], s->window[start++]);
39
        } while (--length != 0);
40
        z_error("invalid match");
41
    }
42
    if (z_verbose > 1) {
43
        fprintf(stderr, "\\[%u,%d]", start-match, length);
44
        do {
45
            putc(s->window[start++], stderr);
46
        } while (--length != 0);
47
    }
48
}
49
#else
50
#define check_match(s, start, match, length)
51
#endif
52
53
Z_INTERNAL void PREFIX(flush_pending)(PREFIX3(stream) *strm);
54
55
/* ===========================================================================
56
 * Save the match info and tally the frequency counts. Return true if
57
 * the current block must be flushed.
58
 */
59
60
extern const unsigned char Z_INTERNAL zng_length_code[];
61
extern const unsigned char Z_INTERNAL zng_dist_code[];
62
63
10.9M
static inline int zng_tr_tally_lit(deflate_state *s, unsigned char c) {
64
    /* c is the unmatched char */
65
10.9M
#ifdef LIT_MEM
66
10.9M
    s->d_buf[s->sym_next] = 0;
67
10.9M
    s->l_buf[s->sym_next++] = c;
68
#else
69
    s->sym_buf[s->sym_next++] = 0;
70
    s->sym_buf[s->sym_next++] = 0;
71
    s->sym_buf[s->sym_next++] = c;
72
#endif
73
10.9M
    s->dyn_ltree[c].Freq++;
74
10.9M
    Tracevv((stderr, "%c", c));
75
10.9M
    Assert(c <= (STD_MAX_MATCH-STD_MIN_MATCH), "zng_tr_tally: bad literal");
76
10.9M
    return (s->sym_next == s->sym_end);
77
10.9M
}
Unexecuted instantiation: deflate.c:zng_tr_tally_lit
deflate_fast.c:zng_tr_tally_lit
Line
Count
Source
63
470k
static inline int zng_tr_tally_lit(deflate_state *s, unsigned char c) {
64
    /* c is the unmatched char */
65
470k
#ifdef LIT_MEM
66
470k
    s->d_buf[s->sym_next] = 0;
67
470k
    s->l_buf[s->sym_next++] = c;
68
#else
69
    s->sym_buf[s->sym_next++] = 0;
70
    s->sym_buf[s->sym_next++] = 0;
71
    s->sym_buf[s->sym_next++] = c;
72
#endif
73
470k
    s->dyn_ltree[c].Freq++;
74
470k
    Tracevv((stderr, "%c", c));
75
470k
    Assert(c <= (STD_MAX_MATCH-STD_MIN_MATCH), "zng_tr_tally: bad literal");
76
470k
    return (s->sym_next == s->sym_end);
77
470k
}
deflate_huff.c:zng_tr_tally_lit
Line
Count
Source
63
5.16M
static inline int zng_tr_tally_lit(deflate_state *s, unsigned char c) {
64
    /* c is the unmatched char */
65
5.16M
#ifdef LIT_MEM
66
5.16M
    s->d_buf[s->sym_next] = 0;
67
5.16M
    s->l_buf[s->sym_next++] = c;
68
#else
69
    s->sym_buf[s->sym_next++] = 0;
70
    s->sym_buf[s->sym_next++] = 0;
71
    s->sym_buf[s->sym_next++] = c;
72
#endif
73
5.16M
    s->dyn_ltree[c].Freq++;
74
5.16M
    Tracevv((stderr, "%c", c));
75
5.16M
    Assert(c <= (STD_MAX_MATCH-STD_MIN_MATCH), "zng_tr_tally: bad literal");
76
5.16M
    return (s->sym_next == s->sym_end);
77
5.16M
}
deflate_medium.c:zng_tr_tally_lit
Line
Count
Source
63
1.87M
static inline int zng_tr_tally_lit(deflate_state *s, unsigned char c) {
64
    /* c is the unmatched char */
65
1.87M
#ifdef LIT_MEM
66
1.87M
    s->d_buf[s->sym_next] = 0;
67
1.87M
    s->l_buf[s->sym_next++] = c;
68
#else
69
    s->sym_buf[s->sym_next++] = 0;
70
    s->sym_buf[s->sym_next++] = 0;
71
    s->sym_buf[s->sym_next++] = c;
72
#endif
73
1.87M
    s->dyn_ltree[c].Freq++;
74
1.87M
    Tracevv((stderr, "%c", c));
75
1.87M
    Assert(c <= (STD_MAX_MATCH-STD_MIN_MATCH), "zng_tr_tally: bad literal");
76
1.87M
    return (s->sym_next == s->sym_end);
77
1.87M
}
Unexecuted instantiation: deflate_quick.c:zng_tr_tally_lit
deflate_rle.c:zng_tr_tally_lit
Line
Count
Source
63
1.82M
static inline int zng_tr_tally_lit(deflate_state *s, unsigned char c) {
64
    /* c is the unmatched char */
65
1.82M
#ifdef LIT_MEM
66
1.82M
    s->d_buf[s->sym_next] = 0;
67
1.82M
    s->l_buf[s->sym_next++] = c;
68
#else
69
    s->sym_buf[s->sym_next++] = 0;
70
    s->sym_buf[s->sym_next++] = 0;
71
    s->sym_buf[s->sym_next++] = c;
72
#endif
73
1.82M
    s->dyn_ltree[c].Freq++;
74
1.82M
    Tracevv((stderr, "%c", c));
75
1.82M
    Assert(c <= (STD_MAX_MATCH-STD_MIN_MATCH), "zng_tr_tally: bad literal");
76
1.82M
    return (s->sym_next == s->sym_end);
77
1.82M
}
deflate_slow.c:zng_tr_tally_lit
Line
Count
Source
63
1.62M
static inline int zng_tr_tally_lit(deflate_state *s, unsigned char c) {
64
    /* c is the unmatched char */
65
1.62M
#ifdef LIT_MEM
66
1.62M
    s->d_buf[s->sym_next] = 0;
67
1.62M
    s->l_buf[s->sym_next++] = c;
68
#else
69
    s->sym_buf[s->sym_next++] = 0;
70
    s->sym_buf[s->sym_next++] = 0;
71
    s->sym_buf[s->sym_next++] = c;
72
#endif
73
1.62M
    s->dyn_ltree[c].Freq++;
74
1.62M
    Tracevv((stderr, "%c", c));
75
1.62M
    Assert(c <= (STD_MAX_MATCH-STD_MIN_MATCH), "zng_tr_tally: bad literal");
76
1.62M
    return (s->sym_next == s->sym_end);
77
1.62M
}
Unexecuted instantiation: deflate_stored.c:zng_tr_tally_lit
Unexecuted instantiation: trees.c:zng_tr_tally_lit
78
79
596k
static inline int zng_tr_tally_dist(deflate_state* s, uint32_t dist, uint32_t len) {
80
    /* dist: distance of matched string */
81
    /* len: match length-STD_MIN_MATCH */
82
596k
#ifdef LIT_MEM
83
596k
    Assert(dist <= UINT16_MAX, "dist should fit in uint16_t");
84
596k
    Assert(len <= UINT8_MAX, "len should fit in uint8_t");
85
596k
    s->d_buf[s->sym_next] = (uint16_t)dist;
86
596k
    s->l_buf[s->sym_next++] = (uint8_t)len;
87
#else
88
    s->sym_buf[s->sym_next++] = (uint8_t)(dist);
89
    s->sym_buf[s->sym_next++] = (uint8_t)(dist >> 8);
90
    s->sym_buf[s->sym_next++] = (uint8_t)len;
91
#endif
92
596k
    s->matches++;
93
596k
    dist--;
94
596k
    Assert(dist < MAX_DIST(s) && (uint16_t)d_code(dist) < (uint16_t)D_CODES,
95
596k
        "zng_tr_tally: bad match");
96
97
596k
    s->dyn_ltree[zng_length_code[len] + LITERALS + 1].Freq++;
98
596k
    s->dyn_dtree[d_code(dist)].Freq++;
99
596k
    return (s->sym_next == s->sym_end);
100
596k
}
Unexecuted instantiation: deflate.c:zng_tr_tally_dist
deflate_fast.c:zng_tr_tally_dist
Line
Count
Source
79
74.1k
static inline int zng_tr_tally_dist(deflate_state* s, uint32_t dist, uint32_t len) {
80
    /* dist: distance of matched string */
81
    /* len: match length-STD_MIN_MATCH */
82
74.1k
#ifdef LIT_MEM
83
74.1k
    Assert(dist <= UINT16_MAX, "dist should fit in uint16_t");
84
74.1k
    Assert(len <= UINT8_MAX, "len should fit in uint8_t");
85
74.1k
    s->d_buf[s->sym_next] = (uint16_t)dist;
86
74.1k
    s->l_buf[s->sym_next++] = (uint8_t)len;
87
#else
88
    s->sym_buf[s->sym_next++] = (uint8_t)(dist);
89
    s->sym_buf[s->sym_next++] = (uint8_t)(dist >> 8);
90
    s->sym_buf[s->sym_next++] = (uint8_t)len;
91
#endif
92
74.1k
    s->matches++;
93
74.1k
    dist--;
94
74.1k
    Assert(dist < MAX_DIST(s) && (uint16_t)d_code(dist) < (uint16_t)D_CODES,
95
74.1k
        "zng_tr_tally: bad match");
96
97
74.1k
    s->dyn_ltree[zng_length_code[len] + LITERALS + 1].Freq++;
98
74.1k
    s->dyn_dtree[d_code(dist)].Freq++;
99
74.1k
    return (s->sym_next == s->sym_end);
100
74.1k
}
Unexecuted instantiation: deflate_huff.c:zng_tr_tally_dist
deflate_medium.c:zng_tr_tally_dist
Line
Count
Source
79
178k
static inline int zng_tr_tally_dist(deflate_state* s, uint32_t dist, uint32_t len) {
80
    /* dist: distance of matched string */
81
    /* len: match length-STD_MIN_MATCH */
82
178k
#ifdef LIT_MEM
83
178k
    Assert(dist <= UINT16_MAX, "dist should fit in uint16_t");
84
178k
    Assert(len <= UINT8_MAX, "len should fit in uint8_t");
85
178k
    s->d_buf[s->sym_next] = (uint16_t)dist;
86
178k
    s->l_buf[s->sym_next++] = (uint8_t)len;
87
#else
88
    s->sym_buf[s->sym_next++] = (uint8_t)(dist);
89
    s->sym_buf[s->sym_next++] = (uint8_t)(dist >> 8);
90
    s->sym_buf[s->sym_next++] = (uint8_t)len;
91
#endif
92
178k
    s->matches++;
93
178k
    dist--;
94
178k
    Assert(dist < MAX_DIST(s) && (uint16_t)d_code(dist) < (uint16_t)D_CODES,
95
178k
        "zng_tr_tally: bad match");
96
97
178k
    s->dyn_ltree[zng_length_code[len] + LITERALS + 1].Freq++;
98
178k
    s->dyn_dtree[d_code(dist)].Freq++;
99
178k
    return (s->sym_next == s->sym_end);
100
178k
}
Unexecuted instantiation: deflate_quick.c:zng_tr_tally_dist
deflate_rle.c:zng_tr_tally_dist
Line
Count
Source
79
125k
static inline int zng_tr_tally_dist(deflate_state* s, uint32_t dist, uint32_t len) {
80
    /* dist: distance of matched string */
81
    /* len: match length-STD_MIN_MATCH */
82
125k
#ifdef LIT_MEM
83
125k
    Assert(dist <= UINT16_MAX, "dist should fit in uint16_t");
84
125k
    Assert(len <= UINT8_MAX, "len should fit in uint8_t");
85
125k
    s->d_buf[s->sym_next] = (uint16_t)dist;
86
125k
    s->l_buf[s->sym_next++] = (uint8_t)len;
87
#else
88
    s->sym_buf[s->sym_next++] = (uint8_t)(dist);
89
    s->sym_buf[s->sym_next++] = (uint8_t)(dist >> 8);
90
    s->sym_buf[s->sym_next++] = (uint8_t)len;
91
#endif
92
125k
    s->matches++;
93
125k
    dist--;
94
125k
    Assert(dist < MAX_DIST(s) && (uint16_t)d_code(dist) < (uint16_t)D_CODES,
95
125k
        "zng_tr_tally: bad match");
96
97
125k
    s->dyn_ltree[zng_length_code[len] + LITERALS + 1].Freq++;
98
125k
    s->dyn_dtree[d_code(dist)].Freq++;
99
125k
    return (s->sym_next == s->sym_end);
100
125k
}
deflate_slow.c:zng_tr_tally_dist
Line
Count
Source
79
218k
static inline int zng_tr_tally_dist(deflate_state* s, uint32_t dist, uint32_t len) {
80
    /* dist: distance of matched string */
81
    /* len: match length-STD_MIN_MATCH */
82
218k
#ifdef LIT_MEM
83
218k
    Assert(dist <= UINT16_MAX, "dist should fit in uint16_t");
84
218k
    Assert(len <= UINT8_MAX, "len should fit in uint8_t");
85
218k
    s->d_buf[s->sym_next] = (uint16_t)dist;
86
218k
    s->l_buf[s->sym_next++] = (uint8_t)len;
87
#else
88
    s->sym_buf[s->sym_next++] = (uint8_t)(dist);
89
    s->sym_buf[s->sym_next++] = (uint8_t)(dist >> 8);
90
    s->sym_buf[s->sym_next++] = (uint8_t)len;
91
#endif
92
218k
    s->matches++;
93
218k
    dist--;
94
218k
    Assert(dist < MAX_DIST(s) && (uint16_t)d_code(dist) < (uint16_t)D_CODES,
95
218k
        "zng_tr_tally: bad match");
96
97
218k
    s->dyn_ltree[zng_length_code[len] + LITERALS + 1].Freq++;
98
218k
    s->dyn_dtree[d_code(dist)].Freq++;
99
218k
    return (s->sym_next == s->sym_end);
100
218k
}
Unexecuted instantiation: deflate_stored.c:zng_tr_tally_dist
Unexecuted instantiation: trees.c:zng_tr_tally_dist
101
102
/* =========================================================================
103
 * Flush as much pending output as possible. All deflate() output, except for some
104
 * deflate_stored() output, goes through this function so some applications may wish to
105
 * modify it to avoid allocating a large strm->next_out buffer and copying into it.
106
 * See also read_buf().
107
 */
108
69.2k
Z_FORCEINLINE static void flush_pending_inline(PREFIX3(stream) *strm) {
109
69.2k
    uint32_t len;
110
69.2k
    deflate_state *s = strm->state;
111
112
69.2k
    zng_tr_flush_bits(s);
113
69.2k
    len = MIN(s->pending, strm->avail_out);
114
69.2k
    if (len == 0)
115
0
        return;
116
117
69.2k
    Tracev((stderr, "[FLUSH]"));
118
69.2k
    memcpy(strm->next_out, s->pending_out, len);
119
69.2k
    strm->next_out  += len;
120
69.2k
    s->pending_out  += len;
121
69.2k
    strm->total_out += len;
122
69.2k
    strm->avail_out -= len;
123
69.2k
    s->pending      -= len;
124
69.2k
    if (s->pending == 0)
125
69.2k
        s->pending_out = s->pending_buf;
126
69.2k
}
deflate.c:flush_pending_inline
Line
Count
Source
108
69.2k
Z_FORCEINLINE static void flush_pending_inline(PREFIX3(stream) *strm) {
109
69.2k
    uint32_t len;
110
69.2k
    deflate_state *s = strm->state;
111
112
69.2k
    zng_tr_flush_bits(s);
113
69.2k
    len = MIN(s->pending, strm->avail_out);
114
69.2k
    if (len == 0)
115
0
        return;
116
117
69.2k
    Tracev((stderr, "[FLUSH]"));
118
69.2k
    memcpy(strm->next_out, s->pending_out, len);
119
69.2k
    strm->next_out  += len;
120
69.2k
    s->pending_out  += len;
121
69.2k
    strm->total_out += len;
122
69.2k
    strm->avail_out -= len;
123
69.2k
    s->pending      -= len;
124
69.2k
    if (s->pending == 0)
125
69.2k
        s->pending_out = s->pending_buf;
126
69.2k
}
Unexecuted instantiation: deflate_fast.c:flush_pending_inline
Unexecuted instantiation: deflate_huff.c:flush_pending_inline
Unexecuted instantiation: deflate_medium.c:flush_pending_inline
Unexecuted instantiation: deflate_quick.c:flush_pending_inline
Unexecuted instantiation: deflate_rle.c:flush_pending_inline
Unexecuted instantiation: deflate_slow.c:flush_pending_inline
Unexecuted instantiation: deflate_stored.c:flush_pending_inline
Unexecuted instantiation: trees.c:flush_pending_inline
127
128
/* ===========================================================================
129
 * Reverse the first len bits of a code using bit manipulation
130
 */
131
3.15M
Z_FORCEINLINE static uint16_t bi_reverse(uint16_t code, int len) {
132
    /* code: the value to invert */
133
    /* len: its bit length */
134
3.15M
    Assert(len >= 1 && len <= 15, "code length must be 1-15");
135
3.15M
    return __builtin_bitreverse16(code) >> (16 - len);
136
3.15M
}
Unexecuted instantiation: deflate.c:bi_reverse
Unexecuted instantiation: deflate_fast.c:bi_reverse
Unexecuted instantiation: deflate_huff.c:bi_reverse
Unexecuted instantiation: deflate_medium.c:bi_reverse
Unexecuted instantiation: deflate_quick.c:bi_reverse
Unexecuted instantiation: deflate_rle.c:bi_reverse
Unexecuted instantiation: deflate_slow.c:bi_reverse
Unexecuted instantiation: deflate_stored.c:bi_reverse
trees.c:bi_reverse
Line
Count
Source
131
3.15M
Z_FORCEINLINE static uint16_t bi_reverse(uint16_t code, int len) {
132
    /* code: the value to invert */
133
    /* len: its bit length */
134
3.15M
    Assert(len >= 1 && len <= 15, "code length must be 1-15");
135
3.15M
    return __builtin_bitreverse16(code) >> (16 - len);
136
3.15M
}
137
138
/* ===========================================================================
139
 * Read a new buffer from the current input stream, update the adler32 and total number of
140
 * bytes read.  All deflate() input goes through this function so some applications may wish
141
 * to modify it to avoid allocating a large strm->next_in buffer and copying from it.
142
 * See also flush_pending_inline().
143
 */
144
41.5k
Z_FORCEINLINE static unsigned read_buf(PREFIX3(stream) *strm, unsigned char *buf, unsigned size) {
145
41.5k
    deflate_state *s = strm->state;
146
41.5k
    uint32_t len = MIN(strm->avail_in, size);
147
148
41.5k
    if (len == 0)
149
0
        return 0;
150
151
41.5k
    if (!DEFLATE_NEED_CHECKSUM(strm)) {
152
0
        memcpy(buf, strm->next_in, len);
153
0
#ifdef GZIP
154
41.5k
    } else if (s->wrap == 2) {
155
0
        strm->adler = FUNCTABLE_CALL(crc32_copy)(strm->adler, buf, strm->next_in, len);
156
0
#endif
157
41.5k
    } else if (s->wrap == 1) {
158
32.4k
        strm->adler = FUNCTABLE_CALL(adler32_copy)(strm->adler, buf, strm->next_in, len);
159
32.4k
    } else {
160
9.10k
        memcpy(buf, strm->next_in, len);
161
9.10k
    }
162
163
41.5k
    strm->avail_in -= len;
164
41.5k
    strm->next_in  += len;
165
41.5k
    strm->total_in += len;
166
41.5k
    return len;
167
41.5k
}
deflate.c:read_buf
Line
Count
Source
144
41.5k
Z_FORCEINLINE static unsigned read_buf(PREFIX3(stream) *strm, unsigned char *buf, unsigned size) {
145
41.5k
    deflate_state *s = strm->state;
146
41.5k
    uint32_t len = MIN(strm->avail_in, size);
147
148
41.5k
    if (len == 0)
149
0
        return 0;
150
151
41.5k
    if (!DEFLATE_NEED_CHECKSUM(strm)) {
152
0
        memcpy(buf, strm->next_in, len);
153
0
#ifdef GZIP
154
41.5k
    } else if (s->wrap == 2) {
155
0
        strm->adler = FUNCTABLE_CALL(crc32_copy)(strm->adler, buf, strm->next_in, len);
156
0
#endif
157
41.5k
    } else if (s->wrap == 1) {
158
32.4k
        strm->adler = FUNCTABLE_CALL(adler32_copy)(strm->adler, buf, strm->next_in, len);
159
32.4k
    } else {
160
9.10k
        memcpy(buf, strm->next_in, len);
161
9.10k
    }
162
163
41.5k
    strm->avail_in -= len;
164
41.5k
    strm->next_in  += len;
165
41.5k
    strm->total_in += len;
166
41.5k
    return len;
167
41.5k
}
Unexecuted instantiation: deflate_fast.c:read_buf
Unexecuted instantiation: deflate_huff.c:read_buf
Unexecuted instantiation: deflate_medium.c:read_buf
Unexecuted instantiation: deflate_quick.c:read_buf
Unexecuted instantiation: deflate_rle.c:read_buf
Unexecuted instantiation: deflate_slow.c:read_buf
Unexecuted instantiation: deflate_stored.c:read_buf
Unexecuted instantiation: trees.c:read_buf
168
169
/* ===========================================================================
170
 * Flush the current block, with given end-of-file flag.
171
 * IN assertion: strstart is set to the end of the current match.
172
 */
173
50.4k
#define FLUSH_BLOCK_ONLY(s, last) { \
174
50.4k
    zng_tr_flush_block(s, (s->block_start >= 0 ? \
175
50.4k
                   (char *)&s->window[(unsigned)s->block_start] : \
176
50.4k
                   NULL), \
177
50.4k
                   (uint32_t)((int)s->strstart - s->block_start), \
178
50.4k
                   (last)); \
179
50.4k
    s->block_start = (int)s->strstart; \
180
50.4k
    PREFIX(flush_pending)(s->strm); \
181
50.4k
}
182
183
/* Same but force premature exit if necessary. */
184
45.1k
#define FLUSH_BLOCK(s, last) { \
185
45.1k
    FLUSH_BLOCK_ONLY(s, last); \
186
45.1k
    if (s->strm->avail_out == 0) return (last) ? finish_started : need_more; \
187
45.1k
}
188
189
/* Maximum stored block length in deflate format (not including header). */
190
0
#define MAX_STORED 65535
191
192
/* Compression function. Returns the block state after the call. */
193
typedef block_state (*compress_func) (deflate_state *s, int flush);
194
/* Match function. Returns the longest match. */
195
typedef uint32_t    (*match_func)    (deflate_state *const s, uint32_t cur_match);
196
197
#endif