Coverage Report

Created: 2025-10-23 06:32

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
206M
#define send_bits(s, t_val, t_len, bi_buf, bi_valid) {\
47
206M
    uint64_t val = (uint64_t)t_val;\
48
206M
    uint32_t len = (uint32_t)t_len;\
49
206M
    uint32_t total_bits = bi_valid + len;\
50
206M
    send_bits_trace(s, val, len);\
51
206M
    sent_bits_add(s, len);\
52
206M
    if (total_bits < BIT_BUF_SIZE && bi_valid < BIT_BUF_SIZE) {\
53
185M
        bi_buf |= val << bi_valid;\
54
185M
        bi_valid = total_bits;\
55
185M
    } 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
20.9M
    } else {\
60
20.9M
        bi_buf |= val << bi_valid;\
61
20.9M
        put_uint64(s, bi_buf);\
62
20.9M
        bi_buf = val >> (BIT_BUF_SIZE - bi_valid);\
63
20.9M
        bi_valid = total_bits - BIT_BUF_SIZE;\
64
20.9M
    }\
65
206M
}
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
194M
    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
9.50k
static void bi_windup(deflate_state *s) {
82
9.50k
    if (s->bi_valid > 56) {
83
640
        put_uint64(s, s->bi_buf);
84
8.86k
    } else {
85
8.86k
        if (s->bi_valid > 24) {
86
2.86k
            put_uint32(s, (uint32_t)s->bi_buf);
87
2.86k
            s->bi_buf >>= 32;
88
2.86k
            s->bi_valid -= 32;
89
2.86k
        }
90
8.86k
        if (s->bi_valid > 8) {
91
3.03k
            put_short(s, (uint16_t)s->bi_buf);
92
3.03k
            s->bi_buf >>= 16;
93
3.03k
            s->bi_valid -= 16;
94
3.03k
        }
95
8.86k
        if (s->bi_valid > 0) {
96
6.17k
            put_byte(s, s->bi_buf);
97
6.17k
        }
98
8.86k
    }
99
9.50k
    s->bi_buf = 0;
100
9.50k
    s->bi_valid = 0;
101
9.50k
}
deflate_quick.c:bi_windup
Line
Count
Source
81
744
static void bi_windup(deflate_state *s) {
82
744
    if (s->bi_valid > 56) {
83
73
        put_uint64(s, s->bi_buf);
84
671
    } else {
85
671
        if (s->bi_valid > 24) {
86
402
            put_uint32(s, (uint32_t)s->bi_buf);
87
402
            s->bi_buf >>= 32;
88
402
            s->bi_valid -= 32;
89
402
        }
90
671
        if (s->bi_valid > 8) {
91
380
            put_short(s, (uint16_t)s->bi_buf);
92
380
            s->bi_buf >>= 16;
93
380
            s->bi_valid -= 16;
94
380
        }
95
671
        if (s->bi_valid > 0) {
96
385
            put_byte(s, s->bi_buf);
97
385
        }
98
671
    }
99
744
    s->bi_buf = 0;
100
744
    s->bi_valid = 0;
101
744
}
trees.c:bi_windup
Line
Count
Source
81
8.76k
static void bi_windup(deflate_state *s) {
82
8.76k
    if (s->bi_valid > 56) {
83
567
        put_uint64(s, s->bi_buf);
84
8.19k
    } else {
85
8.19k
        if (s->bi_valid > 24) {
86
2.46k
            put_uint32(s, (uint32_t)s->bi_buf);
87
2.46k
            s->bi_buf >>= 32;
88
2.46k
            s->bi_valid -= 32;
89
2.46k
        }
90
8.19k
        if (s->bi_valid > 8) {
91
2.65k
            put_short(s, (uint16_t)s->bi_buf);
92
2.65k
            s->bi_buf >>= 16;
93
2.65k
            s->bi_valid -= 16;
94
2.65k
        }
95
8.19k
        if (s->bi_valid > 0) {
96
5.78k
            put_byte(s, s->bi_buf);
97
5.78k
        }
98
8.19k
    }
99
8.76k
    s->bi_buf = 0;
100
8.76k
    s->bi_valid = 0;
101
8.76k
}
102
103
/* ===========================================================================
104
 * Emit literal code
105
 */
106
192M
static inline uint32_t zng_emit_lit(deflate_state *s, const ct_data *ltree, unsigned c) {
107
192M
    uint32_t bi_valid = s->bi_valid;
108
192M
    uint64_t bi_buf = s->bi_buf;
109
110
192M
    send_code(s, c, ltree, bi_buf, bi_valid);
111
112
192M
    s->bi_valid = bi_valid;
113
192M
    s->bi_buf = bi_buf;
114
115
192M
    Tracecv(isgraph(c & 0xff), (stderr, " '%c' ", c));
116
117
192M
    return ltree[c].Len;
118
192M
}
deflate_quick.c:zng_emit_lit
Line
Count
Source
106
32.1M
static inline uint32_t zng_emit_lit(deflate_state *s, const ct_data *ltree, unsigned c) {
107
32.1M
    uint32_t bi_valid = s->bi_valid;
108
32.1M
    uint64_t bi_buf = s->bi_buf;
109
110
32.1M
    send_code(s, c, ltree, bi_buf, bi_valid);
111
112
32.1M
    s->bi_valid = bi_valid;
113
32.1M
    s->bi_buf = bi_buf;
114
115
32.1M
    Tracecv(isgraph(c & 0xff), (stderr, " '%c' ", c));
116
117
32.1M
    return ltree[c].Len;
118
32.1M
}
trees.c:zng_emit_lit
Line
Count
Source
106
160M
static inline uint32_t zng_emit_lit(deflate_state *s, const ct_data *ltree, unsigned c) {
107
160M
    uint32_t bi_valid = s->bi_valid;
108
160M
    uint64_t bi_buf = s->bi_buf;
109
110
160M
    send_code(s, c, ltree, bi_buf, bi_valid);
111
112
160M
    s->bi_valid = bi_valid;
113
160M
    s->bi_buf = bi_buf;
114
115
160M
    Tracecv(isgraph(c & 0xff), (stderr, " '%c' ", c));
116
117
160M
    return ltree[c].Len;
118
160M
}
119
120
/* ===========================================================================
121
 * Emit match distance/length code
122
 */
123
static inline 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
1.87M
        lc -= base_length[code];
143
1.87M
        match_bits |= ((uint64_t)lc << match_bits_len);
144
1.87M
        match_bits_len += extra;
145
1.87M
    }
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.62M
        dist -= base_dist[code];
158
8.62M
        match_bits |= ((uint64_t)dist << match_bits_len);
159
8.62M
        match_bits_len += extra;
160
8.62M
    }
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
1.45M
    uint32_t lc, uint32_t dist) {
125
1.45M
    uint32_t c, extra;
126
1.45M
    uint8_t code;
127
1.45M
    uint64_t match_bits;
128
1.45M
    uint32_t match_bits_len;
129
1.45M
    uint32_t bi_valid = s->bi_valid;
130
1.45M
    uint64_t bi_buf = s->bi_buf;
131
132
    /* Send the length code, len is the match length - STD_MIN_MATCH */
133
1.45M
    code = zng_length_code[lc];
134
1.45M
    c = code+LITERALS+1;
135
1.45M
    Assert(c < L_CODES, "bad l_code");
136
1.45M
    send_code_trace(s, c);
137
138
1.45M
    match_bits = ltree[c].Code;
139
1.45M
    match_bits_len = ltree[c].Len;
140
1.45M
    extra = extra_lbits[code];
141
1.45M
    if (extra != 0) {
142
189k
        lc -= base_length[code];
143
189k
        match_bits |= ((uint64_t)lc << match_bits_len);
144
189k
        match_bits_len += extra;
145
189k
    }
146
147
1.45M
    dist--; /* dist is now the match distance - 1 */
148
1.45M
    code = d_code(dist);
149
1.45M
    Assert(code < D_CODES, "bad d_code");
150
1.45M
    send_code_trace(s, code);
151
152
    /* Send the distance code */
153
1.45M
    match_bits |= ((uint64_t)dtree[code].Code << match_bits_len);
154
1.45M
    match_bits_len += dtree[code].Len;
155
1.45M
    extra = extra_dbits[code];
156
1.45M
    if (extra != 0) {
157
1.44M
        dist -= base_dist[code];
158
1.44M
        match_bits |= ((uint64_t)dist << match_bits_len);
159
1.44M
        match_bits_len += extra;
160
1.44M
    }
161
162
1.45M
    send_bits(s, match_bits, match_bits_len, bi_buf, bi_valid);
163
164
1.45M
    s->bi_valid = bi_valid;
165
1.45M
    s->bi_buf = bi_buf;
166
167
1.45M
    return match_bits_len;
168
1.45M
}
trees.c:zng_emit_dist
Line
Count
Source
124
9.98M
    uint32_t lc, uint32_t dist) {
125
9.98M
    uint32_t c, extra;
126
9.98M
    uint8_t code;
127
9.98M
    uint64_t match_bits;
128
9.98M
    uint32_t match_bits_len;
129
9.98M
    uint32_t bi_valid = s->bi_valid;
130
9.98M
    uint64_t bi_buf = s->bi_buf;
131
132
    /* Send the length code, len is the match length - STD_MIN_MATCH */
133
9.98M
    code = zng_length_code[lc];
134
9.98M
    c = code+LITERALS+1;
135
9.98M
    Assert(c < L_CODES, "bad l_code");
136
9.98M
    send_code_trace(s, c);
137
138
9.98M
    match_bits = ltree[c].Code;
139
9.98M
    match_bits_len = ltree[c].Len;
140
9.98M
    extra = extra_lbits[code];
141
9.98M
    if (extra != 0) {
142
1.68M
        lc -= base_length[code];
143
1.68M
        match_bits |= ((uint64_t)lc << match_bits_len);
144
1.68M
        match_bits_len += extra;
145
1.68M
    }
146
147
9.98M
    dist--; /* dist is now the match distance - 1 */
148
9.98M
    code = d_code(dist);
149
9.98M
    Assert(code < D_CODES, "bad d_code");
150
9.98M
    send_code_trace(s, code);
151
152
    /* Send the distance code */
153
9.98M
    match_bits |= ((uint64_t)dtree[code].Code << match_bits_len);
154
9.98M
    match_bits_len += dtree[code].Len;
155
9.98M
    extra = extra_dbits[code];
156
9.98M
    if (extra != 0) {
157
7.17M
        dist -= base_dist[code];
158
7.17M
        match_bits |= ((uint64_t)dist << match_bits_len);
159
7.17M
        match_bits_len += extra;
160
7.17M
    }
161
162
9.98M
    send_bits(s, match_bits, match_bits_len, bi_buf, bi_valid);
163
164
9.98M
    s->bi_valid = bi_valid;
165
9.98M
    s->bi_buf = bi_buf;
166
167
9.98M
    return match_bits_len;
168
9.98M
}
169
170
/* ===========================================================================
171
 * Emit end block
172
 */
173
15.6k
static inline void zng_emit_end_block(deflate_state *s, const ct_data *ltree, const int last) {
174
15.6k
    uint32_t bi_valid = s->bi_valid;
175
15.6k
    uint64_t bi_buf = s->bi_buf;
176
15.6k
    send_code(s, END_BLOCK, ltree, bi_buf, bi_valid);
177
15.6k
    s->bi_valid = bi_valid;
178
15.6k
    s->bi_buf = bi_buf;
179
15.6k
    Tracev((stderr, "\n+++ Emit End Block: Last: %u Pending: %u Total Out: %" PRIu64 "\n",
180
15.6k
        last, s->pending, (uint64_t)s->strm->total_out));
181
15.6k
    Z_UNUSED(last);
182
15.6k
}
deflate_quick.c:zng_emit_end_block
Line
Count
Source
173
846
static inline void zng_emit_end_block(deflate_state *s, const ct_data *ltree, const int last) {
174
846
    uint32_t bi_valid = s->bi_valid;
175
846
    uint64_t bi_buf = s->bi_buf;
176
846
    send_code(s, END_BLOCK, ltree, bi_buf, bi_valid);
177
846
    s->bi_valid = bi_valid;
178
846
    s->bi_buf = bi_buf;
179
846
    Tracev((stderr, "\n+++ Emit End Block: Last: %u Pending: %u Total Out: %" PRIu64 "\n",
180
846
        last, s->pending, (uint64_t)s->strm->total_out));
181
846
    Z_UNUSED(last);
182
846
}
trees.c:zng_emit_end_block
Line
Count
Source
173
14.7k
static inline void zng_emit_end_block(deflate_state *s, const ct_data *ltree, const int last) {
174
14.7k
    uint32_t bi_valid = s->bi_valid;
175
14.7k
    uint64_t bi_buf = s->bi_buf;
176
14.7k
    send_code(s, END_BLOCK, ltree, bi_buf, bi_valid);
177
14.7k
    s->bi_valid = bi_valid;
178
14.7k
    s->bi_buf = bi_buf;
179
14.7k
    Tracev((stderr, "\n+++ Emit End Block: Last: %u Pending: %u Total Out: %" PRIu64 "\n",
180
14.7k
        last, s->pending, (uint64_t)s->strm->total_out));
181
14.7k
    Z_UNUSED(last);
182
14.7k
}
183
184
/* ===========================================================================
185
 * Emit literal and count bits
186
 */
187
32.1M
static inline void zng_tr_emit_lit(deflate_state *s, const ct_data *ltree, unsigned c) {
188
32.1M
    cmpr_bits_add(s, zng_emit_lit(s, ltree, c));
189
32.1M
}
deflate_quick.c:zng_tr_emit_lit
Line
Count
Source
187
32.1M
static inline void zng_tr_emit_lit(deflate_state *s, const ct_data *ltree, unsigned c) {
188
32.1M
    cmpr_bits_add(s, zng_emit_lit(s, ltree, c));
189
32.1M
}
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
1.45M
    uint32_t lc, uint32_t dist) {
196
1.45M
    cmpr_bits_add(s, zng_emit_dist(s, ltree, dtree, lc, dist));
197
1.45M
}
deflate_quick.c:zng_tr_emit_dist
Line
Count
Source
195
1.45M
    uint32_t lc, uint32_t dist) {
196
1.45M
    cmpr_bits_add(s, zng_emit_dist(s, ltree, dtree, lc, dist));
197
1.45M
}
Unexecuted instantiation: trees.c:zng_tr_emit_dist
198
199
/* ===========================================================================
200
 * Emit start of block
201
 */
202
19.2k
static inline void zng_tr_emit_tree(deflate_state *s, int type, const int last) {
203
19.2k
    uint32_t bi_valid = s->bi_valid;
204
19.2k
    uint64_t bi_buf = s->bi_buf;
205
19.2k
    uint32_t header_bits = (type << 1) + last;
206
19.2k
    send_bits(s, header_bits, 3, bi_buf, bi_valid);
207
19.2k
    cmpr_bits_add(s, 3);
208
19.2k
    s->bi_valid = bi_valid;
209
19.2k
    s->bi_buf = bi_buf;
210
19.2k
    Tracev((stderr, "\n--- Emit Tree: Last: %u\n", last));
211
19.2k
}
deflate_quick.c:zng_tr_emit_tree
Line
Count
Source
202
846
static inline void zng_tr_emit_tree(deflate_state *s, int type, const int last) {
203
846
    uint32_t bi_valid = s->bi_valid;
204
846
    uint64_t bi_buf = s->bi_buf;
205
846
    uint32_t header_bits = (type << 1) + last;
206
846
    send_bits(s, header_bits, 3, bi_buf, bi_valid);
207
846
    cmpr_bits_add(s, 3);
208
846
    s->bi_valid = bi_valid;
209
846
    s->bi_buf = bi_buf;
210
846
    Tracev((stderr, "\n--- Emit Tree: Last: %u\n", last));
211
846
}
trees.c:zng_tr_emit_tree
Line
Count
Source
202
18.3k
static inline void zng_tr_emit_tree(deflate_state *s, int type, const int last) {
203
18.3k
    uint32_t bi_valid = s->bi_valid;
204
18.3k
    uint64_t bi_buf = s->bi_buf;
205
18.3k
    uint32_t header_bits = (type << 1) + last;
206
18.3k
    send_bits(s, header_bits, 3, bi_buf, bi_valid);
207
18.3k
    cmpr_bits_add(s, 3);
208
18.3k
    s->bi_valid = bi_valid;
209
18.3k
    s->bi_buf = bi_buf;
210
18.3k
    Tracev((stderr, "\n--- Emit Tree: Last: %u\n", last));
211
18.3k
}
212
213
/* ===========================================================================
214
 * Align bit buffer on a byte boundary and count bits
215
 */
216
9.50k
static inline void zng_tr_emit_align(deflate_state *s) {
217
9.50k
    bi_windup(s); /* align on byte boundary */
218
9.50k
    sent_bits_align(s);
219
9.50k
}
deflate_quick.c:zng_tr_emit_align
Line
Count
Source
216
744
static inline void zng_tr_emit_align(deflate_state *s) {
217
744
    bi_windup(s); /* align on byte boundary */
218
744
    sent_bits_align(s);
219
744
}
trees.c:zng_tr_emit_align
Line
Count
Source
216
8.76k
static inline void zng_tr_emit_align(deflate_state *s) {
217
8.76k
    bi_windup(s); /* align on byte boundary */
218
8.76k
    sent_bits_align(s);
219
8.76k
}
220
221
/* ===========================================================================
222
 * Emit an end block and align bit buffer if last block
223
 */
224
846
static inline void zng_tr_emit_end_block(deflate_state *s, const ct_data *ltree, const int last) {
225
846
    zng_emit_end_block(s, ltree, last);
226
846
    cmpr_bits_add(s, 7);
227
846
    if (last)
228
744
        zng_tr_emit_align(s);
229
846
}
deflate_quick.c:zng_tr_emit_end_block
Line
Count
Source
224
846
static inline void zng_tr_emit_end_block(deflate_state *s, const ct_data *ltree, const int last) {
225
846
    zng_emit_end_block(s, ltree, last);
226
846
    cmpr_bits_add(s, 7);
227
846
    if (last)
228
744
        zng_tr_emit_align(s);
229
846
}
Unexecuted instantiation: trees.c:zng_tr_emit_end_block
230
231
#endif