From 4acfa58e44cd1782e14de58501458a6d9b17062e Mon Sep 17 00:00:00 2001 From: KAMADA Ken'ichi Date: Sat, 19 Nov 2016 20:16:49 +0900 Subject: [PATCH] Parse SSHORT fields. --- src/value.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/value.rs b/src/value.rs index 801ecd5..2562e13 100644 --- a/src/value.rs +++ b/src/value.rs @@ -48,6 +48,8 @@ pub enum Value<'a> { SByte(Vec), /// Slice of 8-bit bytes. Undefined(&'a [u8]), + /// Vector of 16-bit signed integers. Unused in the Exif specification. + SShort(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. @@ -76,6 +78,7 @@ pub fn get_type_info<'a, E>(typecode: u16) 5 => (8, parse_rational::), 6 => (1, parse_sbyte), 7 => (1, parse_undefined), + 8 => (2, parse_sshort::), _ => (0, parse_unknown), } } @@ -140,6 +143,15 @@ fn parse_undefined<'a>(data: &'a [u8], offset: usize, count: usize) Value::Undefined(&data[offset .. offset + count]) } +fn parse_sshort<'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::loadu16(data, offset + i * 2) as i16); + } + Value::SShort(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) @@ -277,4 +289,20 @@ mod tests { } } } + + #[test] + fn sshort() { + let sets: &[(&[u8], Vec)] = &[ + (b"x", vec![]), + (b"x\x01\x02\xf3\x04", vec![0x0102, -0x0cfc]), + ]; + let (unitlen, parser) = get_type_info::(8); + for &(data, ref ans) in sets { + assert!((data.len() - 1) % unitlen == 0); + match parser(data, 1, (data.len() - 1) / unitlen) { + Value::SShort(v) => assert_eq!(v, *ans), + v => panic!("wrong variant {:?}", v), + } + } + } }