use core::cmp::Ordering;
use minicbor::data::Type;
use minicbor::Decoder;
use crate::error::DecodeError;
pub(crate) fn cde_order(a: &[u8], b: &[u8]) -> Ordering {
a.len().cmp(&b.len()).then_with(|| a.cmp(b))
}
fn minimal_uint_head_len(v: u64) -> usize {
match v {
0..=23 => 1,
24..=0xFF => 2,
0x100..=0xFFFF => 3,
0x1_0000..=0xFFFF_FFFF => 5,
_ => 9,
}
}
fn minimal_int_head_len(v: i64) -> usize {
if v >= 0 {
minimal_uint_head_len(v as u64)
} else {
let argument = (v as u64).wrapping_neg().wrapping_sub(1);
minimal_uint_head_len(argument)
}
}
fn check_head_width(width: usize, minimal: usize) -> Result<(), DecodeError> {
if width == minimal {
Ok(())
} else {
Err(DecodeError::NonMinimalHead)
}
}
pub fn definite_map(d: &mut Decoder<'_>) -> Result<u64, DecodeError> {
let before = d.position();
let n = d
.map()
.map_err(DecodeError::from_minicbor)?
.ok_or(DecodeError::IndefiniteLength)?;
check_head_width(d.position() - before, minimal_uint_head_len(n))?;
Ok(n)
}
pub(crate) fn definite_array(d: &mut Decoder<'_>) -> Result<u64, DecodeError> {
let before = d.position();
let n = d
.array()
.map_err(DecodeError::from_minicbor)?
.ok_or(DecodeError::IndefiniteLength)?;
check_head_width(d.position() - before, minimal_uint_head_len(n))?;
Ok(n)
}
pub(crate) fn text_key<'b>(d: &mut Decoder<'b>) -> Result<&'b str, DecodeError> {
let before = d.position();
match d.datatype().map_err(DecodeError::from_minicbor)? {
Type::String => {
let key = d.str().map_err(DecodeError::from_minicbor)?;
check_head_width(
d.position() - before - key.len(),
minimal_uint_head_len(key.len() as u64),
)?;
Ok(key)
}
other => Err(DecodeError::UnexpectedType {
expected: "text map key",
found: other.to_string(),
}),
}
}
pub fn text_value(d: &mut Decoder<'_>) -> Result<String, DecodeError> {
let before = d.position();
match d.datatype().map_err(DecodeError::from_minicbor)? {
Type::String => {
let value = d.str().map_err(DecodeError::from_minicbor)?.to_owned();
check_head_width(
d.position() - before - value.len(),
minimal_uint_head_len(value.len() as u64),
)?;
Ok(value)
}
other => Err(DecodeError::UnexpectedType {
expected: "text string",
found: other.to_string(),
}),
}
}
pub fn bytes_value(d: &mut Decoder<'_>) -> Result<Vec<u8>, DecodeError> {
let before = d.position();
match d.datatype().map_err(DecodeError::from_minicbor)? {
Type::Bytes => {
let value = d.bytes().map_err(DecodeError::from_minicbor)?.to_vec();
check_head_width(
d.position() - before - value.len(),
minimal_uint_head_len(value.len() as u64),
)?;
Ok(value)
}
other => Err(DecodeError::UnexpectedType {
expected: "byte string",
found: other.to_string(),
}),
}
}
pub const MAX_EXACT_INTEGER: u64 = (1 << 53) - 1;
pub fn exact_uint_value(d: &mut Decoder<'_>) -> Result<u64, DecodeError> {
let value = uint_value(d)?;
if value > MAX_EXACT_INTEGER {
return Err(DecodeError::Constraint(
"integer above 2^53 - 1, which a TypeScript peer cannot hold",
));
}
Ok(value)
}
pub fn uint_value(d: &mut Decoder<'_>) -> Result<u64, DecodeError> {
let before = d.position();
match d.datatype().map_err(DecodeError::from_minicbor)? {
Type::U8 | Type::U16 | Type::U32 | Type::U64 => {
let v = d.u64().map_err(DecodeError::from_minicbor)?;
check_head_width(d.position() - before, minimal_uint_head_len(v))?;
Ok(v)
}
other => Err(DecodeError::UnexpectedType {
expected: "unsigned integer",
found: other.to_string(),
}),
}
}
pub(crate) fn int_value(d: &mut Decoder<'_>) -> Result<i64, DecodeError> {
let before = d.position();
match d.datatype().map_err(DecodeError::from_minicbor)? {
Type::U8 | Type::U16 | Type::U32 | Type::U64 => {
let v = d.u64().map_err(DecodeError::from_minicbor)?;
check_head_width(d.position() - before, minimal_uint_head_len(v))?;
i64::try_from(v)
.map_err(|_| DecodeError::Constraint("integer above i64::MAX where int expected"))
}
Type::I8 | Type::I16 | Type::I32 | Type::I64 | Type::Int => {
let v = d.i64().map_err(DecodeError::from_minicbor)?;
check_head_width(d.position() - before, minimal_int_head_len(v))?;
Ok(v)
}
other => Err(DecodeError::UnexpectedType {
expected: "integer",
found: other.to_string(),
}),
}
}
pub(crate) fn literal(d: &mut Decoder<'_>, expected: &'static str) -> Result<(), DecodeError> {
let found = text_value(d)?;
if found == expected {
Ok(())
} else {
Err(DecodeError::BadLiteral { expected, found })
}
}
pub fn set_once<T>(slot: &mut Option<T>, value: T) -> Result<(), DecodeError> {
if slot.is_some() {
return Err(DecodeError::DuplicateKey);
}
*slot = Some(value);
Ok(())
}
pub(crate) fn encoded_text(s: &str) -> Vec<u8> {
let mut out = Vec::with_capacity(s.len() + 1);
push_head(&mut out, 0x60, s.len() as u64);
out.extend_from_slice(s.as_bytes());
out
}
pub(crate) fn push_head(out: &mut Vec<u8>, major_byte: u8, argument: u64) {
let m = major_byte & 0xE0;
match minimal_uint_head_len(argument) {
1 => out.push(m | argument as u8),
2 => {
out.push(m | 24);
out.push(argument as u8);
}
3 => {
out.push(m | 25);
out.extend_from_slice(&(argument as u16).to_be_bytes());
}
5 => {
out.push(m | 26);
out.extend_from_slice(&(argument as u32).to_be_bytes());
}
_ => {
out.push(m | 27);
out.extend_from_slice(&argument.to_be_bytes());
}
}
}
pub struct KeyOrder {
prev: Option<Vec<u8>>,
}
impl Default for KeyOrder {
fn default() -> Self {
Self::new()
}
}
impl KeyOrder {
pub fn new() -> Self {
KeyOrder { prev: None }
}
pub fn push(&mut self, encoded_key: &[u8]) -> Result<(), DecodeError> {
if let Some(prev) = &self.prev {
match cde_order(prev, encoded_key) {
Ordering::Less => {}
Ordering::Equal => return Err(DecodeError::DuplicateKey),
Ordering::Greater => return Err(DecodeError::UnsortedMapKeys),
}
}
self.prev = Some(encoded_key.to_vec());
Ok(())
}
}
pub struct MapDecoder {
remaining: u64,
order: KeyOrder,
}
impl MapDecoder {
pub fn new(d: &mut Decoder<'_>) -> Result<Self, DecodeError> {
Ok(MapDecoder {
remaining: definite_map(d)?,
order: KeyOrder::new(),
})
}
pub fn next_key<'b>(&mut self, d: &mut Decoder<'b>) -> Result<Option<&'b str>, DecodeError> {
if self.remaining == 0 {
return Ok(None);
}
self.remaining -= 1;
let key = text_key(d)?;
self.order.push(&encoded_text(key))?;
Ok(Some(key))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn minimal_head_len_table() {
assert_eq!(minimal_uint_head_len(0), 1);
assert_eq!(minimal_uint_head_len(23), 1);
assert_eq!(minimal_uint_head_len(24), 2);
assert_eq!(minimal_uint_head_len(0xFF), 2);
assert_eq!(minimal_uint_head_len(0x100), 3);
assert_eq!(minimal_uint_head_len(0xFFFF), 3);
assert_eq!(minimal_uint_head_len(0x1_0000), 5);
assert_eq!(minimal_uint_head_len(0xFFFF_FFFF), 5);
assert_eq!(minimal_uint_head_len(0x1_0000_0000), 9);
assert_eq!(minimal_int_head_len(-1), 1);
assert_eq!(minimal_int_head_len(-24), 1);
assert_eq!(minimal_int_head_len(-25), 2);
assert_eq!(minimal_int_head_len(-256), 2);
assert_eq!(minimal_int_head_len(-257), 3);
}
#[test]
fn non_minimal_uint_head_rejected() {
let mut d = Decoder::new(&[0x18, 0x05]);
assert_eq!(uint_value(&mut d), Err(DecodeError::NonMinimalHead));
let mut d = Decoder::new(&[0x05]);
assert_eq!(uint_value(&mut d), Ok(5));
}
#[test]
fn non_minimal_map_head_rejected() {
let mut d = Decoder::new(&[0xB8, 0x01]);
assert_eq!(definite_map(&mut d), Err(DecodeError::NonMinimalHead));
let mut d = Decoder::new(&[0xA1]);
assert_eq!(definite_map(&mut d), Ok(1));
}
#[test]
fn non_minimal_string_head_rejected() {
let mut d = Decoder::new(&[0x78, 0x01, b'a']);
assert_eq!(text_value(&mut d), Err(DecodeError::NonMinimalHead));
let mut d = Decoder::new(&[0x61, b'a']);
assert_eq!(text_value(&mut d), Ok("a".to_owned()));
}
#[test]
fn key_order_rejects_duplicates_and_misordering() {
let mut order = KeyOrder::new();
assert_eq!(order.push(b"\x63\x61\x62\x63"), Ok(())); assert_eq!(
order.push(b"\x63\x61\x62\x63"),
Err(DecodeError::DuplicateKey)
);
assert_eq!(
order.push(b"\x62\x7a\x7a"), Err(DecodeError::UnsortedMapKeys)
);
assert_eq!(order.push(b"\x64\x61\x62\x63\x64"), Ok(())); }
#[test]
fn map_decoder_rejects_unsorted_keys() {
let bytes = [0xA2, 0x61, b'b', 0x01, 0x61, b'a', 0x02];
let mut d = Decoder::new(&bytes);
let mut m = MapDecoder::new(&mut d).expect("map head");
assert_eq!(m.next_key(&mut d), Ok(Some("b")));
assert_eq!(uint_value(&mut d), Ok(1)); assert_eq!(m.next_key(&mut d), Err(DecodeError::UnsortedMapKeys));
}
}