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;