2016-01-27 12:14:57 +01:00
|
|
|
use std::ops::Deref;
|
2015-12-08 12:33:05 +01:00
|
|
|
use elastic_array::*;
|
2016-01-29 13:59:29 +01:00
|
|
|
use rlp::bytes::{ToBytes, VecLike};
|
2015-12-08 12:59:19 +01:00
|
|
|
use rlp::{Stream, Encoder, Encodable};
|
2016-01-28 20:13:05 +01:00
|
|
|
use rlp::rlptraits::{ByteEncodable, RlpEncodable};
|
2015-12-08 12:33:05 +01:00
|
|
|
|
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
|
|
struct ListInfo {
|
|
|
|
position: usize,
|
|
|
|
current: usize,
|
|
|
|
max: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ListInfo {
|
|
|
|
fn new(position: usize, max: usize) -> ListInfo {
|
|
|
|
ListInfo {
|
|
|
|
position: position,
|
|
|
|
current: 0,
|
|
|
|
max: max,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Appendable rlp encoder.
|
|
|
|
pub struct RlpStream {
|
|
|
|
unfinished_lists: ElasticArray16<ListInfo>,
|
|
|
|
encoder: BasicEncoder,
|
2016-01-27 19:27:11 +01:00
|
|
|
finished_list: bool,
|
2015-12-08 12:33:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Stream for RlpStream {
|
2015-12-22 22:23:01 +01:00
|
|
|
fn new() -> Self {
|
2015-12-08 12:33:05 +01:00
|
|
|
RlpStream {
|
|
|
|
unfinished_lists: ElasticArray16::new(),
|
|
|
|
encoder: BasicEncoder::new(),
|
2016-01-27 19:27:11 +01:00
|
|
|
finished_list: false,
|
2015-12-08 12:33:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn new_list(len: usize) -> Self {
|
|
|
|
let mut stream = RlpStream::new();
|
2016-01-27 12:14:57 +01:00
|
|
|
stream.begin_list(len);
|
2015-12-08 12:33:05 +01:00
|
|
|
stream
|
|
|
|
}
|
|
|
|
|
2016-01-28 20:13:05 +01:00
|
|
|
fn append<'a, E>(&'a mut self, value: &E) -> &'a mut Self where E: RlpEncodable {
|
2016-01-27 19:27:11 +01:00
|
|
|
self.finished_list = false;
|
2016-01-27 12:14:57 +01:00
|
|
|
value.rlp_append(self);
|
2016-01-27 19:27:11 +01:00
|
|
|
if !self.finished_list {
|
2016-01-27 18:58:41 +01:00
|
|
|
self.note_appended(1);
|
|
|
|
}
|
2015-12-08 12:33:05 +01:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2016-01-27 12:14:57 +01:00
|
|
|
fn begin_list(&mut self, len: usize) -> &mut RlpStream {
|
2016-01-27 19:27:11 +01:00
|
|
|
self.finished_list = false;
|
2015-12-08 12:33:05 +01:00
|
|
|
match len {
|
|
|
|
0 => {
|
|
|
|
// we may finish, if the appended list len is equal 0
|
|
|
|
self.encoder.bytes.push(0xc0u8);
|
|
|
|
self.note_appended(1);
|
2016-02-01 01:06:21 +01:00
|
|
|
self.finished_list = true;
|
2015-12-08 12:33:05 +01:00
|
|
|
},
|
2015-12-22 22:23:01 +01:00
|
|
|
_ => {
|
2015-12-08 12:33:05 +01:00
|
|
|
let position = self.encoder.bytes.len();
|
|
|
|
self.unfinished_lists.push(ListInfo::new(position, len));
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// return chainable self
|
|
|
|
self
|
|
|
|
}
|
2015-12-22 22:23:01 +01:00
|
|
|
|
2016-01-19 12:14:29 +01:00
|
|
|
fn append_empty_data(&mut self) -> &mut RlpStream {
|
2015-12-08 12:33:05 +01:00
|
|
|
// self push raw item
|
|
|
|
self.encoder.bytes.push(0x80);
|
|
|
|
|
|
|
|
// try to finish and prepend the length
|
|
|
|
self.note_appended(1);
|
|
|
|
|
|
|
|
// return chainable self
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
fn append_raw<'a>(&'a mut self, bytes: &[u8], item_count: usize) -> &'a mut RlpStream {
|
|
|
|
// push raw items
|
2015-12-22 22:23:01 +01:00
|
|
|
self.encoder.bytes.append_slice(bytes);
|
2015-12-08 12:33:05 +01:00
|
|
|
|
|
|
|
// try to finish and prepend the length
|
|
|
|
self.note_appended(item_count);
|
|
|
|
|
|
|
|
// return chainable self
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
fn clear(&mut self) {
|
|
|
|
// clear bytes
|
|
|
|
self.encoder.bytes.clear();
|
|
|
|
|
|
|
|
// clear lists
|
|
|
|
self.unfinished_lists.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_finished(&self) -> bool {
|
|
|
|
self.unfinished_lists.len() == 0
|
|
|
|
}
|
|
|
|
|
2016-01-08 15:57:50 +01:00
|
|
|
fn as_raw(&self) -> &[u8] {
|
2015-12-12 15:57:08 +01:00
|
|
|
&self.encoder.bytes
|
|
|
|
}
|
|
|
|
|
2015-12-08 12:33:05 +01:00
|
|
|
fn out(self) -> Vec<u8> {
|
|
|
|
match self.is_finished() {
|
|
|
|
true => self.encoder.out().to_vec(),
|
|
|
|
false => panic!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RlpStream {
|
|
|
|
|
2016-01-27 12:14:57 +01:00
|
|
|
/// Appends primitive value to the end of stream
|
|
|
|
fn append_value<E>(&mut self, object: &E) where E: ByteEncodable {
|
|
|
|
// encode given value and add it at the end of the stream
|
|
|
|
self.encoder.emit_value(object);
|
|
|
|
}
|
|
|
|
|
2016-01-28 20:13:05 +01:00
|
|
|
fn append_internal<'a, E>(&'a mut self, value: &E) -> &'a mut Self where E: Encodable {
|
|
|
|
self.finished_list = false;
|
|
|
|
value.rlp_append(self);
|
|
|
|
if !self.finished_list {
|
|
|
|
self.note_appended(1);
|
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2015-12-08 12:33:05 +01:00
|
|
|
/// Try to finish lists
|
|
|
|
fn note_appended(&mut self, inserted_items: usize) -> () {
|
|
|
|
if self.unfinished_lists.len() == 0 {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let back = self.unfinished_lists.len() - 1;
|
|
|
|
let should_finish = match self.unfinished_lists.get_mut(back) {
|
|
|
|
None => false,
|
|
|
|
Some(ref mut x) => {
|
|
|
|
x.current += inserted_items;
|
|
|
|
if x.current > x.max {
|
|
|
|
panic!("You cannot append more items then you expect!");
|
|
|
|
}
|
|
|
|
x.current == x.max
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if should_finish {
|
|
|
|
let x = self.unfinished_lists.pop().unwrap();
|
|
|
|
let len = self.encoder.bytes.len() - x.position;
|
|
|
|
self.encoder.insert_list_len_at_pos(len, x.position);
|
|
|
|
self.note_appended(1);
|
|
|
|
}
|
2016-01-27 19:27:11 +01:00
|
|
|
self.finished_list = should_finish;
|
2015-12-08 12:33:05 +01:00
|
|
|
}
|
2016-01-18 15:47:50 +01:00
|
|
|
|
|
|
|
/// Drain the object and return the underlying ElasticArray.
|
|
|
|
pub fn drain(self) -> ElasticArray1024<u8> {
|
|
|
|
match self.is_finished() {
|
|
|
|
true => self.encoder.bytes,
|
|
|
|
false => panic!()
|
|
|
|
}
|
|
|
|
}
|
2015-12-08 12:33:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
struct BasicEncoder {
|
|
|
|
bytes: ElasticArray1024<u8>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BasicEncoder {
|
|
|
|
fn new() -> BasicEncoder {
|
|
|
|
BasicEncoder { bytes: ElasticArray1024::new() }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// inserts list prefix at given position
|
|
|
|
/// TODO: optimise it further?
|
|
|
|
fn insert_list_len_at_pos(&mut self, len: usize, pos: usize) -> () {
|
2016-01-27 12:14:57 +01:00
|
|
|
let mut res = ElasticArray16::new();
|
2015-12-08 12:33:05 +01:00
|
|
|
match len {
|
|
|
|
0...55 => res.push(0xc0u8 + len as u8),
|
|
|
|
_ => {
|
|
|
|
res.push(0xf7u8 + len.to_bytes_len() as u8);
|
2016-01-27 12:14:57 +01:00
|
|
|
ToBytes::to_bytes(&len, &mut res);
|
2015-12-08 12:33:05 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
self.bytes.insert_slice(pos, &res);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// get encoded value
|
|
|
|
fn out(self) -> ElasticArray1024<u8> {
|
|
|
|
self.bytes
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Encoder for BasicEncoder {
|
2016-01-27 12:14:57 +01:00
|
|
|
fn emit_value<E: ByteEncodable>(&mut self, value: &E) {
|
|
|
|
match value.bytes_len() {
|
2015-12-08 12:33:05 +01:00
|
|
|
// just 0
|
|
|
|
0 => self.bytes.push(0x80u8),
|
2016-01-27 12:14:57 +01:00
|
|
|
// byte is its own encoding if < 0x80
|
|
|
|
1 => {
|
|
|
|
value.to_bytes(&mut self.bytes);
|
|
|
|
let len = self.bytes.len();
|
|
|
|
let last_byte = self.bytes[len - 1];
|
|
|
|
if last_byte >= 0x80 {
|
|
|
|
self.bytes.push(last_byte);
|
|
|
|
self.bytes[len - 1] = 0x81;
|
|
|
|
}
|
|
|
|
}
|
2015-12-08 12:33:05 +01:00
|
|
|
// (prefix + length), followed by the string
|
2016-01-27 12:14:57 +01:00
|
|
|
len @ 2 ... 55 => {
|
2015-12-08 12:33:05 +01:00
|
|
|
self.bytes.push(0x80u8 + len as u8);
|
2016-01-27 12:14:57 +01:00
|
|
|
value.to_bytes(&mut self.bytes);
|
2015-12-08 12:33:05 +01:00
|
|
|
}
|
|
|
|
// (prefix + length of length), followed by the length, followd by the string
|
|
|
|
len => {
|
|
|
|
self.bytes.push(0xb7 + len.to_bytes_len() as u8);
|
2016-01-27 12:14:57 +01:00
|
|
|
ToBytes::to_bytes(&len, &mut self.bytes);
|
|
|
|
value.to_bytes(&mut self.bytes);
|
2015-12-08 12:33:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-07 23:59:50 +01:00
|
|
|
fn emit_raw(&mut self, bytes: &[u8]) -> () {
|
|
|
|
self.bytes.append_slice(bytes);
|
|
|
|
}
|
2015-12-08 12:33:05 +01:00
|
|
|
}
|
|
|
|
|
2016-01-27 12:14:57 +01:00
|
|
|
impl<T> ByteEncodable for T where T: ToBytes {
|
|
|
|
fn to_bytes<V: VecLike<u8>>(&self, out: &mut V) {
|
|
|
|
ToBytes::to_bytes(self, out)
|
2016-01-09 23:47:46 +01:00
|
|
|
}
|
|
|
|
|
2016-01-27 12:14:57 +01:00
|
|
|
fn bytes_len(&self) -> usize {
|
|
|
|
ToBytes::to_bytes_len(self)
|
2015-12-08 12:33:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-28 20:13:05 +01:00
|
|
|
struct U8Slice<'a>(&'a [u8]);
|
|
|
|
|
|
|
|
impl<'a> ByteEncodable for U8Slice<'a> {
|
2016-01-27 12:14:57 +01:00
|
|
|
fn to_bytes<V: VecLike<u8>>(&self, out: &mut V) {
|
2016-01-28 20:13:05 +01:00
|
|
|
out.vec_extend(self.0)
|
2015-12-08 12:33:05 +01:00
|
|
|
}
|
|
|
|
|
2016-01-27 12:14:57 +01:00
|
|
|
fn bytes_len(&self) -> usize {
|
2016-01-28 20:13:05 +01:00
|
|
|
self.0.len()
|
2015-12-08 12:33:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-28 20:13:05 +01:00
|
|
|
impl<'a> Encodable for &'a[u8] {
|
|
|
|
fn rlp_append(&self, s: &mut RlpStream) {
|
|
|
|
s.append_value(&U8Slice(self))
|
2015-12-08 12:33:05 +01:00
|
|
|
}
|
2016-01-28 20:13:05 +01:00
|
|
|
}
|
2015-12-08 12:33:05 +01:00
|
|
|
|
2016-01-28 20:13:05 +01:00
|
|
|
impl Encodable for Vec<u8> {
|
|
|
|
fn rlp_append(&self, s: &mut RlpStream) {
|
|
|
|
s.append_value(&U8Slice(self.deref()))
|
2015-12-08 12:33:05 +01:00
|
|
|
}
|
|
|
|
}
|
2015-12-08 19:01:37 +01:00
|
|
|
|
2016-01-27 12:14:57 +01:00
|
|
|
impl<T> Encodable for T where T: ByteEncodable {
|
|
|
|
fn rlp_append(&self, s: &mut RlpStream) {
|
|
|
|
s.append_value(self)
|
2015-12-08 19:01:37 +01:00
|
|
|
}
|
|
|
|
}
|
2016-01-28 20:13:05 +01:00
|
|
|
|
|
|
|
struct EncodableU8 (u8);
|
|
|
|
|
|
|
|
impl ByteEncodable for EncodableU8 {
|
|
|
|
fn to_bytes<V: VecLike<u8>>(&self, out: &mut V) {
|
|
|
|
out.vec_push(self.0)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bytes_len(&self) -> usize { 1 }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RlpEncodable for u8 {
|
|
|
|
fn rlp_append(&self, s: &mut RlpStream) {
|
|
|
|
s.append_value(&EncodableU8(*self))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, T> Encodable for &'a[T] where T: Encodable {
|
|
|
|
fn rlp_append(&self, s: &mut RlpStream) {
|
|
|
|
s.begin_list(self.len());
|
|
|
|
for el in self.iter() {
|
|
|
|
s.append_internal(el);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Encodable for Vec<T> where T: Encodable {
|
|
|
|
fn rlp_append(&self, s: &mut RlpStream) {
|
|
|
|
Encodable::rlp_append(&self.deref(), s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> RlpEncodable for T where T: Encodable {
|
|
|
|
fn rlp_append(&self, s: &mut RlpStream) {
|
|
|
|
Encodable::rlp_append(self, s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|