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-09-01 13:36:32 +02:00
|
|
|
//! Recursive Length Prefix serialization crate.
|
2015-12-26 15:48:41 +01:00
|
|
|
//!
|
|
|
|
//! Allows encoding, decoding, and view onto rlp-slice
|
2015-12-08 00:07:07 +01:00
|
|
|
//!
|
|
|
|
//!# What should you use when?
|
|
|
|
//!
|
2015-12-26 15:48:41 +01:00
|
|
|
//!### Use `encode` function when:
|
2015-12-08 00:07:07 +01:00
|
|
|
//! * You want to encode something inline.
|
|
|
|
//! * You do not work on big set of data.
|
|
|
|
//! * You want to encode whole data structure at once.
|
|
|
|
//!
|
|
|
|
//!### 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.
|
|
|
|
//!
|
|
|
|
//!### Use `RlpStream` when:
|
|
|
|
//! * You want to encode something in portions.
|
|
|
|
//! * You encode a big set of data.
|
|
|
|
//!
|
|
|
|
//!### 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-12-26 15:48:41 +01:00
|
|
|
//!### Use `UntrustedRlp` when:
|
2015-12-08 00:07:07 +01:00
|
|
|
//! * 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.
|
|
|
|
|
2017-03-20 19:14:29 +01:00
|
|
|
extern crate byteorder;
|
2016-09-13 15:26:31 +02:00
|
|
|
extern crate ethcore_bigint as bigint;
|
2016-09-01 13:36:32 +02:00
|
|
|
extern crate elastic_array;
|
2017-07-06 11:26:14 +02:00
|
|
|
extern crate rustc_hex;
|
2016-09-01 13:36:32 +02:00
|
|
|
|
|
|
|
#[macro_use]
|
|
|
|
extern crate lazy_static;
|
|
|
|
|
2017-03-20 19:14:29 +01:00
|
|
|
mod traits;
|
|
|
|
mod error;
|
|
|
|
mod rlpin;
|
|
|
|
mod untrusted_rlp;
|
|
|
|
mod stream;
|
|
|
|
mod compression;
|
|
|
|
mod common;
|
|
|
|
mod impls;
|
|
|
|
|
|
|
|
use std::borrow::Borrow;
|
2016-09-01 13:36:32 +02:00
|
|
|
use elastic_array::ElasticArray1024;
|
2015-12-19 13:35:26 +01:00
|
|
|
|
2017-03-20 19:14:29 +01:00
|
|
|
pub use error::DecoderError;
|
2017-03-22 14:41:46 +01:00
|
|
|
pub use traits::{Decodable, Encodable, Compressible};
|
2017-03-20 19:14:29 +01:00
|
|
|
pub use untrusted_rlp::{UntrustedRlp, UntrustedRlpIterator, PayloadInfo, Prototype};
|
|
|
|
pub use rlpin::{Rlp, RlpIterator};
|
|
|
|
pub use stream::RlpStream;
|
|
|
|
pub use compression::RlpType;
|
|
|
|
|
2016-02-03 14:51:45 +01:00
|
|
|
/// The RLP encoded empty data (used to mean "null value").
|
2015-12-19 13:35:26 +01:00
|
|
|
pub const NULL_RLP: [u8; 1] = [0x80; 1];
|
2016-02-03 14:51:45 +01:00
|
|
|
/// The RLP encoded empty list.
|
2015-12-26 15:48:41 +01:00
|
|
|
pub const EMPTY_LIST_RLP: [u8; 1] = [0xC0; 1];
|
2015-12-08 00:45:04 +01:00
|
|
|
|
2015-12-08 12:33:05 +01:00
|
|
|
/// Shortcut function to decode trusted rlp
|
2015-12-26 15:48:41 +01:00
|
|
|
///
|
2015-12-08 12:33:05 +01:00
|
|
|
/// ```rust
|
2016-09-01 13:36:32 +02:00
|
|
|
/// extern crate rlp;
|
2015-12-26 15:48:41 +01:00
|
|
|
///
|
2015-12-08 12:33:05 +01:00
|
|
|
/// fn main () {
|
2017-03-22 14:41:46 +01:00
|
|
|
/// let data = vec![0x83, b'c', b'a', b't'];
|
|
|
|
/// let animal: String = rlp::decode(&data);
|
|
|
|
/// assert_eq!(animal, "cat".to_owned());
|
2015-12-08 12:33:05 +01:00
|
|
|
/// }
|
|
|
|
/// ```
|
2017-03-22 14:41:46 +01:00
|
|
|
pub fn decode<T>(bytes: &[u8]) -> T where T: Decodable {
|
2015-12-08 00:45:04 +01:00
|
|
|
let rlp = Rlp::new(bytes);
|
|
|
|
rlp.as_val()
|
|
|
|
}
|
2015-12-08 12:33:05 +01:00
|
|
|
|
2017-03-22 14:41:46 +01:00
|
|
|
pub fn decode_list<T>(bytes: &[u8]) -> Vec<T> where T: Decodable {
|
|
|
|
let rlp = Rlp::new(bytes);
|
|
|
|
rlp.as_list()
|
|
|
|
}
|
|
|
|
|
2015-12-08 12:33:05 +01:00
|
|
|
/// Shortcut function to encode structure into rlp.
|
|
|
|
///
|
|
|
|
/// ```rust
|
2016-09-01 13:36:32 +02:00
|
|
|
/// extern crate rlp;
|
2015-12-26 15:48:41 +01:00
|
|
|
///
|
2015-12-08 12:33:05 +01:00
|
|
|
/// fn main () {
|
2016-01-27 12:14:57 +01:00
|
|
|
/// let animal = "cat";
|
2017-06-28 14:16:53 +02:00
|
|
|
/// let out = rlp::encode(&animal).into_vec();
|
2016-01-27 12:14:57 +01:00
|
|
|
/// assert_eq!(out, vec![0x83, b'c', b'a', b't']);
|
|
|
|
/// }
|
|
|
|
/// ```
|
2017-03-20 19:14:29 +01:00
|
|
|
pub fn encode<E>(object: &E) -> ElasticArray1024<u8> where E: Encodable {
|
2016-01-27 12:14:57 +01:00
|
|
|
let mut stream = RlpStream::new();
|
|
|
|
stream.append(object);
|
|
|
|
stream.drain()
|
|
|
|
}
|
2017-03-20 19:14:29 +01:00
|
|
|
|
|
|
|
pub fn encode_list<E, K>(object: &[K]) -> ElasticArray1024<u8> where E: Encodable, K: Borrow<E> {
|
|
|
|
let mut stream = RlpStream::new();
|
|
|
|
stream.append_list(object);
|
|
|
|
stream.drain()
|
|
|
|
}
|