+static guint tiff_table(unsigned char *data, const guint len, guint offset, ExifByteOrder bo,
+ guint tag, ExifFormatType type,
+ guint *result_offset, guint *result_count)
+{
+ guint count;
+ guint i;
+
+ if (len < offset + 2) return 0;
+ if (type < 0 || type > EXIF_FORMAT_COUNT) return 0;
+
+ count = exif_byte_get_int16(data + offset, bo);
+ offset += 2;
+ if (len < offset + count * 12 + 4) return 0;
+
+ for (i = 0; i < count; i++)
+ {
+ guint segment;
+
+ segment = offset + i * 12;
+ if (exif_byte_get_int16(data + segment, bo) == tag &&
+ exif_byte_get_int16(data + segment + 2, bo) == type)
+ {
+ guint chunk_count;
+ guint chunk_offset;
+ guint chunk_length;
+
+ chunk_count = exif_byte_get_int32(data + segment + 4, bo);
+ chunk_length = ExifFormatList[type].size * chunk_count;
+
+ if (chunk_length > 4)
+ {
+ chunk_offset = exif_byte_get_int32(data + segment + 8, bo);
+ }
+ else
+ {
+ chunk_offset = segment + 8;
+ }
+
+ if (chunk_offset + chunk_length <= len)
+ {
+ *result_offset = chunk_offset;
+ *result_count = chunk_count;
+ }
+
+ return 0;
+ }
+ }
+
+ return exif_byte_get_int32(data + offset + count * 12, bo);
+}
+
+static gint format_tiff_find_tag_data(unsigned char *data, const guint len,
+ guint tag, ExifFormatType type,
+ guint *result_offset, guint *result_count)
+{
+ ExifByteOrder bo;
+ guint offset;
+
+ if (len < 8) return FALSE;
+
+ if (memcmp(data, "II", 2) == 0)
+ {
+ bo = EXIF_BYTE_ORDER_INTEL;
+ }
+ else if (memcmp(data, "MM", 2) == 0)
+ {
+ bo = EXIF_BYTE_ORDER_MOTOROLA;
+ }
+ else
+ {
+ return FALSE;
+ }
+
+ if (exif_byte_get_int16(data + 2, bo) != 0x002A)
+ {
+ return FALSE;
+ }
+
+ offset = exif_byte_get_int32(data + 4, bo);
+
+ while (offset != 0)
+ {
+ guint ro = 0;
+ guint rc = 0;
+
+ offset = tiff_table(data, len, offset, bo, tag, type, &ro, &rc);
+ if (ro != 0)
+ {
+ *result_offset = ro;
+ *result_count = rc;
+ return TRUE;
+ }
+ }
+
+ return FALSE;
+}
+
+static FormatRawEntry *format_raw_find(unsigned char *data, const guint len)