2019-02-07 11:42:43 +09:00
|
|
|
use crate::leb128::{self, read_signed_leb128, write_signed_leb128};
|
|
|
|
use crate::serialize;
|
2019-02-09 17:13:39 +09:00
|
|
|
use std::borrow::Cow;
|
2015-12-25 13:59:02 -05:00
|
|
|
|
2016-01-19 14:39:23 +13:00
|
|
|
// -----------------------------------------------------------------------------
|
2015-12-25 13:59:02 -05:00
|
|
|
// Encoder
|
2016-01-19 14:39:23 +13:00
|
|
|
// -----------------------------------------------------------------------------
|
2015-12-25 13:59:02 -05:00
|
|
|
|
2018-06-04 22:14:02 +02:00
|
|
|
pub type EncodeResult = Result<(), !>;
|
2016-08-28 07:10:22 +03:00
|
|
|
|
2018-06-04 22:14:02 +02:00
|
|
|
pub struct Encoder {
|
|
|
|
pub data: Vec<u8>,
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
|
|
|
|
2018-06-04 22:14:02 +02:00
|
|
|
impl Encoder {
|
|
|
|
pub fn new(data: Vec<u8>) -> Encoder {
|
|
|
|
Encoder { data }
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
2017-12-19 22:31:15 -05:00
|
|
|
|
2018-06-04 22:14:02 +02:00
|
|
|
pub fn into_inner(self) -> Vec<u8> {
|
|
|
|
self.data
|
2017-12-19 22:31:15 -05:00
|
|
|
}
|
2015-12-25 13:59:02 -05:00
|
|
|
|
2018-08-15 02:54:21 -04:00
|
|
|
#[inline]
|
2018-06-04 22:14:02 +02:00
|
|
|
pub fn emit_raw_bytes(&mut self, s: &[u8]) {
|
|
|
|
self.data.extend_from_slice(s);
|
|
|
|
}
|
|
|
|
}
|
2015-12-25 13:59:02 -05:00
|
|
|
|
|
|
|
macro_rules! write_uleb128 {
|
2018-01-09 16:53:35 +01:00
|
|
|
($enc:expr, $value:expr, $fun:ident) => {{
|
2018-06-04 22:14:02 +02:00
|
|
|
leb128::$fun(&mut $enc.data, $value);
|
2015-12-25 13:59:02 -05:00
|
|
|
Ok(())
|
2019-12-22 17:42:04 -05:00
|
|
|
}};
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! write_sleb128 {
|
|
|
|
($enc:expr, $value:expr) => {{
|
2018-06-04 22:14:02 +02:00
|
|
|
write_signed_leb128(&mut $enc.data, $value as i128);
|
2015-12-25 13:59:02 -05:00
|
|
|
Ok(())
|
2019-12-22 17:42:04 -05:00
|
|
|
}};
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
|
|
|
|
2018-06-04 22:14:02 +02:00
|
|
|
impl serialize::Encoder for Encoder {
|
|
|
|
type Error = !;
|
2015-12-25 13:59:02 -05:00
|
|
|
|
2018-01-09 16:53:35 +01:00
|
|
|
#[inline]
|
2018-09-11 23:32:41 +09:00
|
|
|
fn emit_unit(&mut self) -> EncodeResult {
|
2015-12-25 13:59:02 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-01-09 16:53:35 +01:00
|
|
|
#[inline]
|
2016-08-24 02:20:51 -04:00
|
|
|
fn emit_usize(&mut self, v: usize) -> EncodeResult {
|
2018-01-09 16:53:35 +01:00
|
|
|
write_uleb128!(self, v, write_usize_leb128)
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
|
|
|
|
2018-01-09 16:53:35 +01:00
|
|
|
#[inline]
|
2016-08-23 03:56:52 +03:00
|
|
|
fn emit_u128(&mut self, v: u128) -> EncodeResult {
|
2018-01-09 16:53:35 +01:00
|
|
|
write_uleb128!(self, v, write_u128_leb128)
|
2016-08-23 03:56:52 +03:00
|
|
|
}
|
|
|
|
|
2018-01-09 16:53:35 +01:00
|
|
|
#[inline]
|
2015-12-25 13:59:02 -05:00
|
|
|
fn emit_u64(&mut self, v: u64) -> EncodeResult {
|
2018-01-09 16:53:35 +01:00
|
|
|
write_uleb128!(self, v, write_u64_leb128)
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
|
|
|
|
2018-01-09 16:53:35 +01:00
|
|
|
#[inline]
|
2015-12-25 13:59:02 -05:00
|
|
|
fn emit_u32(&mut self, v: u32) -> EncodeResult {
|
2018-01-09 16:53:35 +01:00
|
|
|
write_uleb128!(self, v, write_u32_leb128)
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
|
|
|
|
2018-01-09 16:53:35 +01:00
|
|
|
#[inline]
|
2015-12-25 13:59:02 -05:00
|
|
|
fn emit_u16(&mut self, v: u16) -> EncodeResult {
|
2018-01-09 16:53:35 +01:00
|
|
|
write_uleb128!(self, v, write_u16_leb128)
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
|
|
|
|
2018-01-09 16:53:35 +01:00
|
|
|
#[inline]
|
2015-12-25 13:59:02 -05:00
|
|
|
fn emit_u8(&mut self, v: u8) -> EncodeResult {
|
2018-06-04 22:14:02 +02:00
|
|
|
self.data.push(v);
|
2015-12-25 13:59:02 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-01-09 16:53:35 +01:00
|
|
|
#[inline]
|
2016-08-24 02:20:51 -04:00
|
|
|
fn emit_isize(&mut self, v: isize) -> EncodeResult {
|
2015-12-25 13:59:02 -05:00
|
|
|
write_sleb128!(self, v)
|
|
|
|
}
|
|
|
|
|
2018-01-09 16:53:35 +01:00
|
|
|
#[inline]
|
2016-08-23 03:56:52 +03:00
|
|
|
fn emit_i128(&mut self, v: i128) -> EncodeResult {
|
|
|
|
write_sleb128!(self, v)
|
|
|
|
}
|
|
|
|
|
2018-01-09 16:53:35 +01:00
|
|
|
#[inline]
|
2015-12-25 13:59:02 -05:00
|
|
|
fn emit_i64(&mut self, v: i64) -> EncodeResult {
|
|
|
|
write_sleb128!(self, v)
|
|
|
|
}
|
|
|
|
|
2018-01-09 16:53:35 +01:00
|
|
|
#[inline]
|
2015-12-25 13:59:02 -05:00
|
|
|
fn emit_i32(&mut self, v: i32) -> EncodeResult {
|
|
|
|
write_sleb128!(self, v)
|
|
|
|
}
|
|
|
|
|
2018-01-09 16:53:35 +01:00
|
|
|
#[inline]
|
2015-12-25 13:59:02 -05:00
|
|
|
fn emit_i16(&mut self, v: i16) -> EncodeResult {
|
|
|
|
write_sleb128!(self, v)
|
|
|
|
}
|
|
|
|
|
2018-01-09 16:53:35 +01:00
|
|
|
#[inline]
|
2015-12-25 13:59:02 -05:00
|
|
|
fn emit_i8(&mut self, v: i8) -> EncodeResult {
|
2020-10-13 10:17:05 +02:00
|
|
|
let as_u8: u8 = unsafe { std::mem::transmute(v) };
|
2018-01-09 16:53:35 +01:00
|
|
|
self.emit_u8(as_u8)
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
|
|
|
|
2018-01-09 16:53:35 +01:00
|
|
|
#[inline]
|
2015-12-25 13:59:02 -05:00
|
|
|
fn emit_bool(&mut self, v: bool) -> EncodeResult {
|
2019-12-22 17:42:04 -05:00
|
|
|
self.emit_u8(if v { 1 } else { 0 })
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
|
|
|
|
2018-01-09 16:53:35 +01:00
|
|
|
#[inline]
|
2015-12-25 13:59:02 -05:00
|
|
|
fn emit_f64(&mut self, v: f64) -> EncodeResult {
|
2020-08-06 03:30:02 +06:00
|
|
|
let as_u64: u64 = v.to_bits();
|
2015-12-25 13:59:02 -05:00
|
|
|
self.emit_u64(as_u64)
|
|
|
|
}
|
|
|
|
|
2018-01-09 16:53:35 +01:00
|
|
|
#[inline]
|
2015-12-25 13:59:02 -05:00
|
|
|
fn emit_f32(&mut self, v: f32) -> EncodeResult {
|
2020-08-06 03:30:02 +06:00
|
|
|
let as_u32: u32 = v.to_bits();
|
2015-12-25 13:59:02 -05:00
|
|
|
self.emit_u32(as_u32)
|
|
|
|
}
|
|
|
|
|
2018-01-09 16:53:35 +01:00
|
|
|
#[inline]
|
2015-12-25 13:59:02 -05:00
|
|
|
fn emit_char(&mut self, v: char) -> EncodeResult {
|
|
|
|
self.emit_u32(v as u32)
|
|
|
|
}
|
|
|
|
|
2018-01-09 16:53:35 +01:00
|
|
|
#[inline]
|
2015-12-25 13:59:02 -05:00
|
|
|
fn emit_str(&mut self, v: &str) -> EncodeResult {
|
2016-08-24 02:20:51 -04:00
|
|
|
self.emit_usize(v.len())?;
|
2018-06-04 22:14:02 +02:00
|
|
|
self.emit_raw_bytes(v.as_bytes());
|
2015-12-25 13:59:02 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-04 22:14:02 +02:00
|
|
|
impl Encoder {
|
2018-01-09 16:53:35 +01:00
|
|
|
#[inline]
|
2015-12-25 13:59:02 -05:00
|
|
|
pub fn position(&self) -> usize {
|
2018-06-04 22:14:02 +02:00
|
|
|
self.data.len()
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-19 14:39:23 +13:00
|
|
|
// -----------------------------------------------------------------------------
|
2015-12-25 13:59:02 -05:00
|
|
|
// Decoder
|
2016-01-19 14:39:23 +13:00
|
|
|
// -----------------------------------------------------------------------------
|
2015-12-25 13:59:02 -05:00
|
|
|
|
|
|
|
pub struct Decoder<'a> {
|
|
|
|
pub data: &'a [u8],
|
|
|
|
position: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Decoder<'a> {
|
2018-12-05 18:59:48 +01:00
|
|
|
#[inline]
|
2015-12-25 13:59:02 -05:00
|
|
|
pub fn new(data: &'a [u8], position: usize) -> Decoder<'a> {
|
2019-12-22 17:42:04 -05:00
|
|
|
Decoder { data, position }
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
|
|
|
|
2018-01-09 16:53:35 +01:00
|
|
|
#[inline]
|
2015-12-25 13:59:02 -05:00
|
|
|
pub fn position(&self) -> usize {
|
|
|
|
self.position
|
|
|
|
}
|
|
|
|
|
2018-01-09 16:53:35 +01:00
|
|
|
#[inline]
|
2017-11-28 16:58:02 +01:00
|
|
|
pub fn set_position(&mut self, pos: usize) {
|
|
|
|
self.position = pos
|
|
|
|
}
|
|
|
|
|
2018-01-09 16:53:35 +01:00
|
|
|
#[inline]
|
2015-12-25 13:59:02 -05:00
|
|
|
pub fn advance(&mut self, bytes: usize) {
|
|
|
|
self.position += bytes;
|
|
|
|
}
|
2017-12-19 22:31:15 -05:00
|
|
|
|
2018-08-15 02:54:21 -04:00
|
|
|
#[inline]
|
2017-12-19 22:31:15 -05:00
|
|
|
pub fn read_raw_bytes(&mut self, s: &mut [u8]) -> Result<(), String> {
|
2017-12-22 22:41:09 -05:00
|
|
|
let start = self.position;
|
|
|
|
let end = start + s.len();
|
2017-12-19 22:31:15 -05:00
|
|
|
|
2017-12-22 22:41:09 -05:00
|
|
|
s.copy_from_slice(&self.data[start..end]);
|
2017-12-19 22:31:15 -05:00
|
|
|
|
2017-12-22 22:41:09 -05:00
|
|
|
self.position = end;
|
2017-12-19 22:31:15 -05:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! read_uleb128 {
|
2020-01-30 15:22:53 +11:00
|
|
|
($dec:expr, $fun:ident) => {{
|
2019-12-22 17:42:04 -05:00
|
|
|
let (value, bytes_read) = leb128::$fun(&$dec.data[$dec.position..]);
|
2015-12-25 13:59:02 -05:00
|
|
|
$dec.position += bytes_read;
|
2018-01-09 16:53:35 +01:00
|
|
|
Ok(value)
|
2019-12-22 17:42:04 -05:00
|
|
|
}};
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! read_sleb128 {
|
2019-12-22 17:42:04 -05:00
|
|
|
($dec:expr, $t:ty) => {{
|
2015-12-25 13:59:02 -05:00
|
|
|
let (value, bytes_read) = read_signed_leb128($dec.data, $dec.position);
|
|
|
|
$dec.position += bytes_read;
|
|
|
|
Ok(value as $t)
|
2019-12-22 17:42:04 -05:00
|
|
|
}};
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> serialize::Decoder for Decoder<'a> {
|
2016-09-01 16:55:33 +03:00
|
|
|
type Error = String;
|
2015-12-25 13:59:02 -05:00
|
|
|
|
2016-10-11 12:18:28 +11:00
|
|
|
#[inline]
|
2018-09-11 22:20:09 +09:00
|
|
|
fn read_nil(&mut self) -> Result<(), Self::Error> {
|
2015-12-25 13:59:02 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2016-08-23 03:56:52 +03:00
|
|
|
#[inline]
|
|
|
|
fn read_u128(&mut self) -> Result<u128, Self::Error> {
|
2020-01-30 15:22:53 +11:00
|
|
|
read_uleb128!(self, read_u128_leb128)
|
2016-08-23 03:56:52 +03:00
|
|
|
}
|
|
|
|
|
2016-10-11 12:18:28 +11:00
|
|
|
#[inline]
|
2015-12-25 13:59:02 -05:00
|
|
|
fn read_u64(&mut self) -> Result<u64, Self::Error> {
|
2020-01-30 15:22:53 +11:00
|
|
|
read_uleb128!(self, read_u64_leb128)
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
|
|
|
|
2016-10-11 12:18:28 +11:00
|
|
|
#[inline]
|
2015-12-25 13:59:02 -05:00
|
|
|
fn read_u32(&mut self) -> Result<u32, Self::Error> {
|
2020-01-30 15:22:53 +11:00
|
|
|
read_uleb128!(self, read_u32_leb128)
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
|
|
|
|
2016-10-11 12:18:28 +11:00
|
|
|
#[inline]
|
2015-12-25 13:59:02 -05:00
|
|
|
fn read_u16(&mut self) -> Result<u16, Self::Error> {
|
2020-01-30 15:22:53 +11:00
|
|
|
read_uleb128!(self, read_u16_leb128)
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
|
|
|
|
2016-10-11 12:18:28 +11:00
|
|
|
#[inline]
|
2015-12-25 13:59:02 -05:00
|
|
|
fn read_u8(&mut self) -> Result<u8, Self::Error> {
|
|
|
|
let value = self.data[self.position];
|
|
|
|
self.position += 1;
|
|
|
|
Ok(value)
|
|
|
|
}
|
|
|
|
|
2016-10-11 12:18:28 +11:00
|
|
|
#[inline]
|
2016-08-24 02:20:51 -04:00
|
|
|
fn read_usize(&mut self) -> Result<usize, Self::Error> {
|
2020-01-30 15:22:53 +11:00
|
|
|
read_uleb128!(self, read_usize_leb128)
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
|
|
|
|
2016-08-23 03:56:52 +03:00
|
|
|
#[inline]
|
|
|
|
fn read_i128(&mut self) -> Result<i128, Self::Error> {
|
|
|
|
read_sleb128!(self, i128)
|
|
|
|
}
|
|
|
|
|
2016-10-11 12:18:28 +11:00
|
|
|
#[inline]
|
2015-12-25 13:59:02 -05:00
|
|
|
fn read_i64(&mut self) -> Result<i64, Self::Error> {
|
|
|
|
read_sleb128!(self, i64)
|
|
|
|
}
|
|
|
|
|
2016-10-11 12:18:28 +11:00
|
|
|
#[inline]
|
2015-12-25 13:59:02 -05:00
|
|
|
fn read_i32(&mut self) -> Result<i32, Self::Error> {
|
|
|
|
read_sleb128!(self, i32)
|
|
|
|
}
|
|
|
|
|
2016-10-11 12:18:28 +11:00
|
|
|
#[inline]
|
2015-12-25 13:59:02 -05:00
|
|
|
fn read_i16(&mut self) -> Result<i16, Self::Error> {
|
|
|
|
read_sleb128!(self, i16)
|
|
|
|
}
|
|
|
|
|
2016-10-11 12:18:28 +11:00
|
|
|
#[inline]
|
2015-12-25 13:59:02 -05:00
|
|
|
fn read_i8(&mut self) -> Result<i8, Self::Error> {
|
|
|
|
let as_u8 = self.data[self.position];
|
|
|
|
self.position += 1;
|
2016-01-19 14:30:48 +13:00
|
|
|
unsafe { Ok(::std::mem::transmute(as_u8)) }
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
|
|
|
|
2016-10-11 12:18:28 +11:00
|
|
|
#[inline]
|
2016-08-24 02:20:51 -04:00
|
|
|
fn read_isize(&mut self) -> Result<isize, Self::Error> {
|
2015-12-25 13:59:02 -05:00
|
|
|
read_sleb128!(self, isize)
|
|
|
|
}
|
|
|
|
|
2016-10-11 12:18:28 +11:00
|
|
|
#[inline]
|
2015-12-25 13:59:02 -05:00
|
|
|
fn read_bool(&mut self) -> Result<bool, Self::Error> {
|
2016-03-22 22:01:37 -05:00
|
|
|
let value = self.read_u8()?;
|
2015-12-25 13:59:02 -05:00
|
|
|
Ok(value != 0)
|
|
|
|
}
|
|
|
|
|
2016-10-11 12:18:28 +11:00
|
|
|
#[inline]
|
2015-12-25 13:59:02 -05:00
|
|
|
fn read_f64(&mut self) -> Result<f64, Self::Error> {
|
2016-03-22 22:01:37 -05:00
|
|
|
let bits = self.read_u64()?;
|
2019-06-25 23:08:10 +02:00
|
|
|
Ok(f64::from_bits(bits))
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
|
|
|
|
2016-10-11 12:18:28 +11:00
|
|
|
#[inline]
|
2015-12-25 13:59:02 -05:00
|
|
|
fn read_f32(&mut self) -> Result<f32, Self::Error> {
|
2016-03-22 22:01:37 -05:00
|
|
|
let bits = self.read_u32()?;
|
2019-06-25 23:08:10 +02:00
|
|
|
Ok(f32::from_bits(bits))
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
|
|
|
|
2016-10-11 12:18:28 +11:00
|
|
|
#[inline]
|
2015-12-25 13:59:02 -05:00
|
|
|
fn read_char(&mut self) -> Result<char, Self::Error> {
|
2016-03-22 22:01:37 -05:00
|
|
|
let bits = self.read_u32()?;
|
2020-10-13 10:17:05 +02:00
|
|
|
Ok(std::char::from_u32(bits).unwrap())
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
|
|
|
|
2016-10-11 12:18:28 +11:00
|
|
|
#[inline]
|
2019-02-07 11:42:43 +09:00
|
|
|
fn read_str(&mut self) -> Result<Cow<'_, str>, Self::Error> {
|
2016-08-24 02:20:51 -04:00
|
|
|
let len = self.read_usize()?;
|
2020-10-13 10:17:05 +02:00
|
|
|
let s = std::str::from_utf8(&self.data[self.position..self.position + len]).unwrap();
|
2015-12-25 13:59:02 -05:00
|
|
|
self.position += len;
|
2016-10-10 09:07:18 +11:00
|
|
|
Ok(Cow::Borrowed(s))
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
|
|
|
|
2018-08-15 02:54:21 -04:00
|
|
|
#[inline]
|
2015-12-25 13:59:02 -05:00
|
|
|
fn error(&mut self, err: &str) -> Self::Error {
|
2016-09-01 16:55:33 +03:00
|
|
|
err.to_string()
|
2015-12-25 13:59:02 -05:00
|
|
|
}
|
|
|
|
}
|