Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify SoundPlayer.ValidateSoundData #104239

Merged
merged 1 commit into from
Jul 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Simplify SoundPlayer.ValidateSoundData
  • Loading branch information
stephentoub committed Jul 1, 2024
commit 632a95fc2749fa233cf50ce3b1c1acd97f4aaee4
Original file line number Diff line number Diff line change
Expand Up @@ -630,32 +630,22 @@ private unsafe void ValidateSoundFile(string fileName)

private static void ValidateSoundData(byte[] data)
{
int position;
short wFormatTag = -1;
bool fmtChunkFound = false;

// the RIFF header should be at least 12 bytes long.
if (data.Length < 12)
{
throw new InvalidOperationException(SR.SoundAPIInvalidWaveHeader);
}

// validate the RIFF header
if (data[0] != 'R' || data[1] != 'I' || data[2] != 'F' || data[3] != 'F')
{
throw new InvalidOperationException(SR.SoundAPIInvalidWaveHeader);
}
if (data[8] != 'W' || data[9] != 'A' || data[10] != 'V' || data[11] != 'E')
if (data.Length < 12 ||
!data.AsSpan().StartsWith("RIFF"u8) ||
!data.AsSpan(8).StartsWith("WAVE"u8))
{
throw new InvalidOperationException(SR.SoundAPIInvalidWaveHeader);
}

// we only care about "fmt " chunk
position = 12;
int position = 12;
int len = data.Length;
short wFormatTag = -1;
bool fmtChunkFound = false;
while (!fmtChunkFound && position < len - 8)
{
if (data[position] == (byte)'f' && data[position + 1] == (byte)'m' && data[position + 2] == (byte)'t' && data[position + 3] == (byte)' ')
if (data.AsSpan(position).StartsWith("fmt "u8))
{
// fmt chunk
fmtChunkFound = true;
Expand Down
Loading