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:
parent
329293b547
commit
541e175cdd
2 changed files with 43 additions and 29 deletions
|
|
@ -1,46 +1,59 @@
|
|||
pub fn decompress(input: &[u8], output: &mut Vec<u8>) {
|
||||
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<u8>) -> 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<Self> {
|
||||
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<u8>) -> Result<(), ParseError> {
|
||||
let target_len = input.read::<u32>()? 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(())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue