mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-05-31 18:23:39 -07:00
40 lines
893 B
Rust
40 lines
893 B
Rust
use crate::xor_bytes::{XOR_LEN, XORBytes};
|
|
|
|
#[derive(Debug, Default, PartialEq, Eq, Clone, Copy)]
|
|
pub struct XORIndex(usize);
|
|
|
|
impl XORIndex {
|
|
pub fn bytes<'a>(&mut self, bytes: &'a mut [u8], xor_bytes: XORBytes) -> &'a mut [u8] {
|
|
let len = bytes.len();
|
|
let mut bytes_index = 0;
|
|
|
|
while bytes_index < len {
|
|
bytes[bytes_index] ^= xor_bytes[self.0];
|
|
self.increment();
|
|
bytes_index += 1;
|
|
}
|
|
|
|
bytes
|
|
}
|
|
|
|
#[inline]
|
|
pub fn byte(&mut self, mut byte: u8, xor_bytes: XORBytes) -> u8 {
|
|
byte ^= xor_bytes[self.0];
|
|
self.increment();
|
|
byte
|
|
}
|
|
|
|
#[inline]
|
|
pub fn increment(&mut self) {
|
|
self.0 += 1;
|
|
if self.0 == XOR_LEN {
|
|
self.0 = 0;
|
|
}
|
|
}
|
|
|
|
#[inline]
|
|
pub fn add_assign(&mut self, i: usize) {
|
|
self.0 = (self.0 + i) % XOR_LEN;
|
|
}
|
|
}
|