xz_rs/block/filter/
mod.rs1use crate::{
2 decode::{Decode, DecodeError},
3 encode::Encode,
4};
5
6use super::{BlockDecodeError, VarLengthInt};
7
8#[derive(Debug, Clone)]
9pub struct Filter {
10 pub id: u64,
11 pub properties: Vec<u8>,
12}
13
14impl Encode for Filter {
15 fn encoding(&self) -> Vec<u8> {
16 let mut bytes = Vec::new();
17
18 bytes.extend_from_slice(&VarLengthInt(self.id).encoding());
19 bytes.extend_from_slice(&VarLengthInt(self.properties.len() as u64).encoding());
20 bytes.extend_from_slice(&self.properties);
21
22 bytes
23 }
24}
25
26impl Decode for Filter {
27 fn decode<R: std::io::Read>(src: &mut R) -> Result<Self, DecodeError> {
28 let err = Err(DecodeError::BlockError(BlockDecodeError::InvalidHeader));
29 let id = VarLengthInt::decode(src)?.0;
30
31 if id > 0x4000_0000_0000_0000 {
32 return err;
33 }
34
35 let properties_size = VarLengthInt::decode(src)?.0;
36
37 if properties_size > 1024 {
38 return err;
39 }
40
41 let mut properties = vec![0u8; properties_size as usize];
42 src.read_exact(&mut properties)?;
43
44 Ok(Self { id, properties })
45 }
46}