Skip to content

Commit

Permalink
ipl3checksum_cickind_from_hash_md5 and ipl3checksum_cickind_from_name
Browse files Browse the repository at this point in the history
  • Loading branch information
AngheloAlf committed Dec 16, 2023
1 parent 67de1a0 commit c0c2af6
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 17 deletions.
2 changes: 1 addition & 1 deletion bindings/c/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ endif
$(shell cargo build --lib --features c_bindings $(CARGO_FLAGS))


%.elf: %.c $(LIB)
%.elf: %.c $(LIB) tests/utils.c
$(CC) $(CSTD) $(CFLAGS) $(IINC) $(WARNINGS) -o $@ tests/utils.c $< -L ../../target/$(BUILD_MODE) -Wl,-Bstatic -l ipl3checksum -Wl,-Bdynamic

# Print target for debugging
Expand Down
4 changes: 2 additions & 2 deletions bindings/c/include/ipl3checksum/cickinds.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ uint32_t ipl3checksum_cickind_get_magic(Ipl3Checksum_CICKind self);

// const char *ipl3checksum_cickind_get_hash_md5(Ipl3Checksum_CICKind self);

// Ipl3Checksum_Error ipl3checksum_cickind_from_hash_md5(Ipl3Checksum_CICKind *kind_dst, const char *hash_str);
Ipl3Checksum_Error ipl3checksum_cickind_from_hash_md5(Ipl3Checksum_CICKind *kind_dst, const char *hash_str);

// const char *ipl3checksum_cickind_get_name(Ipl3Checksum_CICKind self);

// Ipl3Checksum_Error ipl3checksum_cickind_from_name(Ipl3Checksum_CICKind *kind_dst, const char *name);
Ipl3Checksum_Error ipl3checksum_cickind_from_name(Ipl3Checksum_CICKind *kind_dst, const char *name);

Ipl3Checksum_Error ipl3checksum_cickind_from_value(Ipl3Checksum_CICKind *kind_dst, size_t value);

Expand Down
1 change: 1 addition & 0 deletions bindings/c/include/ipl3checksum/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ typedef enum Ipl3Checksum_Error_Tag {
Ipl3Checksum_Error_BufferNotBigEnough,
Ipl3Checksum_Error_BufferSizeIsWrong,
Ipl3Checksum_Error_UnableToDetectCIC,
Ipl3Checksum_Error_StringConversion,
} Ipl3Checksum_Error_Tag;

typedef struct Ipl3Checksum_Error {
Expand Down
28 changes: 24 additions & 4 deletions bindings/c/tests/test_checksum.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ void print_usage(int argc, char *argv[]) {
int main(int argc, char *argv[]) {
int ret = 0;

if (argc < 2) {
if (argc < 3) {
print_usage(argc, argv);
return -1;
}
Expand All @@ -32,9 +32,23 @@ int main(int argc, char *argv[]) {
assert(bin_size > 0);
assert(bin != NULL);

fprintf(stderr, "CIC kind: '%s'\n", cic_kind_name);
// TODO: Don't hardcode
Ipl3Checksum_CICKind kind = Ipl3Checksum_CICKind_CIC_6102_7101;
fprintf(stderr, "Passed CIC kind: '%s'\n", cic_kind_name);
Ipl3Checksum_CICKind kind;
{
Ipl3Checksum_Error err = ipl3checksum_cickind_from_name(&kind, cic_kind_name);

if (err.tag == Ipl3Checksum_Error_Okay) {
fprintf(stderr, "Parsed kind: '%i'\n", kind);
} else {
fprintf(stderr, "Passed CIC kind was not valid: %s\n", get_ipl3checksum_error_str(err));
goto cleanup;
}
}

uint32_t expected_checksum0 = read_be_word(bin, 0x10);
uint32_t expected_checksum1 = read_be_word(bin, 0x14);

fprintf(stderr, "Expected checksum: %08X %08X\n", expected_checksum0, expected_checksum1);

{
uint32_t checksum0;
Expand All @@ -44,11 +58,17 @@ int main(int argc, char *argv[]) {

if (err.tag == Ipl3Checksum_Error_Okay) {
fprintf(stderr, "Computed checksum: %08X %08X\n", checksum0, checksum1);
if ((checksum0 == expected_checksum0) && (checksum1 == expected_checksum1)) {
fprintf(stderr, "Checksum matches\n");
} else {
fprintf(stderr, "Checksum doesn't match\n");
}
} else {
fprintf(stderr, "Error trying to compute the checksum: %s\n", get_ipl3checksum_error_str(err));
}
}

cleanup:
free(bin);

return ret;
Expand Down
2 changes: 1 addition & 1 deletion bindings/c/tests/test_detect.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ void print_usage(int argc, char *argv[]) {
int main(int argc, char *argv[]) {
int ret = 0;

if (argc < 1) {
if (argc < 2) {
print_usage(argc, argv);
return -1;
}
Expand Down
4 changes: 4 additions & 0 deletions bindings/c/tests/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ uint8_t *read_binary_file(const char *path, size_t *size) {
return data;
}

uint32_t read_be_word(const uint8_t *src, size_t offset) {
return (src[offset] << 24) | (src[offset+1] << 16) | (src[offset+2] << 8) | (src[offset+3] << 0);
}

const char *const ipl3checksum_error_str[] = {
[Ipl3Checksum_Error_Okay] = "Okay",
Expand All @@ -50,6 +53,7 @@ const char *const ipl3checksum_error_str[] = {
[Ipl3Checksum_Error_BufferNotBigEnough] = "BufferNotBigEnough",
[Ipl3Checksum_Error_BufferSizeIsWrong] = "BufferSizeIsWrong",
[Ipl3Checksum_Error_UnableToDetectCIC] = "UnableToDetectCIC",
[Ipl3Checksum_Error_StringConversion] = "StringConversion",
};

const char *get_ipl3checksum_error_str(Ipl3Checksum_Error error) {
Expand Down
2 changes: 2 additions & 0 deletions bindings/c/tests/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

uint8_t *read_binary_file(const char *path, size_t *size);

uint32_t read_be_word(const uint8_t *src, size_t offset);

const char *get_ipl3checksum_error_str(Ipl3Checksum_Error error);

#endif
50 changes: 41 additions & 9 deletions src/rs/cickinds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ mod python_bindings {

#[cfg(feature = "c_bindings")]
mod c_bindings {
use crate::{CICKind, Ipl3ChecksumError};
use crate::{CICKind, Ipl3ChecksumError, utils};

#[no_mangle]
pub extern "C" fn ipl3checksum_cickind_get_seed(
Expand All @@ -168,13 +168,29 @@ mod c_bindings {
}
*/

/*
#[no_mangle]
pub extern "C" fn ipl3checksum_cickind_from_hash_md5(
kind: CICKind
) -> {
kind_dst: *mut CICKind,
hash_str: *const core::ffi::c_char
) -> Ipl3ChecksumError {
if kind_dst.is_null() || hash_str.is_null() {
return Ipl3ChecksumError::NullPointer;
}

let hash = match utils::static_str_from_c_string(hash_str) {
Err(e) => return e,
Ok(h) => h,
};

let kind = match CICKind::from_hash_md5(hash) {
Some(k) => k,
None => return Ipl3ChecksumError::UnableToDetectCIC,
};

unsafe { *kind_dst = kind };

Ipl3ChecksumError::Okay
}
*/

/*
#[no_mangle]
Expand All @@ -184,13 +200,29 @@ mod c_bindings {
}
*/

/*
#[no_mangle]
pub extern "C" fn ipl3checksum_cickind_from_name(
kind: CICKind
) -> {
kind_dst: *mut CICKind,
c_name: *const core::ffi::c_char
) -> Ipl3ChecksumError {
if kind_dst.is_null() || c_name.is_null() {
return Ipl3ChecksumError::NullPointer;
}

let name = match utils::static_str_from_c_string(c_name) {
Err(e) => return e,
Ok(h) => h,
};

let kind = match CICKind::from_name(name) {
Some(k) => k,
None => return Ipl3ChecksumError::UnableToDetectCIC,
};

unsafe { *kind_dst = kind };

Ipl3ChecksumError::Okay
}
*/

#[no_mangle]
pub extern "C" fn ipl3checksum_cickind_from_value(
Expand Down
2 changes: 2 additions & 0 deletions src/rs/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ pub enum Ipl3ChecksumError {
},
#[error("Unable to detect CIC variant")]
UnableToDetectCIC,
#[error("Failed to convert a FFI string")]
StringConversion,
}

#[cfg(feature = "python_bindings")]
Expand Down
10 changes: 10 additions & 0 deletions src/rs/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,13 @@ pub(crate) fn u8_vec_from_pointer_array(

Ok(bytes)
}

#[cfg(feature = "c_bindings")]
pub(crate) fn static_str_from_c_string(c_str: *const core::ffi::c_char) -> Result<&'static str, Ipl3ChecksumError> {
let converted = unsafe { std::ffi::CStr::from_ptr(c_str) };

match converted.to_str() {
Err(_) => Err(Ipl3ChecksumError::StringConversion),
Ok(s) => Ok(s),
}
}

0 comments on commit c0c2af6

Please sign in to comment.