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
342M
static inline int zng_tr_tally_lit(deflate_state *s, unsigned char c) {
65
    /* c is the unmatched char */
66
342M
    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
342M
#  if OPTIMAL_CMP >= 32
73
342M
    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
342M
    s->sym_next = sym_next + 3;
80
342M
#endif
81
342M
    s->dyn_ltree[c].Freq++;
82
342M
    Tracevv((stderr, "%c", c));
83
342M
    Assert(c <= (STD_MAX_MATCH-STD_MIN_MATCH), "zng_tr_tally: bad literal");
84
342M
    return (s->sym_next == s->sym_end);
85
342M
}
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
229M
static inline int zng_tr_tally_lit(deflate_state *s, unsigned char c) {
65
    /* c is the unmatched char */
66
229M
    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
229M
#  if OPTIMAL_CMP >= 32
73
229M
    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
229M
    s->sym_next = sym_next + 3;
80
229M
#endif
81
229M
    s->dyn_ltree[c].Freq++;
82
229M
    Tracevv((stderr, "%c", c));
83
229M
    Assert(c <= (STD_MAX_MATCH-STD_MIN_MATCH), "zng_tr_tally: bad literal");
84
229M
    return (s->sym_next == s->sym_end);
85
229M
}
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
113M
static inline int zng_tr_tally_lit(deflate_state *s, unsigned char c) {
65
    /* c is the unmatched char */
66
113M
    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
113M
#  if OPTIMAL_CMP >= 32
73
113M
    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
113M
    s->sym_next = sym_next + 3;
80
113M
#endif
81
113M
    s->dyn_ltree[c].Freq++;
82
113M
    Tracevv((stderr, "%c", c));
83
113M
    Assert(c <= (STD_MAX_MATCH-STD_MIN_MATCH), "zng_tr_tally: bad literal");
84
113M
    return (s->sym_next == s->sym_end);
85
113M
}
Unexecuted instantiation: deflate_stored.c:zng_tr_tally_lit
Unexecuted instantiation: trees.c:zng_tr_tally_lit
86
87
12.0M
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
12.0M
    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
12.0M
#  if OPTIMAL_CMP >= 32
99
12.0M
    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
12.0M
    s->sym_next = sym_next + 3;
106
12.0M
#endif
107
12.0M
    dist--;
108
12.0M
    Assert(dist < MAX_DIST(s) && (uint16_t)d_code(dist) < (uint16_t)D_CODES,
109
12.0M
        "zng_tr_tally: bad match");
110
111
12.0M
    s->dyn_ltree[zng_length_code[len] + LITERALS + 1].Freq++;
112
12.0M
    s->dyn_dtree[d_code(dist)].Freq++;
113
12.0M
    return (s->sym_next == s->sym_end);
114
12.0M
}
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.34M
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.34M
    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.34M
#  if OPTIMAL_CMP >= 32
99
8.34M
    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.34M
    s->sym_next = sym_next + 3;
106
8.34M
#endif
107
8.34M
    dist--;
108
8.34M
    Assert(dist < MAX_DIST(s) && (uint16_t)d_code(dist) < (uint16_t)D_CODES,
109
8.34M
        "zng_tr_tally: bad match");
110
111
8.34M
    s->dyn_ltree[zng_length_code[len] + LITERALS + 1].Freq++;
112
8.34M
    s->dyn_dtree[d_code(dist)].Freq++;
113
8.34M
    return (s->sym_next == s->sym_end);
114
8.34M
}
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.66M
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.66M
    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.66M
#  if OPTIMAL_CMP >= 32
99
3.66M
    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.66M
    s->sym_next = sym_next + 3;
106
3.66M
#endif
107
3.66M
    dist--;
108
3.66M
    Assert(dist < MAX_DIST(s) && (uint16_t)d_code(dist) < (uint16_t)D_CODES,
109
3.66M
        "zng_tr_tally: bad match");
110
111
3.66M
    s->dyn_ltree[zng_length_code[len] + LITERALS + 1].Freq++;
112
3.66M
    s->dyn_dtree[d_code(dist)].Freq++;
113
3.66M
    return (s->sym_next == s->sym_end);
114
3.66M
}
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
87.6k
Z_FORCEINLINE static void flush_pending_inline(PREFIX3(stream) *strm) {
123
87.6k
    uint32_t len;
124
87.6k
    deflate_state *s = strm->state;
125
126
87.6k
    zng_tr_flush_bits(s);
127
87.6k
    len = MIN(s->pending, strm->avail_out);
128
87.6k
    if (len == 0)
129
0
        return;
130
131
87.6k
    Tracev((stderr, "[FLUSH]"));
132
87.6k
    memcpy(strm->next_out, s->pending_out, len);
133
87.6k
    strm->next_out  += len;
134
87.6k
    s->pending_out  += len;
135
87.6k
    strm->total_out += len;
136
87.6k
    strm->avail_out -= len;
137
87.6k
    s->pending      -= len;
138
87.6k
    if (s->pending == 0)
139
87.6k
        s->pending_out = s->pending_buf;
140
87.6k
}
deflate.c:flush_pending_inline
Line
Count
Source
122
87.6k
Z_FORCEINLINE static void flush_pending_inline(PREFIX3(stream) *strm) {
123
87.6k
    uint32_t len;
124
87.6k
    deflate_state *s = strm->state;
125
126
87.6k
    zng_tr_flush_bits(s);
127
87.6k
    len = MIN(s->pending, strm->avail_out);
128
87.6k
    if (len == 0)
129
0
        return;
130
131
87.6k
    Tracev((stderr, "[FLUSH]"));
132
87.6k
    memcpy(strm->next_out, s->pending_out, len);
133
87.6k
    strm->next_out  += len;
134
87.6k
    s->pending_out  += len;
135
87.6k
    strm->total_out += len;
136
87.6k
    strm->avail_out -= len;
137
87.6k
    s->pending      -= len;
138
87.6k
    if (s->pending == 0)
139
87.6k
        s->pending_out = s->pending_buf;
140
87.6k
}
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
6.82M
Z_FORCEINLINE static uint16_t bi_reverse(uint16_t code, int len) {
146
    /* code: the value to invert */
147
    /* len: its bit length */
148
6.82M
    Assert(len >= 1 && len <= 15, "code length must be 1-15");
149
6.82M
    return zng_bitreverse16(code) >> (16 - len);
150
6.82M
}
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
6.82M
Z_FORCEINLINE static uint16_t bi_reverse(uint16_t code, int len) {
146
    /* code: the value to invert */
147
    /* len: its bit length */
148
6.82M
    Assert(len >= 1 && len <= 15, "code length must be 1-15");
149
6.82M
    return zng_bitreverse16(code) >> (16 - len);
150
6.82M
}
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
41.5k
Z_FORCEINLINE static unsigned read_buf(PREFIX3(stream) *strm, unsigned char *buf, unsigned size) {
159
41.5k
    deflate_state *s = strm->state;
160
41.5k
    uint32_t len = MIN(strm->avail_in, size);
161
162
41.5k
    if (len == 0)
163
0
        return 0;
164
165
41.5k
    if (!DEFLATE_NEED_CHECKSUM(strm)) {
166
0
        memcpy(buf, strm->next_in, len);
167
0
#ifdef GZIP
168
41.5k
    } else if (s->wrap == 2) {
169
0
        strm->adler = FUNCTABLE_CALL(crc32_copy)(strm->adler, buf, strm->next_in, len);
170
0
#endif
171
41.5k
    } else if (s->wrap == 1) {
172
41.5k
        strm->adler = FUNCTABLE_CALL(adler32_copy)(strm->adler, buf, strm->next_in, len);
173
41.5k
    } else {
174
0
        memcpy(buf, strm->next_in, len);
175
0
    }
176
177
41.5k
    strm->avail_in -= len;
178
41.5k
    strm->next_in  += len;
179
41.5k
    strm->total_in += len;
180
41.5k
    return len;
181
41.5k
}
deflate.c:read_buf
Line
Count
Source
158
41.5k
Z_FORCEINLINE static unsigned read_buf(PREFIX3(stream) *strm, unsigned char *buf, unsigned size) {
159
41.5k
    deflate_state *s = strm->state;
160
41.5k
    uint32_t len = MIN(strm->avail_in, size);
161
162
41.5k
    if (len == 0)
163
0
        return 0;
164
165
41.5k
    if (!DEFLATE_NEED_CHECKSUM(strm)) {
166
0
        memcpy(buf, strm->next_in, len);
167
0
#ifdef GZIP
168
41.5k
    } else if (s->wrap == 2) {
169
0
        strm->adler = FUNCTABLE_CALL(crc32_copy)(strm->adler, buf, strm->next_in, len);
170
0
#endif
171
41.5k
    } else if (s->wrap == 1) {
172
41.5k
        strm->adler = FUNCTABLE_CALL(adler32_copy)(strm->adler, buf, strm->next_in, len);
173
41.5k
    } else {
174
0
        memcpy(buf, strm->next_in, len);
175
0
    }
176
177
41.5k
    strm->avail_in -= len;
178
41.5k
    strm->next_in  += len;
179
41.5k
    strm->total_in += len;
180
41.5k
    return len;
181
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
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
36.7k
#define FLUSH_BLOCK_ONLY(s, window, last) { \
188
36.7k
    int block_start = s->block_start; \
189
36.7k
    zng_tr_flush_block(s, (block_start >= 0 ? \
190
36.7k
                   &window[(unsigned)block_start] : \
191
36.7k
                   NULL), \
192
36.7k
                   (uint32_t)((int)s->strstart - block_start), \
193
36.7k
                   (last)); \
194
36.7k
    s->block_start = (int)s->strstart; \
195
36.7k
    PREFIX(flush_pending)(s->strm); \
196
36.7k
}
197
198
/* Same but force premature exit if necessary. */
199
30.3k
#define FLUSH_BLOCK(s, window, last) { \
200
30.3k
    FLUSH_BLOCK_ONLY(s, window, last); \
201
30.3k
    if (s->strm->avail_out == 0) return (last) ? finish_started : need_more; \
202
30.3k
}
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