Coverage Report

Created: 2025-11-11 07:03

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