Skip to content

Commit

Permalink
Allow to discard unknown SD option
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruslan Iusupov committed Feb 19, 2024
1 parent d2deb5b commit c3e0bfa
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/sd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,12 @@ impl SdHeader {
#[inline]
#[cfg(any(target_pointer_width = "32", target_pointer_width = "64"))]
pub fn read<T: Read + Seek>(reader: &mut T) -> Result<Self, SdReadError> {
SdHeader::read_with_flag(reader, false)
}

#[inline]
#[cfg(any(target_pointer_width = "32", target_pointer_width = "64"))]
pub fn read_with_flag<T: Read + Seek>(reader: &mut T, discard_unknown_option: bool) -> Result<Self, SdReadError> {
const HEADER_LENGTH: usize = 1 + 3 + 4; // flags + rev + entries length
let mut header_bytes: [u8; HEADER_LENGTH] = [0; HEADER_LENGTH];
reader.read_exact(&mut header_bytes)?;
Expand Down Expand Up @@ -373,7 +379,7 @@ impl SdHeader {
options.try_reserve((options_length as usize) / 4)?;

while options_length > 0 {
let (read_bytes, option) = SdOption::read(reader)?;
let (read_bytes, option) = SdOption::read(reader, discard_unknown_option)?;
options.push(option);
options_length -= read_bytes as u32;
}
Expand Down Expand Up @@ -946,7 +952,7 @@ impl From<UnknownDiscardableOption> for SdOption {
impl SdOption {
/// Read the value from a [`std::io::Read`] source.
#[inline]
pub fn read<T: Read + Seek>(reader: &mut T) -> Result<(u16, Self), SdReadError> {
pub fn read<T: Read + Seek>(reader: &mut T, discard_unknown_option: bool) -> Result<(u16, Self), SdReadError> {
use self::sd_options::*;
use self::SdOption::*;
use SdReadError::*;
Expand Down Expand Up @@ -1065,7 +1071,7 @@ impl SdOption {
})
}
option_type => {
if discardable {
if discardable || discard_unknown_option {
// skip unknown options payload if "discardable"
// if length is greater then 1 then we need to skip the rest of the option
// (note that we already read length 1 as this contains the "discardable"
Expand Down

0 comments on commit c3e0bfa

Please sign in to comment.