Coverage Report

Created: 2025-08-28 06:39

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