Separate segments function in ogg_pager

Signed-off-by: Serial <69764315+Serial-ATA@users.noreply.github.com>
This commit is contained in:
Serial 2021-06-26 21:14:03 -04:00
parent 181b4cfef0
commit 822a312b0d
3 changed files with 126 additions and 122 deletions

View file

@ -16,7 +16,7 @@ impl fmt::Display for PageError {
match self {
PageError::InvalidVersion => {
write!(f, "Invalid stream structure version (Should always be 0)")
}
},
PageError::BadSegmentCount => write!(f, "Page has a segment count < 1"),
PageError::MissingMagic => write!(f, "Page is missing a magic signature"),
PageError::Io(..) => write!(f, "Encountered an std::io::Error"),

View file

@ -1,5 +1,5 @@
mod error;
mod crc;
mod error;
use std::io::{Read, Seek, SeekFrom};
@ -40,29 +40,7 @@ impl Page {
}
pub fn segments(&self) -> Vec<u8> {
let len = self.content.len();
let mut last_len = (len % 255) as u8;
if last_len == 0 {
last_len = 255
}
let mut needed = len / 255;
if needed != 255 {
needed += 1
}
let mut segments = Vec::new();
for i in 0..needed {
if i + 1 < needed {
segments.push(255)
} else {
segments.push(last_len)
}
}
segments
segments(&*self.content)
}
pub fn read<V>(mut data: V) -> Result<Self>
@ -122,3 +100,29 @@ impl Page {
self.checksum = crc::crc32(&*self.as_bytes());
}
}
pub fn segments(cont: &[u8]) -> Vec<u8> {
let len = cont.len();
let mut last_len = (len % 255) as u8;
if last_len == 0 {
last_len = 255
}
let mut needed = len / 255;
if needed != 255 {
needed += 1
}
let mut segments = Vec::new();
for i in 0..needed {
if i + 1 < needed {
segments.push(255)
} else {
segments.push(last_len)
}
}
segments
}