4 * Copyright (C) 2008 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"
35 #include "format_canon.h"
36 #include "format_fuji.h"
37 #include "format_nikon.h"
38 #include "format_olympus.h"
41 typedef struct _FormatRawEntry FormatRawEntry;
42 struct _FormatRawEntry {
43 const gchar *extension;
44 FormatRawMatchType magic_type;
45 const guint magic_offset;
46 const void *magic_pattern;
47 const guint magic_length;
48 const FormatRawExifType exif_type;
49 FormatRawExifParseFunc exif_func;
50 const gchar *description;
51 FormatRawParseFunc func_parse;
54 static FormatRawEntry format_raw_list[] = {
56 FORMAT_RAW_DEBUG_TIFF,
64 { NULL, 0, 0, NULL, 0, 0, NULL, NULL, NULL }
68 typedef struct _FormatExifEntry FormatExifEntry;
69 struct _FormatExifEntry {
70 FormatExifMatchType header_type;
71 const void *header_pattern;
72 const guint header_length;
73 const gchar *description;
74 FormatExifParseFunc func_parse;
77 static FormatExifEntry format_exif_list[] = {
86 static guint tiff_table(unsigned char *data, const guint len, guint offset, ExifByteOrder bo,
87 guint tag, ExifFormatType type,
88 guint *result_offset, guint *result_count)
93 if (len < offset + 2) return 0;
94 if (type < 0 || type > EXIF_FORMAT_COUNT) return 0;
96 count = exif_byte_get_int16(data + offset, bo);
98 if (len < offset + count * 12 + 4) return 0;
100 for (i = 0; i < count; i++)
104 segment = offset + i * 12;
105 if (exif_byte_get_int16(data + segment, bo) == tag &&
106 exif_byte_get_int16(data + segment + 2, bo) == type)
112 chunk_count = exif_byte_get_int32(data + segment + 4, bo);
113 chunk_length = ExifFormatList[type].size * chunk_count;
115 if (chunk_length > 4)
117 chunk_offset = exif_byte_get_int32(data + segment + 8, bo);
121 chunk_offset = segment + 8;
124 if (chunk_offset + chunk_length <= len)
126 *result_offset = chunk_offset;
127 *result_count = chunk_count;
134 return exif_byte_get_int32(data + offset + count * 12, bo);
137 static gint format_tiff_find_tag_data(unsigned char *data, const guint len,
138 guint tag, ExifFormatType type,
139 guint *result_offset, guint *result_count)
144 if (len < 8) return FALSE;
146 if (memcmp(data, "II", 2) == 0)
148 bo = EXIF_BYTE_ORDER_INTEL;
150 else if (memcmp(data, "MM", 2) == 0)
152 bo = EXIF_BYTE_ORDER_MOTOROLA;
159 if (exif_byte_get_int16(data + 2, bo) != 0x002A)
164 offset = exif_byte_get_int32(data + 4, bo);
171 offset = tiff_table(data, len, offset, bo, tag, type, &ro, &rc);
183 static FormatRawEntry *format_raw_find(unsigned char *data, const guint len)
187 guint make_count = 0;
188 guint make_offset = 0;
191 (memcmp(data, "II\x2a\x00", 4) == 0 ||
192 memcmp(data, "MM\x00\x2a", 4) == 0));
195 while (format_raw_list[n].magic_pattern)
197 FormatRawEntry *entry = &format_raw_list[n];
199 switch (entry->magic_type)
201 case FORMAT_RAW_MATCH_MAGIC:
202 if (entry->magic_length + entry->magic_offset <= len &&
203 memcmp(data + entry->magic_offset,
204 entry->magic_pattern, entry->magic_length) == 0)
209 case FORMAT_RAW_MATCH_TIFF_MAKE:
212 !format_tiff_find_tag_data(data, len, 0x10f, EXIF_FORMAT_STRING,
213 &make_offset, &make_count))
217 if (make_offset != 0 &&
218 make_count >= entry->magic_offset + entry->magic_length &&
219 memcmp(entry->magic_pattern,
220 data + make_offset + entry->magic_offset, entry->magic_length) == 0)
234 static gint format_raw_parse(FormatRawEntry *entry,
235 unsigned char *data, const guint len,
236 guint *image_offset, guint *exif_offset)
242 if (!entry || !entry->func_parse) return FALSE;
244 DEBUG_1("RAW using file parser for %s", entry->description);
246 found = entry->func_parse(data, len, &io, &eo);
255 if (image_offset) *image_offset = io;
256 if (exif_offset) *exif_offset = eo;
261 gint format_raw_img_exif_offsets(unsigned char *data, const guint len,
262 guint *image_offset, guint *exif_offset)
264 FormatRawEntry *entry;
266 if (!data || len < 1) return FALSE;
268 entry = format_raw_find(data, len);
270 if (!entry || !entry->func_parse) return FALSE;
272 return format_raw_parse(entry, data, len, image_offset, exif_offset);
276 FormatRawExifType format_raw_exif_offset(unsigned char *data, const guint len, guint *exif_offset,
277 FormatRawExifParseFunc *exif_parse_func)
279 FormatRawEntry *entry;
281 if (!data || len < 1) return FALSE;
283 entry = format_raw_find(data, len);
285 if (!entry || !entry->func_parse) return FALSE;
287 if (!format_raw_parse(entry, data, len, NULL, exif_offset)) return FORMAT_RAW_EXIF_NONE;
289 if (entry->exif_type == FORMAT_RAW_EXIF_PROPRIETARY && exif_parse_func)
291 *exif_parse_func = entry->exif_func;
294 return entry->exif_type;
298 gint format_raw_img_exif_offsets_fd(int fd, const gchar *path,
299 unsigned char *header_data, const guint header_len,
300 guint *image_offset, guint *exif_offset)
302 FormatRawEntry *entry;
303 void *map_data = NULL;
308 if (!header_data || fd < 0) return FALSE;
310 /* given image pathname, first do simple (and fast) file extension test */
317 ext = strrchr(path, '.');
318 if (!ext) return FALSE;
322 while (!match && format_raw_list[i].magic_pattern)
324 if (format_raw_list[i].extension &&
325 strcasecmp(format_raw_list[i].extension, ext) == 0)
332 if (!match) return FALSE;
334 DEBUG_1("RAW file parser extension match");
338 * when the target is a tiff file it should be mmaped prior to format_raw_find as
339 * the make field data may not always be within header_data + header_len
341 entry = format_raw_find(header_data, header_len);
343 if (!entry || !entry->func_parse) return FALSE;
345 if (fstat(fd, &st) == -1)
347 printf("Failed to stat file %d\n", fd);
350 map_len = st.st_size;
351 map_data = mmap(0, map_len, PROT_READ, MAP_PRIVATE, fd, 0);
352 if (map_data == MAP_FAILED)
354 printf("Failed to mmap file %d\n", fd);
358 success = format_raw_parse(entry, map_data, map_len, image_offset, exif_offset);
360 if (munmap(map_data, map_len) == -1)
362 printf("Failed to unmap file %d\n", fd);
365 if (success && image_offset)
367 if (lseek(fd, *image_offset, SEEK_SET) != *image_offset)
369 printf("Failed to seek to embedded image\n");
372 if (*exif_offset) *exif_offset = 0;
381 static FormatExifEntry *format_exif_makernote_find(ExifData *exif, unsigned char *tiff,
382 guint offset, guint size)
387 make = exif_get_item(exif, "Exif.Image.Make");
390 while (format_exif_list[n].header_pattern)
392 switch (format_exif_list[n].header_type)
394 case FORMAT_EXIF_MATCH_MAKERNOTE:
395 if (format_exif_list[n].header_length + offset < size &&
396 memcmp(tiff + offset, format_exif_list[n].header_pattern,
397 format_exif_list[n].header_length) == 0)
399 return &format_exif_list[n];
402 case FORMAT_EXIF_MATCH_MAKE:
404 make->data_len >= format_exif_list[n].header_length &&
405 memcmp(make->data, format_exif_list[n].header_pattern,
406 format_exif_list[n].header_length) == 0)
408 return &format_exif_list[n];
418 gint format_exif_makernote_parse(ExifData *exif, unsigned char *tiff, guint offset,
419 guint size, ExifByteOrder bo)
421 FormatExifEntry *entry;
423 entry = format_exif_makernote_find(exif, tiff, offset, size);
425 if (!entry || !entry->func_parse) return FALSE;
427 DEBUG_1("EXIF using makernote parser for %s", entry->description);
429 return entry->func_parse(exif, tiff, offset, size, bo);
433 *-----------------------------------------------------------------------------
434 * Basic TIFF debugger, prints all IFD entries within tiff file
435 *-----------------------------------------------------------------------------
439 static guint format_debug_tiff_table(unsigned char *data, const guint len, guint offset,
440 ExifByteOrder bo, gint level);
442 static void format_debug_tiff_entry(unsigned char *data, const guint len, guint offset,
443 ExifByteOrder bo, gint level)
451 tag = exif_byte_get_int16(data + offset + EXIF_TIFD_OFFSET_TAG, bo);
452 type = exif_byte_get_int16(data + offset + EXIF_TIFD_OFFSET_FORMAT, bo);
453 count = exif_byte_get_int32(data + offset + EXIF_TIFD_OFFSET_COUNT, bo);
455 seg_len = ExifFormatList[type].size * count;
458 segment = exif_byte_get_int32(data + offset + EXIF_TIFD_OFFSET_DATA, bo);
459 if (segment + seg_len > len) return;
463 segment = offset + EXIF_TIFD_OFFSET_DATA;
466 printf("%*stag:0x%04X (%05d), type:%2d %9s, len:%6d [%02X %02X %02X %02X] @ offset:%d\n",
467 level, "", tag, tag, type,
468 (type < EXIF_FORMAT_COUNT) ? ExifFormatList[type].short_name : "???", count,
469 data[segment], data[segment + 1], data[segment + 2], data[segment + 3], segment);
471 if (tag == 0x8769 || tag == 0x14a)
475 printf("%*s~~~ found %s table\n", level, "", (tag == 0x14a) ? "subIFD" : "EXIF" );
477 for (i = 0; i < count; i++)
481 subset = exif_byte_get_int32(data + segment + i * 4, bo);
482 format_debug_tiff_table(data, len, subset, bo, level + 1);
485 else if (tag == 0x8773 && type == EXIF_FORMAT_UNDEFINED)
487 printf("%*s~~~ found ICC color profile at offset %d, length %d\n", level, "", segment, seg_len);
489 else if (tag == 0x201 && (type == EXIF_FORMAT_LONG_UNSIGNED || type == EXIF_FORMAT_LONG))
491 guint subset = exif_byte_get_int32(data + segment, bo);
492 printf("%*s~~~ found jpeg data at offset %d\n", level, "", subset);
494 else if (tag == 0x202 && (type == EXIF_FORMAT_LONG_UNSIGNED || type == EXIF_FORMAT_LONG))
496 guint subset = exif_byte_get_int32(data + segment, bo);
497 printf("%*s~~~ found jpeg data length of %d\n", level, "", subset);
501 static guint format_debug_tiff_table(unsigned char *data, const guint len, guint offset,
502 ExifByteOrder bo, gint level)
507 if (level > EXIF_TIFF_MAX_LEVELS) return 0;
509 if (len < offset + 2) return FALSE;
511 count = exif_byte_get_int16(data + offset, bo);
513 if (len < offset + count * EXIF_TIFD_SIZE + 4) return 0;
515 printf("%*s== tiff table #%d has %d entries ==\n", level, "", level, count);
517 for (i = 0; i < count; i++)
519 format_debug_tiff_entry(data, len, offset + i * EXIF_TIFD_SIZE, bo, level);
522 printf("%*s----------- end of #%d ------------\n", level, "", level);
524 return exif_byte_get_int32(data + offset + count * EXIF_TIFD_SIZE, bo);
527 gint format_debug_tiff_raw(unsigned char *data, const guint len,
528 guint *image_offset, guint *exif_offset)
534 if (len < 8) return FALSE;
536 /* for debugging, we are more relaxed as to magic header */
537 if (memcmp(data, "II", 2) == 0)
539 bo = EXIF_BYTE_ORDER_INTEL;
541 else if (memcmp(data, "MM", 2) == 0)
543 bo = EXIF_BYTE_ORDER_MOTOROLA;
550 printf("*** debug parsing tiff\n");
552 offset = exif_byte_get_int32(data + 4, bo);
554 while (offset && level < EXIF_TIFF_MAX_LEVELS)
556 offset = format_debug_tiff_table(data, len, offset, bo, 0);
562 /* we are debugging, not trying to return any data */