FLAC: Cleanup FlacFile reading

This commit is contained in:
Serial 2023-04-12 13:02:17 -04:00
parent d180dad85f
commit c20796dda4
No known key found for this signature in database
GPG key ID: DA95198DC17C4568
2 changed files with 38 additions and 22 deletions

View file

@ -5,6 +5,12 @@ use std::io::{Read, Seek};
use byteorder::{BigEndian, ReadBytesExt}; use byteorder::{BigEndian, ReadBytesExt};
pub(in crate::flac) const BLOCK_ID_STREAMINFO: u8 = 0;
pub(in crate::flac) const BLOCK_ID_PADDING: u8 = 1;
pub(in crate::flac) const BLOCK_ID_SEEKTABLE: u8 = 3;
pub(in crate::flac) const BLOCK_ID_VORBIS_COMMENTS: u8 = 4;
pub(in crate::flac) const BLOCK_ID_PICTURE: u8 = 6;
pub(crate) struct Block { pub(crate) struct Block {
pub(super) byte: u8, pub(super) byte: u8,
pub(super) ty: u8, pub(super) ty: u8,

View file

@ -2,6 +2,10 @@ use super::block::Block;
use super::properties::FlacProperties; use super::properties::FlacProperties;
use super::FlacFile; use super::FlacFile;
use crate::error::Result; use crate::error::Result;
use crate::flac::block::{
BLOCK_ID_PADDING, BLOCK_ID_PICTURE, BLOCK_ID_SEEKTABLE, BLOCK_ID_STREAMINFO,
BLOCK_ID_VORBIS_COMMENTS,
};
use crate::id3::v2::read::parse_id3v2; use crate::id3::v2::read::parse_id3v2;
use crate::id3::{find_id3v2, ID3FindResults}; use crate::id3::{find_id3v2, ID3FindResults};
use crate::macros::decode_err; use crate::macros::decode_err;
@ -25,7 +29,7 @@ where
let block = Block::read(data)?; let block = Block::read(data)?;
if block.ty != 0 { if block.ty != BLOCK_ID_STREAMINFO {
decode_err!(@BAIL FLAC, "File missing mandatory STREAMINFO block"); decode_err!(@BAIL FLAC, "File missing mandatory STREAMINFO block");
} }
@ -60,31 +64,42 @@ where
let mut last_block = stream_info.last; let mut last_block = stream_info.last;
let mut tag = VorbisComments {
vendor: String::new(),
items: vec![],
pictures: vec![],
};
while !last_block { while !last_block {
let block = Block::read(data)?; let block = Block::read(data)?;
last_block = block.last; last_block = block.last;
if block.content.is_empty() && (block.ty != 1 && block.ty != 3) { if block.content.is_empty()
&& (block.ty != BLOCK_ID_PADDING && block.ty != BLOCK_ID_SEEKTABLE)
{
decode_err!(@BAIL FLAC, "Encountered a zero-sized metadata block"); decode_err!(@BAIL FLAC, "Encountered a zero-sized metadata block");
} }
match block.ty { if block.ty == BLOCK_ID_VORBIS_COMMENTS {
4 => read_comments(&mut &*block.content, block.content.len() as u64, &mut tag)?, if flac_file.vorbis_comments_tag.is_some() {
6 => flac_file decode_err!(@BAIL FLAC, "Streams are only allowed one Vorbis Comments block per stream");
}
let mut vorbis_comments = VorbisComments::new();
read_comments(
&mut &*block.content,
block.content.len() as u64,
&mut vorbis_comments,
)?;
flac_file.vorbis_comments_tag = Some(vorbis_comments);
continue;
}
if block.ty == BLOCK_ID_PICTURE {
flac_file
.pictures .pictures
.push(Picture::from_flac_bytes(&block.content, false)?), .push(Picture::from_flac_bytes(&block.content, false)?)
_ => {},
} }
} }
flac_file.vorbis_comments_tag = if !parse_options.read_properties {
(!(tag.items.is_empty() && tag.pictures.is_empty())).then_some(tag); return Ok(flac_file);
}
let (stream_length, file_length) = { let (stream_length, file_length) = {
let current = data.stream_position()?; let current = data.stream_position()?;
@ -93,13 +108,8 @@ where
(end - current, end) (end - current, end)
}; };
if parse_options.read_properties { flac_file.properties =
flac_file.properties = super::properties::read_properties( super::properties::read_properties(&mut &*stream_info.content, stream_length, file_length)?;
&mut &*stream_info.content,
stream_length,
file_length,
)?;
}
Ok(flac_file) Ok(flac_file)
} }