[qmc2/rc4] fix: match QMC2 decoder logic

This commit is contained in:
鲁树人 2024-09-15 15:41:19 +01:00
parent f75a3ccb34
commit 6a56cc8433

View File

@ -31,8 +31,9 @@ impl QMC2RC4 {
let n = self.key.len(); let n = self.key.len();
for (datum, offset) in data.iter_mut().zip(offset..) { for (datum, offset) in data.iter_mut().zip(offset..) {
let idx = get_segment_key(offset as u64, self.key[offset % n], self.hash) as usize; let idx = get_segment_key(offset as u64, self.key[offset % n], self.hash);
*datum ^= self.key[idx % n]; let idx = idx % (n as u64);
*datum ^= self.key[idx as usize];
} }
} }
@ -43,10 +44,11 @@ impl QMC2RC4 {
let block_offset = offset % OTHER_SEGMENT_SIZE; let block_offset = offset % OTHER_SEGMENT_SIZE;
let seed = self.key[id % n]; let seed = self.key[id % n];
let skip = get_segment_key(id as u64, seed, self.hash) % 512; let skip = get_segment_key(id as u64, seed, self.hash);
let skip = (skip & 0x1FF) as usize;
debug_assert!(data.len() <= OTHER_SEGMENT_SIZE - block_offset); debug_assert!(data.len() <= OTHER_SEGMENT_SIZE - block_offset);
let key_stream = self.key_stream.iter().skip(skip as usize + block_offset); let key_stream = self.key_stream.iter().skip(skip + block_offset);
for (datum, &key) in data.iter_mut().zip(key_stream) { for (datum, &key) in data.iter_mut().zip(key_stream) {
*datum ^= key; *datum ^= key;
} }