mirror of
https://github.com/Grasscutters/Cultivation.git
synced 2025-01-08 12:07:45 +08:00
Don't throw C++ exceptions into Rust
This commit is contained in:
parent
7659e9831a
commit
aeaa7ef76c
@ -53,7 +53,7 @@ bool gen_global_metadata_key(uint8_t* src, size_t srcn) {
|
|||||||
return true;
|
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 longkey[0xB00];
|
||||||
uint8_t longkeyp[0xB0];
|
uint8_t longkeyp[0xB0];
|
||||||
uint8_t shortkey[16];
|
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);
|
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];
|
uint8_t literal_dec_key[0x5000];
|
||||||
|
|
||||||
gen_global_metadata_key(data + size - 0x4000, 0x4000);
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
|
|
||||||
extern "C" void decrypt_global_metadata(uint8_t *data, size_t size);
|
extern "C" int decrypt_global_metadata(uint8_t *data, size_t size);
|
||||||
extern "C" void encrypt_global_metadata(uint8_t *data, size_t size);
|
extern "C" int encrypt_global_metadata(uint8_t *data, size_t size);
|
||||||
|
|
||||||
#endif //METADATA_H
|
#endif //METADATA_H
|
||||||
|
@ -4,29 +4,10 @@ use std::fs::OpenOptions;
|
|||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
|
|
||||||
|
// For these two functions, a non-zero return value indicates failure.
|
||||||
extern "C" {
|
extern "C" {
|
||||||
fn decrypt_global_metadata(data: *mut u8, size: u64);
|
fn decrypt_global_metadata(data: *mut u8, size: usize) -> i32;
|
||||||
fn encrypt_global_metadata(data: *mut u8, size: u64);
|
fn encrypt_global_metadata(data: *mut u8, size: usize) -> i32;
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
@ -89,15 +70,13 @@ fn decrypt_metadata(file_path: &str) -> Vec<u8> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Decrypt metadata file
|
// Decrypt metadata file
|
||||||
match dll_decrypt_global_metadata(data.as_mut_ptr(), data.len().try_into().unwrap()) {
|
let success = unsafe { decrypt_global_metadata(data.as_mut_ptr(), data.len()) } == 0;
|
||||||
Ok(_) => {
|
if success {
|
||||||
println!("Successfully decrypted global-metadata");
|
println!("Successfully decrypted global-metadata");
|
||||||
data
|
data
|
||||||
}
|
} else {
|
||||||
Err(e) => {
|
println!("Failed to decrypt global-metadata");
|
||||||
println!("Failed to decrypt global-metadata: {}", e);
|
Vec::new()
|
||||||
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> {
|
fn encrypt_metadata(old_data: &[u8]) -> Vec<u8> {
|
||||||
let mut data = old_data.to_vec();
|
let mut data = old_data.to_vec();
|
||||||
match dll_encrypt_global_metadata(data.as_mut_ptr(), data.len().try_into().unwrap()) {
|
let success = unsafe { encrypt_global_metadata(data.as_mut_ptr(), data.len()) } == 0;
|
||||||
Ok(_) => {
|
if success {
|
||||||
println!("Successfully encrypted global-metadata");
|
println!("Successfully encrypted global-metadata");
|
||||||
data
|
data
|
||||||
}
|
} else {
|
||||||
Err(e) => {
|
println!("Failed to encrypt global-metadata");
|
||||||
println!("Failed to encrypt global-metadata: {}", e);
|
Vec::new()
|
||||||
Vec::new()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user