Coverage Report

Created: 2026-07-16 06:59

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
3.81k
static inline int zng_tr_tally_lit(deflate_state *s, unsigned char c) {
65
    /* c is the unmatched char */
66
3.81k
    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
3.81k
#  if OPTIMAL_CMP >= 32
73
3.81k
    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
3.81k
    s->sym_next = sym_next + 3;
80
3.81k
#endif
81
3.81k
    s->dyn_ltree[c].Freq++;
82
3.81k
    Tracevv((stderr, "%c", c));
83
3.81k
    Assert(c <= (STD_MAX_MATCH-STD_MIN_MATCH), "zng_tr_tally: bad literal");
84
3.81k
    return (s->sym_next == s->sym_end);
85
3.81k
}
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
Unexecuted instantiation: deflate_medium.c:zng_tr_tally_lit
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
3.81k
static inline int zng_tr_tally_lit(deflate_state *s, unsigned char c) {
65
    /* c is the unmatched char */
66
3.81k
    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
3.81k
#  if OPTIMAL_CMP >= 32
73
3.81k
    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
3.81k
    s->sym_next = sym_next + 3;
80
3.81k
#endif
81
3.81k
    s->dyn_ltree[c].Freq++;
82
3.81k
    Tracevv((stderr, "%c", c));
83
3.81k
    Assert(c <= (STD_MAX_MATCH-STD_MIN_MATCH), "zng_tr_tally: bad literal");
84
3.81k
    return (s->sym_next == s->sym_end);
85
3.81k
}
Unexecuted instantiation: deflate_stored.c:zng_tr_tally_lit
Unexecuted instantiation: trees.c:zng_tr_tally_lit
86
87
1.67M
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
1.67M
    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
1.67M
#  if OPTIMAL_CMP >= 32
99
1.67M
    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
1.67M
    s->sym_next = sym_next + 3;
106
1.67M
#endif
107
1.67M
    dist--;
108
1.67M
    Assert(dist < MAX_DIST(s) && (uint16_t)d_code(dist) < (uint16_t)D_CODES,
109
1.67M
        "zng_tr_tally: bad match");
110
111
1.67M
    s->dyn_ltree[zng_length_code[len] + LITERALS + 1].Freq++;
112
1.67M
    s->dyn_dtree[d_code(dist)].Freq++;
113
1.67M
    return (s->sym_next == s->sym_end);
114
1.67M
}
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
Unexecuted instantiation: deflate_medium.c:zng_tr_tally_dist
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
1.67M
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
1.67M
    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
1.67M
#  if OPTIMAL_CMP >= 32
99
1.67M
    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
1.67M
    s->sym_next = sym_next + 3;
106
1.67M
#endif
107
1.67M
    dist--;
108
1.67M
    Assert(dist < MAX_DIST(s) && (uint16_t)d_code(dist) < (uint16_t)D_CODES,
109
1.67M
        "zng_tr_tally: bad match");
110
111
1.67M
    s->dyn_ltree[zng_length_code[len] + LITERALS + 1].Freq++;
112
1.67M
    s->dyn_dtree[d_code(dist)].Freq++;
113
1.67M
    return (s->sym_next == s->sym_end);
114
1.67M
}
Unexecuted instantiation: deflate_stored.c:zng_tr_tally_dist
Unexecuted instantiation: trees.c:zng_tr_tally_dist
115
116
/* =========================================================================
117
 * Flush as much pending output as possible. All deflate() output, except for some
118
 * deflate_stored() output, goes through this function so some applications may wish to
119
 * modify it to avoid allocating a large strm->next_out buffer and copying into it.
120
 * See also read_buf().
121
 */
122
10.6k
Z_FORCEINLINE static void flush_pending_inline(PREFIX3(stream) *strm) {
123
10.6k
    uint32_t len;
124
10.6k
    deflate_state *s = strm->state;
125
126
10.6k
    zng_tr_flush_bits(s);
127
10.6k
    len = MIN(s->pending, strm->avail_out);
128
10.6k
    if (len == 0)
129
3.04k
        return;
130
131
7.60k
    Tracev((stderr, "[FLUSH]"));
132
7.60k
    memcpy(strm->next_out, s->pending_out, len);
133
7.60k
    strm->next_out  += len;
134
7.60k
    s->pending_out  += len;
135
7.60k
    strm->total_out += len;
136
7.60k
    strm->avail_out -= len;
137
7.60k
    s->pending      -= len;
138
7.60k
    if (s->pending == 0)
139
7.60k
        s->pending_out = s->pending_buf;
140
7.60k
}
deflate.c:flush_pending_inline
Line
Count
Source
122
10.6k
Z_FORCEINLINE static void flush_pending_inline(PREFIX3(stream) *strm) {
123
10.6k
    uint32_t len;
124
10.6k
    deflate_state *s = strm->state;
125
126
10.6k
    zng_tr_flush_bits(s);
127
10.6k
    len = MIN(s->pending, strm->avail_out);
128
10.6k
    if (len == 0)
129
3.04k
        return;
130
131
7.60k
    Tracev((stderr, "[FLUSH]"));
132
7.60k
    memcpy(strm->next_out, s->pending_out, len);
133
7.60k
    strm->next_out  += len;
134
7.60k
    s->pending_out  += len;
135
7.60k
    strm->total_out += len;
136
7.60k
    strm->avail_out -= len;
137
7.60k
    s->pending      -= len;
138
7.60k
    if (s->pending == 0)
139
7.60k
        s->pending_out = s->pending_buf;
140
7.60k
}
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
141
142
/* ===========================================================================
143
 * Reverse the first len bits of a code using bit manipulation
144
 */
145
28.7k
Z_FORCEINLINE static uint16_t bi_reverse(uint16_t code, int len) {
146
    /* code: the value to invert */
147
    /* len: its bit length */
148
28.7k
    Assert(len >= 1 && len <= 15, "code length must be 1-15");
149
28.7k
    return zng_bitreverse16(code) >> (16 - len);
150
28.7k
}
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
145
28.7k
Z_FORCEINLINE static uint16_t bi_reverse(uint16_t code, int len) {
146
    /* code: the value to invert */
147
    /* len: its bit length */
148
28.7k
    Assert(len >= 1 && len <= 15, "code length must be 1-15");
149
28.7k
    return zng_bitreverse16(code) >> (16 - len);
150
28.7k
}
151
152
/* ===========================================================================
153
 * Read a new buffer from the current input stream, update the adler32 and total number of
154
 * bytes read.  All deflate() input goes through this function so some applications may wish
155
 * to modify it to avoid allocating a large strm->next_in buffer and copying from it.
156
 * See also flush_pending_inline().
157
 */
158
16.2k
Z_FORCEINLINE static unsigned read_buf(PREFIX3(stream) *strm, unsigned char *buf, unsigned size) {
159
16.2k
    deflate_state *s = strm->state;
160
16.2k
    uint32_t len = MIN(strm->avail_in, size);
161
162
16.2k
    if (len == 0)
163
0
        return 0;
164
165
16.2k
    if (!DEFLATE_NEED_CHECKSUM(strm)) {
166
0
        memcpy(buf, strm->next_in, len);
167
0
#ifdef GZIP
168
16.2k
    } else if (s->wrap == 2) {
169
0
        strm->adler = FUNCTABLE_CALL(crc32_copy)(strm->adler, buf, strm->next_in, len);
170
0
#endif
171
16.2k
    } else if (s->wrap == 1) {
172
16.2k
        strm->adler = FUNCTABLE_CALL(adler32_copy)(strm->adler, buf, strm->next_in, len);
173
16.2k
    } else {
174
0
        memcpy(buf, strm->next_in, len);
175
0
    }
176
177
16.2k
    strm->avail_in -= len;
178
16.2k
    strm->next_in  += len;
179
16.2k
    strm->total_in += len;
180
16.2k
    return len;
181
16.2k
}
deflate.c:read_buf
Line
Count
Source
158
14.7k
Z_FORCEINLINE static unsigned read_buf(PREFIX3(stream) *strm, unsigned char *buf, unsigned size) {
159
14.7k
    deflate_state *s = strm->state;
160
14.7k
    uint32_t len = MIN(strm->avail_in, size);
161
162
14.7k
    if (len == 0)
163
0
        return 0;
164
165
14.7k
    if (!DEFLATE_NEED_CHECKSUM(strm)) {
166
0
        memcpy(buf, strm->next_in, len);
167
0
#ifdef GZIP
168
14.7k
    } else if (s->wrap == 2) {
169
0
        strm->adler = FUNCTABLE_CALL(crc32_copy)(strm->adler, buf, strm->next_in, len);
170
0
#endif
171
14.7k
    } else if (s->wrap == 1) {
172
14.7k
        strm->adler = FUNCTABLE_CALL(adler32_copy)(strm->adler, buf, strm->next_in, len);
173
14.7k
    } else {
174
0
        memcpy(buf, strm->next_in, len);
175
0
    }
176
177
14.7k
    strm->avail_in -= len;
178
14.7k
    strm->next_in  += len;
179
14.7k
    strm->total_in += len;
180
14.7k
    return len;
181
14.7k
}
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
deflate_stored.c:read_buf
Line
Count
Source
158
1.52k
Z_FORCEINLINE static unsigned read_buf(PREFIX3(stream) *strm, unsigned char *buf, unsigned size) {
159
1.52k
    deflate_state *s = strm->state;
160
1.52k
    uint32_t len = MIN(strm->avail_in, size);
161
162
1.52k
    if (len == 0)
163
0
        return 0;
164
165
1.52k
    if (!DEFLATE_NEED_CHECKSUM(strm)) {
166
0
        memcpy(buf, strm->next_in, len);
167
0
#ifdef GZIP
168
1.52k
    } else if (s->wrap == 2) {
169
0
        strm->adler = FUNCTABLE_CALL(crc32_copy)(strm->adler, buf, strm->next_in, len);
170
0
#endif
171
1.52k
    } else if (s->wrap == 1) {
172
1.52k
        strm->adler = FUNCTABLE_CALL(adler32_copy)(strm->adler, buf, strm->next_in, len);
173
1.52k
    } else {
174
0
        memcpy(buf, strm->next_in, len);
175
0
    }
176
177
1.52k
    strm->avail_in -= len;
178
1.52k
    strm->next_in  += len;
179
1.52k
    strm->total_in += len;
180
1.52k
    return len;
181
1.52k
}
Unexecuted instantiation: trees.c:read_buf
182
183
/* ===========================================================================
184
 * Flush the current block, with given end-of-file flag.
185
 * IN assertion: strstart is set to the end of the current match.
186
 */
187
3.04k
#define FLUSH_BLOCK_ONLY(s, window, last) { \
188
3.04k
    int block_start = s->block_start; \
189
3.04k
    zng_tr_flush_block(s, (block_start >= 0 ? \
190
3.04k
                   &window[(unsigned)block_start] : \
191
3.04k
                   NULL), \
192
3.04k
                   (uint32_t)((int)s->strstart - block_start), \
193
3.04k
                   (last)); \
194
3.04k
    s->block_start = (int)s->strstart; \
195
3.04k
    PREFIX(flush_pending)(s->strm); \
196
3.04k
}
197
198
/* Same but force premature exit if necessary. */
199
3.04k
#define FLUSH_BLOCK(s, window, last) { \
200
3.04k
    FLUSH_BLOCK_ONLY(s, window, last); \
201
3.04k
    if (s->strm->avail_out == 0) return (last) ? finish_started : need_more; \
202
3.04k
}
203
204
/* Maximum stored block length in deflate format (not including header). */
205
4.56k
#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