#[derive(Clone, Debug, PartialEq)]
pub struct Features{
mdc: bool,
aead: bool,
unknown: Box<[u8]>,
}
impl Default for Features {
fn default() -> Self {
Features{
mdc: false,
aead: false,
unknown: Default::default(),
}
}
}
impl Features {
pub fn new(bits: &[u8]) -> Self {
let mdc = bits.get(0)
.map(|x| x & FEATURE_FLAG_MDC != 0).unwrap_or(false);
let aead = bits.get(0)
.map(|x| x & FEATURE_FLAG_AEAD != 0).unwrap_or(false);
let unk = if bits.is_empty() {
Box::default()
} else {
let mut cpy = Vec::from(bits);
cpy[0] &= (FEATURE_FLAG_MDC | FEATURE_FLAG_AEAD) ^ 0xff;
while cpy.last().cloned() == Some(0) { cpy.pop(); }
cpy.into_boxed_slice()
};
Features{
mdc: mdc, aead: aead, unknown: unk
}
}
pub fn sequoia() -> Self {
Features{
mdc: true,
aead: true,
unknown: Default::default(),
}
}
pub(crate) fn as_vec(&self) -> Vec<u8> {
let mut ret = if self.unknown.is_empty() {
vec![0]
} else {
self.unknown.clone().into()
};
if self.mdc { ret[0] |= FEATURE_FLAG_MDC; }
if self.aead { ret[0] |= FEATURE_FLAG_AEAD; }
ret
}
pub fn supports_mdc(&self) -> bool {
self.mdc
}
pub fn set_mdc(mut self, v: bool) -> Self {
self.mdc = v;
self
}
pub fn supports_aead(&self) -> bool {
self.aead
}
pub fn set_aead(mut self, v: bool) -> Self {
self.aead = v;
self
}
}
const FEATURE_FLAG_MDC: u8 = 0x01;
const FEATURE_FLAG_AEAD: u8 = 0x02;