Coverage Report

Created: 2026-09-04 06:47

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
431M
#define send_bits(s, t_val, t_len, bi_buf, bi_valid) do {\
44
431M
    Assert(bi_valid <= 64, "Too many bits in bi_valid");\
45
431M
    uint64_t val = (uint64_t)t_val;\
46
431M
    uint32_t len = (uint32_t)t_len;\
47
431M
    uint32_t total_bits = bi_valid + len;\
48
431M
    trace_bits(s, val, len);\
49
431M
    sent_bits_add(s, len);\
50
431M
    \
51
431M
    /* Unconditionally shift and merge values into the buffer */\
52
431M
    bi_buf |= val << bi_valid;\
53
431M
    \
54
431M
    /* Check if the 64-bit boundary was crossed */\
55
431M
    if (total_bits >= 64) {\
56
110M
        total_bits -= 64;\
57
110M
        put_uint64(s, bi_buf);\
58
110M
        \
59
110M
        /* Secure shift: prevent Undefined Behavior when bi_valid is 0 */\
60
110M
        /* If bi_valid is 0, we shift by 0 (via the mask) and overwrite bi_buf completely */\
61
110M
        bi_buf = (val >> 1) >> (~bi_valid & 63);\
62
110M
    }\
63
431M
    bi_valid = total_bits;\
64
431M
} 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
212M
    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
89.6k
static inline void bi_windup(deflate_state *s) {
81
89.6k
    if (s->bi_valid > 56) {
82
5.71k
        put_uint64(s, s->bi_buf);
83
83.9k
    } else {
84
83.9k
        if (s->bi_valid > 24) {
85
27.5k
            put_uint32(s, (uint32_t)s->bi_buf);
86
27.5k
            s->bi_buf >>= 32;
87
27.5k
            s->bi_valid -= 32;
88
27.5k
        }
89
83.9k
        if (s->bi_valid > 8) {
90
27.9k
            put_short(s, (uint16_t)s->bi_buf);
91
27.9k
            s->bi_buf >>= 16;
92
27.9k
            s->bi_valid -= 16;
93
27.9k
        }
94
83.9k
        if (s->bi_valid > 0) {
95
58.8k
            put_byte(s, s->bi_buf);
96
58.8k
        }
97
83.9k
    }
98
89.6k
    s->bi_used = ((s->bi_valid - 1) & 7) + 1;
99
89.6k
    s->bi_buf = 0;
100
89.6k
    s->bi_valid = 0;
101
89.6k
}
deflate_quick.c:bi_windup
Line
Count
Source
80
7.20k
static inline void bi_windup(deflate_state *s) {
81
7.20k
    if (s->bi_valid > 56) {
82
816
        put_uint64(s, s->bi_buf);
83
6.38k
    } else {
84
6.38k
        if (s->bi_valid > 24) {
85
3.69k
            put_uint32(s, (uint32_t)s->bi_buf);
86
3.69k
            s->bi_buf >>= 32;
87
3.69k
            s->bi_valid -= 32;
88
3.69k
        }
89
6.38k
        if (s->bi_valid > 8) {
90
3.56k
            put_short(s, (uint16_t)s->bi_buf);
91
3.56k
            s->bi_buf >>= 16;
92
3.56k
            s->bi_valid -= 16;
93
3.56k
        }
94
6.38k
        if (s->bi_valid > 0) {
95
3.64k
            put_byte(s, s->bi_buf);
96
3.64k
        }
97
6.38k
    }
98
7.20k
    s->bi_used = ((s->bi_valid - 1) & 7) + 1;
99
7.20k
    s->bi_buf = 0;
100
7.20k
    s->bi_valid = 0;
101
7.20k
}
trees.c:bi_windup
Line
Count
Source
80
82.4k
static inline void bi_windup(deflate_state *s) {
81
82.4k
    if (s->bi_valid > 56) {
82
4.89k
        put_uint64(s, s->bi_buf);
83
77.5k
    } else {
84
77.5k
        if (s->bi_valid > 24) {
85
23.8k
            put_uint32(s, (uint32_t)s->bi_buf);
86
23.8k
            s->bi_buf >>= 32;
87
23.8k
            s->bi_valid -= 32;
88
23.8k
        }
89
77.5k
        if (s->bi_valid > 8) {
90
24.3k
            put_short(s, (uint16_t)s->bi_buf);
91
24.3k
            s->bi_buf >>= 16;
92
24.3k
            s->bi_valid -= 16;
93
24.3k
        }
94
77.5k
        if (s->bi_valid > 0) {
95
55.1k
            put_byte(s, s->bi_buf);
96
55.1k
        }
97
77.5k
    }
98
82.4k
    s->bi_used = ((s->bi_valid - 1) & 7) + 1;
99
82.4k
    s->bi_buf = 0;
100
82.4k
    s->bi_valid = 0;
101
82.4k
}
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
203M
                                uint64_t *bi_buf, uint32_t *bi_valid) {
108
203M
    send_code(s, c, ltree, *bi_buf, *bi_valid);
109
203M
    Tracecv(isgraph(c & 0xff), (stderr, " '%c' ", c));
110
203M
}
deflate_quick.c:zng_emit_lit
Line
Count
Source
107
203M
                                uint64_t *bi_buf, uint32_t *bi_valid) {
108
203M
    send_code(s, c, ltree, *bi_buf, *bi_valid);
109
203M
    Tracecv(isgraph(c & 0xff), (stderr, " '%c' ", c));
110
203M
}
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
37.9M
                                     uint32_t lc, uint32_t dist, uint64_t *bi_buf, uint32_t *bi_valid) {
117
37.9M
    uint64_t match_bits;
118
37.9M
    uint32_t match_bits_len;
119
37.9M
    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
37.9M
    uint32_t c, extra;
122
37.9M
    uint8_t code;
123
124
    /* 1. Process Length Code */
125
37.9M
    code = zng_length_code[lc];
126
37.9M
    c = code + LITERALS + 1;
127
37.9M
    Assert(c < L_CODES, "bad l_code");
128
37.9M
    trace_code(s, c);
129
130
    /*    Send length code, len is the match length - STD_MIN_MATCH */
131
37.9M
    match_bits = ltree[c].Code;
132
37.9M
    match_bits_len = ltree[c].Len;
133
134
    /* 2. Get extra bits count and mask */
135
37.9M
    mask_ext = lmask_extra[code];
136
37.9M
    extra = mask_ext >> 8;
137
138
    /*    Send length extra bits */
139
37.9M
    match_bits |= (uint64_t)(lc & mask_ext) << match_bits_len;
140
37.9M
    match_bits_len += extra;
141
142
    /* 3. Process Distance Code */
143
37.9M
    dist--; /* dist is now the match distance - 1 */
144
37.9M
    code = d_code(dist);
145
37.9M
    Assert(code < D_CODES, "bad d_code");
146
37.9M
    trace_code(s, code);
147
148
    /*    Send distance code */
149
37.9M
    match_bits |= ((uint64_t)dtree[code].Code << match_bits_len);
150
37.9M
    match_bits_len += dtree[code].Len;
151
152
    /* 4. Get extra bits count and mask */
153
37.9M
    mask_ext = dmask_extra[code];
154
37.9M
    extra = mask_ext >> 16;
155
156
    /*    Send dist extra bits */
157
37.9M
    match_bits |= ((uint64_t)(dist & mask_ext) << match_bits_len);
158
37.9M
    match_bits_len += extra;
159
160
37.9M
    send_bits(s, match_bits, match_bits_len, *bi_buf, *bi_valid);
161
162
37.9M
    return match_bits_len;
163
37.9M
}
deflate_quick.c:zng_emit_dist
Line
Count
Source
116
5.02M
                                     uint32_t lc, uint32_t dist, uint64_t *bi_buf, uint32_t *bi_valid) {
117
5.02M
    uint64_t match_bits;
118
5.02M
    uint32_t match_bits_len;
119
5.02M
    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
5.02M
    uint32_t c, extra;
122
5.02M
    uint8_t code;
123
124
    /* 1. Process Length Code */
125
5.02M
    code = zng_length_code[lc];
126
5.02M
    c = code + LITERALS + 1;
127
5.02M
    Assert(c < L_CODES, "bad l_code");
128
5.02M
    trace_code(s, c);
129
130
    /*    Send length code, len is the match length - STD_MIN_MATCH */
131
5.02M
    match_bits = ltree[c].Code;
132
5.02M
    match_bits_len = ltree[c].Len;
133
134
    /* 2. Get extra bits count and mask */
135
5.02M
    mask_ext = lmask_extra[code];
136
5.02M
    extra = mask_ext >> 8;
137
138
    /*    Send length extra bits */
139
5.02M
    match_bits |= (uint64_t)(lc & mask_ext) << match_bits_len;
140
5.02M
    match_bits_len += extra;
141
142
    /* 3. Process Distance Code */
143
5.02M
    dist--; /* dist is now the match distance - 1 */
144
5.02M
    code = d_code(dist);
145
5.02M
    Assert(code < D_CODES, "bad d_code");
146
5.02M
    trace_code(s, code);
147
148
    /*    Send distance code */
149
5.02M
    match_bits |= ((uint64_t)dtree[code].Code << match_bits_len);
150
5.02M
    match_bits_len += dtree[code].Len;
151
152
    /* 4. Get extra bits count and mask */
153
5.02M
    mask_ext = dmask_extra[code];
154
5.02M
    extra = mask_ext >> 16;
155
156
    /*    Send dist extra bits */
157
5.02M
    match_bits |= ((uint64_t)(dist & mask_ext) << match_bits_len);
158
5.02M
    match_bits_len += extra;
159
160
5.02M
    send_bits(s, match_bits, match_bits_len, *bi_buf, *bi_valid);
161
162
5.02M
    return match_bits_len;
163
5.02M
}
trees.c:zng_emit_dist
Line
Count
Source
116
32.9M
                                     uint32_t lc, uint32_t dist, uint64_t *bi_buf, uint32_t *bi_valid) {
117
32.9M
    uint64_t match_bits;
118
32.9M
    uint32_t match_bits_len;
119
32.9M
    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
32.9M
    uint32_t c, extra;
122
32.9M
    uint8_t code;
123
124
    /* 1. Process Length Code */
125
32.9M
    code = zng_length_code[lc];
126
32.9M
    c = code + LITERALS + 1;
127
32.9M
    Assert(c < L_CODES, "bad l_code");
128
32.9M
    trace_code(s, c);
129
130
    /*    Send length code, len is the match length - STD_MIN_MATCH */
131
32.9M
    match_bits = ltree[c].Code;
132
32.9M
    match_bits_len = ltree[c].Len;
133
134
    /* 2. Get extra bits count and mask */
135
32.9M
    mask_ext = lmask_extra[code];
136
32.9M
    extra = mask_ext >> 8;
137
138
    /*    Send length extra bits */
139
32.9M
    match_bits |= (uint64_t)(lc & mask_ext) << match_bits_len;
140
32.9M
    match_bits_len += extra;
141
142
    /* 3. Process Distance Code */
143
32.9M
    dist--; /* dist is now the match distance - 1 */
144
32.9M
    code = d_code(dist);
145
32.9M
    Assert(code < D_CODES, "bad d_code");
146
32.9M
    trace_code(s, code);
147
148
    /*    Send distance code */
149
32.9M
    match_bits |= ((uint64_t)dtree[code].Code << match_bits_len);
150
32.9M
    match_bits_len += dtree[code].Len;
151
152
    /* 4. Get extra bits count and mask */
153
32.9M
    mask_ext = dmask_extra[code];
154
32.9M
    extra = mask_ext >> 16;
155
156
    /*    Send dist extra bits */
157
32.9M
    match_bits |= ((uint64_t)(dist & mask_ext) << match_bits_len);
158
32.9M
    match_bits_len += extra;
159
160
32.9M
    send_bits(s, match_bits, match_bits_len, *bi_buf, *bi_valid);
161
162
32.9M
    return match_bits_len;
163
32.9M
}
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
131k
                                      uint64_t *bi_buf, uint32_t *bi_valid) {
170
131k
    send_code(s, END_BLOCK, ltree, *bi_buf, *bi_valid);
171
131k
    Tracev((stderr, "\n+++ Emit End Block: Last: %u Pending: %u Total Out: %" PRIu64 "\n",
172
131k
        last, s->pending, (uint64_t)s->strm->total_out));
173
131k
    Z_UNUSED(last);
174
131k
}
deflate_quick.c:zng_emit_end_block
Line
Count
Source
169
7.42k
                                      uint64_t *bi_buf, uint32_t *bi_valid) {
170
7.42k
    send_code(s, END_BLOCK, ltree, *bi_buf, *bi_valid);
171
7.42k
    Tracev((stderr, "\n+++ Emit End Block: Last: %u Pending: %u Total Out: %" PRIu64 "\n",
172
7.42k
        last, s->pending, (uint64_t)s->strm->total_out));
173
7.42k
    Z_UNUSED(last);
174
7.42k
}
trees.c:zng_emit_end_block
Line
Count
Source
169
124k
                                      uint64_t *bi_buf, uint32_t *bi_valid) {
170
124k
    send_code(s, END_BLOCK, ltree, *bi_buf, *bi_valid);
171
124k
    Tracev((stderr, "\n+++ Emit End Block: Last: %u Pending: %u Total Out: %" PRIu64 "\n",
172
124k
        last, s->pending, (uint64_t)s->strm->total_out));
173
124k
    Z_UNUSED(last);
174
124k
}
175
176
/* ===========================================================================
177
 * Emit literal and count bits
178
 */
179
203M
static inline void zng_tr_emit_lit(deflate_state *s, const ct_data *ltree, unsigned c) {
180
203M
    uint64_t bi_buf = s->bi_buf;
181
203M
    uint32_t bi_valid = s->bi_valid;
182
203M
    zng_emit_lit(s, ltree, c, &bi_buf, &bi_valid);
183
203M
    s->bi_buf = bi_buf;
184
203M
    s->bi_valid = bi_valid;
185
203M
    cmpr_bits_add(s, ltree[c].Len);
186
203M
}
deflate_quick.c:zng_tr_emit_lit
Line
Count
Source
179
203M
static inline void zng_tr_emit_lit(deflate_state *s, const ct_data *ltree, unsigned c) {
180
203M
    uint64_t bi_buf = s->bi_buf;
181
203M
    uint32_t bi_valid = s->bi_valid;
182
203M
    zng_emit_lit(s, ltree, c, &bi_buf, &bi_valid);
183
203M
    s->bi_buf = bi_buf;
184
203M
    s->bi_valid = bi_valid;
185
203M
    cmpr_bits_add(s, ltree[c].Len);
186
203M
}
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
5.02M
    uint32_t lc, uint32_t dist) {
193
5.02M
    uint64_t bi_buf = s->bi_buf;
194
5.02M
    uint32_t bi_valid = s->bi_valid;
195
5.02M
    uint32_t bits = zng_emit_dist(s, ltree, dtree, lc, dist, &bi_buf, &bi_valid);
196
5.02M
    s->bi_buf = bi_buf;
197
5.02M
    s->bi_valid = bi_valid;
198
5.02M
    cmpr_bits_add(s, bits);
199
5.02M
}
deflate_quick.c:zng_tr_emit_dist
Line
Count
Source
192
5.02M
    uint32_t lc, uint32_t dist) {
193
5.02M
    uint64_t bi_buf = s->bi_buf;
194
5.02M
    uint32_t bi_valid = s->bi_valid;
195
5.02M
    uint32_t bits = zng_emit_dist(s, ltree, dtree, lc, dist, &bi_buf, &bi_valid);
196
5.02M
    s->bi_buf = bi_buf;
197
5.02M
    s->bi_valid = bi_valid;
198
5.02M
    cmpr_bits_add(s, bits);
199
5.02M
}
Unexecuted instantiation: trees.c:zng_tr_emit_dist
200
201
/* ===========================================================================
202
 * Emit start of block
203
 */
204
165k
static inline void zng_tr_emit_tree(deflate_state *s, int type, const int last) {
205
165k
    uint32_t bi_valid = s->bi_valid;
206
165k
    uint64_t bi_buf = s->bi_buf;
207
165k
    uint32_t header_bits = (type << 1) + last;
208
165k
    send_bits(s, header_bits, 3, bi_buf, bi_valid);
209
165k
    cmpr_bits_add(s, 3);
210
165k
    s->bi_valid = bi_valid;
211
165k
    s->bi_buf = bi_buf;
212
165k
    Tracev((stderr, "\n--- Emit Tree: Last: %u\n", last));
213
165k
}
deflate_quick.c:zng_tr_emit_tree
Line
Count
Source
204
7.42k
static inline void zng_tr_emit_tree(deflate_state *s, int type, const int last) {
205
7.42k
    uint32_t bi_valid = s->bi_valid;
206
7.42k
    uint64_t bi_buf = s->bi_buf;
207
7.42k
    uint32_t header_bits = (type << 1) + last;
208
7.42k
    send_bits(s, header_bits, 3, bi_buf, bi_valid);
209
7.42k
    cmpr_bits_add(s, 3);
210
7.42k
    s->bi_valid = bi_valid;
211
7.42k
    s->bi_buf = bi_buf;
212
7.42k
    Tracev((stderr, "\n--- Emit Tree: Last: %u\n", last));
213
7.42k
}
trees.c:zng_tr_emit_tree
Line
Count
Source
204
158k
static inline void zng_tr_emit_tree(deflate_state *s, int type, const int last) {
205
158k
    uint32_t bi_valid = s->bi_valid;
206
158k
    uint64_t bi_buf = s->bi_buf;
207
158k
    uint32_t header_bits = (type << 1) + last;
208
158k
    send_bits(s, header_bits, 3, bi_buf, bi_valid);
209
158k
    cmpr_bits_add(s, 3);
210
158k
    s->bi_valid = bi_valid;
211
158k
    s->bi_buf = bi_buf;
212
158k
    Tracev((stderr, "\n--- Emit Tree: Last: %u\n", last));
213
158k
}
214
215
/* ===========================================================================
216
 * Align bit buffer on a byte boundary and count bits
217
 */
218
89.6k
static inline void zng_tr_emit_align(deflate_state *s) {
219
89.6k
    bi_windup(s); /* align on byte boundary */
220
89.6k
    sent_bits_align(s);
221
89.6k
}
deflate_quick.c:zng_tr_emit_align
Line
Count
Source
218
7.20k
static inline void zng_tr_emit_align(deflate_state *s) {
219
7.20k
    bi_windup(s); /* align on byte boundary */
220
7.20k
    sent_bits_align(s);
221
7.20k
}
trees.c:zng_tr_emit_align
Line
Count
Source
218
82.4k
static inline void zng_tr_emit_align(deflate_state *s) {
219
82.4k
    bi_windup(s); /* align on byte boundary */
220
82.4k
    sent_bits_align(s);
221
82.4k
}
222
223
/* ===========================================================================
224
 * Emit an end block and align bit buffer if last block
225
 */
226
7.42k
static inline void zng_tr_emit_end_block(deflate_state *s, const ct_data *ltree, const int last) {
227
7.42k
    uint64_t bi_buf = s->bi_buf;
228
7.42k
    uint32_t bi_valid = s->bi_valid;
229
7.42k
    zng_emit_end_block(s, ltree, last, &bi_buf, &bi_valid);
230
7.42k
    s->bi_buf = bi_buf;
231
7.42k
    s->bi_valid = bi_valid;
232
7.42k
    cmpr_bits_add(s, 7);
233
7.42k
    if (last)
234
7.20k
        zng_tr_emit_align(s);
235
7.42k
}
deflate_quick.c:zng_tr_emit_end_block
Line
Count
Source
226
7.42k
static inline void zng_tr_emit_end_block(deflate_state *s, const ct_data *ltree, const int last) {
227
7.42k
    uint64_t bi_buf = s->bi_buf;
228
7.42k
    uint32_t bi_valid = s->bi_valid;
229
7.42k
    zng_emit_end_block(s, ltree, last, &bi_buf, &bi_valid);
230
7.42k
    s->bi_buf = bi_buf;
231
7.42k
    s->bi_valid = bi_valid;
232
7.42k
    cmpr_bits_add(s, 7);
233
7.42k
    if (last)
234
7.20k
        zng_tr_emit_align(s);
235
7.42k
}
Unexecuted instantiation: trees.c:zng_tr_emit_end_block
236
237
#endif