From adabbbd27e1f5f5a30568852fc661214706d7f1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=B2=81=E6=A0=91=E4=BA=BA?= Date: Mon, 16 Sep 2024 21:28:07 +0100 Subject: [PATCH] [wasm/kgm] feat #2: add kgm glue --- Cargo.lock | 1 + um_wasm/Cargo.toml | 1 + um_wasm/src/exports/kgm.rs | 37 +++++++++++++++++++++++++++++++++++++ um_wasm/src/exports/mod.rs | 1 + 4 files changed, 40 insertions(+) create mode 100644 um_wasm/src/exports/kgm.rs diff --git a/Cargo.lock b/Cargo.lock index 468c733..a64a0bd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -485,6 +485,7 @@ dependencies = [ "console_error_panic_hook", "getrandom", "um_audio", + "umc_kgm", "umc_kuwo", "umc_ncm", "umc_qmc", diff --git a/um_wasm/Cargo.toml b/um_wasm/Cargo.toml index 82adb91..4a09d40 100644 --- a/um_wasm/Cargo.toml +++ b/um_wasm/Cargo.toml @@ -23,6 +23,7 @@ getrandom = { version = "0.2", features = ["js"] } # all the `std::fmt` and `std::panicking` infrastructure, so isn't great for # code size when deploying. console_error_panic_hook = { version = "0.1.7", optional = true } +umc_kgm = { path = "../um_crypto/kgm" } umc_kuwo = { path = "../um_crypto/kuwo" } umc_ncm = { path = "../um_crypto/ncm" } umc_qmc = { path = "../um_crypto/qmc" } diff --git a/um_wasm/src/exports/kgm.rs b/um_wasm/src/exports/kgm.rs new file mode 100644 index 0000000..18407c6 --- /dev/null +++ b/um_wasm/src/exports/kgm.rs @@ -0,0 +1,37 @@ +use umc_kgm::{header::Header, Decipher}; +use wasm_bindgen::prelude::wasm_bindgen; +use wasm_bindgen::JsError; + +/// KuGou KGM file header. +#[wasm_bindgen(js_name=KuGouHeader)] +pub struct JsKuGouHeader(Header); + +#[wasm_bindgen(js_class = KuGouHeader)] +impl JsKuGouHeader { + /// Parse the KuGou header (0x400 bytes) + pub fn parse(header: &[u8]) -> Result { + Ok(JsKuGouHeader( + Header::from_buffer(header).map_err(JsError::from)?, + )) + } +} + +/// KuGou KGM file decipher. +#[wasm_bindgen(js_name=KuGouDecipher)] +pub struct JsKuGouDecipher(Decipher); + +#[wasm_bindgen(js_class=KuGouDecipher)] +impl JsKuGouDecipher { + /// Create an instance of cipher (decipher) for decryption + #[wasm_bindgen(constructor)] + pub fn new(header: &JsKuGouHeader) -> Result { + Ok(JsKuGouDecipher( + Decipher::new(&header.0).map_err(JsError::from)?, + )) + } + + /// Decrypt a buffer. + 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 f1e7fa6..2d7a5dc 100644 --- a/um_wasm/src/exports/mod.rs +++ b/um_wasm/src/exports/mod.rs @@ -1,4 +1,5 @@ pub mod audio; +pub mod kgm; pub mod kuwo; pub mod ncm; pub mod qmc;