Parse SSHORT fields.

This commit is contained in:
KAMADA Ken'ichi 2016-11-19 20:16:49 +09:00
parent 2612b22d54
commit 4acfa58e44
1 changed files with 28 additions and 0 deletions

View File

@ -48,6 +48,8 @@ pub enum Value<'a> {
SByte(Vec<i8>),
/// Slice of 8-bit bytes.
Undefined(&'a [u8]),
/// Vector of 16-bit signed integers. Unused in the Exif specification.
SShort(Vec<i16>),
/// 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::<E>),
6 => (1, parse_sbyte),
7 => (1, parse_undefined),
8 => (2, parse_sshort::<E>),
_ => (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<i16>)] = &[
(b"x", vec![]),
(b"x\x01\x02\xf3\x04", vec![0x0102, -0x0cfc]),
];
let (unitlen, parser) = get_type_info::<BigEndian>(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),
}
}
}
}