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
use std::io::{Cursor, Error, ErrorKind, Read, Result};
enum Endianness {
Big,
Little,
}
/// A reader for parsing binary data of MP4 containers with support for big-endian and little-endian formats.
pub struct Reader {
endian: Endianness,
inner: Cursor<Vec<u8>>,
}
impl Reader {
/// Creates a new big-endian `Reader` for the given data.
pub fn new_big_endian(data: &[u8]) -> Self {
Self {
endian: Endianness::Big,
inner: Cursor::new(data.to_vec()),
}
}
/// Creates a new little-endian `Reader` for the given data.
pub fn new_little_endian(data: &[u8]) -> Self {
Self {
endian: Endianness::Little,
inner: Cursor::new(data.to_vec()),
}
}
/// Returns a slice referencing the underlying data.
pub fn as_bytes(&self) -> &[u8] {
self.inner.get_ref()
}
/// Returns `true` if there is more data to be read.
pub fn has_more_data(&self) -> bool {
self.inner.position() < (self.inner.get_ref().len() as u64)
}
/// Returns the total length of the data in bytes.
pub fn get_length(&self) -> u64 {
self.inner.get_ref().len() as u64
}
/// Returns the current read position.
pub fn get_position(&self) -> u64 {
self.inner.position()
}
/// Skips the specified number of bytes.
///
/// # Errors
///
/// Returns an error if the new position exceeds the total data length.
pub fn skip(&mut self, bytes: u64) -> Result<()> {
let position = self.get_position() + bytes;
if position > self.get_length() {
return Err(Error::new(
ErrorKind::OutOfMemory,
"skips out of memory bounds.",
));
}
self.inner.set_position(position);
Ok(())
}
/// Reads a 8-bit unsigned integer.
///
/// # Errors
///
/// Returns an error if there is not enough data left to read.
pub fn read_u8(&mut self) -> Result<u8> {
let mut buf = [0; 1];
self.inner.read_exact(&mut buf)?;
Ok(buf[0])
}
/// Reads a 16-bit unsigned integer according to the configured endianness.
///
/// # Errors
///
/// Returns an error if there is not enough data left to read.
pub fn read_u16(&mut self) -> Result<u16> {
let mut buf = [0; 2];
self.inner.read_exact(&mut buf)?;
match self.endian {
Endianness::Big => Ok(u16::from_be_bytes(buf)),
Endianness::Little => Ok(u16::from_le_bytes(buf)),
}
}
/// Reads a 32-bit unsigned integer according to the configured endianness.
///
/// # Errors
///
/// Returns an error if there is not enough data left to read.
pub fn read_u32(&mut self) -> Result<u32> {
let mut buf = [0; 4];
self.inner.read_exact(&mut buf)?;
match self.endian {
Endianness::Big => Ok(u32::from_be_bytes(buf)),
Endianness::Little => Ok(u32::from_le_bytes(buf)),
}
}
/// Reads a 64-bit unsigned integer according to the configured endianness.
///
/// # Errors
///
/// Returns an error if there is not enough data left to read.
pub fn read_u64(&mut self) -> Result<u64> {
let mut buf = [0; 8];
self.inner.read_exact(&mut buf)?;
match self.endian {
Endianness::Big => Ok(u64::from_be_bytes(buf)),
Endianness::Little => Ok(u64::from_le_bytes(buf)),
}
}
/// Reads the specified number of bytes into a vector.
///
/// # Errors
///
/// Returns an error if there is not enough data left to read.
pub fn read_bytes_u8(&mut self, bytes: usize) -> Result<Vec<u8>> {
let mut buf = vec![0; bytes];
self.inner.read_exact(&mut buf)?;
Ok(buf)
}
/// Reads 16-bit unsigned integers from the configured number of bytes.
///
/// # Errors
///
/// Returns an error if there is not enough data left to read.
pub fn read_bytes_u16(&mut self, bytes: usize) -> Result<Vec<u16>> {
Ok(self
.read_bytes_u8(bytes)?
.chunks_exact(2)
.map(|x| match self.endian {
Endianness::Big => u16::from_be_bytes([x[0], x[1]]),
Endianness::Little => u16::from_le_bytes([x[0], x[1]]),
})
.collect())
}
/// Reads a 32-bit signed integer according to the configured endianness.
///
/// # Errors
///
/// Returns an error if there is not enough data left to read.
pub fn read_i32(&mut self) -> Result<i32> {
let mut buf = [0; 4];
self.inner.read_exact(&mut buf)?;
match self.endian {
Endianness::Big => Ok(i32::from_be_bytes(buf)),
Endianness::Little => Ok(i32::from_le_bytes(buf)),
}
}
}