private IFDEntry ParseMakernote(ushort tag, ushort type, uint count, long base_offset, uint offset) { long makernote_offset = base_offset + offset; IFDStructure ifd_structure = new IFDStructure(); // This is the minimum size a makernote should have // The shortest header is PENTAX_HEADER (4) // + IFD entry count (2) // + at least one IFD etry (12) // + next IFD pointer (4) // = 22 .... // we use this number to read a header which is big used // to identify the makernote types int header_size = 18; long length = 0; try { length = file.Length; } catch (Exception) { // Use a safety-value of 4 gigabyte. length = 1073741824L * 4; } if (makernote_offset > length) { file.MarkAsCorrupt("offset to makernote is beyond file size"); return(null); } if (makernote_offset + header_size > length) { file.MarkAsCorrupt("data is to short to contain a maker note ifd"); return(null); } // read header file.Seek(makernote_offset, SeekOrigin.Begin); ByteVector header = file.ReadBlock(header_size); if (header.StartsWith(PANASONIC_HEADER)) { IFDReader reader = new IFDReader(file, is_bigendian, ifd_structure, base_offset, offset + 12, max_offset); reader.ReadIFD(base_offset, offset + 12, max_offset); return(new MakernoteIFDEntry(tag, ifd_structure, MakernoteType.Panasonic, PANASONIC_HEADER, 12, true, null)); } if (header.StartsWith(PENTAX_HEADER)) { IFDReader reader = new IFDReader(file, is_bigendian, ifd_structure, base_offset, offset + 6, max_offset); reader.ReadIFD(base_offset, offset + 6, max_offset); return(new MakernoteIFDEntry(tag, ifd_structure, MakernoteType.Pentax, header.Mid(0, 6), 6, true, null)); } if (header.StartsWith(OLYMPUS1_HEADER)) { IFDReader reader = new IFDReader(file, is_bigendian, ifd_structure, base_offset, offset + 8, max_offset); reader.Read(); return(new MakernoteIFDEntry(tag, ifd_structure, MakernoteType.Olympus1, header.Mid(0, 8), 8, true, null)); } if (header.StartsWith(OLYMPUS2_HEADER)) { IFDReader reader = new IFDReader(file, is_bigendian, ifd_structure, makernote_offset, 12, count); reader.Read(); return(new MakernoteIFDEntry(tag, ifd_structure, MakernoteType.Olympus2, header.Mid(0, 12), 12, false, null)); } if (header.StartsWith(SONY_HEADER)) { IFDReader reader = new IFDReader(file, is_bigendian, ifd_structure, base_offset, offset + 12, max_offset); reader.ReadIFD(base_offset, offset + 12, max_offset); return(new MakernoteIFDEntry(tag, ifd_structure, MakernoteType.Sony, SONY_HEADER, 12, true, null)); } if (header.StartsWith(NIKON_HEADER)) { ByteVector endian_bytes = header.Mid(10, 2); if (endian_bytes.ToString() == "II" || endian_bytes.ToString() == "MM") { bool makernote_endian = endian_bytes.ToString().Equals("MM"); ushort magic = header.Mid(12, 2).ToUShort(is_bigendian); if (magic == 42) { // TODO: the max_offset value is not correct here. However, some nikon files have offsets to a sub-ifd // (preview image) which are not stored with the other makernote data. Therfore, we keep the max_offset // for now. (It is just an upper bound for some checks. So if it is too big, it doesn't matter) var reader = new Nikon3MakernoteReader(file, makernote_endian, ifd_structure, makernote_offset + 10, 8, max_offset - offset - 10); reader.Read(); return(new MakernoteIFDEntry(tag, ifd_structure, MakernoteType.Nikon3, header.Mid(0, 18), 8, false, makernote_endian)); } } } if (header.StartsWith(LEICA_HEADER)) { IFDReader reader = new IFDReader(file, is_bigendian, ifd_structure, makernote_offset, 8, count); reader.Read(); return(new MakernoteIFDEntry(tag, ifd_structure, MakernoteType.Leica, header.Mid(0, 8), 10, false, null)); } try { IFDReader reader = new IFDReader(file, is_bigendian, ifd_structure, base_offset, offset, max_offset); reader.Read(); return(new MakernoteIFDEntry(tag, ifd_structure, MakernoteType.Canon)); } catch { return(null); } }
/// <summary> /// Try to parse the given IFD entry, used to discover format-specific entries. /// </summary> /// <param name="tag"> /// A <see cref="System.UInt16"/> with the tag of the entry. /// </param> /// <param name="type"> /// A <see cref="System.UInt16"/> with the type of the entry. /// </param> /// <param name="count"> /// A <see cref="System.UInt32"/> with the data count of the entry. /// </param> /// <param name="base_offset"> /// A <see cref="System.Int64"/> with the base offset which every offsets in the /// IFD are relative to. /// </param> /// <param name="offset"> /// A <see cref="System.UInt32"/> with the offset of the entry. /// </param> /// <returns> /// A <see cref="IFDEntry"/> with the given parameters, or null if none was parsed, after /// which the normal TIFF parsing is used. /// </returns> protected virtual IFDEntry ParseIFDEntry(ushort tag, ushort type, uint count, long base_offset, uint offset) { if (tag == (ushort)ExifEntryTag.MakerNote && parse_makernote) { return(ParseMakernote(tag, type, count, base_offset, offset)); } if (tag == (ushort)IFDEntryTag.SubIFDs) { var entries = new List <IFDStructure>(); uint[] data; if (count >= 2) { // This is impossible right? if (base_offset + offset > file.Length) { file.MarkAsCorrupt("Length of SubIFD is too long"); return(null); } file.Seek(base_offset + offset, SeekOrigin.Begin); data = ReadUIntArray(count); } else { data = new uint[] { offset }; } foreach (var sub_offset in data) { var sub_structure = new IFDStructure(); var sub_reader = CreateSubIFDReader(file, is_bigendian, sub_structure, base_offset, sub_offset, max_offset); sub_reader.Read(); entries.Add(sub_structure); } return(new SubIFDArrayEntry(tag, entries)); } IFDStructure ifd_structure = new IFDStructure(); IFDReader reader = CreateSubIFDReader(file, is_bigendian, ifd_structure, base_offset, offset, max_offset); // Sub IFDs are either identified by the IFD-type ... if (type == (ushort)IFDEntryType.IFD) { reader.Read(); return(new SubIFDEntry(tag, type, (uint)ifd_structure.Directories.Length, ifd_structure)); } // ... or by one of the following tags switch (tag) { case (ushort)IFDEntryTag.ExifIFD: case (ushort)IFDEntryTag.InteroperabilityIFD: case (ushort)IFDEntryTag.GPSIFD: reader.Read(); return(new SubIFDEntry(tag, (ushort)IFDEntryType.Long, 1, ifd_structure)); default: return(null); } }