forge
Explore
Log in

Don't you dare go Hollow

S13: TIFF compression is an enum, not u64 consts; the raw tag value is only kept for the CCITT message

Browse files at this commit

165954c2916337fd902e4b719da8ea65ddc756fdAhmet Kaan GümüşParent: 4fd4ed4
crates/ec-image/src/tiff.rsmodified+20 -19
@@ -39,11 +39,13 @@ const TAG_TILE_COUNTS: u16 = 325;
39 const TAG_EXTRA_SAMPLES: u16 = 338;39 const TAG_EXTRA_SAMPLES: u16 = 338;
40 const TAG_SAMPLE_FORMAT: u16 = 339;40 const TAG_SAMPLE_FORMAT: u16 = 339;
41 41
42-const COMPRESSION_NONE: u64 = 1;42+#[derive(Clone, Copy, PartialEq, Eq)]
43-const COMPRESSION_LZW: u64 = 5;43+enum Compression {
44-const COMPRESSION_DEFLATE: u64 = 8;44+ None,
45-const COMPRESSION_PACKBITS: u64 = 32773;45+ Lzw,
46-const COMPRESSION_DEFLATE_OLD: u64 = 32946;46+ PackBits,
47+ Deflate,
48+}
47 49
48 /// The endianness the file declared, and the bytes it declared it about.50 /// The endianness the file declared, and the bytes it declared it about.
49 struct Reader<'a> {51 struct Reader<'a> {
@@ -153,7 +155,7 @@ struct Layout {
153 bits: Vec<u16>,155 bits: Vec<u16>,
154 samples: usize,156 samples: usize,
155 photometric: u64,157 photometric: u64,
156- compression: u64,158+ compression: Compression,
157 planar: u64,159 planar: u64,
158 predictor: u64,160 predictor: u64,
159 /// Strip or tile data offsets, in the order the file lists them.161 /// Strip or tile data offsets, in the order the file lists them.
@@ -203,16 +205,15 @@ fn layout(data: &[u8]) -> Result<Layout> {
203 }205 }
204 let entries = directory(&reader, reader.u32_at(4)? as usize)?;206 let entries = directory(&reader, reader.u32_at(4)? as usize)?;
205 207
206- let compression = first(&entries, TAG_COMPRESSION, COMPRESSION_NONE);208+ let raw = first(&entries, TAG_COMPRESSION, 1);
207- match compression {209+ let compression = match raw {
208- COMPRESSION_NONE210+ 1 => Compression::None,
209- | COMPRESSION_LZW211+ 5 => Compression::Lzw,
210- | COMPRESSION_DEFLATE212+ 32773 => Compression::PackBits,
211- | COMPRESSION_PACKBITS213+ 8 | 32946 => Compression::Deflate,
212- | COMPRESSION_DEFLATE_OLD => {}
213 2..=4 => {214 2..=4 => {
214 return Err(Error::unsupported(215 return Err(Error::unsupported(
215- format!("TIFF: CCITT compression {compression}"),216+ format!("TIFF: CCITT compression {raw}"),
216 "the fax coders are a separate format with their own \217 "the fax coders are a separate format with their own \
217 two-dimensional coding modes",218 two-dimensional coding modes",
218 ));219 ));
@@ -225,7 +226,7 @@ fn layout(data: &[u8]) -> Result<Layout> {
225 ));226 ));
226 }227 }
227 other => return Err(Error::corrupt(format!("TIFF: compression {other}"))),228 other => return Err(Error::corrupt(format!("TIFF: compression {other}"))),
228- }229+ };
229 230
230 let samples = first(&entries, TAG_SAMPLES, 1) as usize;231 let samples = first(&entries, TAG_SAMPLES, 1) as usize;
231 if samples == 0 || samples > 4 {232 if samples == 0 || samples > 4 {
@@ -482,10 +483,10 @@ fn segment(layout: &Layout, data: &[u8], index: usize, expect: usize) -> Result<
482 .get(at..at.saturating_add(len))483 .get(at..at.saturating_add(len))
483 .ok_or_else(|| Error::corrupt("TIFF: a strip reaching past the end of the file"))?;484 .ok_or_else(|| Error::corrupt("TIFF: a strip reaching past the end of the file"))?;
484 Ok(match layout.compression {485 Ok(match layout.compression {
485- COMPRESSION_NONE => body.to_vec(),486+ Compression::None => body.to_vec(),
486- COMPRESSION_LZW => lzw(body, expect)?,487+ Compression::Lzw => lzw(body, expect)?,
487- COMPRESSION_PACKBITS => packbits(body, expect),488+ Compression::PackBits => packbits(body, expect),
488- _ => ec_inflate::inflate_zlib(body, expect.saturating_mul(2).max(1024))489+ Compression::Deflate => ec_inflate::inflate_zlib(body, expect.saturating_mul(2).max(1024))
489 .map_err(|e| Error::corrupt(format!("TIFF: a Deflate strip: {e}")))?,490 .map_err(|e| Error::corrupt(format!("TIFF: a Deflate strip: {e}")))?,
490 })491 })
491 }492 }