Coverage Report

Created: 2025-11-24 06:41

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