From aeaa7ef76cd97e1e67044ca808a7db214067e92e Mon Sep 17 00:00:00 2001 From: Brian Bowman Date: Thu, 21 Jul 2022 17:46:06 -0500 Subject: [PATCH] Don't throw C++ exceptions into Rust --- src-tauri/mhycrypto/metadata.cpp | 22 ++++++++++-- src-tauri/mhycrypto/metadata.h | 4 +-- src-tauri/src/metadata_patcher.rs | 57 +++++++++---------------------- 3 files changed, 39 insertions(+), 44 deletions(-) diff --git a/src-tauri/mhycrypto/metadata.cpp b/src-tauri/mhycrypto/metadata.cpp index 392112b..d40f358 100644 --- a/src-tauri/mhycrypto/metadata.cpp +++ b/src-tauri/mhycrypto/metadata.cpp @@ -53,7 +53,7 @@ bool gen_global_metadata_key(uint8_t* src, size_t srcn) { return true; } -extern "C" void decrypt_global_metadata(uint8_t *data, size_t size) { +void decrypt_global_metadata_inner(uint8_t *data, size_t size) { uint8_t longkey[0xB00]; uint8_t longkeyp[0xB0]; uint8_t shortkey[16]; @@ -87,7 +87,16 @@ extern "C" void decrypt_global_metadata(uint8_t *data, size_t size) { recrypt_global_metadata_header_string_literals(data, size, literal_dec_key); } -extern "C" void encrypt_global_metadata(uint8_t* data, size_t size) { +extern "C" int decrypt_global_metadata(uint8_t *data, size_t size) { + try { + decrypt_global_metadata_inner(data, size); + return 0; + } catch (...) { + return -1; + } +} + +void encrypt_global_metadata_inner(uint8_t* data, size_t size) { uint8_t literal_dec_key[0x5000]; gen_global_metadata_key(data + size - 0x4000, 0x4000); @@ -126,3 +135,12 @@ extern "C" void encrypt_global_metadata(uint8_t* data, size_t size) { } } } + +extern "C" int encrypt_global_metadata(uint8_t* data, size_t size) { + try { + encrypt_global_metadata_inner(data, size); + return 0; + } catch (...) { + return -1; + } +} diff --git a/src-tauri/mhycrypto/metadata.h b/src-tauri/mhycrypto/metadata.h index f2c8c5b..d92c18d 100644 --- a/src-tauri/mhycrypto/metadata.h +++ b/src-tauri/mhycrypto/metadata.h @@ -4,7 +4,7 @@ #include #include -extern "C" void decrypt_global_metadata(uint8_t *data, size_t size); -extern "C" void encrypt_global_metadata(uint8_t *data, size_t size); +extern "C" int decrypt_global_metadata(uint8_t *data, size_t size); +extern "C" int encrypt_global_metadata(uint8_t *data, size_t size); #endif //METADATA_H diff --git a/src-tauri/src/metadata_patcher.rs b/src-tauri/src/metadata_patcher.rs index d551330..7965676 100644 --- a/src-tauri/src/metadata_patcher.rs +++ b/src-tauri/src/metadata_patcher.rs @@ -4,29 +4,10 @@ use std::fs::OpenOptions; use std::io::Read; use std::io::Write; +// For these two functions, a non-zero return value indicates failure. extern "C" { - fn decrypt_global_metadata(data: *mut u8, size: u64); - fn encrypt_global_metadata(data: *mut u8, size: u64); -} - -fn dll_decrypt_global_metadata( - data: *mut u8, - size: u64, -) -> Result> { - unsafe { - decrypt_global_metadata(data, size); - Ok(true) - } -} - -fn dll_encrypt_global_metadata( - data: *mut u8, - size: u64, -) -> Result> { - unsafe { - encrypt_global_metadata(data, size); - Ok(true) - } + fn decrypt_global_metadata(data: *mut u8, size: usize) -> i32; + fn encrypt_global_metadata(data: *mut u8, size: usize) -> i32; } #[tauri::command] @@ -89,15 +70,13 @@ fn decrypt_metadata(file_path: &str) -> Vec { } // Decrypt metadata file - match dll_decrypt_global_metadata(data.as_mut_ptr(), data.len().try_into().unwrap()) { - Ok(_) => { - println!("Successfully decrypted global-metadata"); - data - } - Err(e) => { - println!("Failed to decrypt global-metadata: {}", e); - Vec::new() - } + let success = unsafe { decrypt_global_metadata(data.as_mut_ptr(), data.len()) } == 0; + if success { + println!("Successfully decrypted global-metadata"); + data + } else { + println!("Failed to decrypt global-metadata"); + Vec::new() } } @@ -150,15 +129,13 @@ fn replace_rsa_key(old_data: &str, to_replace: &str, file_name: &str) -> String fn encrypt_metadata(old_data: &[u8]) -> Vec { let mut data = old_data.to_vec(); - match dll_encrypt_global_metadata(data.as_mut_ptr(), data.len().try_into().unwrap()) { - Ok(_) => { - println!("Successfully encrypted global-metadata"); - data - } - Err(e) => { - println!("Failed to encrypt global-metadata: {}", e); - Vec::new() - } + let success = unsafe { encrypt_global_metadata(data.as_mut_ptr(), data.len()) } == 0; + if success { + println!("Successfully encrypted global-metadata"); + data + } else { + println!("Failed to encrypt global-metadata"); + Vec::new() } }