466 lines
16 KiB
Rust
466 lines
16 KiB
Rust
use num::bigint::BigInt;
|
|
use num::traits::cast::{FromPrimitive, ToPrimitive};
|
|
use std::borrow::Cow;
|
|
use std::convert::TryFrom;
|
|
use std::convert::TryInto;
|
|
use std::marker::PhantomData;
|
|
use super::super::signed_integer::SignedInteger;
|
|
use super::super::value::{Value, NestedValue, Domain, IOValue, FALSE, TRUE, Map, Set, Record, Annotations};
|
|
|
|
use super::constants::Tag;
|
|
use super::super::reader::{
|
|
BinarySource,
|
|
BytesBinarySource,
|
|
ConfiguredReader,
|
|
IOBinarySource,
|
|
IOResult,
|
|
Reader,
|
|
ReaderResult,
|
|
};
|
|
|
|
use crate::error::{self, ExpectedKind, Received, is_eof_io_error, io_syntax_error};
|
|
|
|
pub struct PackedReader<'de, S: BinarySource<'de>> {
|
|
pub source: S,
|
|
phantom: PhantomData<&'de ()>,
|
|
}
|
|
|
|
impl<'de, S: BinarySource<'de>> BinarySource<'de> for PackedReader<'de, S> {
|
|
fn skip(&mut self) -> IOResult<()> {
|
|
self.source.skip()
|
|
}
|
|
fn peek(&mut self) -> IOResult<u8> {
|
|
self.source.peek()
|
|
}
|
|
fn readbytes(&mut self, count: usize) -> IOResult<Cow<'de, [u8]>> {
|
|
self.source.readbytes(count)
|
|
}
|
|
fn readbytes_into(&mut self, bs: &mut [u8]) -> IOResult<()> {
|
|
self.source.readbytes_into(bs)
|
|
}
|
|
}
|
|
|
|
fn out_of_range<I: Into<BigInt>>(i: I) -> error::Error {
|
|
error::Error::NumberOutOfRange(i.into())
|
|
}
|
|
|
|
impl<'de> PackedReader<'de, BytesBinarySource<'de>> {
|
|
pub fn decode_bytes(bytes: &'de [u8]) -> Self {
|
|
PackedReader::new(BytesBinarySource::new(bytes))
|
|
}
|
|
}
|
|
|
|
impl<'de, 'a, IOR: std::io::Read> PackedReader<'de, IOBinarySource<'a, IOR>> {
|
|
pub fn decode_read(read: &'a mut IOR) -> Self {
|
|
PackedReader::new(IOBinarySource::new(read))
|
|
}
|
|
}
|
|
|
|
impl<'de, S: BinarySource<'de>> PackedReader<'de, S> {
|
|
pub fn new(source: S) -> Self {
|
|
PackedReader { source, phantom: PhantomData }
|
|
}
|
|
|
|
fn read(&mut self) -> IOResult<u8> {
|
|
let v = self.peek()?;
|
|
self.skip()?;
|
|
Ok(v)
|
|
}
|
|
|
|
fn expected(&mut self, k: ExpectedKind) -> error::Error {
|
|
match self.demand_next(true) {
|
|
Ok(v) => error::Error::Expected(k, Received::ReceivedOtherValue(v)),
|
|
Err(e) => e.into()
|
|
}
|
|
}
|
|
|
|
fn varint(&mut self) -> IOResult<usize> {
|
|
let mut shift = 0;
|
|
let mut acc: usize = 0;
|
|
loop {
|
|
let v = self.read()?;
|
|
acc = acc | (((v & 0x7f) as usize) << shift);
|
|
shift = shift + 7;
|
|
if v & 0x80 == 0 { return Ok(acc) }
|
|
}
|
|
}
|
|
|
|
fn peekend(&mut self) -> IOResult<bool> {
|
|
if self.peek()? == Tag::End.into() {
|
|
self.skip()?;
|
|
Ok(true)
|
|
} else {
|
|
Ok(false)
|
|
}
|
|
}
|
|
|
|
fn peek_next_nonannotation_tag(&mut self) -> ReaderResult<Tag> {
|
|
loop {
|
|
match Tag::try_from(self.peek()?)? {
|
|
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(self.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(self.expected(k))
|
|
}
|
|
}
|
|
|
|
fn read_signed_integer(&mut self, count: usize) -> IOResult<SignedInteger> {
|
|
if count == 0 {
|
|
return Ok(SignedInteger::from(0 as i128));
|
|
}
|
|
|
|
if count > 16 {
|
|
let bs = self.readbytes(count)?;
|
|
if (bs[0] & 0x80) == 0 {
|
|
// Positive or zero.
|
|
let mut i = 0;
|
|
while i < count && bs[i] == 0 { i = 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 = 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)))
|
|
}
|
|
}
|
|
|
|
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().or_else(|_| 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().or_else(|_| Err(out_of_range(n)))?;
|
|
f(i).ok_or_else(|| out_of_range(i))
|
|
}
|
|
_ => Err(self.expected(ExpectedKind::SignedInteger))
|
|
}
|
|
}
|
|
|
|
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().or_else(|_| 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().or_else(|_| Err(out_of_range(n)))?;
|
|
f(i).ok_or_else(|| out_of_range(i))
|
|
}
|
|
_ => Err(self.expected(ExpectedKind::SignedInteger))
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<'de, S: BinarySource<'de>> Reader<'de> for PackedReader<'de, S> {
|
|
fn next(&mut self, read_annotations: bool) -> IOResult<Option<IOValue>> {
|
|
match self.peek() {
|
|
Err(e) if is_eof_io_error(&e) => return Ok(None),
|
|
Err(e) => return Err(e),
|
|
Ok(_) => (),
|
|
}
|
|
Ok(Some(match Tag::try_from(self.read()?)? {
|
|
Tag::False => FALSE.clone(),
|
|
Tag::True => TRUE.clone(),
|
|
Tag::Float => {
|
|
let mut bs = [0; 4];
|
|
self.readbytes_into(&mut bs)?;
|
|
Value::from(f32::from_bits(u32::from_be_bytes(bs))).wrap()
|
|
}
|
|
Tag::Double => {
|
|
let mut bs = [0; 8];
|
|
self.readbytes_into(&mut bs)?;
|
|
Value::from(f64::from_bits(u64::from_be_bytes(bs))).wrap()
|
|
}
|
|
Tag::Annotation => {
|
|
if read_annotations {
|
|
let mut annotations = vec![self.demand_next(read_annotations)?];
|
|
while Tag::try_from(self.peek()?)? == Tag::Annotation {
|
|
self.skip()?;
|
|
annotations.push(self.demand_next(read_annotations)?);
|
|
}
|
|
let (existing_annotations, v) = self.demand_next(read_annotations)?.pieces();
|
|
annotations.extend_from_slice(existing_annotations.slice());
|
|
IOValue::wrap(Annotations::new(Some(annotations)), v)
|
|
} else {
|
|
self.skip_value()?;
|
|
while Tag::try_from(self.peek()?)? == Tag::Annotation {
|
|
self.skip()?;
|
|
self.skip_value()?;
|
|
}
|
|
self.demand_next(read_annotations)?
|
|
}
|
|
}
|
|
Tag::Pointer => {
|
|
let v = self.demand_next(read_annotations)?;
|
|
Value::Domain(IOValue::from_preserves(v)).wrap()
|
|
}
|
|
Tag::SmallInteger(v) => {
|
|
// TODO: prebuild these in value.rs
|
|
Value::from(v).wrap()
|
|
}
|
|
Tag::MediumInteger(count) => {
|
|
let n = self.read_signed_integer(count.into())?;
|
|
Value::SignedInteger(n).wrap()
|
|
}
|
|
Tag::SignedInteger => {
|
|
let count = self.varint()?;
|
|
let n = self.read_signed_integer(count)?;
|
|
Value::SignedInteger(n).wrap()
|
|
}
|
|
Tag::String => {
|
|
let count = self.varint()?;
|
|
Value::String(decodestr(self.readbytes(count)?)?.into_owned()).wrap()
|
|
}
|
|
Tag::ByteString => {
|
|
let count = self.varint()?;
|
|
Value::ByteString(self.readbytes(count)?.into_owned()).wrap()
|
|
}
|
|
Tag::Symbol => {
|
|
let count = self.varint()?;
|
|
Value::Symbol(decodestr(self.readbytes(count)?)?.into_owned()).wrap()
|
|
}
|
|
Tag::Record => {
|
|
let iter = DelimitedStream { reader: self.configured(read_annotations) };
|
|
let vs = iter.collect::<IOResult<Vec<IOValue>>>()?;
|
|
if vs.len() < 1 {
|
|
return Err(io_syntax_error("Too few elements in encoded record"))
|
|
}
|
|
Value::Record(Record(vs)).wrap()
|
|
}
|
|
Tag::Sequence => {
|
|
let iter = DelimitedStream { reader: self.configured(read_annotations) };
|
|
let vs = iter.collect::<IOResult<Vec<IOValue>>>()?;
|
|
Value::Sequence(vs).wrap()
|
|
}
|
|
Tag::Set => {
|
|
let iter = DelimitedStream { reader: self.configured(read_annotations) };
|
|
let mut s = Set::new();
|
|
for res in iter { s.insert(res?); }
|
|
Value::Set(s).wrap()
|
|
}
|
|
Tag::Dictionary => {
|
|
let mut iter = DelimitedStream { reader: self.configured(read_annotations) };
|
|
let mut d = Map::new();
|
|
while let Some(kres) = iter.next() {
|
|
let k = kres?;
|
|
match iter.next() {
|
|
Some(vres) => {
|
|
let v = vres?;
|
|
d.insert(k, v);
|
|
}
|
|
None => return Err(io_syntax_error("Missing dictionary value")),
|
|
}
|
|
}
|
|
Value::Dictionary(d).wrap()
|
|
}
|
|
tag @ Tag::End => {
|
|
return Err(io_syntax_error(&format!("Invalid tag: {:?}", tag)));
|
|
}
|
|
}))
|
|
}
|
|
|
|
fn open_record(&mut self, arity: Option<usize>) -> ReaderResult<()> {
|
|
self.next_compound(Tag::Record, ExpectedKind::Record(arity))?;
|
|
self.ensure_more_expected()
|
|
}
|
|
|
|
fn open_sequence_or_set(&mut self) -> ReaderResult<()> {
|
|
match self.peek_next_nonannotation_tag()? {
|
|
Tag::Sequence | Tag::Set => {
|
|
self.skip()?;
|
|
Ok(())
|
|
}
|
|
_ => Err(self.expected(ExpectedKind::SequenceOrSet)),
|
|
}
|
|
}
|
|
|
|
fn open_sequence(&mut self) -> ReaderResult<()> {
|
|
self.next_compound(Tag::Sequence, ExpectedKind::Sequence)
|
|
}
|
|
|
|
fn open_set(&mut self) -> ReaderResult<()> {
|
|
self.next_compound(Tag::Set, ExpectedKind::Set)
|
|
}
|
|
|
|
fn open_dictionary(&mut self) -> ReaderResult<()> {
|
|
self.next_compound(Tag::Dictionary, ExpectedKind::Dictionary)
|
|
}
|
|
|
|
fn close_compound(&mut self) -> ReaderResult<bool> {
|
|
Ok(self.peekend()?)
|
|
}
|
|
|
|
fn open_pointer(&mut self) -> ReaderResult<()> {
|
|
self.next_compound(Tag::Pointer, ExpectedKind::Pointer)
|
|
}
|
|
|
|
fn close_pointer(&mut self) -> ReaderResult<()> {
|
|
Ok(())
|
|
}
|
|
|
|
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(self.expected(ExpectedKind::Boolean)),
|
|
}
|
|
}
|
|
|
|
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(self.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(self.expected(ExpectedKind::Double)),
|
|
}
|
|
}
|
|
|
|
fn next_str(&mut self) -> ReaderResult<Cow<'de, str>> {
|
|
Ok(decodestr(self.next_atomic(Tag::String, ExpectedKind::Symbol)?)?)
|
|
}
|
|
|
|
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>> {
|
|
Ok(decodestr(self.next_atomic(Tag::Symbol, ExpectedKind::Symbol)?)?)
|
|
}
|
|
}
|
|
|
|
struct DelimitedStream<'a, 'de, S: BinarySource<'de>> {
|
|
reader: ConfiguredReader<'de, &'a mut PackedReader<'de, S>>,
|
|
}
|
|
|
|
impl<'a, 'de, S: BinarySource<'de>> Iterator for DelimitedStream<'a, 'de, S>
|
|
{
|
|
type Item = IOResult<IOValue>;
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
match self.reader.reader.peekend() {
|
|
Err(e) => Some(Err(e)),
|
|
Ok(true) => None,
|
|
Ok(false) => Some(self.reader.reader.demand_next(self.reader.read_annotations)),
|
|
}
|
|
}
|
|
}
|
|
|
|
fn decodestr<'de>(cow: Cow<'de, [u8]>) -> IOResult<Cow<'de, str>> {
|
|
match cow {
|
|
Cow::Borrowed(bs) =>
|
|
Ok(Cow::Borrowed(std::str::from_utf8(bs).map_err(|_| io_syntax_error("Invalid UTF-8"))?)),
|
|
Cow::Owned(bs) =>
|
|
Ok(Cow::Owned(String::from_utf8(bs).map_err(|_| io_syntax_error("Invalid UTF-8"))?.to_owned())),
|
|
}
|
|
}
|