aboutsummaryrefslogtreecommitdiff
path: root/matroska/src/read.rs
blob: c3d06fafe7dfc97ae84c57b2f4033458b2159a97 (plain)
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
    This file is part of jellything (https://codeberg.org/metamuffin/jellything)
    which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
    Copyright (C) 2025 metamuffin <metamuffin.org>
*/
use crate::{error::Error, matroska::MatroskaTag, size::EbmlSize, Master, Result};
use log::{debug, warn};
use std::{
    collections::VecDeque,
    io::{Read, Seek, SeekFrom},
};

trait ReadAndSeek: Read + Seek {}
impl<T: Read + Seek> ReadAndSeek for T {}

#[derive(Debug, Clone, Copy)]
pub struct StackTag {
    end: Option<u64>,
    id: u64,
}

pub struct EbmlReader {
    inner: Box<dyn ReadAndSeek>,
    stack: Vec<StackTag>,
    queue: VecDeque<(Option<u64>, MatroskaTag)>,
    position: u64,
}

impl Read for EbmlReader {
    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
        let r = self.inner.read(buf)?;
        self.position += r as u64;
        Ok(r)
    }
}

impl EbmlReader {
    pub fn new<T: Seek + Read + 'static>(inner: T) -> Self {
        Self {
            queue: VecDeque::new(),
            inner: Box::new(inner),
            stack: vec![],
            position: 0,
        }
    }

    #[inline]
    pub fn read_byte(&mut self) -> Result<u8> {
        let mut b = [0u8];
        self.inner.read_exact(&mut b).map_err(Error::Io)?;
        self.position += 1;
        Ok(b[0])
    }

    pub fn read_buf(&mut self, size: impl Into<usize>) -> Result<Vec<u8>> {
        let size = size.into();
        let mut b = vec![0u8; size];
        self.inner.read_exact(&mut b).map_err(Error::Io)?;
        self.position += size as u64;
        Ok(b)
    }

    pub fn read_vint_len(&mut self) -> Result<(u64, usize)> {
        let s = self.read_byte()?;
        let len = s.leading_zeros() + 1;
        if len > 8 {
            Err(Error::VarintTooLong)?
        }
        let mut value = s as u64;
        value -= 1 << (8 - len);
        for _ in 1..len {
            value <<= 8;
            value += self.read_byte()? as u64;
        }
        Ok((value, len as usize))
    }

    #[inline]
    pub fn read_vint(&mut self) -> Result<u64> {
        Ok(self.read_vint_len()?.0)
    }

    #[inline]
    pub fn read_utf8(&mut self, size: impl Into<usize>) -> Result<String> {
        let b = self.read_buf(size)?;
        String::from_utf8(b).map_err(|_| Error::InvalidUTF8)
    }

    #[inline]
    pub fn read_tag_id(&mut self) -> Result<u64> {
        let (value, len) = self.read_vint_len()?;
        Ok(value + (1 << (7 * len)))
    }

    #[inline]
    pub fn read_tag_size(&mut self) -> Result<EbmlSize> {
        Ok(EbmlSize::from_vint(self.read_vint_len()?))
    }

    /// reads *some* amount of tags from the stream and pushes it to the queue.
    pub fn read_stuff(&mut self) -> Result<()> {
        while let Some(e) = self.stack.last().copied() {
            if let Some(end) = e.end {
                if self.position >= end {
                    if self.position != end {
                        warn!("we missed the end by {} bytes", self.position - end)
                    }
                    self.stack.pop();
                    self.queue
                        .push_back((None, MatroskaTag::construct_master(e.id, Master::End)?));
                } else {
                    break;
                }
            } else {
                break;
            }
        }

        let start_position = self.position;
        let id = self.read_tag_id()?;
        let size = self.read_tag_size()?;
        let is_master = MatroskaTag::is_master(id)?;
        let tag = if is_master {
            MatroskaTag::construct_master(id, Master::Start)?
        } else {
            let data = self.read_buf(size.some().unwrap())?;
            MatroskaTag::parse(id, &data)?
        };

        if let Some(path) = tag.path() {
            // we have slightly different rules for closing tags implicitly
            // this closes as many tags as needed to make the next tag a valid child
            while let Some(stag @ StackTag { end: None, .. }) = self.stack.last() {
                if path.last() == Some(&stag.id) {
                    break;
                } else {
                    let end =
                        MatroskaTag::construct_master(self.stack.pop().unwrap().id, Master::End)?;
                    self.queue.push_back((None, end));
                }
            }
        }

        if is_master {
            self.stack.push(StackTag {
                end: size.some().map(|s| s as u64 + self.position),
                id,
            });
        }
        self.queue.push_back((Some(start_position), tag));
        Ok(())
    }

    /// context should be the next expected tag, such that the stack can be derived from its path.
    pub fn seek(&mut self, position: u64, context: MatroskaTag) -> Result<()> {
        let path = context.path().ok_or(Error::GlobalTagsAsContext)?;
        debug!(
            "seeking to {position} with a context restored from path {:x?}",
            path
        );
        self.queue.clear();
        self.position = position;
        self.inner.seek(SeekFrom::Start(position))?;
        self.stack = path
            .iter()
            .map(|id| StackTag { id: *id, end: None })
            .collect();
        Ok(())
    }
}

impl Iterator for EbmlReader {
    type Item = Result<(Option<u64>, MatroskaTag)>;
    fn next(&mut self) -> Option<Self::Item> {
        if let Some(t) = self.queue.pop_front() {
            // match t {
            //     MatroskaTag::SimpleBlock(_) | MatroskaTag::Block(_) => (),
            //     _ => debug!("reader yield: {t:?}"),
            // };
            Some(Ok(t))
        } else {
            match self.read_stuff() {
                Ok(()) => self.next(),
                // in case we reached the end (error: failed to fill whole buffer),
                // return the rest in the queue and pop all items of the stack
                Err(e) => {
                    // TODO this is horrible, should use a custom error enum instead
                    if format!("{e}").as_str() == "failed to fill whole buffer" {
                        match self.queue.pop_front() {
                            Some(q) => Some(Ok(q)),
                            None => match self.stack.pop() {
                                Some(q) => Some(Ok((
                                    None,
                                    MatroskaTag::construct_master(q.id, Master::End).unwrap(),
                                ))),
                                None => Some(Err(e)),
                            },
                        }
                    } else {
                        Some(Err(e))
                    }
                }
            }
        }
    }
}

pub trait ReadValue: Sized {
    fn from_buf(buf: &[u8]) -> Result<Self>;
}

impl ReadValue for u64 {
    fn from_buf(buf: &[u8]) -> Result<Self> {
        if buf.len() > 8 {
            Err(Error::InvalidTypeLen)?
        }
        let mut val = 0u64;
        for byte in buf {
            val <<= 8;
            val |= *byte as u64;
        }
        Ok(val)
    }
}
impl ReadValue for i64 {
    fn from_buf(buf: &[u8]) -> Result<Self> {
        if buf.len() > 8 {
            Err(Error::InvalidTypeLen)?
        }
        Ok(if buf[0] > 127 {
            if buf.len() == 8 {
                i64::from_be_bytes(buf.try_into().unwrap())
            } else {
                -((1 << (buf.len() * 8)) - (u64::from_buf(buf)? as i64))
            }
        } else {
            u64::from_buf(buf)? as i64
        })
    }
}
impl ReadValue for f64 {
    fn from_buf(buf: &[u8]) -> Result<Self> {
        Ok(if buf.len() == 4 {
            f32::from_be_bytes(buf.try_into().unwrap()) as f64
        } else if buf.len() == 8 {
            f64::from_be_bytes(buf.try_into().unwrap())
        } else {
            Err(Error::InvalidTypeLen)?
        })
    }
}

impl ReadValue for Vec<u8> {
    fn from_buf(buf: &[u8]) -> Result<Self> {
        Ok(buf.to_vec())
    }
}
impl ReadValue for String {
    fn from_buf(buf: &[u8]) -> Result<Self> {
        String::from_utf8(Vec::from(buf)).map_err(|_| Error::InvalidUTF8)
    }
}
impl ReadValue for Master {
    fn from_buf(_: &[u8]) -> Result<Self> {
        panic!("master shall not be read like this")
    }
}

pub trait ReadExt: Read {
    fn read_byte(&mut self) -> Result<u8>;
    fn read_vint_len(&mut self) -> Result<(u64, usize)>;
    fn read_vint(&mut self) -> Result<u64>;
}
impl<T: Read> ReadExt for T {
    fn read_byte(&mut self) -> Result<u8> {
        let mut b = [0u8];
        self.read_exact(&mut b).map_err(Error::Io)?;
        Ok(b[0])
    }
    fn read_vint_len(&mut self) -> Result<(u64, usize)> {
        let s = self.read_byte()?;
        let len = s.leading_zeros() + 1;
        if len > 8 {
            Err(Error::VarintTooLong)?
        }
        let mut value = s as u64;
        value -= 1 << (8 - len);
        for _ in 1..len {
            value <<= 8;
            value += self.read_byte()? as u64;
        }
        Ok((value, len as usize))
    }
    #[inline]
    fn read_vint(&mut self) -> Result<u64> {
        Ok(self.read_vint_len()?.0)
    }
}