forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

probe.rs33 lines, 1.3 KiBHistoryRawDownload
1//! Bounded header probes: format, size and EXIF orientation from the front of a file.
2//!
3//! [`probe`] and [`probe_reader`] read no pixels. They look at the signature, the
4//! image header and the EXIF orientation, and at no more than [`PROBE_LIMIT`]
5//! bytes of the file, so a probe of an arbitrarily large picture costs a bounded
6//! read and a bounded allocation (none beyond the window itself).
7//!
8//! A probe reports what the bytes it saw say. An `eXIf`, `EXIF` or APP1 segment
9//! that starts past the window is not seen, and the orientation reads as 1.
10
11use crate::{Info, Result};
12use ec_core::Error;
13use std::io::Read;
14
15/// The most bytes a probe examines: 1 MiB.
16pub const PROBE_LIMIT: usize = 1 << 20;
17
18/// Format, dimensions and orientation from the first [`PROBE_LIMIT`] bytes of
19/// `prefix`. Bytes past the limit are ignored, not an error.
20pub fn probe(prefix: &[u8]) -> Result<Info> {
21 crate::info(&prefix[..prefix.len().min(PROBE_LIMIT)])
22}
23
24/// Format, dimensions and orientation from a reader. At most [`PROBE_LIMIT`]
25/// bytes are read, and the reader is left positioned after them.
26pub fn probe_reader<R: Read>(reader: R) -> Result<Info> {
27 let mut window = Vec::new();
28 reader
29 .take(PROBE_LIMIT as u64)
30 .read_to_end(&mut window)
31 .map_err(Error::Io)?;
32 crate::info(&window)
33}