From 0741492615b982d91328c4cbe21299a53acad3ae Mon Sep 17 00:00:00 2001 From: KAMADA Ken'ichi Date: Sat, 19 Nov 2016 20:21:08 +0900 Subject: [PATCH] Parse SLONG fields. --- src/value.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/value.rs b/src/value.rs index 2562e13..0a4a58c 100644 --- a/src/value.rs +++ b/src/value.rs @@ -50,6 +50,8 @@ pub enum Value<'a> { Undefined(&'a [u8]), /// Vector of 16-bit signed integers. Unused in the Exif specification. SShort(Vec), + /// Vector of 32-bit signed integers. + SLong(Vec), /// The type is unknown to this implementation. /// The associated values are the type and the count, and the /// offset of the "Value Offset" element. @@ -79,6 +81,7 @@ pub fn get_type_info<'a, E>(typecode: u16) 6 => (1, parse_sbyte), 7 => (1, parse_undefined), 8 => (2, parse_sshort::), + 9 => (4, parse_slong::), _ => (0, parse_unknown), } } @@ -152,6 +155,15 @@ fn parse_sshort<'a, E>(data: &'a [u8], offset: usize, count: usize) Value::SShort(val) } +fn parse_slong<'a, E>(data: &'a [u8], offset: usize, count: usize) + -> Value<'a> where E: Endian { + let mut val = Vec::with_capacity(count); + for i in 0..count { + val.push(E::loadu32(data, offset + i * 4) as i32); + } + Value::SLong(val) +} + // This is a dummy function and will never be called. #[allow(unused_variables)] fn parse_unknown<'a>(data: &'a [u8], offset: usize, count: usize) @@ -305,4 +317,21 @@ mod tests { } } } + + #[test] + fn slong() { + let sets: &[(&[u8], Vec)] = &[ + (b"x", vec![]), + (b"x\x01\x02\x03\x04\x85\x06\x07\x08", + vec![0x01020304, -0x7af9f8f8]), + ]; + let (unitlen, parser) = get_type_info::(9); + for &(data, ref ans) in sets { + assert!((data.len() - 1) % unitlen == 0); + match parser(data, 1, (data.len() - 1) / unitlen) { + Value::SLong(v) => assert_eq!(v, *ans), + v => panic!("wrong variant {:?}", v), + } + } + } }