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