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