[kgm/ncm/kwm] refactor: improve performance for large file

This commit is contained in:
鲁树人 2024-09-15 23:25:19 +01:00
parent fd73e8b9a3
commit da806d7ad4
4 changed files with 18 additions and 30 deletions

View File

@ -15,9 +15,8 @@ impl DecipherV2 {
impl Decipher for DecipherV2 {
fn decrypt(&self, buffer: &mut [u8], offset: usize) {
let key_stream = self.key.iter().cycle().skip(offset % self.key.len());
for (datum, &k) in buffer.iter_mut().zip(key_stream) {
*datum ^= k;
for (datum, offset) in buffer.iter_mut().zip(offset..) {
*datum ^= self.key[offset % self.key.len()];
}
}
}

View File

@ -25,34 +25,27 @@ impl DecipherV3 {
Ok(Self { slot_key, file_key })
}
fn offset_key(offset: usize) -> u8 {
let offset_key = (offset as u32).to_ne_bytes();
offset_key[0] ^ offset_key[1] ^ offset_key[2] ^ offset_key[3]
}
}
impl Decipher for DecipherV3 {
fn decrypt(&self, buffer: &mut [u8], offset: usize) {
let slot_key_stream = self
.slot_key
.iter()
.cycle()
.skip(offset % self.slot_key.len());
let file_key_stream = self
.file_key
.iter()
.cycle()
.skip(offset % self.file_key.len());
for (datum, offset) in buffer.iter_mut().zip(offset..) {
let offset_key = Self::offset_key(offset);
let mut offset = offset as u32;
let key_stream = slot_key_stream.zip(file_key_stream);
for (datum, (&slot_key, &file_key)) in buffer.iter_mut().zip(key_stream) {
let offset_key = offset.to_ne_bytes().iter().fold(0, |acc, &x| acc ^ x);
let file_key = self.file_key[offset % self.file_key.len()];
let slot_key = self.slot_key[offset % self.slot_key.len()];
let mut temp = *datum;
temp ^= file_key;
temp ^= temp.wrapping_shl(4);
temp ^= temp << 4;
temp ^= slot_key;
temp ^= offset_key;
*datum = temp;
offset = offset.wrapping_add(1);
}
}
}

View File

@ -11,10 +11,8 @@ const KEY: [u8; 0x20] = [
impl CipherV1 {
pub fn new(resource_id: u32) -> Self {
let mut key = KEY;
for (k, r) in key
.iter_mut()
.zip(resource_id.to_string().as_bytes().iter().cycle())
{
let resource_id = resource_id.to_string();
for (k, &r) in key.iter_mut().zip(resource_id.as_bytes().iter().cycle()) {
*k ^= r;
}
@ -26,10 +24,9 @@ impl CipherV1 {
T: AsMut<[u8]> + ?Sized,
{
let data = data.as_mut();
let key_stream = self.key.iter().cycle().skip(offset % self.key.len());
for (datum, key) in data.iter_mut().zip(key_stream) {
*datum ^= *key;
for (datum, offset) in data.iter_mut().zip(offset..) {
*datum ^= self.key[offset % self.key.len()];
}
}
}

View File

@ -182,9 +182,8 @@ impl NCMFile {
where
T: AsMut<[u8]> + ?Sized,
{
let key_stream = self.audio_rc4_key_stream.iter().cycle().skip(offset & 0xff);
for (datum, &key) in data.as_mut().iter_mut().zip(key_stream) {
*datum ^= key;
for (datum, offset) in data.as_mut().iter_mut().zip(offset..) {
*datum ^= self.audio_rc4_key_stream[offset % self.audio_rc4_key_stream.len()];
}
}
}