//! Implementation of [Reader] for the binary encoding. use crate::error::{self, io_syntax_error, is_eof_io_error, ExpectedKind, Received}; 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::super::{ boundary as B, reader::{BinarySource, Reader, ReaderResult, Token}, repr::Annotations, signed_integer::SignedInteger, CompoundClass, DomainDecode, Map, NestedValue, Record, Set, Value, }; use super::constants::Tag; /// The binary encoding Preserves reader. pub struct PackedReader< 'de, 'src, N: NestedValue, Dec: DomainDecode, S: BinarySource<'de>, > { /// Underlying source of bytes. pub source: &'src mut S, /// Decoder for producing Rust values embedded in the binary data. pub decode_embedded: Dec, phantom: PhantomData<&'de N>, } impl<'de, 'src, N: NestedValue, Dec: DomainDecode, S: BinarySource<'de>> BinarySource<'de> for PackedReader<'de, 'src, N, Dec, S> { type Mark = S::Mark; #[inline(always)] fn mark(&mut self) -> io::Result { self.source.mark() } #[inline(always)] fn restore(&mut self, mark: &Self::Mark) -> 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 { self.source.peek() } #[inline(always)] fn readbytes(&mut self, count: usize) -> io::Result> { self.source.readbytes(count) } #[inline(always)] fn readbytes_into(&mut self, bs: &mut [u8]) -> io::Result<()> { self.source.readbytes_into(bs) } } fn out_of_range>(i: I) -> error::Error { error::Error::NumberOutOfRange(i.into()) } impl<'de, 'src, N: NestedValue, Dec: DomainDecode, S: BinarySource<'de>> PackedReader<'de, 'src, N, Dec, S> { /// Construct a new reader from a byte source and embedded-value decoder. #[inline(always)] pub fn new(source: &'src mut S, decode_embedded: Dec) -> Self { PackedReader { source, decode_embedded, phantom: PhantomData, } } #[inline(always)] fn read(&mut self) -> io::Result { 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(format!("{:?}", v))), Err(e) => e.into(), } } #[inline(always)] fn varint(&mut self) -> io::Result { let mut shift = 0; let mut acc: usize = 0; loop { let v = self.read()?; acc |= ((v & 0x7f) as usize) << shift; shift += 7; if v & 0x80 == 0 { return Ok(acc); } } } #[inline(always)] fn peekend(&mut self) -> io::Result { if self.peek()? == Tag::End.into() { self.skip()?; Ok(true) } else { Ok(false) } } #[inline(always)] fn try_next_nonannotation(&mut self, f: F) -> ReaderResult where F: FnOnce(&mut Self, Tag) -> ReaderResult, { let m = self.source.mark()?; loop { match Tag::try_from(self.peek()?)? { Tag::Annotation => { self.skip()?; self.skip_value()?; } tag => match f(self, tag) { Ok(v) => return Ok(v), Err(e) => { self.source.restore(&m)?; return Err(e); } } } } } fn next_atomic(&mut self, expected_tag: Tag, k: ExpectedKind) -> ReaderResult> { self.try_next_nonannotation(|r, actual_tag| { if actual_tag == expected_tag { r.skip()?; let count = r.varint()?; Ok(r.readbytes(count)?) } else { Err(r.expected(k)) } }) } fn next_compound(&mut self, expected_tag: Tag, k: ExpectedKind) -> ReaderResult<()> { self.try_next_nonannotation(|r, actual_tag| { if actual_tag == expected_tag { r.skip()?; Ok(()) } else { Err(r.expected(k)) } }) } #[inline(always)] fn read_signed_integer(&mut self, count: usize) -> io::Result { if count == 0 { return Ok(SignedInteger::from(0_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 += 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(&mut self, f: F) -> ReaderResult where F: FnOnce(u128) -> Option, { self.try_next_nonannotation(|r, tag| { match tag { Tag::SignedInteger => { r.skip()?; let count = r.varint()?; let n = &r.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(r.expected(ExpectedKind::SignedInteger)), } }) } #[inline(always)] fn next_signed(&mut self, f: F) -> ReaderResult where F: FnOnce(i128) -> Option, { self.try_next_nonannotation(|r, tag| { match tag { Tag::SignedInteger => { r.skip()?; let count = r.varint()?; let n = &r.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(r.expected(ExpectedKind::SignedInteger)), } }) } fn gather_annotations(&mut self) -> io::Result> { let mut annotations = vec![self.demand_next(true)?]; while Tag::try_from(self.peek()?)? == Tag::Annotation { self.skip()?; annotations.push(self.demand_next(true)?); } Ok(annotations) } fn skip_annotations(&mut self) -> io::Result<()> { self.skip_value()?; while Tag::try_from(self.peek()?)? == Tag::Annotation { self.skip()?; self.skip_value()?; } Ok(()) } fn next_upto_end(&mut self, read_annotations: bool) -> io::Result> { match self.peekend()? { true => Ok(None), false => Ok(Some(self.demand_next(read_annotations)?)), } } } impl<'de, 'src, N: NestedValue, Dec: DomainDecode, S: BinarySource<'de>> Reader<'de, N> for PackedReader<'de, 'src, N, Dec, S> { fn next(&mut self, read_annotations: bool) -> io::Result> { 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 => N::new(false), Tag::True => N::new(true), Tag::Annotation => { if read_annotations { let mut annotations = self.gather_annotations()?; let (existing_annotations, v) = self.demand_next(read_annotations)?.pieces(); annotations.extend_from_slice(existing_annotations.slice()); N::wrap(Annotations::new(Some(annotations)), v) } else { self.skip_annotations()?; self.demand_next(read_annotations)? } } Tag::Embedded => Value::Embedded( self.decode_embedded .decode_embedded(self.source, read_annotations)?, ).wrap(), Tag::Ieee754 => match self.varint()? { 8 => { let mut bs = [0; 8]; self.readbytes_into(&mut bs)?; Value::from(f64::from_bits(u64::from_be_bytes(bs))).wrap() } _ => return Err(io_syntax_error("Invalid IEEE754 size")) } 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 mut vs = Vec::new(); while let Some(v) = self.next_upto_end(read_annotations)? { vs.push(v); } if vs.is_empty() { return Err(io_syntax_error("Too few elements in encoded record")); } Value::Record(Record(vs)).wrap() } Tag::Sequence => { let mut vs = Vec::new(); while let Some(v) = self.next_upto_end(read_annotations)? { vs.push(v); } Value::Sequence(vs).wrap() } Tag::Set => { let mut s = Set::new(); while let Some(v) = self.next_upto_end(read_annotations)? { s.insert(v); } Value::Set(s).wrap() } Tag::Dictionary => { let mut d = Map::new(); while let Some(k) = self.next_upto_end(read_annotations)? { match self.next_upto_end(read_annotations)? { Some(v) => { 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))); } })) } #[inline(always)] fn open_record(&mut self, arity: Option) -> ReaderResult { self.next_compound(Tag::Record, ExpectedKind::Record(arity))?; let mut b = B::Type::default(); self.ensure_more_expected(&mut b, &B::Item::RecordLabel)?; Ok(b) } #[inline(always)] fn open_sequence_or_set(&mut self) -> ReaderResult { self.try_next_nonannotation(|r, tag| { match tag { Tag::Sequence => { r.skip()?; Ok(B::Item::SequenceValue) } Tag::Set => { r.skip()?; Ok(B::Item::SetValue) } _ => Err(r.expected(ExpectedKind::SequenceOrSet)), } }) } #[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 boundary(&mut self, _b: &B::Type) -> ReaderResult<()> { Ok(()) } #[inline(always)] fn close_compound(&mut self, _b: &mut B::Type, _i: &B::Item) -> ReaderResult { 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(()) } type Mark = S::Mark; #[inline(always)] fn mark(&mut self) -> io::Result { self.source.mark() } #[inline(always)] fn restore(&mut self, mark: &Self::Mark) -> io::Result<()> { self.source.restore(mark) } fn next_token(&mut self, read_embedded_annotations: bool) -> io::Result> { loop { return Ok(match Tag::try_from(self.peek()?)? { Tag::Embedded => { self.skip()?; Token::Embedded( self.decode_embedded .decode_embedded(self.source, read_embedded_annotations)?, ) } Tag::False | Tag::True | Tag::Ieee754 | Tag::SignedInteger | Tag::String | Tag::ByteString | Tag::Symbol => Token::Atom(self.demand_next(false)?), Tag::Record => { self.skip()?; Token::Compound(CompoundClass::Record) } Tag::Sequence => { self.skip()?; Token::Compound(CompoundClass::Sequence) } Tag::Set => { self.skip()?; Token::Compound(CompoundClass::Set) } Tag::Dictionary => { self.skip()?; Token::Compound(CompoundClass::Dictionary) } Tag::End => { self.skip()?; Token::End } Tag::Annotation => { self.skip()?; self.skip_value()?; continue; } }); } } fn next_annotations_and_token(&mut self) -> io::Result<(Vec, Token)> { match Tag::try_from(self.peek()?)? { Tag::Annotation => { self.skip()?; let annotations = self.gather_annotations()?; Ok((annotations, self.next_token(true)?)) } _ => Ok((Vec::new(), self.next_token(true)?)), } } #[inline(always)] fn next_boolean(&mut self) -> ReaderResult { self.try_next_nonannotation(|r, tag| { match tag { Tag::False => { r.skip()?; Ok(false) }, Tag::True => { r.skip()?; Ok(true) }, _ => Err(r.expected(ExpectedKind::Boolean)), } }) } fn next_signedinteger(&mut self) -> ReaderResult { self.try_next_nonannotation(|r, tag| { match tag { Tag::SignedInteger => { r.skip()?; let count = r.varint()?; Ok(r.read_signed_integer(count)?) } _ => Err(r.expected(ExpectedKind::SignedInteger)), } }) } fn next_i8(&mut self) -> ReaderResult { self.next_signed(|n| n.to_i8()) } fn next_i16(&mut self) -> ReaderResult { self.next_signed(|n| n.to_i16()) } fn next_i32(&mut self) -> ReaderResult { self.next_signed(|n| n.to_i32()) } fn next_i64(&mut self) -> ReaderResult { self.next_signed(|n| n.to_i64()) } fn next_i128(&mut self) -> ReaderResult { self.next_signed(|n| n.to_i128()) } fn next_u8(&mut self) -> ReaderResult { self.next_unsigned(|n| n.to_u8()) } fn next_u16(&mut self) -> ReaderResult { self.next_unsigned(|n| n.to_u16()) } fn next_u32(&mut self) -> ReaderResult { self.next_unsigned(|n| n.to_u32()) } fn next_u64(&mut self) -> ReaderResult { self.next_unsigned(|n| n.to_u64()) } fn next_u128(&mut self) -> ReaderResult { self.next_unsigned(|n| n.to_u128()) } fn next_f64(&mut self) -> ReaderResult { self.try_next_nonannotation(|r, tag| { if tag == Tag::Ieee754 { r.skip()?; match r.varint()? { 8 => { let mut bs = [0; 8]; r.readbytes_into(&mut bs)?; Ok(f64::from_bits(u64::from_be_bytes(bs))) } _ => Err(io_syntax_error("Invalid IEEE754 size"))?, } } else { Err(r.expected(ExpectedKind::Double)) } }) } fn next_str(&mut self) -> ReaderResult> { Ok(decodestr( self.next_atomic(Tag::String, ExpectedKind::Symbol)?, )?) } fn next_bytestring(&mut self) -> ReaderResult> { self.next_atomic(Tag::ByteString, ExpectedKind::Symbol) } fn next_symbol(&mut self) -> ReaderResult> { Ok(decodestr( self.next_atomic(Tag::Symbol, ExpectedKind::Symbol)?, )?) } } #[inline(always)] fn decodestr(cow: Cow<'_, [u8]>) -> io::Result> { 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"))?, )), } }