Coverage Report

Created: 2025-07-12 06:16

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