#![no_std]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![doc(
html_logo_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo.svg",
html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo.svg",
html_root_url = "https://docs.rs/const-oid/0.6.0"
)]
#![forbid(unsafe_code, clippy::unwrap_used)]
#![warn(missing_docs, rust_2018_idioms)]
#[cfg(feature = "std")]
extern crate std;
#[macro_use]
mod macros;
mod arcs;
mod encoder;
mod error;
mod parser;
pub use crate::{
arcs::{Arc, Arcs},
error::{Error, Result},
};
use crate::arcs::RootArcs;
use core::{convert::TryFrom, fmt, str::FromStr};
#[derive(Copy, Clone, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub struct ObjectIdentifier {
bytes: [u8; Self::MAX_LENGTH],
length: u8,
}
#[allow(clippy::len_without_is_empty)]
impl ObjectIdentifier {
pub const MAX_LENGTH: usize = 23;
pub const fn new(s: &str) -> Self {
parser::Parser::parse(s).finish()
}
pub fn from_arcs(arcs: &[Arc]) -> Result<Self> {
let mut bytes = [0u8; Self::MAX_LENGTH];
bytes[0] = match *arcs {
[first, second, _, ..] => RootArcs::new(first, second)?.into(),
_ => return Err(Error),
};
let mut offset = 1;
for &arc in &arcs[2..] {
offset += encoder::write_base128(&mut bytes[offset..], arc)?;
}
Ok(Self {
bytes,
length: offset as u8,
})
}
pub fn from_bytes(ber_bytes: &[u8]) -> Result<Self> {
let len = ber_bytes.len();
if !(2..=Self::MAX_LENGTH).contains(&len) {
return Err(Error);
}
ber_bytes
.get(0)
.cloned()
.ok_or(Error)
.and_then(RootArcs::try_from)?;
let mut arc_offset = 1;
let mut arc_bytes = 0;
while arc_offset < len {
match ber_bytes.get(arc_offset + arc_bytes).cloned() {
Some(byte) => {
arc_bytes += 1;
if arc_bytes == 4 && byte & 0b11110000 != 0 {
return Err(Error);
}
if byte & 0b10000000 == 0 {
arc_offset += arc_bytes;
arc_bytes = 0;
}
}
None => return Err(Error), }
}
let mut bytes = [0u8; Self::MAX_LENGTH];
bytes[..len].copy_from_slice(ber_bytes);
Ok(Self {
bytes,
length: len as u8,
})
}
pub fn as_bytes(&self) -> &[u8] {
&self.bytes[..self.length as usize]
}
pub fn arc(&self, index: usize) -> Option<Arc> {
self.arcs().nth(index)
}
pub fn arcs(&self) -> Arcs<'_> {
Arcs::new(self)
}
}
impl AsRef<[u8]> for ObjectIdentifier {
fn as_ref(&self) -> &[u8] {
self.as_bytes()
}
}
impl FromStr for ObjectIdentifier {
type Err = Error;
fn from_str(string: &str) -> Result<Self> {
let mut split = string.split('.');
let first_arc = split.next().and_then(|s| s.parse().ok()).ok_or(Error)?;
let second_arc = split.next().and_then(|s| s.parse().ok()).ok_or(Error)?;
let mut bytes = [0u8; Self::MAX_LENGTH];
bytes[0] = RootArcs::new(first_arc, second_arc)?.into();
let mut offset = 1;
for s in split {
let arc = s.parse().map_err(|_| Error)?;
offset += encoder::write_base128(&mut bytes[offset..], arc)?;
}
if offset > 1 {
Ok(Self {
bytes,
length: offset as u8,
})
} else {
Err(Error)
}
}
}
impl TryFrom<&[u8]> for ObjectIdentifier {
type Error = Error;
fn try_from(ber_bytes: &[u8]) -> Result<Self> {
Self::from_bytes(ber_bytes)
}
}
impl From<&ObjectIdentifier> for ObjectIdentifier {
fn from(oid: &ObjectIdentifier) -> ObjectIdentifier {
*oid
}
}
impl fmt::Debug for ObjectIdentifier {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "ObjectIdentifier({})", self)
}
}
impl fmt::Display for ObjectIdentifier {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let len = self.arcs().count();
for (i, arc) in self.arcs().enumerate() {
write!(f, "{}", arc)?;
if i < len - 1 {
write!(f, ".")?;
}
}
Ok(())
}
}