use std::cmp::Ordering;
use std::collections::{HashMap, hash_map::DefaultHasher};
use std::fmt;
use std::hash::{Hash, Hasher};
use std::mem::size_of;
use std::str::FromStr;
pub const MAX_VALUE_BYTES: usize = 67_108_864;
pub const DEFAULT_MAX_DEPTH: usize = 128;
pub const MAX_ALLOWED_DEPTH: usize = 1_000_000;
pub const DEFAULT_MAX_ITEMS: usize = 1_000_000;
pub const DEFAULT_MAX_INTEGER_BYTES: usize = 1 << 20;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Limits {
pub max_bytes: usize,
pub max_depth: usize,
pub max_items: usize,
pub max_integer_bytes: usize,
}
impl Default for Limits {
fn default() -> Self {
Self {
max_bytes: MAX_VALUE_BYTES,
max_depth: DEFAULT_MAX_DEPTH,
max_items: DEFAULT_MAX_ITEMS,
max_integer_bytes: DEFAULT_MAX_INTEGER_BYTES,
}
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Sign {
Positive,
Negative,
}
#[derive(Clone, Eq, PartialEq, Hash)]
pub struct Integer {
negative: bool,
magnitude: Vec<u8>,
}
impl Integer {
pub const fn zero() -> Self {
Self {
negative: false,
magnitude: Vec::new(),
}
}
pub fn from_sign_and_magnitude(negative: bool, magnitude: impl AsRef<[u8]>) -> Self {
let bytes = magnitude.as_ref();
let first = bytes
.iter()
.position(|byte| *byte != 0)
.unwrap_or(bytes.len());
if first == bytes.len() {
return Self::zero();
}
Self {
negative,
magnitude: bytes[first..].to_vec(),
}
}
fn from_owned_sign_and_magnitude(negative: bool, mut magnitude: Vec<u8>) -> Self {
let first = magnitude
.iter()
.position(|byte| *byte != 0)
.unwrap_or(magnitude.len());
if first == magnitude.len() {
return Self::zero();
}
if first != 0 {
magnitude.drain(..first);
}
Self {
negative,
magnitude,
}
}
pub fn from_magnitude_be(sign: Sign, magnitude: impl AsRef<[u8]>) -> Self {
Self::from_sign_and_magnitude(sign == Sign::Negative, magnitude)
}
pub fn from_u128(value: u128) -> Self {
if value == 0 {
return Self::zero();
}
let bytes = value.to_be_bytes();
Self::from_sign_and_magnitude(false, bytes)
}
pub fn from_i128(value: i128) -> Self {
if value >= 0 {
return Self::from_u128(value as u128);
}
Self::from_sign_and_magnitude(true, value.unsigned_abs().to_be_bytes())
}
pub fn parse_decimal(value: &str) -> Result<Self> {
let bytes = value.as_bytes();
if bytes.is_empty() {
return Err(Error::InvalidInteger {
offset: 0,
reason: "empty decimal integer",
});
}
let (negative, digits) = match bytes[0] {
b'-' => (true, &bytes[1..]),
b'+' => (false, &bytes[1..]),
_ => (false, bytes),
};
if digits.is_empty() || !digits.iter().all(u8::is_ascii_digit) {
return Err(Error::InvalidInteger {
offset: 0,
reason: "invalid decimal integer",
});
}
let mut magnitude = Vec::new();
for digit in digits {
let mut carry = u16::from(*digit - b'0');
for byte in magnitude.iter_mut().rev() {
let value = u16::from(*byte) * 10 + carry;
*byte = value as u8;
carry = value >> 8;
}
if carry != 0 {
magnitude.insert(0, carry as u8);
}
}
Ok(Self::from_sign_and_magnitude(negative, magnitude))
}
pub const fn is_negative(&self) -> bool {
self.negative
}
pub const fn is_zero(&self) -> bool {
self.magnitude.len() == 0
}
pub const fn sign(&self) -> Sign {
if self.negative {
Sign::Negative
} else {
Sign::Positive
}
}
pub fn magnitude_be(&self) -> &[u8] {
&self.magnitude
}
pub fn as_u128(&self) -> Option<u128> {
if self.negative || self.magnitude.len() > 16 {
return None;
}
let mut bytes = [0; 16];
bytes[16 - self.magnitude.len()..].copy_from_slice(&self.magnitude);
Some(u128::from_be_bytes(bytes))
}
pub fn as_i128(&self) -> Option<i128> {
let magnitude = self.as_u128_magnitude()?;
if self.negative {
if magnitude > (i128::MAX as u128) + 1 {
None
} else if magnitude == (i128::MAX as u128) + 1 {
Some(i128::MIN)
} else {
Some(-(magnitude as i128))
}
} else {
i128::try_from(magnitude).ok()
}
}
fn as_u128_magnitude(&self) -> Option<u128> {
if self.magnitude.len() > 16 {
return None;
}
let mut bytes = [0; 16];
bytes[16 - self.magnitude.len()..].copy_from_slice(&self.magnitude);
Some(u128::from_be_bytes(bytes))
}
fn negative_cbor_magnitude(&self) -> Vec<u8> {
let mut magnitude = self.magnitude.clone();
if magnitude.is_empty() {
return magnitude;
}
let mut index = magnitude.len();
while index > 0 {
index -= 1;
if magnitude[index] != 0 {
magnitude[index] -= 1;
break;
}
magnitude[index] = 0xff;
}
let first = magnitude
.iter()
.position(|byte| *byte != 0)
.unwrap_or(magnitude.len());
magnitude.drain(..first);
magnitude
}
}
impl Default for Integer {
fn default() -> Self {
Self::zero()
}
}
impl fmt::Debug for Integer {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("Integer")
.field("negative", &self.negative)
.field("magnitude", &self.magnitude)
.finish()
}
}
impl fmt::Display for Integer {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.is_zero() {
return formatter.write_str("0");
}
let mut magnitude = self.magnitude.clone();
let mut digits = Vec::new();
while !magnitude.is_empty() {
let mut remainder = 0u16;
for byte in &mut magnitude {
let value = (remainder << 8) | u16::from(*byte);
*byte = (value / 10) as u8;
remainder = value % 10;
}
digits.push((b'0' + remainder as u8) as char);
let first = magnitude
.iter()
.position(|byte| *byte != 0)
.unwrap_or(magnitude.len());
magnitude.drain(..first);
}
if self.negative {
formatter.write_str("-")?;
}
for digit in digits.iter().rev() {
formatter.write_str(&digit.to_string())?;
}
Ok(())
}
}
impl FromStr for Integer {
type Err = Error;
fn from_str(value: &str) -> Result<Self> {
Self::parse_decimal(value)
}
}
impl From<i8> for Integer {
fn from(value: i8) -> Self {
Self::from_i128(value as i128)
}
}
impl From<i16> for Integer {
fn from(value: i16) -> Self {
Self::from_i128(value as i128)
}
}
impl From<i32> for Integer {
fn from(value: i32) -> Self {
Self::from_i128(value as i128)
}
}
impl From<i64> for Integer {
fn from(value: i64) -> Self {
Self::from_i128(value as i128)
}
}
impl From<i128> for Integer {
fn from(value: i128) -> Self {
Self::from_i128(value)
}
}
impl From<u8> for Integer {
fn from(value: u8) -> Self {
Self::from_u128(value as u128)
}
}
impl From<u16> for Integer {
fn from(value: u16) -> Self {
Self::from_u128(value as u128)
}
}
impl From<u32> for Integer {
fn from(value: u32) -> Self {
Self::from_u128(value as u128)
}
}
impl From<u64> for Integer {
fn from(value: u64) -> Self {
Self::from_u128(value as u128)
}
}
impl From<u128> for Integer {
fn from(value: u128) -> Self {
Self::from_u128(value)
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum FloatWidth {
Bits16,
Bits32,
Bits64,
}
impl FloatWidth {
const fn raw_bits_mask(self) -> u64 {
match self {
Self::Bits16 => u16::MAX as u64,
Self::Bits32 => u32::MAX as u64,
Self::Bits64 => u64::MAX,
}
}
const fn byte_width(self) -> usize {
match self {
Self::Bits16 => 2,
Self::Bits32 => 4,
Self::Bits64 => 8,
}
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct Float {
pub width: FloatWidth,
pub raw_bits: u64,
}
impl Float {
pub const fn new(width: FloatWidth, raw_bits: u64) -> Option<Self> {
if raw_bits & !width.raw_bits_mask() == 0 {
Some(Self { width, raw_bits })
} else {
None
}
}
pub const fn is_valid(self) -> bool {
self.raw_bits & !self.width.raw_bits_mask() == 0
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum ValueKind {
Undefined,
Null,
Boolean,
Integer,
Float,
TextString,
Bytes,
Array,
Map,
}
#[derive(Clone, Debug)]
pub enum Value {
Undefined,
Null,
Boolean(bool),
Integer(Integer),
Float(Float),
TextString(String),
Bytes(Vec<u8>),
Array(Vec<Value>),
Map(Vec<(Value, Value)>),
}
impl Drop for Value {
fn drop(&mut self) {
let root = std::mem::replace(self, Self::Undefined);
let mut pending = vec![root];
while let Some(value) = pending.pop() {
let mut value = std::mem::ManuallyDrop::new(value);
unsafe {
match &mut *value {
Self::Array(values) => pending.extend(std::mem::take(values)),
Self::Map(entries) => {
for (key, value) in std::mem::take(entries) {
pending.push(key);
pending.push(value);
}
}
Self::Integer(integer) => std::ptr::drop_in_place(integer),
Self::TextString(text) => std::ptr::drop_in_place(text),
Self::Bytes(bytes) => std::ptr::drop_in_place(bytes),
Self::Undefined | Self::Null | Self::Boolean(_) | Self::Float(_) => {}
}
}
}
}
}
impl Value {
pub const fn kind(&self) -> ValueKind {
match self {
Self::Undefined => ValueKind::Undefined,
Self::Null => ValueKind::Null,
Self::Boolean(_) => ValueKind::Boolean,
Self::Integer(_) => ValueKind::Integer,
Self::Float(_) => ValueKind::Float,
Self::TextString(_) => ValueKind::TextString,
Self::Bytes(_) => ValueKind::Bytes,
Self::Array(_) => ValueKind::Array,
Self::Map(_) => ValueKind::Map,
}
}
pub fn integer(value: impl Into<Integer>) -> Self {
Self::Integer(value.into())
}
pub const fn float16(raw_bits: u16) -> Self {
Self::Float(Float {
width: FloatWidth::Bits16,
raw_bits: raw_bits as u64,
})
}
pub const fn float32(raw_bits: u32) -> Self {
Self::Float(Float {
width: FloatWidth::Bits32,
raw_bits: raw_bits as u64,
})
}
pub const fn float64(raw_bits: u64) -> Self {
Self::Float(Float {
width: FloatWidth::Bits64,
raw_bits,
})
}
pub fn text(value: impl Into<String>) -> Self {
Self::TextString(value.into())
}
pub fn bytes(value: impl Into<Vec<u8>>) -> Self {
Self::Bytes(value.into())
}
pub fn array(values: Vec<Self>) -> Self {
Self::Array(values)
}
pub fn map(entries: Vec<(Value, Value)>) -> Result<Self> {
validate_map_entries(&entries)?;
Ok(Self::Map(entries))
}
pub fn as_bool(&self) -> Option<bool> {
match self {
Self::Boolean(value) => Some(*value),
_ => None,
}
}
pub fn as_integer(&self) -> Option<&Integer> {
match self {
Self::Integer(value) => Some(value),
_ => None,
}
}
pub fn as_float(&self) -> Option<&Float> {
match self {
Self::Float(value) => Some(value),
_ => None,
}
}
pub fn as_str(&self) -> Option<&str> {
match self {
Self::TextString(value) => Some(value),
_ => None,
}
}
pub fn as_bytes(&self) -> Option<&[u8]> {
match self {
Self::Bytes(value) => Some(value),
_ => None,
}
}
pub fn as_array(&self) -> Option<&[Value]> {
match self {
Self::Array(values) => Some(values),
_ => None,
}
}
pub fn as_map(&self) -> Option<&[(Value, Value)]> {
match self {
Self::Map(entries) => Some(entries),
_ => None,
}
}
pub fn map_get<K>(&self, key: K) -> Option<&Value>
where
K: Into<Value>,
{
let entries = self.as_map()?;
let key = key.into();
entries
.iter()
.find_map(|(entry_key, value)| (entry_key == &key).then_some(value))
}
pub fn into_integer(self) -> Option<Integer> {
let mut value = std::mem::ManuallyDrop::new(self);
unsafe {
match &mut *value {
Self::Integer(integer) => Some(std::ptr::read(integer)),
_ => {
std::mem::ManuallyDrop::drop(&mut value);
None
}
}
}
}
pub fn into_string(self) -> Option<String> {
let mut value = std::mem::ManuallyDrop::new(self);
unsafe {
match &mut *value {
Self::TextString(text) => Some(std::ptr::read(text)),
_ => {
std::mem::ManuallyDrop::drop(&mut value);
None
}
}
}
}
pub fn into_bytes(self) -> Option<Vec<u8>> {
let mut value = std::mem::ManuallyDrop::new(self);
unsafe {
match &mut *value {
Self::Bytes(bytes) => Some(std::ptr::read(bytes)),
_ => {
std::mem::ManuallyDrop::drop(&mut value);
None
}
}
}
}
pub fn into_array(self) -> Option<Vec<Value>> {
let mut value = std::mem::ManuallyDrop::new(self);
unsafe {
match &mut *value {
Self::Array(values) => Some(std::ptr::read(values)),
_ => {
std::mem::ManuallyDrop::drop(&mut value);
None
}
}
}
}
pub fn into_map(self) -> Option<Vec<(Value, Value)>> {
let mut value = std::mem::ManuallyDrop::new(self);
unsafe {
match &mut *value {
Self::Map(entries) => Some(std::ptr::read(entries)),
_ => {
std::mem::ManuallyDrop::drop(&mut value);
None
}
}
}
}
pub fn is_scalar_key(&self) -> bool {
matches!(
self,
Self::Undefined
| Self::Null
| Self::Boolean(_)
| Self::Integer(_)
| Self::Float(_)
| Self::TextString(_)
| Self::Bytes(_)
)
}
pub fn to_cbor(&self) -> Result<Vec<u8>> {
encode(self)
}
pub fn from_cbor(bytes: &[u8]) -> Result<Self> {
decode(bytes)
}
}
impl From<bool> for Value {
fn from(value: bool) -> Self {
Self::Boolean(value)
}
}
impl From<String> for Value {
fn from(value: String) -> Self {
Self::TextString(value)
}
}
impl From<&str> for Value {
fn from(value: &str) -> Self {
Self::text(value)
}
}
impl From<Vec<u8>> for Value {
fn from(value: Vec<u8>) -> Self {
Self::Bytes(value)
}
}
impl From<&[u8]> for Value {
fn from(value: &[u8]) -> Self {
Self::bytes(value.to_vec())
}
}
impl<const N: usize> From<&[u8; N]> for Value {
fn from(value: &[u8; N]) -> Self {
Self::bytes(value.as_slice())
}
}
impl From<&Vec<u8>> for Value {
fn from(value: &Vec<u8>) -> Self {
Self::bytes(value.clone())
}
}
macro_rules! impl_integer_value_from {
($($type:ty),+ $(,)?) => {
$(
impl From<$type> for Value {
fn from(value: $type) -> Self {
Self::integer(value)
}
}
)+
};
}
impl_integer_value_from!(i8, i16, i32, i64, i128, u8, u16, u32, u64, u128);
impl From<f32> for Value {
fn from(value: f32) -> Self {
Self::float32(value.to_bits())
}
}
impl From<f64> for Value {
fn from(value: f64) -> Self {
Self::float64(value.to_bits())
}
}
impl PartialEq for Value {
fn eq(&self, other: &Self) -> bool {
values_equal(self, other)
}
}
impl Eq for Value {}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ErrorKind {
ResourceLimit,
Truncated,
TrailingBytes,
InvalidEncoding,
UnsupportedType,
InvalidUtf8,
InvalidInteger,
NonScalarKey,
DuplicateKey,
Allocation,
InvalidFloat,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Resource {
Bytes,
Depth,
Items,
IntegerBytes,
}
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("{resource:?} limit {limit} exceeded by {actual}")]
ResourceLimit {
resource: Resource,
limit: usize,
actual: usize,
},
#[error("failed to allocate {size} bytes")]
Allocation {
size: usize,
},
#[error("invalid {width:?} float raw bits {raw_bits:#018x}")]
InvalidFloat {
width: FloatWidth,
raw_bits: u64,
},
#[error("truncated CBOR at byte {offset}")]
Truncated {
offset: usize,
},
#[error("trailing CBOR bytes at byte {offset}")]
TrailingBytes {
offset: usize,
},
#[error("invalid CBOR encoding at byte {offset}: {reason}")]
InvalidEncoding {
offset: usize,
reason: &'static str,
},
#[error(
"unsupported CBOR type at byte {offset}: major type {major}, additional information {additional}"
)]
UnsupportedType {
offset: usize,
major: u8,
additional: u8,
},
#[error("indefinite-length CBOR item at byte {offset}")]
IndefiniteLength {
offset: usize,
},
#[error("invalid UTF-8 text string at byte {offset}")]
InvalidUtf8 {
offset: usize,
},
#[error("invalid integer at byte {offset}: {reason}")]
InvalidInteger {
offset: usize,
reason: &'static str,
},
#[error("non-scalar map key at entry {index}")]
NonScalarKey {
index: usize,
},
#[error("duplicate map key at entry {index}")]
DuplicateKey {
index: usize,
},
}
impl Error {
pub const fn kind(&self) -> ErrorKind {
match self {
Self::ResourceLimit { .. } => ErrorKind::ResourceLimit,
Self::Allocation { .. } => ErrorKind::Allocation,
Self::InvalidFloat { .. } => ErrorKind::InvalidFloat,
Self::Truncated { .. } => ErrorKind::Truncated,
Self::TrailingBytes { .. } => ErrorKind::TrailingBytes,
Self::InvalidEncoding { .. } | Self::IndefiniteLength { .. } => {
ErrorKind::InvalidEncoding
}
Self::UnsupportedType { .. } => ErrorKind::UnsupportedType,
Self::InvalidUtf8 { .. } => ErrorKind::InvalidUtf8,
Self::InvalidInteger { .. } => ErrorKind::InvalidInteger,
Self::NonScalarKey { .. } => ErrorKind::NonScalarKey,
Self::DuplicateKey { .. } => ErrorKind::DuplicateKey,
}
}
pub const fn category(&self) -> ErrorKind {
self.kind()
}
}
pub type Result<T> = std::result::Result<T, Error>;
pub fn encode(value: &Value) -> Result<Vec<u8>> {
encode_with_limits(value, Limits::default())
}
pub fn encode_with_limits(value: &Value, limits: Limits) -> Result<Vec<u8>> {
encode_with_limits_and_budget(value, limits, usize::MAX, &mut |_| true)
}
pub fn encode_with_limits_and_budget(
value: &Value,
limits: Limits,
max_allocation_bytes: usize,
reserve: &mut impl FnMut(usize) -> bool,
) -> Result<Vec<u8>> {
validate_limits(limits)?;
let mut allocation = AllocationState {
used: 0,
maximum: max_allocation_bytes,
reserve,
};
let mut output = Vec::new();
let mut tasks = Vec::new();
let mut item_count = 0usize;
let mut pending_items = 1usize;
charge_allocation(&mut allocation, size_of::<(&Value, usize)>())?;
tasks
.try_reserve_exact(1)
.map_err(|_| Error::Allocation { size: 1 })?;
tasks.push((value, 0usize));
while let Some((current, depth)) = tasks.pop() {
item_count = item_count
.checked_add(1)
.ok_or_else(|| resource(Resource::Items, limits.max_items, usize::MAX))?;
if item_count > limits.max_items {
return Err(resource(Resource::Items, limits.max_items, item_count));
}
pending_items = pending_items
.checked_sub(1)
.expect("the root or a declared child is always pending");
match current {
Value::Undefined => push_bytes(&mut output, &[0xf7], limits, &mut allocation)?,
Value::Null => push_bytes(&mut output, &[0xf6], limits, &mut allocation)?,
Value::Boolean(false) => push_bytes(&mut output, &[0xf4], limits, &mut allocation)?,
Value::Boolean(true) => push_bytes(&mut output, &[0xf5], limits, &mut allocation)?,
Value::Integer(integer) => {
encode_integer(integer, &mut output, limits, &mut allocation)?
}
Value::Float(float) => {
if !float.is_valid() {
return Err(Error::InvalidFloat {
width: float.width,
raw_bits: float.raw_bits,
});
}
let (marker, width) = match float.width {
FloatWidth::Bits16 => (0xf9, FloatWidth::Bits16),
FloatWidth::Bits32 => (0xfa, FloatWidth::Bits32),
FloatWidth::Bits64 => (0xfb, FloatWidth::Bits64),
};
push_bytes(&mut output, &[marker], limits, &mut allocation)?;
let bytes = float.raw_bits.to_be_bytes();
push_bytes(
&mut output,
&bytes[8 - width.byte_width()..],
limits,
&mut allocation,
)?;
}
Value::TextString(text) => {
append_head(3, text.len(), &mut output, limits, &mut allocation)?;
push_bytes(&mut output, text.as_bytes(), limits, &mut allocation)?;
}
Value::Bytes(bytes) => {
append_head(2, bytes.len(), &mut output, limits, &mut allocation)?;
push_bytes(&mut output, bytes, limits, &mut allocation)?;
}
Value::Array(values) => {
if depth >= limits.max_depth {
return Err(resource(Resource::Depth, limits.max_depth, depth + 1));
}
validate_count(values.len(), limits.max_items)?;
add_pending_items(
&mut pending_items,
values.len(),
item_count,
limits.max_items,
)?;
append_head(4, values.len(), &mut output, limits, &mut allocation)?;
charge_allocation(
&mut allocation,
values
.len()
.checked_mul(size_of::<(&Value, usize)>())
.ok_or_else(|| {
resource(Resource::Bytes, max_allocation_bytes, usize::MAX)
})?,
)?;
tasks
.try_reserve_exact(values.len())
.map_err(|_| Error::Allocation {
size: tasks.len().saturating_add(values.len()),
})?;
for child in values.iter().rev() {
tasks.push((child, depth + 1));
}
}
Value::Map(entries) => {
if depth >= limits.max_depth {
return Err(resource(Resource::Depth, limits.max_depth, depth + 1));
}
let task_count = entries
.len()
.checked_mul(2)
.ok_or_else(|| resource(Resource::Items, limits.max_items, usize::MAX))?;
validate_count(task_count, limits.max_items)?;
add_pending_items(&mut pending_items, task_count, item_count, limits.max_items)?;
validate_map_entries_with_budget(entries, &mut allocation)?;
append_head(5, entries.len(), &mut output, limits, &mut allocation)?;
charge_allocation(
&mut allocation,
task_count
.checked_mul(size_of::<(&Value, usize)>())
.ok_or_else(|| {
resource(Resource::Bytes, max_allocation_bytes, usize::MAX)
})?,
)?;
tasks
.try_reserve_exact(task_count)
.map_err(|_| Error::Allocation {
size: tasks.len().saturating_add(task_count),
})?;
for (key, child) in entries.iter().rev() {
tasks.push((child, depth + 1));
tasks.push((key, depth + 1));
}
}
}
}
Ok(output)
}
pub fn decode(bytes: &[u8]) -> Result<Value> {
decode_with_limits(bytes, Limits::default())
}
pub fn decode_with_limits(bytes: &[u8], limits: Limits) -> Result<Value> {
decode_with_limits_and_budget(bytes, limits, usize::MAX, &mut |_| true)
}
pub fn decode_with_limits_and_budget(
bytes: &[u8],
limits: Limits,
max_allocation_bytes: usize,
reserve: &mut impl FnMut(usize) -> bool,
) -> Result<Value> {
validate_limits(limits)?;
if bytes.len() > limits.max_bytes {
return Err(resource(Resource::Bytes, limits.max_bytes, bytes.len()));
}
if bytes.is_empty() {
return Err(Error::Truncated { offset: 0 });
}
let mut cursor = 0usize;
let mut frames = Vec::new();
let mut root = None;
let mut item_count = 0usize;
let mut pending_items = 1usize;
let mut allocation = AllocationState {
used: 0,
maximum: max_allocation_bytes,
reserve,
};
loop {
if let Some(value) = root {
if cursor != bytes.len() {
return Err(Error::TrailingBytes { offset: cursor });
}
return Ok(value);
}
item_count = item_count
.checked_add(1)
.ok_or_else(|| resource(Resource::Items, limits.max_items, usize::MAX))?;
if item_count > limits.max_items {
return Err(resource(Resource::Items, limits.max_items, item_count));
}
pending_items = pending_items
.checked_sub(1)
.expect("the root or a declared child is always pending");
match parse_item(bytes, &mut cursor, limits, &mut allocation)? {
ParsedItem::Value(value) => {
accept_value(value, &mut frames, &mut root, &mut allocation)?
}
ParsedItem::Array(count) => {
add_pending_items(&mut pending_items, count, item_count, limits.max_items)?;
begin_frame(
Frame::Array {
remaining: count,
values: Vec::new(),
},
&mut frames,
limits,
&mut allocation,
)?;
if count == 0 {
accept_value(
Value::Array(Vec::new()),
&mut frames,
&mut root,
&mut allocation,
)?;
}
}
ParsedItem::Map(count) => {
let child_count = count
.checked_mul(2)
.ok_or_else(|| resource(Resource::Items, limits.max_items, usize::MAX))?;
add_pending_items(
&mut pending_items,
child_count,
item_count,
limits.max_items,
)?;
begin_frame(
Frame::Map {
remaining: child_count,
entries: Vec::new(),
pending_key: None,
key_index: ScalarKeyIndex::new(),
},
&mut frames,
limits,
&mut allocation,
)?;
if count == 0 {
accept_value(
Value::Map(Vec::new()),
&mut frames,
&mut root,
&mut allocation,
)?;
}
}
}
}
}
fn add_pending_items(
pending_items: &mut usize,
child_count: usize,
item_count: usize,
maximum: usize,
) -> Result<()> {
*pending_items = pending_items
.checked_add(child_count)
.ok_or_else(|| resource(Resource::Items, maximum, usize::MAX))?;
let minimum_total = item_count
.checked_add(*pending_items)
.ok_or_else(|| resource(Resource::Items, maximum, usize::MAX))?;
if minimum_total > maximum {
return Err(resource(Resource::Items, maximum, minimum_total));
}
Ok(())
}
pub fn to_cbor(value: &Value) -> Result<Vec<u8>> {
encode(value)
}
pub fn from_cbor(bytes: &[u8]) -> Result<Value> {
decode(bytes)
}
pub mod cbor {
pub use super::{
Error, ErrorKind, Limits, Resource, Result, decode, decode_with_limits,
decode_with_limits_and_budget, encode, encode_with_limits, encode_with_limits_and_budget,
from_cbor, to_cbor,
};
}
fn validate_limits(limits: Limits) -> Result<()> {
if limits.max_bytes == 0 {
return Err(resource(Resource::Bytes, 1, 0));
}
if limits.max_depth == 0 {
return Err(resource(Resource::Depth, 1, 0));
}
if limits.max_depth > MAX_ALLOWED_DEPTH {
return Err(resource(
Resource::Depth,
MAX_ALLOWED_DEPTH,
limits.max_depth,
));
}
if limits.max_items == 0 {
return Err(resource(Resource::Items, 1, 0));
}
if limits.max_integer_bytes == 0 {
return Err(resource(Resource::IntegerBytes, 1, 0));
}
Ok(())
}
struct AllocationState<'a> {
used: usize,
maximum: usize,
reserve: &'a mut dyn FnMut(usize) -> bool,
}
fn charge_allocation(state: &mut AllocationState<'_>, size: usize) -> Result<()> {
let next = state
.used
.checked_add(size)
.ok_or_else(|| resource(Resource::Bytes, state.maximum, usize::MAX))?;
if next > state.maximum || !(state.reserve)(size) {
return Err(resource(Resource::Bytes, state.maximum, next));
}
state.used = next;
Ok(())
}
fn resource(resource: Resource, limit: usize, actual: usize) -> Error {
Error::ResourceLimit {
resource,
limit,
actual,
}
}
fn validate_count(count: usize, maximum: usize) -> Result<()> {
if count > maximum {
Err(resource(Resource::Items, maximum, count))
} else {
Ok(())
}
}
fn push_bytes(
output: &mut Vec<u8>,
bytes: &[u8],
limits: Limits,
allocation: &mut AllocationState<'_>,
) -> Result<()> {
let length = output
.len()
.checked_add(bytes.len())
.ok_or_else(|| resource(Resource::Bytes, limits.max_bytes, usize::MAX))?;
if length > limits.max_bytes {
return Err(resource(Resource::Bytes, limits.max_bytes, length));
}
charge_allocation(allocation, bytes.len())?;
output
.try_reserve_exact(bytes.len())
.map_err(|_| Error::Allocation { size: length })?;
output.extend_from_slice(bytes);
Ok(())
}
fn append_head(
major: u8,
length: usize,
output: &mut Vec<u8>,
limits: Limits,
allocation: &mut AllocationState<'_>,
) -> Result<()> {
let length = u64::try_from(length)
.map_err(|_| resource(Resource::Bytes, limits.max_bytes, usize::MAX))?;
let mut head = [0u8; 9];
let size = if length < 24 {
head[0] = (major << 5) | length as u8;
1
} else if length <= u8::MAX as u64 {
head[0] = (major << 5) | 24;
head[1] = length as u8;
2
} else if length <= u16::MAX as u64 {
head[0] = (major << 5) | 25;
head[1..3].copy_from_slice(&(length as u16).to_be_bytes());
3
} else if length <= u32::MAX as u64 {
head[0] = (major << 5) | 26;
head[1..5].copy_from_slice(&(length as u32).to_be_bytes());
5
} else {
head[0] = (major << 5) | 27;
head[1..9].copy_from_slice(&length.to_be_bytes());
9
};
push_bytes(output, &head[..size], limits, allocation)
}
fn encode_integer(
integer: &Integer,
output: &mut Vec<u8>,
limits: Limits,
allocation: &mut AllocationState<'_>,
) -> Result<()> {
validate_integer_magnitude(integer, limits)?;
charge_allocation(allocation, integer.magnitude.len())?;
let magnitude = if integer.negative {
integer.negative_cbor_magnitude()
} else {
integer.magnitude.clone()
};
if magnitude.len() <= 8 {
let mut bytes = [0u8; 8];
bytes[8 - magnitude.len()..].copy_from_slice(&magnitude);
let number = u64::from_be_bytes(bytes);
let use_major_one = integer.negative;
append_head(
if use_major_one { 1 } else { 0 },
usize::try_from(number)
.map_err(|_| resource(Resource::Bytes, limits.max_bytes, usize::MAX))?,
output,
limits,
allocation,
)?;
} else {
append_head(
6,
if integer.negative { 3 } else { 2 },
output,
limits,
allocation,
)?;
append_head(2, magnitude.len(), output, limits, allocation)?;
push_bytes(output, &magnitude, limits, allocation)?;
}
Ok(())
}
fn validate_integer_magnitude(integer: &Integer, limits: Limits) -> Result<()> {
if integer.magnitude.len() > limits.max_integer_bytes {
return Err(resource(
Resource::IntegerBytes,
limits.max_integer_bytes,
integer.magnitude.len(),
));
}
Ok(())
}
fn validate_map_entries(entries: &[(Value, Value)]) -> Result<()> {
let mut key_index = ScalarKeyIndex::with_capacity(entries.len())?;
for (index, (key, _)) in entries.iter().enumerate() {
if !key.is_scalar_key() {
return Err(Error::NonScalarKey { index });
}
if let Value::Float(float) = key {
if !float.is_valid() {
return Err(Error::InvalidFloat {
width: float.width,
raw_bits: float.raw_bits,
});
}
}
if key_index.contains(entries, key) {
return Err(Error::DuplicateKey { index });
}
key_index.insert(key, index)?;
}
Ok(())
}
fn validate_map_entries_with_budget(
entries: &[(Value, Value)],
allocation: &mut AllocationState<'_>,
) -> Result<()> {
let mut key_index = ScalarKeyIndex::with_capacity_and_budget(entries.len(), allocation)?;
for (index, (key, _)) in entries.iter().enumerate() {
if !key.is_scalar_key() {
return Err(Error::NonScalarKey { index });
}
if let Value::Float(float) = key {
if !float.is_valid() {
return Err(Error::InvalidFloat {
width: float.width,
raw_bits: float.raw_bits,
});
}
}
if key_index.contains(entries, key) {
return Err(Error::DuplicateKey { index });
}
key_index.insert_with_budget(key, index, allocation)?;
}
Ok(())
}
fn hash_map_allocation_size(capacity: usize) -> Result<usize> {
capacity
.checked_mul(size_of::<(u64, Vec<usize>)>())
.ok_or_else(|| resource(Resource::Bytes, usize::MAX, usize::MAX))
}
struct ScalarKeyIndex {
buckets: HashMap<u64, Vec<usize>>,
}
impl ScalarKeyIndex {
fn new() -> Self {
Self {
buckets: HashMap::new(),
}
}
fn with_capacity(capacity: usize) -> Result<Self> {
let mut index = Self::new();
index.reserve(capacity)?;
Ok(index)
}
fn with_capacity_and_budget(
capacity: usize,
allocation: &mut AllocationState<'_>,
) -> Result<Self> {
let mut index = Self::new();
index.reserve_with_budget(capacity, allocation)?;
Ok(index)
}
fn reserve(&mut self, additional: usize) -> Result<()> {
self.buckets
.try_reserve(additional)
.map_err(|_| Error::Allocation { size: additional })
}
fn reserve_with_budget(
&mut self,
additional: usize,
allocation: &mut AllocationState<'_>,
) -> Result<()> {
let table_capacity = additional
.checked_mul(2)
.ok_or_else(|| resource(Resource::Bytes, allocation.maximum, usize::MAX))?;
charge_allocation(allocation, hash_map_allocation_size(table_capacity)?)?;
self.reserve(table_capacity)
}
fn contains(&self, entries: &[(Value, Value)], key: &Value) -> bool {
let hash = scalar_key_hash(key);
self.buckets.get(&hash).is_some_and(|bucket| {
bucket
.iter()
.any(|&index| scalar_equal(key, &entries[index].0))
})
}
fn insert(&mut self, key: &Value, index: usize) -> Result<()> {
let hash = scalar_key_hash(key);
if let Some(bucket) = self.buckets.get_mut(&hash) {
bucket
.try_reserve(1)
.map_err(|_| Error::Allocation { size: 1 })?;
bucket.push(index);
return Ok(());
}
self.buckets
.try_reserve(1)
.map_err(|_| Error::Allocation { size: 1 })?;
let mut bucket = Vec::new();
bucket
.try_reserve_exact(1)
.map_err(|_| Error::Allocation { size: 1 })?;
bucket.push(index);
self.buckets.insert(hash, bucket);
Ok(())
}
fn insert_with_budget(
&mut self,
key: &Value,
index: usize,
allocation: &mut AllocationState<'_>,
) -> Result<()> {
let hash = scalar_key_hash(key);
if let Some(bucket) = self.buckets.get_mut(&hash) {
charge_allocation(allocation, size_of::<usize>())?;
bucket
.try_reserve_exact(1)
.map_err(|_| Error::Allocation { size: 1 })?;
bucket.push(index);
return Ok(());
}
charge_allocation(allocation, size_of::<usize>())?;
if self.buckets.len() >= self.buckets.capacity() {
charge_allocation(allocation, hash_map_allocation_size(1)?)?;
}
self.buckets
.try_reserve(1)
.map_err(|_| Error::Allocation { size: 1 })?;
let mut bucket = Vec::new();
bucket
.try_reserve_exact(1)
.map_err(|_| Error::Allocation { size: 1 })?;
bucket.push(index);
self.buckets.insert(hash, bucket);
Ok(())
}
}
fn scalar_key_hash(value: &Value) -> u64 {
let mut hasher = DefaultHasher::new();
match value {
Value::Undefined => 0u8.hash(&mut hasher),
Value::Null => 1u8.hash(&mut hasher),
Value::Boolean(value) => {
2u8.hash(&mut hasher);
value.hash(&mut hasher);
}
Value::Integer(value) => {
3u8.hash(&mut hasher);
value.hash(&mut hasher);
}
Value::Float(value) => {
4u8.hash(&mut hasher);
value.hash(&mut hasher);
}
Value::TextString(value) => {
7u8.hash(&mut hasher);
value.hash(&mut hasher);
}
Value::Bytes(value) => {
8u8.hash(&mut hasher);
value.hash(&mut hasher);
}
Value::Array(_) | Value::Map(_) => unreachable!("only scalar keys are hashed"),
}
hasher.finish()
}
fn scalar_equal(left: &Value, right: &Value) -> bool {
match (left, right) {
(Value::Undefined, Value::Undefined) | (Value::Null, Value::Null) => true,
(Value::Boolean(left), Value::Boolean(right)) => left == right,
(Value::Integer(left), Value::Integer(right)) => left == right,
(Value::Float(left), Value::Float(right)) => left == right,
(Value::TextString(left), Value::TextString(right)) => left == right,
(Value::Bytes(left), Value::Bytes(right)) => left == right,
_ => false,
}
}
fn values_equal(left: &Value, right: &Value) -> bool {
let mut work = vec![(left, right)];
while let Some((left, right)) = work.pop() {
match (left, right) {
(Value::Undefined, Value::Undefined) | (Value::Null, Value::Null) => {}
(Value::Boolean(left), Value::Boolean(right)) if left == right => {}
(Value::Integer(left), Value::Integer(right)) if left == right => {}
(Value::Float(left), Value::Float(right)) if left == right => {}
(Value::TextString(left), Value::TextString(right)) if left == right => {}
(Value::Bytes(left), Value::Bytes(right)) if left == right => {}
(Value::Array(left), Value::Array(right)) if left.len() == right.len() => {
for (left, right) in left.iter().zip(right).rev() {
work.push((left, right));
}
}
(Value::Map(left), Value::Map(right)) if left.len() == right.len() => {
let mut matched = vec![false; right.len()];
for (left_key, left_value) in left {
let Some(index) =
right
.iter()
.enumerate()
.find_map(|(index, (right_key, _))| {
(!matched[index] && scalar_equal(left_key, right_key))
.then_some(index)
})
else {
return false;
};
matched[index] = true;
work.push((left_value, &right[index].1));
}
}
_ => return false,
}
}
true
}
enum ParsedItem {
Value(Value),
Array(usize),
Map(usize),
}
enum Frame {
Array {
remaining: usize,
values: Vec<Value>,
},
Map {
remaining: usize,
entries: Vec<(Value, Value)>,
pending_key: Option<Value>,
key_index: ScalarKeyIndex,
},
}
fn begin_frame(
mut frame: Frame,
frames: &mut Vec<Frame>,
limits: Limits,
allocation: &mut AllocationState<'_>,
) -> Result<()> {
if frames.len() >= limits.max_depth {
return Err(resource(
Resource::Depth,
limits.max_depth,
frames.len() + 1,
));
}
if frame_remaining(&frame) == 0 {
return Ok(());
}
charge_allocation(allocation, size_of::<Frame>())?;
frames.try_reserve_exact(1).map_err(|_| Error::Allocation {
size: frames.len() + 1,
})?;
match &mut frame {
Frame::Array { remaining, values } => {
validate_count(*remaining, limits.max_items)?;
charge_allocation(
allocation,
remaining
.checked_mul(size_of::<Value>())
.ok_or_else(|| resource(Resource::Bytes, allocation.maximum, usize::MAX))?,
)?;
values
.try_reserve_exact(*remaining)
.map_err(|_| Error::Allocation { size: *remaining })?;
}
Frame::Map {
remaining,
entries,
key_index,
..
} => {
validate_count(*remaining, limits.max_items)?;
let entry_count = *remaining / 2;
charge_allocation(
allocation,
entry_count
.checked_mul(size_of::<(Value, Value)>())
.ok_or_else(|| resource(Resource::Bytes, allocation.maximum, usize::MAX))?,
)?;
entries
.try_reserve_exact(entry_count)
.map_err(|_| Error::Allocation { size: entry_count })?;
key_index.reserve_with_budget(entry_count, allocation)?;
}
}
frames.push(frame);
Ok(())
}
fn accept_value(
mut value: Value,
frames: &mut Vec<Frame>,
root: &mut Option<Value>,
allocation: &mut AllocationState<'_>,
) -> Result<()> {
loop {
let Some(frame) = frames.last_mut() else {
*root = Some(value);
return Ok(());
};
match frame {
Frame::Array { remaining, values } => {
values.push(value);
*remaining -= 1;
}
Frame::Map {
remaining,
entries,
pending_key,
key_index,
} => {
if pending_key.is_none() {
let index = entries.len();
if !value.is_scalar_key() {
return Err(Error::NonScalarKey { index });
}
if key_index.contains(entries, &value) {
return Err(Error::DuplicateKey { index });
}
key_index.insert_with_budget(&value, index, allocation)?;
*pending_key = Some(value);
} else {
let key = pending_key.take().expect("map key was checked above");
entries.push((key, value));
}
*remaining -= 1;
}
}
if frame_remaining(frame) != 0 {
return Ok(());
}
let completed = frames.pop().expect("last frame exists");
value = match completed {
Frame::Array { values, .. } => Value::Array(values),
Frame::Map { entries, .. } => Value::Map(entries),
};
}
}
fn frame_remaining(frame: &Frame) -> usize {
match frame {
Frame::Array { remaining, .. } | Frame::Map { remaining, .. } => *remaining,
}
}
fn parse_item(
bytes: &[u8],
cursor: &mut usize,
limits: Limits,
allocation: &mut AllocationState<'_>,
) -> Result<ParsedItem> {
let offset = *cursor;
let (major, additional) = read_head(bytes, cursor)?;
match major {
0 | 1 => {
let argument = read_argument(bytes, cursor, additional, offset)?;
let magnitude = if major == 0 {
argument as u128
} else {
(argument as u128) + 1
};
charge_allocation(
allocation,
if magnitude == 0 {
0
} else {
(u128::BITS as usize - magnitude.leading_zeros() as usize).div_ceil(8)
},
)?;
let integer = if major == 0 {
Integer::from_u128(argument as u128)
} else {
let magnitude = (argument as u128) + 1;
Integer::from_sign_and_magnitude(true, magnitude.to_be_bytes())
};
validate_integer_magnitude(&integer, limits)?;
Ok(ParsedItem::Value(Value::Integer(integer)))
}
2 | 3 => {
let length = read_length(bytes, cursor, additional, offset, limits, Resource::Bytes)?;
let end = cursor
.checked_add(length)
.ok_or_else(|| resource(Resource::Bytes, limits.max_bytes, usize::MAX))?;
if end > bytes.len() {
return Err(Error::Truncated { offset: *cursor });
}
let content = &bytes[*cursor..end];
*cursor = end;
if major == 2 {
Ok(ParsedItem::Value(Value::Bytes(clone_bytes(
content, allocation,
)?)))
} else {
let text =
std::str::from_utf8(content).map_err(|_| Error::InvalidUtf8 { offset })?;
charge_allocation(allocation, content.len())?;
let mut owned = String::new();
owned
.try_reserve_exact(content.len())
.map_err(|_| Error::Allocation {
size: content.len(),
})?;
owned.push_str(text);
Ok(ParsedItem::Value(Value::TextString(owned)))
}
}
4 => {
let length = read_length(bytes, cursor, additional, offset, limits, Resource::Items)?;
if length > limits.max_items {
return Err(resource(Resource::Items, limits.max_items, length));
}
Ok(ParsedItem::Array(length))
}
5 => {
let length = read_length(bytes, cursor, additional, offset, limits, Resource::Items)?;
if length > limits.max_items {
return Err(resource(Resource::Items, limits.max_items, length));
}
Ok(ParsedItem::Map(length))
}
6 => {
let tag = read_argument(bytes, cursor, additional, offset)?;
if tag != 2 && tag != 3 {
return Err(Error::UnsupportedType {
offset,
major,
additional,
});
}
let bignum_offset = *cursor;
let (bignum_major, bignum_additional) = read_head(bytes, cursor)?;
if bignum_major != 2 {
return Err(Error::InvalidInteger {
offset: bignum_offset,
reason: "bignum tag must wrap a byte string",
});
}
let length = read_length(
bytes,
cursor,
bignum_additional,
bignum_offset,
limits,
Resource::IntegerBytes,
)?;
if length == 0 {
return Err(Error::InvalidInteger {
offset,
reason: "bignum magnitude must not be empty",
});
}
if length > limits.max_integer_bytes {
return Err(resource(
Resource::IntegerBytes,
limits.max_integer_bytes,
length,
));
}
let end = cursor.checked_add(length).ok_or_else(|| {
resource(Resource::IntegerBytes, limits.max_integer_bytes, usize::MAX)
})?;
if end > bytes.len() {
return Err(Error::Truncated { offset: *cursor });
}
let magnitude = &bytes[*cursor..end];
*cursor = end;
if magnitude[0] == 0 {
return Err(Error::InvalidInteger {
offset,
reason: "bignum magnitude is not minimal",
});
}
let model_magnitude = if tag == 3 {
add_one_be(magnitude, limits.max_integer_bytes, allocation)?
} else {
clone_bytes(magnitude, allocation)?
};
Ok(ParsedItem::Value(Value::Integer(
Integer::from_owned_sign_and_magnitude(tag == 3, model_magnitude),
)))
}
7 => match additional {
20 => Ok(ParsedItem::Value(Value::Boolean(false))),
21 => Ok(ParsedItem::Value(Value::Boolean(true))),
22 => Ok(ParsedItem::Value(Value::Null)),
23 => Ok(ParsedItem::Value(Value::Undefined)),
25 => {
let raw = read_fixed(bytes, cursor, 2, offset)?;
Ok(ParsedItem::Value(Value::Float(Float {
width: FloatWidth::Bits16,
raw_bits: u64::from(u16::from_be_bytes([raw[0], raw[1]])),
})))
}
26 => {
let raw = read_fixed(bytes, cursor, 4, offset)?;
Ok(ParsedItem::Value(Value::Float(Float {
width: FloatWidth::Bits32,
raw_bits: u64::from(u32::from_be_bytes([raw[0], raw[1], raw[2], raw[3]])),
})))
}
27 => {
let raw = read_fixed(bytes, cursor, 8, offset)?;
Ok(ParsedItem::Value(Value::Float(Float {
width: FloatWidth::Bits64,
raw_bits: u64::from_be_bytes([
raw[0], raw[1], raw[2], raw[3], raw[4], raw[5], raw[6], raw[7],
]),
})))
}
31 => Err(Error::IndefiniteLength { offset }),
_ => Err(Error::UnsupportedType {
offset,
major,
additional,
}),
},
_ => Err(Error::UnsupportedType {
offset,
major,
additional,
}),
}
}
fn clone_bytes(bytes: &[u8], allocation: &mut AllocationState<'_>) -> Result<Vec<u8>> {
charge_allocation(allocation, bytes.len())?;
let mut owned = Vec::new();
owned
.try_reserve_exact(bytes.len())
.map_err(|_| Error::Allocation { size: bytes.len() })?;
owned.extend_from_slice(bytes);
Ok(owned)
}
fn add_one_be(
bytes: &[u8],
maximum: usize,
allocation: &mut AllocationState<'_>,
) -> Result<Vec<u8>> {
let result_length = if bytes.iter().all(|byte| *byte == u8::MAX) {
bytes
.len()
.checked_add(1)
.ok_or_else(|| resource(Resource::IntegerBytes, maximum, usize::MAX))?
} else {
bytes.len()
};
if result_length > maximum {
return Err(resource(Resource::IntegerBytes, maximum, result_length));
}
charge_allocation(allocation, result_length)?;
let mut result = Vec::new();
result
.try_reserve_exact(result_length)
.map_err(|_| Error::Allocation {
size: result_length,
})?;
result.extend_from_slice(bytes);
let mut carry = 1u16;
for byte in result.iter_mut().rev() {
let value = u16::from(*byte) + carry;
*byte = value as u8;
carry = value >> 8;
}
if carry != 0 {
debug_assert_eq!(result_length, bytes.len() + 1);
result.insert(0, carry as u8);
}
Ok(result)
}
fn read_head(bytes: &[u8], cursor: &mut usize) -> Result<(u8, u8)> {
let offset = *cursor;
let byte = *bytes.get(*cursor).ok_or(Error::Truncated { offset })?;
*cursor += 1;
Ok((byte >> 5, byte & 0x1f))
}
fn read_argument(bytes: &[u8], cursor: &mut usize, additional: u8, offset: usize) -> Result<u64> {
match additional {
0..=23 => Ok(u64::from(additional)),
24 => Ok(u64::from(read_fixed(bytes, cursor, 1, offset)?[0])),
25 => {
let raw = read_fixed(bytes, cursor, 2, offset)?;
Ok(u64::from(u16::from_be_bytes([raw[0], raw[1]])))
}
26 => {
let raw = read_fixed(bytes, cursor, 4, offset)?;
Ok(u64::from(u32::from_be_bytes([
raw[0], raw[1], raw[2], raw[3],
])))
}
27 => {
let raw = read_fixed(bytes, cursor, 8, offset)?;
Ok(u64::from_be_bytes([
raw[0], raw[1], raw[2], raw[3], raw[4], raw[5], raw[6], raw[7],
]))
}
31 => Err(Error::IndefiniteLength { offset }),
_ => Err(Error::InvalidEncoding {
offset,
reason: "invalid additional information",
}),
}
}
fn read_length(
bytes: &[u8],
cursor: &mut usize,
additional: u8,
offset: usize,
limits: Limits,
resource_kind: Resource,
) -> Result<usize> {
let argument = read_argument(bytes, cursor, additional, offset)?;
let maximum = match resource_kind {
Resource::Bytes => limits.max_bytes,
Resource::Depth => limits.max_depth,
Resource::Items => limits.max_items,
Resource::IntegerBytes => limits.max_integer_bytes,
};
let length =
usize::try_from(argument).map_err(|_| resource(resource_kind, maximum, usize::MAX))?;
if length > maximum {
return Err(resource(resource_kind, maximum, length));
}
Ok(length)
}
fn read_fixed<'a>(
bytes: &'a [u8],
cursor: &mut usize,
length: usize,
offset: usize,
) -> Result<&'a [u8]> {
let end = cursor
.checked_add(length)
.ok_or(Error::Truncated { offset })?;
if end > bytes.len() {
return Err(Error::Truncated { offset: *cursor });
}
let result = &bytes[*cursor..end];
*cursor = end;
Ok(result)
}
#[allow(dead_code)]
fn compare_integers(left: &Integer, right: &Integer) -> Ordering {
match (left.negative, right.negative) {
(true, false) => Ordering::Less,
(false, true) => Ordering::Greater,
(negative, _) => {
let magnitude = left.magnitude.len().cmp(&right.magnitude.len());
let magnitude = if magnitude == Ordering::Equal {
left.magnitude.cmp(&right.magnitude)
} else {
magnitude
};
if negative {
magnitude.reverse()
} else {
magnitude
}
}
}
}