kamadak / exif-rs

Exif parsing library written in pure Rust
BSD 2-Clause "Simplified" License
198 stars 44 forks source link

InvalidFormat("Unexpected next IFD") #5

Closed hagsteel closed 6 years ago

hagsteel commented 6 years ago

Trying to read exif data from a Canon CR2 file and get this error:

    InvalidFormat(
        "Unexpected next IFD"
    )

Here is an example CR2 file: https://www.imaging-resource.com/PRODS/canon-1dx/E1DXINBI000050.CR2.HTM

I'm using kamadak-exif = "0.3.0"

Steps to replicate:

  1. Put a CR2 file in /tmp/E1DXINBI000050.CR2
  2. add kamadak-exif = "0.3.0" to Cargo.toml
  3. run cargo run

Here is some code to replicate the error:

extern crate exif;

use std::fs::File;
use std::io::BufReader;

use exif::*;

fn main() {
    let reader = File::open("/tmp/E1DXINBI000050.CR2")
        .map(BufReader::new)
        .map(|mut r| Reader::new(&mut r))
        .unwrap();

    // Output the error
    if reader.is_err() {
        println!("{:#?}", reader.err());
        panic!("fail");
    }   
    let reader = reader.unwrap();
    let field = reader.get_field(Tag::DateTime, false).unwrap();

    if let Value::Ascii(ref vec) = field.value {
        let date = DateTime::from_ascii(vec[0]).unwrap();
        println!("{:#?}", date);
    }  
}

Update: let me know if you need any more info or if there is something else I can do to help with this. I don't have a direct understanding of the EXIF format to try to solve this my self

Tried this with a Nikon NEF file and that works fine so it's only the Canon CR2 format so far

hagsteel commented 6 years ago

As a hacky workaround I removed thumbnail from here: https://github.com/kamadak/exif-rs/blob/master/src/tiff.rs#L141 and it works

Maybe that will provide some insight into where it's going wrong as well.

kamadak commented 6 years ago

Thank you for the report. The CR2 file contains additional (raw and other) images after the primary/thumbnail ones. The Exif information is located within the primary image, so I guess the extra images can be safely ignored.

Temporary patch: https://github.com/kamadak/exif-rs/commit/9bc14f6e868b5c09be7fcf2f2d41bdc95027d9d3

Removing thumbnail is not safe, because a crafted file can cause an infinite recursion without the check.

kamadak commented 6 years ago

Fixed in version 0.3.1.