use crate::canonical::CborBytesRef;
use crate::wire::{self, Cursor};
use crate::{CborError, DecodeLimits, ErrorCode};
pub fn validate(bytes: &[u8], limits: DecodeLimits) -> Result<(), CborError> {
validate_canonical(bytes, limits).map(|_| ())
}
pub fn validate_canonical(
bytes: &'_ [u8],
limits: DecodeLimits,
) -> Result<CborBytesRef<'_>, CborError> {
if bytes.len() > limits.max_input_bytes {
return Err(CborError::new(ErrorCode::MessageLenLimitExceeded, 0));
}
let end = value_end_internal(bytes, 0, Some(limits))?;
if end != bytes.len() {
return Err(CborError::new(ErrorCode::TrailingBytes, end));
}
Ok(CborBytesRef::new(bytes))
}
#[cfg(feature = "alloc")]
pub fn value_end_trusted(data: &[u8], start: usize) -> Result<usize, CborError> {
let mut cursor = Cursor::<CborError>::with_pos(data, start);
let mut items_seen = 0;
wire::skip_one_value::<false, CborError>(&mut cursor, None, &mut items_seen, 0)?;
Ok(cursor.position())
}
fn value_end_internal(
data: &[u8],
start: usize,
limits: Option<DecodeLimits>,
) -> Result<usize, CborError> {
let mut cursor = Cursor::<CborError>::with_pos(data, start);
let mut items_seen = 0;
wire::skip_one_value::<true, CborError>(&mut cursor, limits.as_ref(), &mut items_seen, 0)?;
Ok(cursor.position())
}