openethereum/src/bytes.rs

466 lines
11 KiB
Rust
Raw Normal View History

2015-11-28 03:58:37 +01:00
//! Unified interfaces for bytes operations on basic types
2015-12-26 15:48:41 +01:00
//!
2015-11-25 02:53:35 +01:00
//! # Examples
2015-11-28 03:58:37 +01:00
//! ```rust
//! extern crate ethcore_util as util;
2015-12-26 15:48:41 +01:00
//!
2015-11-28 03:58:37 +01:00
//! fn bytes_convertable() {
//! use util::bytes::BytesConvertable;
//!
//! let arr = [0; 5];
//! let slice: &[u8] = arr.bytes();
//! }
2015-12-26 15:48:41 +01:00
//!
2015-11-28 03:58:37 +01:00
//! fn to_bytes() {
//! use util::bytes::ToBytes;
2015-12-26 15:48:41 +01:00
//!
2015-11-28 03:58:37 +01:00
//! let a: Vec<u8> = "hello_world".to_bytes();
//! let b: Vec<u8> = 400u32.to_bytes();
//! let c: Vec<u8> = 0xffffffffffffffffu64.to_bytes();
//! }
2015-12-26 15:48:41 +01:00
//!
2015-11-28 03:58:37 +01:00
//! fn from_bytes() {
//! use util::bytes::FromBytes;
2015-12-26 15:48:41 +01:00
//!
2015-11-28 03:58:37 +01:00
//! let a = String::from_bytes(&[b'd', b'o', b'g']);
2015-11-29 19:35:22 +01:00
//! let b = u16::from_bytes(&[0xfa]);
2015-11-28 03:58:37 +01:00
//! let c = u64::from_bytes(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]);
//! }
2015-11-25 02:53:35 +01:00
//!
2015-11-28 03:58:37 +01:00
//! fn main() {
//! bytes_convertable();
//! to_bytes();
//! from_bytes();
//! }
//! ```
2015-11-25 02:53:35 +01:00
use std::mem;
2015-11-25 02:53:35 +01:00
use std::fmt;
2016-01-08 11:02:32 +01:00
use std::slice;
use std::cmp::Ordering;
2015-11-25 02:53:35 +01:00
use std::error::Error as StdError;
2016-01-13 15:14:24 +01:00
use std::ops::{Deref, DerefMut};
2015-11-26 15:00:17 +01:00
use uint::{U128, U256};
use hash::FixedHash;
2015-11-25 02:53:35 +01:00
2015-12-01 18:25:18 +01:00
pub struct PrettySlice<'a> (&'a [u8]);
impl<'a> fmt::Debug for PrettySlice<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for i in 0..self.0.len() {
2015-12-01 19:09:48 +01:00
match i > 0 {
true => { try!(write!(f, "·{:02x}", self.0[i])); },
false => { try!(write!(f, "{:02x}", self.0[i])); },
}
2015-12-01 18:25:18 +01:00
}
Ok(())
}
}
impl<'a> fmt::Display for PrettySlice<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for i in 0..self.0.len() {
try!(write!(f, "{:02x}", self.0[i]));
}
Ok(())
}
}
2015-12-01 18:25:18 +01:00
pub trait ToPretty {
fn pretty(&self) -> PrettySlice;
fn to_hex(&self) -> String {
format!("{}", self.pretty())
}
2015-12-01 18:25:18 +01:00
}
impl<'a> ToPretty for &'a [u8] {
fn pretty(&self) -> PrettySlice {
PrettySlice(self)
}
}
impl<'a> ToPretty for &'a Bytes {
fn pretty(&self) -> PrettySlice {
PrettySlice(self.bytes())
}
}
impl ToPretty for Bytes {
fn pretty(&self) -> PrettySlice {
PrettySlice(self.bytes())
}
}
2016-01-13 15:14:24 +01:00
pub enum BytesRef<'a> {
Flexible(&'a mut Bytes),
Fixed(&'a mut [u8])
}
impl<'a> Deref for BytesRef<'a> {
type Target = [u8];
fn deref(&self) -> &[u8] {
match self {
&BytesRef::Flexible(ref bytes) => bytes,
&BytesRef::Fixed(ref bytes) => bytes
}
}
}
impl <'a> DerefMut for BytesRef<'a> {
fn deref_mut(&mut self) -> &mut [u8] {
match self {
&mut BytesRef::Flexible(ref mut bytes) => bytes,
&mut BytesRef::Fixed(ref mut bytes) => bytes
}
}
}
2015-11-28 03:58:37 +01:00
/// Vector of bytes
2015-11-28 00:14:40 +01:00
pub type Bytes = Vec<u8>;
2015-11-28 03:58:37 +01:00
/// Slice of bytes to underlying memory
2015-11-27 17:54:33 +01:00
pub trait BytesConvertable {
2016-01-05 19:05:51 +01:00
// TODO: rename to as_slice
2015-11-27 17:54:33 +01:00
fn bytes(&self) -> &[u8];
2016-01-05 19:05:51 +01:00
fn as_slice(&self) -> &[u8] { self.bytes() }
fn to_bytes(&self) -> Bytes { self.as_slice().to_vec() }
2015-11-27 17:54:33 +01:00
}
impl<'a> BytesConvertable for &'a [u8] {
fn bytes(&self) -> &[u8] { self }
}
impl BytesConvertable for Vec<u8> {
fn bytes(&self) -> &[u8] { self }
}
macro_rules! impl_bytes_convertable_for_array {
2016-01-12 17:40:55 +01:00
($zero: expr) => ();
($len: expr, $($idx: expr),*) => {
impl BytesConvertable for [u8; $len] {
fn bytes(&self) -> &[u8] { self }
}
impl_bytes_convertable_for_array! { $($idx),* }
}
}
2015-11-27 18:18:49 +01:00
// -1 at the end is not expanded
impl_bytes_convertable_for_array! {
2016-01-12 17:40:55 +01:00
32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16,
15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1
}
2015-11-27 17:54:33 +01:00
#[test]
fn bytes_convertable() {
assert_eq!(vec![0x12u8, 0x34].bytes(), &[0x12u8, 0x34]);
2016-01-12 17:40:55 +01:00
assert_eq!([0u8; 0].bytes(), &[]);
2015-11-27 17:54:33 +01:00
}
2015-11-28 03:58:37 +01:00
/// Converts given type to its shortest representation in bytes
///
2015-11-25 02:53:35 +01:00
/// TODO: optimise some conversations
pub trait ToBytes {
2015-11-27 17:54:33 +01:00
fn to_bytes(&self) -> Vec<u8>;
fn to_bytes_len(&self) -> usize { self.to_bytes().len() }
fn first_byte(&self) -> Option<u8> { self.to_bytes().first().map(|&x| { x })}
2015-11-25 02:53:35 +01:00
}
impl <'a> ToBytes for &'a str {
2015-11-27 17:54:33 +01:00
fn to_bytes(&self) -> Vec<u8> {
From::from(*self)
}
2015-12-26 15:48:41 +01:00
2015-11-27 17:54:33 +01:00
fn to_bytes_len(&self) -> usize { self.len() }
2015-11-25 02:53:35 +01:00
}
impl ToBytes for String {
2015-11-27 17:54:33 +01:00
fn to_bytes(&self) -> Vec<u8> {
let s: &str = self.as_ref();
From::from(s)
}
2015-12-26 15:48:41 +01:00
2015-11-27 17:54:33 +01:00
fn to_bytes_len(&self) -> usize { self.len() }
2015-11-25 02:53:35 +01:00
}
impl ToBytes for u64 {
2015-11-27 17:54:33 +01:00
fn to_bytes(&self) -> Vec<u8> {
let mut res= vec![];
let count = self.to_bytes_len();
res.reserve(count);
for i in 0..count {
let j = count - 1 - i;
res.push((*self >> (j * 8)) as u8);
}
res
}
2015-11-25 02:53:35 +01:00
2015-11-27 17:54:33 +01:00
fn to_bytes_len(&self) -> usize { 8 - self.leading_zeros() as usize / 8 }
2015-11-25 02:53:35 +01:00
}
2015-12-26 15:48:41 +01:00
impl ToBytes for bool {
fn to_bytes(&self) -> Vec<u8> {
vec![ if *self { 1u8 } else { 0u8 } ]
}
fn to_bytes_len(&self) -> usize { 1 }
}
2015-11-25 02:53:35 +01:00
macro_rules! impl_map_to_bytes {
2015-11-27 17:54:33 +01:00
($from: ident, $to: ty) => {
impl ToBytes for $from {
fn to_bytes(&self) -> Vec<u8> { (*self as $to).to_bytes() }
fn to_bytes_len(&self) -> usize { (*self as $to).to_bytes_len() }
}
}
2015-11-25 02:53:35 +01:00
}
impl_map_to_bytes!(usize, u64);
impl_map_to_bytes!(u16, u64);
impl_map_to_bytes!(u32, u64);
2015-11-26 15:00:17 +01:00
macro_rules! impl_uint_to_bytes {
2015-11-27 17:54:33 +01:00
($name: ident) => {
impl ToBytes for $name {
2015-12-26 15:48:41 +01:00
fn to_bytes(&self) -> Vec<u8> {
2015-11-27 17:54:33 +01:00
let mut res= vec![];
let count = self.to_bytes_len();
res.reserve(count);
for i in 0..count {
let j = count - 1 - i;
res.push(self.byte(j));
}
res
}
fn to_bytes_len(&self) -> usize { (self.bits() + 7) / 8 }
}
}
2015-11-26 15:00:17 +01:00
}
impl_uint_to_bytes!(U256);
impl_uint_to_bytes!(U128);
impl <T>ToBytes for T where T: FixedHash {
fn to_bytes(&self) -> Vec<u8> {
let mut res: Vec<u8> = vec![];
res.reserve(T::size());
unsafe {
use std::ptr;
ptr::copy(self.bytes().as_ptr(), res.as_mut_ptr(), T::size());
res.set_len(T::size());
}
2015-12-26 15:48:41 +01:00
res
}
}
2015-11-28 03:58:37 +01:00
/// Error returned when FromBytes conversation goes wrong
2015-11-25 02:53:35 +01:00
#[derive(Debug, PartialEq, Eq)]
pub enum FromBytesError {
DataIsTooShort,
DataIsTooLong
2015-11-25 02:53:35 +01:00
}
impl StdError for FromBytesError {
2015-11-27 17:54:33 +01:00
fn description(&self) -> &str { "from_bytes error" }
2015-11-25 02:53:35 +01:00
}
impl fmt::Display for FromBytesError {
2015-11-27 17:54:33 +01:00
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(&self, f)
}
2015-11-25 02:53:35 +01:00
}
2015-11-28 03:58:37 +01:00
/// Alias for the result of FromBytes trait
2015-11-25 02:53:35 +01:00
pub type FromBytesResult<T> = Result<T, FromBytesError>;
2015-11-28 03:58:37 +01:00
/// Converts to given type from its bytes representation
///
2015-11-26 15:00:17 +01:00
/// TODO: check size of bytes before conversation and return appropriate error
2015-11-25 02:53:35 +01:00
pub trait FromBytes: Sized {
2015-11-27 17:54:33 +01:00
fn from_bytes(bytes: &[u8]) -> FromBytesResult<Self>;
2015-11-25 02:53:35 +01:00
}
impl FromBytes for String {
2015-11-27 17:54:33 +01:00
fn from_bytes(bytes: &[u8]) -> FromBytesResult<String> {
Ok(::std::str::from_utf8(bytes).unwrap().to_string())
}
2015-11-25 02:53:35 +01:00
}
macro_rules! impl_uint_from_bytes {
($to: ident) => {
impl FromBytes for $to {
fn from_bytes(bytes: &[u8]) -> FromBytesResult<$to> {
match bytes.len() {
0 => Ok(0),
l if l <= mem::size_of::<$to>() => {
let mut res = 0 as $to;
for i in 0..l {
let shift = (l - 1 - i) * 8;
res = res + ((bytes[i] as $to) << shift);
}
Ok(res)
}
_ => Err(FromBytesError::DataIsTooLong)
2015-11-27 17:54:33 +01:00
}
}
}
}
2015-11-25 02:53:35 +01:00
}
2015-12-26 15:48:41 +01:00
impl FromBytes for bool {
fn from_bytes(bytes: &[u8]) -> FromBytesResult<bool> {
match bytes.len() {
0 => Ok(false),
1 => Ok(bytes[0] != 0),
_ => Err(FromBytesError::DataIsTooLong),
}
}
}
//impl_uint_from_bytes!(u8);
impl_uint_from_bytes!(u16);
impl_uint_from_bytes!(u32);
impl_uint_from_bytes!(u64);
impl_uint_from_bytes!(usize);
2015-11-26 15:00:17 +01:00
macro_rules! impl_uint_from_bytes {
2015-11-27 17:54:33 +01:00
($name: ident) => {
impl FromBytes for $name {
fn from_bytes(bytes: &[u8]) -> FromBytesResult<$name> {
2016-01-11 23:59:33 +01:00
if bytes.len() <= $name::SIZE {
Ok($name::from(bytes))
} else {
Err(FromBytesError::DataIsTooLong)
}
2015-11-27 17:54:33 +01:00
}
}
}
2015-11-26 15:00:17 +01:00
}
impl_uint_from_bytes!(U256);
impl_uint_from_bytes!(U128);
impl <T>FromBytes for T where T: FixedHash {
fn from_bytes(bytes: &[u8]) -> FromBytesResult<T> {
match bytes.len().cmp(&T::size()) {
Ordering::Less => return Err(FromBytesError::DataIsTooShort),
Ordering::Greater => return Err(FromBytesError::DataIsTooLong),
Ordering::Equal => ()
};
unsafe {
use std::{mem, ptr};
let mut res: T = mem::uninitialized();
ptr::copy(bytes.as_ptr(), res.as_slice_mut().as_mut_ptr(), T::size());
Ok(res)
}
}
}
2015-11-29 19:35:22 +01:00
/// Simple trait to allow for raw population of a Sized object from a byte slice.
pub trait Populatable {
2016-01-08 11:02:32 +01:00
/// Copies a bunch of bytes `d` to `self`, overwriting as necessary.
///
/// If `d` is smaller, zero-out the remaining bytes.
2016-01-08 11:43:11 +01:00
fn populate_raw(&mut self, d: &[u8]) {
let mut s = self.as_slice_mut();
for i in 0..s.len() {
s[i] = if i < d.len() {d[i]} else {0};
}
}
2016-01-08 11:02:32 +01:00
/// Copies a bunch of bytes `d` to `self`, overwriting as necessary.
///
/// If `d` is smaller, will leave some bytes untouched.
2016-01-08 12:05:38 +01:00
fn copy_raw(&mut self, d: &[u8]) {
2016-01-08 11:43:11 +01:00
use std::io::Write;
self.as_slice_mut().write(&d).unwrap();
}
/// Copies the raw representation of an object `d` to `self`, overwriting as necessary.
///
/// If `d` is smaller, zero-out the remaining bytes.
fn populate_raw_from(&mut self, d: &BytesConvertable) { self.populate_raw(d.as_slice()); }
/// Copies the raw representation of an object `d` to `self`, overwriting as necessary.
///
/// If `d` is smaller, will leave some bytes untouched.
2016-01-08 12:05:38 +01:00
fn copy_raw_from(&mut self, d: &BytesConvertable) { self.copy_raw(d.as_slice()); }
2016-01-08 11:43:11 +01:00
/// Get the raw slice for this object.
fn as_slice_mut(&mut self) -> &mut [u8];
}
impl<T> Populatable for T where T: Sized {
2016-01-08 11:43:11 +01:00
fn as_slice_mut(&mut self) -> &mut [u8] {
use std::mem;
unsafe {
slice::from_raw_parts_mut(self as *mut T as *mut u8, mem::size_of::<T>())
2016-01-08 11:43:11 +01:00
}
}
}
2016-01-08 11:02:32 +01:00
2016-01-08 11:43:11 +01:00
impl<T> Populatable for [T] where T: Sized {
fn as_slice_mut(&mut self) -> &mut [u8] {
2016-01-08 11:02:32 +01:00
use std::mem;
unsafe {
2016-01-08 11:43:11 +01:00
slice::from_raw_parts_mut(self.as_mut_ptr() as *mut u8, mem::size_of::<T>() * self.len())
}
}
}
2016-01-08 11:02:32 +01:00
#[test]
2016-01-08 11:43:11 +01:00
fn fax_raw() {
2016-01-08 11:02:32 +01:00
let mut x = [255u8; 4];
2016-01-08 12:05:38 +01:00
x.copy_raw(&[1u8; 2][..]);
2016-01-08 11:02:32 +01:00
assert_eq!(x, [1u8, 1, 255, 255]);
2016-01-08 11:43:11 +01:00
let mut x = [255u8; 4];
2016-01-08 12:05:38 +01:00
x.copy_raw(&[1u8; 6][..]);
2016-01-08 11:02:32 +01:00
assert_eq!(x, [1u8, 1, 1, 1]);
}
#[test]
fn populate_raw() {
let mut x = [255u8; 4];
x.populate_raw(&[1u8; 2][..]);
assert_eq!(x, [1u8, 1, 0, 0]);
2016-01-08 11:43:11 +01:00
let mut x = [255u8; 4];
2016-01-08 11:02:32 +01:00
x.populate_raw(&[1u8; 6][..]);
assert_eq!(x, [1u8, 1, 1, 1]);
2016-01-08 11:43:11 +01:00
}
#[test]
fn populate_raw_dyn() {
let mut x = [255u8; 4];
x.populate_raw(&[1u8; 2][..]);
assert_eq!(&x[..], [1u8, 1, 0, 0]);
let mut x = [255u8; 4];
x.populate_raw(&[1u8; 6][..]);
assert_eq!(&x[..], [1u8, 1, 1, 1]);
}
#[test]
fn fax_raw_dyn() {
let mut x = [255u8; 4];
2016-01-08 12:05:38 +01:00
x.copy_raw(&[1u8; 2][..]);
2016-01-08 11:43:11 +01:00
assert_eq!(&x[..], [1u8, 1, 255, 255]);
let mut x = [255u8; 4];
2016-01-08 12:05:38 +01:00
x.copy_raw(&[1u8; 6][..]);
2016-01-08 11:43:11 +01:00
assert_eq!(&x[..], [1u8, 1, 1, 1]);
}
#[test]
fn populate_big_types() {
use hash::*;
let a = address_from_hex("ffffffffffffffffffffffffffffffffffffffff");
let mut h = h256_from_u64(0x69);
h.populate_raw_from(&a);
assert_eq!(h, h256_from_hex("ffffffffffffffffffffffffffffffffffffffff000000000000000000000000"));
let mut h = h256_from_u64(0x69);
2016-01-08 12:05:38 +01:00
h.copy_raw_from(&a);
assert_eq!(h, h256_from_hex("ffffffffffffffffffffffffffffffffffffffff000000000000000000000069"));
2016-01-13 15:14:24 +01:00
}