diff --git a/crates/ec-image/src/bytes.rs b/crates/ec-image/src/bytes.rs new file mode 100644 index 00000000..f72f6fd8 --- /dev/null +++ b/crates/ec-image/src/bytes.rs @@ -0,0 +1,17 @@ +pub(crate) fn u16_at(data: &[u8], at: usize, big_endian: bool) -> Option { + let b: [u8; 2] = data.get(at..at + 2)?.try_into().ok()?; + Some(if big_endian { + u16::from_be_bytes(b) + } else { + u16::from_le_bytes(b) + }) +} + +pub(crate) fn u32_at(data: &[u8], at: usize, big_endian: bool) -> Option { + let b: [u8; 4] = data.get(at..at + 4)?.try_into().ok()?; + Some(if big_endian { + u32::from_be_bytes(b) + } else { + u32::from_le_bytes(b) + }) +} diff --git a/crates/ec-image/src/exif.rs b/crates/ec-image/src/exif.rs index 256f0f73..32a40cad 100644 --- a/crates/ec-image/src/exif.rs +++ b/crates/ec-image/src/exif.rs @@ -8,27 +8,13 @@ /// Orientation 1..=8 from a bare TIFF block, or `None` if the block is not a /// readable TIFF or carries no valid orientation entry. pub(crate) fn tiff_orientation(tiff: &[u8]) -> Option { - let little = match tiff.get(..2)? { - b"II" => true, - b"MM" => false, + let big_endian = match tiff.get(..2)? { + b"II" => false, + b"MM" => true, _ => return None, }; - let u16_at = |at: usize| -> Option { - let b = tiff.get(at..at + 2)?; - Some(if little { - u16::from_le_bytes([b[0], b[1]]) - } else { - u16::from_be_bytes([b[0], b[1]]) - }) - }; - let u32_at = |at: usize| -> Option { - let b = tiff.get(at..at + 4)?; - Some(if little { - u32::from_le_bytes([b[0], b[1], b[2], b[3]]) - } else { - u32::from_be_bytes([b[0], b[1], b[2], b[3]]) - }) - }; + let u16_at = |at: usize| crate::bytes::u16_at(tiff, at, big_endian); + let u32_at = |at: usize| crate::bytes::u32_at(tiff, at, big_endian); if u16_at(2)? != 42 { return None; } diff --git a/crates/ec-image/src/lib.rs b/crates/ec-image/src/lib.rs index dca332e3..2858436d 100644 --- a/crates/ec-image/src/lib.rs +++ b/crates/ec-image/src/lib.rs @@ -42,6 +42,7 @@ pub mod png_encode; pub mod transform; pub mod webp; +mod bytes; mod exif; mod probe; diff --git a/crates/ec-image/src/tiff.rs b/crates/ec-image/src/tiff.rs index a1cd4904..222e5097 100644 --- a/crates/ec-image/src/tiff.rs +++ b/crates/ec-image/src/tiff.rs @@ -55,27 +55,13 @@ struct Reader<'a> { impl<'a> Reader<'a> { fn u16_at(&self, at: usize) -> Result { - let bytes = self - .data - .get(at..at + 2) - .ok_or_else(|| Error::corrupt("TIFF: a field past the end of the file"))?; - Ok(if self.big_endian { - u16::from_be_bytes([bytes[0], bytes[1]]) - } else { - u16::from_le_bytes([bytes[0], bytes[1]]) - }) + crate::bytes::u16_at(self.data, at, self.big_endian) + .ok_or_else(|| Error::corrupt("TIFF: a field past the end of the file")) } fn u32_at(&self, at: usize) -> Result { - let bytes = self - .data - .get(at..at + 4) - .ok_or_else(|| Error::corrupt("TIFF: a field past the end of the file"))?; - Ok(if self.big_endian { - u32::from_be_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]) - } else { - u32::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]) - }) + crate::bytes::u32_at(self.data, at, self.big_endian) + .ok_or_else(|| Error::corrupt("TIFF: a field past the end of the file")) } }