mp4_atom/
coding.rs

1use std::ffi;
2
3use crate::*;
4
5/// Decode a type from a buffer.
6pub trait Decode: Sized {
7    /// Decode the type from the buffer.
8    fn decode<B: Buf>(buf: &mut B) -> Result<Self>;
9
10    /// Helper: Decode exactly size bytes from the buffer.
11    fn decode_exact<B: Buf>(buf: &mut B, size: usize) -> Result<Self> {
12        if buf.remaining() < size {
13            return Err(Error::OutOfBounds);
14        }
15
16        let mut inner = buf.slice(size);
17        let res = Self::decode(&mut inner)?;
18
19        if inner.has_remaining() {
20            return Err(Error::ShortRead);
21        }
22
23        buf.advance(size);
24
25        Ok(res)
26    }
27}
28
29/// Decode a type from a buffer if we have enough data.
30pub trait DecodeMaybe: Sized {
31    fn decode_maybe<B: Buf>(buf: &mut B) -> Result<Option<Self>>;
32}
33
34/// Decode an atom using the provided header
35pub trait DecodeAtom: Sized {
36    fn decode_atom<B: Buf>(header: &Header, buf: &mut B) -> Result<Self>;
37}
38
39/// Encode a type to a buffer.
40//
41// Why not BufMut?
42// Well it's because we need to write the size of each atom.
43// If we use BufMut, we can't seek backwards so we have to calculate it upfront.
44// If we use BufMut or Vec, then we can write 0 for the size, then write the atom, then go back and fix the size.
45pub trait Encode {
46    fn encode<B: BufMut>(&self, buf: &mut B) -> Result<()>;
47}
48
49impl Decode for u8 {
50    fn decode<B: Buf>(buf: &mut B) -> Result<Self> {
51        Ok(Self::from_be_bytes(<[u8; 1]>::decode(buf)?))
52    }
53}
54
55impl Decode for i8 {
56    fn decode<B: Buf>(buf: &mut B) -> Result<Self> {
57        Ok(Self::from_be_bytes(<[u8; 1]>::decode(buf)?))
58    }
59}
60
61impl Decode for u16 {
62    fn decode<B: Buf>(buf: &mut B) -> Result<Self> {
63        Ok(Self::from_be_bytes(<[u8; 2]>::decode(buf)?))
64    }
65}
66
67impl Decode for i16 {
68    fn decode<B: Buf>(buf: &mut B) -> Result<Self> {
69        Ok(Self::from_be_bytes(<[u8; 2]>::decode(buf)?))
70    }
71}
72
73impl Decode for u32 {
74    fn decode<B: Buf>(buf: &mut B) -> Result<Self> {
75        Ok(Self::from_be_bytes(<[u8; 4]>::decode(buf)?))
76    }
77}
78
79impl Decode for i32 {
80    fn decode<B: Buf>(buf: &mut B) -> Result<Self> {
81        Ok(Self::from_be_bytes(<[u8; 4]>::decode(buf)?))
82    }
83}
84
85impl Decode for u64 {
86    fn decode<B: Buf>(buf: &mut B) -> Result<Self> {
87        Ok(Self::from_be_bytes(<[u8; 8]>::decode(buf)?))
88    }
89}
90
91impl Decode for i64 {
92    fn decode<B: Buf>(buf: &mut B) -> Result<Self> {
93        Ok(Self::from_be_bytes(<[u8; 8]>::decode(buf)?))
94    }
95}
96
97impl<const N: usize> Decode for [u8; N] {
98    fn decode<B: Buf>(buf: &mut B) -> Result<Self> {
99        if buf.remaining() < N {
100            return Err(Error::OutOfBounds);
101        }
102
103        let mut v = [0u8; N];
104        v.copy_from_slice(buf.slice(N));
105        buf.advance(N);
106
107        Ok(v)
108    }
109}
110
111impl<T: Decode> Decode for Vec<T> {
112    fn decode<B: Buf>(buf: &mut B) -> Result<Self> {
113        let mut vec = Vec::new();
114        while buf.has_remaining() {
115            let item = T::decode(buf)?;
116            vec.push(item);
117        }
118
119        Ok(vec)
120    }
121}
122
123impl Decode for String {
124    fn decode<B: Buf>(buf: &mut B) -> Result<Self> {
125        let mut bytes = Vec::new();
126        while buf.has_remaining() {
127            let byte = u8::decode(buf)?;
128            if byte == 0 {
129                break;
130            }
131
132            bytes.push(byte);
133        }
134
135        let str = ffi::CString::new(bytes).map_err(|err| Error::InvalidString(err.to_string()))?;
136        str.into_string()
137            .map_err(|err| Error::InvalidString(err.to_string()))
138    }
139}
140
141impl Encode for u8 {
142    fn encode<B: BufMut>(&self, buf: &mut B) -> Result<()> {
143        self.to_be_bytes().encode(buf)
144    }
145}
146
147impl Encode for i8 {
148    fn encode<B: BufMut>(&self, buf: &mut B) -> Result<()> {
149        self.to_be_bytes().encode(buf)
150    }
151}
152
153impl Encode for i16 {
154    fn encode<B: BufMut>(&self, buf: &mut B) -> Result<()> {
155        self.to_be_bytes().encode(buf)
156    }
157}
158
159impl Encode for u16 {
160    fn encode<B: BufMut>(&self, buf: &mut B) -> Result<()> {
161        self.to_be_bytes().encode(buf)
162    }
163}
164
165impl Encode for u32 {
166    fn encode<B: BufMut>(&self, buf: &mut B) -> Result<()> {
167        self.to_be_bytes().encode(buf)
168    }
169}
170
171impl Encode for i32 {
172    fn encode<B: BufMut>(&self, buf: &mut B) -> Result<()> {
173        self.to_be_bytes().encode(buf)
174    }
175}
176
177impl Encode for u64 {
178    fn encode<B: BufMut>(&self, buf: &mut B) -> Result<()> {
179        self.to_be_bytes().encode(buf)
180    }
181}
182
183impl Encode for i64 {
184    fn encode<B: BufMut>(&self, buf: &mut B) -> Result<()> {
185        self.to_be_bytes().encode(buf)
186    }
187}
188
189impl<const N: usize> Encode for [u8; N] {
190    fn encode<B: BufMut>(&self, buf: &mut B) -> Result<()> {
191        buf.append_slice(self);
192        Ok(())
193    }
194}
195
196impl<T: Encode> Encode for &[T] {
197    fn encode<B: BufMut>(&self, buf: &mut B) -> Result<()> {
198        for item in self.iter() {
199            item.encode(buf)?;
200        }
201
202        Ok(())
203    }
204}
205
206impl<T: Encode> Encode for Option<T> {
207    fn encode<B: BufMut>(&self, buf: &mut B) -> Result<()> {
208        match self {
209            Some(v) => v.encode(buf),
210            None => Ok(()),
211        }
212    }
213}
214
215impl Encode for &str {
216    fn encode<B: BufMut>(&self, buf: &mut B) -> Result<()> {
217        self.as_bytes().encode(buf)?;
218        0u8.encode(buf)?;
219        Ok(())
220    }
221}
222
223impl<T: Encode> Encode for Vec<T> {
224    fn encode<B: BufMut>(&self, buf: &mut B) -> Result<()> {
225        for item in self.iter() {
226            item.encode(buf)?;
227        }
228
229        Ok(())
230    }
231}