Debian Codegraph @

third_party/rust/zlib-rs/src/inflate/inftrees.rs

raw
1 #![forbid(unsafe_code)]
2
3 use crate::{Code, ENOUGH_DISTS, ENOUGH_LENS};
4
5 pub(crate) enum CodeType {
6 Codes,
7 Lens,
8 Dists,
9 }
10
11 const MAX_BITS: usize = 15;
12
13 fn min_max<const N: usize>(count: [u16; N]) -> (usize, usize) {
14 let mut max = MAX_BITS;
15 while max >= 1 {
16 if count[max] != 0 {
17 break;
18 }
19 max -= 1;
20 }
21
22 let mut min = 1;
23 while min < max {
24 if count[min] != 0 {
25 break;
26 }
27 min += 1;
28 }
29
30 (min, max)
31 }
32
33 /// Length codes 257..285 base
34 const LBASE: [u16; 31] = [
35 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115, 131,
36 163, 195, 227, 258, 0, 0,
37 ];
38 /// Length codes 257..285 extra
39 const LEXT: [u16; 31] = [
40 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18, 19, 19, 19, 19, 20, 20, 20, 20,
41 21, 21, 21, 21, 16, 77, 202,
42 ];
43 /// Distance codes 0..29 base
44 const DBASE: [u16; 32] = [
45 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537,
46 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577, 0, 0,
47 ];
48 /// Distance codes 0..29 extra
49 const DEXT: [u16; 32] = [
50 16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 23, 24, 24, 25, 25, 26, 26,
51 27, 27, 28, 28, 29, 29, 64, 64,
52 ];
53
54 #[repr(i32)]
55 #[derive(Debug, PartialEq, Eq)]
56 pub(crate) enum InflateTable {
57 EnoughIsNotEnough = 1,
58 Success(usize) = 0,
59 InvalidCode = -1,
60 }
61
62 pub(crate) fn inflate_table(
63 codetype: CodeType,
64 lens: &[u16],
65 codes: usize,
66 table: &mut [Code],
67 bits: usize,
68 work: &mut [u16],
69 ) -> InflateTable {
70 // number of codes of each length
71 let mut count = [0u16; MAX_BITS + 1];
72
73 for len in lens[0..codes].iter().copied() {
74 count[len as usize] += 1;
75 }
76
77 let mut root = bits;
78
79 let (min, max) = min_max(count);
80 root = Ord::min(root, max);
81 root = Ord::max(root, min);
82
83 if max == 0 {
84 // no symbols to code at all
85 let code = Code {
86 op: 64,
87 bits: 1,
88 val: 0,
89 };
90
91 table[0] = code;
92 table[1] = code;
93
94 return InflateTable::Success(1);
95 }
96
97 /* check for an over-subscribed or incomplete set of lengths */
98 let mut left = 1i32;
99 let mut len = 1;
100 while len <= MAX_BITS {
101 left <<= 1;
102 left -= count[len] as i32;
103 if left < 0 {
104 // over-subscribed
105 return InflateTable::InvalidCode;
106 }
107 len += 1;
108 }
109
110 if left > 0 && (matches!(codetype, CodeType::Codes) || max != 1) {
111 // incomplete set
112 return InflateTable::InvalidCode;
113 }
114
115 /* generate offsets into symbol table for each length for sorting */
116
117 // offsets in table for each length
118 let mut offs = [0u16; MAX_BITS + 1];
119 for len in 1..MAX_BITS {
120 offs[len + 1] = offs[len] + count[len];
121 }
122
123 /* sort symbols by length, by symbol order within each length */
124 for (sym, len) in lens[0..codes].iter().copied().enumerate() {
125 if len != 0 {
126 let offset = offs[len as usize];
127 offs[len as usize] += 1;
128 work[offset as usize] = sym as u16;
129 }
130 }
131
132 let (base, extra, match_) = match codetype {
133 CodeType::Codes => (&[] as &[_], &[] as &[_], 20),
134 CodeType::Lens => (&LBASE[..], &LEXT[..], 257),
135 CodeType::Dists => (&DBASE[..], &DEXT[..], 0),
136 };
137
138 let used = 1 << root;
139
140 /* check available table space */
141 if matches!(codetype, CodeType::Lens) && used > ENOUGH_LENS {
142 return InflateTable::EnoughIsNotEnough;
143 }
144
145 if matches!(codetype, CodeType::Dists) && used > ENOUGH_DISTS {
146 return InflateTable::EnoughIsNotEnough;
147 }
148
149 let mut huff = 0; // starting code
150 let mut reversed_huff = 0u32; // starting code, reversed
151 let mut sym = 0;
152 let mut len = min;
153 let mut next = 0usize; // index into `table`
154 let mut curr = root;
155 let mut drop_ = 0;
156 let mut low = usize::MAX; // trigger new subtable when len > root
157 let mut used = 1 << root;
158 let mask = used - 1; /* mask for comparing low */
159
160 // process all codes and make table entries
161 'outer: loop {
162 // create table entry
163 let here = if work[sym] >= match_ {
164 Code {
165 bits: (len - drop_) as u8,
166 op: extra[(work[sym] - match_) as usize] as u8,
167 val: base[(work[sym] - match_) as usize],
168 }
169 } else if work[sym] + 1 < match_ {
170 Code {
171 bits: (len - drop_) as u8,
172 op: 0,
173 val: work[sym],
174 }
175 } else {
176 Code {
177 bits: (len - drop_) as u8,
178 op: 0b01100000,
179 val: 0,
180 }
181 };
182
183 // replicate for those indices with low len bits equal to huff
184 let incr = 1 << (len - drop_);
185 let min = 1 << curr; // also has the name 'fill' in the C code
186
187 let base = &mut table[next + (huff >> drop_)..];
188 for fill in (0..min).step_by(incr) {
189 base[fill] = here;
190 }
191
192 // backwards increment the len-bit code huff
193 reversed_huff = reversed_huff.wrapping_add(0x80000000u32 >> (len - 1));
194 huff = reversed_huff.reverse_bits() as usize;
195
196 // go to next symbol, update count, len
197 sym += 1;
198 count[len] -= 1;
199 if count[len] == 0 {
200 if len == max {
201 break 'outer;
202 }
203 len = lens[work[sym] as usize] as usize;
204 }
205
206 // create new sub-table if needed
207 if len > root && (huff & mask) != low {
208 /* if first time, transition to sub-tables */
209 if drop_ == 0 {
210 drop_ = root;
211 }
212
213 /* increment past last table */
214 next += min; /* here min is 1 << curr */
215
216 /* determine length of next table */
217 curr = len - drop_;
218 let mut left = 1 << curr;
219 while curr + drop_ < max {
220 left -= count[curr + drop_] as i32;
221 if left <= 0 {
222 break;
223 }
224 curr += 1;
225 left <<= 1;
226 }
227
228 /* check for enough space */
229 used += 1usize << curr;
230
231 if matches!(codetype, CodeType::Lens) && used > ENOUGH_LENS {
232 return InflateTable::EnoughIsNotEnough;
233 }
234
235 if matches!(codetype, CodeType::Dists) && used > ENOUGH_DISTS {
236 return InflateTable::EnoughIsNotEnough;
237 }
238
239 /* point entry in root table to sub-table */
240 low = huff & mask;
241 table[low] = Code {
242 op: curr as u8,
243 bits: root as u8,
244 val: next as u16,
245 };
246 }
247 }
248
249 /* fill in remaining table entry if code is incomplete (guaranteed to have
250 at most one remaining entry, since if the code is incomplete, the
251 maximum code length that was allowed to get this far is one bit) */
252 if huff != 0 {
253 let here = Code {
254 op: 64,
255 bits: (len - drop_) as u8,
256 val: 0,
257 };
258
259 table[next..][huff] = here;
260 }
261
262 /* set return parameters */
263 InflateTable::Success(root)
264 }
265
266 #[cfg(test)]
267 mod test {
268 use super::*;
269
270 #[test]
271 fn not_enough_errors() {
272 // we need to call inflate_table() directly in order to manifest
273 // not-enough errors, since zlib insures that enough is always enough
274
275 let table = [Code::default(); crate::ENOUGH_DISTS];
276
277 let mut work = [0; 16];
278 let mut lens: [_; 16] = core::array::from_fn(|i| (i + 1) as u16);
279 lens[15] = 15;
280
281 let mut next = table;
282 let bits = 15;
283 let ret = inflate_table(CodeType::Dists, &lens, 16, &mut next, bits, &mut work);
284 assert_eq!(ret, InflateTable::EnoughIsNotEnough);
285
286 let mut next = table;
287 let bits = 1;
288 let ret = inflate_table(CodeType::Dists, &lens, 16, &mut next, bits, &mut work);
289 assert_eq!(ret, InflateTable::EnoughIsNotEnough);
290 }
291
292 fn build_fixed_length_table(work: &mut [u16]) -> [Code; 512] {
293 let mut lens = [0; 288];
294
295 // literal/length table
296 let mut sym = 0usize;
297 while sym < 144 {
298 lens[sym] = 8;
299 sym += 1;
300 }
301 while sym < 256 {
302 lens[sym] = 9;
303 sym += 1;
304 }
305 while sym < 280 {
306 lens[sym] = 7;
307 sym += 1;
308 }
309 while sym < 288 {
310 lens[sym] = 8;
311 sym += 1;
312 }
313
314 let mut next = [Code::default(); 512];
315 let bits = 9;
316 inflate_table(CodeType::Lens, &lens, 288, &mut next, bits, work);
317
318 core::array::from_fn(|i| {
319 let mut code = next[i];
320
321 code.op = if i & 0b0111_1111 == 99 { 64 } else { code.op };
322
323 code
324 })
325 }
326
327 #[test]
328 fn generate_fixed_length_table() {
329 let mut work = [0; 512];
330 let generated = build_fixed_length_table(&mut work);
331
332 assert_eq!(generated, crate::inflate::inffixed_tbl::LENFIX);
333 }
334
335 fn build_fixed_distance_table(work: &mut [u16]) -> [Code; 32] {
336 let mut lens = [0; 288];
337
338 let mut sym = 0;
339 while sym < 32 {
340 lens[sym] = 5;
341 sym += 1;
342 }
343
344 let mut next = [Code::default(); 32];
345 let bits = 5;
346 inflate_table(CodeType::Dists, &lens, 32, &mut next, bits, work);
347
348 next
349 }
350
351 #[test]
352 fn generate_fixed_distance_table() {
353 let mut work = [0; 512];
354 let generated = build_fixed_distance_table(&mut work);
355
356 assert_eq!(generated, crate::inflate::inffixed_tbl::DISTFIX);
357 }
358 }