Coverage Report

Created: 2023-12-08 06:32

/src/c-blosc2/internal-complibs/zlib-ng-2.0.7/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
#  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
66.1M
#define send_bits(s, t_val, t_len, bi_buf, bi_valid) {\
44
66.1M
    uint64_t val = (uint64_t)t_val;\
45
66.1M
    uint32_t len = (uint32_t)t_len;\
46
66.1M
    uint32_t total_bits = bi_valid + len;\
47
66.1M
    send_bits_trace(s, val, len);\
48
66.1M
    sent_bits_add(s, len);\
49
66.1M
    if (total_bits < BIT_BUF_SIZE) {\
50
58.1M
        bi_buf |= val << bi_valid;\
51
58.1M
        bi_valid = total_bits;\
52
58.1M
    } 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
8.04M
    } else {\
57
8.04M
        bi_buf |= val << bi_valid;\
58
8.04M
        put_uint64(s, bi_buf);\
59
8.04M
        bi_buf = val >> (BIT_BUF_SIZE - bi_valid);\
60
8.04M
        bi_valid = total_bits - BIT_BUF_SIZE;\
61
8.04M
    }\
62
66.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
63.8M
    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
42.4k
static void bi_windup(deflate_state *s) {
79
42.4k
    if (s->bi_valid > 56) {
80
3.54k
        put_uint64(s, s->bi_buf);
81
38.9k
    } else {
82
38.9k
        if (s->bi_valid > 24) {
83
16.2k
            put_uint32(s, (uint32_t)s->bi_buf);
84
16.2k
            s->bi_buf >>= 32;
85
16.2k
            s->bi_valid -= 32;
86
16.2k
        }
87
38.9k
        if (s->bi_valid > 8) {
88
16.2k
            put_short(s, (uint16_t)s->bi_buf);
89
16.2k
            s->bi_buf >>= 16;
90
16.2k
            s->bi_valid -= 16;
91
16.2k
        }
92
38.9k
        if (s->bi_valid > 0) {
93
21.4k
            put_byte(s, s->bi_buf);
94
21.4k
        }
95
38.9k
    }
96
42.4k
    s->bi_buf = 0;
97
42.4k
    s->bi_valid = 0;
98
42.4k
}
deflate_quick.c:bi_windup
Line
Count
Source
78
6.75k
static void bi_windup(deflate_state *s) {
79
6.75k
    if (s->bi_valid > 56) {
80
703
        put_uint64(s, s->bi_buf);
81
6.04k
    } else {
82
6.04k
        if (s->bi_valid > 24) {
83
3.41k
            put_uint32(s, (uint32_t)s->bi_buf);
84
3.41k
            s->bi_buf >>= 32;
85
3.41k
            s->bi_valid -= 32;
86
3.41k
        }
87
6.04k
        if (s->bi_valid > 8) {
88
3.56k
            put_short(s, (uint16_t)s->bi_buf);
89
3.56k
            s->bi_buf >>= 16;
90
3.56k
            s->bi_valid -= 16;
91
3.56k
        }
92
6.04k
        if (s->bi_valid > 0) {
93
3.30k
            put_byte(s, s->bi_buf);
94
3.30k
        }
95
6.04k
    }
96
6.75k
    s->bi_buf = 0;
97
6.75k
    s->bi_valid = 0;
98
6.75k
}
trees.c:bi_windup
Line
Count
Source
78
35.7k
static void bi_windup(deflate_state *s) {
79
35.7k
    if (s->bi_valid > 56) {
80
2.84k
        put_uint64(s, s->bi_buf);
81
32.8k
    } else {
82
32.8k
        if (s->bi_valid > 24) {
83
12.8k
            put_uint32(s, (uint32_t)s->bi_buf);
84
12.8k
            s->bi_buf >>= 32;
85
12.8k
            s->bi_valid -= 32;
86
12.8k
        }
87
32.8k
        if (s->bi_valid > 8) {
88
12.7k
            put_short(s, (uint16_t)s->bi_buf);
89
12.7k
            s->bi_buf >>= 16;
90
12.7k
            s->bi_valid -= 16;
91
12.7k
        }
92
32.8k
        if (s->bi_valid > 0) {
93
18.0k
            put_byte(s, s->bi_buf);
94
18.0k
        }
95
32.8k
    }
96
35.7k
    s->bi_buf = 0;
97
35.7k
    s->bi_valid = 0;
98
35.7k
}
99
100
/* ===========================================================================
101
 * Emit literal code
102
 */
103
60.4M
static inline uint32_t zng_emit_lit(deflate_state *s, const ct_data *ltree, unsigned c) {
104
60.4M
    uint32_t bi_valid = s->bi_valid;
105
60.4M
    uint64_t bi_buf = s->bi_buf;
106
107
60.4M
    send_code(s, c, ltree, bi_buf, bi_valid);
108
109
60.4M
    s->bi_valid = bi_valid;
110
60.4M
    s->bi_buf = bi_buf;
111
112
60.4M
    Tracecv(isgraph(c & 0xff), (stderr, " '%c' ", c));
113
114
60.4M
    return ltree[c].Len;
115
60.4M
}
deflate_quick.c:zng_emit_lit
Line
Count
Source
103
16.6M
static inline uint32_t zng_emit_lit(deflate_state *s, const ct_data *ltree, unsigned c) {
104
16.6M
    uint32_t bi_valid = s->bi_valid;
105
16.6M
    uint64_t bi_buf = s->bi_buf;
106
107
16.6M
    send_code(s, c, ltree, bi_buf, bi_valid);
108
109
16.6M
    s->bi_valid = bi_valid;
110
16.6M
    s->bi_buf = bi_buf;
111
112
16.6M
    Tracecv(isgraph(c & 0xff), (stderr, " '%c' ", c));
113
114
16.6M
    return ltree[c].Len;
115
16.6M
}
trees.c:zng_emit_lit
Line
Count
Source
103
43.7M
static inline uint32_t zng_emit_lit(deflate_state *s, const ct_data *ltree, unsigned c) {
104
43.7M
    uint32_t bi_valid = s->bi_valid;
105
43.7M
    uint64_t bi_buf = s->bi_buf;
106
107
43.7M
    send_code(s, c, ltree, bi_buf, bi_valid);
108
109
43.7M
    s->bi_valid = bi_valid;
110
43.7M
    s->bi_buf = bi_buf;
111
112
43.7M
    Tracecv(isgraph(c & 0xff), (stderr, " '%c' ", c));
113
114
43.7M
    return ltree[c].Len;
115
43.7M
}
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.61M
    uint32_t lc, uint32_t dist) {
122
1.61M
    uint32_t c, extra;
123
1.61M
    uint8_t code;
124
1.61M
    uint64_t match_bits;
125
1.61M
    uint32_t match_bits_len;
126
1.61M
    uint32_t bi_valid = s->bi_valid;
127
1.61M
    uint64_t bi_buf = s->bi_buf;
128
129
    /* Send the length code, len is the match length - MIN_MATCH */
130
1.61M
    code = zng_length_code[lc];
131
1.61M
    c = code+LITERALS+1;
132
1.61M
    Assert(c < L_CODES, "bad l_code");
133
1.61M
    send_code_trace(s, c);
134
135
1.61M
    match_bits = ltree[c].Code;
136
1.61M
    match_bits_len = ltree[c].Len;
137
1.61M
    extra = extra_lbits[code];
138
1.61M
    if (extra != 0) {
139
446k
        lc -= base_length[code];
140
446k
        match_bits |= ((uint64_t)lc << match_bits_len);
141
446k
        match_bits_len += extra;
142
446k
    }
143
144
1.61M
    dist--; /* dist is now the match distance - 1 */
145
1.61M
    code = d_code(dist);
146
1.61M
    Assert(code < D_CODES, "bad d_code");
147
1.61M
    send_code_trace(s, code);
148
149
    /* Send the distance code */
150
1.61M
    match_bits |= ((uint64_t)dtree[code].Code << match_bits_len);
151
1.61M
    match_bits_len += dtree[code].Len;
152
1.61M
    extra = extra_dbits[code];
153
1.61M
    if (extra != 0) {
154
1.48M
        dist -= base_dist[code];
155
1.48M
        match_bits |= ((uint64_t)dist << match_bits_len);
156
1.48M
        match_bits_len += extra;
157
1.48M
    }
158
159
1.61M
    send_bits(s, match_bits, match_bits_len, bi_buf, bi_valid);
160
161
1.61M
    s->bi_valid = bi_valid;
162
1.61M
    s->bi_buf = bi_buf;
163
164
1.61M
    return match_bits_len;
165
1.61M
}
deflate_quick.c:zng_emit_dist
Line
Count
Source
121
425k
    uint32_t lc, uint32_t dist) {
122
425k
    uint32_t c, extra;
123
425k
    uint8_t code;
124
425k
    uint64_t match_bits;
125
425k
    uint32_t match_bits_len;
126
425k
    uint32_t bi_valid = s->bi_valid;
127
425k
    uint64_t bi_buf = s->bi_buf;
128
129
    /* Send the length code, len is the match length - MIN_MATCH */
130
425k
    code = zng_length_code[lc];
131
425k
    c = code+LITERALS+1;
132
425k
    Assert(c < L_CODES, "bad l_code");
133
425k
    send_code_trace(s, c);
134
135
425k
    match_bits = ltree[c].Code;
136
425k
    match_bits_len = ltree[c].Len;
137
425k
    extra = extra_lbits[code];
138
425k
    if (extra != 0) {
139
40.1k
        lc -= base_length[code];
140
40.1k
        match_bits |= ((uint64_t)lc << match_bits_len);
141
40.1k
        match_bits_len += extra;
142
40.1k
    }
143
144
425k
    dist--; /* dist is now the match distance - 1 */
145
425k
    code = d_code(dist);
146
425k
    Assert(code < D_CODES, "bad d_code");
147
425k
    send_code_trace(s, code);
148
149
    /* Send the distance code */
150
425k
    match_bits |= ((uint64_t)dtree[code].Code << match_bits_len);
151
425k
    match_bits_len += dtree[code].Len;
152
425k
    extra = extra_dbits[code];
153
425k
    if (extra != 0) {
154
412k
        dist -= base_dist[code];
155
412k
        match_bits |= ((uint64_t)dist << match_bits_len);
156
412k
        match_bits_len += extra;
157
412k
    }
158
159
425k
    send_bits(s, match_bits, match_bits_len, bi_buf, bi_valid);
160
161
425k
    s->bi_valid = bi_valid;
162
425k
    s->bi_buf = bi_buf;
163
164
425k
    return match_bits_len;
165
425k
}
trees.c:zng_emit_dist
Line
Count
Source
121
1.18M
    uint32_t lc, uint32_t dist) {
122
1.18M
    uint32_t c, extra;
123
1.18M
    uint8_t code;
124
1.18M
    uint64_t match_bits;
125
1.18M
    uint32_t match_bits_len;
126
1.18M
    uint32_t bi_valid = s->bi_valid;
127
1.18M
    uint64_t bi_buf = s->bi_buf;
128
129
    /* Send the length code, len is the match length - MIN_MATCH */
130
1.18M
    code = zng_length_code[lc];
131
1.18M
    c = code+LITERALS+1;
132
1.18M
    Assert(c < L_CODES, "bad l_code");
133
1.18M
    send_code_trace(s, c);
134
135
1.18M
    match_bits = ltree[c].Code;
136
1.18M
    match_bits_len = ltree[c].Len;
137
1.18M
    extra = extra_lbits[code];
138
1.18M
    if (extra != 0) {
139
406k
        lc -= base_length[code];
140
406k
        match_bits |= ((uint64_t)lc << match_bits_len);
141
406k
        match_bits_len += extra;
142
406k
    }
143
144
1.18M
    dist--; /* dist is now the match distance - 1 */
145
1.18M
    code = d_code(dist);
146
1.18M
    Assert(code < D_CODES, "bad d_code");
147
1.18M
    send_code_trace(s, code);
148
149
    /* Send the distance code */
150
1.18M
    match_bits |= ((uint64_t)dtree[code].Code << match_bits_len);
151
1.18M
    match_bits_len += dtree[code].Len;
152
1.18M
    extra = extra_dbits[code];
153
1.18M
    if (extra != 0) {
154
1.06M
        dist -= base_dist[code];
155
1.06M
        match_bits |= ((uint64_t)dist << match_bits_len);
156
1.06M
        match_bits_len += extra;
157
1.06M
    }
158
159
1.18M
    send_bits(s, match_bits, match_bits_len, bi_buf, bi_valid);
160
161
1.18M
    s->bi_valid = bi_valid;
162
1.18M
    s->bi_buf = bi_buf;
163
164
1.18M
    return match_bits_len;
165
1.18M
}
166
167
/* ===========================================================================
168
 * Emit end block
169
 */
170
32.0k
static inline void zng_emit_end_block(deflate_state *s, const ct_data *ltree, const int last) {
171
32.0k
    uint32_t bi_valid = s->bi_valid;
172
32.0k
    uint64_t bi_buf = s->bi_buf;
173
32.0k
    send_code(s, END_BLOCK, ltree, bi_buf, bi_valid);
174
32.0k
    s->bi_valid = bi_valid;
175
32.0k
    s->bi_buf = bi_buf;
176
32.0k
    Tracev((stderr, "\n+++ Emit End Block: Last: %u Pending: %u Total Out: %" PRIu64 "\n",
177
32.0k
        last, s->pending, (uint64_t)s->strm->total_out));
178
32.0k
    Z_UNUSED(last);
179
32.0k
}
deflate_quick.c:zng_emit_end_block
Line
Count
Source
170
6.75k
static inline void zng_emit_end_block(deflate_state *s, const ct_data *ltree, const int last) {
171
6.75k
    uint32_t bi_valid = s->bi_valid;
172
6.75k
    uint64_t bi_buf = s->bi_buf;
173
6.75k
    send_code(s, END_BLOCK, ltree, bi_buf, bi_valid);
174
6.75k
    s->bi_valid = bi_valid;
175
6.75k
    s->bi_buf = bi_buf;
176
6.75k
    Tracev((stderr, "\n+++ Emit End Block: Last: %u Pending: %u Total Out: %" PRIu64 "\n",
177
6.75k
        last, s->pending, (uint64_t)s->strm->total_out));
178
6.75k
    Z_UNUSED(last);
179
6.75k
}
trees.c:zng_emit_end_block
Line
Count
Source
170
25.3k
static inline void zng_emit_end_block(deflate_state *s, const ct_data *ltree, const int last) {
171
25.3k
    uint32_t bi_valid = s->bi_valid;
172
25.3k
    uint64_t bi_buf = s->bi_buf;
173
25.3k
    send_code(s, END_BLOCK, ltree, bi_buf, bi_valid);
174
25.3k
    s->bi_valid = bi_valid;
175
25.3k
    s->bi_buf = bi_buf;
176
25.3k
    Tracev((stderr, "\n+++ Emit End Block: Last: %u Pending: %u Total Out: %" PRIu64 "\n",
177
25.3k
        last, s->pending, (uint64_t)s->strm->total_out));
178
25.3k
    Z_UNUSED(last);
179
25.3k
}
180
181
/* ===========================================================================
182
 * Emit literal and count bits
183
 */
184
16.6M
static inline void zng_tr_emit_lit(deflate_state *s, const ct_data *ltree, unsigned c) {
185
16.6M
    cmpr_bits_add(s, zng_emit_lit(s, ltree, c));
186
16.6M
}
deflate_quick.c:zng_tr_emit_lit
Line
Count
Source
184
16.6M
static inline void zng_tr_emit_lit(deflate_state *s, const ct_data *ltree, unsigned c) {
185
16.6M
    cmpr_bits_add(s, zng_emit_lit(s, ltree, c));
186
16.6M
}
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
425k
    uint32_t lc, uint32_t dist) {
193
425k
    cmpr_bits_add(s, zng_emit_dist(s, ltree, dtree, lc, dist));
194
425k
}
deflate_quick.c:zng_tr_emit_dist
Line
Count
Source
192
425k
    uint32_t lc, uint32_t dist) {
193
425k
    cmpr_bits_add(s, zng_emit_dist(s, ltree, dtree, lc, dist));
194
425k
}
Unexecuted instantiation: trees.c:zng_tr_emit_dist
195
196
/* ===========================================================================
197
 * Emit start of block
198
 */
199
37.2k
static inline void zng_tr_emit_tree(deflate_state *s, int type, const int last) {
200
37.2k
    uint32_t bi_valid = s->bi_valid;
201
37.2k
    uint64_t bi_buf = s->bi_buf;
202
37.2k
    uint32_t header_bits = (type << 1) + last;
203
37.2k
    send_bits(s, header_bits, 3, bi_buf, bi_valid);
204
37.2k
    cmpr_bits_add(s, 3);
205
37.2k
    s->bi_valid = bi_valid;
206
37.2k
    s->bi_buf = bi_buf;
207
37.2k
    Tracev((stderr, "\n--- Emit Tree: Last: %u\n", last));
208
37.2k
}
deflate_quick.c:zng_tr_emit_tree
Line
Count
Source
199
6.75k
static inline void zng_tr_emit_tree(deflate_state *s, int type, const int last) {
200
6.75k
    uint32_t bi_valid = s->bi_valid;
201
6.75k
    uint64_t bi_buf = s->bi_buf;
202
6.75k
    uint32_t header_bits = (type << 1) + last;
203
6.75k
    send_bits(s, header_bits, 3, bi_buf, bi_valid);
204
6.75k
    cmpr_bits_add(s, 3);
205
6.75k
    s->bi_valid = bi_valid;
206
6.75k
    s->bi_buf = bi_buf;
207
6.75k
    Tracev((stderr, "\n--- Emit Tree: Last: %u\n", last));
208
6.75k
}
trees.c:zng_tr_emit_tree
Line
Count
Source
199
30.5k
static inline void zng_tr_emit_tree(deflate_state *s, int type, const int last) {
200
30.5k
    uint32_t bi_valid = s->bi_valid;
201
30.5k
    uint64_t bi_buf = s->bi_buf;
202
30.5k
    uint32_t header_bits = (type << 1) + last;
203
30.5k
    send_bits(s, header_bits, 3, bi_buf, bi_valid);
204
30.5k
    cmpr_bits_add(s, 3);
205
30.5k
    s->bi_valid = bi_valid;
206
30.5k
    s->bi_buf = bi_buf;
207
30.5k
    Tracev((stderr, "\n--- Emit Tree: Last: %u\n", last));
208
30.5k
}
209
210
/* ===========================================================================
211
 * Align bit buffer on a byte boundary and count bits
212
 */
213
42.4k
static inline void zng_tr_emit_align(deflate_state *s) {
214
42.4k
    bi_windup(s); /* align on byte boundary */
215
42.4k
    sent_bits_align(s);
216
42.4k
}
deflate_quick.c:zng_tr_emit_align
Line
Count
Source
213
6.75k
static inline void zng_tr_emit_align(deflate_state *s) {
214
6.75k
    bi_windup(s); /* align on byte boundary */
215
6.75k
    sent_bits_align(s);
216
6.75k
}
trees.c:zng_tr_emit_align
Line
Count
Source
213
35.7k
static inline void zng_tr_emit_align(deflate_state *s) {
214
35.7k
    bi_windup(s); /* align on byte boundary */
215
35.7k
    sent_bits_align(s);
216
35.7k
}
217
218
/* ===========================================================================
219
 * Emit an end block and align bit buffer if last block
220
 */
221
6.75k
static inline void zng_tr_emit_end_block(deflate_state *s, const ct_data *ltree, const int last) {
222
6.75k
    zng_emit_end_block(s, ltree, last);
223
6.75k
    cmpr_bits_add(s, 7);
224
6.75k
    if (last)
225
6.75k
        zng_tr_emit_align(s);
226
6.75k
}
deflate_quick.c:zng_tr_emit_end_block
Line
Count
Source
221
6.75k
static inline void zng_tr_emit_end_block(deflate_state *s, const ct_data *ltree, const int last) {
222
6.75k
    zng_emit_end_block(s, ltree, last);
223
6.75k
    cmpr_bits_add(s, 7);
224
6.75k
    if (last)
225
6.75k
        zng_tr_emit_align(s);
226
6.75k
}
Unexecuted instantiation: trees.c:zng_tr_emit_end_block
227
228
#endif