mirror of
https://github.com/Serial-ATA/lofty-rs
synced 2024-11-10 06:34:18 +00:00
Update ogg_pager
This commit is contained in:
parent
bc9fbd87ab
commit
3f0d6d8b00
8 changed files with 20 additions and 62 deletions
|
@ -15,7 +15,7 @@ flate2 = { version = "1.0.22", optional = true }
|
|||
# Vorbis comments pictures
|
||||
base64 = { version = "0.13.0", optional = true }
|
||||
# OGG Vorbis/Opus
|
||||
ogg_pager = "0.1.8"
|
||||
ogg_pager = "0.1.9"
|
||||
# Key maps
|
||||
lazy_static = "1.4.0"
|
||||
paste = "1.0.6"
|
||||
|
|
|
@ -27,54 +27,10 @@ use std::io::{Read, Seek};
|
|||
|
||||
use ogg_pager::Page;
|
||||
|
||||
#[cfg(feature = "vorbis_comments")]
|
||||
pub(self) fn page_from_packet(packet: &mut [u8]) -> Result<Vec<Page>> {
|
||||
let mut pages: Vec<Page> = Vec::new();
|
||||
|
||||
let reader = &mut &packet[..];
|
||||
|
||||
let mut start = 0_u64;
|
||||
let mut i = 0;
|
||||
|
||||
while !reader.is_empty() {
|
||||
let header_type = if i == 0 { 0 } else { 1_u8 };
|
||||
|
||||
let size = std::cmp::min(65025_u64, reader.len() as u64);
|
||||
|
||||
if i != 0 {
|
||||
if let Some(s) = start.checked_add(size) {
|
||||
start = s
|
||||
} else {
|
||||
return Err(LoftyError::TooMuchData);
|
||||
}
|
||||
}
|
||||
|
||||
let mut content = vec![0; size as usize];
|
||||
reader.read_exact(&mut content)?;
|
||||
|
||||
let end = start + size;
|
||||
|
||||
pages.push(Page {
|
||||
content,
|
||||
header_type,
|
||||
abgp: 0,
|
||||
serial: 0, // Retrieved later
|
||||
seq_num: (i + 1) as u32,
|
||||
checksum: 0, // Calculated later
|
||||
start,
|
||||
end,
|
||||
});
|
||||
|
||||
i += 1;
|
||||
}
|
||||
|
||||
Ok(pages)
|
||||
}
|
||||
|
||||
pub(self) fn verify_signature(page: &Page, sig: &[u8]) -> Result<()> {
|
||||
let sig_len = sig.len();
|
||||
|
||||
if page.content.len() < sig_len || &page.content[..sig_len] != sig {
|
||||
if page.content().len() < sig_len || &page.content()[..sig_len] != sig {
|
||||
return Err(LoftyError::Ogg("File missing magic signature"));
|
||||
}
|
||||
|
||||
|
|
|
@ -97,7 +97,7 @@ where
|
|||
let first_page_abgp = first_page.abgp;
|
||||
|
||||
// Skip identification header
|
||||
let first_page_content = &mut &first_page.content[8..];
|
||||
let first_page_content = &mut &first_page.content()[8..];
|
||||
|
||||
let version = first_page_content.read_u8()?;
|
||||
let channels = first_page_content.read_u8()?;
|
||||
|
|
|
@ -12,12 +12,11 @@ pub(crate) fn write_to(
|
|||
pages: &mut [Page],
|
||||
) -> Result<()> {
|
||||
let reached_md_end: bool;
|
||||
let mut remaining = Vec::new();
|
||||
|
||||
loop {
|
||||
let p = Page::read(data, true)?;
|
||||
|
||||
if p.header_type != 1 {
|
||||
if p.header_type() & 0x01 != 0x01 {
|
||||
data.seek(SeekFrom::Start(p.start as u64))?;
|
||||
reached_md_end = true;
|
||||
break;
|
||||
|
@ -28,6 +27,7 @@ pub(crate) fn write_to(
|
|||
return Err(LoftyError::Opus("File ends with comment header"));
|
||||
}
|
||||
|
||||
let mut remaining = Vec::new();
|
||||
data.read_to_end(&mut remaining)?;
|
||||
|
||||
for mut p in pages.iter_mut() {
|
||||
|
|
|
@ -69,15 +69,15 @@ where
|
|||
|
||||
let mut md_pages: Vec<u8> = Vec::new();
|
||||
|
||||
md_pages.extend(md_page.content[comment_sig.len()..].iter());
|
||||
md_pages.extend(md_page.content()[comment_sig.len()..].iter());
|
||||
|
||||
while let Ok(page) = Page::read(data, false) {
|
||||
if md_pages.len() > 125_829_120 {
|
||||
return Err(LoftyError::TooMuchData);
|
||||
}
|
||||
|
||||
if page.header_type == 1 {
|
||||
md_pages.extend(page.content.iter());
|
||||
if page.header_type() & 0x01 == 1 {
|
||||
md_pages.extend(page.content().iter());
|
||||
} else {
|
||||
data.seek(SeekFrom::Start(page.start))?;
|
||||
break;
|
||||
|
|
|
@ -116,7 +116,7 @@ where
|
|||
let first_page_abgp = first_page.abgp;
|
||||
|
||||
// Skip identification header
|
||||
let first_page_content = &mut &first_page.content[7..];
|
||||
let first_page_content = &mut &first_page.content()[7..];
|
||||
|
||||
let version = first_page_content.read_u32::<LittleEndian>()?;
|
||||
|
||||
|
|
|
@ -36,7 +36,7 @@ pub(crate) fn write_to(
|
|||
loop {
|
||||
let p = Page::read(data, false)?;
|
||||
|
||||
if p.header_type != 1 {
|
||||
if p.header_type() & 0x01 != 1 {
|
||||
data.seek(SeekFrom::Start(p.start as u64))?;
|
||||
data.read_to_end(&mut remaining)?;
|
||||
|
||||
|
@ -44,7 +44,7 @@ pub(crate) fn write_to(
|
|||
break;
|
||||
}
|
||||
|
||||
c.write_all(&p.content)?;
|
||||
c.write_all(p.content())?;
|
||||
}
|
||||
|
||||
if !reached_md_end {
|
||||
|
@ -82,12 +82,12 @@ pub(crate) fn write_to(
|
|||
|
||||
if i == pages_len {
|
||||
// Add back the framing bit
|
||||
p.content.push(1);
|
||||
p.extend(&[1]);
|
||||
|
||||
// The segment tables of current page and the setup header have to be combined
|
||||
let mut seg_table = Vec::new();
|
||||
seg_table.extend(p.segments().iter());
|
||||
seg_table.extend(ogg_pager::segments(&*setup));
|
||||
seg_table.append(&mut ogg_pager::segment_table(p.content().len())?);
|
||||
seg_table.append(&mut ogg_pager::segment_table(setup.len())?);
|
||||
|
||||
let mut seg_table_len = seg_table.len();
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use super::{page_from_packet, verify_signature};
|
||||
use super::verify_signature;
|
||||
use crate::error::{LoftyError, Result};
|
||||
use crate::ogg::constants::{OPUSTAGS, VORBIS_COMMENT_HEAD};
|
||||
use crate::ogg::tag::VorbisCommentsRef;
|
||||
|
@ -79,7 +79,9 @@ pub(super) fn create_pages(
|
|||
writer.write_u32::<LittleEndian>(count)?;
|
||||
writer.seek(SeekFrom::Start(packet_end))?;
|
||||
|
||||
page_from_packet(writer.get_mut())
|
||||
// Stream serial is retrieved later
|
||||
// Checksum is calculated later
|
||||
Ok(ogg_pager::paginate(writer.get_ref(), 0, 0, 0))
|
||||
}
|
||||
|
||||
#[cfg(feature = "vorbis_comments")]
|
||||
|
@ -95,7 +97,7 @@ pub(super) fn write(data: &mut File, tag: &mut VorbisCommentsRef, sig: &[u8]) ->
|
|||
verify_signature(&first_md_page, sig)?;
|
||||
|
||||
// Retain the file's vendor string
|
||||
let md_reader = &mut &first_md_page.content[sig.len()..];
|
||||
let md_reader = &mut &first_md_page.content()[sig.len()..];
|
||||
|
||||
let vendor_len = md_reader.read_u32::<LittleEndian>()?;
|
||||
let mut vendor = vec![0; vendor_len as usize];
|
||||
|
@ -114,7 +116,7 @@ pub(super) fn write(data: &mut File, tag: &mut VorbisCommentsRef, sig: &[u8]) ->
|
|||
super::vorbis::write::write_to(
|
||||
data,
|
||||
&mut writer,
|
||||
first_md_page.content,
|
||||
first_md_page.take_content(),
|
||||
ser,
|
||||
&mut pages,
|
||||
)?;
|
||||
|
|
Loading…
Reference in a new issue