Skip to content

Commit

Permalink
fmt and clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
AngheloAlf committed Dec 16, 2023
1 parent f595339 commit 6c7f557
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 26 deletions.
29 changes: 23 additions & 6 deletions src/rs/checksum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,15 @@ fn read_word_from_ram(rom_words: &[u32], entrypoint_ram: u32, ram_addr: u32) ->
/// let checksum = ipl3checksum::calculate_checksum(&bytes, &kind).unwrap();
/// println!("{:08X} {:08X}", checksum.0, checksum.1);
/// ```
pub fn calculate_checksum(rom_bytes: &[u8], kind: &CICKind) -> Result<(u32, u32), Ipl3ChecksumError> {
pub fn calculate_checksum(
rom_bytes: &[u8],
kind: &CICKind,
) -> Result<(u32, u32), Ipl3ChecksumError> {
if rom_bytes.len() < 0x101000 {
return Err(Ipl3ChecksumError::BufferNotBigEnough{buffer_len: rom_bytes.len(), expected_len: 0x101000});
return Err(Ipl3ChecksumError::BufferNotBigEnough {
buffer_len: rom_bytes.len(),
expected_len: 0x101000,
});
}

let rom_words = utils::read_u32_vec(rom_bytes, 0, 0x101000 / 4)?;
Expand Down Expand Up @@ -300,22 +306,33 @@ pub(crate) mod python_bindings {
use pyo3::prelude::*;

#[pyfunction]
pub(crate) fn calculateChecksum(rom_bytes: &[u8], kind: &super::CICKind) -> Result<Option<(u32, u32)>, super::Ipl3ChecksumError> {
pub(crate) fn calculateChecksum(
rom_bytes: &[u8],
kind: &super::CICKind,
) -> Result<Option<(u32, u32)>, super::Ipl3ChecksumError> {
match super::calculate_checksum(rom_bytes, kind) {
Ok(checksum) => Ok(Some(checksum)),
Err(e) => match e {
super::Ipl3ChecksumError::BufferNotBigEnough { buffer_len, expected_len } => Ok(None),
super::Ipl3ChecksumError::BufferNotBigEnough {
buffer_len: _,
expected_len: _,
} => Ok(None),
_ => Err(e), // To trigger an exception on Python's side
},
}
}

#[pyfunction]
pub(crate) fn calculateChecksumAutodetect(rom_bytes: &[u8]) -> Result<Option<(u32, u32)>, super::Ipl3ChecksumError> {
pub(crate) fn calculateChecksumAutodetect(
rom_bytes: &[u8],
) -> Result<Option<(u32, u32)>, super::Ipl3ChecksumError> {
match super::calculate_checksum_autodetect(rom_bytes) {
Ok(checksum) => Ok(Some(checksum)),
Err(e) => match e {
super::Ipl3ChecksumError::BufferNotBigEnough { buffer_len, expected_len } => Ok(None),
super::Ipl3ChecksumError::BufferNotBigEnough {
buffer_len: _,
expected_len: _,
} => Ok(None),
_ => Err(e), // To trigger an exception on Python's side
},
}
Expand Down
29 changes: 21 additions & 8 deletions src/rs/detect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@ use crate::{cickinds::CICKind, error::Ipl3ChecksumError, utils};
/// * The detected CIC kind, or `Ipl3ChecksumError` if was not able to detect the CIC kind.
pub fn detect_cic_raw(raw_bytes: &[u8]) -> Result<CICKind, Ipl3ChecksumError> {
if raw_bytes.len() != 0xFC0 {
return Err(Ipl3ChecksumError::BufferSizeIsWrong{buffer_len:raw_bytes.len() , expected_len: 0xFC0});
return Err(Ipl3ChecksumError::BufferSizeIsWrong {
buffer_len: raw_bytes.len(),
expected_len: 0xFC0,
});
}

let bytes_hash = utils::get_hash_md5(raw_bytes);

match CICKind::from_hash_md5(&bytes_hash) {
Some(cic) => Ok(cic),
None => Err(Ipl3ChecksumError::UnableToDetectCIC{hash: bytes_hash}),
None => Err(Ipl3ChecksumError::UnableToDetectCIC { hash: bytes_hash }),
}
}

Expand All @@ -48,24 +51,34 @@ pub(crate) mod python_bindings {
use pyo3::prelude::*;

#[pyfunction]
pub(crate) fn detectCICRaw(raw_bytes: &[u8]) -> Result<Option<super::CICKind>, super::Ipl3ChecksumError> {
pub(crate) fn detectCICRaw(
raw_bytes: &[u8],
) -> Result<Option<super::CICKind>, super::Ipl3ChecksumError> {
match super::detect_cic_raw(raw_bytes) {
Ok(cic) => Ok(Some(cic)),
Err(e) => match e {
super::Ipl3ChecksumError::BufferSizeIsWrong { buffer_len, expected_len } => Ok(None),
super::Ipl3ChecksumError::UnableToDetectCIC { hash } => Ok(None),
super::Ipl3ChecksumError::BufferSizeIsWrong {
buffer_len: _,
expected_len: _,
} => Ok(None),
super::Ipl3ChecksumError::UnableToDetectCIC { hash: _ } => Ok(None),
_ => Err(e), // To trigger an exception on Python's side
},
}
}

#[pyfunction]
pub(crate) fn detectCIC(rom_bytes: &[u8]) -> Result<Option<super::CICKind>, super::Ipl3ChecksumError> {
pub(crate) fn detectCIC(
rom_bytes: &[u8],
) -> Result<Option<super::CICKind>, super::Ipl3ChecksumError> {
match super::detect_cic(rom_bytes) {
Ok(cic) => Ok(Some(cic)),
Err(e) => match e {
super::Ipl3ChecksumError::BufferSizeIsWrong { buffer_len, expected_len } => Ok(None),
super::Ipl3ChecksumError::UnableToDetectCIC { hash } => Ok(None),
super::Ipl3ChecksumError::BufferSizeIsWrong {
buffer_len: _,
expected_len: _,
} => Ok(None),
super::Ipl3ChecksumError::UnableToDetectCIC { hash: _ } => Ok(None),
_ => Err(e), // To trigger an exception on Python's side
},
}
Expand Down
24 changes: 16 additions & 8 deletions src/rs/error.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/* SPDX-FileCopyrightText: © 2023 Decompollaborate */
/* SPDX-License-Identifier: MIT */

use thiserror;

#[cfg(feature = "python_bindings")]
use pyo3::exceptions::PyRuntimeError;
#[cfg(feature = "python_bindings")]
Expand All @@ -15,19 +13,29 @@ pub enum Ipl3ChecksumError {
#[error("Not an error")]
Okay,
#[error("Unaligned read at offset 0x{offset:X}")]
UnalignedRead{ offset: usize },
UnalignedRead { offset: usize },
#[error("Failed to convert bytes at offset 0x{offset:X}")]
ByteConversion{ offset: usize },
ByteConversion { offset: usize },
#[error("Tried to access data out of bounds at offset 0x{offset:X}. Requested bytes: 0x{requested_bytes:X}. Buffer length: 0x{buffer_len:X}")]
OutOfBounds{offset: usize, requested_bytes: usize, buffer_len: usize},
OutOfBounds {
offset: usize,
requested_bytes: usize,
buffer_len: usize,
},
#[error("Pointer is null")]
NullPointer,
#[error("The input byte buffer is not big enough. It should be at least 0x{expected_len:X} bytes long, but it was 0x{buffer_len:X} bytes")]
BufferNotBigEnough{ buffer_len: usize, expected_len: usize },
BufferNotBigEnough {
buffer_len: usize,
expected_len: usize,
},
#[error("The input byte buffer didn't have the expected size. It should be exactly 0x{expected_len:X} bytes long, but it was 0x{buffer_len:X} bytes")]
BufferSizeIsWrong{ buffer_len: usize, expected_len: usize },
BufferSizeIsWrong {
buffer_len: usize,
expected_len: usize,
},
#[error("Unable to detect the CIC variant because the computed hash did not match any of the known variants. Computed hash: {hash}")]
UnableToDetectCIC{ hash: String },
UnableToDetectCIC { hash: String },
}

#[cfg(feature = "python_bindings")]
Expand Down
16 changes: 12 additions & 4 deletions src/rs/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,28 @@ use crate::error::Ipl3ChecksumError;

pub(crate) fn read_u32(bytes: &[u8], offset: usize) -> Result<u32, Ipl3ChecksumError> {
if offset % 4 != 0 {
return Err(Ipl3ChecksumError::UnalignedRead{ offset });
return Err(Ipl3ChecksumError::UnalignedRead { offset });
}

if offset + 4 > bytes.len() {
return Err(Ipl3ChecksumError::OutOfBounds{offset, requested_bytes: 4, buffer_len: bytes.len()});
return Err(Ipl3ChecksumError::OutOfBounds {
offset,
requested_bytes: 4,
buffer_len: bytes.len(),
});
}

match bytes[offset..offset + 4].try_into() {
Ok(bytes) => Ok(u32::from_be_bytes(bytes)),
Err(_error) => Err(Ipl3ChecksumError::ByteConversion{offset}),
Err(_error) => Err(Ipl3ChecksumError::ByteConversion { offset }),
}
}

pub(crate) fn read_u32_vec(bytes: &[u8], offset: usize, len: usize) -> Result<Vec<u32>, Ipl3ChecksumError> {
pub(crate) fn read_u32_vec(
bytes: &[u8],
offset: usize,
len: usize,
) -> Result<Vec<u32>, Ipl3ChecksumError> {
let mut ret = Vec::with_capacity(len);

for i in 0..len {
Expand Down

0 comments on commit 6c7f557

Please sign in to comment.