Skip to content

Commit

Permalink
remove unneeded reader and writer references
Browse files Browse the repository at this point in the history
  • Loading branch information
iliazeus committed Oct 13, 2024
1 parent e8fff7a commit 511a989
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 26 deletions.
4 changes: 2 additions & 2 deletions src/executable/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub struct TitleInfo {
}

impl TitleExecutionInfo {
pub fn from_xex<R: Read>(reader: &mut R) -> Result<TitleExecutionInfo, Error> {
pub fn from_xex<R: Read>(mut reader: R) -> Result<TitleExecutionInfo, Error> {
Ok(TitleExecutionInfo {
media_id: reader.read_u32::<BE>()?,
version: reader.read_u32::<BE>()?,
Expand All @@ -38,7 +38,7 @@ impl TitleExecutionInfo {
})
}

pub fn from_xbe<R: Read + Seek>(reader: &mut R) -> Result<TitleExecutionInfo, Error> {
pub fn from_xbe<R: Read + Seek>(mut reader: R) -> Result<TitleExecutionInfo, Error> {
reader.seek(SeekFrom::Current(8))?;
let title_id = reader.read_u32::<LE>()?;

Expand Down
6 changes: 3 additions & 3 deletions src/executable/xbe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ pub struct XbeHeaderFields {
}

impl XbeHeader {
pub fn read<R: Read + Seek>(reader: &mut R) -> Result<XbeHeader, Error> {
Self::check_magic_bytes(reader)?;
pub fn read<R: Read + Seek>(mut reader: R) -> Result<XbeHeader, Error> {
Self::check_magic_bytes(&mut reader)?;

// Offset 0x0104
reader.seek(SeekFrom::Current(256))?;
Expand All @@ -41,7 +41,7 @@ impl XbeHeader {
})
}

fn check_magic_bytes<R: Read + Seek>(reader: &mut R) -> Result<(), Error> {
fn check_magic_bytes<R: Read + Seek>(mut reader: R) -> Result<(), Error> {
let mut magic_bytes = [0u8; 4];
reader.read_exact(&mut magic_bytes)?;

Expand Down
10 changes: 5 additions & 5 deletions src/executable/xex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ enum XexHeaderFieldId {
}

impl XexHeader {
pub fn read<R: Read + Seek>(reader: &mut R) -> Result<XexHeader, Error> {
Self::check_magic_bytes(reader)?;
pub fn read<R: Read + Seek>(mut reader: R) -> Result<XexHeader, Error> {
Self::check_magic_bytes(&mut reader)?;
Self::read_checked(reader)
}

fn check_magic_bytes<R: Read + Seek>(reader: &mut R) -> Result<(), Error> {
fn check_magic_bytes<R: Read + Seek>(mut reader: R) -> Result<(), Error> {
let mut buf = [0_u8; 4];
reader.read_exact(&mut buf)?;

Expand All @@ -94,7 +94,7 @@ impl XexHeader {
Ok(())
}

fn read_checked<R: Read + Seek>(reader: &mut R) -> Result<XexHeader, Error> {
fn read_checked<R: Read + Seek>(mut reader: R) -> Result<XexHeader, Error> {
let header_offset = reader.stream_position()?;

let _ = reader.read_u32::<BE>()?;
Expand Down Expand Up @@ -122,7 +122,7 @@ impl XexHeader {
Some(Key::ExecutionId) => {
let offset = reader.stream_position()?;
reader.seek(SeekFrom::Start(header_offset + (value as u64)))?;
fields.execution_info = Some(TitleExecutionInfo::from_xex(reader)?);
fields.execution_info = Some(TitleExecutionInfo::from_xex(&mut reader)?);
reader.seek(SeekFrom::Start(offset))?;
}

Expand Down
2 changes: 1 addition & 1 deletion src/god/hash_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl HashList {
Sha1::digest(&self.buffer).into()
}

pub fn write<W: Write>(&self, writer: &mut W) -> Result<(), Error> {
pub fn write<W: Write>(&self, mut writer: W) -> Result<(), Error> {
writer.write_all(&self.buffer)?;
Ok(())
}
Expand Down
12 changes: 6 additions & 6 deletions src/iso/directory_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ bitflags! {

impl DirectoryTable {
pub fn read_root<R: Read + Seek>(
reader: &mut R,
reader: R,
volume: &VolumeDescriptor,
) -> Result<DirectoryTable, Error> {
Self::read(
Expand All @@ -51,7 +51,7 @@ impl DirectoryTable {
}

fn read<R: Read + Seek>(
reader: &mut R,
mut reader: R,
volume: &VolumeDescriptor,
sector: u32,
size: u32,
Expand All @@ -64,7 +64,7 @@ impl DirectoryTable {
((sector + sector_index) as u64) * volume.sector_size + volume.root_offset;
reader.seek(SeekFrom::Start(sector_position))?;

while let Some(entry) = DirectoryEntry::read(reader, volume)? {
while let Some(entry) = DirectoryEntry::read(&mut reader, volume)? {
entries.push(entry);
}
}
Expand All @@ -85,7 +85,7 @@ impl DirectoryTable {

impl DirectoryEntry {
fn read<R: Read + Seek>(
reader: &mut R,
mut reader: R,
volume: &VolumeDescriptor,
) -> Result<Option<DirectoryEntry>, Error> {
let subtree_left = reader.read_u16::<LE>()?;
Expand All @@ -103,7 +103,7 @@ impl DirectoryEntry {
let name_length = reader.read_u8()?;

let mut name = vec![0_u8; name_length as usize];
reader.take(name_length as u64).read(&mut name)?;
reader.by_ref().take(name_length as u64).read(&mut name)?;
let name = String::from_utf8_lossy(&name).into_owned();

let alignment_mismatch = ((4 - reader.stream_position()? % 4) % 4) as i64;
Expand All @@ -112,7 +112,7 @@ impl DirectoryEntry {
let is_directory = attributes.contains(DirectoryEntryAttributes::DIRECTORY);
let subdirectory = if is_directory {
let reader_position = reader.stream_position()?;
let subdir = DirectoryTable::read(reader, &volume, sector, size)?;
let subdir = DirectoryTable::read(&mut reader, &volume, sector, size)?;
reader.seek(SeekFrom::Start(reader_position))?;
Some(subdir)
} else {
Expand Down
12 changes: 6 additions & 6 deletions src/iso/iso_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,30 @@ impl IsoType {
}
}

pub fn read<R: Read + Seek>(reader: &mut R) -> Result<Option<IsoType>, Error> {
if Self::check(reader, IsoType::Xsf)? {
pub fn read<R: Read + Seek>(mut reader: R) -> Result<Option<IsoType>, Error> {
if Self::check(&mut reader, IsoType::Xsf)? {
return Ok(Some(IsoType::Xsf));
}

if Self::check(reader, IsoType::Xgd2)? {
if Self::check(&mut reader, IsoType::Xgd2)? {
return Ok(Some(IsoType::Xgd2));
}

if Self::check(reader, IsoType::Xgd1)? {
if Self::check(&mut reader, IsoType::Xgd1)? {
return Ok(Some(IsoType::Xgd1));
}

// original code had no extra check here, simply returning Xgd3 as fallback
// https://github.com/eliecharra/iso2god-cli/blob/a3b266a5/Chilano/Xbox360/Iso/GDF.cs#L268

if Self::check(reader, IsoType::Xgd3)? {
if Self::check(&mut reader, IsoType::Xgd3)? {
return Ok(Some(IsoType::Xgd3));
}

Ok(None)
}

fn check<R: Read + Seek>(reader: &mut R, iso_type: IsoType) -> Result<bool, Error> {
fn check<R: Read + Seek>(mut reader: R, iso_type: IsoType) -> Result<bool, Error> {
let mut buf = [0_u8; 20];

reader.seek(SeekFrom::Start(0x20 * SECTOR_SIZE + iso_type.root_offset()))?;
Expand Down
7 changes: 4 additions & 3 deletions src/iso/volume_descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ pub struct VolumeDescriptor {
}

impl VolumeDescriptor {
pub fn read<R: Read + Seek>(reader: &mut R) -> Result<VolumeDescriptor, Error> {
let iso_type = IsoType::read(reader)?.ok_or_else(|| format_err!("invalid ISO format"))?;
pub fn read<R: Read + Seek>(mut reader: R) -> Result<VolumeDescriptor, Error> {
let iso_type =
IsoType::read(&mut reader)?.ok_or_else(|| format_err!("invalid ISO format"))?;
Self::read_of_type(reader, iso_type)
}

fn read_of_type<R: Read + Seek>(
reader: &mut R,
mut reader: R,
iso_type: IsoType,
) -> Result<VolumeDescriptor, Error> {
reader.seek(SeekFrom::Start(0x20 * SECTOR_SIZE + iso_type.root_offset()))?;
Expand Down

0 comments on commit 511a989

Please sign in to comment.