Coverage Report

Created: 2025-07-11 06:15

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