use std::cmp::Ordering;
use std::fmt;
use super::{Integer, Map, MapKey, TAG_DECIMAL, TAG_UUID, Value, major, simple};
pub const MAX_DEPTH: usize = 64;
pub fn decode(bytes: &[u8]) -> Result<Value, DecodeError> {
let mut decoder = Decoder { bytes, position: 0 };
let value = decoder.value(0)?;
if decoder.position == bytes.len() {
Ok(value)
} else {
Err(DecodeError::new(
decoder.position,
DecodeErrorKind::TrailingBytes,
))
}
}
pub fn decode_first(bytes: &[u8]) -> Result<(Value, usize), DecodeError> {
let mut decoder = Decoder { bytes, position: 0 };
let value = decoder.value(0)?;
Ok((value, decoder.position))
}
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
#[error("{kind} at byte {offset}")]
pub struct DecodeError {
offset: usize,
kind: DecodeErrorKind,
}
impl DecodeError {
fn new(offset: usize, kind: DecodeErrorKind) -> Self {
Self { offset, kind }
}
#[must_use]
pub fn offset(&self) -> usize {
self.offset
}
#[must_use]
pub fn kind(&self) -> &DecodeErrorKind {
&self.kind
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum DecodeErrorKind {
UnexpectedEnd,
TrailingBytes,
NotShortest,
IndefiniteLength,
Reserved,
Float,
SimpleValue,
InvalidUtf8,
InvalidMapKey,
UnsortedKeys,
DuplicateKey,
UnsupportedTag,
InvalidTagContent,
TooDeep,
}
impl fmt::Display for DecodeErrorKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Self::UnexpectedEnd => "unexpected end of input",
Self::TrailingBytes => "bytes after the end of the value",
Self::NotShortest => "number not in its shortest form",
Self::IndefiniteLength => "indefinite length",
Self::Reserved => "reserved additional information",
Self::Float => "floating-point number",
Self::SimpleValue => "simple value other than false, true and null",
Self::InvalidUtf8 => "text that is not valid UTF-8",
Self::InvalidMapKey => "map key that is neither an integer nor text",
Self::UnsortedKeys => "map key out of order",
Self::DuplicateKey => "duplicate map key",
Self::UnsupportedTag => "unsupported tag",
Self::InvalidTagContent => "invalid tag content",
Self::TooDeep => "nesting too deep",
})
}
}
struct Decoder<'a> {
bytes: &'a [u8],
position: usize,
}
impl<'a> Decoder<'a> {
fn value(&mut self, depth: usize) -> Result<Value, DecodeError> {
let start = self.position;
let initial = self.byte()?;
let (major, info) = (initial >> 5, initial & 0x1f);
match major {
major::UNSIGNED => {
let argument = self.argument(info, start)?;
Ok(Value::Integer(Integer::from(argument)))
}
major::NEGATIVE => {
let argument = self.argument(info, start)?;
let value = Integer::new(-1 - i128::from(argument)).expect("at least -2^64");
Ok(Value::Integer(value))
}
major::BYTES => {
let argument = self.argument(info, start)?;
Ok(Value::Bytes(self.take(argument, start)?.to_vec()))
}
major::TEXT => {
let argument = self.argument(info, start)?;
let bytes = self.take(argument, start)?;
let text = std::str::from_utf8(bytes)
.map_err(|_| DecodeError::new(start, DecodeErrorKind::InvalidUtf8))?;
Ok(Value::Text(text.to_owned()))
}
major::ARRAY => {
let argument = self.argument(info, start)?;
Self::enter(depth, start)?;
let count = self.count(argument, start)?;
let mut items = Vec::with_capacity(count);
for _ in 0..count {
items.push(self.value(depth + 1)?);
}
Ok(Value::Array(items))
}
major::MAP => {
let argument = self.argument(info, start)?;
Self::enter(depth, start)?;
let count = self.count(argument, start)?;
self.map(count, depth)
}
major::TAG => {
let argument = self.argument(info, start)?;
self.tag(argument, start)
}
_ => Self::simple(initial, start),
}
}
fn map(&mut self, count: usize, depth: usize) -> Result<Value, DecodeError> {
let mut entries: Vec<(MapKey, Value)> = Vec::with_capacity(count);
for _ in 0..count {
let key_start = self.position;
let key = self.map_key()?;
if let Some((previous, _)) = entries.last() {
match previous.cmp(&key) {
Ordering::Less => {}
Ordering::Equal => {
return Err(DecodeError::new(key_start, DecodeErrorKind::DuplicateKey));
}
Ordering::Greater => {
return Err(DecodeError::new(key_start, DecodeErrorKind::UnsortedKeys));
}
}
}
let value = self.value(depth + 1)?;
entries.push((key, value));
}
Ok(Value::Map(Map::from_sorted(entries)))
}
fn map_key(&mut self) -> Result<MapKey, DecodeError> {
let start = self.position;
let major = self.peek()? >> 5;
if !matches!(major, major::UNSIGNED | major::NEGATIVE | major::TEXT) {
return Err(DecodeError::new(start, DecodeErrorKind::InvalidMapKey));
}
match self.value(0)? {
Value::Integer(integer) => Ok(MapKey::Integer(integer)),
Value::Text(text) => Ok(MapKey::Text(text)),
_ => unreachable!("major types 0, 1 and 3 decode to integers or text"),
}
}
fn tag(&mut self, number: u64, start: usize) -> Result<Value, DecodeError> {
match number {
TAG_DECIMAL => {
let content = self.position;
let invalid = || DecodeError::new(content, DecodeErrorKind::InvalidTagContent);
let (major, count) = self.header()?;
if major != major::ARRAY || count != 2 {
return Err(invalid());
}
let exponent = self.integer().ok_or_else(invalid)??;
let mantissa = self.integer().ok_or_else(invalid)??;
Ok(Value::Decimal { exponent, mantissa })
}
TAG_UUID => {
let content = self.position;
let invalid = || DecodeError::new(content, DecodeErrorKind::InvalidTagContent);
let (major, len) = self.header()?;
if major != major::BYTES || len != 16 {
return Err(invalid());
}
let bytes = self.take(len, content)?;
Ok(Value::Uuid(bytes.try_into().expect("took 16 bytes")))
}
_ => Err(DecodeError::new(start, DecodeErrorKind::UnsupportedTag)),
}
}
fn integer(&mut self) -> Option<Result<Integer, DecodeError>> {
let major = match self.peek() {
Ok(initial) => initial >> 5,
Err(error) => return Some(Err(error)),
};
if !matches!(major, major::UNSIGNED | major::NEGATIVE) {
return None;
}
Some(self.value(0).map(|value| match value {
Value::Integer(integer) => integer,
_ => unreachable!("major types 0 and 1 decode to integers"),
}))
}
fn header(&mut self) -> Result<(u8, u64), DecodeError> {
let start = self.position;
let initial = self.byte()?;
let (major, info) = (initial >> 5, initial & 0x1f);
if major > major::TAG {
return Ok((major, 0));
}
Ok((major, self.argument(info, start)?))
}
fn simple(initial: u8, start: usize) -> Result<Value, DecodeError> {
match initial {
simple::FALSE => Ok(Value::Bool(false)),
simple::TRUE => Ok(Value::Bool(true)),
simple::NULL => Ok(Value::Null),
0xf9..=0xfb => Err(DecodeError::new(start, DecodeErrorKind::Float)),
0xfc..=0xfe => Err(DecodeError::new(start, DecodeErrorKind::Reserved)),
_ => Err(DecodeError::new(start, DecodeErrorKind::SimpleValue)),
}
}
fn enter(depth: usize, start: usize) -> Result<(), DecodeError> {
if depth < MAX_DEPTH {
Ok(())
} else {
Err(DecodeError::new(start, DecodeErrorKind::TooDeep))
}
}
fn argument(&mut self, info: u8, start: usize) -> Result<u64, DecodeError> {
let not_shortest = || DecodeError::new(start, DecodeErrorKind::NotShortest);
match info {
0..=23 => Ok(u64::from(info)),
24 => {
let argument = u64::from(self.byte()?);
if argument < 24 {
Err(not_shortest())
} else {
Ok(argument)
}
}
25 => {
let argument = u64::from(u16::from_be_bytes(self.array()?));
if argument <= 0xff {
Err(not_shortest())
} else {
Ok(argument)
}
}
26 => {
let argument = u64::from(u32::from_be_bytes(self.array()?));
if argument <= 0xffff {
Err(not_shortest())
} else {
Ok(argument)
}
}
27 => {
let argument = u64::from_be_bytes(self.array()?);
if argument <= 0xffff_ffff {
Err(not_shortest())
} else {
Ok(argument)
}
}
28..=30 => Err(DecodeError::new(start, DecodeErrorKind::Reserved)),
_ => Err(DecodeError::new(start, DecodeErrorKind::IndefiniteLength)),
}
}
fn count(&self, count: u64, start: usize) -> Result<usize, DecodeError> {
usize::try_from(count)
.ok()
.filter(|&count| count <= self.remaining())
.ok_or_else(|| DecodeError::new(start, DecodeErrorKind::UnexpectedEnd))
}
fn take(&mut self, len: u64, start: usize) -> Result<&'a [u8], DecodeError> {
let len = self.count(len, start)?;
let bytes = &self.bytes[self.position..self.position + len];
self.position += len;
Ok(bytes)
}
fn peek(&self) -> Result<u8, DecodeError> {
self.bytes
.get(self.position)
.copied()
.ok_or_else(|| DecodeError::new(self.position, DecodeErrorKind::UnexpectedEnd))
}
fn byte(&mut self) -> Result<u8, DecodeError> {
let byte = *self
.bytes
.get(self.position)
.ok_or_else(|| DecodeError::new(self.position, DecodeErrorKind::UnexpectedEnd))?;
self.position += 1;
Ok(byte)
}
fn array<const N: usize>(&mut self) -> Result<[u8; N], DecodeError> {
let start = self.position;
let bytes = self.take(N as u64, start)?;
Ok(bytes.try_into().expect("took exactly N bytes"))
}
fn remaining(&self) -> usize {
self.bytes.len() - self.position
}
}