FFD8
and FFD9
is the start and end of a JPEG image (or an EXIF thumbnail), not an EXIF chunk. EXIF data in JPEGs is stored inside a chunk marked by the FFE1
maker, there is no end marker. You have to read the next 2 bytes to get the EXIF data length and read as much.
For example, ffe1 1b61 4578 6966
, where 1b61
is the amount of bytes in the EXIF data chunk. 7009 bytes, once you've read these you can stop. This is the only correct way.
Some hints may be available to you to check whether you're on the correct path or not (no off by 1 errors in reading etc.). Since EXIF thumbnails are JPEGs they will end with FFD9
as well, and a thumbnail entry will very probably be the last entry inside the EXIF chunk. So the EXIF chunk will very probably end with a FFD9
. A new FFEx
marker will follow, this is the start of a completely different chunk.
Again: do not search for FFD9
and stop, though, since the EXIF chunk may contain several thumbnails which end in FFD9
, get the header size and read accordingly.
This is for JPEG. In other image formats EXIF will be stored differently.