Coverage Report

Created: 2026-09-01 06:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/zlib-ng/trees_emit.h
Line
Count
Source
1
#ifndef TREES_EMIT_H_
2
#define TREES_EMIT_H_
3
4
#include "zbuild.h"
5
#include "trees.h"
6
7
#ifdef ZLIB_DEBUG
8
#  include <ctype.h>
9
#  include <inttypes.h>
10
#endif
11
12
13
/* trees.h */
14
extern Z_INTERNAL const ct_data static_ltree[L_CODES+2];
15
extern Z_INTERNAL const ct_data static_dtree[D_CODES];
16
17
extern Z_INTERNAL const unsigned char zng_dist_code[DIST_CODE_LEN];
18
extern Z_INTERNAL const unsigned char zng_length_code[STD_MAX_MATCH-STD_MIN_MATCH+1];
19
20
/* Combined mask + extra_bits tables for single-lookup optimization */
21
extern Z_INTERNAL const uint16_t lmask_extra[LENGTH_CODES];
22
extern Z_INTERNAL const uint32_t dmask_extra[D_CODES];
23
24
/* Bit buffer and deflate code stderr tracing */
25
#ifdef ZLIB_DEBUG
26
#  define trace_bits(s, value, length) { \
27
        Tracevv((stderr, " l %2d v %4llx ", (int)(length), (long long)(value))); \
28
        Assert(length > 0 && length <= BIT_BUF_SIZE, "invalid length"); \
29
    }
30
#  define trace_code(s, c) \
31
    if (z_verbose > 2) { \
32
        fprintf(stderr, "\ncd %3d ", (c)); \
33
    }
34
#else
35
#  define trace_bits(s, value, length)
36
#  define trace_code(s, c)
37
#endif
38
39
/* If not enough room in bi_buf, use (valid) bits from bi_buf and
40
 * (64 - bi_valid) bits from value, leaving (width - (64-bi_valid))
41
 * unused bits in value.
42
 */
43
36.9M
#define send_bits(s, t_val, t_len, bi_buf, bi_valid) do {\
44
36.9M
    Assert(bi_valid <= 64, "Too many bits in bi_valid");\
45
36.9M
    uint64_t val = (uint64_t)t_val;\
46
36.9M
    uint32_t len = (uint32_t)t_len;\
47
36.9M
    uint32_t total_bits = bi_valid + len;\
48
36.9M
    trace_bits(s, val, len);\
49
36.9M
    sent_bits_add(s, len);\
50
36.9M
    \
51
36.9M
    /* Unconditionally shift and merge values into the buffer */\
52
36.9M
    bi_buf |= val << bi_valid;\
53
36.9M
    \
54
36.9M
    /* Check if the 64-bit boundary was crossed */\
55
36.9M
    if (total_bits >= 64) {\
56
15.1M
        total_bits -= 64;\
57
15.1M
        put_uint64(s, bi_buf);\
58
15.1M
        \
59
15.1M
        /* Secure shift: prevent Undefined Behavior when bi_valid is 0 */\
60
15.1M
        /* If bi_valid is 0, we shift by 0 (via the mask) and overwrite bi_buf completely */\
61
15.1M
        bi_buf = (val >> 1) >> (~bi_valid & 63);\
62
15.1M
    }\
63
36.9M
    bi_valid = total_bits;\
64
36.9M
} while (0)
65
66
/* Send a code of the given tree. c and tree must not have side effects */
67
#ifdef ZLIB_DEBUG
68
#  define send_code(s, c, tree, bi_buf, bi_valid) { \
69
    trace_code(s, c); \
70
    send_bits(s, tree[c].Code, tree[c].Len, bi_buf, bi_valid); \
71
}
72
#else
73
#  define send_code(s, c, tree, bi_buf, bi_valid) \
74
1.46M
    send_bits(s, tree[c].Code, tree[c].Len, bi_buf, bi_valid)
75
#endif
76
77
/* ===========================================================================
78
 * Flush the bit buffer and align the output on a byte boundary
79
 */
80
13.1k
static inline void bi_windup(deflate_state *s) {
81
13.1k
    if (s->bi_valid > 56) {
82
450
        put_uint64(s, s->bi_buf);
83
12.7k
    } else {
84
12.7k
        if (s->bi_valid > 24) {
85
2.11k
            put_uint32(s, (uint32_t)s->bi_buf);
86
2.11k
            s->bi_buf >>= 32;
87
2.11k
            s->bi_valid -= 32;
88
2.11k
        }
89
12.7k
        if (s->bi_valid > 8) {
90
2.33k
            put_short(s, (uint16_t)s->bi_buf);
91
2.33k
            s->bi_buf >>= 16;
92
2.33k
            s->bi_valid -= 16;
93
2.33k
        }
94
12.7k
        if (s->bi_valid > 0) {
95
10.5k
            put_byte(s, s->bi_buf);
96
10.5k
        }
97
12.7k
    }
98
13.1k
    s->bi_used = ((s->bi_valid - 1) & 7) + 1;
99
13.1k
    s->bi_buf = 0;
100
13.1k
    s->bi_valid = 0;
101
13.1k
}
Unexecuted instantiation: deflate_quick.c:bi_windup
trees.c:bi_windup
Line
Count
Source
80
13.1k
static inline void bi_windup(deflate_state *s) {
81
13.1k
    if (s->bi_valid > 56) {
82
450
        put_uint64(s, s->bi_buf);
83
12.7k
    } else {
84
12.7k
        if (s->bi_valid > 24) {
85
2.11k
            put_uint32(s, (uint32_t)s->bi_buf);
86
2.11k
            s->bi_buf >>= 32;
87
2.11k
            s->bi_valid -= 32;
88
2.11k
        }
89
12.7k
        if (s->bi_valid > 8) {
90
2.33k
            put_short(s, (uint16_t)s->bi_buf);
91
2.33k
            s->bi_buf >>= 16;
92
2.33k
            s->bi_valid -= 16;
93
2.33k
        }
94
12.7k
        if (s->bi_valid > 0) {
95
10.5k
            put_byte(s, s->bi_buf);
96
10.5k
        }
97
12.7k
    }
98
13.1k
    s->bi_used = ((s->bi_valid - 1) & 7) + 1;
99
13.1k
    s->bi_buf = 0;
100
13.1k
    s->bi_valid = 0;
101
13.1k
}
102
103
/* ===========================================================================
104
 * Emit literal code
105
 */
106
Z_FORCEINLINE static void zng_emit_lit(deflate_state *s, const ct_data *ltree, unsigned c,
107
0
                                uint64_t *bi_buf, uint32_t *bi_valid) {
108
0
    send_code(s, c, ltree, *bi_buf, *bi_valid);
109
0
    Tracecv(isgraph(c & 0xff), (stderr, " '%c' ", c));
110
0
}
Unexecuted instantiation: deflate_quick.c:zng_emit_lit
Unexecuted instantiation: trees.c:zng_emit_lit
111
112
/* ===========================================================================
113
 * Emit match distance/length code
114
 */
115
static inline uint32_t zng_emit_dist(deflate_state *s, const ct_data *ltree, const ct_data *dtree,
116
2.79M
                                     uint32_t lc, uint32_t dist, uint64_t *bi_buf, uint32_t *bi_valid) {
117
2.79M
    uint64_t match_bits;
118
2.79M
    uint32_t match_bits_len;
119
2.79M
    uint32_t mask_ext;  // Contains both mask and extra, can safely be used directly as mask
120
                        // due to extra bits being outside the range of lc and dist data.
121
2.79M
    uint32_t c, extra;
122
2.79M
    uint8_t code;
123
124
    /* 1. Process Length Code */
125
2.79M
    code = zng_length_code[lc];
126
2.79M
    c = code + LITERALS + 1;
127
2.79M
    Assert(c < L_CODES, "bad l_code");
128
2.79M
    trace_code(s, c);
129
130
    /*    Send length code, len is the match length - STD_MIN_MATCH */
131
2.79M
    match_bits = ltree[c].Code;
132
2.79M
    match_bits_len = ltree[c].Len;
133
134
    /* 2. Get extra bits count and mask */
135
2.79M
    mask_ext = lmask_extra[code];
136
2.79M
    extra = mask_ext >> 8;
137
138
    /*    Send length extra bits */
139
2.79M
    match_bits |= (uint64_t)(lc & mask_ext) << match_bits_len;
140
2.79M
    match_bits_len += extra;
141
142
    /* 3. Process Distance Code */
143
2.79M
    dist--; /* dist is now the match distance - 1 */
144
2.79M
    code = d_code(dist);
145
2.79M
    Assert(code < D_CODES, "bad d_code");
146
2.79M
    trace_code(s, code);
147
148
    /*    Send distance code */
149
2.79M
    match_bits |= ((uint64_t)dtree[code].Code << match_bits_len);
150
2.79M
    match_bits_len += dtree[code].Len;
151
152
    /* 4. Get extra bits count and mask */
153
2.79M
    mask_ext = dmask_extra[code];
154
2.79M
    extra = mask_ext >> 16;
155
156
    /*    Send dist extra bits */
157
2.79M
    match_bits |= ((uint64_t)(dist & mask_ext) << match_bits_len);
158
2.79M
    match_bits_len += extra;
159
160
2.79M
    send_bits(s, match_bits, match_bits_len, *bi_buf, *bi_valid);
161
162
2.79M
    return match_bits_len;
163
2.79M
}
Unexecuted instantiation: deflate_quick.c:zng_emit_dist
trees.c:zng_emit_dist
Line
Count
Source
116
2.79M
                                     uint32_t lc, uint32_t dist, uint64_t *bi_buf, uint32_t *bi_valid) {
117
2.79M
    uint64_t match_bits;
118
2.79M
    uint32_t match_bits_len;
119
2.79M
    uint32_t mask_ext;  // Contains both mask and extra, can safely be used directly as mask
120
                        // due to extra bits being outside the range of lc and dist data.
121
2.79M
    uint32_t c, extra;
122
2.79M
    uint8_t code;
123
124
    /* 1. Process Length Code */
125
2.79M
    code = zng_length_code[lc];
126
2.79M
    c = code + LITERALS + 1;
127
2.79M
    Assert(c < L_CODES, "bad l_code");
128
2.79M
    trace_code(s, c);
129
130
    /*    Send length code, len is the match length - STD_MIN_MATCH */
131
2.79M
    match_bits = ltree[c].Code;
132
2.79M
    match_bits_len = ltree[c].Len;
133
134
    /* 2. Get extra bits count and mask */
135
2.79M
    mask_ext = lmask_extra[code];
136
2.79M
    extra = mask_ext >> 8;
137
138
    /*    Send length extra bits */
139
2.79M
    match_bits |= (uint64_t)(lc & mask_ext) << match_bits_len;
140
2.79M
    match_bits_len += extra;
141
142
    /* 3. Process Distance Code */
143
2.79M
    dist--; /* dist is now the match distance - 1 */
144
2.79M
    code = d_code(dist);
145
2.79M
    Assert(code < D_CODES, "bad d_code");
146
2.79M
    trace_code(s, code);
147
148
    /*    Send distance code */
149
2.79M
    match_bits |= ((uint64_t)dtree[code].Code << match_bits_len);
150
2.79M
    match_bits_len += dtree[code].Len;
151
152
    /* 4. Get extra bits count and mask */
153
2.79M
    mask_ext = dmask_extra[code];
154
2.79M
    extra = mask_ext >> 16;
155
156
    /*    Send dist extra bits */
157
2.79M
    match_bits |= ((uint64_t)(dist & mask_ext) << match_bits_len);
158
2.79M
    match_bits_len += extra;
159
160
2.79M
    send_bits(s, match_bits, match_bits_len, *bi_buf, *bi_valid);
161
162
2.79M
    return match_bits_len;
163
2.79M
}
164
165
/* ===========================================================================
166
 * Emit end block
167
 */
168
static inline void zng_emit_end_block(deflate_state *s, const ct_data *ltree, const int last,
169
16.6k
                                      uint64_t *bi_buf, uint32_t *bi_valid) {
170
16.6k
    send_code(s, END_BLOCK, ltree, *bi_buf, *bi_valid);
171
16.6k
    Tracev((stderr, "\n+++ Emit End Block: Last: %u Pending: %u Total Out: %" PRIu64 "\n",
172
16.6k
        last, s->pending, (uint64_t)s->strm->total_out));
173
16.6k
    Z_UNUSED(last);
174
16.6k
}
Unexecuted instantiation: deflate_quick.c:zng_emit_end_block
trees.c:zng_emit_end_block
Line
Count
Source
169
16.6k
                                      uint64_t *bi_buf, uint32_t *bi_valid) {
170
16.6k
    send_code(s, END_BLOCK, ltree, *bi_buf, *bi_valid);
171
16.6k
    Tracev((stderr, "\n+++ Emit End Block: Last: %u Pending: %u Total Out: %" PRIu64 "\n",
172
16.6k
        last, s->pending, (uint64_t)s->strm->total_out));
173
16.6k
    Z_UNUSED(last);
174
16.6k
}
175
176
/* ===========================================================================
177
 * Emit literal and count bits
178
 */
179
0
static inline void zng_tr_emit_lit(deflate_state *s, const ct_data *ltree, unsigned c) {
180
0
    uint64_t bi_buf = s->bi_buf;
181
0
    uint32_t bi_valid = s->bi_valid;
182
0
    zng_emit_lit(s, ltree, c, &bi_buf, &bi_valid);
183
0
    s->bi_buf = bi_buf;
184
0
    s->bi_valid = bi_valid;
185
0
    cmpr_bits_add(s, ltree[c].Len);
186
0
}
Unexecuted instantiation: deflate_quick.c:zng_tr_emit_lit
Unexecuted instantiation: trees.c:zng_tr_emit_lit
187
188
/* ===========================================================================
189
 * Emit match and count bits
190
 */
191
static inline void zng_tr_emit_dist(deflate_state *s, const ct_data *ltree, const ct_data *dtree,
192
0
    uint32_t lc, uint32_t dist) {
193
0
    uint64_t bi_buf = s->bi_buf;
194
0
    uint32_t bi_valid = s->bi_valid;
195
0
    uint32_t bits = zng_emit_dist(s, ltree, dtree, lc, dist, &bi_buf, &bi_valid);
196
0
    s->bi_buf = bi_buf;
197
0
    s->bi_valid = bi_valid;
198
0
    cmpr_bits_add(s, bits);
199
0
}
Unexecuted instantiation: deflate_quick.c:zng_tr_emit_dist
Unexecuted instantiation: trees.c:zng_tr_emit_dist
200
201
/* ===========================================================================
202
 * Emit start of block
203
 */
204
25.1k
static inline void zng_tr_emit_tree(deflate_state *s, int type, const int last) {
205
25.1k
    uint32_t bi_valid = s->bi_valid;
206
25.1k
    uint64_t bi_buf = s->bi_buf;
207
25.1k
    uint32_t header_bits = (type << 1) + last;
208
25.1k
    send_bits(s, header_bits, 3, bi_buf, bi_valid);
209
25.1k
    cmpr_bits_add(s, 3);
210
25.1k
    s->bi_valid = bi_valid;
211
25.1k
    s->bi_buf = bi_buf;
212
25.1k
    Tracev((stderr, "\n--- Emit Tree: Last: %u\n", last));
213
25.1k
}
Unexecuted instantiation: deflate_quick.c:zng_tr_emit_tree
trees.c:zng_tr_emit_tree
Line
Count
Source
204
25.1k
static inline void zng_tr_emit_tree(deflate_state *s, int type, const int last) {
205
25.1k
    uint32_t bi_valid = s->bi_valid;
206
25.1k
    uint64_t bi_buf = s->bi_buf;
207
25.1k
    uint32_t header_bits = (type << 1) + last;
208
25.1k
    send_bits(s, header_bits, 3, bi_buf, bi_valid);
209
25.1k
    cmpr_bits_add(s, 3);
210
25.1k
    s->bi_valid = bi_valid;
211
25.1k
    s->bi_buf = bi_buf;
212
25.1k
    Tracev((stderr, "\n--- Emit Tree: Last: %u\n", last));
213
25.1k
}
214
215
/* ===========================================================================
216
 * Align bit buffer on a byte boundary and count bits
217
 */
218
13.1k
static inline void zng_tr_emit_align(deflate_state *s) {
219
13.1k
    bi_windup(s); /* align on byte boundary */
220
13.1k
    sent_bits_align(s);
221
13.1k
}
Unexecuted instantiation: deflate_quick.c:zng_tr_emit_align
trees.c:zng_tr_emit_align
Line
Count
Source
218
13.1k
static inline void zng_tr_emit_align(deflate_state *s) {
219
13.1k
    bi_windup(s); /* align on byte boundary */
220
13.1k
    sent_bits_align(s);
221
13.1k
}
222
223
/* ===========================================================================
224
 * Emit an end block and align bit buffer if last block
225
 */
226
0
static inline void zng_tr_emit_end_block(deflate_state *s, const ct_data *ltree, const int last) {
227
0
    uint64_t bi_buf = s->bi_buf;
228
0
    uint32_t bi_valid = s->bi_valid;
229
0
    zng_emit_end_block(s, ltree, last, &bi_buf, &bi_valid);
230
0
    s->bi_buf = bi_buf;
231
0
    s->bi_valid = bi_valid;
232
0
    cmpr_bits_add(s, 7);
233
0
    if (last)
234
0
        zng_tr_emit_align(s);
235
0
}
Unexecuted instantiation: deflate_quick.c:zng_tr_emit_end_block
Unexecuted instantiation: trees.c:zng_tr_emit_end_block
236
237
#endif