use alloc::vec::Vec;
use core::fmt;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct Oid<'a>(pub &'a [u8]);
impl<'a> Oid<'a> {
pub const ACSE_ABSTRACT_SYNTAX: Oid<'static> = Oid(&[0x52, 0x01, 0x00, 0x01]);
pub const MMS_ABSTRACT_SYNTAX: Oid<'static> = Oid(&[0x28, 0xCA, 0x22, 0x02, 0x01]);
pub const MMS_APPLICATION_CONTEXT: Oid<'static> = Oid(&[0x28, 0xCA, 0x22, 0x02, 0x03]);
pub const MMS_APPLICATION_CONTEXT_9506: Oid<'static> = Oid(&[0x28, 0xCA, 0x22, 0x01, 0x01]);
pub const BER: Oid<'static> = Oid(&[0x51, 0x01]);
pub const PASSWORD_MECHANISM: Oid<'static> = Oid(&[0x52, 0x03, 0x01]);
pub fn arcs(&self) -> Arcs<'a> {
Arcs { rest: self.0, pending: None, first: true }
}
}
#[derive(Clone, Debug)]
pub struct Arcs<'a> {
rest: &'a [u8],
pending: Option<u32>,
first: bool,
}
impl Iterator for Arcs<'_> {
type Item = u32;
fn next(&mut self) -> Option<u32> {
if let Some(second) = self.pending.take() {
return Some(second);
}
let mut value: u32 = 0;
loop {
let (&byte, tail) = self.rest.split_first()?;
self.rest = tail;
value = value.checked_mul(128)?.checked_add(u32::from(byte & 0x7F))?;
if byte & 0x80 == 0 {
break;
}
}
if core::mem::take(&mut self.first) {
let (a, b) = if value >= 80 { (2, value - 80) } else { (value / 40, value % 40) };
self.pending = Some(b);
return Some(a);
}
Some(value)
}
}
pub fn encode(arcs: &[u32]) -> Option<Vec<u8>> {
let (&first, &second) = (arcs.first()?, arcs.get(1)?);
if first > 2 || (first < 2 && second >= 40) {
return None;
}
let combined = first.checked_mul(40)?.checked_add(second)?;
let mut out = Vec::with_capacity(arcs.len() + 2);
let push = |value: u32, out: &mut Vec<u8>| {
let mut shift = 28u32;
let mut started = false;
while shift > 0 {
let group = ((value >> shift) & 0x7F) as u8;
if group != 0 || started {
out.push(group | 0x80);
started = true;
}
shift -= 7;
}
out.push((value & 0x7F) as u8);
};
push(combined, &mut out);
for &arc in arcs.get(2..).unwrap_or(&[]) {
push(arc, &mut out);
}
Some(out)
}
impl fmt::Display for Oid<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for (i, arc) in self.arcs().enumerate() {
if i > 0 {
f.write_str(".")?;
}
write!(f, "{arc}")?;
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use alloc::string::ToString;
#[test]
fn the_identifiers_this_profile_uses_decode_to_their_names() {
assert_eq!(Oid::ACSE_ABSTRACT_SYNTAX.to_string(), "2.2.1.0.1");
assert_eq!(Oid::MMS_ABSTRACT_SYNTAX.to_string(), "1.0.9506.2.1");
assert_eq!(Oid::MMS_APPLICATION_CONTEXT.to_string(), "1.0.9506.2.3");
assert_eq!(Oid::MMS_APPLICATION_CONTEXT_9506.to_string(), "1.0.9506.1.1");
assert_eq!(Oid::BER.to_string(), "2.1.1");
assert_eq!(Oid::PASSWORD_MECHANISM.to_string(), "2.2.3.1");
assert_eq!(Oid(&[0x29, 0x02]).to_string(), "1.1.2");
assert_eq!(Oid(&[0x29, 0x01]).to_string(), "1.1.1");
}
#[test]
fn arcs_encode_back_to_the_octets_they_came_from() {
for oid in [Oid::ACSE_ABSTRACT_SYNTAX, Oid::MMS_ABSTRACT_SYNTAX, Oid::MMS_APPLICATION_CONTEXT, Oid::BER, Oid::PASSWORD_MECHANISM] {
let arcs: Vec<u32> = oid.arcs().collect();
assert_eq!(encode(&arcs).as_deref(), Some(oid.0), "{oid}");
}
let title = encode(&[1, 3, 9999, 23]).unwrap();
assert_eq!(Oid(&title).to_string(), "1.3.9999.23");
assert_eq!(encode(&[1]), None, "the first octet holds two arcs");
assert_eq!(encode(&[3, 1]), None, "there is no root arc 3");
assert_eq!(encode(&[2, u32::MAX]), None);
assert_eq!(encode(&[2, u32::MAX - 79]), None);
assert_eq!(Oid(&encode(&[2, u32::MAX - 80]).unwrap()).arcs().collect::<Vec<_>>(), [2, u32::MAX - 80]);
}
#[test]
fn a_malformed_identifier_ends_rather_than_looping() {
assert_eq!(Oid(&[]).arcs().collect::<Vec<_>>(), Vec::<u32>::new());
assert_eq!(Oid(&[0x80]).arcs().count(), 0);
assert_eq!(Oid(&[0xFF; 8]).arcs().count(), 0, "an arc that overflows u32 ends the walk");
}
}