From 51156d9fde2a6de10d08f8a1224003b8e13e3a73 Mon Sep 17 00:00:00 2001 From: Tangent 128 Date: Thu, 29 Jun 2017 02:38:08 -0400 Subject: [PATCH] implement decode_uint --- src/ebml.rs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/ebml.rs b/src/ebml.rs index fe6d56f..d3749a5 100644 --- a/src/ebml.rs +++ b/src/ebml.rs @@ -93,6 +93,18 @@ pub fn decode_tag(bytes: &[u8]) -> Result, Error> { } } +pub fn decode_uint(bytes: &[u8]) -> Result { + if bytes.len() < 1 || bytes.len() > 8 { + return Err(Error::CorruptPayload); + } + + let mut value: u64 = 0; + for byte in bytes { + value = (value << 8) + (*byte as u64); + } + Ok(value) +} + #[derive(Debug, PartialEq)] pub struct Ebml(pub S, pub T); @@ -190,6 +202,25 @@ mod tests { assert_eq!(decode_tag(&[0x85, 0x40, 52]), Ok(Some((5, Value(52), 3)))); } + #[test] + fn bad_uints() { + assert_eq!(decode_uint(&[]), Err(Error::CorruptPayload)); + assert_eq!(decode_uint(&[0; 9]), Err(Error::CorruptPayload)); + } + + #[test] + fn parse_uints() { + assert_eq!(decode_uint(&[0]), Ok(0)); + assert_eq!(decode_uint(&[0; 8]), Ok(0)); + assert_eq!(decode_uint(&[1]), Ok(1)); + assert_eq!(decode_uint(&[0,0,0,0,0,0,0,1]), Ok(1)); + assert_eq!(decode_uint(&[38]), Ok(38)); + assert_eq!(decode_uint(&[0,0,0,0,0,0,0,38]), Ok(38)); + assert_eq!(decode_uint(&[0x7F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF]), Ok(9223372036854775807)); + assert_eq!(decode_uint(&[0x80,0,0,0,0,0,0,0]), Ok(9223372036854775808)); + assert_eq!(decode_uint(&[0x80,0,0,0,0,0,0,1]), Ok(9223372036854775809)); + } + struct Dummy; #[derive(Debug, PartialEq)]