484 lines
17 KiB
Rust
484 lines
17 KiB
Rust
use crate::{ValueClass, AtomClass, Atom};
|
|
use crate::error::{self, ExpectedKind};
|
|
|
|
use num_bigint::BigInt;
|
|
use num_traits::cast::{FromPrimitive, ToPrimitive};
|
|
|
|
use std::borrow::Cow;
|
|
use std::convert::TryFrom;
|
|
use std::convert::TryInto;
|
|
use std::io;
|
|
use std::marker::PhantomData;
|
|
|
|
use super::constants::Tag;
|
|
use super::super::{
|
|
CompoundClass,
|
|
|
|
boundary as B,
|
|
reader::{
|
|
NextToken,
|
|
Reader,
|
|
ReaderResult,
|
|
},
|
|
signed_integer::SignedInteger,
|
|
source::BinarySource,
|
|
};
|
|
|
|
pub struct PackedReader<'de, S: BinarySource<'de>> {
|
|
pub source: S,
|
|
phantom: PhantomData<&'de ()>,
|
|
}
|
|
|
|
impl<'de, S: BinarySource<'de>> BinarySource<'de> for PackedReader<'de, S> {
|
|
#[inline(always)]
|
|
fn mark(&mut self) -> io::Result<usize> {
|
|
self.source.mark()
|
|
}
|
|
#[inline(always)]
|
|
fn restore(&mut self, mark: usize) -> io::Result<()> {
|
|
self.source.restore(mark)
|
|
}
|
|
#[inline(always)]
|
|
fn skip(&mut self) -> io::Result<()> {
|
|
self.source.skip()
|
|
}
|
|
#[inline(always)]
|
|
fn peek(&mut self) -> io::Result<Option<u8>> {
|
|
self.source.peek()
|
|
}
|
|
#[inline(always)]
|
|
fn readbytes(&mut self, count: u64) -> io::Result<Cow<'de, [u8]>> {
|
|
self.source.readbytes(count)
|
|
}
|
|
#[inline(always)]
|
|
fn readbytes_into(&mut self, bs: &mut [u8]) -> io::Result<()> {
|
|
self.source.readbytes_into(bs)
|
|
}
|
|
#[inline(always)]
|
|
fn input_position(&mut self) -> io::Result<Option<usize>> {
|
|
self.source.input_position()
|
|
}
|
|
#[inline(always)]
|
|
fn discard(&mut self, count: u64) -> io::Result<()> {
|
|
self.source.discard(count)
|
|
}
|
|
#[inline(always)]
|
|
fn read_to_end(&mut self) -> io::Result<Cow<'de, [u8]>> {
|
|
self.source.read_to_end()
|
|
}
|
|
}
|
|
|
|
fn out_of_range<I: Into<BigInt>>(i: I) -> error::Error {
|
|
error::Error::NumberOutOfRange(i.into())
|
|
}
|
|
|
|
impl<'de, S: BinarySource<'de>> PackedReader<'de, S> {
|
|
#[inline(always)]
|
|
pub fn new(source: S) -> Self {
|
|
PackedReader { source, phantom: PhantomData }
|
|
}
|
|
|
|
#[inline(always)]
|
|
fn varint(&mut self) -> io::Result<u64> {
|
|
let mut shift = 0;
|
|
let mut acc: u64 = 0;
|
|
loop {
|
|
let v = self.read()?;
|
|
if shift == 63 && v > 1 { Err(error::Error::Message("PackedReader length too long".to_string()))? }
|
|
acc |= ((v & 0x7f) as u64) << shift;
|
|
shift += 7;
|
|
if v & 0x80 == 0 { return Ok(acc) }
|
|
if shift >= 70 { Err(error::Error::Message("PackedReader length too long".to_string()))? }
|
|
}
|
|
}
|
|
|
|
#[inline(always)]
|
|
fn peekend(&mut self) -> io::Result<bool> {
|
|
if self.peek()? == Some(Tag::End.into()) {
|
|
self.skip()?;
|
|
Ok(true)
|
|
} else {
|
|
Ok(false)
|
|
}
|
|
}
|
|
|
|
#[inline(always)]
|
|
fn peek_next_nonannotation_tag(&mut self) -> ReaderResult<Tag> {
|
|
loop {
|
|
match Tag::try_from(self.peek_noeof()?)? {
|
|
Tag::Annotation => {
|
|
self.skip()?;
|
|
self.skip_value()?;
|
|
},
|
|
other => return Ok(other),
|
|
}
|
|
}
|
|
}
|
|
|
|
fn next_atomic(&mut self, expected_tag: Tag, k: ExpectedKind) -> ReaderResult<Cow<'de, [u8]>> {
|
|
let actual_tag = self.peek_next_nonannotation_tag()?;
|
|
if actual_tag == expected_tag {
|
|
self.skip()?;
|
|
let count = self.varint()?;
|
|
Ok(self.readbytes(count)?)
|
|
} else {
|
|
Err(error::Error::Expected(k))
|
|
}
|
|
}
|
|
|
|
fn next_compound(&mut self, expected_tag: Tag, k: ExpectedKind) -> ReaderResult<()>
|
|
{
|
|
let actual_tag = self.peek_next_nonannotation_tag()?;
|
|
if actual_tag == expected_tag {
|
|
self.skip()?;
|
|
Ok(())
|
|
} else {
|
|
Err(error::Error::Expected(k))
|
|
}
|
|
}
|
|
|
|
#[inline(always)]
|
|
fn read_signed_integer(&mut self, count: u64) -> io::Result<SignedInteger> {
|
|
let count_u64 = count;
|
|
let count: usize = count.try_into().map_err(
|
|
|_| error::Error::Message("Signed integer too long".to_string()))?;
|
|
|
|
if count == 0 {
|
|
return Ok(SignedInteger::from(0_i128));
|
|
}
|
|
|
|
if count > 16 {
|
|
let bs = self.readbytes(count_u64)?;
|
|
if (bs[0] & 0x80) == 0 {
|
|
// Positive or zero.
|
|
let mut i = 0;
|
|
while i < count && bs[i] == 0 { i += 1; }
|
|
if count - i <= 16 {
|
|
Ok(SignedInteger::from(u128::from_be_bytes(bs[bs.len() - 16..].try_into().unwrap())))
|
|
} else {
|
|
Ok(SignedInteger::from(Cow::Owned(BigInt::from_bytes_be(num_bigint::Sign::Plus, &bs[i..]))))
|
|
}
|
|
} else {
|
|
// Negative.
|
|
let mut i = 0;
|
|
while i < count && bs[i] == 0xff { i += 1; }
|
|
if count - i <= 16 {
|
|
Ok(SignedInteger::from(i128::from_be_bytes(bs[bs.len() - 16..].try_into().unwrap())))
|
|
} else {
|
|
Ok(SignedInteger::from(Cow::Owned(BigInt::from_signed_bytes_be(&bs))))
|
|
}
|
|
}
|
|
} else {
|
|
let first_byte = self.read()?;
|
|
let prefix_byte = if (first_byte & 0x80) == 0 { 0x00 } else { 0xff };
|
|
let mut bs = [prefix_byte; 16];
|
|
bs[16 - count] = first_byte;
|
|
self.readbytes_into(&mut bs[16 - (count - 1)..])?;
|
|
Ok(SignedInteger::from(i128::from_be_bytes(bs)))
|
|
}
|
|
}
|
|
|
|
#[inline(always)]
|
|
fn next_unsigned<T: FromPrimitive, F>(&mut self, f: F) -> ReaderResult<T>
|
|
where
|
|
F: FnOnce(u128) -> Option<T>
|
|
{
|
|
let tag = self.peek_next_nonannotation_tag()?;
|
|
match tag {
|
|
Tag::SmallInteger(v) => {
|
|
self.skip()?;
|
|
if v < 0 {
|
|
Err(out_of_range(v))
|
|
} else {
|
|
f(v as u128).ok_or_else(|| out_of_range(v))
|
|
}
|
|
}
|
|
Tag::MediumInteger(count) => {
|
|
self.skip()?;
|
|
let n = &self.read_signed_integer(count.into())?;
|
|
let i = n.try_into().map_err(|_| out_of_range(n))?;
|
|
f(i).ok_or_else(|| out_of_range(i))
|
|
}
|
|
Tag::SignedInteger => {
|
|
self.skip()?;
|
|
let count = self.varint()?;
|
|
let n = &self.read_signed_integer(count)?;
|
|
let i = n.try_into().map_err(|_| out_of_range(n))?;
|
|
f(i).ok_or_else(|| out_of_range(i))
|
|
}
|
|
_ => Err(error::Error::Expected(ExpectedKind::SignedInteger))
|
|
}
|
|
}
|
|
|
|
#[inline(always)]
|
|
fn next_signed<T: FromPrimitive, F>(&mut self, f: F) -> ReaderResult<T>
|
|
where
|
|
F: FnOnce(i128) -> Option<T>
|
|
{
|
|
let tag = self.peek_next_nonannotation_tag()?;
|
|
match tag {
|
|
Tag::SmallInteger(v) => {
|
|
self.skip()?;
|
|
f(v.into()).ok_or_else(|| out_of_range(v))
|
|
}
|
|
Tag::MediumInteger(count) => {
|
|
self.skip()?;
|
|
let n = &self.read_signed_integer(count.into())?;
|
|
let i = n.try_into().map_err(|_| out_of_range(n))?;
|
|
f(i).ok_or_else(|| out_of_range(i))
|
|
}
|
|
Tag::SignedInteger => {
|
|
self.skip()?;
|
|
let count = self.varint()?;
|
|
let n = &self.read_signed_integer(count)?;
|
|
let i = n.try_into().map_err(|_| out_of_range(n))?;
|
|
f(i).ok_or_else(|| out_of_range(i))
|
|
}
|
|
_ => Err(error::Error::Expected(ExpectedKind::SignedInteger))
|
|
}
|
|
}
|
|
|
|
#[inline(always)]
|
|
fn decodestr<'a>(&mut self, cow: Cow<'a, [u8]>) -> io::Result<Cow<'a, str>> {
|
|
match cow {
|
|
Cow::Borrowed(bs) =>
|
|
Ok(Cow::Borrowed(std::str::from_utf8(bs).map_err(|_| self.syntax_error("Invalid UTF-8"))?)),
|
|
Cow::Owned(bs) =>
|
|
Ok(Cow::Owned(String::from_utf8(bs).map_err(|_| self.syntax_error("Invalid UTF-8"))?)),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl From<Tag> for Option<NextToken> {
|
|
#[inline(always)]
|
|
fn from(tag: Tag) -> Self {
|
|
match tag {
|
|
Tag::False => Some(NextToken::Value(ValueClass::Atomic(AtomClass::Boolean))),
|
|
Tag::True => Some(NextToken::Value(ValueClass::Atomic(AtomClass::Boolean))),
|
|
Tag::Float => Some(NextToken::Value(ValueClass::Atomic(AtomClass::Float))),
|
|
Tag::Double => Some(NextToken::Value(ValueClass::Atomic(AtomClass::Double))),
|
|
Tag::Annotation => Some(NextToken::Annotation),
|
|
Tag::Embedded => Some(NextToken::Value(ValueClass::Embedded)),
|
|
Tag::SmallInteger(_) => Some(NextToken::Value(ValueClass::Atomic(AtomClass::SignedInteger))),
|
|
Tag::MediumInteger(_) => Some(NextToken::Value(ValueClass::Atomic(AtomClass::SignedInteger))),
|
|
Tag::SignedInteger => Some(NextToken::Value(ValueClass::Atomic(AtomClass::SignedInteger))),
|
|
Tag::String => Some(NextToken::Value(ValueClass::Atomic(AtomClass::String))),
|
|
Tag::ByteString => Some(NextToken::Value(ValueClass::Atomic(AtomClass::ByteString))),
|
|
Tag::Symbol => Some(NextToken::Value(ValueClass::Atomic(AtomClass::Symbol))),
|
|
Tag::Record => Some(NextToken::Value(ValueClass::Compound(CompoundClass::Record))),
|
|
Tag::Sequence => Some(NextToken::Value(ValueClass::Compound(CompoundClass::Sequence))),
|
|
Tag::Set => Some(NextToken::Value(ValueClass::Compound(CompoundClass::Set))),
|
|
Tag::Dictionary => Some(NextToken::Value(ValueClass::Compound(CompoundClass::Dictionary))),
|
|
Tag::End => None,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<'de, S: BinarySource<'de>> Reader<'de> for PackedReader<'de, S> {
|
|
fn peek_class(&mut self) -> io::Result<Option<NextToken>> {
|
|
let tag = Tag::try_from(match self.peek()? {
|
|
None => return Ok(None),
|
|
Some(b) => b,
|
|
})?;
|
|
match tag.into() {
|
|
Some(n) => Ok(Some(n)),
|
|
None => Err(self.syntax_error("Unexpected tag")),
|
|
}
|
|
}
|
|
|
|
fn next_atom(&mut self) -> ReaderResult<Atom<'de>> {
|
|
match Tag::try_from(self.read()?)? {
|
|
Tag::False => Ok(Atom::Boolean(false)),
|
|
Tag::True => Ok(Atom::Boolean(true)),
|
|
Tag::Float => {
|
|
let mut bs = [0; 4];
|
|
self.readbytes_into(&mut bs)?;
|
|
Ok(Atom::Float(f32::from_bits(u32::from_be_bytes(bs))))
|
|
}
|
|
Tag::Double => {
|
|
let mut bs = [0; 8];
|
|
self.readbytes_into(&mut bs)?;
|
|
Ok(Atom::Double(f64::from_bits(u64::from_be_bytes(bs))))
|
|
}
|
|
Tag::SmallInteger(v) => Ok(Atom::SignedInteger(Cow::Owned(v.into()))),
|
|
Tag::MediumInteger(count) => Ok(Atom::SignedInteger(Cow::Owned(self.read_signed_integer(count.into())?))),
|
|
Tag::SignedInteger => {
|
|
let count = self.varint()?;
|
|
Ok(Atom::SignedInteger(Cow::Owned(self.read_signed_integer(count)?)))
|
|
}
|
|
Tag::String => {
|
|
let count = self.varint()?;
|
|
let bs = self.readbytes(count)?;
|
|
Ok(Atom::String(self.decodestr(bs)?))
|
|
}
|
|
Tag::ByteString => {
|
|
let count = self.varint()?;
|
|
Ok(Atom::ByteString(self.readbytes(count)?))
|
|
}
|
|
Tag::Symbol => {
|
|
let count = self.varint()?;
|
|
let bs = self.readbytes(count)?;
|
|
Ok(Atom::Symbol(self.decodestr(bs)?))
|
|
}
|
|
_ => Err(self.syntax_error("Expected atomic value"))?,
|
|
}
|
|
}
|
|
|
|
#[inline(always)]
|
|
fn boundary(&mut self, _b: &B::Type) -> ReaderResult<()> {
|
|
Ok(())
|
|
}
|
|
|
|
#[inline(always)]
|
|
fn open_record(&mut self) -> ReaderResult<()> {
|
|
self.next_compound(Tag::Record, ExpectedKind::Record)
|
|
}
|
|
|
|
#[inline(always)]
|
|
fn open_sequence(&mut self) -> ReaderResult<()> {
|
|
self.next_compound(Tag::Sequence, ExpectedKind::Sequence)
|
|
}
|
|
|
|
#[inline(always)]
|
|
fn open_set(&mut self) -> ReaderResult<()> {
|
|
self.next_compound(Tag::Set, ExpectedKind::Set)
|
|
}
|
|
|
|
#[inline(always)]
|
|
fn open_dictionary(&mut self) -> ReaderResult<()> {
|
|
self.next_compound(Tag::Dictionary, ExpectedKind::Dictionary)
|
|
}
|
|
|
|
#[inline(always)]
|
|
fn close_compound(&mut self, _b: &mut B::Type, _i: &B::Item) -> ReaderResult<bool> {
|
|
Ok(self.peekend()?)
|
|
}
|
|
|
|
#[inline(always)]
|
|
fn open_embedded(&mut self) -> ReaderResult<()> {
|
|
self.next_compound(Tag::Embedded, ExpectedKind::Embedded)
|
|
}
|
|
|
|
#[inline(always)]
|
|
fn close_embedded(&mut self) -> ReaderResult<()> {
|
|
Ok(())
|
|
}
|
|
|
|
#[inline(always)]
|
|
fn mark(&mut self) -> io::Result<usize> {
|
|
self.source.mark()
|
|
}
|
|
|
|
#[inline(always)]
|
|
fn restore(&mut self, mark: usize) -> io::Result<()> {
|
|
self.source.restore(mark)
|
|
}
|
|
|
|
#[inline(always)]
|
|
fn next_boolean(&mut self) -> ReaderResult<bool> {
|
|
match self.peek_next_nonannotation_tag()? {
|
|
Tag::False => { self.skip()?; Ok(false) }
|
|
Tag::True => { self.skip()?; Ok(true) }
|
|
_ => Err(error::Error::Expected(ExpectedKind::Boolean)),
|
|
}
|
|
}
|
|
|
|
fn next_signedinteger(&mut self) -> ReaderResult<SignedInteger> {
|
|
let tag = self.peek_next_nonannotation_tag()?;
|
|
match tag {
|
|
Tag::SmallInteger(v) => {
|
|
self.skip()?;
|
|
Ok(SignedInteger::from(v as i32))
|
|
}
|
|
Tag::MediumInteger(count) => {
|
|
self.skip()?;
|
|
Ok(self.read_signed_integer(count.into())?)
|
|
}
|
|
Tag::SignedInteger => {
|
|
self.skip()?;
|
|
let count = self.varint()?;
|
|
Ok(self.read_signed_integer(count)?)
|
|
}
|
|
_ => Err(error::Error::Expected(ExpectedKind::SignedInteger))
|
|
}
|
|
}
|
|
|
|
fn next_i8(&mut self) -> ReaderResult<i8> { self.next_signed(|n| n.to_i8()) }
|
|
fn next_i16(&mut self) -> ReaderResult<i16> { self.next_signed(|n| n.to_i16()) }
|
|
fn next_i32(&mut self) -> ReaderResult<i32> { self.next_signed(|n| n.to_i32()) }
|
|
fn next_i64(&mut self) -> ReaderResult<i64> { self.next_signed(|n| n.to_i64()) }
|
|
fn next_i128(&mut self) -> ReaderResult<i128> { self.next_signed(|n| n.to_i128()) }
|
|
|
|
fn next_u8(&mut self) -> ReaderResult<u8> { self.next_unsigned(|n| n.to_u8()) }
|
|
fn next_u16(&mut self) -> ReaderResult<u16> { self.next_unsigned(|n| n.to_u16()) }
|
|
fn next_u32(&mut self) -> ReaderResult<u32> { self.next_unsigned(|n| n.to_u32()) }
|
|
fn next_u64(&mut self) -> ReaderResult<u64> { self.next_unsigned(|n| n.to_u64()) }
|
|
fn next_u128(&mut self) -> ReaderResult<u128> { self.next_unsigned(|n| n.to_u128()) }
|
|
|
|
fn next_float(&mut self) -> ReaderResult<f32> {
|
|
match self.peek_next_nonannotation_tag()? {
|
|
Tag::Float => {
|
|
self.skip()?;
|
|
let mut bs = [0; 4];
|
|
self.readbytes_into(&mut bs)?;
|
|
Ok(f32::from_bits(u32::from_be_bytes(bs)))
|
|
},
|
|
Tag::Double => {
|
|
self.skip()?;
|
|
let mut bs = [0; 8];
|
|
self.readbytes_into(&mut bs)?;
|
|
Ok(f64::from_bits(u64::from_be_bytes(bs)) as f32)
|
|
},
|
|
_ => Err(error::Error::Expected(ExpectedKind::Float)),
|
|
}
|
|
}
|
|
|
|
fn next_double(&mut self) -> ReaderResult<f64> {
|
|
match self.peek_next_nonannotation_tag()? {
|
|
Tag::Float => {
|
|
self.skip()?;
|
|
let mut bs = [0; 4];
|
|
self.readbytes_into(&mut bs)?;
|
|
Ok(f32::from_bits(u32::from_be_bytes(bs)) as f64)
|
|
},
|
|
Tag::Double => {
|
|
self.skip()?;
|
|
let mut bs = [0; 8];
|
|
self.readbytes_into(&mut bs)?;
|
|
Ok(f64::from_bits(u64::from_be_bytes(bs)))
|
|
},
|
|
_ => Err(error::Error::Expected(ExpectedKind::Double)),
|
|
}
|
|
}
|
|
|
|
fn next_str(&mut self) -> ReaderResult<Cow<'de, str>> {
|
|
let bs = self.next_atomic(Tag::String, ExpectedKind::Symbol)?;
|
|
Ok(self.decodestr(bs)?)
|
|
}
|
|
|
|
fn next_bytestring(&mut self) -> ReaderResult<Cow<'de, [u8]>> {
|
|
self.next_atomic(Tag::ByteString, ExpectedKind::Symbol)
|
|
}
|
|
|
|
fn next_symbol(&mut self) -> ReaderResult<Cow<'de, str>> {
|
|
let bs = self.next_atomic(Tag::Symbol, ExpectedKind::Symbol)?;
|
|
Ok(self.decodestr(bs)?)
|
|
}
|
|
|
|
fn open_annotation(&mut self) -> ReaderResult<()> {
|
|
if let Tag::Annotation = Tag::try_from(self.peek_noeof()?)? {
|
|
Ok(self.skip()?)
|
|
} else {
|
|
Err(error::Error::Expected(ExpectedKind::Annotation))
|
|
}
|
|
}
|
|
|
|
fn close_annotation(&mut self) -> ReaderResult<()> {
|
|
Ok(())
|
|
}
|
|
|
|
fn specialized(&mut self) -> Option<(&str, &mut dyn BinarySource<'de>)> {
|
|
Some(("packed", &mut self.source))
|
|
}
|
|
}
|