mirror of
https://github.com/Serial-ATA/lofty-rs
synced 2024-11-10 06:34:18 +00:00
FLAC: Allow skipping picture blocks
This commit is contained in:
parent
13337fc7fc
commit
40e11ca729
4 changed files with 24 additions and 16 deletions
|
@ -1,7 +1,9 @@
|
|||
#![allow(dead_code)]
|
||||
|
||||
use crate::error::Result;
|
||||
use crate::macros::try_vec;
|
||||
|
||||
use std::io::{Read, Seek};
|
||||
use std::io::{Read, Seek, SeekFrom};
|
||||
|
||||
use byteorder::{BigEndian, ReadBytesExt};
|
||||
|
||||
|
@ -21,9 +23,10 @@ pub(crate) struct Block {
|
|||
}
|
||||
|
||||
impl Block {
|
||||
pub(crate) fn read<R>(data: &mut R) -> Result<Self>
|
||||
pub(crate) fn read<R, P>(data: &mut R, mut predicate: P) -> Result<Self>
|
||||
where
|
||||
R: Read + Seek,
|
||||
P: FnMut(u8) -> bool,
|
||||
{
|
||||
let start = data.stream_position()?;
|
||||
|
||||
|
@ -34,8 +37,14 @@ impl Block {
|
|||
let size = data.read_u24::<BigEndian>()?;
|
||||
log::trace!("Reading FLAC block, type: {ty}, size: {size}");
|
||||
|
||||
let mut content = try_vec![0; size as usize];
|
||||
data.read_exact(&mut content)?;
|
||||
let mut content;
|
||||
if predicate(ty) {
|
||||
content = try_vec![0; size as usize];
|
||||
data.read_exact(&mut content)?;
|
||||
} else {
|
||||
content = Vec::new();
|
||||
data.seek(SeekFrom::Current(i64::from(size)))?;
|
||||
}
|
||||
|
||||
let end = data.stream_position()?;
|
||||
|
||||
|
|
|
@ -3,10 +3,7 @@ use super::properties::FlacProperties;
|
|||
use super::FlacFile;
|
||||
use crate::config::{ParseOptions, ParsingMode};
|
||||
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::flac::block::{BLOCK_ID_PICTURE, BLOCK_ID_STREAMINFO, BLOCK_ID_VORBIS_COMMENTS};
|
||||
use crate::id3::v2::read::parse_id3v2;
|
||||
use crate::id3::{find_id3v2, FindId3v2Config, ID3FindResults};
|
||||
use crate::macros::decode_err;
|
||||
|
@ -26,7 +23,7 @@ where
|
|||
decode_err!(@BAIL Flac, "File missing \"fLaC\" stream marker");
|
||||
}
|
||||
|
||||
let block = Block::read(data)?;
|
||||
let block = Block::read(data, |_| true)?;
|
||||
|
||||
if block.ty != BLOCK_ID_STREAMINFO {
|
||||
decode_err!(@BAIL Flac, "File missing mandatory STREAMINFO block");
|
||||
|
@ -73,13 +70,15 @@ where
|
|||
let mut last_block = stream_info.last;
|
||||
|
||||
while !last_block {
|
||||
let block = Block::read(data)?;
|
||||
let block = Block::read(data, |block_type| {
|
||||
block_type == BLOCK_ID_VORBIS_COMMENTS
|
||||
|| (block_type == BLOCK_ID_PICTURE && parse_options.read_cover_art)
|
||||
})?;
|
||||
|
||||
last_block = block.last;
|
||||
|
||||
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");
|
||||
if block.content.is_empty() {
|
||||
continue;
|
||||
}
|
||||
|
||||
if block.ty == BLOCK_ID_VORBIS_COMMENTS && parse_options.read_tags {
|
||||
|
|
|
@ -71,7 +71,7 @@ where
|
|||
let mut blocks_remove = Vec::new();
|
||||
|
||||
while !last_block {
|
||||
let block = Block::read(&mut cursor)?;
|
||||
let block = Block::read(&mut cursor, |_| true)?;
|
||||
let start = block.start;
|
||||
let end = block.end;
|
||||
|
||||
|
|
|
@ -805,7 +805,7 @@ where
|
|||
return Ok(());
|
||||
}
|
||||
|
||||
let stream_info_block = crate::flac::block::Block::read(stsd)?;
|
||||
let stream_info_block = crate::flac::block::Block::read(stsd, |_| true)?;
|
||||
let flac_properties =
|
||||
crate::flac::properties::read_properties(&mut &stream_info_block.content[..], 0, 0)?;
|
||||
|
||||
|
|
Loading…
Reference in a new issue