Coverage Report

Created: 2025-12-28 06:36

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
124M
static inline int zng_tr_tally_lit(deflate_state *s, unsigned char c) {
64
    /* c is the unmatched char */
65
124M
#ifdef LIT_MEM
66
124M
    s->d_buf[s->sym_next] = 0;
67
124M
    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
124M
    s->dyn_ltree[c].Freq++;
74
124M
    Tracevv((stderr, "%c", c));
75
124M
    Assert(c <= (STD_MAX_MATCH-STD_MIN_MATCH), "zng_tr_tally: bad literal");
76
124M
    return (s->sym_next == s->sym_end);
77
124M
}
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
63
124M
static inline int zng_tr_tally_lit(deflate_state *s, unsigned char c) {
64
    /* c is the unmatched char */
65
124M
#ifdef LIT_MEM
66
124M
    s->d_buf[s->sym_next] = 0;
67
124M
    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
124M
    s->dyn_ltree[c].Freq++;
74
124M
    Tracevv((stderr, "%c", c));
75
124M
    Assert(c <= (STD_MAX_MATCH-STD_MIN_MATCH), "zng_tr_tally: bad literal");
76
124M
    return (s->sym_next == s->sym_end);
77
124M
}
Unexecuted instantiation: deflate_quick.c:zng_tr_tally_lit
Unexecuted instantiation: deflate_rle.c:zng_tr_tally_lit
Unexecuted instantiation: deflate_slow.c:zng_tr_tally_lit
Unexecuted instantiation: deflate_stored.c:zng_tr_tally_lit
Unexecuted instantiation: trees.c:zng_tr_tally_lit
78
79
3.50M
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
3.50M
#ifdef LIT_MEM
83
3.50M
    Assert(dist <= UINT16_MAX, "dist should fit in uint16_t");
84
3.50M
    Assert(len <= UINT8_MAX, "len should fit in uint8_t");
85
3.50M
    s->d_buf[s->sym_next] = (uint16_t)dist;
86
3.50M
    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
3.50M
    s->matches++;
93
3.50M
    dist--;
94
3.50M
    Assert(dist < MAX_DIST(s) && (uint16_t)d_code(dist) < (uint16_t)D_CODES,
95
3.50M
        "zng_tr_tally: bad match");
96
97
3.50M
    s->dyn_ltree[zng_length_code[len] + LITERALS + 1].Freq++;
98
3.50M
    s->dyn_dtree[d_code(dist)].Freq++;
99
3.50M
    return (s->sym_next == s->sym_end);
100
3.50M
}
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
79
3.50M
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
3.50M
#ifdef LIT_MEM
83
3.50M
    Assert(dist <= UINT16_MAX, "dist should fit in uint16_t");
84
3.50M
    Assert(len <= UINT8_MAX, "len should fit in uint8_t");
85
3.50M
    s->d_buf[s->sym_next] = (uint16_t)dist;
86
3.50M
    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
3.50M
    s->matches++;
93
3.50M
    dist--;
94
3.50M
    Assert(dist < MAX_DIST(s) && (uint16_t)d_code(dist) < (uint16_t)D_CODES,
95
3.50M
        "zng_tr_tally: bad match");
96
97
3.50M
    s->dyn_ltree[zng_length_code[len] + LITERALS + 1].Freq++;
98
3.50M
    s->dyn_dtree[d_code(dist)].Freq++;
99
3.50M
    return (s->sym_next == s->sym_end);
100
3.50M
}
Unexecuted instantiation: deflate_quick.c:zng_tr_tally_dist
Unexecuted instantiation: deflate_rle.c:zng_tr_tally_dist
Unexecuted instantiation: deflate_slow.c:zng_tr_tally_dist
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
21.4k
Z_FORCEINLINE static void flush_pending_inline(PREFIX3(stream) *strm) {
109
21.4k
    uint32_t len;
110
21.4k
    deflate_state *s = strm->state;
111
112
21.4k
    zng_tr_flush_bits(s);
113
21.4k
    len = MIN(s->pending, strm->avail_out);
114
21.4k
    if (len == 0)
115
0
        return;
116
117
21.4k
    Tracev((stderr, "[FLUSH]"));
118
21.4k
    memcpy(strm->next_out, s->pending_out, len);
119
21.4k
    strm->next_out  += len;
120
21.4k
    s->pending_out  += len;
121
21.4k
    strm->total_out += len;
122
21.4k
    strm->avail_out -= len;
123
21.4k
    s->pending      -= len;
124
21.4k
    if (s->pending == 0)
125
21.4k
        s->pending_out = s->pending_buf;
126
21.4k
}
deflate.c:flush_pending_inline
Line
Count
Source
108
21.4k
Z_FORCEINLINE static void flush_pending_inline(PREFIX3(stream) *strm) {
109
21.4k
    uint32_t len;
110
21.4k
    deflate_state *s = strm->state;
111
112
21.4k
    zng_tr_flush_bits(s);
113
21.4k
    len = MIN(s->pending, strm->avail_out);
114
21.4k
    if (len == 0)
115
0
        return;
116
117
21.4k
    Tracev((stderr, "[FLUSH]"));
118
21.4k
    memcpy(strm->next_out, s->pending_out, len);
119
21.4k
    strm->next_out  += len;
120
21.4k
    s->pending_out  += len;
121
21.4k
    strm->total_out += len;
122
21.4k
    strm->avail_out -= len;
123
21.4k
    s->pending      -= len;
124
21.4k
    if (s->pending == 0)
125
21.4k
        s->pending_out = s->pending_buf;
126
21.4k
}
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
2.19M
Z_FORCEINLINE static uint16_t bi_reverse(uint16_t code, int len) {
132
    /* code: the value to invert */
133
    /* len: its bit length */
134
2.19M
    Assert(len >= 1 && len <= 15, "code length must be 1-15");
135
2.19M
    return __builtin_bitreverse16(code) >> (16 - len);
136
2.19M
}
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
2.19M
Z_FORCEINLINE static uint16_t bi_reverse(uint16_t code, int len) {
132
    /* code: the value to invert */
133
    /* len: its bit length */
134
2.19M
    Assert(len >= 1 && len <= 15, "code length must be 1-15");
135
2.19M
    return __builtin_bitreverse16(code) >> (16 - len);
136
2.19M
}
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
10.4k
Z_FORCEINLINE static unsigned read_buf(PREFIX3(stream) *strm, unsigned char *buf, unsigned size) {
145
10.4k
    deflate_state *s = strm->state;
146
10.4k
    uint32_t len = MIN(strm->avail_in, size);
147
148
10.4k
    if (len == 0)
149
0
        return 0;
150
151
10.4k
    if (!DEFLATE_NEED_CHECKSUM(strm)) {
152
0
        memcpy(buf, strm->next_in, len);
153
0
#ifdef GZIP
154
10.4k
    } else if (s->wrap == 2) {
155
0
        FUNCTABLE_CALL(crc32_fold_copy)(&s->crc_fold, buf, strm->next_in, len);
156
0
#endif
157
10.4k
    } else if (s->wrap == 1) {
158
10.4k
        strm->adler = FUNCTABLE_CALL(adler32_copy)(strm->adler, buf, strm->next_in, len);
159
10.4k
    } else {
160
0
        memcpy(buf, strm->next_in, len);
161
0
    }
162
163
10.4k
    strm->avail_in -= len;
164
10.4k
    strm->next_in  += len;
165
10.4k
    strm->total_in += len;
166
10.4k
    return len;
167
10.4k
}
deflate.c:read_buf
Line
Count
Source
144
10.4k
Z_FORCEINLINE static unsigned read_buf(PREFIX3(stream) *strm, unsigned char *buf, unsigned size) {
145
10.4k
    deflate_state *s = strm->state;
146
10.4k
    uint32_t len = MIN(strm->avail_in, size);
147
148
10.4k
    if (len == 0)
149
0
        return 0;
150
151
10.4k
    if (!DEFLATE_NEED_CHECKSUM(strm)) {
152
0
        memcpy(buf, strm->next_in, len);
153
0
#ifdef GZIP
154
10.4k
    } else if (s->wrap == 2) {
155
0
        FUNCTABLE_CALL(crc32_fold_copy)(&s->crc_fold, buf, strm->next_in, len);
156
0
#endif
157
10.4k
    } else if (s->wrap == 1) {
158
10.4k
        strm->adler = FUNCTABLE_CALL(adler32_copy)(strm->adler, buf, strm->next_in, len);
159
10.4k
    } else {
160
0
        memcpy(buf, strm->next_in, len);
161
0
    }
162
163
10.4k
    strm->avail_in -= len;
164
10.4k
    strm->next_in  += len;
165
10.4k
    strm->total_in += len;
166
10.4k
    return len;
167
10.4k
}
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
13.0k
#define FLUSH_BLOCK_ONLY(s, last) { \
174
13.0k
    zng_tr_flush_block(s, (s->block_start >= 0 ? \
175
13.0k
                   (char *)&s->window[(unsigned)s->block_start] : \
176
13.0k
                   NULL), \
177
13.0k
                   (uint32_t)((int)s->strstart - s->block_start), \
178
13.0k
                   (last)); \
179
13.0k
    s->block_start = (int)s->strstart; \
180
13.0k
    PREFIX(flush_pending)(s->strm); \
181
13.0k
}
182
183
/* Same but force premature exit if necessary. */
184
13.0k
#define FLUSH_BLOCK(s, last) { \
185
13.0k
    FLUSH_BLOCK_ONLY(s, last); \
186
13.0k
    if (s->strm->avail_out == 0) return (last) ? finish_started : need_more; \
187
13.0k
}
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