Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target/
/Cargo.lock
19 changes: 9 additions & 10 deletions src/isobmff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ struct Location {
impl<R> Parser<R> where R: BufRead + Seek {
fn new(reader: R) -> Self {
Self {
reader: reader,
reader,
ftyp_checked: false,
item_id: None,
item_location: None,
Expand Down Expand Up @@ -127,7 +127,7 @@ impl<R> Parser<R> where R: BufRead + Seek {
let mut buf = [0; 8];
self.reader.read_exact(&mut buf)?;
let size = match BigEndian::loadu32(&buf, 0) {
0 => Some(std::u64::MAX),
0 => Some(u64::MAX),
1 => read64(&mut self.reader)?.checked_sub(16),
x => u64::from(x).checked_sub(8),
}.ok_or("Invalid box size")?;
Expand All @@ -138,7 +138,7 @@ impl<R> Parser<R> where R: BufRead + Seek {
fn read_file_level_box(&mut self, size: u64) -> Result<Vec<u8>, Error> {
let mut buf;
match size {
std::u64::MAX => {
u64::MAX => {
buf = Vec::new();
self.reader.read_to_end(&mut buf)?;
},
Expand All @@ -154,7 +154,7 @@ impl<R> Parser<R> where R: BufRead + Seek {

fn skip_file_level_box(&mut self, size: u64) -> Result<(), Error> {
match size {
std::u64::MAX => self.reader.seek(SeekFrom::End(0))?,
u64::MAX => self.reader.seek(SeekFrom::End(0))?,
_ => self.reader.seek(SeekFrom::Current(
size.try_into().or(Err("Large seek not supported"))?))?,
};
Expand All @@ -164,7 +164,7 @@ impl<R> Parser<R> where R: BufRead + Seek {
fn parse_ftyp(&mut self, mut boxp: BoxSplitter) -> Result<(), Error> {
let head = boxp.slice(8)?;
let _major_brand = &head[0..4];
let _minor_version = BigEndian::loadu32(&head, 4);
let _minor_version = BigEndian::loadu32(head, 4);
while let Ok(compat_brand) = boxp.array4() {
if HEIF_BRANDS.contains(&compat_brand) {
return Ok(());
Expand Down Expand Up @@ -301,9 +301,8 @@ impl<R> Parser<R> where R: BufRead + Seek {
};
for _ in 0..entry_count {
let (boxtype, body) = boxp.child_box()?;
match boxtype {
b"infe" => self.parse_infe(body)?,
_ => {},
if boxtype == b"infe" {
self.parse_infe(body)?
}
}
Ok(())
Expand Down Expand Up @@ -483,7 +482,7 @@ mod tests {
// to the end of the file
let mut p = Parser::new(Cursor::new(b"\0\0\0\0abcd"));
assert_eq!(p.read_box_header().unwrap(),
Some((std::u64::MAX, *b"abcd")));
Some((u64::MAX, *b"abcd")));
// largesize
let mut p = Parser::new(Cursor::new(
b"\0\0\0\x01abcd\0\0\0\0\0\0\0\x10"));
Expand All @@ -498,7 +497,7 @@ mod tests {
let mut p = Parser::new(Cursor::new(
b"\0\0\0\x01abcd\xff\xff\xff\xff\xff\xff\xff\xff"));
assert_eq!(p.read_box_header().unwrap(),
Some((std::u64::MAX.wrapping_sub(16), *b"abcd")));
Some((u64::MAX.wrapping_sub(16), *b"abcd")));
}

#[test]
Expand Down
23 changes: 20 additions & 3 deletions src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ pub struct Reader {
continue_on_error: bool,
}

impl Default for Reader {
fn default() -> Self {
Self::new()
}
}

impl Reader {
/// Constructs a new `Reader`.
pub fn new() -> Self {
Expand Down Expand Up @@ -106,15 +112,16 @@ impl Reader {
/// If an error occurred, `exif::Error` is returned.
pub fn read_raw(&self, data: Vec<u8>) -> Result<Exif, Error> {
let mut parser = tiff::Parser::new();
parser.continue_on_error = self.continue_on_error.then(|| Vec::new());
parser.continue_on_error = self.continue_on_error.then(Vec::new);
parser.parse(&data)?;
let entry_map = parser.entries.iter().enumerate()
.map(|(i, e)| (e.ifd_num_tag(), i)).collect();
let exif = Exif {
buf: data,
entries: parser.entries,
entry_map: entry_map,
entry_map,
little_endian: parser.little_endian,
bigtiff: parser.bigtiff,
};
match parser.continue_on_error {
Some(v) if !v.is_empty() =>
Expand Down Expand Up @@ -188,6 +195,8 @@ pub struct Exif {
entry_map: HashMap<(In, Tag), usize>,
// True if the TIFF data is little endian.
little_endian: bool,
/// True if the TIFF data is in the BigTIFF format.
bigtiff: bool,
}

impl Exif {
Expand All @@ -201,7 +210,9 @@ impl Exif {
#[inline]
pub fn fields(&self) -> impl ExactSizeIterator<Item = &Field> {
self.entries.iter()
.map(move |e| e.ref_field(&self.buf, self.little_endian))
.map(move |e| {
e.ref_field(&self.buf, self.little_endian)
})
}

/// Returns true if the Exif data (TIFF structure) is in the
Expand All @@ -211,6 +222,12 @@ impl Exif {
self.little_endian
}

/// Returns true if the Exif data (TIFF structure) is in BigTIFF format.
#[inline]
pub fn bigtiff(&self) -> bool {
self.bigtiff
}

/// Returns a reference to the Exif field specified by the tag
/// and the IFD number.
#[inline]
Expand Down
17 changes: 11 additions & 6 deletions src/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -749,10 +749,10 @@ generate_well_known_tag_constants!(
);

// For Value::display_as().
pub fn display_value_as<'a>(value: &'a Value, tag: Tag) -> value::Display<'a> {
pub fn display_value_as(value: &Value, tag: Tag) -> value::Display<'_> {
match get_tag_info(tag) {
Some(ti) => value::Display { fmt: ti.dispval, value: value },
None => value::Display { fmt: d_default, value: value },
Some(ti) => value::Display { fmt: ti.dispval, value },
None => value::Display { fmt: d_default, value },
}
}

Expand Down Expand Up @@ -1048,13 +1048,13 @@ fn d_subjarea(w: &mut dyn fmt::Write, value: &Value) -> fmt::Result {
// Acceleration (Exif 0x9404), CameraElevationAngle (Exif 0x9405)
fn d_optdecimal(w: &mut dyn fmt::Write, value: &Value) -> fmt::Result {
match *value {
Value::Rational(ref v) if v.len() > 0 =>
Value::Rational(ref v) if !v.is_empty() =>
if v[0].denom != 0xffffffff {
write!(w, "{}", v[0].to_f64())
} else {
w.write_str("unknown")
},
Value::SRational(ref v) if v.len() > 0 =>
Value::SRational(ref v) if !v.is_empty() =>
if v[0].denom != -1 {
write!(w, "{}", v[0].to_f64())
} else {
Expand Down Expand Up @@ -1442,9 +1442,14 @@ fn d_default(w: &mut dyn fmt::Write, value: &Value) -> fmt::Result {
Value::SRational(ref v) => d_sub_comma(w, v),
Value::Float(ref v) => d_sub_comma(w, v),
Value::Double(ref v) => d_sub_comma(w, v),
Value::Long8(ref v) => d_sub_comma(w, v),
Value::SLong8(ref v) => d_sub_comma(w, v),
Value::Unknown(t, c, o) =>
write!(w, "unknown value (type={}, count={}, offset={:#x})",
t, c, o),
Value::UnknownBigTiff(t, c, o) =>
write!(w, "unknown value (type={}, count={}, offset={:#x})",
t, c, o),
}
}

Expand All @@ -1463,7 +1468,7 @@ where I: IntoIterator<Item = T>, T: fmt::Display {

struct AsciiDisplay<'a>(&'a [u8]);

impl<'a> fmt::Display for AsciiDisplay<'a> {
impl fmt::Display for AsciiDisplay<'_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
d_sub_ascii(f, self.0)
}
Expand Down
Loading