Coverage Report

Created: 2025-12-11 06:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/c-blosc2/internal-complibs/zlib-ng-2.0.7/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
#  include <stdint.h>
11
#endif
12
13
14
/* trees.h */
15
extern Z_INTERNAL const ct_data static_ltree[L_CODES+2];
16
extern Z_INTERNAL const ct_data static_dtree[D_CODES];
17
18
extern const unsigned char Z_INTERNAL zng_dist_code[DIST_CODE_LEN];
19
extern const unsigned char Z_INTERNAL zng_length_code[MAX_MATCH-MIN_MATCH+1];
20
21
extern Z_INTERNAL const int base_length[LENGTH_CODES];
22
extern Z_INTERNAL const int base_dist[D_CODES];
23
24
/* Bit buffer and deflate code stderr tracing */
25
#ifdef ZLIB_DEBUG
26
#  define send_bits_trace(s, value, length) { \
27
        Tracevv((stderr, " l %2d v %4llx ", (int)(length), (long long)(value))); \
28
        Assert(length > 0 && length <= BIT_BUF_SIZE, "invalid length"); \
29
    }
30
#  define send_code_trace(s, c) \
31
    if (z_verbose > 2) { \
32
        fprintf(stderr, "\ncd %3d ", (c)); \
33
    }
34
#else
35
#  define send_bits_trace(s, value, length)
36
#  define send_code_trace(s, c)
37
#endif
38
39
/* If not enough room in bi_buf, use (valid) bits from bi_buf and
40
 * (64 - bi_valid) bits from value, leaving (width - (64-bi_valid))
41
 * unused bits in value.
42
 */
43
44.1M
#define send_bits(s, t_val, t_len, bi_buf, bi_valid) {\
44
44.1M
    uint64_t val = (uint64_t)t_val;\
45
44.1M
    uint32_t len = (uint32_t)t_len;\
46
44.1M
    uint32_t total_bits = bi_valid + len;\
47
44.1M
    send_bits_trace(s, val, len);\
48
44.1M
    sent_bits_add(s, len);\
49
44.1M
    if (total_bits < BIT_BUF_SIZE) {\
50
38.8M
        bi_buf |= val << bi_valid;\
51
38.8M
        bi_valid = total_bits;\
52
38.8M
    } else if (bi_valid == BIT_BUF_SIZE) {\
53
0
        put_uint64(s, bi_buf);\
54
0
        bi_buf = val;\
55
0
        bi_valid = len;\
56
5.30M
    } else {\
57
5.30M
        bi_buf |= val << bi_valid;\
58
5.30M
        put_uint64(s, bi_buf);\
59
5.30M
        bi_buf = val >> (BIT_BUF_SIZE - bi_valid);\
60
5.30M
        bi_valid = total_bits - BIT_BUF_SIZE;\
61
5.30M
    }\
62
44.1M
}
63
64
/* Send a code of the given tree. c and tree must not have side effects */
65
#ifdef ZLIB_DEBUG
66
#  define send_code(s, c, tree, bi_buf, bi_valid) { \
67
    send_code_trace(s, c); \
68
    send_bits(s, tree[c].Code, tree[c].Len, bi_buf, bi_valid); \
69
}
70
#else
71
#  define send_code(s, c, tree, bi_buf, bi_valid) \
72
42.2M
    send_bits(s, tree[c].Code, tree[c].Len, bi_buf, bi_valid)
73
#endif
74
75
/* ===========================================================================
76
 * Flush the bit buffer and align the output on a byte boundary
77
 */
78
33.8k
static void bi_windup(deflate_state *s) {
79
33.8k
    if (s->bi_valid > 56) {
80
3.07k
        put_uint64(s, s->bi_buf);
81
30.7k
    } else {
82
30.7k
        if (s->bi_valid > 24) {
83
12.9k
            put_uint32(s, (uint32_t)s->bi_buf);
84
12.9k
            s->bi_buf >>= 32;
85
12.9k
            s->bi_valid -= 32;
86
12.9k
        }
87
30.7k
        if (s->bi_valid > 8) {
88
12.9k
            put_short(s, (uint16_t)s->bi_buf);
89
12.9k
            s->bi_buf >>= 16;
90
12.9k
            s->bi_valid -= 16;
91
12.9k
        }
92
30.7k
        if (s->bi_valid > 0) {
93
16.8k
            put_byte(s, s->bi_buf);
94
16.8k
        }
95
30.7k
    }
96
33.8k
    s->bi_buf = 0;
97
33.8k
    s->bi_valid = 0;
98
33.8k
}
deflate_quick.c:bi_windup
Line
Count
Source
78
3.93k
static void bi_windup(deflate_state *s) {
79
3.93k
    if (s->bi_valid > 56) {
80
583
        put_uint64(s, s->bi_buf);
81
3.35k
    } else {
82
3.35k
        if (s->bi_valid > 24) {
83
1.84k
            put_uint32(s, (uint32_t)s->bi_buf);
84
1.84k
            s->bi_buf >>= 32;
85
1.84k
            s->bi_valid -= 32;
86
1.84k
        }
87
3.35k
        if (s->bi_valid > 8) {
88
2.04k
            put_short(s, (uint16_t)s->bi_buf);
89
2.04k
            s->bi_buf >>= 16;
90
2.04k
            s->bi_valid -= 16;
91
2.04k
        }
92
3.35k
        if (s->bi_valid > 0) {
93
1.77k
            put_byte(s, s->bi_buf);
94
1.77k
        }
95
3.35k
    }
96
3.93k
    s->bi_buf = 0;
97
3.93k
    s->bi_valid = 0;
98
3.93k
}
trees.c:bi_windup
Line
Count
Source
78
29.9k
static void bi_windup(deflate_state *s) {
79
29.9k
    if (s->bi_valid > 56) {
80
2.49k
        put_uint64(s, s->bi_buf);
81
27.4k
    } else {
82
27.4k
        if (s->bi_valid > 24) {
83
11.0k
            put_uint32(s, (uint32_t)s->bi_buf);
84
11.0k
            s->bi_buf >>= 32;
85
11.0k
            s->bi_valid -= 32;
86
11.0k
        }
87
27.4k
        if (s->bi_valid > 8) {
88
10.9k
            put_short(s, (uint16_t)s->bi_buf);
89
10.9k
            s->bi_buf >>= 16;
90
10.9k
            s->bi_valid -= 16;
91
10.9k
        }
92
27.4k
        if (s->bi_valid > 0) {
93
15.0k
            put_byte(s, s->bi_buf);
94
15.0k
        }
95
27.4k
    }
96
29.9k
    s->bi_buf = 0;
97
29.9k
    s->bi_valid = 0;
98
29.9k
}
99
100
/* ===========================================================================
101
 * Emit literal code
102
 */
103
39.5M
static inline uint32_t zng_emit_lit(deflate_state *s, const ct_data *ltree, unsigned c) {
104
39.5M
    uint32_t bi_valid = s->bi_valid;
105
39.5M
    uint64_t bi_buf = s->bi_buf;
106
107
39.5M
    send_code(s, c, ltree, bi_buf, bi_valid);
108
109
39.5M
    s->bi_valid = bi_valid;
110
39.5M
    s->bi_buf = bi_buf;
111
112
39.5M
    Tracecv(isgraph(c & 0xff), (stderr, " '%c' ", c));
113
114
39.5M
    return ltree[c].Len;
115
39.5M
}
deflate_quick.c:zng_emit_lit
Line
Count
Source
103
7.09M
static inline uint32_t zng_emit_lit(deflate_state *s, const ct_data *ltree, unsigned c) {
104
7.09M
    uint32_t bi_valid = s->bi_valid;
105
7.09M
    uint64_t bi_buf = s->bi_buf;
106
107
7.09M
    send_code(s, c, ltree, bi_buf, bi_valid);
108
109
7.09M
    s->bi_valid = bi_valid;
110
7.09M
    s->bi_buf = bi_buf;
111
112
7.09M
    Tracecv(isgraph(c & 0xff), (stderr, " '%c' ", c));
113
114
7.09M
    return ltree[c].Len;
115
7.09M
}
trees.c:zng_emit_lit
Line
Count
Source
103
32.4M
static inline uint32_t zng_emit_lit(deflate_state *s, const ct_data *ltree, unsigned c) {
104
32.4M
    uint32_t bi_valid = s->bi_valid;
105
32.4M
    uint64_t bi_buf = s->bi_buf;
106
107
32.4M
    send_code(s, c, ltree, bi_buf, bi_valid);
108
109
32.4M
    s->bi_valid = bi_valid;
110
32.4M
    s->bi_buf = bi_buf;
111
112
32.4M
    Tracecv(isgraph(c & 0xff), (stderr, " '%c' ", c));
113
114
32.4M
    return ltree[c].Len;
115
32.4M
}
116
117
/* ===========================================================================
118
 * Emit match distance/length code
119
 */
120
static inline uint32_t zng_emit_dist(deflate_state *s, const ct_data *ltree, const ct_data *dtree,
121
1.32M
    uint32_t lc, uint32_t dist) {
122
1.32M
    uint32_t c, extra;
123
1.32M
    uint8_t code;
124
1.32M
    uint64_t match_bits;
125
1.32M
    uint32_t match_bits_len;
126
1.32M
    uint32_t bi_valid = s->bi_valid;
127
1.32M
    uint64_t bi_buf = s->bi_buf;
128
129
    /* Send the length code, len is the match length - MIN_MATCH */
130
1.32M
    code = zng_length_code[lc];
131
1.32M
    c = code+LITERALS+1;
132
1.32M
    Assert(c < L_CODES, "bad l_code");
133
1.32M
    send_code_trace(s, c);
134
135
1.32M
    match_bits = ltree[c].Code;
136
1.32M
    match_bits_len = ltree[c].Len;
137
1.32M
    extra = extra_lbits[code];
138
1.32M
    if (extra != 0) {
139
459k
        lc -= base_length[code];
140
459k
        match_bits |= ((uint64_t)lc << match_bits_len);
141
459k
        match_bits_len += extra;
142
459k
    }
143
144
1.32M
    dist--; /* dist is now the match distance - 1 */
145
1.32M
    code = d_code(dist);
146
1.32M
    Assert(code < D_CODES, "bad d_code");
147
1.32M
    send_code_trace(s, code);
148
149
    /* Send the distance code */
150
1.32M
    match_bits |= ((uint64_t)dtree[code].Code << match_bits_len);
151
1.32M
    match_bits_len += dtree[code].Len;
152
1.32M
    extra = extra_dbits[code];
153
1.32M
    if (extra != 0) {
154
1.19M
        dist -= base_dist[code];
155
1.19M
        match_bits |= ((uint64_t)dist << match_bits_len);
156
1.19M
        match_bits_len += extra;
157
1.19M
    }
158
159
1.32M
    send_bits(s, match_bits, match_bits_len, bi_buf, bi_valid);
160
161
1.32M
    s->bi_valid = bi_valid;
162
1.32M
    s->bi_buf = bi_buf;
163
164
1.32M
    return match_bits_len;
165
1.32M
}
deflate_quick.c:zng_emit_dist
Line
Count
Source
121
161k
    uint32_t lc, uint32_t dist) {
122
161k
    uint32_t c, extra;
123
161k
    uint8_t code;
124
161k
    uint64_t match_bits;
125
161k
    uint32_t match_bits_len;
126
161k
    uint32_t bi_valid = s->bi_valid;
127
161k
    uint64_t bi_buf = s->bi_buf;
128
129
    /* Send the length code, len is the match length - MIN_MATCH */
130
161k
    code = zng_length_code[lc];
131
161k
    c = code+LITERALS+1;
132
161k
    Assert(c < L_CODES, "bad l_code");
133
161k
    send_code_trace(s, c);
134
135
161k
    match_bits = ltree[c].Code;
136
161k
    match_bits_len = ltree[c].Len;
137
161k
    extra = extra_lbits[code];
138
161k
    if (extra != 0) {
139
48.4k
        lc -= base_length[code];
140
48.4k
        match_bits |= ((uint64_t)lc << match_bits_len);
141
48.4k
        match_bits_len += extra;
142
48.4k
    }
143
144
161k
    dist--; /* dist is now the match distance - 1 */
145
161k
    code = d_code(dist);
146
161k
    Assert(code < D_CODES, "bad d_code");
147
161k
    send_code_trace(s, code);
148
149
    /* Send the distance code */
150
161k
    match_bits |= ((uint64_t)dtree[code].Code << match_bits_len);
151
161k
    match_bits_len += dtree[code].Len;
152
161k
    extra = extra_dbits[code];
153
161k
    if (extra != 0) {
154
152k
        dist -= base_dist[code];
155
152k
        match_bits |= ((uint64_t)dist << match_bits_len);
156
152k
        match_bits_len += extra;
157
152k
    }
158
159
161k
    send_bits(s, match_bits, match_bits_len, bi_buf, bi_valid);
160
161
161k
    s->bi_valid = bi_valid;
162
161k
    s->bi_buf = bi_buf;
163
164
161k
    return match_bits_len;
165
161k
}
trees.c:zng_emit_dist
Line
Count
Source
121
1.16M
    uint32_t lc, uint32_t dist) {
122
1.16M
    uint32_t c, extra;
123
1.16M
    uint8_t code;
124
1.16M
    uint64_t match_bits;
125
1.16M
    uint32_t match_bits_len;
126
1.16M
    uint32_t bi_valid = s->bi_valid;
127
1.16M
    uint64_t bi_buf = s->bi_buf;
128
129
    /* Send the length code, len is the match length - MIN_MATCH */
130
1.16M
    code = zng_length_code[lc];
131
1.16M
    c = code+LITERALS+1;
132
1.16M
    Assert(c < L_CODES, "bad l_code");
133
1.16M
    send_code_trace(s, c);
134
135
1.16M
    match_bits = ltree[c].Code;
136
1.16M
    match_bits_len = ltree[c].Len;
137
1.16M
    extra = extra_lbits[code];
138
1.16M
    if (extra != 0) {
139
410k
        lc -= base_length[code];
140
410k
        match_bits |= ((uint64_t)lc << match_bits_len);
141
410k
        match_bits_len += extra;
142
410k
    }
143
144
1.16M
    dist--; /* dist is now the match distance - 1 */
145
1.16M
    code = d_code(dist);
146
1.16M
    Assert(code < D_CODES, "bad d_code");
147
1.16M
    send_code_trace(s, code);
148
149
    /* Send the distance code */
150
1.16M
    match_bits |= ((uint64_t)dtree[code].Code << match_bits_len);
151
1.16M
    match_bits_len += dtree[code].Len;
152
1.16M
    extra = extra_dbits[code];
153
1.16M
    if (extra != 0) {
154
1.04M
        dist -= base_dist[code];
155
1.04M
        match_bits |= ((uint64_t)dist << match_bits_len);
156
1.04M
        match_bits_len += extra;
157
1.04M
    }
158
159
1.16M
    send_bits(s, match_bits, match_bits_len, bi_buf, bi_valid);
160
161
1.16M
    s->bi_valid = bi_valid;
162
1.16M
    s->bi_buf = bi_buf;
163
164
1.16M
    return match_bits_len;
165
1.16M
}
166
167
/* ===========================================================================
168
 * Emit end block
169
 */
170
26.2k
static inline void zng_emit_end_block(deflate_state *s, const ct_data *ltree, const int last) {
171
26.2k
    uint32_t bi_valid = s->bi_valid;
172
26.2k
    uint64_t bi_buf = s->bi_buf;
173
26.2k
    send_code(s, END_BLOCK, ltree, bi_buf, bi_valid);
174
26.2k
    s->bi_valid = bi_valid;
175
26.2k
    s->bi_buf = bi_buf;
176
26.2k
    Tracev((stderr, "\n+++ Emit End Block: Last: %u Pending: %u Total Out: %" PRIu64 "\n",
177
26.2k
        last, s->pending, (uint64_t)s->strm->total_out));
178
26.2k
    Z_UNUSED(last);
179
26.2k
}
deflate_quick.c:zng_emit_end_block
Line
Count
Source
170
3.93k
static inline void zng_emit_end_block(deflate_state *s, const ct_data *ltree, const int last) {
171
3.93k
    uint32_t bi_valid = s->bi_valid;
172
3.93k
    uint64_t bi_buf = s->bi_buf;
173
3.93k
    send_code(s, END_BLOCK, ltree, bi_buf, bi_valid);
174
3.93k
    s->bi_valid = bi_valid;
175
3.93k
    s->bi_buf = bi_buf;
176
3.93k
    Tracev((stderr, "\n+++ Emit End Block: Last: %u Pending: %u Total Out: %" PRIu64 "\n",
177
3.93k
        last, s->pending, (uint64_t)s->strm->total_out));
178
3.93k
    Z_UNUSED(last);
179
3.93k
}
trees.c:zng_emit_end_block
Line
Count
Source
170
22.2k
static inline void zng_emit_end_block(deflate_state *s, const ct_data *ltree, const int last) {
171
22.2k
    uint32_t bi_valid = s->bi_valid;
172
22.2k
    uint64_t bi_buf = s->bi_buf;
173
22.2k
    send_code(s, END_BLOCK, ltree, bi_buf, bi_valid);
174
22.2k
    s->bi_valid = bi_valid;
175
22.2k
    s->bi_buf = bi_buf;
176
22.2k
    Tracev((stderr, "\n+++ Emit End Block: Last: %u Pending: %u Total Out: %" PRIu64 "\n",
177
22.2k
        last, s->pending, (uint64_t)s->strm->total_out));
178
22.2k
    Z_UNUSED(last);
179
22.2k
}
180
181
/* ===========================================================================
182
 * Emit literal and count bits
183
 */
184
7.09M
static inline void zng_tr_emit_lit(deflate_state *s, const ct_data *ltree, unsigned c) {
185
7.09M
    cmpr_bits_add(s, zng_emit_lit(s, ltree, c));
186
7.09M
}
deflate_quick.c:zng_tr_emit_lit
Line
Count
Source
184
7.09M
static inline void zng_tr_emit_lit(deflate_state *s, const ct_data *ltree, unsigned c) {
185
7.09M
    cmpr_bits_add(s, zng_emit_lit(s, ltree, c));
186
7.09M
}
Unexecuted instantiation: trees.c:zng_tr_emit_lit
187
188
/* ===========================================================================
189
 * Emit match and count bits
190
 */
191
static inline void zng_tr_emit_dist(deflate_state *s, const ct_data *ltree, const ct_data *dtree,
192
161k
    uint32_t lc, uint32_t dist) {
193
161k
    cmpr_bits_add(s, zng_emit_dist(s, ltree, dtree, lc, dist));
194
161k
}
deflate_quick.c:zng_tr_emit_dist
Line
Count
Source
192
161k
    uint32_t lc, uint32_t dist) {
193
161k
    cmpr_bits_add(s, zng_emit_dist(s, ltree, dtree, lc, dist));
194
161k
}
Unexecuted instantiation: trees.c:zng_tr_emit_dist
195
196
/* ===========================================================================
197
 * Emit start of block
198
 */
199
30.0k
static inline void zng_tr_emit_tree(deflate_state *s, int type, const int last) {
200
30.0k
    uint32_t bi_valid = s->bi_valid;
201
30.0k
    uint64_t bi_buf = s->bi_buf;
202
30.0k
    uint32_t header_bits = (type << 1) + last;
203
30.0k
    send_bits(s, header_bits, 3, bi_buf, bi_valid);
204
30.0k
    cmpr_bits_add(s, 3);
205
30.0k
    s->bi_valid = bi_valid;
206
30.0k
    s->bi_buf = bi_buf;
207
30.0k
    Tracev((stderr, "\n--- Emit Tree: Last: %u\n", last));
208
30.0k
}
deflate_quick.c:zng_tr_emit_tree
Line
Count
Source
199
3.93k
static inline void zng_tr_emit_tree(deflate_state *s, int type, const int last) {
200
3.93k
    uint32_t bi_valid = s->bi_valid;
201
3.93k
    uint64_t bi_buf = s->bi_buf;
202
3.93k
    uint32_t header_bits = (type << 1) + last;
203
3.93k
    send_bits(s, header_bits, 3, bi_buf, bi_valid);
204
3.93k
    cmpr_bits_add(s, 3);
205
3.93k
    s->bi_valid = bi_valid;
206
3.93k
    s->bi_buf = bi_buf;
207
3.93k
    Tracev((stderr, "\n--- Emit Tree: Last: %u\n", last));
208
3.93k
}
trees.c:zng_tr_emit_tree
Line
Count
Source
199
26.1k
static inline void zng_tr_emit_tree(deflate_state *s, int type, const int last) {
200
26.1k
    uint32_t bi_valid = s->bi_valid;
201
26.1k
    uint64_t bi_buf = s->bi_buf;
202
26.1k
    uint32_t header_bits = (type << 1) + last;
203
26.1k
    send_bits(s, header_bits, 3, bi_buf, bi_valid);
204
26.1k
    cmpr_bits_add(s, 3);
205
26.1k
    s->bi_valid = bi_valid;
206
26.1k
    s->bi_buf = bi_buf;
207
26.1k
    Tracev((stderr, "\n--- Emit Tree: Last: %u\n", last));
208
26.1k
}
209
210
/* ===========================================================================
211
 * Align bit buffer on a byte boundary and count bits
212
 */
213
33.8k
static inline void zng_tr_emit_align(deflate_state *s) {
214
33.8k
    bi_windup(s); /* align on byte boundary */
215
33.8k
    sent_bits_align(s);
216
33.8k
}
deflate_quick.c:zng_tr_emit_align
Line
Count
Source
213
3.93k
static inline void zng_tr_emit_align(deflate_state *s) {
214
3.93k
    bi_windup(s); /* align on byte boundary */
215
3.93k
    sent_bits_align(s);
216
3.93k
}
trees.c:zng_tr_emit_align
Line
Count
Source
213
29.9k
static inline void zng_tr_emit_align(deflate_state *s) {
214
29.9k
    bi_windup(s); /* align on byte boundary */
215
29.9k
    sent_bits_align(s);
216
29.9k
}
217
218
/* ===========================================================================
219
 * Emit an end block and align bit buffer if last block
220
 */
221
3.93k
static inline void zng_tr_emit_end_block(deflate_state *s, const ct_data *ltree, const int last) {
222
3.93k
    zng_emit_end_block(s, ltree, last);
223
3.93k
    cmpr_bits_add(s, 7);
224
3.93k
    if (last)
225
3.93k
        zng_tr_emit_align(s);
226
3.93k
}
deflate_quick.c:zng_tr_emit_end_block
Line
Count
Source
221
3.93k
static inline void zng_tr_emit_end_block(deflate_state *s, const ct_data *ltree, const int last) {
222
3.93k
    zng_emit_end_block(s, ltree, last);
223
3.93k
    cmpr_bits_add(s, 7);
224
3.93k
    if (last)
225
3.93k
        zng_tr_emit_align(s);
226
3.93k
}
Unexecuted instantiation: trees.c:zng_tr_emit_end_block
227
228
#endif