Skip to content

Commit

Permalink
Merge pull request #44 from vswarte/fix/remove-padding-bytes-from-bdt…
Browse files Browse the repository at this point in the history
…-entry-reader-output

Remove padding bytes from DvdBnd entry reader output
  • Loading branch information
vswarte authored Jul 21, 2024
2 parents d80c2e4 + 999b662 commit fa3b8e3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
12 changes: 11 additions & 1 deletion crates/dvdbnd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,17 @@ impl DvdBnd {
#[cfg(unix)]
let _ = mmap.advise(memmap2::Advice::Sequential);

Ok(DvdBndEntryReader::new(mmap.make_read_only()?))
// DCXes dont have an unpadded size set
let effective_file_size = if entry.file_size != 0 {
entry.file_size
} else {
entry.file_size_with_padding
} as usize;

Ok(DvdBndEntryReader::new(
mmap.make_read_only()?,
effective_file_size,
))
}
None => Err(DvdBndEntryError::NotFound),
}
Expand Down
19 changes: 13 additions & 6 deletions crates/dvdbnd/src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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 {
Expand Down

0 comments on commit fa3b8e3

Please sign in to comment.