Update ogg_pager

This commit is contained in:
Serial 2022-01-07 13:02:48 -05:00
parent bc9fbd87ab
commit 3f0d6d8b00
8 changed files with 20 additions and 62 deletions

View file

@ -15,7 +15,7 @@ flate2 = { version = "1.0.22", optional = true }
# Vorbis comments pictures # Vorbis comments pictures
base64 = { version = "0.13.0", optional = true } base64 = { version = "0.13.0", optional = true }
# OGG Vorbis/Opus # OGG Vorbis/Opus
ogg_pager = "0.1.8" ogg_pager = "0.1.9"
# Key maps # Key maps
lazy_static = "1.4.0" lazy_static = "1.4.0"
paste = "1.0.6" paste = "1.0.6"

View file

@ -27,54 +27,10 @@ use std::io::{Read, Seek};
use ogg_pager::Page; 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<()> { pub(self) fn verify_signature(page: &Page, sig: &[u8]) -> Result<()> {
let sig_len = sig.len(); 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")); return Err(LoftyError::Ogg("File missing magic signature"));
} }

View file

@ -97,7 +97,7 @@ where
let first_page_abgp = first_page.abgp; let first_page_abgp = first_page.abgp;
// Skip identification header // 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 version = first_page_content.read_u8()?;
let channels = first_page_content.read_u8()?; let channels = first_page_content.read_u8()?;

View file

@ -12,12 +12,11 @@ pub(crate) fn write_to(
pages: &mut [Page], pages: &mut [Page],
) -> Result<()> { ) -> Result<()> {
let reached_md_end: bool; let reached_md_end: bool;
let mut remaining = Vec::new();
loop { loop {
let p = Page::read(data, true)?; 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))?; data.seek(SeekFrom::Start(p.start as u64))?;
reached_md_end = true; reached_md_end = true;
break; break;
@ -28,6 +27,7 @@ pub(crate) fn write_to(
return Err(LoftyError::Opus("File ends with comment header")); return Err(LoftyError::Opus("File ends with comment header"));
} }
let mut remaining = Vec::new();
data.read_to_end(&mut remaining)?; data.read_to_end(&mut remaining)?;
for mut p in pages.iter_mut() { for mut p in pages.iter_mut() {

View file

@ -69,15 +69,15 @@ where
let mut md_pages: Vec<u8> = Vec::new(); 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) { while let Ok(page) = Page::read(data, false) {
if md_pages.len() > 125_829_120 { if md_pages.len() > 125_829_120 {
return Err(LoftyError::TooMuchData); return Err(LoftyError::TooMuchData);
} }
if page.header_type == 1 { if page.header_type() & 0x01 == 1 {
md_pages.extend(page.content.iter()); md_pages.extend(page.content().iter());
} else { } else {
data.seek(SeekFrom::Start(page.start))?; data.seek(SeekFrom::Start(page.start))?;
break; break;

View file

@ -116,7 +116,7 @@ where
let first_page_abgp = first_page.abgp; let first_page_abgp = first_page.abgp;
// Skip identification header // 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>()?; let version = first_page_content.read_u32::<LittleEndian>()?;

View file

@ -36,7 +36,7 @@ pub(crate) fn write_to(
loop { loop {
let p = Page::read(data, false)?; 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.seek(SeekFrom::Start(p.start as u64))?;
data.read_to_end(&mut remaining)?; data.read_to_end(&mut remaining)?;
@ -44,7 +44,7 @@ pub(crate) fn write_to(
break; break;
} }
c.write_all(&p.content)?; c.write_all(p.content())?;
} }
if !reached_md_end { if !reached_md_end {
@ -82,12 +82,12 @@ pub(crate) fn write_to(
if i == pages_len { if i == pages_len {
// Add back the framing bit // 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 // The segment tables of current page and the setup header have to be combined
let mut seg_table = Vec::new(); let mut seg_table = Vec::new();
seg_table.extend(p.segments().iter()); seg_table.append(&mut ogg_pager::segment_table(p.content().len())?);
seg_table.extend(ogg_pager::segments(&*setup)); seg_table.append(&mut ogg_pager::segment_table(setup.len())?);
let mut seg_table_len = seg_table.len(); let mut seg_table_len = seg_table.len();

View file

@ -1,4 +1,4 @@
use super::{page_from_packet, verify_signature}; use super::verify_signature;
use crate::error::{LoftyError, Result}; use crate::error::{LoftyError, Result};
use crate::ogg::constants::{OPUSTAGS, VORBIS_COMMENT_HEAD}; use crate::ogg::constants::{OPUSTAGS, VORBIS_COMMENT_HEAD};
use crate::ogg::tag::VorbisCommentsRef; use crate::ogg::tag::VorbisCommentsRef;
@ -79,7 +79,9 @@ pub(super) fn create_pages(
writer.write_u32::<LittleEndian>(count)?; writer.write_u32::<LittleEndian>(count)?;
writer.seek(SeekFrom::Start(packet_end))?; 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")] #[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)?; verify_signature(&first_md_page, sig)?;
// Retain the file's vendor string // 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 vendor_len = md_reader.read_u32::<LittleEndian>()?;
let mut vendor = vec![0; vendor_len as usize]; 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( super::vorbis::write::write_to(
data, data,
&mut writer, &mut writer,
first_md_page.content, first_md_page.take_content(),
ser, ser,
&mut pages, &mut pages,
)?; )?;