From 87d1d8152a72de209fa72c81f6c359238024dffa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=B2=81=E6=A0=91=E4=BA=BA?= Date: Fri, 20 Sep 2024 23:22:04 +0100 Subject: [PATCH] [wasm/mg3d] feat #3: expose mg3d to wasm glue --- Cargo.lock | 1 + um_wasm/Cargo.toml | 1 + um_wasm/src/exports/mg3d.rs | 30 ++++++++++++++++++++++++++++++ um_wasm/src/exports/mod.rs | 1 + 4 files changed, 33 insertions(+) create mode 100644 um_wasm/src/exports/mg3d.rs diff --git a/Cargo.lock b/Cargo.lock index 9fe22fc..6ac8607 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -555,6 +555,7 @@ dependencies = [ "umc_joox", "umc_kgm", "umc_kuwo", + "umc_mg3d", "umc_ncm", "umc_qmc", "umc_qtfm", diff --git a/um_wasm/Cargo.toml b/um_wasm/Cargo.toml index fc8cd09..29d65f0 100644 --- a/um_wasm/Cargo.toml +++ b/um_wasm/Cargo.toml @@ -26,6 +26,7 @@ console_error_panic_hook = { version = "0.1.7", optional = true } umc_joox = { path = "../um_crypto/joox" } umc_kgm = { path = "../um_crypto/kgm" } umc_kuwo = { path = "../um_crypto/kuwo" } +umc_mg3d = { path = "../um_crypto/mg3d" } umc_ncm = { path = "../um_crypto/ncm" } umc_qmc = { path = "../um_crypto/qmc" } umc_qtfm = { path = "../um_crypto/qtfm" } diff --git a/um_wasm/src/exports/mg3d.rs b/um_wasm/src/exports/mg3d.rs new file mode 100644 index 0000000..fbe8d25 --- /dev/null +++ b/um_wasm/src/exports/mg3d.rs @@ -0,0 +1,30 @@ +use umc_mg3d::{guess_key, Decipher}; +use wasm_bindgen::prelude::wasm_bindgen; +use wasm_bindgen::JsError; + +/// Migu3D MG3D file decipher. +#[wasm_bindgen(js_name=Migu3D)] +pub struct JsMigu3D(Decipher); + +#[wasm_bindgen(js_class=Migu3D)] +impl JsMigu3D { + /// Create a new decipher and guess its key from first 0x100 bytes. + #[wasm_bindgen(js_name=fromHeader)] + pub fn from_header(header: &[u8]) -> Result { + let key = guess_key(header).ok_or_else(|| JsError::new("failed to guess key"))?; + let decipher = Decipher::new_from_final_key(&key)?; + Ok(JsMigu3D(decipher)) + } + + /// Create a new decipher from file_key + #[wasm_bindgen(js_name=fromFileKey)] + pub fn from_file_key(file_key: &str) -> Result { + let decipher = Decipher::new_from_file_key(file_key)?; + Ok(JsMigu3D(decipher)) + } + + /// Decrypt encrypted buffer part. + pub fn decrypt(&self, buffer: &mut [u8], offset: usize) { + self.0.decrypt(buffer, offset) + } +} diff --git a/um_wasm/src/exports/mod.rs b/um_wasm/src/exports/mod.rs index e0642c7..8dc3aae 100644 --- a/um_wasm/src/exports/mod.rs +++ b/um_wasm/src/exports/mod.rs @@ -2,6 +2,7 @@ pub mod audio; pub mod joox; pub mod kgm; pub mod kuwo; +pub mod mg3d; pub mod ncm; pub mod qmc; pub mod qtfm;