Don't throw C++ exceptions into Rust

This commit is contained in:
Brian Bowman 2022-07-21 17:46:06 -05:00
parent 7659e9831a
commit aeaa7ef76c
3 changed files with 39 additions and 44 deletions

View File

@ -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;
}
}

View File

@ -4,7 +4,7 @@
#include <cstdint>
#include <cstdlib>
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

View File

@ -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<bool, Box<dyn std::error::Error>> {
unsafe {
decrypt_global_metadata(data, size);
Ok(true)
}
}
fn dll_encrypt_global_metadata(
data: *mut u8,
size: u64,
) -> Result<bool, Box<dyn std::error::Error>> {
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<u8> {
}
// 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<u8> {
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()
}
}