Coverage Report

Created: 2026-02-14 07:07

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 const unsigned char Z_INTERNAL zng_dist_code[DIST_CODE_LEN];
18
extern const unsigned char Z_INTERNAL zng_length_code[STD_MAX_MATCH-STD_MIN_MATCH+1];
19
20
extern Z_INTERNAL const int base_length[LENGTH_CODES];
21
extern Z_INTERNAL const int base_dist[D_CODES];
22
23
/* Bit buffer and deflate code stderr tracing */
24
#ifdef ZLIB_DEBUG
25
#  define send_bits_trace(s, value, length) { \
26
        Tracevv((stderr, " l %2d v %4llx ", (int)(length), (long long)(value))); \
27
        Assert(length > 0 && length <= BIT_BUF_SIZE, "invalid length"); \
28
    }
29
#  define send_code_trace(s, c) \
30
    if (z_verbose > 2) { \
31
        fprintf(stderr, "\ncd %3d ", (c)); \
32
    }
33
#else
34
#  define send_bits_trace(s, value, length)
35
#  define send_code_trace(s, c)
36
#endif
37
38
/* If not enough room in bi_buf, use (valid) bits from bi_buf and
39
 * (64 - bi_valid) bits from value, leaving (width - (64-bi_valid))
40
 * unused bits in value.
41
 *
42
 * NOTE: Static analyzers can't evaluate value of total_bits, so we
43
 *       also need to make sure bi_valid is within acceptable range,
44
 *       otherwise the shifts will overflow.
45
 */
46
275M
#define send_bits(s, t_val, t_len, bi_buf, bi_valid) {\
47
275M
    uint64_t val = (uint64_t)t_val;\
48
275M
    uint32_t len = (uint32_t)t_len;\
49
275M
    uint32_t total_bits = bi_valid + len;\
50
275M
    send_bits_trace(s, val, len);\
51
275M
    sent_bits_add(s, len);\
52
275M
    if (total_bits < BIT_BUF_SIZE && bi_valid < BIT_BUF_SIZE) {\
53
245M
        bi_buf |= val << bi_valid;\
54
245M
        bi_valid = total_bits;\
55
245M
    } else if (bi_valid >= BIT_BUF_SIZE) {\
56
0
        put_uint64(s, bi_buf);\
57
0
        bi_buf = val;\
58
0
        bi_valid = len;\
59
29.6M
    } else {\
60
29.6M
        bi_buf |= val << bi_valid;\
61
29.6M
        put_uint64(s, bi_buf);\
62
29.6M
        bi_buf = val >> (BIT_BUF_SIZE - bi_valid);\
63
29.6M
        bi_valid = total_bits - BIT_BUF_SIZE;\
64
29.6M
    }\
65
275M
}
66
67
/* Send a code of the given tree. c and tree must not have side effects */
68
#ifdef ZLIB_DEBUG
69
#  define send_code(s, c, tree, bi_buf, bi_valid) { \
70
    send_code_trace(s, c); \
71
    send_bits(s, tree[c].Code, tree[c].Len, bi_buf, bi_valid); \
72
}
73
#else
74
#  define send_code(s, c, tree, bi_buf, bi_valid) \
75
263M
    send_bits(s, tree[c].Code, tree[c].Len, bi_buf, bi_valid)
76
#endif
77
78
/* ===========================================================================
79
 * Flush the bit buffer and align the output on a byte boundary
80
 */
81
16.7k
static inline void bi_windup(deflate_state *s) {
82
16.7k
    if (s->bi_valid > 56) {
83
1.17k
        put_uint64(s, s->bi_buf);
84
15.5k
    } else {
85
15.5k
        if (s->bi_valid > 24) {
86
5.41k
            put_uint32(s, (uint32_t)s->bi_buf);
87
5.41k
            s->bi_buf >>= 32;
88
5.41k
            s->bi_valid -= 32;
89
5.41k
        }
90
15.5k
        if (s->bi_valid > 8) {
91
5.74k
            put_short(s, (uint16_t)s->bi_buf);
92
5.74k
            s->bi_buf >>= 16;
93
5.74k
            s->bi_valid -= 16;
94
5.74k
        }
95
15.5k
        if (s->bi_valid > 0) {
96
10.4k
            put_byte(s, s->bi_buf);
97
10.4k
        }
98
15.5k
    }
99
16.7k
    s->bi_buf = 0;
100
16.7k
    s->bi_valid = 0;
101
16.7k
}
deflate_quick.c:bi_windup
Line
Count
Source
81
1.24k
static inline void bi_windup(deflate_state *s) {
82
1.24k
    if (s->bi_valid > 56) {
83
145
        put_uint64(s, s->bi_buf);
84
1.10k
    } else {
85
1.10k
        if (s->bi_valid > 24) {
86
643
            put_uint32(s, (uint32_t)s->bi_buf);
87
643
            s->bi_buf >>= 32;
88
643
            s->bi_valid -= 32;
89
643
        }
90
1.10k
        if (s->bi_valid > 8) {
91
591
            put_short(s, (uint16_t)s->bi_buf);
92
591
            s->bi_buf >>= 16;
93
591
            s->bi_valid -= 16;
94
591
        }
95
1.10k
        if (s->bi_valid > 0) {
96
637
            put_byte(s, s->bi_buf);
97
637
        }
98
1.10k
    }
99
1.24k
    s->bi_buf = 0;
100
1.24k
    s->bi_valid = 0;
101
1.24k
}
trees.c:bi_windup
Line
Count
Source
81
15.5k
static inline void bi_windup(deflate_state *s) {
82
15.5k
    if (s->bi_valid > 56) {
83
1.03k
        put_uint64(s, s->bi_buf);
84
14.4k
    } else {
85
14.4k
        if (s->bi_valid > 24) {
86
4.77k
            put_uint32(s, (uint32_t)s->bi_buf);
87
4.77k
            s->bi_buf >>= 32;
88
4.77k
            s->bi_valid -= 32;
89
4.77k
        }
90
14.4k
        if (s->bi_valid > 8) {
91
5.15k
            put_short(s, (uint16_t)s->bi_buf);
92
5.15k
            s->bi_buf >>= 16;
93
5.15k
            s->bi_valid -= 16;
94
5.15k
        }
95
14.4k
        if (s->bi_valid > 0) {
96
9.80k
            put_byte(s, s->bi_buf);
97
9.80k
        }
98
14.4k
    }
99
15.5k
    s->bi_buf = 0;
100
15.5k
    s->bi_valid = 0;
101
15.5k
}
102
103
/* ===========================================================================
104
 * Emit literal code
105
 */
106
261M
static inline uint32_t zng_emit_lit(deflate_state *s, const ct_data *ltree, unsigned c) {
107
261M
    uint32_t bi_valid = s->bi_valid;
108
261M
    uint64_t bi_buf = s->bi_buf;
109
110
261M
    send_code(s, c, ltree, bi_buf, bi_valid);
111
112
261M
    s->bi_valid = bi_valid;
113
261M
    s->bi_buf = bi_buf;
114
115
261M
    Tracecv(isgraph(c & 0xff), (stderr, " '%c' ", c));
116
117
261M
    return ltree[c].Len;
118
261M
}
deflate_quick.c:zng_emit_lit
Line
Count
Source
106
79.7M
static inline uint32_t zng_emit_lit(deflate_state *s, const ct_data *ltree, unsigned c) {
107
79.7M
    uint32_t bi_valid = s->bi_valid;
108
79.7M
    uint64_t bi_buf = s->bi_buf;
109
110
79.7M
    send_code(s, c, ltree, bi_buf, bi_valid);
111
112
79.7M
    s->bi_valid = bi_valid;
113
79.7M
    s->bi_buf = bi_buf;
114
115
79.7M
    Tracecv(isgraph(c & 0xff), (stderr, " '%c' ", c));
116
117
79.7M
    return ltree[c].Len;
118
79.7M
}
trees.c:zng_emit_lit
Line
Count
Source
106
181M
static inline uint32_t zng_emit_lit(deflate_state *s, const ct_data *ltree, unsigned c) {
107
181M
    uint32_t bi_valid = s->bi_valid;
108
181M
    uint64_t bi_buf = s->bi_buf;
109
110
181M
    send_code(s, c, ltree, bi_buf, bi_valid);
111
112
181M
    s->bi_valid = bi_valid;
113
181M
    s->bi_buf = bi_buf;
114
115
181M
    Tracecv(isgraph(c & 0xff), (stderr, " '%c' ", c));
116
117
181M
    return ltree[c].Len;
118
181M
}
119
120
/* ===========================================================================
121
 * Emit match distance/length code
122
 */
123
static uint32_t zng_emit_dist(deflate_state *s, const ct_data *ltree, const ct_data *dtree,
124
11.4M
    uint32_t lc, uint32_t dist) {
125
11.4M
    uint32_t c, extra;
126
11.4M
    uint8_t code;
127
11.4M
    uint64_t match_bits;
128
11.4M
    uint32_t match_bits_len;
129
11.4M
    uint32_t bi_valid = s->bi_valid;
130
11.4M
    uint64_t bi_buf = s->bi_buf;
131
132
    /* Send the length code, len is the match length - STD_MIN_MATCH */
133
11.4M
    code = zng_length_code[lc];
134
11.4M
    c = code+LITERALS+1;
135
11.4M
    Assert(c < L_CODES, "bad l_code");
136
11.4M
    send_code_trace(s, c);
137
138
11.4M
    match_bits = ltree[c].Code;
139
11.4M
    match_bits_len = ltree[c].Len;
140
11.4M
    extra = extra_lbits[code];
141
11.4M
    if (extra != 0) {
142
905k
        lc -= base_length[code];
143
905k
        match_bits |= ((uint64_t)lc << match_bits_len);
144
905k
        match_bits_len += extra;
145
905k
    }
146
147
11.4M
    dist--; /* dist is now the match distance - 1 */
148
11.4M
    code = d_code(dist);
149
11.4M
    Assert(code < D_CODES, "bad d_code");
150
11.4M
    send_code_trace(s, code);
151
152
    /* Send the distance code */
153
11.4M
    match_bits |= ((uint64_t)dtree[code].Code << match_bits_len);
154
11.4M
    match_bits_len += dtree[code].Len;
155
11.4M
    extra = extra_dbits[code];
156
11.4M
    if (extra != 0) {
157
8.97M
        dist -= base_dist[code];
158
8.97M
        match_bits |= ((uint64_t)dist << match_bits_len);
159
8.97M
        match_bits_len += extra;
160
8.97M
    }
161
162
11.4M
    send_bits(s, match_bits, match_bits_len, bi_buf, bi_valid);
163
164
11.4M
    s->bi_valid = bi_valid;
165
11.4M
    s->bi_buf = bi_buf;
166
167
11.4M
    return match_bits_len;
168
11.4M
}
deflate_quick.c:zng_emit_dist
Line
Count
Source
124
672k
    uint32_t lc, uint32_t dist) {
125
672k
    uint32_t c, extra;
126
672k
    uint8_t code;
127
672k
    uint64_t match_bits;
128
672k
    uint32_t match_bits_len;
129
672k
    uint32_t bi_valid = s->bi_valid;
130
672k
    uint64_t bi_buf = s->bi_buf;
131
132
    /* Send the length code, len is the match length - STD_MIN_MATCH */
133
672k
    code = zng_length_code[lc];
134
672k
    c = code+LITERALS+1;
135
672k
    Assert(c < L_CODES, "bad l_code");
136
672k
    send_code_trace(s, c);
137
138
672k
    match_bits = ltree[c].Code;
139
672k
    match_bits_len = ltree[c].Len;
140
672k
    extra = extra_lbits[code];
141
672k
    if (extra != 0) {
142
63.5k
        lc -= base_length[code];
143
63.5k
        match_bits |= ((uint64_t)lc << match_bits_len);
144
63.5k
        match_bits_len += extra;
145
63.5k
    }
146
147
672k
    dist--; /* dist is now the match distance - 1 */
148
672k
    code = d_code(dist);
149
672k
    Assert(code < D_CODES, "bad d_code");
150
672k
    send_code_trace(s, code);
151
152
    /* Send the distance code */
153
672k
    match_bits |= ((uint64_t)dtree[code].Code << match_bits_len);
154
672k
    match_bits_len += dtree[code].Len;
155
672k
    extra = extra_dbits[code];
156
672k
    if (extra != 0) {
157
660k
        dist -= base_dist[code];
158
660k
        match_bits |= ((uint64_t)dist << match_bits_len);
159
660k
        match_bits_len += extra;
160
660k
    }
161
162
672k
    send_bits(s, match_bits, match_bits_len, bi_buf, bi_valid);
163
164
672k
    s->bi_valid = bi_valid;
165
672k
    s->bi_buf = bi_buf;
166
167
672k
    return match_bits_len;
168
672k
}
trees.c:zng_emit_dist
Line
Count
Source
124
10.7M
    uint32_t lc, uint32_t dist) {
125
10.7M
    uint32_t c, extra;
126
10.7M
    uint8_t code;
127
10.7M
    uint64_t match_bits;
128
10.7M
    uint32_t match_bits_len;
129
10.7M
    uint32_t bi_valid = s->bi_valid;
130
10.7M
    uint64_t bi_buf = s->bi_buf;
131
132
    /* Send the length code, len is the match length - STD_MIN_MATCH */
133
10.7M
    code = zng_length_code[lc];
134
10.7M
    c = code+LITERALS+1;
135
10.7M
    Assert(c < L_CODES, "bad l_code");
136
10.7M
    send_code_trace(s, c);
137
138
10.7M
    match_bits = ltree[c].Code;
139
10.7M
    match_bits_len = ltree[c].Len;
140
10.7M
    extra = extra_lbits[code];
141
10.7M
    if (extra != 0) {
142
842k
        lc -= base_length[code];
143
842k
        match_bits |= ((uint64_t)lc << match_bits_len);
144
842k
        match_bits_len += extra;
145
842k
    }
146
147
10.7M
    dist--; /* dist is now the match distance - 1 */
148
10.7M
    code = d_code(dist);
149
10.7M
    Assert(code < D_CODES, "bad d_code");
150
10.7M
    send_code_trace(s, code);
151
152
    /* Send the distance code */
153
10.7M
    match_bits |= ((uint64_t)dtree[code].Code << match_bits_len);
154
10.7M
    match_bits_len += dtree[code].Len;
155
10.7M
    extra = extra_dbits[code];
156
10.7M
    if (extra != 0) {
157
8.31M
        dist -= base_dist[code];
158
8.31M
        match_bits |= ((uint64_t)dist << match_bits_len);
159
8.31M
        match_bits_len += extra;
160
8.31M
    }
161
162
10.7M
    send_bits(s, match_bits, match_bits_len, bi_buf, bi_valid);
163
164
10.7M
    s->bi_valid = bi_valid;
165
10.7M
    s->bi_buf = bi_buf;
166
167
10.7M
    return match_bits_len;
168
10.7M
}
169
170
/* ===========================================================================
171
 * Emit end block
172
 */
173
22.1k
static inline void zng_emit_end_block(deflate_state *s, const ct_data *ltree, const int last) {
174
22.1k
    uint32_t bi_valid = s->bi_valid;
175
22.1k
    uint64_t bi_buf = s->bi_buf;
176
22.1k
    send_code(s, END_BLOCK, ltree, bi_buf, bi_valid);
177
22.1k
    s->bi_valid = bi_valid;
178
22.1k
    s->bi_buf = bi_buf;
179
22.1k
    Tracev((stderr, "\n+++ Emit End Block: Last: %u Pending: %u Total Out: %" PRIu64 "\n",
180
22.1k
        last, s->pending, (uint64_t)s->strm->total_out));
181
22.1k
    Z_UNUSED(last);
182
22.1k
}
deflate_quick.c:zng_emit_end_block
Line
Count
Source
173
1.45k
static inline void zng_emit_end_block(deflate_state *s, const ct_data *ltree, const int last) {
174
1.45k
    uint32_t bi_valid = s->bi_valid;
175
1.45k
    uint64_t bi_buf = s->bi_buf;
176
1.45k
    send_code(s, END_BLOCK, ltree, bi_buf, bi_valid);
177
1.45k
    s->bi_valid = bi_valid;
178
1.45k
    s->bi_buf = bi_buf;
179
1.45k
    Tracev((stderr, "\n+++ Emit End Block: Last: %u Pending: %u Total Out: %" PRIu64 "\n",
180
1.45k
        last, s->pending, (uint64_t)s->strm->total_out));
181
1.45k
    Z_UNUSED(last);
182
1.45k
}
trees.c:zng_emit_end_block
Line
Count
Source
173
20.7k
static inline void zng_emit_end_block(deflate_state *s, const ct_data *ltree, const int last) {
174
20.7k
    uint32_t bi_valid = s->bi_valid;
175
20.7k
    uint64_t bi_buf = s->bi_buf;
176
20.7k
    send_code(s, END_BLOCK, ltree, bi_buf, bi_valid);
177
20.7k
    s->bi_valid = bi_valid;
178
20.7k
    s->bi_buf = bi_buf;
179
20.7k
    Tracev((stderr, "\n+++ Emit End Block: Last: %u Pending: %u Total Out: %" PRIu64 "\n",
180
20.7k
        last, s->pending, (uint64_t)s->strm->total_out));
181
20.7k
    Z_UNUSED(last);
182
20.7k
}
183
184
/* ===========================================================================
185
 * Emit literal and count bits
186
 */
187
79.7M
static inline void zng_tr_emit_lit(deflate_state *s, const ct_data *ltree, unsigned c) {
188
79.7M
    cmpr_bits_add(s, zng_emit_lit(s, ltree, c));
189
79.7M
}
deflate_quick.c:zng_tr_emit_lit
Line
Count
Source
187
79.7M
static inline void zng_tr_emit_lit(deflate_state *s, const ct_data *ltree, unsigned c) {
188
79.7M
    cmpr_bits_add(s, zng_emit_lit(s, ltree, c));
189
79.7M
}
Unexecuted instantiation: trees.c:zng_tr_emit_lit
190
191
/* ===========================================================================
192
 * Emit match and count bits
193
 */
194
static inline void zng_tr_emit_dist(deflate_state *s, const ct_data *ltree, const ct_data *dtree,
195
672k
    uint32_t lc, uint32_t dist) {
196
672k
    cmpr_bits_add(s, zng_emit_dist(s, ltree, dtree, lc, dist));
197
672k
}
deflate_quick.c:zng_tr_emit_dist
Line
Count
Source
195
672k
    uint32_t lc, uint32_t dist) {
196
672k
    cmpr_bits_add(s, zng_emit_dist(s, ltree, dtree, lc, dist));
197
672k
}
Unexecuted instantiation: trees.c:zng_tr_emit_dist
198
199
/* ===========================================================================
200
 * Emit start of block
201
 */
202
27.7k
static inline void zng_tr_emit_tree(deflate_state *s, int type, const int last) {
203
27.7k
    uint32_t bi_valid = s->bi_valid;
204
27.7k
    uint64_t bi_buf = s->bi_buf;
205
27.7k
    uint32_t header_bits = (type << 1) + last;
206
27.7k
    send_bits(s, header_bits, 3, bi_buf, bi_valid);
207
27.7k
    cmpr_bits_add(s, 3);
208
27.7k
    s->bi_valid = bi_valid;
209
27.7k
    s->bi_buf = bi_buf;
210
27.7k
    Tracev((stderr, "\n--- Emit Tree: Last: %u\n", last));
211
27.7k
}
deflate_quick.c:zng_tr_emit_tree
Line
Count
Source
202
1.45k
static inline void zng_tr_emit_tree(deflate_state *s, int type, const int last) {
203
1.45k
    uint32_t bi_valid = s->bi_valid;
204
1.45k
    uint64_t bi_buf = s->bi_buf;
205
1.45k
    uint32_t header_bits = (type << 1) + last;
206
1.45k
    send_bits(s, header_bits, 3, bi_buf, bi_valid);
207
1.45k
    cmpr_bits_add(s, 3);
208
1.45k
    s->bi_valid = bi_valid;
209
1.45k
    s->bi_buf = bi_buf;
210
1.45k
    Tracev((stderr, "\n--- Emit Tree: Last: %u\n", last));
211
1.45k
}
trees.c:zng_tr_emit_tree
Line
Count
Source
202
26.2k
static inline void zng_tr_emit_tree(deflate_state *s, int type, const int last) {
203
26.2k
    uint32_t bi_valid = s->bi_valid;
204
26.2k
    uint64_t bi_buf = s->bi_buf;
205
26.2k
    uint32_t header_bits = (type << 1) + last;
206
26.2k
    send_bits(s, header_bits, 3, bi_buf, bi_valid);
207
26.2k
    cmpr_bits_add(s, 3);
208
26.2k
    s->bi_valid = bi_valid;
209
26.2k
    s->bi_buf = bi_buf;
210
26.2k
    Tracev((stderr, "\n--- Emit Tree: Last: %u\n", last));
211
26.2k
}
212
213
/* ===========================================================================
214
 * Align bit buffer on a byte boundary and count bits
215
 */
216
16.7k
static inline void zng_tr_emit_align(deflate_state *s) {
217
16.7k
    bi_windup(s); /* align on byte boundary */
218
16.7k
    sent_bits_align(s);
219
16.7k
}
deflate_quick.c:zng_tr_emit_align
Line
Count
Source
216
1.24k
static inline void zng_tr_emit_align(deflate_state *s) {
217
1.24k
    bi_windup(s); /* align on byte boundary */
218
1.24k
    sent_bits_align(s);
219
1.24k
}
trees.c:zng_tr_emit_align
Line
Count
Source
216
15.5k
static inline void zng_tr_emit_align(deflate_state *s) {
217
15.5k
    bi_windup(s); /* align on byte boundary */
218
15.5k
    sent_bits_align(s);
219
15.5k
}
220
221
/* ===========================================================================
222
 * Emit an end block and align bit buffer if last block
223
 */
224
1.45k
static inline void zng_tr_emit_end_block(deflate_state *s, const ct_data *ltree, const int last) {
225
1.45k
    zng_emit_end_block(s, ltree, last);
226
1.45k
    cmpr_bits_add(s, 7);
227
1.45k
    if (last)
228
1.24k
        zng_tr_emit_align(s);
229
1.45k
}
deflate_quick.c:zng_tr_emit_end_block
Line
Count
Source
224
1.45k
static inline void zng_tr_emit_end_block(deflate_state *s, const ct_data *ltree, const int last) {
225
1.45k
    zng_emit_end_block(s, ltree, last);
226
1.45k
    cmpr_bits_add(s, 7);
227
1.45k
    if (last)
228
1.24k
        zng_tr_emit_align(s);
229
1.45k
}
Unexecuted instantiation: trees.c:zng_tr_emit_end_block
230
231
#endif