2017-05-10 12:52:09 +02:00
|
|
|
// Copyright 2015-2017 Parity Technologies
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
2016-02-05 13:40:41 +01:00
|
|
|
|
2016-01-15 21:03:38 +01:00
|
|
|
use std::fmt;
|
2017-03-22 14:41:46 +01:00
|
|
|
use {UntrustedRlp, PayloadInfo, Prototype, Decodable};
|
2015-12-07 16:32:06 +01:00
|
|
|
|
2015-12-07 23:47:26 +01:00
|
|
|
impl<'a> From<UntrustedRlp<'a>> for Rlp<'a> {
|
|
|
|
fn from(rlp: UntrustedRlp<'a>) -> Rlp<'a> {
|
|
|
|
Rlp { rlp: rlp }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-07 16:32:06 +01:00
|
|
|
/// Data-oriented view onto trusted rlp-slice.
|
2016-02-16 10:42:56 +01:00
|
|
|
///
|
2015-12-07 16:32:06 +01:00
|
|
|
/// Unlikely to `UntrustedRlp` doesn't bother you with error
|
|
|
|
/// handling. It assumes that you know what you are doing.
|
2015-12-07 23:47:26 +01:00
|
|
|
#[derive(Debug)]
|
2015-12-07 16:32:06 +01:00
|
|
|
pub struct Rlp<'a> {
|
|
|
|
rlp: UntrustedRlp<'a>
|
|
|
|
}
|
|
|
|
|
2016-01-15 21:03:38 +01:00
|
|
|
impl<'a> fmt::Display for Rlp<'a> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
|
|
|
write!(f, "{}", self.rlp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-22 14:41:46 +01:00
|
|
|
impl<'a, 'view> Rlp<'a> where 'a: 'view {
|
2015-12-07 16:32:06 +01:00
|
|
|
/// Create a new instance of `Rlp`
|
2017-03-22 14:41:46 +01:00
|
|
|
pub fn new(bytes: &'a [u8]) -> Rlp<'a> {
|
2015-12-07 16:32:06 +01:00
|
|
|
Rlp {
|
|
|
|
rlp: UntrustedRlp::new(bytes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-22 14:41:46 +01:00
|
|
|
/// The raw data of the RLP as slice.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// extern crate rlp;
|
|
|
|
/// use 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 = rlp.at(1).as_raw();
|
|
|
|
/// assert_eq!(dog, &[0x83, b'd', b'o', b'g']);
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
pub fn as_raw(&'view self) -> &'a [u8] {
|
2016-01-08 15:52:43 +01:00
|
|
|
self.rlp.as_raw()
|
2015-12-07 16:32:06 +01:00
|
|
|
}
|
|
|
|
|
2017-03-22 14:41:46 +01:00
|
|
|
/// Get the prototype of the RLP.
|
|
|
|
pub fn prototype(&self) -> Prototype {
|
2015-12-07 23:47:26 +01:00
|
|
|
self.rlp.prototype().unwrap()
|
2015-12-07 16:32:06 +01:00
|
|
|
}
|
|
|
|
|
2017-03-22 14:41:46 +01:00
|
|
|
/// Get payload info.
|
|
|
|
pub fn payload_info(&self) -> PayloadInfo {
|
2015-12-07 23:47:26 +01:00
|
|
|
self.rlp.payload_info().unwrap()
|
2015-12-07 16:32:06 +01:00
|
|
|
}
|
|
|
|
|
2017-03-22 14:41:46 +01:00
|
|
|
/// Get underlieing data.
|
|
|
|
pub fn data(&'view self) -> &'a [u8] {
|
2015-12-07 23:47:26 +01:00
|
|
|
self.rlp.data().unwrap()
|
2015-12-07 16:32:06 +01:00
|
|
|
}
|
|
|
|
|
2017-03-22 14:41:46 +01:00
|
|
|
/// Returns number of RLP items.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// extern crate rlp;
|
|
|
|
/// use 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.item_count(), 2);
|
|
|
|
/// let view = rlp.at(1);
|
|
|
|
/// assert_eq!(view.item_count(), 0);
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
pub fn item_count(&self) -> usize {
|
|
|
|
self.rlp.item_count().unwrap_or(0)
|
2015-12-07 16:32:06 +01:00
|
|
|
}
|
|
|
|
|
2017-03-22 14:41:46 +01:00
|
|
|
/// Returns the number of bytes in the data, or zero if it isn't data.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// extern crate rlp;
|
|
|
|
/// use 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 {
|
2015-12-07 23:47:26 +01:00
|
|
|
self.rlp.size()
|
2015-12-07 16:32:06 +01:00
|
|
|
}
|
|
|
|
|
2017-03-22 14:41:46 +01:00
|
|
|
/// Get view onto RLP-slice at index.
|
|
|
|
///
|
|
|
|
/// Caches offset to given index, so access to successive
|
|
|
|
/// slices is faster.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// extern crate rlp;
|
|
|
|
/// use 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 = rlp.at(1).as_val();
|
|
|
|
/// assert_eq!(dog, "dog".to_string());
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
pub fn at(&'view self, index: usize) -> Rlp<'a> {
|
2015-12-07 23:47:26 +01:00
|
|
|
From::from(self.rlp.at(index).unwrap())
|
2015-12-07 16:32:06 +01:00
|
|
|
}
|
|
|
|
|
2017-03-22 14:41:46 +01:00
|
|
|
/// No value
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// extern crate rlp;
|
|
|
|
/// use rlp::*;
|
|
|
|
///
|
|
|
|
/// fn main () {
|
|
|
|
/// let data = vec![];
|
|
|
|
/// let rlp = Rlp::new(&data);
|
|
|
|
/// assert!(rlp.is_null());
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
pub fn is_null(&self) -> bool {
|
2015-12-07 23:47:26 +01:00
|
|
|
self.rlp.is_null()
|
2015-12-07 16:32:06 +01:00
|
|
|
}
|
|
|
|
|
2017-03-22 14:41:46 +01:00
|
|
|
/// Contains a zero-length string or zero-length list.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// extern crate rlp;
|
|
|
|
/// use rlp::*;
|
|
|
|
///
|
|
|
|
/// fn main () {
|
|
|
|
/// let data = vec![0xc0];
|
|
|
|
/// let rlp = Rlp::new(&data);
|
|
|
|
/// assert!(rlp.is_empty());
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
pub fn is_empty(&self) -> bool {
|
2015-12-07 23:47:26 +01:00
|
|
|
self.rlp.is_empty()
|
2015-12-07 16:32:06 +01:00
|
|
|
}
|
|
|
|
|
2017-03-22 14:41:46 +01:00
|
|
|
/// List value
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// extern crate rlp;
|
|
|
|
/// use 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());
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
pub fn is_list(&self) -> bool {
|
2015-12-07 23:47:26 +01:00
|
|
|
self.rlp.is_list()
|
2015-12-07 16:32:06 +01:00
|
|
|
}
|
|
|
|
|
2017-03-22 14:41:46 +01:00
|
|
|
/// String value
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// extern crate rlp;
|
|
|
|
/// use 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());
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
pub fn is_data(&self) -> bool {
|
2015-12-07 23:47:26 +01:00
|
|
|
self.rlp.is_data()
|
2015-12-07 16:32:06 +01:00
|
|
|
}
|
|
|
|
|
2017-03-22 14:41:46 +01:00
|
|
|
/// Int value
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// extern crate rlp;
|
|
|
|
/// use 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 {
|
2015-12-07 23:47:26 +01:00
|
|
|
self.rlp.is_int()
|
|
|
|
}
|
|
|
|
|
2017-03-22 14:41:46 +01:00
|
|
|
/// Get iterator over rlp-slices
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// extern crate rlp;
|
|
|
|
/// use 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 | i.as_val()).collect();
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
pub fn iter(&'view self) -> RlpIterator<'a, 'view> {
|
2015-12-07 23:47:26 +01:00
|
|
|
self.into_iter()
|
|
|
|
}
|
|
|
|
|
2017-03-22 14:41:46 +01:00
|
|
|
/// Decode data into an object
|
|
|
|
pub fn as_val<T>(&self) -> T where T: Decodable {
|
|
|
|
self.rlp.as_val().expect("Unexpected rlp error")
|
2015-12-07 23:47:26 +01:00
|
|
|
}
|
2015-12-08 14:44:22 +01:00
|
|
|
|
2017-03-22 14:41:46 +01:00
|
|
|
pub fn as_list<T>(&self) -> Vec<T> where T: Decodable {
|
|
|
|
self.iter().map(|rlp| rlp.as_val()).collect()
|
2015-12-07 23:47:26 +01:00
|
|
|
}
|
|
|
|
|
2017-03-22 14:41:46 +01:00
|
|
|
/// Decode data at given list index into an object
|
|
|
|
pub fn val_at<T>(&self, index: usize) -> T where T: Decodable {
|
|
|
|
self.at(index).as_val()
|
2015-12-08 14:44:22 +01:00
|
|
|
}
|
|
|
|
|
2017-03-22 14:41:46 +01:00
|
|
|
pub fn list_at<T>(&self, index: usize) -> Vec<T> where T: Decodable {
|
|
|
|
self.at(index).as_list()
|
2015-12-07 23:47:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Iterator over trusted rlp-slice list elements.
|
|
|
|
pub struct RlpIterator<'a, 'view> where 'a: 'view {
|
|
|
|
rlp: &'view Rlp<'a>,
|
|
|
|
index: usize
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'view> IntoIterator for &'view Rlp<'a> where 'a: 'view {
|
|
|
|
type Item = Rlp<'a>;
|
|
|
|
type IntoIter = RlpIterator<'a, 'view>;
|
|
|
|
|
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
|
|
|
RlpIterator {
|
|
|
|
rlp: self,
|
|
|
|
index: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'view> Iterator for RlpIterator<'a, 'view> {
|
|
|
|
type Item = Rlp<'a>;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Rlp<'a>> {
|
|
|
|
let index = self.index;
|
2016-02-16 10:42:56 +01:00
|
|
|
let result = self.rlp.rlp.at(index).ok().map(From::from);
|
2015-12-07 23:47:26 +01:00
|
|
|
self.index += 1;
|
|
|
|
result
|
2015-12-07 16:32:06 +01:00
|
|
|
}
|
|
|
|
}
|
2016-01-15 21:25:55 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn break_it() {
|
2017-07-06 11:26:14 +02:00
|
|
|
use rustc_hex::FromHex;
|
2016-12-23 16:53:06 +01:00
|
|
|
use bigint::prelude::U256;
|
2016-09-01 13:36:32 +02:00
|
|
|
|
|
|
|
let h: Vec<u8> = FromHex::from_hex("f84d0589010efbef67941f79b2a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a0c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470").unwrap();
|
2016-01-15 21:25:55 +01:00
|
|
|
let r: Rlp = Rlp::new(&h);
|
|
|
|
let u: U256 = r.val_at(1);
|
2016-01-16 16:44:47 +01:00
|
|
|
assert_eq!(format!("{}", u), "19526463837540678066");
|
2016-01-15 21:25:55 +01:00
|
|
|
}
|