4 * Copyright (C) 2008 - 2012 The Geeqie Team
7 * Original version 2005 Lars Ellenberg, base on dcraw by David coffin.
9 * This software is released under the GNU General Public License (GNU GPL).
10 * Please read the included file COPYING for more information.
11 * This software comes with no warranty of any kind, use at your own risk!
23 #include <sys/types.h>
32 #include "format_raw.h"
34 #include "format_canon.h"
35 #include "format_fuji.h"
36 #include "format_nikon.h"
37 #include "format_olympus.h"
40 typedef struct _FormatRawEntry FormatRawEntry;
41 struct _FormatRawEntry {
42 const gchar *extension;
43 FormatRawMatchType magic_type;
44 const guint magic_offset;
45 gconstpointer magic_pattern;
46 const guint magic_length;
47 const FormatRawExifType exif_type;
48 FormatRawExifParseFunc exif_func;
49 const gchar *description;
50 FormatRawParseFunc func_parse;
53 static FormatRawEntry format_raw_list[] = {
55 FORMAT_RAW_DEBUG_TIFF,
63 { NULL, 0, 0, NULL, 0, 0, NULL, NULL, NULL }
67 typedef struct _FormatExifEntry FormatExifEntry;
68 struct _FormatExifEntry {
69 FormatExifMatchType header_type;
70 gconstpointer header_pattern;
71 const guint header_length;
72 const gchar *description;
73 FormatExifParseFunc func_parse;
76 static FormatExifEntry format_exif_list[] = {
81 { 0, NULL, 0, NULL, NULL }
85 static guint tiff_table(guchar *data, const guint len, guint offset, ExifByteOrder bo,
86 guint tag, ExifFormatType type,
87 guint *result_offset, guint *result_count)
92 if (len < offset + 2) return 0;
93 if (type > EXIF_FORMAT_COUNT) return 0;
95 count = exif_byte_get_int16(data + offset, bo);
97 if (len < offset + count * 12 + 4) return 0;
99 for (i = 0; i < count; i++)
103 segment = offset + i * 12;
104 if (exif_byte_get_int16(data + segment, bo) == tag &&
105 exif_byte_get_int16(data + segment + 2, bo) == type)
111 chunk_count = exif_byte_get_int32(data + segment + 4, bo);
112 chunk_length = ExifFormatList[type].size * chunk_count;
114 if (chunk_length > 4)
116 chunk_offset = exif_byte_get_int32(data + segment + 8, bo);
120 chunk_offset = segment + 8;
123 if (chunk_offset + chunk_length <= len)
125 *result_offset = chunk_offset;
126 *result_count = chunk_count;
133 return exif_byte_get_int32(data + offset + count * 12, bo);
136 static gboolean format_tiff_find_tag_data(guchar *data, const guint len,
137 guint tag, ExifFormatType type,
138 guint *result_offset, guint *result_count)
143 if (len < 8) return FALSE;
145 if (memcmp(data, "II", 2) == 0)
147 bo = EXIF_BYTE_ORDER_INTEL;
149 else if (memcmp(data, "MM", 2) == 0)
151 bo = EXIF_BYTE_ORDER_MOTOROLA;
158 if (exif_byte_get_int16(data + 2, bo) != 0x002A)
163 offset = exif_byte_get_int32(data + 4, bo);
170 offset = tiff_table(data, len, offset, bo, tag, type, &ro, &rc);
182 static FormatRawEntry *format_raw_find(guchar *data, const guint len)
186 guint make_count = 0;
187 guint make_offset = 0;
190 (memcmp(data, "II\x2a\x00", 4) == 0 ||
191 memcmp(data, "MM\x00\x2a", 4) == 0));
194 while (format_raw_list[n].magic_pattern)
196 FormatRawEntry *entry = &format_raw_list[n];
198 switch (entry->magic_type)
200 case FORMAT_RAW_MATCH_MAGIC:
201 if (entry->magic_length + entry->magic_offset <= len &&
202 memcmp(data + entry->magic_offset,
203 entry->magic_pattern, entry->magic_length) == 0)
208 case FORMAT_RAW_MATCH_TIFF_MAKE:
211 !format_tiff_find_tag_data(data, len, 0x10f, EXIF_FORMAT_STRING,
212 &make_offset, &make_count))
216 if (make_offset != 0 &&
217 make_count >= entry->magic_offset + entry->magic_length &&
218 memcmp(entry->magic_pattern,
219 data + make_offset + entry->magic_offset, entry->magic_length) == 0)
233 static gboolean format_raw_parse(FormatRawEntry *entry,
234 guchar *data, const guint len,
235 guint *image_offset, guint *exif_offset)
241 if (!entry || !entry->func_parse) return FALSE;
243 DEBUG_1("RAW using file parser for %s", entry->description);
245 found = entry->func_parse(data, len, &io, &eo);
254 if (image_offset) *image_offset = io;
255 if (exif_offset) *exif_offset = eo;
260 gboolean format_raw_img_exif_offsets(guchar *data, const guint len,
261 guint *image_offset, guint *exif_offset)
263 FormatRawEntry *entry;
265 if (!data || len < 1) return FALSE;
267 entry = format_raw_find(data, len);
269 if (!entry || !entry->func_parse) return FALSE;
271 return format_raw_parse(entry, data, len, image_offset, exif_offset);
275 FormatRawExifType format_raw_exif_offset(guchar *data, const guint len, guint *exif_offset,
276 FormatRawExifParseFunc *exif_parse_func)
278 FormatRawEntry *entry;
280 if (!data || len < 1) return FALSE;
282 entry = format_raw_find(data, len);
284 if (!entry || !entry->func_parse) return FALSE;
286 if (!format_raw_parse(entry, data, len, NULL, exif_offset)) return FORMAT_RAW_EXIF_NONE;
288 if (entry->exif_type == FORMAT_RAW_EXIF_PROPRIETARY && exif_parse_func)
290 *exif_parse_func = entry->exif_func;
293 return entry->exif_type;
297 gboolean format_raw_img_exif_offsets_fd(gint fd, const gchar *path,
298 guchar *header_data, const guint header_len,
299 guint *image_offset, guint *exif_offset)
301 FormatRawEntry *entry;
302 gpointer map_data = NULL;
307 if (!header_data || fd < 0) return FALSE;
309 /* given image pathname, first do simple (and fast) file extension test */
313 gboolean match = FALSE;
316 ext = strrchr(path, '.');
317 if (!ext) return FALSE;
321 while (!match && format_raw_list[i].magic_pattern)
323 if (format_raw_list[i].extension &&
324 g_ascii_strcasecmp(format_raw_list[i].extension, ext) == 0)
331 if (!match) return FALSE;
333 DEBUG_1("RAW file parser extension match");
337 * when the target is a tiff file it should be mmaped prior to format_raw_find as
338 * the make field data may not always be within header_data + header_len
340 entry = format_raw_find(header_data, header_len);
342 if (!entry || !entry->func_parse) return FALSE;
344 if (fstat(fd, &st) == -1)
346 log_printf("Failed to stat file %d\n", fd);
349 map_len = st.st_size;
350 map_data = mmap(0, map_len, PROT_READ, MAP_PRIVATE, fd, 0);
351 if (map_data == MAP_FAILED)
353 log_printf("Failed to mmap file %d\n", fd);
357 success = format_raw_parse(entry, map_data, map_len, image_offset, exif_offset);
359 if (munmap(map_data, map_len) == -1)
361 log_printf("Failed to unmap file %d\n", fd);
364 if (success && image_offset)
366 if (lseek(fd, *image_offset, SEEK_SET) != (off_t) *image_offset)
368 log_printf("Failed to seek to embedded image\n");
371 if (*exif_offset) *exif_offset = 0;
380 static FormatExifEntry *format_exif_makernote_find(ExifData *exif, guchar *tiff,
381 guint offset, guint size)
386 make = exif_get_item(exif, "Exif.Image.Make");
389 while (format_exif_list[n].header_pattern)
391 switch (format_exif_list[n].header_type)
393 case FORMAT_EXIF_MATCH_MAKERNOTE:
394 if (format_exif_list[n].header_length + offset < size &&
395 memcmp(tiff + offset, format_exif_list[n].header_pattern,
396 format_exif_list[n].header_length) == 0)
398 return &format_exif_list[n];
401 case FORMAT_EXIF_MATCH_MAKE:
403 make->data_len >= format_exif_list[n].header_length &&
404 memcmp(make->data, format_exif_list[n].header_pattern,
405 format_exif_list[n].header_length) == 0)
407 return &format_exif_list[n];
417 gboolean format_exif_makernote_parse(ExifData *exif, guchar *tiff, guint offset,
418 guint size, ExifByteOrder bo)
420 FormatExifEntry *entry;
422 entry = format_exif_makernote_find(exif, tiff, offset, size);
424 if (!entry || !entry->func_parse) return FALSE;
426 DEBUG_1("EXIF using makernote parser for %s", entry->description);
428 return entry->func_parse(exif, tiff, offset, size, bo);
432 *-----------------------------------------------------------------------------
433 * Basic TIFF debugger, prints all IFD entries within tiff file
434 *-----------------------------------------------------------------------------
438 static guint format_debug_tiff_table(guchar *data, const guint len, guint offset,
439 ExifByteOrder bo, gint level);
441 static void format_debug_tiff_entry(guchar *data, const guint len, guint offset,
442 ExifByteOrder bo, gint level)
450 tag = exif_byte_get_int16(data + offset + EXIF_TIFD_OFFSET_TAG, bo);
451 type = exif_byte_get_int16(data + offset + EXIF_TIFD_OFFSET_FORMAT, bo);
452 count = exif_byte_get_int32(data + offset + EXIF_TIFD_OFFSET_COUNT, bo);
454 seg_len = ExifFormatList[type].size * count;
457 segment = exif_byte_get_int32(data + offset + EXIF_TIFD_OFFSET_DATA, bo);
458 if (segment + seg_len > len) return;
462 segment = offset + EXIF_TIFD_OFFSET_DATA;
465 log_printf("%*stag:0x%04X (%05d), type:%2d %9s, len:%6d [%02X %02X %02X %02X] @ offset:%d\n",
466 level, "", tag, tag, type,
467 (type < EXIF_FORMAT_COUNT) ? ExifFormatList[type].short_name : "???", count,
468 data[segment], data[segment + 1], data[segment + 2], data[segment + 3], segment);
470 if (tag == 0x8769 || tag == 0x14a)
474 log_printf("%*s~~~ found %s table\n", level, "", (tag == 0x14a) ? "subIFD" : "EXIF" );
476 for (i = 0; i < count; i++)
480 subset = exif_byte_get_int32(data + segment + i * 4, bo);
481 format_debug_tiff_table(data, len, subset, bo, level + 1);
484 else if (tag == 0x8773 && type == EXIF_FORMAT_UNDEFINED)
486 log_printf("%*s~~~ found ICC color profile at offset %d, length %d\n", level, "", segment, seg_len);
488 else if (tag == 0x201 && (type == EXIF_FORMAT_LONG_UNSIGNED || type == EXIF_FORMAT_LONG))
490 guint subset = exif_byte_get_int32(data + segment, bo);
491 log_printf("%*s~~~ found jpeg data at offset %d\n", level, "", subset);
493 else if (tag == 0x202 && (type == EXIF_FORMAT_LONG_UNSIGNED || type == EXIF_FORMAT_LONG))
495 guint subset = exif_byte_get_int32(data + segment, bo);
496 log_printf("%*s~~~ found jpeg data length of %d\n", level, "", subset);
500 static guint format_debug_tiff_table(guchar *data, const guint len, guint offset,
501 ExifByteOrder bo, gint level)
506 if (level > EXIF_TIFF_MAX_LEVELS) return 0;
508 if (len < offset + 2) return FALSE;
510 count = exif_byte_get_int16(data + offset, bo);
512 if (len < offset + count * EXIF_TIFD_SIZE + 4) return 0;
514 log_printf("%*s== tiff table #%d has %d entries ==\n", level, "", level, count);
516 for (i = 0; i < count; i++)
518 format_debug_tiff_entry(data, len, offset + i * EXIF_TIFD_SIZE, bo, level);
521 log_printf("%*s----------- end of #%d ------------\n", level, "", level);
523 return exif_byte_get_int32(data + offset + count * EXIF_TIFD_SIZE, bo);
526 gboolean format_debug_tiff_raw(guchar *data, const guint len,
527 guint *image_offset, guint *exif_offset)
533 if (len < 8) return FALSE;
535 /* for debugging, we are more relaxed as to magic header */
536 if (memcmp(data, "II", 2) == 0)
538 bo = EXIF_BYTE_ORDER_INTEL;
540 else if (memcmp(data, "MM", 2) == 0)
542 bo = EXIF_BYTE_ORDER_MOTOROLA;
549 log_printf("*** debug parsing tiff\n");
551 offset = exif_byte_get_int32(data + 4, bo);
553 while (offset && level < EXIF_TIFF_MAX_LEVELS)
555 offset = format_debug_tiff_table(data, len, offset, bo, 0);
559 log_printf("*** end\n");
561 /* we are debugging, not trying to return any data */
568 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */