| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
use std::{fmt, mem}; |
| 17 |
|
| 18 |
pub(crate) use bitstream::Bitstream; |
| 19 |
pub(crate) use block::{Block, Decoded, Kind as BlockKind}; |
| 20 |
pub(crate) use tree::{CanonicalTree, Tree}; |
| 21 |
use window::Window; |
| 22 |
pub use window::WindowSize; |
| 23 |
|
| 24 |
mod bitstream; |
| 25 |
mod block; |
| 26 |
mod tree; |
| 27 |
mod window; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
pub const MAX_CHUNK_SIZE: usize = 32 * 1024; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
pub(crate) struct DecoderState { |
| 36 |
|
| 37 |
window_size: WindowSize, |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
main_tree: CanonicalTree, |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
length_tree: CanonicalTree, |
| 46 |
} |
| 47 |
|
| 48 |
struct PostProcessState { |
| 49 |
|
| 50 |
e8_translation_size: i32, |
| 51 |
|
| 52 |
|
| 53 |
data_chunk: Box<[u8]>, |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
pub struct Lzxd { |
| 74 |
|
| 75 |
window: Window, |
| 76 |
|
| 77 |
|
| 78 |
state: DecoderState, |
| 79 |
|
| 80 |
|
| 81 |
r: [u32; 3], |
| 82 |
|
| 83 |
|
| 84 |
chunk_offset: usize, |
| 85 |
|
| 86 |
|
| 87 |
first_chunk_read: bool, |
| 88 |
|
| 89 |
|
| 90 |
current_block: Block, |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
postprocess: Option<PostProcessState>, |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 99 |
pub enum DecodeFailed { |
| 100 |
|
| 101 |
OverreadBlock, |
| 102 |
|
| 103 |
|
| 104 |
UnexpectedEof, |
| 105 |
|
| 106 |
|
| 107 |
InvalidBlock(u8), |
| 108 |
|
| 109 |
|
| 110 |
InvalidBlockSize(u32), |
| 111 |
|
| 112 |
|
| 113 |
InvalidPretreeElement(u16), |
| 114 |
|
| 115 |
|
| 116 |
InvalidPretreeRle, |
| 117 |
|
| 118 |
|
| 119 |
InvalidPathLengths, |
| 120 |
|
| 121 |
|
| 122 |
EmptyTree, |
| 123 |
|
| 124 |
|
| 125 |
WindowTooSmall, |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
ChunkTooLong, |
| 131 |
} |
| 132 |
|
| 133 |
impl fmt::Display for DecodeFailed { |
| 134 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 135 |
use DecodeFailed::*; |
| 136 |
|
| 137 |
match self { |
| 138 |
OverreadBlock => write!( |
| 139 |
f, |
| 140 |
"read more items than available in the block in a single step" |
| 141 |
), |
| 142 |
UnexpectedEof => write!(f, "reached end of chunk without fully decoding it"), |
| 143 |
InvalidBlock(kind) => write!(f, "block type {} is invalid", kind), |
| 144 |
InvalidBlockSize(size) => write!(f, "block size {} is invalid", size), |
| 145 |
InvalidPretreeElement(elem) => write!(f, "found invalid pretree element {}", elem), |
| 146 |
InvalidPretreeRle => write!(f, "found invalid pretree rle element"), |
| 147 |
InvalidPathLengths => write!(f, "encountered invalid path lengths"), |
| 148 |
EmptyTree => write!(f, "encountered empty decode tree"), |
| 149 |
WindowTooSmall => write!(f, "decode window was too small"), |
| 150 |
ChunkTooLong => write!( |
| 151 |
f, |
| 152 |
"tried reading a chunk longer than {} bytes", |
| 153 |
MAX_CHUNK_SIZE |
| 154 |
), |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
impl std::error::Error for DecodeFailed {} |
| 160 |
|
| 161 |
|
| 162 |
#[derive(Debug, Copy, Clone, PartialEq, Eq)] |
| 163 |
pub struct DecompressError(DecodeFailed); |
| 164 |
|
| 165 |
impl fmt::Display for DecompressError { |
| 166 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 167 |
self.0.fmt(f) |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
impl std::error::Error for DecompressError {} |
| 172 |
|
| 173 |
impl From<DecodeFailed> for DecompressError { |
| 174 |
fn from(value: DecodeFailed) -> Self { |
| 175 |
Self(value) |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
impl Lzxd { |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
pub fn new(window_size: WindowSize) -> Self { |
| 186 |
|
| 187 |
|
| 188 |
let main_tree = CanonicalTree::new(256 + 8 * window_size.position_slots()); |
| 189 |
|
| 190 |
|
| 191 |
let length_tree = CanonicalTree::new(249); |
| 192 |
|
| 193 |
Self { |
| 194 |
window: window_size.create_buffer(), |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
state: DecoderState { |
| 201 |
window_size, |
| 202 |
main_tree, |
| 203 |
length_tree, |
| 204 |
}, |
| 205 |
|
| 206 |
r: [1, 1, 1], |
| 207 |
first_chunk_read: false, |
| 208 |
chunk_offset: 0, |
| 209 |
postprocess: None, |
| 210 |
|
| 211 |
current_block: Block { |
| 212 |
remaining: 0, |
| 213 |
size: 0, |
| 214 |
kind: BlockKind::Uncompressed { r: [1, 1, 1] }, |
| 215 |
}, |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
fn try_read_first_chunk(&mut self, bitstream: &mut Bitstream) -> Result<(), DecodeFailed> { |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
if !self.first_chunk_read { |
| 226 |
self.first_chunk_read = true; |
| 227 |
|
| 228 |
let e8_translation = bitstream.read_bit()? != 0; |
| 229 |
self.postprocess = if e8_translation { |
| 230 |
Some(PostProcessState { |
| 231 |
data_chunk: vec![0; MAX_CHUNK_SIZE].into_boxed_slice(), |
| 232 |
e8_translation_size: bitstream.read_bits(32)? as i32, |
| 233 |
}) |
| 234 |
} else { |
| 235 |
None |
| 236 |
}; |
| 237 |
} |
| 238 |
|
| 239 |
Ok(()) |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
fn postprocess( |
| 244 |
translation_size: i32, |
| 245 |
chunk_offset: usize, |
| 246 |
idata: &mut [u8], |
| 247 |
) -> Result<&[u8], DecodeFailed> { |
| 248 |
let mut processed = 0usize; |
| 249 |
|
| 250 |
|
| 251 |
while let Some(pos) = idata[processed..] |
| 252 |
.iter() |
| 253 |
.position(|&e| e == 0xE8) |
| 254 |
.map(|pos| processed + pos) |
| 255 |
{ |
| 256 |
|
| 257 |
if idata.len() - pos <= 10 { |
| 258 |
break; |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
let current_pointer = chunk_offset + pos; |
| 263 |
|
| 264 |
|
| 265 |
let abs_val = i32::from_le_bytes([ |
| 266 |
idata[pos + 1], |
| 267 |
idata[pos + 2], |
| 268 |
idata[pos + 3], |
| 269 |
idata[pos + 4], |
| 270 |
]); |
| 271 |
if (abs_val >= -(current_pointer as i32)) && abs_val < translation_size { |
| 272 |
let rel_val = if abs_val.is_positive() { |
| 273 |
abs_val.wrapping_sub(current_pointer as i32) |
| 274 |
} else { |
| 275 |
abs_val.wrapping_add(translation_size) |
| 276 |
}; |
| 277 |
|
| 278 |
idata[pos + 1..pos + 5].copy_from_slice(&rel_val.to_le_bytes()); |
| 279 |
} |
| 280 |
|
| 281 |
processed = pos + 5; |
| 282 |
} |
| 283 |
|
| 284 |
Ok(idata) |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
pub fn decompress_next( |
| 289 |
&mut self, |
| 290 |
chunk: &[u8], |
| 291 |
output_len: usize, |
| 292 |
) -> Result<&[u8], DecompressError> { |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
let mut bitstream = Bitstream::new(chunk); |
| 307 |
|
| 308 |
self.try_read_first_chunk(&mut bitstream)?; |
| 309 |
|
| 310 |
let mut decoded_len = 0; |
| 311 |
while decoded_len != output_len { |
| 312 |
if self.current_block.remaining == 0 { |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
if matches!(self.current_block.kind, BlockKind::Uncompressed { .. }) |
| 317 |
&& self.current_block.size % 2 != 0 |
| 318 |
{ |
| 319 |
bitstream.read_byte(); |
| 320 |
} |
| 321 |
self.current_block = Block::read(&mut bitstream, &mut self.state)?; |
| 322 |
assert_ne!(self.current_block.remaining, 0); |
| 323 |
} |
| 324 |
|
| 325 |
let decoded = self |
| 326 |
.current_block |
| 327 |
.decode_element(&mut bitstream, &mut self.r)?; |
| 328 |
|
| 329 |
let advance = match decoded { |
| 330 |
Decoded::Single(value) => { |
| 331 |
self.window.push(value); |
| 332 |
1 |
| 333 |
} |
| 334 |
Decoded::Match { offset, length } => { |
| 335 |
self.window.copy_from_self(offset, length); |
| 336 |
length |
| 337 |
} |
| 338 |
Decoded::Read(length) => { |
| 339 |
|
| 340 |
let length = usize::min(bitstream.remaining_bytes(), length); |
| 341 |
|
| 342 |
self.window.copy_from_bitstream(&mut bitstream, length)?; |
| 343 |
length |
| 344 |
} |
| 345 |
}; |
| 346 |
|
| 347 |
if advance == 0 { |
| 348 |
return Err(DecodeFailed::UnexpectedEof.into()); |
| 349 |
} |
| 350 |
|
| 351 |
decoded_len += advance; |
| 352 |
if let Some(value) = self.current_block.remaining.checked_sub(advance as u32) { |
| 353 |
self.current_block.remaining = value; |
| 354 |
} else { |
| 355 |
return Err(DecodeFailed::OverreadBlock.into()); |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
let chunk_offset = self.chunk_offset; |
| 360 |
self.chunk_offset += decoded_len; |
| 361 |
|
| 362 |
let view = self.window.past_view(decoded_len)?; |
| 363 |
if let Some(postprocess) = self.postprocess.as_mut() { |
| 364 |
|
| 365 |
|
| 366 |
if chunk_offset >= 0x4000_0000 || decoded_len <= 10 { |
| 367 |
Ok(view) |
| 368 |
} else { |
| 369 |
let postprocess_buf = &mut postprocess.data_chunk[..decoded_len]; |
| 370 |
postprocess_buf.copy_from_slice(view); |
| 371 |
|
| 372 |
|
| 373 |
let view = Self::postprocess( |
| 374 |
postprocess.e8_translation_size, |
| 375 |
chunk_offset, |
| 376 |
postprocess_buf, |
| 377 |
)?; |
| 378 |
Ok(view) |
| 379 |
} |
| 380 |
} else { |
| 381 |
Ok(view) |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
pub fn reset(&mut self) { |
| 390 |
let this = Self::new(self.state.window_size); |
| 391 |
let _ = mem::replace(self, this); |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
#[cfg(test)] |
| 396 |
mod tests { |
| 397 |
use super::*; |
| 398 |
|
| 399 |
#[test] |
| 400 |
fn check_uncompressed() { |
| 401 |
let data = [ |
| 402 |
0x00, 0x30, 0x30, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, |
| 403 |
0x00, 0x00, b'a', b'b', b'c', 0x00, |
| 404 |
]; |
| 405 |
|
| 406 |
let mut lzxd = Lzxd::new(WindowSize::KB32); |
| 407 |
let res = lzxd.decompress_next(&data, 3); |
| 408 |
assert_eq!(res.unwrap(), [b'a', b'b', b'c']); |
| 409 |
} |
| 410 |
|
| 411 |
#[test] |
| 412 |
fn reset() { |
| 413 |
let data = [ |
| 414 |
0x00, 0x30, 0x30, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, |
| 415 |
0x00, 0x00, b'a', b'b', b'c', 0x00, |
| 416 |
]; |
| 417 |
|
| 418 |
let mut lzxd = Lzxd::new(WindowSize::KB32); |
| 419 |
let res = lzxd.decompress_next(&data, 3); |
| 420 |
assert_eq!(res.unwrap(), [b'a', b'b', b'c']); |
| 421 |
|
| 422 |
lzxd.reset(); |
| 423 |
let res = lzxd.decompress_next(&data, 3); |
| 424 |
assert_eq!(res.unwrap(), [b'a', b'b', b'c']); |
| 425 |
} |
| 426 |
|
| 427 |
#[test] |
| 428 |
fn check_e8() { |
| 429 |
let data = [ |
| 430 |
0x5B, 0x80, 0x80, 0x8D, 0x00, 0x30, 0x80, 0x0A, 0x18, 0x00, 0x00, 0x00, 0x01, 0x00, |
| 431 |
0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x54, 0x68, 0x69, 0x73, 0x20, 0x66, 0x69, 0x6C, |
| 432 |
0x65, 0x20, 0x68, 0x61, 0x73, 0x20, 0x61, 0x6E, 0x20, 0x45, 0x38, 0x20, 0x62, 0x79, |
| 433 |
0x74, 0x65, 0x20, 0x74, 0x6F, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x45, 0x38, 0x20, |
| 434 |
0x74, 0x72, 0x61, 0x6E, 0x73, 0x6C, 0x61, 0x74, 0x69, 0x6F, 0x6E, 0x2C, 0x20, 0x58, |
| 435 |
0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, |
| 436 |
0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, |
| 437 |
0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, |
| 438 |
0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, |
| 439 |
0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, |
| 440 |
0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, |
| 441 |
0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0xE8, 0x7B, |
| 442 |
0x00, 0x00, 0x00, 0xE8, 0x7B, 0x00, 0x00, 0x00, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, |
| 443 |
0x64, 0x64, 0x64, 0x64, 0x64, 0x64, |
| 444 |
]; |
| 445 |
|
| 446 |
let mut lzxd = Lzxd::new(WindowSize::KB32); |
| 447 |
let res = lzxd.decompress_next(&data, 168); |
| 448 |
assert_eq!( |
| 449 |
res.unwrap(), |
| 450 |
b"This file has an E8 byte to test E8 translation, Xdddddddddddddddd\ |
| 451 |
dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd\ |
| 452 |
dddddddddddddd\xE8\xE9\xFF\xFF\xFF\xE8\xE4\xFF\xFF\xFFdddddddddddd" |
| 453 |
); |
| 454 |
} |
| 455 |
|
| 456 |
#[test] |
| 457 |
fn uncompressed_single_byte_data() { |
| 458 |
let mut lzxd = Lzxd::new(WindowSize::KB32); |
| 459 |
let res = lzxd.decompress_next(&[0x00], 32); |
| 460 |
assert_eq!(res.unwrap_err().0, DecodeFailed::UnexpectedEof); |
| 461 |
} |
| 462 |
|
| 463 |
#[test] |
| 464 |
fn uncompressed_advance_zero() { |
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
let data = vec![ |
| 469 |
0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, |
| 470 |
0x32, 0x32 |
| 471 |
]; |
| 472 |
|
| 473 |
let mut lzxd = Lzxd::new(WindowSize::KB32); |
| 474 |
let res = lzxd.decompress_next(&data, 32); |
| 475 |
assert_eq!(res.unwrap_err().0, DecodeFailed::UnexpectedEof); |
| 476 |
} |
| 477 |
|
| 478 |
#[test] |
| 479 |
fn peek_bits_oneword_bounds_check() { |
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
let data = vec![ |
| 484 |
0x12, 0x12, 0x12, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12, |
| 485 |
0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x00, 0x00, 0x00, 0x12, 0x8a, |
| 486 |
]; |
| 487 |
|
| 488 |
let mut lzxd = Lzxd::new(WindowSize::KB32); |
| 489 |
let res = lzxd.decompress_next(&data, 32); |
| 490 |
assert_eq!(res.unwrap_err().0, DecodeFailed::UnexpectedEof); |
| 491 |
} |
| 492 |
} |