use bytemuck::{Pod, Zeroable};
#[repr(transparent)]
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Pod, Zeroable)]
pub struct EightCC(pub [u8; 8]);
impl EightCC {
#[inline(always)]
pub const fn new(tag: [u8; 8]) -> Self {
Self(tag)
}
#[inline]
pub const fn from_name(name: &str) -> Self {
let bytes = name.as_bytes();
let mut out = [0u8; 8];
let mut i = 0;
while i < 8 && i < bytes.len() {
out[i] = bytes[i];
i += 1;
}
Self(out)
}
pub const fn mix(self, other: EightCC) -> EightCC {
let mut d: u64 = 0xcbf2_9ce4_8422_2325;
let sb = self.0;
let mut i = 0;
while i < 8 {
d ^= sb[i] as u64;
d = d.wrapping_mul(0x0000_0100_0000_01b3);
i += 1;
}
let ob = other.0;
let mut i = 0;
while i < 8 {
d ^= ob[i] as u64;
d = d.wrapping_mul(0x0000_0100_0000_01b3);
i += 1;
}
let db = d.to_le_bytes();
let mut out = self.0;
let mut i = 0;
while i < 8 {
if out[i] & 0x80 != 0 {
out[i] = (out[i] ^ db[i]) | 0x80;
}
i += 1;
}
Self(out)
}
pub const fn mix_str(self, s: &str) -> EightCC {
let mut d: u64 = 0xcbf2_9ce4_8422_2325;
let sb = self.0;
let mut i = 0;
while i < 8 {
d ^= sb[i] as u64;
d = d.wrapping_mul(0x0000_0100_0000_01b3);
i += 1;
}
let bytes = s.as_bytes();
let mut i = 0;
while i < bytes.len() {
d ^= bytes[i] as u64;
d = d.wrapping_mul(0x0000_0100_0000_01b3);
i += 1;
}
let db = d.to_le_bytes();
let mut out = self.0;
let mut i = 0;
while i < 8 {
if out[i] & 0x80 != 0 {
out[i] = (out[i] ^ db[i]) | 0x80;
}
i += 1;
}
Self(out)
}
#[inline(always)]
pub const fn with_ctrl_bit(self) -> EightCC {
let mut out = self.0;
out[7] ^= 0x40;
Self(out)
}
}