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 @@
8 /// Orientation 1..=8 from a bare TIFF block, or `None` if the block is not a8 /// Orientation 1..=8 from a bare TIFF block, or `None` if the block is not a
9 /// readable TIFF or carries no valid orientation entry.9 /// readable TIFF or carries no valid orientation entry.
10 pub(crate) fn tiff_orientation(tiff: &[u8]) -> Option<u8> {10 pub(crate) fn tiff_orientation(tiff: &[u8]) -> Option<u8> {
11- let little = match tiff.get(..2)? {11+ let big_endian = match tiff.get(..2)? {
12- b"II" => true,12+ b"II" => false,
13- b"MM" => false,13+ b"MM" => true,
14 _ => return None,14 _ => return None,
15 };15 };
16- let u16_at = |at: usize| -> Option<u16> {16+ let u16_at = |at: usize| crate::bytes::u16_at(tiff, at, big_endian);
17- let b = tiff.get(at..at + 2)?;17+ let u32_at = |at: usize| crate::bytes::u32_at(tiff, at, big_endian);
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- };
32 if u16_at(2)? != 42 {18 if u16_at(2)? != 42 {
33 return None;19 return None;
34 }20 }
crates/ec-image/src/lib.rsmodified+1 -0
@@ -42,6 +42,7 @@ pub mod png_encode;
42 pub mod transform;42 pub mod transform;
43 pub mod webp;43 pub mod webp;
44 44
45+mod bytes;
45 mod exif;46 mod exif;
46 mod probe;47 mod probe;
47 48
crates/ec-image/src/tiff.rsmodified+4 -18
@@ -55,27 +55,13 @@ struct Reader<'a> {
55 55
56 impl<'a> Reader<'a> {56 impl<'a> Reader<'a> {
57 fn u16_at(&self, at: usize) -> Result<u16> {57 fn u16_at(&self, at: usize) -> Result<u16> {
58- let bytes = self58+ crate::bytes::u16_at(self.data, at, self.big_endian)
59- .data59+ .ok_or_else(|| Error::corrupt("TIFF: a field past the end of the file"))
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- })
67 }60 }
68 61
69 fn u32_at(&self, at: usize) -> Result<u32> {62 fn u32_at(&self, at: usize) -> Result<u32> {
70- let bytes = self63+ crate::bytes::u32_at(self.data, at, self.big_endian)
71- .data64+ .ok_or_else(|| Error::corrupt("TIFF: a field past the end of the file"))
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- })
79 }65 }
80 }66 }
81 67