mirror of
https://git.um-react.app/um/lib_um_crypto_rust.git
synced 2026-03-08 04:29:54 +00:00
[wasm] feat: export ncm
This commit is contained in:
@@ -1,2 +1,3 @@
|
||||
pub mod kuwo;
|
||||
pub mod ncm;
|
||||
pub mod qmc;
|
||||
|
||||
62
um_wasm/src/exports/ncm.rs
Normal file
62
um_wasm/src/exports/ncm.rs
Normal 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."))
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user