perf: add back zero-copy decode for mai 1.50

This commit is contained in:
mokurin000
2025-07-31 12:36:59 +08:00
parent ca4761f83a
commit f2d0daf60d
2 changed files with 7 additions and 20 deletions

View File

@@ -12,8 +12,8 @@ use crate::error::ApiError;
use crate::title::{MaiVersion, MaiVersionExt, Sdgb1_40, Sdgb1_50};
impl MaiVersionExt for Sdgb1_40 {
fn decode(data: impl AsRef<[u8]>) -> Result<Vec<u8>, ApiError> {
let mut decompressed = decompress(data.as_ref());
fn decode(mut data: impl AsMut<[u8]>) -> Result<Vec<u8>, ApiError> {
let mut decompressed = decompress(data.as_mut());
if decompressed.is_empty() {
return Err(ApiError::EmptyResponse);
}
@@ -41,19 +41,13 @@ impl MaiVersionExt for Sdgb1_40 {
}
impl MaiVersionExt for Sdgb1_50 {
fn decode(data: impl AsRef<[u8]>) -> Result<Vec<u8>, ApiError> {
let mut data = data.as_ref().to_vec();
if data.is_empty() {
fn decode(mut data: impl AsMut<[u8]>) -> Result<Vec<u8>, ApiError> {
if data.as_mut().is_empty() {
return Err(ApiError::EmptyResponse);
}
if data.len() % 16 != 0 {
let pad = 16 - (data.len() % 16);
data.resize(data.len() + pad, pad as _);
}
debug!("data size: {}", data.len());
let decrypted = decrypt_vec(&data, Self::AES_KEY, Self::AES_IV)?;
debug!("data size: {}", data.as_mut().len());
let decrypted = decrypt(&mut data, Self::AES_KEY, Self::AES_IV)?;
Ok(decompress(decrypted))
}
@@ -111,13 +105,6 @@ fn decrypt<'ct>(
Ok(result)
}
fn decrypt_vec(data: impl AsRef<[u8]>, key: &[u8; 32], iv: &[u8; 16]) -> Result<Vec<u8>, ApiError> {
let key = GenericArray::from_slice(key);
let iv = GenericArray::from_slice(iv);
let decryptor = Aes256CbcDec::new(key, iv);
Ok(decryptor.decrypt_padded_vec_mut::<Pkcs7>(data.as_ref())?)
}
#[cfg(test)]
mod _tests {

View File

@@ -25,7 +25,7 @@ pub trait MaiVersion {
pub trait MaiVersionExt: MaiVersion {
fn encode(data: impl AsRef<[u8]>) -> Result<Vec<u8>, ApiError>;
fn decode(data: impl AsRef<[u8]>) -> Result<Vec<u8>, ApiError>;
fn decode(data: impl AsMut<[u8]>) -> Result<Vec<u8>, ApiError>;
fn api_hash(api: APIMethod) -> String {
let api_name: &str = api.into();