2018-09-11 10:17:54 +00:00
|
|
|
use super::{
|
|
|
|
LENGTH_BYTES,
|
|
|
|
};
|
|
|
|
|
2018-09-11 11:32:23 +00:00
|
|
|
#[derive(Debug, PartialEq)]
|
2018-09-11 10:17:54 +00:00
|
|
|
pub enum DecodeError {
|
|
|
|
OutOfBounds,
|
|
|
|
TooShort,
|
|
|
|
TooLong,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait Decodable: Sized {
|
2018-09-18 08:47:25 +00:00
|
|
|
fn ssz_decode(bytes: &[u8], index: usize) -> Result<(Self, usize), DecodeError>;
|
2018-09-18 05:53:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Decode the given bytes for the given type
|
|
|
|
///
|
|
|
|
/// The single ssz encoded value will be decoded as the given type at the
|
|
|
|
/// given index.
|
|
|
|
pub fn decode_ssz<T>(ssz_bytes: &[u8], index: usize)
|
2018-09-18 08:47:25 +00:00
|
|
|
-> Result<(T, usize), DecodeError>
|
2018-09-18 05:53:53 +00:00
|
|
|
where T: Decodable
|
|
|
|
{
|
|
|
|
if index >= ssz_bytes.len() {
|
|
|
|
return Err(DecodeError::OutOfBounds)
|
|
|
|
}
|
|
|
|
T::ssz_decode(ssz_bytes, index)
|
2018-09-11 10:17:54 +00:00
|
|
|
}
|
|
|
|
|
2018-09-11 11:32:23 +00:00
|
|
|
/// Return the nth value in some ssz encoded list.
|
|
|
|
///
|
|
|
|
/// The four-byte length prefix is not included in the return.
|
|
|
|
///
|
|
|
|
/// A single ssz encoded value can be considered a list of
|
|
|
|
/// one element, so this function will work on it too.
|
2018-09-11 10:17:54 +00:00
|
|
|
fn nth_value(ssz_bytes: &[u8], n: usize)
|
|
|
|
-> Result<&[u8], DecodeError>
|
|
|
|
{
|
|
|
|
let mut c: usize = 0;
|
|
|
|
for i in 0..(n + 1) {
|
|
|
|
let length = decode_length(&ssz_bytes[c..], LENGTH_BYTES)?;
|
|
|
|
let next = c + LENGTH_BYTES + length;
|
|
|
|
|
|
|
|
if i == n {
|
|
|
|
return Ok(&ssz_bytes[c + LENGTH_BYTES..next]);
|
|
|
|
} else {
|
|
|
|
if next >= ssz_bytes.len() {
|
|
|
|
return Err(DecodeError::OutOfBounds);
|
|
|
|
} else {
|
|
|
|
c = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(DecodeError::OutOfBounds)
|
|
|
|
}
|
|
|
|
|
2018-09-11 11:32:23 +00:00
|
|
|
/// Given some number of bytes, interpret the first four
|
|
|
|
/// bytes as a 32-bit big-endian integer and return the
|
|
|
|
/// result.
|
2018-09-11 10:17:54 +00:00
|
|
|
fn decode_length(bytes: &[u8], length_bytes: usize)
|
|
|
|
-> Result<usize, DecodeError>
|
|
|
|
{
|
|
|
|
if bytes.len() < length_bytes {
|
|
|
|
return Err(DecodeError::TooShort);
|
|
|
|
};
|
|
|
|
let mut len: usize = 0;
|
|
|
|
for i in 0..length_bytes {
|
|
|
|
let offset = (length_bytes - i - 1) * 8;
|
|
|
|
len = ((bytes[i] as usize) << offset) | len;
|
|
|
|
};
|
|
|
|
Ok(len)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2018-09-11 10:25:49 +00:00
|
|
|
use super::super::encode::encode_length;
|
2018-09-11 10:17:54 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_ssz_decode_length() {
|
|
|
|
let decoded = decode_length(
|
2018-09-18 08:47:25 +00:00
|
|
|
&vec![0, 0, 0, 1],
|
2018-09-11 10:17:54 +00:00
|
|
|
LENGTH_BYTES);
|
|
|
|
assert_eq!(decoded.unwrap(), 1);
|
|
|
|
|
|
|
|
let decoded = decode_length(
|
2018-09-18 08:47:25 +00:00
|
|
|
&vec![0, 0, 1, 0],
|
2018-09-11 10:17:54 +00:00
|
|
|
LENGTH_BYTES);
|
|
|
|
assert_eq!(decoded.unwrap(), 256);
|
|
|
|
|
|
|
|
let decoded = decode_length(
|
2018-09-18 08:47:25 +00:00
|
|
|
&vec![0, 0, 1, 255],
|
2018-09-11 10:17:54 +00:00
|
|
|
LENGTH_BYTES);
|
|
|
|
assert_eq!(decoded.unwrap(), 511);
|
|
|
|
|
|
|
|
let decoded = decode_length(
|
2018-09-18 08:47:25 +00:00
|
|
|
&vec![255, 255, 255, 255],
|
2018-09-11 10:17:54 +00:00
|
|
|
LENGTH_BYTES);
|
2018-09-18 08:47:25 +00:00
|
|
|
assert_eq!(decoded.unwrap(), 4294967295);
|
2018-09-11 10:17:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_encode_decode_length() {
|
|
|
|
let params: Vec<usize> = vec![
|
|
|
|
0, 1, 2, 3, 7, 8, 16,
|
|
|
|
2^8, 2^8 + 1,
|
|
|
|
2^16, 2^16 + 1,
|
|
|
|
2^24, 2^24 + 1,
|
|
|
|
2^32,
|
|
|
|
];
|
|
|
|
for i in params {
|
|
|
|
let decoded = decode_length(
|
|
|
|
&encode_length(i, LENGTH_BYTES),
|
|
|
|
LENGTH_BYTES).unwrap();
|
|
|
|
assert_eq!(i, decoded);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|