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;
3939 const TAG_EXTRA_SAMPLES: u16 = 338;
4040 const TAG_SAMPLE_FORMAT: u16 = 339;
4141
42-const COMPRESSION_NONE: u64 = 1;
43-const COMPRESSION_LZW: u64 = 5;
44-const COMPRESSION_DEFLATE: u64 = 8;
45-const COMPRESSION_PACKBITS: u64 = 32773;
46-const COMPRESSION_DEFLATE_OLD: u64 = 32946;
42+#[derive(Clone, Copy, PartialEq, Eq)]
43+enum Compression {
44+ None,
45+ Lzw,
46+ PackBits,
47+ Deflate,
48+}
4749
4850 /// The endianness the file declared, and the bytes it declared it about.
4951 struct Reader<'a> {
@@ -153,7 +155,7 @@ struct Layout {
153155 bits: Vec<u16>,
154156 samples: usize,
155157 photometric: u64,
156- compression: u64,
158+ compression: Compression,
157159 planar: u64,
158160 predictor: u64,
159161 /// Strip or tile data offsets, in the order the file lists them.
@@ -203,16 +205,15 @@ fn layout(data: &[u8]) -> Result<Layout> {
203205 }
204206 let entries = directory(&reader, reader.u32_at(4)? as usize)?;
205207
206- let compression = first(&entries, TAG_COMPRESSION, COMPRESSION_NONE);
207- match compression {
208- COMPRESSION_NONE
209- | COMPRESSION_LZW
210- | COMPRESSION_DEFLATE
211- | COMPRESSION_PACKBITS
212- | COMPRESSION_DEFLATE_OLD => {}
208+ let raw = first(&entries, TAG_COMPRESSION, 1);
209+ let compression = match raw {
210+ 1 => Compression::None,
211+ 5 => Compression::Lzw,
212+ 32773 => Compression::PackBits,
213+ 8 | 32946 => Compression::Deflate,
213214 2..=4 => {
214215 return Err(Error::unsupported(
215- format!("TIFF: CCITT compression {compression}"),
216+ format!("TIFF: CCITT compression {raw}"),
216217 "the fax coders are a separate format with their own \
217218 two-dimensional coding modes",
218219 ));
@@ -225,7 +226,7 @@ fn layout(data: &[u8]) -> Result<Layout> {
225226 ));
226227 }
227228 other => return Err(Error::corrupt(format!("TIFF: compression {other}"))),
228- }
229+ };
229230
230231 let samples = first(&entries, TAG_SAMPLES, 1) as usize;
231232 if samples == 0 || samples > 4 {
@@ -482,10 +483,10 @@ fn segment(layout: &Layout, data: &[u8], index: usize, expect: usize) -> Result<
482483 .get(at..at.saturating_add(len))
483484 .ok_or_else(|| Error::corrupt("TIFF: a strip reaching past the end of the file"))?;
484485 Ok(match layout.compression {
485- COMPRESSION_NONE => body.to_vec(),
486- COMPRESSION_LZW => lzw(body, expect)?,
487- COMPRESSION_PACKBITS => packbits(body, expect),
488- _ => ec_inflate::inflate_zlib(body, expect.saturating_mul(2).max(1024))
486+ Compression::None => body.to_vec(),
487+ Compression::Lzw => lzw(body, expect)?,
488+ Compression::PackBits => packbits(body, expect),
489+ Compression::Deflate => ec_inflate::inflate_zlib(body, expect.saturating_mul(2).max(1024))
489490 .map_err(|e| Error::corrupt(format!("TIFF: a Deflate strip: {e}")))?,
490491 })
491492 }