Skip to content

Commit

Permalink
Merge pull request #15 from Holzhaus/length-checks
Browse files Browse the repository at this point in the history
Add some length checks
  • Loading branch information
Holzhaus authored Feb 18, 2022
2 parents 1464384 + 6b0676f commit 2711f48
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/anlz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1095,6 +1095,15 @@ impl Content {

fn parse_waveform_detail<'a>(input: &'a [u8], header: &Header) -> IResult<&'a [u8], Self> {
let (input, len_entry_bytes) = nom::number::complete::be_u32(input)?;
// All waveform detail entries should be 1 byte long. If we see other values here,
// some reverse-engineering is needed.
if len_entry_bytes != 2 {
return Err(Err::Error(nom::error::Error::from_error_kind(
input,
ErrorKind::LengthValue,
)));
}

let (input, len_entries) = nom::number::complete::be_u32(input)?;
let (input, unknown) = nom::number::complete::be_u32(input)?;
let (input, content_data_slice) = nom::bytes::complete::take(header.content_size())(input)?;
Expand All @@ -1120,6 +1129,15 @@ impl Content {
_header: &Header,
) -> IResult<&'a [u8], Self> {
let (input, len_entry_bytes) = nom::number::complete::be_u32(input)?;
// All waveform color preview entries should be 6 bytes long. If we see other values here,
// some reverse-engineering is needed.
if len_entry_bytes != 2 {
return Err(Err::Error(nom::error::Error::from_error_kind(
input,
ErrorKind::LengthValue,
)));
}

let (input, len_entries) = nom::number::complete::be_u32(input)?;
let entry_count = usize::try_from(len_entries)
.map_err(|_| nom_input_error_with_kind(input, ErrorKind::TooLarge))?;
Expand All @@ -1144,6 +1162,15 @@ impl Content {
_header: &Header,
) -> IResult<&'a [u8], Self> {
let (input, len_entry_bytes) = nom::number::complete::be_u32(input)?;
// All waveform color detail entries should be 2 bytes long. If we see other values here,
// some reverse-engineering is needed.
if len_entry_bytes != 2 {
return Err(Err::Error(nom::error::Error::from_error_kind(
input,
ErrorKind::LengthValue,
)));
}

let (input, len_entries) = nom::number::complete::be_u32(input)?;
let entry_count = usize::try_from(len_entries)
.map_err(|_| nom_input_error_with_kind(input, ErrorKind::TooLarge))?;
Expand All @@ -1165,6 +1192,15 @@ impl Content {

fn parse_song_structure<'a>(input: &'a [u8], _header: &Header) -> IResult<&'a [u8], Self> {
let (input, len_entry_bytes) = nom::number::complete::be_u32(input)?;
// All phrase entries should be 24 bytes long. If we see other values here, some
// reverse-engineering is needed.
if len_entry_bytes != 24 {
return Err(Err::Error(nom::error::Error::from_error_kind(
input,
ErrorKind::LengthValue,
)));
}

let (input, len_entries) = nom::number::complete::be_u16(input)?;
let entry_count = usize::try_from(len_entries)
.map_err(|_| nom_input_error_with_kind(input, ErrorKind::TooLarge))?;
Expand Down

0 comments on commit 2711f48

Please sign in to comment.