mkv_element/
supplement.rs

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