2015-11-30 02:55:03 +01:00
|
|
|
//! Rlp serialization module
|
|
|
|
//!
|
|
|
|
//! Allows encoding, decoding, and view onto rlp-slice
|
2015-11-26 00:37:53 +01:00
|
|
|
//!
|
2015-11-30 11:16:50 +01:00
|
|
|
//!# What should you use when?
|
2015-11-27 20:59:28 +01:00
|
|
|
//!
|
2015-11-30 02:55:03 +01:00
|
|
|
//!### Use `encode` function when:
|
|
|
|
//! * You want to encode something inline.
|
|
|
|
//! * You do not work on big set of data.
|
|
|
|
//! * You want to encode whole data structure at once.
|
2015-11-28 10:50:41 +01:00
|
|
|
//!
|
2015-11-30 02:55:03 +01:00
|
|
|
//!### Use `decode` function when:
|
|
|
|
//! * You want to decode something inline.
|
|
|
|
//! * You do not work on big set of data.
|
|
|
|
//! * You want to decode whole rlp at once.
|
2015-11-26 00:37:53 +01:00
|
|
|
//!
|
2015-11-30 02:55:03 +01:00
|
|
|
//!### Use `RlpStream` when:
|
|
|
|
//! * You want to encode something in portions.
|
|
|
|
//! * You encode a big set of data.
|
2015-11-28 10:50:41 +01:00
|
|
|
//!
|
2015-11-30 02:55:03 +01:00
|
|
|
//!### Use `Rlp` when:
|
|
|
|
//! * You are working on trusted data (not corrupted).
|
|
|
|
//! * You want to get view onto rlp-slice.
|
|
|
|
//! * You don't want to decode whole rlp at once.
|
2015-11-26 00:37:53 +01:00
|
|
|
//!
|
2015-11-30 02:55:03 +01:00
|
|
|
//!### Use `UntrustedRlp` when:
|
|
|
|
//! * You are working on untrusted data (~corrupted).
|
|
|
|
//! * You need to handle data corruption errors.
|
|
|
|
//! * You are working on input data.
|
|
|
|
//! * You want to get view onto rlp-slice.
|
|
|
|
//! * You don't want to decode whole rlp at once.
|
2015-11-25 02:53:35 +01:00
|
|
|
|
|
|
|
use std::fmt;
|
|
|
|
use std::cell::Cell;
|
2015-11-25 23:40:01 +01:00
|
|
|
use std::collections::LinkedList;
|
2015-11-25 02:53:35 +01:00
|
|
|
use std::error::Error as StdError;
|
2015-11-25 12:46:27 +01:00
|
|
|
use bytes::{ToBytes, FromBytes, FromBytesError};
|
2015-11-26 19:09:14 +01:00
|
|
|
use vector::InsertSlice;
|
2015-11-25 02:53:35 +01:00
|
|
|
|
2015-11-30 02:55:03 +01:00
|
|
|
/// Data-oriented view onto rlp-slice.
|
|
|
|
///
|
|
|
|
/// This is immutable structere. No operations change it.
|
|
|
|
///
|
|
|
|
/// Should be used in places where, error handling is required,
|
|
|
|
/// eg. on input
|
2015-11-25 02:53:35 +01:00
|
|
|
#[derive(Debug)]
|
2015-11-29 09:16:15 +01:00
|
|
|
pub struct UntrustedRlp<'a> {
|
2015-11-27 20:59:28 +01:00
|
|
|
bytes: &'a [u8],
|
|
|
|
cache: Cell<OffsetCache>,
|
2015-11-25 02:53:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// rlp offset
|
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
struct OffsetCache {
|
2015-11-27 20:59:28 +01:00
|
|
|
index: usize,
|
|
|
|
offset: usize,
|
2015-11-25 02:53:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl OffsetCache {
|
2015-11-27 20:59:28 +01:00
|
|
|
fn new(index: usize, offset: usize) -> OffsetCache {
|
|
|
|
OffsetCache {
|
|
|
|
index: index,
|
|
|
|
offset: offset,
|
|
|
|
}
|
|
|
|
}
|
2015-11-25 02:53:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// stores basic information about item
|
2015-11-25 21:04:43 +01:00
|
|
|
struct ItemInfo {
|
2015-11-27 20:59:28 +01:00
|
|
|
prefix_len: usize,
|
|
|
|
value_len: usize,
|
2015-11-25 02:53:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ItemInfo {
|
2015-11-27 20:59:28 +01:00
|
|
|
fn new(prefix_len: usize, value_len: usize) -> ItemInfo {
|
|
|
|
ItemInfo {
|
|
|
|
prefix_len: prefix_len,
|
|
|
|
value_len: value_len,
|
|
|
|
}
|
|
|
|
}
|
2015-11-25 02:53:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
|
|
|
pub enum DecoderError {
|
2015-11-27 20:59:28 +01:00
|
|
|
FromBytesError(FromBytesError),
|
2015-11-30 11:33:08 +01:00
|
|
|
RlpIsTooShort,
|
|
|
|
RlpExpectedToBeList,
|
|
|
|
RlpExpectedToBeData,
|
|
|
|
BadRlp,
|
2015-11-25 02:53:35 +01:00
|
|
|
}
|
|
|
|
impl StdError for DecoderError {
|
2015-11-27 20:59:28 +01:00
|
|
|
fn description(&self) -> &str {
|
|
|
|
"builder error"
|
|
|
|
}
|
2015-11-25 02:53:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for DecoderError {
|
2015-11-27 20:59:28 +01:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
fmt::Debug::fmt(&self, f)
|
|
|
|
}
|
2015-11-25 02:53:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<FromBytesError> for DecoderError {
|
2015-11-27 20:59:28 +01:00
|
|
|
fn from(err: FromBytesError) -> DecoderError {
|
|
|
|
DecoderError::FromBytesError(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-30 02:55:03 +01:00
|
|
|
/// Data-oriented view onto trusted rlp-slice.
|
2015-11-28 22:10:58 +01:00
|
|
|
///
|
2015-11-30 02:55:03 +01:00
|
|
|
/// Unlikely to `UntrustedRlp` doesn't bother you with error
|
|
|
|
/// handling. It assumes that you know what you are doing.
|
2015-11-29 09:16:15 +01:00
|
|
|
pub struct Rlp<'a> {
|
|
|
|
rlp: UntrustedRlp<'a>
|
2015-11-28 22:10:58 +01:00
|
|
|
}
|
|
|
|
|
2015-11-29 09:16:15 +01:00
|
|
|
impl<'a> From<UntrustedRlp<'a>> for Rlp<'a> {
|
|
|
|
fn from(rlp: UntrustedRlp<'a>) -> Rlp<'a> {
|
|
|
|
Rlp { rlp: rlp }
|
2015-11-28 22:10:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-29 09:16:15 +01:00
|
|
|
impl<'a> From<Rlp<'a>> for UntrustedRlp<'a> {
|
|
|
|
fn from(unsafe_rlp: Rlp<'a>) -> UntrustedRlp<'a> {
|
2015-11-28 22:10:58 +01:00
|
|
|
unsafe_rlp.rlp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-30 13:19:55 +01:00
|
|
|
pub enum Prototype {
|
|
|
|
Null,
|
|
|
|
Data(usize),
|
|
|
|
List(usize),
|
|
|
|
}
|
|
|
|
|
2015-11-30 17:23:52 +01:00
|
|
|
impl<'a, 'view> Rlp<'a> where 'a: 'view {
|
2015-11-30 02:55:03 +01:00
|
|
|
/// Create a new instance of `Rlp`
|
2015-11-29 09:16:15 +01:00
|
|
|
pub fn new(bytes: &'a [u8]) -> Rlp<'a> {
|
|
|
|
Rlp {
|
|
|
|
rlp: UntrustedRlp::new(bytes)
|
2015-11-28 22:10:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-30 13:19:55 +01:00
|
|
|
/// Get the prototype of the RLP.
|
|
|
|
pub fn prototype(&self) -> Prototype {
|
|
|
|
if self.is_data() {
|
|
|
|
Prototype::Data(self.size())
|
|
|
|
}
|
|
|
|
else if self.is_list() {
|
|
|
|
Prototype::List(self.item_count())
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Prototype::Null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-30 11:54:09 +01:00
|
|
|
/// The bare data of the rlp.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// extern crate ethcore_util as util;
|
|
|
|
/// use util::rlp::*;
|
|
|
|
///
|
|
|
|
/// fn main () {
|
|
|
|
/// let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g'];
|
|
|
|
/// let rlp = Rlp::new(&data);
|
2015-11-30 17:23:52 +01:00
|
|
|
/// let dog = rlp.at(1).data();
|
2015-11-30 11:54:09 +01:00
|
|
|
/// assert_eq!(dog, &[0x83, b'd', b'o', b'g']);
|
|
|
|
/// }
|
|
|
|
/// ```
|
2015-11-30 17:23:52 +01:00
|
|
|
pub fn data(&'view self) -> &'a [u8] {
|
2015-11-30 11:54:09 +01:00
|
|
|
self.rlp.data()
|
|
|
|
}
|
|
|
|
|
2015-11-30 12:41:11 +01:00
|
|
|
/// Returns number of rlp items.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// extern crate ethcore_util as util;
|
|
|
|
/// use util::rlp::*;
|
|
|
|
///
|
|
|
|
/// fn main () {
|
|
|
|
/// let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g'];
|
|
|
|
/// let rlp = Rlp::new(&data);
|
2015-11-30 12:58:23 +01:00
|
|
|
/// assert_eq!(rlp.item_count(), 2);
|
2015-11-30 12:41:11 +01:00
|
|
|
/// let view = rlp.at(1);
|
2015-11-30 12:58:23 +01:00
|
|
|
/// assert_eq!(view.item_count(), 0);
|
2015-11-30 12:41:11 +01:00
|
|
|
/// }
|
|
|
|
/// ```
|
2015-11-30 12:58:23 +01:00
|
|
|
pub fn item_count(&self) -> usize {
|
|
|
|
self.rlp.item_count()
|
2015-11-30 12:41:11 +01:00
|
|
|
}
|
|
|
|
|
2015-11-30 13:06:51 +01:00
|
|
|
/// Returns the number of bytes in the data, or zero if it isn't data.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// extern crate ethcore_util as util;
|
|
|
|
/// use util::rlp::*;
|
|
|
|
///
|
|
|
|
/// fn main () {
|
|
|
|
/// let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g'];
|
|
|
|
/// let rlp = Rlp::new(&data);
|
|
|
|
/// assert_eq!(rlp.size(), 0);
|
|
|
|
/// let view = rlp.at(1);
|
|
|
|
/// assert_eq!(view.size(), 3);
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
pub fn size(&self) -> usize {
|
|
|
|
self.rlp.size()
|
|
|
|
}
|
|
|
|
|
2015-11-30 02:55:03 +01:00
|
|
|
/// Get view onto rlp-slice at index.
|
|
|
|
///
|
|
|
|
/// Caches offset to given index, so access to successive
|
|
|
|
/// slices is faster.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// extern crate ethcore_util as util;
|
|
|
|
/// use util::rlp::*;
|
|
|
|
///
|
|
|
|
/// fn main () {
|
|
|
|
/// let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g'];
|
|
|
|
/// let rlp = Rlp::new(&data);
|
|
|
|
/// let dog = String::decode(&rlp.at(1));
|
|
|
|
/// assert_eq!(dog, "dog".to_string());
|
|
|
|
/// }
|
|
|
|
/// ```
|
2015-11-30 17:23:52 +01:00
|
|
|
pub fn at(&'view self, index: usize) -> Rlp<'a> {
|
2015-11-28 22:10:58 +01:00
|
|
|
From::from(self.rlp.at(index).unwrap())
|
|
|
|
}
|
|
|
|
|
2015-11-30 12:06:08 +01:00
|
|
|
/// No value
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// extern crate ethcore_util as util;
|
|
|
|
/// use util::rlp::*;
|
|
|
|
///
|
|
|
|
/// fn main () {
|
|
|
|
/// let data = vec![];
|
|
|
|
/// let rlp = Rlp::new(&data);
|
|
|
|
/// assert!(rlp.is_null());
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
pub fn is_null(&self) -> bool {
|
|
|
|
self.rlp.is_null()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Contains a zero-length string or zero-length list.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// extern crate ethcore_util as util;
|
|
|
|
/// use util::rlp::*;
|
|
|
|
///
|
|
|
|
/// fn main () {
|
|
|
|
/// let data = vec![0xc0];
|
|
|
|
/// let rlp = Rlp::new(&data);
|
|
|
|
/// assert!(rlp.is_empty());
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.rlp.is_empty()
|
|
|
|
}
|
|
|
|
|
2015-11-30 02:55:03 +01:00
|
|
|
/// List value
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// extern crate ethcore_util as util;
|
|
|
|
/// use util::rlp::*;
|
|
|
|
///
|
|
|
|
/// fn main () {
|
|
|
|
/// let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g'];
|
|
|
|
/// let rlp = Rlp::new(&data);
|
|
|
|
/// assert!(rlp.is_list());
|
|
|
|
/// }
|
|
|
|
/// ```
|
2015-11-28 22:10:58 +01:00
|
|
|
pub fn is_list(&self) -> bool {
|
|
|
|
self.rlp.is_list()
|
|
|
|
}
|
|
|
|
|
2015-11-30 02:55:03 +01:00
|
|
|
/// String value
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// extern crate ethcore_util as util;
|
|
|
|
/// use util::rlp::*;
|
|
|
|
///
|
|
|
|
/// fn main () {
|
|
|
|
/// let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g'];
|
|
|
|
/// let rlp = Rlp::new(&data);
|
|
|
|
/// assert!(rlp.at(1).is_data());
|
|
|
|
/// }
|
|
|
|
/// ```
|
2015-11-30 01:30:35 +01:00
|
|
|
pub fn is_data(&self) -> bool {
|
|
|
|
self.rlp.is_data()
|
2015-11-28 22:10:58 +01:00
|
|
|
}
|
|
|
|
|
2015-11-30 12:28:49 +01:00
|
|
|
/// Int value
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// extern crate ethcore_util as util;
|
|
|
|
/// use util::rlp::*;
|
|
|
|
///
|
|
|
|
/// fn main () {
|
|
|
|
/// let data = vec![0xc1, 0x10];
|
|
|
|
/// let rlp = Rlp::new(&data);
|
|
|
|
/// assert_eq!(rlp.is_int(), false);
|
|
|
|
/// assert_eq!(rlp.at(0).is_int(), true);
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
pub fn is_int(&self) -> bool {
|
|
|
|
self.rlp.is_int()
|
|
|
|
}
|
|
|
|
|
2015-11-30 02:55:03 +01:00
|
|
|
/// Get iterator over rlp-slices
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// extern crate ethcore_util as util;
|
|
|
|
/// use util::rlp::*;
|
|
|
|
///
|
|
|
|
/// fn main () {
|
|
|
|
/// let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g'];
|
|
|
|
/// let rlp = Rlp::new(&data);
|
|
|
|
/// let strings: Vec<String> = rlp.iter().map(| i | String::decode(&i)).collect();
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
pub fn iter(&'a self) -> RlpIterator<'a> {
|
|
|
|
self.into_iter()
|
2015-11-28 22:10:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-30 17:23:52 +01:00
|
|
|
impl<'a, 'view> UntrustedRlp<'a> where 'a: 'view {
|
2015-11-29 09:16:15 +01:00
|
|
|
/// returns new instance of `UntrustedRlp`
|
|
|
|
pub fn new(bytes: &'a [u8]) -> UntrustedRlp<'a> {
|
|
|
|
UntrustedRlp {
|
2015-11-27 20:59:28 +01:00
|
|
|
bytes: bytes,
|
|
|
|
cache: Cell::new(OffsetCache::new(usize::max_value(), 0)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-30 11:54:09 +01:00
|
|
|
/// The bare data of the rlp.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// extern crate ethcore_util as util;
|
|
|
|
/// use util::rlp::*;
|
|
|
|
///
|
|
|
|
/// fn main () {
|
|
|
|
/// let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g'];
|
|
|
|
/// let rlp = UntrustedRlp::new(&data);
|
2015-11-30 17:23:52 +01:00
|
|
|
/// let dog = rlp.at(1).unwrap().data();
|
2015-11-30 11:54:09 +01:00
|
|
|
/// assert_eq!(dog, &[0x83, b'd', b'o', b'g']);
|
|
|
|
/// }
|
|
|
|
/// ```
|
2015-11-30 17:23:52 +01:00
|
|
|
pub fn data(&'view self) -> &'a [u8] {
|
2015-11-30 11:54:09 +01:00
|
|
|
self.bytes
|
|
|
|
}
|
|
|
|
|
2015-11-30 12:41:11 +01:00
|
|
|
/// Returns number of rlp items.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// extern crate ethcore_util as util;
|
|
|
|
/// use util::rlp::*;
|
|
|
|
///
|
|
|
|
/// fn main () {
|
|
|
|
/// let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g'];
|
|
|
|
/// let rlp = UntrustedRlp::new(&data);
|
2015-11-30 12:58:23 +01:00
|
|
|
/// assert_eq!(rlp.item_count(), 2);
|
2015-11-30 12:41:11 +01:00
|
|
|
/// let view = rlp.at(1).unwrap();
|
2015-11-30 12:58:23 +01:00
|
|
|
/// assert_eq!(view.item_count(), 0);
|
2015-11-30 12:41:11 +01:00
|
|
|
/// }
|
|
|
|
/// ```
|
2015-11-30 12:58:23 +01:00
|
|
|
pub fn item_count(&self) -> usize {
|
2015-11-30 12:41:11 +01:00
|
|
|
match self.is_list() {
|
|
|
|
true => self.iter().count(),
|
|
|
|
false => 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-30 13:06:51 +01:00
|
|
|
/// Returns the number of bytes in the data, or zero if it isn't data.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// extern crate ethcore_util as util;
|
|
|
|
/// use util::rlp::*;
|
|
|
|
///
|
|
|
|
/// fn main () {
|
|
|
|
/// let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g'];
|
|
|
|
/// let rlp = UntrustedRlp::new(&data);
|
|
|
|
/// assert_eq!(rlp.size(), 0);
|
|
|
|
/// let view = rlp.at(1).unwrap();
|
|
|
|
/// assert_eq!(view.size(), 3);
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
pub fn size(&self) -> usize {
|
|
|
|
match self.is_data() {
|
|
|
|
true => Self::item_info(self.bytes).unwrap().value_len,
|
|
|
|
false => 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-30 02:55:03 +01:00
|
|
|
/// Get view onto rlp-slice at index
|
|
|
|
///
|
|
|
|
/// Caches offset to given index, so access to successive
|
|
|
|
/// slices is faster.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// extern crate ethcore_util as util;
|
|
|
|
/// use util::rlp::*;
|
|
|
|
///
|
|
|
|
/// fn main () {
|
|
|
|
/// let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g'];
|
|
|
|
/// let rlp = UntrustedRlp::new(&data);
|
|
|
|
/// let dog = String::decode_untrusted(&rlp.at(1).unwrap()).unwrap();
|
|
|
|
/// assert_eq!(dog, "dog".to_string());
|
|
|
|
/// }
|
|
|
|
/// ```
|
2015-11-30 17:23:52 +01:00
|
|
|
pub fn at(&'view self, index: usize) -> Result<UntrustedRlp<'a>, DecoderError> {
|
2015-11-27 20:59:28 +01:00
|
|
|
if !self.is_list() {
|
2015-11-30 11:33:08 +01:00
|
|
|
return Err(DecoderError::RlpExpectedToBeList);
|
2015-11-27 20:59:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// move to cached position if it's index is less or equal to
|
|
|
|
// current search index, otherwise move to beginning of list
|
|
|
|
let c = self.cache.get();
|
|
|
|
let (mut bytes, to_skip) = match c.index <= index {
|
2015-11-29 09:16:15 +01:00
|
|
|
true => (try!(UntrustedRlp::consume(self.bytes, c.offset)), index - c.index),
|
2015-11-27 20:59:28 +01:00
|
|
|
false => (try!(self.consume_list_prefix()), index),
|
|
|
|
};
|
|
|
|
|
|
|
|
// skip up to x items
|
2015-11-29 09:16:15 +01:00
|
|
|
bytes = try!(UntrustedRlp::consume_items(bytes, to_skip));
|
2015-11-27 20:59:28 +01:00
|
|
|
|
|
|
|
// update the cache
|
|
|
|
self.cache.set(OffsetCache::new(index, self.bytes.len() - bytes.len()));
|
|
|
|
|
|
|
|
// construct new rlp
|
2015-11-29 09:16:15 +01:00
|
|
|
let found = try!(UntrustedRlp::item_info(bytes));
|
|
|
|
Ok(UntrustedRlp::new(&bytes[0..found.prefix_len + found.value_len]))
|
2015-11-27 20:59:28 +01:00
|
|
|
}
|
|
|
|
|
2015-11-30 12:06:08 +01:00
|
|
|
/// No value
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// extern crate ethcore_util as util;
|
|
|
|
/// use util::rlp::*;
|
|
|
|
///
|
|
|
|
/// fn main () {
|
|
|
|
/// let data = vec![];
|
|
|
|
/// let rlp = UntrustedRlp::new(&data);
|
|
|
|
/// assert!(rlp.is_null());
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
pub fn is_null(&self) -> bool {
|
|
|
|
self.bytes.len() == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Contains a zero-length string or zero-length list.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// extern crate ethcore_util as util;
|
|
|
|
/// use util::rlp::*;
|
|
|
|
///
|
|
|
|
/// fn main () {
|
|
|
|
/// let data = vec![0xc0];
|
|
|
|
/// let rlp = UntrustedRlp::new(&data);
|
|
|
|
/// assert!(rlp.is_empty());
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
!self.is_null() && (self.bytes[0] == 0xc0 || self.bytes[0] == 0x80)
|
|
|
|
}
|
|
|
|
|
2015-11-30 02:55:03 +01:00
|
|
|
/// List value
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// extern crate ethcore_util as util;
|
|
|
|
/// use util::rlp::*;
|
|
|
|
///
|
|
|
|
/// fn main () {
|
|
|
|
/// let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g'];
|
|
|
|
/// let rlp = UntrustedRlp::new(&data);
|
|
|
|
/// assert!(rlp.is_list());
|
|
|
|
/// }
|
|
|
|
/// ```
|
2015-11-27 20:59:28 +01:00
|
|
|
pub fn is_list(&self) -> bool {
|
2015-11-30 12:06:08 +01:00
|
|
|
!self.is_null() && self.bytes[0] >= 0xc0
|
2015-11-27 20:59:28 +01:00
|
|
|
}
|
|
|
|
|
2015-11-30 02:55:03 +01:00
|
|
|
/// String value
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// extern crate ethcore_util as util;
|
|
|
|
/// use util::rlp::*;
|
|
|
|
///
|
|
|
|
/// fn main () {
|
|
|
|
/// let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g'];
|
|
|
|
/// let rlp = UntrustedRlp::new(&data);
|
|
|
|
/// assert!(rlp.at(1).unwrap().is_data());
|
|
|
|
/// }
|
|
|
|
/// ```
|
2015-11-30 01:30:35 +01:00
|
|
|
pub fn is_data(&self) -> bool {
|
2015-11-30 12:06:08 +01:00
|
|
|
!self.is_null() && self.bytes[0] < 0xc0
|
2015-11-27 20:59:28 +01:00
|
|
|
}
|
|
|
|
|
2015-11-30 12:28:49 +01:00
|
|
|
/// Int value
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// extern crate ethcore_util as util;
|
|
|
|
/// use util::rlp::*;
|
|
|
|
///
|
|
|
|
/// fn main () {
|
|
|
|
/// let data = vec![0xc1, 0x10];
|
|
|
|
/// let rlp = UntrustedRlp::new(&data);
|
|
|
|
/// assert_eq!(rlp.is_int(), false);
|
|
|
|
/// assert_eq!(rlp.at(0).unwrap().is_int(), true);
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
pub fn is_int(&self) -> bool {
|
|
|
|
if self.is_null() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
match self.bytes[0] {
|
|
|
|
0...0x80 => true,
|
|
|
|
0x81...0xb7 => self.bytes[1] != 0,
|
|
|
|
b @ 0xb8...0xbf => self.bytes[1 + b as usize - 0xb7] != 0,
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-30 02:55:03 +01:00
|
|
|
/// Get iterator over rlp-slices
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// extern crate ethcore_util as util;
|
|
|
|
/// use util::rlp::*;
|
|
|
|
///
|
|
|
|
/// fn main () {
|
|
|
|
/// let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g'];
|
|
|
|
/// let rlp = UntrustedRlp::new(&data);
|
|
|
|
/// let strings: Vec<String> = rlp.iter()
|
|
|
|
/// .map(| i | String::decode_untrusted(&i))
|
|
|
|
/// .map(| s | s.unwrap())
|
|
|
|
/// .collect();
|
|
|
|
/// }
|
|
|
|
/// ```
|
2015-11-29 09:16:15 +01:00
|
|
|
pub fn iter(&'a self) -> UntrustedRlpIterator<'a> {
|
2015-11-27 20:59:28 +01:00
|
|
|
self.into_iter()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// consumes first found prefix
|
|
|
|
fn consume_list_prefix(&self) -> Result<&'a [u8], DecoderError> {
|
2015-11-29 09:16:15 +01:00
|
|
|
let item = try!(UntrustedRlp::item_info(self.bytes));
|
|
|
|
let bytes = try!(UntrustedRlp::consume(self.bytes, item.prefix_len));
|
2015-11-27 20:59:28 +01:00
|
|
|
Ok(bytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// consumes fixed number of items
|
|
|
|
fn consume_items(bytes: &'a [u8], items: usize) -> Result<&'a [u8], DecoderError> {
|
|
|
|
let mut result = bytes;
|
|
|
|
for _ in 0..items {
|
2015-11-29 09:16:15 +01:00
|
|
|
let i = try!(UntrustedRlp::item_info(result));
|
|
|
|
result = try!(UntrustedRlp::consume(result, (i.prefix_len + i.value_len)));
|
2015-11-27 20:59:28 +01:00
|
|
|
}
|
|
|
|
Ok(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// return first item info
|
2015-11-29 09:16:15 +01:00
|
|
|
///
|
2015-11-30 11:33:31 +01:00
|
|
|
/// TODO: move this to decoder (?)
|
2015-11-27 20:59:28 +01:00
|
|
|
fn item_info(bytes: &[u8]) -> Result<ItemInfo, DecoderError> {
|
|
|
|
let item = match bytes.first().map(|&x| x) {
|
2015-11-30 11:33:08 +01:00
|
|
|
None => return Err(DecoderError::RlpIsTooShort),
|
2015-11-27 20:59:28 +01:00
|
|
|
Some(0...0x7f) => ItemInfo::new(0, 1),
|
|
|
|
Some(l @ 0x80...0xb7) => ItemInfo::new(1, l as usize - 0x80),
|
|
|
|
Some(l @ 0xb8...0xbf) => {
|
|
|
|
let len_of_len = l as usize - 0xb7;
|
|
|
|
let prefix_len = 1 + len_of_len;
|
|
|
|
let value_len = try!(usize::from_bytes(&bytes[1..prefix_len]));
|
|
|
|
ItemInfo::new(prefix_len, value_len)
|
|
|
|
}
|
|
|
|
Some(l @ 0xc0...0xf7) => ItemInfo::new(1, l as usize - 0xc0),
|
|
|
|
Some(l @ 0xf8...0xff) => {
|
|
|
|
let len_of_len = l as usize - 0xf7;
|
|
|
|
let prefix_len = 1 + len_of_len;
|
|
|
|
let value_len = try!(usize::from_bytes(&bytes[1..prefix_len]));
|
|
|
|
ItemInfo::new(prefix_len, value_len)
|
|
|
|
}
|
2015-11-30 11:33:08 +01:00
|
|
|
_ => return Err(DecoderError::BadRlp),
|
2015-11-27 20:59:28 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
match item.prefix_len + item.value_len <= bytes.len() {
|
|
|
|
true => Ok(item),
|
2015-11-30 11:33:08 +01:00
|
|
|
false => Err(DecoderError::RlpIsTooShort),
|
2015-11-27 20:59:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// consumes slice prefix of length `len`
|
|
|
|
fn consume(bytes: &'a [u8], len: usize) -> Result<&'a [u8], DecoderError> {
|
|
|
|
match bytes.len() >= len {
|
|
|
|
true => Ok(&bytes[len..]),
|
2015-11-30 11:33:08 +01:00
|
|
|
false => Err(DecoderError::RlpIsTooShort),
|
2015-11-27 20:59:28 +01:00
|
|
|
}
|
|
|
|
}
|
2015-11-25 02:53:35 +01:00
|
|
|
}
|
|
|
|
|
2015-11-30 02:55:03 +01:00
|
|
|
/// Iterator over rlp-slice list elements.
|
2015-11-29 09:16:15 +01:00
|
|
|
pub struct UntrustedRlpIterator<'a> {
|
|
|
|
rlp: &'a UntrustedRlp<'a>,
|
2015-11-27 20:59:28 +01:00
|
|
|
index: usize,
|
2015-11-25 02:53:35 +01:00
|
|
|
}
|
|
|
|
|
2015-11-29 09:16:15 +01:00
|
|
|
impl<'a> IntoIterator for &'a UntrustedRlp<'a> {
|
|
|
|
type Item = UntrustedRlp<'a>;
|
2015-11-30 13:19:55 +01:00
|
|
|
type IntoIter = UntrustedRlpIterator<'a>;
|
2015-11-25 02:53:35 +01:00
|
|
|
|
2015-11-27 20:59:28 +01:00
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
2015-11-29 09:16:15 +01:00
|
|
|
UntrustedRlpIterator {
|
2015-11-27 20:59:28 +01:00
|
|
|
rlp: self,
|
|
|
|
index: 0,
|
|
|
|
}
|
|
|
|
}
|
2015-11-25 02:53:35 +01:00
|
|
|
}
|
|
|
|
|
2015-11-29 09:16:15 +01:00
|
|
|
impl<'a> Iterator for UntrustedRlpIterator<'a> {
|
|
|
|
type Item = UntrustedRlp<'a>;
|
2015-11-25 02:53:35 +01:00
|
|
|
|
2015-11-29 09:16:15 +01:00
|
|
|
fn next(&mut self) -> Option<UntrustedRlp<'a>> {
|
2015-11-27 20:59:28 +01:00
|
|
|
let index = self.index;
|
|
|
|
let result = self.rlp.at(index).ok();
|
|
|
|
self.index += 1;
|
|
|
|
result
|
|
|
|
}
|
2015-11-25 02:53:35 +01:00
|
|
|
}
|
|
|
|
|
2015-11-30 02:55:03 +01:00
|
|
|
/// Iterator over trusted rlp-slice list elements.
|
|
|
|
pub struct RlpIterator<'a> {
|
|
|
|
rlp: &'a Rlp<'a>,
|
|
|
|
index: usize
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> IntoIterator for &'a Rlp<'a> {
|
|
|
|
type Item = Rlp<'a>;
|
2015-11-30 13:19:55 +01:00
|
|
|
type IntoIter = RlpIterator<'a>;
|
2015-11-30 02:55:03 +01:00
|
|
|
|
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
|
|
|
RlpIterator {
|
|
|
|
rlp: self,
|
|
|
|
index: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Iterator for RlpIterator<'a> {
|
|
|
|
type Item = Rlp<'a>;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Rlp<'a>> {
|
|
|
|
let index = self.index;
|
|
|
|
let result = self.rlp.rlp.at(index).ok().map(| iter | { From::from(iter) });
|
|
|
|
self.index += 1;
|
|
|
|
result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Shortcut function to decode trusted rlp
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// extern crate ethcore_util as util;
|
|
|
|
/// use util::rlp::*;
|
|
|
|
///
|
|
|
|
/// fn main () {
|
|
|
|
/// let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g'];
|
|
|
|
/// let animals: Vec<String> = decode(&data);
|
|
|
|
/// assert_eq!(animals, vec!["cat".to_string(), "dog".to_string()]);
|
|
|
|
/// }
|
|
|
|
/// ```
|
2015-11-29 09:28:48 +01:00
|
|
|
pub fn decode<T>(bytes: &[u8]) -> T where T: Decodable {
|
|
|
|
let rlp = Rlp::new(bytes);
|
|
|
|
T::decode(&rlp)
|
|
|
|
}
|
|
|
|
|
2015-11-26 02:32:50 +01:00
|
|
|
pub trait Decodable: Sized {
|
2015-11-29 09:28:48 +01:00
|
|
|
fn decode_untrusted(rlp: &UntrustedRlp) -> Result<Self, DecoderError>;
|
|
|
|
fn decode(rlp: &Rlp) -> Self {
|
|
|
|
Self::decode_untrusted(&rlp.rlp).unwrap()
|
|
|
|
}
|
2015-11-26 02:32:50 +01:00
|
|
|
}
|
|
|
|
|
2015-11-29 19:35:22 +01:00
|
|
|
impl<T> Decodable for T where T: FromBytes {
|
2015-11-29 09:28:48 +01:00
|
|
|
fn decode_untrusted(rlp: &UntrustedRlp) -> Result<Self, DecoderError> {
|
2015-11-30 01:30:35 +01:00
|
|
|
match rlp.is_data() {
|
2015-11-29 19:35:22 +01:00
|
|
|
true => BasicDecoder::read_value(rlp.bytes, | bytes | {
|
|
|
|
Ok(try!(T::from_bytes(bytes)))
|
|
|
|
}),
|
2015-11-30 11:33:08 +01:00
|
|
|
false => Err(DecoderError::RlpExpectedToBeData),
|
2015-11-27 20:59:28 +01:00
|
|
|
}
|
|
|
|
}
|
2015-11-26 02:32:50 +01:00
|
|
|
}
|
|
|
|
|
2015-11-29 19:35:22 +01:00
|
|
|
impl<T> Decodable for Vec<T> where T: Decodable {
|
2015-11-29 09:28:48 +01:00
|
|
|
fn decode_untrusted(rlp: &UntrustedRlp) -> Result<Self, DecoderError> {
|
2015-11-27 20:59:28 +01:00
|
|
|
match rlp.is_list() {
|
2015-11-29 09:28:48 +01:00
|
|
|
true => rlp.iter().map(|rlp| T::decode_untrusted(&rlp)).collect(),
|
2015-11-30 11:33:08 +01:00
|
|
|
false => Err(DecoderError::RlpExpectedToBeList),
|
2015-11-27 20:59:28 +01:00
|
|
|
}
|
|
|
|
}
|
2015-11-26 02:32:50 +01:00
|
|
|
}
|
|
|
|
|
2015-11-29 19:35:22 +01:00
|
|
|
impl Decodable for Vec<u8> {
|
|
|
|
fn decode_untrusted(rlp: &UntrustedRlp) -> Result<Self, DecoderError> {
|
2015-11-30 01:30:35 +01:00
|
|
|
match rlp.is_data() {
|
2015-11-29 19:35:22 +01:00
|
|
|
true => BasicDecoder::read_value(rlp.bytes, | bytes | {
|
|
|
|
let mut res = vec![];
|
|
|
|
res.extend(bytes);
|
|
|
|
Ok(res)
|
|
|
|
}),
|
2015-11-30 11:33:08 +01:00
|
|
|
false => Err(DecoderError::RlpExpectedToBeData),
|
2015-11-29 19:35:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-26 02:50:21 +01:00
|
|
|
pub trait Decoder {
|
2015-11-29 19:35:22 +01:00
|
|
|
fn read_value<T, F>(bytes: &[u8], f: F) -> Result<T, DecoderError> where F: FnOnce(&[u8]) -> Result<T, DecoderError>;
|
2015-11-26 02:32:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
struct BasicDecoder;
|
|
|
|
|
|
|
|
impl Decoder for BasicDecoder {
|
2015-11-29 19:35:22 +01:00
|
|
|
fn read_value<T, F>(bytes: &[u8], f: F) -> Result<T, DecoderError> where F: FnOnce(&[u8]) -> Result<T, DecoderError> {
|
2015-11-27 20:59:28 +01:00
|
|
|
match bytes.first().map(|&x| x) {
|
|
|
|
// rlp is too short
|
2015-11-30 11:33:08 +01:00
|
|
|
None => Err(DecoderError::RlpIsTooShort),
|
2015-11-27 20:59:28 +01:00
|
|
|
// single byt value
|
2015-11-29 19:35:22 +01:00
|
|
|
Some(l @ 0...0x7f) => Ok(try!(f(&[l]))),
|
2015-11-27 20:59:28 +01:00
|
|
|
// 0-55 bytes
|
2015-11-29 19:35:22 +01:00
|
|
|
Some(l @ 0x80...0xb7) => Ok(try!(f(&bytes[1..(1 + l as usize - 0x80)]))),
|
2015-11-27 20:59:28 +01:00
|
|
|
// longer than 55 bytes
|
|
|
|
Some(l @ 0xb8...0xbf) => {
|
|
|
|
let len_of_len = l as usize - 0xb7;
|
|
|
|
let begin_of_value = 1 as usize + len_of_len;
|
|
|
|
let len = try!(usize::from_bytes(&bytes[1..begin_of_value]));
|
2015-11-29 19:35:22 +01:00
|
|
|
Ok(try!(f(&bytes[begin_of_value..begin_of_value + len])))
|
2015-11-27 20:59:28 +01:00
|
|
|
}
|
2015-11-30 11:33:08 +01:00
|
|
|
_ => Err(DecoderError::BadRlp),
|
2015-11-27 20:59:28 +01:00
|
|
|
}
|
|
|
|
}
|
2015-11-26 02:32:50 +01:00
|
|
|
}
|
|
|
|
|
2015-11-25 23:40:01 +01:00
|
|
|
#[derive(Debug)]
|
|
|
|
struct ListInfo {
|
2015-11-27 20:59:28 +01:00
|
|
|
position: usize,
|
|
|
|
current: usize,
|
|
|
|
max: usize,
|
2015-11-25 23:40:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ListInfo {
|
2015-11-27 20:59:28 +01:00
|
|
|
fn new(position: usize, max: usize) -> ListInfo {
|
|
|
|
ListInfo {
|
|
|
|
position: position,
|
|
|
|
current: 0,
|
|
|
|
max: max,
|
|
|
|
}
|
|
|
|
}
|
2015-11-25 23:40:01 +01:00
|
|
|
}
|
|
|
|
|
2015-11-30 02:55:03 +01:00
|
|
|
/// Appendable rlp encoder.
|
2015-11-25 17:54:50 +01:00
|
|
|
pub struct RlpStream {
|
2015-11-27 20:59:28 +01:00
|
|
|
unfinished_lists: LinkedList<ListInfo>,
|
|
|
|
encoder: BasicEncoder,
|
2015-11-25 17:54:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl RlpStream {
|
2015-11-30 02:55:03 +01:00
|
|
|
/// Initializes instance of empty `RlpStream`.
|
2015-11-27 20:59:28 +01:00
|
|
|
pub fn new() -> RlpStream {
|
|
|
|
RlpStream {
|
|
|
|
unfinished_lists: LinkedList::new(),
|
|
|
|
encoder: BasicEncoder::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-30 02:55:03 +01:00
|
|
|
/// Initializes the `RLPStream` as a list.
|
2015-11-27 20:59:28 +01:00
|
|
|
pub fn new_list(len: usize) -> RlpStream {
|
|
|
|
let mut stream = RlpStream::new();
|
|
|
|
stream.append_list(len);
|
|
|
|
stream
|
|
|
|
}
|
|
|
|
|
2015-11-29 15:41:29 +01:00
|
|
|
/// Apends value to the end of stream, chainable.
|
2015-11-30 02:55:03 +01:00
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// extern crate ethcore_util as util;
|
|
|
|
/// use util::rlp::*;
|
|
|
|
///
|
|
|
|
/// fn main () {
|
|
|
|
/// let mut stream = RlpStream::new_list(2);
|
|
|
|
/// stream.append(&"cat").append(&"dog");
|
2015-11-30 11:24:03 +01:00
|
|
|
/// let out = stream.out();
|
2015-11-30 02:55:03 +01:00
|
|
|
/// assert_eq!(out, vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g']);
|
|
|
|
/// }
|
|
|
|
/// ```
|
2015-11-30 00:28:03 +01:00
|
|
|
pub fn append<'a, E>(&'a mut self, object: &E) -> &'a mut RlpStream where E: Encodable + fmt::Debug {
|
2015-11-27 20:59:28 +01:00
|
|
|
// encode given value and add it at the end of the stream
|
|
|
|
object.encode(&mut self.encoder);
|
|
|
|
|
|
|
|
// if list is finished, prepend the length
|
2015-11-30 11:29:30 +01:00
|
|
|
self.note_appended(1);
|
2015-11-27 20:59:28 +01:00
|
|
|
|
|
|
|
// return chainable self
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2015-11-29 15:41:29 +01:00
|
|
|
/// Declare appending the list of given size, chainable.
|
2015-11-30 02:55:03 +01:00
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// extern crate ethcore_util as util;
|
|
|
|
/// use util::rlp::*;
|
|
|
|
///
|
|
|
|
/// fn main () {
|
|
|
|
/// let mut stream = RlpStream::new_list(2);
|
|
|
|
/// stream.append_list(2).append(&"cat").append(&"dog");
|
|
|
|
/// stream.append(&"");
|
2015-11-30 11:24:03 +01:00
|
|
|
/// let out = stream.out();
|
2015-11-30 02:55:03 +01:00
|
|
|
/// assert_eq!(out, vec![0xca, 0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g', 0x80]);
|
|
|
|
/// }
|
|
|
|
/// ```
|
2015-11-27 20:59:28 +01:00
|
|
|
pub fn append_list<'a>(&'a mut self, len: usize) -> &'a mut RlpStream {
|
|
|
|
// push new list
|
|
|
|
let position = self.encoder.bytes.len();
|
|
|
|
match len {
|
|
|
|
0 => {
|
|
|
|
// we may finish, if the appended list len is equal 0
|
|
|
|
self.encoder.bytes.push(0xc0u8);
|
2015-11-30 11:29:30 +01:00
|
|
|
self.note_appended(1);
|
2015-11-27 20:59:28 +01:00
|
|
|
}
|
|
|
|
_ => self.unfinished_lists.push_back(ListInfo::new(position, len)),
|
|
|
|
}
|
|
|
|
|
|
|
|
// return chainable self
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2015-11-30 11:16:50 +01:00
|
|
|
/// Apends null to the end of stream, chainable.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// extern crate ethcore_util as util;
|
|
|
|
/// use util::rlp::*;
|
|
|
|
///
|
|
|
|
/// fn main () {
|
|
|
|
/// let mut stream = RlpStream::new_list(2);
|
2015-11-30 12:58:23 +01:00
|
|
|
/// stream.append_empty_data().append_empty_data();
|
2015-11-30 11:24:03 +01:00
|
|
|
/// let out = stream.out();
|
2015-11-30 11:16:50 +01:00
|
|
|
/// assert_eq!(out, vec![0xc2, 0x80, 0x80]);
|
|
|
|
/// }
|
|
|
|
/// ```
|
2015-11-30 12:58:23 +01:00
|
|
|
pub fn append_empty_data<'a>(&'a mut self) -> &'a mut RlpStream {
|
2015-11-30 11:16:50 +01:00
|
|
|
// self push raw item
|
|
|
|
self.encoder.bytes.push(0x80);
|
|
|
|
|
|
|
|
// try to finish and prepend the length
|
2015-11-30 11:29:30 +01:00
|
|
|
self.note_appended(1);
|
2015-11-30 11:16:50 +01:00
|
|
|
|
|
|
|
// return chainable self
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2015-11-29 15:41:29 +01:00
|
|
|
/// Appends raw (pre-serialised) RLP data. Use with caution. Chainable.
|
2015-11-29 18:44:29 +01:00
|
|
|
pub fn append_raw<'a>(&'a mut self, bytes: &[u8], item_count: usize) -> &'a mut RlpStream {
|
2015-11-29 15:41:29 +01:00
|
|
|
// push raw items
|
|
|
|
self.encoder.bytes.extend(bytes);
|
|
|
|
|
|
|
|
// try to finish and prepend the length
|
2015-11-30 11:29:30 +01:00
|
|
|
self.note_appended(item_count);
|
2015-11-29 15:41:29 +01:00
|
|
|
|
|
|
|
// return chainable self
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2015-11-30 11:42:54 +01:00
|
|
|
/// Clear the output stream so far.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// extern crate ethcore_util as util;
|
|
|
|
/// use util::rlp::*;
|
|
|
|
///
|
|
|
|
/// fn main () {
|
|
|
|
/// let mut stream = RlpStream::new_list(3);
|
|
|
|
/// stream.append(&"cat");
|
|
|
|
/// stream.clear();
|
|
|
|
/// stream.append(&"dog");
|
|
|
|
/// let out = stream.out();
|
|
|
|
/// assert_eq!(out, vec![0x83, b'd', b'o', b'g']);
|
|
|
|
/// }
|
|
|
|
pub fn clear(&mut self) {
|
|
|
|
// clear bytes
|
|
|
|
self.encoder.bytes.clear();
|
|
|
|
|
|
|
|
// clear lists
|
|
|
|
self.unfinished_lists.clear();
|
|
|
|
}
|
|
|
|
|
2015-11-30 02:55:03 +01:00
|
|
|
/// Returns true if stream doesnt expect any more items.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// extern crate ethcore_util as util;
|
|
|
|
/// use util::rlp::*;
|
|
|
|
///
|
|
|
|
/// fn main () {
|
|
|
|
/// let mut stream = RlpStream::new_list(2);
|
|
|
|
/// stream.append(&"cat");
|
|
|
|
/// assert_eq!(stream.is_finished(), false);
|
|
|
|
/// stream.append(&"dog");
|
|
|
|
/// assert_eq!(stream.is_finished(), true);
|
2015-11-30 11:24:03 +01:00
|
|
|
/// let out = stream.out();
|
2015-11-30 02:55:03 +01:00
|
|
|
/// assert_eq!(out, vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g']);
|
|
|
|
/// }
|
2015-11-27 20:59:28 +01:00
|
|
|
pub fn is_finished(&self) -> bool {
|
|
|
|
self.unfinished_lists.back().is_none()
|
|
|
|
}
|
|
|
|
|
2015-11-30 02:55:03 +01:00
|
|
|
/// Streams out encoded bytes.
|
|
|
|
///
|
2015-11-30 11:29:30 +01:00
|
|
|
/// panic! if stream is not finished.
|
2015-11-30 11:24:03 +01:00
|
|
|
pub fn out(self) -> Vec<u8> {
|
2015-11-27 20:59:28 +01:00
|
|
|
match self.is_finished() {
|
2015-11-30 11:24:03 +01:00
|
|
|
true => self.encoder.out(),
|
|
|
|
false => panic!()
|
2015-11-27 20:59:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-30 11:29:30 +01:00
|
|
|
/// Try to finish lists
|
|
|
|
fn note_appended(&mut self, inserted_items: usize) -> () {
|
2015-11-27 20:59:28 +01:00
|
|
|
let should_finish = match self.unfinished_lists.back_mut() {
|
|
|
|
None => false,
|
|
|
|
Some(ref mut x) => {
|
2015-11-29 15:41:29 +01:00
|
|
|
x.current += inserted_items;
|
|
|
|
if x.current > x.max {
|
|
|
|
panic!("You cannot append more items then you expect!");
|
|
|
|
}
|
2015-11-27 20:59:28 +01:00
|
|
|
x.current == x.max
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if should_finish {
|
|
|
|
let x = self.unfinished_lists.pop_back().unwrap();
|
|
|
|
let len = self.encoder.bytes.len() - x.position;
|
|
|
|
self.encoder.insert_list_len_at_pos(len, x.position);
|
2015-11-30 11:29:30 +01:00
|
|
|
self.note_appended(1);
|
2015-11-27 20:59:28 +01:00
|
|
|
}
|
|
|
|
}
|
2015-11-25 17:54:50 +01:00
|
|
|
}
|
|
|
|
|
2015-11-30 02:55:03 +01:00
|
|
|
/// Shortcut function to encode structure into rlp.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// extern crate ethcore_util as util;
|
|
|
|
/// use util::rlp::*;
|
|
|
|
///
|
|
|
|
/// fn main () {
|
|
|
|
/// let animals = vec!["cat", "dog"];
|
|
|
|
/// let out = encode(&animals);
|
|
|
|
/// assert_eq!(out, vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g']);
|
|
|
|
/// }
|
|
|
|
/// ```
|
2015-11-29 19:35:22 +01:00
|
|
|
pub fn encode<E>(object: &E) -> Vec<u8> where E: Encodable
|
2015-11-27 20:59:28 +01:00
|
|
|
{
|
|
|
|
let mut encoder = BasicEncoder::new();
|
|
|
|
object.encode(&mut encoder);
|
|
|
|
encoder.out()
|
2015-11-25 12:46:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub trait Encodable {
|
2015-11-27 20:59:28 +01:00
|
|
|
fn encode<E>(&self, encoder: &mut E) -> () where E: Encoder;
|
2015-11-25 12:46:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub trait Encoder {
|
2015-11-27 20:59:28 +01:00
|
|
|
fn emit_value(&mut self, bytes: &[u8]) -> ();
|
|
|
|
fn emit_list<F>(&mut self, f: F) -> () where F: FnOnce(&mut Self) -> ();
|
|
|
|
}
|
|
|
|
|
2015-11-29 19:35:22 +01:00
|
|
|
impl<T> Encodable for T where T: ToBytes {
|
|
|
|
fn encode<E>(&self, encoder: &mut E) -> () where E: Encoder {
|
2015-11-27 20:59:28 +01:00
|
|
|
encoder.emit_value(&self.to_bytes())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-29 19:35:22 +01:00
|
|
|
impl<'a, T> Encodable for &'a [T] where T: Encodable + 'a {
|
|
|
|
fn encode<E>(&self, encoder: &mut E) -> () where E: Encoder {
|
2015-11-27 20:59:28 +01:00
|
|
|
encoder.emit_list(|e| {
|
|
|
|
// insert all list elements
|
|
|
|
for el in self.iter() {
|
|
|
|
el.encode(e);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-29 19:35:22 +01:00
|
|
|
impl<T> Encodable for Vec<T> where T: Encodable {
|
|
|
|
fn encode<E>(&self, encoder: &mut E) -> () where E: Encoder {
|
2015-11-27 20:59:28 +01:00
|
|
|
let r: &[T] = self.as_ref();
|
|
|
|
r.encode(encoder)
|
|
|
|
}
|
2015-11-25 12:46:27 +01:00
|
|
|
}
|
|
|
|
|
2015-11-29 19:35:22 +01:00
|
|
|
/// lets treat bytes differently than other lists
|
|
|
|
/// they are a single value
|
|
|
|
impl<'a> Encodable for &'a [u8] {
|
|
|
|
fn encode<E>(&self, encoder: &mut E) -> () where E: Encoder {
|
|
|
|
encoder.emit_value(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// lets treat bytes differently than other lists
|
|
|
|
/// they are a single value
|
|
|
|
impl Encodable for Vec<u8> {
|
|
|
|
fn encode<E>(&self, encoder: &mut E) -> () where E: Encoder {
|
|
|
|
encoder.emit_value(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-25 21:04:43 +01:00
|
|
|
struct BasicEncoder {
|
2015-11-27 20:59:28 +01:00
|
|
|
bytes: Vec<u8>,
|
2015-11-25 12:46:27 +01:00
|
|
|
}
|
|
|
|
|
2015-11-25 21:04:43 +01:00
|
|
|
impl BasicEncoder {
|
2015-11-27 20:59:28 +01:00
|
|
|
fn new() -> BasicEncoder {
|
|
|
|
BasicEncoder { bytes: vec![] }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// inserts list prefix at given position
|
|
|
|
/// TODO: optimise it further?
|
|
|
|
fn insert_list_len_at_pos(&mut self, len: usize, pos: usize) -> () {
|
|
|
|
let mut res = vec![];
|
|
|
|
match len {
|
|
|
|
0...55 => res.push(0xc0u8 + len as u8),
|
|
|
|
_ => {
|
2015-11-30 00:28:03 +01:00
|
|
|
res.push(0xf7u8 + len.to_bytes_len() as u8);
|
2015-11-27 20:59:28 +01:00
|
|
|
res.extend(len.to_bytes());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
self.bytes.insert_slice(pos, &res);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// get encoded value
|
|
|
|
fn out(self) -> Vec<u8> {
|
|
|
|
self.bytes
|
|
|
|
}
|
2015-11-25 12:46:27 +01:00
|
|
|
}
|
|
|
|
|
2015-11-25 21:04:43 +01:00
|
|
|
impl Encoder for BasicEncoder {
|
2015-11-27 20:59:28 +01:00
|
|
|
fn emit_value(&mut self, bytes: &[u8]) -> () {
|
|
|
|
match bytes.len() {
|
|
|
|
// just 0
|
|
|
|
0 => self.bytes.push(0x80u8),
|
|
|
|
// byte is its own encoding
|
|
|
|
1 if bytes[0] < 0x80 => self.bytes.extend(bytes),
|
|
|
|
// (prefix + length), followed by the string
|
|
|
|
len @ 1 ... 55 => {
|
|
|
|
self.bytes.push(0x80u8 + len as u8);
|
|
|
|
self.bytes.extend(bytes);
|
|
|
|
}
|
|
|
|
// (prefix + length of length), followed by the length, followd by the string
|
|
|
|
len => {
|
|
|
|
self.bytes.push(0xb7 + len.to_bytes_len() as u8);
|
|
|
|
self.bytes.extend(len.to_bytes());
|
|
|
|
self.bytes.extend(bytes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-30 00:28:03 +01:00
|
|
|
fn emit_list<F>(&mut self, f: F) -> () where F: FnOnce(&mut Self) -> ()
|
2015-11-27 20:59:28 +01:00
|
|
|
{
|
|
|
|
// get len before inserting a list
|
|
|
|
let before_len = self.bytes.len();
|
|
|
|
|
|
|
|
// insert all list elements
|
|
|
|
f(self);
|
|
|
|
|
|
|
|
// get len after inserting a list
|
|
|
|
let after_len = self.bytes.len();
|
|
|
|
|
|
|
|
// diff is list len
|
|
|
|
let list_len = after_len - before_len;
|
|
|
|
self.insert_list_len_at_pos(list_len, before_len);
|
|
|
|
}
|
2015-11-25 12:46:27 +01:00
|
|
|
}
|
|
|
|
|
2015-11-25 02:53:35 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2015-11-27 20:59:28 +01:00
|
|
|
use std::{fmt, cmp};
|
|
|
|
use std::str::FromStr;
|
|
|
|
use rlp;
|
2015-11-29 09:16:15 +01:00
|
|
|
use rlp::{UntrustedRlp, RlpStream, Decodable};
|
2015-11-27 20:59:28 +01:00
|
|
|
use uint::U256;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn rlp_at() {
|
|
|
|
let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g'];
|
|
|
|
{
|
2015-11-29 09:16:15 +01:00
|
|
|
let rlp = UntrustedRlp::new(&data);
|
2015-11-27 20:59:28 +01:00
|
|
|
assert!(rlp.is_list());
|
2015-11-29 09:28:48 +01:00
|
|
|
let animals = <Vec<String> as rlp::Decodable>::decode_untrusted(&rlp).unwrap();
|
2015-11-27 20:59:28 +01:00
|
|
|
assert_eq!(animals, vec!["cat".to_string(), "dog".to_string()]);
|
|
|
|
|
|
|
|
let cat = rlp.at(0).unwrap();
|
2015-11-30 01:30:35 +01:00
|
|
|
assert!(cat.is_data());
|
2015-11-27 20:59:28 +01:00
|
|
|
assert_eq!(cat.bytes, &[0x83, b'c', b'a', b't']);
|
2015-11-29 09:28:48 +01:00
|
|
|
assert_eq!(String::decode_untrusted(&cat).unwrap(), "cat".to_string());
|
2015-11-27 20:59:28 +01:00
|
|
|
|
|
|
|
let dog = rlp.at(1).unwrap();
|
2015-11-30 01:30:35 +01:00
|
|
|
assert!(dog.is_data());
|
2015-11-27 20:59:28 +01:00
|
|
|
assert_eq!(dog.bytes, &[0x83, b'd', b'o', b'g']);
|
2015-11-29 09:28:48 +01:00
|
|
|
assert_eq!(String::decode_untrusted(&dog).unwrap(), "dog".to_string());
|
2015-11-27 20:59:28 +01:00
|
|
|
|
|
|
|
let cat_again = rlp.at(0).unwrap();
|
2015-11-30 01:30:35 +01:00
|
|
|
assert!(cat_again.is_data());
|
2015-11-27 20:59:28 +01:00
|
|
|
assert_eq!(cat_again.bytes, &[0x83, b'c', b'a', b't']);
|
2015-11-29 09:28:48 +01:00
|
|
|
assert_eq!(String::decode_untrusted(&cat_again).unwrap(), "cat".to_string());
|
2015-11-27 20:59:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn rlp_at_err() {
|
|
|
|
let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o'];
|
|
|
|
{
|
2015-11-29 09:16:15 +01:00
|
|
|
let rlp = UntrustedRlp::new(&data);
|
2015-11-27 20:59:28 +01:00
|
|
|
assert!(rlp.is_list());
|
|
|
|
|
|
|
|
let cat_err = rlp.at(0).unwrap_err();
|
2015-11-30 11:33:08 +01:00
|
|
|
assert_eq!(cat_err, rlp::DecoderError::RlpIsTooShort);
|
2015-11-27 20:59:28 +01:00
|
|
|
|
|
|
|
let dog_err = rlp.at(1).unwrap_err();
|
2015-11-30 11:33:08 +01:00
|
|
|
assert_eq!(dog_err, rlp::DecoderError::RlpIsTooShort);
|
2015-11-27 20:59:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn rlp_iter() {
|
|
|
|
let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g'];
|
|
|
|
{
|
2015-11-29 09:16:15 +01:00
|
|
|
let rlp = UntrustedRlp::new(&data);
|
2015-11-27 20:59:28 +01:00
|
|
|
let mut iter = rlp.iter();
|
|
|
|
|
|
|
|
let cat = iter.next().unwrap();
|
2015-11-30 01:30:35 +01:00
|
|
|
assert!(cat.is_data());
|
2015-11-27 20:59:28 +01:00
|
|
|
assert_eq!(cat.bytes, &[0x83, b'c', b'a', b't']);
|
|
|
|
|
|
|
|
let dog = iter.next().unwrap();
|
2015-11-30 01:30:35 +01:00
|
|
|
assert!(dog.is_data());
|
2015-11-27 20:59:28 +01:00
|
|
|
assert_eq!(dog.bytes, &[0x83, b'd', b'o', b'g']);
|
|
|
|
|
|
|
|
let none = iter.next();
|
|
|
|
assert!(none.is_none());
|
|
|
|
|
|
|
|
let cat_again = rlp.at(0).unwrap();
|
2015-11-30 01:30:35 +01:00
|
|
|
assert!(cat_again.is_data());
|
2015-11-27 20:59:28 +01:00
|
|
|
assert_eq!(cat_again.bytes, &[0x83, b'c', b'a', b't']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ETestPair<T>(T, Vec<u8>) where T: rlp::Encodable;
|
|
|
|
|
|
|
|
fn run_encode_tests<T>(tests: Vec<ETestPair<T>>)
|
|
|
|
where T: rlp::Encodable
|
|
|
|
{
|
|
|
|
for t in &tests {
|
|
|
|
let res = rlp::encode(&t.0);
|
|
|
|
assert_eq!(res, &t.1[..]);
|
|
|
|
}
|
|
|
|
}
|
2015-11-29 19:35:22 +01:00
|
|
|
|
2015-11-27 20:59:28 +01:00
|
|
|
#[test]
|
|
|
|
fn encode_u16() {
|
|
|
|
let tests = vec![
|
2015-11-30 13:19:55 +01:00
|
|
|
ETestPair(0u16, vec![0x80u8]),
|
|
|
|
ETestPair(0x100, vec![0x82, 0x01, 0x00]),
|
|
|
|
ETestPair(0xffff, vec![0x82, 0xff, 0xff]),
|
|
|
|
];
|
2015-11-27 20:59:28 +01:00
|
|
|
run_encode_tests(tests);
|
|
|
|
}
|
2015-11-25 13:13:26 +01:00
|
|
|
|
2015-11-27 20:59:28 +01:00
|
|
|
#[test]
|
|
|
|
fn encode_u32() {
|
|
|
|
let tests = vec![
|
2015-11-30 13:19:55 +01:00
|
|
|
ETestPair(0u32, vec![0x80u8]),
|
|
|
|
ETestPair(0x10000, vec![0x83, 0x01, 0x00, 0x00]),
|
|
|
|
ETestPair(0xffffff, vec![0x83, 0xff, 0xff, 0xff]),
|
|
|
|
];
|
2015-11-27 20:59:28 +01:00
|
|
|
run_encode_tests(tests);
|
|
|
|
}
|
2015-11-25 13:13:26 +01:00
|
|
|
|
2015-11-27 20:59:28 +01:00
|
|
|
#[test]
|
|
|
|
fn encode_u64() {
|
|
|
|
let tests = vec![
|
2015-11-30 13:19:55 +01:00
|
|
|
ETestPair(0u64, vec![0x80u8]),
|
|
|
|
ETestPair(0x1000000, vec![0x84, 0x01, 0x00, 0x00, 0x00]),
|
|
|
|
ETestPair(0xFFFFFFFF, vec![0x84, 0xff, 0xff, 0xff, 0xff]),
|
|
|
|
];
|
2015-11-27 20:59:28 +01:00
|
|
|
run_encode_tests(tests);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn encode_u256() {
|
|
|
|
let tests = vec![ETestPair(U256::from(0u64), vec![0x80u8]),
|
2015-11-30 13:19:55 +01:00
|
|
|
ETestPair(U256::from(0x1000000u64), vec![0x84, 0x01, 0x00, 0x00, 0x00]),
|
|
|
|
ETestPair(U256::from(0xffffffffu64),
|
|
|
|
vec![0x84, 0xff, 0xff, 0xff, 0xff]),
|
|
|
|
ETestPair(U256::from_str("8090a0b0c0d0e0f00910203040506077000000000000\
|
|
|
|
000100000000000012f0")
|
|
|
|
.unwrap(),
|
|
|
|
vec![0xa0, 0x80, 0x90, 0xa0, 0xb0, 0xc0, 0xd0, 0xe0, 0xf0,
|
|
|
|
0x09, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x77, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x12, 0xf0])];
|
2015-11-27 20:59:28 +01:00
|
|
|
run_encode_tests(tests);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn encode_str() {
|
|
|
|
let tests = vec![ETestPair("cat", vec![0x83, b'c', b'a', b't']),
|
2015-11-30 13:19:55 +01:00
|
|
|
ETestPair("dog", vec![0x83, b'd', b'o', b'g']),
|
|
|
|
ETestPair("Marek", vec![0x85, b'M', b'a', b'r', b'e', b'k']),
|
|
|
|
ETestPair("", vec![0x80]),
|
|
|
|
ETestPair("Lorem ipsum dolor sit amet, consectetur adipisicing elit",
|
|
|
|
vec![0xb8, 0x38, b'L', b'o', b'r', b'e', b'm', b' ', b'i',
|
|
|
|
b'p', b's', b'u', b'm', b' ', b'd', b'o', b'l', b'o',
|
|
|
|
b'r', b' ', b's', b'i', b't', b' ', b'a', b'm', b'e',
|
|
|
|
b't', b',', b' ', b'c', b'o', b'n', b's', b'e', b'c',
|
|
|
|
b't', b'e', b't', b'u', b'r', b' ', b'a', b'd', b'i',
|
|
|
|
b'p', b'i', b's', b'i', b'c', b'i', b'n', b'g', b' ',
|
|
|
|
b'e', b'l', b'i', b't'])];
|
2015-11-27 20:59:28 +01:00
|
|
|
run_encode_tests(tests);
|
|
|
|
}
|
|
|
|
|
2015-11-28 11:25:00 +01:00
|
|
|
#[test]
|
|
|
|
fn encode_address() {
|
|
|
|
use hash::*;
|
|
|
|
|
|
|
|
let tests = vec![
|
|
|
|
ETestPair(Address::from_str("ef2d6d194084c2de36e0dabfce45d046b37d1106").unwrap(),
|
|
|
|
vec![0x94, 0xef, 0x2d, 0x6d, 0x19, 0x40, 0x84, 0xc2, 0xde,
|
2015-11-30 13:19:55 +01:00
|
|
|
0x36, 0xe0, 0xda, 0xbf, 0xce, 0x45, 0xd0, 0x46,
|
|
|
|
0xb3, 0x7d, 0x11, 0x06])
|
2015-11-28 11:25:00 +01:00
|
|
|
];
|
|
|
|
run_encode_tests(tests);
|
|
|
|
}
|
|
|
|
|
2015-11-29 19:35:22 +01:00
|
|
|
/// Vec<u8> is treated as a single value
|
|
|
|
#[test]
|
|
|
|
fn encode_vector_u8() {
|
|
|
|
let tests = vec![
|
|
|
|
ETestPair(vec![], vec![0x80]),
|
|
|
|
ETestPair(vec![0u8], vec![0]),
|
|
|
|
ETestPair(vec![0x15], vec![0x15]),
|
|
|
|
ETestPair(vec![0x40, 0x00], vec![0x82, 0x40, 0x00]),
|
|
|
|
];
|
|
|
|
run_encode_tests(tests);
|
|
|
|
}
|
2015-11-25 13:13:26 +01:00
|
|
|
|
2015-11-27 20:59:28 +01:00
|
|
|
#[test]
|
|
|
|
fn encode_vector_u64() {
|
|
|
|
let tests = vec![
|
2015-11-30 13:19:55 +01:00
|
|
|
ETestPair(vec![], vec![0xc0]),
|
|
|
|
ETestPair(vec![15u64], vec![0xc1, 0x0f]),
|
|
|
|
ETestPair(vec![1, 2, 3, 7, 0xff], vec![0xc6, 1, 2, 3, 7, 0x81, 0xff]),
|
|
|
|
ETestPair(vec![0xffffffff, 1, 2, 3, 7, 0xff], vec![0xcb, 0x84, 0xff, 0xff, 0xff, 0xff, 1, 2, 3, 7, 0x81, 0xff]),
|
|
|
|
];
|
2015-11-27 20:59:28 +01:00
|
|
|
run_encode_tests(tests);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn encode_vector_str() {
|
|
|
|
let tests = vec![ETestPair(vec!["cat", "dog"],
|
2015-11-30 13:19:55 +01:00
|
|
|
vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g'])];
|
2015-11-27 20:59:28 +01:00
|
|
|
run_encode_tests(tests);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn encode_vector_of_vectors_str() {
|
|
|
|
let tests = vec![ETestPair(vec![vec!["cat"]], vec![0xc5, 0xc4, 0x83, b'c', b'a', b't'])];
|
|
|
|
run_encode_tests(tests);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2015-11-30 15:35:10 +01:00
|
|
|
fn encode_bytes() {
|
|
|
|
let vec = vec![0u8];
|
|
|
|
let slice: &[u8] = &vec;
|
|
|
|
let res = rlp::encode(&slice);
|
|
|
|
assert_eq!(res, vec![0u8]);
|
|
|
|
}
|
|
|
|
|
2015-11-27 20:59:28 +01:00
|
|
|
#[test]
|
|
|
|
fn rlp_stream() {
|
|
|
|
let mut stream = RlpStream::new_list(2);
|
|
|
|
stream.append(&"cat").append(&"dog");
|
2015-11-30 11:24:03 +01:00
|
|
|
let out = stream.out();
|
2015-11-27 20:59:28 +01:00
|
|
|
assert_eq!(out,
|
2015-11-30 13:19:55 +01:00
|
|
|
vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g']);
|
2015-11-27 20:59:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn rlp_stream_list() {
|
|
|
|
let mut stream = RlpStream::new_list(3);
|
|
|
|
stream.append_list(0);
|
|
|
|
stream.append_list(1).append_list(0);
|
|
|
|
stream.append_list(2).append_list(0).append_list(1).append_list(0);
|
2015-11-30 11:24:03 +01:00
|
|
|
let out = stream.out();
|
2015-11-27 20:59:28 +01:00
|
|
|
assert_eq!(out, vec![0xc7, 0xc0, 0xc1, 0xc0, 0xc3, 0xc0, 0xc1, 0xc0]);
|
|
|
|
}
|
|
|
|
|
2015-11-30 00:28:03 +01:00
|
|
|
#[test]
|
|
|
|
fn rlp_stream_list2() {
|
|
|
|
let mut stream = RlpStream::new();
|
|
|
|
stream.append_list(17);
|
|
|
|
for _ in 0..17 {
|
|
|
|
stream.append(&"");
|
|
|
|
}
|
2015-11-30 11:24:03 +01:00
|
|
|
let out = stream.out();
|
2015-11-30 00:28:03 +01:00
|
|
|
assert_eq!(out, vec![0xd1, 0x80, 0x80, 0x80, 0x80, 0x80,
|
2015-11-30 13:19:55 +01:00
|
|
|
0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
|
2015-11-30 00:28:03 +01:00
|
|
|
0x80, 0x80, 0x80, 0x80, 0x80, 0x80]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn rlp_stream_list3() {
|
|
|
|
let mut stream = RlpStream::new();
|
|
|
|
stream.append_list(17);
|
|
|
|
|
|
|
|
let mut res = vec![0xf8, 0x44];
|
|
|
|
for _ in 0..17 {
|
|
|
|
stream.append(&"aaa");
|
|
|
|
res.extend(vec![0x83, b'a', b'a', b'a']);
|
|
|
|
}
|
2015-11-30 11:24:03 +01:00
|
|
|
let out = stream.out();
|
2015-11-30 00:28:03 +01:00
|
|
|
assert_eq!(out, res);
|
2015-11-30 11:25:09 +01:00
|
|
|
}
|
2015-11-30 00:28:03 +01:00
|
|
|
|
2015-11-27 20:59:28 +01:00
|
|
|
struct DTestPair<T>(T, Vec<u8>) where T: rlp::Decodable + fmt::Debug + cmp::Eq;
|
|
|
|
|
2015-11-29 19:35:22 +01:00
|
|
|
fn run_decode_tests<T>(tests: Vec<DTestPair<T>>) where T: rlp::Decodable + fmt::Debug + cmp::Eq {
|
2015-11-27 20:59:28 +01:00
|
|
|
for t in &tests {
|
2015-11-30 02:55:03 +01:00
|
|
|
let res: T = rlp::decode(&t.1);
|
2015-11-27 20:59:28 +01:00
|
|
|
assert_eq!(res, t.0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-29 19:35:22 +01:00
|
|
|
/// Vec<u8> is treated as a single value
|
|
|
|
#[test]
|
|
|
|
fn decode_vector_u8() {
|
|
|
|
let tests = vec![
|
|
|
|
DTestPair(vec![], vec![0x80]),
|
|
|
|
DTestPair(vec![0u8], vec![0]),
|
|
|
|
DTestPair(vec![0x15], vec![0x15]),
|
|
|
|
DTestPair(vec![0x40, 0x00], vec![0x82, 0x40, 0x00]),
|
|
|
|
];
|
|
|
|
run_decode_tests(tests);
|
|
|
|
}
|
2015-11-26 02:32:50 +01:00
|
|
|
|
2015-11-27 20:59:28 +01:00
|
|
|
#[test]
|
2015-11-29 09:28:48 +01:00
|
|
|
fn decode_untrusted_u16() {
|
2015-11-27 20:59:28 +01:00
|
|
|
let tests = vec![
|
2015-11-30 13:19:55 +01:00
|
|
|
DTestPair(0u16, vec![0u8]),
|
|
|
|
DTestPair(0x100, vec![0x82, 0x01, 0x00]),
|
|
|
|
DTestPair(0xffff, vec![0x82, 0xff, 0xff]),
|
|
|
|
];
|
2015-11-29 19:35:22 +01:00
|
|
|
run_decode_tests(tests);
|
2015-11-27 20:59:28 +01:00
|
|
|
}
|
2015-11-26 02:32:50 +01:00
|
|
|
|
2015-11-27 20:59:28 +01:00
|
|
|
#[test]
|
2015-11-29 09:28:48 +01:00
|
|
|
fn decode_untrusted_u32() {
|
2015-11-27 20:59:28 +01:00
|
|
|
let tests = vec![
|
2015-11-30 13:19:55 +01:00
|
|
|
DTestPair(0u32, vec![0u8]),
|
|
|
|
DTestPair(0x10000, vec![0x83, 0x01, 0x00, 0x00]),
|
|
|
|
DTestPair(0xffffff, vec![0x83, 0xff, 0xff, 0xff]),
|
|
|
|
];
|
2015-11-29 19:35:22 +01:00
|
|
|
run_decode_tests(tests);
|
2015-11-27 20:59:28 +01:00
|
|
|
}
|
2015-11-26 02:32:50 +01:00
|
|
|
|
2015-11-27 20:59:28 +01:00
|
|
|
#[test]
|
2015-11-29 09:28:48 +01:00
|
|
|
fn decode_untrusted_u64() {
|
2015-11-27 20:59:28 +01:00
|
|
|
let tests = vec![
|
2015-11-30 13:19:55 +01:00
|
|
|
DTestPair(0u64, vec![0u8]),
|
|
|
|
DTestPair(0x1000000, vec![0x84, 0x01, 0x00, 0x00, 0x00]),
|
|
|
|
DTestPair(0xFFFFFFFF, vec![0x84, 0xff, 0xff, 0xff, 0xff]),
|
|
|
|
];
|
2015-11-29 19:35:22 +01:00
|
|
|
run_decode_tests(tests);
|
2015-11-27 20:59:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2015-11-29 09:28:48 +01:00
|
|
|
fn decode_untrusted_u256() {
|
2015-11-27 20:59:28 +01:00
|
|
|
let tests = vec![DTestPair(U256::from(0u64), vec![0x80u8]),
|
2015-11-30 13:19:55 +01:00
|
|
|
DTestPair(U256::from(0x1000000u64), vec![0x84, 0x01, 0x00, 0x00, 0x00]),
|
|
|
|
DTestPair(U256::from(0xffffffffu64),
|
|
|
|
vec![0x84, 0xff, 0xff, 0xff, 0xff]),
|
|
|
|
DTestPair(U256::from_str("8090a0b0c0d0e0f00910203040506077000000000000\
|
|
|
|
000100000000000012f0")
|
|
|
|
.unwrap(),
|
|
|
|
vec![0xa0, 0x80, 0x90, 0xa0, 0xb0, 0xc0, 0xd0, 0xe0, 0xf0,
|
|
|
|
0x09, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x77, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x12, 0xf0])];
|
2015-11-29 19:35:22 +01:00
|
|
|
run_decode_tests(tests);
|
2015-11-27 20:59:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2015-11-29 09:28:48 +01:00
|
|
|
fn decode_untrusted_str() {
|
2015-11-27 20:59:28 +01:00
|
|
|
let tests = vec![DTestPair("cat".to_string(), vec![0x83, b'c', b'a', b't']),
|
2015-11-30 13:19:55 +01:00
|
|
|
DTestPair("dog".to_string(), vec![0x83, b'd', b'o', b'g']),
|
|
|
|
DTestPair("Marek".to_string(),
|
|
|
|
vec![0x85, b'M', b'a', b'r', b'e', b'k']),
|
|
|
|
DTestPair("".to_string(), vec![0x80]),
|
|
|
|
DTestPair("Lorem ipsum dolor sit amet, consectetur adipisicing elit"
|
|
|
|
.to_string(),
|
|
|
|
vec![0xb8, 0x38, b'L', b'o', b'r', b'e', b'm', b' ', b'i',
|
|
|
|
b'p', b's', b'u', b'm', b' ', b'd', b'o', b'l', b'o',
|
|
|
|
b'r', b' ', b's', b'i', b't', b' ', b'a', b'm', b'e',
|
|
|
|
b't', b',', b' ', b'c', b'o', b'n', b's', b'e', b'c',
|
|
|
|
b't', b'e', b't', b'u', b'r', b' ', b'a', b'd', b'i',
|
|
|
|
b'p', b'i', b's', b'i', b'c', b'i', b'n', b'g', b' ',
|
|
|
|
b'e', b'l', b'i', b't'])];
|
2015-11-29 19:35:22 +01:00
|
|
|
run_decode_tests(tests);
|
2015-11-27 20:59:28 +01:00
|
|
|
}
|
|
|
|
|
2015-11-28 11:25:00 +01:00
|
|
|
#[test]
|
2015-11-29 09:28:48 +01:00
|
|
|
fn decode_untrusted_address() {
|
2015-11-28 11:25:00 +01:00
|
|
|
use hash::*;
|
|
|
|
|
|
|
|
let tests = vec![
|
|
|
|
DTestPair(Address::from_str("ef2d6d194084c2de36e0dabfce45d046b37d1106").unwrap(),
|
|
|
|
vec![0x94, 0xef, 0x2d, 0x6d, 0x19, 0x40, 0x84, 0xc2, 0xde,
|
2015-11-30 13:19:55 +01:00
|
|
|
0x36, 0xe0, 0xda, 0xbf, 0xce, 0x45, 0xd0, 0x46,
|
|
|
|
0xb3, 0x7d, 0x11, 0x06])
|
2015-11-28 11:25:00 +01:00
|
|
|
];
|
2015-11-29 19:35:22 +01:00
|
|
|
run_decode_tests(tests);
|
2015-11-28 11:25:00 +01:00
|
|
|
}
|
|
|
|
|
2015-11-27 20:59:28 +01:00
|
|
|
#[test]
|
2015-11-29 09:28:48 +01:00
|
|
|
fn decode_untrusted_vector_u64() {
|
2015-11-27 20:59:28 +01:00
|
|
|
let tests = vec![
|
2015-11-30 13:19:55 +01:00
|
|
|
DTestPair(vec![], vec![0xc0]),
|
|
|
|
DTestPair(vec![15u64], vec![0xc1, 0x0f]),
|
|
|
|
DTestPair(vec![1, 2, 3, 7, 0xff], vec![0xc6, 1, 2, 3, 7, 0x81, 0xff]),
|
|
|
|
DTestPair(vec![0xffffffff, 1, 2, 3, 7, 0xff], vec![0xcb, 0x84, 0xff, 0xff, 0xff, 0xff, 1, 2, 3, 7, 0x81, 0xff]),
|
|
|
|
];
|
2015-11-29 19:35:22 +01:00
|
|
|
run_decode_tests(tests);
|
2015-11-27 20:59:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2015-11-29 09:28:48 +01:00
|
|
|
fn decode_untrusted_vector_str() {
|
2015-11-27 20:59:28 +01:00
|
|
|
let tests = vec![DTestPair(vec!["cat".to_string(), "dog".to_string()],
|
2015-11-30 13:19:55 +01:00
|
|
|
vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g'])];
|
2015-11-29 19:35:22 +01:00
|
|
|
run_decode_tests(tests);
|
2015-11-27 20:59:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2015-11-29 09:28:48 +01:00
|
|
|
fn decode_untrusted_vector_of_vectors_str() {
|
2015-11-27 20:59:28 +01:00
|
|
|
let tests = vec![DTestPair(vec![vec!["cat".to_string()]],
|
2015-11-30 13:19:55 +01:00
|
|
|
vec![0xc5, 0xc4, 0x83, b'c', b'a', b't'])];
|
2015-11-29 19:35:22 +01:00
|
|
|
run_decode_tests(tests);
|
2015-11-27 20:59:28 +01:00
|
|
|
}
|
2015-11-30 17:23:52 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_view() {
|
|
|
|
struct View<'a> {
|
|
|
|
bytes: &'a [u8]
|
|
|
|
}
|
|
|
|
|
|
|
|
impl <'a, 'view> View<'a> where 'a: 'view {
|
|
|
|
fn new(bytes: &'a [u8]) -> View<'a> {
|
|
|
|
View {
|
|
|
|
bytes: bytes
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn offset(&'view self, len: usize) -> View<'a> {
|
|
|
|
View::new(&self.bytes[len..])
|
|
|
|
}
|
|
|
|
|
|
|
|
fn data(&'view self) -> &'a [u8] {
|
|
|
|
self.bytes
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let data = vec![0, 1, 2, 3];
|
|
|
|
let view = View::new(&data);
|
|
|
|
let _data_slice = view.offset(1).data();
|
|
|
|
}
|
2015-11-25 02:53:35 +01:00
|
|
|
}
|