2016-04-20 13:17:11 +02:00
|
|
|
// Copyright 2015, 2016 Ethcore (UK) Ltd.
|
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
//! Binary representation of types
|
|
|
|
|
2016-04-21 21:18:13 +02:00
|
|
|
use util::bytes::Populatable;
|
2016-05-16 18:33:32 +02:00
|
|
|
use util::numbers::{U256, U512, H256, H2048, Address};
|
2016-04-20 18:01:53 +02:00
|
|
|
use std::mem;
|
2016-04-21 21:18:13 +02:00
|
|
|
use std::collections::VecDeque;
|
2016-05-16 18:33:32 +02:00
|
|
|
use std::ops::Range;
|
2016-04-20 18:01:53 +02:00
|
|
|
|
2016-04-21 21:18:13 +02:00
|
|
|
#[derive(Debug)]
|
2016-04-20 17:02:17 +02:00
|
|
|
pub struct BinaryConvertError;
|
2016-04-20 13:17:11 +02:00
|
|
|
|
2016-04-20 17:02:17 +02:00
|
|
|
pub trait BinaryConvertable : Sized {
|
2016-04-21 21:18:13 +02:00
|
|
|
fn size(&self) -> usize {
|
|
|
|
mem::size_of::<Self>()
|
|
|
|
}
|
2016-04-20 13:17:11 +02:00
|
|
|
|
2016-04-21 21:18:13 +02:00
|
|
|
fn to_bytes(&self, buffer: &mut [u8], length_stack: &mut VecDeque<usize>) -> Result<(), BinaryConvertError>;
|
2016-04-20 17:02:17 +02:00
|
|
|
|
2016-04-21 21:18:13 +02:00
|
|
|
fn from_bytes(buffer: &[u8], length_stack: &mut VecDeque<usize>) -> Result<Self, BinaryConvertError>;
|
2016-04-20 19:22:24 +02:00
|
|
|
|
|
|
|
fn from_empty_bytes() -> Result<Self, BinaryConvertError> {
|
|
|
|
Err(BinaryConvertError)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn len_params() -> usize {
|
|
|
|
0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> BinaryConvertable for Option<T> where T: BinaryConvertable {
|
|
|
|
fn size(&self) -> usize {
|
|
|
|
match * self { None => 0, Some(ref val) => val.size() }
|
|
|
|
}
|
|
|
|
|
2016-04-21 21:18:13 +02:00
|
|
|
fn to_bytes(&self, buffer: &mut [u8], length_stack: &mut VecDeque<usize>) -> Result<(), BinaryConvertError> {
|
|
|
|
match *self { None => Err(BinaryConvertError), Some(ref val) => val.to_bytes(buffer, length_stack) }
|
2016-04-20 19:22:24 +02:00
|
|
|
}
|
|
|
|
|
2016-04-21 21:18:13 +02:00
|
|
|
fn from_bytes(buffer: &[u8], length_stack: &mut VecDeque<usize>) -> Result<Self, BinaryConvertError> {
|
|
|
|
Ok(Some(try!(T::from_bytes(buffer, length_stack))))
|
2016-04-20 19:22:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn from_empty_bytes() -> Result<Self, BinaryConvertError> {
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn len_params() -> usize {
|
|
|
|
1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-22 18:45:09 +02:00
|
|
|
impl<E: BinaryConvertable> BinaryConvertable for Result<(), E> {
|
|
|
|
fn size(&self) -> usize {
|
2016-05-19 14:36:15 +02:00
|
|
|
match *self {
|
2016-04-23 17:15:50 +02:00
|
|
|
Ok(_) => 0,
|
2016-04-22 18:45:09 +02:00
|
|
|
Err(ref e) => e.size(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_bytes(&self, buffer: &mut [u8], length_stack: &mut VecDeque<usize>) -> Result<(), BinaryConvertError> {
|
|
|
|
match *self {
|
2016-05-19 14:36:15 +02:00
|
|
|
Ok(_) => Err(BinaryConvertError),
|
2016-04-22 18:45:09 +02:00
|
|
|
Err(ref e) => Ok(try!(e.to_bytes(buffer, length_stack))),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_bytes(buffer: &[u8], length_stack: &mut VecDeque<usize>) -> Result<Self, BinaryConvertError> {
|
2016-05-19 14:36:15 +02:00
|
|
|
Ok(Err(try!(E::from_bytes(&buffer, length_stack))))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_empty_bytes() -> Result<Self, BinaryConvertError> {
|
|
|
|
Ok(Ok(()))
|
2016-04-22 18:45:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn len_params() -> usize {
|
|
|
|
1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl<R: BinaryConvertable, E: BinaryConvertable> BinaryConvertable for Result<R, E> {
|
|
|
|
fn size(&self) -> usize {
|
|
|
|
1usize + match *self {
|
|
|
|
Ok(ref r) => r.size(),
|
|
|
|
Err(ref e) => e.size(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_bytes(&self, buffer: &mut [u8], length_stack: &mut VecDeque<usize>) -> Result<(), BinaryConvertError> {
|
|
|
|
match *self {
|
2016-06-02 19:04:42 +02:00
|
|
|
Ok(ref r) => {
|
|
|
|
buffer[0] = 0;
|
|
|
|
if r.size() > 0 {
|
|
|
|
Ok(try!(r.to_bytes(&mut buffer[1..], length_stack)))
|
|
|
|
}
|
|
|
|
else { Ok(()) }
|
|
|
|
},
|
|
|
|
Err(ref e) => {
|
|
|
|
buffer[0] = 1;
|
|
|
|
if e.size() > 0 {
|
|
|
|
Ok(try!(e.to_bytes(&mut buffer[1..], length_stack)))
|
|
|
|
}
|
|
|
|
else { Ok(()) }
|
|
|
|
},
|
2016-04-22 18:45:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_bytes(buffer: &[u8], length_stack: &mut VecDeque<usize>) -> Result<Self, BinaryConvertError> {
|
|
|
|
match buffer[0] {
|
2016-06-02 19:04:42 +02:00
|
|
|
0 => {
|
|
|
|
match buffer.len() {
|
|
|
|
1 => Ok(Ok(try!(R::from_empty_bytes()))),
|
|
|
|
_ => Ok(Ok(try!(R::from_bytes(&buffer[1..], length_stack)))),
|
|
|
|
}
|
|
|
|
}
|
2016-04-22 18:45:09 +02:00
|
|
|
1 => Ok(Err(try!(E::from_bytes(&buffer[1..], length_stack)))),
|
|
|
|
_ => Err(BinaryConvertError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn len_params() -> usize {
|
|
|
|
1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-20 19:22:24 +02:00
|
|
|
impl<T> BinaryConvertable for Vec<T> where T: BinaryConvertable {
|
|
|
|
fn size(&self) -> usize {
|
|
|
|
match T::len_params() {
|
|
|
|
0 => mem::size_of::<T>() * self.len(),
|
|
|
|
_ => self.iter().fold(0usize, |acc, t| acc + t.size()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-21 21:18:13 +02:00
|
|
|
fn to_bytes(&self, buffer: &mut [u8], length_stack: &mut VecDeque<usize>) -> Result<(), BinaryConvertError> {
|
2016-04-20 19:22:24 +02:00
|
|
|
let mut offset = 0usize;
|
2016-04-21 21:18:13 +02:00
|
|
|
for item in self.iter() {
|
|
|
|
let next_size = match T::len_params() {
|
|
|
|
0 => mem::size_of::<T>(),
|
|
|
|
_ => { let size = item.size(); length_stack.push_back(size); size },
|
|
|
|
};
|
|
|
|
if next_size > 0 {
|
|
|
|
let item_end = offset + next_size;
|
|
|
|
try!(item.to_bytes(&mut buffer[offset..item_end], length_stack));
|
|
|
|
offset = item_end;
|
|
|
|
}
|
2016-04-20 19:22:24 +02:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2016-04-21 21:18:13 +02:00
|
|
|
fn from_bytes(buffer: &[u8], length_stack: &mut VecDeque<usize>) -> Result<Self, BinaryConvertError> {
|
|
|
|
let mut index = 0;
|
|
|
|
let mut result = Self::with_capacity(
|
|
|
|
match T::len_params() {
|
|
|
|
0 => buffer.len() / mem::size_of::<T>(),
|
|
|
|
_ => 128,
|
|
|
|
});
|
|
|
|
|
2016-06-02 19:04:42 +02:00
|
|
|
if buffer.len() == 0 { return Ok(result); }
|
|
|
|
|
2016-04-21 21:18:13 +02:00
|
|
|
loop {
|
|
|
|
let next_size = match T::len_params() {
|
|
|
|
0 => mem::size_of::<T>(),
|
|
|
|
_ => try!(length_stack.pop_front().ok_or(BinaryConvertError)),
|
|
|
|
};
|
|
|
|
let item = if next_size == 0 {
|
|
|
|
try!(T::from_empty_bytes())
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
try!(T::from_bytes(&buffer[index..index+next_size], length_stack))
|
|
|
|
};
|
|
|
|
result.push(item);
|
|
|
|
|
|
|
|
index = index + next_size;
|
|
|
|
if index == buffer.len() { break; }
|
|
|
|
if index > buffer.len() {
|
|
|
|
return Err(BinaryConvertError)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(result)
|
2016-04-20 19:22:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn from_empty_bytes() -> Result<Self, BinaryConvertError> {
|
|
|
|
Ok(Self::new())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn len_params() -> usize {
|
|
|
|
1
|
|
|
|
}
|
2016-04-20 13:17:11 +02:00
|
|
|
}
|
2016-04-20 18:01:53 +02:00
|
|
|
|
2016-04-22 18:45:09 +02:00
|
|
|
impl BinaryConvertable for String {
|
|
|
|
fn size(&self) -> usize {
|
|
|
|
self.as_bytes().len()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_empty_bytes() -> Result<Self, BinaryConvertError> {
|
|
|
|
Ok(String::new())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_bytes(&self, buffer: &mut [u8], _length_stack: &mut VecDeque<usize>) -> Result<(), BinaryConvertError> {
|
|
|
|
buffer[..].clone_from_slice(self.as_bytes());
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_bytes(buffer: &[u8], _length_stack: &mut VecDeque<usize>) -> Result<Self, BinaryConvertError> {
|
|
|
|
Ok(::std::str::from_utf8(buffer).unwrap().to_owned())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn len_params() -> usize {
|
|
|
|
1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-25 16:20:38 +02:00
|
|
|
impl<T> BinaryConvertable for ::std::cell::RefCell<T> where T: BinaryConvertable {
|
|
|
|
fn size(&self) -> usize {
|
|
|
|
self.borrow().size()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_empty_bytes() -> Result<Self, BinaryConvertError> {
|
|
|
|
Ok(::std::cell::RefCell::new(try!(T::from_empty_bytes())))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_bytes(buffer: &[u8], length_stack: &mut VecDeque<usize>) -> Result<Self, BinaryConvertError> {
|
|
|
|
Ok(::std::cell::RefCell::new(try!(T::from_bytes(buffer, length_stack))))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_bytes(&self, buffer: &mut [u8], length_stack: &mut VecDeque<usize>) -> Result<(), BinaryConvertError> {
|
|
|
|
try!(self.borrow().to_bytes(buffer, length_stack));
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn len_params() -> usize {
|
|
|
|
T::len_params()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-16 18:33:32 +02:00
|
|
|
impl<T> BinaryConvertable for ::std::cell::Cell<T> where T: BinaryConvertable + Copy {
|
|
|
|
fn size(&self) -> usize {
|
|
|
|
self.get().size()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_empty_bytes() -> Result<Self, BinaryConvertError> {
|
|
|
|
Ok(::std::cell::Cell::new(try!(T::from_empty_bytes())))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_bytes(buffer: &[u8], length_stack: &mut VecDeque<usize>) -> Result<Self, BinaryConvertError> {
|
|
|
|
Ok(::std::cell::Cell::new(try!(T::from_bytes(buffer, length_stack))))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_bytes(&self, buffer: &mut [u8], length_stack: &mut VecDeque<usize>) -> Result<(), BinaryConvertError> {
|
|
|
|
try!(self.get().to_bytes(buffer, length_stack));
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn len_params() -> usize {
|
|
|
|
T::len_params()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-22 18:45:09 +02:00
|
|
|
impl BinaryConvertable for Vec<u8> {
|
|
|
|
fn size(&self) -> usize {
|
|
|
|
self.len()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_empty_bytes() -> Result<Self, BinaryConvertError> {
|
|
|
|
Ok(Vec::new())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_bytes(&self, buffer: &mut [u8], _length_stack: &mut VecDeque<usize>) -> Result<(), BinaryConvertError> {
|
|
|
|
buffer[..].clone_from_slice(&self[..]);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_bytes(buffer: &[u8], _length_stack: &mut VecDeque<usize>) -> Result<Self, BinaryConvertError> {
|
|
|
|
let mut res = Self::with_capacity(buffer.len());
|
|
|
|
unsafe { res.set_len(buffer.len()) }
|
|
|
|
res[..].clone_from_slice(&buffer[..]);
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn len_params() -> usize {
|
|
|
|
1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-21 21:18:13 +02:00
|
|
|
pub fn deserialize_from<T, R>(r: &mut R) -> Result<T, BinaryConvertError>
|
|
|
|
where R: ::std::io::Read,
|
|
|
|
T: BinaryConvertable
|
|
|
|
{
|
|
|
|
let mut fake_stack = VecDeque::new();
|
|
|
|
|
2016-04-23 17:15:50 +02:00
|
|
|
match T::len_params() {
|
|
|
|
0 => {
|
|
|
|
let fixed_size = mem::size_of::<T>();
|
|
|
|
let mut payload_buffer = Vec::with_capacity(fixed_size);
|
|
|
|
unsafe { payload_buffer.set_len(fixed_size); }
|
|
|
|
try!(r.read(&mut payload_buffer).map_err(|_| BinaryConvertError));
|
|
|
|
T::from_bytes(&payload_buffer[..], &mut fake_stack)
|
|
|
|
},
|
|
|
|
_ => {
|
2016-05-19 14:36:15 +02:00
|
|
|
let mut payload = Vec::new();
|
|
|
|
try!(r.read_to_end(&mut payload).map_err(|_| BinaryConvertError));
|
|
|
|
|
|
|
|
let stack_len = try!(u64::from_bytes(&payload[0..8], &mut fake_stack)) as usize;
|
2016-06-02 19:04:42 +02:00
|
|
|
let mut length_stack = VecDeque::<usize>::with_capacity(stack_len);
|
2016-04-23 17:15:50 +02:00
|
|
|
|
2016-05-19 14:36:15 +02:00
|
|
|
if stack_len > 0 {
|
2016-04-23 17:15:50 +02:00
|
|
|
for idx in 0..stack_len {
|
2016-05-19 14:36:15 +02:00
|
|
|
let stack_item = try!(u64::from_bytes(&payload[8 + idx*8..8 + (idx+1)*8], &mut fake_stack));
|
2016-04-23 17:15:50 +02:00
|
|
|
length_stack.push_back(stack_item as usize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-19 14:36:15 +02:00
|
|
|
//try!(r.read(&mut size_buffer).map_err(|_| BinaryConvertError));
|
|
|
|
let size = try!(u64::from_bytes(&payload[8+stack_len*8..16+stack_len*8], &mut fake_stack)) as usize;
|
|
|
|
match size {
|
|
|
|
0 => {
|
|
|
|
T::from_empty_bytes()
|
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
T::from_bytes(&payload[16+stack_len*8..], &mut length_stack)
|
|
|
|
}
|
|
|
|
}
|
2016-04-25 01:43:21 +02:00
|
|
|
},
|
2016-04-23 17:15:50 +02:00
|
|
|
}
|
2016-04-21 21:18:13 +02:00
|
|
|
}
|
|
|
|
|
2016-04-22 18:45:09 +02:00
|
|
|
pub fn deserialize<T: BinaryConvertable>(buffer: &[u8]) -> Result<T, BinaryConvertError> {
|
|
|
|
use std::io::Cursor;
|
|
|
|
let mut buff = Cursor::new(buffer);
|
|
|
|
deserialize_from::<T, _>(&mut buff)
|
|
|
|
}
|
|
|
|
|
2016-04-21 21:18:13 +02:00
|
|
|
pub fn serialize_into<T, W>(t: &T, w: &mut W) -> Result<(), BinaryConvertError>
|
|
|
|
where W: ::std::io::Write,
|
|
|
|
T: BinaryConvertable
|
|
|
|
{
|
|
|
|
let mut fake_stack = VecDeque::new();
|
2016-04-23 17:15:50 +02:00
|
|
|
|
|
|
|
match T::len_params() {
|
|
|
|
0 => {
|
|
|
|
let fixed_size = mem::size_of::<T>();
|
|
|
|
let mut buffer = Vec::with_capacity(fixed_size);
|
|
|
|
unsafe { buffer.set_len(fixed_size); }
|
|
|
|
try!(t.to_bytes(&mut buffer[..], &mut fake_stack));
|
|
|
|
try!(w.write(&buffer[..]).map_err(|_| BinaryConvertError));
|
|
|
|
Ok(())
|
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
let mut length_stack = VecDeque::<usize>::new();
|
|
|
|
let mut size_buffer = [0u8; 8];
|
|
|
|
|
|
|
|
let size = t.size();
|
2016-05-19 14:36:15 +02:00
|
|
|
if size == 0 {
|
|
|
|
try!(w.write(&size_buffer).map_err(|_| BinaryConvertError));
|
|
|
|
try!(w.write(&size_buffer).map_err(|_| BinaryConvertError));
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2016-04-23 17:15:50 +02:00
|
|
|
let mut buffer = Vec::with_capacity(size);
|
|
|
|
unsafe { buffer.set_len(size); }
|
|
|
|
try!(t.to_bytes(&mut buffer[..], &mut length_stack));
|
|
|
|
|
|
|
|
let stack_len = length_stack.len();
|
|
|
|
try!((stack_len as u64).to_bytes(&mut size_buffer[..], &mut fake_stack));
|
|
|
|
try!(w.write(&size_buffer[..]).map_err(|_| BinaryConvertError));
|
|
|
|
if stack_len > 0 {
|
|
|
|
let mut header_buffer = Vec::with_capacity(stack_len * 8);
|
|
|
|
unsafe { header_buffer.set_len(stack_len * 8); };
|
|
|
|
try!((stack_len as u64).to_bytes(&mut header_buffer[0..8], &mut fake_stack));
|
|
|
|
let mut idx = 0;
|
|
|
|
loop {
|
|
|
|
match length_stack.pop_front() {
|
|
|
|
Some(val) => try!((val as u64).to_bytes(&mut header_buffer[idx * 8..(idx+1) * 8], &mut fake_stack)),
|
|
|
|
None => { break; }
|
|
|
|
}
|
|
|
|
idx = idx + 1;
|
|
|
|
}
|
|
|
|
try!(w.write(&header_buffer[..]).map_err(|_| BinaryConvertError));
|
2016-04-21 21:18:13 +02:00
|
|
|
}
|
|
|
|
|
2016-04-23 17:15:50 +02:00
|
|
|
try!((size as u64).to_bytes(&mut size_buffer[..], &mut fake_stack));
|
|
|
|
try!(w.write(&size_buffer[..]).map_err(|_| BinaryConvertError));
|
2016-04-21 21:18:13 +02:00
|
|
|
|
2016-04-23 17:15:50 +02:00
|
|
|
try!(w.write(&buffer[..]).map_err(|_| BinaryConvertError));
|
2016-04-21 21:18:13 +02:00
|
|
|
|
2016-04-23 17:15:50 +02:00
|
|
|
Ok(())
|
2016-04-25 01:43:21 +02:00
|
|
|
},
|
2016-04-23 17:15:50 +02:00
|
|
|
}
|
2016-04-21 21:18:13 +02:00
|
|
|
}
|
|
|
|
|
2016-04-22 18:45:09 +02:00
|
|
|
pub fn serialize<T: BinaryConvertable>(t: &T) -> Result<Vec<u8>, BinaryConvertError> {
|
|
|
|
use std::io::Cursor;
|
|
|
|
let mut buff = Cursor::new(Vec::new());
|
|
|
|
try!(serialize_into(t, &mut buff));
|
2016-05-19 14:36:15 +02:00
|
|
|
let into_inner = buff.into_inner();
|
|
|
|
Ok(into_inner)
|
2016-04-22 18:45:09 +02:00
|
|
|
}
|
|
|
|
|
2016-05-16 18:33:32 +02:00
|
|
|
#[macro_export]
|
2016-04-20 18:01:53 +02:00
|
|
|
macro_rules! binary_fixed_size {
|
2016-05-16 18:33:32 +02:00
|
|
|
($target_ty: ty) => {
|
2016-04-20 18:01:53 +02:00
|
|
|
impl BinaryConvertable for $target_ty {
|
2016-04-21 21:18:13 +02:00
|
|
|
fn from_bytes(bytes: &[u8], _length_stack: &mut VecDeque<usize>) -> Result<Self, BinaryConvertError> {
|
2016-04-20 18:01:53 +02:00
|
|
|
match bytes.len().cmp(&::std::mem::size_of::<$target_ty>()) {
|
|
|
|
::std::cmp::Ordering::Less => return Err(BinaryConvertError),
|
|
|
|
::std::cmp::Ordering::Greater => return Err(BinaryConvertError),
|
|
|
|
::std::cmp::Ordering::Equal => ()
|
|
|
|
};
|
|
|
|
let mut res: Self = unsafe { ::std::mem::uninitialized() };
|
|
|
|
res.copy_raw(bytes);
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
|
2016-04-21 21:18:13 +02:00
|
|
|
fn to_bytes(&self, buffer: &mut [u8], _length_stack: &mut VecDeque<usize>) -> Result<(), BinaryConvertError> {
|
2016-04-20 18:01:53 +02:00
|
|
|
let sz = ::std::mem::size_of::<$target_ty>();
|
|
|
|
let ip: *const $target_ty = self;
|
|
|
|
let ptr: *const u8 = ip as *const _;
|
|
|
|
unsafe {
|
|
|
|
::std::ptr::copy(ptr, buffer.as_mut_ptr(), sz);
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
binary_fixed_size!(u64);
|
|
|
|
binary_fixed_size!(u32);
|
2016-04-25 16:20:38 +02:00
|
|
|
binary_fixed_size!(usize);
|
2016-04-22 18:45:09 +02:00
|
|
|
binary_fixed_size!(i32);
|
2016-04-20 18:01:53 +02:00
|
|
|
binary_fixed_size!(bool);
|
2016-04-25 16:20:38 +02:00
|
|
|
binary_fixed_size!(U256);
|
2016-05-16 18:33:32 +02:00
|
|
|
binary_fixed_size!(U512);
|
2016-04-25 16:20:38 +02:00
|
|
|
binary_fixed_size!(H256);
|
|
|
|
binary_fixed_size!(H2048);
|
|
|
|
binary_fixed_size!(Address);
|
2016-05-16 18:33:32 +02:00
|
|
|
binary_fixed_size!(Range<usize>);
|
|
|
|
binary_fixed_size!(Range<u64>);
|
2016-04-21 21:18:13 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn vec_serialize() {
|
|
|
|
let mut v = Vec::new();
|
|
|
|
v.push(5u64);
|
|
|
|
v.push(10u64);
|
|
|
|
let mut length_stack = VecDeque::new();
|
|
|
|
let mut data = Vec::with_capacity(v.size());
|
|
|
|
unsafe { data.set_len(v.size()); }
|
|
|
|
let result = v.to_bytes(&mut data[..], &mut length_stack);
|
|
|
|
|
|
|
|
assert!(result.is_ok());
|
|
|
|
assert_eq!(5, data[0]);
|
|
|
|
assert_eq!(0, data[1]);
|
|
|
|
assert_eq!(10, data[8]);
|
|
|
|
assert_eq!(0, data[12]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn calculates_size() {
|
|
|
|
let mut v = Vec::new();
|
|
|
|
v.push(5u64);
|
|
|
|
v.push(10u64);
|
|
|
|
|
|
|
|
assert_eq!(16, v.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn vec_deserialize() {
|
|
|
|
let data = [
|
|
|
|
10u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8,
|
|
|
|
5u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8,
|
|
|
|
];
|
|
|
|
|
|
|
|
let mut length_stack = VecDeque::new();
|
|
|
|
let vec = Vec::<u64>::from_bytes(&data[..], &mut length_stack).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(vec![10u64, 5u64], vec);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn vec_deserialize_chained() {
|
|
|
|
let mut v = Vec::new();
|
|
|
|
v.push(Some(5u64));
|
|
|
|
v.push(Some(10u64));
|
|
|
|
v.push(None);
|
|
|
|
v.push(Some(12u64));
|
|
|
|
|
|
|
|
let mut length_stack = VecDeque::new();
|
|
|
|
let mut data = Vec::with_capacity(v.size());
|
|
|
|
unsafe { data.set_len(v.size()); }
|
|
|
|
let result = v.to_bytes(&mut data[..], &mut length_stack);
|
|
|
|
|
|
|
|
assert!(result.is_ok());
|
|
|
|
assert_eq!(4, length_stack.len());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn vec_serialize_deserialize() {
|
|
|
|
let mut v = Vec::new();
|
|
|
|
v.push(Some(5u64));
|
|
|
|
v.push(None);
|
|
|
|
v.push(Some(10u64));
|
|
|
|
v.push(None);
|
|
|
|
v.push(Some(12u64));
|
|
|
|
|
|
|
|
|
|
|
|
let mut data = Vec::with_capacity(v.size());
|
|
|
|
unsafe { data.set_len(v.size()); }
|
|
|
|
let mut length_stack = VecDeque::new();
|
|
|
|
|
|
|
|
v.to_bytes(&mut data[..], &mut length_stack).unwrap();
|
|
|
|
let de_v = Vec::<Option<u64>>::from_bytes(&data[..], &mut length_stack).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(v, de_v);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn serialize_into_ok() {
|
|
|
|
use std::io::Cursor;
|
|
|
|
let mut buff = Cursor::new(vec![0; 128]);
|
|
|
|
|
|
|
|
let mut v = Vec::new();
|
|
|
|
v.push(Some(5u64));
|
|
|
|
v.push(None);
|
|
|
|
v.push(Some(10u64));
|
|
|
|
v.push(None);
|
|
|
|
v.push(Some(12u64));
|
|
|
|
|
|
|
|
serialize_into(&v, &mut buff).unwrap();
|
|
|
|
assert_eq!(5, buff.get_ref()[0]);
|
|
|
|
assert_eq!(8, buff.get_ref()[8]);
|
|
|
|
assert_eq!(0, buff.get_ref()[16]);
|
|
|
|
assert_eq!(8, buff.get_ref()[24]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn deserialize_from_ok() {
|
|
|
|
use std::io::Cursor;
|
|
|
|
let mut buff = Cursor::new(vec![
|
|
|
|
0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8,
|
|
|
|
16u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8,
|
|
|
|
10u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8,
|
|
|
|
5u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8,
|
|
|
|
]);
|
|
|
|
|
|
|
|
let vec = deserialize_from::<Vec<u64>, _>(&mut buff).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(vec![10u64, 5u64], vec);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn serialize_into_deserialize_from() {
|
|
|
|
use std::io::{Cursor, SeekFrom, Seek};
|
|
|
|
|
2016-05-19 14:36:15 +02:00
|
|
|
let mut buff = Cursor::new(Vec::new());
|
2016-04-21 21:18:13 +02:00
|
|
|
let mut v = Vec::new();
|
|
|
|
v.push(Some(5u64));
|
|
|
|
v.push(None);
|
|
|
|
v.push(Some(10u64));
|
|
|
|
v.push(None);
|
|
|
|
v.push(Some(12u64));
|
|
|
|
|
|
|
|
serialize_into(&v, &mut buff).unwrap();
|
|
|
|
buff.seek(SeekFrom::Start(0)).unwrap();
|
|
|
|
let de_v = deserialize_from::<Vec<Option<u64>>, _>(&mut buff).unwrap();
|
|
|
|
assert_eq!(v, de_v);
|
|
|
|
}
|
2016-05-19 14:36:15 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn serialize_opt_vec() {
|
|
|
|
use std::io::Cursor;
|
|
|
|
|
|
|
|
let mut buff = Cursor::new(Vec::new());
|
|
|
|
let optional_vec: Option<Vec<u8>> = None;
|
|
|
|
serialize_into(&optional_vec, &mut buff).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(&vec![0u8; 16], buff.get_ref());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn serialize_opt_vec_payload() {
|
|
|
|
use std::io::Cursor;
|
|
|
|
|
|
|
|
let optional_vec: Option<Vec<u8>> = None;
|
|
|
|
let payload = serialize(&optional_vec).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(vec![0u8;16], payload);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn deserialize_opt_vec() {
|
|
|
|
use std::io::Cursor;
|
|
|
|
let mut buff = Cursor::new(vec![0u8; 16]);
|
|
|
|
|
|
|
|
let vec = deserialize_from::<Option<Vec<u8>>, _>(&mut buff).unwrap();
|
|
|
|
|
|
|
|
assert!(vec.is_none());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn deserialize_simple_err() {
|
|
|
|
use std::io::Cursor;
|
|
|
|
let mut buff = Cursor::new(vec![0u8; 16]);
|
|
|
|
|
|
|
|
let result = deserialize_from::<Result<(), u32>, _>(&mut buff).unwrap();
|
|
|
|
|
|
|
|
assert!(result.is_ok());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-06-02 19:04:42 +02:00
|
|
|
fn serialize_opt_vec_in_out() {
|
2016-05-19 14:36:15 +02:00
|
|
|
use std::io::{Cursor, SeekFrom, Seek};
|
|
|
|
|
|
|
|
let mut buff = Cursor::new(Vec::new());
|
|
|
|
let optional_vec: Option<Vec<u8>> = None;
|
|
|
|
serialize_into(&optional_vec, &mut buff).unwrap();
|
|
|
|
|
|
|
|
buff.seek(SeekFrom::Start(0)).unwrap();
|
|
|
|
let vec = deserialize_from::<Option<Vec<u8>>, _>(&mut buff).unwrap();
|
|
|
|
|
|
|
|
assert!(vec.is_none());
|
|
|
|
}
|
2016-06-02 19:04:42 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn serialize_err_opt_vec_in_out() {
|
|
|
|
use std::io::{Cursor, SeekFrom, Seek};
|
|
|
|
|
|
|
|
let mut buff = Cursor::new(Vec::new());
|
|
|
|
let optional_vec: Result<Option<Vec<u8>>, u32> = Ok(None);
|
|
|
|
serialize_into(&optional_vec, &mut buff).unwrap();
|
|
|
|
|
|
|
|
buff.seek(SeekFrom::Start(0)).unwrap();
|
|
|
|
let vec = deserialize_from::<Result<Option<Vec<u8>>, u32>, _>(&mut buff).unwrap();
|
|
|
|
|
|
|
|
assert!(vec.is_ok());
|
|
|
|
}
|