use crate::{
Csa,
CsaBatch,
};
pub const PACKET_SIZE: usize = 188;
const SYNC_BYTE: u8 = 0x47;
const TSC_MASK: u8 = 0xC0;
const TSC_EVEN: u8 = 0x80;
const TSC_ODD: u8 = 0xC0;
const BATCH_MIN: usize = 4;
struct Key {
csa: Csa,
batch: CsaBatch,
}
impl Key {
fn new(cw: &[u8; 8]) -> Self {
let mut csa = Csa::default();
csa.set_cw(cw);
Self {
csa,
batch: CsaBatch::new(cw),
}
}
}
#[derive(Default)]
pub struct Descrambler {
even: Option<Key>,
odd: Option<Key>,
scratch: Vec<u8>,
index: Vec<(usize, usize)>,
blocks: Vec<u8>,
}
impl Descrambler {
pub fn new() -> Self {
Self::default()
}
pub fn set_even(&mut self, cw: &[u8; 8]) {
self.even = Some(Key::new(cw));
}
pub fn set_odd(&mut self, cw: &[u8; 8]) {
self.odd = Some(Key::new(cw));
}
pub fn descramble(&mut self, packets: &mut [u8]) {
assert!(
packets.len().is_multiple_of(PACKET_SIZE),
"buffer length must be a multiple of 188"
);
let Self {
even,
odd,
scratch,
index,
blocks,
} = self;
if let Some(key) = even {
descramble_parity(packets, TSC_EVEN, key, scratch, index, blocks);
}
if let Some(key) = odd {
descramble_parity(packets, TSC_ODD, key, scratch, index, blocks);
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Parity {
Even,
Odd,
}
#[derive(Default)]
pub struct Scrambler {
even: Option<Key>,
odd: Option<Key>,
scratch: Vec<u8>,
index: Vec<usize>,
}
impl Scrambler {
pub fn new() -> Self {
Self::default()
}
pub fn set_even(&mut self, cw: &[u8; 8]) {
self.even = Some(Key::new(cw));
}
pub fn set_odd(&mut self, cw: &[u8; 8]) {
self.odd = Some(Key::new(cw));
}
pub fn scramble(&mut self, packets: &mut [u8], parity: Parity) {
assert!(
packets.len().is_multiple_of(PACKET_SIZE),
"buffer length must be a multiple of 188"
);
let (key, tsc) = match parity {
Parity::Even => (&mut self.even, TSC_EVEN),
Parity::Odd => (&mut self.odd, TSC_ODD),
};
let Some(key) = key else {
return;
};
let index = &mut self.index;
index.clear();
for (i, ts) in packets.chunks_exact_mut(PACKET_SIZE).enumerate() {
if ts[0] != SYNC_BYTE || ts[3] & TSC_MASK != 0 {
continue;
}
let offset = match ts[3] & 0x30 {
0x10 => 4,
0x30 => 5 + ts[4] as usize,
_ => continue,
};
if offset >= PACKET_SIZE {
continue;
}
ts[3] |= tsc;
if offset == 4 {
index.push(i * PACKET_SIZE);
} else {
key.csa.encrypt_payload(&mut ts[offset ..]);
}
}
if index.len() < BATCH_MIN {
for &at in index.iter() {
key.csa
.encrypt_payload(&mut packets[at + 4 .. at + PACKET_SIZE]);
}
return;
}
let scratch = &mut self.scratch;
scratch.clear();
for &at in index.iter() {
scratch.extend_from_slice(&packets[at .. at + PACKET_SIZE]);
}
key.batch.encrypt_in_place(scratch);
for (slot, &at) in scratch.chunks_exact(PACKET_SIZE).zip(index.iter()) {
packets[at + 4 .. at + PACKET_SIZE].copy_from_slice(&slot[4 ..]);
}
}
}
fn descramble_parity(
packets: &mut [u8],
tsc: u8,
key: &mut Key,
scratch: &mut Vec<u8>,
index: &mut Vec<(usize, usize)>,
blocks: &mut Vec<u8>,
) {
index.clear();
for (i, ts) in packets.chunks_exact_mut(PACKET_SIZE).enumerate() {
if ts[0] != SYNC_BYTE || ts[3] & TSC_MASK != tsc {
continue;
}
ts[3] &= !TSC_MASK;
let offset = match ts[3] & 0x30 {
0x10 => 4,
0x30 => 5 + ts[4] as usize,
_ => continue,
};
if offset + 8 <= PACKET_SIZE {
index.push((i * PACKET_SIZE + offset, PACKET_SIZE - offset));
}
}
if index.len() < BATCH_MIN {
for &(at, len) in index.iter() {
key.csa.decrypt_payload(&mut packets[at .. at + len]);
}
return;
}
scratch.clear();
scratch.resize(index.len() * PACKET_SIZE, 0);
blocks.clear();
for (slot, &(at, len)) in scratch.chunks_exact_mut(PACKET_SIZE).zip(index.iter()) {
slot[4 .. 4 + len].copy_from_slice(&packets[at .. at + len]);
blocks.push((len / 8) as u8);
}
key.batch.decrypt_payloads_in_place(scratch, blocks);
for (slot, &(at, len)) in scratch.chunks_exact(PACKET_SIZE).zip(index.iter()) {
packets[at .. at + len].copy_from_slice(&slot[4 .. 4 + len]);
}
}