Coverage Report

Created: 2026-01-17 06:26

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