diff --git a/src/demo/lzss.rs b/src/demo/lzss.rs index c8c8997..14b2a41 100644 --- a/src/demo/lzss.rs +++ b/src/demo/lzss.rs @@ -1,46 +1,59 @@ -pub fn decompress(input: &[u8], output: &mut Vec) { - decompress_(input, output); +use bitbuffer::{BitRead, LittleEndian}; + +use crate::{ParseError, Stream}; + +const LZSS_ERROR: ParseError = ParseError::InvalidDemo("invalid LZSS encoding"); + +#[derive(Debug)] +struct CopyInfo { + offset: u16, + count: u8, } -/// inner fn that returns an option so we can use ? for short circuiting return -fn decompress_(input: &[u8], output: &mut Vec) -> Option<()> { - let mut len_bytes = [0; 4]; - len_bytes.copy_from_slice(input.get(0..4)?); - let target_len = u32::from_le_bytes(len_bytes) as usize; +// can't derive this, since it uses big-endian encoding for the `offset` +impl BitRead<'_, LittleEndian> for CopyInfo { + fn read(stream: &mut Stream) -> bitbuffer::Result { + let bytes: [u8; 2] = stream.read()?; - let mut read_pos = 4; - let mut read_byte = move || { - let byte = *input.get(read_pos)?; - read_pos += 1; - Some(byte) - }; + Ok(CopyInfo { + offset: (bytes[0] as u16) << 4 | (bytes[1] as u16 >> 4), + count: (bytes[1] & 0x0F) + 1, + }) + } +} - loop { - let mut cmd_byte = read_byte()?; +pub fn decompress(input: &mut Stream, output: &mut Vec) -> Result<(), ParseError> { + let target_len = input.read::()? as usize; + + while output.len() < target_len { + let mut cmd_byte: u8 = input.read()?; for _ in 0..8 { if cmd_byte & 0x01 == 0x01 { - let pos = (read_byte()? as usize) << 4; - let mixed = read_byte()? as usize; - let pos = pos | (mixed >> 4); - let count = (mixed & 0x0F) + 1; - if count == 1 { - return None; + let info: CopyInfo = input.read()?; + + if info.count == 1 { + break; } - if output.len() + count > target_len { - return None; + if output.len() + info.count as usize > target_len { + return Err(LZSS_ERROR); } - let start = output.len() - pos - 1; + let start = output + .len() + .checked_sub(info.offset as usize + 1) + .ok_or(LZSS_ERROR)?; // can't do extend_from_within since it start + count can be larger than output.len - for i in 0..count { - output.push(*output.get(start + i)?); + for i in 0..info.count { + output.push(*output.get(start + i as usize).ok_or(LZSS_ERROR)?); } } else { - output.push(read_byte()?); + output.push(input.read()?); } cmd_byte >>= 1; } } + + Ok(()) } diff --git a/src/demo/message/stringtable.rs b/src/demo/message/stringtable.rs index 0f41305..6bd2cca 100644 --- a/src/demo/message/stringtable.rs +++ b/src/demo/message/stringtable.rs @@ -101,9 +101,10 @@ impl<'a> Parse<'a> for CreateStringTableMessage<'a> { table_data = BitReadStream::new(buffer); } b"LZSS" => { - let compressed_data = table_data.read_bytes(compressed_size as usize - 4)?; + let mut compressed_data = + table_data.read_bits((compressed_size as usize - 4) * 8)?; let mut decompressed_data = Vec::with_capacity(decompressed_size as usize); - decompress(&compressed_data, &mut decompressed_data); + decompress(&mut compressed_data, &mut decompressed_data)?; if decompressed_data.len() != decompressed_size as usize { return Err(ParseError::UnexpectedDecompressedSize {