diff src/format_raw.c @ 54:b58cac75ad12

Thu Jun 9 22:23:18 2005 John Ellis <johne@verizon.net> * exif.[ch]: A lot of code clean up, add generic tiff header parser, remove use of packed structures to interpret tiff file format, fix possible endless loops in tiff parser with corrupt IFD tables, and fix possible overflow in jpeg exif parser. * format_canon.[ch]: Add additional makernote values, plus a few spelling fixes. Header update. * format_fuji.[ch]: Header update. * format_nikon.[ch]: Updates to use new tiff parsing utils in exif.c, code cleanup. Header update. * format_raw.[ch]: Add pathname argument to file descriptor version of raw parser to quickly rule out non-raw files based on file extension. Add raw header match type to check for tiff "make" field value. * image-load.c (image_loader_begin): Add image filename for raw parser.
author gqview
date Fri, 10 Jun 2005 02:44:36 +0000
parents 00843150f7c8
children a8c9992320f4
line wrap: on
line diff
--- a/src/format_raw.c	Tue Jun 07 07:55:00 2005 +0000
+++ b/src/format_raw.c	Fri Jun 10 02:44:36 2005 +0000
@@ -39,8 +39,11 @@
 
 typedef struct _FormatRawEntry FormatRawEntry;
 struct _FormatRawEntry {
-	const void *header_pattern;
-	const guint header_length;
+	const gchar *extension;
+	FormatRawMatchType magic_type;
+	const guint magic_offset;
+	const void *magic_pattern;
+	const guint magic_length;
 	const gchar *description;
 	FormatRawParseFunc func_parse;
 };
@@ -49,7 +52,7 @@
 	FORMAT_RAW_CANON,
 	FORMAT_RAW_FUJI,
 	FORMAT_RAW_NIKON,
-	{ NULL, 0, NULL, NULL }
+	{ NULL, 0, 0, NULL, 0, NULL, NULL }
 };
 
 
@@ -70,17 +73,147 @@
 };
 
 
-static FormatRawEntry *format_raw_find(const void *data, const guint len)
+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)
 {
 	gint n;
+	gint tiff;
+	guint make_count = 0;
+	guint make_offset = 0;
+
+	tiff = (len > 8 &&
+		(memcmp(data, "II\x2a\x00", 4) == 0 ||
+		 memcmp(data, "MM\x00\x2a", 4) == 0));
 
 	n = 0;
-	while (format_raw_list[n].header_pattern)
+	while (format_raw_list[n].magic_pattern)
 		{
-		if (format_raw_list[n].header_length <= len &&
-		    memcmp(data, format_raw_list[n].header_pattern, format_raw_list[n].header_length) == 0)
+		FormatRawEntry *entry = &format_raw_list[n];
+
+		switch (entry->magic_type)
 			{
-			return &format_raw_list[n];
+			case FORMAT_RAW_MATCH_MAGIC:
+				if (entry->magic_length + entry->magic_offset <= len &&
+				    memcmp(data + entry->magic_offset,
+					   entry->magic_pattern, entry->magic_length) == 0)
+					{
+					return entry;
+					}
+				break;
+			case FORMAT_RAW_MATCH_TIFF_MAKE:
+				if (tiff &&
+				    make_offset == 0 &&
+				    !format_tiff_find_tag_data(data, len, 0x10f, EXIF_FORMAT_STRING,
+							       &make_offset, &make_count))
+					{
+					tiff = FALSE;
+					}
+				if (make_offset != 0 &&
+				    make_count >= entry->magic_offset + entry->magic_length &&
+				    memcmp(entry->magic_pattern,
+					   data + make_offset + entry->magic_offset, entry->magic_length) == 0)
+					{
+					return entry;
+					}
+				break;
+			default:
+				break;
 			}
 		n++;
 		}
@@ -89,7 +222,7 @@
 }
 
 static gint format_raw_parse(FormatRawEntry *entry,
-			     const void *data, const guint len,
+			     unsigned char *data, const guint len,
 			     guint *image_offset, guint *exif_offset)
 {
 	guint io = 0;
@@ -115,7 +248,7 @@
 	return TRUE;
 }
 
-gint format_raw_img_exif_offsets(const void *data, const guint len,
+gint format_raw_img_exif_offsets(unsigned char *data, const guint len,
 				 guint *image_offset, guint *exif_offset)
 {
 	FormatRawEntry *entry;
@@ -130,7 +263,8 @@
 }
 
 
-gint format_raw_img_exif_offsets_fd(int fd, const void *header_data, const guint header_len,
+gint format_raw_img_exif_offsets_fd(int fd, const gchar *path,
+				    unsigned char *header_data, const guint header_len,
 				    guint *image_offset, guint *exif_offset)
 {
 	FormatRawEntry *entry;
@@ -141,6 +275,33 @@
 
 	if (!header_data || fd < 0) return FALSE;
 
+	/* given image pathname, first do simple (and fast) file extension test */
+	if (path)
+		{
+		const gchar *ext;
+		gint match = FALSE;
+		gint i;
+
+		ext = strrchr(path, '.');
+		if (!ext) return FALSE;
+		ext++;
+
+		i = 0;
+		while (!match && format_raw_list[i].magic_pattern)
+			{
+			if (format_raw_list[i].extension &&
+			    strcasecmp(format_raw_list[i].extension, ext) == 0)
+				{
+				match = TRUE;
+				}
+			i++;
+			}
+
+		if (!match) return FALSE;
+
+		if (debug) printf("RAW file parser extension match\n");
+		}
+
 	entry = format_raw_find(header_data, header_len);
 
 	if (!entry || !entry->func_parse) return FALSE;
@@ -219,7 +380,7 @@
 }
 
 gint format_exif_makernote_parse(ExifData *exif, unsigned char *tiff, guint offset,
-				 guint size, ExifByteOrder byte_order)
+				 guint size, ExifByteOrder bo)
 {
 	FormatExifEntry *entry;
 
@@ -229,7 +390,7 @@
 
 	if (debug) printf("EXIF using makernote parser for %s\n", entry->description);
 
-	return entry->func_parse(exif, tiff, offset, size, byte_order);
+	return entry->func_parse(exif, tiff, offset, size, bo);
 }