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-20 18:01:53 +02:00
|
|
|
use util::bytes::*;
|
|
|
|
use std::mem;
|
|
|
|
|
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-20 18:01:53 +02:00
|
|
|
fn size(&self) -> usize;
|
2016-04-20 13:17:11 +02:00
|
|
|
|
2016-04-20 18:01:53 +02:00
|
|
|
fn to_bytes(&self, buffer: &mut [u8]) -> Result<(), BinaryConvertError>;
|
2016-04-20 17:02:17 +02:00
|
|
|
|
|
|
|
fn from_bytes(buffer: &[u8]) -> Result<Self, BinaryConvertError>;
|
2016-04-20 13:17:11 +02:00
|
|
|
}
|
2016-04-20 18:01:53 +02:00
|
|
|
|
|
|
|
macro_rules! binary_fixed_size {
|
|
|
|
($target_ty: ident) => {
|
|
|
|
impl BinaryConvertable for $target_ty {
|
|
|
|
fn size(&self) -> usize {
|
|
|
|
mem::size_of::<$target_ty>()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_bytes(bytes: &[u8]) -> Result<Self, BinaryConvertError> {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_bytes(&self, buffer: &mut [u8]) -> Result<(), BinaryConvertError> {
|
|
|
|
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);
|
|
|
|
binary_fixed_size!(bool);
|