mp4san/parse/
array.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#![allow(missing_docs)]

use std::fmt::Debug;
use std::marker::PhantomData;

use bytes::{Buf, BufMut, BytesMut};
use derive_where::derive_where;
use mediasan_common::error::WhileParsingType;
use mediasan_common::ResultExt;

use crate::error::Result;

use super::{Mp4Prim, Mp4Value, Mp4ValueWriterExt, ParseError};

#[derive(Default, PartialEq, Eq)]
#[derive_where(Clone, Debug; C)]
pub struct BoundedArray<C, T> {
    entry_count: C,
    array: UnboundedArray<T>,
}

#[derive(Default, PartialEq, Eq)]
#[derive_where(Clone, Debug)]
pub struct UnboundedArray<T> {
    entries: BytesMut,
    _t: PhantomData<T>,
}

#[derive(Clone, Copy, Default, Debug, PartialEq, Eq)]
pub struct ArrayEntry<'a, T> {
    data: &'a [u8],
    _t: PhantomData<T>,
}

#[derive(Default, Debug, PartialEq, Eq)]
pub struct ArrayEntryMut<'a, T> {
    data: &'a mut [u8],
    _t: PhantomData<T>,
}

//
// BoundedArray impls
//

impl<C: Clone, T: Mp4Prim> BoundedArray<C, T> {
    pub fn entries(&self) -> impl Iterator<Item = ArrayEntry<'_, T>> + ExactSizeIterator + '_ {
        self.array.entries()
    }

    pub fn entries_mut(&mut self) -> impl Iterator<Item = ArrayEntryMut<'_, T>> + ExactSizeIterator + '_ {
        self.array.entries_mut()
    }

    pub fn entry_count(&self) -> C {
        self.entry_count.clone()
    }
}

impl<C: Mp4Prim + Into<u32> + Clone, T: Mp4Prim> Mp4Value for BoundedArray<C, T> {
    fn parse(buf: &mut BytesMut) -> Result<Self, ParseError> {
        let entry_count = C::parse(&mut *buf).while_parsing_type()?;
        let entries_len = (T::encoded_len() as u32)
            .checked_mul(entry_count.clone().into())
            .ok_or_else(|| report_attach!(ParseError::InvalidInput, "overflow", WhileParsingType::new::<Self>()))?;
        ensure_attach!(
            buf.remaining() as u32 >= entries_len,
            ParseError::TruncatedBox,
            WhileParsingType::new::<Self>(),
        );
        let mut array_bytes = buf.split_to(entries_len as usize);
        let array = UnboundedArray::parse(&mut array_bytes)?;
        Ok(Self { entry_count, array })
    }

    fn encoded_len(&self) -> u64 {
        C::encoded_len() + self.array.encoded_len()
    }

    fn put_buf<B: BufMut>(&self, mut buf: B) {
        buf.put_mp4_value(&self.entry_count);
        buf.put_slice(&self.array.entries);
    }
}

impl<C: From<u32>, T: Mp4Prim> FromIterator<T> for BoundedArray<C, T> {
    fn from_iter<I: IntoIterator<Item = T>>(entries: I) -> Self {
        let array = UnboundedArray::from_iter(entries);
        Self { entry_count: (array.entry_count() as u32).into(), array }
    }
}

//
// UnboundedArray impls
//

impl<T: Mp4Prim> UnboundedArray<T> {
    pub fn entries(&self) -> impl Iterator<Item = ArrayEntry<'_, T>> + ExactSizeIterator + '_ {
        self.entries
            .chunks_exact(T::encoded_len() as usize)
            .map(|data| ArrayEntry { data, _t: PhantomData })
    }

    pub fn entries_mut(&mut self) -> impl Iterator<Item = ArrayEntryMut<'_, T>> + ExactSizeIterator + '_ {
        self.entries
            .chunks_exact_mut(T::encoded_len() as usize)
            .map(|data| ArrayEntryMut { data, _t: PhantomData })
    }

    pub fn entry_count(&self) -> usize {
        self.entries.len() / T::encoded_len() as usize
    }
}

impl<T: Mp4Prim> Mp4Value for UnboundedArray<T> {
    fn parse(buf: &mut BytesMut) -> Result<Self, ParseError> {
        let entries = buf.split();
        Ok(Self { entries, _t: PhantomData })
    }

    fn encoded_len(&self) -> u64 {
        self.entries.len() as u64
    }

    fn put_buf<B: BufMut>(&self, mut buf: B) {
        buf.put_slice(&self.entries[..])
    }
}

impl<T: Mp4Prim> FromIterator<T> for UnboundedArray<T> {
    fn from_iter<I: IntoIterator<Item = T>>(entries: I) -> Self {
        let mut entries_bytes = BytesMut::new();
        for entry in entries {
            entry.put_buf(&mut entries_bytes);
        }
        Self { entries: entries_bytes, _t: PhantomData }
    }
}

//
// ArrayEntry impls
//

impl<T: Mp4Prim> ArrayEntry<'_, T> {
    pub fn get(&self) -> Result<T, ParseError> {
        T::parse(self.data)
    }
}

//
// ArrayEntryMut impls
//

impl<T: Mp4Prim> ArrayEntryMut<'_, T> {
    pub fn get(&self) -> Result<T, ParseError> {
        T::parse(&*self.data)
    }

    pub fn set(&mut self, value: T) {
        self.data.put_mp4_value(&value)
    }
}