aboutsummaryrefslogtreecommitdiff
path: root/matroska/src/read.rs
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2023-08-05 11:51:26 +0200
committermetamuffin <metamuffin@disroot.org>2023-08-05 11:51:26 +0200
commit7cba183debcf2cefd90b4c1e7a630fb0c2152d06 (patch)
tree4d662af700078a2b53df08371f31bba0ccd1a216 /matroska/src/read.rs
parent4a0f08126d80dc589e3c97bf0a07571b8b828a74 (diff)
downloadjellything-7cba183debcf2cefd90b4c1e7a630fb0c2152d06.tar
jellything-7cba183debcf2cefd90b4c1e7a630fb0c2152d06.tar.bz2
jellything-7cba183debcf2cefd90b4c1e7a630fb0c2152d06.tar.zst
(semi-)proper error handling in matroska
Diffstat (limited to 'matroska/src/read.rs')
-rw-r--r--matroska/src/read.rs45
1 files changed, 23 insertions, 22 deletions
diff --git a/matroska/src/read.rs b/matroska/src/read.rs
index e2bf15c..cb3e45e 100644
--- a/matroska/src/read.rs
+++ b/matroska/src/read.rs
@@ -3,8 +3,9 @@
which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
Copyright (C) 2023 metamuffin <metamuffin.org>
*/
+use crate::error::Error;
+use crate::Result;
use crate::{matroska::MatroskaTag, size::EbmlSize, unflatten::IterWithPos, Master};
-use anyhow::{anyhow, bail, Result};
use log::{debug, warn};
use std::{
collections::VecDeque,
@@ -47,14 +48,14 @@ impl EbmlReader {
pub fn read_byte(&mut self) -> Result<u8> {
let mut b = [0u8];
- self.inner.read_exact(&mut b)?;
+ 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)?;
+ self.inner.read_exact(&mut b).map_err(Error::Io)?;
self.position += size;
Ok(b)
}
@@ -62,7 +63,7 @@ impl EbmlReader {
let s = self.read_byte()?;
let len = s.leading_zeros() + 1;
if len > 8 {
- bail!("varint too long");
+ Err(Error::VarintTooLong)?
}
let mut value = s as u64;
value -= 1 << (8 - len);
@@ -77,7 +78,7 @@ impl EbmlReader {
}
pub fn read_utf8(&mut self, size: impl Into<usize>) -> Result<String> {
let b = self.read_buf(size)?;
- Ok(String::from_utf8(b)?)
+ Ok(String::from_utf8(b).map_err(|_| Error::InvalidUTF8)?)
}
pub fn read_tag_id(&mut self) -> Result<u64> {
let (value, len) = self.read_vint_len()?;
@@ -141,16 +142,16 @@ impl EbmlReader {
/// context should be the next expected tag, such that the stack can be derived from its path.
pub fn seek(&mut self, position: usize, context: MatroskaTag) -> Result<()> {
- let path = context
- .path()
- .ok_or(anyhow!("global tags dont give context"))?;
+ 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 as u64))?;
+ self.inner
+ .seek(SeekFrom::Start(position as u64))
+ .map_err(Error::Io)?;
self.stack = path
.iter()
.map(|id| StackTag { id: *id, end: None })
@@ -198,13 +199,13 @@ impl IterWithPos for EbmlReader {
}
pub trait ReadValue: Sized {
- fn from_buf(buf: &[u8]) -> anyhow::Result<Self>;
+ fn from_buf(buf: &[u8]) -> Result<Self>;
}
impl ReadValue for u64 {
- fn from_buf(buf: &[u8]) -> anyhow::Result<Self> {
+ fn from_buf(buf: &[u8]) -> Result<Self> {
if buf.len() > 8 {
- bail!("u64 too big")
+ Err(Error::InvalidTypeLen)?
}
let mut val = 0u64;
for byte in buf {
@@ -215,9 +216,9 @@ impl ReadValue for u64 {
}
}
impl ReadValue for i64 {
- fn from_buf(buf: &[u8]) -> anyhow::Result<Self> {
+ fn from_buf(buf: &[u8]) -> Result<Self> {
if buf.len() > 8 {
- bail!("i64 too big")
+ Err(Error::InvalidTypeLen)?
}
Ok(if buf[0] > 127 {
if buf.len() == 8 {
@@ -231,29 +232,29 @@ impl ReadValue for i64 {
}
}
impl ReadValue for f64 {
- fn from_buf(buf: &[u8]) -> anyhow::Result<Self> {
+ 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 {
- bail!("float is not 4 or 8 bytes long");
+ Err(Error::InvalidTypeLen)?
})
}
}
impl ReadValue for Vec<u8> {
- fn from_buf(buf: &[u8]) -> anyhow::Result<Self> {
+ fn from_buf(buf: &[u8]) -> Result<Self> {
Ok(buf.to_vec())
}
}
impl ReadValue for String {
- fn from_buf(buf: &[u8]) -> anyhow::Result<Self> {
- Ok(String::from_utf8(Vec::from(buf))?)
+ fn from_buf(buf: &[u8]) -> Result<Self> {
+ Ok(String::from_utf8(Vec::from(buf)).map_err(|_| Error::InvalidUTF8)?)
}
}
impl ReadValue for Master {
- fn from_buf(_: &[u8]) -> anyhow::Result<Self> {
+ fn from_buf(_: &[u8]) -> Result<Self> {
panic!("master shall not be read like this")
}
}
@@ -266,14 +267,14 @@ pub trait ReadExt: Read {
impl<T: Read> ReadExt for T {
fn read_byte(&mut self) -> Result<u8> {
let mut b = [0u8];
- self.read_exact(&mut b)?;
+ 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 {
- bail!("varint too long");
+ Err(Error::VarintTooLong)?
}
let mut value = s as u64;
value -= 1 << (8 - len);