[wasm] feat: export ncm

This commit is contained in:
鲁树人 2024-09-14 18:40:59 +01:00
parent bc4109d0c0
commit 239567a5b1
4 changed files with 65 additions and 0 deletions

1
Cargo.lock generated
View File

@ -447,6 +447,7 @@ dependencies = [
"console_error_panic_hook", "console_error_panic_hook",
"getrandom", "getrandom",
"umc_kuwo", "umc_kuwo",
"umc_ncm",
"umc_qmc", "umc_qmc",
"wasm-bindgen", "wasm-bindgen",
"wasm-bindgen-test", "wasm-bindgen-test",

View File

@ -24,6 +24,7 @@ getrandom = { version = "0.2", features = ["js"] }
# code size when deploying. # code size when deploying.
console_error_panic_hook = { version = "0.1.7", optional = true } console_error_panic_hook = { version = "0.1.7", optional = true }
umc_kuwo = { path = "../um_crypto/kuwo" } umc_kuwo = { path = "../um_crypto/kuwo" }
umc_ncm = { path = "../um_crypto/ncm" }
umc_qmc = { path = "../um_crypto/qmc" } umc_qmc = { path = "../um_crypto/qmc" }
[dev-dependencies] [dev-dependencies]

View File

@ -1,2 +1,3 @@
pub mod kuwo; pub mod kuwo;
pub mod ncm;
pub mod qmc; pub mod qmc;

View File

@ -0,0 +1,62 @@
use umc_ncm::header::NCMFile;
use umc_ncm::NetEaseCryptoError;
use wasm_bindgen::prelude::wasm_bindgen;
use wasm_bindgen::JsError;
/// QMC Footer.
#[wasm_bindgen(js_name=NCMFile)]
pub struct JsNCMFile {
ncm: Option<NCMFile>,
}
#[wasm_bindgen(js_class=NCMFile)]
impl JsNCMFile {
/// Open NCM file.
/// If everything is ok, return `0`.
///
/// # Arguments
///
/// * `header`: Header bytes of NCM file.
///
/// returns: Result<usize, JsError>
///
/// If it needs more bytes, the new header size will be returned.
/// If the header was large enough, it will return 0.
pub fn open(&mut self, header: &[u8]) -> Result<usize, JsError> {
match NCMFile::new(header) {
Ok(ncm) => {
self.ncm = Some(ncm);
Ok(0)
}
Err(NetEaseCryptoError::HeaderTooSmall(n)) => Ok(n),
Err(err) => Err(JsError::new(err.to_string().as_str())),
}
}
/// Decrypt buffer.
///
/// # Arguments
///
/// * `buffer`: Buffer to decrypt.
/// * `offset`: Offset (start from 0, of encrypted binary data)
///
/// returns: Result<(), JsError>
pub fn decrypt(&self, buffer: &mut [u8], offset: usize) -> Result<(), JsError> {
if let Some(ncm) = &self.ncm {
ncm.decrypt(buffer, offset);
Ok(())
} else {
Err(JsError::new("NCMFile not initialized."))
}
}
/// Get audio data offset.
#[wasm_bindgen(getter, js_name=audioOffset)]
pub fn get_audio_offset(&self) -> Result<usize, JsError> {
if let Some(ncm) = &self.ncm {
Ok(ncm.audio_data_offset)
} else {
Err(JsError::new("NCMFile not initialized."))
}
}
}