[wasm/kgm] refactor: simplify kgm interface

This commit is contained in:
鲁树人 2024-09-18 22:02:18 +01:00
parent 38a770161a
commit 4ca1bfe2c8

View File

@ -2,32 +2,18 @@ use umc_kgm::{header::Header, Decipher};
use wasm_bindgen::prelude::wasm_bindgen; use wasm_bindgen::prelude::wasm_bindgen;
use wasm_bindgen::JsError; 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. /// KuGou KGM file decipher.
#[wasm_bindgen(js_name=KuGouDecipher)] #[wasm_bindgen(js_name=KuGou)]
pub struct JsKuGouDecipher(Decipher); pub struct JsKuGou(Decipher);
#[wasm_bindgen(js_class=KuGouDecipher)] #[wasm_bindgen(js_class = KuGou)]
impl JsKuGouDecipher { impl JsKuGou {
/// Create an instance of cipher (decipher) for decryption /// Parse the KuGou header (0x400 bytes)
#[wasm_bindgen(constructor)] pub fn from_header(header: &[u8]) -> Result<JsKuGou, JsError> {
pub fn new(header: &JsKuGouHeader) -> Result<JsKuGouDecipher, JsError> { let header = Header::from_buffer(header).map_err(JsError::from)?;
Ok(JsKuGouDecipher( let decipher = Decipher::new(&header).map_err(JsError::from)?;
Decipher::new(&header.0).map_err(JsError::from)?,
)) Ok(JsKuGou(decipher))
} }
/// Decrypt a buffer. /// Decrypt a buffer.