Use nom's verbose error in deser so we can add context

This commit is contained in:
QuantumEntangledAndy committed 2021-11-27 13:32:09 +07:00
1 parent 0145983198
commit 0f40722e33
2 files changed
+50 -25

No files matched your search

+1 -1
View File
@@ -17,7 +17,7 @@ lazy_static = "1.4"
local-ip-address = "0.4.4"
log = { version = "0.4" }
md5 = "0.7"
nom = "6.1.2"
nom = { version = "6.1.2", features = ["alloc"] }
rand = "0.8.4"
regex = "1.5.4"
socket2 = "0.3"
+49 -24
View File
@@ -2,11 +2,12 @@ use super::model::BcMediaIframe;
use super::model::*;
use crate::RX_TIMEOUT;
use err_derive::Error;
use nom::IResult;
use nom::{combinator::*, named, number::streaming::*, take, take_str};
use nom::{combinator::*, error::context, number::streaming::*, take};
use std::io::Read;
use time::OffsetDateTime;
type IResult<I, O, E = nom::error::VerboseError<I>> = Result<(I, O), nom::Err<E>>;
// PAD_SIZE: Media packets use 8 byte padding
const PAD_SIZE: u32 = 8;
@@ -20,13 +21,13 @@ pub enum Error {
#[error(display = "I/O error")]
IoError(#[error(source)] std::io::Error),
}
type NomErrorType<'a> = nom::error::Error<&'a [u8]>;
type NomErrorType<'a> = nom::error::VerboseError<&'a [u8]>;
impl<'a> From<nom::Err<NomErrorType<'a>>> for Error {
fn from(k: nom::Err<NomErrorType<'a>>) -> Self {
let reason = match k {
nom::Err::Error(e) => format!("Nom Error: {:?}", e),
nom::Err::Failure(e) => format!("Nom Error: {:?}", e),
nom::Err::Error(e) => format!("Nom Error: {:x?}", e),
nom::Err::Failure(e) => format!("Nom Error: {:x?}", e),
_ => "Unknown Nom error".to_string(),
};
Error::NomError(reason)
@@ -37,7 +38,7 @@ fn read_from_reader<P, O, E, R>(mut parser: P, mut rdr: R) -> Result<O, E>
where
R: Read,
E: for<'a> From<nom::Err<NomErrorType<'a>>> + From<std::io::Error>,
P: FnMut(&[u8]) -> nom::IResult<&[u8], O>,
P: FnMut(&[u8]) -> IResult<&[u8], O>,
{
let mut input: Vec<u8> = Vec::new();
loop {
@@ -91,17 +92,20 @@ impl BcMedia {
}
fn bcmedia(buf: &[u8]) -> IResult<&[u8], BcMedia> {
let (buf, magic) = verify(le_u32, |x| {
matches!(
*x,
MAGIC_HEADER_BCMEDIA_INFO_V1
| MAGIC_HEADER_BCMEDIA_INFO_V2
| MAGIC_HEADER_BCMEDIA_IFRAME..=MAGIC_HEADER_BCMEDIA_IFRAME_LAST
| MAGIC_HEADER_BCMEDIA_PFRAME..=MAGIC_HEADER_BCMEDIA_PFRAME_LAST
| MAGIC_HEADER_BCMEDIA_AAC
| MAGIC_HEADER_BCMEDIA_ADPCM
)
})(buf)?;
let (buf, magic) = context(
"Failed to match any known bcmedia",
verify(le_u32, |x| {
matches!(
*x,
MAGIC_HEADER_BCMEDIA_INFO_V1
| MAGIC_HEADER_BCMEDIA_INFO_V2
| MAGIC_HEADER_BCMEDIA_IFRAME..=MAGIC_HEADER_BCMEDIA_IFRAME_LAST
| MAGIC_HEADER_BCMEDIA_PFRAME..=MAGIC_HEADER_BCMEDIA_PFRAME_LAST
| MAGIC_HEADER_BCMEDIA_AAC
| MAGIC_HEADER_BCMEDIA_ADPCM
)
}),
)(buf)?;
match magic {
MAGIC_HEADER_BCMEDIA_INFO_V1 => {
@@ -133,7 +137,10 @@ fn bcmedia(buf: &[u8]) -> IResult<&[u8], BcMedia> {
}
fn bcmedia_info_v1(buf: &[u8]) -> IResult<&[u8], BcMediaInfoV1> {
let (buf, _header_size) = verify(le_u32, |x| *x == 32)(buf)?;
let (buf, _header_size) = context(
"Header size mismatch in BCMedia InfoV1",
verify(le_u32, |x| *x == 32),
)(buf)?;
let (buf, video_width) = le_u32(buf)?;
let (buf, video_height) = le_u32(buf)?;
let (buf, _unknown) = le_u8(buf)?;
@@ -176,7 +183,10 @@ fn bcmedia_info_v1(buf: &[u8]) -> IResult<&[u8], BcMediaInfoV1> {
}
fn bcmedia_info_v2(buf: &[u8]) -> IResult<&[u8], BcMediaInfoV2> {
let (buf, _header_size) = verify(le_u32, |x| *x == 32)(buf)?;
let (buf, _header_size) = context(
"Failed to match headersize in BCMedia Info V2",
verify(le_u32, |x| *x == 32),
)(buf)?;
let (buf, video_width) = le_u32(buf)?;
let (buf, video_height) = le_u32(buf)?;
let (buf, _unknown) = le_u8(buf)?;
@@ -218,9 +228,17 @@ fn bcmedia_info_v2(buf: &[u8]) -> IResult<&[u8], BcMediaInfoV2> {
))
}
fn take4(buf: &[u8]) -> IResult<&[u8], &str> {
map_res(nom::bytes::streaming::take(4usize), |r| {
std::str::from_utf8(r)
})(buf)
}
fn bcmedia_iframe(buf: &[u8]) -> IResult<&[u8], BcMediaIframe> {
named!(take4str( &[u8] ) -> &str, take_str!( 4 ) );
let (buf, video_type_str) = verify(take4str, |x| matches!(x, "H264" | "H265"))(buf)?;
let (buf, video_type_str) = context(
"Video Type is unrecognised in IFrame",
verify(take4, |x| matches!(x, "H264" | "H265")),
)(buf)?;
let (buf, payload_size) = le_u32(buf)?;
let (buf, _unknown_a) = le_u32(buf)?;
let (buf, microseconds) = le_u32(buf)?;
@@ -233,6 +251,7 @@ fn bcmedia_iframe(buf: &[u8]) -> IResult<&[u8], BcMediaIframe> {
n => PAD_SIZE - n,
};
let (buf, _padding) = take!(buf, pad_size)?;
assert_eq!(payload_size as usize, data_slice.len());
let video_type = match video_type_str {
"H264" => VideoType::H264,
@@ -253,8 +272,10 @@ fn bcmedia_iframe(buf: &[u8]) -> IResult<&[u8], BcMediaIframe> {
}
fn bcmedia_pframe(buf: &[u8]) -> IResult<&[u8], BcMediaPframe> {
named!(take4str( &[u8] ) -> &str, take_str!( 4 ) );
let (buf, video_type_str) = verify(take4str, |x| matches!(x, "H264" | "H265"))(buf)?;
let (buf, video_type_str) = context(
"Video Type is unrecognised in PFrame",
verify(take4, |x| matches!(x, "H264" | "H265")),
)(buf)?;
let (buf, payload_size) = le_u32(buf)?;
let (buf, _unknown_a) = le_u32(buf)?;
let (buf, microseconds) = le_u32(buf)?;
@@ -265,6 +286,7 @@ fn bcmedia_pframe(buf: &[u8]) -> IResult<&[u8], BcMediaPframe> {
n => PAD_SIZE - n,
};
let (buf, _padding) = take!(buf, pad_size)?;
assert_eq!(payload_size as usize, data_slice.len());
let video_type = match video_type_str {
"H264" => VideoType::H264,
@@ -307,7 +329,10 @@ fn bcmedia_adpcm(buf: &[u8]) -> IResult<&[u8], BcMediaAdpcm> {
let (buf, payload_size) = le_u16(buf)?;
let (buf, _payload_size_b) = le_u16(buf)?;
let (buf, _magic) = verify(le_u16, |x| *x == MAGIC_HEADER_BCMEDIA_ADPCM_DATA)(buf)?;
let (buf, _magic) = context(
"ADPCM data magic value is invalid",
verify(le_u16, |x| *x == MAGIC_HEADER_BCMEDIA_ADPCM_DATA),
)(buf)?;
// On some camera this value is just 2
// On other cameras is half the block size without the header
let (buf, _half_block_size) = le_u16(buf)?;