wacore_binary/
util.rs

1use crate::error::{BinaryError, Result};
2use flate2::read::ZlibDecoder;
3use std::borrow::Cow;
4use std::io::Read;
5
6pub fn unpack(data: &[u8]) -> Result<Cow<'_, [u8]>> {
7    if data.is_empty() {
8        return Err(BinaryError::Eof);
9    }
10    let data_type = data[0];
11    let data = &data[1..];
12
13    if (data_type & 2) > 0 {
14        let mut decoder = ZlibDecoder::new(data);
15        let mut decompressed = Vec::new();
16        decoder
17            .read_to_end(&mut decompressed)
18            .map_err(|e| BinaryError::Zlib(e.to_string()))?;
19        Ok(Cow::Owned(decompressed))
20    } else {
21        Ok(Cow::Borrowed(data))
22    }
23}