[wasm/kgm] feat #2: add kgm glue

This commit is contained in:
鲁树人 2024-09-16 21:28:07 +01:00
parent 75ebe1e631
commit adabbbd27e
4 changed files with 40 additions and 0 deletions

1
Cargo.lock generated
View File

@ -485,6 +485,7 @@ dependencies = [
"console_error_panic_hook",
"getrandom",
"um_audio",
"umc_kgm",
"umc_kuwo",
"umc_ncm",
"umc_qmc",

View File

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

View File

@ -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<JsKuGouHeader, JsError> {
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<JsKuGouDecipher, JsError> {
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)
}
}

View File

@ -1,4 +1,5 @@
pub mod audio;
pub mod kgm;
pub mod kuwo;
pub mod ncm;
pub mod qmc;