Skip to content

Commit

Permalink
wav: Always read the proper amount of data for fmt chunk.
Browse files Browse the repository at this point in the history
If the fmt chunk contains a 40-byte WaveFormatEx structure, then
even if the extension size is declared 0, 40-bytes must be read
from the chunk.

Fixes #87.
  • Loading branch information
pdeljanov committed Dec 18, 2021
1 parent f1ff9f6 commit a1e533d
Showing 1 changed file with 17 additions and 19 deletions.
36 changes: 17 additions & 19 deletions symphonia-format-wav/src/chunks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,26 +274,24 @@ impl WaveFormatChunk {
n_channels: u16,
len: u32,
) -> Result<WaveFormatData> {
// WaveFormat for a PCM format /may/ be extended with an extra data length parameter
// followed by the extra data itself. Use the chunk length to determine if the format chunk
// is extended.
let is_extended = match len {
// Minimal WavFormat struct, no extension.
16 => false,
// WaveFormatEx with exta data length field present, but not extra data.
18 => true,
// WaveFormatEx with extra data length field and extra data.
40 => true,
_ => return decode_error("wav: malformed fmt_pcm chunk"),
};

// If there is extra data, read the length, and discard the extra data.
if is_extended {
let extra_size = reader.read_u16()?;

if extra_size > 0 {
reader.ignore_bytes(u64::from(extra_size))?;
// WaveFormat for a PCM format may be extended with an extra data length field followed by
// the extension data itself. Use the chunk length to determine if the format chunk is
// extended.
match len {
// Basic WavFormat struct, no extension.
16 => (),
// WaveFormatEx with extension data length field present, but no extension data.
18 => {
// Extension data length should be 0.
let _extension_len = reader.read_be_u16()?;
}
// WaveFormatEx with extension data length field present, and extension data.
40 => {
// Extension data length should be either 0 or 22 (if valid data is present).
let _extension_len = reader.read_u16()?;
reader.ignore_bytes(22)?;
}
_ => return decode_error("wav: malformed fmt_pcm chunk"),
}

// Bits per sample for PCM is both the encoded sample width, and the actual sample width.
Expand Down

0 comments on commit a1e533d

Please sign in to comment.