Skip to main content

mkv_element/
supplement.rs

1use std::ops::Deref;
2
3use crate::base::VInt64;
4use crate::element::Element;
5
6use crate::*;
7
8/// Ebml Void element, used for padding.
9///
10/// ### Note:
11/// Every Master element contains an optional Void element at the end of its body, which is used for padding.
12/// This library automatically aggregates multiple Void elements into one at the end.
13/// * When reading, all Void elements at the same level will be counted as one, sizes are accumulated.
14/// * When writing, only one Void element will be written at the end, with size equal to the sum of all Void elements at the same level.
15#[derive(Debug, Clone, Copy, PartialEq, Eq)]
16pub struct Void {
17    /// Size of the void element in bytes.
18    pub size: u64,
19}
20impl Element for Void {
21    const ID: VInt64 = VInt64::from_encoded(0xEC);
22    fn decode_body<B: Buf>(buf: &mut B) -> crate::Result<Self> {
23        let len = buf.remaining();
24        buf.advance(len);
25        Ok(Self { size: len as u64 })
26    }
27    fn encode_body<B: BufMut>(&self, buf: &mut B) -> crate::Result<()> {
28        buf.put_slice(&vec![0; self.size as usize]);
29        Ok(())
30    }
31}
32
33/// CRC-32 element, used for integrity checking. The CRC-32 is stored as a little-endian u32.
34///
35/// ### Note:
36/// * This element can be included in any Master element to provide a CRC-32 checksum of the element's data.
37/// * It has to be the **first** element in the Master element's body if it is present.
38#[derive(Debug, Clone, Copy, PartialEq, Eq)]
39pub struct Crc32(pub u32);
40impl Deref for Crc32 {
41    type Target = u32;
42    fn deref(&self) -> &Self::Target {
43        &self.0
44    }
45}
46impl Element for Crc32 {
47    const ID: VInt64 = VInt64::from_encoded(0xBF);
48    fn decode_body<B: Buf>(buf: &mut B) -> crate::Result<Self> {
49        let buf = <[u8; 4]>::decode(buf)?;
50        Ok(Self(u32::from_le_bytes(buf)))
51    }
52    fn encode_body<B: BufMut>(&self, buf: &mut B) -> crate::Result<()> {
53        buf.put_slice(&self.0.to_le_bytes());
54        Ok(())
55    }
56}