From 35ae32a8a5b85724f66af20208f9f908fca882ae Mon Sep 17 00:00:00 2001 From: debris Date: Wed, 2 Dec 2015 14:05:17 +0100 Subject: [PATCH] payload_info moved to decoder --- src/rlp.rs | 71 +++++++++++++++++++++++++++--------------------------- 1 file changed, 36 insertions(+), 35 deletions(-) diff --git a/src/rlp.rs b/src/rlp.rs index 1648f8807..67fad0bb0 100644 --- a/src/rlp.rs +++ b/src/rlp.rs @@ -355,7 +355,7 @@ impl<'a, 'view> UntrustedRlp<'a> where 'a: 'view { } pub fn data(&'view self) -> &'a [u8] { - let ii = Self::item_info(self.bytes).unwrap(); + let ii = BasicDecoder::payload_info(self.bytes).unwrap(); &self.bytes[ii.header_len..(ii.header_len + ii.value_len)] } @@ -396,7 +396,7 @@ impl<'a, 'view> UntrustedRlp<'a> where 'a: 'view { /// ``` pub fn size(&self) -> usize { match self.is_data() { - true => Self::item_info(self.bytes).unwrap().value_len, + true => BasicDecoder::payload_info(self.bytes).unwrap().value_len, false => 0 } } @@ -439,7 +439,7 @@ impl<'a, 'view> UntrustedRlp<'a> where 'a: 'view { self.cache.set(OffsetCache::new(index, self.bytes.len() - bytes.len())); // construct new rlp - let found = try!(UntrustedRlp::item_info(bytes)); + let found = try!(BasicDecoder::payload_info(bytes)); Ok(UntrustedRlp::new(&bytes[0..found.header_len + found.value_len])) } @@ -554,7 +554,7 @@ impl<'a, 'view> UntrustedRlp<'a> where 'a: 'view { /// consumes first found prefix fn consume_list_prefix(&self) -> Result<&'a [u8], DecoderError> { - let item = try!(UntrustedRlp::item_info(self.bytes)); + let item = try!(BasicDecoder::payload_info(self.bytes)); let bytes = try!(UntrustedRlp::consume(self.bytes, item.header_len)); Ok(bytes) } @@ -563,42 +563,12 @@ impl<'a, 'view> UntrustedRlp<'a> where 'a: 'view { fn consume_items(bytes: &'a [u8], items: usize) -> Result<&'a [u8], DecoderError> { let mut result = bytes; for _ in 0..items { - let i = try!(UntrustedRlp::item_info(result)); + let i = try!(BasicDecoder::payload_info(result)); result = try!(UntrustedRlp::consume(result, (i.header_len + i.value_len))); } Ok(result) } - /// return first item info - /// - /// TODO: move this to decoder (?) - fn item_info(bytes: &[u8]) -> Result { - let item = match bytes.first().map(|&x| x) { - None => return Err(DecoderError::RlpIsTooShort), - Some(0...0x7f) => PayloadInfo::new(0, 1), - Some(l @ 0x80...0xb7) => PayloadInfo::new(1, l as usize - 0x80), - Some(l @ 0xb8...0xbf) => { - let len_of_len = l as usize - 0xb7; - let header_len = 1 + len_of_len; - let value_len = try!(usize::from_bytes(&bytes[1..header_len])); - PayloadInfo::new(header_len, value_len) - } - Some(l @ 0xc0...0xf7) => PayloadInfo::new(1, l as usize - 0xc0), - Some(l @ 0xf8...0xff) => { - let len_of_len = l as usize - 0xf7; - let header_len = 1 + len_of_len; - let value_len = try!(usize::from_bytes(&bytes[1..header_len])); - PayloadInfo::new(header_len, value_len) - } - // we cant reach this place, but rust requires _ to be implemented - _ => return Err(DecoderError::BadRlp), - }; - - match item.header_len + item.value_len <= bytes.len() { - true => Ok(item), - false => Err(DecoderError::RlpIsTooShort), - } - } /// consumes slice prefix of length `len` fn consume(bytes: &'a [u8], len: usize) -> Result<&'a [u8], DecoderError> { @@ -730,6 +700,37 @@ pub trait Decoder { struct BasicDecoder; +impl BasicDecoder { + /// Return first item info + fn payload_info(bytes: &[u8]) -> Result { + let item = match bytes.first().map(|&x| x) { + None => return Err(DecoderError::RlpIsTooShort), + Some(0...0x7f) => PayloadInfo::new(0, 1), + Some(l @ 0x80...0xb7) => PayloadInfo::new(1, l as usize - 0x80), + Some(l @ 0xb8...0xbf) => { + let len_of_len = l as usize - 0xb7; + let header_len = 1 + len_of_len; + let value_len = try!(usize::from_bytes(&bytes[1..header_len])); + PayloadInfo::new(header_len, value_len) + } + Some(l @ 0xc0...0xf7) => PayloadInfo::new(1, l as usize - 0xc0), + Some(l @ 0xf8...0xff) => { + let len_of_len = l as usize - 0xf7; + let header_len = 1 + len_of_len; + let value_len = try!(usize::from_bytes(&bytes[1..header_len])); + PayloadInfo::new(header_len, value_len) + } + // we cant reach this place, but rust requires _ to be implemented + _ => return Err(DecoderError::BadRlp), + }; + + match item.header_len + item.value_len <= bytes.len() { + true => Ok(item), + false => Err(DecoderError::RlpIsTooShort), + } + } +} + impl Decoder for BasicDecoder { fn read_value(bytes: &[u8], f: F) -> Result where F: FnOnce(&[u8]) -> Result { match bytes.first().map(|&x| x) {