1
0
Fork 0
mirror of https://codeberg.org/demostf/parser.git synced 2026-11-30 10:40:50 +01:00

cleanup LZSS decoding and remove the need to copy the compressed data

This commit is contained in:
Robin Appelman 2026-11-01 17:35:16 +01:00
commit 541e175cdd
Signed by: robin
SSH key fingerprint: SHA256:kifqgrqnut7q1YUIrI0AdMNC23qiKAS/T9uGFmIYvbE
2 changed files with 43 additions and 29 deletions

View file

@ -1,46 +1,59 @@
pub fn decompress(input: &[u8], output: &mut Vec<u8>) { use bitbuffer::{BitRead, LittleEndian};
decompress_(input, output);
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 // can't derive this, since it uses big-endian encoding for the `offset`
fn decompress_(input: &[u8], output: &mut Vec<u8>) -> Option<()> { impl BitRead<'_, LittleEndian> for CopyInfo {
let mut len_bytes = [0; 4]; fn read(stream: &mut Stream) -> bitbuffer::Result<Self> {
len_bytes.copy_from_slice(input.get(0..4)?); let bytes: [u8; 2] = stream.read()?;
let target_len = u32::from_le_bytes(len_bytes) as usize;
let mut read_pos = 4; Ok(CopyInfo {
let mut read_byte = move || { offset: (bytes[0] as u16) << 4 | (bytes[1] as u16 >> 4),
let byte = *input.get(read_pos)?; count: (bytes[1] & 0x0F) + 1,
read_pos += 1; })
Some(byte) }
}; }
loop { pub fn decompress(input: &mut Stream, output: &mut Vec<u8>) -> Result<(), ParseError> {
let mut cmd_byte = read_byte()?; let target_len = input.read::<u32>()? as usize;
while output.len() < target_len {
let mut cmd_byte: u8 = input.read()?;
for _ in 0..8 { for _ in 0..8 {
if cmd_byte & 0x01 == 0x01 { if cmd_byte & 0x01 == 0x01 {
let pos = (read_byte()? as usize) << 4; let info: CopyInfo = input.read()?;
let mixed = read_byte()? as usize;
let pos = pos | (mixed >> 4); if info.count == 1 {
let count = (mixed & 0x0F) + 1; break;
if count == 1 {
return None;
} }
if output.len() + count > target_len { if output.len() + info.count as usize > target_len {
return None; 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 // can't do extend_from_within since it start + count can be larger than output.len
for i in 0..count { for i in 0..info.count {
output.push(*output.get(start + i)?); output.push(*output.get(start + i as usize).ok_or(LZSS_ERROR)?);
} }
} else { } else {
output.push(read_byte()?); output.push(input.read()?);
} }
cmd_byte >>= 1; cmd_byte >>= 1;
} }
} }
Ok(())
} }

View file

@ -101,9 +101,10 @@ impl<'a> Parse<'a> for CreateStringTableMessage<'a> {
table_data = BitReadStream::new(buffer); table_data = BitReadStream::new(buffer);
} }
b"LZSS" => { 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); 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 { if decompressed_data.len() != decompressed_size as usize {
return Err(ParseError::UnexpectedDecompressedSize { return Err(ParseError::UnexpectedDecompressedSize {