Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove padding bytes from DvdBnd entry reader output #44

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Introduce length field to DvdBndEntryReader to limit the output to no…
…n-padding bytes
vswarte committed Jul 21, 2024
commit 18deb0ea663699b430e54894eddf2756b77c032e
5 changes: 4 additions & 1 deletion crates/dvdbnd/src/lib.rs
Original file line number Diff line number Diff line change
@@ -154,7 +154,10 @@ impl DvdBnd {
#[cfg(unix)]
let _ = mmap.advise(memmap2::Advice::Sequential);

Ok(DvdBndEntryReader::new(mmap.make_read_only()?))
Ok(DvdBndEntryReader::new(
mmap.make_read_only()?,
entry.file_size as usize,
))
}
None => Err(DvdBndEntryError::NotFound),
}
19 changes: 13 additions & 6 deletions crates/dvdbnd/src/reader.rs
Original file line number Diff line number Diff line change
@@ -5,18 +5,25 @@ use memmap2::Mmap;
pub struct DvdBndEntryReader {
mmap: Mmap,
position: usize,
length: usize,
}

impl DvdBndEntryReader {
pub fn new(mmap: Mmap) -> Self {
Self { mmap, position: 0 }
pub fn new(mmap: Mmap, length: usize) -> Self {
Self {
mmap,
position: 0,
length,
}
}

pub fn data(&self) -> &[u8] {
&self.mmap[..]
&self.mmap[..self.length]
}
}

// Do we really need this? With the length being a thing now to deal with the
// padding on the output this conversion is no longer lossless.
impl From<DvdBndEntryReader> for Mmap {
fn from(value: DvdBndEntryReader) -> Self {
value.mmap
@@ -25,7 +32,7 @@ impl From<DvdBndEntryReader> for Mmap {

impl Read for DvdBndEntryReader {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
let mut data = &self.mmap[self.position..];
let mut data = &self.data()[self.position..];
let read = data.read(buf)?;

self.position += read;
@@ -38,12 +45,12 @@ impl Seek for DvdBndEntryReader {
fn seek(&mut self, pos: SeekFrom) -> std::io::Result<u64> {
let new_pos = match pos {
SeekFrom::Start(start_offset) => Some(start_offset as usize),
SeekFrom::End(end_offset) => self.mmap.len().checked_add_signed(end_offset as isize),
SeekFrom::End(end_offset) => self.data().len().checked_add_signed(end_offset as isize),
SeekFrom::Current(offset) => self.position.checked_add_signed(offset as isize),
}
.ok_or(Error::other("invalid seek offset"))?;

if new_pos < self.mmap.len() {
if new_pos < self.data().len() {
self.position = new_pos;
Ok(self.position as u64)
} else {