Coverage Report

Created: 2026-02-24 06:18

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