Coverage Report

Created: 2025-08-26 06:46

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