[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::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_name=KuGou)]
pub struct JsKuGou(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)?,
))
#[wasm_bindgen(js_class = KuGou)]
impl JsKuGou {
/// Parse the KuGou header (0x400 bytes)
pub fn from_header(header: &[u8]) -> Result<JsKuGou, JsError> {
let header = Header::from_buffer(header).map_err(JsError::from)?;
let decipher = Decipher::new(&header).map_err(JsError::from)?;
Ok(JsKuGou(decipher))
}
/// Decrypt a buffer.