forge
Explore
Log in

Don't you dare go Hollow

S13: ec-image bytes.rs holds the endian readers; tiff Reader and exif share them (same reads, same bounds)

Browse files at this commit

3155b28d59603f96f605767bd4d81b3f3be45bbcAhmet Kaan GümüşParent: 5442b3d
crates/ec-image/src/bytes.rsadded+17 -0
@@ -0,0 +1,17 @@
1+pub(crate) fn u16_at(data: &[u8], at: usize, big_endian: bool) -> Option<u16> {
2+ let b: [u8; 2] = data.get(at..at + 2)?.try_into().ok()?;
3+ Some(if big_endian {
4+ u16::from_be_bytes(b)
5+ } else {
6+ u16::from_le_bytes(b)
7+ })
8+}
9+
10+pub(crate) fn u32_at(data: &[u8], at: usize, big_endian: bool) -> Option<u32> {
11+ let b: [u8; 4] = data.get(at..at + 4)?.try_into().ok()?;
12+ Some(if big_endian {
13+ u32::from_be_bytes(b)
14+ } else {
15+ u32::from_le_bytes(b)
16+ })
17+}
crates/ec-image/src/exif.rsmodified+5 -19
@@ -8,27 +8,13 @@
88 /// Orientation 1..=8 from a bare TIFF block, or `None` if the block is not a
99 /// readable TIFF or carries no valid orientation entry.
1010 pub(crate) fn tiff_orientation(tiff: &[u8]) -> Option<u8> {
11- let little = match tiff.get(..2)? {
12- b"II" => true,
13- b"MM" => false,
11+ let big_endian = match tiff.get(..2)? {
12+ b"II" => false,
13+ b"MM" => true,
1414 _ => return None,
1515 };
16- let u16_at = |at: usize| -> Option<u16> {
17- let b = tiff.get(at..at + 2)?;
18- Some(if little {
19- u16::from_le_bytes([b[0], b[1]])
20- } else {
21- u16::from_be_bytes([b[0], b[1]])
22- })
23- };
24- let u32_at = |at: usize| -> Option<u32> {
25- let b = tiff.get(at..at + 4)?;
26- Some(if little {
27- u32::from_le_bytes([b[0], b[1], b[2], b[3]])
28- } else {
29- u32::from_be_bytes([b[0], b[1], b[2], b[3]])
30- })
31- };
16+ let u16_at = |at: usize| crate::bytes::u16_at(tiff, at, big_endian);
17+ let u32_at = |at: usize| crate::bytes::u32_at(tiff, at, big_endian);
3218 if u16_at(2)? != 42 {
3319 return None;
3420 }
crates/ec-image/src/lib.rsmodified+1 -0
@@ -42,6 +42,7 @@ pub mod png_encode;
4242 pub mod transform;
4343 pub mod webp;
4444
45+mod bytes;
4546 mod exif;
4647 mod probe;
4748
crates/ec-image/src/tiff.rsmodified+4 -18
@@ -55,27 +55,13 @@ struct Reader<'a> {
5555
5656 impl<'a> Reader<'a> {
5757 fn u16_at(&self, at: usize) -> Result<u16> {
58- let bytes = self
59- .data
60- .get(at..at + 2)
61- .ok_or_else(|| Error::corrupt("TIFF: a field past the end of the file"))?;
62- Ok(if self.big_endian {
63- u16::from_be_bytes([bytes[0], bytes[1]])
64- } else {
65- u16::from_le_bytes([bytes[0], bytes[1]])
66- })
58+ crate::bytes::u16_at(self.data, at, self.big_endian)
59+ .ok_or_else(|| Error::corrupt("TIFF: a field past the end of the file"))
6760 }
6861
6962 fn u32_at(&self, at: usize) -> Result<u32> {
70- let bytes = self
71- .data
72- .get(at..at + 4)
73- .ok_or_else(|| Error::corrupt("TIFF: a field past the end of the file"))?;
74- Ok(if self.big_endian {
75- u32::from_be_bytes([bytes[0], bytes[1], bytes[2], bytes[3]])
76- } else {
77- u32::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]])
78- })
63+ crate::bytes::u32_at(self.data, at, self.big_endian)
64+ .ok_or_else(|| Error::corrupt("TIFF: a field past the end of the file"))
7965 }
8066 }
8167